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