machdep.c revision 1.27 1 1.27 thorpej /* $NetBSD: machdep.c,v 1.27 1998/09/13 09:15:52 thorpej Exp $ */
2 1.1 ws
3 1.1 ws /*
4 1.1 ws * Copyright (C) 1995, 1996 Wolfgang Solfrank.
5 1.1 ws * Copyright (C) 1995, 1996 TooLs GmbH.
6 1.1 ws * All rights reserved.
7 1.1 ws *
8 1.1 ws * Redistribution and use in source and binary forms, with or without
9 1.1 ws * modification, are permitted provided that the following conditions
10 1.1 ws * are met:
11 1.1 ws * 1. Redistributions of source code must retain the above copyright
12 1.1 ws * notice, this list of conditions and the following disclaimer.
13 1.1 ws * 2. Redistributions in binary form must reproduce the above copyright
14 1.1 ws * notice, this list of conditions and the following disclaimer in the
15 1.1 ws * documentation and/or other materials provided with the distribution.
16 1.1 ws * 3. All advertising materials mentioning features or use of this software
17 1.1 ws * must display the following acknowledgement:
18 1.1 ws * This product includes software developed by TooLs GmbH.
19 1.1 ws * 4. The name of TooLs GmbH may not be used to endorse or promote products
20 1.1 ws * derived from this software without specific prior written permission.
21 1.1 ws *
22 1.1 ws * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
23 1.1 ws * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 1.1 ws * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 1.1 ws * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 1.1 ws * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
27 1.1 ws * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
28 1.1 ws * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29 1.1 ws * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
30 1.1 ws * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
31 1.1 ws * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 1.1 ws */
33 1.21 jonathan
34 1.27 thorpej #include "opt_compat_netbsd.h"
35 1.21 jonathan #include "opt_ddb.h"
36 1.22 jonathan #include "opt_inet.h"
37 1.23 jonathan #include "opt_ccitt.h"
38 1.24 jonathan #include "opt_iso.h"
39 1.25 jonathan #include "opt_ns.h"
40 1.19 sakamoto #include "opt_uvm.h"
41 1.4 ws #include "ipkdb.h"
42 1.1 ws
43 1.1 ws #include <sys/param.h>
44 1.1 ws #include <sys/buf.h>
45 1.1 ws #include <sys/callout.h>
46 1.1 ws #include <sys/exec.h>
47 1.1 ws #include <sys/malloc.h>
48 1.1 ws #include <sys/map.h>
49 1.1 ws #include <sys/mbuf.h>
50 1.1 ws #include <sys/mount.h>
51 1.1 ws #include <sys/msgbuf.h>
52 1.1 ws #include <sys/proc.h>
53 1.1 ws #include <sys/reboot.h>
54 1.1 ws #include <sys/syscallargs.h>
55 1.1 ws #include <sys/syslog.h>
56 1.1 ws #include <sys/systm.h>
57 1.1 ws #include <sys/user.h>
58 1.1 ws
59 1.1 ws #include <vm/vm.h>
60 1.1 ws #include <vm/vm_kern.h>
61 1.1 ws
62 1.19 sakamoto #if defined(UVM)
63 1.19 sakamoto #include <uvm/uvm_extern.h>
64 1.19 sakamoto #endif
65 1.19 sakamoto
66 1.1 ws #include <net/netisr.h>
67 1.1 ws
68 1.1 ws #include <machine/bat.h>
69 1.1 ws #include <machine/pmap.h>
70 1.1 ws #include <machine/powerpc.h>
71 1.1 ws #include <machine/trap.h>
72 1.1 ws
73 1.1 ws /*
74 1.1 ws * Global variables used here and there
75 1.1 ws */
76 1.19 sakamoto #if defined(UVM)
77 1.19 sakamoto vm_map_t exec_map = NULL;
78 1.19 sakamoto vm_map_t mb_map = NULL;
79 1.19 sakamoto vm_map_t phys_map = NULL;
80 1.19 sakamoto #endif
81 1.19 sakamoto
82 1.1 ws struct pcb *curpcb;
83 1.1 ws struct pmap *curpm;
84 1.1 ws struct proc *fpuproc;
85 1.1 ws
86 1.1 ws extern struct user *proc0paddr;
87 1.1 ws
88 1.1 ws struct bat battable[16];
89 1.1 ws
90 1.1 ws int astpending;
91 1.1 ws
92 1.1 ws char *bootpath;
93 1.1 ws
94 1.18 sakamoto #define MSGBUFADDR 0x3000
95 1.18 sakamoto
96 1.1 ws caddr_t allocsys __P((caddr_t));
97 1.1 ws
98 1.7 thorpej static int fake_spl __P((void));
99 1.7 thorpej static int fake_splx __P((int));
100 1.7 thorpej static void fake_setsoft __P((void));
101 1.7 thorpej static void fake_clock_return __P((struct clockframe *, int));
102 1.1 ws static void fake_irq_establish __P((int, int, void (*)(void *), void *));
103 1.1 ws
104 1.1 ws struct machvec machine_interface = {
105 1.7 thorpej fake_spl,
106 1.7 thorpej fake_spl,
107 1.7 thorpej fake_spl,
108 1.7 thorpej fake_spl,
109 1.7 thorpej fake_spl,
110 1.7 thorpej fake_spl,
111 1.7 thorpej fake_spl,
112 1.7 thorpej fake_spl,
113 1.7 thorpej fake_spl,
114 1.1 ws fake_splx,
115 1.7 thorpej fake_setsoft,
116 1.7 thorpej fake_setsoft,
117 1.7 thorpej fake_clock_return,
118 1.1 ws fake_irq_establish,
119 1.1 ws };
120 1.1 ws
121 1.1 ws int cold = 1;
122 1.1 ws
123 1.1 ws void
124 1.1 ws initppc(startkernel, endkernel, args)
125 1.1 ws u_int startkernel, endkernel;
126 1.1 ws char *args;
127 1.1 ws {
128 1.1 ws int phandle, qhandle;
129 1.1 ws char name[32];
130 1.1 ws struct machvec *mp;
131 1.1 ws extern trapcode, trapsize;
132 1.1 ws extern dsitrap, dsisize;
133 1.1 ws extern isitrap, isisize;
134 1.1 ws extern decrint, decrsize;
135 1.1 ws extern tlbimiss, tlbimsize;
136 1.1 ws extern tlbdlmiss, tlbdlmsize;
137 1.1 ws extern tlbdsmiss, tlbdsmsize;
138 1.14 sakamoto #ifdef DDB
139 1.14 sakamoto extern ddblow, ddbsize;
140 1.14 sakamoto extern void *startsym, *endsym;
141 1.14 sakamoto #endif
142 1.4 ws #if NIPKDB > 0
143 1.4 ws extern ipkdblow, ipkdbsize;
144 1.1 ws #endif
145 1.1 ws extern void consinit __P((void));
146 1.1 ws extern void callback __P((void *));
147 1.1 ws int exc, scratch;
148 1.1 ws
149 1.1 ws proc0.p_addr = proc0paddr;
150 1.1 ws bzero(proc0.p_addr, sizeof *proc0.p_addr);
151 1.1 ws
152 1.1 ws curpcb = &proc0paddr->u_pcb;
153 1.1 ws
154 1.1 ws curpm = curpcb->pcb_pmreal = curpcb->pcb_pm = pmap_kernel();
155 1.1 ws
156 1.1 ws /*
157 1.1 ws * i386 port says, that this shouldn't be here,
158 1.1 ws * but I really think the console should be initialized
159 1.1 ws * as early as possible.
160 1.1 ws */
161 1.1 ws consinit();
162 1.1 ws
163 1.1 ws #ifdef __notyet__ /* Needs some rethinking regarding real/virtual OFW */
164 1.1 ws OF_set_callback(callback);
165 1.1 ws #endif
166 1.1 ws /*
167 1.1 ws * Initialize BAT registers to unmapped to not generate
168 1.1 ws * overlapping mappings below.
169 1.1 ws */
170 1.1 ws asm volatile ("mtibatu 0,%0" :: "r"(0));
171 1.1 ws asm volatile ("mtibatu 1,%0" :: "r"(0));
172 1.1 ws asm volatile ("mtibatu 2,%0" :: "r"(0));
173 1.1 ws asm volatile ("mtibatu 3,%0" :: "r"(0));
174 1.1 ws asm volatile ("mtdbatu 0,%0" :: "r"(0));
175 1.1 ws asm volatile ("mtdbatu 1,%0" :: "r"(0));
176 1.1 ws asm volatile ("mtdbatu 2,%0" :: "r"(0));
177 1.1 ws asm volatile ("mtdbatu 3,%0" :: "r"(0));
178 1.1 ws
179 1.1 ws /*
180 1.1 ws * Set up initial BAT table to only map the lowest 256 MB area
181 1.1 ws */
182 1.1 ws battable[0].batl = BATL(0x00000000, BAT_M);
183 1.1 ws battable[0].batu = BATU(0x00000000);
184 1.1 ws
185 1.1 ws /*
186 1.1 ws * Now setup fixed bat registers
187 1.1 ws *
188 1.1 ws * Note that we still run in real mode, and the BAT
189 1.1 ws * registers were cleared above.
190 1.1 ws */
191 1.1 ws /* IBAT0 used for initial 256 MB segment */
192 1.1 ws asm volatile ("mtibatl 0,%0; mtibatu 0,%1"
193 1.1 ws :: "r"(battable[0].batl), "r"(battable[0].batu));
194 1.1 ws /* DBAT0 used similar */
195 1.1 ws asm volatile ("mtdbatl 0,%0; mtdbatu 0,%1"
196 1.1 ws :: "r"(battable[0].batl), "r"(battable[0].batu));
197 1.1 ws
198 1.1 ws /*
199 1.1 ws * Set up trap vectors
200 1.1 ws */
201 1.1 ws for (exc = EXC_RSVD; exc <= EXC_LAST; exc += 0x100)
202 1.1 ws switch (exc) {
203 1.1 ws default:
204 1.1 ws bcopy(&trapcode, (void *)exc, (size_t)&trapsize);
205 1.1 ws break;
206 1.1 ws case EXC_EXI:
207 1.1 ws /*
208 1.1 ws * This one is (potentially) installed during autoconf
209 1.1 ws */
210 1.1 ws break;
211 1.1 ws case EXC_DSI:
212 1.1 ws bcopy(&dsitrap, (void *)EXC_DSI, (size_t)&dsisize);
213 1.1 ws break;
214 1.1 ws case EXC_ISI:
215 1.1 ws bcopy(&isitrap, (void *)EXC_ISI, (size_t)&isisize);
216 1.1 ws break;
217 1.1 ws case EXC_DECR:
218 1.1 ws bcopy(&decrint, (void *)EXC_DECR, (size_t)&decrsize);
219 1.1 ws break;
220 1.1 ws case EXC_IMISS:
221 1.1 ws bcopy(&tlbimiss, (void *)EXC_IMISS, (size_t)&tlbimsize);
222 1.1 ws break;
223 1.1 ws case EXC_DLMISS:
224 1.1 ws bcopy(&tlbdlmiss, (void *)EXC_DLMISS, (size_t)&tlbdlmsize);
225 1.1 ws break;
226 1.1 ws case EXC_DSMISS:
227 1.1 ws bcopy(&tlbdsmiss, (void *)EXC_DSMISS, (size_t)&tlbdsmsize);
228 1.1 ws break;
229 1.15 thorpej #if defined(DDB) || NIPKDB > 0
230 1.14 sakamoto case EXC_PGM:
231 1.14 sakamoto case EXC_TRC:
232 1.14 sakamoto case EXC_BPT:
233 1.15 thorpej #if defined(DDB)
234 1.14 sakamoto bcopy(&ddblow, (void *)exc, (size_t)&ddbsize);
235 1.15 thorpej #else
236 1.15 thorpej bcopy(&ipkdblow, (void *)exc, (size_t)&ipkdbsize);
237 1.14 sakamoto #endif
238 1.1 ws break;
239 1.15 thorpej #endif /* DDB || NIPKDB > 0 */
240 1.1 ws }
241 1.1 ws
242 1.1 ws syncicache((void *)EXC_RST, EXC_LAST - EXC_RST + 0x100);
243 1.1 ws
244 1.1 ws /*
245 1.1 ws * Now enable translation (and machine checks/recoverable interrupts).
246 1.1 ws */
247 1.1 ws asm volatile ("mfmsr %0; ori %0,%0,%1; mtmsr %0; isync"
248 1.1 ws : "=r"(scratch) : "K"(PSL_IR|PSL_DR|PSL_ME|PSL_RI));
249 1.1 ws
250 1.1 ws /*
251 1.1 ws * Parse arg string.
252 1.1 ws */
253 1.1 ws bootpath = args;
254 1.7 thorpej while (*++args && *args != ' ');
255 1.1 ws if (*args) {
256 1.1 ws *args++ = 0;
257 1.1 ws while (*args) {
258 1.1 ws switch (*args++) {
259 1.1 ws case 'a':
260 1.1 ws boothowto |= RB_ASKNAME;
261 1.1 ws break;
262 1.1 ws case 's':
263 1.1 ws boothowto |= RB_SINGLE;
264 1.1 ws break;
265 1.1 ws case 'd':
266 1.1 ws boothowto |= RB_KDB;
267 1.1 ws break;
268 1.1 ws }
269 1.1 ws }
270 1.1 ws }
271 1.1 ws
272 1.14 sakamoto #ifdef DDB
273 1.14 sakamoto /* ddb_init(startsym, endsym); */
274 1.14 sakamoto #endif
275 1.4 ws #if NIPKDB > 0
276 1.1 ws /*
277 1.4 ws * Now trap to IPKDB
278 1.1 ws */
279 1.4 ws ipkdb_init();
280 1.1 ws if (boothowto & RB_KDB)
281 1.4 ws ipkdb_connect(0);
282 1.1 ws #endif
283 1.16 thorpej
284 1.16 thorpej /*
285 1.16 thorpej * Set the page size.
286 1.16 thorpej */
287 1.19 sakamoto #if defined(UVM)
288 1.19 sakamoto uvm_setpagesize();
289 1.19 sakamoto #else
290 1.16 thorpej vm_set_page_size();
291 1.19 sakamoto #endif
292 1.1 ws
293 1.1 ws /*
294 1.1 ws * Initialize pmap module.
295 1.1 ws */
296 1.1 ws pmap_bootstrap(startkernel, endkernel);
297 1.1 ws }
298 1.1 ws
299 1.1 ws /*
300 1.1 ws * This should probably be in autoconf! XXX
301 1.1 ws */
302 1.1 ws int cpu;
303 1.1 ws char cpu_model[80];
304 1.9 veego char machine[] = MACHINE; /* from <machine/param.h> */
305 1.9 veego char machine_arch[] = MACHINE_ARCH; /* from <machine/param.h> */
306 1.1 ws
307 1.1 ws void
308 1.1 ws identifycpu()
309 1.1 ws {
310 1.1 ws int phandle, pvr;
311 1.1 ws char name[32];
312 1.1 ws
313 1.1 ws /*
314 1.1 ws * Find cpu type (Do it by OpenFirmware?)
315 1.1 ws */
316 1.1 ws asm ("mfpvr %0" : "=r"(pvr));
317 1.1 ws cpu = pvr >> 16;
318 1.1 ws switch (cpu) {
319 1.1 ws case 1:
320 1.3 christos sprintf(cpu_model, "601");
321 1.1 ws break;
322 1.1 ws case 3:
323 1.3 christos sprintf(cpu_model, "603");
324 1.1 ws break;
325 1.1 ws case 4:
326 1.3 christos sprintf(cpu_model, "604");
327 1.1 ws break;
328 1.1 ws case 5:
329 1.3 christos sprintf(cpu_model, "602");
330 1.1 ws break;
331 1.1 ws case 6:
332 1.3 christos sprintf(cpu_model, "603e");
333 1.1 ws break;
334 1.1 ws case 7:
335 1.3 christos sprintf(cpu_model, "603ev");
336 1.1 ws break;
337 1.1 ws case 9:
338 1.3 christos sprintf(cpu_model, "604ev");
339 1.1 ws break;
340 1.1 ws case 20:
341 1.3 christos sprintf(cpu_model, "620");
342 1.1 ws break;
343 1.1 ws default:
344 1.3 christos sprintf(cpu_model, "Version %x", cpu);
345 1.1 ws break;
346 1.1 ws }
347 1.1 ws sprintf(cpu_model + strlen(cpu_model), " (Revision %x)", pvr & 0xffff);
348 1.3 christos printf("CPU: %s\n", cpu_model);
349 1.1 ws }
350 1.1 ws
351 1.1 ws void
352 1.1 ws install_extint(handler)
353 1.1 ws void (*handler) __P((void));
354 1.1 ws {
355 1.1 ws extern extint, extsize;
356 1.1 ws extern u_long extint_call;
357 1.1 ws u_long offset = (u_long)handler - (u_long)&extint_call;
358 1.1 ws int omsr, msr;
359 1.1 ws
360 1.1 ws #ifdef DIAGNOSTIC
361 1.1 ws if (offset > 0x1ffffff)
362 1.1 ws panic("install_extint: too far away");
363 1.1 ws #endif
364 1.7 thorpej asm volatile ("mfmsr %0; andi. %1,%0,%2; mtmsr %1"
365 1.1 ws : "=r"(omsr), "=r"(msr) : "K"((u_short)~PSL_EE));
366 1.1 ws extint_call = (extint_call & 0xfc000003) | offset;
367 1.1 ws bcopy(&extint, (void *)EXC_EXI, (size_t)&extsize);
368 1.1 ws syncicache((void *)&extint_call, sizeof extint_call);
369 1.1 ws syncicache((void *)EXC_EXI, (int)&extsize);
370 1.1 ws asm volatile ("mtmsr %0" :: "r"(omsr));
371 1.1 ws }
372 1.1 ws
373 1.1 ws /*
374 1.1 ws * Machine dependent startup code.
375 1.1 ws */
376 1.1 ws void
377 1.1 ws cpu_startup()
378 1.1 ws {
379 1.1 ws int sz, i;
380 1.1 ws caddr_t v;
381 1.26 sakamoto paddr_t minaddr, maxaddr;
382 1.1 ws int base, residual;
383 1.18 sakamoto
384 1.18 sakamoto /*
385 1.18 sakamoto * Initialize error message buffer (at end of core).
386 1.18 sakamoto */
387 1.18 sakamoto initmsgbuf((caddr_t)MSGBUFADDR, round_page(MSGBUFSIZE));
388 1.18 sakamoto
389 1.1 ws proc0.p_addr = proc0paddr;
390 1.1 ws v = (caddr_t)proc0paddr + USPACE;
391 1.1 ws
392 1.3 christos printf("%s", version);
393 1.1 ws identifycpu();
394 1.1 ws
395 1.3 christos printf("real mem = %d\n", ctob(physmem));
396 1.19 sakamoto
397 1.1 ws /*
398 1.1 ws * Find out how much space we need, allocate it,
399 1.1 ws * and then give everything true virtual addresses.
400 1.1 ws */
401 1.1 ws sz = (int)allocsys((caddr_t)0);
402 1.19 sakamoto #if defined(UVM)
403 1.19 sakamoto if ((v = (caddr_t)uvm_km_zalloc(kernel_map, round_page(sz))) == 0)
404 1.19 sakamoto panic("startup: no room for tables");
405 1.19 sakamoto #else
406 1.1 ws if ((v = (caddr_t)kmem_alloc(kernel_map, round_page(sz))) == 0)
407 1.1 ws panic("startup: no room for tables");
408 1.19 sakamoto #endif
409 1.1 ws if (allocsys(v) - v != sz)
410 1.1 ws panic("startup: table size inconsistency");
411 1.19 sakamoto
412 1.1 ws /*
413 1.1 ws * Now allocate buffers proper. They are different than the above
414 1.1 ws * in that they usually occupy more virtual memory than physical.
415 1.1 ws */
416 1.1 ws sz = MAXBSIZE * nbuf;
417 1.19 sakamoto #if defined(UVM)
418 1.26 sakamoto if (uvm_map(kernel_map, (vaddr_t *)&buffers, round_page(sz),
419 1.19 sakamoto NULL, UVM_UNKNOWN_OFFSET,
420 1.19 sakamoto UVM_MAPFLAG(UVM_PROT_NONE, UVM_PROT_NONE, UVM_INH_NONE,
421 1.19 sakamoto UVM_ADV_NORMAL, 0)) != KERN_SUCCESS)
422 1.19 sakamoto panic("startup: cannot allocate VM for buffers");
423 1.19 sakamoto #else
424 1.1 ws buffer_map = kmem_suballoc(kernel_map, &minaddr, &maxaddr, sz, TRUE);
425 1.1 ws buffers = (char *)minaddr;
426 1.26 sakamoto if (vm_map_find(buffer_map, vm_object_allocate(sz), (vaddr_t)0,
427 1.1 ws &minaddr, sz, FALSE) != KERN_SUCCESS)
428 1.1 ws panic("startup: cannot allocate buffers");
429 1.19 sakamoto #endif
430 1.1 ws base = bufpages / nbuf;
431 1.1 ws residual = bufpages % nbuf;
432 1.1 ws if (base >= MAXBSIZE) {
433 1.1 ws /* Don't want to alloc more physical mem than ever needed */
434 1.1 ws base = MAXBSIZE;
435 1.1 ws residual = 0;
436 1.1 ws }
437 1.1 ws for (i = 0; i < nbuf; i++) {
438 1.19 sakamoto #if defined(UVM)
439 1.26 sakamoto vsize_t curbufsize;
440 1.26 sakamoto vaddr_t curbuf;
441 1.19 sakamoto struct vm_page *pg;
442 1.19 sakamoto
443 1.19 sakamoto /*
444 1.19 sakamoto * Each buffer has MAXBSIZE bytes of VM space allocated. Of
445 1.19 sakamoto * that MAXBSIZE space, we allocate and map (base+1) pages
446 1.19 sakamoto * for the first "residual" buffers, and then we allocate
447 1.19 sakamoto * "base" pages for the rest.
448 1.19 sakamoto */
449 1.26 sakamoto curbuf = (vaddr_t) buffers + (i * MAXBSIZE);
450 1.19 sakamoto curbufsize = CLBYTES * ((i < residual) ? (base+1) : base);
451 1.19 sakamoto
452 1.19 sakamoto while (curbufsize) {
453 1.19 sakamoto pg = uvm_pagealloc(NULL, 0, NULL);
454 1.19 sakamoto if (pg == NULL)
455 1.19 sakamoto panic("startup: not enough memory for "
456 1.19 sakamoto "buffer cache");
457 1.19 sakamoto pmap_enter(kernel_map->pmap, curbuf,
458 1.19 sakamoto VM_PAGE_TO_PHYS(pg), VM_PROT_ALL, TRUE);
459 1.19 sakamoto curbuf += PAGE_SIZE;
460 1.19 sakamoto curbufsize -= PAGE_SIZE;
461 1.19 sakamoto }
462 1.19 sakamoto #else
463 1.26 sakamoto vsize_t curbufsize;
464 1.26 sakamoto vaddr_t curbuf;
465 1.1 ws
466 1.26 sakamoto curbuf = (vaddr_t)buffers + i * MAXBSIZE;
467 1.1 ws curbufsize = CLBYTES * (i < residual ? base + 1 : base);
468 1.1 ws vm_map_pageable(buffer_map, curbuf, curbuf + curbufsize, FALSE);
469 1.1 ws vm_map_simplify(buffer_map, curbuf);
470 1.19 sakamoto #endif
471 1.1 ws }
472 1.1 ws
473 1.1 ws /*
474 1.1 ws * Allocate a submap for exec arguments. This map effectively
475 1.1 ws * limits the number of processes exec'ing at any time.
476 1.1 ws */
477 1.19 sakamoto #if defined(UVM)
478 1.19 sakamoto exec_map = uvm_km_suballoc(kernel_map, &minaddr, &maxaddr,
479 1.19 sakamoto 16*NCARGS, TRUE, FALSE, NULL);
480 1.19 sakamoto #else
481 1.1 ws exec_map = kmem_suballoc(kernel_map, &minaddr, &maxaddr,
482 1.1 ws 16*NCARGS, TRUE);
483 1.19 sakamoto #endif
484 1.1 ws
485 1.1 ws /*
486 1.1 ws * Allocate a submap for physio
487 1.1 ws */
488 1.19 sakamoto #if defined(UVM)
489 1.19 sakamoto phys_map = uvm_km_suballoc(kernel_map, &minaddr, &maxaddr,
490 1.19 sakamoto VM_PHYS_SIZE, TRUE, FALSE, NULL);
491 1.19 sakamoto #else
492 1.1 ws phys_map = kmem_suballoc(kernel_map, &minaddr, &maxaddr,
493 1.1 ws VM_PHYS_SIZE, TRUE);
494 1.19 sakamoto #endif
495 1.19 sakamoto
496 1.1 ws /*
497 1.6 thorpej * Finally, allocate mbuf cluster submap.
498 1.1 ws */
499 1.19 sakamoto #if defined(UVM)
500 1.26 sakamoto mb_map = uvm_km_suballoc(kernel_map, (vaddr_t *)&mbutl, &maxaddr,
501 1.19 sakamoto VM_MBUF_SIZE, FALSE, FALSE, NULL);
502 1.19 sakamoto #else
503 1.26 sakamoto mb_map = kmem_suballoc(kernel_map, (vaddr_t *)&mbutl, &maxaddr,
504 1.1 ws VM_MBUF_SIZE, FALSE);
505 1.19 sakamoto #endif
506 1.19 sakamoto
507 1.1 ws /*
508 1.1 ws * Initialize callouts.
509 1.1 ws */
510 1.1 ws callfree = callout;
511 1.1 ws for (i = 1; i < ncallout; i++)
512 1.1 ws callout[i - 1].c_next = &callout[i];
513 1.19 sakamoto
514 1.19 sakamoto #if defined(UVM)
515 1.20 sakamoto printf("avail memory = %d\n", ptoa(uvmexp.free));
516 1.19 sakamoto #else
517 1.20 sakamoto printf("avail memory = %d\n", ptoa(cnt.v_free_count));
518 1.19 sakamoto #endif
519 1.3 christos printf("using %d buffers containing %d bytes of memory\n",
520 1.1 ws nbuf, bufpages * CLBYTES);
521 1.1 ws
522 1.1 ws /*
523 1.1 ws * Set up the buffers.
524 1.1 ws */
525 1.1 ws bufinit();
526 1.1 ws
527 1.1 ws /*
528 1.7 thorpej * For now, use soft spl handling.
529 1.7 thorpej */
530 1.7 thorpej {
531 1.7 thorpej extern struct machvec soft_machvec;
532 1.7 thorpej
533 1.7 thorpej machine_interface = soft_machvec;
534 1.7 thorpej }
535 1.7 thorpej
536 1.7 thorpej /*
537 1.1 ws * Now allow hardware interrupts.
538 1.1 ws */
539 1.1 ws {
540 1.1 ws int msr;
541 1.1 ws
542 1.1 ws splhigh();
543 1.7 thorpej asm volatile ("mfmsr %0; ori %0,%0,%1; mtmsr %0"
544 1.7 thorpej : "=r"(msr) : "K"((u_short)(PSL_EE|PSL_RI)));
545 1.1 ws }
546 1.1 ws
547 1.1 ws /*
548 1.1 ws * Configure devices.
549 1.1 ws */
550 1.1 ws configure();
551 1.1 ws }
552 1.1 ws
553 1.1 ws /*
554 1.1 ws * Allocate space for system data structures.
555 1.1 ws */
556 1.1 ws caddr_t
557 1.1 ws allocsys(v)
558 1.1 ws caddr_t v;
559 1.1 ws {
560 1.1 ws #define valloc(name, type, num) \
561 1.1 ws v = (caddr_t)(((name) = (type *)v) + (num))
562 1.1 ws
563 1.1 ws valloc(callout, struct callout, ncallout);
564 1.1 ws #ifdef SYSVSHM
565 1.1 ws valloc(shmsegs, struct shmid_ds, shminfo.shmmni);
566 1.1 ws #endif
567 1.1 ws #ifdef SYSVSEM
568 1.1 ws valloc(sema, struct semid_ds, seminfo.semmni);
569 1.1 ws valloc(sem, struct sem, seminfo.semmns);
570 1.1 ws valloc(semu, int, (seminfo.semmnu * seminfo.semusz) / sizeof(int));
571 1.1 ws #endif
572 1.1 ws #ifdef SYSVMSG
573 1.1 ws valloc(msgpool, char, msginfo.msgmax);
574 1.1 ws valloc(msgmaps, struct msgmap, msginfo.msgseg);
575 1.1 ws valloc(msghdrs, struct msg, msginfo.msgtql);
576 1.1 ws valloc(msqids, struct msqid_ds, msginfo.msgmni);
577 1.1 ws #endif
578 1.1 ws
579 1.1 ws /*
580 1.1 ws * Decide on buffer space to use.
581 1.1 ws */
582 1.1 ws if (bufpages == 0)
583 1.1 ws bufpages = (physmem / 20) / CLSIZE;
584 1.1 ws if (nbuf == 0) {
585 1.1 ws nbuf = bufpages;
586 1.1 ws if (nbuf < 16)
587 1.1 ws nbuf = 16;
588 1.1 ws }
589 1.1 ws if (nswbuf == 0) {
590 1.1 ws nswbuf = (nbuf / 2) & ~1;
591 1.1 ws if (nswbuf > 256)
592 1.1 ws nswbuf = 256;
593 1.1 ws }
594 1.19 sakamoto #if !defined(UVM)
595 1.1 ws valloc(swbuf, struct buf, nswbuf);
596 1.19 sakamoto #endif
597 1.1 ws valloc(buf, struct buf, nbuf);
598 1.1 ws
599 1.1 ws return v;
600 1.1 ws }
601 1.1 ws
602 1.1 ws /*
603 1.1 ws * consinit
604 1.1 ws * Initialize system console.
605 1.1 ws */
606 1.1 ws void
607 1.1 ws consinit()
608 1.1 ws {
609 1.1 ws static int initted;
610 1.1 ws
611 1.1 ws if (initted)
612 1.1 ws return;
613 1.1 ws initted = 1;
614 1.1 ws cninit();
615 1.1 ws }
616 1.1 ws
617 1.1 ws /*
618 1.7 thorpej * Set set up registers on exec.
619 1.1 ws */
620 1.1 ws void
621 1.11 mycroft setregs(p, pack, stack)
622 1.1 ws struct proc *p;
623 1.1 ws struct exec_package *pack;
624 1.1 ws u_long stack;
625 1.1 ws {
626 1.1 ws struct trapframe *tf = trapframe(p);
627 1.7 thorpej struct ps_strings arginfo;
628 1.1 ws
629 1.1 ws bzero(tf, sizeof *tf);
630 1.1 ws tf->fixreg[1] = -roundup(-stack + 8, 16);
631 1.7 thorpej
632 1.7 thorpej /*
633 1.7 thorpej * XXX Machine-independent code has already copied arguments and
634 1.7 thorpej * XXX environment to userland. Get them back here.
635 1.7 thorpej */
636 1.7 thorpej (void)copyin((char *)PS_STRINGS, &arginfo, sizeof(arginfo));
637 1.7 thorpej
638 1.7 thorpej /*
639 1.7 thorpej * Set up arguments for _start():
640 1.7 thorpej * _start(argc, argv, envp, obj, cleanup, ps_strings);
641 1.7 thorpej *
642 1.7 thorpej * Notes:
643 1.7 thorpej * - obj and cleanup are the auxilliary and termination
644 1.7 thorpej * vectors. They are fixed up by ld.elf_so.
645 1.7 thorpej * - ps_strings is a NetBSD extention, and will be
646 1.7 thorpej * ignored by executables which are strictly
647 1.7 thorpej * compliant with the SVR4 ABI.
648 1.7 thorpej *
649 1.7 thorpej * XXX We have to set both regs and retval here due to different
650 1.7 thorpej * XXX calling convention in trap.c and init_main.c.
651 1.7 thorpej */
652 1.12 mycroft tf->fixreg[3] = arginfo.ps_nargvstr;
653 1.12 mycroft tf->fixreg[4] = (register_t)arginfo.ps_argvstr;
654 1.7 thorpej tf->fixreg[5] = (register_t)arginfo.ps_envstr;
655 1.7 thorpej tf->fixreg[6] = 0; /* auxillary vector */
656 1.7 thorpej tf->fixreg[7] = 0; /* termination vector */
657 1.7 thorpej tf->fixreg[8] = (register_t)PS_STRINGS; /* NetBSD extension */
658 1.7 thorpej
659 1.1 ws tf->srr0 = pack->ep_entry;
660 1.1 ws tf->srr1 = PSL_MBO | PSL_USERSET | PSL_FE_DFLT;
661 1.1 ws p->p_addr->u_pcb.pcb_flags = 0;
662 1.1 ws }
663 1.1 ws
664 1.1 ws /*
665 1.1 ws * Send a signal to process.
666 1.1 ws */
667 1.1 ws void
668 1.1 ws sendsig(catcher, sig, mask, code)
669 1.1 ws sig_t catcher;
670 1.27 thorpej int sig;
671 1.27 thorpej sigset_t *mask;
672 1.1 ws u_long code;
673 1.1 ws {
674 1.1 ws struct proc *p = curproc;
675 1.1 ws struct trapframe *tf;
676 1.1 ws struct sigframe *fp, frame;
677 1.1 ws struct sigacts *psp = p->p_sigacts;
678 1.27 thorpej int onstack;
679 1.27 thorpej
680 1.1 ws tf = trapframe(p);
681 1.27 thorpej
682 1.27 thorpej /* Do we need to jump onto the signal stack? */
683 1.27 thorpej onstack =
684 1.27 thorpej (psp->ps_sigstk.ss_flags & (SS_DISABLE | SS_ONSTACK)) == 0 &&
685 1.27 thorpej (psp->ps_sigact[sig].sa_flags & SA_ONSTACK) != 0;
686 1.27 thorpej
687 1.27 thorpej /* Allocate space for the signal handler context. */
688 1.27 thorpej if (onstack)
689 1.27 thorpej fp = (struct sigframe *)((caddr_t)psp->ps_sigstk.ss_sp +
690 1.27 thorpej psp->ps_sigstk.ss_size);
691 1.27 thorpej else
692 1.1 ws fp = (struct sigframe *)tf->fixreg[1];
693 1.1 ws fp = (struct sigframe *)((int)(fp - 1) & ~0xf);
694 1.27 thorpej
695 1.27 thorpej /* Build stack frame for signal trampoline. */
696 1.27 thorpej frame.sf_signum = sig;
697 1.1 ws frame.sf_code = code;
698 1.27 thorpej
699 1.27 thorpej /* Save register context. */
700 1.27 thorpej bcopy(tf, &frame.sf_sc.sc_frame, sizeof *tf);
701 1.27 thorpej
702 1.27 thorpej /* Save signal stack. */
703 1.27 thorpej frame.sf_sc.sc_onstack = psp->ps_sigstk.ss_flags & SS_ONSTACK;
704 1.27 thorpej
705 1.27 thorpej /* Save signal mask. */
706 1.27 thorpej frame.sf_sc.sc_mask = *mask;
707 1.27 thorpej
708 1.27 thorpej #ifdef COMPAT_13
709 1.1 ws /*
710 1.27 thorpej * XXX We always have to save an old style signal mask because
711 1.27 thorpej * XXX we might be delivering a signal to a process which will
712 1.27 thorpej * XXX escape from the signal in a non-standard way and invoke
713 1.27 thorpej * XXX sigreturn() directly.
714 1.1 ws */
715 1.27 thorpej native_sigset_to_sigset13(mask, &frame.sf_sc.__sc_mask13);
716 1.27 thorpej #endif
717 1.27 thorpej
718 1.27 thorpej if (copyout(&frame, fp, sizeof frame) != 0) {
719 1.27 thorpej /*
720 1.27 thorpej * Process has trashed its stack; give it an illegal
721 1.27 thorpej * instructoin to halt it in its tracks.
722 1.27 thorpej */
723 1.1 ws sigexit(p, SIGILL);
724 1.27 thorpej }
725 1.27 thorpej
726 1.27 thorpej /*
727 1.27 thorpej * Build context to run handler in.
728 1.27 thorpej */
729 1.1 ws tf->fixreg[1] = (int)fp;
730 1.1 ws tf->lr = (int)catcher;
731 1.1 ws tf->fixreg[3] = (int)sig;
732 1.1 ws tf->fixreg[4] = (int)code;
733 1.1 ws tf->fixreg[5] = (int)&frame.sf_sc;
734 1.1 ws tf->srr0 = (int)(((char *)PS_STRINGS)
735 1.1 ws - (p->p_emul->e_esigcode - p->p_emul->e_sigcode));
736 1.1 ws }
737 1.1 ws
738 1.1 ws /*
739 1.1 ws * System call to cleanup state after a signal handler returns.
740 1.1 ws */
741 1.1 ws int
742 1.27 thorpej sys___sigreturn14(p, v, retval)
743 1.1 ws struct proc *p;
744 1.1 ws void *v;
745 1.1 ws register_t *retval;
746 1.1 ws {
747 1.27 thorpej struct sys___sigreturn14_args /* {
748 1.1 ws syscallarg(struct sigcontext *) sigcntxp;
749 1.1 ws } */ *uap = v;
750 1.1 ws struct sigcontext sc;
751 1.1 ws struct trapframe *tf;
752 1.1 ws int error;
753 1.27 thorpej
754 1.27 thorpej /*
755 1.27 thorpej * The trampoline hands us the context.
756 1.27 thorpej * It is unsafe to keep track of it ourselves, in the event that a
757 1.27 thorpej * program jumps out of a signal hander.
758 1.27 thorpej */
759 1.27 thorpej if ((error = copyin(SCARG(uap, sigcntxp), &sc, sizeof sc)) != 0)
760 1.27 thorpej return (error);
761 1.27 thorpej
762 1.27 thorpej /* Restore the register context. */
763 1.1 ws tf = trapframe(p);
764 1.1 ws if ((sc.sc_frame.srr1 & PSL_USERSTATIC) != (tf->srr1 & PSL_USERSTATIC))
765 1.27 thorpej return (EINVAL);
766 1.1 ws bcopy(&sc.sc_frame, tf, sizeof *tf);
767 1.27 thorpej
768 1.27 thorpej /* Restore signal stack. */
769 1.27 thorpej if (sc.sc_onstack & SS_ONSTACK)
770 1.1 ws p->p_sigacts->ps_sigstk.ss_flags |= SS_ONSTACK;
771 1.1 ws else
772 1.1 ws p->p_sigacts->ps_sigstk.ss_flags &= ~SS_ONSTACK;
773 1.27 thorpej
774 1.27 thorpej /* Restore signal mask. */
775 1.27 thorpej (void) sigprocmask1(p, SIG_SETMASK, &sc.sc_mask, 0);
776 1.27 thorpej
777 1.27 thorpej return (EJUSTRETURN);
778 1.1 ws }
779 1.1 ws
780 1.1 ws /*
781 1.1 ws * Machine dependent system variables.
782 1.1 ws * None for now.
783 1.1 ws */
784 1.1 ws int
785 1.1 ws cpu_sysctl(name, namelen, oldp, oldlenp, newp, newlen, p)
786 1.1 ws int *name;
787 1.1 ws u_int namelen;
788 1.1 ws void *oldp;
789 1.1 ws size_t *oldlenp;
790 1.1 ws void *newp;
791 1.1 ws size_t newlen;
792 1.1 ws struct proc *p;
793 1.1 ws {
794 1.1 ws /* all sysctl names at this level are terminal */
795 1.1 ws if (namelen != 1)
796 1.1 ws return ENOTDIR;
797 1.1 ws switch (name[0]) {
798 1.1 ws default:
799 1.1 ws return EOPNOTSUPP;
800 1.1 ws }
801 1.1 ws }
802 1.1 ws
803 1.1 ws /*
804 1.1 ws * Crash dump handling.
805 1.1 ws */
806 1.1 ws u_long dumpmag = 0x8fca0101; /* magic number */
807 1.1 ws int dumpsize = 0; /* size of dump in pages */
808 1.1 ws long dumplo = -1; /* blocks */
809 1.1 ws
810 1.1 ws void
811 1.1 ws dumpsys()
812 1.1 ws {
813 1.3 christos printf("dumpsys: TBD\n");
814 1.1 ws }
815 1.1 ws
816 1.1 ws /*
817 1.1 ws * Soft networking interrupts.
818 1.1 ws */
819 1.1 ws void
820 1.1 ws softnet()
821 1.1 ws {
822 1.1 ws int isr = netisr;
823 1.1 ws
824 1.1 ws netisr = 0;
825 1.1 ws #ifdef INET
826 1.13 veego #include "arp.h"
827 1.13 veego #if NARP > 0
828 1.1 ws if (isr & (1 << NETISR_ARP))
829 1.1 ws arpintr();
830 1.1 ws #endif
831 1.1 ws if (isr & (1 << NETISR_IP))
832 1.1 ws ipintr();
833 1.1 ws #endif
834 1.1 ws #ifdef IMP
835 1.1 ws if (isr & (1 << NETISR_IMP))
836 1.1 ws impintr();
837 1.1 ws #endif
838 1.1 ws #ifdef NS
839 1.1 ws if (isr & (1 << NETISR_NS))
840 1.1 ws nsintr();
841 1.1 ws #endif
842 1.1 ws #ifdef ISO
843 1.1 ws if (isr & (1 << NETISR_ISO))
844 1.1 ws clnlintr();
845 1.1 ws #endif
846 1.1 ws #ifdef CCITT
847 1.1 ws if (isr & (1 << NETISR_CCITT))
848 1.1 ws ccittintr();
849 1.1 ws #endif
850 1.1 ws #include "ppp.h"
851 1.1 ws #if NPPP > 0
852 1.1 ws if (isr & (1 << NETISR_PPP))
853 1.1 ws pppintr();
854 1.1 ws #endif
855 1.1 ws }
856 1.1 ws
857 1.1 ws /*
858 1.1 ws * Stray interrupts.
859 1.1 ws */
860 1.1 ws void
861 1.1 ws strayintr(irq)
862 1.1 ws int irq;
863 1.1 ws {
864 1.1 ws log(LOG_ERR, "stray interrupt %d\n", irq);
865 1.1 ws }
866 1.1 ws
867 1.1 ws /*
868 1.1 ws * Halt or reboot the machine after syncing/dumping according to howto.
869 1.1 ws */
870 1.1 ws void
871 1.5 gwr cpu_reboot(howto, what)
872 1.1 ws int howto;
873 1.1 ws char *what;
874 1.1 ws {
875 1.1 ws static int syncing;
876 1.1 ws static char str[256];
877 1.1 ws char *ap = str, *ap1 = ap;
878 1.1 ws
879 1.1 ws boothowto = howto;
880 1.1 ws if (!cold && !(howto & RB_NOSYNC) && !syncing) {
881 1.1 ws syncing = 1;
882 1.1 ws vfs_shutdown(); /* sync */
883 1.1 ws resettodr(); /* set wall clock */
884 1.1 ws }
885 1.1 ws splhigh();
886 1.1 ws if (howto & RB_HALT) {
887 1.1 ws doshutdownhooks();
888 1.3 christos printf("halted\n\n");
889 1.1 ws ppc_exit();
890 1.1 ws }
891 1.1 ws if (!cold && (howto & RB_DUMP))
892 1.1 ws dumpsys();
893 1.1 ws doshutdownhooks();
894 1.3 christos printf("rebooting\n\n");
895 1.1 ws if (what && *what) {
896 1.1 ws if (strlen(what) > sizeof str - 5)
897 1.3 christos printf("boot string too large, ignored\n");
898 1.1 ws else {
899 1.1 ws strcpy(str, what);
900 1.1 ws ap1 = ap = str + strlen(str);
901 1.1 ws *ap++ = ' ';
902 1.1 ws }
903 1.1 ws }
904 1.1 ws *ap++ = '-';
905 1.1 ws if (howto & RB_SINGLE)
906 1.1 ws *ap++ = 's';
907 1.1 ws if (howto & RB_KDB)
908 1.1 ws *ap++ = 'd';
909 1.1 ws *ap++ = 0;
910 1.1 ws if (ap[-2] == '-')
911 1.1 ws *ap1 = 0;
912 1.1 ws ppc_boot(str);
913 1.1 ws }
914 1.1 ws
915 1.1 ws /*
916 1.1 ws * OpenFirmware callback routine
917 1.1 ws */
918 1.1 ws void
919 1.1 ws callback(p)
920 1.1 ws void *p;
921 1.1 ws {
922 1.1 ws panic("callback"); /* for now XXX */
923 1.1 ws }
924 1.1 ws
925 1.1 ws /*
926 1.7 thorpej * Initial Machine Interface.
927 1.1 ws */
928 1.7 thorpej static int
929 1.7 thorpej fake_spl()
930 1.7 thorpej {
931 1.7 thorpej int scratch;
932 1.7 thorpej
933 1.7 thorpej asm volatile ("mfmsr %0; andi. %0,%0,%1; mtmsr %0; isync"
934 1.7 thorpej : "=r"(scratch) : "K"((u_short)~(PSL_EE|PSL_ME)));
935 1.7 thorpej return -1;
936 1.7 thorpej }
937 1.7 thorpej
938 1.1 ws static void
939 1.7 thorpej fake_setsoft()
940 1.7 thorpej {
941 1.7 thorpej /* Do nothing */
942 1.7 thorpej }
943 1.7 thorpej
944 1.7 thorpej static int
945 1.1 ws fake_splx(new)
946 1.1 ws int new;
947 1.1 ws {
948 1.7 thorpej return fake_spl();
949 1.7 thorpej }
950 1.7 thorpej
951 1.7 thorpej static void
952 1.7 thorpej fake_clock_return(frame, nticks)
953 1.7 thorpej struct clockframe *frame;
954 1.7 thorpej int nticks;
955 1.7 thorpej {
956 1.7 thorpej /* Do nothing */
957 1.1 ws }
958 1.1 ws
959 1.1 ws static void
960 1.1 ws fake_irq_establish(irq, level, handler, arg)
961 1.1 ws int irq, level;
962 1.1 ws void (*handler) __P((void *));
963 1.1 ws void *arg;
964 1.1 ws {
965 1.1 ws panic("fake_irq_establish");
966 1.1 ws }
967