CeDOS - Commit 11cba429

Improved scheduler Process stacks now reside in same virtual memory location.
Celina Sophie Kalus
Sat, 13 Jan 2018 23:53:57 +0100
9 files changed, 49 insertions(+), 37 deletions(-)
M include/assembly.hinclude/assembly.h

@@ -92,35 +92,25 @@ /*!

* Used to find out the current value of the flags register. * \return Value of the EFLAGS-register. */ -__attribute((always_inline)) inline uint32_t eflags() { +__attribute((always_inline)) inline uint32_t get_eflags() { uint32_t eflags; __asm__ volatile ("pushf; pop %%eax; mov %%eax, %0" : "=m" (eflags) : : "eax"); return eflags; } /*! - * Waits for the next interrupt. - */ -__attribute__((always_inline)) inline void hlt(void) { - __asm__ volatile ("hlt"); -} - -/*! - * Disables interrupts and returns the current EFLAGS-value. - * \return Current value of the EFLAGS-register. + * Sets the value of the EFLAGS-register. + * \param eflags New value of the EFLAGS-register. */ -__attribute__((always_inline)) inline uint32_t disable_interrupts(void) { - uint32_t eflags; - __asm__ volatile ("pushf; cli; pop %%eax; mov %%eax, %0" : "=m" (eflags) : : "eax"); - return eflags; +__attribute__((always_inline)) inline void set_eflags(uint32_t eflags) { + __asm__ volatile ("mov %0, %%eax; push %%eax; popf" : : "m" (eflags) : "eax"); } /*! - * Restores a prior state of the EFLAGS-register. Used in tandem with \m disable_interrupts. - * \param eflags Prior state of the EFLAGS-register. + * Waits for the next interrupt. */ -__attribute__((always_inline)) inline void restore_interrupts(uint32_t eflags) { - __asm__ volatile ("mov %0, %%eax; push %%eax; popf" : : "m" (eflags) : "eax"); +__attribute__((always_inline)) inline void hlt(void) { + __asm__ volatile ("hlt"); } /*!
M include/cedos/core.hinclude/cedos/core.h

@@ -12,5 +12,7 @@ int core_init(void);

void printk(const char* string, ...); void kpanic(const char* string); void memdump(void* start, uint32_t size); +void crit_enter(void); +void crit_exit(void); #endif
M include/cedos/process.hinclude/cedos/process.h

@@ -10,10 +10,10 @@ /*!

* Defines all possible states for processes. */ typedef enum { - PSTATE_TERMINATED + PSTATE_TERMINATED, PSTATE_READY, PSTATE_RUNNING, - PSTATE_BLOCKED, + PSTATE_BLOCKED } PROCESS_STATE; /*!
M include/cedos/sched.hinclude/cedos/sched.h

@@ -29,7 +29,13 @@

/*! * Executes a task. */ -PROCESS_ID sched_exec(PHYS_ADDR page_dir, VIRT_ADDR eip, uint32_t eflags, VIRT_ADDR ebp); +PROCESS_ID sched_exec(PHYS_ADDR page_dir, VIRT_ADDR eip, uint32_t eflags); + +/*! + * Return the ID of the current process. + * \return ID of current process. + */ +PROCESS_ID sched_get_current_process(void); /*! * Initializes the scheduler.
M kernel/core.ckernel/core.c

@@ -77,7 +77,7 @@ printk(" ESI=%i EDI=%i ESP=%i EBP=%i\n", esi, edi, esp, ebp);

} void printk(const char* fmt, ...) { - uint32_t eflags = disable_interrupts(); + crit_enter(); va_list args; va_start(args, fmt); uint32_t index = 0;

@@ -107,7 +107,7 @@

fmt++; } - restore_interrupts(eflags); + crit_exit(); } void kpanic(const char* string) {

@@ -117,6 +117,16 @@ // register dump / stack dump

regdump(); stackdump(); while (1) {} +} + +uint32_t volatile eflags = 0; +void crit_enter(void) { + eflags = get_eflags(); + cli(); +} + +void crit_exit(void) { + set_eflags(eflags); } int core_init(void) {
M kernel/main.ckernel/main.c

@@ -70,9 +70,9 @@ infodump();

// create test tasks printk("Creating tasks.\n"); - sched_exec(create_empty_page_dir(), task1, eflags(), 0xC0280000); - sched_exec(create_empty_page_dir(), task2, eflags(), 0xC0300000); - sched_exec(create_empty_page_dir(), task3, eflags(), 0xC0380000); + sched_exec(create_empty_page_dir(), task1, get_eflags()); + sched_exec(create_empty_page_dir(), task2, get_eflags()); + sched_exec(create_empty_page_dir(), task3, get_eflags()); printk("Starting scheduler.\n"); sched_start();
M kernel/mm/paging.ckernel/mm/paging.c

@@ -163,7 +163,7 @@

EXCEPTION(page_fault_isr, frame, error_code) { volatile VIRT_ADDR faulty_addr; __asm__ volatile ("mov %%cr2, %0" : "=a" (faulty_addr)); - printk("PAGE FAULT: %i", faulty_addr); + printk("PAGE FAULT: %i\n", faulty_addr); PHYS_ADDR new_page = get_free_page(); map_page_to_this(new_page, PAGE_DIR_INDEX(faulty_addr), PAGE_TABLE_INDEX(faulty_addr), PAGE_TABLE_FLAGS); // dump registers to stdout
M kernel/sched.ckernel/sched.c

@@ -10,7 +10,7 @@ #include "assembly.h"

#include "common.h" #include "cedos/drivers/speaker.h" -void* const SCHED_STACK = (void*)(0xC0400000); +#define KERNEL_PRIVATE_STACK (void*)(0xC0600000) PROCESS* get_slot(void) { static PROCESS free_slots[4];

@@ -23,15 +23,16 @@

/*! * Executes a task. */ -PROCESS_ID sched_exec(PHYS_ADDR page_dir, VIRT_ADDR eip, uint32_t eflags, VIRT_ADDR ebp) { +PROCESS_ID sched_exec(PHYS_ADDR page_dir, VIRT_ADDR eip, uint32_t eflags) { + crit_enter(); PHYS_ADDR tmp_page_dir = switch_page_dir(page_dir); PROCESS* p = get_slot(); // set process context p->page_dir = page_dir; p->eip = sched_dispatcher; - p->ebp = ebp; - p->esp = ebp - sizeof(SCHED_FRAME); + p->ebp = KERNEL_PRIVATE_STACK; + p->esp = KERNEL_PRIVATE_STACK - sizeof(SCHED_FRAME); p->eflags = eflags; p->entry = (PROCESS_MAIN*)eip;

@@ -51,6 +52,7 @@ p->state = PSTATE_READY;

switch_page_dir(tmp_page_dir); + crit_exit(); return add_process(p); }

@@ -97,13 +99,13 @@ }

int sched_init(void) { // create idle process - sched_exec(create_empty_page_dir(), idle, eflags() | (1 << 9), 0xC0200000); + sched_exec(create_empty_page_dir(), idle, get_eflags()); return 1; } int sched_start(void) { - current_pid = get_process(0); + current_pid = 0; // perform the first timer interrupt manually pic_unmask_interrupt(0);

@@ -115,14 +117,16 @@ INT(0x20);

} int sched_kill(PROCESS_ID pid) { + crit_enter(); if (pid == current_pid) { //remove_process(pid); sched_yield(); - return 1; } else { //remove_process(pid); - return 1; } + + crit_exit(); + return 1; } int sched_dispatcher(void) {
M kernel/sched.skernel/sched.s

@@ -9,8 +9,8 @@

// switch to scheduler stack mov %esp, %eax mov %ebp, %ebx - mov $SCHED_STACK, %esp - mov $SCHED_STACK, %ebp + mov $0xC0400000, %esp + mov $0xC0400000, %ebp // pass process stack as arguments push %ebx