在64位系统上向Linux Kernel 3.13添加新的系统调用 [英] Adding new System Call to Linux Kernel 3.13 on 64 bit system
问题描述
我正在尝试向64位系统上的 kernel 3.13.0-37-generic
添加一个简单的 helloworld
系统调用.
I'm trying to add a simple helloworld
System Call to kernel 3.13.0-37-generic
on 64 bit system.
我将尝试逐步展示到目前为止我所做的事情:
1-我已通过以下方式下载了内核源:
sudo apt-get source linux-image-3.13.0-37-generic
然后,将内核源文件提取到/usr/src/
After that, kernel source files extracted to /usr/src/
2-定义一个新的系统调用 sys_hello()
:
2- Define a new system call sys_hello()
:
我已经在/usr/src/linux-3.13/
然后我在 hello
目录中创建了一个 hello.c
文件,其内容如下:
And I created a hello.c
file in hello
directory with below content :
#include <linux/kernel.h>
asmlinkage long sys_hello(void)
{
printk("Hello world\n");
return 0;
}
然后我在hello目录中创建了一个 Makefile
,内容如下:
Then I created a Makefile
in the hello directory with following content :
obj-y := hello.o
3-将hello目录添加到内核的Makefile
我更改了/usr/src/linux-3.13/Makefile
中的以下行:
core-y += kernel/ mm/ fs/ ipc/ security/ crypto/ block/
至:
core-y += kernel/ mm/ fs/ ipc/ security/ crypto/ block/ hello/
4-将新的系统调用 sys_hello()
添加到系统调用表(syscall_64.tbl文件)
4- Add the new system call sys_hello()
into the system call table (syscall_64.tbl file)
因为我使用的是64位系统,所以我需要更改以下文件中的 syscall_64.tbl
文件:
Because I'm using 64 bit system, I need to alter the syscall_64.tbl
file in :
/usr/src/linux-3.13/arch/x86/syscalls/syscall_64.tbl
在文件末尾添加了以下行:
Added the following line in the end of the file :
-最后一行是 313
314 common hello sys_hello
5-在系统调用头文件中添加新的系统调用 sys_hello()
5- Add the new system call sys_hello()
in the system call header file
vim /usr/src/linux-3.13/include/linux/syscalls.h
我已经在文件末尾的#endif语句之前添加了以下行:
I've added the following line to the end of the file just before the #endif statement at the very bottom :
asmlinkage long sys_hello(void);
6-在我的系统上编译此内核
要配置内核,我尝试了以下命令:
To configure the kernel I tried the following command :
sudo make menuconfig
执行上述命令后,会弹出一个窗口,我确保选择了 ext4
,然后选择了保存
.
After above command a pop up window came up and I made sure that ext4
was selected and then save
.
然后:
# cd /usr/src/linux-3.13/
# make
花了2到3个小时.
之后:
# make modules_install install
之后,我重新启动了系统.
After that, I rebooted my system.
7-测试系统调用(问题在这里)
重新启动后,我在主文件夹中创建了一个名为 hello.c
的文件,其内容如下:
After reboot, I created a file with name hello.c
in home folder with following content :
#include <stdio.h>
#include <linux/kernel.h>
#include <sys/syscall.h>
#include <unistd.h>
int main()
{
long int amma = syscall(314); // 314 is the number of sys_hello line in `syscall_64.tbl`
printf("System call sys_hello returned %ld\n", amma);
return 0;
}
然后:
# gcc hello.c
# ./a.out
输出为:
System call sys_hello returned -1
问题完全是 -1
.它必须返回 0
而不是 -1
.
Problem exactly is -1
. It must return 0
not -1
.
似乎没有将 sys_hello
添加到内核系统调用中.
Seems like sys_hello
doesn't added to Kernel System Call.
我在做什么错了?
推荐答案
问题是从步骤6到最后一步(编译内核).
The problem was from step 6 to last step (Compile Kernel).
在第5步之后,我们必须执行以下步骤:
After step 5, we have to do following steps :
6-在我的系统上编译此内核
要配置内核,我尝试了以下命令:
To configure the kernel I tried the following command :
# make menuconfig
执行上述命令后,将弹出一个窗口,我确保已选择ext4,然后保存.
After above command a pop up window came up and I made sure that ext4 was selected and then save.
然后要从新内核创建 DEB
文件,我们必须:
Then to create DEB
file from new kernel we have to :
# make -j 5 KDEB_PKGVERSION=1.arbitrary-name deb-pkg
它将在/usr/src/
中创建一些 deb
文件.
It will create some deb
files in /usr/src/
.
之后,我们需要安装它们:
After that we need to install them :
# dpkg -i linux*.deb
它将在您的系统上安装新内核.
It will install new kernel on your system.
现在,重新启动系统.系统重新启动后,您可以了解是否已安装新内核:
Now, reboot your system. After system rebooted you can find out whether new kernel is installed or not :
$ uname -r
如果您想知道是否将新的系统调用添加到内核中,则只需键入:
And if you want to know your new System Call added to kernel or not just type :
$ cat /proc/kallsyms | grep <system call name>
就我而言:
$ cat /proc/kallsyms | grep hello
以下输出表明您的系统调用已成功添加到内核:
Following output indicates that your System Call successfully added to the Kernel :
0000000000000000 T sys_hello
这篇关于在64位系统上向Linux Kernel 3.13添加新的系统调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!