Accessibility

6.033--Computer System Engineering

Suggestions for classroom discussion


Topic: Magic moments in the life of a thread

By J. H. Saltzer, February 19, 1997


There are two instants in the life of a thread when something moderately mysterious happens: when it crosses into (or out of) a kernel, and when the scheduler switches the processor from one thread to another. One of the reasons why these instants are mysterious is that at those instants the very question of the thread's existence is at stake.

1. Entering and leaving the kernel. The basic issue on entering a kernel is that two things must happen at the same time:

  1. the processor state bit that protects the kernel from damage must switch from the user state to the kernel state, and

  2. the program counter (PC) must move to a place that the kernel trusts to be a suitable point of entry, or gate.

Similarly, two distinct things must happen when exiting the kernel:

  1. the processor state bit that protects the kernel from damage must switch from the kernel state to the user state, and

  2. the program counter (PC) must move to the appropriate return (or start) point in a user address space.
In addition, the situation is usually made even more complicated by a need to change address spaces, which may be accomplished by reloading a pointer to a page map. So there may be three things that must happen simultaneously.

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.


Comments and suggestions: Saltzer@mit.edu