December
18th
2006

Adding a New System Call into the Linux Kernel 2.6

电脑技术 没有评论

评分: 很差劲不怎样还可以还不错太棒了
Loading ... Loading ...

最近在学习关于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/

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

2. Un-compress and un-archive

  1. 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.

  1. 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:

  1. #include #include asmlinkage int sys_mysyscall(void)
  2. {
  3. printk("This is the my new system call build in the kernel!\n");
  4. return 0;
  5. }

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.

  1. 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:

  1. #include #define __NR_mysyscall 318
  2. _syscall0(long, mysyscall);
  3. int main ()
  4. {
  5. mysyscall();
  6. return 0;
  7. }

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

  1. gcc -Wall testsyscall.c -o testsyscall.o

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

  1. ./testsyscall.o

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

  1. 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.

  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • Twitter
  • Live
  • LinkedIn
  • Yahoo! Bookmarks
  • RSS
  • email
  • Print
  • PDF

相关文章阅读

喜欢这篇文章?订阅我的RSS,就可以自动获取最新内容了!

Tags:.
评论数量: 没有评论
引用: http://www.i4wei.com/2006/12/1/


  1. 没有评论.
  1. 还没有trackbacks.