1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Copyright (C) 1995 Linus Torvalds 4 * 5 * Support of BIGMEM added by Gerhard Wichert, Siemens AG, July 1999 6 * 7 * Memory region support 8 * David Parsons <orc@pell.chi.il.us>, July-August 1999 9 * 10 * Added E820 sanitization routine (removes overlapping memory regions); 11 * Brian Moyle <bmoyle@mvista.com>, February 2001 12 * 13 * Moved CPU detection code to cpu/${cpu}.c 14 * Patrick Mochel <mochel@osdl.org>, March 2002 15 * 16 * Provisions for empty E820 memory regions (reported by certain BIOSes). 17 * Alex Achenbach <xela@slit.de>, December 2002. 18 * 19 */ 20 21 /* 22 * This file handles the architecture-dependent parts of initialization 23 */ 24 25 #include <linux/sched.h> 26 #include <linux/mm.h> 27 #include <linux/mmzone.h> 28 #include <linux/screen_info.h> 29 #include <linux/ioport.h> 30 #include <linux/acpi.h> 31 #include <linux/sfi.h> 32 #include <linux/apm_bios.h> 33 #include <linux/initrd.h> 34 #include <linux/memblock.h> 35 #include <linux/seq_file.h> 36 #include <linux/console.h> 37 #include <linux/root_dev.h> 38 #include <linux/highmem.h> 39 #include <linux/export.h> 40 #include <linux/efi.h> 41 #include <linux/init.h> 42 #include <linux/edd.h> 43 #include <linux/iscsi_ibft.h> 44 #include <linux/nodemask.h> 45 #include <linux/kexec.h> 46 #include <linux/dmi.h> 47 #include <linux/pfn.h> 48 #include <linux/pci.h> 49 #include <asm/pci-direct.h> 50 #include <linux/init_ohci1394_dma.h> 51 #include <linux/kvm_para.h> 52 #include <linux/dma-contiguous.h> 53 #include <xen/xen.h> 54 #include <uapi/linux/mount.h> 55 56 #include <linux/errno.h> 57 #include <linux/kernel.h> 58 #include <linux/stddef.h> 59 #include <linux/unistd.h> 60 #include <linux/ptrace.h> 61 #include <linux/user.h> 62 #include <linux/delay.h> 63 64 #include <linux/kallsyms.h> 65 #include <linux/cpufreq.h> 66 #include <linux/dma-mapping.h> 67 #include <linux/ctype.h> 68 #include <linux/uaccess.h> 69 70 #include <linux/percpu.h> 71 #include <linux/crash_dump.h> 72 #include <linux/tboot.h> 73 #include <linux/jiffies.h> 74 #include <linux/mem_encrypt.h> 75 #include <linux/sizes.h> 76 77 #include <linux/usb/xhci-dbgp.h> 78 #include <video/edid.h> 79 80 #include <asm/mtrr.h> 81 #include <asm/apic.h> 82 #include <asm/realmode.h> 83 #include <asm/e820/api.h> 84 #include <asm/mpspec.h> 85 #include <asm/setup.h> 86 #include <asm/efi.h> 87 #include <asm/timer.h> 88 #include <asm/i8259.h> 89 #include <asm/sections.h> 90 #include <asm/io_apic.h> 91 #include <asm/ist.h> 92 #include <asm/setup_arch.h> 93 #include <asm/bios_ebda.h> 94 #include <asm/cacheflush.h> 95 #include <asm/processor.h> 96 #include <asm/bugs.h> 97 #include <asm/kasan.h> 98 99 #include <asm/vsyscall.h> 100 #include <asm/cpu.h> 101 #include <asm/desc.h> 102 #include <asm/dma.h> 103 #include <asm/iommu.h> 104 #include <asm/gart.h> 105 #include <asm/mmu_context.h> 106 #include <asm/proto.h> 107 108 #include <asm/paravirt.h> 109 #include <asm/hypervisor.h> 110 #include <asm/olpc_ofw.h> 111 112 #include <asm/percpu.h> 113 #include <asm/topology.h> 114 #include <asm/apicdef.h> 115 #include <asm/amd_nb.h> 116 #include <asm/mce.h> 117 #include <asm/alternative.h> 118 #include <asm/prom.h> 119 #include <asm/microcode.h> 120 #include <asm/kaslr.h> 121 #include <asm/unwind.h> 122 123 /* 124 * max_low_pfn_mapped: highest direct mapped pfn under 4GB 125 * max_pfn_mapped: highest direct mapped pfn over 4GB 126 * 127 * The direct mapping only covers E820_TYPE_RAM regions, so the ranges and gaps are 128 * represented by pfn_mapped 129 */ 130 unsigned long max_low_pfn_mapped; 131 unsigned long max_pfn_mapped; 132 133 #ifdef CONFIG_DMI 134 RESERVE_BRK(dmi_alloc, 65536); 135 #endif 136 137 138 static __initdata unsigned long _brk_start = (unsigned long)__brk_base; 139 unsigned long _brk_end = (unsigned long)__brk_base; 140 141 struct boot_params boot_params; 142 143 /* 144 * Machine setup.. 145 */ 146 static struct resource rodata_resource = { 147 .name = "Kernel rodata", 148 .start = 0, 149 .end = 0, 150 .flags = IORESOURCE_BUSY | IORESOURCE_SYSTEM_RAM 151 }; 152 153 static struct resource data_resource = { 154 .name = "Kernel data", 155 .start = 0, 156 .end = 0, 157 .flags = IORESOURCE_BUSY | IORESOURCE_SYSTEM_RAM 158 }; 159 160 static struct resource code_resource = { 161 .name = "Kernel code", 162 .start = 0, 163 .end = 0, 164 .flags = IORESOURCE_BUSY | IORESOURCE_SYSTEM_RAM 165 }; 166 167 static struct resource bss_resource = { 168 .name = "Kernel bss", 169 .start = 0, 170 .end = 0, 171 .flags = IORESOURCE_BUSY | IORESOURCE_SYSTEM_RAM 172 }; 173 174 175 #ifdef CONFIG_X86_32 176 /* cpu data as detected by the assembly code in head_32.S */ 177 struct cpuinfo_x86 new_cpu_data; 178 179 /* common cpu data for all cpus */ 180 struct cpuinfo_x86 boot_cpu_data __read_mostly; 181 EXPORT_SYMBOL(boot_cpu_data); 182 183 unsigned int def_to_bigsmp; 184 185 /* for MCA, but anyone else can use it if they want */ 186 unsigned int machine_id; 187 unsigned int machine_submodel_id; 188 unsigned int BIOS_revision; 189 190 struct apm_info apm_info; 191 EXPORT_SYMBOL(apm_info); 192 193 #if defined(CONFIG_X86_SPEEDSTEP_SMI) || \ 194 defined(CONFIG_X86_SPEEDSTEP_SMI_MODULE) 195 struct ist_info ist_info; 196 EXPORT_SYMBOL(ist_info); 197 #else 198 struct ist_info ist_info; 199 #endif 200 201 #else 202 struct cpuinfo_x86 boot_cpu_data __read_mostly; 203 EXPORT_SYMBOL(boot_cpu_data); 204 #endif 205 206 207 #if !defined(CONFIG_X86_PAE) || defined(CONFIG_X86_64) 208 __visible unsigned long mmu_cr4_features __ro_after_init; 209 #else 210 __visible unsigned long mmu_cr4_features __ro_after_init = X86_CR4_PAE; 211 #endif 212 213 /* Boot loader ID and version as integers, for the benefit of proc_dointvec */ 214 int bootloader_type, bootloader_version; 215 216 /* 217 * Setup options 218 */ 219 struct screen_info screen_info; 220 EXPORT_SYMBOL(screen_info); 221 struct edid_info edid_info; 222 EXPORT_SYMBOL_GPL(edid_info); 223 224 extern int root_mountflags; 225 226 unsigned long saved_video_mode; 227 228 #define RAMDISK_IMAGE_START_MASK 0x07FF 229 #define RAMDISK_PROMPT_FLAG 0x8000 230 #define RAMDISK_LOAD_FLAG 0x4000 231 232 static char __initdata command_line[COMMAND_LINE_SIZE]; 233 #ifdef CONFIG_CMDLINE_BOOL 234 static char __initdata builtin_cmdline[COMMAND_LINE_SIZE] = CONFIG_CMDLINE; 235 #endif 236 237 #if defined(CONFIG_EDD) || defined(CONFIG_EDD_MODULE) 238 struct edd edd; 239 #ifdef CONFIG_EDD_MODULE 240 EXPORT_SYMBOL(edd); 241 #endif 242 /** 243 * copy_edd() - Copy the BIOS EDD information 244 * from boot_params into a safe place. 245 * 246 */ 247 static inline void __init copy_edd(void) 248 { 249 memcpy(edd.mbr_signature, boot_params.edd_mbr_sig_buffer, 250 sizeof(edd.mbr_signature)); 251 memcpy(edd.edd_info, boot_params.eddbuf, sizeof(edd.edd_info)); 252 edd.mbr_signature_nr = boot_params.edd_mbr_sig_buf_entries; 253 edd.edd_info_nr = boot_params.eddbuf_entries; 254 } 255 #else 256 static inline void __init copy_edd(void) 257 { 258 } 259 #endif 260 261 void * __init extend_brk(size_t size, size_t align) 262 { 263 size_t mask = align - 1; 264 void *ret; 265 266 BUG_ON(_brk_start == 0); 267 BUG_ON(align & mask); 268 269 _brk_end = (_brk_end + mask) & ~mask; 270 BUG_ON((char *)(_brk_end + size) > __brk_limit); 271 272 ret = (void *)_brk_end; 273 _brk_end += size; 274 275 memset(ret, 0, size); 276 277 return ret; 278 } 279 280 #ifdef CONFIG_X86_32 281 static void __init cleanup_highmap(void) 282 { 283 } 284 #endif 285 286 static void __init reserve_brk(void) 287 { 288 if (_brk_end > _brk_start) 289 memblock_reserve(__pa_symbol(_brk_start), 290 _brk_end - _brk_start); 291 292 /* Mark brk area as locked down and no longer taking any 293 new allocations */ 294 _brk_start = 0; 295 } 296 297 u64 relocated_ramdisk; 298 299 #ifdef CONFIG_BLK_DEV_INITRD 300 301 static u64 __init get_ramdisk_image(void) 302 { 303 u64 ramdisk_image = boot_params.hdr.ramdisk_image; 304 305 ramdisk_image |= (u64)boot_params.ext_ramdisk_image << 32; 306 307 return ramdisk_image; 308 } 309 static u64 __init get_ramdisk_size(void) 310 { 311 u64 ramdisk_size = boot_params.hdr.ramdisk_size; 312 313 ramdisk_size |= (u64)boot_params.ext_ramdisk_size << 32; 314 315 return ramdisk_size; 316 } 317 318 static void __init relocate_initrd(void) 319 { 320 /* Assume only end is not page aligned */ 321 u64 ramdisk_image = get_ramdisk_image(); 322 u64 ramdisk_size = get_ramdisk_size(); 323 u64 area_size = PAGE_ALIGN(ramdisk_size); 324 325 /* We need to move the initrd down into directly mapped mem */ 326 relocated_ramdisk = memblock_find_in_range(0, PFN_PHYS(max_pfn_mapped), 327 area_size, PAGE_SIZE); 328 329 if (!relocated_ramdisk) 330 panic("Cannot find place for new RAMDISK of size %lld\n", 331 ramdisk_size); 332 333 /* Note: this includes all the mem currently occupied by 334 the initrd, we rely on that fact to keep the data intact. */ 335 memblock_reserve(relocated_ramdisk, area_size); 336 initrd_start = relocated_ramdisk + PAGE_OFFSET; 337 initrd_end = initrd_start + ramdisk_size; 338 printk(KERN_INFO "Allocated new RAMDISK: [mem %#010llx-%#010llx]\n", 339 relocated_ramdisk, relocated_ramdisk + ramdisk_size - 1); 340 341 copy_from_early_mem((void *)initrd_start, ramdisk_image, ramdisk_size); 342 343 printk(KERN_INFO "Move RAMDISK from [mem %#010llx-%#010llx] to" 344 " [mem %#010llx-%#010llx]\n", 345 ramdisk_image, ramdisk_image + ramdisk_size - 1, 346 relocated_ramdisk, relocated_ramdisk + ramdisk_size - 1); 347 } 348 349 static void __init early_reserve_initrd(void) 350 { 351 /* Assume only end is not page aligned */ 352 u64 ramdisk_image = get_ramdisk_image(); 353 u64 ramdisk_size = get_ramdisk_size(); 354 u64 ramdisk_end = PAGE_ALIGN(ramdisk_image + ramdisk_size); 355 356 if (!boot_params.hdr.type_of_loader || 357 !ramdisk_image || !ramdisk_size) 358 return; /* No initrd provided by bootloader */ 359 360 memblock_reserve(ramdisk_image, ramdisk_end - ramdisk_image); 361 } 362 static void __init reserve_initrd(void) 363 { 364 /* Assume only end is not page aligned */ 365 u64 ramdisk_image = get_ramdisk_image(); 366 u64 ramdisk_size = get_ramdisk_size(); 367 u64 ramdisk_end = PAGE_ALIGN(ramdisk_image + ramdisk_size); 368 u64 mapped_size; 369 370 if (!boot_params.hdr.type_of_loader || 371 !ramdisk_image || !ramdisk_size) 372 return; /* No initrd provided by bootloader */ 373 374 initrd_start = 0; 375 376 mapped_size = memblock_mem_size(max_pfn_mapped); 377 if (ramdisk_size >= (mapped_size>>1)) 378 panic("initrd too large to handle, " 379 "disabling initrd (%lld needed, %lld available)\n", 380 ramdisk_size, mapped_size>>1); 381 382 printk(KERN_INFO "RAMDISK: [mem %#010llx-%#010llx]\n", ramdisk_image, 383 ramdisk_end - 1); 384 385 if (pfn_range_is_mapped(PFN_DOWN(ramdisk_image), 386 PFN_DOWN(ramdisk_end))) { 387 /* All are mapped, easy case */ 388 initrd_start = ramdisk_image + PAGE_OFFSET; 389 initrd_end = initrd_start + ramdisk_size; 390 return; 391 } 392 393 relocate_initrd(); 394 395 memblock_free(ramdisk_image, ramdisk_end - ramdisk_image); 396 } 397 398 #else 399 static void __init early_reserve_initrd(void) 400 { 401 } 402 static void __init reserve_initrd(void) 403 { 404 } 405 #endif /* CONFIG_BLK_DEV_INITRD */ 406 407 static void __init parse_setup_data(void) 408 { 409 struct setup_data *data; 410 u64 pa_data, pa_next; 411 412 pa_data = boot_params.hdr.setup_data; 413 while (pa_data) { 414 u32 data_len, data_type; 415 416 data = early_memremap(pa_data, sizeof(*data)); 417 data_len = data->len + sizeof(struct setup_data); 418 data_type = data->type; 419 pa_next = data->next; 420 early_memunmap(data, sizeof(*data)); 421 422 switch (data_type) { 423 case SETUP_E820_EXT: 424 e820__memory_setup_extended(pa_data, data_len); 425 break; 426 case SETUP_DTB: 427 add_dtb(pa_data); 428 break; 429 case SETUP_EFI: 430 parse_efi_setup(pa_data, data_len); 431 break; 432 default: 433 break; 434 } 435 pa_data = pa_next; 436 } 437 } 438 439 static void __init memblock_x86_reserve_range_setup_data(void) 440 { 441 struct setup_data *data; 442 u64 pa_data; 443 444 pa_data = boot_params.hdr.setup_data; 445 while (pa_data) { 446 data = early_memremap(pa_data, sizeof(*data)); 447 memblock_reserve(pa_data, sizeof(*data) + data->len); 448 449 if (data->type == SETUP_INDIRECT && 450 ((struct setup_indirect *)data->data)->type != SETUP_INDIRECT) 451 memblock_reserve(((struct setup_indirect *)data->data)->addr, 452 ((struct setup_indirect *)data->data)->len); 453 454 pa_data = data->next; 455 early_memunmap(data, sizeof(*data)); 456 } 457 } 458 459 /* 460 * --------- Crashkernel reservation ------------------------------ 461 */ 462 463 #ifdef CONFIG_KEXEC_CORE 464 465 /* 16M alignment for crash kernel regions */ 466 #define CRASH_ALIGN SZ_16M 467 468 /* 469 * Keep the crash kernel below this limit. 470 * 471 * On 32 bits earlier kernels would limit the kernel to the low 512 MiB 472 * due to mapping restrictions. 473 * 474 * On 64bit, kdump kernel need be restricted to be under 64TB, which is 475 * the upper limit of system RAM in 4-level paging mode. Since the kdump 476 * jumping could be from 5-level to 4-level, the jumping will fail if 477 * kernel is put above 64TB, and there's no way to detect the paging mode 478 * of the kernel which will be loaded for dumping during the 1st kernel 479 * bootup. 480 */ 481 #ifdef CONFIG_X86_32 482 # define CRASH_ADDR_LOW_MAX SZ_512M 483 # define CRASH_ADDR_HIGH_MAX SZ_512M 484 #else 485 # define CRASH_ADDR_LOW_MAX SZ_4G 486 # define CRASH_ADDR_HIGH_MAX SZ_64T 487 #endif 488 489 static int __init reserve_crashkernel_low(void) 490 { 491 #ifdef CONFIG_X86_64 492 unsigned long long base, low_base = 0, low_size = 0; 493 unsigned long total_low_mem; 494 int ret; 495 496 total_low_mem = memblock_mem_size(1UL << (32 - PAGE_SHIFT)); 497 498 /* crashkernel=Y,low */ 499 ret = parse_crashkernel_low(boot_command_line, total_low_mem, &low_size, &base); 500 if (ret) { 501 /* 502 * two parts from kernel/dma/swiotlb.c: 503 * -swiotlb size: user-specified with swiotlb= or default. 504 * 505 * -swiotlb overflow buffer: now hardcoded to 32k. We round it 506 * to 8M for other buffers that may need to stay low too. Also 507 * make sure we allocate enough extra low memory so that we 508 * don't run out of DMA buffers for 32-bit devices. 509 */ 510 low_size = max(swiotlb_size_or_default() + (8UL << 20), 256UL << 20); 511 } else { 512 /* passed with crashkernel=0,low ? */ 513 if (!low_size) 514 return 0; 515 } 516 517 low_base = memblock_find_in_range(0, 1ULL << 32, low_size, CRASH_ALIGN); 518 if (!low_base) { 519 pr_err("Cannot reserve %ldMB crashkernel low memory, please try smaller size.\n", 520 (unsigned long)(low_size >> 20)); 521 return -ENOMEM; 522 } 523 524 ret = memblock_reserve(low_base, low_size); 525 if (ret) { 526 pr_err("%s: Error reserving crashkernel low memblock.\n", __func__); 527 return ret; 528 } 529 530 pr_info("Reserving %ldMB of low memory at %ldMB for crashkernel (System low RAM: %ldMB)\n", 531 (unsigned long)(low_size >> 20), 532 (unsigned long)(low_base >> 20), 533 (unsigned long)(total_low_mem >> 20)); 534 535 crashk_low_res.start = low_base; 536 crashk_low_res.end = low_base + low_size - 1; 537 insert_resource(&iomem_resource, &crashk_low_res); 538 #endif 539 return 0; 540 } 541 542 static void __init reserve_crashkernel(void) 543 { 544 unsigned long long crash_size, crash_base, total_mem; 545 bool high = false; 546 int ret; 547 548 total_mem = memblock_phys_mem_size(); 549 550 /* crashkernel=XM */ 551 ret = parse_crashkernel(boot_command_line, total_mem, &crash_size, &crash_base); 552 if (ret != 0 || crash_size <= 0) { 553 /* crashkernel=X,high */ 554 ret = parse_crashkernel_high(boot_command_line, total_mem, 555 &crash_size, &crash_base); 556 if (ret != 0 || crash_size <= 0) 557 return; 558 high = true; 559 } 560 561 if (xen_pv_domain()) { 562 pr_info("Ignoring crashkernel for a Xen PV domain\n"); 563 return; 564 } 565 566 /* 0 means: find the address automatically */ 567 if (!crash_base) { 568 /* 569 * Set CRASH_ADDR_LOW_MAX upper bound for crash memory, 570 * crashkernel=x,high reserves memory over 4G, also allocates 571 * 256M extra low memory for DMA buffers and swiotlb. 572 * But the extra memory is not required for all machines. 573 * So try low memory first and fall back to high memory 574 * unless "crashkernel=size[KMG],high" is specified. 575 */ 576 if (!high) 577 crash_base = memblock_find_in_range(CRASH_ALIGN, 578 CRASH_ADDR_LOW_MAX, 579 crash_size, CRASH_ALIGN); 580 if (!crash_base) 581 crash_base = memblock_find_in_range(CRASH_ALIGN, 582 CRASH_ADDR_HIGH_MAX, 583 crash_size, CRASH_ALIGN); 584 if (!crash_base) { 585 pr_info("crashkernel reservation failed - No suitable area found.\n"); 586 return; 587 } 588 } else { 589 unsigned long long start; 590 591 start = memblock_find_in_range(crash_base, 592 crash_base + crash_size, 593 crash_size, 1 << 20); 594 if (start != crash_base) { 595 pr_info("crashkernel reservation failed - memory is in use.\n"); 596 return; 597 } 598 } 599 ret = memblock_reserve(crash_base, crash_size); 600 if (ret) { 601 pr_err("%s: Error reserving crashkernel memblock.\n", __func__); 602 return; 603 } 604 605 if (crash_base >= (1ULL << 32) && reserve_crashkernel_low()) { 606 memblock_free(crash_base, crash_size); 607 return; 608 } 609 610 pr_info("Reserving %ldMB of memory at %ldMB for crashkernel (System RAM: %ldMB)\n", 611 (unsigned long)(crash_size >> 20), 612 (unsigned long)(crash_base >> 20), 613 (unsigned long)(total_mem >> 20)); 614 615 crashk_res.start = crash_base; 616 crashk_res.end = crash_base + crash_size - 1; 617 insert_resource(&iomem_resource, &crashk_res); 618 } 619 #else 620 static void __init reserve_crashkernel(void) 621 { 622 } 623 #endif 624 625 static struct resource standard_io_resources[] = { 626 { .name = "dma1", .start = 0x00, .end = 0x1f, 627 .flags = IORESOURCE_BUSY | IORESOURCE_IO }, 628 { .name = "pic1", .start = 0x20, .end = 0x21, 629 .flags = IORESOURCE_BUSY | IORESOURCE_IO }, 630 { .name = "timer0", .start = 0x40, .end = 0x43, 631 .flags = IORESOURCE_BUSY | IORESOURCE_IO }, 632 { .name = "timer1", .start = 0x50, .end = 0x53, 633 .flags = IORESOURCE_BUSY | IORESOURCE_IO }, 634 { .name = "keyboard", .start = 0x60, .end = 0x60, 635 .flags = IORESOURCE_BUSY | IORESOURCE_IO }, 636 { .name = "keyboard", .start = 0x64, .end = 0x64, 637 .flags = IORESOURCE_BUSY | IORESOURCE_IO }, 638 { .name = "dma page reg", .start = 0x80, .end = 0x8f, 639 .flags = IORESOURCE_BUSY | IORESOURCE_IO }, 640 { .name = "pic2", .start = 0xa0, .end = 0xa1, 641 .flags = IORESOURCE_BUSY | IORESOURCE_IO }, 642 { .name = "dma2", .start = 0xc0, .end = 0xdf, 643 .flags = IORESOURCE_BUSY | IORESOURCE_IO }, 644 { .name = "fpu", .start = 0xf0, .end = 0xff, 645 .flags = IORESOURCE_BUSY | IORESOURCE_IO } 646 }; 647 648 void __init reserve_standard_io_resources(void) 649 { 650 int i; 651 652 /* request I/O space for devices used on all i[345]86 PCs */ 653 for (i = 0; i < ARRAY_SIZE(standard_io_resources); i++) 654 request_resource(&ioport_resource, &standard_io_resources[i]); 655 656 } 657 658 static __init void reserve_ibft_region(void) 659 { 660 unsigned long addr, size = 0; 661 662 addr = find_ibft_region(&size); 663 664 if (size) 665 memblock_reserve(addr, size); 666 } 667 668 static bool __init snb_gfx_workaround_needed(void) 669 { 670 #ifdef CONFIG_PCI 671 int i; 672 u16 vendor, devid; 673 static const __initconst u16 snb_ids[] = { 674 0x0102, 675 0x0112, 676 0x0122, 677 0x0106, 678 0x0116, 679 0x0126, 680 0x010a, 681 }; 682 683 /* Assume no if something weird is going on with PCI */ 684 if (!early_pci_allowed()) 685 return false; 686 687 vendor = read_pci_config_16(0, 2, 0, PCI_VENDOR_ID); 688 if (vendor != 0x8086) 689 return false; 690 691 devid = read_pci_config_16(0, 2, 0, PCI_DEVICE_ID); 692 for (i = 0; i < ARRAY_SIZE(snb_ids); i++) 693 if (devid == snb_ids[i]) 694 return true; 695 #endif 696 697 return false; 698 } 699 700 /* 701 * Sandy Bridge graphics has trouble with certain ranges, exclude 702 * them from allocation. 703 */ 704 static void __init trim_snb_memory(void) 705 { 706 static const __initconst unsigned long bad_pages[] = { 707 0x20050000, 708 0x20110000, 709 0x20130000, 710 0x20138000, 711 0x40004000, 712 }; 713 int i; 714 715 if (!snb_gfx_workaround_needed()) 716 return; 717 718 printk(KERN_DEBUG "reserving inaccessible SNB gfx pages\n"); 719 720 /* 721 * Reserve all memory below the 1 MB mark that has not 722 * already been reserved. 723 */ 724 memblock_reserve(0, 1<<20); 725 726 for (i = 0; i < ARRAY_SIZE(bad_pages); i++) { 727 if (memblock_reserve(bad_pages[i], PAGE_SIZE)) 728 printk(KERN_WARNING "failed to reserve 0x%08lx\n", 729 bad_pages[i]); 730 } 731 } 732 733 /* 734 * Here we put platform-specific memory range workarounds, i.e. 735 * memory known to be corrupt or otherwise in need to be reserved on 736 * specific platforms. 737 * 738 * If this gets used more widely it could use a real dispatch mechanism. 739 */ 740 static void __init trim_platform_memory_ranges(void) 741 { 742 trim_snb_memory(); 743 } 744 745 static void __init trim_bios_range(void) 746 { 747 /* 748 * A special case is the first 4Kb of memory; 749 * This is a BIOS owned area, not kernel ram, but generally 750 * not listed as such in the E820 table. 751 * 752 * This typically reserves additional memory (64KiB by default) 753 * since some BIOSes are known to corrupt low memory. See the 754 * Kconfig help text for X86_RESERVE_LOW. 755 */ 756 e820__range_update(0, PAGE_SIZE, E820_TYPE_RAM, E820_TYPE_RESERVED); 757 758 /* 759 * special case: Some BIOSes report the PC BIOS 760 * area (640Kb -> 1Mb) as RAM even though it is not. 761 * take them out. 762 */ 763 e820__range_remove(BIOS_BEGIN, BIOS_END - BIOS_BEGIN, E820_TYPE_RAM, 1); 764 765 e820__update_table(e820_table); 766 } 767 768 /* called before trim_bios_range() to spare extra sanitize */ 769 static void __init e820_add_kernel_range(void) 770 { 771 u64 start = __pa_symbol(_text); 772 u64 size = __pa_symbol(_end) - start; 773 774 /* 775 * Complain if .text .data and .bss are not marked as E820_TYPE_RAM and 776 * attempt to fix it by adding the range. We may have a confused BIOS, 777 * or the user may have used memmap=exactmap or memmap=xxM$yyM to 778 * exclude kernel range. If we really are running on top non-RAM, 779 * we will crash later anyways. 780 */ 781 if (e820__mapped_all(start, start + size, E820_TYPE_RAM)) 782 return; 783 784 pr_warn(".text .data .bss are not marked as E820_TYPE_RAM!\n"); 785 e820__range_remove(start, size, E820_TYPE_RAM, 0); 786 e820__range_add(start, size, E820_TYPE_RAM); 787 } 788 789 static unsigned reserve_low = CONFIG_X86_RESERVE_LOW << 10; 790 791 static int __init parse_reservelow(char *p) 792 { 793 unsigned long long size; 794 795 if (!p) 796 return -EINVAL; 797 798 size = memparse(p, &p); 799 800 if (size < 4096) 801 size = 4096; 802 803 if (size > 640*1024) 804 size = 640*1024; 805 806 reserve_low = size; 807 808 return 0; 809 } 810 811 early_param("reservelow", parse_reservelow); 812 813 static void __init trim_low_memory_range(void) 814 { 815 memblock_reserve(0, ALIGN(reserve_low, PAGE_SIZE)); 816 } 817 818 /* 819 * Dump out kernel offset information on panic. 820 */ 821 static int 822 dump_kernel_offset(struct notifier_block *self, unsigned long v, void *p) 823 { 824 if (kaslr_enabled()) { 825 pr_emerg("Kernel Offset: 0x%lx from 0x%lx (relocation range: 0x%lx-0x%lx)\n", 826 kaslr_offset(), 827 __START_KERNEL, 828 __START_KERNEL_map, 829 MODULES_VADDR-1); 830 } else { 831 pr_emerg("Kernel Offset: disabled\n"); 832 } 833 834 return 0; 835 } 836 837 /* 838 * Determine if we were loaded by an EFI loader. If so, then we have also been 839 * passed the efi memmap, systab, etc., so we should use these data structures 840 * for initialization. Note, the efi init code path is determined by the 841 * global efi_enabled. This allows the same kernel image to be used on existing 842 * systems (with a traditional BIOS) as well as on EFI systems. 843 */ 844 /* 845 * setup_arch - architecture-specific boot-time initializations 846 * 847 * Note: On x86_64, fixmaps are ready for use even before this is called. 848 */ 849 850 void __init setup_arch(char **cmdline_p) 851 { 852 /* 853 * Reserve the memory occupied by the kernel between _text and 854 * __end_of_kernel_reserve symbols. Any kernel sections after the 855 * __end_of_kernel_reserve symbol must be explicitly reserved with a 856 * separate memblock_reserve() or they will be discarded. 857 */ 858 memblock_reserve(__pa_symbol(_text), 859 (unsigned long)__end_of_kernel_reserve - (unsigned long)_text); 860 861 /* 862 * Make sure page 0 is always reserved because on systems with 863 * L1TF its contents can be leaked to user processes. 864 */ 865 memblock_reserve(0, PAGE_SIZE); 866 867 early_reserve_initrd(); 868 869 /* 870 * At this point everything still needed from the boot loader 871 * or BIOS or kernel text should be early reserved or marked not 872 * RAM in e820. All other memory is free game. 873 */ 874 875 #ifdef CONFIG_X86_32 876 memcpy(&boot_cpu_data, &new_cpu_data, sizeof(new_cpu_data)); 877 878 /* 879 * copy kernel address range established so far and switch 880 * to the proper swapper page table 881 */ 882 clone_pgd_range(swapper_pg_dir + KERNEL_PGD_BOUNDARY, 883 initial_page_table + KERNEL_PGD_BOUNDARY, 884 KERNEL_PGD_PTRS); 885 886 load_cr3(swapper_pg_dir); 887 /* 888 * Note: Quark X1000 CPUs advertise PGE incorrectly and require 889 * a cr3 based tlb flush, so the following __flush_tlb_all() 890 * will not flush anything because the cpu quirk which clears 891 * X86_FEATURE_PGE has not been invoked yet. Though due to the 892 * load_cr3() above the TLB has been flushed already. The 893 * quirk is invoked before subsequent calls to __flush_tlb_all() 894 * so proper operation is guaranteed. 895 */ 896 __flush_tlb_all(); 897 #else 898 printk(KERN_INFO "Command line: %s\n", boot_command_line); 899 boot_cpu_data.x86_phys_bits = MAX_PHYSMEM_BITS; 900 #endif 901 902 /* 903 * If we have OLPC OFW, we might end up relocating the fixmap due to 904 * reserve_top(), so do this before touching the ioremap area. 905 */ 906 olpc_ofw_detect(); 907 908 idt_setup_early_traps(); 909 early_cpu_init(); 910 arch_init_ideal_nops(); 911 jump_label_init(); 912 early_ioremap_init(); 913 914 setup_olpc_ofw_pgd(); 915 916 ROOT_DEV = old_decode_dev(boot_params.hdr.root_dev); 917 screen_info = boot_params.screen_info; 918 edid_info = boot_params.edid_info; 919 #ifdef CONFIG_X86_32 920 apm_info.bios = boot_params.apm_bios_info; 921 ist_info = boot_params.ist_info; 922 #endif 923 saved_video_mode = boot_params.hdr.vid_mode; 924 bootloader_type = boot_params.hdr.type_of_loader; 925 if ((bootloader_type >> 4) == 0xe) { 926 bootloader_type &= 0xf; 927 bootloader_type |= (boot_params.hdr.ext_loader_type+0x10) << 4; 928 } 929 bootloader_version = bootloader_type & 0xf; 930 bootloader_version |= boot_params.hdr.ext_loader_ver << 4; 931 932 #ifdef CONFIG_BLK_DEV_RAM 933 rd_image_start = boot_params.hdr.ram_size & RAMDISK_IMAGE_START_MASK; 934 rd_prompt = ((boot_params.hdr.ram_size & RAMDISK_PROMPT_FLAG) != 0); 935 rd_doload = ((boot_params.hdr.ram_size & RAMDISK_LOAD_FLAG) != 0); 936 #endif 937 #ifdef CONFIG_EFI 938 if (!strncmp((char *)&boot_params.efi_info.efi_loader_signature, 939 EFI32_LOADER_SIGNATURE, 4)) { 940 set_bit(EFI_BOOT, &efi.flags); 941 } else if (!strncmp((char *)&boot_params.efi_info.efi_loader_signature, 942 EFI64_LOADER_SIGNATURE, 4)) { 943 set_bit(EFI_BOOT, &efi.flags); 944 set_bit(EFI_64BIT, &efi.flags); 945 } 946 #endif 947 948 x86_init.oem.arch_setup(); 949 950 iomem_resource.end = (1ULL << boot_cpu_data.x86_phys_bits) - 1; 951 e820__memory_setup(); 952 parse_setup_data(); 953 954 copy_edd(); 955 956 if (!boot_params.hdr.root_flags) 957 root_mountflags &= ~MS_RDONLY; 958 init_mm.start_code = (unsigned long) _text; 959 init_mm.end_code = (unsigned long) _etext; 960 init_mm.end_data = (unsigned long) _edata; 961 init_mm.brk = _brk_end; 962 963 mpx_mm_init(&init_mm); 964 965 code_resource.start = __pa_symbol(_text); 966 code_resource.end = __pa_symbol(_etext)-1; 967 rodata_resource.start = __pa_symbol(__start_rodata); 968 rodata_resource.end = __pa_symbol(__end_rodata)-1; 969 data_resource.start = __pa_symbol(_sdata); 970 data_resource.end = __pa_symbol(_edata)-1; 971 bss_resource.start = __pa_symbol(__bss_start); 972 bss_resource.end = __pa_symbol(__bss_stop)-1; 973 974 #ifdef CONFIG_CMDLINE_BOOL 975 #ifdef CONFIG_CMDLINE_OVERRIDE 976 strlcpy(boot_command_line, builtin_cmdline, COMMAND_LINE_SIZE); 977 #else 978 if (builtin_cmdline[0]) { 979 /* append boot loader cmdline to builtin */ 980 strlcat(builtin_cmdline, " ", COMMAND_LINE_SIZE); 981 strlcat(builtin_cmdline, boot_command_line, COMMAND_LINE_SIZE); 982 strlcpy(boot_command_line, builtin_cmdline, COMMAND_LINE_SIZE); 983 } 984 #endif 985 #endif 986 987 strlcpy(command_line, boot_command_line, COMMAND_LINE_SIZE); 988 *cmdline_p = command_line; 989 990 /* 991 * x86_configure_nx() is called before parse_early_param() to detect 992 * whether hardware doesn't support NX (so that the early EHCI debug 993 * console setup can safely call set_fixmap()). It may then be called 994 * again from within noexec_setup() during parsing early parameters 995 * to honor the respective command line option. 996 */ 997 x86_configure_nx(); 998 999 parse_early_param(); 1000 1001 if (efi_enabled(EFI_BOOT)) 1002 efi_memblock_x86_reserve_range(); 1003 #ifdef CONFIG_MEMORY_HOTPLUG 1004 /* 1005 * Memory used by the kernel cannot be hot-removed because Linux 1006 * cannot migrate the kernel pages. When memory hotplug is 1007 * enabled, we should prevent memblock from allocating memory 1008 * for the kernel. 1009 * 1010 * ACPI SRAT records all hotpluggable memory ranges. But before 1011 * SRAT is parsed, we don't know about it. 1012 * 1013 * The kernel image is loaded into memory at very early time. We 1014 * cannot prevent this anyway. So on NUMA system, we set any 1015 * node the kernel resides in as un-hotpluggable. 1016 * 1017 * Since on modern servers, one node could have double-digit 1018 * gigabytes memory, we can assume the memory around the kernel 1019 * image is also un-hotpluggable. So before SRAT is parsed, just 1020 * allocate memory near the kernel image to try the best to keep 1021 * the kernel away from hotpluggable memory. 1022 */ 1023 if (movable_node_is_enabled()) 1024 memblock_set_bottom_up(true); 1025 #endif 1026 1027 x86_report_nx(); 1028 1029 /* after early param, so could get panic from serial */ 1030 memblock_x86_reserve_range_setup_data(); 1031 1032 if (acpi_mps_check()) { 1033 #ifdef CONFIG_X86_LOCAL_APIC 1034 disable_apic = 1; 1035 #endif 1036 setup_clear_cpu_cap(X86_FEATURE_APIC); 1037 } 1038 1039 e820__reserve_setup_data(); 1040 e820__finish_early_params(); 1041 1042 if (efi_enabled(EFI_BOOT)) 1043 efi_init(); 1044 1045 dmi_setup(); 1046 1047 /* 1048 * VMware detection requires dmi to be available, so this 1049 * needs to be done after dmi_setup(), for the boot CPU. 1050 */ 1051 init_hypervisor_platform(); 1052 1053 tsc_early_init(); 1054 x86_init.resources.probe_roms(); 1055 1056 /* after parse_early_param, so could debug it */ 1057 insert_resource(&iomem_resource, &code_resource); 1058 insert_resource(&iomem_resource, &rodata_resource); 1059 insert_resource(&iomem_resource, &data_resource); 1060 insert_resource(&iomem_resource, &bss_resource); 1061 1062 e820_add_kernel_range(); 1063 trim_bios_range(); 1064 #ifdef CONFIG_X86_32 1065 if (ppro_with_ram_bug()) { 1066 e820__range_update(0x70000000ULL, 0x40000ULL, E820_TYPE_RAM, 1067 E820_TYPE_RESERVED); 1068 e820__update_table(e820_table); 1069 printk(KERN_INFO "fixed physical RAM map:\n"); 1070 e820__print_table("bad_ppro"); 1071 } 1072 #else 1073 early_gart_iommu_check(); 1074 #endif 1075 1076 /* 1077 * partially used pages are not usable - thus 1078 * we are rounding upwards: 1079 */ 1080 max_pfn = e820__end_of_ram_pfn(); 1081 1082 /* update e820 for memory not covered by WB MTRRs */ 1083 mtrr_bp_init(); 1084 if (mtrr_trim_uncached_memory(max_pfn)) 1085 max_pfn = e820__end_of_ram_pfn(); 1086 1087 max_possible_pfn = max_pfn; 1088 1089 /* 1090 * This call is required when the CPU does not support PAT. If 1091 * mtrr_bp_init() invoked it already via pat_init() the call has no 1092 * effect. 1093 */ 1094 init_cache_modes(); 1095 1096 /* 1097 * Define random base addresses for memory sections after max_pfn is 1098 * defined and before each memory section base is used. 1099 */ 1100 kernel_randomize_memory(); 1101 1102 #ifdef CONFIG_X86_32 1103 /* max_low_pfn get updated here */ 1104 find_low_pfn_range(); 1105 #else 1106 check_x2apic(); 1107 1108 /* How many end-of-memory variables you have, grandma! */ 1109 /* need this before calling reserve_initrd */ 1110 if (max_pfn > (1UL<<(32 - PAGE_SHIFT))) 1111 max_low_pfn = e820__end_of_low_ram_pfn(); 1112 else 1113 max_low_pfn = max_pfn; 1114 1115 high_memory = (void *)__va(max_pfn * PAGE_SIZE - 1) + 1; 1116 #endif 1117 1118 /* 1119 * Find and reserve possible boot-time SMP configuration: 1120 */ 1121 find_smp_config(); 1122 1123 reserve_ibft_region(); 1124 1125 early_alloc_pgt_buf(); 1126 1127 /* 1128 * Need to conclude brk, before e820__memblock_setup() 1129 * it could use memblock_find_in_range, could overlap with 1130 * brk area. 1131 */ 1132 reserve_brk(); 1133 1134 cleanup_highmap(); 1135 1136 memblock_set_current_limit(ISA_END_ADDRESS); 1137 e820__memblock_setup(); 1138 1139 reserve_bios_regions(); 1140 1141 efi_fake_memmap(); 1142 efi_find_mirror(); 1143 efi_esrt_init(); 1144 1145 /* 1146 * The EFI specification says that boot service code won't be 1147 * called after ExitBootServices(). This is, in fact, a lie. 1148 */ 1149 efi_reserve_boot_services(); 1150 1151 /* preallocate 4k for mptable mpc */ 1152 e820__memblock_alloc_reserved_mpc_new(); 1153 1154 #ifdef CONFIG_X86_CHECK_BIOS_CORRUPTION 1155 setup_bios_corruption_check(); 1156 #endif 1157 1158 #ifdef CONFIG_X86_32 1159 printk(KERN_DEBUG "initial memory mapped: [mem 0x00000000-%#010lx]\n", 1160 (max_pfn_mapped<<PAGE_SHIFT) - 1); 1161 #endif 1162 1163 reserve_real_mode(); 1164 1165 trim_platform_memory_ranges(); 1166 trim_low_memory_range(); 1167 1168 init_mem_mapping(); 1169 1170 idt_setup_early_pf(); 1171 1172 /* 1173 * Update mmu_cr4_features (and, indirectly, trampoline_cr4_features) 1174 * with the current CR4 value. This may not be necessary, but 1175 * auditing all the early-boot CR4 manipulation would be needed to 1176 * rule it out. 1177 * 1178 * Mask off features that don't work outside long mode (just 1179 * PCIDE for now). 1180 */ 1181 mmu_cr4_features = __read_cr4() & ~X86_CR4_PCIDE; 1182 1183 memblock_set_current_limit(get_max_mapped()); 1184 1185 /* 1186 * NOTE: On x86-32, only from this point on, fixmaps are ready for use. 1187 */ 1188 1189 #ifdef CONFIG_PROVIDE_OHCI1394_DMA_INIT 1190 if (init_ohci1394_dma_early) 1191 init_ohci1394_dma_on_all_controllers(); 1192 #endif 1193 /* Allocate bigger log buffer */ 1194 setup_log_buf(1); 1195 1196 if (efi_enabled(EFI_BOOT)) { 1197 switch (boot_params.secure_boot) { 1198 case efi_secureboot_mode_disabled: 1199 pr_info("Secure boot disabled\n"); 1200 break; 1201 case efi_secureboot_mode_enabled: 1202 pr_info("Secure boot enabled\n"); 1203 break; 1204 default: 1205 pr_info("Secure boot could not be determined\n"); 1206 break; 1207 } 1208 } 1209 1210 reserve_initrd(); 1211 1212 acpi_table_upgrade(); 1213 1214 vsmp_init(); 1215 1216 io_delay_init(); 1217 1218 early_platform_quirks(); 1219 1220 /* 1221 * Parse the ACPI tables for possible boot-time SMP configuration. 1222 */ 1223 acpi_boot_table_init(); 1224 1225 early_acpi_boot_init(); 1226 1227 initmem_init(); 1228 dma_contiguous_reserve(max_pfn_mapped << PAGE_SHIFT); 1229 1230 /* 1231 * Reserve memory for crash kernel after SRAT is parsed so that it 1232 * won't consume hotpluggable memory. 1233 */ 1234 reserve_crashkernel(); 1235 1236 memblock_find_dma_reserve(); 1237 1238 if (!early_xdbc_setup_hardware()) 1239 early_xdbc_register_console(); 1240 1241 x86_init.paging.pagetable_init(); 1242 1243 kasan_init(); 1244 1245 /* 1246 * Sync back kernel address range. 1247 * 1248 * FIXME: Can the later sync in setup_cpu_entry_areas() replace 1249 * this call? 1250 */ 1251 sync_initial_page_table(); 1252 1253 tboot_probe(); 1254 1255 map_vsyscall(); 1256 1257 generic_apic_probe(); 1258 1259 early_quirks(); 1260 1261 /* 1262 * Read APIC and some other early information from ACPI tables. 1263 */ 1264 acpi_boot_init(); 1265 sfi_init(); 1266 x86_dtb_init(); 1267 1268 /* 1269 * get boot-time SMP configuration: 1270 */ 1271 get_smp_config(); 1272 1273 /* 1274 * Systems w/o ACPI and mptables might not have it mapped the local 1275 * APIC yet, but prefill_possible_map() might need to access it. 1276 */ 1277 init_apic_mappings(); 1278 1279 prefill_possible_map(); 1280 1281 init_cpu_to_node(); 1282 1283 io_apic_init_mappings(); 1284 1285 x86_init.hyper.guest_late_init(); 1286 1287 e820__reserve_resources(); 1288 e820__register_nosave_regions(max_pfn); 1289 1290 x86_init.resources.reserve_resources(); 1291 1292 e820__setup_pci_gap(); 1293 1294 #ifdef CONFIG_VT 1295 #if defined(CONFIG_VGA_CONSOLE) 1296 if (!efi_enabled(EFI_BOOT) || (efi_mem_type(0xa0000) != EFI_CONVENTIONAL_MEMORY)) 1297 conswitchp = &vga_con; 1298 #elif defined(CONFIG_DUMMY_CONSOLE) 1299 conswitchp = &dummy_con; 1300 #endif 1301 #endif 1302 x86_init.oem.banner(); 1303 1304 x86_init.timers.wallclock_init(); 1305 1306 mcheck_init(); 1307 1308 register_refined_jiffies(CLOCK_TICK_RATE); 1309 1310 #ifdef CONFIG_EFI 1311 if (efi_enabled(EFI_BOOT)) 1312 efi_apply_memmap_quirks(); 1313 #endif 1314 1315 unwind_init(); 1316 } 1317 1318 #ifdef CONFIG_X86_32 1319 1320 static struct resource video_ram_resource = { 1321 .name = "Video RAM area", 1322 .start = 0xa0000, 1323 .end = 0xbffff, 1324 .flags = IORESOURCE_BUSY | IORESOURCE_MEM 1325 }; 1326 1327 void __init i386_reserve_resources(void) 1328 { 1329 request_resource(&iomem_resource, &video_ram_resource); 1330 reserve_standard_io_resources(); 1331 } 1332 1333 #endif /* CONFIG_X86_32 */ 1334 1335 static struct notifier_block kernel_offset_notifier = { 1336 .notifier_call = dump_kernel_offset 1337 }; 1338 1339 static int __init register_kernel_offset_dumper(void) 1340 { 1341 atomic_notifier_chain_register(&panic_notifier_list, 1342 &kernel_offset_notifier); 1343 return 0; 1344 } 1345 __initcall(register_kernel_offset_dumper); 1346