From 666be8db7f181b19f29d16fb2c66a5fec1c713cd Mon Sep 17 00:00:00 2001 From: hasheddan Date: Tue, 5 Jan 2021 16:28:08 -0600 Subject: [PATCH 1/8] Fix server typos in syscall doc comments Fixes a few minor typos in the sycall documentation comments. Signed-off-by: hasheddan --- xous-rs/src/syscall.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/xous-rs/src/syscall.rs b/xous-rs/src/syscall.rs index a30df8957..85570f777 100644 --- a/xous-rs/src/syscall.rs +++ b/xous-rs/src/syscall.rs @@ -228,7 +228,7 @@ pub enum SysCall { /// * **ServerExists**: The server hash is already in use. CreateServerWithAddress(SID /* server hash */), - /// Connect to a server. This turns a 128-bit Serever ID into a 32-bit + /// Connect to a server. This turns a 128-bit Server ID into a 32-bit /// Connection ID. Blocks until the server is available. /// /// # Errors @@ -236,7 +236,7 @@ pub enum SysCall { /// None Connect(SID /* server id */), - /// Try to connect to a server. This turns a 128-bit Serever ID into a 32-bit + /// Try to connect to a server. This turns a 128-bit Server ID into a 32-bit /// Connection ID. /// /// # Errors @@ -275,7 +275,7 @@ pub enum SysCall { /// Create a new Server /// /// This will return a 128-bit Server ID that can be used to send messages - /// to this server. The returned Derver ID is random. + /// to this server. The returned Server ID is random. /// /// # Returns /// From 6cbec949535d171d59f2160a9c9b327459e7b1b7 Mon Sep 17 00:00:00 2001 From: Sean Cross Date: Thu, 7 Jan 2021 13:34:42 +0800 Subject: [PATCH 2/8] kernel: riscv: use page base addr when allocating pages When allocating a page, it is only assigned a physical address once it is actually needed. This on-demand paging works well, however we were assuming that the first address of a page was accessed first. This fixes an issue where a Message is allocated but the first byte isn't accessed first. Signed-off-by: Sean Cross --- kernel/src/arch/riscv/irq.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/kernel/src/arch/riscv/irq.rs b/kernel/src/arch/riscv/irq.rs index f31147f11..217e30058 100644 --- a/kernel/src/arch/riscv/irq.rs +++ b/kernel/src/arch/riscv/irq.rs @@ -126,7 +126,8 @@ pub extern "C" fn trap_handler( "KERNEL({}): RISC-V fault: {} @ {:08x}, addr {:08x}", pid, ex, pc, addr ); - let entry = crate::arch::mem::pagetable_entry(addr).unwrap_or_else(|x| { + let virt = addr & !0xfff; + let entry = crate::arch::mem::pagetable_entry(virt).unwrap_or_else(|x| { // MemoryManagerHandle::get().print_ownership(); MemoryMapping::current().print_map(); panic!( @@ -153,7 +154,6 @@ pub extern "C" fn trap_handler( flush_mmu(); // Zero-out the page - let virt = addr & !0xfff; (virt as *mut usize) .write_bytes(0, PAGE_SIZE / core::mem::size_of::()); From 2d33c3903d9766e7b87795518285534021fca0c6 Mon Sep 17 00:00:00 2001 From: Sean Cross Date: Thu, 7 Jan 2021 18:49:01 +0800 Subject: [PATCH 3/8] emulation: sync renode addresses The addresses of the target system have changed. Update the platform definition file to keep these addresses in sync. Signed-off-by: Sean Cross --- emulation/betrusted.repl | 29 +++++++++++------------------ emulation/xous-release.resc | 1 - 2 files changed, 11 insertions(+), 19 deletions(-) diff --git a/emulation/betrusted.repl b/emulation/betrusted.repl index d864f1235..42841d359 100644 --- a/emulation/betrusted.repl +++ b/emulation/betrusted.repl @@ -4,29 +4,23 @@ cpu: CPU.VexRiscv @ sysbus privilegeArchitecture: PrivilegeArchitecture.Priv1_10 uart: UART.LiteX_UART @ { - sysbus 0x60006000; - sysbus 0xF0006000 // shadow + sysbus 0x60007000; + sysbus 0xF0007000 // shadow } -> cpu@1002 uartconsole: UART.LiteX_UART @ { - sysbus 0x60007000; - sysbus 0xF0007000 // shadow + sysbus 0x60009000; + sysbus 0xF0009000 // shadow } -> cpu@1003 uartserver0: UART.LiteX_UART @ { - sysbus 0x60008000; - sysbus 0xF0008000 // shadow + sysbus 0x6000B000; + sysbus 0xF000B000 // shadow } -> cpu@1004 -uartserver1: UART.LiteX_UART @ { - sysbus 0x60009000; - sysbus 0xF0009000 // shadow - } - -> cpu@1005 - timer0: Timers.LiteX_Timer_32 @ { sysbus 0x60002000; sysbus 0xF0002000 // shadow @@ -49,20 +43,20 @@ sram: Memory.MappedMemory @ { fbmem: Memory.MappedMemory @ sysbus 0xB0000000 size: 0x6000 -memlcd: Video.BetrustedLCD @ sysbus 0xF000C000 +memlcd: Video.BetrustedLCD @ sysbus 0xF000E000 -keyboard: Input.BetrustedKbd @ sysbus 0xF0015000 +keyboard: Input.BetrustedKbd @ sysbus 0xF0017000 -trng_server: Miscellaneous.BetrustedRNGServer @ sysbus 0xF001A000 +trng_kernel: Miscellaneous.BetrustedRNGKernel @ sysbus 0xF001B000 -trng_kernel: Miscellaneous.BetrustedRNGKernel @ sysbus 0xF0019000 +trng_server: Miscellaneous.BetrustedRNGServer @ sysbus 0xF001C000 cpu_timer: Timers.LiteX_CPUTimer @ sysbus 0xf0000800 frequency: 100000000 // IRQ #100 is Machine Timer Interrupt -> cpu@100 -ticktimer: Timers.TickTimer @ sysbus 0xF0012000 +ticktimer: Timers.TickTimer @ sysbus 0xF0014000 periodInMs: 1 -> cpu @ 1008 @@ -78,4 +72,3 @@ flash_mem: Memory.MappedMemory @ { sysbus: init: Tag <0xF0000000 0x1000> "CTRL" - Tag <0xF001B000 0x1000> "TRNG" diff --git a/emulation/xous-release.resc b/emulation/xous-release.resc index c283b423a..4c2f41623 100644 --- a/emulation/xous-release.resc +++ b/emulation/xous-release.resc @@ -23,7 +23,6 @@ machine StartGdbServer 3333 true showAnalyzer uart showAnalyzer uartconsole showAnalyzer uartserver0 -showAnalyzer uartserver1 showAnalyzer memlcd logLevel -1 kbd From 05464b945b02438f7fd4a9b668d8208b06020538 Mon Sep 17 00:00:00 2001 From: Sean Cross Date: Thu, 7 Jan 2021 18:50:19 +0800 Subject: [PATCH 4/8] kernel: print out panic messages This enables `println!()` but avoids the excessive debug spew Signed-off-by: Sean Cross --- kernel/Cargo.toml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/kernel/Cargo.toml b/kernel/Cargo.toml index 248e0a12b..733438418 100644 --- a/kernel/Cargo.toml +++ b/kernel/Cargo.toml @@ -21,7 +21,8 @@ riscv = "0.5.6" debug-print = [] print-panics = [] report-memory = ["stats_alloc"] -default = ["print-panics", "debug-print"] +default = ["print-panics"] +# default = ["print-panics", "debug-print"] # default = [] [target.'cfg(any(windows, unix))'.dependencies] From b16ad82e565ee94f597406880e8c193a80197490 Mon Sep 17 00:00:00 2001 From: Sean Cross Date: Thu, 7 Jan 2021 18:50:48 +0800 Subject: [PATCH 5/8] kernel: riscv: print out allocated address This will aid in debugging. Signed-off-by: Sean Cross --- kernel/src/arch/riscv/irq.rs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/kernel/src/arch/riscv/irq.rs b/kernel/src/arch/riscv/irq.rs index 217e30058..70c16c95f 100644 --- a/kernel/src/arch/riscv/irq.rs +++ b/kernel/src/arch/riscv/irq.rs @@ -122,8 +122,8 @@ pub extern "C" fn trap_handler( match ex { RiscvException::StorePageFault(pc, addr) | RiscvException::LoadPageFault(pc, addr) => { #[cfg(any(feature = "debug-print", feature = "print-panics"))] - println!( - "KERNEL({}): RISC-V fault: {} @ {:08x}, addr {:08x}", + print!( + "KERNEL({}): RISC-V fault: {} @ {:08x}, addr {:08x} - ", pid, ex, pc, addr ); let virt = addr & !0xfff; @@ -164,6 +164,7 @@ pub extern "C" fn trap_handler( flush_mmu(); }; + println!("Handing page {:08x} to process", new_page); ArchProcess::with_current_mut(|process| { crate::arch::syscall::resume( current_pid().get() == 1, @@ -171,6 +172,7 @@ pub extern "C" fn trap_handler( ) }); } + println!("Page was not allocated"); } RiscvException::InstructionPageFault(RETURN_FROM_ISR, _offset) => { // If we hit this address, then an ISR has just returned. Since From f6bfd61ccef91c98c527a7698ce47975194ae0e9 Mon Sep 17 00:00:00 2001 From: Sean Cross Date: Thu, 7 Jan 2021 18:51:14 +0800 Subject: [PATCH 6/8] loader: assign sections to process ownership This ensures these addresses aren't re-used and handed out again by memory allocation. Signed-off-by: Sean Cross --- loader/src/main.rs | 29 ++++++++++++++++++++--------- 1 file changed, 20 insertions(+), 9 deletions(-) diff --git a/loader/src/main.rs b/loader/src/main.rs index ca832b07b..bcef4c52e 100644 --- a/loader/src/main.rs +++ b/loader/src/main.rs @@ -220,18 +220,24 @@ impl MiniElf { allocator.change_owner(pid as XousPid, satp_address); // Turn the satp address into a pointer + println!(" Pagetable"); let satp = unsafe { &mut *(satp_address as *mut PageTable) }; allocator.map_page(satp, satp_address, PAGE_TABLE_ROOT_OFFSET, FLG_R | FLG_W); - // Allocate context for this process - let context_address = allocator.alloc() as usize; - allocator.map_page(satp, context_address, CONTEXT_OFFSET, FLG_R | FLG_W); + // Allocate thread 1 for this process + println!(" Thread"); + let thread_address = allocator.alloc() as usize; + 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 { let sp_page = allocator.alloc() as usize; allocator.map_page( @@ -257,6 +263,7 @@ impl MiniElf { // Example: Page starts at oxf0c0 and is 128 bytes long // 1. Copy 128 bytes to page 1 for section in self.sections { + println!(" Section @ {:08x}", section.virt as usize); let flag_defaults = FLG_U | FLG_R | if section.flags() & 1 == 1 { FLG_W } else { 0 } @@ -273,6 +280,7 @@ impl MiniElf { // this section and the previous one are all zeroed out. if this_page != page_addr { 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; @@ -288,6 +296,7 @@ impl MiniElf { // Part 2: Copy any full pages. while bytes_to_copy > PAGE_SIZE { 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; @@ -298,9 +307,9 @@ impl MiniElf { if bytes_to_copy > 0 { let this_page = (section.virt as usize + section.len()) & !(PAGE_SIZE - 1); 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; } previous_addr = section.virt as usize + section.len(); @@ -397,14 +406,17 @@ impl ProgramDescription { // Turn the satp address into a pointer let satp = unsafe { &mut *(satp_address as *mut PageTable) }; allocator.map_page(satp, satp_address, PAGE_TABLE_ROOT_OFFSET, FLG_R | FLG_W); + allocator.change_owner(pid as XousPid, satp_address as usize); // Allocate context for this process - let context_address = allocator.alloc() as usize; - allocator.map_page(satp, context_address, CONTEXT_OFFSET, FLG_R | FLG_W); + let thread_address = allocator.alloc() as usize; + 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..STACK_PAGE_COUNT { @@ -921,6 +933,7 @@ impl BootConfig { PAGE_TABLE_OFFSET + vpn1 * PAGE_SIZE, FLG_R | FLG_W, ); + self.change_owner(1 as XousPid, new_addr as usize); } } } @@ -1112,9 +1125,6 @@ pub fn phase_2(cfg: &mut BootConfig) { if tag.name == u32::from_le_bytes(*b"IniE") { let inie = MiniElf::new(&tag); println!("Mapping program into memory"); - // let init = unsafe { &*(tag.data.as_ptr() as *const ProgramDescription) }; - // let load_size_rounded = ((init.text_size as usize + PAGE_SIZE - 1) & !(PAGE_SIZE - 1)) - // + (((init.data_size + init.bss_size) as usize + PAGE_SIZE - 1) & !(PAGE_SIZE - 1)); process_offset -= inie.load(cfg, process_offset, pid); pid += 1; } else if tag.name == u32::from_le_bytes(*b"XKrn") { @@ -1142,6 +1152,7 @@ pub fn phase_2(cfg: &mut BootConfig) { addr + KERNEL_ARGUMENT_OFFSET, FLG_R | FLG_W, ); + cfg.change_owner(1 as XousPid, (addr + krn_struct_start) as usize); } // Copy the kernel's "MMU Page 1023" into every process. From 037bbdc43049160266dc58550042801950a67b2c Mon Sep 17 00:00:00 2001 From: Sean Cross Date: Thu, 7 Jan 2021 18:51:57 +0800 Subject: [PATCH 7/8] xtask: emulation: add nameserver to renode image Signed-off-by: Sean Cross --- xtask/src/main.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/xtask/src/main.rs b/xtask/src/main.rs index fba606d73..5a918af4f 100644 --- a/xtask/src/main.rs +++ b/xtask/src/main.rs @@ -158,6 +158,7 @@ fn renode_image(debug: bool) -> Result<(), DynError> { "graphics-server", "ticktimer-server", "com", + "xous-names", ] { init.push(build(pkg, debug, Some(TARGET), None)?); } From e8f85c7373cf8ee969553f6515c2bff37c307501 Mon Sep 17 00:00:00 2001 From: Sean Cross Date: Thu, 7 Jan 2021 19:28:37 +0800 Subject: [PATCH 8/8] graphics-server: minifb: use iterators for drawing the screen This removes the bounds checks that were being performed, and goes for a pure iterator approach. Signed-off-by: Sean Cross --- .../graphics-server/src/backend/minifb.rs | 26 +++++++------------ 1 file changed, 9 insertions(+), 17 deletions(-) diff --git a/services/graphics-server/src/backend/minifb.rs b/services/graphics-server/src/backend/minifb.rs index c23651370..7ea64dbe2 100644 --- a/services/graphics-server/src/backend/minifb.rs +++ b/services/graphics-server/src/backend/minifb.rs @@ -76,27 +76,19 @@ impl XousDisplay { } fn emulated_to_native(&mut self) { - for y in 0..HEIGHT { - for x in 0..WIDTH { - // print!("({}, {}): {} @ {}: ", x, y, (x + y * 44 * 8) / 8, self.emulated_buffer.len()); - // println!("{:08x}", self.emulated_buffer[(x + y * 44 * 8) / 8]); - if ((x + y * 11 * 32) / 32) >= self.emulated_buffer.len() { - panic!( - "Value exceeds src buffer ({}, {}) @ {}", - x, - y, - self.emulated_buffer.len() - ); - } - if (x + y * WIDTH) > self.native_buffer.len() { - panic!("Value exceeds dest buffer"); - } - self.native_buffer[x + y * WIDTH] = - if ((self.emulated_buffer[(x + y * 11 * 32) / 32] >> (x % 32)) & 1) > 0 { + for (dest_row, src_row) in self + .native_buffer + .chunks_mut(WIDTH as _) + .zip(self.emulated_buffer.chunks(WIDTH_WORDS as _)) + { + for (dest_cell, src_cell) in dest_row.chunks_mut(32).zip(src_row) { + for (bit, dest) in dest_cell.iter_mut().enumerate() { + *dest = if src_cell & (1 << bit) != 0 { DARK_COLOUR } else { LIGHT_COLOUR }; + } } } }