machdep.c revision 1.5 1 1.5 gwr /* $NetBSD: machdep.c,v 1.5 1997/03/26 22:43:03 gwr 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.4 ws #include "ipkdb.h"
34 1.1 ws
35 1.1 ws #include <sys/param.h>
36 1.1 ws #include <sys/buf.h>
37 1.1 ws #include <sys/callout.h>
38 1.1 ws #include <sys/exec.h>
39 1.1 ws #include <sys/malloc.h>
40 1.1 ws #include <sys/map.h>
41 1.1 ws #include <sys/mbuf.h>
42 1.1 ws #include <sys/mount.h>
43 1.1 ws #include <sys/msgbuf.h>
44 1.1 ws #include <sys/proc.h>
45 1.1 ws #include <sys/reboot.h>
46 1.1 ws #include <sys/syscallargs.h>
47 1.1 ws #include <sys/syslog.h>
48 1.1 ws #include <sys/systm.h>
49 1.1 ws #include <sys/user.h>
50 1.1 ws
51 1.1 ws #include <vm/vm.h>
52 1.1 ws #include <vm/vm_kern.h>
53 1.1 ws
54 1.1 ws #include <net/netisr.h>
55 1.1 ws
56 1.1 ws #include <machine/bat.h>
57 1.1 ws #include <machine/pmap.h>
58 1.1 ws #include <machine/powerpc.h>
59 1.1 ws #include <machine/trap.h>
60 1.1 ws
61 1.1 ws /*
62 1.1 ws * Global variables used here and there
63 1.1 ws */
64 1.1 ws struct pcb *curpcb;
65 1.1 ws struct pmap *curpm;
66 1.1 ws struct proc *fpuproc;
67 1.1 ws
68 1.1 ws extern struct user *proc0paddr;
69 1.1 ws
70 1.1 ws struct bat battable[16];
71 1.1 ws
72 1.1 ws int astpending;
73 1.1 ws
74 1.1 ws char *bootpath;
75 1.1 ws
76 1.1 ws /*
77 1.1 ws * We use the page just above the interrupt vector as message buffer
78 1.1 ws */
79 1.1 ws struct msgbuf *msgbufp = (struct msgbuf *)0x3000;
80 1.1 ws int msgbufmapped = 1; /* message buffer is always mapped */
81 1.1 ws
82 1.1 ws caddr_t allocsys __P((caddr_t));
83 1.1 ws
84 1.1 ws static void fake_splx __P((int));
85 1.1 ws static void fake_irq_establish __P((int, int, void (*)(void *), void *));
86 1.1 ws
87 1.1 ws struct machvec machine_interface = {
88 1.1 ws fake_splx,
89 1.1 ws fake_irq_establish,
90 1.1 ws };
91 1.1 ws
92 1.1 ws int cold = 1;
93 1.1 ws
94 1.1 ws void
95 1.1 ws initppc(startkernel, endkernel, args)
96 1.1 ws u_int startkernel, endkernel;
97 1.1 ws char *args;
98 1.1 ws {
99 1.1 ws int phandle, qhandle;
100 1.1 ws char name[32];
101 1.1 ws struct machvec *mp;
102 1.1 ws extern trapcode, trapsize;
103 1.1 ws extern dsitrap, dsisize;
104 1.1 ws extern isitrap, isisize;
105 1.1 ws extern decrint, decrsize;
106 1.1 ws extern tlbimiss, tlbimsize;
107 1.1 ws extern tlbdlmiss, tlbdlmsize;
108 1.1 ws extern tlbdsmiss, tlbdsmsize;
109 1.4 ws #if NIPKDB > 0
110 1.4 ws extern ipkdblow, ipkdbsize;
111 1.1 ws #endif
112 1.1 ws extern void consinit __P((void));
113 1.1 ws extern void callback __P((void *));
114 1.1 ws int exc, scratch;
115 1.1 ws
116 1.1 ws proc0.p_addr = proc0paddr;
117 1.1 ws bzero(proc0.p_addr, sizeof *proc0.p_addr);
118 1.1 ws
119 1.1 ws curpcb = &proc0paddr->u_pcb;
120 1.1 ws
121 1.1 ws curpm = curpcb->pcb_pmreal = curpcb->pcb_pm = pmap_kernel();
122 1.1 ws
123 1.1 ws /*
124 1.1 ws * i386 port says, that this shouldn't be here,
125 1.1 ws * but I really think the console should be initialized
126 1.1 ws * as early as possible.
127 1.1 ws */
128 1.1 ws consinit();
129 1.1 ws
130 1.1 ws #ifdef __notyet__ /* Needs some rethinking regarding real/virtual OFW */
131 1.1 ws OF_set_callback(callback);
132 1.1 ws #endif
133 1.1 ws /*
134 1.1 ws * Initialize BAT registers to unmapped to not generate
135 1.1 ws * overlapping mappings below.
136 1.1 ws */
137 1.1 ws asm volatile ("mtibatu 0,%0" :: "r"(0));
138 1.1 ws asm volatile ("mtibatu 1,%0" :: "r"(0));
139 1.1 ws asm volatile ("mtibatu 2,%0" :: "r"(0));
140 1.1 ws asm volatile ("mtibatu 3,%0" :: "r"(0));
141 1.1 ws asm volatile ("mtdbatu 0,%0" :: "r"(0));
142 1.1 ws asm volatile ("mtdbatu 1,%0" :: "r"(0));
143 1.1 ws asm volatile ("mtdbatu 2,%0" :: "r"(0));
144 1.1 ws asm volatile ("mtdbatu 3,%0" :: "r"(0));
145 1.1 ws
146 1.1 ws /*
147 1.1 ws * Set up initial BAT table to only map the lowest 256 MB area
148 1.1 ws */
149 1.1 ws battable[0].batl = BATL(0x00000000, BAT_M);
150 1.1 ws battable[0].batu = BATU(0x00000000);
151 1.1 ws
152 1.1 ws /*
153 1.1 ws * Now setup fixed bat registers
154 1.1 ws *
155 1.1 ws * Note that we still run in real mode, and the BAT
156 1.1 ws * registers were cleared above.
157 1.1 ws */
158 1.1 ws /* IBAT0 used for initial 256 MB segment */
159 1.1 ws asm volatile ("mtibatl 0,%0; mtibatu 0,%1"
160 1.1 ws :: "r"(battable[0].batl), "r"(battable[0].batu));
161 1.1 ws /* DBAT0 used similar */
162 1.1 ws asm volatile ("mtdbatl 0,%0; mtdbatu 0,%1"
163 1.1 ws :: "r"(battable[0].batl), "r"(battable[0].batu));
164 1.1 ws
165 1.1 ws /*
166 1.1 ws * Set up trap vectors
167 1.1 ws */
168 1.1 ws for (exc = EXC_RSVD; exc <= EXC_LAST; exc += 0x100)
169 1.1 ws switch (exc) {
170 1.1 ws default:
171 1.1 ws bcopy(&trapcode, (void *)exc, (size_t)&trapsize);
172 1.1 ws break;
173 1.1 ws case EXC_EXI:
174 1.1 ws /*
175 1.1 ws * This one is (potentially) installed during autoconf
176 1.1 ws */
177 1.1 ws break;
178 1.1 ws case EXC_DSI:
179 1.1 ws bcopy(&dsitrap, (void *)EXC_DSI, (size_t)&dsisize);
180 1.1 ws break;
181 1.1 ws case EXC_ISI:
182 1.1 ws bcopy(&isitrap, (void *)EXC_ISI, (size_t)&isisize);
183 1.1 ws break;
184 1.1 ws case EXC_DECR:
185 1.1 ws bcopy(&decrint, (void *)EXC_DECR, (size_t)&decrsize);
186 1.1 ws break;
187 1.1 ws case EXC_IMISS:
188 1.1 ws bcopy(&tlbimiss, (void *)EXC_IMISS, (size_t)&tlbimsize);
189 1.1 ws break;
190 1.1 ws case EXC_DLMISS:
191 1.1 ws bcopy(&tlbdlmiss, (void *)EXC_DLMISS, (size_t)&tlbdlmsize);
192 1.1 ws break;
193 1.1 ws case EXC_DSMISS:
194 1.1 ws bcopy(&tlbdsmiss, (void *)EXC_DSMISS, (size_t)&tlbdsmsize);
195 1.1 ws break;
196 1.4 ws #if NIPKDB > 0
197 1.1 ws case EXC_PGM:
198 1.1 ws case EXC_TRC:
199 1.1 ws case EXC_BPT:
200 1.4 ws bcopy(&ipkdblow, (void *)exc, (size_t)&ipkdbsize);
201 1.1 ws break;
202 1.1 ws #endif
203 1.1 ws }
204 1.1 ws
205 1.1 ws syncicache((void *)EXC_RST, EXC_LAST - EXC_RST + 0x100);
206 1.1 ws
207 1.1 ws /*
208 1.1 ws * Now enable translation (and machine checks/recoverable interrupts).
209 1.1 ws */
210 1.1 ws asm volatile ("mfmsr %0; ori %0,%0,%1; mtmsr %0; isync"
211 1.1 ws : "=r"(scratch) : "K"(PSL_IR|PSL_DR|PSL_ME|PSL_RI));
212 1.1 ws
213 1.1 ws /*
214 1.1 ws * Parse arg string.
215 1.1 ws */
216 1.1 ws bootpath = args;
217 1.1 ws while ( *++args && *args != ' ');
218 1.1 ws if (*args) {
219 1.1 ws *args++ = 0;
220 1.1 ws while (*args) {
221 1.1 ws switch (*args++) {
222 1.1 ws case 'a':
223 1.1 ws boothowto |= RB_ASKNAME;
224 1.1 ws break;
225 1.1 ws case 's':
226 1.1 ws boothowto |= RB_SINGLE;
227 1.1 ws break;
228 1.1 ws case 'd':
229 1.1 ws boothowto |= RB_KDB;
230 1.1 ws break;
231 1.1 ws }
232 1.1 ws }
233 1.1 ws }
234 1.1 ws
235 1.4 ws #if NIPKDB > 0
236 1.1 ws /*
237 1.4 ws * Now trap to IPKDB
238 1.1 ws */
239 1.4 ws ipkdb_init();
240 1.1 ws if (boothowto & RB_KDB)
241 1.4 ws ipkdb_connect(0);
242 1.1 ws #endif
243 1.1 ws
244 1.1 ws /*
245 1.1 ws * Initialize pmap module.
246 1.1 ws */
247 1.1 ws pmap_bootstrap(startkernel, endkernel);
248 1.1 ws }
249 1.1 ws
250 1.1 ws /*
251 1.1 ws * This should probably be in autoconf! XXX
252 1.1 ws */
253 1.1 ws int cpu;
254 1.1 ws char cpu_model[80];
255 1.1 ws char machine[] = "PowerPC"; /* cpu architecture */
256 1.1 ws
257 1.1 ws void
258 1.1 ws identifycpu()
259 1.1 ws {
260 1.1 ws int phandle, pvr;
261 1.1 ws char name[32];
262 1.1 ws
263 1.1 ws /*
264 1.1 ws * Find cpu type (Do it by OpenFirmware?)
265 1.1 ws */
266 1.1 ws asm ("mfpvr %0" : "=r"(pvr));
267 1.1 ws cpu = pvr >> 16;
268 1.1 ws switch (cpu) {
269 1.1 ws case 1:
270 1.3 christos sprintf(cpu_model, "601");
271 1.1 ws break;
272 1.1 ws case 3:
273 1.3 christos sprintf(cpu_model, "603");
274 1.1 ws break;
275 1.1 ws case 4:
276 1.3 christos sprintf(cpu_model, "604");
277 1.1 ws break;
278 1.1 ws case 5:
279 1.3 christos sprintf(cpu_model, "602");
280 1.1 ws break;
281 1.1 ws case 6:
282 1.3 christos sprintf(cpu_model, "603e");
283 1.1 ws break;
284 1.1 ws case 7:
285 1.3 christos sprintf(cpu_model, "603ev");
286 1.1 ws break;
287 1.1 ws case 9:
288 1.3 christos sprintf(cpu_model, "604ev");
289 1.1 ws break;
290 1.1 ws case 20:
291 1.3 christos sprintf(cpu_model, "620");
292 1.1 ws break;
293 1.1 ws default:
294 1.3 christos sprintf(cpu_model, "Version %x", cpu);
295 1.1 ws break;
296 1.1 ws }
297 1.1 ws sprintf(cpu_model + strlen(cpu_model), " (Revision %x)", pvr & 0xffff);
298 1.3 christos printf("CPU: %s\n", cpu_model);
299 1.1 ws }
300 1.1 ws
301 1.1 ws void
302 1.1 ws install_extint(handler)
303 1.1 ws void (*handler) __P((void));
304 1.1 ws {
305 1.1 ws extern extint, extsize;
306 1.1 ws extern u_long extint_call;
307 1.1 ws u_long offset = (u_long)handler - (u_long)&extint_call;
308 1.1 ws int omsr, msr;
309 1.1 ws
310 1.1 ws #ifdef DIAGNOSTIC
311 1.1 ws if (offset > 0x1ffffff)
312 1.1 ws panic("install_extint: too far away");
313 1.1 ws #endif
314 1.1 ws asm volatile ("mfmsr %0; andi. %1, %0, %2; mtmsr %1"
315 1.1 ws : "=r"(omsr), "=r"(msr) : "K"((u_short)~PSL_EE));
316 1.1 ws extint_call = (extint_call & 0xfc000003) | offset;
317 1.1 ws bcopy(&extint, (void *)EXC_EXI, (size_t)&extsize);
318 1.1 ws syncicache((void *)&extint_call, sizeof extint_call);
319 1.1 ws syncicache((void *)EXC_EXI, (int)&extsize);
320 1.1 ws asm volatile ("mtmsr %0" :: "r"(omsr));
321 1.1 ws }
322 1.1 ws
323 1.1 ws /*
324 1.1 ws * Machine dependent startup code.
325 1.1 ws */
326 1.1 ws void
327 1.1 ws cpu_startup()
328 1.1 ws {
329 1.1 ws int sz, i;
330 1.1 ws caddr_t v;
331 1.1 ws vm_offset_t minaddr, maxaddr;
332 1.1 ws int base, residual;
333 1.1 ws
334 1.1 ws proc0.p_addr = proc0paddr;
335 1.1 ws v = (caddr_t)proc0paddr + USPACE;
336 1.1 ws
337 1.3 christos printf("%s", version);
338 1.1 ws identifycpu();
339 1.1 ws
340 1.3 christos printf("real mem = %d\n", ctob(physmem));
341 1.1 ws
342 1.1 ws /*
343 1.1 ws * Find out how much space we need, allocate it,
344 1.1 ws * and then give everything true virtual addresses.
345 1.1 ws */
346 1.1 ws sz = (int)allocsys((caddr_t)0);
347 1.1 ws if ((v = (caddr_t)kmem_alloc(kernel_map, round_page(sz))) == 0)
348 1.1 ws panic("startup: no room for tables");
349 1.1 ws if (allocsys(v) - v != sz)
350 1.1 ws panic("startup: table size inconsistency");
351 1.1 ws
352 1.1 ws /*
353 1.1 ws * Now allocate buffers proper. They are different than the above
354 1.1 ws * in that they usually occupy more virtual memory than physical.
355 1.1 ws */
356 1.1 ws sz = MAXBSIZE * nbuf;
357 1.1 ws buffer_map = kmem_suballoc(kernel_map, &minaddr, &maxaddr, sz, TRUE);
358 1.1 ws buffers = (char *)minaddr;
359 1.1 ws if (vm_map_find(buffer_map, vm_object_allocate(sz), (vm_offset_t)0,
360 1.1 ws &minaddr, sz, FALSE) != KERN_SUCCESS)
361 1.1 ws panic("startup: cannot allocate buffers");
362 1.1 ws base = bufpages / nbuf;
363 1.1 ws residual = bufpages % nbuf;
364 1.1 ws if (base >= MAXBSIZE) {
365 1.1 ws /* Don't want to alloc more physical mem than ever needed */
366 1.1 ws base = MAXBSIZE;
367 1.1 ws residual = 0;
368 1.1 ws }
369 1.1 ws for (i = 0; i < nbuf; i++) {
370 1.1 ws vm_size_t curbufsize;
371 1.1 ws vm_offset_t curbuf;
372 1.1 ws
373 1.1 ws curbuf = (vm_offset_t)buffers + i * MAXBSIZE;
374 1.1 ws curbufsize = CLBYTES * (i < residual ? base + 1 : base);
375 1.1 ws vm_map_pageable(buffer_map, curbuf, curbuf + curbufsize, FALSE);
376 1.1 ws vm_map_simplify(buffer_map, curbuf);
377 1.1 ws }
378 1.1 ws
379 1.1 ws /*
380 1.1 ws * Allocate a submap for exec arguments. This map effectively
381 1.1 ws * limits the number of processes exec'ing at any time.
382 1.1 ws */
383 1.1 ws exec_map = kmem_suballoc(kernel_map, &minaddr, &maxaddr,
384 1.1 ws 16*NCARGS, TRUE);
385 1.1 ws
386 1.1 ws /*
387 1.1 ws * Allocate a submap for physio
388 1.1 ws */
389 1.1 ws phys_map = kmem_suballoc(kernel_map, &minaddr, &maxaddr,
390 1.1 ws VM_PHYS_SIZE, TRUE);
391 1.1 ws
392 1.1 ws /*
393 1.1 ws * Allocate mbuf pool.
394 1.1 ws */
395 1.1 ws mclrefcnt = (char *)malloc(NMBCLUSTERS + CLBYTES/MCLBYTES,
396 1.1 ws M_MBUF, M_NOWAIT);
397 1.1 ws bzero(mclrefcnt, NMBCLUSTERS + CLBYTES/MCLBYTES);
398 1.1 ws mb_map = kmem_suballoc(kernel_map, (vm_offset_t *)&mbutl, &maxaddr,
399 1.1 ws VM_MBUF_SIZE, FALSE);
400 1.1 ws
401 1.1 ws /*
402 1.1 ws * Initialize callouts.
403 1.1 ws */
404 1.1 ws callfree = callout;
405 1.1 ws for (i = 1; i < ncallout; i++)
406 1.1 ws callout[i - 1].c_next = &callout[i];
407 1.1 ws
408 1.3 christos printf("avail mem = %d\n", ptoa(cnt.v_free_count));
409 1.3 christos printf("using %d buffers containing %d bytes of memory\n",
410 1.1 ws nbuf, bufpages * CLBYTES);
411 1.1 ws
412 1.1 ws /*
413 1.1 ws * Set up the buffers.
414 1.1 ws */
415 1.1 ws bufinit();
416 1.1 ws
417 1.1 ws /*
418 1.1 ws * Now allow hardware interrupts.
419 1.1 ws */
420 1.1 ws {
421 1.1 ws int msr;
422 1.1 ws
423 1.1 ws splhigh();
424 1.1 ws asm volatile ("mfmsr %0; ori %0, %0, %1; mtmsr %0"
425 1.1 ws : "=r"(msr) : "K"(PSL_EE));
426 1.1 ws }
427 1.1 ws
428 1.1 ws /*
429 1.1 ws * Configure devices.
430 1.1 ws */
431 1.1 ws configure();
432 1.1 ws
433 1.1 ws }
434 1.1 ws
435 1.1 ws /*
436 1.1 ws * Allocate space for system data structures.
437 1.1 ws */
438 1.1 ws caddr_t
439 1.1 ws allocsys(v)
440 1.1 ws caddr_t v;
441 1.1 ws {
442 1.1 ws #define valloc(name, type, num) \
443 1.1 ws v = (caddr_t)(((name) = (type *)v) + (num))
444 1.1 ws
445 1.1 ws valloc(callout, struct callout, ncallout);
446 1.1 ws valloc(swapmap, struct map, nswapmap = maxproc * 2);
447 1.1 ws #ifdef SYSVSHM
448 1.1 ws valloc(shmsegs, struct shmid_ds, shminfo.shmmni);
449 1.1 ws #endif
450 1.1 ws #ifdef SYSVSEM
451 1.1 ws valloc(sema, struct semid_ds, seminfo.semmni);
452 1.1 ws valloc(sem, struct sem, seminfo.semmns);
453 1.1 ws valloc(semu, int, (seminfo.semmnu * seminfo.semusz) / sizeof(int));
454 1.1 ws #endif
455 1.1 ws #ifdef SYSVMSG
456 1.1 ws valloc(msgpool, char, msginfo.msgmax);
457 1.1 ws valloc(msgmaps, struct msgmap, msginfo.msgseg);
458 1.1 ws valloc(msghdrs, struct msg, msginfo.msgtql);
459 1.1 ws valloc(msqids, struct msqid_ds, msginfo.msgmni);
460 1.1 ws #endif
461 1.1 ws
462 1.1 ws /*
463 1.1 ws * Decide on buffer space to use.
464 1.1 ws */
465 1.1 ws if (bufpages == 0)
466 1.1 ws bufpages = (physmem / 20) / CLSIZE;
467 1.1 ws if (nbuf == 0) {
468 1.1 ws nbuf = bufpages;
469 1.1 ws if (nbuf < 16)
470 1.1 ws nbuf = 16;
471 1.1 ws }
472 1.1 ws if (nswbuf == 0) {
473 1.1 ws nswbuf = (nbuf / 2) & ~1;
474 1.1 ws if (nswbuf > 256)
475 1.1 ws nswbuf = 256;
476 1.1 ws }
477 1.1 ws valloc(swbuf, struct buf, nswbuf);
478 1.1 ws valloc(buf, struct buf, nbuf);
479 1.1 ws
480 1.1 ws return v;
481 1.1 ws }
482 1.1 ws
483 1.1 ws /*
484 1.1 ws * consinit
485 1.1 ws * Initialize system console.
486 1.1 ws */
487 1.1 ws void
488 1.1 ws consinit()
489 1.1 ws {
490 1.1 ws static int initted;
491 1.1 ws
492 1.1 ws if (initted)
493 1.1 ws return;
494 1.1 ws initted = 1;
495 1.1 ws cninit();
496 1.1 ws }
497 1.1 ws
498 1.1 ws /*
499 1.1 ws * Clear registers on exec
500 1.1 ws */
501 1.1 ws void
502 1.1 ws setregs(p, pack, stack, retval)
503 1.1 ws struct proc *p;
504 1.1 ws struct exec_package *pack;
505 1.1 ws u_long stack;
506 1.1 ws register_t *retval;
507 1.1 ws {
508 1.1 ws struct trapframe *tf = trapframe(p);
509 1.1 ws
510 1.1 ws bzero(tf, sizeof *tf);
511 1.1 ws tf->fixreg[1] = -roundup(-stack + 8, 16);
512 1.1 ws tf->fixreg[3] = *retval = stack; /* bug in init_main.c XXX */
513 1.1 ws tf->srr0 = pack->ep_entry;
514 1.1 ws tf->srr1 = PSL_MBO | PSL_USERSET | PSL_FE_DFLT;
515 1.1 ws p->p_addr->u_pcb.pcb_flags = 0;
516 1.1 ws }
517 1.1 ws
518 1.1 ws /*
519 1.1 ws * Send a signal to process.
520 1.1 ws */
521 1.1 ws void
522 1.1 ws sendsig(catcher, sig, mask, code)
523 1.1 ws sig_t catcher;
524 1.1 ws int sig, mask;
525 1.1 ws u_long code;
526 1.1 ws {
527 1.1 ws struct proc *p = curproc;
528 1.1 ws struct trapframe *tf;
529 1.1 ws struct sigframe *fp, frame;
530 1.1 ws struct sigacts *psp = p->p_sigacts;
531 1.1 ws int oldonstack;
532 1.1 ws
533 1.1 ws frame.sf_signum = sig;
534 1.1 ws
535 1.1 ws tf = trapframe(p);
536 1.1 ws oldonstack = psp->ps_sigstk.ss_flags & SS_ONSTACK;
537 1.1 ws
538 1.1 ws /*
539 1.1 ws * Allocate stack space for signal handler.
540 1.1 ws */
541 1.1 ws if ((psp->ps_flags & SAS_ALTSTACK)
542 1.1 ws && !oldonstack
543 1.1 ws && (psp->ps_sigonstack & sigmask(sig))) {
544 1.1 ws fp = (struct sigframe *)(psp->ps_sigstk.ss_sp
545 1.1 ws + psp->ps_sigstk.ss_size);
546 1.1 ws psp->ps_sigstk.ss_flags |= SS_ONSTACK;
547 1.1 ws } else
548 1.1 ws fp = (struct sigframe *)tf->fixreg[1];
549 1.1 ws fp = (struct sigframe *)((int)(fp - 1) & ~0xf);
550 1.1 ws
551 1.1 ws frame.sf_code = code;
552 1.1 ws
553 1.1 ws /*
554 1.1 ws * Generate signal context for SYS_sigreturn.
555 1.1 ws */
556 1.1 ws frame.sf_sc.sc_onstack = oldonstack;
557 1.1 ws frame.sf_sc.sc_mask = mask;
558 1.1 ws bcopy(tf, &frame.sf_sc.sc_frame, sizeof *tf);
559 1.1 ws if (copyout(&frame, fp, sizeof frame) != 0)
560 1.1 ws sigexit(p, SIGILL);
561 1.1 ws
562 1.1 ws tf->fixreg[1] = (int)fp;
563 1.1 ws tf->lr = (int)catcher;
564 1.1 ws tf->fixreg[3] = (int)sig;
565 1.1 ws tf->fixreg[4] = (int)code;
566 1.1 ws tf->fixreg[5] = (int)&frame.sf_sc;
567 1.1 ws tf->srr0 = (int)(((char *)PS_STRINGS)
568 1.1 ws - (p->p_emul->e_esigcode - p->p_emul->e_sigcode));
569 1.1 ws }
570 1.1 ws
571 1.1 ws /*
572 1.1 ws * System call to cleanup state after a signal handler returns.
573 1.1 ws */
574 1.1 ws int
575 1.1 ws sys_sigreturn(p, v, retval)
576 1.1 ws struct proc *p;
577 1.1 ws void *v;
578 1.1 ws register_t *retval;
579 1.1 ws {
580 1.1 ws struct sys_sigreturn_args /* {
581 1.1 ws syscallarg(struct sigcontext *) sigcntxp;
582 1.1 ws } */ *uap = v;
583 1.1 ws struct sigcontext sc;
584 1.1 ws struct trapframe *tf;
585 1.1 ws int error;
586 1.1 ws
587 1.1 ws if (error = copyin(SCARG(uap, sigcntxp), &sc, sizeof sc))
588 1.1 ws return error;
589 1.1 ws tf = trapframe(p);
590 1.1 ws if ((sc.sc_frame.srr1 & PSL_USERSTATIC) != (tf->srr1 & PSL_USERSTATIC))
591 1.1 ws return EINVAL;
592 1.1 ws bcopy(&sc.sc_frame, tf, sizeof *tf);
593 1.1 ws if (sc.sc_onstack & 1)
594 1.1 ws p->p_sigacts->ps_sigstk.ss_flags |= SS_ONSTACK;
595 1.1 ws else
596 1.1 ws p->p_sigacts->ps_sigstk.ss_flags &= ~SS_ONSTACK;
597 1.1 ws p->p_sigmask = sc.sc_mask & ~sigcantmask;
598 1.1 ws return EJUSTRETURN;
599 1.1 ws }
600 1.1 ws
601 1.1 ws /*
602 1.1 ws * Machine dependent system variables.
603 1.1 ws * None for now.
604 1.1 ws */
605 1.1 ws int
606 1.1 ws cpu_sysctl(name, namelen, oldp, oldlenp, newp, newlen, p)
607 1.1 ws int *name;
608 1.1 ws u_int namelen;
609 1.1 ws void *oldp;
610 1.1 ws size_t *oldlenp;
611 1.1 ws void *newp;
612 1.1 ws size_t newlen;
613 1.1 ws struct proc *p;
614 1.1 ws {
615 1.1 ws /* all sysctl names at this level are terminal */
616 1.1 ws if (namelen != 1)
617 1.1 ws return ENOTDIR;
618 1.1 ws switch (name[0]) {
619 1.1 ws default:
620 1.1 ws return EOPNOTSUPP;
621 1.1 ws }
622 1.1 ws }
623 1.1 ws
624 1.1 ws /*
625 1.1 ws * Crash dump handling.
626 1.1 ws */
627 1.1 ws u_long dumpmag = 0x8fca0101; /* magic number */
628 1.1 ws int dumpsize = 0; /* size of dump in pages */
629 1.1 ws long dumplo = -1; /* blocks */
630 1.1 ws
631 1.1 ws void
632 1.1 ws dumpsys()
633 1.1 ws {
634 1.3 christos printf("dumpsys: TBD\n");
635 1.1 ws }
636 1.1 ws
637 1.1 ws int cpl;
638 1.1 ws int clockpending, softclockpending, softnetpending;
639 1.1 ws
640 1.1 ws /*
641 1.1 ws * Soft networking interrupts.
642 1.1 ws */
643 1.1 ws void
644 1.1 ws softnet()
645 1.1 ws {
646 1.1 ws int isr = netisr;
647 1.1 ws
648 1.1 ws netisr = 0;
649 1.1 ws #ifdef INET
650 1.1 ws #include "ether.h"
651 1.1 ws #if NETHER > 0
652 1.1 ws if (isr & (1 << NETISR_ARP))
653 1.1 ws arpintr();
654 1.1 ws #endif
655 1.1 ws if (isr & (1 << NETISR_IP))
656 1.1 ws ipintr();
657 1.1 ws #endif
658 1.1 ws #ifdef IMP
659 1.1 ws if (isr & (1 << NETISR_IMP))
660 1.1 ws impintr();
661 1.1 ws #endif
662 1.1 ws #ifdef NS
663 1.1 ws if (isr & (1 << NETISR_NS))
664 1.1 ws nsintr();
665 1.1 ws #endif
666 1.1 ws #ifdef ISO
667 1.1 ws if (isr & (1 << NETISR_ISO))
668 1.1 ws clnlintr();
669 1.1 ws #endif
670 1.1 ws #ifdef CCITT
671 1.1 ws if (isr & (1 << NETISR_CCITT))
672 1.1 ws ccittintr();
673 1.1 ws #endif
674 1.1 ws #include "ppp.h"
675 1.1 ws #if NPPP > 0
676 1.1 ws if (isr & (1 << NETISR_PPP))
677 1.1 ws pppintr();
678 1.1 ws #endif
679 1.1 ws }
680 1.1 ws
681 1.1 ws /*
682 1.1 ws * Stray interrupts.
683 1.1 ws */
684 1.1 ws void
685 1.1 ws strayintr(irq)
686 1.1 ws int irq;
687 1.1 ws {
688 1.1 ws log(LOG_ERR, "stray interrupt %d\n", irq);
689 1.1 ws }
690 1.1 ws
691 1.1 ws int
692 1.1 ws splraise(bits)
693 1.1 ws int bits;
694 1.1 ws {
695 1.1 ws int old;
696 1.1 ws
697 1.1 ws old = cpl;
698 1.1 ws cpl |= bits;
699 1.1 ws
700 1.1 ws if ((bits & SPLMACHINE) & ~old)
701 1.1 ws (*machine_interface.splx)(cpl & SPLMACHINE);
702 1.1 ws
703 1.1 ws return old;
704 1.1 ws }
705 1.1 ws
706 1.1 ws int
707 1.1 ws splx(new)
708 1.1 ws int new;
709 1.1 ws {
710 1.1 ws int pending, old = cpl;
711 1.1 ws int emsr, dmsr;
712 1.1 ws
713 1.1 ws asm ("mfmsr %0" : "=r"(emsr));
714 1.1 ws dmsr = emsr & ~PSL_EE;
715 1.1 ws
716 1.1 ws cpl = new;
717 1.1 ws
718 1.1 ws if ((new & SPLMACHINE) != (old & SPLMACHINE))
719 1.1 ws (*machine_interface.splx)(new & SPLMACHINE);
720 1.1 ws
721 1.1 ws while (1) {
722 1.1 ws cpl = new;
723 1.1 ws
724 1.1 ws asm volatile ("mtmsr %0" :: "r"(dmsr));
725 1.1 ws if (clockpending && !(cpl & SPLCLOCK)) {
726 1.1 ws struct clockframe frame;
727 1.1 ws extern int intr_depth;
728 1.1 ws
729 1.1 ws cpl |= SPLCLOCK;
730 1.1 ws clockpending--;
731 1.1 ws asm volatile ("mtmsr %0" :: "r"(emsr));
732 1.1 ws
733 1.1 ws /*
734 1.1 ws * Fake a clock interrupt frame
735 1.1 ws */
736 1.1 ws frame.pri = new;
737 1.1 ws frame.depth = intr_depth + 1;
738 1.1 ws frame.srr1 = 0;
739 1.1 ws frame.srr0 = (int)splx;
740 1.1 ws /*
741 1.1 ws * Do standard timer interrupt stuff
742 1.1 ws */
743 1.1 ws hardclock(&frame);
744 1.1 ws continue;
745 1.1 ws }
746 1.1 ws if (softclockpending && !(cpl & SPLSOFTCLOCK)) {
747 1.1 ws
748 1.1 ws cpl |= SPLSOFTCLOCK;
749 1.1 ws softclockpending = 0;
750 1.1 ws asm volatile ("mtmsr %0" :: "r"(emsr));
751 1.1 ws
752 1.1 ws softclock();
753 1.1 ws continue;
754 1.1 ws }
755 1.1 ws if (softnetpending && !(cpl & SPLSOFTNET)) {
756 1.1 ws cpl |= SPLSOFTNET;
757 1.1 ws softnetpending = 0;
758 1.1 ws asm volatile ("mtmsr %0" :: "r"(emsr));
759 1.1 ws softnet();
760 1.1 ws continue;
761 1.1 ws }
762 1.1 ws
763 1.1 ws asm volatile ("mtmsr %0" :: "r"(emsr));
764 1.1 ws
765 1.1 ws return old;
766 1.1 ws }
767 1.1 ws }
768 1.1 ws
769 1.1 ws /*
770 1.1 ws * This one is similar to the above, but returns with interrupts disabled.
771 1.1 ws * It is intended for use during interrupt exit (as the name implies :-)).
772 1.1 ws */
773 1.1 ws void
774 1.1 ws intr_return(level)
775 1.1 ws int level;
776 1.1 ws {
777 1.1 ws int pending, old = cpl;
778 1.1 ws int emsr, dmsr;
779 1.1 ws
780 1.1 ws asm ("mfmsr %0" : "=r"(emsr));
781 1.1 ws dmsr = emsr & ~PSL_EE;
782 1.1 ws
783 1.1 ws cpl = level;
784 1.1 ws
785 1.1 ws if ((level & SPLMACHINE) != (old & SPLMACHINE))
786 1.1 ws (*machine_interface.splx)(level & SPLMACHINE);
787 1.1 ws
788 1.1 ws while (1) {
789 1.1 ws cpl = level;
790 1.1 ws
791 1.1 ws asm volatile ("mtmsr %0" :: "r"(dmsr));
792 1.1 ws if (clockpending && !(cpl & SPLCLOCK)) {
793 1.1 ws struct clockframe frame;
794 1.1 ws extern int intr_depth;
795 1.1 ws
796 1.1 ws cpl |= SPLCLOCK;
797 1.1 ws clockpending--;
798 1.1 ws asm volatile ("mtmsr %0" :: "r"(emsr));
799 1.1 ws
800 1.1 ws /*
801 1.1 ws * Fake a clock interrupt frame
802 1.1 ws */
803 1.1 ws frame.pri = level | (clockpending ? SPLSOFTCLOCK : 0);
804 1.1 ws frame.depth = intr_depth + 1;
805 1.1 ws frame.srr1 = 0;
806 1.1 ws frame.srr0 = (int)splx;
807 1.1 ws /*
808 1.1 ws * Do standard timer interrupt stuff
809 1.1 ws */
810 1.1 ws hardclock(&frame);
811 1.1 ws continue;
812 1.1 ws }
813 1.1 ws if (softclockpending && !(cpl & SPLSOFTCLOCK)) {
814 1.1 ws
815 1.1 ws cpl |= SPLSOFTCLOCK;
816 1.1 ws softclockpending = 0;
817 1.1 ws asm volatile ("mtmsr %0" :: "r"(emsr));
818 1.1 ws
819 1.1 ws softclock();
820 1.1 ws continue;
821 1.1 ws }
822 1.1 ws if (softnetpending && !(cpl & SPLSOFTNET)) {
823 1.1 ws cpl |= SPLSOFTNET;
824 1.1 ws softnetpending = 0;
825 1.1 ws asm volatile ("mtmsr %0" :: "r"(emsr));
826 1.1 ws softnet();
827 1.1 ws continue;
828 1.1 ws }
829 1.1 ws break;
830 1.1 ws }
831 1.1 ws }
832 1.1 ws
833 1.1 ws /*
834 1.1 ws * Halt or reboot the machine after syncing/dumping according to howto.
835 1.1 ws */
836 1.1 ws void
837 1.5 gwr cpu_reboot(howto, what)
838 1.1 ws int howto;
839 1.1 ws char *what;
840 1.1 ws {
841 1.1 ws static int syncing;
842 1.1 ws static char str[256];
843 1.1 ws char *ap = str, *ap1 = ap;
844 1.1 ws
845 1.1 ws boothowto = howto;
846 1.1 ws if (!cold && !(howto & RB_NOSYNC) && !syncing) {
847 1.1 ws syncing = 1;
848 1.1 ws vfs_shutdown(); /* sync */
849 1.1 ws resettodr(); /* set wall clock */
850 1.1 ws }
851 1.1 ws splhigh();
852 1.1 ws if (howto & RB_HALT) {
853 1.1 ws doshutdownhooks();
854 1.3 christos printf("halted\n\n");
855 1.1 ws ppc_exit();
856 1.1 ws }
857 1.1 ws if (!cold && (howto & RB_DUMP))
858 1.1 ws dumpsys();
859 1.1 ws doshutdownhooks();
860 1.3 christos printf("rebooting\n\n");
861 1.1 ws if (what && *what) {
862 1.1 ws if (strlen(what) > sizeof str - 5)
863 1.3 christos printf("boot string too large, ignored\n");
864 1.1 ws else {
865 1.1 ws strcpy(str, what);
866 1.1 ws ap1 = ap = str + strlen(str);
867 1.1 ws *ap++ = ' ';
868 1.1 ws }
869 1.1 ws }
870 1.1 ws *ap++ = '-';
871 1.1 ws if (howto & RB_SINGLE)
872 1.1 ws *ap++ = 's';
873 1.1 ws if (howto & RB_KDB)
874 1.1 ws *ap++ = 'd';
875 1.1 ws *ap++ = 0;
876 1.1 ws if (ap[-2] == '-')
877 1.1 ws *ap1 = 0;
878 1.1 ws ppc_boot(str);
879 1.1 ws }
880 1.1 ws
881 1.1 ws /*
882 1.1 ws * OpenFirmware callback routine
883 1.1 ws */
884 1.1 ws void
885 1.1 ws callback(p)
886 1.1 ws void *p;
887 1.1 ws {
888 1.1 ws panic("callback"); /* for now XXX */
889 1.1 ws }
890 1.1 ws
891 1.1 ws /*
892 1.1 ws * Fake routines for spl/interrupt handling before autoconfig
893 1.1 ws */
894 1.1 ws static void
895 1.1 ws fake_splx(new)
896 1.1 ws int new;
897 1.1 ws {
898 1.1 ws }
899 1.1 ws
900 1.1 ws static void
901 1.1 ws fake_irq_establish(irq, level, handler, arg)
902 1.1 ws int irq, level;
903 1.1 ws void (*handler) __P((void *));
904 1.1 ws void *arg;
905 1.1 ws {
906 1.1 ws panic("fake_irq_establish");
907 1.1 ws }
908