
Bare Metal Rust 2: Retarget your compiler so interrupts are not evil - dbaupp
http://www.randomhacks.net/2015/11/11/bare-metal-rust-custom-target-kernel-space/
======
jscheel
I've been playing with Rust for os dev and emulation for a little bit. It's
great to have others who are significantly more well-versed in this field
sharing their knowledge with those of us who are struggling through it.

~~~
easytiger
With that memory model its not surprising.

On the other hand, i really really would encourage any new developers to read
the rust docs before learning c++ etc. It is amazing to have a language that
puts its mem model first. The docs tackle the very basics really well.

The k&r c book is amazing but did a poor job of dealing with the true memory
model because that was common knowledge at time of writing. Its short because
of assumptions

the best java (top 0.01%) developers are the only ones I encounter often who
understand the x86 + javas basterdised memory model

The rest just take it for granted. Even c++ developers (I'm one).

~~~
Manishearth
I actually doubt Rust puts the memory _model_ first, but it provides a path
through which you effectively don't have to worry about it at all. Which is
wonderful and good enough, really.

IIRC Rust has a similarly confusing underlying memory model. However, due to a
lack of direct shared mutable state being available, you don't get to see this
often. In fact, one might call Rust's model more confusing than C++ since you
have `noalias` everywhere which can enable more aggressive optimizations. But
it's mostly irrelevant since you don't deal with it unless you're writing
unsafe code.

On the "encourage new devs to read the Rust docs" front I agree, though. We've
had tons of people saying that they code better C++ after learning Rust. Also
I've heard of companies wanting to start programming in C++/Rust-y things with
a majority of python/ruby/js/etc devs use Rust because "Rust teaches a lot of
things to the programmers that they no longer have to". Something like that.

~~~
easytiger
Agreed. What i meant was they start talking about the memory model at the very
beginning of their online book, more than most languages bother with.

[https://doc.rust-lang.org/book/the-stack-and-the-heap.html](https://doc.rust-
lang.org/book/the-stack-and-the-heap.html)

~~~
steveklabnik
Yeah, this is because we want people who may not have a systems backround to
be able to use Rust. That's just general info on the concept of stack vs heap,
it's not Rust-specific.

------
br1
Can you just not use the first 128 bytes of the stack on a interrupt?

~~~
DSMan195276
No - Part of how interrupts work is that the CPU itself pushes the values of
certain registers onto the stack (As well as an error code in the case of an
exception). The CPU has to store those values _somewhere_ so that when the
interrupt is finished it can return to where the CPU was running when the
interrupt happened, and the stack is the obvious location to do so. You can't
tell the CPU to do anything different in this case, so you're forced to simply
make sure your stack pointer always points to the top of the stack so it
doesn't get clobbered.

At the end of the day, it's not really that big of a deal - Allowing the
redzone only really allows you to avoid two `sub` and `add` instructions on
the stack-pointer. It's a nice idea, but losing it isn't that big of a deal at
the end of the day. The majority of functions can't take advantage of the red-
zone anyway, because any function that calls another function has to make sure
its stack-pointer is correct before calling it.

~~~
br1
Understood. Thanks.

------
steveklabnik
There's been a lot of really neat stuff focusing on beginners in the Rust
OSDev space lately. Glad to see even more posts about it!

------
viraptor
I was wondering, would the monolitic/micro-kernel discussion be different
today due to userspace services being able to use all the fancy cpu
extensions, or would that still disappear compared to the cost of ring
switching? Now that filesystems are more complex databases, maybe extensions
could help with fast indexing/checksumming.

