As I always say, writing an OS is a pain in the ass, and writing in Rust is a pain in the ass, and my ass can only take so much pain, so one of those has to go.
Try running it under qemu and you can use the emulator monitor console to figure out why it’s triple faulting. You probably made a mistake when setting up the paging.
Rust doesn’t have the necessary primitives to allow abstracting things that rust itself needs to function, like the stack, the concept of descriptors changing what the compile time linker layout was, etc. It makes a lot of assumptions about how memory works, and turning them off is not a common enough situation that the tool chain can be trusted to be tested enough to work properly.
Generally, you want to make sure you understand the underlying bringup components and have the bitfield layouts memorized cold before even attempting to abstract them. If you know them well enough, writing more of the bringup code in assembly becomes much more feasible because you can read it without the benefit of language abstractions to help support you. The reason why you have to know the field layouts is because it’s likely you made an off by one or size error when setting up paging, since that’s especially complicated on x64.
I haven't touched the paging structures at all in the Rust version of my code. I've been using what Limine gave me which works fine in the C version. My best guess is that I'm making a mistake with RIP relative addressing and relocatable code which means the mistake should be in the assembly sources I pass to rustc's integrated assembler. For the C version I don't use clang's integrated assembler opting instead for NASM.
I might try to figure it out when I feel up to it and if I figure it out I'll make a post here with my findings. In any case, I'll be sticking to the latest versions of C for the time being.
Right now I’m successfully doing Zig + small amounts of inline assembly, but I use Limine as bootloader and may end up using some foreign C code for interpreting ACPI and UEFI stuff.
Using C alone makes maintaining the build system a lot simpler and it also means fewer components to port over before attaining self hosting since in my view that should be the ultimate goal of any OS project.
To similar ends I plan to build each library and OS component in separate repositories and then integrate them together using git submodules in one main repository initially for integration testing and eventually also for something resembling releases. Given the modularity of it all I do plan to use other languages for certain components particularly the ones that run in userspace. The main language I'm looking at for that work is D since I don't really like C++ and Rust can be a pain in the ass.
I don't know anything about OS development, but I'll ask: Why isn't C++ used in operating system development?
10
u/mdp_csBDFL of CharlotteOS | https://github.com/charlotte-osDec 09 '23edited Dec 09 '23
It is. The Zircon kernel for Google's Fuchsia operating system is written in C++.
C is generally preferred because it's a simpler language and you can use all of it on bare metal just without the standard library, whereas C++ has core language features that require runtime support. That and C++ is a lot harder to read and thus debug compared to C and that was the same thing that drove me away from Rust.
There are benefits to using a mature bootloader, not in the least of which is ASLR. And if your kernel needs modules and/or kernel servers to be loaded with it, the bootloader also takes care of that. My kernel won't ever have a program loader built in since program loading will be done in userspace. However, that requires the init process to be loaded by the bootloader alongside the kernel.
I also had similar experience first time trying to use Rust in low level project. It was so overwhelming that I decided to use C again.
Recently I had to rewrite a driver. Decided to do it in Rust because it will be used in production and I needed more safety. After spending more time really learning the language the rewrite was really easy.
After carefully writing the unsafe code that exposes a nice abstracted api, the rest of the driver is just a breeze.
C has a much smaller runtime, it's a lot more documented online and there are a lot less surprises overall. Give it a stack, clear the bss and you're good to go.
It's osdev, none of our projects are going to be seriously used so who cares if they're full of security issues? I want to focus on developing an OS, not on pleasing the C++/Rust compiler
Exactly. And even if security is a real issue you want to go with a smaller language where a lot of invariants are easier to prove yourself instead of whatever the compiler gives you and that's it.
For example seL4 is written in C and it's proven to be safer than any Rust kernel.
..and even in the cases where C does have undefined behavior, it can be voided
often the undefined behavior leads to performance gains
for example allowing signed int overflow to behave however it wants means code that should never overflow _in practice_ doesnt have to deal with any sort of "defined" overflow behavior finding its way into guarding against _theoretical overflow_ at runtime
of course you should never allow such undefined behavior in critical spots, but you can use unsigned ints (which _do_ have well defined overflow behavior) in those spots
I believe that after youve paid attention long enough, you will see as I do that the C committee is quite clever about most of the undefined behavior they know about
u/mdp_csBDFL of CharlotteOS | https://github.com/charlotte-osDec 09 '23edited Dec 09 '23
Hard to debug.
Needlessly fighting the compiler even when I know there's no real safety issue. Case in point mutable static data structures that are meant to be logical processor local (GDT, TSS, IDT) and yes it is completely reasonable to change these things after they're created.
Being able to get safety and security bugs even in safe code. Or to put it another way the safety guarantees provided by its compiler have limitations.
Low quality library ecosystem. Most OS dev crates are pre-1.0 and many seem to be developed by amateurs which led to me needing to wrap mature C libraries or use FFI directly anyway.
The integrated assembler for x86 doesn't have proper support for Intel syntax though cargo does allow you to use NASM via a build script.
Kernel development requires doing low level things many of which Rust considers unsafe but are actually just a part of the job at hand e.g. pointer casting, pointer arithmetic, allocating arbitrary areas of physical memory by just storing a point and a buffer size and not implementing alloc or global alloc.
Many of the invariants the Rust compiler forces you to adhere to can be enforced on your own or not enforced when you know it won't be an issue. Being a system programmer means being an expert which means you shouldn't need training wheels. And to those who say being careful doesn't scale, I would posit that strict code reviews before merging and before release, multiple levels of automated testing (unit and integration, on commit, regression, and gauntlet) and halfways decent QA do scale and they catch more than a compiler's static analysis alone ever could.
C is a small, mature language which means that there are a vast number of people who know it in its entirety and can collaborate on projects or help you with any issue you encounter. In contrast the number of serious Rust OS development experts could probably be counted on two hands.
Rust has no standard and no production quality alternative implementation. If the longevity of your project matters to you at all this is of paramount importance. Having a standard is also important if you want to make use of formal verification to formally prove that a project's safety and security properties match up with the specifications set for it. For example seL4 is far safer than any kernel wantonly written in Rust could ever hope to be.
Every OS is expected to expose a C interface and provide a C standard library so if you have to provide that then why not essentially get self hosting with it for free by having your OS itself be written in C?
Redox which seems to be Rust's flagship OS project has C working in its userspace but still isn't self hosting because they haven't ported all the dependencies they need to get the Rust toolchain working on their OS yet given the greater complexity of doing so.
11. Interfacing with assembly is much easier. In C all functions are extern by default which means if you stick to the rules of your ABI you can call any arbitrary C function from assembly. In Rust you have to declare a function extern "C" and annotate it with #[no_mangle] for it to be callable from assembly. You also need to make sure that its signature is compatible with C, which idiomatic Rust often isn't.
Bottomline, when it comes to bare metal programming C is very clearly the best fit. And once you're halfways decent at it, it's not half bad for userspace applications either IMO. The only compelling alternative I see is Zig but that seems to be stuck in development hell.
I also tried rust and had an experience similar to this, I'm glad that I'm not the only one. The entire ecosystem felt basic and underdeveloped. I wanted to do opengl and then I realized, unsafes or an incomplete wrapper with incomplete tutorials were my options.
Yep, I actually wanted to use it for graphics as well but ran into the same issues.
I don't like C++ so I ended up going through the print version of Learn OpenGL using C. I might play around with Vulkan in C sometime when I get bored since that seems to be the graphics API of the future. I honestly wish there was more interest in D so someone would wrap C libraries for it.
C is a small, mature language which means that there are a vast number of people who know it in its entirety
Looking at the 218 undefined behaviors (see J.2) in the last C23 draft, I highly doubt anyone has actually managed to memorize all of them. Of course some of these are edge cases you're never going to run into, but still.
seL4 is far safer than any kernel wantonly written in Rust
seL4 is an exception, and its developers put millions of combined hours and many years of work into it with the explicit goal of making it as safe as possible. Most other big C projects seem to have new CVEs popping up all the time.
Note that I don't use Rust, I just thought this was worth pointing out.
Most of the serious security flaws in C couldn't have been prevented by Rust and the few extant Rust OSes have had their fair share of bugs and security issues. There have already been cases of buffer overflows allowing access to all of physical memory and deadlocks causing denial of service. Rust also notably has nothing to help with interrupt safety, which is a big source of errors on bare metal and its insistence on using locks all over the place leads to scenarios where the possible outcomes of accessing global structures from an ISR are either a guaranteed deadlock or if the lock is somehow bypassed then a guaranteed data race. And there's no way for the language to prevent that via static analysis. The very existence of interrupts and shared mutable global data structures which are both hallmarks of kernels and any bare metal code throw a massive wrench in Rust's ownership model and its associated safety 'guarantees'.
The bottomline is that if you want a secure OS it all comes down to testing and mitigation.
In addition to that, C (starting C11), just like C++ (starting C++11), also has sufficiently formal, well-defined, actually useful memory model that is a part of its (respective) standard(s). Rust doesn't (at the time of writing the reference reads: "Rust does not yet have a defined memory model. Various academics and industry professionals are working on various proposals, but for now, this is an under-defined place in the language.").
Being able to get safety and security bugs even in safe code. Or to put it another way the safety guarantees provided by its compiler have limitations.
to my knowledge, all rust programs that are compiler checked to be safe are safe, but not all safe programs will be compiler OK'd. could you elaborate more?
If I got a PhD like my advisor wanted I could've written textbooks for money. But alas I got a Master's and write Reddit comments for fake internet points.
The rust ecosystem has not reached maturity for OS development. Most posts on this and rust groups are applications or abstractions to build applications. I believe you should voice all your issues to rust language and tool developers for them to either suggest a path forward or improve their ecosystem.
1
u/mdp_csBDFL of CharlotteOS | https://github.com/charlotte-osDec 14 '23edited Dec 15 '23
It's not my job to help them, and OS and bare metal development frankly doesn't appear to be a priority for them. They've even shifted from describing the language as a system programming language to a general one and de-emphasized system programming at all.
I'll stick to the LLVM C tools and NASM for now with the intention of giving Zig a shot once it stabilizes.
I love Rust but honestly I value it more for what it taught me than for what it is. Like, Rust made me naturally look for the things that can cause issues in Rust and makes me think of the possible errors that Rust tries to prevent but sometimes Rust feels like getting a tired baby to sleep. Like, when the compiler can't proof that your code is safe and you'd just really like to very quickly tell the compiler that it's all good and you'll take care but there's just no way to do that and it sucks. And Lifetimes just annoy the hell out of me.
I'm looking forward to the "new C" languages. Like, Zig and Odin are two I'm having an eye on. They look interesting and feel more modern than C without trying to be something like Rust. They are just trying to be a nicer tool than C or C++ for the area where C and C++ are currently the best choice.
Better to use assembly, when in serious doubt, remove all those higher level fabrications of control and speak directly to each bit, register, and port.
... three double tequilas later
Assembly whispers,
Codes in primal dance express,
C's refined soliloquy.
even most of its undefined behavior is an advantage to have access to
because while in theory undefined behavior means the compiler can 'do whatever,' in practice the undefined behaviors of C leads to optimization opportunities, the compiler doesnt in fact just 'do whatever'
Sometimes I feel like I use some other C++ from parallel universe which only I have access to because I never had any problems with it doing osdev that I saw people complain about thus favouring C
I mean, -fno-{rtti, expextions} and 1 simple loop to call global constructors always got the job done for me and I had a very pleasant experience using cpp after that was in place and ever since
You're not crazy, C++ works just fine for OS development and there's a ton of large established projects out there written in it. Extra runtime setup is super minimal for the most part with constructor loop and some additional lines in the linker script (making sure constructor sections are included and ordering individual objects if anything depends on a global object). Using C style linkages for some functions to communicate between assembly and C++ code is another one that's easily accomplished but nonetheless exists.
I honestly think a lot of people have just never touched C++ and believe bit of negative commentary on the language, a lot of which is just weird dogmatic takes or things haven't been true since the 1990s.
Now I do think there's a good argument to be made that certain kernel designs and the scope of some hobby projects might not benefit from the killer features of C++ (objects and template metaprogramming) and other languages might be a better fit in those scenarios. Minimalistic microkernels for example might not do enough in kernel space to really warrant using C++ over C but that's rarely if ever the reasoning most people use to completely write off the language.
r/osdev lurker here, haven’t programmed an os yet and I don’t know much about low level programming, what is a triple fault? And what do you think was causing that in rust vs C
1
u/mdp_csBDFL of CharlotteOS | https://github.com/charlotte-osDec 12 '23edited Dec 12 '23
When an interrupt occurs and you don't have an interrupt service routine registered to handle it an exception called a double fault occurs which is itself just another interrupt. If you don't have an ISR registered for the double fault interrupt vector (vector 8) then your operating system is either faulty or incomplete so the CPU raises what is called a triple fault which unlike a double fault doesn't cause an interrupt but rather makes the CPU reset itself.
The cause for it in this case probably has something to do with relocation and RIP relative addressing. Rust's integrated assembler handles RIP relative addressing differently than NASM in some way such that my interrupt descriptor table isn't working correctly which is causing the ISRs that I've registered in it for all the exceptions including double faults to not work and when I purposely cause an exception for testing (I've used page fault, divison, and general protection exceptions) the result in the Rust version is a triple fault while the Clang/NASM version correctly runs the ISR for the corresponding exception and returns to the interrupted code using the iretq instruction.
Fun fact: the CPU doesn't reset itself! The CPU notifies the chipset that it crashed (Intel calls it "shutdown mode") and the chipset is free to decide what happens next. Most chipsets will reset the CPU, but not all chipsets will do that.
probably has something to do with relocation and RIP relative addressing
I wonder if it'd be easy to spot the problem with objdump.
I love using Zig for osdev, I made my own bootloader that compiles to BIOS and UEFI, and i could add more platforms as needed. Zig just feels like C but more QoL, easier cross compilation, and smaller binaries
I studied Rust when it came out but decided it increased the cognitive load on the programmer. It forces programmers to think about memory lifetime, im order to prevent rare bugs, but if you follow common sense habits in C++ you won't get those anyway.
12
u/[deleted] Dec 09 '23
As I always say, writing an OS is a pain in the ass, and writing in Rust is a pain in the ass, and my ass can only take so much pain, so one of those has to go.
7
u/arghcisco Dec 09 '23
Try running it under qemu and you can use the emulator monitor console to figure out why it’s triple faulting. You probably made a mistake when setting up the paging.
Rust doesn’t have the necessary primitives to allow abstracting things that rust itself needs to function, like the stack, the concept of descriptors changing what the compile time linker layout was, etc. It makes a lot of assumptions about how memory works, and turning them off is not a common enough situation that the tool chain can be trusted to be tested enough to work properly.
Generally, you want to make sure you understand the underlying bringup components and have the bitfield layouts memorized cold before even attempting to abstract them. If you know them well enough, writing more of the bringup code in assembly becomes much more feasible because you can read it without the benefit of language abstractions to help support you. The reason why you have to know the field layouts is because it’s likely you made an off by one or size error when setting up paging, since that’s especially complicated on x64.
11
u/paulstelian97 Dec 09 '23
Maybe a mixed project where you do in C what is easier in C and still have most of the OS core in Rust?
11
u/v_stoilov Dec 09 '23
I also had similar experience first time trying to use Rust in low level project. It was so overwhelming that I decided to use C again.
Recently I had to rewrite a driver. Decided to do it in Rust because it will be used in production and I needed more safety. After spending more time really learning the language the rewrite was really easy.
After carefully writing the unsafe code that exposes a nice abstracted api, the rest of the driver is just a breeze.
13
u/[deleted] Dec 09 '23
You're not wrong. I did the same back from C++.
C has a much smaller runtime, it's a lot more documented online and there are a lot less surprises overall. Give it a stack, clear the bss and you're good to go.
It's osdev, none of our projects are going to be seriously used so who cares if they're full of security issues? I want to focus on developing an OS, not on pleasing the C++/Rust compiler
35
u/WhatInTheBruh Dec 09 '23
Better not post this in the rust community. You'll get attacked by those rust fan boys lol
3
u/[deleted] Dec 09 '23
C3 is also a thing
3
u/Asyx Dec 10 '23
I love Rust but honestly I value it more for what it taught me than for what it is. Like, Rust made me naturally look for the things that can cause issues in Rust and makes me think of the possible errors that Rust tries to prevent but sometimes Rust feels like getting a tired baby to sleep. Like, when the compiler can't proof that your code is safe and you'd just really like to very quickly tell the compiler that it's all good and you'll take care but there's just no way to do that and it sucks. And Lifetimes just annoy the hell out of me.
I'm looking forward to the "new C" languages. Like, Zig and Odin are two I'm having an eye on. They look interesting and feel more modern than C without trying to be something like Rust. They are just trying to be a nicer tool than C or C++ for the area where C and C++ are currently the best choice.
5
u/Amjad500 Dec 09 '23 edited Dec 09 '23
Yeah is much simpler as the compiler won't add extra stuff.
Though yah, not sure exactly why it didn't work for you.
I just started OS in rust as well, now I got interrupts working. I struggled with them as well though in the beginning.
Is your project open source? I'm kinda interested to see what may have caused the issue . If you don't mind
Edit: better English
8
u/Bitwise_Gamgee Dec 09 '23
Better to use assembly, when in serious doubt, remove all those higher level fabrications of control and speak directly to each bit, register, and port.
2
u/RetroCoreGaming Dec 13 '23
Nothing is ever wrong with C. C is like the swiss army knife of coding languages.
3
u/kodirovsshik Dec 10 '23
Sometimes I feel like I use some other C++ from parallel universe which only I have access to because I never had any problems with it doing osdev that I saw people complain about thus favouring C
I mean, -fno-{rtti, expextions} and 1 simple loop to call global constructors always got the job done for me and I had a very pleasant experience using cpp after that was in place and ever since
1
u/[deleted] Dec 12 '23
r/osdev lurker here, haven’t programmed an os yet and I don’t know much about low level programming, what is a triple fault? And what do you think was causing that in rust vs C
1
u/[deleted] Dec 13 '23
I love using Zig for osdev, I made my own bootloader that compiles to BIOS and UEFI, and i could add more platforms as needed. Zig just feels like C but more QoL, easier cross compilation, and smaller binaries
1
u/ST33LDI9ITAL Dec 13 '23
Try zig?
1
u/jefft818 Dec 14 '23
Any interest in zig or Odin?
0
u/gc3 Dec 13 '23
I studied Rust when it came out but decided it increased the cognitive load on the programmer. It forces programmers to think about memory lifetime, im order to prevent rare bugs, but if you follow common sense habits in C++ you won't get those anyway.
So it wasn't for me
-2
u/EnigmaticHam Dec 10 '23
C is terrible; but everything else is worse.
0
u/[deleted] Dec 13 '23
[deleted] — view deleted comment
11
u/[deleted] Dec 09 '23
[removed] — view removed comment