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.
Loads the registers of the new process from the saved values in the
process table entry for it.
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 stored in the stack.
(What routine will the new process be executing when control is
transferred to it?)