NetBSD/sys/arch/sun3/sun3x/machdep.c
gwr dad92086a4 Make sure the string passed to mon_reboot() is in our data segment
where the PROM can see it.  (Bug found by Jeremy  -- Thanks!)
1997-01-16 22:08:31 +00:00

1183 lines
30 KiB
C

/* $NetBSD: machdep.c,v 1.2 1997/01/16 22:08:31 gwr Exp $ */
/*
* Copyright (c) 1988 University of Utah.
* Copyright (c) 1982, 1986, 1990, 1993
* The Regents of the University of California. All rights reserved.
*
* This code is derived from software contributed to Berkeley by
* the Systems Programming Group of the University of Utah Computer
* Science Department.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* from: Utah Hdr: machdep.c 1.74 92/12/20
* from: @(#)machdep.c 8.10 (Berkeley) 4/20/94
*/
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/signalvar.h>
#include <sys/kernel.h>
#include <sys/map.h>
#include <sys/proc.h>
#include <sys/buf.h>
#include <sys/reboot.h>
#include <sys/conf.h>
#include <sys/file.h>
#include <sys/clist.h>
#include <sys/callout.h>
#include <sys/malloc.h>
#include <sys/mbuf.h>
#include <sys/msgbuf.h>
#include <sys/ioctl.h>
#include <sys/tty.h>
#include <sys/mount.h>
#include <sys/user.h>
#include <sys/exec.h>
#include <sys/core.h>
#include <sys/kcore.h>
#include <sys/vnode.h>
#include <sys/sysctl.h>
#include <sys/syscallargs.h>
#ifdef SYSVMSG
#include <sys/msg.h>
#endif
#ifdef SYSVSEM
#include <sys/sem.h>
#endif
#ifdef SYSVSHM
#include <sys/shm.h>
#endif
#include <vm/vm.h>
#include <vm/vm_map.h>
#include <vm/vm_kern.h>
#include <vm/vm_page.h>
#include <dev/cons.h>
#include <machine/cpu.h>
#include <machine/reg.h>
#include <machine/psl.h>
#include <machine/pte.h>
#include <machine/mon.h>
#include <machine/dvma.h>
#include <machine/db_machdep.h>
#include "machdep.h"
extern char *cpu_string;
extern char version[];
extern short exframesize[];
/* Defined in locore.s */
extern char kernel_text[];
/* Defined by the linker */
extern char etext[];
int physmem;
int fpu_type;
int msgbufmapped;
vm_offset_t vmmap;
/*
* safepri is a safe priority for sleep to set for a spin-wait
* during autoconfiguration or after a panic.
*/
int safepri = PSL_LOWIPL;
/*
* Declare these as initialized data so we can patch them.
*/
int nswbuf = 0;
#ifdef NBUF
int nbuf = NBUF;
#else
int nbuf = 0;
#endif
#ifdef BUFPAGES
int bufpages = BUFPAGES;
#else
int bufpages = 0;
#endif
label_t *nofault;
static void identifycpu __P((void));
static void initcpu __P((void));
/*
* Console initialization: called early on from main,
* before vm init or startup. Do enough configuration
* to choose and initialize a console.
*/
void consinit()
{
cninit();
#ifdef KGDB
/* XXX - Ask on console for kgdb_dev? */
zs_kgdb_init(); /* XXX */
/* Note: kgdb_connect() will just return if kgdb_dev<0 */
if (boothowto & RB_KDB)
kgdb_connect(1);
#endif
#ifdef DDB
/* Now that we have a console, we can stop in DDB. */
db_machine_init();
ddb_init();
if (boothowto & RB_KDB)
Debugger();
#endif DDB
}
/*
* allocsys() - Private routine used by cpu_startup() below.
*
* Allocate space for system data structures. We are given
* a starting virtual address and we return a final virtual
* address; along the way we set each data structure pointer.
*
* We call allocsys() with 0 to find out how much space we want,
* allocate that much and fill it with zeroes, and then call
* allocsys() again with the correct base virtual address.
*/
#define valloc(name, type, num) \
v = (caddr_t)(((name) = (type *)v) + (num))
static caddr_t allocsys __P((caddr_t));
static caddr_t
allocsys(v)
register caddr_t v;
{
#ifdef REAL_CLISTS
valloc(cfree, struct cblock, nclist);
#endif
valloc(callout, struct callout, ncallout);
valloc(swapmap, struct map, nswapmap = maxproc * 2);
#ifdef SYSVSHM
valloc(shmsegs, struct shmid_ds, shminfo.shmmni);
#endif
#ifdef SYSVSEM
valloc(sema, struct semid_ds, seminfo.semmni);
valloc(sem, struct sem, seminfo.semmns);
/* This is pretty disgusting! */
valloc(semu, int, (seminfo.semmnu * seminfo.semusz) / sizeof(int));
#endif
#ifdef SYSVMSG
valloc(msgpool, char, msginfo.msgmax);
valloc(msgmaps, struct msgmap, msginfo.msgseg);
valloc(msghdrs, struct msg, msginfo.msgtql);
valloc(msqids, struct msqid_ds, msginfo.msgmni);
#endif
/*
* Determine how many buffers to allocate. We allocate
* the BSD standard of use 10% of memory for the first 2 Meg,
* 5% of remaining. Insure a minimum of 16 buffers.
* Allocate 1/2 as many swap buffer headers as file i/o buffers.
*/
if (bufpages == 0) {
/* We always have more than 2MB of memory. */
bufpages = ((btoc(2 * 1024 * 1024) + physmem) /
(20 * CLSIZE));
}
if (nbuf == 0) {
nbuf = bufpages;
if (nbuf < 16)
nbuf = 16;
}
if (nswbuf == 0) {
nswbuf = (nbuf / 2) &~ 1; /* force even */
if (nswbuf > 256)
nswbuf = 256; /* sanity */
}
valloc(swbuf, struct buf, nswbuf);
valloc(buf, struct buf, nbuf);
return v;
}
#undef valloc
/*
* cpu_startup: allocate memory for variable-sized tables,
* initialize cpu, and do autoconfiguration.
*
* This is called early in init_main.c:main(), after the
* kernel memory allocator is ready for use, but before
* the creation of processes 1,2, and mountroot, etc.
*/
void
cpu_startup()
{
caddr_t v;
int sz, i;
vm_size_t size;
int base, residual;
vm_offset_t minaddr, maxaddr;
/*
* Initialize message buffer (for kernel printf).
* This is put in physical page zero so it will
* always be in the same place after a reboot.
* Its mapping was prepared in pmap_bootstrap().
* Also, offset some to avoid PROM scribbles.
*/
v = (caddr_t) KERNBASE;
msgbufp = (struct msgbuf *)(v + 0x1000);
msgbufmapped = 1;
/*
* Good {morning,afternoon,evening,night}.
*/
printf(version);
identifycpu();
initfpu(); /* also prints FPU type */
printf("real mem = %d\n", ctob(physmem));
/*
* Find out how much space we need, allocate it,
* and then give everything true virtual addresses.
*/
sz = (int)allocsys((caddr_t)0);
if ((v = (caddr_t)kmem_alloc(kernel_map, round_page(sz))) == 0)
panic("startup: no room for tables");
if (allocsys(v) - v != sz)
panic("startup: table size inconsistency");
/*
* Now allocate buffers proper. They are different than the above
* in that they usually occupy more virtual memory than physical.
*/
size = MAXBSIZE * nbuf;
buffer_map = kmem_suballoc(kernel_map, (vm_offset_t *)&buffers,
&maxaddr, size, TRUE);
minaddr = (vm_offset_t)buffers;
if (vm_map_find(buffer_map, vm_object_allocate(size), (vm_offset_t)0,
&minaddr, size, FALSE) != KERN_SUCCESS)
panic("startup: cannot allocate buffers");
if ((bufpages / nbuf) >= btoc(MAXBSIZE)) {
/* don't want to alloc more physical mem than needed */
bufpages = btoc(MAXBSIZE) * nbuf;
}
base = bufpages / nbuf;
residual = bufpages % nbuf;
for (i = 0; i < nbuf; i++) {
vm_size_t curbufsize;
vm_offset_t curbuf;
/*
* First <residual> buffers get (base+1) physical pages
* allocated for them. The rest get (base) physical pages.
*
* The rest of each buffer occupies virtual space,
* but has no physical memory allocated for it.
*/
curbuf = (vm_offset_t)buffers + i * MAXBSIZE;
curbufsize = CLBYTES * (i < residual ? base+1 : base);
vm_map_pageable(buffer_map, curbuf, curbuf+curbufsize, FALSE);
vm_map_simplify(buffer_map, curbuf);
}
/*
* Allocate a submap for exec arguments. This map effectively
* limits the number of processes exec'ing at any time.
*/
exec_map = kmem_suballoc(kernel_map, &minaddr, &maxaddr,
16*NCARGS, TRUE);
/*
* We don't use a submap for physio, and use a separate map
* for DVMA allocations. Our vmapbuf just maps pages into
* the kernel map (any kernel mapping is OK) and then the
* device drivers clone the kernel mappings into DVMA space.
*/
/*
* Finally, allocate mbuf pool. Since mclrefcnt is an off-size
* we use the more space efficient malloc in place of kmem_alloc.
*/
mclrefcnt = (char *)malloc(NMBCLUSTERS+CLBYTES/MCLBYTES,
M_MBUF, M_NOWAIT);
bzero(mclrefcnt, NMBCLUSTERS+CLBYTES/MCLBYTES);
mb_map = kmem_suballoc(kernel_map, (vm_offset_t *)&mbutl, &maxaddr,
VM_MBUF_SIZE, FALSE);
/*
* Initialize callouts
*/
callfree = callout;
for (i = 1; i < ncallout; i++)
callout[i-1].c_next = &callout[i];
callout[i-1].c_next = NULL;
printf("avail mem = %d\n", (int) ptoa(cnt.v_free_count));
printf("using %d buffers containing %d bytes of memory\n",
nbuf, bufpages * CLBYTES);
/*
* Tell the VM system that writing to kernel text isn't allowed.
* If we don't, we might end up COW'ing the text segment!
*/
if (vm_map_protect(kernel_map, (vm_offset_t) kernel_text,
sun3x_trunc_page((vm_offset_t) etext),
VM_PROT_READ|VM_PROT_EXECUTE, TRUE)
!= KERN_SUCCESS)
panic("can't protect kernel text");
/*
* Allocate a virtual page (for use by /dev/mem)
* This page is handed to pmap_enter() therefore
* it has to be in the normal kernel VA range.
*/
vmmap = kmem_alloc_wait(kernel_map, NBPG);
/*
* Create the DVMA maps.
*/
dvma_init();
/*
* Set up CPU-specific registers, cache, etc.
*/
initcpu();
/*
* Set up buffers, so they can be used to read disk labels.
*/
bufinit();
/*
* Configure the system.
*/
configure();
}
/*
* Set registers on exec.
* XXX Should clear registers except sp, pc,
* but would break init; should be fixed soon.
*/
void
setregs(p, pack, stack, retval)
register struct proc *p;
struct exec_package *pack;
u_long stack;
register_t *retval;
{
struct frame *frame = (struct frame *)p->p_md.md_regs;
frame->f_pc = pack->ep_entry & ~1;
frame->f_regs[SP] = stack;
frame->f_regs[A2] = (int)PS_STRINGS;
/* restore a null state frame */
p->p_addr->u_pcb.pcb_fpregs.fpf_null = 0;
if (fpu_type) {
m68881_restore(&p->p_addr->u_pcb.pcb_fpregs);
}
p->p_md.md_flags = 0;
/* XXX - HPUX sigcode hack would go here... */
}
/*
* Info for CTL_HW
*/
char machine[] = "sun3x"; /* cpu "architecture" */
char cpu_model[120];
extern long hostid;
void
identifycpu()
{
/*
* actual identification done earlier because i felt like it,
* and i believe i will need the info to deal with some VAC, and awful
* framebuffer placement problems. could be moved later.
*/
strcpy(cpu_model, "Sun 3/");
/* should eventually include whether it has a VAC, mc6888x version, etc */
strcat(cpu_model, cpu_string);
printf("Model: %s (hostid %x)\n", cpu_model, (int) hostid);
}
/*
* machine dependent system variables.
*/
int
cpu_sysctl(name, namelen, oldp, oldlenp, newp, newlen, p)
int *name;
u_int namelen;
void *oldp;
size_t *oldlenp;
void *newp;
size_t newlen;
struct proc *p;
{
int error;
dev_t consdev;
/* all sysctl names at this level are terminal */
if (namelen != 1)
return (ENOTDIR); /* overloaded */
switch (name[0]) {
case CPU_CONSDEV:
if (cn_tab != NULL)
consdev = cn_tab->cn_dev;
else
consdev = NODEV;
error = sysctl_rdstruct(oldp, oldlenp, newp,
&consdev, sizeof consdev);
break;
#if 0 /* XXX - Not yet... */
case CPU_ROOT_DEVICE:
error = sysctl_rdstring(oldp, oldlenp, newp, root_device);
break;
case CPU_BOOTED_KERNEL:
error = sysctl_rdstring(oldp, oldlenp, newp, booted_kernel);
break;
#endif
default:
error = EOPNOTSUPP;
}
return (error);
}
#define SS_RTEFRAME 1
#define SS_FPSTATE 2
#define SS_USERREGS 4
struct sigstate {
int ss_flags; /* which of the following are valid */
struct frame ss_frame; /* original exception frame */
struct fpframe ss_fpstate; /* 68881/68882 state info */
};
/*
* WARNING: code in locore.s assumes the layout shown for sf_signum
* thru sf_handler so... don't screw with them!
*/
struct sigframe {
int sf_signum; /* signo for handler */
int sf_code; /* additional info for handler */
struct sigcontext *sf_scp; /* context ptr for handler */
sig_t sf_handler; /* handler addr for u_sigc */
struct sigstate sf_state; /* state of the hardware */
struct sigcontext sf_sc; /* actual context */
};
#ifdef DEBUG
int sigdebug = 0;
int sigpid = 0;
#define SDB_FOLLOW 0x01
#define SDB_KSTACK 0x02
#define SDB_FPSTATE 0x04
#endif
/*
* Send an interrupt to process.
*/
void
sendsig(catcher, sig, mask, code)
sig_t catcher;
int sig, mask;
u_long code;
{
register struct proc *p = curproc;
register struct sigframe *fp, *kfp;
register struct frame *frame;
register struct sigacts *psp = p->p_sigacts;
register short ft;
int oonstack, fsize;
extern char sigcode[], esigcode[];
frame = (struct frame *)p->p_md.md_regs;
ft = frame->f_format;
oonstack = psp->ps_sigstk.ss_flags & SS_ONSTACK;
/*
* Allocate and validate space for the signal handler
* context. Note that if the stack is in P0 space, the
* call to grow() is a nop, and the useracc() check
* will fail if the process has not already allocated
* the space with a `brk'.
*/
fsize = sizeof(struct sigframe);
if ((psp->ps_flags & SAS_ALTSTACK) && !oonstack &&
(psp->ps_sigonstack & sigmask(sig))) {
fp = (struct sigframe *)(psp->ps_sigstk.ss_sp +
psp->ps_sigstk.ss_size - fsize);
psp->ps_sigstk.ss_flags |= SS_ONSTACK;
} else
fp = (struct sigframe *)(frame->f_regs[SP] - fsize);
if ((unsigned)fp <= USRSTACK - ctob(p->p_vmspace->vm_ssize))
(void)grow(p, (unsigned)fp);
#ifdef DEBUG
if ((sigdebug & SDB_KSTACK) && p->p_pid == sigpid)
printf("sendsig(%d): sig %d ssp %x usp %x scp %x ft %d\n",
p->p_pid, sig, &oonstack, fp, &fp->sf_sc, ft);
#endif
if (useracc((caddr_t)fp, fsize, B_WRITE) == 0) {
#ifdef DEBUG
if ((sigdebug & SDB_KSTACK) && p->p_pid == sigpid)
printf("sendsig(%d): useracc failed on sig %d\n",
p->p_pid, sig);
#endif
/*
* Process has trashed its stack; give it an illegal
* instruction to halt it in its tracks.
*/
SIGACTION(p, SIGILL) = SIG_DFL;
sig = sigmask(SIGILL);
p->p_sigignore &= ~sig;
p->p_sigcatch &= ~sig;
p->p_sigmask &= ~sig;
psignal(p, SIGILL);
return;
}
kfp = (struct sigframe *)malloc((u_long)fsize, M_TEMP, M_WAITOK);
/*
* Build the argument list for the signal handler.
*/
kfp->sf_signum = sig;
kfp->sf_code = code;
kfp->sf_scp = &fp->sf_sc;
kfp->sf_handler = catcher;
/*
* Save necessary hardware state. Currently this includes:
* - general registers
* - original exception frame (if not a "normal" frame)
* - FP coprocessor state
*/
kfp->sf_state.ss_flags = SS_USERREGS;
bcopy((caddr_t)frame->f_regs,
(caddr_t)kfp->sf_state.ss_frame.f_regs, sizeof frame->f_regs);
if (ft >= FMT7) {
#ifdef DEBUG
if (ft > 15 || exframesize[ft] < 0)
panic("sendsig: bogus frame type");
#endif
kfp->sf_state.ss_flags |= SS_RTEFRAME;
kfp->sf_state.ss_frame.f_format = frame->f_format;
kfp->sf_state.ss_frame.f_vector = frame->f_vector;
bcopy((caddr_t)&frame->F_u,
(caddr_t)&kfp->sf_state.ss_frame.F_u,
(size_t) exframesize[ft]);
/*
* Leave an indicator that we need to clean up the kernel
* stack. We do this by setting the "pad word" above the
* hardware stack frame to the amount the stack must be
* adjusted by.
*
* N.B. we increment rather than just set f_stackadj in
* case we are called from syscall when processing a
* sigreturn. In that case, f_stackadj may be non-zero.
*/
frame->f_stackadj += exframesize[ft];
frame->f_format = frame->f_vector = 0;
#ifdef DEBUG
if (sigdebug & SDB_FOLLOW)
printf("sendsig(%d): copy out %d of frame %d\n",
p->p_pid, exframesize[ft], ft);
#endif
}
if (fpu_type) {
kfp->sf_state.ss_flags |= SS_FPSTATE;
m68881_save(&kfp->sf_state.ss_fpstate);
}
#ifdef DEBUG
if ((sigdebug & SDB_FPSTATE) && *(char *)&kfp->sf_state.ss_fpstate)
printf("sendsig(%d): copy out FP state (%x) to %x\n",
p->p_pid, *(u_int *)&kfp->sf_state.ss_fpstate,
&kfp->sf_state.ss_fpstate);
#endif
/*
* Build the signal context to be used by sigreturn.
*/
kfp->sf_sc.sc_onstack = oonstack;
kfp->sf_sc.sc_mask = mask;
kfp->sf_sc.sc_sp = frame->f_regs[SP];
kfp->sf_sc.sc_fp = frame->f_regs[A6];
kfp->sf_sc.sc_ap = (int)&fp->sf_state;
kfp->sf_sc.sc_pc = frame->f_pc;
kfp->sf_sc.sc_ps = frame->f_sr;
(void) copyout((caddr_t)kfp, (caddr_t)fp, fsize);
frame->f_regs[SP] = (int)fp;
#ifdef DEBUG
if (sigdebug & SDB_FOLLOW)
printf("sendsig(%d): sig %d scp %x fp %x sc_sp %x sc_ap %x\n",
p->p_pid, sig, kfp->sf_scp, fp,
kfp->sf_sc.sc_sp, kfp->sf_sc.sc_ap);
#endif
/*
* Signal trampoline code is at base of user stack.
*/
frame->f_pc = (int)PS_STRINGS - (esigcode - sigcode);
#ifdef DEBUG
if ((sigdebug & SDB_KSTACK) && p->p_pid == sigpid)
printf("sendsig(%d): sig %d returns\n",
p->p_pid, sig);
#endif
free((caddr_t)kfp, M_TEMP);
}
/*
* System call to cleanup state after a signal
* has been taken. Reset signal mask and
* stack state from context left by sendsig (above).
* Return to previous pc and psl as specified by
* context left by sendsig. Check carefully to
* make sure that the user has not modified the
* psl to gain improper priviledges or to cause
* a machine fault.
*/
int
sys_sigreturn(p, v, retval)
struct proc *p;
void *v;
register_t *retval;
{
struct sys_sigreturn_args *uap = v;
register struct sigcontext *scp;
register struct frame *frame;
register int rf;
struct sigcontext tsigc;
struct sigstate tstate;
int flags;
scp = SCARG(uap, sigcntxp);
#ifdef DEBUG
if (sigdebug & SDB_FOLLOW)
printf("sigreturn: pid %d, scp %x\n", p->p_pid, scp);
#endif
if ((int)scp & 1)
return (EINVAL);
/*
* Test and fetch the context structure.
* We grab it all at once for speed.
*/
if (useracc((caddr_t)scp, sizeof (*scp), B_WRITE) == 0 ||
copyin((caddr_t)scp, (caddr_t)&tsigc, sizeof tsigc))
return (EINVAL);
scp = &tsigc;
if ((scp->sc_ps & (PSL_MBZ|PSL_IPL|PSL_S)) != 0)
return (EINVAL);
/*
* Restore the user supplied information
*/
if (scp->sc_onstack & 01)
p->p_sigacts->ps_sigstk.ss_flags |= SS_ONSTACK;
else
p->p_sigacts->ps_sigstk.ss_flags &= ~SS_ONSTACK;
p->p_sigmask = scp->sc_mask &~ sigcantmask;
frame = (struct frame *) p->p_md.md_regs;
frame->f_regs[SP] = scp->sc_sp;
frame->f_regs[A6] = scp->sc_fp;
frame->f_pc = scp->sc_pc;
frame->f_sr = scp->sc_ps;
/*
* Grab pointer to hardware state information.
* If zero, the user is probably doing a longjmp.
*/
if ((rf = scp->sc_ap) == 0)
return (EJUSTRETURN);
/*
* See if there is anything to do before we go to the
* expense of copying in close to 1/2K of data
*/
flags = fuword((caddr_t)rf);
#ifdef DEBUG
if (sigdebug & SDB_FOLLOW)
printf("sigreturn(%d): sc_ap %x flags %x\n",
p->p_pid, rf, flags);
#endif
/*
* fuword failed (bogus sc_ap value).
*/
if (flags == -1)
return (EINVAL);
if (flags == 0 || copyin((caddr_t)rf, (caddr_t)&tstate, sizeof tstate))
return (EJUSTRETURN);
#ifdef DEBUG
if ((sigdebug & SDB_KSTACK) && p->p_pid == sigpid)
printf("sigreturn(%d): ssp %x usp %x scp %x ft %d\n",
p->p_pid, &flags, scp->sc_sp, SCARG(uap, sigcntxp),
(flags&SS_RTEFRAME) ? tstate.ss_frame.f_format : -1);
#endif
/*
* Restore most of the users registers except for A6 and SP
* which were handled above.
*/
if (flags & SS_USERREGS)
bcopy((caddr_t)tstate.ss_frame.f_regs,
(caddr_t)frame->f_regs, sizeof(frame->f_regs)-2*NBPW);
/*
* Restore long stack frames. Note that we do not copy
* back the saved SR or PC, they were picked up above from
* the sigcontext structure.
*/
if (flags & SS_RTEFRAME) {
register int sz;
/* grab frame type and validate */
sz = tstate.ss_frame.f_format;
if (sz > 15 || (sz = exframesize[sz]) < 0)
return (EINVAL);
frame->f_stackadj -= sz;
frame->f_format = tstate.ss_frame.f_format;
frame->f_vector = tstate.ss_frame.f_vector;
bcopy((caddr_t)&tstate.ss_frame.F_u, (caddr_t)&frame->F_u, sz);
#ifdef DEBUG
if (sigdebug & SDB_FOLLOW)
printf("sigreturn(%d): copy in %d of frame type %d\n",
p->p_pid, sz, tstate.ss_frame.f_format);
#endif
}
/*
* Finally we restore the original FP context
*/
if (flags & SS_FPSTATE)
m68881_restore(&tstate.ss_fpstate);
#ifdef DEBUG
if ((sigdebug & SDB_FPSTATE) && *(char *)&tstate.ss_fpstate)
printf("sigreturn(%d): copied in FP state (%x) at %x\n",
p->p_pid, *(u_int *)&tstate.ss_fpstate,
&tstate.ss_fpstate);
if ((sigdebug & SDB_FOLLOW) ||
((sigdebug & SDB_KSTACK) && p->p_pid == sigpid))
printf("sigreturn(%d): returns\n", p->p_pid);
#endif
return (EJUSTRETURN);
}
/*
* Do a sync in preparation for a reboot.
* XXX - This could probably be common code.
* XXX - And now, most of it is in vfs_shutdown()
* XXX - Put waittime checks in there too?
*/
int waittime = -1; /* XXX - Who else looks at this? -gwr */
static void
reboot_sync __P((void))
{
/* Check waittime here to localize its use to this function. */
if (waittime >= 0)
return;
waittime = 0;
vfs_shutdown();
}
/*
* Common part of the BSD and SunOS reboot system calls.
* XXX - Should be named: cpu_reboot maybe? -gwr
*/
__dead void
boot(howto, user_boot_string)
int howto;
char *user_boot_string;
{
/* Note: this string MUST be static! */
static char bootstr[128];
char *p;
/* If system is cold, just halt. (early panic?) */
if (cold)
goto haltsys;
if ((howto & RB_NOSYNC) == 0) {
reboot_sync();
/*
* If we've been adjusting the clock, the todr
* will be out of synch; adjust it now.
*
* XXX - However, if the kernel has been sitting in ddb,
* the time will be way off, so don't set the HW clock!
* XXX - Should do sanity check against HW clock. -gwr
*/
/* resettodr(); */
}
/* Disable interrupts. */
splhigh();
/* Write out a crash dump if asked. */
if (howto & RB_DUMP)
dumpsys();
/* run any shutdown hooks */
doshutdownhooks();
if (howto & RB_HALT) {
haltsys:
printf("Kernel halted.\n");
sun3x_mon_halt();
}
/*
* Automatic reboot.
*/
if (user_boot_string)
strncpy(bootstr, user_boot_string, sizeof(bootstr));
else {
/*
* Build our own boot string with an empty
* boot device/file and (maybe) some flags.
* The PROM will supply the device/file name.
*/
p = bootstr;
*p = '\0';
if (howto & (RB_KDB|RB_ASKNAME|RB_SINGLE)) {
/* Append the boot flags. */
*p++ = ' ';
*p++ = '-';
if (howto & RB_KDB)
*p++ = 'd';
if (howto & RB_ASKNAME)
*p++ = 'a';
if (howto & RB_SINGLE)
*p++ = 's';
*p = '\0';
}
}
printf("Kernel rebooting...\n");
sun3x_mon_reboot(bootstr);
for (;;) ;
/*NOTREACHED*/
}
/*
* These variables are needed by /sbin/savecore
*/
u_long dumpmag = 0x8fca0101; /* magic number */
int dumpsize = 0; /* pages */
long dumplo = 0; /* blocks */
/*
* This is called by cpu_startup to set dumplo, dumpsize.
* Dumps always skip the first CLBYTES of disk space
* in case there might be a disk label stored there.
* If there is extra space, put dump at the end to
* reduce the chance that swapping trashes it.
*/
void
dumpconf()
{
int nblks; /* size of dump area */
int maj;
int (*getsize)__P((dev_t));
if (dumpdev == NODEV)
return;
maj = major(dumpdev);
if (maj < 0 || maj >= nblkdev)
panic("dumpconf: bad dumpdev=0x%x", dumpdev);
getsize = bdevsw[maj].d_psize;
if (getsize == NULL)
return;
nblks = (*getsize)(dumpdev);
if (nblks <= ctod(1))
return;
/* Position dump image near end of space, page aligned. */
dumpsize = physmem; /* pages */
dumplo = nblks - ctod(dumpsize);
dumplo &= ~(ctod(1)-1);
/* If it does not fit, truncate it by moving dumplo. */
/* Note: Must force signed comparison. */
if (dumplo < ((long)ctod(1))) {
dumplo = ctod(1);
dumpsize = dtoc(nblks - dumplo);
}
}
struct pcb dumppcb;
extern vm_offset_t avail_start;
/*
* Write a crash dump. The format while in swap is:
* kcore_seg_t cpu_hdr;
* cpu_kcore_hdr_t cpu_data;
* padding (NBPG-sizeof(kcore_seg_t))
* pagemap (2*NBPG)
* physical memory...
*/
void
dumpsys()
{
struct bdevsw *dsw;
char *vaddr;
vm_offset_t paddr;
int psize, todo, chunk;
daddr_t blkno;
int error = 0;
msgbufmapped = 0;
if (dumpdev == NODEV)
return;
/*
* For dumps during autoconfiguration,
* if dump device has already configured...
*/
if (dumpsize == 0)
dumpconf();
if (dumplo <= 0)
return;
savectx(&dumppcb);
dsw = &bdevsw[major(dumpdev)];
psize = (*(dsw->d_psize))(dumpdev);
if (psize == -1) {
printf("dump area unavailable\n");
return;
}
printf("\ndumping to dev %x, offset %d\n",
(int) dumpdev, (int) dumplo);
/*
* Write the dump header, including MMU state.
*/
blkno = dumplo;
todo = dumpsize; /* pages */
/*
* Now dump physical memory. Have to do it in two chunks.
* The first chunk is "unmanaged" (by the VM code) and its
* range of physical addresses is not allow in pmap_enter.
* However, that segment is mapped linearly, so we can just
* use the virtual mappings already in place. The second
* chunk is done the normal way, using pmap_enter.
*
* Note that vaddr==(paddr+KERNBASE) for paddr=0 through etext.
*/
/* Do the first chunk (0 <= PA < avail_start) */
paddr = 0;
chunk = btoc(avail_start);
if (chunk > todo)
chunk = todo;
do {
if ((todo & 0xf) == 0)
printf("\r%4d", todo);
vaddr = (char*)(paddr + KERNBASE);
error = (*dsw->d_dump)(dumpdev, blkno, vaddr, NBPG);
if (error)
goto fail;
paddr += NBPG;
blkno += btodb(NBPG);
--todo;
} while (--chunk > 0);
/* Do the second chunk (avail_start <= PA < dumpsize) */
vaddr = (char*)vmmap; /* Borrow /dev/mem VA */
do {
if ((todo & 0xf) == 0)
printf("\r%4d", todo);
pmap_enter(pmap_kernel(), vmmap, paddr | PMAP_NC,
VM_PROT_READ, FALSE);
error = (*dsw->d_dump)(dumpdev, blkno, vaddr, NBPG);
pmap_remove(pmap_kernel(), vmmap, vmmap + NBPG);
if (error)
goto fail;
paddr += NBPG;
blkno += btodb(NBPG);
} while (--todo > 0);
printf("\rdump succeeded\n");
return;
fail:
printf(" dump error=%d\n", error);
}
static void
initcpu()
{
/* XXX: Enable RAM parity/ECC checking? */
/* XXX: parityenable(); */
nofault = NULL; /* XXX - needed? */
#ifdef HAVECACHE
cache_enable();
#endif
}
/* called from locore.s */
void straytrap __P((struct trapframe));
void
straytrap(frame)
struct trapframe frame;
{
printf("unexpected trap; vector=0x%x at pc=0x%x\n",
frame.tf_vector, frame.tf_pc);
#ifdef DDB
kdb_trap(-1, (db_regs_t *) &frame);
#endif
}
/* from hp300: badaddr() */
/* peek_byte(), peek_word() moved to autoconf.c */
/* XXX: parityenable() ? */
static void dumpmem __P((int *, int, int));
static char *hexstr __P((int, int));
/*
* Print a register and stack dump.
*/
void
regdump(fp, sbytes)
struct frame *fp; /* must not be register */
int sbytes;
{
static int doingdump = 0;
register int i;
int s;
if (doingdump)
return;
s = splhigh();
doingdump = 1;
printf("pid = %d, pc = %s, ",
curproc ? curproc->p_pid : -1, hexstr(fp->f_pc, 8));
printf("ps = %s, ", hexstr(fp->f_sr, 4));
printf("sfc = %s, ", hexstr(getsfc(), 4));
printf("dfc = %s\n", hexstr(getdfc(), 4));
printf("Registers:\n ");
for (i = 0; i < 8; i++)
printf(" %d", i);
printf("\ndreg:");
for (i = 0; i < 8; i++)
printf(" %s", hexstr(fp->f_regs[i], 8));
printf("\nareg:");
for (i = 0; i < 8; i++)
printf(" %s", hexstr(fp->f_regs[i+8], 8));
if (sbytes > 0) {
if (fp->f_sr & PSL_S) {
printf("\n\nKernel stack (%s):",
hexstr((int)(((int *)&fp)-1), 8));
dumpmem(((int *)&fp)-1, sbytes, 0);
} else {
printf("\n\nUser stack (%s):", hexstr(fp->f_regs[SP], 8));
dumpmem((int *)fp->f_regs[SP], sbytes, 1);
}
}
doingdump = 0;
splx(s);
}
#define KSADDR ((int *)((u_int)curproc->p_addr + USPACE - NBPG))
static void
dumpmem(ptr, sz, ustack)
register int *ptr;
int sz, ustack;
{
register int i, val;
for (i = 0; i < sz; i++) {
if ((i & 7) == 0)
printf("\n%s: ", hexstr((int)ptr, 6));
else
printf(" ");
if (ustack == 1) {
if ((val = fuword(ptr++)) == -1)
break;
} else {
if (ustack == 0 &&
(ptr < KSADDR || ptr > KSADDR+(NBPG/4-1)))
break;
val = *ptr++;
}
printf("%s", hexstr(val, 8));
}
printf("\n");
}
static char *
hexstr(val, len)
register int val;
int len;
{
static char nbuf[9];
register int x, i;
if (len > 8)
return("");
nbuf[len] = '\0';
for (i = len-1; i >= 0; --i) {
x = val & 0xF;
/* Isn't this a cool trick? */
nbuf[i] = "0123456789ABCDEF"[x];
val >>= 4;
}
return(nbuf);
}
/*
* cpu_exec_aout_makecmds():
* cpu-dependent a.out format hook for execve().
*
* Determine if the given exec package refers to something which we
* understand and, if so, set up the vmcmds for it.
*/
int
cpu_exec_aout_makecmds(p, epp)
struct proc *p;
struct exec_package *epp;
{
int error = ENOEXEC;
#ifdef COMPAT_SUNOS
extern sunos_exec_aout_makecmds
__P((struct proc *, struct exec_package *));
if ((error = sunos_exec_aout_makecmds(p, epp)) == 0)
return 0;
#endif
return error;
}