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