sysdep-x86_64.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. /* This file was copied directly from the Linux implementation and modified to work on FreeBSD.
  15. It may contain a lot of unnecessary code. */
  16. #ifndef _LINUX_X86_64_SYSDEP_H
  17. #define _LINUX_X86_64_SYSDEP_H 1
  18. #include <sysdeps/generic/sysdep.h>
  19. #include <sys/syscall.h>
  20. /* BSD useds SYS_* sys call names, so we can use these to map syscall names to syscall numbers from syscall.h */
  21. #define SYS_ifyBSD(syscall_name) SYS_##syscall_name
  22. #ifdef __ASSEMBLER__
  23. /* ELF uses byte-counts for .align, most others use log2 of count of bytes. */
  24. #define ALIGNARG(log2) (1 << (log2))
  25. #define ASM_GLOBAL_DIRECTIVE .global
  26. /* For ELF we need the `.type' directive to make shared libs work right. */
  27. #define ASM_TYPE_DIRECTIVE(name,typearg) .type name,typearg;
  28. #define ASM_SIZE_DIRECTIVE(name) .size name,.-name;
  29. /* Define an entry point visible from C. */
  30. #define ENTRY(name) \
  31. ASM_GLOBAL_DIRECTIVE name; \
  32. ASM_TYPE_DIRECTIVE (name,@function) \
  33. .align ALIGNARG(4); \
  34. name: \
  35. cfi_startproc;
  36. #undef END
  37. #define END(name) \
  38. cfi_endproc; \
  39. ASM_SIZE_DIRECTIVE(name)
  40. #undef DO_CALL
  41. #define DO_CALL(syscall_name, args) \
  42. DOARGS_##args \
  43. movl $SYS_ify (syscall_name), %eax; \
  44. syscall;
  45. #define DOARGS_0 /* nothing */
  46. #define DOARGS_1 /* nothing */
  47. #define DOARGS_2 /* nothing */
  48. #define DOARGS_3 /* nothing */
  49. #define DOARGS_4 movq %rcx, %r10;
  50. #define DOARGS_5 DOARGS_4
  51. #define DOARGS_6 DOARGS_5
  52. #else /* !__ASSEMBLER__ */
  53. /* Define a macro which expands inline into the wrapper code for a system
  54. call. */
  55. #undef INLINE_SYSCALL
  56. #define INLINE_SYSCALL(name, nr, args...) INTERNAL_SYSCALL(name, , nr, args)
  57. #undef INTERNAL_SYSCALL_DECL
  58. #define INTERNAL_SYSCALL_DECL(err) do { } while (0)
  59. #define INTERNAL_SYSCALL_NCS(name, err, nr, args...) \
  60. ({ \
  61. LOAD_ARGS_##nr (args) \
  62. LOAD_REGS_##nr \
  63. unsigned long resultvar; \
  64. asm volatile ( \
  65. "int $0x80\n\t" \
  66. : "=a" (resultvar) \
  67. : "0" (name) ASM_ARGS_##nr \
  68. : "memory", "cc"); \
  69. (long) IS_SYSCALL_ERROR(resultvar); \
  70. })
  71. #undef INTERNAL_SYSCALL
  72. #define INTERNAL_SYSCALL(name, err, nr, args...) \
  73. INTERNAL_SYSCALL_NCS(SYS_ifyBSD(name), err, nr, ##args)
  74. #undef INTERNAL_SYSCALL_ERROR
  75. #define INTERNAL_SYSCALL_ERROR(val) ((val) < 0)
  76. #undef INTERNAL_SYSCALL_ERROR_P
  77. #define INTERNAL_SYSCALL_ERROR_P(val) \
  78. ((unsigned long) (val) >= -4095L)
  79. #undef INTERNAL_SYSCALL_ERRNO
  80. #define INTERNAL_SYSCALL_ERRNO(val) (-(val))
  81. #undef INTERNAL_SYSCALL_ERRNO_P
  82. #define INTERNAL_SYSCALL_ERRNO_P(val) (-((long) val))
  83. /*
  84. If a syscall fails, it generally sets the carry flag and returns the error code in rax.
  85. To simplify matters and reuse a lot of the Linux code, we change rax to negative after checking the carry flag.
  86. */
  87. #define IS_SYSCALL_ERROR(val) \
  88. ({ \
  89. int carry; \
  90. asm volatile("mov $0, %0\n\t" \
  91. "adc $0, %0\n\t" \
  92. : "=b"(carry) \
  93. : \
  94. : "cc", "memory", "eax"); \
  95. (carry) ? -(val) : (val); })
  96. #define LOAD_ARGS_0()
  97. #define LOAD_REGS_0
  98. #define ASM_ARGS_0
  99. #define LOAD_ARGS_1(a1) \
  100. long int __arg1 = (long)(a1); \
  101. LOAD_ARGS_0()
  102. #define LOAD_REGS_1 \
  103. register long int _a1 asm ("rdi") = __arg1; \
  104. LOAD_REGS_0
  105. #define ASM_ARGS_1 ASM_ARGS_0, "r" (_a1)
  106. #define LOAD_ARGS_2(a1, a2) \
  107. long int __arg2 = (long)(a2); \
  108. LOAD_ARGS_1(a1)
  109. #define LOAD_REGS_2 \
  110. register long int _a2 asm ("rsi") = __arg2; \
  111. LOAD_REGS_1
  112. #define ASM_ARGS_2 ASM_ARGS_1, "r" (_a2)
  113. #define LOAD_ARGS_3(a1, a2, a3) \
  114. long int __arg3 = (long)(a3); \
  115. LOAD_ARGS_2(a1, a2)
  116. #define LOAD_REGS_3 \
  117. register long int _a3 asm ("rdx") = __arg3; \
  118. LOAD_REGS_2
  119. #define ASM_ARGS_3 ASM_ARGS_2, "r" (_a3)
  120. #define LOAD_ARGS_4(a1, a2, a3, a4) \
  121. long int __arg4 = (long)(a4); \
  122. LOAD_ARGS_3(a1, a2, a3)
  123. #define LOAD_REGS_4 \
  124. register long int _a4 asm ("rcx") = __arg4; \
  125. LOAD_REGS_3
  126. #define ASM_ARGS_4 ASM_ARGS_3, "r" (_a4)
  127. #define LOAD_ARGS_5(a1, a2, a3, a4, a5) \
  128. long int __arg5 = (long)(a5); \
  129. LOAD_ARGS_4(a1, a2, a3, a4)
  130. #define LOAD_REGS_5 \
  131. register long int _a5 asm ("r8") = __arg5; \
  132. LOAD_REGS_4
  133. #define ASM_ARGS_5 ASM_ARGS_4, "r" (_a5)
  134. #define LOAD_ARGS_6(a1, a2, a3, a4, a5, a6) \
  135. long int __arg6 = (long)(a6); \
  136. LOAD_ARGS_5(a1, a2, a3, a4, a5)
  137. #define LOAD_REGS_6 \
  138. register long int _a6 asm ("r9") = __arg6; \
  139. LOAD_REGS_5
  140. #define ASM_ARGS_6 ASM_ARGS_5, "r" (_a6)
  141. #endif /* __ASSEMBLER__ */
  142. #endif /* linux/x86_64/sysdep.h */