entry_32.S 37 KB

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