Skip to content

Commit abcd496

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 48149d4 commit abcd496

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

kernel/task.c

Lines changed: 16 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"
@@ -492,12 +493,19 @@ void dispatch(void)
492493
uint32_t ready_count = 0;
493494
list_foreach(kcb->tasks, delay_update_batch, &ready_count);
494495

496+
/* Save old task before scheduler modifies task_current */
497+
memspace_t *old_mspace = ((tcb_t *) kcb->task_current->data)->mspace;
498+
495499
/* Hook for real-time scheduler - if it selects a task, use it */
496500
if (kcb->rt_sched() < 0)
497501
sched_select_next_task(); /* Use O(1) priority scheduler */
498502

499503
hal_interrupt_tick();
500504

505+
/* Switch PMP configuration if tasks have different memory spaces */
506+
memspace_t *new_mspace = ((tcb_t *) kcb->task_current->data)->mspace;
507+
pmp_switch_context(old_mspace, new_mspace);
508+
501509
/* Restore next task context */
502510
hal_context_restore(((tcb_t *) kcb->task_current->data)->context, 1);
503511
}
@@ -523,7 +531,15 @@ void yield(void)
523531
if (!kcb->preemptive)
524532
list_foreach(kcb->tasks, delay_update, NULL);
525533

534+
/* Save old task before scheduler modifies task_current */
535+
memspace_t *old_mspace = ((tcb_t *) kcb->task_current->data)->mspace;
536+
526537
sched_select_next_task(); /* Use O(1) priority scheduler */
538+
539+
/* Switch PMP configuration if tasks have different memory spaces */
540+
memspace_t *new_mspace = ((tcb_t *) kcb->task_current->data)->mspace;
541+
pmp_switch_context(old_mspace, new_mspace);
542+
527543
hal_context_restore(((tcb_t *) kcb->task_current->data)->context, 1);
528544
}
529545

0 commit comments

Comments
 (0)