Context-switching is the kernel code which saves the old task’s context and restores the new running task’s context, so that the new task continues from the point when it was context-switched out earlier. During context switching:
1. The old task context is stored onto that task’s own stack. The context includes all the CPU registers.
2. The old task’s stack pointer value is stored in the kernel’s internal task structure.
3. The new task’s previous stack pointer is read from the kernel’s internal task structure.
4. The values of the CPU registers are read from the new task’s stack.
5. The new task resumes from where it was before it stopped running previously.
6. From the point of view of the resumed task, it was previously interrupted by the system tick interrupt, and now finally the interrupt handler has returned.
When a task is created, REXIS sets up its task structure such that the above procedure works even if the task has not run previously.