sysdep-x86_64.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. /* -*- mode:c; c-file-style:"k&r"; c-basic-offset: 4; tab-width:4; indent-tabs-mode:nil; mode:auto-fill; fill-column:78; -*- */
  2. /* vim: set ts=4 sw=4 et tw=78 fo=cqt wm=0: */
  3. /* Copyright (C) 2014 OSCAR lab, Stony Brook University
  4. This file is part of Graphene Library OS.
  5. Graphene Library OS is free software: you can redistribute it and/or
  6. modify it under the terms of the GNU General Public License
  7. as published by the Free Software Foundation, either version 3 of the
  8. License, or (at your option) any later version.
  9. Graphene Library OS is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  15. /* This file is imported and modified from the GNU C Library */
  16. /* This file was copied directly from the Linux implementation and modified to work on FreeBSD.
  17. It may contain a lot of unnecessary code. */
  18. #ifndef _LINUX_X86_64_SYSDEP_H
  19. #define _LINUX_X86_64_SYSDEP_H 1
  20. #include <sysdeps/generic/sysdep.h>
  21. #include <libc-symbols.h>
  22. #include<sys/syscall.h>
  23. /* BSD useds SYS_* sys call names, so we can use these to map syscall names to syscall numbers from syscall.h */
  24. #define SYS_ifyBSD(syscall_name) SYS_##syscall_name
  25. #ifdef __ASSEMBLER__
  26. /* Syntactic details of assembler. */
  27. #ifdef HAVE_ELF
  28. /* ELF uses byte-counts for .align, most others use log2 of count of bytes. */
  29. #define ALIGNARG(log2) 1<<log2
  30. /* For ELF we need the `.type' directive to make shared libs work right. */
  31. #define ASM_TYPE_DIRECTIVE(name,typearg) .type name,typearg;
  32. #define ASM_SIZE_DIRECTIVE(name) .size name,.-name;
  33. /* In ELF C symbols are asm symbols. */
  34. #undef NO_UNDERSCORES
  35. #define NO_UNDERSCORES
  36. #else
  37. #define ALIGNARG(log2) log2
  38. #define ASM_TYPE_DIRECTIVE(name,type) /* Nothing is specified. */
  39. #define ASM_SIZE_DIRECTIVE(name) /* Nothing is specified. */
  40. #endif
  41. /* Define an entry point visible from C. */
  42. #define ENTRY(name) \
  43. ASM_GLOBAL_DIRECTIVE C_SYMBOL_NAME(name); \
  44. ASM_TYPE_DIRECTIVE (C_SYMBOL_NAME(name),@function) \
  45. .align ALIGNARG(4); \
  46. C_LABEL(name) \
  47. cfi_startproc; \
  48. CALL_MCOUNT
  49. #undef END
  50. #define END(name) \
  51. cfi_endproc; \
  52. ASM_SIZE_DIRECTIVE(name)
  53. /* If compiled for profiling, call `mcount' at the start of each function. */
  54. #ifdef PROF
  55. /* The mcount code relies on a normal frame pointer being on the stack
  56. to locate our caller, so push one just for its benefit. */
  57. #define CALL_MCOUNT \
  58. pushq %rbp; \
  59. cfi_adjust_cfa_offset(8); \
  60. movq %rsp, %rbp; \
  61. cfi_def_cfa_register(%rbp); \
  62. call JUMPTARGET(mcount); \
  63. popq %rbp; \
  64. cfi_def_cfa(rsp,8);
  65. #else
  66. #define CALL_MCOUNT /* Do nothing. */
  67. #endif
  68. #ifdef NO_UNDERSCORES
  69. /* Since C identifiers are not normally prefixed with an underscore
  70. on this system, the asm identifier `syscall_error' intrudes on the
  71. C name space. Make sure we use an innocuous name. */
  72. #define syscall_error __syscall_error
  73. #define mcount _mcount
  74. #endif
  75. /* Linux uses a negative return value to indicate syscall errors,
  76. unlike most Unices, which use the condition codes' carry flag.
  77. Since version 2.1 the return value of a system call might be
  78. negative even if the call succeeded. E.g., the `lseek' system call
  79. might return a large offset. Therefore we must not anymore test
  80. for < 0, but test for a real error by making sure the value in %eax
  81. is a real error number. Linus said he will make sure the no syscall
  82. returns a value in -1 .. -4095 as a valid result so we can safely
  83. test with -4095. */
  84. /* We don't want the label for the error handle to be global when we define
  85. it here. */
  86. #define SYSCALL_ERROR_LABEL syscall_error
  87. #undef PSEUDO
  88. #define PSEUDO(name, syscall_name, args) \
  89. .text; \
  90. ENTRY (name) \
  91. DO_CALL (syscall_name, args); \
  92. cmpq $-4095, %rax; \
  93. jae SYSCALL_ERROR_LABEL; \
  94. L(pseudo_end):
  95. #undef PSEUDO_END
  96. #define PSEUDO_END(name) \
  97. END (name)
  98. #undef PSEUDO_NOERRNO
  99. #define PSEUDO_NOERRNO(name, syscall_name, args) \
  100. .text; \
  101. ENTRY (name) \
  102. DO_CALL (syscall_name, args)
  103. #undef PSEUDO_END_NOERRNO
  104. #define PSEUDO_END_NOERRNO(name) \
  105. END (name)
  106. #define ret_NOERRNO ret
  107. #undef PSEUDO_ERRVAL
  108. #define PSEUDO_ERRVAL(name, syscall_name, args) \
  109. .text; \
  110. ENTRY (name) \
  111. DO_CALL (syscall_name, args); \
  112. negq %rax
  113. #undef PSEUDO_END_ERRVAL
  114. #define PSEUDO_END_ERRVAL(name) \
  115. END (name)
  116. #define ret_ERRVAL ret
  117. #undef DO_CALL
  118. #define DO_CALL(syscall_name, args) \
  119. DOARGS_##args \
  120. movl $SYS_ify (syscall_name), %eax; \
  121. syscall;
  122. #define DOARGS_0 /* nothing */
  123. #define DOARGS_1 /* nothing */
  124. #define DOARGS_2 /* nothing */
  125. #define DOARGS_3 /* nothing */
  126. #define DOARGS_4 movq %rcx, %r10;
  127. #define DOARGS_5 DOARGS_4
  128. #define DOARGS_6 DOARGS_5
  129. #else /* !__ASSEMBLER__ */
  130. /* Define a macro which expands inline into the wrapper code for a system
  131. call. */
  132. #undef INLINE_SYSCALL
  133. #define INLINE_SYSCALL(name, nr, args...) INTERNAL_SYSCALL(name, , nr, args)
  134. #undef INTERNAL_SYSCALL_DECL
  135. #define INTERNAL_SYSCALL_DECL(err) do { } while (0)
  136. #define INTERNAL_SYSCALL_NCS(name, err, nr, args...) \
  137. ({ \
  138. LOAD_ARGS_##nr (args) \
  139. LOAD_REGS_##nr \
  140. unsigned long resultvar; \
  141. asm volatile ( \
  142. "int $0x80\n\t" \
  143. : "=a" (resultvar) \
  144. : "0" (name) ASM_ARGS_##nr \
  145. : "memory", "cc"); \
  146. (long) IS_SYSCALL_ERROR(resultvar); \
  147. })
  148. #undef INTERNAL_SYSCALL
  149. #define INTERNAL_SYSCALL(name, err, nr, args...) \
  150. INTERNAL_SYSCALL_NCS(SYS_ifyBSD(name), err, nr, ##args)
  151. #undef INTERNAL_SYSCALL_ERROR
  152. #define INTERNAL_SYSCALL_ERROR(val) ((val) < 0)
  153. #undef INTERNAL_SYSCALL_ERROR_P
  154. #define INTERNAL_SYSCALL_ERROR_P(val) \
  155. ((unsigned long) (val) >= -4095L)
  156. #undef INTERNAL_SYSCALL_ERRNO
  157. #define INTERNAL_SYSCALL_ERRNO(val) (-(val))
  158. #undef INTERNAL_SYSCALL_ERRNO_P
  159. #define INTERNAL_SYSCALL_ERRNO_P(val) (-((long) val))
  160. /*
  161. If a syscall fails, it generally sets the carry flag and returns the error code in rax.
  162. To simplify matters and reuse a lot of the Linux code, we change rax to negative after checking the carry flag.
  163. */
  164. #define IS_SYSCALL_ERROR(val) \
  165. ({ \
  166. int carry; \
  167. asm volatile("mov $0, %0\n\t" \
  168. "adc $0, %0\n\t" \
  169. : "=b"(carry) \
  170. : \
  171. : "cc", "memory", "eax"); \
  172. (carry) ? -val : val; })
  173. #define LOAD_ARGS_0()
  174. #define LOAD_REGS_0
  175. #define ASM_ARGS_0
  176. #define LOAD_ARGS_1(a1) \
  177. long int __arg1 = (long) (a1); \
  178. LOAD_ARGS_0 ()
  179. #define LOAD_REGS_1 \
  180. register long int _a1 asm ("rdi") = __arg1; \
  181. LOAD_REGS_0
  182. #define ASM_ARGS_1 ASM_ARGS_0, "r" (_a1)
  183. #define LOAD_ARGS_2(a1, a2) \
  184. long int __arg2 = (long) (a2); \
  185. LOAD_ARGS_1 (a1)
  186. #define LOAD_REGS_2 \
  187. register long int _a2 asm ("rsi") = __arg2; \
  188. LOAD_REGS_1
  189. #define ASM_ARGS_2 ASM_ARGS_1, "r" (_a2)
  190. #define LOAD_ARGS_3(a1, a2, a3) \
  191. long int __arg3 = (long) (a3); \
  192. LOAD_ARGS_2 (a1, a2)
  193. #define LOAD_REGS_3 \
  194. register long int _a3 asm ("rdx") = __arg3; \
  195. LOAD_REGS_2
  196. #define ASM_ARGS_3 ASM_ARGS_2, "r" (_a3)
  197. #define LOAD_ARGS_4(a1, a2, a3, a4) \
  198. long int __arg4 = (long) (a4); \
  199. LOAD_ARGS_3 (a1, a2, a3)
  200. #define LOAD_REGS_4 \
  201. register long int _a4 asm ("rcx") = __arg4; \
  202. LOAD_REGS_3
  203. #define ASM_ARGS_4 ASM_ARGS_3, "r" (_a4)
  204. #define LOAD_ARGS_5(a1, a2, a3, a4, a5) \
  205. long int __arg5 = (long) (a5); \
  206. LOAD_ARGS_4 (a1, a2, a3, a4)
  207. #define LOAD_REGS_5 \
  208. register long int _a5 asm ("r8") = __arg5; \
  209. LOAD_REGS_4
  210. #define ASM_ARGS_5 ASM_ARGS_4, "r" (_a5)
  211. #define LOAD_ARGS_6(a1, a2, a3, a4, a5, a6) \
  212. long int __arg6 = (long) (a6); \
  213. LOAD_ARGS_5 (a1, a2, a3, a4, a5)
  214. #define LOAD_REGS_6 \
  215. register long int _a6 asm ("r9") = __arg6; \
  216. LOAD_REGS_5
  217. #define ASM_ARGS_6 ASM_ARGS_5, "r" (_a6)
  218. #endif /* __ASSEMBLER__ */
  219. #endif /* linux/x86_64/sysdep.h */