2 <img width="auto" src="docs/img/lunaix-os-logo.png">
6 <a href="docs/README.cn.md">简体中文</a> | <a href="#the-lunaixos-project">English</a>
11 The Lunaix kernel (or soon-to-be LunaixOS) is a hobby kernel, started in 2022,
12 written entirely from scratch. With a big ambition of being POSIX-compliance,
13 and designed with performance and modernity in mind, with some independent
14 thoughts being applied in architectural design.
17 This project is built entirely from first principles - meaning no code was
18 copy-pasted or recycled from other OS projects or tutorials. Lunaix didn't begin
19 as an attempt to outperform anything; it started as a personal challenge to
20 learn OS theory and explore advanced kernel features through original design
21 and implementation. Everything you see here was developed solo, part-time,
22 with only hardware specs and my beloved **Modern Operating Systems** as guidance.
24 If you're a kernel hobbyist looking for a fresh take or tired of mass-produced, tutorial-style
25 projects, then welcome, you're in the right place!
28 |  |
30 | *Lunaix over serial* (`console=/dev/ttyS0`) |
32 |  |
34 | *Lunaix over VGA text mode* (`console=/dev/ttyVCON0`) |
36 > 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!)
40 Lunaix is a multi-architecture, general-purpose kernel built with performance,
41 modularity, and robustness in mind. Its design emphasizes advanced abstractions,
42 proactive error detection, and subsystem isolation. Lunaix is
44 + **Fully-preemptive** for responsive multi-tasking
45 + **Modular** with compile-time configurable components and extensible
47 + **High-performance**, leveraging modern caching strategies and efficient
49 + **Fault-tolerant**, with built-in error handling and stack backtracing
50 + **Robust by design**, using mechanisms like proactive deadlock detection
53 A significant amount of effort has gone into crafting clean abstractions,
54 implementing advanced kernel features, and applying performance optimizations
55 throughout the system.
57 To better illustrate the scope of work already done, the following non-exhaustive
58 list outlines currently supported features in Lunaix:
60 ### List of All Features
63 <summary>Click to Expand</summary>
70 + abstraction for different protocol
71 + configurable kernel boot-time parameters
72 + Platform resource management and definition
73 + read-only ACPI table interpretation
74 + full devicetree implementation
76 + architecture-neutral abstraction
83 + reverse mapping (rmap)
84 + memory compaction (WIP)
85 + slab-style object allocator
86 + inter-process address space access
93 + round-robin scheduler (for now)
94 + kernel level multi-tasking (i.e. kernel threads)
95 + taskfs: file system interface to process and threads
97 + POSIX-compliant interface
98 + virtual file system framework
99 + file system mounting mechanism
100 + page cache for file IO
101 + inode/dnode caching
102 + ext2 (rev.0, rev.1)
103 + iso9660 (rock-ridge)
104 + twifs: kernel state fs interface.
105 + Device management and interrupt handling
106 + unified IRQ framework
107 + unified driver framework for heterogenous devices
108 + modular driver model allow compiled-time toggling
109 + asynchronous operation model supported
110 + devfs: device fs interface.
112 + unified block IO interface
113 + IO request packets caching
114 + asynchronous IO operation
116 + POSIX-compliant serial IO model
117 + Caching Infrastructure
118 + LRU replacement policy and pooling
119 + kernel daemon for dynamic and transparent cache managements
120 + Error handling and detection
121 + stack backtracing with symbol resolution
122 + stack unwinding for nested exception
124 + deadlock/hung-up detection
128 ### List of Built-in Drivers
132 <summary>Click to Expand</summary>
134 + Architecture Neutral
135 + UART 16650-compatible driver
142 + IOAPIC IRQ controller
144 + Legacy i8042 keyboard controller
152 There are documentations avaliable if you want to dig deeper. Keep in mind they are still work in progress and missing localisation.
154 + [Luna's Tour - Into Lunaix's Inner Workings](docs/lunaix-internal.md)
155 + [Syscall Number Assignments](docs/lunaix-syscall-table.md)
159 Building lunaix is simple, no more bloated dependencies to install, basic
160 `build-essentials` installation plus a python are sufficient.
162 + gcc (recommend v12+)
165 + python (recommend v3.11+)
167 And also one should have environment variable `ARCH=<arch>` exported, where
168 `<arch>` is one of the supported arhcitecture (`x86_32`, `x86_64`, `aarch64`).
170 For cross compilation, also export `CX_PREFIX` to the gcc prefix for the
171 corresponding `<arch>`.
173 The following `make` actions are then available to use.
175 | Make command | Usage |
177 | `make all` | Build the kernel bin |
178 | `make rootfs` | Build the stock rootfs |
179 | `make clean` | clean |
180 | `make config` | run configuration tool |
181 | `make reconfig` | revert to default and restart configuration |
183 A successful build will give `build/bin/kernel.bin`.
185 > Please note: this is the kernel, not a bootable image, it require a bootloader
186 > to boot and specify the rootfs.
190 If you are impatient, or just want something to run and don't want to went
191 through tedious process of configuring rootfs and tweak kernel parameters. You
192 can use the `live_debug.sh` provided in the lunaix root directory to quickly
193 bring up the system with default parameters (also used by the author for
198 This will get you up and running real quick. We will use `x86_64` as example.
200 Assuming a Linux or other Unix-like shell environment. We also expect you to be able to sort out trivial issues like missing packages.
202 **Select your target**
212 Ensure at least `3.11`
219 Ensure `x86_64-linux-gnu` or anything resemble `x86_64`
223 $ which qemu-system-x86_64
226 Should display a valid installation path
228 **Optional: Setting up Cross-Compiler**
231 $ export CX_PREFIX=x86_64-linux-gnu-
234 **Run Configuration**
239 Then hitting `q` in the interactive shell to accept default setting.
241 **Build stock rootfs**
247 Note, this requires root for creating fs image. You can check script `lunaix-os/scripts/mkrootfs` if you feel uncertain.
254 you should see gdb now take control of your shell
256 **Connect to serial via telnet**
258 Open up another window or session
260 telnet localhost 12345
263 **Commence simulation**
265 Back to the gdb session and type `c` to countine
268 **Watch Lunaix booting!**
270 Congrats, enjoy your lunaix! (or submit an issue)
273 ### Not so Quick Start
275 Here is a slower and yet more verbose steps:
277 1. Select an architecture `<arch>`
278 2. Check the compilation prerequisites and presence of `qemu-system-<arch>`
279 3. Optionally export `CX_PREFIX` if you are building for another architecture.
280 4. Run `make ARCH=<arch> rootfs` to build stock rootfs image, require support
281 of `dd`,`mkfs.ext2`, `mount -o loop`, `mktemp`.
282 5. Run `ARCH=<arch> live_debug.sh` to boot in QEMU with gdb hooked (one should
284 6. telnet to `localhost:12345`, this is QEMU emulated serial port
285 7. type `c` in the active gdb session and commence emulation.
286 8. Congrats, enjoy your lunaix!
290 ## Booting the kernel
292 Since lunaix is a kernel, much like linux. It requires additional setup to do
293 the magic. And, as in "much like linux", methods to make linux kernel boot can
294 also apply to lunaix without or with little translation, as we will discuss
297 The bootloader part is generic, any bootloader, for example GRUB will work (not
298 tested for UEFI, but I expect this would be an exception), or booting up in QEMU
299 using `-kernel` option
301 The kernel command line, is however, a bit differentiated.
302 The syntax is similar, both takes form of space-separated array of `<key>=<val>`
303 pairs or boolean `<flag>`.
305 Currently, lunaix support the following options
307 | Option | Default Value | Optional | Usage |
308 | ------ | ---- | ----- | ---- |
309 | console | `/dev/ttyS0` | No | Specify the system console device, path within lunaix's devfs |
310 | rootfs | `/dev/block/sda` | No | Specify the device contain rootfs image, path within lunaix's devfs |
311 | init | `/init` | Yes | Path within rootfs of the `init` |
316 If one ran into bug, one can submit an issue by filling up the following template
319 1. Describe the problem
320 "How does it look like, anything descriptive: visual, sonic, emotional experience"
322 2. Steps to reproduce
323 "How you ran into this mess?"
325 3. Expected behaviour
326 "What do you intended/expected to achieve/to be"
328 4. Lunaix's panic trace (if applicable)
330 5. Other clues that you think might be helpful
336 The development process is still in motion, any limitation can be categorised as
337 a feature yet to be. However, some features that the author considered to be the
338 most urgent and wish the matters to be discussed.
340 Lunaix is under impression of uniprocessor and not capable of running in SMP
341 environment. This is major held back of being a modern operating system. It has
342 the highest priority among all other tasks
344 Lunaix do not have a mature (or even, an infant) user space ecosystem, mainly
345 because the lack of a proper and sophisticated libc. Efforts need to be done for
346 porting one to the target. However, given the author's tight schedule, this task
347 is unfortunately still beyond the horizon.
351 Albeit one must realise that the author has mentioned it in the very beginning,
352 the author would like to emphaise **again** on the nature of this project.
354 As a personal challenge, this project is independently developed by the author
355 single-handly, which means:
357 + No reference to existing tutorials, books, online courses or any open source
358 project that might provide any example, hint or working prototype on the
359 design and implementation of kernel, subsystems or anythings that can be
360 contributed towards a working prototype.
361 + The author has no prior knowledge on Linux kernel through out 90% of the
363 + All knowledge on the kernel design is coming from the basic textbook on
364 operating system theory, that is, *Modern Operating System* by Tanenbaum.
365 + All knowledge on the system programming is coming from the basic textbook,
366 that is, *Computer System - A Programmer's Perspective Third Edition*
367 + All knowledge on the generic framework design and driver development are
368 ingested from various technical specifications gathered across the Internet.
372 + Intel 64 and IA-32 Architecture Software Developer's Manual (Full Volume Bundle)
373 + ACPI Specification (version 6.4)
374 + Devicetree Specification
375 + ARM® Generic Interrupt Controller (v3)
376 + Arm® Architecture Reference Manual (Profile-A)
377 + Procedure Call Standard for the Arm® 64-bit Architecture (AArch64)
378 + IBM PC/AT Technical Reference
379 + IBM VGA/XGA Technical Reference
380 + 82093AA I/O Advanced Programmable Controller (IOAPIC) (Datasheet)
381 + MC146818A (Datasheet)
382 + Intel 500 Series Chipset Family Platform Controller Hub (Datasheet - Volume 2)
383 + PCI Local Bus Specification, Revision 3.0
384 + PCI Express Base Specification, Revision 1.1
385 + PCI Firmware Specification, Revision 3.0
386 + Serial ATA - Advanced Host Controller Interface (AHCI), Revision 1.3.1
387 + Serial ATA: High Speed Serialized AT Attachment, Revision 3.2
388 + SCSI Command Reference Manual
389 + ATA/ATAPI Command Set - 3 (ACS-3)
391 + Rock Ridge Interchange Protocol (RRIP: IEEE P1282)
392 + System Use Sharing Protocol (SUSP: IEEE P1281)
393 + Tool Interface Standard (TIS) Portable Formats Specification (Version 1.1)
394 + *Computer System - A Programmer's Perspective Third Edition* (Bryant, R & O'Hallaron, D), a.k.a. CS:APP
395 + *Modern Operating System* (Tanenbaum, A)
396 + Free VGA, http://www.osdever.net/FreeVGA/home.htm
397 + GNU CC & LD online documentation.
398 + PCI Lookup, https://www.pcilookup.com/
401 ## Appendix 1: Supported System Call<a id="appendix1"></a>
403 Refer to [Lunaix Syscall Table](docs/lunaix-syscall-table.md)
405 ## Appendix 2: Debugging with GDB remotely via UART
407 **(((( Broken after a refactoring years ago, need rework ))))**
410 <summary> Click to expand </summary>
411 The LunaixOS kernel comes with a built-in GDB debugging server, which runs on
412 COM1@9600Bd. However, LunaixOS must be in debug mode before involving GDB.
414 One could trigger the debug mode by writing a byte sequence `0x40` `0x63` `0x6D`
415 `0x63`, to the same serial port. A text "DEBUG MODE" with magenta-coloured background shall be present at the bottom of the screen.
417 Note that, whenever the text appears, the LunaixOS always halt all activities
418 other than the debugging server, which means no scheduling and no external
419 interrupt servicing. Users are now recommended to attach their GDB and drive
420 the kernel with the debugging workflow.
422 Currently, LunaixOS implements the required minimal server-side command subset
423 required by GDB Remote Protocol, namely, `g`, `G`, `p`, `P`, `Q`, `S`, `k`, `?`,
424 `m`, `M`, `X`. Which should be enough to cover most debugging activities.
426 When debugging is finished, one shall disconnect with `kill` command. This
427 command will not force LunaixOS to power down the computer, instead it just
428 resume the execution (identical behavior as `c` command). However, disconnecting
429 does not means exiting of debug mode. The debug mode is still actived and any
430 subsequent GDB attaching request shall remain the highest priority amongst all
431 other activity. One shall deactivate the debug mode by writing byte sequence
432 `0x40` `0x79` `0x61` `0x79` to the port, after GDB detached.
436 Currently, one should avoid the use of `info stack`, `bt` or any other command
437 that involves stack unwinding or stack backtracing. As it will somehow corrupt
438 the stack layout and result in undefined behaviour. This issue should be
439 addressed in future releases.