1*2443Swnj /* kern_synch.c 4.5 02/15/81 */ 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" 13*2443Swnj #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 if(runin != 0) { 5933Sbill runin = 0; 6033Sbill wakeup((caddr_t)&runin); 6133Sbill } 6233Sbill swtch(); 63181Sbill if(ISSIG(rp)) 6433Sbill goto psig; 6533Sbill } else { 66207Sbill rp->p_stat = SSLEEP; 67131Sbill (void) spl0(); 6833Sbill swtch(); 6933Sbill } 70187Sbill out: 7133Sbill splx(s); 7233Sbill return; 7333Sbill 7433Sbill /* 7533Sbill * If priority was low (>PZERO) and 7633Sbill * there has been a signal, 7733Sbill * execute non-local goto to 7833Sbill * the qsav location. 7933Sbill * (see trap1/trap.c) 8033Sbill */ 8133Sbill psig: 8233Sbill longjmp(u.u_qsav); 8333Sbill /*NOTREACHED*/ 8433Sbill } 8533Sbill 8633Sbill /* 87100Sbill * Sleep on chan at pri. 88100Sbill * Return in no more than the indicated number of seconds. 89100Sbill * (If seconds==0, no timeout implied) 90100Sbill * Return TS_OK if chan was awakened normally 91100Sbill * TS_TIME if timeout occurred 92100Sbill * TS_SIG if asynchronous signal occurred 93100Sbill */ 94100Sbill tsleep(chan, pri, seconds) 95100Sbill caddr_t chan; 96100Sbill { 97100Sbill label_t lqsav; 98100Sbill register struct proc *pp; 99100Sbill register sec, n, rval; 100100Sbill 101100Sbill pp = u.u_procp; 102102Sbill n = spl7(); 103100Sbill sec = 0; 104100Sbill rval = 0; 105100Sbill if (pp->p_clktim && pp->p_clktim<seconds) 106100Sbill seconds = 0; 107100Sbill if (seconds) { 108100Sbill pp->p_flag |= STIMO; 109103Sbill sec = pp->p_clktim-seconds; 110100Sbill pp->p_clktim = seconds; 111100Sbill } 112100Sbill bcopy((caddr_t)u.u_qsav, (caddr_t)lqsav, sizeof (label_t)); 113100Sbill if (setjmp(u.u_qsav)) 114100Sbill rval = TS_SIG; 115100Sbill else { 116100Sbill sleep(chan, pri); 117100Sbill if ((pp->p_flag&STIMO)==0 && seconds) 118100Sbill rval = TS_TIME; 119100Sbill else 120100Sbill rval = TS_OK; 121100Sbill } 122100Sbill pp->p_flag &= ~STIMO; 123100Sbill bcopy((caddr_t)lqsav, (caddr_t)u.u_qsav, sizeof (label_t)); 124103Sbill if (sec > 0) 125103Sbill pp->p_clktim += sec; 126103Sbill else 127103Sbill pp->p_clktim = 0; 128100Sbill splx(n); 129100Sbill return(rval); 130100Sbill } 131100Sbill 132100Sbill /* 133181Sbill * Remove a process from its wait queue 134181Sbill */ 135181Sbill unsleep(p) 136181Sbill register struct proc *p; 137181Sbill { 138181Sbill register struct proc **hp; 139181Sbill register s; 140181Sbill 141181Sbill s = spl6(); 142181Sbill if (p->p_wchan) { 143181Sbill hp = &slpque[HASH(p->p_wchan)]; 144181Sbill while (*hp != p) 145181Sbill hp = &(*hp)->p_link; 146181Sbill *hp = p->p_link; 147181Sbill p->p_wchan = 0; 148181Sbill } 149181Sbill splx(s); 150181Sbill } 151181Sbill 152181Sbill /* 15333Sbill * Wake up all processes sleeping on chan. 15433Sbill */ 15533Sbill wakeup(chan) 15633Sbill register caddr_t chan; 15733Sbill { 158187Sbill register struct proc *p, **q, **h; 15933Sbill int s; 16033Sbill 16133Sbill s = spl6(); 162187Sbill h = &slpque[HASH(chan)]; 16333Sbill restart: 164187Sbill for (q = h; p = *q; ) { 165181Sbill if (p->p_rlink || p->p_stat != SSLEEP && p->p_stat != SSTOP) 16633Sbill panic("wakeup"); 167207Sbill if (p->p_wchan==chan) { 16833Sbill p->p_wchan = 0; 169187Sbill *q = p->p_link; 17033Sbill p->p_slptime = 0; 171181Sbill if (p->p_stat == SSLEEP) { 172181Sbill /* OPTIMIZED INLINE EXPANSION OF setrun(p) */ 173181Sbill p->p_stat = SRUN; 174181Sbill if (p->p_flag & SLOAD) { 17533Sbill #ifndef FASTVAX 176181Sbill p->p_link = runq; 177181Sbill runq = p->p_link; 17833Sbill #else 179181Sbill setrq(p); 18033Sbill #endif 181181Sbill } 182*2443Swnj if(p->p_pri < curpri) { 183181Sbill runrun++; 184*2443Swnj aston(); 185*2443Swnj } 186181Sbill if(runout != 0 && (p->p_flag&SLOAD) == 0) { 187181Sbill runout = 0; 188181Sbill wakeup((caddr_t)&runout); 189181Sbill } 190181Sbill /* END INLINE EXPANSION */ 191187Sbill goto restart; 19233Sbill } 193187Sbill } else 194187Sbill q = &p->p_link; 19533Sbill } 19633Sbill splx(s); 19733Sbill } 19833Sbill 19933Sbill #ifdef FASTVAX 20033Sbill /* 20133Sbill * Initialize the (doubly-linked) run queues 20233Sbill * to be empty. 20333Sbill */ 20433Sbill rqinit() 20533Sbill { 20633Sbill register int i; 20733Sbill 20833Sbill for (i = 0; i < NQS; i++) 20933Sbill qs[i].ph_link = qs[i].ph_rlink = (struct proc *)&qs[i]; 21033Sbill } 21133Sbill #endif 21233Sbill 21333Sbill /* 21433Sbill * Set the process running; 21533Sbill * arrange for it to be swapped in if necessary. 21633Sbill */ 21733Sbill setrun(p) 21833Sbill register struct proc *p; 21933Sbill { 22033Sbill register s; 22133Sbill 22233Sbill s = spl6(); 22333Sbill switch (p->p_stat) { 22433Sbill 22533Sbill case 0: 22633Sbill case SWAIT: 22733Sbill case SRUN: 22833Sbill case SZOMB: 22933Sbill default: 23033Sbill panic("setrun"); 23133Sbill 232207Sbill case SSTOP: 23333Sbill case SSLEEP: 234181Sbill unsleep(p); /* e.g. when sending signals */ 23533Sbill break; 23633Sbill 23733Sbill case SIDL: 23833Sbill break; 23933Sbill } 24033Sbill p->p_stat = SRUN; 24133Sbill if (p->p_flag & SLOAD) 24233Sbill setrq(p); 24333Sbill splx(s); 244*2443Swnj if(p->p_pri < curpri) { 24533Sbill runrun++; 246*2443Swnj aston(); 247*2443Swnj } 24833Sbill if(runout != 0 && (p->p_flag&SLOAD) == 0) { 24933Sbill runout = 0; 25033Sbill wakeup((caddr_t)&runout); 25133Sbill } 25233Sbill } 25333Sbill 25433Sbill /* 25533Sbill * Set user priority. 25633Sbill * The rescheduling flag (runrun) 25733Sbill * is set if the priority is better 25833Sbill * than the currently running process. 25933Sbill */ 26033Sbill setpri(pp) 26133Sbill register struct proc *pp; 26233Sbill { 26333Sbill register p; 26433Sbill 26533Sbill p = (pp->p_cpu & 0377)/16; 2661543Sbill p += PUSER + 2*(pp->p_nice - NZERO); 26733Sbill if(p > 127) 26833Sbill p = 127; 26933Sbill if(p < curpri) 27033Sbill runrun++; 27133Sbill pp->p_usrpri = p; 27233Sbill return(p); 27333Sbill } 27433Sbill 27533Sbill /* 27633Sbill * Create a new process-- the internal version of 27733Sbill * sys fork. 27833Sbill * It returns 1 in the new process, 0 in the old. 27933Sbill */ 28033Sbill newproc(isvfork) 28133Sbill { 28233Sbill register struct proc *p; 28333Sbill register struct proc *rpp, *rip; 28433Sbill register int n; 28533Sbill 28633Sbill p = NULL; 28733Sbill /* 28833Sbill * First, just locate a slot for a process 28933Sbill * and copy the useful info from this process into it. 29033Sbill * The panic "cannot happen" because fork has already 29133Sbill * checked for the existence of a slot. 29233Sbill */ 29333Sbill retry: 29433Sbill mpid++; 29533Sbill if(mpid >= 30000) { 29633Sbill mpid = 0; 29733Sbill goto retry; 29833Sbill } 29933Sbill for(rpp = &proc[0]; rpp < &proc[NPROC]; rpp++) { 30033Sbill if(rpp->p_stat == NULL && p==NULL) 30133Sbill p = rpp; 30233Sbill if (rpp->p_pid==mpid || rpp->p_pgrp==mpid) 30333Sbill goto retry; 30433Sbill } 30533Sbill if ((rpp = p)==NULL) 30633Sbill panic("no procs"); 30733Sbill 30833Sbill /* 30933Sbill * make proc entry for new proc 31033Sbill */ 31133Sbill 31233Sbill rip = u.u_procp; 31333Sbill rpp->p_stat = SIDL; 31433Sbill rpp->p_clktim = 0; 315913Sbill rpp->p_flag = SLOAD | (rip->p_flag & (SPAGI|SDETACH|SNUSIG)); 31633Sbill if (isvfork) { 31733Sbill rpp->p_flag |= SVFORK; 31833Sbill rpp->p_ndx = rip->p_ndx; 31933Sbill } else 32033Sbill rpp->p_ndx = rpp - proc; 32133Sbill rpp->p_uid = rip->p_uid; 32233Sbill rpp->p_pgrp = rip->p_pgrp; 32333Sbill rpp->p_nice = rip->p_nice; 32433Sbill rpp->p_textp = isvfork ? 0 : rip->p_textp; 32533Sbill rpp->p_pid = mpid; 32633Sbill rpp->p_ppid = rip->p_pid; 327181Sbill rpp->p_pptr = rip; 32833Sbill rpp->p_time = 0; 32933Sbill rpp->p_cpu = 0; 330181Sbill rpp->p_siga0 = rip->p_siga0; 331181Sbill rpp->p_siga1 = rip->p_siga1; 332181Sbill /* take along any pending signals, like stops? */ 33333Sbill if (isvfork) { 33433Sbill rpp->p_tsize = rpp->p_dsize = rpp->p_ssize = 0; 33533Sbill rpp->p_szpt = clrnd(ctopt(UPAGES)); 33633Sbill forkstat.cntvfork++; 33733Sbill forkstat.sizvfork += rip->p_dsize + rip->p_ssize; 33833Sbill } else { 33933Sbill rpp->p_tsize = rip->p_tsize; 34033Sbill rpp->p_dsize = rip->p_dsize; 34133Sbill rpp->p_ssize = rip->p_ssize; 34233Sbill rpp->p_szpt = rip->p_szpt; 34333Sbill forkstat.cntfork++; 34433Sbill forkstat.sizfork += rip->p_dsize + rip->p_ssize; 34533Sbill } 34633Sbill rpp->p_rssize = 0; 34733Sbill rpp->p_wchan = 0; 34833Sbill rpp->p_slptime = 0; 3491400Sbill rpp->p_pctcpu = 0; 3501400Sbill rpp->p_cpticks = 0; 35133Sbill n = PIDHASH(rpp->p_pid); 35233Sbill p->p_idhash = pidhash[n]; 35333Sbill pidhash[n] = rpp - proc; 35433Sbill 35533Sbill /* 35633Sbill * make duplicate entries 35733Sbill * where needed 35833Sbill */ 35933Sbill 36033Sbill multprog++; 36133Sbill 36233Sbill for(n=0; n<NOFILE; n++) 36333Sbill if(u.u_ofile[n] != NULL) { 3642329Swnj #ifdef UCBIPC 3652329Swnj if (u.u_pofile[n] & ISPORT) 3662329Swnj u.u_oport[n]->pt_count++; 3672329Swnj else { 3682329Swnj #endif 3692329Swnj u.u_ofile[n]->f_count++; 3702329Swnj if(!isvfork && u.u_vrpages[n]) 3712329Swnj u.u_ofile[n]->f_inode->i_vfdcnt++; 3722329Swnj #ifdef UCBIPC 3732329Swnj } 3742329Swnj #endif UCBIPC 37533Sbill } 37633Sbill 37733Sbill u.u_cdir->i_count++; 37833Sbill if (u.u_rdir) 37933Sbill u.u_rdir->i_count++; 38033Sbill /* 38133Sbill * Partially simulate the environment 38233Sbill * of the new process so that when it is actually 38333Sbill * created (by copying) it will look right. 38433Sbill */ 38533Sbill 38633Sbill rip->p_flag |= SKEEP; /* prevent parent from being swapped */ 38733Sbill 38833Sbill if (procdup(rpp, isvfork)) 38933Sbill return (1); 39033Sbill 3911788Sbill (void) spl6(); 39233Sbill rpp->p_stat = SRUN; 39333Sbill setrq(rpp); 3941793Sbill (void) spl0(); 39588Sbill /* SSWAP NOT NEEDED IN THIS CASE AS u.u_pcb.pcb_sswap SUFFICES */ 39633Sbill /* rpp->p_flag |= SSWAP; */ 39733Sbill rip->p_flag &= ~SKEEP; 39833Sbill if (isvfork) { 39933Sbill u.u_procp->p_xlink = rpp; 40033Sbill u.u_procp->p_flag |= SNOVM; 40133Sbill while (rpp->p_flag & SVFORK) 40233Sbill sleep((caddr_t)rpp, PZERO - 1); 40333Sbill if ((rpp->p_flag & SLOAD) == 0) 40433Sbill panic("newproc vfork"); 40533Sbill uaccess(rpp, Vfmap, &vfutl); 40633Sbill u.u_procp->p_xlink = 0; 40733Sbill vpassvm(rpp, u.u_procp, &vfutl, &u, Vfmap); 40833Sbill for (n = 0; n < NOFILE; n++) 40933Sbill if (vfutl.u_vrpages[n]) { 41033Sbill if ((u.u_vrpages[n] = vfutl.u_vrpages[n] - 1) == 0) 41133Sbill if (--u.u_ofile[n]->f_inode->i_vfdcnt < 0) 41233Sbill panic("newproc i_vfdcnt"); 41333Sbill vfutl.u_vrpages[n] = 0; 41433Sbill } 41533Sbill u.u_procp->p_flag &= ~SNOVM; 41633Sbill rpp->p_ndx = rpp - proc; 41733Sbill rpp->p_flag |= SVFDONE; 41833Sbill wakeup((caddr_t)rpp); 41933Sbill } 42033Sbill return (0); 42133Sbill } 422