Newer
Older
//! The Redox OS Kernel is a hybrid kernel that supports X86_64 systems and
//! provides Unix-like syscalls for primarily Rust applications
#![feature(drop_types_in_const)]
#![feature(integer_atomics)]
use arch::interrupt;
/// Architecture specific items (test)
#[cfg(test)]
/// Architecture specific items (ARM)
#[cfg(all(not(test), target_arch = "arm"))]
#[macro_use]
extern crate arch_arm as arch;
/// Architecture specific items (x86_64)
#[cfg(all(not(test), target_arch = "x86_64"))]
#[macro_use]
extern crate arch_x86_64 as arch;
extern crate alloc;
#[macro_use]
extern crate collections;

Jeremy Soller
committed
use core::sync::atomic::{AtomicUsize, ATOMIC_USIZE_INIT, Ordering};
use scheme::FileHandle;

Jeremy Soller
committed
/// Shared data structures
pub mod common;

Jeremy Soller
committed
/// Context management
pub mod context;
/// ELF file parsing
pub mod elf;
/// A unique number that identifies the current CPU - used for scheduling

Jeremy Soller
committed
#[thread_local]
static CPU_ID: AtomicUsize = ATOMIC_USIZE_INIT;

Jeremy Soller
committed
#[inline(always)]
pub fn cpu_id() -> usize {
CPU_ID.load(Ordering::Relaxed)
}
/// The count of all CPUs that can have work scheduled

Jeremy Soller
committed
static CPU_COUNT : AtomicUsize = ATOMIC_USIZE_INIT;

Jeremy Soller
committed
#[inline(always)]
pub fn cpu_count() -> usize {
CPU_COUNT.load(Ordering::Relaxed)
}
/// Initialize userspace by running the initfs:bin/init process
/// This function will also set the CWD to initfs:bin and open debug: as stdio
assert_eq!(syscall::chdir(b"initfs:bin"), Ok(0));
assert_eq!(syscall::open(b"debug:", syscall::flag::O_RDONLY).map(FileHandle::into), Ok(0));
assert_eq!(syscall::open(b"debug:", syscall::flag::O_WRONLY).map(FileHandle::into), Ok(1));
assert_eq!(syscall::open(b"debug:", syscall::flag::O_WRONLY).map(FileHandle::into), Ok(2));

Jeremy Soller
committed
syscall::exec(b"initfs:bin/init", &[]).expect("failed to execute initfs:init");
/// Allow exception handlers to send signal to arch-independant kernel
#[no_mangle]
pub extern fn ksignal(signal: usize) {
println!("SIGNAL {}, CPU {}, PID {:?}", signal, cpu_id(), context::context_id());
{
let contexts = context::contexts();
if let Some(context_lock) = contexts.current() {
let context = context_lock.read();
println!("NAME {}", unsafe { ::core::str::from_utf8_unchecked(&context.name.lock()) });
}
}
/// This is the kernel entry point for the primary CPU. The arch crate is responsible for calling this

Jeremy Soller
committed
pub extern fn kmain(cpus: usize) {

Jeremy Soller
committed
CPU_ID.store(0, Ordering::SeqCst);

Jeremy Soller
committed
CPU_COUNT.store(cpus, Ordering::SeqCst);

Jeremy Soller
committed

Jeremy Soller
committed
println!("BSP: {:?} {}", pid, cpus);
match context::contexts_mut().spawn(userspace_init) {
Ok(context_lock) => {
let mut context = context_lock.write();
context.status = context::Status::Runnable;
},
Err(err) => {
panic!("failed to spawn userspace_init: {:?}", err);
}
}
interrupt::disable();
if context::switch() {
interrupt::enable_and_nop();
} else {
// Enable interrupts, then halt CPU (to save power) until the next interrupt is actually fired.
}
}
/// This is the main kernel entry point for secondary CPUs
#[no_mangle]
pub extern fn kmain_ap(id: usize) {

Jeremy Soller
committed
CPU_ID.store(id, Ordering::SeqCst);
let pid = syscall::getpid();
println!("AP {}: {:?}", id, pid);
// Disable APs for now
loop {
unsafe { interrupt::enable_and_halt(); }
}
loop {
unsafe {
interrupt::disable();
if context::switch() {
interrupt::enable_and_nop();
} else {
// Enable interrupts, then halt CPU (to save power) until the next interrupt is actually fired.
interrupt::enable_and_halt();
}
}