| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167 |
- /*
- * Context-switch PIT IRQ stub.
- *
- * On entry the CPU has already pushed EFLAGS, CS, EIP onto the current
- * process's stack. We save the rest, bump the tick counter, and -- when
- * sched_tick_bsp() says the timeslice expired -- call schedule() and swap
- * ESP to the next process.
- *
- * The only process facts this file knows are:
- * - %gs:0 is struct cpu.current
- * - offset 0 of struct process is esp (saved/restored here)
- * CR3 and TSS.esp0 are switched by schedule() in C.
- *
- * Serial poll: every tick, check UART for received data and push to the ring
- * buffer so no bytes are lost during preemption.
- */
- .code32
- .text
- .extern timer_tick_advance
- .extern schedule
- .extern sched_tick_bsp
- .extern sched_tick_ap
- .extern sched_finish_switch
- .extern serial_poll
- .extern lapic_eoi
- .extern smp_resched_wake
- .include "irqret.inc"
- .globl pit_irq_stub
- pit_irq_stub:
- pusha
- /* Load the kernel selectors. The PIT can fire while a ring-3 process
- * is running; DS would then be null after the IRET that entered ring 3,
- * and the accesses below would #GP. GS is the per-CPU data selector. */
- movw $0x10, %ax
- movw %ax, %ds
- movw %ax, %es
- movw %ax, %fs
- movw $0x28, %ax
- movw %ax, %gs
- /* Advance the millisecond clock and send EOI -- always, even when we
- * don't switch. The PIT period is 1 ms while the BSP runs a process
- * and 10 ms while it is idle (timer_tick_advance adds the period). */
- call timer_tick_advance
- movb $0x20, %al
- outb %al, $0x20
- /* Poll UART for received serial data (prevents FIFO overflow). */
- call serial_poll
- /* Account the tick and decide whether the timeslice expired. */
- call sched_tick_bsp
- testl %eax, %eax
- jz .no_switch
- /* Save the interrupted ESP into current->esp. */
- movl %gs:0, %eax
- movl %esp, (%eax)
- /* Let the C scheduler pick the next process and switch CR3/TSS. */
- call schedule
- /* Load the new process's ESP. */
- movl %gs:0, %eax
- movl (%eax), %esp
- .no_switch:
- /* We are on the resumed process's kernel stack now; safe to release a
- * zombie this CPU switched away from earlier. */
- call sched_finish_switch
- /* Both paths must leave a usable data selector for the context being
- * resumed; ring-3 needs 0x23, ring-0 uses 0x10 + GS 0x28. */
- IRQ_RETURN_DS 36
- popa
- iret
- /*
- * AP local-APIC timer: EOI the LAPIC (no PIC EOI), account the tick against
- * the current process and switch when its slice expired. Unlike the PIT
- * path this never touches the global tick counter; the BSP PIT is the system
- * clock.
- */
- .globl lapic_timer_stub
- lapic_timer_stub:
- pusha
- movw $0x10, %ax
- movw %ax, %ds
- movw %ax, %es
- movw %ax, %fs
- movw $0x28, %ax
- movw %ax, %gs
- call lapic_eoi
- call sched_tick_ap
- testl %eax, %eax
- jz .lapic_no_switch
- movl %gs:0, %eax
- movl %esp, (%eax)
- call schedule
- movl %gs:0, %eax
- movl (%eax), %esp
- .lapic_no_switch:
- call sched_finish_switch
- IRQ_RETURN_DS 36
- popa
- iret
- /*
- * Reschedule IPI: request a reschedule on this CPU. The next local timer
- * tick picks it up. A fixed-delivery IPI still has to be EOIed on the target
- * LAPIC, otherwise it stays in service and blocks the timer vector.
- */
- .globl lapic_resched_stub
- lapic_resched_stub:
- pusha
- movw $0x10, %ax
- movw %ax, %ds
- movw %ax, %es
- movw %ax, %fs
- movw $0x28, %ax
- movw %ax, %gs
- call smp_resched_wake
- call lapic_eoi
- IRQ_RETURN_DS 36
- popa
- iret
- /* Spurious local-APIC interrupt: no EOI, no accounting. */
- .globl lapic_spurious_stub
- lapic_spurious_stub:
- iret
- /*
- * Voluntary reschedule gate: int 0x81. Entered from ring 0 only, with the
- * CPU having pushed EFLAGS/CS/EIP exactly like the PIT path. No EOI and no
- * tick accounting; just save ESP, pick a new process and restore its frame.
- */
- .globl resched_stub
- resched_stub:
- pusha
- movw $0x10, %ax
- movw %ax, %ds
- movw %ax, %es
- movw %ax, %fs
- movw $0x28, %ax
- movw %ax, %gs
- movl %gs:0, %eax
- movl %esp, (%eax)
- call schedule
- movl %gs:0, %eax
- movl (%eax), %esp
- call sched_finish_switch
- IRQ_RETURN_DS 36
- popa
- iret
|