Skip to content

Commit f5dfd26

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

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
@@ -7,6 +7,7 @@
77

88
#include <hal.h>
99
#include <lib/queue.h>
10+
#include <pmp.h>
1011
#include <sys/task.h>
1112

1213
#include "private/error.h"
@@ -495,12 +496,19 @@ void dispatch(void)
495496
uint32_t ready_count = 0;
496497
list_foreach(kcb->tasks, delay_update_batch, &ready_count);
497498

499+
/* Save old task before scheduler modifies task_current */
500+
memspace_t *old_mspace = ((tcb_t *) kcb->task_current->data)->mspace;
501+
498502
/* Hook for real-time scheduler - if it selects a task, use it */
499503
if (kcb->rt_sched() < 0)
500504
sched_select_next_task(); /* Use O(1) priority scheduler */
501505

502506
hal_interrupt_tick();
503507

508+
/* Switch PMP configuration if tasks have different memory spaces */
509+
memspace_t *new_mspace = ((tcb_t *) kcb->task_current->data)->mspace;
510+
pmp_switch_context(old_mspace, new_mspace);
511+
504512
/* Restore next task context */
505513
hal_context_restore(((tcb_t *) kcb->task_current->data)->context, 1);
506514
}
@@ -526,7 +534,15 @@ void yield(void)
526534
if (!kcb->preemptive)
527535
list_foreach(kcb->tasks, delay_update, NULL);
528536

537+
/* Save old task before scheduler modifies task_current */
538+
memspace_t *old_mspace = ((tcb_t *) kcb->task_current->data)->mspace;
539+
529540
sched_select_next_task(); /* Use O(1) priority scheduler */
541+
542+
/* Switch PMP configuration if tasks have different memory spaces */
543+
memspace_t *new_mspace = ((tcb_t *) kcb->task_current->data)->mspace;
544+
pmp_switch_context(old_mspace, new_mspace);
545+
530546
hal_context_restore(((tcb_t *) kcb->task_current->data)->context, 1);
531547
}
532548

0 commit comments

Comments
 (0)