Skip to content

Commit 35be7c2

Browse files
committed
Integrate PMP context switching into dispatcher
Switch memory protection configuration during task context switches for both preemptive and cooperative scheduling. The old task's memory space is captured before the scheduler updates its internal state, allowing both old and new memory spaces to be passed to the PMP switching logic.
1 parent 5dad6eb commit 35be7c2

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

kernel/task.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <hal.h>
99
#include <lib/libc.h>
1010
#include <lib/queue.h>
11+
#include <pmp.h>
1112
#include <sys/task.h>
1213

1314
#include "private/error.h"
@@ -616,6 +617,9 @@ void dispatch(void)
616617
next_task->state = TASK_RUNNING;
617618
next_task->time_slice = get_priority_timeslice(next_task->prio_level);
618619

620+
/* Switch PMP configuration if tasks have different memory spaces */
621+
pmp_switch_context(prev_task->mspace, next_task->mspace);
622+
619623
/* Perform context switch based on scheduling mode */
620624
if (kcb->preemptive) {
621625
/* Same task - no context switch needed */
@@ -675,7 +679,15 @@ void yield(void)
675679
/* In cooperative mode, delays are only processed on an explicit yield. */
676680
list_foreach(kcb->tasks, delay_update, NULL);
677681

682+
/* Save current task before scheduler modifies task_current */
683+
tcb_t *prev_task = (tcb_t *) kcb->task_current->data;
684+
678685
sched_select_next_task(); /* Use O(1) priority scheduler */
686+
687+
/* Switch PMP configuration if tasks have different memory spaces */
688+
tcb_t *next_task = (tcb_t *) kcb->task_current->data;
689+
pmp_switch_context(prev_task->mspace, next_task->mspace);
690+
679691
hal_context_restore(((tcb_t *) kcb->task_current->data)->context, 1);
680692
}
681693

0 commit comments

Comments
 (0)