# system call explanation in the book



## jronald (Mar 19, 2010)

In the book "The design and implementation of the FreeBSD Operating System",
it explain the return value of system call as below.
I have some questions:
1. Is the success/fail flag passed by the carry bit of psw or eax?
2. when to set the value of errno?



> Eventually, the system call returns to the calling process, either successfully or unsuccessfully. On the PC architecture, success or failure is returned as the carry bit in the user process's program status longword: If it is zero, the return was successful; otherwise, it was unsuccessful. On many machines, return values of C functions are passed back through a general-purpose register (for the PC, data register EAX). The routines in the kernel that implement system calls return the values that are normally associated with the global variable errno. After a system call, the kernel system-call handler leaves this value in the register. If the system call failed, a C library routine moves that value into errno, and sets the return register to -1. The calling process is expected to notice the value of the return register, and then to examine errno. The mechanism involving the carry bit and the global variable errno exists for historical reasons derived from the PDP-11.


----------



## misha (Mar 20, 2010)

http://www.freebsd.org/doc/en/books/developers-handbook/x86-return-values.html
Where Are the Return Values?
For most system calls it is in EAX, but not for all

can you clarify your second question?


----------



## jronald (Mar 21, 2010)

misha said:
			
		

> http://www.freebsd.org/doc/en/books/developers-handbook/x86-return-values.html
> Where Are the Return Values?
> For most system calls it is in EAX, but not for all
> 
> can you clarify your second question?



Thanks, the introduction in the handbook is clear for the first question.
For the second one, I want to know when the variable 'errno' is set.
After read the text in the handbook, I think I have the answer, it should be in the libc function as below:

```
void libc_func()
{
    sys_call();
    if (psw_cf is set)
    {
        sys_call_return_value = -1;
        errno = eax;
    }        
    else
        sys_call_return_value = eax;
}
```


----------

