entry_64.S 45 KB

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