RTEMS Logo

RTEMS 4.6.99.3 On-Line Library


Task Context Management Performing a Context Switch

PREV UP NEXT Bookshelf RTEMS Porting Guide

6.3.3: Performing a Context Switch

The _CPU_Context_switch performs a normal non-FP context switch from the context of the current executing thread to the context of the heir thread.

void _CPU_Context_switch(
  Context_Control  *run,
  Context_Control  *heir
);

This routine begins by saving the current state of the CPU (i.e. the context) in the context area at run. Then the routine should load the CPU context pointed to by heir. Loading the new context will cause a branch to its task code, so the task that invoked _CPU_Context_switch will not run for a while. When, eventually, a context switch is made to load context from *run again, this task will resume and _CPU_Context_switch will return to its caller.

Care should be exercise when writing this routine. All registers assumed to be preserved across subroutine calls must be preserved. These registers may be saved in the task's context area or on its stack. However, the stack pointer and address to resume executing the task at must be included in the context (normally the subroutine return address to the caller of _Thread_Dispatch. The decision of where to store the task's context is based on numerous factors including the capabilities of the CPU architecture itself and simplicity as well as external considerations such as debuggers wishing to examine a task's context. In this case, it is often simpler to save all data in the context area.

Also there may be special considerations when loading the stack pointers or interrupt level of the incoming task. Independent of CPU specific considerations, if some context is saved on the task stack, then the porter must ensure that the stack pointer is adjusted BEFORE to make room for this context information before the information is written. Otherwise, an interrupt could occur writing over the context data. The following is an example of an INCORRECT sequence:

save part of context beyond current top of stack
interrupt pushes context -- overwriting written context
interrupt returns
adjust stack pointer


PREV UP NEXT Bookshelf RTEMS Porting Guide

Copyright © 1988-2004 OAR Corporation