From 92f848244796881994c1f147633123c45da219b6 Mon Sep 17 00:00:00 2001 From: Anton Kling Date: Sat, 13 Apr 2024 21:07:02 +0200 Subject: Kernel: Don't hard fail if the kernel can't allocate memory. Currently this is just a improvement of error handling but it should also try to free up memory where it is possible. --- kernel/sched/scheduler.c | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) (limited to 'kernel/sched') diff --git a/kernel/sched/scheduler.c b/kernel/sched/scheduler.c index 45969b0..6f36a76 100644 --- a/kernel/sched/scheduler.c +++ b/kernel/sched/scheduler.c @@ -111,8 +111,15 @@ process_t *create_process(process_t *p, u32 esp, u32 eip) { sizeof(current_task->program_name)); } - r->cr3 = (p) ? clone_directory(get_active_pagedirectory()) - : get_active_pagedirectory(); + if (p) { + r->cr3 = clone_directory(p->cr3); + if (!r->cr3) { + kfree(r); + return NULL; + } + } else { + r->cr3 = get_active_pagedirectory(); + } r->parent = p; r->tcb = kcalloc(1, sizeof(struct TCB)); @@ -168,6 +175,7 @@ process_t *create_process(process_t *p, u32 esp, u32 eip) { void tasking_init(void) { current_task = ready_queue = create_process(NULL, 0, 0); + assert(current_task); current_task_TCB = current_task->tcb; current_task->tcb->CR3 = current_task->cr3->physical_address; } @@ -321,9 +329,14 @@ process_t *internal_fork(process_t *parent); int fork(void) { process_t *new_task; new_task = internal_fork(current_task); - if (NULL == new_task) { + if (0 == new_task) { return 0; } + if ((process_t *)1 == new_task) { + return -ENOMEM; // FIXME: This is probably the reason it failed now but is + // not the only reason it could fail(at least in the + // future). + } process_t *tmp_task = (process_t *)ready_queue; for (; tmp_task->next;) { -- cgit v1.2.3