5 ms·
Writing an OS in Rust: Handling Exceptions
- Sir_Cmpwn 10y agoPreviously: https://hn.algolia.com/?q=phil-opp.com https://hn.algolia.com/?q=phil-opp.com Just about every page of this tutorial has frontpaged on HN. I guess people really, really, really want more kernels written in Rust!
- adamnemecek 10y ago> I guess people really, really, really want more kernels written in Rust! Anyone who doesn't either doesn't kernel much or has Stockholm syndrome and is in denial.
- Sir_Cmpwn 10y ago"I must be right because anyone who disagrees with me is ignorant or insane"
- adamnemecek 10y agoCan you point out to me a single criticism of Rust that isn't "it's too immature" or a variation thereof (e.g. less code written in it)?
- Sir_Cmpwn 10y agoI don't necessarily disagree with you, just with the way you present your point.
- nickpsecurity 10y agoThat "it's too immature" is a great, dismissal line given it's incredibly important for choosing a language to write a safe/secure kernel in. My choice against it as main language would be due to lack of assurance technology made for it. With C subset and/or SPARK Ada, I have a pile of tech from formal specifications to static analysis to test generators to assembly certifiers. Add powerful IDE's & ecosystem to it. Then, even if people don't want that, there's regular compilers for what I produce on countless platforms for increased usage opportunities and extra security through hardware diversity/obfuscation. Rust doesn't give me all this. Not just "immature:" nobody working on most of it at all or with enough effort I know it will be there in 1-3 years when I need it. That said, I love and encourage your work on Redox plus these other Rust-based kernels. There's no reason to gripe about them that I see. The posts are good for learning like most kernel dev w/ extra language & safety content on the side. You Redox devs in particular are really rolling on it being safe & practically usable. There's just that the gap between what's achievable on high end with Rust and C/SPARK is the size of the grand canyon. Many PhD's worth of work.
- jeffdavis 10y agoHow about "it's too immature to properly criticize"? ;-)
- kibwen 10y agoWhat are your criteria for maturity? I tend to agree that Rust has a ways to go before it's capable of calling itself a world-class implementation, but lots of those aspects (e.g. IDE support) are on this year's roadmap and seeing active development.
- steveklabnik 10y agoHere's a good one in which Rust isn't yet mature enough in this space: this still requires nightly.
- jeffdavis 10y agoMy comment was not entirely sincere; I'm very optimistic about rust's future. The grain of truth is that, if you grant that rust is immature, then it's unfair to say that's the only criticism because a lot of problems can be hidden behind immaturity.
- frozenport 10y ago237 reasons https://www.reddit.com/r/cpp/comments/611811/have_you_used_rust_do_you_prefer_it_over_modern_c/ https://www.reddit.com/r/cpp/comments/611811/have_you_used_r...
- jeffdavis 10y agoIt's a way to learn about OSes and rust at the same time. It's also high-quality content.
- kibwen 10y agoI don't think Phil's goal is to produce a Rust-based kernel so much as it is to demonstrate some of the nitty-gritty details of how kernels work, while also just happening to use Rust (which provides an opportunity to discuss how many of those gnarly unsafe bits can be encapsulated behind safe interfaces, while others can't).
- glangdale 10y agoIs it a surprise? Rust claims to be a systems language - and what better proof of that than someone writing a kernel in it? To me this is a clear example of "doing what it says on the tin". Also, I view the fact that the code is startlingly readable and seems (to my untutored eyes) to look a lot like other Rust code as a pretty strong validation that they are delivering on their promise.
- pcwalton 10y agoOr maybe these tutorials are some of the best tutorials on low-level operating system design in any language. I've certainly learned a lot from them.
- milesrout 10y agoNo surprise that you get responses from the Rust Defence League. They downvote any criticisms of Rust here and on reddit, and upvote anything that mentions it in a positive light. They rush to its defence everywhere. I seriously reckon that steveklabnik and pcwalton must have a bot watching HN and reddit for mentions of Rust so they can immediately rush to its defence every time it comes up. It's a pity because I actually think the language has potential, but it's not going to be successful if they're incapable of taking any critique on board. And it's not ready, it's not mature. They have to accept that.
- wtetzner 10y ago> And it's not ready, it's not mature. They have to accept that. Do you have anything specific in mind that makes it "not ready"? And when you say it's not ready, what isn't it ready for?
- bluejekyll 10y agoI wish I had more spare time to help work on something like this. I've been reading all of these posts since the beginning, and they are all so excellent. I knew a lot of these things abstractly, but this explains it all in such solid detail, it's awesome. Thank you! Now a question for people in the know about micro-kernel architectures. One thing often claimed is that the message passing is too expensive, causing essentially double the work per system call. After reading this, I started wondering, and now want to read more about it: is it possible to use page faults and a handler to effectively build a message passing setup in the microkernel? Essentially using the pagefault as the mechanism to switch stacks from one kernel-service to another? Is this novel, stupid, or even faster than existing mechanisms? Obviously I need to go read how L4 and others do this. What's great about posts like this is that it makes me want to go learn about it...
- adamnemecek 10y ago> Obviously I need to go read how L4 and others do this. The L4 guy wrote a paper on it. https://www.cs.nyu.edu/~mwalfish/classes/15fa/ref/liedtke93improving.pdf https://www.cs.nyu.edu/~mwalfish/classes/15fa/ref/liedtke93i... I think I've been intrigued by the same idea, I believe that Rust enables you to build a reactive OS which would simplify a lot of programming. Given that interrupts are basically events, it's a match made in heaven. If someone wants to talk about this (by "this" I guess I mean the new OS architectures that are now possible due to Rust) more, shoot me an email (it's in my profile).
- mastax 10y agoYou may be interested in these projects: https://github.com/ryanra/RustOS https://github.com/ryanra/RustOS RustOS is hobby OS project that's pretty bare bones, but has similar ideas to what you've mentioned. It's essentially a port of the Rust standard library to bare metal, relying on the safety of the language rather than hardware features. It's sort-of a unikernel in implementation if not intention. https://github.com/helena-project/tock https://github.com/helena-project/tock Tock is an interesting academic project to create an embedded OS. It still uses more traditional hardware protection for user processes, since these can be written in any language. The kernel, however, is composed of cooperatively-scheduled components called capsules, which rely on the language for isolation and don't have the overhead associated with real processes. See this doc for more details: https://www.tockos.org/documentation/design/ https://www.tockos.org/documentation/design/
- phil-opp 10y agoWhy is this marked as dupe? Yes, there were already posts about exception handling ( "Catching Exceptions" [1]), "Better Exception Messages" [2], and "Returning from Exceptions" [3]). These posts used naked functions and a lot of inline assembly for exception handling. This is a new post that I published today. It uses the new `x86-interrupt` calling convention [4] instead of naked functions, which is much easier. [1]: https://os.phil-opp.com/catching-exceptions.html https://os.phil-opp.com/catching-exceptions.html [2]: https://os.phil-opp.com/better-exception-messages.html https://os.phil-opp.com/better-exception-messages.html [3]: https://os.phil-opp.com/returning-from-exceptions.html https://os.phil-opp.com/returning-from-exceptions.html [4]: https://github.com/rust-lang/rust/pull/39832 https://github.com/rust-lang/rust/pull/39832
- Animats 10y ago"x86-interrupt calling convention is such a calling convention" Language support for interrupts. That's convenient. Usually it takes assembly language glue code to save and restore CPU state.
- andreiw 10y agoOne thing that stands out over almost every other similar article is that there are alternative calling conventions available. That is - awesome. Not sure if this is something easily defined in a crate, or if it is intrinsic, but it is very liberating to know that you don't need to craft exception entry / exit code unless you really want to. That makes OS-level tinkering much much more approachable.