entry_64.S 44 KB

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