entry_64.S 43 KB

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