switch.S 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. /* struct process offsets (must match process.h / process.c) */
  24. .equ PCB_ESP, 0
  25. .equ PCB_SIZE, 24 /* sizeof(struct process): 4+4+4+12 = 24 */
  26. .equ TIMESLICE, 20
  27. .equ FLUSH_INTERVAL, 33 /* 1000Hz PIT / 30Hz flush ≈ 33 ticks */
  28. .globl pit_irq_stub
  29. pit_irq_stub:
  30. pusha
  31. /* ticks++ and send EOI -- always, even when we don't switch. */
  32. incl ticks
  33. movb $0x20, %al
  34. outb %al, $0x20
  35. /* Poll UART for received serial data (prevents FIFO overflow). */
  36. call serial_poll
  37. /* Shadow buffer flush at 30Hz: set pending flag, main loop does the copy. */
  38. decl flush_counter
  39. jnz .no_flush
  40. movl $FLUSH_INTERVAL, flush_counter
  41. movl $1, vga_shadow_flush_pending
  42. .no_flush:
  43. /* Decrement timeslice counter; switch only when it reaches 0. */
  44. decl slice_counter
  45. jnz .no_switch
  46. /* Reset counter for next timeslice. */
  47. movl $TIMESLICE, slice_counter
  48. /* Save current ESP into procs[current_pid].esp */
  49. movl current_pid, %eax
  50. imull $PCB_SIZE, %eax
  51. leal procs(%eax), %ebx
  52. movl %esp, PCB_ESP(%ebx)
  53. /* Let the C scheduler pick the next process. */
  54. call schedule
  55. /* Load new ESP from procs[current_pid].esp */
  56. movl current_pid, %eax
  57. imull $PCB_SIZE, %eax
  58. leal procs(%eax), %ebx
  59. movl PCB_ESP(%ebx), %esp
  60. .no_switch:
  61. popa
  62. iret
  63. .data
  64. .align 4
  65. slice_counter:
  66. .long TIMESLICE
  67. flush_counter:
  68. .long FLUSH_INTERVAL