A tiny 32 bit kernel written in Rust

Overview

rustboot

A tiny 32 bit kernel written in Rust.

I was inspired to download Rust and try to do this after seeing zero.rs - a stub that lets Rust programs run almost freestanding.

It paints the screen bright red and then hangs. That's it:

Interesting forks

Setup

You need a few things to run rustboot:

  1. qemu
  2. a cross-compiler for i386
  3. nasm
  4. Rust's master branch or 0.7 release.

OSX

To set things up on OSX, do this:

Install nasm and qemu from homebrew:

$ brew install nasm
$ brew install qemu

Make sure the brew version of nasm is being used:

$ nasm -v
NASM version 2.11.02 compiled on Apr 14 2014

Install binutils from source.

I personally keep things I manually compile limited to my home directory, so I use the --prefix=/Users/steve option. Put this wherever you want, of course.

$ wget http://ftp.gnu.org/gnu/binutils/binutils-2.24.tar.gz
$ tar xf binutils-2.24.tar.gz
$ cd binutils-2.24
$ ./configure --target=i386-elf --disable-werror --prefix=/Users/steve
$ make && make install

To get edge Rust going, grab it from git:

$ git clone https://github.com/mozilla/rust
$ cd rust
$ ./configure --prefix=/Users/steve
$ make && make install

Same thing about the prefix applies.

Then, just make sure that ~/bin is in your PATH, if you're using a prefix.

Running it

To compile, simply

$ make

To run,

