# 筆記: Linux Kernel - 新增 system call (Linux 5.15.74) ###### tags: `Linux` ## 步驟: 1. 安裝vmware workstation player 2. 安裝 Ubuntu 20.04LTS 3. sudo apt update 4. sudo apt install python3 build-essential libncurses-dev libssl-dev libelf-dev bison flex dwarves bc cpio zstd -y 5. wget https://cdn.kernel.org/pub/linux/kernel/v5.x/linux-5.15.74.tar.xz 6. tar -xf linux-5.15.74.tar.xz 7. cd linux-5.15.74 <!-- 程式碼部分 --> 8. 修改linux-5.15.74/arch/x86/entry/syscalls/syscall_64.tbl,新增一行表格: ``` 548 common hello sys_hello ``` 9. cd linux-5.15.74 10. mkdir hello 11. cd hello 12. touch hello.c 13. hello.c 完成以下內容 ```c= #include <linux/kernel.h> #include <linux/syscalls.h> SYSCALL_DEFINE1(hello, int, n) { char str[64]; sprintf(str, "=====HELLO=LINUX=KERNEL[n=%d]=====\n", n); printk(KERN_INFO(str)); return 0; } ``` 14. touch Makefile 15. Makefile 內容設為 ```Makefile= obj-y := hello.o ``` 15. 回到上一層,修改這裡的Makefile,找到 `core-y += kernel/ certs/ mm/ fs/ ipc/ security/ crypto/ block/` (或類似的,可以用`core-y`去找),在尾端加上` hello/` 16. 更改 linux-5.15.74/include/linux/syscalls.h,在檔案尾端#endif之前新增一行 ```c asmlinkage long sys_hello(int n); ``` ``` [--:--] make oldconfig [14:50] sudo make -j$(nproc) [15:56] # still runnung... [16:20] #OK [16:42] sudo make modules_install -j$(nproc) [16:32] #OK [16:33] sudo make install -j$(nproc) [16:37] #OK [16:38] sudo reboot ``` ``` [16:52] sudo make -j$(nproc) [17:52] # still running ``` ## 心得(抱怨) + 虛擬機所需要的硬碟空間,VMWARE預設的20GB根本不夠啊(後來設為64GB),= = + 有篇學長的文章common打成commom ,我還以為我原本的打錯,編譯完才發現完蛋= = + 實驗室主機真的太慢...,第一次Make到reboot要花快2個小時= = + 學長提供建議,若Kernel編譯過程太漫長,可以試試用`CCACHE` + 使用方法`sudo make CC="ccache gcc" -j$(nproc)` + [Speeding Up Linux Kernel Builds With Ccache](http://nickdesaulniers.github.io/blog/2018/06/02/speeding-up-linux-kernel-builds-with-ccache/) + 測試結果: 好像快一點 ## 參考資料 + [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) + 2022小組筆記(凱哥超罩) + [Linux OS - Project 1](https://hackmd.io/@NsmHODqwS72fJnV4ZaWwdA/B1Lp9pz4j) + [Linux OS - Project 2](https://hackmd.io/@NsmHODqwS72fJnV4ZaWwdA/Hy9t1nTvo) + [Linux OS - Project 3](https://hackmd.io/@NsmHODqwS72fJnV4ZaWwdA/B1ViZcFti) + [2021學長的](https://hackmd.io/aoUBV9NDTLOJiwt0VsMevA) <- 打錯common的那篇 + 5-level page system https://stackoverflow.com/questions/58743052/getting-error-when-compiling-kernel-for-page-table-walk + [透過proc/pid/pagemap獲取實體記憶體位址](http://fivelinesofcode.blogspot.com/2014/03/how-to-translate-virtual-to-physical.html) + [中文版](https://www.cnblogs.com/pengdonglin137/p/6802108.html) + [How to access physical addresses from user space in Linux?](https://stackoverflow.com/questions/12040303/how-to-access-physical-addresses-from-user-space-in-linux) + [Linux的进程地址空间[三]](https://zhuanlan.zhihu.com/p/68398179) <!-- + ![mm_struct所標示的segment位址圖](https://i.imgur.com/4xCQGl3.png) --> + [Linux thread 與 mm_struct 關係](https://medium.com/@yovan/os-process-thread-user-kernel-%E7%AD%86%E8%A8%98-aa6e04d35002) <!-- + ![課程投影片](https://i.imgur.com/8inuNTj.png) --> <!-- + ![Project1 概念圖](https://i.imgur.com/9oWgqG7.png) --> + [Linux 的Thread Local Storege 介紹](https://blog.csdn.net/u014426028/article/details/108037971) <!-- + ![TLS](https://i.imgur.com/sNeP8wU.png) --> + [模擬MMU獲得physical address](https://stackoverflow.com/questions/41090469/linux-kernel-how-to-get-physical-address-memory-management) + [虚拟地址转换为物理地址](https://blog.csdn.net/shuningzhang/article/details/38090621) <!-- + ![](https://i.imgur.com/3nPYspi.png) --> + [5 level page tables](https://stackoverflow.com/questions/58743052/getting-error-when-compiling-kernel-for-page-table-walk) + Linux kernel 4.11 之後就改為5 level 的系統(舊版是4-level) + https://tw511.com/a/01/17017.html + 2.3 4-Level Paging 再來看一個4級頁表分頁模式,支援的頁大小是==4KB==、2MB和1GB。 以4KB頁大小為例,如下圖所示,顯然相比於32-bit Paging,4-Level Paging擴充套件了==線性地址(48bit)== 和實體地址(52bit)。 隨著計算機的發展,32bit的地址空間顯很侷促,尤其是物理定址範圍也只有32bit,即4GB實體地址空間,在計算機發展初期4GB空間是天文數位,現在已經淪落到選擇個人PC都看不上4GB記憶體,起碼是8GB記憶體起步。這也是為什麼X86 32位元CPU支援PAE(Physical Address Extension)的原因。同樣的,ARM-v7也支援了LPAE(Large Physical Address Extension),名字雖不同,但困境如出一轍。 ![](https://i.imgur.com/Vk7vUkp.png) + 5-level page 與 4-level page 最大差異在於 linear address width 的最大寬度支援由48到了57,實體記憶體位址最大寬度上限與支援的page size是一樣的。 + ![](https://i.imgur.com/FEHGH9o.png) + x86架構基本上都是由CR3暫存器指向第一個page table(如上圖)。 + https://blog.csdn.net/pwl999/article/details/109453180