entry_64.S 46 KB

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