sysdep-x86_64.h 6.6 KB

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