switch.S 3.9 KB

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