10 ms·
Writing an x86 bootloader in Rust that can launch vmlinux
- secondcoming 6y ago> "Although the slice and the target buf should be the same, the target is filled with 255 from some point. " Doesn't Rust prevent things like overwriting memory?
- zaarn 6y agoNot necessarily, there are a bunch of unsafe methods you kinda have to use going this low level and if you're in unsafe mode, then you can overwrite memory or do things to corrupt it.
- grayhatter 6y agoSo all all of rust's issues, and none of it's safety?
- zaarn 6y agoYou still get safety, if you write normal rust code then rust will largely behave the same. But it allows you to for example to dereference raw pointers (and by proxy, allows pointer math). A lot of unsafe functions become available, like unguarded array access (ie, you can go out of bounds of an array) but you have to explicitly do that.
- Ar-Curunir 6y agoNo, you only face this in the unsafe code; the rest of your code is still safe.
- johnisgood 6y agoNo, unsafe code may affect the rest of your code (safe code).
- zaarn 6y agoYou only have to audit the unsafe code for unsoundness though.
- uxp100 6y agoHonestly, many of rust’s safety promises don’t help the type of low level code I write, so I’m skeptical of the “embedded rust” push. But embedded is a huge field so some people will definitely benefit from what rust makes available. But yeah, I think for some projects rust brings you a great type system, a gross syntax, and very little additional safety. But OTOH, on those projects writing rust will be not that different than writing C. Either language will still let you forget to sanitize a length value from an external struct that is put into a DMA engine. And either language will let you create a dangerous_u32 type that needs to be sanitized before your routines write it into a peripheral. But rust’s probably would be a little more ergonomic for the programmers, a little harder to use improperly, and involve pressing every punctuation mark on your keyboard.
- mlindner 6y ago> Honestly, many of rust’s safety promises don’t help the type of low level code I write, so I’m skeptical of the “embedded rust” push. But embedded is a huge field so some people will definitely benefit from what rust makes available. Why exactly? Once you write an abstraction on top of the raw addresses for configuring the hardware (usually already provided by a library) you don't need to use unsafe at all. One of the first things you do in C is write macros for all the addresses you use, writing safe wrappers isn't that much more complex than that.
- uxp100 6y agoBecause nothing the C code is doing would be "unsafe" in rust terms. What is the advantage rust has for following an SDMMC controller init sequence?
- zaarn 6y agoOnce you define a safe interface, you only have to audit the implementation of that interface for errors that aren't guarded for in unsafe code. The safe interface you can then use without fearing memory corruption or having to audit that code for unsafety. Any safe code is pretty much only logic errors.
- Aengeuad 6y agoUsing unsafe doesn't disable the safety features that Rust has like ownership rules or type safety, it's an acknowledgement that undefined behaviour can exist and that it's the programmers responsibility to ensure that undefined behaviour isn't introduced into safe Rust. There's no real way around this, if you want to write to arbitrary memory addresses Rust can't magically prove that you're doing so safely but if you can prove you're doing so safely then Rust will take care of the rest and make sure you're not introducing data races, use after free, buffer overflows, etc.
- fortran77 6y agoNone of it is safety? I would think some of it safety, like strong type checking. But, you're right in that this isn't the primary use case of Rust.
- viraptor 6y agoThis is lower level than code you normally expect. The change seems to happen around switching addressing modes, so it could be something like pages descriptors not being set up correctly. In short - rust safe memory guarantees don't hold if you mess with memory mapping at runtime.
- varbhat 6y agoLooks nice. I personally don't use Bootloaders anymore , using UEFI directly to boot my kernel. Edit: It doesn't require any manual tinkering/maintainance even when updating. Also it boots fast. https://wiki.archlinux.org/index.php/EFISTUB https://wiki.archlinux.org/index.php/EFISTUB
- bestouff 6y agoHow do you do that ? I didn't know you could compile a kernel directly to an UEFI payload. Edit: Indeed it's possible with CONFIG_EFI_STUB=y : https://wiki.archlinux.org/index.php/EFISTUB https://wiki.archlinux.org/index.php/EFISTUB - thanks for the hint !
- mehrdadn 6y agoNote that just because you can, it doesn't mean you should. How much maintenance this needs depends on your distro, your machine and firmware's own quirks (if any), and your own usage/update/maintenance patterns/quirks.
- deleted 6y ago[deleted]
- bleepblorp 6y agoAlso: directly booting the kernel as a UEFI module makes it much more difficult to set kernel parameters temporarily on boot. If you need to boot into runlevel 3 (or use init=/bin/bash) for maintenance, it's going to be much more complicated to make this happen than it is when booting through Grub.
- josteink 6y agoIs it really? You can just boot into the UEFI Interaractive Shell (think of it as a BIOS terminal/cmd.exe), which most BIOSes have, and launch your kernel with those parameters, as regular arguments to a regular command, right? What's hard about that?
- non-entity 6y agoIs the site down right now? I can't seem to connect.
- merricksb 6y agoMust be swamped with traffic. Google cache works: https://webcache.googleusercontent.com/search?q=cache:QBPnreVXSYEJ:https://vmm.dev/en/rust/krabs.md+&cd=1&hl=en&ct=clnk&gl=au https://webcache.googleusercontent.com/search?q=cache:QBPnre...
- cpach 6y agoThat URL (to Google’s cached version) took about three minutes to load for me. Does anyone know why Google’s cache is so slow sometimes...?
- numpad0 6y agoClick “text only version”. Full version is just online fetch with extra header and often naturally fails to load
- gruturo 6y agoIt was still attempting to load images, which probably caused the slowness - there is an option in the cached page header to request a text-only version which will load immediately.
- stas2k 6y agoGithub repo has the same text in the README: https://github.com/o8vm/krabs https://github.com/o8vm/krabs
- 0x008 6y agoYes
- azhenley 6y agoFor those interested, a colleague of mine wrote a tutorial series on implementing an OS using Rust that targets RISC-V: https://osblog.stephenmarz.com/ https://osblog.stephenmarz.com/
- m463 6y agoI can't help but suspect the first item on the list was the whole reason for this project. :) Hardware initialization: ==> Setting the keyboard repeat rate. Disable interrupts and mask all interrupt levels. Setting Interrupt descriptor (IDT) and segment descriptor (GDT). As a result, all selectors (CS, DS, ES, FS, GS) refer to the 4 Gbyte flat linear address space. Change the address bus to 32 bits (Enable A20 line). Transition to protected mode. If the target is ELF64, set the 4G boot pagetable and transition to long mode.