2

From the point of view of application programmer the system call is just invocation of a procedure from wrapper library. As I don't have good knowledge of assembly I was wondering how it is done on lowest level. How else can an application invoke system call without using library?

closed as too broad by Scott Chamberlain, Tog, Kevin Panko, random Feb 27 '14 at 2:44

Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 1
    A system call is not the invocation of a procedure from a library. – Maria Ines Parnisari Feb 25 '14 at 21:45
3

System calls are architecture dependent, but they all share similar traits.

In x86, system calls are done either via software interrupts or via a special "systenter" instruction. Linux can use either, but a software interrupt is most common. On boot time, the kernel sets up handlers for interrupts, both interrupts generated by hardware and those generated by software. In x86 there is a special assembly instruction 'int' which generates a software interrupt. The kernel has set up a handler for software interrupt 0x80 to be the system call interrupt.

This table shows each possible system call and its parameters and how they can be passed. As you can see, a number in the eax register indicates which system call. The other registers point to some parameter to the system call.

So the userland software simply sets up the correct parameters in the registers, then issues interrupt 0x80. The kernel has the handler set up to interpret the system call, perform the function, and return having modified the registers with any possible return value.

While wrapper functions do all this and usually in a portable way, there is no reason why you couldn't write some inline assembly in any C/C++ code to issue a system call directly. You would only need a basic knowledge of assembly: Move the correct data in the correct registers and then issue 'int 0x80'

Not the answer you're looking for? Browse other questions tagged or ask your own question.