machdep.c revision 1.2 1 1.2 gwr /* $NetBSD: machdep.c,v 1.2 1997/01/16 22:08:31 gwr Exp $ */
2 1.1 gwr
3 1.1 gwr /*
4 1.1 gwr * Copyright (c) 1988 University of Utah.
5 1.1 gwr * Copyright (c) 1982, 1986, 1990, 1993
6 1.1 gwr * The Regents of the University of California. All rights reserved.
7 1.1 gwr *
8 1.1 gwr * This code is derived from software contributed to Berkeley by
9 1.1 gwr * the Systems Programming Group of the University of Utah Computer
10 1.1 gwr * Science Department.
11 1.1 gwr *
12 1.1 gwr * Redistribution and use in source and binary forms, with or without
13 1.1 gwr * modification, are permitted provided that the following conditions
14 1.1 gwr * are met:
15 1.1 gwr * 1. Redistributions of source code must retain the above copyright
16 1.1 gwr * notice, this list of conditions and the following disclaimer.
17 1.1 gwr * 2. Redistributions in binary form must reproduce the above copyright
18 1.1 gwr * notice, this list of conditions and the following disclaimer in the
19 1.1 gwr * documentation and/or other materials provided with the distribution.
20 1.1 gwr * 3. All advertising materials mentioning features or use of this software
21 1.1 gwr * must display the following acknowledgement:
22 1.1 gwr * This product includes software developed by the University of
23 1.1 gwr * California, Berkeley and its contributors.
24 1.1 gwr * 4. Neither the name of the University nor the names of its contributors
25 1.1 gwr * may be used to endorse or promote products derived from this software
26 1.1 gwr * without specific prior written permission.
27 1.1 gwr *
28 1.1 gwr * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29 1.1 gwr * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 1.1 gwr * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 1.1 gwr * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32 1.1 gwr * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 1.1 gwr * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34 1.1 gwr * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35 1.1 gwr * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36 1.1 gwr * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37 1.1 gwr * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 1.1 gwr * SUCH DAMAGE.
39 1.1 gwr *
40 1.1 gwr * from: Utah Hdr: machdep.c 1.74 92/12/20
41 1.1 gwr * from: @(#)machdep.c 8.10 (Berkeley) 4/20/94
42 1.1 gwr */
43 1.1 gwr
44 1.1 gwr #include <sys/param.h>
45 1.1 gwr #include <sys/systm.h>
46 1.1 gwr #include <sys/signalvar.h>
47 1.1 gwr #include <sys/kernel.h>
48 1.1 gwr #include <sys/map.h>
49 1.1 gwr #include <sys/proc.h>
50 1.1 gwr #include <sys/buf.h>
51 1.1 gwr #include <sys/reboot.h>
52 1.1 gwr #include <sys/conf.h>
53 1.1 gwr #include <sys/file.h>
54 1.1 gwr #include <sys/clist.h>
55 1.1 gwr #include <sys/callout.h>
56 1.1 gwr #include <sys/malloc.h>
57 1.1 gwr #include <sys/mbuf.h>
58 1.1 gwr #include <sys/msgbuf.h>
59 1.1 gwr #include <sys/ioctl.h>
60 1.1 gwr #include <sys/tty.h>
61 1.1 gwr #include <sys/mount.h>
62 1.1 gwr #include <sys/user.h>
63 1.1 gwr #include <sys/exec.h>
64 1.1 gwr #include <sys/core.h>
65 1.1 gwr #include <sys/kcore.h>
66 1.1 gwr #include <sys/vnode.h>
67 1.1 gwr #include <sys/sysctl.h>
68 1.1 gwr #include <sys/syscallargs.h>
69 1.1 gwr #ifdef SYSVMSG
70 1.1 gwr #include <sys/msg.h>
71 1.1 gwr #endif
72 1.1 gwr #ifdef SYSVSEM
73 1.1 gwr #include <sys/sem.h>
74 1.1 gwr #endif
75 1.1 gwr #ifdef SYSVSHM
76 1.1 gwr #include <sys/shm.h>
77 1.1 gwr #endif
78 1.1 gwr
79 1.1 gwr #include <vm/vm.h>
80 1.1 gwr #include <vm/vm_map.h>
81 1.1 gwr #include <vm/vm_kern.h>
82 1.1 gwr #include <vm/vm_page.h>
83 1.1 gwr
84 1.1 gwr #include <dev/cons.h>
85 1.1 gwr
86 1.1 gwr #include <machine/cpu.h>
87 1.1 gwr #include <machine/reg.h>
88 1.1 gwr #include <machine/psl.h>
89 1.1 gwr #include <machine/pte.h>
90 1.1 gwr #include <machine/mon.h>
91 1.1 gwr #include <machine/dvma.h>
92 1.1 gwr #include <machine/db_machdep.h>
93 1.1 gwr
94 1.1 gwr #include "machdep.h"
95 1.1 gwr
96 1.1 gwr extern char *cpu_string;
97 1.1 gwr extern char version[];
98 1.1 gwr extern short exframesize[];
99 1.1 gwr
100 1.1 gwr /* Defined in locore.s */
101 1.1 gwr extern char kernel_text[];
102 1.1 gwr /* Defined by the linker */
103 1.1 gwr extern char etext[];
104 1.1 gwr
105 1.1 gwr int physmem;
106 1.1 gwr int fpu_type;
107 1.1 gwr int msgbufmapped;
108 1.1 gwr
109 1.1 gwr vm_offset_t vmmap;
110 1.1 gwr
111 1.1 gwr /*
112 1.1 gwr * safepri is a safe priority for sleep to set for a spin-wait
113 1.1 gwr * during autoconfiguration or after a panic.
114 1.1 gwr */
115 1.1 gwr int safepri = PSL_LOWIPL;
116 1.1 gwr
117 1.1 gwr /*
118 1.1 gwr * Declare these as initialized data so we can patch them.
119 1.1 gwr */
120 1.1 gwr int nswbuf = 0;
121 1.1 gwr #ifdef NBUF
122 1.1 gwr int nbuf = NBUF;
123 1.1 gwr #else
124 1.1 gwr int nbuf = 0;
125 1.1 gwr #endif
126 1.1 gwr #ifdef BUFPAGES
127 1.1 gwr int bufpages = BUFPAGES;
128 1.1 gwr #else
129 1.1 gwr int bufpages = 0;
130 1.1 gwr #endif
131 1.1 gwr label_t *nofault;
132 1.1 gwr
133 1.1 gwr static void identifycpu __P((void));
134 1.1 gwr static void initcpu __P((void));
135 1.1 gwr
136 1.1 gwr /*
137 1.1 gwr * Console initialization: called early on from main,
138 1.1 gwr * before vm init or startup. Do enough configuration
139 1.1 gwr * to choose and initialize a console.
140 1.1 gwr */
141 1.1 gwr void consinit()
142 1.1 gwr {
143 1.1 gwr cninit();
144 1.1 gwr
145 1.1 gwr #ifdef KGDB
146 1.1 gwr /* XXX - Ask on console for kgdb_dev? */
147 1.1 gwr zs_kgdb_init(); /* XXX */
148 1.1 gwr /* Note: kgdb_connect() will just return if kgdb_dev<0 */
149 1.1 gwr if (boothowto & RB_KDB)
150 1.1 gwr kgdb_connect(1);
151 1.1 gwr #endif
152 1.1 gwr #ifdef DDB
153 1.1 gwr /* Now that we have a console, we can stop in DDB. */
154 1.1 gwr db_machine_init();
155 1.1 gwr ddb_init();
156 1.1 gwr if (boothowto & RB_KDB)
157 1.1 gwr Debugger();
158 1.1 gwr #endif DDB
159 1.1 gwr }
160 1.1 gwr
161 1.1 gwr /*
162 1.1 gwr * allocsys() - Private routine used by cpu_startup() below.
163 1.1 gwr *
164 1.1 gwr * Allocate space for system data structures. We are given
165 1.1 gwr * a starting virtual address and we return a final virtual
166 1.1 gwr * address; along the way we set each data structure pointer.
167 1.1 gwr *
168 1.1 gwr * We call allocsys() with 0 to find out how much space we want,
169 1.1 gwr * allocate that much and fill it with zeroes, and then call
170 1.1 gwr * allocsys() again with the correct base virtual address.
171 1.1 gwr */
172 1.1 gwr #define valloc(name, type, num) \
173 1.1 gwr v = (caddr_t)(((name) = (type *)v) + (num))
174 1.1 gwr static caddr_t allocsys __P((caddr_t));
175 1.1 gwr static caddr_t
176 1.1 gwr allocsys(v)
177 1.1 gwr register caddr_t v;
178 1.1 gwr {
179 1.1 gwr
180 1.1 gwr #ifdef REAL_CLISTS
181 1.1 gwr valloc(cfree, struct cblock, nclist);
182 1.1 gwr #endif
183 1.1 gwr valloc(callout, struct callout, ncallout);
184 1.1 gwr valloc(swapmap, struct map, nswapmap = maxproc * 2);
185 1.1 gwr #ifdef SYSVSHM
186 1.1 gwr valloc(shmsegs, struct shmid_ds, shminfo.shmmni);
187 1.1 gwr #endif
188 1.1 gwr #ifdef SYSVSEM
189 1.1 gwr valloc(sema, struct semid_ds, seminfo.semmni);
190 1.1 gwr valloc(sem, struct sem, seminfo.semmns);
191 1.1 gwr /* This is pretty disgusting! */
192 1.1 gwr valloc(semu, int, (seminfo.semmnu * seminfo.semusz) / sizeof(int));
193 1.1 gwr #endif
194 1.1 gwr #ifdef SYSVMSG
195 1.1 gwr valloc(msgpool, char, msginfo.msgmax);
196 1.1 gwr valloc(msgmaps, struct msgmap, msginfo.msgseg);
197 1.1 gwr valloc(msghdrs, struct msg, msginfo.msgtql);
198 1.1 gwr valloc(msqids, struct msqid_ds, msginfo.msgmni);
199 1.1 gwr #endif
200 1.1 gwr
201 1.1 gwr /*
202 1.1 gwr * Determine how many buffers to allocate. We allocate
203 1.1 gwr * the BSD standard of use 10% of memory for the first 2 Meg,
204 1.1 gwr * 5% of remaining. Insure a minimum of 16 buffers.
205 1.1 gwr * Allocate 1/2 as many swap buffer headers as file i/o buffers.
206 1.1 gwr */
207 1.1 gwr if (bufpages == 0) {
208 1.1 gwr /* We always have more than 2MB of memory. */
209 1.1 gwr bufpages = ((btoc(2 * 1024 * 1024) + physmem) /
210 1.1 gwr (20 * CLSIZE));
211 1.1 gwr }
212 1.1 gwr if (nbuf == 0) {
213 1.1 gwr nbuf = bufpages;
214 1.1 gwr if (nbuf < 16)
215 1.1 gwr nbuf = 16;
216 1.1 gwr }
217 1.1 gwr if (nswbuf == 0) {
218 1.1 gwr nswbuf = (nbuf / 2) &~ 1; /* force even */
219 1.1 gwr if (nswbuf > 256)
220 1.1 gwr nswbuf = 256; /* sanity */
221 1.1 gwr }
222 1.1 gwr valloc(swbuf, struct buf, nswbuf);
223 1.1 gwr valloc(buf, struct buf, nbuf);
224 1.1 gwr return v;
225 1.1 gwr }
226 1.1 gwr #undef valloc
227 1.1 gwr
228 1.1 gwr /*
229 1.1 gwr * cpu_startup: allocate memory for variable-sized tables,
230 1.1 gwr * initialize cpu, and do autoconfiguration.
231 1.1 gwr *
232 1.1 gwr * This is called early in init_main.c:main(), after the
233 1.1 gwr * kernel memory allocator is ready for use, but before
234 1.1 gwr * the creation of processes 1,2, and mountroot, etc.
235 1.1 gwr */
236 1.1 gwr void
237 1.1 gwr cpu_startup()
238 1.1 gwr {
239 1.1 gwr caddr_t v;
240 1.1 gwr int sz, i;
241 1.1 gwr vm_size_t size;
242 1.1 gwr int base, residual;
243 1.1 gwr vm_offset_t minaddr, maxaddr;
244 1.1 gwr
245 1.1 gwr /*
246 1.1 gwr * Initialize message buffer (for kernel printf).
247 1.1 gwr * This is put in physical page zero so it will
248 1.1 gwr * always be in the same place after a reboot.
249 1.1 gwr * Its mapping was prepared in pmap_bootstrap().
250 1.1 gwr * Also, offset some to avoid PROM scribbles.
251 1.1 gwr */
252 1.1 gwr v = (caddr_t) KERNBASE;
253 1.1 gwr msgbufp = (struct msgbuf *)(v + 0x1000);
254 1.1 gwr msgbufmapped = 1;
255 1.1 gwr
256 1.1 gwr /*
257 1.1 gwr * Good {morning,afternoon,evening,night}.
258 1.1 gwr */
259 1.1 gwr printf(version);
260 1.1 gwr identifycpu();
261 1.1 gwr initfpu(); /* also prints FPU type */
262 1.1 gwr
263 1.1 gwr printf("real mem = %d\n", ctob(physmem));
264 1.1 gwr
265 1.1 gwr /*
266 1.1 gwr * Find out how much space we need, allocate it,
267 1.1 gwr * and then give everything true virtual addresses.
268 1.1 gwr */
269 1.1 gwr sz = (int)allocsys((caddr_t)0);
270 1.1 gwr if ((v = (caddr_t)kmem_alloc(kernel_map, round_page(sz))) == 0)
271 1.1 gwr panic("startup: no room for tables");
272 1.1 gwr if (allocsys(v) - v != sz)
273 1.1 gwr panic("startup: table size inconsistency");
274 1.1 gwr
275 1.1 gwr /*
276 1.1 gwr * Now allocate buffers proper. They are different than the above
277 1.1 gwr * in that they usually occupy more virtual memory than physical.
278 1.1 gwr */
279 1.1 gwr size = MAXBSIZE * nbuf;
280 1.1 gwr buffer_map = kmem_suballoc(kernel_map, (vm_offset_t *)&buffers,
281 1.1 gwr &maxaddr, size, TRUE);
282 1.1 gwr minaddr = (vm_offset_t)buffers;
283 1.1 gwr if (vm_map_find(buffer_map, vm_object_allocate(size), (vm_offset_t)0,
284 1.1 gwr &minaddr, size, FALSE) != KERN_SUCCESS)
285 1.1 gwr panic("startup: cannot allocate buffers");
286 1.1 gwr if ((bufpages / nbuf) >= btoc(MAXBSIZE)) {
287 1.1 gwr /* don't want to alloc more physical mem than needed */
288 1.1 gwr bufpages = btoc(MAXBSIZE) * nbuf;
289 1.1 gwr }
290 1.1 gwr base = bufpages / nbuf;
291 1.1 gwr residual = bufpages % nbuf;
292 1.1 gwr for (i = 0; i < nbuf; i++) {
293 1.1 gwr vm_size_t curbufsize;
294 1.1 gwr vm_offset_t curbuf;
295 1.1 gwr
296 1.1 gwr /*
297 1.1 gwr * First <residual> buffers get (base+1) physical pages
298 1.1 gwr * allocated for them. The rest get (base) physical pages.
299 1.1 gwr *
300 1.1 gwr * The rest of each buffer occupies virtual space,
301 1.1 gwr * but has no physical memory allocated for it.
302 1.1 gwr */
303 1.1 gwr curbuf = (vm_offset_t)buffers + i * MAXBSIZE;
304 1.1 gwr curbufsize = CLBYTES * (i < residual ? base+1 : base);
305 1.1 gwr vm_map_pageable(buffer_map, curbuf, curbuf+curbufsize, FALSE);
306 1.1 gwr vm_map_simplify(buffer_map, curbuf);
307 1.1 gwr }
308 1.1 gwr
309 1.1 gwr /*
310 1.1 gwr * Allocate a submap for exec arguments. This map effectively
311 1.1 gwr * limits the number of processes exec'ing at any time.
312 1.1 gwr */
313 1.1 gwr exec_map = kmem_suballoc(kernel_map, &minaddr, &maxaddr,
314 1.1 gwr 16*NCARGS, TRUE);
315 1.1 gwr
316 1.1 gwr /*
317 1.1 gwr * We don't use a submap for physio, and use a separate map
318 1.1 gwr * for DVMA allocations. Our vmapbuf just maps pages into
319 1.1 gwr * the kernel map (any kernel mapping is OK) and then the
320 1.1 gwr * device drivers clone the kernel mappings into DVMA space.
321 1.1 gwr */
322 1.1 gwr
323 1.1 gwr /*
324 1.1 gwr * Finally, allocate mbuf pool. Since mclrefcnt is an off-size
325 1.1 gwr * we use the more space efficient malloc in place of kmem_alloc.
326 1.1 gwr */
327 1.1 gwr mclrefcnt = (char *)malloc(NMBCLUSTERS+CLBYTES/MCLBYTES,
328 1.1 gwr M_MBUF, M_NOWAIT);
329 1.1 gwr bzero(mclrefcnt, NMBCLUSTERS+CLBYTES/MCLBYTES);
330 1.1 gwr mb_map = kmem_suballoc(kernel_map, (vm_offset_t *)&mbutl, &maxaddr,
331 1.1 gwr VM_MBUF_SIZE, FALSE);
332 1.1 gwr
333 1.1 gwr /*
334 1.1 gwr * Initialize callouts
335 1.1 gwr */
336 1.1 gwr callfree = callout;
337 1.1 gwr for (i = 1; i < ncallout; i++)
338 1.1 gwr callout[i-1].c_next = &callout[i];
339 1.1 gwr callout[i-1].c_next = NULL;
340 1.1 gwr
341 1.1 gwr printf("avail mem = %d\n", (int) ptoa(cnt.v_free_count));
342 1.1 gwr printf("using %d buffers containing %d bytes of memory\n",
343 1.1 gwr nbuf, bufpages * CLBYTES);
344 1.1 gwr
345 1.1 gwr /*
346 1.1 gwr * Tell the VM system that writing to kernel text isn't allowed.
347 1.1 gwr * If we don't, we might end up COW'ing the text segment!
348 1.1 gwr */
349 1.1 gwr if (vm_map_protect(kernel_map, (vm_offset_t) kernel_text,
350 1.1 gwr sun3x_trunc_page((vm_offset_t) etext),
351 1.1 gwr VM_PROT_READ|VM_PROT_EXECUTE, TRUE)
352 1.1 gwr != KERN_SUCCESS)
353 1.1 gwr panic("can't protect kernel text");
354 1.1 gwr
355 1.1 gwr /*
356 1.1 gwr * Allocate a virtual page (for use by /dev/mem)
357 1.1 gwr * This page is handed to pmap_enter() therefore
358 1.1 gwr * it has to be in the normal kernel VA range.
359 1.1 gwr */
360 1.1 gwr vmmap = kmem_alloc_wait(kernel_map, NBPG);
361 1.1 gwr
362 1.1 gwr /*
363 1.1 gwr * Create the DVMA maps.
364 1.1 gwr */
365 1.1 gwr dvma_init();
366 1.1 gwr
367 1.1 gwr /*
368 1.1 gwr * Set up CPU-specific registers, cache, etc.
369 1.1 gwr */
370 1.1 gwr initcpu();
371 1.1 gwr
372 1.1 gwr /*
373 1.1 gwr * Set up buffers, so they can be used to read disk labels.
374 1.1 gwr */
375 1.1 gwr bufinit();
376 1.1 gwr
377 1.1 gwr /*
378 1.1 gwr * Configure the system.
379 1.1 gwr */
380 1.1 gwr configure();
381 1.1 gwr }
382 1.1 gwr
383 1.1 gwr /*
384 1.1 gwr * Set registers on exec.
385 1.1 gwr * XXX Should clear registers except sp, pc,
386 1.1 gwr * but would break init; should be fixed soon.
387 1.1 gwr */
388 1.1 gwr void
389 1.1 gwr setregs(p, pack, stack, retval)
390 1.1 gwr register struct proc *p;
391 1.1 gwr struct exec_package *pack;
392 1.1 gwr u_long stack;
393 1.1 gwr register_t *retval;
394 1.1 gwr {
395 1.1 gwr struct frame *frame = (struct frame *)p->p_md.md_regs;
396 1.1 gwr
397 1.1 gwr frame->f_pc = pack->ep_entry & ~1;
398 1.1 gwr frame->f_regs[SP] = stack;
399 1.1 gwr frame->f_regs[A2] = (int)PS_STRINGS;
400 1.1 gwr
401 1.1 gwr /* restore a null state frame */
402 1.1 gwr p->p_addr->u_pcb.pcb_fpregs.fpf_null = 0;
403 1.1 gwr if (fpu_type) {
404 1.1 gwr m68881_restore(&p->p_addr->u_pcb.pcb_fpregs);
405 1.1 gwr }
406 1.1 gwr p->p_md.md_flags = 0;
407 1.1 gwr /* XXX - HPUX sigcode hack would go here... */
408 1.1 gwr }
409 1.1 gwr
410 1.1 gwr /*
411 1.1 gwr * Info for CTL_HW
412 1.1 gwr */
413 1.1 gwr char machine[] = "sun3x"; /* cpu "architecture" */
414 1.1 gwr char cpu_model[120];
415 1.1 gwr extern long hostid;
416 1.1 gwr
417 1.1 gwr void
418 1.1 gwr identifycpu()
419 1.1 gwr {
420 1.1 gwr /*
421 1.1 gwr * actual identification done earlier because i felt like it,
422 1.1 gwr * and i believe i will need the info to deal with some VAC, and awful
423 1.1 gwr * framebuffer placement problems. could be moved later.
424 1.1 gwr */
425 1.1 gwr strcpy(cpu_model, "Sun 3/");
426 1.1 gwr
427 1.1 gwr /* should eventually include whether it has a VAC, mc6888x version, etc */
428 1.1 gwr strcat(cpu_model, cpu_string);
429 1.1 gwr
430 1.1 gwr printf("Model: %s (hostid %x)\n", cpu_model, (int) hostid);
431 1.1 gwr }
432 1.1 gwr
433 1.1 gwr /*
434 1.1 gwr * machine dependent system variables.
435 1.1 gwr */
436 1.1 gwr int
437 1.1 gwr cpu_sysctl(name, namelen, oldp, oldlenp, newp, newlen, p)
438 1.1 gwr int *name;
439 1.1 gwr u_int namelen;
440 1.1 gwr void *oldp;
441 1.1 gwr size_t *oldlenp;
442 1.1 gwr void *newp;
443 1.1 gwr size_t newlen;
444 1.1 gwr struct proc *p;
445 1.1 gwr {
446 1.1 gwr int error;
447 1.1 gwr dev_t consdev;
448 1.1 gwr
449 1.1 gwr /* all sysctl names at this level are terminal */
450 1.1 gwr if (namelen != 1)
451 1.1 gwr return (ENOTDIR); /* overloaded */
452 1.1 gwr
453 1.1 gwr switch (name[0]) {
454 1.1 gwr case CPU_CONSDEV:
455 1.1 gwr if (cn_tab != NULL)
456 1.1 gwr consdev = cn_tab->cn_dev;
457 1.1 gwr else
458 1.1 gwr consdev = NODEV;
459 1.1 gwr error = sysctl_rdstruct(oldp, oldlenp, newp,
460 1.1 gwr &consdev, sizeof consdev);
461 1.1 gwr break;
462 1.1 gwr
463 1.1 gwr #if 0 /* XXX - Not yet... */
464 1.1 gwr case CPU_ROOT_DEVICE:
465 1.1 gwr error = sysctl_rdstring(oldp, oldlenp, newp, root_device);
466 1.1 gwr break;
467 1.1 gwr
468 1.1 gwr case CPU_BOOTED_KERNEL:
469 1.1 gwr error = sysctl_rdstring(oldp, oldlenp, newp, booted_kernel);
470 1.1 gwr break;
471 1.1 gwr #endif
472 1.1 gwr
473 1.1 gwr default:
474 1.1 gwr error = EOPNOTSUPP;
475 1.1 gwr }
476 1.1 gwr return (error);
477 1.1 gwr }
478 1.1 gwr
479 1.1 gwr #define SS_RTEFRAME 1
480 1.1 gwr #define SS_FPSTATE 2
481 1.1 gwr #define SS_USERREGS 4
482 1.1 gwr
483 1.1 gwr struct sigstate {
484 1.1 gwr int ss_flags; /* which of the following are valid */
485 1.1 gwr struct frame ss_frame; /* original exception frame */
486 1.1 gwr struct fpframe ss_fpstate; /* 68881/68882 state info */
487 1.1 gwr };
488 1.1 gwr
489 1.1 gwr /*
490 1.1 gwr * WARNING: code in locore.s assumes the layout shown for sf_signum
491 1.1 gwr * thru sf_handler so... don't screw with them!
492 1.1 gwr */
493 1.1 gwr struct sigframe {
494 1.1 gwr int sf_signum; /* signo for handler */
495 1.1 gwr int sf_code; /* additional info for handler */
496 1.1 gwr struct sigcontext *sf_scp; /* context ptr for handler */
497 1.1 gwr sig_t sf_handler; /* handler addr for u_sigc */
498 1.1 gwr struct sigstate sf_state; /* state of the hardware */
499 1.1 gwr struct sigcontext sf_sc; /* actual context */
500 1.1 gwr };
501 1.1 gwr
502 1.1 gwr #ifdef DEBUG
503 1.1 gwr int sigdebug = 0;
504 1.1 gwr int sigpid = 0;
505 1.1 gwr #define SDB_FOLLOW 0x01
506 1.1 gwr #define SDB_KSTACK 0x02
507 1.1 gwr #define SDB_FPSTATE 0x04
508 1.1 gwr #endif
509 1.1 gwr
510 1.1 gwr /*
511 1.1 gwr * Send an interrupt to process.
512 1.1 gwr */
513 1.1 gwr void
514 1.1 gwr sendsig(catcher, sig, mask, code)
515 1.1 gwr sig_t catcher;
516 1.1 gwr int sig, mask;
517 1.1 gwr u_long code;
518 1.1 gwr {
519 1.1 gwr register struct proc *p = curproc;
520 1.1 gwr register struct sigframe *fp, *kfp;
521 1.1 gwr register struct frame *frame;
522 1.1 gwr register struct sigacts *psp = p->p_sigacts;
523 1.1 gwr register short ft;
524 1.1 gwr int oonstack, fsize;
525 1.1 gwr extern char sigcode[], esigcode[];
526 1.1 gwr
527 1.1 gwr frame = (struct frame *)p->p_md.md_regs;
528 1.1 gwr ft = frame->f_format;
529 1.1 gwr oonstack = psp->ps_sigstk.ss_flags & SS_ONSTACK;
530 1.1 gwr
531 1.1 gwr /*
532 1.1 gwr * Allocate and validate space for the signal handler
533 1.1 gwr * context. Note that if the stack is in P0 space, the
534 1.1 gwr * call to grow() is a nop, and the useracc() check
535 1.1 gwr * will fail if the process has not already allocated
536 1.1 gwr * the space with a `brk'.
537 1.1 gwr */
538 1.1 gwr fsize = sizeof(struct sigframe);
539 1.1 gwr if ((psp->ps_flags & SAS_ALTSTACK) && !oonstack &&
540 1.1 gwr (psp->ps_sigonstack & sigmask(sig))) {
541 1.1 gwr fp = (struct sigframe *)(psp->ps_sigstk.ss_sp +
542 1.1 gwr psp->ps_sigstk.ss_size - fsize);
543 1.1 gwr psp->ps_sigstk.ss_flags |= SS_ONSTACK;
544 1.1 gwr } else
545 1.1 gwr fp = (struct sigframe *)(frame->f_regs[SP] - fsize);
546 1.1 gwr if ((unsigned)fp <= USRSTACK - ctob(p->p_vmspace->vm_ssize))
547 1.1 gwr (void)grow(p, (unsigned)fp);
548 1.1 gwr #ifdef DEBUG
549 1.1 gwr if ((sigdebug & SDB_KSTACK) && p->p_pid == sigpid)
550 1.1 gwr printf("sendsig(%d): sig %d ssp %x usp %x scp %x ft %d\n",
551 1.1 gwr p->p_pid, sig, &oonstack, fp, &fp->sf_sc, ft);
552 1.1 gwr #endif
553 1.1 gwr if (useracc((caddr_t)fp, fsize, B_WRITE) == 0) {
554 1.1 gwr #ifdef DEBUG
555 1.1 gwr if ((sigdebug & SDB_KSTACK) && p->p_pid == sigpid)
556 1.1 gwr printf("sendsig(%d): useracc failed on sig %d\n",
557 1.1 gwr p->p_pid, sig);
558 1.1 gwr #endif
559 1.1 gwr /*
560 1.1 gwr * Process has trashed its stack; give it an illegal
561 1.1 gwr * instruction to halt it in its tracks.
562 1.1 gwr */
563 1.1 gwr SIGACTION(p, SIGILL) = SIG_DFL;
564 1.1 gwr sig = sigmask(SIGILL);
565 1.1 gwr p->p_sigignore &= ~sig;
566 1.1 gwr p->p_sigcatch &= ~sig;
567 1.1 gwr p->p_sigmask &= ~sig;
568 1.1 gwr psignal(p, SIGILL);
569 1.1 gwr return;
570 1.1 gwr }
571 1.1 gwr kfp = (struct sigframe *)malloc((u_long)fsize, M_TEMP, M_WAITOK);
572 1.1 gwr /*
573 1.1 gwr * Build the argument list for the signal handler.
574 1.1 gwr */
575 1.1 gwr kfp->sf_signum = sig;
576 1.1 gwr kfp->sf_code = code;
577 1.1 gwr kfp->sf_scp = &fp->sf_sc;
578 1.1 gwr kfp->sf_handler = catcher;
579 1.1 gwr /*
580 1.1 gwr * Save necessary hardware state. Currently this includes:
581 1.1 gwr * - general registers
582 1.1 gwr * - original exception frame (if not a "normal" frame)
583 1.1 gwr * - FP coprocessor state
584 1.1 gwr */
585 1.1 gwr kfp->sf_state.ss_flags = SS_USERREGS;
586 1.1 gwr bcopy((caddr_t)frame->f_regs,
587 1.1 gwr (caddr_t)kfp->sf_state.ss_frame.f_regs, sizeof frame->f_regs);
588 1.1 gwr if (ft >= FMT7) {
589 1.1 gwr #ifdef DEBUG
590 1.1 gwr if (ft > 15 || exframesize[ft] < 0)
591 1.1 gwr panic("sendsig: bogus frame type");
592 1.1 gwr #endif
593 1.1 gwr kfp->sf_state.ss_flags |= SS_RTEFRAME;
594 1.1 gwr kfp->sf_state.ss_frame.f_format = frame->f_format;
595 1.1 gwr kfp->sf_state.ss_frame.f_vector = frame->f_vector;
596 1.1 gwr bcopy((caddr_t)&frame->F_u,
597 1.1 gwr (caddr_t)&kfp->sf_state.ss_frame.F_u,
598 1.1 gwr (size_t) exframesize[ft]);
599 1.1 gwr /*
600 1.1 gwr * Leave an indicator that we need to clean up the kernel
601 1.1 gwr * stack. We do this by setting the "pad word" above the
602 1.1 gwr * hardware stack frame to the amount the stack must be
603 1.1 gwr * adjusted by.
604 1.1 gwr *
605 1.1 gwr * N.B. we increment rather than just set f_stackadj in
606 1.1 gwr * case we are called from syscall when processing a
607 1.1 gwr * sigreturn. In that case, f_stackadj may be non-zero.
608 1.1 gwr */
609 1.1 gwr frame->f_stackadj += exframesize[ft];
610 1.1 gwr frame->f_format = frame->f_vector = 0;
611 1.1 gwr #ifdef DEBUG
612 1.1 gwr if (sigdebug & SDB_FOLLOW)
613 1.1 gwr printf("sendsig(%d): copy out %d of frame %d\n",
614 1.1 gwr p->p_pid, exframesize[ft], ft);
615 1.1 gwr #endif
616 1.1 gwr }
617 1.1 gwr
618 1.1 gwr if (fpu_type) {
619 1.1 gwr kfp->sf_state.ss_flags |= SS_FPSTATE;
620 1.1 gwr m68881_save(&kfp->sf_state.ss_fpstate);
621 1.1 gwr }
622 1.1 gwr #ifdef DEBUG
623 1.1 gwr if ((sigdebug & SDB_FPSTATE) && *(char *)&kfp->sf_state.ss_fpstate)
624 1.1 gwr printf("sendsig(%d): copy out FP state (%x) to %x\n",
625 1.1 gwr p->p_pid, *(u_int *)&kfp->sf_state.ss_fpstate,
626 1.1 gwr &kfp->sf_state.ss_fpstate);
627 1.1 gwr #endif
628 1.1 gwr
629 1.1 gwr /*
630 1.1 gwr * Build the signal context to be used by sigreturn.
631 1.1 gwr */
632 1.1 gwr kfp->sf_sc.sc_onstack = oonstack;
633 1.1 gwr kfp->sf_sc.sc_mask = mask;
634 1.1 gwr kfp->sf_sc.sc_sp = frame->f_regs[SP];
635 1.1 gwr kfp->sf_sc.sc_fp = frame->f_regs[A6];
636 1.1 gwr kfp->sf_sc.sc_ap = (int)&fp->sf_state;
637 1.1 gwr kfp->sf_sc.sc_pc = frame->f_pc;
638 1.1 gwr kfp->sf_sc.sc_ps = frame->f_sr;
639 1.1 gwr (void) copyout((caddr_t)kfp, (caddr_t)fp, fsize);
640 1.1 gwr frame->f_regs[SP] = (int)fp;
641 1.1 gwr #ifdef DEBUG
642 1.1 gwr if (sigdebug & SDB_FOLLOW)
643 1.1 gwr printf("sendsig(%d): sig %d scp %x fp %x sc_sp %x sc_ap %x\n",
644 1.1 gwr p->p_pid, sig, kfp->sf_scp, fp,
645 1.1 gwr kfp->sf_sc.sc_sp, kfp->sf_sc.sc_ap);
646 1.1 gwr #endif
647 1.1 gwr /*
648 1.1 gwr * Signal trampoline code is at base of user stack.
649 1.1 gwr */
650 1.1 gwr frame->f_pc = (int)PS_STRINGS - (esigcode - sigcode);
651 1.1 gwr #ifdef DEBUG
652 1.1 gwr if ((sigdebug & SDB_KSTACK) && p->p_pid == sigpid)
653 1.1 gwr printf("sendsig(%d): sig %d returns\n",
654 1.1 gwr p->p_pid, sig);
655 1.1 gwr #endif
656 1.1 gwr free((caddr_t)kfp, M_TEMP);
657 1.1 gwr }
658 1.1 gwr
659 1.1 gwr /*
660 1.1 gwr * System call to cleanup state after a signal
661 1.1 gwr * has been taken. Reset signal mask and
662 1.1 gwr * stack state from context left by sendsig (above).
663 1.1 gwr * Return to previous pc and psl as specified by
664 1.1 gwr * context left by sendsig. Check carefully to
665 1.1 gwr * make sure that the user has not modified the
666 1.1 gwr * psl to gain improper priviledges or to cause
667 1.1 gwr * a machine fault.
668 1.1 gwr */
669 1.1 gwr int
670 1.1 gwr sys_sigreturn(p, v, retval)
671 1.1 gwr struct proc *p;
672 1.1 gwr void *v;
673 1.1 gwr register_t *retval;
674 1.1 gwr {
675 1.1 gwr struct sys_sigreturn_args *uap = v;
676 1.1 gwr register struct sigcontext *scp;
677 1.1 gwr register struct frame *frame;
678 1.1 gwr register int rf;
679 1.1 gwr struct sigcontext tsigc;
680 1.1 gwr struct sigstate tstate;
681 1.1 gwr int flags;
682 1.1 gwr
683 1.1 gwr scp = SCARG(uap, sigcntxp);
684 1.1 gwr #ifdef DEBUG
685 1.1 gwr if (sigdebug & SDB_FOLLOW)
686 1.1 gwr printf("sigreturn: pid %d, scp %x\n", p->p_pid, scp);
687 1.1 gwr #endif
688 1.1 gwr if ((int)scp & 1)
689 1.1 gwr return (EINVAL);
690 1.1 gwr
691 1.1 gwr /*
692 1.1 gwr * Test and fetch the context structure.
693 1.1 gwr * We grab it all at once for speed.
694 1.1 gwr */
695 1.1 gwr if (useracc((caddr_t)scp, sizeof (*scp), B_WRITE) == 0 ||
696 1.1 gwr copyin((caddr_t)scp, (caddr_t)&tsigc, sizeof tsigc))
697 1.1 gwr return (EINVAL);
698 1.1 gwr scp = &tsigc;
699 1.1 gwr if ((scp->sc_ps & (PSL_MBZ|PSL_IPL|PSL_S)) != 0)
700 1.1 gwr return (EINVAL);
701 1.1 gwr /*
702 1.1 gwr * Restore the user supplied information
703 1.1 gwr */
704 1.1 gwr if (scp->sc_onstack & 01)
705 1.1 gwr p->p_sigacts->ps_sigstk.ss_flags |= SS_ONSTACK;
706 1.1 gwr else
707 1.1 gwr p->p_sigacts->ps_sigstk.ss_flags &= ~SS_ONSTACK;
708 1.1 gwr p->p_sigmask = scp->sc_mask &~ sigcantmask;
709 1.1 gwr frame = (struct frame *) p->p_md.md_regs;
710 1.1 gwr frame->f_regs[SP] = scp->sc_sp;
711 1.1 gwr frame->f_regs[A6] = scp->sc_fp;
712 1.1 gwr frame->f_pc = scp->sc_pc;
713 1.1 gwr frame->f_sr = scp->sc_ps;
714 1.1 gwr
715 1.1 gwr /*
716 1.1 gwr * Grab pointer to hardware state information.
717 1.1 gwr * If zero, the user is probably doing a longjmp.
718 1.1 gwr */
719 1.1 gwr if ((rf = scp->sc_ap) == 0)
720 1.1 gwr return (EJUSTRETURN);
721 1.1 gwr /*
722 1.1 gwr * See if there is anything to do before we go to the
723 1.1 gwr * expense of copying in close to 1/2K of data
724 1.1 gwr */
725 1.1 gwr flags = fuword((caddr_t)rf);
726 1.1 gwr #ifdef DEBUG
727 1.1 gwr if (sigdebug & SDB_FOLLOW)
728 1.1 gwr printf("sigreturn(%d): sc_ap %x flags %x\n",
729 1.1 gwr p->p_pid, rf, flags);
730 1.1 gwr #endif
731 1.1 gwr /*
732 1.1 gwr * fuword failed (bogus sc_ap value).
733 1.1 gwr */
734 1.1 gwr if (flags == -1)
735 1.1 gwr return (EINVAL);
736 1.1 gwr if (flags == 0 || copyin((caddr_t)rf, (caddr_t)&tstate, sizeof tstate))
737 1.1 gwr return (EJUSTRETURN);
738 1.1 gwr #ifdef DEBUG
739 1.1 gwr if ((sigdebug & SDB_KSTACK) && p->p_pid == sigpid)
740 1.1 gwr printf("sigreturn(%d): ssp %x usp %x scp %x ft %d\n",
741 1.1 gwr p->p_pid, &flags, scp->sc_sp, SCARG(uap, sigcntxp),
742 1.1 gwr (flags&SS_RTEFRAME) ? tstate.ss_frame.f_format : -1);
743 1.1 gwr #endif
744 1.1 gwr /*
745 1.1 gwr * Restore most of the users registers except for A6 and SP
746 1.1 gwr * which were handled above.
747 1.1 gwr */
748 1.1 gwr if (flags & SS_USERREGS)
749 1.1 gwr bcopy((caddr_t)tstate.ss_frame.f_regs,
750 1.1 gwr (caddr_t)frame->f_regs, sizeof(frame->f_regs)-2*NBPW);
751 1.1 gwr /*
752 1.1 gwr * Restore long stack frames. Note that we do not copy
753 1.1 gwr * back the saved SR or PC, they were picked up above from
754 1.1 gwr * the sigcontext structure.
755 1.1 gwr */
756 1.1 gwr if (flags & SS_RTEFRAME) {
757 1.1 gwr register int sz;
758 1.1 gwr
759 1.1 gwr /* grab frame type and validate */
760 1.1 gwr sz = tstate.ss_frame.f_format;
761 1.1 gwr if (sz > 15 || (sz = exframesize[sz]) < 0)
762 1.1 gwr return (EINVAL);
763 1.1 gwr frame->f_stackadj -= sz;
764 1.1 gwr frame->f_format = tstate.ss_frame.f_format;
765 1.1 gwr frame->f_vector = tstate.ss_frame.f_vector;
766 1.1 gwr bcopy((caddr_t)&tstate.ss_frame.F_u, (caddr_t)&frame->F_u, sz);
767 1.1 gwr #ifdef DEBUG
768 1.1 gwr if (sigdebug & SDB_FOLLOW)
769 1.1 gwr printf("sigreturn(%d): copy in %d of frame type %d\n",
770 1.1 gwr p->p_pid, sz, tstate.ss_frame.f_format);
771 1.1 gwr #endif
772 1.1 gwr }
773 1.1 gwr
774 1.1 gwr /*
775 1.1 gwr * Finally we restore the original FP context
776 1.1 gwr */
777 1.1 gwr if (flags & SS_FPSTATE)
778 1.1 gwr m68881_restore(&tstate.ss_fpstate);
779 1.1 gwr #ifdef DEBUG
780 1.1 gwr if ((sigdebug & SDB_FPSTATE) && *(char *)&tstate.ss_fpstate)
781 1.1 gwr printf("sigreturn(%d): copied in FP state (%x) at %x\n",
782 1.1 gwr p->p_pid, *(u_int *)&tstate.ss_fpstate,
783 1.1 gwr &tstate.ss_fpstate);
784 1.1 gwr if ((sigdebug & SDB_FOLLOW) ||
785 1.1 gwr ((sigdebug & SDB_KSTACK) && p->p_pid == sigpid))
786 1.1 gwr printf("sigreturn(%d): returns\n", p->p_pid);
787 1.1 gwr #endif
788 1.1 gwr return (EJUSTRETURN);
789 1.1 gwr }
790 1.1 gwr
791 1.1 gwr
792 1.1 gwr /*
793 1.1 gwr * Do a sync in preparation for a reboot.
794 1.1 gwr * XXX - This could probably be common code.
795 1.1 gwr * XXX - And now, most of it is in vfs_shutdown()
796 1.1 gwr * XXX - Put waittime checks in there too?
797 1.1 gwr */
798 1.1 gwr int waittime = -1; /* XXX - Who else looks at this? -gwr */
799 1.1 gwr static void
800 1.1 gwr reboot_sync __P((void))
801 1.1 gwr {
802 1.1 gwr
803 1.1 gwr /* Check waittime here to localize its use to this function. */
804 1.1 gwr if (waittime >= 0)
805 1.1 gwr return;
806 1.1 gwr waittime = 0;
807 1.1 gwr vfs_shutdown();
808 1.1 gwr }
809 1.1 gwr
810 1.1 gwr /*
811 1.1 gwr * Common part of the BSD and SunOS reboot system calls.
812 1.1 gwr * XXX - Should be named: cpu_reboot maybe? -gwr
813 1.1 gwr */
814 1.1 gwr __dead void
815 1.1 gwr boot(howto, user_boot_string)
816 1.1 gwr int howto;
817 1.1 gwr char *user_boot_string;
818 1.1 gwr {
819 1.2 gwr /* Note: this string MUST be static! */
820 1.2 gwr static char bootstr[128];
821 1.2 gwr char *p;
822 1.1 gwr
823 1.1 gwr /* If system is cold, just halt. (early panic?) */
824 1.1 gwr if (cold)
825 1.1 gwr goto haltsys;
826 1.1 gwr
827 1.1 gwr if ((howto & RB_NOSYNC) == 0) {
828 1.1 gwr reboot_sync();
829 1.1 gwr /*
830 1.1 gwr * If we've been adjusting the clock, the todr
831 1.1 gwr * will be out of synch; adjust it now.
832 1.1 gwr *
833 1.1 gwr * XXX - However, if the kernel has been sitting in ddb,
834 1.1 gwr * the time will be way off, so don't set the HW clock!
835 1.1 gwr * XXX - Should do sanity check against HW clock. -gwr
836 1.1 gwr */
837 1.1 gwr /* resettodr(); */
838 1.1 gwr }
839 1.1 gwr
840 1.1 gwr /* Disable interrupts. */
841 1.1 gwr splhigh();
842 1.1 gwr
843 1.1 gwr /* Write out a crash dump if asked. */
844 1.1 gwr if (howto & RB_DUMP)
845 1.1 gwr dumpsys();
846 1.1 gwr
847 1.1 gwr /* run any shutdown hooks */
848 1.1 gwr doshutdownhooks();
849 1.1 gwr
850 1.1 gwr if (howto & RB_HALT) {
851 1.1 gwr haltsys:
852 1.1 gwr printf("Kernel halted.\n");
853 1.1 gwr sun3x_mon_halt();
854 1.1 gwr }
855 1.1 gwr
856 1.1 gwr /*
857 1.1 gwr * Automatic reboot.
858 1.1 gwr */
859 1.2 gwr if (user_boot_string)
860 1.2 gwr strncpy(bootstr, user_boot_string, sizeof(bootstr));
861 1.2 gwr else {
862 1.1 gwr /*
863 1.1 gwr * Build our own boot string with an empty
864 1.1 gwr * boot device/file and (maybe) some flags.
865 1.1 gwr * The PROM will supply the device/file name.
866 1.1 gwr */
867 1.2 gwr p = bootstr;
868 1.2 gwr *p = '\0';
869 1.1 gwr if (howto & (RB_KDB|RB_ASKNAME|RB_SINGLE)) {
870 1.1 gwr /* Append the boot flags. */
871 1.1 gwr *p++ = ' ';
872 1.1 gwr *p++ = '-';
873 1.1 gwr if (howto & RB_KDB)
874 1.1 gwr *p++ = 'd';
875 1.1 gwr if (howto & RB_ASKNAME)
876 1.1 gwr *p++ = 'a';
877 1.1 gwr if (howto & RB_SINGLE)
878 1.1 gwr *p++ = 's';
879 1.1 gwr *p = '\0';
880 1.1 gwr }
881 1.1 gwr }
882 1.1 gwr printf("Kernel rebooting...\n");
883 1.2 gwr sun3x_mon_reboot(bootstr);
884 1.1 gwr for (;;) ;
885 1.1 gwr /*NOTREACHED*/
886 1.1 gwr }
887 1.1 gwr
888 1.1 gwr /*
889 1.1 gwr * These variables are needed by /sbin/savecore
890 1.1 gwr */
891 1.1 gwr u_long dumpmag = 0x8fca0101; /* magic number */
892 1.1 gwr int dumpsize = 0; /* pages */
893 1.1 gwr long dumplo = 0; /* blocks */
894 1.1 gwr
895 1.1 gwr /*
896 1.1 gwr * This is called by cpu_startup to set dumplo, dumpsize.
897 1.1 gwr * Dumps always skip the first CLBYTES of disk space
898 1.1 gwr * in case there might be a disk label stored there.
899 1.1 gwr * If there is extra space, put dump at the end to
900 1.1 gwr * reduce the chance that swapping trashes it.
901 1.1 gwr */
902 1.1 gwr void
903 1.1 gwr dumpconf()
904 1.1 gwr {
905 1.1 gwr int nblks; /* size of dump area */
906 1.1 gwr int maj;
907 1.1 gwr int (*getsize)__P((dev_t));
908 1.1 gwr
909 1.1 gwr if (dumpdev == NODEV)
910 1.1 gwr return;
911 1.1 gwr
912 1.1 gwr maj = major(dumpdev);
913 1.1 gwr if (maj < 0 || maj >= nblkdev)
914 1.1 gwr panic("dumpconf: bad dumpdev=0x%x", dumpdev);
915 1.1 gwr getsize = bdevsw[maj].d_psize;
916 1.1 gwr if (getsize == NULL)
917 1.1 gwr return;
918 1.1 gwr nblks = (*getsize)(dumpdev);
919 1.1 gwr if (nblks <= ctod(1))
920 1.1 gwr return;
921 1.1 gwr
922 1.1 gwr /* Position dump image near end of space, page aligned. */
923 1.1 gwr dumpsize = physmem; /* pages */
924 1.1 gwr dumplo = nblks - ctod(dumpsize);
925 1.1 gwr dumplo &= ~(ctod(1)-1);
926 1.1 gwr
927 1.1 gwr /* If it does not fit, truncate it by moving dumplo. */
928 1.1 gwr /* Note: Must force signed comparison. */
929 1.1 gwr if (dumplo < ((long)ctod(1))) {
930 1.1 gwr dumplo = ctod(1);
931 1.1 gwr dumpsize = dtoc(nblks - dumplo);
932 1.1 gwr }
933 1.1 gwr }
934 1.1 gwr
935 1.1 gwr struct pcb dumppcb;
936 1.1 gwr extern vm_offset_t avail_start;
937 1.1 gwr
938 1.1 gwr /*
939 1.1 gwr * Write a crash dump. The format while in swap is:
940 1.1 gwr * kcore_seg_t cpu_hdr;
941 1.1 gwr * cpu_kcore_hdr_t cpu_data;
942 1.1 gwr * padding (NBPG-sizeof(kcore_seg_t))
943 1.1 gwr * pagemap (2*NBPG)
944 1.1 gwr * physical memory...
945 1.1 gwr */
946 1.1 gwr void
947 1.1 gwr dumpsys()
948 1.1 gwr {
949 1.1 gwr struct bdevsw *dsw;
950 1.1 gwr char *vaddr;
951 1.1 gwr vm_offset_t paddr;
952 1.1 gwr int psize, todo, chunk;
953 1.1 gwr daddr_t blkno;
954 1.1 gwr int error = 0;
955 1.1 gwr
956 1.1 gwr msgbufmapped = 0;
957 1.1 gwr if (dumpdev == NODEV)
958 1.1 gwr return;
959 1.1 gwr
960 1.1 gwr /*
961 1.1 gwr * For dumps during autoconfiguration,
962 1.1 gwr * if dump device has already configured...
963 1.1 gwr */
964 1.1 gwr if (dumpsize == 0)
965 1.1 gwr dumpconf();
966 1.1 gwr if (dumplo <= 0)
967 1.1 gwr return;
968 1.1 gwr savectx(&dumppcb);
969 1.1 gwr
970 1.1 gwr dsw = &bdevsw[major(dumpdev)];
971 1.1 gwr psize = (*(dsw->d_psize))(dumpdev);
972 1.1 gwr if (psize == -1) {
973 1.1 gwr printf("dump area unavailable\n");
974 1.1 gwr return;
975 1.1 gwr }
976 1.1 gwr
977 1.1 gwr printf("\ndumping to dev %x, offset %d\n",
978 1.1 gwr (int) dumpdev, (int) dumplo);
979 1.1 gwr
980 1.1 gwr /*
981 1.1 gwr * Write the dump header, including MMU state.
982 1.1 gwr */
983 1.1 gwr blkno = dumplo;
984 1.1 gwr todo = dumpsize; /* pages */
985 1.1 gwr
986 1.1 gwr /*
987 1.1 gwr * Now dump physical memory. Have to do it in two chunks.
988 1.1 gwr * The first chunk is "unmanaged" (by the VM code) and its
989 1.1 gwr * range of physical addresses is not allow in pmap_enter.
990 1.1 gwr * However, that segment is mapped linearly, so we can just
991 1.1 gwr * use the virtual mappings already in place. The second
992 1.1 gwr * chunk is done the normal way, using pmap_enter.
993 1.1 gwr *
994 1.1 gwr * Note that vaddr==(paddr+KERNBASE) for paddr=0 through etext.
995 1.1 gwr */
996 1.1 gwr
997 1.1 gwr /* Do the first chunk (0 <= PA < avail_start) */
998 1.1 gwr paddr = 0;
999 1.1 gwr chunk = btoc(avail_start);
1000 1.1 gwr if (chunk > todo)
1001 1.1 gwr chunk = todo;
1002 1.1 gwr do {
1003 1.1 gwr if ((todo & 0xf) == 0)
1004 1.1 gwr printf("\r%4d", todo);
1005 1.1 gwr vaddr = (char*)(paddr + KERNBASE);
1006 1.1 gwr error = (*dsw->d_dump)(dumpdev, blkno, vaddr, NBPG);
1007 1.1 gwr if (error)
1008 1.1 gwr goto fail;
1009 1.1 gwr paddr += NBPG;
1010 1.1 gwr blkno += btodb(NBPG);
1011 1.1 gwr --todo;
1012 1.1 gwr } while (--chunk > 0);
1013 1.1 gwr
1014 1.1 gwr /* Do the second chunk (avail_start <= PA < dumpsize) */
1015 1.1 gwr vaddr = (char*)vmmap; /* Borrow /dev/mem VA */
1016 1.1 gwr do {
1017 1.1 gwr if ((todo & 0xf) == 0)
1018 1.1 gwr printf("\r%4d", todo);
1019 1.1 gwr pmap_enter(pmap_kernel(), vmmap, paddr | PMAP_NC,
1020 1.1 gwr VM_PROT_READ, FALSE);
1021 1.1 gwr error = (*dsw->d_dump)(dumpdev, blkno, vaddr, NBPG);
1022 1.1 gwr pmap_remove(pmap_kernel(), vmmap, vmmap + NBPG);
1023 1.1 gwr if (error)
1024 1.1 gwr goto fail;
1025 1.1 gwr paddr += NBPG;
1026 1.1 gwr blkno += btodb(NBPG);
1027 1.1 gwr } while (--todo > 0);
1028 1.1 gwr
1029 1.1 gwr printf("\rdump succeeded\n");
1030 1.1 gwr return;
1031 1.1 gwr fail:
1032 1.1 gwr printf(" dump error=%d\n", error);
1033 1.1 gwr }
1034 1.1 gwr
1035 1.1 gwr static void
1036 1.1 gwr initcpu()
1037 1.1 gwr {
1038 1.1 gwr /* XXX: Enable RAM parity/ECC checking? */
1039 1.1 gwr /* XXX: parityenable(); */
1040 1.1 gwr
1041 1.1 gwr nofault = NULL; /* XXX - needed? */
1042 1.1 gwr
1043 1.1 gwr #ifdef HAVECACHE
1044 1.1 gwr cache_enable();
1045 1.1 gwr #endif
1046 1.1 gwr }
1047 1.1 gwr
1048 1.1 gwr /* called from locore.s */
1049 1.1 gwr void straytrap __P((struct trapframe));
1050 1.1 gwr void
1051 1.1 gwr straytrap(frame)
1052 1.1 gwr struct trapframe frame;
1053 1.1 gwr {
1054 1.1 gwr printf("unexpected trap; vector=0x%x at pc=0x%x\n",
1055 1.1 gwr frame.tf_vector, frame.tf_pc);
1056 1.1 gwr #ifdef DDB
1057 1.1 gwr kdb_trap(-1, (db_regs_t *) &frame);
1058 1.1 gwr #endif
1059 1.1 gwr }
1060 1.1 gwr
1061 1.1 gwr /* from hp300: badaddr() */
1062 1.1 gwr /* peek_byte(), peek_word() moved to autoconf.c */
1063 1.1 gwr
1064 1.1 gwr /* XXX: parityenable() ? */
1065 1.1 gwr
1066 1.1 gwr static void dumpmem __P((int *, int, int));
1067 1.1 gwr static char *hexstr __P((int, int));
1068 1.1 gwr
1069 1.1 gwr /*
1070 1.1 gwr * Print a register and stack dump.
1071 1.1 gwr */
1072 1.1 gwr void
1073 1.1 gwr regdump(fp, sbytes)
1074 1.1 gwr struct frame *fp; /* must not be register */
1075 1.1 gwr int sbytes;
1076 1.1 gwr {
1077 1.1 gwr static int doingdump = 0;
1078 1.1 gwr register int i;
1079 1.1 gwr int s;
1080 1.1 gwr
1081 1.1 gwr if (doingdump)
1082 1.1 gwr return;
1083 1.1 gwr s = splhigh();
1084 1.1 gwr doingdump = 1;
1085 1.1 gwr printf("pid = %d, pc = %s, ",
1086 1.1 gwr curproc ? curproc->p_pid : -1, hexstr(fp->f_pc, 8));
1087 1.1 gwr printf("ps = %s, ", hexstr(fp->f_sr, 4));
1088 1.1 gwr printf("sfc = %s, ", hexstr(getsfc(), 4));
1089 1.1 gwr printf("dfc = %s\n", hexstr(getdfc(), 4));
1090 1.1 gwr printf("Registers:\n ");
1091 1.1 gwr for (i = 0; i < 8; i++)
1092 1.1 gwr printf(" %d", i);
1093 1.1 gwr printf("\ndreg:");
1094 1.1 gwr for (i = 0; i < 8; i++)
1095 1.1 gwr printf(" %s", hexstr(fp->f_regs[i], 8));
1096 1.1 gwr printf("\nareg:");
1097 1.1 gwr for (i = 0; i < 8; i++)
1098 1.1 gwr printf(" %s", hexstr(fp->f_regs[i+8], 8));
1099 1.1 gwr if (sbytes > 0) {
1100 1.1 gwr if (fp->f_sr & PSL_S) {
1101 1.1 gwr printf("\n\nKernel stack (%s):",
1102 1.1 gwr hexstr((int)(((int *)&fp)-1), 8));
1103 1.1 gwr dumpmem(((int *)&fp)-1, sbytes, 0);
1104 1.1 gwr } else {
1105 1.1 gwr printf("\n\nUser stack (%s):", hexstr(fp->f_regs[SP], 8));
1106 1.1 gwr dumpmem((int *)fp->f_regs[SP], sbytes, 1);
1107 1.1 gwr }
1108 1.1 gwr }
1109 1.1 gwr doingdump = 0;
1110 1.1 gwr splx(s);
1111 1.1 gwr }
1112 1.1 gwr
1113 1.1 gwr #define KSADDR ((int *)((u_int)curproc->p_addr + USPACE - NBPG))
1114 1.1 gwr
1115 1.1 gwr static void
1116 1.1 gwr dumpmem(ptr, sz, ustack)
1117 1.1 gwr register int *ptr;
1118 1.1 gwr int sz, ustack;
1119 1.1 gwr {
1120 1.1 gwr register int i, val;
1121 1.1 gwr
1122 1.1 gwr for (i = 0; i < sz; i++) {
1123 1.1 gwr if ((i & 7) == 0)
1124 1.1 gwr printf("\n%s: ", hexstr((int)ptr, 6));
1125 1.1 gwr else
1126 1.1 gwr printf(" ");
1127 1.1 gwr if (ustack == 1) {
1128 1.1 gwr if ((val = fuword(ptr++)) == -1)
1129 1.1 gwr break;
1130 1.1 gwr } else {
1131 1.1 gwr if (ustack == 0 &&
1132 1.1 gwr (ptr < KSADDR || ptr > KSADDR+(NBPG/4-1)))
1133 1.1 gwr break;
1134 1.1 gwr val = *ptr++;
1135 1.1 gwr }
1136 1.1 gwr printf("%s", hexstr(val, 8));
1137 1.1 gwr }
1138 1.1 gwr printf("\n");
1139 1.1 gwr }
1140 1.1 gwr
1141 1.1 gwr static char *
1142 1.1 gwr hexstr(val, len)
1143 1.1 gwr register int val;
1144 1.1 gwr int len;
1145 1.1 gwr {
1146 1.1 gwr static char nbuf[9];
1147 1.1 gwr register int x, i;
1148 1.1 gwr
1149 1.1 gwr if (len > 8)
1150 1.1 gwr return("");
1151 1.1 gwr nbuf[len] = '\0';
1152 1.1 gwr for (i = len-1; i >= 0; --i) {
1153 1.1 gwr x = val & 0xF;
1154 1.1 gwr /* Isn't this a cool trick? */
1155 1.1 gwr nbuf[i] = "0123456789ABCDEF"[x];
1156 1.1 gwr val >>= 4;
1157 1.1 gwr }
1158 1.1 gwr return(nbuf);
1159 1.1 gwr }
1160 1.1 gwr
1161 1.1 gwr /*
1162 1.1 gwr * cpu_exec_aout_makecmds():
1163 1.1 gwr * cpu-dependent a.out format hook for execve().
1164 1.1 gwr *
1165 1.1 gwr * Determine if the given exec package refers to something which we
1166 1.1 gwr * understand and, if so, set up the vmcmds for it.
1167 1.1 gwr */
1168 1.1 gwr int
1169 1.1 gwr cpu_exec_aout_makecmds(p, epp)
1170 1.1 gwr struct proc *p;
1171 1.1 gwr struct exec_package *epp;
1172 1.1 gwr {
1173 1.1 gwr int error = ENOEXEC;
1174 1.1 gwr
1175 1.1 gwr #ifdef COMPAT_SUNOS
1176 1.1 gwr extern sunos_exec_aout_makecmds
1177 1.1 gwr __P((struct proc *, struct exec_package *));
1178 1.1 gwr if ((error = sunos_exec_aout_makecmds(p, epp)) == 0)
1179 1.1 gwr return 0;
1180 1.1 gwr #endif
1181 1.1 gwr return error;
1182 1.1 gwr }
1183