entry_64.S 49 KB

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