entry.S 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665
  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. # system call tracing in operation / emulation
  209. /* Note, _TIF_SECCOMP is bit number 8, and so it needs testw and not testb */
  210. testw $(_TIF_SYSCALL_EMU|_TIF_SYSCALL_TRACE|_TIF_SECCOMP|_TIF_SYSCALL_AUDIT),TI_flags(%ebp)
  211. jnz syscall_trace_entry
  212. cmpl $(nr_syscalls), %eax
  213. jae syscall_badsys
  214. syscall_call:
  215. call *sys_call_table(,%eax,4)
  216. movl %eax,EAX(%esp) # store the return value
  217. syscall_exit:
  218. cli # make sure we don't miss an interrupt
  219. # setting need_resched or sigpending
  220. # between sampling and the iret
  221. movl TI_flags(%ebp), %ecx
  222. testw $_TIF_ALLWORK_MASK, %cx # current->work
  223. jne syscall_exit_work
  224. restore_all:
  225. movl EFLAGS(%esp), %eax # mix EFLAGS, SS and CS
  226. # Warning: OLDSS(%esp) contains the wrong/random values if we
  227. # are returning to the kernel.
  228. # See comments in process.c:copy_thread() for details.
  229. movb OLDSS(%esp), %ah
  230. movb CS(%esp), %al
  231. andl $(VM_MASK | (4 << 8) | 3), %eax
  232. cmpl $((4 << 8) | 3), %eax
  233. je ldt_ss # returning to user-space with LDT SS
  234. restore_nocheck:
  235. RESTORE_REGS
  236. addl $4, %esp
  237. 1: iret
  238. .section .fixup,"ax"
  239. iret_exc:
  240. sti
  241. pushl $0 # no error code
  242. pushl $do_iret_error
  243. jmp error_code
  244. .previous
  245. .section __ex_table,"a"
  246. .align 4
  247. .long 1b,iret_exc
  248. .previous
  249. ldt_ss:
  250. larl OLDSS(%esp), %eax
  251. jnz restore_nocheck
  252. testl $0x00400000, %eax # returning to 32bit stack?
  253. jnz restore_nocheck # allright, normal return
  254. /* If returning to userspace with 16bit stack,
  255. * try to fix the higher word of ESP, as the CPU
  256. * won't restore it.
  257. * This is an "official" bug of all the x86-compatible
  258. * CPUs, which we can try to work around to make
  259. * dosemu and wine happy. */
  260. subl $8, %esp # reserve space for switch16 pointer
  261. cli
  262. movl %esp, %eax
  263. /* Set up the 16bit stack frame with switch32 pointer on top,
  264. * and a switch16 pointer on top of the current frame. */
  265. call setup_x86_bogus_stack
  266. RESTORE_REGS
  267. lss 20+4(%esp), %esp # switch to 16bit stack
  268. 1: iret
  269. .section __ex_table,"a"
  270. .align 4
  271. .long 1b,iret_exc
  272. .previous
  273. # perform work that needs to be done immediately before resumption
  274. ALIGN
  275. work_pending:
  276. testb $_TIF_NEED_RESCHED, %cl
  277. jz work_notifysig
  278. work_resched:
  279. call schedule
  280. cli # make sure we don't miss an interrupt
  281. # setting need_resched or sigpending
  282. # between sampling and the iret
  283. movl TI_flags(%ebp), %ecx
  284. andl $_TIF_WORK_MASK, %ecx # is there any work to be done other
  285. # than syscall tracing?
  286. jz restore_all
  287. testb $_TIF_NEED_RESCHED, %cl
  288. jnz work_resched
  289. work_notifysig: # deal with pending signals and
  290. # notify-resume requests
  291. testl $VM_MASK, EFLAGS(%esp)
  292. movl %esp, %eax
  293. jne work_notifysig_v86 # returning to kernel-space or
  294. # vm86-space
  295. xorl %edx, %edx
  296. call do_notify_resume
  297. jmp resume_userspace
  298. ALIGN
  299. work_notifysig_v86:
  300. #ifdef CONFIG_VM86
  301. pushl %ecx # save ti_flags for do_notify_resume
  302. call save_v86_state # %eax contains pt_regs pointer
  303. popl %ecx
  304. movl %eax, %esp
  305. xorl %edx, %edx
  306. call do_notify_resume
  307. jmp resume_userspace
  308. #endif
  309. # perform syscall exit tracing
  310. ALIGN
  311. syscall_trace_entry:
  312. movl $-ENOSYS,EAX(%esp)
  313. movl %esp, %eax
  314. xorl %edx,%edx
  315. call do_syscall_trace
  316. cmpl $0, %eax
  317. jne resume_userspace # ret != 0 -> running under PTRACE_SYSEMU,
  318. # so must skip actual syscall
  319. movl ORIG_EAX(%esp), %eax
  320. cmpl $(nr_syscalls), %eax
  321. jnae syscall_call
  322. jmp syscall_exit
  323. # perform syscall exit tracing
  324. ALIGN
  325. syscall_exit_work:
  326. testb $(_TIF_SYSCALL_TRACE|_TIF_SYSCALL_AUDIT|_TIF_SINGLESTEP), %cl
  327. jz work_pending
  328. sti # could let do_syscall_trace() call
  329. # schedule() instead
  330. movl %esp, %eax
  331. movl $1, %edx
  332. call do_syscall_trace
  333. jmp resume_userspace
  334. ALIGN
  335. syscall_fault:
  336. pushl %eax # save orig_eax
  337. SAVE_ALL
  338. GET_THREAD_INFO(%ebp)
  339. movl $-EFAULT,EAX(%esp)
  340. jmp resume_userspace
  341. ALIGN
  342. syscall_badsys:
  343. movl $-ENOSYS,EAX(%esp)
  344. jmp resume_userspace
  345. #define FIXUP_ESPFIX_STACK \
  346. movl %esp, %eax; \
  347. /* switch to 32bit stack using the pointer on top of 16bit stack */ \
  348. lss %ss:CPU_16BIT_STACK_SIZE-8, %esp; \
  349. /* copy data from 16bit stack to 32bit stack */ \
  350. call fixup_x86_bogus_stack; \
  351. /* put ESP to the proper location */ \
  352. movl %eax, %esp;
  353. #define UNWIND_ESPFIX_STACK \
  354. pushl %eax; \
  355. movl %ss, %eax; \
  356. /* see if on 16bit stack */ \
  357. cmpw $__ESPFIX_SS, %ax; \
  358. jne 28f; \
  359. movl $__KERNEL_DS, %edx; \
  360. movl %edx, %ds; \
  361. movl %edx, %es; \
  362. /* switch to 32bit stack */ \
  363. FIXUP_ESPFIX_STACK \
  364. 28: popl %eax;
  365. /*
  366. * Build the entry stubs and pointer table with
  367. * some assembler magic.
  368. */
  369. .data
  370. ENTRY(interrupt)
  371. .text
  372. vector=0
  373. ENTRY(irq_entries_start)
  374. .rept NR_IRQS
  375. ALIGN
  376. 1: pushl $vector-256
  377. jmp common_interrupt
  378. .data
  379. .long 1b
  380. .text
  381. vector=vector+1
  382. .endr
  383. ALIGN
  384. common_interrupt:
  385. SAVE_ALL
  386. movl %esp,%eax
  387. call do_IRQ
  388. jmp ret_from_intr
  389. #define BUILD_INTERRUPT(name, nr) \
  390. ENTRY(name) \
  391. pushl $nr-256; \
  392. SAVE_ALL \
  393. movl %esp,%eax; \
  394. call smp_/**/name; \
  395. jmp ret_from_intr;
  396. /* The include is where all of the SMP etc. interrupts come from */
  397. #include "entry_arch.h"
  398. ENTRY(divide_error)
  399. pushl $0 # no error code
  400. pushl $do_divide_error
  401. ALIGN
  402. error_code:
  403. pushl %ds
  404. pushl %eax
  405. xorl %eax, %eax
  406. pushl %ebp
  407. pushl %edi
  408. pushl %esi
  409. pushl %edx
  410. decl %eax # eax = -1
  411. pushl %ecx
  412. pushl %ebx
  413. cld
  414. pushl %es
  415. UNWIND_ESPFIX_STACK
  416. popl %ecx
  417. movl ES(%esp), %edi # get the function address
  418. movl ORIG_EAX(%esp), %edx # get the error code
  419. movl %eax, ORIG_EAX(%esp)
  420. movl %ecx, ES(%esp)
  421. movl $(__USER_DS), %ecx
  422. movl %ecx, %ds
  423. movl %ecx, %es
  424. movl %esp,%eax # pt_regs pointer
  425. call *%edi
  426. jmp ret_from_exception
  427. ENTRY(coprocessor_error)
  428. pushl $0
  429. pushl $do_coprocessor_error
  430. jmp error_code
  431. ENTRY(simd_coprocessor_error)
  432. pushl $0
  433. pushl $do_simd_coprocessor_error
  434. jmp error_code
  435. ENTRY(device_not_available)
  436. pushl $-1 # mark this as an int
  437. SAVE_ALL
  438. movl %cr0, %eax
  439. testl $0x4, %eax # EM (math emulation bit)
  440. jne device_not_available_emulate
  441. preempt_stop
  442. call math_state_restore
  443. jmp ret_from_exception
  444. device_not_available_emulate:
  445. pushl $0 # temporary storage for ORIG_EIP
  446. call math_emulate
  447. addl $4, %esp
  448. jmp ret_from_exception
  449. /*
  450. * Debug traps and NMI can happen at the one SYSENTER instruction
  451. * that sets up the real kernel stack. Check here, since we can't
  452. * allow the wrong stack to be used.
  453. *
  454. * "TSS_sysenter_esp0+12" is because the NMI/debug handler will have
  455. * already pushed 3 words if it hits on the sysenter instruction:
  456. * eflags, cs and eip.
  457. *
  458. * We just load the right stack, and push the three (known) values
  459. * by hand onto the new stack - while updating the return eip past
  460. * the instruction that would have done it for sysenter.
  461. */
  462. #define FIX_STACK(offset, ok, label) \
  463. cmpw $__KERNEL_CS,4(%esp); \
  464. jne ok; \
  465. label: \
  466. movl TSS_sysenter_esp0+offset(%esp),%esp; \
  467. pushfl; \
  468. pushl $__KERNEL_CS; \
  469. pushl $sysenter_past_esp
  470. KPROBE_ENTRY(debug)
  471. cmpl $sysenter_entry,(%esp)
  472. jne debug_stack_correct
  473. FIX_STACK(12, debug_stack_correct, debug_esp_fix_insn)
  474. debug_stack_correct:
  475. pushl $-1 # mark this as an int
  476. SAVE_ALL
  477. xorl %edx,%edx # error code 0
  478. movl %esp,%eax # pt_regs pointer
  479. call do_debug
  480. jmp ret_from_exception
  481. .previous .text
  482. /*
  483. * NMI is doubly nasty. It can happen _while_ we're handling
  484. * a debug fault, and the debug fault hasn't yet been able to
  485. * clear up the stack. So we first check whether we got an
  486. * NMI on the sysenter entry path, but after that we need to
  487. * check whether we got an NMI on the debug path where the debug
  488. * fault happened on the sysenter path.
  489. */
  490. ENTRY(nmi)
  491. pushl %eax
  492. movl %ss, %eax
  493. cmpw $__ESPFIX_SS, %ax
  494. popl %eax
  495. je nmi_16bit_stack
  496. cmpl $sysenter_entry,(%esp)
  497. je nmi_stack_fixup
  498. pushl %eax
  499. movl %esp,%eax
  500. /* Do not access memory above the end of our stack page,
  501. * it might not exist.
  502. */
  503. andl $(THREAD_SIZE-1),%eax
  504. cmpl $(THREAD_SIZE-20),%eax
  505. popl %eax
  506. jae nmi_stack_correct
  507. cmpl $sysenter_entry,12(%esp)
  508. je nmi_debug_stack_check
  509. nmi_stack_correct:
  510. pushl %eax
  511. SAVE_ALL
  512. xorl %edx,%edx # zero error code
  513. movl %esp,%eax # pt_regs pointer
  514. call do_nmi
  515. jmp restore_all
  516. nmi_stack_fixup:
  517. FIX_STACK(12,nmi_stack_correct, 1)
  518. jmp nmi_stack_correct
  519. nmi_debug_stack_check:
  520. cmpw $__KERNEL_CS,16(%esp)
  521. jne nmi_stack_correct
  522. cmpl $debug,(%esp)
  523. jb nmi_stack_correct
  524. cmpl $debug_esp_fix_insn,(%esp)
  525. ja nmi_stack_correct
  526. FIX_STACK(24,nmi_stack_correct, 1)
  527. jmp nmi_stack_correct
  528. nmi_16bit_stack:
  529. /* create the pointer to lss back */
  530. pushl %ss
  531. pushl %esp
  532. movzwl %sp, %esp
  533. addw $4, (%esp)
  534. /* copy the iret frame of 12 bytes */
  535. .rept 3
  536. pushl 16(%esp)
  537. .endr
  538. pushl %eax
  539. SAVE_ALL
  540. FIXUP_ESPFIX_STACK # %eax == %esp
  541. xorl %edx,%edx # zero error code
  542. call do_nmi
  543. RESTORE_REGS
  544. lss 12+4(%esp), %esp # back to 16bit stack
  545. 1: iret
  546. .section __ex_table,"a"
  547. .align 4
  548. .long 1b,iret_exc
  549. .previous
  550. KPROBE_ENTRY(int3)
  551. pushl $-1 # mark this as an int
  552. SAVE_ALL
  553. xorl %edx,%edx # zero error code
  554. movl %esp,%eax # pt_regs pointer
  555. call do_int3
  556. jmp ret_from_exception
  557. .previous .text
  558. ENTRY(overflow)
  559. pushl $0
  560. pushl $do_overflow
  561. jmp error_code
  562. ENTRY(bounds)
  563. pushl $0
  564. pushl $do_bounds
  565. jmp error_code
  566. ENTRY(invalid_op)
  567. pushl $0
  568. pushl $do_invalid_op
  569. jmp error_code
  570. ENTRY(coprocessor_segment_overrun)
  571. pushl $0
  572. pushl $do_coprocessor_segment_overrun
  573. jmp error_code
  574. ENTRY(invalid_TSS)
  575. pushl $do_invalid_TSS
  576. jmp error_code
  577. ENTRY(segment_not_present)
  578. pushl $do_segment_not_present
  579. jmp error_code
  580. ENTRY(stack_segment)
  581. pushl $do_stack_segment
  582. jmp error_code
  583. KPROBE_ENTRY(general_protection)
  584. pushl $do_general_protection
  585. jmp error_code
  586. .previous .text
  587. ENTRY(alignment_check)
  588. pushl $do_alignment_check
  589. jmp error_code
  590. KPROBE_ENTRY(page_fault)
  591. pushl $do_page_fault
  592. jmp error_code
  593. .previous .text
  594. #ifdef CONFIG_X86_MCE
  595. ENTRY(machine_check)
  596. pushl $0
  597. pushl machine_check_vector
  598. jmp error_code
  599. #endif
  600. ENTRY(spurious_interrupt_bug)
  601. pushl $0
  602. pushl $do_spurious_interrupt_bug
  603. jmp error_code
  604. .section .rodata,"a"
  605. #include "syscall_table.S"
  606. syscall_table_size=(.-sys_call_table)