Total Pageviews

Thursday 3 March 2016

在Web上运行的Linux系统


神器在这里http://bellard.org/jslinux/

请使用Firefox 或者Chrome或者safari打开上面这个页面,那么你会看到一个Linux的启动过程,请注意:这不是什么模拟Linux启动的假游戏,而是确确实实通过JS编写的模拟器启动了一个确实存在的Linux系统

该操作系统由Fabrice Bellard大牛完成,这个神奇的东西引起了包括Javascript的创建者Brendan Eich在内N多人的注意。随后,Fabrice Bellard大牛发布了相关的技术说明:http://bellard.org/jslinux/tech.html,从这份文档中可以知道:

这个模似器完全由Javascript写成,CPU仿真器使用的是QEMU,Linux  使用了2.6.20内核,磁盘用的是Ram Disk,在启动的时候装载。其文件系统由Buildroot 和BusyBox产生。

Web的时代……

Javascript PC Emulator - Technical Notes

By Fabrice Bellard - May 14, 2011

This PC emulator is written in Javascript. The emulated hardware consists in the following devices:


32 bit x86 compatible CPU

8259 Programmble Interrupt Controller

8254 Programmble Interrupt Timer

16450 UART

Real Time Clock.
The code is written in pure Javascript using the W3C Typed Arrays which are available in some recent browsers. Only this feature is tested to determine if a given browser can launch the PC emulator. It was tested with Firefox 4 and Google Chrome 11 on Linux, Window and Mac (it does not work with Chrome 12 beta. As far as I know, it is a bug in the browser). In any case, a fast Javascript engine is needed to have good performance.



CPU Emulation

Some of the code is inspired from my x86 dynamic translator present in QEMU, but there are important differences because here it is an interpreter. The CPU is close to a 486 compatible x86 without FPU. The lack of FPU is not a problem when running Linux as Operating System because it contains a FPU emulator. In order to be able to run Linux, a complete MMU is implemented. The exact restrictions of the emulated CPU are:


No FPU/MMX/SSE

No segment limit and right checks when accessing memory (Linux does not rely on them for memory protection, so it is not an issue. The x86 emulator of QEMU has the same restriction).

No CS/DS/ES/SS segment overrides. FS/GS overrides are implemented because they are needed for Thread Local Storage in Linux.

A few seldom used instructions are missing (BCD operations, BOUND, ...).

No single-stepping

No real mode

No 16 bit protected mode (although most 16 bit instructions are present because they are needed to run 32 bit programs).
Most of these restrictions are easy to remove, but I decided to implement the strict minimum to be able to use a recent Linux kernel and its user applications.

I added some tricks which are not present in QEMU to be more precise when emulating unaligned load/stores at page boundaries. The condition code emulation is also more efficient than the one in QEMU.

Devices

Currently there is no synchronization between the PIT frequency and the real time, so there is a variable drift between the time returned by Linux (try the "date" command) and the real time.

The UART (serial port) does not support FIFO mode. Perhaps it could help to improve the display speed.

There is no network emulation at this point.

A clipboard device (seen as /dev/clipboard in the emulator) was added to allow exchange of data between the emulator and the outside world.

Javascript terminal

Although I could have reused the excellent termlib, I decided to write my own because I was curious to see how it could be done. The main problem is the key handling which is different among browsers and OSes, as described here.

Linux distribution

I compiled a 2.6.20 Linux kernel (I guess any other version would work provided there is still an FPU emulator). The configuration is here and there is a small optional patchto avoid outputting warnings due to the slow serial port. An uncompressed kernel image is used instead of a compressed one to have a faster boot. It is generated with "objcopy -O binary vmlinux vmlinux.bin".

The disk image is just a ram disk image loaded at boot time. It contains a filesystem generated with Buildroot containing BusyBox. I added my toy C compiler TinyCC and my unfinished but usable emacs clone QEmacs.



Javascript

I happen to be interested by the implementation of Javascript engines these days - but I don't know yet if I will write my own any time soon ! Anyway, this emulator was a way to learn how to write optimized code for recent Javascript engines, in particular Jaeger Monkey (for Firefox 4) and V8 (for Chrome).

A troubling thing is that the PC emulator is about 2 times slower using V8 than Jaeger Monkey (I used the 32 bit version for both). I have no precise explanation yet because I only looked at the Jeager Monkey code so far.

What's the use ?

I did it for fun, just because newer Javascript Engines are fast enough to do complicated things. Real use could be:


Benchmarking of Javascript engines (how much time takes your Javascript engine to boot Linux ?). For this particular application, efficient handling of 32 bit signed and unsigned integers and of typed arrays is important.

Learning to use command line Unix tools without leaving the browser.

Client side processing using an x86 library, for example for cryptographic purposes. For such application, the x86 emulator can be modified to provide an API to load x86 dynamic libraries and to provide a js-ctypes like API to call the C/C++ functions from javascript.

A more advanced version would allow to use old DOS PC software such as games.
[Back to the PC emulator]