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