switch.S 1.7 KB

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