I've been researching the POSIX specifications and I'm a little confused by them as to what they define as a syscall. Lets take fork() for example I know it got to be in the C library but I'm a little confused as to the underlying implementation. On Linux the syscall is int 0x80/eax=2 but is there anything in the POSIX specification that says that needs to be how it's called or is it OS dependent?
1 Answer
POSIX specifies a list of library functions (the system interfaces) a POSIX-compatible system must provide. It does not specify how these are to be implemented or whether they are system calls or not.
If you want to do system calls from assembly, I recommend going through these POSIX functions instead of trying to mess with “raw” system calls by directly issuing int $0x80 or similar instructions. This sidesteps the entire question of whether something is a system call or not and makes your program easier to port to other POSIX systems.
5 Comments
Peter Cordes
In my opinion, it's fun to use system calls directly a few times when you're learning how things work. To understand how CPUs work and how system calls work, it's good to understand that the system-call ABI involves
int or a similar instruction to enter kernel mode with args at a known position where the kernel will look for them. (Often in registers, but 32-bit x86 Darwin / OS X and *BSD use the user-space stack, so the libc wrappers can leave the args where they are.) But sure, your advice is reasonable in general.fuz
Unfortunately most tutorials do raw system calls to show that this is what happens under the hood, so people think that this is how you are supposed to program in assembly. You are not! You'll just make your life much more difficult this way. I am giving these answers to steer people towards more conventional software design practices so they don't get lost in these irrelevant details.
Joshua
On having done it both ways, calling the library functions makes my life more difficult.
Joshua
The main problem with this answer is writing down the definition of errno in assembly.
fuz
Write a C function that returns a pointer to
errno and call it from assembly.
syscall()interface is not part of the POSIX standard. Otherwiseman 2 syscallwould list it under the Standards section the same wayman 2 forkdoes forfork().int 0x80has not been the preferred way to make system calls for a long time. x86 usessysenter, x86-64 usessyscall. See here. And of course ARM etc. have their own conventions. Which is another reason it's not part of POSIX as there is no way to standardize assembly