1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 /* 22 * Copyright 2007 Sun Microsystems, Inc. All rights reserved. 23 * Use is subject to license terms. 24 */ 25 26 #pragma ident "%Z%%M% %I% %E% SMI" 27 28 #include <sys/types.h> 29 #include <sys/t_lock.h> 30 #include <sys/param.h> 31 #include <sys/sysmacros.h> 32 #include <sys/signal.h> 33 #include <sys/systm.h> 34 #include <sys/user.h> 35 #include <sys/mman.h> 36 #include <sys/vm.h> 37 #include <sys/conf.h> 38 #include <sys/avintr.h> 39 #include <sys/autoconf.h> 40 #include <sys/disp.h> 41 #include <sys/class.h> 42 #include <sys/bitmap.h> 43 44 #include <sys/privregs.h> 45 46 #include <sys/proc.h> 47 #include <sys/buf.h> 48 #include <sys/kmem.h> 49 #include <sys/mem.h> 50 #include <sys/kstat.h> 51 52 #include <sys/reboot.h> 53 54 #include <sys/cred.h> 55 #include <sys/vnode.h> 56 #include <sys/file.h> 57 58 #include <sys/procfs.h> 59 60 #include <sys/vfs.h> 61 #include <sys/cmn_err.h> 62 #include <sys/utsname.h> 63 #include <sys/debug.h> 64 #include <sys/kdi.h> 65 66 #include <sys/dumphdr.h> 67 #include <sys/bootconf.h> 68 #include <sys/varargs.h> 69 #include <sys/promif.h> 70 #include <sys/modctl.h> /* for "procfs" hack */ 71 72 #include <sys/sunddi.h> 73 #include <sys/sunndi.h> 74 #include <sys/ndi_impldefs.h> 75 #include <sys/ddidmareq.h> 76 #include <sys/psw.h> 77 #include <sys/regset.h> 78 #include <sys/clock.h> 79 #include <sys/pte.h> 80 #include <sys/tss.h> 81 #include <sys/stack.h> 82 #include <sys/trap.h> 83 #include <sys/fp.h> 84 #include <vm/anon.h> 85 #include <vm/as.h> 86 #include <vm/page.h> 87 #include <vm/seg.h> 88 #include <vm/seg_dev.h> 89 #include <vm/seg_kmem.h> 90 #include <vm/seg_kpm.h> 91 #include <vm/seg_map.h> 92 #include <vm/seg_vn.h> 93 #include <vm/seg_kp.h> 94 #include <sys/memnode.h> 95 #include <vm/vm_dep.h> 96 #include <sys/thread.h> 97 #include <sys/sysconf.h> 98 #include <sys/vm_machparam.h> 99 #include <sys/archsystm.h> 100 #include <sys/machsystm.h> 101 #include <vm/hat.h> 102 #include <vm/hat_i86.h> 103 #include <sys/pmem.h> 104 #include <sys/smp_impldefs.h> 105 #include <sys/x86_archext.h> 106 #include <sys/segments.h> 107 #include <sys/clconf.h> 108 #include <sys/kobj.h> 109 #include <sys/kobj_lex.h> 110 #include <sys/cpc_impl.h> 111 #include <sys/pg.h> 112 #include <sys/x86_archext.h> 113 #include <sys/cpu_module.h> 114 #include <sys/smbios.h> 115 #include <sys/debug_info.h> 116 117 118 #include <sys/bootinfo.h> 119 #include <vm/kboot_mmu.h> 120 121 extern void progressbar_init(void); 122 extern void progressbar_start(void); 123 extern void brand_init(void); 124 125 /* 126 * XXX make declaration below "static" when drivers no longer use this 127 * interface. 128 */ 129 extern caddr_t p0_va; /* Virtual address for accessing physical page 0 */ 130 131 /* 132 * segkp 133 */ 134 extern int segkp_fromheap; 135 136 static void kvm_init(void); 137 static void startup_init(void); 138 static void startup_memlist(void); 139 static void startup_kmem(void); 140 static void startup_modules(void); 141 static void startup_vm(void); 142 static void startup_end(void); 143 static void layout_kernel_va(void); 144 145 /* 146 * Declare these as initialized data so we can patch them. 147 */ 148 #ifdef __i386 149 150 /* 151 * Due to virtual address space limitations running in 32 bit mode, restrict 152 * the amount of physical memory configured to a max of PHYSMEM pages (16g). 153 * 154 * If the physical max memory size of 64g were allowed to be configured, the 155 * size of user virtual address space will be less than 1g. A limited user 156 * address space greatly reduces the range of applications that can run. 157 * 158 * If more physical memory than PHYSMEM is required, users should preferably 159 * run in 64 bit mode which has far looser virtual address space limitations. 160 * 161 * If 64 bit mode is not available (as in IA32) and/or more physical memory 162 * than PHYSMEM is required in 32 bit mode, physmem can be set to the desired 163 * value or to 0 (to configure all available memory) via eeprom(1M). kernelbase 164 * should also be carefully tuned to balance out the need of the user 165 * application while minimizing the risk of kernel heap exhaustion due to 166 * kernelbase being set too high. 167 */ 168 #define PHYSMEM 0x400000 169 170 #else /* __amd64 */ 171 172 /* 173 * For now we can handle memory with physical addresses up to about 174 * 64 Terabytes. This keeps the kernel above the VA hole, leaving roughly 175 * half the VA space for seg_kpm. When systems get bigger than 64TB this 176 * code will need revisiting. There is an implicit assumption that there 177 * are no *huge* holes in the physical address space too. 178 */ 179 #define TERABYTE (1ul << 40) 180 #define PHYSMEM_MAX64 mmu_btop(64 * TERABYTE) 181 #define PHYSMEM PHYSMEM_MAX64 182 #define AMD64_VA_HOLE_END 0xFFFF800000000000ul 183 184 #endif /* __amd64 */ 185 186 pgcnt_t physmem = PHYSMEM; 187 pgcnt_t obp_pages; /* Memory used by PROM for its text and data */ 188 189 char *kobj_file_buf; 190 int kobj_file_bufsize; /* set in /etc/system */ 191 192 /* Global variables for MP support. Used in mp_startup */ 193 caddr_t rm_platter_va; 194 uint32_t rm_platter_pa; 195 196 int auto_lpg_disable = 1; 197 198 /* 199 * Some CPUs have holes in the middle of the 64-bit virtual address range. 200 */ 201 uintptr_t hole_start, hole_end; 202 203 /* 204 * kpm mapping window 205 */ 206 caddr_t kpm_vbase; 207 size_t kpm_size; 208 static int kpm_desired; 209 #ifdef __amd64 210 static uintptr_t segkpm_base = (uintptr_t)SEGKPM_BASE; 211 #endif 212 213 /* 214 * Configuration parameters set at boot time. 215 */ 216 217 caddr_t econtig; /* end of first block of contiguous kernel */ 218 219 struct bootops *bootops = 0; /* passed in from boot */ 220 struct bootops **bootopsp; 221 struct boot_syscalls *sysp; /* passed in from boot */ 222 223 char bootblock_fstype[16]; 224 225 char kern_bootargs[OBP_MAXPATHLEN]; 226 227 /* 228 * ZFS zio segment. This allows us to exclude large portions of ZFS data that 229 * gets cached in kmem caches on the heap. If this is set to zero, we allocate 230 * zio buffers from their own segment, otherwise they are allocated from the 231 * heap. The optimization of allocating zio buffers from their own segment is 232 * only valid on 64-bit kernels. 233 */ 234 #if defined(__amd64) 235 int segzio_fromheap = 0; 236 #else 237 int segzio_fromheap = 1; 238 #endif 239 240 /* 241 * new memory fragmentations are possible in startup() due to BOP_ALLOCs. this 242 * depends on number of BOP_ALLOC calls made and requested size, memory size 243 * combination and whether boot.bin memory needs to be freed. 244 */ 245 #define POSS_NEW_FRAGMENTS 12 246 247 /* 248 * VM data structures 249 */ 250 long page_hashsz; /* Size of page hash table (power of two) */ 251 struct page *pp_base; /* Base of initial system page struct array */ 252 struct page **page_hash; /* Page hash table */ 253 struct seg ktextseg; /* Segment used for kernel executable image */ 254 struct seg kvalloc; /* Segment used for "valloc" mapping */ 255 struct seg kpseg; /* Segment used for pageable kernel virt mem */ 256 struct seg kmapseg; /* Segment used for generic kernel mappings */ 257 struct seg kdebugseg; /* Segment used for the kernel debugger */ 258 259 struct seg *segkmap = &kmapseg; /* Kernel generic mapping segment */ 260 static struct seg *segmap = &kmapseg; /* easier to use name for in here */ 261 262 struct seg *segkp = &kpseg; /* Pageable kernel virtual memory segment */ 263 264 #if defined(__amd64) 265 struct seg kvseg_core; /* Segment used for the core heap */ 266 struct seg kpmseg; /* Segment used for physical mapping */ 267 struct seg *segkpm = &kpmseg; /* 64bit kernel physical mapping segment */ 268 #else 269 struct seg *segkpm = NULL; /* Unused on IA32 */ 270 #endif 271 272 caddr_t segkp_base; /* Base address of segkp */ 273 caddr_t segzio_base; /* Base address of segzio */ 274 #if defined(__amd64) 275 pgcnt_t segkpsize = btop(SEGKPDEFSIZE); /* size of segkp segment in pages */ 276 #else 277 pgcnt_t segkpsize = 0; 278 #endif 279 pgcnt_t segziosize = 0; /* size of zio segment in pages */ 280 281 /* 282 * VA range available to the debugger 283 */ 284 const caddr_t kdi_segdebugbase = (const caddr_t)SEGDEBUGBASE; 285 const size_t kdi_segdebugsize = SEGDEBUGSIZE; 286 287 struct memseg *memseg_base; 288 struct vnode unused_pages_vp; 289 290 #define FOURGB 0x100000000LL 291 292 struct memlist *memlist; 293 294 caddr_t s_text; /* start of kernel text segment */ 295 caddr_t e_text; /* end of kernel text segment */ 296 caddr_t s_data; /* start of kernel data segment */ 297 caddr_t e_data; /* end of kernel data segment */ 298 caddr_t modtext; /* start of loadable module text reserved */ 299 caddr_t e_modtext; /* end of loadable module text reserved */ 300 caddr_t moddata; /* start of loadable module data reserved */ 301 caddr_t e_moddata; /* end of loadable module data reserved */ 302 303 struct memlist *phys_install; /* Total installed physical memory */ 304 struct memlist *phys_avail; /* Total available physical memory */ 305 306 /* 307 * kphysm_init returns the number of pages that were processed 308 */ 309 static pgcnt_t kphysm_init(page_t *, pgcnt_t); 310 311 #define IO_PROP_SIZE 64 /* device property size */ 312 313 /* 314 * a couple useful roundup macros 315 */ 316 #define ROUND_UP_PAGE(x) \ 317 ((uintptr_t)P2ROUNDUP((uintptr_t)(x), (uintptr_t)MMU_PAGESIZE)) 318 #define ROUND_UP_LPAGE(x) \ 319 ((uintptr_t)P2ROUNDUP((uintptr_t)(x), mmu.level_size[1])) 320 #define ROUND_UP_4MEG(x) \ 321 ((uintptr_t)P2ROUNDUP((uintptr_t)(x), (uintptr_t)FOUR_MEG)) 322 #define ROUND_UP_TOPLEVEL(x) \ 323 ((uintptr_t)P2ROUNDUP((uintptr_t)(x), mmu.level_size[mmu.max_level])) 324 325 /* 326 * 32-bit Kernel's Virtual memory layout. 327 * +-----------------------+ 328 * | | 329 * 0xFFC00000 -|-----------------------|- ARGSBASE 330 * | debugger | 331 * 0xFF800000 -|-----------------------|- SEGDEBUGBASE 332 * | Kernel Data | 333 * 0xFEC00000 -|-----------------------| 334 * | Kernel Text | 335 * 0xFE800000 -|-----------------------|- KERNEL_TEXT 336 * |--- GDT ---|- GDT page (GDT_VA) 337 * |--- debug info ---|- debug info (DEBUG_INFO_VA) 338 * | | 339 * | page_t structures | 340 * | memsegs, memlists, | 341 * | page hash, etc. | 342 * --- -|-----------------------|- ekernelheap, valloc_base (floating) 343 * | | (segkp is just an arena in the heap) 344 * | | 345 * | kvseg | 346 * | | 347 * | | 348 * --- -|-----------------------|- kernelheap (floating) 349 * | Segkmap | 350 * 0xC3002000 -|-----------------------|- segmap_start (floating) 351 * | Red Zone | 352 * 0xC3000000 -|-----------------------|- kernelbase / userlimit (floating) 353 * | | || 354 * | Shared objects | \/ 355 * | | 356 * : : 357 * | user data | 358 * |-----------------------| 359 * | user text | 360 * 0x08048000 -|-----------------------| 361 * | user stack | 362 * : : 363 * | invalid | 364 * 0x00000000 +-----------------------+ 365 * 366 * 367 * 64-bit Kernel's Virtual memory layout. (assuming 64 bit app) 368 * +-----------------------+ 369 * | | 370 * 0xFFFFFFFF.FFC00000 |-----------------------|- ARGSBASE 371 * | debugger (?) | 372 * 0xFFFFFFFF.FF800000 |-----------------------|- SEGDEBUGBASE 373 * | unused | 374 * +-----------------------+ 375 * | Kernel Data | 376 * 0xFFFFFFFF.FBC00000 |-----------------------| 377 * | Kernel Text | 378 * 0xFFFFFFFF.FB800000 |-----------------------|- KERNEL_TEXT 379 * |--- GDT ---|- GDT page (GDT_VA) 380 * |--- debug info ---|- debug info (DEBUG_INFO_VA) 381 * | | 382 * | Core heap | (used for loadable modules) 383 * 0xFFFFFFFF.C0000000 |-----------------------|- core_base / ekernelheap 384 * | Kernel | 385 * | heap | 386 * 0xFFFFFXXX.XXX00000 |-----------------------|- kernelheap (floating) 387 * | segmap | 388 * 0xFFFFFXXX.XXX00000 |-----------------------|- segmap_start (floating) 389 * | device mappings | 390 * 0xFFFFFXXX.XXX00000 |-----------------------|- toxic_addr (floating) 391 * | segzio | 392 * 0xFFFFFXXX.XXX00000 |-----------------------|- segzio_base (floating) 393 * | segkp | 394 * --- |-----------------------|- segkp_base (floating) 395 * | page_t structures | valloc_base + valloc_sz 396 * | memsegs, memlists, | 397 * | page hash, etc. | 398 * 0xFFFFFF00.00000000 |-----------------------|- valloc_base (lower if > 1TB) 399 * | segkpm | 400 * 0xFFFFFE00.00000000 |-----------------------| 401 * | Red Zone | 402 * 0xFFFFFD80.00000000 |-----------------------|- KERNELBASE (lower if > 1TB) 403 * | User stack |- User space memory 404 * | | 405 * | shared objects, etc | (grows downwards) 406 * : : 407 * | | 408 * 0xFFFF8000.00000000 |-----------------------| 409 * | | 410 * | VA Hole / unused | 411 * | | 412 * 0x00008000.00000000 |-----------------------| 413 * | | 414 * | | 415 * : : 416 * | user heap | (grows upwards) 417 * | | 418 * | user data | 419 * |-----------------------| 420 * | user text | 421 * 0x00000000.04000000 |-----------------------| 422 * | invalid | 423 * 0x00000000.00000000 +-----------------------+ 424 * 425 * A 32 bit app on the 64 bit kernel sees the same layout as on the 32 bit 426 * kernel, except that userlimit is raised to 0xfe000000 427 * 428 * Floating values: 429 * 430 * valloc_base: start of the kernel's memory management/tracking data 431 * structures. This region contains page_t structures for 432 * physical memory, memsegs, memlists, and the page hash. 433 * 434 * core_base: start of the kernel's "core" heap area on 64-bit systems. 435 * This area is intended to be used for global data as well as for module 436 * text/data that does not fit into the nucleus pages. The core heap is 437 * restricted to a 2GB range, allowing every address within it to be 438 * accessed using rip-relative addressing 439 * 440 * ekernelheap: end of kernelheap and start of segmap. 441 * 442 * kernelheap: start of kernel heap. On 32-bit systems, this starts right 443 * above a red zone that separates the user's address space from the 444 * kernel's. On 64-bit systems, it sits above segkp and segkpm. 445 * 446 * segmap_start: start of segmap. The length of segmap can be modified 447 * by changing segmapsize in /etc/system (preferred) or eeprom (deprecated). 448 * The default length is 16MB on 32-bit systems and 64MB on 64-bit systems. 449 * 450 * kernelbase: On a 32-bit kernel the default value of 0xd4000000 will be 451 * decreased by 2X the size required for page_t. This allows the kernel 452 * heap to grow in size with physical memory. With sizeof(page_t) == 80 453 * bytes, the following shows the values of kernelbase and kernel heap 454 * sizes for different memory configurations (assuming default segmap and 455 * segkp sizes). 456 * 457 * mem size for kernelbase kernel heap 458 * size page_t's size 459 * ---- --------- ---------- ----------- 460 * 1gb 0x01400000 0xd1800000 684MB 461 * 2gb 0x02800000 0xcf000000 704MB 462 * 4gb 0x05000000 0xca000000 744MB 463 * 6gb 0x07800000 0xc5000000 784MB 464 * 8gb 0x0a000000 0xc0000000 824MB 465 * 16gb 0x14000000 0xac000000 984MB 466 * 32gb 0x28000000 0x84000000 1304MB 467 * 64gb 0x50000000 0x34000000 1944MB (*) 468 * 469 * kernelbase is less than the abi minimum of 0xc0000000 for memory 470 * configurations above 8gb. 471 * 472 * (*) support for memory configurations above 32gb will require manual tuning 473 * of kernelbase to balance out the need of user applications. 474 */ 475 476 /* real-time-clock initialization parameters */ 477 extern time_t process_rtc_config_file(void); 478 479 uintptr_t kernelbase; 480 uintptr_t postbootkernelbase; /* not set till boot loader is gone */ 481 uintptr_t eprom_kernelbase; 482 size_t segmapsize; 483 uintptr_t segmap_start; 484 int segmapfreelists; 485 pgcnt_t npages; 486 pgcnt_t orig_npages; 487 size_t core_size; /* size of "core" heap */ 488 uintptr_t core_base; /* base address of "core" heap */ 489 490 /* 491 * List of bootstrap pages. We mark these as allocated in startup. 492 * release_bootstrap() will free them when we're completely done with 493 * the bootstrap. 494 */ 495 static page_t *bootpages; 496 497 /* 498 * boot time pages that have a vnode from the ramdisk will keep that forever. 499 */ 500 static page_t *rd_pages; 501 502 struct system_hardware system_hardware; 503 504 /* 505 * Enable some debugging messages concerning memory usage... 506 */ 507 static void 508 print_memlist(char *title, struct memlist *mp) 509 { 510 prom_printf("MEMLIST: %s:\n", title); 511 while (mp != NULL) { 512 prom_printf("\tAddress 0x%" PRIx64 ", size 0x%" PRIx64 "\n", 513 mp->address, mp->size); 514 mp = mp->next; 515 } 516 } 517 518 /* 519 * XX64 need a comment here.. are these just default values, surely 520 * we read the "cpuid" type information to figure this out. 521 */ 522 int l2cache_sz = 0x80000; 523 int l2cache_linesz = 0x40; 524 int l2cache_assoc = 1; 525 526 static size_t textrepl_min_gb = 10; 527 528 /* 529 * on 64 bit we use a predifined VA range for mapping devices in the kernel 530 * on 32 bit the mappings are intermixed in the heap, so we use a bit map 531 */ 532 #ifdef __amd64 533 534 vmem_t *device_arena; 535 uintptr_t toxic_addr = (uintptr_t)NULL; 536 size_t toxic_size = 1024 * 1024 * 1024; /* Sparc uses 1 gig too */ 537 538 #else /* __i386 */ 539 540 ulong_t *toxic_bit_map; /* one bit for each 4k of VA in heap_arena */ 541 size_t toxic_bit_map_len = 0; /* in bits */ 542 543 #endif /* __i386 */ 544 545 /* 546 * Simple boot time debug facilities 547 */ 548 static char *prm_dbg_str[] = { 549 "%s:%d: '%s' is 0x%x\n", 550 "%s:%d: '%s' is 0x%llx\n" 551 }; 552 553 int prom_debug; 554 555 #define PRM_DEBUG(q) if (prom_debug) \ 556 prom_printf(prm_dbg_str[sizeof (q) >> 3], "startup.c", __LINE__, #q, q); 557 #define PRM_POINT(q) if (prom_debug) \ 558 prom_printf("%s:%d: %s\n", "startup.c", __LINE__, q); 559 560 /* 561 * This structure is used to keep track of the intial allocations 562 * done in startup_memlist(). The value of NUM_ALLOCATIONS needs to 563 * be >= the number of ADD_TO_ALLOCATIONS() executed in the code. 564 */ 565 #define NUM_ALLOCATIONS 7 566 int num_allocations = 0; 567 struct { 568 void **al_ptr; 569 size_t al_size; 570 } allocations[NUM_ALLOCATIONS]; 571 size_t valloc_sz = 0; 572 uintptr_t valloc_base; 573 574 #define ADD_TO_ALLOCATIONS(ptr, size) { \ 575 size = ROUND_UP_PAGE(size); \ 576 if (num_allocations == NUM_ALLOCATIONS) \ 577 panic("too many ADD_TO_ALLOCATIONS()"); \ 578 allocations[num_allocations].al_ptr = (void**)&ptr; \ 579 allocations[num_allocations].al_size = size; \ 580 valloc_sz += size; \ 581 ++num_allocations; \ 582 } 583 584 /* 585 * Allocate all the initial memory needed by the page allocator. 586 */ 587 static void 588 perform_allocations(void) 589 { 590 caddr_t mem; 591 int i; 592 int valloc_align; 593 594 PRM_DEBUG(valloc_base); 595 PRM_DEBUG(valloc_sz); 596 valloc_align = mmu.level_size[mmu.max_page_level > 0]; 597 mem = BOP_ALLOC(bootops, (caddr_t)valloc_base, valloc_sz, valloc_align); 598 if (mem != (caddr_t)valloc_base) 599 panic("BOP_ALLOC() failed"); 600 bzero(mem, valloc_sz); 601 for (i = 0; i < num_allocations; ++i) { 602 *allocations[i].al_ptr = (void *)mem; 603 mem += allocations[i].al_size; 604 } 605 } 606 607 /* 608 * Our world looks like this at startup time. 609 * 610 * In a 32-bit OS, boot loads the kernel text at 0xfe800000 and kernel data 611 * at 0xfec00000. On a 64-bit OS, kernel text and data are loaded at 612 * 0xffffffff.fe800000 and 0xffffffff.fec00000 respectively. Those 613 * addresses are fixed in the binary at link time. 614 * 615 * On the text page: 616 * unix/genunix/krtld/module text loads. 617 * 618 * On the data page: 619 * unix/genunix/krtld/module data loads. 620 * 621 * Machine-dependent startup code 622 */ 623 void 624 startup(void) 625 { 626 extern void startup_bios_disk(void); 627 extern void startup_pci_bios(void); 628 #if defined(__amd64) 629 kpm_desired = 1; 630 #endif 631 kpm_enable = 0; 632 633 progressbar_init(); 634 startup_init(); 635 startup_memlist(); 636 startup_kmem(); 637 startup_vm(); 638 startup_pci_bios(); 639 startup_modules(); 640 startup_bios_disk(); 641 startup_end(); 642 progressbar_start(); 643 } 644 645 static void 646 startup_init() 647 { 648 PRM_POINT("startup_init() starting..."); 649 650 /* 651 * Complete the extraction of cpuid data 652 */ 653 cpuid_pass2(CPU); 654 655 (void) check_boot_version(BOP_GETVERSION(bootops)); 656 657 /* 658 * Check for prom_debug in boot environment 659 */ 660 if (BOP_GETPROPLEN(bootops, "prom_debug") >= 0) { 661 ++prom_debug; 662 PRM_POINT("prom_debug found in boot enviroment"); 663 } 664 665 /* 666 * Collect node, cpu and memory configuration information. 667 */ 668 get_system_configuration(); 669 670 /* 671 * Halt if this is an unsupported processor. 672 */ 673 if (x86_type == X86_TYPE_486 || x86_type == X86_TYPE_CYRIX_486) { 674 printf("\n486 processor (\"%s\") detected.\n", 675 CPU->cpu_brandstr); 676 halt("This processor is not supported by this release " 677 "of Solaris."); 678 } 679 680 PRM_POINT("startup_init() done"); 681 } 682 683 /* 684 * Callback for copy_memlist_filter() to filter nucleus, kadb/kmdb, (ie. 685 * everything mapped above KERNEL_TEXT) pages from phys_avail. Note it 686 * also filters out physical page zero. There is some reliance on the 687 * boot loader allocating only a few contiguous physical memory chunks. 688 */ 689 static void 690 avail_filter(uint64_t *addr, uint64_t *size) 691 { 692 uintptr_t va; 693 uintptr_t next_va; 694 pfn_t pfn; 695 uint64_t pfn_addr; 696 uint64_t pfn_eaddr; 697 uint_t prot; 698 size_t len; 699 uint_t change; 700 701 if (prom_debug) 702 prom_printf("\tFilter: in: a=%" PRIx64 ", s=%" PRIx64 "\n", 703 *addr, *size); 704 705 /* 706 * page zero is required for BIOS.. never make it available 707 */ 708 if (*addr == 0) { 709 *addr += MMU_PAGESIZE; 710 *size -= MMU_PAGESIZE; 711 } 712 713 /* 714 * First we trim from the front of the range. Since kbm_probe() 715 * walks ranges in virtual order, but addr/size are physical, we need 716 * to the list until no changes are seen. This deals with the case 717 * where page "p" is mapped at v, page "p + PAGESIZE" is mapped at w 718 * but w < v. 719 */ 720 do { 721 change = 0; 722 for (va = KERNEL_TEXT; 723 *size > 0 && kbm_probe(&va, &len, &pfn, &prot) != 0; 724 va = next_va) { 725 726 next_va = va + len; 727 pfn_addr = pfn_to_pa(pfn); 728 pfn_eaddr = pfn_addr + len; 729 730 if (pfn_addr <= *addr && pfn_eaddr > *addr) { 731 change = 1; 732 while (*size > 0 && len > 0) { 733 *addr += MMU_PAGESIZE; 734 *size -= MMU_PAGESIZE; 735 len -= MMU_PAGESIZE; 736 } 737 } 738 } 739 if (change && prom_debug) 740 prom_printf("\t\ttrim: a=%" PRIx64 ", s=%" PRIx64 "\n", 741 *addr, *size); 742 } while (change); 743 744 /* 745 * Trim pages from the end of the range. 746 */ 747 for (va = KERNEL_TEXT; 748 *size > 0 && kbm_probe(&va, &len, &pfn, &prot) != 0; 749 va = next_va) { 750 751 next_va = va + len; 752 pfn_addr = pfn_to_pa(pfn); 753 754 if (pfn_addr >= *addr && pfn_addr < *addr + *size) 755 *size = pfn_addr - *addr; 756 } 757 758 if (prom_debug) 759 prom_printf("\tFilter out: a=%" PRIx64 ", s=%" PRIx64 "\n", 760 *addr, *size); 761 } 762 763 static void 764 kpm_init() 765 { 766 struct segkpm_crargs b; 767 768 /* 769 * These variables were all designed for sfmmu in which segkpm is 770 * mapped using a single pagesize - either 8KB or 4MB. On x86, we 771 * might use 2+ page sizes on a single machine, so none of these 772 * variables have a single correct value. They are set up as if we 773 * always use a 4KB pagesize, which should do no harm. In the long 774 * run, we should get rid of KPM's assumption that only a single 775 * pagesize is used. 776 */ 777 kpm_pgshft = MMU_PAGESHIFT; 778 kpm_pgsz = MMU_PAGESIZE; 779 kpm_pgoff = MMU_PAGEOFFSET; 780 kpmp2pshft = 0; 781 kpmpnpgs = 1; 782 ASSERT(((uintptr_t)kpm_vbase & (kpm_pgsz - 1)) == 0); 783 784 PRM_POINT("about to create segkpm"); 785 rw_enter(&kas.a_lock, RW_WRITER); 786 787 if (seg_attach(&kas, kpm_vbase, kpm_size, segkpm) < 0) 788 panic("cannot attach segkpm"); 789 790 b.prot = PROT_READ | PROT_WRITE; 791 b.nvcolors = 1; 792 793 if (segkpm_create(segkpm, (caddr_t)&b) != 0) 794 panic("segkpm_create segkpm"); 795 796 rw_exit(&kas.a_lock); 797 } 798 799 /* 800 * The debug info page provides enough information to allow external 801 * inspectors (e.g. when running under a hypervisor) to bootstrap 802 * themselves into allowing full-blown kernel debugging. 803 */ 804 static void 805 init_debug_info(void) 806 { 807 caddr_t mem; 808 debug_info_t *di; 809 810 #ifndef __lint 811 ASSERT(sizeof (debug_info_t) < MMU_PAGESIZE); 812 #endif 813 814 mem = BOP_ALLOC(bootops, (caddr_t)DEBUG_INFO_VA, MMU_PAGESIZE, 815 MMU_PAGESIZE); 816 817 if (mem != (caddr_t)DEBUG_INFO_VA) 818 panic("BOP_ALLOC() failed"); 819 bzero(mem, MMU_PAGESIZE); 820 821 di = (debug_info_t *)mem; 822 823 di->di_magic = DEBUG_INFO_MAGIC; 824 di->di_version = DEBUG_INFO_VERSION; 825 } 826 827 /* 828 * Build the memlists and other kernel essential memory system data structures. 829 * This is everything at valloc_base. 830 */ 831 static void 832 startup_memlist(void) 833 { 834 size_t memlist_sz; 835 size_t memseg_sz; 836 size_t pagehash_sz; 837 size_t pp_sz; 838 uintptr_t va; 839 size_t len; 840 uint_t prot; 841 pfn_t pfn; 842 int memblocks; 843 caddr_t pagecolor_mem; 844 size_t pagecolor_memsz; 845 caddr_t page_ctrs_mem; 846 size_t page_ctrs_size; 847 struct memlist *current; 848 extern void startup_build_mem_nodes(struct memlist *); 849 850 /* XX64 fix these - they should be in include files */ 851 extern size_t page_coloring_init(uint_t, int, int); 852 extern void page_coloring_setup(caddr_t); 853 854 PRM_POINT("startup_memlist() starting..."); 855 856 /* 857 * Use leftover large page nucleus text/data space for loadable modules. 858 * Use at most MODTEXT/MODDATA. 859 */ 860 len = kbm_nucleus_size; 861 ASSERT(len > MMU_PAGESIZE); 862 863 moddata = (caddr_t)ROUND_UP_PAGE(e_data); 864 e_moddata = (caddr_t)P2ROUNDUP((uintptr_t)e_data, (uintptr_t)len); 865 if (e_moddata - moddata > MODDATA) 866 e_moddata = moddata + MODDATA; 867 868 modtext = (caddr_t)ROUND_UP_PAGE(e_text); 869 e_modtext = (caddr_t)P2ROUNDUP((uintptr_t)e_text, (uintptr_t)len); 870 if (e_modtext - modtext > MODTEXT) 871 e_modtext = modtext + MODTEXT; 872 873 econtig = e_moddata; 874 875 PRM_DEBUG(modtext); 876 PRM_DEBUG(e_modtext); 877 PRM_DEBUG(moddata); 878 PRM_DEBUG(e_moddata); 879 PRM_DEBUG(econtig); 880 881 /* 882 * Examine the boot loader physical memory map to find out: 883 * - total memory in system - physinstalled 884 * - the max physical address - physmax 885 * - the number of discontiguous segments of memory. 886 */ 887 if (prom_debug) 888 print_memlist("boot physinstalled", 889 bootops->boot_mem->physinstalled); 890 installed_top_size(bootops->boot_mem->physinstalled, &physmax, 891 &physinstalled, &memblocks); 892 PRM_DEBUG(physmax); 893 PRM_DEBUG(physinstalled); 894 PRM_DEBUG(memblocks); 895 896 /* 897 * Initialize hat's mmu parameters. 898 * Check for enforce-prot-exec in boot environment. It's used to 899 * enable/disable support for the page table entry NX bit. 900 * The default is to enforce PROT_EXEC on processors that support NX. 901 * Boot seems to round up the "len", but 8 seems to be big enough. 902 */ 903 mmu_init(); 904 905 #ifdef __i386 906 /* 907 * physmax is lowered if there is more memory than can be 908 * physically addressed in 32 bit (PAE/non-PAE) modes. 909 */ 910 if (mmu.pae_hat) { 911 if (PFN_ABOVE64G(physmax)) { 912 physinstalled -= (physmax - (PFN_64G - 1)); 913 physmax = PFN_64G - 1; 914 } 915 } else { 916 if (PFN_ABOVE4G(physmax)) { 917 physinstalled -= (physmax - (PFN_4G - 1)); 918 physmax = PFN_4G - 1; 919 } 920 } 921 #endif 922 923 startup_build_mem_nodes(bootops->boot_mem->physinstalled); 924 925 if (BOP_GETPROPLEN(bootops, "enforce-prot-exec") >= 0) { 926 int len = BOP_GETPROPLEN(bootops, "enforce-prot-exec"); 927 char value[8]; 928 929 if (len < 8) 930 (void) BOP_GETPROP(bootops, "enforce-prot-exec", value); 931 else 932 (void) strcpy(value, ""); 933 if (strcmp(value, "off") == 0) 934 mmu.pt_nx = 0; 935 } 936 PRM_DEBUG(mmu.pt_nx); 937 938 /* 939 * We will need page_t's for every page in the system, except for 940 * memory mapped at or above above the start of the kernel text segment. 941 * 942 * pages above e_modtext are attributed to kernel debugger (obp_pages) 943 */ 944 npages = physinstalled - 1; /* avail_filter() skips page 0, so "- 1" */ 945 obp_pages = 0; 946 va = KERNEL_TEXT; 947 while (kbm_probe(&va, &len, &pfn, &prot) != 0) { 948 npages -= len >> MMU_PAGESHIFT; 949 if (va >= (uintptr_t)e_moddata) 950 obp_pages += len >> MMU_PAGESHIFT; 951 va += len; 952 } 953 PRM_DEBUG(npages); 954 PRM_DEBUG(obp_pages); 955 956 /* 957 * If physmem is patched to be non-zero, use it instead of the computed 958 * value unless it is larger than the actual amount of memory on hand. 959 */ 960 if (physmem == 0 || physmem > npages) { 961 physmem = npages; 962 } else if (physmem < npages) { 963 orig_npages = npages; 964 npages = physmem; 965 } 966 PRM_DEBUG(physmem); 967 968 /* 969 * We now compute the sizes of all the initial allocations for 970 * structures the kernel needs in order do kmem_alloc(). These 971 * include: 972 * memsegs 973 * memlists 974 * page hash table 975 * page_t's 976 * page coloring data structs 977 */ 978 memseg_sz = sizeof (struct memseg) * (memblocks + POSS_NEW_FRAGMENTS); 979 ADD_TO_ALLOCATIONS(memseg_base, memseg_sz); 980 PRM_DEBUG(memseg_sz); 981 982 /* 983 * Reserve space for memlists. There's no real good way to know exactly 984 * how much room we'll need, but this should be a good upper bound. 985 */ 986 memlist_sz = ROUND_UP_PAGE(2 * sizeof (struct memlist) * 987 (memblocks + POSS_NEW_FRAGMENTS)); 988 ADD_TO_ALLOCATIONS(memlist, memlist_sz); 989 PRM_DEBUG(memlist_sz); 990 991 /* 992 * The page structure hash table size is a power of 2 993 * such that the average hash chain length is PAGE_HASHAVELEN. 994 */ 995 page_hashsz = npages / PAGE_HASHAVELEN; 996 page_hashsz = 1 << highbit(page_hashsz); 997 pagehash_sz = sizeof (struct page *) * page_hashsz; 998 ADD_TO_ALLOCATIONS(page_hash, pagehash_sz); 999 PRM_DEBUG(pagehash_sz); 1000 1001 /* 1002 * Set aside room for the page structures themselves. 1003 */ 1004 PRM_DEBUG(npages); 1005 pp_sz = sizeof (struct page) * npages; 1006 ADD_TO_ALLOCATIONS(pp_base, pp_sz); 1007 PRM_DEBUG(pp_sz); 1008 1009 /* 1010 * determine l2 cache info and memory size for page coloring 1011 */ 1012 (void) getl2cacheinfo(CPU, 1013 &l2cache_sz, &l2cache_linesz, &l2cache_assoc); 1014 pagecolor_memsz = 1015 page_coloring_init(l2cache_sz, l2cache_linesz, l2cache_assoc); 1016 ADD_TO_ALLOCATIONS(pagecolor_mem, pagecolor_memsz); 1017 PRM_DEBUG(pagecolor_memsz); 1018 1019 page_ctrs_size = page_ctrs_sz(); 1020 ADD_TO_ALLOCATIONS(page_ctrs_mem, page_ctrs_size); 1021 PRM_DEBUG(page_ctrs_size); 1022 1023 #if defined(__amd64) 1024 valloc_sz = ROUND_UP_LPAGE(valloc_sz); 1025 valloc_base = VALLOC_BASE; 1026 1027 /* 1028 * The default values of VALLOC_BASE and SEGKPM_BASE should work 1029 * for values of physmax up to 1 Terabyte. They need adjusting when 1030 * memory is at addresses above 1 TB. 1031 */ 1032 if (physmax + 1 > mmu_btop(TERABYTE)) { 1033 uint64_t kpm_resv_amount = mmu_ptob(physmax + 1); 1034 1035 /* Round to largest possible pagesize for now */ 1036 kpm_resv_amount = P2ROUNDUP(kpm_resv_amount, ONE_GIG); 1037 1038 segkpm_base = -(2 * kpm_resv_amount); /* down from top VA */ 1039 1040 /* make sure we leave some space for user apps above hole */ 1041 segkpm_base = MAX(segkpm_base, AMD64_VA_HOLE_END + TERABYTE); 1042 if (segkpm_base > SEGKPM_BASE) 1043 segkpm_base = SEGKPM_BASE; 1044 PRM_DEBUG(segkpm_base); 1045 1046 valloc_base = segkpm_base + kpm_resv_amount; 1047 PRM_DEBUG(valloc_base); 1048 } 1049 #else /* __i386 */ 1050 valloc_base = (uintptr_t)(MISC_VA_BASE - valloc_sz); 1051 valloc_base = P2ALIGN(valloc_base, mmu.level_size[1]); 1052 PRM_DEBUG(valloc_base); 1053 #endif /* __i386 */ 1054 1055 /* 1056 * do all the initial allocations 1057 */ 1058 perform_allocations(); 1059 1060 /* 1061 * Build phys_install and phys_avail in kernel memspace. 1062 * - phys_install should be all memory in the system. 1063 * - phys_avail is phys_install minus any memory mapped before this 1064 * point above KERNEL_TEXT. 1065 */ 1066 current = phys_install = memlist; 1067 copy_memlist_filter(bootops->boot_mem->physinstalled, ¤t, NULL); 1068 if ((caddr_t)current > (caddr_t)memlist + memlist_sz) 1069 panic("physinstalled was too big!"); 1070 if (prom_debug) 1071 print_memlist("phys_install", phys_install); 1072 1073 phys_avail = current; 1074 PRM_POINT("Building phys_avail:\n"); 1075 copy_memlist_filter(bootops->boot_mem->physinstalled, ¤t, 1076 avail_filter); 1077 if ((caddr_t)current > (caddr_t)memlist + memlist_sz) 1078 panic("physavail was too big!"); 1079 if (prom_debug) 1080 print_memlist("phys_avail", phys_avail); 1081 1082 /* 1083 * setup page coloring 1084 */ 1085 page_coloring_setup(pagecolor_mem); 1086 page_lock_init(); /* currently a no-op */ 1087 1088 /* 1089 * free page list counters 1090 */ 1091 (void) page_ctrs_alloc(page_ctrs_mem); 1092 1093 /* 1094 * Initialize the page structures from the memory lists. 1095 */ 1096 availrmem_initial = availrmem = freemem = 0; 1097 PRM_POINT("Calling kphysm_init()..."); 1098 npages = kphysm_init(pp_base, npages); 1099 PRM_POINT("kphysm_init() done"); 1100 PRM_DEBUG(npages); 1101 1102 init_debug_info(); 1103 1104 /* 1105 * Now that page_t's have been initialized, remove all the 1106 * initial allocation pages from the kernel free page lists. 1107 */ 1108 boot_mapin((caddr_t)valloc_base, valloc_sz); 1109 boot_mapin((caddr_t)GDT_VA, MMU_PAGESIZE); 1110 boot_mapin((caddr_t)DEBUG_INFO_VA, MMU_PAGESIZE); 1111 PRM_POINT("startup_memlist() done"); 1112 1113 PRM_DEBUG(valloc_sz); 1114 1115 #if defined(__amd64) 1116 if ((availrmem >> (30 - MMU_PAGESHIFT)) >= 1117 textrepl_min_gb && l2cache_sz <= 2 << 20) { 1118 extern size_t textrepl_size_thresh; 1119 textrepl_size_thresh = (16 << 20) - 1; 1120 } 1121 #endif 1122 } 1123 1124 /* 1125 * Layout the kernel's part of address space and initialize kmem allocator. 1126 */ 1127 static void 1128 startup_kmem(void) 1129 { 1130 extern void page_set_colorequiv_arr(void); 1131 1132 PRM_POINT("startup_kmem() starting..."); 1133 1134 #if defined(__amd64) 1135 if (eprom_kernelbase && eprom_kernelbase != KERNELBASE) 1136 cmn_err(CE_NOTE, "!kernelbase cannot be changed on 64-bit " 1137 "systems."); 1138 kernelbase = segkpm_base - KERNEL_REDZONE_SIZE; 1139 core_base = (uintptr_t)COREHEAP_BASE; 1140 core_size = (size_t)MISC_VA_BASE - COREHEAP_BASE; 1141 #else /* __i386 */ 1142 /* 1143 * We configure kernelbase based on: 1144 * 1145 * 1. user specified kernelbase via eeprom command. Value cannot exceed 1146 * KERNELBASE_MAX. we large page align eprom_kernelbase 1147 * 1148 * 2. Default to KERNELBASE and adjust to 2X less the size for page_t. 1149 * On large memory systems we must lower kernelbase to allow 1150 * enough room for page_t's for all of memory. 1151 * 1152 * The value set here, might be changed a little later. 1153 */ 1154 if (eprom_kernelbase) { 1155 kernelbase = eprom_kernelbase & mmu.level_mask[1]; 1156 if (kernelbase > KERNELBASE_MAX) 1157 kernelbase = KERNELBASE_MAX; 1158 } else { 1159 kernelbase = (uintptr_t)KERNELBASE; 1160 kernelbase -= ROUND_UP_4MEG(2 * valloc_sz); 1161 } 1162 ASSERT((kernelbase & mmu.level_offset[1]) == 0); 1163 core_base = valloc_base; 1164 core_size = 0; 1165 #endif /* __i386 */ 1166 1167 PRM_DEBUG(core_base); 1168 PRM_DEBUG(core_size); 1169 PRM_DEBUG(kernelbase); 1170 1171 #if defined(__i386) 1172 segkp_fromheap = 1; 1173 #endif /* __i386 */ 1174 1175 ekernelheap = (char *)core_base; 1176 PRM_DEBUG(ekernelheap); 1177 1178 /* 1179 * Now that we know the real value of kernelbase, 1180 * update variables that were initialized with a value of 1181 * KERNELBASE (in common/conf/param.c). 1182 * 1183 * XXX The problem with this sort of hackery is that the 1184 * compiler just may feel like putting the const declarations 1185 * (in param.c) into the .text section. Perhaps they should 1186 * just be declared as variables there? 1187 */ 1188 1189 *(uintptr_t *)&_kernelbase = kernelbase; 1190 *(uintptr_t *)&_userlimit = kernelbase; 1191 #if defined(__amd64) 1192 *(uintptr_t *)&_userlimit -= KERNELBASE - USERLIMIT; 1193 #else 1194 *(uintptr_t *)&_userlimit32 = _userlimit; 1195 #endif 1196 PRM_DEBUG(_kernelbase); 1197 PRM_DEBUG(_userlimit); 1198 PRM_DEBUG(_userlimit32); 1199 1200 layout_kernel_va(); 1201 1202 #if defined(__i386) 1203 /* 1204 * If segmap is too large we can push the bottom of the kernel heap 1205 * higher than the base. Or worse, it could exceed the top of the 1206 * VA space entirely, causing it to wrap around. 1207 */ 1208 if (kernelheap >= ekernelheap || (uintptr_t)kernelheap < kernelbase) 1209 panic("too little address space available for kernelheap," 1210 " use eeprom for lower kernelbase or smaller segmapsize"); 1211 #endif /* __i386 */ 1212 1213 /* 1214 * Initialize the kernel heap. Note 3rd argument must be > 1st. 1215 */ 1216 kernelheap_init(kernelheap, ekernelheap, 1217 kernelheap + MMU_PAGESIZE, 1218 (void *)core_base, (void *)(core_base + core_size)); 1219 1220 /* 1221 * Initialize kernel memory allocator. 1222 */ 1223 kmem_init(); 1224 1225 /* 1226 * Factor in colorequiv to check additional 'equivalent' bins 1227 */ 1228 page_set_colorequiv_arr(); 1229 1230 /* 1231 * print this out early so that we know what's going on 1232 */ 1233 cmn_err(CE_CONT, "?features: %b\n", x86_feature, FMT_X86_FEATURE); 1234 1235 /* 1236 * Initialize bp_mapin(). 1237 */ 1238 bp_init(MMU_PAGESIZE, HAT_STORECACHING_OK); 1239 1240 /* 1241 * orig_npages is non-zero if physmem has been configured for less 1242 * than the available memory. 1243 */ 1244 if (orig_npages) { 1245 cmn_err(CE_WARN, "!%slimiting physmem to 0x%lx of 0x%lx pages", 1246 (npages == PHYSMEM ? "Due to virtual address space " : ""), 1247 npages, orig_npages); 1248 } 1249 #if defined(__i386) 1250 if (eprom_kernelbase && (eprom_kernelbase != kernelbase)) 1251 cmn_err(CE_WARN, "kernelbase value, User specified 0x%lx, " 1252 "System using 0x%lx", 1253 (uintptr_t)eprom_kernelbase, (uintptr_t)kernelbase); 1254 #endif 1255 1256 #ifdef KERNELBASE_ABI_MIN 1257 if (kernelbase < (uintptr_t)KERNELBASE_ABI_MIN) { 1258 cmn_err(CE_NOTE, "!kernelbase set to 0x%lx, system is not " 1259 "i386 ABI compliant.", (uintptr_t)kernelbase); 1260 } 1261 #endif 1262 1263 PRM_POINT("startup_kmem() done"); 1264 } 1265 1266 static void 1267 startup_modules(void) 1268 { 1269 unsigned int i; 1270 extern void prom_setup(void); 1271 1272 PRM_POINT("startup_modules() starting..."); 1273 /* 1274 * Initialize ten-micro second timer so that drivers will 1275 * not get short changed in their init phase. This was 1276 * not getting called until clkinit which, on fast cpu's 1277 * caused the drv_usecwait to be way too short. 1278 */ 1279 microfind(); 1280 1281 /* 1282 * Read the GMT lag from /etc/rtc_config. 1283 */ 1284 sgmtl(process_rtc_config_file()); 1285 1286 /* 1287 * Calculate default settings of system parameters based upon 1288 * maxusers, yet allow to be overridden via the /etc/system file. 1289 */ 1290 param_calc(0); 1291 1292 mod_setup(); 1293 1294 /* 1295 * Initialize system parameters. 1296 */ 1297 param_init(); 1298 1299 /* 1300 * Initialize the default brands 1301 */ 1302 brand_init(); 1303 1304 /* 1305 * maxmem is the amount of physical memory we're playing with. 1306 */ 1307 maxmem = physmem; 1308 1309 /* 1310 * Initialize segment management stuff. 1311 */ 1312 seg_init(); 1313 1314 if (modload("fs", "specfs") == -1) 1315 halt("Can't load specfs"); 1316 1317 if (modload("fs", "devfs") == -1) 1318 halt("Can't load devfs"); 1319 1320 if (modload("fs", "dev") == -1) 1321 halt("Can't load dev"); 1322 1323 (void) modloadonly("sys", "lbl_edition"); 1324 1325 dispinit(); 1326 1327 /* 1328 * This is needed here to initialize hw_serial[] for cluster booting. 1329 */ 1330 if ((i = modload("misc", "sysinit")) != (unsigned int)-1) 1331 (void) modunload(i); 1332 else 1333 cmn_err(CE_CONT, "sysinit load failed"); 1334 1335 /* Read cluster configuration data. */ 1336 clconf_init(); 1337 1338 /* 1339 * Create a kernel device tree. First, create rootnex and 1340 * then invoke bus specific code to probe devices. 1341 */ 1342 setup_ddi(); 1343 1344 /* 1345 * Set up the CPU module subsystem. Modifies the device tree, so it 1346 * must be done after setup_ddi(). 1347 */ 1348 cmi_init(); 1349 1350 /* 1351 * Initialize the MCA handlers 1352 */ 1353 if (x86_feature & X86_MCA) 1354 cmi_mca_init(); 1355 1356 /* 1357 * Fake a prom tree such that /dev/openprom continues to work 1358 */ 1359 PRM_POINT("startup_modules: calling prom_setup..."); 1360 prom_setup(); 1361 PRM_POINT("startup_modules: done"); 1362 1363 /* 1364 * Load all platform specific modules 1365 */ 1366 PRM_POINT("startup_modules: calling psm_modload..."); 1367 psm_modload(); 1368 1369 PRM_POINT("startup_modules() done"); 1370 } 1371 1372 /* 1373 * claim a "setaside" boot page for use in the kernel 1374 */ 1375 page_t * 1376 boot_claim_page(pfn_t pfn) 1377 { 1378 page_t *pp; 1379 1380 pp = page_numtopp_nolock(pfn); 1381 ASSERT(pp != NULL); 1382 1383 if (PP_ISBOOTPAGES(pp)) { 1384 if (pp->p_next != NULL) 1385 pp->p_next->p_prev = pp->p_prev; 1386 if (pp->p_prev == NULL) 1387 bootpages = pp->p_next; 1388 else 1389 pp->p_prev->p_next = pp->p_next; 1390 } else { 1391 /* 1392 * htable_attach() expects a base pagesize page 1393 */ 1394 if (pp->p_szc != 0) 1395 page_boot_demote(pp); 1396 pp = page_numtopp(pfn, SE_EXCL); 1397 } 1398 return (pp); 1399 } 1400 1401 /* 1402 * Walk through the pagetables looking for pages mapped in by boot. If the 1403 * setaside flag is set the pages are expected to be returned to the 1404 * kernel later in boot, so we add them to the bootpages list. 1405 */ 1406 static void 1407 protect_boot_range(uintptr_t low, uintptr_t high, int setaside) 1408 { 1409 uintptr_t va = low; 1410 size_t len; 1411 uint_t prot; 1412 pfn_t pfn; 1413 page_t *pp; 1414 pgcnt_t boot_protect_cnt = 0; 1415 1416 while (kbm_probe(&va, &len, &pfn, &prot) != 0 && va < high) { 1417 if (va + len >= high) 1418 panic("0x%lx byte mapping at 0x%p exceeds boot's " 1419 "legal range.", len, (void *)va); 1420 1421 while (len > 0) { 1422 pp = page_numtopp_alloc(pfn); 1423 if (pp != NULL) { 1424 if (setaside == 0) 1425 panic("Unexpected mapping by boot. " 1426 "addr=%p pfn=%lx\n", 1427 (void *)va, pfn); 1428 1429 pp->p_next = bootpages; 1430 pp->p_prev = NULL; 1431 PP_SETBOOTPAGES(pp); 1432 if (bootpages != NULL) { 1433 bootpages->p_prev = pp; 1434 } 1435 bootpages = pp; 1436 ++boot_protect_cnt; 1437 } 1438 1439 ++pfn; 1440 len -= MMU_PAGESIZE; 1441 va += MMU_PAGESIZE; 1442 } 1443 } 1444 PRM_DEBUG(boot_protect_cnt); 1445 } 1446 1447 /* 1448 * 1449 */ 1450 static void 1451 layout_kernel_va(void) 1452 { 1453 PRM_POINT("layout_kernel_va() starting..."); 1454 /* 1455 * Establish the final size of the kernel's heap, size of segmap, 1456 * segkp, etc. 1457 */ 1458 1459 #if defined(__amd64) 1460 1461 kpm_vbase = (caddr_t)segkpm_base; 1462 kpm_size = ROUND_UP_LPAGE(mmu_ptob(physmax + 1)); 1463 if ((uintptr_t)kpm_vbase + kpm_size > (uintptr_t)valloc_base) 1464 panic("not enough room for kpm!"); 1465 PRM_DEBUG(kpm_size); 1466 PRM_DEBUG(kpm_vbase); 1467 1468 /* 1469 * By default we create a seg_kp in 64 bit kernels, it's a little 1470 * faster to access than embedding it in the heap. 1471 */ 1472 segkp_base = (caddr_t)valloc_base + valloc_sz; 1473 if (!segkp_fromheap) { 1474 size_t sz = mmu_ptob(segkpsize); 1475 1476 /* 1477 * determine size of segkp 1478 */ 1479 if (sz < SEGKPMINSIZE || sz > SEGKPMAXSIZE) { 1480 sz = SEGKPDEFSIZE; 1481 cmn_err(CE_WARN, "!Illegal value for segkpsize. " 1482 "segkpsize has been reset to %ld pages", 1483 mmu_btop(sz)); 1484 } 1485 sz = MIN(sz, MAX(SEGKPMINSIZE, mmu_ptob(physmem))); 1486 1487 segkpsize = mmu_btop(ROUND_UP_LPAGE(sz)); 1488 } 1489 PRM_DEBUG(segkp_base); 1490 PRM_DEBUG(segkpsize); 1491 1492 /* 1493 * segzio is used for ZFS cached data. It uses a distinct VA 1494 * segment (from kernel heap) so that we can easily tell not to 1495 * include it in kernel crash dumps on 64 bit kernels. The trick is 1496 * to give it lots of VA, but not constrain the kernel heap. 1497 * We scale the size of segzio linearly with physmem up to 1498 * SEGZIOMAXSIZE. Above that amount it scales at 50% of physmem. 1499 */ 1500 segzio_base = segkp_base + mmu_ptob(segkpsize); 1501 if (segzio_fromheap) { 1502 segziosize = 0; 1503 } else { 1504 size_t physmem_size = mmu_ptob(physmem); 1505 size_t size = (segziosize == 0) ? 1506 physmem_size : mmu_ptob(segziosize); 1507 1508 if (size < SEGZIOMINSIZE) 1509 size = SEGZIOMINSIZE; 1510 if (size > SEGZIOMAXSIZE) { 1511 size = SEGZIOMAXSIZE; 1512 if (physmem_size > size) 1513 size += (physmem_size - size) / 2; 1514 } 1515 segziosize = mmu_btop(ROUND_UP_LPAGE(size)); 1516 } 1517 PRM_DEBUG(segziosize); 1518 PRM_DEBUG(segzio_base); 1519 1520 /* 1521 * Put the range of VA for device mappings next, kmdb knows to not 1522 * grep in this range of addresses. 1523 */ 1524 toxic_addr = 1525 ROUND_UP_LPAGE((uintptr_t)segzio_base + mmu_ptob(segziosize)); 1526 PRM_DEBUG(toxic_addr); 1527 segmap_start = ROUND_UP_LPAGE(toxic_addr + toxic_size); 1528 #else /* __i386 */ 1529 segmap_start = ROUND_UP_LPAGE(kernelbase); 1530 #endif /* __i386 */ 1531 PRM_DEBUG(segmap_start); 1532 1533 /* 1534 * Users can change segmapsize through eeprom or /etc/system. 1535 * If the variable is tuned through eeprom, there is no upper 1536 * bound on the size of segmap. If it is tuned through 1537 * /etc/system on 32-bit systems, it must be no larger than we 1538 * planned for in startup_memlist(). 1539 */ 1540 segmapsize = MAX(ROUND_UP_LPAGE(segmapsize), SEGMAPDEFAULT); 1541 1542 #if defined(__i386) 1543 /* 1544 * 32-bit systems don't have segkpm or segkp, so segmap appears at 1545 * the bottom of the kernel's address range. Set aside space for a 1546 * small red zone just below the start of segmap. 1547 */ 1548 segmap_start += KERNEL_REDZONE_SIZE; 1549 segmapsize -= KERNEL_REDZONE_SIZE; 1550 #endif 1551 1552 PRM_DEBUG(segmap_start); 1553 PRM_DEBUG(segmapsize); 1554 kernelheap = (caddr_t)ROUND_UP_LPAGE(segmap_start + segmapsize); 1555 PRM_DEBUG(kernelheap); 1556 PRM_POINT("layout_kernel_va() done..."); 1557 } 1558 1559 /* 1560 * Finish initializing the VM system, now that we are no longer 1561 * relying on the boot time memory allocators. 1562 */ 1563 static void 1564 startup_vm(void) 1565 { 1566 struct segmap_crargs a; 1567 1568 extern int use_brk_lpg, use_stk_lpg; 1569 1570 PRM_POINT("startup_vm() starting..."); 1571 1572 /* 1573 * Initialize the hat layer. 1574 */ 1575 hat_init(); 1576 1577 /* 1578 * Do final allocations of HAT data structures that need to 1579 * be allocated before quiescing the boot loader. 1580 */ 1581 PRM_POINT("Calling hat_kern_alloc()..."); 1582 hat_kern_alloc((caddr_t)segmap_start, segmapsize, ekernelheap); 1583 PRM_POINT("hat_kern_alloc() done"); 1584 1585 /* 1586 * Setup MTRR (Memory type range registers) 1587 */ 1588 setup_mtrr(); 1589 1590 /* 1591 * The next two loops are done in distinct steps in order 1592 * to be sure that any page that is doubly mapped (both above 1593 * KERNEL_TEXT and below kernelbase) is dealt with correctly. 1594 * Note this may never happen, but it might someday. 1595 */ 1596 bootpages = NULL; 1597 PRM_POINT("Protecting boot pages"); 1598 1599 /* 1600 * Protect any pages mapped above KERNEL_TEXT that somehow have 1601 * page_t's. This can only happen if something weird allocated 1602 * in this range (like kadb/kmdb). 1603 */ 1604 protect_boot_range(KERNEL_TEXT, (uintptr_t)-1, 0); 1605 1606 /* 1607 * Before we can take over memory allocation/mapping from the boot 1608 * loader we must remove from our free page lists any boot allocated 1609 * pages that stay mapped until release_bootstrap(). 1610 */ 1611 protect_boot_range(0, kernelbase, 1); 1612 1613 /* 1614 * Switch to running on regular HAT (not boot_mmu) 1615 */ 1616 PRM_POINT("Calling hat_kern_setup()..."); 1617 hat_kern_setup(); 1618 1619 /* 1620 * It is no longer safe to call BOP_ALLOC(), so make sure we don't. 1621 */ 1622 bop_no_more_mem(); 1623 1624 PRM_POINT("hat_kern_setup() done"); 1625 1626 hat_cpu_online(CPU); 1627 1628 /* 1629 * Initialize VM system 1630 */ 1631 PRM_POINT("Calling kvm_init()..."); 1632 kvm_init(); 1633 PRM_POINT("kvm_init() done"); 1634 1635 /* 1636 * Tell kmdb that the VM system is now working 1637 */ 1638 if (boothowto & RB_DEBUG) 1639 kdi_dvec_vmready(); 1640 1641 /* 1642 * Mangle the brand string etc. 1643 */ 1644 cpuid_pass3(CPU); 1645 1646 #if defined(__amd64) 1647 1648 /* 1649 * Create the device arena for toxic (to dtrace/kmdb) mappings. 1650 */ 1651 device_arena = vmem_create("device", (void *)toxic_addr, 1652 toxic_size, MMU_PAGESIZE, NULL, NULL, NULL, 0, VM_SLEEP); 1653 1654 #else /* __i386 */ 1655 1656 /* 1657 * allocate the bit map that tracks toxic pages 1658 */ 1659 toxic_bit_map_len = btop((ulong_t)(valloc_base - kernelbase)); 1660 PRM_DEBUG(toxic_bit_map_len); 1661 toxic_bit_map = 1662 kmem_zalloc(BT_SIZEOFMAP(toxic_bit_map_len), KM_NOSLEEP); 1663 ASSERT(toxic_bit_map != NULL); 1664 PRM_DEBUG(toxic_bit_map); 1665 1666 #endif /* __i386 */ 1667 1668 1669 /* 1670 * Now that we've got more VA, as well as the ability to allocate from 1671 * it, tell the debugger. 1672 */ 1673 if (boothowto & RB_DEBUG) 1674 kdi_dvec_memavail(); 1675 1676 /* 1677 * The following code installs a special page fault handler (#pf) 1678 * to work around a pentium bug. 1679 */ 1680 #if !defined(__amd64) 1681 if (x86_type == X86_TYPE_P5) { 1682 desctbr_t idtr; 1683 gate_desc_t *newidt; 1684 struct machcpu *mcpu = &CPU->cpu_m; 1685 1686 if ((newidt = kmem_zalloc(MMU_PAGESIZE, KM_NOSLEEP)) == NULL) 1687 panic("failed to install pentium_pftrap"); 1688 1689 bcopy(idt0, newidt, sizeof (idt0)); 1690 set_gatesegd(&newidt[T_PGFLT], &pentium_pftrap, 1691 KCS_SEL, SDT_SYSIGT, SEL_KPL); 1692 1693 (void) as_setprot(&kas, (caddr_t)newidt, MMU_PAGESIZE, 1694 PROT_READ|PROT_EXEC); 1695 1696 mcpu->mcpu_idt = newidt; 1697 idtr.dtr_base = (uintptr_t)mcpu->mcpu_idt; 1698 idtr.dtr_limit = sizeof (idt0) - 1; 1699 wr_idtr(&idtr); 1700 } 1701 #endif /* !__amd64 */ 1702 1703 /* 1704 * Map page pfn=0 for drivers, such as kd, that need to pick up 1705 * parameters left there by controllers/BIOS. 1706 */ 1707 PRM_POINT("setup up p0_va"); 1708 p0_va = i86devmap(0, 1, PROT_READ); 1709 PRM_DEBUG(p0_va); 1710 1711 cmn_err(CE_CONT, "?mem = %luK (0x%lx)\n", 1712 physinstalled << (MMU_PAGESHIFT - 10), ptob(physinstalled)); 1713 1714 /* 1715 * disable automatic large pages for small memory systems or 1716 * when the disable flag is set. 1717 */ 1718 if (!auto_lpg_disable && mmu.max_page_level > 0) { 1719 max_uheap_lpsize = LEVEL_SIZE(1); 1720 max_ustack_lpsize = LEVEL_SIZE(1); 1721 max_privmap_lpsize = LEVEL_SIZE(1); 1722 max_uidata_lpsize = LEVEL_SIZE(1); 1723 max_utext_lpsize = LEVEL_SIZE(1); 1724 max_shm_lpsize = LEVEL_SIZE(1); 1725 } 1726 if (physmem < privm_lpg_min_physmem || mmu.max_page_level == 0 || 1727 auto_lpg_disable) { 1728 use_brk_lpg = 0; 1729 use_stk_lpg = 0; 1730 } 1731 if (mmu.max_page_level > 0) { 1732 mcntl0_lpsize = LEVEL_SIZE(1); 1733 } 1734 1735 PRM_POINT("Calling hat_init_finish()..."); 1736 hat_init_finish(); 1737 PRM_POINT("hat_init_finish() done"); 1738 1739 /* 1740 * Initialize the segkp segment type. 1741 */ 1742 rw_enter(&kas.a_lock, RW_WRITER); 1743 PRM_POINT("Attaching segkp"); 1744 if (segkp_fromheap) { 1745 segkp->s_as = &kas; 1746 } else if (seg_attach(&kas, (caddr_t)segkp_base, mmu_ptob(segkpsize), 1747 segkp) < 0) { 1748 panic("startup: cannot attach segkp"); 1749 /*NOTREACHED*/ 1750 } 1751 PRM_POINT("Doing segkp_create()"); 1752 if (segkp_create(segkp) != 0) { 1753 panic("startup: segkp_create failed"); 1754 /*NOTREACHED*/ 1755 } 1756 PRM_DEBUG(segkp); 1757 rw_exit(&kas.a_lock); 1758 1759 /* 1760 * kpm segment 1761 */ 1762 segmap_kpm = 0; 1763 if (kpm_desired) { 1764 kpm_init(); 1765 kpm_enable = 1; 1766 vpm_enable = 1; 1767 } 1768 1769 /* 1770 * Now create segmap segment. 1771 */ 1772 rw_enter(&kas.a_lock, RW_WRITER); 1773 if (seg_attach(&kas, (caddr_t)segmap_start, segmapsize, segmap) < 0) { 1774 panic("cannot attach segmap"); 1775 /*NOTREACHED*/ 1776 } 1777 PRM_DEBUG(segmap); 1778 1779 a.prot = PROT_READ | PROT_WRITE; 1780 a.shmsize = 0; 1781 a.nfreelist = segmapfreelists; 1782 1783 if (segmap_create(segmap, (caddr_t)&a) != 0) 1784 panic("segmap_create segmap"); 1785 rw_exit(&kas.a_lock); 1786 1787 setup_vaddr_for_ppcopy(CPU); 1788 1789 segdev_init(); 1790 pmem_init(); 1791 1792 PRM_POINT("startup_vm() done"); 1793 } 1794 1795 /* 1796 * Load a tod module for the non-standard tod part found on this system. 1797 */ 1798 static void 1799 load_tod_module(char *todmod) 1800 { 1801 if (modload("tod", todmod) == -1) 1802 halt("Can't load TOD module"); 1803 } 1804 1805 static void 1806 startup_end(void) 1807 { 1808 extern void setx86isalist(void); 1809 1810 PRM_POINT("startup_end() starting..."); 1811 1812 /* 1813 * Perform tasks that get done after most of the VM 1814 * initialization has been done but before the clock 1815 * and other devices get started. 1816 */ 1817 kern_setup1(); 1818 1819 /* 1820 * Perform CPC initialization for this CPU. 1821 */ 1822 kcpc_hw_init(CPU); 1823 1824 #if defined(OPTERON_WORKAROUND_6323525) 1825 if (opteron_workaround_6323525) 1826 patch_workaround_6323525(); 1827 #endif 1828 /* 1829 * If needed, load TOD module now so that ddi_get_time(9F) etc. work 1830 * (For now, "needed" is defined as set tod_module_name in /etc/system) 1831 */ 1832 if (tod_module_name != NULL) { 1833 PRM_POINT("load_tod_module()"); 1834 load_tod_module(tod_module_name); 1835 } 1836 1837 /* 1838 * Configure the system. 1839 */ 1840 PRM_POINT("Calling configure()..."); 1841 configure(); /* set up devices */ 1842 PRM_POINT("configure() done"); 1843 1844 /* 1845 * Set the isa_list string to the defined instruction sets we 1846 * support. 1847 */ 1848 setx86isalist(); 1849 cpu_intr_alloc(CPU, NINTR_THREADS); 1850 psm_install(); 1851 1852 /* 1853 * We're done with bootops. We don't unmap the bootstrap yet because 1854 * we're still using bootsvcs. 1855 */ 1856 PRM_POINT("NULLing out bootops"); 1857 *bootopsp = (struct bootops *)NULL; 1858 bootops = (struct bootops *)NULL; 1859 1860 PRM_POINT("Enabling interrupts"); 1861 (*picinitf)(); 1862 sti(); 1863 1864 (void) add_avsoftintr((void *)&softlevel1_hdl, 1, softlevel1, 1865 "softlevel1", NULL, NULL); /* XXX to be moved later */ 1866 1867 PRM_POINT("startup_end() done"); 1868 } 1869 1870 extern char hw_serial[]; 1871 char *_hs1107 = hw_serial; 1872 ulong_t _bdhs34; 1873 1874 void 1875 post_startup(void) 1876 { 1877 /* 1878 * Set the system wide, processor-specific flags to be passed 1879 * to userland via the aux vector for performance hints and 1880 * instruction set extensions. 1881 */ 1882 bind_hwcap(); 1883 1884 /* 1885 * Load the System Management BIOS into the global ksmbios 1886 * handle, if an SMBIOS is present on this system. 1887 */ 1888 ksmbios = smbios_open(NULL, SMB_VERSION, ksmbios_flags, NULL); 1889 1890 /* 1891 * Startup the memory scrubber. 1892 */ 1893 memscrub_init(); 1894 1895 /* 1896 * Complete CPU module initialization 1897 */ 1898 cmi_post_init(); 1899 1900 /* 1901 * Perform forceloading tasks for /etc/system. 1902 */ 1903 (void) mod_sysctl(SYS_FORCELOAD, NULL); 1904 1905 /* 1906 * ON4.0: Force /proc module in until clock interrupt handle fixed 1907 * ON4.0: This must be fixed or restated in /etc/systems. 1908 */ 1909 (void) modload("fs", "procfs"); 1910 1911 #if defined(__i386) 1912 /* 1913 * Check for required functional Floating Point hardware, 1914 * unless FP hardware explicitly disabled. 1915 */ 1916 if (fpu_exists && (fpu_pentium_fdivbug || fp_kind == FP_NO)) 1917 halt("No working FP hardware found"); 1918 #endif 1919 1920 maxmem = freemem; 1921 1922 add_cpunode2devtree(CPU->cpu_id, CPU->cpu_m.mcpu_cpi); 1923 } 1924 1925 static int 1926 pp_in_ramdisk(page_t *pp) 1927 { 1928 extern uint64_t ramdisk_start, ramdisk_end; 1929 1930 return ((pp->p_pagenum >= btop(ramdisk_start)) && 1931 (pp->p_pagenum < btopr(ramdisk_end))); 1932 } 1933 1934 void 1935 release_bootstrap(void) 1936 { 1937 int root_is_ramdisk; 1938 page_t *pp; 1939 extern void kobj_boot_unmountroot(void); 1940 extern dev_t rootdev; 1941 1942 /* unmount boot ramdisk and release kmem usage */ 1943 kobj_boot_unmountroot(); 1944 1945 /* 1946 * We're finished using the boot loader so free its pages. 1947 */ 1948 PRM_POINT("Unmapping lower boot pages"); 1949 clear_boot_mappings(0, _userlimit); 1950 postbootkernelbase = kernelbase; 1951 1952 /* 1953 * If root isn't on ramdisk, destroy the hardcoded 1954 * ramdisk node now and release the memory. Else, 1955 * ramdisk memory is kept in rd_pages. 1956 */ 1957 root_is_ramdisk = (getmajor(rootdev) == ddi_name_to_major("ramdisk")); 1958 if (!root_is_ramdisk) { 1959 dev_info_t *dip = ddi_find_devinfo("ramdisk", -1, 0); 1960 ASSERT(dip && ddi_get_parent(dip) == ddi_root_node()); 1961 ndi_rele_devi(dip); /* held from ddi_find_devinfo */ 1962 (void) ddi_remove_child(dip, 0); 1963 } 1964 1965 PRM_POINT("Releasing boot pages"); 1966 while (bootpages) { 1967 pp = bootpages; 1968 bootpages = pp->p_next; 1969 if (root_is_ramdisk && pp_in_ramdisk(pp)) { 1970 pp->p_next = rd_pages; 1971 rd_pages = pp; 1972 continue; 1973 } 1974 pp->p_next = (struct page *)0; 1975 pp->p_prev = (struct page *)0; 1976 PP_CLRBOOTPAGES(pp); 1977 page_free(pp, 1); 1978 } 1979 PRM_POINT("Boot pages released"); 1980 1981 /* 1982 * Find 1 page below 1 MB so that other processors can boot up. 1983 * Make sure it has a kernel VA as well as a 1:1 mapping. 1984 * We should have just free'd one up. 1985 */ 1986 if (use_mp) { 1987 pfn_t pfn; 1988 1989 for (pfn = 1; pfn < btop(1*1024*1024); pfn++) { 1990 if (page_numtopp_alloc(pfn) == NULL) 1991 continue; 1992 rm_platter_va = i86devmap(pfn, 1, 1993 PROT_READ | PROT_WRITE | PROT_EXEC); 1994 rm_platter_pa = ptob(pfn); 1995 hat_devload(kas.a_hat, 1996 (caddr_t)(uintptr_t)rm_platter_pa, MMU_PAGESIZE, 1997 pfn, PROT_READ | PROT_WRITE | PROT_EXEC, 1998 HAT_LOAD_NOCONSIST); 1999 break; 2000 } 2001 if (pfn == btop(1*1024*1024)) 2002 panic("No page available for starting " 2003 "other processors"); 2004 } 2005 2006 } 2007 2008 /* 2009 * Initialize the platform-specific parts of a page_t. 2010 */ 2011 void 2012 add_physmem_cb(page_t *pp, pfn_t pnum) 2013 { 2014 pp->p_pagenum = pnum; 2015 pp->p_mapping = NULL; 2016 pp->p_embed = 0; 2017 pp->p_share = 0; 2018 pp->p_mlentry = 0; 2019 } 2020 2021 /* 2022 * kphysm_init() initializes physical memory. 2023 */ 2024 static pgcnt_t 2025 kphysm_init( 2026 page_t *pp, 2027 pgcnt_t npages) 2028 { 2029 struct memlist *pmem; 2030 struct memseg *cur_memseg; 2031 pfn_t base_pfn; 2032 pgcnt_t num; 2033 pgcnt_t pages_done = 0; 2034 uint64_t addr; 2035 uint64_t size; 2036 extern pfn_t ddiphysmin; 2037 2038 ASSERT(page_hash != NULL && page_hashsz != 0); 2039 2040 cur_memseg = memseg_base; 2041 for (pmem = phys_avail; pmem && npages; pmem = pmem->next) { 2042 /* 2043 * In a 32 bit kernel can't use higher memory if we're 2044 * not booting in PAE mode. This check takes care of that. 2045 */ 2046 addr = pmem->address; 2047 size = pmem->size; 2048 if (btop(addr) > physmax) 2049 continue; 2050 2051 /* 2052 * align addr and size - they may not be at page boundaries 2053 */ 2054 if ((addr & MMU_PAGEOFFSET) != 0) { 2055 addr += MMU_PAGEOFFSET; 2056 addr &= ~(uint64_t)MMU_PAGEOFFSET; 2057 size -= addr - pmem->address; 2058 } 2059 2060 /* only process pages below or equal to physmax */ 2061 if ((btop(addr + size) - 1) > physmax) 2062 size = ptob(physmax - btop(addr) + 1); 2063 2064 num = btop(size); 2065 if (num == 0) 2066 continue; 2067 2068 if (num > npages) 2069 num = npages; 2070 2071 npages -= num; 2072 pages_done += num; 2073 base_pfn = btop(addr); 2074 2075 if (prom_debug) 2076 prom_printf("MEMSEG addr=0x%" PRIx64 2077 " pgs=0x%lx pfn 0x%lx-0x%lx\n", 2078 addr, num, base_pfn, base_pfn + num); 2079 2080 /* 2081 * Ignore pages below ddiphysmin to simplify ddi memory 2082 * allocation with non-zero addr_lo requests. 2083 */ 2084 if (base_pfn < ddiphysmin) { 2085 if (base_pfn + num <= ddiphysmin) 2086 continue; 2087 pp += (ddiphysmin - base_pfn); 2088 num -= (ddiphysmin - base_pfn); 2089 base_pfn = ddiphysmin; 2090 } 2091 2092 /* 2093 * Build the memsegs entry 2094 */ 2095 cur_memseg->pages = pp; 2096 cur_memseg->epages = pp + num; 2097 cur_memseg->pages_base = base_pfn; 2098 cur_memseg->pages_end = base_pfn + num; 2099 2100 /* 2101 * Insert into memseg list in decreasing pfn range order. 2102 * Low memory is typically more fragmented such that this 2103 * ordering keeps the larger ranges at the front of the list 2104 * for code that searches memseg. 2105 * This ASSERTS that the memsegs coming in from boot are in 2106 * increasing physical address order and not contiguous. 2107 */ 2108 if (memsegs != NULL) { 2109 ASSERT(cur_memseg->pages_base >= memsegs->pages_end); 2110 cur_memseg->next = memsegs; 2111 } 2112 memsegs = cur_memseg; 2113 2114 /* 2115 * add_physmem() initializes the PSM part of the page 2116 * struct by calling the PSM back with add_physmem_cb(). 2117 * In addition it coalesces pages into larger pages as 2118 * it initializes them. 2119 */ 2120 add_physmem(pp, num, base_pfn); 2121 cur_memseg++; 2122 availrmem_initial += num; 2123 availrmem += num; 2124 2125 pp += num; 2126 } 2127 2128 PRM_DEBUG(availrmem_initial); 2129 PRM_DEBUG(availrmem); 2130 PRM_DEBUG(freemem); 2131 build_pfn_hash(); 2132 return (pages_done); 2133 } 2134 2135 /* 2136 * Kernel VM initialization. 2137 */ 2138 static void 2139 kvm_init(void) 2140 { 2141 ASSERT((((uintptr_t)s_text) & MMU_PAGEOFFSET) == 0); 2142 2143 /* 2144 * Put the kernel segments in kernel address space. 2145 */ 2146 rw_enter(&kas.a_lock, RW_WRITER); 2147 as_avlinit(&kas); 2148 2149 (void) seg_attach(&kas, s_text, e_moddata - s_text, &ktextseg); 2150 (void) segkmem_create(&ktextseg); 2151 2152 (void) seg_attach(&kas, (caddr_t)valloc_base, valloc_sz, &kvalloc); 2153 (void) segkmem_create(&kvalloc); 2154 2155 (void) seg_attach(&kas, kernelheap, 2156 ekernelheap - kernelheap, &kvseg); 2157 (void) segkmem_create(&kvseg); 2158 2159 if (core_size > 0) { 2160 PRM_POINT("attaching kvseg_core"); 2161 (void) seg_attach(&kas, (caddr_t)core_base, core_size, 2162 &kvseg_core); 2163 (void) segkmem_create(&kvseg_core); 2164 } 2165 2166 if (segziosize > 0) { 2167 PRM_POINT("attaching segzio"); 2168 (void) seg_attach(&kas, segzio_base, mmu_ptob(segziosize), 2169 &kzioseg); 2170 (void) segkmem_zio_create(&kzioseg); 2171 2172 /* create zio area covering new segment */ 2173 segkmem_zio_init(segzio_base, mmu_ptob(segziosize)); 2174 } 2175 2176 (void) seg_attach(&kas, kdi_segdebugbase, kdi_segdebugsize, &kdebugseg); 2177 (void) segkmem_create(&kdebugseg); 2178 2179 rw_exit(&kas.a_lock); 2180 2181 /* 2182 * Ensure that the red zone at kernelbase is never accessible. 2183 */ 2184 PRM_POINT("protecting redzone"); 2185 (void) as_setprot(&kas, (caddr_t)kernelbase, KERNEL_REDZONE_SIZE, 0); 2186 2187 /* 2188 * Make the text writable so that it can be hot patched by DTrace. 2189 */ 2190 (void) as_setprot(&kas, s_text, e_modtext - s_text, 2191 PROT_READ | PROT_WRITE | PROT_EXEC); 2192 2193 /* 2194 * Make data writable until end. 2195 */ 2196 (void) as_setprot(&kas, s_data, e_moddata - s_data, 2197 PROT_READ | PROT_WRITE | PROT_EXEC); 2198 } 2199 2200 /* 2201 * These are MTTR registers supported by P6 2202 */ 2203 static struct mtrrvar mtrrphys_arr[MAX_MTRRVAR]; 2204 static uint64_t mtrr64k, mtrr16k1, mtrr16k2; 2205 static uint64_t mtrr4k1, mtrr4k2, mtrr4k3; 2206 static uint64_t mtrr4k4, mtrr4k5, mtrr4k6; 2207 static uint64_t mtrr4k7, mtrr4k8, mtrrcap; 2208 uint64_t mtrrdef, pat_attr_reg; 2209 2210 /* 2211 * Disable reprogramming of MTRRs by default. 2212 */ 2213 int enable_relaxed_mtrr = 0; 2214 2215 void 2216 setup_mtrr(void) 2217 { 2218 int i, ecx; 2219 int vcnt; 2220 struct mtrrvar *mtrrphys; 2221 2222 if (!(x86_feature & X86_MTRR)) 2223 return; 2224 2225 mtrrcap = rdmsr(REG_MTRRCAP); 2226 mtrrdef = rdmsr(REG_MTRRDEF); 2227 if (mtrrcap & MTRRCAP_FIX) { 2228 mtrr64k = rdmsr(REG_MTRR64K); 2229 mtrr16k1 = rdmsr(REG_MTRR16K1); 2230 mtrr16k2 = rdmsr(REG_MTRR16K2); 2231 mtrr4k1 = rdmsr(REG_MTRR4K1); 2232 mtrr4k2 = rdmsr(REG_MTRR4K2); 2233 mtrr4k3 = rdmsr(REG_MTRR4K3); 2234 mtrr4k4 = rdmsr(REG_MTRR4K4); 2235 mtrr4k5 = rdmsr(REG_MTRR4K5); 2236 mtrr4k6 = rdmsr(REG_MTRR4K6); 2237 mtrr4k7 = rdmsr(REG_MTRR4K7); 2238 mtrr4k8 = rdmsr(REG_MTRR4K8); 2239 } 2240 if ((vcnt = (mtrrcap & MTRRCAP_VCNTMASK)) > MAX_MTRRVAR) 2241 vcnt = MAX_MTRRVAR; 2242 2243 for (i = 0, ecx = REG_MTRRPHYSBASE0, mtrrphys = mtrrphys_arr; 2244 i < vcnt - 1; i++, ecx += 2, mtrrphys++) { 2245 mtrrphys->mtrrphys_base = rdmsr(ecx); 2246 mtrrphys->mtrrphys_mask = rdmsr(ecx + 1); 2247 if ((x86_feature & X86_PAT) && enable_relaxed_mtrr) 2248 mtrrphys->mtrrphys_mask &= ~MTRRPHYSMASK_V; 2249 } 2250 if (x86_feature & X86_PAT) { 2251 if (enable_relaxed_mtrr) 2252 mtrrdef = MTRR_TYPE_WB|MTRRDEF_FE|MTRRDEF_E; 2253 pat_attr_reg = PAT_DEFAULT_ATTRIBUTE; 2254 } 2255 2256 mtrr_sync(); 2257 } 2258 2259 /* 2260 * Sync current cpu mtrr with the incore copy of mtrr. 2261 * This function has to be invoked with interrupts disabled 2262 * Currently we do not capture other cpu's. This is invoked on cpu0 2263 * just after reading /etc/system. 2264 * On other cpu's its invoked from mp_startup(). 2265 */ 2266 void 2267 mtrr_sync(void) 2268 { 2269 uint_t crvalue, cr0_orig; 2270 int vcnt, i, ecx; 2271 struct mtrrvar *mtrrphys; 2272 2273 cr0_orig = crvalue = getcr0(); 2274 crvalue |= CR0_CD; 2275 crvalue &= ~CR0_NW; 2276 setcr0(crvalue); 2277 invalidate_cache(); 2278 2279 reload_cr3(); 2280 if (x86_feature & X86_PAT) 2281 wrmsr(REG_MTRRPAT, pat_attr_reg); 2282 2283 wrmsr(REG_MTRRDEF, rdmsr(REG_MTRRDEF) & 2284 ~((uint64_t)(uintptr_t)MTRRDEF_E)); 2285 2286 if (mtrrcap & MTRRCAP_FIX) { 2287 wrmsr(REG_MTRR64K, mtrr64k); 2288 wrmsr(REG_MTRR16K1, mtrr16k1); 2289 wrmsr(REG_MTRR16K2, mtrr16k2); 2290 wrmsr(REG_MTRR4K1, mtrr4k1); 2291 wrmsr(REG_MTRR4K2, mtrr4k2); 2292 wrmsr(REG_MTRR4K3, mtrr4k3); 2293 wrmsr(REG_MTRR4K4, mtrr4k4); 2294 wrmsr(REG_MTRR4K5, mtrr4k5); 2295 wrmsr(REG_MTRR4K6, mtrr4k6); 2296 wrmsr(REG_MTRR4K7, mtrr4k7); 2297 wrmsr(REG_MTRR4K8, mtrr4k8); 2298 } 2299 if ((vcnt = (mtrrcap & MTRRCAP_VCNTMASK)) > MAX_MTRRVAR) 2300 vcnt = MAX_MTRRVAR; 2301 for (i = 0, ecx = REG_MTRRPHYSBASE0, mtrrphys = mtrrphys_arr; 2302 i < vcnt - 1; i++, ecx += 2, mtrrphys++) { 2303 wrmsr(ecx, mtrrphys->mtrrphys_base); 2304 wrmsr(ecx + 1, mtrrphys->mtrrphys_mask); 2305 } 2306 wrmsr(REG_MTRRDEF, mtrrdef); 2307 2308 reload_cr3(); 2309 invalidate_cache(); 2310 setcr0(cr0_orig); 2311 } 2312 2313 /* 2314 * resync mtrr so that BIOS is happy. Called from mdboot 2315 */ 2316 void 2317 mtrr_resync(void) 2318 { 2319 if ((x86_feature & X86_PAT) && enable_relaxed_mtrr) { 2320 /* 2321 * We could have changed the default mtrr definition. 2322 * Put it back to uncached which is what it is at power on 2323 */ 2324 mtrrdef = MTRR_TYPE_UC|MTRRDEF_FE|MTRRDEF_E; 2325 mtrr_sync(); 2326 } 2327 } 2328 2329 void 2330 get_system_configuration(void) 2331 { 2332 char prop[32]; 2333 u_longlong_t nodes_ll, cpus_pernode_ll, lvalue; 2334 2335 if (BOP_GETPROPLEN(bootops, "nodes") > sizeof (prop) || 2336 BOP_GETPROP(bootops, "nodes", prop) < 0 || 2337 kobj_getvalue(prop, &nodes_ll) == -1 || 2338 nodes_ll > MAXNODES || 2339 BOP_GETPROPLEN(bootops, "cpus_pernode") > sizeof (prop) || 2340 BOP_GETPROP(bootops, "cpus_pernode", prop) < 0 || 2341 kobj_getvalue(prop, &cpus_pernode_ll) == -1) { 2342 system_hardware.hd_nodes = 1; 2343 system_hardware.hd_cpus_per_node = 0; 2344 } else { 2345 system_hardware.hd_nodes = (int)nodes_ll; 2346 system_hardware.hd_cpus_per_node = (int)cpus_pernode_ll; 2347 } 2348 2349 if (BOP_GETPROPLEN(bootops, "kernelbase") > sizeof (prop) || 2350 BOP_GETPROP(bootops, "kernelbase", prop) < 0 || 2351 kobj_getvalue(prop, &lvalue) == -1) 2352 eprom_kernelbase = NULL; 2353 else 2354 eprom_kernelbase = (uintptr_t)lvalue; 2355 2356 if (BOP_GETPROPLEN(bootops, "segmapsize") > sizeof (prop) || 2357 BOP_GETPROP(bootops, "segmapsize", prop) < 0 || 2358 kobj_getvalue(prop, &lvalue) == -1) 2359 segmapsize = SEGMAPDEFAULT; 2360 else 2361 segmapsize = (uintptr_t)lvalue; 2362 2363 if (BOP_GETPROPLEN(bootops, "segmapfreelists") > sizeof (prop) || 2364 BOP_GETPROP(bootops, "segmapfreelists", prop) < 0 || 2365 kobj_getvalue(prop, &lvalue) == -1) 2366 segmapfreelists = 0; /* use segmap driver default */ 2367 else 2368 segmapfreelists = (int)lvalue; 2369 2370 /* physmem used to be here, but moved much earlier to fakebop.c */ 2371 } 2372 2373 /* 2374 * Add to a memory list. 2375 * start = start of new memory segment 2376 * len = length of new memory segment in bytes 2377 * new = pointer to a new struct memlist 2378 * memlistp = memory list to which to add segment. 2379 */ 2380 void 2381 memlist_add( 2382 uint64_t start, 2383 uint64_t len, 2384 struct memlist *new, 2385 struct memlist **memlistp) 2386 { 2387 struct memlist *cur; 2388 uint64_t end = start + len; 2389 2390 new->address = start; 2391 new->size = len; 2392 2393 cur = *memlistp; 2394 2395 while (cur) { 2396 if (cur->address >= end) { 2397 new->next = cur; 2398 *memlistp = new; 2399 new->prev = cur->prev; 2400 cur->prev = new; 2401 return; 2402 } 2403 ASSERT(cur->address + cur->size <= start); 2404 if (cur->next == NULL) { 2405 cur->next = new; 2406 new->prev = cur; 2407 new->next = NULL; 2408 return; 2409 } 2410 memlistp = &cur->next; 2411 cur = cur->next; 2412 } 2413 } 2414 2415 void 2416 kobj_vmem_init(vmem_t **text_arena, vmem_t **data_arena) 2417 { 2418 size_t tsize = e_modtext - modtext; 2419 size_t dsize = e_moddata - moddata; 2420 2421 *text_arena = vmem_create("module_text", tsize ? modtext : NULL, tsize, 2422 1, segkmem_alloc, segkmem_free, heaptext_arena, 0, VM_SLEEP); 2423 *data_arena = vmem_create("module_data", dsize ? moddata : NULL, dsize, 2424 1, segkmem_alloc, segkmem_free, heap32_arena, 0, VM_SLEEP); 2425 } 2426 2427 caddr_t 2428 kobj_text_alloc(vmem_t *arena, size_t size) 2429 { 2430 return (vmem_alloc(arena, size, VM_SLEEP | VM_BESTFIT)); 2431 } 2432 2433 /*ARGSUSED*/ 2434 caddr_t 2435 kobj_texthole_alloc(caddr_t addr, size_t size) 2436 { 2437 panic("unexpected call to kobj_texthole_alloc()"); 2438 /*NOTREACHED*/ 2439 return (0); 2440 } 2441 2442 /*ARGSUSED*/ 2443 void 2444 kobj_texthole_free(caddr_t addr, size_t size) 2445 { 2446 panic("unexpected call to kobj_texthole_free()"); 2447 } 2448 2449 /* 2450 * This is called just after configure() in startup(). 2451 * 2452 * The ISALIST concept is a bit hopeless on Intel, because 2453 * there's no guarantee of an ever-more-capable processor 2454 * given that various parts of the instruction set may appear 2455 * and disappear between different implementations. 2456 * 2457 * While it would be possible to correct it and even enhance 2458 * it somewhat, the explicit hardware capability bitmask allows 2459 * more flexibility. 2460 * 2461 * So, we just leave this alone. 2462 */ 2463 void 2464 setx86isalist(void) 2465 { 2466 char *tp; 2467 size_t len; 2468 extern char *isa_list; 2469 2470 #define TBUFSIZE 1024 2471 2472 tp = kmem_alloc(TBUFSIZE, KM_SLEEP); 2473 *tp = '\0'; 2474 2475 #if defined(__amd64) 2476 (void) strcpy(tp, "amd64 "); 2477 #endif 2478 2479 switch (x86_vendor) { 2480 case X86_VENDOR_Intel: 2481 case X86_VENDOR_AMD: 2482 case X86_VENDOR_TM: 2483 if (x86_feature & X86_CMOV) { 2484 /* 2485 * Pentium Pro or later 2486 */ 2487 (void) strcat(tp, "pentium_pro"); 2488 (void) strcat(tp, x86_feature & X86_MMX ? 2489 "+mmx pentium_pro " : " "); 2490 } 2491 /*FALLTHROUGH*/ 2492 case X86_VENDOR_Cyrix: 2493 /* 2494 * The Cyrix 6x86 does not have any Pentium features 2495 * accessible while not at privilege level 0. 2496 */ 2497 if (x86_feature & X86_CPUID) { 2498 (void) strcat(tp, "pentium"); 2499 (void) strcat(tp, x86_feature & X86_MMX ? 2500 "+mmx pentium " : " "); 2501 } 2502 break; 2503 default: 2504 break; 2505 } 2506 (void) strcat(tp, "i486 i386 i86"); 2507 len = strlen(tp) + 1; /* account for NULL at end of string */ 2508 isa_list = strcpy(kmem_alloc(len, KM_SLEEP), tp); 2509 kmem_free(tp, TBUFSIZE); 2510 2511 #undef TBUFSIZE 2512 } 2513 2514 2515 #ifdef __amd64 2516 2517 void * 2518 device_arena_alloc(size_t size, int vm_flag) 2519 { 2520 return (vmem_alloc(device_arena, size, vm_flag)); 2521 } 2522 2523 void 2524 device_arena_free(void *vaddr, size_t size) 2525 { 2526 vmem_free(device_arena, vaddr, size); 2527 } 2528 2529 #else /* __i386 */ 2530 2531 void * 2532 device_arena_alloc(size_t size, int vm_flag) 2533 { 2534 caddr_t vaddr; 2535 uintptr_t v; 2536 size_t start; 2537 size_t end; 2538 2539 vaddr = vmem_alloc(heap_arena, size, vm_flag); 2540 if (vaddr == NULL) 2541 return (NULL); 2542 2543 v = (uintptr_t)vaddr; 2544 ASSERT(v >= kernelbase); 2545 ASSERT(v + size <= valloc_base); 2546 2547 start = btop(v - kernelbase); 2548 end = btop(v + size - 1 - kernelbase); 2549 ASSERT(start < toxic_bit_map_len); 2550 ASSERT(end < toxic_bit_map_len); 2551 2552 while (start <= end) { 2553 BT_ATOMIC_SET(toxic_bit_map, start); 2554 ++start; 2555 } 2556 return (vaddr); 2557 } 2558 2559 void 2560 device_arena_free(void *vaddr, size_t size) 2561 { 2562 uintptr_t v = (uintptr_t)vaddr; 2563 size_t start; 2564 size_t end; 2565 2566 ASSERT(v >= kernelbase); 2567 ASSERT(v + size <= valloc_base); 2568 2569 start = btop(v - kernelbase); 2570 end = btop(v + size - 1 - kernelbase); 2571 ASSERT(start < toxic_bit_map_len); 2572 ASSERT(end < toxic_bit_map_len); 2573 2574 while (start <= end) { 2575 ASSERT(BT_TEST(toxic_bit_map, start) != 0); 2576 BT_ATOMIC_CLEAR(toxic_bit_map, start); 2577 ++start; 2578 } 2579 vmem_free(heap_arena, vaddr, size); 2580 } 2581 2582 /* 2583 * returns 1st address in range that is in device arena, or NULL 2584 * if len is not NULL it returns the length of the toxic range 2585 */ 2586 void * 2587 device_arena_contains(void *vaddr, size_t size, size_t *len) 2588 { 2589 uintptr_t v = (uintptr_t)vaddr; 2590 uintptr_t eaddr = v + size; 2591 size_t start; 2592 size_t end; 2593 2594 /* 2595 * if called very early by kmdb, just return NULL 2596 */ 2597 if (toxic_bit_map == NULL) 2598 return (NULL); 2599 2600 /* 2601 * First check if we're completely outside the bitmap range. 2602 */ 2603 if (v >= valloc_base || eaddr < kernelbase) 2604 return (NULL); 2605 2606 /* 2607 * Trim ends of search to look at only what the bitmap covers. 2608 */ 2609 if (v < kernelbase) 2610 v = kernelbase; 2611 start = btop(v - kernelbase); 2612 end = btop(eaddr - kernelbase); 2613 if (end >= toxic_bit_map_len) 2614 end = toxic_bit_map_len; 2615 2616 if (bt_range(toxic_bit_map, &start, &end, end) == 0) 2617 return (NULL); 2618 2619 v = kernelbase + ptob(start); 2620 if (len != NULL) 2621 *len = ptob(end - start); 2622 return ((void *)v); 2623 } 2624 2625 #endif /* __i386 */ 2626