sys: Refactor GDT initialization, enhance syscall handling, and improve user mode entry

Finally we got usermode working again, It took awhile but we got it done. A few things we did in this commit:

- Simplified GDT entry initialization in gdt.c for kernel and user segments.
- Fixed TSS structure for task switching.
- Implemented cr2 handling in a page fault.
- Enhanced user stack setup in usermode.c to return the correct RSP.
- Improved syscall implementation in syscall.c, including new syscall numbers.
- Updated syscall entry to correctly handle context switching and argument passing.
- Refactored init.c to demonstrate file operations using syscalls.
- Added new syscalls for file operations in syscalls.h.
- Modified VFS to handle leading slashes in paths correctly.

Signed-off-by: kaguya <vpshinomiya@protonmail.com>
This commit is contained in:
kaguya
2026-04-26 02:06:28 -04:00
parent 3b6e68bc16
commit e6e8b1209b
19 changed files with 263 additions and 266 deletions
+12 -117
View File
@@ -126,15 +126,15 @@ static uacpi_interrupt_ret handle_power_button(uacpi_handle ctx) {
return UACPI_INTERRUPT_HANDLED;
}
// The following will be our kernel's entry point.
// If renaming kmain() to something else, make sure to change the
// linker script accordingly.
void kmain(void) {
// Ensure the bootloader actually understands our base revision (see spec).
if (LIMINE_BASE_REVISION_SUPPORTED(limine_base_revision) == false) {
hcf();
}
uint64_t rsp;
asm volatile("mov %%rsp, %0" : "=r"(rsp));
// Ensure we got a framebuffer.
if (framebuffer_request.response == NULL
|| framebuffer_request.response->framebuffer_count < 1) {
@@ -179,8 +179,6 @@ void kmain(void) {
entry->type);
}
x86_64_DisableInterrupts();
uint32_t msr_lo, msr_hi;
asm volatile("rdmsr" : "=a"(msr_lo), "=d"(msr_hi) : "c"(0x1B));
@@ -202,7 +200,6 @@ void kmain(void) {
x86_64_PIT_Initialize(1000);
asm volatile("sti");
calibrate_tsc();
//while (1) asm volatile("hlt");
@@ -220,102 +217,8 @@ void kmain(void) {
hcf();
}
ext2_read_root_dir();
printf("test");
uint8_t* file_buffer = kmalloc(4096); // adjust if needed
uint32_t file_size = 0;
if (ext2_read_file_from_root("a.txt", file_buffer, &file_size)) {
printf("Read file (size %d):\n", file_size);
for (uint32_t i = 0; i < file_size; i++) {
printf("%c", file_buffer[i]); // or printf("%c", ...)
}
printf("\n");
} else {
printf("Failed to read file\n");
}
kfree(file_buffer);
uint8_t* file2_buffer = kmalloc(4096); // adjust if needed
uint32_t file2_size = 0;
if (ext2_read_file_from_root("dogcatman.txt", file2_buffer, &file2_size)) {
printf("Read file (size %d):\n", file2_size);
for (uint32_t i = 0; i < file2_size; i++) {
printf("%c", file2_buffer[i]); // or printf("%c", ...)
}
printf("\n");
} else {
printf("Failed to read file\n");
}
kfree(file2_buffer);
uint8_t* file3_buffer = kmalloc(4096); // adjust if needed
uint32_t file3_size = 0;
if (ext2_read_file_from_root("test.txt", file3_buffer, &file3_size)) {
printf("Read file (size %d):\n", file3_size);
for (uint32_t i = 0; i < file3_size; i++) {
printf("%c", file3_buffer[i]); // or printf("%c", ...)
}
printf("\n");
} else {
printf("Failed to read file\n");
}
kfree(file3_buffer);
uint8_t* file4_buffer = kmalloc(4096); // adjust if needed
uint32_t file4_size = 0;
if (ext2_read_file_from_root("qwerty.txt", file4_buffer, &file4_size)) {
printf("Read file (size %d):\n", file4_size);
for (uint32_t i = 0; i < file4_size; i++) {
printf("%c", file4_buffer[i]); // or printf("%c", ...)
}
printf("\n");
} else {
printf("Failed to read file\n");
}
kfree(file4_buffer);
const char* msg = "hello i wrote from kernel!";
ext2_write_file_from_root("qwerty.txt", (const uint8_t*)msg, strlen(msg), EXT2_WRITE_APPEND);
uint8_t* file5_buffer = kmalloc(4096); // adjust if needed
uint32_t file5_size = 0;
if (ext2_read_file_from_root("qwerty.txt", file5_buffer, &file5_size)) {
printf("Read file (size %d):\n", file5_size);
for (uint32_t i = 0; i < file5_size; i++) {
printf("%c", file5_buffer[i]); // or printf("%c", ...)
}
printf("\n");
} else {
printf("Failed to read file\n");
}
kfree(file5_buffer);
uint32_t inum = ext2_resolve_path("charlie.tga");
if (!inum) {
@@ -364,20 +267,8 @@ void kmain(void) {
kfree(img);
//clear_screen(0xFF1E1E1E);
fd_t fd = VFS_Open("/a.txt");
if (fd < 0) {
printf("asudg");
}
uint8_t buf[128];
int n;
while ((n = VFS_Read(fd, buf, sizeof(buf))) > 0)
{
VFS_Write(VFS_FD_STDOUT, buf, n);
}
VFS_Close(fd);
printf("\nKirkOS %s\n", KIRKOS_VERSION);
x86_64_EnableInterrupts();
@@ -454,8 +345,12 @@ void kmain(void) {
input_init();
ps2_kbd_init();
//syscall_init();
//start_userspace();
gdt_load_tss((size_t)&kernel_tss);
kernel_tss.rsp0 = rsp;
syscall_init();
start_userspace();
// We're done, just hang...
hcf();