Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1392057
  • 博文数量: 120
  • 博客积分: 182
  • 博客等级: 入伍新兵
  • 技术积分: 2278
  • 用 户 组: 普通用户
  • 注册时间: 2012-07-19 16:31
文章分类

全部博文(120)

文章存档

2015年(12)

2014年(13)

2013年(40)

2012年(55)

分类:

2012-07-28 10:12:56

原文地址:添加系统调用 作者:hangkong77

  添加一个简单的系统调用
1 谈加系统调用号:
   系统调用号是在inistd.h中定义的。内核中每个系统调用号都以 __NR_开头。
在arch/86/include/asm/unistd_32.h

点击(此处)折叠或打开

  1. 341 #define __NR_preadv 333
  2. 342 #define __NR_pwritev 334
  3. 343 #define __NR_rt_tgsigqueueinfo 335
  4. 344 #define __NR_perf_event_open 336
  5. 345 #define __NR_recvmmsg 337
  6. 346 #define __NR_mysyscall 338 <<<<<<此处为添加的系统调用
  7. 347
  8. 348 #ifdef __KERNEL__
  9. 349
  10. 350 #define NR_syscalls 339 <<<<<< 这里一定要记着改
 /usr/include/asm/unistd_32.h

点击(此处)折叠或打开

  1. 341 #define __NR_preadv 333
  2. 342 #define __NR_pwritev 334
  3. 343 #define __NR_rt_tgsigqueueinfo 335
  4. 344 #define __NR_perf_event_open 336
  5. 345 #define __NR_recvmmsg 337
  6. 346 #define __NR_mysyscall 338
添加系统调用号以后,系统才能把这个号作为索引去查找系统调用表 sys_call_table 中的对应项。

2。在系统调用表中添加相应的表项
 arch/x86/kernel/syscall_table_32.S

点击(此处)折叠或打开

  1. 1   ENTRY(sys_call_table)
  2. 2     .long sys_restart_syscall3  /* 0 - old "setup()" system call, used for r    estarting */
  3.     ...
  4. 338   .long sys_perf_event_open
  5. 339   .long sys_recvmmsg
  6. 340   .long sys_mysyscall   /*338*/
3。实现系统调用服务例程

点击(此处)折叠或打开

  1. asmlinkage int sys_mysyscall(int x)

  2. {

  3.     printk(“hello this is my syscall\n”);
  4.     printk(%d\n”,x);

  5. }
asmlinkage 是gcc中的一个特殊标志。gcc 常用的一种编译优化方法是使用寄存器传递函数的参数,而加了 asmlinkage 修饰符的函数必须从堆栈中而不是寄存器中获取参数。
4。重新编译内核
5。编写用户程序测试

点击(此处)折叠或打开

  1. #include<stdio.h>
  2. main()
  3. {
  4.     syscall(338,1);
  5. }
























阅读(1176) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~