sysdep-x86_64.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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. #ifndef _LINUX_X86_64_SYSDEP_H
  17. #define _LINUX_X86_64_SYSDEP_H 1
  18. #include <sysdeps/generic/sysdep.h>
  19. #include <libc-symbols.h>
  20. /* For Linux we can use the system call table in the header file
  21. /usr/include/asm/unistd.h
  22. of the kernel. But these symbols do not follow the SYS_* syntax
  23. so we have to redefine the `SYS_ify' macro here. */
  24. #undef SYS_ify
  25. #define SYS_ify(syscall_name) __NR_##syscall_name
  26. /* This is a kludge to make syscalls.list find these under the names
  27. pread and pwrite, since some kernel headers define those names
  28. and some define the *64 names for the same system calls. */
  29. #if !defined __NR_pread && defined __NR_pread64
  30. # define __NR_pread __NR_pread64
  31. #endif
  32. #if !defined __NR_pwrite && defined __NR_pwrite64
  33. # define __NR_pwrite __NR_pwrite64
  34. #endif
  35. /* This is to help the old kernel headers where __NR_semtimedop is not
  36. available. */
  37. #ifndef __NR_semtimedop
  38. # define __NR_semtimedop 220
  39. #endif
  40. #ifdef __ASSEMBLER__
  41. /* Syntactic details of assembler. */
  42. #ifdef HAVE_ELF
  43. /* ELF uses byte-counts for .align, most others use log2 of count of bytes. */
  44. #define ALIGNARG(log2) 1<<log2
  45. /* For ELF we need the `.type' directive to make shared libs work right. */
  46. #define ASM_TYPE_DIRECTIVE(name,typearg) .type name,typearg;
  47. #define ASM_SIZE_DIRECTIVE(name) .size name,.-name;
  48. /* In ELF C symbols are asm symbols. */
  49. #undef NO_UNDERSCORES
  50. #define NO_UNDERSCORES
  51. #else
  52. #define ALIGNARG(log2) log2
  53. #define ASM_TYPE_DIRECTIVE(name,type) /* Nothing is specified. */
  54. #define ASM_SIZE_DIRECTIVE(name) /* Nothing is specified. */
  55. #endif
  56. /* Define an entry point visible from C. */
  57. #define ENTRY(name) \
  58. ASM_GLOBAL_DIRECTIVE C_SYMBOL_NAME(name); \
  59. ASM_TYPE_DIRECTIVE (C_SYMBOL_NAME(name),@function) \
  60. .align ALIGNARG(4); \
  61. C_LABEL(name) \
  62. cfi_startproc; \
  63. CALL_MCOUNT
  64. #undef END
  65. #define END(name) \
  66. cfi_endproc; \
  67. ASM_SIZE_DIRECTIVE(name)
  68. /* If compiled for profiling, call `mcount' at the start of each function. */
  69. #ifdef PROF
  70. /* The mcount code relies on a normal frame pointer being on the stack
  71. to locate our caller, so push one just for its benefit. */
  72. #define CALL_MCOUNT \
  73. pushq %rbp; \
  74. cfi_adjust_cfa_offset(8); \
  75. movq %rsp, %rbp; \
  76. cfi_def_cfa_register(%rbp); \
  77. call JUMPTARGET(mcount); \
  78. popq %rbp; \
  79. cfi_def_cfa(rsp,8);
  80. #else
  81. #define CALL_MCOUNT /* Do nothing. */
  82. #endif
  83. #ifdef NO_UNDERSCORES
  84. /* Since C identifiers are not normally prefixed with an underscore
  85. on this system, the asm identifier `syscall_error' intrudes on the
  86. C name space. Make sure we use an innocuous name. */
  87. #define syscall_error __syscall_error
  88. #define mcount _mcount
  89. #endif
  90. /* Linux uses a negative return value to indicate syscall errors,
  91. unlike most Unices, which use the condition codes' carry flag.
  92. Since version 2.1 the return value of a system call might be
  93. negative even if the call succeeded. E.g., the `lseek' system call
  94. might return a large offset. Therefore we must not anymore test
  95. for < 0, but test for a real error by making sure the value in %eax
  96. is a real error number. Linus said he will make sure the no syscall
  97. returns a value in -1 .. -4095 as a valid result so we can safely
  98. test with -4095. */
  99. /* We don't want the label for the error handle to be global when we define
  100. it here. */
  101. #ifdef PIC
  102. # define SYSCALL_ERROR_LABEL 0f
  103. #else
  104. # define SYSCALL_ERROR_LABEL syscall_error
  105. #endif
  106. #undef PSEUDO
  107. #define PSEUDO(name, syscall_name, args) \
  108. .text; \
  109. ENTRY (name) \
  110. DO_CALL (syscall_name, args); \
  111. cmpq $-4095, %rax; \
  112. jae SYSCALL_ERROR_LABEL; \
  113. L(pseudo_end):
  114. #undef PSEUDO_END
  115. #define PSEUDO_END(name) \
  116. END (name)
  117. #undef PSEUDO_NOERRNO
  118. #define PSEUDO_NOERRNO(name, syscall_name, args) \
  119. .text; \
  120. ENTRY (name) \
  121. DO_CALL (syscall_name, args)
  122. #undef PSEUDO_END_NOERRNO
  123. #define PSEUDO_END_NOERRNO(name) \
  124. END (name)
  125. #define ret_NOERRNO ret
  126. #undef PSEUDO_ERRVAL
  127. #define PSEUDO_ERRVAL(name, syscall_name, args) \
  128. .text; \
  129. ENTRY (name) \
  130. DO_CALL (syscall_name, args); \
  131. negq %rax
  132. #undef PSEUDO_END_ERRVAL
  133. #define PSEUDO_END_ERRVAL(name) \
  134. END (name)
  135. #define ret_ERRVAL ret
  136. /* The Linux/x86-64 kernel expects the system call parameters in
  137. registers according to the following table:
  138. syscall number rax
  139. arg 1 rdi
  140. arg 2 rsi
  141. arg 3 rdx
  142. arg 4 r10
  143. arg 5 r8
  144. arg 6 r9
  145. The Linux kernel uses and destroys internally these registers:
  146. return address from
  147. syscall rcx
  148. additionally clobbered: r12-r15,rbx,rbp
  149. eflags from syscall r11
  150. Normal function call, including calls to the system call stub
  151. functions in the libc, get the first six parameters passed in
  152. registers and the seventh parameter and later on the stack. The
  153. register use is as follows:
  154. system call number in the DO_CALL macro
  155. arg 1 rdi
  156. arg 2 rsi
  157. arg 3 rdx
  158. arg 4 rcx
  159. arg 5 r8
  160. arg 6 r9
  161. We have to take care that the stack is aligned to 16 bytes. When
  162. called the stack is not aligned since the return address has just
  163. been pushed.
  164. Syscalls of more than 6 arguments are not supported. */
  165. #ifndef DO_SYSCALL
  166. #define DO_SYSCALL syscall
  167. #endif
  168. #undef DO_CALL
  169. #define DO_CALL(syscall_name, args) \
  170. DOARGS_##args \
  171. movl $SYS_ify (syscall_name), %eax; \
  172. DO_SYSCALL;
  173. #define DOARGS_0 /* nothing */
  174. #define DOARGS_1 /* nothing */
  175. #define DOARGS_2 /* nothing */
  176. #define DOARGS_3 /* nothing */
  177. #define DOARGS_4 movq %rcx, %r10;
  178. #define DOARGS_5 DOARGS_4
  179. #define DOARGS_6 DOARGS_5
  180. #else /* !__ASSEMBLER__ */
  181. /* Define a macro which expands inline into the wrapper code for a system
  182. call. */
  183. #undef INLINE_SYSCALL
  184. #define INLINE_SYSCALL(name, nr, args...) INTERNAL_SYSCALL(name, , nr, args)
  185. #undef INTERNAL_SYSCALL_DECL
  186. #define INTERNAL_SYSCALL_DECL(err) do { } while (0)
  187. #ifndef DO_SYSCALL
  188. #define DO_SYSCALL "syscall"
  189. #endif
  190. #define INTERNAL_SYSCALL_NCS(name, err, nr, args...) \
  191. ({ \
  192. unsigned long resultvar; \
  193. LOAD_ARGS_##nr (args) \
  194. LOAD_REGS_##nr \
  195. asm volatile ( \
  196. DO_SYSCALL \
  197. : "=a" (resultvar) \
  198. : "0" (name) ASM_ARGS_##nr : "memory", "cc", "r11", "cx"); \
  199. (long) resultvar; })
  200. #undef INTERNAL_SYSCALL
  201. #define INTERNAL_SYSCALL(name, err, nr, args...) \
  202. INTERNAL_SYSCALL_NCS (__NR_##name, err, nr, ##args)
  203. #undef INTERNAL_SYSCALL_ERROR
  204. #define INTERNAL_SYSCALL_ERROR(val) ((val) < 0)
  205. #undef INTERNAL_SYSCALL_ERROR_P
  206. #define INTERNAL_SYSCALL_ERROR_P(val) \
  207. ((unsigned long) (val) >= -4095L)
  208. #undef INTERNAL_SYSCALL_ERRNO
  209. #define INTERNAL_SYSCALL_ERRNO(val) (-(val))
  210. #undef INTERNAL_SYSCALL_ERRNO_P
  211. #define INTERNAL_SYSCALL_ERRNO_P(val) (-((long) val))
  212. #define LOAD_ARGS_0()
  213. #define LOAD_REGS_0
  214. #define ASM_ARGS_0
  215. #define LOAD_ARGS_1(a1) \
  216. long int __arg1 = (long) (a1); \
  217. LOAD_ARGS_0 ()
  218. #define LOAD_REGS_1 \
  219. register long int _a1 asm ("rdi") = __arg1; \
  220. LOAD_REGS_0
  221. #define ASM_ARGS_1 ASM_ARGS_0, "r" (_a1)
  222. #define LOAD_ARGS_2(a1, a2) \
  223. long int __arg2 = (long) (a2); \
  224. LOAD_ARGS_1 (a1)
  225. #define LOAD_REGS_2 \
  226. register long int _a2 asm ("rsi") = __arg2; \
  227. LOAD_REGS_1
  228. #define ASM_ARGS_2 ASM_ARGS_1, "r" (_a2)
  229. #define LOAD_ARGS_3(a1, a2, a3) \
  230. long int __arg3 = (long) (a3); \
  231. LOAD_ARGS_2 (a1, a2)
  232. #define LOAD_REGS_3 \
  233. register long int _a3 asm ("rdx") = __arg3; \
  234. LOAD_REGS_2
  235. #define ASM_ARGS_3 ASM_ARGS_2, "r" (_a3)
  236. #define LOAD_ARGS_4(a1, a2, a3, a4) \
  237. long int __arg4 = (long) (a4); \
  238. LOAD_ARGS_3 (a1, a2, a3)
  239. #define LOAD_REGS_4 \
  240. register long int _a4 asm ("r10") = __arg4; \
  241. LOAD_REGS_3
  242. #define ASM_ARGS_4 ASM_ARGS_3, "r" (_a4)
  243. #define LOAD_ARGS_5(a1, a2, a3, a4, a5) \
  244. long int __arg5 = (long) (a5); \
  245. LOAD_ARGS_4 (a1, a2, a3, a4)
  246. #define LOAD_REGS_5 \
  247. register long int _a5 asm ("r8") = __arg5; \
  248. LOAD_REGS_4
  249. #define ASM_ARGS_5 ASM_ARGS_4, "r" (_a5)
  250. #define LOAD_ARGS_6(a1, a2, a3, a4, a5, a6) \
  251. long int __arg6 = (long) (a6); \
  252. LOAD_ARGS_5 (a1, a2, a3, a4, a5)
  253. #define LOAD_REGS_6 \
  254. register long int _a6 asm ("r9") = __arg6; \
  255. LOAD_REGS_5
  256. #define ASM_ARGS_6 ASM_ARGS_5, "r" (_a6)
  257. #endif /* __ASSEMBLER__ */
  258. #endif /* linux/x86_64/sysdep.h */