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