syscalls.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. #ifndef _BITS_SYSCALLS_H
  2. #define _BITS_SYSCALLS_H
  3. #ifndef _SYSCALL_H
  4. # error "Never use <bits/syscalls.h> directly; include <sys/syscall.h> instead."
  5. #endif
  6. /* This includes the `__NR_<name>' syscall numbers taken from the Linux kernel
  7. * header files. It also defines the traditional `SYS_<name>' macros for older
  8. * programs. */
  9. #include <bits/sysnum.h>
  10. #ifndef __set_errno
  11. # define __set_errno(val) (*__errno_location ()) = (val)
  12. #endif
  13. #ifndef SYS_ify
  14. # define SYS_ify(syscall_name) (__NR_##syscall_name)
  15. #endif
  16. #ifndef __ASSEMBLER__
  17. #define __SYSCALL_CLOBBERS "$1", "$3", "$8", "$9", "$10", "$11", "$12", "$13", \
  18. "$14", "$15", "$24", "$25", "memory"
  19. #define _syscall0(type,name) \
  20. type name(void) \
  21. { \
  22. long err; \
  23. long sys_result; \
  24. { \
  25. register unsigned long __v0 asm("$2"); \
  26. register unsigned long __a3 asm("$7"); \
  27. __asm__ volatile ( \
  28. ".set noreorder\n\t" \
  29. "li $2, %2 # " #name "\n\t" \
  30. "syscall\n\t" \
  31. ".set reorder" \
  32. : "=r" (__v0), "=r" (__a3) \
  33. : "i" (SYS_ify(name)) \
  34. : __SYSCALL_CLOBBERS); \
  35. err = __a3; \
  36. sys_result = __v0; \
  37. } \
  38. if (err == 0) \
  39. return (type) sys_result; \
  40. __set_errno(sys_result); \
  41. return (type)-1; \
  42. }
  43. #define _syscall1(type,name,atype,a) \
  44. type name(atype a) \
  45. { \
  46. long err; \
  47. long sys_result; \
  48. { \
  49. register unsigned long __v0 asm("$2"); \
  50. register unsigned long __a0 asm("$4") = (unsigned long) a; \
  51. register unsigned long __a3 asm("$7"); \
  52. __asm__ volatile ( \
  53. ".set noreorder\n\t" \
  54. "li $2, %3\t\t\t# " #name "\n\t" \
  55. "syscall\n\t" \
  56. ".set reorder" \
  57. : "=r" (__v0), "=r" (__a3) \
  58. : "r" (__a0), "i" (SYS_ify(name)) \
  59. : __SYSCALL_CLOBBERS); \
  60. err = __a3; \
  61. sys_result = __v0; \
  62. } \
  63. if (err == 0) \
  64. return (type) sys_result; \
  65. __set_errno(sys_result); \
  66. return (type)-1; \
  67. }
  68. #define _syscall2(type,name,atype,a,btype,b) \
  69. type name(atype a,btype b) \
  70. { \
  71. long err; \
  72. long sys_result; \
  73. { \
  74. register unsigned long __v0 asm("$2"); \
  75. register unsigned long __a0 asm("$4") = (unsigned long) a; \
  76. register unsigned long __a1 asm("$5") = (unsigned long) b; \
  77. register unsigned long __a3 asm("$7"); \
  78. __asm__ volatile ( \
  79. ".set noreorder\n\t" \
  80. "li $2, %4\t\t\t# " #name "\n\t" \
  81. "syscall\n\t" \
  82. ".set reorder" \
  83. : "=r" (__v0), "=r" (__a3) \
  84. : "r" (__a0), "r" (__a1), "i" (SYS_ify(name)) \
  85. : __SYSCALL_CLOBBERS); \
  86. err = __a3; \
  87. sys_result = __v0; \
  88. } \
  89. if (err == 0) \
  90. return (type) sys_result; \
  91. __set_errno(sys_result); \
  92. return (type)-1; \
  93. }
  94. #define _syscall3(type,name,atype,a,btype,b,ctype,c) \
  95. type name (atype a, btype b, ctype c) \
  96. { \
  97. long err; \
  98. long sys_result; \
  99. { \
  100. register unsigned long __v0 asm("$2"); \
  101. register unsigned long __a0 asm("$4") = (unsigned long) a; \
  102. register unsigned long __a1 asm("$5") = (unsigned long) b; \
  103. register unsigned long __a2 asm("$6") = (unsigned long) c; \
  104. register unsigned long __a3 asm("$7"); \
  105. __asm__ volatile ( \
  106. ".set noreorder\n\t" \
  107. "li $2, %5\t\t\t# " #name "\n\t" \
  108. "syscall\n\t" \
  109. ".set reorder" \
  110. : "=r" (__v0), "=r" (__a3) \
  111. : "r" (__a0), "r" (__a1), "r" (__a2), "i" (SYS_ify(name)) \
  112. : __SYSCALL_CLOBBERS); \
  113. err = __a3; \
  114. sys_result = __v0; \
  115. } \
  116. if (err == 0) \
  117. return (type) sys_result; \
  118. __set_errno(sys_result); \
  119. return (type)-1; \
  120. }
  121. #define _syscall4(type,name,atype,a,btype,b,ctype,c,dtype,d) \
  122. type name (atype a, btype b, ctype c, dtype d) \
  123. { \
  124. long err; \
  125. long sys_result; \
  126. { \
  127. register unsigned long __v0 asm("$2"); \
  128. register unsigned long __a0 asm("$4") = (unsigned long) a; \
  129. register unsigned long __a1 asm("$5") = (unsigned long) b; \
  130. register unsigned long __a2 asm("$6") = (unsigned long) c; \
  131. register unsigned long __a3 asm("$7") = (unsigned long) d; \
  132. __asm__ volatile ( \
  133. ".set noreorder\n\t" \
  134. "li $2, %5\t\t\t# " #name "\n\t" \
  135. "syscall\n\t" \
  136. ".set reorder" \
  137. : "=r" (__v0), "+r" (__a3) \
  138. : "r" (__a0), "r" (__a1), "r" (__a2), "i" (SYS_ify(name)) \
  139. : __SYSCALL_CLOBBERS); \
  140. err = __a3; \
  141. sys_result = __v0; \
  142. } \
  143. if (err == 0) \
  144. return (type) sys_result; \
  145. __set_errno(sys_result); \
  146. return (type)-1; \
  147. }
  148. #define _syscall5(type,name,atype,a,btype,b,ctype,c,dtype,d,etype,e) \
  149. type name (atype a,btype b,ctype c,dtype d,etype e) \
  150. { \
  151. long err; \
  152. long sys_result; \
  153. const unsigned long *constE = (void*)(unsigned long) e; \
  154. { \
  155. register unsigned long __v0 asm("$2"); \
  156. register unsigned long __a0 asm("$4") = (unsigned long) a; \
  157. register unsigned long __a1 asm("$5") = (unsigned long) b; \
  158. register unsigned long __a2 asm("$6") = (unsigned long) c; \
  159. register unsigned long __a3 asm("$7") = (unsigned long) d; \
  160. __asm__ volatile ( \
  161. ".set noreorder\n\t" \
  162. "lw $2, %6\n\t" \
  163. "subu $29, 32\n\t" \
  164. "sw $2, 16($29)\n\t" \
  165. "li $2, %5\t\t\t# " #name "\n\t" \
  166. "syscall\n\t" \
  167. "addiu $29, 32\n\t" \
  168. ".set reorder" \
  169. : "=r" (__v0), "+r" (__a3) \
  170. : "r" (__a0), "r" (__a1), "r" (__a2), "i" (SYS_ify(name)), \
  171. "m" (constE) \
  172. : __SYSCALL_CLOBBERS); \
  173. err = __a3; \
  174. sys_result = __v0; \
  175. } \
  176. if (err == 0) \
  177. return (type) sys_result; \
  178. __set_errno(sys_result); \
  179. return (type)-1; \
  180. }
  181. #define _syscall6(type,name,atype,a,btype,b,ctype,c,dtype,d,etype,e,ftype,f) \
  182. type name (atype a,btype b,ctype c,dtype d,etype e,ftype f) \
  183. { \
  184. long err; \
  185. long sys_result; \
  186. const unsigned long *constE = (void*)(unsigned long) e; \
  187. const unsigned long *constF = (void*)(unsigned long) f; \
  188. { \
  189. register unsigned long __v0 asm("$2"); \
  190. register unsigned long __a0 asm("$4") = (unsigned long) a; \
  191. register unsigned long __a1 asm("$5") = (unsigned long) b; \
  192. register unsigned long __a2 asm("$6") = (unsigned long) c; \
  193. register unsigned long __a3 asm("$7") = (unsigned long) d; \
  194. __asm__ volatile ( \
  195. ".set noreorder\n\t" \
  196. "lw $2, %6\n\t" \
  197. "lw $8, %7\n\t" \
  198. "subu $29, 32\n\t" \
  199. "sw $2, 16($29)\n\t" \
  200. "sw $8, 20($29)\n\t" \
  201. "li $2, %5\t\t\t# " #name "\n\t" \
  202. "syscall\n\t" \
  203. "addiu $29, 32\n\t" \
  204. ".set reorder" \
  205. : "=r" (__v0), "+r" (__a3) \
  206. : "r" (__a0), "r" (__a1), "r" (__a2), "i" (SYS_ify(name)), \
  207. "m" (constE), "m" (constF) \
  208. : __SYSCALL_CLOBBERS); \
  209. err = __a3; \
  210. sys_result = __v0; \
  211. } \
  212. if (err == 0) \
  213. return (type) sys_result; \
  214. __set_errno(sys_result); \
  215. return (type)-1; \
  216. }
  217. #define _syscall7(type,name,atype,a,btype,b,ctype,c,dtype,d,etype,e,ftype,f,gtype,g) \
  218. type name (atype a,btype b,ctype c,dtype d,etype e,ftype f,gtype g) \
  219. { \
  220. long err; \
  221. long sys_result; \
  222. const unsigned long *constE = (void*)(unsigned long) e; \
  223. const unsigned long *constF = (void*)(unsigned long) f; \
  224. const unsigned long *constG = (void*)(unsigned long) g; \
  225. { \
  226. register unsigned long __v0 asm("$2"); \
  227. register unsigned long __a0 asm("$4") = (unsigned long) a; \
  228. register unsigned long __a1 asm("$5") = (unsigned long) b; \
  229. register unsigned long __a2 asm("$6") = (unsigned long) c; \
  230. register unsigned long __a3 asm("$7") = (unsigned long) d; \
  231. __asm__ volatile ( \
  232. ".set noreorder\n\t" \
  233. "lw $2, %6\n\t" \
  234. "lw $8, %7\n\t" \
  235. "lw $9, %8\n\t" \
  236. "subu $29, 32\n\t" \
  237. "sw $2, 16($29)\n\t" \
  238. "sw $8, 20($29)\n\t" \
  239. "sw $9, 24($29)\n\t" \
  240. "li $2, %5\t\t\t# " #name "\n\t" \
  241. "syscall\n\t" \
  242. "addiu $29, 32\n\t" \
  243. ".set reorder" \
  244. : "=r" (__v0), "+r" (__a3) \
  245. : "r" (__a0), "r" (__a1), "r" (__a2), "i" (SYS_ify(name)), \
  246. "m" (constE), "m" (constF), "m" (constG) \
  247. : __SYSCALL_CLOBBERS); \
  248. err = __a3; \
  249. sys_result = __v0; \
  250. } \
  251. if (err == 0) \
  252. return (type) sys_result; \
  253. __set_errno(sys_result); \
  254. return (type)-1; \
  255. }
  256. #endif /* __ASSEMBLER__ */
  257. #endif /* _BITS_SYSCALLS_H */