From 6c4958b3e677a0a0df58b39474530c94ef7ae211 Mon Sep 17 00:00:00 2001 From: MemoryShore <1353318529@qq.com> Date: Fri, 30 Aug 2024 15:15:47 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E5=96=84=E7=94=A8=E6=88=B7=E6=A0=88?= =?UTF-8?q?=E7=9A=84=E5=AD=97=E8=8A=82=E5=AF=B9=E9=BD=90=E6=9C=BA=E5=88=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- kernel/src/arch/x86_64/process/syscall.rs | 10 ++-------- kernel/src/process/exec.rs | 17 ++++++++++------- 2 files changed, 12 insertions(+), 15 deletions(-) diff --git a/kernel/src/arch/x86_64/process/syscall.rs b/kernel/src/arch/x86_64/process/syscall.rs index add3c6692..7fe4944cf 100644 --- a/kernel/src/arch/x86_64/process/syscall.rs +++ b/kernel/src/arch/x86_64/process/syscall.rs @@ -74,7 +74,7 @@ impl Syscall { // 生成16字节随机数 // TODO 暂时设为0 - param.init_info_mut().rand_num = 0; + param.init_info_mut().rand_num = [0u8; 16]; // 把proc_init_info写到用户栈上 let mut ustack_message = unsafe { @@ -87,13 +87,7 @@ impl Syscall { let (user_sp, argv_ptr) = unsafe { param .init_info_mut() - .push_at( - // address_space - // .write() - // .user_stack_mut() - // .expect("No user stack found"), - &mut ustack_message, - ) + .push_at(&mut ustack_message) .expect("Failed to push proc_init_info to user stack") }; address_space.write().user_stack = Some(ustack_message); diff --git a/kernel/src/process/exec.rs b/kernel/src/process/exec.rs index 7535a9f49..9d0cba351 100644 --- a/kernel/src/process/exec.rs +++ b/kernel/src/process/exec.rs @@ -206,7 +206,7 @@ pub struct ProcInitInfo { pub args: Vec, pub envs: Vec, pub auxv: BTreeMap, - pub rand_num: u128, + pub rand_num: [u8; 16], } impl ProcInitInfo { @@ -216,7 +216,7 @@ impl ProcInitInfo { args: Vec::new(), envs: Vec::new(), auxv: BTreeMap::new(), - rand_num: 0, + rand_num: [0u8; 16], } } @@ -258,11 +258,14 @@ impl ProcInitInfo { self.auxv .insert(super::abi::AtType::Random as u8, ustack.sp().data()); - // 实现栈的16字节对齐,如果argv、envp、argc在栈中的长度加起来不能对齐16字节,则手动填充一个空字节 - // TODO 感觉这样对齐有点简陋,后续可能要完善一下 - if (envps.len() + argps.len() + 1) * core::mem::align_of::() % 0x10 != 0 { - self.push_slice(ustack, &[0u8; 1])?; - } + // 实现栈的16字节对齐 + // 用当前栈顶地址减去后续要压栈的长度,得到的压栈后的栈顶地址与0xF按位与操作得到对齐要填充的字节数 + let length_to_push = (self.auxv.len() + envps.len() + 1 + argps.len() + 1 + 1) + * core::mem::align_of::(); + self.push_slice( + ustack, + &vec![0u8; (ustack.sp().data() - length_to_push) & 0xF], + )?; // 压入auxv self.push_slice(ustack, &[null::(), null::()])?;