switch.S 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /*
  2. * Context-switch PIT IRQ stub.
  3. *
  4. * On entry the CPU has already pushed EFLAGS, CS, EIP onto the current
  5. * process's stack. We save the rest, bump the tick counter, and -- when
  6. * sched_tick_bsp() says the timeslice expired -- call schedule() and swap
  7. * ESP to the next process.
  8. *
  9. * The only process facts this file knows are:
  10. * - %gs:0 is struct cpu.current
  11. * - offset 0 of struct process is esp (saved/restored here)
  12. * CR3 and TSS.esp0 are switched by schedule() in C.
  13. *
  14. * Serial poll: every tick, check UART for received data and push to the ring
  15. * buffer so no bytes are lost during preemption.
  16. */
  17. .code32
  18. .text
  19. .extern ticks
  20. .extern schedule
  21. .extern sched_tick_bsp
  22. .extern sched_tick_ap
  23. .extern sched_finish_switch
  24. .extern serial_poll
  25. .extern lapic_eoi
  26. .include "irqret.inc"
  27. .globl pit_irq_stub
  28. pit_irq_stub:
  29. pusha
  30. /* Load the kernel selectors. The PIT can fire while a ring-3 process
  31. * is running; DS would then be null after the IRET that entered ring 3,
  32. * and the accesses below would #GP. GS is the per-CPU data selector. */
  33. movw $0x10, %ax
  34. movw %ax, %ds
  35. movw %ax, %es
  36. movw %ax, %fs
  37. movw $0x28, %ax
  38. movw %ax, %gs
  39. /* ticks++ and send EOI -- always, even when we don't switch. */
  40. incl ticks
  41. movb $0x20, %al
  42. outb %al, $0x20
  43. /* Poll UART for received serial data (prevents FIFO overflow). */
  44. call serial_poll
  45. /* Account the tick and decide whether the timeslice expired. */
  46. call sched_tick_bsp
  47. testl %eax, %eax
  48. jz .no_switch
  49. /* Save the interrupted ESP into current->esp. */
  50. movl %gs:0, %eax
  51. movl %esp, (%eax)
  52. /* Let the C scheduler pick the next process and switch CR3/TSS. */
  53. call schedule
  54. /* Load the new process's ESP. */
  55. movl %gs:0, %eax
  56. movl (%eax), %esp
  57. .no_switch:
  58. /* We are on the resumed process's kernel stack now; safe to release a
  59. * zombie this CPU switched away from earlier. */
  60. call sched_finish_switch
  61. /* Both paths must leave a usable data selector for the context being
  62. * resumed; ring-3 needs 0x23, ring-0 uses 0x10 + GS 0x28. */
  63. IRQ_RETURN_DS 36
  64. popa
  65. iret
  66. /*
  67. * AP local-APIC timer: EOI the LAPIC (no PIC EOI), account the tick against
  68. * the current process and switch when its slice expired. Unlike the PIT
  69. * path this never touches the global tick counter; the BSP PIT is the system
  70. * clock.
  71. */
  72. .globl lapic_timer_stub
  73. lapic_timer_stub:
  74. pusha
  75. movw $0x10, %ax
  76. movw %ax, %ds
  77. movw %ax, %es
  78. movw %ax, %fs
  79. movw $0x28, %ax
  80. movw %ax, %gs
  81. call lapic_eoi
  82. call sched_tick_ap
  83. testl %eax, %eax
  84. jz .lapic_no_switch
  85. movl %gs:0, %eax
  86. movl %esp, (%eax)
  87. call schedule
  88. movl %gs:0, %eax
  89. movl (%eax), %esp
  90. .lapic_no_switch:
  91. call sched_finish_switch
  92. IRQ_RETURN_DS 36
  93. popa
  94. iret
  95. /*
  96. * Reschedule IPI: request a reschedule on this CPU. The next local timer
  97. * tick picks it up. A fixed-delivery IPI still has to be EOIed on the target
  98. * LAPIC, otherwise it stays in service and blocks the timer vector.
  99. */
  100. .globl lapic_resched_stub
  101. lapic_resched_stub:
  102. pusha
  103. movw $0x10, %ax
  104. movw %ax, %ds
  105. movw %ax, %es
  106. movw %ax, %fs
  107. movw $0x28, %ax
  108. movw %ax, %gs
  109. movl %gs:0, %eax
  110. movl $1, 24(%eax) /* struct cpu.need_resched */
  111. call lapic_eoi
  112. IRQ_RETURN_DS 36
  113. popa
  114. iret
  115. /* Spurious local-APIC interrupt: no EOI, no accounting. */
  116. .globl lapic_spurious_stub
  117. lapic_spurious_stub:
  118. iret
  119. /*
  120. * Voluntary reschedule gate: int 0x81. Entered from ring 0 only, with the
  121. * CPU having pushed EFLAGS/CS/EIP exactly like the PIT path. No EOI and no
  122. * tick accounting; just save ESP, pick a new process and restore its frame.
  123. */
  124. .globl resched_stub
  125. resched_stub:
  126. pusha
  127. movw $0x10, %ax
  128. movw %ax, %ds
  129. movw %ax, %es
  130. movw %ax, %fs
  131. movw $0x28, %ax
  132. movw %ax, %gs
  133. movl %gs:0, %eax
  134. movl %esp, (%eax)
  135. call schedule
  136. movl %gs:0, %eax
  137. movl (%eax), %esp
  138. call sched_finish_switch
  139. IRQ_RETURN_DS 36
  140. popa
  141. iret