diff --git a/loader/src/minielf.rs b/loader/src/minielf.rs index 8fe8caa66..2552cac8a 100644 --- a/loader/src/minielf.rs +++ b/loader/src/minielf.rs @@ -1,14 +1,18 @@ use core::{mem, slice}; #[cfg(feature = "atsama5d27")] +use armv7::structures::paging::TranslationTableMemory; + +#[cfg(all(feature = "atsama5d27", feature = "debug-print"))] use armv7::{ VirtualAddress, structures::paging::{ InMemoryRegister, PageTableDescriptor, - Readable, TranslationTableDescriptor, TranslationTableMemory, - TranslationTableType, SMALL_PAGE_FLAGS, + Readable, TranslationTableDescriptor, TranslationTableType, + SMALL_PAGE_FLAGS, } }; + use crate::*; pub const FLG_VALID: usize = 0x1; @@ -109,7 +113,7 @@ impl MiniElf { // Allocate a page to handle the top-level memory translation #[cfg(not(feature = "atsama5d27"))] - let (tt, tt_address) = { + let (tt, _tt_address) = { let tt_address = allocator.alloc() as usize; let tt = unsafe { &mut *(tt_address as *mut PageTable) }; @@ -119,7 +123,7 @@ impl MiniElf { (tt, tt_address) }; #[cfg(feature = "atsama5d27")] - let (tt, tt_address) = { + let (tt, _tt_address) = { let pid_idx = pid as usize - 1; // Allocate a page to handle the top-level memory translation @@ -142,7 +146,7 @@ impl MiniElf { }; // Turn the satp address into a pointer - println!(" Pagetable @ {:08x}", tt_address); + println!(" Pagetable @ {:08x}", _tt_address); // Allocate thread 1 for this process let thread_address = allocator.alloc() as usize; diff --git a/loader/src/phase2.rs b/loader/src/phase2.rs index ecd39b2b8..6afc1dd0f 100644 --- a/loader/src/phase2.rs +++ b/loader/src/phase2.rs @@ -28,6 +28,8 @@ pub fn phase_2(cfg: &mut BootConfig) { let mut kdata_size = 0; #[cfg(feature = "atsama5d27")] let mut kernel_exception_sp = 0; + #[cfg(feature = "atsama5d27")] + let mut kernel_irq_sp = 0; for tag in args.iter() { if tag.name == u32::from_le_bytes(*b"IniE") { let inie = MiniElf::new(&tag); @@ -54,7 +56,7 @@ pub fn phase_2(cfg: &mut BootConfig) { } #[cfg(feature = "atsama5d27")] { - (ktext_offset, kdata_offset, kernel_exception_sp) = xkrn.load(cfg, process_offset - load_size_rounded, 1); + (ktext_offset, kdata_offset, kernel_exception_sp, kernel_irq_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)), @@ -117,6 +119,7 @@ pub fn phase_2(cfg: &mut BootConfig) { kdata_size, kdata_virt_offset, kernel_exception_sp, + kernel_irq_sp, krn_struct_start, ); } diff --git a/loader/src/platform/atsama5d27/boot.rs b/loader/src/platform/atsama5d27/boot.rs index ec65e60de..ce0e43c2f 100644 --- a/loader/src/platform/atsama5d27/boot.rs +++ b/loader/src/platform/atsama5d27/boot.rs @@ -12,11 +12,7 @@ use armv7::structures::paging::{ PAGE_TABLE_FLAGS, SMALL_PAGE_FLAGS, }; use armv7::{PhysicalAddress, VirtualAddress}; -use crate::consts::{ - FLG_R, FLG_U, FLG_VALID, FLG_W, FLG_X, GUARD_MEMORY_BYTES, KERNEL_ARGUMENT_OFFSET, - LOADER_CODE_ADDRESS, PAGE_TABLE_OFFSET, KERNEL_STACK_PAGE_COUNT, - EXCEPTION_STACK_TOP, -}; +use crate::consts::{FLG_R, FLG_U, FLG_VALID, FLG_W, FLG_X, GUARD_MEMORY_BYTES, KERNEL_ARGUMENT_OFFSET, LOADER_CODE_ADDRESS, PAGE_TABLE_OFFSET, KERNEL_STACK_PAGE_COUNT, EXCEPTION_STACK_TOP, IRQ_STACK_TOP}; use crate::{ bzero, println, BootConfig, XousPid, PAGE_SIZE, WORD_SIZE, }; @@ -394,6 +390,7 @@ pub fn map_kernel_to_processes( kdata_size: usize, kdata_virt_offset: usize, kernel_exception_sp: usize, + kernel_irq_sp: usize, krn_struct_start: usize, ) { let processes = unsafe { core::mem::transmute::<_, &[InitialProcess]>( &*cfg.processes) }; @@ -444,6 +441,17 @@ pub fn map_kernel_to_processes( ); } + println!("Mapping irq stack page to the process PID{}", process.asid); + let virt = IRQ_STACK_TOP; + let phys = kernel_irq_sp; + println!("MAP ({:08x}): {:08x} -> {:08x}", translation_table as usize, virt, phys); + cfg.map_page( + translation_table, + phys, + virt, + FLG_VALID | FLG_R | FLG_W, + ); + // TODO: For now, make the UART visible for all the processes. // Later on we may reuse the mapping made by the kernel. println!("Mapping UART registers to the process PID{}", process.asid); diff --git a/loader/src/platform/atsama5d27/consts.rs b/loader/src/platform/atsama5d27/consts.rs index 7ab990ce2..d77b632fc 100644 --- a/loader/src/platform/atsama5d27/consts.rs +++ b/loader/src/platform/atsama5d27/consts.rs @@ -14,6 +14,7 @@ pub const PAGE_TABLE_ROOT_OFFSET: usize = 0xff80_0000; pub const CONTEXT_OFFSET: usize = 0xff80_4000; pub const USER_AREA_END: usize = 0xfe00_0000; pub const EXCEPTION_STACK_TOP: usize = 0xffff_0000; +pub const IRQ_STACK_TOP: usize = 0xfffd_0000; pub const KERNEL_LOAD_OFFSET: usize = 0xffd0_0000; pub const KERNEL_STACK_TOP: usize = 0xfff8_0000; pub const KERNEL_ARGUMENT_OFFSET: usize = 0xffc0_0000; @@ -21,6 +22,7 @@ pub const GUARD_MEMORY_BYTES: usize = 2 * PAGE_SIZE; // Allocate more pages for the kernel stacks pub const KERNEL_STACK_PAGE_COUNT: usize = 4; +pub const IRQ_STACK_PAGE_COUNT: usize = 1; pub const FLG_VALID: usize = 0x1; pub const FLG_X: usize = 0x8; diff --git a/loader/src/platform/atsama5d27/debug.rs b/loader/src/platform/atsama5d27/debug.rs index f3702011a..7e17e3125 100644 --- a/loader/src/platform/atsama5d27/debug.rs +++ b/loader/src/platform/atsama5d27/debug.rs @@ -17,6 +17,7 @@ pub struct Uart { } impl Uart { + #[allow(dead_code)] pub fn new() -> Self { Self { inner: UartType::new(), @@ -89,7 +90,7 @@ fn print_l2_pagetable(vpn1: usize, phys_addr: PhysicalAddress) { let mut no_valid_items = true; for (i, pt_desc) in l2_pt.iter().enumerate() { - let virt_addr = (vpn1 << 20) | (i << 12); + let _virt_addr = (vpn1 << 20) | (i << 12); if let PageTableType::Invalid = pt_desc.get_type() { continue; @@ -97,16 +98,16 @@ fn print_l2_pagetable(vpn1: usize, phys_addr: PhysicalAddress) { no_valid_items = false; - let phys_addr = pt_desc.get_addr().expect("addr"); + let _phys_addr = pt_desc.get_addr().expect("addr"); match pt_desc.get_type() { PageTableType::LargePage => println!( " - {:02x} (64K) Large Page {:08x} -> {:08x}", - i, virt_addr, phys_addr + i, _virt_addr, _phys_addr ), PageTableType::SmallPage => println!( " - {:02x} (4K) Small Page {:08x} -> {:08x}", - i, virt_addr, phys_addr + i, _virt_addr, _phys_addr ), _ => (), } @@ -132,25 +133,25 @@ pub fn print_pagetable(root: usize) { match tt_desc.get_type() { TranslationTableType::Page => { - let virt_addr = i << 20; + let _virt_addr = i << 20; println!( " - {:03x} (1MB) {:08x} L2 page table @ {:08x}", - i, virt_addr, phys_addr + i, _virt_addr, phys_addr ); print_l2_pagetable(i, phys_addr); } TranslationTableType::Section => { - let virt_addr = i * 1024; // 1 MB + let _virt_addr = i * 1024; // 1 MB println!( " - {:03x} (1MB) section {:08x} -> {:08x}", - i, virt_addr, phys_addr + i, _virt_addr, phys_addr ); } TranslationTableType::Supersection => { - let virt_addr = i * (1024 * 16); // 16 MB + let _virt_addr = i * (1024 * 16); // 16 MB println!( " - {:03x} (16MB) supersection {:08x} -> {:08x}", - i, virt_addr, phys_addr + i, _virt_addr, phys_addr ); } diff --git a/loader/src/platform/atsama5d27/load.rs b/loader/src/platform/atsama5d27/load.rs index 4ce36d8d0..b78590b5d 100644 --- a/loader/src/platform/atsama5d27/load.rs +++ b/loader/src/platform/atsama5d27/load.rs @@ -2,7 +2,7 @@ // SPDX-FileCopyrightText: 2023 Foundation Devices, Inc // SPDX-License-Identifier: Apache-2.0 -use crate::consts::{CONTEXT_OFFSET, EXCEPTION_STACK_TOP, FLG_R, FLG_U, FLG_VALID, FLG_W, FLG_X, KERNEL_LOAD_OFFSET, PAGE_TABLE_ROOT_OFFSET, USER_AREA_END, USER_STACK_TOP, KERNEL_STACK_PAGE_COUNT, KERNEL_STACK_TOP}; +use crate::consts::{CONTEXT_OFFSET, EXCEPTION_STACK_TOP, FLG_R, FLG_U, FLG_VALID, FLG_W, FLG_X, KERNEL_LOAD_OFFSET, PAGE_TABLE_ROOT_OFFSET, USER_AREA_END, USER_STACK_TOP, KERNEL_STACK_PAGE_COUNT, KERNEL_STACK_TOP, IRQ_STACK_PAGE_COUNT, IRQ_STACK_TOP}; use crate::{ println, BootConfig, ProgramDescription, XousPid, PAGE_SIZE, STACK_PAGE_COUNT, VDBG, @@ -33,7 +33,7 @@ impl ProgramDescription { /// that is passed in should be used instead of `self.load_offset` /// for this reason. pub fn load(&self, allocator: &mut BootConfig, load_offset: usize, pid: XousPid) - -> (usize, usize, usize) { + -> (usize, usize, usize, usize) { assert_ne!(pid, 0, "PID must not be 0"); println!("Mapping PID {} into offset {:08x}", pid, load_offset); @@ -151,6 +151,27 @@ impl ProgramDescription { } } + // Allocate IRQ stack pages + let mut irq_sp = 0; + for i in 0..IRQ_STACK_PAGE_COUNT { + // If it's the kernel, also allocate an exception page + if is_kernel { + let sp_page = allocator.alloc() as usize; + // Remember only the first (top) exception stack page + if irq_sp == 0 { + irq_sp = sp_page; + } + println!("Allocated IRQ stack page: {:08x}", sp_page); + allocator.map_page( + translation_table, + sp_page, + (IRQ_STACK_TOP - 16 - PAGE_SIZE * i) & !(PAGE_SIZE - 1), + flag_defaults, + ); + allocator.change_owner(pid as XousPid, sp_page); + } + } + assert_eq!((self.text_offset as usize & (PAGE_SIZE - 1)), 0); assert_eq!((self.data_offset as usize & (PAGE_SIZE - 1)), 0); if allocator.no_copy { @@ -207,6 +228,6 @@ impl ProgramDescription { allocator.processes[pid_idx].sp = stack_addr; allocator.processes[pid_idx].asid = pid; - (text_phys_offset, data_phys_offset, exception_sp) + (text_phys_offset, data_phys_offset, exception_sp, irq_sp) } }