sysdep-x86_64.h 7.7 KB

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