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 nacpitagp = (multiboot_tag_new_acpi_t *) 1606 dboot_multiboot2_find_tag(mb2_info, 1607 MULTIBOOT_TAG_TYPE_ACPI_NEW); 1608 oacpitagp = (multiboot_tag_old_acpi_t *) 1609 dboot_multiboot2_find_tag(mb2_info, 1610 MULTIBOOT_TAG_TYPE_ACPI_OLD); 1611 1612 if (nacpitagp != NULL) { 1613 bi->bi_acpi_rsdp = (native_ptr_t)(uintptr_t) 1614 &nacpitagp->mb_rsdp[0]; 1615 } else if (oacpitagp != NULL) { 1616 bi->bi_acpi_rsdp = (native_ptr_t)(uintptr_t) 1617 &oacpitagp->mb_rsdp[0]; 1618 } else { 1619 bi->bi_acpi_rsdp = NULL; 1620 } 1621 } 1622 #endif /* !__xpv */ 1623 1624 /* 1625 * Simple memory allocator, allocates aligned physical memory. 1626 * Note that startup_kernel() only allocates memory, never frees. 1627 * Memory usage just grows in an upward direction. 1628 */ 1629 static void * 1630 do_mem_alloc(uint32_t size, uint32_t align) 1631 { 1632 uint_t i; 1633 uint64_t best; 1634 uint64_t start; 1635 uint64_t end; 1636 1637 /* 1638 * make sure size is a multiple of pagesize 1639 */ 1640 size = RNDUP(size, MMU_PAGESIZE); 1641 next_avail_addr = RNDUP(next_avail_addr, align); 1642 1643 /* 1644 * XXPV fixme joe 1645 * 1646 * a really large bootarchive that causes you to run out of memory 1647 * may cause this to blow up 1648 */ 1649 /* LINTED E_UNEXPECTED_UINT_PROMOTION */ 1650 best = (uint64_t)-size; 1651 for (i = 0; i < memlists_used; ++i) { 1652 start = memlists[i].addr; 1653 #if defined(__xpv) 1654 start += mfn_base; 1655 #endif 1656 end = start + memlists[i].size; 1657 1658 /* 1659 * did we find the desired address? 1660 */ 1661 if (start <= next_avail_addr && next_avail_addr + size <= end) { 1662 best = next_avail_addr; 1663 goto done; 1664 } 1665 1666 /* 1667 * if not is this address the best so far? 1668 */ 1669 if (start > next_avail_addr && start < best && 1670 RNDUP(start, align) + size <= end) 1671 best = RNDUP(start, align); 1672 } 1673 1674 /* 1675 * We didn't find exactly the address we wanted, due to going off the 1676 * end of a memory region. Return the best found memory address. 1677 */ 1678 done: 1679 next_avail_addr = best + size; 1680 #if defined(__xpv) 1681 if (next_avail_addr > scratch_end) 1682 dboot_panic("Out of mem next_avail: 0x%lx, scratch_end: " 1683 "0x%lx", (ulong_t)next_avail_addr, 1684 (ulong_t)scratch_end); 1685 #endif 1686 (void) memset((void *)(uintptr_t)best, 0, size); 1687 return ((void *)(uintptr_t)best); 1688 } 1689 1690 void * 1691 mem_alloc(uint32_t size) 1692 { 1693 return (do_mem_alloc(size, MMU_PAGESIZE)); 1694 } 1695 1696 1697 /* 1698 * Build page tables to map all of memory used so far as well as the kernel. 1699 */ 1700 static void 1701 build_page_tables(void) 1702 { 1703 uint32_t psize; 1704 uint32_t level; 1705 uint32_t off; 1706 uint64_t start; 1707 #if !defined(__xpv) 1708 uint32_t i; 1709 uint64_t end; 1710 #endif /* __xpv */ 1711 1712 /* 1713 * If we're on metal, we need to create the top level pagetable. 1714 */ 1715 #if defined(__xpv) 1716 top_page_table = (paddr_t)(uintptr_t)xen_info->pt_base; 1717 #else /* __xpv */ 1718 top_page_table = (paddr_t)(uintptr_t)mem_alloc(MMU_PAGESIZE); 1719 #endif /* __xpv */ 1720 DBG((uintptr_t)top_page_table); 1721 1722 /* 1723 * Determine if we'll use large mappings for kernel, then map it. 1724 */ 1725 if (largepage_support) { 1726 psize = lpagesize; 1727 level = 1; 1728 } else { 1729 psize = MMU_PAGESIZE; 1730 level = 0; 1731 } 1732 1733 DBG_MSG("Mapping kernel\n"); 1734 DBG(ktext_phys); 1735 DBG(target_kernel_text); 1736 DBG(ksize); 1737 DBG(psize); 1738 for (off = 0; off < ksize; off += psize) 1739 map_pa_at_va(ktext_phys + off, target_kernel_text + off, level); 1740 1741 /* 1742 * The kernel will need a 1 page window to work with page tables 1743 */ 1744 bi->bi_pt_window = (uintptr_t)mem_alloc(MMU_PAGESIZE); 1745 DBG(bi->bi_pt_window); 1746 bi->bi_pte_to_pt_window = 1747 (uintptr_t)find_pte(bi->bi_pt_window, NULL, 0, 0); 1748 DBG(bi->bi_pte_to_pt_window); 1749 1750 #if defined(__xpv) 1751 if (!DOMAIN_IS_INITDOMAIN(xen_info)) { 1752 /* If this is a domU we're done. */ 1753 DBG_MSG("\nPage tables constructed\n"); 1754 return; 1755 } 1756 #endif /* __xpv */ 1757 1758 /* 1759 * We need 1:1 mappings for the lower 1M of memory to access 1760 * BIOS tables used by a couple of drivers during boot. 1761 * 1762 * The following code works because our simple memory allocator 1763 * only grows usage in an upwards direction. 1764 * 1765 * Note that by this point in boot some mappings for low memory 1766 * may already exist because we've already accessed device in low 1767 * memory. (Specifically the video frame buffer and keyboard 1768 * status ports.) If we're booting on raw hardware then GRUB 1769 * created these mappings for us. If we're booting under a 1770 * hypervisor then we went ahead and remapped these devices into 1771 * memory allocated within dboot itself. 1772 */ 1773 if (map_debug) 1774 dboot_printf("1:1 map pa=0..1Meg\n"); 1775 for (start = 0; start < 1024 * 1024; start += MMU_PAGESIZE) { 1776 #if defined(__xpv) 1777 map_ma_at_va(start, start, 0); 1778 #else /* __xpv */ 1779 map_pa_at_va(start, start, 0); 1780 #endif /* __xpv */ 1781 } 1782 1783 #if !defined(__xpv) 1784 for (i = 0; i < memlists_used; ++i) { 1785 start = memlists[i].addr; 1786 1787 end = start + memlists[i].size; 1788 1789 if (map_debug) 1790 dboot_printf("1:1 map pa=%" PRIx64 "..%" PRIx64 "\n", 1791 start, end); 1792 while (start < end && start < next_avail_addr) { 1793 map_pa_at_va(start, start, 0); 1794 start += MMU_PAGESIZE; 1795 } 1796 } 1797 #endif /* !__xpv */ 1798 1799 DBG_MSG("\nPage tables constructed\n"); 1800 } 1801 1802 #define NO_MULTIBOOT \ 1803 "multiboot is no longer used to boot the Solaris Operating System.\n\ 1804 The grub entry should be changed to:\n\ 1805 kernel$ /platform/i86pc/kernel/$ISADIR/unix\n\ 1806 module$ /platform/i86pc/$ISADIR/boot_archive\n\ 1807 See http://illumos.org/msg/SUNOS-8000-AK for details.\n" 1808 1809 static void 1810 dboot_init_xboot_consinfo(void) 1811 { 1812 uintptr_t addr; 1813 /* 1814 * boot info must be 16 byte aligned for 64 bit kernel ABI 1815 */ 1816 addr = (uintptr_t)boot_info; 1817 addr = (addr + 0xf) & ~0xf; 1818 bi = (struct xboot_info *)addr; 1819 1820 #if !defined(__xpv) 1821 switch (multiboot_version) { 1822 case 1: 1823 dboot_multiboot1_xboot_consinfo(); 1824 break; 1825 case 2: 1826 dboot_multiboot2_xboot_consinfo(); 1827 break; 1828 default: 1829 dboot_panic("Unknown multiboot version: %d\n", 1830 multiboot_version); 1831 break; 1832 } 1833 /* 1834 * Lookup environment module for the console. Complete module list 1835 * will be built after console setup. 1836 */ 1837 dboot_find_env(); 1838 #endif 1839 } 1840 1841 /* 1842 * Set up basic data from the boot loader. 1843 * The load_addr is part of AOUT kludge setup in dboot_grub.s, to support 1844 * 32-bit dboot code setup used to set up and start 64-bit kernel. 1845 * AOUT kludge does allow 32-bit boot loader, such as grub1, to load and 1846 * start 64-bit illumos kernel. 1847 */ 1848 static void 1849 dboot_loader_init(void) 1850 { 1851 #if !defined(__xpv) 1852 mb_info = NULL; 1853 mb2_info = NULL; 1854 1855 switch (mb_magic) { 1856 case MB_BOOTLOADER_MAGIC: 1857 multiboot_version = 1; 1858 mb_info = (multiboot_info_t *)(uintptr_t)mb_addr; 1859 #if defined(_BOOT_TARGET_amd64) 1860 load_addr = mb_header.load_addr; 1861 #endif 1862 break; 1863 1864 case MULTIBOOT2_BOOTLOADER_MAGIC: 1865 multiboot_version = 2; 1866 mb2_info = (multiboot2_info_header_t *)(uintptr_t)mb_addr; 1867 mb2_mmap_tagp = dboot_multiboot2_get_mmap_tagp(mb2_info); 1868 #if defined(_BOOT_TARGET_amd64) 1869 load_addr = mb2_load_addr; 1870 #endif 1871 break; 1872 1873 default: 1874 dboot_panic("Unknown bootloader magic: 0x%x\n", mb_magic); 1875 break; 1876 } 1877 #endif /* !defined(__xpv) */ 1878 } 1879 1880 /* Extract the kernel command line from [multi]boot information. */ 1881 static char * 1882 dboot_loader_cmdline(void) 1883 { 1884 char *line = NULL; 1885 1886 #if defined(__xpv) 1887 line = (char *)xen_info->cmd_line; 1888 #else /* __xpv */ 1889 1890 switch (multiboot_version) { 1891 case 1: 1892 if (mb_info->flags & MB_INFO_CMDLINE) 1893 line = (char *)mb_info->cmdline; 1894 break; 1895 1896 case 2: 1897 line = dboot_multiboot2_cmdline(mb2_info); 1898 break; 1899 1900 default: 1901 dboot_panic("Unknown multiboot version: %d\n", 1902 multiboot_version); 1903 break; 1904 } 1905 1906 #endif /* __xpv */ 1907 1908 /* 1909 * Make sure we have valid pointer so the string operations 1910 * will not crash us. 1911 */ 1912 if (line == NULL) 1913 line = ""; 1914 1915 return (line); 1916 } 1917 1918 static char * 1919 dboot_loader_name(void) 1920 { 1921 #if defined(__xpv) 1922 return (NULL); 1923 #else /* __xpv */ 1924 multiboot_tag_string_t *tag; 1925 1926 switch (multiboot_version) { 1927 case 1: 1928 return ((char *)mb_info->boot_loader_name); 1929 1930 case 2: 1931 tag = dboot_multiboot2_find_tag(mb2_info, 1932 MULTIBOOT_TAG_TYPE_BOOT_LOADER_NAME); 1933 return (tag->mb_string); 1934 default: 1935 dboot_panic("Unknown multiboot version: %d\n", 1936 multiboot_version); 1937 break; 1938 } 1939 1940 return (NULL); 1941 #endif /* __xpv */ 1942 } 1943 /* 1944 * startup_kernel has a pretty simple job. It builds pagetables which reflect 1945 * 1:1 mappings for all memory in use. It then also adds mappings for 1946 * the kernel nucleus at virtual address of target_kernel_text using large page 1947 * mappings. The page table pages are also accessible at 1:1 mapped 1948 * virtual addresses. 1949 */ 1950 /*ARGSUSED*/ 1951 void 1952 startup_kernel(void) 1953 { 1954 char *cmdline; 1955 char *bootloader; 1956 #if defined(__xpv) 1957 physdev_set_iopl_t set_iopl; 1958 #endif /* __xpv */ 1959 1960 dboot_loader_init(); 1961 /* 1962 * At this point we are executing in a 32 bit real mode. 1963 */ 1964 1965 bootloader = dboot_loader_name(); 1966 cmdline = dboot_loader_cmdline(); 1967 1968 #if defined(__xpv) 1969 /* 1970 * For dom0, before we initialize the console subsystem we'll 1971 * need to enable io operations, so set I/O priveldge level to 1. 1972 */ 1973 if (DOMAIN_IS_INITDOMAIN(xen_info)) { 1974 set_iopl.iopl = 1; 1975 (void) HYPERVISOR_physdev_op(PHYSDEVOP_set_iopl, &set_iopl); 1976 } 1977 #endif /* __xpv */ 1978 1979 dboot_init_xboot_consinfo(); 1980 bi->bi_cmdline = (native_ptr_t)(uintptr_t)cmdline; 1981 bcons_init(bi); 1982 1983 prom_debug = (find_boot_prop("prom_debug") != NULL); 1984 map_debug = (find_boot_prop("map_debug") != NULL); 1985 1986 #if !defined(__xpv) 1987 dboot_multiboot_get_fwtables(); 1988 #endif 1989 DBG_MSG("\n\nillumos prekernel set: "); 1990 DBG_MSG(cmdline); 1991 DBG_MSG("\n"); 1992 1993 if (bootloader != NULL && prom_debug) { 1994 dboot_printf("Kernel loaded by: %s\n", bootloader); 1995 #if !defined(__xpv) 1996 dboot_printf("Using multiboot %d boot protocol.\n", 1997 multiboot_version); 1998 #endif 1999 } 2000 2001 if (strstr(cmdline, "multiboot") != NULL) { 2002 dboot_panic(NO_MULTIBOOT); 2003 } 2004 2005 DBG((uintptr_t)bi); 2006 #if !defined(__xpv) 2007 DBG((uintptr_t)mb_info); 2008 DBG((uintptr_t)mb2_info); 2009 if (mb2_info != NULL) 2010 DBG(mb2_info->mbi_total_size); 2011 DBG(bi->bi_acpi_rsdp); 2012 #endif 2013 2014 /* 2015 * Need correct target_kernel_text value 2016 */ 2017 #if defined(_BOOT_TARGET_amd64) 2018 target_kernel_text = KERNEL_TEXT_amd64; 2019 #elif defined(__xpv) 2020 target_kernel_text = KERNEL_TEXT_i386_xpv; 2021 #else 2022 target_kernel_text = KERNEL_TEXT_i386; 2023 #endif 2024 DBG(target_kernel_text); 2025 2026 #if defined(__xpv) 2027 2028 /* 2029 * XXPV Derive this stuff from CPUID / what the hypervisor has enabled 2030 */ 2031 2032 #if defined(_BOOT_TARGET_amd64) 2033 /* 2034 * 64-bit hypervisor. 2035 */ 2036 amd64_support = 1; 2037 pae_support = 1; 2038 2039 #else /* _BOOT_TARGET_amd64 */ 2040 2041 /* 2042 * See if we are running on a PAE Hypervisor 2043 */ 2044 { 2045 xen_capabilities_info_t caps; 2046 2047 if (HYPERVISOR_xen_version(XENVER_capabilities, &caps) != 0) 2048 dboot_panic("HYPERVISOR_xen_version(caps) failed"); 2049 caps[sizeof (caps) - 1] = 0; 2050 if (prom_debug) 2051 dboot_printf("xen capabilities %s\n", caps); 2052 if (strstr(caps, "x86_32p") != NULL) 2053 pae_support = 1; 2054 } 2055 2056 #endif /* _BOOT_TARGET_amd64 */ 2057 { 2058 xen_platform_parameters_t p; 2059 2060 if (HYPERVISOR_xen_version(XENVER_platform_parameters, &p) != 0) 2061 dboot_panic("HYPERVISOR_xen_version(parms) failed"); 2062 DBG(p.virt_start); 2063 mfn_to_pfn_mapping = (pfn_t *)(xen_virt_start = p.virt_start); 2064 } 2065 2066 /* 2067 * The hypervisor loads stuff starting at 1Gig 2068 */ 2069 mfn_base = ONE_GIG; 2070 DBG(mfn_base); 2071 2072 /* 2073 * enable writable page table mode for the hypervisor 2074 */ 2075 if (HYPERVISOR_vm_assist(VMASST_CMD_enable, 2076 VMASST_TYPE_writable_pagetables) < 0) 2077 dboot_panic("HYPERVISOR_vm_assist(writable_pagetables) failed"); 2078 2079 /* 2080 * check for NX support 2081 */ 2082 if (pae_support) { 2083 uint32_t eax = 0x80000000; 2084 uint32_t edx = get_cpuid_edx(&eax); 2085 2086 if (eax >= 0x80000001) { 2087 eax = 0x80000001; 2088 edx = get_cpuid_edx(&eax); 2089 if (edx & CPUID_AMD_EDX_NX) 2090 NX_support = 1; 2091 } 2092 } 2093 2094 #if !defined(_BOOT_TARGET_amd64) 2095 2096 /* 2097 * The 32-bit hypervisor uses segmentation to protect itself from 2098 * guests. This means when a guest attempts to install a flat 4GB 2099 * code or data descriptor the 32-bit hypervisor will protect itself 2100 * by silently shrinking the segment such that if the guest attempts 2101 * any access where the hypervisor lives a #gp fault is generated. 2102 * The problem is that some applications expect a full 4GB flat 2103 * segment for their current thread pointer and will use negative 2104 * offset segment wrap around to access data. TLS support in linux 2105 * brand is one example of this. 2106 * 2107 * The 32-bit hypervisor can catch the #gp fault in these cases 2108 * and emulate the access without passing the #gp fault to the guest 2109 * but only if VMASST_TYPE_4gb_segments is explicitly turned on. 2110 * Seems like this should have been the default. 2111 * Either way, we want the hypervisor -- and not Solaris -- to deal 2112 * to deal with emulating these accesses. 2113 */ 2114 if (HYPERVISOR_vm_assist(VMASST_CMD_enable, 2115 VMASST_TYPE_4gb_segments) < 0) 2116 dboot_panic("HYPERVISOR_vm_assist(4gb_segments) failed"); 2117 #endif /* !_BOOT_TARGET_amd64 */ 2118 2119 #else /* __xpv */ 2120 2121 /* 2122 * use cpuid to enable MMU features 2123 */ 2124 if (have_cpuid()) { 2125 uint32_t eax, edx; 2126 2127 eax = 1; 2128 edx = get_cpuid_edx(&eax); 2129 if (edx & CPUID_INTC_EDX_PSE) 2130 largepage_support = 1; 2131 if (edx & CPUID_INTC_EDX_PGE) 2132 pge_support = 1; 2133 if (edx & CPUID_INTC_EDX_PAE) 2134 pae_support = 1; 2135 2136 eax = 0x80000000; 2137 edx = get_cpuid_edx(&eax); 2138 if (eax >= 0x80000001) { 2139 eax = 0x80000001; 2140 edx = get_cpuid_edx(&eax); 2141 if (edx & CPUID_AMD_EDX_LM) 2142 amd64_support = 1; 2143 if (edx & CPUID_AMD_EDX_NX) 2144 NX_support = 1; 2145 } 2146 } else { 2147 dboot_printf("cpuid not supported\n"); 2148 } 2149 #endif /* __xpv */ 2150 2151 2152 #if defined(_BOOT_TARGET_amd64) 2153 if (amd64_support == 0) 2154 dboot_panic("long mode not supported, rebooting"); 2155 else if (pae_support == 0) 2156 dboot_panic("long mode, but no PAE; rebooting"); 2157 #else 2158 /* 2159 * Allow the command line to over-ride use of PAE for 32 bit. 2160 */ 2161 if (strstr(cmdline, "disablePAE=true") != NULL) { 2162 pae_support = 0; 2163 NX_support = 0; 2164 amd64_support = 0; 2165 } 2166 #endif 2167 2168 /* 2169 * initialize the simple memory allocator 2170 */ 2171 init_mem_alloc(); 2172 2173 #if !defined(__xpv) && !defined(_BOOT_TARGET_amd64) 2174 /* 2175 * disable PAE on 32 bit h/w w/o NX and < 4Gig of memory 2176 */ 2177 if (max_mem < FOUR_GIG && NX_support == 0) 2178 pae_support = 0; 2179 #endif 2180 2181 /* 2182 * configure mmu information 2183 */ 2184 if (pae_support) { 2185 shift_amt = shift_amt_pae; 2186 ptes_per_table = 512; 2187 pte_size = 8; 2188 lpagesize = TWO_MEG; 2189 #if defined(_BOOT_TARGET_amd64) 2190 top_level = 3; 2191 #else 2192 top_level = 2; 2193 #endif 2194 } else { 2195 pae_support = 0; 2196 NX_support = 0; 2197 shift_amt = shift_amt_nopae; 2198 ptes_per_table = 1024; 2199 pte_size = 4; 2200 lpagesize = FOUR_MEG; 2201 top_level = 1; 2202 } 2203 2204 DBG(pge_support); 2205 DBG(NX_support); 2206 DBG(largepage_support); 2207 DBG(amd64_support); 2208 DBG(top_level); 2209 DBG(pte_size); 2210 DBG(ptes_per_table); 2211 DBG(lpagesize); 2212 2213 #if defined(__xpv) 2214 ktext_phys = ONE_GIG; /* from UNIX Mapfile */ 2215 #else 2216 ktext_phys = FOUR_MEG; /* from UNIX Mapfile */ 2217 #endif 2218 2219 #if !defined(__xpv) && defined(_BOOT_TARGET_amd64) 2220 /* 2221 * For grub, copy kernel bits from the ELF64 file to final place. 2222 */ 2223 DBG_MSG("\nAllocating nucleus pages.\n"); 2224 ktext_phys = (uintptr_t)do_mem_alloc(ksize, FOUR_MEG); 2225 if (ktext_phys == 0) 2226 dboot_panic("failed to allocate aligned kernel memory"); 2227 DBG(load_addr); 2228 if (dboot_elfload64(load_addr) != 0) 2229 dboot_panic("failed to parse kernel ELF image, rebooting"); 2230 #endif 2231 2232 DBG(ktext_phys); 2233 2234 /* 2235 * Allocate page tables. 2236 */ 2237 build_page_tables(); 2238 2239 /* 2240 * return to assembly code to switch to running kernel 2241 */ 2242 entry_addr_low = (uint32_t)target_kernel_text; 2243 DBG(entry_addr_low); 2244 bi->bi_use_largepage = largepage_support; 2245 bi->bi_use_pae = pae_support; 2246 bi->bi_use_pge = pge_support; 2247 bi->bi_use_nx = NX_support; 2248 2249 #if defined(__xpv) 2250 2251 bi->bi_next_paddr = next_avail_addr - mfn_base; 2252 DBG(bi->bi_next_paddr); 2253 bi->bi_next_vaddr = (native_ptr_t)next_avail_addr; 2254 DBG(bi->bi_next_vaddr); 2255 2256 /* 2257 * unmap unused pages in start area to make them available for DMA 2258 */ 2259 while (next_avail_addr < scratch_end) { 2260 (void) HYPERVISOR_update_va_mapping(next_avail_addr, 2261 0, UVMF_INVLPG | UVMF_LOCAL); 2262 next_avail_addr += MMU_PAGESIZE; 2263 } 2264 2265 bi->bi_xen_start_info = (uintptr_t)xen_info; 2266 DBG((uintptr_t)HYPERVISOR_shared_info); 2267 bi->bi_shared_info = (native_ptr_t)HYPERVISOR_shared_info; 2268 bi->bi_top_page_table = (uintptr_t)top_page_table - mfn_base; 2269 2270 #else /* __xpv */ 2271 2272 bi->bi_next_paddr = next_avail_addr; 2273 DBG(bi->bi_next_paddr); 2274 bi->bi_next_vaddr = (uintptr_t)next_avail_addr; 2275 DBG(bi->bi_next_vaddr); 2276 bi->bi_mb_version = multiboot_version; 2277 2278 switch (multiboot_version) { 2279 case 1: 2280 bi->bi_mb_info = (uintptr_t)mb_info; 2281 break; 2282 case 2: 2283 bi->bi_mb_info = (uintptr_t)mb2_info; 2284 break; 2285 default: 2286 dboot_panic("Unknown multiboot version: %d\n", 2287 multiboot_version); 2288 break; 2289 } 2290 bi->bi_top_page_table = (uintptr_t)top_page_table; 2291 2292 #endif /* __xpv */ 2293 2294 bi->bi_kseg_size = FOUR_MEG; 2295 DBG(bi->bi_kseg_size); 2296 2297 #ifndef __xpv 2298 if (map_debug) 2299 dump_tables(); 2300 #endif 2301 2302 DBG_MSG("\n\n*** DBOOT DONE -- back to asm to jump to kernel\n\n"); 2303 } 2304