Sat, 27 Jan 2018 16:51:47 +0100
10 files changed,
73 insertions(+),
27 deletions(-)
A
apps/assembly.h
@@ -0,0 +1,25 @@
+#ifndef ASSEMBLY_H +#define ASSEMBLY_H + +/*! + * Performs a syscall. + */ +__attribute__((always_inline)) inline int syscall(int eax, int ebx, int ecx, int edx) { + int res; + __asm__ volatile ( "mov %1, %%eax;" + "mov %2, %%ebx;" + "mov %3, %%ecx;" + "mov %4, %%edx;" + "int $0x30;" + "mov %%eax, %0;" : "=m" (res) : "" (eax), "" (ebx), "" (ecx), "" (edx) : "eax", "ebx", "ecx", "edx"); + return res; +} + +/*! + * Waits for the next interrupt. + */ +__attribute__((always_inline)) inline void hlt(void) { + __asm__ volatile ("hlt"); +} + +#endif
A
apps/fibonacci.c
@@ -0,0 +1,14 @@
+#include "assembly.h" + +#include <stdint.h> + +void fib(void) { + uint32_t a = 0, b = 1; + while (1) { + uint32_t tmp = a + b; + a = b; + b = tmp; + syscall(0, a, 0, 0); + hlt(); + } +}
M
apps/link.txt
→
apps/link.txt
@@ -12,5 +12,6 @@ {
.text : AT(0x0000) { */start.c.o(.*) + *.*(.*) } >CODE }
M
apps/start.c
→
apps/start.c
@@ -1,24 +1,16 @@
+#include "assembly.h" + #include <stdint.h> -/*! - * Performs a syscall. - */ -__attribute__((always_inline)) inline int syscall(int eax, int ebx, int ecx, int edx) { - int res; - __asm__ volatile ( "mov %1, %%eax;" - "mov %2, %%ebx;" - "mov %3, %%ecx;" - "mov %4, %%edx;" - "int $0x30;" - "mov %%eax, %0;" : "=m" (res) : "" (eax), "" (ebx), "" (ecx), "" (edx) : "eax", "ebx", "ecx", "edx"); - return res; -} +extern void fib(void); int start(void) { uint32_t eip, esp, ebp; __asm__ volatile ("call jump; jump: pop %0; mov %%esp, %1; mov %%ebp, %2" : "=m" (eip), "=m" (esp), "=m" (ebp)); syscall(0, eip, esp, ebp); + + fib(); while(1); }
M
include/cedos/core.h
→
include/cedos/core.h
@@ -16,6 +16,7 @@ void crit_enter(void);
void crit_exit(void); uint32_t crit_stash(void); void crit_restore(uint32_t state); +void crit_reset(void); void hard_reset(void); #endif
M
include/cedos/sched/stack_check.h
→
include/cedos/sched/stack_check.h
@@ -5,6 +5,7 @@ #include <stdint.h>
typedef uint32_t STACK_CHECKSUM; -STACK_CHECKSUM stack_check(const void *esp, const void *ebp); +void stack_compute_checksum(STACK_CHECKSUM* checksum, const void *esp, const void *ebp); +int stack_compare_checksum(STACK_CHECKSUM* a, STACK_CHECKSUM* b); #endif
M
kernel/core.c
→
kernel/core.c
@@ -200,6 +200,12 @@ cli();
} } +void crit_reset(void) { + crit_sect_counter = 0; + uint32_t eflags = get_eflags() | if_state; + set_eflags(eflags); +} + void hard_reset(void) { outb(0xFE, 0x64); }
M
kernel/sched/sched.c
→
kernel/sched/sched.c
@@ -69,7 +69,7 @@ // load stack
copy_to_pdir(&frame, sizeof(frame), p->page_dir, p->esp); // save stack checksum - p->checksum = stack_check(&frame, &(&frame)[1]); + stack_compute_checksum(&(p->checksum), &frame, &(&frame)[1]); PROCESS_ID pid = add_process(p, current_pid); //printk("Executing task %i...\n", pid);@@ -89,7 +89,7 @@ current->eip = frame->eip;
current->eflags = frame->eflags; // save stack checksum - current->checksum = stack_check(current->esp, current->ebp); + stack_compute_checksum(&(current->checksum), current->esp, current->ebp); } // select next process@@ -106,8 +106,11 @@ // prepare to return to process
PROCESS* next = get_process(current_pid); switch_page_dir(next->page_dir); + STACK_CHECKSUM checksum; + stack_compute_checksum(&(checksum), next->esp, next->ebp); + // check stack - if (current_pid != 0 && next->checksum != stack_check(next->esp, next->ebp)) { + if (stack_compare_checksum(&(next->checksum), &(checksum))) { printk("STACK DAMAGED: PROCESS %i (%s), ESP %X, EBP %X\n", current_pid, get_process(current_pid)->name, next->esp, next->ebp); memdump((void*)(next->esp), (void*)(next->ebp - next->esp)); kpanic("CRITICAL STACK DAMAGE");@@ -161,11 +164,6 @@
crit_exit(); } -/** - * IMPORTANT NOTE: - * This method has to be modified for processes to be able to kill themselves! - * Right now, this will lead to undefined behaviour! - */ int sched_kill(PROCESS_ID pid) { int success = 1; crit_enter();@@ -180,6 +178,12 @@
remove_process(process->id); } else { success = 0; + } + + if (get_process(current_pid) == NULL) { + // current process has terminated + crit_reset(); + sched_yield(); }
M
kernel/sched/stack_check.c
→
kernel/sched/stack_check.c
@@ -1,11 +1,13 @@
#include "cedos/sched/stack_check.h" -STACK_CHECKSUM stack_check(const void *esp, const void *ebp) { - STACK_CHECKSUM sum = 0; +void stack_compute_checksum(STACK_CHECKSUM* checksum, const void *esp, const void *ebp) { + *checksum = 0; for (uint32_t *p = esp; p < ebp; p = &p[1]) { - sum ^= *p; + *checksum ^= *p; } +} - return sum; +int stack_compare_checksum(STACK_CHECKSUM* a, STACK_CHECKSUM* b) { + return (a == b); }
M
kernel/syscall.c
→
kernel/syscall.c
@@ -3,7 +3,7 @@ #include "cedos/core.h"
#include "cedos/sched/sched.h" void test(uint32_t ebx, uint32_t ecx, uint32_t edx) { - printk("SYSCALL 0x01: EBX=%X ECX=%X EDX=%X\n", ebx, ecx, edx); + printk("SYSCALL 0x01: EBX=%i ECX=%X EDX=%X\n", ebx, ecx, edx); } void* SYSCALL_TABLE[] = {