Skip to content

Stage 14

Round Robin Scheduler

A new scheduler module is introduced, that is called from boot module

The Pre-Condition

When the Timer Interrupt decides its time to switch

1) User Context: The Timer saves registers onto the Kernel Stack (using backup) 2) Marks the process READY 3) Executes MOD_5

The Switch

1) The Scheduler saves the current SP into the Process Table 2) Looks through Process Table to find a READY or CREATED process. 3) It loades the SP, PTBR and PTLR of the new process from Process Table

The Return to READY Process

1) The Scheduler executes a return instruction 2) return pops the address from the current stack. 3) restore is called, pops the register and calls ireturn.

Exception

  • There is an exception when it comes to CREATED processes
  • CREATED has not been scheduled yet, so there is no concern of BP or SP as compared to a READY process. IRETURN would fetch the first instruction address from the User Stack Important (READY has to go back to the timer to complete the restore command too).

Why store BP again in scheduler?

The EXPL compiler automatically stores all registers into User Stack except BP, which the OS kernel should manage. The scheduler can be called by a system call service. So the scheduler pushes BP into kernel stack before context switch.

Pseudocode

Timer
// Save the SP to UPTR
// Save User Area Page * 512 - 1 to SP
// Change the state of the process to READY
// BACKUP
// Increment Ticks of all the processes
// call MODULE_5
// restore
// clear MODE Flag
// change the SP back to UPTR
// ireturn
Scheduler
// push BP (Scheduler can be called from anywhere)
// Save the KPTR
// Save the PTBR
// Save the PTLR
// Find new process that is either CREATED or READY
// Change SP to Kernel Stack of the new process
// Get the new PTBR and PTLR values
// if the new process is CREATED
    // Change the STATE to RUNNING
    // change SP to UPTR
    // Clear MODE Flag
    // ireturn
// Change the State to RUNNING
// Change the SP to KPTR
// POP BP
// Clear MODE Flag
// return