sysdep-x86_64.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /* Copyright (C) 2014 Stony Brook University
  2. This file is part of Graphene Library OS.
  3. Graphene Library OS is free software: you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public License
  5. as published by the Free Software Foundation, either version 3 of the
  6. License, or (at your option) any later version.
  7. Graphene Library OS is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  13. /* This file is imported and modified from the GNU C Library */
  14. #ifndef _LINUX_X86_64_SYSDEP_H
  15. #define _LINUX_X86_64_SYSDEP_H 1
  16. #include <sysdeps/generic/sysdep.h>
  17. /* For Linux we can use the system call table in the header file
  18. /usr/include/asm/unistd.h
  19. of the kernel. But these symbols do not follow the SYS_* syntax
  20. so we have to redefine the `SYS_ify' macro here. */
  21. #undef SYS_ify
  22. #define SYS_ify(syscall_name) __NR_##syscall_name
  23. /* This is a kludge to make syscalls.list find these under the names
  24. pread and pwrite, since some kernel headers define those names
  25. and some define the *64 names for the same system calls. */
  26. #if !defined __NR_pread && defined __NR_pread64
  27. #define __NR_pread __NR_pread64
  28. #endif
  29. #if !defined __NR_pwrite && defined __NR_pwrite64
  30. #define __NR_pwrite __NR_pwrite64
  31. #endif
  32. /* This is to help the old kernel headers where __NR_semtimedop is not
  33. available. */
  34. #ifndef __NR_semtimedop
  35. #define __NR_semtimedop 220
  36. #endif
  37. #ifdef __ASSEMBLER__
  38. /* ELF uses byte-counts for .align, most others use log2 of count of bytes. */
  39. #define ALIGNARG(log2) (1 << (log2))
  40. #define ASM_GLOBAL_DIRECTIVE .global
  41. /* For ELF we need the `.type' directive to make shared libs work right. */
  42. #define ASM_TYPE_DIRECTIVE(name, typearg) .type name,typearg;
  43. #define ASM_SIZE_DIRECTIVE(name) .size name,.-name;
  44. #define C_LABEL(name) name
  45. /* Define an entry point visible from C. */
  46. #define ENTRY(name) \
  47. ASM_GLOBAL_DIRECTIVE name; \
  48. ASM_TYPE_DIRECTIVE(name, @function); \
  49. .align ALIGNARG(4); \
  50. name: \
  51. cfi_startproc;
  52. #undef END
  53. #define END(name) \
  54. cfi_endproc; \
  55. ASM_SIZE_DIRECTIVE(name)
  56. /* The Linux/x86-64 kernel expects the system call parameters in
  57. registers according to the following table:
  58. syscall number rax
  59. arg 1 rdi
  60. arg 2 rsi
  61. arg 3 rdx
  62. arg 4 r10
  63. arg 5 r8
  64. arg 6 r9
  65. The Linux kernel uses and destroys internally these registers:
  66. return address from
  67. syscall rcx
  68. additionally clobbered: r12-r15,rbx,rbp
  69. eflags from syscall r11
  70. Normal function call, including calls to the system call stub
  71. functions in the libc, get the first six parameters passed in
  72. registers and the seventh parameter and later on the stack. The
  73. register use is as follows:
  74. system call number in the DO_CALL macro
  75. arg 1 rdi
  76. arg 2 rsi
  77. arg 3 rdx
  78. arg 4 rcx
  79. arg 5 r8
  80. arg 6 r9
  81. We have to take care that the stack is aligned to 16 bytes. When
  82. called the stack is not aligned since the return address has just
  83. been pushed.
  84. Syscalls of more than 6 arguments are not supported. */
  85. #ifndef DO_SYSCALL
  86. #define DO_SYSCALL syscall
  87. #endif
  88. #undef DO_CALL
  89. #define DO_CALL(syscall_name, args) \
  90. DOARGS_##args; \
  91. movl $SYS_ify(syscall_name), %eax; \
  92. DO_SYSCALL;
  93. #define DOARGS_0 /* nothing */
  94. #define DOARGS_1 /* nothing */
  95. #define DOARGS_2 /* nothing */
  96. #define DOARGS_3 /* nothing */
  97. #define DOARGS_4 movq %rcx, %r10;
  98. #define DOARGS_5 DOARGS_4
  99. #define DOARGS_6 DOARGS_5
  100. #else /* !__ASSEMBLER__ */
  101. /* Define a macro which expands inline into the wrapper code for a system
  102. call. */
  103. #undef INLINE_SYSCALL
  104. #define INLINE_SYSCALL(name, nr, args...) INTERNAL_SYSCALL(name, , nr, args)
  105. #undef INTERNAL_SYSCALL_DECL
  106. #define INTERNAL_SYSCALL_DECL(err) \
  107. do { \
  108. } while (0)
  109. #ifndef DO_SYSCALL
  110. #define DO_SYSCALL "syscall"
  111. #endif
  112. #define INTERNAL_SYSCALL_NCS(name, err, nr, args...) \
  113. ({ \
  114. unsigned long resultvar; \
  115. LOAD_ARGS_##nr(args); \
  116. LOAD_REGS_##nr; \
  117. __asm__ volatile(DO_SYSCALL \
  118. : "=a"(resultvar) \
  119. : "0"(name)ASM_ARGS_##nr \
  120. : "memory", "cc", "r11", "cx"); \
  121. (long)resultvar; \
  122. })
  123. #undef INTERNAL_SYSCALL
  124. #define INTERNAL_SYSCALL(name, err, nr, args...) INTERNAL_SYSCALL_NCS(__NR_##name, err, nr, ##args)
  125. #undef INTERNAL_SYSCALL_ERROR
  126. #define INTERNAL_SYSCALL_ERROR(val) ((val) < 0)
  127. #undef INTERNAL_SYSCALL_ERROR_P
  128. #define INTERNAL_SYSCALL_ERROR_P(val) ((unsigned long)(val) >= (unsigned long)-4095L)
  129. #undef INTERNAL_SYSCALL_ERRNO
  130. #define INTERNAL_SYSCALL_ERRNO(val) (-(val))
  131. #undef INTERNAL_SYSCALL_ERRNO_P
  132. #define INTERNAL_SYSCALL_ERRNO_P(val) (-((long)val))
  133. #define LOAD_ARGS_0()
  134. #define LOAD_REGS_0
  135. #define ASM_ARGS_0
  136. #define LOAD_ARGS_1(a1) \
  137. long int __arg1 = (long)(a1); \
  138. LOAD_ARGS_0()
  139. #define LOAD_REGS_1 \
  140. register long int _a1 __asm__("rdi") = __arg1; \
  141. LOAD_REGS_0
  142. #define ASM_ARGS_1 ASM_ARGS_0, "r"(_a1)
  143. #define LOAD_ARGS_2(a1, a2) \
  144. long int __arg2 = (long)(a2); \
  145. LOAD_ARGS_1(a1)
  146. #define LOAD_REGS_2 \
  147. register long int _a2 __asm__("rsi") = __arg2; \
  148. LOAD_REGS_1
  149. #define ASM_ARGS_2 ASM_ARGS_1, "r"(_a2)
  150. #define LOAD_ARGS_3(a1, a2, a3) \
  151. long int __arg3 = (long)(a3); \
  152. LOAD_ARGS_2(a1, a2)
  153. #define LOAD_REGS_3 \
  154. register long int _a3 __asm__("rdx") = __arg3; \
  155. LOAD_REGS_2
  156. #define ASM_ARGS_3 ASM_ARGS_2, "r"(_a3)
  157. #define LOAD_ARGS_4(a1, a2, a3, a4) \
  158. long int __arg4 = (long)(a4); \
  159. LOAD_ARGS_3(a1, a2, a3)
  160. #define LOAD_REGS_4 \
  161. register long int _a4 __asm__("r10") = __arg4; \
  162. LOAD_REGS_3
  163. #define ASM_ARGS_4 ASM_ARGS_3, "r"(_a4)
  164. #define LOAD_ARGS_5(a1, a2, a3, a4, a5) \
  165. long int __arg5 = (long)(a5); \
  166. LOAD_ARGS_4(a1, a2, a3, a4)
  167. #define LOAD_REGS_5 \
  168. register long int _a5 __asm__("r8") = __arg5; \
  169. LOAD_REGS_4
  170. #define ASM_ARGS_5 ASM_ARGS_4, "r"(_a5)
  171. #define LOAD_ARGS_6(a1, a2, a3, a4, a5, a6) \
  172. long int __arg6 = (long)(a6); \
  173. LOAD_ARGS_5(a1, a2, a3, a4, a5)
  174. #define LOAD_REGS_6 \
  175. register long int _a6 __asm__("r9") = __arg6; \
  176. LOAD_REGS_5
  177. #define ASM_ARGS_6 ASM_ARGS_5, "r"(_a6)
  178. #endif /* __ASSEMBLER__ */
  179. #endif /* linux/x86_64/sysdep.h */