mirror of
https://github.com/saymrwulf/xous-core.git
synced 2026-11-15 08:30:02 +00:00
loader: include atsama5d27 arm platform
This commit is contained in:
parent
f93670a30d
commit
1867f6cf1a
12 changed files with 1267 additions and 1 deletions
|
|
@ -6,4 +6,9 @@ pub use precursor::*;
|
|||
#[cfg(any(feature="cramium-soc", feature="cramium-fpga"))]
|
||||
mod cramium;
|
||||
#[cfg(any(feature="cramium-soc", feature="cramium-fpga"))]
|
||||
pub use cramium::*;
|
||||
pub use cramium::*;
|
||||
|
||||
#[cfg(feature="atsama5d27")]
|
||||
pub mod atsama5d27;
|
||||
#[cfg(feature="atsama5d27")]
|
||||
pub use self::atsama5d27::*;
|
||||
|
|
|
|||
9
loader/src/platform/atsama5d27.rs
Normal file
9
loader/src/platform/atsama5d27.rs
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
// SPDX-FileCopyrightText: 2022 Sean Cross <sean@xobs.io>
|
||||
// SPDX-FileCopyrightText: 2023 Foundation Devices, Inc <hello@foundationdevices.com>
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
mod memtest;
|
||||
pub mod boot;
|
||||
pub mod load;
|
||||
mod panic;
|
||||
mod secboot;
|
||||
172
loader/src/platform/atsama5d27/asm.S
Normal file
172
loader/src/platform/atsama5d27/asm.S
Normal file
|
|
@ -0,0 +1,172 @@
|
|||
/*
|
||||
* SPDX-FileCopyrightText: 2023 Foundation Devices, Inc <hello@foundationdevices.com>
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#define FPU_NON_SECURE_ACCESS_OFFSET 10
|
||||
#define FPU_ACCESS_CONTROL_OFFSET 20
|
||||
#define FPU_FPEXC_EN_BIT 0x40000000
|
||||
|
||||
.section .text.init, "ax"
|
||||
.text
|
||||
|
||||
.global reset
|
||||
.align 4
|
||||
|
||||
reset:
|
||||
|
||||
/* Exception vectors (should be a branch to be detected as a valid code by the rom */
|
||||
_exception_vectors:
|
||||
b reset_vector /* reset */
|
||||
b undef_vector /* Undefined Instruction */
|
||||
b swi_vector /* Software Interrupt */
|
||||
b pabt_vector /* Prefetch Abort */
|
||||
b dabt_vector /* Data Abort */
|
||||
.word _romsize /* Size of the binary for ROMCode loading */
|
||||
b irq_vector /* IRQ : read the AIC */
|
||||
b fiq_vector /* FIQ */
|
||||
|
||||
undef_vector:
|
||||
bkpt /* Issue debugger breakpoint */
|
||||
b undef_vector
|
||||
swi_vector:
|
||||
bkpt /* Issue debugger breakpoint */
|
||||
b swi_vector
|
||||
pabt_vector:
|
||||
bkpt /* Issue debugger breakpoint */
|
||||
b pabt_vector
|
||||
dabt_vector:
|
||||
bkpt /* Issue debugger breakpoint */
|
||||
b dabt_vector
|
||||
rsvd_vector:
|
||||
bkpt /* Issue debugger breakpoint */
|
||||
b rsvd_vector
|
||||
irq_vector:
|
||||
bkpt /* Issue debugger breakpoint */
|
||||
b irq_vector
|
||||
fiq_vector:
|
||||
bkpt /* Issue debugger breakpoint */
|
||||
b fiq_vector
|
||||
reset_vector:
|
||||
|
||||
/*
|
||||
* We must configure VBAR for the reset vectors to be at
|
||||
* the start of memory (_mem_start)
|
||||
*/
|
||||
mrc p15, 0, r2, c12, c0, 0 /* Read VBAR into R2 */
|
||||
ldr r2, =_mem_start
|
||||
mcr p15, 0, r2, c12, c0, 0
|
||||
|
||||
/* Enable FPU */
|
||||
/* Grant non secure access for CP10 and CP11 */
|
||||
mrc p15, 0, r0, c1, c1, 2
|
||||
orr r0, r0, #3 << FPU_NON_SECURE_ACCESS_OFFSET
|
||||
mcr p15, 0, r0, c1, c1, 2
|
||||
/* Set CP10 and CP11 access permission (Privileged and User mode) */
|
||||
ldr r0, =(0xF << FPU_ACCESS_CONTROL_OFFSET)
|
||||
mcr p15, 0, r0, c1, c0, 2
|
||||
/* Set the FPEXC EN bit to enable the FPU (and NEON instructions) */
|
||||
mov r1, #FPU_FPEXC_EN_BIT
|
||||
vmsr FPEXC, r1
|
||||
|
||||
/* Init the stack */
|
||||
_init_stack:
|
||||
|
||||
ldr r0, =_top_of_memory
|
||||
|
||||
# Set the stack for IRQ mode
|
||||
msr cpsr_c, #0xd2
|
||||
mov sp, r0
|
||||
|
||||
# Set the stack for FIQ mode
|
||||
msr cpsr_c, #0xd1
|
||||
mov sp, r0
|
||||
|
||||
# Set the stack for Abort mode
|
||||
msr cpsr_c, #0xd7
|
||||
mov sp, r0
|
||||
|
||||
# Set the stack for Undefined Instruction mode
|
||||
msr cpsr_c, #0xdb
|
||||
mov sp, r0
|
||||
|
||||
# Back to Supervisor mode, (IRQ and FIQ both masked, Arm instruction
|
||||
# set) set the stack for Supervisor mode
|
||||
msr cpsr_c, #0xd3
|
||||
mov sp, r0
|
||||
|
||||
/* Clear Abort condition if it is pending with help of the abort handler */
|
||||
ldr r1, =(0x100)
|
||||
mrs r0, cpsr
|
||||
eor r0, r0, r1
|
||||
msr cpsr_x, r0
|
||||
nop
|
||||
eor r0, r0, r1
|
||||
msr cpsr_x, r0
|
||||
|
||||
/* Jump to the entrypoint */
|
||||
_branch_loader_main:
|
||||
# Loader arguments: kernel image address, signature
|
||||
ldr r0, =(0x20100000) /* The 0x20100000 is a hardcoded kernel image address in the DRAM for dev purposes */
|
||||
mov r1, #0
|
||||
bl rust_entry
|
||||
b .
|
||||
|
||||
.global start_kernel
|
||||
start_kernel:
|
||||
# Set TTBR0 and TTBR1 to point to the kernel page table stored in arg #2 (r1)
|
||||
mcr p15, 0, r1, c2, c0, 0
|
||||
mcr p15, 0, r1, c2, c0, 1
|
||||
|
||||
# Set TTBCR to the default value
|
||||
ldr r1, =(0x00)
|
||||
mcr p15, 0, r1, c2, c0, 2
|
||||
|
||||
# Disable domain permission checks for now
|
||||
ldr r1, =(0xffffffff)
|
||||
mcr p15, 0, r1, c3, c0, 0
|
||||
|
||||
# Configure the system
|
||||
mrc p15, 0, r1, c1, c0, 0
|
||||
orr r1, #0x1 /* MMU enable */
|
||||
orr r1, #0x10000000 /* TRE=1 */
|
||||
mcr p15, 0, r1, c1, c0, 0
|
||||
nop
|
||||
nop
|
||||
nop
|
||||
|
||||
# Prevent speculative access after MMU is enabled
|
||||
isb
|
||||
dsb
|
||||
|
||||
# Flush caches and branch predictor
|
||||
mcr p15, 0, r1, c7, c5, 0
|
||||
mcr p15, 0, r1, c7, c5, 6
|
||||
isb
|
||||
dsb
|
||||
|
||||
_prep_args:
|
||||
# Back up kernel stack pointer into r4
|
||||
mov r4, r0
|
||||
# Back up kernel entrypoint into r5
|
||||
mov r5, r2
|
||||
|
||||
# Prepare arguments for the kernel `init` function (entrypoint)
|
||||
mov r0, r3
|
||||
pop {r2, r1}
|
||||
|
||||
# Restore kernel stack pointer from the r4
|
||||
mov sp, r4
|
||||
|
||||
# Make room for `r11` and `lr` on the stack.
|
||||
# Because the kernel `init` function's prelude will push these registers onto the stack.
|
||||
# But currently the `sp` points at the very end of the stack area and without this correction,
|
||||
# pushing more values overflows it
|
||||
sub sp, #8
|
||||
|
||||
# Jump to kernel
|
||||
bx r5
|
||||
|
||||
.global __aeabi_unwind_cpp_pr0
|
||||
__aeabi_unwind_cpp_pr0:
|
||||
b __aeabi_unwind_cpp_pr0
|
||||
18
loader/src/platform/atsama5d27/asm.rs
Normal file
18
loader/src/platform/atsama5d27/asm.rs
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
// SPDX-FileCopyrightText: 2022 Sean Cross <sean@xobs.io>
|
||||
// SPDX-FileCopyrightText: 2023 Foundation Devices, Inc <hello@foundationdevices.com>
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
#![cfg_attr(feature = "atsama5d27", allow(dead_code))]
|
||||
|
||||
extern "C" {
|
||||
pub fn start_kernel(
|
||||
stack: usize, // r0
|
||||
ttbr: usize, // r1
|
||||
entrypoint: usize, // r2
|
||||
args: usize, // r3
|
||||
ip: usize,
|
||||
rpt: usize,
|
||||
debug: bool,
|
||||
resume: bool,
|
||||
) -> !;
|
||||
}
|
||||
491
loader/src/platform/atsama5d27/boot.rs
Normal file
491
loader/src/platform/atsama5d27/boot.rs
Normal file
|
|
@ -0,0 +1,491 @@
|
|||
// SPDX-FileCopyrightText: 2022 Sean Cross <sean@xobs.io>
|
||||
// SPDX-FileCopyrightText: 2023 Foundation Devices, Inc <hello@foundationdevices.com>
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
use core::mem;
|
||||
use core::num::NonZeroUsize;
|
||||
|
||||
pub use armv7::structures::paging::{
|
||||
PageTable as L2PageTable, TranslationTable, TranslationTableDescriptor,
|
||||
TranslationTableType, PageTableDescriptor, PAGE_TABLE_SIZE, PageTableType,
|
||||
TranslationTableMemory, PageTableMemory,
|
||||
PAGE_TABLE_FLAGS, SMALL_PAGE_FLAGS,
|
||||
};
|
||||
pub 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::{
|
||||
bzero, println, BootConfig, XousPid, PAGE_SIZE, WORD_SIZE,
|
||||
};
|
||||
use crate::platform::atsama5d27::load::InitialProcess;
|
||||
|
||||
const DEBUG_PAGE_MAPPING: bool = false;
|
||||
macro_rules! dprint {
|
||||
($($args:tt)*) => ({
|
||||
if DEBUG_PAGE_MAPPING {
|
||||
crate::print!($($args)*)
|
||||
}
|
||||
});
|
||||
}
|
||||
macro_rules! dprintln {
|
||||
($($args:tt)*) => ({
|
||||
if DEBUG_PAGE_MAPPING {
|
||||
crate::println!($($args)*)
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
impl BootConfig {
|
||||
pub fn get_top(&self) -> *mut usize {
|
||||
let val = unsafe {
|
||||
self.sram_start.add(
|
||||
(self.sram_size - self.init_size - self.extra_pages * PAGE_SIZE)
|
||||
/ mem::size_of::<usize>(),
|
||||
)
|
||||
};
|
||||
assert!((val as usize) >= (self.sram_start as usize));
|
||||
assert!(
|
||||
(val as usize) < (self.sram_start as usize) + self.sram_size,
|
||||
"top address {:08x} > (start + size) {:08x} + {} = {:08x}",
|
||||
val as usize,
|
||||
self.sram_start as usize,
|
||||
self.sram_size,
|
||||
self.sram_start as usize + self.sram_size
|
||||
);
|
||||
val
|
||||
}
|
||||
|
||||
/// Zero-alloc a new page, mark it as owned by PID1, and return it.
|
||||
/// Decrement the `next_page_offset` (npo) variable by one page.
|
||||
pub fn alloc(&mut self) -> *mut usize {
|
||||
self.extra_pages += 1;
|
||||
let pg = self.get_top();
|
||||
unsafe {
|
||||
// Grab the page address and zero it out
|
||||
bzero(
|
||||
pg as *mut usize,
|
||||
pg.add(PAGE_SIZE / mem::size_of::<usize>()) as *mut usize,
|
||||
);
|
||||
}
|
||||
// Mark this page as in-use by the kernel
|
||||
let extra_bytes = self.extra_pages * PAGE_SIZE;
|
||||
self.runtime_page_tracker[(self.sram_size - (extra_bytes + self.init_size)) / PAGE_SIZE] =
|
||||
1;
|
||||
|
||||
dprintln!("Allocated a physical page: {:08x}", pg as usize);
|
||||
|
||||
// Return the address
|
||||
pg as *mut usize
|
||||
}
|
||||
|
||||
/// Allocates four 4K pages for L1 translation table
|
||||
/// May waste some pages as dummy pages due to alignment requirements.
|
||||
/// Sometimes there are none, but could be up to 3 pages wasted.
|
||||
pub fn alloc_l1_page_table(&mut self, pid: XousPid) -> *mut usize {
|
||||
// ARMv7A Level 1 Translation Table is required to be aligned at 16K boundary
|
||||
const ALIGNMENT_16K: usize = 16 * 1024;
|
||||
|
||||
// It should take no more than 4 tries to get to the next 16K-aligned 4K sized page
|
||||
let mut num_alloc_pages = 0;
|
||||
for _ in 0..4 {
|
||||
let mut allocated_page_ptr = self.alloc();
|
||||
num_alloc_pages += 1;
|
||||
let is_aligned = allocated_page_ptr as usize & (ALIGNMENT_16K - 1) == 0;
|
||||
self.change_owner(pid, allocated_page_ptr as usize);
|
||||
|
||||
if is_aligned {
|
||||
return if num_alloc_pages != 4 {
|
||||
dprintln!("Allocated a dummy page (aligned but not enough pages allocated yet): {:08x}", allocated_page_ptr as usize);
|
||||
|
||||
// Allocate 4 more pages for a whole L1 translation table
|
||||
for _ in 0..4 {
|
||||
dprintln!(
|
||||
"Allocated a page {:08x} for PID {} L1 page table",
|
||||
allocated_page_ptr as usize,
|
||||
pid
|
||||
);
|
||||
allocated_page_ptr = self.alloc();
|
||||
self.change_owner(pid, allocated_page_ptr as usize);
|
||||
}
|
||||
|
||||
dprintln!(
|
||||
"Allocated a L1 page table at {:08x}",
|
||||
allocated_page_ptr as usize
|
||||
);
|
||||
|
||||
allocated_page_ptr
|
||||
} else {
|
||||
allocated_page_ptr
|
||||
};
|
||||
} else {
|
||||
dprintln!(
|
||||
"Allocated a dummy page for alignment: {:08x}",
|
||||
allocated_page_ptr as usize
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
unreachable!("Couldn't allocate a 16K-aligned page for L1 page table base")
|
||||
}
|
||||
|
||||
pub fn change_owner(&mut self, pid: XousPid, addr: usize) {
|
||||
dprintln!("A new owner of {:08x} page is {}", addr, pid);
|
||||
|
||||
// First, check to see if the region is in RAM,
|
||||
if addr >= self.sram_start as usize && addr < self.sram_start as usize + self.sram_size {
|
||||
// Mark this page as in-use by the PID
|
||||
self.runtime_page_tracker[(addr - self.sram_start as usize) / PAGE_SIZE] = pid;
|
||||
return;
|
||||
}
|
||||
// The region isn't in RAM, so check the other memory regions.
|
||||
let mut rpt_offset = self.sram_size / PAGE_SIZE;
|
||||
|
||||
for region in self.regions.iter() {
|
||||
let rstart = region.start as usize;
|
||||
let rlen = region.length as usize;
|
||||
if addr >= rstart && addr < rstart + rlen {
|
||||
self.runtime_page_tracker[rpt_offset + (addr - rstart) / PAGE_SIZE] = pid;
|
||||
return;
|
||||
}
|
||||
rpt_offset += rlen / PAGE_SIZE;
|
||||
}
|
||||
panic!(
|
||||
"Tried to change region {:08x} that isn't in defined memory!",
|
||||
addr
|
||||
);
|
||||
}
|
||||
|
||||
/// Map the given page to the specified process table. If necessary,
|
||||
/// allocate a new page.
|
||||
///
|
||||
/// # Panics
|
||||
///
|
||||
/// * If you try to map a page twice
|
||||
pub fn map_page(
|
||||
&mut self,
|
||||
translation_table: *mut TranslationTableMemory,
|
||||
phys: usize,
|
||||
virt: usize,
|
||||
flags: usize,
|
||||
) {
|
||||
match WORD_SIZE {
|
||||
4 => self.map_page_32(translation_table, phys, virt, flags),
|
||||
8 => panic!("map_page doesn't work on 64-bit devices"),
|
||||
_ => panic!("unrecognized word size: {}", WORD_SIZE),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn map_page_32(
|
||||
&mut self,
|
||||
translation_table: *mut TranslationTableMemory,
|
||||
phys: usize,
|
||||
virt: usize,
|
||||
flags: usize,
|
||||
) {
|
||||
dprintln!(
|
||||
"PageTable: {:p} {:08x}",
|
||||
translation_table,
|
||||
translation_table as usize
|
||||
);
|
||||
dprint!("MAP: p0x{:08x} -> v0x{:08x} ", phys, virt);
|
||||
print_flags(flags);
|
||||
dprintln!();
|
||||
|
||||
let v = VirtualAddress::new(virt as u32);
|
||||
let vpn1 = v.translation_table_index();
|
||||
let vpn2 = v.page_table_index();
|
||||
|
||||
let p = phys & !(0xfff);
|
||||
let ppn2 = (p >> 12) & 0xff;
|
||||
|
||||
assert!(vpn1 < 4096);
|
||||
assert!(vpn2 < 256);
|
||||
assert!(ppn2 < 256);
|
||||
|
||||
dprintln!(
|
||||
"vpn1: {:04x}, vpn2: {:02x}, ppn2: {:08x}, phys frame addr: {:08x}",
|
||||
vpn1,
|
||||
vpn2,
|
||||
ppn2,
|
||||
p
|
||||
);
|
||||
|
||||
let mut tt = TranslationTable::new(translation_table);
|
||||
let tt = unsafe { tt.table_mut() };
|
||||
let mut new_addr = None;
|
||||
|
||||
// Allocate a new level 1 translation table entry if one doesn't exist.
|
||||
dprintln!("tt[{:08x}] = {:032b}", vpn1, tt[vpn1]);
|
||||
|
||||
if tt[vpn1].get_type() == TranslationTableType::Invalid {
|
||||
dprintln!("Previously unmapped L1 entry");
|
||||
|
||||
let na = self.alloc();
|
||||
let phys = PhysicalAddress::from_ptr(na);
|
||||
let entry_flags = u32::from(PAGE_TABLE_FLAGS::VALID::Enable)
|
||||
| u32::from(PAGE_TABLE_FLAGS::DOMAIN.val(0xf));
|
||||
let descriptor =
|
||||
TranslationTableDescriptor::new(TranslationTableType::Page, phys, entry_flags)
|
||||
.expect("tt descriptor");
|
||||
dprintln!("New TT descriptor: {:032b}", descriptor);
|
||||
tt[vpn1] = descriptor;
|
||||
|
||||
dprintln!("new tt[{:08x}] = {:032b}", vpn1, tt[vpn1]);
|
||||
new_addr = Some(NonZeroUsize::new(na as usize).unwrap());
|
||||
}
|
||||
|
||||
let existing_entry = tt[vpn1];
|
||||
dprintln!("existing tt[{:08x}] = {:032b}", vpn1, existing_entry);
|
||||
match existing_entry.get_type() {
|
||||
TranslationTableType::Page => {
|
||||
let l2_phys_addr = existing_entry.get_addr().expect("invalid l1 entry");
|
||||
let ptr: *mut PageTableMemory = l2_phys_addr.as_mut_ptr();
|
||||
let mut l2_pt = unsafe { L2PageTable::new_from_ptr(ptr) };
|
||||
let l2_pt = unsafe { l2_pt.table_mut() };
|
||||
|
||||
dprintln!("l2 ptr: {:p}", l2_pt);
|
||||
|
||||
let existing_l2_entry = l2_pt[vpn2];
|
||||
|
||||
dprintln!(
|
||||
"({:08x}) l2_pt[{:08x}] = {:032b}",
|
||||
l2_phys_addr,
|
||||
vpn2,
|
||||
existing_l2_entry
|
||||
);
|
||||
|
||||
if existing_l2_entry.get_type() == PageTableType::SmallPage {
|
||||
let mapped_addr = existing_l2_entry
|
||||
.get_addr()
|
||||
.expect("invalid l2 entry")
|
||||
.as_u32() as usize;
|
||||
|
||||
dprintln!(
|
||||
"L2 entry {:02x} already mapped to {:08x}",
|
||||
vpn2,
|
||||
mapped_addr
|
||||
);
|
||||
|
||||
// Ensure the entry hasn't already been mapped to a different address.
|
||||
if mapped_addr != p {
|
||||
panic!(
|
||||
"Page {:08x} was already allocated to {:08x}, so cannot map to {:08x}!",
|
||||
virt, mapped_addr, phys
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// Map the L2 entry
|
||||
let mut small_page_flags = 0;
|
||||
let is_valid = flags & FLG_VALID != 0;
|
||||
if is_valid {
|
||||
small_page_flags |= u32::from(SMALL_PAGE_FLAGS::VALID::Enable);
|
||||
|
||||
if flags & FLG_X == 0 {
|
||||
small_page_flags |= u32::from(SMALL_PAGE_FLAGS::XN::Enable);
|
||||
}
|
||||
}
|
||||
|
||||
if flags & FLG_U != 0 {
|
||||
small_page_flags |= u32::from(SMALL_PAGE_FLAGS::AP::FullAccess);
|
||||
}
|
||||
if flags & FLG_W == 0 {
|
||||
small_page_flags |= u32::from(SMALL_PAGE_FLAGS::AP2::Enable);
|
||||
}
|
||||
|
||||
let new_entry = PageTableDescriptor::new(
|
||||
PageTableType::SmallPage,
|
||||
PhysicalAddress::new(p as u32),
|
||||
small_page_flags,
|
||||
)
|
||||
.expect("new l2 entry");
|
||||
l2_pt[vpn2] = new_entry;
|
||||
dprintln!(
|
||||
"new ({:08x}) l2_pt[{:08x}] = {:032b}",
|
||||
l2_phys_addr,
|
||||
vpn2,
|
||||
l2_pt[vpn2]
|
||||
);
|
||||
|
||||
// If we had to allocate a translation table (L1) entry, ensure that it's
|
||||
// mapped into our address space, owned by PID 1.
|
||||
if let Some(addr) = new_addr {
|
||||
let page_virt_addr = PAGE_TABLE_OFFSET + vpn1 * PAGE_SIZE;
|
||||
dprintln!(
|
||||
">>> Recursively mapping new address {:08x} -> {:08x}",
|
||||
addr.get(),
|
||||
page_virt_addr,
|
||||
);
|
||||
self.map_page(
|
||||
translation_table,
|
||||
addr.get(),
|
||||
page_virt_addr,
|
||||
FLG_R | FLG_W | FLG_VALID,
|
||||
);
|
||||
self.change_owner(1 as XousPid, addr.get());
|
||||
dprintln!("<<< Done mapping new address");
|
||||
}
|
||||
}
|
||||
|
||||
_ => panic!(
|
||||
"Invalid translation table entry type: {:?}",
|
||||
existing_entry.get_type()
|
||||
),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn map_structs_to_kernel(cfg: &mut BootConfig, table_addr: usize, krn_struct_start: usize) {
|
||||
let tt = table_addr as *mut TranslationTableMemory;
|
||||
|
||||
// Create a transparent mapping for a single page of the loader code.
|
||||
// The loader code will setup and enable MMU and then jump to the kernel entrypoint.
|
||||
// We want to map the same virtual address to the same physical address so it won't fail
|
||||
// as soon as the MMU is enabled
|
||||
dprintln!("Making the first 4K of the loader code visible");
|
||||
let translation_table = cfg.processes[0].ttbr0 as *mut TranslationTableMemory;
|
||||
cfg.map_page(
|
||||
translation_table,
|
||||
LOADER_CODE_ADDRESS,
|
||||
LOADER_CODE_ADDRESS,
|
||||
FLG_R | FLG_X | FLG_VALID,
|
||||
);
|
||||
|
||||
// Map the last stack page (4K) to the kernel to make it visible from the trampoline code
|
||||
// Otherwise some arguments passed via stack won't be available after the MMU is turned on
|
||||
dprintln!("Making the loader stack visible");
|
||||
cfg.map_page(
|
||||
translation_table,
|
||||
0x200ff000,
|
||||
0x200ff000,
|
||||
FLG_R | FLG_X | FLG_VALID,
|
||||
);
|
||||
|
||||
// Identity map the page that contains kernel arguments
|
||||
dprintln!("Making the kernel arguments visible");
|
||||
cfg.map_page(
|
||||
translation_table,
|
||||
0x20100000,
|
||||
0x20100000,
|
||||
FLG_R | FLG_X | FLG_VALID,
|
||||
);
|
||||
|
||||
for addr in (0..cfg.init_size - GUARD_MEMORY_BYTES).step_by(PAGE_SIZE) {
|
||||
cfg.map_page(
|
||||
tt,
|
||||
addr + krn_struct_start,
|
||||
addr + KERNEL_ARGUMENT_OFFSET,
|
||||
FLG_R | FLG_W | FLG_VALID,
|
||||
);
|
||||
cfg.change_owner(1 as XousPid, addr + krn_struct_start);
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
pub fn map_kernel_to_processes(
|
||||
cfg: &mut BootConfig,
|
||||
ktext_offset: usize,
|
||||
ktext_size: usize,
|
||||
ktext_virt_offset: usize,
|
||||
kdata_offset: usize,
|
||||
kdata_size: usize,
|
||||
kdata_virt_offset: usize,
|
||||
kernel_exception_sp: usize,
|
||||
krn_struct_start: usize,
|
||||
) {
|
||||
let processes = unsafe { core::mem::transmute::<_, &[InitialProcess]>( &*cfg.processes) };
|
||||
|
||||
assert_ne!(kernel_exception_sp, 0, "No exception stack allocated for the kernel!");
|
||||
|
||||
for process in processes[1..].iter() {
|
||||
println!("Mapping kernel (PID1) text to process PID{}", process.asid);
|
||||
println!("Offset: {:08x}, size: {:08x}", ktext_offset, ktext_size);
|
||||
let translation_table = process.ttbr0 as *mut TranslationTableMemory;
|
||||
for addr in (0..ktext_size).step_by(PAGE_SIZE) {
|
||||
let phys = ktext_offset + addr;
|
||||
let virt = ktext_virt_offset + addr;
|
||||
println!("MAP ({:08x}): {:08x} -> {:08x}", translation_table as usize, virt, phys);
|
||||
|
||||
cfg.map_page(
|
||||
translation_table,
|
||||
phys,
|
||||
virt,
|
||||
FLG_VALID | FLG_R | FLG_X,
|
||||
);
|
||||
}
|
||||
println!("Mapping kernel (PID1) data to process PID{}", process.asid);
|
||||
println!("Offset: {:08x}, size: {:08x}", kdata_offset, kdata_size);
|
||||
for addr in (0..kdata_size).step_by(PAGE_SIZE) {
|
||||
let phys = kdata_offset + addr;
|
||||
let virt = kdata_virt_offset + addr;
|
||||
println!("MAP ({:08x}): {:08x} -> {:08x}", translation_table as usize, virt, phys);
|
||||
|
||||
cfg.map_page(
|
||||
translation_table,
|
||||
phys,
|
||||
virt,
|
||||
FLG_VALID | FLG_R | FLG_W,
|
||||
);
|
||||
}
|
||||
|
||||
println!("Mapping kernel exception stack pages to the process PID{}", process.asid);
|
||||
for i in 0..KERNEL_STACK_PAGE_COUNT {
|
||||
let virt = EXCEPTION_STACK_TOP - (PAGE_SIZE * KERNEL_STACK_PAGE_COUNT) + (PAGE_SIZE * i);
|
||||
let phys = kernel_exception_sp - (PAGE_SIZE * KERNEL_STACK_PAGE_COUNT) + (PAGE_SIZE * (i + 1));
|
||||
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);
|
||||
for i in 0..4 {
|
||||
let virt = 0xffcf_0000 + (i * PAGE_SIZE);
|
||||
let phys = 0xf802_0000 + (i * PAGE_SIZE);
|
||||
println!("MAP ({:08x}): {:08x} -> {:08x}", translation_table as usize, virt, phys);
|
||||
cfg.map_page(
|
||||
translation_table,
|
||||
phys,
|
||||
virt,
|
||||
FLG_VALID | FLG_R | FLG_W | FLG_X,
|
||||
);
|
||||
}
|
||||
|
||||
println!("Mapping kernel structures to PID{}", process.asid);
|
||||
for addr in (0..cfg.init_size - GUARD_MEMORY_BYTES).step_by(PAGE_SIZE) {
|
||||
cfg.map_page(
|
||||
translation_table,
|
||||
addr + krn_struct_start,
|
||||
addr + KERNEL_ARGUMENT_OFFSET,
|
||||
FLG_R | FLG_W | FLG_VALID,
|
||||
);
|
||||
cfg.change_owner(1 as XousPid, addr + krn_struct_start);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn print_flags(flags: usize) {
|
||||
if flags & FLG_R != 0 {
|
||||
dprint!("R");
|
||||
}
|
||||
if flags & FLG_W != 0 {
|
||||
dprint!("W");
|
||||
}
|
||||
if flags & FLG_X != 0 {
|
||||
dprint!("X");
|
||||
}
|
||||
if flags & FLG_VALID != 0 {
|
||||
dprint!("V");
|
||||
}
|
||||
if flags & FLG_U != 0 {
|
||||
dprint!("U");
|
||||
}
|
||||
}
|
||||
29
loader/src/platform/atsama5d27/consts.rs
Normal file
29
loader/src/platform/atsama5d27/consts.rs
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
// SPDX-FileCopyrightText: 2022 Sean Cross <sean@xobs.io>
|
||||
// SPDX-FileCopyrightText: 2023 Foundation Devices, Inc <hello@foundationdevices.com>
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
use crate::PAGE_SIZE;
|
||||
|
||||
/// Virtual memory location of stack pages for kernel and processes.
|
||||
pub const USER_STACK_TOP: usize = 0x8000_0000;
|
||||
|
||||
pub const LOADER_CODE_ADDRESS: usize = 0x20000000;
|
||||
|
||||
pub const PAGE_TABLE_OFFSET: usize = 0xfefe_0000;
|
||||
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 KERNEL_LOAD_OFFSET: usize = 0xffd0_0000;
|
||||
pub const KERNEL_STACK_TOP: usize = 0xfff8_0000;
|
||||
pub const KERNEL_ARGUMENT_OFFSET: usize = 0xffc0_0000;
|
||||
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 FLG_VALID: usize = 0x1;
|
||||
pub const FLG_X: usize = 0x8;
|
||||
pub const FLG_W: usize = 0x4;
|
||||
pub const FLG_R: usize = 0x2;
|
||||
pub const FLG_U: usize = 0x10;
|
||||
161
loader/src/platform/atsama5d27/debug.rs
Normal file
161
loader/src/platform/atsama5d27/debug.rs
Normal file
|
|
@ -0,0 +1,161 @@
|
|||
// SPDX-FileCopyrightText: 2022 Sean Cross <sean@xobs.io>
|
||||
// SPDX-FileCopyrightText: 2023 Foundation Devices, Inc <hello@foundationdevices.com>
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
use atsama5d27::uart::{Uart as UartHw, Uart1};
|
||||
pub use armv7::structures::paging::{
|
||||
PageTable as L2PageTable, TranslationTable, TranslationTableDescriptor,
|
||||
TranslationTableType, PageTableDescriptor, PAGE_TABLE_SIZE, PageTableType,
|
||||
TranslationTableMemory, PageTableMemory,
|
||||
PAGE_TABLE_FLAGS, SMALL_PAGE_FLAGS,
|
||||
};
|
||||
pub use armv7::{PhysicalAddress, VirtualAddress};
|
||||
|
||||
type UartType = UartHw<Uart1>;
|
||||
|
||||
pub struct Uart {
|
||||
inner: UartType,
|
||||
}
|
||||
|
||||
impl Uart {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
inner: UartType::new(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
use core::fmt::{Error, Write};
|
||||
|
||||
impl Write for Uart {
|
||||
fn write_str(&mut self, s: &str) -> Result<(), Error> {
|
||||
self.inner.write_str(s);
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
#[macro_use]
|
||||
#[cfg(all(not(test), feature = "debug-print"))]
|
||||
pub mod debug_print_hardware {
|
||||
#[macro_export]
|
||||
macro_rules! print
|
||||
{
|
||||
($($args:tt)+) => ({
|
||||
use core::fmt::Write;
|
||||
let _ = write!($crate::debug::Uart::new(), $($args)+);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
#[macro_use]
|
||||
#[cfg(all(not(test), not(feature = "debug-print")))]
|
||||
mod debug_print_hardware {
|
||||
#[macro_export]
|
||||
#[allow(unused_variables)]
|
||||
macro_rules! print {
|
||||
($($args:tt)+) => {{}};
|
||||
}
|
||||
}
|
||||
|
||||
#[macro_use]
|
||||
#[cfg(test)]
|
||||
mod debug_print_hardware {
|
||||
#[macro_export]
|
||||
#[allow(unused_variables)]
|
||||
macro_rules! print {
|
||||
($($args:tt)+) => ({
|
||||
std::print!($($args)+)
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! println
|
||||
{
|
||||
() => ({
|
||||
$crate::print!("\r\n")
|
||||
});
|
||||
($fmt:expr) => ({
|
||||
$crate::print!(concat!($fmt, "\r\n"))
|
||||
});
|
||||
($fmt:expr, $($args:tt)+) => ({
|
||||
$crate::print!(concat!($fmt, "\r\n"), $($args)+)
|
||||
});
|
||||
}
|
||||
|
||||
fn print_l2_pagetable(vpn1: usize, phys_addr: PhysicalAddress) {
|
||||
let ptr: *mut PageTableMemory = phys_addr.as_mut_ptr();
|
||||
let mut l2_pt = unsafe { L2PageTable::new_from_ptr(ptr) };
|
||||
let l2_pt = unsafe { l2_pt.table_mut() };
|
||||
|
||||
let mut no_valid_items = true;
|
||||
for (i, pt_desc) in l2_pt.iter().enumerate() {
|
||||
let virt_addr = (vpn1 << 20) | (i << 12);
|
||||
|
||||
if let PageTableType::Invalid = pt_desc.get_type() {
|
||||
continue;
|
||||
}
|
||||
|
||||
no_valid_items = false;
|
||||
|
||||
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
|
||||
),
|
||||
PageTableType::SmallPage => println!(
|
||||
" - {:02x} (4K) Small Page {:08x} -> {:08x}",
|
||||
i, virt_addr, phys_addr
|
||||
),
|
||||
_ => (),
|
||||
}
|
||||
}
|
||||
|
||||
if no_valid_items {
|
||||
println!(" - <no valid items>");
|
||||
}
|
||||
}
|
||||
|
||||
pub fn print_pagetable(root: usize) {
|
||||
println!("Memory Maps (Root: {:08x}):", root,);
|
||||
|
||||
let tt_ptr = root as *mut TranslationTableMemory;
|
||||
let tt = TranslationTable::new(tt_ptr);
|
||||
|
||||
for (i, tt_desc) in tt.table().iter().enumerate() {
|
||||
if let TranslationTableType::Invalid = tt_desc.get_type() {
|
||||
continue;
|
||||
}
|
||||
|
||||
let phys_addr = tt_desc.get_addr().expect("addr");
|
||||
|
||||
match tt_desc.get_type() {
|
||||
TranslationTableType::Page => {
|
||||
let virt_addr = i << 20;
|
||||
println!(
|
||||
" - {:03x} (1MB) {:08x} L2 page table @ {:08x}",
|
||||
i, virt_addr, phys_addr
|
||||
);
|
||||
print_l2_pagetable(i, phys_addr);
|
||||
}
|
||||
TranslationTableType::Section => {
|
||||
let virt_addr = i * 1024; // 1 MB
|
||||
println!(
|
||||
" - {:03x} (1MB) section {:08x} -> {:08x}",
|
||||
i, virt_addr, phys_addr
|
||||
);
|
||||
}
|
||||
TranslationTableType::Supersection => {
|
||||
let virt_addr = i * (1024 * 16); // 16 MB
|
||||
println!(
|
||||
" - {:03x} (16MB) supersection {:08x} -> {:08x}",
|
||||
i, virt_addr, phys_addr
|
||||
);
|
||||
}
|
||||
|
||||
_ => (),
|
||||
}
|
||||
}
|
||||
}
|
||||
130
loader/src/platform/atsama5d27/link.x
Normal file
130
loader/src/platform/atsama5d27/link.x
Normal file
|
|
@ -0,0 +1,130 @@
|
|||
MEMORY
|
||||
{
|
||||
RAM : ORIGIN = 0x20000000, LENGTH = 1M
|
||||
}
|
||||
|
||||
PROVIDE(_mem_start = ORIGIN(RAM)); /* Start of the memory */
|
||||
PROVIDE(_top_of_memory = ORIGIN(RAM) + LENGTH(RAM)); /* Top of the memory */
|
||||
|
||||
ENTRY(reset);
|
||||
|
||||
REGION_ALIAS("REGION_TEXT", RAM);
|
||||
REGION_ALIAS("REGION_RODATA", RAM);
|
||||
REGION_ALIAS("REGION_DATA", RAM);
|
||||
REGION_ALIAS("REGION_BSS", RAM);
|
||||
REGION_ALIAS("REGION_STACK", RAM);
|
||||
REGION_ALIAS("REGION_HEAP", RAM);
|
||||
|
||||
/* Size of the main kernel stack */
|
||||
_stack_size = 16K;
|
||||
_eheap = ORIGIN(RAM) + LENGTH(RAM);
|
||||
|
||||
PROVIDE(_stext = ORIGIN(REGION_TEXT));
|
||||
PROVIDE(_stack_start = ORIGIN(REGION_STACK) + LENGTH(REGION_STACK));
|
||||
PROVIDE(_max_hart_id = 0);
|
||||
PROVIDE(_hart_stack_size = 2K);
|
||||
PROVIDE(_heap_size = 0);
|
||||
|
||||
SECTIONS
|
||||
{
|
||||
. = ALIGN(4);
|
||||
.text.dummy (NOLOAD) :
|
||||
{
|
||||
/* This section is intended to make _stext address work */
|
||||
. = ABSOLUTE(_stext);
|
||||
} > REGION_TEXT
|
||||
|
||||
.text _stext :
|
||||
{
|
||||
*(.text); /* Place .text section first so that the reset vector is always placed at the load address */
|
||||
*(.text.*); /* Place other code sections next */
|
||||
} > REGION_TEXT
|
||||
|
||||
.rodata : ALIGN(4)
|
||||
{
|
||||
*(.rodata .rodata.*);
|
||||
|
||||
/* 4-byte align the end (VMA) of this section.
|
||||
This is required by LLD to ensure the LMA of the following .data
|
||||
section will have the correct alignment. */
|
||||
. = ALIGN(4);
|
||||
_etext = .;
|
||||
} > REGION_RODATA
|
||||
|
||||
.data : ALIGN(4)
|
||||
{
|
||||
_sidata = LOADADDR(.data);
|
||||
_sdata = .;
|
||||
/* Must be called __global_pointer$ for linker relaxations to work. */
|
||||
PROVIDE(__global_pointer$ = . + 0x800);
|
||||
*(.sdata .sdata.* .sdata2 .sdata2.*);
|
||||
*(.data .data.*);
|
||||
. = ALIGN(4);
|
||||
_edata = .;
|
||||
} > REGION_DATA AT > REGION_RODATA
|
||||
|
||||
.bss (NOLOAD) :
|
||||
{
|
||||
_sbss = .;
|
||||
*(.sbss .sbss.* .bss .bss.*);
|
||||
. = ALIGN(4);
|
||||
_ebss = .;
|
||||
} > REGION_BSS
|
||||
|
||||
/* fictitious region that represents the memory available for the stack */
|
||||
.stack (NOLOAD) :
|
||||
{
|
||||
_sstack = .;
|
||||
. += _stack_size;
|
||||
. = ALIGN(4096);
|
||||
_estack = .;
|
||||
} > REGION_STACK
|
||||
|
||||
/* fictitious region that represents the memory available for the heap */
|
||||
.heap (NOLOAD) :
|
||||
{
|
||||
. = ALIGN(4);
|
||||
_sheap = .;
|
||||
/* _eheap is defined elsewhere and is the remainder of RAM */
|
||||
} > REGION_HEAP
|
||||
|
||||
/* fake output .got section */
|
||||
/* Dynamic relocations are unsupported. This section is only used to detect
|
||||
relocatable code in the input files and raise an error if relocatable code
|
||||
is found */
|
||||
.got (INFO) :
|
||||
{
|
||||
KEEP(*(.got .got.*));
|
||||
}
|
||||
|
||||
/* Discard .eh_frame, we are not doing unwind on panic so it is not needed */
|
||||
/DISCARD/ :
|
||||
{
|
||||
*(.eh_frame);
|
||||
*(.eh_frame_hdr);
|
||||
}
|
||||
}
|
||||
|
||||
PROVIDE(_romsize = _edata - _stext);
|
||||
PROVIDE(_sramsize = _ebss - _stext);
|
||||
|
||||
end = .; /* define a global symbol marking the end of application */
|
||||
|
||||
/* Do not exceed this mark in the error messages below | */
|
||||
ASSERT(ORIGIN(RAM) % 4 == 0, "
|
||||
ERROR(arm-rt): the start of the RAM must be 4-byte aligned");
|
||||
|
||||
ASSERT(_sdata % 4 == 0 && _edata % 4 == 0, "
|
||||
BUG(arm-rt): .data is not 4-byte aligned");
|
||||
|
||||
ASSERT(_sbss % 4 == 0 && _ebss % 4 == 0, "
|
||||
BUG(arm-rt): .bss is not 4-byte aligned");
|
||||
|
||||
ASSERT(SIZEOF(.got) == 0, "
|
||||
.got section detected in the input files. Dynamic relocations are not
|
||||
supported. If you are linking to C code compiled using the `gcc` crate
|
||||
then modify your build script to compile the C code _without_ the
|
||||
-fPIC flag. See the documentation of the `gcc::Config.fpic` method for
|
||||
details.");
|
||||
|
||||
/* Do not exceed this mark in the error messages above | */
|
||||
218
loader/src/platform/atsama5d27/load.rs
Normal file
218
loader/src/platform/atsama5d27/load.rs
Normal file
|
|
@ -0,0 +1,218 @@
|
|||
// SPDX-FileCopyrightText: 2022 Sean Cross <sean@xobs.io>
|
||||
// SPDX-FileCopyrightText: 2023 Foundation Devices, Inc <hello@foundationdevices.com>
|
||||
// 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::{
|
||||
println, BootConfig, ProgramDescription,
|
||||
XousPid, PAGE_SIZE, STACK_PAGE_COUNT, VDBG,
|
||||
};
|
||||
pub use armv7::structures::paging::{
|
||||
PageTable as L2PageTable, TranslationTable, TranslationTableDescriptor,
|
||||
TranslationTableType, PageTableDescriptor, PAGE_TABLE_SIZE, PageTableType,
|
||||
TranslationTableMemory, PageTableMemory,
|
||||
PAGE_TABLE_FLAGS, SMALL_PAGE_FLAGS,
|
||||
};
|
||||
pub use armv7::{PhysicalAddress, VirtualAddress};
|
||||
|
||||
#[repr(C)]
|
||||
#[derive(Debug, Copy, Clone)]
|
||||
/// **Note**: this struct must be in sync with the loader version.
|
||||
pub struct InitialProcess {
|
||||
/// Level-1 translation table base address of the process
|
||||
pub ttbr0: usize,
|
||||
|
||||
/// Address Space ID (PID) of the process.
|
||||
pub asid: u8,
|
||||
|
||||
/// Where execution begins
|
||||
pub entrypoint: usize,
|
||||
|
||||
/// Address of the top of the stack
|
||||
pub sp: usize,
|
||||
}
|
||||
|
||||
impl ProgramDescription {
|
||||
/// Map this ProgramDescription into RAM.
|
||||
/// The program may already have been relocated, and so may be
|
||||
/// 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.
|
||||
pub fn load(&self, allocator: &mut BootConfig, load_offset: usize, pid: XousPid)
|
||||
-> (usize, usize, usize) {
|
||||
assert_ne!(pid, 0, "PID must not be 0");
|
||||
|
||||
println!("Mapping PID {} into offset {:08x}", pid, load_offset);
|
||||
let pid_idx = (pid - 1) as usize;
|
||||
let is_kernel = pid == 1;
|
||||
let flag_defaults = FLG_R | FLG_W | FLG_VALID | if is_kernel { 0 } else { FLG_U };
|
||||
let stack_addr = if is_kernel { KERNEL_STACK_TOP } else { USER_STACK_TOP } - 16;
|
||||
if is_kernel {
|
||||
println!(
|
||||
"self.text_offset: {:08x}, KERNEL_LOAD_OFFSET: {:08x}",
|
||||
self.text_offset, KERNEL_LOAD_OFFSET
|
||||
);
|
||||
assert_eq!(self.text_offset as usize, KERNEL_LOAD_OFFSET);
|
||||
assert!(((self.text_offset + self.text_size) as usize) < EXCEPTION_STACK_TOP);
|
||||
assert!(
|
||||
((self.data_offset + self.data_size + self.bss_size) as usize)
|
||||
< EXCEPTION_STACK_TOP - 16
|
||||
);
|
||||
assert!(self.data_offset as usize >= KERNEL_LOAD_OFFSET);
|
||||
} else {
|
||||
assert!(((self.text_offset + self.text_size) as usize) < USER_AREA_END);
|
||||
assert!(((self.data_offset + self.data_size) as usize) < USER_AREA_END);
|
||||
}
|
||||
|
||||
// Translation table address must be zero
|
||||
if allocator.processes[pid_idx].ttbr0 != 0 {
|
||||
panic!("tried to re-use a process id {}", pid);
|
||||
}
|
||||
|
||||
// Allocate physical pages for L1 translation table
|
||||
let tt_address = allocator.alloc_l1_page_table(pid) as usize;
|
||||
if VDBG {
|
||||
println!("Setting {:08x} as translation table address for PID {}", tt_address, pid);
|
||||
}
|
||||
|
||||
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 kernel address space
|
||||
for offset in 0..4 {
|
||||
let offset = offset * PAGE_SIZE;
|
||||
println!("Map L1 pages: {:08x} -> {:08x}", tt_address + offset, PAGE_TABLE_ROOT_OFFSET + offset);
|
||||
allocator.map_page(
|
||||
translation_table,
|
||||
tt_address + offset,
|
||||
PAGE_TABLE_ROOT_OFFSET + offset,
|
||||
FLG_R | FLG_W | FLG_VALID,
|
||||
);
|
||||
allocator.change_owner(pid as XousPid, tt_address);
|
||||
}
|
||||
|
||||
// Allocate context for this process
|
||||
let thread_address = allocator.alloc() as usize;
|
||||
if VDBG {
|
||||
println!("PID {} thread: 0x{:08x}", pid, thread_address);
|
||||
}
|
||||
|
||||
allocator.map_page(
|
||||
translation_table,
|
||||
thread_address,
|
||||
CONTEXT_OFFSET,
|
||||
FLG_R | FLG_W | FLG_VALID,
|
||||
);
|
||||
allocator.change_owner(pid as XousPid, thread_address);
|
||||
|
||||
// Allocate stack pages.
|
||||
let total_stack_pages = if is_kernel {
|
||||
KERNEL_STACK_PAGE_COUNT
|
||||
} else {
|
||||
STACK_PAGE_COUNT
|
||||
};
|
||||
|
||||
if VDBG {
|
||||
println!("Mapping {} stack pages for PID {}", total_stack_pages, pid);
|
||||
}
|
||||
|
||||
let mut exception_sp = 0;
|
||||
for i in 0..total_stack_pages {
|
||||
if i == 0 {
|
||||
let sp_page = allocator.alloc() as usize;
|
||||
println!("Allocated stack page: {:08x}", sp_page);
|
||||
|
||||
allocator.map_page(
|
||||
translation_table,
|
||||
sp_page,
|
||||
(stack_addr - PAGE_SIZE * i) & !(PAGE_SIZE - 1),
|
||||
flag_defaults,
|
||||
);
|
||||
allocator.change_owner(pid as XousPid, sp_page);
|
||||
} else {
|
||||
// Reserve every page other than the 1st stack page
|
||||
allocator.map_page(
|
||||
translation_table,
|
||||
0,
|
||||
(stack_addr - PAGE_SIZE * i) & !(PAGE_SIZE - 1),
|
||||
flag_defaults & !FLG_VALID,
|
||||
);
|
||||
}
|
||||
|
||||
// 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 exception_sp == 0 {
|
||||
exception_sp = sp_page;
|
||||
}
|
||||
println!("Allocated exception stack page: {:08x}", sp_page);
|
||||
allocator.map_page(
|
||||
translation_table,
|
||||
sp_page,
|
||||
(EXCEPTION_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 {
|
||||
assert_eq!((self.load_offset as usize & (PAGE_SIZE - 1)), 0);
|
||||
}
|
||||
|
||||
// Map the process text section into RAM.
|
||||
// Either this is on SPI flash at an aligned address, or it
|
||||
// has been copied into RAM already. This is why we ignore `self.load_offset`
|
||||
// and use the `load_offset` parameter instead.
|
||||
let rounded_data_bss =
|
||||
((self.data_size + self.bss_size) as usize + PAGE_SIZE - 1) & !(PAGE_SIZE - 1);
|
||||
|
||||
// let load_size_rounded = (self.text_size as usize + PAGE_SIZE - 1) & !(PAGE_SIZE - 1);
|
||||
let text_phys_offset = load_offset + rounded_data_bss;
|
||||
for offset in (0..self.text_size as usize).step_by(PAGE_SIZE) {
|
||||
if VDBG {
|
||||
println!(
|
||||
" TEXT: Mapping {:08x} -> {:08x}",
|
||||
load_offset + offset + rounded_data_bss,
|
||||
self.text_offset as usize + offset
|
||||
);
|
||||
}
|
||||
allocator.map_page(
|
||||
translation_table,
|
||||
load_offset + offset + rounded_data_bss,
|
||||
self.text_offset as usize + offset,
|
||||
flag_defaults | FLG_X | FLG_VALID,
|
||||
);
|
||||
allocator.change_owner(pid as XousPid, load_offset + offset + rounded_data_bss);
|
||||
}
|
||||
|
||||
// Map the process data section into RAM.
|
||||
let data_phys_offset = load_offset;
|
||||
for offset in (0..(self.data_size + self.bss_size) as usize).step_by(PAGE_SIZE) {
|
||||
// let page_addr = allocator.alloc();
|
||||
if VDBG {
|
||||
println!(
|
||||
" DATA: Mapping {:08x} -> {:08x}",
|
||||
load_offset + offset,
|
||||
self.data_offset as usize + offset
|
||||
);
|
||||
}
|
||||
allocator.map_page(
|
||||
translation_table,
|
||||
load_offset + offset,
|
||||
self.data_offset as usize + offset,
|
||||
flag_defaults,
|
||||
);
|
||||
allocator.change_owner(pid as XousPid, load_offset + offset);
|
||||
}
|
||||
|
||||
allocator.processes[pid_idx].entrypoint = self.entrypoint as usize;
|
||||
allocator.processes[pid_idx].sp = stack_addr;
|
||||
allocator.processes[pid_idx].asid = pid;
|
||||
|
||||
(text_phys_offset, data_phys_offset, exception_sp)
|
||||
}
|
||||
}
|
||||
8
loader/src/platform/atsama5d27/memtest.rs
Normal file
8
loader/src/platform/atsama5d27/memtest.rs
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
// SPDX-FileCopyrightText: 2022 Sean Cross <sean@xobs.io>
|
||||
// SPDX-FileCopyrightText: 2023 Foundation Devices, Inc <hello@foundationdevices.com>
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
#[allow(dead_code)]
|
||||
pub fn memtest() {
|
||||
todo!()
|
||||
}
|
||||
16
loader/src/platform/atsama5d27/panic.rs
Normal file
16
loader/src/platform/atsama5d27/panic.rs
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
// SPDX-FileCopyrightText: 2022 Sean Cross <sean@xobs.io>
|
||||
// SPDX-FileCopyrightText: 2023 Foundation Devices, Inc <hello@foundationdevices.com>
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
use core::panic::PanicInfo;
|
||||
use armv7;
|
||||
|
||||
#[cfg(feature = "atsama5d27")]
|
||||
#[panic_handler]
|
||||
fn handle_panic(_arg: &PanicInfo) -> ! {
|
||||
crate::println!("{}", _arg);
|
||||
|
||||
armv7::asm::bkpt(); // Invoke a debugger breakpoint
|
||||
|
||||
loop {}
|
||||
}
|
||||
9
loader/src/platform/atsama5d27/secboot.rs
Normal file
9
loader/src/platform/atsama5d27/secboot.rs
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
// SPDX-FileCopyrightText: 2022 Sean Cross <sean@xobs.io>
|
||||
// SPDX-FileCopyrightText: 2023 Foundation Devices, Inc <hello@foundationdevices.com>
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
#[allow(dead_code)]
|
||||
/// returns true if the kernel is valid
|
||||
pub fn validate_xous_img(_xous_img_offset: *const u32) -> bool {
|
||||
true // TODO
|
||||
}
|
||||
Loading…
Reference in a new issue