Skip to content

添加thread和futex机制#411

Merged
fslongjin merged 28 commits intoDragonOS-Community:masterfrom
GnoCiYeH:patch-thread
Nov 1, 2023
Merged

添加thread和futex机制#411
fslongjin merged 28 commits intoDragonOS-Community:masterfrom
GnoCiYeH:patch-thread

Conversation

@GnoCiYeH
Copy link
Member

主要

  • 实现线程机制,主要是体现在clone系统调用和arch_prctl系统调用的实现
  • 初步实现futex机制,因为目前优先级以及共享内存机制未实现,futex只做了一个初步版本,其中对于arch的部分代码可能有问题

次要

为了适配musl,实现了一下几个系统调用

  • get_random:与linux语义不一致,属于是暴力实现,因为我们没有其他的随机源
  • stat:即为fstat改写
  • set_tid_address

BUG修复

  • 修改一些内存对齐计算上的错误
  • 修改出现内存已映射的问题(边界条件)


#[allow(dead_code)]
pub mod define {
pub const FUTEX_WAIT: u32 = 0;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这些要分开

}

bitflags! {
pub struct GRandFlags: u8{
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个与arch无关


fn do_stat(path: &str) -> Result<PosixKstat, SystemError> {
// 文件名过长
if path.len() > MAX_PATHLEN as usize {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里先open再用fstat的,然后再close

Comment on lines +293 to +294
child_trapframe.rbp = usp.data() as u64 + 11 * core::mem::size_of::<usize>() as u64;
child_trapframe.rsp = usp.data() as u64;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这为啥是11

arg2: usize,
from_user: bool,
) -> Result<usize, SystemError> {
let mut arch_info = pcb.arch_info();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里要irq save

let uaddr2 = args[4] as *const u32;
let val3 = args[5] as u32;

let mut timespec: *mut TimeSpec = core::ptr::null::<TimeSpec>() as *mut TimeSpec;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里有内存越界问题,应当写成

let mut timespec = TimeSpec::default();

这样才有充足的空间。

另外,这里的所有裸指针最好都进行检验,能够拷贝的小的参数都先拷贝了。

}

#[derive(Debug)]
pub enum Provider {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个需要加注释,不然下回就不知道是啥了


// TODO: 处理CLONE_PARENT 与 SIGNAL_UNKILLABLE的情况

// 如果新进程使用不同的 pid 或用户名空间,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

namespace

unimplemented!()
}

SYS_POLL => Ok(0),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这些都加warning

fn run(&self) {
let r = self.0.lock().timer_func.run();
let mut timer = self.0.lock();
timer.timeout = true;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

改为triggered更直观

let mut address = uaddr as u64;

// 计算相对页的偏移量
let offset = address % MMArch::PAGE_SIZE as u64;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

改为位运算address & (MMArch::PAGE_SIZE -1)

use super::constant::*;

lazy_static! {
pub(super) static ref FUTEX_DATA: SpinLock<HashMap<FutexKey, LockedFutexHashBucket>> =
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

用了全局锁的话,里面LockedFutexHashBucket就不需要lock的了,因为它不是多进程/线程共享

pub fn cancel(&self) -> bool {
TIMER_LIST
.lock()
.drain_filter(|x| Arc::<Timer>::as_ptr(&x) == self as *const Timer);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Arc::ptr_eq(this, other)

// 将user_tid_addr置0
if thread.clear_child_tid.is_some() {
let _ =
unsafe { clear_user(thread.clear_child_tid.unwrap(), core::mem::size_of::<i32>()) };
Copy link
Member

@fslongjin fslongjin Oct 29, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个应该是要在线程的exit的地方执行,而不是这个drop函数

Suggested change
unsafe { clear_user(thread.clear_child_tid.unwrap(), core::mem::size_of::<i32>()) };
unsafe { clear_user(thread.clear_child_tid.unwrap(), core::mem::size_of::<i32>()) };

Hdksg10 and others added 2 commits October 31, 2023 15:19
* 测试RESET

* 测试RESET

* 基于轮询的实现

* 规范化部分unsafe的使用

* 完成中断处理函数,同时去除了不必要的内存拷贝行为,准备编写napi机制

* 实现现有协议栈下的部分napi机制;修复了内存泄漏的问题;添加了一部分代码注释

* 去除部分无用代码

* 去除一些无用代码

* 适配新的驱动模型

* 完成msi中断测试

* 去除一些无用代码

* 格式化代码

* 增加了一些注释,提高代码可读性

* 去除无关文件

* 优化了读取mac地址的方式,提高可读性
@fslongjin fslongjin merged commit 971462b into DragonOS-Community:master Nov 1, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants