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;
|
|
|
|
|
|
2020-06-03 07:34:55 +00:00
|
|
|
#[cfg(baremetal)]
|
2020-03-10 04:04:17 +00:00
|
|
|
#[macro_use]
|
|
|
|
|
mod debug;
|
|
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
|
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-03-13 14:39:49 +00:00
|
|
|
use services::SystemServicesHandle;
|
2020-03-10 04:04:17 +00:00
|
|
|
use xous::*;
|
|
|
|
|
|
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) -> ! {
|
|
|
|
|
println!("PANIC in PID {}!", crate::arch::current_pid());
|
|
|
|
|
println!("Details: {:?}", _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
|
|
|
}
|
|
|
|
|
|
2020-06-03 07:34:55 +00:00
|
|
|
#[cfg(baremetal)]
|
2020-03-10 04:04:17 +00:00
|
|
|
#[no_mangle]
|
|
|
|
|
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();
|
2020-06-03 10:28:22 +00:00
|
|
|
// Everything needs memory, so the first thing we should do is initialize the memory manager.
|
2020-03-10 04:04:17 +00:00
|
|
|
{
|
2020-06-03 07:34:55 +00:00
|
|
|
use mem::MemoryManagerHandle;
|
2020-03-10 04:04:17 +00:00
|
|
|
let mut memory_manager = MemoryManagerHandle::get();
|
|
|
|
|
memory_manager
|
2020-06-03 10:28:22 +00:00
|
|
|
.init_from_memory(rpt_offset, &args)
|
2020-03-10 04:04:17 +00:00
|
|
|
.expect("couldn't initialize memory manager");
|
|
|
|
|
}
|
|
|
|
|
{
|
|
|
|
|
let mut system_services = SystemServicesHandle::get();
|
2020-06-03 10:28:22 +00:00
|
|
|
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.
|
|
|
|
|
arch::init();
|
|
|
|
|
}
|
|
|
|
|
|
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-06-03 07:34:55 +00:00
|
|
|
let current_pid = last_pid.unwrap_or_default() as usize;
|
2020-03-19 09:25:12 +00:00
|
|
|
|
|
|
|
|
let system_services = SystemServicesHandle::get();
|
|
|
|
|
for test_idx in current_pid..system_services.processes.len() {
|
|
|
|
|
if system_services.processes[test_idx].ppid == 1 {
|
2020-03-27 07:58:59 +00:00
|
|
|
// print!("PID {} is owned by PID1... ", test_idx + 1);
|
2020-03-19 09:25:12 +00:00
|
|
|
if system_services.processes[test_idx].runnable() {
|
2020-03-27 07:58:59 +00:00
|
|
|
// println!(" and is runnable");
|
2020-03-19 09:25:12 +00:00
|
|
|
return Some((test_idx + 1) as PID);
|
|
|
|
|
}
|
2020-03-27 07:58:59 +00:00
|
|
|
// println!(" and is NOT RUNNABLE");
|
2020-03-19 09:25:12 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
for test_idx in 0..current_pid {
|
|
|
|
|
if system_services.processes[test_idx].ppid == 1 {
|
2020-03-27 07:58:59 +00:00
|
|
|
// print!("PID {} is owned by PID1... ", test_idx + 1);
|
2020-03-19 09:25:12 +00:00
|
|
|
if system_services.processes[test_idx].runnable() {
|
2020-03-27 07:58:59 +00:00
|
|
|
// println!(" and is runnable");
|
2020-03-19 09:25:12 +00:00
|
|
|
return Some((test_idx + 1) as PID);
|
|
|
|
|
}
|
2020-03-27 07:58:59 +00:00
|
|
|
// println!(" and is NOT RUNNABLE");
|
2020-03-19 09:25:12 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
None
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-03 10:28:22 +00:00
|
|
|
#[no_mangle]
|
|
|
|
|
pub extern "C" fn kmain() {
|
2020-03-10 04:04:17 +00:00
|
|
|
// Either map memory using a syscall, or if we're debugging the syscall
|
|
|
|
|
// handler then directly map it.
|
2020-06-03 07:34:55 +00:00
|
|
|
#[cfg(all(feature = "debug-print", baremetal))]
|
2020-03-10 04:04:17 +00:00
|
|
|
{
|
2020-06-03 07:34:55 +00:00
|
|
|
use mem::MemoryManagerHandle;
|
2020-03-10 04:04:17 +00:00
|
|
|
let mut memory_manager = MemoryManagerHandle::get();
|
|
|
|
|
memory_manager
|
|
|
|
|
.map_range(
|
|
|
|
|
0xF0002000 as *mut usize,
|
|
|
|
|
((debug::SUPERVISOR_UART.base as u32) & !4095) as *mut usize,
|
|
|
|
|
4096,
|
2020-03-31 10:03:19 +00:00
|
|
|
1,
|
2020-03-10 04:04:17 +00:00
|
|
|
MemoryFlags::R | MemoryFlags::W,
|
2020-03-31 10:03:19 +00:00
|
|
|
MemoryType::Default,
|
2020-03-10 04:04:17 +00:00
|
|
|
)
|
|
|
|
|
.expect("unable to map serial port");
|
|
|
|
|
println!("KMAIN: Supervisor mode started...");
|
|
|
|
|
debug::SUPERVISOR_UART.enable_rx();
|
|
|
|
|
println!("Claiming IRQ 3 via syscall...");
|
2020-03-31 10:03:19 +00:00
|
|
|
xous::claim_interrupt(3, debug::irq, 0 as *mut usize).expect("Couldn't claim interrupt 3");
|
2020-03-10 04:04:17 +00:00
|
|
|
print!("}} ");
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-03 07:34:55 +00:00
|
|
|
#[cfg(all(feature = "debug-print", baremetal))]
|
2020-03-10 04:04:17 +00:00
|
|
|
{
|
|
|
|
|
let args = args::KernelArguments::get();
|
|
|
|
|
println!("Kernel arguments:");
|
|
|
|
|
for arg in args.iter() {
|
|
|
|
|
println!(" {}", arg);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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-03-10 04:04:17 +00:00
|
|
|
loop {
|
2020-06-03 07:34:55 +00:00
|
|
|
// print!("Determining next PID to run...");
|
2020-03-19 09:25:12 +00:00
|
|
|
arch::irq::disable_all_irqs();
|
|
|
|
|
pid = next_pid_to_run(pid);
|
|
|
|
|
arch::irq::enable_all_irqs();
|
2020-06-03 07:34:55 +00:00
|
|
|
// println!(" {:?}", 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-03-27 07:58:59 +00:00
|
|
|
// println!("Attempting to switch to PID {}", pid);
|
2020-03-25 06:07:46 +00:00
|
|
|
xous::rsyscall(xous::SysCall::SwitchTo(pid, 0)).expect("couldn't switch to pid");
|
2020-03-10 04:04:17 +00:00
|
|
|
()
|
|
|
|
|
}
|
|
|
|
|
None => {
|
|
|
|
|
println!("No runnable tasks found. Zzz...");
|
2020-06-03 07:34:55 +00:00
|
|
|
arch::idle();
|
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.
|
|
|
|
|
#[cfg(not(baremetal))]
|
|
|
|
|
fn main() {
|
|
|
|
|
kmain();
|
|
|
|
|
}
|