add "-fno-stack-protector" to explicit disable unwanted instrumentation
[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, 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.
15
16
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.
23
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!
26
27
28 | ![showcase_lunaix-over-serial.png](docs/img/showcase_lunaix-over-serial.png) |
29 |:--:|
30 | *Lunaix over serial* (`console=/dev/ttyS0`) |
31
32 | ![showcase_lunaix-over-serial.png](docs/img/showcase_lunaix-over-vga-text.png) |
33 |:--:|
34 | *Lunaix over VGA text mode* (`console=/dev/ttyVCON0`) |
35
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!)
37
38 ## Features
39
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
43
44 + **Fully-preemptive** for responsive multi-tasking
45 + **Modular** with compile-time configurable components and extensible 
46   subsystems
47 + **High-performance**, leveraging modern caching strategies and efficient 
48   infrastructures.
49 + **Fault-tolerant**, with built-in error handling and stack backtracing
50 + **Robust by design**, using mechanisms like proactive deadlock detection 
51   and driver isolation
52
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.
56
57 To better illustrate the scope of work already done, the following non-exhaustive 
58 list outlines currently supported features in Lunaix:
59
60 ### List of All Features
61 <details>
62
63 <summary>Click to Expand</summary>
64
65 + Multi-ISA
66   + x86_32
67   + x86_64
68   + Aarch64 (WIP)
69 + Boot protocol
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
75 + Memory management
76   + architecture-neutral abstraction
77   + highmem
78   + copy-on-write
79   + page sharing
80   + explicit huge page
81   + on-demand paging
82   + compound page
83   + reverse mapping (rmap)
84   + memory compaction (WIP)
85   + slab-style object allocator
86   + inter-process address space access
87 + Multi-tasking
88   + fully preemptive
89   + protection levels
90   + process isolation
91   + native threading
92   + signal mechanism
93   + round-robin scheduler (for now)
94   + kernel level multi-tasking (i.e. kernel threads)
95   + taskfs: file system interface to process and threads
96 + File system
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.
111 + Block I/O (blkio)
112   + unified block IO interface
113   + IO request packets caching
114   + asynchronous IO operation
115 + Serial I/O
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
123   + CPU state dumping
124   + deadlock/hung-up detection
125
126 </details>
127
128 ### List of Built-in Drivers
129
130 <details>
131
132 <summary>Click to Expand</summary>
133
134 + Architecture Neutral
135   + UART 16650-compatible driver
136   + Serial ATA AHCI
137   + PCI 3.0
138   + PCIe 1.1
139   + Standard VGA
140 + Intel x86
141   + RTC (Intel PCH)
142   + IOAPIC IRQ controller
143   + APIC Timer
144   + Legacy i8042 keyboard controller
145 + ARM
146   + GICv3
147   + PL011 (WIP)
148 </details>
149
150 ## Documentations
151
152 There are documentations avaliable if you want to dig deeper. Keep in mind they are still work in progress and missing localisation.
153
154 + [Luna's Tour - Into Lunaix's Inner Workings](docs/lunaix-internal.md)
155 + [Syscall Number Assignments](docs/lunaix-syscall-table.md)
156
157 ## Compile and Build
158
159 Building lunaix is simple, no more bloated dependencies to install, basic 
160 `build-essentials` installation plus a python are sufficient.
161
162 + gcc (recommend v12+)
163 + binutils
164 + make
165 + python (recommend v3.11+)
166
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`).
169
170 For cross compilation, also export `CX_PREFIX` to the gcc prefix for the 
171 corresponding `<arch>`.
172
173 The following `make` actions are then available to use.
174
175 | Make command | Usage |
176 | ---- | ---- |
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 |
182
183 A successful build will give `build/bin/kernel.bin`.
184
185 > Please note: this is the kernel, not a bootable image, it require a bootloader 
186 > to boot and specify the rootfs.
187
188 ## Get Started
189
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 
194 debugging).
195
196 ### Quick Start
197
198 This will get you up and running real quick. We will use `x86_64` as example.
199
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.
201
202 **Select your target**
203 ```sh
204 $ export ARCH=x86_64
205 ```
206
207 **Check Python**
208 ```sh
209 $ python --version
210 ```
211
212 Ensure at least `3.11`
213
214 **Check compiler**
215 ```sh
216 $ gcc -dumpmachine
217 ```
218
219 Ensure `x86_64-linux-gnu` or anything resemble `x86_64`
220
221 **Check QEMU**
222 ```sh
223 $ which qemu-system-x86_64
224 ```
225
226 Should display a valid installation path
227
228 **Optional: Setting up Cross-Compiler**
229
230 ```sh
231 $ export CX_PREFIX=x86_64-linux-gnu-
232 ```
233
234 **Run Configuration**
235 ```sh
236 make config
237 ```
238
239 Then hitting `q` in the interactive shell to accept default setting.
240
241 **Build stock rootfs**
242
243 ```sh
244 make rootfs
245 ```
246
247 Note, this requires root for creating fs image. You can check script `lunaix-os/scripts/mkrootfs` if you feel uncertain.
248
249 **Build & Run**
250 ```sh
251 ./live_debug.sh
252 ```
253
254 you should see gdb now take control of your shell
255
256 **Connect to serial via telnet**
257
258 Open up another window or session
259 ```sh
260 telnet localhost 12345
261 ```
262
263 **Commence simulation**
264
265 Back to the gdb session and type `c` to countine
266
267
268 **Watch Lunaix booting!**
269
270 Congrats, enjoy your lunaix! (or submit an issue)
271
272
273 ### Not so Quick Start
274
275 Here is a slower and yet more verbose steps:
276
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 
283    see a gdb session)
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!
287 (or submit an issue)
288
289
290 ## Booting the kernel
291
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 
295 below.
296
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
300
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>`.
304
305 Currently, lunaix support the following options
306
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` |
312
313
314 ## Submit an Issue
315
316 If one ran into bug, one can submit an issue by filling up the following template
317
318 ```
319 1. Describe the problem
320     "How does it look like, anything descriptive: visual, sonic, emotional experience"
321
322 2. Steps to reproduce
323     "How you ran into this mess?"
324
325 3. Expected behaviour
326     "What do you intended/expected to achieve/to be"
327
328 4. Lunaix's panic trace (if applicable)
329
330 5. Other clues that you think might be helpful
331 ```
332
333
334 ## Limitations
335
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.
339
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
343
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.
348
349 ## Acknowledgement
350
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.
353
354 As a personal challenge, this project is independently developed by the author 
355 single-handly, which means:
356
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 
362   project time.
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.
369
370 ## References
371
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)
390 + ECMA-119 (ISO9660)
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/
399 + Linux man pages
400
401 ## Appendix 1: Supported System Call<a id="appendix1"></a>
402
403 Refer to [Lunaix Syscall Table](docs/lunaix-syscall-table.md)
404
405 ## Appendix 2: Debugging with GDB remotely via UART
406
407 **(((( Broken after a refactoring years ago, need rework ))))**
408
409 <details>
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.
413
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.
416
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.
421
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.
425
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.
433
434 ### Limitations
435
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.
440 </details>