-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkernel.c
More file actions
341 lines (274 loc) · 9.95 KB
/
kernel.c
File metadata and controls
341 lines (274 loc) · 9.95 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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
#include "types.h"
#include "x86_64.h"
#include "multiboot2.h"
#include "console.h"
#include "pmm.h"
#include "vmm.h"
#include "heap.h"
#include "process.h"
#include "scheduler.h"
#include "vfs.h"
#include "net.h"
#include "usermode.h"
#include "elf.h"
struct BootInfo boot_info;
extern char __kernel_start[];
extern char __kernel_end[];
extern char __bss_start[];
extern char __bss_end[];
static void parse_multiboot(struct Multiboot2Info *mbi);
static void print_boot_info(void);
void init_process(void *arg);
void __noreturn kernel_main(uint32_t mb_info_addr, uint32_t mb_magic) {
console_init();
console_clear();
console_set_color(VGA_COLOR_LIGHT_CYAN, VGA_COLOR_BLACK);
kprintf("kernel\n");
kprintf("======\n\n");
console_set_color(VGA_COLOR_LIGHT_GRAY, VGA_COLOR_BLACK);
if (mb_magic != MULTIBOOT2_BOOTLOADER_MAGIC) {
kprintf("ERROR: Invalid multiboot2 magic: 0x%x\n", mb_magic);
kprintf("Expected: 0x%x\n", MULTIBOOT2_BOOTLOADER_MAGIC);
goto halt;
}
kprintf("[BOOT] Parsing multiboot2 info at 0x%x\n", mb_info_addr);
parse_multiboot((struct Multiboot2Info *)(uint64_t)mb_info_addr);
print_boot_info();
kprintf("[CPU] Initializing GDT... ");
gdt_init();
kprintf("OK\n");
kprintf("[CPU] Initializing IDT... ");
idt_init();
kprintf("OK\n");
kprintf("[CPU] Initializing PIC... ");
pic_init();
kprintf("OK\n");
kprintf("[CPU] Initializing TSS... ");
tss_init();
kprintf("OK\n");
kprintf("[MEM] Initializing PMM... ");
pmm_init();
kprintf("OK (%u MB available)\n",
(uint32_t)(pmm_get_free_memory() / MB));
kprintf("[MEM] Initializing VMM... ");
vmm_init();
kprintf("OK\n");
kprintf("[MEM] Initializing heap... ");
heap_init();
kprintf("OK\n");
kprintf("[DRV] Initializing PIT timer... ");
timer_init(1000);
kprintf("OK\n");
kprintf("[DRV] Initializing keyboard... ");
keyboard_init();
kprintf("OK\n");
kprintf("[PROC] Initializing scheduler... ");
scheduler_init();
kprintf("OK\n");
kprintf("[FS] Initializing VFS... ");
vfs_init();
kprintf("OK\n");
kprintf("[NET] Initializing network... ");
net_init();
udp_init();
socket_init();
kprintf("OK\n");
kprintf("[USER] Initializing Ring 3 subsystem... ");
usermode_initialize_subsystem();
kprintf("OK\n");
kprintf("[ELF] Initializing ELF loader... ");
elf_init();
kprintf("OK\n");
kprintf("\n[KERN] Enabling interrupts...\n");
sti();
console_set_color(VGA_COLOR_LIGHT_GREEN, VGA_COLOR_BLACK);
kprintf("\n=== Kernel initialization complete ===\n\n");
console_set_color(VGA_COLOR_LIGHT_GRAY, VGA_COLOR_BLACK);
kprintf("[PROC] Creating init process...\n");
process_create_kernel_thread("init", init_process, NULL);
kprintf("[PROC] Starting scheduler...\n\n");
scheduler_start();
halt:
kprintf("\nKernel halted.\n");
for (;;) {
hlt();
}
}
static void parse_multiboot(struct Multiboot2Info *mbi) {
struct Multiboot2Tag *tag;
boot_info.kernel_start = (uint64_t)__kernel_start;
boot_info.kernel_end = (uint64_t)__kernel_end;
boot_info.cmdline = NULL;
boot_info.total_memory = 0;
boot_info.has_framebuffer = false;
boot_info.acpi_rsdp = NULL;
boot_info.mmap_entries = NULL;
boot_info.mmap_entry_count = 0;
for (tag = (struct Multiboot2Tag *)((uint8_t *)mbi + 8);
tag->type != MULTIBOOT2_INFO_END;
tag = (struct Multiboot2Tag *)((uint8_t *)tag + ALIGN_UP(tag->size, 8))) {
switch (tag->type) {
case MULTIBOOT2_INFO_CMDLINE: {
struct Multiboot2TagString *cmdline = (void *)tag;
boot_info.cmdline = cmdline->string;
break;
}
case MULTIBOOT2_INFO_BASIC_MEMINFO: {
struct Multiboot2TagBasicMeminfo *mem = (void *)tag;
boot_info.mem_lower = mem->mem_lower;
boot_info.mem_upper = mem->mem_upper;
break;
}
case MULTIBOOT2_INFO_MMAP: {
struct Multiboot2TagMmap *mmap = (void *)tag;
boot_info.mmap_entries = mmap->entries;
boot_info.mmap_entry_size = mmap->entry_size;
boot_info.mmap_entry_count =
(tag->size - sizeof(struct Multiboot2TagMmap)) / mmap->entry_size;
for (uint32_t i = 0; i < boot_info.mmap_entry_count; i++) {
struct Multiboot2MmapEntry *entry =
(void *)((uint8_t *)mmap->entries + i * mmap->entry_size);
if (entry->type == MULTIBOOT2_MEMORY_AVAILABLE) {
boot_info.total_memory += entry->len;
}
}
break;
}
case MULTIBOOT2_INFO_FRAMEBUFFER: {
struct Multiboot2TagFramebufferCommon *fb = (void *)tag;
boot_info.has_framebuffer = true;
boot_info.fb_addr = fb->framebuffer_addr;
boot_info.fb_pitch = fb->framebuffer_pitch;
boot_info.fb_width = fb->framebuffer_width;
boot_info.fb_height = fb->framebuffer_height;
boot_info.fb_bpp = fb->framebuffer_bpp;
break;
}
case MULTIBOOT2_INFO_ACPI_OLD:
case MULTIBOOT2_INFO_ACPI_NEW: {
boot_info.acpi_rsdp = (void *)((uint8_t *)tag + 8);
break;
}
}
}
}
static void print_boot_info(void) {
kprintf("[BOOT] Kernel: 0x%llx - 0x%llx (%u KB)\n",
(unsigned long long)boot_info.kernel_start,
(unsigned long long)boot_info.kernel_end,
(uint32_t)((boot_info.kernel_end - boot_info.kernel_start) / KB));
kprintf("[BOOT] Total memory: %u MB\n",
(uint32_t)(boot_info.total_memory / MB));
if (boot_info.cmdline) {
kprintf("[BOOT] Command line: %s\n", boot_info.cmdline);
}
if (boot_info.has_framebuffer) {
kprintf("[BOOT] Framebuffer: %ux%u @ 0x%llx\n",
boot_info.fb_width, boot_info.fb_height,
(unsigned long long)boot_info.fb_addr);
}
kprintf("\n");
}
void init_process(void *arg) {
(void)arg;
kprintf("[INIT] Init process started (PID %d)\n", process_current()->pid);
kprintf("[INIT] Mounting root filesystem...\n");
vfs_mount("/", "ramfs", NULL);
vfs_mkdir("/dev", 0755);
vfs_mkdir("/proc", 0755);
vfs_mkdir("/tmp", 0755);
vfs_mkdir("/home", 0755);
vfs_mkdir("/bin", 0755);
kprintf("[INIT] Creating device nodes...\n");
kprintf("[INIT] Testing ELF loader...\n");
static uint8_t elf_test_payload[] __aligned(16) = {
0x7F, 0x45, 0x4C, 0x46, 0x02, 0x01, 0x01, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x02, 0x00,
0x3E, 0x00,
0x01, 0x00, 0x00, 0x00,
0x78, 0x00, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00,
0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x40, 0x00,
0x38, 0x00,
0x01, 0x00,
0x00, 0x00,
0x00, 0x00,
0x00, 0x00,
0x01, 0x00, 0x00, 0x00,
0x05, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00,
0xB0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0xB0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x48, 0xC7, 0xC0, 0x01, 0x00, 0x00, 0x00,
0x48, 0xC7, 0xC7, 0x01, 0x00, 0x00, 0x00,
0x48, 0x8D, 0x35, 0x1C, 0x00, 0x00, 0x00,
0x48, 0xC7, 0xC2, 0x12, 0x00, 0x00, 0x00,
0x0F, 0x05,
0x48, 0xC7, 0xC0, 0x3C, 0x00, 0x00, 0x00,
0x48, 0x31, 0xFF,
0x0F, 0x05,
0xEB, 0xFE,
0x5B, 0x45, 0x4C, 0x46, 0x5D, 0x20, 0x48, 0x65,
0x6C, 0x6C, 0x6F, 0x20, 0x66, 0x72, 0x6F, 0x6D,
0x20, 0x45, 0x4C, 0x46, 0x21, 0x0A
};
ElfValidationResult valid_result = elf_validate(
elf_test_payload, sizeof(elf_test_payload));
kprintf("[INIT] ELF validation: %s\n", elf_validation_str(valid_result));
if (valid_result == ELF_VALIDATION_SUCCESS) {
char *test_argv[] = { "/bin/hello", NULL };
char *test_envp[] = { "PATH=/bin", "HOME=/home", NULL };
struct Process *elf_proc = elf_spawn_process(
"hello",
elf_test_payload,
sizeof(elf_test_payload),
1,
test_argv,
test_envp
);
if (elf_proc) {
kprintf("[INIT] ELF process created (PID %d)\n", elf_proc->pid);
} else {
kprintf("[INIT] ELF process creation failed\n");
}
}
kprintf("[INIT] Starting shell...\n\n");
console_set_color(VGA_COLOR_WHITE, VGA_COLOR_BLACK);
kprintf("Welcome to kernel\n");
kprintf("Type 'help' for available commands.\n\n");
shell_run();
}
void __noreturn panic(const char *fmt, ...) {
cli();
console_set_color(VGA_COLOR_WHITE, VGA_COLOR_RED);
kprintf("\n!!! KERNEL PANIC !!!\n\n");
console_set_color(VGA_COLOR_LIGHT_RED, VGA_COLOR_BLACK);
va_list args;
va_start(args, fmt);
kvprintf(fmt, args);
va_end(args);
kprintf("\n\nSystem halted.\n");
uint64_t rsp, rbp;
__asm__ __volatile__("mov %%rsp, %0" : "=r"(rsp));
__asm__ __volatile__("mov %%rbp, %0" : "=r"(rbp));
kprintf("\nRSP: 0x%llx RBP: 0x%llx\n",
(unsigned long long)rsp, (unsigned long long)rbp);
kprintf("\nStack trace:\n");
uint64_t *frame = (uint64_t *)rbp;
for (int i = 0; i < 10 && frame; i++) {
uint64_t ret_addr = frame[1];
if (ret_addr == 0)
break;
kprintf(" [%d] 0x%llx\n", i, (unsigned long long)ret_addr);
frame = (uint64_t *)frame[0];
}
for (;;) {
hlt();
}
}