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