loader: split hw specific details with feature gates in common modules

This commit is contained in:
eupn 2023-03-09 20:22:15 +04:00
parent a0b9868068
commit 899351214b
No known key found for this signature in database
GPG key ID: 0BBE77FB42014014
5 changed files with 236 additions and 56 deletions

View file

@ -1,5 +1,6 @@
#[cfg(not(feature = "atsama5d27"))]
use core::num::NonZeroUsize;
#[cfg(not(feature = "atsama5d27"))]
use core::mem;
use crate::*;
@ -71,6 +72,7 @@ impl Default for BootConfig {
}
}
#[cfg(not(feature = "atsama5d27"))]
impl BootConfig {
/// Used by Phase 1 to keep track of where we are in terms of physical pages of memory allocated
pub fn get_top(&self) -> *mut usize {

View file

@ -177,9 +177,17 @@ fn boot_sequence(args: KernelArguments, _signature: u32) -> ! {
let ip_offset = cfg.processes.as_ptr() as usize - krn_struct_start + KERNEL_ARGUMENT_OFFSET;
let rpt_offset =
cfg.runtime_page_tracker.as_ptr() as usize - krn_struct_start + KERNEL_ARGUMENT_OFFSET;
#[cfg(not(feature = "atsama5d27"))]
let tt_addr = {
cfg.processes[0].satp
};
#[cfg(feature = "atsama5d27")]
let tt_addr = {
cfg.processes[0].ttbr0
};
println!(
"Jumping to kernel @ {:08x} with map @ {:08x} and stack @ {:08x} (kargs: {:08x}, ip: {:08x}, rpt: {:08x})",
cfg.processes[0].entrypoint, cfg.processes[0].satp, cfg.processes[0].sp,
cfg.processes[0].entrypoint, tt_addr, cfg.processes[0].sp,
arg_offset, ip_offset, rpt_offset,
);
@ -191,6 +199,7 @@ fn boot_sequence(args: KernelArguments, _signature: u32) -> ! {
// actions into "verify" actions during a clean resume (right now we just don't run them),
// so that attempts to mess with the args during a resume can't lead to overwriting
// critical parameters like these kernel arguments.
#[cfg(not(feature = "atsama5d27"))]
unsafe {
let backup_args: *mut [u32; 7] = BACKUP_ARGS_ADDR as *mut[u32; 7];
(*backup_args)[0] = arg_offset as u32;
@ -210,20 +219,38 @@ fn boot_sequence(args: KernelArguments, _signature: u32) -> ! {
}
}
}
use utralib::generated::*;
let mut gpio_csr = CSR::new(utra::gpio::HW_GPIO_BASE as *mut u32);
gpio_csr.wfo(utra::gpio::UARTSEL_UARTSEL, 1); // patch us over to a different UART for debug (1=LOG 2=APP, 0=KERNEL(hw reset default))
start_kernel(
arg_offset,
ip_offset,
rpt_offset,
cfg.processes[0].satp,
cfg.processes[0].entrypoint,
cfg.processes[0].sp,
cfg.debug,
clean,
);
#[cfg(not(feature = "atsama5d27"))]
{
use utralib::generated::*;
let mut gpio_csr = CSR::new(utra::gpio::HW_GPIO_BASE as *mut u32);
gpio_csr.wfo(utra::gpio::UARTSEL_UARTSEL, 1); // patch us over to a different UART for debug (1=LOG 2=APP, 0=KERNEL(hw reset default))
start_kernel(
arg_offset,
ip_offset,
rpt_offset,
cfg.processes[0].satp,
cfg.processes[0].entrypoint,
cfg.processes[0].sp,
cfg.debug,
clean,
);
}
#[cfg(feature = "atsama5d27")]
unsafe {
start_kernel(
cfg.processes[0].sp,
cfg.processes[0].ttbr0,
cfg.processes[0].entrypoint,
arg_offset,
ip_offset,
rpt_offset,
cfg.debug,
clean,
)
}
} else {
#[cfg(feature="resume")]
unsafe {

View file

@ -1,5 +1,14 @@
use core::{mem, slice};
#[cfg(feature = "atsama5d27")]
pub use armv7::{
PhysicalAddress, VirtualAddress,
structures::paging::{
InMemoryRegister, PageTable as L2PageTable, PageTableDescriptor, PageTableMemory,
PageTableType, Readable, TranslationTable, TranslationTableDescriptor, TranslationTableMemory,
TranslationTableType, Writeable, PAGE_TABLE_FLAGS, PAGE_TABLE_SIZE, SMALL_PAGE_FLAGS,
}
};
use crate::*;
pub const FLG_VALID: usize = 0x1;
@ -7,7 +16,9 @@ pub const FLG_X: usize = 0x8;
pub const FLG_W: usize = 0x4;
pub const FLG_R: usize = 0x2;
pub const FLG_U: usize = 0x10;
#[cfg(not(feature = "atsama5d27"))]
pub const FLG_A: usize = 0x40;
#[cfg(not(feature = "atsama5d27"))]
pub const FLG_D: usize = 0x80;
pub const MINIELF_FLG_W: u8 = 1;
@ -97,18 +108,46 @@ impl MiniElf {
let stack_addr = USER_STACK_TOP - 16;
// Allocate a page to handle the top-level memory translation
let satp_address = allocator.alloc() as usize;
allocator.change_owner(pid as XousPid, satp_address);
#[cfg(not(feature = "atsama5d27"))]
let (tt, tt_address) = {
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);
(tt, tt_address)
};
#[cfg(feature = "atsama5d27")]
let (tt, tt_address) = {
let pid_idx = pid as usize - 1;
// Allocate a page to handle the top-level memory translation
let tt_address = allocator.alloc_l1_page_table(pid) as usize;
allocator.processes[pid_idx].ttbr0 = tt_address;
let translation_table = tt_address as *mut TranslationTableMemory;
// Map all four pages of the translation table to the process' virtual address space
for offset in 0..4 {
let offset = offset * PAGE_SIZE;
allocator.map_page(
translation_table,
tt_address + offset,
PAGE_TABLE_ROOT_OFFSET + offset,
FLG_R | FLG_W | FLG_VALID,
);
}
(translation_table, tt_address)
};
// Turn the satp address into a pointer
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 | FLG_VALID);
println!(" Pagetable @ {:08x}", tt_address);
// Allocate thread 1 for this process
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 | FLG_VALID);
allocator.map_page(tt, thread_address, CONTEXT_OFFSET, FLG_R | FLG_W | FLG_VALID);
allocator.change_owner(pid as XousPid, thread_address as usize);
// Allocate stack pages.
@ -118,7 +157,7 @@ impl MiniElf {
// For the initial stack frame, allocate a valid page
let sp_page = allocator.alloc() as usize;
allocator.map_page(
satp,
tt,
sp_page,
(stack_addr - PAGE_SIZE * i) & !(PAGE_SIZE - 1),
FLG_U | FLG_R | FLG_W | FLG_VALID,
@ -127,7 +166,7 @@ impl MiniElf {
} else {
// Reserve every other stack page other than the 1st page.
allocator.map_page(
satp,
tt,
0,
(stack_addr - PAGE_SIZE * i) & !(PAGE_SIZE - 1),
FLG_U | FLG_R | FLG_W,
@ -173,7 +212,7 @@ impl MiniElf {
// this section and the previous one are all zeroed out.
if this_page != current_page_addr || previous_addr == current_page_addr {
if VDBG {println!("1 {:08x} -> {:08x}", top as usize, this_page);}
allocator.map_page(satp, top as usize, this_page, flag_defaults);
allocator.map_page(tt, top as usize, this_page, flag_defaults);
allocator.change_owner(pid as XousPid, top as usize);
allocated_bytes += PAGE_SIZE;
top -= PAGE_SIZE;
@ -211,7 +250,7 @@ impl MiniElf {
// Part 2: Copy any full pages.
while bytes_to_copy >= PAGE_SIZE {
if VDBG {println!("2 {:08x} -> {:08x}", top as usize, this_page);}
allocator.map_page(satp, top as usize, this_page, flag_defaults);
allocator.map_page(tt, top as usize, this_page, flag_defaults);
allocator.change_owner(pid as XousPid, top as usize);
allocated_bytes += PAGE_SIZE;
top -= PAGE_SIZE;
@ -223,7 +262,7 @@ impl MiniElf {
if bytes_to_copy > 0 {
let this_page = (section.virt as usize + section.len()) & !(PAGE_SIZE - 1);
if VDBG {println!("3 {:08x} -> {:08x}", top as usize, this_page);}
allocator.map_page(satp, top as usize, this_page, flag_defaults);
allocator.map_page(tt, top as usize, this_page, flag_defaults);
allocator.change_owner(pid as XousPid, top as usize);
allocated_bytes += PAGE_SIZE;
top -= PAGE_SIZE;
@ -270,7 +309,7 @@ impl MiniElf {
// --- map FLASH 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(satp, map_phys_addr, virt_page, flag_defaults);
allocator.map_page(tt, map_phys_addr, virt_page, flag_defaults);
last_mapped_xip = virt_page;
allocator.change_owner(pid as XousPid, top as usize);
@ -288,7 +327,15 @@ impl MiniElf {
let mut process = &mut allocator.processes[pid as usize - 1];
process.entrypoint = self.entry_point as usize;
process.sp = stack_addr;
process.satp = 0x8000_0000 | ((pid as usize) << 22) | (satp_address >> 12);
#[cfg(not(feature = "atsama5d27"))]
{
process.satp = 0x8000_0000 | ((pid as usize) << 22) | (tt_address >> 12);
}
#[cfg(feature = "atsama5d27")]
{
process.asid = pid;
}
println!("load allocated 0x{:x} bytes", allocated_bytes);
allocated_bytes
}
@ -299,11 +346,18 @@ impl MiniElf {
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;
// the process offset is always 1 less than the PID, because that's how we built the table.
let satp = allocator.processes[pid as usize - 1].satp;
#[cfg(not(feature = "atsama5d27"))]
let tt = {
allocator.processes[pid as usize - 1].satp
};
#[cfg(feature = "atsama5d27")]
let tt = {
allocator.processes[pid as usize - 1].ttbr0
};
let mut section_offset = 0;
for (index, section) in self.sections.iter().enumerate() {
if let Some(dest_offset) = pt_walk(satp, section.virt as usize) {
if let Some(dest_offset) = pt_walk(tt, section.virt as usize) {
println!(" Section {} start 0x{:x}(PA src), 0x{:x}(VA dst), 0x{:x}(PA dst) len {}/0x{:x}",
index,
section_offset + image_phys_base,
@ -314,7 +368,7 @@ impl MiniElf {
// dumping routines
let dump_pa_src = section_offset + image_phys_base;
let dump_pa_dst = dest_offset;
let dump_pa_end_dst = pt_walk(satp, section.virt as usize + section.len() - 20);
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:] ");
@ -343,7 +397,8 @@ fn dump_addr(addr: usize, label: &str) {
}
print!("\n");
}
#[cfg(feature="debug-print")]
#[cfg(all(feature="debug-print", not(feature = "atsama5d27")))]
pub fn pt_walk(root: usize, va: usize) -> Option<usize> {
let l1_pt = unsafe { &mut (*((root << 12) as *mut PageTable)) };
let l1_entry = l1_pt.entries[(va & 0xFFC0_0000) >> 22];
@ -358,4 +413,36 @@ pub fn pt_walk(root: usize, va: usize) -> Option<usize> {
} else {
None
}
}
}
#[cfg(all(feature="debug-print", feature = "atsama5d27"))]
pub fn pt_walk(root: usize, va: usize) -> Option<usize> {
if va & 3 != 0 {
return None;
}
let v = VirtualAddress::new(va as u32);
let vpn1 = v.translation_table_index();
let vpn2 = v.page_table_index();
assert!(vpn1 < 4096);
assert!(vpn2 < 256);
let existing_l1_entry = unsafe {
((root as *mut u32).add(vpn1) as *mut TranslationTableDescriptor).read_volatile()
};
if existing_l1_entry.get_type() == TranslationTableType::Invalid {
return None
}
let l2_pt_addr = (existing_l1_entry.as_u32() & 0xfff) + vpn1 as u32 * PAGE_SIZE as u32;
let existing_l2_entry_addr =
unsafe { (l2_pt_addr as *mut u32).add(vpn2) as *mut PageTableDescriptor };
let current_entry: PageTableDescriptor = unsafe { existing_l2_entry_addr.read_volatile() };
let flags_u32 = current_entry.get_flags().expect("flags");
let flags: InMemoryRegister<u32, SMALL_PAGE_FLAGS::Register> = InMemoryRegister::new(flags_u32);
let is_valid = flags.read(SMALL_PAGE_FLAGS::VALID) != 0;
let phys = (current_entry.as_u32() & !0xfff) as usize;
if is_valid {
return Some(phys)
}
None
}

View file

@ -2,7 +2,11 @@ use core::{mem, slice};
use crate::*;
#[cfg(feature = "atsama5d27")]
pub use crate::platform::atsama5d27::load::InitialProcess;
#[repr(C)]
#[cfg(not(feature = "atsama5d27"))]
pub struct InitialProcess {
/// The RISC-V SATP value, which includes the offset of the root page
/// table plus the process ID.

View file

@ -14,6 +14,20 @@ pub fn phase_2(cfg: &mut BootConfig) {
// Go through all Init processes and the kernel, setting up their
// page tables and mapping memory to them.
let mut pid = 2;
#[cfg(feature = "atsama5d27")]
let mut ktext_offset = 0;
#[cfg(feature = "atsama5d27")]
let mut ktext_virt_offset = 0;
#[cfg(feature = "atsama5d27")]
let mut kdata_offset = 0;
#[cfg(feature = "atsama5d27")]
let mut kdata_virt_offset = 0;
#[cfg(feature = "atsama5d27")]
let mut ktext_size = 0;
#[cfg(feature = "atsama5d27")]
let mut kdata_size = 0;
#[cfg(feature = "atsama5d27")]
let mut kernel_exception_sp = 0;
for tag in args.iter() {
if tag.name == u32::from_le_bytes(*b"IniE") {
let inie = MiniElf::new(&tag);
@ -34,47 +48,92 @@ pub fn phase_2(cfg: &mut BootConfig) {
let xkrn = unsafe { &*(tag.data.as_ptr() as *const ProgramDescription) };
let load_size_rounded = ((xkrn.text_size as usize + PAGE_SIZE - 1) & !(PAGE_SIZE - 1))
+ (((xkrn.data_size + xkrn.bss_size) as usize + PAGE_SIZE - 1) & !(PAGE_SIZE - 1));
xkrn.load(cfg, process_offset - load_size_rounded, 1);
#[cfg(not(feature = "atsama5d27"))]
{
xkrn.load(cfg, process_offset - load_size_rounded, 1);
}
#[cfg(feature = "atsama5d27")]
{
(ktext_offset, kdata_offset, kernel_exception_sp) = xkrn.load(cfg, process_offset - load_size_rounded, 1);
(ktext_size, kdata_size, ktext_virt_offset, kdata_virt_offset) = (
(xkrn.text_size as usize + PAGE_SIZE - 1) & !(PAGE_SIZE - 1),
(((xkrn.data_size + xkrn.bss_size) as usize + PAGE_SIZE - 1) & !(PAGE_SIZE - 1)),
xkrn.text_offset as usize,
xkrn.data_offset as usize
);
}
process_offset -= load_size_rounded;
}
}
println!("Done loading.");
let krn_struct_start = cfg.sram_start as usize + cfg.sram_size - cfg.init_size;
#[cfg(feature = "atsama5d27")]
let krn_l1_pt_addr = cfg.processes[0].ttbr0;
#[cfg(not(feature = "atsama5d27"))]
let krn_l1_pt_addr = cfg.processes[0].satp << 12;
assert!(krn_struct_start & (PAGE_SIZE - 1) == 0);
let krn_pg1023_ptr = unsafe { (krn_l1_pt_addr as *const usize).add(1023).read() };
// Map boot-generated kernel structures into the kernel
let satp = unsafe { &mut *(krn_l1_pt_addr as *mut PageTable) };
for addr in (0..cfg.init_size-GUARD_MEMORY_BYTES).step_by(PAGE_SIZE as usize) {
cfg.map_page(
satp,
addr + krn_struct_start,
addr + KERNEL_ARGUMENT_OFFSET,
FLG_R | FLG_W | FLG_VALID,
);
cfg.change_owner(1 as XousPid, (addr + krn_struct_start) as usize);
println!("krn_l1_pt_addr: {:08x}", krn_l1_pt_addr);
#[cfg(not(feature = "atsama5d27"))]
{
assert!(krn_struct_start & (PAGE_SIZE - 1) == 0);
let krn_pg1023_ptr = unsafe { (krn_l1_pt_addr as *const usize).add(1023).read() };
// Map boot-generated kernel structures into the kernel
let satp = unsafe { &mut *(krn_l1_pt_addr as *mut PageTable) };
for addr in (0..cfg.init_size - GUARD_MEMORY_BYTES).step_by(PAGE_SIZE as usize) {
cfg.map_page(
satp,
addr + krn_struct_start,
addr + KERNEL_ARGUMENT_OFFSET,
FLG_R | FLG_W | FLG_VALID,
);
cfg.change_owner(1 as XousPid, (addr + krn_struct_start) as usize);
}
// Copy the kernel's "MMU Page 1023" into every process.
// This ensures a context switch into the kernel can
// always be made, and that the `stvec` is always valid.
// Since it's a megapage, all we need to do is write
// the one address to get all 4MB mapped.
println!("Mapping MMU page 1023 to all processes");
for process in cfg.processes[1..].iter() {
let l1_pt_addr = process.satp << 12;
unsafe { (l1_pt_addr as *mut usize).add(1023).write(krn_pg1023_ptr) };
}
}
// Copy the kernel's "MMU Page 1023" into every process.
// This ensures a context switch into the kernel can
// always be made, and that the `stvec` is always valid.
// Since it's a megapage, all we need to do is write
// the one address to get all 4MB mapped.
println!("Mapping MMU page 1023 to all processes");
for process in cfg.processes[1..].iter() {
let l1_pt_addr = process.satp << 12;
unsafe { (l1_pt_addr as *mut usize).add(1023).write(krn_pg1023_ptr) };
#[cfg(feature = "atsama5d27")]
{
// Map boot-generated kernel structures into the kernel
crate::platform::atsama5d27::boot::map_structs_to_kernel(cfg, krn_l1_pt_addr, krn_struct_start);
crate::platform::atsama5d27::boot::map_kernel_to_processes(
cfg,
ktext_offset,
ktext_size,
ktext_virt_offset,
kdata_offset,
kdata_size,
kdata_virt_offset,
kernel_exception_sp,
krn_struct_start,
);
}
if VVDBG {
println!("PID1 pagetables:");
#[cfg(feature = "atsama5d27")]
debug::print_pagetable(cfg.processes[0].ttbr0);
#[cfg(not(feature = "atsama5d27"))]
debug::print_pagetable(cfg.processes[0].satp);
println!();
println!();
for (_pid, process) in cfg.processes[1..].iter().enumerate() {
println!("PID{} pagetables:", _pid + 2);
#[cfg(feature = "atsama5d27")]
debug::print_pagetable(process.ttbr0);
#[cfg(not(feature = "atsama5d27"))]
debug::print_pagetable(process.satp);
println!();
println!();
@ -127,6 +186,7 @@ impl ProgramDescription {
/// either on SPI flash or in RAM. The `load_offset` argument
/// that is passed in should be used instead of `self.load_offset`
/// for this reason.
#[cfg(not(feature = "atsama5d27"))]
pub fn load(&self, allocator: &mut BootConfig, load_offset: usize, pid: XousPid) {
assert!(pid != 0);
println!("Mapping PID {} into offset {:08x}", pid, load_offset);