Tuesday, March 13, 2012

Linux Kernel Space, Context Switch, etc

I am reading some stuff on Linux. Just want to summarize some information here.

Linux is a monolithic kernel; that is, the linux kernel executes in a single address space entirely in kernel mode. Linux dose not differentiate between threads and normal processes. A thread is merely a process that shares certain resources with other processes.

In Linux, each processor is doing exactly one of the three things below at any given moment:

  1. In user-space, executing user code in a process.
  2. In kernel-space, in process context, executing on behalf of a specific process.
  3. In kernel-space, in interrupt context, not associated with a process, handling an interrupt.

When executing kernel code, the system is in kernel-space executing in kernel mode. When executing a regular process, the system is in user-space executing in user mode.

When an application executes a system call, we say that the kernel is executing on behalf of the application. Furthermore, the application is said to be executing a system call in kernel-space, and the kernel is running in process context.

A context switch can mean a register context switch, a task context switch, a thread context switch, or a process context switch. What constitutes the context is determined by the processor and the operating system.

When an interrupt occurs, the hardware automatically switches a part of the context (at least enough to allow the handler to return to the interrupted code). The handler may save additional context, depending on details of the particular hardware and software designs. Often only a minimal part of the context is changed in order to minimize the amount of time spent handling the interrupt. The kernel does not spawn or schedule a special process to handle interrupts, but instead the handler executes in the (often partial) context established at the beginning of interrupt handling. Once interrupt servicing is complete, the context in effect before the interrupt occurred is restored so that the interrupted process can resume execution in its proper state.

When a transition between user mode and kernel mode is required in an operating system, a context switch is not necessary; a mode transition is not by itself a context switch. However, depending on the operating system, a context switch may also take place at this time.

When an interrupt occurs, unless the interrupt is disabled in the processor, the processor immediately stops what it is doing, disables the interrupt system, and jumps to a predefined location in memory and executes the code located there. To balance the large amount of work with the need for quick execution, the kernel divides the work of processing interrupts into two halves. The interrupt handler is the top half. The bottom half can be executed later.

References

1. Linux Kernel Development, 3rd edition, by Robert Love
2. http://en.wikipedia.org/wiki/Context_switch

2 comments:

  1. Hi, I am a beginner and thought your page has good information. Can you explain or give some good pointers to kernel space vs user space? Here is what I am not clear about:
    1) What is special about kernel space? If two processes are running in kernel space, do they access memory similar to user space programs and their memory gets swapped to disk?
    2) When a user space program makes a system call, does the process switch to kernel space at some point?
    3)

    ReplyDelete
    Replies
    1. I am not an expert on this. But I think the main difference about the kernel space is that it has a protected memory space and has full access to the hardware. The user space can not use that protected memory space directly. It can use system call. And when an application makes a system call, the process switches from the user space to the kernel space.

      Delete