Linux Kernel Architecture

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

Appendix C: Notes on C


Attributes are specified by prefixing the declaration of a variable or function with the keyword
attribute((list))as follows:


int add (int a, int b) __attribute__((regparam(3));
struct xyy { } __attribute((__aligned__(SMP_CACHE_BYTES))

The following attributes are used in the kernel sources:


❑ noreturnis specified if a function does not return to the caller. Optimization contributes to
slightly better code (however, because functions that do not return usually cause programs to
abort, the fact that the code is better is of little relevance). This attribute is used primarily to pre-
vent compiler warnings about non-initialized variables that can occur in corresponding code.
In the kernel, this keyword is appropriate for functions that trigger a panic or stop the machine
once it has been shut down normally.
❑ regparamis an IA-32–specific directive that specifies that function arguments are to be passed
in registers and not on the stack as they usually would be. It requires a parameter to indicate the
maximumnumber of arguments that can be passed in this way — provided there are enough free
registers. Given the scarcity of registers in this architecture, this is never certain. Theeax,edx,
andecxregisters are used for this purpose.
The kernel defines the following macros that use the attribute:
include/asm-x86/linkage_32.h
#define asmlinkage CPP_ASMLINKAGE __attribute__((regparm(0)))
#define FASTCALL(x) x __attribute__((regparm(3)))
#define fastcall __attribute__((regparm(3)))

FASTCALLis used — as the name clearly suggests — to invoke a function quickly.
asmlinkageidentifies functions to be called from within assembler code. Because parameter
passing must be coded manually in this case (and are therefore not accessible to the compiler),
there must be no surprises as to how many parameters are to be passed in registers and how
many are to be passed on the stack — this is why the option of passing parameters in registers
must be explicitly disabled. TheCPP_ASMLINKAGEkeyword usually expands to an empty string
(theextern Ckeyword is inserted only if a C++ compiler is used to compile the kernel), which
instructs the compiler to use the C calling convention (the first argument is last on the stack)
instead of the C++ calling convention (the first argument is first on the stack).
On all architectures other than IA-32, the macros shown above are defined to expand to an
empty string.
❑ sectionallows the compiler to place variables and functions in other sections of the binary file
than would usually be the case (refer to Appendix E for a more detailed description of the binary
format). This is important when implementing theinit and exit calls mentioned throughout this
book. The name of the section where the material is to be placed must be passed to the attribute
as a string parameter.
To define init calls, the compiler uses, for example, the following macro to place the functions in
sections named.initcall0.initand so on:
<init.h>
#define __define_initcall(level,fn,id) \
static initcall_t __initcall_##fn##id __attribute_used__ \
__attribute__((__section__(".initcall" level ".init"))) = fn
Free download pdf