entry_32.S 26 KB

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