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