entry_64.S 47 KB

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