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