Chinaunix首页 | 论坛 | 博客
  • 博客访问: 62369
  • 博文数量: 12
  • 博客积分: 469
  • 博客等级: 二等列兵
  • 技术积分: 155
  • 用 户 组: 普通用户
  • 注册时间: 2012-05-24 22:00
文章分类

全部博文(12)

文章存档

2014年(2)

2012年(10)

分类: LINUX

2012-07-27 15:43:50

  添加一个简单的系统调用
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. }
























阅读(1541) | 评论(4) | 转发(1) |
0

上一篇:函数调用

下一篇:系统调用的初始化

给主人留下些什么吧!~~

hangkong772012-07-29 09:11:38

Bean_lee: 系统调用不是说以trap的形式 实现吗? 你这个算是内核编程吧, 虽然指定了个系统调用号。 当然我不是太清楚.....
在内核中添加自己的系统调用。我是新手,正在学习中。

Bean_lee2012-07-28 15:04:46

系统调用不是说以trap的形式 实现吗? 你这个算是内核编程吧, 虽然指定了个系统调用号。 当然我不是太清楚

hangkong772012-07-27 20:39:54

zhe_wang: printk(“%d\n”,x);  你这个x是哪儿来的?你写的系统调用服务例程中没有参数阿。.....
打错了

zhe_wang2012-07-27 18:30:10

printk(“%d\n”,x);  你这个x是哪儿来的?你写的系统调用服务例程中没有参数阿。