2016-08-14 02:21:46 +02:00
|
|
|
/// This function is where the kernel sets up IRQ handlers
|
|
|
|
/// It is increcibly unsafe, and should be minimal in nature
|
|
|
|
/// It must create the IDT with the correct entries, those entries are
|
|
|
|
/// defined in other files inside of the `arch` module
|
2016-08-14 02:58:31 +02:00
|
|
|
|
2016-08-18 03:06:33 +02:00
|
|
|
use core::sync::atomic::{AtomicBool, ATOMIC_BOOL_INIT, AtomicUsize, ATOMIC_USIZE_INIT, Ordering};
|
|
|
|
use x86::controlregs;
|
2016-08-18 01:40:18 +02:00
|
|
|
|
2016-08-16 02:37:58 +02:00
|
|
|
use acpi;
|
2016-08-15 23:27:32 +02:00
|
|
|
use allocator::{HEAP_START, HEAP_SIZE};
|
2016-08-15 19:29:53 +02:00
|
|
|
use externs::memset;
|
2016-08-15 22:34:20 +02:00
|
|
|
use gdt;
|
2016-08-15 19:29:53 +02:00
|
|
|
use idt;
|
2016-08-18 03:34:33 +02:00
|
|
|
use interrupt;
|
2016-08-18 00:54:48 +02:00
|
|
|
use memory;
|
2016-08-17 05:25:48 +02:00
|
|
|
use paging::{self, entry, Page, VirtualAddress};
|
2016-08-15 19:29:53 +02:00
|
|
|
|
|
|
|
/// Test of zero values in BSS.
|
|
|
|
static BSS_TEST_ZERO: usize = 0;
|
|
|
|
/// Test of non-zero values in BSS.
|
|
|
|
static BSS_TEST_NONZERO: usize = 0xFFFFFFFFFFFFFFFF;
|
|
|
|
|
2016-08-18 03:34:33 +02:00
|
|
|
static AP_COUNT: AtomicUsize = ATOMIC_USIZE_INIT;
|
2016-08-18 01:40:18 +02:00
|
|
|
static BSP_READY: AtomicBool = ATOMIC_BOOL_INIT;
|
2016-08-18 03:06:33 +02:00
|
|
|
static BSP_PAGE_TABLE: AtomicUsize = ATOMIC_USIZE_INIT;
|
2016-08-18 01:40:18 +02:00
|
|
|
|
2016-08-15 19:29:53 +02:00
|
|
|
extern {
|
|
|
|
/// Kernel main function
|
|
|
|
fn kmain() -> !;
|
|
|
|
}
|
2016-08-14 17:31:35 +02:00
|
|
|
|
2016-08-17 05:25:48 +02:00
|
|
|
/// The entry to Rust, all things must be initialized
|
2016-08-14 02:21:46 +02:00
|
|
|
#[no_mangle]
|
2016-08-14 19:45:47 +02:00
|
|
|
pub unsafe extern fn kstart() -> ! {
|
2016-08-14 17:31:35 +02:00
|
|
|
{
|
2016-08-16 00:29:54 +02:00
|
|
|
extern {
|
|
|
|
/// The starting byte of the _.bss_ (uninitialized data) segment.
|
|
|
|
static mut __bss_start: u8;
|
|
|
|
/// The ending byte of the _.bss_ (uninitialized data) segment.
|
|
|
|
static mut __bss_end: u8;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Zero BSS, this initializes statics that are set to 0
|
|
|
|
{
|
|
|
|
let start_ptr = &mut __bss_start as *mut u8;
|
|
|
|
let end_ptr = & __bss_end as *const u8 as usize;
|
2016-08-14 17:31:35 +02:00
|
|
|
|
2016-08-16 00:29:54 +02:00
|
|
|
if start_ptr as usize <= end_ptr {
|
|
|
|
let size = end_ptr - start_ptr as usize;
|
|
|
|
memset(start_ptr, 0, size);
|
|
|
|
}
|
|
|
|
|
|
|
|
debug_assert_eq!(BSS_TEST_ZERO, 0);
|
|
|
|
debug_assert_eq!(BSS_TEST_NONZERO, 0xFFFFFFFFFFFFFFFF);
|
2016-08-14 17:31:35 +02:00
|
|
|
}
|
|
|
|
|
2016-08-16 00:29:54 +02:00
|
|
|
// Set up GDT
|
|
|
|
gdt::init();
|
2016-08-14 17:31:35 +02:00
|
|
|
|
2016-08-16 00:29:54 +02:00
|
|
|
// Set up IDT
|
2016-08-16 02:37:58 +02:00
|
|
|
idt::init();
|
2016-08-15 22:34:20 +02:00
|
|
|
|
2016-08-16 00:29:54 +02:00
|
|
|
// Initialize memory management
|
2016-08-18 00:26:43 +02:00
|
|
|
memory::init(0, &__bss_end as *const u8 as usize);
|
2016-08-14 17:31:35 +02:00
|
|
|
|
2016-08-18 00:26:43 +02:00
|
|
|
// TODO: allocate a stack
|
|
|
|
let stack_start = 0x00080000;
|
|
|
|
let stack_end = 0x0009F000;
|
2016-08-14 02:58:31 +02:00
|
|
|
|
2016-08-18 00:26:43 +02:00
|
|
|
// Initialize paging
|
|
|
|
let mut active_table = paging::init(stack_start, stack_end);
|
2016-08-14 17:31:35 +02:00
|
|
|
|
2016-08-18 03:34:33 +02:00
|
|
|
// Reset AP variables
|
|
|
|
AP_COUNT.store(1, Ordering::SeqCst);
|
|
|
|
BSP_READY.store(false, Ordering::SeqCst);
|
2016-08-18 03:06:33 +02:00
|
|
|
BSP_PAGE_TABLE.store(controlregs::cr3() as usize, Ordering::SeqCst);
|
|
|
|
|
|
|
|
// Read ACPI tables, starts APs
|
2016-08-18 00:26:43 +02:00
|
|
|
acpi::init(&mut active_table);
|
2016-08-16 02:37:58 +02:00
|
|
|
|
2016-08-18 00:26:43 +02:00
|
|
|
// Map heap
|
|
|
|
let heap_start_page = Page::containing_address(VirtualAddress::new(HEAP_START));
|
|
|
|
let heap_end_page = Page::containing_address(VirtualAddress::new(HEAP_START + HEAP_SIZE-1));
|
2016-08-15 23:01:24 +02:00
|
|
|
|
2016-08-18 00:26:43 +02:00
|
|
|
for page in Page::range_inclusive(heap_start_page, heap_end_page) {
|
|
|
|
active_table.map(page, entry::WRITABLE | entry::NO_EXECUTE);
|
2016-08-17 23:47:54 +02:00
|
|
|
}
|
2016-08-18 01:40:18 +02:00
|
|
|
|
|
|
|
BSP_READY.store(true, Ordering::SeqCst);
|
2016-08-18 03:34:33 +02:00
|
|
|
|
|
|
|
print!("BSP\n");
|
2016-08-18 01:40:18 +02:00
|
|
|
}
|
|
|
|
|
2016-08-14 19:45:47 +02:00
|
|
|
kmain();
|
2016-08-14 02:58:31 +02:00
|
|
|
}
|
2016-08-17 05:25:48 +02:00
|
|
|
|
|
|
|
/// Entry to rust for an AP
|
2016-08-17 23:47:54 +02:00
|
|
|
pub unsafe extern fn kstart_ap(stack_start: usize, stack_end: usize) -> ! {
|
2016-08-18 00:26:43 +02:00
|
|
|
{
|
|
|
|
// Set up GDT for AP
|
|
|
|
gdt::init_ap();
|
2016-08-17 23:47:54 +02:00
|
|
|
|
2016-08-18 00:26:43 +02:00
|
|
|
// Set up IDT for aP
|
|
|
|
idt::init_ap();
|
2016-08-17 23:47:54 +02:00
|
|
|
|
|
|
|
// Initialize paging
|
2016-08-18 03:34:33 +02:00
|
|
|
//let mut active_table =
|
2016-08-18 03:06:33 +02:00
|
|
|
paging::init_ap(stack_start, stack_end, BSP_PAGE_TABLE.load(Ordering::SeqCst));
|
2016-08-17 23:47:54 +02:00
|
|
|
}
|
|
|
|
|
2016-08-18 03:34:33 +02:00
|
|
|
let ap_number = AP_COUNT.fetch_add(1, Ordering::SeqCst);
|
|
|
|
|
2016-08-18 01:40:18 +02:00
|
|
|
while ! BSP_READY.load(Ordering::SeqCst) {
|
|
|
|
asm!("pause" : : : : "intel", "volatile");
|
|
|
|
}
|
|
|
|
|
2016-08-18 03:34:33 +02:00
|
|
|
print!("{}", ::core::str::from_utf8_unchecked(&[b'A', b'P', b' ', ap_number as u8 + b'0', b'\n']));
|
|
|
|
|
2016-08-17 05:25:48 +02:00
|
|
|
loop {
|
2016-08-18 03:34:33 +02:00
|
|
|
interrupt::enable_and_halt();
|
2016-08-17 05:25:48 +02:00
|
|
|
}
|
|
|
|
}
|