Home | History | Annotate | Line # | Download | only in arm32
arm32_machdep.c revision 1.43
      1 /*	$NetBSD: arm32_machdep.c,v 1.43 2004/02/13 11:36:10 wiz 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 <sys/cdefs.h>
     45 __KERNEL_RCSID(0, "$NetBSD: arm32_machdep.c,v 1.43 2004/02/13 11:36:10 wiz Exp $");
     46 
     47 #include "opt_md.h"
     48 #include "opt_pmap_debug.h"
     49 
     50 #include <sys/param.h>
     51 #include <sys/systm.h>
     52 #include <sys/reboot.h>
     53 #include <sys/proc.h>
     54 #include <sys/user.h>
     55 #include <sys/kernel.h>
     56 #include <sys/mbuf.h>
     57 #include <sys/mount.h>
     58 #include <sys/buf.h>
     59 #include <sys/msgbuf.h>
     60 #include <sys/device.h>
     61 #include <uvm/uvm_extern.h>
     62 #include <sys/sysctl.h>
     63 
     64 #include <dev/cons.h>
     65 
     66 #include <arm/arm32/katelib.h>
     67 #include <arm/arm32/machdep.h>
     68 #include <machine/bootconfig.h>
     69 
     70 #include "opt_ipkdb.h"
     71 #include "md.h"
     72 
     73 struct vm_map *exec_map = NULL;
     74 struct vm_map *mb_map = NULL;
     75 struct vm_map *phys_map = NULL;
     76 
     77 extern int physmem;
     78 
     79 #if NMD > 0 && defined(MEMORY_DISK_HOOKS) && !defined(MEMORY_DISK_ROOT_SIZE)
     80 extern size_t md_root_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 void data_abort_handler		__P((trapframe_t *frame));
    106 void prefetch_abort_handler	__P((trapframe_t *frame));
    107 extern void configure		__P((void));
    108 
    109 /*
    110  * arm32_vector_init:
    111  *
    112  *	Initialize the vector page, and select whether or not to
    113  *	relocate the vectors.
    114  *
    115  *	NOTE: We expect the vector page to be mapped at its expected
    116  *	destination.
    117  */
    118 void
    119 arm32_vector_init(vaddr_t va, int which)
    120 {
    121 	extern unsigned int page0[], page0_data[];
    122 	unsigned int *vectors = (int *) va;
    123 	unsigned int *vectors_data = vectors + (page0_data - page0);
    124 	int vec;
    125 
    126 	/*
    127 	 * Loop through the vectors we're taking over, and copy the
    128 	 * vector's insn and data word.
    129 	 */
    130 	for (vec = 0; vec < ARM_NVEC; vec++) {
    131 		if ((which & (1 << vec)) == 0) {
    132 			/* Don't want to take over this vector. */
    133 			continue;
    134 		}
    135 		vectors[vec] = page0[vec];
    136 		vectors_data[vec] = page0_data[vec];
    137 	}
    138 
    139 	/* Now sync the vectors. */
    140 	cpu_icache_sync_range(va, (ARM_NVEC * 2) * sizeof(u_int));
    141 
    142 	vector_page = va;
    143 
    144 	if (va == ARM_VECTORS_HIGH) {
    145 		/*
    146 		 * Assume the MD caller knows what it's doing here, and
    147 		 * really does want the vector page relocated.
    148 		 *
    149 		 * Note: This has to be done here (and not just in
    150 		 * cpu_setup()) because the vector page needs to be
    151 		 * accessible *before* cpu_startup() is called.
    152 		 * Think ddb(9) ...
    153 		 *
    154 		 * NOTE: If the CPU control register is not readable,
    155 		 * this will totally fail!  We'll just assume that
    156 		 * any system that has high vector support has a
    157 		 * readable CPU control register, for now.  If we
    158 		 * ever encounter one that does not, we'll have to
    159 		 * rethink this.
    160 		 */
    161 		cpu_control(CPU_CONTROL_VECRELOC, CPU_CONTROL_VECRELOC);
    162 	}
    163 }
    164 
    165 /*
    166  * Debug function just to park the CPU
    167  */
    168 
    169 void
    170 halt()
    171 {
    172 	while (1)
    173 		cpu_sleep(0);
    174 }
    175 
    176 
    177 /* Sync the discs and unmount the filesystems */
    178 
    179 void
    180 bootsync(void)
    181 {
    182 	static int bootsyncdone = 0;
    183 
    184 	if (bootsyncdone) return;
    185 
    186 	bootsyncdone = 1;
    187 
    188 	/* Make sure we can still manage to do things */
    189 	if (GetCPSR() & I32_bit) {
    190 		/*
    191 		 * If we get here then boot has been called without RB_NOSYNC
    192 		 * and interrupts were disabled. This means the boot() call
    193 		 * did not come from a user process e.g. shutdown, but must
    194 		 * have come from somewhere in the kernel.
    195 		 */
    196 		IRQenable;
    197 		printf("Warning IRQ's disabled during boot()\n");
    198 	}
    199 
    200 	vfs_shutdown();
    201 }
    202 
    203 /*
    204  * void cpu_startup(void)
    205  *
    206  * Machine dependant startup code.
    207  *
    208  */
    209 void
    210 cpu_startup()
    211 {
    212 	vaddr_t minaddr;
    213 	vaddr_t maxaddr;
    214 	u_int loop;
    215 	char pbuf[9];
    216 
    217 	/* Set the CPU control register */
    218 	cpu_setup(boot_args);
    219 
    220 	/* Lock down zero page */
    221 	vector_page_setprot(VM_PROT_READ);
    222 
    223 	/*
    224 	 * Give pmap a chance to set up a few more things now the vm
    225 	 * is initialised
    226 	 */
    227 	pmap_postinit();
    228 
    229 	/*
    230 	 * Initialize error message buffer (at end of core).
    231 	 */
    232 
    233 	/* msgbufphys was setup during the secondary boot strap */
    234 	for (loop = 0; loop < btoc(MSGBUFSIZE); ++loop)
    235 		pmap_kenter_pa((vaddr_t)msgbufaddr + loop * PAGE_SIZE,
    236 		    msgbufphys + loop * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE);
    237 	pmap_update(pmap_kernel());
    238 	initmsgbuf(msgbufaddr, round_page(MSGBUFSIZE));
    239 
    240 	/*
    241 	 * Identify ourselves for the msgbuf (everything printed earlier will
    242 	 * not be buffered).
    243 	 */
    244 	printf(version);
    245 
    246 	format_bytes(pbuf, sizeof(pbuf), arm_ptob(physmem));
    247 	printf("total memory = %s\n", pbuf);
    248 
    249 	minaddr = 0;
    250 
    251 	/*
    252 	 * Allocate a submap for exec arguments.  This map effectively
    253 	 * limits the number of processes exec'ing at any time.
    254 	 */
    255 	exec_map = uvm_km_suballoc(kernel_map, &minaddr, &maxaddr,
    256 				   16*NCARGS, VM_MAP_PAGEABLE, FALSE, NULL);
    257 
    258 	/*
    259 	 * Allocate a submap for physio
    260 	 */
    261 	phys_map = uvm_km_suballoc(kernel_map, &minaddr, &maxaddr,
    262 				   VM_PHYS_SIZE, 0, FALSE, NULL);
    263 
    264 	/*
    265 	 * Finally, allocate mbuf cluster submap.
    266 	 */
    267 	mb_map = uvm_km_suballoc(kernel_map, &minaddr, &maxaddr,
    268 				 nmbclusters * mclbytes, VM_MAP_INTRSAFE,
    269 				 FALSE, NULL);
    270 
    271 	format_bytes(pbuf, sizeof(pbuf), ptoa(uvmexp.free));
    272 	printf("avail memory = %s\n", pbuf);
    273 
    274 	curpcb = &lwp0.l_addr->u_pcb;
    275 	curpcb->pcb_flags = 0;
    276 	curpcb->pcb_un.un_32.pcb32_und_sp = (u_int)lwp0.l_addr +
    277 	    USPACE_UNDEF_STACK_TOP;
    278 	curpcb->pcb_un.un_32.pcb32_sp = (u_int)lwp0.l_addr +
    279 	    USPACE_SVC_STACK_TOP;
    280 	pmap_set_pcb_pagedir(pmap_kernel(), curpcb);
    281 
    282         curpcb->pcb_tf = (struct trapframe *)curpcb->pcb_un.un_32.pcb32_sp - 1;
    283 }
    284 
    285 /*
    286  * machine dependent system variables.
    287  */
    288 static int
    289 sysctl_machdep_booted_device(SYSCTLFN_ARGS)
    290 {
    291 	struct sysctlnode node;
    292 
    293 	if (booted_device == NULL)
    294 		return (EOPNOTSUPP);
    295 
    296 	node = *rnode;
    297 	node.sysctl_data = booted_device->dv_xname;
    298 	node.sysctl_size = strlen(booted_device->dv_xname) + 1;
    299 	return (sysctl_lookup(SYSCTLFN_CALL(&node)));
    300 }
    301 
    302 static int
    303 sysctl_machdep_booted_kernel(SYSCTLFN_ARGS)
    304 {
    305 	struct sysctlnode node;
    306 
    307 	if (booted_kernel == NULL || booted_kernel[0] == '\0')
    308 		return (EOPNOTSUPP);
    309 
    310 	node = *rnode;
    311 	node.sysctl_data = booted_kernel;
    312 	node.sysctl_size = strlen(booted_kernel) + 1;
    313 	return (sysctl_lookup(SYSCTLFN_CALL(&node)));
    314 }
    315 
    316 static int
    317 sysctl_machdep_powersave(SYSCTLFN_ARGS)
    318 {
    319 	struct sysctlnode node = *rnode;
    320 	int error, newval;
    321 
    322 	newval = cpu_do_powersave;
    323 	node.sysctl_data = &newval;
    324 	if (cpufuncs.cf_sleep == (void *) cpufunc_nullop)
    325 		node.sysctl_flags &= ~SYSCTL_READWRITE;
    326 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
    327 	if (error || newp == NULL || newval == cpu_do_powersave)
    328 		return (error);
    329 
    330 	if (newval < 0 || newval > 1)
    331 		return (EINVAL);
    332 	cpu_do_powersave = newval;
    333 
    334 	return (0);
    335 }
    336 
    337 SYSCTL_SETUP(sysctl_machdep_setup, "sysctl machdep subtree setup")
    338 {
    339 
    340 	sysctl_createv(SYSCTL_PERMANENT,
    341 		       CTLTYPE_NODE, "machdep", NULL,
    342 		       NULL, 0, NULL, 0,
    343 		       CTL_MACHDEP, CTL_EOL);
    344 
    345 	sysctl_createv(SYSCTL_PERMANENT|SYSCTL_READWRITE,
    346 		       CTLTYPE_INT, "debug", NULL,
    347 		       NULL, 0, &kernel_debug, 0,
    348 		       CTL_MACHDEP, CPU_DEBUG, CTL_EOL);
    349 	sysctl_createv(SYSCTL_PERMANENT,
    350 		       CTLTYPE_STRING, "booted_device", NULL,
    351 		       sysctl_machdep_booted_device, 0, NULL, 0,
    352 		       CTL_MACHDEP, CPU_BOOTED_DEVICE, CTL_EOL);
    353 	sysctl_createv(SYSCTL_PERMANENT,
    354 		       CTLTYPE_STRING, "booted_kernel", NULL,
    355 		       sysctl_machdep_booted_kernel, 0, NULL, 0,
    356 		       CTL_MACHDEP, CPU_BOOTED_KERNEL, CTL_EOL);
    357 	sysctl_createv(SYSCTL_PERMANENT,
    358 		       CTLTYPE_STRUCT, "console_device", NULL,
    359 		       sysctl_consdev, 0, NULL, sizeof(dev_t),
    360 		       CTL_MACHDEP, CPU_CONSDEV, CTL_EOL);
    361 	sysctl_createv(SYSCTL_PERMANENT|SYSCTL_READWRITE,
    362 		       CTLTYPE_INT, "powersave", NULL,
    363 		       sysctl_machdep_powersave, 0, &cpu_do_powersave, 0,
    364 		       CTL_MACHDEP, CPU_POWERSAVE, CTL_EOL);
    365 }
    366 
    367 void
    368 parse_mi_bootargs(args)
    369 	char *args;
    370 {
    371 	int integer;
    372 
    373 	if (get_bootconf_option(args, "single", BOOTOPT_TYPE_BOOLEAN, &integer)
    374 	    || get_bootconf_option(args, "-s", BOOTOPT_TYPE_BOOLEAN, &integer))
    375 		if (integer)
    376 			boothowto |= RB_SINGLE;
    377 	if (get_bootconf_option(args, "kdb", BOOTOPT_TYPE_BOOLEAN, &integer)
    378 	    || get_bootconf_option(args, "-k", BOOTOPT_TYPE_BOOLEAN, &integer))
    379 		if (integer)
    380 			boothowto |= RB_KDB;
    381 	if (get_bootconf_option(args, "ask", BOOTOPT_TYPE_BOOLEAN, &integer)
    382 	    || get_bootconf_option(args, "-a", BOOTOPT_TYPE_BOOLEAN, &integer))
    383 		if (integer)
    384 			boothowto |= RB_ASKNAME;
    385 
    386 #ifdef PMAP_DEBUG
    387 	if (get_bootconf_option(args, "pmapdebug", BOOTOPT_TYPE_INT, &integer)) {
    388 		pmap_debug_level = integer;
    389 		pmap_debug(pmap_debug_level);
    390 	}
    391 #endif	/* PMAP_DEBUG */
    392 
    393 /*	if (get_bootconf_option(args, "nbuf", BOOTOPT_TYPE_INT, &integer))
    394 		bufpages = integer;*/
    395 
    396 #if NMD > 0 && defined(MEMORY_DISK_HOOKS) && !defined(MEMORY_DISK_ROOT_SIZE)
    397 	if (get_bootconf_option(args, "memorydisc", BOOTOPT_TYPE_INT, &integer)
    398 	    || get_bootconf_option(args, "memorydisk", BOOTOPT_TYPE_INT, &integer)) {
    399 		md_root_size = integer;
    400 		md_root_size *= 1024;
    401 		if (md_root_size < 32*1024)
    402 			md_root_size = 32*1024;
    403 		if (md_root_size > 2048*1024)
    404 			md_root_size = 2048*1024;
    405 	}
    406 #endif	/* NMD && MEMORY_DISK_HOOKS && !MEMORY_DISK_ROOT_SIZE */
    407 
    408 	if (get_bootconf_option(args, "quiet", BOOTOPT_TYPE_BOOLEAN, &integer)
    409 	    || get_bootconf_option(args, "-q", BOOTOPT_TYPE_BOOLEAN, &integer))
    410 		if (integer)
    411 			boothowto |= AB_QUIET;
    412 	if (get_bootconf_option(args, "verbose", BOOTOPT_TYPE_BOOLEAN, &integer)
    413 	    || get_bootconf_option(args, "-v", BOOTOPT_TYPE_BOOLEAN, &integer))
    414 		if (integer)
    415 			boothowto |= AB_VERBOSE;
    416 }
    417