entry_64.S 44 KB

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