1

I am following a Linux System Programming Video Tutorial.

When I reached "how to add your own Linux System Call" section, the instructor shows that all System Call IDs (macros starting with __NR) are present in arch/x86/include/asm/unistd_32.h or unistd_64.h (depending on the target).

But in my source code(linux-5.0.1) I do not see those files, there's just one unistd.h which does not contain the system call IDs. Were these files moved elsewhere or does x86 not have its own system call table now.

Edit: I downloaded the latest kernel source code from kernel.org and I am trying to modify it. I cannot find unistd_32.h and unistd_64.h files at the aforementioned location. Do I need to do something first?

1 Answers1

2

Arch Linux ships unistd_32.h and unistd_64.h in /usr/include/asm/. Just look at those headers unless you're modifying the kernel to add new system calls.

<asm/unistd.h> checks macros to figure out if its being included in 32 or 64-bit code (and checks for x32), and uses #include to pull in the right set of definitions for the target.

On my up-to-date x86-64 Arch system:

$ pacman -Fo /usr/include/asm/unistd*
usr/include/asm/unistd_32.h is owned by core/linux-api-headers 4.7-1
usr/include/asm/unistd_64.h is owned by core/linux-api-headers 4.7-1
usr/include/asm/unistd.h is owned by core/linux-api-headers 4.7-1
usr/include/asm/unistd_x32.h is owned by core/linux-api-headers 4.7-1

In the kernel source itself, starting with version 3.3, the unistd_32.h for use by user-space is built from other files.

https://github.com/torvalds/linux/search?q=unistd_32.h&unscoped_q=unistd_32.h finds this in arch/x86/entry/syscalls/Makefile

$(uapi)/unistd_32.h: $(syscall32) $(syshdr)
    $(call if_changed,syshdr)

The syscall tables are defined in: arch/x86/entry/syscalls/syscall_32.tbl and .../syscall_64.tbl

https://github.com/torvalds/linux/tree/6f0d349d922ba44e4348a17a78ea51b7135965b1/arch/x86/entry/syscalls

The contents of syscall_32.tbl looks like:

# some comments
0   i386    restart_syscall     sys_restart_syscall     __ia32_sys_restart_syscall
1   i386    exit            sys_exit            __ia32_sys_exit
2   i386    fork            sys_fork            __ia32_sys_fork
3   i386    read            sys_read            __ia32_sys_read
...
Hadi Brais
  • 22,259
  • 3
  • 54
  • 95
Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
  • I downloaded the latest kernel source code from kernel.org and I am trying to modify it. I cannot find unistd_32.h and unistd_64.h files at the aforementioned location. Do I need to do something first? –  Mar 14 '19 at 15:10
  • @ShaikMuhammadYahiya: you don't modify them, you modify `syscall_32.tbl` and 64.tbl, then run `make` to *generate* the user-space headers. Like my answer says, they're not part of the kernel *source* directly. – Peter Cordes Mar 14 '19 at 16:22
  • 1
    @ShaikMuhammadYahiya Your tutorial is probably using some version of Linux older than 3.3. Indeed, on these versions, `unistd_32.h` and `unistd_64.h` did exist in the directories you have specified in the question. You can instead download v3.2 if you want to follow the tutorial. – Hadi Brais Mar 14 '19 at 16:24
  • @HadiBrais Yes, you are right, the instructor has Linux-2.6.32.7. But I would want to be updated with latest Linux Kernel, right? What do you suggest, I do? –  Mar 15 '19 at 07:42
  • @PeterCordes Thanks, looks like there's a different Linux Kernel version that the instructor is using. –  Mar 15 '19 at 07:44
  • @ShaikMuhammadYahiya It's better to use 2.6.32.7 then so that you can easily follow the tutorial. If this answers your question, you should click on the check mark below the vote arrows to accept the answer. – Hadi Brais Mar 15 '19 at 15:57
  • 1
    @HadiBrais: IDK about using an ancient kernel version. Yes there will probably be other differences, too, each a possible stumbling block, but the end goal is to learn your way around current Linux, not to give yourself 9 years of catching up to do. (Although once you understand some basics, this specific Q&A was trivial for me to figure out. Seeing it as a target in a Makefile told me it was built from something else, not a proper source file itself.) If there are any newer tutorials, finding one might be a better bet. – Peter Cordes Mar 15 '19 at 16:35