update readme introduction part, add demo screen-shot
[lunaix-os.git] / README.md
1 <p align="center">
2   <img width="auto" src="docs/img/lunaix-os-logo.png">
3 </p>
4
5 <p align="center">
6   <a href="docs/README.cn.md">简体中文</a> | <a href="#the-lunaixos-project">English</a>
7 </p>
8
9 # The Lunaix Project
10
11 The Lunaix kernel (or soon-to-be LunaixOS) is a hobby kernel, written entirely from scratch. Designed to be POSIX-compliance, performance critical and modern, with some independent thoughts being applied in architectural design.
12
13 This project is based solely on first principles. Meaning that it does neither copy-pasting nor recycling other os projects/tutorials. However, this doesn't necessarily imply superiority; In fact, Lunaix starts out as a mean to challenge my understanding in OS theory and also a platform for experimenting some advanced (and cool) kernel features. If you are a kernel hobbyist who want some new perspectives or just simply get fatigued on those recycled content, then you have came to the right place!
14
15
16 | ![showcase_lunaix-over-serial.png](docs/img/showcase_lunaix-over-serial.png) | ![showcase_lunaix-over-serial.png](docs/img/showcase_lunaix-over-vga-text.png) |
17 |:--:| :--:| 
18 | *Lunaix over serial* | *Lunaix over VGA text mode* |
19
20 > By the way, do you know there is an online video course  by the author on the design of lunaix? [Check it out](https://space.bilibili.com/12995787/channel/collectiondetail?sid=196337) (although it is in Chinese!)
21
22 ## Features
23
24 In a nutshell, Lunaix is a multi-architectural general purpose kernel, and she is:
25
26 + fully-preemptive
27 + modular design with configurable components at compile-time and extendable subsystems
28 + high-performance by utilising advanced caching techniques and infrastructure.
29 + fault-tolerance with sophisticated builtin error handling and tracing techniques.
30 + robust in nature with techniques such as proactive deadlock detection and driver isolation mechanism.
31
32 The author has put a significant amount of time on devising better abstractions, advance kernel features and various optimisation techniques. To give a better understanding (and appreciation) of the works being done, the following non-exhaust list has been compiled with features that are currently supported in lunaix:
33
34 + Multi-ISA
35   + x86_32
36   + x86_64
37   + Aarch64 (W.I.P)
38 + Boot protocol
39   + abstraction for different protocol
40   + configurable kernel boot-time parameters
41 + Platform resource management and definition
42   + ACPI
43   + Devicetree
44 + Memory management
45   + architecture-neutral abstraction
46   + on-demand paging
47   + copy-on-write and page sharing
48   + compound page support
49   + explicit huge page support (sorry, not THP!)
50   + reverse mapping indexing (rmap)
51   + memory compaction (W.I.P)
52   + slab-alike object allocator
53   + highmem support
54   + remote address space accessing
55 + Multi-tasking
56   + Protection level and process image isolation
57   + Native threading support (no more lightweight process nonsense)
58   + Signal mechanism
59   + Kernel level multi-tasking (i.e. kernel threads)
60   + Round-robin scheduling (for now)
61   + Preemptive kernel design
62   + taskfs: file system interface to process and threads
63 + File system
64   + virtual file system framework
65   + ...with POSIX compliant interfaces
66   + file system mounting mechanism
67   + page cache for file IO
68   + node cache for vfs structure representation.
69   + ext2 (rev.0, rev.1)
70   + iso9660 (rock-ridge)
71   + twifs: file system interface to kernel states.
72 + Device management and interrupt handling
73   + architecture-neutral design
74   + generalised driver framework
75   + generalised irq framework
76   + driver modularisation design
77   + support asynchronous device model
78   + devfs: file system interface to device subsystem
79 + Block I/O (blkio)
80   + generalised block IO interface and encapsulation
81   + blkio packets caching
82   + asynchronous blkio operation in nature
83 + Serial I/O
84   + POSIX-compliant serial port model
85   + serial device driver framework (part of driver framework)
86 + Caching Infrastructure
87   + primitive: generic sparse associative array (spatial data)
88   + LRU replacement policy and pooling
89   + kernel daemon for scheduled cache eviction
90 + Error handling and detection
91   + stack back-tracing with symbol resolution
92   + nested exception unfolding
93   + CPU state dumping
94   + Deadlock/hung-up detection
95
96 For the device drivers that are currently support see below:
97
98 + Arhcitecture Neutral
99   + UART 16650-compatible driver
100   + Serial ATA AHCI
101   + PCI 3.0
102   + PCIe 1.1
103   + Standard VGA
104 + Intel x86
105   + RTC (Intel PCH)
106   + IOAPIC irq controller
107   + APIC Timer
108   + Legacy i8042 keyboard controller
109 + ARM
110   + GICv3
111   + PL011 (W.I.P)
112
113 ## Project Structure
114
115 | Path | Description |
116 |-----|------|
117 | [lunaix-os](../lunaix-os/) | LunaixOS source code |
118 | [slides](../slides/) | Slides used in my videos |
119 | [reference-material](../reference-material/)| References |
120
121 ## Compile and Build
122
123 Building lunaix is simple, no more bloated dependencies to install, basic `build-essentials` installation plus a python are sufficient.
124
125 + gcc (recommend v12+)
126 + binutils
127 + make
128 + python (recommend v3.11+)
129
130 And also one should have environment variable `ARCH=<arch>` exported, where `<arch>` is one of the supported arhcitecture (`x86_32`, `x86_64`, `aarch64`).
131
132 For cross compilation, also export `CX_PREFIX` to the gcc prefix for the corresponding `<arch>`.
133
134 The following `make` actions are then available to use.
135
136 | Make command | Usage |
137 | ---- | ---- |
138 | `make all`               | Build the kernel bin |
139 | `make rootfs`            | Build the stock rootfs |
140 | `make clean`             | clean |
141 | `make config`            | menuconfig |
142
143 A successful build will give `build/bin/kernel.bin`.
144
145 **Please note: this is the kernel, not a bootable image, it require a bootloader to boot and specify the rootfs.**
146
147 ## Booting the kernel
148
149 Since lunaix is a kernel, much like linux. It requires additional setup to do the magic. And, as in "much like linux", methods to make linux kernel boot can also apply to lunaix without or with little translation, as we will discuss below.
150
151 The bootloader part is generic, any bootloader, for example GRUB will work (not tested for UEFI, but I expect this would be an exception), or booting up in QEMU using `-kernel` option
152
153 The kernel command line, is however, a bit differentiated.
154 The syntax is similar, both takes form of space-separated array of `<key>=<val>` pairs or boolean `<flag>`.
155
156 Currently, lunaix support the following options
157
158 | Option | Default Value | Optional | Usage |
159 | ------ | ----          |  -----   | ----  |
160 | console | `/dev/ttyS0`   | No | Specify the system console device, path within lunaix's devfs |
161 | rootfs | `/dev/block/sda` | No | Specify the device contain rootfs image, path within lunaix's devfs |
162 | init | `/init` | Yes | Path within rootfs of the `init` |
163
164 ### A quick 'Get Started'
165
166 One can use the `live_debug.sh` provided in the lunaix root directory to quickly bring up the system with default parameters (also used by the author for debugging).
167
168 Following the steps:
169
170 1. Select an architecture `<arch>`
171 2. Check the compilation prerequisites and presence of `qemu-system-<arch>`
172 3. Run `make ARCH=<arch> user` to build the stock user program
173 4. Run `make ARCH=<arch> rootfs` to build stock rootfs image, require support of `dd`,`mkfs.ext2`, `mount -o loop`, `mktemp`.
174 5. Run `ARCH=<arch> live_debug.sh` to boot in QEMU with gdb hooked (one should see a gdb session)
175 6. telenet to `localhost:12345`, this is QEMU emulated serial port
176 7. type `c` in the active gdb session and commence emulation.
177 8. Congrats, enjoy your lunaix!
178 (or submit an issue)
179
180
181 ## Submit an Issue
182
183 If one ran into bug, one can submit an issue by filling up the following template
184
185 ```
186 1. Describe the problem
187     "How does it look like, anything descriptive: visual, sonic, emotional experience"
188
189 2. Steps to reproduce
190     "How you ran into this mess?"
191
192 3. Expected behaviour
193     "What do you intended/expected to achieve/to be"
194
195 4. Lunaix's panic trace (if applicable)
196
197 5. Other clues that you think might be helpful
198 ```
199
200
201 ## Limitations
202
203 The development process is still in motion, any limitation can be categorised as a feature yet to be. However, some features that the author considered to be the most urgent and wish the matters to be discussed.
204
205 Lunaix is under impression of uniprocessor and not capable of running in SMP environment. This is major held back of being a modern operating system. It has the highest priority among all other tasks
206
207 Lunaix do not have a mature (or even, an infant) user space ecosystem, mainly because the lack of a proper and sophisticated libc. Efforts need to be done for porting one to the target. However, given the author's tight schedule, this task is unfortunately still beyond the horizon.
208
209 ## Acknowledgement
210
211 Albeit one must realise that the author has mentioned it in the very beginning, the author would like to emphaise **again** on the nature of this project.
212
213 As a personal challenge, this project is independently developed by the author single-handly, which means:
214
215 + No reference to existing tutorials, books, online courses or any open source project that might provide any example, hint or working prototype on the design and implementation of kernel, subsystems or anythings that can be contributed towards a working prototype.
216 + The author has no prior knowledge on Linux kernel through out 90% of the project time.
217 + All knowledge on the kernel design is coming from the basic textbook on operating system theory, that is, *Modern Operating System* by Tanenbaum.
218 + All knowledge on the system programming is coming from the basic textbook, that is, *Computer System - A Programmer's Perspective Third Edition*
219 + All knowledge on the generic framework design and driver development are ingested from various technical specifications gathered across the Internet.
220
221 ## References
222
223 + Intel 64 and IA-32 Architecture Software Developer's Manual (Full Volume Bundle)
224 + ACPI Specification (version 6.4)
225 + Devicetree Specification
226 + ARM® Generic Interrupt Controller (v3)
227 + Arm® Architecture Reference Manual (Profile-A)
228 + Procedure Call Standard for the Arm® 64-bit Architecture (AArch64)
229 + IBM PC/AT Technical Reference
230 + IBM VGA/XGA Technical Reference
231 + 82093AA I/O Advanced Programmable Controller (IOAPIC) (Datasheet)
232 + MC146818A (Datasheet)
233 + Intel 500 Series Chipset Family Platform Controller Hub (Datasheet - Volume 2)
234 + PCI Local Bus Specification, Revision 3.0
235 + PCI Express Base Specification, Revision 1.1
236 + PCI Firmware Specification, Revision 3.0
237 + Serial ATA - Advanced Host Controller Interface (AHCI), Revision 1.3.1
238 + Serial ATA: High Speed Serialized AT Attachment, Revision 3.2
239 + SCSI Command Reference Manual
240 + ATA/ATAPI Command Set - 3 (ACS-3)
241 + ECMA-119 (ISO9660)
242 + Rock Ridge Interchange Protocol (RRIP: IEEE P1282)
243 + System Use Sharing Protocol (SUSP: IEEE P1281)
244 + Tool Interface Standard (TIS) Portable Formats Specification (Version 1.1)
245 + *Computer System - A Programmer's Perspective Third Edition* (Bryant, R & O'Hallaron, D), a.k.a. CS:APP
246 + *Modern Operating System* (Tanenbaum, A)
247 + Free VGA, http://www.osdever.net/FreeVGA/home.htm 
248 + GNU CC & LD online documentation.
249 + PCI Lookup, https://www.pcilookup.com/
250 + Linux man pages
251
252 ## Appendix 1: Supported System Call<a id="appendix1"></a>
253
254 **Unix/Linux/POSIX**
255
256 1. `sleep(3)`
257 1. `wait(2)`
258 1. `waitpid(2)`
259 1. `fork(2)`
260 1. `getpid(2)`
261 1. `getppid(2)`
262 1. `getpgid(2)`
263 1. `setpgid(2)`
264 1. `brk(2)`
265 1. `sbrk(2)`
266 1. `_exit(2)`
267 1. `sigreturn(2)`
268 1. `sigprocmask(2)`
269 1. `signal(2)`
270 1. `kill(2)`
271 1. `sigpending(2)`
272 1. `sigsuspend(2)`
273 2. `read(2)`
274 2. `write(2)`
275 2. `open(2)`
276 2. `close(2)`
277 2. `mkdir(2)`
278 2. `lseek(2)`
279 2. `readdir(2)`
280 2. `readlink(2)`※
281 2. `readlinkat(2)`※
282 2. `rmdir(2)`※
283 2. `unlink(2)`※
284 2. `unlinkat(2)`※
285 2. `link(2)`※
286 2. `fsync(2)`※
287 2. `dup(2)`
288 2. `dup2(2)`
289 2. `symlink(2)`※
290 2. `chdir(2)`
291 2. `fchdir(2)`
292 2. `getcwd(2)`
293 2. `rename(2)`※
294 2. `mount(2)`
295 2. `unmount` (a.k.a `umount(2)`)※
296 2. `getxattr(2)`※
297 2. `setxattr(2)`※
298 2. `fgetxattr(2)`※
299 2. `fsetxattr(2)`※
300 2. `ioctl(2)`
301 2. `getpgid(2)`
302 2. `setpgid(2)`
303 2. `mmap(2)`
304 2. `munmap(2)`
305 2. `execve(2)`
306 3. `poll(2)` (via `pollctl`)
307 3. `epoll_create(2)` (via `pollctl`)
308 3. `epoll_ctl(2)` (via `pollctl`)
309 3. `epoll_wait(2)` (via `pollctl`)
310 4. `pthread_create`
311 4. `pthread_self`
312 4. `pthread_exit`
313 4. `pthread_join`
314 4. `pthread_kill`
315 4. `pthread_detach`
316 4. `pthread_sigmask`
317
318 **LunaixOS**
319
320 1. `yield`
321 2. `geterrno`
322 3. `realpathat`
323
324 ( **※**:Indicate syscall is not tested )
325
326 ## Appendix 2: Debugging with GDB remotely via UART
327
328 **(((( Not working yet, need rework ))))**
329
330 The LunaixOS kernel comes with a built-in GDB debugging server, which runs on COM1@9600Bd. However, LunaixOS must be in debug mode before involving GDB.
331
332 One could trigger the debug mode by writing a byte sequence `0x40` `0x63` `0x6D` `0x63`, to the same serial port. A text "DEBUG MODE" with magenta-coloured background shall be present at the bottom of the screen.
333
334 Note that, whenever the text appears, the LunaixOS always halt all activities other than the debugging server, which means no scheduling and no external interrupt servicing. Users are now recommended to attach their GDB and drive the kernel with the debugging workflow.
335
336 Currently, LunaixOS implements the required minimal server-side command subset required by GDB Remote Protocol, namely, `g`, `G`, `p`, `P`, `Q`, `S`, `k`, `?`, `m`, `M`, `X`. Which should be enough to cover most debugging activities.
337
338 When debugging is finished, one shall disconnect with `kill` command. This command will not force LunaixOS to power down the computer, instead it just resume the execution (identical behavior as `c` command). However, disconnecting does not means exiting of debug mode. The debug mode is still actived and any subsequent GDB attaching request shall remain the highest priority amongst all other activity. One shall deactivate the debug mode by writing byte sequence `0x40` `0x79` `0x61` `0x79` to the port, after GDB detached.
339
340 ### Limitations
341
342 Currently, one should avoid the use of `info stack`, `bt` or any other command that involves stack unwinding or stack backtracing. As it will somehow corrupt the stack layout and result in undefined behaviour. This issue should be addressed in future releases.