
Boot a linux kernel right inside your browser.  - neopanz
http://bellard.org/jslinux/
======
nupark2
This is demonstrative of the advantages of the new low-level APIs being added
to JavaScript to work efficiently with binary data.

Fabrice uses this to implement an x86 interpreter -- it could not be done
efficiently without typed arrays. However, it is still slow -- imagine what
kind of advances could be made if a common bytecode was established that would
be JIT'd by the JavaScript VM, and could be output directly by the emulator.

 _This_ is why so many people want to see the browser execution environment
offer more complete, low-level APIs instead of high-level APIs locked to
HTML/CSS and legacy browser technology. Efficiently supporting high-
performance, high-complexity systems such as an _x86 emulator_ (or a video
game, or custom font rendering, or even an application framework) absolutely
require efficient low-level APIs.

~~~
tlrobinson
_Imagine what kind of advances could be made if a common bytecode was
established_

I'm beginning to believe NativeClient and PNaCl (LLVM on NaCl:
<http://nativeclient.googlecode.com/svn/data/site/pnacl.pdf>) has the best
shot at becoming this.

Google is already including NaCl in Chrome developer builds, so I wouldn't be
surprised if they enabled it in releases by the end of the year, and started
releasing Native Client applications soon after.

[http://blog.chromium.org/2011/02/native-client-getting-
ready...](http://blog.chromium.org/2011/02/native-client-getting-ready-for-
takeoff.html)

~~~
tectonic
I don't know a lot about NaCl but my gut reaction is that it will be insecure
and allow my computer to be remotely exploited. Why am I wrong?

~~~
tlrobinson
The _entire point_ of NaCl is to run native code securely.

I won't get into the details because there's tons of information out there
([http://www.chromium.org/nativeclient/reference/research-
pape...](http://www.chromium.org/nativeclient/reference/research-papers)), but
code must be compiled using a special NaCl compiler, then before it's executed
the client runs it through the NaCl verifier to ensure it's safe to execute.

Of course it's possible there are bugs in NaCl, but Google won't enable it in
Chrome by default until they're very confident it's secure.

------
jbk
After FFmpeg (used in all your TVs and gadgets, very likely), QEmu (used also
by Xen and VBox and other), tcc and his IOCC entries, the DVB-T emission with
an ATI card, Fabrice comes, once again with something crazy...

He is really impressive...

~~~
vessenes
Any Macarthur Grant folks floating around here? Take note! That's an
impressive resume.

~~~
neopanz
Great idea. That guy deserves it. Though I suspect he would be too modest to
accept the money since he GNUed all his creations.

~~~
limmeau
In the days before KVM, he wrote a paravirtualization kernel module for QEMU
(kqemu), gave away binaries and tried to sell the source. I don't know what
happened with that business.

~~~
xilun0
Yes he wrote that.

Indeed he also wrote QEMU.

------
nkurz
I'm utterly dumbfounded. Not only does it boot, it's got emacs, and a
compiler.

    
    
      Welcome to JS/Linux  
      ~ # emacs test.c
      ~ # cat test.c                                                                                                                                                 
      void main(void) {                                                               
          printf("Hello World!\n");                                                   
      }      
      ~ # tcc test.c -o hello                                                         
      ~ # ./hello                                                                     
      Hello World!
    

[Edit: just realized that there is already a 'hello.c' in the directory that
shows just this with better diction.]

~~~
nl
adduser works (except you need to create a /home directory)

~~~
retube
cd /bin for binaries. I just tried telnet, but fails as "no network"

~~~
MarkSweep
If you start the httpd you can telnet HTTP requests by hand.

------
shazow
I just forkbombed my browser. Nothing is sacred anymore.

    
    
      ~ # f(){ f|f & };f
      sh: can't fork
      sh: can't fork
      sh: can't fork
      sh: can't fork
      ...

------
wildmXranat
Fabrice is one of my all time favorite software engineers. Reading his code,
studying his methodologies and learning by copying has been a long time task
for me.

My first encounter with his code was QEmu, when it was required to run the XO
OLPC linux images. Society truly benefits when people like him are devoted in
support of open source software.

------
eliben
Fabrice Bellard is truly impressive. There's a nice article about him, called
"Fabrice Bellard: Portrait of a Superproductive programmer", here:
[http://www.softwarequalityconnection.com/2011/03/fabrice-
bel...](http://www.softwarequalityconnection.com/2011/03/fabrice-bellard-
portrait-of-a-superproductive-programmer/)

~~~
buluzhai
Another:[http://www.freearchive.org/o/55dfc9935a719fc36ab1d1656797273...](http://www.freearchive.org/o/55dfc9935a719fc36ab1d16567972732c2db1fd7d7e3826fd73ee07e4c3c7d0b)

------
js4all
Unbelievable, this is like magic. I am totally impressed. It is a real linux
instance. BTW. I see the hello.c file someone mentioned here. Have we all
mounted the same disk image?

I see so many use cases for this, but I don't fully understand what is going
on behind the scenes. Maybe someone can shed some light on it:

    
    
      1) How is the disk emulated. Is it a local image, or is it running on the backend?
    
      2) Is there a remote possibility to get the networking up and running?
    
      3) Can the disk image be externally accessed to be customized?

