<< Chapter < Page Chapter >> Page >

rv = read(0,buff,sizeof(buff));

Somewhere, deep down in the operating system kernel, is a function that implements this read operation. For example, in Linux, the routine is called sys_read().

The path from the simple read() function call in your program to the sys_read() routine in the kernel takes you through some interesting and crucial magic. The path goes from your code to a system call stub function that contains a trap or interrupts instruction, to an interrupt handler in the kernel, to the actual kernel function. The return path is similar, with the addition of some important interactions with the process dispatcher.

System call stub functions

The system call stub functions provide a high-level language interface to a function whose main job is to generate the software interrupt (trap) needed to get the kernel's attention. These functions are often called wrappers.

The stub functions on most operating systems do the same basic steps. While the details of implementation differ, they include the following:

(1)

set up the parameters,

(2)

trap to the kernel,

(3)

check the return value when the kernel returns, and

(4)

(a)

if no error: return immediately, else

(b)

if there is an error: set a global error number variable (called "errno") and return a value of -1.

Below are annotated examples of this code from both the Linux (x86) and Solaris (SPARC) version of the C library. As an exercise, for the Linux and Solaris versions of the code, divide the code into the parts described above and label each part.

x86 Linux read (glibc 2.1.3)

read: push %ebx mov 0x10(%esp,1),%edx ; put the 3 parms in registersmov 0xc(%esp,1),%ecx mov 0x8(%esp,1),%ebxmov $0x3,%eax ; 3 is the syscall # for read int $0x80 ; trap to kernelpop %ebx cmp $0xfffff001,%eax ; check return valuejae read_err read_ret: ret ; return if OK.read_err: push %ebx call read_next ; push PC on stackread_next: pop %ebx ; pop PC off stack to %ebx xor %edx,%edx ; clear %edxadd $0x49a9,%ebx ; the following is a bunch of sub %eax,%edx ; ...messy stuff that sets thepush %edx ; ...value fo the errno variable call 0x4000dfc0<__errno_location>pop %ecx pop %ebxmov %ecx,(%eax) or $0xffffffff,%eax ; set return value to -1jmp read_ret ; return

SPARC Solaris 8

read: st %o0,[%sp+0x44] ! save argument 1 (fd) on stackread_retry: mov 3,%g1 ! 3 is the syscall # for read ta 8 ! trap to kernelbcc read_ret ! branch if no error cmp %o0,0x5b ! check for interrupt syscallbe,a read_retry ! ... and restart if so ld [%sp+0x44],%o0 ! restore 1st param (fd) mov %o7,%g1 ! save return addresscall read_next ! set %o7 to PC sethi %hi(0x1d800), %o5 ! the following is a bunch ofread_next: or %o5, 0x10, %o5 ! ...messy stuff that sets the add %o5,%o7,%o5 ! ...value of the errno variablemov %g1, %o7 ! ...by calling _cerror. also the ld [%o5+0xe28],%o5 ! ...return value is set to -1 jmp %o5nop read_ret: retlnop

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Operating systems. OpenStax CNX. Aug 13, 2009 Download for free at http://cnx.org/content/col10785/1.2
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Operating systems' conversation and receive update notifications?

Ask