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.