machdep.c revision 1.1 1 /* $NetBSD: machdep.c,v 1.1 1997/01/14 20:57:09 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 char *bs, *p;
820 char default_boot_string[8];
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 sun3x_mon_halt();
853 }
854
855 /*
856 * Automatic reboot.
857 */
858 bs = user_boot_string;
859 if (bs == NULL) {
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 bs = default_boot_string;
866 *bs = '\0';
867 if (howto & (RB_KDB|RB_ASKNAME|RB_SINGLE)) {
868 /* Append the boot flags. */
869 p = bs;
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 sun3x_mon_reboot(bs);
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