Page 1 of 1

how to start with system calls

Posted: Sun Apr 09, 2006 4:47 am
by vibhory2j
hi all,

i am new to os development and i have implemented basic tables, paging and keyboard drivers . now i want to add system calls to my operating system. Please give me some good resources to implement system calls.

thanks in advance for any help

Re:how to start with system calls

Posted: Sun Apr 09, 2006 6:23 am
by kernel64_lazy
System calls are software-initiated interrupts/traps. So the C library that supports your OS (if you have one) hides the system call inside the functions like printf, scanf, etc. If you don't have a C library then you can provide C function prototypes yourself (along with stubs which would be assembler) or the system calls can be accessed with assembly language, because assembler is the only language that can access the interrupt or trap call directly.

What happens on the x86 is that the kernel decides on an interrupt vector number as the system call handler. This vector number is selected among those available on the architecture. (The CPU and other hardware devices would have some of those already). This is the "global" system call handler, say, sys_interrupt.

Let's suppose that you choose on the x86, like Linux does (and maybe lots of other systems) to pass the system call number in eax. The system call number selects which actual system call the caller wants to invoke, like read/write/socket or whatever. Then the kernel can use this vector number in the sys_interrupt handler to index into a table of handlers (function pointers for example) and call the correct one. The vector number has no relationship to the interrupt number that you assign to call the "global" interrupt handler, sys_interrupt.

The other parameters are typically passed in other registers. Since the x86 is starved of registers, sometimes they might get passed in with a pointer to wherever the arguments are.

There are more details like checking for a valid system call number, saving and restoring registers, maybe checking for a needed reschedule afterwards before returning, updating process statistics and so forth. I recommend you read the source code for Minix and Linux. (For Linux, see /kernel/sys.c for some miscellaneous system call code, and /arch/i386/kernel, especially entry.S, for x86 system call code that does the handling and low-level stuff. It's quite hairy, so perhaps see the Minix code first.)

For more on system calls read the relevant chapter in Linux Kernel Development by Robert Love (2nd Edition) (and maybe read the whole book because it's a fine read and you can pick things up that theoretically-inclined OS books neglect).

More experienced kernel hackers might like to clarify on the above...

HTH.

Re:how to start with system calls

Posted: Sun Apr 09, 2006 6:24 am
by Solar
http://www.mega-tokyo.com/forum/index.php?board=1;action=search, searching the OS development board for "system calls" as phrase. The relevant parts of the Intel Manuals also provide information on the various ways to do this (SYSCALL / SYSENTER / INT / ... ).

Re:how to start with system calls

Posted: Sun Apr 09, 2006 2:25 pm
by GLneo
did you use a tutorial? becouse some (bkerndev) make it easyer than others.

p.s. in your sig I think you mispelled "off"