entry_64.S 43 KB

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