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