~~~
rickard
1): The disk doesn't seem to be emulated; it's just a rootfs in RAM.

2): See another thread here.

3): Check out cpux86.js. In the start() function at the very end, the
following section might be enlighting (even though it is a bit obfuscated by a
javascript compressor):

    
    
      function start() {
      [...]
        If=32*1024*1024;
        ya.phys_mem_resize(If);
        ya.load_binary("vmlinux26.bin",0x00100000);
        Jf=ya.load_binary("root.bin",0x00400000);
        start=0x10000;
        ya.load_binary("linuxstart.bin",start);
        ya.eip=start;
    

The files vmlinux26.bin, root.bin and linuxstart.bin are fetched from the
server.

~~~
js4all
Thanks for the info. I got it running locally with those bins you mentioned.
There is currently no further backend action.

------
sigil
This is completely awesome. When I saw <http://cb.vu> a few years ago I
thought hey, someone has finally done it, but that turned out to be a hack.
Kudos to Bellard once again.

Nitpicking: the terminal emulation is messed up. It keeps resizing
horizontally, and less gets confused. I'm sure the terminal emulator was fun
to play with. A correct vt100 state machine implementation [1] would probably
not be quite as fun [2]. There's a vt100.js library used by a couple projects
that might improve things. [3]

[1] <http://vt100.net/emu/dec_ansi_parser>

[2] <http://vt100.net/emu/vt500_parser.png>

[3] <http://fzort.org/bi/o.php>

~~~
wladimir
Indeed, this is completely awesome. I first thought it was some remotely
hosted virtual machine, but it really runs inside the browser. Insane!
Especially considered how fast it is.

When he adds X, we can run Firefox inside Linux inside Firefox inside Linux
:')

Edit: seems the terminal emulator supports ANSI escape codes such as the 16
Linux colors, but not the XTerm 256 color mode (\e[38;5;CCm) or Konsole 24 bit
(\e[38;2;RR;GG;BBm). Guess it could be easily added as it's HTML.

~~~
Maxious
> When he adds X, we can run Firefox inside Linux inside Firefox inside Linux
> :')

Skip the middle man, output gtk via html5
[http://blogs.gnome.org/alexl/2011/03/15/gtk-html-backend-
upd...](http://blogs.gnome.org/alexl/2011/03/15/gtk-html-backend-update/) (I
don't actually know if it still has dependencies on X11 :P)

~~~
wladimir
That would be even better. One could make a Linux device driver to directly
access Canvas, WebGL, or even the DOM. It seems that in my post I used "X" as
a generic name for "Linux graphics" which is pretty short-sighted.

------
swah
And you are very proud of your Javascript templating library.

------
antirez
I admire Fabrice a lot, he is a programming hero for me. However it is not
true that he is not interested in money, for instance the QEMU module
performing the run-time code translation to go fast was not released for free
in order to earn from this. I don't know if he managed to earn or not from
QEMU in the end, but IMHO that of selling the module to final user was not the
right strategy, when QEMU was started and became famous, "virtualization" was
an huge buzz and interest for many companies, so many ways to make money from
this.

Also this new code is not released under a free license, so by default as far
he is not sure what he's going to do with the code the "open source way" is
not the default. This is not a critique. I think Fabrice Bellard is a genius,
but as an observer he seems interested in making a profit from his work like
many other programmers.

Instead subjectively I've to admit today I no longer consider the GPL a
completely free license, but this is another matter and may be related not to
earning but to the meaning "free software" has for you. For me it is as simple
as letting people to do everything with your code. IMHO the BSD license turns
out to be better both for users AND for you to earn from your product later.

~~~
shareme
GPL has no restriction on selling code, RMS earned his living money by selling
reel tapes of code when GNU was first founded.

~~~
strmpnk
I doubt that's what he means. Regardless, very few people would pay for code
like that now that the web is ubiquitous.

------
m0th87
The emulator code is _just_ 86 kb, and it isn't even aggressively minified!

<http://bellard.org/jslinux/cpux86.js>

~~~
gamache
I love how it begins with "use strict";

------
aristidb
If it doesn't work for you on Chrome 12, this is probably the reason: "it does
not work with Chrome 12 beta. As far as I know, it is a bug in the browser"

From <http://bellard.org/jslinux/tech.html>

------
neopanz
Not sure how long ago he posted this. This guy never ceases to amaze me. He's
the Grigori Perelman of Hackerdom.

~~~
utoku
uname -a shows

Linux (none) 2.6.20 #3 Sat May 14 19:08:30 CEST 2011 i586 GNU/Linux

~~~
VMG
I wonder why he chose this kernel version instead of the latest

------
michaelf
This is the most amazing thing I've ever seen in my browser.

------
sagarun
I just did 'rm -rf /' then ctrl+D on the terminal. Kernel Panic! Very nicely
done! Technical notes here <http://bellard.org/jslinux/tech.html>

------
delinka
Cloud VPS in the browser. ("Please standby while we create a browser tab for
your new instance...")

"The browser as the operating."

Someone _must_ buzzword the hell out of this and then create a new startup
based on it.

------
nl
~ # tcc -o hello hello.c

~ # ./hello

Hello World

That is just too impressive for me to process.

------
vessenes
My favorite bit:

# cat /proc/cpuinfo

...

f00f bug: yes

#

Hah!

20 bogomips by the way, at least on Chrome on a MacBook Pro.

I sense a whole new era of browser performance testing..

~~~
vessenes
Okay, I take that back. My favorite part comes from the tech notes, when he
lists all the stuff he added:

[... and] my unfinished but usable emacs clone QEmacs.

?!? I am wasting my life in comparison to this super-hacker.

~~~
vessenes
Further update: qemacs works.

Unfortunately, M-x spook does not, so he clearly still has some work to do for
feature completeness.

~~~
lauri
Esc-x works for me.

------
spydum
I don't know why it's so amusing, but the network stack is fully functional
for loopback. Fire up telnetd, and telnet back to yourself. telnetd -l /bin/sh
-p 23 telnet 127.0.0.1 23 plenty of other services: httpd.. what a hoot.

------
wicknicks
He even ported his tcc compiler to it. Brilliant! I wonder if we could tap
into the device file system. Given the increasing number of sensors in mobile
phones and no standard way of reading from them, a linux like interface would
be awesome.

    
    
      int main (int argc, char **argv)
      {
           int f = open("/dev/android_accelerometer");   
           double ax = f.read();
           double ay = f.read();
           double az = f.read();
           printf ("Current 3d acceleration: %d %d %d", ax, ay, az);
           return 0;
      }

------
petdog

        ~ # cat over.c
        main () {
                int i = 0x00ffffff;
                while (i > 0) i--;
        }
        ~ # tcc over.c -o out
        ~ # time ./out
        real    0m 13.58s
        user    0m 13.57s
        sys     0m 0.01s
    

I love this stuff.

------
brudgers
> _"I did it for fun, just because..."_

Easily the best hack I've seen on HN.

------
shykes
Any volunteers to wrap a websocket transport in an network interface driver?
:)

~~~
cturner
What are the high-level steps for writing a driver?

------
spiffworks
It even has an incomplete version of Emacs. Emacs!

~~~
webholics
And Vi of course ;)

~~~
swah
He had to write Emacs somewhere.

------
maverhick
When fabrice opens the browser, the kernel compiles itself.

------
zwischenzug
If anything deserves a HackerNews circle jerk, this is it.

------
srean
Feel bad to miss out on this as I am on FF 3.6. But this remind reminded me of
a service from a long time ago. Dont remember their name, this was some 10
years ago. But they used to provide a KDE desktop via the browser as a java
applet. It was not terribly snappy, but quite surprisingly usable. Any one
remembers this ?

~~~
wladimir
With Guacamole (<http://guacamole.sourceforge.net/>) you can run a VNC session
in Javascript, so you can have your desktop, but alas this still needs a
remote (virtual) machine running the desktop.

Hmm if network emulation was added to the javascript VM you could combine it
with this.

------
kmil
Fabris should put donate button on the page! I would happily donate to thank
him for his great contribution to the Open Source and pushing boundaries of
what possible to do with modern technology. thank U Fabris :)

------
mikeytown2
Anyone know how to get the network working? ping/wget don't work. But
seriously, this is pretty flippin sweet!

~~~
rickard
Implement emulation of a network card... ;) The tech notes page states that
"there is no network emulation at this point".

~~~
terminus
Yeah. That would be a whole interesting can of worms. Would need
PCI/USB/whatever bus your ethernet card was on. Plus emulation of the selected
ethernet card itself.

Loopback (which is present) is so much simpler.

~~~
rickard
I guess a tun/tap to some server-side service might be possible too, but
perhaps not very interesting. The x86 dynamic library api suggested in the
tech notes sounds more useful.

Edit: Easier idea might be adding ttyS1 and connecting that to a websocket on
the server. Combined with something like socat or ppp, that ought to work.
Since the console is already ttyS0 and connected to the js terminal, it might
be doable even with the obfuscated source.

~~~
plasma
Maybe power the network using a server side relay script and an ajax request?

~~~
shawndumas
WebSockets?

------
wslh
Now I can buy a Chromebook :-)

~~~
KaiP
Gets stuck at "Freeing unused Kernel memory: 124k freed" on my Cr-48...

~~~
kleevr
Getting the same thing on Google Chrome 12 on Ubuntu 9.04.

------
gmosx
Didn't know about this guy! Unbelievable track record of consistently
releasing groundbreaking software. Oh, and the x86 js emulator is cool too!
made my day!

------
Aissen
Anyone know why he used 2.6.20 ? I tried compiling a 2.6.38.6, but it doesn't
boot. It's stopped at "Starting Linux" and CPU stays at 100% all the time.

~~~
wesnet
compiling 2.6.38.6 ?

~~~
Aissen
If you wanna know what I did:

    
    
      #from a 2.6.38.6 vanilla tree
      wget http://bellard.org/jslinux/config_linux-2.6.20 -O .config
      yes "" | make oldconfig
      # small edit in drivers/tty/serial/8250.c to copy Bellard's patch (http://bellard.org/jslinux/patch_linux-2.6.20)
      make vmlinux
      cp arch/x86/boot/vmlinux.bin /path/to/copy/of/jslinux/hosted/with/a/simple/httpd/
    

Edit: I just tried again with a 2.6.20.21 kernel using more or less the
procedure showed above: it works! So it must be something with more recent
kernels (scheduler ? dynticks ? merging of i386 and x86_68 arch in x86 ?
something else ?) that is not emulated by jslinux.

~~~
Aissen
Well the answer was simply in the technical notes: jslinux requires an FPU
emulator in the OS, and in Linux it seems it was disabled after 2.6.20 for
x86.

~~~
KonradKlause
2.6.23 boots also fine. x86 and x86_64 were merged in 2.6.24, maybe here
support for 486 without fpu got broken...

------
pbhjpbhj
Anyone else wondering if they're now part of this guys compile/render farm!?

------
rman666

      ~ # httpd
      ~ # telnet 127.0.0.1 80     
      get / http/1.0
    
      HTTP/1.0 404 Not Found                                                          
      Content-type: text/html                                                         
      Date: Tue, 17 May 2011 20:10:52 GMT                                             
      Connection: close
    
      <HTML><HEAD><TITLE>404 Not Found</TITLE></HEAD>                                 
      <BODY><H1>404 Not Found</H1>                                                    
      The requested URL was not found                                                 
      </BODY></HTML>                                                                  
      Connection closed by foreign host                                               
      ~ #
    

Coolness!

------
tmachinecharmer
There is EMACS TOO!!! This is insane. I am going mad with happiness. I really
really have tears in my eyes.

------
SomeIdiot
So any interesting ideas for what this can be used for?

~~~
dcposch
It might be useful for CS education.

I have a bit of experience with this as a course helper in the introductory
(CS106 series) CS classes at Stanford.

I think it could be a very cool platform for assignments in CS107 (where
students are introduced to Unix and C programming) or CS110/CS140 (the core OS
classes).

Instead of ssh-ing into a locked-down cluster computer, students could boot a
machine in their browser and get root access. No setup, no configuration.
Everyone gets an identical setup, which would be really nice for the people
making the assignments. The main thing people would need is a way to save
files to a persistent disk. Maybe a filesystem driver could be added that uses
HTML5 Local Storage, or talks to a web service running on the class website?

Incidentally, students in CS140 are already using Fabrice Bellard's software.
You write most of a very simple Unix kernel, testing and running it in QEMU.

~~~
xilun0
Why would you want to use QEMU for CS140 and an inferior solution for CS107?

------
terminus
Neat. You can mount sysfs like so:

    
    
      # mkdir /sys; mount -t sysfs /sys /sys
    

That would allow you to look at the emulated hardware that is present. Looks
like only virtual devices are present -- ram, loopback network etc.

Makes sense to me. The MVP here is cpu emulation.

------
gabi38
How is this thing loads the kernel? I've read the
<http://bellard.org/jslinux/tech.html> but it doesn't say, Does it loads it
over the net or what?

~~~
masklinn
It downloads a kernel image (and a disk image) over the network (open the
developer tools in your browser and reload the page, you will see 3 .bin files
of 1~2MB being transferred)

------
angusgr
I'm sad the source has been compressed so there's no easy human-readable View
Source, like there was on the VAX emulator that came up a few weeks ago. :(
Still awe-inspiring, though. :)

~~~
hhdk
Paste the source at <http://jsbeautifier.org/> \- It makes the source readable

~~~
angusgr
Thanks both of you, jsbeautify extension now installed. :)

