calling.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #include <linux/jump_label.h>
  3. #include <asm/unwind_hints.h>
  4. #include <asm/cpufeatures.h>
  5. #include <asm/page_types.h>
  6. /*
  7. x86 function call convention, 64-bit:
  8. -------------------------------------
  9. arguments | callee-saved | extra caller-saved | return
  10. [callee-clobbered] | | [callee-clobbered] |
  11. ---------------------------------------------------------------------------
  12. rdi rsi rdx rcx r8-9 | rbx rbp [*] r12-15 | r10-11 | rax, rdx [**]
  13. ( rsp is obviously invariant across normal function calls. (gcc can 'merge'
  14. functions when it sees tail-call optimization possibilities) rflags is
  15. clobbered. Leftover arguments are passed over the stack frame.)
  16. [*] In the frame-pointers case rbp is fixed to the stack frame.
  17. [**] for struct return values wider than 64 bits the return convention is a
  18. bit more complex: up to 128 bits width we return small structures
  19. straight in rax, rdx. For structures larger than that (3 words or
  20. larger) the caller puts a pointer to an on-stack return struct
  21. [allocated in the caller's stack frame] into the first argument - i.e.
  22. into rdi. All other arguments shift up by one in this case.
  23. Fortunately this case is rare in the kernel.
  24. For 32-bit we have the following conventions - kernel is built with
  25. -mregparm=3 and -freg-struct-return:
  26. x86 function calling convention, 32-bit:
  27. ----------------------------------------
  28. arguments | callee-saved | extra caller-saved | return
  29. [callee-clobbered] | | [callee-clobbered] |
  30. -------------------------------------------------------------------------
  31. eax edx ecx | ebx edi esi ebp [*] | <none> | eax, edx [**]
  32. ( here too esp is obviously invariant across normal function calls. eflags
  33. is clobbered. Leftover arguments are passed over the stack frame. )
  34. [*] In the frame-pointers case ebp is fixed to the stack frame.
  35. [**] We build with -freg-struct-return, which on 32-bit means similar
  36. semantics as on 64-bit: edx can be used for a second return value
  37. (i.e. covering integer and structure sizes up to 64 bits) - after that
  38. it gets more complex and more expensive: 3-word or larger struct returns
  39. get done in the caller's frame and the pointer to the return struct goes
  40. into regparm0, i.e. eax - the other arguments shift up and the
  41. function's register parameters degenerate to regparm=2 in essence.
  42. */
  43. #ifdef CONFIG_X86_64
  44. /*
  45. * 64-bit system call stack frame layout defines and helpers,
  46. * for assembly code:
  47. */
  48. /* The layout forms the "struct pt_regs" on the stack: */
  49. /*
  50. * C ABI says these regs are callee-preserved. They aren't saved on kernel entry
  51. * unless syscall needs a complete, fully filled "struct pt_regs".
  52. */
  53. #define R15 0*8
  54. #define R14 1*8
  55. #define R13 2*8
  56. #define R12 3*8
  57. #define RBP 4*8
  58. #define RBX 5*8
  59. /* These regs are callee-clobbered. Always saved on kernel entry. */
  60. #define R11 6*8
  61. #define R10 7*8
  62. #define R9 8*8
  63. #define R8 9*8
  64. #define RAX 10*8
  65. #define RCX 11*8
  66. #define RDX 12*8
  67. #define RSI 13*8
  68. #define RDI 14*8
  69. /*
  70. * On syscall entry, this is syscall#. On CPU exception, this is error code.
  71. * On hw interrupt, it's IRQ number:
  72. */
  73. #define ORIG_RAX 15*8
  74. /* Return frame for iretq */
  75. #define RIP 16*8
  76. #define CS 17*8
  77. #define EFLAGS 18*8
  78. #define RSP 19*8
  79. #define SS 20*8
  80. #define SIZEOF_PTREGS 21*8
  81. .macro ALLOC_PT_GPREGS_ON_STACK
  82. addq $-(15*8), %rsp
  83. .endm
  84. .macro SAVE_C_REGS_HELPER offset=0 rax=1 rcx=1 r8910=1 r11=1
  85. .if \r11
  86. movq %r11, 6*8+\offset(%rsp)
  87. .endif
  88. .if \r8910
  89. movq %r10, 7*8+\offset(%rsp)
  90. movq %r9, 8*8+\offset(%rsp)
  91. movq %r8, 9*8+\offset(%rsp)
  92. .endif
  93. .if \rax
  94. movq %rax, 10*8+\offset(%rsp)
  95. .endif
  96. .if \rcx
  97. movq %rcx, 11*8+\offset(%rsp)
  98. .endif
  99. movq %rdx, 12*8+\offset(%rsp)
  100. movq %rsi, 13*8+\offset(%rsp)
  101. movq %rdi, 14*8+\offset(%rsp)
  102. UNWIND_HINT_REGS offset=\offset extra=0
  103. .endm
  104. .macro SAVE_C_REGS offset=0
  105. SAVE_C_REGS_HELPER \offset, 1, 1, 1, 1
  106. .endm
  107. .macro SAVE_C_REGS_EXCEPT_RAX_RCX offset=0
  108. SAVE_C_REGS_HELPER \offset, 0, 0, 1, 1
  109. .endm
  110. .macro SAVE_C_REGS_EXCEPT_R891011
  111. SAVE_C_REGS_HELPER 0, 1, 1, 0, 0
  112. .endm
  113. .macro SAVE_C_REGS_EXCEPT_RCX_R891011
  114. SAVE_C_REGS_HELPER 0, 1, 0, 0, 0
  115. .endm
  116. .macro SAVE_C_REGS_EXCEPT_RAX_RCX_R11
  117. SAVE_C_REGS_HELPER 0, 0, 0, 1, 0
  118. .endm
  119. .macro SAVE_EXTRA_REGS offset=0
  120. movq %r15, 0*8+\offset(%rsp)
  121. movq %r14, 1*8+\offset(%rsp)
  122. movq %r13, 2*8+\offset(%rsp)
  123. movq %r12, 3*8+\offset(%rsp)
  124. movq %rbp, 4*8+\offset(%rsp)
  125. movq %rbx, 5*8+\offset(%rsp)
  126. UNWIND_HINT_REGS offset=\offset
  127. .endm
  128. .macro POP_EXTRA_REGS
  129. popq %r15
  130. popq %r14
  131. popq %r13
  132. popq %r12
  133. popq %rbp
  134. popq %rbx
  135. .endm
  136. .macro POP_C_REGS
  137. popq %r11
  138. popq %r10
  139. popq %r9
  140. popq %r8
  141. popq %rax
  142. popq %rcx
  143. popq %rdx
  144. popq %rsi
  145. popq %rdi
  146. .endm
  147. .macro icebp
  148. .byte 0xf1
  149. .endm
  150. /*
  151. * This is a sneaky trick to help the unwinder find pt_regs on the stack. The
  152. * frame pointer is replaced with an encoded pointer to pt_regs. The encoding
  153. * is just setting the LSB, which makes it an invalid stack address and is also
  154. * a signal to the unwinder that it's a pt_regs pointer in disguise.
  155. *
  156. * NOTE: This macro must be used *after* SAVE_EXTRA_REGS because it corrupts
  157. * the original rbp.
  158. */
  159. .macro ENCODE_FRAME_POINTER ptregs_offset=0
  160. #ifdef CONFIG_FRAME_POINTER
  161. .if \ptregs_offset
  162. leaq \ptregs_offset(%rsp), %rbp
  163. .else
  164. mov %rsp, %rbp
  165. .endif
  166. orq $0x1, %rbp
  167. #endif
  168. .endm
  169. #ifdef CONFIG_PAGE_TABLE_ISOLATION
  170. /* PAGE_TABLE_ISOLATION PGDs are 8k. Flip bit 12 to switch between the two halves: */
  171. #define PTI_SWITCH_MASK (1<<PAGE_SHIFT)
  172. .macro ADJUST_KERNEL_CR3 reg:req
  173. /* Clear "PAGE_TABLE_ISOLATION bit", point CR3 at kernel pagetables: */
  174. andq $(~PTI_SWITCH_MASK), \reg
  175. .endm
  176. .macro ADJUST_USER_CR3 reg:req
  177. /* Move CR3 up a page to the user page tables: */
  178. orq $(PTI_SWITCH_MASK), \reg
  179. .endm
  180. .macro SWITCH_TO_KERNEL_CR3 scratch_reg:req
  181. ALTERNATIVE "jmp .Lend_\@", "", X86_FEATURE_PTI
  182. mov %cr3, \scratch_reg
  183. ADJUST_KERNEL_CR3 \scratch_reg
  184. mov \scratch_reg, %cr3
  185. .Lend_\@:
  186. .endm
  187. .macro SWITCH_TO_USER_CR3 scratch_reg:req
  188. ALTERNATIVE "jmp .Lend_\@", "", X86_FEATURE_PTI
  189. mov %cr3, \scratch_reg
  190. ADJUST_USER_CR3 \scratch_reg
  191. mov \scratch_reg, %cr3
  192. .Lend_\@:
  193. .endm
  194. .macro SAVE_AND_SWITCH_TO_KERNEL_CR3 scratch_reg:req save_reg:req
  195. ALTERNATIVE "jmp .Ldone_\@", "", X86_FEATURE_PTI
  196. movq %cr3, \scratch_reg
  197. movq \scratch_reg, \save_reg
  198. /*
  199. * Is the switch bit zero? This means the address is
  200. * up in real PAGE_TABLE_ISOLATION patches in a moment.
  201. */
  202. testq $(PTI_SWITCH_MASK), \scratch_reg
  203. jz .Ldone_\@
  204. ADJUST_KERNEL_CR3 \scratch_reg
  205. movq \scratch_reg, %cr3
  206. .Ldone_\@:
  207. .endm
  208. .macro RESTORE_CR3 save_reg:req
  209. ALTERNATIVE "jmp .Lend_\@", "", X86_FEATURE_PTI
  210. /*
  211. * The CR3 write could be avoided when not changing its value,
  212. * but would require a CR3 read *and* a scratch register.
  213. */
  214. movq \save_reg, %cr3
  215. .Lend_\@:
  216. .endm
  217. #else /* CONFIG_PAGE_TABLE_ISOLATION=n: */
  218. .macro SWITCH_TO_KERNEL_CR3 scratch_reg:req
  219. .endm
  220. .macro SWITCH_TO_USER_CR3 scratch_reg:req
  221. .endm
  222. .macro SAVE_AND_SWITCH_TO_KERNEL_CR3 scratch_reg:req save_reg:req
  223. .endm
  224. .macro RESTORE_CR3 save_reg:req
  225. .endm
  226. #endif
  227. #endif /* CONFIG_X86_64 */
  228. /*
  229. * This does 'call enter_from_user_mode' unless we can avoid it based on
  230. * kernel config or using the static jump infrastructure.
  231. */
  232. .macro CALL_enter_from_user_mode
  233. #ifdef CONFIG_CONTEXT_TRACKING
  234. #ifdef HAVE_JUMP_LABEL
  235. STATIC_JUMP_IF_FALSE .Lafter_call_\@, context_tracking_enabled, def=0
  236. #endif
  237. call enter_from_user_mode
  238. .Lafter_call_\@:
  239. #endif
  240. .endm