Re: System Call

Drew Eckhardt (drew@poohsticks.org)
Thu, 20 Jul 1995 00:29:54 -0600


In message <Pine.SUN.3.91.950718165801.13756B-100000@netcom16>, mikenel@netcom.
com writes:
>Can someone explain how a system call works -- on both the user and the
>kernel side?

Sure. A system call operates in the context of the invoking user level
process but in the kernel address space.

On the user side, the syscall number (from <linux/syscall.h>) gets
stuck in the eax register; any arguments end up in ebx, ecx, edx,
esi, and edi (that lets you have five arguments. If you need more,
you need to stick them in a structure), and you do a int 0x80. On
return, you check the result; passing it as-is to the caller if it's
non-negative; putting -return in errno and returning -1 if it is.

On the kernel side, we catch the trap; push all of the registers to
the stack in the right (read as reverse) order, sanity check the
syscall number; if it's good, do an indirect call to the corresponding
syscall entry (sys_whatever), check for things like signals and tracing
syscalls along the way, and when we're done, pop the registers and do
an iret.

The kernel code can access things in user space using the user/fs functions.

You want to look at include/linux/unistd.h; include/i386/unistd.h,
arch/i386/entry.S, and arch/i386/traps.c.