Linux Kernel Architecture

(Jacob Rumans) #1

Chapter 14: Kernel Activities


Switchingto KernelMode
The switch to kernel mode is based on assembly language code executed by the processor automatically
after every interrupt. The tasks of this code are described above. Its implementation can be found in
arch/arch/kernel/entry.S,^12 which usually defines various entry points at which the processor sets
the flow of control when an interrupt occurs.

Only the most necessary actions are executed directly in assembly language code. The kernel attempts to
return to regular C code as quickly as possible because it is far easier to handle. To this end, an environ-
ment must be created that is compatible with the expectations of the C compiler.

Functions are called in C by placing the required data — return address and parameters — on the stack
in a certain order. When switching between user mode and kernel mode, it is also necessary to save the
most important registers on the stack so that they can be restored later. These two actions are performed
by platform-dependent assembly language code. On most platforms, control flow is then passed to the
Cfunctiondo_IRQ,^13 whose implementation is also platform-dependent, but which greatly simplifies
the situation. Depending on the platform, the function receives as its parameter either the processor
register

arch/arch/kernel/irq.c
fastcall unsigned int do_IRQ(struct pt_regs regs)

or the number of the interrupt together with a pointer to the processor register

arch/arch/kernel/irq.c
unsigned int do_IRQ(int irq, struct pt_regs *regs)

pt_regsis used to save the registers used by the kernel. The values are pushed one after another onto
the stack (by assembly language code) and are left there before the C function is invoked.

pt_regsis defined to ensure that the register entries on the stack coincide with the elements of the
structure. The values are not only saved for later, but can also be read by the C code. Figure 14-8
illustrates this.

register n

register 3
.
.

register 2

register 1

Kernel-Stack

ThesocketcallSystem Call


Frames

struct
pt_regs

Figure 14-8: Stack layout after entry
into kernel mode.

(^12) The unified x86 architecture distinguishes betweenentry_32for IA-32 andentry_64for AMD64 systems.
(^13) Exceptions are Sparc, Sparc64, and Alpha.

Free download pdf