entry.S 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669
  1. /*
  2. * linux/arch/i386/entry.S
  3. *
  4. * Copyright (C) 1991, 1992 Linus Torvalds
  5. */
  6. /*
  7. * entry.S contains the system-call and fault low-level handling routines.
  8. * This also contains the timer-interrupt handler, as well as all interrupts
  9. * and faults that can result in a task-switch.
  10. *
  11. * NOTE: This code handles signal-recognition, which happens every time
  12. * after a timer-interrupt and after each system call.
  13. *
  14. * I changed all the .align's to 4 (16 byte alignment), as that's faster
  15. * on a 486.
  16. *
  17. * Stack layout in 'ret_from_system_call':
  18. * ptrace needs to have all regs on the stack.
  19. * if the order here is changed, it needs to be
  20. * updated in fork.c:copy_process, signal.c:do_signal,
  21. * ptrace.c and ptrace.h
  22. *
  23. * 0(%esp) - %ebx
  24. * 4(%esp) - %ecx
  25. * 8(%esp) - %edx
  26. * C(%esp) - %esi
  27. * 10(%esp) - %edi
  28. * 14(%esp) - %ebp
  29. * 18(%esp) - %eax
  30. * 1C(%esp) - %ds
  31. * 20(%esp) - %es
  32. * 24(%esp) - orig_eax
  33. * 28(%esp) - %eip
  34. * 2C(%esp) - %cs
  35. * 30(%esp) - %eflags
  36. * 34(%esp) - %oldesp
  37. * 38(%esp) - %oldss
  38. *
  39. * "current" is in register %ebx during any slow entries.
  40. */
  41. #include <linux/config.h>
  42. #include <linux/linkage.h>
  43. #include <asm/thread_info.h>
  44. #include <asm/errno.h>
  45. #include <asm/segment.h>
  46. #include <asm/smp.h>
  47. #include <asm/page.h>
  48. #include <asm/desc.h>
  49. #include "irq_vectors.h"
  50. #define nr_syscalls ((syscall_table_size)/4)
  51. EBX = 0x00
  52. ECX = 0x04
  53. EDX = 0x08
  54. ESI = 0x0C
  55. EDI = 0x10
  56. EBP = 0x14
  57. EAX = 0x18
  58. DS = 0x1C
  59. ES = 0x20
  60. ORIG_EAX = 0x24
  61. EIP = 0x28
  62. CS = 0x2C
  63. EFLAGS = 0x30
  64. OLDESP = 0x34
  65. OLDSS = 0x38
  66. CF_MASK = 0x00000001
  67. TF_MASK = 0x00000100
  68. IF_MASK = 0x00000200
  69. DF_MASK = 0x00000400
  70. NT_MASK = 0x00004000
  71. VM_MASK = 0x00020000
  72. #ifdef CONFIG_PREEMPT
  73. #define preempt_stop cli
  74. #else
  75. #define preempt_stop
  76. #define resume_kernel restore_nocheck
  77. #endif
  78. #define SAVE_ALL \
  79. cld; \
  80. pushl %es; \
  81. pushl %ds; \
  82. pushl %eax; \
  83. pushl %ebp; \
  84. pushl %edi; \
  85. pushl %esi; \
  86. pushl %edx; \
  87. pushl %ecx; \
  88. pushl %ebx; \
  89. movl $(__USER_DS), %edx; \
  90. movl %edx, %ds; \
  91. movl %edx, %es;
  92. #define RESTORE_INT_REGS \
  93. popl %ebx; \
  94. popl %ecx; \
  95. popl %edx; \
  96. popl %esi; \
  97. popl %edi; \
  98. popl %ebp; \
  99. popl %eax
  100. #define RESTORE_REGS \
  101. RESTORE_INT_REGS; \
  102. 1: popl %ds; \
  103. 2: popl %es; \
  104. .section .fixup,"ax"; \
  105. 3: movl $0,(%esp); \
  106. jmp 1b; \
  107. 4: movl $0,(%esp); \
  108. jmp 2b; \
  109. .previous; \
  110. .section __ex_table,"a";\
  111. .align 4; \
  112. .long 1b,3b; \
  113. .long 2b,4b; \
  114. .previous
  115. ENTRY(ret_from_fork)
  116. pushl %eax
  117. call schedule_tail
  118. GET_THREAD_INFO(%ebp)
  119. popl %eax
  120. jmp syscall_exit
  121. /*
  122. * Return to user mode is not as complex as all this looks,
  123. * but we want the default path for a system call return to
  124. * go as quickly as possible which is why some of this is
  125. * less clear than it otherwise should be.
  126. */
  127. # userspace resumption stub bypassing syscall exit tracing
  128. ALIGN
  129. ret_from_exception:
  130. preempt_stop
  131. ret_from_intr:
  132. GET_THREAD_INFO(%ebp)
  133. movl EFLAGS(%esp), %eax # mix EFLAGS and CS
  134. movb CS(%esp), %al
  135. testl $(VM_MASK | 3), %eax
  136. jz resume_kernel
  137. ENTRY(resume_userspace)
  138. cli # make sure we don't miss an interrupt
  139. # setting need_resched or sigpending
  140. # between sampling and the iret
  141. movl TI_flags(%ebp), %ecx
  142. andl $_TIF_WORK_MASK, %ecx # is there any work to be done on
  143. # int/exception return?
  144. jne work_pending
  145. jmp restore_all
  146. #ifdef CONFIG_PREEMPT
  147. ENTRY(resume_kernel)
  148. cli
  149. cmpl $0,TI_preempt_count(%ebp) # non-zero preempt_count ?
  150. jnz restore_nocheck
  151. need_resched:
  152. movl TI_flags(%ebp), %ecx # need_resched set ?
  153. testb $_TIF_NEED_RESCHED, %cl
  154. jz restore_all
  155. testl $IF_MASK,EFLAGS(%esp) # interrupts off (exception path) ?
  156. jz restore_all
  157. call preempt_schedule_irq
  158. jmp need_resched
  159. #endif
  160. /* SYSENTER_RETURN points to after the "sysenter" instruction in
  161. the vsyscall page. See vsyscall-sysentry.S, which defines the symbol. */
  162. # sysenter call handler stub
  163. ENTRY(sysenter_entry)
  164. movl TSS_sysenter_esp0(%esp),%esp
  165. sysenter_past_esp:
  166. sti
  167. pushl $(__USER_DS)
  168. pushl %ebp
  169. pushfl
  170. pushl $(__USER_CS)
  171. pushl $SYSENTER_RETURN
  172. /*
  173. * Load the potential sixth argument from user stack.
  174. * Careful about security.
  175. */
  176. cmpl $__PAGE_OFFSET-3,%ebp
  177. jae syscall_fault
  178. 1: movl (%ebp),%ebp
  179. .section __ex_table,"a"
  180. .align 4
  181. .long 1b,syscall_fault
  182. .previous
  183. pushl %eax
  184. SAVE_ALL
  185. GET_THREAD_INFO(%ebp)
  186. /* Note, _TIF_SECCOMP is bit number 8, and so it needs testw and not testb */
  187. testw $(_TIF_SYSCALL_EMU|_TIF_SYSCALL_TRACE|_TIF_SECCOMP|_TIF_SYSCALL_AUDIT),TI_flags(%ebp)
  188. jnz syscall_trace_entry
  189. cmpl $(nr_syscalls), %eax
  190. jae syscall_badsys
  191. call *sys_call_table(,%eax,4)
  192. movl %eax,EAX(%esp)
  193. cli
  194. movl TI_flags(%ebp), %ecx
  195. testw $_TIF_ALLWORK_MASK, %cx
  196. jne syscall_exit_work
  197. /* if something modifies registers it must also disable sysexit */
  198. movl EIP(%esp), %edx
  199. movl OLDESP(%esp), %ecx
  200. xorl %ebp,%ebp
  201. sti
  202. sysexit
  203. # system call handler stub
  204. ENTRY(system_call)
  205. pushl %eax # save orig_eax
  206. SAVE_ALL
  207. GET_THREAD_INFO(%ebp)
  208. testl $TF_MASK,EFLAGS(%esp)
  209. jz no_singlestep
  210. orl $_TIF_SINGLESTEP,TI_flags(%ebp)
  211. no_singlestep:
  212. # system call tracing in operation / emulation
  213. /* Note, _TIF_SECCOMP is bit number 8, and so it needs testw and not testb */
  214. testw $(_TIF_SYSCALL_EMU|_TIF_SYSCALL_TRACE|_TIF_SECCOMP|_TIF_SYSCALL_AUDIT),TI_flags(%ebp)
  215. jnz syscall_trace_entry
  216. cmpl $(nr_syscalls), %eax
  217. jae syscall_badsys
  218. syscall_call:
  219. call *sys_call_table(,%eax,4)
  220. movl %eax,EAX(%esp) # store the return value
  221. syscall_exit:
  222. cli # make sure we don't miss an interrupt
  223. # setting need_resched or sigpending
  224. # between sampling and the iret
  225. movl TI_flags(%ebp), %ecx
  226. testw $_TIF_ALLWORK_MASK, %cx # current->work
  227. jne syscall_exit_work
  228. restore_all:
  229. movl EFLAGS(%esp), %eax # mix EFLAGS, SS and CS
  230. # Warning: OLDSS(%esp) contains the wrong/random values if we
  231. # are returning to the kernel.
  232. # See comments in process.c:copy_thread() for details.
  233. movb OLDSS(%esp), %ah
  234. movb CS(%esp), %al
  235. andl $(VM_MASK | (4 << 8) | 3), %eax
  236. cmpl $((4 << 8) | 3), %eax
  237. je ldt_ss # returning to user-space with LDT SS
  238. restore_nocheck:
  239. RESTORE_REGS
  240. addl $4, %esp
  241. 1: iret
  242. .section .fixup,"ax"
  243. iret_exc:
  244. sti
  245. pushl $0 # no error code
  246. pushl $do_iret_error
  247. jmp error_code
  248. .previous
  249. .section __ex_table,"a"
  250. .align 4
  251. .long 1b,iret_exc
  252. .previous
  253. ldt_ss:
  254. larl OLDSS(%esp), %eax
  255. jnz restore_nocheck
  256. testl $0x00400000, %eax # returning to 32bit stack?
  257. jnz restore_nocheck # allright, normal return
  258. /* If returning to userspace with 16bit stack,
  259. * try to fix the higher word of ESP, as the CPU
  260. * won't restore it.
  261. * This is an "official" bug of all the x86-compatible
  262. * CPUs, which we can try to work around to make
  263. * dosemu and wine happy. */
  264. subl $8, %esp # reserve space for switch16 pointer
  265. cli
  266. movl %esp, %eax
  267. /* Set up the 16bit stack frame with switch32 pointer on top,
  268. * and a switch16 pointer on top of the current frame. */
  269. call setup_x86_bogus_stack
  270. RESTORE_REGS
  271. lss 20+4(%esp), %esp # switch to 16bit stack
  272. 1: iret
  273. .section __ex_table,"a"
  274. .align 4
  275. .long 1b,iret_exc
  276. .previous
  277. # perform work that needs to be done immediately before resumption
  278. ALIGN
  279. work_pending:
  280. testb $_TIF_NEED_RESCHED, %cl
  281. jz work_notifysig
  282. work_resched:
  283. call schedule
  284. cli # make sure we don't miss an interrupt
  285. # setting need_resched or sigpending
  286. # between sampling and the iret
  287. movl TI_flags(%ebp), %ecx
  288. andl $_TIF_WORK_MASK, %ecx # is there any work to be done other
  289. # than syscall tracing?
  290. jz restore_all
  291. testb $_TIF_NEED_RESCHED, %cl
  292. jnz work_resched
  293. work_notifysig: # deal with pending signals and
  294. # notify-resume requests
  295. testl $VM_MASK, EFLAGS(%esp)
  296. movl %esp, %eax
  297. jne work_notifysig_v86 # returning to kernel-space or
  298. # vm86-space
  299. xorl %edx, %edx
  300. call do_notify_resume
  301. jmp resume_userspace
  302. ALIGN
  303. work_notifysig_v86:
  304. #ifdef CONFIG_VM86
  305. pushl %ecx # save ti_flags for do_notify_resume
  306. call save_v86_state # %eax contains pt_regs pointer
  307. popl %ecx
  308. movl %eax, %esp
  309. xorl %edx, %edx
  310. call do_notify_resume
  311. jmp resume_userspace
  312. #endif
  313. # perform syscall exit tracing
  314. ALIGN
  315. syscall_trace_entry:
  316. movl $-ENOSYS,EAX(%esp)
  317. movl %esp, %eax
  318. xorl %edx,%edx
  319. call do_syscall_trace
  320. cmpl $0, %eax
  321. jne resume_userspace # ret != 0 -> running under PTRACE_SYSEMU,
  322. # so must skip actual syscall
  323. movl ORIG_EAX(%esp), %eax
  324. cmpl $(nr_syscalls), %eax
  325. jnae syscall_call
  326. jmp syscall_exit
  327. # perform syscall exit tracing
  328. ALIGN
  329. syscall_exit_work:
  330. testb $(_TIF_SYSCALL_TRACE|_TIF_SYSCALL_AUDIT|_TIF_SINGLESTEP), %cl
  331. jz work_pending
  332. sti # could let do_syscall_trace() call
  333. # schedule() instead
  334. movl %esp, %eax
  335. movl $1, %edx
  336. call do_syscall_trace
  337. jmp resume_userspace
  338. ALIGN
  339. syscall_fault:
  340. pushl %eax # save orig_eax
  341. SAVE_ALL
  342. GET_THREAD_INFO(%ebp)
  343. movl $-EFAULT,EAX(%esp)
  344. jmp resume_userspace
  345. ALIGN
  346. syscall_badsys:
  347. movl $-ENOSYS,EAX(%esp)
  348. jmp resume_userspace
  349. #define FIXUP_ESPFIX_STACK \
  350. movl %esp, %eax; \
  351. /* switch to 32bit stack using the pointer on top of 16bit stack */ \
  352. lss %ss:CPU_16BIT_STACK_SIZE-8, %esp; \
  353. /* copy data from 16bit stack to 32bit stack */ \
  354. call fixup_x86_bogus_stack; \
  355. /* put ESP to the proper location */ \
  356. movl %eax, %esp;
  357. #define UNWIND_ESPFIX_STACK \
  358. pushl %eax; \
  359. movl %ss, %eax; \
  360. /* see if on 16bit stack */ \
  361. cmpw $__ESPFIX_SS, %ax; \
  362. jne 28f; \
  363. movl $__KERNEL_DS, %edx; \
  364. movl %edx, %ds; \
  365. movl %edx, %es; \
  366. /* switch to 32bit stack */ \
  367. FIXUP_ESPFIX_STACK \
  368. 28: popl %eax;
  369. /*
  370. * Build the entry stubs and pointer table with
  371. * some assembler magic.
  372. */
  373. .data
  374. ENTRY(interrupt)
  375. .text
  376. vector=0
  377. ENTRY(irq_entries_start)
  378. .rept NR_IRQS
  379. ALIGN
  380. 1: pushl $vector-256
  381. jmp common_interrupt
  382. .data
  383. .long 1b
  384. .text
  385. vector=vector+1
  386. .endr
  387. ALIGN
  388. common_interrupt:
  389. SAVE_ALL
  390. movl %esp,%eax
  391. call do_IRQ
  392. jmp ret_from_intr
  393. #define BUILD_INTERRUPT(name, nr) \
  394. ENTRY(name) \
  395. pushl $nr-256; \
  396. SAVE_ALL \
  397. movl %esp,%eax; \
  398. call smp_/**/name; \
  399. jmp ret_from_intr;
  400. /* The include is where all of the SMP etc. interrupts come from */
  401. #include "entry_arch.h"
  402. ENTRY(divide_error)
  403. pushl $0 # no error code
  404. pushl $do_divide_error
  405. ALIGN
  406. error_code:
  407. pushl %ds
  408. pushl %eax
  409. xorl %eax, %eax
  410. pushl %ebp
  411. pushl %edi
  412. pushl %esi
  413. pushl %edx
  414. decl %eax # eax = -1
  415. pushl %ecx
  416. pushl %ebx
  417. cld
  418. pushl %es
  419. UNWIND_ESPFIX_STACK
  420. popl %ecx
  421. movl ES(%esp), %edi # get the function address
  422. movl ORIG_EAX(%esp), %edx # get the error code
  423. movl %eax, ORIG_EAX(%esp)
  424. movl %ecx, ES(%esp)
  425. movl $(__USER_DS), %ecx
  426. movl %ecx, %ds
  427. movl %ecx, %es
  428. movl %esp,%eax # pt_regs pointer
  429. call *%edi
  430. jmp ret_from_exception
  431. ENTRY(coprocessor_error)
  432. pushl $0
  433. pushl $do_coprocessor_error
  434. jmp error_code
  435. ENTRY(simd_coprocessor_error)
  436. pushl $0
  437. pushl $do_simd_coprocessor_error
  438. jmp error_code
  439. ENTRY(device_not_available)
  440. pushl $-1 # mark this as an int
  441. SAVE_ALL
  442. movl %cr0, %eax
  443. testl $0x4, %eax # EM (math emulation bit)
  444. jne device_not_available_emulate
  445. preempt_stop
  446. call math_state_restore
  447. jmp ret_from_exception
  448. device_not_available_emulate:
  449. pushl $0 # temporary storage for ORIG_EIP
  450. call math_emulate
  451. addl $4, %esp
  452. jmp ret_from_exception
  453. /*
  454. * Debug traps and NMI can happen at the one SYSENTER instruction
  455. * that sets up the real kernel stack. Check here, since we can't
  456. * allow the wrong stack to be used.
  457. *
  458. * "TSS_sysenter_esp0+12" is because the NMI/debug handler will have
  459. * already pushed 3 words if it hits on the sysenter instruction:
  460. * eflags, cs and eip.
  461. *
  462. * We just load the right stack, and push the three (known) values
  463. * by hand onto the new stack - while updating the return eip past
  464. * the instruction that would have done it for sysenter.
  465. */
  466. #define FIX_STACK(offset, ok, label) \
  467. cmpw $__KERNEL_CS,4(%esp); \
  468. jne ok; \
  469. label: \
  470. movl TSS_sysenter_esp0+offset(%esp),%esp; \
  471. pushfl; \
  472. pushl $__KERNEL_CS; \
  473. pushl $sysenter_past_esp
  474. KPROBE_ENTRY(debug)
  475. cmpl $sysenter_entry,(%esp)
  476. jne debug_stack_correct
  477. FIX_STACK(12, debug_stack_correct, debug_esp_fix_insn)
  478. debug_stack_correct:
  479. pushl $-1 # mark this as an int
  480. SAVE_ALL
  481. xorl %edx,%edx # error code 0
  482. movl %esp,%eax # pt_regs pointer
  483. call do_debug
  484. jmp ret_from_exception
  485. .previous .text
  486. /*
  487. * NMI is doubly nasty. It can happen _while_ we're handling
  488. * a debug fault, and the debug fault hasn't yet been able to
  489. * clear up the stack. So we first check whether we got an
  490. * NMI on the sysenter entry path, but after that we need to
  491. * check whether we got an NMI on the debug path where the debug
  492. * fault happened on the sysenter path.
  493. */
  494. ENTRY(nmi)
  495. pushl %eax
  496. movl %ss, %eax
  497. cmpw $__ESPFIX_SS, %ax
  498. popl %eax
  499. je nmi_16bit_stack
  500. cmpl $sysenter_entry,(%esp)
  501. je nmi_stack_fixup
  502. pushl %eax
  503. movl %esp,%eax
  504. /* Do not access memory above the end of our stack page,
  505. * it might not exist.
  506. */
  507. andl $(THREAD_SIZE-1),%eax
  508. cmpl $(THREAD_SIZE-20),%eax
  509. popl %eax
  510. jae nmi_stack_correct
  511. cmpl $sysenter_entry,12(%esp)
  512. je nmi_debug_stack_check
  513. nmi_stack_correct:
  514. pushl %eax
  515. SAVE_ALL
  516. xorl %edx,%edx # zero error code
  517. movl %esp,%eax # pt_regs pointer
  518. call do_nmi
  519. jmp restore_all
  520. nmi_stack_fixup:
  521. FIX_STACK(12,nmi_stack_correct, 1)
  522. jmp nmi_stack_correct
  523. nmi_debug_stack_check:
  524. cmpw $__KERNEL_CS,16(%esp)
  525. jne nmi_stack_correct
  526. cmpl $debug,(%esp)
  527. jb nmi_stack_correct
  528. cmpl $debug_esp_fix_insn,(%esp)
  529. ja nmi_stack_correct
  530. FIX_STACK(24,nmi_stack_correct, 1)
  531. jmp nmi_stack_correct
  532. nmi_16bit_stack:
  533. /* create the pointer to lss back */
  534. pushl %ss
  535. pushl %esp
  536. movzwl %sp, %esp
  537. addw $4, (%esp)
  538. /* copy the iret frame of 12 bytes */
  539. .rept 3
  540. pushl 16(%esp)
  541. .endr
  542. pushl %eax
  543. SAVE_ALL
  544. FIXUP_ESPFIX_STACK # %eax == %esp
  545. xorl %edx,%edx # zero error code
  546. call do_nmi
  547. RESTORE_REGS
  548. lss 12+4(%esp), %esp # back to 16bit stack
  549. 1: iret
  550. .section __ex_table,"a"
  551. .align 4
  552. .long 1b,iret_exc
  553. .previous
  554. KPROBE_ENTRY(int3)
  555. pushl $-1 # mark this as an int
  556. SAVE_ALL
  557. xorl %edx,%edx # zero error code
  558. movl %esp,%eax # pt_regs pointer
  559. call do_int3
  560. jmp ret_from_exception
  561. .previous .text
  562. ENTRY(overflow)
  563. pushl $0
  564. pushl $do_overflow
  565. jmp error_code
  566. ENTRY(bounds)
  567. pushl $0
  568. pushl $do_bounds
  569. jmp error_code
  570. ENTRY(invalid_op)
  571. pushl $0
  572. pushl $do_invalid_op
  573. jmp error_code
  574. ENTRY(coprocessor_segment_overrun)
  575. pushl $0
  576. pushl $do_coprocessor_segment_overrun
  577. jmp error_code
  578. ENTRY(invalid_TSS)
  579. pushl $do_invalid_TSS
  580. jmp error_code
  581. ENTRY(segment_not_present)
  582. pushl $do_segment_not_present
  583. jmp error_code
  584. ENTRY(stack_segment)
  585. pushl $do_stack_segment
  586. jmp error_code
  587. KPROBE_ENTRY(general_protection)
  588. pushl $do_general_protection
  589. jmp error_code
  590. .previous .text
  591. ENTRY(alignment_check)
  592. pushl $do_alignment_check
  593. jmp error_code
  594. KPROBE_ENTRY(page_fault)
  595. pushl $do_page_fault
  596. jmp error_code
  597. .previous .text
  598. #ifdef CONFIG_X86_MCE
  599. ENTRY(machine_check)
  600. pushl $0
  601. pushl machine_check_vector
  602. jmp error_code
  603. #endif
  604. ENTRY(spurious_interrupt_bug)
  605. pushl $0
  606. pushl $do_spurious_interrupt_bug
  607. jmp error_code
  608. .section .rodata,"a"
  609. #include "syscall_table.S"
  610. syscall_table_size=(.-sys_call_table)