diff --git a/kernel/src/arch/riscv/process.rs b/kernel/src/arch/riscv/process.rs index c78ea63fc..ebbb69f8c 100644 --- a/kernel/src/arch/riscv/process.rs +++ b/kernel/src/arch/riscv/process.rs @@ -127,6 +127,9 @@ pub struct InitialProcess { /// Address of the top of the stack pub sp: usize, + + /// Address of the environment block + pub env: usize, } impl InitialProcess { diff --git a/kernel/src/services.rs b/kernel/src/services.rs index 9c1a91ac3..71c5aec9b 100644 --- a/kernel/src/services.rs +++ b/kernel/src/services.rs @@ -31,8 +31,6 @@ const MINIELF_FLG_NC: u8 = 2; const MINIELF_FLG_X: u8 = 4; #[cfg(baremetal)] const MINIELF_FLG_EHF: u8 = 8; -#[cfg(baremetal)] -const MINIELF_FLG_EHH: u8 = 0x10; #[derive(Debug, Copy, Clone, PartialEq)] pub struct ExceptionHandler { @@ -414,25 +412,26 @@ impl SystemServices { // - Process #1 is the kernel // Any changes to this will break this code! let mut eh_frame = 0; - let mut eh_frame_size = 0; - let mut eh_frame_header = 0; - let mut eh_frame_header_size = 0; if let Some(arg) = arg_iter.next() { for section in arg.data.chunks_exact(2) { let flags = (section[1] >> 24) as u8; if flags & MINIELF_FLG_EHF != 0 { eh_frame = section[0]; - eh_frame_size = section[1] & 0xFF_FFFF; - } else if flags & MINIELF_FLG_EHH != 0 { - eh_frame_header = section[0]; - eh_frame_header_size = section[1] & 0xFF_FFFF; } } } + let arg0 = eh_frame as _; + #[cfg(target_arch = "riscv32")] + let arg1 = init.env as _; + #[cfg(not(target_arch = "riscv32"))] + let arg1 = 0; + let arg2 = 0; + let arg3 = 0; + // end of assumption area process.state = ProcessState::Setup(ThreadInit::new( - unsafe { core::mem::transmute::(init.entrypoint) }, + init.entrypoint as _, unsafe { MemoryRange::new( init.sp - crate::arch::process::DEFAULT_STACK_SIZE, @@ -440,10 +439,10 @@ impl SystemServices { ) .unwrap() }, - eh_frame as _, - eh_frame_size as _, - eh_frame_header as _, - eh_frame_header_size as _, + arg0, + arg1, + arg2, + arg3, )); } // log_process_update(file!(), line!(), process, old_state); diff --git a/loader/src/consts.rs b/loader/src/consts.rs index f96e46244..154e81a26 100644 --- a/loader/src/consts.rs +++ b/loader/src/consts.rs @@ -1,6 +1,7 @@ pub const BACKUP_ARGS_ADDR: usize = crate::platform::RAM_BASE + crate::platform::RAM_SIZE - 0x2000; pub const USER_STACK_TOP: usize = 0x8000_0000; +pub const USER_STACK_PADDING: usize = 16; pub const PAGE_TABLE_OFFSET: usize = 0xff40_0000; pub const PAGE_TABLE_ROOT_OFFSET: usize = 0xff80_0000; pub const CONTEXT_OFFSET: usize = 0xff80_1000; diff --git a/loader/src/minielf.rs b/loader/src/minielf.rs index d61ebc9b6..5ce806f87 100644 --- a/loader/src/minielf.rs +++ b/loader/src/minielf.rs @@ -95,6 +95,7 @@ impl MiniElf { allocator: &mut BootConfig, load_offset: usize, pid: XousPid, + params: &[u8], xip: bool, ) -> usize { println!("Mapping PID {} starting at offset {:08x}", pid, load_offset); @@ -121,7 +122,7 @@ impl MiniElf { // The load offset is the end of this process. Shift it down by one page // so we get the start of the first page. let mut top = load_offset - PAGE_SIZE; - let stack_addr = USER_STACK_TOP - 16; + let stack_addr = USER_STACK_TOP - USER_STACK_PADDING; // Allocate a page to handle the top-level memory translation #[cfg(not(feature = "atsama5d27"))] @@ -183,6 +184,12 @@ impl MiniElf { if i == 0 { // For the initial stack frame, allocate a valid page let sp_page = allocator.alloc() as usize; + // Copy the params block into this first page + let sp_page_slice = unsafe { slice::from_raw_parts_mut(sp_page as *mut u8, PAGE_SIZE) }; + let params_start = sp_page_slice.len() - params.len(); + sp_page_slice[params_start..].copy_from_slice(params); + + // Attach the page to the process allocator.map_page( tt, sp_page, @@ -377,7 +384,8 @@ impl MiniElf { let process = &mut allocator.processes[pid as usize - 1]; process.entrypoint = self.entry_point as usize; - process.sp = stack_addr; + process.sp = (stack_addr - params.len()) & !0xf; + process.env = stack_addr - params.len() + USER_STACK_PADDING; #[cfg(not(feature = "atsama5d27"))] { process.satp = 0x8000_0000 | ((pid as usize) << 22) | (_tt_address >> 12); diff --git a/loader/src/phase1.rs b/loader/src/phase1.rs index 19db4b085..14bc23c12 100644 --- a/loader/src/phase1.rs +++ b/loader/src/phase1.rs @@ -17,6 +17,9 @@ pub struct InitialProcess { /// Address of the top of the stack pub sp: usize, + + /// Address of the start of the env block + pub env: usize, } /// Phase 1: diff --git a/loader/src/phase2.rs b/loader/src/phase2.rs index baf3ba02c..3d2910356 100644 --- a/loader/src/phase2.rs +++ b/loader/src/phase2.rs @@ -11,6 +11,36 @@ pub fn phase_2(cfg: &mut BootConfig) { let mut process_offset = cfg.sram_start as usize + cfg.sram_size - cfg.init_size; println!("\n\nPhase2: Processess start out @ {:08x}", process_offset); + // Construct an environment block. This will be used by processes + // to access environment variables and system parameters. This + // is hardcoded for now, and can be expanded later. + #[rustfmt::skip] + let mut env = [ + 0x41, 0x70, 0x70, 0x50, // 'AppP' indicating application parameters + 0x08, 0x00, 0x00, 0x00, // Size of AppP tag contents + 0xb2, 0x00, 0x00, 0x00, // Size of entire AppP block including all tags + 0x02, 0x00, 0x00, 0x00, // Number of tags present + 0x45, 0x6e, 0x76, 0x42, // 'EnvB' indicating an environment block + 0x9a, 0x00, 0x00, 0x00, // Number of bytes that follows for the environment block + 0x01, 0x00, // Number of environment variables + 0x14, 0x00, // Length of name of first variable + // Name of first variable 'ROOT_FILESYSTEM_HASH': + 0x52, 0x4f, 0x4f, 0x54, 0x5f, 0x46, 0x49, 0x4c, 0x45, 0x53, 0x59, 0x53, 0x54, 0x45, 0x4d, + 0x5f, 0x48, 0x41, 0x53, 0x48, + // Length of the contents of the first variable + 0x80, 0x00, + // Root filesystem hash contents begin here: + 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, + 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, + 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, + 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, + 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, + 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, + 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, + 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, + 0x30, 0x41, 0x42, 0x43, 0x44, 0x45, 0x30, 0x30, + ]; + // Go through all Init processes and the kernel, setting up their // page tables and mapping memory to them. let mut pid = 2; @@ -34,14 +64,14 @@ pub fn phase_2(cfg: &mut BootConfig) { if tag.name == u32::from_le_bytes(*b"IniE") { let inie = MiniElf::new(&tag); println!("\n\nCopying IniE program into memory"); - let allocated = inie.load(cfg, process_offset, pid, false); + let allocated = inie.load(cfg, process_offset, pid, &env, false); println!("IniE Allocated {:x}", allocated); process_offset -= allocated; pid += 1; } else if tag.name == u32::from_le_bytes(*b"IniF") { let inif = MiniElf::new(&tag); println!("\n\nMapping IniF program into memory"); - let allocated = inif.load(cfg, process_offset, pid, true); + let allocated = inif.load(cfg, process_offset, pid, &env, true); println!("IniF Allocated {:x}", allocated); process_offset -= allocated; pid += 1; @@ -260,6 +290,8 @@ impl ProgramDescription { STACK_PAGE_COUNT } { if i == 0 { + // Pre-allocate the first stack offset, since it + // will definitely be used let sp_page = allocator.alloc() as usize; allocator.map_page( satp, @@ -358,6 +390,7 @@ impl ProgramDescription { let process = &mut allocator.processes[pid_idx]; process.entrypoint = self.entrypoint as usize; process.sp = stack_addr; + process.env = 0; process.satp = 0x8000_0000 | ((pid as usize) << 22) | (satp_address >> 12); } }