entry_64.S 46 KB

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