entry_64.S 41 KB

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