entry_64.S 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488
  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)
  358. .align 8
  359. GLOBAL(stub_x32_execve)
  360. call compat_sys_execve
  361. jmp return_from_execve
  362. END(stub_x32_execve)
  363. .align 8
  364. GLOBAL(stub_x32_execveat)
  365. call compat_sys_execveat
  366. jmp return_from_execve
  367. END(stub_x32_execveat)
  368. #endif
  369. /*
  370. * sigreturn is special because it needs to restore all registers on return.
  371. * This cannot be done with SYSRET, so use the IRET return path instead.
  372. */
  373. ENTRY(stub_rt_sigreturn)
  374. /*
  375. * SAVE_EXTRA_REGS result is not normally needed:
  376. * sigreturn overwrites all pt_regs->GPREGS.
  377. * But sigreturn can fail (!), and there is no easy way to detect that.
  378. * To make sure RESTORE_EXTRA_REGS doesn't restore garbage on error,
  379. * we SAVE_EXTRA_REGS here.
  380. */
  381. SAVE_EXTRA_REGS 8
  382. call sys_rt_sigreturn
  383. return_from_stub:
  384. addq $8, %rsp
  385. RESTORE_EXTRA_REGS
  386. movq %rax, RAX(%rsp)
  387. jmp int_ret_from_sys_call
  388. END(stub_rt_sigreturn)
  389. #ifdef CONFIG_X86_X32_ABI
  390. ENTRY(stub_x32_rt_sigreturn)
  391. SAVE_EXTRA_REGS 8
  392. call sys32_x32_rt_sigreturn
  393. jmp return_from_stub
  394. END(stub_x32_rt_sigreturn)
  395. #endif
  396. /*
  397. * A newly forked process directly context switches into this address.
  398. *
  399. * rdi: prev task we switched from
  400. */
  401. ENTRY(ret_from_fork)
  402. LOCK ; btr $TIF_FORK, TI_flags(%r8)
  403. pushq $0x0002
  404. popfq /* reset kernel eflags */
  405. call schedule_tail /* rdi: 'prev' task parameter */
  406. RESTORE_EXTRA_REGS
  407. testb $3, CS(%rsp) /* from kernel_thread? */
  408. /*
  409. * By the time we get here, we have no idea whether our pt_regs,
  410. * ti flags, and ti status came from the 64-bit SYSCALL fast path,
  411. * the slow path, or one of the 32-bit compat paths.
  412. * Use IRET code path to return, since it can safely handle
  413. * all of the above.
  414. */
  415. jnz int_ret_from_sys_call
  416. /*
  417. * We came from kernel_thread
  418. * nb: we depend on RESTORE_EXTRA_REGS above
  419. */
  420. movq %rbp, %rdi
  421. call *%rbx
  422. movl $0, RAX(%rsp)
  423. RESTORE_EXTRA_REGS
  424. jmp int_ret_from_sys_call
  425. END(ret_from_fork)
  426. /*
  427. * Build the entry stubs with some assembler magic.
  428. * We pack 1 stub into every 8-byte block.
  429. */
  430. .align 8
  431. ENTRY(irq_entries_start)
  432. vector=FIRST_EXTERNAL_VECTOR
  433. .rept (FIRST_SYSTEM_VECTOR - FIRST_EXTERNAL_VECTOR)
  434. pushq $(~vector+0x80) /* Note: always in signed byte range */
  435. vector=vector+1
  436. jmp common_interrupt
  437. .align 8
  438. .endr
  439. END(irq_entries_start)
  440. /*
  441. * Interrupt entry/exit.
  442. *
  443. * Interrupt entry points save only callee clobbered registers in fast path.
  444. *
  445. * Entry runs with interrupts off.
  446. */
  447. /* 0(%rsp): ~(interrupt number) */
  448. .macro interrupt func
  449. cld
  450. ALLOC_PT_GPREGS_ON_STACK
  451. SAVE_C_REGS
  452. SAVE_EXTRA_REGS
  453. testb $3, CS(%rsp)
  454. jz 1f
  455. /*
  456. * IRQ from user mode. Switch to kernel gsbase and inform context
  457. * tracking that we're in kernel mode.
  458. */
  459. SWAPGS
  460. /*
  461. * We need to tell lockdep that IRQs are off. We can't do this until
  462. * we fix gsbase, and we should do it before enter_from_user_mode
  463. * (which can take locks). Since TRACE_IRQS_OFF idempotent,
  464. * the simplest way to handle it is to just call it twice if
  465. * we enter from user mode. There's no reason to optimize this since
  466. * TRACE_IRQS_OFF is a no-op if lockdep is off.
  467. */
  468. TRACE_IRQS_OFF
  469. CALL_enter_from_user_mode
  470. 1:
  471. /*
  472. * Save previous stack pointer, optionally switch to interrupt stack.
  473. * irq_count is used to check if a CPU is already on an interrupt stack
  474. * or not. While this is essentially redundant with preempt_count it is
  475. * a little cheaper to use a separate counter in the PDA (short of
  476. * moving irq_enter into assembly, which would be too much work)
  477. */
  478. movq %rsp, %rdi
  479. incl PER_CPU_VAR(irq_count)
  480. cmovzq PER_CPU_VAR(irq_stack_ptr), %rsp
  481. pushq %rdi
  482. /* We entered an interrupt context - irqs are off: */
  483. TRACE_IRQS_OFF
  484. call \func /* rdi points to pt_regs */
  485. .endm
  486. /*
  487. * The interrupt stubs push (~vector+0x80) onto the stack and
  488. * then jump to common_interrupt.
  489. */
  490. .p2align CONFIG_X86_L1_CACHE_SHIFT
  491. common_interrupt:
  492. ASM_CLAC
  493. addq $-0x80, (%rsp) /* Adjust vector to [-256, -1] range */
  494. interrupt do_IRQ
  495. /* 0(%rsp): old RSP */
  496. ret_from_intr:
  497. DISABLE_INTERRUPTS(CLBR_NONE)
  498. TRACE_IRQS_OFF
  499. decl PER_CPU_VAR(irq_count)
  500. /* Restore saved previous stack */
  501. popq %rsp
  502. testb $3, CS(%rsp)
  503. jz retint_kernel
  504. /* Interrupt came from user space */
  505. GLOBAL(retint_user)
  506. mov %rsp,%rdi
  507. call prepare_exit_to_usermode
  508. TRACE_IRQS_IRETQ
  509. SWAPGS
  510. jmp restore_regs_and_iret
  511. /* Returning to kernel space */
  512. retint_kernel:
  513. #ifdef CONFIG_PREEMPT
  514. /* Interrupts are off */
  515. /* Check if we need preemption */
  516. bt $9, EFLAGS(%rsp) /* were interrupts off? */
  517. jnc 1f
  518. 0: cmpl $0, PER_CPU_VAR(__preempt_count)
  519. jnz 1f
  520. call preempt_schedule_irq
  521. jmp 0b
  522. 1:
  523. #endif
  524. /*
  525. * The iretq could re-enable interrupts:
  526. */
  527. TRACE_IRQS_IRETQ
  528. /*
  529. * At this label, code paths which return to kernel and to user,
  530. * which come from interrupts/exception and from syscalls, merge.
  531. */
  532. GLOBAL(restore_regs_and_iret)
  533. RESTORE_EXTRA_REGS
  534. restore_c_regs_and_iret:
  535. RESTORE_C_REGS
  536. REMOVE_PT_GPREGS_FROM_STACK 8
  537. INTERRUPT_RETURN
  538. ENTRY(native_iret)
  539. /*
  540. * Are we returning to a stack segment from the LDT? Note: in
  541. * 64-bit mode SS:RSP on the exception stack is always valid.
  542. */
  543. #ifdef CONFIG_X86_ESPFIX64
  544. testb $4, (SS-RIP)(%rsp)
  545. jnz native_irq_return_ldt
  546. #endif
  547. .global native_irq_return_iret
  548. native_irq_return_iret:
  549. /*
  550. * This may fault. Non-paranoid faults on return to userspace are
  551. * handled by fixup_bad_iret. These include #SS, #GP, and #NP.
  552. * Double-faults due to espfix64 are handled in do_double_fault.
  553. * Other faults here are fatal.
  554. */
  555. iretq
  556. #ifdef CONFIG_X86_ESPFIX64
  557. native_irq_return_ldt:
  558. pushq %rax
  559. pushq %rdi
  560. SWAPGS
  561. movq PER_CPU_VAR(espfix_waddr), %rdi
  562. movq %rax, (0*8)(%rdi) /* RAX */
  563. movq (2*8)(%rsp), %rax /* RIP */
  564. movq %rax, (1*8)(%rdi)
  565. movq (3*8)(%rsp), %rax /* CS */
  566. movq %rax, (2*8)(%rdi)
  567. movq (4*8)(%rsp), %rax /* RFLAGS */
  568. movq %rax, (3*8)(%rdi)
  569. movq (6*8)(%rsp), %rax /* SS */
  570. movq %rax, (5*8)(%rdi)
  571. movq (5*8)(%rsp), %rax /* RSP */
  572. movq %rax, (4*8)(%rdi)
  573. andl $0xffff0000, %eax
  574. popq %rdi
  575. orq PER_CPU_VAR(espfix_stack), %rax
  576. SWAPGS
  577. movq %rax, %rsp
  578. popq %rax
  579. jmp native_irq_return_iret
  580. #endif
  581. END(common_interrupt)
  582. /*
  583. * APIC interrupts.
  584. */
  585. .macro apicinterrupt3 num sym do_sym
  586. ENTRY(\sym)
  587. ASM_CLAC
  588. pushq $~(\num)
  589. .Lcommon_\sym:
  590. interrupt \do_sym
  591. jmp ret_from_intr
  592. END(\sym)
  593. .endm
  594. #ifdef CONFIG_TRACING
  595. #define trace(sym) trace_##sym
  596. #define smp_trace(sym) smp_trace_##sym
  597. .macro trace_apicinterrupt num sym
  598. apicinterrupt3 \num trace(\sym) smp_trace(\sym)
  599. .endm
  600. #else
  601. .macro trace_apicinterrupt num sym do_sym
  602. .endm
  603. #endif
  604. .macro apicinterrupt num sym do_sym
  605. apicinterrupt3 \num \sym \do_sym
  606. trace_apicinterrupt \num \sym
  607. .endm
  608. #ifdef CONFIG_SMP
  609. apicinterrupt3 IRQ_MOVE_CLEANUP_VECTOR irq_move_cleanup_interrupt smp_irq_move_cleanup_interrupt
  610. apicinterrupt3 REBOOT_VECTOR reboot_interrupt smp_reboot_interrupt
  611. #endif
  612. #ifdef CONFIG_X86_UV
  613. apicinterrupt3 UV_BAU_MESSAGE uv_bau_message_intr1 uv_bau_message_interrupt
  614. #endif
  615. apicinterrupt LOCAL_TIMER_VECTOR apic_timer_interrupt smp_apic_timer_interrupt
  616. apicinterrupt X86_PLATFORM_IPI_VECTOR x86_platform_ipi smp_x86_platform_ipi
  617. #ifdef CONFIG_HAVE_KVM
  618. apicinterrupt3 POSTED_INTR_VECTOR kvm_posted_intr_ipi smp_kvm_posted_intr_ipi
  619. apicinterrupt3 POSTED_INTR_WAKEUP_VECTOR kvm_posted_intr_wakeup_ipi smp_kvm_posted_intr_wakeup_ipi
  620. #endif
  621. #ifdef CONFIG_X86_MCE_THRESHOLD
  622. apicinterrupt THRESHOLD_APIC_VECTOR threshold_interrupt smp_threshold_interrupt
  623. #endif
  624. #ifdef CONFIG_X86_MCE_AMD
  625. apicinterrupt DEFERRED_ERROR_VECTOR deferred_error_interrupt smp_deferred_error_interrupt
  626. #endif
  627. #ifdef CONFIG_X86_THERMAL_VECTOR
  628. apicinterrupt THERMAL_APIC_VECTOR thermal_interrupt smp_thermal_interrupt
  629. #endif
  630. #ifdef CONFIG_SMP
  631. apicinterrupt CALL_FUNCTION_SINGLE_VECTOR call_function_single_interrupt smp_call_function_single_interrupt
  632. apicinterrupt CALL_FUNCTION_VECTOR call_function_interrupt smp_call_function_interrupt
  633. apicinterrupt RESCHEDULE_VECTOR reschedule_interrupt smp_reschedule_interrupt
  634. #endif
  635. apicinterrupt ERROR_APIC_VECTOR error_interrupt smp_error_interrupt
  636. apicinterrupt SPURIOUS_APIC_VECTOR spurious_interrupt smp_spurious_interrupt
  637. #ifdef CONFIG_IRQ_WORK
  638. apicinterrupt IRQ_WORK_VECTOR irq_work_interrupt smp_irq_work_interrupt
  639. #endif
  640. /*
  641. * Exception entry points.
  642. */
  643. #define CPU_TSS_IST(x) PER_CPU_VAR(cpu_tss) + (TSS_ist + ((x) - 1) * 8)
  644. .macro idtentry sym do_sym has_error_code:req paranoid=0 shift_ist=-1
  645. ENTRY(\sym)
  646. /* Sanity check */
  647. .if \shift_ist != -1 && \paranoid == 0
  648. .error "using shift_ist requires paranoid=1"
  649. .endif
  650. ASM_CLAC
  651. PARAVIRT_ADJUST_EXCEPTION_FRAME
  652. .ifeq \has_error_code
  653. pushq $-1 /* ORIG_RAX: no syscall to restart */
  654. .endif
  655. ALLOC_PT_GPREGS_ON_STACK
  656. .if \paranoid
  657. .if \paranoid == 1
  658. testb $3, CS(%rsp) /* If coming from userspace, switch stacks */
  659. jnz 1f
  660. .endif
  661. call paranoid_entry
  662. .else
  663. call error_entry
  664. .endif
  665. /* returned flag: ebx=0: need swapgs on exit, ebx=1: don't need it */
  666. .if \paranoid
  667. .if \shift_ist != -1
  668. TRACE_IRQS_OFF_DEBUG /* reload IDT in case of recursion */
  669. .else
  670. TRACE_IRQS_OFF
  671. .endif
  672. .endif
  673. movq %rsp, %rdi /* pt_regs pointer */
  674. .if \has_error_code
  675. movq ORIG_RAX(%rsp), %rsi /* get error code */
  676. movq $-1, ORIG_RAX(%rsp) /* no syscall to restart */
  677. .else
  678. xorl %esi, %esi /* no error code */
  679. .endif
  680. .if \shift_ist != -1
  681. subq $EXCEPTION_STKSZ, CPU_TSS_IST(\shift_ist)
  682. .endif
  683. call \do_sym
  684. .if \shift_ist != -1
  685. addq $EXCEPTION_STKSZ, CPU_TSS_IST(\shift_ist)
  686. .endif
  687. /* these procedures expect "no swapgs" flag in ebx */
  688. .if \paranoid
  689. jmp paranoid_exit
  690. .else
  691. jmp error_exit
  692. .endif
  693. .if \paranoid == 1
  694. /*
  695. * Paranoid entry from userspace. Switch stacks and treat it
  696. * as a normal entry. This means that paranoid handlers
  697. * run in real process context if user_mode(regs).
  698. */
  699. 1:
  700. call error_entry
  701. movq %rsp, %rdi /* pt_regs pointer */
  702. call sync_regs
  703. movq %rax, %rsp /* switch stack */
  704. movq %rsp, %rdi /* pt_regs pointer */
  705. .if \has_error_code
  706. movq ORIG_RAX(%rsp), %rsi /* get error code */
  707. movq $-1, ORIG_RAX(%rsp) /* no syscall to restart */
  708. .else
  709. xorl %esi, %esi /* no error code */
  710. .endif
  711. call \do_sym
  712. jmp error_exit /* %ebx: no swapgs flag */
  713. .endif
  714. END(\sym)
  715. .endm
  716. #ifdef CONFIG_TRACING
  717. .macro trace_idtentry sym do_sym has_error_code:req
  718. idtentry trace(\sym) trace(\do_sym) has_error_code=\has_error_code
  719. idtentry \sym \do_sym has_error_code=\has_error_code
  720. .endm
  721. #else
  722. .macro trace_idtentry sym do_sym has_error_code:req
  723. idtentry \sym \do_sym has_error_code=\has_error_code
  724. .endm
  725. #endif
  726. idtentry divide_error do_divide_error has_error_code=0
  727. idtentry overflow do_overflow has_error_code=0
  728. idtentry bounds do_bounds has_error_code=0
  729. idtentry invalid_op do_invalid_op has_error_code=0
  730. idtentry device_not_available do_device_not_available has_error_code=0
  731. idtentry double_fault do_double_fault has_error_code=1 paranoid=2
  732. idtentry coprocessor_segment_overrun do_coprocessor_segment_overrun has_error_code=0
  733. idtentry invalid_TSS do_invalid_TSS has_error_code=1
  734. idtentry segment_not_present do_segment_not_present has_error_code=1
  735. idtentry spurious_interrupt_bug do_spurious_interrupt_bug has_error_code=0
  736. idtentry coprocessor_error do_coprocessor_error has_error_code=0
  737. idtentry alignment_check do_alignment_check has_error_code=1
  738. idtentry simd_coprocessor_error do_simd_coprocessor_error has_error_code=0
  739. /*
  740. * Reload gs selector with exception handling
  741. * edi: new selector
  742. */
  743. ENTRY(native_load_gs_index)
  744. pushfq
  745. DISABLE_INTERRUPTS(CLBR_ANY & ~CLBR_RDI)
  746. SWAPGS
  747. gs_change:
  748. movl %edi, %gs
  749. 2: mfence /* workaround */
  750. SWAPGS
  751. popfq
  752. ret
  753. END(native_load_gs_index)
  754. _ASM_EXTABLE(gs_change, bad_gs)
  755. .section .fixup, "ax"
  756. /* running with kernelgs */
  757. bad_gs:
  758. SWAPGS /* switch back to user gs */
  759. xorl %eax, %eax
  760. movl %eax, %gs
  761. jmp 2b
  762. .previous
  763. /* Call softirq on interrupt stack. Interrupts are off. */
  764. ENTRY(do_softirq_own_stack)
  765. pushq %rbp
  766. mov %rsp, %rbp
  767. incl PER_CPU_VAR(irq_count)
  768. cmove PER_CPU_VAR(irq_stack_ptr), %rsp
  769. push %rbp /* frame pointer backlink */
  770. call __do_softirq
  771. leaveq
  772. decl PER_CPU_VAR(irq_count)
  773. ret
  774. END(do_softirq_own_stack)
  775. #ifdef CONFIG_XEN
  776. idtentry xen_hypervisor_callback xen_do_hypervisor_callback has_error_code=0
  777. /*
  778. * A note on the "critical region" in our callback handler.
  779. * We want to avoid stacking callback handlers due to events occurring
  780. * during handling of the last event. To do this, we keep events disabled
  781. * until we've done all processing. HOWEVER, we must enable events before
  782. * popping the stack frame (can't be done atomically) and so it would still
  783. * be possible to get enough handler activations to overflow the stack.
  784. * Although unlikely, bugs of that kind are hard to track down, so we'd
  785. * like to avoid the possibility.
  786. * So, on entry to the handler we detect whether we interrupted an
  787. * existing activation in its critical region -- if so, we pop the current
  788. * activation and restart the handler using the previous one.
  789. */
  790. ENTRY(xen_do_hypervisor_callback) /* do_hypervisor_callback(struct *pt_regs) */
  791. /*
  792. * Since we don't modify %rdi, evtchn_do_upall(struct *pt_regs) will
  793. * see the correct pointer to the pt_regs
  794. */
  795. movq %rdi, %rsp /* we don't return, adjust the stack frame */
  796. 11: incl PER_CPU_VAR(irq_count)
  797. movq %rsp, %rbp
  798. cmovzq PER_CPU_VAR(irq_stack_ptr), %rsp
  799. pushq %rbp /* frame pointer backlink */
  800. call xen_evtchn_do_upcall
  801. popq %rsp
  802. decl PER_CPU_VAR(irq_count)
  803. #ifndef CONFIG_PREEMPT
  804. call xen_maybe_preempt_hcall
  805. #endif
  806. jmp error_exit
  807. END(xen_do_hypervisor_callback)
  808. /*
  809. * Hypervisor uses this for application faults while it executes.
  810. * We get here for two reasons:
  811. * 1. Fault while reloading DS, ES, FS or GS
  812. * 2. Fault while executing IRET
  813. * Category 1 we do not need to fix up as Xen has already reloaded all segment
  814. * registers that could be reloaded and zeroed the others.
  815. * Category 2 we fix up by killing the current process. We cannot use the
  816. * normal Linux return path in this case because if we use the IRET hypercall
  817. * to pop the stack frame we end up in an infinite loop of failsafe callbacks.
  818. * We distinguish between categories by comparing each saved segment register
  819. * with its current contents: any discrepancy means we in category 1.
  820. */
  821. ENTRY(xen_failsafe_callback)
  822. movl %ds, %ecx
  823. cmpw %cx, 0x10(%rsp)
  824. jne 1f
  825. movl %es, %ecx
  826. cmpw %cx, 0x18(%rsp)
  827. jne 1f
  828. movl %fs, %ecx
  829. cmpw %cx, 0x20(%rsp)
  830. jne 1f
  831. movl %gs, %ecx
  832. cmpw %cx, 0x28(%rsp)
  833. jne 1f
  834. /* All segments match their saved values => Category 2 (Bad IRET). */
  835. movq (%rsp), %rcx
  836. movq 8(%rsp), %r11
  837. addq $0x30, %rsp
  838. pushq $0 /* RIP */
  839. pushq %r11
  840. pushq %rcx
  841. jmp general_protection
  842. 1: /* Segment mismatch => Category 1 (Bad segment). Retry the IRET. */
  843. movq (%rsp), %rcx
  844. movq 8(%rsp), %r11
  845. addq $0x30, %rsp
  846. pushq $-1 /* orig_ax = -1 => not a system call */
  847. ALLOC_PT_GPREGS_ON_STACK
  848. SAVE_C_REGS
  849. SAVE_EXTRA_REGS
  850. jmp error_exit
  851. END(xen_failsafe_callback)
  852. apicinterrupt3 HYPERVISOR_CALLBACK_VECTOR \
  853. xen_hvm_callback_vector xen_evtchn_do_upcall
  854. #endif /* CONFIG_XEN */
  855. #if IS_ENABLED(CONFIG_HYPERV)
  856. apicinterrupt3 HYPERVISOR_CALLBACK_VECTOR \
  857. hyperv_callback_vector hyperv_vector_handler
  858. #endif /* CONFIG_HYPERV */
  859. idtentry debug do_debug has_error_code=0 paranoid=1 shift_ist=DEBUG_STACK
  860. idtentry int3 do_int3 has_error_code=0 paranoid=1 shift_ist=DEBUG_STACK
  861. idtentry stack_segment do_stack_segment has_error_code=1
  862. #ifdef CONFIG_XEN
  863. idtentry xen_debug do_debug has_error_code=0
  864. idtentry xen_int3 do_int3 has_error_code=0
  865. idtentry xen_stack_segment do_stack_segment has_error_code=1
  866. #endif
  867. idtentry general_protection do_general_protection has_error_code=1
  868. trace_idtentry page_fault do_page_fault has_error_code=1
  869. #ifdef CONFIG_KVM_GUEST
  870. idtentry async_page_fault do_async_page_fault has_error_code=1
  871. #endif
  872. #ifdef CONFIG_X86_MCE
  873. idtentry machine_check has_error_code=0 paranoid=1 do_sym=*machine_check_vector(%rip)
  874. #endif
  875. /*
  876. * Save all registers in pt_regs, and switch gs if needed.
  877. * Use slow, but surefire "are we in kernel?" check.
  878. * Return: ebx=0: need swapgs on exit, ebx=1: otherwise
  879. */
  880. ENTRY(paranoid_entry)
  881. cld
  882. SAVE_C_REGS 8
  883. SAVE_EXTRA_REGS 8
  884. movl $1, %ebx
  885. movl $MSR_GS_BASE, %ecx
  886. rdmsr
  887. testl %edx, %edx
  888. js 1f /* negative -> in kernel */
  889. SWAPGS
  890. xorl %ebx, %ebx
  891. 1: ret
  892. END(paranoid_entry)
  893. /*
  894. * "Paranoid" exit path from exception stack. This is invoked
  895. * only on return from non-NMI IST interrupts that came
  896. * from kernel space.
  897. *
  898. * We may be returning to very strange contexts (e.g. very early
  899. * in syscall entry), so checking for preemption here would
  900. * be complicated. Fortunately, we there's no good reason
  901. * to try to handle preemption here.
  902. *
  903. * On entry, ebx is "no swapgs" flag (1: don't need swapgs, 0: need it)
  904. */
  905. ENTRY(paranoid_exit)
  906. DISABLE_INTERRUPTS(CLBR_NONE)
  907. TRACE_IRQS_OFF_DEBUG
  908. testl %ebx, %ebx /* swapgs needed? */
  909. jnz paranoid_exit_no_swapgs
  910. TRACE_IRQS_IRETQ
  911. SWAPGS_UNSAFE_STACK
  912. jmp paranoid_exit_restore
  913. paranoid_exit_no_swapgs:
  914. TRACE_IRQS_IRETQ_DEBUG
  915. paranoid_exit_restore:
  916. RESTORE_EXTRA_REGS
  917. RESTORE_C_REGS
  918. REMOVE_PT_GPREGS_FROM_STACK 8
  919. INTERRUPT_RETURN
  920. END(paranoid_exit)
  921. /*
  922. * Save all registers in pt_regs, and switch gs if needed.
  923. * Return: EBX=0: came from user mode; EBX=1: otherwise
  924. */
  925. ENTRY(error_entry)
  926. cld
  927. SAVE_C_REGS 8
  928. SAVE_EXTRA_REGS 8
  929. xorl %ebx, %ebx
  930. testb $3, CS+8(%rsp)
  931. jz .Lerror_kernelspace
  932. .Lerror_entry_from_usermode_swapgs:
  933. /*
  934. * We entered from user mode or we're pretending to have entered
  935. * from user mode due to an IRET fault.
  936. */
  937. SWAPGS
  938. .Lerror_entry_from_usermode_after_swapgs:
  939. /*
  940. * We need to tell lockdep that IRQs are off. We can't do this until
  941. * we fix gsbase, and we should do it before enter_from_user_mode
  942. * (which can take locks).
  943. */
  944. TRACE_IRQS_OFF
  945. CALL_enter_from_user_mode
  946. ret
  947. .Lerror_entry_done:
  948. TRACE_IRQS_OFF
  949. ret
  950. /*
  951. * There are two places in the kernel that can potentially fault with
  952. * usergs. Handle them here. B stepping K8s sometimes report a
  953. * truncated RIP for IRET exceptions returning to compat mode. Check
  954. * for these here too.
  955. */
  956. .Lerror_kernelspace:
  957. incl %ebx
  958. leaq native_irq_return_iret(%rip), %rcx
  959. cmpq %rcx, RIP+8(%rsp)
  960. je .Lerror_bad_iret
  961. movl %ecx, %eax /* zero extend */
  962. cmpq %rax, RIP+8(%rsp)
  963. je .Lbstep_iret
  964. cmpq $gs_change, RIP+8(%rsp)
  965. jne .Lerror_entry_done
  966. /*
  967. * hack: gs_change can fail with user gsbase. If this happens, fix up
  968. * gsbase and proceed. We'll fix up the exception and land in
  969. * gs_change's error handler with kernel gsbase.
  970. */
  971. jmp .Lerror_entry_from_usermode_swapgs
  972. .Lbstep_iret:
  973. /* Fix truncated RIP */
  974. movq %rcx, RIP+8(%rsp)
  975. /* fall through */
  976. .Lerror_bad_iret:
  977. /*
  978. * We came from an IRET to user mode, so we have user gsbase.
  979. * Switch to kernel gsbase:
  980. */
  981. SWAPGS
  982. /*
  983. * Pretend that the exception came from user mode: set up pt_regs
  984. * as if we faulted immediately after IRET and clear EBX so that
  985. * error_exit knows that we will be returning to user mode.
  986. */
  987. mov %rsp, %rdi
  988. call fixup_bad_iret
  989. mov %rax, %rsp
  990. decl %ebx
  991. jmp .Lerror_entry_from_usermode_after_swapgs
  992. END(error_entry)
  993. /*
  994. * On entry, EBS is a "return to kernel mode" flag:
  995. * 1: already in kernel mode, don't need SWAPGS
  996. * 0: user gsbase is loaded, we need SWAPGS and standard preparation for return to usermode
  997. */
  998. ENTRY(error_exit)
  999. movl %ebx, %eax
  1000. DISABLE_INTERRUPTS(CLBR_NONE)
  1001. TRACE_IRQS_OFF
  1002. testl %eax, %eax
  1003. jnz retint_kernel
  1004. jmp retint_user
  1005. END(error_exit)
  1006. /* Runs on exception stack */
  1007. ENTRY(nmi)
  1008. /*
  1009. * Fix up the exception frame if we're on Xen.
  1010. * PARAVIRT_ADJUST_EXCEPTION_FRAME is guaranteed to push at most
  1011. * one value to the stack on native, so it may clobber the rdx
  1012. * scratch slot, but it won't clobber any of the important
  1013. * slots past it.
  1014. *
  1015. * Xen is a different story, because the Xen frame itself overlaps
  1016. * the "NMI executing" variable.
  1017. */
  1018. PARAVIRT_ADJUST_EXCEPTION_FRAME
  1019. /*
  1020. * We allow breakpoints in NMIs. If a breakpoint occurs, then
  1021. * the iretq it performs will take us out of NMI context.
  1022. * This means that we can have nested NMIs where the next
  1023. * NMI is using the top of the stack of the previous NMI. We
  1024. * can't let it execute because the nested NMI will corrupt the
  1025. * stack of the previous NMI. NMI handlers are not re-entrant
  1026. * anyway.
  1027. *
  1028. * To handle this case we do the following:
  1029. * Check the a special location on the stack that contains
  1030. * a variable that is set when NMIs are executing.
  1031. * The interrupted task's stack is also checked to see if it
  1032. * is an NMI stack.
  1033. * If the variable is not set and the stack is not the NMI
  1034. * stack then:
  1035. * o Set the special variable on the stack
  1036. * o Copy the interrupt frame into an "outermost" location on the
  1037. * stack
  1038. * o Copy the interrupt frame into an "iret" location on the stack
  1039. * o Continue processing the NMI
  1040. * If the variable is set or the previous stack is the NMI stack:
  1041. * o Modify the "iret" location to jump to the repeat_nmi
  1042. * o return back to the first NMI
  1043. *
  1044. * Now on exit of the first NMI, we first clear the stack variable
  1045. * The NMI stack will tell any nested NMIs at that point that it is
  1046. * nested. Then we pop the stack normally with iret, and if there was
  1047. * a nested NMI that updated the copy interrupt stack frame, a
  1048. * jump will be made to the repeat_nmi code that will handle the second
  1049. * NMI.
  1050. *
  1051. * However, espfix prevents us from directly returning to userspace
  1052. * with a single IRET instruction. Similarly, IRET to user mode
  1053. * can fault. We therefore handle NMIs from user space like
  1054. * other IST entries.
  1055. */
  1056. /* Use %rdx as our temp variable throughout */
  1057. pushq %rdx
  1058. testb $3, CS-RIP+8(%rsp)
  1059. jz .Lnmi_from_kernel
  1060. /*
  1061. * NMI from user mode. We need to run on the thread stack, but we
  1062. * can't go through the normal entry paths: NMIs are masked, and
  1063. * we don't want to enable interrupts, because then we'll end
  1064. * up in an awkward situation in which IRQs are on but NMIs
  1065. * are off.
  1066. *
  1067. * We also must not push anything to the stack before switching
  1068. * stacks lest we corrupt the "NMI executing" variable.
  1069. */
  1070. SWAPGS_UNSAFE_STACK
  1071. cld
  1072. movq %rsp, %rdx
  1073. movq PER_CPU_VAR(cpu_current_top_of_stack), %rsp
  1074. pushq 5*8(%rdx) /* pt_regs->ss */
  1075. pushq 4*8(%rdx) /* pt_regs->rsp */
  1076. pushq 3*8(%rdx) /* pt_regs->flags */
  1077. pushq 2*8(%rdx) /* pt_regs->cs */
  1078. pushq 1*8(%rdx) /* pt_regs->rip */
  1079. pushq $-1 /* pt_regs->orig_ax */
  1080. pushq %rdi /* pt_regs->di */
  1081. pushq %rsi /* pt_regs->si */
  1082. pushq (%rdx) /* pt_regs->dx */
  1083. pushq %rcx /* pt_regs->cx */
  1084. pushq %rax /* pt_regs->ax */
  1085. pushq %r8 /* pt_regs->r8 */
  1086. pushq %r9 /* pt_regs->r9 */
  1087. pushq %r10 /* pt_regs->r10 */
  1088. pushq %r11 /* pt_regs->r11 */
  1089. pushq %rbx /* pt_regs->rbx */
  1090. pushq %rbp /* pt_regs->rbp */
  1091. pushq %r12 /* pt_regs->r12 */
  1092. pushq %r13 /* pt_regs->r13 */
  1093. pushq %r14 /* pt_regs->r14 */
  1094. pushq %r15 /* pt_regs->r15 */
  1095. /*
  1096. * At this point we no longer need to worry about stack damage
  1097. * due to nesting -- we're on the normal thread stack and we're
  1098. * done with the NMI stack.
  1099. */
  1100. movq %rsp, %rdi
  1101. movq $-1, %rsi
  1102. call do_nmi
  1103. /*
  1104. * Return back to user mode. We must *not* do the normal exit
  1105. * work, because we don't want to enable interrupts. Fortunately,
  1106. * do_nmi doesn't modify pt_regs.
  1107. */
  1108. SWAPGS
  1109. jmp restore_c_regs_and_iret
  1110. .Lnmi_from_kernel:
  1111. /*
  1112. * Here's what our stack frame will look like:
  1113. * +---------------------------------------------------------+
  1114. * | original SS |
  1115. * | original Return RSP |
  1116. * | original RFLAGS |
  1117. * | original CS |
  1118. * | original RIP |
  1119. * +---------------------------------------------------------+
  1120. * | temp storage for rdx |
  1121. * +---------------------------------------------------------+
  1122. * | "NMI executing" variable |
  1123. * +---------------------------------------------------------+
  1124. * | iret SS } Copied from "outermost" frame |
  1125. * | iret Return RSP } on each loop iteration; overwritten |
  1126. * | iret RFLAGS } by a nested NMI to force another |
  1127. * | iret CS } iteration if needed. |
  1128. * | iret RIP } |
  1129. * +---------------------------------------------------------+
  1130. * | outermost SS } initialized in first_nmi; |
  1131. * | outermost Return RSP } will not be changed before |
  1132. * | outermost RFLAGS } NMI processing is done. |
  1133. * | outermost CS } Copied to "iret" frame on each |
  1134. * | outermost RIP } iteration. |
  1135. * +---------------------------------------------------------+
  1136. * | pt_regs |
  1137. * +---------------------------------------------------------+
  1138. *
  1139. * The "original" frame is used by hardware. Before re-enabling
  1140. * NMIs, we need to be done with it, and we need to leave enough
  1141. * space for the asm code here.
  1142. *
  1143. * We return by executing IRET while RSP points to the "iret" frame.
  1144. * That will either return for real or it will loop back into NMI
  1145. * processing.
  1146. *
  1147. * The "outermost" frame is copied to the "iret" frame on each
  1148. * iteration of the loop, so each iteration starts with the "iret"
  1149. * frame pointing to the final return target.
  1150. */
  1151. /*
  1152. * Determine whether we're a nested NMI.
  1153. *
  1154. * If we interrupted kernel code between repeat_nmi and
  1155. * end_repeat_nmi, then we are a nested NMI. We must not
  1156. * modify the "iret" frame because it's being written by
  1157. * the outer NMI. That's okay; the outer NMI handler is
  1158. * about to about to call do_nmi anyway, so we can just
  1159. * resume the outer NMI.
  1160. */
  1161. movq $repeat_nmi, %rdx
  1162. cmpq 8(%rsp), %rdx
  1163. ja 1f
  1164. movq $end_repeat_nmi, %rdx
  1165. cmpq 8(%rsp), %rdx
  1166. ja nested_nmi_out
  1167. 1:
  1168. /*
  1169. * Now check "NMI executing". If it's set, then we're nested.
  1170. * This will not detect if we interrupted an outer NMI just
  1171. * before IRET.
  1172. */
  1173. cmpl $1, -8(%rsp)
  1174. je nested_nmi
  1175. /*
  1176. * Now test if the previous stack was an NMI stack. This covers
  1177. * the case where we interrupt an outer NMI after it clears
  1178. * "NMI executing" but before IRET. We need to be careful, though:
  1179. * there is one case in which RSP could point to the NMI stack
  1180. * despite there being no NMI active: naughty userspace controls
  1181. * RSP at the very beginning of the SYSCALL targets. We can
  1182. * pull a fast one on naughty userspace, though: we program
  1183. * SYSCALL to mask DF, so userspace cannot cause DF to be set
  1184. * if it controls the kernel's RSP. We set DF before we clear
  1185. * "NMI executing".
  1186. */
  1187. lea 6*8(%rsp), %rdx
  1188. /* Compare the NMI stack (rdx) with the stack we came from (4*8(%rsp)) */
  1189. cmpq %rdx, 4*8(%rsp)
  1190. /* If the stack pointer is above the NMI stack, this is a normal NMI */
  1191. ja first_nmi
  1192. subq $EXCEPTION_STKSZ, %rdx
  1193. cmpq %rdx, 4*8(%rsp)
  1194. /* If it is below the NMI stack, it is a normal NMI */
  1195. jb first_nmi
  1196. /* Ah, it is within the NMI stack. */
  1197. testb $(X86_EFLAGS_DF >> 8), (3*8 + 1)(%rsp)
  1198. jz first_nmi /* RSP was user controlled. */
  1199. /* This is a nested NMI. */
  1200. nested_nmi:
  1201. /*
  1202. * Modify the "iret" frame to point to repeat_nmi, forcing another
  1203. * iteration of NMI handling.
  1204. */
  1205. subq $8, %rsp
  1206. leaq -10*8(%rsp), %rdx
  1207. pushq $__KERNEL_DS
  1208. pushq %rdx
  1209. pushfq
  1210. pushq $__KERNEL_CS
  1211. pushq $repeat_nmi
  1212. /* Put stack back */
  1213. addq $(6*8), %rsp
  1214. nested_nmi_out:
  1215. popq %rdx
  1216. /* We are returning to kernel mode, so this cannot result in a fault. */
  1217. INTERRUPT_RETURN
  1218. first_nmi:
  1219. /* Restore rdx. */
  1220. movq (%rsp), %rdx
  1221. /* Make room for "NMI executing". */
  1222. pushq $0
  1223. /* Leave room for the "iret" frame */
  1224. subq $(5*8), %rsp
  1225. /* Copy the "original" frame to the "outermost" frame */
  1226. .rept 5
  1227. pushq 11*8(%rsp)
  1228. .endr
  1229. /* Everything up to here is safe from nested NMIs */
  1230. #ifdef CONFIG_DEBUG_ENTRY
  1231. /*
  1232. * For ease of testing, unmask NMIs right away. Disabled by
  1233. * default because IRET is very expensive.
  1234. */
  1235. pushq $0 /* SS */
  1236. pushq %rsp /* RSP (minus 8 because of the previous push) */
  1237. addq $8, (%rsp) /* Fix up RSP */
  1238. pushfq /* RFLAGS */
  1239. pushq $__KERNEL_CS /* CS */
  1240. pushq $1f /* RIP */
  1241. INTERRUPT_RETURN /* continues at repeat_nmi below */
  1242. 1:
  1243. #endif
  1244. repeat_nmi:
  1245. /*
  1246. * If there was a nested NMI, the first NMI's iret will return
  1247. * here. But NMIs are still enabled and we can take another
  1248. * nested NMI. The nested NMI checks the interrupted RIP to see
  1249. * if it is between repeat_nmi and end_repeat_nmi, and if so
  1250. * it will just return, as we are about to repeat an NMI anyway.
  1251. * This makes it safe to copy to the stack frame that a nested
  1252. * NMI will update.
  1253. *
  1254. * RSP is pointing to "outermost RIP". gsbase is unknown, but, if
  1255. * we're repeating an NMI, gsbase has the same value that it had on
  1256. * the first iteration. paranoid_entry will load the kernel
  1257. * gsbase if needed before we call do_nmi. "NMI executing"
  1258. * is zero.
  1259. */
  1260. movq $1, 10*8(%rsp) /* Set "NMI executing". */
  1261. /*
  1262. * Copy the "outermost" frame to the "iret" frame. NMIs that nest
  1263. * here must not modify the "iret" frame while we're writing to
  1264. * it or it will end up containing garbage.
  1265. */
  1266. addq $(10*8), %rsp
  1267. .rept 5
  1268. pushq -6*8(%rsp)
  1269. .endr
  1270. subq $(5*8), %rsp
  1271. end_repeat_nmi:
  1272. /*
  1273. * Everything below this point can be preempted by a nested NMI.
  1274. * If this happens, then the inner NMI will change the "iret"
  1275. * frame to point back to repeat_nmi.
  1276. */
  1277. pushq $-1 /* ORIG_RAX: no syscall to restart */
  1278. ALLOC_PT_GPREGS_ON_STACK
  1279. /*
  1280. * Use paranoid_entry to handle SWAPGS, but no need to use paranoid_exit
  1281. * as we should not be calling schedule in NMI context.
  1282. * Even with normal interrupts enabled. An NMI should not be
  1283. * setting NEED_RESCHED or anything that normal interrupts and
  1284. * exceptions might do.
  1285. */
  1286. call paranoid_entry
  1287. /* paranoidentry do_nmi, 0; without TRACE_IRQS_OFF */
  1288. movq %rsp, %rdi
  1289. movq $-1, %rsi
  1290. call do_nmi
  1291. testl %ebx, %ebx /* swapgs needed? */
  1292. jnz nmi_restore
  1293. nmi_swapgs:
  1294. SWAPGS_UNSAFE_STACK
  1295. nmi_restore:
  1296. RESTORE_EXTRA_REGS
  1297. RESTORE_C_REGS
  1298. /* Point RSP at the "iret" frame. */
  1299. REMOVE_PT_GPREGS_FROM_STACK 6*8
  1300. /*
  1301. * Clear "NMI executing". Set DF first so that we can easily
  1302. * distinguish the remaining code between here and IRET from
  1303. * the SYSCALL entry and exit paths. On a native kernel, we
  1304. * could just inspect RIP, but, on paravirt kernels,
  1305. * INTERRUPT_RETURN can translate into a jump into a
  1306. * hypercall page.
  1307. */
  1308. std
  1309. movq $0, 5*8(%rsp) /* clear "NMI executing" */
  1310. /*
  1311. * INTERRUPT_RETURN reads the "iret" frame and exits the NMI
  1312. * stack in a single instruction. We are returning to kernel
  1313. * mode, so this cannot result in a fault.
  1314. */
  1315. INTERRUPT_RETURN
  1316. END(nmi)
  1317. ENTRY(ignore_sysret)
  1318. mov $-ENOSYS, %eax
  1319. sysret
  1320. END(ignore_sysret)