alnyan/yggdrasil: change how args are passed from the kernel

This commit is contained in:
2023-11-14 11:51:31 +02:00
parent 44a0cc2a48
commit d3a8b67604
2 changed files with 43 additions and 29 deletions
+41 -28
View File
@@ -1,5 +1,6 @@
use crate::ffi::OsString; use crate::ffi::OsString;
use crate::fmt; use crate::fmt;
use crate::mem::size_of;
use crate::str::FromStr; use crate::str::FromStr;
#[allow(dead_code)] #[allow(dead_code)]
@@ -12,15 +13,10 @@ pub struct Args {
// Argument structure layout: // Argument structure layout:
// //
// 0x000: args len // 0x000: args pointer
// 0x008: arg ptr 0 // 0x008: envs pointer
// 0x010: arg len 0
// 0x018: arg ptr 1
// 0x020: arg len 1
// ...
// .....: arg data 0
// .....: arg data 1
static mut ARGS: usize = 0; static mut ARGS: usize = 0;
static mut ENVS: usize = 0;
pub fn args() -> Args { pub fn args() -> Args {
let base = unsafe { ARGS }; let base = unsafe { ARGS };
@@ -31,12 +27,33 @@ pub fn args() -> Args {
let len = unsafe { let len = unsafe {
#[allow(fuzzy_provenance_casts)] #[allow(fuzzy_provenance_casts)]
(base as *mut usize).read() (base as *const usize).read()
}; };
Args { base, len, index: 0 } Args { base, len, index: 0 }
} }
impl Args {
const fn item(base: usize, index: usize) -> usize {
base + (/* array len */1 + /* pair */ 2 * index) * size_of::<usize>()
}
fn get(&self, index: usize) -> &'static str {
assert!(index < self.len);
let len = unsafe {
#[allow(fuzzy_provenance_casts)]
(Self::item(self.base, index) as *const usize).read()
};
let ptr = unsafe {
#[allow(fuzzy_provenance_casts)]
((Self::item(self.base, index) + size_of::<usize>()) as *const *const u8).read()
};
let slice = unsafe { core::slice::from_raw_parts(ptr, len) };
core::str::from_utf8(slice).unwrap()
}
}
impl fmt::Debug for Args { impl fmt::Debug for Args {
fn fmt(&self, _f: &mut fmt::Formatter<'_>) -> fmt::Result { fn fmt(&self, _f: &mut fmt::Formatter<'_>) -> fmt::Result {
todo!() todo!()
@@ -51,26 +68,12 @@ impl Iterator for Args {
return None; return None;
} }
let ptr_ptr_addr = self.base + (1 + self.index * 2) * 8; let str = self.get(self.index);
let arg_ptr = unsafe {
#[allow(fuzzy_provenance_casts)]
(ptr_ptr_addr as *mut usize).read()
};
let arg_len = unsafe {
#[allow(fuzzy_provenance_casts)]
((ptr_ptr_addr + 8) as *mut usize).read()
};
let slice = unsafe {
#[allow(fuzzy_provenance_casts)]
crate::slice::from_raw_parts(arg_ptr as *const u8, arg_len)
};
let arg_str = crate::str::from_utf8(slice).unwrap();
let arg_os_string = OsString::from_str(arg_str).unwrap();
self.index += 1; self.index += 1;
Some(arg_os_string) let os_str = OsString::from_str(str).unwrap();
Some(os_str)
} }
} }
@@ -87,5 +90,15 @@ impl DoubleEndedIterator for Args {
} }
pub unsafe fn init(program_arg: usize) { pub unsafe fn init(program_arg: usize) {
ARGS = program_arg; #[repr(C)]
struct Header {
args_ptr: usize,
envs_ptr: usize,
};
#[allow(fuzzy_provenance_casts)]
let hdr = program_arg as *const Header;
ARGS = (*hdr).args_ptr;
ENVS = (*hdr).envs_ptr;
} }
+2 -1
View File
@@ -88,7 +88,8 @@ impl FileType {
} }
pub fn is_symlink(&self) -> bool { pub fn is_symlink(&self) -> bool {
todo!() // TODO: symlinks NYI
false
} }
} }