Live data from Hacker News

Making a RISC-V Operating System Using Rust

web.eecs.utk.edu

41–47 of 47 posts

Re: Making a RISC-V Operating System Using Rust

#41
post #14
post #8

I really really like the first sentence > RISC-V ("risk five") and the Rust programming language both start with an R, so naturally they fit together

When RISC-V inevitably renames itself CISC-V, then what language would naturally fit together? And don't say cobol.

C

Worse is better, forever.

Re: Making a RISC-V Operating System Using Rust

#42
post #39

Earlier quoted context omitted.

> the ergonomics of the language make dealing with allocation failure difficult; sufficiently difficult that none of the projects I've seen actually bother attempting it. Honest question: what languages make this ergonomic and can you share any projects that handle this gracefully?

Off-hand I don't know of any that make this ergonomic, at least none that don't make use of exceptions. Rust isn't unique in this regard. What makes it a potential impediment in Rust is that the constraints and burdens of the borrow checker are offset by mechanisms like Box. The fact that all the extant examples choose the convenience of Box over handling OOM, even in situations where not handling OOM is obviously a…

Writing a KBox that calls a kmalloc and where new() returns a Result, KAllocFailure> should be pretty trivial.

Re: Making a RISC-V Operating System Using Rust

#43
I took Programing Languages from him, as well as TA'd for his Operating Systems class. He is an amazing lecturer, and I am glad he is releasing something like this to the public! Definitely something worthwhile to read. Looking forward to more content being released.

Re: Making a RISC-V Operating System Using Rust

#44
post #34

Earlier quoted context omitted.

The Rust standard library is. The Rust Core library (the far more minimal library that gets used when you mark a crate as #![no_std]) does not. That's how the other "OS in Rust" and "allocator in Rust" projects work.

Well, this OS in Rust project simply commits the same sins as the standard library by using a custom allocation routine that panics on allocation failure: https://os.phil-opp.com/heap-allocation/#allocations-in-rust Saying that Rust the language doesn't require a non-failing allocator misses the point--the ergonomics of the language make dealing with allocation failure difficult; sufficiently difficult that none of t…

That's not quite right. Rust has two ways to allocate a Box.

One is the `box` keyword: this guarantees that the object will be constructed in place on the heap, but doesn't work with fallible allocators (or, in fact, anything but the default allocator). This is what the research paper you linked is talking about. However, all these years later, `box` has never been stabilized; nor will it be in its current form, because it's considered too inflexible. Whatever form of in-place construction does eventually get stabilized will likely support fallibility.

The other way to allocate a Box is `Box::new`. This is not compiler magic; it's simply a regular function, implemented in Rust, that calls the allocator and then moves (i.e. memcpys) an existing object into the new allocation. If you write your own Box-like type, there's nothing stopping you from making your `new` function fallible.

What about optimizations? Does `Box::new` get optimized in ways that a fallible version won't? Well, no. The compiler will inline `Box::new`, and if you call it with a fresh stack allocation as an argument, LLVM can theoretically, sometimes, elide the stack allocation and the memcpy altogether, instead initializing the object directly on the heap. Theoretically. The paper claims that it always does so, but the paper is wrong. [1] In fact, the compiler doesn't do so even in relatively easy cases. [2] It would be nice if LLVM did better here, but it doesn't seem to be a big source of overhead in Rust programs in practice. If LLVM did improve the optimization, it would probably work equally well for a fallible allocator as for `Box::new`, because they're equally complex from its perspective: `Box::new` can panic, and LLVM treats panics as branches.

(Box does have compiler magic for a different case: the ability to move out of it. Not being able to replicate that in a custom type is suboptimal, but not the end of the world.)

As for why those OS projects panic on allocation failure:

The phil-opp.com one implements the standard allocator interface in order to use the standard library container types. I think it sucks that Rust's standard containers don't support allocation failure, but you don't need to use them...

For Redox... I'm not actually sure what they're doing, but I think "fn oom" is a hook for users of the allocator to call if they want to panic on out-of-memory, not something that mandates panicking. At least, that's the behavior of `std::alloc::handle_alloc_error` [3], which was moved there from being a trait method on `GlobalAlloc` named `oom`. However, they're implementing a different `oom` on an old version of the `Alloc` (not `GlobalAlloc`) trait, which is unstable; that method was removed entirely well over a year ago, so I guess the code must be out of date.

[1] https://users.rust-lang.org/t/how-to-create-large-objects-di...

[2] https://play.rust-lang.org/?version=stable&mode=release&edit... (press "..." -> "ASM")

[3] https://doc.rust-lang.org/nightly/std/alloc/fn.handle_alloc_...

Re: Making a RISC-V Operating System Using Rust

#45

Amazing tutorial, and really exciting for RISC-V. I hope he creates a patreon or some other donation platform, as it must take some time to write this up. I'm sure some people would be willing to send a bit his way.

Done! Thanks for the suggestion. The Patreon link is at the top of the blog.

One suggestion: Your patreon page only mentions the Aarch64 project, not the Rust on RISC-V project. That might alienate people coming from the Rust blog; they might think they landed in the wrong spot (I sure did)

Re: Making a RISC-V Operating System Using Rust

#46
post #23

Earlier quoted context omitted.

Old wives tail from ancient times, before CompuServe: The first 20% of the effort gets you about 80% of the results. So everything seems exciting. But to that that last 20% of the results requires 80% of the total work. There is a ton of un-fun, un-glamorous work getting a gazillion device drivers written, for example.

You can always use a hypervisor, a driver OS that re-uses Windows/Linux drivers, and the new OS in a VM using virtual drivers. That's what some L4-based setups, including commercial OKL4, did. They also let you write native drivers directly on the microkernel or in OS VM's for situations where effort was justified. I'm surprised more haven't done this. Rump kernels are the closest trend.

> I'm surprised more haven't done this.

Because this is generally not secure. The driver OS will have access to hardware that can bypass the memory restrictions set upon it by the microkernel.

There is sometimes special hardware to address this but they are too complex to manage from the kernel.

Re: Making a RISC-V Operating System Using Rust

#47

Earlier quoted context omitted.

You can always use a hypervisor, a driver OS that re-uses Windows/Linux drivers, and the new OS in a VM using virtual drivers. That's what some L4-based setups, including commercial OKL4, did. They also let you write native drivers directly on the microkernel or in OS VM's for situations where effort was justified. I'm surprised more haven't done this. Rump kernels are the closest trend.

> I'm surprised more haven't done this. Because this is generally not secure. The driver OS will have access to hardware that can bypass the memory restrictions set upon it by the microkernel. There is sometimes special hardware to address this but they are too complex to manage from the kernel.

"Because this is generally not secure."

Most OS's aren't designed to be that secure, though. That's why I wonder why it hasn't been tried more for usability. A security-focused setup certainly has more to be concerned about. Like I advocated with Xen, a good start would be making the host OpenBSD. They should be able to get hardware that would be compatible.

Post reply on HN