xref: /original-bsd/sys/luna68k/luna68k/machdep.c (revision 860e07fc)
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.29 (Berkeley) 7/8/92
17  *
18  *	@(#)machdep.c	7.3 (Berkeley) 07/23/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 initcpu()
765 {
766 	parityenable();
767 }
768 
769 straytrap(pc, evec)
770 	int pc;
771 	u_short evec;
772 {
773 	printf("unexpected trap (vector offset %x) from %x\n",
774 	       evec & 0xFFF, pc);
775 }
776 
777 int	*nofault;
778 
779 badaddr(addr)
780 	register caddr_t addr;
781 {
782 	register int i;
783 	label_t	faultbuf;
784 
785 #ifdef lint
786 	i = *addr; if (i) return(0);
787 #endif
788 	nofault = (int *) &faultbuf;
789 	if (setjmp((label_t *)nofault)) {
790 		nofault = (int *) 0;
791 		return(1);
792 	}
793 	i = *(volatile short *)addr;
794 	nofault = (int *) 0;
795 	return(0);
796 }
797 
798 badbaddr(addr)
799 	register caddr_t addr;
800 {
801 	register int i;
802 	label_t	faultbuf;
803 
804 #ifdef lint
805 	i = *addr; if (i) return(0);
806 #endif
807 	nofault = (int *) &faultbuf;
808 	if (setjmp((label_t *)nofault)) {
809 		nofault = (int *) 0;
810 		return(1);
811 	}
812 	i = *(volatile char *)addr;
813 	nofault = (int *) 0;
814 	return(0);
815 }
816 
817 netintr()
818 {
819 #ifdef INET
820 	if (netisr & (1 << NETISR_ARP)) {
821 		netisr &= ~(1 << NETISR_ARP);
822 		arpintr();
823 	}
824 	if (netisr & (1 << NETISR_IP)) {
825 		netisr &= ~(1 << NETISR_IP);
826 		ipintr();
827 	}
828 #endif
829 #ifdef NS
830 	if (netisr & (1 << NETISR_NS)) {
831 		netisr &= ~(1 << NETISR_NS);
832 		nsintr();
833 	}
834 #endif
835 #ifdef ISO
836 	if (netisr & (1 << NETISR_ISO)) {
837 		netisr &= ~(1 << NETISR_ISO);
838 		clnlintr();
839 	}
840 #endif
841 }
842 
843 #ifdef	notfdef
844 intrhand(sr)
845 	int sr;
846 {
847 	register struct isr *isr;
848 	register int found = 0;
849 	register int ipl;
850 	extern struct isr isrqueue[];
851 
852 	ipl = (sr >> 8) & 7;
853 	switch (ipl) {
854 
855 	case 3:
856 	case 4:
857 	case 5:
858 		ipl = ISRIPL(ipl);
859 		isr = isrqueue[ipl].isr_forw;
860 		for (; isr != &isrqueue[ipl]; isr = isr->isr_forw) {
861 			if ((isr->isr_intr)(isr->isr_arg)) {
862 				found++;
863 				break;
864 			}
865 		}
866 		if (found == 0)
867 			printf("stray interrupt, sr 0x%x\n", sr);
868 		break;
869 
870 	case 0:
871 	case 1:
872 	case 2:
873 	case 6:
874 	case 7:
875 		printf("intrhand: unexpected sr 0x%x\n", sr);
876 		break;
877 	}
878 }
879 #endif
880 
881 #if defined(DEBUG) && !defined(PANICBUTTON)
882 #define PANICBUTTON
883 #endif
884 
885 #ifdef PANICBUTTON
886 int panicbutton = 1;	/* non-zero if panic buttons are enabled */
887 int crashandburn = 0;
888 int candbdelay = 50;	/* give em half a second */
889 
890 void
891 candbtimer(arg)
892 	void *arg;
893 {
894 
895 	crashandburn = 0;
896 }
897 #endif
898 
899 /*
900  * Level 7 interrupts can be caused by the keyboard or parity errors.
901  */
902 nmihand(frame)
903 	struct frame frame;
904 {
905 #ifdef PANICBUTTON
906        	static int innmihand = 0;
907 
908        	/*
909        	 * Attempt to reduce the window of vulnerability for recursive
910        	 * NMIs (e.g. someone holding down the keyboard reset button).
911        	 */
912        	if (innmihand == 0) {
913        		innmihand = 1;
914        		printf("Got a keyboard NMI\n");
915        		innmihand = 0;
916        	}
917        	if (panicbutton) {
918        		if (crashandburn) {
919        			crashandburn = 0;
920        			panic(panicstr ?
921        			      "forced crash, nosync" : "forced crash");
922        		}
923        		crashandburn++;
924        		timeout(candbtimer, (void *)0, candbdelay);
925        	}
926 #endif
927        	return;
928 }
929 
930 regdump(rp, sbytes)
931   int *rp; /* must not be register */
932   int sbytes;
933 {
934 	static int doingdump = 0;
935 	register int i;
936 	int s;
937 	extern char *hexstr();
938 
939 	if (doingdump)
940 		return;
941 	s = splhigh();
942 	doingdump = 1;
943 	printf("pid = %d, pc = %s, ", curproc->p_pid, hexstr(rp[PC], 8));
944 	printf("ps = %s, ", hexstr(rp[PS], 4));
945 	printf("sfc = %s, ", hexstr(getsfc(), 4));
946 	printf("dfc = %s\n", hexstr(getdfc(), 4));
947 	printf("Registers:\n     ");
948 	for (i = 0; i < 8; i++)
949 		printf("        %d", i);
950 	printf("\ndreg:");
951 	for (i = 0; i < 8; i++)
952 		printf(" %s", hexstr(rp[i], 8));
953 	printf("\nareg:");
954 	for (i = 0; i < 8; i++)
955 		printf(" %s", hexstr(rp[i+8], 8));
956 	if (sbytes > 0) {
957 		if (rp[PS] & PSL_S) {
958 			printf("\n\nKernel stack (%s):",
959 			       hexstr((int)(((int *)&rp)-1), 8));
960 			dumpmem(((int *)&rp)-1, sbytes, 0);
961 		} else {
962 			printf("\n\nUser stack (%s):", hexstr(rp[SP], 8));
963 			dumpmem((int *)rp[SP], sbytes, 1);
964 		}
965 	}
966 	doingdump = 0;
967 	splx(s);
968 }
969 
970 extern char kstack[];
971 #define KSADDR	((int *)&(kstack[(UPAGES-1)*NBPG]))
972 
973 dumpmem(ptr, sz, ustack)
974 	register int *ptr;
975 	int sz;
976 {
977 	register int i, val;
978 	extern char *hexstr();
979 
980 	for (i = 0; i < sz; i++) {
981 		if ((i & 7) == 0)
982 			printf("\n%s: ", hexstr((int)ptr, 6));
983 		else
984 			printf(" ");
985 		if (ustack == 1) {
986 			if ((val = fuword(ptr++)) == -1)
987 				break;
988 		} else {
989 			if (ustack == 0 &&
990 			    (ptr < KSADDR || ptr > KSADDR+(NBPG/4-1)))
991 				break;
992 			val = *ptr++;
993 		}
994 		printf("%s", hexstr(val, 8));
995 	}
996 	printf("\n");
997 }
998 
999 char *
1000 hexstr(val, len)
1001 	register int val;
1002 {
1003 	static char nbuf[9];
1004 	register int x, i;
1005 
1006 	if (len > 8)
1007 		return("");
1008 	nbuf[len] = '\0';
1009 	for (i = len-1; i >= 0; --i) {
1010 		x = val & 0xF;
1011 		if (x > 9)
1012 			nbuf[i] = x - 10 + 'A';
1013 		else
1014 			nbuf[i] = x + '0';
1015 		val >>= 4;
1016 	}
1017 	return(nbuf);
1018 }
1019 
1020 /* for LUNA */
1021 
1022 /*
1023  * Enable parity detection
1024  */
1025 #define PARREG		((volatile short *)0x49000003)
1026 #define	PARITY_ENABLE	0xC
1027 parityenable()
1028 {
1029     *PARREG = PARITY_ENABLE;
1030 }
1031 
1032 #ifdef FPCOPROC
1033 #define EXT_FPP_ADDR		0x6F000000	/* External 68882 board  */
1034 #define INT_FPP_ADDR		0x6B000000	/* Internal 68881 chip   */
1035 
1036 #define FPP_ON			0x80		/* selected fpp on	 */
1037 #define FPP_OFF			0x00		/* selected fpp off    	 */
1038 
1039 #define	SET_INT_FPP	(*(char *)INT_FPP_ADDR = FPP_ON);(*(char *)EXT_FPP_ADDR = FPP_OFF)
1040 #define	SET_EXT_FPP	(*(char *)INT_FPP_ADDR = FPP_OFF);(*(char *)EXT_FPP_ADDR = FPP_ON)
1041 
1042 #define	FPP68881	1
1043 #define	FPP68882	2
1044 
1045 unsigned char fpp_svarea[212];
1046 
1047 #ifndef	OLD_LUNA
1048 /*
1049  * Check FPP type 68881/68882.
1050  */
1051 
1052 void checkfpp()
1053 {
1054     SET_INT_FPP;	/* internal = on, external = off */
1055     if( is_68882() )
1056       fpptype = FPP68882;
1057     else
1058       fpptype = FPP68881;
1059     return;
1060 }
1061 #else
1062 
1063 /*
1064  * Check in/ex-ternal fpp, and determine which we use.
1065  * Also set fpp type(MC68881/68882).
1066  */
1067 
1068 void checkfpp()
1069 {
1070 int	internal_exist,external_exist;
1071 int	external_68882;
1072 
1073     SET_INT_FPP;	/* internal = on, external = off */
1074     if ( internal_exist = havefpp() && is_68882() ) {	/* internal = 68882 */
1075 	fpptype = FPP68882;
1076 	return;
1077     } else {		/* internal don't exist  or it is not 68882 */
1078         SET_EXT_FPP;	/* internal = off, external = on */
1079 	if ( internal_exist && 	/* internal = 68882, external <> 68882 */
1080 	     (!(external_exist = havefpp()) || !(external_68882 = is_68882())) ) {
1081 	   SET_INT_FPP;	/* internal = on, external = off */
1082 	   fpptype = FPP68881;
1083 	   return;
1084         }
1085 	if ( internal_exist ) { /* internal = 68881, external = 68882 */
1086 	   fpptype = FPP68882;
1087 	   return;
1088         }
1089 	if ( external_exist )  /* internal not exist, external exist */
1090 	    if ( external_68882 ) { 	/* external = 68882 */
1091 		fpptype = FPP68882;
1092 		return;
1093 	    } else { 			/* external = 68881 */
1094 		fpptype = FPP68881;
1095 		return;
1096 	    }
1097 	else		/* in/ex-ternal non exist */
1098 	    panic("fpp non-existence");
1099     }
1100 }
1101 #endif
1102 #endif
1103