summaryrefslogtreecommitdiff
path: root/content/blog/abaos-intro.md
blob: 9102704aa3c998f6d6c54a4ddc7bceb35364775e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
+++
title = "AbaOS - Intro"
categories = [ "Operating System", "AbaOS", "Development" ]
date = "2017-07-14T10:50:39+02:00"
thumbnail = "/images/blog/abaos-intro/abaos.png"

+++

## History

It started with a question from a collegue at work: "Why
are all operating systems nowadays written in C?".

I wanted to dig deeper...

## Resources

Some years ago it would have been extremly tedious to write
an operating system on your own. Don't get me wrong, it's still
a tremendous task, and I don't think anybody should do it for
the sake of proving the world, he is able to build the best
operating system ever.

PC emulators like [Qemu](http://www.qemu.org) and
[Bochs](http://bochs.sourceforge.net) (to name two open-source ones)
make testing really easy compared to dual-booting.

Tutorials are around everywhere, especially nice are video tutorials
of people who are actually implementing an operating system with
you "live".

I used the following two:

- [Writing a Simple Operating System - from Scratch, Nick Blundell](https://www.youtube.com/watch?v=YvZhgRO7hL4)
- [Write your own Operating System, Viktor Engelmann](http://wyoos.org/)

There are also nice web pages out there collecting stuff for
OS developers, to pick too of them here:

- [OSDEV Wiki](http://wiki.osdev.org/Main_Page)
- [Lowlevel.eu](http://lowelevel.eu)

## Details

I'm using standard C99 with either gcc, clang, tcc or pcc currently for
the C parts. NASM for the assembly parts. No inline-assembly. I want
to see clearly where the CPU/machine-dependent parts are.

One goal is to write a minimal self-hosting operating system. This is
a long way to go. Many tools like make, binutils, nasm, compilers require
some part of a POSIX environment.

So far the simplest explanation I have for everybody using C is simple:

- the operating system has been written in C ages ago
- every possible library is built on top of C and POSIX, and nobody
  wants to rewrite everything all the time
- the most prominent platform is Intel, and Intel has some special
  "features" like I/O ports and alignment, packing of structures which
  requires some low-level features from a language. C has those features
  (alignment and packing). Other platforms may be less quirky and old
  and be less demanding on the language to provide such features.
- open API specification: the Cdecl calling convention is available everywhere.
  The reason why so many scripting languages are popular, is that they
  can reuse tons of libraries via the Cdecl API.
- there is a lot documentation about how to do operating system tasks
  in C, there are not so many around for other languages.

This said, C++, D, Ada, Pascal are valid alternatives for building an
operating system, to name two real-world examples, not just toys:

- C++ has been used for [Haiku](https://www.haiku-os.org)
- Pascal for [Ultibo](https://ultibo.org/) on RaspberryPi

For embedded or special purpose operating systems it's more feasible
to use another language than C. Exokernels especially open up a
new area in operating system development, where relying on the past
is not really that important.

Don't be fooled, if I say C, this doesn't mean you cannot benefit early
from an object oriented design, as soon for instance PCI with loadable drivers
or a widget libary come into play. The following articles are useful
to build simple-inheritance with C:

- http://www.drdobbs.com/extending-c-for-object-oriented-programm/184402731
- https://www.state-machine.com/doc/AN_OOP_in_C.pdf
- https://www.cs.rit.edu/~ats/books/ooc.pdf
- https://dmitryfrank.com/articles/oop_in_c

And, yes, the Linux kernel applies exactly this principle, as we can see
in https://lwn.net/Articles/444910/.

## Status

I'm following the Wyoss guide and I'm trying to do almost everything in
C which was done in C++ in the tutorial. I'm also deviating a little bit
here and there:

- I have my own small little C library (src/libc).
- I wrote a boot loader (yes, I know the comments on OsDev regarding
  writing one). Nevertheless it's informational and has some historic
  value.
- I try to use as many compilers and cross-compilers as possible not
  just one toolchain.

## Sources

As always you can find the sources of the project on my personal git
repository at:

http://git.andreasbaumann.cc/cgit/abaos/