Skip to content

Commit 7d5491f

Browse files
committed
Initialize per-task memory spaces at creation
Allocate a dedicated memory space for each task and register the task stack as a flexpage. This establishes the memory protection metadata that will be loaded into hardware regions during context switches.
1 parent c0767ec commit 7d5491f

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

arch/riscv/pmp.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <sys/memprot.h>
1010
#include <types.h>
1111

12+
#include "csr.h"
1213
#include "hal.h"
1314

1415
/* PMP Region Priority Levels (lower value = higher priority)

kernel/task.c

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -760,6 +760,30 @@ static int32_t task_spawn_impl(void *task_entry,
760760
panic(ERR_STACK_ALLOC);
761761
}
762762

763+
/* Create memory space for task */
764+
tcb->mspace = mo_memspace_create(kcb->next_tid, 0);
765+
if (!tcb->mspace) {
766+
free(tcb->stack);
767+
free(tcb);
768+
panic(ERR_TCB_ALLOC);
769+
}
770+
771+
/* Register stack as flexpage */
772+
fpage_t *stack_fpage =
773+
mo_fpage_create((uint32_t) tcb->stack, new_stack_size,
774+
PMPCFG_R | PMPCFG_W, PMP_PRIORITY_STACK);
775+
if (!stack_fpage) {
776+
mo_memspace_destroy(tcb->mspace);
777+
free(tcb->stack);
778+
free(tcb);
779+
panic(ERR_TCB_ALLOC);
780+
}
781+
782+
/* Add stack to memory space */
783+
stack_fpage->as_next = tcb->mspace->first;
784+
tcb->mspace->first = stack_fpage;
785+
tcb->mspace->pmp_stack = stack_fpage;
786+
763787
/* Minimize critical section duration */
764788
CRITICAL_ENTER();
765789

0 commit comments

Comments
 (0)