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. /*CFI_REL_OFFSET orig_rax, ORIG_RAX-ORIG_RAX*/
  189. .endm
  190. /*
  191. * frame that enables calling into C.
  192. */
  193. .macro PARTIAL_FRAME start=1 offset=0
  194. XCPT_FRAME \start, ORIG_RAX+\offset-ARGOFFSET
  195. CFI_REL_OFFSET rdi, RDI+\offset-ARGOFFSET
  196. CFI_REL_OFFSET rsi, RSI+\offset-ARGOFFSET
  197. CFI_REL_OFFSET rdx, RDX+\offset-ARGOFFSET
  198. CFI_REL_OFFSET rcx, RCX+\offset-ARGOFFSET
  199. CFI_REL_OFFSET rax, RAX+\offset-ARGOFFSET
  200. CFI_REL_OFFSET r8, R8+\offset-ARGOFFSET
  201. CFI_REL_OFFSET r9, R9+\offset-ARGOFFSET
  202. CFI_REL_OFFSET r10, R10+\offset-ARGOFFSET
  203. CFI_REL_OFFSET r11, R11+\offset-ARGOFFSET
  204. .endm
  205. /*
  206. * frame that enables passing a complete pt_regs to a C function.
  207. */
  208. .macro DEFAULT_FRAME start=1 offset=0
  209. PARTIAL_FRAME \start, R11+\offset-R15
  210. CFI_REL_OFFSET rbx, RBX+\offset
  211. CFI_REL_OFFSET rbp, RBP+\offset
  212. CFI_REL_OFFSET r12, R12+\offset
  213. CFI_REL_OFFSET r13, R13+\offset
  214. CFI_REL_OFFSET r14, R14+\offset
  215. CFI_REL_OFFSET r15, R15+\offset
  216. .endm
  217. /* save partial stack frame */
  218. .macro SAVE_ARGS_IRQ
  219. cld
  220. /* start from rbp in pt_regs and jump over */
  221. movq_cfi rdi, (RDI-RBP)
  222. movq_cfi rsi, (RSI-RBP)
  223. movq_cfi rdx, (RDX-RBP)
  224. movq_cfi rcx, (RCX-RBP)
  225. movq_cfi rax, (RAX-RBP)
  226. movq_cfi r8, (R8-RBP)
  227. movq_cfi r9, (R9-RBP)
  228. movq_cfi r10, (R10-RBP)
  229. movq_cfi r11, (R11-RBP)
  230. /* Save rbp so that we can unwind from get_irq_regs() */
  231. movq_cfi rbp, 0
  232. /* Save previous stack value */
  233. movq %rsp, %rsi
  234. leaq -RBP(%rsp),%rdi /* arg1 for handler */
  235. testl $3, CS-RBP(%rsi)
  236. je 1f
  237. SWAPGS
  238. /*
  239. * irq_count is used to check if a CPU is already on an interrupt stack
  240. * or not. While this is essentially redundant with preempt_count it is
  241. * a little cheaper to use a separate counter in the PDA (short of
  242. * moving irq_enter into assembly, which would be too much work)
  243. */
  244. 1: incl PER_CPU_VAR(irq_count)
  245. cmovzq PER_CPU_VAR(irq_stack_ptr),%rsp
  246. CFI_DEF_CFA_REGISTER rsi
  247. /* Store previous stack value */
  248. pushq %rsi
  249. CFI_ESCAPE 0x0f /* DW_CFA_def_cfa_expression */, 6, \
  250. 0x77 /* DW_OP_breg7 */, 0, \
  251. 0x06 /* DW_OP_deref */, \
  252. 0x08 /* DW_OP_const1u */, SS+8-RBP, \
  253. 0x22 /* DW_OP_plus */
  254. /* We entered an interrupt context - irqs are off: */
  255. TRACE_IRQS_OFF
  256. .endm
  257. ENTRY(save_paranoid)
  258. XCPT_FRAME 1 RDI+8
  259. cld
  260. movq_cfi rdi, RDI+8
  261. movq_cfi rsi, RSI+8
  262. movq_cfi rdx, RDX+8
  263. movq_cfi rcx, RCX+8
  264. movq_cfi rax, RAX+8
  265. movq_cfi r8, R8+8
  266. movq_cfi r9, R9+8
  267. movq_cfi r10, R10+8
  268. movq_cfi r11, R11+8
  269. movq_cfi rbx, RBX+8
  270. movq_cfi rbp, RBP+8
  271. movq_cfi r12, R12+8
  272. movq_cfi r13, R13+8
  273. movq_cfi r14, R14+8
  274. movq_cfi r15, R15+8
  275. movl $1,%ebx
  276. movl $MSR_GS_BASE,%ecx
  277. rdmsr
  278. testl %edx,%edx
  279. js 1f /* negative -> in kernel */
  280. SWAPGS
  281. xorl %ebx,%ebx
  282. 1: ret
  283. CFI_ENDPROC
  284. END(save_paranoid)
  285. /*
  286. * A newly forked process directly context switches into this address.
  287. *
  288. * rdi: prev task we switched from
  289. */
  290. ENTRY(ret_from_fork)
  291. DEFAULT_FRAME
  292. LOCK ; btr $TIF_FORK,TI_flags(%r8)
  293. pushq_cfi $0x0002
  294. popfq_cfi # reset kernel eflags
  295. call schedule_tail # rdi: 'prev' task parameter
  296. GET_THREAD_INFO(%rcx)
  297. RESTORE_REST
  298. testl $3, CS-ARGOFFSET(%rsp) # from kernel_thread?
  299. jz 1f
  300. testl $_TIF_IA32, TI_flags(%rcx) # 32-bit compat task needs IRET
  301. jnz int_ret_from_sys_call
  302. RESTORE_TOP_OF_STACK %rdi, -ARGOFFSET
  303. jmp ret_from_sys_call # go to the SYSRET fastpath
  304. 1:
  305. subq $REST_SKIP, %rsp # leave space for volatiles
  306. CFI_ADJUST_CFA_OFFSET REST_SKIP
  307. movq %rbp, %rdi
  308. call *%rbx
  309. movl $0, RAX(%rsp)
  310. RESTORE_REST
  311. jmp int_ret_from_sys_call
  312. CFI_ENDPROC
  313. END(ret_from_fork)
  314. /*
  315. * System call entry. Up to 6 arguments in registers are supported.
  316. *
  317. * SYSCALL does not save anything on the stack and does not change the
  318. * stack pointer. However, it does mask the flags register for us, so
  319. * CLD and CLAC are not needed.
  320. */
  321. /*
  322. * Register setup:
  323. * rax system call number
  324. * rdi arg0
  325. * rcx return address for syscall/sysret, C arg3
  326. * rsi arg1
  327. * rdx arg2
  328. * r10 arg3 (--> moved to rcx for C)
  329. * r8 arg4
  330. * r9 arg5
  331. * r11 eflags for syscall/sysret, temporary for C
  332. * r12-r15,rbp,rbx saved by C code, not touched.
  333. *
  334. * Interrupts are off on entry.
  335. * Only called from user space.
  336. *
  337. * XXX if we had a free scratch register we could save the RSP into the stack frame
  338. * and report it properly in ps. Unfortunately we haven't.
  339. *
  340. * When user can change the frames always force IRET. That is because
  341. * it deals with uncanonical addresses better. SYSRET has trouble
  342. * with them due to bugs in both AMD and Intel CPUs.
  343. */
  344. ENTRY(system_call)
  345. CFI_STARTPROC simple
  346. CFI_SIGNAL_FRAME
  347. CFI_DEF_CFA rsp,KERNEL_STACK_OFFSET
  348. CFI_REGISTER rip,rcx
  349. /*CFI_REGISTER rflags,r11*/
  350. SWAPGS_UNSAFE_STACK
  351. /*
  352. * A hypervisor implementation might want to use a label
  353. * after the swapgs, so that it can do the swapgs
  354. * for the guest and jump here on syscall.
  355. */
  356. GLOBAL(system_call_after_swapgs)
  357. movq %rsp,PER_CPU_VAR(old_rsp)
  358. movq PER_CPU_VAR(kernel_stack),%rsp
  359. /*
  360. * No need to follow this irqs off/on section - it's straight
  361. * and short:
  362. */
  363. ENABLE_INTERRUPTS(CLBR_NONE)
  364. SAVE_ARGS 8,0
  365. movq %rax,ORIG_RAX-ARGOFFSET(%rsp)
  366. movq %rcx,RIP-ARGOFFSET(%rsp)
  367. CFI_REL_OFFSET rip,RIP-ARGOFFSET
  368. testl $_TIF_WORK_SYSCALL_ENTRY,TI_flags+THREAD_INFO(%rsp,RIP-ARGOFFSET)
  369. jnz tracesys
  370. system_call_fastpath:
  371. #if __SYSCALL_MASK == ~0
  372. cmpq $__NR_syscall_max,%rax
  373. #else
  374. andl $__SYSCALL_MASK,%eax
  375. cmpl $__NR_syscall_max,%eax
  376. #endif
  377. ja badsys
  378. movq %r10,%rcx
  379. call *sys_call_table(,%rax,8) # XXX: rip relative
  380. movq %rax,RAX-ARGOFFSET(%rsp)
  381. /*
  382. * Syscall return path ending with SYSRET (fast path)
  383. * Has incomplete stack frame and undefined top of stack.
  384. */
  385. ret_from_sys_call:
  386. movl $_TIF_ALLWORK_MASK,%edi
  387. /* edi: flagmask */
  388. sysret_check:
  389. LOCKDEP_SYS_EXIT
  390. DISABLE_INTERRUPTS(CLBR_NONE)
  391. TRACE_IRQS_OFF
  392. movl TI_flags+THREAD_INFO(%rsp,RIP-ARGOFFSET),%edx
  393. andl %edi,%edx
  394. jnz sysret_careful
  395. CFI_REMEMBER_STATE
  396. /*
  397. * sysretq will re-enable interrupts:
  398. */
  399. TRACE_IRQS_ON
  400. movq RIP-ARGOFFSET(%rsp),%rcx
  401. CFI_REGISTER rip,rcx
  402. RESTORE_ARGS 1,-ARG_SKIP,0
  403. /*CFI_REGISTER rflags,r11*/
  404. movq PER_CPU_VAR(old_rsp), %rsp
  405. USERGS_SYSRET64
  406. CFI_RESTORE_STATE
  407. /* Handle reschedules */
  408. /* edx: work, edi: workmask */
  409. sysret_careful:
  410. bt $TIF_NEED_RESCHED,%edx
  411. jnc sysret_signal
  412. TRACE_IRQS_ON
  413. ENABLE_INTERRUPTS(CLBR_NONE)
  414. pushq_cfi %rdi
  415. SCHEDULE_USER
  416. popq_cfi %rdi
  417. jmp sysret_check
  418. /* Handle a signal */
  419. sysret_signal:
  420. TRACE_IRQS_ON
  421. ENABLE_INTERRUPTS(CLBR_NONE)
  422. #ifdef CONFIG_AUDITSYSCALL
  423. bt $TIF_SYSCALL_AUDIT,%edx
  424. jc sysret_audit
  425. #endif
  426. /*
  427. * We have a signal, or exit tracing or single-step.
  428. * These all wind up with the iret return path anyway,
  429. * so just join that path right now.
  430. */
  431. FIXUP_TOP_OF_STACK %r11, -ARGOFFSET
  432. jmp int_check_syscall_exit_work
  433. badsys:
  434. movq $-ENOSYS,RAX-ARGOFFSET(%rsp)
  435. jmp ret_from_sys_call
  436. #ifdef CONFIG_AUDITSYSCALL
  437. /*
  438. * Fast path for syscall audit without full syscall trace.
  439. * We just call __audit_syscall_entry() directly, and then
  440. * jump back to the normal fast path.
  441. */
  442. auditsys:
  443. movq %r10,%r9 /* 6th arg: 4th syscall arg */
  444. movq %rdx,%r8 /* 5th arg: 3rd syscall arg */
  445. movq %rsi,%rcx /* 4th arg: 2nd syscall arg */
  446. movq %rdi,%rdx /* 3rd arg: 1st syscall arg */
  447. movq %rax,%rsi /* 2nd arg: syscall number */
  448. movl $AUDIT_ARCH_X86_64,%edi /* 1st arg: audit arch */
  449. call __audit_syscall_entry
  450. LOAD_ARGS 0 /* reload call-clobbered registers */
  451. jmp system_call_fastpath
  452. /*
  453. * Return fast path for syscall audit. Call __audit_syscall_exit()
  454. * directly and then jump back to the fast path with TIF_SYSCALL_AUDIT
  455. * masked off.
  456. */
  457. sysret_audit:
  458. movq RAX-ARGOFFSET(%rsp),%rsi /* second arg, syscall return value */
  459. cmpq $-MAX_ERRNO,%rsi /* is it < -MAX_ERRNO? */
  460. setbe %al /* 1 if so, 0 if not */
  461. movzbl %al,%edi /* zero-extend that into %edi */
  462. call __audit_syscall_exit
  463. movl $(_TIF_ALLWORK_MASK & ~_TIF_SYSCALL_AUDIT),%edi
  464. jmp sysret_check
  465. #endif /* CONFIG_AUDITSYSCALL */
  466. /* Do syscall tracing */
  467. tracesys:
  468. #ifdef CONFIG_AUDITSYSCALL
  469. testl $(_TIF_WORK_SYSCALL_ENTRY & ~_TIF_SYSCALL_AUDIT),TI_flags+THREAD_INFO(%rsp,RIP-ARGOFFSET)
  470. jz auditsys
  471. #endif
  472. SAVE_REST
  473. movq $-ENOSYS,RAX(%rsp) /* ptrace can change this for a bad syscall */
  474. FIXUP_TOP_OF_STACK %rdi
  475. movq %rsp,%rdi
  476. call syscall_trace_enter
  477. /*
  478. * Reload arg registers from stack in case ptrace changed them.
  479. * We don't reload %rax because syscall_trace_enter() returned
  480. * the value it wants us to use in the table lookup.
  481. */
  482. LOAD_ARGS ARGOFFSET, 1
  483. RESTORE_REST
  484. #if __SYSCALL_MASK == ~0
  485. cmpq $__NR_syscall_max,%rax
  486. #else
  487. andl $__SYSCALL_MASK,%eax
  488. cmpl $__NR_syscall_max,%eax
  489. #endif
  490. ja int_ret_from_sys_call /* RAX(%rsp) set to -ENOSYS above */
  491. movq %r10,%rcx /* fixup for C */
  492. call *sys_call_table(,%rax,8)
  493. movq %rax,RAX-ARGOFFSET(%rsp)
  494. /* Use IRET because user could have changed frame */
  495. /*
  496. * Syscall return path ending with IRET.
  497. * Has correct top of stack, but partial stack frame.
  498. */
  499. GLOBAL(int_ret_from_sys_call)
  500. DISABLE_INTERRUPTS(CLBR_NONE)
  501. TRACE_IRQS_OFF
  502. movl $_TIF_ALLWORK_MASK,%edi
  503. /* edi: mask to check */
  504. GLOBAL(int_with_check)
  505. LOCKDEP_SYS_EXIT_IRQ
  506. GET_THREAD_INFO(%rcx)
  507. movl TI_flags(%rcx),%edx
  508. andl %edi,%edx
  509. jnz int_careful
  510. andl $~TS_COMPAT,TI_status(%rcx)
  511. jmp retint_swapgs
  512. /* Either reschedule or signal or syscall exit tracking needed. */
  513. /* First do a reschedule test. */
  514. /* edx: work, edi: workmask */
  515. int_careful:
  516. bt $TIF_NEED_RESCHED,%edx
  517. jnc int_very_careful
  518. TRACE_IRQS_ON
  519. ENABLE_INTERRUPTS(CLBR_NONE)
  520. pushq_cfi %rdi
  521. SCHEDULE_USER
  522. popq_cfi %rdi
  523. DISABLE_INTERRUPTS(CLBR_NONE)
  524. TRACE_IRQS_OFF
  525. jmp int_with_check
  526. /* handle signals and tracing -- both require a full stack frame */
  527. int_very_careful:
  528. TRACE_IRQS_ON
  529. ENABLE_INTERRUPTS(CLBR_NONE)
  530. int_check_syscall_exit_work:
  531. SAVE_REST
  532. /* Check for syscall exit trace */
  533. testl $_TIF_WORK_SYSCALL_EXIT,%edx
  534. jz int_signal
  535. pushq_cfi %rdi
  536. leaq 8(%rsp),%rdi # &ptregs -> arg1
  537. call syscall_trace_leave
  538. popq_cfi %rdi
  539. andl $~(_TIF_WORK_SYSCALL_EXIT|_TIF_SYSCALL_EMU),%edi
  540. jmp int_restore_rest
  541. int_signal:
  542. testl $_TIF_DO_NOTIFY_MASK,%edx
  543. jz 1f
  544. movq %rsp,%rdi # &ptregs -> arg1
  545. xorl %esi,%esi # oldset -> arg2
  546. call do_notify_resume
  547. 1: movl $_TIF_WORK_MASK,%edi
  548. int_restore_rest:
  549. RESTORE_REST
  550. DISABLE_INTERRUPTS(CLBR_NONE)
  551. TRACE_IRQS_OFF
  552. jmp int_with_check
  553. CFI_ENDPROC
  554. END(system_call)
  555. .macro FORK_LIKE func
  556. ENTRY(stub_\func)
  557. CFI_STARTPROC
  558. popq %r11 /* save return address */
  559. PARTIAL_FRAME 0
  560. SAVE_REST
  561. pushq %r11 /* put it back on stack */
  562. FIXUP_TOP_OF_STACK %r11, 8
  563. DEFAULT_FRAME 0 8 /* offset 8: return address */
  564. call sys_\func
  565. RESTORE_TOP_OF_STACK %r11, 8
  566. ret $REST_SKIP /* pop extended registers */
  567. CFI_ENDPROC
  568. END(stub_\func)
  569. .endm
  570. .macro FIXED_FRAME label,func
  571. ENTRY(\label)
  572. CFI_STARTPROC
  573. PARTIAL_FRAME 0 8 /* offset 8: return address */
  574. FIXUP_TOP_OF_STACK %r11, 8-ARGOFFSET
  575. call \func
  576. RESTORE_TOP_OF_STACK %r11, 8-ARGOFFSET
  577. ret
  578. CFI_ENDPROC
  579. END(\label)
  580. .endm
  581. FORK_LIKE clone
  582. FORK_LIKE fork
  583. FORK_LIKE vfork
  584. FIXED_FRAME stub_iopl, sys_iopl
  585. ENTRY(ptregscall_common)
  586. DEFAULT_FRAME 1 8 /* offset 8: return address */
  587. RESTORE_TOP_OF_STACK %r11, 8
  588. movq_cfi_restore R15+8, r15
  589. movq_cfi_restore R14+8, r14
  590. movq_cfi_restore R13+8, r13
  591. movq_cfi_restore R12+8, r12
  592. movq_cfi_restore RBP+8, rbp
  593. movq_cfi_restore RBX+8, rbx
  594. ret $REST_SKIP /* pop extended registers */
  595. CFI_ENDPROC
  596. END(ptregscall_common)
  597. ENTRY(stub_execve)
  598. CFI_STARTPROC
  599. addq $8, %rsp
  600. PARTIAL_FRAME 0
  601. SAVE_REST
  602. FIXUP_TOP_OF_STACK %r11
  603. call sys_execve
  604. movq %rax,RAX(%rsp)
  605. RESTORE_REST
  606. jmp int_ret_from_sys_call
  607. CFI_ENDPROC
  608. END(stub_execve)
  609. /*
  610. * sigreturn is special because it needs to restore all registers on return.
  611. * This cannot be done with SYSRET, so use the IRET return path instead.
  612. */
  613. ENTRY(stub_rt_sigreturn)
  614. CFI_STARTPROC
  615. addq $8, %rsp
  616. PARTIAL_FRAME 0
  617. SAVE_REST
  618. FIXUP_TOP_OF_STACK %r11
  619. call sys_rt_sigreturn
  620. movq %rax,RAX(%rsp) # fixme, this could be done at the higher layer
  621. RESTORE_REST
  622. jmp int_ret_from_sys_call
  623. CFI_ENDPROC
  624. END(stub_rt_sigreturn)
  625. #ifdef CONFIG_X86_X32_ABI
  626. ENTRY(stub_x32_rt_sigreturn)
  627. CFI_STARTPROC
  628. addq $8, %rsp
  629. PARTIAL_FRAME 0
  630. SAVE_REST
  631. FIXUP_TOP_OF_STACK %r11
  632. call sys32_x32_rt_sigreturn
  633. movq %rax,RAX(%rsp) # fixme, this could be done at the higher layer
  634. RESTORE_REST
  635. jmp int_ret_from_sys_call
  636. CFI_ENDPROC
  637. END(stub_x32_rt_sigreturn)
  638. ENTRY(stub_x32_execve)
  639. CFI_STARTPROC
  640. addq $8, %rsp
  641. PARTIAL_FRAME 0
  642. SAVE_REST
  643. FIXUP_TOP_OF_STACK %r11
  644. call compat_sys_execve
  645. RESTORE_TOP_OF_STACK %r11
  646. movq %rax,RAX(%rsp)
  647. RESTORE_REST
  648. jmp int_ret_from_sys_call
  649. CFI_ENDPROC
  650. END(stub_x32_execve)
  651. #endif
  652. /*
  653. * Build the entry stubs and pointer table with some assembler magic.
  654. * We pack 7 stubs into a single 32-byte chunk, which will fit in a
  655. * single cache line on all modern x86 implementations.
  656. */
  657. .section .init.rodata,"a"
  658. ENTRY(interrupt)
  659. .section .entry.text
  660. .p2align 5
  661. .p2align CONFIG_X86_L1_CACHE_SHIFT
  662. ENTRY(irq_entries_start)
  663. INTR_FRAME
  664. vector=FIRST_EXTERNAL_VECTOR
  665. .rept (NR_VECTORS-FIRST_EXTERNAL_VECTOR+6)/7
  666. .balign 32
  667. .rept 7
  668. .if vector < NR_VECTORS
  669. .if vector <> FIRST_EXTERNAL_VECTOR
  670. CFI_ADJUST_CFA_OFFSET -8
  671. .endif
  672. 1: pushq_cfi $(~vector+0x80) /* Note: always in signed byte range */
  673. .if ((vector-FIRST_EXTERNAL_VECTOR)%7) <> 6
  674. jmp 2f
  675. .endif
  676. .previous
  677. .quad 1b
  678. .section .entry.text
  679. vector=vector+1
  680. .endif
  681. .endr
  682. 2: jmp common_interrupt
  683. .endr
  684. CFI_ENDPROC
  685. END(irq_entries_start)
  686. .previous
  687. END(interrupt)
  688. .previous
  689. /*
  690. * Interrupt entry/exit.
  691. *
  692. * Interrupt entry points save only callee clobbered registers in fast path.
  693. *
  694. * Entry runs with interrupts off.
  695. */
  696. /* 0(%rsp): ~(interrupt number) */
  697. .macro interrupt func
  698. /* reserve pt_regs for scratch regs and rbp */
  699. subq $ORIG_RAX-RBP, %rsp
  700. CFI_ADJUST_CFA_OFFSET ORIG_RAX-RBP
  701. SAVE_ARGS_IRQ
  702. call \func
  703. .endm
  704. /*
  705. * The interrupt stubs push (~vector+0x80) onto the stack and
  706. * then jump to common_interrupt.
  707. */
  708. .p2align CONFIG_X86_L1_CACHE_SHIFT
  709. common_interrupt:
  710. XCPT_FRAME
  711. ASM_CLAC
  712. addq $-0x80,(%rsp) /* Adjust vector to [-256,-1] range */
  713. interrupt do_IRQ
  714. /* 0(%rsp): old_rsp-ARGOFFSET */
  715. ret_from_intr:
  716. DISABLE_INTERRUPTS(CLBR_NONE)
  717. TRACE_IRQS_OFF
  718. decl PER_CPU_VAR(irq_count)
  719. /* Restore saved previous stack */
  720. popq %rsi
  721. CFI_DEF_CFA rsi,SS+8-RBP /* reg/off reset after def_cfa_expr */
  722. leaq ARGOFFSET-RBP(%rsi), %rsp
  723. CFI_DEF_CFA_REGISTER rsp
  724. CFI_ADJUST_CFA_OFFSET RBP-ARGOFFSET
  725. exit_intr:
  726. GET_THREAD_INFO(%rcx)
  727. testl $3,CS-ARGOFFSET(%rsp)
  728. je retint_kernel
  729. /* Interrupt came from user space */
  730. /*
  731. * Has a correct top of stack, but a partial stack frame
  732. * %rcx: thread info. Interrupts off.
  733. */
  734. retint_with_reschedule:
  735. movl $_TIF_WORK_MASK,%edi
  736. retint_check:
  737. LOCKDEP_SYS_EXIT_IRQ
  738. movl TI_flags(%rcx),%edx
  739. andl %edi,%edx
  740. CFI_REMEMBER_STATE
  741. jnz retint_careful
  742. retint_swapgs: /* return to user-space */
  743. /*
  744. * The iretq could re-enable interrupts:
  745. */
  746. DISABLE_INTERRUPTS(CLBR_ANY)
  747. TRACE_IRQS_IRETQ
  748. SWAPGS
  749. jmp restore_args
  750. retint_restore_args: /* return to kernel space */
  751. DISABLE_INTERRUPTS(CLBR_ANY)
  752. /*
  753. * The iretq could re-enable interrupts:
  754. */
  755. TRACE_IRQS_IRETQ
  756. restore_args:
  757. RESTORE_ARGS 1,8,1
  758. irq_return:
  759. INTERRUPT_RETURN
  760. ENTRY(native_iret)
  761. /*
  762. * Are we returning to a stack segment from the LDT? Note: in
  763. * 64-bit mode SS:RSP on the exception stack is always valid.
  764. */
  765. #ifdef CONFIG_X86_ESPFIX64
  766. testb $4,(SS-RIP)(%rsp)
  767. jnz native_irq_return_ldt
  768. #endif
  769. native_irq_return_iret:
  770. iretq
  771. _ASM_EXTABLE(native_irq_return_iret, bad_iret)
  772. #ifdef CONFIG_X86_ESPFIX64
  773. native_irq_return_ldt:
  774. pushq_cfi %rax
  775. pushq_cfi %rdi
  776. SWAPGS
  777. movq PER_CPU_VAR(espfix_waddr),%rdi
  778. movq %rax,(0*8)(%rdi) /* RAX */
  779. movq (2*8)(%rsp),%rax /* RIP */
  780. movq %rax,(1*8)(%rdi)
  781. movq (3*8)(%rsp),%rax /* CS */
  782. movq %rax,(2*8)(%rdi)
  783. movq (4*8)(%rsp),%rax /* RFLAGS */
  784. movq %rax,(3*8)(%rdi)
  785. movq (6*8)(%rsp),%rax /* SS */
  786. movq %rax,(5*8)(%rdi)
  787. movq (5*8)(%rsp),%rax /* RSP */
  788. movq %rax,(4*8)(%rdi)
  789. andl $0xffff0000,%eax
  790. popq_cfi %rdi
  791. orq PER_CPU_VAR(espfix_stack),%rax
  792. SWAPGS
  793. movq %rax,%rsp
  794. popq_cfi %rax
  795. jmp native_irq_return_iret
  796. #endif
  797. .section .fixup,"ax"
  798. bad_iret:
  799. /*
  800. * The iret traps when the %cs or %ss being restored is bogus.
  801. * We've lost the original trap vector and error code.
  802. * #GPF is the most likely one to get for an invalid selector.
  803. * So pretend we completed the iret and took the #GPF in user mode.
  804. *
  805. * We are now running with the kernel GS after exception recovery.
  806. * But error_entry expects us to have user GS to match the user %cs,
  807. * so swap back.
  808. */
  809. pushq $0
  810. SWAPGS
  811. jmp general_protection
  812. .previous
  813. /* edi: workmask, edx: work */
  814. retint_careful:
  815. CFI_RESTORE_STATE
  816. bt $TIF_NEED_RESCHED,%edx
  817. jnc retint_signal
  818. TRACE_IRQS_ON
  819. ENABLE_INTERRUPTS(CLBR_NONE)
  820. pushq_cfi %rdi
  821. SCHEDULE_USER
  822. popq_cfi %rdi
  823. GET_THREAD_INFO(%rcx)
  824. DISABLE_INTERRUPTS(CLBR_NONE)
  825. TRACE_IRQS_OFF
  826. jmp retint_check
  827. retint_signal:
  828. testl $_TIF_DO_NOTIFY_MASK,%edx
  829. jz retint_swapgs
  830. TRACE_IRQS_ON
  831. ENABLE_INTERRUPTS(CLBR_NONE)
  832. SAVE_REST
  833. movq $-1,ORIG_RAX(%rsp)
  834. xorl %esi,%esi # oldset
  835. movq %rsp,%rdi # &pt_regs
  836. call do_notify_resume
  837. RESTORE_REST
  838. DISABLE_INTERRUPTS(CLBR_NONE)
  839. TRACE_IRQS_OFF
  840. GET_THREAD_INFO(%rcx)
  841. jmp retint_with_reschedule
  842. #ifdef CONFIG_PREEMPT
  843. /* Returning to kernel space. Check if we need preemption */
  844. /* rcx: threadinfo. interrupts off. */
  845. ENTRY(retint_kernel)
  846. cmpl $0,PER_CPU_VAR(__preempt_count)
  847. jnz retint_restore_args
  848. bt $9,EFLAGS-ARGOFFSET(%rsp) /* interrupts off? */
  849. jnc retint_restore_args
  850. call preempt_schedule_irq
  851. jmp exit_intr
  852. #endif
  853. CFI_ENDPROC
  854. END(common_interrupt)
  855. /*
  856. * If IRET takes a fault on the espfix stack, then we
  857. * end up promoting it to a doublefault. In that case,
  858. * modify the stack to make it look like we just entered
  859. * the #GP handler from user space, similar to bad_iret.
  860. */
  861. #ifdef CONFIG_X86_ESPFIX64
  862. ALIGN
  863. __do_double_fault:
  864. XCPT_FRAME 1 RDI+8
  865. movq RSP(%rdi),%rax /* Trap on the espfix stack? */
  866. sarq $PGDIR_SHIFT,%rax
  867. cmpl $ESPFIX_PGD_ENTRY,%eax
  868. jne do_double_fault /* No, just deliver the fault */
  869. cmpl $__KERNEL_CS,CS(%rdi)
  870. jne do_double_fault
  871. movq RIP(%rdi),%rax
  872. cmpq $native_irq_return_iret,%rax
  873. jne do_double_fault /* This shouldn't happen... */
  874. movq PER_CPU_VAR(kernel_stack),%rax
  875. subq $(6*8-KERNEL_STACK_OFFSET),%rax /* Reset to original stack */
  876. movq %rax,RSP(%rdi)
  877. movq $0,(%rax) /* Missing (lost) #GP error code */
  878. movq $general_protection,RIP(%rdi)
  879. retq
  880. CFI_ENDPROC
  881. END(__do_double_fault)
  882. #else
  883. # define __do_double_fault do_double_fault
  884. #endif
  885. /*
  886. * APIC interrupts.
  887. */
  888. .macro apicinterrupt3 num sym do_sym
  889. ENTRY(\sym)
  890. INTR_FRAME
  891. ASM_CLAC
  892. pushq_cfi $~(\num)
  893. .Lcommon_\sym:
  894. interrupt \do_sym
  895. jmp ret_from_intr
  896. CFI_ENDPROC
  897. END(\sym)
  898. .endm
  899. #ifdef CONFIG_TRACING
  900. #define trace(sym) trace_##sym
  901. #define smp_trace(sym) smp_trace_##sym
  902. .macro trace_apicinterrupt num sym
  903. apicinterrupt3 \num trace(\sym) smp_trace(\sym)
  904. .endm
  905. #else
  906. .macro trace_apicinterrupt num sym do_sym
  907. .endm
  908. #endif
  909. .macro apicinterrupt num sym do_sym
  910. apicinterrupt3 \num \sym \do_sym
  911. trace_apicinterrupt \num \sym
  912. .endm
  913. #ifdef CONFIG_SMP
  914. apicinterrupt3 IRQ_MOVE_CLEANUP_VECTOR \
  915. irq_move_cleanup_interrupt smp_irq_move_cleanup_interrupt
  916. apicinterrupt3 REBOOT_VECTOR \
  917. reboot_interrupt smp_reboot_interrupt
  918. #endif
  919. #ifdef CONFIG_X86_UV
  920. apicinterrupt3 UV_BAU_MESSAGE \
  921. uv_bau_message_intr1 uv_bau_message_interrupt
  922. #endif
  923. apicinterrupt LOCAL_TIMER_VECTOR \
  924. apic_timer_interrupt smp_apic_timer_interrupt
  925. apicinterrupt X86_PLATFORM_IPI_VECTOR \
  926. x86_platform_ipi smp_x86_platform_ipi
  927. #ifdef CONFIG_HAVE_KVM
  928. apicinterrupt3 POSTED_INTR_VECTOR \
  929. kvm_posted_intr_ipi smp_kvm_posted_intr_ipi
  930. #endif
  931. #ifdef CONFIG_X86_MCE_THRESHOLD
  932. apicinterrupt THRESHOLD_APIC_VECTOR \
  933. threshold_interrupt smp_threshold_interrupt
  934. #endif
  935. #ifdef CONFIG_X86_THERMAL_VECTOR
  936. apicinterrupt THERMAL_APIC_VECTOR \
  937. thermal_interrupt smp_thermal_interrupt
  938. #endif
  939. #ifdef CONFIG_SMP
  940. apicinterrupt CALL_FUNCTION_SINGLE_VECTOR \
  941. call_function_single_interrupt smp_call_function_single_interrupt
  942. apicinterrupt CALL_FUNCTION_VECTOR \
  943. call_function_interrupt smp_call_function_interrupt
  944. apicinterrupt RESCHEDULE_VECTOR \
  945. reschedule_interrupt smp_reschedule_interrupt
  946. #endif
  947. apicinterrupt ERROR_APIC_VECTOR \
  948. error_interrupt smp_error_interrupt
  949. apicinterrupt SPURIOUS_APIC_VECTOR \
  950. spurious_interrupt smp_spurious_interrupt
  951. #ifdef CONFIG_IRQ_WORK
  952. apicinterrupt IRQ_WORK_VECTOR \
  953. irq_work_interrupt smp_irq_work_interrupt
  954. #endif
  955. /*
  956. * Exception entry points.
  957. */
  958. #define INIT_TSS_IST(x) PER_CPU_VAR(init_tss) + (TSS_ist + ((x) - 1) * 8)
  959. .macro idtentry sym do_sym has_error_code:req paranoid=0 shift_ist=-1
  960. ENTRY(\sym)
  961. /* Sanity check */
  962. .if \shift_ist != -1 && \paranoid == 0
  963. .error "using shift_ist requires paranoid=1"
  964. .endif
  965. .if \has_error_code
  966. XCPT_FRAME
  967. .else
  968. INTR_FRAME
  969. .endif
  970. ASM_CLAC
  971. PARAVIRT_ADJUST_EXCEPTION_FRAME
  972. .ifeq \has_error_code
  973. pushq_cfi $-1 /* ORIG_RAX: no syscall to restart */
  974. .endif
  975. subq $ORIG_RAX-R15, %rsp
  976. CFI_ADJUST_CFA_OFFSET ORIG_RAX-R15
  977. .if \paranoid
  978. call save_paranoid
  979. .else
  980. call error_entry
  981. .endif
  982. DEFAULT_FRAME 0
  983. .if \paranoid
  984. .if \shift_ist != -1
  985. TRACE_IRQS_OFF_DEBUG /* reload IDT in case of recursion */
  986. .else
  987. TRACE_IRQS_OFF
  988. .endif
  989. .endif
  990. movq %rsp,%rdi /* pt_regs pointer */
  991. .if \has_error_code
  992. movq ORIG_RAX(%rsp),%rsi /* get error code */
  993. movq $-1,ORIG_RAX(%rsp) /* no syscall to restart */
  994. .else
  995. xorl %esi,%esi /* no error code */
  996. .endif
  997. .if \shift_ist != -1
  998. subq $EXCEPTION_STKSZ, INIT_TSS_IST(\shift_ist)
  999. .endif
  1000. call \do_sym
  1001. .if \shift_ist != -1
  1002. addq $EXCEPTION_STKSZ, INIT_TSS_IST(\shift_ist)
  1003. .endif
  1004. .if \paranoid
  1005. jmp paranoid_exit /* %ebx: no swapgs flag */
  1006. .else
  1007. jmp error_exit /* %ebx: no swapgs flag */
  1008. .endif
  1009. CFI_ENDPROC
  1010. END(\sym)
  1011. .endm
  1012. #ifdef CONFIG_TRACING
  1013. .macro trace_idtentry sym do_sym has_error_code:req
  1014. idtentry trace(\sym) trace(\do_sym) has_error_code=\has_error_code
  1015. idtentry \sym \do_sym has_error_code=\has_error_code
  1016. .endm
  1017. #else
  1018. .macro trace_idtentry sym do_sym has_error_code:req
  1019. idtentry \sym \do_sym has_error_code=\has_error_code
  1020. .endm
  1021. #endif
  1022. idtentry divide_error do_divide_error has_error_code=0
  1023. idtentry overflow do_overflow has_error_code=0
  1024. idtentry bounds do_bounds has_error_code=0
  1025. idtentry invalid_op do_invalid_op has_error_code=0
  1026. idtentry device_not_available do_device_not_available has_error_code=0
  1027. idtentry double_fault __do_double_fault has_error_code=1 paranoid=1
  1028. idtentry coprocessor_segment_overrun do_coprocessor_segment_overrun has_error_code=0
  1029. idtentry invalid_TSS do_invalid_TSS has_error_code=1
  1030. idtentry segment_not_present do_segment_not_present has_error_code=1
  1031. idtentry spurious_interrupt_bug do_spurious_interrupt_bug has_error_code=0
  1032. idtentry coprocessor_error do_coprocessor_error has_error_code=0
  1033. idtentry alignment_check do_alignment_check has_error_code=1
  1034. idtentry simd_coprocessor_error do_simd_coprocessor_error has_error_code=0
  1035. /* Reload gs selector with exception handling */
  1036. /* edi: new selector */
  1037. ENTRY(native_load_gs_index)
  1038. CFI_STARTPROC
  1039. pushfq_cfi
  1040. DISABLE_INTERRUPTS(CLBR_ANY & ~CLBR_RDI)
  1041. SWAPGS
  1042. gs_change:
  1043. movl %edi,%gs
  1044. 2: mfence /* workaround */
  1045. SWAPGS
  1046. popfq_cfi
  1047. ret
  1048. CFI_ENDPROC
  1049. END(native_load_gs_index)
  1050. _ASM_EXTABLE(gs_change,bad_gs)
  1051. .section .fixup,"ax"
  1052. /* running with kernelgs */
  1053. bad_gs:
  1054. SWAPGS /* switch back to user gs */
  1055. xorl %eax,%eax
  1056. movl %eax,%gs
  1057. jmp 2b
  1058. .previous
  1059. /* Call softirq on interrupt stack. Interrupts are off. */
  1060. ENTRY(do_softirq_own_stack)
  1061. CFI_STARTPROC
  1062. pushq_cfi %rbp
  1063. CFI_REL_OFFSET rbp,0
  1064. mov %rsp,%rbp
  1065. CFI_DEF_CFA_REGISTER rbp
  1066. incl PER_CPU_VAR(irq_count)
  1067. cmove PER_CPU_VAR(irq_stack_ptr),%rsp
  1068. push %rbp # backlink for old unwinder
  1069. call __do_softirq
  1070. leaveq
  1071. CFI_RESTORE rbp
  1072. CFI_DEF_CFA_REGISTER rsp
  1073. CFI_ADJUST_CFA_OFFSET -8
  1074. decl PER_CPU_VAR(irq_count)
  1075. ret
  1076. CFI_ENDPROC
  1077. END(do_softirq_own_stack)
  1078. #ifdef CONFIG_XEN
  1079. idtentry xen_hypervisor_callback xen_do_hypervisor_callback has_error_code=0
  1080. /*
  1081. * A note on the "critical region" in our callback handler.
  1082. * We want to avoid stacking callback handlers due to events occurring
  1083. * during handling of the last event. To do this, we keep events disabled
  1084. * until we've done all processing. HOWEVER, we must enable events before
  1085. * popping the stack frame (can't be done atomically) and so it would still
  1086. * be possible to get enough handler activations to overflow the stack.
  1087. * Although unlikely, bugs of that kind are hard to track down, so we'd
  1088. * like to avoid the possibility.
  1089. * So, on entry to the handler we detect whether we interrupted an
  1090. * existing activation in its critical region -- if so, we pop the current
  1091. * activation and restart the handler using the previous one.
  1092. */
  1093. ENTRY(xen_do_hypervisor_callback) # do_hypervisor_callback(struct *pt_regs)
  1094. CFI_STARTPROC
  1095. /*
  1096. * Since we don't modify %rdi, evtchn_do_upall(struct *pt_regs) will
  1097. * see the correct pointer to the pt_regs
  1098. */
  1099. movq %rdi, %rsp # we don't return, adjust the stack frame
  1100. CFI_ENDPROC
  1101. DEFAULT_FRAME
  1102. 11: incl PER_CPU_VAR(irq_count)
  1103. movq %rsp,%rbp
  1104. CFI_DEF_CFA_REGISTER rbp
  1105. cmovzq PER_CPU_VAR(irq_stack_ptr),%rsp
  1106. pushq %rbp # backlink for old unwinder
  1107. call xen_evtchn_do_upcall
  1108. popq %rsp
  1109. CFI_DEF_CFA_REGISTER rsp
  1110. decl PER_CPU_VAR(irq_count)
  1111. jmp error_exit
  1112. CFI_ENDPROC
  1113. END(xen_do_hypervisor_callback)
  1114. /*
  1115. * Hypervisor uses this for application faults while it executes.
  1116. * We get here for two reasons:
  1117. * 1. Fault while reloading DS, ES, FS or GS
  1118. * 2. Fault while executing IRET
  1119. * Category 1 we do not need to fix up as Xen has already reloaded all segment
  1120. * registers that could be reloaded and zeroed the others.
  1121. * Category 2 we fix up by killing the current process. We cannot use the
  1122. * normal Linux return path in this case because if we use the IRET hypercall
  1123. * to pop the stack frame we end up in an infinite loop of failsafe callbacks.
  1124. * We distinguish between categories by comparing each saved segment register
  1125. * with its current contents: any discrepancy means we in category 1.
  1126. */
  1127. ENTRY(xen_failsafe_callback)
  1128. INTR_FRAME 1 (6*8)
  1129. /*CFI_REL_OFFSET gs,GS*/
  1130. /*CFI_REL_OFFSET fs,FS*/
  1131. /*CFI_REL_OFFSET es,ES*/
  1132. /*CFI_REL_OFFSET ds,DS*/
  1133. CFI_REL_OFFSET r11,8
  1134. CFI_REL_OFFSET rcx,0
  1135. movw %ds,%cx
  1136. cmpw %cx,0x10(%rsp)
  1137. CFI_REMEMBER_STATE
  1138. jne 1f
  1139. movw %es,%cx
  1140. cmpw %cx,0x18(%rsp)
  1141. jne 1f
  1142. movw %fs,%cx
  1143. cmpw %cx,0x20(%rsp)
  1144. jne 1f
  1145. movw %gs,%cx
  1146. cmpw %cx,0x28(%rsp)
  1147. jne 1f
  1148. /* All segments match their saved values => Category 2 (Bad IRET). */
  1149. movq (%rsp),%rcx
  1150. CFI_RESTORE rcx
  1151. movq 8(%rsp),%r11
  1152. CFI_RESTORE r11
  1153. addq $0x30,%rsp
  1154. CFI_ADJUST_CFA_OFFSET -0x30
  1155. pushq_cfi $0 /* RIP */
  1156. pushq_cfi %r11
  1157. pushq_cfi %rcx
  1158. jmp general_protection
  1159. CFI_RESTORE_STATE
  1160. 1: /* Segment mismatch => Category 1 (Bad segment). Retry the IRET. */
  1161. movq (%rsp),%rcx
  1162. CFI_RESTORE rcx
  1163. movq 8(%rsp),%r11
  1164. CFI_RESTORE r11
  1165. addq $0x30,%rsp
  1166. CFI_ADJUST_CFA_OFFSET -0x30
  1167. pushq_cfi $-1 /* orig_ax = -1 => not a system call */
  1168. SAVE_ALL
  1169. jmp error_exit
  1170. CFI_ENDPROC
  1171. END(xen_failsafe_callback)
  1172. apicinterrupt3 HYPERVISOR_CALLBACK_VECTOR \
  1173. xen_hvm_callback_vector xen_evtchn_do_upcall
  1174. #endif /* CONFIG_XEN */
  1175. #if IS_ENABLED(CONFIG_HYPERV)
  1176. apicinterrupt3 HYPERVISOR_CALLBACK_VECTOR \
  1177. hyperv_callback_vector hyperv_vector_handler
  1178. #endif /* CONFIG_HYPERV */
  1179. idtentry debug do_debug has_error_code=0 paranoid=1 shift_ist=DEBUG_STACK
  1180. idtentry int3 do_int3 has_error_code=0 paranoid=1 shift_ist=DEBUG_STACK
  1181. idtentry stack_segment do_stack_segment has_error_code=1 paranoid=1
  1182. #ifdef CONFIG_XEN
  1183. idtentry xen_debug do_debug has_error_code=0
  1184. idtentry xen_int3 do_int3 has_error_code=0
  1185. idtentry xen_stack_segment do_stack_segment has_error_code=1
  1186. #endif
  1187. idtentry general_protection do_general_protection has_error_code=1
  1188. trace_idtentry page_fault do_page_fault has_error_code=1
  1189. #ifdef CONFIG_KVM_GUEST
  1190. idtentry async_page_fault do_async_page_fault has_error_code=1
  1191. #endif
  1192. #ifdef CONFIG_X86_MCE
  1193. idtentry machine_check has_error_code=0 paranoid=1 do_sym=*machine_check_vector(%rip)
  1194. #endif
  1195. /*
  1196. * "Paranoid" exit path from exception stack.
  1197. * Paranoid because this is used by NMIs and cannot take
  1198. * any kernel state for granted.
  1199. * We don't do kernel preemption checks here, because only
  1200. * NMI should be common and it does not enable IRQs and
  1201. * cannot get reschedule ticks.
  1202. *
  1203. * "trace" is 0 for the NMI handler only, because irq-tracing
  1204. * is fundamentally NMI-unsafe. (we cannot change the soft and
  1205. * hard flags at once, atomically)
  1206. */
  1207. /* ebx: no swapgs flag */
  1208. ENTRY(paranoid_exit)
  1209. DEFAULT_FRAME
  1210. DISABLE_INTERRUPTS(CLBR_NONE)
  1211. TRACE_IRQS_OFF_DEBUG
  1212. testl %ebx,%ebx /* swapgs needed? */
  1213. jnz paranoid_restore
  1214. testl $3,CS(%rsp)
  1215. jnz paranoid_userspace
  1216. paranoid_swapgs:
  1217. TRACE_IRQS_IRETQ 0
  1218. SWAPGS_UNSAFE_STACK
  1219. RESTORE_ALL 8
  1220. jmp irq_return
  1221. paranoid_restore:
  1222. TRACE_IRQS_IRETQ_DEBUG 0
  1223. RESTORE_ALL 8
  1224. jmp irq_return
  1225. paranoid_userspace:
  1226. GET_THREAD_INFO(%rcx)
  1227. movl TI_flags(%rcx),%ebx
  1228. andl $_TIF_WORK_MASK,%ebx
  1229. jz paranoid_swapgs
  1230. movq %rsp,%rdi /* &pt_regs */
  1231. call sync_regs
  1232. movq %rax,%rsp /* switch stack for scheduling */
  1233. testl $_TIF_NEED_RESCHED,%ebx
  1234. jnz paranoid_schedule
  1235. movl %ebx,%edx /* arg3: thread flags */
  1236. TRACE_IRQS_ON
  1237. ENABLE_INTERRUPTS(CLBR_NONE)
  1238. xorl %esi,%esi /* arg2: oldset */
  1239. movq %rsp,%rdi /* arg1: &pt_regs */
  1240. call do_notify_resume
  1241. DISABLE_INTERRUPTS(CLBR_NONE)
  1242. TRACE_IRQS_OFF
  1243. jmp paranoid_userspace
  1244. paranoid_schedule:
  1245. TRACE_IRQS_ON
  1246. ENABLE_INTERRUPTS(CLBR_ANY)
  1247. SCHEDULE_USER
  1248. DISABLE_INTERRUPTS(CLBR_ANY)
  1249. TRACE_IRQS_OFF
  1250. jmp paranoid_userspace
  1251. CFI_ENDPROC
  1252. END(paranoid_exit)
  1253. /*
  1254. * Exception entry point. This expects an error code/orig_rax on the stack.
  1255. * returns in "no swapgs flag" in %ebx.
  1256. */
  1257. ENTRY(error_entry)
  1258. XCPT_FRAME
  1259. CFI_ADJUST_CFA_OFFSET 15*8
  1260. /* oldrax contains error code */
  1261. cld
  1262. movq_cfi rdi, RDI+8
  1263. movq_cfi rsi, RSI+8
  1264. movq_cfi rdx, RDX+8
  1265. movq_cfi rcx, RCX+8
  1266. movq_cfi rax, RAX+8
  1267. movq_cfi r8, R8+8
  1268. movq_cfi r9, R9+8
  1269. movq_cfi r10, R10+8
  1270. movq_cfi r11, R11+8
  1271. movq_cfi rbx, RBX+8
  1272. movq_cfi rbp, RBP+8
  1273. movq_cfi r12, R12+8
  1274. movq_cfi r13, R13+8
  1275. movq_cfi r14, R14+8
  1276. movq_cfi r15, R15+8
  1277. xorl %ebx,%ebx
  1278. testl $3,CS+8(%rsp)
  1279. je error_kernelspace
  1280. error_swapgs:
  1281. SWAPGS
  1282. error_sti:
  1283. TRACE_IRQS_OFF
  1284. ret
  1285. /*
  1286. * There are two places in the kernel that can potentially fault with
  1287. * usergs. Handle them here. The exception handlers after iret run with
  1288. * kernel gs again, so don't set the user space flag. B stepping K8s
  1289. * sometimes report an truncated RIP for IRET exceptions returning to
  1290. * compat mode. Check for these here too.
  1291. */
  1292. error_kernelspace:
  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)