Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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?

share|improve this question

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

There are either too many possible answers, or good answers would be too long for this format. Please add details to narrow the answer set or to isolate an issue that can be answered in a few paragraphs.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. –  l19 Feb 25 at 21:45

1 Answer 1

up vote 3 down vote accepted

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'

share|improve this answer
1  
In Linux the sysenter instruction may be used instead. trilithium.com/johan/2005/08/linux-gate –  ultrasawblade Feb 26 at 2:09
    
@ultrasawblade I had forgotten about that thanks for mentioning it. –  Dougvj Feb 26 at 2:20

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