syscall.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. /* -*-comment-start: "//";comment-end:""-*-
  2. * GNU Mes --- Maxwell Equations of Software
  3. * Copyright © 2016,2017,2018,2019,2020 Jan (janneke) Nieuwenhuizen <janneke@gnu.org>
  4. *
  5. * This file is part of GNU Mes.
  6. *
  7. * GNU Mes is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 3 of the License, or (at
  10. * your option) any later version.
  11. *
  12. * GNU Mes is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with GNU Mes. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. #include <errno.h>
  21. #include <linux/x86/syscall.h>
  22. #if !__TINYC__
  23. // *INDENT-OFF*
  24. long
  25. __sys_call (long sys_call)
  26. {
  27. long r;
  28. asm (
  29. "mov r7, %1\n\t"
  30. "swi $0\n\t"
  31. "mov %0, r0\n\t"
  32. : "=r" (r)
  33. : "r" (sys_call)
  34. : "r0", "r7"
  35. );
  36. return r;
  37. }
  38. #else //__TINYC__
  39. long
  40. __sys_call (long sys_call)
  41. {
  42. long r;
  43. __asm__ (".int 0xe1a07000\n"); //mov r7, r0
  44. // __asm__ (".int 0xe1a00001\n"); //mov r0, r1
  45. // __asm__ (".int 0xe1a01002\n"); //mov r1, r2
  46. // __asm__ (".int 0xe1a02003\n"); //mov r2, r3
  47. // __asm__ (".int 0xe1a03004\n"); //mov r3, r4
  48. __asm__ (".int 0xef000000\n"); //svc 0x00000000
  49. __asm__ (".int 0xe50b0004\n"); //str r0, [fp, #-4]
  50. return r;
  51. }
  52. #endif //__TINYC__
  53. #if !__TINYC__
  54. long
  55. __sys_call1 (long sys_call, long one)
  56. {
  57. long r;
  58. asm (
  59. "mov r7, %1\n\t"
  60. "mov r0, %2\n\t"
  61. "swi $0\n\t"
  62. "mov %0, r0\n\t"
  63. : "=r" (r)
  64. : "r" (sys_call), "r" (one)
  65. : "r0", "r7"
  66. );
  67. return r;
  68. }
  69. #else //__TINYC__
  70. long
  71. __sys_call1 (long sys_call, long one)
  72. {
  73. long r;
  74. __asm__ (".int 0xe1a07000\n"); //mov r7, r0
  75. __asm__ (".int 0xe1a00001\n"); //mov r0, r1
  76. // __asm__ (".int 0xe1a01002\n"); //mov r1, r2
  77. // __asm__ (".int 0xe1a02003\n"); //mov r2, r3
  78. // __asm__ (".int 0xe1a03004\n"); //mov r3, r4
  79. __asm__ (".int 0xef000000\n"); //svc 0x00000000
  80. __asm__ (".int 0xe50b0004\n"); //str r0, [fp, #-4]
  81. return r;
  82. }
  83. #endif //__TINYC__
  84. #if !__TINYC__
  85. long
  86. __sys_call2 (long sys_call, long one, long two)
  87. {
  88. long r;
  89. asm (
  90. "mov r7, %1\n\t"
  91. "mov r0, %2\n\t"
  92. "mov r1, %3\n\t"
  93. "swi $0\n\t"
  94. "mov %0, r0\n\t"
  95. : "=r" (r)
  96. : "r" (sys_call), "r" (one), "r" (two)
  97. : "r0", "r1", "r7"
  98. );
  99. return r;
  100. }
  101. #else //__TINYC__
  102. long
  103. __sys_call2 (long sys_call, long one, long two)
  104. {
  105. long r;
  106. __asm__ (".int 0xe1a07000\n"); //mov r7, r0
  107. __asm__ (".int 0xe1a00001\n"); //mov r0, r1
  108. __asm__ (".int 0xe1a01002\n"); //mov r1, r2
  109. // __asm__ (".int 0xe1a02003\n"); //mov r2, r3
  110. // __asm__ (".int 0xe1a03004\n"); //mov r3, r4
  111. __asm__ (".int 0xef000000\n"); //svc 0x00000000
  112. __asm__ (".int 0xe50b0004\n"); //str r0, [fp, #-4]
  113. return r;
  114. }
  115. #endif //__TINYC__
  116. #if !__TINYC__
  117. long
  118. __sys_call3 (long sys_call, long one, long two, long three)
  119. {
  120. long r;
  121. asm (
  122. "mov r7, %1\n\t"
  123. "mov r0, %2\n\t"
  124. "mov r1, %3\n\t"
  125. "mov r2, %4\n\t"
  126. "swi $0\n\t"
  127. "mov %0, r0\n\t"
  128. : "=r" (r)
  129. : "r" (sys_call), "r" (one), "r" (two), "r" (three)
  130. : "r0", "r1", "r2", "r7"
  131. );
  132. return r;
  133. }
  134. #else //__TINYC__
  135. long
  136. __sys_call3 (long sys_call, long one, long two, long three)
  137. {
  138. long r;
  139. __asm__ (".int 0xe1a07000\n"); //mov r7, r0
  140. __asm__ (".int 0xe1a00001\n"); //mov r0, r1
  141. __asm__ (".int 0xe1a01002\n"); //mov r1, r2
  142. __asm__ (".int 0xe1a02003\n"); //mov r2, r3
  143. // __asm__ (".int 0xe1a03004\n"); //mov r3, r4
  144. __asm__ (".int 0xef000000\n"); //svc 0x00000000
  145. __asm__ (".int 0xe50b0004\n"); //str r0, [fp, #-4]
  146. return r;
  147. }
  148. #endif //__TINYC__
  149. #if !__TINYC__
  150. long
  151. __sys_call4 (long sys_call, long one, long two, long three, long four)
  152. {
  153. long r;
  154. asm (
  155. "mov r7, %1\n\t"
  156. "mov r0, %2\n\t"
  157. "mov r1, %3\n\t"
  158. "mov r2, %4\n\t"
  159. "mov r3, %5\n\t"
  160. "swi $0\n\t"
  161. "mov %0, r0\n\t"
  162. : "=r" (r)
  163. : "r" (sys_call), "r" (one), "r" (two), "r" (three), "r" (four)
  164. : "r0", "r1", "r2", "r3", "r7"
  165. );
  166. return r;
  167. }
  168. #else //__TINYC__
  169. long
  170. __sys_call4 (long sys_call, long one, long two, long three, long four)
  171. {
  172. long r;
  173. __asm__ (".int 0xe1a07000\n"); //mov r7, r0
  174. __asm__ (".int 0xe1a00001\n"); //mov r0, r1
  175. __asm__ (".int 0xe1a01002\n"); //mov r1, r2
  176. __asm__ (".int 0xe1a02003\n"); //mov r2, r3
  177. __asm__ (".int 0xe1a03004\n"); //mov r3, r4
  178. __asm__ (".int 0xef000000\n"); //svc 0x00000000
  179. __asm__ (".int 0xe50b0004\n"); //str r0, [fp, #-4]
  180. return r;
  181. }
  182. #endif //__TINYC__
  183. #if 0
  184. long
  185. __sys_call6 (long sys_call, long one, long two, long three, long four, long five, long six)
  186. {
  187. long r;
  188. asm (
  189. "mov r7, %1\n\t"
  190. "mov r0, %2\n\t"
  191. "mov r1, %3\n\t"
  192. "mov r2, %4\n\t"
  193. "mov r3, %5\n\t"
  194. "mov r4, %6\n\t"
  195. "mov r5, %7\n\t"
  196. "swi $0\n\t"
  197. "mov %0, r0\n\t"
  198. : "=r" (r)
  199. : "r" (sys_call), "r" (one), "r" (two), "r" (three), "r" (four), "r" (five), "r" (six)
  200. : "r0", "r1", "r2", "r3", "r4", "r5" //, "r7" FIXME
  201. );
  202. return r;
  203. }
  204. #endif
  205. // *INDENT-ON*
  206. long
  207. _sys_call (long sys_call)
  208. {
  209. long r = __sys_call (sys_call);
  210. if (r < 0)
  211. {
  212. errno = -r;
  213. r = -1;
  214. }
  215. else
  216. errno = 0;
  217. return r;
  218. }
  219. long
  220. _sys_call1 (long sys_call, long one)
  221. {
  222. long r = __sys_call1 (sys_call, one);
  223. if (r < 0)
  224. {
  225. errno = -r;
  226. r = -1;
  227. }
  228. else
  229. errno = 0;
  230. return r;
  231. }
  232. long
  233. _sys_call2 (long sys_call, long one, long two)
  234. {
  235. long r = __sys_call2 (sys_call, one, two);
  236. if (r < 0)
  237. {
  238. errno = -r;
  239. r = -1;
  240. }
  241. else
  242. errno = 0;
  243. return r;
  244. }
  245. long
  246. _sys_call3 (long sys_call, long one, long two, long three)
  247. {
  248. long r = __sys_call3 (sys_call, one, two, three);
  249. if (r < 0)
  250. {
  251. errno = -r;
  252. r = -1;
  253. }
  254. else
  255. errno = 0;
  256. return r;
  257. }
  258. long
  259. _sys_call4 (long sys_call, long one, long two, long three, long four)
  260. {
  261. long r = __sys_call4 (sys_call, one, two, three, four);
  262. if (r < 0)
  263. {
  264. errno = -r;
  265. r = -1;
  266. }
  267. else
  268. errno = 0;
  269. return r;
  270. }
  271. #if 0
  272. long
  273. _sys_call6 (long sys_call, long one, long two, long three, long four, long five, long six)
  274. {
  275. long r = __sys_call6 (sys_call, one, two, three, four, five, six);
  276. if (r < 0)
  277. {
  278. errno = -r;
  279. r = -1;
  280. }
  281. else
  282. errno = 0;
  283. return r;
  284. }
  285. #endif