entry_64.S 43 KB

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