1*3530Swnj /* kern_synch.c 4.12 81/04/15 */ 233Sbill 333Sbill #include "../h/param.h" 433Sbill #include "../h/systm.h" 533Sbill #include "../h/dir.h" 633Sbill #include "../h/user.h" 733Sbill #include "../h/proc.h" 833Sbill #include "../h/file.h" 933Sbill #include "../h/inode.h" 1033Sbill #include "../h/vm.h" 1133Sbill #include "../h/pte.h" 12181Sbill #include "../h/inline.h" 132443Swnj #include "../h/mtpr.h" 1433Sbill 1533Sbill #define SQSIZE 0100 /* Must be power of 2 */ 1633Sbill #define HASH(x) (( (int) x >> 5) & (SQSIZE-1)) 1733Sbill struct proc *slpque[SQSIZE]; 1833Sbill 1933Sbill /* 2033Sbill * Give up the processor till a wakeup occurs 2133Sbill * on chan, at which time the process 2233Sbill * enters the scheduling queue at priority pri. 2333Sbill * The most important effect of pri is that when 2433Sbill * pri<=PZERO a signal cannot disturb the sleep; 2533Sbill * if pri>PZERO signals will be processed. 2633Sbill * Callers of this routine must be prepared for 2733Sbill * premature return, and check that the reason for 2833Sbill * sleeping has gone away. 2933Sbill */ 3033Sbill sleep(chan, pri) 3133Sbill caddr_t chan; 3233Sbill { 33187Sbill register struct proc *rp, **hp; 34207Sbill register s; 3533Sbill 3633Sbill rp = u.u_procp; 3733Sbill s = spl6(); 3833Sbill if (chan==0 || rp->p_stat != SRUN || rp->p_rlink) 3933Sbill panic("sleep"); 4033Sbill rp->p_wchan = chan; 4133Sbill rp->p_slptime = 0; 4233Sbill rp->p_pri = pri; 43187Sbill hp = &slpque[HASH(chan)]; 44187Sbill rp->p_link = *hp; 45187Sbill *hp = rp; 4633Sbill if(pri > PZERO) { 47181Sbill if(ISSIG(rp)) { 48187Sbill if (rp->p_wchan) 49187Sbill unsleep(rp); 5033Sbill rp->p_stat = SRUN; 51131Sbill (void) spl0(); 5233Sbill goto psig; 5333Sbill } 54187Sbill if (rp->p_wchan == 0) 55187Sbill goto out; 56187Sbill rp->p_stat = SSLEEP; 57131Sbill (void) spl0(); 5833Sbill swtch(); 59181Sbill if(ISSIG(rp)) 6033Sbill goto psig; 6133Sbill } else { 62207Sbill rp->p_stat = SSLEEP; 63131Sbill (void) spl0(); 6433Sbill swtch(); 6533Sbill } 66187Sbill out: 6733Sbill splx(s); 6833Sbill return; 6933Sbill 7033Sbill /* 7133Sbill * If priority was low (>PZERO) and 7233Sbill * there has been a signal, 7333Sbill * execute non-local goto to 7433Sbill * the qsav location. 7533Sbill * (see trap1/trap.c) 7633Sbill */ 7733Sbill psig: 7833Sbill longjmp(u.u_qsav); 7933Sbill /*NOTREACHED*/ 8033Sbill } 8133Sbill 8233Sbill /* 83100Sbill * Sleep on chan at pri. 84100Sbill * Return in no more than the indicated number of seconds. 85100Sbill * (If seconds==0, no timeout implied) 86100Sbill * Return TS_OK if chan was awakened normally 87100Sbill * TS_TIME if timeout occurred 88100Sbill * TS_SIG if asynchronous signal occurred 89100Sbill */ 90100Sbill tsleep(chan, pri, seconds) 91100Sbill caddr_t chan; 92100Sbill { 93100Sbill label_t lqsav; 94100Sbill register struct proc *pp; 95100Sbill register sec, n, rval; 96100Sbill 97100Sbill pp = u.u_procp; 98102Sbill n = spl7(); 99100Sbill sec = 0; 100100Sbill rval = 0; 101100Sbill if (pp->p_clktim && pp->p_clktim<seconds) 102100Sbill seconds = 0; 103100Sbill if (seconds) { 104100Sbill pp->p_flag |= STIMO; 105103Sbill sec = pp->p_clktim-seconds; 106100Sbill pp->p_clktim = seconds; 107100Sbill } 108100Sbill bcopy((caddr_t)u.u_qsav, (caddr_t)lqsav, sizeof (label_t)); 109100Sbill if (setjmp(u.u_qsav)) 110100Sbill rval = TS_SIG; 111100Sbill else { 112100Sbill sleep(chan, pri); 113100Sbill if ((pp->p_flag&STIMO)==0 && seconds) 114100Sbill rval = TS_TIME; 115100Sbill else 116100Sbill rval = TS_OK; 117100Sbill } 118100Sbill pp->p_flag &= ~STIMO; 119100Sbill bcopy((caddr_t)lqsav, (caddr_t)u.u_qsav, sizeof (label_t)); 120103Sbill if (sec > 0) 121103Sbill pp->p_clktim += sec; 122103Sbill else 123103Sbill pp->p_clktim = 0; 124100Sbill splx(n); 125100Sbill return(rval); 126100Sbill } 127100Sbill 128100Sbill /* 129181Sbill * Remove a process from its wait queue 130181Sbill */ 131181Sbill unsleep(p) 132181Sbill register struct proc *p; 133181Sbill { 134181Sbill register struct proc **hp; 135181Sbill register s; 136181Sbill 137181Sbill s = spl6(); 138181Sbill if (p->p_wchan) { 139181Sbill hp = &slpque[HASH(p->p_wchan)]; 140181Sbill while (*hp != p) 141181Sbill hp = &(*hp)->p_link; 142181Sbill *hp = p->p_link; 143181Sbill p->p_wchan = 0; 144181Sbill } 145181Sbill splx(s); 146181Sbill } 147181Sbill 148181Sbill /* 14933Sbill * Wake up all processes sleeping on chan. 15033Sbill */ 15133Sbill wakeup(chan) 15233Sbill register caddr_t chan; 15333Sbill { 154187Sbill register struct proc *p, **q, **h; 15533Sbill int s; 15633Sbill 15733Sbill s = spl6(); 158187Sbill h = &slpque[HASH(chan)]; 15933Sbill restart: 160187Sbill for (q = h; p = *q; ) { 161181Sbill if (p->p_rlink || p->p_stat != SSLEEP && p->p_stat != SSTOP) 16233Sbill panic("wakeup"); 163207Sbill if (p->p_wchan==chan) { 16433Sbill p->p_wchan = 0; 165187Sbill *q = p->p_link; 16633Sbill p->p_slptime = 0; 167181Sbill if (p->p_stat == SSLEEP) { 168181Sbill /* OPTIMIZED INLINE EXPANSION OF setrun(p) */ 169181Sbill p->p_stat = SRUN; 1702702Swnj if (p->p_flag & SLOAD) 171181Sbill setrq(p); 1722443Swnj if(p->p_pri < curpri) { 173181Sbill runrun++; 1742443Swnj aston(); 1752443Swnj } 176181Sbill if(runout != 0 && (p->p_flag&SLOAD) == 0) { 177181Sbill runout = 0; 178181Sbill wakeup((caddr_t)&runout); 179181Sbill } 180181Sbill /* END INLINE EXPANSION */ 181187Sbill goto restart; 18233Sbill } 183187Sbill } else 184187Sbill q = &p->p_link; 18533Sbill } 18633Sbill splx(s); 18733Sbill } 18833Sbill 18933Sbill /* 19033Sbill * Initialize the (doubly-linked) run queues 19133Sbill * to be empty. 19233Sbill */ 19333Sbill rqinit() 19433Sbill { 19533Sbill register int i; 19633Sbill 19733Sbill for (i = 0; i < NQS; i++) 19833Sbill qs[i].ph_link = qs[i].ph_rlink = (struct proc *)&qs[i]; 19933Sbill } 20033Sbill 20133Sbill /* 20233Sbill * Set the process running; 20333Sbill * arrange for it to be swapped in if necessary. 20433Sbill */ 20533Sbill setrun(p) 20633Sbill register struct proc *p; 20733Sbill { 20833Sbill register s; 20933Sbill 21033Sbill s = spl6(); 21133Sbill switch (p->p_stat) { 21233Sbill 21333Sbill case 0: 21433Sbill case SWAIT: 21533Sbill case SRUN: 21633Sbill case SZOMB: 21733Sbill default: 21833Sbill panic("setrun"); 21933Sbill 220207Sbill case SSTOP: 22133Sbill case SSLEEP: 222181Sbill unsleep(p); /* e.g. when sending signals */ 22333Sbill break; 22433Sbill 22533Sbill case SIDL: 22633Sbill break; 22733Sbill } 22833Sbill p->p_stat = SRUN; 22933Sbill if (p->p_flag & SLOAD) 23033Sbill setrq(p); 23133Sbill splx(s); 2322443Swnj if(p->p_pri < curpri) { 23333Sbill runrun++; 2342443Swnj aston(); 2352443Swnj } 23633Sbill if(runout != 0 && (p->p_flag&SLOAD) == 0) { 23733Sbill runout = 0; 23833Sbill wakeup((caddr_t)&runout); 23933Sbill } 24033Sbill } 24133Sbill 24233Sbill /* 24333Sbill * Set user priority. 24433Sbill * The rescheduling flag (runrun) 24533Sbill * is set if the priority is better 24633Sbill * than the currently running process. 24733Sbill */ 24833Sbill setpri(pp) 24933Sbill register struct proc *pp; 25033Sbill { 25133Sbill register p; 25233Sbill 25333Sbill p = (pp->p_cpu & 0377)/16; 2541543Sbill p += PUSER + 2*(pp->p_nice - NZERO); 255*3530Swnj if (pp->p_rssize > pp->p_maxrss && freemem < desfree) 256*3530Swnj p += 2*4; /* effectively, nice(4) */ 25733Sbill if(p > 127) 25833Sbill p = 127; 2592453Swnj if(p < curpri) { 26033Sbill runrun++; 2612453Swnj aston(); 2622453Swnj } 26333Sbill pp->p_usrpri = p; 26433Sbill return(p); 26533Sbill } 26633Sbill 26733Sbill /* 26833Sbill * Create a new process-- the internal version of 26933Sbill * sys fork. 27033Sbill * It returns 1 in the new process, 0 in the old. 27133Sbill */ 27233Sbill newproc(isvfork) 27333Sbill { 27433Sbill register struct proc *p; 27533Sbill register struct proc *rpp, *rip; 27633Sbill register int n; 27733Sbill 27833Sbill p = NULL; 27933Sbill /* 28033Sbill * First, just locate a slot for a process 28133Sbill * and copy the useful info from this process into it. 28233Sbill * The panic "cannot happen" because fork has already 28333Sbill * checked for the existence of a slot. 28433Sbill */ 28533Sbill retry: 28633Sbill mpid++; 28733Sbill if(mpid >= 30000) { 28833Sbill mpid = 0; 28933Sbill goto retry; 29033Sbill } 2912741Swnj for(rpp = proc; rpp < procNPROC; rpp++) { 29233Sbill if(rpp->p_stat == NULL && p==NULL) 29333Sbill p = rpp; 29433Sbill if (rpp->p_pid==mpid || rpp->p_pgrp==mpid) 29533Sbill goto retry; 29633Sbill } 29733Sbill if ((rpp = p)==NULL) 29833Sbill panic("no procs"); 29933Sbill 30033Sbill /* 30133Sbill * make proc entry for new proc 30233Sbill */ 30333Sbill 30433Sbill rip = u.u_procp; 30533Sbill rpp->p_stat = SIDL; 30633Sbill rpp->p_clktim = 0; 307913Sbill rpp->p_flag = SLOAD | (rip->p_flag & (SPAGI|SDETACH|SNUSIG)); 30833Sbill if (isvfork) { 30933Sbill rpp->p_flag |= SVFORK; 31033Sbill rpp->p_ndx = rip->p_ndx; 31133Sbill } else 31233Sbill rpp->p_ndx = rpp - proc; 31333Sbill rpp->p_uid = rip->p_uid; 31433Sbill rpp->p_pgrp = rip->p_pgrp; 31533Sbill rpp->p_nice = rip->p_nice; 31633Sbill rpp->p_textp = isvfork ? 0 : rip->p_textp; 31733Sbill rpp->p_pid = mpid; 31833Sbill rpp->p_ppid = rip->p_pid; 319181Sbill rpp->p_pptr = rip; 32033Sbill rpp->p_time = 0; 32133Sbill rpp->p_cpu = 0; 322181Sbill rpp->p_siga0 = rip->p_siga0; 323181Sbill rpp->p_siga1 = rip->p_siga1; 324181Sbill /* take along any pending signals, like stops? */ 32533Sbill if (isvfork) { 32633Sbill rpp->p_tsize = rpp->p_dsize = rpp->p_ssize = 0; 32733Sbill rpp->p_szpt = clrnd(ctopt(UPAGES)); 32833Sbill forkstat.cntvfork++; 32933Sbill forkstat.sizvfork += rip->p_dsize + rip->p_ssize; 33033Sbill } else { 33133Sbill rpp->p_tsize = rip->p_tsize; 33233Sbill rpp->p_dsize = rip->p_dsize; 33333Sbill rpp->p_ssize = rip->p_ssize; 33433Sbill rpp->p_szpt = rip->p_szpt; 33533Sbill forkstat.cntfork++; 33633Sbill forkstat.sizfork += rip->p_dsize + rip->p_ssize; 33733Sbill } 33833Sbill rpp->p_rssize = 0; 3393514Sroot rpp->p_maxrss = rip->p_maxrss; 34033Sbill rpp->p_wchan = 0; 34133Sbill rpp->p_slptime = 0; 3421400Sbill rpp->p_pctcpu = 0; 3431400Sbill rpp->p_cpticks = 0; 34433Sbill n = PIDHASH(rpp->p_pid); 34533Sbill p->p_idhash = pidhash[n]; 34633Sbill pidhash[n] = rpp - proc; 34733Sbill 34833Sbill /* 34933Sbill * make duplicate entries 35033Sbill * where needed 35133Sbill */ 35233Sbill 35333Sbill multprog++; 35433Sbill 35533Sbill for(n=0; n<NOFILE; n++) 35633Sbill if(u.u_ofile[n] != NULL) { 3572938Swnj u.u_ofile[n]->f_count++; 3582938Swnj if(!isvfork && u.u_vrpages[n]) 3592938Swnj u.u_ofile[n]->f_inode->i_vfdcnt++; 36033Sbill } 36133Sbill 36233Sbill u.u_cdir->i_count++; 36333Sbill if (u.u_rdir) 36433Sbill u.u_rdir->i_count++; 36533Sbill /* 36633Sbill * Partially simulate the environment 36733Sbill * of the new process so that when it is actually 36833Sbill * created (by copying) it will look right. 36933Sbill */ 37033Sbill 37133Sbill rip->p_flag |= SKEEP; /* prevent parent from being swapped */ 37233Sbill 37333Sbill if (procdup(rpp, isvfork)) 37433Sbill return (1); 37533Sbill 3761788Sbill (void) spl6(); 37733Sbill rpp->p_stat = SRUN; 37833Sbill setrq(rpp); 3791793Sbill (void) spl0(); 38088Sbill /* SSWAP NOT NEEDED IN THIS CASE AS u.u_pcb.pcb_sswap SUFFICES */ 38133Sbill /* rpp->p_flag |= SSWAP; */ 38233Sbill rip->p_flag &= ~SKEEP; 38333Sbill if (isvfork) { 38433Sbill u.u_procp->p_xlink = rpp; 38533Sbill u.u_procp->p_flag |= SNOVM; 38633Sbill while (rpp->p_flag & SVFORK) 38733Sbill sleep((caddr_t)rpp, PZERO - 1); 38833Sbill if ((rpp->p_flag & SLOAD) == 0) 38933Sbill panic("newproc vfork"); 39033Sbill uaccess(rpp, Vfmap, &vfutl); 39133Sbill u.u_procp->p_xlink = 0; 39233Sbill vpassvm(rpp, u.u_procp, &vfutl, &u, Vfmap); 39333Sbill for (n = 0; n < NOFILE; n++) 39433Sbill if (vfutl.u_vrpages[n]) { 39533Sbill if ((u.u_vrpages[n] = vfutl.u_vrpages[n] - 1) == 0) 39633Sbill if (--u.u_ofile[n]->f_inode->i_vfdcnt < 0) 39733Sbill panic("newproc i_vfdcnt"); 39833Sbill vfutl.u_vrpages[n] = 0; 39933Sbill } 40033Sbill u.u_procp->p_flag &= ~SNOVM; 40133Sbill rpp->p_ndx = rpp - proc; 40233Sbill rpp->p_flag |= SVFDONE; 40333Sbill wakeup((caddr_t)rpp); 40433Sbill } 40533Sbill return (0); 40633Sbill } 407