【内核】Linux添加系统调用(静态,非动态加载模块)

来源:互联网 发布:电脑电子琴软件 编辑:程序博客网 时间:2024/06/05 02:29

环境:linux enterprise 5

内核:linux-2.6.18

整理:吴龙平

第一步:解压内核

转到/usr/src目录下,将从官网下载过来的linux-2.6.18.tar.bz2拷到该目录下,并解压:

官方网站http://www.kernel.org/pub/linux/kernel/,下载。

国内可在http://download.chinaunix.net/download/0007000/6156.shtml有各种版本2.4.x,2.6.x版本下载。

*******************************************************************************

[root@localhost src]# pwd

/usr/src

[root@localhost src]# ls

kernels  linux-2.6.18.tar.bz2  redhat

[root@localhost src]# tar -xvjf linux-2.6.18

kernels  linux-2.6.18  linux-2.6.18.tar.bz2  redhat

*******************************************************************************

第二步:添加自己希望调用的内核代码

*******************************************************************************

[root@localhost kernel]# pwd

/usr/src/linux-2.6.18/kernel

[root@localhost kernel]# vim sys.c

代码:

asmlinkage int sys_mycall(void)

{

        printk("this is wulongping's first sys_call!/n");

        return 0;

}

如果你不知道具体的位置,请添加该文件的末端

*******************************************************************************

第三步:将你的内核函数添加系统的调用清单中去:

*******************************************************************************

[root@localhost asm-i386]# pwd

/usr/src/linux-2.6.18/include/asm-i386

[root@localhost asm-i386]# vim unistd.h

代码清单:

#define __NR_mycall             318                     //你的函数名字,加上调用号

#define NR_syscalls               319            //这是系统调用总数

*******************************************************************************

注意:1、在没有任何验证的情况下,将你的系统调用添加到清单的最后,勿插队。

2、系统调用总数一定要加上你的系统调用个数。

第四步:添加新的内核函数指针

*******************************************************************************

[root@localhost kernel]# pwd

/usr/src/linux-2.6.18/arch/i386/kernel

[root@localhost kernel]# vim syscall_table.S

在末端添加

代码清单:

        .long sys_mycall

*******************************************************************************

第五步:配置config文件,编译内核

*******************************************************************************

转至:

[root@localhost linux-2.6.18]# pwd

/usr/src/linux-2.6.18

[root@localhost linux-2.6.18]# make menuconfig

不做任何事情,退出并保存即可;

[r oot@localhost linux-2.6.18]# make modules

[root@localhost linux-2.6.18]# make modules_install

这一步大约一个小时,请耐心等待。

*******************************************************************************

第六步:制作镜像文件

*******************************************************************************

make bzImage生成的bzImage文件拷到/boot

[root@localhost linux-2.6.18]# cp /usr/src/linux-2.6.18/arch/i386/boot/bzImage /boot/

并且改名为

[root@localhost boot]# mv bzImage vmlinuz-2.6.18

制作新的内核版本号

[root@localhost boot]# mkinitrd initrd-2.6.18.img 2.6.18

*******************************************************************************

第七步:修改启动文件

*******************************************************************************

[root@localhost grub]# pwd

/boot/grub

[root@localhost grub]# vim menu.lst

添加如下命令配置:

title Red Hat Enterprise Linux Test

        root (hd0,0)

        kernel /vmlinuz-2.6.18 ro root=/dev/VolGroup00/LogVol00 rhgb quiet

        initrd /initrd-2.6.18.img

*******************************************************************************

第八步:测试

*******************************************************************************

[root@localhost grub]# reboot

重新启动后,按任意键进入Red Hat Enterprise Linux Test

待启动完毕之后,测试

代码清单:

#include <sys/unistd.h>

#include <sys/syscall.h>

int main()

{

        syscall(318);

        return 0;

}

测试结果如下:

[root@localhost home]# ./test

[root@localhost home]# dmesg

this is wulongping's first sys_call!

*******************************************************************************

完。

原创粉丝点击