arm32_machdep.c revision 1.73.2.1 1 /* $NetBSD: arm32_machdep.c,v 1.73.2.1 2010/03/18 04:36:47 rmind 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 <sys/cdefs.h>
45 __KERNEL_RCSID(0, "$NetBSD: arm32_machdep.c,v 1.73.2.1 2010/03/18 04:36:47 rmind Exp $");
46
47 #include "opt_modular.h"
48 #include "opt_md.h"
49 #include "opt_pmap_debug.h"
50
51 #include <sys/param.h>
52 #include <sys/systm.h>
53 #include <sys/reboot.h>
54 #include <sys/proc.h>
55 #include <sys/kauth.h>
56 #include <sys/kernel.h>
57 #include <sys/mbuf.h>
58 #include <sys/mount.h>
59 #include <sys/buf.h>
60 #include <sys/msgbuf.h>
61 #include <sys/device.h>
62 #include <uvm/uvm_extern.h>
63 #include <sys/sysctl.h>
64 #include <sys/cpu.h>
65 #include <sys/module.h>
66
67 #include <dev/cons.h>
68 #include <dev/mm.h>
69
70 #include <arm/arm32/katelib.h>
71 #include <arm/arm32/machdep.h>
72 #include <machine/bootconfig.h>
73
74 #include "md.h"
75
76 struct vm_map *phys_map = NULL;
77
78 #if NMD > 0 && defined(MEMORY_DISK_HOOKS) && !defined(MEMORY_DISK_ROOT_SIZE)
79 extern size_t md_root_size; /* Memory disc size */
80 #endif /* NMD && MEMORY_DISK_HOOKS && !MEMORY_DISK_ROOT_SIZE */
81
82 pv_addr_t kernelstack;
83
84 void * msgbufaddr;
85 extern paddr_t msgbufphys;
86
87 int kernel_debug = 0;
88
89 /* exported variable to be filled in by the bootloaders */
90 char *booted_kernel;
91
92
93 /* Prototypes */
94
95 void data_abort_handler(trapframe_t *frame);
96 void prefetch_abort_handler(trapframe_t *frame);
97 extern void configure(void);
98
99 /*
100 * arm32_vector_init:
101 *
102 * Initialize the vector page, and select whether or not to
103 * relocate the vectors.
104 *
105 * NOTE: We expect the vector page to be mapped at its expected
106 * destination.
107 */
108 void
109 arm32_vector_init(vaddr_t va, int which)
110 {
111 extern unsigned int page0[], page0_data[];
112 unsigned int *vectors = (int *) va;
113 unsigned int *vectors_data = vectors + (page0_data - page0);
114 int vec;
115
116 /*
117 * Loop through the vectors we're taking over, and copy the
118 * vector's insn and data word.
119 */
120 for (vec = 0; vec < ARM_NVEC; vec++) {
121 if ((which & (1 << vec)) == 0) {
122 /* Don't want to take over this vector. */
123 continue;
124 }
125 vectors[vec] = page0[vec];
126 vectors_data[vec] = page0_data[vec];
127 }
128
129 /* Now sync the vectors. */
130 cpu_icache_sync_range(va, (ARM_NVEC * 2) * sizeof(u_int));
131
132 vector_page = va;
133
134 if (va == ARM_VECTORS_HIGH) {
135 /*
136 * Assume the MD caller knows what it's doing here, and
137 * really does want the vector page relocated.
138 *
139 * Note: This has to be done here (and not just in
140 * cpu_setup()) because the vector page needs to be
141 * accessible *before* cpu_startup() is called.
142 * Think ddb(9) ...
143 *
144 * NOTE: If the CPU control register is not readable,
145 * this will totally fail! We'll just assume that
146 * any system that has high vector support has a
147 * readable CPU control register, for now. If we
148 * ever encounter one that does not, we'll have to
149 * rethink this.
150 */
151 cpu_control(CPU_CONTROL_VECRELOC, CPU_CONTROL_VECRELOC);
152 }
153 }
154
155 /*
156 * Debug function just to park the CPU
157 */
158
159 void
160 halt(void)
161 {
162 while (1)
163 cpu_sleep(0);
164 }
165
166
167 /* Sync the discs and unmount the filesystems */
168
169 void
170 bootsync(void)
171 {
172 static bool bootsyncdone = false;
173
174 if (bootsyncdone) return;
175
176 bootsyncdone = true;
177
178 /* Make sure we can still manage to do things */
179 if (GetCPSR() & I32_bit) {
180 /*
181 * If we get here then boot has been called without RB_NOSYNC
182 * and interrupts were disabled. This means the boot() call
183 * did not come from a user process e.g. shutdown, but must
184 * have come from somewhere in the kernel.
185 */
186 IRQenable;
187 printf("Warning IRQ's disabled during boot()\n");
188 }
189
190 vfs_shutdown();
191 }
192
193 /*
194 * void cpu_startup(void)
195 *
196 * Machine dependant startup code.
197 *
198 */
199 void
200 cpu_startup(void)
201 {
202 vaddr_t minaddr;
203 vaddr_t maxaddr;
204 u_int loop;
205 char pbuf[9];
206
207 /* Set the CPU control register */
208 cpu_setup(boot_args);
209
210 /* Lock down zero page */
211 vector_page_setprot(VM_PROT_READ);
212
213 /*
214 * Give pmap a chance to set up a few more things now the vm
215 * is initialised
216 */
217 pmap_postinit();
218
219 /*
220 * Initialize error message buffer (at end of core).
221 */
222
223 /* msgbufphys was setup during the secondary boot strap */
224 for (loop = 0; loop < btoc(MSGBUFSIZE); ++loop)
225 pmap_kenter_pa((vaddr_t)msgbufaddr + loop * PAGE_SIZE,
226 msgbufphys + loop * PAGE_SIZE,
227 VM_PROT_READ|VM_PROT_WRITE, 0);
228 pmap_update(pmap_kernel());
229 initmsgbuf(msgbufaddr, round_page(MSGBUFSIZE));
230
231 /*
232 * Identify ourselves for the msgbuf (everything printed earlier will
233 * not be buffered).
234 */
235 printf("%s%s", copyright, version);
236
237 format_bytes(pbuf, sizeof(pbuf), arm_ptob(physmem));
238 printf("total memory = %s\n", pbuf);
239
240 minaddr = 0;
241
242 /*
243 * Allocate a submap for physio
244 */
245 phys_map = uvm_km_suballoc(kernel_map, &minaddr, &maxaddr,
246 VM_PHYS_SIZE, 0, false, NULL);
247
248 format_bytes(pbuf, sizeof(pbuf), ptoa(uvmexp.free));
249 printf("avail memory = %s\n", pbuf);
250
251 curpcb = lwp_getpcb(&lwp0);
252 curpcb->pcb_flags = 0;
253 curpcb->pcb_un.un_32.pcb32_sp =
254 uvm_lwp_getuarea(&lwp0) + USPACE_SVC_STACK_TOP;
255 curpcb->pcb_tf = (struct trapframe *)curpcb->pcb_un.un_32.pcb32_sp - 1;
256 }
257
258 /*
259 * machine dependent system variables.
260 */
261 static int
262 sysctl_machdep_booted_device(SYSCTLFN_ARGS)
263 {
264 struct sysctlnode node;
265
266 if (booted_device == NULL)
267 return (EOPNOTSUPP);
268
269 node = *rnode;
270 node.sysctl_data = booted_device->dv_xname;
271 node.sysctl_size = strlen(booted_device->dv_xname) + 1;
272 return (sysctl_lookup(SYSCTLFN_CALL(&node)));
273 }
274
275 static int
276 sysctl_machdep_booted_kernel(SYSCTLFN_ARGS)
277 {
278 struct sysctlnode node;
279
280 if (booted_kernel == NULL || booted_kernel[0] == '\0')
281 return (EOPNOTSUPP);
282
283 node = *rnode;
284 node.sysctl_data = booted_kernel;
285 node.sysctl_size = strlen(booted_kernel) + 1;
286 return (sysctl_lookup(SYSCTLFN_CALL(&node)));
287 }
288
289 static int
290 sysctl_machdep_powersave(SYSCTLFN_ARGS)
291 {
292 struct sysctlnode node = *rnode;
293 int error, newval;
294
295 newval = cpu_do_powersave;
296 node.sysctl_data = &newval;
297 if (cpufuncs.cf_sleep == (void *) cpufunc_nullop)
298 node.sysctl_flags &= ~CTLFLAG_READWRITE;
299 error = sysctl_lookup(SYSCTLFN_CALL(&node));
300 if (error || newp == NULL || newval == cpu_do_powersave)
301 return (error);
302
303 if (newval < 0 || newval > 1)
304 return (EINVAL);
305 cpu_do_powersave = newval;
306
307 return (0);
308 }
309
310 SYSCTL_SETUP(sysctl_machdep_setup, "sysctl machdep subtree setup")
311 {
312
313 sysctl_createv(clog, 0, NULL, NULL,
314 CTLFLAG_PERMANENT,
315 CTLTYPE_NODE, "machdep", NULL,
316 NULL, 0, NULL, 0,
317 CTL_MACHDEP, CTL_EOL);
318
319 sysctl_createv(clog, 0, NULL, NULL,
320 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
321 CTLTYPE_INT, "debug", NULL,
322 NULL, 0, &kernel_debug, 0,
323 CTL_MACHDEP, CPU_DEBUG, CTL_EOL);
324 sysctl_createv(clog, 0, NULL, NULL,
325 CTLFLAG_PERMANENT,
326 CTLTYPE_STRING, "booted_device", NULL,
327 sysctl_machdep_booted_device, 0, NULL, 0,
328 CTL_MACHDEP, CPU_BOOTED_DEVICE, CTL_EOL);
329 sysctl_createv(clog, 0, NULL, NULL,
330 CTLFLAG_PERMANENT,
331 CTLTYPE_STRING, "booted_kernel", NULL,
332 sysctl_machdep_booted_kernel, 0, NULL, 0,
333 CTL_MACHDEP, CPU_BOOTED_KERNEL, CTL_EOL);
334 sysctl_createv(clog, 0, NULL, NULL,
335 CTLFLAG_PERMANENT,
336 CTLTYPE_STRUCT, "console_device", NULL,
337 sysctl_consdev, 0, NULL, sizeof(dev_t),
338 CTL_MACHDEP, CPU_CONSDEV, CTL_EOL);
339 sysctl_createv(clog, 0, NULL, NULL,
340 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
341 CTLTYPE_INT, "powersave", NULL,
342 sysctl_machdep_powersave, 0, &cpu_do_powersave, 0,
343 CTL_MACHDEP, CPU_POWERSAVE, CTL_EOL);
344 }
345
346 void
347 parse_mi_bootargs(char *args)
348 {
349 int integer;
350
351 if (get_bootconf_option(args, "single", BOOTOPT_TYPE_BOOLEAN, &integer)
352 || get_bootconf_option(args, "-s", BOOTOPT_TYPE_BOOLEAN, &integer))
353 if (integer)
354 boothowto |= RB_SINGLE;
355 if (get_bootconf_option(args, "kdb", BOOTOPT_TYPE_BOOLEAN, &integer)
356 || get_bootconf_option(args, "-k", BOOTOPT_TYPE_BOOLEAN, &integer))
357 if (integer)
358 boothowto |= RB_KDB;
359 if (get_bootconf_option(args, "ask", BOOTOPT_TYPE_BOOLEAN, &integer)
360 || get_bootconf_option(args, "-a", BOOTOPT_TYPE_BOOLEAN, &integer))
361 if (integer)
362 boothowto |= RB_ASKNAME;
363
364 #ifdef PMAP_DEBUG
365 if (get_bootconf_option(args, "pmapdebug", BOOTOPT_TYPE_INT, &integer)) {
366 pmap_debug_level = integer;
367 pmap_debug(pmap_debug_level);
368 }
369 #endif /* PMAP_DEBUG */
370
371 /* if (get_bootconf_option(args, "nbuf", BOOTOPT_TYPE_INT, &integer))
372 bufpages = integer;*/
373
374 #if NMD > 0 && defined(MEMORY_DISK_HOOKS) && !defined(MEMORY_DISK_ROOT_SIZE)
375 if (get_bootconf_option(args, "memorydisc", BOOTOPT_TYPE_INT, &integer)
376 || get_bootconf_option(args, "memorydisk", BOOTOPT_TYPE_INT, &integer)) {
377 md_root_size = integer;
378 md_root_size *= 1024;
379 if (md_root_size < 32*1024)
380 md_root_size = 32*1024;
381 if (md_root_size > 2048*1024)
382 md_root_size = 2048*1024;
383 }
384 #endif /* NMD && MEMORY_DISK_HOOKS && !MEMORY_DISK_ROOT_SIZE */
385
386 if (get_bootconf_option(args, "quiet", BOOTOPT_TYPE_BOOLEAN, &integer)
387 || get_bootconf_option(args, "-q", BOOTOPT_TYPE_BOOLEAN, &integer))
388 if (integer)
389 boothowto |= AB_QUIET;
390 if (get_bootconf_option(args, "verbose", BOOTOPT_TYPE_BOOLEAN, &integer)
391 || get_bootconf_option(args, "-v", BOOTOPT_TYPE_BOOLEAN, &integer))
392 if (integer)
393 boothowto |= AB_VERBOSE;
394 }
395
396 #ifdef __HAVE_FAST_SOFTINTS
397 #if IPL_SOFTSERIAL != IPL_SOFTNET + 1
398 #error IPLs are screwed up
399 #elif IPL_SOFTNET != IPL_SOFTBIO + 1
400 #error IPLs are screwed up
401 #elif IPL_SOFTBIO != IPL_SOFTCLOCK + 1
402 #error IPLs are screwed up
403 #elif !(IPL_SOFTCLOCK > IPL_NONE)
404 #error IPLs are screwed up
405 #elif (IPL_NONE != 0)
406 #error IPLs are screwed up
407 #endif
408
409 #define SOFTINT2IPLMAP \
410 (((IPL_SOFTSERIAL - IPL_SOFTCLOCK) << (SOFTINT_SERIAL * 4)) | \
411 ((IPL_SOFTNET - IPL_SOFTCLOCK) << (SOFTINT_NET * 4)) | \
412 ((IPL_SOFTBIO - IPL_SOFTCLOCK) << (SOFTINT_BIO * 4)) | \
413 ((IPL_SOFTCLOCK - IPL_SOFTCLOCK) << (SOFTINT_CLOCK * 4)))
414 #define SOFTINT2IPL(l) ((SOFTINT2IPLMAP >> ((l) * 4)) & 0x0f)
415
416 /*
417 * This returns a mask of softint IPLs that be dispatch at <ipl>
418 * SOFTIPLMASK(IPL_NONE) = 0x0000000f
419 * SOFTIPLMASK(IPL_SOFTCLOCK) = 0x0000000e
420 * SOFTIPLMASK(IPL_SOFTBIO) = 0x0000000c
421 * SOFTIPLMASK(IPL_SOFTNET) = 0x00000008
422 * SOFTIPLMASK(IPL_SOFTSERIAL) = 0x00000000
423 */
424 #define SOFTIPLMASK(ipl) (0x0f << (ipl))
425
426 void softint_switch(lwp_t *, int);
427
428 void
429 softint_trigger(uintptr_t mask)
430 {
431 curcpu()->ci_softints |= mask;
432 }
433
434 void
435 softint_init_md(lwp_t *l, u_int level, uintptr_t *machdep)
436 {
437 lwp_t ** lp = &curcpu()->ci_softlwps[level];
438 KASSERT(*lp == NULL || *lp == l);
439 *lp = l;
440 *machdep = 1 << SOFTINT2IPL(level);
441 KASSERT(level != SOFTINT_CLOCK || *machdep == (1 << (IPL_SOFTCLOCK - IPL_SOFTCLOCK)));
442 KASSERT(level != SOFTINT_BIO || *machdep == (1 << (IPL_SOFTBIO - IPL_SOFTCLOCK)));
443 KASSERT(level != SOFTINT_NET || *machdep == (1 << (IPL_SOFTNET - IPL_SOFTCLOCK)));
444 KASSERT(level != SOFTINT_SERIAL || *machdep == (1 << (IPL_SOFTSERIAL - IPL_SOFTCLOCK)));
445 }
446
447 void
448 dosoftints(void)
449 {
450 struct cpu_info * const ci = curcpu();
451 const int opl = ci->ci_cpl;
452 const uint32_t softiplmask = SOFTIPLMASK(opl);
453
454 for (;;) {
455 u_int softints = ci->ci_softints & softiplmask;
456 KASSERT((softints != 0) == ((ci->ci_softints >> opl) != 0));
457 if (softints == 0)
458 return;
459 ci->ci_cpl = IPL_HIGH;
460 #define DOSOFTINT(n) \
461 if (softints & (1 << (IPL_SOFT ## n - IPL_SOFTCLOCK))) { \
462 ci->ci_softints &= \
463 ~(1 << (IPL_SOFT ## n - IPL_SOFTCLOCK)); \
464 softint_switch(ci->ci_softlwps[SOFTINT_ ## n], \
465 IPL_SOFT ## n); \
466 ci->ci_cpl = opl; \
467 continue; \
468 }
469 DOSOFTINT(SERIAL);
470 DOSOFTINT(NET);
471 DOSOFTINT(BIO);
472 DOSOFTINT(CLOCK);
473 panic("dosoftints wtf (softints=%u?, ipl=%d)", softints, opl);
474 }
475 }
476 #endif /* __HAVE_FAST_SOFTINTS */
477
478 #ifdef MODULAR
479 /*
480 * Push any modules loaded by the boot loader.
481 */
482 void
483 module_init_md(void)
484 {
485 }
486 #endif /* MODULAR */
487
488 int
489 mm_md_physacc(paddr_t pa, vm_prot_t prot)
490 {
491
492 if (pa < ctob(physmem))
493 return 0;
494
495 return kauth_authorize_machdep(kauth_cred_get(),
496 KAUTH_MACHDEP_UNMANAGEDMEM, NULL, NULL, NULL, NULL);
497 }
498