entry_32.S 37 KB

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