WebAssembly (WASM) arch support for the Linux kernel

marcodiego | 288 points

That’s fast. Buggy, but fast. I’m totally impressed! Especially because I researched the necessary steps to do the same thing 10 years ago based on [0]. The patches required for this hack touch LLVM, libc, Linux kernel, BusyBox, ... and total approximately 15,000 lines of code.

I ran a small performance test with 'bc -lq' and compared with [0]:

  scale=1000
  4*a(1)
This WASM architecture compilation completely blows away my old emulation setup, which only managed around 200 MIPS. Maybe this approach can be generalized. Running a full Linux distribution at near-native speed right in the browser would be awesome.

[0] https://github.com/s-macke/jor1k

s-macke | 2 days ago

Tried running:

  ~ # du -h 
  (...)
  [Runner sh (2390656)]: Wasm crash: RuntimeError: operation does not support unaligned accesses
  [Main]: Stopping CPU 0
  [Main]: Stopping CPU 1
  [Main]: Stopping CPU 2
  Kernel panic - not syncing: Aiee, killing interrupt handler!
  [Runner sh (2390656)]: Kernel panic: Aiee, killing interrupt handle
> Due to a bug in LLVM's build system, building LLVM a second time fails when building runtimes (complaining that clang fails to build a simple test program). A workaround is to build it yet again (it works each other time, i.e. the 1st, 3rd, 5th etc. time).

I'm incredibly curious what this bug might be!

lalitmaganti | 2 days ago

This is cool because it avoids emulation. However I think it has many shortcomings today which could all be solved by emulating a real CPU architecture (e.g memory protection support, ecosystem with tooling and Linux distributions).

By the way I have developed a similar project, WebCM, a RISC-V emulator capable of running full Alpine Linux that can be embedded in the Web browser and can reach up to 500 MIPS for some users, which I think is pretty fast despite the emulation, you can try at https://edubart.github.io/webcm/. Booting is also fast, it always boots from scratch when you open the page, so you can boot fast even with emulation.

edubart | 2 days ago

Really impressive work. Would love to see it progress.

Some ways I can see it could improve:

  1. setjmp/longjmp could implemented via Wasm Exceptions (this is how we do it on WASIX) - no need to wait on stack switching proposal
  2. fork could work easily with asyncify (start/resume), per binary compiled
  3. JIT could work via dlopen/dlsym (compiling the Wasm and linking it), even with runtime patching (using memory spaces on tables and updating them as you go to newly compiled code).
In general, I recommend taking an inspiration from WASIX [1] for those things, as we have spend quite a bit of time to make things work as much as possible!

[1] https://wasix.org/

syrusakbary | a day ago

How does this compare to the c2w container2wasm approach?

container2wasm/container2wasm: https://github.com/container2wasm/container2wasm :

> container2wasm is a container-to-wasm image converter that enables to run the container on WASM.

> Converts a container to WASM with emulation by Bochs (for x86_64 containers), TinyEMU (for riscv64 containers) and QEMU.

> Runs on WASI runtimes (e.g. wasmtime, wamr, wasmer, wasmedge, wazero)

> Runs on browser

> x86_64, riscv64 or AArch64 containers are recommended.

/? container2wasm: https://hn.algolia.com/?dateRange=all&page=0&prefix=false&qu...

ktock/vscode-container-wasm https://github.com/ktock/vscode-container-wasm :

