entry_64.S 47 KB

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