News for hackers. Stuff that matters.
Re:Writing an OS in Rust by dang on Tuesday December 29, 2015 @02:08PM (#10808412)
Re:Writing an OS in Rust by VeilEm on Tuesday December 29, 2015 @01:36PM (#10808225)
I recently got started working with Rust and even though I have pretty decent programming experience in statically typed languages Rust has been quite a challenge. The borrow checker learning curve is very high. Even if you understand the basics you won't always know what macros and functions will be moving things until you run into an error. Lifetime management also requires a bit of time to grok. Some things that are simple in almost any other language can be frustrating in Rust, like someone wrote a book for how to write a linked list in Rust.[0]

My advice is that when you're getting started with Rust, compile often. Don't write a tome of code only to find out later it has no chance of compiling. It gets easier the more you do it, so don't give up, but move in small steps.

[0] http://cglab.ca/~abeinges/blah/too-many-lists/book/

Re:Writing an OS in Rust by nightpool on Tuesday December 29, 2015 @01:55PM (#10808342)
to be fair, that book covers 6 different types of linked lists, not just "how to write a linked list". I could write a similarly-sized book (by word count) on different types of linked lists in C++
Re:Writing an OS in Rust by VeilEm on Tuesday December 29, 2015 @02:04PM (#10808384)
Writing a linked list is pretty hard in Rust for someone new to the language. Perhaps this wasn't as obvious from the book's introduction as I thought it would be.
Re:Writing an OS in Rust by bluejekyll on Tuesday December 29, 2015 @01:02PM (#10808017)
So there is this, and there is also http://www.redox-os.org. Are there any other OS' being built in Rust?

I'm curious about the intention of these two projects, this seems to be a bottom up OS in Rust probably oriented toward unikernel or possibly a server OS. To be successful I assume it's going to allow for NetBSD or Linux drivers to be integrated into the Kernel in some way?

Re:Writing an OS in Rust by cmrx64 on Tuesday December 29, 2015 @01:19PM (#10808124)
Me and some other people are working on a seL4-based OS in Rust. We're getting ready to publically announce our first milestone by next week.

There's also https://github.com/thepowersgang/rust_os, which is one of the oldest OS written in Rust, but never gets any press.

Re:Writing an OS in Rust by phil-opp on Tuesday December 29, 2015 @08:35PM (#10810233)
Yeah, thepowersgang's kernel is severely underrated.
Re:Writing an OS in Rust by bluejekyll on Tuesday December 29, 2015 @06:41PM (#10809826)
Sweet! I love the L4 design.
Re:Writing an OS in Rust by valarauca1 on Tuesday December 29, 2015 @01:19PM (#10808127)
/u/steveklabnik1 is writing one https://intermezzos.github.io/
Re:Writing an OS in Rust by drzaiusapelord on Tuesday December 29, 2015 @01:47PM (#10808303)
> I assume it's going to allow for NetBSD or Linux drivers to be integrated into the Kernel in some way?

How is that possible? An intermediate layer? The kernel is going to be written in rust. You can't just put C code in there and expect it to work. I'm not even sure how you could transcode these drivers into rust considering rust uses a completely different memory model. I suspect a 'real' RustOS will require new drivers to be written from scratch. From a mass adoption perspective, how would have linux worked out if Linus decided to write windows driver shims?

I wonder if its feasible to rewrite things like apache, nginx, php, mysql, gnu utils, etc in Rust but keep the linux kernel as-is. Seems like you'd get the best of both worlds there - a mature and powerful kernel and a Rust userland that's hard to exploit. Most exploited vulnerabilities and stability issues aren't kernel related anyway.

Mozilla's servo project is also interesting. Maybe I don't need a RustOS (yet), but just my internet facing applications in Rust.

Re:Writing an OS in Rust by polyfractal on Tuesday December 29, 2015 @01:49PM (#10808318)
You can do what Rump kernels do: write a shim layer. The linux/netbsd drivers think they are talking to their host kernel, when in reality they are talking to a shim that translates linux syscalls into the equivalent "RustOS" syscalls.

Or you could just implement the linux syscalls directly in the kernel, but that seems a bit self-defeating if you want to explore new ideas.

Re:Writing an OS in Rust by bluejekyll on Tuesday December 29, 2015 @02:04PM (#10808388)
The reason this seems valid, is that it takes a long time to get the full system of device drivers you need to run on anything more than just a single platform.

Just seems like a good starting point than reworking the entire world.

Also, on the question of "how" you'd do this, I'm not a kernel hacker, but I thought it would be possible to expose FFI, i.e. the shim you talk about, that would allow drivers to be used from something like NetBSD. But you're right that it could be self-defeating in that supporting all of those interfaces might end up constraining the OS in it's attempt to deliver on these features.

On a side note, it seems like it would be nice to expose POSIX FFI hooks to make porting software to a Rust OS easier.

Re:Writing an OS in Rust by polyfractal on Tuesday December 29, 2015 @02:22PM (#10808491)
Agreed, that's why I personally like the rump kernel idea. Pick and choose the various NetBSD drivers that you need, automatically gain battle-tested drivers for a wide variety of devices. Bonus points that they are POSIX-compliant which helps with porting code.
Re:Writing an OS in Rust by drzaiusapelord on Tuesday December 29, 2015 @01:59PM (#10808364)
Is this feasible for a production server or just POC? Its worrying to think of a layer between the OS and its video driver, raid controller driver, sound driver, network driver, etc. Any real decrease in performance or stability will be a non-starter for most.
Re:Writing an OS in Rust by polyfractal on Tuesday December 29, 2015 @02:20PM (#10808475)
Well, that's how all the NetBSD rump drivers work. IIUC, a while back they refactored all the drivers to interact with the NetBSD kernel through the Rump interface. Which means that all the drivers can work in a traditional monolithic kernel, or can be extracted and used piecemeal in your own kernel, or compiled directly into the application as a unikernel. Checkout http://rumpkernel.org/ for more info.

I dunno about performance impact, but I bet it is fairly negligible in the grand scheme of things. In the rump kernel case, these shims really don't do anything, they just pass the call through. Think of it as an API facade. In a hobby OS it might be heavier, if your OS doesn't have a 1:1 mapping of the POSIX/linux syscall to an equivalent.

Also, we virtualize this stuff all the time in cloud environments. Your java app uses the JVM to execute a linux syscall which the kernel translates into a Xen hypercall which Xen uses to call a native driver which talks to the disk, etc etc. You obviously do pay a performance penalty for stacking more and more layers, but it isn't as bad as you'd think (or as good as you hope, take your pick).

But the point is that layering abstractions like this is very much alive and tolerated today in moderately high performance systems :)

Re:Writing an OS in Rust by steveklabnik on Tuesday December 29, 2015 @09:00PM (#10810303)
Rumprun already has Rust support.
Re:Writing an OS in Rust by kg6cvv on Tuesday December 29, 2015 @01:15PM (#10808107)
I spent a bunch of time figuring out how to get to long mode in a multiboot kernel. Finally got it working and discovered that a 64 bit UEFI app starts in long mode. Is it really so important to maintain support for non-UEFI x86-64 systems?
Re:Writing an OS in Rust by legulere on Tuesday December 29, 2015 @02:34PM (#10808554)
My guess why it's done:

BIOS booting is still standard in all Virtual Machines and UEFI might even be a pain in the ass to set up. This is where in the beginning your OS will run most of the time.

Also UEFI has it's own pitfalls (the main function you have to provide and the UEFI functions you can use use the windows calling ABI).

If you were to remove BIOS booting from an operating system that's actually used you would get lots of complaints from people that a) have a system that doesn't support UEFI b) are too lazy to switch on UEFI on their mainboard and c) the most vocal group probably would be people that hate UEFI

Re:Writing an OS in Rust by phil-opp on Wednesday December 30, 2015 @11:34AM (#10812774)
Besides not liking UEFI, I'm open to support it as well. Do you know some good resources?
Re:Writing an OS in Rust by leonardinius on Tuesday December 29, 2015 @01:05PM (#10808039)
Reminds me of https://github.com/intermezzOS/kernel

Do they work in parallel on same problem/same style/ veru similar codebases? When why not to combine efforts?

Re:Writing an OS in Rust by polyfractal on Tuesday December 29, 2015 @01:09PM (#10808075)
If I understand correctly, Phil started writing his series first. That tutorial prompted several people to start working on their own hobby OS, including Steve's and another one here: http://www.randomhacks.net/bare-metal-rust/. I think Redox was already underway when Phil started writing, but I may be mixing up my timelines.

Edit: There is also http://www.alexeyshmalko.com/2015/bkernel-a-rust-operating-s...

Phil has stated his series is basically for fun and learning. I imagine Steve's and Eric's are similar. You don't necessarily want to "combine effort" with someone when you are exploring ideas on your own.

Re:Writing an OS in Rust by leonardinius on Tuesday December 29, 2015 @01:29PM (#10808182)
I mostly agree with you.

However part of it's effort is directed into producing tutorials/book material for the educational etc use.

It's obviously up to 100% to them how and where are they going.

From the educational perspective it's _imo_ something worth considering at some point. E.g. combining this collective know-how into some sort of book. I know I would buy it.

Re:Writing an OS in Rust by steveklabnik on Tuesday December 29, 2015 @09:03PM (#10810311)
We are all very friendly with each other, and hang out in our IRCs, and #rust-osdev. I expect the situation to be better for all of us, as we can cross-pollinate, and improve each other's work. Ecosystems can be better than single monolithic projects.

There are a LOT of decisions that can go into making an OS. The compare/contrast in a few years should be really interesting.

Re:Writing an OS in Rust by valarauca1 on Tuesday December 29, 2015 @01:22PM (#10808140)
Redox was started independently by an OS hobbyist. It was linked to reddit.com/r/rust by a member of the community. It's since gone on to gain a moderate developer base within the Rust community.
Re:Writing an OS in Rust by steveklabnik on Tuesday December 29, 2015 @09:00PM (#10810300)
http://intermezzos.github.io/book/ explains. We are sharing the "get into long mode" code, but after that, will diverge. Phil's tutorial is directly resposible for getting me back into OS dev.
Re:Writing an OS in Rust by matthewmacleod on Tuesday December 29, 2015 @02:01PM (#10808374)
This has been a very useful and educational project for me to follow – both in terms of low-level kernel bootstrapping, and in terms of understanding Rust and the tooling it provides. Absolutely love it and similar well-written pieces like this.
Re:Writing an OS in Rust by phil-opp on Tuesday December 29, 2015 @07:35PM (#10810051)
Thanks a lot! New stuff is in the making :)
Re:Writing an OS in Rust by billconan on Tuesday December 29, 2015 @01:49PM (#10808314)
how do debug an OS? is the kernel debugger support from the OS, or it is from an even lower level of the system?

Do you get the debugability automatically? or you have to code to support the debugger?

Re:Writing an OS in Rust by jvns on Tuesday December 29, 2015 @01:59PM (#10808362)
the easiest way I found was to attach gdb to qemu (since you're normally running the OS in an emulator). there are instructions for how to do this in a comment on this blog post: http://jvns.ca/blog/2013/12/04/day-37-how-a-keyboard-works/

here are the instructions:

> Sam Rose Julia Evans • 2 years ago

> If you haven't yet experienced the joys of remote debugging with QEMU and gdb, you should totally try it out.

> Run qemu with the flag "-monitor stdio" and that should give you a terminal into QEMU. Then in that terminal, run "gdbserver" and it'll open up a gdb server on port 1234.

> Then, in another terminal, open gdb and run "target remote :1234" to connect to the gdbserver running in qemu.

> At first you'll find that gdb has no fucking idea what's going on. This is because it has no symbol data, so you'll need to load your kernel binary into it with the "file" command.

> After loading your kernel symbol table, you should be free to roam around the inside of your kernel using gdb as normal. If gdb isn't familiar to you, I personally really enjoyed Beej's Quick Guide to GDB: http://beej.us/guide/bggdb/ (pretty much everything by

Re:Writing an OS in Rust by sitkack on Tuesday December 29, 2015 @04:11PM (#10809089)
This would make an excellent screencast (not a request)!
Re:Writing an OS in Rust by phil-opp on Tuesday December 29, 2015 @07:10PM (#10809958)
The quick guide seems really great! Thanks for the link
Re:Writing an OS in Rust by phil-opp on Tuesday December 29, 2015 @07:16PM (#10809979)
QEMU supports gdb out of the box. Unfortunately gdb has some problems with the transition to 64 bit, but fixing it is relatively easy (you need to build gdb from source though).

I just wrote a guide that should make it straightforward: http://os.phil-opp.com/set-up-gdb.html

Re:Writing an OS in Rust by phil-opp on Tuesday December 29, 2015 @07:19PM (#10809989)
It's a bit more complicated for real hardware. I think it could be done by using the serial port and hardware breakpoint registers.
Re:Writing an OS in Rust by linuxlizard on Tuesday December 29, 2015 @01:55PM (#10808345)
Debugging an OS: - printf

- printf to serial port

- LEDs attached to GPIOs