Linux Kernel Architecture

(Jacob Rumans) #1
Mauerer app03.tex V1 - 09/04/2008 6:11pm Page 1194

Appendix C: Notes on C


❑ alignspecifies the minimum alignment of data — in other words, their alignment in memory.
The attribute requires an integer argument that must be divisible by the memory address (at
which the data are held) without a remainder. The unit used is bytes.
This attribute is important for the kernel because it allows maximum use to be made of CPU
caches by placing the key parts of a structure at the best place in memory.
The____cacheline_alignedmacro, for example, is defined as follows:
<cache.h>
#define __cacheline_aligned
__attribute__((__aligned__(SMP_CACHE_BYTES), \
__section__(".data.cacheline_aligned")))

Its purpose is to align data on the L1 cache of the processor even if the constant used suggests
that alignment is achieved only on multiprocessor systems. The preceding code implements
a generic version of the keyword, but individual architectures are free to provide their own
definitions:
A slightly stricter version of the macro looks like this:
<cache.h>
#define INTERNODE_CACHE_SHIFT L1_CACHE_SHIFT
#define ____cacheline_internodealigned_in_smp \
__attribute__((__aligned__(1 << (INTERNODE_CACHE_SHIFT))))

Alignment is based on the maximum possible L1 cache size for the underlying
architecture — regardless of whether the processor actually has an L1 cache of this size.
This means that the defined alignment yields maximum cache benefits but wastes more space,
which is why its use should be carefully considered.

C.1.7 Inline Assembler


When short assembler segments are to be inserted in C code, it is unpractical and cumbersome to create
a separate assembler file, translate it into binary code, and link it with the generated object code of the C
compiler. Therefore, GCC features a special option to integrate assembler code directly in C with the help
of special statements — the compiler assumes responsibility for joint code generation. Not only does this
method require less technical effort from the programmer, it has the added advantage that the machine
code generated from C code can be refined to interoperate with the assembler segment because the
compiler has more information about its structure than is the case when an assembler object file is linked.
The programmer does not need to guess in which register or at which point in memory any required
input parameters are held — this can be defined unambiguously by means of the interface between C
and the inline assembler.

Of course, inserting assembler code is a platform-specific affair, because the opcodes and registers used
differ between the individual processor architectures. Nevertheless, the mechanism that integrates the
statements in the C code is platform-independent.

Theasmstatement is employed to specify the assembler code itself and the registers used. Its syntax is as
follows (the equivalent__asm__keyword may also be used instead ofasm):

asm ("Assembler code";
: Output operand specification
: Input operand specification
Free download pdf