entry_64.S 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451
  1. /*
  2. * linux/arch/x86_64/entry.S
  3. *
  4. * Copyright (C) 1991, 1992 Linus Torvalds
  5. * Copyright (C) 2000, 2001, 2002 Andi Kleen SuSE Labs
  6. * Copyright (C) 2000 Pavel Machek <pavel@suse.cz>
  7. *
  8. * entry.S contains the system-call and fault low-level handling routines.
  9. *
  10. * Some of this is documented in Documentation/x86/entry_64.txt
  11. *
  12. * A note on terminology:
  13. * - iret frame: Architecture defined interrupt frame from SS to RIP
  14. * at the top of the kernel process stack.
  15. *
  16. * Some macro usage:
  17. * - ENTRY/END: Define functions in the symbol table.
  18. * - TRACE_IRQ_*: Trace hardirq state for lock debugging.
  19. * - idtentry: Define exception entry points.
  20. */
  21. #include <linux/linkage.h>
  22. #include <asm/segment.h>
  23. #include <asm/cache.h>
  24. #include <asm/errno.h>
  25. #include "calling.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 <linux/err.h>
  39. /* Avoid __ASSEMBLER__'ifying <linux/audit.h> just for this. */
  40. #include <linux/elf-em.h>
  41. #define AUDIT_ARCH_X86_64 (EM_X86_64|__AUDIT_ARCH_64BIT|__AUDIT_ARCH_LE)
  42. #define __AUDIT_ARCH_64BIT 0x80000000
  43. #define __AUDIT_ARCH_LE 0x40000000
  44. .code64
  45. .section .entry.text, "ax"
  46. #ifdef CONFIG_PARAVIRT
  47. ENTRY(native_usergs_sysret64)
  48. swapgs
  49. sysretq
  50. ENDPROC(native_usergs_sysret64)
  51. #endif /* CONFIG_PARAVIRT */
  52. .macro TRACE_IRQS_IRETQ
  53. #ifdef CONFIG_TRACE_IRQFLAGS
  54. bt $9, EFLAGS(%rsp) /* interrupts off? */
  55. jnc 1f
  56. TRACE_IRQS_ON
  57. 1:
  58. #endif
  59. .endm
  60. /*
  61. * When dynamic function tracer is enabled it will add a breakpoint
  62. * to all locations that it is about to modify, sync CPUs, update
  63. * all the code, sync CPUs, then remove the breakpoints. In this time
  64. * if lockdep is enabled, it might jump back into the debug handler
  65. * outside the updating of the IST protection. (TRACE_IRQS_ON/OFF).
  66. *
  67. * We need to change the IDT table before calling TRACE_IRQS_ON/OFF to
  68. * make sure the stack pointer does not get reset back to the top
  69. * of the debug stack, and instead just reuses the current stack.
  70. */
  71. #if defined(CONFIG_DYNAMIC_FTRACE) && defined(CONFIG_TRACE_IRQFLAGS)
  72. .macro TRACE_IRQS_OFF_DEBUG
  73. call debug_stack_set_zero
  74. TRACE_IRQS_OFF
  75. call debug_stack_reset
  76. .endm
  77. .macro TRACE_IRQS_ON_DEBUG
  78. call debug_stack_set_zero
  79. TRACE_IRQS_ON
  80. call debug_stack_reset
  81. .endm
  82. .macro TRACE_IRQS_IRETQ_DEBUG
  83. bt $9, EFLAGS(%rsp) /* interrupts off? */
  84. jnc 1f
  85. TRACE_IRQS_ON_DEBUG
  86. 1:
  87. .endm
  88. #else
  89. # define TRACE_IRQS_OFF_DEBUG TRACE_IRQS_OFF
  90. # define TRACE_IRQS_ON_DEBUG TRACE_IRQS_ON
  91. # define TRACE_IRQS_IRETQ_DEBUG TRACE_IRQS_IRETQ
  92. #endif
  93. /*
  94. * 64-bit SYSCALL instruction entry. Up to 6 arguments in registers.
  95. *
  96. * This is the only entry point used for 64-bit system calls. The
  97. * hardware interface is reasonably well designed and the register to
  98. * argument mapping Linux uses fits well with the registers that are
  99. * available when SYSCALL is used.
  100. *
  101. * SYSCALL instructions can be found inlined in libc implementations as
  102. * well as some other programs and libraries. There are also a handful
  103. * of SYSCALL instructions in the vDSO used, for example, as a
  104. * clock_gettimeofday fallback.
  105. *
  106. * 64-bit SYSCALL saves rip to rcx, clears rflags.RF, then saves rflags to r11,
  107. * then loads new ss, cs, and rip from previously programmed MSRs.
  108. * rflags gets masked by a value from another MSR (so CLD and CLAC
  109. * are not needed). SYSCALL does not save anything on the stack
  110. * and does not change rsp.
  111. *
  112. * Registers on entry:
  113. * rax system call number
  114. * rcx return address
  115. * r11 saved rflags (note: r11 is callee-clobbered register in C ABI)
  116. * rdi arg0
  117. * rsi arg1
  118. * rdx arg2
  119. * r10 arg3 (needs to be moved to rcx to conform to C ABI)
  120. * r8 arg4
  121. * r9 arg5
  122. * (note: r12-r15, rbp, rbx are callee-preserved in C ABI)
  123. *
  124. * Only called from user space.
  125. *
  126. * When user can change pt_regs->foo always force IRET. That is because
  127. * it deals with uncanonical addresses better. SYSRET has trouble
  128. * with them due to bugs in both AMD and Intel CPUs.
  129. */
  130. ENTRY(entry_SYSCALL_64)
  131. /*
  132. * Interrupts are off on entry.
  133. * We do not frame this tiny irq-off block with TRACE_IRQS_OFF/ON,
  134. * it is too small to ever cause noticeable irq latency.
  135. */
  136. SWAPGS_UNSAFE_STACK
  137. /*
  138. * A hypervisor implementation might want to use a label
  139. * after the swapgs, so that it can do the swapgs
  140. * for the guest and jump here on syscall.
  141. */
  142. GLOBAL(entry_SYSCALL_64_after_swapgs)
  143. movq %rsp, PER_CPU_VAR(rsp_scratch)
  144. movq PER_CPU_VAR(cpu_current_top_of_stack), %rsp
  145. TRACE_IRQS_OFF
  146. /* Construct struct pt_regs on stack */
  147. pushq $__USER_DS /* pt_regs->ss */
  148. pushq PER_CPU_VAR(rsp_scratch) /* pt_regs->sp */
  149. pushq %r11 /* pt_regs->flags */
  150. pushq $__USER_CS /* pt_regs->cs */
  151. pushq %rcx /* pt_regs->ip */
  152. pushq %rax /* pt_regs->orig_ax */
  153. pushq %rdi /* pt_regs->di */
  154. pushq %rsi /* pt_regs->si */
  155. pushq %rdx /* pt_regs->dx */
  156. pushq %rcx /* pt_regs->cx */
  157. pushq $-ENOSYS /* pt_regs->ax */
  158. pushq %r8 /* pt_regs->r8 */
  159. pushq %r9 /* pt_regs->r9 */
  160. pushq %r10 /* pt_regs->r10 */
  161. pushq %r11 /* pt_regs->r11 */
  162. sub $(6*8), %rsp /* pt_regs->bp, bx, r12-15 not saved */
  163. /*
  164. * If we need to do entry work or if we guess we'll need to do
  165. * exit work, go straight to the slow path.
  166. */
  167. testl $_TIF_WORK_SYSCALL_ENTRY|_TIF_ALLWORK_MASK, ASM_THREAD_INFO(TI_flags, %rsp, SIZEOF_PTREGS)
  168. jnz entry_SYSCALL64_slow_path
  169. entry_SYSCALL_64_fastpath:
  170. /*
  171. * Easy case: enable interrupts and issue the syscall. If the syscall
  172. * needs pt_regs, we'll call a stub that disables interrupts again
  173. * and jumps to the slow path.
  174. */
  175. TRACE_IRQS_ON
  176. ENABLE_INTERRUPTS(CLBR_NONE)
  177. #if __SYSCALL_MASK == ~0
  178. cmpq $__NR_syscall_max, %rax
  179. #else
  180. andl $__SYSCALL_MASK, %eax
  181. cmpl $__NR_syscall_max, %eax
  182. #endif
  183. ja 1f /* return -ENOSYS (already in pt_regs->ax) */
  184. movq %r10, %rcx
  185. /*
  186. * This call instruction is handled specially in stub_ptregs_64.
  187. * It might end up jumping to the slow path. If it jumps, RAX
  188. * and all argument registers are clobbered.
  189. */
  190. call *sys_call_table(, %rax, 8)
  191. .Lentry_SYSCALL_64_after_fastpath_call:
  192. movq %rax, RAX(%rsp)
  193. 1:
  194. /*
  195. * If we get here, then we know that pt_regs is clean for SYSRET64.
  196. * If we see that no exit work is required (which we are required
  197. * to check with IRQs off), then we can go straight to SYSRET64.
  198. */
  199. DISABLE_INTERRUPTS(CLBR_NONE)
  200. TRACE_IRQS_OFF
  201. testl $_TIF_ALLWORK_MASK, ASM_THREAD_INFO(TI_flags, %rsp, SIZEOF_PTREGS)
  202. jnz 1f
  203. LOCKDEP_SYS_EXIT
  204. TRACE_IRQS_ON /* user mode is traced as IRQs on */
  205. movq RIP(%rsp), %rcx
  206. movq EFLAGS(%rsp), %r11
  207. RESTORE_C_REGS_EXCEPT_RCX_R11
  208. movq RSP(%rsp), %rsp
  209. USERGS_SYSRET64
  210. 1:
  211. /*
  212. * The fast path looked good when we started, but something changed
  213. * along the way and we need to switch to the slow path. Calling
  214. * raise(3) will trigger this, for example. IRQs are off.
  215. */
  216. TRACE_IRQS_ON
  217. ENABLE_INTERRUPTS(CLBR_NONE)
  218. SAVE_EXTRA_REGS
  219. movq %rsp, %rdi
  220. call syscall_return_slowpath /* returns with IRQs disabled */
  221. jmp return_from_SYSCALL_64
  222. entry_SYSCALL64_slow_path:
  223. /* IRQs are off. */
  224. SAVE_EXTRA_REGS
  225. movq %rsp, %rdi
  226. call do_syscall_64 /* returns with IRQs disabled */
  227. return_from_SYSCALL_64:
  228. RESTORE_EXTRA_REGS
  229. TRACE_IRQS_IRETQ /* we're about to change IF */
  230. /*
  231. * Try to use SYSRET instead of IRET if we're returning to
  232. * a completely clean 64-bit userspace context.
  233. */
  234. movq RCX(%rsp), %rcx
  235. movq RIP(%rsp), %r11
  236. cmpq %rcx, %r11 /* RCX == RIP */
  237. jne opportunistic_sysret_failed
  238. /*
  239. * On Intel CPUs, SYSRET with non-canonical RCX/RIP will #GP
  240. * in kernel space. This essentially lets the user take over
  241. * the kernel, since userspace controls RSP.
  242. *
  243. * If width of "canonical tail" ever becomes variable, this will need
  244. * to be updated to remain correct on both old and new CPUs.
  245. */
  246. .ifne __VIRTUAL_MASK_SHIFT - 47
  247. .error "virtual address width changed -- SYSRET checks need update"
  248. .endif
  249. /* Change top 16 bits to be the sign-extension of 47th bit */
  250. shl $(64 - (__VIRTUAL_MASK_SHIFT+1)), %rcx
  251. sar $(64 - (__VIRTUAL_MASK_SHIFT+1)), %rcx
  252. /* If this changed %rcx, it was not canonical */
  253. cmpq %rcx, %r11
  254. jne opportunistic_sysret_failed
  255. cmpq $__USER_CS, CS(%rsp) /* CS must match SYSRET */
  256. jne opportunistic_sysret_failed
  257. movq R11(%rsp), %r11
  258. cmpq %r11, EFLAGS(%rsp) /* R11 == RFLAGS */
  259. jne opportunistic_sysret_failed
  260. /*
  261. * SYSCALL clears RF when it saves RFLAGS in R11 and SYSRET cannot
  262. * restore RF properly. If the slowpath sets it for whatever reason, we
  263. * need to restore it correctly.
  264. *
  265. * SYSRET can restore TF, but unlike IRET, restoring TF results in a
  266. * trap from userspace immediately after SYSRET. This would cause an
  267. * infinite loop whenever #DB happens with register state that satisfies
  268. * the opportunistic SYSRET conditions. For example, single-stepping
  269. * this user code:
  270. *
  271. * movq $stuck_here, %rcx
  272. * pushfq
  273. * popq %r11
  274. * stuck_here:
  275. *
  276. * would never get past 'stuck_here'.
  277. */
  278. testq $(X86_EFLAGS_RF|X86_EFLAGS_TF), %r11
  279. jnz opportunistic_sysret_failed
  280. /* nothing to check for RSP */
  281. cmpq $__USER_DS, SS(%rsp) /* SS must match SYSRET */
  282. jne opportunistic_sysret_failed
  283. /*
  284. * We win! This label is here just for ease of understanding
  285. * perf profiles. Nothing jumps here.
  286. */
  287. syscall_return_via_sysret:
  288. /* rcx and r11 are already restored (see code above) */
  289. RESTORE_C_REGS_EXCEPT_RCX_R11
  290. movq RSP(%rsp), %rsp
  291. USERGS_SYSRET64
  292. opportunistic_sysret_failed:
  293. SWAPGS
  294. jmp restore_c_regs_and_iret
  295. END(entry_SYSCALL_64)
  296. ENTRY(stub_ptregs_64)
  297. /*
  298. * Syscalls marked as needing ptregs land here.
  299. * If we are on the fast path, we need to save the extra regs,
  300. * which we achieve by trying again on the slow path. If we are on
  301. * the slow path, the extra regs are already saved.
  302. *
  303. * RAX stores a pointer to the C function implementing the syscall.
  304. * IRQs are on.
  305. */
  306. cmpq $.Lentry_SYSCALL_64_after_fastpath_call, (%rsp)
  307. jne 1f
  308. /*
  309. * Called from fast path -- disable IRQs again, pop return address
  310. * and jump to slow path
  311. */
  312. DISABLE_INTERRUPTS(CLBR_NONE)
  313. TRACE_IRQS_OFF
  314. popq %rax
  315. jmp entry_SYSCALL64_slow_path
  316. 1:
  317. /* Called from C */
  318. jmp *%rax /* called from C */
  319. END(stub_ptregs_64)
  320. .macro ptregs_stub func
  321. ENTRY(ptregs_\func)
  322. leaq \func(%rip), %rax
  323. jmp stub_ptregs_64
  324. END(ptregs_\func)
  325. .endm
  326. /* Instantiate ptregs_stub for each ptregs-using syscall */
  327. #define __SYSCALL_64_QUAL_(sym)
  328. #define __SYSCALL_64_QUAL_ptregs(sym) ptregs_stub sym
  329. #define __SYSCALL_64(nr, sym, qual) __SYSCALL_64_QUAL_##qual(sym)
  330. #include <asm/syscalls_64.h>
  331. /*
  332. * A newly forked process directly context switches into this address.
  333. *
  334. * rdi: prev task we switched from
  335. */
  336. ENTRY(ret_from_fork)
  337. LOCK ; btr $TIF_FORK, TI_flags(%r8)
  338. call schedule_tail /* rdi: 'prev' task parameter */
  339. testb $3, CS(%rsp) /* from kernel_thread? */
  340. jnz 1f
  341. /*
  342. * We came from kernel_thread. This code path is quite twisted, and
  343. * someone should clean it up.
  344. *
  345. * copy_thread_tls stashes the function pointer in RBX and the
  346. * parameter to be passed in RBP. The called function is permitted
  347. * to call do_execve and thereby jump to user mode.
  348. */
  349. movq RBP(%rsp), %rdi
  350. call *RBX(%rsp)
  351. movl $0, RAX(%rsp)
  352. /*
  353. * Fall through as though we're exiting a syscall. This makes a
  354. * twisted sort of sense if we just called do_execve.
  355. */
  356. 1:
  357. movq %rsp, %rdi
  358. call syscall_return_slowpath /* returns with IRQs disabled */
  359. TRACE_IRQS_ON /* user mode is traced as IRQS on */
  360. SWAPGS
  361. jmp restore_regs_and_iret
  362. END(ret_from_fork)
  363. /*
  364. * Build the entry stubs with some assembler magic.
  365. * We pack 1 stub into every 8-byte block.
  366. */
  367. .align 8
  368. ENTRY(irq_entries_start)
  369. vector=FIRST_EXTERNAL_VECTOR
  370. .rept (FIRST_SYSTEM_VECTOR - FIRST_EXTERNAL_VECTOR)
  371. pushq $(~vector+0x80) /* Note: always in signed byte range */
  372. vector=vector+1
  373. jmp common_interrupt
  374. .align 8
  375. .endr
  376. END(irq_entries_start)
  377. /*
  378. * Interrupt entry/exit.
  379. *
  380. * Interrupt entry points save only callee clobbered registers in fast path.
  381. *
  382. * Entry runs with interrupts off.
  383. */
  384. /* 0(%rsp): ~(interrupt number) */
  385. .macro interrupt func
  386. cld
  387. ALLOC_PT_GPREGS_ON_STACK
  388. SAVE_C_REGS
  389. SAVE_EXTRA_REGS
  390. testb $3, CS(%rsp)
  391. jz 1f
  392. /*
  393. * IRQ from user mode. Switch to kernel gsbase and inform context
  394. * tracking that we're in kernel mode.
  395. */
  396. SWAPGS
  397. /*
  398. * We need to tell lockdep that IRQs are off. We can't do this until
  399. * we fix gsbase, and we should do it before enter_from_user_mode
  400. * (which can take locks). Since TRACE_IRQS_OFF idempotent,
  401. * the simplest way to handle it is to just call it twice if
  402. * we enter from user mode. There's no reason to optimize this since
  403. * TRACE_IRQS_OFF is a no-op if lockdep is off.
  404. */
  405. TRACE_IRQS_OFF
  406. CALL_enter_from_user_mode
  407. 1:
  408. /*
  409. * Save previous stack pointer, optionally switch to interrupt stack.
  410. * irq_count is used to check if a CPU is already on an interrupt stack
  411. * or not. While this is essentially redundant with preempt_count it is
  412. * a little cheaper to use a separate counter in the PDA (short of
  413. * moving irq_enter into assembly, which would be too much work)
  414. */
  415. movq %rsp, %rdi
  416. incl PER_CPU_VAR(irq_count)
  417. cmovzq PER_CPU_VAR(irq_stack_ptr), %rsp
  418. pushq %rdi
  419. /* We entered an interrupt context - irqs are off: */
  420. TRACE_IRQS_OFF
  421. call \func /* rdi points to pt_regs */
  422. .endm
  423. /*
  424. * The interrupt stubs push (~vector+0x80) onto the stack and
  425. * then jump to common_interrupt.
  426. */
  427. .p2align CONFIG_X86_L1_CACHE_SHIFT
  428. common_interrupt:
  429. ASM_CLAC
  430. addq $-0x80, (%rsp) /* Adjust vector to [-256, -1] range */
  431. interrupt do_IRQ
  432. /* 0(%rsp): old RSP */
  433. ret_from_intr:
  434. DISABLE_INTERRUPTS(CLBR_NONE)
  435. TRACE_IRQS_OFF
  436. decl PER_CPU_VAR(irq_count)
  437. /* Restore saved previous stack */
  438. popq %rsp
  439. testb $3, CS(%rsp)
  440. jz retint_kernel
  441. /* Interrupt came from user space */
  442. GLOBAL(retint_user)
  443. mov %rsp,%rdi
  444. call prepare_exit_to_usermode
  445. TRACE_IRQS_IRETQ
  446. SWAPGS
  447. jmp restore_regs_and_iret
  448. /* Returning to kernel space */
  449. retint_kernel:
  450. #ifdef CONFIG_PREEMPT
  451. /* Interrupts are off */
  452. /* Check if we need preemption */
  453. bt $9, EFLAGS(%rsp) /* were interrupts off? */
  454. jnc 1f
  455. 0: cmpl $0, PER_CPU_VAR(__preempt_count)
  456. jnz 1f
  457. call preempt_schedule_irq
  458. jmp 0b
  459. 1:
  460. #endif
  461. /*
  462. * The iretq could re-enable interrupts:
  463. */
  464. TRACE_IRQS_IRETQ
  465. /*
  466. * At this label, code paths which return to kernel and to user,
  467. * which come from interrupts/exception and from syscalls, merge.
  468. */
  469. GLOBAL(restore_regs_and_iret)
  470. RESTORE_EXTRA_REGS
  471. restore_c_regs_and_iret:
  472. RESTORE_C_REGS
  473. REMOVE_PT_GPREGS_FROM_STACK 8
  474. INTERRUPT_RETURN
  475. ENTRY(native_iret)
  476. /*
  477. * Are we returning to a stack segment from the LDT? Note: in
  478. * 64-bit mode SS:RSP on the exception stack is always valid.
  479. */
  480. #ifdef CONFIG_X86_ESPFIX64
  481. testb $4, (SS-RIP)(%rsp)
  482. jnz native_irq_return_ldt
  483. #endif
  484. .global native_irq_return_iret
  485. native_irq_return_iret:
  486. /*
  487. * This may fault. Non-paranoid faults on return to userspace are
  488. * handled by fixup_bad_iret. These include #SS, #GP, and #NP.
  489. * Double-faults due to espfix64 are handled in do_double_fault.
  490. * Other faults here are fatal.
  491. */
  492. iretq
  493. #ifdef CONFIG_X86_ESPFIX64
  494. native_irq_return_ldt:
  495. pushq %rax
  496. pushq %rdi
  497. SWAPGS
  498. movq PER_CPU_VAR(espfix_waddr), %rdi
  499. movq %rax, (0*8)(%rdi) /* RAX */
  500. movq (2*8)(%rsp), %rax /* RIP */
  501. movq %rax, (1*8)(%rdi)
  502. movq (3*8)(%rsp), %rax /* CS */
  503. movq %rax, (2*8)(%rdi)
  504. movq (4*8)(%rsp), %rax /* RFLAGS */
  505. movq %rax, (3*8)(%rdi)
  506. movq (6*8)(%rsp), %rax /* SS */
  507. movq %rax, (5*8)(%rdi)
  508. movq (5*8)(%rsp), %rax /* RSP */
  509. movq %rax, (4*8)(%rdi)
  510. andl $0xffff0000, %eax
  511. popq %rdi
  512. orq PER_CPU_VAR(espfix_stack), %rax
  513. SWAPGS
  514. movq %rax, %rsp
  515. popq %rax
  516. jmp native_irq_return_iret
  517. #endif
  518. END(common_interrupt)
  519. /*
  520. * APIC interrupts.
  521. */
  522. .macro apicinterrupt3 num sym do_sym
  523. ENTRY(\sym)
  524. ASM_CLAC
  525. pushq $~(\num)
  526. .Lcommon_\sym:
  527. interrupt \do_sym
  528. jmp ret_from_intr
  529. END(\sym)
  530. .endm
  531. #ifdef CONFIG_TRACING
  532. #define trace(sym) trace_##sym
  533. #define smp_trace(sym) smp_trace_##sym
  534. .macro trace_apicinterrupt num sym
  535. apicinterrupt3 \num trace(\sym) smp_trace(\sym)
  536. .endm
  537. #else
  538. .macro trace_apicinterrupt num sym do_sym
  539. .endm
  540. #endif
  541. /* Make sure APIC interrupt handlers end up in the irqentry section: */
  542. #if defined(CONFIG_FUNCTION_GRAPH_TRACER) || defined(CONFIG_KASAN)
  543. # define PUSH_SECTION_IRQENTRY .pushsection .irqentry.text, "ax"
  544. # define POP_SECTION_IRQENTRY .popsection
  545. #else
  546. # define PUSH_SECTION_IRQENTRY
  547. # define POP_SECTION_IRQENTRY
  548. #endif
  549. .macro apicinterrupt num sym do_sym
  550. PUSH_SECTION_IRQENTRY
  551. apicinterrupt3 \num \sym \do_sym
  552. trace_apicinterrupt \num \sym
  553. POP_SECTION_IRQENTRY
  554. .endm
  555. #ifdef CONFIG_SMP
  556. apicinterrupt3 IRQ_MOVE_CLEANUP_VECTOR irq_move_cleanup_interrupt smp_irq_move_cleanup_interrupt
  557. apicinterrupt3 REBOOT_VECTOR reboot_interrupt smp_reboot_interrupt
  558. #endif
  559. #ifdef CONFIG_X86_UV
  560. apicinterrupt3 UV_BAU_MESSAGE uv_bau_message_intr1 uv_bau_message_interrupt
  561. #endif
  562. apicinterrupt LOCAL_TIMER_VECTOR apic_timer_interrupt smp_apic_timer_interrupt
  563. apicinterrupt X86_PLATFORM_IPI_VECTOR x86_platform_ipi smp_x86_platform_ipi
  564. #ifdef CONFIG_HAVE_KVM
  565. apicinterrupt3 POSTED_INTR_VECTOR kvm_posted_intr_ipi smp_kvm_posted_intr_ipi
  566. apicinterrupt3 POSTED_INTR_WAKEUP_VECTOR kvm_posted_intr_wakeup_ipi smp_kvm_posted_intr_wakeup_ipi
  567. #endif
  568. #ifdef CONFIG_X86_MCE_THRESHOLD
  569. apicinterrupt THRESHOLD_APIC_VECTOR threshold_interrupt smp_threshold_interrupt
  570. #endif
  571. #ifdef CONFIG_X86_MCE_AMD
  572. apicinterrupt DEFERRED_ERROR_VECTOR deferred_error_interrupt smp_deferred_error_interrupt
  573. #endif
  574. #ifdef CONFIG_X86_THERMAL_VECTOR
  575. apicinterrupt THERMAL_APIC_VECTOR thermal_interrupt smp_thermal_interrupt
  576. #endif
  577. #ifdef CONFIG_SMP
  578. apicinterrupt CALL_FUNCTION_SINGLE_VECTOR call_function_single_interrupt smp_call_function_single_interrupt
  579. apicinterrupt CALL_FUNCTION_VECTOR call_function_interrupt smp_call_function_interrupt
  580. apicinterrupt RESCHEDULE_VECTOR reschedule_interrupt smp_reschedule_interrupt
  581. #endif
  582. apicinterrupt ERROR_APIC_VECTOR error_interrupt smp_error_interrupt
  583. apicinterrupt SPURIOUS_APIC_VECTOR spurious_interrupt smp_spurious_interrupt
  584. #ifdef CONFIG_IRQ_WORK
  585. apicinterrupt IRQ_WORK_VECTOR irq_work_interrupt smp_irq_work_interrupt
  586. #endif
  587. /*
  588. * Exception entry points.
  589. */
  590. #define CPU_TSS_IST(x) PER_CPU_VAR(cpu_tss) + (TSS_ist + ((x) - 1) * 8)
  591. .macro idtentry sym do_sym has_error_code:req paranoid=0 shift_ist=-1
  592. ENTRY(\sym)
  593. /* Sanity check */
  594. .if \shift_ist != -1 && \paranoid == 0
  595. .error "using shift_ist requires paranoid=1"
  596. .endif
  597. ASM_CLAC
  598. PARAVIRT_ADJUST_EXCEPTION_FRAME
  599. .ifeq \has_error_code
  600. pushq $-1 /* ORIG_RAX: no syscall to restart */
  601. .endif
  602. ALLOC_PT_GPREGS_ON_STACK
  603. .if \paranoid
  604. .if \paranoid == 1
  605. testb $3, CS(%rsp) /* If coming from userspace, switch stacks */
  606. jnz 1f
  607. .endif
  608. call paranoid_entry
  609. .else
  610. call error_entry
  611. .endif
  612. /* returned flag: ebx=0: need swapgs on exit, ebx=1: don't need it */
  613. .if \paranoid
  614. .if \shift_ist != -1
  615. TRACE_IRQS_OFF_DEBUG /* reload IDT in case of recursion */
  616. .else
  617. TRACE_IRQS_OFF
  618. .endif
  619. .endif
  620. movq %rsp, %rdi /* pt_regs pointer */
  621. .if \has_error_code
  622. movq ORIG_RAX(%rsp), %rsi /* get error code */
  623. movq $-1, ORIG_RAX(%rsp) /* no syscall to restart */
  624. .else
  625. xorl %esi, %esi /* no error code */
  626. .endif
  627. .if \shift_ist != -1
  628. subq $EXCEPTION_STKSZ, CPU_TSS_IST(\shift_ist)
  629. .endif
  630. call \do_sym
  631. .if \shift_ist != -1
  632. addq $EXCEPTION_STKSZ, CPU_TSS_IST(\shift_ist)
  633. .endif
  634. /* these procedures expect "no swapgs" flag in ebx */
  635. .if \paranoid
  636. jmp paranoid_exit
  637. .else
  638. jmp error_exit
  639. .endif
  640. .if \paranoid == 1
  641. /*
  642. * Paranoid entry from userspace. Switch stacks and treat it
  643. * as a normal entry. This means that paranoid handlers
  644. * run in real process context if user_mode(regs).
  645. */
  646. 1:
  647. call error_entry
  648. movq %rsp, %rdi /* pt_regs pointer */
  649. call sync_regs
  650. movq %rax, %rsp /* switch stack */
  651. movq %rsp, %rdi /* pt_regs pointer */
  652. .if \has_error_code
  653. movq ORIG_RAX(%rsp), %rsi /* get error code */
  654. movq $-1, ORIG_RAX(%rsp) /* no syscall to restart */
  655. .else
  656. xorl %esi, %esi /* no error code */
  657. .endif
  658. call \do_sym
  659. jmp error_exit /* %ebx: no swapgs flag */
  660. .endif
  661. END(\sym)
  662. .endm
  663. #ifdef CONFIG_TRACING
  664. .macro trace_idtentry sym do_sym has_error_code:req
  665. idtentry trace(\sym) trace(\do_sym) has_error_code=\has_error_code
  666. idtentry \sym \do_sym has_error_code=\has_error_code
  667. .endm
  668. #else
  669. .macro trace_idtentry sym do_sym has_error_code:req
  670. idtentry \sym \do_sym has_error_code=\has_error_code
  671. .endm
  672. #endif
  673. idtentry divide_error do_divide_error has_error_code=0
  674. idtentry overflow do_overflow has_error_code=0
  675. idtentry bounds do_bounds has_error_code=0
  676. idtentry invalid_op do_invalid_op has_error_code=0
  677. idtentry device_not_available do_device_not_available has_error_code=0
  678. idtentry double_fault do_double_fault has_error_code=1 paranoid=2
  679. idtentry coprocessor_segment_overrun do_coprocessor_segment_overrun has_error_code=0
  680. idtentry invalid_TSS do_invalid_TSS has_error_code=1
  681. idtentry segment_not_present do_segment_not_present has_error_code=1
  682. idtentry spurious_interrupt_bug do_spurious_interrupt_bug has_error_code=0
  683. idtentry coprocessor_error do_coprocessor_error has_error_code=0
  684. idtentry alignment_check do_alignment_check has_error_code=1
  685. idtentry simd_coprocessor_error do_simd_coprocessor_error has_error_code=0
  686. /*
  687. * Reload gs selector with exception handling
  688. * edi: new selector
  689. */
  690. ENTRY(native_load_gs_index)
  691. pushfq
  692. DISABLE_INTERRUPTS(CLBR_ANY & ~CLBR_RDI)
  693. SWAPGS
  694. .Lgs_change:
  695. movl %edi, %gs
  696. 2: ALTERNATIVE "", "mfence", X86_BUG_SWAPGS_FENCE
  697. SWAPGS
  698. popfq
  699. ret
  700. END(native_load_gs_index)
  701. _ASM_EXTABLE(.Lgs_change, bad_gs)
  702. .section .fixup, "ax"
  703. /* running with kernelgs */
  704. bad_gs:
  705. SWAPGS /* switch back to user gs */
  706. .macro ZAP_GS
  707. /* This can't be a string because the preprocessor needs to see it. */
  708. movl $__USER_DS, %eax
  709. movl %eax, %gs
  710. .endm
  711. ALTERNATIVE "", "ZAP_GS", X86_BUG_NULL_SEG
  712. xorl %eax, %eax
  713. movl %eax, %gs
  714. jmp 2b
  715. .previous
  716. /* Call softirq on interrupt stack. Interrupts are off. */
  717. ENTRY(do_softirq_own_stack)
  718. pushq %rbp
  719. mov %rsp, %rbp
  720. incl PER_CPU_VAR(irq_count)
  721. cmove PER_CPU_VAR(irq_stack_ptr), %rsp
  722. push %rbp /* frame pointer backlink */
  723. call __do_softirq
  724. leaveq
  725. decl PER_CPU_VAR(irq_count)
  726. ret
  727. END(do_softirq_own_stack)
  728. #ifdef CONFIG_XEN
  729. idtentry xen_hypervisor_callback xen_do_hypervisor_callback has_error_code=0
  730. /*
  731. * A note on the "critical region" in our callback handler.
  732. * We want to avoid stacking callback handlers due to events occurring
  733. * during handling of the last event. To do this, we keep events disabled
  734. * until we've done all processing. HOWEVER, we must enable events before
  735. * popping the stack frame (can't be done atomically) and so it would still
  736. * be possible to get enough handler activations to overflow the stack.
  737. * Although unlikely, bugs of that kind are hard to track down, so we'd
  738. * like to avoid the possibility.
  739. * So, on entry to the handler we detect whether we interrupted an
  740. * existing activation in its critical region -- if so, we pop the current
  741. * activation and restart the handler using the previous one.
  742. */
  743. ENTRY(xen_do_hypervisor_callback) /* do_hypervisor_callback(struct *pt_regs) */
  744. /*
  745. * Since we don't modify %rdi, evtchn_do_upall(struct *pt_regs) will
  746. * see the correct pointer to the pt_regs
  747. */
  748. movq %rdi, %rsp /* we don't return, adjust the stack frame */
  749. 11: incl PER_CPU_VAR(irq_count)
  750. movq %rsp, %rbp
  751. cmovzq PER_CPU_VAR(irq_stack_ptr), %rsp
  752. pushq %rbp /* frame pointer backlink */
  753. call xen_evtchn_do_upcall
  754. popq %rsp
  755. decl PER_CPU_VAR(irq_count)
  756. #ifndef CONFIG_PREEMPT
  757. call xen_maybe_preempt_hcall
  758. #endif
  759. jmp error_exit
  760. END(xen_do_hypervisor_callback)
  761. /*
  762. * Hypervisor uses this for application faults while it executes.
  763. * We get here for two reasons:
  764. * 1. Fault while reloading DS, ES, FS or GS
  765. * 2. Fault while executing IRET
  766. * Category 1 we do not need to fix up as Xen has already reloaded all segment
  767. * registers that could be reloaded and zeroed the others.
  768. * Category 2 we fix up by killing the current process. We cannot use the
  769. * normal Linux return path in this case because if we use the IRET hypercall
  770. * to pop the stack frame we end up in an infinite loop of failsafe callbacks.
  771. * We distinguish between categories by comparing each saved segment register
  772. * with its current contents: any discrepancy means we in category 1.
  773. */
  774. ENTRY(xen_failsafe_callback)
  775. movl %ds, %ecx
  776. cmpw %cx, 0x10(%rsp)
  777. jne 1f
  778. movl %es, %ecx
  779. cmpw %cx, 0x18(%rsp)
  780. jne 1f
  781. movl %fs, %ecx
  782. cmpw %cx, 0x20(%rsp)
  783. jne 1f
  784. movl %gs, %ecx
  785. cmpw %cx, 0x28(%rsp)
  786. jne 1f
  787. /* All segments match their saved values => Category 2 (Bad IRET). */
  788. movq (%rsp), %rcx
  789. movq 8(%rsp), %r11
  790. addq $0x30, %rsp
  791. pushq $0 /* RIP */
  792. pushq %r11
  793. pushq %rcx
  794. jmp general_protection
  795. 1: /* Segment mismatch => Category 1 (Bad segment). Retry the IRET. */
  796. movq (%rsp), %rcx
  797. movq 8(%rsp), %r11
  798. addq $0x30, %rsp
  799. pushq $-1 /* orig_ax = -1 => not a system call */
  800. ALLOC_PT_GPREGS_ON_STACK
  801. SAVE_C_REGS
  802. SAVE_EXTRA_REGS
  803. jmp error_exit
  804. END(xen_failsafe_callback)
  805. apicinterrupt3 HYPERVISOR_CALLBACK_VECTOR \
  806. xen_hvm_callback_vector xen_evtchn_do_upcall
  807. #endif /* CONFIG_XEN */
  808. #if IS_ENABLED(CONFIG_HYPERV)
  809. apicinterrupt3 HYPERVISOR_CALLBACK_VECTOR \
  810. hyperv_callback_vector hyperv_vector_handler
  811. #endif /* CONFIG_HYPERV */
  812. idtentry debug do_debug has_error_code=0 paranoid=1 shift_ist=DEBUG_STACK
  813. idtentry int3 do_int3 has_error_code=0 paranoid=1 shift_ist=DEBUG_STACK
  814. idtentry stack_segment do_stack_segment has_error_code=1
  815. #ifdef CONFIG_XEN
  816. idtentry xen_debug do_debug has_error_code=0
  817. idtentry xen_int3 do_int3 has_error_code=0
  818. idtentry xen_stack_segment do_stack_segment has_error_code=1
  819. #endif
  820. idtentry general_protection do_general_protection has_error_code=1
  821. trace_idtentry page_fault do_page_fault has_error_code=1
  822. #ifdef CONFIG_KVM_GUEST
  823. idtentry async_page_fault do_async_page_fault has_error_code=1
  824. #endif
  825. #ifdef CONFIG_X86_MCE
  826. idtentry machine_check has_error_code=0 paranoid=1 do_sym=*machine_check_vector(%rip)
  827. #endif
  828. /*
  829. * Save all registers in pt_regs, and switch gs if needed.
  830. * Use slow, but surefire "are we in kernel?" check.
  831. * Return: ebx=0: need swapgs on exit, ebx=1: otherwise
  832. */
  833. ENTRY(paranoid_entry)
  834. cld
  835. SAVE_C_REGS 8
  836. SAVE_EXTRA_REGS 8
  837. movl $1, %ebx
  838. movl $MSR_GS_BASE, %ecx
  839. rdmsr
  840. testl %edx, %edx
  841. js 1f /* negative -> in kernel */
  842. SWAPGS
  843. xorl %ebx, %ebx
  844. 1: ret
  845. END(paranoid_entry)
  846. /*
  847. * "Paranoid" exit path from exception stack. This is invoked
  848. * only on return from non-NMI IST interrupts that came
  849. * from kernel space.
  850. *
  851. * We may be returning to very strange contexts (e.g. very early
  852. * in syscall entry), so checking for preemption here would
  853. * be complicated. Fortunately, we there's no good reason
  854. * to try to handle preemption here.
  855. *
  856. * On entry, ebx is "no swapgs" flag (1: don't need swapgs, 0: need it)
  857. */
  858. ENTRY(paranoid_exit)
  859. DISABLE_INTERRUPTS(CLBR_NONE)
  860. TRACE_IRQS_OFF_DEBUG
  861. testl %ebx, %ebx /* swapgs needed? */
  862. jnz paranoid_exit_no_swapgs
  863. TRACE_IRQS_IRETQ
  864. SWAPGS_UNSAFE_STACK
  865. jmp paranoid_exit_restore
  866. paranoid_exit_no_swapgs:
  867. TRACE_IRQS_IRETQ_DEBUG
  868. paranoid_exit_restore:
  869. RESTORE_EXTRA_REGS
  870. RESTORE_C_REGS
  871. REMOVE_PT_GPREGS_FROM_STACK 8
  872. INTERRUPT_RETURN
  873. END(paranoid_exit)
  874. /*
  875. * Save all registers in pt_regs, and switch gs if needed.
  876. * Return: EBX=0: came from user mode; EBX=1: otherwise
  877. */
  878. ENTRY(error_entry)
  879. cld
  880. SAVE_C_REGS 8
  881. SAVE_EXTRA_REGS 8
  882. xorl %ebx, %ebx
  883. testb $3, CS+8(%rsp)
  884. jz .Lerror_kernelspace
  885. .Lerror_entry_from_usermode_swapgs:
  886. /*
  887. * We entered from user mode or we're pretending to have entered
  888. * from user mode due to an IRET fault.
  889. */
  890. SWAPGS
  891. .Lerror_entry_from_usermode_after_swapgs:
  892. /*
  893. * We need to tell lockdep that IRQs are off. We can't do this until
  894. * we fix gsbase, and we should do it before enter_from_user_mode
  895. * (which can take locks).
  896. */
  897. TRACE_IRQS_OFF
  898. CALL_enter_from_user_mode
  899. ret
  900. .Lerror_entry_done:
  901. TRACE_IRQS_OFF
  902. ret
  903. /*
  904. * There are two places in the kernel that can potentially fault with
  905. * usergs. Handle them here. B stepping K8s sometimes report a
  906. * truncated RIP for IRET exceptions returning to compat mode. Check
  907. * for these here too.
  908. */
  909. .Lerror_kernelspace:
  910. incl %ebx
  911. leaq native_irq_return_iret(%rip), %rcx
  912. cmpq %rcx, RIP+8(%rsp)
  913. je .Lerror_bad_iret
  914. movl %ecx, %eax /* zero extend */
  915. cmpq %rax, RIP+8(%rsp)
  916. je .Lbstep_iret
  917. cmpq $.Lgs_change, RIP+8(%rsp)
  918. jne .Lerror_entry_done
  919. /*
  920. * hack: .Lgs_change can fail with user gsbase. If this happens, fix up
  921. * gsbase and proceed. We'll fix up the exception and land in
  922. * .Lgs_change's error handler with kernel gsbase.
  923. */
  924. jmp .Lerror_entry_from_usermode_swapgs
  925. .Lbstep_iret:
  926. /* Fix truncated RIP */
  927. movq %rcx, RIP+8(%rsp)
  928. /* fall through */
  929. .Lerror_bad_iret:
  930. /*
  931. * We came from an IRET to user mode, so we have user gsbase.
  932. * Switch to kernel gsbase:
  933. */
  934. SWAPGS
  935. /*
  936. * Pretend that the exception came from user mode: set up pt_regs
  937. * as if we faulted immediately after IRET and clear EBX so that
  938. * error_exit knows that we will be returning to user mode.
  939. */
  940. mov %rsp, %rdi
  941. call fixup_bad_iret
  942. mov %rax, %rsp
  943. decl %ebx
  944. jmp .Lerror_entry_from_usermode_after_swapgs
  945. END(error_entry)
  946. /*
  947. * On entry, EBS is a "return to kernel mode" flag:
  948. * 1: already in kernel mode, don't need SWAPGS
  949. * 0: user gsbase is loaded, we need SWAPGS and standard preparation for return to usermode
  950. */
  951. ENTRY(error_exit)
  952. movl %ebx, %eax
  953. DISABLE_INTERRUPTS(CLBR_NONE)
  954. TRACE_IRQS_OFF
  955. testl %eax, %eax
  956. jnz retint_kernel
  957. jmp retint_user
  958. END(error_exit)
  959. /* Runs on exception stack */
  960. ENTRY(nmi)
  961. /*
  962. * Fix up the exception frame if we're on Xen.
  963. * PARAVIRT_ADJUST_EXCEPTION_FRAME is guaranteed to push at most
  964. * one value to the stack on native, so it may clobber the rdx
  965. * scratch slot, but it won't clobber any of the important
  966. * slots past it.
  967. *
  968. * Xen is a different story, because the Xen frame itself overlaps
  969. * the "NMI executing" variable.
  970. */
  971. PARAVIRT_ADJUST_EXCEPTION_FRAME
  972. /*
  973. * We allow breakpoints in NMIs. If a breakpoint occurs, then
  974. * the iretq it performs will take us out of NMI context.
  975. * This means that we can have nested NMIs where the next
  976. * NMI is using the top of the stack of the previous NMI. We
  977. * can't let it execute because the nested NMI will corrupt the
  978. * stack of the previous NMI. NMI handlers are not re-entrant
  979. * anyway.
  980. *
  981. * To handle this case we do the following:
  982. * Check the a special location on the stack that contains
  983. * a variable that is set when NMIs are executing.
  984. * The interrupted task's stack is also checked to see if it
  985. * is an NMI stack.
  986. * If the variable is not set and the stack is not the NMI
  987. * stack then:
  988. * o Set the special variable on the stack
  989. * o Copy the interrupt frame into an "outermost" location on the
  990. * stack
  991. * o Copy the interrupt frame into an "iret" location on the stack
  992. * o Continue processing the NMI
  993. * If the variable is set or the previous stack is the NMI stack:
  994. * o Modify the "iret" location to jump to the repeat_nmi
  995. * o return back to the first NMI
  996. *
  997. * Now on exit of the first NMI, we first clear the stack variable
  998. * The NMI stack will tell any nested NMIs at that point that it is
  999. * nested. Then we pop the stack normally with iret, and if there was
  1000. * a nested NMI that updated the copy interrupt stack frame, a
  1001. * jump will be made to the repeat_nmi code that will handle the second
  1002. * NMI.
  1003. *
  1004. * However, espfix prevents us from directly returning to userspace
  1005. * with a single IRET instruction. Similarly, IRET to user mode
  1006. * can fault. We therefore handle NMIs from user space like
  1007. * other IST entries.
  1008. */
  1009. /* Use %rdx as our temp variable throughout */
  1010. pushq %rdx
  1011. testb $3, CS-RIP+8(%rsp)
  1012. jz .Lnmi_from_kernel
  1013. /*
  1014. * NMI from user mode. We need to run on the thread stack, but we
  1015. * can't go through the normal entry paths: NMIs are masked, and
  1016. * we don't want to enable interrupts, because then we'll end
  1017. * up in an awkward situation in which IRQs are on but NMIs
  1018. * are off.
  1019. *
  1020. * We also must not push anything to the stack before switching
  1021. * stacks lest we corrupt the "NMI executing" variable.
  1022. */
  1023. SWAPGS_UNSAFE_STACK
  1024. cld
  1025. movq %rsp, %rdx
  1026. movq PER_CPU_VAR(cpu_current_top_of_stack), %rsp
  1027. pushq 5*8(%rdx) /* pt_regs->ss */
  1028. pushq 4*8(%rdx) /* pt_regs->rsp */
  1029. pushq 3*8(%rdx) /* pt_regs->flags */
  1030. pushq 2*8(%rdx) /* pt_regs->cs */
  1031. pushq 1*8(%rdx) /* pt_regs->rip */
  1032. pushq $-1 /* pt_regs->orig_ax */
  1033. pushq %rdi /* pt_regs->di */
  1034. pushq %rsi /* pt_regs->si */
  1035. pushq (%rdx) /* pt_regs->dx */
  1036. pushq %rcx /* pt_regs->cx */
  1037. pushq %rax /* pt_regs->ax */
  1038. pushq %r8 /* pt_regs->r8 */
  1039. pushq %r9 /* pt_regs->r9 */
  1040. pushq %r10 /* pt_regs->r10 */
  1041. pushq %r11 /* pt_regs->r11 */
  1042. pushq %rbx /* pt_regs->rbx */
  1043. pushq %rbp /* pt_regs->rbp */
  1044. pushq %r12 /* pt_regs->r12 */
  1045. pushq %r13 /* pt_regs->r13 */
  1046. pushq %r14 /* pt_regs->r14 */
  1047. pushq %r15 /* pt_regs->r15 */
  1048. /*
  1049. * At this point we no longer need to worry about stack damage
  1050. * due to nesting -- we're on the normal thread stack and we're
  1051. * done with the NMI stack.
  1052. */
  1053. movq %rsp, %rdi
  1054. movq $-1, %rsi
  1055. call do_nmi
  1056. /*
  1057. * Return back to user mode. We must *not* do the normal exit
  1058. * work, because we don't want to enable interrupts. Fortunately,
  1059. * do_nmi doesn't modify pt_regs.
  1060. */
  1061. SWAPGS
  1062. jmp restore_c_regs_and_iret
  1063. .Lnmi_from_kernel:
  1064. /*
  1065. * Here's what our stack frame will look like:
  1066. * +---------------------------------------------------------+
  1067. * | original SS |
  1068. * | original Return RSP |
  1069. * | original RFLAGS |
  1070. * | original CS |
  1071. * | original RIP |
  1072. * +---------------------------------------------------------+
  1073. * | temp storage for rdx |
  1074. * +---------------------------------------------------------+
  1075. * | "NMI executing" variable |
  1076. * +---------------------------------------------------------+
  1077. * | iret SS } Copied from "outermost" frame |
  1078. * | iret Return RSP } on each loop iteration; overwritten |
  1079. * | iret RFLAGS } by a nested NMI to force another |
  1080. * | iret CS } iteration if needed. |
  1081. * | iret RIP } |
  1082. * +---------------------------------------------------------+
  1083. * | outermost SS } initialized in first_nmi; |
  1084. * | outermost Return RSP } will not be changed before |
  1085. * | outermost RFLAGS } NMI processing is done. |
  1086. * | outermost CS } Copied to "iret" frame on each |
  1087. * | outermost RIP } iteration. |
  1088. * +---------------------------------------------------------+
  1089. * | pt_regs |
  1090. * +---------------------------------------------------------+
  1091. *
  1092. * The "original" frame is used by hardware. Before re-enabling
  1093. * NMIs, we need to be done with it, and we need to leave enough
  1094. * space for the asm code here.
  1095. *
  1096. * We return by executing IRET while RSP points to the "iret" frame.
  1097. * That will either return for real or it will loop back into NMI
  1098. * processing.
  1099. *
  1100. * The "outermost" frame is copied to the "iret" frame on each
  1101. * iteration of the loop, so each iteration starts with the "iret"
  1102. * frame pointing to the final return target.
  1103. */
  1104. /*
  1105. * Determine whether we're a nested NMI.
  1106. *
  1107. * If we interrupted kernel code between repeat_nmi and
  1108. * end_repeat_nmi, then we are a nested NMI. We must not
  1109. * modify the "iret" frame because it's being written by
  1110. * the outer NMI. That's okay; the outer NMI handler is
  1111. * about to about to call do_nmi anyway, so we can just
  1112. * resume the outer NMI.
  1113. */
  1114. movq $repeat_nmi, %rdx
  1115. cmpq 8(%rsp), %rdx
  1116. ja 1f
  1117. movq $end_repeat_nmi, %rdx
  1118. cmpq 8(%rsp), %rdx
  1119. ja nested_nmi_out
  1120. 1:
  1121. /*
  1122. * Now check "NMI executing". If it's set, then we're nested.
  1123. * This will not detect if we interrupted an outer NMI just
  1124. * before IRET.
  1125. */
  1126. cmpl $1, -8(%rsp)
  1127. je nested_nmi
  1128. /*
  1129. * Now test if the previous stack was an NMI stack. This covers
  1130. * the case where we interrupt an outer NMI after it clears
  1131. * "NMI executing" but before IRET. We need to be careful, though:
  1132. * there is one case in which RSP could point to the NMI stack
  1133. * despite there being no NMI active: naughty userspace controls
  1134. * RSP at the very beginning of the SYSCALL targets. We can
  1135. * pull a fast one on naughty userspace, though: we program
  1136. * SYSCALL to mask DF, so userspace cannot cause DF to be set
  1137. * if it controls the kernel's RSP. We set DF before we clear
  1138. * "NMI executing".
  1139. */
  1140. lea 6*8(%rsp), %rdx
  1141. /* Compare the NMI stack (rdx) with the stack we came from (4*8(%rsp)) */
  1142. cmpq %rdx, 4*8(%rsp)
  1143. /* If the stack pointer is above the NMI stack, this is a normal NMI */
  1144. ja first_nmi
  1145. subq $EXCEPTION_STKSZ, %rdx
  1146. cmpq %rdx, 4*8(%rsp)
  1147. /* If it is below the NMI stack, it is a normal NMI */
  1148. jb first_nmi
  1149. /* Ah, it is within the NMI stack. */
  1150. testb $(X86_EFLAGS_DF >> 8), (3*8 + 1)(%rsp)
  1151. jz first_nmi /* RSP was user controlled. */
  1152. /* This is a nested NMI. */
  1153. nested_nmi:
  1154. /*
  1155. * Modify the "iret" frame to point to repeat_nmi, forcing another
  1156. * iteration of NMI handling.
  1157. */
  1158. subq $8, %rsp
  1159. leaq -10*8(%rsp), %rdx
  1160. pushq $__KERNEL_DS
  1161. pushq %rdx
  1162. pushfq
  1163. pushq $__KERNEL_CS
  1164. pushq $repeat_nmi
  1165. /* Put stack back */
  1166. addq $(6*8), %rsp
  1167. nested_nmi_out:
  1168. popq %rdx
  1169. /* We are returning to kernel mode, so this cannot result in a fault. */
  1170. INTERRUPT_RETURN
  1171. first_nmi:
  1172. /* Restore rdx. */
  1173. movq (%rsp), %rdx
  1174. /* Make room for "NMI executing". */
  1175. pushq $0
  1176. /* Leave room for the "iret" frame */
  1177. subq $(5*8), %rsp
  1178. /* Copy the "original" frame to the "outermost" frame */
  1179. .rept 5
  1180. pushq 11*8(%rsp)
  1181. .endr
  1182. /* Everything up to here is safe from nested NMIs */
  1183. #ifdef CONFIG_DEBUG_ENTRY
  1184. /*
  1185. * For ease of testing, unmask NMIs right away. Disabled by
  1186. * default because IRET is very expensive.
  1187. */
  1188. pushq $0 /* SS */
  1189. pushq %rsp /* RSP (minus 8 because of the previous push) */
  1190. addq $8, (%rsp) /* Fix up RSP */
  1191. pushfq /* RFLAGS */
  1192. pushq $__KERNEL_CS /* CS */
  1193. pushq $1f /* RIP */
  1194. INTERRUPT_RETURN /* continues at repeat_nmi below */
  1195. 1:
  1196. #endif
  1197. repeat_nmi:
  1198. /*
  1199. * If there was a nested NMI, the first NMI's iret will return
  1200. * here. But NMIs are still enabled and we can take another
  1201. * nested NMI. The nested NMI checks the interrupted RIP to see
  1202. * if it is between repeat_nmi and end_repeat_nmi, and if so
  1203. * it will just return, as we are about to repeat an NMI anyway.
  1204. * This makes it safe to copy to the stack frame that a nested
  1205. * NMI will update.
  1206. *
  1207. * RSP is pointing to "outermost RIP". gsbase is unknown, but, if
  1208. * we're repeating an NMI, gsbase has the same value that it had on
  1209. * the first iteration. paranoid_entry will load the kernel
  1210. * gsbase if needed before we call do_nmi. "NMI executing"
  1211. * is zero.
  1212. */
  1213. movq $1, 10*8(%rsp) /* Set "NMI executing". */
  1214. /*
  1215. * Copy the "outermost" frame to the "iret" frame. NMIs that nest
  1216. * here must not modify the "iret" frame while we're writing to
  1217. * it or it will end up containing garbage.
  1218. */
  1219. addq $(10*8), %rsp
  1220. .rept 5
  1221. pushq -6*8(%rsp)
  1222. .endr
  1223. subq $(5*8), %rsp
  1224. end_repeat_nmi:
  1225. /*
  1226. * Everything below this point can be preempted by a nested NMI.
  1227. * If this happens, then the inner NMI will change the "iret"
  1228. * frame to point back to repeat_nmi.
  1229. */
  1230. pushq $-1 /* ORIG_RAX: no syscall to restart */
  1231. ALLOC_PT_GPREGS_ON_STACK
  1232. /*
  1233. * Use paranoid_entry to handle SWAPGS, but no need to use paranoid_exit
  1234. * as we should not be calling schedule in NMI context.
  1235. * Even with normal interrupts enabled. An NMI should not be
  1236. * setting NEED_RESCHED or anything that normal interrupts and
  1237. * exceptions might do.
  1238. */
  1239. call paranoid_entry
  1240. /* paranoidentry do_nmi, 0; without TRACE_IRQS_OFF */
  1241. movq %rsp, %rdi
  1242. movq $-1, %rsi
  1243. call do_nmi
  1244. testl %ebx, %ebx /* swapgs needed? */
  1245. jnz nmi_restore
  1246. nmi_swapgs:
  1247. SWAPGS_UNSAFE_STACK
  1248. nmi_restore:
  1249. RESTORE_EXTRA_REGS
  1250. RESTORE_C_REGS
  1251. /* Point RSP at the "iret" frame. */
  1252. REMOVE_PT_GPREGS_FROM_STACK 6*8
  1253. /*
  1254. * Clear "NMI executing". Set DF first so that we can easily
  1255. * distinguish the remaining code between here and IRET from
  1256. * the SYSCALL entry and exit paths. On a native kernel, we
  1257. * could just inspect RIP, but, on paravirt kernels,
  1258. * INTERRUPT_RETURN can translate into a jump into a
  1259. * hypercall page.
  1260. */
  1261. std
  1262. movq $0, 5*8(%rsp) /* clear "NMI executing" */
  1263. /*
  1264. * INTERRUPT_RETURN reads the "iret" frame and exits the NMI
  1265. * stack in a single instruction. We are returning to kernel
  1266. * mode, so this cannot result in a fault.
  1267. */
  1268. INTERRUPT_RETURN
  1269. END(nmi)
  1270. ENTRY(ignore_sysret)
  1271. mov $-ENOSYS, %eax
  1272. sysret
  1273. END(ignore_sysret)
  1274. ENTRY(rewind_stack_do_exit)
  1275. /* Prevent any naive code from trying to unwind to our caller. */
  1276. xorl %ebp, %ebp
  1277. movq PER_CPU_VAR(cpu_current_top_of_stack), %rax
  1278. leaq -TOP_OF_KERNEL_STACK_PADDING-PTREGS_SIZE(%rax), %rsp
  1279. call do_exit
  1280. 1: jmp 1b
  1281. END(rewind_stack_do_exit)