From 8b06017c6610e97bc3118bcdc4566eca4ef85d82 Mon Sep 17 00:00:00 2001 From: Sean Cross Date: Mon, 12 Apr 2021 17:04:29 +0800 Subject: [PATCH 1/3] loader: fix double-allocation of memory regions Due to an error in how small regions were loaded, memory was mapped to the wrong offset. This didn't affect us for the most part, since these small regions were mostly in the backtrace report which we don't usually hit. With this patch, we now can verify that processes are loaded correctly. For example: (gdb) compare-sections Section .rodata, range 0x100e0 -- 0x13bdc: matched. Section .eh_frame, range 0x13bdc -- 0x15040: matched. Section .text, range 0x16040 -- 0x2dd6e: matched. Section .sdata, range 0x2ed70 -- 0x2ed78: matched. Section .data, range 0x2ed78 -- 0x2eda4: matched. (gdb) Signed-off-by: Sean Cross --- loader/src/main.rs | 113 ++++++++++++++++++++++++++++++--------------- 1 file changed, 76 insertions(+), 37 deletions(-) diff --git a/loader/src/main.rs b/loader/src/main.rs index 8d8e3e058..26ee628cf 100644 --- a/loader/src/main.rs +++ b/loader/src/main.rs @@ -5,6 +5,7 @@ mod args; use args::{KernelArgument, KernelArguments}; +use core::num::NonZeroUsize; use core::{mem, ptr, slice}; pub type XousPid = u8; @@ -210,7 +211,7 @@ impl MiniElf { println!("Mapping PID {} starting at offset {:08x}", pid, load_offset); let mut allocated_bytes = 0; - let mut page_addr: usize = 0; + let mut current_page_addr: usize = 0; let mut previous_addr: usize = 0; // The load offset is the end of this process. Shift it down by one page @@ -223,22 +224,16 @@ impl MiniElf { allocator.change_owner(pid as XousPid, satp_address); // Turn the satp address into a pointer - println!(" Pagetable"); + println!(" Pagetable @ {:08x}", satp_address); let satp = unsafe { &mut *(satp_address as *mut PageTable) }; allocator.map_page(satp, satp_address, PAGE_TABLE_ROOT_OFFSET, FLG_R | FLG_W); // Allocate thread 1 for this process - println!(" Thread"); let thread_address = allocator.alloc() as usize; + println!(" Thread 1 @ {:08x}", thread_address); allocator.map_page(satp, thread_address, CONTEXT_OFFSET, FLG_R | FLG_W); allocator.change_owner(pid as XousPid, thread_address as usize); - // Ensure the pagetables are mapped as well - println!(" Pagetables"); - let pt_addr = allocator.alloc() as usize; - allocator.map_page(satp, pt_addr, PAGE_TABLE_OFFSET, FLG_R | FLG_W); - allocator.change_owner(pid as XousPid, pt_addr as usize); - // Allocate stack pages. println!(" Stack"); for i in 0..STACK_PAGE_COUNT { @@ -263,7 +258,7 @@ impl MiniElf { // Example: Page starts at 0xf000 and is 128 bytes long // 1. Copy 128 bytes to page 1 // - // Example: Page starts at oxf0c0 and is 128 bytes long + // Example: Page starts at 0xf0c0 and is 128 bytes long // 1. Copy 128 bytes to page 1 for section in self.sections { println!(" Section @ {:08x}", section.virt as usize); @@ -281,23 +276,46 @@ impl MiniElf { // If this is not a new page, ensure the uninitialized values from between // this section and the previous one are all zeroed out. - if this_page != page_addr { + if this_page != current_page_addr { + println!("1 {:08x} -> {:08x}", top as usize, this_page); allocator.map_page(satp, top as usize, this_page, flag_defaults); allocator.change_owner(pid as XousPid, top as usize); allocated_bytes += PAGE_SIZE; top -= PAGE_SIZE; this_page += PAGE_SIZE; - } - // Part 1: Copy the first chunk over. - let mut first_chunk_size = PAGE_SIZE - (section.virt as usize & (PAGE_SIZE - 1)); - if first_chunk_size > section.len() { - first_chunk_size = section.len(); + // Part 1: Copy the first chunk over. + let mut first_chunk_size = PAGE_SIZE - (section.virt as usize & (PAGE_SIZE - 1)); + if first_chunk_size > section.len() { + first_chunk_size = section.len(); + } + bytes_to_copy -= first_chunk_size; + } else { + println!( + "This page is {:08x}, and last page was {:08x}", + this_page, current_page_addr + ); + // This is a continuation of the previous section, and as a result + // the memory will have been copied already. Avoid copying this data + // to a new page. + let first_chunk_size = PAGE_SIZE - (section.virt as usize & (PAGE_SIZE - 1)); + println!("First chunk size: {}", first_chunk_size); + if bytes_to_copy < first_chunk_size { + bytes_to_copy = 0; + println!("Clamping to 0 bytes"); + } else { + bytes_to_copy -= first_chunk_size; + println!( + "Clamping to {} bytes by cutting off {} bytes", + bytes_to_copy, first_chunk_size + ); + } + this_page += PAGE_SIZE; } - bytes_to_copy -= first_chunk_size; // Part 2: Copy any full pages. while bytes_to_copy > PAGE_SIZE { + println!("2 {:08x} -> {:08x}", top as usize, this_page); allocator.map_page(satp, top as usize, this_page, flag_defaults); allocator.change_owner(pid as XousPid, top as usize); allocated_bytes += PAGE_SIZE; @@ -309,6 +327,7 @@ impl MiniElf { // Part 3: Copy the final residual partial page if bytes_to_copy > 0 { let this_page = (section.virt as usize + section.len()) & !(PAGE_SIZE - 1); + println!("3 {:08x} -> {:08x}", top as usize, this_page); allocator.map_page(satp, top as usize, this_page, flag_defaults); allocator.change_owner(pid as XousPid, top as usize); allocated_bytes += PAGE_SIZE; @@ -316,7 +335,7 @@ impl MiniElf { } previous_addr = section.virt as usize + section.len(); - page_addr = previous_addr & !(PAGE_SIZE - 1); + current_page_addr = previous_addr & !(PAGE_SIZE - 1); } let mut process = &mut allocator.processes[pid as usize - 1]; @@ -416,13 +435,12 @@ impl ProgramDescription { allocator.map_page(satp, thread_address, CONTEXT_OFFSET, FLG_R | FLG_W); allocator.change_owner(pid as XousPid, thread_address as usize); - // Ensure the pagetables are mapped as well - let pt_addr = allocator.alloc() as usize; - allocator.map_page(satp, pt_addr, PAGE_TABLE_OFFSET, FLG_R | FLG_W); - allocator.change_owner(pid as XousPid, pt_addr as usize); - // Allocate stack pages. - for i in 0..if is_kernel { KERNEL_STACK_PAGE_COUNT} else { STACK_PAGE_COUNT } { + for i in 0..if is_kernel { + KERNEL_STACK_PAGE_COUNT + } else { + STACK_PAGE_COUNT + } { let sp_page = allocator.alloc() as usize; allocator.map_page( satp, @@ -710,7 +728,11 @@ fn copy_processes(cfg: &mut BootConfig) { // Part 3: Copy the final residual partial page if bytes_to_copy > 0 { - println!("Copying final section -- {} bytes", bytes_to_copy); + println!( + "Copying final section -- {} bytes @ {:08x}", + bytes_to_copy, + section.virt + (section.len() as u32) - (bytes_to_copy as u32) + ); cfg.extra_pages += 1; top = cfg.get_top() as *mut u8; if !section.no_copy() { @@ -905,38 +927,52 @@ impl BootConfig { assert!(vpo < 4096); let l1_pt = &mut root.entries; - let mut new_addr = 0; + let mut new_addr = None; // Allocate a new level 1 pagetable entry if one doesn't exist. if l1_pt[vpn1] & FLG_VALID == 0 { - new_addr = self.alloc() as usize; + let na = self.alloc() as usize; + println!("The Level 1 page table is invalid ({:08x}) @ {:08x} -- allocating a new one @ {:08x}", + unsafe { l1_pt.as_ptr().add(vpn1) } as usize, l1_pt[vpn1], na); // Mark this entry as a leaf node (WRX as 0), and indicate // it is a valid page by setting "V". - l1_pt[vpn1] = ((new_addr >> 12) << 10) | FLG_VALID; + l1_pt[vpn1] = ((na >> 12) << 10) | FLG_VALID; + new_addr = Some(NonZeroUsize::new(na).unwrap()); } let l0_pt_idx = unsafe { &mut (*(((l1_pt[vpn1] << 2) & !((1 << 12) - 1)) as *mut PageTable)) }; let l0_pt = &mut l0_pt_idx.entries; - // Ensure the entry hasn't already been mapped. - // if l0_pt[vpn0] & 1 != 0 { - // panic!("Page already allocated!"); - // } + // Ensure the entry hasn't already been mapped to a different address. + if l0_pt[vpn0] & 1 != 0 && (l0_pt[vpn0] & 0xffff_fc00) != ((ppn1 << 20) | (ppn0 << 10)) { + panic!( + "Page {:08x} was already allocated to {:08x}, so cannot map to {:08x}!", + phys, + (l0_pt[vpn0] >> 10) << 12, + virt + ); + } let previous_flags = l0_pt[vpn0] & 0xf; l0_pt[vpn0] = (ppn1 << 20) | (ppn0 << 10) | flags | previous_flags | FLG_VALID | FLG_D | FLG_A; // If we had to allocate a level 1 pagetable entry, ensure that it's - // mapped into our address space. - if new_addr != 0 { + // mapped into our address space, owned by PID 1. + if let Some(addr) = new_addr { + println!( + ">>> Mapping new address {:08x} -> {:08x}", + addr.get(), + PAGE_TABLE_OFFSET + vpn1 * PAGE_SIZE + ); self.map_page( root, - new_addr, + addr.get(), PAGE_TABLE_OFFSET + vpn1 * PAGE_SIZE, FLG_R | FLG_W, ); - self.change_owner(1 as XousPid, new_addr as usize); + self.change_owner(1 as XousPid, addr.get()); + println!("<<< Done mapping new address"); } } } @@ -1042,7 +1078,10 @@ fn boot_sequence(args: KernelArguments, _signature: u32) -> ! { println!("Font maps located as follows:"); println!(" Hanzi @ {:08x}", fonts::hanzi::DATA_HANZI.as_ptr() as u32); println!(" Emoji @ {:08x}", fonts::emoji::DATA_EMOJI.as_ptr() as u32); - println!(" Regular @ {:08x}", fonts::regular::DATA_REGULAR.as_ptr() as u32); + println!( + " Regular @ {:08x}", + fonts::regular::DATA_REGULAR.as_ptr() as u32 + ); println!(" Small @ {:08x}", fonts::small::DATA_SMALL.as_ptr() as u32); println!(" Bold @ {:08x}", fonts::bold::DATA_BOLD.as_ptr() as u32); From c5ca371e084d2a875b49cc76037a80771ce506d7 Mon Sep 17 00:00:00 2001 From: Sean Cross Date: Mon, 12 Apr 2021 17:15:52 +0800 Subject: [PATCH 2/3] loader: fix debug warnings These warnings appeared as part of a change in Rust 1.50. Signed-off-by: Sean Cross --- loader/src/debug.rs | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/loader/src/debug.rs b/loader/src/debug.rs index 2e7c12adb..4831402f8 100644 --- a/loader/src/debug.rs +++ b/loader/src/debug.rs @@ -1,11 +1,12 @@ use utralib::generated::*; pub struct Uart { - pub base: *mut u32, + // pub base: *mut u32, } impl Uart { pub fn putc(&self, c: u8) { - let mut uart = CSR::new(self.base); + let base = utra::uart::HW_UART_BASE as *mut u32; + let mut uart = CSR::new(base); // Wait until TXFULL is `0` while uart.r(utra::uart::TXFULL) != 0 {} uart.wo(utra::uart::RXTX, c as u32) @@ -25,17 +26,12 @@ impl Write for Uart { #[macro_use] #[cfg(all(not(test), feature = "debug-print"))] pub mod debug_print_hardware { - use crate::debug::*; - pub const SUPERVISOR_UART: Uart = Uart { - base: utra::uart::HW_UART_BASE as *mut u32, - }; - #[macro_export] macro_rules! print { ($($args:tt)+) => ({ use core::fmt::Write; - let _ = write!(crate::debug::debug_print_hardware::SUPERVISOR_UART, $($args)+); + let _ = write!(crate::debug::Uart {}, $($args)+); }); } } From ac9570a7673e27cc1996103e2b25e3e0e5f293be Mon Sep 17 00:00:00 2001 From: Sean Cross Date: Mon, 12 Apr 2021 17:47:26 +0800 Subject: [PATCH 3/3] log-server: remove UB in log server init Previously, the log server would call `setup` on an uninitialized struct. This is very undefined behavior, and risked running into optimization errors. This reworks the log server such that the singleton is protected by a `None` value that gets initialized. Signed-off-by: Sean Cross --- services/log-server/src/lib.rs | 96 +++++++++++++++++++++------------- 1 file changed, 61 insertions(+), 35 deletions(-) diff --git a/services/log-server/src/lib.rs b/services/log-server/src/lib.rs index 116a6f0cb..0fe37fdce 100644 --- a/services/log-server/src/lib.rs +++ b/services/log-server/src/lib.rs @@ -1,10 +1,16 @@ #![cfg_attr(target_os = "none", no_std)] use core::fmt::Write; use core::sync::atomic::{AtomicBool, Ordering}; -use xous_ipc::{String, Buffer}; +use xous_ipc::{Buffer, String}; pub mod api; +#[derive(Debug)] +pub enum LogError { + LoggerExists, + NoConnection, +} + static XOUS_LOGGER: XousLogger = XousLogger { locked: AtomicBool::new(false), }; @@ -13,33 +19,41 @@ struct XousLogger { locked: AtomicBool, } -static mut XOUS_LOGGER_BACKING: XousLoggerBacking = XousLoggerBacking { - conn: 0, - initialized: false, - buffer: None, -}; +// static mut XOUS_LOGGER_BACKING: XousLoggerBacking = XousLoggerBacking { +// conn: 0, +// initialized: false, +// buffer: None, +// }; + +static mut XOUS_LOGGER_BACKING: Option = None; struct XousLoggerBacking<'a> { conn: xous::CID, - buffer: Option>, - initialized: bool, + buffer: Buffer<'a>, +} + +impl<'a> XousLoggerBacking<'a> { + pub fn new() -> Result { + Ok(XousLoggerBacking { + conn: xous::connect(xous::SID::from_bytes(b"xous-log-server ").unwrap())?, + // why 4000? tests non-power of 2 sizes in rkyv APIs. Could make it 4096 as well... + buffer: Buffer::new(4000), + }) + } +} + +impl Default for XousLoggerBacking<'_> { + fn default() -> Self { + XousLoggerBacking { + conn: xous::connect(xous::SID::from_bytes(b"xous-log-server ").unwrap()).unwrap(), + // why 4000? tests non-power of 2 sizes in rkyv APIs. Could make it 4096 as well... + buffer: Buffer::new(4000), + } + } } impl XousLoggerBacking<'_> { - fn init(&mut self) -> Result<(), xous::Error> { - if self.initialized { - return Ok(()); - } - self.conn = xous::connect(xous::SID::from_bytes(b"xous-log-server ").unwrap())?; - self.buffer = Some(Buffer::new(4000)); // why 4000? tests non-power of 2 sizes in rkyv APIs. Could make it 4096 as well... - self.initialized = true; - Ok(()) - } - fn log_impl(&mut self, record: &log::Record) { - if !self.initialized && self.init().is_err() { - return; - } let mut args = String::<2800>::new(); write!(args, "{}", record.args()).unwrap(); let lr = api::LogRecord { @@ -50,10 +64,8 @@ impl XousLoggerBacking<'_> { args, }; - if let Some(buf) = self.buffer.as_mut() { - buf.rewrite(lr).unwrap(); - buf.lend(self.conn, 0).unwrap(); // there is only one type of buffer we should be sending! - } + self.buffer.rewrite(lr).unwrap(); + self.buffer.lend(self.conn, 0).unwrap(); // there is only one type of buffer we should be sending! } } @@ -71,7 +83,10 @@ impl log::Log for XousLogger { xous::yield_slice(); } - unsafe { XOUS_LOGGER_BACKING.log_impl(record) }; + if unsafe { XOUS_LOGGER_BACKING.is_none() } { + unsafe { XOUS_LOGGER_BACKING = Some(XousLoggerBacking::default()) }; + } + unsafe { XOUS_LOGGER_BACKING.as_mut().unwrap().log_impl(record) }; self.locked .compare_exchange(true, false, Ordering::SeqCst, Ordering::Acquire) .expect("LOG: logger became unlocked somehow"); @@ -79,19 +94,30 @@ impl log::Log for XousLogger { fn flush(&self) {} } -pub fn init() -> Result<(), log::SetLoggerError> { - log::set_logger(&XOUS_LOGGER)?; - log::set_max_level(log::LevelFilter::Info); - Ok(()) +pub fn init() -> Result<(), LogError> { + if let Ok(backing) = XousLoggerBacking::new() { + unsafe { + XOUS_LOGGER_BACKING = Some(backing); + } + log::set_logger(&XOUS_LOGGER).map_err(|_| LogError::LoggerExists)?; + log::set_max_level(log::LevelFilter::Info); + Ok(()) + } else { + Err(LogError::NoConnection) + } } pub fn init_wait() -> Result<(), log::SetLoggerError> { + loop { + if let Ok(backing) = XousLoggerBacking::new() { + unsafe { + XOUS_LOGGER_BACKING = Some(backing); + break; + } + } + xous::yield_slice(); + } log::set_logger(&XOUS_LOGGER)?; log::set_max_level(log::LevelFilter::Info); - unsafe { - while XOUS_LOGGER_BACKING.init().is_err() { - xous::yield_slice(); - } - } Ok(()) }