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