Kernel Wrapper Routines

Although system calls are used mainly by User Mode processes, they can also be invoked by kernel threads, which cannot use library functions. To simplify the declarations of the corresponding wrapper routines, Linux defines a set of seven macros called _syscall0 through _syscall6.

In the name of each macro, the numbers 0 through 6 correspond to the number of parameters used by the system call (excluding the system call number). The macros are used to declare wrapper routines that are not already included in the libc standard library (for instance, because the Linux system call is not yet supported by the library); however, they cannot be used to define wrapper routines for system calls that have more than six parameters (excluding the system call number) or for system calls that yield nonstandard return values.

Each macro requires exactly 2+2xn parameters, with n being the number of parameters of the system call. The first two parameters specify the return type and the name of the system call; each additional pair of parameters specifies the type and the name of the corresponding system call parameter. Thus, for instance, the wrapper routine of the fork( ) system call may be generated by:

syscall0(int,fork)

while the wrapper routine of the write( ) system call may be generated by:

syscall3(int,write,int,fd,const char *,buf,unsigned int,count)

In the latter case, the macro yields the following code:

int write(int fd,const char * buf,unsigned int count) {

: "0" (__NR_write), "b" ((long)fd),

"c" ((long)buf), "d" ((long)count)); if ((unsigned long) res >= (unsigned long)-125) { errno = - res; _res = -1;

The _ _NR_write macro is derived from the second parameter of _syscall3; it expands into the system call number of write( ). When compiling the preceding function, the following assembly language code is produced:

write:

pushl %ebx ; push ebx into stack movl 8(%esp), %ebx ; put first parameter in ebx

.L1: popl %ebx ret movl 12(%esp), %ecx movl 16(%esp), %edx movl $4, %eax int $0x80

negl %eax movl %eax, errno movl $-1, %eax put second parameter in ecx put third parameter in edx put NR write in eax invoke system call check return code if no error, jump complement the value of eax put result in errno set eax to -1

pop ebx from stack return to calling program

Notice how the parameters of the write( ) function are loaded into the CPU registers before the int $0x8 0 instruction is executed. The value returned in eax must be interpreted as an error code if it lies between -1 and -125 (the kernel assumes that the largest error code defined in include/asm-i386/errno.h is 125). If this is the case, the wrapper routine stores the value of -eax in errno and returns the value -1; otherwise, it returns the value of eax.

I [email protected] RuBoard

Continue reading here: The Role of Signals

Was this article helpful?

0 0