machdep.c revision 1.49
1/* $NetBSD: machdep.c,v 1.49 2007/01/24 13:08:13 hubertf Exp $ */ 2 3/* 4 * Copyright (c) 1992, 1993 5 * The Regents of the University of California. All rights reserved. 6 * 7 * This code is derived from software contributed to Berkeley by 8 * the Systems Programming Group of the University of Utah Computer 9 * Science Department, The Mach Operating System project at 10 * Carnegie-Mellon University and Ralph Campbell. 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. Neither the name of the University nor the names of its contributors 21 * may be used to endorse or promote products derived from this software 22 * without specific prior written permission. 23 * 24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34 * SUCH DAMAGE. 35 * 36 * @(#)machdep.c 8.3 (Berkeley) 1/12/94 37 */ 38/* 39 * Copyright (c) 1988 University of Utah. 40 * 41 * This code is derived from software contributed to Berkeley by 42 * the Systems Programming Group of the University of Utah Computer 43 * Science Department, The Mach Operating System project at 44 * Carnegie-Mellon University and Ralph Campbell. 45 * 46 * Redistribution and use in source and binary forms, with or without 47 * modification, are permitted provided that the following conditions 48 * are met: 49 * 1. Redistributions of source code must retain the above copyright 50 * notice, this list of conditions and the following disclaimer. 51 * 2. Redistributions in binary form must reproduce the above copyright 52 * notice, this list of conditions and the following disclaimer in the 53 * documentation and/or other materials provided with the distribution. 54 * 3. All advertising materials mentioning features or use of this software 55 * must display the following acknowledgement: 56 * This product includes software developed by the University of 57 * California, Berkeley and its contributors. 58 * 4. Neither the name of the University nor the names of its contributors 59 * may be used to endorse or promote products derived from this software 60 * without specific prior written permission. 61 * 62 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 63 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 64 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 65 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 66 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 67 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 68 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 69 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 70 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 71 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 72 * SUCH DAMAGE. 73 * 74 * @(#)machdep.c 8.3 (Berkeley) 1/12/94 75 */ 76 77#include <sys/cdefs.h> /* RCS ID & Copyright macro defns */ 78 79__KERNEL_RCSID(0, "$NetBSD: machdep.c,v 1.49 2007/01/24 13:08:13 hubertf Exp $"); 80 81/* from: Utah Hdr: machdep.c 1.63 91/04/24 */ 82 83#include "opt_ddb.h" 84#include "opt_kgdb.h" 85 86#include <sys/param.h> 87#include <sys/signalvar.h> 88#include <sys/kernel.h> 89#include <sys/proc.h> 90#include <sys/buf.h> 91#include <sys/reboot.h> 92#include <sys/conf.h> 93#include <sys/file.h> 94#include <sys/callout.h> 95#include <sys/malloc.h> 96#include <sys/mbuf.h> 97#include <sys/msgbuf.h> 98#include <sys/ioctl.h> 99#include <sys/device.h> 100#include <sys/user.h> 101#include <sys/exec.h> 102#include <sys/mount.h> 103#include <sys/sa.h> 104#include <sys/syscallargs.h> 105#include <sys/kcore.h> 106#include <sys/ksyms.h> 107 108#include <uvm/uvm_extern.h> 109 110#include <ufs/mfs/mfs_extern.h> /* mfs_initminiroot() */ 111 112#include <machine/cpu.h> 113#include <machine/reg.h> 114#include <machine/psl.h> 115#include <machine/pte.h> 116 117#ifdef DDB 118#include <machine/db_machdep.h> 119#include <ddb/db_extern.h> 120#endif 121 122#include <machine/intr.h> 123#include <machine/mainboard.h> 124#include <machine/sysconf.h> 125#include <machine/autoconf.h> 126#include <machine/bootinfo.h> 127#include <machine/prom.h> 128#include <dev/clock_subr.h> 129#include <dev/cons.h> 130 131#include <sys/boot_flag.h> 132 133#include "fs_mfs.h" 134#include "opt_execfmt.h" 135 136#include "zsc.h" /* XXX */ 137#include "com.h" /* XXX */ 138#include "ksyms.h" 139 140/* Our exported CPU info; we can have only one. */ 141struct cpu_info cpu_info_store; 142 143/* maps for VM objects */ 144 145struct vm_map *exec_map = NULL; 146struct vm_map *mb_map = NULL; 147struct vm_map *phys_map = NULL; 148 149int physmem; /* max supported memory, changes to actual */ 150char *bootinfo = NULL; /* pointer to bootinfo structure */ 151 152phys_ram_seg_t mem_clusters[VM_PHYSSEG_MAX]; 153int mem_cluster_cnt; 154 155void to_monitor __P((int)) __attribute__((__noreturn__)); 156void prom_halt __P((int)) __attribute__((__noreturn__)); 157 158#ifdef KGDB 159void zs_kgdb_init __P((void)); 160void kgdb_connect __P((int)); 161#endif 162 163/* 164 * Local functions. 165 */ 166int initcpu __P((void)); 167void configure __P((void)); 168 169void mach_init __P((int, char *[], char*[], u_int, char *)); 170int memsize_scan __P((caddr_t)); 171 172#ifdef DEBUG 173/* stacktrace code violates prototypes to get callee's registers */ 174extern void stacktrace __P((void)); /*XXX*/ 175#endif 176 177/* 178 * safepri is a safe priority for sleep to set for a spin-wait 179 * during autoconfiguration or after a panic. Used as an argument to splx(). 180 * XXX disables interrupt 5 to disable mips3 on-chip clock, which also 181 * disables mips1 FPU interrupts. 182 */ 183int safepri = MIPS3_PSL_LOWIPL; /* XXX */ 184extern struct user *proc0paddr; 185 186/* locore callback-vector setup */ 187extern void mips_vector_init __P((void)); 188extern void prom_init __P((void)); 189extern void pizazz_init __P((void)); 190 191/* platform-specific initialization vector */ 192static void unimpl_cons_init __P((void)); 193static void unimpl_iointr __P((unsigned, unsigned, unsigned, unsigned)); 194static int unimpl_memsize __P((caddr_t)); 195static void unimpl_intr_establish __P((int, int (*)__P((void *)), void *)); 196 197struct platform platform = { 198 .iobus = "iobus not set", 199 .cons_init = unimpl_cons_init, 200 .iointr = unimpl_iointr, 201 .memsize = unimpl_memsize, 202 .intr_establish = unimpl_intr_establish, 203 .clkinit = NULL, 204}; 205 206struct consdev *cn_tab = NULL; 207extern struct consdev consdev_prom; 208extern struct consdev consdev_zs; 209 210static void null_cnprobe __P((struct consdev *)); 211static void prom_cninit __P((struct consdev *)); 212static int prom_cngetc __P((dev_t)); 213static void prom_cnputc __P((dev_t, int)); 214static void null_cnpollc __P((dev_t, int)); 215 216struct consdev consdev_prom = { 217 null_cnprobe, 218 prom_cninit, 219 prom_cngetc, 220 prom_cnputc, 221 null_cnpollc, 222}; 223 224 225/* 226 * Do all the stuff that locore normally does before calling main(). 227 * Process arguments passed to us by the prom monitor. 228 * Return the first page address following the system. 229 */ 230void 231mach_init(argc, argv, envp, bim, bip) 232 int argc; 233 char *argv[]; 234 char *envp[]; 235 u_int bim; 236 char *bip; 237{ 238 u_long first, last; 239 caddr_t kernend, v; 240 char *cp; 241 int i, howto; 242 extern char edata[], end[]; 243 const char *bi_msg; 244#if NKSYMS || defined(DDB) || defined(LKM) 245 int nsym = 0; 246 caddr_t ssym = 0; 247 caddr_t esym = 0; 248 struct btinfo_symtab *bi_syms; 249#endif 250 251 252 /* Check for valid bootinfo passed from bootstrap */ 253 if (bim == BOOTINFO_MAGIC) { 254 struct btinfo_magic *bi_magic; 255 256 bootinfo = (char *)BOOTINFO_ADDR; /* XXX */ 257 bi_magic = lookup_bootinfo(BTINFO_MAGIC); 258 if (bi_magic == NULL || bi_magic->magic != BOOTINFO_MAGIC) 259 bi_msg = "invalid bootinfo structure.\n"; 260 else 261 bi_msg = NULL; 262 } else 263 bi_msg = "invalid bootinfo (standalone boot?)\n"; 264 265 /* clear the BSS segment */ 266 kernend = (caddr_t)mips_round_page(end); 267 memset(edata, 0, end - edata); 268 269#if NKSYMS || defined(DDB) || defined(LKM) 270 bi_syms = lookup_bootinfo(BTINFO_SYMTAB); 271 272 /* Load sysmbol table if present */ 273 if (bi_syms != NULL) { 274 nsym = bi_syms->nsym; 275 ssym = (caddr_t)bi_syms->ssym; 276 esym = (caddr_t)bi_syms->esym; 277 kernend = (caddr_t)mips_round_page(esym); 278 } 279#endif 280 281 prom_init(); 282 consinit(); 283 284 if (bi_msg != NULL) 285 printf(bi_msg); 286 287 /* 288 * Set the VM page size. 289 */ 290 uvm_setpagesize(); 291 292 /* Find out how much memory is available. */ 293 physmem = memsize_scan(kernend); 294 295 /* 296 * Now that we know how much memory we have, initialize the 297 * mem cluster array. 298 */ 299 mem_clusters[0].start = 0; /* XXX is this correct? */ 300 mem_clusters[0].size = ctob(physmem); 301 mem_cluster_cnt = 1; 302 303 /* 304 * Copy exception-dispatch code down to exception vector. 305 * Initialize locore-function vector. 306 * Clear out the I and D caches. 307 */ 308 mips_vector_init(); 309 310 /* Look at argv[0] and compute bootdev */ 311 makebootdev(argv[0]); 312 313 /* 314 * Look at arguments passed to us and compute boothowto. 315 */ 316 boothowto = RB_AUTOBOOT; 317 for (i = 1; i < argc; i++) { 318 for (cp = argv[i]; *cp; cp++) { 319 /* Ignore superfluous '-', if there is one */ 320 if (*cp == '-') 321 continue; 322 323 howto = 0; 324 BOOT_FLAG(*cp, howto); 325 if (! howto) 326 printf("bootflag '%c' not recognised\n", *cp); 327 else 328 boothowto |= howto; 329 } 330 } 331 332 333#if NKSYMS || defined(DDB) || defined(LKM) 334 /* init symbols if present */ 335 if (esym) 336 ksyms_init(esym - ssym, ssym, esym); 337#endif 338#ifdef DDB 339 if (boothowto & RB_KDB) 340 Debugger(); 341#endif 342#ifdef KGDB 343 zs_kgdb_init(); /* XXX */ 344 if (boothowto & RB_KDB) 345 kgdb_connect(0); 346#endif 347 348#ifdef MFS 349 /* 350 * Check to see if a mini-root was loaded into memory. It resides 351 * at the start of the next page just after the end of BSS. 352 */ 353 if (boothowto & RB_MINIROOT) 354 kernend += round_page(mfs_initminiroot(kernend)); 355#endif 356 357 /* 358 * Load the rest of the available pages into the VM system. 359 */ 360 first = round_page(MIPS_KSEG0_TO_PHYS(kernend)); 361 last = mem_clusters[0].start + mem_clusters[0].size; 362 uvm_page_physload(atop(first), atop(last), atop(first), atop(last), 363 VM_FREELIST_DEFAULT); 364 365 /* 366 * Initialize error message buffer (at end of core). 367 */ 368 mips_init_msgbuf(); 369 370 /* 371 * Initialize the virtual memory system. 372 */ 373 pmap_bootstrap(); 374 375 /* 376 * Allocate space for proc0's USPACE. 377 */ 378 v = (caddr_t)uvm_pageboot_alloc(USPACE); 379 lwp0.l_addr = proc0paddr = (struct user *)v; 380 lwp0.l_md.md_regs = (struct frame *)(v + USPACE) - 1; 381 curpcb = &lwp0.l_addr->u_pcb; 382 curpcb->pcb_context[11] = MIPS_INT_MASK | MIPS_SR_INT_IE; /* SR */ 383 384 /* 385 * Set up interrupt handling and I/O addresses. 386 */ 387 pizazz_init(); 388} 389 390 391 392/* 393 * cpu_startup: allocate memory for variable-sized tables, 394 * initialize CPU, and do autoconfiguration. 395 */ 396void 397cpu_startup() 398{ 399 vaddr_t minaddr, maxaddr; 400 char pbuf[9]; 401#ifdef DEBUG 402 extern int pmapdebug; 403 int opmapdebug = pmapdebug; 404 405 pmapdebug = 0; 406#endif 407 408 /* 409 * Good {morning,afternoon,evening,night}. 410 */ 411 printf("%s%s", copyright, version); 412 printf("%s\n", cpu_model); 413 format_bytes(pbuf, sizeof(pbuf), ctob(physmem)); 414 printf("total memory = %s\n", pbuf); 415 416 minaddr = 0; 417 /* 418 * Allocate a submap for exec arguments. This map effectively 419 * limits the number of processes exec'ing at any time. 420 */ 421 exec_map = uvm_km_suballoc(kernel_map, &minaddr, &maxaddr, 422 16 * NCARGS, TRUE, FALSE, NULL); 423 /* 424 * Allocate a submap for physio 425 */ 426 phys_map = uvm_km_suballoc(kernel_map, &minaddr, &maxaddr, 427 VM_PHYS_SIZE, TRUE, FALSE, NULL); 428 429 /* 430 * No need to allocate an mbuf cluster submap. Mbuf clusters 431 * are allocated via the pool allocator, and we use KSEG to 432 * map those pages. 433 */ 434 435#ifdef DEBUG 436 pmapdebug = opmapdebug; 437#endif 438 format_bytes(pbuf, sizeof(pbuf), ptoa(uvmexp.free)); 439 printf("avail memory = %s\n", pbuf); 440} 441 442/* 443 * Look up information in bootinfo of boot loader. 444 */ 445void * 446lookup_bootinfo(type) 447 int type; 448{ 449 struct btinfo_common *bt; 450 char *help = bootinfo; 451 452 /* Check for a bootinfo record first. */ 453 if (help == NULL) 454 return (NULL); 455 456 do { 457 bt = (struct btinfo_common *)help; 458 if (bt->type == type) 459 return ((void *)help); 460 help += bt->next; 461 } while (bt->next != 0 && 462 (size_t)help < (size_t)bootinfo + BOOTINFO_SIZE); 463 464 return (NULL); 465} 466 467int waittime = -1; 468 469/* 470 * call PROM to halt or reboot. 471 */ 472void 473prom_halt(howto) 474 int howto; 475{ 476 if (howto & RB_HALT) 477 MIPS_PROM(reinit)(); 478 MIPS_PROM(reboot)(); 479 /* NOTREACHED */ 480} 481 482void 483cpu_reboot(howto, bootstr) 484 volatile int howto; 485 char *bootstr; 486{ 487 /* take a snap shot before clobbering any registers */ 488 if (curlwp) 489 savectx((struct user *)curpcb); 490 491#ifdef DEBUG 492 if (panicstr) 493 stacktrace(); 494#endif 495 496 /* If system is cold, just halt. */ 497 if (cold) { 498 howto |= RB_HALT; 499 goto haltsys; 500 } 501 502 /* If "always halt" was specified as a boot flag, obey. */ 503 if ((boothowto & RB_HALT) != 0) 504 howto |= RB_HALT; 505 506 boothowto = howto; 507 if ((howto & RB_NOSYNC) == 0 && waittime < 0) { 508 /* 509 * Synchronize the disks.... 510 */ 511 waittime = 0; 512 vfs_shutdown(); 513 514 /* 515 * If we've been adjusting the clock, the todr 516 * will be out of synch; adjust it now. 517 */ 518 resettodr(); 519 } 520 521 /* Disable interrupts. */ 522 splhigh(); 523 524 /* If rebooting and a dump is requested do it. */ 525#if 0 526 if ((howto & (RB_DUMP | RB_HALT)) == RB_DUMP) 527#else 528 if (howto & RB_DUMP) 529#endif 530 dumpsys(); 531 532haltsys: 533 534 /* run any shutdown hooks */ 535 doshutdownhooks(); 536 537 if ((howto & RB_POWERDOWN) == RB_POWERDOWN) 538 prom_halt(0x80); /* rom monitor RB_PWOFF */ 539 540 /* Finally, halt/reboot the system. */ 541 printf("%s\n\n", howto & RB_HALT ? "halted." : "rebooting..."); 542 prom_halt(howto & RB_HALT); 543 /*NOTREACHED*/ 544} 545 546int 547initcpu() 548{ 549 spl0(); /* safe to turn interrupts on now */ 550 return 0; 551} 552 553static void 554unimpl_cons_init() 555{ 556 557 panic("sysconf.init didn't set cons_init"); 558} 559 560static void 561unimpl_iointr(mask, pc, statusreg, causereg) 562 u_int mask; 563 u_int pc; 564 u_int statusreg; 565 u_int causereg; 566{ 567 568 panic("sysconf.init didn't set intr"); 569} 570 571static int 572unimpl_memsize(first) 573caddr_t first; 574{ 575 576 panic("sysconf.init didn't set memsize"); 577} 578 579void 580unimpl_intr_establish(level, func, arg) 581 int level; 582 int (*func) __P((void *)); 583 void *arg; 584{ 585 panic("sysconf.init didn't init intr_establish"); 586} 587 588void 589delay(n) 590 int n; 591{ 592 DELAY(n); 593} 594 595/* 596 * Find out how much memory is available by testing memory. 597 * Be careful to save and restore the original contents for msgbuf. 598 */ 599int 600memsize_scan(first) 601 caddr_t first; 602{ 603 volatile int *vp, *vp0; 604 int mem, tmp, tmp0; 605 606#define PATTERN1 0xa5a5a5a5 607#define PATTERN2 ~PATTERN1 608 609 /* 610 * Non destructive scan of memory to determine the size 611 * Use the first page to test for memory aliases. This 612 * also has the side effect of flushing the bus alignment 613 * buffer 614 */ 615 mem = btoc((paddr_t)first - MIPS_KSEG0_START); 616 vp = (int *)MIPS_PHYS_TO_KSEG1(mem << PGSHIFT); 617 vp0 = (int *)MIPS_PHYS_TO_KSEG1(0); /* Start of physical memory */ 618 tmp0 = *vp0; 619 while (vp < (int *)MIPS_MAX_MEM_ADDR) { 620 tmp = *vp; 621 *vp = PATTERN1; 622 *vp0 = PATTERN2; 623 wbflush(); 624 if (*vp != PATTERN1) 625 break; 626 *vp = PATTERN2; 627 *vp0 = PATTERN1; 628 wbflush(); 629 if (*vp != PATTERN2) 630 break; 631 *vp = tmp; 632 vp += PAGE_SIZE/sizeof(int); 633 mem++; 634 } 635 *vp0 = tmp0; 636 return mem; 637} 638 639/* 640 * Console initialization: called early on from main, 641 * before vm init or startup. Do enough configuration 642 * to choose and initialize a console. 643 */ 644 645static void 646null_cnprobe(cn) 647 struct consdev *cn; 648{ 649} 650 651static void 652prom_cninit(cn) 653 struct consdev *cn; 654{ 655 extern const struct cdevsw cons_cdevsw; 656 657 cn->cn_dev = makedev(cdevsw_lookup_major(&cons_cdevsw), 0); 658 cn->cn_pri = CN_REMOTE; 659} 660 661static int 662prom_cngetc(dev) 663 dev_t dev; 664{ 665 return MIPS_PROM(getchar)(); 666} 667 668static void 669prom_cnputc(dev, c) 670 dev_t dev; 671 int c; 672{ 673 MIPS_PROM(putchar)(c); 674} 675 676static void 677null_cnpollc(dev, on) 678 dev_t dev; 679 int on; 680{ 681} 682 683void 684consinit() 685{ 686 int zs_unit; 687 688 zs_unit = 0; 689 cn_tab = &consdev_zs; 690 691 (*cn_tab->cn_init)(cn_tab); 692} 693