entry_32.S 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080
  1. /*
  2. * Copyright (C) 1991,1992 Linus Torvalds
  3. *
  4. * entry_32.S contains the system-call and low-level fault and trap handling routines.
  5. *
  6. * Stack layout while running C code:
  7. * ptrace needs to have all registers on the stack.
  8. * If the order here is changed, it needs to be
  9. * updated in fork.c:copy_process(), signal.c:do_signal(),
  10. * ptrace.c and ptrace.h
  11. *
  12. * 0(%esp) - %ebx
  13. * 4(%esp) - %ecx
  14. * 8(%esp) - %edx
  15. * C(%esp) - %esi
  16. * 10(%esp) - %edi
  17. * 14(%esp) - %ebp
  18. * 18(%esp) - %eax
  19. * 1C(%esp) - %ds
  20. * 20(%esp) - %es
  21. * 24(%esp) - %fs
  22. * 28(%esp) - %gs saved iff !CONFIG_X86_32_LAZY_GS
  23. * 2C(%esp) - orig_eax
  24. * 30(%esp) - %eip
  25. * 34(%esp) - %cs
  26. * 38(%esp) - %eflags
  27. * 3C(%esp) - %oldesp
  28. * 40(%esp) - %oldss
  29. */
  30. #include <linux/linkage.h>
  31. #include <linux/err.h>
  32. #include <asm/thread_info.h>
  33. #include <asm/irqflags.h>
  34. #include <asm/errno.h>
  35. #include <asm/segment.h>
  36. #include <asm/smp.h>
  37. #include <asm/page_types.h>
  38. #include <asm/percpu.h>
  39. #include <asm/processor-flags.h>
  40. #include <asm/ftrace.h>
  41. #include <asm/irq_vectors.h>
  42. #include <asm/cpufeature.h>
  43. #include <asm/alternative-asm.h>
  44. #include <asm/asm.h>
  45. #include <asm/smap.h>
  46. .section .entry.text, "ax"
  47. /*
  48. * We use macros for low-level operations which need to be overridden
  49. * for paravirtualization. The following will never clobber any registers:
  50. * INTERRUPT_RETURN (aka. "iret")
  51. * GET_CR0_INTO_EAX (aka. "movl %cr0, %eax")
  52. * ENABLE_INTERRUPTS_SYSEXIT (aka "sti; sysexit").
  53. *
  54. * For DISABLE_INTERRUPTS/ENABLE_INTERRUPTS (aka "cli"/"sti"), you must
  55. * specify what registers can be overwritten (CLBR_NONE, CLBR_EAX/EDX/ECX/ANY).
  56. * Allowing a register to be clobbered can shrink the paravirt replacement
  57. * enough to patch inline, increasing performance.
  58. */
  59. #ifdef CONFIG_PREEMPT
  60. # define preempt_stop(clobbers) DISABLE_INTERRUPTS(clobbers); TRACE_IRQS_OFF
  61. #else
  62. # define preempt_stop(clobbers)
  63. # define resume_kernel restore_all
  64. #endif
  65. .macro TRACE_IRQS_IRET
  66. #ifdef CONFIG_TRACE_IRQFLAGS
  67. testl $X86_EFLAGS_IF, PT_EFLAGS(%esp) # interrupts off?
  68. jz 1f
  69. TRACE_IRQS_ON
  70. 1:
  71. #endif
  72. .endm
  73. /*
  74. * User gs save/restore
  75. *
  76. * %gs is used for userland TLS and kernel only uses it for stack
  77. * canary which is required to be at %gs:20 by gcc. Read the comment
  78. * at the top of stackprotector.h for more info.
  79. *
  80. * Local labels 98 and 99 are used.
  81. */
  82. #ifdef CONFIG_X86_32_LAZY_GS
  83. /* unfortunately push/pop can't be no-op */
  84. .macro PUSH_GS
  85. pushl $0
  86. .endm
  87. .macro POP_GS pop=0
  88. addl $(4 + \pop), %esp
  89. .endm
  90. .macro POP_GS_EX
  91. .endm
  92. /* all the rest are no-op */
  93. .macro PTGS_TO_GS
  94. .endm
  95. .macro PTGS_TO_GS_EX
  96. .endm
  97. .macro GS_TO_REG reg
  98. .endm
  99. .macro REG_TO_PTGS reg
  100. .endm
  101. .macro SET_KERNEL_GS reg
  102. .endm
  103. #else /* CONFIG_X86_32_LAZY_GS */
  104. .macro PUSH_GS
  105. pushl %gs
  106. .endm
  107. .macro POP_GS pop=0
  108. 98: popl %gs
  109. .if \pop <> 0
  110. add $\pop, %esp
  111. .endif
  112. .endm
  113. .macro POP_GS_EX
  114. .pushsection .fixup, "ax"
  115. 99: movl $0, (%esp)
  116. jmp 98b
  117. .popsection
  118. _ASM_EXTABLE(98b, 99b)
  119. .endm
  120. .macro PTGS_TO_GS
  121. 98: mov PT_GS(%esp), %gs
  122. .endm
  123. .macro PTGS_TO_GS_EX
  124. .pushsection .fixup, "ax"
  125. 99: movl $0, PT_GS(%esp)
  126. jmp 98b
  127. .popsection
  128. _ASM_EXTABLE(98b, 99b)
  129. .endm
  130. .macro GS_TO_REG reg
  131. movl %gs, \reg
  132. .endm
  133. .macro REG_TO_PTGS reg
  134. movl \reg, PT_GS(%esp)
  135. .endm
  136. .macro SET_KERNEL_GS reg
  137. movl $(__KERNEL_STACK_CANARY), \reg
  138. movl \reg, %gs
  139. .endm
  140. #endif /* CONFIG_X86_32_LAZY_GS */
  141. .macro SAVE_ALL pt_regs_ax=%eax
  142. cld
  143. PUSH_GS
  144. pushl %fs
  145. pushl %es
  146. pushl %ds
  147. pushl \pt_regs_ax
  148. pushl %ebp
  149. pushl %edi
  150. pushl %esi
  151. pushl %edx
  152. pushl %ecx
  153. pushl %ebx
  154. movl $(__USER_DS), %edx
  155. movl %edx, %ds
  156. movl %edx, %es
  157. movl $(__KERNEL_PERCPU), %edx
  158. movl %edx, %fs
  159. SET_KERNEL_GS %edx
  160. .endm
  161. .macro RESTORE_INT_REGS
  162. popl %ebx
  163. popl %ecx
  164. popl %edx
  165. popl %esi
  166. popl %edi
  167. popl %ebp
  168. popl %eax
  169. .endm
  170. .macro RESTORE_REGS pop=0
  171. RESTORE_INT_REGS
  172. 1: popl %ds
  173. 2: popl %es
  174. 3: popl %fs
  175. POP_GS \pop
  176. .pushsection .fixup, "ax"
  177. 4: movl $0, (%esp)
  178. jmp 1b
  179. 5: movl $0, (%esp)
  180. jmp 2b
  181. 6: movl $0, (%esp)
  182. jmp 3b
  183. .popsection
  184. _ASM_EXTABLE(1b, 4b)
  185. _ASM_EXTABLE(2b, 5b)
  186. _ASM_EXTABLE(3b, 6b)
  187. POP_GS_EX
  188. .endm
  189. ENTRY(ret_from_fork)
  190. pushl %eax
  191. call schedule_tail
  192. GET_THREAD_INFO(%ebp)
  193. popl %eax
  194. pushl $0x0202 # Reset kernel eflags
  195. popfl
  196. /* When we fork, we trace the syscall return in the child, too. */
  197. movl %esp, %eax
  198. call syscall_return_slowpath
  199. jmp restore_all
  200. END(ret_from_fork)
  201. ENTRY(ret_from_kernel_thread)
  202. pushl %eax
  203. call schedule_tail
  204. GET_THREAD_INFO(%ebp)
  205. popl %eax
  206. pushl $0x0202 # Reset kernel eflags
  207. popfl
  208. movl PT_EBP(%esp), %eax
  209. call *PT_EBX(%esp)
  210. movl $0, PT_EAX(%esp)
  211. /*
  212. * Kernel threads return to userspace as if returning from a syscall.
  213. * We should check whether anything actually uses this path and, if so,
  214. * consider switching it over to ret_from_fork.
  215. */
  216. movl %esp, %eax
  217. call syscall_return_slowpath
  218. jmp restore_all
  219. ENDPROC(ret_from_kernel_thread)
  220. /*
  221. * Return to user mode is not as complex as all this looks,
  222. * but we want the default path for a system call return to
  223. * go as quickly as possible which is why some of this is
  224. * less clear than it otherwise should be.
  225. */
  226. # userspace resumption stub bypassing syscall exit tracing
  227. ALIGN
  228. ret_from_exception:
  229. preempt_stop(CLBR_ANY)
  230. ret_from_intr:
  231. GET_THREAD_INFO(%ebp)
  232. #ifdef CONFIG_VM86
  233. movl PT_EFLAGS(%esp), %eax # mix EFLAGS and CS
  234. movb PT_CS(%esp), %al
  235. andl $(X86_EFLAGS_VM | SEGMENT_RPL_MASK), %eax
  236. #else
  237. /*
  238. * We can be coming here from child spawned by kernel_thread().
  239. */
  240. movl PT_CS(%esp), %eax
  241. andl $SEGMENT_RPL_MASK, %eax
  242. #endif
  243. cmpl $USER_RPL, %eax
  244. jb resume_kernel # not returning to v8086 or userspace
  245. ENTRY(resume_userspace)
  246. DISABLE_INTERRUPTS(CLBR_ANY)
  247. TRACE_IRQS_OFF
  248. movl %esp, %eax
  249. call prepare_exit_to_usermode
  250. jmp restore_all
  251. END(ret_from_exception)
  252. #ifdef CONFIG_PREEMPT
  253. ENTRY(resume_kernel)
  254. DISABLE_INTERRUPTS(CLBR_ANY)
  255. need_resched:
  256. cmpl $0, PER_CPU_VAR(__preempt_count)
  257. jnz restore_all
  258. testl $X86_EFLAGS_IF, PT_EFLAGS(%esp) # interrupts off (exception path) ?
  259. jz restore_all
  260. call preempt_schedule_irq
  261. jmp need_resched
  262. END(resume_kernel)
  263. #endif
  264. # SYSENTER call handler stub
  265. ENTRY(entry_SYSENTER_32)
  266. movl TSS_sysenter_sp0(%esp), %esp
  267. sysenter_past_esp:
  268. pushl $__USER_DS /* pt_regs->ss */
  269. pushl %ecx /* pt_regs->cx */
  270. pushfl /* pt_regs->flags (except IF = 0) */
  271. orl $X86_EFLAGS_IF, (%esp) /* Fix IF */
  272. pushl $__USER_CS /* pt_regs->cs */
  273. pushl $0 /* pt_regs->ip = 0 (placeholder) */
  274. pushl %eax /* pt_regs->orig_ax */
  275. SAVE_ALL pt_regs_ax=$-ENOSYS /* save rest */
  276. /*
  277. * User mode is traced as though IRQs are on, and SYSENTER
  278. * turned them off.
  279. */
  280. TRACE_IRQS_OFF
  281. movl %esp, %eax
  282. call do_fast_syscall_32
  283. testl %eax, %eax
  284. jz .Lsyscall_32_done
  285. /* Opportunistic SYSEXIT */
  286. TRACE_IRQS_ON /* User mode traces as IRQs on. */
  287. movl PT_EIP(%esp), %edx /* pt_regs->ip */
  288. movl PT_OLDESP(%esp), %ecx /* pt_regs->sp */
  289. 1: mov PT_FS(%esp), %fs
  290. PTGS_TO_GS
  291. popl %ebx /* pt_regs->bx */
  292. addl $2*4, %esp /* skip pt_regs->cx and pt_regs->dx */
  293. popl %esi /* pt_regs->si */
  294. popl %edi /* pt_regs->di */
  295. popl %ebp /* pt_regs->bp */
  296. popl %eax /* pt_regs->ax */
  297. /*
  298. * Return back to the vDSO, which will pop ecx and edx.
  299. * Don't bother with DS and ES (they already contain __USER_DS).
  300. */
  301. ENABLE_INTERRUPTS_SYSEXIT
  302. .pushsection .fixup, "ax"
  303. 2: movl $0, PT_FS(%esp)
  304. jmp 1b
  305. .popsection
  306. _ASM_EXTABLE(1b, 2b)
  307. PTGS_TO_GS_EX
  308. ENDPROC(entry_SYSENTER_32)
  309. # system call handler stub
  310. ENTRY(entry_INT80_32)
  311. ASM_CLAC
  312. pushl %eax /* pt_regs->orig_ax */
  313. SAVE_ALL pt_regs_ax=$-ENOSYS /* save rest */
  314. /*
  315. * User mode is traced as though IRQs are on. Unlike the 64-bit
  316. * case, INT80 is a trap gate on 32-bit kernels, so interrupts
  317. * are already on (unless user code is messing around with iopl).
  318. */
  319. movl %esp, %eax
  320. call do_syscall_32_irqs_on
  321. .Lsyscall_32_done:
  322. restore_all:
  323. TRACE_IRQS_IRET
  324. restore_all_notrace:
  325. #ifdef CONFIG_X86_ESPFIX32
  326. movl PT_EFLAGS(%esp), %eax # mix EFLAGS, SS and CS
  327. /*
  328. * Warning: PT_OLDSS(%esp) contains the wrong/random values if we
  329. * are returning to the kernel.
  330. * See comments in process.c:copy_thread() for details.
  331. */
  332. movb PT_OLDSS(%esp), %ah
  333. movb PT_CS(%esp), %al
  334. andl $(X86_EFLAGS_VM | (SEGMENT_TI_MASK << 8) | SEGMENT_RPL_MASK), %eax
  335. cmpl $((SEGMENT_LDT << 8) | USER_RPL), %eax
  336. je ldt_ss # returning to user-space with LDT SS
  337. #endif
  338. restore_nocheck:
  339. RESTORE_REGS 4 # skip orig_eax/error_code
  340. irq_return:
  341. INTERRUPT_RETURN
  342. .section .fixup, "ax"
  343. ENTRY(iret_exc )
  344. pushl $0 # no error code
  345. pushl $do_iret_error
  346. jmp error_code
  347. .previous
  348. _ASM_EXTABLE(irq_return, iret_exc)
  349. #ifdef CONFIG_X86_ESPFIX32
  350. ldt_ss:
  351. #ifdef CONFIG_PARAVIRT
  352. /*
  353. * The kernel can't run on a non-flat stack if paravirt mode
  354. * is active. Rather than try to fixup the high bits of
  355. * ESP, bypass this code entirely. This may break DOSemu
  356. * and/or Wine support in a paravirt VM, although the option
  357. * is still available to implement the setting of the high
  358. * 16-bits in the INTERRUPT_RETURN paravirt-op.
  359. */
  360. cmpl $0, pv_info+PARAVIRT_enabled
  361. jne restore_nocheck
  362. #endif
  363. /*
  364. * Setup and switch to ESPFIX stack
  365. *
  366. * We're returning to userspace with a 16 bit stack. The CPU will not
  367. * restore the high word of ESP for us on executing iret... This is an
  368. * "official" bug of all the x86-compatible CPUs, which we can work
  369. * around to make dosemu and wine happy. We do this by preloading the
  370. * high word of ESP with the high word of the userspace ESP while
  371. * compensating for the offset by changing to the ESPFIX segment with
  372. * a base address that matches for the difference.
  373. */
  374. #define GDT_ESPFIX_SS PER_CPU_VAR(gdt_page) + (GDT_ENTRY_ESPFIX_SS * 8)
  375. mov %esp, %edx /* load kernel esp */
  376. mov PT_OLDESP(%esp), %eax /* load userspace esp */
  377. mov %dx, %ax /* eax: new kernel esp */
  378. sub %eax, %edx /* offset (low word is 0) */
  379. shr $16, %edx
  380. mov %dl, GDT_ESPFIX_SS + 4 /* bits 16..23 */
  381. mov %dh, GDT_ESPFIX_SS + 7 /* bits 24..31 */
  382. pushl $__ESPFIX_SS
  383. pushl %eax /* new kernel esp */
  384. /*
  385. * Disable interrupts, but do not irqtrace this section: we
  386. * will soon execute iret and the tracer was already set to
  387. * the irqstate after the IRET:
  388. */
  389. DISABLE_INTERRUPTS(CLBR_EAX)
  390. lss (%esp), %esp /* switch to espfix segment */
  391. jmp restore_nocheck
  392. #endif
  393. ENDPROC(entry_INT80_32)
  394. .macro FIXUP_ESPFIX_STACK
  395. /*
  396. * Switch back for ESPFIX stack to the normal zerobased stack
  397. *
  398. * We can't call C functions using the ESPFIX stack. This code reads
  399. * the high word of the segment base from the GDT and swiches to the
  400. * normal stack and adjusts ESP with the matching offset.
  401. */
  402. #ifdef CONFIG_X86_ESPFIX32
  403. /* fixup the stack */
  404. mov GDT_ESPFIX_SS + 4, %al /* bits 16..23 */
  405. mov GDT_ESPFIX_SS + 7, %ah /* bits 24..31 */
  406. shl $16, %eax
  407. addl %esp, %eax /* the adjusted stack pointer */
  408. pushl $__KERNEL_DS
  409. pushl %eax
  410. lss (%esp), %esp /* switch to the normal stack segment */
  411. #endif
  412. .endm
  413. .macro UNWIND_ESPFIX_STACK
  414. #ifdef CONFIG_X86_ESPFIX32
  415. movl %ss, %eax
  416. /* see if on espfix stack */
  417. cmpw $__ESPFIX_SS, %ax
  418. jne 27f
  419. movl $__KERNEL_DS, %eax
  420. movl %eax, %ds
  421. movl %eax, %es
  422. /* switch to normal stack */
  423. FIXUP_ESPFIX_STACK
  424. 27:
  425. #endif
  426. .endm
  427. /*
  428. * Build the entry stubs with some assembler magic.
  429. * We pack 1 stub into every 8-byte block.
  430. */
  431. .align 8
  432. ENTRY(irq_entries_start)
  433. vector=FIRST_EXTERNAL_VECTOR
  434. .rept (FIRST_SYSTEM_VECTOR - FIRST_EXTERNAL_VECTOR)
  435. pushl $(~vector+0x80) /* Note: always in signed byte range */
  436. vector=vector+1
  437. jmp common_interrupt
  438. .align 8
  439. .endr
  440. END(irq_entries_start)
  441. /*
  442. * the CPU automatically disables interrupts when executing an IRQ vector,
  443. * so IRQ-flags tracing has to follow that:
  444. */
  445. .p2align CONFIG_X86_L1_CACHE_SHIFT
  446. common_interrupt:
  447. ASM_CLAC
  448. addl $-0x80, (%esp) /* Adjust vector into the [-256, -1] range */
  449. SAVE_ALL
  450. TRACE_IRQS_OFF
  451. movl %esp, %eax
  452. call do_IRQ
  453. jmp ret_from_intr
  454. ENDPROC(common_interrupt)
  455. #define BUILD_INTERRUPT3(name, nr, fn) \
  456. ENTRY(name) \
  457. ASM_CLAC; \
  458. pushl $~(nr); \
  459. SAVE_ALL; \
  460. TRACE_IRQS_OFF \
  461. movl %esp, %eax; \
  462. call fn; \
  463. jmp ret_from_intr; \
  464. ENDPROC(name)
  465. #ifdef CONFIG_TRACING
  466. # define TRACE_BUILD_INTERRUPT(name, nr) BUILD_INTERRUPT3(trace_##name, nr, smp_trace_##name)
  467. #else
  468. # define TRACE_BUILD_INTERRUPT(name, nr)
  469. #endif
  470. #define BUILD_INTERRUPT(name, nr) \
  471. BUILD_INTERRUPT3(name, nr, smp_##name); \
  472. TRACE_BUILD_INTERRUPT(name, nr)
  473. /* The include is where all of the SMP etc. interrupts come from */
  474. #include <asm/entry_arch.h>
  475. ENTRY(coprocessor_error)
  476. ASM_CLAC
  477. pushl $0
  478. pushl $do_coprocessor_error
  479. jmp error_code
  480. END(coprocessor_error)
  481. ENTRY(simd_coprocessor_error)
  482. ASM_CLAC
  483. pushl $0
  484. #ifdef CONFIG_X86_INVD_BUG
  485. /* AMD 486 bug: invd from userspace calls exception 19 instead of #GP */
  486. ALTERNATIVE "pushl $do_general_protection", \
  487. "pushl $do_simd_coprocessor_error", \
  488. X86_FEATURE_XMM
  489. #else
  490. pushl $do_simd_coprocessor_error
  491. #endif
  492. jmp error_code
  493. END(simd_coprocessor_error)
  494. ENTRY(device_not_available)
  495. ASM_CLAC
  496. pushl $-1 # mark this as an int
  497. pushl $do_device_not_available
  498. jmp error_code
  499. END(device_not_available)
  500. #ifdef CONFIG_PARAVIRT
  501. ENTRY(native_iret)
  502. iret
  503. _ASM_EXTABLE(native_iret, iret_exc)
  504. END(native_iret)
  505. ENTRY(native_irq_enable_sysexit)
  506. sti
  507. sysexit
  508. END(native_irq_enable_sysexit)
  509. #endif
  510. ENTRY(overflow)
  511. ASM_CLAC
  512. pushl $0
  513. pushl $do_overflow
  514. jmp error_code
  515. END(overflow)
  516. ENTRY(bounds)
  517. ASM_CLAC
  518. pushl $0
  519. pushl $do_bounds
  520. jmp error_code
  521. END(bounds)
  522. ENTRY(invalid_op)
  523. ASM_CLAC
  524. pushl $0
  525. pushl $do_invalid_op
  526. jmp error_code
  527. END(invalid_op)
  528. ENTRY(coprocessor_segment_overrun)
  529. ASM_CLAC
  530. pushl $0
  531. pushl $do_coprocessor_segment_overrun
  532. jmp error_code
  533. END(coprocessor_segment_overrun)
  534. ENTRY(invalid_TSS)
  535. ASM_CLAC
  536. pushl $do_invalid_TSS
  537. jmp error_code
  538. END(invalid_TSS)
  539. ENTRY(segment_not_present)
  540. ASM_CLAC
  541. pushl $do_segment_not_present
  542. jmp error_code
  543. END(segment_not_present)
  544. ENTRY(stack_segment)
  545. ASM_CLAC
  546. pushl $do_stack_segment
  547. jmp error_code
  548. END(stack_segment)
  549. ENTRY(alignment_check)
  550. ASM_CLAC
  551. pushl $do_alignment_check
  552. jmp error_code
  553. END(alignment_check)
  554. ENTRY(divide_error)
  555. ASM_CLAC
  556. pushl $0 # no error code
  557. pushl $do_divide_error
  558. jmp error_code
  559. END(divide_error)
  560. #ifdef CONFIG_X86_MCE
  561. ENTRY(machine_check)
  562. ASM_CLAC
  563. pushl $0
  564. pushl machine_check_vector
  565. jmp error_code
  566. END(machine_check)
  567. #endif
  568. ENTRY(spurious_interrupt_bug)
  569. ASM_CLAC
  570. pushl $0
  571. pushl $do_spurious_interrupt_bug
  572. jmp error_code
  573. END(spurious_interrupt_bug)
  574. #ifdef CONFIG_XEN
  575. /*
  576. * Xen doesn't set %esp to be precisely what the normal SYSENTER
  577. * entry point expects, so fix it up before using the normal path.
  578. */
  579. ENTRY(xen_sysenter_target)
  580. addl $5*4, %esp /* remove xen-provided frame */
  581. jmp sysenter_past_esp
  582. ENTRY(xen_hypervisor_callback)
  583. pushl $-1 /* orig_ax = -1 => not a system call */
  584. SAVE_ALL
  585. TRACE_IRQS_OFF
  586. /*
  587. * Check to see if we got the event in the critical
  588. * region in xen_iret_direct, after we've reenabled
  589. * events and checked for pending events. This simulates
  590. * iret instruction's behaviour where it delivers a
  591. * pending interrupt when enabling interrupts:
  592. */
  593. movl PT_EIP(%esp), %eax
  594. cmpl $xen_iret_start_crit, %eax
  595. jb 1f
  596. cmpl $xen_iret_end_crit, %eax
  597. jae 1f
  598. jmp xen_iret_crit_fixup
  599. ENTRY(xen_do_upcall)
  600. 1: mov %esp, %eax
  601. call xen_evtchn_do_upcall
  602. #ifndef CONFIG_PREEMPT
  603. call xen_maybe_preempt_hcall
  604. #endif
  605. jmp ret_from_intr
  606. ENDPROC(xen_hypervisor_callback)
  607. /*
  608. * Hypervisor uses this for application faults while it executes.
  609. * We get here for two reasons:
  610. * 1. Fault while reloading DS, ES, FS or GS
  611. * 2. Fault while executing IRET
  612. * Category 1 we fix up by reattempting the load, and zeroing the segment
  613. * register if the load fails.
  614. * Category 2 we fix up by jumping to do_iret_error. We cannot use the
  615. * normal Linux return path in this case because if we use the IRET hypercall
  616. * to pop the stack frame we end up in an infinite loop of failsafe callbacks.
  617. * We distinguish between categories by maintaining a status value in EAX.
  618. */
  619. ENTRY(xen_failsafe_callback)
  620. pushl %eax
  621. movl $1, %eax
  622. 1: mov 4(%esp), %ds
  623. 2: mov 8(%esp), %es
  624. 3: mov 12(%esp), %fs
  625. 4: mov 16(%esp), %gs
  626. /* EAX == 0 => Category 1 (Bad segment)
  627. EAX != 0 => Category 2 (Bad IRET) */
  628. testl %eax, %eax
  629. popl %eax
  630. lea 16(%esp), %esp
  631. jz 5f
  632. jmp iret_exc
  633. 5: pushl $-1 /* orig_ax = -1 => not a system call */
  634. SAVE_ALL
  635. jmp ret_from_exception
  636. .section .fixup, "ax"
  637. 6: xorl %eax, %eax
  638. movl %eax, 4(%esp)
  639. jmp 1b
  640. 7: xorl %eax, %eax
  641. movl %eax, 8(%esp)
  642. jmp 2b
  643. 8: xorl %eax, %eax
  644. movl %eax, 12(%esp)
  645. jmp 3b
  646. 9: xorl %eax, %eax
  647. movl %eax, 16(%esp)
  648. jmp 4b
  649. .previous
  650. _ASM_EXTABLE(1b, 6b)
  651. _ASM_EXTABLE(2b, 7b)
  652. _ASM_EXTABLE(3b, 8b)
  653. _ASM_EXTABLE(4b, 9b)
  654. ENDPROC(xen_failsafe_callback)
  655. BUILD_INTERRUPT3(xen_hvm_callback_vector, HYPERVISOR_CALLBACK_VECTOR,
  656. xen_evtchn_do_upcall)
  657. #endif /* CONFIG_XEN */
  658. #if IS_ENABLED(CONFIG_HYPERV)
  659. BUILD_INTERRUPT3(hyperv_callback_vector, HYPERVISOR_CALLBACK_VECTOR,
  660. hyperv_vector_handler)
  661. #endif /* CONFIG_HYPERV */
  662. #ifdef CONFIG_FUNCTION_TRACER
  663. #ifdef CONFIG_DYNAMIC_FTRACE
  664. ENTRY(mcount)
  665. ret
  666. END(mcount)
  667. ENTRY(ftrace_caller)
  668. pushl %eax
  669. pushl %ecx
  670. pushl %edx
  671. pushl $0 /* Pass NULL as regs pointer */
  672. movl 4*4(%esp), %eax
  673. movl 0x4(%ebp), %edx
  674. movl function_trace_op, %ecx
  675. subl $MCOUNT_INSN_SIZE, %eax
  676. .globl ftrace_call
  677. ftrace_call:
  678. call ftrace_stub
  679. addl $4, %esp /* skip NULL pointer */
  680. popl %edx
  681. popl %ecx
  682. popl %eax
  683. ftrace_ret:
  684. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  685. .globl ftrace_graph_call
  686. ftrace_graph_call:
  687. jmp ftrace_stub
  688. #endif
  689. .globl ftrace_stub
  690. ftrace_stub:
  691. ret
  692. END(ftrace_caller)
  693. ENTRY(ftrace_regs_caller)
  694. pushf /* push flags before compare (in cs location) */
  695. /*
  696. * i386 does not save SS and ESP when coming from kernel.
  697. * Instead, to get sp, &regs->sp is used (see ptrace.h).
  698. * Unfortunately, that means eflags must be at the same location
  699. * as the current return ip is. We move the return ip into the
  700. * ip location, and move flags into the return ip location.
  701. */
  702. pushl 4(%esp) /* save return ip into ip slot */
  703. pushl $0 /* Load 0 into orig_ax */
  704. pushl %gs
  705. pushl %fs
  706. pushl %es
  707. pushl %ds
  708. pushl %eax
  709. pushl %ebp
  710. pushl %edi
  711. pushl %esi
  712. pushl %edx
  713. pushl %ecx
  714. pushl %ebx
  715. movl 13*4(%esp), %eax /* Get the saved flags */
  716. movl %eax, 14*4(%esp) /* Move saved flags into regs->flags location */
  717. /* clobbering return ip */
  718. movl $__KERNEL_CS, 13*4(%esp)
  719. movl 12*4(%esp), %eax /* Load ip (1st parameter) */
  720. subl $MCOUNT_INSN_SIZE, %eax /* Adjust ip */
  721. movl 0x4(%ebp), %edx /* Load parent ip (2nd parameter) */
  722. movl function_trace_op, %ecx /* Save ftrace_pos in 3rd parameter */
  723. pushl %esp /* Save pt_regs as 4th parameter */
  724. GLOBAL(ftrace_regs_call)
  725. call ftrace_stub
  726. addl $4, %esp /* Skip pt_regs */
  727. movl 14*4(%esp), %eax /* Move flags back into cs */
  728. movl %eax, 13*4(%esp) /* Needed to keep addl from modifying flags */
  729. movl 12*4(%esp), %eax /* Get return ip from regs->ip */
  730. movl %eax, 14*4(%esp) /* Put return ip back for ret */
  731. popl %ebx
  732. popl %ecx
  733. popl %edx
  734. popl %esi
  735. popl %edi
  736. popl %ebp
  737. popl %eax
  738. popl %ds
  739. popl %es
  740. popl %fs
  741. popl %gs
  742. addl $8, %esp /* Skip orig_ax and ip */
  743. popf /* Pop flags at end (no addl to corrupt flags) */
  744. jmp ftrace_ret
  745. popf
  746. jmp ftrace_stub
  747. #else /* ! CONFIG_DYNAMIC_FTRACE */
  748. ENTRY(mcount)
  749. cmpl $__PAGE_OFFSET, %esp
  750. jb ftrace_stub /* Paging not enabled yet? */
  751. cmpl $ftrace_stub, ftrace_trace_function
  752. jnz trace
  753. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  754. cmpl $ftrace_stub, ftrace_graph_return
  755. jnz ftrace_graph_caller
  756. cmpl $ftrace_graph_entry_stub, ftrace_graph_entry
  757. jnz ftrace_graph_caller
  758. #endif
  759. .globl ftrace_stub
  760. ftrace_stub:
  761. ret
  762. /* taken from glibc */
  763. trace:
  764. pushl %eax
  765. pushl %ecx
  766. pushl %edx
  767. movl 0xc(%esp), %eax
  768. movl 0x4(%ebp), %edx
  769. subl $MCOUNT_INSN_SIZE, %eax
  770. call *ftrace_trace_function
  771. popl %edx
  772. popl %ecx
  773. popl %eax
  774. jmp ftrace_stub
  775. END(mcount)
  776. #endif /* CONFIG_DYNAMIC_FTRACE */
  777. #endif /* CONFIG_FUNCTION_TRACER */
  778. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  779. ENTRY(ftrace_graph_caller)
  780. pushl %eax
  781. pushl %ecx
  782. pushl %edx
  783. movl 0xc(%esp), %eax
  784. lea 0x4(%ebp), %edx
  785. movl (%ebp), %ecx
  786. subl $MCOUNT_INSN_SIZE, %eax
  787. call prepare_ftrace_return
  788. popl %edx
  789. popl %ecx
  790. popl %eax
  791. ret
  792. END(ftrace_graph_caller)
  793. .globl return_to_handler
  794. return_to_handler:
  795. pushl %eax
  796. pushl %edx
  797. movl %ebp, %eax
  798. call ftrace_return_to_handler
  799. movl %eax, %ecx
  800. popl %edx
  801. popl %eax
  802. jmp *%ecx
  803. #endif
  804. #ifdef CONFIG_TRACING
  805. ENTRY(trace_page_fault)
  806. ASM_CLAC
  807. pushl $trace_do_page_fault
  808. jmp error_code
  809. END(trace_page_fault)
  810. #endif
  811. ENTRY(page_fault)
  812. ASM_CLAC
  813. pushl $do_page_fault
  814. ALIGN
  815. error_code:
  816. /* the function address is in %gs's slot on the stack */
  817. pushl %fs
  818. pushl %es
  819. pushl %ds
  820. pushl %eax
  821. pushl %ebp
  822. pushl %edi
  823. pushl %esi
  824. pushl %edx
  825. pushl %ecx
  826. pushl %ebx
  827. cld
  828. movl $(__KERNEL_PERCPU), %ecx
  829. movl %ecx, %fs
  830. UNWIND_ESPFIX_STACK
  831. GS_TO_REG %ecx
  832. movl PT_GS(%esp), %edi # get the function address
  833. movl PT_ORIG_EAX(%esp), %edx # get the error code
  834. movl $-1, PT_ORIG_EAX(%esp) # no syscall to restart
  835. REG_TO_PTGS %ecx
  836. SET_KERNEL_GS %ecx
  837. movl $(__USER_DS), %ecx
  838. movl %ecx, %ds
  839. movl %ecx, %es
  840. TRACE_IRQS_OFF
  841. movl %esp, %eax # pt_regs pointer
  842. call *%edi
  843. jmp ret_from_exception
  844. END(page_fault)
  845. /*
  846. * Debug traps and NMI can happen at the one SYSENTER instruction
  847. * that sets up the real kernel stack. Check here, since we can't
  848. * allow the wrong stack to be used.
  849. *
  850. * "TSS_sysenter_sp0+12" is because the NMI/debug handler will have
  851. * already pushed 3 words if it hits on the sysenter instruction:
  852. * eflags, cs and eip.
  853. *
  854. * We just load the right stack, and push the three (known) values
  855. * by hand onto the new stack - while updating the return eip past
  856. * the instruction that would have done it for sysenter.
  857. */
  858. .macro FIX_STACK offset ok label
  859. cmpw $__KERNEL_CS, 4(%esp)
  860. jne \ok
  861. \label:
  862. movl TSS_sysenter_sp0 + \offset(%esp), %esp
  863. pushfl
  864. pushl $__KERNEL_CS
  865. pushl $sysenter_past_esp
  866. .endm
  867. ENTRY(debug)
  868. ASM_CLAC
  869. cmpl $entry_SYSENTER_32, (%esp)
  870. jne debug_stack_correct
  871. FIX_STACK 12, debug_stack_correct, debug_esp_fix_insn
  872. debug_stack_correct:
  873. pushl $-1 # mark this as an int
  874. SAVE_ALL
  875. TRACE_IRQS_OFF
  876. xorl %edx, %edx # error code 0
  877. movl %esp, %eax # pt_regs pointer
  878. call do_debug
  879. jmp ret_from_exception
  880. END(debug)
  881. /*
  882. * NMI is doubly nasty. It can happen _while_ we're handling
  883. * a debug fault, and the debug fault hasn't yet been able to
  884. * clear up the stack. So we first check whether we got an
  885. * NMI on the sysenter entry path, but after that we need to
  886. * check whether we got an NMI on the debug path where the debug
  887. * fault happened on the sysenter path.
  888. */
  889. ENTRY(nmi)
  890. ASM_CLAC
  891. #ifdef CONFIG_X86_ESPFIX32
  892. pushl %eax
  893. movl %ss, %eax
  894. cmpw $__ESPFIX_SS, %ax
  895. popl %eax
  896. je nmi_espfix_stack
  897. #endif
  898. cmpl $entry_SYSENTER_32, (%esp)
  899. je nmi_stack_fixup
  900. pushl %eax
  901. movl %esp, %eax
  902. /*
  903. * Do not access memory above the end of our stack page,
  904. * it might not exist.
  905. */
  906. andl $(THREAD_SIZE-1), %eax
  907. cmpl $(THREAD_SIZE-20), %eax
  908. popl %eax
  909. jae nmi_stack_correct
  910. cmpl $entry_SYSENTER_32, 12(%esp)
  911. je nmi_debug_stack_check
  912. nmi_stack_correct:
  913. pushl %eax
  914. SAVE_ALL
  915. xorl %edx, %edx # zero error code
  916. movl %esp, %eax # pt_regs pointer
  917. call do_nmi
  918. jmp restore_all_notrace
  919. nmi_stack_fixup:
  920. FIX_STACK 12, nmi_stack_correct, 1
  921. jmp nmi_stack_correct
  922. nmi_debug_stack_check:
  923. cmpw $__KERNEL_CS, 16(%esp)
  924. jne nmi_stack_correct
  925. cmpl $debug, (%esp)
  926. jb nmi_stack_correct
  927. cmpl $debug_esp_fix_insn, (%esp)
  928. ja nmi_stack_correct
  929. FIX_STACK 24, nmi_stack_correct, 1
  930. jmp nmi_stack_correct
  931. #ifdef CONFIG_X86_ESPFIX32
  932. nmi_espfix_stack:
  933. /*
  934. * create the pointer to lss back
  935. */
  936. pushl %ss
  937. pushl %esp
  938. addl $4, (%esp)
  939. /* copy the iret frame of 12 bytes */
  940. .rept 3
  941. pushl 16(%esp)
  942. .endr
  943. pushl %eax
  944. SAVE_ALL
  945. FIXUP_ESPFIX_STACK # %eax == %esp
  946. xorl %edx, %edx # zero error code
  947. call do_nmi
  948. RESTORE_REGS
  949. lss 12+4(%esp), %esp # back to espfix stack
  950. jmp irq_return
  951. #endif
  952. END(nmi)
  953. ENTRY(int3)
  954. ASM_CLAC
  955. pushl $-1 # mark this as an int
  956. SAVE_ALL
  957. TRACE_IRQS_OFF
  958. xorl %edx, %edx # zero error code
  959. movl %esp, %eax # pt_regs pointer
  960. call do_int3
  961. jmp ret_from_exception
  962. END(int3)
  963. ENTRY(general_protection)
  964. pushl $do_general_protection
  965. jmp error_code
  966. END(general_protection)
  967. #ifdef CONFIG_KVM_GUEST
  968. ENTRY(async_page_fault)
  969. ASM_CLAC
  970. pushl $do_async_page_fault
  971. jmp error_code
  972. END(async_page_fault)
  973. #endif