entry_32.S 25 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142
  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 in 'syscall_exit':
  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
  142. cld
  143. PUSH_GS
  144. pushl %fs
  145. pushl %es
  146. pushl %ds
  147. pushl %eax
  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. jmp syscall_exit
  197. END(ret_from_fork)
  198. ENTRY(ret_from_kernel_thread)
  199. pushl %eax
  200. call schedule_tail
  201. GET_THREAD_INFO(%ebp)
  202. popl %eax
  203. pushl $0x0202 # Reset kernel eflags
  204. popfl
  205. movl PT_EBP(%esp), %eax
  206. call *PT_EBX(%esp)
  207. movl $0, PT_EAX(%esp)
  208. jmp syscall_exit
  209. ENDPROC(ret_from_kernel_thread)
  210. /*
  211. * Return to user mode is not as complex as all this looks,
  212. * but we want the default path for a system call return to
  213. * go as quickly as possible which is why some of this is
  214. * less clear than it otherwise should be.
  215. */
  216. # userspace resumption stub bypassing syscall exit tracing
  217. ALIGN
  218. ret_from_exception:
  219. preempt_stop(CLBR_ANY)
  220. ret_from_intr:
  221. GET_THREAD_INFO(%ebp)
  222. #ifdef CONFIG_VM86
  223. movl PT_EFLAGS(%esp), %eax # mix EFLAGS and CS
  224. movb PT_CS(%esp), %al
  225. andl $(X86_EFLAGS_VM | SEGMENT_RPL_MASK), %eax
  226. #else
  227. /*
  228. * We can be coming here from child spawned by kernel_thread().
  229. */
  230. movl PT_CS(%esp), %eax
  231. andl $SEGMENT_RPL_MASK, %eax
  232. #endif
  233. cmpl $USER_RPL, %eax
  234. jb resume_kernel # not returning to v8086 or userspace
  235. ENTRY(resume_userspace)
  236. LOCKDEP_SYS_EXIT
  237. DISABLE_INTERRUPTS(CLBR_ANY)
  238. TRACE_IRQS_OFF
  239. movl %esp, %eax
  240. call prepare_exit_to_usermode
  241. jmp restore_all
  242. END(ret_from_exception)
  243. #ifdef CONFIG_PREEMPT
  244. ENTRY(resume_kernel)
  245. DISABLE_INTERRUPTS(CLBR_ANY)
  246. need_resched:
  247. cmpl $0, PER_CPU_VAR(__preempt_count)
  248. jnz restore_all
  249. testl $X86_EFLAGS_IF, PT_EFLAGS(%esp) # interrupts off (exception path) ?
  250. jz restore_all
  251. call preempt_schedule_irq
  252. jmp need_resched
  253. END(resume_kernel)
  254. #endif
  255. /*
  256. * SYSENTER_RETURN points to after the SYSENTER instruction
  257. * in the vsyscall page. See vsyscall-sysentry.S, which defines
  258. * the symbol.
  259. */
  260. # SYSENTER call handler stub
  261. ENTRY(entry_SYSENTER_32)
  262. movl TSS_sysenter_sp0(%esp), %esp
  263. sysenter_past_esp:
  264. /*
  265. * Interrupts are disabled here, but we can't trace it until
  266. * enough kernel state to call TRACE_IRQS_OFF can be called - but
  267. * we immediately enable interrupts at that point anyway.
  268. */
  269. pushl $__USER_DS
  270. pushl %ebp
  271. pushfl
  272. orl $X86_EFLAGS_IF, (%esp)
  273. pushl $__USER_CS
  274. /*
  275. * Push current_thread_info()->sysenter_return to the stack.
  276. * A tiny bit of offset fixup is necessary: TI_sysenter_return
  277. * is relative to thread_info, which is at the bottom of the
  278. * kernel stack page. 4*4 means the 4 words pushed above;
  279. * TOP_OF_KERNEL_STACK_PADDING takes us to the top of the stack;
  280. * and THREAD_SIZE takes us to the bottom.
  281. */
  282. pushl ((TI_sysenter_return) - THREAD_SIZE + TOP_OF_KERNEL_STACK_PADDING + 4*4)(%esp)
  283. pushl %eax
  284. SAVE_ALL
  285. ENABLE_INTERRUPTS(CLBR_NONE)
  286. /*
  287. * Load the potential sixth argument from user stack.
  288. * Careful about security.
  289. */
  290. cmpl $__PAGE_OFFSET-3, %ebp
  291. jae syscall_fault
  292. ASM_STAC
  293. 1: movl (%ebp), %ebp
  294. ASM_CLAC
  295. movl %ebp, PT_EBP(%esp)
  296. _ASM_EXTABLE(1b, syscall_fault)
  297. GET_THREAD_INFO(%ebp)
  298. testl $_TIF_WORK_SYSCALL_ENTRY, TI_flags(%ebp)
  299. jnz syscall_trace_entry
  300. sysenter_do_call:
  301. cmpl $(NR_syscalls), %eax
  302. jae sysenter_badsys
  303. call *sys_call_table(, %eax, 4)
  304. sysenter_after_call:
  305. movl %eax, PT_EAX(%esp)
  306. LOCKDEP_SYS_EXIT
  307. DISABLE_INTERRUPTS(CLBR_ANY)
  308. TRACE_IRQS_OFF
  309. movl TI_flags(%ebp), %ecx
  310. testl $_TIF_ALLWORK_MASK, %ecx
  311. jnz syscall_exit_work_irqs_off
  312. sysenter_exit:
  313. /* if something modifies registers it must also disable sysexit */
  314. movl PT_EIP(%esp), %edx
  315. movl PT_OLDESP(%esp), %ecx
  316. xorl %ebp, %ebp
  317. TRACE_IRQS_ON
  318. 1: mov PT_FS(%esp), %fs
  319. PTGS_TO_GS
  320. ENABLE_INTERRUPTS_SYSEXIT
  321. .pushsection .fixup, "ax"
  322. 2: movl $0, PT_FS(%esp)
  323. jmp 1b
  324. .popsection
  325. _ASM_EXTABLE(1b, 2b)
  326. PTGS_TO_GS_EX
  327. ENDPROC(entry_SYSENTER_32)
  328. # system call handler stub
  329. ENTRY(entry_INT80_32)
  330. ASM_CLAC
  331. pushl %eax # save orig_eax
  332. SAVE_ALL
  333. GET_THREAD_INFO(%ebp)
  334. # system call tracing in operation / emulation
  335. testl $_TIF_WORK_SYSCALL_ENTRY, TI_flags(%ebp)
  336. jnz syscall_trace_entry
  337. cmpl $(NR_syscalls), %eax
  338. jae syscall_badsys
  339. syscall_call:
  340. call *sys_call_table(, %eax, 4)
  341. syscall_after_call:
  342. movl %eax, PT_EAX(%esp) # store the return value
  343. syscall_exit:
  344. LOCKDEP_SYS_EXIT
  345. jmp syscall_exit_work
  346. restore_all:
  347. TRACE_IRQS_IRET
  348. restore_all_notrace:
  349. #ifdef CONFIG_X86_ESPFIX32
  350. movl PT_EFLAGS(%esp), %eax # mix EFLAGS, SS and CS
  351. /*
  352. * Warning: PT_OLDSS(%esp) contains the wrong/random values if we
  353. * are returning to the kernel.
  354. * See comments in process.c:copy_thread() for details.
  355. */
  356. movb PT_OLDSS(%esp), %ah
  357. movb PT_CS(%esp), %al
  358. andl $(X86_EFLAGS_VM | (SEGMENT_TI_MASK << 8) | SEGMENT_RPL_MASK), %eax
  359. cmpl $((SEGMENT_LDT << 8) | USER_RPL), %eax
  360. je ldt_ss # returning to user-space with LDT SS
  361. #endif
  362. restore_nocheck:
  363. RESTORE_REGS 4 # skip orig_eax/error_code
  364. irq_return:
  365. INTERRUPT_RETURN
  366. .section .fixup, "ax"
  367. ENTRY(iret_exc )
  368. pushl $0 # no error code
  369. pushl $do_iret_error
  370. jmp error_code
  371. .previous
  372. _ASM_EXTABLE(irq_return, iret_exc)
  373. #ifdef CONFIG_X86_ESPFIX32
  374. ldt_ss:
  375. #ifdef CONFIG_PARAVIRT
  376. /*
  377. * The kernel can't run on a non-flat stack if paravirt mode
  378. * is active. Rather than try to fixup the high bits of
  379. * ESP, bypass this code entirely. This may break DOSemu
  380. * and/or Wine support in a paravirt VM, although the option
  381. * is still available to implement the setting of the high
  382. * 16-bits in the INTERRUPT_RETURN paravirt-op.
  383. */
  384. cmpl $0, pv_info+PARAVIRT_enabled
  385. jne restore_nocheck
  386. #endif
  387. /*
  388. * Setup and switch to ESPFIX stack
  389. *
  390. * We're returning to userspace with a 16 bit stack. The CPU will not
  391. * restore the high word of ESP for us on executing iret... This is an
  392. * "official" bug of all the x86-compatible CPUs, which we can work
  393. * around to make dosemu and wine happy. We do this by preloading the
  394. * high word of ESP with the high word of the userspace ESP while
  395. * compensating for the offset by changing to the ESPFIX segment with
  396. * a base address that matches for the difference.
  397. */
  398. #define GDT_ESPFIX_SS PER_CPU_VAR(gdt_page) + (GDT_ENTRY_ESPFIX_SS * 8)
  399. mov %esp, %edx /* load kernel esp */
  400. mov PT_OLDESP(%esp), %eax /* load userspace esp */
  401. mov %dx, %ax /* eax: new kernel esp */
  402. sub %eax, %edx /* offset (low word is 0) */
  403. shr $16, %edx
  404. mov %dl, GDT_ESPFIX_SS + 4 /* bits 16..23 */
  405. mov %dh, GDT_ESPFIX_SS + 7 /* bits 24..31 */
  406. pushl $__ESPFIX_SS
  407. pushl %eax /* new kernel esp */
  408. /*
  409. * Disable interrupts, but do not irqtrace this section: we
  410. * will soon execute iret and the tracer was already set to
  411. * the irqstate after the IRET:
  412. */
  413. DISABLE_INTERRUPTS(CLBR_EAX)
  414. lss (%esp), %esp /* switch to espfix segment */
  415. jmp restore_nocheck
  416. #endif
  417. ENDPROC(entry_INT80_32)
  418. # perform syscall exit tracing
  419. ALIGN
  420. syscall_trace_entry:
  421. movl $-ENOSYS, PT_EAX(%esp)
  422. movl %esp, %eax
  423. call syscall_trace_enter
  424. /* What it returned is what we'll actually use. */
  425. cmpl $(NR_syscalls), %eax
  426. jnae syscall_call
  427. jmp syscall_exit
  428. END(syscall_trace_entry)
  429. # perform syscall exit tracing
  430. ALIGN
  431. syscall_exit_work_irqs_off:
  432. TRACE_IRQS_ON
  433. ENABLE_INTERRUPTS(CLBR_ANY)
  434. syscall_exit_work:
  435. movl %esp, %eax
  436. call syscall_return_slowpath
  437. jmp restore_all
  438. END(syscall_exit_work)
  439. syscall_fault:
  440. ASM_CLAC
  441. GET_THREAD_INFO(%ebp)
  442. movl $-EFAULT, PT_EAX(%esp)
  443. jmp resume_userspace
  444. END(syscall_fault)
  445. syscall_badsys:
  446. movl $-ENOSYS, %eax
  447. jmp syscall_after_call
  448. END(syscall_badsys)
  449. sysenter_badsys:
  450. movl $-ENOSYS, %eax
  451. jmp sysenter_after_call
  452. END(sysenter_badsys)
  453. .macro FIXUP_ESPFIX_STACK
  454. /*
  455. * Switch back for ESPFIX stack to the normal zerobased stack
  456. *
  457. * We can't call C functions using the ESPFIX stack. This code reads
  458. * the high word of the segment base from the GDT and swiches to the
  459. * normal stack and adjusts ESP with the matching offset.
  460. */
  461. #ifdef CONFIG_X86_ESPFIX32
  462. /* fixup the stack */
  463. mov GDT_ESPFIX_SS + 4, %al /* bits 16..23 */
  464. mov GDT_ESPFIX_SS + 7, %ah /* bits 24..31 */
  465. shl $16, %eax
  466. addl %esp, %eax /* the adjusted stack pointer */
  467. pushl $__KERNEL_DS
  468. pushl %eax
  469. lss (%esp), %esp /* switch to the normal stack segment */
  470. #endif
  471. .endm
  472. .macro UNWIND_ESPFIX_STACK
  473. #ifdef CONFIG_X86_ESPFIX32
  474. movl %ss, %eax
  475. /* see if on espfix stack */
  476. cmpw $__ESPFIX_SS, %ax
  477. jne 27f
  478. movl $__KERNEL_DS, %eax
  479. movl %eax, %ds
  480. movl %eax, %es
  481. /* switch to normal stack */
  482. FIXUP_ESPFIX_STACK
  483. 27:
  484. #endif
  485. .endm
  486. /*
  487. * Build the entry stubs with some assembler magic.
  488. * We pack 1 stub into every 8-byte block.
  489. */
  490. .align 8
  491. ENTRY(irq_entries_start)
  492. vector=FIRST_EXTERNAL_VECTOR
  493. .rept (FIRST_SYSTEM_VECTOR - FIRST_EXTERNAL_VECTOR)
  494. pushl $(~vector+0x80) /* Note: always in signed byte range */
  495. vector=vector+1
  496. jmp common_interrupt
  497. .align 8
  498. .endr
  499. END(irq_entries_start)
  500. /*
  501. * the CPU automatically disables interrupts when executing an IRQ vector,
  502. * so IRQ-flags tracing has to follow that:
  503. */
  504. .p2align CONFIG_X86_L1_CACHE_SHIFT
  505. common_interrupt:
  506. ASM_CLAC
  507. addl $-0x80, (%esp) /* Adjust vector into the [-256, -1] range */
  508. SAVE_ALL
  509. TRACE_IRQS_OFF
  510. movl %esp, %eax
  511. call do_IRQ
  512. jmp ret_from_intr
  513. ENDPROC(common_interrupt)
  514. #define BUILD_INTERRUPT3(name, nr, fn) \
  515. ENTRY(name) \
  516. ASM_CLAC; \
  517. pushl $~(nr); \
  518. SAVE_ALL; \
  519. TRACE_IRQS_OFF \
  520. movl %esp, %eax; \
  521. call fn; \
  522. jmp ret_from_intr; \
  523. ENDPROC(name)
  524. #ifdef CONFIG_TRACING
  525. # define TRACE_BUILD_INTERRUPT(name, nr) BUILD_INTERRUPT3(trace_##name, nr, smp_trace_##name)
  526. #else
  527. # define TRACE_BUILD_INTERRUPT(name, nr)
  528. #endif
  529. #define BUILD_INTERRUPT(name, nr) \
  530. BUILD_INTERRUPT3(name, nr, smp_##name); \
  531. TRACE_BUILD_INTERRUPT(name, nr)
  532. /* The include is where all of the SMP etc. interrupts come from */
  533. #include <asm/entry_arch.h>
  534. ENTRY(coprocessor_error)
  535. ASM_CLAC
  536. pushl $0
  537. pushl $do_coprocessor_error
  538. jmp error_code
  539. END(coprocessor_error)
  540. ENTRY(simd_coprocessor_error)
  541. ASM_CLAC
  542. pushl $0
  543. #ifdef CONFIG_X86_INVD_BUG
  544. /* AMD 486 bug: invd from userspace calls exception 19 instead of #GP */
  545. ALTERNATIVE "pushl $do_general_protection", \
  546. "pushl $do_simd_coprocessor_error", \
  547. X86_FEATURE_XMM
  548. #else
  549. pushl $do_simd_coprocessor_error
  550. #endif
  551. jmp error_code
  552. END(simd_coprocessor_error)
  553. ENTRY(device_not_available)
  554. ASM_CLAC
  555. pushl $-1 # mark this as an int
  556. pushl $do_device_not_available
  557. jmp error_code
  558. END(device_not_available)
  559. #ifdef CONFIG_PARAVIRT
  560. ENTRY(native_iret)
  561. iret
  562. _ASM_EXTABLE(native_iret, iret_exc)
  563. END(native_iret)
  564. ENTRY(native_irq_enable_sysexit)
  565. sti
  566. sysexit
  567. END(native_irq_enable_sysexit)
  568. #endif
  569. ENTRY(overflow)
  570. ASM_CLAC
  571. pushl $0
  572. pushl $do_overflow
  573. jmp error_code
  574. END(overflow)
  575. ENTRY(bounds)
  576. ASM_CLAC
  577. pushl $0
  578. pushl $do_bounds
  579. jmp error_code
  580. END(bounds)
  581. ENTRY(invalid_op)
  582. ASM_CLAC
  583. pushl $0
  584. pushl $do_invalid_op
  585. jmp error_code
  586. END(invalid_op)
  587. ENTRY(coprocessor_segment_overrun)
  588. ASM_CLAC
  589. pushl $0
  590. pushl $do_coprocessor_segment_overrun
  591. jmp error_code
  592. END(coprocessor_segment_overrun)
  593. ENTRY(invalid_TSS)
  594. ASM_CLAC
  595. pushl $do_invalid_TSS
  596. jmp error_code
  597. END(invalid_TSS)
  598. ENTRY(segment_not_present)
  599. ASM_CLAC
  600. pushl $do_segment_not_present
  601. jmp error_code
  602. END(segment_not_present)
  603. ENTRY(stack_segment)
  604. ASM_CLAC
  605. pushl $do_stack_segment
  606. jmp error_code
  607. END(stack_segment)
  608. ENTRY(alignment_check)
  609. ASM_CLAC
  610. pushl $do_alignment_check
  611. jmp error_code
  612. END(alignment_check)
  613. ENTRY(divide_error)
  614. ASM_CLAC
  615. pushl $0 # no error code
  616. pushl $do_divide_error
  617. jmp error_code
  618. END(divide_error)
  619. #ifdef CONFIG_X86_MCE
  620. ENTRY(machine_check)
  621. ASM_CLAC
  622. pushl $0
  623. pushl machine_check_vector
  624. jmp error_code
  625. END(machine_check)
  626. #endif
  627. ENTRY(spurious_interrupt_bug)
  628. ASM_CLAC
  629. pushl $0
  630. pushl $do_spurious_interrupt_bug
  631. jmp error_code
  632. END(spurious_interrupt_bug)
  633. #ifdef CONFIG_XEN
  634. /*
  635. * Xen doesn't set %esp to be precisely what the normal SYSENTER
  636. * entry point expects, so fix it up before using the normal path.
  637. */
  638. ENTRY(xen_sysenter_target)
  639. addl $5*4, %esp /* remove xen-provided frame */
  640. jmp sysenter_past_esp
  641. ENTRY(xen_hypervisor_callback)
  642. pushl $-1 /* orig_ax = -1 => not a system call */
  643. SAVE_ALL
  644. TRACE_IRQS_OFF
  645. /*
  646. * Check to see if we got the event in the critical
  647. * region in xen_iret_direct, after we've reenabled
  648. * events and checked for pending events. This simulates
  649. * iret instruction's behaviour where it delivers a
  650. * pending interrupt when enabling interrupts:
  651. */
  652. movl PT_EIP(%esp), %eax
  653. cmpl $xen_iret_start_crit, %eax
  654. jb 1f
  655. cmpl $xen_iret_end_crit, %eax
  656. jae 1f
  657. jmp xen_iret_crit_fixup
  658. ENTRY(xen_do_upcall)
  659. 1: mov %esp, %eax
  660. call xen_evtchn_do_upcall
  661. #ifndef CONFIG_PREEMPT
  662. call xen_maybe_preempt_hcall
  663. #endif
  664. jmp ret_from_intr
  665. ENDPROC(xen_hypervisor_callback)
  666. /*
  667. * Hypervisor uses this for application faults while it executes.
  668. * We get here for two reasons:
  669. * 1. Fault while reloading DS, ES, FS or GS
  670. * 2. Fault while executing IRET
  671. * Category 1 we fix up by reattempting the load, and zeroing the segment
  672. * register if the load fails.
  673. * Category 2 we fix up by jumping to do_iret_error. We cannot use the
  674. * normal Linux return path in this case because if we use the IRET hypercall
  675. * to pop the stack frame we end up in an infinite loop of failsafe callbacks.
  676. * We distinguish between categories by maintaining a status value in EAX.
  677. */
  678. ENTRY(xen_failsafe_callback)
  679. pushl %eax
  680. movl $1, %eax
  681. 1: mov 4(%esp), %ds
  682. 2: mov 8(%esp), %es
  683. 3: mov 12(%esp), %fs
  684. 4: mov 16(%esp), %gs
  685. /* EAX == 0 => Category 1 (Bad segment)
  686. EAX != 0 => Category 2 (Bad IRET) */
  687. testl %eax, %eax
  688. popl %eax
  689. lea 16(%esp), %esp
  690. jz 5f
  691. jmp iret_exc
  692. 5: pushl $-1 /* orig_ax = -1 => not a system call */
  693. SAVE_ALL
  694. jmp ret_from_exception
  695. .section .fixup, "ax"
  696. 6: xorl %eax, %eax
  697. movl %eax, 4(%esp)
  698. jmp 1b
  699. 7: xorl %eax, %eax
  700. movl %eax, 8(%esp)
  701. jmp 2b
  702. 8: xorl %eax, %eax
  703. movl %eax, 12(%esp)
  704. jmp 3b
  705. 9: xorl %eax, %eax
  706. movl %eax, 16(%esp)
  707. jmp 4b
  708. .previous
  709. _ASM_EXTABLE(1b, 6b)
  710. _ASM_EXTABLE(2b, 7b)
  711. _ASM_EXTABLE(3b, 8b)
  712. _ASM_EXTABLE(4b, 9b)
  713. ENDPROC(xen_failsafe_callback)
  714. BUILD_INTERRUPT3(xen_hvm_callback_vector, HYPERVISOR_CALLBACK_VECTOR,
  715. xen_evtchn_do_upcall)
  716. #endif /* CONFIG_XEN */
  717. #if IS_ENABLED(CONFIG_HYPERV)
  718. BUILD_INTERRUPT3(hyperv_callback_vector, HYPERVISOR_CALLBACK_VECTOR,
  719. hyperv_vector_handler)
  720. #endif /* CONFIG_HYPERV */
  721. #ifdef CONFIG_FUNCTION_TRACER
  722. #ifdef CONFIG_DYNAMIC_FTRACE
  723. ENTRY(mcount)
  724. ret
  725. END(mcount)
  726. ENTRY(ftrace_caller)
  727. pushl %eax
  728. pushl %ecx
  729. pushl %edx
  730. pushl $0 /* Pass NULL as regs pointer */
  731. movl 4*4(%esp), %eax
  732. movl 0x4(%ebp), %edx
  733. movl function_trace_op, %ecx
  734. subl $MCOUNT_INSN_SIZE, %eax
  735. .globl ftrace_call
  736. ftrace_call:
  737. call ftrace_stub
  738. addl $4, %esp /* skip NULL pointer */
  739. popl %edx
  740. popl %ecx
  741. popl %eax
  742. ftrace_ret:
  743. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  744. .globl ftrace_graph_call
  745. ftrace_graph_call:
  746. jmp ftrace_stub
  747. #endif
  748. .globl ftrace_stub
  749. ftrace_stub:
  750. ret
  751. END(ftrace_caller)
  752. ENTRY(ftrace_regs_caller)
  753. pushf /* push flags before compare (in cs location) */
  754. /*
  755. * i386 does not save SS and ESP when coming from kernel.
  756. * Instead, to get sp, &regs->sp is used (see ptrace.h).
  757. * Unfortunately, that means eflags must be at the same location
  758. * as the current return ip is. We move the return ip into the
  759. * ip location, and move flags into the return ip location.
  760. */
  761. pushl 4(%esp) /* save return ip into ip slot */
  762. pushl $0 /* Load 0 into orig_ax */
  763. pushl %gs
  764. pushl %fs
  765. pushl %es
  766. pushl %ds
  767. pushl %eax
  768. pushl %ebp
  769. pushl %edi
  770. pushl %esi
  771. pushl %edx
  772. pushl %ecx
  773. pushl %ebx
  774. movl 13*4(%esp), %eax /* Get the saved flags */
  775. movl %eax, 14*4(%esp) /* Move saved flags into regs->flags location */
  776. /* clobbering return ip */
  777. movl $__KERNEL_CS, 13*4(%esp)
  778. movl 12*4(%esp), %eax /* Load ip (1st parameter) */
  779. subl $MCOUNT_INSN_SIZE, %eax /* Adjust ip */
  780. movl 0x4(%ebp), %edx /* Load parent ip (2nd parameter) */
  781. movl function_trace_op, %ecx /* Save ftrace_pos in 3rd parameter */
  782. pushl %esp /* Save pt_regs as 4th parameter */
  783. GLOBAL(ftrace_regs_call)
  784. call ftrace_stub
  785. addl $4, %esp /* Skip pt_regs */
  786. movl 14*4(%esp), %eax /* Move flags back into cs */
  787. movl %eax, 13*4(%esp) /* Needed to keep addl from modifying flags */
  788. movl 12*4(%esp), %eax /* Get return ip from regs->ip */
  789. movl %eax, 14*4(%esp) /* Put return ip back for ret */
  790. popl %ebx
  791. popl %ecx
  792. popl %edx
  793. popl %esi
  794. popl %edi
  795. popl %ebp
  796. popl %eax
  797. popl %ds
  798. popl %es
  799. popl %fs
  800. popl %gs
  801. addl $8, %esp /* Skip orig_ax and ip */
  802. popf /* Pop flags at end (no addl to corrupt flags) */
  803. jmp ftrace_ret
  804. popf
  805. jmp ftrace_stub
  806. #else /* ! CONFIG_DYNAMIC_FTRACE */
  807. ENTRY(mcount)
  808. cmpl $__PAGE_OFFSET, %esp
  809. jb ftrace_stub /* Paging not enabled yet? */
  810. cmpl $ftrace_stub, ftrace_trace_function
  811. jnz trace
  812. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  813. cmpl $ftrace_stub, ftrace_graph_return
  814. jnz ftrace_graph_caller
  815. cmpl $ftrace_graph_entry_stub, ftrace_graph_entry
  816. jnz ftrace_graph_caller
  817. #endif
  818. .globl ftrace_stub
  819. ftrace_stub:
  820. ret
  821. /* taken from glibc */
  822. trace:
  823. pushl %eax
  824. pushl %ecx
  825. pushl %edx
  826. movl 0xc(%esp), %eax
  827. movl 0x4(%ebp), %edx
  828. subl $MCOUNT_INSN_SIZE, %eax
  829. call *ftrace_trace_function
  830. popl %edx
  831. popl %ecx
  832. popl %eax
  833. jmp ftrace_stub
  834. END(mcount)
  835. #endif /* CONFIG_DYNAMIC_FTRACE */
  836. #endif /* CONFIG_FUNCTION_TRACER */
  837. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  838. ENTRY(ftrace_graph_caller)
  839. pushl %eax
  840. pushl %ecx
  841. pushl %edx
  842. movl 0xc(%esp), %eax
  843. lea 0x4(%ebp), %edx
  844. movl (%ebp), %ecx
  845. subl $MCOUNT_INSN_SIZE, %eax
  846. call prepare_ftrace_return
  847. popl %edx
  848. popl %ecx
  849. popl %eax
  850. ret
  851. END(ftrace_graph_caller)
  852. .globl return_to_handler
  853. return_to_handler:
  854. pushl %eax
  855. pushl %edx
  856. movl %ebp, %eax
  857. call ftrace_return_to_handler
  858. movl %eax, %ecx
  859. popl %edx
  860. popl %eax
  861. jmp *%ecx
  862. #endif
  863. #ifdef CONFIG_TRACING
  864. ENTRY(trace_page_fault)
  865. ASM_CLAC
  866. pushl $trace_do_page_fault
  867. jmp error_code
  868. END(trace_page_fault)
  869. #endif
  870. ENTRY(page_fault)
  871. ASM_CLAC
  872. pushl $do_page_fault
  873. ALIGN
  874. error_code:
  875. /* the function address is in %gs's slot on the stack */
  876. pushl %fs
  877. pushl %es
  878. pushl %ds
  879. pushl %eax
  880. pushl %ebp
  881. pushl %edi
  882. pushl %esi
  883. pushl %edx
  884. pushl %ecx
  885. pushl %ebx
  886. cld
  887. movl $(__KERNEL_PERCPU), %ecx
  888. movl %ecx, %fs
  889. UNWIND_ESPFIX_STACK
  890. GS_TO_REG %ecx
  891. movl PT_GS(%esp), %edi # get the function address
  892. movl PT_ORIG_EAX(%esp), %edx # get the error code
  893. movl $-1, PT_ORIG_EAX(%esp) # no syscall to restart
  894. REG_TO_PTGS %ecx
  895. SET_KERNEL_GS %ecx
  896. movl $(__USER_DS), %ecx
  897. movl %ecx, %ds
  898. movl %ecx, %es
  899. TRACE_IRQS_OFF
  900. movl %esp, %eax # pt_regs pointer
  901. call *%edi
  902. jmp ret_from_exception
  903. END(page_fault)
  904. /*
  905. * Debug traps and NMI can happen at the one SYSENTER instruction
  906. * that sets up the real kernel stack. Check here, since we can't
  907. * allow the wrong stack to be used.
  908. *
  909. * "TSS_sysenter_sp0+12" is because the NMI/debug handler will have
  910. * already pushed 3 words if it hits on the sysenter instruction:
  911. * eflags, cs and eip.
  912. *
  913. * We just load the right stack, and push the three (known) values
  914. * by hand onto the new stack - while updating the return eip past
  915. * the instruction that would have done it for sysenter.
  916. */
  917. .macro FIX_STACK offset ok label
  918. cmpw $__KERNEL_CS, 4(%esp)
  919. jne \ok
  920. \label:
  921. movl TSS_sysenter_sp0 + \offset(%esp), %esp
  922. pushfl
  923. pushl $__KERNEL_CS
  924. pushl $sysenter_past_esp
  925. .endm
  926. ENTRY(debug)
  927. ASM_CLAC
  928. cmpl $entry_SYSENTER_32, (%esp)
  929. jne debug_stack_correct
  930. FIX_STACK 12, debug_stack_correct, debug_esp_fix_insn
  931. debug_stack_correct:
  932. pushl $-1 # mark this as an int
  933. SAVE_ALL
  934. TRACE_IRQS_OFF
  935. xorl %edx, %edx # error code 0
  936. movl %esp, %eax # pt_regs pointer
  937. call do_debug
  938. jmp ret_from_exception
  939. END(debug)
  940. /*
  941. * NMI is doubly nasty. It can happen _while_ we're handling
  942. * a debug fault, and the debug fault hasn't yet been able to
  943. * clear up the stack. So we first check whether we got an
  944. * NMI on the sysenter entry path, but after that we need to
  945. * check whether we got an NMI on the debug path where the debug
  946. * fault happened on the sysenter path.
  947. */
  948. ENTRY(nmi)
  949. ASM_CLAC
  950. #ifdef CONFIG_X86_ESPFIX32
  951. pushl %eax
  952. movl %ss, %eax
  953. cmpw $__ESPFIX_SS, %ax
  954. popl %eax
  955. je nmi_espfix_stack
  956. #endif
  957. cmpl $entry_SYSENTER_32, (%esp)
  958. je nmi_stack_fixup
  959. pushl %eax
  960. movl %esp, %eax
  961. /*
  962. * Do not access memory above the end of our stack page,
  963. * it might not exist.
  964. */
  965. andl $(THREAD_SIZE-1), %eax
  966. cmpl $(THREAD_SIZE-20), %eax
  967. popl %eax
  968. jae nmi_stack_correct
  969. cmpl $entry_SYSENTER_32, 12(%esp)
  970. je nmi_debug_stack_check
  971. nmi_stack_correct:
  972. pushl %eax
  973. SAVE_ALL
  974. xorl %edx, %edx # zero error code
  975. movl %esp, %eax # pt_regs pointer
  976. call do_nmi
  977. jmp restore_all_notrace
  978. nmi_stack_fixup:
  979. FIX_STACK 12, nmi_stack_correct, 1
  980. jmp nmi_stack_correct
  981. nmi_debug_stack_check:
  982. cmpw $__KERNEL_CS, 16(%esp)
  983. jne nmi_stack_correct
  984. cmpl $debug, (%esp)
  985. jb nmi_stack_correct
  986. cmpl $debug_esp_fix_insn, (%esp)
  987. ja nmi_stack_correct
  988. FIX_STACK 24, nmi_stack_correct, 1
  989. jmp nmi_stack_correct
  990. #ifdef CONFIG_X86_ESPFIX32
  991. nmi_espfix_stack:
  992. /*
  993. * create the pointer to lss back
  994. */
  995. pushl %ss
  996. pushl %esp
  997. addl $4, (%esp)
  998. /* copy the iret frame of 12 bytes */
  999. .rept 3
  1000. pushl 16(%esp)
  1001. .endr
  1002. pushl %eax
  1003. SAVE_ALL
  1004. FIXUP_ESPFIX_STACK # %eax == %esp
  1005. xorl %edx, %edx # zero error code
  1006. call do_nmi
  1007. RESTORE_REGS
  1008. lss 12+4(%esp), %esp # back to espfix stack
  1009. jmp irq_return
  1010. #endif
  1011. END(nmi)
  1012. ENTRY(int3)
  1013. ASM_CLAC
  1014. pushl $-1 # mark this as an int
  1015. SAVE_ALL
  1016. TRACE_IRQS_OFF
  1017. xorl %edx, %edx # zero error code
  1018. movl %esp, %eax # pt_regs pointer
  1019. call do_int3
  1020. jmp ret_from_exception
  1021. END(int3)
  1022. ENTRY(general_protection)
  1023. pushl $do_general_protection
  1024. jmp error_code
  1025. END(general_protection)
  1026. #ifdef CONFIG_KVM_GUEST
  1027. ENTRY(async_page_fault)
  1028. ASM_CLAC
  1029. pushl $do_async_page_fault
  1030. jmp error_code
  1031. END(async_page_fault)
  1032. #endif