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