syscall.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /* -*-comment-start: "//";comment-end:""-*-
  2. * GNU Mes --- Maxwell Equations of Software
  3. * Copyright © 2016,2017,2018 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. // *INDENT-OFF*
  23. long
  24. __sys_call (long sys_call)
  25. {
  26. long r;
  27. asm (
  28. "mov %1,%%eax\n\t"
  29. "int $0x80\n\t"
  30. "mov %%eax,%0\n\t"
  31. : "=r" (r)
  32. : "rm" (sys_call)
  33. : "eax"
  34. );
  35. return r;
  36. }
  37. long
  38. __sys_call1 (long sys_call, long one)
  39. {
  40. long r;
  41. asm (
  42. "mov %1,%%eax\n\t"
  43. "mov %2,%%ebx\n\t"
  44. "int $0x80\n\t"
  45. "mov %%eax,%0\n\t"
  46. : "=r" (r)
  47. : "rm" (sys_call), "rm" (one)
  48. : "eax", "ebx"
  49. );
  50. return r;
  51. }
  52. long
  53. __sys_call2 (long sys_call, long one, long two)
  54. {
  55. long r;
  56. asm (
  57. "mov %1,%%eax\n\t"
  58. "mov %2,%%ebx\n\t"
  59. "mov %3,%%ecx\n\t"
  60. "int $0x80\n\t"
  61. "mov %%eax,%0\n\t"
  62. : "=r" (r)
  63. : "rm" (sys_call), "rm" (one), "rm" (two)
  64. : "eax", "ebx", "ecx"
  65. );
  66. return r;
  67. }
  68. long
  69. __sys_call3 (long sys_call, long one, long two, long three)
  70. {
  71. long r;
  72. asm (
  73. "mov %2,%%ebx\n\t"
  74. "mov %3,%%ecx\n\t"
  75. "mov %4,%%edx\n\t"
  76. "mov %1,%%eax\n\t"
  77. "int $0x80\n\t"
  78. "mov %%eax,%0\n\t"
  79. : "=r" (r)
  80. : "rm" (sys_call), "rm" (one), "rm" (two), "rm" (three)
  81. : "eax", "ebx", "ecx", "edx"
  82. );
  83. return r;
  84. }
  85. long
  86. __sys_call4 (long sys_call, long one, long two, long three, long four)
  87. {
  88. long r;
  89. asm (
  90. "mov %2,%%ebx\n\t"
  91. "mov %3,%%ecx\n\t"
  92. "mov %4,%%edx\n\t"
  93. "mov %5,%%esi\n\t"
  94. "mov %1,%%eax\n\t"
  95. "int $0x80\n\t"
  96. "mov %%eax,%0\n\t"
  97. : "=r" (r)
  98. : "rm" (sys_call), "rm" (one), "rm" (two), "rm" (three), "rm" (four)
  99. : "eax", "ebx", "ecx", "edx", "esi"
  100. );
  101. return r;
  102. }
  103. // *INDENT-ON*
  104. long
  105. _sys_call (long sys_call)
  106. {
  107. long r = __sys_call (sys_call);
  108. if (r < 0)
  109. {
  110. errno = -r;
  111. r = -1;
  112. }
  113. else
  114. errno = 0;
  115. return r;
  116. }
  117. long
  118. _sys_call1 (long sys_call, long one)
  119. {
  120. long r = __sys_call1 (sys_call, one);
  121. if (r < 0)
  122. {
  123. errno = -r;
  124. r = -1;
  125. }
  126. else
  127. errno = 0;
  128. return r;
  129. }
  130. long
  131. _sys_call2 (long sys_call, long one, long two)
  132. {
  133. long r = __sys_call2 (sys_call, one, two);
  134. if (r < 0)
  135. {
  136. errno = -r;
  137. r = -1;
  138. }
  139. else
  140. errno = 0;
  141. return r;
  142. }
  143. long
  144. _sys_call3 (long sys_call, long one, long two, long three)
  145. {
  146. long r = __sys_call3 (sys_call, one, two, three);
  147. if (r < 0)
  148. {
  149. errno = -r;
  150. r = -1;
  151. }
  152. else
  153. errno = 0;
  154. return r;
  155. }
  156. long
  157. _sys_call4 (long sys_call, long one, long two, long three, long four)
  158. {
  159. long r = __sys_call4 (sys_call, one, two, three, four);
  160. if (r < 0)
  161. {
  162. errno = -r;
  163. r = -1;
  164. }
  165. else
  166. errno = 0;
  167. return r;
  168. }