entry_64.S 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * linux/arch/x86_64/entry.S
  4. *
  5. * Copyright (C) 1991, 1992 Linus Torvalds
  6. * Copyright (C) 2000, 2001, 2002 Andi Kleen SuSE Labs
  7. * Copyright (C) 2000 Pavel Machek <pavel@suse.cz>
  8. *
  9. * entry.S contains the system-call and fault low-level handling routines.
  10. *
  11. * Some of this is documented in Documentation/arch/x86/entry_64.rst
  12. *
  13. * A note on terminology:
  14. * - iret frame: Architecture defined interrupt frame from SS to RIP
  15. * at the top of the kernel process stack.
  16. *
  17. * Some macro usage:
  18. * - SYM_FUNC_START/END:Define functions in the symbol table.
  19. * - idtentry: Define exception entry points.
  20. */
  21. #include <linux/export.h>
  22. #include <linux/linkage.h>
  23. #include <asm/segment.h>
  24. #include <asm/cache.h>
  25. #include <asm/errno.h>
  26. #include <asm/asm-offsets.h>
  27. #include <asm/msr.h>
  28. #include <asm/unistd.h>
  29. #include <asm/thread_info.h>
  30. #include <asm/hw_irq.h>
  31. #include <asm/page_types.h>
  32. #include <asm/irqflags.h>
  33. #include <asm/paravirt.h>
  34. #include <asm/percpu.h>
  35. #include <asm/asm.h>
  36. #include <asm/smap.h>
  37. #include <asm/pgtable_types.h>
  38. #include <asm/frame.h>
  39. #include <asm/trapnr.h>
  40. #include <asm/nospec-branch.h>
  41. #include <asm/fsgsbase.h>
  42. #include <linux/err.h>
  43. #include "calling.h"
  44. .code64
  45. .section .entry.text, "ax"
  46. /*
  47. * 64-bit SYSCALL instruction entry. Up to 6 arguments in registers.
  48. *
  49. * This is the only entry point used for 64-bit system calls. The
  50. * hardware interface is reasonably well designed and the register to
  51. * argument mapping Linux uses fits well with the registers that are
  52. * available when SYSCALL is used.
  53. *
  54. * SYSCALL instructions can be found inlined in libc implementations as
  55. * well as some other programs and libraries. There are also a handful
  56. * of SYSCALL instructions in the vDSO used, for example, as a
  57. * clock_gettimeofday fallback.
  58. *
  59. * 64-bit SYSCALL saves rip to rcx, clears rflags.RF, then saves rflags to r11,
  60. * then loads new ss, cs, and rip from previously programmed MSRs.
  61. * rflags gets masked by a value from another MSR (so CLD and CLAC
  62. * are not needed). SYSCALL does not save anything on the stack
  63. * and does not change rsp.
  64. *
  65. * Registers on entry:
  66. * rax system call number
  67. * rcx return address
  68. * r11 saved rflags (note: r11 is callee-clobbered register in C ABI)
  69. * rdi arg0
  70. * rsi arg1
  71. * rdx arg2
  72. * r10 arg3 (needs to be moved to rcx to conform to C ABI)
  73. * r8 arg4
  74. * r9 arg5
  75. * (note: r12-r15, rbp, rbx are callee-preserved in C ABI)
  76. *
  77. * Only called from user space.
  78. *
  79. * When user can change pt_regs->foo always force IRET. That is because
  80. * it deals with uncanonical addresses better. SYSRET has trouble
  81. * with them due to bugs in both AMD and Intel CPUs.
  82. */
  83. SYM_CODE_START(entry_SYSCALL_64)
  84. UNWIND_HINT_ENTRY
  85. ENDBR
  86. swapgs
  87. /* tss.sp2 is scratch space. */
  88. movq %rsp, PER_CPU_VAR(cpu_tss_rw + TSS_sp2)
  89. SWITCH_TO_KERNEL_CR3 scratch_reg=%rsp
  90. movq PER_CPU_VAR(pcpu_hot + X86_top_of_stack), %rsp
  91. SYM_INNER_LABEL(entry_SYSCALL_64_safe_stack, SYM_L_GLOBAL)
  92. ANNOTATE_NOENDBR
  93. /* Construct struct pt_regs on stack */
  94. pushq $__USER_DS /* pt_regs->ss */
  95. pushq PER_CPU_VAR(cpu_tss_rw + TSS_sp2) /* pt_regs->sp */
  96. pushq %r11 /* pt_regs->flags */
  97. pushq $__USER_CS /* pt_regs->cs */
  98. pushq %rcx /* pt_regs->ip */
  99. SYM_INNER_LABEL(entry_SYSCALL_64_after_hwframe, SYM_L_GLOBAL)
  100. pushq %rax /* pt_regs->orig_ax */
  101. PUSH_AND_CLEAR_REGS rax=$-ENOSYS
  102. /* IRQs are off. */
  103. movq %rsp, %rdi
  104. /* Sign extend the lower 32bit as syscall numbers are treated as int */
  105. movslq %eax, %rsi
  106. /* clobbers %rax, make sure it is after saving the syscall nr */
  107. IBRS_ENTER
  108. UNTRAIN_RET
  109. CLEAR_BRANCH_HISTORY
  110. call do_syscall_64 /* returns with IRQs disabled */
  111. /*
  112. * Try to use SYSRET instead of IRET if we're returning to
  113. * a completely clean 64-bit userspace context. If we're not,
  114. * go to the slow exit path.
  115. * In the Xen PV case we must use iret anyway.
  116. */
  117. ALTERNATIVE "testb %al, %al; jz swapgs_restore_regs_and_return_to_usermode", \
  118. "jmp swapgs_restore_regs_and_return_to_usermode", X86_FEATURE_XENPV
  119. /*
  120. * We win! This label is here just for ease of understanding
  121. * perf profiles. Nothing jumps here.
  122. */
  123. syscall_return_via_sysret:
  124. IBRS_EXIT
  125. POP_REGS pop_rdi=0
  126. /*
  127. * Now all regs are restored except RSP and RDI.
  128. * Save old stack pointer and switch to trampoline stack.
  129. */
  130. movq %rsp, %rdi
  131. movq PER_CPU_VAR(cpu_tss_rw + TSS_sp0), %rsp
  132. UNWIND_HINT_END_OF_STACK
  133. pushq RSP-RDI(%rdi) /* RSP */
  134. pushq (%rdi) /* RDI */
  135. /*
  136. * We are on the trampoline stack. All regs except RDI are live.
  137. * We can do future final exit work right here.
  138. */
  139. STACKLEAK_ERASE_NOCLOBBER
  140. SWITCH_TO_USER_CR3_STACK scratch_reg=%rdi
  141. popq %rdi
  142. popq %rsp
  143. SYM_INNER_LABEL(entry_SYSRETQ_unsafe_stack, SYM_L_GLOBAL)
  144. ANNOTATE_NOENDBR
  145. swapgs
  146. CLEAR_CPU_BUFFERS
  147. sysretq
  148. SYM_INNER_LABEL(entry_SYSRETQ_end, SYM_L_GLOBAL)
  149. ANNOTATE_NOENDBR
  150. int3
  151. SYM_CODE_END(entry_SYSCALL_64)
  152. /*
  153. * %rdi: prev task
  154. * %rsi: next task
  155. */
  156. .pushsection .text, "ax"
  157. SYM_FUNC_START(__switch_to_asm)
  158. /*
  159. * Save callee-saved registers
  160. * This must match the order in inactive_task_frame
  161. */
  162. pushq %rbp
  163. pushq %rbx
  164. pushq %r12
  165. pushq %r13
  166. pushq %r14
  167. pushq %r15
  168. /* switch stack */
  169. movq %rsp, TASK_threadsp(%rdi)
  170. movq TASK_threadsp(%rsi), %rsp
  171. #ifdef CONFIG_STACKPROTECTOR
  172. movq TASK_stack_canary(%rsi), %rbx
  173. movq %rbx, PER_CPU_VAR(fixed_percpu_data + FIXED_stack_canary)
  174. #endif
  175. /*
  176. * When switching from a shallower to a deeper call stack
  177. * the RSB may either underflow or use entries populated
  178. * with userspace addresses. On CPUs where those concerns
  179. * exist, overwrite the RSB with entries which capture
  180. * speculative execution to prevent attack.
  181. */
  182. FILL_RETURN_BUFFER %r12, RSB_CLEAR_LOOPS, X86_FEATURE_RSB_CTXSW
  183. /* restore callee-saved registers */
  184. popq %r15
  185. popq %r14
  186. popq %r13
  187. popq %r12
  188. popq %rbx
  189. popq %rbp
  190. jmp __switch_to
  191. SYM_FUNC_END(__switch_to_asm)
  192. .popsection
  193. /*
  194. * A newly forked process directly context switches into this address.
  195. *
  196. * rax: prev task we switched from
  197. * rbx: kernel thread func (NULL for user thread)
  198. * r12: kernel thread arg
  199. */
  200. .pushsection .text, "ax"
  201. SYM_CODE_START(ret_from_fork_asm)
  202. /*
  203. * This is the start of the kernel stack; even through there's a
  204. * register set at the top, the regset isn't necessarily coherent
  205. * (consider kthreads) and one cannot unwind further.
  206. *
  207. * This ensures stack unwinds of kernel threads terminate in a known
  208. * good state.
  209. */
  210. UNWIND_HINT_END_OF_STACK
  211. ANNOTATE_NOENDBR // copy_thread
  212. CALL_DEPTH_ACCOUNT
  213. movq %rax, %rdi /* prev */
  214. movq %rsp, %rsi /* regs */
  215. movq %rbx, %rdx /* fn */
  216. movq %r12, %rcx /* fn_arg */
  217. call ret_from_fork
  218. /*
  219. * Set the stack state to what is expected for the target function
  220. * -- at this point the register set should be a valid user set
  221. * and unwind should work normally.
  222. */
  223. UNWIND_HINT_REGS
  224. #ifdef CONFIG_X86_FRED
  225. ALTERNATIVE "jmp swapgs_restore_regs_and_return_to_usermode", \
  226. "jmp asm_fred_exit_user", X86_FEATURE_FRED
  227. #else
  228. jmp swapgs_restore_regs_and_return_to_usermode
  229. #endif
  230. SYM_CODE_END(ret_from_fork_asm)
  231. .popsection
  232. .macro DEBUG_ENTRY_ASSERT_IRQS_OFF
  233. #ifdef CONFIG_DEBUG_ENTRY
  234. pushq %rax
  235. SAVE_FLAGS
  236. testl $X86_EFLAGS_IF, %eax
  237. jz .Lokay_\@
  238. ud2
  239. .Lokay_\@:
  240. popq %rax
  241. #endif
  242. .endm
  243. SYM_CODE_START(xen_error_entry)
  244. ANNOTATE_NOENDBR
  245. UNWIND_HINT_FUNC
  246. PUSH_AND_CLEAR_REGS save_ret=1
  247. ENCODE_FRAME_POINTER 8
  248. UNTRAIN_RET_FROM_CALL
  249. RET
  250. SYM_CODE_END(xen_error_entry)
  251. /**
  252. * idtentry_body - Macro to emit code calling the C function
  253. * @cfunc: C function to be called
  254. * @has_error_code: Hardware pushed error code on stack
  255. */
  256. .macro idtentry_body cfunc has_error_code:req
  257. /*
  258. * Call error_entry() and switch to the task stack if from userspace.
  259. *
  260. * When in XENPV, it is already in the task stack, and it can't fault
  261. * for native_iret() nor native_load_gs_index() since XENPV uses its
  262. * own pvops for IRET and load_gs_index(). And it doesn't need to
  263. * switch the CR3. So it can skip invoking error_entry().
  264. */
  265. ALTERNATIVE "call error_entry; movq %rax, %rsp", \
  266. "call xen_error_entry", X86_FEATURE_XENPV
  267. ENCODE_FRAME_POINTER
  268. UNWIND_HINT_REGS
  269. movq %rsp, %rdi /* pt_regs pointer into 1st argument*/
  270. .if \has_error_code == 1
  271. movq ORIG_RAX(%rsp), %rsi /* get error code into 2nd argument*/
  272. movq $-1, ORIG_RAX(%rsp) /* no syscall to restart */
  273. .endif
  274. call \cfunc
  275. /* For some configurations \cfunc ends up being a noreturn. */
  276. REACHABLE
  277. jmp error_return
  278. .endm
  279. /**
  280. * idtentry - Macro to generate entry stubs for simple IDT entries
  281. * @vector: Vector number
  282. * @asmsym: ASM symbol for the entry point
  283. * @cfunc: C function to be called
  284. * @has_error_code: Hardware pushed error code on stack
  285. *
  286. * The macro emits code to set up the kernel context for straight forward
  287. * and simple IDT entries. No IST stack, no paranoid entry checks.
  288. */
  289. .macro idtentry vector asmsym cfunc has_error_code:req
  290. SYM_CODE_START(\asmsym)
  291. .if \vector == X86_TRAP_BP
  292. /* #BP advances %rip to the next instruction */
  293. UNWIND_HINT_IRET_ENTRY offset=\has_error_code*8 signal=0
  294. .else
  295. UNWIND_HINT_IRET_ENTRY offset=\has_error_code*8
  296. .endif
  297. ENDBR
  298. ASM_CLAC
  299. cld
  300. .if \has_error_code == 0
  301. pushq $-1 /* ORIG_RAX: no syscall to restart */
  302. .endif
  303. .if \vector == X86_TRAP_BP
  304. /*
  305. * If coming from kernel space, create a 6-word gap to allow the
  306. * int3 handler to emulate a call instruction.
  307. */
  308. testb $3, CS-ORIG_RAX(%rsp)
  309. jnz .Lfrom_usermode_no_gap_\@
  310. .rept 6
  311. pushq 5*8(%rsp)
  312. .endr
  313. UNWIND_HINT_IRET_REGS offset=8
  314. .Lfrom_usermode_no_gap_\@:
  315. .endif
  316. idtentry_body \cfunc \has_error_code
  317. _ASM_NOKPROBE(\asmsym)
  318. SYM_CODE_END(\asmsym)
  319. .endm
  320. /*
  321. * Interrupt entry/exit.
  322. *
  323. + The interrupt stubs push (vector) onto the stack, which is the error_code
  324. * position of idtentry exceptions, and jump to one of the two idtentry points
  325. * (common/spurious).
  326. *
  327. * common_interrupt is a hotpath, align it to a cache line
  328. */
  329. .macro idtentry_irq vector cfunc
  330. .p2align CONFIG_X86_L1_CACHE_SHIFT
  331. idtentry \vector asm_\cfunc \cfunc has_error_code=1
  332. .endm
  333. /**
  334. * idtentry_mce_db - Macro to generate entry stubs for #MC and #DB
  335. * @vector: Vector number
  336. * @asmsym: ASM symbol for the entry point
  337. * @cfunc: C function to be called
  338. *
  339. * The macro emits code to set up the kernel context for #MC and #DB
  340. *
  341. * If the entry comes from user space it uses the normal entry path
  342. * including the return to user space work and preemption checks on
  343. * exit.
  344. *
  345. * If hits in kernel mode then it needs to go through the paranoid
  346. * entry as the exception can hit any random state. No preemption
  347. * check on exit to keep the paranoid path simple.
  348. */
  349. .macro idtentry_mce_db vector asmsym cfunc
  350. SYM_CODE_START(\asmsym)
  351. UNWIND_HINT_IRET_ENTRY
  352. ENDBR
  353. ASM_CLAC
  354. cld
  355. pushq $-1 /* ORIG_RAX: no syscall to restart */
  356. /*
  357. * If the entry is from userspace, switch stacks and treat it as
  358. * a normal entry.
  359. */
  360. testb $3, CS-ORIG_RAX(%rsp)
  361. jnz .Lfrom_usermode_switch_stack_\@
  362. /* paranoid_entry returns GS information for paranoid_exit in EBX. */
  363. call paranoid_entry
  364. UNWIND_HINT_REGS
  365. movq %rsp, %rdi /* pt_regs pointer */
  366. call \cfunc
  367. jmp paranoid_exit
  368. /* Switch to the regular task stack and use the noist entry point */
  369. .Lfrom_usermode_switch_stack_\@:
  370. idtentry_body noist_\cfunc, has_error_code=0
  371. _ASM_NOKPROBE(\asmsym)
  372. SYM_CODE_END(\asmsym)
  373. .endm
  374. #ifdef CONFIG_AMD_MEM_ENCRYPT
  375. /**
  376. * idtentry_vc - Macro to generate entry stub for #VC
  377. * @vector: Vector number
  378. * @asmsym: ASM symbol for the entry point
  379. * @cfunc: C function to be called
  380. *
  381. * The macro emits code to set up the kernel context for #VC. The #VC handler
  382. * runs on an IST stack and needs to be able to cause nested #VC exceptions.
  383. *
  384. * To make this work the #VC entry code tries its best to pretend it doesn't use
  385. * an IST stack by switching to the task stack if coming from user-space (which
  386. * includes early SYSCALL entry path) or back to the stack in the IRET frame if
  387. * entered from kernel-mode.
  388. *
  389. * If entered from kernel-mode the return stack is validated first, and if it is
  390. * not safe to use (e.g. because it points to the entry stack) the #VC handler
  391. * will switch to a fall-back stack (VC2) and call a special handler function.
  392. *
  393. * The macro is only used for one vector, but it is planned to be extended in
  394. * the future for the #HV exception.
  395. */
  396. .macro idtentry_vc vector asmsym cfunc
  397. SYM_CODE_START(\asmsym)
  398. UNWIND_HINT_IRET_ENTRY
  399. ENDBR
  400. ASM_CLAC
  401. cld
  402. /*
  403. * If the entry is from userspace, switch stacks and treat it as
  404. * a normal entry.
  405. */
  406. testb $3, CS-ORIG_RAX(%rsp)
  407. jnz .Lfrom_usermode_switch_stack_\@
  408. /*
  409. * paranoid_entry returns SWAPGS flag for paranoid_exit in EBX.
  410. * EBX == 0 -> SWAPGS, EBX == 1 -> no SWAPGS
  411. */
  412. call paranoid_entry
  413. UNWIND_HINT_REGS
  414. /*
  415. * Switch off the IST stack to make it free for nested exceptions. The
  416. * vc_switch_off_ist() function will switch back to the interrupted
  417. * stack if it is safe to do so. If not it switches to the VC fall-back
  418. * stack.
  419. */
  420. movq %rsp, %rdi /* pt_regs pointer */
  421. call vc_switch_off_ist
  422. movq %rax, %rsp /* Switch to new stack */
  423. ENCODE_FRAME_POINTER
  424. UNWIND_HINT_REGS
  425. /* Update pt_regs */
  426. movq ORIG_RAX(%rsp), %rsi /* get error code into 2nd argument*/
  427. movq $-1, ORIG_RAX(%rsp) /* no syscall to restart */
  428. movq %rsp, %rdi /* pt_regs pointer */
  429. call kernel_\cfunc
  430. /*
  431. * No need to switch back to the IST stack. The current stack is either
  432. * identical to the stack in the IRET frame or the VC fall-back stack,
  433. * so it is definitely mapped even with PTI enabled.
  434. */
  435. jmp paranoid_exit
  436. /* Switch to the regular task stack */
  437. .Lfrom_usermode_switch_stack_\@:
  438. idtentry_body user_\cfunc, has_error_code=1
  439. _ASM_NOKPROBE(\asmsym)
  440. SYM_CODE_END(\asmsym)
  441. .endm
  442. #endif
  443. /*
  444. * Double fault entry. Straight paranoid. No checks from which context
  445. * this comes because for the espfix induced #DF this would do the wrong
  446. * thing.
  447. */
  448. .macro idtentry_df vector asmsym cfunc
  449. SYM_CODE_START(\asmsym)
  450. UNWIND_HINT_IRET_ENTRY offset=8
  451. ENDBR
  452. ASM_CLAC
  453. cld
  454. /* paranoid_entry returns GS information for paranoid_exit in EBX. */
  455. call paranoid_entry
  456. UNWIND_HINT_REGS
  457. movq %rsp, %rdi /* pt_regs pointer into first argument */
  458. movq ORIG_RAX(%rsp), %rsi /* get error code into 2nd argument*/
  459. movq $-1, ORIG_RAX(%rsp) /* no syscall to restart */
  460. call \cfunc
  461. /* For some configurations \cfunc ends up being a noreturn. */
  462. REACHABLE
  463. jmp paranoid_exit
  464. _ASM_NOKPROBE(\asmsym)
  465. SYM_CODE_END(\asmsym)
  466. .endm
  467. /*
  468. * Include the defines which emit the idt entries which are shared
  469. * shared between 32 and 64 bit and emit the __irqentry_text_* markers
  470. * so the stacktrace boundary checks work.
  471. */
  472. __ALIGN
  473. .globl __irqentry_text_start
  474. __irqentry_text_start:
  475. #include <asm/idtentry.h>
  476. __ALIGN
  477. .globl __irqentry_text_end
  478. __irqentry_text_end:
  479. ANNOTATE_NOENDBR
  480. SYM_CODE_START_LOCAL(common_interrupt_return)
  481. SYM_INNER_LABEL(swapgs_restore_regs_and_return_to_usermode, SYM_L_GLOBAL)
  482. IBRS_EXIT
  483. #ifdef CONFIG_XEN_PV
  484. ALTERNATIVE "", "jmp xenpv_restore_regs_and_return_to_usermode", X86_FEATURE_XENPV
  485. #endif
  486. #ifdef CONFIG_MITIGATION_PAGE_TABLE_ISOLATION
  487. ALTERNATIVE "", "jmp .Lpti_restore_regs_and_return_to_usermode", X86_FEATURE_PTI
  488. #endif
  489. STACKLEAK_ERASE
  490. POP_REGS
  491. add $8, %rsp /* orig_ax */
  492. UNWIND_HINT_IRET_REGS
  493. .Lswapgs_and_iret:
  494. swapgs
  495. CLEAR_CPU_BUFFERS
  496. /* Assert that the IRET frame indicates user mode. */
  497. testb $3, 8(%rsp)
  498. jnz .Lnative_iret
  499. ud2
  500. #ifdef CONFIG_MITIGATION_PAGE_TABLE_ISOLATION
  501. .Lpti_restore_regs_and_return_to_usermode:
  502. POP_REGS pop_rdi=0
  503. /*
  504. * The stack is now user RDI, orig_ax, RIP, CS, EFLAGS, RSP, SS.
  505. * Save old stack pointer and switch to trampoline stack.
  506. */
  507. movq %rsp, %rdi
  508. movq PER_CPU_VAR(cpu_tss_rw + TSS_sp0), %rsp
  509. UNWIND_HINT_END_OF_STACK
  510. /* Copy the IRET frame to the trampoline stack. */
  511. pushq 6*8(%rdi) /* SS */
  512. pushq 5*8(%rdi) /* RSP */
  513. pushq 4*8(%rdi) /* EFLAGS */
  514. pushq 3*8(%rdi) /* CS */
  515. pushq 2*8(%rdi) /* RIP */
  516. /* Push user RDI on the trampoline stack. */
  517. pushq (%rdi)
  518. /*
  519. * We are on the trampoline stack. All regs except RDI are live.
  520. * We can do future final exit work right here.
  521. */
  522. STACKLEAK_ERASE_NOCLOBBER
  523. push %rax
  524. SWITCH_TO_USER_CR3 scratch_reg=%rdi scratch_reg2=%rax
  525. pop %rax
  526. /* Restore RDI. */
  527. popq %rdi
  528. jmp .Lswapgs_and_iret
  529. #endif
  530. SYM_INNER_LABEL(restore_regs_and_return_to_kernel, SYM_L_GLOBAL)
  531. #ifdef CONFIG_DEBUG_ENTRY
  532. /* Assert that pt_regs indicates kernel mode. */
  533. testb $3, CS(%rsp)
  534. jz 1f
  535. ud2
  536. 1:
  537. #endif
  538. POP_REGS
  539. addq $8, %rsp /* skip regs->orig_ax */
  540. /*
  541. * ARCH_HAS_MEMBARRIER_SYNC_CORE rely on IRET core serialization
  542. * when returning from IPI handler.
  543. */
  544. #ifdef CONFIG_XEN_PV
  545. SYM_INNER_LABEL(early_xen_iret_patch, SYM_L_GLOBAL)
  546. ANNOTATE_NOENDBR
  547. .byte 0xe9
  548. .long .Lnative_iret - (. + 4)
  549. #endif
  550. .Lnative_iret:
  551. UNWIND_HINT_IRET_REGS
  552. /*
  553. * Are we returning to a stack segment from the LDT? Note: in
  554. * 64-bit mode SS:RSP on the exception stack is always valid.
  555. */
  556. #ifdef CONFIG_X86_ESPFIX64
  557. testb $4, (SS-RIP)(%rsp)
  558. jnz native_irq_return_ldt
  559. #endif
  560. SYM_INNER_LABEL(native_irq_return_iret, SYM_L_GLOBAL)
  561. ANNOTATE_NOENDBR // exc_double_fault
  562. /*
  563. * This may fault. Non-paranoid faults on return to userspace are
  564. * handled by fixup_bad_iret. These include #SS, #GP, and #NP.
  565. * Double-faults due to espfix64 are handled in exc_double_fault.
  566. * Other faults here are fatal.
  567. */
  568. iretq
  569. #ifdef CONFIG_X86_ESPFIX64
  570. native_irq_return_ldt:
  571. /*
  572. * We are running with user GSBASE. All GPRs contain their user
  573. * values. We have a percpu ESPFIX stack that is eight slots
  574. * long (see ESPFIX_STACK_SIZE). espfix_waddr points to the bottom
  575. * of the ESPFIX stack.
  576. *
  577. * We clobber RAX and RDI in this code. We stash RDI on the
  578. * normal stack and RAX on the ESPFIX stack.
  579. *
  580. * The ESPFIX stack layout we set up looks like this:
  581. *
  582. * --- top of ESPFIX stack ---
  583. * SS
  584. * RSP
  585. * RFLAGS
  586. * CS
  587. * RIP <-- RSP points here when we're done
  588. * RAX <-- espfix_waddr points here
  589. * --- bottom of ESPFIX stack ---
  590. */
  591. pushq %rdi /* Stash user RDI */
  592. swapgs /* to kernel GS */
  593. SWITCH_TO_KERNEL_CR3 scratch_reg=%rdi /* to kernel CR3 */
  594. movq PER_CPU_VAR(espfix_waddr), %rdi
  595. movq %rax, (0*8)(%rdi) /* user RAX */
  596. movq (1*8)(%rsp), %rax /* user RIP */
  597. movq %rax, (1*8)(%rdi)
  598. movq (2*8)(%rsp), %rax /* user CS */
  599. movq %rax, (2*8)(%rdi)
  600. movq (3*8)(%rsp), %rax /* user RFLAGS */
  601. movq %rax, (3*8)(%rdi)
  602. movq (5*8)(%rsp), %rax /* user SS */
  603. movq %rax, (5*8)(%rdi)
  604. movq (4*8)(%rsp), %rax /* user RSP */
  605. movq %rax, (4*8)(%rdi)
  606. /* Now RAX == RSP. */
  607. andl $0xffff0000, %eax /* RAX = (RSP & 0xffff0000) */
  608. /*
  609. * espfix_stack[31:16] == 0. The page tables are set up such that
  610. * (espfix_stack | (X & 0xffff0000)) points to a read-only alias of
  611. * espfix_waddr for any X. That is, there are 65536 RO aliases of
  612. * the same page. Set up RSP so that RSP[31:16] contains the
  613. * respective 16 bits of the /userspace/ RSP and RSP nonetheless
  614. * still points to an RO alias of the ESPFIX stack.
  615. */
  616. orq PER_CPU_VAR(espfix_stack), %rax
  617. SWITCH_TO_USER_CR3_STACK scratch_reg=%rdi
  618. swapgs /* to user GS */
  619. popq %rdi /* Restore user RDI */
  620. movq %rax, %rsp
  621. UNWIND_HINT_IRET_REGS offset=8
  622. /*
  623. * At this point, we cannot write to the stack any more, but we can
  624. * still read.
  625. */
  626. popq %rax /* Restore user RAX */
  627. CLEAR_CPU_BUFFERS
  628. /*
  629. * RSP now points to an ordinary IRET frame, except that the page
  630. * is read-only and RSP[31:16] are preloaded with the userspace
  631. * values. We can now IRET back to userspace.
  632. */
  633. jmp native_irq_return_iret
  634. #endif
  635. SYM_CODE_END(common_interrupt_return)
  636. _ASM_NOKPROBE(common_interrupt_return)
  637. /*
  638. * Reload gs selector with exception handling
  639. * di: new selector
  640. *
  641. * Is in entry.text as it shouldn't be instrumented.
  642. */
  643. SYM_FUNC_START(asm_load_gs_index)
  644. FRAME_BEGIN
  645. swapgs
  646. .Lgs_change:
  647. ANNOTATE_NOENDBR // error_entry
  648. movl %edi, %gs
  649. 2: ALTERNATIVE "", "mfence", X86_BUG_SWAPGS_FENCE
  650. swapgs
  651. FRAME_END
  652. RET
  653. /* running with kernelgs */
  654. .Lbad_gs:
  655. swapgs /* switch back to user gs */
  656. .macro ZAP_GS
  657. /* This can't be a string because the preprocessor needs to see it. */
  658. movl $__USER_DS, %eax
  659. movl %eax, %gs
  660. .endm
  661. ALTERNATIVE "", "ZAP_GS", X86_BUG_NULL_SEG
  662. xorl %eax, %eax
  663. movl %eax, %gs
  664. jmp 2b
  665. _ASM_EXTABLE(.Lgs_change, .Lbad_gs)
  666. SYM_FUNC_END(asm_load_gs_index)
  667. EXPORT_SYMBOL(asm_load_gs_index)
  668. #ifdef CONFIG_XEN_PV
  669. /*
  670. * A note on the "critical region" in our callback handler.
  671. * We want to avoid stacking callback handlers due to events occurring
  672. * during handling of the last event. To do this, we keep events disabled
  673. * until we've done all processing. HOWEVER, we must enable events before
  674. * popping the stack frame (can't be done atomically) and so it would still
  675. * be possible to get enough handler activations to overflow the stack.
  676. * Although unlikely, bugs of that kind are hard to track down, so we'd
  677. * like to avoid the possibility.
  678. * So, on entry to the handler we detect whether we interrupted an
  679. * existing activation in its critical region -- if so, we pop the current
  680. * activation and restart the handler using the previous one.
  681. *
  682. * C calling convention: exc_xen_hypervisor_callback(struct *pt_regs)
  683. */
  684. __FUNC_ALIGN
  685. SYM_CODE_START_LOCAL_NOALIGN(exc_xen_hypervisor_callback)
  686. /*
  687. * Since we don't modify %rdi, evtchn_do_upall(struct *pt_regs) will
  688. * see the correct pointer to the pt_regs
  689. */
  690. UNWIND_HINT_FUNC
  691. movq %rdi, %rsp /* we don't return, adjust the stack frame */
  692. UNWIND_HINT_REGS
  693. call xen_pv_evtchn_do_upcall
  694. jmp error_return
  695. SYM_CODE_END(exc_xen_hypervisor_callback)
  696. /*
  697. * Hypervisor uses this for application faults while it executes.
  698. * We get here for two reasons:
  699. * 1. Fault while reloading DS, ES, FS or GS
  700. * 2. Fault while executing IRET
  701. * Category 1 we do not need to fix up as Xen has already reloaded all segment
  702. * registers that could be reloaded and zeroed the others.
  703. * Category 2 we fix up by killing the current process. We cannot use the
  704. * normal Linux return path in this case because if we use the IRET hypercall
  705. * to pop the stack frame we end up in an infinite loop of failsafe callbacks.
  706. * We distinguish between categories by comparing each saved segment register
  707. * with its current contents: any discrepancy means we in category 1.
  708. */
  709. __FUNC_ALIGN
  710. SYM_CODE_START_NOALIGN(xen_failsafe_callback)
  711. UNWIND_HINT_UNDEFINED
  712. ENDBR
  713. movl %ds, %ecx
  714. cmpw %cx, 0x10(%rsp)
  715. jne 1f
  716. movl %es, %ecx
  717. cmpw %cx, 0x18(%rsp)
  718. jne 1f
  719. movl %fs, %ecx
  720. cmpw %cx, 0x20(%rsp)
  721. jne 1f
  722. movl %gs, %ecx
  723. cmpw %cx, 0x28(%rsp)
  724. jne 1f
  725. /* All segments match their saved values => Category 2 (Bad IRET). */
  726. movq (%rsp), %rcx
  727. movq 8(%rsp), %r11
  728. addq $0x30, %rsp
  729. pushq $0 /* RIP */
  730. UNWIND_HINT_IRET_REGS offset=8
  731. jmp asm_exc_general_protection
  732. 1: /* Segment mismatch => Category 1 (Bad segment). Retry the IRET. */
  733. movq (%rsp), %rcx
  734. movq 8(%rsp), %r11
  735. addq $0x30, %rsp
  736. UNWIND_HINT_IRET_REGS
  737. pushq $-1 /* orig_ax = -1 => not a system call */
  738. PUSH_AND_CLEAR_REGS
  739. ENCODE_FRAME_POINTER
  740. jmp error_return
  741. SYM_CODE_END(xen_failsafe_callback)
  742. #endif /* CONFIG_XEN_PV */
  743. /*
  744. * Save all registers in pt_regs. Return GSBASE related information
  745. * in EBX depending on the availability of the FSGSBASE instructions:
  746. *
  747. * FSGSBASE R/EBX
  748. * N 0 -> SWAPGS on exit
  749. * 1 -> no SWAPGS on exit
  750. *
  751. * Y GSBASE value at entry, must be restored in paranoid_exit
  752. *
  753. * R14 - old CR3
  754. * R15 - old SPEC_CTRL
  755. */
  756. SYM_CODE_START(paranoid_entry)
  757. ANNOTATE_NOENDBR
  758. UNWIND_HINT_FUNC
  759. PUSH_AND_CLEAR_REGS save_ret=1
  760. ENCODE_FRAME_POINTER 8
  761. /*
  762. * Always stash CR3 in %r14. This value will be restored,
  763. * verbatim, at exit. Needed if paranoid_entry interrupted
  764. * another entry that already switched to the user CR3 value
  765. * but has not yet returned to userspace.
  766. *
  767. * This is also why CS (stashed in the "iret frame" by the
  768. * hardware at entry) can not be used: this may be a return
  769. * to kernel code, but with a user CR3 value.
  770. *
  771. * Switching CR3 does not depend on kernel GSBASE so it can
  772. * be done before switching to the kernel GSBASE. This is
  773. * required for FSGSBASE because the kernel GSBASE has to
  774. * be retrieved from a kernel internal table.
  775. */
  776. SAVE_AND_SWITCH_TO_KERNEL_CR3 scratch_reg=%rax save_reg=%r14
  777. /*
  778. * Handling GSBASE depends on the availability of FSGSBASE.
  779. *
  780. * Without FSGSBASE the kernel enforces that negative GSBASE
  781. * values indicate kernel GSBASE. With FSGSBASE no assumptions
  782. * can be made about the GSBASE value when entering from user
  783. * space.
  784. */
  785. ALTERNATIVE "jmp .Lparanoid_entry_checkgs", "", X86_FEATURE_FSGSBASE
  786. /*
  787. * Read the current GSBASE and store it in %rbx unconditionally,
  788. * retrieve and set the current CPUs kernel GSBASE. The stored value
  789. * has to be restored in paranoid_exit unconditionally.
  790. *
  791. * The unconditional write to GS base below ensures that no subsequent
  792. * loads based on a mispredicted GS base can happen, therefore no LFENCE
  793. * is needed here.
  794. */
  795. SAVE_AND_SET_GSBASE scratch_reg=%rax save_reg=%rbx
  796. jmp .Lparanoid_gsbase_done
  797. .Lparanoid_entry_checkgs:
  798. /* EBX = 1 -> kernel GSBASE active, no restore required */
  799. movl $1, %ebx
  800. /*
  801. * The kernel-enforced convention is a negative GSBASE indicates
  802. * a kernel value. No SWAPGS needed on entry and exit.
  803. */
  804. movl $MSR_GS_BASE, %ecx
  805. rdmsr
  806. testl %edx, %edx
  807. js .Lparanoid_kernel_gsbase
  808. /* EBX = 0 -> SWAPGS required on exit */
  809. xorl %ebx, %ebx
  810. swapgs
  811. .Lparanoid_kernel_gsbase:
  812. FENCE_SWAPGS_KERNEL_ENTRY
  813. .Lparanoid_gsbase_done:
  814. /*
  815. * Once we have CR3 and %GS setup save and set SPEC_CTRL. Just like
  816. * CR3 above, keep the old value in a callee saved register.
  817. */
  818. IBRS_ENTER save_reg=%r15
  819. UNTRAIN_RET_FROM_CALL
  820. RET
  821. SYM_CODE_END(paranoid_entry)
  822. /*
  823. * "Paranoid" exit path from exception stack. This is invoked
  824. * only on return from non-NMI IST interrupts that came
  825. * from kernel space.
  826. *
  827. * We may be returning to very strange contexts (e.g. very early
  828. * in syscall entry), so checking for preemption here would
  829. * be complicated. Fortunately, there's no good reason to try
  830. * to handle preemption here.
  831. *
  832. * R/EBX contains the GSBASE related information depending on the
  833. * availability of the FSGSBASE instructions:
  834. *
  835. * FSGSBASE R/EBX
  836. * N 0 -> SWAPGS on exit
  837. * 1 -> no SWAPGS on exit
  838. *
  839. * Y User space GSBASE, must be restored unconditionally
  840. *
  841. * R14 - old CR3
  842. * R15 - old SPEC_CTRL
  843. */
  844. SYM_CODE_START_LOCAL(paranoid_exit)
  845. UNWIND_HINT_REGS
  846. /*
  847. * Must restore IBRS state before both CR3 and %GS since we need access
  848. * to the per-CPU x86_spec_ctrl_shadow variable.
  849. */
  850. IBRS_EXIT save_reg=%r15
  851. /*
  852. * The order of operations is important. PARANOID_RESTORE_CR3 requires
  853. * kernel GSBASE.
  854. *
  855. * NB to anyone to try to optimize this code: this code does
  856. * not execute at all for exceptions from user mode. Those
  857. * exceptions go through error_return instead.
  858. */
  859. PARANOID_RESTORE_CR3 scratch_reg=%rax save_reg=%r14
  860. /* Handle the three GSBASE cases */
  861. ALTERNATIVE "jmp .Lparanoid_exit_checkgs", "", X86_FEATURE_FSGSBASE
  862. /* With FSGSBASE enabled, unconditionally restore GSBASE */
  863. wrgsbase %rbx
  864. jmp restore_regs_and_return_to_kernel
  865. .Lparanoid_exit_checkgs:
  866. /* On non-FSGSBASE systems, conditionally do SWAPGS */
  867. testl %ebx, %ebx
  868. jnz restore_regs_and_return_to_kernel
  869. /* We are returning to a context with user GSBASE */
  870. swapgs
  871. jmp restore_regs_and_return_to_kernel
  872. SYM_CODE_END(paranoid_exit)
  873. /*
  874. * Switch GS and CR3 if needed.
  875. */
  876. SYM_CODE_START(error_entry)
  877. ANNOTATE_NOENDBR
  878. UNWIND_HINT_FUNC
  879. PUSH_AND_CLEAR_REGS save_ret=1
  880. ENCODE_FRAME_POINTER 8
  881. testb $3, CS+8(%rsp)
  882. jz .Lerror_kernelspace
  883. /*
  884. * We entered from user mode or we're pretending to have entered
  885. * from user mode due to an IRET fault.
  886. */
  887. swapgs
  888. FENCE_SWAPGS_USER_ENTRY
  889. /* We have user CR3. Change to kernel CR3. */
  890. SWITCH_TO_KERNEL_CR3 scratch_reg=%rax
  891. IBRS_ENTER
  892. UNTRAIN_RET_FROM_CALL
  893. leaq 8(%rsp), %rdi /* arg0 = pt_regs pointer */
  894. /* Put us onto the real thread stack. */
  895. jmp sync_regs
  896. /*
  897. * There are two places in the kernel that can potentially fault with
  898. * usergs. Handle them here. B stepping K8s sometimes report a
  899. * truncated RIP for IRET exceptions returning to compat mode. Check
  900. * for these here too.
  901. */
  902. .Lerror_kernelspace:
  903. leaq native_irq_return_iret(%rip), %rcx
  904. cmpq %rcx, RIP+8(%rsp)
  905. je .Lerror_bad_iret
  906. movl %ecx, %eax /* zero extend */
  907. cmpq %rax, RIP+8(%rsp)
  908. je .Lbstep_iret
  909. cmpq $.Lgs_change, RIP+8(%rsp)
  910. jne .Lerror_entry_done_lfence
  911. /*
  912. * hack: .Lgs_change can fail with user gsbase. If this happens, fix up
  913. * gsbase and proceed. We'll fix up the exception and land in
  914. * .Lgs_change's error handler with kernel gsbase.
  915. */
  916. swapgs
  917. /*
  918. * Issue an LFENCE to prevent GS speculation, regardless of whether it is a
  919. * kernel or user gsbase.
  920. */
  921. .Lerror_entry_done_lfence:
  922. FENCE_SWAPGS_KERNEL_ENTRY
  923. CALL_DEPTH_ACCOUNT
  924. leaq 8(%rsp), %rax /* return pt_regs pointer */
  925. VALIDATE_UNRET_END
  926. RET
  927. .Lbstep_iret:
  928. /* Fix truncated RIP */
  929. movq %rcx, RIP+8(%rsp)
  930. /* fall through */
  931. .Lerror_bad_iret:
  932. /*
  933. * We came from an IRET to user mode, so we have user
  934. * gsbase and CR3. Switch to kernel gsbase and CR3:
  935. */
  936. swapgs
  937. FENCE_SWAPGS_USER_ENTRY
  938. SWITCH_TO_KERNEL_CR3 scratch_reg=%rax
  939. IBRS_ENTER
  940. UNTRAIN_RET_FROM_CALL
  941. /*
  942. * Pretend that the exception came from user mode: set up pt_regs
  943. * as if we faulted immediately after IRET.
  944. */
  945. leaq 8(%rsp), %rdi /* arg0 = pt_regs pointer */
  946. call fixup_bad_iret
  947. mov %rax, %rdi
  948. jmp sync_regs
  949. SYM_CODE_END(error_entry)
  950. SYM_CODE_START_LOCAL(error_return)
  951. UNWIND_HINT_REGS
  952. DEBUG_ENTRY_ASSERT_IRQS_OFF
  953. testb $3, CS(%rsp)
  954. jz restore_regs_and_return_to_kernel
  955. jmp swapgs_restore_regs_and_return_to_usermode
  956. SYM_CODE_END(error_return)
  957. /*
  958. * Runs on exception stack. Xen PV does not go through this path at all,
  959. * so we can use real assembly here.
  960. *
  961. * Registers:
  962. * %r14: Used to save/restore the CR3 of the interrupted context
  963. * when MITIGATION_PAGE_TABLE_ISOLATION is in use. Do not clobber.
  964. */
  965. SYM_CODE_START(asm_exc_nmi)
  966. UNWIND_HINT_IRET_ENTRY
  967. ENDBR
  968. /*
  969. * We allow breakpoints in NMIs. If a breakpoint occurs, then
  970. * the iretq it performs will take us out of NMI context.
  971. * This means that we can have nested NMIs where the next
  972. * NMI is using the top of the stack of the previous NMI. We
  973. * can't let it execute because the nested NMI will corrupt the
  974. * stack of the previous NMI. NMI handlers are not re-entrant
  975. * anyway.
  976. *
  977. * To handle this case we do the following:
  978. * Check a special location on the stack that contains a
  979. * variable that is set when NMIs are executing.
  980. * The interrupted task's stack is also checked to see if it
  981. * is an NMI stack.
  982. * If the variable is not set and the stack is not the NMI
  983. * stack then:
  984. * o Set the special variable on the stack
  985. * o Copy the interrupt frame into an "outermost" location on the
  986. * stack
  987. * o Copy the interrupt frame into an "iret" location on the stack
  988. * o Continue processing the NMI
  989. * If the variable is set or the previous stack is the NMI stack:
  990. * o Modify the "iret" location to jump to the repeat_nmi
  991. * o return back to the first NMI
  992. *
  993. * Now on exit of the first NMI, we first clear the stack variable
  994. * The NMI stack will tell any nested NMIs at that point that it is
  995. * nested. Then we pop the stack normally with iret, and if there was
  996. * a nested NMI that updated the copy interrupt stack frame, a
  997. * jump will be made to the repeat_nmi code that will handle the second
  998. * NMI.
  999. *
  1000. * However, espfix prevents us from directly returning to userspace
  1001. * with a single IRET instruction. Similarly, IRET to user mode
  1002. * can fault. We therefore handle NMIs from user space like
  1003. * other IST entries.
  1004. */
  1005. ASM_CLAC
  1006. cld
  1007. /* Use %rdx as our temp variable throughout */
  1008. pushq %rdx
  1009. testb $3, CS-RIP+8(%rsp)
  1010. jz .Lnmi_from_kernel
  1011. /*
  1012. * NMI from user mode. We need to run on the thread stack, but we
  1013. * can't go through the normal entry paths: NMIs are masked, and
  1014. * we don't want to enable interrupts, because then we'll end
  1015. * up in an awkward situation in which IRQs are on but NMIs
  1016. * are off.
  1017. *
  1018. * We also must not push anything to the stack before switching
  1019. * stacks lest we corrupt the "NMI executing" variable.
  1020. */
  1021. swapgs
  1022. FENCE_SWAPGS_USER_ENTRY
  1023. SWITCH_TO_KERNEL_CR3 scratch_reg=%rdx
  1024. movq %rsp, %rdx
  1025. movq PER_CPU_VAR(pcpu_hot + X86_top_of_stack), %rsp
  1026. UNWIND_HINT_IRET_REGS base=%rdx offset=8
  1027. pushq 5*8(%rdx) /* pt_regs->ss */
  1028. pushq 4*8(%rdx) /* pt_regs->rsp */
  1029. pushq 3*8(%rdx) /* pt_regs->flags */
  1030. pushq 2*8(%rdx) /* pt_regs->cs */
  1031. pushq 1*8(%rdx) /* pt_regs->rip */
  1032. UNWIND_HINT_IRET_REGS
  1033. pushq $-1 /* pt_regs->orig_ax */
  1034. PUSH_AND_CLEAR_REGS rdx=(%rdx)
  1035. ENCODE_FRAME_POINTER
  1036. IBRS_ENTER
  1037. UNTRAIN_RET
  1038. /*
  1039. * At this point we no longer need to worry about stack damage
  1040. * due to nesting -- we're on the normal thread stack and we're
  1041. * done with the NMI stack.
  1042. */
  1043. movq %rsp, %rdi
  1044. call exc_nmi
  1045. /*
  1046. * Return back to user mode. We must *not* do the normal exit
  1047. * work, because we don't want to enable interrupts.
  1048. */
  1049. jmp swapgs_restore_regs_and_return_to_usermode
  1050. .Lnmi_from_kernel:
  1051. /*
  1052. * Here's what our stack frame will look like:
  1053. * +---------------------------------------------------------+
  1054. * | original SS |
  1055. * | original Return RSP |
  1056. * | original RFLAGS |
  1057. * | original CS |
  1058. * | original RIP |
  1059. * +---------------------------------------------------------+
  1060. * | temp storage for rdx |
  1061. * +---------------------------------------------------------+
  1062. * | "NMI executing" variable |
  1063. * +---------------------------------------------------------+
  1064. * | iret SS } Copied from "outermost" frame |
  1065. * | iret Return RSP } on each loop iteration; overwritten |
  1066. * | iret RFLAGS } by a nested NMI to force another |
  1067. * | iret CS } iteration if needed. |
  1068. * | iret RIP } |
  1069. * +---------------------------------------------------------+
  1070. * | outermost SS } initialized in first_nmi; |
  1071. * | outermost Return RSP } will not be changed before |
  1072. * | outermost RFLAGS } NMI processing is done. |
  1073. * | outermost CS } Copied to "iret" frame on each |
  1074. * | outermost RIP } iteration. |
  1075. * +---------------------------------------------------------+
  1076. * | pt_regs |
  1077. * +---------------------------------------------------------+
  1078. *
  1079. * The "original" frame is used by hardware. Before re-enabling
  1080. * NMIs, we need to be done with it, and we need to leave enough
  1081. * space for the asm code here.
  1082. *
  1083. * We return by executing IRET while RSP points to the "iret" frame.
  1084. * That will either return for real or it will loop back into NMI
  1085. * processing.
  1086. *
  1087. * The "outermost" frame is copied to the "iret" frame on each
  1088. * iteration of the loop, so each iteration starts with the "iret"
  1089. * frame pointing to the final return target.
  1090. */
  1091. /*
  1092. * Determine whether we're a nested NMI.
  1093. *
  1094. * If we interrupted kernel code between repeat_nmi and
  1095. * end_repeat_nmi, then we are a nested NMI. We must not
  1096. * modify the "iret" frame because it's being written by
  1097. * the outer NMI. That's okay; the outer NMI handler is
  1098. * about to call exc_nmi() anyway, so we can just resume
  1099. * the outer NMI.
  1100. */
  1101. movq $repeat_nmi, %rdx
  1102. cmpq 8(%rsp), %rdx
  1103. ja 1f
  1104. movq $end_repeat_nmi, %rdx
  1105. cmpq 8(%rsp), %rdx
  1106. ja nested_nmi_out
  1107. 1:
  1108. /*
  1109. * Now check "NMI executing". If it's set, then we're nested.
  1110. * This will not detect if we interrupted an outer NMI just
  1111. * before IRET.
  1112. */
  1113. cmpl $1, -8(%rsp)
  1114. je nested_nmi
  1115. /*
  1116. * Now test if the previous stack was an NMI stack. This covers
  1117. * the case where we interrupt an outer NMI after it clears
  1118. * "NMI executing" but before IRET. We need to be careful, though:
  1119. * there is one case in which RSP could point to the NMI stack
  1120. * despite there being no NMI active: naughty userspace controls
  1121. * RSP at the very beginning of the SYSCALL targets. We can
  1122. * pull a fast one on naughty userspace, though: we program
  1123. * SYSCALL to mask DF, so userspace cannot cause DF to be set
  1124. * if it controls the kernel's RSP. We set DF before we clear
  1125. * "NMI executing".
  1126. */
  1127. lea 6*8(%rsp), %rdx
  1128. /* Compare the NMI stack (rdx) with the stack we came from (4*8(%rsp)) */
  1129. cmpq %rdx, 4*8(%rsp)
  1130. /* If the stack pointer is above the NMI stack, this is a normal NMI */
  1131. ja first_nmi
  1132. subq $EXCEPTION_STKSZ, %rdx
  1133. cmpq %rdx, 4*8(%rsp)
  1134. /* If it is below the NMI stack, it is a normal NMI */
  1135. jb first_nmi
  1136. /* Ah, it is within the NMI stack. */
  1137. testb $(X86_EFLAGS_DF >> 8), (3*8 + 1)(%rsp)
  1138. jz first_nmi /* RSP was user controlled. */
  1139. /* This is a nested NMI. */
  1140. nested_nmi:
  1141. /*
  1142. * Modify the "iret" frame to point to repeat_nmi, forcing another
  1143. * iteration of NMI handling.
  1144. */
  1145. subq $8, %rsp
  1146. leaq -10*8(%rsp), %rdx
  1147. pushq $__KERNEL_DS
  1148. pushq %rdx
  1149. pushfq
  1150. pushq $__KERNEL_CS
  1151. pushq $repeat_nmi
  1152. /* Put stack back */
  1153. addq $(6*8), %rsp
  1154. nested_nmi_out:
  1155. popq %rdx
  1156. /* We are returning to kernel mode, so this cannot result in a fault. */
  1157. iretq
  1158. first_nmi:
  1159. /* Restore rdx. */
  1160. movq (%rsp), %rdx
  1161. /* Make room for "NMI executing". */
  1162. pushq $0
  1163. /* Leave room for the "iret" frame */
  1164. subq $(5*8), %rsp
  1165. /* Copy the "original" frame to the "outermost" frame */
  1166. .rept 5
  1167. pushq 11*8(%rsp)
  1168. .endr
  1169. UNWIND_HINT_IRET_REGS
  1170. /* Everything up to here is safe from nested NMIs */
  1171. #ifdef CONFIG_DEBUG_ENTRY
  1172. /*
  1173. * For ease of testing, unmask NMIs right away. Disabled by
  1174. * default because IRET is very expensive.
  1175. */
  1176. pushq $0 /* SS */
  1177. pushq %rsp /* RSP (minus 8 because of the previous push) */
  1178. addq $8, (%rsp) /* Fix up RSP */
  1179. pushfq /* RFLAGS */
  1180. pushq $__KERNEL_CS /* CS */
  1181. pushq $1f /* RIP */
  1182. iretq /* continues at repeat_nmi below */
  1183. UNWIND_HINT_IRET_REGS
  1184. 1:
  1185. #endif
  1186. repeat_nmi:
  1187. ANNOTATE_NOENDBR // this code
  1188. /*
  1189. * If there was a nested NMI, the first NMI's iret will return
  1190. * here. But NMIs are still enabled and we can take another
  1191. * nested NMI. The nested NMI checks the interrupted RIP to see
  1192. * if it is between repeat_nmi and end_repeat_nmi, and if so
  1193. * it will just return, as we are about to repeat an NMI anyway.
  1194. * This makes it safe to copy to the stack frame that a nested
  1195. * NMI will update.
  1196. *
  1197. * RSP is pointing to "outermost RIP". gsbase is unknown, but, if
  1198. * we're repeating an NMI, gsbase has the same value that it had on
  1199. * the first iteration. paranoid_entry will load the kernel
  1200. * gsbase if needed before we call exc_nmi(). "NMI executing"
  1201. * is zero.
  1202. */
  1203. movq $1, 10*8(%rsp) /* Set "NMI executing". */
  1204. /*
  1205. * Copy the "outermost" frame to the "iret" frame. NMIs that nest
  1206. * here must not modify the "iret" frame while we're writing to
  1207. * it or it will end up containing garbage.
  1208. */
  1209. addq $(10*8), %rsp
  1210. .rept 5
  1211. pushq -6*8(%rsp)
  1212. .endr
  1213. subq $(5*8), %rsp
  1214. end_repeat_nmi:
  1215. ANNOTATE_NOENDBR // this code
  1216. /*
  1217. * Everything below this point can be preempted by a nested NMI.
  1218. * If this happens, then the inner NMI will change the "iret"
  1219. * frame to point back to repeat_nmi.
  1220. */
  1221. pushq $-1 /* ORIG_RAX: no syscall to restart */
  1222. /*
  1223. * Use paranoid_entry to handle SWAPGS, but no need to use paranoid_exit
  1224. * as we should not be calling schedule in NMI context.
  1225. * Even with normal interrupts enabled. An NMI should not be
  1226. * setting NEED_RESCHED or anything that normal interrupts and
  1227. * exceptions might do.
  1228. */
  1229. call paranoid_entry
  1230. UNWIND_HINT_REGS
  1231. movq %rsp, %rdi
  1232. call exc_nmi
  1233. /* Always restore stashed SPEC_CTRL value (see paranoid_entry) */
  1234. IBRS_EXIT save_reg=%r15
  1235. PARANOID_RESTORE_CR3 scratch_reg=%r15 save_reg=%r14
  1236. /*
  1237. * The above invocation of paranoid_entry stored the GSBASE
  1238. * related information in R/EBX depending on the availability
  1239. * of FSGSBASE.
  1240. *
  1241. * If FSGSBASE is enabled, restore the saved GSBASE value
  1242. * unconditionally, otherwise take the conditional SWAPGS path.
  1243. */
  1244. ALTERNATIVE "jmp nmi_no_fsgsbase", "", X86_FEATURE_FSGSBASE
  1245. wrgsbase %rbx
  1246. jmp nmi_restore
  1247. nmi_no_fsgsbase:
  1248. /* EBX == 0 -> invoke SWAPGS */
  1249. testl %ebx, %ebx
  1250. jnz nmi_restore
  1251. nmi_swapgs:
  1252. swapgs
  1253. nmi_restore:
  1254. POP_REGS
  1255. /*
  1256. * Skip orig_ax and the "outermost" frame to point RSP at the "iret"
  1257. * at the "iret" frame.
  1258. */
  1259. addq $6*8, %rsp
  1260. /*
  1261. * Clear "NMI executing". Set DF first so that we can easily
  1262. * distinguish the remaining code between here and IRET from
  1263. * the SYSCALL entry and exit paths.
  1264. *
  1265. * We arguably should just inspect RIP instead, but I (Andy) wrote
  1266. * this code when I had the misapprehension that Xen PV supported
  1267. * NMIs, and Xen PV would break that approach.
  1268. */
  1269. std
  1270. movq $0, 5*8(%rsp) /* clear "NMI executing" */
  1271. /*
  1272. * Skip CLEAR_CPU_BUFFERS here, since it only helps in rare cases like
  1273. * NMI in kernel after user state is restored. For an unprivileged user
  1274. * these conditions are hard to meet.
  1275. */
  1276. /*
  1277. * iretq reads the "iret" frame and exits the NMI stack in a
  1278. * single instruction. We are returning to kernel mode, so this
  1279. * cannot result in a fault. Similarly, we don't need to worry
  1280. * about espfix64 on the way back to kernel mode.
  1281. */
  1282. iretq
  1283. SYM_CODE_END(asm_exc_nmi)
  1284. /*
  1285. * This handles SYSCALL from 32-bit code. There is no way to program
  1286. * MSRs to fully disable 32-bit SYSCALL.
  1287. */
  1288. SYM_CODE_START(entry_SYSCALL32_ignore)
  1289. UNWIND_HINT_END_OF_STACK
  1290. ENDBR
  1291. mov $-ENOSYS, %eax
  1292. CLEAR_CPU_BUFFERS
  1293. sysretl
  1294. SYM_CODE_END(entry_SYSCALL32_ignore)
  1295. .pushsection .text, "ax"
  1296. __FUNC_ALIGN
  1297. SYM_CODE_START_NOALIGN(rewind_stack_and_make_dead)
  1298. UNWIND_HINT_FUNC
  1299. /* Prevent any naive code from trying to unwind to our caller. */
  1300. xorl %ebp, %ebp
  1301. movq PER_CPU_VAR(pcpu_hot + X86_top_of_stack), %rax
  1302. leaq -PTREGS_SIZE(%rax), %rsp
  1303. UNWIND_HINT_REGS
  1304. call make_task_dead
  1305. SYM_CODE_END(rewind_stack_and_make_dead)
  1306. .popsection
  1307. /*
  1308. * This sequence executes branches in order to remove user branch information
  1309. * from the branch history tracker in the Branch Predictor, therefore removing
  1310. * user influence on subsequent BTB lookups.
  1311. *
  1312. * It should be used on parts prior to Alder Lake. Newer parts should use the
  1313. * BHI_DIS_S hardware control instead. If a pre-Alder Lake part is being
  1314. * virtualized on newer hardware the VMM should protect against BHI attacks by
  1315. * setting BHI_DIS_S for the guests.
  1316. *
  1317. * CALLs/RETs are necessary to prevent Loop Stream Detector(LSD) from engaging
  1318. * and not clearing the branch history. The call tree looks like:
  1319. *
  1320. * call 1
  1321. * call 2
  1322. * call 2
  1323. * call 2
  1324. * call 2
  1325. * call 2
  1326. * ret
  1327. * ret
  1328. * ret
  1329. * ret
  1330. * ret
  1331. * ret
  1332. *
  1333. * This means that the stack is non-constant and ORC can't unwind it with %rsp
  1334. * alone. Therefore we unconditionally set up the frame pointer, which allows
  1335. * ORC to unwind properly.
  1336. *
  1337. * The alignment is for performance and not for safety, and may be safely
  1338. * refactored in the future if needed.
  1339. */
  1340. SYM_FUNC_START(clear_bhb_loop)
  1341. push %rbp
  1342. mov %rsp, %rbp
  1343. movl $5, %ecx
  1344. ANNOTATE_INTRA_FUNCTION_CALL
  1345. call 1f
  1346. jmp 5f
  1347. .align 64, 0xcc
  1348. ANNOTATE_INTRA_FUNCTION_CALL
  1349. 1: call 2f
  1350. RET
  1351. .align 64, 0xcc
  1352. 2: movl $5, %eax
  1353. 3: jmp 4f
  1354. nop
  1355. 4: sub $1, %eax
  1356. jnz 3b
  1357. sub $1, %ecx
  1358. jnz 1b
  1359. RET
  1360. 5: lfence
  1361. pop %rbp
  1362. RET
  1363. SYM_FUNC_END(clear_bhb_loop)
  1364. EXPORT_SYMBOL_GPL(clear_bhb_loop)
  1365. STACK_FRAME_NON_STANDARD(clear_bhb_loop)