views:

1206

answers:

3

Explains both UNIX (BSD flavor) & Linux system call conventions for x86-32:

Can any one please tell me or point me to similar doc for x86-64 on both UNIX & Linux?

+5  A: 

Perhaps you're looking for the x86_64 ABI?

If that's not precisely what you're after, use 'x86_64 abi' in your preferred search engine to find alternative references.

Jonathan Leffler
actually, I'm only want the System Call convention. esp for UNIX (FreeBSD)
claws
@claws: the system call convention is one part of the ABI.
Jonathan Leffler
yeah. I've gone to each individual OS's kernel development irc and asked them about it. They've told me to look into the source and figure out. I don't understand without documenting stuff how can they just start developing? So, I've added an answer from info I collected, hoping for others to fill in the rest of the details.
claws
A: 

In addition to the link that Jonathan Leffler provides in his answer, also Agner Fog's Calling Conventions pdf may be useful to you.

PhiS
actually, I'm only want the System Call convention.
claws
+3  A: 

I verified these using Gnu Assembler (gas) on Linux.

User Interface

x86-32 Function Calling convention:

In x86-32 parameters were passed on stack. last paramter was pushed first on to the stack until all parameters are done and then call instruction was executed. This is used for calling C library (libc) functions on linux from assembly.

x86-64 Function Calling convention: (complicated)

I guess because of the reason that we have so many general purpose registers and higher width other registers the function calling mechanism is changed. In this new mechanism. First the parameters are divided into classes. Depending on the class of these parameters parameters are passed.

For complete information refer to : "3.2 Function Calling Sequence" of System V Application Binary Interface AMD64 Architecture Processor Supplement

Here is a snippet from it:

Once arguments are classified, the registers get assigned (in left-to-right order) for passing as follows:

  1. If the class is MEMORY, pass the argument on the stack.
  2. If the class is INTEGER, the next available register of the sequence %rdi, %rsi, %rdx, %rcx, %r8 and %r9 is used

i.e.. %rdi, %rsi, %rdx, %rcx, %r8 and %r9 are the registers in order used to pass parameters to any libc function from assembly. %rdi is used for first parameter. %rsi for 2nd, %rdx for 3rd and so on. Then call instruction should be given.

If parameters are more than 6 then 7th parameter onwards is passed on the stack.

Kernel Interface

x86-32 Linux System Call convention:

In x86-32 parameters for linux system call are passed using registers. %eax for syscall_number %ebx, %ecx, %edx, %esi, %edi, %ebp are used for passing 6 parameters to system calls.

I took fllowing snippet from http://www.cin.ufpe.br/~if817/arquivos/asmtut/index.html#syscalls but I'm doubtful about this. If any one can show an example. It would be great.

If there are more than six arguments, %ebx must contain the memory location where the list of arguments is stored - but don't worry about this because it's unlikely that you'll use a syscall with more than six arguments.

for example and little more reading, refer to http://www.int80h.org/bsdasm/#alternate-calling-convention

x86-32 [Free|Open|Net|DragonFly]BSD UNIX System Call convention:

Parameters are passed on the stack. Push the parameters (last parameter pushed first) on to the stack. Then push an additional 32-bit of dummy data (Its not actually dummy data. refer to following link for more info) and then give a system call instruction int $0x80

http://www.int80h.org/bsdasm/#default-calling-convention

x86-64 Linux & DragonFly BSD System Call convention:

refer to section: "A.2 AMD64 Linux Kernel Conventions" of System V Application Binary Interface AMD64 Architecture Processor Supplement

Here is the snippet from this section:

  1. User-level applications use as integer registers for passing the sequence %rdi, %rsi, %rdx, %rcx, %r8 and %r9. The kernel interface uses %rdi, %rsi, %rdx, %r10, %r8 and %r9.
  2. A system-call is done via the syscall instruction. The kernel destroys registers %rcx and %r11.
  3. The number of the syscall has to be passed in register %rax.
  4. System-calls are limited to six arguments, no argument is passed directly on the stack.
  5. Returning from the syscall, register %rax contains the result of the system-call. A value in the range between -4095 and -1 indicates an error, it is -errno.
  6. Only values of class INTEGER or class MEMORY are passed to the kernel.

x86-64 [Free|Open|Net]BSD UNIX System Call convention:

???

claws