1 /* 2 * Copyright (c) 1988 University of Utah. 3 * Copyright (c) 1992 The Regents of the University of California. 4 * All rights reserved. 5 * 6 * This code is derived from software contributed to Berkeley by 7 * the Systems Programming Group of the University of Utah Computer 8 * Science Department, The Mach Operating System project at 9 * Carnegie-Mellon University, Ralph Campbell, Sony Corp. and Kazumasa 10 * Utashiro of Software Research Associates, Inc. 11 * 12 * %sccs.include.redist.c% 13 * 14 * @(#)machdep.c 7.7 (Berkeley) 01/20/93 15 */ 16 17 /* from: Utah $Hdr: machdep.c 1.63 91/04/24$ */ 18 19 #include <sys/param.h> 20 #include <sys/systm.h> 21 #include <sys/signalvar.h> 22 #include <sys/kernel.h> 23 #include <sys/map.h> 24 #include <sys/proc.h> 25 #include <sys/buf.h> 26 #include <sys/reboot.h> 27 #include <sys/conf.h> 28 #include <sys/file.h> 29 #include <sys/clist.h> 30 #include <sys/callout.h> 31 #include <sys/malloc.h> 32 #include <sys/mbuf.h> 33 #include <sys/msgbuf.h> 34 #include <sys/user.h> 35 #include <sys/exec.h> 36 #ifdef SYSVSHM 37 #include <sys/shm.h> 38 #endif 39 40 #include <vm/vm.h> 41 #include <vm/vm_kern.h> 42 #include <vm/vm_page.h> 43 44 #include <machine/cpu.h> 45 #include <machine/reg.h> 46 #include <machine/psl.h> 47 #include <machine/pte.h> 48 49 #include <machine/adrsmap.h> 50 51 vm_map_t buffer_map; 52 53 /* 54 * Declare these as initialized data so we can patch them. 55 */ 56 int nswbuf = 0; 57 #ifdef NBUF 58 int nbuf = NBUF; 59 #else 60 int nbuf = 0; 61 #endif 62 #ifdef BUFPAGES 63 int bufpages = BUFPAGES; 64 #else 65 int bufpages = 0; 66 #endif 67 int msgbufmapped = 0; /* set when safe to use msgbuf */ 68 int maxmem; /* max memory per process */ 69 int physmem; /* max supported memory, changes to actual */ 70 /* 71 * safepri is a safe priority for sleep to set for a spin-wait 72 * during autoconfiguration or after a panic. 73 */ 74 int safepri = PSL_LOWIPL; 75 76 struct user *proc0paddr; 77 struct proc nullproc; /* for use by swtch_exit() */ 78 79 /* 80 * Do all the stuff that locore normally does before calling main(). 81 * Process arguments passed to us by the prom monitor. 82 * Return the first page address following the system. 83 */ 84 mach_init(x_boothowto, x_unkown, x_bootdev, x_maxmem) 85 int x_boothowto; 86 int x_unkown; 87 int x_bootdev; 88 int x_maxmem; 89 { 90 register char *cp; 91 register int i; 92 register unsigned firstaddr; 93 register caddr_t v; 94 caddr_t start; 95 extern u_long bootdev; 96 extern char edata[], end[]; 97 extern char MachUTLBMiss[], MachUTLBMissEnd[]; 98 extern char MachException[], MachExceptionEnd[]; 99 #ifdef ATTR 100 extern char *pmap_attributes; 101 #endif 102 103 /* clear the BSS segment */ 104 v = (caddr_t)pmax_round_page(end); 105 bzero(edata, v - edata); 106 107 boothowto = x_boothowto; 108 bootdev = x_bootdev; 109 maxmem = physmem = pmax_btop(x_maxmem); 110 111 /* 112 * Look at arguments passed to us and compute boothowto. 113 */ 114 #ifdef GENERIC 115 boothowto |= RB_SINGLE | RB_ASKNAME; 116 #endif 117 #ifdef KADB 118 boothowto |= RB_KDB; 119 #endif 120 121 #ifdef MFS 122 /* 123 * Check to see if a mini-root was loaded into memory. It resides 124 * at the start of the next page just after the end of BSS. 125 */ 126 if (boothowto & RB_MINIROOT) 127 v += mfs_initminiroot(v); 128 #endif 129 130 /* 131 * Init mapping for u page(s) for proc[0], pm_tlbpid 1. 132 */ 133 start = v; 134 curproc->p_addr = proc0paddr = (struct user *)v; 135 curproc->p_md.md_regs = proc0paddr->u_pcb.pcb_regs; 136 firstaddr = MACH_CACHED_TO_PHYS(v); 137 for (i = 0; i < UPAGES; i++) { 138 MachTLBWriteIndexed(i, 139 (UADDR + (i << PGSHIFT)) | (1 << VMMACH_TLB_PID_SHIFT), 140 curproc->p_md.md_upte[i] = firstaddr | PG_V | PG_M); 141 firstaddr += NBPG; 142 } 143 v += UPAGES * NBPG; 144 MachSetPID(1); 145 146 /* 147 * init nullproc for swtch_exit(). 148 * init mapping for u page(s), pm_tlbpid 0 149 * This could be used for an idle process. 150 */ 151 nullproc.p_addr = (struct user *)v; 152 nullproc.p_md.md_regs = ((struct user *)v)->u_pcb.pcb_regs; 153 for (i = 0; i < UPAGES; i++) { 154 nullproc.p_md.md_upte[i] = firstaddr | PG_V | PG_M; 155 firstaddr += NBPG; 156 } 157 v += UPAGES * NBPG; 158 159 /* clear pages for u areas */ 160 bzero(start, v - start); 161 162 /* 163 * Copy down exception vector code. 164 */ 165 if (MachUTLBMissEnd - MachUTLBMiss > 0x80) 166 panic("startup: UTLB code too large"); 167 bcopy(MachUTLBMiss, (char *)MACH_UTLB_MISS_EXC_VEC, 168 MachUTLBMissEnd - MachUTLBMiss); 169 bcopy(MachException, (char *)MACH_GEN_EXC_VEC, 170 MachExceptionEnd - MachException); 171 172 /* 173 * Clear out the I and D caches. 174 */ 175 MachConfigCache(); 176 MachFlushCache(); 177 178 /* 179 * Initialize error message buffer (at end of core). 180 */ 181 maxmem -= btoc(sizeof (struct msgbuf)); 182 msgbufp = (struct msgbuf *)(MACH_PHYS_TO_CACHED(maxmem << PGSHIFT)); 183 msgbufmapped = 1; 184 185 /* 186 * Allocate space for system data structures. 187 * The first available kernel virtual address is in "v". 188 * As pages of kernel virtual memory are allocated, "v" is incremented. 189 * 190 * These data structures are allocated here instead of cpu_startup() 191 * because physical memory is directly addressable. We don't have 192 * to map these into virtual address space. 193 */ 194 start = v; 195 196 #define valloc(name, type, num) \ 197 (name) = (type *)v; v = (caddr_t)((name)+(num)) 198 #define valloclim(name, type, num, lim) \ 199 (name) = (type *)v; v = (caddr_t)((lim) = ((name)+(num))) 200 valloc(cfree, struct cblock, nclist); 201 valloc(callout, struct callout, ncallout); 202 valloc(swapmap, struct map, nswapmap = maxproc * 2); 203 #ifdef SYSVSHM 204 valloc(shmsegs, struct shmid_ds, shminfo.shmmni); 205 #endif 206 #ifdef ATTR 207 /* this is allocated here just to save a few bytes */ 208 valloc(pmap_attributes, char, physmem); 209 #endif 210 211 /* 212 * Determine how many buffers to allocate. 213 * We allocate more buffer space than the BSD standard of 214 * using 10% of memory for the first 2 Meg, 5% of remaining. 215 * We just allocate a flat 10%. Insure a minimum of 16 buffers. 216 * We allocate 1/2 as many swap buffer headers as file i/o buffers. 217 */ 218 if (bufpages == 0) 219 bufpages = physmem / 10 / CLSIZE; 220 if (nbuf == 0) { 221 nbuf = bufpages; 222 if (nbuf < 16) 223 nbuf = 16; 224 } 225 if (nswbuf == 0) { 226 nswbuf = (nbuf / 2) &~ 1; /* force even */ 227 if (nswbuf > 256) 228 nswbuf = 256; /* sanity */ 229 } 230 valloc(swbuf, struct buf, nswbuf); 231 valloc(buf, struct buf, nbuf); 232 233 /* 234 * Clear allocated memory. 235 */ 236 v = (caddr_t)pmax_round_page(v); 237 bzero(start, v - start); 238 239 /* 240 * Initialize the virtual memory system. 241 */ 242 pmap_bootstrap((vm_offset_t)MACH_CACHED_TO_PHYS(v)); 243 } 244 245 /* 246 * Console initialization: called early on from main, 247 * before vm init or startup. Do enough configuration 248 * to choose and initialize a console. 249 * XXX need something better here. 250 */ 251 #define SCC_CONSOLE 0 252 #define SW_CONSOLE 0x07 253 #define SW_NWB512 0x04 254 #define SW_NWB225 0x01 255 #define SW_FBPOP 0x02 256 #define SW_FBPOP1 0x06 257 #define SW_FBPOP2 0x03 258 #define SW_AUTOSEL 0x07 259 consinit() 260 { 261 extern dev_t consdev; 262 extern struct tty *constty, *cn_tty, *rs_tty; 263 int dipsw = (int)*(volatile u_char *)DIP_SWITCH; 264 265 #include "bm.h" 266 #if NBM > 0 267 #if defined(news3200) || defined(news3400) /* KU:XXX */ 268 fbbm_probe(dipsw|2); 269 #else 270 fbbm_probe(dipsw); 271 #endif 272 vt100_open(); 273 setup_fnt(); 274 setup_fnt24(); 275 #else 276 dipsw &= SW_CONSOLE; 277 #endif 278 279 switch (dipsw & SW_CONSOLE) { 280 case 0: 281 scc_open(SCC_CONSOLE); 282 consdev = makedev(1, 0); 283 constty = rs_tty; 284 break; 285 286 default: 287 #if NBM > 0 288 consdev = makedev(22, 0); 289 constty = cn_tty; 290 #endif 291 break; 292 } 293 return(0); 294 } 295 296 /* 297 * cpu_startup: allocate memory for variable-sized tables, 298 * initialize cpu, and do autoconfiguration. 299 */ 300 cpu_startup() 301 { 302 register unsigned i; 303 register caddr_t v; 304 int base, residual; 305 extern long Usrptsize; 306 extern struct map *useriomap; 307 #ifdef DEBUG 308 extern int pmapdebug; 309 int opmapdebug = pmapdebug; 310 #endif 311 vm_offset_t minaddr, maxaddr; 312 vm_size_t size; 313 314 #ifdef DEBUG 315 pmapdebug = 0; 316 #endif 317 318 /* 319 * Good {morning,afternoon,evening,night}. 320 */ 321 printf(version); 322 printf("real mem = %d\n", ctob(physmem)); 323 324 /* 325 * Allocate virtual address space for file I/O buffers. 326 * Note they are different than the array of headers, 'buf', 327 * and usually occupy more virtual memory than physical. 328 */ 329 size = MAXBSIZE * nbuf; 330 buffer_map = kmem_suballoc(kernel_map, (vm_offset_t *)&buffers, 331 &maxaddr, size, FALSE); 332 minaddr = (vm_offset_t)buffers; 333 if (vm_map_find(buffer_map, vm_object_allocate(size), (vm_offset_t)0, 334 &minaddr, size, FALSE) != KERN_SUCCESS) 335 panic("startup: cannot allocate buffers"); 336 base = bufpages / nbuf; 337 residual = bufpages % nbuf; 338 for (i = 0; i < nbuf; i++) { 339 vm_size_t curbufsize; 340 vm_offset_t curbuf; 341 342 /* 343 * First <residual> buffers get (base+1) physical pages 344 * allocated for them. The rest get (base) physical pages. 345 * 346 * The rest of each buffer occupies virtual space, 347 * but has no physical memory allocated for it. 348 */ 349 curbuf = (vm_offset_t)buffers + i * MAXBSIZE; 350 curbufsize = CLBYTES * (i < residual ? base+1 : base); 351 vm_map_pageable(buffer_map, curbuf, curbuf+curbufsize, FALSE); 352 vm_map_simplify(buffer_map, curbuf); 353 } 354 /* 355 * Allocate a submap for exec arguments. This map effectively 356 * limits the number of processes exec'ing at any time. 357 */ 358 exec_map = kmem_suballoc(kernel_map, &minaddr, &maxaddr, 359 16*NCARGS, TRUE); 360 /* 361 * Allocate a submap for physio 362 */ 363 phys_map = kmem_suballoc(kernel_map, &minaddr, &maxaddr, 364 VM_PHYS_SIZE, TRUE); 365 366 /* 367 * Finally, allocate mbuf pool. Since mclrefcnt is an off-size 368 * we use the more space efficient malloc in place of kmem_alloc. 369 */ 370 mclrefcnt = (char *)malloc(NMBCLUSTERS+CLBYTES/MCLBYTES, 371 M_MBUF, M_NOWAIT); 372 bzero(mclrefcnt, NMBCLUSTERS+CLBYTES/MCLBYTES); 373 mb_map = kmem_suballoc(kernel_map, (vm_offset_t *)&mbutl, &maxaddr, 374 VM_MBUF_SIZE, FALSE); 375 /* 376 * Initialize callouts 377 */ 378 callfree = callout; 379 for (i = 1; i < ncallout; i++) 380 callout[i-1].c_next = &callout[i]; 381 callout[i-1].c_next = NULL; 382 383 #ifdef DEBUG 384 pmapdebug = opmapdebug; 385 #endif 386 printf("avail mem = %d\n", ptoa(cnt.v_free_count)); 387 printf("using %d buffers containing %d bytes of memory\n", 388 nbuf, bufpages * CLBYTES); 389 /* 390 * Set up CPU-specific registers, cache, etc. 391 */ 392 initcpu(); 393 394 /* 395 * Set up buffers, so they can be used to read disk labels. 396 */ 397 bufinit(); 398 399 /* 400 * Configure the system. 401 */ 402 configure(); 403 } 404 405 /* 406 * Set registers on exec. 407 * Clear all registers except sp, pc. 408 */ 409 setregs(p, entry, retval) 410 register struct proc *p; 411 u_long entry; 412 int retval[2]; 413 { 414 int sp = p->p_md.md_regs[SP]; 415 extern struct proc *machFPCurProcPtr; 416 417 bzero((caddr_t)p->p_md.md_regs, (FSR + 1) * sizeof(int)); 418 p->p_md.md_regs[SP] = sp; 419 p->p_md.md_regs[PC] = entry & ~3; 420 p->p_md.md_regs[PS] = PSL_USERSET; 421 p->p_md.md_flags & ~MDP_FPUSED; 422 if (machFPCurProcPtr == p) 423 machFPCurProcPtr = (struct proc *)0; 424 } 425 426 /* 427 * WARNING: code in locore.s assumes the layout shown for sf_signum 428 * thru sf_handler so... don't screw with them! 429 */ 430 struct sigframe { 431 int sf_signum; /* signo for handler */ 432 int sf_code; /* additional info for handler */ 433 struct sigcontext *sf_scp; /* context ptr for handler */ 434 sig_t sf_handler; /* handler addr for u_sigc */ 435 struct sigcontext sf_sc; /* actual context */ 436 }; 437 438 #ifdef DEBUG 439 int sigdebug = 0; 440 int sigpid = 0; 441 #define SDB_FOLLOW 0x01 442 #define SDB_KSTACK 0x02 443 #define SDB_FPSTATE 0x04 444 #endif 445 446 /* 447 * Send an interrupt to process. 448 */ 449 void 450 sendsig(catcher, sig, mask, code) 451 sig_t catcher; 452 int sig, mask; 453 unsigned code; 454 { 455 register struct proc *p = curproc; 456 register struct sigframe *fp; 457 register int *regs; 458 register struct sigacts *psp = p->p_sigacts; 459 int oonstack, fsize; 460 struct sigcontext ksc; 461 extern char sigcode[], esigcode[]; 462 463 regs = p->p_md.md_regs; 464 oonstack = psp->ps_sigstk.ss_flags & SA_ONSTACK; 465 /* 466 * Allocate and validate space for the signal handler 467 * context. Note that if the stack is in data space, the 468 * call to grow() is a nop, and the copyout() 469 * will fail if the process has not already allocated 470 * the space with a `brk'. 471 */ 472 fsize = sizeof(struct sigframe); 473 if ((psp->ps_flags & SAS_ALTSTACK) && 474 (psp->ps_sigstk.ss_flags & SA_ONSTACK) == 0 && 475 (psp->ps_sigonstack & sigmask(sig))) { 476 fp = (struct sigframe *)(psp->ps_sigstk.ss_base + 477 psp->ps_sigstk.ss_size - fsize); 478 psp->ps_sigstk.ss_flags |= SA_ONSTACK; 479 } else 480 fp = (struct sigframe *)(regs[SP] - fsize); 481 if ((unsigned)fp <= USRSTACK - ctob(p->p_vmspace->vm_ssize)) 482 (void)grow(p, (unsigned)fp); 483 #ifdef DEBUG 484 if ((sigdebug & SDB_FOLLOW) || 485 (sigdebug & SDB_KSTACK) && p->p_pid == sigpid) 486 printf("sendsig(%d): sig %d ssp %x usp %x scp %x\n", 487 p->p_pid, sig, &oonstack, fp, &fp->sf_sc); 488 #endif 489 /* 490 * Build the signal context to be used by sigreturn. 491 */ 492 ksc.sc_onstack = oonstack; 493 ksc.sc_mask = mask; 494 ksc.sc_pc = regs[PC]; 495 ksc.sc_regs[ZERO] = 0xACEDBADE; /* magic number */ 496 bcopy((caddr_t)®s[1], (caddr_t)&ksc.sc_regs[1], 497 sizeof(ksc.sc_regs) - sizeof(int)); 498 ksc.sc_fpused = p->p_md.md_flags & MDP_FPUSED; 499 if (ksc.sc_fpused) { 500 extern struct proc *machFPCurProcPtr; 501 502 /* if FPU has current state, save it first */ 503 if (p == machFPCurProcPtr) 504 MachSaveCurFPState(p); 505 bcopy((caddr_t)&p->p_md.md_regs[F0], (caddr_t)ksc.sc_fpregs, 506 sizeof(ksc.sc_fpregs)); 507 } 508 if (copyout((caddr_t)&ksc, (caddr_t)&fp->sf_sc, sizeof(ksc))) { 509 /* 510 * Process has trashed its stack; give it an illegal 511 * instruction to halt it in its tracks. 512 */ 513 SIGACTION(p, SIGILL) = SIG_DFL; 514 sig = sigmask(SIGILL); 515 p->p_sigignore &= ~sig; 516 p->p_sigcatch &= ~sig; 517 p->p_sigmask &= ~sig; 518 psignal(p, SIGILL); 519 return; 520 } 521 /* 522 * Build the argument list for the signal handler. 523 */ 524 regs[A0] = sig; 525 regs[A1] = code; 526 regs[A2] = (int)&fp->sf_sc; 527 regs[A3] = (int)catcher; 528 529 regs[PC] = (int)catcher; 530 regs[SP] = (int)fp; 531 /* 532 * Signal trampoline code is at base of user stack. 533 */ 534 regs[RA] = (int)PS_STRINGS - (esigcode - sigcode); 535 #ifdef DEBUG 536 if ((sigdebug & SDB_FOLLOW) || 537 (sigdebug & SDB_KSTACK) && p->p_pid == sigpid) 538 printf("sendsig(%d): sig %d returns\n", 539 p->p_pid, sig); 540 #endif 541 } 542 543 /* 544 * System call to cleanup state after a signal 545 * has been taken. Reset signal mask and 546 * stack state from context left by sendsig (above). 547 * Return to previous pc and psl as specified by 548 * context left by sendsig. Check carefully to 549 * make sure that the user has not modified the 550 * psl to gain improper priviledges or to cause 551 * a machine fault. 552 */ 553 struct sigreturn_args { 554 struct sigcontext *sigcntxp; 555 }; 556 /* ARGSUSED */ 557 sigreturn(p, uap, retval) 558 struct proc *p; 559 struct sigreturn_args *uap; 560 int *retval; 561 { 562 register struct sigcontext *scp; 563 register int *regs; 564 struct sigcontext ksc; 565 int error; 566 567 scp = uap->sigcntxp; 568 #ifdef DEBUG 569 if (sigdebug & SDB_FOLLOW) 570 printf("sigreturn: pid %d, scp %x\n", p->p_pid, scp); 571 #endif 572 regs = p->p_md.md_regs; 573 /* 574 * Test and fetch the context structure. 575 * We grab it all at once for speed. 576 */ 577 error = copyin((caddr_t)scp, (caddr_t)&ksc, sizeof(ksc)); 578 if (error || ksc.sc_regs[ZERO] != 0xACEDBADE) { 579 #ifdef DEBUG 580 if (!(sigdebug & SDB_FOLLOW)) 581 printf("sigreturn: pid %d, scp %x\n", p->p_pid, scp); 582 printf(" old sp %x ra %x pc %x\n", 583 regs[SP], regs[RA], regs[PC]); 584 printf(" new sp %x ra %x pc %x err %d z %x\n", 585 ksc.sc_regs[SP], ksc.sc_regs[RA], ksc.sc_regs[PC], 586 error, ksc.sc_regs[ZERO]); 587 #endif 588 return (EINVAL); 589 } 590 scp = &ksc; 591 /* 592 * Restore the user supplied information 593 */ 594 if (scp->sc_onstack & 01) 595 p->p_sigacts->ps_sigstk.ss_flags |= SA_ONSTACK; 596 else 597 p->p_sigacts->ps_sigstk.ss_flags &= ~SA_ONSTACK; 598 p->p_sigmask = scp->sc_mask &~ sigcantmask; 599 regs[PC] = scp->sc_pc; 600 bcopy((caddr_t)&scp->sc_regs[1], (caddr_t)®s[1], 601 sizeof(scp->sc_regs) - sizeof(int)); 602 if (scp->sc_fpused) 603 bcopy((caddr_t)scp->sc_fpregs, (caddr_t)&p->p_md.md_regs[F0], 604 sizeof(scp->sc_fpregs)); 605 return (EJUSTRETURN); 606 } 607 608 int waittime = -1; 609 610 boot(howto) 611 register int howto; 612 { 613 614 /* take a snap shot before clobbering any registers */ 615 if (curproc) 616 savectx(curproc->p_addr, 0); 617 618 howto |= RB_HALT; /* XXX */ 619 boothowto = howto; 620 if ((howto&RB_NOSYNC) == 0 && waittime < 0) { 621 register struct buf *bp; 622 int iter, nbusy; 623 624 waittime = 0; 625 (void) spl0(); 626 printf("syncing disks... "); 627 /* 628 * Release vnodes held by texts before sync. 629 */ 630 if (panicstr == 0) 631 vnode_pager_umount(NULL); 632 #ifdef notyet 633 #include "fd.h" 634 #if NFD > 0 635 fdshutdown(); 636 #endif 637 #endif 638 sync(&proc0, (void *)NULL, (int *)NULL); 639 640 for (iter = 0; iter < 20; iter++) { 641 nbusy = 0; 642 for (bp = &buf[nbuf]; --bp >= buf; ) 643 if ((bp->b_flags & (B_BUSY|B_INVAL)) == B_BUSY) 644 nbusy++; 645 if (nbusy == 0) 646 break; 647 printf("%d ", nbusy); 648 DELAY(40000 * iter); 649 } 650 if (nbusy) 651 printf("giving up\n"); 652 else 653 printf("done\n"); 654 /* 655 * If we've been adjusting the clock, the todr 656 * will be out of synch; adjust it now. 657 */ 658 resettodr(); 659 } 660 (void) splhigh(); /* extreme priority */ 661 if (howto & RB_HALT) { 662 halt(howto); 663 /*NOTREACHED*/ 664 } else { 665 if (howto & RB_DUMP) 666 dumpsys(); 667 halt(howto); 668 /*NOTREACHED*/ 669 } 670 /*NOTREACHED*/ 671 } 672 673 halt(howto) 674 int howto; 675 { 676 if (*(volatile u_char *)DIP_SWITCH & 0x20) 677 howto |= RB_HALT; 678 to_monitor(howto); 679 /*NOTREACHED*/ 680 } 681 682 int dumpmag = 0x8fca0101; /* magic number for savecore */ 683 int dumpsize = 0; /* also for savecore */ 684 long dumplo = 0; 685 686 dumpconf() 687 { 688 int nblks; 689 690 dumpsize = physmem; 691 if (dumpdev != NODEV && bdevsw[major(dumpdev)].d_psize) { 692 nblks = (*bdevsw[major(dumpdev)].d_psize)(dumpdev); 693 if (dumpsize > btoc(dbtob(nblks - dumplo))) 694 dumpsize = btoc(dbtob(nblks - dumplo)); 695 else if (dumplo == 0) 696 dumplo = nblks - btodb(ctob(physmem)); 697 } 698 /* 699 * Don't dump on the first CLBYTES (why CLBYTES?) 700 * in case the dump device includes a disk label. 701 */ 702 if (dumplo < btodb(CLBYTES)) 703 dumplo = btodb(CLBYTES); 704 } 705 706 /* 707 * Doadump comes here after turning off memory management and 708 * getting on the dump stack, either when called above, or by 709 * the auto-restart code. 710 */ 711 dumpsys() 712 { 713 int error; 714 715 msgbufmapped = 0; 716 if (dumpdev == NODEV) 717 return; 718 /* 719 * For dumps during autoconfiguration, 720 * if dump device has already configured... 721 */ 722 if (dumpsize == 0) 723 dumpconf(); 724 if (dumplo < 0) 725 return; 726 printf("\ndumping to dev %x, offset %d\n", dumpdev, dumplo); 727 printf("dump "); 728 switch (error = (*bdevsw[major(dumpdev)].d_dump)(dumpdev)) { 729 730 case ENXIO: 731 printf("device bad\n"); 732 break; 733 734 case EFAULT: 735 printf("device not ready\n"); 736 break; 737 738 case EINVAL: 739 printf("area improper\n"); 740 break; 741 742 case EIO: 743 printf("i/o error\n"); 744 break; 745 746 default: 747 printf("error %d\n", error); 748 break; 749 750 case 0: 751 printf("succeeded\n"); 752 } 753 } 754 755 /* 756 * Return the best possible estimate of the time in the timeval 757 * to which tvp points. Unfortunately, we can't read the hardware registers. 758 * We guarantee that the time will be greater than the value obtained by a 759 * previous call. 760 */ 761 microtime(tvp) 762 register struct timeval *tvp; 763 { 764 int s = splclock(); 765 static struct timeval lasttime; 766 767 *tvp = time; 768 #ifdef notdef 769 tvp->tv_usec += clkread(); 770 while (tvp->tv_usec > 1000000) { 771 tvp->tv_sec++; 772 tvp->tv_usec -= 1000000; 773 } 774 #endif 775 if (tvp->tv_sec == lasttime.tv_sec && 776 tvp->tv_usec <= lasttime.tv_usec && 777 (tvp->tv_usec = lasttime.tv_usec + 1) > 1000000) { 778 tvp->tv_sec++; 779 tvp->tv_usec -= 1000000; 780 } 781 lasttime = *tvp; 782 splx(s); 783 } 784 785 initcpu() 786 { 787 788 /* 789 * clear LEDs 790 */ 791 *(char*)DEBUG_PORT = (char)DP_WRITE|DP_LED0|DP_LED1|DP_LED2|DP_LED3; 792 793 /* 794 * clear all interrupts 795 */ 796 *(char*)INTCLR0 = 0; 797 *(char*)INTCLR1 = 0; 798 799 /* 800 * It's not a time to enable timer yet. 801 * 802 * INTEN0: PERR ABORT BERR TIMER KBD MS CFLT CBSY 803 * o o o x o o x x 804 * INTEN1: BEEP SCC LANCE DMA SLOT1 SLOT3 EXT1 EXT3 805 * x o o o o o x x 806 */ 807 808 *(char*)INTEN0 = (char) INTEN0_PERR|INTEN0_ABORT|INTEN0_BERR| 809 INTEN0_KBDINT|INTEN0_MSINT; 810 811 *(char*)INTEN1 = (char) INTEN1_SCC|INTEN1_LANCE|INTEN1_DMA| 812 INTEN1_SLOT1|INTEN1_SLOT3; 813 814 spl0(); /* safe to turn interrupts on now */ 815 } 816 817 /* 818 * Convert an ASCII string into an integer. 819 */ 820 int 821 atoi(s) 822 char *s; 823 { 824 int c; 825 unsigned base = 10, d; 826 int neg = 0, val = 0; 827 828 if (s == 0 || (c = *s++) == 0) 829 goto out; 830 831 /* skip spaces if any */ 832 while (c == ' ' || c == '\t') 833 c = *s++; 834 835 /* parse sign, allow more than one (compat) */ 836 while (c == '-') { 837 neg = !neg; 838 c = *s++; 839 } 840 841 /* parse base specification, if any */ 842 if (c == '0') { 843 c = *s++; 844 switch (c) { 845 case 'X': 846 case 'x': 847 base = 16; 848 break; 849 case 'B': 850 case 'b': 851 base = 2; 852 break; 853 default: 854 base = 8; 855 break; 856 } 857 } 858 859 /* parse number proper */ 860 for (;;) { 861 if (c >= '0' && c <= '9') 862 d = c - '0'; 863 else if (c >= 'a' && c <= 'z') 864 d = c - 'a' + 10; 865 else if (c >= 'A' && c <= 'Z') 866 d = c - 'A' + 10; 867 else 868 break; 869 val *= base; 870 val += d; 871 c = *s++; 872 } 873 if (neg) 874 val = -val; 875 out: 876 return val; 877 } 878 879 #ifdef CPU_SINGLE 880 /* 881 * small ring buffers for keyboard/mouse 882 */ 883 struct ring_buf { 884 u_char head; 885 u_char tail; 886 u_char count; 887 u_char buf[13]; 888 } ring_buf[2]; 889 890 xputc(c, chan) 891 u_char c; 892 int chan; 893 { 894 register struct ring_buf *p = &ring_buf[chan]; 895 int s = splhigh(); 896 897 if (p->count >= sizeof (p->buf)) { 898 (void) splx(s); 899 return (-1); 900 } 901 p->buf[p->head] = c; 902 if (++p->head >= sizeof (p->buf)) 903 p->head = 0; 904 p->count++; 905 (void) splx(s); 906 return (c); 907 } 908 909 xgetc(chan) 910 int chan; 911 { 912 register struct ring_buf *p = &ring_buf[chan]; 913 int c; 914 int s = splhigh(); 915 916 if (p->count == 0) { 917 (void) splx(s); 918 return (-1); 919 } 920 c = p->buf[p->tail]; 921 if (++p->tail >= sizeof (p->buf)) 922 p->tail = 0; 923 p->count--; 924 (void) splx(s); 925 return (c); 926 } 927 #endif /* CPU_SINGLE */ 928 929 _delay(time) 930 register int time; 931 { 932 extern int cpuspeed; 933 934 time *= cpuspeed; 935 while(time--) 936 ; 937 } 938