mirror of
https://github.com/saymrwulf/xous-core.git
synced 2026-11-17 08:40:52 +00:00
clean up some boot process holdovers
This commit is contained in:
parent
640ddb48e8
commit
260b154741
19 changed files with 99 additions and 118 deletions
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
|
|
@ -17,12 +17,12 @@ pub fn current_pid() -> PID {
|
|||
PID::new(satp::read().asid() as _).unwrap()
|
||||
}
|
||||
|
||||
pub fn init(resume: bool) {
|
||||
pub fn init() {
|
||||
unsafe {
|
||||
sie::set_ssoft();
|
||||
sie::set_sext();
|
||||
}
|
||||
rand::init(resume);
|
||||
rand::init();
|
||||
}
|
||||
|
||||
/// Put the core to sleep until an interrupt hits. Returns `true`
|
||||
|
|
|
|||
|
|
@ -15,7 +15,7 @@ pub struct Trng {
|
|||
pub base: *mut usize,
|
||||
}
|
||||
|
||||
pub fn init(resume: bool) {
|
||||
pub fn init() {
|
||||
// Map the TRNG so that we can allocate names
|
||||
// hardware guarantees that:
|
||||
// - TRNG will automatically power on
|
||||
|
|
@ -23,20 +23,18 @@ pub fn init(resume: bool) {
|
|||
// - Kernel FIFO will fill with TRNGs such that at least the next 512 calls to get_u32() will succeed without delay
|
||||
// - The kernel will start a TRNG server
|
||||
// - All further security decisions and policies are 100% delegated to this new server.
|
||||
if !resume {
|
||||
MemoryManager::with_mut(|memory_manager| {
|
||||
memory_manager
|
||||
.map_range(
|
||||
utra::trng_kernel::HW_TRNG_KERNEL_BASE as *mut u8,
|
||||
((TRNG_KERNEL.base as u32) & !4095) as *mut u8,
|
||||
4096,
|
||||
PID::new(1).unwrap(),
|
||||
MemoryFlags::R | MemoryFlags::W,
|
||||
MemoryType::Default,
|
||||
)
|
||||
.expect("unable to map TRNG")
|
||||
});
|
||||
}
|
||||
MemoryManager::with_mut(|memory_manager| {
|
||||
memory_manager
|
||||
.map_range(
|
||||
utra::trng_kernel::HW_TRNG_KERNEL_BASE as *mut u8,
|
||||
((TRNG_KERNEL.base as u32) & !4095) as *mut u8,
|
||||
4096,
|
||||
PID::new(1).unwrap(),
|
||||
MemoryFlags::R | MemoryFlags::W,
|
||||
MemoryType::Default,
|
||||
)
|
||||
.expect("unable to map TRNG")
|
||||
});
|
||||
|
||||
let trng_kernel_csr = CSR::new(TRNG_KERNEL.base as *mut u32);
|
||||
while trng_kernel_csr.rf(utra::trng_kernel::STATUS_AVAIL) == 0 {}
|
||||
|
|
|
|||
|
|
@ -30,26 +30,38 @@ _start:
|
|||
|
||||
/////// resume prep code
|
||||
// if resume flag is false (0), skip this section
|
||||
beq a3, zero, coldboot
|
||||
beq a7, zero, coldboot
|
||||
|
||||
/*
|
||||
This is the resume code. The overall strategy is to have a suspend/resume cycle
|
||||
occur within an interrupt context. Thus, at this point of the suspend/resume
|
||||
cycle (e.g. the resume half), our strategy is to restore a minimal environment which
|
||||
allows a Xous interrupt context to run, which allows the Xous return-from-interrupt
|
||||
handler to set up the remainder of the resume tasks.
|
||||
*/
|
||||
// Restore a default stack pointer
|
||||
li sp, 0xfffefffc
|
||||
|
||||
// setup sie ssoft, sext: bits 1 and 9 0b10_0000_0010
|
||||
// setup sie [ssoft, sext]. This corresponds to bits 1 and 9, or 0b10_0000_0010
|
||||
li t0, 0x202
|
||||
csrw sie, t0
|
||||
|
||||
// restore the SIM with the previously socked away value
|
||||
// restore the SIM with the value previously socked away by the interrupt handler routine during the suspend half
|
||||
jal ra, _enable_all_irqs
|
||||
|
||||
// set scause to simulate an interrupt's occurence
|
||||
// set scause to simulate an interrupt's occurence -- this makes the resume "feel like" an interrupt
|
||||
// vectored into the same routine that caused the suspend, but without having gone through the typical
|
||||
// interrupt handler mechanisms
|
||||
li t0, 0x80000000
|
||||
csrw scause, t0
|
||||
|
||||
// Note that registers $a0-$a7 still contain the arguments
|
||||
// this is the same path the suspend interrupt took, but we set it up manually
|
||||
// and the loader had flipped the "resume" bit, so this should bring us into the resume branch.
|
||||
j _start_trap_rust
|
||||
|
||||
coldboot:
|
||||
// Note that registers $a0-$a7 still contain the kernel arguments
|
||||
|
||||
// Jump to a program to initialize global variables.
|
||||
// Note that at this point, the .data section should
|
||||
// have already been set up by the loader.
|
||||
|
|
|
|||
|
|
@ -39,112 +39,61 @@ fn handle_panic(_arg: &PanicInfo) -> ! {
|
|||
}
|
||||
}
|
||||
|
||||
use utralib::generated::*;
|
||||
pub const RESUME_KICKER: ResumeKicker = ResumeKicker {
|
||||
// the manually chosen virtual address has to be in the top 4MiB as it is the only page shared among all processes
|
||||
base: 0xffcd_0000 as *mut usize, // see https://github.com/betrusted-io/xous-core/blob/master/docs/memory.md
|
||||
};
|
||||
|
||||
pub struct ResumeKicker {
|
||||
pub base: *mut usize,
|
||||
}
|
||||
|
||||
#[cfg(baremetal)]
|
||||
#[no_mangle]
|
||||
/// This function is called from baremetal startup code to initialize various kernel structures
|
||||
/// based on arguments passed by the bootloader. It is unused when running under an operating system.
|
||||
pub extern "C" fn init(arg_offset: *const u32, init_offset: *const u32, rpt_offset: *mut u32, resume: bool) {
|
||||
if !resume {
|
||||
unsafe { args::KernelArguments::init(arg_offset) };
|
||||
let args = args::KernelArguments::get();
|
||||
// Everything needs memory, so the first thing we should do is initialize the memory manager.
|
||||
crate::mem::MemoryManager::with_mut(|mm| {
|
||||
mm.init_from_memory(rpt_offset, &args)
|
||||
.expect("couldn't initialize memory manager")
|
||||
});
|
||||
SystemServices::with_mut(|system_services| {
|
||||
system_services.init_from_memory(init_offset, &args)
|
||||
});
|
||||
}
|
||||
pub extern "C" fn init(arg_offset: *const u32, init_offset: *const u32, rpt_offset: *mut u32) {
|
||||
unsafe { args::KernelArguments::init(arg_offset) };
|
||||
let args = args::KernelArguments::get();
|
||||
// Everything needs memory, so the first thing we should do is initialize the memory manager.
|
||||
crate::mem::MemoryManager::with_mut(|mm| {
|
||||
mm.init_from_memory(rpt_offset, &args)
|
||||
.expect("couldn't initialize memory manager")
|
||||
});
|
||||
SystemServices::with_mut(|system_services| {
|
||||
system_services.init_from_memory(init_offset, &args)
|
||||
});
|
||||
|
||||
// Now that the memory manager is set up, perform any arch-specific initializations.
|
||||
// each sub-call will handle things differently depending on the resume state
|
||||
arch::init(resume);
|
||||
arch::init();
|
||||
|
||||
// Either map memory using a syscall, or if we're debugging the syscall
|
||||
// handler then directly map it.
|
||||
#[cfg(any(feature = "debug-print", feature = "print-panics"))]
|
||||
{
|
||||
use utralib::generated::*;
|
||||
if !resume {
|
||||
// Map the serial port so println!() works as expected.
|
||||
mem::MemoryManager::with_mut(|memory_manager| {
|
||||
memory_manager
|
||||
.map_range(
|
||||
utra::uart::HW_UART_BASE as *mut u8,
|
||||
((debug::SUPERVISOR_UART_ADDR as u32) & !4095) as *mut u8,
|
||||
4096,
|
||||
PID::new(1).unwrap(),
|
||||
MemoryFlags::R | MemoryFlags::W,
|
||||
MemoryType::Default,
|
||||
)
|
||||
.expect("unable to map serial port")
|
||||
});
|
||||
debug::Uart{}.init();
|
||||
println!("KMAIN (clean boot): Supervisor mode started...");
|
||||
println!("Claiming IRQ {} via syscall...", utra::uart::UART_IRQ);
|
||||
xous_kernel::claim_interrupt(utra::uart::UART_IRQ, debug::irq, 0 as *mut usize)
|
||||
.expect("Couldn't claim debug interrupt");
|
||||
print!("}} ");
|
||||
} else {
|
||||
println!("KMAIN (resume): Loader is instructing us to resume...");
|
||||
}
|
||||
|
||||
if !resume {
|
||||
// Print the processed kernel arguments
|
||||
let args = args::KernelArguments::get();
|
||||
println!("Kernel arguments:");
|
||||
for arg in args.iter() {
|
||||
println!(" {}", arg);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if !resume {
|
||||
// map in the resume kicker to its fixed virtual address for the next time we start with a resume
|
||||
// Map the serial port so println!() works as expected.
|
||||
mem::MemoryManager::with_mut(|memory_manager| {
|
||||
memory_manager
|
||||
.map_range(
|
||||
utra::resumekicker::HW_RESUMEKICKER_BASE as *mut u8,
|
||||
((RESUME_KICKER.base as u32) & !4095) as *mut u8,
|
||||
utra::uart::HW_UART_BASE as *mut u8,
|
||||
((debug::SUPERVISOR_UART_ADDR as u32) & !4095) as *mut u8,
|
||||
4096,
|
||||
PID::new(1).unwrap(),
|
||||
MemoryFlags::R | MemoryFlags::W,
|
||||
MemoryType::Default,
|
||||
)
|
||||
.expect("unable to map resume kicker for future resume use")
|
||||
.expect("unable to map serial port")
|
||||
});
|
||||
debug::Uart{}.init();
|
||||
println!("KMAIN (clean boot): Supervisor mode started...");
|
||||
println!("Claiming IRQ {} via syscall...", utra::uart::UART_IRQ);
|
||||
xous_kernel::claim_interrupt(utra::uart::UART_IRQ, debug::irq, 0 as *mut usize)
|
||||
.expect("Couldn't claim debug interrupt");
|
||||
print!("}} ");
|
||||
|
||||
// Print the processed kernel arguments
|
||||
let args = args::KernelArguments::get();
|
||||
println!("Kernel arguments:");
|
||||
for arg in args.iter() {
|
||||
println!(" {}", arg);
|
||||
}
|
||||
}
|
||||
|
||||
// rand::init() already clears the initial pipe, but pump the TRNG a little more out of no other reason than sheer paranoia
|
||||
arch::rand::get_u32();
|
||||
arch::rand::get_u32();
|
||||
|
||||
if resume {
|
||||
println!("Resume: enabling IRQs and kicking into the resume interrupt context");
|
||||
// this will reload the SIM from its backing store in RAM
|
||||
crate::arch::irq::enable_all_irqs();
|
||||
// we should already have our page mapped from a prior boot
|
||||
let mut resumekicker = CSR::new(RESUME_KICKER.base as *mut u32);
|
||||
|
||||
// this should trigger a pending interrupt immediately
|
||||
// note that the resumekicker's "kick" signal is a wire-OR into the susres SOFT_INT bit
|
||||
// and that the loader will have set EV_ENABLE on this before handing control to the kernel
|
||||
resumekicker.wfo(utra::resumekicker::KICKER_KICK, 1);
|
||||
// when the handler returns to this closing curly brace,
|
||||
// the control flow returns to the asm.S loader
|
||||
// and the very next immediate thing it does is jump to kmain() and schedule the next PID to run!
|
||||
}
|
||||
}
|
||||
|
||||
/// Loop through the SystemServices list to determine the next PID to be run.
|
||||
|
|
|
|||
|
|
@ -15,8 +15,8 @@ rand = "0.7.3"
|
|||
[features]
|
||||
debug-print = []
|
||||
earlyprintk = []
|
||||
default = ["debug-print"]
|
||||
#default = []
|
||||
#default = ["debug-print"]
|
||||
default = []
|
||||
|
||||
[profile.release]
|
||||
codegen-units = 1 # 1 better optimizations
|
||||
|
|
|
|||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
|
|
@ -50,8 +50,5 @@ start_kernel:
|
|||
// Reposition the stack at the offset passed by $a5
|
||||
move sp, a5
|
||||
|
||||
// copy the clean suspend marker to the fourth argument slot (a3)
|
||||
move a3, a7
|
||||
|
||||
// Issue the return, which will jump to $mepc in Supervisor mode
|
||||
mret
|
||||
|
|
|
|||
|
|
@ -1078,15 +1078,27 @@ fn boot_sequence(args: KernelArguments, _signature: u32) -> ! {
|
|||
};
|
||||
read_initial_config(&mut cfg);
|
||||
|
||||
// check to see if we are recovering from a clean suspend or not
|
||||
let (clean, was_forced_suspend) = check_resume(&mut cfg);
|
||||
|
||||
if !clean {
|
||||
// cold boot path
|
||||
println!("no suspend marker found, doing a cold boot!");
|
||||
phase_1(&mut cfg);
|
||||
phase_2(&mut cfg);
|
||||
println!("done initializing for cold boot.");
|
||||
} else {
|
||||
// resume path
|
||||
use utralib::generated::*;
|
||||
// setup for a resume
|
||||
// TRNG virtual memory mapping already set up, but the hardware needs
|
||||
// to have its pipeline flushed, as the first result is always 0.
|
||||
let trng_csr = CSR::new(utra::trng_kernel::HW_TRNG_KERNEL_BASE as *mut u32);
|
||||
for _ in 0..4 { // only one is strictly necessary but more is no problem
|
||||
while trng_csr.rf(utra::trng_kernel::STATUS_AVAIL) == 0 {}
|
||||
trng_csr.rf(utra::trng_kernel::DATA_DATA);
|
||||
}
|
||||
|
||||
// setup the `susres` register for a resume
|
||||
let mut resume_csr = CSR::new(utra::susres::HW_SUSRES_BASE as *mut u32);
|
||||
// set the resume marker for the SUSRES server, noting the forced suspend status
|
||||
if was_forced_suspend {
|
||||
|
|
@ -1103,7 +1115,6 @@ fn boot_sequence(args: KernelArguments, _signature: u32) -> ! {
|
|||
|
||||
// trigger the interrupt; it's not immediately handled, but rather checked later on by the kernel on clean resume
|
||||
resume_csr.wfo(utra::susres::INTERRUPT_INTERRUPT, 1);
|
||||
// SATP restore is handled by the `start_kernel` assembly pre-amble
|
||||
}
|
||||
|
||||
println!("Font maps located as follows:");
|
||||
|
|
@ -1130,6 +1141,14 @@ fn boot_sequence(args: KernelArguments, _signature: u32) -> ! {
|
|||
arg_offset, ip_offset, rpt_offset,
|
||||
);
|
||||
|
||||
// save a copy of the computed kernel registers at the bottom of the page reserved
|
||||
// for the bootloader stack. Note there is no stack-smash protection for these arguments,
|
||||
// so we're definitely vulnerable to e.g. buffer overrun attacks in the early bootloader.
|
||||
//
|
||||
// Probably the right long-term solution to this is to turn all the bootloader "loading"
|
||||
// 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.
|
||||
unsafe {
|
||||
let backup_args: *mut [u32; 7] = 0x40FF_F000 as *mut[u32; 7];
|
||||
(*backup_args)[0] = arg_offset as u32;
|
||||
|
|
@ -1139,9 +1158,12 @@ fn boot_sequence(args: KernelArguments, _signature: u32) -> ! {
|
|||
(*backup_args)[4] = cfg.processes[0].entrypoint as u32;
|
||||
(*backup_args)[5] = cfg.processes[0].sp as u32;
|
||||
(*backup_args)[6] = if cfg.debug {1} else {0};
|
||||
println!("Backup kernel args:");
|
||||
for i in 0..7 {
|
||||
println!("0x{:08x}", (*backup_args)[i]);
|
||||
#[cfg(feature="debug-print")]
|
||||
{
|
||||
println!("Backup kernel args:");
|
||||
for i in 0..7 {
|
||||
println!("0x{:08x}", (*backup_args)[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
unsafe {
|
||||
|
|
@ -1159,13 +1181,16 @@ fn boot_sequence(args: KernelArguments, _signature: u32) -> ! {
|
|||
} else {
|
||||
unsafe {
|
||||
let backup_args: *mut [u32; 7] = 0x40FF_F000 as *mut[u32; 7];
|
||||
println!("Using backed up kernel args:");
|
||||
for i in 0..7 {
|
||||
println!("0x{:08x}", (*backup_args)[i]);
|
||||
#[cfg(feature="debug-print")]
|
||||
{
|
||||
println!("Using backed up kernel args:");
|
||||
for i in 0..7 {
|
||||
println!("0x{:08x}", (*backup_args)[i]);
|
||||
}
|
||||
}
|
||||
let satp = ((*backup_args)[3] as usize) & 0x803F_FFFF | (10 << 22);
|
||||
//let satp = (*backup_args)[3];
|
||||
println!("Adjusting SATP was: 0x{:08x} now: 0x{:08x}", (*backup_args)[3], satp);
|
||||
println!("Adjusting SATP to the sures process. Was: 0x{:08x} now: 0x{:08x}", (*backup_args)[3], satp);
|
||||
|
||||
if true {
|
||||
use utralib::generated::*;
|
||||
|
|
|
|||
|
|
@ -22,4 +22,4 @@ utralib = { path = "../../utralib"}
|
|||
|
||||
[features]
|
||||
debugprint = []
|
||||
default = ["debugprint"]
|
||||
default = []
|
||||
|
|
|
|||
|
|
@ -72,7 +72,7 @@ mod implementation {
|
|||
|
||||
if sr.csr.rf(utra::susres::STATE_RESUME) == 0 {
|
||||
println!("going into suspend");
|
||||
if false { // this is just for testing
|
||||
if true { // this is just for testing
|
||||
let mut reboot_csr = CSR::new(REBOOT_CSR.load(Ordering::Relaxed) as *mut u32);
|
||||
reboot_csr.wfo(utra::reboot::SOC_RESET_SOC_RESET, 0xAC);
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue