asm.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef _ASM_X86_ASM_H
  3. #define _ASM_X86_ASM_H
  4. #ifdef __ASSEMBLY__
  5. # define __ASM_FORM(x, ...) x,## __VA_ARGS__
  6. # define __ASM_FORM_RAW(x, ...) x,## __VA_ARGS__
  7. # define __ASM_FORM_COMMA(x, ...) x,## __VA_ARGS__,
  8. # define __ASM_REGPFX %
  9. #else
  10. #include <linux/stringify.h>
  11. # define __ASM_FORM(x, ...) " " __stringify(x,##__VA_ARGS__) " "
  12. # define __ASM_FORM_RAW(x, ...) __stringify(x,##__VA_ARGS__)
  13. # define __ASM_FORM_COMMA(x, ...) " " __stringify(x,##__VA_ARGS__) ","
  14. # define __ASM_REGPFX %%
  15. #endif
  16. #define _ASM_BYTES(x, ...) __ASM_FORM(.byte x,##__VA_ARGS__ ;)
  17. #ifndef __x86_64__
  18. /* 32 bit */
  19. # define __ASM_SEL(a,b) __ASM_FORM(a)
  20. # define __ASM_SEL_RAW(a,b) __ASM_FORM_RAW(a)
  21. #else
  22. /* 64 bit */
  23. # define __ASM_SEL(a,b) __ASM_FORM(b)
  24. # define __ASM_SEL_RAW(a,b) __ASM_FORM_RAW(b)
  25. #endif
  26. #define __ASM_SIZE(inst, ...) __ASM_SEL(inst##l##__VA_ARGS__, \
  27. inst##q##__VA_ARGS__)
  28. #define __ASM_REG(reg) __ASM_SEL_RAW(e##reg, r##reg)
  29. #define _ASM_PTR __ASM_SEL(.long, .quad)
  30. #define _ASM_ALIGN __ASM_SEL(.balign 4, .balign 8)
  31. #define _ASM_MOV __ASM_SIZE(mov)
  32. #define _ASM_INC __ASM_SIZE(inc)
  33. #define _ASM_DEC __ASM_SIZE(dec)
  34. #define _ASM_ADD __ASM_SIZE(add)
  35. #define _ASM_SUB __ASM_SIZE(sub)
  36. #define _ASM_XADD __ASM_SIZE(xadd)
  37. #define _ASM_MUL __ASM_SIZE(mul)
  38. #define _ASM_AX __ASM_REG(ax)
  39. #define _ASM_BX __ASM_REG(bx)
  40. #define _ASM_CX __ASM_REG(cx)
  41. #define _ASM_DX __ASM_REG(dx)
  42. #define _ASM_SP __ASM_REG(sp)
  43. #define _ASM_BP __ASM_REG(bp)
  44. #define _ASM_SI __ASM_REG(si)
  45. #define _ASM_DI __ASM_REG(di)
  46. /* Adds a (%rip) suffix on 64 bits only; for immediate memory references */
  47. #define _ASM_RIP(x) __ASM_SEL_RAW(x, x (__ASM_REGPFX rip))
  48. #ifndef __x86_64__
  49. /* 32 bit */
  50. #define _ASM_ARG1 _ASM_AX
  51. #define _ASM_ARG2 _ASM_DX
  52. #define _ASM_ARG3 _ASM_CX
  53. #define _ASM_ARG1L eax
  54. #define _ASM_ARG2L edx
  55. #define _ASM_ARG3L ecx
  56. #define _ASM_ARG1W ax
  57. #define _ASM_ARG2W dx
  58. #define _ASM_ARG3W cx
  59. #define _ASM_ARG1B al
  60. #define _ASM_ARG2B dl
  61. #define _ASM_ARG3B cl
  62. #else
  63. /* 64 bit */
  64. #define _ASM_ARG1 _ASM_DI
  65. #define _ASM_ARG2 _ASM_SI
  66. #define _ASM_ARG3 _ASM_DX
  67. #define _ASM_ARG4 _ASM_CX
  68. #define _ASM_ARG5 r8
  69. #define _ASM_ARG6 r9
  70. #define _ASM_ARG1Q rdi
  71. #define _ASM_ARG2Q rsi
  72. #define _ASM_ARG3Q rdx
  73. #define _ASM_ARG4Q rcx
  74. #define _ASM_ARG5Q r8
  75. #define _ASM_ARG6Q r9
  76. #define _ASM_ARG1L edi
  77. #define _ASM_ARG2L esi
  78. #define _ASM_ARG3L edx
  79. #define _ASM_ARG4L ecx
  80. #define _ASM_ARG5L r8d
  81. #define _ASM_ARG6L r9d
  82. #define _ASM_ARG1W di
  83. #define _ASM_ARG2W si
  84. #define _ASM_ARG3W dx
  85. #define _ASM_ARG4W cx
  86. #define _ASM_ARG5W r8w
  87. #define _ASM_ARG6W r9w
  88. #define _ASM_ARG1B dil
  89. #define _ASM_ARG2B sil
  90. #define _ASM_ARG3B dl
  91. #define _ASM_ARG4B cl
  92. #define _ASM_ARG5B r8b
  93. #define _ASM_ARG6B r9b
  94. #endif
  95. /*
  96. * Macros to generate condition code outputs from inline assembly,
  97. * The output operand must be type "bool".
  98. */
  99. #ifdef __GCC_ASM_FLAG_OUTPUTS__
  100. # define CC_SET(c) "\n\t/* output condition code " #c "*/\n"
  101. # define CC_OUT(c) "=@cc" #c
  102. #else
  103. # define CC_SET(c) "\n\tset" #c " %[_cc_" #c "]\n"
  104. # define CC_OUT(c) [_cc_ ## c] "=qm"
  105. #endif
  106. #ifdef __KERNEL__
  107. # include <asm/extable_fixup_types.h>
  108. /* Exception table entry */
  109. #ifdef __ASSEMBLY__
  110. # define _ASM_EXTABLE_TYPE(from, to, type) \
  111. .pushsection "__ex_table","a" ; \
  112. .balign 4 ; \
  113. .long (from) - . ; \
  114. .long (to) - . ; \
  115. .long type ; \
  116. .popsection
  117. # ifdef CONFIG_KPROBES
  118. # define _ASM_NOKPROBE(entry) \
  119. .pushsection "_kprobe_blacklist","aw" ; \
  120. _ASM_ALIGN ; \
  121. _ASM_PTR (entry); \
  122. .popsection
  123. # else
  124. # define _ASM_NOKPROBE(entry)
  125. # endif
  126. #else /* ! __ASSEMBLY__ */
  127. # define DEFINE_EXTABLE_TYPE_REG \
  128. ".macro extable_type_reg type:req reg:req\n" \
  129. ".set .Lfound, 0\n" \
  130. ".set .Lregnr, 0\n" \
  131. ".irp rs,rax,rcx,rdx,rbx,rsp,rbp,rsi,rdi,r8,r9,r10,r11,r12,r13,r14,r15\n" \
  132. ".ifc \\reg, %%\\rs\n" \
  133. ".set .Lfound, .Lfound+1\n" \
  134. ".long \\type + (.Lregnr << 8)\n" \
  135. ".endif\n" \
  136. ".set .Lregnr, .Lregnr+1\n" \
  137. ".endr\n" \
  138. ".set .Lregnr, 0\n" \
  139. ".irp rs,eax,ecx,edx,ebx,esp,ebp,esi,edi,r8d,r9d,r10d,r11d,r12d,r13d,r14d,r15d\n" \
  140. ".ifc \\reg, %%\\rs\n" \
  141. ".set .Lfound, .Lfound+1\n" \
  142. ".long \\type + (.Lregnr << 8)\n" \
  143. ".endif\n" \
  144. ".set .Lregnr, .Lregnr+1\n" \
  145. ".endr\n" \
  146. ".if (.Lfound != 1)\n" \
  147. ".error \"extable_type_reg: bad register argument\"\n" \
  148. ".endif\n" \
  149. ".endm\n"
  150. # define UNDEFINE_EXTABLE_TYPE_REG \
  151. ".purgem extable_type_reg\n"
  152. # define _ASM_EXTABLE_TYPE(from, to, type) \
  153. " .pushsection \"__ex_table\",\"a\"\n" \
  154. " .balign 4\n" \
  155. " .long (" #from ") - .\n" \
  156. " .long (" #to ") - .\n" \
  157. " .long " __stringify(type) " \n" \
  158. " .popsection\n"
  159. # define _ASM_EXTABLE_TYPE_REG(from, to, type, reg) \
  160. " .pushsection \"__ex_table\",\"a\"\n" \
  161. " .balign 4\n" \
  162. " .long (" #from ") - .\n" \
  163. " .long (" #to ") - .\n" \
  164. DEFINE_EXTABLE_TYPE_REG \
  165. "extable_type_reg reg=" __stringify(reg) ", type=" __stringify(type) " \n"\
  166. UNDEFINE_EXTABLE_TYPE_REG \
  167. " .popsection\n"
  168. /* For C file, we already have NOKPROBE_SYMBOL macro */
  169. /*
  170. * This output constraint should be used for any inline asm which has a "call"
  171. * instruction. Otherwise the asm may be inserted before the frame pointer
  172. * gets set up by the containing function. If you forget to do this, objtool
  173. * may print a "call without frame pointer save/setup" warning.
  174. */
  175. register unsigned long current_stack_pointer asm(_ASM_SP);
  176. #define ASM_CALL_CONSTRAINT "+r" (current_stack_pointer)
  177. #endif /* __ASSEMBLY__ */
  178. #define _ASM_EXTABLE(from, to) \
  179. _ASM_EXTABLE_TYPE(from, to, EX_TYPE_DEFAULT)
  180. #define _ASM_EXTABLE_UA(from, to) \
  181. _ASM_EXTABLE_TYPE(from, to, EX_TYPE_UACCESS)
  182. #define _ASM_EXTABLE_CPY(from, to) \
  183. _ASM_EXTABLE_TYPE(from, to, EX_TYPE_COPY)
  184. #define _ASM_EXTABLE_FAULT(from, to) \
  185. _ASM_EXTABLE_TYPE(from, to, EX_TYPE_FAULT)
  186. #endif /* __KERNEL__ */
  187. #endif /* _ASM_X86_ASM_H */