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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
//! Rust version of the StuBS Kernel
//!
//! The assignments are documented in the [assignments] module.
#![doc = include_str!("../README.md")]
// don't link the Rust standard library
#![no_std]
#![no_main]
// unstable features
#![feature(abi_x86_interrupt)]
#![feature(let_chains)]
#![feature(naked_functions)]
// warnings
#![allow(clippy::assertions_on_constants)]
#![allow(clippy::redundant_pattern_matching)]

use core::arch::global_asm;
use core::panic::PanicInfo;

use multiboot as mb;

#[macro_use]
mod device;
#[macro_use]
mod arch;

mod assignments;
mod gdt;
#[cfg(feature = "graphics")]
mod graphics;
mod interrupts;
mod multiboot;
mod threading;
mod user;
mod util;

use device::cga::Window;
use device::KEYBOARD;

use crate::arch::acpi::Acpi;
#[cfg(feature = "smp")]
use crate::arch::smp;
use crate::arch::{cpu, int};
use crate::device::{DBG, KOUT};

/// Maximal number of cores the OS supports.
#[cfg(feature = "smp")]
const MAX_CPUS: usize = 4;
#[cfg(not(feature = "smp"))]
const MAX_CPUS: usize = 1;

/// Multiboot flags
#[cfg(not(feature = "graphics"))]
const FLAGS: u32 = mb::Header::PAGE_ALIGN | mb::Header::MEM_INFO;
#[cfg(feature = "graphics")]
const FLAGS: u32 = mb::Header::PAGE_ALIGN | mb::Header::MEM_INFO | mb::Header::VIDEO_MODE;

const SCREEN_WIDTH: usize = 1280;
const SCREEN_HEIGHT: usize = 1024;

/// Multiboot header, used by QEMU/Grub
#[link_section = ".multiboot"]
#[no_mangle]
pub static MBOOT: mb::Header = mb::Header::new(FLAGS, SCREEN_WIDTH as _, SCREEN_HEIGHT as _, 32);

/// Signature of the multiboot header.
#[no_mangle]
pub static mut MBOOT_SIG: u32 = 0;
/// Pointer to the multiboot header.
#[no_mangle]
pub static mut MBOOT_PTR: u32 = 0;

// Boot assembly
global_asm!(include_str!("start.s"), options(raw));

#[allow(unused)]
extern "C" {
    /// Start address of the loaded kernel code.
    static KERNEL_BEGIN: u8;
    /// End address of the loaded kernel code.
    static KERNEL_END: u8;
    /// The start_high function.
    static start_high: u8;
}

const STACK_S: usize = 16 * 1024;

/// The stack size for the init and thread stacks.
///
/// Note: If you create objects, they are usually allocated on the stack first
/// before beeing moved or copied elswhere.
#[no_mangle]
pub static STACK_SIZE: usize = STACK_S;

/// Stacks for each core, used for initialization (set before calling main).
///
/// It is also reused for the idle threads.
#[no_mangle]
pub static mut INIT_STACKS: [[u32; STACK_S / 4]; MAX_CPUS] = [[0; STACK_S / 4]; MAX_CPUS];

/// Main function that is called for every core
#[no_mangle]
pub extern "C" fn main() -> ! {
    // boot processor only
    if cpu::online() == 0 {
        // TODO: BSB A1 - Initialize serial console

        Window::whole().clear();
        unsafe { DBG.clear() };
        unsafe { KOUT.clear() };
        println!("Hello World!");

        // Load system tables and setup interrupt controllers
        let acpi = Acpi::load().unwrap();
        serial!("global int");
        #[allow(unused_variables)]
        let cpus = interrupts::setup_global(acpi);

        interrupts::setup_local();

        unsafe { KEYBOARD.init() };

        // TODO: BSB A4 - Setup threads

        #[cfg(feature = "smp")]
        if cpus > 1 {
            smp::boot();
            // Wait for cores to be online
            while cpu::online().count_ones() < cpus as u32 {
                core::hint::spin_loop();
            }
        }
    } else {
        interrupts::setup_local();
    }

    // TODO: BSB A2 - Enable interrupts

    // TODO: BSB A4 - Start the first thread (also enter the guard)

    loop {
        int::enable(false);
        cpu::halt();
    }
}

/// This function is called on panic.
#[panic_handler]
fn panic(info: &PanicInfo) -> ! {
    serial!(force: "{info}");
    debug!("{info}");
    loop {
        int::enable(false);
        cpu::halt();
    }
}