$ make run
Comments
  • i386-elf-ld: No such file or directory

    i386-elf-ld: No such file or directory

    Any idea what might cause this? My rust is at f254d119eab514c91fbd6590fba821ec7a0bfd7f.

    nasm -o loader.bin -f bin loader.asm
    nasm -f elf32 -o runtime.o runtime.asm
    rustc -O --target i386-intel-linux --lib -o main.o -c main.rs
    warning: missing crate link meta `name`, using `main` as default
    warning: missing crate link meta `vers`, using `0.0` as default
    i386-elf-ld -o main.bin -T linker.ld runtime.o main.o
    make: i386-elf-ld: No such file or directory
    make: *** [main.bin] Error 1
    
    opened by steveklabnik 17
  • Public domain?

    Public domain?

    Awesome stuff. To ensure it is useful as widely as possible, please consider putting the project into the public domain with http://unlicense.org/ or CC0.

    For instance, there are people writing public domain bootloaders and kernels on GitHub:

    https://github.com/shonma29/eotan/blob/master/drivers/i686/startup.s https://github.com/search?o=desc&p=1&q=UNLICENSE&s=indexed&type=Code

    ...and they wouldn't be able to utilize your code if it is MIT-licensed.

    opened by artob 5
  • error: Unrecognized option: 'lib'. while running the rustc command in Makefile

    error: Unrecognized option: 'lib'. while running the rustc command in Makefile

    I am not sure if this is a problem with the version of the rust compiler used or what not. PS I am running on a OSX machine.

    rustboot:$ make rustc -O --target i386-intel-linux --lib -o main.o -c main.rs error: Unrecognized option: 'lib'. make: *** [main.o] Error 101

    rustc --version rustc 0.10-pre (9e89ffc 2014-03-16 14:11:26 -0700) host: x86_64-apple-darwin

    Also I believe I have the latest version of the rustboot code.

    rustboot:$ git pull Already up-to-date.

    opened by jonnadul 3
  • Not a code issue, just a question

    Not a code issue, just a question

    #[allow(ctypes)];
    #[no_std];
    #[no_core];
    
    mod zero;
    
    enum Color {
        Black       = 0,
        Blue        = 1,
        Green       = 2,
        Cyan        = 3,
        Red         = 4,
        Pink        = 5,
        Brown       = 6,
        LightGray   = 7,
        DarkGray    = 8,
        LightBlue   = 9,
        LightGreen  = 10,
        LightCyan   = 11,
        LightRed    = 12,
        LightPink   = 13,
        Yellow      = 14,
        White       = 15,
    }
    
    fn range(lo: uint, hi: uint, it: &fn(uint)) {
        let mut iter = lo;
        while iter < hi {
            it(iter);
            iter += 1;
        }
    }
    
    unsafe fn print_screen(i: uint, ch: u8) {
        *((0xb8000 + i * 2) as *mut u8) = ch;
    }
    
    unsafe fn set_color(i: uint, foreground: Color, background: Color) {
        *((0xb8000 + i * 2 + 1) as *mut u8) = ((background as u8) << 4) ^ (foreground as u8);
    }
    
    unsafe fn clear_screen(foreground: Color, background: Color) {
        range(0, 80*25, |i| {
            set_color(i, foreground, background);
            print_screen(i, 0)
        });
    }
    
    fn is_char(ch: u8) -> bool {
        return (ch >= 97 && ch <= 122) ||
               (ch >= 65 && ch <= 90);
    }
    
    fn is_whitespace(ch: u8) -> bool {
        return (ch == 32);
    }
    
    pub fn len(s: &str) -> uint {
        let mut i: uint = 0;
        while is_char(s[i]) {
            i += 1;
        }
        return i;
    }
    
    unsafe fn print_num(number: uint)
    {
        let mut j = 0;
        let mut i = number;
        while i != 0 {
            let sym = i % 10 as u8;
            print_screen(j, sym + 48);
            j += 1;
            i = i / 10;
        }
    }
    
    #[no_mangle]
    pub unsafe fn main() {
        let string: &str = "Hello World";
        let length = len(string);
        clear_screen(White, Blue);
        range(0, length, |i| {
            print_screen(i, string[i] as u8)
        });
        print_num(length);
    }
    
    

    This program should return 5ello, first printing 'Hello' and then printing '5'. However, it just prints 5, if I change the length variable to 5, it will print out 5ello. I am confused at what is happening, maybe someone with deeper asm knowledge could explain?

    opened by txdv 3
  • Fixed to compile with rustc 0.13.0-nightly 2015-01-02

    Fixed to compile with rustc 0.13.0-nightly 2015-01-02

    This pull request has the same content as pull request #27 but is from a different branch since I want to use the master branch in my fork for further development. Sorry about the confusion!

    opened by thesam 1
  • Link to pczarn's fork

    Link to pczarn's fork

    Hey, Could you mention pczarn's fork of rustboot, at https://github.com/pczarn/rustboot, on your README? It's still under active development, yet it's overshadowed in the search results by this repository.

    opened by alexchandel 1
  • Trying to execute code outside RAM or ROM at 0x000a0000

    Trying to execute code outside RAM or ROM at 0x000a0000

    After building, it fails to run in QEMU on OSX

    NASM version is 2.10.07 QEMU version is 1.4.1 rustc version is 0.6 (a394298 2013-05-30 10:37:46 -0700)

    nasm -o loader.bin -f bin loader.asm
    nasm -f elf32 -o runtime.o runtime.asm
    rustc -O --target i386-intel-linux --lib -o main.o -c main.rs
    warning: missing crate link meta `name`, using `main` as default
    warning: missing crate link meta `vers`, using `0.0` as default
    i386-elf-ld -o main.bin -T linker.ld runtime.o main.o
    cat loader.bin main.bin > floppy.img
    qemu-system-i386 -fda floppy.img
    qemu: fatal: Trying to execute code outside RAM or ROM at 0x000a0000
    
    EAX=00007bc7 EBX=00007e20 ECX=00000000 EDX=00000000
    ESI=0000fe10 EDI=00000000 EBP=00007bf7 ESP=00007bab
    EIP=0009ffb0 EFL=00000002 [-------] CPL=0 II=0 A20=1 SMM=0 HLT=0
    ES =0010 00000000 000fffff 004f9300 DPL=0 DS   [-WA]
    CS =0008 00000000 000fffff 004f9a00 DPL=0 CS32 [-R-]
    SS =0010 00000000 000fffff 004f9300 DPL=0 DS   [-WA]
    DS =0010 00000000 000fffff 004f9300 DPL=0 DS   [-WA]
    FS =0010 00000000 000fffff 004f9300 DPL=0 DS   [-WA]
    GS =0010 00000000 000fffff 004f9300 DPL=0 DS   [-WA]
    LDT=0000 00000000 0000ffff 00008200 DPL=0 LDT
    TR =0000 00000000 0000ffff 00008b00 DPL=0 TSS32-busy
    GDT=     00007c91 00000019
    IDT=     00000000 00000000
    CR0=00000011 CR2=00000000 CR3=00000000 CR4=00000000
    DR0=00000000 DR1=00000000 DR2=00000000 DR3=00000000 
    DR6=ffff0ff0 DR7=00000400
    CCS=00007bc7 CCD=00007c28 CCO=ADDB    
    EFER=0000000000000000
    FCW=037f FSW=0000 [ST=0] FTW=00 MXCSR=00001f80
    FPR0=0000000000000000 0000 FPR1=0000000000000000 0000
    FPR2=0000000000000000 0000 FPR3=0000000000000000 0000
    FPR4=0000000000000000 0000 FPR5=0000000000000000 0000
    FPR6=0000000000000000 0000 FPR7=0000000000000000 0000
    XMM00=00000000000000000000000000000000 XMM01=00000000000000000000000000000000
    XMM02=00000000000000000000000000000000 XMM03=00000000000000000000000000000000
    XMM04=00000000000000000000000000000000 XMM05=00000000000000000000000000000000
    XMM06=00000000000000000000000000000000 XMM07=00000000000000000000000000000000
    make: *** [run] Abort trap: 6
    
    opened by takeiteasy 1
  • Unable to make

    Unable to make

    Collins-MacBook-Air:rustboot collinglass$ make
    nasm -o loader.bin -f bin loader.asm
    loader.asm:82: error: expression syntax error
    loader.asm:89: error: expression syntax error
    make: *** [loader.bin] Error 1
    
    opened by collinglass 0
  • Cannot build kernel

    Cannot build kernel

    When trying to build the kernel with v0.12 of the rustc compiler I recieve the error

    rustc -O --target i386-intel-linux --crate-type lib -o main.o --emit obj main.rs
    error: requires `sized` lang_item
    error: aborting due to previous error
    Makefile:13: recipe for target 'main.o' failed
    make: *** [main.o] Error 101
    

    I believe this has to do with changes to the language, and I have made changes which allow it to complile https://github.com/ragingSloth/rustboot/blob/39d3187f0f7e77501f58ae7b271dc27dab3035b8/main.rs

    opened by ragingSloth 1
  • Mutable static variables don't work

    Mutable static variables don't work

    Rust can't compile truly freestanding, position-dependent code. It attempts to access global variables through _GLOBAL_OFFSET_TABLE_.

    Currently it seems the best workaround is to compile emitted LLVM bitcode with clang -ffreestanding. However, optimizations made by clang could break things.

    opened by pczarn 0
  • main.rs is unable to compile

    main.rs is unable to compile

    When running make, it spits out this error message.

    rustc -O --target i386-intel-linux --lib -o main.o -c main.rs
    error: no item found for `ty_desc`
    error: no item found for `opaque`
    error: no item found for `ty_visitor`
    error: no item found for `freeze`
    error: no item found for `send`
    error: no item found for `closure_exchange_malloc`
    error: aborting due to 6 previous errors
    make: *** [main.o] Error 101
    

    It seems dependent on some part of the rust runtime? maybe?

    I'm running rustc version 0.7

    rustc 0.7
    host: x86_64-apple-darwin
    

    any tips you can give?

    opened by coder543 4
  • Not able to compile

    Not able to compile

    I followed the steps given on README.md After all the prerequisites I cloned the rustboot exported all the PATH $~ echo $PATH /home/sagar.sakre/binutils/i386-elf/bin/:/home/sagar.sakre/binutils/bin/:/home/sagar.sakre/rust-bin/bin/:/opt/qemu/bin/4.4.3/bin/:/opt/qemu/bin/:/home/nithin/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games

    I got error while make.. sagar.sakre@sagar-vm ~ $ cd rustboot/ sagar.sakre@sagar-vm ~/rustboot $ ls LICENSE.txt linker.ld loader.asm main.rs Makefile README.md runtime.asm zero.rs sagar.sakre@sagar-vm ~/rustboot $ make nasm -o loader.bin -f bin loader.asm nasm -f elf32 -o runtime.o runtime.asm rustc -O --target i386-intel-linux --lib -o main.o -c main.rs error: no item found for sized error: aborting due to previous error make: *** [main.o] Error 101

    can you please tel me where I am goin wrong?

    opened by sagarsakre 4
