assemblyrustkernellibrariesosdev

In kernel development, why do developers avoid pasting in high level libraries directly to the kernel?


Context: I am a somewhat seasoned C++ and Python developer, and a beginner in Rust with no experience in assembly or machine code following the blogOS guide trying to create a minimal kernel that prints Hello World to the screen when booted, to learn about low-level programming.

To reiterate the title: Why can I not just paste in the logic for println! macro into a custom library for the kernel and write it as if it were high-level Rust?

I have gone a little off-book and tried importing the macro but to no avail, I also got lost trying to follow to rabbit hole of how the println! macro actually works. Maybe this isn't the best way to start learning.

My current code is as followed by the guide so far:

#![no_std] // Don't link the standard library.
#![no_main] // Disable all Rust-level entry points.

use core::panic::PanicInfo;

#[unsafe(no_mangle)] // Don't mangle the name of this function.
pub extern "C" fn _start() -> ! {
    // This function is the entry point, since the linker looks for a function named `_start` by default.
    loop {}
}

// This function is called on panic.
#[panic_handler]
fn panic(_info: &PanicInfo) -> ! {
    loop {}
}

Solution

  • High level libraries like usually the standard libraries in most languages implementations depend eventually on making low level calls to the operating system APIs. When you write an operating system kernel you don't have any of those available, because you are implementing those low level APIs. Also you can not just use some implementation that doesn't consider the limitations of writing kernel code. For example memory or threading management inside the kernel.