kernel/src/main.rs

75 lines
1.7 KiB
Rust
Raw Normal View History

2024-05-14 11:03:08 -04:00
// src/main.rs
2024-05-13 18:31:37 -04:00
#![no_std]
#![no_main]
#![feature(naked_functions)]
use core::panic::PanicInfo;
2024-05-14 11:03:08 -04:00
mod heap;
2024-05-13 23:12:29 -04:00
mod uart;
2024-05-13 18:31:37 -04:00
#[naked]
#[no_mangle]
#[link_section = ".text.init"]
unsafe extern "C" fn _start() -> ! {
use core::arch::asm;
asm!(
2024-05-14 11:06:57 -04:00
// before we use the `la` pseudo-instruction for the first time,
// we need to set `gp` (google linker relaxation)
".option push",
".option norelax",
"la gp, _global_pointer",
".option pop",
2024-05-13 18:31:37 -04:00
2024-05-14 11:06:57 -04:00
// set the stack pointer
"la sp, _init_stack_top",
2024-05-13 18:31:37 -04:00
2024-05-14 11:06:57 -04:00
// clear the BSS
"la t0, _bss_start",
"la t1, _bss_end",
"bgeu t0, t1, 2f",
2024-05-13 23:59:41 -04:00
"1:",
2024-05-14 11:06:57 -04:00
"sb zero, 0(t0)",
"addi t0, t0, 1",
"bne t0, t1, 1b",
2024-05-13 23:59:41 -04:00
"2:",
2024-05-14 11:06:57 -04:00
// BSS is clear!
2024-05-13 23:59:41 -04:00
2024-05-14 11:06:57 -04:00
// "tail-call" to {entry} (call without saving a return address)
"tail {entry}",
entry = sym entry, // {entry} refers to the function [entry] below
options(noreturn) // we must handle "returning" from assembly
2024-05-13 18:31:37 -04:00
);
}
extern "C" fn entry() -> ! {
2024-05-14 11:03:08 -04:00
// UNSAFE: Called exactly once, right here.
unsafe { heap::init() };
// Now we're free to use dynamic allocation!
{
extern crate alloc;
use alloc::boxed::Box;
let _my_heap_pointer = Box::new(10);
// _my_heap_pointer lives on the heap!
}
2024-05-13 23:59:41 -04:00
println!("This should not print because the console is not initialised.");
unsafe { uart::init_console(0x1000_0000) };
println!("Hello, world!");
loop {
let c = uart::CONSOLE.lock().as_mut().and_then(uart::Device::get);
if let Some(c) = c {
print!("{}", c as char);
}
2024-05-13 18:31:37 -04:00
}
}
#[panic_handler]
fn on_panic(_info: &PanicInfo) -> ! {
loop {}
}
2024-05-14 11:03:08 -04:00
2024-05-14 11:06:57 -04:00
// TODO unit testing