2021-01-25 13:54:00 +00:00
|
|
|
// SPDX-FileCopyrightText: 2020 Sean Cross <sean@xobs.io>
|
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
|
2020-06-03 07:34:55 +00:00
|
|
|
#![cfg_attr(baremetal, no_main)]
|
|
|
|
|
#![cfg_attr(baremetal, no_std)]
|
2020-03-10 04:04:17 +00:00
|
|
|
|
2020-06-03 07:34:55 +00:00
|
|
|
#[cfg(baremetal)]
|
2020-03-10 04:04:17 +00:00
|
|
|
#[macro_use]
|
|
|
|
|
extern crate bitflags;
|
|
|
|
|
|
|
|
|
|
#[macro_use]
|
|
|
|
|
mod debug;
|
|
|
|
|
|
2020-08-07 10:57:55 +00:00
|
|
|
#[cfg(all(test, not(baremetal)))]
|
2020-03-10 04:04:17 +00:00
|
|
|
mod test;
|
|
|
|
|
|
|
|
|
|
mod arch;
|
|
|
|
|
|
|
|
|
|
#[macro_use]
|
|
|
|
|
mod args;
|
|
|
|
|
mod irq;
|
2020-03-24 06:13:11 +00:00
|
|
|
mod macros;
|
2020-03-10 04:04:17 +00:00
|
|
|
mod mem;
|
2020-03-25 06:07:46 +00:00
|
|
|
mod server;
|
2020-03-13 14:39:49 +00:00
|
|
|
mod services;
|
2020-03-10 04:04:17 +00:00
|
|
|
mod syscall;
|
|
|
|
|
|
2020-06-25 09:41:17 +00:00
|
|
|
use services::SystemServices;
|
2020-08-17 10:37:40 +00:00
|
|
|
use xous_kernel::*;
|
2020-10-13 14:54:12 +00:00
|
|
|
|
2020-06-03 07:34:55 +00:00
|
|
|
#[cfg(baremetal)]
|
2020-03-10 04:04:17 +00:00
|
|
|
use core::panic::PanicInfo;
|
2020-06-03 07:34:55 +00:00
|
|
|
#[cfg(baremetal)]
|
2020-03-10 04:04:17 +00:00
|
|
|
#[panic_handler]
|
|
|
|
|
fn handle_panic(_arg: &PanicInfo) -> ! {
|
2020-08-09 16:35:37 +00:00
|
|
|
println!("PANIC in PID {}: {}", crate::arch::current_pid(), _arg);
|
2020-04-01 05:21:23 +00:00
|
|
|
loop {
|
2020-06-03 07:34:55 +00:00
|
|
|
arch::idle();
|
2020-04-01 05:21:23 +00:00
|
|
|
}
|
2020-03-10 04:04:17 +00:00
|
|
|
}
|
|
|
|
|
|
2021-04-29 17:55:38 +00:00
|
|
|
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,
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-03 07:34:55 +00:00
|
|
|
#[cfg(baremetal)]
|
2020-03-10 04:04:17 +00:00
|
|
|
#[no_mangle]
|
2020-06-15 10:13:10 +00:00
|
|
|
/// 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.
|
2021-04-29 17:55:38 +00:00
|
|
|
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)
|
|
|
|
|
});
|
|
|
|
|
}
|
2020-03-10 04:04:17 +00:00
|
|
|
|
|
|
|
|
// Now that the memory manager is set up, perform any arch-specific initializations.
|
2021-04-29 17:55:38 +00:00
|
|
|
// each sub-call will handle things differently depending on the resume state
|
|
|
|
|
arch::init(resume);
|
2020-03-10 04:04:17 +00:00
|
|
|
|
2020-06-15 10:13:10 +00:00
|
|
|
// Either map memory using a syscall, or if we're debugging the syscall
|
|
|
|
|
// handler then directly map it.
|
2020-08-29 16:34:10 +00:00
|
|
|
#[cfg(any(feature = "debug-print", feature = "print-panics"))]
|
2020-06-15 10:13:10 +00:00
|
|
|
{
|
2020-11-23 09:24:28 +00:00
|
|
|
use utralib::generated::*;
|
2021-04-29 17:55:38 +00:00
|
|
|
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
|
2020-08-09 15:08:54 +00:00
|
|
|
mem::MemoryManager::with_mut(|memory_manager| {
|
|
|
|
|
memory_manager
|
|
|
|
|
.map_range(
|
2021-04-29 17:55:38 +00:00
|
|
|
utra::resumekicker::HW_RESUMEKICKER_BASE as *mut u8,
|
|
|
|
|
((RESUME_KICKER.base as u32) & !4095) as *mut u8,
|
2020-08-09 15:08:54 +00:00
|
|
|
4096,
|
|
|
|
|
PID::new(1).unwrap(),
|
|
|
|
|
MemoryFlags::R | MemoryFlags::W,
|
|
|
|
|
MemoryType::Default,
|
|
|
|
|
)
|
2021-04-29 17:55:38 +00:00
|
|
|
.expect("unable to map resume kicker for future resume use")
|
2020-08-09 15:08:54 +00:00
|
|
|
});
|
2020-08-09 11:21:54 +00:00
|
|
|
}
|
2020-12-14 09:57:29 +00:00
|
|
|
|
2021-04-29 17:55:38 +00:00
|
|
|
// 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 {
|
2021-04-30 11:29:36 +00:00
|
|
|
println!("Resume: enabling IRQs and kicking into the resume interrupt context");
|
2021-04-29 17:55:38 +00:00
|
|
|
// 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!
|
2020-12-14 09:57:29 +00:00
|
|
|
}
|
2020-08-09 11:21:54 +00:00
|
|
|
}
|
2020-06-15 10:13:10 +00:00
|
|
|
|
|
|
|
|
/// Loop through the SystemServices list to determine the next PID to be run.
|
|
|
|
|
/// If no process is ready, return `None`.
|
2020-03-19 09:25:12 +00:00
|
|
|
fn next_pid_to_run(last_pid: Option<PID>) -> Option<PID> {
|
|
|
|
|
// PIDs are 1-indexed but arrays are 0-indexed. By not subtracting
|
|
|
|
|
// 1 from the PID when we use it as an array index, we automatically
|
|
|
|
|
// pick the next process in the list.
|
2020-08-09 11:21:54 +00:00
|
|
|
let current_pid = last_pid.unwrap_or(unsafe { PID::new_unchecked(1) }).get() as usize;
|
2020-03-19 09:25:12 +00:00
|
|
|
|
2020-06-25 09:41:17 +00:00
|
|
|
SystemServices::with(|system_services| {
|
|
|
|
|
for test_idx in current_pid..system_services.processes.len() {
|
|
|
|
|
if system_services.processes[test_idx].ppid.get() == 1 {
|
|
|
|
|
// print!("PID {} is owned by PID1... ", test_idx + 1);
|
|
|
|
|
if system_services.processes[test_idx].runnable() {
|
|
|
|
|
// println!(" and is runnable");
|
|
|
|
|
return match pid_from_usize(test_idx + 1) {
|
|
|
|
|
Ok(x) => Some(x),
|
|
|
|
|
Err(_) => None,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
// println!(" and is NOT RUNNABLE");
|
2020-03-19 09:25:12 +00:00
|
|
|
}
|
|
|
|
|
}
|
2020-06-25 09:41:17 +00:00
|
|
|
for test_idx in 0..current_pid {
|
|
|
|
|
if system_services.processes[test_idx].ppid.get() == 1 {
|
|
|
|
|
// print!("PID {} is owned by PID1... ", test_idx + 1);
|
|
|
|
|
if system_services.processes[test_idx].runnable() {
|
|
|
|
|
// println!(" and is runnable");
|
|
|
|
|
return match pid_from_usize(test_idx + 1) {
|
|
|
|
|
Ok(x) => Some(x),
|
|
|
|
|
Err(_) => None,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
// println!(" and is NOT RUNNABLE");
|
2020-03-19 09:25:12 +00:00
|
|
|
}
|
|
|
|
|
}
|
2020-06-25 09:41:17 +00:00
|
|
|
None
|
|
|
|
|
})
|
2020-03-19 09:25:12 +00:00
|
|
|
}
|
|
|
|
|
|
2020-06-15 10:13:10 +00:00
|
|
|
/// Common main function for baremetal and hosted environments.
|
2020-06-03 10:28:22 +00:00
|
|
|
#[no_mangle]
|
2020-06-24 09:51:31 +00:00
|
|
|
pub extern "C" fn kmain() {
|
2020-06-03 07:34:55 +00:00
|
|
|
// Start performing round-robin on all child processes.
|
|
|
|
|
// Note that at this point, no new direct children of INIT may be created.
|
2020-03-19 09:25:12 +00:00
|
|
|
let mut pid = None;
|
2020-10-29 11:46:17 +00:00
|
|
|
|
|
|
|
|
#[cfg(not(target_os = "none"))]
|
|
|
|
|
{
|
|
|
|
|
use std::panic;
|
|
|
|
|
panic::set_hook(Box::new(|arg| {
|
|
|
|
|
println!("Panic Details: {:?}", arg);
|
|
|
|
|
debug_here::debug_here!();
|
|
|
|
|
}));
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-10 04:04:17 +00:00
|
|
|
loop {
|
2020-03-19 09:25:12 +00:00
|
|
|
pid = next_pid_to_run(pid);
|
2020-03-10 04:04:17 +00:00
|
|
|
|
2020-03-19 09:25:12 +00:00
|
|
|
match pid {
|
2020-03-10 04:04:17 +00:00
|
|
|
Some(pid) => {
|
2020-10-30 12:43:22 +00:00
|
|
|
// #[cfg(feature = "debug-print")]
|
2020-11-23 09:24:28 +00:00
|
|
|
// klog!("switching to pid {}", pid);
|
2021-04-30 11:29:36 +00:00
|
|
|
println!("switching to pid {}", pid);
|
2020-11-23 09:24:28 +00:00
|
|
|
xous_kernel::rsyscall(xous_kernel::SysCall::SwitchTo(pid, 0))
|
|
|
|
|
.expect("couldn't switch to pid");
|
2020-03-10 04:04:17 +00:00
|
|
|
}
|
|
|
|
|
None => {
|
2020-11-11 08:03:29 +00:00
|
|
|
#[cfg(feature = "debug-print")]
|
|
|
|
|
println!("NO RUNNABLE TASKS FOUND, entering idle state");
|
2021-03-02 11:25:41 +00:00
|
|
|
|
|
|
|
|
#[cfg(feature = "debug-print")]
|
|
|
|
|
SystemServices::with(|system_services| {
|
2021-03-23 11:42:33 +00:00
|
|
|
for (test_idx, process) in system_services.processes.iter().enumerate() {
|
|
|
|
|
if ! process.free() {
|
|
|
|
|
println!("PID {}: {:?}", test_idx + 1, process);
|
|
|
|
|
}
|
2021-03-02 11:25:41 +00:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2020-06-23 05:58:15 +00:00
|
|
|
// Special case for testing: idle can return `false` to indicate exit
|
2020-08-09 11:21:54 +00:00
|
|
|
if !arch::idle() {
|
2020-06-23 05:58:15 +00:00
|
|
|
return;
|
|
|
|
|
}
|
2020-03-10 04:04:17 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-06-03 10:28:22 +00:00
|
|
|
|
|
|
|
|
/// The main entrypoint when run in hosted mode. When running in embedded mode,
|
|
|
|
|
/// this function does not exist.
|
2020-08-02 08:08:56 +00:00
|
|
|
#[cfg(all(not(baremetal)))]
|
2020-06-03 10:28:22 +00:00
|
|
|
fn main() {
|
2020-06-24 09:51:31 +00:00
|
|
|
kmain();
|
2020-06-03 10:28:22 +00:00
|
|
|
}
|