#![cfg_attr(baremetal, no_main)]
#![cfg_attr(baremetal, no_std)]
#[cfg(baremetal)]
#[macro_use]
extern crate bitflags;
#[macro_use]
mod debug;
#[cfg(all(test, not(baremetal)))]
mod test;
mod arch;
#[macro_use]
mod args;
mod io;
mod irq;
mod macros;
mod mem;
mod platform;
mod server;
mod services;
mod syscall;
use services::SystemServices;
use xous_kernel::*;
#[cfg(baremetal)]
use core::panic::PanicInfo;
#[cfg(baremetal)]
#[panic_handler]
fn handle_panic(_arg: &PanicInfo) -> ! {
println!("PANIC in PID {}: {}", crate::arch::current_pid(), _arg);
loop {
arch::idle();
}
}
#[cfg(baremetal)]
#[no_mangle]
pub unsafe extern "C" fn init(
arg_offset: *const u32,
init_offset: *const u32,
rpt_offset: *mut u32,
) {
args::KernelArguments::init(arg_offset);
let args = args::KernelArguments::get();
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)
});
arch::init();
platform::init();
println!("KMAIN (clean boot): Supervisor mode started...");
platform::rand::get_u32();
platform::rand::get_u32();
}
fn next_pid_to_run(last_pid: Option<PID>) -> Option<PID> {
let next_pid = last_pid.map(|v| v.get() as usize).unwrap_or(1);
SystemServices::with(|system_services| {
for process in system_services.processes[next_pid..]
.iter()
.chain(system_services.processes[..next_pid].iter())
{
if process.runnable() {
return Some(process.pid);
}
}
None
})
}
#[no_mangle]
pub extern "C" fn kmain() {
let mut pid = None;
#[cfg(not(any(target_os = "none", target_os = "xous", all(ci, test))))]
{
use std::panic;
panic::set_hook(Box::new(|arg| {
println!("Panic Details: {:?}", arg);
}));
}
loop {
pid = next_pid_to_run(pid);
match pid {
Some(pid) => {
xous_kernel::rsyscall(xous_kernel::SysCall::SwitchTo(pid, 0))
.expect("couldn't switch to pid");
}
None => {
#[cfg(feature = "debug-print")]
println!("NO RUNNABLE TASKS FOUND, entering idle state");
#[cfg(feature = "debug-print")]
SystemServices::with(|system_services| {
for (test_idx, process) in system_services.processes.iter().enumerate() {
if !process.free() {
println!("PID {}: {:?}", test_idx + 1, process);
}
}
});
if !arch::idle() {
return;
}
}
}
}
}
#[cfg(all(not(baremetal)))]
fn main() {
kmain();
}