9.3 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) 
{ 
    long _  _res; 
    asm("int $0x80" 
        : "=a" (_ _res) 
        : "0" (_ _NR_write), "b" ((long)fd), 
          "c" ((long)buf), "d" ((long)count)); 
    if ((unsigned long)_ _res >= (unsigned long)-125) { 
        errno = -_ _res; 
        _ _res = -1; 
    } 
    return (int) _ _res; 
} 

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
     movl 12(%esp), %ecx     ; put second parameter in ecx
     movl 16(%esp), %edx     ; put third parameter in edx
     movl $4, %eax           ; put _ _NR_write in eax
     int $0x80               ; invoke system call
     cmpl $-126, %eax        ; check return code
     jbe .L1                 ; if no error, jump
     negl %eax               ; complement the value of eax
     movl %eax, errno        ; put result in errno
     movl $-1, %eax          ; set eax to -1
.L1: popl %ebx               ; pop ebx from stack
     ret                     ; return to calling program

Notice how the parameters of the write( ) function are loaded into the CPU registers before the int $0x80 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.