From 8c5fef6e00b0afe7a4f72fe9f8c72ade1ce4c673 Mon Sep 17 00:00:00 2001 From: Marvin Borner Date: Thu, 6 Aug 2020 20:41:07 +0200 Subject: Some context switch fixes. I'm quite tired actually and I don't seem to find my mistakes... --- apps/b.c | 5 ++++- apps/init.c | 3 +-- src/features/load.c | 7 ------- src/features/proc.c | 15 +++++++++++++-- src/features/syscall.c | 8 +++++--- src/inc/load.h | 6 ------ src/inc/proc.h | 8 ++++++++ 7 files changed, 31 insertions(+), 21 deletions(-) diff --git a/apps/b.c b/apps/b.c index 3de20bf..a0134bc 100644 --- a/apps/b.c +++ b/apps/b.c @@ -42,5 +42,8 @@ void serial_print(const char *data) void main() { - serial_print("b"); + serial_print("\nB loaded\n"); + while (1) { + serial_print("b"); + } } diff --git a/apps/init.c b/apps/init.c index 1c7540c..bd33033 100644 --- a/apps/init.c +++ b/apps/init.c @@ -43,8 +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/features/load.c b/src/features/load.c index 36548bb..5382c77 100644 --- a/src/features/load.c +++ b/src/features/load.c @@ -15,11 +15,4 @@ 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 594e9ec..46b55fb 100644 --- a/src/features/proc.c +++ b/src/features/proc.c @@ -6,6 +6,7 @@ #include #include #include +#include #include u32 pid = 0; @@ -42,8 +43,8 @@ void proc_print() printf("\n"); while (proc) { - printf("Process %d [%s]\n", proc->pid, - proc->state == PROC_RUNNING ? "running" : "sleeping"); + printf("Process %d [%s]: %s\n", proc->pid, + proc->state == PROC_RUNNING ? "running" : "sleeping", proc->name); proc = proc->next; } printf("\n"); @@ -68,6 +69,15 @@ struct proc *proc_make() struct proc *proc = malloc(sizeof(*proc)); proc->pid = pid++; proc->state = PROC_RUNNING; + + // Configure registers (default 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; + if (current) proc_attach(proc); last = proc; @@ -81,6 +91,7 @@ void proc_init() root = proc_make(); bin_load("/init", root); + strcpy(root->name, "root"); proc_print(); sti(); hlt(); diff --git a/src/features/syscall.c b/src/features/syscall.c index 2279429..3d012cf 100644 --- a/src/features/syscall.c +++ b/src/features/syscall.c @@ -5,15 +5,17 @@ #include #include #include +#include +int i = 0; void syscall_handler(struct regs *r) { printf("[SYSCALL] %d\n", r->eax); struct proc *a = proc_make(); - bin_load("/a", a); - sti(); - hlt(); + bin_load(++i ? "/a" : "/b", a); + strcpy(a->name, "a"); + proc_print(); } void syscall_init() diff --git a/src/inc/load.h b/src/inc/load.h index 697248e..60fecf9 100644 --- a/src/inc/load.h +++ b/src/inc/load.h @@ -5,12 +5,6 @@ #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 dc8b9e9..8231810 100644 --- a/src/inc/proc.h +++ b/src/inc/proc.h @@ -6,17 +6,25 @@ #include #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 + enum state { PROC_RUNNING, PROC_ASLEEP }; struct proc { u32 pid; enum state state; + char name[32]; struct regs regs; /* struct proc *parent; */ struct proc *next; }; void proc_init(); +void proc_print(); struct proc *proc_make(); #endif -- cgit v1.2.3