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