By J. H. Saltzer, February 19, 1997
1. Entering and leaving the kernel. The basic issue on entering a kernel is that two things must happen at the same time:
Similarly, two distinct things must happen when exiting the kernel:
An attempt to write a machine language sequence to do these things as distinct steps usually runs into problems:
- if the first thing you try to do is change the PC to the gate address, the next instruction will be taken from the gate address in this address space, rather than from the address space of the kernel.
- If the first thing you do is change the address space pointer, the next instruction will be taken from the current PC's position in the new address space. But you can't do that anyway, because you can't change the address space pointer in user mode.
- if the first thing you do is change the user/kernel state bit, there had better be something that guarantees that the next two steps follow inevitably, or there is a chance of ending up in kernel state but with the PC in a user procedure.
A similar set of dilemmas confronts the programmer trying to switch from kernel mode back to user mode.
For this reason there is usually special support from the hardware. A typical form of support is the "supervisor call" instruction, or SVC. This instruction typically switches the user/kernel bit to kernel mode, saves the PC in a designated register. and then loads the PC from a fixed location in memory. If address space switching is required, it may also load the address space pointer from a fixed location in memory. Presumably these fixed locations have been previously set by the kernel to contain values useful to the kernel, and they are in regions of memory that are not accessible in user mode. The processor then takes its next instruction from the place the PC is pointing, presumably the gate into the kernel.
Similarly, on return (or initial entry) to the user there is usually special support from the hardware. A typical user entry sequence involves transferring a designated register into the PC, transferring a second designated register into the address space pointer, and switching the kernel/user bit to user mode.
Saltzer@mit.edu