In the name of each macro the numbers 0 through 6

This preview shows page 21 - 22 out of 22 pages.

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 + 2 x n 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:
Image of page 21

Subscribe to view the full document.

_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)-129) { 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 $-125, %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 -129 (the kernel assumes that the largest error code defined in include/generic/errno.h is 129). 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 .
Image of page 22
You've reached the end of this preview.
  • Spring '12
  • GwangS.Jung
  • Assembly Language, Virtual memory, Subroutine, Control flow

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern