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