
Writing an OS in Rust: Advanced Paging - ingve
https://os.phil-opp.com/advanced-paging/
======
fotcorn
I started to create the counterpart, a x86_64 bit emulator written in Rust:
[https://github.com/fotcorn/x86emu](https://github.com/fotcorn/x86emu)

Disclaimer: Almost no comments, unidiomatic Rust, tons of hacks. Currently can
run the uncompress code of Linux and some initial startup code. Not much more
than instruction decoding & execution and basic page handling is implemented,
the rest of the hardware is missing.

Sadly I do not really have time right now to work on it. I am still torn if
Rust helps more than it makes everything more complicated, but recent Rust
version should have made some things I wrangled with much easier.

~~~
bluejekyll
> I am still torn if Rust helps more than it makes everything more complicated

Do you have an example in your code of what is complicated? I took a quick
look, and didn’t see anything crazy.

------
rurounijones
For someone who is completley not into OS programming but has heard a lot
about safety and rust etc. etc.

Assuming the rust safety lives up to the type.

Is this (or any other project) likely to ever become a "real" useful os. What
about including rust modules in the linux kernel? What are the barriers?

~~~
jerf
Is someone likely to build a Rust OS that is real and useful? Yes. Projects
are already in progress, and even if they all shut down, I'm sure more will
exist in the future.

Is that Rust OS going to experience "success" by traditional metrics, such as
taking a significant chunk of Linux's marketshare away, and becoming a viable
thing you can run on some real piece of hardware for some real purpose? I
won't say "no" but it will be at least a decade before that will happen. Not
because Rust is bad, or the resulting OS will be bad, but all the entrenched
advantages existing OSes have are just staggeringly huge. If a perfect OS
jewel of perfection appeared today, but with no other support for it (no
software, drivers, applications that can only run on it, etc.) it would be a
long, long time for it to get any traction.

~~~
AnIdiotOnTheNet
The key to getting there (aside from the driver issue), I think, would be
creating an environment that is simple, easy to work with, and friendly to
creating new tools. The faster you can spin up new tooling, and the more you
want to, the more willing people will be to move to the new system. Then you
provide VMs, emulators, and/or compatibility environments for running software
from "legacy" systems.

Do not attempt to make a new posix system, because we already have giant,
entrenched, mature posix OSs and you'll bring nothing new or interesting
enough to the table to warrant switching while being held back by the creaking
old standard.

I'd be willing to make a lot of sacrifices to move to an OS that met my
personal list of wants.

~~~
pjc50
Maybe, but if you dont't have a POSIX API you'll cut yourself off from a lot
of applications. Even Windows is gradually admitting defeat with WSL.

You can only get adoption of a non-POSIX OS if it offers something really
unique to the user, or is in a very different market space.

~~~
AnIdiotOnTheNet
You can do it in a VM or compatibility layer. Your OS can support
"personalities" like Windows uses to implement WSL, but with containers. So
now you can have multiple POSIX containers on your non-POSIX system.

You should encourage people to make native software using the new paradigms
and abstractions of the OS, built from the ground up with decades of
hindsight, rather than encouraging crappy ports.

> You can only get adoption of a non-POSIX OS if it offers something really
> unique to the user

At this point, just not being built on 50 year old crappy standards would be
pretty unique. I have my own personal wish-list of features that would lure me
in even if the only hardware supported was an RPi.

~~~
glhaynes
You still end up in the OS/2 situation - even if non-native apps didn't always
run great, had much worse memory usage, didn't feel native, etc... they were
still good enough that nearly nobody (comparatively) wrote native OS/2 apps.
It's not impossible to imagine an alternate past where OS/2 had worse
DOS/Windows compatibility and ended up more successful! (In actuality, I think
it'd probably have done even worse — showing that the strategic options to
replace the foundation of an entrenched ecosystem are "bad" vs. "worse".)

I'd like to see other paradigms explored, too, but it seems unfortunately hard
to get there from here.

~~~
AnIdiotOnTheNet
Yeah, it is hard. So what? So is just about everything worth doing.

One thing is for certain: you will not replace the current players by cloning
them.

~~~
kbenson
Isn't that how Linux replaced most the UNIX players, by cloning them?

~~~
AnIdiotOnTheNet
I think that had more to do with the important innovation of being free and
open.

~~~
kbenson
There were plenty of free and open operating systems. Linux and FreeBSD had
the advantage of also being similar if not the same as what they were
replacing, as well as allowing a lot of the software that was designed for
UNIX's to also work with little or no change. In other words, they cloned the
look, feel and compatibility (well , FreeBSd didn't really clone it, it IS
it).

~~~
AnIdiotOnTheNet
IIRC FreeBSD was legally nebulous at the time, and Minix couldn't be modified
and redistributed. I don't recall any other significant players.

~~~
kbenson
AFAIK, any possible nebulous legal situation with FreeBSD should have been
resolved by late 1994[1], which is _extremely_ early in Linux's lifetime if
we're talking about supplanting major operating systems. I think you're right
that there weren't a lot though.

That said, I can't imagine Linux actually having gone very far if it wasn't a
UNIX clone. I think it would have been a real long uphill battle and it would
have gotten handily beat by whatever open source UNIX clone came along next,
for the pure reason that it's much easier to deal with POSIX compliance (and
all the software it brings that actually makes it worthwhile to run the OS)
when trying to copy a system that already did it and worked out the kinks.

I mean, if someone came out with a free (maybe open source) version of Windows
at that time which was legal, we could easily be living in a world where
that's the dominant free OS.

I guess what I'm saying that that being vastly better on one metric (in this
case, free) doesn't necessarily mean you'll take over a market (because a free
operating system that can't run any software isn't very useful), and just
being a clone won't either, but being a clone that's generally as good one
most metrics and vastly better on a few others (e.g. a UNIX which is also free
and open source, instead of just some other UNIX) _may_.

1:
[https://en.wikipedia.org/wiki/FreeBSD#Lawsuit](https://en.wikipedia.org/wiki/FreeBSD#Lawsuit)

------
vectorEQ
creating recursive page tables is bad idea.

~~~
twtw
Why?

~~~
Rusky
It's not a general solution- you often need to access the page tables of other
address spaces, and those aren't picked up by a recursive mapping.

And while a single recursive mapping is a relatively small fraction of the
address space, you probably don't want to do it for all address spaces
simultaneously.

So you need something more general anyway, and you may as well just use it all
the time.

~~~
phil-opp
It is possible to access other address spaces, it is just a bit more
complicated:

\- Set the recursive entry of the active level 4 table to the (recursive)
level 4 table of the other address space. \- Now the recursive addresses point
to the other address space, so you can access its tables freely. \- The
difficult part is to switch back to the active address space again because can
no longer access the original level 4 entry. This can be solved with a
temporary mapping to the active level 4 table or by using different level 4
entries for the recursive mapping of active and inactive address spaces.

However, I'm no longer sure whether recursive page tables are a good choice
for the blog since they're complicated, difficult to learn and not portable.
See my reply to DSMan.

------
gbrown_
Previous discussion
[https://news.ycombinator.com/item?id=18903235](https://news.ycombinator.com/item?id=18903235)

Edit: Discussion of previous post.

~~~
zaarn
That's actually a different post.

~~~
sctb
As an exercise in optimistic parsing, the OP could've meant “Previous
discussion of previous post”. It's a handy link in either case, thanks!

