310fd752e471f6c7c3b55219ebbcac4ba6b1279c
[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, LunaixOS, or Lunaix kernel to be exact, is a multi-architectural general purpose kernel written from scratch by the author *independently*. And it is the author's years-long personal endeavor and also a challenge to oneself for writing a functioning kernel **without** any external reference such as existing implementation, tutorial and books (with code) on kernel design. 
12
13 The overall design is aimed to be modern and POSIX-compliance. In a nutshell, Lunaix is:
14
15 + fully-preemptive
16 + modular design with configurable components at compile-time and extendable subsystems
17 + high-performance by utilising advanced caching techniques and infrastructure.
18 + fault-tolerance with sophisticated builtin error handling and tracing techniques.
19 + robust in nature with techniques such as proactive deadlock detection and driver isolation mechanism.
20
21 To give a better understanding (and appreciation) of the works, the following list has been compiled with all feature that lunaix currently supported:
22
23 + Multi-ISA
24   + x86_32
25   + x86_64
26   + Aarch64 (W.I.P)
27 + Boot protocol abstraction
28 + Platform resource management and definition
29   + ACPI
30   + Devicetree
31 + Memory management
32   + multi-architecture abstraction
33   + on-demand paging
34   + copy-on-write and page sharing
35   + compound page support
36   + explicit huge page support (sorry, not THP!)
37   + reverse mapping indexing (rmap)
38   + memory compaction (W.I.P)
39   + slab-alike object allocator
40 + Multi-threadingg and multi-tasking
41   + Protection level and memory space isolation
42   + Native threading support (no more lightweight process nonsense)
43   + Signal handling
44   + Kernel-level multi-tasking (i.e. kernel threads)
45   + Round-robin scheduling (for now)
46   + Fully-preemptive kernel design
47   + taskfs: file system interface to process and threads
48 + File system
49   + virtual file system framework
50   + ...with POSIX compliant file system interface
51   + mountable file system
52   + page cache for file IO
53   + node cache for vfs structure representation.
54   + ext2 (rev.0, rev.1)
55   + iso9660 (rock-ridge)
56   + twifs: file system interface to kernel states.
57 + Device management and interrupt handling
58   + architectural decoupled design
59   + generalised driver framework
60   + generalised irq framework
61   + driver modularisation design
62   + support asynchronous device model
63   + devfs: file system interface to device subsystem
64 + Block I/O (blkio)
65   + generalised block IO interface and encapsulation
66   + blkio request caching
67   + asynchronous blkio operation in nature
68 + Serial I/O
69   + POSIX-compliant serial port model
70   + serial device driver framework (part of driver framework)
71 + Caching Infrastructure
72   + primtive: generic sparse associative array
73   + LRU replacement policy and pooling
74   + kernel daemon for periodical cache eviction
75 + Error handling and detection
76   + stack back-tracing with symbol resolution
77   + nested exception unfolding
78   + CPU state dumping
79   + Deadlock/hung-up detection
80
81 For the device driver that is currently support see below:
82
83 + Arhcitecture Neutral
84   + UART 16650-compatible driver
85   + Serial ATA AHCI
86   + PCI 3.0
87   + PCIe 1.1
88   + Standard VGA
89 + Intel x86
90   + RTC (Intel PCH)
91   + IOAPIC irq controller
92   + APIC Timer
93   + Legacy i8042 keyboard controller
94 + ARM
95   + GICv3
96   + PL011 (W.I.P)
97
98 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!) 
99
100 ## Project Structure
101
102 | | |
103 |-----|------|
104 | [lunaix-os](../lunaix-os/) | LunaixOS source code |
105 | [slides](../slides/) | Slides used in my videos |
106 | [reference-material](../reference-material/)| References |
107
108 ## Compile and Build
109
110 You will need following dependencies in order to build Lunaix
111
112 + gcc (recommend v12+)
113 + binutils
114 + make
115 + python (recommend v3.11+)
116
117 And also one should have environment variable `ARCH=<arch>` exported, where `<arch>` is one of the supported arhcitecture (`x86_32`, `x86_64`, `arm64`)
118
119 The following `make` actions are available to use.
120
121 | Make command | Usage |
122 | ---- | ---- |
123 | `make all`               | Build the kernel bin |
124 | `make rootfs`            | Build the stock rootfs |
125 | `make clean`             | clean |
126 | `make config`            | menuconfig |
127
128 A successful build will give `build/bin/kernel.bin`.
129
130 **Please note: this is the kernel, not a bootable image, it require a bootloader to boot and specify the rootfs.**
131
132 ## Booting the kernel
133
134 Since lunaix is a kernel, much like linux. It require additional setup to make the magic. And, as it is "much like linux", methods to make linux kernel boot can also apply to lunaix without or with little translation, as we will discuss below.
135
136 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
137
138 The kernel command line, is however, a bit differentiated.
139 The syntax is similar, both takes form of space-separated array of `<key>=<val>` pairs or boolean `<flag>`.
140
141 Currently, lunaix support the following options
142
143 | Option | Default Value | Optional | Usage |
144 | ------ | ----          |  -----   | ----  |
145 | console | `/dev/ttyS0`   | No | Specify the system console device, path within lunaix's devfs |
146 | rootfs | `/dev/block/sda` | No | Specify the device contain rootfs image, path within lunaix's devfs |
147 | init | `/init` | Yes | Path within rootfs of the `init` |
148
149 ### A quick 'Get Started'
150
151 One can use the `live_debug.sh` provided in the lunaix root directory to quickly bring up the system with default parameter (which is used by the author for debugging).
152
153 Following the steps:
154
155 1. Select a architecture `<arch>`
156 2. Check the compilation prerequisites and presence of `qemu-system-<arch>`
157 3. Run `make ARCH=<arch> user` to build the stock user program
158 4. Run `make ARCH=<arch> rootfs` to build stock rootfs image, require support of `dd`,`mkfs.ext2`, `mount -o loop`, `mktemp`.
159 5. Run `ARCH=<arch> live_debug.sh` to boot in QEMU with gdb hooked (one should see a gdb session)
160 6. telenet to `localhost:12345`, this is QEMU emulated serial port
161 7. type `c` in the active gdb session and commence emualtion.
162 8. Congrats, enjoy your lunaix!
163 (or submit an issue)
164
165
166 ## Submit an Issue
167
168 If one ran into bug, one can submit an issue by filling up the following template
169
170 ```
171 1. Describe the problem
172     "How does it look like, anything descriptive: visual, sonic, emotional experience"
173 2. Steps to reproduce
174     "How you ran into this mess?"
175 3. Expected behaviour
176     "What do you intended/expected to achieve/to be"
177 4. Lunaix's panic trace (if applicable)
178 5. Other clues that you think might be helpful
179 ```
180
181
182 ## Limitations
183
184 The development process is still in motion, any limitation can be categorised as yet to be finished feature. However, some feature that the author considered to be the most urgent and wish the matters to be discussed.
185
186 Lunaix is under assumption 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
187
188 Lunaix is do not have a mature 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.
189
190 ## Acknowledgement
191
192 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.
193
194 As a personal challenge, this project is independently developed by the author single-handly, which means:
195
196 + No reference to existing tutorial, books, online course or any open source project that might provide example, hint or working prototype on the design and implementation of kernel, it's various subsystems or anythings that can be contributed towards a working prototype.
197 + The author has no prior knowledge on Linux kernel through out 90% of the project time.
198 + All knowledge on the kernel design is comming from the basic textbook on operating system theory, that is, *Modern Operating System* by Tanenbaum.
199 + All knowledge on the system programming is commingg from the basic textbook, that is, *Computer System - A Programmer's Perspective Third Edition*
200 + All knowledge on the generic framework design and driver development are ingested from various technical specifications gathered across internet.
201
202 ## References
203
204 + Intel 64 and IA-32 Architecture Software Developer's Manual (Full Volume Bundle)
205 + ACPI Specification (version 6.4)
206 + Devicetree Specification
207 + ARM® Generic Interrupt Controller (v3)
208 + Arm® Architecture Reference Manual (Profile-A)
209 + Procedure Call Standard for the Arm® 64-bit Architecture (AArch64)
210 + IBM PC/AT Technical Reference
211 + IBM VGA/XGA Technical Reference
212 + 82093AA I/O Advanced Programmable Controller (IOAPIC) (Datasheet)
213 + MC146818A (Datasheet)
214 + Intel 500 Series Chipset Family Platform Controller Hub (Datasheet - Volume 2)
215 + PCI Local Bus Specification, Revision 3.0
216 + PCI Express Base Specification, Revision 1.1
217 + PCI Firmware Specification, Revision 3.0
218 + Serial ATA - Advanced Host Controller Interface (AHCI), Revision 1.3.1
219 + Serial ATA: High Speed Serialized AT Attachment, Revision 3.2
220 + SCSI Command Reference Manual
221 + ATA/ATAPI Command Set - 3 (ACS-3)
222 + ECMA-119 (ISO9660)
223 + Rock Ridge Interchange Protocol (RRIP: IEEE P1282)
224 + System Use Sharing Protocol (SUSP: IEEE P1281)
225 + Tool Interface Standard (TIS) Portable Formats Specification (Version 1.1)
226 + *Computer System - A Programmer's Perspective Third Edition* (Bryant, R & O'Hallaron, D), a.k.a. CS:APP
227 + *Modern Operating System* (Tanenbaum, A)
228 + Free VGA, http://www.osdever.net/FreeVGA/home.htm 
229 + GNU CC & LD online documentation.
230 + PCI Lookup, https://www.pcilookup.com/
231 + Linux man pages
232
233 ## Appendix 1: Supported System Call<a id="appendix1"></a>
234
235 **Unix/Linux/POSIX**
236
237 1. `sleep(3)`
238 1. `wait(2)`
239 1. `waitpid(2)`
240 1. `fork(2)`
241 1. `getpid(2)`
242 1. `getppid(2)`
243 1. `getpgid(2)`
244 1. `setpgid(2)`
245 1. `brk(2)`
246 1. `sbrk(2)`
247 1. `_exit(2)`
248 1. `sigreturn(2)`
249 1. `sigprocmask(2)`
250 1. `signal(2)`
251 1. `kill(2)`
252 1. `sigpending(2)`
253 1. `sigsuspend(2)`
254 2. `read(2)`
255 2. `write(2)`
256 2. `open(2)`
257 2. `close(2)`
258 2. `mkdir(2)`
259 2. `lseek(2)`
260 2. `readdir(2)`
261 2. `readlink(2)`※
262 2. `readlinkat(2)`※
263 2. `rmdir(2)`※
264 2. `unlink(2)`※
265 2. `unlinkat(2)`※
266 2. `link(2)`※
267 2. `fsync(2)`※
268 2. `dup(2)`
269 2. `dup2(2)`
270 2. `symlink(2)`※
271 2. `chdir(2)`
272 2. `fchdir(2)`
273 2. `getcwd(2)`
274 2. `rename(2)`※
275 2. `mount(2)`
276 2. `unmount` (a.k.a `umount(2)`)※
277 2. `getxattr(2)`※
278 2. `setxattr(2)`※
279 2. `fgetxattr(2)`※
280 2. `fsetxattr(2)`※
281 2. `ioctl(2)`
282 2. `getpgid(2)`
283 2. `setpgid(2)`
284 2. `mmap(2)`
285 2. `munmap(2)`
286 2. `execve(2)`
287 3. `poll(2)` (via `pollctl`)
288 3. `epoll_create(2)` (via `pollctl`)
289 3. `epoll_ctl(2)` (via `pollctl`)
290 3. `epoll_wait(2)` (via `pollctl`)
291 4. `pthread_create`
292 4. `pthread_self`
293 4. `pthread_exit`
294 4. `pthread_join`
295 4. `pthread_kill`
296 4. `pthread_detach`
297 4. `pthread_sigmask`
298
299 **LunaixOS**
300
301 1. `yield`
302 2. `geterrno`
303 3. `realpathat`
304
305 ( **※**:Indicate syscall is not tested )
306
307 ## Appendix 2: Debugging with GDB remotely via UART
308
309 **(((( Not working yet, need rework ))))**
310
311 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.
312
313 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.
314
315 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.
316
317 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.
318
319 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.
320
321 ### Limitations
322
323 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.