arm32_machdep.c revision 1.49.12.3 1 /* $NetBSD: arm32_machdep.c,v 1.49.12.3 2007/11/06 23:14:58 matt 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.49.12.3 2007/11/06 23:14:58 matt Exp $");
46
47 #include "opt_md.h"
48 #include "opt_cpuoptions.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/user.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
66 #include <dev/cons.h>
67
68 #include <arm/arm32/katelib.h>
69 #include <arm/arm32/machdep.h>
70 #include <machine/bootconfig.h>
71
72 #include "opt_ipkdb.h"
73 #include "md.h"
74
75 struct vm_map *exec_map = NULL;
76 struct vm_map *mb_map = NULL;
77 struct vm_map *phys_map = NULL;
78
79 extern int physmem;
80
81 #if NMD > 0 && defined(MEMORY_DISK_HOOKS) && !defined(MEMORY_DISK_ROOT_SIZE)
82 extern size_t md_root_size; /* Memory disc size */
83 #endif /* NMD && MEMORY_DISK_HOOKS && !MEMORY_DISK_ROOT_SIZE */
84
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 .ci_cpl = IPL_HIGH,
94 #ifndef PROCESS_ID_IS_CURLWP
95 .ci_curlwp = &lwp0,
96 #endif
97 };
98
99 void * msgbufaddr;
100 extern paddr_t msgbufphys;
101
102 int kernel_debug = 0;
103
104 struct user *proc0paddr;
105
106 /* exported variable to be filled in by the bootloaders */
107 char *booted_kernel;
108
109
110 /* Prototypes */
111
112 void data_abort_handler __P((trapframe_t *frame));
113 void prefetch_abort_handler __P((trapframe_t *frame));
114 extern void configure __P((void));
115
116 /*
117 * arm32_vector_init:
118 *
119 * Initialize the vector page, and select whether or not to
120 * relocate the vectors.
121 *
122 * NOTE: We expect the vector page to be mapped at its expected
123 * destination.
124 */
125 void
126 arm32_vector_init(vaddr_t va, int which)
127 {
128 extern unsigned int page0[], page0_data[];
129 unsigned int *vectors = (int *) va;
130 unsigned int *vectors_data = vectors + (page0_data - page0);
131 int vec;
132
133 /*
134 * Loop through the vectors we're taking over, and copy the
135 * vector's insn and data word.
136 */
137 for (vec = 0; vec < ARM_NVEC; vec++) {
138 if ((which & (1 << vec)) == 0) {
139 /* Don't want to take over this vector. */
140 continue;
141 }
142 vectors[vec] = page0[vec];
143 vectors_data[vec] = page0_data[vec];
144 }
145
146 /* Now sync the vectors. */
147 cpu_icache_sync_range(va, (ARM_NVEC * 2) * sizeof(u_int));
148
149 vector_page = va;
150
151 if (va == ARM_VECTORS_HIGH) {
152 /*
153 * Assume the MD caller knows what it's doing here, and
154 * really does want the vector page relocated.
155 *
156 * Note: This has to be done here (and not just in
157 * cpu_setup()) because the vector page needs to be
158 * accessible *before* cpu_startup() is called.
159 * Think ddb(9) ...
160 *
161 * NOTE: If the CPU control register is not readable,
162 * this will totally fail! We'll just assume that
163 * any system that has high vector support has a
164 * readable CPU control register, for now. If we
165 * ever encounter one that does not, we'll have to
166 * rethink this.
167 */
168 cpu_control(CPU_CONTROL_VECRELOC, CPU_CONTROL_VECRELOC);
169 }
170 }
171
172 /*
173 * Debug function just to park the CPU
174 */
175
176 void
177 halt()
178 {
179 while (1)
180 cpu_sleep(0);
181 }
182
183
184 /* Sync the discs and unmount the filesystems */
185
186 void
187 bootsync(void)
188 {
189 static int bootsyncdone = 0;
190
191 if (bootsyncdone) return;
192
193 bootsyncdone = 1;
194
195 /* Make sure we can still manage to do things */
196 if (GetCPSR() & I32_bit) {
197 /*
198 * If we get here then boot has been called without RB_NOSYNC
199 * and interrupts were disabled. This means the boot() call
200 * did not come from a user process e.g. shutdown, but must
201 * have come from somewhere in the kernel.
202 */
203 IRQenable;
204 printf("Warning IRQ's disabled during boot()\n");
205 }
206
207 vfs_shutdown();
208 }
209
210 /*
211 * void cpu_startup(void)
212 *
213 * Machine dependant startup code.
214 *
215 */
216 void
217 cpu_startup()
218 {
219 vaddr_t minaddr;
220 vaddr_t maxaddr;
221 u_int loop;
222 char pbuf[9];
223
224 /* Set the CPU control register */
225 cpu_setup(boot_args);
226
227 /* Lock down zero page */
228 vector_page_setprot(VM_PROT_READ);
229
230 /*
231 * Give pmap a chance to set up a few more things now the vm
232 * is initialised
233 */
234 pmap_postinit();
235
236 /*
237 * Initialize error message buffer (at end of core).
238 */
239
240 /* msgbufphys was setup during the secondary boot strap */
241 for (loop = 0; loop < btoc(MSGBUFSIZE); ++loop)
242 pmap_kenter_pa((vaddr_t)msgbufaddr + loop * PAGE_SIZE,
243 msgbufphys + loop * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE);
244 pmap_update(pmap_kernel());
245 initmsgbuf(msgbufaddr, round_page(MSGBUFSIZE));
246
247 /*
248 * Identify ourselves for the msgbuf (everything printed earlier will
249 * not be buffered).
250 */
251 printf("%s%s", copyright, version);
252
253 format_bytes(pbuf, sizeof(pbuf), arm_ptob(physmem));
254 printf("total memory = %s\n", pbuf);
255
256 minaddr = 0;
257
258 /*
259 * Allocate a submap for exec arguments. This map effectively
260 * limits the number of processes exec'ing at any time.
261 */
262 exec_map = uvm_km_suballoc(kernel_map, &minaddr, &maxaddr,
263 16*NCARGS, VM_MAP_PAGEABLE, false, NULL);
264
265 /*
266 * Allocate a submap for physio
267 */
268 phys_map = uvm_km_suballoc(kernel_map, &minaddr, &maxaddr,
269 VM_PHYS_SIZE, 0, false, NULL);
270
271 /*
272 * Finally, allocate mbuf cluster submap.
273 */
274 mb_map = uvm_km_suballoc(kernel_map, &minaddr, &maxaddr,
275 nmbclusters * mclbytes, VM_MAP_INTRSAFE,
276 false, NULL);
277
278 format_bytes(pbuf, sizeof(pbuf), ptoa(uvmexp.free));
279 printf("avail memory = %s\n", pbuf);
280
281 curpcb = &lwp0.l_addr->u_pcb;
282 curpcb->pcb_flags = 0;
283 curpcb->pcb_un.un_32.pcb32_und_sp = (u_int)lwp0.l_addr +
284 USPACE_UNDEF_STACK_TOP;
285 curpcb->pcb_un.un_32.pcb32_sp = (u_int)lwp0.l_addr +
286 USPACE_SVC_STACK_TOP;
287
288 curpcb->pcb_tf = (struct trapframe *)curpcb->pcb_un.un_32.pcb32_sp - 1;
289 }
290
291 /*
292 * machine dependent system variables.
293 */
294 static int
295 sysctl_machdep_booted_device(SYSCTLFN_ARGS)
296 {
297 struct sysctlnode node;
298
299 if (booted_device == NULL)
300 return (EOPNOTSUPP);
301
302 node = *rnode;
303 node.sysctl_data = booted_device->dv_xname;
304 node.sysctl_size = strlen(booted_device->dv_xname) + 1;
305 return (sysctl_lookup(SYSCTLFN_CALL(&node)));
306 }
307
308 static int
309 sysctl_machdep_booted_kernel(SYSCTLFN_ARGS)
310 {
311 struct sysctlnode node;
312
313 if (booted_kernel == NULL || booted_kernel[0] == '\0')
314 return (EOPNOTSUPP);
315
316 node = *rnode;
317 node.sysctl_data = booted_kernel;
318 node.sysctl_size = strlen(booted_kernel) + 1;
319 return (sysctl_lookup(SYSCTLFN_CALL(&node)));
320 }
321
322 static int
323 sysctl_machdep_powersave(SYSCTLFN_ARGS)
324 {
325 struct sysctlnode node = *rnode;
326 int error, newval;
327
328 newval = cpu_do_powersave;
329 node.sysctl_data = &newval;
330 if (cpufuncs.cf_sleep == (void *) cpufunc_nullop)
331 node.sysctl_flags &= ~CTLFLAG_READWRITE;
332 error = sysctl_lookup(SYSCTLFN_CALL(&node));
333 if (error || newp == NULL || newval == cpu_do_powersave)
334 return (error);
335
336 if (newval < 0 || newval > 1)
337 return (EINVAL);
338 cpu_do_powersave = newval;
339
340 return (0);
341 }
342
343 SYSCTL_SETUP(sysctl_machdep_setup, "sysctl machdep subtree setup")
344 {
345
346 sysctl_createv(clog, 0, NULL, NULL,
347 CTLFLAG_PERMANENT,
348 CTLTYPE_NODE, "machdep", NULL,
349 NULL, 0, NULL, 0,
350 CTL_MACHDEP, CTL_EOL);
351
352 sysctl_createv(clog, 0, NULL, NULL,
353 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
354 CTLTYPE_INT, "debug", NULL,
355 NULL, 0, &kernel_debug, 0,
356 CTL_MACHDEP, CPU_DEBUG, CTL_EOL);
357 sysctl_createv(clog, 0, NULL, NULL,
358 CTLFLAG_PERMANENT,
359 CTLTYPE_STRING, "booted_device", NULL,
360 sysctl_machdep_booted_device, 0, NULL, 0,
361 CTL_MACHDEP, CPU_BOOTED_DEVICE, CTL_EOL);
362 sysctl_createv(clog, 0, NULL, NULL,
363 CTLFLAG_PERMANENT,
364 CTLTYPE_STRING, "booted_kernel", NULL,
365 sysctl_machdep_booted_kernel, 0, NULL, 0,
366 CTL_MACHDEP, CPU_BOOTED_KERNEL, CTL_EOL);
367 sysctl_createv(clog, 0, NULL, NULL,
368 CTLFLAG_PERMANENT,
369 CTLTYPE_STRUCT, "console_device", NULL,
370 sysctl_consdev, 0, NULL, sizeof(dev_t),
371 CTL_MACHDEP, CPU_CONSDEV, CTL_EOL);
372 sysctl_createv(clog, 0, NULL, NULL,
373 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
374 CTLTYPE_INT, "powersave", NULL,
375 sysctl_machdep_powersave, 0, &cpu_do_powersave, 0,
376 CTL_MACHDEP, CPU_POWERSAVE, CTL_EOL);
377 }
378
379 void
380 parse_mi_bootargs(args)
381 char *args;
382 {
383 int integer;
384
385 if (get_bootconf_option(args, "single", BOOTOPT_TYPE_BOOLEAN, &integer)
386 || get_bootconf_option(args, "-s", BOOTOPT_TYPE_BOOLEAN, &integer))
387 if (integer)
388 boothowto |= RB_SINGLE;
389 if (get_bootconf_option(args, "kdb", BOOTOPT_TYPE_BOOLEAN, &integer)
390 || get_bootconf_option(args, "-k", BOOTOPT_TYPE_BOOLEAN, &integer))
391 if (integer)
392 boothowto |= RB_KDB;
393 if (get_bootconf_option(args, "ask", BOOTOPT_TYPE_BOOLEAN, &integer)
394 || get_bootconf_option(args, "-a", BOOTOPT_TYPE_BOOLEAN, &integer))
395 if (integer)
396 boothowto |= RB_ASKNAME;
397
398 #ifdef PMAP_DEBUG
399 if (get_bootconf_option(args, "pmapdebug", BOOTOPT_TYPE_INT, &integer)) {
400 pmap_debug_level = integer;
401 pmap_debug(pmap_debug_level);
402 }
403 #endif /* PMAP_DEBUG */
404
405 /* if (get_bootconf_option(args, "nbuf", BOOTOPT_TYPE_INT, &integer))
406 bufpages = integer;*/
407
408 #if NMD > 0 && defined(MEMORY_DISK_HOOKS) && !defined(MEMORY_DISK_ROOT_SIZE)
409 if (get_bootconf_option(args, "memorydisc", BOOTOPT_TYPE_INT, &integer)
410 || get_bootconf_option(args, "memorydisk", BOOTOPT_TYPE_INT, &integer)) {
411 md_root_size = integer;
412 md_root_size *= 1024;
413 if (md_root_size < 32*1024)
414 md_root_size = 32*1024;
415 if (md_root_size > 2048*1024)
416 md_root_size = 2048*1024;
417 }
418 #endif /* NMD && MEMORY_DISK_HOOKS && !MEMORY_DISK_ROOT_SIZE */
419
420 if (get_bootconf_option(args, "quiet", BOOTOPT_TYPE_BOOLEAN, &integer)
421 || get_bootconf_option(args, "-q", BOOTOPT_TYPE_BOOLEAN, &integer))
422 if (integer)
423 boothowto |= AB_QUIET;
424 if (get_bootconf_option(args, "verbose", BOOTOPT_TYPE_BOOLEAN, &integer)
425 || get_bootconf_option(args, "-v", BOOTOPT_TYPE_BOOLEAN, &integer))
426 if (integer)
427 boothowto |= AB_VERBOSE;
428 }
429
430 void
431 cpu_need_resched(struct cpu_info *ci, int flags)
432 {
433 bool immed = (flags & RESCHED_IMMED) != 0;
434
435 if (ci->ci_want_resched && !immed)
436 return;
437
438 ci->ci_want_resched = 1;
439 if (curlwp != ci->ci_data.cpu_idlelwp)
440 setsoftast();
441 }
442