[https://chrome.google.com/webstore/detail/kkioiolcacgoihiiek...](https://chrome.google.com/webstore/detail/kkioiolcacgoihiiekambdciinadbpfk)

------
ayanb
92.3 KB minified for the two JavaScript files.

wow!

------
albertzeyer
Actually I thought about doing the same via LLVM and Emscripten. This seems
like he emulates x86. Which is probably less hackish but also slower.

------
vsajko
boots in firefox on android although you can't type anything. I am amazed

------
anc2020
Awesome

    
    
        cd /
        rm -rf ./*
        echo *

------
mark-r
If you have a programmers.stackexchange.com account, you might want to put a
vote in here: [http://programmers.stackexchange.com/questions/47197/are-
the...](http://programmers.stackexchange.com/questions/47197/are-there-any-
famous-one-man-army-programmers/47240#47240)

------
josepsanzcamp
I tested the JS/Linux with google chrome 12 (beta and gnu/linux) with a new
vmlinux26.bin and root.bin compiled using the 2.6.30 kernel with buildroot and
work as expected (fine).

I don't know what is the problem of the original 2.6.20 linux kernel with
JS/Linux but with this new compilation work perfectly.

------
asadotzler
Twice as fast in Firefox as in Chrome.

------
swah
How long until you see the prompt?

------
geekzgalore
[http://www.softwarequalityconnection.com/2011/03/fabrice-
bel...](http://www.softwarequalityconnection.com/2011/03/fabrice-bellard-
portrait-of-a-superproductive-programmer/)

------
cfq
I saw this and thought "how does it know who I am?":

io scheduler cfq registered (default)

------
jjm
This is hot! Needs logging facility too but otherwise I'm blown away!

------
rakkhi
This looks awesome! I'm just starting to learn ruby and was going to look
today on whether there was a way to run it in a browser very easily. Wonder if
this is the answer

~~~
guelo
<http://tryruby.org/>

~~~
rakkhi
Yeah try ruby is a great start. Just need the same interface not just in
lesson form to allow you to create Ruby on Rails applications

------
loso
wow, it is projects like this that get the imagination going and makes you
feel bad for putting off a problem set that might have frustrated you at the
time.

------
whatwhatwhat
Can i run Node.js on this?

/evil grin

~~~
robinduckett
Can I run this in Node.js!?

~~~
maratd
Should be able to run it in PhantomJS if you need something headless.
<http://www.phantomjs.org/>

------
tcarnell
Awesome - so could you run a web-server from within the browser? Would it be
possible to connect to an instance of jslinux remotely?

------
ramidarigaz
Woohoo! Kernel panic! Haven't seen one of those in... well... forever. (Newbie
here)

Edit: This is _damn_ cool.

------
vecter
Could someone write a program that would cause a stack overflow, compile it
with tcc, and run it?

------
tectonic
Why is it that whenever I play with a temporary Linux install, I always end up
typing "rm -rf /"?

------
agilo
This reminds me of <http://uni.xkcd.com/>

------
mrpixel
Now let's mass-email Bellard to coax him into publishing an open-source
version. :)

------
senthilnayagam
this can be a good tool to for students to learn basic linux commands

/usr/bin has many goodies

vi, top, ps, grep, awk

------
bane
_A more advanced version would allow to use old DOS PC software such as
games._

well I'm sold.

~~~
phowat
there's naclbox: <http://www.naclbox.com/> for google's native client.

------
plainOldText
I believe this submission got the highest number of upvotes in HN history.

------
braindead_in
Looks so similar to Linux booted on an embedded device. Nostalgia.

------
josepsanzcamp
What license do you want use to publish this job???

~~~
neurolysis
I mailed him about it earlier, he says he hasn't decided on a license yet.

~~~
josepsanzcamp
Thanks.

I expect a response with a lot of emotion.

Josep.

------
blackman
now I'm wondering how hard it would be to write a commodore 64 or nes emulator
in javascript to embed games in webpages?

------
wonginator1221
Well, It's a great way to try out rm -rf /

------
quattrofan
but not in my android browser apparently

------
dendory
Fun command to use:

reboot -f

~~~
jokermatt999
Crashed Firefox portable 4.0.1 for me. What's the intended result?

~~~
danvet
Iirc the way to reboot an x86 machine is to triple-fault, i.e. execute an
interrupt in an interrupt in an interrupt. Could be done by e.g. marking the
pagefault handler's stack space as "not available" in the pagetables.

Now if the virtual cpu does not implement that reboot behaviour, the likely
result is just an endless recursion of fault handlers. So the script runs an
endless loop, hanging Firefox until it notices this and stops it.

~~~
xilun0
argggggg Might have been an interesting way to proceed because of various
weird reasons 15 or 20 yeats ago, but current x86 plateforms are not that
convoluted (for at least 10 years). Just outw (or is it outb? don't remember)
the correct value to port 0cf9h (which btw has nothing to do with the legacy
PCI conf addr register, which is 32 bits starting at io port 0cf8h for great
confusion...) and your computer reboot.

------
benihana
First thing I did after booting?

    
    
        alias ll='ls -lh'

------
awaz
interesting!

------
EGreg
this is some crazy shit

------
thristian
The page was near-unreadable for me because the CSS uses the font stack
"courier,fixed,swiss,sans-serif", and courier is just too thin and wispy
against a black background. I opened Firebug and changed the CSS declaration
to be "monospace" and then all was right with the world.

That said, it's a phenomenal achievement, and should be a great way for
websites to demonstrate command-line tools to people. :)

(also: browsers let you store more information with DOM Local Storage than you
could in a cookie; If you could expose that to Linux as a block device...)

------
Qerub

      ERROR: your browser is too old to run JS/Linux.
      You should use a recent browser such as Firefox 4.x or Google Chrome.
    

No, it isn't. Stop using user-agent matching and start using JavaScript
feature detection instead.

~~~
re
It does. It tests for ArrayBuffer support, outputting that message if the
necessary constructors aren't found. What browser are you using that you
expect to be supported? (Safari doesn't have support for ArrayBuffer, but a
recent WebKit nightly does, and this runs in that).

~~~
aw3c2
Even in that case the error messages is still wrong. My browser is up to date,
it just does not support the needed feature(s).

