entry_64.S 43 KB

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