entry_32.S 37 KB

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