next up previous
Next: The Null Process Up: Xinu Mechanism for Previous: resched

ctxsw

The routine ctxsw, described in the text (pg 60), does the following:

Saves the registers of the old process in the process table entry for it. Registers R1-R5, the stack pointer, the program counter, and the process status are all saved. (Compare this with register saves in a procedure call) It has to be careful while saving a value for the PC. It does not save the address of any of the remaining instructions in ctxsw (why?). Instead, it saves the return address of ctxsw. Thus, when the old process is resumed, it starts executing after the statement in Resched that calls ctxsw. The stack pointer must be adjusted to make it look as if a return from ctxsw occurred. In the LSI version, this is done by popping the return address from the stack. The parameters will be popped by the calling procedure.

Loads the registers of the new process from the saved values in the process table entry for it. In particular, loads the saved stack pointer of the new process's context block, switching stacks. Again, care has to be taken with the program counter. This register can be loaded only after the rest of the state has been restored. The routine uses the rtt instruction to transfer return control to the new process. This instruction loads the PC and PS from values saved in the stack. Thus ctxsw makes sure that these values are pushed on the (new process's) stack before rtt is called. (What routine will the new process be executing when control is transferred to it?)


Prasun Dewan
Tue Jan 20 11:58:31 EST 2004