What are the calling conventions for UNIX & Linux system calls on i386 and x86-64

A system call is the fundamental interface between an application and the Linux kernel. When a Unix/Linux program does file I/O, network data transfer, or invokes processes that interact with low-level instructions, system calls are involved. Making these calls usually involves using a library called glibc which contains the functions.

Common System Calls

Below is a list of some frequently used system calls and their purpose ?

Sr.No System Call Purpose
1 chmod change permissions of a file
2 chdir change working directory
3 fork create a child process
4 unlink delete a name and possibly the file it refers to

What is a Calling Convention?

A systems programmer writes programs that specify which system call to use rather than directly making system calls. This involves using a calling convention which depends on the hardware architecture where the kernel runs. Different architectures have different calling conventions.

A calling convention is an implementation-level design for how subroutines receive parameters from their caller and how results are returned. It defines where parameters, return values, return addresses, and scope links are placed (registers, stack, or memory), and how the tasks of preparing for and cleaning up after a function call are divided between caller and callee.

Calling Convention Variations

Calling conventions vary between different architectures in several ways ?

  • Which registers the called function must preserve for the caller
  • How the task of setting up for and cleaning up after a function call is divided between caller and callee
  • How return values are delivered from callee back to caller (stack, register, or heap)
  • Where parameters, return values, and return addresses are placed
  • The order in which actual arguments for formal parameters are passed

System Call Implementation Example

Here's how you can make a system call using Python's os module, which internally uses the appropriate calling convention ?

import os

# Change directory (uses chdir system call)
original_dir = os.getcwd()
print(f"Original directory: {original_dir}")

# Create a test directory
test_dir = "/tmp/test_syscall"
try:
    os.mkdir(test_dir)
    print(f"Created directory: {test_dir}")
    
    # Change to the new directory
    os.chdir(test_dir)
    print(f"Changed to: {os.getcwd()}")
    
    # Change file permissions (uses chmod system call)
    test_file = "test.txt"
    with open(test_file, 'w') as f:
        f.write("System call test")
    
    # Set read-only permissions
    os.chmod(test_file, 0o444)
    print(f"File permissions changed to read-only")
    
finally:
    # Cleanup
    os.chdir(original_dir)
    if os.path.exists(test_dir + "/test.txt"):
        os.chmod(test_dir + "/test.txt", 0o644)
        os.remove(test_dir + "/test.txt")
    if os.path.exists(test_dir):
        os.rmdir(test_dir)
Original directory: /home/user
Created directory: /tmp/test_syscall
Changed to: /tmp/test_syscall
File permissions changed to read-only

Comparing i386 and x86-64 Calling Conventions

A single CPU architecture can have multiple calling conventions, but the industry has agreed on general approaches. The 32-bit architecture has different register usage compared to x86-64, which extends x86's 8 general-purpose registers to 64-bit ?

Aspect i386 (32-bit) x86-64 (64-bit)
System Call Registers %ebx, %ecx, %edx, %esi, %edi, %ebp %rdi, %rsi, %rdx, %r10, %r8, %r9
Parameter Passing Parameters passed on stack using PUSH. If >6 args, %ebx contains memory location of argument list System calls limited to 6 arguments. 7th+ integer parameters passed on stack
Return Value Stored in %eax register Stored in %rax register
Parameter Order Last parameter pushed first onto stack, then call instruction executed Parameters divided into classes, each class determines passing method
ABI Compatibility 32-bit ABI usable in 64-bit code 64-bit ABI cannot be used in 32-bit systems

Conclusion

System call calling conventions differ significantly between i386 and x86-64 architectures, primarily in register usage and parameter passing mechanisms. Understanding these differences is crucial for low-level programming and system optimization.

Updated on: 2026-03-15T17:27:21+05:30

950 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements