switch to ring3

This commit is contained in:
rami 2024-07-03 11:23:21 -04:00
parent 269f7f6845
commit 28cd9deb9b
3 changed files with 44 additions and 3 deletions

View File

@ -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

View File

@ -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:

View File

@ -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 (;;) {}