“swtch” is called by “trap” (0770, 0791), “sleep” (2084, 2093), “expand” (2287), “exit” (3256), “stop” (4027) and “xalloc” (4480).
This procedure is unique in that its execution is in three phases which in general involve three separate kernel processes. The first and third of these processes will be called the “retiring” and the “arising” processes respectively. Process #0 is always the intermediate process; it may be the “retiring” or the “arising” process as well.
Note that the only variables used by “swtch” are either registers, or global or static (stored globally).
The static structure pointer, “p”, defines a starting point for searching through the “proc” array to locate the next process to activate. Its use reduces the bias shown to processes entered early in the “proc” array. If “p” is null, set its value to the beginning of the “proc” array. This should only occur upon the very first call on “swtch”;
A call on “savu” (0725) saves the current values of the environment and stack pointers (r5 and r6);
“retu” (0740) resets r5 and r6, and, most importantly, resets the kernel address register 6 to address the “scheduler’s” data segment;
Phase Two begins:
The code from this line to line 2224 is only ever executed by kernel process #0. There are two nested loops, from which there is no exit until a runnable process can be found.
At slack periods, the processor spends most of its time executing line 2220. It is only disturbed thence by an interrupt (e.g. from the clock);
The flag “runrun” is reset. (It is used to indicate that a higher priority process than the current process is ready to run. “swtch” is about to look for the highest priority process.);
The priority of the “arising” process is noted in “curpri” (a global variable) for future reference and comparison;
Another call on “retu” resets r5, r6 and the seventh kernel address register to values appropriate for the “arising” process;
Phase Three begins:
“sureg” (1739) resets the user mode hardware segmentation registers using the stored prototypes for the arising process;
The comment which begins here is not encouraging. We will return to this point again towards the end of this chapter;
If you check, you will find that none of the procedures which call “swtch” directly examines the value returned here.
Only the procedures which call “newproc” which are interested in this value, because of the way the child process is first activated!