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