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