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 *)&_userlimit32 = _userlimit; 1193 #endif 1194 PRM_DEBUG(_kernelbase); 1195 PRM_DEBUG(_userlimit); 1196 PRM_DEBUG(_userlimit32); 1197 1198 layout_kernel_va(); 1199 1200 #if defined(__i386) 1201 /* 1202 * If segmap is too large we can push the bottom of the kernel heap 1203 * higher than the base. Or worse, it could exceed the top of the 1204 * VA space entirely, causing it to wrap around. 1205 */ 1206 if (kernelheap >= ekernelheap || (uintptr_t)kernelheap < kernelbase) 1207 panic("too little address space available for kernelheap," 1208 " use eeprom for lower kernelbase or smaller segmapsize"); 1209 #endif /* __i386 */ 1210 1211 /* 1212 * Initialize the kernel heap. Note 3rd argument must be > 1st. 1213 */ 1214 kernelheap_init(kernelheap, ekernelheap, 1215 kernelheap + MMU_PAGESIZE, 1216 (void *)core_base, (void *)(core_base + core_size)); 1217 1218 /* 1219 * Initialize kernel memory allocator. 1220 */ 1221 kmem_init(); 1222 1223 /* 1224 * Factor in colorequiv to check additional 'equivalent' bins 1225 */ 1226 page_set_colorequiv_arr(); 1227 1228 /* 1229 * print this out early so that we know what's going on 1230 */ 1231 cmn_err(CE_CONT, "?features: %b\n", x86_feature, FMT_X86_FEATURE); 1232 1233 /* 1234 * Initialize bp_mapin(). 1235 */ 1236 bp_init(MMU_PAGESIZE, HAT_STORECACHING_OK); 1237 1238 /* 1239 * orig_npages is non-zero if physmem has been configured for less 1240 * than the available memory. 1241 */ 1242 if (orig_npages) { 1243 cmn_err(CE_WARN, "!%slimiting physmem to 0x%lx of 0x%lx pages", 1244 (npages == PHYSMEM ? "Due to virtual address space " : ""), 1245 npages, orig_npages); 1246 } 1247 #if defined(__i386) 1248 if (eprom_kernelbase && (eprom_kernelbase != kernelbase)) 1249 cmn_err(CE_WARN, "kernelbase value, User specified 0x%lx, " 1250 "System using 0x%lx", 1251 (uintptr_t)eprom_kernelbase, (uintptr_t)kernelbase); 1252 #endif 1253 1254 #ifdef KERNELBASE_ABI_MIN 1255 if (kernelbase < (uintptr_t)KERNELBASE_ABI_MIN) { 1256 cmn_err(CE_NOTE, "!kernelbase set to 0x%lx, system is not " 1257 "i386 ABI compliant.", (uintptr_t)kernelbase); 1258 } 1259 #endif 1260 1261 PRM_POINT("startup_kmem() done"); 1262 } 1263 1264 static void 1265 startup_modules(void) 1266 { 1267 unsigned int i; 1268 extern void prom_setup(void); 1269 1270 PRM_POINT("startup_modules() starting..."); 1271 /* 1272 * Initialize ten-micro second timer so that drivers will 1273 * not get short changed in their init phase. This was 1274 * not getting called until clkinit which, on fast cpu's 1275 * caused the drv_usecwait to be way too short. 1276 */ 1277 microfind(); 1278 1279 /* 1280 * Read the GMT lag from /etc/rtc_config. 1281 */ 1282 sgmtl(process_rtc_config_file()); 1283 1284 /* 1285 * Calculate default settings of system parameters based upon 1286 * maxusers, yet allow to be overridden via the /etc/system file. 1287 */ 1288 param_calc(0); 1289 1290 mod_setup(); 1291 1292 /* 1293 * Initialize system parameters. 1294 */ 1295 param_init(); 1296 1297 /* 1298 * Initialize the default brands 1299 */ 1300 brand_init(); 1301 1302 /* 1303 * maxmem is the amount of physical memory we're playing with. 1304 */ 1305 maxmem = physmem; 1306 1307 /* 1308 * Initialize segment management stuff. 1309 */ 1310 seg_init(); 1311 1312 if (modload("fs", "specfs") == -1) 1313 halt("Can't load specfs"); 1314 1315 if (modload("fs", "devfs") == -1) 1316 halt("Can't load devfs"); 1317 1318 if (modload("fs", "dev") == -1) 1319 halt("Can't load dev"); 1320 1321 (void) modloadonly("sys", "lbl_edition"); 1322 1323 dispinit(); 1324 1325 /* 1326 * This is needed here to initialize hw_serial[] for cluster booting. 1327 */ 1328 if ((i = modload("misc", "sysinit")) != (unsigned int)-1) 1329 (void) modunload(i); 1330 else 1331 cmn_err(CE_CONT, "sysinit load failed"); 1332 1333 /* Read cluster configuration data. */ 1334 clconf_init(); 1335 1336 /* 1337 * Create a kernel device tree. First, create rootnex and 1338 * then invoke bus specific code to probe devices. 1339 */ 1340 setup_ddi(); 1341 1342 /* 1343 * Set up the CPU module subsystem. Modifies the device tree, so it 1344 * must be done after setup_ddi(). 1345 */ 1346 cmi_init(); 1347 1348 /* 1349 * Initialize the MCA handlers 1350 */ 1351 if (x86_feature & X86_MCA) 1352 cmi_mca_init(); 1353 1354 /* 1355 * Fake a prom tree such that /dev/openprom continues to work 1356 */ 1357 PRM_POINT("startup_modules: calling prom_setup..."); 1358 prom_setup(); 1359 PRM_POINT("startup_modules: done"); 1360 1361 /* 1362 * Load all platform specific modules 1363 */ 1364 PRM_POINT("startup_modules: calling psm_modload..."); 1365 psm_modload(); 1366 1367 PRM_POINT("startup_modules() done"); 1368 } 1369 1370 /* 1371 * claim a "setaside" boot page for use in the kernel 1372 */ 1373 page_t * 1374 boot_claim_page(pfn_t pfn) 1375 { 1376 page_t *pp; 1377 1378 pp = page_numtopp_nolock(pfn); 1379 ASSERT(pp != NULL); 1380 1381 if (PP_ISBOOTPAGES(pp)) { 1382 if (pp->p_next != NULL) 1383 pp->p_next->p_prev = pp->p_prev; 1384 if (pp->p_prev == NULL) 1385 bootpages = pp->p_next; 1386 else 1387 pp->p_prev->p_next = pp->p_next; 1388 } else { 1389 /* 1390 * htable_attach() expects a base pagesize page 1391 */ 1392 if (pp->p_szc != 0) 1393 page_boot_demote(pp); 1394 pp = page_numtopp(pfn, SE_EXCL); 1395 } 1396 return (pp); 1397 } 1398 1399 /* 1400 * Walk through the pagetables looking for pages mapped in by boot. If the 1401 * setaside flag is set the pages are expected to be returned to the 1402 * kernel later in boot, so we add them to the bootpages list. 1403 */ 1404 static void 1405 protect_boot_range(uintptr_t low, uintptr_t high, int setaside) 1406 { 1407 uintptr_t va = low; 1408 size_t len; 1409 uint_t prot; 1410 pfn_t pfn; 1411 page_t *pp; 1412 pgcnt_t boot_protect_cnt = 0; 1413 1414 while (kbm_probe(&va, &len, &pfn, &prot) != 0 && va < high) { 1415 if (va + len >= high) 1416 panic("0x%lx byte mapping at 0x%p exceeds boot's " 1417 "legal range.", len, (void *)va); 1418 1419 while (len > 0) { 1420 pp = page_numtopp_alloc(pfn); 1421 if (pp != NULL) { 1422 if (setaside == 0) 1423 panic("Unexpected mapping by boot. " 1424 "addr=%p pfn=%lx\n", 1425 (void *)va, pfn); 1426 1427 pp->p_next = bootpages; 1428 pp->p_prev = NULL; 1429 PP_SETBOOTPAGES(pp); 1430 if (bootpages != NULL) { 1431 bootpages->p_prev = pp; 1432 } 1433 bootpages = pp; 1434 ++boot_protect_cnt; 1435 } 1436 1437 ++pfn; 1438 len -= MMU_PAGESIZE; 1439 va += MMU_PAGESIZE; 1440 } 1441 } 1442 PRM_DEBUG(boot_protect_cnt); 1443 } 1444 1445 /* 1446 * 1447 */ 1448 static void 1449 layout_kernel_va(void) 1450 { 1451 PRM_POINT("layout_kernel_va() starting..."); 1452 /* 1453 * Establish the final size of the kernel's heap, size of segmap, 1454 * segkp, etc. 1455 */ 1456 1457 #if defined(__amd64) 1458 1459 kpm_vbase = (caddr_t)segkpm_base; 1460 kpm_size = ROUND_UP_LPAGE(mmu_ptob(physmax + 1)); 1461 if ((uintptr_t)kpm_vbase + kpm_size > (uintptr_t)valloc_base) 1462 panic("not enough room for kpm!"); 1463 PRM_DEBUG(kpm_size); 1464 PRM_DEBUG(kpm_vbase); 1465 1466 /* 1467 * By default we create a seg_kp in 64 bit kernels, it's a little 1468 * faster to access than embedding it in the heap. 1469 */ 1470 segkp_base = (caddr_t)valloc_base + valloc_sz; 1471 if (!segkp_fromheap) { 1472 size_t sz = mmu_ptob(segkpsize); 1473 1474 /* 1475 * determine size of segkp 1476 */ 1477 if (sz < SEGKPMINSIZE || sz > SEGKPMAXSIZE) { 1478 sz = SEGKPDEFSIZE; 1479 cmn_err(CE_WARN, "!Illegal value for segkpsize. " 1480 "segkpsize has been reset to %ld pages", 1481 mmu_btop(sz)); 1482 } 1483 sz = MIN(sz, MAX(SEGKPMINSIZE, mmu_ptob(physmem))); 1484 1485 segkpsize = mmu_btop(ROUND_UP_LPAGE(sz)); 1486 } 1487 PRM_DEBUG(segkp_base); 1488 PRM_DEBUG(segkpsize); 1489 1490 /* 1491 * segzio is used for ZFS cached data. It uses a distinct VA 1492 * segment (from kernel heap) so that we can easily tell not to 1493 * include it in kernel crash dumps on 64 bit kernels. The trick is 1494 * to give it lots of VA, but not constrain the kernel heap. 1495 * We scale the size of segzio linearly with physmem up to 1496 * SEGZIOMAXSIZE. Above that amount it scales at 50% of physmem. 1497 */ 1498 segzio_base = segkp_base + mmu_ptob(segkpsize); 1499 if (segzio_fromheap) { 1500 segziosize = 0; 1501 } else { 1502 size_t physmem_size = mmu_ptob(physmem); 1503 size_t size = (segziosize == 0) ? 1504 physmem_size : mmu_ptob(segziosize); 1505 1506 if (size < SEGZIOMINSIZE) 1507 size = SEGZIOMINSIZE; 1508 if (size > SEGZIOMAXSIZE) { 1509 size = SEGZIOMAXSIZE; 1510 if (physmem_size > size) 1511 size += (physmem_size - size) / 2; 1512 } 1513 segziosize = mmu_btop(ROUND_UP_LPAGE(size)); 1514 } 1515 PRM_DEBUG(segziosize); 1516 PRM_DEBUG(segzio_base); 1517 1518 /* 1519 * Put the range of VA for device mappings next, kmdb knows to not 1520 * grep in this range of addresses. 1521 */ 1522 toxic_addr = 1523 ROUND_UP_LPAGE((uintptr_t)segzio_base + mmu_ptob(segziosize)); 1524 PRM_DEBUG(toxic_addr); 1525 segmap_start = ROUND_UP_LPAGE(toxic_addr + toxic_size); 1526 #else /* __i386 */ 1527 segmap_start = ROUND_UP_LPAGE(kernelbase); 1528 #endif /* __i386 */ 1529 PRM_DEBUG(segmap_start); 1530 1531 /* 1532 * Users can change segmapsize through eeprom or /etc/system. 1533 * If the variable is tuned through eeprom, there is no upper 1534 * bound on the size of segmap. If it is tuned through 1535 * /etc/system on 32-bit systems, it must be no larger than we 1536 * planned for in startup_memlist(). 1537 */ 1538 segmapsize = MAX(ROUND_UP_LPAGE(segmapsize), SEGMAPDEFAULT); 1539 1540 #if defined(__i386) 1541 /* 1542 * 32-bit systems don't have segkpm or segkp, so segmap appears at 1543 * the bottom of the kernel's address range. Set aside space for a 1544 * small red zone just below the start of segmap. 1545 */ 1546 segmap_start += KERNEL_REDZONE_SIZE; 1547 segmapsize -= KERNEL_REDZONE_SIZE; 1548 #endif 1549 1550 PRM_DEBUG(segmap_start); 1551 PRM_DEBUG(segmapsize); 1552 kernelheap = (caddr_t)ROUND_UP_LPAGE(segmap_start + segmapsize); 1553 PRM_DEBUG(kernelheap); 1554 PRM_POINT("layout_kernel_va() done..."); 1555 } 1556 1557 /* 1558 * Finish initializing the VM system, now that we are no longer 1559 * relying on the boot time memory allocators. 1560 */ 1561 static void 1562 startup_vm(void) 1563 { 1564 struct segmap_crargs a; 1565 1566 extern int use_brk_lpg, use_stk_lpg; 1567 1568 PRM_POINT("startup_vm() starting..."); 1569 1570 /* 1571 * Initialize the hat layer. 1572 */ 1573 hat_init(); 1574 1575 /* 1576 * Do final allocations of HAT data structures that need to 1577 * be allocated before quiescing the boot loader. 1578 */ 1579 PRM_POINT("Calling hat_kern_alloc()..."); 1580 hat_kern_alloc((caddr_t)segmap_start, segmapsize, ekernelheap); 1581 PRM_POINT("hat_kern_alloc() done"); 1582 1583 /* 1584 * Setup MTRR (Memory type range registers) 1585 */ 1586 setup_mtrr(); 1587 1588 /* 1589 * The next two loops are done in distinct steps in order 1590 * to be sure that any page that is doubly mapped (both above 1591 * KERNEL_TEXT and below kernelbase) is dealt with correctly. 1592 * Note this may never happen, but it might someday. 1593 */ 1594 bootpages = NULL; 1595 PRM_POINT("Protecting boot pages"); 1596 1597 /* 1598 * Protect any pages mapped above KERNEL_TEXT that somehow have 1599 * page_t's. This can only happen if something weird allocated 1600 * in this range (like kadb/kmdb). 1601 */ 1602 protect_boot_range(KERNEL_TEXT, (uintptr_t)-1, 0); 1603 1604 /* 1605 * Before we can take over memory allocation/mapping from the boot 1606 * loader we must remove from our free page lists any boot allocated 1607 * pages that stay mapped until release_bootstrap(). 1608 */ 1609 protect_boot_range(0, kernelbase, 1); 1610 1611 /* 1612 * Switch to running on regular HAT (not boot_mmu) 1613 */ 1614 PRM_POINT("Calling hat_kern_setup()..."); 1615 hat_kern_setup(); 1616 1617 /* 1618 * It is no longer safe to call BOP_ALLOC(), so make sure we don't. 1619 */ 1620 bop_no_more_mem(); 1621 1622 PRM_POINT("hat_kern_setup() done"); 1623 1624 hat_cpu_online(CPU); 1625 1626 /* 1627 * Initialize VM system 1628 */ 1629 PRM_POINT("Calling kvm_init()..."); 1630 kvm_init(); 1631 PRM_POINT("kvm_init() done"); 1632 1633 /* 1634 * Tell kmdb that the VM system is now working 1635 */ 1636 if (boothowto & RB_DEBUG) 1637 kdi_dvec_vmready(); 1638 1639 /* 1640 * Mangle the brand string etc. 1641 */ 1642 cpuid_pass3(CPU); 1643 1644 #if defined(__amd64) 1645 1646 /* 1647 * Create the device arena for toxic (to dtrace/kmdb) mappings. 1648 */ 1649 device_arena = vmem_create("device", (void *)toxic_addr, 1650 toxic_size, MMU_PAGESIZE, NULL, NULL, NULL, 0, VM_SLEEP); 1651 1652 #else /* __i386 */ 1653 1654 /* 1655 * allocate the bit map that tracks toxic pages 1656 */ 1657 toxic_bit_map_len = btop((ulong_t)(valloc_base - kernelbase)); 1658 PRM_DEBUG(toxic_bit_map_len); 1659 toxic_bit_map = 1660 kmem_zalloc(BT_SIZEOFMAP(toxic_bit_map_len), KM_NOSLEEP); 1661 ASSERT(toxic_bit_map != NULL); 1662 PRM_DEBUG(toxic_bit_map); 1663 1664 #endif /* __i386 */ 1665 1666 1667 /* 1668 * Now that we've got more VA, as well as the ability to allocate from 1669 * it, tell the debugger. 1670 */ 1671 if (boothowto & RB_DEBUG) 1672 kdi_dvec_memavail(); 1673 1674 /* 1675 * The following code installs a special page fault handler (#pf) 1676 * to work around a pentium bug. 1677 */ 1678 #if !defined(__amd64) 1679 if (x86_type == X86_TYPE_P5) { 1680 desctbr_t idtr; 1681 gate_desc_t *newidt; 1682 struct machcpu *mcpu = &CPU->cpu_m; 1683 1684 if ((newidt = kmem_zalloc(MMU_PAGESIZE, KM_NOSLEEP)) == NULL) 1685 panic("failed to install pentium_pftrap"); 1686 1687 bcopy(idt0, newidt, sizeof (idt0)); 1688 set_gatesegd(&newidt[T_PGFLT], &pentium_pftrap, 1689 KCS_SEL, SDT_SYSIGT, SEL_KPL); 1690 1691 (void) as_setprot(&kas, (caddr_t)newidt, MMU_PAGESIZE, 1692 PROT_READ|PROT_EXEC); 1693 1694 mcpu->mcpu_idt = newidt; 1695 idtr.dtr_base = (uintptr_t)mcpu->mcpu_idt; 1696 idtr.dtr_limit = sizeof (idt0) - 1; 1697 wr_idtr(&idtr); 1698 } 1699 #endif /* !__amd64 */ 1700 1701 /* 1702 * Map page pfn=0 for drivers, such as kd, that need to pick up 1703 * parameters left there by controllers/BIOS. 1704 */ 1705 PRM_POINT("setup up p0_va"); 1706 p0_va = i86devmap(0, 1, PROT_READ); 1707 PRM_DEBUG(p0_va); 1708 1709 cmn_err(CE_CONT, "?mem = %luK (0x%lx)\n", 1710 physinstalled << (MMU_PAGESHIFT - 10), ptob(physinstalled)); 1711 1712 /* 1713 * disable automatic large pages for small memory systems or 1714 * when the disable flag is set. 1715 */ 1716 if (!auto_lpg_disable && mmu.max_page_level > 0) { 1717 max_uheap_lpsize = LEVEL_SIZE(1); 1718 max_ustack_lpsize = LEVEL_SIZE(1); 1719 max_privmap_lpsize = LEVEL_SIZE(1); 1720 max_uidata_lpsize = LEVEL_SIZE(1); 1721 max_utext_lpsize = LEVEL_SIZE(1); 1722 max_shm_lpsize = LEVEL_SIZE(1); 1723 } 1724 if (physmem < privm_lpg_min_physmem || mmu.max_page_level == 0 || 1725 auto_lpg_disable) { 1726 use_brk_lpg = 0; 1727 use_stk_lpg = 0; 1728 } 1729 if (mmu.max_page_level > 0) { 1730 mcntl0_lpsize = LEVEL_SIZE(1); 1731 } 1732 1733 PRM_POINT("Calling hat_init_finish()..."); 1734 hat_init_finish(); 1735 PRM_POINT("hat_init_finish() done"); 1736 1737 /* 1738 * Initialize the segkp segment type. 1739 */ 1740 rw_enter(&kas.a_lock, RW_WRITER); 1741 PRM_POINT("Attaching segkp"); 1742 if (segkp_fromheap) { 1743 segkp->s_as = &kas; 1744 } else if (seg_attach(&kas, (caddr_t)segkp_base, mmu_ptob(segkpsize), 1745 segkp) < 0) { 1746 panic("startup: cannot attach segkp"); 1747 /*NOTREACHED*/ 1748 } 1749 PRM_POINT("Doing segkp_create()"); 1750 if (segkp_create(segkp) != 0) { 1751 panic("startup: segkp_create failed"); 1752 /*NOTREACHED*/ 1753 } 1754 PRM_DEBUG(segkp); 1755 rw_exit(&kas.a_lock); 1756 1757 /* 1758 * kpm segment 1759 */ 1760 segmap_kpm = 0; 1761 if (kpm_desired) { 1762 kpm_init(); 1763 kpm_enable = 1; 1764 vpm_enable = 1; 1765 } 1766 1767 /* 1768 * Now create segmap segment. 1769 */ 1770 rw_enter(&kas.a_lock, RW_WRITER); 1771 if (seg_attach(&kas, (caddr_t)segmap_start, segmapsize, segmap) < 0) { 1772 panic("cannot attach segmap"); 1773 /*NOTREACHED*/ 1774 } 1775 PRM_DEBUG(segmap); 1776 1777 a.prot = PROT_READ | PROT_WRITE; 1778 a.shmsize = 0; 1779 a.nfreelist = segmapfreelists; 1780 1781 if (segmap_create(segmap, (caddr_t)&a) != 0) 1782 panic("segmap_create segmap"); 1783 rw_exit(&kas.a_lock); 1784 1785 setup_vaddr_for_ppcopy(CPU); 1786 1787 segdev_init(); 1788 pmem_init(); 1789 1790 PRM_POINT("startup_vm() done"); 1791 } 1792 1793 /* 1794 * Load a tod module for the non-standard tod part found on this system. 1795 */ 1796 static void 1797 load_tod_module(char *todmod) 1798 { 1799 if (modload("tod", todmod) == -1) 1800 halt("Can't load TOD module"); 1801 } 1802 1803 static void 1804 startup_end(void) 1805 { 1806 extern void setx86isalist(void); 1807 1808 PRM_POINT("startup_end() starting..."); 1809 1810 /* 1811 * Perform tasks that get done after most of the VM 1812 * initialization has been done but before the clock 1813 * and other devices get started. 1814 */ 1815 kern_setup1(); 1816 1817 /* 1818 * Perform CPC initialization for this CPU. 1819 */ 1820 kcpc_hw_init(CPU); 1821 1822 #if defined(OPTERON_WORKAROUND_6323525) 1823 if (opteron_workaround_6323525) 1824 patch_workaround_6323525(); 1825 #endif 1826 /* 1827 * If needed, load TOD module now so that ddi_get_time(9F) etc. work 1828 * (For now, "needed" is defined as set tod_module_name in /etc/system) 1829 */ 1830 if (tod_module_name != NULL) { 1831 PRM_POINT("load_tod_module()"); 1832 load_tod_module(tod_module_name); 1833 } 1834 1835 /* 1836 * Configure the system. 1837 */ 1838 PRM_POINT("Calling configure()..."); 1839 configure(); /* set up devices */ 1840 PRM_POINT("configure() done"); 1841 1842 /* 1843 * Set the isa_list string to the defined instruction sets we 1844 * support. 1845 */ 1846 setx86isalist(); 1847 cpu_intr_alloc(CPU, NINTR_THREADS); 1848 psm_install(); 1849 1850 /* 1851 * We're done with bootops. We don't unmap the bootstrap yet because 1852 * we're still using bootsvcs. 1853 */ 1854 PRM_POINT("NULLing out bootops"); 1855 *bootopsp = (struct bootops *)NULL; 1856 bootops = (struct bootops *)NULL; 1857 1858 PRM_POINT("Enabling interrupts"); 1859 (*picinitf)(); 1860 sti(); 1861 1862 (void) add_avsoftintr((void *)&softlevel1_hdl, 1, softlevel1, 1863 "softlevel1", NULL, NULL); /* XXX to be moved later */ 1864 1865 PRM_POINT("startup_end() done"); 1866 } 1867 1868 extern char hw_serial[]; 1869 char *_hs1107 = hw_serial; 1870 ulong_t _bdhs34; 1871 1872 void 1873 post_startup(void) 1874 { 1875 /* 1876 * Set the system wide, processor-specific flags to be passed 1877 * to userland via the aux vector for performance hints and 1878 * instruction set extensions. 1879 */ 1880 bind_hwcap(); 1881 1882 /* 1883 * Load the System Management BIOS into the global ksmbios 1884 * handle, if an SMBIOS is present on this system. 1885 */ 1886 ksmbios = smbios_open(NULL, SMB_VERSION, ksmbios_flags, NULL); 1887 1888 /* 1889 * Startup the memory scrubber. 1890 */ 1891 memscrub_init(); 1892 1893 /* 1894 * Complete CPU module initialization 1895 */ 1896 cmi_post_init(); 1897 1898 /* 1899 * Perform forceloading tasks for /etc/system. 1900 */ 1901 (void) mod_sysctl(SYS_FORCELOAD, NULL); 1902 1903 /* 1904 * ON4.0: Force /proc module in until clock interrupt handle fixed 1905 * ON4.0: This must be fixed or restated in /etc/systems. 1906 */ 1907 (void) modload("fs", "procfs"); 1908 1909 #if defined(__i386) 1910 /* 1911 * Check for required functional Floating Point hardware, 1912 * unless FP hardware explicitly disabled. 1913 */ 1914 if (fpu_exists && (fpu_pentium_fdivbug || fp_kind == FP_NO)) 1915 halt("No working FP hardware found"); 1916 #endif 1917 1918 maxmem = freemem; 1919 1920 add_cpunode2devtree(CPU->cpu_id, CPU->cpu_m.mcpu_cpi); 1921 } 1922 1923 static int 1924 pp_in_ramdisk(page_t *pp) 1925 { 1926 extern uint64_t ramdisk_start, ramdisk_end; 1927 1928 return ((pp->p_pagenum >= btop(ramdisk_start)) && 1929 (pp->p_pagenum < btopr(ramdisk_end))); 1930 } 1931 1932 void 1933 release_bootstrap(void) 1934 { 1935 int root_is_ramdisk; 1936 page_t *pp; 1937 extern void kobj_boot_unmountroot(void); 1938 extern dev_t rootdev; 1939 1940 /* unmount boot ramdisk and release kmem usage */ 1941 kobj_boot_unmountroot(); 1942 1943 /* 1944 * We're finished using the boot loader so free its pages. 1945 */ 1946 PRM_POINT("Unmapping lower boot pages"); 1947 clear_boot_mappings(0, _userlimit); 1948 postbootkernelbase = kernelbase; 1949 1950 /* 1951 * If root isn't on ramdisk, destroy the hardcoded 1952 * ramdisk node now and release the memory. Else, 1953 * ramdisk memory is kept in rd_pages. 1954 */ 1955 root_is_ramdisk = (getmajor(rootdev) == ddi_name_to_major("ramdisk")); 1956 if (!root_is_ramdisk) { 1957 dev_info_t *dip = ddi_find_devinfo("ramdisk", -1, 0); 1958 ASSERT(dip && ddi_get_parent(dip) == ddi_root_node()); 1959 ndi_rele_devi(dip); /* held from ddi_find_devinfo */ 1960 (void) ddi_remove_child(dip, 0); 1961 } 1962 1963 PRM_POINT("Releasing boot pages"); 1964 while (bootpages) { 1965 pp = bootpages; 1966 bootpages = pp->p_next; 1967 if (root_is_ramdisk && pp_in_ramdisk(pp)) { 1968 pp->p_next = rd_pages; 1969 rd_pages = pp; 1970 continue; 1971 } 1972 pp->p_next = (struct page *)0; 1973 pp->p_prev = (struct page *)0; 1974 PP_CLRBOOTPAGES(pp); 1975 page_free(pp, 1); 1976 } 1977 PRM_POINT("Boot pages released"); 1978 1979 /* 1980 * Find 1 page below 1 MB so that other processors can boot up. 1981 * Make sure it has a kernel VA as well as a 1:1 mapping. 1982 * We should have just free'd one up. 1983 */ 1984 if (use_mp) { 1985 pfn_t pfn; 1986 1987 for (pfn = 1; pfn < btop(1*1024*1024); pfn++) { 1988 if (page_numtopp_alloc(pfn) == NULL) 1989 continue; 1990 rm_platter_va = i86devmap(pfn, 1, 1991 PROT_READ | PROT_WRITE | PROT_EXEC); 1992 rm_platter_pa = ptob(pfn); 1993 hat_devload(kas.a_hat, 1994 (caddr_t)(uintptr_t)rm_platter_pa, MMU_PAGESIZE, 1995 pfn, PROT_READ | PROT_WRITE | PROT_EXEC, 1996 HAT_LOAD_NOCONSIST); 1997 break; 1998 } 1999 if (pfn == btop(1*1024*1024)) 2000 panic("No page available for starting " 2001 "other processors"); 2002 } 2003 2004 } 2005 2006 /* 2007 * Initialize the platform-specific parts of a page_t. 2008 */ 2009 void 2010 add_physmem_cb(page_t *pp, pfn_t pnum) 2011 { 2012 pp->p_pagenum = pnum; 2013 pp->p_mapping = NULL; 2014 pp->p_embed = 0; 2015 pp->p_share = 0; 2016 pp->p_mlentry = 0; 2017 } 2018 2019 /* 2020 * kphysm_init() initializes physical memory. 2021 */ 2022 static pgcnt_t 2023 kphysm_init( 2024 page_t *pp, 2025 pgcnt_t npages) 2026 { 2027 struct memlist *pmem; 2028 struct memseg *cur_memseg; 2029 pfn_t base_pfn; 2030 pgcnt_t num; 2031 pgcnt_t pages_done = 0; 2032 uint64_t addr; 2033 uint64_t size; 2034 extern pfn_t ddiphysmin; 2035 2036 ASSERT(page_hash != NULL && page_hashsz != 0); 2037 2038 cur_memseg = memseg_base; 2039 for (pmem = phys_avail; pmem && npages; pmem = pmem->next) { 2040 /* 2041 * In a 32 bit kernel can't use higher memory if we're 2042 * not booting in PAE mode. This check takes care of that. 2043 */ 2044 addr = pmem->address; 2045 size = pmem->size; 2046 if (btop(addr) > physmax) 2047 continue; 2048 2049 /* 2050 * align addr and size - they may not be at page boundaries 2051 */ 2052 if ((addr & MMU_PAGEOFFSET) != 0) { 2053 addr += MMU_PAGEOFFSET; 2054 addr &= ~(uint64_t)MMU_PAGEOFFSET; 2055 size -= addr - pmem->address; 2056 } 2057 2058 /* only process pages below or equal to physmax */ 2059 if ((btop(addr + size) - 1) > physmax) 2060 size = ptob(physmax - btop(addr) + 1); 2061 2062 num = btop(size); 2063 if (num == 0) 2064 continue; 2065 2066 if (num > npages) 2067 num = npages; 2068 2069 npages -= num; 2070 pages_done += num; 2071 base_pfn = btop(addr); 2072 2073 if (prom_debug) 2074 prom_printf("MEMSEG addr=0x%" PRIx64 2075 " pgs=0x%lx pfn 0x%lx-0x%lx\n", 2076 addr, num, base_pfn, base_pfn + num); 2077 2078 /* 2079 * Ignore pages below ddiphysmin to simplify ddi memory 2080 * allocation with non-zero addr_lo requests. 2081 */ 2082 if (base_pfn < ddiphysmin) { 2083 if (base_pfn + num <= ddiphysmin) 2084 continue; 2085 pp += (ddiphysmin - base_pfn); 2086 num -= (ddiphysmin - base_pfn); 2087 base_pfn = ddiphysmin; 2088 } 2089 2090 /* 2091 * Build the memsegs entry 2092 */ 2093 cur_memseg->pages = pp; 2094 cur_memseg->epages = pp + num; 2095 cur_memseg->pages_base = base_pfn; 2096 cur_memseg->pages_end = base_pfn + num; 2097 2098 /* 2099 * Insert into memseg list in decreasing pfn range order. 2100 * Low memory is typically more fragmented such that this 2101 * ordering keeps the larger ranges at the front of the list 2102 * for code that searches memseg. 2103 * This ASSERTS that the memsegs coming in from boot are in 2104 * increasing physical address order and not contiguous. 2105 */ 2106 if (memsegs != NULL) { 2107 ASSERT(cur_memseg->pages_base >= memsegs->pages_end); 2108 cur_memseg->next = memsegs; 2109 } 2110 memsegs = cur_memseg; 2111 2112 /* 2113 * add_physmem() initializes the PSM part of the page 2114 * struct by calling the PSM back with add_physmem_cb(). 2115 * In addition it coalesces pages into larger pages as 2116 * it initializes them. 2117 */ 2118 add_physmem(pp, num, base_pfn); 2119 cur_memseg++; 2120 availrmem_initial += num; 2121 availrmem += num; 2122 2123 pp += num; 2124 } 2125 2126 PRM_DEBUG(availrmem_initial); 2127 PRM_DEBUG(availrmem); 2128 PRM_DEBUG(freemem); 2129 build_pfn_hash(); 2130 return (pages_done); 2131 } 2132 2133 /* 2134 * Kernel VM initialization. 2135 */ 2136 static void 2137 kvm_init(void) 2138 { 2139 ASSERT((((uintptr_t)s_text) & MMU_PAGEOFFSET) == 0); 2140 2141 /* 2142 * Put the kernel segments in kernel address space. 2143 */ 2144 rw_enter(&kas.a_lock, RW_WRITER); 2145 as_avlinit(&kas); 2146 2147 (void) seg_attach(&kas, s_text, e_moddata - s_text, &ktextseg); 2148 (void) segkmem_create(&ktextseg); 2149 2150 (void) seg_attach(&kas, (caddr_t)valloc_base, valloc_sz, &kvalloc); 2151 (void) segkmem_create(&kvalloc); 2152 2153 (void) seg_attach(&kas, kernelheap, 2154 ekernelheap - kernelheap, &kvseg); 2155 (void) segkmem_create(&kvseg); 2156 2157 if (core_size > 0) { 2158 PRM_POINT("attaching kvseg_core"); 2159 (void) seg_attach(&kas, (caddr_t)core_base, core_size, 2160 &kvseg_core); 2161 (void) segkmem_create(&kvseg_core); 2162 } 2163 2164 if (segziosize > 0) { 2165 PRM_POINT("attaching segzio"); 2166 (void) seg_attach(&kas, segzio_base, mmu_ptob(segziosize), 2167 &kzioseg); 2168 (void) segkmem_zio_create(&kzioseg); 2169 2170 /* create zio area covering new segment */ 2171 segkmem_zio_init(segzio_base, mmu_ptob(segziosize)); 2172 } 2173 2174 (void) seg_attach(&kas, kdi_segdebugbase, kdi_segdebugsize, &kdebugseg); 2175 (void) segkmem_create(&kdebugseg); 2176 2177 rw_exit(&kas.a_lock); 2178 2179 /* 2180 * Ensure that the red zone at kernelbase is never accessible. 2181 */ 2182 PRM_POINT("protecting redzone"); 2183 (void) as_setprot(&kas, (caddr_t)kernelbase, KERNEL_REDZONE_SIZE, 0); 2184 2185 /* 2186 * Make the text writable so that it can be hot patched by DTrace. 2187 */ 2188 (void) as_setprot(&kas, s_text, e_modtext - s_text, 2189 PROT_READ | PROT_WRITE | PROT_EXEC); 2190 2191 /* 2192 * Make data writable until end. 2193 */ 2194 (void) as_setprot(&kas, s_data, e_moddata - s_data, 2195 PROT_READ | PROT_WRITE | PROT_EXEC); 2196 } 2197 2198 /* 2199 * These are MTTR registers supported by P6 2200 */ 2201 static struct mtrrvar mtrrphys_arr[MAX_MTRRVAR]; 2202 static uint64_t mtrr64k, mtrr16k1, mtrr16k2; 2203 static uint64_t mtrr4k1, mtrr4k2, mtrr4k3; 2204 static uint64_t mtrr4k4, mtrr4k5, mtrr4k6; 2205 static uint64_t mtrr4k7, mtrr4k8, mtrrcap; 2206 uint64_t mtrrdef, pat_attr_reg; 2207 2208 /* 2209 * Disable reprogramming of MTRRs by default. 2210 */ 2211 int enable_relaxed_mtrr = 0; 2212 2213 void 2214 setup_mtrr(void) 2215 { 2216 int i, ecx; 2217 int vcnt; 2218 struct mtrrvar *mtrrphys; 2219 2220 if (!(x86_feature & X86_MTRR)) 2221 return; 2222 2223 mtrrcap = rdmsr(REG_MTRRCAP); 2224 mtrrdef = rdmsr(REG_MTRRDEF); 2225 if (mtrrcap & MTRRCAP_FIX) { 2226 mtrr64k = rdmsr(REG_MTRR64K); 2227 mtrr16k1 = rdmsr(REG_MTRR16K1); 2228 mtrr16k2 = rdmsr(REG_MTRR16K2); 2229 mtrr4k1 = rdmsr(REG_MTRR4K1); 2230 mtrr4k2 = rdmsr(REG_MTRR4K2); 2231 mtrr4k3 = rdmsr(REG_MTRR4K3); 2232 mtrr4k4 = rdmsr(REG_MTRR4K4); 2233 mtrr4k5 = rdmsr(REG_MTRR4K5); 2234 mtrr4k6 = rdmsr(REG_MTRR4K6); 2235 mtrr4k7 = rdmsr(REG_MTRR4K7); 2236 mtrr4k8 = rdmsr(REG_MTRR4K8); 2237 } 2238 if ((vcnt = (mtrrcap & MTRRCAP_VCNTMASK)) > MAX_MTRRVAR) 2239 vcnt = MAX_MTRRVAR; 2240 2241 for (i = 0, ecx = REG_MTRRPHYSBASE0, mtrrphys = mtrrphys_arr; 2242 i < vcnt - 1; i++, ecx += 2, mtrrphys++) { 2243 mtrrphys->mtrrphys_base = rdmsr(ecx); 2244 mtrrphys->mtrrphys_mask = rdmsr(ecx + 1); 2245 if ((x86_feature & X86_PAT) && enable_relaxed_mtrr) 2246 mtrrphys->mtrrphys_mask &= ~MTRRPHYSMASK_V; 2247 } 2248 if (x86_feature & X86_PAT) { 2249 if (enable_relaxed_mtrr) 2250 mtrrdef = MTRR_TYPE_WB|MTRRDEF_FE|MTRRDEF_E; 2251 pat_attr_reg = PAT_DEFAULT_ATTRIBUTE; 2252 } 2253 2254 mtrr_sync(); 2255 } 2256 2257 /* 2258 * Sync current cpu mtrr with the incore copy of mtrr. 2259 * This function has to be invoked with interrupts disabled 2260 * Currently we do not capture other cpu's. This is invoked on cpu0 2261 * just after reading /etc/system. 2262 * On other cpu's its invoked from mp_startup(). 2263 */ 2264 void 2265 mtrr_sync(void) 2266 { 2267 uint_t crvalue, cr0_orig; 2268 int vcnt, i, ecx; 2269 struct mtrrvar *mtrrphys; 2270 2271 cr0_orig = crvalue = getcr0(); 2272 crvalue |= CR0_CD; 2273 crvalue &= ~CR0_NW; 2274 setcr0(crvalue); 2275 invalidate_cache(); 2276 2277 reload_cr3(); 2278 if (x86_feature & X86_PAT) 2279 wrmsr(REG_MTRRPAT, pat_attr_reg); 2280 2281 wrmsr(REG_MTRRDEF, rdmsr(REG_MTRRDEF) & 2282 ~((uint64_t)(uintptr_t)MTRRDEF_E)); 2283 2284 if (mtrrcap & MTRRCAP_FIX) { 2285 wrmsr(REG_MTRR64K, mtrr64k); 2286 wrmsr(REG_MTRR16K1, mtrr16k1); 2287 wrmsr(REG_MTRR16K2, mtrr16k2); 2288 wrmsr(REG_MTRR4K1, mtrr4k1); 2289 wrmsr(REG_MTRR4K2, mtrr4k2); 2290 wrmsr(REG_MTRR4K3, mtrr4k3); 2291 wrmsr(REG_MTRR4K4, mtrr4k4); 2292 wrmsr(REG_MTRR4K5, mtrr4k5); 2293 wrmsr(REG_MTRR4K6, mtrr4k6); 2294 wrmsr(REG_MTRR4K7, mtrr4k7); 2295 wrmsr(REG_MTRR4K8, mtrr4k8); 2296 } 2297 if ((vcnt = (mtrrcap & MTRRCAP_VCNTMASK)) > MAX_MTRRVAR) 2298 vcnt = MAX_MTRRVAR; 2299 for (i = 0, ecx = REG_MTRRPHYSBASE0, mtrrphys = mtrrphys_arr; 2300 i < vcnt - 1; i++, ecx += 2, mtrrphys++) { 2301 wrmsr(ecx, mtrrphys->mtrrphys_base); 2302 wrmsr(ecx + 1, mtrrphys->mtrrphys_mask); 2303 } 2304 wrmsr(REG_MTRRDEF, mtrrdef); 2305 2306 reload_cr3(); 2307 invalidate_cache(); 2308 setcr0(cr0_orig); 2309 } 2310 2311 /* 2312 * resync mtrr so that BIOS is happy. Called from mdboot 2313 */ 2314 void 2315 mtrr_resync(void) 2316 { 2317 if ((x86_feature & X86_PAT) && enable_relaxed_mtrr) { 2318 /* 2319 * We could have changed the default mtrr definition. 2320 * Put it back to uncached which is what it is at power on 2321 */ 2322 mtrrdef = MTRR_TYPE_UC|MTRRDEF_FE|MTRRDEF_E; 2323 mtrr_sync(); 2324 } 2325 } 2326 2327 void 2328 get_system_configuration(void) 2329 { 2330 char prop[32]; 2331 u_longlong_t nodes_ll, cpus_pernode_ll, lvalue; 2332 2333 if (BOP_GETPROPLEN(bootops, "nodes") > sizeof (prop) || 2334 BOP_GETPROP(bootops, "nodes", prop) < 0 || 2335 kobj_getvalue(prop, &nodes_ll) == -1 || 2336 nodes_ll > MAXNODES || 2337 BOP_GETPROPLEN(bootops, "cpus_pernode") > sizeof (prop) || 2338 BOP_GETPROP(bootops, "cpus_pernode", prop) < 0 || 2339 kobj_getvalue(prop, &cpus_pernode_ll) == -1) { 2340 system_hardware.hd_nodes = 1; 2341 system_hardware.hd_cpus_per_node = 0; 2342 } else { 2343 system_hardware.hd_nodes = (int)nodes_ll; 2344 system_hardware.hd_cpus_per_node = (int)cpus_pernode_ll; 2345 } 2346 2347 if (BOP_GETPROPLEN(bootops, "kernelbase") > sizeof (prop) || 2348 BOP_GETPROP(bootops, "kernelbase", prop) < 0 || 2349 kobj_getvalue(prop, &lvalue) == -1) 2350 eprom_kernelbase = NULL; 2351 else 2352 eprom_kernelbase = (uintptr_t)lvalue; 2353 2354 if (BOP_GETPROPLEN(bootops, "segmapsize") > sizeof (prop) || 2355 BOP_GETPROP(bootops, "segmapsize", prop) < 0 || 2356 kobj_getvalue(prop, &lvalue) == -1) 2357 segmapsize = SEGMAPDEFAULT; 2358 else 2359 segmapsize = (uintptr_t)lvalue; 2360 2361 if (BOP_GETPROPLEN(bootops, "segmapfreelists") > sizeof (prop) || 2362 BOP_GETPROP(bootops, "segmapfreelists", prop) < 0 || 2363 kobj_getvalue(prop, &lvalue) == -1) 2364 segmapfreelists = 0; /* use segmap driver default */ 2365 else 2366 segmapfreelists = (int)lvalue; 2367 2368 /* physmem used to be here, but moved much earlier to fakebop.c */ 2369 } 2370 2371 /* 2372 * Add to a memory list. 2373 * start = start of new memory segment 2374 * len = length of new memory segment in bytes 2375 * new = pointer to a new struct memlist 2376 * memlistp = memory list to which to add segment. 2377 */ 2378 void 2379 memlist_add( 2380 uint64_t start, 2381 uint64_t len, 2382 struct memlist *new, 2383 struct memlist **memlistp) 2384 { 2385 struct memlist *cur; 2386 uint64_t end = start + len; 2387 2388 new->address = start; 2389 new->size = len; 2390 2391 cur = *memlistp; 2392 2393 while (cur) { 2394 if (cur->address >= end) { 2395 new->next = cur; 2396 *memlistp = new; 2397 new->prev = cur->prev; 2398 cur->prev = new; 2399 return; 2400 } 2401 ASSERT(cur->address + cur->size <= start); 2402 if (cur->next == NULL) { 2403 cur->next = new; 2404 new->prev = cur; 2405 new->next = NULL; 2406 return; 2407 } 2408 memlistp = &cur->next; 2409 cur = cur->next; 2410 } 2411 } 2412 2413 void 2414 kobj_vmem_init(vmem_t **text_arena, vmem_t **data_arena) 2415 { 2416 size_t tsize = e_modtext - modtext; 2417 size_t dsize = e_moddata - moddata; 2418 2419 *text_arena = vmem_create("module_text", tsize ? modtext : NULL, tsize, 2420 1, segkmem_alloc, segkmem_free, heaptext_arena, 0, VM_SLEEP); 2421 *data_arena = vmem_create("module_data", dsize ? moddata : NULL, dsize, 2422 1, segkmem_alloc, segkmem_free, heap32_arena, 0, VM_SLEEP); 2423 } 2424 2425 caddr_t 2426 kobj_text_alloc(vmem_t *arena, size_t size) 2427 { 2428 return (vmem_alloc(arena, size, VM_SLEEP | VM_BESTFIT)); 2429 } 2430 2431 /*ARGSUSED*/ 2432 caddr_t 2433 kobj_texthole_alloc(caddr_t addr, size_t size) 2434 { 2435 panic("unexpected call to kobj_texthole_alloc()"); 2436 /*NOTREACHED*/ 2437 return (0); 2438 } 2439 2440 /*ARGSUSED*/ 2441 void 2442 kobj_texthole_free(caddr_t addr, size_t size) 2443 { 2444 panic("unexpected call to kobj_texthole_free()"); 2445 } 2446 2447 /* 2448 * This is called just after configure() in startup(). 2449 * 2450 * The ISALIST concept is a bit hopeless on Intel, because 2451 * there's no guarantee of an ever-more-capable processor 2452 * given that various parts of the instruction set may appear 2453 * and disappear between different implementations. 2454 * 2455 * While it would be possible to correct it and even enhance 2456 * it somewhat, the explicit hardware capability bitmask allows 2457 * more flexibility. 2458 * 2459 * So, we just leave this alone. 2460 */ 2461 void 2462 setx86isalist(void) 2463 { 2464 char *tp; 2465 size_t len; 2466 extern char *isa_list; 2467 2468 #define TBUFSIZE 1024 2469 2470 tp = kmem_alloc(TBUFSIZE, KM_SLEEP); 2471 *tp = '\0'; 2472 2473 #if defined(__amd64) 2474 (void) strcpy(tp, "amd64 "); 2475 #endif 2476 2477 switch (x86_vendor) { 2478 case X86_VENDOR_Intel: 2479 case X86_VENDOR_AMD: 2480 case X86_VENDOR_TM: 2481 if (x86_feature & X86_CMOV) { 2482 /* 2483 * Pentium Pro or later 2484 */ 2485 (void) strcat(tp, "pentium_pro"); 2486 (void) strcat(tp, x86_feature & X86_MMX ? 2487 "+mmx pentium_pro " : " "); 2488 } 2489 /*FALLTHROUGH*/ 2490 case X86_VENDOR_Cyrix: 2491 /* 2492 * The Cyrix 6x86 does not have any Pentium features 2493 * accessible while not at privilege level 0. 2494 */ 2495 if (x86_feature & X86_CPUID) { 2496 (void) strcat(tp, "pentium"); 2497 (void) strcat(tp, x86_feature & X86_MMX ? 2498 "+mmx pentium " : " "); 2499 } 2500 break; 2501 default: 2502 break; 2503 } 2504 (void) strcat(tp, "i486 i386 i86"); 2505 len = strlen(tp) + 1; /* account for NULL at end of string */ 2506 isa_list = strcpy(kmem_alloc(len, KM_SLEEP), tp); 2507 kmem_free(tp, TBUFSIZE); 2508 2509 #undef TBUFSIZE 2510 } 2511 2512 2513 #ifdef __amd64 2514 2515 void * 2516 device_arena_alloc(size_t size, int vm_flag) 2517 { 2518 return (vmem_alloc(device_arena, size, vm_flag)); 2519 } 2520 2521 void 2522 device_arena_free(void *vaddr, size_t size) 2523 { 2524 vmem_free(device_arena, vaddr, size); 2525 } 2526 2527 #else /* __i386 */ 2528 2529 void * 2530 device_arena_alloc(size_t size, int vm_flag) 2531 { 2532 caddr_t vaddr; 2533 uintptr_t v; 2534 size_t start; 2535 size_t end; 2536 2537 vaddr = vmem_alloc(heap_arena, size, vm_flag); 2538 if (vaddr == NULL) 2539 return (NULL); 2540 2541 v = (uintptr_t)vaddr; 2542 ASSERT(v >= kernelbase); 2543 ASSERT(v + size <= valloc_base); 2544 2545 start = btop(v - kernelbase); 2546 end = btop(v + size - 1 - kernelbase); 2547 ASSERT(start < toxic_bit_map_len); 2548 ASSERT(end < toxic_bit_map_len); 2549 2550 while (start <= end) { 2551 BT_ATOMIC_SET(toxic_bit_map, start); 2552 ++start; 2553 } 2554 return (vaddr); 2555 } 2556 2557 void 2558 device_arena_free(void *vaddr, size_t size) 2559 { 2560 uintptr_t v = (uintptr_t)vaddr; 2561 size_t start; 2562 size_t end; 2563 2564 ASSERT(v >= kernelbase); 2565 ASSERT(v + size <= valloc_base); 2566 2567 start = btop(v - kernelbase); 2568 end = btop(v + size - 1 - kernelbase); 2569 ASSERT(start < toxic_bit_map_len); 2570 ASSERT(end < toxic_bit_map_len); 2571 2572 while (start <= end) { 2573 ASSERT(BT_TEST(toxic_bit_map, start) != 0); 2574 BT_ATOMIC_CLEAR(toxic_bit_map, start); 2575 ++start; 2576 } 2577 vmem_free(heap_arena, vaddr, size); 2578 } 2579 2580 /* 2581 * returns 1st address in range that is in device arena, or NULL 2582 * if len is not NULL it returns the length of the toxic range 2583 */ 2584 void * 2585 device_arena_contains(void *vaddr, size_t size, size_t *len) 2586 { 2587 uintptr_t v = (uintptr_t)vaddr; 2588 uintptr_t eaddr = v + size; 2589 size_t start; 2590 size_t end; 2591 2592 /* 2593 * if called very early by kmdb, just return NULL 2594 */ 2595 if (toxic_bit_map == NULL) 2596 return (NULL); 2597 2598 /* 2599 * First check if we're completely outside the bitmap range. 2600 */ 2601 if (v >= valloc_base || eaddr < kernelbase) 2602 return (NULL); 2603 2604 /* 2605 * Trim ends of search to look at only what the bitmap covers. 2606 */ 2607 if (v < kernelbase) 2608 v = kernelbase; 2609 start = btop(v - kernelbase); 2610 end = btop(eaddr - kernelbase); 2611 if (end >= toxic_bit_map_len) 2612 end = toxic_bit_map_len; 2613 2614 if (bt_range(toxic_bit_map, &start, &end, end) == 0) 2615 return (NULL); 2616 2617 v = kernelbase + ptob(start); 2618 if (len != NULL) 2619 *len = ptob(end - start); 2620 return ((void *)v); 2621 } 2622 2623 #endif /* __i386 */ 2624