KarxOS/src/main.rs

100 lines
2.3 KiB
Rust
Raw Permalink Normal View History

2021-09-16 17:30:15 -05:00
#![allow(non_snake_case)]
2021-09-15 14:56:54 -05:00
#![no_std]
#![no_main]
2021-09-16 10:57:11 -05:00
#![feature(custom_test_frameworks)]
#![test_runner(crate::test_runner)]
#![reexport_test_harness_main = "test_main"]
#![feature(abi_x86_interrupt)]
2021-09-19 13:59:28 -05:00
#![feature(alloc_error_handler)]
2021-09-16 10:57:11 -05:00
2021-09-20 09:28:49 -05:00
mod allocator;
mod clock;
2021-09-16 11:20:56 -05:00
mod gdt;
2021-09-17 21:33:58 -05:00
mod interrupts;
2021-09-20 09:28:49 -05:00
mod memory;
mod shell;
2021-09-17 21:33:58 -05:00
mod vga_buffer;
2021-09-26 13:14:30 -05:00
use bootloader::entry_point;
2021-09-26 13:14:57 -05:00
use bootloader::BootInfo;
2021-09-20 09:28:49 -05:00
use core::panic::PanicInfo;
2021-09-15 14:56:54 -05:00
2021-09-19 13:59:28 -05:00
extern crate alloc;
2021-09-15 14:56:54 -05:00
#[panic_handler]
2021-09-15 21:26:38 -05:00
fn panic(info: &PanicInfo) -> ! {
println!("{}", info);
2021-09-15 14:56:54 -05:00
loop {}
}
2021-09-19 13:59:28 -05:00
#[alloc_error_handler]
fn alloc_error_handler(layout: alloc::alloc::Layout) -> ! {
panic!("allocation error: {:?}", layout)
}
2021-09-16 10:57:11 -05:00
fn init() {
2021-09-16 11:20:56 -05:00
gdt::init_gdt();
2021-09-16 12:13:36 -05:00
interrupts::init();
2021-09-26 13:14:30 -05:00
unsafe {
interrupts::PICS.lock().initialize();
}
x86_64::instructions::interrupts::enable();
2021-09-16 10:57:11 -05:00
}
macro_rules! status {
($n:expr) => {
print!("[ ");
change_color(Color::Green, Color::Black);
print!("OK");
change_color(Color::White, Color::Black);
println!(" ] {}", $n);
2021-09-26 13:14:57 -05:00
};
}
2021-09-26 13:14:30 -05:00
entry_point!(main);
fn main(boot_info: &'static BootInfo) -> ! {
2021-09-26 13:14:57 -05:00
use crate::vga_buffer::{change_color, Color};
2021-09-19 14:12:41 -05:00
use memory::BootInfoFrameAllocator;
2021-09-26 13:14:57 -05:00
use x86_64::VirtAddr; // For status! macro
2021-09-15 21:23:09 -05:00
2021-09-16 10:57:11 -05:00
init();
status!("Initialized GDT and Interrupts");
clock::init();
status!("Initialized system clock");
2021-09-15 15:08:34 -05:00
2021-09-19 14:12:41 -05:00
let phys_mem_offset = VirtAddr::new(boot_info.physical_memory_offset);
let mut mapper = unsafe { memory::init(phys_mem_offset) };
2021-09-20 09:28:49 -05:00
let mut frame_allocator = unsafe { BootInfoFrameAllocator::init(&boot_info.memory_map) };
status!("Initialized Mapper and Frame allocator");
2021-09-19 14:12:41 -05:00
allocator::init_heap(&mut mapper, &mut frame_allocator).expect("Heap initialization failed");
status!("Initialized heap");
2021-09-19 14:12:41 -05:00
println!();
2021-09-17 08:52:22 -05:00
print!("Welcome to ");
change_color(Color::Blue, Color::Black);
println!("KarxOS!");
change_color(Color::White, Color::Black);
2021-09-16 10:57:11 -05:00
2021-09-17 12:58:37 -05:00
// First prompt, future prompts will be handled by shell::evaluate
print!(">>> ");
2021-09-16 10:57:11 -05:00
#[cfg(test)]
test_main();
2021-09-16 12:13:36 -05:00
loop {
2021-09-17 21:29:33 -05:00
// Halt CPU so that usage isn't 100% all the time
2021-09-16 12:13:36 -05:00
x86_64::instructions::hlt();
}
2021-09-15 11:34:02 -05:00
}
2021-09-16 10:57:11 -05:00
#[cfg(test)]
fn test_runner(tests: &[&dyn Fn()]) {
println!("Running {} tests", tests.len());
for test in tests {
test();
}
}