switch.S 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. /* ExtINT delivered through the LAPIC needs a LAPIC EOI as well. */
  47. call lapic_eoi
  48. /* Poll UART for received serial data (prevents FIFO overflow). */
  49. call serial_poll
  50. /* Account the tick and decide whether the timeslice expired. */
  51. call sched_tick_bsp
  52. testl %eax, %eax
  53. jz .no_switch
  54. /* Save the interrupted ESP into current->esp. */
  55. movl %gs:0, %eax
  56. movl %esp, (%eax)
  57. /* Let the C scheduler pick the next process and switch CR3/TSS. */
  58. call schedule
  59. /* Load the new process's ESP. */
  60. movl %gs:0, %eax
  61. movl (%eax), %esp
  62. .no_switch:
  63. /* We are on the resumed process's kernel stack now; safe to release a
  64. * zombie this CPU switched away from earlier. */
  65. call sched_finish_switch
  66. /* Both paths must leave a usable data selector for the context being
  67. * resumed; ring-3 needs 0x23, ring-0 uses 0x10 + GS 0x28. */
  68. IRQ_RETURN_DS 36
  69. popa
  70. iret
  71. /*
  72. * AP local-APIC timer: EOI the LAPIC (no PIC EOI), account the tick against
  73. * the current process and switch when its slice expired. Unlike the PIT
  74. * path this never touches the global tick counter; the BSP PIT is the system
  75. * clock.
  76. */
  77. .globl lapic_timer_stub
  78. lapic_timer_stub:
  79. pusha
  80. movw $0x10, %ax
  81. movw %ax, %ds
  82. movw %ax, %es
  83. movw %ax, %fs
  84. movw $0x28, %ax
  85. movw %ax, %gs
  86. call lapic_eoi
  87. call sched_tick_ap
  88. testl %eax, %eax
  89. jz .lapic_no_switch
  90. movl %gs:0, %eax
  91. movl %esp, (%eax)
  92. call schedule
  93. movl %gs:0, %eax
  94. movl (%eax), %esp
  95. .lapic_no_switch:
  96. call sched_finish_switch
  97. IRQ_RETURN_DS 36
  98. popa
  99. iret
  100. /*
  101. * Reschedule IPI: request a reschedule on this CPU. The next local timer
  102. * tick picks it up. A fixed-delivery IPI still has to be EOIed on the target
  103. * LAPIC, otherwise it stays in service and blocks the timer vector.
  104. */
  105. .globl lapic_resched_stub
  106. lapic_resched_stub:
  107. pusha
  108. movw $0x10, %ax
  109. movw %ax, %ds
  110. movw %ax, %es
  111. movw %ax, %fs
  112. movw $0x28, %ax
  113. movw %ax, %gs
  114. call smp_resched_wake
  115. call lapic_eoi
  116. IRQ_RETURN_DS 36
  117. popa
  118. iret
  119. /* Spurious local-APIC interrupt: no EOI, no accounting. */
  120. .globl lapic_spurious_stub
  121. lapic_spurious_stub:
  122. iret
  123. /*
  124. * Voluntary reschedule gate: int 0x81. Entered from ring 0 only, with the
  125. * CPU having pushed EFLAGS/CS/EIP exactly like the PIT path. No EOI and no
  126. * tick accounting; just save ESP, pick a new process and restore its frame.
  127. */
  128. .globl resched_stub
  129. resched_stub:
  130. pusha
  131. movw $0x10, %ax
  132. movw %ax, %ds
  133. movw %ax, %es
  134. movw %ax, %fs
  135. movw $0x28, %ax
  136. movw %ax, %gs
  137. movl %gs:0, %eax
  138. movl %esp, (%eax)
  139. call schedule
  140. movl %gs:0, %eax
  141. movl (%eax), %esp
  142. call sched_finish_switch
  143. IRQ_RETURN_DS 36
  144. popa
  145. iret