entry_32.S 34 KB

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