[][src]Crate rustos

Writing an OS in Rust

Examples

Current start_kernel() function 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 bootloader;
extern crate rustos;
extern crate x86_64;
use bootloader::{entry_point, BootInfo};
use core::panic::PanicInfo;
use rustos::println;
use x86_64::{
   structures::paging::{MapperAllSizes, Page},
   VirtAddr,
};

entry_point!(start_kernel);

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

    rustos::init();

    // frame allocator.
    let phys_mem_offset = VirtAddr::new(boot_info.physical_memory_offset);
    let mut mapper = unsafe { rustos::memory::init(phys_mem_offset) };
    let mut frame_allocator =
        unsafe { rustos::memory::BootInfoFrameAllocator::init(&boot_info.memory_map) };

    // write the string "New!" to the screen!
    let page = Page::containing_address(VirtAddr::new(0));
    create_example_mapping(page, &mut mapper, &mut frame_allocator);
    let page_ptr: *mut u64 = page.start_address().as_mut_ptr();
    unsafe { page_ptr.offset(400).write_volatile(0x_f021_f077_f065_f04e) };

    #[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)
}

use x86_64::{
    structures::paging::{
        FrameAllocator, Mapper, OffsetPageTable, PhysFrame, Size4KiB, UnusedPhysFrame,
    },
    PhysAddr,
};

fn create_example_mapping(
    page: Page,
    mapper: &mut OffsetPageTable,
    frame_allocator: &mut impl FrameAllocator<Size4KiB>,
{
    use x86_64::structures::paging::PageTableFlags as Flags;

    let frame = PhysFrame::containing_address(PhysAddr::new(0xb8000));
    let unused_frame = unsafe { UnusedPhysFrame::new(frame) };
    let flags = Flags::PRESENT | Flags::WRITABLE;

    let map_to_result = mapper.map_to(page, unused_frame, flags, frame_allocator);
    map_to_result.expect("map_to failed").flush();
}

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.

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.