免费注册 查看新帖 |

Chinaunix

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

在 Linux Kernel 内新增一个 System Call [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2003-05-24 19:26 |只看该作者 |倒序浏览

  1. 注:偶忘记在那里发现的了,文章虽然很简单,但是对初学者还是很不错哦
  2. 如何在 Linux Kernel 内新增一个 System Call
  3.   

  4. 使用 system call 去呼叫系统的函式是非常好玩的,但是要如何写出一个自己的system call 呢?这边有以下数个步骤,如果你是自己想要呼叫 system call 那可以跳过 (1) (2) 直接到第三项这边假设你己经熟悉 kernel 的 compile 和如何利用新 compile 的 kernel 重新开机

  5. (1) 设定 include 档内的 syscall function

  6. 首先,找到 /usr/inlcude/asm/unistd.h 这个档案,在这一行

  7. #define __NR_getdents64 220
  8. #define __NR_fcntl64 221



  9. 的后面加上 :
  10. #define __NR_myfunc 222



  11. 然后找到 /usr/include/bits/syscall.h 这个档案,再加上一行 :


  12. #define SYS_myfunc __NR_myfunc



  13. 找到 /usr/src/linux/arch/i386/kernel/entry.S 这个档案也是在最后面加上并修改标记为红色的这二行

  14. .long SYMBOL_NAME(sys_getdents64) /* 220 */
  15. .long SYMBOL_NAME(sys_fcntl64)
  16. .long SYMBOL_NAME(sys_myfunc)                   -->; 增加这一行
  17. #ifdef CONFIG_TUX
  18. .long SYMBOL_NAME(__sys_tux)
  19. #else
  20. # ifdef CONFIG_TUX_MODULE
  21. .long SYMBOL_NAME(sys_tux)
  22. # endif
  23. #endif

  24. /*
  25. * NOTE!! This doesn't have to be exact - we just have
  26. * to make sure we have _enough_ of the "sys_ni_syscall"
  27. * entries. Don't panic if you notice that this hasn't
  28. * been shrunk every time we add a new system call.
  29. */
  30. .rept NR_syscalls-222                  ---->; 改成 NR_syscalls-223
  31. .long SYMBOL_NAME(sys_ni_syscall)
  32. .endr




  33. (2) 撰写 syscall 的范例程序


  34. 假设你的 linux kernel code 在 /usr/src/linux 下找到 /usr/src/linux/kernel/sys.c

  35. 加上以上这几行 :


  36. asmlinkageintsys_myfunc(int input){
  37. printk("<1>; Input value is : %d \n",input);
  38. return input*10;
  39. }



  40. 改完以后,就可以重新 compile kernel 并且重新开机了。

  41. (3) 撰写 user space 的小程序


  42. use_syscall.c
  43. #include <stdio.h>;
  44. #include <stdlib.h>;
  45. #include <linux/unistd.h>;

  46. static inline _syscall1(int,myfunc,int,a)

  47. int main(void){
  48. printf("Return Value: %d\n",myfunc(10));
  49. }



  50. 这样执行完以后,你就可以看到这个程序输出 100

  51. 如果你还有兴趣,可以使用 tail -f /var/log/message 会出现类似的讯息,表示你的程序有经由 printk 印到画面上


  52. Sep 3 22:02:02 private kernel: Input value is : 10



  53. _syscall1 是一个 macro 指令,事实上是 _syscallN 的指令 , N 代表系统呼叫所需要用到的参数个数

  54. _syscallN(arg1,arg2,arg3,arg4) :

  55. arg1 : 代表的是传回值
  56. arg2 : 代表的是要呼叫的 syscall name
  57. arg3 : 代表的是传入参数的型态
  58. arg4 : 代表的是传入参数的名称


  59. 系统总共定义了 6 个 _syscallN , 从 _syscall0 到 _syscall5 . 因为这是呼叫 int 0x80 的限制,各位大概发现了一件事,这个只是协助各位去呼叫 int 0x80 这个系统中断函式,不过 linux 帮我们包的很好

  60. (4) 编程程序

  61. #gcc -O2 use_syscall.c use_syscall
  62. #./use_syscall



  63. Return Value: 100

  64. 有任何问题欢迎来信讨论
复制代码

论坛徽章:
0
2 [报告]
发表于 2003-05-24 19:33 |只看该作者

在 Linux Kernel 内新增一个 System Call

欢迎大家去c\c++作客“)

论坛徽章:
0
3 [报告]
发表于 2003-05-24 19:56 |只看该作者

在 Linux Kernel 内新增一个 System Call

欢迎大家去BSD作客

论坛徽章:
0
4 [报告]
发表于 2003-05-24 20:14 |只看该作者

在 Linux Kernel 内新增一个 System Call

看不懂呀

论坛徽章:
1
荣誉会员
日期:2011-11-23 16:44:17
5 [报告]
发表于 2003-05-25 00:54 |只看该作者

在 Linux Kernel 内新增一个 System Call

好文章,
push一下。

论坛徽章:
0
6 [报告]
发表于 2003-05-25 07:26 |只看该作者

在 Linux Kernel 内新增一个 System Call

[quote]原帖由 "i2era"]欢迎大家去BSD作客[/quote 发表:

倒,来偶贴子做广告,      哈哈!
其实偶最喜欢的是freebsd   

论坛徽章:
1
荣誉版主
日期:2011-11-23 16:44:17
7 [报告]
发表于 2003-05-25 12:09 |只看该作者

在 Linux Kernel 内新增一个 System Call

当我不存在嘛!! 哼哼哼!!! :)

rdd能把这个帖子的标题改成跟着个调用相关的嘛?
我把他置成精华,便于以后查找。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP