# Add a system call (in x86 QEMU Environment)
<style>
.blue {
color: blue;
}
.bgblue {
color: blue;
font-size: 24px;
font-weight: bold;
}
.red {
color: red;
font-size: 24px;
font-weight: bold;
}
h1 {text-align: center;}
</style>
Authors: WhoAmI, CrazyMonkey
Date: 20231012
E-mail: kccddb@gmail.com
Copyright: CC BY-NC-SA
這是方便讀者使用 QEMU 進行實驗
**主要讓 讀者 理解 system call 的運作原理**
基本方法可參考:
1. 這與 Linux kernel 版本 和 arch 有關, 這裡是 Linux kernel 3.8
2. [與QEMU 版本也有關, 不要使用太新的, QEMU 2.x.x i386 32bits 版, 否則太複雜不容易懂]
3. qemu-2.0.0, 可以用新的 與 自己用 busybox 做的 rootfs
4. (https://download.qemu.org/)
:::info
請參考:
[Adding a system call in X86 QEMU Environment](https://linuxseekernel.blogspot.com/2014/07/adding-system-call-in-x86-qemu.html)
:::
Linux kernel 3.8
https://mirrors.edge.kernel.org/pub/linux/kernel/
wget https://mirrors.edge.kernel.org/pub/linux/kernel/v3.x/linux-3.8.tar.gz
**不過 不幸的是 3.xx gcc version 5.xx 不能用, 因此只能用 4.x.x 的 kernel**
:::info
linux 4.4.50:
https://elixir.bootlin.com/linux/v4.4.50/source/arch/x86/entry/syscalls/syscall_32.tbl
也注意 unistd.h 中 NR_syscall的大小
:::
syscall_32.tbl
https://elixir.bootlin.com/linux/v3.8/source/arch/x86/syscalls/syscall_32.tbl
syscall.h
https://elixir.bootlin.com/linux/v3.8/source/include/linux/syscalls.h
**重新 編譯 linux 3.8**
**當然QEMU 使用新的 kernel/rootfs 開機來執行**
---
其中 user mode 部分
作者提的:
(a)
syscall() function in unistd.h can be used with syscall number to invoke it. In our case, 351.
**這裏提的 unistd.h 是指 gcc tools 內的 unistd.h**
(b)
If this application is compiled with -static then there is no need for C libs. It works fine.
這是指的是:
1. 假設 application 名稱 hello.c
2. gcc -static hello.c -o hello
**也練習使用 ldd, strace, ... 觀察 有無 static 的差別**
[Adding A System Call To The Linux Kernel (5.8.1) In Ubuntu (20.04 LTS)](https://dev.to/jasper/adding-a-system-call-to-the-linux-kernel-5-8-1-in-ubuntu-20-04-lts-2ga8)
:::success
Futher Reading:
[Adding A System Call To The Linux Kernel (5.8.1) In Ubuntu (20.04 LTS)](https://dev.to/jasper/adding-a-system-call-to-the-linux-kernel-5-8-1-in-ubuntu-20-04-lts-2ga8)
[System Calls](https://linux-kernel-labs.github.io/refs/heads/master/lectures/syscalls.html)
[ long syscall(long number, ...);](https://man7.org/linux/man-pages/man2/syscall.2.html)
:::