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