more fixes
This commit is contained in:
+2
-2
@@ -39,8 +39,6 @@ bool clock_switch(struct clock* clock) {
|
||||
}
|
||||
}
|
||||
|
||||
log("clock: Switching to clock source \"%s\"\n", clock->name);
|
||||
|
||||
uint64_t elapsed = clock_get_elapsed_ns_locked();
|
||||
clock->reset(clock);
|
||||
|
||||
@@ -48,6 +46,8 @@ bool clock_switch(struct clock* clock) {
|
||||
active_clock = clock;
|
||||
|
||||
spin_unlock(&lock);
|
||||
|
||||
log("clock: Switching to clock source \"%s\"\n", clock->name);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
@@ -14,13 +14,6 @@
|
||||
#include <bits/elf.h>
|
||||
#include <stdckdint.h>
|
||||
|
||||
struct exec_info {
|
||||
struct vma* space;
|
||||
struct vmo* file_obj;
|
||||
const char** argv;
|
||||
const char** envp;
|
||||
};
|
||||
|
||||
errno_t elf_load(struct exec_info* info, struct task** out) {
|
||||
errno_t err;
|
||||
|
||||
|
||||
@@ -3,6 +3,7 @@ kernel_sources += files(
|
||||
'init.c',
|
||||
'pmm.c',
|
||||
'slab.c',
|
||||
'stack.c',
|
||||
'usercopy.c',
|
||||
'virt.c',
|
||||
'vma.c',
|
||||
|
||||
@@ -116,3 +116,21 @@ fail:
|
||||
spin_unlock(&pt->lock);
|
||||
return status;
|
||||
}
|
||||
|
||||
errno_t pmap_unmap(struct pmap* pt, uintptr_t vaddr) {
|
||||
spin_lock(&pt->lock);
|
||||
|
||||
errno_t status = 0;
|
||||
pte_t* pte = get_pte(pt, vaddr, false, true);
|
||||
if (pte == nullptr || !pte_is_present(pte)) {
|
||||
status = ENOENT;
|
||||
goto fail;
|
||||
}
|
||||
|
||||
pte_clear(pte);
|
||||
pte_flush(vaddr);
|
||||
|
||||
fail:
|
||||
spin_unlock(&pt->lock);
|
||||
return status;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,60 @@
|
||||
#include <zinnia/alloc.h>
|
||||
#include <zinnia/errno.h>
|
||||
#include <zinnia/pmap.h>
|
||||
#include <zinnia/stack.h>
|
||||
#include <zinnia/utils.h>
|
||||
#include <zinnia/virt.h>
|
||||
#include <zinnia/vma.h>
|
||||
#include <arch/mem.h>
|
||||
|
||||
errno_t stack_init(struct stack* ks, size_t size) {
|
||||
if (!ks || size == 0)
|
||||
return EINVAL;
|
||||
|
||||
const size_t page_size = arch_mem_page_size();
|
||||
size = ALIGN_UP(size, page_size);
|
||||
|
||||
uintptr_t va = vm_alloc(size + page_size);
|
||||
if (!va)
|
||||
return ENOMEM;
|
||||
|
||||
phys_t phys;
|
||||
errno_t status = mem_phys_alloc(size / page_size, 0, &phys);
|
||||
if (status)
|
||||
goto err_vm;
|
||||
|
||||
const uintptr_t base = va + page_size;
|
||||
size_t mapped = 0;
|
||||
for (; mapped < size; mapped += page_size) {
|
||||
status = pmap_map(&kernel_vma.pmap, base + mapped, phys + mapped, PTE_READ | PTE_WRITE, CACHE_WRITE_BACK);
|
||||
if (status)
|
||||
goto err_map;
|
||||
}
|
||||
|
||||
ks->base = base;
|
||||
ks->phys = phys;
|
||||
ks->size = size;
|
||||
return 0;
|
||||
|
||||
err_map:
|
||||
for (size_t i = 0; i < mapped; i += page_size)
|
||||
pmap_unmap(&kernel_vma.pmap, base + i);
|
||||
mem_phys_free(phys, size / page_size);
|
||||
err_vm:
|
||||
vm_free(va, size + page_size);
|
||||
return status;
|
||||
}
|
||||
|
||||
void stack_destroy(struct stack* ks) {
|
||||
if (!ks || !ks->base)
|
||||
return;
|
||||
|
||||
const size_t page_size = arch_mem_page_size();
|
||||
for (size_t i = 0; i < ks->size; i += page_size)
|
||||
pmap_unmap(&kernel_vma.pmap, ks->base + i);
|
||||
|
||||
mem_phys_free(ks->phys, ks->size / page_size);
|
||||
vm_free(ks->base - page_size, ks->size + page_size);
|
||||
|
||||
*ks = (struct stack){0};
|
||||
}
|
||||
@@ -3,6 +3,7 @@ kernel_sources += files(
|
||||
'console.c',
|
||||
'elf.c',
|
||||
'irq.c',
|
||||
'module.c',
|
||||
'panic.c',
|
||||
'percpu.c',
|
||||
'print.c',
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
#include <zinnia/assert.h>
|
||||
#include <zinnia/module.h>
|
||||
|
||||
extern errno_t (*__ld_mod_entry_start[])(struct module*);
|
||||
extern errno_t (*__ld_mod_entry_end[])(struct module*);
|
||||
|
||||
void module_init() {
|
||||
for (auto start = __ld_mod_entry_start; start < __ld_mod_entry_end; start += 1) {
|
||||
errno_t mod_status = (*start)(nullptr);
|
||||
assert(mod_status == EOK, "Built-in entry point returned %i", mod_status);
|
||||
}
|
||||
}
|
||||
+13
-2
@@ -2,11 +2,12 @@
|
||||
#include <zinnia/console.h>
|
||||
#include <zinnia/panic.h>
|
||||
#include <zinnia/print.h>
|
||||
#include <zinnia/spin.h>
|
||||
#include <zinnia/stdlib.h>
|
||||
#include <zinnia/string.h>
|
||||
#include <stdarg.h>
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
#include <zinnia/stdlib.h>
|
||||
#include <zinnia/string.h>
|
||||
|
||||
typedef void (*log_fn_t)(void* ctx, const char* buf, size_t len);
|
||||
|
||||
@@ -312,13 +313,23 @@ static void printf_console(void*, const char* msg, size_t len) {
|
||||
console_write(msg, len);
|
||||
}
|
||||
|
||||
static struct spinlock print_lock = {};
|
||||
|
||||
void kprintf(const char* message, ...) {
|
||||
spin_lock(&print_lock);
|
||||
|
||||
va_list args;
|
||||
va_start(args, message);
|
||||
do_print(printf_console, nullptr, message, args);
|
||||
va_end(args);
|
||||
|
||||
spin_unlock(&print_lock);
|
||||
}
|
||||
|
||||
void kvprintf(const char* message, va_list args) {
|
||||
spin_lock(&print_lock);
|
||||
|
||||
do_print(printf_console, nullptr, message, args);
|
||||
|
||||
spin_unlock(&print_lock);
|
||||
}
|
||||
|
||||
+23
-8
@@ -8,6 +8,7 @@
|
||||
#include <zinnia/print.h>
|
||||
#include <zinnia/sched.h>
|
||||
#include <zinnia/spin.h>
|
||||
#include <zinnia/stack.h>
|
||||
#include <zinnia/string.h>
|
||||
#include <zinnia/tailq.h>
|
||||
#include <zinnia/vma.h>
|
||||
@@ -67,6 +68,11 @@ static struct task* next_task(struct scheduler* sched) {
|
||||
return t;
|
||||
}
|
||||
|
||||
static void finish_switch(struct task* prev) {
|
||||
if (prev->state == TASK_STATE_DEAD)
|
||||
task_destroy(prev);
|
||||
}
|
||||
|
||||
static void do_reschedule(struct scheduler* sched) {
|
||||
struct task* from = sched->current;
|
||||
struct task* to = next_task(sched);
|
||||
@@ -82,12 +88,13 @@ static void do_reschedule(struct scheduler* sched) {
|
||||
|
||||
struct percpu* cpu = percpu_get();
|
||||
from->user_stack = cpu->user_stack;
|
||||
cpu->kernel_stack = to->kernel_stack + KERNEL_STACK_SIZE;
|
||||
cpu->kernel_stack = kernel_stack_top(&to->kernel_stack);
|
||||
cpu->user_stack = to->user_stack;
|
||||
|
||||
struct irq_percpu irq_state = *percpu_ref(irq_data);
|
||||
arch_sched_switch(from, to);
|
||||
struct task* prev = arch_sched_switch(from, to);
|
||||
*percpu_ref(irq_data) = irq_state;
|
||||
finish_switch(prev);
|
||||
irq_unlock();
|
||||
}
|
||||
|
||||
@@ -117,17 +124,17 @@ errno_t task_create(const char* name, struct vma* space, task_fn_t entry, uintpt
|
||||
strncpy(new_task->name, name, sizeof(new_task->name));
|
||||
|
||||
// Allocate a kernel stack.
|
||||
new_task->kernel_stack = (uintptr_t)mem_alloc(KERNEL_STACK_SIZE, 0);
|
||||
if (!new_task->kernel_stack) {
|
||||
errno_t s = stack_init(&new_task->kernel_stack, KERNEL_STACK_SIZE);
|
||||
if (s != 0) {
|
||||
mem_free(new_task);
|
||||
return ENOMEM;
|
||||
return s;
|
||||
}
|
||||
new_task->user_stack = 0;
|
||||
|
||||
// Initialize the arch-dependent parts.
|
||||
errno_t s = arch_task_init(&new_task->context, entry, arg0, new_task->kernel_stack + KERNEL_STACK_SIZE, true);
|
||||
s = arch_task_init(&new_task->context, entry, arg0, kernel_stack_top(&new_task->kernel_stack), true);
|
||||
if (s != 0) {
|
||||
mem_free((void*)new_task->kernel_stack);
|
||||
stack_destroy(&new_task->kernel_stack);
|
||||
mem_free(new_task);
|
||||
return s;
|
||||
}
|
||||
@@ -136,7 +143,15 @@ errno_t task_create(const char* name, struct vma* space, task_fn_t entry, uintpt
|
||||
return 0;
|
||||
}
|
||||
|
||||
void task_entry(task_fn_t entry, uintptr_t arg0) {
|
||||
void task_destroy(struct task* task) {
|
||||
arch_task_fini(&task->context);
|
||||
stack_destroy(&task->kernel_stack);
|
||||
mem_free(task);
|
||||
}
|
||||
|
||||
void task_entry(task_fn_t entry, uintptr_t arg0, struct task* prev) {
|
||||
finish_switch(prev);
|
||||
|
||||
*percpu_ref(irq_data) = (struct irq_percpu){
|
||||
.depth = 1,
|
||||
.saved_state = true,
|
||||
|
||||
Reference in New Issue
Block a user