entry_32.S 26 KB

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