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