entry.S 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663
  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 restore_all
  298. ALIGN
  299. work_notifysig_v86:
  300. pushl %ecx # save ti_flags for do_notify_resume
  301. call save_v86_state # %eax contains pt_regs pointer
  302. popl %ecx
  303. movl %eax, %esp
  304. xorl %edx, %edx
  305. call do_notify_resume
  306. jmp restore_all
  307. # perform syscall exit tracing
  308. ALIGN
  309. syscall_trace_entry:
  310. movl $-ENOSYS,EAX(%esp)
  311. movl %esp, %eax
  312. xorl %edx,%edx
  313. call do_syscall_trace
  314. cmpl $0, %eax
  315. jne resume_userspace # ret != 0 -> running under PTRACE_SYSEMU,
  316. # so must skip actual syscall
  317. movl ORIG_EAX(%esp), %eax
  318. cmpl $(nr_syscalls), %eax
  319. jnae syscall_call
  320. jmp syscall_exit
  321. # perform syscall exit tracing
  322. ALIGN
  323. syscall_exit_work:
  324. testb $(_TIF_SYSCALL_TRACE|_TIF_SYSCALL_AUDIT|_TIF_SINGLESTEP), %cl
  325. jz work_pending
  326. sti # could let do_syscall_trace() call
  327. # schedule() instead
  328. movl %esp, %eax
  329. movl $1, %edx
  330. call do_syscall_trace
  331. jmp resume_userspace
  332. ALIGN
  333. syscall_fault:
  334. pushl %eax # save orig_eax
  335. SAVE_ALL
  336. GET_THREAD_INFO(%ebp)
  337. movl $-EFAULT,EAX(%esp)
  338. jmp resume_userspace
  339. ALIGN
  340. syscall_badsys:
  341. movl $-ENOSYS,EAX(%esp)
  342. jmp resume_userspace
  343. #define FIXUP_ESPFIX_STACK \
  344. movl %esp, %eax; \
  345. /* switch to 32bit stack using the pointer on top of 16bit stack */ \
  346. lss %ss:CPU_16BIT_STACK_SIZE-8, %esp; \
  347. /* copy data from 16bit stack to 32bit stack */ \
  348. call fixup_x86_bogus_stack; \
  349. /* put ESP to the proper location */ \
  350. movl %eax, %esp;
  351. #define UNWIND_ESPFIX_STACK \
  352. pushl %eax; \
  353. movl %ss, %eax; \
  354. /* see if on 16bit stack */ \
  355. cmpw $__ESPFIX_SS, %ax; \
  356. jne 28f; \
  357. movl $__KERNEL_DS, %edx; \
  358. movl %edx, %ds; \
  359. movl %edx, %es; \
  360. /* switch to 32bit stack */ \
  361. FIXUP_ESPFIX_STACK \
  362. 28: popl %eax;
  363. /*
  364. * Build the entry stubs and pointer table with
  365. * some assembler magic.
  366. */
  367. .data
  368. ENTRY(interrupt)
  369. .text
  370. vector=0
  371. ENTRY(irq_entries_start)
  372. .rept NR_IRQS
  373. ALIGN
  374. 1: pushl $vector-256
  375. jmp common_interrupt
  376. .data
  377. .long 1b
  378. .text
  379. vector=vector+1
  380. .endr
  381. ALIGN
  382. common_interrupt:
  383. SAVE_ALL
  384. movl %esp,%eax
  385. call do_IRQ
  386. jmp ret_from_intr
  387. #define BUILD_INTERRUPT(name, nr) \
  388. ENTRY(name) \
  389. pushl $nr-256; \
  390. SAVE_ALL \
  391. movl %esp,%eax; \
  392. call smp_/**/name; \
  393. jmp ret_from_intr;
  394. /* The include is where all of the SMP etc. interrupts come from */
  395. #include "entry_arch.h"
  396. ENTRY(divide_error)
  397. pushl $0 # no error code
  398. pushl $do_divide_error
  399. ALIGN
  400. error_code:
  401. pushl %ds
  402. pushl %eax
  403. xorl %eax, %eax
  404. pushl %ebp
  405. pushl %edi
  406. pushl %esi
  407. pushl %edx
  408. decl %eax # eax = -1
  409. pushl %ecx
  410. pushl %ebx
  411. cld
  412. pushl %es
  413. UNWIND_ESPFIX_STACK
  414. popl %ecx
  415. movl ES(%esp), %edi # get the function address
  416. movl ORIG_EAX(%esp), %edx # get the error code
  417. movl %eax, ORIG_EAX(%esp)
  418. movl %ecx, ES(%esp)
  419. movl $(__USER_DS), %ecx
  420. movl %ecx, %ds
  421. movl %ecx, %es
  422. movl %esp,%eax # pt_regs pointer
  423. call *%edi
  424. jmp ret_from_exception
  425. ENTRY(coprocessor_error)
  426. pushl $0
  427. pushl $do_coprocessor_error
  428. jmp error_code
  429. ENTRY(simd_coprocessor_error)
  430. pushl $0
  431. pushl $do_simd_coprocessor_error
  432. jmp error_code
  433. ENTRY(device_not_available)
  434. pushl $-1 # mark this as an int
  435. SAVE_ALL
  436. movl %cr0, %eax
  437. testl $0x4, %eax # EM (math emulation bit)
  438. jne device_not_available_emulate
  439. preempt_stop
  440. call math_state_restore
  441. jmp ret_from_exception
  442. device_not_available_emulate:
  443. pushl $0 # temporary storage for ORIG_EIP
  444. call math_emulate
  445. addl $4, %esp
  446. jmp ret_from_exception
  447. /*
  448. * Debug traps and NMI can happen at the one SYSENTER instruction
  449. * that sets up the real kernel stack. Check here, since we can't
  450. * allow the wrong stack to be used.
  451. *
  452. * "TSS_sysenter_esp0+12" is because the NMI/debug handler will have
  453. * already pushed 3 words if it hits on the sysenter instruction:
  454. * eflags, cs and eip.
  455. *
  456. * We just load the right stack, and push the three (known) values
  457. * by hand onto the new stack - while updating the return eip past
  458. * the instruction that would have done it for sysenter.
  459. */
  460. #define FIX_STACK(offset, ok, label) \
  461. cmpw $__KERNEL_CS,4(%esp); \
  462. jne ok; \
  463. label: \
  464. movl TSS_sysenter_esp0+offset(%esp),%esp; \
  465. pushfl; \
  466. pushl $__KERNEL_CS; \
  467. pushl $sysenter_past_esp
  468. KPROBE_ENTRY(debug)
  469. cmpl $sysenter_entry,(%esp)
  470. jne debug_stack_correct
  471. FIX_STACK(12, debug_stack_correct, debug_esp_fix_insn)
  472. debug_stack_correct:
  473. pushl $-1 # mark this as an int
  474. SAVE_ALL
  475. xorl %edx,%edx # error code 0
  476. movl %esp,%eax # pt_regs pointer
  477. call do_debug
  478. jmp ret_from_exception
  479. .previous .text
  480. /*
  481. * NMI is doubly nasty. It can happen _while_ we're handling
  482. * a debug fault, and the debug fault hasn't yet been able to
  483. * clear up the stack. So we first check whether we got an
  484. * NMI on the sysenter entry path, but after that we need to
  485. * check whether we got an NMI on the debug path where the debug
  486. * fault happened on the sysenter path.
  487. */
  488. ENTRY(nmi)
  489. pushl %eax
  490. movl %ss, %eax
  491. cmpw $__ESPFIX_SS, %ax
  492. popl %eax
  493. je nmi_16bit_stack
  494. cmpl $sysenter_entry,(%esp)
  495. je nmi_stack_fixup
  496. pushl %eax
  497. movl %esp,%eax
  498. /* Do not access memory above the end of our stack page,
  499. * it might not exist.
  500. */
  501. andl $(THREAD_SIZE-1),%eax
  502. cmpl $(THREAD_SIZE-20),%eax
  503. popl %eax
  504. jae nmi_stack_correct
  505. cmpl $sysenter_entry,12(%esp)
  506. je nmi_debug_stack_check
  507. nmi_stack_correct:
  508. pushl %eax
  509. SAVE_ALL
  510. xorl %edx,%edx # zero error code
  511. movl %esp,%eax # pt_regs pointer
  512. call do_nmi
  513. jmp restore_all
  514. nmi_stack_fixup:
  515. FIX_STACK(12,nmi_stack_correct, 1)
  516. jmp nmi_stack_correct
  517. nmi_debug_stack_check:
  518. cmpw $__KERNEL_CS,16(%esp)
  519. jne nmi_stack_correct
  520. cmpl $debug - 1,(%esp)
  521. jle nmi_stack_correct
  522. cmpl $debug_esp_fix_insn,(%esp)
  523. jle nmi_debug_stack_fixup
  524. nmi_debug_stack_fixup:
  525. FIX_STACK(24,nmi_stack_correct, 1)
  526. jmp nmi_stack_correct
  527. nmi_16bit_stack:
  528. /* create the pointer to lss back */
  529. pushl %ss
  530. pushl %esp
  531. movzwl %sp, %esp
  532. addw $4, (%esp)
  533. /* copy the iret frame of 12 bytes */
  534. .rept 3
  535. pushl 16(%esp)
  536. .endr
  537. pushl %eax
  538. SAVE_ALL
  539. FIXUP_ESPFIX_STACK # %eax == %esp
  540. xorl %edx,%edx # zero error code
  541. call do_nmi
  542. RESTORE_REGS
  543. lss 12+4(%esp), %esp # back to 16bit stack
  544. 1: iret
  545. .section __ex_table,"a"
  546. .align 4
  547. .long 1b,iret_exc
  548. .previous
  549. KPROBE_ENTRY(int3)
  550. pushl $-1 # mark this as an int
  551. SAVE_ALL
  552. xorl %edx,%edx # zero error code
  553. movl %esp,%eax # pt_regs pointer
  554. call do_int3
  555. jmp ret_from_exception
  556. .previous .text
  557. ENTRY(overflow)
  558. pushl $0
  559. pushl $do_overflow
  560. jmp error_code
  561. ENTRY(bounds)
  562. pushl $0
  563. pushl $do_bounds
  564. jmp error_code
  565. ENTRY(invalid_op)
  566. pushl $0
  567. pushl $do_invalid_op
  568. jmp error_code
  569. ENTRY(coprocessor_segment_overrun)
  570. pushl $0
  571. pushl $do_coprocessor_segment_overrun
  572. jmp error_code
  573. ENTRY(invalid_TSS)
  574. pushl $do_invalid_TSS
  575. jmp error_code
  576. ENTRY(segment_not_present)
  577. pushl $do_segment_not_present
  578. jmp error_code
  579. ENTRY(stack_segment)
  580. pushl $do_stack_segment
  581. jmp error_code
  582. KPROBE_ENTRY(general_protection)
  583. pushl $do_general_protection
  584. jmp error_code
  585. .previous .text
  586. ENTRY(alignment_check)
  587. pushl $do_alignment_check
  588. jmp error_code
  589. KPROBE_ENTRY(page_fault)
  590. pushl $do_page_fault
  591. jmp error_code
  592. .previous .text
  593. #ifdef CONFIG_X86_MCE
  594. ENTRY(machine_check)
  595. pushl $0
  596. pushl machine_check_vector
  597. jmp error_code
  598. #endif
  599. ENTRY(spurious_interrupt_bug)
  600. pushl $0
  601. pushl $do_spurious_interrupt_bug
  602. jmp error_code
  603. #include "syscall_table.S"
  604. syscall_table_size=(.-sys_call_table)