entry_32.S 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451
  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. #define PTI_SWITCH_MASK (1 << PAGE_SHIFT)
  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. /* Unconditionally switch to user cr3 */
  144. .macro SWITCH_TO_USER_CR3 scratch_reg:req
  145. ALTERNATIVE "jmp .Lend_\@", "", X86_FEATURE_PTI
  146. movl %cr3, \scratch_reg
  147. orl $PTI_SWITCH_MASK, \scratch_reg
  148. movl \scratch_reg, %cr3
  149. .Lend_\@:
  150. .endm
  151. /*
  152. * Switch to kernel cr3 if not already loaded and return current cr3 in
  153. * \scratch_reg
  154. */
  155. .macro SWITCH_TO_KERNEL_CR3 scratch_reg:req
  156. ALTERNATIVE "jmp .Lend_\@", "", X86_FEATURE_PTI
  157. movl %cr3, \scratch_reg
  158. /* Test if we are already on kernel CR3 */
  159. testl $PTI_SWITCH_MASK, \scratch_reg
  160. jz .Lend_\@
  161. andl $(~PTI_SWITCH_MASK), \scratch_reg
  162. movl \scratch_reg, %cr3
  163. /* Return original CR3 in \scratch_reg */
  164. orl $PTI_SWITCH_MASK, \scratch_reg
  165. .Lend_\@:
  166. .endm
  167. .macro SAVE_ALL pt_regs_ax=%eax switch_stacks=0
  168. cld
  169. PUSH_GS
  170. pushl %fs
  171. pushl %es
  172. pushl %ds
  173. pushl \pt_regs_ax
  174. pushl %ebp
  175. pushl %edi
  176. pushl %esi
  177. pushl %edx
  178. pushl %ecx
  179. pushl %ebx
  180. movl $(__USER_DS), %edx
  181. movl %edx, %ds
  182. movl %edx, %es
  183. movl $(__KERNEL_PERCPU), %edx
  184. movl %edx, %fs
  185. SET_KERNEL_GS %edx
  186. /* Switch to kernel stack if necessary */
  187. .if \switch_stacks > 0
  188. SWITCH_TO_KERNEL_STACK
  189. .endif
  190. .endm
  191. .macro SAVE_ALL_NMI cr3_reg:req
  192. SAVE_ALL
  193. /*
  194. * Now switch the CR3 when PTI is enabled.
  195. *
  196. * We can enter with either user or kernel cr3, the code will
  197. * store the old cr3 in \cr3_reg and switches to the kernel cr3
  198. * if necessary.
  199. */
  200. SWITCH_TO_KERNEL_CR3 scratch_reg=\cr3_reg
  201. .Lend_\@:
  202. .endm
  203. /*
  204. * This is a sneaky trick to help the unwinder find pt_regs on the stack. The
  205. * frame pointer is replaced with an encoded pointer to pt_regs. The encoding
  206. * is just clearing the MSB, which makes it an invalid stack address and is also
  207. * a signal to the unwinder that it's a pt_regs pointer in disguise.
  208. *
  209. * NOTE: This macro must be used *after* SAVE_ALL because it corrupts the
  210. * original rbp.
  211. */
  212. .macro ENCODE_FRAME_POINTER
  213. #ifdef CONFIG_FRAME_POINTER
  214. mov %esp, %ebp
  215. andl $0x7fffffff, %ebp
  216. #endif
  217. .endm
  218. .macro RESTORE_INT_REGS
  219. popl %ebx
  220. popl %ecx
  221. popl %edx
  222. popl %esi
  223. popl %edi
  224. popl %ebp
  225. popl %eax
  226. .endm
  227. .macro RESTORE_REGS pop=0
  228. RESTORE_INT_REGS
  229. 1: popl %ds
  230. 2: popl %es
  231. 3: popl %fs
  232. POP_GS \pop
  233. .pushsection .fixup, "ax"
  234. 4: movl $0, (%esp)
  235. jmp 1b
  236. 5: movl $0, (%esp)
  237. jmp 2b
  238. 6: movl $0, (%esp)
  239. jmp 3b
  240. .popsection
  241. _ASM_EXTABLE(1b, 4b)
  242. _ASM_EXTABLE(2b, 5b)
  243. _ASM_EXTABLE(3b, 6b)
  244. POP_GS_EX
  245. .endm
  246. .macro RESTORE_ALL_NMI cr3_reg:req pop=0
  247. /*
  248. * Now switch the CR3 when PTI is enabled.
  249. *
  250. * We enter with kernel cr3 and switch the cr3 to the value
  251. * stored on \cr3_reg, which is either a user or a kernel cr3.
  252. */
  253. ALTERNATIVE "jmp .Lswitched_\@", "", X86_FEATURE_PTI
  254. testl $PTI_SWITCH_MASK, \cr3_reg
  255. jz .Lswitched_\@
  256. /* User cr3 in \cr3_reg - write it to hardware cr3 */
  257. movl \cr3_reg, %cr3
  258. .Lswitched_\@:
  259. RESTORE_REGS pop=\pop
  260. .endm
  261. .macro CHECK_AND_APPLY_ESPFIX
  262. #ifdef CONFIG_X86_ESPFIX32
  263. #define GDT_ESPFIX_SS PER_CPU_VAR(gdt_page) + (GDT_ENTRY_ESPFIX_SS * 8)
  264. ALTERNATIVE "jmp .Lend_\@", "", X86_BUG_ESPFIX
  265. movl PT_EFLAGS(%esp), %eax # mix EFLAGS, SS and CS
  266. /*
  267. * Warning: PT_OLDSS(%esp) contains the wrong/random values if we
  268. * are returning to the kernel.
  269. * See comments in process.c:copy_thread() for details.
  270. */
  271. movb PT_OLDSS(%esp), %ah
  272. movb PT_CS(%esp), %al
  273. andl $(X86_EFLAGS_VM | (SEGMENT_TI_MASK << 8) | SEGMENT_RPL_MASK), %eax
  274. cmpl $((SEGMENT_LDT << 8) | USER_RPL), %eax
  275. jne .Lend_\@ # returning to user-space with LDT SS
  276. /*
  277. * Setup and switch to ESPFIX stack
  278. *
  279. * We're returning to userspace with a 16 bit stack. The CPU will not
  280. * restore the high word of ESP for us on executing iret... This is an
  281. * "official" bug of all the x86-compatible CPUs, which we can work
  282. * around to make dosemu and wine happy. We do this by preloading the
  283. * high word of ESP with the high word of the userspace ESP while
  284. * compensating for the offset by changing to the ESPFIX segment with
  285. * a base address that matches for the difference.
  286. */
  287. mov %esp, %edx /* load kernel esp */
  288. mov PT_OLDESP(%esp), %eax /* load userspace esp */
  289. mov %dx, %ax /* eax: new kernel esp */
  290. sub %eax, %edx /* offset (low word is 0) */
  291. shr $16, %edx
  292. mov %dl, GDT_ESPFIX_SS + 4 /* bits 16..23 */
  293. mov %dh, GDT_ESPFIX_SS + 7 /* bits 24..31 */
  294. pushl $__ESPFIX_SS
  295. pushl %eax /* new kernel esp */
  296. /*
  297. * Disable interrupts, but do not irqtrace this section: we
  298. * will soon execute iret and the tracer was already set to
  299. * the irqstate after the IRET:
  300. */
  301. DISABLE_INTERRUPTS(CLBR_ANY)
  302. lss (%esp), %esp /* switch to espfix segment */
  303. .Lend_\@:
  304. #endif /* CONFIG_X86_ESPFIX32 */
  305. .endm
  306. /*
  307. * Called with pt_regs fully populated and kernel segments loaded,
  308. * so we can access PER_CPU and use the integer registers.
  309. *
  310. * We need to be very careful here with the %esp switch, because an NMI
  311. * can happen everywhere. If the NMI handler finds itself on the
  312. * entry-stack, it will overwrite the task-stack and everything we
  313. * copied there. So allocate the stack-frame on the task-stack and
  314. * switch to it before we do any copying.
  315. */
  316. #define CS_FROM_ENTRY_STACK (1 << 31)
  317. #define CS_FROM_USER_CR3 (1 << 30)
  318. .macro SWITCH_TO_KERNEL_STACK
  319. ALTERNATIVE "", "jmp .Lend_\@", X86_FEATURE_XENPV
  320. SWITCH_TO_KERNEL_CR3 scratch_reg=%eax
  321. /*
  322. * %eax now contains the entry cr3 and we carry it forward in
  323. * that register for the time this macro runs
  324. */
  325. /* Are we on the entry stack? Bail out if not! */
  326. movl PER_CPU_VAR(cpu_entry_area), %ecx
  327. addl $CPU_ENTRY_AREA_entry_stack + SIZEOF_entry_stack, %ecx
  328. subl %esp, %ecx /* ecx = (end of entry_stack) - esp */
  329. cmpl $SIZEOF_entry_stack, %ecx
  330. jae .Lend_\@
  331. /* Load stack pointer into %esi and %edi */
  332. movl %esp, %esi
  333. movl %esi, %edi
  334. /* Move %edi to the top of the entry stack */
  335. andl $(MASK_entry_stack), %edi
  336. addl $(SIZEOF_entry_stack), %edi
  337. /* Load top of task-stack into %edi */
  338. movl TSS_entry2task_stack(%edi), %edi
  339. /*
  340. * Clear unused upper bits of the dword containing the word-sized CS
  341. * slot in pt_regs in case hardware didn't clear it for us.
  342. */
  343. andl $(0x0000ffff), PT_CS(%esp)
  344. /* Special case - entry from kernel mode via entry stack */
  345. testl $SEGMENT_RPL_MASK, PT_CS(%esp)
  346. jz .Lentry_from_kernel_\@
  347. /* Bytes to copy */
  348. movl $PTREGS_SIZE, %ecx
  349. #ifdef CONFIG_VM86
  350. testl $X86_EFLAGS_VM, PT_EFLAGS(%esi)
  351. jz .Lcopy_pt_regs_\@
  352. /*
  353. * Stack-frame contains 4 additional segment registers when
  354. * coming from VM86 mode
  355. */
  356. addl $(4 * 4), %ecx
  357. #endif
  358. .Lcopy_pt_regs_\@:
  359. /* Allocate frame on task-stack */
  360. subl %ecx, %edi
  361. /* Switch to task-stack */
  362. movl %edi, %esp
  363. /*
  364. * We are now on the task-stack and can safely copy over the
  365. * stack-frame
  366. */
  367. shrl $2, %ecx
  368. cld
  369. rep movsl
  370. jmp .Lend_\@
  371. .Lentry_from_kernel_\@:
  372. /*
  373. * This handles the case when we enter the kernel from
  374. * kernel-mode and %esp points to the entry-stack. When this
  375. * happens we need to switch to the task-stack to run C code,
  376. * but switch back to the entry-stack again when we approach
  377. * iret and return to the interrupted code-path. This usually
  378. * happens when we hit an exception while restoring user-space
  379. * segment registers on the way back to user-space or when the
  380. * sysenter handler runs with eflags.tf set.
  381. *
  382. * When we switch to the task-stack here, we can't trust the
  383. * contents of the entry-stack anymore, as the exception handler
  384. * might be scheduled out or moved to another CPU. Therefore we
  385. * copy the complete entry-stack to the task-stack and set a
  386. * marker in the iret-frame (bit 31 of the CS dword) to detect
  387. * what we've done on the iret path.
  388. *
  389. * On the iret path we copy everything back and switch to the
  390. * entry-stack, so that the interrupted kernel code-path
  391. * continues on the same stack it was interrupted with.
  392. *
  393. * Be aware that an NMI can happen anytime in this code.
  394. *
  395. * %esi: Entry-Stack pointer (same as %esp)
  396. * %edi: Top of the task stack
  397. * %eax: CR3 on kernel entry
  398. */
  399. /* Calculate number of bytes on the entry stack in %ecx */
  400. movl %esi, %ecx
  401. /* %ecx to the top of entry-stack */
  402. andl $(MASK_entry_stack), %ecx
  403. addl $(SIZEOF_entry_stack), %ecx
  404. /* Number of bytes on the entry stack to %ecx */
  405. sub %esi, %ecx
  406. /* Mark stackframe as coming from entry stack */
  407. orl $CS_FROM_ENTRY_STACK, PT_CS(%esp)
  408. /*
  409. * Test the cr3 used to enter the kernel and add a marker
  410. * so that we can switch back to it before iret.
  411. */
  412. testl $PTI_SWITCH_MASK, %eax
  413. jz .Lcopy_pt_regs_\@
  414. orl $CS_FROM_USER_CR3, PT_CS(%esp)
  415. /*
  416. * %esi and %edi are unchanged, %ecx contains the number of
  417. * bytes to copy. The code at .Lcopy_pt_regs_\@ will allocate
  418. * the stack-frame on task-stack and copy everything over
  419. */
  420. jmp .Lcopy_pt_regs_\@
  421. .Lend_\@:
  422. .endm
  423. /*
  424. * Switch back from the kernel stack to the entry stack.
  425. *
  426. * The %esp register must point to pt_regs on the task stack. It will
  427. * first calculate the size of the stack-frame to copy, depending on
  428. * whether we return to VM86 mode or not. With that it uses 'rep movsl'
  429. * to copy the contents of the stack over to the entry stack.
  430. *
  431. * We must be very careful here, as we can't trust the contents of the
  432. * task-stack once we switched to the entry-stack. When an NMI happens
  433. * while on the entry-stack, the NMI handler will switch back to the top
  434. * of the task stack, overwriting our stack-frame we are about to copy.
  435. * Therefore we switch the stack only after everything is copied over.
  436. */
  437. .macro SWITCH_TO_ENTRY_STACK
  438. ALTERNATIVE "", "jmp .Lend_\@", X86_FEATURE_XENPV
  439. /* Bytes to copy */
  440. movl $PTREGS_SIZE, %ecx
  441. #ifdef CONFIG_VM86
  442. testl $(X86_EFLAGS_VM), PT_EFLAGS(%esp)
  443. jz .Lcopy_pt_regs_\@
  444. /* Additional 4 registers to copy when returning to VM86 mode */
  445. addl $(4 * 4), %ecx
  446. .Lcopy_pt_regs_\@:
  447. #endif
  448. /* Initialize source and destination for movsl */
  449. movl PER_CPU_VAR(cpu_tss_rw + TSS_sp0), %edi
  450. subl %ecx, %edi
  451. movl %esp, %esi
  452. /* Save future stack pointer in %ebx */
  453. movl %edi, %ebx
  454. /* Copy over the stack-frame */
  455. shrl $2, %ecx
  456. cld
  457. rep movsl
  458. /*
  459. * Switch to entry-stack - needs to happen after everything is
  460. * copied because the NMI handler will overwrite the task-stack
  461. * when on entry-stack
  462. */
  463. movl %ebx, %esp
  464. .Lend_\@:
  465. .endm
  466. /*
  467. * This macro handles the case when we return to kernel-mode on the iret
  468. * path and have to switch back to the entry stack and/or user-cr3
  469. *
  470. * See the comments below the .Lentry_from_kernel_\@ label in the
  471. * SWITCH_TO_KERNEL_STACK macro for more details.
  472. */
  473. .macro PARANOID_EXIT_TO_KERNEL_MODE
  474. /*
  475. * Test if we entered the kernel with the entry-stack. Most
  476. * likely we did not, because this code only runs on the
  477. * return-to-kernel path.
  478. */
  479. testl $CS_FROM_ENTRY_STACK, PT_CS(%esp)
  480. jz .Lend_\@
  481. /* Unlikely slow-path */
  482. /* Clear marker from stack-frame */
  483. andl $(~CS_FROM_ENTRY_STACK), PT_CS(%esp)
  484. /* Copy the remaining task-stack contents to entry-stack */
  485. movl %esp, %esi
  486. movl PER_CPU_VAR(cpu_tss_rw + TSS_sp0), %edi
  487. /* Bytes on the task-stack to ecx */
  488. movl PER_CPU_VAR(cpu_tss_rw + TSS_sp1), %ecx
  489. subl %esi, %ecx
  490. /* Allocate stack-frame on entry-stack */
  491. subl %ecx, %edi
  492. /*
  493. * Save future stack-pointer, we must not switch until the
  494. * copy is done, otherwise the NMI handler could destroy the
  495. * contents of the task-stack we are about to copy.
  496. */
  497. movl %edi, %ebx
  498. /* Do the copy */
  499. shrl $2, %ecx
  500. cld
  501. rep movsl
  502. /* Safe to switch to entry-stack now */
  503. movl %ebx, %esp
  504. /*
  505. * We came from entry-stack and need to check if we also need to
  506. * switch back to user cr3.
  507. */
  508. testl $CS_FROM_USER_CR3, PT_CS(%esp)
  509. jz .Lend_\@
  510. /* Clear marker from stack-frame */
  511. andl $(~CS_FROM_USER_CR3), PT_CS(%esp)
  512. SWITCH_TO_USER_CR3 scratch_reg=%eax
  513. .Lend_\@:
  514. .endm
  515. /*
  516. * %eax: prev task
  517. * %edx: next task
  518. */
  519. ENTRY(__switch_to_asm)
  520. /*
  521. * Save callee-saved registers
  522. * This must match the order in struct inactive_task_frame
  523. */
  524. pushl %ebp
  525. pushl %ebx
  526. pushl %edi
  527. pushl %esi
  528. /* switch stack */
  529. movl %esp, TASK_threadsp(%eax)
  530. movl TASK_threadsp(%edx), %esp
  531. #ifdef CONFIG_STACKPROTECTOR
  532. movl TASK_stack_canary(%edx), %ebx
  533. movl %ebx, PER_CPU_VAR(stack_canary)+stack_canary_offset
  534. #endif
  535. #ifdef CONFIG_RETPOLINE
  536. /*
  537. * When switching from a shallower to a deeper call stack
  538. * the RSB may either underflow or use entries populated
  539. * with userspace addresses. On CPUs where those concerns
  540. * exist, overwrite the RSB with entries which capture
  541. * speculative execution to prevent attack.
  542. */
  543. FILL_RETURN_BUFFER %ebx, RSB_CLEAR_LOOPS, X86_FEATURE_RSB_CTXSW
  544. #endif
  545. /* restore callee-saved registers */
  546. popl %esi
  547. popl %edi
  548. popl %ebx
  549. popl %ebp
  550. jmp __switch_to
  551. END(__switch_to_asm)
  552. /*
  553. * The unwinder expects the last frame on the stack to always be at the same
  554. * offset from the end of the page, which allows it to validate the stack.
  555. * Calling schedule_tail() directly would break that convention because its an
  556. * asmlinkage function so its argument has to be pushed on the stack. This
  557. * wrapper creates a proper "end of stack" frame header before the call.
  558. */
  559. ENTRY(schedule_tail_wrapper)
  560. FRAME_BEGIN
  561. pushl %eax
  562. call schedule_tail
  563. popl %eax
  564. FRAME_END
  565. ret
  566. ENDPROC(schedule_tail_wrapper)
  567. /*
  568. * A newly forked process directly context switches into this address.
  569. *
  570. * eax: prev task we switched from
  571. * ebx: kernel thread func (NULL for user thread)
  572. * edi: kernel thread arg
  573. */
  574. ENTRY(ret_from_fork)
  575. call schedule_tail_wrapper
  576. testl %ebx, %ebx
  577. jnz 1f /* kernel threads are uncommon */
  578. 2:
  579. /* When we fork, we trace the syscall return in the child, too. */
  580. movl %esp, %eax
  581. call syscall_return_slowpath
  582. jmp restore_all
  583. /* kernel thread */
  584. 1: movl %edi, %eax
  585. CALL_NOSPEC %ebx
  586. /*
  587. * A kernel thread is allowed to return here after successfully
  588. * calling do_execve(). Exit to userspace to complete the execve()
  589. * syscall.
  590. */
  591. movl $0, PT_EAX(%esp)
  592. jmp 2b
  593. END(ret_from_fork)
  594. /*
  595. * Return to user mode is not as complex as all this looks,
  596. * but we want the default path for a system call return to
  597. * go as quickly as possible which is why some of this is
  598. * less clear than it otherwise should be.
  599. */
  600. # userspace resumption stub bypassing syscall exit tracing
  601. ALIGN
  602. ret_from_exception:
  603. preempt_stop(CLBR_ANY)
  604. ret_from_intr:
  605. #ifdef CONFIG_VM86
  606. movl PT_EFLAGS(%esp), %eax # mix EFLAGS and CS
  607. movb PT_CS(%esp), %al
  608. andl $(X86_EFLAGS_VM | SEGMENT_RPL_MASK), %eax
  609. #else
  610. /*
  611. * We can be coming here from child spawned by kernel_thread().
  612. */
  613. movl PT_CS(%esp), %eax
  614. andl $SEGMENT_RPL_MASK, %eax
  615. #endif
  616. cmpl $USER_RPL, %eax
  617. jb resume_kernel # not returning to v8086 or userspace
  618. ENTRY(resume_userspace)
  619. DISABLE_INTERRUPTS(CLBR_ANY)
  620. TRACE_IRQS_OFF
  621. movl %esp, %eax
  622. call prepare_exit_to_usermode
  623. jmp restore_all
  624. END(ret_from_exception)
  625. #ifdef CONFIG_PREEMPT
  626. ENTRY(resume_kernel)
  627. DISABLE_INTERRUPTS(CLBR_ANY)
  628. .Lneed_resched:
  629. cmpl $0, PER_CPU_VAR(__preempt_count)
  630. jnz restore_all_kernel
  631. testl $X86_EFLAGS_IF, PT_EFLAGS(%esp) # interrupts off (exception path) ?
  632. jz restore_all_kernel
  633. call preempt_schedule_irq
  634. jmp .Lneed_resched
  635. END(resume_kernel)
  636. #endif
  637. GLOBAL(__begin_SYSENTER_singlestep_region)
  638. /*
  639. * All code from here through __end_SYSENTER_singlestep_region is subject
  640. * to being single-stepped if a user program sets TF and executes SYSENTER.
  641. * There is absolutely nothing that we can do to prevent this from happening
  642. * (thanks Intel!). To keep our handling of this situation as simple as
  643. * possible, we handle TF just like AC and NT, except that our #DB handler
  644. * will ignore all of the single-step traps generated in this range.
  645. */
  646. #ifdef CONFIG_XEN
  647. /*
  648. * Xen doesn't set %esp to be precisely what the normal SYSENTER
  649. * entry point expects, so fix it up before using the normal path.
  650. */
  651. ENTRY(xen_sysenter_target)
  652. addl $5*4, %esp /* remove xen-provided frame */
  653. jmp .Lsysenter_past_esp
  654. #endif
  655. /*
  656. * 32-bit SYSENTER entry.
  657. *
  658. * 32-bit system calls through the vDSO's __kernel_vsyscall enter here
  659. * if X86_FEATURE_SEP is available. This is the preferred system call
  660. * entry on 32-bit systems.
  661. *
  662. * The SYSENTER instruction, in principle, should *only* occur in the
  663. * vDSO. In practice, a small number of Android devices were shipped
  664. * with a copy of Bionic that inlined a SYSENTER instruction. This
  665. * never happened in any of Google's Bionic versions -- it only happened
  666. * in a narrow range of Intel-provided versions.
  667. *
  668. * SYSENTER loads SS, ESP, CS, and EIP from previously programmed MSRs.
  669. * IF and VM in RFLAGS are cleared (IOW: interrupts are off).
  670. * SYSENTER does not save anything on the stack,
  671. * and does not save old EIP (!!!), ESP, or EFLAGS.
  672. *
  673. * To avoid losing track of EFLAGS.VM (and thus potentially corrupting
  674. * user and/or vm86 state), we explicitly disable the SYSENTER
  675. * instruction in vm86 mode by reprogramming the MSRs.
  676. *
  677. * Arguments:
  678. * eax system call number
  679. * ebx arg1
  680. * ecx arg2
  681. * edx arg3
  682. * esi arg4
  683. * edi arg5
  684. * ebp user stack
  685. * 0(%ebp) arg6
  686. */
  687. ENTRY(entry_SYSENTER_32)
  688. /*
  689. * On entry-stack with all userspace-regs live - save and
  690. * restore eflags and %eax to use it as scratch-reg for the cr3
  691. * switch.
  692. */
  693. pushfl
  694. pushl %eax
  695. SWITCH_TO_KERNEL_CR3 scratch_reg=%eax
  696. popl %eax
  697. popfl
  698. /* Stack empty again, switch to task stack */
  699. movl TSS_entry2task_stack(%esp), %esp
  700. .Lsysenter_past_esp:
  701. pushl $__USER_DS /* pt_regs->ss */
  702. pushl %ebp /* pt_regs->sp (stashed in bp) */
  703. pushfl /* pt_regs->flags (except IF = 0) */
  704. orl $X86_EFLAGS_IF, (%esp) /* Fix IF */
  705. pushl $__USER_CS /* pt_regs->cs */
  706. pushl $0 /* pt_regs->ip = 0 (placeholder) */
  707. pushl %eax /* pt_regs->orig_ax */
  708. SAVE_ALL pt_regs_ax=$-ENOSYS /* save rest, stack already switched */
  709. /*
  710. * SYSENTER doesn't filter flags, so we need to clear NT, AC
  711. * and TF ourselves. To save a few cycles, we can check whether
  712. * either was set instead of doing an unconditional popfq.
  713. * This needs to happen before enabling interrupts so that
  714. * we don't get preempted with NT set.
  715. *
  716. * If TF is set, we will single-step all the way to here -- do_debug
  717. * will ignore all the traps. (Yes, this is slow, but so is
  718. * single-stepping in general. This allows us to avoid having
  719. * a more complicated code to handle the case where a user program
  720. * forces us to single-step through the SYSENTER entry code.)
  721. *
  722. * NB.: .Lsysenter_fix_flags is a label with the code under it moved
  723. * out-of-line as an optimization: NT is unlikely to be set in the
  724. * majority of the cases and instead of polluting the I$ unnecessarily,
  725. * we're keeping that code behind a branch which will predict as
  726. * not-taken and therefore its instructions won't be fetched.
  727. */
  728. testl $X86_EFLAGS_NT|X86_EFLAGS_AC|X86_EFLAGS_TF, PT_EFLAGS(%esp)
  729. jnz .Lsysenter_fix_flags
  730. .Lsysenter_flags_fixed:
  731. /*
  732. * User mode is traced as though IRQs are on, and SYSENTER
  733. * turned them off.
  734. */
  735. TRACE_IRQS_OFF
  736. movl %esp, %eax
  737. call do_fast_syscall_32
  738. /* XEN PV guests always use IRET path */
  739. ALTERNATIVE "testl %eax, %eax; jz .Lsyscall_32_done", \
  740. "jmp .Lsyscall_32_done", X86_FEATURE_XENPV
  741. /* Opportunistic SYSEXIT */
  742. TRACE_IRQS_ON /* User mode traces as IRQs on. */
  743. /*
  744. * Setup entry stack - we keep the pointer in %eax and do the
  745. * switch after almost all user-state is restored.
  746. */
  747. /* Load entry stack pointer and allocate frame for eflags/eax */
  748. movl PER_CPU_VAR(cpu_tss_rw + TSS_sp0), %eax
  749. subl $(2*4), %eax
  750. /* Copy eflags and eax to entry stack */
  751. movl PT_EFLAGS(%esp), %edi
  752. movl PT_EAX(%esp), %esi
  753. movl %edi, (%eax)
  754. movl %esi, 4(%eax)
  755. /* Restore user registers and segments */
  756. movl PT_EIP(%esp), %edx /* pt_regs->ip */
  757. movl PT_OLDESP(%esp), %ecx /* pt_regs->sp */
  758. 1: mov PT_FS(%esp), %fs
  759. PTGS_TO_GS
  760. popl %ebx /* pt_regs->bx */
  761. addl $2*4, %esp /* skip pt_regs->cx and pt_regs->dx */
  762. popl %esi /* pt_regs->si */
  763. popl %edi /* pt_regs->di */
  764. popl %ebp /* pt_regs->bp */
  765. /* Switch to entry stack */
  766. movl %eax, %esp
  767. /* Now ready to switch the cr3 */
  768. SWITCH_TO_USER_CR3 scratch_reg=%eax
  769. /*
  770. * Restore all flags except IF. (We restore IF separately because
  771. * STI gives a one-instruction window in which we won't be interrupted,
  772. * whereas POPF does not.)
  773. */
  774. btrl $X86_EFLAGS_IF_BIT, (%esp)
  775. popfl
  776. popl %eax
  777. /*
  778. * Return back to the vDSO, which will pop ecx and edx.
  779. * Don't bother with DS and ES (they already contain __USER_DS).
  780. */
  781. sti
  782. sysexit
  783. .pushsection .fixup, "ax"
  784. 2: movl $0, PT_FS(%esp)
  785. jmp 1b
  786. .popsection
  787. _ASM_EXTABLE(1b, 2b)
  788. PTGS_TO_GS_EX
  789. .Lsysenter_fix_flags:
  790. pushl $X86_EFLAGS_FIXED
  791. popfl
  792. jmp .Lsysenter_flags_fixed
  793. GLOBAL(__end_SYSENTER_singlestep_region)
  794. ENDPROC(entry_SYSENTER_32)
  795. /*
  796. * 32-bit legacy system call entry.
  797. *
  798. * 32-bit x86 Linux system calls traditionally used the INT $0x80
  799. * instruction. INT $0x80 lands here.
  800. *
  801. * This entry point can be used by any 32-bit perform system calls.
  802. * Instances of INT $0x80 can be found inline in various programs and
  803. * libraries. It is also used by the vDSO's __kernel_vsyscall
  804. * fallback for hardware that doesn't support a faster entry method.
  805. * Restarted 32-bit system calls also fall back to INT $0x80
  806. * regardless of what instruction was originally used to do the system
  807. * call. (64-bit programs can use INT $0x80 as well, but they can
  808. * only run on 64-bit kernels and therefore land in
  809. * entry_INT80_compat.)
  810. *
  811. * This is considered a slow path. It is not used by most libc
  812. * implementations on modern hardware except during process startup.
  813. *
  814. * Arguments:
  815. * eax system call number
  816. * ebx arg1
  817. * ecx arg2
  818. * edx arg3
  819. * esi arg4
  820. * edi arg5
  821. * ebp arg6
  822. */
  823. ENTRY(entry_INT80_32)
  824. ASM_CLAC
  825. pushl %eax /* pt_regs->orig_ax */
  826. SAVE_ALL pt_regs_ax=$-ENOSYS switch_stacks=1 /* save rest */
  827. /*
  828. * User mode is traced as though IRQs are on, and the interrupt gate
  829. * turned them off.
  830. */
  831. TRACE_IRQS_OFF
  832. movl %esp, %eax
  833. call do_int80_syscall_32
  834. .Lsyscall_32_done:
  835. restore_all:
  836. TRACE_IRQS_IRET
  837. SWITCH_TO_ENTRY_STACK
  838. .Lrestore_all_notrace:
  839. CHECK_AND_APPLY_ESPFIX
  840. .Lrestore_nocheck:
  841. /* Switch back to user CR3 */
  842. SWITCH_TO_USER_CR3 scratch_reg=%eax
  843. /* Restore user state */
  844. RESTORE_REGS pop=4 # skip orig_eax/error_code
  845. .Lirq_return:
  846. /*
  847. * ARCH_HAS_MEMBARRIER_SYNC_CORE rely on IRET core serialization
  848. * when returning from IPI handler and when returning from
  849. * scheduler to user-space.
  850. */
  851. INTERRUPT_RETURN
  852. restore_all_kernel:
  853. TRACE_IRQS_IRET
  854. PARANOID_EXIT_TO_KERNEL_MODE
  855. RESTORE_REGS 4
  856. jmp .Lirq_return
  857. .section .fixup, "ax"
  858. ENTRY(iret_exc )
  859. pushl $0 # no error code
  860. pushl $do_iret_error
  861. jmp common_exception
  862. .previous
  863. _ASM_EXTABLE(.Lirq_return, iret_exc)
  864. ENDPROC(entry_INT80_32)
  865. .macro FIXUP_ESPFIX_STACK
  866. /*
  867. * Switch back for ESPFIX stack to the normal zerobased stack
  868. *
  869. * We can't call C functions using the ESPFIX stack. This code reads
  870. * the high word of the segment base from the GDT and swiches to the
  871. * normal stack and adjusts ESP with the matching offset.
  872. */
  873. #ifdef CONFIG_X86_ESPFIX32
  874. /* fixup the stack */
  875. mov GDT_ESPFIX_SS + 4, %al /* bits 16..23 */
  876. mov GDT_ESPFIX_SS + 7, %ah /* bits 24..31 */
  877. shl $16, %eax
  878. addl %esp, %eax /* the adjusted stack pointer */
  879. pushl $__KERNEL_DS
  880. pushl %eax
  881. lss (%esp), %esp /* switch to the normal stack segment */
  882. #endif
  883. .endm
  884. .macro UNWIND_ESPFIX_STACK
  885. #ifdef CONFIG_X86_ESPFIX32
  886. movl %ss, %eax
  887. /* see if on espfix stack */
  888. cmpw $__ESPFIX_SS, %ax
  889. jne 27f
  890. movl $__KERNEL_DS, %eax
  891. movl %eax, %ds
  892. movl %eax, %es
  893. /* switch to normal stack */
  894. FIXUP_ESPFIX_STACK
  895. 27:
  896. #endif
  897. .endm
  898. /*
  899. * Build the entry stubs with some assembler magic.
  900. * We pack 1 stub into every 8-byte block.
  901. */
  902. .align 8
  903. ENTRY(irq_entries_start)
  904. vector=FIRST_EXTERNAL_VECTOR
  905. .rept (FIRST_SYSTEM_VECTOR - FIRST_EXTERNAL_VECTOR)
  906. pushl $(~vector+0x80) /* Note: always in signed byte range */
  907. vector=vector+1
  908. jmp common_interrupt
  909. .align 8
  910. .endr
  911. END(irq_entries_start)
  912. /*
  913. * the CPU automatically disables interrupts when executing an IRQ vector,
  914. * so IRQ-flags tracing has to follow that:
  915. */
  916. .p2align CONFIG_X86_L1_CACHE_SHIFT
  917. common_interrupt:
  918. ASM_CLAC
  919. addl $-0x80, (%esp) /* Adjust vector into the [-256, -1] range */
  920. SAVE_ALL switch_stacks=1
  921. ENCODE_FRAME_POINTER
  922. TRACE_IRQS_OFF
  923. movl %esp, %eax
  924. call do_IRQ
  925. jmp ret_from_intr
  926. ENDPROC(common_interrupt)
  927. #define BUILD_INTERRUPT3(name, nr, fn) \
  928. ENTRY(name) \
  929. ASM_CLAC; \
  930. pushl $~(nr); \
  931. SAVE_ALL switch_stacks=1; \
  932. ENCODE_FRAME_POINTER; \
  933. TRACE_IRQS_OFF \
  934. movl %esp, %eax; \
  935. call fn; \
  936. jmp ret_from_intr; \
  937. ENDPROC(name)
  938. #define BUILD_INTERRUPT(name, nr) \
  939. BUILD_INTERRUPT3(name, nr, smp_##name); \
  940. /* The include is where all of the SMP etc. interrupts come from */
  941. #include <asm/entry_arch.h>
  942. ENTRY(coprocessor_error)
  943. ASM_CLAC
  944. pushl $0
  945. pushl $do_coprocessor_error
  946. jmp common_exception
  947. END(coprocessor_error)
  948. ENTRY(simd_coprocessor_error)
  949. ASM_CLAC
  950. pushl $0
  951. #ifdef CONFIG_X86_INVD_BUG
  952. /* AMD 486 bug: invd from userspace calls exception 19 instead of #GP */
  953. ALTERNATIVE "pushl $do_general_protection", \
  954. "pushl $do_simd_coprocessor_error", \
  955. X86_FEATURE_XMM
  956. #else
  957. pushl $do_simd_coprocessor_error
  958. #endif
  959. jmp common_exception
  960. END(simd_coprocessor_error)
  961. ENTRY(device_not_available)
  962. ASM_CLAC
  963. pushl $-1 # mark this as an int
  964. pushl $do_device_not_available
  965. jmp common_exception
  966. END(device_not_available)
  967. #ifdef CONFIG_PARAVIRT
  968. ENTRY(native_iret)
  969. iret
  970. _ASM_EXTABLE(native_iret, iret_exc)
  971. END(native_iret)
  972. #endif
  973. ENTRY(overflow)
  974. ASM_CLAC
  975. pushl $0
  976. pushl $do_overflow
  977. jmp common_exception
  978. END(overflow)
  979. ENTRY(bounds)
  980. ASM_CLAC
  981. pushl $0
  982. pushl $do_bounds
  983. jmp common_exception
  984. END(bounds)
  985. ENTRY(invalid_op)
  986. ASM_CLAC
  987. pushl $0
  988. pushl $do_invalid_op
  989. jmp common_exception
  990. END(invalid_op)
  991. ENTRY(coprocessor_segment_overrun)
  992. ASM_CLAC
  993. pushl $0
  994. pushl $do_coprocessor_segment_overrun
  995. jmp common_exception
  996. END(coprocessor_segment_overrun)
  997. ENTRY(invalid_TSS)
  998. ASM_CLAC
  999. pushl $do_invalid_TSS
  1000. jmp common_exception
  1001. END(invalid_TSS)
  1002. ENTRY(segment_not_present)
  1003. ASM_CLAC
  1004. pushl $do_segment_not_present
  1005. jmp common_exception
  1006. END(segment_not_present)
  1007. ENTRY(stack_segment)
  1008. ASM_CLAC
  1009. pushl $do_stack_segment
  1010. jmp common_exception
  1011. END(stack_segment)
  1012. ENTRY(alignment_check)
  1013. ASM_CLAC
  1014. pushl $do_alignment_check
  1015. jmp common_exception
  1016. END(alignment_check)
  1017. ENTRY(divide_error)
  1018. ASM_CLAC
  1019. pushl $0 # no error code
  1020. pushl $do_divide_error
  1021. jmp common_exception
  1022. END(divide_error)
  1023. #ifdef CONFIG_X86_MCE
  1024. ENTRY(machine_check)
  1025. ASM_CLAC
  1026. pushl $0
  1027. pushl machine_check_vector
  1028. jmp common_exception
  1029. END(machine_check)
  1030. #endif
  1031. ENTRY(spurious_interrupt_bug)
  1032. ASM_CLAC
  1033. pushl $0
  1034. pushl $do_spurious_interrupt_bug
  1035. jmp common_exception
  1036. END(spurious_interrupt_bug)
  1037. #ifdef CONFIG_XEN
  1038. ENTRY(xen_hypervisor_callback)
  1039. pushl $-1 /* orig_ax = -1 => not a system call */
  1040. SAVE_ALL
  1041. ENCODE_FRAME_POINTER
  1042. TRACE_IRQS_OFF
  1043. /*
  1044. * Check to see if we got the event in the critical
  1045. * region in xen_iret_direct, after we've reenabled
  1046. * events and checked for pending events. This simulates
  1047. * iret instruction's behaviour where it delivers a
  1048. * pending interrupt when enabling interrupts:
  1049. */
  1050. movl PT_EIP(%esp), %eax
  1051. cmpl $xen_iret_start_crit, %eax
  1052. jb 1f
  1053. cmpl $xen_iret_end_crit, %eax
  1054. jae 1f
  1055. jmp xen_iret_crit_fixup
  1056. ENTRY(xen_do_upcall)
  1057. 1: mov %esp, %eax
  1058. call xen_evtchn_do_upcall
  1059. #ifndef CONFIG_PREEMPT
  1060. call xen_maybe_preempt_hcall
  1061. #endif
  1062. jmp ret_from_intr
  1063. ENDPROC(xen_hypervisor_callback)
  1064. /*
  1065. * Hypervisor uses this for application faults while it executes.
  1066. * We get here for two reasons:
  1067. * 1. Fault while reloading DS, ES, FS or GS
  1068. * 2. Fault while executing IRET
  1069. * Category 1 we fix up by reattempting the load, and zeroing the segment
  1070. * register if the load fails.
  1071. * Category 2 we fix up by jumping to do_iret_error. We cannot use the
  1072. * normal Linux return path in this case because if we use the IRET hypercall
  1073. * to pop the stack frame we end up in an infinite loop of failsafe callbacks.
  1074. * We distinguish between categories by maintaining a status value in EAX.
  1075. */
  1076. ENTRY(xen_failsafe_callback)
  1077. pushl %eax
  1078. movl $1, %eax
  1079. 1: mov 4(%esp), %ds
  1080. 2: mov 8(%esp), %es
  1081. 3: mov 12(%esp), %fs
  1082. 4: mov 16(%esp), %gs
  1083. /* EAX == 0 => Category 1 (Bad segment)
  1084. EAX != 0 => Category 2 (Bad IRET) */
  1085. testl %eax, %eax
  1086. popl %eax
  1087. lea 16(%esp), %esp
  1088. jz 5f
  1089. jmp iret_exc
  1090. 5: pushl $-1 /* orig_ax = -1 => not a system call */
  1091. SAVE_ALL
  1092. ENCODE_FRAME_POINTER
  1093. jmp ret_from_exception
  1094. .section .fixup, "ax"
  1095. 6: xorl %eax, %eax
  1096. movl %eax, 4(%esp)
  1097. jmp 1b
  1098. 7: xorl %eax, %eax
  1099. movl %eax, 8(%esp)
  1100. jmp 2b
  1101. 8: xorl %eax, %eax
  1102. movl %eax, 12(%esp)
  1103. jmp 3b
  1104. 9: xorl %eax, %eax
  1105. movl %eax, 16(%esp)
  1106. jmp 4b
  1107. .previous
  1108. _ASM_EXTABLE(1b, 6b)
  1109. _ASM_EXTABLE(2b, 7b)
  1110. _ASM_EXTABLE(3b, 8b)
  1111. _ASM_EXTABLE(4b, 9b)
  1112. ENDPROC(xen_failsafe_callback)
  1113. BUILD_INTERRUPT3(xen_hvm_callback_vector, HYPERVISOR_CALLBACK_VECTOR,
  1114. xen_evtchn_do_upcall)
  1115. #endif /* CONFIG_XEN */
  1116. #if IS_ENABLED(CONFIG_HYPERV)
  1117. BUILD_INTERRUPT3(hyperv_callback_vector, HYPERVISOR_CALLBACK_VECTOR,
  1118. hyperv_vector_handler)
  1119. BUILD_INTERRUPT3(hyperv_reenlightenment_vector, HYPERV_REENLIGHTENMENT_VECTOR,
  1120. hyperv_reenlightenment_intr)
  1121. BUILD_INTERRUPT3(hv_stimer0_callback_vector, HYPERV_STIMER0_VECTOR,
  1122. hv_stimer0_vector_handler)
  1123. #endif /* CONFIG_HYPERV */
  1124. ENTRY(page_fault)
  1125. ASM_CLAC
  1126. pushl $do_page_fault
  1127. ALIGN
  1128. jmp common_exception
  1129. END(page_fault)
  1130. common_exception:
  1131. /* the function address is in %gs's slot on the stack */
  1132. pushl %fs
  1133. pushl %es
  1134. pushl %ds
  1135. pushl %eax
  1136. movl $(__USER_DS), %eax
  1137. movl %eax, %ds
  1138. movl %eax, %es
  1139. movl $(__KERNEL_PERCPU), %eax
  1140. movl %eax, %fs
  1141. pushl %ebp
  1142. pushl %edi
  1143. pushl %esi
  1144. pushl %edx
  1145. pushl %ecx
  1146. pushl %ebx
  1147. SWITCH_TO_KERNEL_STACK
  1148. ENCODE_FRAME_POINTER
  1149. cld
  1150. UNWIND_ESPFIX_STACK
  1151. GS_TO_REG %ecx
  1152. movl PT_GS(%esp), %edi # get the function address
  1153. movl PT_ORIG_EAX(%esp), %edx # get the error code
  1154. movl $-1, PT_ORIG_EAX(%esp) # no syscall to restart
  1155. REG_TO_PTGS %ecx
  1156. SET_KERNEL_GS %ecx
  1157. TRACE_IRQS_OFF
  1158. movl %esp, %eax # pt_regs pointer
  1159. CALL_NOSPEC %edi
  1160. jmp ret_from_exception
  1161. END(common_exception)
  1162. ENTRY(debug)
  1163. /*
  1164. * Entry from sysenter is now handled in common_exception
  1165. */
  1166. ASM_CLAC
  1167. pushl $-1 # mark this as an int
  1168. pushl $do_debug
  1169. jmp common_exception
  1170. END(debug)
  1171. /*
  1172. * NMI is doubly nasty. It can happen on the first instruction of
  1173. * entry_SYSENTER_32 (just like #DB), but it can also interrupt the beginning
  1174. * of the #DB handler even if that #DB in turn hit before entry_SYSENTER_32
  1175. * switched stacks. We handle both conditions by simply checking whether we
  1176. * interrupted kernel code running on the SYSENTER stack.
  1177. */
  1178. ENTRY(nmi)
  1179. ASM_CLAC
  1180. #ifdef CONFIG_X86_ESPFIX32
  1181. pushl %eax
  1182. movl %ss, %eax
  1183. cmpw $__ESPFIX_SS, %ax
  1184. popl %eax
  1185. je .Lnmi_espfix_stack
  1186. #endif
  1187. pushl %eax # pt_regs->orig_ax
  1188. SAVE_ALL_NMI cr3_reg=%edi
  1189. ENCODE_FRAME_POINTER
  1190. xorl %edx, %edx # zero error code
  1191. movl %esp, %eax # pt_regs pointer
  1192. /* Are we currently on the SYSENTER stack? */
  1193. movl PER_CPU_VAR(cpu_entry_area), %ecx
  1194. addl $CPU_ENTRY_AREA_entry_stack + SIZEOF_entry_stack, %ecx
  1195. subl %eax, %ecx /* ecx = (end of entry_stack) - esp */
  1196. cmpl $SIZEOF_entry_stack, %ecx
  1197. jb .Lnmi_from_sysenter_stack
  1198. /* Not on SYSENTER stack. */
  1199. call do_nmi
  1200. jmp .Lnmi_return
  1201. .Lnmi_from_sysenter_stack:
  1202. /*
  1203. * We're on the SYSENTER stack. Switch off. No one (not even debug)
  1204. * is using the thread stack right now, so it's safe for us to use it.
  1205. */
  1206. movl %esp, %ebx
  1207. movl PER_CPU_VAR(cpu_current_top_of_stack), %esp
  1208. call do_nmi
  1209. movl %ebx, %esp
  1210. .Lnmi_return:
  1211. CHECK_AND_APPLY_ESPFIX
  1212. RESTORE_ALL_NMI cr3_reg=%edi pop=4
  1213. jmp .Lirq_return
  1214. #ifdef CONFIG_X86_ESPFIX32
  1215. .Lnmi_espfix_stack:
  1216. /*
  1217. * create the pointer to lss back
  1218. */
  1219. pushl %ss
  1220. pushl %esp
  1221. addl $4, (%esp)
  1222. /* copy the iret frame of 12 bytes */
  1223. .rept 3
  1224. pushl 16(%esp)
  1225. .endr
  1226. pushl %eax
  1227. SAVE_ALL_NMI cr3_reg=%edi
  1228. ENCODE_FRAME_POINTER
  1229. FIXUP_ESPFIX_STACK # %eax == %esp
  1230. xorl %edx, %edx # zero error code
  1231. call do_nmi
  1232. RESTORE_ALL_NMI cr3_reg=%edi
  1233. lss 12+4(%esp), %esp # back to espfix stack
  1234. jmp .Lirq_return
  1235. #endif
  1236. END(nmi)
  1237. ENTRY(int3)
  1238. ASM_CLAC
  1239. pushl $-1 # mark this as an int
  1240. SAVE_ALL switch_stacks=1
  1241. ENCODE_FRAME_POINTER
  1242. TRACE_IRQS_OFF
  1243. xorl %edx, %edx # zero error code
  1244. movl %esp, %eax # pt_regs pointer
  1245. call do_int3
  1246. jmp ret_from_exception
  1247. END(int3)
  1248. ENTRY(general_protection)
  1249. pushl $do_general_protection
  1250. jmp common_exception
  1251. END(general_protection)
  1252. #ifdef CONFIG_KVM_GUEST
  1253. ENTRY(async_page_fault)
  1254. ASM_CLAC
  1255. pushl $do_async_page_fault
  1256. jmp common_exception
  1257. END(async_page_fault)
  1258. #endif
  1259. ENTRY(rewind_stack_do_exit)
  1260. /* Prevent any naive code from trying to unwind to our caller. */
  1261. xorl %ebp, %ebp
  1262. movl PER_CPU_VAR(cpu_current_top_of_stack), %esi
  1263. leal -TOP_OF_KERNEL_STACK_PADDING-PTREGS_SIZE(%esi), %esp
  1264. call do_exit
  1265. 1: jmp 1b
  1266. END(rewind_stack_do_exit)