syscall_stub.S 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. /* int 0x80 syscall stub (ring-3 -> ring-0).
  2. *
  3. * On entry the CPU has pushed SS, ESP, EFLAGS, CS, EIP (from ring-3).
  4. * Registers: eax = syscall number, ebx/ecx/edx = args 1..3.
  5. * The dispatcher's return value is placed in eax before returning to user. */
  6. .code32
  7. .text
  8. .include "irqret.inc"
  9. .globl syscall_stub
  10. syscall_stub:
  11. /* Disable interrupts for the duration of the syscall: the trap gate keeps
  12. * IF=1, so otherwise the PIT could fire and the context switch would switch
  13. * the stack out from under us mid-syscall. Blocking syscalls (sleep/exit)
  14. * re-enable interrupts themselves. */
  15. cli
  16. /* Load the kernel selectors: DS/ES/FS = 0x10, GS = 0x28 (the per-CPU
  17. * data selector used by this_cpu()). int 0x80 only switches CS and SS;
  18. * IRET reset DS (etc.) to null when it returned to ring-3, so DS is null
  19. * here and the C dispatcher's data accesses would #GP. */
  20. pushl %eax
  21. movw $0x10, %ax
  22. movw %ax, %ds
  23. movw %ax, %es
  24. movw %ax, %fs
  25. movw $0x28, %ax
  26. movw %ax, %gs
  27. popl %eax
  28. pusha
  29. pushl %edx
  30. pushl %ecx
  31. pushl %ebx
  32. pushl %eax
  33. call syscall_dispatch
  34. addl $16, %esp
  35. movl %eax, 28(%esp) /* overwrite the saved eax with the return value */
  36. /* Returning to the ring-3 caller: leave it a usable data selector (the
  37. * IRET would null 0x10, and user code #GPs on its first data access).
  38. * See irqret.inc for the why; this gate is ring-3-only. */
  39. IRQ_RETURN_DS 36
  40. popa
  41. iret