# Linux Kernel Module Cheat

### 28.7. Linux system calls

The following Userland setup programs illustrate how to make system calls:

- x86_64

  - userland/arch/x86_64/freestanding/linux/hello.S

  - userland/arch/x86_64/freestanding/linux/int_system_call.S

  - userland/arch/x86_64/inline_asm/freestanding/linux/hello.c: this shows how to do system calls from inline assembly without any C standard library helpers like `syscall`

  - userland/arch/x86_64/inline_asm/freestanding/linux/hello_regvar.c: same as userland/arch/x86_64/inline_asm/freestanding/linux/hello.c but using register variables instead of register constraints

- arm

  - userland/arch/arm/freestanding/linux/hello.S

  - userland/arch/arm/inline_asm/freestanding/linux/hello.c: there are no register constraints in ARM, so register variables are the most efficient way of storing variables in specific general purpose registers: https://stackoverflow.com/questions/3929442/how-to-specify-an-individual-register-as-constraint-in-arm-gcc-inline-assembly/54845046#54845046

- aarch64

  - userland/arch/aarch64/freestanding/linux/hello.S

  - userland/arch/aarch64/inline_asm/freestanding/linux/hello.c

  - userland/arch/aarch64/inline_asm/freestanding/linux/hello_clobbers.c

Determining the ARM syscall numbers:

- https://reverseengineering.stackexchange.com/questions/16917/arm64-syscalls-table

- arm: https://github.com/torvalds/linux/blob/v4.17/arch/arm/tools/syscall.tbl

- aarch64: https://github.com/torvalds/linux/blob/v4.17/include/uapi/asm-generic/unistd.h

Determining the ARM syscall interface:

- https://stackoverflow.com/questions/12946958/what-is-the-interface-for-arm-system-calls-and-where-is-it-defined-in-the-linux

- https://stackoverflow.com/questions/45742869/linux-syscall-conventions-for-armv8

Questions about the C inline assembly examples:

- x86_64: https://stackoverflow.com/questions/9506353/how-to-invoke-a-system-call-via-sysenter-in-inline-assembly/54956854#54956854

- ARM: https://stackoverflow.com/questions/21729497/doing-a-syscall-without-libc-using-arm-inline-assembly

- futex system call

- <code>getcpu</code> system call and the <code>sched_getaffinity</code> glibc wrapper

- <code>perf_event_open</code> system call