Linux and C in the browser
There was a time when it was hard to learn how to write low-level driver or kernel code. You really needed two machines: one to work with and one to screw up over and over again until you were right. These days, you can just spin up a virtual machine and restore it whenever you go completely wrong. Much easier! We don’t think it’s very convenient, but [nsommer] has an interesting article on loading a C compiler and compiling linux for a virtual machine. What is different? Oh, the virtual machine is in your browser.
The v86 CPU emulator runs in the browser and looks like a Pentium III computer with the usual hardware. You might think it’s slow and certainly won’t be rocket fast, but it translates machine code to WebAssembly, so performance isn’t as bad as you might think.
The post details how to build and create a simple machine web page that hosts v86. Once you have cross-compiled the kernel, you can boot the machine virtually. The other interesting part is the addition of
tcc which is a fairly capable C compiler and much smaller and faster than the very traditional
tcc build is tricky because the normal build process compiles the compiler and then uses the same compiler to build the default libraries. When cross compiling this doesn’t work well because the library you want for the host compilation is different from the library you want to target for the second pass. You will see how to get around this in the post. The post goes on to show how to do remote debugging and even brings QEMU into the mix. Debugging in v86 doesn’t seem to work so far. There are more posts on this topic promised.
Honestly, it’s one of those things like teaching a chicken to play checkers. It’s doable, there’s little practical value, but it’s still something to see. On the other hand, if you spend the weekend working on it, your next Linux porting project should seem easy by comparison.
Amazing what you can achieve with WebAssembly. If you need a quick introduction, check out this one from [Ben James].