calling.h 9.2 KB

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