entry_64.S 37 KB

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