entry_64.S 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809
  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/x86/entry_64.txt
  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. * - ENTRY/END: Define functions in the symbol table.
  19. * - TRACE_IRQ_*: Trace hardirq state for lock debugging.
  20. * - idtentry: Define exception entry points.
  21. */
  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/export.h>
  39. #include <asm/frame.h>
  40. #include <asm/nospec-branch.h>
  41. #include <linux/err.h>
  42. #include "calling.h"
  43. .code64
  44. .section .entry.text, "ax"
  45. #ifdef CONFIG_PARAVIRT
  46. ENTRY(native_usergs_sysret64)
  47. UNWIND_HINT_EMPTY
  48. swapgs
  49. sysretq
  50. END(native_usergs_sysret64)
  51. #endif /* CONFIG_PARAVIRT */
  52. .macro TRACE_IRQS_FLAGS flags:req
  53. #ifdef CONFIG_TRACE_IRQFLAGS
  54. bt $9, \flags /* interrupts off? */
  55. jnc 1f
  56. TRACE_IRQS_ON
  57. 1:
  58. #endif
  59. .endm
  60. .macro TRACE_IRQS_IRETQ
  61. TRACE_IRQS_FLAGS EFLAGS(%rsp)
  62. .endm
  63. /*
  64. * When dynamic function tracer is enabled it will add a breakpoint
  65. * to all locations that it is about to modify, sync CPUs, update
  66. * all the code, sync CPUs, then remove the breakpoints. In this time
  67. * if lockdep is enabled, it might jump back into the debug handler
  68. * outside the updating of the IST protection. (TRACE_IRQS_ON/OFF).
  69. *
  70. * We need to change the IDT table before calling TRACE_IRQS_ON/OFF to
  71. * make sure the stack pointer does not get reset back to the top
  72. * of the debug stack, and instead just reuses the current stack.
  73. */
  74. #if defined(CONFIG_DYNAMIC_FTRACE) && defined(CONFIG_TRACE_IRQFLAGS)
  75. .macro TRACE_IRQS_OFF_DEBUG
  76. call debug_stack_set_zero
  77. TRACE_IRQS_OFF
  78. call debug_stack_reset
  79. .endm
  80. .macro TRACE_IRQS_ON_DEBUG
  81. call debug_stack_set_zero
  82. TRACE_IRQS_ON
  83. call debug_stack_reset
  84. .endm
  85. .macro TRACE_IRQS_IRETQ_DEBUG
  86. bt $9, EFLAGS(%rsp) /* interrupts off? */
  87. jnc 1f
  88. TRACE_IRQS_ON_DEBUG
  89. 1:
  90. .endm
  91. #else
  92. # define TRACE_IRQS_OFF_DEBUG TRACE_IRQS_OFF
  93. # define TRACE_IRQS_ON_DEBUG TRACE_IRQS_ON
  94. # define TRACE_IRQS_IRETQ_DEBUG TRACE_IRQS_IRETQ
  95. #endif
  96. /*
  97. * 64-bit SYSCALL instruction entry. Up to 6 arguments in registers.
  98. *
  99. * This is the only entry point used for 64-bit system calls. The
  100. * hardware interface is reasonably well designed and the register to
  101. * argument mapping Linux uses fits well with the registers that are
  102. * available when SYSCALL is used.
  103. *
  104. * SYSCALL instructions can be found inlined in libc implementations as
  105. * well as some other programs and libraries. There are also a handful
  106. * of SYSCALL instructions in the vDSO used, for example, as a
  107. * clock_gettimeofday fallback.
  108. *
  109. * 64-bit SYSCALL saves rip to rcx, clears rflags.RF, then saves rflags to r11,
  110. * then loads new ss, cs, and rip from previously programmed MSRs.
  111. * rflags gets masked by a value from another MSR (so CLD and CLAC
  112. * are not needed). SYSCALL does not save anything on the stack
  113. * and does not change rsp.
  114. *
  115. * Registers on entry:
  116. * rax system call number
  117. * rcx return address
  118. * r11 saved rflags (note: r11 is callee-clobbered register in C ABI)
  119. * rdi arg0
  120. * rsi arg1
  121. * rdx arg2
  122. * r10 arg3 (needs to be moved to rcx to conform to C ABI)
  123. * r8 arg4
  124. * r9 arg5
  125. * (note: r12-r15, rbp, rbx are callee-preserved in C ABI)
  126. *
  127. * Only called from user space.
  128. *
  129. * When user can change pt_regs->foo always force IRET. That is because
  130. * it deals with uncanonical addresses better. SYSRET has trouble
  131. * with them due to bugs in both AMD and Intel CPUs.
  132. */
  133. .pushsection .entry_trampoline, "ax"
  134. /*
  135. * The code in here gets remapped into cpu_entry_area's trampoline. This means
  136. * that the assembler and linker have the wrong idea as to where this code
  137. * lives (and, in fact, it's mapped more than once, so it's not even at a
  138. * fixed address). So we can't reference any symbols outside the entry
  139. * trampoline and expect it to work.
  140. *
  141. * Instead, we carefully abuse %rip-relative addressing.
  142. * _entry_trampoline(%rip) refers to the start of the remapped) entry
  143. * trampoline. We can thus find cpu_entry_area with this macro:
  144. */
  145. #define CPU_ENTRY_AREA \
  146. _entry_trampoline - CPU_ENTRY_AREA_entry_trampoline(%rip)
  147. /* The top word of the SYSENTER stack is hot and is usable as scratch space. */
  148. #define RSP_SCRATCH CPU_ENTRY_AREA_entry_stack + \
  149. SIZEOF_entry_stack - 8 + CPU_ENTRY_AREA
  150. ENTRY(entry_SYSCALL_64_trampoline)
  151. UNWIND_HINT_EMPTY
  152. swapgs
  153. /* Stash the user RSP. */
  154. movq %rsp, RSP_SCRATCH
  155. /* Note: using %rsp as a scratch reg. */
  156. SWITCH_TO_KERNEL_CR3 scratch_reg=%rsp
  157. /* Load the top of the task stack into RSP */
  158. movq CPU_ENTRY_AREA_tss + TSS_sp1 + CPU_ENTRY_AREA, %rsp
  159. /* Start building the simulated IRET frame. */
  160. pushq $__USER_DS /* pt_regs->ss */
  161. pushq RSP_SCRATCH /* pt_regs->sp */
  162. pushq %r11 /* pt_regs->flags */
  163. pushq $__USER_CS /* pt_regs->cs */
  164. pushq %rcx /* pt_regs->ip */
  165. /*
  166. * x86 lacks a near absolute jump, and we can't jump to the real
  167. * entry text with a relative jump. We could push the target
  168. * address and then use retq, but this destroys the pipeline on
  169. * many CPUs (wasting over 20 cycles on Sandy Bridge). Instead,
  170. * spill RDI and restore it in a second-stage trampoline.
  171. */
  172. pushq %rdi
  173. movq $entry_SYSCALL_64_stage2, %rdi
  174. JMP_NOSPEC %rdi
  175. END(entry_SYSCALL_64_trampoline)
  176. .popsection
  177. ENTRY(entry_SYSCALL_64_stage2)
  178. UNWIND_HINT_EMPTY
  179. popq %rdi
  180. jmp entry_SYSCALL_64_after_hwframe
  181. END(entry_SYSCALL_64_stage2)
  182. ENTRY(entry_SYSCALL_64)
  183. UNWIND_HINT_EMPTY
  184. /*
  185. * Interrupts are off on entry.
  186. * We do not frame this tiny irq-off block with TRACE_IRQS_OFF/ON,
  187. * it is too small to ever cause noticeable irq latency.
  188. */
  189. swapgs
  190. /*
  191. * This path is not taken when PAGE_TABLE_ISOLATION is disabled so it
  192. * is not required to switch CR3.
  193. */
  194. movq %rsp, PER_CPU_VAR(rsp_scratch)
  195. movq PER_CPU_VAR(cpu_current_top_of_stack), %rsp
  196. /* Construct struct pt_regs on stack */
  197. pushq $__USER_DS /* pt_regs->ss */
  198. pushq PER_CPU_VAR(rsp_scratch) /* pt_regs->sp */
  199. pushq %r11 /* pt_regs->flags */
  200. pushq $__USER_CS /* pt_regs->cs */
  201. pushq %rcx /* pt_regs->ip */
  202. GLOBAL(entry_SYSCALL_64_after_hwframe)
  203. pushq %rax /* pt_regs->orig_ax */
  204. pushq %rdi /* pt_regs->di */
  205. pushq %rsi /* pt_regs->si */
  206. pushq %rdx /* pt_regs->dx */
  207. pushq %rcx /* pt_regs->cx */
  208. pushq $-ENOSYS /* pt_regs->ax */
  209. pushq %r8 /* pt_regs->r8 */
  210. pushq %r9 /* pt_regs->r9 */
  211. pushq %r10 /* pt_regs->r10 */
  212. pushq %r11 /* pt_regs->r11 */
  213. sub $(6*8), %rsp /* pt_regs->bp, bx, r12-15 not saved */
  214. UNWIND_HINT_REGS extra=0
  215. TRACE_IRQS_OFF
  216. /*
  217. * If we need to do entry work or if we guess we'll need to do
  218. * exit work, go straight to the slow path.
  219. */
  220. movq PER_CPU_VAR(current_task), %r11
  221. testl $_TIF_WORK_SYSCALL_ENTRY|_TIF_ALLWORK_MASK, TASK_TI_flags(%r11)
  222. jnz entry_SYSCALL64_slow_path
  223. entry_SYSCALL_64_fastpath:
  224. /*
  225. * Easy case: enable interrupts and issue the syscall. If the syscall
  226. * needs pt_regs, we'll call a stub that disables interrupts again
  227. * and jumps to the slow path.
  228. */
  229. TRACE_IRQS_ON
  230. ENABLE_INTERRUPTS(CLBR_NONE)
  231. #if __SYSCALL_MASK == ~0
  232. cmpq $__NR_syscall_max, %rax
  233. #else
  234. andl $__SYSCALL_MASK, %eax
  235. cmpl $__NR_syscall_max, %eax
  236. #endif
  237. ja 1f /* return -ENOSYS (already in pt_regs->ax) */
  238. movq %r10, %rcx
  239. /*
  240. * This call instruction is handled specially in stub_ptregs_64.
  241. * It might end up jumping to the slow path. If it jumps, RAX
  242. * and all argument registers are clobbered.
  243. */
  244. #ifdef CONFIG_RETPOLINE
  245. movq sys_call_table(, %rax, 8), %rax
  246. call __x86_indirect_thunk_rax
  247. #else
  248. call *sys_call_table(, %rax, 8)
  249. #endif
  250. .Lentry_SYSCALL_64_after_fastpath_call:
  251. movq %rax, RAX(%rsp)
  252. 1:
  253. /*
  254. * If we get here, then we know that pt_regs is clean for SYSRET64.
  255. * If we see that no exit work is required (which we are required
  256. * to check with IRQs off), then we can go straight to SYSRET64.
  257. */
  258. DISABLE_INTERRUPTS(CLBR_ANY)
  259. TRACE_IRQS_OFF
  260. movq PER_CPU_VAR(current_task), %r11
  261. testl $_TIF_ALLWORK_MASK, TASK_TI_flags(%r11)
  262. jnz 1f
  263. LOCKDEP_SYS_EXIT
  264. TRACE_IRQS_ON /* user mode is traced as IRQs on */
  265. movq RIP(%rsp), %rcx
  266. movq EFLAGS(%rsp), %r11
  267. addq $6*8, %rsp /* skip extra regs -- they were preserved */
  268. UNWIND_HINT_EMPTY
  269. jmp .Lpop_c_regs_except_rcx_r11_and_sysret
  270. 1:
  271. /*
  272. * The fast path looked good when we started, but something changed
  273. * along the way and we need to switch to the slow path. Calling
  274. * raise(3) will trigger this, for example. IRQs are off.
  275. */
  276. TRACE_IRQS_ON
  277. ENABLE_INTERRUPTS(CLBR_ANY)
  278. SAVE_EXTRA_REGS
  279. movq %rsp, %rdi
  280. call syscall_return_slowpath /* returns with IRQs disabled */
  281. jmp return_from_SYSCALL_64
  282. entry_SYSCALL64_slow_path:
  283. /* IRQs are off. */
  284. SAVE_EXTRA_REGS
  285. movq %rsp, %rdi
  286. call do_syscall_64 /* returns with IRQs disabled */
  287. return_from_SYSCALL_64:
  288. TRACE_IRQS_IRETQ /* we're about to change IF */
  289. /*
  290. * Try to use SYSRET instead of IRET if we're returning to
  291. * a completely clean 64-bit userspace context. If we're not,
  292. * go to the slow exit path.
  293. */
  294. movq RCX(%rsp), %rcx
  295. movq RIP(%rsp), %r11
  296. cmpq %rcx, %r11 /* SYSRET requires RCX == RIP */
  297. jne swapgs_restore_regs_and_return_to_usermode
  298. /*
  299. * On Intel CPUs, SYSRET with non-canonical RCX/RIP will #GP
  300. * in kernel space. This essentially lets the user take over
  301. * the kernel, since userspace controls RSP.
  302. *
  303. * If width of "canonical tail" ever becomes variable, this will need
  304. * to be updated to remain correct on both old and new CPUs.
  305. *
  306. * Change top bits to match most significant bit (47th or 56th bit
  307. * depending on paging mode) in the address.
  308. */
  309. shl $(64 - (__VIRTUAL_MASK_SHIFT+1)), %rcx
  310. sar $(64 - (__VIRTUAL_MASK_SHIFT+1)), %rcx
  311. /* If this changed %rcx, it was not canonical */
  312. cmpq %rcx, %r11
  313. jne swapgs_restore_regs_and_return_to_usermode
  314. cmpq $__USER_CS, CS(%rsp) /* CS must match SYSRET */
  315. jne swapgs_restore_regs_and_return_to_usermode
  316. movq R11(%rsp), %r11
  317. cmpq %r11, EFLAGS(%rsp) /* R11 == RFLAGS */
  318. jne swapgs_restore_regs_and_return_to_usermode
  319. /*
  320. * SYSCALL clears RF when it saves RFLAGS in R11 and SYSRET cannot
  321. * restore RF properly. If the slowpath sets it for whatever reason, we
  322. * need to restore it correctly.
  323. *
  324. * SYSRET can restore TF, but unlike IRET, restoring TF results in a
  325. * trap from userspace immediately after SYSRET. This would cause an
  326. * infinite loop whenever #DB happens with register state that satisfies
  327. * the opportunistic SYSRET conditions. For example, single-stepping
  328. * this user code:
  329. *
  330. * movq $stuck_here, %rcx
  331. * pushfq
  332. * popq %r11
  333. * stuck_here:
  334. *
  335. * would never get past 'stuck_here'.
  336. */
  337. testq $(X86_EFLAGS_RF|X86_EFLAGS_TF), %r11
  338. jnz swapgs_restore_regs_and_return_to_usermode
  339. /* nothing to check for RSP */
  340. cmpq $__USER_DS, SS(%rsp) /* SS must match SYSRET */
  341. jne swapgs_restore_regs_and_return_to_usermode
  342. /*
  343. * We win! This label is here just for ease of understanding
  344. * perf profiles. Nothing jumps here.
  345. */
  346. syscall_return_via_sysret:
  347. /* rcx and r11 are already restored (see code above) */
  348. UNWIND_HINT_EMPTY
  349. POP_EXTRA_REGS
  350. .Lpop_c_regs_except_rcx_r11_and_sysret:
  351. popq %rsi /* skip r11 */
  352. popq %r10
  353. popq %r9
  354. popq %r8
  355. popq %rax
  356. popq %rsi /* skip rcx */
  357. popq %rdx
  358. popq %rsi
  359. /*
  360. * Now all regs are restored except RSP and RDI.
  361. * Save old stack pointer and switch to trampoline stack.
  362. */
  363. movq %rsp, %rdi
  364. movq PER_CPU_VAR(cpu_tss_rw + TSS_sp0), %rsp
  365. pushq RSP-RDI(%rdi) /* RSP */
  366. pushq (%rdi) /* RDI */
  367. /*
  368. * We are on the trampoline stack. All regs except RDI are live.
  369. * We can do future final exit work right here.
  370. */
  371. SWITCH_TO_USER_CR3_STACK scratch_reg=%rdi
  372. popq %rdi
  373. popq %rsp
  374. USERGS_SYSRET64
  375. END(entry_SYSCALL_64)
  376. ENTRY(stub_ptregs_64)
  377. /*
  378. * Syscalls marked as needing ptregs land here.
  379. * If we are on the fast path, we need to save the extra regs,
  380. * which we achieve by trying again on the slow path. If we are on
  381. * the slow path, the extra regs are already saved.
  382. *
  383. * RAX stores a pointer to the C function implementing the syscall.
  384. * IRQs are on.
  385. */
  386. cmpq $.Lentry_SYSCALL_64_after_fastpath_call, (%rsp)
  387. jne 1f
  388. /*
  389. * Called from fast path -- disable IRQs again, pop return address
  390. * and jump to slow path
  391. */
  392. DISABLE_INTERRUPTS(CLBR_ANY)
  393. TRACE_IRQS_OFF
  394. popq %rax
  395. UNWIND_HINT_REGS extra=0
  396. jmp entry_SYSCALL64_slow_path
  397. 1:
  398. JMP_NOSPEC %rax /* Called from C */
  399. END(stub_ptregs_64)
  400. .macro ptregs_stub func
  401. ENTRY(ptregs_\func)
  402. UNWIND_HINT_FUNC
  403. leaq \func(%rip), %rax
  404. jmp stub_ptregs_64
  405. END(ptregs_\func)
  406. .endm
  407. /* Instantiate ptregs_stub for each ptregs-using syscall */
  408. #define __SYSCALL_64_QUAL_(sym)
  409. #define __SYSCALL_64_QUAL_ptregs(sym) ptregs_stub sym
  410. #define __SYSCALL_64(nr, sym, qual) __SYSCALL_64_QUAL_##qual(sym)
  411. #include <asm/syscalls_64.h>
  412. /*
  413. * %rdi: prev task
  414. * %rsi: next task
  415. */
  416. ENTRY(__switch_to_asm)
  417. UNWIND_HINT_FUNC
  418. /*
  419. * Save callee-saved registers
  420. * This must match the order in inactive_task_frame
  421. */
  422. pushq %rbp
  423. pushq %rbx
  424. pushq %r12
  425. pushq %r13
  426. pushq %r14
  427. pushq %r15
  428. /* switch stack */
  429. movq %rsp, TASK_threadsp(%rdi)
  430. movq TASK_threadsp(%rsi), %rsp
  431. #ifdef CONFIG_CC_STACKPROTECTOR
  432. movq TASK_stack_canary(%rsi), %rbx
  433. movq %rbx, PER_CPU_VAR(irq_stack_union)+stack_canary_offset
  434. #endif
  435. #ifdef CONFIG_RETPOLINE
  436. /*
  437. * When switching from a shallower to a deeper call stack
  438. * the RSB may either underflow or use entries populated
  439. * with userspace addresses. On CPUs where those concerns
  440. * exist, overwrite the RSB with entries which capture
  441. * speculative execution to prevent attack.
  442. */
  443. FILL_RETURN_BUFFER %r12, RSB_CLEAR_LOOPS, X86_FEATURE_RSB_CTXSW
  444. #endif
  445. /* restore callee-saved registers */
  446. popq %r15
  447. popq %r14
  448. popq %r13
  449. popq %r12
  450. popq %rbx
  451. popq %rbp
  452. jmp __switch_to
  453. END(__switch_to_asm)
  454. /*
  455. * A newly forked process directly context switches into this address.
  456. *
  457. * rax: prev task we switched from
  458. * rbx: kernel thread func (NULL for user thread)
  459. * r12: kernel thread arg
  460. */
  461. ENTRY(ret_from_fork)
  462. UNWIND_HINT_EMPTY
  463. movq %rax, %rdi
  464. call schedule_tail /* rdi: 'prev' task parameter */
  465. testq %rbx, %rbx /* from kernel_thread? */
  466. jnz 1f /* kernel threads are uncommon */
  467. 2:
  468. UNWIND_HINT_REGS
  469. movq %rsp, %rdi
  470. call syscall_return_slowpath /* returns with IRQs disabled */
  471. TRACE_IRQS_ON /* user mode is traced as IRQS on */
  472. jmp swapgs_restore_regs_and_return_to_usermode
  473. 1:
  474. /* kernel thread */
  475. movq %r12, %rdi
  476. CALL_NOSPEC %rbx
  477. /*
  478. * A kernel thread is allowed to return here after successfully
  479. * calling do_execve(). Exit to userspace to complete the execve()
  480. * syscall.
  481. */
  482. movq $0, RAX(%rsp)
  483. jmp 2b
  484. END(ret_from_fork)
  485. /*
  486. * Build the entry stubs with some assembler magic.
  487. * We pack 1 stub into every 8-byte block.
  488. */
  489. .align 8
  490. ENTRY(irq_entries_start)
  491. vector=FIRST_EXTERNAL_VECTOR
  492. .rept (FIRST_SYSTEM_VECTOR - FIRST_EXTERNAL_VECTOR)
  493. UNWIND_HINT_IRET_REGS
  494. pushq $(~vector+0x80) /* Note: always in signed byte range */
  495. jmp common_interrupt
  496. .align 8
  497. vector=vector+1
  498. .endr
  499. END(irq_entries_start)
  500. .macro DEBUG_ENTRY_ASSERT_IRQS_OFF
  501. #ifdef CONFIG_DEBUG_ENTRY
  502. pushq %rax
  503. SAVE_FLAGS(CLBR_RAX)
  504. testl $X86_EFLAGS_IF, %eax
  505. jz .Lokay_\@
  506. ud2
  507. .Lokay_\@:
  508. popq %rax
  509. #endif
  510. .endm
  511. /*
  512. * Enters the IRQ stack if we're not already using it. NMI-safe. Clobbers
  513. * flags and puts old RSP into old_rsp, and leaves all other GPRs alone.
  514. * Requires kernel GSBASE.
  515. *
  516. * The invariant is that, if irq_count != -1, then the IRQ stack is in use.
  517. */
  518. .macro ENTER_IRQ_STACK regs=1 old_rsp
  519. DEBUG_ENTRY_ASSERT_IRQS_OFF
  520. movq %rsp, \old_rsp
  521. .if \regs
  522. UNWIND_HINT_REGS base=\old_rsp
  523. .endif
  524. incl PER_CPU_VAR(irq_count)
  525. jnz .Lirq_stack_push_old_rsp_\@
  526. /*
  527. * Right now, if we just incremented irq_count to zero, we've
  528. * claimed the IRQ stack but we haven't switched to it yet.
  529. *
  530. * If anything is added that can interrupt us here without using IST,
  531. * it must be *extremely* careful to limit its stack usage. This
  532. * could include kprobes and a hypothetical future IST-less #DB
  533. * handler.
  534. *
  535. * The OOPS unwinder relies on the word at the top of the IRQ
  536. * stack linking back to the previous RSP for the entire time we're
  537. * on the IRQ stack. For this to work reliably, we need to write
  538. * it before we actually move ourselves to the IRQ stack.
  539. */
  540. movq \old_rsp, PER_CPU_VAR(irq_stack_union + IRQ_STACK_SIZE - 8)
  541. movq PER_CPU_VAR(irq_stack_ptr), %rsp
  542. #ifdef CONFIG_DEBUG_ENTRY
  543. /*
  544. * If the first movq above becomes wrong due to IRQ stack layout
  545. * changes, the only way we'll notice is if we try to unwind right
  546. * here. Assert that we set up the stack right to catch this type
  547. * of bug quickly.
  548. */
  549. cmpq -8(%rsp), \old_rsp
  550. je .Lirq_stack_okay\@
  551. ud2
  552. .Lirq_stack_okay\@:
  553. #endif
  554. .Lirq_stack_push_old_rsp_\@:
  555. pushq \old_rsp
  556. .if \regs
  557. UNWIND_HINT_REGS indirect=1
  558. .endif
  559. .endm
  560. /*
  561. * Undoes ENTER_IRQ_STACK.
  562. */
  563. .macro LEAVE_IRQ_STACK regs=1
  564. DEBUG_ENTRY_ASSERT_IRQS_OFF
  565. /* We need to be off the IRQ stack before decrementing irq_count. */
  566. popq %rsp
  567. .if \regs
  568. UNWIND_HINT_REGS
  569. .endif
  570. /*
  571. * As in ENTER_IRQ_STACK, irq_count == 0, we are still claiming
  572. * the irq stack but we're not on it.
  573. */
  574. decl PER_CPU_VAR(irq_count)
  575. .endm
  576. /*
  577. * Interrupt entry/exit.
  578. *
  579. * Interrupt entry points save only callee clobbered registers in fast path.
  580. *
  581. * Entry runs with interrupts off.
  582. */
  583. /* 0(%rsp): ~(interrupt number) */
  584. .macro interrupt func
  585. cld
  586. testb $3, CS-ORIG_RAX(%rsp)
  587. jz 1f
  588. SWAPGS
  589. call switch_to_thread_stack
  590. 1:
  591. ALLOC_PT_GPREGS_ON_STACK
  592. SAVE_C_REGS
  593. SAVE_EXTRA_REGS
  594. ENCODE_FRAME_POINTER
  595. testb $3, CS(%rsp)
  596. jz 1f
  597. /*
  598. * IRQ from user mode.
  599. *
  600. * We need to tell lockdep that IRQs are off. We can't do this until
  601. * we fix gsbase, and we should do it before enter_from_user_mode
  602. * (which can take locks). Since TRACE_IRQS_OFF idempotent,
  603. * the simplest way to handle it is to just call it twice if
  604. * we enter from user mode. There's no reason to optimize this since
  605. * TRACE_IRQS_OFF is a no-op if lockdep is off.
  606. */
  607. TRACE_IRQS_OFF
  608. CALL_enter_from_user_mode
  609. 1:
  610. ENTER_IRQ_STACK old_rsp=%rdi
  611. /* We entered an interrupt context - irqs are off: */
  612. TRACE_IRQS_OFF
  613. call \func /* rdi points to pt_regs */
  614. .endm
  615. /*
  616. * The interrupt stubs push (~vector+0x80) onto the stack and
  617. * then jump to common_interrupt.
  618. */
  619. .p2align CONFIG_X86_L1_CACHE_SHIFT
  620. common_interrupt:
  621. ASM_CLAC
  622. addq $-0x80, (%rsp) /* Adjust vector to [-256, -1] range */
  623. interrupt do_IRQ
  624. /* 0(%rsp): old RSP */
  625. ret_from_intr:
  626. DISABLE_INTERRUPTS(CLBR_ANY)
  627. TRACE_IRQS_OFF
  628. LEAVE_IRQ_STACK
  629. testb $3, CS(%rsp)
  630. jz retint_kernel
  631. /* Interrupt came from user space */
  632. GLOBAL(retint_user)
  633. mov %rsp,%rdi
  634. call prepare_exit_to_usermode
  635. TRACE_IRQS_IRETQ
  636. GLOBAL(swapgs_restore_regs_and_return_to_usermode)
  637. #ifdef CONFIG_DEBUG_ENTRY
  638. /* Assert that pt_regs indicates user mode. */
  639. testb $3, CS(%rsp)
  640. jnz 1f
  641. ud2
  642. 1:
  643. #endif
  644. POP_EXTRA_REGS
  645. popq %r11
  646. popq %r10
  647. popq %r9
  648. popq %r8
  649. popq %rax
  650. popq %rcx
  651. popq %rdx
  652. popq %rsi
  653. /*
  654. * The stack is now user RDI, orig_ax, RIP, CS, EFLAGS, RSP, SS.
  655. * Save old stack pointer and switch to trampoline stack.
  656. */
  657. movq %rsp, %rdi
  658. movq PER_CPU_VAR(cpu_tss_rw + TSS_sp0), %rsp
  659. /* Copy the IRET frame to the trampoline stack. */
  660. pushq 6*8(%rdi) /* SS */
  661. pushq 5*8(%rdi) /* RSP */
  662. pushq 4*8(%rdi) /* EFLAGS */
  663. pushq 3*8(%rdi) /* CS */
  664. pushq 2*8(%rdi) /* RIP */
  665. /* Push user RDI on the trampoline stack. */
  666. pushq (%rdi)
  667. /*
  668. * We are on the trampoline stack. All regs except RDI are live.
  669. * We can do future final exit work right here.
  670. */
  671. SWITCH_TO_USER_CR3_STACK scratch_reg=%rdi
  672. /* Restore RDI. */
  673. popq %rdi
  674. SWAPGS
  675. INTERRUPT_RETURN
  676. /* Returning to kernel space */
  677. retint_kernel:
  678. #ifdef CONFIG_PREEMPT
  679. /* Interrupts are off */
  680. /* Check if we need preemption */
  681. bt $9, EFLAGS(%rsp) /* were interrupts off? */
  682. jnc 1f
  683. 0: cmpl $0, PER_CPU_VAR(__preempt_count)
  684. jnz 1f
  685. call preempt_schedule_irq
  686. jmp 0b
  687. 1:
  688. #endif
  689. /*
  690. * The iretq could re-enable interrupts:
  691. */
  692. TRACE_IRQS_IRETQ
  693. GLOBAL(restore_regs_and_return_to_kernel)
  694. #ifdef CONFIG_DEBUG_ENTRY
  695. /* Assert that pt_regs indicates kernel mode. */
  696. testb $3, CS(%rsp)
  697. jz 1f
  698. ud2
  699. 1:
  700. #endif
  701. POP_EXTRA_REGS
  702. POP_C_REGS
  703. addq $8, %rsp /* skip regs->orig_ax */
  704. INTERRUPT_RETURN
  705. ENTRY(native_iret)
  706. UNWIND_HINT_IRET_REGS
  707. /*
  708. * Are we returning to a stack segment from the LDT? Note: in
  709. * 64-bit mode SS:RSP on the exception stack is always valid.
  710. */
  711. #ifdef CONFIG_X86_ESPFIX64
  712. testb $4, (SS-RIP)(%rsp)
  713. jnz native_irq_return_ldt
  714. #endif
  715. .global native_irq_return_iret
  716. native_irq_return_iret:
  717. /*
  718. * This may fault. Non-paranoid faults on return to userspace are
  719. * handled by fixup_bad_iret. These include #SS, #GP, and #NP.
  720. * Double-faults due to espfix64 are handled in do_double_fault.
  721. * Other faults here are fatal.
  722. */
  723. iretq
  724. #ifdef CONFIG_X86_ESPFIX64
  725. native_irq_return_ldt:
  726. /*
  727. * We are running with user GSBASE. All GPRs contain their user
  728. * values. We have a percpu ESPFIX stack that is eight slots
  729. * long (see ESPFIX_STACK_SIZE). espfix_waddr points to the bottom
  730. * of the ESPFIX stack.
  731. *
  732. * We clobber RAX and RDI in this code. We stash RDI on the
  733. * normal stack and RAX on the ESPFIX stack.
  734. *
  735. * The ESPFIX stack layout we set up looks like this:
  736. *
  737. * --- top of ESPFIX stack ---
  738. * SS
  739. * RSP
  740. * RFLAGS
  741. * CS
  742. * RIP <-- RSP points here when we're done
  743. * RAX <-- espfix_waddr points here
  744. * --- bottom of ESPFIX stack ---
  745. */
  746. pushq %rdi /* Stash user RDI */
  747. SWAPGS /* to kernel GS */
  748. SWITCH_TO_KERNEL_CR3 scratch_reg=%rdi /* to kernel CR3 */
  749. movq PER_CPU_VAR(espfix_waddr), %rdi
  750. movq %rax, (0*8)(%rdi) /* user RAX */
  751. movq (1*8)(%rsp), %rax /* user RIP */
  752. movq %rax, (1*8)(%rdi)
  753. movq (2*8)(%rsp), %rax /* user CS */
  754. movq %rax, (2*8)(%rdi)
  755. movq (3*8)(%rsp), %rax /* user RFLAGS */
  756. movq %rax, (3*8)(%rdi)
  757. movq (5*8)(%rsp), %rax /* user SS */
  758. movq %rax, (5*8)(%rdi)
  759. movq (4*8)(%rsp), %rax /* user RSP */
  760. movq %rax, (4*8)(%rdi)
  761. /* Now RAX == RSP. */
  762. andl $0xffff0000, %eax /* RAX = (RSP & 0xffff0000) */
  763. /*
  764. * espfix_stack[31:16] == 0. The page tables are set up such that
  765. * (espfix_stack | (X & 0xffff0000)) points to a read-only alias of
  766. * espfix_waddr for any X. That is, there are 65536 RO aliases of
  767. * the same page. Set up RSP so that RSP[31:16] contains the
  768. * respective 16 bits of the /userspace/ RSP and RSP nonetheless
  769. * still points to an RO alias of the ESPFIX stack.
  770. */
  771. orq PER_CPU_VAR(espfix_stack), %rax
  772. SWITCH_TO_USER_CR3_STACK scratch_reg=%rdi
  773. SWAPGS /* to user GS */
  774. popq %rdi /* Restore user RDI */
  775. movq %rax, %rsp
  776. UNWIND_HINT_IRET_REGS offset=8
  777. /*
  778. * At this point, we cannot write to the stack any more, but we can
  779. * still read.
  780. */
  781. popq %rax /* Restore user RAX */
  782. /*
  783. * RSP now points to an ordinary IRET frame, except that the page
  784. * is read-only and RSP[31:16] are preloaded with the userspace
  785. * values. We can now IRET back to userspace.
  786. */
  787. jmp native_irq_return_iret
  788. #endif
  789. END(common_interrupt)
  790. /*
  791. * APIC interrupts.
  792. */
  793. .macro apicinterrupt3 num sym do_sym
  794. ENTRY(\sym)
  795. UNWIND_HINT_IRET_REGS
  796. ASM_CLAC
  797. pushq $~(\num)
  798. .Lcommon_\sym:
  799. interrupt \do_sym
  800. jmp ret_from_intr
  801. END(\sym)
  802. .endm
  803. /* Make sure APIC interrupt handlers end up in the irqentry section: */
  804. #define PUSH_SECTION_IRQENTRY .pushsection .irqentry.text, "ax"
  805. #define POP_SECTION_IRQENTRY .popsection
  806. .macro apicinterrupt num sym do_sym
  807. PUSH_SECTION_IRQENTRY
  808. apicinterrupt3 \num \sym \do_sym
  809. POP_SECTION_IRQENTRY
  810. .endm
  811. #ifdef CONFIG_SMP
  812. apicinterrupt3 IRQ_MOVE_CLEANUP_VECTOR irq_move_cleanup_interrupt smp_irq_move_cleanup_interrupt
  813. apicinterrupt3 REBOOT_VECTOR reboot_interrupt smp_reboot_interrupt
  814. #endif
  815. #ifdef CONFIG_X86_UV
  816. apicinterrupt3 UV_BAU_MESSAGE uv_bau_message_intr1 uv_bau_message_interrupt
  817. #endif
  818. apicinterrupt LOCAL_TIMER_VECTOR apic_timer_interrupt smp_apic_timer_interrupt
  819. apicinterrupt X86_PLATFORM_IPI_VECTOR x86_platform_ipi smp_x86_platform_ipi
  820. #ifdef CONFIG_HAVE_KVM
  821. apicinterrupt3 POSTED_INTR_VECTOR kvm_posted_intr_ipi smp_kvm_posted_intr_ipi
  822. apicinterrupt3 POSTED_INTR_WAKEUP_VECTOR kvm_posted_intr_wakeup_ipi smp_kvm_posted_intr_wakeup_ipi
  823. apicinterrupt3 POSTED_INTR_NESTED_VECTOR kvm_posted_intr_nested_ipi smp_kvm_posted_intr_nested_ipi
  824. #endif
  825. #ifdef CONFIG_X86_MCE_THRESHOLD
  826. apicinterrupt THRESHOLD_APIC_VECTOR threshold_interrupt smp_threshold_interrupt
  827. #endif
  828. #ifdef CONFIG_X86_MCE_AMD
  829. apicinterrupt DEFERRED_ERROR_VECTOR deferred_error_interrupt smp_deferred_error_interrupt
  830. #endif
  831. #ifdef CONFIG_X86_THERMAL_VECTOR
  832. apicinterrupt THERMAL_APIC_VECTOR thermal_interrupt smp_thermal_interrupt
  833. #endif
  834. #ifdef CONFIG_SMP
  835. apicinterrupt CALL_FUNCTION_SINGLE_VECTOR call_function_single_interrupt smp_call_function_single_interrupt
  836. apicinterrupt CALL_FUNCTION_VECTOR call_function_interrupt smp_call_function_interrupt
  837. apicinterrupt RESCHEDULE_VECTOR reschedule_interrupt smp_reschedule_interrupt
  838. #endif
  839. apicinterrupt ERROR_APIC_VECTOR error_interrupt smp_error_interrupt
  840. apicinterrupt SPURIOUS_APIC_VECTOR spurious_interrupt smp_spurious_interrupt
  841. #ifdef CONFIG_IRQ_WORK
  842. apicinterrupt IRQ_WORK_VECTOR irq_work_interrupt smp_irq_work_interrupt
  843. #endif
  844. /*
  845. * Exception entry points.
  846. */
  847. #define CPU_TSS_IST(x) PER_CPU_VAR(cpu_tss_rw) + (TSS_ist + ((x) - 1) * 8)
  848. /*
  849. * Switch to the thread stack. This is called with the IRET frame and
  850. * orig_ax on the stack. (That is, RDI..R12 are not on the stack and
  851. * space has not been allocated for them.)
  852. */
  853. ENTRY(switch_to_thread_stack)
  854. UNWIND_HINT_FUNC
  855. pushq %rdi
  856. /* Need to switch before accessing the thread stack. */
  857. SWITCH_TO_KERNEL_CR3 scratch_reg=%rdi
  858. movq %rsp, %rdi
  859. movq PER_CPU_VAR(cpu_current_top_of_stack), %rsp
  860. UNWIND_HINT sp_offset=16 sp_reg=ORC_REG_DI
  861. pushq 7*8(%rdi) /* regs->ss */
  862. pushq 6*8(%rdi) /* regs->rsp */
  863. pushq 5*8(%rdi) /* regs->eflags */
  864. pushq 4*8(%rdi) /* regs->cs */
  865. pushq 3*8(%rdi) /* regs->ip */
  866. pushq 2*8(%rdi) /* regs->orig_ax */
  867. pushq 8(%rdi) /* return address */
  868. UNWIND_HINT_FUNC
  869. movq (%rdi), %rdi
  870. ret
  871. END(switch_to_thread_stack)
  872. .macro idtentry sym do_sym has_error_code:req paranoid=0 shift_ist=-1
  873. ENTRY(\sym)
  874. UNWIND_HINT_IRET_REGS offset=\has_error_code*8
  875. /* Sanity check */
  876. .if \shift_ist != -1 && \paranoid == 0
  877. .error "using shift_ist requires paranoid=1"
  878. .endif
  879. ASM_CLAC
  880. .if \has_error_code == 0
  881. pushq $-1 /* ORIG_RAX: no syscall to restart */
  882. .endif
  883. ALLOC_PT_GPREGS_ON_STACK
  884. .if \paranoid < 2
  885. testb $3, CS(%rsp) /* If coming from userspace, switch stacks */
  886. jnz .Lfrom_usermode_switch_stack_\@
  887. .endif
  888. .if \paranoid
  889. call paranoid_entry
  890. .else
  891. call error_entry
  892. .endif
  893. UNWIND_HINT_REGS
  894. /* returned flag: ebx=0: need swapgs on exit, ebx=1: don't need it */
  895. .if \paranoid
  896. .if \shift_ist != -1
  897. TRACE_IRQS_OFF_DEBUG /* reload IDT in case of recursion */
  898. .else
  899. TRACE_IRQS_OFF
  900. .endif
  901. .endif
  902. movq %rsp, %rdi /* pt_regs pointer */
  903. .if \has_error_code
  904. movq ORIG_RAX(%rsp), %rsi /* get error code */
  905. movq $-1, ORIG_RAX(%rsp) /* no syscall to restart */
  906. .else
  907. xorl %esi, %esi /* no error code */
  908. .endif
  909. .if \shift_ist != -1
  910. subq $EXCEPTION_STKSZ, CPU_TSS_IST(\shift_ist)
  911. .endif
  912. call \do_sym
  913. .if \shift_ist != -1
  914. addq $EXCEPTION_STKSZ, CPU_TSS_IST(\shift_ist)
  915. .endif
  916. /* these procedures expect "no swapgs" flag in ebx */
  917. .if \paranoid
  918. jmp paranoid_exit
  919. .else
  920. jmp error_exit
  921. .endif
  922. .if \paranoid < 2
  923. /*
  924. * Entry from userspace. Switch stacks and treat it
  925. * as a normal entry. This means that paranoid handlers
  926. * run in real process context if user_mode(regs).
  927. */
  928. .Lfrom_usermode_switch_stack_\@:
  929. call error_entry
  930. movq %rsp, %rdi /* pt_regs pointer */
  931. .if \has_error_code
  932. movq ORIG_RAX(%rsp), %rsi /* get error code */
  933. movq $-1, ORIG_RAX(%rsp) /* no syscall to restart */
  934. .else
  935. xorl %esi, %esi /* no error code */
  936. .endif
  937. call \do_sym
  938. jmp error_exit /* %ebx: no swapgs flag */
  939. .endif
  940. END(\sym)
  941. .endm
  942. idtentry divide_error do_divide_error has_error_code=0
  943. idtentry overflow do_overflow has_error_code=0
  944. idtentry bounds do_bounds has_error_code=0
  945. idtentry invalid_op do_invalid_op has_error_code=0
  946. idtentry device_not_available do_device_not_available has_error_code=0
  947. idtentry double_fault do_double_fault has_error_code=1 paranoid=2
  948. idtentry coprocessor_segment_overrun do_coprocessor_segment_overrun has_error_code=0
  949. idtentry invalid_TSS do_invalid_TSS has_error_code=1
  950. idtentry segment_not_present do_segment_not_present has_error_code=1
  951. idtentry spurious_interrupt_bug do_spurious_interrupt_bug has_error_code=0
  952. idtentry coprocessor_error do_coprocessor_error has_error_code=0
  953. idtentry alignment_check do_alignment_check has_error_code=1
  954. idtentry simd_coprocessor_error do_simd_coprocessor_error has_error_code=0
  955. /*
  956. * Reload gs selector with exception handling
  957. * edi: new selector
  958. */
  959. ENTRY(native_load_gs_index)
  960. FRAME_BEGIN
  961. pushfq
  962. DISABLE_INTERRUPTS(CLBR_ANY & ~CLBR_RDI)
  963. TRACE_IRQS_OFF
  964. SWAPGS
  965. .Lgs_change:
  966. movl %edi, %gs
  967. 2: ALTERNATIVE "", "mfence", X86_BUG_SWAPGS_FENCE
  968. SWAPGS
  969. TRACE_IRQS_FLAGS (%rsp)
  970. popfq
  971. FRAME_END
  972. ret
  973. ENDPROC(native_load_gs_index)
  974. EXPORT_SYMBOL(native_load_gs_index)
  975. _ASM_EXTABLE(.Lgs_change, bad_gs)
  976. .section .fixup, "ax"
  977. /* running with kernelgs */
  978. bad_gs:
  979. SWAPGS /* switch back to user gs */
  980. .macro ZAP_GS
  981. /* This can't be a string because the preprocessor needs to see it. */
  982. movl $__USER_DS, %eax
  983. movl %eax, %gs
  984. .endm
  985. ALTERNATIVE "", "ZAP_GS", X86_BUG_NULL_SEG
  986. xorl %eax, %eax
  987. movl %eax, %gs
  988. jmp 2b
  989. .previous
  990. /* Call softirq on interrupt stack. Interrupts are off. */
  991. ENTRY(do_softirq_own_stack)
  992. pushq %rbp
  993. mov %rsp, %rbp
  994. ENTER_IRQ_STACK regs=0 old_rsp=%r11
  995. call __do_softirq
  996. LEAVE_IRQ_STACK regs=0
  997. leaveq
  998. ret
  999. ENDPROC(do_softirq_own_stack)
  1000. #ifdef CONFIG_XEN
  1001. idtentry hypervisor_callback xen_do_hypervisor_callback has_error_code=0
  1002. /*
  1003. * A note on the "critical region" in our callback handler.
  1004. * We want to avoid stacking callback handlers due to events occurring
  1005. * during handling of the last event. To do this, we keep events disabled
  1006. * until we've done all processing. HOWEVER, we must enable events before
  1007. * popping the stack frame (can't be done atomically) and so it would still
  1008. * be possible to get enough handler activations to overflow the stack.
  1009. * Although unlikely, bugs of that kind are hard to track down, so we'd
  1010. * like to avoid the possibility.
  1011. * So, on entry to the handler we detect whether we interrupted an
  1012. * existing activation in its critical region -- if so, we pop the current
  1013. * activation and restart the handler using the previous one.
  1014. */
  1015. ENTRY(xen_do_hypervisor_callback) /* do_hypervisor_callback(struct *pt_regs) */
  1016. /*
  1017. * Since we don't modify %rdi, evtchn_do_upall(struct *pt_regs) will
  1018. * see the correct pointer to the pt_regs
  1019. */
  1020. UNWIND_HINT_FUNC
  1021. movq %rdi, %rsp /* we don't return, adjust the stack frame */
  1022. UNWIND_HINT_REGS
  1023. ENTER_IRQ_STACK old_rsp=%r10
  1024. call xen_evtchn_do_upcall
  1025. LEAVE_IRQ_STACK
  1026. #ifndef CONFIG_PREEMPT
  1027. call xen_maybe_preempt_hcall
  1028. #endif
  1029. jmp error_exit
  1030. END(xen_do_hypervisor_callback)
  1031. /*
  1032. * Hypervisor uses this for application faults while it executes.
  1033. * We get here for two reasons:
  1034. * 1. Fault while reloading DS, ES, FS or GS
  1035. * 2. Fault while executing IRET
  1036. * Category 1 we do not need to fix up as Xen has already reloaded all segment
  1037. * registers that could be reloaded and zeroed the others.
  1038. * Category 2 we fix up by killing the current process. We cannot use the
  1039. * normal Linux return path in this case because if we use the IRET hypercall
  1040. * to pop the stack frame we end up in an infinite loop of failsafe callbacks.
  1041. * We distinguish between categories by comparing each saved segment register
  1042. * with its current contents: any discrepancy means we in category 1.
  1043. */
  1044. ENTRY(xen_failsafe_callback)
  1045. UNWIND_HINT_EMPTY
  1046. movl %ds, %ecx
  1047. cmpw %cx, 0x10(%rsp)
  1048. jne 1f
  1049. movl %es, %ecx
  1050. cmpw %cx, 0x18(%rsp)
  1051. jne 1f
  1052. movl %fs, %ecx
  1053. cmpw %cx, 0x20(%rsp)
  1054. jne 1f
  1055. movl %gs, %ecx
  1056. cmpw %cx, 0x28(%rsp)
  1057. jne 1f
  1058. /* All segments match their saved values => Category 2 (Bad IRET). */
  1059. movq (%rsp), %rcx
  1060. movq 8(%rsp), %r11
  1061. addq $0x30, %rsp
  1062. pushq $0 /* RIP */
  1063. UNWIND_HINT_IRET_REGS offset=8
  1064. jmp general_protection
  1065. 1: /* Segment mismatch => Category 1 (Bad segment). Retry the IRET. */
  1066. movq (%rsp), %rcx
  1067. movq 8(%rsp), %r11
  1068. addq $0x30, %rsp
  1069. UNWIND_HINT_IRET_REGS
  1070. pushq $-1 /* orig_ax = -1 => not a system call */
  1071. ALLOC_PT_GPREGS_ON_STACK
  1072. SAVE_C_REGS
  1073. SAVE_EXTRA_REGS
  1074. ENCODE_FRAME_POINTER
  1075. jmp error_exit
  1076. END(xen_failsafe_callback)
  1077. apicinterrupt3 HYPERVISOR_CALLBACK_VECTOR \
  1078. xen_hvm_callback_vector xen_evtchn_do_upcall
  1079. #endif /* CONFIG_XEN */
  1080. #if IS_ENABLED(CONFIG_HYPERV)
  1081. apicinterrupt3 HYPERVISOR_CALLBACK_VECTOR \
  1082. hyperv_callback_vector hyperv_vector_handler
  1083. #endif /* CONFIG_HYPERV */
  1084. idtentry debug do_debug has_error_code=0 paranoid=1 shift_ist=DEBUG_STACK
  1085. idtentry int3 do_int3 has_error_code=0 paranoid=1 shift_ist=DEBUG_STACK
  1086. idtentry stack_segment do_stack_segment has_error_code=1
  1087. #ifdef CONFIG_XEN
  1088. idtentry xennmi do_nmi has_error_code=0
  1089. idtentry xendebug do_debug has_error_code=0
  1090. idtentry xenint3 do_int3 has_error_code=0
  1091. #endif
  1092. idtentry general_protection do_general_protection has_error_code=1
  1093. idtentry page_fault do_page_fault has_error_code=1
  1094. #ifdef CONFIG_KVM_GUEST
  1095. idtentry async_page_fault do_async_page_fault has_error_code=1
  1096. #endif
  1097. #ifdef CONFIG_X86_MCE
  1098. idtentry machine_check do_mce has_error_code=0 paranoid=1
  1099. #endif
  1100. /*
  1101. * Save all registers in pt_regs, and switch gs if needed.
  1102. * Use slow, but surefire "are we in kernel?" check.
  1103. * Return: ebx=0: need swapgs on exit, ebx=1: otherwise
  1104. */
  1105. ENTRY(paranoid_entry)
  1106. UNWIND_HINT_FUNC
  1107. cld
  1108. SAVE_C_REGS 8
  1109. SAVE_EXTRA_REGS 8
  1110. ENCODE_FRAME_POINTER 8
  1111. movl $1, %ebx
  1112. movl $MSR_GS_BASE, %ecx
  1113. rdmsr
  1114. testl %edx, %edx
  1115. js 1f /* negative -> in kernel */
  1116. SWAPGS
  1117. xorl %ebx, %ebx
  1118. 1:
  1119. SAVE_AND_SWITCH_TO_KERNEL_CR3 scratch_reg=%rax save_reg=%r14
  1120. ret
  1121. END(paranoid_entry)
  1122. /*
  1123. * "Paranoid" exit path from exception stack. This is invoked
  1124. * only on return from non-NMI IST interrupts that came
  1125. * from kernel space.
  1126. *
  1127. * We may be returning to very strange contexts (e.g. very early
  1128. * in syscall entry), so checking for preemption here would
  1129. * be complicated. Fortunately, we there's no good reason
  1130. * to try to handle preemption here.
  1131. *
  1132. * On entry, ebx is "no swapgs" flag (1: don't need swapgs, 0: need it)
  1133. */
  1134. ENTRY(paranoid_exit)
  1135. UNWIND_HINT_REGS
  1136. DISABLE_INTERRUPTS(CLBR_ANY)
  1137. TRACE_IRQS_OFF_DEBUG
  1138. testl %ebx, %ebx /* swapgs needed? */
  1139. jnz .Lparanoid_exit_no_swapgs
  1140. TRACE_IRQS_IRETQ
  1141. RESTORE_CR3 scratch_reg=%rbx save_reg=%r14
  1142. SWAPGS_UNSAFE_STACK
  1143. jmp .Lparanoid_exit_restore
  1144. .Lparanoid_exit_no_swapgs:
  1145. TRACE_IRQS_IRETQ_DEBUG
  1146. .Lparanoid_exit_restore:
  1147. jmp restore_regs_and_return_to_kernel
  1148. END(paranoid_exit)
  1149. /*
  1150. * Save all registers in pt_regs, and switch gs if needed.
  1151. * Return: EBX=0: came from user mode; EBX=1: otherwise
  1152. */
  1153. ENTRY(error_entry)
  1154. UNWIND_HINT_FUNC
  1155. cld
  1156. SAVE_C_REGS 8
  1157. SAVE_EXTRA_REGS 8
  1158. ENCODE_FRAME_POINTER 8
  1159. xorl %ebx, %ebx
  1160. testb $3, CS+8(%rsp)
  1161. jz .Lerror_kernelspace
  1162. /*
  1163. * We entered from user mode or we're pretending to have entered
  1164. * from user mode due to an IRET fault.
  1165. */
  1166. SWAPGS
  1167. /* We have user CR3. Change to kernel CR3. */
  1168. SWITCH_TO_KERNEL_CR3 scratch_reg=%rax
  1169. .Lerror_entry_from_usermode_after_swapgs:
  1170. /* Put us onto the real thread stack. */
  1171. popq %r12 /* save return addr in %12 */
  1172. movq %rsp, %rdi /* arg0 = pt_regs pointer */
  1173. call sync_regs
  1174. movq %rax, %rsp /* switch stack */
  1175. ENCODE_FRAME_POINTER
  1176. pushq %r12
  1177. /*
  1178. * We need to tell lockdep that IRQs are off. We can't do this until
  1179. * we fix gsbase, and we should do it before enter_from_user_mode
  1180. * (which can take locks).
  1181. */
  1182. TRACE_IRQS_OFF
  1183. CALL_enter_from_user_mode
  1184. ret
  1185. .Lerror_entry_done:
  1186. TRACE_IRQS_OFF
  1187. ret
  1188. /*
  1189. * There are two places in the kernel that can potentially fault with
  1190. * usergs. Handle them here. B stepping K8s sometimes report a
  1191. * truncated RIP for IRET exceptions returning to compat mode. Check
  1192. * for these here too.
  1193. */
  1194. .Lerror_kernelspace:
  1195. incl %ebx
  1196. leaq native_irq_return_iret(%rip), %rcx
  1197. cmpq %rcx, RIP+8(%rsp)
  1198. je .Lerror_bad_iret
  1199. movl %ecx, %eax /* zero extend */
  1200. cmpq %rax, RIP+8(%rsp)
  1201. je .Lbstep_iret
  1202. cmpq $.Lgs_change, RIP+8(%rsp)
  1203. jne .Lerror_entry_done
  1204. /*
  1205. * hack: .Lgs_change can fail with user gsbase. If this happens, fix up
  1206. * gsbase and proceed. We'll fix up the exception and land in
  1207. * .Lgs_change's error handler with kernel gsbase.
  1208. */
  1209. SWAPGS
  1210. SWITCH_TO_KERNEL_CR3 scratch_reg=%rax
  1211. jmp .Lerror_entry_done
  1212. .Lbstep_iret:
  1213. /* Fix truncated RIP */
  1214. movq %rcx, RIP+8(%rsp)
  1215. /* fall through */
  1216. .Lerror_bad_iret:
  1217. /*
  1218. * We came from an IRET to user mode, so we have user
  1219. * gsbase and CR3. Switch to kernel gsbase and CR3:
  1220. */
  1221. SWAPGS
  1222. SWITCH_TO_KERNEL_CR3 scratch_reg=%rax
  1223. /*
  1224. * Pretend that the exception came from user mode: set up pt_regs
  1225. * as if we faulted immediately after IRET and clear EBX so that
  1226. * error_exit knows that we will be returning to user mode.
  1227. */
  1228. mov %rsp, %rdi
  1229. call fixup_bad_iret
  1230. mov %rax, %rsp
  1231. decl %ebx
  1232. jmp .Lerror_entry_from_usermode_after_swapgs
  1233. END(error_entry)
  1234. /*
  1235. * On entry, EBX is a "return to kernel mode" flag:
  1236. * 1: already in kernel mode, don't need SWAPGS
  1237. * 0: user gsbase is loaded, we need SWAPGS and standard preparation for return to usermode
  1238. */
  1239. ENTRY(error_exit)
  1240. UNWIND_HINT_REGS
  1241. DISABLE_INTERRUPTS(CLBR_ANY)
  1242. TRACE_IRQS_OFF
  1243. testl %ebx, %ebx
  1244. jnz retint_kernel
  1245. jmp retint_user
  1246. END(error_exit)
  1247. /*
  1248. * Runs on exception stack. Xen PV does not go through this path at all,
  1249. * so we can use real assembly here.
  1250. *
  1251. * Registers:
  1252. * %r14: Used to save/restore the CR3 of the interrupted context
  1253. * when PAGE_TABLE_ISOLATION is in use. Do not clobber.
  1254. */
  1255. ENTRY(nmi)
  1256. UNWIND_HINT_IRET_REGS
  1257. /*
  1258. * We allow breakpoints in NMIs. If a breakpoint occurs, then
  1259. * the iretq it performs will take us out of NMI context.
  1260. * This means that we can have nested NMIs where the next
  1261. * NMI is using the top of the stack of the previous NMI. We
  1262. * can't let it execute because the nested NMI will corrupt the
  1263. * stack of the previous NMI. NMI handlers are not re-entrant
  1264. * anyway.
  1265. *
  1266. * To handle this case we do the following:
  1267. * Check the a special location on the stack that contains
  1268. * a variable that is set when NMIs are executing.
  1269. * The interrupted task's stack is also checked to see if it
  1270. * is an NMI stack.
  1271. * If the variable is not set and the stack is not the NMI
  1272. * stack then:
  1273. * o Set the special variable on the stack
  1274. * o Copy the interrupt frame into an "outermost" location on the
  1275. * stack
  1276. * o Copy the interrupt frame into an "iret" location on the stack
  1277. * o Continue processing the NMI
  1278. * If the variable is set or the previous stack is the NMI stack:
  1279. * o Modify the "iret" location to jump to the repeat_nmi
  1280. * o return back to the first NMI
  1281. *
  1282. * Now on exit of the first NMI, we first clear the stack variable
  1283. * The NMI stack will tell any nested NMIs at that point that it is
  1284. * nested. Then we pop the stack normally with iret, and if there was
  1285. * a nested NMI that updated the copy interrupt stack frame, a
  1286. * jump will be made to the repeat_nmi code that will handle the second
  1287. * NMI.
  1288. *
  1289. * However, espfix prevents us from directly returning to userspace
  1290. * with a single IRET instruction. Similarly, IRET to user mode
  1291. * can fault. We therefore handle NMIs from user space like
  1292. * other IST entries.
  1293. */
  1294. ASM_CLAC
  1295. /* Use %rdx as our temp variable throughout */
  1296. pushq %rdx
  1297. testb $3, CS-RIP+8(%rsp)
  1298. jz .Lnmi_from_kernel
  1299. /*
  1300. * NMI from user mode. We need to run on the thread stack, but we
  1301. * can't go through the normal entry paths: NMIs are masked, and
  1302. * we don't want to enable interrupts, because then we'll end
  1303. * up in an awkward situation in which IRQs are on but NMIs
  1304. * are off.
  1305. *
  1306. * We also must not push anything to the stack before switching
  1307. * stacks lest we corrupt the "NMI executing" variable.
  1308. */
  1309. swapgs
  1310. cld
  1311. SWITCH_TO_KERNEL_CR3 scratch_reg=%rdx
  1312. movq %rsp, %rdx
  1313. movq PER_CPU_VAR(cpu_current_top_of_stack), %rsp
  1314. UNWIND_HINT_IRET_REGS base=%rdx offset=8
  1315. pushq 5*8(%rdx) /* pt_regs->ss */
  1316. pushq 4*8(%rdx) /* pt_regs->rsp */
  1317. pushq 3*8(%rdx) /* pt_regs->flags */
  1318. pushq 2*8(%rdx) /* pt_regs->cs */
  1319. pushq 1*8(%rdx) /* pt_regs->rip */
  1320. UNWIND_HINT_IRET_REGS
  1321. pushq $-1 /* pt_regs->orig_ax */
  1322. pushq %rdi /* pt_regs->di */
  1323. pushq %rsi /* pt_regs->si */
  1324. pushq (%rdx) /* pt_regs->dx */
  1325. pushq %rcx /* pt_regs->cx */
  1326. pushq %rax /* pt_regs->ax */
  1327. pushq %r8 /* pt_regs->r8 */
  1328. pushq %r9 /* pt_regs->r9 */
  1329. pushq %r10 /* pt_regs->r10 */
  1330. pushq %r11 /* pt_regs->r11 */
  1331. pushq %rbx /* pt_regs->rbx */
  1332. pushq %rbp /* pt_regs->rbp */
  1333. pushq %r12 /* pt_regs->r12 */
  1334. pushq %r13 /* pt_regs->r13 */
  1335. pushq %r14 /* pt_regs->r14 */
  1336. pushq %r15 /* pt_regs->r15 */
  1337. UNWIND_HINT_REGS
  1338. ENCODE_FRAME_POINTER
  1339. /*
  1340. * At this point we no longer need to worry about stack damage
  1341. * due to nesting -- we're on the normal thread stack and we're
  1342. * done with the NMI stack.
  1343. */
  1344. movq %rsp, %rdi
  1345. movq $-1, %rsi
  1346. call do_nmi
  1347. /*
  1348. * Return back to user mode. We must *not* do the normal exit
  1349. * work, because we don't want to enable interrupts.
  1350. */
  1351. jmp swapgs_restore_regs_and_return_to_usermode
  1352. .Lnmi_from_kernel:
  1353. /*
  1354. * Here's what our stack frame will look like:
  1355. * +---------------------------------------------------------+
  1356. * | original SS |
  1357. * | original Return RSP |
  1358. * | original RFLAGS |
  1359. * | original CS |
  1360. * | original RIP |
  1361. * +---------------------------------------------------------+
  1362. * | temp storage for rdx |
  1363. * +---------------------------------------------------------+
  1364. * | "NMI executing" variable |
  1365. * +---------------------------------------------------------+
  1366. * | iret SS } Copied from "outermost" frame |
  1367. * | iret Return RSP } on each loop iteration; overwritten |
  1368. * | iret RFLAGS } by a nested NMI to force another |
  1369. * | iret CS } iteration if needed. |
  1370. * | iret RIP } |
  1371. * +---------------------------------------------------------+
  1372. * | outermost SS } initialized in first_nmi; |
  1373. * | outermost Return RSP } will not be changed before |
  1374. * | outermost RFLAGS } NMI processing is done. |
  1375. * | outermost CS } Copied to "iret" frame on each |
  1376. * | outermost RIP } iteration. |
  1377. * +---------------------------------------------------------+
  1378. * | pt_regs |
  1379. * +---------------------------------------------------------+
  1380. *
  1381. * The "original" frame is used by hardware. Before re-enabling
  1382. * NMIs, we need to be done with it, and we need to leave enough
  1383. * space for the asm code here.
  1384. *
  1385. * We return by executing IRET while RSP points to the "iret" frame.
  1386. * That will either return for real or it will loop back into NMI
  1387. * processing.
  1388. *
  1389. * The "outermost" frame is copied to the "iret" frame on each
  1390. * iteration of the loop, so each iteration starts with the "iret"
  1391. * frame pointing to the final return target.
  1392. */
  1393. /*
  1394. * Determine whether we're a nested NMI.
  1395. *
  1396. * If we interrupted kernel code between repeat_nmi and
  1397. * end_repeat_nmi, then we are a nested NMI. We must not
  1398. * modify the "iret" frame because it's being written by
  1399. * the outer NMI. That's okay; the outer NMI handler is
  1400. * about to about to call do_nmi anyway, so we can just
  1401. * resume the outer NMI.
  1402. */
  1403. movq $repeat_nmi, %rdx
  1404. cmpq 8(%rsp), %rdx
  1405. ja 1f
  1406. movq $end_repeat_nmi, %rdx
  1407. cmpq 8(%rsp), %rdx
  1408. ja nested_nmi_out
  1409. 1:
  1410. /*
  1411. * Now check "NMI executing". If it's set, then we're nested.
  1412. * This will not detect if we interrupted an outer NMI just
  1413. * before IRET.
  1414. */
  1415. cmpl $1, -8(%rsp)
  1416. je nested_nmi
  1417. /*
  1418. * Now test if the previous stack was an NMI stack. This covers
  1419. * the case where we interrupt an outer NMI after it clears
  1420. * "NMI executing" but before IRET. We need to be careful, though:
  1421. * there is one case in which RSP could point to the NMI stack
  1422. * despite there being no NMI active: naughty userspace controls
  1423. * RSP at the very beginning of the SYSCALL targets. We can
  1424. * pull a fast one on naughty userspace, though: we program
  1425. * SYSCALL to mask DF, so userspace cannot cause DF to be set
  1426. * if it controls the kernel's RSP. We set DF before we clear
  1427. * "NMI executing".
  1428. */
  1429. lea 6*8(%rsp), %rdx
  1430. /* Compare the NMI stack (rdx) with the stack we came from (4*8(%rsp)) */
  1431. cmpq %rdx, 4*8(%rsp)
  1432. /* If the stack pointer is above the NMI stack, this is a normal NMI */
  1433. ja first_nmi
  1434. subq $EXCEPTION_STKSZ, %rdx
  1435. cmpq %rdx, 4*8(%rsp)
  1436. /* If it is below the NMI stack, it is a normal NMI */
  1437. jb first_nmi
  1438. /* Ah, it is within the NMI stack. */
  1439. testb $(X86_EFLAGS_DF >> 8), (3*8 + 1)(%rsp)
  1440. jz first_nmi /* RSP was user controlled. */
  1441. /* This is a nested NMI. */
  1442. nested_nmi:
  1443. /*
  1444. * Modify the "iret" frame to point to repeat_nmi, forcing another
  1445. * iteration of NMI handling.
  1446. */
  1447. subq $8, %rsp
  1448. leaq -10*8(%rsp), %rdx
  1449. pushq $__KERNEL_DS
  1450. pushq %rdx
  1451. pushfq
  1452. pushq $__KERNEL_CS
  1453. pushq $repeat_nmi
  1454. /* Put stack back */
  1455. addq $(6*8), %rsp
  1456. nested_nmi_out:
  1457. popq %rdx
  1458. /* We are returning to kernel mode, so this cannot result in a fault. */
  1459. iretq
  1460. first_nmi:
  1461. /* Restore rdx. */
  1462. movq (%rsp), %rdx
  1463. /* Make room for "NMI executing". */
  1464. pushq $0
  1465. /* Leave room for the "iret" frame */
  1466. subq $(5*8), %rsp
  1467. /* Copy the "original" frame to the "outermost" frame */
  1468. .rept 5
  1469. pushq 11*8(%rsp)
  1470. .endr
  1471. UNWIND_HINT_IRET_REGS
  1472. /* Everything up to here is safe from nested NMIs */
  1473. #ifdef CONFIG_DEBUG_ENTRY
  1474. /*
  1475. * For ease of testing, unmask NMIs right away. Disabled by
  1476. * default because IRET is very expensive.
  1477. */
  1478. pushq $0 /* SS */
  1479. pushq %rsp /* RSP (minus 8 because of the previous push) */
  1480. addq $8, (%rsp) /* Fix up RSP */
  1481. pushfq /* RFLAGS */
  1482. pushq $__KERNEL_CS /* CS */
  1483. pushq $1f /* RIP */
  1484. iretq /* continues at repeat_nmi below */
  1485. UNWIND_HINT_IRET_REGS
  1486. 1:
  1487. #endif
  1488. repeat_nmi:
  1489. /*
  1490. * If there was a nested NMI, the first NMI's iret will return
  1491. * here. But NMIs are still enabled and we can take another
  1492. * nested NMI. The nested NMI checks the interrupted RIP to see
  1493. * if it is between repeat_nmi and end_repeat_nmi, and if so
  1494. * it will just return, as we are about to repeat an NMI anyway.
  1495. * This makes it safe to copy to the stack frame that a nested
  1496. * NMI will update.
  1497. *
  1498. * RSP is pointing to "outermost RIP". gsbase is unknown, but, if
  1499. * we're repeating an NMI, gsbase has the same value that it had on
  1500. * the first iteration. paranoid_entry will load the kernel
  1501. * gsbase if needed before we call do_nmi. "NMI executing"
  1502. * is zero.
  1503. */
  1504. movq $1, 10*8(%rsp) /* Set "NMI executing". */
  1505. /*
  1506. * Copy the "outermost" frame to the "iret" frame. NMIs that nest
  1507. * here must not modify the "iret" frame while we're writing to
  1508. * it or it will end up containing garbage.
  1509. */
  1510. addq $(10*8), %rsp
  1511. .rept 5
  1512. pushq -6*8(%rsp)
  1513. .endr
  1514. subq $(5*8), %rsp
  1515. end_repeat_nmi:
  1516. /*
  1517. * Everything below this point can be preempted by a nested NMI.
  1518. * If this happens, then the inner NMI will change the "iret"
  1519. * frame to point back to repeat_nmi.
  1520. */
  1521. pushq $-1 /* ORIG_RAX: no syscall to restart */
  1522. ALLOC_PT_GPREGS_ON_STACK
  1523. /*
  1524. * Use paranoid_entry to handle SWAPGS, but no need to use paranoid_exit
  1525. * as we should not be calling schedule in NMI context.
  1526. * Even with normal interrupts enabled. An NMI should not be
  1527. * setting NEED_RESCHED or anything that normal interrupts and
  1528. * exceptions might do.
  1529. */
  1530. call paranoid_entry
  1531. UNWIND_HINT_REGS
  1532. /* paranoidentry do_nmi, 0; without TRACE_IRQS_OFF */
  1533. movq %rsp, %rdi
  1534. movq $-1, %rsi
  1535. call do_nmi
  1536. RESTORE_CR3 scratch_reg=%r15 save_reg=%r14
  1537. testl %ebx, %ebx /* swapgs needed? */
  1538. jnz nmi_restore
  1539. nmi_swapgs:
  1540. SWAPGS_UNSAFE_STACK
  1541. nmi_restore:
  1542. POP_EXTRA_REGS
  1543. POP_C_REGS
  1544. /*
  1545. * Skip orig_ax and the "outermost" frame to point RSP at the "iret"
  1546. * at the "iret" frame.
  1547. */
  1548. addq $6*8, %rsp
  1549. /*
  1550. * Clear "NMI executing". Set DF first so that we can easily
  1551. * distinguish the remaining code between here and IRET from
  1552. * the SYSCALL entry and exit paths.
  1553. *
  1554. * We arguably should just inspect RIP instead, but I (Andy) wrote
  1555. * this code when I had the misapprehension that Xen PV supported
  1556. * NMIs, and Xen PV would break that approach.
  1557. */
  1558. std
  1559. movq $0, 5*8(%rsp) /* clear "NMI executing" */
  1560. /*
  1561. * iretq reads the "iret" frame and exits the NMI stack in a
  1562. * single instruction. We are returning to kernel mode, so this
  1563. * cannot result in a fault. Similarly, we don't need to worry
  1564. * about espfix64 on the way back to kernel mode.
  1565. */
  1566. iretq
  1567. END(nmi)
  1568. ENTRY(ignore_sysret)
  1569. UNWIND_HINT_EMPTY
  1570. mov $-ENOSYS, %eax
  1571. sysret
  1572. END(ignore_sysret)
  1573. ENTRY(rewind_stack_do_exit)
  1574. UNWIND_HINT_FUNC
  1575. /* Prevent any naive code from trying to unwind to our caller. */
  1576. xorl %ebp, %ebp
  1577. movq PER_CPU_VAR(cpu_current_top_of_stack), %rax
  1578. leaq -PTREGS_SIZE(%rax), %rsp
  1579. UNWIND_HINT_FUNC sp_offset=PTREGS_SIZE
  1580. call do_exit
  1581. END(rewind_stack_do_exit)