xref: /original-bsd/sys/tahoe/tahoe/machdep.c (revision 89a39cb6)
1 /*
2  * Copyright (c) 1982,1987,1988 Regents of the University of California.
3  * All rights reserved.  The Berkeley software License Agreement
4  * specifies the terms and conditions for redistribution.
5  *
6  *	@(#)machdep.c	7.12 (Berkeley) 06/21/90
7  */
8 
9 #include "param.h"
10 #include "systm.h"
11 /* #include "user.h" */
12 #include "syscontext.h"
13 #include "kernel.h"
14 #include "map.h"
15 #include "vm.h"
16 #include "proc.h"
17 #include "buf.h"
18 #include "reboot.h"
19 #include "conf.h"
20 #include "file.h"
21 #include "text.h"
22 #include "clist.h"
23 #include "callout.h"
24 #include "cmap.h"
25 #include "malloc.h"
26 #include "mbuf.h"
27 #include "msgbuf.h"
28 #ifdef SYSVSHM
29 #include "shm.h"
30 #endif
31 
32 #include "cpu.h"
33 #include "reg.h"
34 #include "pte.h"
35 #include "psl.h"
36 #include "mem.h"
37 #include "mtpr.h"
38 #include "cp.h"
39 
40 #include "../tahoevba/vbavar.h"
41 
42 /*
43  * Declare these as initialized data so we can patch them.
44  */
45 int	nswbuf = 0;
46 #ifdef	NBUF
47 int	nbuf = NBUF;
48 #else
49 int	nbuf = 0;
50 #endif
51 #ifdef	BUFPAGES
52 int	bufpages = BUFPAGES;
53 #else
54 int	bufpages = 0;
55 #endif
56 #include "yc.h"
57 #if NCY > 0
58 #include "../tahoevba/cyreg.h"
59 #endif
60 int	msgbufmapped;		/* set when safe to use msgbuf */
61 int	physmem = MAXMEM;	/* max supported memory, changes to actual */
62 
63 /*
64  * Machine-dependent startup code
65  */
66 startup(firstaddr)
67 	int firstaddr;
68 {
69 	register int unixsize;
70 	register unsigned i;
71 	register struct pte *pte;
72 	int mapaddr, j;
73 	register caddr_t v;
74 	int maxbufs, base, residual;
75 
76 	/*
77 	 * Initialize error message buffer (at end of core).
78 	 */
79 	maxmem = physmem - btoc(sizeof (struct msgbuf));
80 	pte = msgbufmap;
81 	for (i = 1; i < btoc(sizeof (struct msgbuf)) + 1; i++)
82 		*(int *)pte++ = PG_V | PG_KW | (physmem - i);
83 	mtpr(TBIA, 1);
84 	msgbufmapped = 1;
85 #ifdef KADB
86 	kdb_init();			/* startup kernel debugger */
87 	(void) cnopen(makedev(0, 1), 0);	/* open console XXX */
88 #endif
89 	/*
90 	 * Good {morning,afternoon,evening,night}.
91 	 */
92 	printf(version);
93 	printf("real mem = %d\n", ctob(physmem));
94 
95 	/*
96 	 * Allocate space for system data structures.
97 	 * The first available real memory address is in "firstaddr".
98 	 * The first available kernel virtual address is in "v".
99 	 * As pages of kernel virtual memory are allocated, "v" is incremented.
100 	 * As pages of memory are allocated and cleared,
101 	 * "firstaddr" is incremented.
102 	 * An index into the kernel page table corresponding to the
103 	 * virtual memory address maintained in "v" is kept in "mapaddr".
104 	 */
105 	v = (caddr_t)(0xc0000000 | (firstaddr * NBPG));
106 #define	valloc(name, type, num) \
107 	    (name) = (type *)v; v = (caddr_t)((name)+(num))
108 #define	valloclim(name, type, num, lim) \
109 	    (name) = (type *)v; v = (caddr_t)((lim) = ((name)+(num)))
110 #if NCY > 0
111 	/*
112 	 * Allocate raw buffers for tapemaster controllers
113 	 * first, as they need buffers in the first megabyte.
114 	 */
115 	valloc(cybuf, char, NCY * CYMAXIO);
116 #endif
117 	valloclim(file, struct file, nfile, fileNFILE);
118 	valloclim(proc, struct proc, nproc, procNPROC);
119 	valloclim(text, struct text, ntext, textNTEXT);
120 	valloc(cfree, struct cblock, nclist);
121 	valloc(callout, struct callout, ncallout);
122 	valloc(swapmap, struct map, nswapmap = nproc * 2);
123 	valloc(argmap, struct map, ARGMAPSIZE);
124 	valloc(kernelmap, struct map, nproc);
125 	valloc(mbmap, struct map, nmbclusters/4);
126 	valloc(kmemmap, struct map, ekmempt - kmempt);
127 	valloc(kmemusage, struct kmemusage, ekmempt - kmempt);
128 #ifdef SYSVSHM
129 	valloc(shmsegs, struct shmid_ds, shminfo.shmmni);
130 #endif
131 
132 	/*
133 	 * Determine how many buffers to allocate.
134 	 * Use 10% of memory for the first 2 Meg, 5% of the remaining
135 	 * memory. Insure a minimum of 16 buffers.
136 	 * We allocate 1/2 as many swap buffer headers as file i/o buffers.
137 	 */
138 	if (bufpages == 0)
139 		if (physmem < (2 * 1024 * 1024))
140 			bufpages = physmem / 10 / CLSIZE;
141 		else
142 			bufpages = ((2 * 1024 * 1024 + physmem) / 20) / CLSIZE;
143 	if (nbuf == 0) {
144 		nbuf = bufpages / 2;
145 		if (nbuf < 16)
146 			nbuf = 16;
147 	}
148 	if (nswbuf == 0) {
149 		nswbuf = (nbuf / 2) &~ 1;	/* force even */
150 		if (nswbuf > 256)
151 			nswbuf = 256;		/* sanity */
152 	}
153 	valloc(swbuf, struct buf, nswbuf);
154 
155 	/*
156 	 * Now the amount of virtual memory remaining for buffers
157 	 * can be calculated, estimating needs for the cmap.
158 	 */
159 	ncmap = (maxmem*NBPG - ((int)v &~ 0xc0000000)) /
160 		(CLBYTES + sizeof(struct cmap)) + 2;
161 	maxbufs = ((SYSPTSIZE * NBPG) -
162 	    ((int)(v + ncmap * sizeof(struct cmap)) - 0xc0000000)) /
163 		(MAXBSIZE + sizeof(struct buf));
164 	if (maxbufs < 16)
165 		panic("sys pt too small");
166 	if (nbuf > maxbufs) {
167 		printf("SYSPTSIZE limits number of buffers to %d\n", maxbufs);
168 		nbuf = maxbufs;
169 	}
170 	if (bufpages > nbuf * (MAXBSIZE / CLBYTES))
171 		bufpages = nbuf * (MAXBSIZE / CLBYTES);
172 	valloc(buf, struct buf, nbuf);
173 
174 	/*
175 	 * Allocate space for core map.
176 	 * Allow space for all of phsical memory minus the amount
177 	 * dedicated to the system. The amount of physical memory
178 	 * dedicated to the system is the total virtual memory of
179 	 * the system thus far, plus core map, buffer pages,
180 	 * and buffer headers not yet allocated.
181 	 * Add 2: 1 because the 0th entry is unused, 1 for rounding.
182 	 */
183 	ncmap = (maxmem*NBPG - ((int)(v + bufpages*CLBYTES) &~ 0xc0000000)) /
184 		(CLBYTES + sizeof(struct cmap)) + 2;
185 	valloclim(cmap, struct cmap, ncmap, ecmap);
186 
187 	/*
188 	 * Clear space allocated thus far, and make r/w entries
189 	 * for the space in the kernel map.
190 	 */
191 	unixsize = btoc((int)v &~ 0xc0000000);
192 	while (firstaddr < unixsize) {
193 		*(int *)(&Sysmap[firstaddr]) = PG_V | PG_KW | firstaddr;
194 		clearseg((unsigned)firstaddr);
195 		firstaddr++;
196 	}
197 
198 	/*
199 	 * Now allocate buffers proper.  They are different than the above
200 	 * in that they usually occupy more virtual memory than physical.
201 	 */
202 	v = (caddr_t) ((int)(v + PGOFSET) &~ PGOFSET);
203 	valloc(buffers, char, MAXBSIZE * nbuf);
204 	base = bufpages / nbuf;
205 	residual = bufpages % nbuf;
206 	mapaddr = firstaddr;
207 	for (i = 0; i < residual; i++) {
208 		for (j = 0; j < (base + 1) * CLSIZE; j++) {
209 			*(int *)(&Sysmap[mapaddr+j]) = PG_V | PG_KW | firstaddr;
210 			clearseg((unsigned)firstaddr);
211 			firstaddr++;
212 		}
213 		mapaddr += MAXBSIZE / NBPG;
214 	}
215 	for (i = residual; i < nbuf; i++) {
216 		for (j = 0; j < base * CLSIZE; j++) {
217 			*(int *)(&Sysmap[mapaddr+j]) = PG_V | PG_KW | firstaddr;
218 			clearseg((unsigned)firstaddr);
219 			firstaddr++;
220 		}
221 		mapaddr += MAXBSIZE / NBPG;
222 	}
223 
224 	unixsize = btoc((int)v &~ 0xc0000000);
225 	if (firstaddr >= physmem - 8*UPAGES)
226 		panic("no memory");
227 	mtpr(TBIA, 1);			/* After we just cleared it all! */
228 
229 	/*
230 	 * Initialize callouts
231 	 */
232 	callfree = callout;
233 	for (i = 1; i < ncallout; i++)
234 		callout[i-1].c_next = &callout[i];
235 
236 	/*
237 	 * Initialize memory allocator and swap
238 	 * and user page table maps.
239 	 *
240 	 * THE USER PAGE TABLE MAP IS CALLED ``kernelmap''
241 	 * WHICH IS A VERY UNDESCRIPTIVE AND INCONSISTENT NAME.
242 	 */
243 	meminit(firstaddr, maxmem);
244 	maxmem = freemem;
245 	printf("avail mem = %d\n", ctob(maxmem));
246 	printf("using %d buffers containing %d bytes of memory\n",
247 		nbuf, bufpages * CLBYTES);
248 	rminit(kernelmap, (long)USRPTSIZE, (long)1,
249 	    "usrpt", nproc);
250 	rminit(mbmap, (long)(nmbclusters * CLSIZE), (long)CLSIZE,
251 	    "mbclusters", nmbclusters/4);
252 	kmeminit();		/* now safe to do malloc/free */
253 	intenable = 1;		/* Enable interrupts from now on */
254 
255 	/*
256 	 * Set up CPU-specific registers, cache, etc.
257 	 */
258 	initcpu();
259 
260 	/*
261 	 * Set up buffers, so they can be used to read disk labels.
262 	 */
263 	bhinit();
264 	binit();
265 
266 	/*
267 	 * Configure the system.
268 	 */
269 	configure();
270 }
271 
272 #ifdef PGINPROF
273 /*
274  * Return the difference (in microseconds)
275  * between the  current time and a previous
276  * time as represented  by the arguments.
277  * If there is a pending clock interrupt
278  * which has not been serviced due to high
279  * ipl, return error code.
280  */
281 /*ARGSUSED*/
282 vmtime(otime, olbolt, oicr)
283 	register int otime, olbolt, oicr;
284 {
285 
286 	return (((time.tv_sec-otime)*60 + lbolt-olbolt)*16667);
287 }
288 #endif
289 
290 /*
291  * Send an interrupt to process.
292  *
293  * Stack is set up to allow sigcode stored
294  * in u. to call routine, followed by kcall
295  * to sigreturn routine below.  After sigreturn
296  * resets the signal mask, the stack, and the
297  * frame pointer, it returns to the user
298  * specified pc, psl.
299  */
300 sendsig(catcher, sig, mask, code)
301 	sig_t catcher;
302 	int sig, mask;
303 	unsigned code;
304 {
305 	register struct sigcontext *scp;
306 	register struct proc *p = u.u_procp;
307 	register int *regs;
308 	register struct sigframe {
309 		int	sf_signum;
310 		int	sf_code;
311 		struct	sigcontext *sf_scp;
312 		sig_t	sf_handler;
313 		int	sf_regs[6];		/* r0-r5 */
314 		struct	sigcontext *sf_scpcopy;
315 	} *fp;
316 	int oonstack;
317 
318 	regs = u.u_ar0;
319 	oonstack = u.u_onstack;
320 	/*
321 	 * Allocate and validate space for the signal handler
322 	 * context. Note that if the stack is in P0 space, the
323 	 * call to grow() is a nop, and the useracc() check
324 	 * will fail if the process has not already allocated
325 	 * the space with a `brk'.
326 	 */
327 	if (!u.u_onstack && (u.u_sigonstack & sigmask(sig))) {
328 		scp = (struct sigcontext *)u.u_sigsp - 1;
329 		u.u_onstack = 1;
330 	} else
331 		scp = (struct sigcontext *)regs[SP] - 1;
332 	fp = (struct sigframe *)scp - 1;
333 	if ((int)fp <= USRSTACK - ctob(u.u_ssize))
334 		(void) grow((unsigned)fp);
335 	if (useracc((caddr_t)fp, sizeof (*fp) + sizeof (*scp), B_WRITE) == 0) {
336 		/*
337 		 * Process has trashed its stack; give it an illegal
338 		 * instruction to halt it in its tracks.
339 		 */
340 		SIGACTION(p, SIGILL) = SIG_DFL;
341 		sig = sigmask(SIGILL);
342 		p->p_sigignore &= ~sig;
343 		p->p_sigcatch &= ~sig;
344 		p->p_sigmask &= ~sig;
345 		psignal(p, SIGILL);
346 		return;
347 	}
348 	/*
349 	 * Build the argument list for the signal handler.
350 	 */
351 	fp->sf_signum = sig;
352 	fp->sf_code = code;
353 	fp->sf_scp = scp;
354 	fp->sf_handler = catcher;
355 	/*
356 	 * Build the callf argument frame to be used to call sigreturn.
357 	 */
358 	fp->sf_scpcopy = scp;
359 	/*
360 	 * Build the signal context to be used by sigreturn.
361 	 */
362 	scp->sc_onstack = oonstack;
363 	scp->sc_mask = mask;
364 	scp->sc_sp = regs[SP];
365 	scp->sc_fp = regs[FP];
366 	scp->sc_pc = regs[PC];
367 	scp->sc_ps = regs[PS];
368 	regs[SP] = (int)fp;
369 	regs[PC] = (int)u.u_pcb.pcb_sigc;
370 }
371 
372 /*
373  * System call to cleanup state after a signal
374  * has been taken.  Reset signal mask and
375  * stack state from context left by sendsig (above).
376  * Return to previous pc and psl as specified by
377  * context left by sendsig. Check carefully to
378  * make sure that the user has not modified the
379  * psl to gain improper priviledges or to cause
380  * a machine fault.
381  */
382 /* ARGSUSED */
383 sigreturn(p, uap, retval)
384 	struct proc *p;
385 	struct args {
386 		struct sigcontext *sigcntxp;
387 	} *uap;
388 	int *retval;
389 {
390 	register struct sigcontext *scp;
391 	register int *regs = u.u_ar0;
392 
393 	scp = uap->sigcntxp;
394 	if (useracc((caddr_t)scp, sizeof (*scp), 0) == 0)
395 		RETURN (EINVAL);
396 	if ((scp->sc_ps & (PSL_MBZ|PSL_IPL|PSL_IS)) != 0 ||
397 	    (scp->sc_ps & (PSL_PRVMOD|PSL_CURMOD)) != (PSL_PRVMOD|PSL_CURMOD))
398 		RETURN (EINVAL);
399 	u.u_onstack = scp->sc_onstack & 01;
400 	p->p_sigmask = scp->sc_mask &~ sigcantmask;
401 	regs[FP] = scp->sc_fp;
402 	regs[SP] = scp->sc_sp;
403 	regs[PC] = scp->sc_pc;
404 	regs[PS] = scp->sc_ps;
405 	RETURN (EJUSTRETURN);
406 }
407 
408 int	waittime = -1;
409 
410 boot(arghowto)
411 	int arghowto;
412 {
413 	register long dummy;		/* r12 is reserved */
414 	register int howto;		/* r11 == how to boot */
415 	register int devtype;		/* r10 == major of root dev */
416 	extern char *panicstr;
417 
418 	howto = arghowto;
419 	if ((howto&RB_NOSYNC) == 0 && waittime < 0 && bfreelist[0].b_forw) {
420 		register struct buf *bp;
421 		int iter, nbusy;
422 
423 		waittime = 0;
424 		(void) splnet();
425 		printf("syncing disks... ");
426 		/*
427 		 * Release vnodes held by texts before update.
428 		 */
429 		if (panicstr == 0)
430 			xumount(NULL);
431 		sync();
432 
433 		for (iter = 0; iter < 20; iter++) {
434 			nbusy = 0;
435 			for (bp = &buf[nbuf]; --bp >= buf; )
436 				if ((bp->b_flags & (B_BUSY|B_INVAL)) == B_BUSY)
437 					nbusy++;
438 			if (nbusy == 0)
439 				break;
440 			printf("%d ", nbusy);
441 			DELAY(40000 * iter);
442 		}
443 		if (nbusy)
444 			printf("giving up\n");
445 		else
446 			printf("done\n");
447 		DELAY(10000);			/* wait for printf to finish */
448 	}
449 	mtpr(IPL, 0x1f);			/* extreme priority */
450 	devtype = major(rootdev);
451 	*(int *)CPBFLG = howto;
452 	if (howto&RB_HALT) {
453 		printf("halting (in tight loop); hit ~h\n\n");
454 		mtpr(IPL, 0x1f);
455 		for (;;)
456 			;
457 	} else {
458 		if (howto & RB_DUMP) {
459 			doadump();		/* CPBOOT's itsself */
460 			/*NOTREACHED*/
461 		}
462 		tocons(CPBOOT);
463 	}
464 #ifdef lint
465 	dummy = 0; dummy = dummy;
466 	printf("howto %d, devtype %d\n", arghowto, devtype);
467 #endif
468 	for (;;)
469 		asm("halt");
470 	/*NOTREACHED*/
471 }
472 
473 struct	cpdcb_o cpcontrol;
474 
475 /*
476  * Send the given comand ('c') to the console processor.
477  * Assumed to be one of the last things the OS does before
478  *  halting or rebooting.
479  */
480 tocons(c)
481 {
482 	register timeout;
483 
484 	cpcontrol.cp_hdr.cp_unit = CPUNIT;
485 	cpcontrol.cp_hdr.cp_comm =  (char)c;
486 	if (c != CPBOOT)
487 		cpcontrol.cp_hdr.cp_count = 1;	/* Just for sanity */
488 	else {
489 		cpcontrol.cp_hdr.cp_count = 4;
490 		*(int *)cpcontrol.cp_buf = 0;	/* r11 value for reboot */
491 	}
492 	timeout = 100000;				/* Delay loop */
493 	while (timeout-- && (cnlast->cp_unit&CPDONE) == 0)
494 		uncache(&cnlast->cp_unit);
495 	/* give up, force it to listen */
496 	mtpr(CPMDCB, vtoph((struct proc *)0, (unsigned)&cpcontrol));
497 }
498 
499 #if CLSIZE != 1
500 /*
501  * Invalidate single all pte's in a cluster
502  */
503 tbiscl(v)
504 	unsigned v;
505 {
506 	register caddr_t addr;		/* must be first reg var */
507 	register int i;
508 
509 	addr = ptob(v);
510 	for (i = 0; i < CLSIZE; i++) {
511 		mtpr(TBIS, addr);
512 		addr += NBPG;
513 	}
514 }
515 #endif
516 
517 int	dumpmag = 0x8fca0101;	/* magic number for savecore */
518 int	dumpsize = 0;		/* also for savecore */
519 
520 dumpconf()
521 {
522 	int nblks;
523 
524 	dumpsize = physmem;
525 	if (dumpdev != NODEV && bdevsw[major(dumpdev)].d_psize) {
526 		nblks = (*bdevsw[major(dumpdev)].d_psize)(dumpdev);
527 		if (dumpsize > btoc(dbtob(nblks - dumplo)))
528 			dumpsize = btoc(dbtob(nblks - dumplo));
529 		else if (dumplo == 0)
530 			dumplo = nblks - btodb(ctob(physmem));
531 	}
532 	/*
533 	 * Don't dump on the first CLSIZE pages,
534 	 * in case the dump device includes a disk label.
535 	 */
536 	if (dumplo < CLSIZE)
537 		dumplo = CLSIZE;
538 }
539 
540 /*
541  * Doadump comes here after turning off memory management and
542  * getting on the dump stack, either when called above, or by
543  * the auto-restart code.
544  */
545 dumpsys()
546 {
547 
548 	if (dumpdev == NODEV)
549 		return;
550 	/*
551 	 * For dumps during autoconfiguration,
552 	 * if dump device has already configured...
553 	 */
554 	if (dumpsize == 0)
555 		dumpconf();
556 	if (dumplo < 0)
557 		return;
558 	printf("\ndumping to dev %x, offset %d\n", dumpdev, dumplo);
559 	printf("dump ");
560 	switch ((*bdevsw[major(dumpdev)].d_dump)(dumpdev)) {
561 
562 	case ENXIO:
563 		printf("device bad\n");
564 		break;
565 
566 	case EFAULT:
567 		printf("device not ready\n");
568 		break;
569 
570 	case EINVAL:
571 		printf("area improper\n");
572 		break;
573 
574 	case EIO:
575 		printf("i/o error\n");
576 		break;
577 
578 	default:
579 		printf("succeeded\n");
580 		break;
581 	}
582 	printf("\n\n");
583 	DELAY(1000);
584 	tocons(CPBOOT);
585 }
586 
587 /*
588  * Bus error 'recovery' code.
589  * Print out the buss frame and then give up.
590  * (More information from special registers can be printed here.)
591  */
592 
593 /*
594  * Frame for bus error
595  */
596 struct buserframe {
597 	int	which_bus;		/* primary or secondary */
598 	int	memerreg;		/* memory error register */
599 	int	trp_pc;			/* trapped pc */
600 	int	trp_psl;		/* trapped psl */
601 };
602 
603 char	*mem_errcd[8] = {
604 	"Unknown error code 0",
605 	"Address parity error",		/* APE */
606 	"Data parity error",		/* DPE */
607 	"Data check error",		/* DCE */
608 	"Versabus timeout",		/* VTO */
609 	"Versabus error",		/* VBE */
610 	"Non-existent memory",		/* NEM */
611 	"Unknown error code 7",
612 };
613 
614 buserror(v)
615 	caddr_t v;
616 {
617 	register struct buserframe *busef = (struct buserframe *)v;
618 	register long reg;
619 
620 	printf("bus error, address %x, psl %x\n",
621 	    busef->trp_pc, busef->trp_psl);
622 	reg =  busef->memerreg;
623 	printf("mear %x %s\n",
624 	    ((reg&MEAR)>>16)&0xffff, mem_errcd[reg & ERRCD]);
625 	if (reg&AXE)
626 		printf("adapter external error\n");
627 	printf("error master: %s\n", reg&ERM ? "versabus" : "tahoe");
628 	if (reg&IVV)
629 		printf("illegal interrupt vector from ipl %d\n", (reg>>2)&7);
630 	reg = busef->which_bus;
631 	printf("mcbr %x versabus type %x\n",
632 	    ((reg&MCBR)>>16)&0xffff, reg & 0xffc3);
633 	if ((busef->memerreg&IVV) == 0)
634 		panic("buserror");
635 }
636 
637 microtime(tvp)
638 	register struct timeval *tvp;
639 {
640 	int s = splhigh();
641 
642 	*tvp = time;
643 	tvp->tv_usec += tick;
644 	while (tvp->tv_usec > 1000000) {
645 		tvp->tv_sec++;
646 		tvp->tv_usec -= 1000000;
647 	}
648 	splx(s);
649 }
650 
651 initcpu()
652 {
653 	register struct proc *p;
654 
655 	p = &proc[0];
656 #define	initkey(which, p, index) \
657     which/**/_cache[index] = 1, which/**/_cnt[index] = 1; \
658     p->p_/**/which = index;
659 	initkey(ckey, p, MAXCKEY);
660 	initkey(dkey, p, MAXDKEY);
661 }
662 
663 /*
664  * Clear registers on exec
665  */
666 /* ARGSUSED */
667 setregs(entry, retval)
668 	u_long entry;
669 	int *retval;
670 {
671 
672 #ifdef notdef
673 	/* should pass args to init on the stack */
674 	for (rp = &u.u_ar0[0]; rp < &u.u_ar0[16];)
675 		*rp++ = 0;
676 #endif
677 	u.u_ar0[FP] = 0;	/* bottom of the fp chain */
678 	u.u_ar0[PC] = entry + 2;
679 }
680