免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1497 | 回复: 0
打印 上一主题 下一主题

[原创]Adding a New System Call into the Linux Kernel 2.6 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2006-11-20 11:49 |只看该作者 |倒序浏览
最近在学习关于Linux的Kernel以及modules方面的内容,今天刚试验成功了一次,特此写出具体过程,分享一下我的经验。

==========================================
Stage 1: To modify the kernel to add our own system call
==========================================

1. Download the Linux kernel 2.6.18 from kernel.org to /usr/src/

cd /usr/src/ && wget http://www.kernel.org/pub/linux/kernel/v2.6/linux-2.6.18.tar.gz

2. Un-compress and un-archive

tar zxvf linux-2.6.18.tar.gz

3. Link it to directory linux, from this point on, there a several files that needs to be modified from and added to the kernel source tree.

ln -s linux-2.6.18 linux

4. Edit file /usr/src/linux/arch/i386/kernel/syscall_table.S

Add
.long sys_mysyscall
after the last line of the file.

5. Edit file /usr/src/linux/arch/i386/kernel/Makefile
Add
obj-y += mysyscall.o

6. Create a new file call mysyscall.c under /usr/src/linux/arch/i386/kernel/ directory.
Type the following into the file:

#include <linux/linkage.h>
#include <linux/kernel.h>

asmlinkage int sys_mysyscall(void)
{
printk("This is the my new system call build in the kernel!\n");
return 0;
}

7. Edit file /usr/src/linux/include/asm-i386/unistd.h
Add
#define __NR_mysyscall              318
after
#define __NR_move_pages         317

Modify
#define __NR_syscalls           318
to
#define __NR_syscalls           319

8. At this point, you can start building your new kernel.

make menuconfig && make && make modules_install && make install

9.After a successful build, reboot the machine and boot into the newly built kernel.

===========================
Stage 2: To test the new system call
===========================

10.Create a new file call testsyscall.c in your directory.
Type the following into the file:

#include <linux/unistd.h>

#define __NR_mysyscall          318

_syscall0(long, mysyscall);

int main ()
{
mysyscall();
return 0;
}

11.Compile the above testsyscall.c source code with :

gcci -Wall testsyscall.c -o testsyscall.o

12.If you arrive here without problems, you can now test the newly created system call just type

./testsyscall.o

13. To check if it works, you can use:

dmesg | tail -n 5

you will see there is "This is the my new system call build in the kernel!\n" in the last line.
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP