Android 系统调用

来源:互联网 发布:大数据可视化公司 编辑:程序博客网 时间:2024/06/17 13:12

另外: http://hamer-blog.logdown.com/posts/174690-android-find-kernel-headers-the-source-needs

http://blog.csdn.net/heqiangflytosky/article/details/8941466


http://www.eoeandroid.com/thread-4668-1-1.html

http://blog.chinaunix.net/uid-9078996-id-2010323.html

http://blog.csdn.net/andyhuabing/article/details/7183369

1、Syscall stub:

每个系统调用函数都由一个小的汇编代码段实现(称为”syscall stub”),这是由tools/gensyscalls.py工具自动生成的,它从SYSCALL.TXT中取得输入参数。
SYSCALLS.TXT包含了一份要生成的系统调用块列表,和相应的系统调用数字标识符(ARM和X86不一样),以及它的签署。
如果你要修改这个文件,你可能要使用tools/checksyscalls.py工具,检查它里面是否包含官方linux核心头文件,如果有不合法的syscall ids,它会报告错误。
有时,C库函数其实就是一个包装,它内部调用相应的syscall名称,例如,exit函数由C库提供,它调用了_exit()这个syscall stub.
详细内容请参考SYSCALLS.TXT文件。

2、比如要增加消息队列的函数:
(1). 修改 SYSCALL.TXT
增加:
long ipc(unsigned int call, int first, unsigned long second, unsigned long third, void __user *ptr, long fifth) 117
(2). 执行 tools/gensyscalls.py
(3). 以下内容会由 tools/gensyscalls.py 自动产生:

bionic/libc/arch-mips/syscalls.mk
----------------------------------------------------------------------------------------------------------------------------
syscall_src += arch-mips/syscalls/ipc.S
----------------------------------------------------------------------------------------------------------------------------

bionic/libc/include/sys/linux-syscalls.h
----------------------------------------------------------------------------------------------------------------------------
#define _NR_ipc (_NR_SYSCALL_BASE + 117)
----------------------------------------------------------------------------------------------------------------------------

bionic/libc/include/sys/linux-unistd.h
----------------------------------------------------------------------------------------------------------------------------
int ipc (unsigned int call, int first, int second, int third, void* ptr, long fifth);
----------------------------------------------------------------------------------------------------------------------------

(4). 手动编写
bionic/libc/Android.mk
----------------------------------------------------------------------------------------------------------------------------
ifeq ($(TARGET_ARCH),mips)
libc_common_src_files += \
arch-mips/bionic/sys_ipc.c \

----------------------------------------------------------------------------------------------------------------------------
bionic/libc/arch-mips/bionic/sys_ipc.c
----------------------------------------------------------------------------------------------------------------------------
[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. #include <stdarg.h>  
  2. #include <sys/msg.h>  
  3.   
  4. extern int ipc (unsigned int call, int first, int second, int third, void* ptr, long fifth);  
  5. int semget (key_t  key, int  nsems, int  semflg)  
  6. {  
  7.     return ipc(SEMGET, key, nsems, semflg, (void*)NULL, (long)NULL);  
  8. }  
  9. int semop (int  semid, struct sembuf*  sops, size_t  nsops)  
  10. {  
  11.     return ipc(SEMOP, semid, (int)nsops, 0, sops, (long)NULL);  
  12. }  
  13. void* shmat (int  shmid, const void*  shmaddr, int  shmflg)  
  14. {  
  15.     int rval;  
  16.     unsigned long raddr;  
  17.   
  18.   
  19.     rval = ipc(SHMAT, shmid, shmflg, (int)&raddr, (void*)shmaddr, (long)NULL);  
  20.     if (rval < 0)  
  21.         return (char *)rval;  
  22.   
  23.   
  24.     return (char *)raddr;  
  25. }  
  26.   
  27. int shmctl (int  shmid, int  cmd, struct shmid_ds*  buf)  
  28. {  
  29.     return ipc(SHMCTL, shmid, cmd, 0, buf, (long)NULL);  
  30. }  
  31. int shmget (key_t  key, size_t  size, int  shmflg)  
  32. {  
  33.     return ipc(SHMGET, key, size, shmflg, (void*)NULL, (long)NULL);  
  34. }  
  35.   
  36. int msgctl (int  msqid, int  cmd, struct msqid_ds *buf)  
  37. {  
  38.     return ipc(MSGCTL, msqid, cmd, 0, buf, (long)NULL);  
  39. }  
  40.   
  41. int shmdt (const void*  shmaddr)  
  42. {  
  43.     return ipc(SHMDT, 0, 0, 0, (void*)shmaddr, (long)NULL);  
  44. }  
  45.   
  46. int msgget (key_t  key, int  msgflg)  
  47. {  
  48.     return ipc(MSGGET, key, msgflg, 0, NULL, (long)NULL);  
  49. }  
  50. ssize_t msgrcv (int msqid, void *msgp, size_t msgsz, long int msgtyp, int msgflg)  
  51. {  
  52.     struct ipc_kludge tmp;  
  53.   
  54.   
  55.     tmp.msgp = msgp;  
  56.     tmp.msgtyp = msgtyp;  
  57.   
  58.     return ipc(MSGRCV, msqid, msgsz, msgflg, &tmp, (long)NULL);  
  59. }  
  60. int msgsnd (int  msqid, const void* msgp, size_t  msgsz, int  msgflg)  
  61. {  
  62.     return ipc(MSGSND, msqid, msgsz, msgflg, (void*)msgp, (long)NULL);  
  63. }  



bionic/libc/arch-mips/syscalls/ipc.S
----------------------------------------------------------------------------------------------------------------------------
/* autogenerated by gensyscalls.py */
#include <sys/linux-syscalls.h>
    .text
    .globl ipc
    .align 4
    .ent ipc

ipc:
    .set noreorder
    .cpload $t9
    li $v0, __NR_ipc
    syscall
    bnez $a3, 1f
     move $a0, $v0
    j $ra
     nop
1:
    la $t9,__set_errno
    j $t9
     nop
    .set reorder
    .end ipc

----------------------------------------------------------------------------------------------------------------------------
bionic/libc/include/sys/msg.h
----------------------------------------------------------------------------------------------------------------------------

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. #ifndef _SYS_MSG_H  
  2. #define _SYS_MSG_H  
  3.   
  4. #include <features.h>  
  5.   
  6. #define __need_size_t  
  7. #include <stddef.h>  
  8.   
  9. /* Get common definition of System V style IPC.  */  
  10. #include <sys/ipc.h>  
  11.   
  12. /* Get system dependent definition of `struct msqid_ds' and more.  */  
  13. #include <msq.h>  
  14.   
  15. /* Define types required by the standard.  */  
  16. #define    __need_time_t  
  17. #include <time.h>  
  18.   
  19. #ifndef __pid_t_defined  
  20. typedef __pid_t pid_t;  
  21. # define __pid_t_defined  
  22. #endif  
  23.   
  24. #ifndef __ssize_t_defined  
  25. typedef __ssize_t ssize_t;  
  26. # define __ssize_t_defined  
  27. #endif  
  28.   
  29. #define SEMOP         1  
  30. #define SEMGET         2  
  31. #define SEMCTL         3  
  32. #define SEMTIMEDOP     4  
  33. #define MSGSND        11  
  34. #define MSGRCV        12  
  35. #define MSGGET        13  
  36. #define MSGCTL        14  
  37. #define SHMAT        21  
  38. #define SHMDT        22  
  39. #define SHMGET        23  
  40. #define SHMCTL        24  
  41. /* The following System V style IPC functions implement a message queue 
  42.    system.  The definition is found in XPG2.  */  
  43.   
  44. #ifdef __USE_GNU  
  45. /* Template for struct to be used as argument for `msgsnd' and `msgrcv'.  */  
  46. struct msgbuf  
  47.   {  
  48.     long int mtype;        /* type of received/sent message */  
  49.     char mtext[1];        /* text of the message */  
  50.   };  
  51. #endif  
  52.   
  53. struct ipc_kludge {  
  54.     struct msgbuf __user *msgp;  
  55.     long msgtyp;  
  56. };  
  57.   
  58. __BEGIN_DECLS  
  59.   
  60. /* Message queue control operation.  */  
  61. extern int msgctl (int __msqid, int __cmd, struct msqid_ds *__buf) ;  
  62.   
  63. /* Get messages queue.  */  
  64. extern int msgget (key_t __key, int __msgflg) ;  
  65.   
  66. /* Receive message from message queue. 
  67.  
  68.  
  69.    This function is a cancellation point and therefore not marked with 
  70.    __THROW.  */  
  71. extern ssize_t msgrcv (int __msqid, void *__msgp, size_t __msgsz, long int __msgtyp, int __msgflg);  
  72.   
  73. /* Send message to message queue. 
  74.  
  75.    This function is a cancellation point and therefore not marked with 
  76.    __THROW.  */  
  77. extern int msgsnd (int __msqid, __const void *__msgp, size_t __msgsz,   int __msgflg);  
  78.   
  79. __END_DECLS  
  80.   
  81. #endif /* sys/msg.h */  


----------------------------------------------------------------------------------------------------------------------------
Create bionic/libc/include/msq.h

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. #ifndef _SYS_MSG_H  
  2. #error "Never use <msq.h> directly; include <sys/msg.h> instead."   
  3. #endif  
  4.   
  5. #include <sys/types.h>  
  6.   
  7. /* Define options for message queue functions.  */  
  8. #define MSG_NOERROR     010000  /* no error if message is too big */  
  9.   
  10. /* Types used in the structure definition.  */  
  11. typedef unsigned short int msgqnum_t;  
  12. typedef unsigned short int msglen_t;  
  13.   
  14. /* Structure of record for one message inside the kernel. 
  15.    The type `struct __msg' is opaque.  */  
  16. struct msqid_ds  
  17. {  
  18.   struct ipc_perm msg_perm;     /* structure describing operation permission */  
  19.   __time_t msg_stime;           /* time of last msgsnd command */  
  20.   __time_t msg_rtime;           /* time of last msgrcv command */  
  21.   __time_t msg_ctime;           /* time of last change */  
  22.   msgqnum_t msg_qnum;           /* number of messages currently on queue */  
  23.   msglen_t msg_qbytes;          /* max number of bytes allowed on queue */  
  24.   __pid_t msg_lspid;            /* pid of last msgsnd() */  
  25.   __pid_t msg_lrpid;            /* pid of last msgrcv() */  
  26. }; 

0 0