switch.S 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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. .code32
  10. .text
  11. .extern ticks
  12. .extern current_pid
  13. .extern procs
  14. .extern schedule
  15. /* struct process offsets (must match process.h / process.c) */
  16. .equ PCB_ESP, 0
  17. .equ PCB_SIZE, 24 /* sizeof(struct process): 4+4+4+12 = 24 */
  18. .equ TIMESLICE, 20
  19. .globl pit_irq_stub
  20. pit_irq_stub:
  21. pusha
  22. /* ticks++ and send EOI -- always, even when we don't switch. */
  23. incl ticks
  24. movb $0x20, %al
  25. outb %al, $0x20
  26. /* Decrement timeslice counter; switch only when it reaches 0. */
  27. decl slice_counter
  28. jnz .no_switch
  29. /* Reset counter for next timeslice. */
  30. movl $TIMESLICE, slice_counter
  31. /* Save current ESP into procs[current_pid].esp */
  32. movl current_pid, %eax
  33. imull $PCB_SIZE, %eax
  34. leal procs(%eax), %ebx
  35. movl %esp, PCB_ESP(%ebx)
  36. /* Let the C scheduler pick the next process. */
  37. call schedule
  38. /* Load new ESP from procs[current_pid].esp */
  39. movl current_pid, %eax
  40. imull $PCB_SIZE, %eax
  41. leal procs(%eax), %ebx
  42. movl PCB_ESP(%ebx), %esp
  43. .no_switch:
  44. popa
  45. iret
  46. .data
  47. .align 4
  48. slice_counter:
  49. .long TIMESLICE