entry_64.S 44 KB

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