entry_64.S 47 KB

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