entry_64.S 41 KB

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