Home | History | Annotate | Line # | Download | only in arm32
arm32_machdep.c revision 1.20
      1 /*	$NetBSD: arm32_machdep.c,v 1.20 2002/03/24 03:37:18 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_SIZE)
     80 extern u_int memory_disc_size;		/* Memory disc size */
     81 #endif	/* NMD && MEMORY_DISK_HOOKS && !MEMORY_DISK_SIZE */
     82 
     83 pv_addr_t systempage;
     84 pv_addr_t kernelstack;
     85 
     86 /* the following is used externally (sysctl_hw) */
     87 char	machine[] = MACHINE;		/* from <machine/param.h> */
     88 char	machine_arch[] = MACHINE_ARCH;	/* from <machine/param.h> */
     89 
     90 /* Our exported CPU info; we can have only one. */
     91 struct cpu_info cpu_info_store;
     92 
     93 caddr_t	msgbufaddr;
     94 extern paddr_t msgbufphys;
     95 
     96 int kernel_debug = 0;
     97 
     98 struct user *proc0paddr;
     99 
    100 /* exported variable to be filled in by the bootloaders */
    101 char *booted_kernel;
    102 
    103 
    104 /* Prototypes */
    105 
    106 u_long strtoul			__P((const char *s, char **ptr, int base));
    107 void data_abort_handler		__P((trapframe_t *frame));
    108 void prefetch_abort_handler	__P((trapframe_t *frame));
    109 void zero_page_readonly		__P((void));
    110 void zero_page_readwrite	__P((void));
    111 extern void configure		__P((void));
    112 
    113 /*
    114  * Debug function just to park the CPU
    115  */
    116 
    117 void
    118 halt()
    119 {
    120 	while (1)
    121 		cpu_sleep(0);
    122 }
    123 
    124 
    125 /* Sync the discs and unmount the filesystems */
    126 
    127 void
    128 bootsync(void)
    129 {
    130 	static int bootsyncdone = 0;
    131 
    132 	if (bootsyncdone) return;
    133 
    134 	bootsyncdone = 1;
    135 
    136 	/* Make sure we can still manage to do things */
    137 	if (GetCPSR() & I32_bit) {
    138 		/*
    139 		 * If we get here then boot has been called without RB_NOSYNC
    140 		 * and interrupts were disabled. This means the boot() call
    141 		 * did not come from a user process e.g. shutdown, but must
    142 		 * have come from somewhere in the kernel.
    143 		 */
    144 		IRQenable;
    145 		printf("Warning IRQ's disabled during boot()\n");
    146 	}
    147 
    148 	vfs_shutdown();
    149 }
    150 
    151 /*
    152  * void cpu_startup(void)
    153  *
    154  * Machine dependant startup code.
    155  *
    156  */
    157 
    158 void
    159 cpu_startup()
    160 {
    161 	int loop;
    162 	paddr_t minaddr;
    163 	paddr_t maxaddr;
    164 	caddr_t sysbase;
    165 	caddr_t size;
    166 	vsize_t bufsize;
    167 	int base, residual;
    168 	char pbuf[9];
    169 
    170 	proc0paddr = (struct user *)kernelstack.pv_va;
    171 	proc0.p_addr = proc0paddr;
    172 
    173 	/* Set the cpu control register */
    174 	cpu_setup(boot_args);
    175 
    176 	/* All domains MUST be clients, permissions are VERY important */
    177 	cpu_domains(DOMAIN_CLIENT);
    178 
    179 	/* Lock down zero page */
    180 	zero_page_readonly();
    181 
    182 	/*
    183 	 * Give pmap a chance to set up a few more things now the vm
    184 	 * is initialised
    185 	 */
    186 	pmap_postinit();
    187 
    188 	/*
    189 	 * Initialize error message buffer (at end of core).
    190 	 */
    191 
    192 	/* msgbufphys was setup during the secondary boot strap */
    193 	for (loop = 0; loop < btoc(MSGBUFSIZE); ++loop)
    194 		pmap_kenter_pa((vaddr_t)msgbufaddr + loop * NBPG,
    195 		    msgbufphys + loop * NBPG, VM_PROT_READ|VM_PROT_WRITE);
    196 	pmap_update(pmap_kernel());
    197 	initmsgbuf(msgbufaddr, round_page(MSGBUFSIZE));
    198 
    199 	/*
    200 	 * Identify ourselves for the msgbuf (everything printed earlier will
    201 	 * not be buffered).
    202 	 */
    203 	printf(version);
    204 
    205 	format_bytes(pbuf, sizeof(pbuf), arm_ptob(physmem));
    206 	printf("total memory = %s\n", pbuf);
    207 
    208 	/*
    209 	 * Find out how much space we need, allocate it,
    210 	 * and then give everything true virtual addresses.
    211 	 */
    212 	size = allocsys(NULL, NULL);
    213 	sysbase = (caddr_t)uvm_km_zalloc(kernel_map, round_page((vaddr_t)size));
    214 	if (sysbase == 0)
    215 		panic(
    216 		    "cpu_startup: no room for system tables; %d bytes required",
    217 		    (u_int)size);
    218 	if ((caddr_t)((allocsys(sysbase, NULL) - sysbase)) != size)
    219 		panic("cpu_startup: system table size inconsistency");
    220 
    221    	/*
    222 	 * Now allocate buffers proper.  They are different than the above
    223 	 * in that they usually occupy more virtual memory than physical.
    224 	 */
    225 	bufsize = MAXBSIZE * nbuf;
    226 	if (uvm_map(kernel_map, (vaddr_t *)&buffers, round_page(bufsize),
    227 	    NULL, UVM_UNKNOWN_OFFSET, 0,
    228 	    UVM_MAPFLAG(UVM_PROT_NONE, UVM_PROT_NONE, UVM_INH_NONE,
    229 	    UVM_ADV_NORMAL, 0)) != 0)
    230 		panic("cpu_startup: cannot allocate UVM space for buffers");
    231 	minaddr = (vaddr_t)buffers;
    232 	if ((bufpages / nbuf) >= btoc(MAXBSIZE)) {
    233 		/* don't want to alloc more physical mem than needed */
    234 		bufpages = btoc(MAXBSIZE) * nbuf;
    235 	}
    236 
    237 	base = bufpages / nbuf;
    238 	residual = bufpages % nbuf;
    239 	for (loop = 0; loop < nbuf; ++loop) {
    240 		vsize_t curbufsize;
    241 		vaddr_t curbuf;
    242 		struct vm_page *pg;
    243 
    244 		/*
    245 		 * Each buffer has MAXBSIZE bytes of VM space allocated.  Of
    246 		 * that MAXBSIZE space, we allocate and map (base+1) pages
    247 		 * for the first "residual" buffers, and then we allocate
    248 		 * "base" pages for the rest.
    249 		 */
    250 		curbuf = (vaddr_t) buffers + (loop * MAXBSIZE);
    251 		curbufsize = NBPG * ((loop < residual) ? (base+1) : base);
    252 
    253 		while (curbufsize) {
    254 			pg = uvm_pagealloc(NULL, 0, NULL, 0);
    255 			if (pg == NULL)
    256 				panic("cpu_startup: not enough memory for buffer cache");
    257 			pmap_kenter_pa(curbuf, VM_PAGE_TO_PHYS(pg),
    258 				VM_PROT_READ|VM_PROT_WRITE);
    259 			curbuf += PAGE_SIZE;
    260 			curbufsize -= PAGE_SIZE;
    261 		}
    262 	}
    263 	pmap_update(pmap_kernel());
    264 
    265 	/*
    266 	 * Allocate a submap for exec arguments.  This map effectively
    267 	 * limits the number of processes exec'ing at any time.
    268 	 */
    269 	exec_map = uvm_km_suballoc(kernel_map, &minaddr, &maxaddr,
    270 				   16*NCARGS, VM_MAP_PAGEABLE, FALSE, NULL);
    271 
    272 	/*
    273 	 * Allocate a submap for physio
    274 	 */
    275 	phys_map = uvm_km_suballoc(kernel_map, &minaddr, &maxaddr,
    276 				   VM_PHYS_SIZE, 0, FALSE, NULL);
    277 
    278 	/*
    279 	 * Finally, allocate mbuf cluster submap.
    280 	 */
    281 	mb_map = uvm_km_suballoc(kernel_map, &minaddr, &maxaddr,
    282 				 nmbclusters * mclbytes, VM_MAP_INTRSAFE,
    283 				 FALSE, NULL);
    284 
    285 	format_bytes(pbuf, sizeof(pbuf), ptoa(uvmexp.free));
    286 	printf("avail memory = %s\n", pbuf);
    287 	format_bytes(pbuf, sizeof(pbuf), bufpages * NBPG);
    288 	printf("using %d buffers containing %s of memory\n", nbuf, pbuf);
    289 
    290 	/*
    291 	 * Set up buffers, so they can be used to read disk labels.
    292 	 */
    293 	bufinit();
    294 
    295 	curpcb = &proc0.p_addr->u_pcb;
    296 	curpcb->pcb_flags = 0;
    297 	curpcb->pcb_un.un_32.pcb32_und_sp = (u_int)proc0.p_addr +
    298 	    USPACE_UNDEF_STACK_TOP;
    299 	curpcb->pcb_un.un_32.pcb32_sp = (u_int)proc0.p_addr +
    300 	    USPACE_SVC_STACK_TOP;
    301 	(void) pmap_extract(pmap_kernel(), (vaddr_t)(pmap_kernel())->pm_pdir,
    302 	    (paddr_t *)&curpcb->pcb_pagedir);
    303 
    304         curpcb->pcb_tf = (struct trapframe *)curpcb->pcb_un.un_32.pcb32_sp - 1;
    305 }
    306 
    307 /*
    308  * Modify the current mapping for zero page to make it read only
    309  *
    310  * This routine is only used until things start forking. Then new
    311  * system pages are mapped read only in pmap_enter().
    312  */
    313 
    314 void
    315 zero_page_readonly()
    316 {
    317 	WriteWord(PTE_BASE + 0, L2_PTE((systempage.pv_pa & PG_FRAME), AP_KR));
    318 	cpu_tlb_flushID_SE(0x00000000);
    319 }
    320 
    321 
    322 /*
    323  * Modify the current mapping for zero page to make it read/write
    324  *
    325  * This routine is only used until things start forking. Then system
    326  * pages belonging to user processes are never made writable.
    327  */
    328 
    329 void
    330 zero_page_readwrite()
    331 {
    332 	WriteWord(PTE_BASE + 0, L2_PTE((systempage.pv_pa & PG_FRAME), AP_KRW));
    333 	cpu_tlb_flushID_SE(0x00000000);
    334 }
    335 
    336 
    337 /*
    338  * machine dependent system variables.
    339  */
    340 
    341 int
    342 cpu_sysctl(name, namelen, oldp, oldlenp, newp, newlen, p)
    343 	int *name;
    344 	u_int namelen;
    345 	void *oldp;
    346 	size_t *oldlenp;
    347 	void *newp;
    348 	size_t newlen;
    349 	struct proc *p;
    350 {
    351 	/* all sysctl names at this level are terminal */
    352 	if (namelen != 1)
    353 		return (ENOTDIR);		/* overloaded */
    354 
    355 	switch (name[0]) {
    356 	case CPU_DEBUG:
    357 		return(sysctl_int(oldp, oldlenp, newp, newlen, &kernel_debug));
    358 
    359 	case CPU_BOOTED_DEVICE:
    360 		if (booted_device != NULL)
    361 			return (sysctl_rdstring(oldp, oldlenp, newp,
    362 			    booted_device->dv_xname));
    363 		return (EOPNOTSUPP);
    364 
    365 	case CPU_CONSDEV: {
    366 		dev_t consdev;
    367 		if (cn_tab != NULL)
    368 			consdev = cn_tab->cn_dev;
    369 		else
    370 			consdev = NODEV;
    371 		return (sysctl_rdstruct(oldp, oldlenp, newp, &consdev,
    372 			sizeof consdev));
    373 	}
    374 	case CPU_BOOTED_KERNEL: {
    375 		if (booted_kernel != NULL && booted_kernel[0] != '\0')
    376 			return sysctl_rdstring(oldp, oldlenp, newp,
    377 			    booted_kernel);
    378 		return (EOPNOTSUPP);
    379 	}
    380 
    381 	default:
    382 		return (EOPNOTSUPP);
    383 	}
    384 	/* NOTREACHED */
    385 }
    386 
    387 void
    388 parse_mi_bootargs(args)
    389 	char *args;
    390 {
    391 	int integer;
    392 
    393 	if (get_bootconf_option(args, "single", BOOTOPT_TYPE_BOOLEAN, &integer)
    394 	    || get_bootconf_option(args, "-s", BOOTOPT_TYPE_BOOLEAN, &integer))
    395 		if (integer)
    396 			boothowto |= RB_SINGLE;
    397 	if (get_bootconf_option(args, "kdb", BOOTOPT_TYPE_BOOLEAN, &integer)
    398 	    || get_bootconf_option(args, "-k", BOOTOPT_TYPE_BOOLEAN, &integer))
    399 		if (integer)
    400 			boothowto |= RB_KDB;
    401 	if (get_bootconf_option(args, "ask", BOOTOPT_TYPE_BOOLEAN, &integer)
    402 	    || get_bootconf_option(args, "-a", BOOTOPT_TYPE_BOOLEAN, &integer))
    403 		if (integer)
    404 			boothowto |= RB_ASKNAME;
    405 
    406 #ifdef PMAP_DEBUG
    407 	if (get_bootconf_option(args, "pmapdebug", BOOTOPT_TYPE_INT, &integer)) {
    408 		pmap_debug_level = integer;
    409 		pmap_debug(pmap_debug_level);
    410 	}
    411 #endif	/* PMAP_DEBUG */
    412 
    413 /*	if (get_bootconf_option(args, "nbuf", BOOTOPT_TYPE_INT, &integer))
    414 		bufpages = integer;*/
    415 
    416 #ifndef PMAP_STATIC_L1S
    417 	if (get_bootconf_option(args, "maxproc", BOOTOPT_TYPE_INT, &integer)) {
    418 		max_processes = integer;
    419 		if (max_processes < 16)
    420 			max_processes = 16;
    421 		/* Limit is PDSIZE * (max_processes + 1) <= 4MB */
    422 		if (max_processes > 255)
    423 			max_processes = 255;
    424 	}
    425 #endif	/* !PMAP_STATUC_L1S */
    426 #if NMD > 0 && defined(MEMORY_DISK_HOOKS) && !defined(MEMORY_DISK_SIZE)
    427 	if (get_bootconf_option(args, "memorydisc", BOOTOPT_TYPE_INT, &integer)
    428 	    || get_bootconf_option(args, "memorydisk", BOOTOPT_TYPE_INT, &integer)) {
    429 		memory_disc_size = integer;
    430 		memory_disc_size *= 1024;
    431 		if (memory_disc_size < 32*1024)
    432 			memory_disc_size = 32*1024;
    433 		if (memory_disc_size > 2048*1024)
    434 			memory_disc_size = 2048*1024;
    435 	}
    436 #endif	/* NMD && MEMORY_DISK_HOOKS && !MEMORY_DISK_SIZE */
    437 
    438 	if (get_bootconf_option(args, "quiet", BOOTOPT_TYPE_BOOLEAN, &integer)
    439 	    || get_bootconf_option(args, "-q", BOOTOPT_TYPE_BOOLEAN, &integer))
    440 		if (integer)
    441 			boothowto |= AB_QUIET;
    442 	if (get_bootconf_option(args, "verbose", BOOTOPT_TYPE_BOOLEAN, &integer)
    443 	    || get_bootconf_option(args, "-v", BOOTOPT_TYPE_BOOLEAN, &integer))
    444 		if (integer)
    445 			boothowto |= AB_VERBOSE;
    446 }
    447 
    448 /* End of machdep.c */
    449