Owner
Charlie Somerville
Charlie Somerville
Experimental kernel for embedded devices written in Rust

bkernel is an experimental kernel for embedded devices written in Rust. I'm mostly trying out Rust now to see how it applies to kernel development. Pr

Alexey Shmalko 84 Dec 13, 2022
A new operating system kernel with Linux binary compatibility written in Rust.

Kerla Kerla is a monolithic operating system kernel from scratch in Rust which aims to be compatible with the Linux ABI, that is, runs Linux binaries

Seiya Nuta 3.1k Jan 1, 2023
Minimal x86_64 OS kernel written in Rust

rkernel A minimal x86_64 Rust OS kernel. Multiboot2 VGA driver PIC PIT PS/2 Keyboard driver PS/2 Mouse driver TSC RTC Allocator ATA PIO (In progress..

Divy Srivastava 36 Apr 26, 2022
🍒 Small, simple, and fast kernel written in Rust. 🌸

?? Small, simple, and fast kernel written in Rust. ??

Cherry Developers 5 May 20, 2022
Xrs is a POSIX-subset operating system kernel written in Rust.

XRS-OS ( ?? WIP) Xrs is a POSIX-subset operating system kernel written in Rust. Current project team members 0x5459 core developer (he/him) 0x5457 cor

null 7 Nov 16, 2022
Linux kernel modules written in Rust

Linux kernel modules written in Rust A collection of in-progress experimental Linux kernel modules written for the Rust for Linux project To run the o

Milan 10 Nov 13, 2022
Rux - An x86_64 toy operating system kernel written in Rust

Rux - An x86_64 toy operating system kernel written in Rust. Rux is a port of the Hux kernel, my x86 32-bit single-CPU toy kernel written in C, following the OSTEP book structure and terminology.

Guanzhou Jose Hu 6 Feb 26, 2022
An super minimal kernel written in rust

Grisha This project depends on this blog serie Philipp Oppermann's blog Required Knowlege I don't know what you really need to know to learn efficient

Ismail Ait Bella 3 Feb 3, 2022
Linux ABI-compatible kernel written in Rust

Linux ABI-compatible kernel written in Rust ??️ Screenshot (v0.1.0-alpha.1) ?? Build dependencies To compile GalaxyOS kernel and create basic OS ISO i

Krzysztof Stefańczyk 3 Dec 5, 2022
wait-free 4-level 64-bit pagetable for contiguous low-contention concurrent metadata

pagetable Wait-free 4-level page table that maps from a u64 key to an &AtomicU64 value. Page fan-out is 2^16. If a key doesn't exist, intermediate pag

Komora 21 Nov 20, 2022
Open Source Rust kernel; Runs WASM and WASI as lightweight containers.

?? etheryal Kernel etheryal kernel is an Open Source capability-based Kernel written in the Rust programming language. The kernel allows implementing

null 32 Dec 4, 2022
Basic Rust kernel using Limine

Rust Limine Barebones This is a small kernel that boots using Limine. Build First of all, download Rust ! (I guess you already did it if you are here

Quentincestino 16 Dec 23, 2022
Examples on how to write Windows kernel drivers in Rust

windows-kernel-rs Note: this is still work in progress! This is a Windows kernel framework in Rust that consists of windows-kernel-sys, a crate that p

S.J.R. van Schaik 77 Dec 28, 2022
An x86-64 kernel with ~100% Rust (originally) in a week

litchi-rs An x86-64 kernel with ~100% Rust (originally) in a week. The continuation of Litchi. Try it Make sure the Rust toolchains and qemu-system-x8

Bugen Zhao 38 Oct 11, 2022
Kernel density estimation in Rust.

kernel-density-estimation Kernel density estimation in Rust. Kernel density estimation (KDE) is a non-parametric method to estimate the probability de

Seaton Ullberg 16 Jan 16, 2023
💻 An x86_64 kernel in the works

BruhOS a basic x86_64 kernel in the works. cool stuff written in rust boots with any stivale2-compliant bootloader framebuffer bitmap font renderer pm

Safin Singh 9 Jun 8, 2021
The official kernel for Popcorn OS, and operating system designed for handheld devices.

About Popkern is the kernel for Popcorn OS, an operating system designed for handheld devices. As such, the kernel is (to be) optimised at all levels

Team Scena 3 Sep 19, 2021
The kernel for LibertyOS.

This is the official repository of the LibertyOS kernel. LibertyOS is an operating system, built with Rust, that is open-source, free-to-use, and open to new contributors.

null 229 Dec 5, 2022
A custom kernel for educational reasons

A custom kernel for educational reasons

TornaxO7 16 Dec 25, 2022