Linux Kernel Architecture

(Jacob Rumans) #1

Chapter 13: System Calls


Dispatchingand ParameterPassing


System calls are uniquely identified by a number assigned by the kernel. This is done for practical reasons
that become clear when system calls are activated. All calls are handled by asinglecentral piece of code
that uses the number to dispatch a specific function by reference to a static table. The parameters passed
are also handled by the central code so that parameter passing is implemented independently of the
actual system call.

Switching from user to kernel mode — and therefore to dispatching and parameter passing — is imple-
mented in assembly language code to cater for many platform-specific features. Owing to the very large
number of architectures supported, every detail cannot be covered, and our description is therefore
restricted to the widespread IA-32 architectures. The implementation approach is much the same on
other processors, even though assembler details may differ.

To permit switching between user and kernel mode, the user process must first draw attention to itself by
means of a special machine instruction; this requires the assistance of the C standard library. The kernel
must also provide a routine that satisfies the switch request and looks after the technical details. This
routine cannot be implemented in userspace because commands are needed that normal applications are
not permitted to execute.

Parameter Passing


Different platforms use different assembler methods to execute system calls.^5 System call parameters
are passed directly in registers on all platforms — which handler function parameter is held in which
register is precisely defined. A further register is needed to define the system call number used during
subsequent dispatching to find the matching handler function.

The following overview shows the methods used by afew popular architectures to make system calls:

❑ On IA-32 systems, the assembly language instructionint $0x80raises software interrupt 128.
This is acall gateto which a specific function is assigned to continue system call processing. The
system call number is passed in registereax, while parameters are passed in registersebx,ecx,
edx,esi,andedi.^6
On more modern processors of the IA-32 series (Pentium II and higher), two assembly language
instructions (sysenterandsysexit) are used to enter and exit kernel mode quickly. The way in
which parameters are passed and returned is the same, but switching between privilege levels is
faster.
To enablesysentercalls to be made faster without losing downward compatibility with older
processors, the kernel maps a memory page into the top end of address space (at0x0xffffe000).
Depending on processor type, the system call code on this page includes eitherint 0x80or
sysenter.

(^5) ThedetailsareeasytofindinthesourcesoftheGNU standard library by referring to the filenamedsysdeps/unix/sysv/
linux/arch/syscall.S. The assembly language code required for the particular platform can be found under thesyscall
label; this code provides a general interface for invoking system calls for the rest of the library.
(^6) In addition to the0x80call gate, kernel implementation on IA-32 processors features two other ways of entering kernel mode and
executing system calls — the lcall7 and lcall27 call gates. Theseare used to perform binary emulation for BSD and Solaris because
these systems make system calls in native mode. They differ only slightly from the standard Linux method and offer little in the way
of new insight — which is why I do not bother to discuss them here.

Free download pdf