static char *hexstr __P((int, int)); static void reboot_sync __P((void)); int  reboot2 __P((int, char *));   void straytrap __P((struct trapframe));	   void consinit() {  cninit();  #ifdef KGDB  zs_kgdb_init();		  if (boothowto & RB_KDB) kgdb_connect(1); #endif #ifdef DDB  db_machine_init(); ddb_init(); if (boothowto & RB_KDB) Debugger(); #endif }    #define	valloc(name, type, num) \ v = (caddr_t)(((name) = (type *)v) + (num))  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);  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   if (bufpages == 0) {  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;	 if (nswbuf > 256) nswbuf = 256;		 } valloc(swbuf, struct buf, nswbuf); valloc(buf, struct buf, nbuf); return v; } #undef	valloc   void cpu_startup() { caddr_t v; int sz, i; vm_size_t size; int base, residual; vm_offset_t minaddr, maxaddr;     printf(version); identifycpu(); initfpu();	  printf("real mem = %d\n", ctob(physmem));   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");   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)) {  bufpages = btoc(MAXBSIZE) * nbuf; } base = bufpages / nbuf; residual = bufpages % nbuf; for (i = 0; i < nbuf; i++) { vm_size_t curbufsize; vm_offset_t curbuf;   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); }   exec_map = kmem_suballoc(kernel_map, &minaddr, &maxaddr, 16*NCARGS, TRUE);     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);   callfree = callout; for (i = 1; i < ncallout; i++) callout[i-1].c_next = &callout[i]; callout[i-1].c_next = NULL;  printf("avail mem = %ld\n", ptoa(cnt.v_free_count)); printf("using %d buffers containing %d bytes of memory\n", nbuf, bufpages * CLBYTES);   vmmap = kmem_alloc_wait(kernel_map, NBPG);   dvma_init();   initcpu();   bufinit();   if (boothowto & RB_CONFIG) { #ifdef BOOT_CONFIG user_config(); #else printf("kernel does not support -c; continuing..\n"); #endif } configure(); }   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;   p->p_addr->u_pcb.pcb_fpregs.fpf_null = 0; if (fpu_type) { m68881_restore(&p->p_addr->u_pcb.pcb_fpregs); }  }   char	machine[] = "sun3";		 char	cpu_model[120]; extern	long hostid;  static void identifycpu() {  strcpy(cpu_model, "Sun 3/");   strcat(cpu_model, cpu_string);  printf("Model: %s (hostid %lx)\n", cpu_model, hostid); }   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; { dev_t consdev;   if (namelen != 1) return (ENOTDIR);		  switch (name[0]) { case CPU_CONSDEV: if (cn_tab != NULL) consdev = cn_tab->cn_dev; else consdev = NODEV; return (sysctl_rdstruct(oldp, oldlenp, newp, &consdev, sizeof consdev)); default: return (EOPNOTSUPP); }  }  #define SS_RTEFRAME	1 #define SS_FPSTATE	2 #define SS_USERREGS	4  struct sigstate { int	ss_flags;		 struct	frame ss_frame;		 struct	fpframe ss_fpstate;	 };   struct sigframe { int	sf_signum;		 int	sf_code;		 struct	sigcontext *sf_scp;	 sig_t	sf_handler;		 struct	sigstate sf_state;	 struct	sigcontext sf_sc;	 };  #ifdef DEBUG int sigdebug = 0; int sigpid = 0; #define SDB_FOLLOW	0x01 #define SDB_KSTACK	0x02 #define SDB_FPSTATE	0x04 #endif   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;   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  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);  kfp->sf_signum = sig; kfp->sf_code = code; kfp->sf_scp = &fp->sf_sc; kfp->sf_handler = catcher;  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]);  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   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  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); }   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);   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);  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;   if ((rf = scp->sc_ap) == 0) return (EJUSTRETURN);  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  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  if (flags & SS_USERREGS) bcopy((caddr_t)tstate.ss_frame.f_regs, (caddr_t)frame->f_regs, sizeof(frame->f_regs)-2*NBPW);  if (flags & SS_RTEFRAME) { register int sz;   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 }   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); }    int waittime = -1;	 static void reboot_sync() { extern struct proc proc0;   if (waittime >= 0) return;  if (curproc == NULL) curproc = &proc0; waittime = 0; vfs_shutdown(); }   int reboot2(howto, user_boot_string) int howto; char *user_boot_string; { char *bs, *p; char default_boot_string[8];   if (cold) goto haltsys;  if ((howto & RB_NOSYNC) == 0) { reboot_sync();   }   splhigh();   if (howto & RB_DUMP) dumpsys();   doshutdownhooks();  if (howto & RB_HALT) { haltsys: printf("Kernel halted.\n"); sun3_mon_halt(); }   bs = user_boot_string; if (bs == NULL) {  bs = default_boot_string; *bs = '\0'; if (howto & (RB_KDB|RB_ASKNAME|RB_SINGLE)) {  p = bs; *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"); sun3_mon_reboot(bs); for (;;) ;  }   void boot(howto) int howto; { (void) reboot2(howto, NULL); for(;;);  }   u_long	dumpmag = 0x8fca0101;	 int 	dumpsize = 0;		 long	dumplo = 0; 		   vm_offset_t dumppage_va; vm_offset_t dumppage_pa;  #define		DUMP_EXTRA	3	   void dumpconf() { int nblks;	 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;   dumpsize = physmem + DUMP_EXTRA;  dumplo = nblks - ctod(dumpsize); dumplo &= ~(ctod(1)-1);    if (dumplo < ((long)ctod(1))) { dumplo = ctod(1); dumpsize = dtoc(nblks - dumplo); } }  struct pcb dumppcb; extern vm_offset_t avail_start;   void dumpsys() { struct bdevsw *dsw; kcore_seg_t	*kseg_p; cpu_kcore_hdr_t	*chdr_p; char *vaddr; vm_offset_t paddr; int psize, todo, chunk; daddr_t blkno; int error = 0;  msgbufmapped = 0; if (dumpdev == NODEV) return; if (dumppage_va == 0) return;   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 %ld\n", dumpdev, dumplo);   blkno = dumplo; todo = dumpsize - DUMP_EXTRA;	 vaddr = (char *)dumppage_va; bzero(vaddr, NBPG);   kseg_p = (kcore_seg_t *)vaddr; CORE_SETMAGIC(*kseg_p, KCORE_MAGIC, MID_MACHINE, CORE_CPU); kseg_p->c_size = (ctob(DUMP_EXTRA) - sizeof(kcore_seg_t));   chdr_p = (cpu_kcore_hdr_t *) (kseg_p + 1); pmap_get_ksegmap(chdr_p->ksegmap); error = (*dsw->d_dump)(dumpdev, blkno, vaddr, NBPG); if (error) goto fail; blkno += btodb(NBPG);   pmap_get_pagemap((int *)vaddr, 0); error = (*dsw->d_dump)(dumpdev, blkno, vaddr, NBPG); if (error) goto fail; blkno += btodb(NBPG);   pmap_get_pagemap((int *)vaddr, NBPG); error = (*dsw->d_dump)(dumpdev, blkno, vaddr, NBPG); if (error) goto fail; blkno += btodb(NBPG);     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);   vaddr = (char*)vmmap;	 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); }  void initcpu() {    nofault = NULL;	  #ifdef	HAVECACHE cache_enable(); #endif }  void straytrap(frame) struct trapframe frame; { printf("unexpected trap; vector offset 0x%x from 0x%x\n", frame.tf_vector, frame.tf_pc); #ifdef	DDB  kdb_trap(-1, (struct mc68020_saved_state *) &frame); #endif }     void regdump(fp, sbytes) struct frame *fp;  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"); }  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);}