Information Technology Reference
In-Depth Information
2.3.2
Kernel to user mode
Just as there are several different causes for transitions from user-mode to kernel-
mode, there are also several causes for transitions from kernel-mode to user-
mode:
New process. To start a new process, the kernel copies the program
into memory, sets the program counter to be the first instruction of the
process, sets the stack pointer to be the base of the user stack, and switches
to user-mode.
Resume after an exception, interrupt or system call. When the
kernel finishes handling the request, it resumes execution of the inter-
rupted process by restoring its program counter, restoring its registers,
and changing the mode back to user-level.
Switch to a different process. In some cases, such as on a timer inter-
rupt, the kernel will decide to switch to running a different process than
the one that had been running before the interrupt, exception, or system
call. Since the kernel will eventually want to resume the old process, the
kernel needs to save the process's state | its program counter, registers,
and so forth | in the process's control block. The kernel can then resume
a dierent process, by loading its state | its program counter, registers,
and so forth | from the process's control block into the processor, and
then switching to user-mode.
User-level upcall. Many operating systems provide user programs the
ability to receive asynchronous notification of events. The mechanism,
which we will describe shortly, is very similar to kernel interrupt handling,
except at user-level.
2.3.3
Safe mode switch
Whether transitioning from user to kernel mode, or in the opposite direction,
care must be taken to ensure that a buggy or malicious user program cannot
corrupt the kernel. Although the basic idea is simple, the low-level implemen-
tation can be a bit gnarly: we need the processor to save its state and switch
what it is doing, all while it continues to execute instructions that might alter
the state that is in the process of saving. This is akin to rebuilding a car's
transmission while it barrels down the road at 60 mph.
The context switch code must be carefully crafted, and it relies on some
amount of hardware support. To avoid confusion and reduce the possibility
of error, most operating systems have a common sequence of instructions for
entering the kernel | whether due to interrupts, exceptions or system calls
| and a common sequence of instructions for returning to user level, again
regardless of the cause.
Search WWH ::




Custom Search