switch.S 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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: every ~33 ticks (30Hz), set pending flag.
  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 vga_shadow_flush_pending
  22. .extern serial_poll
  23. .extern kdbg_pit_tick
  24. .extern tss
  25. .include "irqret.inc"
  26. /* struct process offsets (must match process.h / process.c) */
  27. .equ PCB_ESP, 0
  28. .equ PCB_CR3, 32
  29. .equ PCB_KSTACK_TOP, 36
  30. .equ PCB_SIZE, 44 /* sizeof(struct process): 4+4+4+12+4+4+4+4+4 = 44 */
  31. .equ TIMESLICE, 20
  32. .equ FLUSH_INTERVAL, 33 /* 1000Hz PIT / 30Hz flush ≈ 33 ticks */
  33. .globl pit_irq_stub
  34. pit_irq_stub:
  35. pusha
  36. /* Load the kernel data segment. The PIT can fire while a ring-3 process
  37. * is running, and in that case the CPU has switched CS/SS but left DS
  38. * null (IRET reset it when it returned to ring-3); the data accesses
  39. * below (ticks, counters, procs, tss) go through DS and would #GP. */
  40. movw $0x10, %ax
  41. movw %ax, %ds
  42. movw %ax, %es
  43. movw %ax, %fs
  44. movw %ax, %gs
  45. /* ticks++ and send EOI -- always, even when we don't switch. */
  46. incl ticks
  47. movb $0x20, %al
  48. outb %al, $0x20
  49. /* Poll UART for received serial data (prevents FIFO overflow). */
  50. call serial_poll
  51. /* On-screen debug heartbeat / status refresh (no serial console on
  52. * some targets; this keeps a live indicator on the framebuffer). */
  53. call kdbg_pit_tick
  54. /* Shadow buffer flush at 30Hz: set pending flag, main loop does the copy. */
  55. decl flush_counter
  56. jnz .no_flush
  57. movl $FLUSH_INTERVAL, flush_counter
  58. movl $1, vga_shadow_flush_pending
  59. .no_flush:
  60. /* Decrement timeslice counter; switch only when it reaches 0. */
  61. decl slice_counter
  62. jnz .no_switch
  63. /* Reset counter for next timeslice. */
  64. movl $TIMESLICE, slice_counter
  65. /* Save current ESP into procs[current_pid].esp (procs is a pointer). */
  66. movl current_pid, %eax
  67. imull $PCB_SIZE, %eax
  68. addl procs, %eax
  69. movl %esp, PCB_ESP(%eax)
  70. /* Let the C scheduler pick the next process. */
  71. call schedule
  72. /* Load new ESP, CR3, and TSS esp0 from procs[current_pid]. */
  73. movl current_pid, %eax
  74. imull $PCB_SIZE, %eax
  75. addl procs, %eax
  76. movl PCB_ESP(%eax), %esp
  77. movl PCB_CR3(%eax), %ebx
  78. movl %ebx, %cr3
  79. movl PCB_KSTACK_TOP(%eax), %ebx
  80. movl %ebx, tss+4 /* TSS.esp0 = this process's ring-0 stack top */
  81. .no_switch:
  82. /* Both paths (return to the interrupted process, or to one just switched
  83. * in) must leave a usable data segment selector for the context being
  84. * resumed; ring-3 needs 0x23 (see irqret.inc). */
  85. IRQ_RETURN_DS 36
  86. popa
  87. iret
  88. .data
  89. .align 4
  90. slice_counter:
  91. .long TIMESLICE
  92. flush_counter:
  93. .long FLUSH_INTERVAL