“30 Days to Make Your Own OS” on Ubuntu

By far, it is the only English version of recording steps taken to follow the Japanese book.

This post serves the purpose of recording and discussion of my own progress of self-learning. The code is synchronized with my Github repository.

Day 1


Install Image on QEMU on EC2 Ubuntu

Testing images on QEMU is a common task. This section talks about how to install images on QEMU in the linux environment.

Qemu is a visualization emulator running on hosting os (ubuntu in my case). Qemu has a universal distribution with ubuntu. Type qemu- will list a batch of qemu related binaries, however qemu commend itself is not enlisted.

P.S. I tried to install from qemu official git. It ended up with configuration failures with my EC2 ubuntu.

Turing to the default qemu with Ubuntu, use qemu-system-x86_64 to replace qemu.

qemu-system-x86_64 -fda workstation/OSASK-Linux/src/helloos.img

return error

Could not access KVM kernel module: No such file or directory
failed to initialize KVM: No such file or directory
Back to tcg accelerator.
Failed to allocate 402653184 B: Cannot allocate memory
Aborted (core dumped)

There are two errors, no KVM and no enough memory.

From KVM install post, try egrep -c '(vmx|svm)' /proc/cpuinfo which returns `0`, meaning CPU does not support visualization. Therefore, I need to enable flag `-no-kvm` to disable hardware acceleration. Note, qemu is independent of kvm.

For memory allocation, use flag `-m 32` to limit 32MB memory.

qemu-system-x86_64 -m 32 -no-kvm -fda workstation/OSASK-Linux/src/helloos.img

return error

Could not initialize SDL(No available video device) – exiting

Qemu by default export visualization through vga, but since running on ec2 instance, there is no vga device. Flag `-curses` can be used to direct output to std with no graphics. However, for the rest of practice, it is necessary to have a graphical UI. Flag `-vnc` is used to redirect output to a vnc4server program.

sudo apt-get install gnome-core
sudo apt-get install vnc4server
vim .vnc/xstartup

qemu-system-x86_64 -vnc -m 32 -no-kvm -fda workstation/OSASK-Linux/src/helloos.img

Open a vnc client (tightvnc in my case) and connect to the ec2 instance. Be sure to enable port 5901 in the aws security group.

Using NASM to Compile Assembly

Install NASM (rather than author’s own NASM version NASK for better support)

sudo apt-get install nasm
nasm -f elf64 helloos.nas -o helloos_nas.img
vi helloos_nas.img :%!xxd
dd if=helloos_nas.img of=helloos_nas.img bs=1c skip=384
tail -c +385 helloos_nas.img > helloos.img

By nasm -hf, we know that NASM support linux x86_64 (Executable and Linkable Format).0x
helloos.nas is the assembly code and we output its corresponding binary image.
When using vi with mode xxd, we find that NASM appended 0x180 bytes as elf header to the actual image data.
We need to remove the header by using either `dd` or `tail`. Latter one is much faster than the old school approach.
P.S. the idea was from the post in Japanese.
Note, 0x180 in decimal is 384.

After that, I did a normal deployment of helloos.img with vnc support.

Code some real assembly

Following the book should not cause too many problems, except that two points.

One is that the comments for the code is not well understood for me, I could find more information about DOS’s boot protocols in the links.

Second point is that when I compiled the code using NASM, it threw error “error: invalid operand type” at the line of RESB 0x1fe-$. After reading NASM manual, it said that RESB takes critical expression only, and since `$` could not be determined at the first pass, the number of bytes to reserve was unknown for second pass. I followed the answer. Basically, I added labels at the start and the targeted places, so that I can use `$` to calculate the `relative` position of current location, and using a valuable `len` to be used in `RESE` statement.


    • echo -ne '\x09\x65' | iconv -f utf-8 //check what hex means in UTF-8, some illegal char may appear.
    • NASM ignores white spaces, but for readability, I indented twice in between label, command, values and comments to give enough space. File `.vimr` is created at home directory, and set tabstop=4. Otherwise, I need to type :set ts=4 each time I log in vi.
    • vi mode :set nu is useful for debugging
    • ctrl-v I `editing one example` ESC is useful for repeating same inserting operation for multiple lines, e.g. add tabs to multiple lines.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: