entry_64.S 39 KB

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