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