1

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?

7
  • The entire syscall() interface is not part of the POSIX standard. Otherwise man 2 syscall would list it under the Standards section the same way man 2 fork does for fork(). Commented Jun 26 at 4:49
  • Ohhh okay, so I'm assuming then that the underlying fork() function could call say int 0x20/eax=5 and it would still be posix compatible due to the fact fork() is implemented? Commented Jun 26 at 4:55
  • 2
    The functionality is specified, not how it is achieved. There is no requirement for it to be achieved with a single system call. For that matter, there is no requirement for it to be a system call if the underlying OS does not use kernel/userspace separation, has a micro-kernel where a system service handles process launches, or whatever else you can imagine. Commented Jun 26 at 5:58
  • Right, I think I understand, as long as it does what is required (i.e. copy the process) it doesn't matter how its achieved (e.g. what interrupt is used) then its acceptable and POSIX compatible. If I'm wrong then please feel free to correct me otherwise thank for the help. Commented Jun 26 at 6:15
  • 1
    BTW: int 0x80 has not been the preferred way to make system calls for a long time. x86 uses sysenter, x86-64 uses syscall. 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 Commented Jun 26 at 13:00

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

5 Comments

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.
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.
On having done it both ways, calling the library functions makes my life more difficult.
The main problem with this answer is writing down the definition of errno in assembly.
Write a C function that returns a pointer to errno and call it from assembly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.