1*3514Sroot /* kern_synch.c 4.11 81/04/13 */ 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); 25533Sbill if(p > 127) 25633Sbill p = 127; 2572453Swnj if(p < curpri) { 25833Sbill runrun++; 2592453Swnj aston(); 2602453Swnj } 26133Sbill pp->p_usrpri = p; 26233Sbill return(p); 26333Sbill } 26433Sbill 26533Sbill /* 26633Sbill * Create a new process-- the internal version of 26733Sbill * sys fork. 26833Sbill * It returns 1 in the new process, 0 in the old. 26933Sbill */ 27033Sbill newproc(isvfork) 27133Sbill { 27233Sbill register struct proc *p; 27333Sbill register struct proc *rpp, *rip; 27433Sbill register int n; 27533Sbill 27633Sbill p = NULL; 27733Sbill /* 27833Sbill * First, just locate a slot for a process 27933Sbill * and copy the useful info from this process into it. 28033Sbill * The panic "cannot happen" because fork has already 28133Sbill * checked for the existence of a slot. 28233Sbill */ 28333Sbill retry: 28433Sbill mpid++; 28533Sbill if(mpid >= 30000) { 28633Sbill mpid = 0; 28733Sbill goto retry; 28833Sbill } 2892741Swnj for(rpp = proc; rpp < procNPROC; rpp++) { 29033Sbill if(rpp->p_stat == NULL && p==NULL) 29133Sbill p = rpp; 29233Sbill if (rpp->p_pid==mpid || rpp->p_pgrp==mpid) 29333Sbill goto retry; 29433Sbill } 29533Sbill if ((rpp = p)==NULL) 29633Sbill panic("no procs"); 29733Sbill 29833Sbill /* 29933Sbill * make proc entry for new proc 30033Sbill */ 30133Sbill 30233Sbill rip = u.u_procp; 30333Sbill rpp->p_stat = SIDL; 30433Sbill rpp->p_clktim = 0; 305913Sbill rpp->p_flag = SLOAD | (rip->p_flag & (SPAGI|SDETACH|SNUSIG)); 30633Sbill if (isvfork) { 30733Sbill rpp->p_flag |= SVFORK; 30833Sbill rpp->p_ndx = rip->p_ndx; 30933Sbill } else 31033Sbill rpp->p_ndx = rpp - proc; 31133Sbill rpp->p_uid = rip->p_uid; 31233Sbill rpp->p_pgrp = rip->p_pgrp; 31333Sbill rpp->p_nice = rip->p_nice; 31433Sbill rpp->p_textp = isvfork ? 0 : rip->p_textp; 31533Sbill rpp->p_pid = mpid; 31633Sbill rpp->p_ppid = rip->p_pid; 317181Sbill rpp->p_pptr = rip; 31833Sbill rpp->p_time = 0; 31933Sbill rpp->p_cpu = 0; 320181Sbill rpp->p_siga0 = rip->p_siga0; 321181Sbill rpp->p_siga1 = rip->p_siga1; 322181Sbill /* take along any pending signals, like stops? */ 32333Sbill if (isvfork) { 32433Sbill rpp->p_tsize = rpp->p_dsize = rpp->p_ssize = 0; 32533Sbill rpp->p_szpt = clrnd(ctopt(UPAGES)); 32633Sbill forkstat.cntvfork++; 32733Sbill forkstat.sizvfork += rip->p_dsize + rip->p_ssize; 32833Sbill } else { 32933Sbill rpp->p_tsize = rip->p_tsize; 33033Sbill rpp->p_dsize = rip->p_dsize; 33133Sbill rpp->p_ssize = rip->p_ssize; 33233Sbill rpp->p_szpt = rip->p_szpt; 33333Sbill forkstat.cntfork++; 33433Sbill forkstat.sizfork += rip->p_dsize + rip->p_ssize; 33533Sbill } 33633Sbill rpp->p_rssize = 0; 337*3514Sroot rpp->p_maxrss = rip->p_maxrss; 33833Sbill rpp->p_wchan = 0; 33933Sbill rpp->p_slptime = 0; 3401400Sbill rpp->p_pctcpu = 0; 3411400Sbill rpp->p_cpticks = 0; 34233Sbill n = PIDHASH(rpp->p_pid); 34333Sbill p->p_idhash = pidhash[n]; 34433Sbill pidhash[n] = rpp - proc; 34533Sbill 34633Sbill /* 34733Sbill * make duplicate entries 34833Sbill * where needed 34933Sbill */ 35033Sbill 35133Sbill multprog++; 35233Sbill 35333Sbill for(n=0; n<NOFILE; n++) 35433Sbill if(u.u_ofile[n] != NULL) { 3552938Swnj u.u_ofile[n]->f_count++; 3562938Swnj if(!isvfork && u.u_vrpages[n]) 3572938Swnj u.u_ofile[n]->f_inode->i_vfdcnt++; 35833Sbill } 35933Sbill 36033Sbill u.u_cdir->i_count++; 36133Sbill if (u.u_rdir) 36233Sbill u.u_rdir->i_count++; 36333Sbill /* 36433Sbill * Partially simulate the environment 36533Sbill * of the new process so that when it is actually 36633Sbill * created (by copying) it will look right. 36733Sbill */ 36833Sbill 36933Sbill rip->p_flag |= SKEEP; /* prevent parent from being swapped */ 37033Sbill 37133Sbill if (procdup(rpp, isvfork)) 37233Sbill return (1); 37333Sbill 3741788Sbill (void) spl6(); 37533Sbill rpp->p_stat = SRUN; 37633Sbill setrq(rpp); 3771793Sbill (void) spl0(); 37888Sbill /* SSWAP NOT NEEDED IN THIS CASE AS u.u_pcb.pcb_sswap SUFFICES */ 37933Sbill /* rpp->p_flag |= SSWAP; */ 38033Sbill rip->p_flag &= ~SKEEP; 38133Sbill if (isvfork) { 38233Sbill u.u_procp->p_xlink = rpp; 38333Sbill u.u_procp->p_flag |= SNOVM; 38433Sbill while (rpp->p_flag & SVFORK) 38533Sbill sleep((caddr_t)rpp, PZERO - 1); 38633Sbill if ((rpp->p_flag & SLOAD) == 0) 38733Sbill panic("newproc vfork"); 38833Sbill uaccess(rpp, Vfmap, &vfutl); 38933Sbill u.u_procp->p_xlink = 0; 39033Sbill vpassvm(rpp, u.u_procp, &vfutl, &u, Vfmap); 39133Sbill for (n = 0; n < NOFILE; n++) 39233Sbill if (vfutl.u_vrpages[n]) { 39333Sbill if ((u.u_vrpages[n] = vfutl.u_vrpages[n] - 1) == 0) 39433Sbill if (--u.u_ofile[n]->f_inode->i_vfdcnt < 0) 39533Sbill panic("newproc i_vfdcnt"); 39633Sbill vfutl.u_vrpages[n] = 0; 39733Sbill } 39833Sbill u.u_procp->p_flag &= ~SNOVM; 39933Sbill rpp->p_ndx = rpp - proc; 40033Sbill rpp->p_flag |= SVFDONE; 40133Sbill wakeup((caddr_t)rpp); 40233Sbill } 40333Sbill return (0); 40433Sbill } 405