Page 1 of 1

Kernel runtime error.

Posted: Sun Jun 23, 2019 2:35 pm
by alittlepotato
I'm coding an OS in Assembly and Rust.
I use my own bootloader assembled in NASM and I load the Kernel from floppy.
My problem is that my os crash when I increment a structure int variable or a public static (global) int variable.

Code: Select all

//A
pub fn display(&mut self, data: &str) -> u8{
    for byte in data.bytes(){
        // [...]
        self.column += 1; // for example it will crash here.
        if self.column == 80{
            self.newline();
            self.column = 0;
        }
    }
    return 0;
}
I first thought it was a problem of memory then I realised that I forgot to enable A20 line #-o but it still doesn't work.
The bootloader's steps (if I forgot something) :
Boot
Read kernel
Switch to PM,
Boot second stage
Enable A20
Switch Long Mode
Call kernel at 0x1000.
(I assemble the second stage as ELF64 and I link it to the Rust code (kernel) using LD then I link it to the first stage BIN file).
Sorry if I did basics mistakes I'm still learning.
Thanks.

Re: Kernel runtime error.

Posted: Wed Jun 26, 2019 10:53 am
by eekee
Um... I don't know anything about Rust specifically, but just looking at the wiki page, did you include the annotation to use only the core library? I'm wondering if it's a problem with missing runtime allocation, and guessing the compiler would catch it if you included the annotation. Of course, I could easily be waaay off target. :)

Looking at a few links, Rust seems really nice for building kernels, minimizing the fuss of getting the compiler and language to output appropriate code... except for floating point being a mandatory part of libcore. There's something wrong with everything! :D

Re: Kernel runtime error.

Posted: Wed Jun 26, 2019 12:24 pm
by Ethin
I'm surprised Rust is letting you do this at all (modifying a mutable static -- or even interacting with one -- is unsafe). Try storing all your contextual data (I'm assuming this is the VGA buffer) in a struct, then use the lazy_static crate to initialize it on its first use. (I'd highly recommend you also use the spin crate and lock a spinlock around it to prevent a data race.)
Finally, ensure your running on the nightly channel; OS Development requires a lot of unsafe and unstable features :). Some crates that will help you greatly:
x86_64 - allows you to use some asm instructions and read/write to some CPU registers
uart_16550 - serial port output (but not input)
volatile - ensures that volatile reads/writes to memory are not optimized away
spin - great, handy, useful spinlocks
pic8259_simple - simple interaction with the 8259 PIC (I still don't know how to use the APIC, am trying to get ACPI in haha)
pc-keyboard - decoding and processing of keys. (Don't use this as your sole keyboard processor though, build a keyboard driver -- its much better that way IMO)!
raw-cpuid - easy CPU identification
cpuio - CPU IO to ports
bit_field - easy manipulation of bit fields