switch.S 1.8 KB

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