1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Copyright (C) 1995 Linus Torvalds 4 * 5 * This file contains the setup_arch() code, which handles the architecture-dependent 6 * parts of early kernel initialization. 7 */ 8 #include <linux/acpi.h> 9 #include <linux/console.h> 10 #include <linux/cpu.h> 11 #include <linux/crash_dump.h> 12 #include <linux/dma-map-ops.h> 13 #include <linux/efi.h> 14 #include <linux/ima.h> 15 #include <linux/init_ohci1394_dma.h> 16 #include <linux/initrd.h> 17 #include <linux/iscsi_ibft.h> 18 #include <linux/memblock.h> 19 #include <linux/panic_notifier.h> 20 #include <linux/pci.h> 21 #include <linux/root_dev.h> 22 #include <linux/hugetlb.h> 23 #include <linux/tboot.h> 24 #include <linux/usb/xhci-dbgp.h> 25 #include <linux/static_call.h> 26 #include <linux/swiotlb.h> 27 #include <linux/random.h> 28 29 #include <uapi/linux/mount.h> 30 31 #include <xen/xen.h> 32 33 #include <asm/apic.h> 34 #include <asm/efi.h> 35 #include <asm/numa.h> 36 #include <asm/bios_ebda.h> 37 #include <asm/bugs.h> 38 #include <asm/cacheinfo.h> 39 #include <asm/coco.h> 40 #include <asm/cpu.h> 41 #include <asm/efi.h> 42 #include <asm/gart.h> 43 #include <asm/hypervisor.h> 44 #include <asm/io_apic.h> 45 #include <asm/kasan.h> 46 #include <asm/kaslr.h> 47 #include <asm/mce.h> 48 #include <asm/memtype.h> 49 #include <asm/mtrr.h> 50 #include <asm/realmode.h> 51 #include <asm/olpc_ofw.h> 52 #include <asm/pci-direct.h> 53 #include <asm/prom.h> 54 #include <asm/proto.h> 55 #include <asm/thermal.h> 56 #include <asm/unwind.h> 57 #include <asm/vsyscall.h> 58 #include <linux/vmalloc.h> 59 #if defined(CONFIG_X86_LOCAL_APIC) 60 #include <asm/nmi.h> 61 #endif 62 63 /* 64 * max_low_pfn_mapped: highest directly mapped pfn < 4 GB 65 * max_pfn_mapped: highest directly mapped pfn > 4 GB 66 * 67 * The direct mapping only covers E820_TYPE_RAM regions, so the ranges and gaps are 68 * represented by pfn_mapped[]. 69 */ 70 unsigned long max_low_pfn_mapped; 71 unsigned long max_pfn_mapped; 72 73 #ifdef CONFIG_DMI 74 RESERVE_BRK(dmi_alloc, 65536); 75 #endif 76 77 78 unsigned long _brk_start = (unsigned long)__brk_base; 79 unsigned long _brk_end = (unsigned long)__brk_base; 80 81 struct boot_params boot_params; 82 83 /* 84 * These are the four main kernel memory regions, we put them into 85 * the resource tree so that kdump tools and other debugging tools 86 * recover it: 87 */ 88 89 static struct resource rodata_resource = { 90 .name = "Kernel rodata", 91 .start = 0, 92 .end = 0, 93 .flags = IORESOURCE_BUSY | IORESOURCE_SYSTEM_RAM 94 }; 95 96 static struct resource data_resource = { 97 .name = "Kernel data", 98 .start = 0, 99 .end = 0, 100 .flags = IORESOURCE_BUSY | IORESOURCE_SYSTEM_RAM 101 }; 102 103 static struct resource code_resource = { 104 .name = "Kernel code", 105 .start = 0, 106 .end = 0, 107 .flags = IORESOURCE_BUSY | IORESOURCE_SYSTEM_RAM 108 }; 109 110 static struct resource bss_resource = { 111 .name = "Kernel bss", 112 .start = 0, 113 .end = 0, 114 .flags = IORESOURCE_BUSY | IORESOURCE_SYSTEM_RAM 115 }; 116 117 118 #ifdef CONFIG_X86_32 119 /* CPU data as detected by the assembly code in head_32.S */ 120 struct cpuinfo_x86 new_cpu_data; 121 122 struct apm_info apm_info; 123 EXPORT_SYMBOL(apm_info); 124 125 #if defined(CONFIG_X86_SPEEDSTEP_SMI) || \ 126 defined(CONFIG_X86_SPEEDSTEP_SMI_MODULE) 127 struct ist_info ist_info; 128 EXPORT_SYMBOL(ist_info); 129 #else 130 struct ist_info ist_info; 131 #endif 132 133 #endif 134 135 struct cpuinfo_x86 boot_cpu_data __read_mostly; 136 EXPORT_SYMBOL(boot_cpu_data); 137 138 #if !defined(CONFIG_X86_PAE) || defined(CONFIG_X86_64) 139 __visible unsigned long mmu_cr4_features __ro_after_init; 140 #else 141 __visible unsigned long mmu_cr4_features __ro_after_init = X86_CR4_PAE; 142 #endif 143 144 #ifdef CONFIG_IMA 145 static phys_addr_t ima_kexec_buffer_phys; 146 static size_t ima_kexec_buffer_size; 147 #endif 148 149 /* Boot loader ID and version as integers, for the benefit of proc_dointvec */ 150 int bootloader_type, bootloader_version; 151 152 static const struct ctl_table x86_sysctl_table[] = { 153 { 154 .procname = "panic_on_unrecovered_nmi", 155 .data = &panic_on_unrecovered_nmi, 156 .maxlen = sizeof(int), 157 .mode = 0644, 158 .proc_handler = proc_dointvec, 159 }, 160 { 161 .procname = "panic_on_io_nmi", 162 .data = &panic_on_io_nmi, 163 .maxlen = sizeof(int), 164 .mode = 0644, 165 .proc_handler = proc_dointvec, 166 }, 167 { 168 .procname = "bootloader_type", 169 .data = &bootloader_type, 170 .maxlen = sizeof(int), 171 .mode = 0444, 172 .proc_handler = proc_dointvec, 173 }, 174 { 175 .procname = "bootloader_version", 176 .data = &bootloader_version, 177 .maxlen = sizeof(int), 178 .mode = 0444, 179 .proc_handler = proc_dointvec, 180 }, 181 { 182 .procname = "io_delay_type", 183 .data = &io_delay_type, 184 .maxlen = sizeof(int), 185 .mode = 0644, 186 .proc_handler = proc_dointvec, 187 }, 188 #if defined(CONFIG_X86_LOCAL_APIC) 189 { 190 .procname = "unknown_nmi_panic", 191 .data = &unknown_nmi_panic, 192 .maxlen = sizeof(int), 193 .mode = 0644, 194 .proc_handler = proc_dointvec, 195 }, 196 #endif 197 #if defined(CONFIG_ACPI_SLEEP) 198 { 199 .procname = "acpi_video_flags", 200 .data = &acpi_realmode_flags, 201 .maxlen = sizeof(unsigned long), 202 .mode = 0644, 203 .proc_handler = proc_doulongvec_minmax, 204 }, 205 #endif 206 }; 207 208 static int __init init_x86_sysctl(void) 209 { 210 register_sysctl_init("kernel", x86_sysctl_table); 211 return 0; 212 } 213 arch_initcall(init_x86_sysctl); 214 215 /* 216 * Setup options 217 */ 218 struct screen_info screen_info; 219 EXPORT_SYMBOL(screen_info); 220 struct edid_info edid_info; 221 EXPORT_SYMBOL_GPL(edid_info); 222 223 extern int root_mountflags; 224 225 unsigned long saved_video_mode; 226 227 #define RAMDISK_IMAGE_START_MASK 0x07FF 228 #define RAMDISK_PROMPT_FLAG 0x8000 229 #define RAMDISK_LOAD_FLAG 0x4000 230 231 static char __initdata command_line[COMMAND_LINE_SIZE]; 232 #ifdef CONFIG_CMDLINE_BOOL 233 char builtin_cmdline[COMMAND_LINE_SIZE] = CONFIG_CMDLINE; 234 bool builtin_cmdline_added __ro_after_init; 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 #ifdef CONFIG_BLK_DEV_INITRD 298 299 static u64 __init get_ramdisk_image(void) 300 { 301 u64 ramdisk_image = boot_params.hdr.ramdisk_image; 302 303 ramdisk_image |= (u64)boot_params.ext_ramdisk_image << 32; 304 305 if (ramdisk_image == 0) 306 ramdisk_image = phys_initrd_start; 307 308 return ramdisk_image; 309 } 310 static u64 __init get_ramdisk_size(void) 311 { 312 u64 ramdisk_size = boot_params.hdr.ramdisk_size; 313 314 ramdisk_size |= (u64)boot_params.ext_ramdisk_size << 32; 315 316 if (ramdisk_size == 0) 317 ramdisk_size = phys_initrd_size; 318 319 return ramdisk_size; 320 } 321 322 static void __init relocate_initrd(void) 323 { 324 /* Assume only end is not page aligned */ 325 u64 ramdisk_image = get_ramdisk_image(); 326 u64 ramdisk_size = get_ramdisk_size(); 327 u64 area_size = PAGE_ALIGN(ramdisk_size); 328 int ret = 0; 329 330 /* We need to move the initrd down into directly mapped mem */ 331 u64 relocated_ramdisk = memblock_phys_alloc_range(area_size, PAGE_SIZE, 0, 332 PFN_PHYS(max_pfn_mapped)); 333 if (!relocated_ramdisk) 334 panic("Cannot find place for new RAMDISK of size %lld\n", 335 ramdisk_size); 336 337 initrd_start = relocated_ramdisk + PAGE_OFFSET; 338 initrd_end = initrd_start + ramdisk_size; 339 printk(KERN_INFO "Allocated new RAMDISK: [mem %#010llx-%#010llx]\n", 340 relocated_ramdisk, relocated_ramdisk + ramdisk_size - 1); 341 342 ret = copy_from_early_mem((void *)initrd_start, ramdisk_image, ramdisk_size); 343 if (ret) 344 panic("Copy RAMDISK failed\n"); 345 346 printk(KERN_INFO "Move RAMDISK from [mem %#010llx-%#010llx] to" 347 " [mem %#010llx-%#010llx]\n", 348 ramdisk_image, ramdisk_image + ramdisk_size - 1, 349 relocated_ramdisk, relocated_ramdisk + ramdisk_size - 1); 350 } 351 352 static void __init early_reserve_initrd(void) 353 { 354 /* Assume only end is not page aligned */ 355 u64 ramdisk_image = get_ramdisk_image(); 356 u64 ramdisk_size = get_ramdisk_size(); 357 u64 ramdisk_end = PAGE_ALIGN(ramdisk_image + ramdisk_size); 358 359 if (!boot_params.hdr.type_of_loader || 360 !ramdisk_image || !ramdisk_size) 361 return; /* No initrd provided by bootloader */ 362 363 memblock_reserve(ramdisk_image, ramdisk_end - ramdisk_image); 364 } 365 366 static void __init reserve_initrd(void) 367 { 368 /* Assume only end is not page aligned */ 369 u64 ramdisk_image = get_ramdisk_image(); 370 u64 ramdisk_size = get_ramdisk_size(); 371 u64 ramdisk_end = PAGE_ALIGN(ramdisk_image + ramdisk_size); 372 373 if (!boot_params.hdr.type_of_loader || 374 !ramdisk_image || !ramdisk_size) 375 return; /* No initrd provided by bootloader */ 376 377 initrd_start = 0; 378 379 printk(KERN_INFO "RAMDISK: [mem %#010llx-%#010llx]\n", ramdisk_image, 380 ramdisk_end - 1); 381 382 if (pfn_range_is_mapped(PFN_DOWN(ramdisk_image), 383 PFN_DOWN(ramdisk_end))) { 384 /* All are mapped, easy case */ 385 initrd_start = ramdisk_image + PAGE_OFFSET; 386 initrd_end = initrd_start + ramdisk_size; 387 return; 388 } 389 390 relocate_initrd(); 391 392 memblock_phys_free(ramdisk_image, ramdisk_end - ramdisk_image); 393 } 394 395 #else 396 static void __init early_reserve_initrd(void) 397 { 398 } 399 static void __init reserve_initrd(void) 400 { 401 } 402 #endif /* CONFIG_BLK_DEV_INITRD */ 403 404 static void __init add_early_ima_buffer(u64 phys_addr) 405 { 406 #ifdef CONFIG_IMA 407 struct ima_setup_data *data; 408 409 data = early_memremap(phys_addr + sizeof(struct setup_data), sizeof(*data)); 410 if (!data) { 411 pr_warn("setup: failed to memremap ima_setup_data entry\n"); 412 return; 413 } 414 415 if (data->size) { 416 memblock_reserve(data->addr, data->size); 417 ima_kexec_buffer_phys = data->addr; 418 ima_kexec_buffer_size = data->size; 419 } 420 421 early_memunmap(data, sizeof(*data)); 422 #else 423 pr_warn("Passed IMA kexec data, but CONFIG_IMA not set. Ignoring.\n"); 424 #endif 425 } 426 427 #if defined(CONFIG_HAVE_IMA_KEXEC) && !defined(CONFIG_OF_FLATTREE) 428 int __init ima_free_kexec_buffer(void) 429 { 430 if (!ima_kexec_buffer_size) 431 return -ENOENT; 432 433 memblock_free_late(ima_kexec_buffer_phys, 434 ima_kexec_buffer_size); 435 436 ima_kexec_buffer_phys = 0; 437 ima_kexec_buffer_size = 0; 438 439 return 0; 440 } 441 442 int __init ima_get_kexec_buffer(void **addr, size_t *size) 443 { 444 if (!ima_kexec_buffer_size) 445 return -ENOENT; 446 447 *addr = __va(ima_kexec_buffer_phys); 448 *size = ima_kexec_buffer_size; 449 450 return 0; 451 } 452 #endif 453 454 static void __init parse_setup_data(void) 455 { 456 struct setup_data *data; 457 u64 pa_data, pa_next; 458 459 pa_data = boot_params.hdr.setup_data; 460 while (pa_data) { 461 u32 data_len, data_type; 462 463 data = early_memremap(pa_data, sizeof(*data)); 464 data_len = data->len + sizeof(struct setup_data); 465 data_type = data->type; 466 pa_next = data->next; 467 early_memunmap(data, sizeof(*data)); 468 469 switch (data_type) { 470 case SETUP_E820_EXT: 471 e820__memory_setup_extended(pa_data, data_len); 472 break; 473 case SETUP_DTB: 474 add_dtb(pa_data); 475 break; 476 case SETUP_EFI: 477 parse_efi_setup(pa_data, data_len); 478 break; 479 case SETUP_IMA: 480 add_early_ima_buffer(pa_data); 481 break; 482 case SETUP_RNG_SEED: 483 data = early_memremap(pa_data, data_len); 484 add_bootloader_randomness(data->data, data->len); 485 /* Zero seed for forward secrecy. */ 486 memzero_explicit(data->data, data->len); 487 /* Zero length in case we find ourselves back here by accident. */ 488 memzero_explicit(&data->len, sizeof(data->len)); 489 early_memunmap(data, data_len); 490 break; 491 default: 492 break; 493 } 494 pa_data = pa_next; 495 } 496 } 497 498 /* 499 * Translate the fields of 'struct boot_param' into global variables 500 * representing these parameters. 501 */ 502 static void __init parse_boot_params(void) 503 { 504 ROOT_DEV = old_decode_dev(boot_params.hdr.root_dev); 505 screen_info = boot_params.screen_info; 506 edid_info = boot_params.edid_info; 507 #ifdef CONFIG_X86_32 508 apm_info.bios = boot_params.apm_bios_info; 509 ist_info = boot_params.ist_info; 510 #endif 511 saved_video_mode = boot_params.hdr.vid_mode; 512 bootloader_type = boot_params.hdr.type_of_loader; 513 if ((bootloader_type >> 4) == 0xe) { 514 bootloader_type &= 0xf; 515 bootloader_type |= (boot_params.hdr.ext_loader_type+0x10) << 4; 516 } 517 bootloader_version = bootloader_type & 0xf; 518 bootloader_version |= boot_params.hdr.ext_loader_ver << 4; 519 520 #ifdef CONFIG_BLK_DEV_RAM 521 rd_image_start = boot_params.hdr.ram_size & RAMDISK_IMAGE_START_MASK; 522 #endif 523 #ifdef CONFIG_EFI 524 if (!strncmp((char *)&boot_params.efi_info.efi_loader_signature, 525 EFI32_LOADER_SIGNATURE, 4)) { 526 set_bit(EFI_BOOT, &efi.flags); 527 } else if (!strncmp((char *)&boot_params.efi_info.efi_loader_signature, 528 EFI64_LOADER_SIGNATURE, 4)) { 529 set_bit(EFI_BOOT, &efi.flags); 530 set_bit(EFI_64BIT, &efi.flags); 531 } 532 #endif 533 534 if (!boot_params.hdr.root_flags) 535 root_mountflags &= ~MS_RDONLY; 536 } 537 538 static void __init memblock_x86_reserve_range_setup_data(void) 539 { 540 struct setup_indirect *indirect; 541 struct setup_data *data; 542 u64 pa_data, pa_next; 543 u32 len; 544 545 pa_data = boot_params.hdr.setup_data; 546 while (pa_data) { 547 data = early_memremap(pa_data, sizeof(*data)); 548 if (!data) { 549 pr_warn("setup: failed to memremap setup_data entry\n"); 550 return; 551 } 552 553 len = sizeof(*data); 554 pa_next = data->next; 555 556 memblock_reserve(pa_data, sizeof(*data) + data->len); 557 558 if (data->type == SETUP_INDIRECT) { 559 len += data->len; 560 early_memunmap(data, sizeof(*data)); 561 data = early_memremap(pa_data, len); 562 if (!data) { 563 pr_warn("setup: failed to memremap indirect setup_data\n"); 564 return; 565 } 566 567 indirect = (struct setup_indirect *)data->data; 568 569 if (indirect->type != SETUP_INDIRECT) 570 memblock_reserve(indirect->addr, indirect->len); 571 } 572 573 pa_data = pa_next; 574 early_memunmap(data, len); 575 } 576 } 577 578 static void __init arch_reserve_crashkernel(void) 579 { 580 unsigned long long crash_base, crash_size, low_size = 0; 581 bool high = false; 582 int ret; 583 584 if (!IS_ENABLED(CONFIG_CRASH_RESERVE)) 585 return; 586 587 ret = parse_crashkernel(boot_command_line, memblock_phys_mem_size(), 588 &crash_size, &crash_base, 589 &low_size, &high); 590 if (ret) 591 return; 592 593 if (xen_pv_domain()) { 594 pr_info("Ignoring crashkernel for a Xen PV domain\n"); 595 return; 596 } 597 598 reserve_crashkernel_generic(crash_size, crash_base, low_size, high); 599 } 600 601 static struct resource standard_io_resources[] = { 602 { .name = "dma1", .start = 0x00, .end = 0x1f, 603 .flags = IORESOURCE_BUSY | IORESOURCE_IO }, 604 { .name = "pic1", .start = 0x20, .end = 0x21, 605 .flags = IORESOURCE_BUSY | IORESOURCE_IO }, 606 { .name = "timer0", .start = 0x40, .end = 0x43, 607 .flags = IORESOURCE_BUSY | IORESOURCE_IO }, 608 { .name = "timer1", .start = 0x50, .end = 0x53, 609 .flags = IORESOURCE_BUSY | IORESOURCE_IO }, 610 { .name = "keyboard", .start = 0x60, .end = 0x60, 611 .flags = IORESOURCE_BUSY | IORESOURCE_IO }, 612 { .name = "keyboard", .start = 0x64, .end = 0x64, 613 .flags = IORESOURCE_BUSY | IORESOURCE_IO }, 614 { .name = "dma page reg", .start = 0x80, .end = 0x8f, 615 .flags = IORESOURCE_BUSY | IORESOURCE_IO }, 616 { .name = "pic2", .start = 0xa0, .end = 0xa1, 617 .flags = IORESOURCE_BUSY | IORESOURCE_IO }, 618 { .name = "dma2", .start = 0xc0, .end = 0xdf, 619 .flags = IORESOURCE_BUSY | IORESOURCE_IO }, 620 { .name = "fpu", .start = 0xf0, .end = 0xff, 621 .flags = IORESOURCE_BUSY | IORESOURCE_IO } 622 }; 623 624 void __init reserve_standard_io_resources(void) 625 { 626 int i; 627 628 /* request I/O space for devices used on all i[345]86 PCs */ 629 for (i = 0; i < ARRAY_SIZE(standard_io_resources); i++) 630 request_resource(&ioport_resource, &standard_io_resources[i]); 631 632 } 633 634 static void __init setup_kernel_resources(void) 635 { 636 code_resource.start = __pa_symbol(_text); 637 code_resource.end = __pa_symbol(_etext)-1; 638 rodata_resource.start = __pa_symbol(__start_rodata); 639 rodata_resource.end = __pa_symbol(__end_rodata)-1; 640 data_resource.start = __pa_symbol(_sdata); 641 data_resource.end = __pa_symbol(_edata)-1; 642 bss_resource.start = __pa_symbol(__bss_start); 643 bss_resource.end = __pa_symbol(__bss_stop)-1; 644 645 insert_resource(&iomem_resource, &code_resource); 646 insert_resource(&iomem_resource, &rodata_resource); 647 insert_resource(&iomem_resource, &data_resource); 648 insert_resource(&iomem_resource, &bss_resource); 649 } 650 651 static bool __init snb_gfx_workaround_needed(void) 652 { 653 #ifdef CONFIG_PCI 654 int i; 655 u16 vendor, devid; 656 static const __initconst u16 snb_ids[] = { 657 0x0102, 658 0x0112, 659 0x0122, 660 0x0106, 661 0x0116, 662 0x0126, 663 0x010a, 664 }; 665 666 /* Assume no if something weird is going on with PCI */ 667 if (!early_pci_allowed()) 668 return false; 669 670 vendor = read_pci_config_16(0, 2, 0, PCI_VENDOR_ID); 671 if (vendor != 0x8086) 672 return false; 673 674 devid = read_pci_config_16(0, 2, 0, PCI_DEVICE_ID); 675 for (i = 0; i < ARRAY_SIZE(snb_ids); i++) 676 if (devid == snb_ids[i]) 677 return true; 678 #endif 679 680 return false; 681 } 682 683 /* 684 * Sandy Bridge graphics has trouble with certain ranges, exclude 685 * them from allocation. 686 */ 687 static void __init trim_snb_memory(void) 688 { 689 static const __initconst unsigned long bad_pages[] = { 690 0x20050000, 691 0x20110000, 692 0x20130000, 693 0x20138000, 694 0x40004000, 695 }; 696 int i; 697 698 if (!snb_gfx_workaround_needed()) 699 return; 700 701 printk(KERN_DEBUG "reserving inaccessible SNB gfx pages\n"); 702 703 /* 704 * SandyBridge integrated graphics devices have a bug that prevents 705 * them from accessing certain memory ranges, namely anything below 706 * 1M and in the pages listed in bad_pages[] above. 707 * 708 * To avoid these pages being ever accessed by SNB gfx devices reserve 709 * bad_pages that have not already been reserved at boot time. 710 * All memory below the 1 MB mark is anyway reserved later during 711 * setup_arch(), so there is no need to reserve it here. 712 */ 713 714 for (i = 0; i < ARRAY_SIZE(bad_pages); i++) { 715 if (memblock_reserve(bad_pages[i], PAGE_SIZE)) 716 printk(KERN_WARNING "failed to reserve 0x%08lx\n", 717 bad_pages[i]); 718 } 719 } 720 721 static void __init trim_bios_range(void) 722 { 723 /* 724 * A special case is the first 4Kb of memory; 725 * This is a BIOS owned area, not kernel ram, but generally 726 * not listed as such in the E820 table. 727 * 728 * This typically reserves additional memory (64KiB by default) 729 * since some BIOSes are known to corrupt low memory. See the 730 * Kconfig help text for X86_RESERVE_LOW. 731 */ 732 e820__range_update(0, PAGE_SIZE, E820_TYPE_RAM, E820_TYPE_RESERVED); 733 734 /* 735 * special case: Some BIOSes report the PC BIOS 736 * area (640Kb -> 1Mb) as RAM even though it is not. 737 * take them out. 738 */ 739 e820__range_remove(BIOS_BEGIN, BIOS_END - BIOS_BEGIN, E820_TYPE_RAM, 1); 740 741 e820__update_table(e820_table); 742 } 743 744 /* called before trim_bios_range() to spare extra sanitize */ 745 static void __init e820_add_kernel_range(void) 746 { 747 u64 start = __pa_symbol(_text); 748 u64 size = __pa_symbol(_end) - start; 749 750 /* 751 * Complain if .text .data and .bss are not marked as E820_TYPE_RAM and 752 * attempt to fix it by adding the range. We may have a confused BIOS, 753 * or the user may have used memmap=exactmap or memmap=xxM$yyM to 754 * exclude kernel range. If we really are running on top non-RAM, 755 * we will crash later anyways. 756 */ 757 if (e820__mapped_all(start, start + size, E820_TYPE_RAM)) 758 return; 759 760 pr_warn(".text .data .bss are not marked as E820_TYPE_RAM!\n"); 761 e820__range_remove(start, size, E820_TYPE_RAM, 0); 762 e820__range_add(start, size, E820_TYPE_RAM); 763 } 764 765 static void __init early_reserve_memory(void) 766 { 767 /* 768 * Reserve the memory occupied by the kernel between _text and 769 * __end_of_kernel_reserve symbols. Any kernel sections after the 770 * __end_of_kernel_reserve symbol must be explicitly reserved with a 771 * separate memblock_reserve() or they will be discarded. 772 */ 773 memblock_reserve(__pa_symbol(_text), 774 (unsigned long)__end_of_kernel_reserve - (unsigned long)_text); 775 776 /* 777 * The first 4Kb of memory is a BIOS owned area, but generally it is 778 * not listed as such in the E820 table. 779 * 780 * Reserve the first 64K of memory since some BIOSes are known to 781 * corrupt low memory. After the real mode trampoline is allocated the 782 * rest of the memory below 640k is reserved. 783 * 784 * In addition, make sure page 0 is always reserved because on 785 * systems with L1TF its contents can be leaked to user processes. 786 */ 787 memblock_reserve(0, SZ_64K); 788 789 early_reserve_initrd(); 790 791 memblock_x86_reserve_range_setup_data(); 792 793 reserve_bios_regions(); 794 trim_snb_memory(); 795 } 796 797 /* 798 * Dump out kernel offset information on panic. 799 */ 800 static int 801 dump_kernel_offset(struct notifier_block *self, unsigned long v, void *p) 802 { 803 if (kaslr_enabled()) { 804 pr_emerg("Kernel Offset: 0x%lx from 0x%lx (relocation range: 0x%lx-0x%lx)\n", 805 kaslr_offset(), 806 __START_KERNEL, 807 __START_KERNEL_map, 808 MODULES_VADDR-1); 809 } else { 810 pr_emerg("Kernel Offset: disabled\n"); 811 } 812 813 return 0; 814 } 815 816 void x86_configure_nx(void) 817 { 818 if (boot_cpu_has(X86_FEATURE_NX)) 819 __supported_pte_mask |= _PAGE_NX; 820 else 821 __supported_pte_mask &= ~_PAGE_NX; 822 } 823 824 static void __init x86_report_nx(void) 825 { 826 if (!boot_cpu_has(X86_FEATURE_NX)) { 827 printk(KERN_NOTICE "Notice: NX (Execute Disable) protection " 828 "missing in CPU!\n"); 829 } else { 830 #if defined(CONFIG_X86_64) || defined(CONFIG_X86_PAE) 831 printk(KERN_INFO "NX (Execute Disable) protection: active\n"); 832 #else 833 /* 32bit non-PAE kernel, NX cannot be used */ 834 printk(KERN_NOTICE "Notice: NX (Execute Disable) protection " 835 "cannot be enabled: non-PAE kernel!\n"); 836 #endif 837 } 838 } 839 840 /* 841 * Determine if we were loaded by an EFI loader. If so, then we have also been 842 * passed the efi memmap, systab, etc., so we should use these data structures 843 * for initialization. Note, the efi init code path is determined by the 844 * global efi_enabled. This allows the same kernel image to be used on existing 845 * systems (with a traditional BIOS) as well as on EFI systems. 846 */ 847 /* 848 * setup_arch - architecture-specific boot-time initializations 849 * 850 * Note: On x86_64, fixmaps are ready for use even before this is called. 851 */ 852 853 void __init setup_arch(char **cmdline_p) 854 { 855 #ifdef CONFIG_X86_32 856 memcpy(&boot_cpu_data, &new_cpu_data, sizeof(new_cpu_data)); 857 858 /* 859 * copy kernel address range established so far and switch 860 * to the proper swapper page table 861 */ 862 clone_pgd_range(swapper_pg_dir + KERNEL_PGD_BOUNDARY, 863 initial_page_table + KERNEL_PGD_BOUNDARY, 864 KERNEL_PGD_PTRS); 865 866 load_cr3(swapper_pg_dir); 867 /* 868 * Note: Quark X1000 CPUs advertise PGE incorrectly and require 869 * a cr3 based tlb flush, so the following __flush_tlb_all() 870 * will not flush anything because the CPU quirk which clears 871 * X86_FEATURE_PGE has not been invoked yet. Though due to the 872 * load_cr3() above the TLB has been flushed already. The 873 * quirk is invoked before subsequent calls to __flush_tlb_all() 874 * so proper operation is guaranteed. 875 */ 876 __flush_tlb_all(); 877 #else 878 printk(KERN_INFO "Command line: %s\n", boot_command_line); 879 boot_cpu_data.x86_phys_bits = MAX_PHYSMEM_BITS; 880 #endif 881 882 #ifdef CONFIG_CMDLINE_BOOL 883 #ifdef CONFIG_CMDLINE_OVERRIDE 884 strscpy(boot_command_line, builtin_cmdline, COMMAND_LINE_SIZE); 885 #else 886 if (builtin_cmdline[0]) { 887 /* append boot loader cmdline to builtin */ 888 strlcat(builtin_cmdline, " ", COMMAND_LINE_SIZE); 889 strlcat(builtin_cmdline, boot_command_line, COMMAND_LINE_SIZE); 890 strscpy(boot_command_line, builtin_cmdline, COMMAND_LINE_SIZE); 891 } 892 #endif 893 builtin_cmdline_added = true; 894 #endif 895 896 strscpy(command_line, boot_command_line, COMMAND_LINE_SIZE); 897 *cmdline_p = command_line; 898 899 /* 900 * If we have OLPC OFW, we might end up relocating the fixmap due to 901 * reserve_top(), so do this before touching the ioremap area. 902 */ 903 olpc_ofw_detect(); 904 905 idt_setup_early_traps(); 906 early_cpu_init(); 907 jump_label_init(); 908 static_call_init(); 909 early_ioremap_init(); 910 911 setup_olpc_ofw_pgd(); 912 913 parse_boot_params(); 914 915 x86_init.oem.arch_setup(); 916 917 /* 918 * Do some memory reservations *before* memory is added to memblock, so 919 * memblock allocations won't overwrite it. 920 * 921 * After this point, everything still needed from the boot loader or 922 * firmware or kernel text should be early reserved or marked not RAM in 923 * e820. All other memory is free game. 924 * 925 * This call needs to happen before e820__memory_setup() which calls the 926 * xen_memory_setup() on Xen dom0 which relies on the fact that those 927 * early reservations have happened already. 928 */ 929 early_reserve_memory(); 930 931 iomem_resource.end = (1ULL << boot_cpu_data.x86_phys_bits) - 1; 932 e820__memory_setup(); 933 parse_setup_data(); 934 935 copy_edd(); 936 937 setup_initial_init_mm(_text, _etext, _edata, (void *)_brk_end); 938 939 /* 940 * x86_configure_nx() is called before parse_early_param() to detect 941 * whether hardware doesn't support NX (so that the early EHCI debug 942 * console setup can safely call set_fixmap()). 943 */ 944 x86_configure_nx(); 945 946 parse_early_param(); 947 948 if (efi_enabled(EFI_BOOT)) 949 efi_memblock_x86_reserve_range(); 950 951 x86_report_nx(); 952 953 apic_setup_apic_calls(); 954 955 if (acpi_mps_check()) { 956 #ifdef CONFIG_X86_LOCAL_APIC 957 apic_is_disabled = true; 958 #endif 959 setup_clear_cpu_cap(X86_FEATURE_APIC); 960 } 961 962 e820__finish_early_params(); 963 964 if (efi_enabled(EFI_BOOT)) 965 efi_init(); 966 967 reserve_ibft_region(); 968 x86_init.resources.dmi_setup(); 969 970 /* 971 * VMware detection requires dmi to be available, so this 972 * needs to be done after dmi_setup(), for the boot CPU. 973 * For some guest types (Xen PV, SEV-SNP, TDX) it is required to be 974 * called before cache_bp_init() for setting up MTRR state. 975 */ 976 init_hypervisor_platform(); 977 978 tsc_early_init(); 979 x86_init.resources.probe_roms(); 980 981 /* 982 * Add resources for kernel text and data to the iomem_resource. 983 * Do it after parse_early_param, so it can be debugged. 984 */ 985 setup_kernel_resources(); 986 987 e820_add_kernel_range(); 988 trim_bios_range(); 989 #ifdef CONFIG_X86_32 990 if (ppro_with_ram_bug()) { 991 e820__range_update(0x70000000ULL, 0x40000ULL, E820_TYPE_RAM, 992 E820_TYPE_RESERVED); 993 e820__update_table(e820_table); 994 printk(KERN_INFO "fixed physical RAM map:\n"); 995 e820__print_table("bad_ppro"); 996 } 997 #else 998 early_gart_iommu_check(); 999 #endif 1000 1001 /* 1002 * partially used pages are not usable - thus 1003 * we are rounding upwards: 1004 */ 1005 max_pfn = e820__end_of_ram_pfn(); 1006 1007 /* update e820 for memory not covered by WB MTRRs */ 1008 cache_bp_init(); 1009 if (mtrr_trim_uncached_memory(max_pfn)) 1010 max_pfn = e820__end_of_ram_pfn(); 1011 1012 max_possible_pfn = max_pfn; 1013 1014 /* 1015 * Define random base addresses for memory sections after max_pfn is 1016 * defined and before each memory section base is used. 1017 */ 1018 kernel_randomize_memory(); 1019 1020 #ifdef CONFIG_X86_32 1021 /* max_low_pfn get updated here */ 1022 find_low_pfn_range(); 1023 #else 1024 check_x2apic(); 1025 1026 /* How many end-of-memory variables you have, grandma! */ 1027 /* need this before calling reserve_initrd */ 1028 if (max_pfn > (1UL<<(32 - PAGE_SHIFT))) 1029 max_low_pfn = e820__end_of_low_ram_pfn(); 1030 else 1031 max_low_pfn = max_pfn; 1032 #endif 1033 1034 /* Find and reserve MPTABLE area */ 1035 x86_init.mpparse.find_mptable(); 1036 1037 early_alloc_pgt_buf(); 1038 1039 /* 1040 * Need to conclude brk, before e820__memblock_setup() 1041 * it could use memblock_find_in_range, could overlap with 1042 * brk area. 1043 */ 1044 reserve_brk(); 1045 1046 cleanup_highmap(); 1047 1048 e820__memblock_setup(); 1049 1050 /* 1051 * Needs to run after memblock setup because it needs the physical 1052 * memory size. 1053 */ 1054 mem_encrypt_setup_arch(); 1055 cc_random_init(); 1056 1057 efi_find_mirror(); 1058 efi_esrt_init(); 1059 efi_mokvar_table_init(); 1060 1061 /* 1062 * The EFI specification says that boot service code won't be 1063 * called after ExitBootServices(). This is, in fact, a lie. 1064 */ 1065 efi_reserve_boot_services(); 1066 1067 /* preallocate 4k for mptable mpc */ 1068 e820__memblock_alloc_reserved_mpc_new(); 1069 1070 #ifdef CONFIG_X86_CHECK_BIOS_CORRUPTION 1071 setup_bios_corruption_check(); 1072 #endif 1073 1074 #ifdef CONFIG_X86_32 1075 printk(KERN_DEBUG "initial memory mapped: [mem 0x00000000-%#010lx]\n", 1076 (max_pfn_mapped<<PAGE_SHIFT) - 1); 1077 #endif 1078 1079 /* 1080 * Find free memory for the real mode trampoline and place it there. If 1081 * there is not enough free memory under 1M, on EFI-enabled systems 1082 * there will be additional attempt to reclaim the memory for the real 1083 * mode trampoline at efi_free_boot_services(). 1084 * 1085 * Unconditionally reserve the entire first 1M of RAM because BIOSes 1086 * are known to corrupt low memory and several hundred kilobytes are not 1087 * worth complex detection what memory gets clobbered. Windows does the 1088 * same thing for very similar reasons. 1089 * 1090 * Moreover, on machines with SandyBridge graphics or in setups that use 1091 * crashkernel the entire 1M is reserved anyway. 1092 * 1093 * Note the host kernel TDX also requires the first 1MB being reserved. 1094 */ 1095 x86_platform.realmode_reserve(); 1096 1097 init_mem_mapping(); 1098 1099 /* 1100 * init_mem_mapping() relies on the early IDT page fault handling. 1101 * Now either enable FRED or install the real page fault handler 1102 * for 64-bit in the IDT. 1103 */ 1104 cpu_init_replace_early_idt(); 1105 1106 /* 1107 * Update mmu_cr4_features (and, indirectly, trampoline_cr4_features) 1108 * with the current CR4 value. This may not be necessary, but 1109 * auditing all the early-boot CR4 manipulation would be needed to 1110 * rule it out. 1111 * 1112 * Mask off features that don't work outside long mode (just 1113 * PCIDE for now). 1114 */ 1115 mmu_cr4_features = __read_cr4() & ~X86_CR4_PCIDE; 1116 1117 memblock_set_current_limit(get_max_mapped()); 1118 1119 /* 1120 * NOTE: On x86-32, only from this point on, fixmaps are ready for use. 1121 */ 1122 1123 #ifdef CONFIG_PROVIDE_OHCI1394_DMA_INIT 1124 if (init_ohci1394_dma_early) 1125 init_ohci1394_dma_on_all_controllers(); 1126 #endif 1127 /* Allocate bigger log buffer */ 1128 setup_log_buf(1); 1129 1130 if (efi_enabled(EFI_BOOT)) { 1131 switch (boot_params.secure_boot) { 1132 case efi_secureboot_mode_disabled: 1133 pr_info("Secure boot disabled\n"); 1134 break; 1135 case efi_secureboot_mode_enabled: 1136 pr_info("Secure boot enabled\n"); 1137 break; 1138 default: 1139 pr_info("Secure boot could not be determined\n"); 1140 break; 1141 } 1142 } 1143 1144 reserve_initrd(); 1145 1146 acpi_table_upgrade(); 1147 /* Look for ACPI tables and reserve memory occupied by them. */ 1148 acpi_boot_table_init(); 1149 1150 vsmp_init(); 1151 1152 io_delay_init(); 1153 1154 early_platform_quirks(); 1155 1156 /* Some platforms need the APIC registered for NUMA configuration */ 1157 early_acpi_boot_init(); 1158 x86_init.mpparse.early_parse_smp_cfg(); 1159 1160 x86_flattree_get_config(); 1161 1162 initmem_init(); 1163 dma_contiguous_reserve(max_pfn_mapped << PAGE_SHIFT); 1164 1165 if (boot_cpu_has(X86_FEATURE_GBPAGES)) { 1166 hugetlb_cma_reserve(PUD_SHIFT - PAGE_SHIFT); 1167 hugetlb_bootmem_alloc(); 1168 } 1169 1170 /* 1171 * Reserve memory for crash kernel after SRAT is parsed so that it 1172 * won't consume hotpluggable memory. 1173 */ 1174 arch_reserve_crashkernel(); 1175 1176 if (!early_xdbc_setup_hardware()) 1177 early_xdbc_register_console(); 1178 1179 x86_init.paging.pagetable_init(); 1180 1181 kasan_init(); 1182 1183 /* 1184 * Sync back kernel address range. 1185 * 1186 * FIXME: Can the later sync in setup_cpu_entry_areas() replace 1187 * this call? 1188 */ 1189 sync_initial_page_table(); 1190 1191 tboot_probe(); 1192 1193 map_vsyscall(); 1194 1195 x86_32_probe_apic(); 1196 1197 early_quirks(); 1198 1199 topology_apply_cmdline_limits_early(); 1200 1201 /* 1202 * Parse SMP configuration. Try ACPI first and then the platform 1203 * specific parser. 1204 */ 1205 acpi_boot_init(); 1206 x86_init.mpparse.parse_smp_cfg(); 1207 1208 /* Last opportunity to detect and map the local APIC */ 1209 init_apic_mappings(); 1210 1211 topology_init_possible_cpus(); 1212 1213 init_cpu_to_node(); 1214 init_gi_nodes(); 1215 1216 io_apic_init_mappings(); 1217 1218 x86_init.hyper.guest_late_init(); 1219 1220 e820__reserve_resources(); 1221 e820__register_nosave_regions(max_pfn); 1222 1223 x86_init.resources.reserve_resources(); 1224 1225 e820__setup_pci_gap(); 1226 1227 #ifdef CONFIG_VT 1228 #if defined(CONFIG_VGA_CONSOLE) 1229 if (!efi_enabled(EFI_BOOT) || (efi_mem_type(0xa0000) != EFI_CONVENTIONAL_MEMORY)) 1230 vgacon_register_screen(&screen_info); 1231 #endif 1232 #endif 1233 x86_init.oem.banner(); 1234 1235 x86_init.timers.wallclock_init(); 1236 1237 /* 1238 * This needs to run before setup_local_APIC() which soft-disables the 1239 * local APIC temporarily and that masks the thermal LVT interrupt, 1240 * leading to softlockups on machines which have configured SMI 1241 * interrupt delivery. 1242 */ 1243 therm_lvt_init(); 1244 1245 mcheck_init(); 1246 1247 register_refined_jiffies(CLOCK_TICK_RATE); 1248 1249 #ifdef CONFIG_EFI 1250 if (efi_enabled(EFI_BOOT)) 1251 efi_apply_memmap_quirks(); 1252 #endif 1253 1254 unwind_init(); 1255 } 1256 1257 #ifdef CONFIG_X86_32 1258 1259 static struct resource video_ram_resource = { 1260 .name = "Video RAM area", 1261 .start = 0xa0000, 1262 .end = 0xbffff, 1263 .flags = IORESOURCE_BUSY | IORESOURCE_MEM 1264 }; 1265 1266 void __init i386_reserve_resources(void) 1267 { 1268 request_resource(&iomem_resource, &video_ram_resource); 1269 reserve_standard_io_resources(); 1270 } 1271 1272 #endif /* CONFIG_X86_32 */ 1273 1274 static struct notifier_block kernel_offset_notifier = { 1275 .notifier_call = dump_kernel_offset 1276 }; 1277 1278 static int __init register_kernel_offset_dumper(void) 1279 { 1280 atomic_notifier_chain_register(&panic_notifier_list, 1281 &kernel_offset_notifier); 1282 return 0; 1283 } 1284 __initcall(register_kernel_offset_dumper); 1285 1286 #ifdef CONFIG_HOTPLUG_CPU 1287 bool arch_cpu_is_hotpluggable(int cpu) 1288 { 1289 return cpu > 0; 1290 } 1291 #endif /* CONFIG_HOTPLUG_CPU */ 1292