|
Figure 8 - System Call Code Stub
#include <syscall.h>
.globl _write, _errno
# amtwritten = write(fildes, address, count);
_write: # caller places arguments on stack
lea SYS_write,%eax # select desired system call
lcall $0x7,0 # call the system
jb 1f # if system returns error, handle
ret # otherwise return
1: movl %eax,_errno # save error in global variable
movl $-1,%eax # indicate error has occured
ret # and return
Figure 8 is a code template for the system call stub used in 386BSD, in this case a write system call. The lcall instruction is an intersegmental call instruction that references a special segment selector, known to be a UNIX system call gate into the kernel. The selector corresponds to the first descriptor in the processes local descriptor table. To designate which system call is to be used, the eax register is loaded with the index from the table. Arguments for each system call are present on the stack, and this stub is called from another procedure. System calls return after the lcall instruction, returning values in the eax and edx registers (just as other C procedures do). System calls report failure by setting the carry bit and recording error notification in eax.
|