entry_64.S 43 KB

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