entry_32.S 37 KB

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