[][src]Crate rustos

Writing an OS in Rust

Examples

Here is the current entry point, start_kernel(), and the panic handler.

#![no_std]
#![no_main]
#![feature(custom_test_frameworks)]
#![test_runner(rustos::test_runner)]
#![reexport_test_harness_main = "test_main"]
extern crate alloc;
extern crate bootloader;
extern crate rustos;
extern crate x86_64;
use alloc::{boxed::Box, rc::Rc, vec, vec::Vec};
use bootloader::{entry_point, BootInfo};
use core::panic::PanicInfo;
use rustos::println;

entry_point!(start_kernel);

fn start_kernel(boot_info: &'static BootInfo) -> ! {
    println!("Welcome to the real world!");

    // Initialize the kernel.
    rustos::init();
    rustos::memory::init(boot_info);

    // Let's box it on heap!
    let x = Box::new(41);
    println!("x={:p}", x);

    // and then vector!
    let mut vec = Vec::new();
    for i in 0..500 {
        vec.push(i);
    }
    println!("vec at {:p}", vec.as_slice());

    // now, a reference counted vector.
    let reference = Rc::new(vec![1, 2, 3]);
    let cloned = Rc::clone(&reference);
    println!("current reference count is {}", Rc::strong_count(&cloned));
    core::mem::drop(reference);
    println!("reference count is {} now", Rc::strong_count(&cloned));

    // Long lived many boxes allocation!
    let long_lived = Box::new(1);
    for i in 0..rustos::HEAP_SIZE {
        let x = Box::new(i);
        assert_eq!(*x, i);
    }
    assert_eq!(*long_lived, 1);

    #[cfg(test)]
    test_main();
    println!("It did not crash!!!");
    rustos::hlt_loop();
}

#[cfg(not(test))]
#[panic_handler]
fn panic(info: &PanicInfo) -> ! {
    println!("{}", info);
    rustos::hlt_loop();
}

#[cfg(test)]
#[panic_handler]
fn panic(info: &PanicInfo) -> ! {
    rustos::test_panic_handler(info)
}

Modules

memory

Memory mapper and the frame allocator

serial

Serial driver

vga

VGA driver

Macros

print

Print out the message on the VGA console.

println

Print out the message on the VGA console.

serial_print

Print out the message to the serial port.

serial_println

Print out the message to the serial port.

Enums

QemuExitCode

Qemu exit codes.

Constants

HEAP_SIZE

Kernel heap size.

HEAP_START

Kernel heap start address.

Functions

exit_qemu

Qemu exit function.

hlt_loop

hlt instruction based kernel loop.

init

Kernel initialization function.

test_panic_handler

Unit and the integration test panic handler.

test_runner

Unit and the integration test runner.