xref: /csrg-svn/sys/kern/kern_synch.c (revision 187)
1*187Sbill /*	kern_synch.c	3.9	10/14/12	*/
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"
1333Sbill 
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 {
33*187Sbill 	register struct proc *rp, **hp;
3433Sbill 	register s, h;
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;
43*187Sbill 	hp = &slpque[HASH(chan)];
44*187Sbill 	rp->p_link = *hp;
45*187Sbill 	*hp = rp;
4633Sbill 	if(pri > PZERO) {
47181Sbill 		if(ISSIG(rp)) {
48*187Sbill 			if (rp->p_wchan)
49*187Sbill 				unsleep(rp);
5033Sbill 			rp->p_stat = SRUN;
51131Sbill 			(void) spl0();
5233Sbill 			goto psig;
5333Sbill 		}
54*187Sbill 		if (rp->p_wchan == 0)
55*187Sbill 			goto out;
56*187Sbill 		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 {
66131Sbill 		(void) spl0();
6733Sbill 		swtch();
6833Sbill 	}
69*187Sbill out:
7033Sbill 	splx(s);
7133Sbill 	return;
7233Sbill 
7333Sbill 	/*
7433Sbill 	 * If priority was low (>PZERO) and
7533Sbill 	 * there has been a signal,
7633Sbill 	 * execute non-local goto to
7733Sbill 	 * the qsav location.
7833Sbill 	 * (see trap1/trap.c)
7933Sbill 	 */
8033Sbill psig:
8133Sbill 	longjmp(u.u_qsav);
8233Sbill 	/*NOTREACHED*/
8333Sbill }
8433Sbill 
8533Sbill /*
86100Sbill  * Sleep on chan at pri.
87100Sbill  * Return in no more than the indicated number of seconds.
88100Sbill  * (If seconds==0, no timeout implied)
89100Sbill  * Return	TS_OK if chan was awakened normally
90100Sbill  *		TS_TIME if timeout occurred
91100Sbill  *		TS_SIG if asynchronous signal occurred
92100Sbill  */
93100Sbill tsleep(chan, pri, seconds)
94100Sbill caddr_t chan;
95100Sbill {
96100Sbill 	label_t lqsav;
97100Sbill 	register struct proc *pp;
98100Sbill 	register sec, n, rval;
99100Sbill 
100100Sbill 	pp = u.u_procp;
101102Sbill 	n = spl7();
102100Sbill 	sec = 0;
103100Sbill 	rval = 0;
104100Sbill 	if (pp->p_clktim && pp->p_clktim<seconds)
105100Sbill 		seconds = 0;
106100Sbill 	if (seconds) {
107100Sbill 		pp->p_flag |= STIMO;
108103Sbill 		sec = pp->p_clktim-seconds;
109100Sbill 		pp->p_clktim = seconds;
110100Sbill 	}
111100Sbill 	bcopy((caddr_t)u.u_qsav, (caddr_t)lqsav, sizeof (label_t));
112100Sbill 	if (setjmp(u.u_qsav))
113100Sbill 		rval = TS_SIG;
114100Sbill 	else {
115100Sbill 		sleep(chan, pri);
116100Sbill 		if ((pp->p_flag&STIMO)==0 && seconds)
117100Sbill 			rval = TS_TIME;
118100Sbill 		else
119100Sbill 			rval = TS_OK;
120100Sbill 	}
121100Sbill 	pp->p_flag &= ~STIMO;
122100Sbill 	bcopy((caddr_t)lqsav, (caddr_t)u.u_qsav, sizeof (label_t));
123103Sbill 	if (sec > 0)
124103Sbill 		pp->p_clktim += sec;
125103Sbill 	else
126103Sbill 		pp->p_clktim = 0;
127100Sbill 	splx(n);
128100Sbill 	return(rval);
129100Sbill }
130100Sbill 
131100Sbill /*
132181Sbill  * Remove a process from its wait queue
133181Sbill  */
134181Sbill unsleep(p)
135181Sbill register struct proc *p;
136181Sbill {
137181Sbill 	register struct proc **hp;
138181Sbill 	register s;
139181Sbill 
140181Sbill 	s = spl6();
141181Sbill 	if (p->p_wchan) {
142181Sbill 		hp = &slpque[HASH(p->p_wchan)];
143181Sbill 		while (*hp != p)
144181Sbill 			hp = &(*hp)->p_link;
145181Sbill 		*hp = p->p_link;
146181Sbill 		p->p_wchan = 0;
147181Sbill 	}
148181Sbill 	splx(s);
149181Sbill }
150181Sbill 
151181Sbill /*
15233Sbill  * Wake up all processes sleeping on chan.
15333Sbill  */
15433Sbill wakeup(chan)
15533Sbill register caddr_t chan;
15633Sbill {
157*187Sbill 	register struct proc *p, **q, **h;
15833Sbill 	int s;
15933Sbill 
16033Sbill 	s = spl6();
161*187Sbill 	h = &slpque[HASH(chan)];
16233Sbill restart:
163*187Sbill 	for (q = h; p = *q; ) {
164181Sbill 		if (p->p_rlink || p->p_stat != SSLEEP && p->p_stat != SSTOP)
16533Sbill 			panic("wakeup");
16633Sbill 		if (p->p_wchan==chan && p->p_stat!=SZOMB) {
16733Sbill 			p->p_wchan = 0;
168*187Sbill 			*q = p->p_link;
16933Sbill 			p->p_slptime = 0;
170181Sbill 			if (p->p_stat == SSLEEP) {
171181Sbill 				/* OPTIMIZED INLINE EXPANSION OF setrun(p) */
172181Sbill 				p->p_stat = SRUN;
173181Sbill 				if (p->p_flag & SLOAD) {
17433Sbill #ifndef FASTVAX
175181Sbill 					p->p_link = runq;
176181Sbill 					runq = p->p_link;
17733Sbill #else
178181Sbill 					setrq(p);
17933Sbill #endif
180181Sbill 				}
181181Sbill 				if(p->p_pri < curpri)
182181Sbill 					runrun++;
183181Sbill 				if(runout != 0 && (p->p_flag&SLOAD) == 0) {
184181Sbill 					runout = 0;
185181Sbill 					wakeup((caddr_t)&runout);
186181Sbill 				}
187181Sbill 				/* END INLINE EXPANSION */
188*187Sbill 				goto restart;
18933Sbill 			}
190*187Sbill 		} else
191*187Sbill 			q = &p->p_link;
19233Sbill 	}
19333Sbill 	splx(s);
19433Sbill }
19533Sbill 
19633Sbill #ifdef FASTVAX
19733Sbill /*
19833Sbill  * Initialize the (doubly-linked) run queues
19933Sbill  * to be empty.
20033Sbill  */
20133Sbill rqinit()
20233Sbill {
20333Sbill 	register int i;
20433Sbill 
20533Sbill 	for (i = 0; i < NQS; i++)
20633Sbill 		qs[i].ph_link = qs[i].ph_rlink = (struct proc *)&qs[i];
20733Sbill }
20833Sbill #endif
20933Sbill 
21033Sbill /*
21133Sbill  * Set the process running;
21233Sbill  * arrange for it to be swapped in if necessary.
21333Sbill  */
21433Sbill setrun(p)
21533Sbill register struct proc *p;
21633Sbill {
21733Sbill 	register caddr_t w;
21833Sbill 	register s;
21933Sbill 
22033Sbill 	s = spl6();
22133Sbill 	switch (p->p_stat) {
22233Sbill 
22333Sbill 	case 0:
22433Sbill 	case SWAIT:
22533Sbill 	case SRUN:
22633Sbill 	case SZOMB:
22733Sbill 	default:
22833Sbill 		panic("setrun");
22933Sbill 
23033Sbill 	case SSLEEP:
231181Sbill 		unsleep(p);		/* e.g. when sending signals */
23233Sbill 		break;
23333Sbill 
23433Sbill 	case SIDL:
23533Sbill 	case SSTOP:
23633Sbill 		break;
23733Sbill 	}
23833Sbill 	p->p_stat = SRUN;
23933Sbill 	if (p->p_flag & SLOAD)
24033Sbill 		setrq(p);
24133Sbill 	splx(s);
24233Sbill 	if(p->p_pri < curpri)
24333Sbill 		runrun++;
24433Sbill 	if(runout != 0 && (p->p_flag&SLOAD) == 0) {
24533Sbill 		runout = 0;
24633Sbill 		wakeup((caddr_t)&runout);
24733Sbill 	}
24833Sbill }
24933Sbill 
25033Sbill /*
25133Sbill  * Set user priority.
25233Sbill  * The rescheduling flag (runrun)
25333Sbill  * is set if the priority is better
25433Sbill  * than the currently running process.
25533Sbill  */
25633Sbill setpri(pp)
25733Sbill register struct proc *pp;
25833Sbill {
25933Sbill 	register p;
26033Sbill 
26133Sbill 	p = (pp->p_cpu & 0377)/16;
26233Sbill 	p += PUSER + pp->p_nice - NZERO;
26333Sbill 	if(p > 127)
26433Sbill 		p = 127;
26533Sbill 	if(p < curpri)
26633Sbill 		runrun++;
26733Sbill 	pp->p_usrpri = p;
26833Sbill 	return(p);
26933Sbill }
27033Sbill 
27133Sbill /*
27233Sbill  * Create a new process-- the internal version of
27333Sbill  * sys fork.
27433Sbill  * It returns 1 in the new process, 0 in the old.
27533Sbill  */
27633Sbill newproc(isvfork)
27733Sbill {
27833Sbill 	register struct proc *p;
27933Sbill 	register struct proc *rpp, *rip;
28033Sbill 	register int n;
28133Sbill 
28233Sbill 	p = NULL;
28333Sbill 	/*
28433Sbill 	 * First, just locate a slot for a process
28533Sbill 	 * and copy the useful info from this process into it.
28633Sbill 	 * The panic "cannot happen" because fork has already
28733Sbill 	 * checked for the existence of a slot.
28833Sbill 	 */
28933Sbill retry:
29033Sbill 	mpid++;
29133Sbill 	if(mpid >= 30000) {
29233Sbill 		mpid = 0;
29333Sbill 		goto retry;
29433Sbill 	}
29533Sbill 	for(rpp = &proc[0]; rpp < &proc[NPROC]; rpp++) {
29633Sbill 		if(rpp->p_stat == NULL && p==NULL)
29733Sbill 			p = rpp;
29833Sbill 		if (rpp->p_pid==mpid || rpp->p_pgrp==mpid)
29933Sbill 			goto retry;
30033Sbill 	}
30133Sbill 	if ((rpp = p)==NULL)
30233Sbill 		panic("no procs");
30333Sbill 
30433Sbill 	/*
30533Sbill 	 * make proc entry for new proc
30633Sbill 	 */
30733Sbill 
30833Sbill 	rip = u.u_procp;
30933Sbill 	rpp->p_stat = SIDL;
31033Sbill 	rpp->p_clktim = 0;
31133Sbill 	rpp->p_flag = SLOAD | (rip->p_flag & SPAGI);
31233Sbill 	if (isvfork) {
31333Sbill 		rpp->p_flag |= SVFORK;
31433Sbill 		rpp->p_ndx = rip->p_ndx;
31533Sbill 	} else
31633Sbill 		rpp->p_ndx = rpp - proc;
31733Sbill 	rpp->p_uid = rip->p_uid;
31833Sbill 	rpp->p_pgrp = rip->p_pgrp;
31933Sbill 	rpp->p_nice = rip->p_nice;
32033Sbill 	rpp->p_textp = isvfork ? 0 : rip->p_textp;
32133Sbill 	rpp->p_pid = mpid;
32233Sbill 	rpp->p_ppid = rip->p_pid;
323181Sbill 	rpp->p_pptr = rip;
32433Sbill 	rpp->p_time = 0;
32533Sbill 	rpp->p_cpu = 0;
326181Sbill 	rpp->p_siga0 = rip->p_siga0;
327181Sbill 	rpp->p_siga1 = rip->p_siga1;
328181Sbill 	/* take along any pending signals, like stops? */
32933Sbill 	if (isvfork) {
33033Sbill 		rpp->p_tsize = rpp->p_dsize = rpp->p_ssize = 0;
33133Sbill 		rpp->p_szpt = clrnd(ctopt(UPAGES));
33233Sbill 		forkstat.cntvfork++;
33333Sbill 		forkstat.sizvfork += rip->p_dsize + rip->p_ssize;
33433Sbill 	} else {
33533Sbill 		rpp->p_tsize = rip->p_tsize;
33633Sbill 		rpp->p_dsize = rip->p_dsize;
33733Sbill 		rpp->p_ssize = rip->p_ssize;
33833Sbill 		rpp->p_szpt = rip->p_szpt;
33933Sbill 		forkstat.cntfork++;
34033Sbill 		forkstat.sizfork += rip->p_dsize + rip->p_ssize;
34133Sbill 	}
34233Sbill 	rpp->p_rssize = 0;
34333Sbill 	rpp->p_wchan = 0;
34433Sbill 	rpp->p_slptime = 0;
34533Sbill 	rpp->p_aveflt = rip->p_aveflt;
34633Sbill 	rate.v_pgin += rip->p_aveflt;
34733Sbill 	rpp->p_faults = 0;
34833Sbill 	n = PIDHASH(rpp->p_pid);
34933Sbill 	p->p_idhash = pidhash[n];
35033Sbill 	pidhash[n] = rpp - proc;
35133Sbill 
35233Sbill 	/*
35333Sbill 	 * make duplicate entries
35433Sbill 	 * where needed
35533Sbill 	 */
35633Sbill 
35733Sbill 	multprog++;
35833Sbill 
35933Sbill 	for(n=0; n<NOFILE; n++)
36033Sbill 		if(u.u_ofile[n] != NULL) {
36133Sbill 			u.u_ofile[n]->f_count++;
36233Sbill 			if(!isvfork && u.u_vrpages[n])
36333Sbill 				u.u_ofile[n]->f_inode->i_vfdcnt++;
36433Sbill 		}
36533Sbill 
36633Sbill 	u.u_cdir->i_count++;
36733Sbill 	if (u.u_rdir)
36833Sbill 		u.u_rdir->i_count++;
36933Sbill 	/*
37033Sbill 	 * Partially simulate the environment
37133Sbill 	 * of the new process so that when it is actually
37233Sbill 	 * created (by copying) it will look right.
37333Sbill 	 */
37433Sbill 
37533Sbill 	rip->p_flag |= SKEEP;	/* prevent parent from being swapped */
37633Sbill 
37733Sbill 	if (procdup(rpp, isvfork))
37833Sbill 		return (1);
37933Sbill 
38033Sbill 	spl6();
38133Sbill 	rpp->p_stat = SRUN;
38233Sbill 	setrq(rpp);
38333Sbill 	spl0();
38488Sbill 	/* SSWAP NOT NEEDED IN THIS CASE AS u.u_pcb.pcb_sswap SUFFICES */
38533Sbill 	/* rpp->p_flag |= SSWAP; */
38633Sbill 	rip->p_flag &= ~SKEEP;
38733Sbill 	if (isvfork) {
38833Sbill 		u.u_procp->p_xlink = rpp;
38933Sbill 		u.u_procp->p_flag |= SNOVM;
39033Sbill 		while (rpp->p_flag & SVFORK)
39133Sbill 			sleep((caddr_t)rpp, PZERO - 1);
39233Sbill 		if ((rpp->p_flag & SLOAD) == 0)
39333Sbill 			panic("newproc vfork");
39433Sbill 		uaccess(rpp, Vfmap, &vfutl);
39533Sbill 		u.u_procp->p_xlink = 0;
39633Sbill 		vpassvm(rpp, u.u_procp, &vfutl, &u, Vfmap);
39733Sbill 		for (n = 0; n < NOFILE; n++)
39833Sbill 			if (vfutl.u_vrpages[n]) {
39933Sbill 				if ((u.u_vrpages[n] = vfutl.u_vrpages[n] - 1) == 0)
40033Sbill 					if (--u.u_ofile[n]->f_inode->i_vfdcnt < 0)
40133Sbill 						panic("newproc i_vfdcnt");
40233Sbill 				vfutl.u_vrpages[n] = 0;
40333Sbill 			}
40433Sbill 		u.u_procp->p_flag &= ~SNOVM;
40533Sbill 		rpp->p_ndx = rpp - proc;
40633Sbill 		rpp->p_flag |= SVFDONE;
40733Sbill 		wakeup((caddr_t)rpp);
40833Sbill 	}
40933Sbill 	return (0);
41033Sbill }
411