Linux Kernel Architecture

(Jacob Rumans) #1
Mauerer runapp01.tex V1 - 09/04/2008 6:08pm Page 1138

Appendix A: Architecture Specifics


The function performs a context switch by saving the state of the process specified byprevand activating
the process designated bynext.

Although thelastparameter may initially appear to be superfluous, it is used to find the process that
was running immediately prior to the functionreturn.Notethatswitch_tois not a function in the usual
sense because the system state can change in any number of ways between the start and end of the
function.

This can be best understood via an example in which the kernel switches from process A to process B.
prevpoints toAandnextto B. Both are local variables in context A.

After process B has executed, the kernel switches to other processes and finally arrives at process X;
when this process ends, the kernel reverts to process A. Because process A was exited in the middle
ofswitch_to, execution resumes in the second half of the process. The local variables are retained (the
process is not allowed to notice that the scheduler has reclaimed the CPU in the meantime), soprev
points to A andnextto B. However, this information is not sufficient to enable the kernel to establish
which process was running immediately prior to activation of A — although it is important to know this
at various points in the kernel. This is where thelastvariable comes in. The low-level assembler code
to implement context switching must ensure thatlastpoints to the task structure of the process that ran
last so that this information is still available to the kernel after a switch has been made to process A.

A.10.3 Finding the Current Process


The purpose of thecurrentmacro is to find a pointer to the task structure of the currently running
process. It must be declared by each architecture in<asm-arch/current.h>.Thepointerisheldina
separate processor register that can be queried directly or indirectly usingcurrent. The registers reserved
by the individual architectures are listed in Table A-1.

Table A-1: Registers Holding Pointers to the Currenttask_structorthread_info
Instance

Architecture Register Contents

IA-32 esp thread_info

IA-64 r13 task_struct

ARM sp thread_info

Sparc and Sparc64 g6 thread_info

Alpha r8 thread_info

Mips r28 thread_info

Note that the register is used for different purposes,depending on architecture. Some architectures use
it to store a pointer to the currenttask_structinstance, but most use it to hold a pointer to the cur-
rently validthread_infoinstance. Since, on the latter architectures,thread_infocontains a pointer
Free download pdf