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 /* 23 * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 24 * Use is subject to license terms. 25 * 26 * Copyright 2013 Joyent, Inc. All rights reserved. 27 */ 28 29 30 #include <sys/types.h> 31 #include <sys/machparam.h> 32 #include <sys/x86_archext.h> 33 #include <sys/systm.h> 34 #include <sys/mach_mmu.h> 35 #include <sys/multiboot.h> 36 #include <sys/multiboot2.h> 37 #include <sys/multiboot2_impl.h> 38 #include <sys/sysmacros.h> 39 #include <sys/framebuffer.h> 40 #include <sys/sha1.h> 41 #include <util/string.h> 42 #include <util/strtolctype.h> 43 #include <sys/efi.h> 44 45 /* 46 * Compile time debug knob. We do not have any early mechanism to control it 47 * as the boot is the earliest mechanism we have, and we do not want to have 48 * it being switched on by default. 49 */ 50 int dboot_debug = 0; 51 52 #if defined(__xpv) 53 54 #include <sys/hypervisor.h> 55 uintptr_t xen_virt_start; 56 pfn_t *mfn_to_pfn_mapping; 57 58 #else /* !__xpv */ 59 60 extern multiboot_header_t mb_header; 61 extern uint32_t mb2_load_addr; 62 extern int have_cpuid(void); 63 64 #endif /* !__xpv */ 65 66 #include <sys/inttypes.h> 67 #include <sys/bootinfo.h> 68 #include <sys/mach_mmu.h> 69 #include <sys/boot_console.h> 70 71 #include "dboot_asm.h" 72 #include "dboot_printf.h" 73 #include "dboot_xboot.h" 74 #include "dboot_elfload.h" 75 76 #define SHA1_ASCII_LENGTH (SHA1_DIGEST_LENGTH * 2) 77 78 /* 79 * This file contains code that runs to transition us from either a multiboot 80 * compliant loader (32 bit non-paging) or a XPV domain loader to 81 * regular kernel execution. Its task is to setup the kernel memory image 82 * and page tables. 83 * 84 * The code executes as: 85 * - 32 bits under GRUB (for 32 or 64 bit Solaris) 86 * - a 32 bit program for the 32-bit PV hypervisor 87 * - a 64 bit program for the 64-bit PV hypervisor (at least for now) 88 * 89 * Under the PV hypervisor, we must create mappings for any memory beyond the 90 * initial start of day allocation (such as the kernel itself). 91 * 92 * When on the metal, the mapping between maddr_t and paddr_t is 1:1. 93 * Since we are running in real mode, so all such memory is accessible. 94 */ 95 96 /* 97 * Standard bits used in PTE (page level) and PTP (internal levels) 98 */ 99 x86pte_t ptp_bits = PT_VALID | PT_REF | PT_WRITABLE | PT_USER; 100 x86pte_t pte_bits = PT_VALID | PT_REF | PT_WRITABLE | PT_MOD | PT_NOCONSIST; 101 102 /* 103 * This is the target addresses (physical) where the kernel text and data 104 * nucleus pages will be unpacked. On the hypervisor this is actually a 105 * virtual address. 106 */ 107 paddr_t ktext_phys; 108 uint32_t ksize = 2 * FOUR_MEG; /* kernel nucleus is 8Meg */ 109 110 static uint64_t target_kernel_text; /* value to use for KERNEL_TEXT */ 111 112 /* 113 * The stack is setup in assembler before entering startup_kernel() 114 */ 115 char stack_space[STACK_SIZE]; 116 117 /* 118 * Used to track physical memory allocation 119 */ 120 static paddr_t next_avail_addr = 0; 121 122 #if defined(__xpv) 123 /* 124 * Additional information needed for hypervisor memory allocation. 125 * Only memory up to scratch_end is mapped by page tables. 126 * mfn_base is the start of the hypervisor virtual image. It's ONE_GIG, so 127 * to derive a pfn from a pointer, you subtract mfn_base. 128 */ 129 130 static paddr_t scratch_end = 0; /* we can't write all of mem here */ 131 static paddr_t mfn_base; /* addr corresponding to mfn_list[0] */ 132 start_info_t *xen_info; 133 134 #else /* __xpv */ 135 136 /* 137 * If on the metal, then we have a multiboot loader. 138 */ 139 uint32_t mb_magic; /* magic from boot loader */ 140 uint32_t mb_addr; /* multiboot info package from loader */ 141 int multiboot_version; 142 multiboot_info_t *mb_info; 143 multiboot2_info_header_t *mb2_info; 144 multiboot_tag_mmap_t *mb2_mmap_tagp; 145 int num_entries; /* mmap entry count */ 146 boolean_t num_entries_set; /* is mmap entry count set */ 147 uintptr_t load_addr; 148 static boot_framebuffer_t framebuffer __aligned(16); 149 static boot_framebuffer_t *fb; 150 151 /* can not be automatic variables because of alignment */ 152 static efi_guid_t smbios3 = SMBIOS3_TABLE_GUID; 153 static efi_guid_t smbios = SMBIOS_TABLE_GUID; 154 static efi_guid_t acpi2 = EFI_ACPI_TABLE_GUID; 155 static efi_guid_t acpi1 = ACPI_10_TABLE_GUID; 156 #endif /* __xpv */ 157 158 /* 159 * This contains information passed to the kernel 160 */ 161 struct xboot_info boot_info __aligned(16); 162 struct xboot_info *bi; 163 164 /* 165 * Page table and memory stuff. 166 */ 167 static paddr_t max_mem; /* maximum memory address */ 168 169 /* 170 * Information about processor MMU 171 */ 172 int amd64_support = 0; 173 int largepage_support = 0; 174 int pae_support = 0; 175 int pge_support = 0; 176 int NX_support = 0; 177 int PAT_support = 0; 178 179 /* 180 * Low 32 bits of kernel entry address passed back to assembler. 181 * When running a 64 bit kernel, the high 32 bits are 0xffffffff. 182 */ 183 uint32_t entry_addr_low; 184 185 /* 186 * Memlists for the kernel. We shouldn't need a lot of these. 187 */ 188 #define MAX_MEMLIST (50) 189 struct boot_memlist memlists[MAX_MEMLIST]; 190 uint_t memlists_used = 0; 191 struct boot_memlist pcimemlists[MAX_MEMLIST]; 192 uint_t pcimemlists_used = 0; 193 struct boot_memlist rsvdmemlists[MAX_MEMLIST]; 194 uint_t rsvdmemlists_used = 0; 195 196 /* 197 * This should match what's in the bootloader. It's arbitrary, but GRUB 198 * in particular has limitations on how much space it can use before it 199 * stops working properly. This should be enough. 200 */ 201 struct boot_modules modules[MAX_BOOT_MODULES]; 202 uint_t modules_used = 0; 203 204 #ifdef __xpv 205 /* 206 * Xen strips the size field out of the mb_memory_map_t, see struct e820entry 207 * definition in Xen source. 208 */ 209 typedef struct { 210 uint32_t base_addr_low; 211 uint32_t base_addr_high; 212 uint32_t length_low; 213 uint32_t length_high; 214 uint32_t type; 215 } mmap_t; 216 217 /* 218 * There is 512KB of scratch area after the boot stack page. 219 * We'll use that for everything except the kernel nucleus pages which are too 220 * big to fit there and are allocated last anyway. 221 */ 222 #define MAXMAPS 100 223 static mmap_t map_buffer[MAXMAPS]; 224 #else 225 typedef mb_memory_map_t mmap_t; 226 #endif 227 228 /* 229 * Debugging macros 230 */ 231 uint_t prom_debug = 0; 232 uint_t map_debug = 0; 233 234 static char noname[2] = "-"; 235 236 /* 237 * Either hypervisor-specific or grub-specific code builds the initial 238 * memlists. This code does the sort/merge/link for final use. 239 */ 240 static void 241 sort_physinstall(void) 242 { 243 int i; 244 #if !defined(__xpv) 245 int j; 246 struct boot_memlist tmp; 247 248 /* 249 * Now sort the memlists, in case they weren't in order. 250 * Yeah, this is a bubble sort; small, simple and easy to get right. 251 */ 252 DBG_MSG("Sorting phys-installed list\n"); 253 for (j = memlists_used - 1; j > 0; --j) { 254 for (i = 0; i < j; ++i) { 255 if (memlists[i].addr < memlists[i + 1].addr) 256 continue; 257 tmp = memlists[i]; 258 memlists[i] = memlists[i + 1]; 259 memlists[i + 1] = tmp; 260 } 261 } 262 263 /* 264 * Merge any memlists that don't have holes between them. 265 */ 266 for (i = 0; i <= memlists_used - 1; ++i) { 267 if (memlists[i].addr + memlists[i].size != memlists[i + 1].addr) 268 continue; 269 270 if (prom_debug) 271 dboot_printf( 272 "merging mem segs %" PRIx64 "...%" PRIx64 273 " w/ %" PRIx64 "...%" PRIx64 "\n", 274 memlists[i].addr, 275 memlists[i].addr + memlists[i].size, 276 memlists[i + 1].addr, 277 memlists[i + 1].addr + memlists[i + 1].size); 278 279 memlists[i].size += memlists[i + 1].size; 280 for (j = i + 1; j < memlists_used - 1; ++j) 281 memlists[j] = memlists[j + 1]; 282 --memlists_used; 283 DBG(memlists_used); 284 --i; /* after merging we need to reexamine, so do this */ 285 } 286 #endif /* __xpv */ 287 288 if (prom_debug) { 289 dboot_printf("\nFinal memlists:\n"); 290 for (i = 0; i < memlists_used; ++i) { 291 dboot_printf("\t%d: addr=%" PRIx64 " size=%" 292 PRIx64 "\n", i, memlists[i].addr, memlists[i].size); 293 } 294 } 295 296 /* 297 * link together the memlists with native size pointers 298 */ 299 memlists[0].next = 0; 300 memlists[0].prev = 0; 301 for (i = 1; i < memlists_used; ++i) { 302 memlists[i].prev = (native_ptr_t)(uintptr_t)(memlists + i - 1); 303 memlists[i].next = 0; 304 memlists[i - 1].next = (native_ptr_t)(uintptr_t)(memlists + i); 305 } 306 bi->bi_phys_install = (native_ptr_t)(uintptr_t)memlists; 307 DBG(bi->bi_phys_install); 308 } 309 310 /* 311 * build bios reserved memlists 312 */ 313 static void 314 build_rsvdmemlists(void) 315 { 316 int i; 317 318 rsvdmemlists[0].next = 0; 319 rsvdmemlists[0].prev = 0; 320 for (i = 1; i < rsvdmemlists_used; ++i) { 321 rsvdmemlists[i].prev = 322 (native_ptr_t)(uintptr_t)(rsvdmemlists + i - 1); 323 rsvdmemlists[i].next = 0; 324 rsvdmemlists[i - 1].next = 325 (native_ptr_t)(uintptr_t)(rsvdmemlists + i); 326 } 327 bi->bi_rsvdmem = (native_ptr_t)(uintptr_t)rsvdmemlists; 328 DBG(bi->bi_rsvdmem); 329 } 330 331 #if defined(__xpv) 332 333 /* 334 * halt on the hypervisor after a delay to drain console output 335 */ 336 void 337 dboot_halt(void) 338 { 339 uint_t i = 10000; 340 341 while (--i) 342 (void) HYPERVISOR_yield(); 343 (void) HYPERVISOR_shutdown(SHUTDOWN_poweroff); 344 } 345 346 /* 347 * From a machine address, find the corresponding pseudo-physical address. 348 * Pseudo-physical address are contiguous and run from mfn_base in each VM. 349 * Machine addresses are the real underlying hardware addresses. 350 * These are needed for page table entries. Note that this routine is 351 * poorly protected. A bad value of "ma" will cause a page fault. 352 */ 353 paddr_t 354 ma_to_pa(maddr_t ma) 355 { 356 ulong_t pgoff = ma & MMU_PAGEOFFSET; 357 ulong_t pfn = mfn_to_pfn_mapping[mmu_btop(ma)]; 358 paddr_t pa; 359 360 if (pfn >= xen_info->nr_pages) 361 return (-(paddr_t)1); 362 pa = mfn_base + mmu_ptob((paddr_t)pfn) + pgoff; 363 #ifdef DEBUG 364 if (ma != pa_to_ma(pa)) 365 dboot_printf("ma_to_pa(%" PRIx64 ") got %" PRIx64 ", " 366 "pa_to_ma() says %" PRIx64 "\n", ma, pa, pa_to_ma(pa)); 367 #endif 368 return (pa); 369 } 370 371 /* 372 * From a pseudo-physical address, find the corresponding machine address. 373 */ 374 maddr_t 375 pa_to_ma(paddr_t pa) 376 { 377 pfn_t pfn; 378 ulong_t mfn; 379 380 pfn = mmu_btop(pa - mfn_base); 381 if (pa < mfn_base || pfn >= xen_info->nr_pages) 382 dboot_panic("pa_to_ma(): illegal address 0x%lx", (ulong_t)pa); 383 mfn = ((ulong_t *)xen_info->mfn_list)[pfn]; 384 #ifdef DEBUG 385 if (mfn_to_pfn_mapping[mfn] != pfn) 386 dboot_printf("pa_to_ma(pfn=%lx) got %lx ma_to_pa() says %lx\n", 387 pfn, mfn, mfn_to_pfn_mapping[mfn]); 388 #endif 389 return (mfn_to_ma(mfn) | (pa & MMU_PAGEOFFSET)); 390 } 391 392 #endif /* __xpv */ 393 394 x86pte_t 395 get_pteval(paddr_t table, uint_t index) 396 { 397 if (pae_support) 398 return (((x86pte_t *)(uintptr_t)table)[index]); 399 return (((x86pte32_t *)(uintptr_t)table)[index]); 400 } 401 402 /*ARGSUSED*/ 403 void 404 set_pteval(paddr_t table, uint_t index, uint_t level, x86pte_t pteval) 405 { 406 #ifdef __xpv 407 mmu_update_t t; 408 maddr_t mtable = pa_to_ma(table); 409 int retcnt; 410 411 t.ptr = (mtable + index * pte_size) | MMU_NORMAL_PT_UPDATE; 412 t.val = pteval; 413 if (HYPERVISOR_mmu_update(&t, 1, &retcnt, DOMID_SELF) || retcnt != 1) 414 dboot_panic("HYPERVISOR_mmu_update() failed"); 415 #else /* __xpv */ 416 uintptr_t tab_addr = (uintptr_t)table; 417 418 if (pae_support) 419 ((x86pte_t *)tab_addr)[index] = pteval; 420 else 421 ((x86pte32_t *)tab_addr)[index] = (x86pte32_t)pteval; 422 if (level == top_level && level == 2) 423 reload_cr3(); 424 #endif /* __xpv */ 425 } 426 427 paddr_t 428 make_ptable(x86pte_t *pteval, uint_t level) 429 { 430 paddr_t new_table = (paddr_t)(uintptr_t)mem_alloc(MMU_PAGESIZE); 431 432 if (level == top_level && level == 2) 433 *pteval = pa_to_ma((uintptr_t)new_table) | PT_VALID; 434 else 435 *pteval = pa_to_ma((uintptr_t)new_table) | ptp_bits; 436 437 #ifdef __xpv 438 /* Remove write permission to the new page table. */ 439 if (HYPERVISOR_update_va_mapping(new_table, 440 *pteval & ~(x86pte_t)PT_WRITABLE, UVMF_INVLPG | UVMF_LOCAL)) 441 dboot_panic("HYP_update_va_mapping error"); 442 #endif 443 444 if (map_debug) 445 dboot_printf("new page table lvl=%d paddr=0x%lx ptp=0x%" 446 PRIx64 "\n", level, (ulong_t)new_table, *pteval); 447 return (new_table); 448 } 449 450 x86pte_t * 451 map_pte(paddr_t table, uint_t index) 452 { 453 return ((x86pte_t *)(uintptr_t)(table + index * pte_size)); 454 } 455 456 /* 457 * dump out the contents of page tables... 458 */ 459 static void 460 dump_tables(void) 461 { 462 uint_t save_index[4]; /* for recursion */ 463 char *save_table[4]; /* for recursion */ 464 uint_t l; 465 uint64_t va; 466 uint64_t pgsize; 467 int index; 468 int i; 469 x86pte_t pteval; 470 char *table; 471 static char *tablist = "\t\t\t"; 472 char *tabs = tablist + 3 - top_level; 473 uint_t pa, pa1; 474 #if !defined(__xpv) 475 #define maddr_t paddr_t 476 #endif /* !__xpv */ 477 478 dboot_printf("Finished pagetables:\n"); 479 table = (char *)(uintptr_t)top_page_table; 480 l = top_level; 481 va = 0; 482 for (index = 0; index < ptes_per_table; ++index) { 483 pgsize = 1ull << shift_amt[l]; 484 if (pae_support) 485 pteval = ((x86pte_t *)table)[index]; 486 else 487 pteval = ((x86pte32_t *)table)[index]; 488 if (pteval == 0) 489 goto next_entry; 490 491 dboot_printf("%s %p[0x%x] = %" PRIx64 ", va=%" PRIx64, 492 tabs + l, (void *)table, index, (uint64_t)pteval, va); 493 pa = ma_to_pa(pteval & MMU_PAGEMASK); 494 dboot_printf(" physaddr=%x\n", pa); 495 496 /* 497 * Don't try to walk hypervisor private pagetables 498 */ 499 if ((l > 1 || (l == 1 && (pteval & PT_PAGESIZE) == 0))) { 500 save_table[l] = table; 501 save_index[l] = index; 502 --l; 503 index = -1; 504 table = (char *)(uintptr_t) 505 ma_to_pa(pteval & MMU_PAGEMASK); 506 goto recursion; 507 } 508 509 /* 510 * shorten dump for consecutive mappings 511 */ 512 for (i = 1; index + i < ptes_per_table; ++i) { 513 if (pae_support) 514 pteval = ((x86pte_t *)table)[index + i]; 515 else 516 pteval = ((x86pte32_t *)table)[index + i]; 517 if (pteval == 0) 518 break; 519 pa1 = ma_to_pa(pteval & MMU_PAGEMASK); 520 if (pa1 != pa + i * pgsize) 521 break; 522 } 523 if (i > 2) { 524 dboot_printf("%s...\n", tabs + l); 525 va += pgsize * (i - 2); 526 index += i - 2; 527 } 528 next_entry: 529 va += pgsize; 530 if (l == 3 && index == 256) /* VA hole */ 531 va = 0xffff800000000000ull; 532 recursion: 533 ; 534 } 535 if (l < top_level) { 536 ++l; 537 index = save_index[l]; 538 table = save_table[l]; 539 goto recursion; 540 } 541 } 542 543 /* 544 * Add a mapping for the machine page at the given virtual address. 545 */ 546 static void 547 map_ma_at_va(maddr_t ma, native_ptr_t va, uint_t level) 548 { 549 x86pte_t *ptep; 550 x86pte_t pteval; 551 552 pteval = ma | pte_bits; 553 if (level > 0) 554 pteval |= PT_PAGESIZE; 555 if (va >= target_kernel_text && pge_support) 556 pteval |= PT_GLOBAL; 557 558 if (map_debug && ma != va) 559 dboot_printf("mapping ma=0x%" PRIx64 " va=0x%" PRIx64 560 " pte=0x%" PRIx64 " l=%d\n", 561 (uint64_t)ma, (uint64_t)va, pteval, level); 562 563 #if defined(__xpv) 564 /* 565 * see if we can avoid find_pte() on the hypervisor 566 */ 567 if (HYPERVISOR_update_va_mapping(va, pteval, 568 UVMF_INVLPG | UVMF_LOCAL) == 0) 569 return; 570 #endif 571 572 /* 573 * Find the pte that will map this address. This creates any 574 * missing intermediate level page tables 575 */ 576 ptep = find_pte(va, NULL, level, 0); 577 578 /* 579 * When paravirtualized, we must use hypervisor calls to modify the 580 * PTE, since paging is active. On real hardware we just write to 581 * the pagetables which aren't in use yet. 582 */ 583 #if defined(__xpv) 584 ptep = ptep; /* shut lint up */ 585 if (HYPERVISOR_update_va_mapping(va, pteval, UVMF_INVLPG | UVMF_LOCAL)) 586 dboot_panic("mmu_update failed-map_pa_at_va va=0x%" PRIx64 587 " l=%d ma=0x%" PRIx64 ", pte=0x%" PRIx64 "", 588 (uint64_t)va, level, (uint64_t)ma, pteval); 589 #else 590 if (va < 1024 * 1024) 591 pteval |= PT_NOCACHE; /* for video RAM */ 592 if (pae_support) 593 *ptep = pteval; 594 else 595 *((x86pte32_t *)ptep) = (x86pte32_t)pteval; 596 #endif 597 } 598 599 /* 600 * Add a mapping for the physical page at the given virtual address. 601 */ 602 static void 603 map_pa_at_va(paddr_t pa, native_ptr_t va, uint_t level) 604 { 605 map_ma_at_va(pa_to_ma(pa), va, level); 606 } 607 608 /* 609 * This is called to remove start..end from the 610 * possible range of PCI addresses. 611 */ 612 const uint64_t pci_lo_limit = 0x00100000ul; 613 const uint64_t pci_hi_limit = 0xfff00000ul; 614 static void 615 exclude_from_pci(uint64_t start, uint64_t end) 616 { 617 int i; 618 int j; 619 struct boot_memlist *ml; 620 621 for (i = 0; i < pcimemlists_used; ++i) { 622 ml = &pcimemlists[i]; 623 624 /* delete the entire range? */ 625 if (start <= ml->addr && ml->addr + ml->size <= end) { 626 --pcimemlists_used; 627 for (j = i; j < pcimemlists_used; ++j) 628 pcimemlists[j] = pcimemlists[j + 1]; 629 --i; /* to revisit the new one at this index */ 630 } 631 632 /* split a range? */ 633 else if (ml->addr < start && end < ml->addr + ml->size) { 634 635 ++pcimemlists_used; 636 if (pcimemlists_used > MAX_MEMLIST) 637 dboot_panic("too many pcimemlists"); 638 639 for (j = pcimemlists_used - 1; j > i; --j) 640 pcimemlists[j] = pcimemlists[j - 1]; 641 ml->size = start - ml->addr; 642 643 ++ml; 644 ml->size = (ml->addr + ml->size) - end; 645 ml->addr = end; 646 ++i; /* skip on to next one */ 647 } 648 649 /* cut memory off the start? */ 650 else if (ml->addr < end && end < ml->addr + ml->size) { 651 ml->size -= end - ml->addr; 652 ml->addr = end; 653 } 654 655 /* cut memory off the end? */ 656 else if (ml->addr <= start && start < ml->addr + ml->size) { 657 ml->size = start - ml->addr; 658 } 659 } 660 } 661 662 /* 663 * During memory allocation, find the highest address not used yet. 664 */ 665 static void 666 check_higher(paddr_t a) 667 { 668 if (a < next_avail_addr) 669 return; 670 next_avail_addr = RNDUP(a + 1, MMU_PAGESIZE); 671 DBG(next_avail_addr); 672 } 673 674 static int 675 dboot_loader_mmap_entries(void) 676 { 677 #if !defined(__xpv) 678 if (num_entries_set == B_TRUE) 679 return (num_entries); 680 681 switch (multiboot_version) { 682 case 1: 683 DBG(mb_info->flags); 684 if (mb_info->flags & 0x40) { 685 mb_memory_map_t *mmap; 686 687 DBG(mb_info->mmap_addr); 688 DBG(mb_info->mmap_length); 689 check_higher(mb_info->mmap_addr + mb_info->mmap_length); 690 691 for (mmap = (mb_memory_map_t *)mb_info->mmap_addr; 692 (uint32_t)mmap < mb_info->mmap_addr + 693 mb_info->mmap_length; 694 mmap = (mb_memory_map_t *)((uint32_t)mmap + 695 mmap->size + sizeof (mmap->size))) 696 ++num_entries; 697 698 num_entries_set = B_TRUE; 699 } 700 break; 701 case 2: 702 num_entries_set = B_TRUE; 703 num_entries = dboot_multiboot2_mmap_nentries(mb2_info, 704 mb2_mmap_tagp); 705 break; 706 default: 707 dboot_panic("Unknown multiboot version: %d\n", 708 multiboot_version); 709 break; 710 } 711 return (num_entries); 712 #else 713 return (MAXMAPS); 714 #endif 715 } 716 717 static uint32_t 718 dboot_loader_mmap_get_type(int index) 719 { 720 #if !defined(__xpv) 721 mb_memory_map_t *mp, *mpend; 722 int i; 723 724 switch (multiboot_version) { 725 case 1: 726 mp = (mb_memory_map_t *)mb_info->mmap_addr; 727 mpend = (mb_memory_map_t *) 728 (mb_info->mmap_addr + mb_info->mmap_length); 729 730 for (i = 0; mp < mpend && i != index; i++) 731 mp = (mb_memory_map_t *)((uint32_t)mp + mp->size + 732 sizeof (mp->size)); 733 if (mp >= mpend) { 734 dboot_panic("dboot_loader_mmap_get_type(): index " 735 "out of bounds: %d\n", index); 736 } 737 return (mp->type); 738 739 case 2: 740 return (dboot_multiboot2_mmap_get_type(mb2_info, 741 mb2_mmap_tagp, index)); 742 743 default: 744 dboot_panic("Unknown multiboot version: %d\n", 745 multiboot_version); 746 break; 747 } 748 return (0); 749 #else 750 return (map_buffer[index].type); 751 #endif 752 } 753 754 static uint64_t 755 dboot_loader_mmap_get_base(int index) 756 { 757 #if !defined(__xpv) 758 mb_memory_map_t *mp, *mpend; 759 int i; 760 761 switch (multiboot_version) { 762 case 1: 763 mp = (mb_memory_map_t *)mb_info->mmap_addr; 764 mpend = (mb_memory_map_t *) 765 (mb_info->mmap_addr + mb_info->mmap_length); 766 767 for (i = 0; mp < mpend && i != index; i++) 768 mp = (mb_memory_map_t *)((uint32_t)mp + mp->size + 769 sizeof (mp->size)); 770 if (mp >= mpend) { 771 dboot_panic("dboot_loader_mmap_get_base(): index " 772 "out of bounds: %d\n", index); 773 } 774 return (((uint64_t)mp->base_addr_high << 32) + 775 (uint64_t)mp->base_addr_low); 776 777 case 2: 778 return (dboot_multiboot2_mmap_get_base(mb2_info, 779 mb2_mmap_tagp, index)); 780 781 default: 782 dboot_panic("Unknown multiboot version: %d\n", 783 multiboot_version); 784 break; 785 } 786 return (0); 787 #else 788 return (((uint64_t)map_buffer[index].base_addr_high << 32) + 789 (uint64_t)map_buffer[index].base_addr_low); 790 #endif 791 } 792 793 static uint64_t 794 dboot_loader_mmap_get_length(int index) 795 { 796 #if !defined(__xpv) 797 mb_memory_map_t *mp, *mpend; 798 int i; 799 800 switch (multiboot_version) { 801 case 1: 802 mp = (mb_memory_map_t *)mb_info->mmap_addr; 803 mpend = (mb_memory_map_t *) 804 (mb_info->mmap_addr + mb_info->mmap_length); 805 806 for (i = 0; mp < mpend && i != index; i++) 807 mp = (mb_memory_map_t *)((uint32_t)mp + mp->size + 808 sizeof (mp->size)); 809 if (mp >= mpend) { 810 dboot_panic("dboot_loader_mmap_get_length(): index " 811 "out of bounds: %d\n", index); 812 } 813 return (((uint64_t)mp->length_high << 32) + 814 (uint64_t)mp->length_low); 815 816 case 2: 817 return (dboot_multiboot2_mmap_get_length(mb2_info, 818 mb2_mmap_tagp, index)); 819 820 default: 821 dboot_panic("Unknown multiboot version: %d\n", 822 multiboot_version); 823 break; 824 } 825 return (0); 826 #else 827 return (((uint64_t)map_buffer[index].length_high << 32) + 828 (uint64_t)map_buffer[index].length_low); 829 #endif 830 } 831 832 static void 833 build_pcimemlists(void) 834 { 835 uint64_t page_offset = MMU_PAGEOFFSET; /* needs to be 64 bits */ 836 uint64_t start; 837 uint64_t end; 838 int i, num; 839 840 /* 841 * initialize 842 */ 843 pcimemlists[0].addr = pci_lo_limit; 844 pcimemlists[0].size = pci_hi_limit - pci_lo_limit; 845 pcimemlists_used = 1; 846 847 num = dboot_loader_mmap_entries(); 848 /* 849 * Fill in PCI memlists. 850 */ 851 for (i = 0; i < num; ++i) { 852 start = dboot_loader_mmap_get_base(i); 853 end = start + dboot_loader_mmap_get_length(i); 854 855 if (prom_debug) 856 dboot_printf("\ttype: %d %" PRIx64 "..%" 857 PRIx64 "\n", dboot_loader_mmap_get_type(i), 858 start, end); 859 860 /* 861 * page align start and end 862 */ 863 start = (start + page_offset) & ~page_offset; 864 end &= ~page_offset; 865 if (end <= start) 866 continue; 867 868 exclude_from_pci(start, end); 869 } 870 871 /* 872 * Finish off the pcimemlist 873 */ 874 if (prom_debug) { 875 for (i = 0; i < pcimemlists_used; ++i) { 876 dboot_printf("pcimemlist entry 0x%" PRIx64 "..0x%" 877 PRIx64 "\n", pcimemlists[i].addr, 878 pcimemlists[i].addr + pcimemlists[i].size); 879 } 880 } 881 pcimemlists[0].next = 0; 882 pcimemlists[0].prev = 0; 883 for (i = 1; i < pcimemlists_used; ++i) { 884 pcimemlists[i].prev = 885 (native_ptr_t)(uintptr_t)(pcimemlists + i - 1); 886 pcimemlists[i].next = 0; 887 pcimemlists[i - 1].next = 888 (native_ptr_t)(uintptr_t)(pcimemlists + i); 889 } 890 bi->bi_pcimem = (native_ptr_t)(uintptr_t)pcimemlists; 891 DBG(bi->bi_pcimem); 892 } 893 894 #if defined(__xpv) 895 /* 896 * Initialize memory allocator stuff from hypervisor-supplied start info. 897 */ 898 static void 899 init_mem_alloc(void) 900 { 901 int local; /* variables needed to find start region */ 902 paddr_t scratch_start; 903 xen_memory_map_t map; 904 905 DBG_MSG("Entered init_mem_alloc()\n"); 906 907 /* 908 * Free memory follows the stack. There's at least 512KB of scratch 909 * space, rounded up to at least 2Mb alignment. That should be enough 910 * for the page tables we'll need to build. The nucleus memory is 911 * allocated last and will be outside the addressible range. We'll 912 * switch to new page tables before we unpack the kernel 913 */ 914 scratch_start = RNDUP((paddr_t)(uintptr_t)&local, MMU_PAGESIZE); 915 DBG(scratch_start); 916 scratch_end = RNDUP((paddr_t)scratch_start + 512 * 1024, TWO_MEG); 917 DBG(scratch_end); 918 919 /* 920 * For paranoia, leave some space between hypervisor data and ours. 921 * Use 500 instead of 512. 922 */ 923 next_avail_addr = scratch_end - 500 * 1024; 924 DBG(next_avail_addr); 925 926 /* 927 * The domain builder gives us at most 1 module 928 */ 929 DBG(xen_info->mod_len); 930 if (xen_info->mod_len > 0) { 931 DBG(xen_info->mod_start); 932 modules[0].bm_addr = 933 (native_ptr_t)(uintptr_t)xen_info->mod_start; 934 modules[0].bm_size = xen_info->mod_len; 935 bi->bi_module_cnt = 1; 936 bi->bi_modules = (native_ptr_t)(uintptr_t)modules; 937 } else { 938 bi->bi_module_cnt = 0; 939 bi->bi_modules = (native_ptr_t)(uintptr_t)NULL; 940 } 941 DBG(bi->bi_module_cnt); 942 DBG(bi->bi_modules); 943 944 DBG(xen_info->mfn_list); 945 DBG(xen_info->nr_pages); 946 max_mem = (paddr_t)xen_info->nr_pages << MMU_PAGESHIFT; 947 DBG(max_mem); 948 949 /* 950 * Using pseudo-physical addresses, so only 1 memlist element 951 */ 952 memlists[0].addr = 0; 953 DBG(memlists[0].addr); 954 memlists[0].size = max_mem; 955 DBG(memlists[0].size); 956 memlists_used = 1; 957 DBG(memlists_used); 958 959 /* 960 * finish building physinstall list 961 */ 962 sort_physinstall(); 963 964 /* 965 * build bios reserved memlists 966 */ 967 build_rsvdmemlists(); 968 969 if (DOMAIN_IS_INITDOMAIN(xen_info)) { 970 /* 971 * build PCI Memory list 972 */ 973 map.nr_entries = MAXMAPS; 974 /*LINTED: constant in conditional context*/ 975 set_xen_guest_handle(map.buffer, map_buffer); 976 if (HYPERVISOR_memory_op(XENMEM_machine_memory_map, &map) != 0) 977 dboot_panic("getting XENMEM_machine_memory_map failed"); 978 build_pcimemlists(); 979 } 980 } 981 982 #else /* !__xpv */ 983 984 static void 985 dboot_multiboot1_xboot_consinfo(void) 986 { 987 fb->framebuffer = 0; 988 } 989 990 static void 991 dboot_multiboot2_xboot_consinfo(void) 992 { 993 multiboot_tag_framebuffer_t *fbtag; 994 fbtag = dboot_multiboot2_find_tag(mb2_info, 995 MULTIBOOT_TAG_TYPE_FRAMEBUFFER); 996 fb->framebuffer = (uint64_t)(uintptr_t)fbtag; 997 } 998 999 static int 1000 dboot_multiboot_modcount(void) 1001 { 1002 switch (multiboot_version) { 1003 case 1: 1004 return (mb_info->mods_count); 1005 1006 case 2: 1007 return (dboot_multiboot2_modcount(mb2_info)); 1008 1009 default: 1010 dboot_panic("Unknown multiboot version: %d\n", 1011 multiboot_version); 1012 break; 1013 } 1014 return (0); 1015 } 1016 1017 static uint32_t 1018 dboot_multiboot_modstart(int index) 1019 { 1020 switch (multiboot_version) { 1021 case 1: 1022 return (((mb_module_t *)mb_info->mods_addr)[index].mod_start); 1023 1024 case 2: 1025 return (dboot_multiboot2_modstart(mb2_info, index)); 1026 1027 default: 1028 dboot_panic("Unknown multiboot version: %d\n", 1029 multiboot_version); 1030 break; 1031 } 1032 return (0); 1033 } 1034 1035 static uint32_t 1036 dboot_multiboot_modend(int index) 1037 { 1038 switch (multiboot_version) { 1039 case 1: 1040 return (((mb_module_t *)mb_info->mods_addr)[index].mod_end); 1041 1042 case 2: 1043 return (dboot_multiboot2_modend(mb2_info, index)); 1044 1045 default: 1046 dboot_panic("Unknown multiboot version: %d\n", 1047 multiboot_version); 1048 break; 1049 } 1050 return (0); 1051 } 1052 1053 static char * 1054 dboot_multiboot_modcmdline(int index) 1055 { 1056 switch (multiboot_version) { 1057 case 1: 1058 return ((char *)((mb_module_t *) 1059 mb_info->mods_addr)[index].mod_name); 1060 1061 case 2: 1062 return (dboot_multiboot2_modcmdline(mb2_info, index)); 1063 1064 default: 1065 dboot_panic("Unknown multiboot version: %d\n", 1066 multiboot_version); 1067 break; 1068 } 1069 return (0); 1070 } 1071 1072 /* 1073 * Find the environment module for console setup. 1074 * Since we need the console to print early boot messages, the console is set up 1075 * before anything else and therefore we need to pick up the environment module 1076 * early too. 1077 * 1078 * Note, we just will search for and if found, will pass the env 1079 * module to console setup, the proper module list processing will happen later. 1080 */ 1081 static void 1082 dboot_find_env(void) 1083 { 1084 int i, modcount; 1085 uint32_t mod_start, mod_end; 1086 char *cmdline; 1087 1088 modcount = dboot_multiboot_modcount(); 1089 1090 for (i = 0; i < modcount; ++i) { 1091 cmdline = dboot_multiboot_modcmdline(i); 1092 if (cmdline == NULL) 1093 continue; 1094 1095 if (strstr(cmdline, "type=environment") == NULL) 1096 continue; 1097 1098 mod_start = dboot_multiboot_modstart(i); 1099 mod_end = dboot_multiboot_modend(i); 1100 modules[0].bm_addr = (native_ptr_t)(uintptr_t)mod_start; 1101 modules[0].bm_size = mod_end - mod_start; 1102 modules[0].bm_name = (native_ptr_t)(uintptr_t)NULL; 1103 modules[0].bm_hash = (native_ptr_t)(uintptr_t)NULL; 1104 modules[0].bm_type = BMT_ENV; 1105 bi->bi_modules = (native_ptr_t)(uintptr_t)modules; 1106 bi->bi_module_cnt = 1; 1107 return; 1108 } 1109 } 1110 1111 static boolean_t 1112 dboot_multiboot_basicmeminfo(uint32_t *lower, uint32_t *upper) 1113 { 1114 boolean_t rv = B_FALSE; 1115 1116 switch (multiboot_version) { 1117 case 1: 1118 if (mb_info->flags & 0x01) { 1119 *lower = mb_info->mem_lower; 1120 *upper = mb_info->mem_upper; 1121 rv = B_TRUE; 1122 } 1123 break; 1124 1125 case 2: 1126 return (dboot_multiboot2_basicmeminfo(mb2_info, lower, upper)); 1127 1128 default: 1129 dboot_panic("Unknown multiboot version: %d\n", 1130 multiboot_version); 1131 break; 1132 } 1133 return (rv); 1134 } 1135 1136 static uint8_t 1137 dboot_a2h(char v) 1138 { 1139 if (v >= 'a') 1140 return (v - 'a' + 0xa); 1141 else if (v >= 'A') 1142 return (v - 'A' + 0xa); 1143 else if (v >= '0') 1144 return (v - '0'); 1145 else 1146 dboot_panic("bad ASCII hex character %c\n", v); 1147 1148 return (0); 1149 } 1150 1151 static void 1152 digest_a2h(const char *ascii, uint8_t *digest) 1153 { 1154 unsigned int i; 1155 1156 for (i = 0; i < SHA1_DIGEST_LENGTH; i++) { 1157 digest[i] = dboot_a2h(ascii[i * 2]) << 4; 1158 digest[i] |= dboot_a2h(ascii[i * 2 + 1]); 1159 } 1160 } 1161 1162 /* 1163 * Generate a SHA-1 hash of the first len bytes of image, and compare it with 1164 * the ASCII-format hash found in the 40-byte buffer at ascii. If they 1165 * match, return 0, otherwise -1. This works only for images smaller than 1166 * 4 GB, which should not be a problem. 1167 */ 1168 static int 1169 check_image_hash(uint_t midx) 1170 { 1171 const char *ascii; 1172 const void *image; 1173 size_t len; 1174 SHA1_CTX ctx; 1175 uint8_t digest[SHA1_DIGEST_LENGTH]; 1176 uint8_t baseline[SHA1_DIGEST_LENGTH]; 1177 unsigned int i; 1178 1179 ascii = (const char *)(uintptr_t)modules[midx].bm_hash; 1180 image = (const void *)(uintptr_t)modules[midx].bm_addr; 1181 len = (size_t)modules[midx].bm_size; 1182 1183 digest_a2h(ascii, baseline); 1184 1185 SHA1Init(&ctx); 1186 SHA1Update(&ctx, image, len); 1187 SHA1Final(digest, &ctx); 1188 1189 for (i = 0; i < SHA1_DIGEST_LENGTH; i++) { 1190 if (digest[i] != baseline[i]) 1191 return (-1); 1192 } 1193 1194 return (0); 1195 } 1196 1197 static const char * 1198 type_to_str(boot_module_type_t type) 1199 { 1200 switch (type) { 1201 case BMT_ROOTFS: 1202 return ("rootfs"); 1203 case BMT_FILE: 1204 return ("file"); 1205 case BMT_HASH: 1206 return ("hash"); 1207 case BMT_ENV: 1208 return ("environment"); 1209 default: 1210 return ("unknown"); 1211 } 1212 } 1213 1214 static void 1215 check_images(void) 1216 { 1217 uint_t i; 1218 char displayhash[SHA1_ASCII_LENGTH + 1]; 1219 1220 for (i = 0; i < modules_used; i++) { 1221 if (prom_debug) { 1222 dboot_printf("module #%d: name %s type %s " 1223 "addr %lx size %lx\n", 1224 i, (char *)(uintptr_t)modules[i].bm_name, 1225 type_to_str(modules[i].bm_type), 1226 (ulong_t)modules[i].bm_addr, 1227 (ulong_t)modules[i].bm_size); 1228 } 1229 1230 if (modules[i].bm_type == BMT_HASH || 1231 modules[i].bm_hash == (native_ptr_t)(uintptr_t)NULL) { 1232 DBG_MSG("module has no hash; skipping check\n"); 1233 continue; 1234 } 1235 (void) memcpy(displayhash, 1236 (void *)(uintptr_t)modules[i].bm_hash, 1237 SHA1_ASCII_LENGTH); 1238 displayhash[SHA1_ASCII_LENGTH] = '\0'; 1239 if (prom_debug) { 1240 dboot_printf("checking expected hash [%s]: ", 1241 displayhash); 1242 } 1243 1244 if (check_image_hash(i) != 0) 1245 dboot_panic("hash mismatch!\n"); 1246 else 1247 DBG_MSG("OK\n"); 1248 } 1249 } 1250 1251 /* 1252 * Determine the module's starting address, size, name, and type, and fill the 1253 * boot_modules structure. This structure is used by the bop code, except for 1254 * hashes which are checked prior to transferring control to the kernel. 1255 */ 1256 static void 1257 process_module(int midx) 1258 { 1259 uint32_t mod_start = dboot_multiboot_modstart(midx); 1260 uint32_t mod_end = dboot_multiboot_modend(midx); 1261 char *cmdline = dboot_multiboot_modcmdline(midx); 1262 char *p, *q; 1263 1264 check_higher(mod_end); 1265 if (prom_debug) { 1266 dboot_printf("\tmodule #%d: '%s' at 0x%lx, end 0x%lx\n", 1267 midx, cmdline, (ulong_t)mod_start, (ulong_t)mod_end); 1268 } 1269 1270 if (mod_start > mod_end) { 1271 dboot_panic("module #%d: module start address 0x%lx greater " 1272 "than end address 0x%lx", midx, 1273 (ulong_t)mod_start, (ulong_t)mod_end); 1274 } 1275 1276 /* 1277 * A brief note on lengths and sizes: GRUB, for reasons unknown, passes 1278 * the address of the last valid byte in a module plus 1 as mod_end. 1279 * This is of course a bug; the multiboot specification simply states 1280 * that mod_start and mod_end "contain the start and end addresses of 1281 * the boot module itself" which is pretty obviously not what GRUB is 1282 * doing. However, fixing it requires that not only this code be 1283 * changed but also that other code consuming this value and values 1284 * derived from it be fixed, and that the kernel and GRUB must either 1285 * both have the bug or neither. While there are a lot of combinations 1286 * that will work, there are also some that won't, so for simplicity 1287 * we'll just cope with the bug. That means we won't actually hash the 1288 * byte at mod_end, and we will expect that mod_end for the hash file 1289 * itself is one greater than some multiple of 41 (40 bytes of ASCII 1290 * hash plus a newline for each module). We set bm_size to the true 1291 * correct number of bytes in each module, achieving exactly this. 1292 */ 1293 1294 modules[midx].bm_addr = (native_ptr_t)(uintptr_t)mod_start; 1295 modules[midx].bm_size = mod_end - mod_start; 1296 modules[midx].bm_name = (native_ptr_t)(uintptr_t)cmdline; 1297 modules[midx].bm_hash = (native_ptr_t)(uintptr_t)NULL; 1298 modules[midx].bm_type = BMT_FILE; 1299 1300 if (cmdline == NULL) { 1301 modules[midx].bm_name = (native_ptr_t)(uintptr_t)noname; 1302 return; 1303 } 1304 1305 p = cmdline; 1306 modules[midx].bm_name = 1307 (native_ptr_t)(uintptr_t)strsep(&p, " \t\f\n\r"); 1308 1309 while (p != NULL) { 1310 q = strsep(&p, " \t\f\n\r"); 1311 if (strncmp(q, "name=", 5) == 0) { 1312 if (q[5] != '\0' && !isspace(q[5])) { 1313 modules[midx].bm_name = 1314 (native_ptr_t)(uintptr_t)(q + 5); 1315 } 1316 continue; 1317 } 1318 1319 if (strncmp(q, "type=", 5) == 0) { 1320 if (q[5] == '\0' || isspace(q[5])) 1321 continue; 1322 q += 5; 1323 if (strcmp(q, "rootfs") == 0) { 1324 modules[midx].bm_type = BMT_ROOTFS; 1325 } else if (strcmp(q, "hash") == 0) { 1326 modules[midx].bm_type = BMT_HASH; 1327 } else if (strcmp(q, "environment") == 0) { 1328 modules[midx].bm_type = BMT_ENV; 1329 } else if (strcmp(q, "file") != 0) { 1330 dboot_printf("\tmodule #%d: unknown module " 1331 "type '%s'; defaulting to 'file'\n", 1332 midx, q); 1333 } 1334 continue; 1335 } 1336 1337 if (strncmp(q, "hash=", 5) == 0) { 1338 if (q[5] != '\0' && !isspace(q[5])) { 1339 modules[midx].bm_hash = 1340 (native_ptr_t)(uintptr_t)(q + 5); 1341 } 1342 continue; 1343 } 1344 1345 dboot_printf("ignoring unknown option '%s'\n", q); 1346 } 1347 } 1348 1349 /* 1350 * Backward compatibility: if there are exactly one or two modules, both 1351 * of type 'file' and neither with an embedded hash value, we have been 1352 * given the legacy style modules. In this case we need to treat the first 1353 * module as a rootfs and the second as a hash referencing that module. 1354 * Otherwise, even if the configuration is invalid, we assume that the 1355 * operator knows what he's doing or at least isn't being bitten by this 1356 * interface change. 1357 */ 1358 static void 1359 fixup_modules(void) 1360 { 1361 if (modules_used == 0 || modules_used > 2) 1362 return; 1363 1364 if (modules[0].bm_type != BMT_FILE || 1365 modules_used > 1 && modules[1].bm_type != BMT_FILE) { 1366 return; 1367 } 1368 1369 if (modules[0].bm_hash != (native_ptr_t)(uintptr_t)NULL || 1370 modules_used > 1 && 1371 modules[1].bm_hash != (native_ptr_t)(uintptr_t)NULL) { 1372 return; 1373 } 1374 1375 modules[0].bm_type = BMT_ROOTFS; 1376 if (modules_used > 1) { 1377 modules[1].bm_type = BMT_HASH; 1378 modules[1].bm_name = modules[0].bm_name; 1379 } 1380 } 1381 1382 /* 1383 * For modules that do not have assigned hashes but have a separate hash module, 1384 * find the assigned hash module and set the primary module's bm_hash to point 1385 * to the hash data from that module. We will then ignore modules of type 1386 * BMT_HASH from this point forward. 1387 */ 1388 static void 1389 assign_module_hashes(void) 1390 { 1391 uint_t i, j; 1392 1393 for (i = 0; i < modules_used; i++) { 1394 if (modules[i].bm_type == BMT_HASH || 1395 modules[i].bm_hash != (native_ptr_t)(uintptr_t)NULL) { 1396 continue; 1397 } 1398 1399 for (j = 0; j < modules_used; j++) { 1400 if (modules[j].bm_type != BMT_HASH || 1401 strcmp((char *)(uintptr_t)modules[j].bm_name, 1402 (char *)(uintptr_t)modules[i].bm_name) != 0) { 1403 continue; 1404 } 1405 1406 if (modules[j].bm_size < SHA1_ASCII_LENGTH) { 1407 dboot_printf("Short hash module of length " 1408 "0x%lx bytes; ignoring\n", 1409 (ulong_t)modules[j].bm_size); 1410 } else { 1411 modules[i].bm_hash = modules[j].bm_addr; 1412 } 1413 break; 1414 } 1415 } 1416 } 1417 1418 /* 1419 * Walk through the module information finding the last used address. 1420 * The first available address will become the top level page table. 1421 */ 1422 static void 1423 dboot_process_modules(void) 1424 { 1425 int i, modcount; 1426 extern char _end[]; 1427 1428 DBG_MSG("\nFinding Modules\n"); 1429 modcount = dboot_multiboot_modcount(); 1430 if (modcount > MAX_BOOT_MODULES) { 1431 dboot_panic("Too many modules (%d) -- the maximum is %d.", 1432 modcount, MAX_BOOT_MODULES); 1433 } 1434 /* 1435 * search the modules to find the last used address 1436 * we'll build the module list while we're walking through here 1437 */ 1438 check_higher((paddr_t)(uintptr_t)&_end); 1439 for (i = 0; i < modcount; ++i) { 1440 process_module(i); 1441 modules_used++; 1442 } 1443 bi->bi_modules = (native_ptr_t)(uintptr_t)modules; 1444 DBG(bi->bi_modules); 1445 bi->bi_module_cnt = modcount; 1446 DBG(bi->bi_module_cnt); 1447 1448 fixup_modules(); 1449 assign_module_hashes(); 1450 check_images(); 1451 } 1452 1453 /* 1454 * We then build the phys_install memlist from the multiboot information. 1455 */ 1456 static void 1457 dboot_process_mmap(void) 1458 { 1459 uint64_t start; 1460 uint64_t end; 1461 uint64_t page_offset = MMU_PAGEOFFSET; /* needs to be 64 bits */ 1462 uint32_t lower, upper; 1463 int i, mmap_entries; 1464 1465 /* 1466 * Walk through the memory map from multiboot and build our memlist 1467 * structures. Note these will have native format pointers. 1468 */ 1469 DBG_MSG("\nFinding Memory Map\n"); 1470 num_entries = 0; 1471 num_entries_set = B_FALSE; 1472 max_mem = 0; 1473 if ((mmap_entries = dboot_loader_mmap_entries()) > 0) { 1474 for (i = 0; i < mmap_entries; i++) { 1475 uint32_t type = dboot_loader_mmap_get_type(i); 1476 start = dboot_loader_mmap_get_base(i); 1477 end = start + dboot_loader_mmap_get_length(i); 1478 1479 if (prom_debug) 1480 dboot_printf("\ttype: %d %" PRIx64 "..%" 1481 PRIx64 "\n", type, start, end); 1482 1483 /* 1484 * page align start and end 1485 */ 1486 start = (start + page_offset) & ~page_offset; 1487 end &= ~page_offset; 1488 if (end <= start) 1489 continue; 1490 1491 /* 1492 * only type 1 is usable RAM 1493 */ 1494 switch (type) { 1495 case 1: 1496 if (end > max_mem) 1497 max_mem = end; 1498 memlists[memlists_used].addr = start; 1499 memlists[memlists_used].size = end - start; 1500 ++memlists_used; 1501 if (memlists_used > MAX_MEMLIST) 1502 dboot_panic("too many memlists"); 1503 break; 1504 case 2: 1505 rsvdmemlists[rsvdmemlists_used].addr = start; 1506 rsvdmemlists[rsvdmemlists_used].size = 1507 end - start; 1508 ++rsvdmemlists_used; 1509 if (rsvdmemlists_used > MAX_MEMLIST) 1510 dboot_panic("too many rsvdmemlists"); 1511 break; 1512 default: 1513 continue; 1514 } 1515 } 1516 build_pcimemlists(); 1517 } else if (dboot_multiboot_basicmeminfo(&lower, &upper)) { 1518 DBG(lower); 1519 memlists[memlists_used].addr = 0; 1520 memlists[memlists_used].size = lower * 1024; 1521 ++memlists_used; 1522 DBG(upper); 1523 memlists[memlists_used].addr = 1024 * 1024; 1524 memlists[memlists_used].size = upper * 1024; 1525 ++memlists_used; 1526 1527 /* 1528 * Old platform - assume I/O space at the end of memory. 1529 */ 1530 pcimemlists[0].addr = (upper * 1024) + (1024 * 1024); 1531 pcimemlists[0].size = pci_hi_limit - pcimemlists[0].addr; 1532 pcimemlists[0].next = 0; 1533 pcimemlists[0].prev = 0; 1534 bi->bi_pcimem = (native_ptr_t)(uintptr_t)pcimemlists; 1535 DBG(bi->bi_pcimem); 1536 } else { 1537 dboot_panic("No memory info from boot loader!!!"); 1538 } 1539 1540 /* 1541 * finish processing the physinstall list 1542 */ 1543 sort_physinstall(); 1544 1545 /* 1546 * build bios reserved mem lists 1547 */ 1548 build_rsvdmemlists(); 1549 } 1550 1551 /* 1552 * The highest address is used as the starting point for dboot's simple 1553 * memory allocator. 1554 * 1555 * Finding the highest address in case of Multiboot 1 protocol is 1556 * quite painful in the sense that some information provided by 1557 * the multiboot info structure points to BIOS data, and some to RAM. 1558 * 1559 * The module list was processed and checked already by dboot_process_modules(), 1560 * so we will check the command line string and the memory map. 1561 * 1562 * This list of to be checked items is based on our current knowledge of 1563 * allocations made by grub1 and will need to be reviewed if there 1564 * are updates about the information provided by Multiboot 1. 1565 * 1566 * In the case of the Multiboot 2, our life is much simpler, as the MB2 1567 * information tag list is one contiguous chunk of memory. 1568 */ 1569 static paddr_t 1570 dboot_multiboot1_highest_addr(void) 1571 { 1572 paddr_t addr = (paddr_t)(uintptr_t)NULL; 1573 char *cmdl = (char *)mb_info->cmdline; 1574 1575 if (mb_info->flags & MB_INFO_CMDLINE) 1576 addr = ((paddr_t)((uintptr_t)cmdl + strlen(cmdl) + 1)); 1577 1578 if (mb_info->flags & MB_INFO_MEM_MAP) 1579 addr = MAX(addr, 1580 ((paddr_t)(mb_info->mmap_addr + mb_info->mmap_length))); 1581 return (addr); 1582 } 1583 1584 static void 1585 dboot_multiboot_highest_addr(void) 1586 { 1587 paddr_t addr; 1588 1589 switch (multiboot_version) { 1590 case 1: 1591 addr = dboot_multiboot1_highest_addr(); 1592 if (addr != (paddr_t)(uintptr_t)NULL) 1593 check_higher(addr); 1594 break; 1595 case 2: 1596 addr = dboot_multiboot2_highest_addr(mb2_info); 1597 if (addr != (paddr_t)(uintptr_t)NULL) 1598 check_higher(addr); 1599 break; 1600 default: 1601 dboot_panic("Unknown multiboot version: %d\n", 1602 multiboot_version); 1603 break; 1604 } 1605 } 1606 1607 /* 1608 * Walk the boot loader provided information and find the highest free address. 1609 */ 1610 static void 1611 init_mem_alloc(void) 1612 { 1613 DBG_MSG("Entered init_mem_alloc()\n"); 1614 dboot_process_modules(); 1615 dboot_process_mmap(); 1616 dboot_multiboot_highest_addr(); 1617 } 1618 1619 static int 1620 dboot_same_guids(efi_guid_t *g1, efi_guid_t *g2) 1621 { 1622 int i; 1623 1624 if (g1->time_low != g2->time_low) 1625 return (0); 1626 if (g1->time_mid != g2->time_mid) 1627 return (0); 1628 if (g1->time_hi_and_version != g2->time_hi_and_version) 1629 return (0); 1630 if (g1->clock_seq_hi_and_reserved != g2->clock_seq_hi_and_reserved) 1631 return (0); 1632 if (g1->clock_seq_low != g2->clock_seq_low) 1633 return (0); 1634 1635 for (i = 0; i < 6; i++) { 1636 if (g1->node_addr[i] != g2->node_addr[i]) 1637 return (0); 1638 } 1639 return (1); 1640 } 1641 1642 static void 1643 process_efi32(EFI_SYSTEM_TABLE32 *efi) 1644 { 1645 uint32_t entries; 1646 EFI_CONFIGURATION_TABLE32 *config; 1647 int i; 1648 1649 entries = efi->NumberOfTableEntries; 1650 config = (EFI_CONFIGURATION_TABLE32 *)(uintptr_t) 1651 efi->ConfigurationTable; 1652 1653 for (i = 0; i < entries; i++) { 1654 if (dboot_same_guids(&config[i].VendorGuid, &smbios3)) { 1655 bi->bi_smbios = (native_ptr_t)(uintptr_t) 1656 config[i].VendorTable; 1657 } 1658 if (bi->bi_smbios == NULL && 1659 dboot_same_guids(&config[i].VendorGuid, &smbios)) { 1660 bi->bi_smbios = (native_ptr_t)(uintptr_t) 1661 config[i].VendorTable; 1662 } 1663 if (dboot_same_guids(&config[i].VendorGuid, &acpi2)) { 1664 bi->bi_acpi_rsdp = (native_ptr_t)(uintptr_t) 1665 config[i].VendorTable; 1666 } 1667 if (bi->bi_acpi_rsdp == NULL && 1668 dboot_same_guids(&config[i].VendorGuid, &acpi1)) { 1669 bi->bi_acpi_rsdp = (native_ptr_t)(uintptr_t) 1670 config[i].VendorTable; 1671 } 1672 } 1673 } 1674 1675 static void 1676 process_efi64(EFI_SYSTEM_TABLE64 *efi) 1677 { 1678 uint64_t entries; 1679 EFI_CONFIGURATION_TABLE64 *config; 1680 int i; 1681 1682 entries = efi->NumberOfTableEntries; 1683 config = (EFI_CONFIGURATION_TABLE64 *)(uintptr_t) 1684 efi->ConfigurationTable; 1685 1686 for (i = 0; i < entries; i++) { 1687 if (dboot_same_guids(&config[i].VendorGuid, &smbios3)) { 1688 bi->bi_smbios = (native_ptr_t)(uintptr_t) 1689 config[i].VendorTable; 1690 } 1691 if (bi->bi_smbios == NULL && 1692 dboot_same_guids(&config[i].VendorGuid, &smbios)) { 1693 bi->bi_smbios = (native_ptr_t)(uintptr_t) 1694 config[i].VendorTable; 1695 } 1696 /* Prefer acpi v2+ over v1. */ 1697 if (dboot_same_guids(&config[i].VendorGuid, &acpi2)) { 1698 bi->bi_acpi_rsdp = (native_ptr_t)(uintptr_t) 1699 config[i].VendorTable; 1700 } 1701 if (bi->bi_acpi_rsdp == NULL && 1702 dboot_same_guids(&config[i].VendorGuid, &acpi1)) { 1703 bi->bi_acpi_rsdp = (native_ptr_t)(uintptr_t) 1704 config[i].VendorTable; 1705 } 1706 } 1707 } 1708 1709 static void 1710 dboot_multiboot_get_fwtables(void) 1711 { 1712 multiboot_tag_new_acpi_t *nacpitagp; 1713 multiboot_tag_old_acpi_t *oacpitagp; 1714 multiboot_tag_efi64_t *efi64tagp = NULL; 1715 multiboot_tag_efi32_t *efi32tagp = NULL; 1716 1717 /* no fw tables from multiboot 1 */ 1718 if (multiboot_version != 2) 1719 return; 1720 1721 efi64tagp = (multiboot_tag_efi64_t *) 1722 dboot_multiboot2_find_tag(mb2_info, MULTIBOOT_TAG_TYPE_EFI64); 1723 if (efi64tagp != NULL) { 1724 bi->bi_uefi_arch = XBI_UEFI_ARCH_64; 1725 bi->bi_uefi_systab = (native_ptr_t)(uintptr_t) 1726 efi64tagp->mb_pointer; 1727 process_efi64((EFI_SYSTEM_TABLE64 *)(uintptr_t) 1728 efi64tagp->mb_pointer); 1729 } else { 1730 efi32tagp = (multiboot_tag_efi32_t *) 1731 dboot_multiboot2_find_tag(mb2_info, 1732 MULTIBOOT_TAG_TYPE_EFI32); 1733 if (efi32tagp != NULL) { 1734 bi->bi_uefi_arch = XBI_UEFI_ARCH_32; 1735 bi->bi_uefi_systab = (native_ptr_t)(uintptr_t) 1736 efi32tagp->mb_pointer; 1737 process_efi32((EFI_SYSTEM_TABLE32 *)(uintptr_t) 1738 efi32tagp->mb_pointer); 1739 } 1740 } 1741 1742 /* 1743 * The ACPI RSDP can be found by scanning the BIOS memory areas or 1744 * from the EFI system table. The boot loader may pass in the address 1745 * it found the ACPI tables at. 1746 */ 1747 nacpitagp = (multiboot_tag_new_acpi_t *) 1748 dboot_multiboot2_find_tag(mb2_info, 1749 MULTIBOOT_TAG_TYPE_ACPI_NEW); 1750 oacpitagp = (multiboot_tag_old_acpi_t *) 1751 dboot_multiboot2_find_tag(mb2_info, 1752 MULTIBOOT_TAG_TYPE_ACPI_OLD); 1753 1754 if (nacpitagp != NULL) { 1755 bi->bi_acpi_rsdp = (native_ptr_t)(uintptr_t) 1756 &nacpitagp->mb_rsdp[0]; 1757 } else if (oacpitagp != NULL) { 1758 bi->bi_acpi_rsdp = (native_ptr_t)(uintptr_t) 1759 &oacpitagp->mb_rsdp[0]; 1760 } 1761 } 1762 1763 /* print out EFI version string with newline */ 1764 static void 1765 dboot_print_efi_version(uint32_t ver) 1766 { 1767 int rev; 1768 1769 dboot_printf("%d.", EFI_REV_MAJOR(ver)); 1770 1771 rev = EFI_REV_MINOR(ver); 1772 if ((rev % 10) != 0) { 1773 dboot_printf("%d.%d\n", rev / 10, rev % 10); 1774 } else { 1775 dboot_printf("%d\n", rev / 10); 1776 } 1777 } 1778 1779 static void 1780 print_efi32(EFI_SYSTEM_TABLE32 *efi) 1781 { 1782 uint16_t *data; 1783 EFI_CONFIGURATION_TABLE32 *conf; 1784 int i; 1785 1786 dboot_printf("EFI32 signature: %llx\n", 1787 (unsigned long long)efi->Hdr.Signature); 1788 dboot_printf("EFI system version: "); 1789 dboot_print_efi_version(efi->Hdr.Revision); 1790 dboot_printf("EFI system vendor: "); 1791 data = (uint16_t *)(uintptr_t)efi->FirmwareVendor; 1792 for (i = 0; data[i] != 0; i++) 1793 dboot_printf("%c", (char)data[i]); 1794 dboot_printf("\nEFI firmware revision: "); 1795 dboot_print_efi_version(efi->FirmwareRevision); 1796 dboot_printf("EFI system table number of entries: %d\n", 1797 efi->NumberOfTableEntries); 1798 conf = (EFI_CONFIGURATION_TABLE32 *)(uintptr_t) 1799 efi->ConfigurationTable; 1800 for (i = 0; i < (int)efi->NumberOfTableEntries; i++) { 1801 dboot_printf("%d: 0x%x 0x%x 0x%x 0x%x 0x%x", i, 1802 conf[i].VendorGuid.time_low, 1803 conf[i].VendorGuid.time_mid, 1804 conf[i].VendorGuid.time_hi_and_version, 1805 conf[i].VendorGuid.clock_seq_hi_and_reserved, 1806 conf[i].VendorGuid.clock_seq_low); 1807 dboot_printf(" 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x\n", 1808 conf[i].VendorGuid.node_addr[0], 1809 conf[i].VendorGuid.node_addr[1], 1810 conf[i].VendorGuid.node_addr[2], 1811 conf[i].VendorGuid.node_addr[3], 1812 conf[i].VendorGuid.node_addr[4], 1813 conf[i].VendorGuid.node_addr[5]); 1814 } 1815 } 1816 1817 static void 1818 print_efi64(EFI_SYSTEM_TABLE64 *efi) 1819 { 1820 uint16_t *data; 1821 EFI_CONFIGURATION_TABLE64 *conf; 1822 int i; 1823 1824 dboot_printf("EFI64 signature: %llx\n", 1825 (unsigned long long)efi->Hdr.Signature); 1826 dboot_printf("EFI system version: "); 1827 dboot_print_efi_version(efi->Hdr.Revision); 1828 dboot_printf("EFI system vendor: "); 1829 data = (uint16_t *)(uintptr_t)efi->FirmwareVendor; 1830 for (i = 0; data[i] != 0; i++) 1831 dboot_printf("%c", (char)data[i]); 1832 dboot_printf("\nEFI firmware revision: "); 1833 dboot_print_efi_version(efi->FirmwareRevision); 1834 dboot_printf("EFI system table number of entries: %lld\n", 1835 efi->NumberOfTableEntries); 1836 conf = (EFI_CONFIGURATION_TABLE64 *)(uintptr_t) 1837 efi->ConfigurationTable; 1838 for (i = 0; i < (int)efi->NumberOfTableEntries; i++) { 1839 dboot_printf("%d: 0x%x 0x%x 0x%x 0x%x 0x%x", i, 1840 conf[i].VendorGuid.time_low, 1841 conf[i].VendorGuid.time_mid, 1842 conf[i].VendorGuid.time_hi_and_version, 1843 conf[i].VendorGuid.clock_seq_hi_and_reserved, 1844 conf[i].VendorGuid.clock_seq_low); 1845 dboot_printf(" 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x\n", 1846 conf[i].VendorGuid.node_addr[0], 1847 conf[i].VendorGuid.node_addr[1], 1848 conf[i].VendorGuid.node_addr[2], 1849 conf[i].VendorGuid.node_addr[3], 1850 conf[i].VendorGuid.node_addr[4], 1851 conf[i].VendorGuid.node_addr[5]); 1852 } 1853 } 1854 #endif /* !__xpv */ 1855 1856 /* 1857 * Simple memory allocator, allocates aligned physical memory. 1858 * Note that startup_kernel() only allocates memory, never frees. 1859 * Memory usage just grows in an upward direction. 1860 */ 1861 static void * 1862 do_mem_alloc(uint32_t size, uint32_t align) 1863 { 1864 uint_t i; 1865 uint64_t best; 1866 uint64_t start; 1867 uint64_t end; 1868 1869 /* 1870 * make sure size is a multiple of pagesize 1871 */ 1872 size = RNDUP(size, MMU_PAGESIZE); 1873 next_avail_addr = RNDUP(next_avail_addr, align); 1874 1875 /* 1876 * XXPV fixme joe 1877 * 1878 * a really large bootarchive that causes you to run out of memory 1879 * may cause this to blow up 1880 */ 1881 /* LINTED E_UNEXPECTED_UINT_PROMOTION */ 1882 best = (uint64_t)-size; 1883 for (i = 0; i < memlists_used; ++i) { 1884 start = memlists[i].addr; 1885 #if defined(__xpv) 1886 start += mfn_base; 1887 #endif 1888 end = start + memlists[i].size; 1889 1890 /* 1891 * did we find the desired address? 1892 */ 1893 if (start <= next_avail_addr && next_avail_addr + size <= end) { 1894 best = next_avail_addr; 1895 goto done; 1896 } 1897 1898 /* 1899 * if not is this address the best so far? 1900 */ 1901 if (start > next_avail_addr && start < best && 1902 RNDUP(start, align) + size <= end) 1903 best = RNDUP(start, align); 1904 } 1905 1906 /* 1907 * We didn't find exactly the address we wanted, due to going off the 1908 * end of a memory region. Return the best found memory address. 1909 */ 1910 done: 1911 next_avail_addr = best + size; 1912 #if defined(__xpv) 1913 if (next_avail_addr > scratch_end) 1914 dboot_panic("Out of mem next_avail: 0x%lx, scratch_end: " 1915 "0x%lx", (ulong_t)next_avail_addr, 1916 (ulong_t)scratch_end); 1917 #endif 1918 (void) memset((void *)(uintptr_t)best, 0, size); 1919 return ((void *)(uintptr_t)best); 1920 } 1921 1922 void * 1923 mem_alloc(uint32_t size) 1924 { 1925 return (do_mem_alloc(size, MMU_PAGESIZE)); 1926 } 1927 1928 1929 /* 1930 * Build page tables to map all of memory used so far as well as the kernel. 1931 */ 1932 static void 1933 build_page_tables(void) 1934 { 1935 uint32_t psize; 1936 uint32_t level; 1937 uint32_t off; 1938 uint64_t start; 1939 #if !defined(__xpv) 1940 uint32_t i; 1941 uint64_t end; 1942 #endif /* __xpv */ 1943 1944 /* 1945 * If we're on metal, we need to create the top level pagetable. 1946 */ 1947 #if defined(__xpv) 1948 top_page_table = (paddr_t)(uintptr_t)xen_info->pt_base; 1949 #else /* __xpv */ 1950 top_page_table = (paddr_t)(uintptr_t)mem_alloc(MMU_PAGESIZE); 1951 #endif /* __xpv */ 1952 DBG((uintptr_t)top_page_table); 1953 1954 /* 1955 * Determine if we'll use large mappings for kernel, then map it. 1956 */ 1957 if (largepage_support) { 1958 psize = lpagesize; 1959 level = 1; 1960 } else { 1961 psize = MMU_PAGESIZE; 1962 level = 0; 1963 } 1964 1965 DBG_MSG("Mapping kernel\n"); 1966 DBG(ktext_phys); 1967 DBG(target_kernel_text); 1968 DBG(ksize); 1969 DBG(psize); 1970 for (off = 0; off < ksize; off += psize) 1971 map_pa_at_va(ktext_phys + off, target_kernel_text + off, level); 1972 1973 /* 1974 * The kernel will need a 1 page window to work with page tables 1975 */ 1976 bi->bi_pt_window = (native_ptr_t)(uintptr_t)mem_alloc(MMU_PAGESIZE); 1977 DBG(bi->bi_pt_window); 1978 bi->bi_pte_to_pt_window = 1979 (native_ptr_t)(uintptr_t)find_pte(bi->bi_pt_window, NULL, 0, 0); 1980 DBG(bi->bi_pte_to_pt_window); 1981 1982 #if defined(__xpv) 1983 if (!DOMAIN_IS_INITDOMAIN(xen_info)) { 1984 /* If this is a domU we're done. */ 1985 DBG_MSG("\nPage tables constructed\n"); 1986 return; 1987 } 1988 #endif /* __xpv */ 1989 1990 /* 1991 * We need 1:1 mappings for the lower 1M of memory to access 1992 * BIOS tables used by a couple of drivers during boot. 1993 * 1994 * The following code works because our simple memory allocator 1995 * only grows usage in an upwards direction. 1996 * 1997 * Note that by this point in boot some mappings for low memory 1998 * may already exist because we've already accessed device in low 1999 * memory. (Specifically the video frame buffer and keyboard 2000 * status ports.) If we're booting on raw hardware then GRUB 2001 * created these mappings for us. If we're booting under a 2002 * hypervisor then we went ahead and remapped these devices into 2003 * memory allocated within dboot itself. 2004 */ 2005 if (map_debug) 2006 dboot_printf("1:1 map pa=0..1Meg\n"); 2007 for (start = 0; start < 1024 * 1024; start += MMU_PAGESIZE) { 2008 #if defined(__xpv) 2009 map_ma_at_va(start, start, 0); 2010 #else /* __xpv */ 2011 map_pa_at_va(start, start, 0); 2012 #endif /* __xpv */ 2013 } 2014 2015 #if !defined(__xpv) 2016 2017 for (i = 0; i < memlists_used; ++i) { 2018 start = memlists[i].addr; 2019 end = start + memlists[i].size; 2020 2021 if (map_debug) 2022 dboot_printf("1:1 map pa=%" PRIx64 "..%" PRIx64 "\n", 2023 start, end); 2024 while (start < end && start < next_avail_addr) { 2025 map_pa_at_va(start, start, 0); 2026 start += MMU_PAGESIZE; 2027 } 2028 if (start >= next_avail_addr) 2029 break; 2030 } 2031 2032 /* 2033 * Map framebuffer memory as PT_NOCACHE as this is memory from a 2034 * device and therefore must not be cached. 2035 */ 2036 if (bi->bi_framebuffer != NULL && fb->framebuffer != 0) { 2037 multiboot_tag_framebuffer_t *fb_tagp; 2038 fb_tagp = (multiboot_tag_framebuffer_t *)(uintptr_t) 2039 fb->framebuffer; 2040 2041 start = fb_tagp->framebuffer_common.framebuffer_addr; 2042 end = start + fb_tagp->framebuffer_common.framebuffer_height * 2043 fb_tagp->framebuffer_common.framebuffer_pitch; 2044 2045 if (map_debug) 2046 dboot_printf("FB 1:1 map pa=%" PRIx64 "..%" PRIx64 "\n", 2047 start, end); 2048 pte_bits |= PT_NOCACHE; 2049 if (PAT_support != 0) 2050 pte_bits |= PT_PAT_4K; 2051 2052 while (start < end) { 2053 map_pa_at_va(start, start, 0); 2054 start += MMU_PAGESIZE; 2055 } 2056 pte_bits &= ~PT_NOCACHE; 2057 if (PAT_support != 0) 2058 pte_bits &= ~PT_PAT_4K; 2059 } 2060 #endif /* !__xpv */ 2061 2062 DBG_MSG("\nPage tables constructed\n"); 2063 } 2064 2065 #define NO_MULTIBOOT \ 2066 "multiboot is no longer used to boot the Solaris Operating System.\n\ 2067 The grub entry should be changed to:\n\ 2068 kernel$ /platform/i86pc/kernel/$ISADIR/unix\n\ 2069 module$ /platform/i86pc/$ISADIR/boot_archive\n\ 2070 See http://illumos.org/msg/SUNOS-8000-AK for details.\n" 2071 2072 static void 2073 dboot_init_xboot_consinfo(void) 2074 { 2075 bi = &boot_info; 2076 2077 #if !defined(__xpv) 2078 fb = &framebuffer; 2079 bi->bi_framebuffer = (native_ptr_t)(uintptr_t)fb; 2080 2081 switch (multiboot_version) { 2082 case 1: 2083 dboot_multiboot1_xboot_consinfo(); 2084 break; 2085 case 2: 2086 dboot_multiboot2_xboot_consinfo(); 2087 break; 2088 default: 2089 dboot_panic("Unknown multiboot version: %d\n", 2090 multiboot_version); 2091 break; 2092 } 2093 /* 2094 * Lookup environment module for the console. Complete module list 2095 * will be built after console setup. 2096 */ 2097 dboot_find_env(); 2098 #endif 2099 } 2100 2101 /* 2102 * Set up basic data from the boot loader. 2103 * The load_addr is part of AOUT kludge setup in dboot_grub.s, to support 2104 * 32-bit dboot code setup used to set up and start 64-bit kernel. 2105 * AOUT kludge does allow 32-bit boot loader, such as grub1, to load and 2106 * start 64-bit illumos kernel. 2107 */ 2108 static void 2109 dboot_loader_init(void) 2110 { 2111 #if !defined(__xpv) 2112 mb_info = NULL; 2113 mb2_info = NULL; 2114 2115 switch (mb_magic) { 2116 case MB_BOOTLOADER_MAGIC: 2117 multiboot_version = 1; 2118 mb_info = (multiboot_info_t *)(uintptr_t)mb_addr; 2119 #if defined(_BOOT_TARGET_amd64) 2120 load_addr = mb_header.load_addr; 2121 #endif 2122 break; 2123 2124 case MULTIBOOT2_BOOTLOADER_MAGIC: 2125 multiboot_version = 2; 2126 mb2_info = (multiboot2_info_header_t *)(uintptr_t)mb_addr; 2127 mb2_mmap_tagp = dboot_multiboot2_get_mmap_tagp(mb2_info); 2128 #if defined(_BOOT_TARGET_amd64) 2129 load_addr = mb2_load_addr; 2130 #endif 2131 break; 2132 2133 default: 2134 dboot_panic("Unknown bootloader magic: 0x%x\n", mb_magic); 2135 break; 2136 } 2137 #endif /* !defined(__xpv) */ 2138 } 2139 2140 /* Extract the kernel command line from [multi]boot information. */ 2141 static char * 2142 dboot_loader_cmdline(void) 2143 { 2144 char *line = NULL; 2145 2146 #if defined(__xpv) 2147 line = (char *)xen_info->cmd_line; 2148 #else /* __xpv */ 2149 2150 switch (multiboot_version) { 2151 case 1: 2152 if (mb_info->flags & MB_INFO_CMDLINE) 2153 line = (char *)mb_info->cmdline; 2154 break; 2155 2156 case 2: 2157 line = dboot_multiboot2_cmdline(mb2_info); 2158 break; 2159 2160 default: 2161 dboot_panic("Unknown multiboot version: %d\n", 2162 multiboot_version); 2163 break; 2164 } 2165 2166 #endif /* __xpv */ 2167 2168 /* 2169 * Make sure we have valid pointer so the string operations 2170 * will not crash us. 2171 */ 2172 if (line == NULL) 2173 line = ""; 2174 2175 return (line); 2176 } 2177 2178 static char * 2179 dboot_loader_name(void) 2180 { 2181 #if defined(__xpv) 2182 return (NULL); 2183 #else /* __xpv */ 2184 multiboot_tag_string_t *tag; 2185 2186 switch (multiboot_version) { 2187 case 1: 2188 return ((char *)mb_info->boot_loader_name); 2189 2190 case 2: 2191 tag = dboot_multiboot2_find_tag(mb2_info, 2192 MULTIBOOT_TAG_TYPE_BOOT_LOADER_NAME); 2193 return (tag->mb_string); 2194 default: 2195 dboot_panic("Unknown multiboot version: %d\n", 2196 multiboot_version); 2197 break; 2198 } 2199 2200 return (NULL); 2201 #endif /* __xpv */ 2202 } 2203 2204 /* 2205 * startup_kernel has a pretty simple job. It builds pagetables which reflect 2206 * 1:1 mappings for all memory in use. It then also adds mappings for 2207 * the kernel nucleus at virtual address of target_kernel_text using large page 2208 * mappings. The page table pages are also accessible at 1:1 mapped 2209 * virtual addresses. 2210 */ 2211 /*ARGSUSED*/ 2212 void 2213 startup_kernel(void) 2214 { 2215 char *cmdline; 2216 char *bootloader; 2217 #if defined(__xpv) 2218 physdev_set_iopl_t set_iopl; 2219 #endif /* __xpv */ 2220 2221 if (dboot_debug == 1) 2222 bcons_init(NULL); /* Set very early console to ttya. */ 2223 dboot_loader_init(); 2224 /* 2225 * At this point we are executing in a 32 bit real mode. 2226 */ 2227 2228 bootloader = dboot_loader_name(); 2229 cmdline = dboot_loader_cmdline(); 2230 2231 #if defined(__xpv) 2232 /* 2233 * For dom0, before we initialize the console subsystem we'll 2234 * need to enable io operations, so set I/O priveldge level to 1. 2235 */ 2236 if (DOMAIN_IS_INITDOMAIN(xen_info)) { 2237 set_iopl.iopl = 1; 2238 (void) HYPERVISOR_physdev_op(PHYSDEVOP_set_iopl, &set_iopl); 2239 } 2240 #endif /* __xpv */ 2241 2242 dboot_init_xboot_consinfo(); 2243 bi->bi_cmdline = (native_ptr_t)(uintptr_t)cmdline; 2244 bcons_init(bi); /* Now we can set the real console. */ 2245 2246 prom_debug = (find_boot_prop("prom_debug") != NULL); 2247 map_debug = (find_boot_prop("map_debug") != NULL); 2248 2249 #if !defined(__xpv) 2250 dboot_multiboot_get_fwtables(); 2251 #endif 2252 DBG_MSG("\n\nillumos prekernel set: "); 2253 DBG_MSG(cmdline); 2254 DBG_MSG("\n"); 2255 2256 if (bootloader != NULL && prom_debug) { 2257 dboot_printf("Kernel loaded by: %s\n", bootloader); 2258 #if !defined(__xpv) 2259 dboot_printf("Using multiboot %d boot protocol.\n", 2260 multiboot_version); 2261 #endif 2262 } 2263 2264 if (strstr(cmdline, "multiboot") != NULL) { 2265 dboot_panic(NO_MULTIBOOT); 2266 } 2267 2268 DBG((uintptr_t)bi); 2269 #if !defined(__xpv) 2270 DBG((uintptr_t)mb_info); 2271 DBG((uintptr_t)mb2_info); 2272 if (mb2_info != NULL) 2273 DBG(mb2_info->mbi_total_size); 2274 DBG(bi->bi_acpi_rsdp); 2275 DBG(bi->bi_smbios); 2276 DBG(bi->bi_uefi_arch); 2277 DBG(bi->bi_uefi_systab); 2278 2279 if (bi->bi_uefi_systab && prom_debug) { 2280 if (bi->bi_uefi_arch == XBI_UEFI_ARCH_64) { 2281 print_efi64((EFI_SYSTEM_TABLE64 *)(uintptr_t) 2282 bi->bi_uefi_systab); 2283 } else { 2284 print_efi32((EFI_SYSTEM_TABLE32 *)(uintptr_t) 2285 bi->bi_uefi_systab); 2286 } 2287 } 2288 #endif 2289 2290 /* 2291 * Need correct target_kernel_text value 2292 */ 2293 #if defined(_BOOT_TARGET_amd64) 2294 target_kernel_text = KERNEL_TEXT_amd64; 2295 #elif defined(__xpv) 2296 target_kernel_text = KERNEL_TEXT_i386_xpv; 2297 #else 2298 target_kernel_text = KERNEL_TEXT_i386; 2299 #endif 2300 DBG(target_kernel_text); 2301 2302 #if defined(__xpv) 2303 2304 /* 2305 * XXPV Derive this stuff from CPUID / what the hypervisor has enabled 2306 */ 2307 2308 #if defined(_BOOT_TARGET_amd64) 2309 /* 2310 * 64-bit hypervisor. 2311 */ 2312 amd64_support = 1; 2313 pae_support = 1; 2314 2315 #else /* _BOOT_TARGET_amd64 */ 2316 2317 /* 2318 * See if we are running on a PAE Hypervisor 2319 */ 2320 { 2321 xen_capabilities_info_t caps; 2322 2323 if (HYPERVISOR_xen_version(XENVER_capabilities, &caps) != 0) 2324 dboot_panic("HYPERVISOR_xen_version(caps) failed"); 2325 caps[sizeof (caps) - 1] = 0; 2326 if (prom_debug) 2327 dboot_printf("xen capabilities %s\n", caps); 2328 if (strstr(caps, "x86_32p") != NULL) 2329 pae_support = 1; 2330 } 2331 2332 #endif /* _BOOT_TARGET_amd64 */ 2333 { 2334 xen_platform_parameters_t p; 2335 2336 if (HYPERVISOR_xen_version(XENVER_platform_parameters, &p) != 0) 2337 dboot_panic("HYPERVISOR_xen_version(parms) failed"); 2338 DBG(p.virt_start); 2339 mfn_to_pfn_mapping = (pfn_t *)(xen_virt_start = p.virt_start); 2340 } 2341 2342 /* 2343 * The hypervisor loads stuff starting at 1Gig 2344 */ 2345 mfn_base = ONE_GIG; 2346 DBG(mfn_base); 2347 2348 /* 2349 * enable writable page table mode for the hypervisor 2350 */ 2351 if (HYPERVISOR_vm_assist(VMASST_CMD_enable, 2352 VMASST_TYPE_writable_pagetables) < 0) 2353 dboot_panic("HYPERVISOR_vm_assist(writable_pagetables) failed"); 2354 2355 /* 2356 * check for NX support 2357 */ 2358 if (pae_support) { 2359 uint32_t eax = 0x80000000; 2360 uint32_t edx = get_cpuid_edx(&eax); 2361 2362 if (eax >= 0x80000001) { 2363 eax = 0x80000001; 2364 edx = get_cpuid_edx(&eax); 2365 if (edx & CPUID_AMD_EDX_NX) 2366 NX_support = 1; 2367 } 2368 } 2369 2370 /* 2371 * check for PAT support 2372 */ 2373 { 2374 uint32_t eax = 1; 2375 uint32_t edx = get_cpuid_edx(&eax); 2376 2377 if (edx & CPUID_INTC_EDX_PAT) 2378 PAT_support = 1; 2379 } 2380 #if !defined(_BOOT_TARGET_amd64) 2381 2382 /* 2383 * The 32-bit hypervisor uses segmentation to protect itself from 2384 * guests. This means when a guest attempts to install a flat 4GB 2385 * code or data descriptor the 32-bit hypervisor will protect itself 2386 * by silently shrinking the segment such that if the guest attempts 2387 * any access where the hypervisor lives a #gp fault is generated. 2388 * The problem is that some applications expect a full 4GB flat 2389 * segment for their current thread pointer and will use negative 2390 * offset segment wrap around to access data. TLS support in linux 2391 * brand is one example of this. 2392 * 2393 * The 32-bit hypervisor can catch the #gp fault in these cases 2394 * and emulate the access without passing the #gp fault to the guest 2395 * but only if VMASST_TYPE_4gb_segments is explicitly turned on. 2396 * Seems like this should have been the default. 2397 * Either way, we want the hypervisor -- and not Solaris -- to deal 2398 * to deal with emulating these accesses. 2399 */ 2400 if (HYPERVISOR_vm_assist(VMASST_CMD_enable, 2401 VMASST_TYPE_4gb_segments) < 0) 2402 dboot_panic("HYPERVISOR_vm_assist(4gb_segments) failed"); 2403 #endif /* !_BOOT_TARGET_amd64 */ 2404 2405 #else /* __xpv */ 2406 2407 /* 2408 * use cpuid to enable MMU features 2409 */ 2410 if (have_cpuid()) { 2411 uint32_t eax, edx; 2412 2413 eax = 1; 2414 edx = get_cpuid_edx(&eax); 2415 if (edx & CPUID_INTC_EDX_PSE) 2416 largepage_support = 1; 2417 if (edx & CPUID_INTC_EDX_PGE) 2418 pge_support = 1; 2419 if (edx & CPUID_INTC_EDX_PAE) 2420 pae_support = 1; 2421 if (edx & CPUID_INTC_EDX_PAT) 2422 PAT_support = 1; 2423 2424 eax = 0x80000000; 2425 edx = get_cpuid_edx(&eax); 2426 if (eax >= 0x80000001) { 2427 eax = 0x80000001; 2428 edx = get_cpuid_edx(&eax); 2429 if (edx & CPUID_AMD_EDX_LM) 2430 amd64_support = 1; 2431 if (edx & CPUID_AMD_EDX_NX) 2432 NX_support = 1; 2433 } 2434 } else { 2435 dboot_printf("cpuid not supported\n"); 2436 } 2437 #endif /* __xpv */ 2438 2439 2440 #if defined(_BOOT_TARGET_amd64) 2441 if (amd64_support == 0) 2442 dboot_panic("long mode not supported, rebooting"); 2443 else if (pae_support == 0) 2444 dboot_panic("long mode, but no PAE; rebooting"); 2445 #else 2446 /* 2447 * Allow the command line to over-ride use of PAE for 32 bit. 2448 */ 2449 if (strstr(cmdline, "disablePAE=true") != NULL) { 2450 pae_support = 0; 2451 NX_support = 0; 2452 amd64_support = 0; 2453 } 2454 #endif 2455 2456 /* 2457 * initialize the simple memory allocator 2458 */ 2459 init_mem_alloc(); 2460 2461 #if !defined(__xpv) && !defined(_BOOT_TARGET_amd64) 2462 /* 2463 * disable PAE on 32 bit h/w w/o NX and < 4Gig of memory 2464 */ 2465 if (max_mem < FOUR_GIG && NX_support == 0) 2466 pae_support = 0; 2467 #endif 2468 2469 /* 2470 * configure mmu information 2471 */ 2472 if (pae_support) { 2473 shift_amt = shift_amt_pae; 2474 ptes_per_table = 512; 2475 pte_size = 8; 2476 lpagesize = TWO_MEG; 2477 #if defined(_BOOT_TARGET_amd64) 2478 top_level = 3; 2479 #else 2480 top_level = 2; 2481 #endif 2482 } else { 2483 pae_support = 0; 2484 NX_support = 0; 2485 shift_amt = shift_amt_nopae; 2486 ptes_per_table = 1024; 2487 pte_size = 4; 2488 lpagesize = FOUR_MEG; 2489 top_level = 1; 2490 } 2491 2492 DBG(PAT_support); 2493 DBG(pge_support); 2494 DBG(NX_support); 2495 DBG(largepage_support); 2496 DBG(amd64_support); 2497 DBG(top_level); 2498 DBG(pte_size); 2499 DBG(ptes_per_table); 2500 DBG(lpagesize); 2501 2502 #if defined(__xpv) 2503 ktext_phys = ONE_GIG; /* from UNIX Mapfile */ 2504 #else 2505 ktext_phys = FOUR_MEG; /* from UNIX Mapfile */ 2506 #endif 2507 2508 #if !defined(__xpv) && defined(_BOOT_TARGET_amd64) 2509 /* 2510 * For grub, copy kernel bits from the ELF64 file to final place. 2511 */ 2512 DBG_MSG("\nAllocating nucleus pages.\n"); 2513 ktext_phys = (uintptr_t)do_mem_alloc(ksize, FOUR_MEG); 2514 2515 if (ktext_phys == 0) 2516 dboot_panic("failed to allocate aligned kernel memory"); 2517 DBG(load_addr); 2518 if (dboot_elfload64(load_addr) != 0) 2519 dboot_panic("failed to parse kernel ELF image, rebooting"); 2520 #endif 2521 2522 DBG(ktext_phys); 2523 2524 /* 2525 * Allocate page tables. 2526 */ 2527 build_page_tables(); 2528 2529 /* 2530 * return to assembly code to switch to running kernel 2531 */ 2532 entry_addr_low = (uint32_t)target_kernel_text; 2533 DBG(entry_addr_low); 2534 bi->bi_use_largepage = largepage_support; 2535 bi->bi_use_pae = pae_support; 2536 bi->bi_use_pge = pge_support; 2537 bi->bi_use_nx = NX_support; 2538 2539 #if defined(__xpv) 2540 2541 bi->bi_next_paddr = next_avail_addr - mfn_base; 2542 DBG(bi->bi_next_paddr); 2543 bi->bi_next_vaddr = (native_ptr_t)(uintptr_t)next_avail_addr; 2544 DBG(bi->bi_next_vaddr); 2545 2546 /* 2547 * unmap unused pages in start area to make them available for DMA 2548 */ 2549 while (next_avail_addr < scratch_end) { 2550 (void) HYPERVISOR_update_va_mapping(next_avail_addr, 2551 0, UVMF_INVLPG | UVMF_LOCAL); 2552 next_avail_addr += MMU_PAGESIZE; 2553 } 2554 2555 bi->bi_xen_start_info = (native_ptr_t)(uintptr_t)xen_info; 2556 DBG((uintptr_t)HYPERVISOR_shared_info); 2557 bi->bi_shared_info = (native_ptr_t)HYPERVISOR_shared_info; 2558 bi->bi_top_page_table = (uintptr_t)top_page_table - mfn_base; 2559 2560 #else /* __xpv */ 2561 2562 bi->bi_next_paddr = next_avail_addr; 2563 DBG(bi->bi_next_paddr); 2564 bi->bi_next_vaddr = (native_ptr_t)(uintptr_t)next_avail_addr; 2565 DBG(bi->bi_next_vaddr); 2566 bi->bi_mb_version = multiboot_version; 2567 2568 switch (multiboot_version) { 2569 case 1: 2570 bi->bi_mb_info = (native_ptr_t)(uintptr_t)mb_info; 2571 break; 2572 case 2: 2573 bi->bi_mb_info = (native_ptr_t)(uintptr_t)mb2_info; 2574 break; 2575 default: 2576 dboot_panic("Unknown multiboot version: %d\n", 2577 multiboot_version); 2578 break; 2579 } 2580 bi->bi_top_page_table = (uintptr_t)top_page_table; 2581 2582 #endif /* __xpv */ 2583 2584 bi->bi_kseg_size = FOUR_MEG; 2585 DBG(bi->bi_kseg_size); 2586 2587 #ifndef __xpv 2588 if (map_debug) 2589 dump_tables(); 2590 #endif 2591 2592 DBG_MSG("\n\n*** DBOOT DONE -- back to asm to jump to kernel\n\n"); 2593 2594 #ifndef __xpv 2595 /* Update boot info with FB data */ 2596 fb->cursor.origin.x = fb_info.cursor.origin.x; 2597 fb->cursor.origin.y = fb_info.cursor.origin.y; 2598 fb->cursor.pos.x = fb_info.cursor.pos.x; 2599 fb->cursor.pos.y = fb_info.cursor.pos.y; 2600 fb->cursor.visible = fb_info.cursor.visible; 2601 #endif 2602 } 2603