entry_32.S 29 KB

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