entry_64.S 43 KB

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