diff --git a/loader/src/minielf.rs b/loader/src/minielf.rs index be4a68dc8..afc678c14 100644 --- a/loader/src/minielf.rs +++ b/loader/src/minielf.rs @@ -243,8 +243,7 @@ impl MiniElf { | FLG_X | FLG_VALID | if section.flags() & 1 == 1 { FLG_W } else { 0 } - | if section.flags() & 4 == 4 { FLG_X } else { 0 } - | if ini_type == IniType::IniS { FLG_P } else { 0 }; + | if section.flags() & 4 == 4 { FLG_X } else { 0 }; let copy_to_ram = ((section.flags() as u8) & MINIELF_FLG_W) != 0; if (section.virt as usize) < previous_addr { @@ -371,8 +370,17 @@ impl MiniElf { // --- map FLASH or swap pages to virtual memory --- 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, pid as XousPid); + let map_phys_addr = if ini_type != IniType::IniS { + (image_phys_base + section_map_phys_offset) & !(PAGE_SIZE - 1) + } else { + 0 // IniS physical addresses are non-existent; make that abundantly clear + }; + let flags = if ini_type == IniType::IniS { + flag_defaults & !FLG_VALID | FLG_P + } else { + flag_defaults + }; + allocator.map_page(tt, map_phys_addr, virt_page, flags, pid as XousPid); last_mapped_xip = virt_page; section_map_phys_offset += PAGE_SIZE; @@ -404,18 +412,28 @@ impl MiniElf { } /// Page through a processes allocated pages and check against the file spec. - #[cfg(feature = "debug-print")] - pub fn check(&self, allocator: &mut BootConfig, load_offset: usize, pid: XousPid, xip: bool) { - println!( - "Checking {} PID {} starting at offset {:08x}", - if xip { "xip" } else { "ram" }, - pid, - load_offset - ); - let image_phys_base = allocator.base_addr as usize + self.load_offset as usize; + #[cfg(any(feature = "debug-print", feature = "swap"))] + pub fn check(&self, allocator: &mut BootConfig, load_offset: usize, pid: XousPid, ini_type: IniType) { + println!("Checking {:?} PID {} starting at offset {:08x}", ini_type, pid, load_offset); + let image_phys_base = match ini_type { + IniType::IniE | IniType::IniF => allocator.base_addr as usize + self.load_offset as usize, + IniType::IniS => load_offset, + }; // the process offset is always 1 less than the PID, because that's how we built the table. #[cfg(not(feature = "atsama5d27"))] - let tt = { allocator.processes[pid as usize - 1].satp }; + let tt = match ini_type { + IniType::IniE | IniType::IniF => allocator.processes[pid as usize - 1].satp, + IniType::IniS => { + #[cfg(feature = "swap")] + { + allocator.processes[pid as usize - 1].swap_root >> 12 + } + #[cfg(not(feature = "swap"))] + { + 0 + } + } + }; #[cfg(feature = "atsama5d27")] let tt = { allocator.processes[pid as usize - 1].ttbr0 }; @@ -435,18 +453,63 @@ impl MiniElf { let dump_pa_src = section_offset + image_phys_base; let dump_pa_dst = dest_offset; let dump_pa_end_dst = pt_walk(tt, section.virt as usize + section.len() - 20); - dump_addr(dump_pa_src, " Src [:20] "); - dump_addr(dump_pa_dst, " Dst [:20] "); - dump_addr(dump_pa_src + section.len() - 20, " Src [-20:] "); - // recompute the end section mapping, because PA/VA mappings don't have to be linear (in fact - // they go in the opposite direction) - if let Some(pa_dst_end) = dump_pa_end_dst { - dump_addr(pa_dst_end, " Dst [-20:] "); - } else { - println!( - " End of destination VA 0x{:x}, ERR UNMAPPED!", - section.virt as usize + section.len() - 20 - ); + match ini_type { + IniType::IniE | IniType::IniF => { + dump_addr(dump_pa_src, " Src [:20] "); + dump_addr(dump_pa_dst, " Dst [:20] "); + dump_addr(dump_pa_src + section.len() - 20, " Src [-20:] "); + // recompute the end section mapping, because PA/VA mappings don't have to be linear + // (in fact they go in the opposite direction) + if let Some(pa_dst_end) = dump_pa_end_dst { + dump_addr(pa_dst_end, " Dst [-20:] "); + } else { + println!( + " End of destination VA 0x{:x}, ERR UNMAPPED!", + section.virt as usize + section.len() - 20 + ); + } + } + IniType::IniS => { + #[cfg(feature = "swap")] + if let Some(swap) = allocator.swap_hal.as_mut() { + let dump_disk = swap.decrypt_src_page_at(dump_pa_src & !(PAGE_SIZE - 1)); + dump_slice(&dump_disk[dump_pa_src & (PAGE_SIZE - 1)..], " Src [:20] "); + let dump_swap = swap.decrypt_swap_from( + dest_offset & !(PAGE_SIZE - 1), + (section.virt as usize) & !(PAGE_SIZE - 1), + pid, + ); + dump_slice( + &dump_swap[(section.virt as usize) & (PAGE_SIZE - 1)..], + " Dst [:20] ", + ); + let dump_disk = swap + .decrypt_src_page_at((dump_pa_src + section.len() - 20) & !(PAGE_SIZE - 1)); + dump_slice( + &dump_disk[(dump_pa_src + section.len() - 20) & (PAGE_SIZE - 1)..], + " Src [-20:] ", + ); + // recompute the end section mapping, because PA/VA mappings don't have to be + // linear (in fact they go in the opposite direction) + if let Some(pa_dst_end) = dump_pa_end_dst { + let dump_swap = swap.decrypt_swap_from( + pa_dst_end & !(PAGE_SIZE - 1), + (section.virt as usize + section.len() - 20) & !(PAGE_SIZE - 1), + pid, + ); + dump_slice( + &dump_swap + [(section.virt as usize + section.len() - 20) & (PAGE_SIZE - 1)..], + " Dst [-20:] ", + ); + } else { + println!( + " End of destination VA 0x{:x}, ERR UNMAPPED!", + section.virt as usize + section.len() - 20 + ); + } + } + } } } else { println!( @@ -461,7 +524,7 @@ impl MiniElf { } } -#[cfg(feature = "debug-print")] +#[cfg(any(feature = "debug-print", feature = "swap"))] fn dump_addr(addr: usize, label: &str) { print!("{}", label); let slice = unsafe { core::slice::from_raw_parts(addr as *const u8, 20) }; @@ -470,8 +533,18 @@ fn dump_addr(addr: usize, label: &str) { } print!("\n\r"); } +#[cfg(feature = "swap")] +fn dump_slice(slice: &[u8], label: &str) { + print!("{}", label); + // handle case that our decrypt region isn't 20 bytes long... + let len = slice.len().min(20); + for &b in &slice[..len] { + print!("{:02x}", b); + } + print!("\n\r"); +} -#[cfg(all(feature = "debug-print", not(feature = "atsama5d27")))] +#[cfg(all(any(feature = "debug-print", feature = "swap"), not(feature = "atsama5d27")))] pub fn pt_walk(root: usize, va: usize) -> Option { let l1_pt = unsafe { &mut (*((root << 12) as *mut PageTable)) }; let l1_entry = l1_pt.entries[(va & 0xFFC0_0000) >> 22];