Repository navigation
Expand file tree
/
Copy path00_Kernel_Contents.txt
More file actions
190 lines (169 loc) · 9.29 KB
/
Copy path00_Kernel_Contents.txt
File metadata and controls
190 lines (169 loc) · 9.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
00_Kernel_Contents.txt
Kernel - repository contents
================================================================
1. TREE
ForPublic/
|-- 00_Kernel_Contents.txt this file
|-- build/
| `-- kernel_image.bin 262,144 B bootable kernel image
`-- src/
|-- make_boot.py 8,003 B build script + boot loader source
|-- kernel.ld 597 B linker script
|-- kernel.c 137,500 B the kernel
|-- cop.c 10,275 B AVX-512 co-processor routines
`-- font8x16.h 9,234 B screen font
2. BOOT ORDER (raw path)
2.1 BIOS loads stage1 (sector 0) at 0x7C00.
2.2 stage1 reads stage2 + kernel from the disk to 0x7E00 upward.
2.3 stage2 switches 16-bit -> 32-bit -> 64-bit long mode.
2.4 stage2 copies the kernel from 0x8E00 to 0x100000 (1 MB) and jumps to it.
2.5 kernel_main (kernel.c) runs.
3. FILES
3.1 src/make_boot.py
What it is : Python build script. It also holds the assembly source of
stage1 and stage2 as text inside the script.
What it does:
3.1.1 Compiles kernel.c -> kernel.o
(gcc, 64-bit, freestanding, -O2, general registers only).
3.1.2 Compiles cop.c -> cop.o
(gcc, 64-bit, freestanding, -O3, AVX-512F + AVX-512DQ).
3.1.3 Links kernel.o + cop.o with kernel.ld -> kernel.bin (flat binary).
Stops if kernel.bin is larger than 94,720 bytes.
3.1.4 Writes stage2.s, assembles and links it at 0x7E00 -> stage2.bin
(padded to 8 sectors). The kernel size is written into stage2
so it knows how much to copy to 1 MB.
3.1.5 Writes stage1.s, assembles and links it at 0x7C00 -> stage1.bin
(512 bytes, boot signature 0xAA55).
3.1.6 Joins stage1 + stage2 + kernel, pads to 512 sectors
-> build/kernel_image.bin.
3.1.7 Creates disk/DiskImage.img (512 MB) if it does not exist, and
writes the image over its first 512 sectors. The rest of the disk
is kept.
Needs : python3, gcc, as, ld.
Run : python3 src/make_boot.py
stage1 (inside make_boot.py)
16-bit boot sector. Resets the disk, reads the payload with BIOS
int 0x13 AH=42h (LBA, 64 sectors per read, 3 retries), jumps to
0x7E00. Prints "E" and stops on a disk error.
stage2 (inside make_boot.py)
Loads a GDT, enters protected mode, builds page tables at 0x20000
(first 2 MB identity mapped), enables long mode, clears the VGA
text screen, prints "64-BIT LONG MODE", copies the kernel to 1 MB
and jumps to it.
3.2 src/kernel.ld
What it is : linker script for the kernel.
What it does:
3.2.1 Entry point = kernel_main.
3.2.2 Places the kernel at address 0x100000 (1 MB).
3.2.3 Order: .text (entry first), .rodata, then .data and .bss on their
own 4 KB pages.
3.2.4 Defines _kernel_start and _kernel_end. kernel.c uses them to keep
the kernel's memory hidden from a guest OS.
3.2.5 Discards .comment, .note and .eh_frame.
3.3 src/kernel.c
What it is : the kernel. One C file, 2,461 lines, 64-bit long mode.
Header names it "v2.0 Kernel" with v3.0 and v4.0 parts.
What it does (in file order):
3.3.1 Nest 0 engine (engine_step): 4-state machine.
KERNEL_0 -> PHASE_1 (register counts 0 up to 0x7FFFFFFF)
-> KERNEL_S0 -> PHASE_2 (register counts down to 0) -> cycle + 1.
3.3.2 Text HUD: writes to VGA text memory (0xB8000) and, if present,
to a linear framebuffer using font8x16.h.
3.3.3 Multiboot2 parser (GRUB path): framebuffer, command line,
modules, memory map, EFI system table, ACPI RSDP.
3.3.4 GDT + TSS: kernel and user segments.
3.3.5 Panic record: 16 bytes in CMOS NVRAM, survives a warm reboot.
3.3.6 IDT: CPU exceptions 0-31 with a fault screen (vector, error
code, RIP), then halt.
3.3.7 Kernel heap: 64 KB, first-fit, coalescing, counters, self-test.
3.3.8 Scheduler: 6 tasks, round-robin on the 100 Hz timer (IRQ0),
each task has its own stack and its own CR3.
T0 engine T1, T2 kernel workers T3 ring 3 user task
T4 shell T5 co-processor
3.3.9 Paging: identity map 0-64 GB with 2 MB pages; extra mapping for
a framebuffer above that range.
3.3.10 SYSCALL/SYSRET: one syscall (number 1 = report work counter),
used by the ring 3 task T3.
3.3.11 ATA/PIO disk driver: primary bus, polling, IDENTIFY, 28-bit LBA
read/write of one sector. Boot self-test writes and reads back
LBA 256. Writes are allowed only at LBA 256 and above.
3.3.12 Linux boot stage (GRUB path): takes a Linux kernel + initrd
given as GRUB modules, builds the boot parameters, then either
jumps to Linux directly or, if the module string contains
"krn_vm", runs it as a guest (3.3.16).
3.3.13 AMD-V (SVM): enables SVM, builds a VMCB, runs a small test
guest. Nested page tables (NPT) map 0-2 TB for the guest with
all kernel pages removed, so the guest cannot touch the kernel.
3.3.14 NVMe driver: finds the controller on PCI, admin queue + one I/O
queue, polling. Reads anywhere. Writes only inside one fixed
LBA window (NVME_WR_LBA_LO .. NVME_WR_LBA_HI).
3.3.15 Co-processor control: turns AVX-512 on (vec_enable), runs the
cop.c routines in task T5, command line options
cop=off | on | load:N | wr:N | cpu:N | fullcore.
3.3.16 Linux as an SVM guest: one VCPU per CPU, handles CPUID, MSR,
APIC-write, HLT/MWAIT, NMI exits. The engine runs when the
guest is idle on CPU 0. A read-only telemetry page (magic
"KRN3") shows the kernel's state to the guest (CPUID leaf
0x4B520000). A guest can submit a 2 MB job for Nest 5 (CPUID
leaf 0x4B520002). A fatal fault stops all CPUs.
3.3.17 SMP: reads the ACPI MADT, starts the other CPUs with
INIT-SIPI-SIPI and a trampoline. One CPU can be hidden from
the guest and used for the co-processor.
3.3.18 Shell (task T4): keyboard (IRQ1, scancode set 1), prompt
"KRN> ". Commands: help stat tasks heap fb key ata
disk rd|wr N mods linux stay svm cpus clear reboot
fault div|ud|pf lastpanic [clr] cop [...] nvme [rd N|wr N].
3.3.19 PIC remap, PIT at ~100 Hz, keyboard interrupt handler.
3.3.20 kernel_main: sets everything up in order, draws the HUD, then
loops engine_step forever.
3.4 src/cop.c
What it is : the "co-processor". AVX-512 vector code, compiled
separately from kernel.c (kernel.c uses no vector
registers). Called from kernel.c.
What it does:
3.4.1 cop_burst: 8 chains x 16 float lanes, acc = 0.5 * acc + 1.0,
2^20 iterations per burst. Records time (TSC), iteration count
and a checksum.
3.4.2 cop_stage: a 2 MB staging buffer (4,096 sectors of 512 bytes).
Disk sectors are read into it / written from it by kernel.c.
3.4.3 cop_stage_fill: fills the buffer with the test pattern
word[i] = i * 0x9E3779B1.
3.4.4 cop_stream: one pass over the buffer as 32-bit words. Produces
two checksums (XOR, and sum of word * 0x9E3779B1) and the time.
3.4.5 nest5_compute / nest5_pass: "Nest 5 Energy Wave Engine" over a
2 MB field of 524,288 words. s = word & 0x7FFFFFFF.
W = floor(sum / N), R = sum mod N, d = s - W.
Counts pockets (d > 0) and voids (d <= 0), excess E, deficit D,
largest d and its position. Check: E - D == R.
3.4.6 All results are plain global counters that kernel.c reads and
shows on the HUD.
3.5 src/font8x16.h
What it is : C header with a bitmap font. 95 glyphs, ASCII 0x20-0x7E,
8 pixels wide, 16 pixels high, 16 bytes per glyph.
Its comment says: from Terminus (Lat15-Terminus16.psf).
What it does: kernel.c uses it to draw text on the framebuffer.
3.6 build/kernel_image.bin
What it is : the bootable kernel image made by make_boot.py.
512 sectors x 512 bytes = 262,144 bytes.
Layout (for this build, kernel = 57,412 bytes = 113 sectors):
LBA 0 stage1 (boot sector)
LBA 1-8 stage2
LBA 9-121 kernel (kernel.c + cop.c)
LBA 122-255 zero padding
LBA 256-511 scratch area for the ATA self-test
What it does: boots as a raw disk image (raw path, section 2).
4. GUEST OPERATING SYSTEM
4.1 The kernel does not need Linux. With no guest given, it runs alone.
4.2 The Linux guest code (3.3.12 and 3.3.16) is optional. It follows the
Linux boot format, so it loads Linux only.
4.3 It is the working example of how this kernel loads and runs a guest.
To run another operating system as the guest, replace the Linux
loader with a loader for that system. The functions are
linux_prepare, linux_go, linux_go_vm and lx_vmcb_setup_bsp.
5. HARDWARE AND SUPPORT
5.1 Built and tested on one machine: AMD Ryzen 5 7600, NVMe disk.
The hypervisor needs AMD SVM. The co-processor needs AVX-512.
5.2 The NVMe write window (NVME_WR_LBA_LO, NVME_WR_LBA_HI in kernel.c)
is set for the author's disk. Change it before using "nvme wr" or
"cop wr" on another machine.
5.3 Provided as is. No support is offered.