Implementation of Linux system calls
-
- Preface
- 01 Experiment content
- 02 Experimental environment
- 03 Replace domestic sources
- 04 Install the dependencies required for compilation
- 05 Get the kernel source code
- 06 Add custom system calls
- 07 Add function declaration and definition
- 08 Compile the kernel
- 09 Verification
- 10 PS—Kernel removal
- Afterword
Preface
I didn’t learn the operating system for a semester. When I really thought about making up for the experiments I owed, I realized that it was really torture. I made mistakes six or seven times, so I suddenly had the idea to record the entire experiment process. Let this experiment at least not be entirely fruitless.
01 Experiment content
- Complete kernel compilation
- Complete a system call
02 Experimental environment
- VMware Workstation Pro 16
- Ubuntu 20.04 LTS
- Linux Kernel 5.10.2
To install Ubuntu on VMware, please refer to: Tutorial link
Try to download everything from the official website. The failure of several of my experiments was due to problems with the images I downloaded from other channels. It was really torture.
And try to allocate 55GB of memory when selecting storage space at the beginning
1) To avoid compilation failure due to insufficient space during later compilation
2) If you do not allocate enough memory at the beginning, it will be very troublesome to reallocate it after the installation is completed (don’t ask How do I know? In the end I chose to uninstall and reinstall :)
Remember to change settings after installation
03 Replace domestic sources
The default source is an external source, and domestic access is very slow or even unavailable at all, so we changed it to a domestic source before the experiment.
Back up the original source
sudo cp /etc/apt/sources.list /etc/apt/sources.list.bak
Open source file
sudo gedit /etc/apt/sources.list
Modify to the corresponding version mirror source
Generally, we will choose Alibaba source or Tsinghua source for domestic image sources (the author used Alibaba source during the experiment, which will be faster.
Aliyuan 20.04 LTS
deb http://mirrors.aliyun.com/ubuntu/ focal main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ focal main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ focal-security main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ focal-security main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ focal-updates main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ focal-updates main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ focal-proposed main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ focal-proposed main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ focal-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ focal-backports main restricted universe multiverse
Tsinghuayuan 20.04LTS
# 默认注释了源码镜像以提高 apt update 速度,如有需要可自行取消注释
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-updates main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-updates main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-backports main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-backports main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-security main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-security main restricted universe multiverse
renew
sudo apt-get update
sudo apt-get upgrade
04 Install the dependencies required for compilation
I have not had any problems during this step of installation. After reading some information on the Internet, if an error is reported at this step, it may be that the version of the previous image source does not match the system version.
sudo apt-get install libc6-dev
sudo apt-get install libelf-dev
sudo apt-get install libncurses5-dev libssl-dev
sudo apt-get install build-essential openssl
sudo apt-get install libidn11-dev libidn11
sudo apt-get install zlibc minizip
sudo apt-get install bison
sudo apt-get install flex
sudo apt-get install pkg-config
05 Get the kernel source code
View the currently used kernel
uname -r (查看当前内核版本
dpkg --get-selections | grep linux(查看所有相关配置文件
Generally, choose a kernel that is higher than the current default version. The kernel source code can be downloaded from the official website . The version I chose is: Linux Kernel 5.10.2 (it is said that this version has fewer problems.
Save the installation package in a certain directory, then right-click in this directory to open a terminal and enter the command to obtain permissions (if you have already obtained permissions, enter it later under root, without adding sudo
sudo su
unzip
tar -xavf linux-5.10.2.tar.xz
06 Add custom system calls
First, enter the directory where the decompressed kernel source code is located.
cd linux-5.10.2
Open the syscall_64.tbl file with the custom system call number and add the custom system call after the last common
sudo gedit arch/x86/entry/syscalls/syscall_64.tbl
07 Add function declaration and definition
Open the syscalls.h file
sudo gedit include/linux/syscalls.h
Add function declaration at the end
/*mq's syscall */
asmlinkage long sys_mysyscall(int number);
Open the sys.c file
sudo gedit kernel/sys.c
Add function definition at the end
/*mq's syscall */
SYSCALL_DEFINE1(mysyscall,int,number)
{
printk("mysyscall\n");
printk("The Number You Enter Is %d\n",number);
return number;
}
08 Compile the kernel
Clean kernel files
sudo make mrproper
Delete various compilation files, configuration files and various backup files
sudo make clean
Automatically select the compilation scope based on the modules loaded in the kernel (saves time compared to make meunconfig), press Enter all the way and OK
make localmodconfig
Modify the configuration file (big pit!!
First enter the configuration file, then press Ctrl+F to search for CONFIG_SYSTEM_TRUSTED_KEYS and set it to empty
. If all process selections are consistent with the author, then this parameter in the .config file is originally set to empty.
sudo gedit .config
Compilation (the number behind is how many cores are used and how many cores are used)
sudo make -j8
The last folder compiled is sound, which can be used to determine whether the compilation process is terminated midway.
Reset the kernel (I have failed at this step many times. If this step fails, check what you did wrong in the previous process!
sudo make modules_install
sudo make install
09 Verification
Restart
reboot
Check the current kernel version . If it is the kernel version just compiled, the experiment is successful.
uname -r
Verify added system calls
- Open the editor and write a C program
sudo gedit test.c
#include <linux/kernel.h>
#include <sys/syscall.h>
#include <unistd.h>
#include <stdio.h>
int main(int argc,char **argv)
{
//441:long sys_mysyscall(int)
long temp;
temp = syscall(441,1314);
printf("mysyscall return %ld\n",temp);
return 0;
}
- Save, exit and run
sudo gcc -o test test.c
sudo ./test
- Output 1314 is success
10 PS—Kernel removal
There were a few experiments where I wanted to change the kernel version midway, so I uninstalled the virtual machine and reinstalled it. Later I found that I could just delete the kernel... I'm here from Chubby.
View all kernel files
dpkg --get-selections | grep linux
Uninstall (the installed file has the word install after it, and after uninstalling it, it is deinstall
sudo apt-get remove + 文件名
Delete configuration files (more complete cleanup
sudo apt-get purge
renew
sudo update-grub
Afterword
Anyway, the experiment finally succeeded = =. The steps of the subsequent experiments were obviously exactly the same but there were still problems (so weird!!), and then I just changed to a mirrored version (even weirder orz), so the computer is really It’s metaphysics (I’m sure).