xref: /original-bsd/sys/luna68k/luna68k/machdep.c (revision 68d9582f)
1 /*
2  * Copyright (c) 1988 University of Utah.
3  * Copyright (c) 1992 OMRON Corporation.
4  * Copyright (c) 1982, 1986, 1990, 1992 The Regents of the University of California.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to Berkeley by
8  * the Systems Programming Group of the University of Utah Computer
9  * Science Department.
10  *
11  * %sccs.include.redist.c%
12  *
13  * from: Utah $Hdr: machdep.c 1.63 91/04/24$
14  * OMRON: $Id: machdep.c,v 1.3 92/06/14 06:17:12 moti Exp $
15  *
16  * from: hp300/hp300/machdep.c	7.28 (Berkeley) 6/5/92
17  *
18  *	@(#)machdep.c	7.2 (Berkeley) 06/16/92
19  */
20 
21 #include "param.h"
22 #include "systm.h"
23 #include "signalvar.h"
24 #include "kernel.h"
25 #include "map.h"
26 #include "proc.h"
27 #include "buf.h"
28 #include "reboot.h"
29 #include "conf.h"
30 #include "file.h"
31 #include "clist.h"
32 #include "callout.h"
33 #include "malloc.h"
34 #include "mbuf.h"
35 #include "msgbuf.h"
36 #include "mount.h"
37 #include "user.h"
38 #include "exec.h"
39 #ifdef SYSVSHM
40 #include "shm.h"
41 #endif
42 
43 #include "../include/cpu.h"
44 #include "../include/reg.h"
45 #include "../include/psl.h"
46 #include "isr.h"
47 #include "pte.h"
48 #include "net/netisr.h"
49 
50 #define	MAXMEM	64*1024*CLSIZE	/* XXX - from cmap.h */
51 #include "vm/vm_kern.h"
52 
53 vm_map_t buffer_map;
54 extern vm_offset_t avail_end;
55 
56 /*
57  * Declare these as initialized data so we can patch them.
58  */
59 int	nswbuf = 0;
60 #ifdef	NBUF
61 int	nbuf = NBUF;
62 #else
63 int	nbuf = 0;
64 #endif
65 #ifdef	BUFPAGES
66 int	bufpages = BUFPAGES;
67 #else
68 int	bufpages = 0;
69 #endif
70 int	msgbufmapped;		/* set when safe to use msgbuf */
71 int	maxmem;			/* max memory per process */
72 int	physmem = MAXMEM;	/* max supported memory, changes to actual */
73 /*
74  * safepri is a safe priority for sleep to set for a spin-wait
75  * during autoconfiguration or after a panic.
76  */
77 int	safepri = PSL_LOWIPL;
78 
79 extern	u_int lowram;
80 
81 #ifdef FPCOPROC
82 int	fpptype = -1;
83 #endif
84 
85 /*
86  * Console initialization: called early on from main,
87  * before vm init or startup.  Do enough configuration
88  * to choose and initialize a console.
89  */
90 consinit()
91 {
92 
93 	/*
94 	 * Set cpuspeed immediately since cninit() called routines
95 	 * might use delay.
96 	 */
97 
98 	cpuspeed = MHZ_25;
99 
100 	/*
101          * Find what hardware is attached to this machine.
102          */
103 	find_devs();
104 
105 	/*
106 	 * Initialize the console before we print anything out.
107 	 */
108 	cninit();
109 }
110 
111 /*
112  * cpu_startup: allocate memory for variable-sized tables,
113  * initialize cpu, and do autoconfiguration.
114  */
115 cpu_startup()
116 {
117 	register unsigned i;
118 	register caddr_t v, firstaddr;
119 	int base, residual;
120 	vm_offset_t minaddr, maxaddr;
121 	vm_size_t size;
122 #ifdef DEBUG
123 	extern int pmapdebug;
124 	int opmapdebug = pmapdebug;
125 
126 	pmapdebug = 0;
127 #endif
128 	/*
129 	 * Initialize error message buffer (at end of core).
130 	 */
131 	for (i = 0; i < btoc(sizeof (struct msgbuf)); i++)
132 		pmap_enter(kernel_pmap, (vm_offset_t)msgbufp,
133 		    avail_end + i * NBPG, VM_PROT_ALL, TRUE);
134 	msgbufmapped = 1;
135 
136 	/*
137 	 * Good {morning,afternoon,evening,night}.
138 	 */
139 	printf(version);
140 	identifyfpu();
141 	printf("real mem = %d\n", ctob(physmem));
142 
143 	/*
144 	 * Allocate space for system data structures.
145 	 * The first available real memory address is in "firstaddr".
146 	 * The first available kernel virtual address is in "v".
147 	 * As pages of kernel virtual memory are allocated, "v" is incremented.
148 	 * As pages of memory are allocated and cleared,
149 	 * "firstaddr" is incremented.
150 	 * An index into the kernel page table corresponding to the
151 	 * virtual memory address maintained in "v" is kept in "mapaddr".
152 	 */
153 	/*
154 	 * Make two passes.  The first pass calculates how much memory is
155 	 * needed and allocates it.  The second pass assigns virtual
156 	 * addresses to the various data structures.
157 	 */
158 	firstaddr = 0;
159 again:
160 	v = (caddr_t)firstaddr;
161 
162 #define	valloc(name, type, num) \
163 	    (name) = (type *)v; v = (caddr_t)((name)+(num))
164 #define	valloclim(name, type, num, lim) \
165 	    (name) = (type *)v; v = (caddr_t)((lim) = ((name)+(num)))
166 	valloc(cfree, struct cblock, nclist);
167 	valloc(callout, struct callout, ncallout);
168 	valloc(swapmap, struct map, nswapmap = maxproc * 2);
169 #ifdef SYSVSHM
170 	valloc(shmsegs, struct shmid_ds, shminfo.shmmni);
171 #endif
172 
173 	/*
174 	 * Determine how many buffers to allocate.
175 	 * Since HPs tend to be long on memory and short on disk speed,
176 	 * we allocate more buffer space than the BSD standard of
177 	 * use 10% of memory for the first 2 Meg, 5% of remaining.
178 	 * We just allocate a flat 10%.  Insure a minimum of 16 buffers.
179 	 * We allocate 1/2 as many swap buffer headers as file i/o buffers.
180 	 */
181 	if (bufpages == 0)
182 		bufpages = physmem / 10 / CLSIZE;
183 	if (nbuf == 0) {
184 		nbuf = bufpages;
185 		if (nbuf < 16)
186 			nbuf = 16;
187 	}
188 	if (nswbuf == 0) {
189 		nswbuf = (nbuf / 2) &~ 1;	/* force even */
190 		if (nswbuf > 256)
191 			nswbuf = 256;		/* sanity */
192 	}
193 	valloc(swbuf, struct buf, nswbuf);
194 	valloc(buf, struct buf, nbuf);
195 	/*
196 	 * End of first pass, size has been calculated so allocate memory
197 	 */
198 	if (firstaddr == 0) {
199 		size = (vm_size_t)(v - firstaddr);
200 		firstaddr = (caddr_t) kmem_alloc(kernel_map, round_page(size));
201 		if (firstaddr == 0)
202 			panic("startup: no room for tables");
203 		goto again;
204 	}
205 	/*
206 	 * End of second pass, addresses have been assigned
207 	 */
208 	if ((vm_size_t)(v - firstaddr) != size)
209 		panic("startup: table size inconsistency");
210 	/*
211 	 * Now allocate buffers proper.  They are different than the above
212 	 * in that they usually occupy more virtual memory than physical.
213 	 */
214 	size = MAXBSIZE * nbuf;
215 	buffer_map = kmem_suballoc(kernel_map, (vm_offset_t *)&buffers,
216 				   &maxaddr, size, FALSE);
217 	minaddr = (vm_offset_t)buffers;
218 	if (vm_map_find(buffer_map, vm_object_allocate(size), (vm_offset_t)0,
219 			&minaddr, size, FALSE) != KERN_SUCCESS)
220 		panic("startup: cannot allocate buffers");
221 	base = bufpages / nbuf;
222 	residual = bufpages % nbuf;
223 	for (i = 0; i < nbuf; i++) {
224 		vm_size_t curbufsize;
225 		vm_offset_t curbuf;
226 
227 		/*
228 		 * First <residual> buffers get (base+1) physical pages
229 		 * allocated for them.  The rest get (base) physical pages.
230 		 *
231 		 * The rest of each buffer occupies virtual space,
232 		 * but has no physical memory allocated for it.
233 		 */
234 		curbuf = (vm_offset_t)buffers + i * MAXBSIZE;
235 		curbufsize = CLBYTES * (i < residual ? base+1 : base);
236 		vm_map_pageable(buffer_map, curbuf, curbuf+curbufsize, FALSE);
237 		vm_map_simplify(buffer_map, curbuf);
238 	}
239 	/*
240 	 * Allocate a submap for exec arguments.  This map effectively
241 	 * limits the number of processes exec'ing at any time.
242 	 */
243 	exec_map = kmem_suballoc(kernel_map, &minaddr, &maxaddr,
244 				 16*NCARGS, TRUE);
245 	/*
246 	 * Allocate a submap for physio
247 	 */
248 	phys_map = kmem_suballoc(kernel_map, &minaddr, &maxaddr,
249 				 VM_PHYS_SIZE, TRUE);
250 
251 	/*
252 	 * Finally, allocate mbuf pool.  Since mclrefcnt is an off-size
253 	 * we use the more space efficient malloc in place of kmem_alloc.
254 	 */
255 	mclrefcnt = (char *)malloc(NMBCLUSTERS+CLBYTES/MCLBYTES,
256 				   M_MBUF, M_NOWAIT);
257 	bzero(mclrefcnt, NMBCLUSTERS+CLBYTES/MCLBYTES);
258 	mb_map = kmem_suballoc(kernel_map, (vm_offset_t *)&mbutl, &maxaddr,
259 			       VM_MBUF_SIZE, FALSE);
260 	/*
261 	 * Initialize callouts
262 	 */
263 	callfree = callout;
264 	for (i = 1; i < ncallout; i++)
265 		callout[i-1].c_next = &callout[i];
266 	callout[i-1].c_next = NULL;
267 
268 #ifdef DEBUG
269 	pmapdebug = opmapdebug;
270 #endif
271 	printf("avail mem = %d\n", ptoa(cnt.v_free_count));
272 	printf("using %d buffers containing %d bytes of memory\n",
273 		nbuf, bufpages * CLBYTES);
274 	/*
275 	 * Set up CPU-specific registers, cache, etc.
276 	 */
277 	initcpu();
278 
279 	/*
280 	 * Set up buffers, so they can be used to read disk labels.
281 	 */
282 	bufinit();
283 
284 	/*
285 	 * Configure the system.
286 	 */
287 	configure();
288 }
289 
290 /*
291  * Set registers on exec.
292  * XXX Should clear registers except sp, pc,
293  * but would break init; should be fixed soon.
294  */
295 setregs(p, entry, retval)
296 	register struct proc *p;
297 	u_long entry;
298 	int retval[2];
299 {
300 	p->p_md.md_regs[PC] = entry & ~1;
301 #ifdef FPCOPROC
302 	/* restore a null state frame */
303 	p->p_addr->u_pcb.pcb_fpregs.fpf_null = 0;
304 	m68881_restore(&p->p_addr->u_pcb.pcb_fpregs);
305 #endif
306 }
307 
308 identifyfpu()
309 {
310     if ( fpptype == -1 ) {
311 	printf("unknow FPU type \n");
312 	panic("startup");
313     }
314     printf("LUNA(20Mhz MC68030 CPU, 20Mhz MC6888%d FPU)\n",fpptype);
315 }
316 
317 #define SS_RTEFRAME	1
318 #define SS_FPSTATE	2
319 #define SS_USERREGS	4
320 
321 struct sigstate {
322 	int	ss_flags;		/* which of the following are valid */
323 	struct	frame ss_frame;		/* original exception frame */
324 	struct	fpframe ss_fpstate;	/* 68881/68882 state info */
325 };
326 
327 /*
328  * WARNING: code in locore.s assumes the layout shown for sf_signum
329  * thru sf_handler so... don't screw with them!
330  */
331 struct sigframe {
332 	int	sf_signum;		/* signo for handler */
333 	int	sf_code;		/* additional info for handler */
334 	struct	sigcontext *sf_scp;	/* context ptr for handler */
335 	sig_t	sf_handler;		/* handler addr for u_sigc */
336 	struct	sigstate sf_state;	/* state of the hardware */
337 	struct	sigcontext sf_sc;	/* actual context */
338 };
339 
340 #ifdef DEBUG
341 int sigdebug = 0;
342 int sigpid = 0;
343 #define SDB_FOLLOW	0x01
344 #define SDB_KSTACK	0x02
345 #define SDB_FPSTATE	0x04
346 #endif
347 
348 /*
349  * Send an interrupt to process.
350  */
351 void
352 sendsig(catcher, sig, mask, code)
353 	sig_t catcher;
354 	int sig, mask;
355 	unsigned code;
356 {
357 	register struct proc *p = curproc;
358 	register struct sigframe *fp, *kfp;
359 	register struct frame *frame;
360 	register struct sigacts *psp = p->p_sigacts;
361 	register short ft;
362 	int oonstack, fsize;
363 	extern short exframesize[];
364 	extern char sigcode[], esigcode[];
365 
366 	frame = (struct frame *)p->p_md.md_regs;
367 	ft = frame->f_format;
368 	oonstack = psp->ps_sigstk.ss_flags & SA_ONSTACK;
369 	/*
370 	 * Allocate and validate space for the signal handler
371 	 * context. Note that if the stack is in P0 space, the
372 	 * call to grow() is a nop, and the useracc() check
373 	 * will fail if the process has not already allocated
374 	 * the space with a `brk'.
375 	 */
376 	fsize = sizeof(struct sigframe);
377 	if ((psp->ps_flags & SAS_ALTSTACK) &&
378 	    (psp->ps_sigstk.ss_flags & SA_ONSTACK) == 0 &&
379 	    (psp->ps_sigonstack & sigmask(sig))) {
380 		fp = (struct sigframe *)(psp->ps_sigstk.ss_base +
381 					 psp->ps_sigstk.ss_size - fsize);
382 		psp->ps_sigstk.ss_flags |= SA_ONSTACK;
383 	} else
384 		fp = (struct sigframe *)(frame->f_regs[SP] - fsize);
385 	if ((unsigned)fp <= USRSTACK - ctob(p->p_vmspace->vm_ssize))
386 		(void)grow(p, (unsigned)fp);
387 #ifdef DEBUG
388 	if ((sigdebug & SDB_KSTACK) && p->p_pid == sigpid)
389 		printf("sendsig(%d): sig %d ssp %x usp %x scp %x ft %d\n",
390 		       p->p_pid, sig, &oonstack, fp, &fp->sf_sc, ft);
391 #endif
392 	if (useracc((caddr_t)fp, fsize, B_WRITE) == 0) {
393 #ifdef DEBUG
394 		if ((sigdebug & SDB_KSTACK) && p->p_pid == sigpid)
395 			printf("sendsig(%d): useracc failed on sig %d\n",
396 			       p->p_pid, sig);
397 #endif
398 		/*
399 		 * Process has trashed its stack; give it an illegal
400 		 * instruction to halt it in its tracks.
401 		 */
402 		SIGACTION(p, SIGILL) = SIG_DFL;
403 		sig = sigmask(SIGILL);
404 		p->p_sigignore &= ~sig;
405 		p->p_sigcatch &= ~sig;
406 		p->p_sigmask &= ~sig;
407 		psignal(p, SIGILL);
408 		return;
409 	}
410 	kfp = (struct sigframe *)malloc((u_long)fsize, M_TEMP, M_WAITOK);
411 	/*
412 	 * Build the argument list for the signal handler.
413 	 */
414 	kfp->sf_signum = sig;
415 	kfp->sf_code = code;
416 	kfp->sf_scp = &fp->sf_sc;
417 	kfp->sf_handler = catcher;
418 	/*
419 	 * Save necessary hardware state.  Currently this includes:
420 	 *	- general registers
421 	 *	- original exception frame (if not a "normal" frame)
422 	 *	- FP coprocessor state
423 	 */
424 	kfp->sf_state.ss_flags = SS_USERREGS;
425 	bcopy((caddr_t)frame->f_regs,
426 	      (caddr_t)kfp->sf_state.ss_frame.f_regs, sizeof frame->f_regs);
427 	if (ft >= FMT7) {
428 #ifdef DEBUG
429 		if (ft != FMT9 && ft != FMTA && ft != FMTB)
430 			panic("sendsig: bogus frame type");
431 #endif
432 		kfp->sf_state.ss_flags |= SS_RTEFRAME;
433 		kfp->sf_state.ss_frame.f_format = frame->f_format;
434 		kfp->sf_state.ss_frame.f_vector = frame->f_vector;
435 		bcopy((caddr_t)&frame->F_u,
436 		      (caddr_t)&kfp->sf_state.ss_frame.F_u, exframesize[ft]);
437 		/*
438 		 * Leave an indicator that we need to clean up the kernel
439 		 * stack.  We do this by setting the "pad word" above the
440 		 * hardware stack frame to the amount the stack must be
441 		 * adjusted by.
442 		 *
443 		 * N.B. we increment rather than just set f_stackadj in
444 		 * case we are called from syscall when processing a
445 		 * sigreturn.  In that case, f_stackadj may be non-zero.
446 		 */
447 		frame->f_stackadj += exframesize[ft];
448 		frame->f_format = frame->f_vector = 0;
449 #ifdef DEBUG
450 		if (sigdebug & SDB_FOLLOW)
451 			printf("sendsig(%d): copy out %d of frame %d\n",
452 			       p->p_pid, exframesize[ft], ft);
453 #endif
454 	}
455 #ifdef FPCOPROC
456 	kfp->sf_state.ss_flags |= SS_FPSTATE;
457 	m68881_save(&kfp->sf_state.ss_fpstate);
458 #ifdef DEBUG
459 	if ((sigdebug & SDB_FPSTATE) && *(char *)&kfp->sf_state.ss_fpstate)
460 		printf("sendsig(%d): copy out FP state (%x) to %x\n",
461 		       p->p_pid, *(u_int *)&kfp->sf_state.ss_fpstate,
462 		       &kfp->sf_state.ss_fpstate);
463 #endif
464 #endif
465 	/*
466 	 * Build the signal context to be used by sigreturn.
467 	 */
468 	kfp->sf_sc.sc_onstack = oonstack;
469 	kfp->sf_sc.sc_mask = mask;
470 	kfp->sf_sc.sc_sp = frame->f_regs[SP];
471 	kfp->sf_sc.sc_fp = frame->f_regs[A6];
472 	kfp->sf_sc.sc_ap = (int)&fp->sf_state;
473 	kfp->sf_sc.sc_pc = frame->f_pc;
474 	kfp->sf_sc.sc_ps = frame->f_sr;
475 	(void) copyout((caddr_t)kfp, (caddr_t)fp, fsize);
476 	frame->f_regs[SP] = (int)fp;
477 #ifdef DEBUG
478 	if (sigdebug & SDB_FOLLOW)
479 		printf("sendsig(%d): sig %d scp %x fp %x sc_sp %x sc_ap %x\n",
480 		       p->p_pid, sig, kfp->sf_scp, fp,
481 		       kfp->sf_sc.sc_sp, kfp->sf_sc.sc_ap);
482 #endif
483 	/*
484 	 * Signal trampoline code is at base of user stack.
485 	 */
486 	frame->f_pc = (int)PS_STRINGS - (esigcode - sigcode);
487 #ifdef DEBUG
488 	if ((sigdebug & SDB_KSTACK) && p->p_pid == sigpid)
489 		printf("sendsig(%d): sig %d returns\n",
490 		       p->p_pid, sig);
491 #endif
492 	free((caddr_t)kfp, M_TEMP);
493 }
494 
495 /*
496  * System call to cleanup state after a signal
497  * has been taken.  Reset signal mask and
498  * stack state from context left by sendsig (above).
499  * Return to previous pc and psl as specified by
500  * context left by sendsig. Check carefully to
501  * make sure that the user has not modified the
502  * psl to gain improper priviledges or to cause
503  * a machine fault.
504  */
505 /* ARGSUSED */
506 sigreturn(p, uap, retval)
507 	struct proc *p;
508 	struct args {
509 		struct sigcontext *sigcntxp;
510 	} *uap;
511 	int *retval;
512 {
513 	register struct sigcontext *scp;
514 	register struct frame *frame;
515 	register int rf;
516 	struct sigcontext tsigc;
517 	struct sigstate tstate;
518 	int flags;
519 	extern short exframesize[];
520 
521 	scp = uap->sigcntxp;
522 #ifdef DEBUG
523 	if (sigdebug & SDB_FOLLOW)
524 		printf("sigreturn: pid %d, scp %x\n", p->p_pid, scp);
525 #endif
526 	if ((int)scp & 1)
527 		return (EINVAL);
528 	/*
529 	 * Test and fetch the context structure.
530 	 * We grab it all at once for speed.
531 	 */
532 	if (useracc((caddr_t)scp, sizeof (*scp), B_WRITE) == 0 ||
533 	    copyin((caddr_t)scp, (caddr_t)&tsigc, sizeof tsigc))
534 		return (EINVAL);
535 	scp = &tsigc;
536 	if ((scp->sc_ps & (PSL_MBZ|PSL_IPL|PSL_S)) != 0)
537 		return (EINVAL);
538 	/*
539 	 * Restore the user supplied information
540 	 */
541 	if (scp->sc_onstack & 01)
542 		p->p_sigacts->ps_sigstk.ss_flags |= SA_ONSTACK;
543 	else
544 		p->p_sigacts->ps_sigstk.ss_flags &= ~SA_ONSTACK;
545 	p->p_sigmask = scp->sc_mask &~ sigcantmask;
546 	frame = (struct frame *) p->p_md.md_regs;
547 	frame->f_regs[SP] = scp->sc_sp;
548 	frame->f_regs[A6] = scp->sc_fp;
549 	frame->f_pc = scp->sc_pc;
550 	frame->f_sr = scp->sc_ps;
551 	/*
552 	 * Grab pointer to hardware state information.
553 	 * If zero, the user is probably doing a longjmp.
554 	 */
555 	if ((rf = scp->sc_ap) == 0)
556 		return (EJUSTRETURN);
557 	/*
558 	 * See if there is anything to do before we go to the
559 	 * expense of copying in close to 1/2K of data
560 	 */
561 	flags = fuword((caddr_t)rf);
562 #ifdef DEBUG
563 	if (sigdebug & SDB_FOLLOW)
564 		printf("sigreturn(%d): sc_ap %x flags %x\n",
565 		       p->p_pid, rf, flags);
566 #endif
567 	/*
568 	 * fuword failed (bogus sc_ap value).
569 	 */
570 	if (flags == -1)
571 		return (EINVAL);
572 	if (flags == 0 || copyin((caddr_t)rf, (caddr_t)&tstate, sizeof tstate))
573 		return (EJUSTRETURN);
574 #ifdef DEBUG
575 	if ((sigdebug & SDB_KSTACK) && p->p_pid == sigpid)
576 		printf("sigreturn(%d): ssp %x usp %x scp %x ft %d\n",
577 		       p->p_pid, &flags, scp->sc_sp, uap->sigcntxp,
578 		       (flags&SS_RTEFRAME) ? tstate.ss_frame.f_format : -1);
579 #endif
580 	/*
581 	 * Restore most of the users registers except for A6 and SP
582 	 * which were handled above.
583 	 */
584 	if (flags & SS_USERREGS)
585 		bcopy((caddr_t)tstate.ss_frame.f_regs,
586 		      (caddr_t)frame->f_regs, sizeof(frame->f_regs)-2*NBPW);
587 	/*
588 	 * Restore long stack frames.  Note that we do not copy
589 	 * back the saved SR or PC, they were picked up above from
590 	 * the sigcontext structure.
591 	 */
592 	if (flags & SS_RTEFRAME) {
593 		register int sz;
594 
595 		/* grab frame type and validate */
596 		sz = tstate.ss_frame.f_format;
597 		if (sz > 15 || (sz = exframesize[sz]) < 0)
598 			return (EINVAL);
599 		frame->f_stackadj -= sz;
600 		frame->f_format = tstate.ss_frame.f_format;
601 		frame->f_vector = tstate.ss_frame.f_vector;
602 		bcopy((caddr_t)&tstate.ss_frame.F_u, (caddr_t)&frame->F_u, sz);
603 #ifdef DEBUG
604 		if (sigdebug & SDB_FOLLOW)
605 			printf("sigreturn(%d): copy in %d of frame type %d\n",
606 			       p->p_pid, sz, tstate.ss_frame.f_format);
607 #endif
608 	}
609 #ifdef FPCOPROC
610 	/*
611 	 * Finally we restore the original FP context
612 	 */
613 	if (flags & SS_FPSTATE)
614 		m68881_restore(&tstate.ss_fpstate);
615 #ifdef DEBUG
616 	if ((sigdebug & SDB_FPSTATE) && *(char *)&tstate.ss_fpstate)
617 		printf("sigreturn(%d): copied in FP state (%x) at %x\n",
618 		       p->p_pid, *(u_int *)&tstate.ss_fpstate,
619 		       &tstate.ss_fpstate);
620 #endif
621 #endif
622 #ifdef DEBUG
623 	if ((sigdebug & SDB_FOLLOW) ||
624 	    ((sigdebug & SDB_KSTACK) && p->p_pid == sigpid))
625 		printf("sigreturn(%d): returns\n", p->p_pid);
626 #endif
627 	return (EJUSTRETURN);
628 }
629 
630 int	waittime = -1;
631 
632 boot(howto)
633 	register int howto;
634 {
635 	/* take a snap shot before clobbering any registers */
636 	if (curproc)
637 		savectx(curproc->p_addr, 0);
638 
639 	boothowto = howto;
640 	if ((howto&RB_NOSYNC) == 0 && waittime < 0 && bfreelist[0].b_forw) {
641 		register struct buf *bp;
642 		int iter, nbusy;
643 
644 		waittime = 0;
645 		(void) spl0();
646 		printf("syncing disks... ");
647 		/*
648 		 * Release vnodes held by texts before sync.
649 		 */
650 		if (panicstr == 0)
651 			vnode_pager_umount(NULL);
652 #ifdef notdef
653 #include "fd.h"
654 #if NFD > 0
655 		fdshutdown();
656 #endif
657 #endif
658 		sync(&proc0, (void *)NULL, (int *)NULL);
659 
660 		for (iter = 0; iter < 20; iter++) {
661 			nbusy = 0;
662 			for (bp = &buf[nbuf]; --bp >= buf; )
663 				if ((bp->b_flags & (B_BUSY|B_INVAL)) == B_BUSY)
664 					nbusy++;
665 			if (nbusy == 0)
666 				break;
667 			printf("%d ", nbusy);
668 			DELAY(40000 * iter);
669 		}
670 		if (nbusy)
671 			printf("giving up\n");
672 		else
673 			printf("done\n");
674 
675 		/*
676 		 * If we've been adjusting the clock, the todr
677 		 * will be out of synch; adjust it now.
678 		 */
679 		resettodr();
680 	}
681 	splhigh();			/* extreme priority */
682 	if (howto&RB_HALT) {
683 		printf("halted\n\n");
684 		asm("	stop	#0x2700");
685 	} else {
686 		printf("\r\n\n");
687 		if (howto & RB_DUMP)
688 			dumpsys();
689 		doboot();
690 		/*NOTREACHED*/
691 	}
692 	/*NOTREACHED*/
693 }
694 
695 int	dumpmag = 0x8fca0101;	/* magic number for savecore */
696 int	dumpsize = 0;		/* also for savecore */
697 long	dumplo = 0;
698 
699 dumpconf()
700 {
701 	int nblks;
702 
703 	dumpsize = physmem;
704 	if (dumpdev != NODEV && bdevsw[major(dumpdev)].d_psize) {
705 		nblks = (*bdevsw[major(dumpdev)].d_psize)(dumpdev);
706 		if (dumpsize > btoc(dbtob(nblks - dumplo)))
707 			dumpsize = btoc(dbtob(nblks - dumplo));
708 		else if (dumplo == 0)
709 			dumplo = nblks - btodb(ctob(physmem));
710 	}
711 	/*
712 	 * Don't dump on the first CLBYTES (why CLBYTES?)
713 	 * in case the dump device includes a disk label.
714 	 */
715 	if (dumplo < btodb(CLBYTES))
716 		dumplo = btodb(CLBYTES);
717 }
718 
719 /*
720  * Doadump comes here after turning off memory management and
721  * getting on the dump stack, either when called above, or by
722  * the auto-restart code.
723  */
724 dumpsys()
725 {
726 
727 	msgbufmapped = 0;
728 	if (dumpdev == NODEV)
729 		return;
730 	/*
731 	 * For dumps during autoconfiguration,
732 	 * if dump device has already configured...
733 	 */
734 	if (dumpsize == 0)
735 		dumpconf();
736 	if (dumplo < 0)
737 		return;
738 	printf("\ndumping to dev %x, offset %d\n", dumpdev, dumplo);
739 	printf("dump ");
740 	switch ((*bdevsw[major(dumpdev)].d_dump)(dumpdev)) {
741 
742 	case ENXIO:
743 		printf("device bad\n");
744 		break;
745 
746 	case EFAULT:
747 		printf("device not ready\n");
748 		break;
749 
750 	case EINVAL:
751 		printf("area improper\n");
752 		break;
753 
754 	case EIO:
755 		printf("i/o error\n");
756 		break;
757 
758 	default:
759 		printf("succeeded\n");
760 		break;
761 	}
762 }
763 
764 microtime(tvp)
765 	register struct timeval *tvp;
766 {
767 	int s = splhigh();
768 
769 	*tvp = time;
770 	tvp->tv_usec += tick;
771 	while (tvp->tv_usec > 1000000) {
772 		tvp->tv_sec++;
773 		tvp->tv_usec -= 1000000;
774 	}
775 	splx(s);
776 }
777 
778 initcpu()
779 {
780 	parityenable();
781 }
782 
783 straytrap(pc, evec)
784 	int pc;
785 	u_short evec;
786 {
787 	printf("unexpected trap (vector offset %x) from %x\n",
788 	       evec & 0xFFF, pc);
789 }
790 
791 int	*nofault;
792 
793 badaddr(addr)
794 	register caddr_t addr;
795 {
796 	register int i;
797 	label_t	faultbuf;
798 
799 #ifdef lint
800 	i = *addr; if (i) return(0);
801 #endif
802 	nofault = (int *) &faultbuf;
803 	if (setjmp((label_t *)nofault)) {
804 		nofault = (int *) 0;
805 		return(1);
806 	}
807 	i = *(volatile short *)addr;
808 	nofault = (int *) 0;
809 	return(0);
810 }
811 
812 badbaddr(addr)
813 	register caddr_t addr;
814 {
815 	register int i;
816 	label_t	faultbuf;
817 
818 #ifdef lint
819 	i = *addr; if (i) return(0);
820 #endif
821 	nofault = (int *) &faultbuf;
822 	if (setjmp((label_t *)nofault)) {
823 		nofault = (int *) 0;
824 		return(1);
825 	}
826 	i = *(volatile char *)addr;
827 	nofault = (int *) 0;
828 	return(0);
829 }
830 
831 netintr()
832 {
833 #ifdef INET
834 	if (netisr & (1 << NETISR_ARP)) {
835 		netisr &= ~(1 << NETISR_ARP);
836 		arpintr();
837 	}
838 	if (netisr & (1 << NETISR_IP)) {
839 		netisr &= ~(1 << NETISR_IP);
840 		ipintr();
841 	}
842 #endif
843 #ifdef NS
844 	if (netisr & (1 << NETISR_NS)) {
845 		netisr &= ~(1 << NETISR_NS);
846 		nsintr();
847 	}
848 #endif
849 #ifdef ISO
850 	if (netisr & (1 << NETISR_ISO)) {
851 		netisr &= ~(1 << NETISR_ISO);
852 		clnlintr();
853 	}
854 #endif
855 }
856 
857 #ifdef	notfdef
858 intrhand(sr)
859 	int sr;
860 {
861 	register struct isr *isr;
862 	register int found = 0;
863 	register int ipl;
864 	extern struct isr isrqueue[];
865 
866 	ipl = (sr >> 8) & 7;
867 	switch (ipl) {
868 
869 	case 3:
870 	case 4:
871 	case 5:
872 		ipl = ISRIPL(ipl);
873 		isr = isrqueue[ipl].isr_forw;
874 		for (; isr != &isrqueue[ipl]; isr = isr->isr_forw) {
875 			if ((isr->isr_intr)(isr->isr_arg)) {
876 				found++;
877 				break;
878 			}
879 		}
880 		if (found == 0)
881 			printf("stray interrupt, sr 0x%x\n", sr);
882 		break;
883 
884 	case 0:
885 	case 1:
886 	case 2:
887 	case 6:
888 	case 7:
889 		printf("intrhand: unexpected sr 0x%x\n", sr);
890 		break;
891 	}
892 }
893 #endif
894 
895 #if defined(DEBUG) && !defined(PANICBUTTON)
896 #define PANICBUTTON
897 #endif
898 
899 #ifdef PANICBUTTON
900 int panicbutton = 1;	/* non-zero if panic buttons are enabled */
901 int crashandburn = 0;
902 int candbdelay = 50;	/* give em half a second */
903 
904 candbtimer()
905 {
906 	crashandburn = 0;
907 }
908 #endif
909 
910 /*
911  * Level 7 interrupts can be caused by the keyboard or parity errors.
912  */
913 nmihand(frame)
914 	struct frame frame;
915 {
916 #ifdef PANICBUTTON
917        	static int innmihand = 0;
918 
919        	/*
920        	 * Attempt to reduce the window of vulnerability for recursive
921        	 * NMIs (e.g. someone holding down the keyboard reset button).
922        	 */
923        	if (innmihand == 0) {
924        		innmihand = 1;
925        		printf("Got a keyboard NMI\n");
926        		innmihand = 0;
927        	}
928        	if (panicbutton) {
929        		if (crashandburn) {
930        			crashandburn = 0;
931        			panic(panicstr ?
932        			      "forced crash, nosync" : "forced crash");
933        		}
934        		crashandburn++;
935        		timeout(candbtimer, (caddr_t)0, candbdelay);
936        	}
937 #endif
938        	return;
939 }
940 
941 regdump(rp, sbytes)
942   int *rp; /* must not be register */
943   int sbytes;
944 {
945 	static int doingdump = 0;
946 	register int i;
947 	int s;
948 	extern char *hexstr();
949 
950 	if (doingdump)
951 		return;
952 	s = splhigh();
953 	doingdump = 1;
954 	printf("pid = %d, pc = %s, ", curproc->p_pid, hexstr(rp[PC], 8));
955 	printf("ps = %s, ", hexstr(rp[PS], 4));
956 	printf("sfc = %s, ", hexstr(getsfc(), 4));
957 	printf("dfc = %s\n", hexstr(getdfc(), 4));
958 	printf("Registers:\n     ");
959 	for (i = 0; i < 8; i++)
960 		printf("        %d", i);
961 	printf("\ndreg:");
962 	for (i = 0; i < 8; i++)
963 		printf(" %s", hexstr(rp[i], 8));
964 	printf("\nareg:");
965 	for (i = 0; i < 8; i++)
966 		printf(" %s", hexstr(rp[i+8], 8));
967 	if (sbytes > 0) {
968 		if (rp[PS] & PSL_S) {
969 			printf("\n\nKernel stack (%s):",
970 			       hexstr((int)(((int *)&rp)-1), 8));
971 			dumpmem(((int *)&rp)-1, sbytes, 0);
972 		} else {
973 			printf("\n\nUser stack (%s):", hexstr(rp[SP], 8));
974 			dumpmem((int *)rp[SP], sbytes, 1);
975 		}
976 	}
977 	doingdump = 0;
978 	splx(s);
979 }
980 
981 extern char kstack[];
982 #define KSADDR	((int *)&(kstack[(UPAGES-1)*NBPG]))
983 
984 dumpmem(ptr, sz, ustack)
985 	register int *ptr;
986 	int sz;
987 {
988 	register int i, val;
989 	extern char *hexstr();
990 
991 	for (i = 0; i < sz; i++) {
992 		if ((i & 7) == 0)
993 			printf("\n%s: ", hexstr((int)ptr, 6));
994 		else
995 			printf(" ");
996 		if (ustack == 1) {
997 			if ((val = fuword(ptr++)) == -1)
998 				break;
999 		} else {
1000 			if (ustack == 0 &&
1001 			    (ptr < KSADDR || ptr > KSADDR+(NBPG/4-1)))
1002 				break;
1003 			val = *ptr++;
1004 		}
1005 		printf("%s", hexstr(val, 8));
1006 	}
1007 	printf("\n");
1008 }
1009 
1010 char *
1011 hexstr(val, len)
1012 	register int val;
1013 {
1014 	static char nbuf[9];
1015 	register int x, i;
1016 
1017 	if (len > 8)
1018 		return("");
1019 	nbuf[len] = '\0';
1020 	for (i = len-1; i >= 0; --i) {
1021 		x = val & 0xF;
1022 		if (x > 9)
1023 			nbuf[i] = x - 10 + 'A';
1024 		else
1025 			nbuf[i] = x + '0';
1026 		val >>= 4;
1027 	}
1028 	return(nbuf);
1029 }
1030 
1031 /* for LUNA */
1032 
1033 /*
1034  * Enable parity detection
1035  */
1036 #define PARREG		((volatile short *)0x49000003)
1037 #define	PARITY_ENABLE	0xC
1038 parityenable()
1039 {
1040     *PARREG = PARITY_ENABLE;
1041 }
1042 
1043 #ifdef FPCOPROC
1044 #define EXT_FPP_ADDR		0x6F000000	/* External 68882 board  */
1045 #define INT_FPP_ADDR		0x6B000000	/* Internal 68881 chip   */
1046 
1047 #define FPP_ON			0x80		/* selected fpp on	 */
1048 #define FPP_OFF			0x00		/* selected fpp off    	 */
1049 
1050 #define	SET_INT_FPP	(*(char *)INT_FPP_ADDR = FPP_ON);(*(char *)EXT_FPP_ADDR = FPP_OFF)
1051 #define	SET_EXT_FPP	(*(char *)INT_FPP_ADDR = FPP_OFF);(*(char *)EXT_FPP_ADDR = FPP_ON)
1052 
1053 #define	FPP68881	1
1054 #define	FPP68882	2
1055 
1056 unsigned char fpp_svarea[212];
1057 
1058 #ifndef	OLD_LUNA
1059 /*
1060  * Check FPP type 68881/68882.
1061  */
1062 
1063 void checkfpp()
1064 {
1065     SET_INT_FPP;	/* internal = on, external = off */
1066     if( is_68882() )
1067       fpptype = FPP68882;
1068     else
1069       fpptype = FPP68881;
1070     return;
1071 }
1072 #else
1073 
1074 /*
1075  * Check in/ex-ternal fpp, and determine which we use.
1076  * Also set fpp type(MC68881/68882).
1077  */
1078 
1079 void checkfpp()
1080 {
1081 int	internal_exist,external_exist;
1082 int	external_68882;
1083 
1084     SET_INT_FPP;	/* internal = on, external = off */
1085     if ( internal_exist = havefpp() && is_68882() ) {	/* internal = 68882 */
1086 	fpptype = FPP68882;
1087 	return;
1088     } else {		/* internal don't exist  or it is not 68882 */
1089         SET_EXT_FPP;	/* internal = off, external = on */
1090 	if ( internal_exist && 	/* internal = 68882, external <> 68882 */
1091 	     (!(external_exist = havefpp()) || !(external_68882 = is_68882())) ) {
1092 	   SET_INT_FPP;	/* internal = on, external = off */
1093 	   fpptype = FPP68881;
1094 	   return;
1095         }
1096 	if ( internal_exist ) { /* internal = 68881, external = 68882 */
1097 	   fpptype = FPP68882;
1098 	   return;
1099         }
1100 	if ( external_exist )  /* internal not exist, external exist */
1101 	    if ( external_68882 ) { 	/* external = 68882 */
1102 		fpptype = FPP68882;
1103 		return;
1104 	    } else { 			/* external = 68881 */
1105 		fpptype = FPP68881;
1106 		return;
1107 	    }
1108 	else		/* in/ex-ternal non exist */
1109 	    panic("fpp non-existence");
1110     }
1111 }
1112 #endif
1113 #endif
1114