Skip to content

Commit dd4a381

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 f5dfd26 commit dd4a381

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

arch/riscv/pmp.h

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

12+
#include "csr.h"
13+
1214
/* PMP Region Priority Levels (lower value = higher priority)
1315
*
1416
* Used for eviction decisions when hardware PMP regions are exhausted.

kernel/task.c

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -606,6 +606,30 @@ int32_t mo_task_spawn(void *task_entry, uint16_t stack_size_req)
606606
panic(ERR_STACK_ALLOC);
607607
}
608608

609+
/* Create memory space for task */
610+
tcb->mspace = mo_memspace_create(kcb->next_tid, 0);
611+
if (!tcb->mspace) {
612+
free(tcb->stack);
613+
free(tcb);
614+
panic(ERR_TCB_ALLOC);
615+
}
616+
617+
/* Register stack as flexpage */
618+
fpage_t *stack_fpage =
619+
mo_fpage_create((uint32_t) tcb->stack, new_stack_size,
620+
PMPCFG_R | PMPCFG_W, PMP_PRIORITY_STACK);
621+
if (!stack_fpage) {
622+
mo_memspace_destroy(tcb->mspace);
623+
free(tcb->stack);
624+
free(tcb);
625+
panic(ERR_TCB_ALLOC);
626+
}
627+
628+
/* Add stack to memory space */
629+
stack_fpage->as_next = tcb->mspace->first;
630+
tcb->mspace->first = stack_fpage;
631+
tcb->mspace->pmp_stack = stack_fpage;
632+
609633
/* Minimize critical section duration */
610634
CRITICAL_ENTER();
611635

0 commit comments

Comments
 (0)