entry_32.S 27 KB

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