switch.S 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*
  2. * Context-switch PIT IRQ stub.
  3. *
  4. * On entry the CPU has already pushed EFLAGS, CS, EIP onto the
  5. * current process's stack. We save the rest, bump the tick counter,
  6. * and -- when the timeslice expires -- call schedule() and swap ESP
  7. * to the next process.
  8. *
  9. * Shadow buffer flush: damage-driven, done by the main loop (byte budget).
  10. * Main loop checks dirty rows and copies only what changed.
  11. *
  12. * Serial poll: every tick, check UART for received data and push
  13. * to ring buffer so no bytes are lost during preemption.
  14. */
  15. .code32
  16. .text
  17. .extern ticks
  18. .extern current_pid
  19. .extern procs
  20. .extern schedule
  21. .extern serial_poll
  22. .extern tss
  23. .include "irqret.inc"
  24. /* struct process offsets (must match process.h / process.c) */
  25. .equ PCB_ESP, 0
  26. .equ PCB_CR3, 32
  27. .equ PCB_KSTACK_TOP, 36
  28. .equ PCB_CPU_TICKS, 108 /* after cwd[64] */
  29. .equ PCB_SIZE, 120 /* sizeof(struct process): 44 + cwd[64] + 2*4 + target */
  30. .equ TIMESLICE, 20
  31. .globl pit_irq_stub
  32. pit_irq_stub:
  33. pusha
  34. /* Load the kernel data segment. The PIT can fire while a ring-3 process
  35. * is running, and in that case the CPU has switched CS/SS but left DS
  36. * null (IRET reset it when it returned to ring-3); the data accesses
  37. * below (ticks, counters, procs, tss) go through DS and would #GP. */
  38. movw $0x10, %ax
  39. movw %ax, %ds
  40. movw %ax, %es
  41. movw %ax, %fs
  42. movw %ax, %gs
  43. /* ticks++ and send EOI -- always, even when we don't switch. */
  44. incl ticks
  45. movb $0x20, %al
  46. outb %al, $0x20
  47. /* Account this tick to the process that was interrupted (for `top`). */
  48. movl current_pid, %eax
  49. imull $PCB_SIZE, %eax
  50. addl procs, %eax
  51. incl PCB_CPU_TICKS(%eax)
  52. /* Poll UART for received serial data (prevents FIFO overflow). */
  53. call serial_poll
  54. /* Decrement timeslice counter; switch only when it reaches 0. */
  55. decl slice_counter
  56. jnz .no_switch
  57. /* Reset counter for next timeslice. */
  58. movl $TIMESLICE, slice_counter
  59. /* Save current ESP into procs[current_pid].esp (procs is a pointer). */
  60. movl current_pid, %eax
  61. imull $PCB_SIZE, %eax
  62. addl procs, %eax
  63. movl %esp, PCB_ESP(%eax)
  64. /* Let the C scheduler pick the next process. */
  65. call schedule
  66. /* Load new ESP, CR3, and TSS esp0 from procs[current_pid]. */
  67. movl current_pid, %eax
  68. imull $PCB_SIZE, %eax
  69. addl procs, %eax
  70. movl PCB_ESP(%eax), %esp
  71. movl PCB_CR3(%eax), %ebx
  72. movl %ebx, %cr3
  73. movl PCB_KSTACK_TOP(%eax), %ebx
  74. movl %ebx, tss+4 /* TSS.esp0 = this process's ring-0 stack top */
  75. .no_switch:
  76. /* Both paths (return to the interrupted process, or to one just switched
  77. * in) must leave a usable data segment selector for the context being
  78. * resumed; ring-3 needs 0x23 (see irqret.inc). */
  79. IRQ_RETURN_DS 36
  80. popa
  81. iret
  82. .data
  83. .align 4
  84. slice_counter:
  85. .long TIMESLICE