Kernel Drivers Compiled to Javascript and Run in Browser


November 07, 2012 posted by Antti Kantee

The unique anykernel capability of NetBSD allows the creation of rump kernels, which are partially paravirtualized kernels running on top of a high-level hypervisor. This technology e.g. enables running the same file system driver in the monolithic kernel or as a microkernel style server in userspace. POSIX-compatible systems have been more or less supported as rump kernel hypervisors for the past 5 years. A long-time goal has been to extend hypervisor support further, for example to embedded systems. This would bring the solid driverbase of NetBSD available to such systems with only the cost of implementing the hypervisor.

To see how far things can go, last week I started toying with the idea of using a javascript engine as a rump kernel hypervisor. I was planning to compile the NetBSD kernel sources into javascript and manually implement the hypervisor. After some searching for a C->javascript compiler, I found emscripten, which translates C into javascript via LLVM bitcode. Not only is the compiler itself extremely mature, but there is also extensive support for the POSIX API. This meant that I could not only compile the kernel drivers to javascript with emscripten, I could also compile the existing POSIX hypervisor and have it work.

The approach of compiling kernel drivers into javascript allows them to be directly accessed from existing javascript code. Yes, I did add a sys/arch/javascript into the kernel source tree. This contrasts the approach taken by another similar experiment, where an x86 Linux is run inside a x86 machine emulator running in a javascript engine.

I have thrown together a small proof-of-concept demo of how to build a web service with the capability to access file system images using kernel file system drivers compiled to javascript. I compiled a rump kernel with support for the FFS, tmpfs and kernfs file systems. This rump kernel backend is tied to a lightweight web page which passes requests from forms to the rump kernel and displays results. When the javascript is run, it downloads an FFS image (rump.data), bootstraps a rump kernel, and mounts the FFS image r/o at /ffs. The status can be further manipulated with interactive commands.

The demo is available here. I've tested it to work with Firefox and tested it to not work with Internet Explorer. YMMV with other browsers. Note, the javascript and the FFS image together are close to 5.5MB in size, so the page may load for a few moments over a slow link -- javascript is not exactly compact and whitespace removal was the only size reduction technique I used. If you're interested in comparing the generated javascript with the C sources, you can also look at the unoptimized version (14MB).

[16 comments]

 



Comments:

OMG See you at the hackathon...

Posted by whygee on November 08, 2012 at 01:26 PM UTC #

It works fine under Internet Explorer 10.

Posted by xenu on November 08, 2012 at 02:27 PM UTC #

Works fine with Safari 6.0.2, OS X 10.8.2.

Posted by jideel on November 08, 2012 at 04:14 PM UTC #

Works great under Chrome 22!

Posted by Jon on November 08, 2012 at 04:17 PM UTC #

Works in Opera 12.2

Posted by vdvluc on November 08, 2012 at 11:08 PM UTC #

Works with xombrero browser

Posted by sjakke on November 08, 2012 at 11:22 PM UTC #

Nicely done! Works on IOS 6.1 Safari! (iPhone)

Posted by Tim on November 09, 2012 at 02:50 AM UTC #

Works fine on chrome for android

Posted by sergi on November 09, 2012 at 06:15 AM UTC #

Works fine with Chrome 24, Ubuntu 12.10.

Posted by Gary Gapinski on November 09, 2012 at 12:46 PM UTC #

Now you just need to add a decent command-line to your demo (like https://github.com/mozilla/gcli) and you've got something wonderful!

Posted by Panos Astithas on November 09, 2012 at 01:15 PM UTC #

I'm really amazed of what you are doing, keep it up.

Posted by Sci3ntist on November 10, 2012 at 08:50 PM UTC #

As an educator, I can see a *lot* of utility in your approach. Using this approach I can give students access to systems programming in the browser. I was wondering if you would mind explaining a little more to those of us from a non-NetBSD background. For example, I can see the code translated from C to JS, but I can't identify where the /ffs file system is. Presumably, one needs to modify this file system image to provide a "full" shell such as bash or zsh? Is there any way of installing ports on /ffs? Coming from a Linux background, this project demonstrates some of the unique features of NetBSD very well.

Posted by Aidan Delaney on November 11, 2012 at 12:29 PM UTC #

Aidan: I agree that rump kernels have some good characteristics for systems education, both from the perspective of giving the student a low barrier of entry to real-world kernel code and from the perspective of the educator being able to verify the results of the exercise. However, I am not sure why you consider the web browser a good environment for this. Is there any specific reason, or, since you mention not being familiar with NetBSD, is this simply because you are unaware of the capability to host rump kernels in regular processes? If you're interested in investigating further, I'd suggest starting by walking through this tutorial: http://www.netbsd.org/docs/rump/sptut.html

Posted by Antti Kantee on November 12, 2012 at 03:05 PM UTC #

Using the web has several advantages, one of which is that I can present shell scripting notes and an interpreter side-by-side. There are other advantages such as zero-install. I'm reading the rump kernel documentation. I'm assuming that applications on top of the rump kernel need to treat JavaScript as their "ISA". As in, I'd need to emscripten dash (or similar) to run on the rump kernel?

Posted by Aidan Delaney on November 14, 2012 at 09:24 PM UTC #

A rump kernel is not a program execution environment. It's more on the lines of "kernel as a service". Now, I don't think this is the best possible place to delve into details, so if you want to discuss this more, please contact me via email.

Posted by Antti Kantee on November 23, 2012 at 02:04 PM UTC #

my mind has been blown

Posted by don bright on December 22, 2012 at 03:08 AM UTC #

Post a Comment:
Comments are closed for this entry.