From 9dbf8131d6aaff1617ef2bc7ebf54838f808688d Mon Sep 17 00:00:00 2001 From: Marvin Borner Date: Thu, 6 Aug 2020 20:02:44 +0200 Subject: Fixed messed up registers --- apps/init.c | 2 +- src/drivers/interrupts.asm | 14 ++++++++++++++ src/features/load.c | 8 ++++++++ src/features/proc.c | 27 +++++++++++---------------- src/features/syscall.c | 4 +++- src/inc/load.h | 6 ++++++ src/inc/proc.h | 1 - 7 files changed, 43 insertions(+), 19 deletions(-) diff --git a/apps/init.c b/apps/init.c index 859ffe8..1c7540c 100644 --- a/apps/init.c +++ b/apps/init.c @@ -43,7 +43,7 @@ void serial_print(const char *data) void main() { serial_print("Init loaded\n"); - __asm__ volatile("int $0x80"); + /* __asm__ volatile("int $0x80"); */ while (1) { __asm__ volatile("hlt"); }; diff --git a/src/drivers/interrupts.asm b/src/drivers/interrupts.asm index 8f8afbb..59c323c 100644 --- a/src/drivers/interrupts.asm +++ b/src/drivers/interrupts.asm @@ -37,6 +37,13 @@ irq_common_stub: push fs push gs + mov ax, 0x10 + mov ds, ax + mov es, ax + mov fs, ax + mov gs, ax + cld + push esp call irq_handler add esp, 4 @@ -113,6 +120,13 @@ isr_common_stub: push fs push gs + mov ax, 0x10 + mov ds, ax + mov es, ax + mov fs, ax + mov gs, ax + cld + push esp call isr_handler add esp, 4 diff --git a/src/features/load.c b/src/features/load.c index bc56235..36548bb 100644 --- a/src/features/load.c +++ b/src/features/load.c @@ -2,6 +2,7 @@ #include #include +#include #include #include #include @@ -14,4 +15,11 @@ void bin_load(char *path, struct proc *proc) proc->regs.ebp = (u32)stack; proc->regs.esp = (u32)stack; proc->regs.eip = (u32)data; + + proc->regs.ds = GDT_DATA_OFFSET; + proc->regs.es = GDT_DATA_OFFSET; + proc->regs.fs = GDT_DATA_OFFSET; + proc->regs.gs = GDT_DATA_OFFSET; + proc->regs.cs = GDT_CODE_OFFSET; + proc->regs.eflags = EFLAGS_ALWAYS | EFLAGS_INTERRUPTS; } diff --git a/src/features/proc.c b/src/features/proc.c index 0378183..594e9ec 100644 --- a/src/features/proc.c +++ b/src/features/proc.c @@ -15,12 +15,14 @@ struct proc *last; void scheduler(struct regs *regs) { - printf("%d", current->pid); - memcpy(¤t->regs, regs, sizeof(struct regs)); + if (current) { + printf("%d", current->pid); + memcpy(¤t->regs, regs, sizeof(struct regs)); + } timer_handler(); - if (current->next) + if (current && current->next) current = current->next; else current = root; @@ -38,6 +40,7 @@ void proc_print() { struct proc *proc = root; + printf("\n"); while (proc) { printf("Process %d [%s]\n", proc->pid, proc->state == PROC_RUNNING ? "running" : "sleeping"); @@ -60,6 +63,8 @@ void proc_attach(struct proc *proc) struct proc *proc_make() { + /* if (current) */ + /* current->state = PROC_ASLEEP; */ struct proc *proc = malloc(sizeof(*proc)); proc->pid = pid++; proc->state = PROC_RUNNING; @@ -69,24 +74,14 @@ struct proc *proc_make() return proc; } -void proc_jump(struct proc *proc) -{ - void (*entry)(); - *(void **)(&entry) = (u32 *)proc->regs.eip; - __asm__ volatile("movl %%eax, %%ebp" ::"a"(proc->regs.ebp)); - __asm__ volatile("movl %%eax, %%esp" ::"a"(proc->regs.esp)); - current = proc; - sti(); - entry(); -} - void proc_init() { cli(); irq_install_handler(0, scheduler); - current = root = proc_make(); + root = proc_make(); bin_load("/init", root); proc_print(); - proc_jump(root); + sti(); + hlt(); } diff --git a/src/features/syscall.c b/src/features/syscall.c index ebfff59..2279429 100644 --- a/src/features/syscall.c +++ b/src/features/syscall.c @@ -1,5 +1,6 @@ // MIT License, Copyright (c) 2020 Marvin Borner +#include #include #include #include @@ -11,7 +12,8 @@ void syscall_handler(struct regs *r) struct proc *a = proc_make(); bin_load("/a", a); - proc_jump(a); + sti(); + hlt(); } void syscall_init() diff --git a/src/inc/load.h b/src/inc/load.h index 60fecf9..697248e 100644 --- a/src/inc/load.h +++ b/src/inc/load.h @@ -5,6 +5,12 @@ #include +#define EFLAGS_ALWAYS 0x2 // Always one +#define EFLAGS_INTERRUPTS 0x200 // Enable interrupts + +#define GDT_DATA_OFFSET 0x10 // Data segment offset in GDT +#define GDT_CODE_OFFSET 0x8 // Code segment offset in GDT + void bin_load(char *path, struct proc *proc); #endif diff --git a/src/inc/proc.h b/src/inc/proc.h index bc3d115..dc8b9e9 100644 --- a/src/inc/proc.h +++ b/src/inc/proc.h @@ -17,7 +17,6 @@ struct proc { }; void proc_init(); -void proc_jump(struct proc *proc); struct proc *proc_make(); #endif -- cgit v1.2.3