entry_32.S 29 KB

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