calling.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  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 PUSH_AND_CLEAR_REGS rdx=%rdx rax=%rax save_ret=0
  85. /*
  86. * Push registers and sanitize registers of values that a
  87. * speculation attack might otherwise want to exploit. The
  88. * lower registers are likely clobbered well before they
  89. * could be put to use in a speculative execution gadget.
  90. * Interleave XOR with PUSH for better uop scheduling:
  91. */
  92. .if \save_ret
  93. pushq %rsi /* pt_regs->si */
  94. movq 8(%rsp), %rsi /* temporarily store the return address in %rsi */
  95. movq %rdi, 8(%rsp) /* pt_regs->di (overwriting original return address) */
  96. .else
  97. pushq %rdi /* pt_regs->di */
  98. pushq %rsi /* pt_regs->si */
  99. .endif
  100. pushq \rdx /* pt_regs->dx */
  101. xorl %edx, %edx /* nospec dx */
  102. pushq %rcx /* pt_regs->cx */
  103. xorl %ecx, %ecx /* nospec cx */
  104. pushq \rax /* pt_regs->ax */
  105. pushq %r8 /* pt_regs->r8 */
  106. xorl %r8d, %r8d /* nospec r8 */
  107. pushq %r9 /* pt_regs->r9 */
  108. xorl %r9d, %r9d /* nospec r9 */
  109. pushq %r10 /* pt_regs->r10 */
  110. xorl %r10d, %r10d /* nospec r10 */
  111. pushq %r11 /* pt_regs->r11 */
  112. xorl %r11d, %r11d /* nospec r11*/
  113. pushq %rbx /* pt_regs->rbx */
  114. xorl %ebx, %ebx /* nospec rbx*/
  115. pushq %rbp /* pt_regs->rbp */
  116. xorl %ebp, %ebp /* nospec rbp*/
  117. pushq %r12 /* pt_regs->r12 */
  118. xorl %r12d, %r12d /* nospec r12*/
  119. pushq %r13 /* pt_regs->r13 */
  120. xorl %r13d, %r13d /* nospec r13*/
  121. pushq %r14 /* pt_regs->r14 */
  122. xorl %r14d, %r14d /* nospec r14*/
  123. pushq %r15 /* pt_regs->r15 */
  124. xorl %r15d, %r15d /* nospec r15*/
  125. UNWIND_HINT_REGS
  126. .if \save_ret
  127. pushq %rsi /* return address on top of stack */
  128. .endif
  129. .endm
  130. .macro POP_REGS pop_rdi=1 skip_r11rcx=0
  131. popq %r15
  132. popq %r14
  133. popq %r13
  134. popq %r12
  135. popq %rbp
  136. popq %rbx
  137. .if \skip_r11rcx
  138. popq %rsi
  139. .else
  140. popq %r11
  141. .endif
  142. popq %r10
  143. popq %r9
  144. popq %r8
  145. popq %rax
  146. .if \skip_r11rcx
  147. popq %rsi
  148. .else
  149. popq %rcx
  150. .endif
  151. popq %rdx
  152. popq %rsi
  153. .if \pop_rdi
  154. popq %rdi
  155. .endif
  156. .endm
  157. /*
  158. * This is a sneaky trick to help the unwinder find pt_regs on the stack. The
  159. * frame pointer is replaced with an encoded pointer to pt_regs. The encoding
  160. * is just setting the LSB, which makes it an invalid stack address and is also
  161. * a signal to the unwinder that it's a pt_regs pointer in disguise.
  162. *
  163. * NOTE: This macro must be used *after* PUSH_AND_CLEAR_REGS because it corrupts
  164. * the original rbp.
  165. */
  166. .macro ENCODE_FRAME_POINTER ptregs_offset=0
  167. #ifdef CONFIG_FRAME_POINTER
  168. leaq 1+\ptregs_offset(%rsp), %rbp
  169. #endif
  170. .endm
  171. #ifdef CONFIG_PAGE_TABLE_ISOLATION
  172. /*
  173. * PAGE_TABLE_ISOLATION PGDs are 8k. Flip bit 12 to switch between the two
  174. * halves:
  175. */
  176. #define PTI_USER_PGTABLE_BIT PAGE_SHIFT
  177. #define PTI_USER_PGTABLE_MASK (1 << PTI_USER_PGTABLE_BIT)
  178. #define PTI_USER_PCID_BIT X86_CR3_PTI_PCID_USER_BIT
  179. #define PTI_USER_PCID_MASK (1 << PTI_USER_PCID_BIT)
  180. #define PTI_USER_PGTABLE_AND_PCID_MASK (PTI_USER_PCID_MASK | PTI_USER_PGTABLE_MASK)
  181. .macro SET_NOFLUSH_BIT reg:req
  182. bts $X86_CR3_PCID_NOFLUSH_BIT, \reg
  183. .endm
  184. .macro ADJUST_KERNEL_CR3 reg:req
  185. ALTERNATIVE "", "SET_NOFLUSH_BIT \reg", X86_FEATURE_PCID
  186. /* Clear PCID and "PAGE_TABLE_ISOLATION bit", point CR3 at kernel pagetables: */
  187. andq $(~PTI_USER_PGTABLE_AND_PCID_MASK), \reg
  188. .endm
  189. .macro SWITCH_TO_KERNEL_CR3 scratch_reg:req
  190. ALTERNATIVE "jmp .Lend_\@", "", X86_FEATURE_PTI
  191. mov %cr3, \scratch_reg
  192. ADJUST_KERNEL_CR3 \scratch_reg
  193. mov \scratch_reg, %cr3
  194. .Lend_\@:
  195. .endm
  196. #define THIS_CPU_user_pcid_flush_mask \
  197. PER_CPU_VAR(cpu_tlbstate) + TLB_STATE_user_pcid_flush_mask
  198. .macro SWITCH_TO_USER_CR3_NOSTACK scratch_reg:req scratch_reg2:req
  199. ALTERNATIVE "jmp .Lend_\@", "", X86_FEATURE_PTI
  200. mov %cr3, \scratch_reg
  201. ALTERNATIVE "jmp .Lwrcr3_\@", "", X86_FEATURE_PCID
  202. /*
  203. * Test if the ASID needs a flush.
  204. */
  205. movq \scratch_reg, \scratch_reg2
  206. andq $(0x7FF), \scratch_reg /* mask ASID */
  207. bt \scratch_reg, THIS_CPU_user_pcid_flush_mask
  208. jnc .Lnoflush_\@
  209. /* Flush needed, clear the bit */
  210. btr \scratch_reg, THIS_CPU_user_pcid_flush_mask
  211. movq \scratch_reg2, \scratch_reg
  212. jmp .Lwrcr3_pcid_\@
  213. .Lnoflush_\@:
  214. movq \scratch_reg2, \scratch_reg
  215. SET_NOFLUSH_BIT \scratch_reg
  216. .Lwrcr3_pcid_\@:
  217. /* Flip the ASID to the user version */
  218. orq $(PTI_USER_PCID_MASK), \scratch_reg
  219. .Lwrcr3_\@:
  220. /* Flip the PGD to the user version */
  221. orq $(PTI_USER_PGTABLE_MASK), \scratch_reg
  222. mov \scratch_reg, %cr3
  223. .Lend_\@:
  224. .endm
  225. .macro SWITCH_TO_USER_CR3_STACK scratch_reg:req
  226. pushq %rax
  227. SWITCH_TO_USER_CR3_NOSTACK scratch_reg=\scratch_reg scratch_reg2=%rax
  228. popq %rax
  229. .endm
  230. .macro SAVE_AND_SWITCH_TO_KERNEL_CR3 scratch_reg:req save_reg:req
  231. ALTERNATIVE "jmp .Ldone_\@", "", X86_FEATURE_PTI
  232. movq %cr3, \scratch_reg
  233. movq \scratch_reg, \save_reg
  234. /*
  235. * Test the user pagetable bit. If set, then the user page tables
  236. * are active. If clear CR3 already has the kernel page table
  237. * active.
  238. */
  239. bt $PTI_USER_PGTABLE_BIT, \scratch_reg
  240. jnc .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 $PTI_USER_PGTABLE_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. .macro STACKLEAK_ERASE_NOCLOBBER
  287. #ifdef CONFIG_GCC_PLUGIN_STACKLEAK
  288. PUSH_AND_CLEAR_REGS
  289. call stackleak_erase
  290. POP_REGS
  291. #endif
  292. .endm
  293. #endif /* CONFIG_X86_64 */
  294. .macro STACKLEAK_ERASE
  295. #ifdef CONFIG_GCC_PLUGIN_STACKLEAK
  296. call stackleak_erase
  297. #endif
  298. .endm
  299. /*
  300. * This does 'call enter_from_user_mode' unless we can avoid it based on
  301. * kernel config or using the static jump infrastructure.
  302. */
  303. .macro CALL_enter_from_user_mode
  304. #ifdef CONFIG_CONTEXT_TRACKING
  305. #ifdef HAVE_JUMP_LABEL
  306. STATIC_JUMP_IF_FALSE .Lafter_call_\@, context_tracking_enabled, def=0
  307. #endif
  308. call enter_from_user_mode
  309. .Lafter_call_\@:
  310. #endif
  311. .endm