diff options
author | Marvin Borner | 2020-08-01 20:55:53 +0200 |
---|---|---|
committer | Marvin Borner | 2020-08-01 20:55:53 +0200 |
commit | b7f59b28b380d55f9e7abd8e450f1f9c7f050221 (patch) | |
tree | c6b89fa0ff2bebd5ed06a4384d766990bcb42c0a | |
parent | 1686173757af4e453e7a32d152ec4bd20d789652 (diff) |
Added syscalls
-rw-r--r-- | apps/root.c | 1 | ||||
-rw-r--r-- | src/Makefile | 1 | ||||
-rw-r--r-- | src/drivers/interrupts.asm | 1 | ||||
-rw-r--r-- | src/drivers/interrupts.c | 5 | ||||
-rw-r--r-- | src/features/load.c | 2 | ||||
-rw-r--r-- | src/features/proc.c | 30 | ||||
-rw-r--r-- | src/features/syscall.c | 19 | ||||
-rw-r--r-- | src/inc/cpu.h | 8 | ||||
-rw-r--r-- | src/inc/syscall.h | 8 | ||||
-rw-r--r-- | src/main.c | 3 |
10 files changed, 71 insertions, 7 deletions
diff --git a/apps/root.c b/apps/root.c index e7cbf4e..097d0db 100644 --- a/apps/root.c +++ b/apps/root.c @@ -43,6 +43,7 @@ void serial_print(const char *data) void main() { serial_print("root loaded\n"); + __asm__ volatile("int $0x80"); while (1) { }; } diff --git a/src/Makefile b/src/Makefile index e4b11fd..d568901 100644 --- a/src/Makefile +++ b/src/Makefile @@ -14,6 +14,7 @@ COBJS = main.o \ features/gui.o \ features/load.o \ features/proc.o \ + features/syscall.o \ lib/str.o \ lib/mem.o \ lib/math.o \ diff --git a/src/drivers/interrupts.asm b/src/drivers/interrupts.asm index fd5dbfa..59c323c 100644 --- a/src/drivers/interrupts.asm +++ b/src/drivers/interrupts.asm @@ -109,6 +109,7 @@ ISR_NOERRCODE 28 ISR_NOERRCODE 29 ISR_NOERRCODE 30 ISR_NOERRCODE 31 +ISR_NOERRCODE 128 extern isr_handler isr_common_stub: diff --git a/src/drivers/interrupts.c b/src/drivers/interrupts.c index 1bd0b44..08f4c15 100644 --- a/src/drivers/interrupts.c +++ b/src/drivers/interrupts.c @@ -137,7 +137,8 @@ void isr_handler(struct regs *r) if (handler) { handler(r); } else if (r->int_no <= 32) { - printf("#%d Exception, halting!\n", r->int_no); + printf("\n#%d Exception, halting!\n", r->int_no); + printf("Error code: %d\n", r->err_code); __asm__("cli"); while (1) { }; @@ -181,6 +182,8 @@ void isr_install() idt_set_gate(29, (u32)isr29, 0x08, 0x8E); idt_set_gate(30, (u32)isr30, 0x08, 0x8E); idt_set_gate(31, (u32)isr31, 0x08, 0x8E); + + idt_set_gate(0x80, (u32)isr128, 0x08, 0x8E); } /** diff --git a/src/features/load.c b/src/features/load.c index 2cfa546..62da8b4 100644 --- a/src/features/load.c +++ b/src/features/load.c @@ -13,5 +13,5 @@ void bin_load(char *path, struct proc *proc) proc->regs.ebp = stack; proc->regs.esp = stack; - proc->regs.eip = (u32)&data; + proc->regs.eip = (u32)data; } diff --git a/src/features/proc.c b/src/features/proc.c index 92e17f1..fbf1b5a 100644 --- a/src/features/proc.c +++ b/src/features/proc.c @@ -14,15 +14,15 @@ struct proc *last; void scheduler(struct regs *regs) { - printf("."); + printf("%d", current->pid); memcpy(¤t->regs, regs, sizeof(struct regs)); timer_handler(); - if (!current->next) - current = root; - else + if (current->next) current = current->next; + else + current = root; while (current->state == PROC_ASLEEP) if (!current->next) @@ -33,6 +33,18 @@ void scheduler(struct regs *regs) memcpy(regs, ¤t->regs, sizeof(struct regs)); } +void proc_print() +{ + struct proc *proc = root; + + while (proc) { + printf("Process %d [%s]\n", proc->pid, + proc->state == PROC_RUNNING ? "running" : "sleeping"); + proc = proc->next; + } + printf("\n"); +} + void proc_attach(struct proc *proc) { if (!last->next) { @@ -47,7 +59,7 @@ void proc_attach(struct proc *proc) struct proc *proc_make() { - struct proc *proc = (struct proc *)malloc(sizeof(*proc)); + struct proc *proc = malloc(sizeof(*proc)); proc->pid = pid++; proc->state = PROC_RUNNING; if (current) @@ -61,4 +73,12 @@ void proc_init() current = root = proc_make(); bin_load("/root", root); irq_install_handler(0, scheduler); + proc_print(); + + // JUMP! + void (*entry)(); + *(void **)(&entry) = (u32 *)root->regs.eip; + __asm__ volatile("movl %%eax, %%ebp" ::"a"(root->regs.ebp)); + __asm__ volatile("movl %%eax, %%esp" ::"a"(root->regs.esp)); + entry(); } diff --git a/src/features/syscall.c b/src/features/syscall.c new file mode 100644 index 0000000..ab1849c --- /dev/null +++ b/src/features/syscall.c @@ -0,0 +1,19 @@ +// MIT License, Copyright (c) 2020 Marvin Borner + +#include <interrupts.h> +#include <load.h> +#include <print.h> +#include <proc.h> + +void syscall_handler(struct regs *r) +{ + printf("[SYSCALL] %d\n", r->eax); + + struct proc *a = proc_make(); + bin_load("/a", a); +} + +void syscall_install() +{ + isr_install_handler(0x80, syscall_handler); +} diff --git a/src/inc/cpu.h b/src/inc/cpu.h index b8897de..092fd3f 100644 --- a/src/inc/cpu.h +++ b/src/inc/cpu.h @@ -14,4 +14,12 @@ void outb(u16 port, u8 data); void outw(u16 port, u16 data); void outl(u16 port, u32 data); +static inline void spinlock(int *ptr) +{ + int prev; + do + __asm__ volatile("lock xchgl %0,%1" : "=a"(prev) : "m"(*ptr), "a"(1)); + while (prev); +} + #endif diff --git a/src/inc/syscall.h b/src/inc/syscall.h new file mode 100644 index 0000000..b0de099 --- /dev/null +++ b/src/inc/syscall.h @@ -0,0 +1,8 @@ +// MIT License, Copyright (c) 2020 Marvin Borner + +#ifndef SYSCALL_H +#define SYSCALL_H + +void syscall_install(); + +#endif @@ -10,6 +10,7 @@ #include <load.h> #include <print.h> #include <serial.h> +#include <syscall.h> #include <timer.h> u32 HEAP = 0x00200000; @@ -42,6 +43,8 @@ void kernel_main(struct mem_info *mem_info, struct vid_info *vid_info) gui_init(FONT_PATH); gui_term_write("Wake up, " USERNAME "...\n"); + + syscall_install(); proc_init(); while (1) { |