Home
Luke's Weblog - HelloOS, or, Back to basics [entries|archive|friends|userinfo]
Luke Gorrie

[ website | My Website ]
[ userinfo | livejournal userinfo ]
[ archive | journal archive ]

HelloOS, or, Back to basics [Sep. 15th, 2008|04:48 pm]
Previous Entry Add to Memories Tell a Friend Next Entry
[Tags|, , ]

Suppose you wanted to write a really trivial operating system for the OLPC XO, one that just prints Hello, world! without using Linux.

HelloOS 1.0 we write in Forth. That's the simplest and most sensible idea given that the XO already runs everybody's favourite firmware. Create a file called olpc.fth on a USB stick looking something like this:

." Hello, world!" cr
and you're done. Just poke the USB stick into an XO and it's installed.

HelloOS 2.0 is the same thing but written in C, for no good reason at all. This is very easy too because openfirmware ships with a subset libc implementation that gives us printf for free:

main() { printf("Hello, world!\n"); }
we just have to use the right incantations when we compile:
gcc -g -m32 -fno-builtin -fno-stack-limit -fno-stack-protector -c ../../hello/hello.c
ld -melf_i386 -Bstatic -N -Ttext 0x100000 -o hello.elf start.o hello.o libobp.a -lc
cp hello.elf hello.syms
strip hello.elf
and then we have hello.elf. We can stick that on a USB stick and run boot u:\hello.elf and our operating system will print its message.

HelloOS 3.0 is the same but ever-so-slightly more macho. Instead of using premade libc functions we'll call into Forth directly when we want a primitive -- and that's trivially easy too:

main() { OFInterpret0(".\" Hello, world!\" cr"); }
and since we're writing so many operating systems we can do up a snazzy Makefile too:
OFW = /home/luke/hacking/openfirmware
OFW_CLIENT = ${OFW}/clients/lib/x86

SYSTEMS = os1.elf

all: ${SYSTEMS}

%.o: %.c
	${CC} ${CFLAGS} -c $<

%.elf: %.o
	ld -melf_i386 -Bstatic -N -Ttext 0x100000 \
	   -o $@ ${OFW_CLIENT}/start.o $< ${OFW_CLIENT}/libobp.a -lc
	strip $@
and then we start to see how one could write primitives (system calls) in Forth for use by a "user program". That would be quite OSey.

Hurray So there we have it three operating systems in one afternoon. Life sure is grand :-)

LinkReply

Comments:
From: (Anonymous)
2008-09-16 03:44 am (UTC)

HelloOS

(Link)

Great posting, Luke. Thanks a lot for it. Forth is just cool!!! Suits you.

Greetings, Dressguard.
From: (Anonymous)
2008-09-16 05:22 am (UTC)

I like

(Link)

Thanks.. This will be interesting.

I'm starting to think about window managers. That are the only thing I'm missing at the moment. A Forth computer driven by 9v battery. Connected to my old VGA monitor. With a window manager. And when I open my first window, a Forth REPL appear.

/Mats
[User Picture]From: [info]alogic
2008-09-18 12:06 pm (UTC)

(Link)

Good introduction.