From 8592352552d9048121ed38200182904cec1c6333 Mon Sep 17 00:00:00 2001 From: Sean Cross Date: Fri, 29 Dec 2023 18:26:23 +0800 Subject: [PATCH 01/11] kernel: mem: add function for verifying mappings Add a function to ensure mappings are unique across the whole system. This takes a very long time and can crash if the watchdog timer isn't paused. Signed-off-by: Sean Cross --- kernel/src/mem.rs | 135 ++++++++++++++++++++++++++++++++++------------ 1 file changed, 102 insertions(+), 33 deletions(-) diff --git a/kernel/src/mem.rs b/kernel/src/mem.rs index 5c38f90fe..5f1a76720 100644 --- a/kernel/src/mem.rs +++ b/kernel/src/mem.rs @@ -254,51 +254,19 @@ impl MemoryManager { // println!("Allocating page for PID {}", pid); unsafe { let end_point = self.ram_size / PAGE_SIZE; - let starting_point = self.last_ram_page; + let starting_point = self.last_ram_page.max(end_point); for (allocation, index) in MEMORY_ALLOCATIONS[starting_point..end_point] .iter_mut() .zip(starting_point..) .chain(MEMORY_ALLOCATIONS[..starting_point].iter_mut().zip(0..)) { - // println!( - // " Checking {:08x}...", - // index * PAGE_SIZE + self.ram_start as usize - // ); if allocation.is_none() { *allocation = Some(pid); self.last_ram_page = index + 1; - // if self.last_ram_page >= end_point { - // self.last_ram_page = 0; - // } let page = index * PAGE_SIZE + self.ram_start; return Ok(page); } } - // for (index, allocation) in MEMORY_ALLOCATIONS - // [self.last_ram_page..((self.ram_size as usize) / PAGE_SIZE)] - // .iter_mut() - // .enumerate() - // { - // println!(" 1. Checking {:08x}...", (self.last_ram_page + index) * PAGE_SIZE + self.ram_start as usize); - // if allocation.is_none() { - // *allocation = Some(pid); - // self.last_ram_page = self.last_ram_page + index + 1; - // let page = (self.last_ram_page + index) * PAGE_SIZE + self.ram_start; - // return Ok(page); - // } - // } - // for (index, allocation) in MEMORY_ALLOCATIONS[..self.last_ram_page] - // .iter_mut() - // .enumerate() - // { - // println!(" 2. Checking {:08x}...", index * PAGE_SIZE + self.ram_start as usize); - // if allocation.is_none() { - // *allocation = Some(pid); - // self.last_ram_page = index + 1; - // let page = index * PAGE_SIZE + self.ram_start; - // return Ok(page); - // } - // } } Err(xous_kernel::Error::OutOfMemory) } @@ -873,6 +841,107 @@ impl MemoryManager { Ok(()) } + + #[cfg(baremetal)] + pub fn check_for_duplicates(&self) { + use crate::services::SystemServices; + + SystemServices::with(|system_services| { + let current_pid = system_services.current_pid(); + + // Actiavte the debugging process and iterate through it, + // noting down each active thread. + for phys in (self.ram_start..self.ram_start + self.ram_size).step_by(PAGE_SIZE) { + let mut owner = None; + for pid in 1..crate::services::MAX_PROCESS_COUNT { + let pid = PID::new(pid as u8).unwrap(); + let Ok(process) = system_services.get_process(pid) else { + continue; + }; + let Ok(_) = process.activate() else { + continue; + }; + match MemoryMapping::current().phys_to_virt(phys) { + Err(e) => { + println!("!!! ERROR {:?} !!!", e); + continue; + } + Ok(None) => continue, + Ok(Some(virt)) => { + let allocation_offset = (phys - self.ram_start) / PAGE_SIZE; + let existing_owner = unsafe { MEMORY_ALLOCATIONS[allocation_offset] }; + if existing_owner != Some(pid) { + let is_lent = { + if let Some(existing_owner) = existing_owner { + system_services + .get_process(existing_owner) + .unwrap() + .activate() + .unwrap(); + let is_lent = if let Ok(Some(owned_address)) = + MemoryMapping::current().phys_to_virt(phys) + { + crate::arch::mem::page_is_lent(owned_address as *mut u8) + } else { + false + }; + system_services + .get_process(pid) + .unwrap() + .activate() + .unwrap(); + is_lent + } else { + false + } + }; + println!( + "!!! 0x{:08x} is owned by {} ({}) but is mapped to {} ({}) -- {}", + phys, + existing_owner.map(|v| v.get() as isize).unwrap_or(-1), + existing_owner + .map(|v| system_services + .process_name(v) + .unwrap_or("")) + .unwrap_or(""), + pid.get(), + system_services.process_name(pid).unwrap_or(""), + if is_lent { "page is lent" } else { "duplicate!" }, + ); + } + if !crate::arch::mem::page_is_lent(virt as *mut u8) { + if owner.is_none() { + owner = Some((pid, virt)); + } else { + println!( + "!!! DUPLICATE !!! Page {:08x} owned by both {} ({}) @ {:08x} and {} ({}) @ {:08x}", + phys, + owner.map(|v| v.0.get() as isize).unwrap_or(-1), + owner + .map(|v| system_services + .process_name(v.0) + .unwrap_or("")) + .unwrap_or(""), + owner.map(|v| v.1).unwrap_or(0), + pid.get(), + system_services.process_name(pid).unwrap_or(""), + virt, + ); + } + } + } + } + } + } + + // Restore the previous PID + system_services + .get_process(current_pid) + .unwrap() + .activate() + .unwrap(); + }) + } } pub unsafe fn bzero(mut start: *mut T, end: *mut T) From 13d54b7928854209b38ff44dd5f9968491f1c603 Mon Sep 17 00:00:00 2001 From: Sean Cross Date: Fri, 29 Dec 2023 18:30:37 +0800 Subject: [PATCH 02/11] kernel: debug: add "check memory" command This command ensures memory is all owned by the correct process and is unique in its ownership. It takes a long time to run, so ensure the watchdog timer is disabled. Signed-off-by: Sean Cross --- kernel/src/debug/shell.rs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/kernel/src/debug/shell.rs b/kernel/src/debug/shell.rs index 181654f0c..b33e9cf75 100644 --- a/kernel/src/debug/shell.rs +++ b/kernel/src/debug/shell.rs @@ -100,6 +100,14 @@ fn handle_character(b: u8) { }); }); } + #[cfg(all(baremetal, target_arch = "riscv32"))] + b'k' => { + println!("Checking RAM for duplicate pages (this will take a few minutes)"); + crate::mem::MemoryManager::with(|mm| { + mm.check_for_duplicates(); + }); + println!("Check complete"); + } b'm' => { println!("Printing memory page tables"); crate::services::SystemServices::with(|system_services| { @@ -228,6 +236,8 @@ fn print_help() { println!("--- + -----------------------"); println!(" h | print this message"); println!(" i | print irq handlers"); + #[cfg(all(baremetal, target_arch = "riscv32"))] + println!(" k | check RAM to make sure pages are unique"); println!(" m | print MMU page tables of all processes"); println!(" p | print all processes"); println!(" P | print all processes and threads"); From 6749f24b311fc54a456d6e9306d343bd0c52a90d Mon Sep 17 00:00:00 2001 From: Sean Cross Date: Fri, 29 Dec 2023 18:31:51 +0800 Subject: [PATCH 03/11] fixup kernel/mem.rs Signed-off-by: Sean Cross --- kernel/src/mem.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/kernel/src/mem.rs b/kernel/src/mem.rs index 5f1a76720..3c80e834f 100644 --- a/kernel/src/mem.rs +++ b/kernel/src/mem.rs @@ -842,7 +842,7 @@ impl MemoryManager { Ok(()) } - #[cfg(baremetal)] + #[cfg(all(baremetal, target_arch = "riscv32"))] pub fn check_for_duplicates(&self) { use crate::services::SystemServices; From 2e88fb0620b37232402d6490e0592b4f2adb1b7c Mon Sep 17 00:00:00 2001 From: Sean Cross Date: Fri, 29 Dec 2023 18:32:17 +0800 Subject: [PATCH 04/11] kernel: services: don't crash when a process quits When enumerating processes, the `find_process()` process didn't consult whether a process was actually free. This resulted in a crash, particularly when attaching a debugger to a crashed process. Signed-off-by: Sean Cross --- kernel/src/services.rs | 44 +++++++++++++----------------------------- 1 file changed, 13 insertions(+), 31 deletions(-) diff --git a/kernel/src/services.rs b/kernel/src/services.rs index 10b941958..c3fed4c23 100644 --- a/kernel/src/services.rs +++ b/kernel/src/services.rs @@ -509,47 +509,29 @@ impl SystemServices { pub fn get_process(&self, pid: PID) -> Result<&Process, xous_kernel::Error> { // PID0 doesn't exist -- process IDs are offset by 1. let pid_idx = pid.get() as usize - 1; - if cfg!(baremetal) && self.processes[pid_idx].mapping.get_pid() != pid { - klog!( - "Process doesn't match ({} vs {})", - self.processes[pid_idx].mapping.get_pid(), - pid - ); - return Err(xous_kernel::Error::ProcessNotFound); + if cfg!(baremetal) && self.processes[pid_idx].mapping.get_pid() != Some(pid) { + Err(xous_kernel::Error::ProcessNotFound) + } else if self.processes[pid_idx].state == ProcessState::Free { + Err(xous_kernel::Error::ProcessNotFound) + } else { + Ok(&self.processes[pid_idx]) } - Ok(&self.processes[pid_idx]) } pub fn get_process_mut(&mut self, pid: PID) -> Result<&mut Process, xous_kernel::Error> { // PID0 doesn't exist -- process IDs are offset by 1. let pid_idx = pid.get() as usize - 1; - - // if self.processes[pid_idx].mapping.get_pid() != pid { - // println!( - // "Process doesn't match ({} vs {})", - // self.processes[pid_idx].mapping.get_pid(), - // pid - // ); - // return Err(xous_kernel::Error::ProcessNotFound); - // } - Ok(&mut self.processes[pid_idx]) + if cfg!(baremetal) && self.processes[pid_idx].mapping.get_pid() != Some(pid) { + Err(xous_kernel::Error::ProcessNotFound) + } else if self.processes[pid_idx].state == ProcessState::Free { + Err(xous_kernel::Error::ProcessNotFound) + } else { + Ok(&mut self.processes[pid_idx]) + } } pub fn current_pid(&self) -> PID { arch::process::current_pid() - // PID0 doesn't exist -- process IDs are offset by 1. - // assert_eq!( - // self.processes[pid as usize - 1].mapping, - // MemoryMapping::current(), - // "process memory map doesn't match -- current_pid: {}", - // pid - // ); - // assert_eq!( - // pid, self.pid, - // "current pid {} doesn't match arch pid: {}", - // self.pid, pid - // ); - // pid } /// Create a stack frame in the specified process and jump to it. From 2941066c3d314fe22d5c35f3250d872b7a42b1df Mon Sep 17 00:00:00 2001 From: Sean Cross Date: Fri, 29 Dec 2023 18:33:40 +0800 Subject: [PATCH 05/11] loader: don't reserve the "susres" page The "susres" page should be owned by the "susres" server and not the kernel. Avoid mapping this page to the kernel to prevent it from getting double-mapped. Signed-off-by: Sean Cross --- loader/src/consts.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/loader/src/consts.rs b/loader/src/consts.rs index 847241af6..f96e46244 100644 --- a/loader/src/consts.rs +++ b/loader/src/consts.rs @@ -13,4 +13,4 @@ pub const KERNEL_STACK_TOP: usize = 0xfff8_0000; pub const KERNEL_LOAD_OFFSET: usize = 0xffd0_0000; pub const KERNEL_STACK_PAGE_COUNT: usize = 1; pub const KERNEL_ARGUMENT_OFFSET: usize = 0xffc0_0000; -pub const GUARD_MEMORY_BYTES: usize = 2 * crate::PAGE_SIZE; +pub const GUARD_MEMORY_BYTES: usize = 3 * crate::PAGE_SIZE; From 5260ef9f5aa2c0488d55c1592a0eea3ffb0b113e Mon Sep 17 00:00:00 2001 From: Sean Cross Date: Fri, 29 Dec 2023 18:35:07 +0800 Subject: [PATCH 06/11] loader: add pid to `map_page()` call Add the pid as the page owner to `map_page()` calls. This ensures pages are owned by their respective processes, and removes a lot of `change_owner()` calls that immediately follow page mapping. It also allows for superpages to be owned by their respective processes. Signed-off-by: Sean Cross --- loader/src/bootconfig.rs | 29 ++++++++++++--- loader/src/minielf.rs | 20 +++++------ loader/src/phase2.rs | 24 ++++++++----- loader/src/platform/atsama5d27/boot.rs | 50 ++++++++++++++++++++++---- loader/src/platform/atsama5d27/load.rs | 13 +++---- 5 files changed, 100 insertions(+), 36 deletions(-) diff --git a/loader/src/bootconfig.rs b/loader/src/bootconfig.rs index 5a14c89e8..b3585e16a 100644 --- a/loader/src/bootconfig.rs +++ b/loader/src/bootconfig.rs @@ -148,21 +148,42 @@ impl BootConfig { /// # Panics /// /// * If you try to map a page twice - pub fn map_page(&mut self, root: &mut PageTable, phys: usize, virt: usize, flags: usize) { + pub fn map_page( + &mut self, + root: &mut PageTable, + phys: usize, + virt: usize, + flags: usize, + owner: XousPid, + ) { if VDBG { println!( " map pa {:x} -> va {:x} (satp {:x})", phys, virt, root as *mut PageTable as u32 ); } + assert!( + !(phys == 0 && flags & FLG_VALID != 0), + "cannot map zero page" + ); + if flags & FLG_VALID != 0 { + self.change_owner(owner, phys); + } match WORD_SIZE { - 4 => self.map_page_32(root, phys, virt, flags), + 4 => self.map_page_32(root, phys, virt, flags, owner), 8 => panic!("map_page doesn't work on 64-bit devices"), _ => panic!("unrecognized word size: {}", WORD_SIZE), } } - pub fn map_page_32(&mut self, root: &mut PageTable, phys: usize, virt: usize, flags: usize) { + pub fn map_page_32( + &mut self, + root: &mut PageTable, + phys: usize, + virt: usize, + flags: usize, + owner: XousPid, + ) { let ppn1 = (phys >> 22) & ((1 << 12) - 1); let ppn0 = (phys >> 12) & ((1 << 10) - 1); let ppo = (phys) & ((1 << 12) - 1); @@ -225,8 +246,8 @@ impl BootConfig { addr.get(), PAGE_TABLE_OFFSET + vpn1 * PAGE_SIZE, FLG_R | FLG_W | FLG_VALID, + owner, ); - self.change_owner(1 as XousPid, addr.get()); if VDBG { println!("<<< Done mapping new address"); } diff --git a/loader/src/minielf.rs b/loader/src/minielf.rs index 25332940e..d61ebc9b6 100644 --- a/loader/src/minielf.rs +++ b/loader/src/minielf.rs @@ -129,12 +129,12 @@ impl MiniElf { let tt_address = allocator.alloc() as usize; let tt = unsafe { &mut *(tt_address as *mut PageTable) }; - allocator.change_owner(pid as XousPid, tt_address); allocator.map_page( tt, tt_address, PAGE_TABLE_ROOT_OFFSET, FLG_R | FLG_W | FLG_VALID, + pid as XousPid, ); (tt, tt_address) @@ -156,6 +156,7 @@ impl MiniElf { tt_address + offset, PAGE_TABLE_ROOT_OFFSET + offset, FLG_R | FLG_W | FLG_VALID, + pid as XousPid, ); } @@ -173,8 +174,8 @@ impl MiniElf { thread_address, CONTEXT_OFFSET, FLG_R | FLG_W | FLG_VALID, + pid, ); - allocator.change_owner(pid as XousPid, thread_address as usize); // Allocate stack pages. println!(" Stack"); @@ -187,8 +188,8 @@ impl MiniElf { sp_page, (stack_addr - PAGE_SIZE * i) & !(PAGE_SIZE - 1), FLG_U | FLG_R | FLG_W | FLG_VALID, + pid, ); - allocator.change_owner(pid as XousPid, sp_page); } else { // Reserve every other stack page other than the 1st page. allocator.map_page( @@ -196,6 +197,7 @@ impl MiniElf { 0, (stack_addr - PAGE_SIZE * i) & !(PAGE_SIZE - 1), FLG_U | FLG_R | FLG_W, + pid, ); } } @@ -242,8 +244,7 @@ impl MiniElf { if VDBG { println!("1 {:08x} -> {:08x}", top as usize, this_page); } - allocator.map_page(tt, top as usize, this_page, flag_defaults); - allocator.change_owner(pid as XousPid, top as usize); + allocator.map_page(tt, top as usize, this_page, flag_defaults, pid as XousPid); allocated_bytes += PAGE_SIZE; top -= PAGE_SIZE; this_page += PAGE_SIZE; @@ -291,8 +292,7 @@ impl MiniElf { if VDBG { println!("2 {:08x} -> {:08x}", top as usize, this_page); } - allocator.map_page(tt, top as usize, this_page, flag_defaults); - allocator.change_owner(pid as XousPid, top as usize); + allocator.map_page(tt, top as usize, this_page, flag_defaults, pid as XousPid); allocated_bytes += PAGE_SIZE; top -= PAGE_SIZE; this_page += PAGE_SIZE; @@ -305,8 +305,7 @@ impl MiniElf { if VDBG { println!("3 {:08x} -> {:08x}", top as usize, this_page); } - allocator.map_page(tt, top as usize, this_page, flag_defaults); - allocator.change_owner(pid as XousPid, top as usize); + allocator.map_page(tt, top as usize, this_page, flag_defaults, pid as XousPid); allocated_bytes += PAGE_SIZE; top -= PAGE_SIZE; } @@ -362,9 +361,8 @@ impl MiniElf { while pages_to_map > 0 { let map_phys_addr = (image_phys_base + section_map_phys_offset) & !(PAGE_SIZE - 1); - allocator.map_page(tt, map_phys_addr, virt_page, flag_defaults); + allocator.map_page(tt, map_phys_addr, virt_page, flag_defaults, pid as XousPid); last_mapped_xip = virt_page; - allocator.change_owner(pid as XousPid, top as usize); section_map_phys_offset += PAGE_SIZE; virt_page += PAGE_SIZE; diff --git a/loader/src/phase2.rs b/loader/src/phase2.rs index 2b1e03e18..baf3ba02c 100644 --- a/loader/src/phase2.rs +++ b/loader/src/phase2.rs @@ -97,8 +97,8 @@ pub fn phase_2(cfg: &mut BootConfig) { addr + krn_struct_start, addr + KERNEL_ARGUMENT_OFFSET, FLG_R | FLG_W | FLG_VALID, + 1 as XousPid, ); - cfg.change_owner(1 as XousPid, (addr + krn_struct_start) as usize); } // Copy the kernel's "MMU Page 1023" into every process. @@ -240,8 +240,8 @@ impl ProgramDescription { satp_address, PAGE_TABLE_ROOT_OFFSET, FLG_R | FLG_W | FLG_VALID, + pid as XousPid, ); - allocator.change_owner(pid as XousPid, satp_address as usize); // Allocate context for this process let thread_address = allocator.alloc() as usize; @@ -250,8 +250,8 @@ impl ProgramDescription { thread_address, CONTEXT_OFFSET, FLG_R | FLG_W | FLG_VALID, + pid as XousPid, ); - allocator.change_owner(pid as XousPid, thread_address as usize); // Allocate stack pages. for i in 0..if is_kernel { @@ -266,8 +266,8 @@ impl ProgramDescription { sp_page, (stack_addr - PAGE_SIZE * i) & !(PAGE_SIZE - 1), flag_defaults, + pid as XousPid, ); - allocator.change_owner(pid as XousPid, sp_page); } else { // Reserve every page other than the 1st stack page allocator.map_page( @@ -275,6 +275,7 @@ impl ProgramDescription { 0, (stack_addr - PAGE_SIZE * i) & !(PAGE_SIZE - 1), flag_defaults & !FLG_VALID, + pid as XousPid, ); } @@ -286,8 +287,8 @@ impl ProgramDescription { sp_page, (EXCEPTION_STACK_TOP - 16 - PAGE_SIZE * i) & !(PAGE_SIZE - 1), flag_defaults, + pid as XousPid, ); - allocator.change_owner(pid as XousPid, sp_page); } } @@ -318,8 +319,8 @@ impl ProgramDescription { load_offset + offset + rounded_data_bss, self.text_offset as usize + offset, flag_defaults | FLG_X | FLG_VALID, + pid as XousPid, ); - allocator.change_owner(pid as XousPid, load_offset + offset + rounded_data_bss); } // Map the process data section into RAM. @@ -337,16 +338,21 @@ impl ProgramDescription { load_offset + offset, self.data_offset as usize + offset, flag_defaults, + pid as XousPid, ); - allocator.change_owner(pid as XousPid, load_offset as usize + offset); } // Allocate pages for .bss, if necessary // Our "earlyprintk" equivalent if cfg!(feature = "earlyprintk") && is_kernel { - allocator.map_page(satp, 0xF000_2000, 0xffcf_0000, FLG_R | FLG_W | FLG_VALID); - allocator.change_owner(pid as XousPid, 0xF000_2000); + allocator.map_page( + satp, + 0xF000_2000, + 0xffcf_0000, + FLG_R | FLG_W | FLG_VALID, + pid as XousPid, + ); } let process = &mut allocator.processes[pid_idx]; diff --git a/loader/src/platform/atsama5d27/boot.rs b/loader/src/platform/atsama5d27/boot.rs index 8a5ab6501..dc89aea92 100644 --- a/loader/src/platform/atsama5d27/boot.rs +++ b/loader/src/platform/atsama5d27/boot.rs @@ -166,7 +166,15 @@ impl BootConfig { phys: usize, virt: usize, flags: usize, + pid: XousPid, ) { + assert!( + !(phys == 0 && flags & FLG_VALID != 0), + "cannot map zero page" + ); + if flags & FLG_VALID != 0 { + self.change_owner(owner, phys); + } match WORD_SIZE { 4 => self.map_page_32(translation_table, phys, virt, flags), 8 => panic!("map_page doesn't work on 64-bit devices"), @@ -180,6 +188,7 @@ impl BootConfig { phys: usize, virt: usize, flags: usize, + pid: XousPid, ) { dprintln!( "PageTable: {:p} {:08x}", @@ -320,8 +329,8 @@ impl BootConfig { addr.get(), page_virt_addr, FLG_R | FLG_W | FLG_VALID, + pid, ); - self.change_owner(1 as XousPid, addr.get()); dprintln!("<<< Done mapping new address"); } } @@ -348,6 +357,7 @@ pub fn map_structs_to_kernel(cfg: &mut BootConfig, table_addr: usize, krn_struct LOADER_CODE_ADDRESS, LOADER_CODE_ADDRESS, FLG_R | FLG_X | FLG_VALID, + 1 as XousPid, ); // Map the last stack page (4K) to the kernel to make it visible from the trampoline code @@ -358,6 +368,7 @@ pub fn map_structs_to_kernel(cfg: &mut BootConfig, table_addr: usize, krn_struct 0x200ff000, 0x200ff000, FLG_R | FLG_X | FLG_VALID, + 1 as XousPid, ); // Identity map the page that contains kernel arguments @@ -367,6 +378,7 @@ pub fn map_structs_to_kernel(cfg: &mut BootConfig, table_addr: usize, krn_struct 0x20100000, 0x20100000, FLG_R | FLG_X | FLG_VALID, + 1 as XousPid, ); for addr in (0..cfg.init_size - GUARD_MEMORY_BYTES).step_by(PAGE_SIZE) { @@ -375,8 +387,8 @@ pub fn map_structs_to_kernel(cfg: &mut BootConfig, table_addr: usize, krn_struct addr + krn_struct_start, addr + KERNEL_ARGUMENT_OFFSET, FLG_R | FLG_W | FLG_VALID, + 1 as XousPid, ); - cfg.change_owner(1 as XousPid, addr + krn_struct_start); } } @@ -412,7 +424,13 @@ pub fn map_kernel_to_processes( translation_table as usize, virt, phys ); - cfg.map_page(translation_table, phys, virt, FLG_VALID | FLG_R | FLG_X); + cfg.map_page( + translation_table, + phys, + virt, + FLG_VALID | FLG_R | FLG_X, + 1 as XousPid, + ); } println!("Mapping kernel (PID1) data to process PID{}", process.asid); println!("Offset: {:08x}, size: {:08x}", kdata_offset, kdata_size); @@ -424,7 +442,13 @@ pub fn map_kernel_to_processes( translation_table as usize, virt, phys ); - cfg.map_page(translation_table, phys, virt, FLG_VALID | FLG_R | FLG_W); + cfg.map_page( + translation_table, + phys, + virt, + FLG_VALID | FLG_R | FLG_W, + 1 as XousPid, + ); } println!( @@ -440,7 +464,13 @@ pub fn map_kernel_to_processes( "MAP ({:08x}): {:08x} -> {:08x}", translation_table as usize, virt, phys ); - cfg.map_page(translation_table, phys, virt, FLG_VALID | FLG_R | FLG_W); + cfg.map_page( + translation_table, + phys, + virt, + FLG_VALID | FLG_R | FLG_W, + 1 as XousPid, + ); } println!("Mapping irq stack page to the process PID{}", process.asid); @@ -450,7 +480,13 @@ pub fn map_kernel_to_processes( "MAP ({:08x}): {:08x} -> {:08x}", translation_table as usize, virt, phys ); - cfg.map_page(translation_table, phys, virt, FLG_VALID | FLG_R | FLG_W); + cfg.map_page( + translation_table, + phys, + virt, + FLG_VALID | FLG_R | FLG_W, + 1 as XousPid, + ); // TODO: For now, make the UART visible for all the processes. // Later on we may reuse the mapping made by the kernel. @@ -467,6 +503,7 @@ pub fn map_kernel_to_processes( phys, virt, FLG_VALID | FLG_R | FLG_W | FLG_X, + 1 as XousPid, ); } @@ -477,6 +514,7 @@ pub fn map_kernel_to_processes( addr + krn_struct_start, addr + KERNEL_ARGUMENT_OFFSET, FLG_R | FLG_W | FLG_VALID, + 1 as XousPid, ); cfg.change_owner(1 as XousPid, addr + krn_struct_start); } diff --git a/loader/src/platform/atsama5d27/load.rs b/loader/src/platform/atsama5d27/load.rs index c407c24f3..a28a1f319 100644 --- a/loader/src/platform/atsama5d27/load.rs +++ b/loader/src/platform/atsama5d27/load.rs @@ -97,8 +97,8 @@ impl ProgramDescription { tt_address + offset, PAGE_TABLE_ROOT_OFFSET + offset, FLG_R | FLG_W | FLG_VALID, + pid as XousPid, ); - allocator.change_owner(pid as XousPid, tt_address); } // Allocate context for this process @@ -112,8 +112,8 @@ impl ProgramDescription { thread_address, CONTEXT_OFFSET, FLG_R | FLG_W | FLG_VALID, + pid as XousPid, ); - allocator.change_owner(pid as XousPid, thread_address); // Allocate stack pages. let total_stack_pages = if is_kernel { @@ -137,8 +137,8 @@ impl ProgramDescription { sp_page, (stack_addr - PAGE_SIZE * i) & !(PAGE_SIZE - 1), flag_defaults, + pid as XousPid, ); - allocator.change_owner(pid as XousPid, sp_page); } else { // Reserve every page other than the 1st stack page allocator.map_page( @@ -146,6 +146,7 @@ impl ProgramDescription { 0, (stack_addr - PAGE_SIZE * i) & !(PAGE_SIZE - 1), flag_defaults & !FLG_VALID, + pid as XousPid, ); } @@ -162,8 +163,8 @@ impl ProgramDescription { sp_page, (EXCEPTION_STACK_TOP - 16 - PAGE_SIZE * i) & !(PAGE_SIZE - 1), flag_defaults, + pid as XousPid, ); - allocator.change_owner(pid as XousPid, sp_page); } } @@ -183,8 +184,8 @@ impl ProgramDescription { sp_page, (IRQ_STACK_TOP - 16 - PAGE_SIZE * i) & !(PAGE_SIZE - 1), flag_defaults, + pid as XousPid, ); - allocator.change_owner(pid as XousPid, sp_page); } } @@ -216,8 +217,8 @@ impl ProgramDescription { load_offset + offset + rounded_data_bss, self.text_offset as usize + offset, flag_defaults | FLG_X | FLG_VALID, + pid as XousPid, ); - allocator.change_owner(pid as XousPid, load_offset + offset + rounded_data_bss); } // Map the process data section into RAM. From 08abed53bdcf6118c754467a68eb7ba70960eecc Mon Sep 17 00:00:00 2001 From: Sean Cross Date: Fri, 29 Dec 2023 18:36:31 +0800 Subject: [PATCH 07/11] emulation: ec: add new riscv suffixes for csr/fence The riscv foundation changed the descriptor of riscv cores. We must now add "_zicsr_zifencei" to the "rv32i" designation in order to ensure we have access to CSRs and the "FENCE.I" instruction. Signed-off-by: Sean Cross --- emulation/ec/betrusted-ec.repl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/emulation/ec/betrusted-ec.repl b/emulation/ec/betrusted-ec.repl index e1918ef07..5ac00d7ec 100644 --- a/emulation/ec/betrusted-ec.repl +++ b/emulation/ec/betrusted-ec.repl @@ -1,5 +1,5 @@ cpu: CPU.VexRiscv @ sysbus - cpuType: "rv32i" + cpuType: "rv32i_zicsr_zifencei" PerformanceInMips: 6 uart: UART.LiteX_UART @ { From 88aa25959913647afb51357a4d247377b35a541e Mon Sep 17 00:00:00 2001 From: Sean Cross Date: Fri, 29 Dec 2023 18:37:37 +0800 Subject: [PATCH 08/11] emulation: soc: add _zicsr_zifencei to rv32imac Add "_zicsr_zifencei" to "rv32imac" when describing the CPU core. This is required by newer versions of the spec, and will eventually be a requirement of Renode. Signed-off-by: Sean Cross --- emulation/soc/betrusted-soc.repl | 2 +- svd2repl/src/generate.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/emulation/soc/betrusted-soc.repl b/emulation/soc/betrusted-soc.repl index c6569e33b..f982a4e12 100644 --- a/emulation/soc/betrusted-soc.repl +++ b/emulation/soc/betrusted-soc.repl @@ -1,7 +1,7 @@ // Renode Platform file generated by svd2repl // This file is automatically generated cpu: CPU.Betrusted.AesVexRiscv @ sysbus - cpuType: "rv32imac" + cpuType: "rv32imac_zicsr_zifencei" privilegeArchitecture: PrivilegeArchitecture.Priv1_10 PerformanceInMips: 120 // Platform Peripherals diff --git a/svd2repl/src/generate.rs b/svd2repl/src/generate.rs index ec8fea788..27f230cb2 100644 --- a/svd2repl/src/generate.rs +++ b/svd2repl/src/generate.rs @@ -491,7 +491,7 @@ fn print_header(out: &mut U) -> std::io::Result<()> { let s = r####"// Renode Platform file generated by svd2repl // This file is automatically generated cpu: CPU.Betrusted.AesVexRiscv @ sysbus - cpuType: "rv32imac" + cpuType: "rv32imac_zicsr_zifencei" privilegeArchitecture: PrivilegeArchitecture.Priv1_10 PerformanceInMips: 120 "####; From 64ef8281522989fa437f592ae489f5fc231f5c76 Mon Sep 17 00:00:00 2001 From: Sean Cross Date: Fri, 29 Dec 2023 18:38:30 +0800 Subject: [PATCH 09/11] emulation: ComConnector: remove IInterestingType Apparently SPI connectors are no longer interesting. Remove this designator as it no longer exists. Signed-off-by: Sean Cross --- emulation/peripherals/ComConnector.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/emulation/peripherals/ComConnector.cs b/emulation/peripherals/ComConnector.cs index 6d3ba11ed..021bd4a9f 100644 --- a/emulation/peripherals/ComConnector.cs +++ b/emulation/peripherals/ComConnector.cs @@ -13,13 +13,13 @@ using Antmicro.Renode.Utilities; namespace Antmicro.Renode.Peripherals.SPI.Betrusted { - public interface IComPeripheral : IPeripheral, IInterestingType + public interface IComPeripheral : IPeripheral { ushort Transmit(ushort data); bool HasData(); void FinishTransmission(); } - public interface IComController : IPeripheral, IInterestingType, IGPIOReceiver + public interface IComController : IPeripheral, IGPIOReceiver { ushort Transmit(ushort data); void FinishTransmission(); From d8c6881554c1ecd97a33dcef074db0c78a8efd89 Mon Sep 17 00:00:00 2001 From: Sean Cross Date: Sun, 7 Jan 2024 22:36:34 +0800 Subject: [PATCH 10/11] loader: pass environment to processes via stack Load environment variables to processes by placing the value onto the stack. This will support additional data in the future such as command line arguments. Note this is a breaking change and requires a kernel modification. Signed-off-by: Sean Cross --- loader/src/consts.rs | 1 + loader/src/minielf.rs | 12 ++++++++++-- loader/src/phase1.rs | 3 +++ loader/src/phase2.rs | 37 +++++++++++++++++++++++++++++++++++-- 4 files changed, 49 insertions(+), 4 deletions(-) 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); } } From 60f2a176496a861da15faa933d0e0432aa30c905 Mon Sep 17 00:00:00 2001 From: Sean Cross Date: Sun, 7 Jan 2024 22:44:51 +0800 Subject: [PATCH 11/11] kernel: support receiving process arguments Support passing process arguments in register 2 as part of arguments to a process. This requires updating the `InitialProcess` struct that is received from the loader, and so requires modeifications to be made in tandem. Signed-off-by: Sean Cross --- kernel/src/arch/riscv/process.rs | 3 +++ kernel/src/services.rs | 27 +++++++++++++-------------- 2 files changed, 16 insertions(+), 14 deletions(-) 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);