Chinaunix首页 | 论坛 | 博客
  • 博客访问: 900031
  • 博文数量: 119
  • 博客积分: 2493
  • 博客等级: 大尉
  • 技术积分: 2363
  • 用 户 组: 普通用户
  • 注册时间: 2012-06-03 14:00
文章分类

全部博文(119)

文章存档

2013年(19)

2012年(100)

分类: LINUX

2012-06-20 21:35:04

1,什么是封装例程?
        系统调用是内核实现的,内核向用户态提供服务,内核向用户态提供系统调用的接口是一个软中断。
也就是int 0x80.如果用户直接调用系统调用会很麻烦。下面就举个直接调用系统调用的例子。用汇编实现的。
  1. # hello.s ----> intel汇编的注释用的; 而ATT用的#
  2. # display a string "Hello, world."

  3. .section .rodata
  4. msg:
  5. .ascii "Hello, world.\n"

  6. .section .text
  7. .globl _start
  8. _start:

  9. movl $2, %eax #调用fork系统调用
  10. int $0x80


  11. movl $4, %eax # system call 系统调用号(sys_write)
  12. movl $1, %ebx # file descriptor 参数一:文件描述符(stdout)
  13. movl $msg, %ecx # string address 参数二:要显示的字符串
  14. movl $14, %edx # string length 参数三:字符串长度
  15. int $0x80 # 调用内核功能

  16. movl $1, %eax # 系统调用号(sys_exit)
  17. movl $0, %ebx # 参数一:退出代码
  18. int $0x80 # 调用内核功能
这就需要对系统调用进行封装,便于用户调用,比如说fork(),getpid(),这些函数都是libc库中的函数
都对系统调用进行了封装。而且都是遵循POSIX标准的。
---------------------------------------------------------------------------------------------
2,什么是POSIX?
      POSIX是一套操作系统标准,它是随着UNIX标准化而诞生的。linux是遵循POSIX标准的操作系统,如果
