Sorry, your browser cannot access this site
This page requires browser support (enable) JavaScript
Learn more >

一、系统初始化(刚装完系统后)

1️⃣ 配置国内镜像源(两种都写)

备份原 repo

1
2
sudo mkdir -p /etc/yum.repos.d/backup
sudo mv /etc/yum.repos.d/*.repo /etc/yum.repos.d/backup/

写入阿里云源

1
sudo vim /etc/yum.repos.d/centos-stream-ali.repo

内容 原样写入

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
[baseos]
name=CentOS Stream 9 - BaseOS
baseurl=https://mirrors.aliyun.com/centos-stream/9-stream/BaseOS/$basearch/os/
gpgcheck=1
enabled=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial

[appstream]
name=CentOS Stream 9 - AppStream
baseurl=https://mirrors.aliyun.com/centos-stream/9-stream/AppStream/$basearch/os/
gpgcheck=1
enabled=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial

[crb]
name=CentOS Stream 9 - CRB
baseurl=https://mirrors.aliyun.com/centos-stream/9-stream/CRB/$basearch/os/
gpgcheck=1
enabled=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial
1
2
sudo curl -o /etc/yum.repos.d/centos-stream-billma.repo \
https://repo.billma.top/centos9/centos-stream.repo

刷新缓存

1
2
sudo dnf clean all
sudo dnf makecache

2️⃣ 安装内核编译依赖(不使用 epel)

1
2
3
4
5
6
7
8
9
10
sudo dnf groupinstall -y "Development Tools"
sudo dnf install -y \
ncurses-devel \
elfutils-libelf-devel \
openssl-devel \
bc \
flex \
bison \
perl \
dwarves

二、下载并准备 Linux 5.10.220 源码

1
2
3
4
cd ~
wget https://cdn.kernel.org/pub/linux/kernel/v5.x/linux-5.10.220.tar.xz
tar xf linux-5.10.220.tar.xz
cd linux-5.10.220

三、生成并修改 .config你之前卡死的关键步骤

1️⃣ 复制当前系统配置

1
cp /boot/config-$(uname -r) .config

2️⃣ 打开并修改 .config

1
vim .config

必须完成的修改(逐条确认)

搜索并修改模块签名

1
/CONFIG_MODULE_SIG

改为:

1
# CONFIG_MODULE_SIG is not set

搜索并修改证书路径

1
/CONFIG_SYSTEM_TRUSTED_KEYS

改为:

1
CONFIG_SYSTEM_TRUSTED_KEYS=""

如果存在这一项(有就改,没有不用加)

1
/CONFIG_SYSTEM_REVOCATION_KEYS

改为:

1
CONFIG_SYSTEM_REVOCATION_KEYS=""

保存退出

1
:wq

3️⃣ 让配置生效(必须)

1
make olddefconfig

4️⃣ 编译前自检(非常重要)

1
2
grep CONFIG_MODULE_SIG .config
grep CONFIG_SYSTEM_TRUSTED_KEYS .config

必须看到:

1
2
# CONFIG_MODULE_SIG is not set
CONFIG_SYSTEM_TRUSTED_KEYS=""

否则后面一定在 certs/ 报错。


四、添加“计算器”系统调用

1️⃣ 添加系统调用号

1
vim arch/x86/entry/syscalls/syscall_64.tbl

文件末尾添加一行:

1
548    common    calc    __x64_sys_calc

2️⃣ 添加系统调用实现

1
vim kernel/sys.c

直接写在文件末尾(在所有 #endif 之后)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
SYSCALL_DEFINE3(calc, int, a, int, b, char, op)
{
long result = 0;
int i;

switch (op) {
case '+': result = a + b; break;
case '-': result = a - b; break;
case '*': result = a * b; break;
case '/':
if (b == 0) return -EINVAL;
result = a / b;
break;
case '%':
if (b == 0) return -EINVAL;
result = a % b;
break;
case '^':
result = a ^ b;
break;
case 'p': /* 表示 ** */
result = 1;
for (i = 0; i < b; i++)
result *= a;
break;
default:
return -EINVAL;
}

printk(KERN_INFO "sys_calc: %d %c %d = %ld\n", a, op, b, result);
return result;
}

五、编译并安装内核

1
2
3
4
make clean
make -j$(nproc)
sudo make modules_install
sudo make install

六、重启并进入新内核

1
reboot

在 GRUB 中选择 Linux 5.10.220(你刚编译的)


七、用户态验证系统调用

1️⃣ 编写测试程序

1
vim test_calc.c
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <stdio.h>
#include <unistd.h>
#include <sys/syscall.h>

#define __NR_calc 548

int main(void)
{
printf("3 + 4 = %ld\n", syscall(__NR_calc, 3, 4, '+'));
printf("5 ** 3 = %ld\n", syscall(__NR_calc, 5, 3, 'p'));
printf("10 %% 4 = %ld\n", syscall(__NR_calc, 10, 4, '%'));
return 0;
}

2️⃣ 编译并运行

1
2
gcc test_calc.c -o test_calc
./test_calc

3️⃣ 查看内核日志

1
dmesg | tail

应看到:

1
2
3
sys_calc: 3 + 4 = 7
sys_calc: 5 p 3 = 125
sys_calc: 10 % 4 = 2

评论