# add syscall to linux kernel (v6.0.6) ### 系統環境 * 作業系統: ubuntu 18.04 * Kernel 版本: 5.4.0-131-generic ### 下載 Kernel Source 到 [Kernel Archive](https://www.kernel.org/) 下載 kernel source,選擇最新的 6.0.6 kernel。 **<font size=4>解壓縮</font>** ``` # 進入 root 模式 sudo su # 把檔案解壓縮到 /usr/src 目錄底下 tar -xvf linux-6.0.6.tar.xz -C /usr/src ``` ### 新增 syscall **<font size=4>建立資料夾</font>** ``` # 把目錄轉到剛剛解壓縮完的 kernel 檔案夾 cd /usr/src/linux-6.0.6 # 在裡面創建一個名叫 hello 的資料夾 mkdir hello # 把目錄轉到 hello 資料夾 cd hello ``` **<font size=4>建立 hello.c</font>** ``` vim hello.c ``` **<font size=4>hello.c 的內容</font>** ``` #include <linux/kernel.h> #include <linux/syscalls.h> SYSCALL_DEFINE0(hello){ printk("Hello world!\n"); return 0; } ``` **<font size=4>於同一個目錄下再建立一個 Makefile</font>** ``` vim Makefile ``` **<font size=4>Makefile 的內容</font>** ``` obj-y := hello.o ``` **<font size=4>接著回上個目錄改 Makefile</font>** ``` # 回上個目錄 cd .. # 打開此目錄下的 Makefile vim Makefile ``` **<font size=4>可以找到 core-y 如下圖</font>**  **<font size=4>在最後面補上 hello 這樣 kernel 在編譯時才會到 hello 目錄</font>** ``` core-y += kernel/ certs/ mm/ fs/ ipc/ security/ crypto/ hello/ ``` **<font size=4>接下來要去修改 syscall_64.tbl 檔</font>** ``` cd arch/x86/entry/syscalls vim syscall_64.tbl ``` **<font size=4>在最後一行添加上 system call,然後請把編號記住, 等一下會用</font>**  ``` 註:會發現下面突然跳到 400 多號,那是放 x32 的東西,我們要擺在 300 多號那邊 ``` **<font size=4>接著編輯 syscalls.h 檔,將 syscall 的原型添加進檔案 (#endif之前)</font>** ``` # 把目錄轉回去 cd /usr/src/linux-6.0.6 cd include/linux vim syscalls.h ```  ### 編譯 kernel **<font size=4>先把會用到的套件安裝好</font>** ``` # 這個套件可以幫我們 build 出 kernel-pakage sudo apt-get install fakeroot build-essential kernel-package libncurses5 libncurses5-dev ``` **<font size=4>package configuration (會跳出一個視窗)</font>** ``` 選擇第二個,保持本地版本就好 ``` **<font size=4>複製 kernel 的 config</font>** ``` # 把目錄轉回去 cd /usr/src/linux-6.0.6 cp /boot/config-`uname -r` ./.config ``` **<font size=4>然後可以用 make menuconfig 來設定組態</font>** ``` make menuconfig ``` **<font size=4>會跳出一個畫面可以設定 configuration,直接離開就好,</font>** **<font size=4>然後就可以 compile 你的 kernel 了</font>** ``` # 清理殘留的 package make-kpkg clean # 編譯 kernel fakeroot make-kpkg -j 8 --initrd kernel_image kernel_headers # 等編譯完後會產生 kernel 的 image 和 headers ``` ### 編譯完成 **<font size=4>編譯完成後會在 /usr/src 路徑下產生了這個 kernel 的 image 和 headers, 回到上個目錄安裝它們</font>** ``` # 轉目錄 cd /usr/src # 安裝 kernel image sudo dpkg -i linux-image-6.0.6_6.0.6-10.00.Custom_amd64.deb # 安裝 kernel headers sudo dpkg -i linux-headers-6.0.6_6.0.6-10.00.Custom_amd64.deb # 安裝完後就重開機 reboot ``` **<font size=4>重開機後再看看 kernel 版本有沒有變新</font>** ``` uname -r ```  ### 測試 syscall ``` # 創建一個 hello.c vim hello.c ``` **<font size=4>hello.c 的內容如下</font>** ``` #include <linux/kernel.h> #include <unistd.h> #include <sys/syscall.h> #include <stdio.h> int main(){ /* 使用我們剛剛新增的 system call */ long int sys = syscall(335); /* print 出 syscall 的回傳值, 若為 0 則代表成功 */ printf("sys_hello return %ld\n", sys); return 0; } ``` ``` # 編譯 hello.c gcc -o hello hello.c # 執行 ./hello ``` **<font size=4>使用 dmesg 來查看 kernel 內的訊息</font>** ``` dmesg ```  ### reference [https://wenyuangg.github.io/posts/linux/linux-add-system-call.html](https://wenyuangg.github.io/posts/linux/linux-add-system-call.html) [https://blog.csdn.net/cswhl/article/details/110919572](https://blog.csdn.net/cswhl/article/details/110919572) [https://linuxgazette.net/112/krishnakumar.html](https://linuxgazette.net/112/krishnakumar.html) [https://phoenixnap.com/kb/build-linux-kernel](https://phoenixnap.com/kb/build-linux-kernel) [https://biscuitos.github.io/blog/SYSCALL_PARAMENTER_ZERO/](https://biscuitos.github.io/blog/SYSCALL_PARAMENTER_ZERO/) [https://stackoverflow.com/questions/10949986/whats-meaning-of-obj-y-something-in-linux-kernel-makefile](https://stackoverflow.com/questions/10949986/whats-meaning-of-obj-y-something-in-linux-kernel-makefile)
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up