Linux内核添加系统调用
注意,光是更换内核的整个过程就会大致耗时2h+!
建议使用4.9以下内核版本,否则编译时间会过长
首先测试更换内核
版本说明
环境版本
kun@ubuntu:~$ cat /etc/issue
Ubuntu 16.04.6 LTS
下载链接:https://cdimage.ubuntu.com/ubuntu-mate/releases/16.04/release/ubuntu-mate-16.04.6-desktop-amd64.iso
请自行用vmware安装
Ubuntu版本查看方法:
kun@ubuntu:~$ cat /proc/version
Linux version 4.15.0-45-generic (buildd@lcy01-amd64-027) (gcc version 5.4.0 20160609 (Ubuntu 5.4.0-6ubuntu1~16.04.10)) #48~16.04.1-Ubuntu SMP Tue Jan 29 18:03:48 UTC 2019
或者使用
uname -r
linux内核版本
linux-4.6
内核下载链接:https://www.kernel.org/pub/linux/kernel/v4.x/linux-4.6.tar.xz
下载linux-4.6.tar.xz文件后复制到Ubuntu的文件系统中,我将复制到了Downloads/文件下。
Linux内核编译
准备编译
解压准备
xz -d Downloads/linux-4.6.tar.xz
tar –xvf Downloads/linux-4.6.tar
sudo cp -r Downloads/linux-4.6 /usr/src
cd /usr/src
编译配置
cp linux-headers-4.15.0-45-generic/.config linux-4.6/
cd linux-4.6/
内核配置
内核提供了各种不同的工具来简化内核配置,make config/gconfig/oldconfig等,下面以基于ncurse库编制的图形界面工具配置。
make menuconfig
出现可视化菜单,选择load→OK→Save→OK→EXIT→EXIT。
执行完,则内核配置完毕。
内核配置不成功的错误
如果没有出现可视化菜单,而是出现错误:
root@ubuntu:/usr/src/linux-4.6# make menuconfig
HOSTCC scripts/basic/fixdep
HOSTCC scripts/kconfig/mconf.o
In file included from scripts/kconfig/mconf.c:23:0:
scripts/kconfig/lxdialog/dialog.h:38:20: fatal error: curses.h: No such file or directory
compilation terminated.
scripts/Makefile.host:108: recipe for target 'scripts/kconfig/mconf.o' failed
make[1]: *** [scripts/kconfig/mconf.o] Error 1
Makefile:538: recipe for target 'menuconfig' failed
make: *** [menuconfig] Error 2
解决方法:
sudo apt-get update
sudo apt-get install build-essential
sudo apt-get install libncurses5
sudo apt-get install libncurses5-dev
重新尝试,在可视化菜单中选择load→OK→Save→OK→EXIT→EXIT
root@ubuntu:/usr/src/kernel/linux-4.6# make menuconfig
HOSTCC scripts/kconfig/mconf.o
HOSTCC scripts/kconfig/zconf.tab.o
HOSTCC scripts/kconfig/lxdialog/checklist.o
HOSTCC scripts/kconfig/lxdialog/util.o
HOSTCC scripts/kconfig/lxdialog/inputbox.o
HOSTCC scripts/kconfig/lxdialog/textbox.o
HOSTCC scripts/kconfig/lxdialog/yesno.o
HOSTCC scripts/kconfig/lxdialog/menubox.o
HOSTLD scripts/kconfig/mconf
scripts/kconfig/mconf Kconfig
*** End of the configuration.
*** Execute 'make' to start the build or try 'make help'.
如果出现仍错误,如下
scripts/kconfig/mconf Kconfig
.config:4244:warning: symbol value 'm' invalid for GPIO_MB86S7X
Your display is too small to run Menuconfig!
It must be at least 19 lines by 80 columns.
scripts/kconfig/Makefile:28: recipe for target 'menuconfig' failed
make[1]: *** [menuconfig] Error 1
Makefile:538: recipe for target 'menuconfig' failed
make: *** [menuconfig] Error 2
则只需要调大终端窗口大小重试即可。
其他的配置工具简介如下:
make config ;字符界面下
make gconfig ;基于gk+的图形工具
make defconfig ;基于默认配置
make oldconfig ;修改过配置文件后,或者用已有配置文件配置新的代码树的时候,应该执行
编译和安装内核
编译内核
仍然在你的linux-4.6/下执行,可能耗时数小时:
make
老版的编译方式会编译启动映像make bzImage。这里不使用,make就可以搞定。
可能出现错误:
scripts/sign-file.c:25:30: fatal error: openssl/opensslv.h: No such file or directory
compilation terminated.
scripts/Makefile.host:91: recipe for target 'scripts/sign-file' failed
make[1]: *** [scripts/sign-file] Error 1
Makefile:552: recipe for target 'scripts' failed
make: *** [scripts] Error 2
解决方法:
sudo apt-get install libssl-dev
重新尝试make,开始等待。结束结果如下:
Setup is 17404 bytes (padded to 17408 bytes).
System is 6942 kB
CRC f4cb1ce4
Kernel: arch/x86/boot/bzImage is ready (#1)
如遇问题可以尝试先使用make clean再make
sudo make clean
安装内核模块:
sudo make modules_install
就可以把已编译的模块安装到正确的主目录/lib/modules下。
sudo make install
安装内核模块
这一步生成新内核的引导文件。重启系统:
sudo reboot
最后开机时狂按shift,进入高级选项,然后再选择跟换的内核启动即可
过程结束。
添加系统调用:
修改sys.c
sudo vim /usr/src/linux-4.6/kernel/sys.c
在末尾贴入以下代码:
SYSCALL_DEFINE1(gaoxi,char,n)
{
printk("gaoxi的系统调用!");
if(n>='0'&&n<='9')return 0;
if(n>='a'&&n<='z')return 1;
else return 2;
}
修改syscalls.h
sudo vim /usr/src/linux-4.6/include/linux/syscalls.h
#在末尾添加
asmlinkage long sys_gaoxi(char n);
修改syscall_64.tbl
sudo vim /usr/src/linux-4.6/arch/x86/entry/syscalls/syscall_64.tbl

开始编译
可以使用如下命令开始编译:
sudo make
或者使用多线程,n表示cpu核数*2,如果是两核就写j4:
sudo make -jn
如果你使用现有的配置文件,这会是一个漫长的过程...
two years later...
OK,现在你已经编译完成了,使用如下命令来安装吧~
#为内核安装模块
`sudo make modules_install`
#将内核安装到当前系统
`sudo make install`
这一步也许会很久,也许一下就好了
校验系统调用
这里提供两种方案,第一种是查询关键字
cat /proc/kallsyms | grep gaoxi
或者写一个C程序,vim test.c,内容如下:
#include<stdio.h>
#include<unistd.h>
#include<linux/kernel.h>
#include<error.h>
#include<sys/syscall.h>
int main()
{
char n;
long int t;
printf("Please Enter a symbol:");
scanf("%c",&n);
//这里用到了系统调用号
t=syscall(329,n);
printf("System call sys_gaoxi_syscall return %ld\n",t);
if(t==0)
printf("This is a number.\n");
else if(t==1)
printf("This is a small letter.\n");
else
printf("This is another notation.\n");
return 0;
}
使用gcc编译
gcc -o test.out test.c
执行
./test.out
根据提示输入一个字符,即可看到系统调用的返回值即相应的判断结果:


浙公网安备 33010602011771号