Home | History | Annotate | Line # | Download | only in arm32
arm32_machdep.c revision 1.22
      1 /*	$NetBSD: arm32_machdep.c,v 1.22 2002/04/03 23:33:28 thorpej Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1994-1998 Mark Brinicombe.
      5  * Copyright (c) 1994 Brini.
      6  * All rights reserved.
      7  *
      8  * This code is derived from software written for Brini by Mark Brinicombe
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *	This product includes software developed by Mark Brinicombe
     21  *	for the NetBSD Project.
     22  * 4. The name of the company nor the name of the author may be used to
     23  *    endorse or promote products derived from this software without specific
     24  *    prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
     27  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
     28  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     29  * IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
     30  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     31  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     32  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     36  * SUCH DAMAGE.
     37  *
     38  * Machine dependant functions for kernel setup
     39  *
     40  * Created      : 17/09/94
     41  * Updated	: 18/04/01 updated for new wscons
     42  */
     43 
     44 #include "opt_md.h"
     45 #include "opt_pmap_debug.h"
     46 
     47 #include <sys/param.h>
     48 #include <sys/systm.h>
     49 #include <sys/reboot.h>
     50 #include <sys/proc.h>
     51 #include <sys/user.h>
     52 #include <sys/kernel.h>
     53 #include <sys/mbuf.h>
     54 #include <sys/mount.h>
     55 #include <sys/buf.h>
     56 #include <sys/msgbuf.h>
     57 #include <sys/device.h>
     58 #include <uvm/uvm_extern.h>
     59 #include <sys/sysctl.h>
     60 
     61 #include <dev/cons.h>
     62 
     63 #include <arm/arm32/katelib.h>
     64 #include <arm/arm32/machdep.h>
     65 #include <machine/bootconfig.h>
     66 
     67 #include "opt_ipkdb.h"
     68 #include "md.h"
     69 
     70 struct vm_map *exec_map = NULL;
     71 struct vm_map *mb_map = NULL;
     72 struct vm_map *phys_map = NULL;
     73 
     74 extern int physmem;
     75 
     76 #ifndef PMAP_STATIC_L1S
     77 extern int max_processes;
     78 #endif	/* !PMAP_STATIC_L1S */
     79 #if NMD > 0 && defined(MEMORY_DISK_HOOKS) && !defined(MEMORY_DISK_ROOT_SIZE)
     80 extern u_int memory_disc_size;		/* Memory disc size */
     81 #endif	/* NMD && MEMORY_DISK_HOOKS && !MEMORY_DISK_ROOT_SIZE */
     82 
     83 pv_addr_t kernelstack;
     84 
     85 /* the following is used externally (sysctl_hw) */
     86 char	machine[] = MACHINE;		/* from <machine/param.h> */
     87 char	machine_arch[] = MACHINE_ARCH;	/* from <machine/param.h> */
     88 
     89 /* Our exported CPU info; we can have only one. */
     90 struct cpu_info cpu_info_store;
     91 
     92 caddr_t	msgbufaddr;
     93 extern paddr_t msgbufphys;
     94 
     95 int kernel_debug = 0;
     96 
     97 struct user *proc0paddr;
     98 
     99 /* exported variable to be filled in by the bootloaders */
    100 char *booted_kernel;
    101 
    102 
    103 /* Prototypes */
    104 
    105 u_long strtoul			__P((const char *s, char **ptr, int base));
    106 void data_abort_handler		__P((trapframe_t *frame));
    107 void prefetch_abort_handler	__P((trapframe_t *frame));
    108 extern void configure		__P((void));
    109 
    110 /*
    111  * arm32_vector_init:
    112  *
    113  *	Initialize the vector page, and select whether or not to
    114  *	relocate the vectors.
    115  *
    116  *	NOTE: We expect the vector page to be mapped at its expected
    117  *	destination.
    118  */
    119 void
    120 arm32_vector_init(vaddr_t va, int which)
    121 {
    122 	extern unsigned int page0[], page0_data[];
    123 	unsigned int *vectors = (int *) va;
    124 	unsigned int *vectors_data = vectors + (page0_data - page0);
    125 	unsigned int ctrl;
    126 	int vec;
    127 
    128 	/* Make sure we have a legal vector page VA. */
    129 	switch (va) {
    130 	case ARM_VECTORS_LOW:
    131 		ctrl = 0;
    132 		break;
    133 
    134 	case ARM_VECTORS_HIGH:
    135 		ctrl = CPU_CONTROL_VECRELOC;
    136 		break;
    137 
    138 	default:
    139 		panic("arm32_vector_init: invalid vector address: 0x%08lx\n",
    140 		    va);
    141 		/* NOTREACHED */
    142 	}
    143 
    144 	/*
    145 	 * Loop through the vectors we're taking over, and copy the
    146 	 * vector's insn and data word.
    147 	 */
    148 	for (vec = 0; vec < ARM_NVEC; vec++) {
    149 		if ((which & (1 << vec)) == 0) {
    150 			/* Don't want to take over this vector. */
    151 			continue;
    152 		}
    153 		vectors[vec] = page0[vec];
    154 		vectors_data[vec] = page0_data[vec];
    155 	}
    156 
    157 	/* Now sync the vectors. */
    158 	cpu_icache_sync_range(va, (ARM_NVEC * 2) * sizeof(u_int));
    159 
    160 	vector_page = va;
    161 	cpu_control(CPU_CONTROL_VECRELOC, ctrl);
    162 }
    163 
    164 /*
    165  * Debug function just to park the CPU
    166  */
    167 
    168 void
    169 halt()
    170 {
    171 	while (1)
    172 		cpu_sleep(0);
    173 }
    174 
    175 
    176 /* Sync the discs and unmount the filesystems */
    177 
    178 void
    179 bootsync(void)
    180 {
    181 	static int bootsyncdone = 0;
    182 
    183 	if (bootsyncdone) return;
    184 
    185 	bootsyncdone = 1;
    186 
    187 	/* Make sure we can still manage to do things */
    188 	if (GetCPSR() & I32_bit) {
    189 		/*
    190 		 * If we get here then boot has been called without RB_NOSYNC
    191 		 * and interrupts were disabled. This means the boot() call
    192 		 * did not come from a user process e.g. shutdown, but must
    193 		 * have come from somewhere in the kernel.
    194 		 */
    195 		IRQenable;
    196 		printf("Warning IRQ's disabled during boot()\n");
    197 	}
    198 
    199 	vfs_shutdown();
    200 }
    201 
    202 /*
    203  * void cpu_startup(void)
    204  *
    205  * Machine dependant startup code.
    206  *
    207  */
    208 void
    209 cpu_startup()
    210 {
    211 	int loop;
    212 	paddr_t minaddr;
    213 	paddr_t maxaddr;
    214 	caddr_t sysbase;
    215 	caddr_t size;
    216 	vsize_t bufsize;
    217 	int base, residual;
    218 	char pbuf[9];
    219 
    220 	proc0paddr = (struct user *)kernelstack.pv_va;
    221 	proc0.p_addr = proc0paddr;
    222 
    223 	/* Set the cpu control register */
    224 	cpu_setup(boot_args);
    225 
    226 	/* All domains MUST be clients, permissions are VERY important */
    227 	cpu_domains(DOMAIN_CLIENT);
    228 
    229 	/* Lock down zero page */
    230 	vector_page_setprot(VM_PROT_READ);
    231 
    232 	/*
    233 	 * Give pmap a chance to set up a few more things now the vm
    234 	 * is initialised
    235 	 */
    236 	pmap_postinit();
    237 
    238 	/*
    239 	 * Initialize error message buffer (at end of core).
    240 	 */
    241 
    242 	/* msgbufphys was setup during the secondary boot strap */
    243 	for (loop = 0; loop < btoc(MSGBUFSIZE); ++loop)
    244 		pmap_kenter_pa((vaddr_t)msgbufaddr + loop * NBPG,
    245 		    msgbufphys + loop * NBPG, VM_PROT_READ|VM_PROT_WRITE);
    246 	pmap_update(pmap_kernel());
    247 	initmsgbuf(msgbufaddr, round_page(MSGBUFSIZE));
    248 
    249 	/*
    250 	 * Identify ourselves for the msgbuf (everything printed earlier will
    251 	 * not be buffered).
    252 	 */
    253 	printf(version);
    254 
    255 	format_bytes(pbuf, sizeof(pbuf), arm_ptob(physmem));
    256 	printf("total memory = %s\n", pbuf);
    257 
    258 	/*
    259 	 * Find out how much space we need, allocate it,
    260 	 * and then give everything true virtual addresses.
    261 	 */
    262 	size = allocsys(NULL, NULL);
    263 	sysbase = (caddr_t)uvm_km_zalloc(kernel_map, round_page((vaddr_t)size));
    264 	if (sysbase == 0)
    265 		panic(
    266 		    "cpu_startup: no room for system tables; %d bytes required",
    267 		    (u_int)size);
    268 	if ((caddr_t)((allocsys(sysbase, NULL) - sysbase)) != size)
    269 		panic("cpu_startup: system table size inconsistency");
    270 
    271    	/*
    272 	 * Now allocate buffers proper.  They are different than the above
    273 	 * in that they usually occupy more virtual memory than physical.
    274 	 */
    275 	bufsize = MAXBSIZE * nbuf;
    276 	if (uvm_map(kernel_map, (vaddr_t *)&buffers, round_page(bufsize),
    277 	    NULL, UVM_UNKNOWN_OFFSET, 0,
    278 	    UVM_MAPFLAG(UVM_PROT_NONE, UVM_PROT_NONE, UVM_INH_NONE,
    279 	    UVM_ADV_NORMAL, 0)) != 0)
    280 		panic("cpu_startup: cannot allocate UVM space for buffers");
    281 	minaddr = (vaddr_t)buffers;
    282 	if ((bufpages / nbuf) >= btoc(MAXBSIZE)) {
    283 		/* don't want to alloc more physical mem than needed */
    284 		bufpages = btoc(MAXBSIZE) * nbuf;
    285 	}
    286 
    287 	base = bufpages / nbuf;
    288 	residual = bufpages % nbuf;
    289 	for (loop = 0; loop < nbuf; ++loop) {
    290 		vsize_t curbufsize;
    291 		vaddr_t curbuf;
    292 		struct vm_page *pg;
    293 
    294 		/*
    295 		 * Each buffer has MAXBSIZE bytes of VM space allocated.  Of
    296 		 * that MAXBSIZE space, we allocate and map (base+1) pages
    297 		 * for the first "residual" buffers, and then we allocate
    298 		 * "base" pages for the rest.
    299 		 */
    300 		curbuf = (vaddr_t) buffers + (loop * MAXBSIZE);
    301 		curbufsize = NBPG * ((loop < residual) ? (base+1) : base);
    302 
    303 		while (curbufsize) {
    304 			pg = uvm_pagealloc(NULL, 0, NULL, 0);
    305 			if (pg == NULL)
    306 				panic("cpu_startup: not enough memory for buffer cache");
    307 			pmap_kenter_pa(curbuf, VM_PAGE_TO_PHYS(pg),
    308 				VM_PROT_READ|VM_PROT_WRITE);
    309 			curbuf += PAGE_SIZE;
    310 			curbufsize -= PAGE_SIZE;
    311 		}
    312 	}
    313 	pmap_update(pmap_kernel());
    314 
    315 	/*
    316 	 * Allocate a submap for exec arguments.  This map effectively
    317 	 * limits the number of processes exec'ing at any time.
    318 	 */
    319 	exec_map = uvm_km_suballoc(kernel_map, &minaddr, &maxaddr,
    320 				   16*NCARGS, VM_MAP_PAGEABLE, FALSE, NULL);
    321 
    322 	/*
    323 	 * Allocate a submap for physio
    324 	 */
    325 	phys_map = uvm_km_suballoc(kernel_map, &minaddr, &maxaddr,
    326 				   VM_PHYS_SIZE, 0, FALSE, NULL);
    327 
    328 	/*
    329 	 * Finally, allocate mbuf cluster submap.
    330 	 */
    331 	mb_map = uvm_km_suballoc(kernel_map, &minaddr, &maxaddr,
    332 				 nmbclusters * mclbytes, VM_MAP_INTRSAFE,
    333 				 FALSE, NULL);
    334 
    335 	format_bytes(pbuf, sizeof(pbuf), ptoa(uvmexp.free));
    336 	printf("avail memory = %s\n", pbuf);
    337 	format_bytes(pbuf, sizeof(pbuf), bufpages * NBPG);
    338 	printf("using %d buffers containing %s of memory\n", nbuf, pbuf);
    339 
    340 	/*
    341 	 * Set up buffers, so they can be used to read disk labels.
    342 	 */
    343 	bufinit();
    344 
    345 	curpcb = &proc0.p_addr->u_pcb;
    346 	curpcb->pcb_flags = 0;
    347 	curpcb->pcb_un.un_32.pcb32_und_sp = (u_int)proc0.p_addr +
    348 	    USPACE_UNDEF_STACK_TOP;
    349 	curpcb->pcb_un.un_32.pcb32_sp = (u_int)proc0.p_addr +
    350 	    USPACE_SVC_STACK_TOP;
    351 	(void) pmap_extract(pmap_kernel(), (vaddr_t)(pmap_kernel())->pm_pdir,
    352 	    (paddr_t *)&curpcb->pcb_pagedir);
    353 
    354         curpcb->pcb_tf = (struct trapframe *)curpcb->pcb_un.un_32.pcb32_sp - 1;
    355 }
    356 
    357 /*
    358  * machine dependent system variables.
    359  */
    360 
    361 int
    362 cpu_sysctl(name, namelen, oldp, oldlenp, newp, newlen, p)
    363 	int *name;
    364 	u_int namelen;
    365 	void *oldp;
    366 	size_t *oldlenp;
    367 	void *newp;
    368 	size_t newlen;
    369 	struct proc *p;
    370 {
    371 	/* all sysctl names at this level are terminal */
    372 	if (namelen != 1)
    373 		return (ENOTDIR);		/* overloaded */
    374 
    375 	switch (name[0]) {
    376 	case CPU_DEBUG:
    377 		return(sysctl_int(oldp, oldlenp, newp, newlen, &kernel_debug));
    378 
    379 	case CPU_BOOTED_DEVICE:
    380 		if (booted_device != NULL)
    381 			return (sysctl_rdstring(oldp, oldlenp, newp,
    382 			    booted_device->dv_xname));
    383 		return (EOPNOTSUPP);
    384 
    385 	case CPU_CONSDEV: {
    386 		dev_t consdev;
    387 		if (cn_tab != NULL)
    388 			consdev = cn_tab->cn_dev;
    389 		else
    390 			consdev = NODEV;
    391 		return (sysctl_rdstruct(oldp, oldlenp, newp, &consdev,
    392 			sizeof consdev));
    393 	}
    394 	case CPU_BOOTED_KERNEL: {
    395 		if (booted_kernel != NULL && booted_kernel[0] != '\0')
    396 			return sysctl_rdstring(oldp, oldlenp, newp,
    397 			    booted_kernel);
    398 		return (EOPNOTSUPP);
    399 	}
    400 
    401 	default:
    402 		return (EOPNOTSUPP);
    403 	}
    404 	/* NOTREACHED */
    405 }
    406 
    407 void
    408 parse_mi_bootargs(args)
    409 	char *args;
    410 {
    411 	int integer;
    412 
    413 	if (get_bootconf_option(args, "single", BOOTOPT_TYPE_BOOLEAN, &integer)
    414 	    || get_bootconf_option(args, "-s", BOOTOPT_TYPE_BOOLEAN, &integer))
    415 		if (integer)
    416 			boothowto |= RB_SINGLE;
    417 	if (get_bootconf_option(args, "kdb", BOOTOPT_TYPE_BOOLEAN, &integer)
    418 	    || get_bootconf_option(args, "-k", BOOTOPT_TYPE_BOOLEAN, &integer))
    419 		if (integer)
    420 			boothowto |= RB_KDB;
    421 	if (get_bootconf_option(args, "ask", BOOTOPT_TYPE_BOOLEAN, &integer)
    422 	    || get_bootconf_option(args, "-a", BOOTOPT_TYPE_BOOLEAN, &integer))
    423 		if (integer)
    424 			boothowto |= RB_ASKNAME;
    425 
    426 #ifdef PMAP_DEBUG
    427 	if (get_bootconf_option(args, "pmapdebug", BOOTOPT_TYPE_INT, &integer)) {
    428 		pmap_debug_level = integer;
    429 		pmap_debug(pmap_debug_level);
    430 	}
    431 #endif	/* PMAP_DEBUG */
    432 
    433 /*	if (get_bootconf_option(args, "nbuf", BOOTOPT_TYPE_INT, &integer))
    434 		bufpages = integer;*/
    435 
    436 #ifndef PMAP_STATIC_L1S
    437 	if (get_bootconf_option(args, "maxproc", BOOTOPT_TYPE_INT, &integer)) {
    438 		max_processes = integer;
    439 		if (max_processes < 16)
    440 			max_processes = 16;
    441 		/* Limit is PDSIZE * (max_processes + 1) <= 4MB */
    442 		if (max_processes > 255)
    443 			max_processes = 255;
    444 	}
    445 #endif	/* !PMAP_STATUC_L1S */
    446 #if NMD > 0 && defined(MEMORY_DISK_HOOKS) && !defined(MEMORY_DISK_ROOT_SIZE)
    447 	if (get_bootconf_option(args, "memorydisc", BOOTOPT_TYPE_INT, &integer)
    448 	    || get_bootconf_option(args, "memorydisk", BOOTOPT_TYPE_INT, &integer)) {
    449 		memory_disc_size = integer;
    450 		memory_disc_size *= 1024;
    451 		if (memory_disc_size < 32*1024)
    452 			memory_disc_size = 32*1024;
    453 		if (memory_disc_size > 2048*1024)
    454 			memory_disc_size = 2048*1024;
    455 	}
    456 #endif	/* NMD && MEMORY_DISK_HOOKS && !MEMORY_DISK_ROOT_SIZE */
    457 
    458 	if (get_bootconf_option(args, "quiet", BOOTOPT_TYPE_BOOLEAN, &integer)
    459 	    || get_bootconf_option(args, "-q", BOOTOPT_TYPE_BOOLEAN, &integer))
    460 		if (integer)
    461 			boothowto |= AB_QUIET;
    462 	if (get_bootconf_option(args, "verbose", BOOTOPT_TYPE_BOOLEAN, &integer)
    463 	    || get_bootconf_option(args, "-v", BOOTOPT_TYPE_BOOLEAN, &integer))
    464 		if (integer)
    465 			boothowto |= AB_VERBOSE;
    466 }
    467