entry_64.S 42 KB

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