Lesson 14 of 15

Syscall Table

The System Call Table

When a user program calls read() or write(), it triggers a software interrupt that transfers control to the kernel. The kernel looks up the syscall number in the syscall table — a global array of function pointers — and dispatches to the right handler.

// From arch/x86/entry/syscalls/syscall_64.tbl (abbreviated)
// 0  read
// 1  write
// 2  open
// 3  close
// ...

On x86-64, the syscall number is in rax and the kernel uses it as an index into sys_call_table[].

Your Implementation

typedef void (*syscall_fn)(void);
#define NR_SYSCALLS 16

void register_syscall(syscall_fn table[], int nr, syscall_fn fn) {
    if (nr >= 0 && nr < NR_SYSCALLS) table[nr] = fn;
}

void do_syscall(syscall_fn table[], int nr) {
    if (nr >= 0 && nr < NR_SYSCALLS && table[nr])
        table[nr]();
    else
        printf("unknown syscall %d\n", nr);
}

Your Task

Implement register_syscall and do_syscall for a 16-entry syscall table.

TCC compiler loading...
Loading...
Click "Run" to execute your code.