From 28cd9deb9b1b94213d2925b97c4bb593393b68dd Mon Sep 17 00:00:00 2001 From: rami Date: Wed, 3 Jul 2024 11:23:21 -0400 Subject: [PATCH] switch to ring3 --- Makefile | 1 + kernel/init.asm | 32 ++++++++++++++++++++++++++++++++ kernel/kernel.c | 14 +++++++++++--- 3 files changed, 44 insertions(+), 3 deletions(-) diff --git a/Makefile b/Makefile index f562848..0629f87 100644 --- a/Makefile +++ b/Makefile @@ -20,6 +20,7 @@ ISO := Hazel.iso CFLAGS := -ffreestanding -Wall -Wextra -Werror -I $(INCLUDEDIR) -I lib LDFLAGS := -ffreestanding -nostdlib -lgcc -T kernel/kernel.ld QEMUFLAGS := -cdrom $(BUILDDIR)/$(ISO) \ + -d int -m 512M \ -serial stdio diff --git a/kernel/init.asm b/kernel/init.asm index 6abe6a5..47f47f1 100644 --- a/kernel/init.asm +++ b/kernel/init.asm @@ -57,6 +57,24 @@ pit_isr: call pit_handler iret +global jmp_user_mode +jmp_user_mode: + mov ax, (4*8) | 3 + mov ds, ax + mov es, ax + mov fs, ax + mov gs, ax ; SS is handled by iret + + mov eax, esp + push (4*8) | 3 + push eax + pushf + push (3*8) | 3 + extern test_entry + push test_entry + + iret + extern exception_handler %macro isr_err_stub 1 isr_stub_%+%1: @@ -142,6 +160,20 @@ gdt_kern_data: db 0b10010010 db 0b11001111 db 0x00 +gdt_user_code: + dw 0xffff + dw 0x0000 + db 0x00 + db 0b11111010 + db 0b11001111 + db 0x00 +gdt_user_data: + dw 0xffff + dw 0x0000 + db 0x00 + db 0b11110010 + db 0b11001111 + db 0x00 gdt_end: gdtp: diff --git a/kernel/kernel.c b/kernel/kernel.c index 1d85259..b3bf1b5 100644 --- a/kernel/kernel.c +++ b/kernel/kernel.c @@ -12,6 +12,12 @@ struct idtr idtr = {0}; extern uint32_t isr_stub_table[32]; extern void pit_isr(void); +extern void jmp_user_mode(void); + +void test_entry(void) { + asm volatile ("cli" ::); + for (;;) {} +} void exception_handler(int_stack_frame_t r) { LOG("Exception 0x%X (0x%X) has occurred\nEAX: 0x%08X\nECX: 0x%08X\nEDX: 0x%08X\nEBX: 0x%08X\nESI: 0x%08X\nEDI: 0x%08X\nEBP: 0x%08X\nESP: 0x%08X\n", @@ -36,7 +42,7 @@ void kernel(multiboot_info_t *info) { LOG("Kernel log being sent to COM1\n"); if (!mmap_init()) goto halt; - LOG("%d bytes of RAM detected\nCreated a %d byte large physical memory map at 0x%08X\n", ctx.mmap_size*BLOCK_SIZE*8, ctx.mmap_size, ctx.mmap); + LOG("%d bytes of RAM detected\nCreated a %d byte large physical memory map at 0x%08X\n", ctx.mmap_size*BLOCK_SIZE*8, ctx.mmap_size, (uint32_t)ctx.mmap); for (int i = 0; i < 32; i++) { idt_encode_entry(idt, i, isr_stub_table[i], GDT_SEGMENT_SELECTOR(1, 0), TRAP_GATE_32 | INT_RING0 | INT_PRESENT); @@ -48,10 +54,12 @@ void kernel(multiboot_info_t *info) { asm volatile ("lidt %0" :: "m"(idtr)); pic_remap(PIC_1_START, PIC_2_START); - asm volatile ("sti" ::); + //asm volatile ("sti" ::); ctx.ticks = 0; - pit_init(); + //pit_init(); + + jmp_user_mode(); halt: for (;;) {}