2016-08-14 00:34:27 +02:00
|
|
|
//! # The Redox OS Kernel, version 2
|
|
|
|
//!
|
2016-11-04 20:38:40 +01:00
|
|
|
//! The Redox OS Kernel is a hybrid kernel that supports X86_64 systems and
|
2016-08-14 00:34:27 +02:00
|
|
|
//! provides Unix-like syscalls for primarily Rust applications
|
2016-08-14 02:21:46 +02:00
|
|
|
|
2016-12-14 04:41:43 +01:00
|
|
|
#![deny(warnings)]
|
2016-08-15 06:05:32 +02:00
|
|
|
#![feature(alloc)]
|
2016-08-19 03:44:31 +02:00
|
|
|
#![feature(asm)]
|
2016-08-15 06:05:32 +02:00
|
|
|
#![feature(collections)]
|
2016-08-15 02:16:56 +02:00
|
|
|
#![feature(const_fn)]
|
Orbital (#16)
* Port previous ethernet scheme
* Add ipd
* Fix initfs rebuilds, use QEMU user networking addresses in ipd
* Add tcp/udp, netutils, dns, and network config
* Add fsync to network driver
* Add dns, router, subnet by default
* Fix e1000 driver. Make ethernet and IP non-blocking to avoid deadlocks
* Add orbital server, WIP
* Add futex
* Add orbutils and orbital
* Update libstd, orbutils, and orbital
Move ANSI key encoding to vesad
* Add orbital assets
* Update orbital
* Update to add login manager
* Add blocking primitives, block for most things except waitpid, update orbital
* Wait in waitpid and IRQ, improvements for other waits
* Fevent in root scheme
* WIP: Switch to using fevent
* Reorganize
* Event based e1000d driver
* Superuser-only access to some network schemes, display, and disk
* Superuser root and irq schemes
* Fix orbital
2016-10-14 01:21:42 +02:00
|
|
|
#![feature(core_intrinsics)]
|
2016-08-18 16:10:08 +02:00
|
|
|
#![feature(drop_types_in_const)]
|
2016-09-23 00:14:45 +02:00
|
|
|
#![feature(heap_api)]
|
2016-10-05 22:24:08 +02:00
|
|
|
#![feature(integer_atomics)]
|
2016-09-12 05:04:34 +02:00
|
|
|
#![feature(never_type)]
|
2016-08-20 21:43:35 +02:00
|
|
|
#![feature(thread_local)]
|
2016-08-14 02:21:46 +02:00
|
|
|
#![no_std]
|
|
|
|
|
2016-08-18 03:34:33 +02:00
|
|
|
use arch::interrupt;
|
2016-08-14 17:31:35 +02:00
|
|
|
|
2016-08-14 22:59:18 +02:00
|
|
|
/// Architecture specific items (test)
|
|
|
|
#[cfg(test)]
|
2016-08-14 19:45:47 +02:00
|
|
|
#[macro_use]
|
2016-08-14 22:59:18 +02:00
|
|
|
extern crate arch_test as arch;
|
|
|
|
|
2016-08-26 01:03:01 +02:00
|
|
|
/// Architecture specific items (ARM)
|
|
|
|
#[cfg(all(not(test), target_arch = "arm"))]
|
|
|
|
#[macro_use]
|
|
|
|
extern crate arch_arm as arch;
|
|
|
|
|
2016-08-14 22:59:18 +02:00
|
|
|
/// Architecture specific items (x86_64)
|
|
|
|
#[cfg(all(not(test), target_arch = "x86_64"))]
|
|
|
|
#[macro_use]
|
|
|
|
extern crate arch_x86_64 as arch;
|
2016-08-14 02:21:46 +02:00
|
|
|
|
2016-08-15 06:05:32 +02:00
|
|
|
extern crate alloc;
|
|
|
|
#[macro_use]
|
|
|
|
extern crate collections;
|
|
|
|
|
2016-08-16 19:04:14 +02:00
|
|
|
#[macro_use]
|
|
|
|
extern crate bitflags;
|
2016-08-19 15:57:24 +02:00
|
|
|
extern crate goblin;
|
2016-08-16 19:04:14 +02:00
|
|
|
extern crate spin;
|
|
|
|
|
2016-09-15 16:35:07 +02:00
|
|
|
use core::sync::atomic::{AtomicUsize, ATOMIC_USIZE_INIT, Ordering};
|
2016-11-13 21:47:04 +01:00
|
|
|
use scheme::FileHandle;
|
2016-09-15 16:35:07 +02:00
|
|
|
|
2016-11-13 13:59:58 +01:00
|
|
|
#[macro_use]
|
|
|
|
/// Shared data structures
|
|
|
|
pub mod common;
|
2016-09-15 16:35:07 +02:00
|
|
|
|
2016-08-15 02:16:56 +02:00
|
|
|
/// Context management
|
|
|
|
pub mod context;
|
|
|
|
|
2016-08-19 03:44:31 +02:00
|
|
|
/// ELF file parsing
|
|
|
|
pub mod elf;
|
|
|
|
|
2016-08-15 05:38:32 +02:00
|
|
|
/// Schemes, filesystem handlers
|
|
|
|
pub mod scheme;
|
|
|
|
|
Orbital (#16)
* Port previous ethernet scheme
* Add ipd
* Fix initfs rebuilds, use QEMU user networking addresses in ipd
* Add tcp/udp, netutils, dns, and network config
* Add fsync to network driver
* Add dns, router, subnet by default
* Fix e1000 driver. Make ethernet and IP non-blocking to avoid deadlocks
* Add orbital server, WIP
* Add futex
* Add orbutils and orbital
* Update libstd, orbutils, and orbital
Move ANSI key encoding to vesad
* Add orbital assets
* Update orbital
* Update to add login manager
* Add blocking primitives, block for most things except waitpid, update orbital
* Wait in waitpid and IRQ, improvements for other waits
* Fevent in root scheme
* WIP: Switch to using fevent
* Reorganize
* Event based e1000d driver
* Superuser-only access to some network schemes, display, and disk
* Superuser root and irq schemes
* Fix orbital
2016-10-14 01:21:42 +02:00
|
|
|
/// Synchronization primitives
|
|
|
|
pub mod sync;
|
|
|
|
|
2016-08-14 23:58:35 +02:00
|
|
|
/// Syscall handlers
|
|
|
|
pub mod syscall;
|
|
|
|
|
2016-08-14 22:59:18 +02:00
|
|
|
/// Tests
|
|
|
|
#[cfg(test)]
|
|
|
|
pub mod tests;
|
|
|
|
|
2016-11-04 03:29:19 +01:00
|
|
|
/// A unique number that identifies the current CPU - used for scheduling
|
2016-09-15 16:35:07 +02:00
|
|
|
#[thread_local]
|
|
|
|
static CPU_ID: AtomicUsize = ATOMIC_USIZE_INIT;
|
|
|
|
|
2016-11-04 03:29:19 +01:00
|
|
|
/// Get the current CPU's scheduling ID
|
2016-09-15 16:35:07 +02:00
|
|
|
#[inline(always)]
|
|
|
|
pub fn cpu_id() -> usize {
|
|
|
|
CPU_ID.load(Ordering::Relaxed)
|
|
|
|
}
|
|
|
|
|
2016-11-04 03:29:19 +01:00
|
|
|
/// The count of all CPUs that can have work scheduled
|
2016-10-23 17:13:12 +02:00
|
|
|
static CPU_COUNT : AtomicUsize = ATOMIC_USIZE_INIT;
|
|
|
|
|
2016-11-04 03:29:19 +01:00
|
|
|
/// Get the number of CPUs currently active
|
2016-10-23 17:13:12 +02:00
|
|
|
#[inline(always)]
|
|
|
|
pub fn cpu_count() -> usize {
|
|
|
|
CPU_COUNT.load(Ordering::Relaxed)
|
|
|
|
}
|
|
|
|
|
2016-11-04 03:29:19 +01:00
|
|
|
/// Initialize userspace by running the initfs:bin/init process
|
|
|
|
/// This function will also set the CWD to initfs:bin and open debug: as stdio
|
2016-09-11 06:06:09 +02:00
|
|
|
pub extern fn userspace_init() {
|
2016-11-30 02:14:10 +01:00
|
|
|
assert_eq!(syscall::chdir(b"initfs:"), Ok(0));
|
2016-09-18 03:01:34 +02:00
|
|
|
|
2016-11-26 02:24:38 +01:00
|
|
|
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));
|
2016-08-24 18:35:42 +02:00
|
|
|
|
2016-11-30 02:15:53 +01:00
|
|
|
syscall::exec(b"/bin/init", &[]).expect("failed to execute init");
|
2016-09-11 06:06:09 +02:00
|
|
|
|
2016-11-30 02:15:53 +01:00
|
|
|
panic!("init returned")
|
2016-08-24 18:35:42 +02:00
|
|
|
}
|
|
|
|
|
2016-10-31 17:49:00 +01:00
|
|
|
/// Allow exception handlers to send signal to arch-independant kernel
|
|
|
|
#[no_mangle]
|
|
|
|
pub extern fn ksignal(signal: usize) {
|
2016-11-13 15:36:52 +01:00
|
|
|
println!("SIGNAL {}, CPU {}, PID {:?}", signal, cpu_id(), context::context_id());
|
2016-10-31 17:49:00 +01:00
|
|
|
{
|
|
|
|
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()) });
|
|
|
|
}
|
|
|
|
}
|
2016-11-17 20:12:02 +01:00
|
|
|
syscall::exit(signal & 0x7F);
|
2016-10-31 17:49:00 +01:00
|
|
|
}
|
|
|
|
|
2016-11-04 03:29:19 +01:00
|
|
|
/// This is the kernel entry point for the primary CPU. The arch crate is responsible for calling this
|
2016-08-14 17:31:35 +02:00
|
|
|
#[no_mangle]
|
2016-10-23 17:13:12 +02:00
|
|
|
pub extern fn kmain(cpus: usize) {
|
2016-09-15 16:35:07 +02:00
|
|
|
CPU_ID.store(0, Ordering::SeqCst);
|
2016-10-23 17:13:12 +02:00
|
|
|
CPU_COUNT.store(cpus, Ordering::SeqCst);
|
2016-09-15 16:35:07 +02:00
|
|
|
|
2016-08-20 22:32:45 +02:00
|
|
|
context::init();
|
|
|
|
|
2016-09-01 19:51:33 +02:00
|
|
|
let pid = syscall::getpid();
|
2016-10-23 17:13:12 +02:00
|
|
|
println!("BSP: {:?} {}", pid, cpus);
|
2016-08-19 03:44:31 +02:00
|
|
|
|
2016-09-12 02:03:10 +02:00
|
|
|
match context::contexts_mut().spawn(userspace_init) {
|
|
|
|
Ok(context_lock) => {
|
|
|
|
let mut context = context_lock.write();
|
2016-09-16 19:10:53 +02:00
|
|
|
context.status = context::Status::Runnable;
|
2016-09-12 02:03:10 +02:00
|
|
|
},
|
|
|
|
Err(err) => {
|
|
|
|
panic!("failed to spawn userspace_init: {:?}", err);
|
|
|
|
}
|
|
|
|
}
|
2016-08-24 18:35:42 +02:00
|
|
|
|
2016-08-14 19:45:47 +02:00
|
|
|
loop {
|
2016-09-15 16:39:20 +02:00
|
|
|
unsafe {
|
2016-09-15 16:40:16 +02:00
|
|
|
interrupt::disable();
|
2016-09-23 23:47:53 +02:00
|
|
|
if context::switch() {
|
|
|
|
interrupt::enable_and_nop();
|
|
|
|
} else {
|
2016-10-25 00:03:03 +02:00
|
|
|
// Enable interrupts, then halt CPU (to save power) until the next interrupt is actually fired.
|
2016-09-23 23:47:53 +02:00
|
|
|
interrupt::enable_and_halt();
|
|
|
|
}
|
2016-09-15 16:39:20 +02:00
|
|
|
}
|
2016-08-18 03:38:04 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-04 03:29:19 +01:00
|
|
|
/// This is the main kernel entry point for secondary CPUs
|
2016-08-18 03:38:04 +02:00
|
|
|
#[no_mangle]
|
2016-08-19 03:44:31 +02:00
|
|
|
pub extern fn kmain_ap(id: usize) {
|
2016-09-15 16:35:07 +02:00
|
|
|
CPU_ID.store(id, Ordering::SeqCst);
|
|
|
|
|
2016-08-20 22:32:45 +02:00
|
|
|
context::init();
|
|
|
|
|
2016-09-01 19:51:33 +02:00
|
|
|
let pid = syscall::getpid();
|
|
|
|
println!("AP {}: {:?}", id, pid);
|
2016-08-19 03:44:31 +02:00
|
|
|
|
2016-11-04 20:38:40 +01:00
|
|
|
// Disable APs for now
|
|
|
|
loop {
|
|
|
|
unsafe { interrupt::enable_and_halt(); }
|
|
|
|
}
|
2016-11-17 04:54:38 +01:00
|
|
|
/*
|
2016-08-18 03:38:04 +02:00
|
|
|
loop {
|
2016-10-31 17:49:00 +01:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
}
|
2016-08-14 19:45:47 +02:00
|
|
|
}
|
2016-11-17 04:54:38 +01:00
|
|
|
*/
|
2016-08-14 17:31:35 +02:00
|
|
|
}
|