1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#![no_std]
#![feature(allocator_api)]
#![feature(naked_functions)]
#![feature(abi_x86_interrupt)]
#![feature(alloc_error_handler)]

use core::sync::atomic::{AtomicBool, Ordering};

extern crate alloc;

pub mod arch;
pub mod console;
pub mod drivers;
pub mod memory;
pub mod task;
pub mod user;

static START_SCHEDULE: AtomicBool = AtomicBool::new(false);

pub fn init_framework() {
    memory::init();
    console::init();
    arch::smp::CPUS.lock().init_bsp();
    arch::interrupts::IDT.load();
    #[cfg(feature = "smp")]
    arch::smp::CPUS.lock().init_ap();
    arch::acpi::init();
    drivers::hpet::init();

    let mut lapic = arch::apic::get_lapic();
    unsafe {
        lapic.enable();
        arch::apic::calibrate_timer(&mut lapic);
        lapic.enable_timer();
    }

    arch::apic::init();
    drivers::mouse::init();
    drivers::pci::init();
    user::init();
    task::scheduler::init();
}

#[inline]
pub fn start_schedule() {
    START_SCHEDULE.store(true, Ordering::Relaxed);
    x86_64::instructions::interrupts::enable();
}

pub fn addr_of<T>(reffer: &T) -> usize {
    reffer as *const T as usize
}

pub fn ref_to_mut<T>(reffer: &T) -> &mut T {
    unsafe { &mut *(addr_of(reffer) as *const T as *mut T) }
}

pub fn ref_to_static<T>(reffer: &T) -> &'static T {
    unsafe { &*(addr_of(reffer) as *const T) }
}

#[macro_export]
macro_rules! unsafe_trait_impl {
    ($struct: ident, $trait: ident) => {
        unsafe impl $trait for $struct {}
    };
    ($struct: ident, $trait: ident, $life: tt) => {
        unsafe impl<$life> $trait for $struct<$life> {}
    };
}