Summary
The general work of adding a system call is:
- Download the Linux kernel
- Add calls in the kernel
- Compile the changed kernel
- Replace the kernel and test
Ready to work
Configuration library
Since a lot of libraries are needed during the compilation process, it needs to be configured in advance. If the errors encountered during the compilation process are recorded in the error message below. The command for the library to be installed is:
sudo apt-get install libncurses5-dev libncursesw5-dev
sudo apt-get install bison flex
sudo apt-get install libelf-dev libssl-dev
Prepare the Linux kernel
Download the Linux kernel you want to compile at https://www.kernel.org . In order to adapt to the following configuration files, the author downloaded version 5.7.9.
After moving the compressed package to the ~
directory, decompress the file, specifically:
xz -d linux-5.7.9.tar.xz
tar -xf linux-5.7.9.tar
Download WSL 2 configuration file
Because it is the process of compiling and replacing the Linux kernel for WSL 2, directly compiling the kernel will not be able to adapt to the WSL 2 system, resulting in failure to start normally. The method of directly compiling the kernel is also recorded at the end of the article. So go to download the relevant configuration file, because the official file is older, so go to GitHub to download the newer version, the address link , the configuration file adapts to the 5.7.x kernel.
Name the file config-wsl
. In linux-5.7.9
creating a new folder under the folder Microsoft
, and config-wsl
move folders Microsoft
in.
The above process is as follows:
Add function
Open the kernel/sys.c
file and add a system call at the end, as follows:
SYSCALL_DEFINE0(hellolyg){
printk("Hello YigeLIU *_*");
return 1;
}
Add function declaration
Open the include/linux/syscalls.h
file and add the function declaration in it, as follows:
Add system call number
Open the file arch/x86/entry/syscalls/syscall_64.tbl
and declare an unused system call number in it. Note that it cannot be declared at the end, because there is such a comment in the file, as follows:
The meaning of this comment is:
The specific system call number for 32-bit systems starts from 512 to avoid cache impact on native 64-bit operations. If X86_X32 is defined, __x32_compat_sys stubs will be dynamically created for the compat_sys_x*() compatibility system calls.
At the same time, call numbers 387 to 423 cannot be used, because there is a comment like this in the file, as follows:
Combined with the above description, after adding the new call number to the common section, it is as follows:
Compile the kernel and replace the kernel
This step is exactly the same as the compilation process in the author's article WSL 2 (Ubuntu18.04) to compile the Linux kernel (5.7.9) and replace the original kernel of WSL 2 , so I won't repeat it. The compilation result is as follows:
Check the kernel version after the replacement and observe the successful update, as follows:
Test system call
Write the test program as follows:
The dmesg
command used after compilation is as follows:
The system call is displayed successfully, as follows:
Some possible problems
Since the author encountered a lot of ups and downs in the compilation process, and each time the compilation did not clear the last compilation information, I don’t know if the final compilation passed because of the following reasons, that kernel/sys.c
is, when the file was first modified , the added custom system call function was defined as :
Reference
https://blog.csdn.net/xiaobai__lee/article/details/101979481
https://blog.csdn.net/qq_41175905/article/details/80529245