entry_32.S 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166
  1. /*
  2. * Copyright (C) 1991,1992 Linus Torvalds
  3. *
  4. * entry_32.S contains the system-call and low-level fault and trap handling routines.
  5. *
  6. * Stack layout while running C code:
  7. * ptrace needs to have all registers on the stack.
  8. * If the order here is changed, it needs to be
  9. * updated in fork.c:copy_process(), signal.c:do_signal(),
  10. * ptrace.c and ptrace.h
  11. *
  12. * 0(%esp) - %ebx
  13. * 4(%esp) - %ecx
  14. * 8(%esp) - %edx
  15. * C(%esp) - %esi
  16. * 10(%esp) - %edi
  17. * 14(%esp) - %ebp
  18. * 18(%esp) - %eax
  19. * 1C(%esp) - %ds
  20. * 20(%esp) - %es
  21. * 24(%esp) - %fs
  22. * 28(%esp) - %gs saved iff !CONFIG_X86_32_LAZY_GS
  23. * 2C(%esp) - orig_eax
  24. * 30(%esp) - %eip
  25. * 34(%esp) - %cs
  26. * 38(%esp) - %eflags
  27. * 3C(%esp) - %oldesp
  28. * 40(%esp) - %oldss
  29. */
  30. #include <linux/linkage.h>
  31. #include <linux/err.h>
  32. #include <asm/thread_info.h>
  33. #include <asm/irqflags.h>
  34. #include <asm/errno.h>
  35. #include <asm/segment.h>
  36. #include <asm/smp.h>
  37. #include <asm/page_types.h>
  38. #include <asm/percpu.h>
  39. #include <asm/processor-flags.h>
  40. #include <asm/ftrace.h>
  41. #include <asm/irq_vectors.h>
  42. #include <asm/cpufeatures.h>
  43. #include <asm/alternative-asm.h>
  44. #include <asm/asm.h>
  45. #include <asm/smap.h>
  46. .section .entry.text, "ax"
  47. /*
  48. * We use macros for low-level operations which need to be overridden
  49. * for paravirtualization. The following will never clobber any registers:
  50. * INTERRUPT_RETURN (aka. "iret")
  51. * GET_CR0_INTO_EAX (aka. "movl %cr0, %eax")
  52. * ENABLE_INTERRUPTS_SYSEXIT (aka "sti; sysexit").
  53. *
  54. * For DISABLE_INTERRUPTS/ENABLE_INTERRUPTS (aka "cli"/"sti"), you must
  55. * specify what registers can be overwritten (CLBR_NONE, CLBR_EAX/EDX/ECX/ANY).
  56. * Allowing a register to be clobbered can shrink the paravirt replacement
  57. * enough to patch inline, increasing performance.
  58. */
  59. #ifdef CONFIG_PREEMPT
  60. # define preempt_stop(clobbers) DISABLE_INTERRUPTS(clobbers); TRACE_IRQS_OFF
  61. #else
  62. # define preempt_stop(clobbers)
  63. # define resume_kernel restore_all
  64. #endif
  65. .macro TRACE_IRQS_IRET
  66. #ifdef CONFIG_TRACE_IRQFLAGS
  67. testl $X86_EFLAGS_IF, PT_EFLAGS(%esp) # interrupts off?
  68. jz 1f
  69. TRACE_IRQS_ON
  70. 1:
  71. #endif
  72. .endm
  73. /*
  74. * User gs save/restore
  75. *
  76. * %gs is used for userland TLS and kernel only uses it for stack
  77. * canary which is required to be at %gs:20 by gcc. Read the comment
  78. * at the top of stackprotector.h for more info.
  79. *
  80. * Local labels 98 and 99 are used.
  81. */
  82. #ifdef CONFIG_X86_32_LAZY_GS
  83. /* unfortunately push/pop can't be no-op */
  84. .macro PUSH_GS
  85. pushl $0
  86. .endm
  87. .macro POP_GS pop=0
  88. addl $(4 + \pop), %esp
  89. .endm
  90. .macro POP_GS_EX
  91. .endm
  92. /* all the rest are no-op */
  93. .macro PTGS_TO_GS
  94. .endm
  95. .macro PTGS_TO_GS_EX
  96. .endm
  97. .macro GS_TO_REG reg
  98. .endm
  99. .macro REG_TO_PTGS reg
  100. .endm
  101. .macro SET_KERNEL_GS reg
  102. .endm
  103. #else /* CONFIG_X86_32_LAZY_GS */
  104. .macro PUSH_GS
  105. pushl %gs
  106. .endm
  107. .macro POP_GS pop=0
  108. 98: popl %gs
  109. .if \pop <> 0
  110. add $\pop, %esp
  111. .endif
  112. .endm
  113. .macro POP_GS_EX
  114. .pushsection .fixup, "ax"
  115. 99: movl $0, (%esp)
  116. jmp 98b
  117. .popsection
  118. _ASM_EXTABLE(98b, 99b)
  119. .endm
  120. .macro PTGS_TO_GS
  121. 98: mov PT_GS(%esp), %gs
  122. .endm
  123. .macro PTGS_TO_GS_EX
  124. .pushsection .fixup, "ax"
  125. 99: movl $0, PT_GS(%esp)
  126. jmp 98b
  127. .popsection
  128. _ASM_EXTABLE(98b, 99b)
  129. .endm
  130. .macro GS_TO_REG reg
  131. movl %gs, \reg
  132. .endm
  133. .macro REG_TO_PTGS reg
  134. movl \reg, PT_GS(%esp)
  135. .endm
  136. .macro SET_KERNEL_GS reg
  137. movl $(__KERNEL_STACK_CANARY), \reg
  138. movl \reg, %gs
  139. .endm
  140. #endif /* CONFIG_X86_32_LAZY_GS */
  141. .macro SAVE_ALL pt_regs_ax=%eax
  142. cld
  143. PUSH_GS
  144. pushl %fs
  145. pushl %es
  146. pushl %ds
  147. pushl \pt_regs_ax
  148. pushl %ebp
  149. pushl %edi
  150. pushl %esi
  151. pushl %edx
  152. pushl %ecx
  153. pushl %ebx
  154. movl $(__USER_DS), %edx
  155. movl %edx, %ds
  156. movl %edx, %es
  157. movl $(__KERNEL_PERCPU), %edx
  158. movl %edx, %fs
  159. SET_KERNEL_GS %edx
  160. .endm
  161. .macro RESTORE_INT_REGS
  162. popl %ebx
  163. popl %ecx
  164. popl %edx
  165. popl %esi
  166. popl %edi
  167. popl %ebp
  168. popl %eax
  169. .endm
  170. .macro RESTORE_REGS pop=0
  171. RESTORE_INT_REGS
  172. 1: popl %ds
  173. 2: popl %es
  174. 3: popl %fs
  175. POP_GS \pop
  176. .pushsection .fixup, "ax"
  177. 4: movl $0, (%esp)
  178. jmp 1b
  179. 5: movl $0, (%esp)
  180. jmp 2b
  181. 6: movl $0, (%esp)
  182. jmp 3b
  183. .popsection
  184. _ASM_EXTABLE(1b, 4b)
  185. _ASM_EXTABLE(2b, 5b)
  186. _ASM_EXTABLE(3b, 6b)
  187. POP_GS_EX
  188. .endm
  189. ENTRY(ret_from_fork)
  190. pushl %eax
  191. call schedule_tail
  192. popl %eax
  193. /* When we fork, we trace the syscall return in the child, too. */
  194. movl %esp, %eax
  195. call syscall_return_slowpath
  196. jmp restore_all
  197. END(ret_from_fork)
  198. ENTRY(ret_from_kernel_thread)
  199. pushl %eax
  200. call schedule_tail
  201. popl %eax
  202. movl PT_EBP(%esp), %eax
  203. call *PT_EBX(%esp)
  204. movl $0, PT_EAX(%esp)
  205. /*
  206. * Kernel threads return to userspace as if returning from a syscall.
  207. * We should check whether anything actually uses this path and, if so,
  208. * consider switching it over to ret_from_fork.
  209. */
  210. movl %esp, %eax
  211. call syscall_return_slowpath
  212. jmp restore_all
  213. ENDPROC(ret_from_kernel_thread)
  214. /*
  215. * Return to user mode is not as complex as all this looks,
  216. * but we want the default path for a system call return to
  217. * go as quickly as possible which is why some of this is
  218. * less clear than it otherwise should be.
  219. */
  220. # userspace resumption stub bypassing syscall exit tracing
  221. ALIGN
  222. ret_from_exception:
  223. preempt_stop(CLBR_ANY)
  224. ret_from_intr:
  225. #ifdef CONFIG_VM86
  226. movl PT_EFLAGS(%esp), %eax # mix EFLAGS and CS
  227. movb PT_CS(%esp), %al
  228. andl $(X86_EFLAGS_VM | SEGMENT_RPL_MASK), %eax
  229. #else
  230. /*
  231. * We can be coming here from child spawned by kernel_thread().
  232. */
  233. movl PT_CS(%esp), %eax
  234. andl $SEGMENT_RPL_MASK, %eax
  235. #endif
  236. cmpl $USER_RPL, %eax
  237. jb resume_kernel # not returning to v8086 or userspace
  238. ENTRY(resume_userspace)
  239. DISABLE_INTERRUPTS(CLBR_ANY)
  240. TRACE_IRQS_OFF
  241. movl %esp, %eax
  242. call prepare_exit_to_usermode
  243. jmp restore_all
  244. END(ret_from_exception)
  245. #ifdef CONFIG_PREEMPT
  246. ENTRY(resume_kernel)
  247. DISABLE_INTERRUPTS(CLBR_ANY)
  248. need_resched:
  249. cmpl $0, PER_CPU_VAR(__preempt_count)
  250. jnz restore_all
  251. testl $X86_EFLAGS_IF, PT_EFLAGS(%esp) # interrupts off (exception path) ?
  252. jz restore_all
  253. call preempt_schedule_irq
  254. jmp need_resched
  255. END(resume_kernel)
  256. #endif
  257. GLOBAL(__begin_SYSENTER_singlestep_region)
  258. /*
  259. * All code from here through __end_SYSENTER_singlestep_region is subject
  260. * to being single-stepped if a user program sets TF and executes SYSENTER.
  261. * There is absolutely nothing that we can do to prevent this from happening
  262. * (thanks Intel!). To keep our handling of this situation as simple as
  263. * possible, we handle TF just like AC and NT, except that our #DB handler
  264. * will ignore all of the single-step traps generated in this range.
  265. */
  266. #ifdef CONFIG_XEN
  267. /*
  268. * Xen doesn't set %esp to be precisely what the normal SYSENTER
  269. * entry point expects, so fix it up before using the normal path.
  270. */
  271. ENTRY(xen_sysenter_target)
  272. addl $5*4, %esp /* remove xen-provided frame */
  273. jmp sysenter_past_esp
  274. #endif
  275. /*
  276. * 32-bit SYSENTER entry.
  277. *
  278. * 32-bit system calls through the vDSO's __kernel_vsyscall enter here
  279. * if X86_FEATURE_SEP is available. This is the preferred system call
  280. * entry on 32-bit systems.
  281. *
  282. * The SYSENTER instruction, in principle, should *only* occur in the
  283. * vDSO. In practice, a small number of Android devices were shipped
  284. * with a copy of Bionic that inlined a SYSENTER instruction. This
  285. * never happened in any of Google's Bionic versions -- it only happened
  286. * in a narrow range of Intel-provided versions.
  287. *
  288. * SYSENTER loads SS, ESP, CS, and EIP from previously programmed MSRs.
  289. * IF and VM in RFLAGS are cleared (IOW: interrupts are off).
  290. * SYSENTER does not save anything on the stack,
  291. * and does not save old EIP (!!!), ESP, or EFLAGS.
  292. *
  293. * To avoid losing track of EFLAGS.VM (and thus potentially corrupting
  294. * user and/or vm86 state), we explicitly disable the SYSENTER
  295. * instruction in vm86 mode by reprogramming the MSRs.
  296. *
  297. * Arguments:
  298. * eax system call number
  299. * ebx arg1
  300. * ecx arg2
  301. * edx arg3
  302. * esi arg4
  303. * edi arg5
  304. * ebp user stack
  305. * 0(%ebp) arg6
  306. */
  307. ENTRY(entry_SYSENTER_32)
  308. movl TSS_sysenter_sp0(%esp), %esp
  309. sysenter_past_esp:
  310. pushl $__USER_DS /* pt_regs->ss */
  311. pushl %ebp /* pt_regs->sp (stashed in bp) */
  312. pushfl /* pt_regs->flags (except IF = 0) */
  313. orl $X86_EFLAGS_IF, (%esp) /* Fix IF */
  314. pushl $__USER_CS /* pt_regs->cs */
  315. pushl $0 /* pt_regs->ip = 0 (placeholder) */
  316. pushl %eax /* pt_regs->orig_ax */
  317. SAVE_ALL pt_regs_ax=$-ENOSYS /* save rest */
  318. /*
  319. * SYSENTER doesn't filter flags, so we need to clear NT, AC
  320. * and TF ourselves. To save a few cycles, we can check whether
  321. * either was set instead of doing an unconditional popfq.
  322. * This needs to happen before enabling interrupts so that
  323. * we don't get preempted with NT set.
  324. *
  325. * If TF is set, we will single-step all the way to here -- do_debug
  326. * will ignore all the traps. (Yes, this is slow, but so is
  327. * single-stepping in general. This allows us to avoid having
  328. * a more complicated code to handle the case where a user program
  329. * forces us to single-step through the SYSENTER entry code.)
  330. *
  331. * NB.: .Lsysenter_fix_flags is a label with the code under it moved
  332. * out-of-line as an optimization: NT is unlikely to be set in the
  333. * majority of the cases and instead of polluting the I$ unnecessarily,
  334. * we're keeping that code behind a branch which will predict as
  335. * not-taken and therefore its instructions won't be fetched.
  336. */
  337. testl $X86_EFLAGS_NT|X86_EFLAGS_AC|X86_EFLAGS_TF, PT_EFLAGS(%esp)
  338. jnz .Lsysenter_fix_flags
  339. .Lsysenter_flags_fixed:
  340. /*
  341. * User mode is traced as though IRQs are on, and SYSENTER
  342. * turned them off.
  343. */
  344. TRACE_IRQS_OFF
  345. movl %esp, %eax
  346. call do_fast_syscall_32
  347. /* XEN PV guests always use IRET path */
  348. ALTERNATIVE "testl %eax, %eax; jz .Lsyscall_32_done", \
  349. "jmp .Lsyscall_32_done", X86_FEATURE_XENPV
  350. /* Opportunistic SYSEXIT */
  351. TRACE_IRQS_ON /* User mode traces as IRQs on. */
  352. movl PT_EIP(%esp), %edx /* pt_regs->ip */
  353. movl PT_OLDESP(%esp), %ecx /* pt_regs->sp */
  354. 1: mov PT_FS(%esp), %fs
  355. PTGS_TO_GS
  356. popl %ebx /* pt_regs->bx */
  357. addl $2*4, %esp /* skip pt_regs->cx and pt_regs->dx */
  358. popl %esi /* pt_regs->si */
  359. popl %edi /* pt_regs->di */
  360. popl %ebp /* pt_regs->bp */
  361. popl %eax /* pt_regs->ax */
  362. /*
  363. * Restore all flags except IF. (We restore IF separately because
  364. * STI gives a one-instruction window in which we won't be interrupted,
  365. * whereas POPF does not.)
  366. */
  367. addl $PT_EFLAGS-PT_DS, %esp /* point esp at pt_regs->flags */
  368. btr $X86_EFLAGS_IF_BIT, (%esp)
  369. popfl
  370. /*
  371. * Return back to the vDSO, which will pop ecx and edx.
  372. * Don't bother with DS and ES (they already contain __USER_DS).
  373. */
  374. sti
  375. sysexit
  376. .pushsection .fixup, "ax"
  377. 2: movl $0, PT_FS(%esp)
  378. jmp 1b
  379. .popsection
  380. _ASM_EXTABLE(1b, 2b)
  381. PTGS_TO_GS_EX
  382. .Lsysenter_fix_flags:
  383. pushl $X86_EFLAGS_FIXED
  384. popfl
  385. jmp .Lsysenter_flags_fixed
  386. GLOBAL(__end_SYSENTER_singlestep_region)
  387. ENDPROC(entry_SYSENTER_32)
  388. /*
  389. * 32-bit legacy system call entry.
  390. *
  391. * 32-bit x86 Linux system calls traditionally used the INT $0x80
  392. * instruction. INT $0x80 lands here.
  393. *
  394. * This entry point can be used by any 32-bit perform system calls.
  395. * Instances of INT $0x80 can be found inline in various programs and
  396. * libraries. It is also used by the vDSO's __kernel_vsyscall
  397. * fallback for hardware that doesn't support a faster entry method.
  398. * Restarted 32-bit system calls also fall back to INT $0x80
  399. * regardless of what instruction was originally used to do the system
  400. * call. (64-bit programs can use INT $0x80 as well, but they can
  401. * only run on 64-bit kernels and therefore land in
  402. * entry_INT80_compat.)
  403. *
  404. * This is considered a slow path. It is not used by most libc
  405. * implementations on modern hardware except during process startup.
  406. *
  407. * Arguments:
  408. * eax system call number
  409. * ebx arg1
  410. * ecx arg2
  411. * edx arg3
  412. * esi arg4
  413. * edi arg5
  414. * ebp arg6
  415. */
  416. ENTRY(entry_INT80_32)
  417. ASM_CLAC
  418. pushl %eax /* pt_regs->orig_ax */
  419. SAVE_ALL pt_regs_ax=$-ENOSYS /* save rest */
  420. /*
  421. * User mode is traced as though IRQs are on, and the interrupt gate
  422. * turned them off.
  423. */
  424. TRACE_IRQS_OFF
  425. movl %esp, %eax
  426. call do_int80_syscall_32
  427. .Lsyscall_32_done:
  428. restore_all:
  429. TRACE_IRQS_IRET
  430. restore_all_notrace:
  431. #ifdef CONFIG_X86_ESPFIX32
  432. ALTERNATIVE "jmp restore_nocheck", "", X86_BUG_ESPFIX
  433. movl PT_EFLAGS(%esp), %eax # mix EFLAGS, SS and CS
  434. /*
  435. * Warning: PT_OLDSS(%esp) contains the wrong/random values if we
  436. * are returning to the kernel.
  437. * See comments in process.c:copy_thread() for details.
  438. */
  439. movb PT_OLDSS(%esp), %ah
  440. movb PT_CS(%esp), %al
  441. andl $(X86_EFLAGS_VM | (SEGMENT_TI_MASK << 8) | SEGMENT_RPL_MASK), %eax
  442. cmpl $((SEGMENT_LDT << 8) | USER_RPL), %eax
  443. je ldt_ss # returning to user-space with LDT SS
  444. #endif
  445. restore_nocheck:
  446. RESTORE_REGS 4 # skip orig_eax/error_code
  447. irq_return:
  448. INTERRUPT_RETURN
  449. .section .fixup, "ax"
  450. ENTRY(iret_exc )
  451. pushl $0 # no error code
  452. pushl $do_iret_error
  453. jmp error_code
  454. .previous
  455. _ASM_EXTABLE(irq_return, iret_exc)
  456. #ifdef CONFIG_X86_ESPFIX32
  457. ldt_ss:
  458. /*
  459. * Setup and switch to ESPFIX stack
  460. *
  461. * We're returning to userspace with a 16 bit stack. The CPU will not
  462. * restore the high word of ESP for us on executing iret... This is an
  463. * "official" bug of all the x86-compatible CPUs, which we can work
  464. * around to make dosemu and wine happy. We do this by preloading the
  465. * high word of ESP with the high word of the userspace ESP while
  466. * compensating for the offset by changing to the ESPFIX segment with
  467. * a base address that matches for the difference.
  468. */
  469. #define GDT_ESPFIX_SS PER_CPU_VAR(gdt_page) + (GDT_ENTRY_ESPFIX_SS * 8)
  470. mov %esp, %edx /* load kernel esp */
  471. mov PT_OLDESP(%esp), %eax /* load userspace esp */
  472. mov %dx, %ax /* eax: new kernel esp */
  473. sub %eax, %edx /* offset (low word is 0) */
  474. shr $16, %edx
  475. mov %dl, GDT_ESPFIX_SS + 4 /* bits 16..23 */
  476. mov %dh, GDT_ESPFIX_SS + 7 /* bits 24..31 */
  477. pushl $__ESPFIX_SS
  478. pushl %eax /* new kernel esp */
  479. /*
  480. * Disable interrupts, but do not irqtrace this section: we
  481. * will soon execute iret and the tracer was already set to
  482. * the irqstate after the IRET:
  483. */
  484. DISABLE_INTERRUPTS(CLBR_EAX)
  485. lss (%esp), %esp /* switch to espfix segment */
  486. jmp restore_nocheck
  487. #endif
  488. ENDPROC(entry_INT80_32)
  489. .macro FIXUP_ESPFIX_STACK
  490. /*
  491. * Switch back for ESPFIX stack to the normal zerobased stack
  492. *
  493. * We can't call C functions using the ESPFIX stack. This code reads
  494. * the high word of the segment base from the GDT and swiches to the
  495. * normal stack and adjusts ESP with the matching offset.
  496. */
  497. #ifdef CONFIG_X86_ESPFIX32
  498. /* fixup the stack */
  499. mov GDT_ESPFIX_SS + 4, %al /* bits 16..23 */
  500. mov GDT_ESPFIX_SS + 7, %ah /* bits 24..31 */
  501. shl $16, %eax
  502. addl %esp, %eax /* the adjusted stack pointer */
  503. pushl $__KERNEL_DS
  504. pushl %eax
  505. lss (%esp), %esp /* switch to the normal stack segment */
  506. #endif
  507. .endm
  508. .macro UNWIND_ESPFIX_STACK
  509. #ifdef CONFIG_X86_ESPFIX32
  510. movl %ss, %eax
  511. /* see if on espfix stack */
  512. cmpw $__ESPFIX_SS, %ax
  513. jne 27f
  514. movl $__KERNEL_DS, %eax
  515. movl %eax, %ds
  516. movl %eax, %es
  517. /* switch to normal stack */
  518. FIXUP_ESPFIX_STACK
  519. 27:
  520. #endif
  521. .endm
  522. /*
  523. * Build the entry stubs with some assembler magic.
  524. * We pack 1 stub into every 8-byte block.
  525. */
  526. .align 8
  527. ENTRY(irq_entries_start)
  528. vector=FIRST_EXTERNAL_VECTOR
  529. .rept (FIRST_SYSTEM_VECTOR - FIRST_EXTERNAL_VECTOR)
  530. pushl $(~vector+0x80) /* Note: always in signed byte range */
  531. vector=vector+1
  532. jmp common_interrupt
  533. .align 8
  534. .endr
  535. END(irq_entries_start)
  536. /*
  537. * the CPU automatically disables interrupts when executing an IRQ vector,
  538. * so IRQ-flags tracing has to follow that:
  539. */
  540. .p2align CONFIG_X86_L1_CACHE_SHIFT
  541. common_interrupt:
  542. ASM_CLAC
  543. addl $-0x80, (%esp) /* Adjust vector into the [-256, -1] range */
  544. SAVE_ALL
  545. TRACE_IRQS_OFF
  546. movl %esp, %eax
  547. call do_IRQ
  548. jmp ret_from_intr
  549. ENDPROC(common_interrupt)
  550. #define BUILD_INTERRUPT3(name, nr, fn) \
  551. ENTRY(name) \
  552. ASM_CLAC; \
  553. pushl $~(nr); \
  554. SAVE_ALL; \
  555. TRACE_IRQS_OFF \
  556. movl %esp, %eax; \
  557. call fn; \
  558. jmp ret_from_intr; \
  559. ENDPROC(name)
  560. #ifdef CONFIG_TRACING
  561. # define TRACE_BUILD_INTERRUPT(name, nr) BUILD_INTERRUPT3(trace_##name, nr, smp_trace_##name)
  562. #else
  563. # define TRACE_BUILD_INTERRUPT(name, nr)
  564. #endif
  565. #define BUILD_INTERRUPT(name, nr) \
  566. BUILD_INTERRUPT3(name, nr, smp_##name); \
  567. TRACE_BUILD_INTERRUPT(name, nr)
  568. /* The include is where all of the SMP etc. interrupts come from */
  569. #include <asm/entry_arch.h>
  570. ENTRY(coprocessor_error)
  571. ASM_CLAC
  572. pushl $0
  573. pushl $do_coprocessor_error
  574. jmp error_code
  575. END(coprocessor_error)
  576. ENTRY(simd_coprocessor_error)
  577. ASM_CLAC
  578. pushl $0
  579. #ifdef CONFIG_X86_INVD_BUG
  580. /* AMD 486 bug: invd from userspace calls exception 19 instead of #GP */
  581. ALTERNATIVE "pushl $do_general_protection", \
  582. "pushl $do_simd_coprocessor_error", \
  583. X86_FEATURE_XMM
  584. #else
  585. pushl $do_simd_coprocessor_error
  586. #endif
  587. jmp error_code
  588. END(simd_coprocessor_error)
  589. ENTRY(device_not_available)
  590. ASM_CLAC
  591. pushl $-1 # mark this as an int
  592. pushl $do_device_not_available
  593. jmp error_code
  594. END(device_not_available)
  595. #ifdef CONFIG_PARAVIRT
  596. ENTRY(native_iret)
  597. iret
  598. _ASM_EXTABLE(native_iret, iret_exc)
  599. END(native_iret)
  600. #endif
  601. ENTRY(overflow)
  602. ASM_CLAC
  603. pushl $0
  604. pushl $do_overflow
  605. jmp error_code
  606. END(overflow)
  607. ENTRY(bounds)
  608. ASM_CLAC
  609. pushl $0
  610. pushl $do_bounds
  611. jmp error_code
  612. END(bounds)
  613. ENTRY(invalid_op)
  614. ASM_CLAC
  615. pushl $0
  616. pushl $do_invalid_op
  617. jmp error_code
  618. END(invalid_op)
  619. ENTRY(coprocessor_segment_overrun)
  620. ASM_CLAC
  621. pushl $0
  622. pushl $do_coprocessor_segment_overrun
  623. jmp error_code
  624. END(coprocessor_segment_overrun)
  625. ENTRY(invalid_TSS)
  626. ASM_CLAC
  627. pushl $do_invalid_TSS
  628. jmp error_code
  629. END(invalid_TSS)
  630. ENTRY(segment_not_present)
  631. ASM_CLAC
  632. pushl $do_segment_not_present
  633. jmp error_code
  634. END(segment_not_present)
  635. ENTRY(stack_segment)
  636. ASM_CLAC
  637. pushl $do_stack_segment
  638. jmp error_code
  639. END(stack_segment)
  640. ENTRY(alignment_check)
  641. ASM_CLAC
  642. pushl $do_alignment_check
  643. jmp error_code
  644. END(alignment_check)
  645. ENTRY(divide_error)
  646. ASM_CLAC
  647. pushl $0 # no error code
  648. pushl $do_divide_error
  649. jmp error_code
  650. END(divide_error)
  651. #ifdef CONFIG_X86_MCE
  652. ENTRY(machine_check)
  653. ASM_CLAC
  654. pushl $0
  655. pushl machine_check_vector
  656. jmp error_code
  657. END(machine_check)
  658. #endif
  659. ENTRY(spurious_interrupt_bug)
  660. ASM_CLAC
  661. pushl $0
  662. pushl $do_spurious_interrupt_bug
  663. jmp error_code
  664. END(spurious_interrupt_bug)
  665. #ifdef CONFIG_XEN
  666. ENTRY(xen_hypervisor_callback)
  667. pushl $-1 /* orig_ax = -1 => not a system call */
  668. SAVE_ALL
  669. TRACE_IRQS_OFF
  670. /*
  671. * Check to see if we got the event in the critical
  672. * region in xen_iret_direct, after we've reenabled
  673. * events and checked for pending events. This simulates
  674. * iret instruction's behaviour where it delivers a
  675. * pending interrupt when enabling interrupts:
  676. */
  677. movl PT_EIP(%esp), %eax
  678. cmpl $xen_iret_start_crit, %eax
  679. jb 1f
  680. cmpl $xen_iret_end_crit, %eax
  681. jae 1f
  682. jmp xen_iret_crit_fixup
  683. ENTRY(xen_do_upcall)
  684. 1: mov %esp, %eax
  685. call xen_evtchn_do_upcall
  686. #ifndef CONFIG_PREEMPT
  687. call xen_maybe_preempt_hcall
  688. #endif
  689. jmp ret_from_intr
  690. ENDPROC(xen_hypervisor_callback)
  691. /*
  692. * Hypervisor uses this for application faults while it executes.
  693. * We get here for two reasons:
  694. * 1. Fault while reloading DS, ES, FS or GS
  695. * 2. Fault while executing IRET
  696. * Category 1 we fix up by reattempting the load, and zeroing the segment
  697. * register if the load fails.
  698. * Category 2 we fix up by jumping to do_iret_error. We cannot use the
  699. * normal Linux return path in this case because if we use the IRET hypercall
  700. * to pop the stack frame we end up in an infinite loop of failsafe callbacks.
  701. * We distinguish between categories by maintaining a status value in EAX.
  702. */
  703. ENTRY(xen_failsafe_callback)
  704. pushl %eax
  705. movl $1, %eax
  706. 1: mov 4(%esp), %ds
  707. 2: mov 8(%esp), %es
  708. 3: mov 12(%esp), %fs
  709. 4: mov 16(%esp), %gs
  710. /* EAX == 0 => Category 1 (Bad segment)
  711. EAX != 0 => Category 2 (Bad IRET) */
  712. testl %eax, %eax
  713. popl %eax
  714. lea 16(%esp), %esp
  715. jz 5f
  716. jmp iret_exc
  717. 5: pushl $-1 /* orig_ax = -1 => not a system call */
  718. SAVE_ALL
  719. jmp ret_from_exception
  720. .section .fixup, "ax"
  721. 6: xorl %eax, %eax
  722. movl %eax, 4(%esp)
  723. jmp 1b
  724. 7: xorl %eax, %eax
  725. movl %eax, 8(%esp)
  726. jmp 2b
  727. 8: xorl %eax, %eax
  728. movl %eax, 12(%esp)
  729. jmp 3b
  730. 9: xorl %eax, %eax
  731. movl %eax, 16(%esp)
  732. jmp 4b
  733. .previous
  734. _ASM_EXTABLE(1b, 6b)
  735. _ASM_EXTABLE(2b, 7b)
  736. _ASM_EXTABLE(3b, 8b)
  737. _ASM_EXTABLE(4b, 9b)
  738. ENDPROC(xen_failsafe_callback)
  739. BUILD_INTERRUPT3(xen_hvm_callback_vector, HYPERVISOR_CALLBACK_VECTOR,
  740. xen_evtchn_do_upcall)
  741. #endif /* CONFIG_XEN */
  742. #if IS_ENABLED(CONFIG_HYPERV)
  743. BUILD_INTERRUPT3(hyperv_callback_vector, HYPERVISOR_CALLBACK_VECTOR,
  744. hyperv_vector_handler)
  745. #endif /* CONFIG_HYPERV */
  746. #ifdef CONFIG_FUNCTION_TRACER
  747. #ifdef CONFIG_DYNAMIC_FTRACE
  748. ENTRY(mcount)
  749. ret
  750. END(mcount)
  751. ENTRY(ftrace_caller)
  752. pushl %eax
  753. pushl %ecx
  754. pushl %edx
  755. pushl $0 /* Pass NULL as regs pointer */
  756. movl 4*4(%esp), %eax
  757. movl 0x4(%ebp), %edx
  758. movl function_trace_op, %ecx
  759. subl $MCOUNT_INSN_SIZE, %eax
  760. .globl ftrace_call
  761. ftrace_call:
  762. call ftrace_stub
  763. addl $4, %esp /* skip NULL pointer */
  764. popl %edx
  765. popl %ecx
  766. popl %eax
  767. ftrace_ret:
  768. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  769. .globl ftrace_graph_call
  770. ftrace_graph_call:
  771. jmp ftrace_stub
  772. #endif
  773. .globl ftrace_stub
  774. ftrace_stub:
  775. ret
  776. END(ftrace_caller)
  777. ENTRY(ftrace_regs_caller)
  778. pushf /* push flags before compare (in cs location) */
  779. /*
  780. * i386 does not save SS and ESP when coming from kernel.
  781. * Instead, to get sp, &regs->sp is used (see ptrace.h).
  782. * Unfortunately, that means eflags must be at the same location
  783. * as the current return ip is. We move the return ip into the
  784. * ip location, and move flags into the return ip location.
  785. */
  786. pushl 4(%esp) /* save return ip into ip slot */
  787. pushl $0 /* Load 0 into orig_ax */
  788. pushl %gs
  789. pushl %fs
  790. pushl %es
  791. pushl %ds
  792. pushl %eax
  793. pushl %ebp
  794. pushl %edi
  795. pushl %esi
  796. pushl %edx
  797. pushl %ecx
  798. pushl %ebx
  799. movl 13*4(%esp), %eax /* Get the saved flags */
  800. movl %eax, 14*4(%esp) /* Move saved flags into regs->flags location */
  801. /* clobbering return ip */
  802. movl $__KERNEL_CS, 13*4(%esp)
  803. movl 12*4(%esp), %eax /* Load ip (1st parameter) */
  804. subl $MCOUNT_INSN_SIZE, %eax /* Adjust ip */
  805. movl 0x4(%ebp), %edx /* Load parent ip (2nd parameter) */
  806. movl function_trace_op, %ecx /* Save ftrace_pos in 3rd parameter */
  807. pushl %esp /* Save pt_regs as 4th parameter */
  808. GLOBAL(ftrace_regs_call)
  809. call ftrace_stub
  810. addl $4, %esp /* Skip pt_regs */
  811. movl 14*4(%esp), %eax /* Move flags back into cs */
  812. movl %eax, 13*4(%esp) /* Needed to keep addl from modifying flags */
  813. movl 12*4(%esp), %eax /* Get return ip from regs->ip */
  814. movl %eax, 14*4(%esp) /* Put return ip back for ret */
  815. popl %ebx
  816. popl %ecx
  817. popl %edx
  818. popl %esi
  819. popl %edi
  820. popl %ebp
  821. popl %eax
  822. popl %ds
  823. popl %es
  824. popl %fs
  825. popl %gs
  826. addl $8, %esp /* Skip orig_ax and ip */
  827. popf /* Pop flags at end (no addl to corrupt flags) */
  828. jmp ftrace_ret
  829. popf
  830. jmp ftrace_stub
  831. #else /* ! CONFIG_DYNAMIC_FTRACE */
  832. ENTRY(mcount)
  833. cmpl $__PAGE_OFFSET, %esp
  834. jb ftrace_stub /* Paging not enabled yet? */
  835. cmpl $ftrace_stub, ftrace_trace_function
  836. jnz trace
  837. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  838. cmpl $ftrace_stub, ftrace_graph_return
  839. jnz ftrace_graph_caller
  840. cmpl $ftrace_graph_entry_stub, ftrace_graph_entry
  841. jnz ftrace_graph_caller
  842. #endif
  843. .globl ftrace_stub
  844. ftrace_stub:
  845. ret
  846. /* taken from glibc */
  847. trace:
  848. pushl %eax
  849. pushl %ecx
  850. pushl %edx
  851. movl 0xc(%esp), %eax
  852. movl 0x4(%ebp), %edx
  853. subl $MCOUNT_INSN_SIZE, %eax
  854. call *ftrace_trace_function
  855. popl %edx
  856. popl %ecx
  857. popl %eax
  858. jmp ftrace_stub
  859. END(mcount)
  860. #endif /* CONFIG_DYNAMIC_FTRACE */
  861. #endif /* CONFIG_FUNCTION_TRACER */
  862. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  863. ENTRY(ftrace_graph_caller)
  864. pushl %eax
  865. pushl %ecx
  866. pushl %edx
  867. movl 0xc(%esp), %eax
  868. lea 0x4(%ebp), %edx
  869. movl (%ebp), %ecx
  870. subl $MCOUNT_INSN_SIZE, %eax
  871. call prepare_ftrace_return
  872. popl %edx
  873. popl %ecx
  874. popl %eax
  875. ret
  876. END(ftrace_graph_caller)
  877. .globl return_to_handler
  878. return_to_handler:
  879. pushl %eax
  880. pushl %edx
  881. movl %ebp, %eax
  882. call ftrace_return_to_handler
  883. movl %eax, %ecx
  884. popl %edx
  885. popl %eax
  886. jmp *%ecx
  887. #endif
  888. #ifdef CONFIG_TRACING
  889. ENTRY(trace_page_fault)
  890. ASM_CLAC
  891. pushl $trace_do_page_fault
  892. jmp error_code
  893. END(trace_page_fault)
  894. #endif
  895. ENTRY(page_fault)
  896. ASM_CLAC
  897. pushl $do_page_fault
  898. ALIGN
  899. error_code:
  900. /* the function address is in %gs's slot on the stack */
  901. pushl %fs
  902. pushl %es
  903. pushl %ds
  904. pushl %eax
  905. pushl %ebp
  906. pushl %edi
  907. pushl %esi
  908. pushl %edx
  909. pushl %ecx
  910. pushl %ebx
  911. cld
  912. movl $(__KERNEL_PERCPU), %ecx
  913. movl %ecx, %fs
  914. UNWIND_ESPFIX_STACK
  915. GS_TO_REG %ecx
  916. movl PT_GS(%esp), %edi # get the function address
  917. movl PT_ORIG_EAX(%esp), %edx # get the error code
  918. movl $-1, PT_ORIG_EAX(%esp) # no syscall to restart
  919. REG_TO_PTGS %ecx
  920. SET_KERNEL_GS %ecx
  921. movl $(__USER_DS), %ecx
  922. movl %ecx, %ds
  923. movl %ecx, %es
  924. TRACE_IRQS_OFF
  925. movl %esp, %eax # pt_regs pointer
  926. call *%edi
  927. jmp ret_from_exception
  928. END(page_fault)
  929. ENTRY(debug)
  930. /*
  931. * #DB can happen at the first instruction of
  932. * entry_SYSENTER_32 or in Xen's SYSENTER prologue. If this
  933. * happens, then we will be running on a very small stack. We
  934. * need to detect this condition and switch to the thread
  935. * stack before calling any C code at all.
  936. *
  937. * If you edit this code, keep in mind that NMIs can happen in here.
  938. */
  939. ASM_CLAC
  940. pushl $-1 # mark this as an int
  941. SAVE_ALL
  942. xorl %edx, %edx # error code 0
  943. movl %esp, %eax # pt_regs pointer
  944. /* Are we currently on the SYSENTER stack? */
  945. PER_CPU(cpu_tss + CPU_TSS_SYSENTER_stack + SIZEOF_SYSENTER_stack, %ecx)
  946. subl %eax, %ecx /* ecx = (end of SYSENTER_stack) - esp */
  947. cmpl $SIZEOF_SYSENTER_stack, %ecx
  948. jb .Ldebug_from_sysenter_stack
  949. TRACE_IRQS_OFF
  950. call do_debug
  951. jmp ret_from_exception
  952. .Ldebug_from_sysenter_stack:
  953. /* We're on the SYSENTER stack. Switch off. */
  954. movl %esp, %ebp
  955. movl PER_CPU_VAR(cpu_current_top_of_stack), %esp
  956. TRACE_IRQS_OFF
  957. call do_debug
  958. movl %ebp, %esp
  959. jmp ret_from_exception
  960. END(debug)
  961. /*
  962. * NMI is doubly nasty. It can happen on the first instruction of
  963. * entry_SYSENTER_32 (just like #DB), but it can also interrupt the beginning
  964. * of the #DB handler even if that #DB in turn hit before entry_SYSENTER_32
  965. * switched stacks. We handle both conditions by simply checking whether we
  966. * interrupted kernel code running on the SYSENTER stack.
  967. */
  968. ENTRY(nmi)
  969. ASM_CLAC
  970. #ifdef CONFIG_X86_ESPFIX32
  971. pushl %eax
  972. movl %ss, %eax
  973. cmpw $__ESPFIX_SS, %ax
  974. popl %eax
  975. je nmi_espfix_stack
  976. #endif
  977. pushl %eax # pt_regs->orig_ax
  978. SAVE_ALL
  979. xorl %edx, %edx # zero error code
  980. movl %esp, %eax # pt_regs pointer
  981. /* Are we currently on the SYSENTER stack? */
  982. PER_CPU(cpu_tss + CPU_TSS_SYSENTER_stack + SIZEOF_SYSENTER_stack, %ecx)
  983. subl %eax, %ecx /* ecx = (end of SYSENTER_stack) - esp */
  984. cmpl $SIZEOF_SYSENTER_stack, %ecx
  985. jb .Lnmi_from_sysenter_stack
  986. /* Not on SYSENTER stack. */
  987. call do_nmi
  988. jmp restore_all_notrace
  989. .Lnmi_from_sysenter_stack:
  990. /*
  991. * We're on the SYSENTER stack. Switch off. No one (not even debug)
  992. * is using the thread stack right now, so it's safe for us to use it.
  993. */
  994. movl %esp, %ebp
  995. movl PER_CPU_VAR(cpu_current_top_of_stack), %esp
  996. call do_nmi
  997. movl %ebp, %esp
  998. jmp restore_all_notrace
  999. #ifdef CONFIG_X86_ESPFIX32
  1000. nmi_espfix_stack:
  1001. /*
  1002. * create the pointer to lss back
  1003. */
  1004. pushl %ss
  1005. pushl %esp
  1006. addl $4, (%esp)
  1007. /* copy the iret frame of 12 bytes */
  1008. .rept 3
  1009. pushl 16(%esp)
  1010. .endr
  1011. pushl %eax
  1012. SAVE_ALL
  1013. FIXUP_ESPFIX_STACK # %eax == %esp
  1014. xorl %edx, %edx # zero error code
  1015. call do_nmi
  1016. RESTORE_REGS
  1017. lss 12+4(%esp), %esp # back to espfix stack
  1018. jmp irq_return
  1019. #endif
  1020. END(nmi)
  1021. ENTRY(int3)
  1022. ASM_CLAC
  1023. pushl $-1 # mark this as an int
  1024. SAVE_ALL
  1025. TRACE_IRQS_OFF
  1026. xorl %edx, %edx # zero error code
  1027. movl %esp, %eax # pt_regs pointer
  1028. call do_int3
  1029. jmp ret_from_exception
  1030. END(int3)
  1031. ENTRY(general_protection)
  1032. pushl $do_general_protection
  1033. jmp error_code
  1034. END(general_protection)
  1035. #ifdef CONFIG_KVM_GUEST
  1036. ENTRY(async_page_fault)
  1037. ASM_CLAC
  1038. pushl $do_async_page_fault
  1039. jmp error_code
  1040. END(async_page_fault)
  1041. #endif
  1042. ENTRY(rewind_stack_do_exit)
  1043. /* Prevent any naive code from trying to unwind to our caller. */
  1044. xorl %ebp, %ebp
  1045. movl PER_CPU_VAR(cpu_current_top_of_stack), %esi
  1046. leal -TOP_OF_KERNEL_STACK_PADDING-PTREGS_SIZE(%esi), %esp
  1047. call do_exit
  1048. 1: jmp 1b
  1049. END(rewind_stack_do_exit)