linux遵循POSIX标准的话,UNIX上的应用程序就可以顺利移植到linux上来了。形象点说也就是linux和UNIX
很多函数名字及其参数都定义的是一样的。
---------------------------------------------------------------------------------------------
3,旧方式封装(淘汰了),__syscall0()  ~ __syscall6()
     这几个宏实际上是利用了系统调用参数的个数不能超个6个来进行分类的。为了内核的安全考虑。从内核2.6.18开始该宏就从头文件中删除了,其实完全也可以自己加入到 指定的头文件中去。该宏被syscall()取代了,syscall()是glibc中的函数。而上面的_syscall()宏是不依赖于库的。
  1. #define __syscall_return(type, res) \
  2. do { \
  3. if ((unsigned long)(res) >= (unsigned long)(-MAX_ERRNO)) { \
  4. res = -1; \
  5. } \
  6. return (type) (res); \
  7. } while (0)

  8. #define _syscall0(type,name) \
  9. type name(void) \
  10. { \
  11. long __res; \
  12. __asm__ volatile ("int $0x80" \
  13. : "=a" (__res) \
  14. : "0" (__NR_##name)); \
  15. __syscall_return(type,__res); \
  16. }

  17. #define _syscall1(type,name,type1,arg1) \
  18. type name(type1 arg1) \
  19. { \
  20. long __res; \
  21. __asm__ volatile ("push %%ebx ; movl %2,%%ebx ; int $0x80 ; pop %%ebx" \
  22. : "=a" (__res) \
  23. : "0" (__NR_##name),"ri" ((long)(arg1)) : "memory"); \
  24. __syscall_return(type,__res); \
  25. }

  26. #define _syscall2(type,name,type1,arg1,type2,arg2) \
  27. type name(type1 arg1,type2 arg2) \
  28. { \
  29. long __res; \
  30. __asm__ volatile ("push %%ebx ; movl %2,%%ebx ; int $0x80 ; pop %%ebx" \
  31. : "=a" (__res) \
  32. : "0" (__NR_##name),"ri" ((long)(arg1)),"c" ((long)(arg2)) \
  33. : "memory"); \
  34. __syscall_return(type,__res); \
  35. }

  36. #define _syscall3(type,name,type1,arg1,type2,arg2,type3,arg3) \
  37. type name(type1 arg1,type2 arg2,type3 arg3) \
  38. { \
  39. long __res; \
  40. __asm__ volatile ("push %%ebx ; movl %2,%%ebx ; int $0x80 ; pop %%ebx" \
  41. : "=a" (__res) \
  42. : "0" (__NR_##name),"ri" ((long)(arg1)),"c" ((long)(arg2)), \
  43. "d" ((long)(arg3)) : "memory"); \
  44. __syscall_return(type,__res); \
  45. }

  46. #define _syscall4(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4) \
  47. type name (type1 arg1, type2 arg2, type3 arg3, type4 arg4) \
  48. { \
  49. long __res; \
  50. __asm__ volatile ("push %%ebx ; movl %2,%%ebx ; int $0x80 ; pop %%ebx" \
  51. : "=a" (__res) \
  52. : "0" (__NR_##name),"ri" ((long)(arg1)),"c" ((long)(arg2)), \
  53. "d" ((long)(arg3)),"S" ((long)(arg4)) : "memory"); \
  54. __syscall_return(type,__res); \
  55. }

  56. #define _syscall5(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
  57. type5,arg5) \
  58. type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4,type5 arg5) \
  59. { \
  60. long __res; \
  61. __asm__ volatile ("push %%ebx ; movl %2,%%ebx ; movl %1,%%eax ; " \
  62. "int $0x80 ; pop %%ebx" \
  63. : "=a" (__res) \
  64. : "i" (__NR_##name),"ri" ((long)(arg1)),"c" ((long)(arg2)), \
  65. "d" ((long)(arg3)),"S" ((long)(arg4)),"D" ((long)(arg5)) \
  66. : "memory"); \
  67. __syscall_return(type,__res); \
  68. }

  69. #define _syscall6(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
  70. type5,arg5,type6,arg6) \
  71. type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4,type5 arg5,type6 arg6) \
  72. { \
  73. long __res; \
  74. struct { long __a1; long __a6; } __s = { (long)arg1, (long)arg6 }; \
  75. __asm__ volatile ("push %%ebp ; push %%ebx ; movl 4(%2),%%ebp ; " \
  76. "movl 0(%2),%%ebx ; movl %1,%%eax ; int $0x80 ; " \
  77. "pop %%ebx ; pop %%ebp" \
  78. : "=a" (__res) \
  79. : "i" (__NR_##name),"0" ((long)(&__s)),"c" ((long)(arg2)), \
  80. "d" ((long)(arg3)),"S" ((long)(arg4)),"D" ((long)(arg5)) \
  81. : "memory"); \
  82. __syscall_return(type,__res); \
  83. }
---------------------------------------------------------------------------------------------
4,新方式封装。(现在的封装方式)
现在对系统调用的封装都是用的syscall函数,该函数是不定参数,很好用,可以调用所有的系统调用。

  1.   1 #include <linux/unistd.h>
  2.   2 #include <syscall.h>
  3.   3 #include <sys/types.h>
  4.   4 #include <stdio.h>
  5.   6
  6.   7
  7.   8 int main(void)
  8.   9 {
  9.  10 long pid1;
  10.  11 pid1 = syscall(SYS_getpid); //getpid
  11.  12 printf("pid = %ld\n",pid1);
  12.  13 return 0;
  13.  14 }
---------------------------------------------------------------------------------------------
5,问题:我自己添加了一个系统调用,想在用户态下测试下,代码如下:

  1.   1 #include <linux/unistd.h>
  2.   2 #include <syscall.h>
  3.   3 #include <sys/types.h>
  4.   4 #include <stdio.h>
  5.   5
  6.   6
  7.   7 int main(void)
  8.   8 {
  9.   9 long n = 0;
  10.  10 n = syscall(SYS_mysyscall,190);
  11.  11 return 0;
  12.  12 }
该程序会提示“SYS_mysyscall”没有定义。
解决办法:
1,/usr/include/bits/syscall.h文件中添加:#define SYS_mysyscall __NR_mysyscall
2,/usr/include/i386-linux-gnu/asm/unistd_32文件末尾添加:
      #define __NR_mysyscall          345
      这样,你添加的系统调用和系统本身自带的系统调用的用户态使用上就一模一样了。
---------------------------------------------------------------------------------------------





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