Skip to content

Commit f116592

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 abcd496 commit f116592

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
@@ -609,6 +609,30 @@ int32_t mo_task_spawn(void *task_entry, uint16_t stack_size_req)
609609
panic(ERR_STACK_ALLOC);
610610
}
611611

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

0 commit comments

Comments
 (0)