Skip to content
systemcoding.dev
Kernels

Syscall Calling Conventions on x86-64, ARM64 and RISC-V

Compare Linux system calls on x86-64, ARM64 and RISC-V: the trap instruction, number register, argument registers and the same hello program in each.

6 min read
Summarize in AI
Flat illustration of an assembly listing with addresses and mnemonics, with the Intel, Arm and RISC-V logos.

A system call is the same idea on every CPU, but the details are different on each one: which instruction enters the kernel, which register holds the call number, which registers carry arguments, and what the call numbers themselves are. If you write assembly, a runtime, a tracer or a seccomp filter, you must get these right for each target. This post compares the three architectures most Linux developers meet today, x86-64, ARM64 (AArch64) and 64-bit RISC-V, using a Linux 6.12 kernel, and ends with the same "hello" program written for each.

The comparison in one table

x86-64ARM64RISC-V 64
Trap instructionsyscallsvc #0ecall
Call numberraxx8a7
Arguments 1 to 6rdi, rsi, rdx, r10, r8, r9x0 to x5a0 to a5
Resultraxx0a0
Registers the kernel may clobberrcx, r11none beyond the resultnone beyond the result
write16464
exit609393
exit_group2319494
getpid39172172

The authoritative source for this is the syscall(2) manual page, which has a table of the registers for each architecture, and the kernel's own entry code under arch/<arch>/. The numbers come from arch/x86/entry/syscalls/syscall_64.tbl for x86-64 and from the shared table in include/uapi/asm-generic/unistd.h for ARM64 and RISC-V.

Why ARM64 and RISC-V share numbers

ARM64 and RISC-V were added to Linux after the old system calls had accumulated historical baggage. Neither architecture has the legacy interfaces such as open, fork, dup2 or stat in the kernel; they use only the modern replacements (openat, clone, dup3, fstatat). Both use the generic table in asm-generic/unistd.h, which is why their numbers match.

x86-64 predates that clean-up and keeps its original numbering, with the legacy calls still present. The order of the numbers there reflects the history of the i386 interface, not any logic. The practical effect is that a table of numbers is only valid for one architecture, and a program that hard-codes 1 for write works on x86-64 and writes to the wrong place elsewhere.

x86-64 in detail

The syscall instruction does three things in hardware: it copies the address of the next instruction into rcx, copies the flags register into r11, and jumps to the kernel entry address stored in the LSTAR register. That is why rcx and r11 are clobbered by every system call. The kernel return path uses sysretq, which restores the instruction pointer and flags from those two registers.

The fourth argument is r10, not rcx as in the C calling convention. The mismatch is a direct consequence of syscall overwriting rcx: the kernel could not receive a value in a register the instruction destroys. Glibc wrappers copy rcx to r10 before the trap.

hello_x86_64.sasm
.intel_syntax noprefix
.globl _start
.text
_start:
	mov	eax, 1
	mov	edi, 1
	lea	rsi, [rip + msg]
	mov	edx, 6
	syscall
	mov	eax, 60
	xor	edi, edi
	syscall

.section .rodata
msg:	.ascii "hello\n"
Shell
as -o hello_x86_64.o hello_x86_64.s
ld -o hello_x86_64 hello_x86_64.o
./hello_x86_64
Text
hello

ARM64 in detail

On ARM64 the trap is the supervisor call, svc #0. The immediate is ignored by Linux; the kernel reads the number from x8 instead. The CPU takes a synchronous exception from the lower exception level (EL0, user) to EL1, where the vector table sends it to the code that handles system calls. Linux's handler is el0_svc in arch/arm64/kernel/syscall.c. Only x0 carries the result, and the kernel preserves all other registers that the AArch64 calling convention does not allow it to change, so there are no rcx-style surprises.

hello_arm64.sasm
.globl _start
.text
_start:
	mov	x0, #1			// fd = stdout
	adr	x1, msg
	mov	x2, #6
	mov	x8, #64			// write
	svc	#0
	mov	x0, #0
	mov	x8, #93			// exit
	svc	#0

.section .rodata
msg:	.ascii "hello\n"
Shell
aarch64-linux-gnu-as -o hello_arm64.o hello_arm64.s
aarch64-linux-gnu-ld -o hello_arm64 hello_arm64.o
Text
hello

The output shows the program running on an ARM64 machine or under an emulator; the program text is what matters here. The tool names assume a cross-binutils package for AArch64 on an x86-64 host.

RISC-V in detail

RISC-V uses ecall, "environment call", which raises a trap from user mode to the next higher privilege level. Linux runs in supervisor mode and handles the trap in arch/riscv/kernel/entry.S. The number goes in a7, the arguments in a0 to a5, and the result comes back in a0. The register names a0 to a7 are the ABI names for x10 to x17.

hello_riscv64.sasm
.globl _start
.text
_start:
	li	a0, 1
	la	a1, msg
	li	a2, 6
	li	a7, 64
	ecall
	li	a0, 0
	li	a7, 93
	ecall

.section .rodata
msg:	.ascii "hello\n"
Shell
riscv64-linux-gnu-as -o hello_riscv64.o hello_riscv64.s
riscv64-linux-gnu-ld -o hello_riscv64 hello_riscv64.o
Text
hello

The listings carry no comments because the comment character differs between the three assemblers; the ARM64 one uses // and the others use #. Compare the three listings. The structure is identical: load the file descriptor, the address and the length; load the number; trap; then repeat for exit with a status of zero. Only the registers, the trap instruction and the numbers change.

Return values and errors

On all three architectures covered here, an error is reported as a negative number in the result register, in the range -4095 to -1, and the C library converts it to errno. This is the same convention described in the previous posts in this cluster. Some other architectures differ: MIPS and Alpha return a positive error number and set a separate error flag in a register, and the glibc wrapper reads the flag. That difference is handled below the C library and is invisible to ordinary programs, but it matters if you write raw assembly for those targets.

Differences that trip people up

Three details cause most mistakes when porting code between these architectures.

  • Missing legacy calls. open, stat, fork, pipe, dup2 and getdents do not exist on ARM64 or RISC-V. Use openat, newfstatat, clone with fork flags, pipe2, dup3 and getdents64. A seccomp filter or an strace script keyed on x86-64 names will not match.

  • **Argument order for clone.** The fourth and fifth arguments of clone are swapped on some architectures. The clone(2) manual page documents this, so check it before writing a raw call.

  • Seccomp architecture field. A seccomp filter sees an arch field in struct seccomp_data along with the number. A filter written for x86-64 numbers must check arch first, or it misreads the number on a different ABI, including the 32-bit compat interface on the same machine.

Reading the numbers from your own system

You do not need to memorize the tables. The kernel headers on a development machine contain the numbers for the architecture it targets:

Shell
grep -E '__NR_(write|exit_group|getpid) ' /usr/include/asm/unistd_64.h
Text
#define __NR_write 1
#define __NR_getpid 39
#define __NR_exit_group 231

On an ARM64 or RISC-V machine, the same command against its headers prints the numbers 64, 172 and 94. The header path varies by distribution and by cross-compiler sysroot. In C, use the SYS_ constants from <sys/syscall.h> instead of numbers, so the code compiles correctly for each target.

When portability is the goal

For portable code, do not issue the trap yourself. Use the C library, or syscall() with the SYS_ constants, so the compiler and library choose the right registers and numbers. Write the architecture-specific code only when there is no C library, as in a minimal static program or a runtime that ships its own system call layer, and keep one small file per architecture so each stays easy to read.

Also inAssembly
Share

Keep reading

Get new posts by email

We send one email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.