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