aboutsummaryrefslogtreecommitdiff
path: root/src/kernel
diff options
context:
space:
mode:
Diffstat (limited to 'src/kernel')
-rw-r--r--src/kernel/core/paging.c12
-rw-r--r--src/kernel/core/thread.c8
-rw-r--r--src/kernel/user/process.c7
3 files changed, 20 insertions, 7 deletions
diff --git a/src/kernel/core/paging.c b/src/kernel/core/paging.c
index 0b46be1..38e8407 100644
--- a/src/kernel/core/paging.c
+++ b/src/kernel/core/paging.c
@@ -6,6 +6,7 @@
#include <mutex.h>
#include <thread.h>
#include <malloc.h>
+#include <freemem.h>
#define PAGE_OF_ADDR(x) (((size_t)(x) >> PAGE_SHIFT) % N_PAGES_IN_PT)
#define PT_OF_ADDR(x) ((size_t)(x) >> (PAGE_SHIFT + PT_SHIFT))
@@ -210,10 +211,17 @@ bool pd_map_page(void* vaddr, uint32_t frame_id, bool rw) {
mutex_lock(&pdd->mutex);
if (!(pd->page[pt] & PTE_PRESENT)) {
- uint32_t new_pt_frame = frame_alloc(1);
+
+ uint32_t new_pt_frame;
+ int tries = 0;
+ while ((new_pt_frame = frame_alloc(1)) == 0 && (tries++) < 3) {
+ mutex_unlock(&pdd->mutex);
+ free_some_memory();
+ mutex_lock(&pdd->mutex);
+ }
if (new_pt_frame == 0) {
mutex_unlock(&pdd->mutex);
- return false; // OOM
+ return false;
}
current_pd->page[pt] = pd->page[pt] =
diff --git a/src/kernel/core/thread.c b/src/kernel/core/thread.c
index cd46891..02e81e1 100644
--- a/src/kernel/core/thread.c
+++ b/src/kernel/core/thread.c
@@ -8,6 +8,7 @@
#include <paging.h>
#include <worker.h>
#include <process.h>
+#include <freemem.h>
void save_context_and_enter_scheduler(saved_context_t *ctx);
void resume_context(saved_context_t *ctx);
@@ -137,10 +138,15 @@ thread_t *new_thread(entry_t entry, void* data) {
void* stack_high = stack_low + KPROC_STACK_SIZE;
for (void* i = stack_low; i < stack_high; i += PAGE_SIZE) {
- uint32_t f = frame_alloc(1);
+ uint32_t f;
+ int tries = 0;
+ while ((f = frame_alloc(1)) == 0 && (tries++) < 3) {
+ free_some_memory();
+ }
if (f == 0) {
PANIC("TODO (OOM could not create kernel stack for new thread)");
}
+
bool map_ok = pd_map_page(i, f, true);
if (!map_ok) {
PANIC("TODO (OOM(2) could not create kernel stack for new thread)");
diff --git a/src/kernel/user/process.c b/src/kernel/user/process.c
index 989a47a..4dea882 100644
--- a/src/kernel/user/process.c
+++ b/src/kernel/user/process.c
@@ -4,6 +4,7 @@
#include <frame.h>
#include <process.h>
+#include <freemem.h>
static int next_pid = 1;
@@ -345,14 +346,12 @@ static void proc_usermem_pf(void* p, registers_t *regs, void* addr) {
frame = file_get_page(r->file, addr - r->addr + r->file_offset);
}
if (frame == 0) {
- dbg_printf("OOM for process %d ; yielding and waiting for someone to free some RAM.\n", proc->pid);
- yield();
+ free_some_memory();
}
} while (frame == 0);
while(!pd_map_page(addr, frame, (r->mode & MM_WRITE) != 0)) {
- dbg_printf("OOM(2) for process %d ; yielding and waiting for someone to free some RAM.\n", proc->pid);
- yield();
+ free_some_memory();
}
if (r->file == 0) memset(addr, 0, PAGE_SIZE); // zero out