Home | History | Annotate | Line # | Download | only in ofppc
machdep.c revision 1.43
      1 /*	$NetBSD: machdep.c,v 1.43 1999/09/17 20:04:46 thorpej Exp $	*/
      2 
      3 /*
      4  * Copyright (C) 1995, 1996 Wolfgang Solfrank.
      5  * Copyright (C) 1995, 1996 TooLs GmbH.
      6  * All rights reserved.
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in the
     15  *    documentation and/or other materials provided with the distribution.
     16  * 3. All advertising materials mentioning features or use of this software
     17  *    must display the following acknowledgement:
     18  *	This product includes software developed by TooLs GmbH.
     19  * 4. The name of TooLs GmbH may not be used to endorse or promote products
     20  *    derived from this software without specific prior written permission.
     21  *
     22  * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
     23  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     24  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     25  * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     26  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     27  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
     28  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
     29  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
     30  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
     31  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     32  */
     33 
     34 #include "opt_compat_netbsd.h"
     35 #include "opt_ddb.h"
     36 #include "opt_inet.h"
     37 #include "opt_ccitt.h"
     38 #include "opt_iso.h"
     39 #include "opt_ns.h"
     40 #include "ipkdb.h"
     41 
     42 #include <sys/param.h>
     43 #include <sys/buf.h>
     44 #include <sys/callout.h>
     45 #include <sys/exec.h>
     46 #include <sys/malloc.h>
     47 #include <sys/map.h>
     48 #include <sys/mbuf.h>
     49 #include <sys/mount.h>
     50 #include <sys/msgbuf.h>
     51 #include <sys/proc.h>
     52 #include <sys/reboot.h>
     53 #include <sys/syscallargs.h>
     54 #include <sys/syslog.h>
     55 #include <sys/systm.h>
     56 #include <sys/kernel.h>
     57 #include <sys/user.h>
     58 
     59 #include <vm/vm.h>
     60 #include <vm/vm_kern.h>
     61 
     62 #include <uvm/uvm_extern.h>
     63 
     64 #include <net/netisr.h>
     65 
     66 #include <machine/bat.h>
     67 #include <machine/pmap.h>
     68 #include <machine/powerpc.h>
     69 #include <machine/trap.h>
     70 
     71 /*
     72  * Global variables used here and there
     73  */
     74 vm_map_t exec_map = NULL;
     75 vm_map_t mb_map = NULL;
     76 vm_map_t phys_map = NULL;
     77 
     78 struct pcb *curpcb;
     79 struct pmap *curpm;
     80 struct proc *fpuproc;
     81 
     82 extern struct user *proc0paddr;
     83 
     84 struct bat battable[16];
     85 
     86 int astpending;
     87 
     88 char *bootpath;
     89 
     90 paddr_t msgbuf_paddr;
     91 vaddr_t msgbuf_vaddr;
     92 
     93 static int fake_spl __P((void));
     94 static int fake_splx __P((int));
     95 static void fake_setsoft __P((void));
     96 static void fake_clock_return __P((struct clockframe *, int));
     97 static void fake_irq_establish __P((int, int, void (*)(void *), void *));
     98 
     99 struct machvec machine_interface = {
    100 	fake_spl,
    101 	fake_spl,
    102 	fake_spl,
    103 	fake_spl,
    104 	fake_spl,
    105 	fake_spl,
    106 	fake_spl,
    107 	fake_spl,
    108 	fake_spl,
    109 	fake_splx,
    110 	fake_setsoft,
    111 	fake_setsoft,
    112 	fake_clock_return,
    113 	fake_irq_establish,
    114 };
    115 
    116 void
    117 initppc(startkernel, endkernel, args)
    118 	u_int startkernel, endkernel;
    119 	char *args;
    120 {
    121 	int phandle, qhandle;
    122 	char name[32];
    123 	struct machvec *mp;
    124 	extern trapcode, trapsize;
    125 	extern dsitrap, dsisize;
    126 	extern isitrap, isisize;
    127 	extern decrint, decrsize;
    128 	extern tlbimiss, tlbimsize;
    129 	extern tlbdlmiss, tlbdlmsize;
    130 	extern tlbdsmiss, tlbdsmsize;
    131 #ifdef DDB
    132 	extern ddblow, ddbsize;
    133 	extern void *startsym, *endsym;
    134 #endif
    135 #if NIPKDB > 0
    136 	extern ipkdblow, ipkdbsize;
    137 #endif
    138 	extern void consinit __P((void));
    139 	extern void callback __P((void *));
    140 	int exc, scratch;
    141 
    142 	proc0.p_addr = proc0paddr;
    143 	bzero(proc0.p_addr, sizeof *proc0.p_addr);
    144 
    145 	curpcb = &proc0paddr->u_pcb;
    146 
    147 	curpm = curpcb->pcb_pmreal = curpcb->pcb_pm = pmap_kernel();
    148 
    149 	/*
    150 	 * i386 port says, that this shouldn't be here,
    151 	 * but I really think the console should be initialized
    152 	 * as early as possible.
    153 	 */
    154 	consinit();
    155 
    156 #ifdef	__notyet__		/* Needs some rethinking regarding real/virtual OFW */
    157 	OF_set_callback(callback);
    158 #endif
    159 	/*
    160 	 * Initialize BAT registers to unmapped to not generate
    161 	 * overlapping mappings below.
    162 	 */
    163 	asm volatile ("mtibatu 0,%0" :: "r"(0));
    164 	asm volatile ("mtibatu 1,%0" :: "r"(0));
    165 	asm volatile ("mtibatu 2,%0" :: "r"(0));
    166 	asm volatile ("mtibatu 3,%0" :: "r"(0));
    167 	asm volatile ("mtdbatu 0,%0" :: "r"(0));
    168 	asm volatile ("mtdbatu 1,%0" :: "r"(0));
    169 	asm volatile ("mtdbatu 2,%0" :: "r"(0));
    170 	asm volatile ("mtdbatu 3,%0" :: "r"(0));
    171 
    172 	/*
    173 	 * Set up initial BAT table to only map the lowest 256 MB area
    174 	 */
    175 	battable[0].batl = BATL(0x00000000, BAT_M);
    176 	battable[0].batu = BATU(0x00000000);
    177 
    178 	/*
    179 	 * Now setup fixed bat registers
    180 	 *
    181 	 * Note that we still run in real mode, and the BAT
    182 	 * registers were cleared above.
    183 	 */
    184 	/* IBAT0 used for initial 256 MB segment */
    185 	asm volatile ("mtibatl 0,%0; mtibatu 0,%1"
    186 		      :: "r"(battable[0].batl), "r"(battable[0].batu));
    187 	/* DBAT0 used similar */
    188 	asm volatile ("mtdbatl 0,%0; mtdbatu 0,%1"
    189 		      :: "r"(battable[0].batl), "r"(battable[0].batu));
    190 
    191 	/*
    192 	 * Set up trap vectors
    193 	 */
    194 	for (exc = EXC_RSVD; exc <= EXC_LAST; exc += 0x100)
    195 		switch (exc) {
    196 		default:
    197 			bcopy(&trapcode, (void *)exc, (size_t)&trapsize);
    198 			break;
    199 		case EXC_EXI:
    200 			/*
    201 			 * This one is (potentially) installed during autoconf
    202 			 */
    203 			break;
    204 		case EXC_DSI:
    205 			bcopy(&dsitrap, (void *)EXC_DSI, (size_t)&dsisize);
    206 			break;
    207 		case EXC_ISI:
    208 			bcopy(&isitrap, (void *)EXC_ISI, (size_t)&isisize);
    209 			break;
    210 		case EXC_DECR:
    211 			bcopy(&decrint, (void *)EXC_DECR, (size_t)&decrsize);
    212 			break;
    213 		case EXC_IMISS:
    214 			bcopy(&tlbimiss, (void *)EXC_IMISS, (size_t)&tlbimsize);
    215 			break;
    216 		case EXC_DLMISS:
    217 			bcopy(&tlbdlmiss, (void *)EXC_DLMISS, (size_t)&tlbdlmsize);
    218 			break;
    219 		case EXC_DSMISS:
    220 			bcopy(&tlbdsmiss, (void *)EXC_DSMISS, (size_t)&tlbdsmsize);
    221 			break;
    222 #if defined(DDB) || NIPKDB > 0
    223 		case EXC_PGM:
    224 		case EXC_TRC:
    225 		case EXC_BPT:
    226 #if defined(DDB)
    227 			bcopy(&ddblow, (void *)exc, (size_t)&ddbsize);
    228 #else
    229 			bcopy(&ipkdblow, (void *)exc, (size_t)&ipkdbsize);
    230 #endif
    231 			break;
    232 #endif /* DDB || NIPKDB > 0 */
    233 		}
    234 
    235 	__syncicache((void *)EXC_RST, EXC_LAST - EXC_RST + 0x100);
    236 
    237 	/*
    238 	 * Now enable translation (and machine checks/recoverable interrupts).
    239 	 */
    240 	asm volatile ("mfmsr %0; ori %0,%0,%1; mtmsr %0; isync"
    241 		      : "=r"(scratch) : "K"(PSL_IR|PSL_DR|PSL_ME|PSL_RI));
    242 
    243 	/*
    244 	 * Parse arg string.
    245 	 */
    246 	bootpath = args;
    247 	while (*++args && *args != ' ');
    248 	if (*args) {
    249 		*args++ = 0;
    250 		while (*args) {
    251 			switch (*args++) {
    252 			case 'a':
    253 				boothowto |= RB_ASKNAME;
    254 				break;
    255 			case 's':
    256 				boothowto |= RB_SINGLE;
    257 				break;
    258 			case 'd':
    259 				boothowto |= RB_KDB;
    260 				break;
    261 			}
    262 		}
    263 	}
    264 
    265 #ifdef DDB
    266 	/* ddb_init((int)(endsym - startsym), startsym, endsym); */
    267 #endif
    268 #if NIPKDB > 0
    269 	/*
    270 	 * Now trap to IPKDB
    271 	 */
    272 	ipkdb_init();
    273 	if (boothowto & RB_KDB)
    274 		ipkdb_connect(0);
    275 #endif
    276 
    277 	/*
    278 	 * Set the page size.
    279 	 */
    280 	uvm_setpagesize();
    281 
    282 	/*
    283 	 * Initialize pmap module.
    284 	 */
    285 	pmap_bootstrap(startkernel, endkernel);
    286 }
    287 
    288 /*
    289  * This should probably be in autoconf!				XXX
    290  */
    291 int cpu;
    292 char cpu_model[80];
    293 char machine[] = MACHINE;		/* from <machine/param.h> */
    294 char machine_arch[] = MACHINE_ARCH;	/* from <machine/param.h> */
    295 
    296 void
    297 identifycpu()
    298 {
    299 	int phandle, pvr;
    300 	char name[32];
    301 
    302 	/*
    303 	 * Find cpu type (Do it by OpenFirmware?)
    304 	 */
    305 	asm ("mfpvr %0" : "=r"(pvr));
    306 	cpu = pvr >> 16;
    307 	switch (cpu) {
    308 	case 1:
    309 		sprintf(cpu_model, "601");
    310 		break;
    311 	case 3:
    312 		sprintf(cpu_model, "603");
    313 		break;
    314 	case 4:
    315 		sprintf(cpu_model, "604");
    316 		break;
    317 	case 5:
    318 		sprintf(cpu_model, "602");
    319 		break;
    320 	case 6:
    321 		sprintf(cpu_model, "603e");
    322 		break;
    323 	case 7:
    324 		sprintf(cpu_model, "603ev");
    325 		break;
    326 	case 9:
    327 		sprintf(cpu_model, "604ev");
    328 		break;
    329 	case 20:
    330 		sprintf(cpu_model, "620");
    331 		break;
    332 	default:
    333 		sprintf(cpu_model, "Version %x", cpu);
    334 		break;
    335 	}
    336 	sprintf(cpu_model + strlen(cpu_model), " (Revision %x)", pvr & 0xffff);
    337 	printf("CPU: %s\n", cpu_model);
    338 }
    339 
    340 void
    341 install_extint(handler)
    342 	void (*handler) __P((void));
    343 {
    344 	extern extint, extsize;
    345 	extern u_long extint_call;
    346 	u_long offset = (u_long)handler - (u_long)&extint_call;
    347 	int omsr, msr;
    348 
    349 #ifdef	DIAGNOSTIC
    350 	if (offset > 0x1ffffff)
    351 		panic("install_extint: too far away");
    352 #endif
    353 	asm volatile ("mfmsr %0; andi. %1,%0,%2; mtmsr %1"
    354 		      : "=r"(omsr), "=r"(msr) : "K"((u_short)~PSL_EE));
    355 	extint_call = (extint_call & 0xfc000003) | offset;
    356 	bcopy(&extint, (void *)EXC_EXI, (size_t)&extsize);
    357 	__syncicache((void *)&extint_call, sizeof extint_call);
    358 	__syncicache((void *)EXC_EXI, (int)&extsize);
    359 	asm volatile ("mtmsr %0" :: "r"(omsr));
    360 }
    361 
    362 /*
    363  * Machine dependent startup code.
    364  */
    365 void
    366 cpu_startup()
    367 {
    368 	int sz, i;
    369 	caddr_t v;
    370 	paddr_t minaddr, maxaddr;
    371 	int base, residual;
    372 	char pbuf[9];
    373 
    374 	proc0.p_addr = proc0paddr;
    375 	v = (caddr_t)proc0paddr + USPACE;
    376 
    377 	/*
    378 	 * Initialize error message buffer (at end of core).
    379 	 */
    380 	if (!(msgbuf_vaddr = uvm_km_alloc(kernel_map, round_page(MSGBUFSIZE))))
    381 		panic("startup: no room for message buffer");
    382 	for (i = 0; i < btoc(MSGBUFSIZE); i++)
    383 		pmap_enter(pmap_kernel(), msgbuf_vaddr + i * NBPG,
    384 		    msgbuf_paddr + i * NBPG, VM_PROT_READ|VM_PROT_WRITE, TRUE,
    385 		    VM_PROT_READ|VM_PROT_WRITE);
    386 	initmsgbuf((caddr_t)msgbuf_vaddr, round_page(MSGBUFSIZE));
    387 
    388 	printf("%s", version);
    389 	identifycpu();
    390 
    391 	format_bytes(pbuf, sizeof(pbuf), ctob(physmem));
    392 	printf("total memory = %s\n", pbuf);
    393 
    394 	/*
    395 	 * Find out how much space we need, allocate it,
    396 	 * and then give everything true virtual addresses.
    397 	 */
    398 	sz = (int)allocsys(NULL, NULL);
    399 	if ((v = (caddr_t)uvm_km_zalloc(kernel_map, round_page(sz))) == 0)
    400 		panic("startup: no room for tables");
    401 	if (allocsys(v, NULL) - v != sz)
    402 		panic("startup: table size inconsistency");
    403 
    404 	/*
    405 	 * Now allocate buffers proper.  They are different than the above
    406 	 * in that they usually occupy more virtual memory than physical.
    407 	 */
    408 	sz = MAXBSIZE * nbuf;
    409 	if (uvm_map(kernel_map, (vaddr_t *)&buffers, round_page(sz),
    410 		    NULL, UVM_UNKNOWN_OFFSET,
    411 		    UVM_MAPFLAG(UVM_PROT_NONE, UVM_PROT_NONE, UVM_INH_NONE,
    412 				UVM_ADV_NORMAL, 0)) != KERN_SUCCESS)
    413 		panic("startup: cannot allocate VM for buffers");
    414 	minaddr = (vaddr_t)buffers;
    415 	base = bufpages / nbuf;
    416 	residual = bufpages % nbuf;
    417 	if (base >= MAXBSIZE) {
    418 		/* Don't want to alloc more physical mem than ever needed */
    419 		base = MAXBSIZE;
    420 		residual = 0;
    421 	}
    422 	for (i = 0; i < nbuf; i++) {
    423 		vsize_t curbufsize;
    424 		vaddr_t curbuf;
    425 		struct vm_page *pg;
    426 
    427 		/*
    428 		 * Each buffer has MAXBSIZE bytes of VM space allocated.  Of
    429 		 * that MAXBSIZE space, we allocate and map (base+1) pages
    430 		 * for the first "residual" buffers, and then we allocate
    431 		 * "base" pages for the rest.
    432 		 */
    433 		curbuf = (vaddr_t) buffers + (i * MAXBSIZE);
    434 		curbufsize = CLBYTES * ((i < residual) ? (base+1) : base);
    435 
    436 		while (curbufsize) {
    437 			pg = uvm_pagealloc(NULL, 0, NULL, 0);
    438 			if (pg == NULL)
    439 				panic("startup: not enough memory for "
    440 					"buffer cache");
    441 			pmap_enter(kernel_map->pmap, curbuf,
    442 			    VM_PAGE_TO_PHYS(pg), VM_PROT_READ|VM_PROT_WRITE,
    443 			    TRUE, VM_PROT_READ|VM_PROT_WRITE);
    444 			curbuf += PAGE_SIZE;
    445 			curbufsize -= PAGE_SIZE;
    446 		}
    447 	}
    448 
    449 	/*
    450 	 * Allocate a submap for exec arguments.  This map effectively
    451 	 * limits the number of processes exec'ing at any time.
    452 	 */
    453 	exec_map = uvm_km_suballoc(kernel_map, &minaddr, &maxaddr,
    454 				 16*NCARGS, VM_MAP_PAGEABLE, FALSE, NULL);
    455 
    456 	/*
    457 	 * Allocate a submap for physio
    458 	 */
    459 	phys_map = uvm_km_suballoc(kernel_map, &minaddr, &maxaddr,
    460 				 VM_PHYS_SIZE, 0, FALSE, NULL);
    461 
    462 	/*
    463 	 * No need to allocate an mbuf cluster submap.  Mbuf clusters
    464 	 * are allocated via the pool allocator, and we use direct-mapped
    465 	 * pool pages.
    466 	 */
    467 
    468 	/*
    469 	 * Initialize callouts.
    470 	 */
    471 	callfree = callout;
    472 	for (i = 1; i < ncallout; i++)
    473 		callout[i - 1].c_next = &callout[i];
    474 
    475 	format_bytes(pbuf, sizeof(pbuf), ptoa(uvmexp.free));
    476 	printf("avail memory = %s\n", pbuf);
    477 	format_bytes(pbuf, sizeof(pbuf), bufpages * CLBYTES);
    478 	printf("using %d buffers containing %s of memory\n", nbuf, pbuf);
    479 
    480 	/*
    481 	 * Set up the buffers.
    482 	 */
    483 	bufinit();
    484 
    485 	/*
    486 	 * For now, use soft spl handling.
    487 	 */
    488 	{
    489 		extern struct machvec soft_machvec;
    490 
    491 		machine_interface = soft_machvec;
    492 	}
    493 
    494 	/*
    495 	 * Now allow hardware interrupts.
    496 	 */
    497 	{
    498 		int msr;
    499 
    500 		splhigh();
    501 		asm volatile ("mfmsr %0; ori %0,%0,%1; mtmsr %0"
    502 			      : "=r"(msr) : "K"((u_short)(PSL_EE|PSL_RI)));
    503 	}
    504 }
    505 
    506 /*
    507  * consinit
    508  * Initialize system console.
    509  */
    510 void
    511 consinit()
    512 {
    513 	static int initted;
    514 
    515 	if (initted)
    516 		return;
    517 	initted = 1;
    518 	cninit();
    519 }
    520 
    521 /*
    522  * Set set up registers on exec.
    523  */
    524 void
    525 setregs(p, pack, stack)
    526 	struct proc *p;
    527 	struct exec_package *pack;
    528 	u_long stack;
    529 {
    530 	struct trapframe *tf = trapframe(p);
    531 	struct ps_strings arginfo;
    532 
    533 	bzero(tf, sizeof *tf);
    534 	tf->fixreg[1] = -roundup(-stack + 8, 16);
    535 
    536 	/*
    537 	 * XXX Machine-independent code has already copied arguments and
    538 	 * XXX environment to userland.  Get them back here.
    539 	 */
    540 	(void)copyin((char *)PS_STRINGS, &arginfo, sizeof (arginfo));
    541 
    542 	/*
    543 	 * Set up arguments for _start():
    544 	 *	_start(argc, argv, envp, obj, cleanup, ps_strings);
    545 	 *
    546 	 * Notes:
    547 	 *	- obj and cleanup are the auxilliary and termination
    548 	 *	  vectors.  They are fixed up by ld.elf_so.
    549 	 *	- ps_strings is a NetBSD extention, and will be
    550 	 * 	  ignored by executables which are strictly
    551 	 *	  compliant with the SVR4 ABI.
    552 	 *
    553 	 * XXX We have to set both regs and retval here due to different
    554 	 * XXX calling convention in trap.c and init_main.c.
    555 	 */
    556 	tf->fixreg[3] = arginfo.ps_nargvstr;
    557 	tf->fixreg[4] = (register_t)arginfo.ps_argvstr;
    558 	tf->fixreg[5] = (register_t)arginfo.ps_envstr;
    559 	tf->fixreg[6] = 0;			/* auxillary vector */
    560 	tf->fixreg[7] = 0;			/* termination vector */
    561 	tf->fixreg[8] = (register_t)PS_STRINGS;	/* NetBSD extension */
    562 
    563 	tf->srr0 = pack->ep_entry;
    564 	tf->srr1 = PSL_MBO | PSL_USERSET | PSL_FE_DFLT;
    565 	p->p_addr->u_pcb.pcb_flags = 0;
    566 }
    567 
    568 /*
    569  * Send a signal to process.
    570  */
    571 void
    572 sendsig(catcher, sig, mask, code)
    573 	sig_t catcher;
    574 	int sig;
    575 	sigset_t *mask;
    576 	u_long code;
    577 {
    578 	struct proc *p = curproc;
    579 	struct trapframe *tf;
    580 	struct sigframe *fp, frame;
    581 	struct sigacts *psp = p->p_sigacts;
    582 	int onstack;
    583 
    584 	tf = trapframe(p);
    585 
    586 	/* Do we need to jump onto the signal stack? */
    587 	onstack =
    588 	    (psp->ps_sigstk.ss_flags & (SS_DISABLE | SS_ONSTACK)) == 0 &&
    589 	    (psp->ps_sigact[sig].sa_flags & SA_ONSTACK) != 0;
    590 
    591 	/* Allocate space for the signal handler context. */
    592 	if (onstack)
    593 		fp = (struct sigframe *)((caddr_t)psp->ps_sigstk.ss_sp +
    594 						  psp->ps_sigstk.ss_size);
    595 	else
    596 		fp = (struct sigframe *)tf->fixreg[1];
    597 	fp = (struct sigframe *)((int)(fp - 1) & ~0xf);
    598 
    599 	/* Build stack frame for signal trampoline. */
    600 	frame.sf_signum = sig;
    601 	frame.sf_code = code;
    602 
    603 	/* Save register context. */
    604 	bcopy(tf, &frame.sf_sc.sc_frame, sizeof *tf);
    605 
    606 	/* Save signal stack. */
    607 	frame.sf_sc.sc_onstack = psp->ps_sigstk.ss_flags & SS_ONSTACK;
    608 
    609 	/* Save signal mask. */
    610 	frame.sf_sc.sc_mask = *mask;
    611 
    612 #ifdef COMPAT_13
    613 	/*
    614 	 * XXX We always have to save an old style signal mask because
    615 	 * XXX we might be delivering a signal to a process which will
    616 	 * XXX escape from the signal in a non-standard way and invoke
    617 	 * XXX sigreturn() directly.
    618 	 */
    619 	native_sigset_to_sigset13(mask, &frame.sf_sc.__sc_mask13);
    620 #endif
    621 
    622 	if (copyout(&frame, fp, sizeof frame) != 0) {
    623 		/*
    624 		 * Process has trashed its stack; give it an illegal
    625 		 * instructoin to halt it in its tracks.
    626 		 */
    627 		sigexit(p, SIGILL);
    628 		/* NOTREACHED */
    629 	}
    630 
    631 	/*
    632 	 * Build context to run handler in.
    633 	 */
    634 	tf->fixreg[1] = (int)fp;
    635 	tf->lr = (int)catcher;
    636 	tf->fixreg[3] = (int)sig;
    637 	tf->fixreg[4] = (int)code;
    638 	tf->fixreg[5] = (int)&frame.sf_sc;
    639 	tf->srr0 = (int)psp->ps_sigcode;
    640 
    641 	/* Remember that we're now on the signal stack. */
    642 	if (onstack)
    643 		psp->ps_sigstk.ss_flags |= SS_ONSTACK;
    644 }
    645 
    646 /*
    647  * System call to cleanup state after a signal handler returns.
    648  */
    649 int
    650 sys___sigreturn14(p, v, retval)
    651 	struct proc *p;
    652 	void *v;
    653 	register_t *retval;
    654 {
    655 	struct sys___sigreturn14_args /* {
    656 		syscallarg(struct sigcontext *) sigcntxp;
    657 	} */ *uap = v;
    658 	struct sigcontext sc;
    659 	struct trapframe *tf;
    660 	int error;
    661 
    662 	/*
    663 	 * The trampoline hands us the context.
    664 	 * It is unsafe to keep track of it ourselves, in the event that a
    665 	 * program jumps out of a signal hander.
    666 	 */
    667 	if ((error = copyin(SCARG(uap, sigcntxp), &sc, sizeof sc)) != 0)
    668 		return (error);
    669 
    670 	/* Restore the register context. */
    671 	tf = trapframe(p);
    672 	if ((sc.sc_frame.srr1 & PSL_USERSTATIC) != (tf->srr1 & PSL_USERSTATIC))
    673 		return (EINVAL);
    674 	bcopy(&sc.sc_frame, tf, sizeof *tf);
    675 
    676 	/* Restore signal stack. */
    677 	if (sc.sc_onstack & SS_ONSTACK)
    678 		p->p_sigacts->ps_sigstk.ss_flags |= SS_ONSTACK;
    679 	else
    680 		p->p_sigacts->ps_sigstk.ss_flags &= ~SS_ONSTACK;
    681 
    682 	/* Restore signal mask. */
    683 	(void) sigprocmask1(p, SIG_SETMASK, &sc.sc_mask, 0);
    684 
    685 	return (EJUSTRETURN);
    686 }
    687 
    688 /*
    689  * Machine dependent system variables.
    690  */
    691 int
    692 cpu_sysctl(name, namelen, oldp, oldlenp, newp, newlen, p)
    693 	int *name;
    694 	u_int namelen;
    695 	void *oldp;
    696 	size_t *oldlenp;
    697 	void *newp;
    698 	size_t newlen;
    699 	struct proc *p;
    700 {
    701 	/* all sysctl names at this level are terminal */
    702 	if (namelen != 1)
    703 		return (ENOTDIR);
    704 
    705 	switch (name[0]) {
    706 	case CPU_CACHELINE:
    707 		return sysctl_rdint(oldp, oldlenp, newp, CACHELINESIZE);
    708 	default:
    709 		return (EOPNOTSUPP);
    710 	}
    711 }
    712 
    713 /*
    714  * Crash dump handling.
    715  */
    716 u_long dumpmag = 0x8fca0101;		/* magic number */
    717 int dumpsize = 0;			/* size of dump in pages */
    718 long dumplo = -1;			/* blocks */
    719 
    720 void
    721 dumpsys()
    722 {
    723 	printf("dumpsys: TBD\n");
    724 }
    725 
    726 /*
    727  * Soft networking interrupts.
    728  */
    729 void
    730 softnet()
    731 {
    732 	int isr = netisr;
    733 
    734 	netisr = 0;
    735 #ifdef	INET
    736 #include "arp.h"
    737 #if NARP > 0
    738 	if (isr & (1 << NETISR_ARP))
    739 		arpintr();
    740 #endif
    741 	if (isr & (1 << NETISR_IP))
    742 		ipintr();
    743 #endif
    744 #ifdef	INET6
    745 	if (isr & (1 << NETISR_IPV6))
    746 		ip6intr();
    747 #endif
    748 #ifdef	IMP
    749 	if (isr & (1 << NETISR_IMP))
    750 		impintr();
    751 #endif
    752 #ifdef	NS
    753 	if (isr & (1 << NETISR_NS))
    754 		nsintr();
    755 #endif
    756 #ifdef	ISO
    757 	if (isr & (1 << NETISR_ISO))
    758 		clnlintr();
    759 #endif
    760 #ifdef	CCITT
    761 	if (isr & (1 << NETISR_CCITT))
    762 		ccittintr();
    763 #endif
    764 #include "ppp.h"
    765 #if NPPP > 0
    766 	if (isr & (1 << NETISR_PPP))
    767 		pppintr();
    768 #endif
    769 }
    770 
    771 /*
    772  * Stray interrupts.
    773  */
    774 void
    775 strayintr(irq)
    776 	int irq;
    777 {
    778 	log(LOG_ERR, "stray interrupt %d\n", irq);
    779 }
    780 
    781 /*
    782  * Halt or reboot the machine after syncing/dumping according to howto.
    783  */
    784 void
    785 cpu_reboot(howto, what)
    786 	int howto;
    787 	char *what;
    788 {
    789 	static int syncing;
    790 	static char str[256];
    791 	char *ap = str, *ap1 = ap;
    792 
    793 	boothowto = howto;
    794 	if (!cold && !(howto & RB_NOSYNC) && !syncing) {
    795 		syncing = 1;
    796 		vfs_shutdown();		/* sync */
    797 		resettodr();		/* set wall clock */
    798 	}
    799 	splhigh();
    800 	if (howto & RB_HALT) {
    801 		doshutdownhooks();
    802 		printf("halted\n\n");
    803 		ppc_exit();
    804 	}
    805 	if (!cold && (howto & RB_DUMP))
    806 		dumpsys();
    807 	doshutdownhooks();
    808 	printf("rebooting\n\n");
    809 	if (what && *what) {
    810 		if (strlen(what) > sizeof str - 5)
    811 			printf("boot string too large, ignored\n");
    812 		else {
    813 			strcpy(str, what);
    814 			ap1 = ap = str + strlen(str);
    815 			*ap++ = ' ';
    816 		}
    817 	}
    818 	*ap++ = '-';
    819 	if (howto & RB_SINGLE)
    820 		*ap++ = 's';
    821 	if (howto & RB_KDB)
    822 		*ap++ = 'd';
    823 	*ap++ = 0;
    824 	if (ap[-2] == '-')
    825 		*ap1 = 0;
    826 	ppc_boot(str);
    827 }
    828 
    829 /*
    830  * OpenFirmware callback routine
    831  */
    832 void
    833 callback(p)
    834 	void *p;
    835 {
    836 	panic("callback");	/* for now			XXX */
    837 }
    838 
    839 /*
    840  * Initial Machine Interface.
    841  */
    842 static int
    843 fake_spl()
    844 {
    845 	int scratch;
    846 
    847 	asm volatile ("mfmsr %0; andi. %0,%0,%1; mtmsr %0; isync"
    848 	    : "=r"(scratch) : "K"((u_short)~(PSL_EE|PSL_ME)));
    849 	return (-1);
    850 }
    851 
    852 static void
    853 fake_setsoft()
    854 {
    855 	/* Do nothing */
    856 }
    857 
    858 static int
    859 fake_splx(new)
    860 	int new;
    861 {
    862 	return (fake_spl());
    863 }
    864 
    865 static void
    866 fake_clock_return(frame, nticks)
    867 	struct clockframe *frame;
    868 	int nticks;
    869 {
    870 	/* Do nothing */
    871 }
    872 
    873 static void
    874 fake_irq_establish(irq, level, handler, arg)
    875 	int irq, level;
    876 	void (*handler) __P((void *));
    877 	void *arg;
    878 {
    879 	panic("fake_irq_establish");
    880 }
    881