> Containers on VSCode for the Web [ https://vscode.dev ]

ktock/vscode-container-wasm-gcc-example: https://github.com/ktock/vscode-container-wasm-gcc-example

JupyterLite works without install on Chromebooks.

JupyterLite still lacks a Terminal e.g. with BusyBox Ash in WASM, with a file system integrated with the Jupyter-xeus kernel file system.

This appears to load much more quickly than other Linux and I think even just bash in WASM demos I've seen.

westurner | 4 days ago

This is crazy cool. 8,000 CPUs. I wonder if any types of programs would ever make 10k tasks in their normal runtime behavior.

"One important difference is that there is no way to suspend execution of a task. There is a way around this though: Linux supports up to 8k CPUs (or possibly more...). We can just spin up a new CPU dedicated to each user task (process/thread) and never preempt it. Each task is backed by a Web Worker, which is in practice backed by a thread in the host OS (through the WebAssembly implementation). "

koolala | 2 days ago

Very cool! I'm curious as to how it compares with WASIX in terms of both compatibility and performance.

Also tangentially related: I'd love to see a performant build of Node.js compatible with this runtime (or really any flavor of WASM), but I think you'd run into the same issues that I have with WASIX. Namely build headaches, JIT, and wasm(-in-wasm) support. I'd explore it myself but I've already sunk way more time than is reasonable on that endeavor.

seanw265 | 2 days ago

Could this work with https://github.com/webassembly/wasi-libc ?

It seems like OP put together their own musl-based libc which is awesome, but being able to compile against WASI would open up a lot of possibilities.

This also reminds me of the recent thread on user-mode linux -- how easy it would be to compile to WASM was definitely on my mind.

hardwaresofton | 2 days ago

   ~ # ping 8.8.8.8
   PING 8.8.8.8 (8.8.8.8): 56 data bytes
   ping: can't create raw socket: Function not implemented
   [Runner sh (18823808)]: Wasm crash: RuntimeError: memory access out of bounds
darn
philipwhiuk | 2 days ago

This is such a brilliant thing.

I am not a person involved in building anything like this but I am a person who frequently used copy.sh/v86 and was actually building a tinycore fork which added jujutsu so that people can run jujutsu to try it out in their browser. It was a project which made me understand so much more about building linux from scratch, what isos are, and everything.

One surprising thing was that I was able to make the iso actually have jujutsu and it could run on something like qemu but not on tinycore due to some minor issue

Basically I believe that some really interesting quick-tries of some software can happen if we can have customized linux with our apps directly in the browser

This is such an interesting project really and it feels very snappy to me.

I have a quick suggestion as someone trying to make a custom iso linux / binary apps just work on linux in browser: Can you please, please, create a docker image where you can give static applications as an input in the docker and it would automatically generate the html page with wasm or the wasm output containing that static application in the /usr/bin

You mention something like this this

docker run -it -name full-linux-wasm linux-wasm-contained:dev /linux-wasm/linux-wasm.sh all

I haven't read the project more but I do find it incredibly minimalist and I genuinely hope you can add the ability to add static/hopefully some day glibc as well but I am mentioning it because there are a lot of golang tools which are statically linked and it would be so interesting to running them in browser if possible via their binaries

Yes I know that they could probably run via compiling into wasm itself but that just felt so much messy to me on how I could provide other tools like busybox with it or how it would actually feel like a linux environment to test it out as an example

I have to admit, your project is really really cool. Good luck on this project! Starred, and have a nice day.

Imustaskforhelp | 2 days ago

I think lightweight wasm OS runtimes can have real use cases for instance in "cloud terminals" (such as in Rancher or at hyperscalers) which currently just connect a web terminal to some k8s pod. Or in more snappy Jupyter scientific notebook ecosystems. But people have to meet a sweep spot of downloading <1MB runtime, realizing networking and enough tools within the VM.

ktpsns | a day ago

Considering that WASM has a Lispy text syntax (known as WAT), I guess this is could be seen as a revival of Lisp Machine...somehow?

https://en.wikipedia.org/wiki/Lisp_machine

stevefan1999 | a day ago

So next step they run a browser in this Linux in WASM in the browser. And then run Doom in that browser using WASM.

VladVladikoff | a day ago

How does it compare to https://xrsh.isvery.ninja (ignoring the XR aspect)?

utopiah | 2 days ago

Very cool.

If anyone's curious to see what's packed in here at a glance: https://modsurfer.dylibso.com/module?hash=3fa6b28252b0d72c82...

nilslice | 2 days ago

Hopefully this will make WASM more popular. I tried to get into it but lack of documentation was already one reason to not invest too much; speed concerns mentioned by other bloggers also amplified this issue recently. For some reason WebAssembly is not really "breaking through" right now. Perhaps it is inertia, perhaps another reason.

shevy-java | 2 days ago

I hope the situation gets better for Firefox.

evanjrowley | 4 days ago

This is great, I started working on this during the early days of WASM, however left coz of busy schedule... Happy to see someone making progress ;)

virajk_31 | a day ago

killed by the fork bomb

    :(){ :|:& };:
frizlab | 2 days ago

The Gary Bernhardt prophecy is still alive.

(https://www.destroyallsoftware.com/talks/the-birth-and-death...)

littlestymaar | 2 days ago

~ # wget

[Runner sh (18815616)]: Wasm crash: RuntimeError: abort

Illegal instruction

u8080 | 2 days ago

when docker ;)

acdbddh | 2 days ago
[deleted]
| 2 days ago
[deleted]
| 4 days ago

[flagged]

jgoode19 | 2 days ago

What's the point ?! It's insane. Who on earth would use that?

iberator | 2 days ago

$ rm -rf /

and it's gone

throwaway031125 | 2 days ago