/*------------------------------------------------------------------------ * resched -- reschedule processor to highest priority ready process * * Notes: Upon entry, currpid gives current process id. * Proctab[currpid].pstate gives correct NEXT state for * current process if other than PRREADY. *------------------------------------------------------------------------ */ int resched() { STATWORD PS; register struct pentry *optr; /* pointer to old process entry */ register struct pentry *nptr; /* pointer to new process entry */ disable(PS); /* no switch needed if current process priority higher than next*/ if ( ( (optr= &proctab[currpid])->pstate == PRCURR) && (lastkey(rdytail)<optr->pprio)) { restore(PS); return(OK); } #ifdef STKCHK /* make sure current stack has room for ctsw */ asm("movl %esp, currSP"); if (currSP - optr->plimit < 48) { kprintf("Bad SP current process, pid=%d (%s), lim=0x%lx, currently 0x%lx\n", currpid, optr->pname, (unsigned long) optr->plimit, (unsigned long) currSP); panic("current process stack overflow"); } #endif /* force context switch */ if (optr->pstate == PRCURR) { optr->pstate = PRREADY; insert(currpid,rdyhead,optr->pprio); } /* remove highest priority process at end of ready list */ nptr = &proctab[ (currpid = getlast(rdytail)) ]; nptr->pstate = PRCURR; /* mark it currently running */ #ifdef notdef #ifdef STKCHK if ( *( (int *)nptr->pbase ) != MAGIC ) { kprintf("Bad magic pid=%d value=0x%lx, at 0x%lx\n", currpid, (unsigned long) *( (int *)nptr->pbase ), (unsigned long) nptr->pbase); panic("stack corrupted"); } /* * need ~16 longs of stack space below, so include that in check * below. */ if (nptr->pesp - nptr->plimit < 48) { kprintf("Bad SP pid=%d (%s), lim=0x%lx will be 0x%lx\n", currpid, nptr->pname, (unsigned long) nptr->plimit, (unsigned long) nptr->pesp); panic("stack overflow"); } #endif /* STKCHK */ #endif /* notdef */ #ifdef RTCLOCK preempt = QUANTUM; /* reset preemption counter */ #endif #ifdef DEBUG PrintSaved(nptr); #endif // When performing a context switch between processes we must also // switch between memory spaces. This is accomplished by adjusting // the PDBR register with every context switch. // // 5 - Context switch // - every process has separate page directory // - before ctxsw() load CR3 with the process' PDBR set_PDBR(VA2VPNO(nptr->pd)); #if DUSTYDEBUG kprintf("switching to process %d\n", (nptr - proctab)); #endif ctxsw(&optr->pesp, optr->pirmask, &nptr->pesp, nptr->pirmask); #ifdef DEBUG PrintSaved(nptr); #endif /* The OLD process returns here when resumed. */ restore(PS); return OK; }
/*------------------------------------------------------------------------ * resched -- reschedule processor to highest priority ready process * * Notes: Upon entry, currpid gives current process id. * Proctab[currpid].pstate gives correct NEXT state for * current process if other than PRREADY. *------------------------------------------------------------------------ */ int resched() { STATWORD PS; register struct pentry *optr; /* pointer to old process entry */ register struct pentry *nptr; /* pointer to new process entry */ register int i; disable(PS); /* no switch needed if current process priority higher than next*/ if ( ( (optr= &proctab[currpid])->pstate == PRCURR) && (lastkey(rdytail)<optr->pprio)) { restore(PS); return(OK); } #ifdef STKCHK /* make sure current stack has room for ctsw */ asm("movl %esp, currSP"); if (currSP - optr->plimit < 48) { kprintf("Bad SP current process, pid=%d (%s), lim=0x%lx, currently 0x%lx\n", currpid, optr->pname, (unsigned long) optr->plimit, (unsigned long) currSP); panic("current process stack overflow"); } #endif /* force context switch */ if (optr->pstate == PRCURR) { optr->pstate = PRREADY; insert(currpid,rdyhead,optr->pprio); } /* remove highest priority process at end of ready list */ nptr = &proctab[ (currpid = getlast(rdytail)) ]; nptr->pstate = PRCURR; /* mark it currently running */ #ifdef notdef #ifdef STKCHK if ( *( (int *)nptr->pbase ) != MAGIC ) { kprintf("Bad magic pid=%d value=0x%lx, at 0x%lx\n", currpid, (unsigned long) *( (int *)nptr->pbase ), (unsigned long) nptr->pbase); panic("stack corrupted"); } /* * need ~16 longs of stack space below, so include that in check * below. */ if (nptr->pesp - nptr->plimit < 48) { kprintf("Bad SP pid=%d (%s), lim=0x%lx will be 0x%lx\n", currpid, nptr->pname, (unsigned long) nptr->plimit, (unsigned long) nptr->pesp); panic("stack overflow"); } #endif /* STKCHK */ #endif /* notdef */ #ifdef RTCLOCK preempt = QUANTUM; /* reset preemption counter */ #endif #ifdef DEBUG PrintSaved(nptr); #endif //OS proj 3 modify write_cr3(nptr->pdbr); ctxsw(&optr->pesp, optr->pirmask, &nptr->pesp, nptr->pirmask); #ifdef DEBUG PrintSaved(nptr); #endif /* The OLD process returns here when resumed. */ restore(PS); return OK; }