arm32_machdep.c revision 1.6.2.5 1 /* $NetBSD: arm32_machdep.c,v 1.6.2.5 2002/02/28 04:07:20 nathanw 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 * void cpu_startup(void)
155 *
156 * Machine dependant startup code.
157 *
158 */
159
160 void
161 cpu_startup()
162 {
163 int loop;
164 paddr_t minaddr;
165 paddr_t maxaddr;
166 caddr_t sysbase;
167 caddr_t size;
168 vsize_t bufsize;
169 int base, residual;
170 char pbuf[9];
171
172 proc0paddr = (struct user *)kernelstack.pv_va;
173 lwp0.l_addr = proc0paddr;
174
175 /* Set the cpu control register */
176 cpu_setup(boot_args);
177
178 /* All domains MUST be clients, permissions are VERY important */
179 cpu_domains(DOMAIN_CLIENT);
180
181 /* Lock down zero page */
182 zero_page_readonly();
183
184 /*
185 * Give pmap a chance to set up a few more things now the vm
186 * is initialised
187 */
188 pmap_postinit();
189
190 /*
191 * Initialize error message buffer (at end of core).
192 */
193
194 /* msgbufphys was setup during the secondary boot strap */
195 for (loop = 0; loop < btoc(MSGBUFSIZE); ++loop)
196 pmap_kenter_pa((vaddr_t)msgbufaddr + loop * NBPG,
197 msgbufphys + loop * NBPG, VM_PROT_READ|VM_PROT_WRITE);
198 pmap_update(pmap_kernel());
199 initmsgbuf(msgbufaddr, round_page(MSGBUFSIZE));
200
201 /*
202 * Identify ourselves for the msgbuf (everything printed earlier will
203 * not be buffered).
204 */
205 printf(version);
206
207 format_bytes(pbuf, sizeof(pbuf), arm_page_to_byte(physmem));
208 printf("total memory = %s\n", pbuf);
209
210 /*
211 * Find out how much space we need, allocate it,
212 * and then give everything true virtual addresses.
213 */
214 size = allocsys(NULL, NULL);
215 sysbase = (caddr_t)uvm_km_zalloc(kernel_map, round_page((vaddr_t)size));
216 if (sysbase == 0)
217 panic(
218 "cpu_startup: no room for system tables; %d bytes required",
219 (u_int)size);
220 if ((caddr_t)((allocsys(sysbase, NULL) - sysbase)) != size)
221 panic("cpu_startup: system table size inconsistency");
222
223 /*
224 * Now allocate buffers proper. They are different than the above
225 * in that they usually occupy more virtual memory than physical.
226 */
227 bufsize = MAXBSIZE * nbuf;
228 if (uvm_map(kernel_map, (vaddr_t *)&buffers, round_page(bufsize),
229 NULL, UVM_UNKNOWN_OFFSET, 0,
230 UVM_MAPFLAG(UVM_PROT_NONE, UVM_PROT_NONE, UVM_INH_NONE,
231 UVM_ADV_NORMAL, 0)) != 0)
232 panic("cpu_startup: cannot allocate UVM space for buffers");
233 minaddr = (vaddr_t)buffers;
234 if ((bufpages / nbuf) >= btoc(MAXBSIZE)) {
235 /* don't want to alloc more physical mem than needed */
236 bufpages = btoc(MAXBSIZE) * nbuf;
237 }
238
239 base = bufpages / nbuf;
240 residual = bufpages % nbuf;
241 for (loop = 0; loop < nbuf; ++loop) {
242 vsize_t curbufsize;
243 vaddr_t curbuf;
244 struct vm_page *pg;
245
246 /*
247 * Each buffer has MAXBSIZE bytes of VM space allocated. Of
248 * that MAXBSIZE space, we allocate and map (base+1) pages
249 * for the first "residual" buffers, and then we allocate
250 * "base" pages for the rest.
251 */
252 curbuf = (vaddr_t) buffers + (loop * MAXBSIZE);
253 curbufsize = NBPG * ((loop < residual) ? (base+1) : base);
254
255 while (curbufsize) {
256 pg = uvm_pagealloc(NULL, 0, NULL, 0);
257 if (pg == NULL)
258 panic("cpu_startup: not enough memory for buffer cache");
259 pmap_kenter_pa(curbuf, VM_PAGE_TO_PHYS(pg),
260 VM_PROT_READ|VM_PROT_WRITE);
261 curbuf += PAGE_SIZE;
262 curbufsize -= PAGE_SIZE;
263 }
264 }
265 pmap_update(pmap_kernel());
266
267 /*
268 * Allocate a submap for exec arguments. This map effectively
269 * limits the number of processes exec'ing at any time.
270 */
271 exec_map = uvm_km_suballoc(kernel_map, &minaddr, &maxaddr,
272 16*NCARGS, VM_MAP_PAGEABLE, FALSE, NULL);
273
274 /*
275 * Allocate a submap for physio
276 */
277 phys_map = uvm_km_suballoc(kernel_map, &minaddr, &maxaddr,
278 VM_PHYS_SIZE, 0, FALSE, NULL);
279
280 /*
281 * Finally, allocate mbuf cluster submap.
282 */
283 mb_map = uvm_km_suballoc(kernel_map, &minaddr, &maxaddr,
284 nmbclusters * mclbytes, VM_MAP_INTRSAFE,
285 FALSE, NULL);
286
287 format_bytes(pbuf, sizeof(pbuf), ptoa(uvmexp.free));
288 printf("avail memory = %s\n", pbuf);
289 format_bytes(pbuf, sizeof(pbuf), bufpages * NBPG);
290 printf("using %d buffers containing %s of memory\n", nbuf, pbuf);
291
292 /*
293 * Set up buffers, so they can be used to read disk labels.
294 */
295 bufinit();
296
297 curpcb = &lwp0.l_addr->u_pcb;
298 curpcb->pcb_flags = 0;
299 curpcb->pcb_un.un_32.pcb32_und_sp = (u_int)lwp0.l_addr +
300 USPACE_UNDEF_STACK_TOP;
301 curpcb->pcb_un.un_32.pcb32_sp = (u_int)lwp0.l_addr +
302 USPACE_SVC_STACK_TOP;
303 (void) pmap_extract(pmap_kernel(), (vaddr_t)(pmap_kernel())->pm_pdir,
304 (paddr_t *)&curpcb->pcb_pagedir);
305
306 curpcb->pcb_tf = (struct trapframe *)curpcb->pcb_un.un_32.pcb32_sp - 1;
307 }
308
309 /*
310 * Modify the current mapping for zero page to make it read only
311 *
312 * This routine is only used until things start forking. Then new
313 * system pages are mapped read only in pmap_enter().
314 */
315
316 void
317 zero_page_readonly()
318 {
319 WriteWord(PROCESS_PAGE_TBLS_BASE + 0,
320 L2_PTE((systempage.pv_pa & PG_FRAME), AP_KR));
321 cpu_tlb_flushID_SE(0x00000000);
322 }
323
324
325 /*
326 * Modify the current mapping for zero page to make it read/write
327 *
328 * This routine is only used until things start forking. Then system
329 * pages belonging to user processes are never made writable.
330 */
331
332 void
333 zero_page_readwrite()
334 {
335 WriteWord(PROCESS_PAGE_TBLS_BASE + 0,
336 L2_PTE((systempage.pv_pa & PG_FRAME), AP_KRW));
337 cpu_tlb_flushID_SE(0x00000000);
338 }
339
340
341 /*
342 * machine dependent system variables.
343 */
344
345 int
346 cpu_sysctl(name, namelen, oldp, oldlenp, newp, newlen, p)
347 int *name;
348 u_int namelen;
349 void *oldp;
350 size_t *oldlenp;
351 void *newp;
352 size_t newlen;
353 struct proc *p;
354 {
355 /* all sysctl names at this level are terminal */
356 if (namelen != 1)
357 return (ENOTDIR); /* overloaded */
358
359 switch (name[0]) {
360 case CPU_DEBUG:
361 return(sysctl_int(oldp, oldlenp, newp, newlen, &kernel_debug));
362
363 case CPU_BOOTED_DEVICE:
364 if (booted_device != NULL)
365 return (sysctl_rdstring(oldp, oldlenp, newp,
366 booted_device->dv_xname));
367 return (EOPNOTSUPP);
368
369 case CPU_CONSDEV: {
370 dev_t consdev;
371 if (cn_tab != NULL)
372 consdev = cn_tab->cn_dev;
373 else
374 consdev = NODEV;
375 return (sysctl_rdstruct(oldp, oldlenp, newp, &consdev,
376 sizeof consdev));
377 }
378 case CPU_BOOTED_KERNEL: {
379 if (booted_kernel != NULL && booted_kernel[0] != '\0')
380 return sysctl_rdstring(oldp, oldlenp, newp,
381 booted_kernel);
382 return (EOPNOTSUPP);
383 }
384
385 default:
386 return (EOPNOTSUPP);
387 }
388 /* NOTREACHED */
389 }
390
391 void
392 parse_mi_bootargs(args)
393 char *args;
394 {
395 int integer;
396
397 if (get_bootconf_option(args, "single", BOOTOPT_TYPE_BOOLEAN, &integer)
398 || get_bootconf_option(args, "-s", BOOTOPT_TYPE_BOOLEAN, &integer))
399 if (integer)
400 boothowto |= RB_SINGLE;
401 if (get_bootconf_option(args, "kdb", BOOTOPT_TYPE_BOOLEAN, &integer)
402 || get_bootconf_option(args, "-k", BOOTOPT_TYPE_BOOLEAN, &integer))
403 if (integer)
404 boothowto |= RB_KDB;
405 if (get_bootconf_option(args, "ask", BOOTOPT_TYPE_BOOLEAN, &integer)
406 || get_bootconf_option(args, "-a", BOOTOPT_TYPE_BOOLEAN, &integer))
407 if (integer)
408 boothowto |= RB_ASKNAME;
409
410 #ifdef PMAP_DEBUG
411 if (get_bootconf_option(args, "pmapdebug", BOOTOPT_TYPE_INT, &integer)) {
412 pmap_debug_level = integer;
413 pmap_debug(pmap_debug_level);
414 }
415 #endif /* PMAP_DEBUG */
416
417 /* if (get_bootconf_option(args, "nbuf", BOOTOPT_TYPE_INT, &integer))
418 bufpages = integer;*/
419
420 #ifndef PMAP_STATIC_L1S
421 if (get_bootconf_option(args, "maxproc", BOOTOPT_TYPE_INT, &integer)) {
422 max_processes = integer;
423 if (max_processes < 16)
424 max_processes = 16;
425 /* Limit is PDSIZE * (max_processes + 1) <= 4MB */
426 if (max_processes > 255)
427 max_processes = 255;
428 }
429 #endif /* !PMAP_STATUC_L1S */
430 #if NMD > 0 && defined(MEMORY_DISK_HOOKS) && !defined(MINIROOTSIZE)
431 if (get_bootconf_option(args, "memorydisc", BOOTOPT_TYPE_INT, &integer)
432 || get_bootconf_option(args, "memorydisk", BOOTOPT_TYPE_INT, &integer)) {
433 memory_disc_size = integer;
434 memory_disc_size *= 1024;
435 if (memory_disc_size < 32*1024)
436 memory_disc_size = 32*1024;
437 if (memory_disc_size > 2048*1024)
438 memory_disc_size = 2048*1024;
439 }
440 #endif /* NMD && MEMORY_DISK_HOOKS && !MINIROOTSIZE */
441
442 if (get_bootconf_option(args, "quiet", BOOTOPT_TYPE_BOOLEAN, &integer)
443 || get_bootconf_option(args, "-q", BOOTOPT_TYPE_BOOLEAN, &integer))
444 if (integer)
445 boothowto |= AB_QUIET;
446 if (get_bootconf_option(args, "verbose", BOOTOPT_TYPE_BOOLEAN, &integer)
447 || get_bootconf_option(args, "-v", BOOTOPT_TYPE_BOOLEAN, &integer))
448 if (integer)
449 boothowto |= AB_VERBOSE;
450 }
451
452 /* End of machdep.c */
453