1 /**	$MirOS: src/sys/arch/sparc/sparc/machdep.c,v 1.11 2013/10/31 20:06:48 tg Exp $ */
2 /*	$OpenBSD: machdep.c,v 1.98 2004/03/10 23:02:54 tom Exp $	*/
3 /*	$NetBSD: machdep.c,v 1.85 1997/09/12 08:55:02 pk Exp $ */
4 
5 /*
6  * Copyright © 2013
7  *	Thorsten “mirabilos” Glaser <tg@mirbsd.org>
8  * Copyright (c) 1992, 1993
9  *	The Regents of the University of California.  All rights reserved.
10  *
11  * This software was developed by the Computer Systems Engineering group
12  * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
13  * contributed to Berkeley.
14  *
15  * All advertising materials mentioning features or use of this software
16  * must display the following acknowledgement:
17  *	This product includes software developed by the University of
18  *	California, Lawrence Berkeley Laboratory.
19  *
20  * Redistribution and use in source and binary forms, with or without
21  * modification, are permitted provided that the following conditions
22  * are met:
23  * 1. Redistributions of source code must retain the above copyright
24  *    notice, this list of conditions and the following disclaimer.
25  * 2. Redistributions in binary form must reproduce the above copyright
26  *    notice, this list of conditions and the following disclaimer in the
27  *    documentation and/or other materials provided with the distribution.
28  * 3. Neither the name of the University nor the names of its contributors
29  *    may be used to endorse or promote products derived from this software
30  *    without specific prior written permission.
31  *
32  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
33  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
34  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
35  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
36  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
37  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
38  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
39  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
40  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
41  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
42  * SUCH DAMAGE.
43  *
44  *	@(#)machdep.c	8.6 (Berkeley) 1/14/94
45  */
46 
47 #include <sys/param.h>
48 #include <sys/signal.h>
49 #include <sys/signalvar.h>
50 #include <sys/proc.h>
51 #include <sys/user.h>
52 #include <sys/buf.h>
53 #include <sys/device.h>
54 #include <sys/reboot.h>
55 #include <sys/systm.h>
56 #include <sys/conf.h>
57 #include <sys/file.h>
58 #include <sys/timeout.h>
59 #include <sys/malloc.h>
60 #include <sys/mbuf.h>
61 #include <sys/mount.h>
62 #include <sys/msgbuf.h>
63 #include <sys/syscallargs.h>
64 #ifdef SYSVMSG
65 #include <sys/msg.h>
66 #endif
67 #include <sys/exec.h>
68 #include <sys/sysctl.h>
69 #include <sys/extent.h>
70 
71 #include <uvm/uvm_extern.h>
72 
73 #include <dev/cons.h>
74 #include <dev/rndvar.h>
75 
76 #include <machine/autoconf.h>
77 #include <machine/frame.h>
78 #include <machine/cpu.h>
79 #include <machine/pmap.h>
80 #include <machine/oldmon.h>
81 #include <machine/bsd_openprom.h>
82 
83 #include <sparc/sparc/asm.h>
84 #include <sparc/sparc/cache.h>
85 #include <sparc/sparc/vaddrs.h>
86 #include <sparc/sparc/cpuvar.h>
87 
88 #include <uvm/uvm.h>
89 
90 #ifdef SUN4M
91 #include <sparc/dev/power.h>
92 #include "power.h"
93 #include "scf.h"
94 #include "tctrl.h"
95 #if NTCTRL > 0
96 #include <sparc/dev/tctrlvar.h>
97 #endif
98 #endif
99 
100 #include "auxreg.h"
101 
102 #ifdef SUN4
103 #include <sparc/dev/led.h>
104 #include "led.h"
105 #endif
106 
107 struct vm_map *exec_map = NULL;
108 struct vm_map *phys_map = NULL;
109 
110 /*
111  * Declare these as initialized data so we can patch them.
112  */
113 #ifdef	NBUF
114 int	nbuf = NBUF;
115 #else
116 int	nbuf = 0;
117 #endif
118 
119 #ifndef BUFCACHEPERCENT
120 #define BUFCACHEPERCENT 5
121 #endif
122 
123 #ifdef	BUFPAGES
124 int	bufpages = BUFPAGES;
125 #else
126 int	bufpages = 0;
127 #endif
128 int	bufcachepercent = BUFCACHEPERCENT;
129 
130 int	physmem;
131 
132 /* sysctl settable */
133 int	sparc_led_blink = 0;
134 
135 /*
136  * safepri is a safe priority for sleep to set for a spin-wait
137  * during autoconfiguration or after a panic.
138  */
139 int   safepri = 0;
140 
141 /*
142  * dvmamap is used to manage DVMA memory. Note: this coincides with
143  * the memory range in `phys_map' (which is mostly a place-holder).
144  */
145 vaddr_t dvma_base, dvma_end;
146 struct extent *dvmamap_extent;
147 
148 caddr_t allocsys(caddr_t);
149 void	dumpsys(void);
150 void	stackdump(void);
151 
152 /*
153  * Machine-dependent startup code
154  */
155 void
cpu_startup()156 cpu_startup()
157 {
158 	unsigned i;
159 	caddr_t v;
160 	int sz;
161 	int base, residual;
162 #ifdef DEBUG
163 	extern int pmapdebug;
164 	int opmapdebug = pmapdebug;
165 #endif
166 	vaddr_t minaddr, maxaddr;
167 	vsize_t size;
168 	extern struct user *proc0paddr;
169 
170 #ifdef DEBUG
171 	pmapdebug = 0;
172 #endif
173 
174 	/*
175 	 * fix message buffer mapping, note phys addr of msgbuf is 0
176 	 */
177 	pmap_map(MSGBUF_VA, 0, MSGBUFSIZE, VM_PROT_READ|VM_PROT_WRITE);
178 	initmsgbuf((caddr_t)(MSGBUF_VA + (CPU_ISSUN4 ? 4096 : 0)), MSGBUFSIZE);
179 
180 	proc0.p_addr = proc0paddr;
181 
182 	/*
183 	 * Good {morning,afternoon,evening,night}.
184 	 */
185 	printf(version);
186 	/*identifycpu();*/
187 	printf("real mem = %d\n", ctob(physmem));
188 
189 	/*
190 	 * Find out how much space we need, allocate it,
191 	 * and then give everything true virtual addresses.
192 	 */
193 	sz = (int)allocsys((caddr_t)0);
194 
195 	if ((v = (caddr_t)uvm_km_alloc(kernel_map, round_page(sz))) == 0)
196 		panic("startup: no room for tables");
197 
198 	if (allocsys(v) - v != sz)
199 		panic("startup: table size inconsistency");
200 
201         /*
202          * allocate virtual and physical memory for the buffers.
203          */
204         size = MAXBSIZE * nbuf;         /* # bytes for buffers */
205 
206         /* allocate VM for buffers... area is not managed by VM system */
207         if (uvm_map(kernel_map, (vaddr_t *) &buffers, round_page(size),
208                     NULL, UVM_UNKNOWN_OFFSET, 0,
209                     UVM_MAPFLAG(UVM_PROT_NONE, UVM_PROT_NONE, UVM_INH_NONE,
210                                 UVM_ADV_NORMAL, 0)))
211         	panic("cpu_startup: cannot allocate VM for buffers");
212 
213         minaddr = (vaddr_t) buffers;
214         if ((bufpages / nbuf) >= btoc(MAXBSIZE)) {
215         	bufpages = btoc(MAXBSIZE) * nbuf; /* do not overallocate RAM */
216         }
217         base = bufpages / nbuf;
218         residual = bufpages % nbuf;
219 
220         /* now allocate RAM for buffers */
221 	for (i = 0 ; i < nbuf ; i++) {
222 		vaddr_t curbuf;
223 		vsize_t curbufsize;
224 		struct vm_page *pg;
225 
226 		/*
227 		 * each buffer has MAXBSIZE bytes of VM space allocated.  of
228 		 * that MAXBSIZE space we allocate and map (base+1) pages
229 		 * for the first "residual" buffers, and then we allocate
230 		 * "base" pages for the rest.
231 		 */
232 		curbuf = (vaddr_t) buffers + (i * MAXBSIZE);
233 		curbufsize = PAGE_SIZE * ((i < residual) ? (base+1) : base);
234 
235 		while (curbufsize) {
236 			pg = uvm_pagealloc(NULL, 0, NULL, 0);
237 			if (pg == NULL)
238 				panic("cpu_startup: "
239 				    "not enough RAM for buffer cache");
240 			pmap_kenter_pa(curbuf, VM_PAGE_TO_PHYS(pg),
241 				VM_PROT_READ|VM_PROT_WRITE);
242 			curbuf += PAGE_SIZE;
243 			curbufsize -= PAGE_SIZE;
244 		}
245 	}
246 	pmap_update(pmap_kernel());
247 
248 	/*
249 	 * Allocate a submap for exec arguments.  This map effectively
250 	 * limits the number of processes exec'ing at any time.
251 	 */
252 	minaddr = vm_map_min(kernel_map);
253 	exec_map = uvm_km_suballoc(kernel_map, &minaddr, &maxaddr,
254 				 16*NCARGS, VM_MAP_PAGEABLE, FALSE, NULL);
255 
256 	/*
257 	 * Allocate a map for physio.  Others use a submap of the kernel
258 	 * map, but we want one completely separate, even though it uses
259 	 * the same pmap.
260 	 */
261 	dvma_base = CPU_ISSUN4M ? DVMA4M_BASE : DVMA_BASE;
262 	dvma_end = CPU_ISSUN4M ? DVMA4M_END : DVMA_END;
263 	phys_map = uvm_map_create(pmap_kernel(), dvma_base, dvma_end,
264 	    VM_MAP_INTRSAFE);
265 	if (phys_map == NULL)
266 		panic("unable to create DVMA map");
267 	/*
268 	 * Allocate DVMA space and dump into a privately managed
269 	 * resource map for double mappings which is usable from
270 	 * interrupt contexts.
271 	 */
272 	if (uvm_km_valloc_wait(phys_map, (dvma_end-dvma_base)) != dvma_base)
273 		panic("unable to allocate from DVMA map");
274 	dvmamap_extent = extent_create("dvmamap", dvma_base, dvma_end,
275 				       M_DEVBUF, NULL, 0, EX_NOWAIT);
276 	if (dvmamap_extent == 0)
277 		panic("unable to allocate extent for dvma");
278 
279 #ifdef DEBUG
280 	pmapdebug = opmapdebug;
281 #endif
282 	printf("avail mem = %ld\n", ptoa(uvmexp.free));
283 	printf("using %d buffers containing %d bytes of memory\n",
284 		nbuf, bufpages * PAGE_SIZE);
285 
286 	/*
287 	 * Set up buffers, so they can be used to read disk labels.
288 	 */
289 	bufinit();
290 }
291 
292 /*
293  * Allocate space for system data structures.  We are given
294  * a starting virtual address and we return a final virtual
295  * address; along the way we set each data structure pointer.
296  *
297  * You call allocsys() with 0 to find out how much space we want,
298  * allocate that much and fill it with zeroes, and then call
299  * allocsys() again with the correct base virtual address.
300  */
301 caddr_t
allocsys(v)302 allocsys(v)
303 	caddr_t v;
304 {
305 
306 #define	valloc(name, type, num) \
307 	    v = (caddr_t)(((name) = (type *)v) + (num))
308 #ifdef SYSVMSG
309 	valloc(msgpool, char, msginfo.msgmax);
310 	valloc(msgmaps, struct msgmap, msginfo.msgseg);
311 	valloc(msghdrs, struct msg, msginfo.msgtql);
312 	valloc(msqids, struct msqid_ds, msginfo.msgmni);
313 #endif
314 
315 	/*
316 	 * Determine how many buffers to allocate (enough to
317 	 * hold 5% of total physical memory, but at least 16).
318 	 * Allocate 1/2 as many swap buffer headers as file i/o buffers.
319 	 */
320 	if (bufpages == 0)
321 		bufpages = physmem * bufcachepercent / 100;
322 	if (nbuf == 0) {
323 		nbuf = bufpages;
324 		if (nbuf < 16)
325 			nbuf = 16;
326 	}
327 	if (nbuf > 200)
328 		nbuf = 200;	/* or we run out of PMEGS */
329 	/* Restrict to at most 70% filled kvm */
330 	if (nbuf * MAXBSIZE >
331 	    (VM_MAX_KERNEL_ADDRESS-VM_MIN_KERNEL_ADDRESS) * 7 / 10)
332 		nbuf = (VM_MAX_KERNEL_ADDRESS-VM_MIN_KERNEL_ADDRESS) /
333 		    MAXBSIZE * 7 / 10;
334 
335 	/* More buffer pages than fits into the buffers is senseless.  */
336 	if (bufpages > nbuf * MAXBSIZE / PAGE_SIZE)
337 		bufpages = nbuf * MAXBSIZE / PAGE_SIZE;
338 
339 	valloc(buf, struct buf, nbuf);
340 	return (v);
341 }
342 
343 /*
344  * Set up registers on exec.
345  *
346  * XXX this entire mess must be fixed
347  */
348 /* ARGSUSED */
349 void
setregs(p,pack,stack,retval)350 setregs(p, pack, stack, retval)
351 	struct proc *p;
352 	struct exec_package *pack;
353 	u_long stack;
354 	register_t *retval;
355 {
356 	struct trapframe *tf = p->p_md.md_tf;
357 	struct fpstate *fs;
358 	int psr;
359 
360 	/* Setup the process StackGhost cookie which will be XORed into
361 	 * the return pointer as register windows are over/underflowed
362 	 */
363 	p->p_addr->u_pcb.pcb_wcookie = arc4random();
364 
365 	/* The cookie needs to guarantee invalid alignment after the XOR */
366 	switch (p->p_addr->u_pcb.pcb_wcookie % 3) {
367 	case 0: /* Two lsb's already both set except if the cookie is 0 */
368 		p->p_addr->u_pcb.pcb_wcookie |= 0x3;
369 		break;
370 	case 1: /* Set the lsb */
371 		p->p_addr->u_pcb.pcb_wcookie = 1 |
372 			(p->p_addr->u_pcb.pcb_wcookie & ~0x3);
373 		break;
374 	case 2: /* Set the second most lsb */
375 		p->p_addr->u_pcb.pcb_wcookie = 2 |
376 			(p->p_addr->u_pcb.pcb_wcookie & ~0x3);
377 		break;
378 	}
379 
380 
381 	/* Don't allow misaligned code by default */
382 	p->p_md.md_flags &= ~MDP_FIXALIGN;
383 
384 	/*
385 	 * The syscall will ``return'' to npc or %g7 or %g2; set them all.
386 	 * Set the rest of the registers to 0 except for %o6 (stack pointer,
387 	 * built in exec()) and psr (retain CWP and PSR_S bits).
388 	 */
389 	psr = tf->tf_psr & (PSR_S | PSR_CWP);
390 	if ((fs = p->p_md.md_fpstate) != NULL) {
391 		/*
392 		 * We hold an FPU state.  If we own *the* FPU chip state
393 		 * we must get rid of it, and the only way to do that is
394 		 * to save it.  In any case, get rid of our FPU state.
395 		 */
396 		if (p == cpuinfo.fpproc) {
397 			savefpstate(fs);
398 			cpuinfo.fpproc = NULL;
399 		}
400 		free((void *)fs, M_SUBPROC);
401 		p->p_md.md_fpstate = NULL;
402 	}
403 	bzero((caddr_t)tf, sizeof *tf);
404 	tf->tf_psr = psr;
405 	tf->tf_npc = pack->ep_entry & ~3;
406 	tf->tf_global[1] = (int)PS_STRINGS;
407 	tf->tf_global[2] = tf->tf_global[7] = tf->tf_npc;
408 	stack -= sizeof(struct rwindow);
409 	tf->tf_out[6] = stack;
410 	retval[1] = 0;
411 }
412 
413 #ifdef DEBUG
414 int sigdebug = 0;
415 int sigpid = 0;
416 #define SDB_FOLLOW	0x01
417 #define SDB_KSTACK	0x02
418 #define SDB_FPSTATE	0x04
419 #endif
420 
421 struct sigframe {
422 	int	sf_signo;		/* signal number */
423 	siginfo_t *sf_sip;		/* points to siginfo_t */
424 	int	sf_xxx;			/* placeholder */
425 	caddr_t	sf_addr;		/* SunOS compat */
426 	struct	sigcontext sf_sc;	/* actual sigcontext */
427 	siginfo_t sf_si;
428 };
429 
430 /*
431  * machine dependent system variables.
432  */
433 int
cpu_sysctl(name,namelen,oldp,oldlenp,newp,newlen,p)434 cpu_sysctl(name, namelen, oldp, oldlenp, newp, newlen, p)
435 	int *name;
436 	u_int namelen;
437 	void *oldp;
438 	size_t *oldlenp;
439 	void *newp;
440 	size_t newlen;
441 	struct proc *p;
442 {
443 #if (NAUXREG > 0) || (NLED > 0)
444 	int oldval;
445 #endif
446 #if (NLED > 0) || (NAUXREG > 0) || (NSCF > 0)
447 	int ret;
448 #endif
449 	extern int v8mul;
450 	dev_t dev;
451 
452 	/* all sysctl names are this level are terminal */
453 	if (namelen != 1)
454 		return (ENOTDIR);	/* overloaded */
455 
456 	switch (name[0]) {
457 	case CPU_LED_BLINK:
458 #if (NLED > 0) || (NAUXREG > 0) || (NSCF > 0)
459 		oldval = sparc_led_blink;
460 		ret = sysctl_int(oldp, oldlenp, newp, newlen,
461 		    &sparc_led_blink);
462 
463 		/*
464 		 * If we were false and are now true, call led_blink().
465 		 * led_blink() itself will catch the other case.
466 		 */
467 		if (!oldval && sparc_led_blink > oldval) {
468 #if NAUXREG > 0
469 			led_blink((caddr_t *)0);
470 #endif
471 #if NLED > 0
472 			led_cycle((caddr_t *)led_sc);
473 #endif
474 #if NSCF > 0
475 			scfblink((caddr_t *)0);
476 #endif
477 		}
478 
479 		return (ret);
480 #else
481 		return (EOPNOTSUPP);
482 #endif
483 	case CPU_CPUTYPE:
484 		return (sysctl_rdint(oldp, oldlenp, newp, cputyp));
485 	case CPU_V8MUL:
486 		return (sysctl_rdint(oldp, oldlenp, newp, v8mul));
487 	case CPU_CONSDEV:
488 		dev = (cn_tab == NULL) ? NODEV : cn_tab->cn_dev;
489 		return (sysctl_rdstruct(oldp, oldlenp, newp, &dev,
490 		    sizeof (dev)));
491 	default:
492 		return (EOPNOTSUPP);
493 	}
494 	/* NOTREACHED */
495 }
496 
497 /*
498  * Send an interrupt to process.
499  */
500 void
sendsig(catcher,sig,mask,code,type,val)501 sendsig(catcher, sig, mask, code, type, val)
502 	sig_t catcher;
503 	int sig, mask;
504 	u_long code;
505 	int type;
506 	union sigval val;
507 {
508 	struct proc *p = curproc;
509 	struct sigacts *psp = p->p_sigacts;
510 	struct sigframe *fp;
511 	struct trapframe *tf;
512 	int caddr, oonstack, oldsp, newsp;
513 	struct sigframe sf;
514 
515 	tf = p->p_md.md_tf;
516 	oldsp = tf->tf_out[6];
517 	oonstack = psp->ps_sigstk.ss_flags & SS_ONSTACK;
518 	/*
519 	 * Compute new user stack addresses, subtract off
520 	 * one signal frame, and align.
521 	 */
522 	if ((psp->ps_flags & SAS_ALTSTACK) && !oonstack &&
523 	    (psp->ps_sigonstack & sigmask(sig))) {
524 		fp = (struct sigframe *)(psp->ps_sigstk.ss_sp +
525 					 psp->ps_sigstk.ss_size);
526 		psp->ps_sigstk.ss_flags |= SS_ONSTACK;
527 	} else
528 		fp = (struct sigframe *)oldsp;
529 	fp = (struct sigframe *)((int)(fp - 1) & ~7);
530 
531 #ifdef DEBUG
532 	if ((sigdebug & SDB_KSTACK) && p->p_pid == sigpid)
533 		printf("sendsig: %s[%d] sig %d newusp %p scp %p\n",
534 		    p->p_comm, p->p_pid, sig, fp, &fp->sf_sc);
535 #endif
536 	/*
537 	 * Now set up the signal frame.  We build it in kernel space
538 	 * and then copy it out.  We probably ought to just build it
539 	 * directly in user space....
540 	 */
541 	sf.sf_signo = sig;
542 	sf.sf_sip = NULL;
543 
544 	/*
545 	 * Build the signal context to be used by sigreturn.
546 	 */
547 	sf.sf_sc.sc_onstack = oonstack;
548 	sf.sf_sc.sc_mask = mask;
549 	sf.sf_sc.sc_sp = oldsp;
550 	sf.sf_sc.sc_pc = tf->tf_pc;
551 	sf.sf_sc.sc_npc = tf->tf_npc;
552 	sf.sf_sc.sc_psr = tf->tf_psr;
553 	sf.sf_sc.sc_g1 = tf->tf_global[1];
554 	sf.sf_sc.sc_o0 = tf->tf_out[0];
555 
556 	if (psp->ps_siginfo & sigmask(sig)) {
557 		sf.sf_sip = &fp->sf_si;
558 		initsiginfo(&sf.sf_si, sig, code, type, val);
559 	}
560 
561 	/*
562 	 * Put the stack in a consistent state before we whack away
563 	 * at it.  Note that write_user_windows may just dump the
564 	 * registers into the pcb; we need them in the process's memory.
565 	 * We also need to make sure that when we start the signal handler,
566 	 * its %i6 (%fp), which is loaded from the newly allocated stack area,
567 	 * joins seamlessly with the frame it was in when the signal occurred,
568 	 * so that the debugger and _longjmp code can back up through it.
569 	 */
570 	newsp = (int)fp - sizeof(struct rwindow);
571 	write_user_windows();
572 	/* XXX do not copyout siginfo if not needed */
573 	if (rwindow_save(p) || copyout((caddr_t)&sf, (caddr_t)fp, sizeof sf) ||
574 	    copyout(&oldsp, &((struct rwindow *)newsp)->rw_in[6],
575 	      sizeof(register_t)) != 0) {
576 		/*
577 		 * Process has trashed its stack; give it an illegal
578 		 * instruction to halt it in its tracks.
579 		 */
580 #ifdef DEBUG
581 		if ((sigdebug & SDB_KSTACK) && p->p_pid == sigpid)
582 			printf("sendsig: window save or copyout error\n");
583 #endif
584 		sigexit(p, SIGILL);
585 		/* NOTREACHED */
586 	}
587 #ifdef DEBUG
588 	if (sigdebug & SDB_FOLLOW)
589 		printf("sendsig: %s[%d] sig %d scp %p\n",
590 		       p->p_comm, p->p_pid, sig, &fp->sf_sc);
591 #endif
592 	/*
593 	 * Arrange to continue execution at the code copied out in exec().
594 	 * It needs the function to call in %g1, and a new stack pointer.
595 	 */
596 	{
597 		caddr = p->p_sigcode;
598 		tf->tf_global[1] = (int)catcher;
599 	}
600 	tf->tf_pc = caddr;
601 	tf->tf_npc = caddr + 4;
602 	tf->tf_out[6] = newsp;
603 #ifdef DEBUG
604 	if ((sigdebug & SDB_KSTACK) && p->p_pid == sigpid)
605 		printf("sendsig: about to return to catcher\n");
606 #endif
607 }
608 
609 /*
610  * System call to cleanup state after a signal
611  * has been taken.  Reset signal mask and
612  * stack state from context left by sendsig (above),
613  * and return to the given trap frame (if there is one).
614  * Check carefully to make sure that the user has not
615  * modified the state to gain improper privileges or to cause
616  * a machine fault.
617  */
618 /* ARGSUSED */
619 int
sys_sigreturn(p,v,retval)620 sys_sigreturn(p, v, retval)
621 	struct proc *p;
622 	void *v;
623 	register_t *retval;
624 {
625 	struct sys_sigreturn_args /* {
626 		syscallarg(struct sigcontext *) sigcntxp;
627 	} */ *uap = v;
628 	struct sigcontext ksc;
629 	struct trapframe *tf;
630 	int error;
631 
632 	/* First ensure consistent stack state (see sendsig). */
633 	write_user_windows();
634 	if (rwindow_save(p))
635 		sigexit(p, SIGILL);
636 #ifdef DEBUG
637 	if (sigdebug & SDB_FOLLOW)
638 		printf("sigreturn: %s[%d], sigcntxp %p\n",
639 		    p->p_comm, p->p_pid, SCARG(uap, sigcntxp));
640 #endif
641 	if ((error = copyin(SCARG(uap, sigcntxp), &ksc, sizeof(ksc))) != 0)
642 		return (error);
643 	tf = p->p_md.md_tf;
644 	/*
645 	 * Only the icc bits in the psr are used, so it need not be
646 	 * verified.  pc and npc must be multiples of 4.  This is all
647 	 * that is required; if it holds, just do it.
648 	 */
649 	if (((ksc.sc_pc | ksc.sc_npc) & 3) != 0)
650 		return (EINVAL);
651 	/* take only psr ICC field */
652 	tf->tf_psr = (tf->tf_psr & ~PSR_ICC) | (ksc.sc_psr & PSR_ICC);
653 	tf->tf_pc = ksc.sc_pc;
654 	tf->tf_npc = ksc.sc_npc;
655 	tf->tf_global[1] = ksc.sc_g1;
656 	tf->tf_out[0] = ksc.sc_o0;
657 	tf->tf_out[6] = ksc.sc_sp;
658 	if (ksc.sc_onstack & 1)
659 		p->p_sigacts->ps_sigstk.ss_flags |= SS_ONSTACK;
660 	else
661 		p->p_sigacts->ps_sigstk.ss_flags &= ~SS_ONSTACK;
662 	p->p_sigmask = ksc.sc_mask & ~sigcantmask;
663 	return (EJUSTRETURN);
664 }
665 
666 int	waittime = -1;
667 
668 void
boot(howto)669 boot(howto)
670 	int howto;
671 {
672 	int i;
673 	static char str[4];	/* room for "-sd\0" */
674 
675 	/* If system is cold, just halt. */
676 	if (cold) {
677 		/* (Unless the user explicitly asked for reboot.) */
678 		if ((howto & RB_USERREQ) == 0)
679 			howto |= RB_HALT;
680 		goto haltsys;
681 	}
682 
683 	fb_unblank();
684 	boothowto = howto;
685 	if ((howto & RB_NOSYNC) == 0 && waittime < 0) {
686 		extern struct proc proc0;
687 
688 		/* XXX protect against curproc->p_stats.foo refs in sync() */
689 		if (curproc == NULL)
690 			curproc = &proc0;
691 		waittime = 0;
692 		vfs_shutdown();
693 
694 		/*
695 		 * If we've been adjusting the clock, the todr
696 		 * will be out of synch; adjust it now unless
697 		 * the system was sitting in ddb.
698 		 */
699 		if ((howto & RB_TIMEBAD) == 0) {
700 			resettodr();
701 		} else {
702 			printf("WARNING: not updating battery clock\n");
703 		}
704 	}
705 	(void) splhigh();		/* ??? */
706 
707 	if (howto & RB_DUMP)
708 		dumpsys();
709 
710 haltsys:
711 	/* Run any shutdown hooks */
712 	doshutdownhooks();
713 
714 	if ((howto & RB_HALT) || (howto & RB_POWERDOWN)) {
715 #if defined(SUN4M)
716 		if (howto & RB_POWERDOWN) {
717 #if NPOWER > 0 || NTCTRL >0
718 			printf("attempting to power down... %X\n",
719 			    arc4random());
720 #if NPOWER > 0
721 			powerdown();
722 #endif
723 #if NTCTRL > 0
724 			tadpole_powerdown();
725 #endif
726 #endif /* NPOWER || MTCTRL */
727 			rominterpret("power-off");
728 			printf("WARNING: powerdown failed!\n");
729 		}
730 #endif /* SUN4M */
731 		printf("halted %X\n\n", arc4random());
732 		romhalt();
733 	}
734 
735 	printf("rebooting %X\n\n", arc4random());
736 	i = 1;
737 	if (howto & RB_SINGLE)
738 		str[i++] = 's';
739 	if (howto & RB_KDB)
740 		str[i++] = 'd';
741 	if (i > 1) {
742 		str[0] = '-';
743 		str[i] = 0;
744 	} else
745 		str[0] = 0;
746 	romboot(str);
747 	/*NOTREACHED*/
748 }
749 
750 /* magic number for savecore */
751 u_long	dumpmag __attribute__((__used__)) = 0x8fca0101;
752 int	dumpsize = 0;		/* also for savecore */
753 long	dumplo = 0;
754 
755 void
dumpconf()756 dumpconf()
757 {
758 	int nblks, dumpblks;
759 
760 	if (dumpdev == NODEV || bdevsw[major(dumpdev)].d_psize == 0)
761 		/* No usable dump device */
762 		return;
763 
764 	nblks = (*bdevsw[major(dumpdev)].d_psize)(dumpdev);
765 
766 	dumpblks = ctod(physmem) + ctod(pmap_dumpsize());
767 	if (dumpblks > (nblks - ctod(1)))
768 		/*
769 		 * dump size is too big for the partition.
770 		 * Note, we safeguard a click at the front for a
771 		 * possible disk label.
772 		 */
773 		return;
774 
775 	/* Put the dump at the end of the partition */
776 	dumplo = nblks - dumpblks;
777 
778 	/*
779 	 * savecore(8) expects dumpsize to be the number of pages
780 	 * of actual core dumped (i.e. excluding the MMU stuff).
781 	 */
782 	dumpsize = physmem;
783 }
784 
785 #define	BYTES_PER_DUMP	(32 * 1024)	/* must be a multiple of pagesize */
786 static vaddr_t dumpspace;
787 
788 /*
789  * Allocate the dump i/o buffer area during kernel memory allocation
790  */
791 caddr_t
reserve_dumppages(p)792 reserve_dumppages(p)
793 	caddr_t p;
794 {
795 
796 	dumpspace = (vaddr_t)p;
797 	return (p + BYTES_PER_DUMP);
798 }
799 
800 /*
801  * Write a crash dump.
802  */
803 void
dumpsys()804 dumpsys()
805 {
806 	int psize;
807 	daddr_t blkno;
808 	int (*dump)(dev_t, daddr_t, caddr_t, size_t);
809 	int error = 0;
810 	struct memarr *mp;
811 	int nmem;
812 	extern struct memarr pmemarr[];
813 	extern int npmemarr;
814 
815 	/* copy registers to memory */
816 	snapshot(cpcb);
817 	stackdump();
818 
819 	if (dumpdev == NODEV)
820 		return;
821 
822 	/*
823 	 * For dumps during autoconfiguration,
824 	 * if dump device has already configured...
825 	 */
826 	if (dumpsize == 0)
827 		dumpconf();
828 	if (dumplo <= 0)
829 		return;
830 	printf("\ndumping to dev(%d,%d), at offset %ld blocks\n",
831 	    major(dumpdev), minor(dumpdev), dumplo);
832 
833 	psize = (*bdevsw[major(dumpdev)].d_psize)(dumpdev);
834 	printf("dump ");
835 	if (psize == -1) {
836 		printf("area unavailable\n");
837 		return;
838 	}
839 	blkno = dumplo;
840 	dump = bdevsw[major(dumpdev)].d_dump;
841 
842 	printf("mmu ");
843 	error = pmap_dumpmmu(dump, blkno);
844 	blkno += ctod(pmap_dumpsize());
845 
846 	printf("memory ");
847 	for (mp = pmemarr, nmem = npmemarr; --nmem >= 0 && error == 0; mp++) {
848 		unsigned i = 0, n;
849 		unsigned maddr = mp->addr;
850 
851 		/* XXX - what's so special about PA 0 that we can't dump it? */
852 		if (maddr == 0) {
853 			/* Skip first page at physical address 0 */
854 			maddr += NBPG;
855 			i += NBPG;
856 			blkno += btodb(NBPG);
857 		}
858 
859 		printf("@0x%x:", maddr);
860 
861 		for (; i < mp->len; i += n) {
862 			n = mp->len - i;
863 			if (n > BYTES_PER_DUMP)
864 				 n = BYTES_PER_DUMP;
865 
866 			/* print out which MBs we are dumping */
867 			if (i % (1024*1024) <= NBPG)
868 				printf("%d ", i / (1024*1024));
869 
870 			(void) pmap_map(dumpspace, maddr, maddr + n,
871 					VM_PROT_READ);
872 			error = (*dump)(dumpdev, blkno,
873 					(caddr_t)dumpspace, (int)n);
874 			pmap_remove(pmap_kernel(), dumpspace, dumpspace + n);
875 			if (error)
876 				break;
877 			maddr += n;
878 			blkno += btodb(n);
879 		}
880 	}
881 
882 	switch (error) {
883 
884 	case ENXIO:
885 		printf("device bad\n");
886 		break;
887 
888 	case EFAULT:
889 		printf("device not ready\n");
890 		break;
891 
892 	case EINVAL:
893 		printf("area improper\n");
894 		break;
895 
896 	case EIO:
897 		printf("i/o error\n");
898 		break;
899 
900 	case 0:
901 		printf("succeeded\n");
902 		break;
903 
904 	default:
905 		printf("error %d\n", error);
906 		break;
907 	}
908 }
909 
910 /*
911  * get the fp and dump the stack as best we can.  don't leave the
912  * current stack page
913  */
914 void
stackdump()915 stackdump()
916 {
917 	struct frame *fp = getfp(), *sfp;
918 
919 	sfp = fp;
920 	printf("Frame pointer is at %p\n", fp);
921 	printf("Call traceback:\n");
922 	while (fp && ((u_long)fp >> PGSHIFT) == ((u_long)sfp >> PGSHIFT)) {
923 		printf("  pc = 0x%x  args = (0x%x, 0x%x, 0x%x, 0x%x, 0x%x, 0x%x, 0x%x) fp = %p\n",
924 		    fp->fr_pc, fp->fr_arg[0], fp->fr_arg[1], fp->fr_arg[2],
925 		    fp->fr_arg[3], fp->fr_arg[4], fp->fr_arg[5], fp->fr_arg[6],
926 		    fp->fr_fp);
927 		fp = fp->fr_fp;
928 	}
929 }
930 
931 /*
932  * Map an I/O device given physical address and size in bytes, e.g.,
933  *
934  *	mydev = (struct mydev *)mapdev(myioaddr, 0,
935  *				       0, sizeof(struct mydev));
936  *
937  * See also machine/autoconf.h.
938  *
939  * XXXART - verify types (too tired now).
940  */
941 void *
mapdev(phys,virt,offset,size)942 mapdev(phys, virt, offset, size)
943 	struct rom_reg *phys;
944 	int offset, virt, size;
945 {
946 	vaddr_t va;
947 	paddr_t pa;
948 	void *ret;
949 	static vaddr_t iobase;
950 	unsigned int pmtype;
951 
952 	if (iobase == 0)
953 		iobase = IODEV_BASE;
954 
955 	size = round_page(size);
956 	if (size == 0)
957 		panic("mapdev: zero size");
958 
959 	if (virt)
960 		va = trunc_page(virt);
961 	else {
962 		va = iobase;
963 		iobase += size;
964 		if (iobase > IODEV_END)	/* unlikely */
965 			panic("mapiodev");
966 	}
967 	ret = (void *)(va | (((u_long)phys->rr_paddr + offset) & PGOFSET));
968 			/* note: preserve page offset */
969 
970 	pa = trunc_page((vaddr_t)phys->rr_paddr + offset);
971 	pmtype = PMAP_IOENC(phys->rr_iospace);
972 
973 	do {
974 		pmap_kenter_pa(va, pa | pmtype | PMAP_NC,
975 		    VM_PROT_READ | VM_PROT_WRITE);
976 		va += PAGE_SIZE;
977 		pa += PAGE_SIZE;
978 	} while ((size -= PAGE_SIZE) > 0);
979 	pmap_update(pmap_kernel());
980 	return (ret);
981 }
982 
983 int
cpu_exec_aout_makecmds(p,epp)984 cpu_exec_aout_makecmds(p, epp)
985 	struct proc *p;
986 	struct exec_package *epp;
987 {
988 	int error = ENOEXEC;
989 
990 	return error;
991 }
992 
993 #ifdef SUN4
994 void
oldmon_w_trace(va)995 oldmon_w_trace(va)
996 	u_long va;
997 {
998 	u_long stop;
999 	struct frame *fp;
1000 
1001 	if (curproc)
1002 		printf("curproc = %p, pid %d\n", curproc, curproc->p_pid);
1003 	else
1004 		printf("no curproc\n");
1005 	printf("uvm: swtch %d, trap %d, sys %d, intr %d, soft %d, faults %d\n",
1006 	       uvmexp.swtch, uvmexp.traps, uvmexp.syscalls, uvmexp.intrs,
1007 	       uvmexp.softs, uvmexp.faults);
1008 	write_user_windows();
1009 
1010 	printf("\nstack trace with sp = 0x%lx\n", va);
1011 	stop = round_page(va);
1012 	printf("stop at 0x%lx\n", stop);
1013 	fp = (struct frame *) va;
1014 	while (round_page((u_long) fp) == stop) {
1015 		printf("  0x%x(0x%x, 0x%x, 0x%x, 0x%x, 0x%x, 0x%x, 0x%x) fp %p\n", fp->fr_pc,
1016 		    fp->fr_arg[0], fp->fr_arg[1], fp->fr_arg[2], fp->fr_arg[3],
1017 		    fp->fr_arg[4], fp->fr_arg[5], fp->fr_arg[6], fp->fr_fp);
1018 		fp = fp->fr_fp;
1019 		if (fp == NULL)
1020 			break;
1021 	}
1022 	printf("end of stack trace\n");
1023 }
1024 
1025 void
oldmon_w_cmd(va,ar)1026 oldmon_w_cmd(va, ar)
1027 	u_long va;
1028 	char *ar;
1029 {
1030 	switch (*ar) {
1031 	case '\0':
1032 		switch (va) {
1033 		case 0:
1034 			panic("g0 panic");
1035 		case 4:
1036 			printf("w: case 4\n");
1037 			break;
1038 		default:
1039 			printf("w: unknown case %ld\n", va);
1040 			break;
1041 		}
1042 		break;
1043 	case 't':
1044 		oldmon_w_trace(va);
1045 		break;
1046 	default:
1047 		printf("w: arg not allowed\n");
1048 	}
1049 }
1050 #endif /* SUN4 */
1051 
1052 int
ldcontrolb(addr)1053 ldcontrolb(addr)
1054 caddr_t addr;
1055 {
1056 	struct pcb *xpcb;
1057 	extern struct user *proc0paddr;
1058 	u_long saveonfault;
1059 	int res;
1060 	int s;
1061 
1062 	if (CPU_ISSUN4M) {
1063 		printf("warning: ldcontrolb called in sun4m\n");
1064 		return 0;
1065 	}
1066 
1067 	s = splhigh();
1068 	if (curproc == NULL)
1069 		xpcb = (struct pcb *)proc0paddr;
1070 	else
1071 		xpcb = &curproc->p_addr->u_pcb;
1072 
1073 	saveonfault = (u_long)xpcb->pcb_onfault;
1074         res = xldcontrolb(addr, xpcb);
1075 	xpcb->pcb_onfault = (caddr_t)saveonfault;
1076 
1077 	splx(s);
1078 	return (res);
1079 }
1080 
1081 void
wzero(vb,l)1082 wzero(vb, l)
1083 	void *vb;
1084 	u_int l;
1085 {
1086 	u_char *b = vb;
1087 	u_char *be = b + l;
1088 	u_short *sp;
1089 
1090 	if (l == 0)
1091 		return;
1092 
1093 	/* front, */
1094 	if ((u_long)b & 1)
1095 		*b++ = 0;
1096 
1097 	/* back, */
1098 	if (b != be && ((u_long)be & 1) != 0) {
1099 		be--;
1100 		*be = 0;
1101 	}
1102 
1103 	/* and middle. */
1104 	sp = (u_short *)b;
1105 	while (sp != (u_short *)be)
1106 		*sp++ = 0;
1107 }
1108 
1109 void
wcopy(vb1,vb2,l)1110 wcopy(vb1, vb2, l)
1111 	const void *vb1;
1112 	void *vb2;
1113 	u_int l;
1114 {
1115 	const u_char *b1e, *b1 = vb1;
1116 	u_char *b2 = vb2;
1117 	u_short *sp;
1118 	int bstore = 0;
1119 
1120 	if (l == 0)
1121 		return;
1122 
1123 	/* front, */
1124 	if ((u_long)b1 & 1) {
1125 		*b2++ = *b1++;
1126 		l--;
1127 	}
1128 
1129 	/* middle, */
1130 	sp = (u_short *)b1;
1131 	b1e = b1 + l;
1132 	if (l & 1)
1133 		b1e--;
1134 	bstore = (u_long)b2 & 1;
1135 
1136 	while (sp < (u_short *)b1e) {
1137 		if (bstore) {
1138 			b2[1] = *sp & 0xff;
1139 			b2[0] = *sp >> 8;
1140 		} else
1141 			*((short *)b2) = *sp;
1142 		sp++;
1143 		b2 += 2;
1144 	}
1145 
1146 	/* and back. */
1147 	if (l & 1)
1148 		*b2 = *b1e;
1149 }
1150