xref: /linux/arch/x86/kernel/setup.c (revision adc574269bca8dbde9f8f22841e40d3380241921)
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 static void __init memblock_x86_reserve_range_setup_data(void)
499 {
500 	struct setup_indirect *indirect;
501 	struct setup_data *data;
502 	u64 pa_data, pa_next;
503 	u32 len;
504 
505 	pa_data = boot_params.hdr.setup_data;
506 	while (pa_data) {
507 		data = early_memremap(pa_data, sizeof(*data));
508 		if (!data) {
509 			pr_warn("setup: failed to memremap setup_data entry\n");
510 			return;
511 		}
512 
513 		len = sizeof(*data);
514 		pa_next = data->next;
515 
516 		memblock_reserve(pa_data, sizeof(*data) + data->len);
517 
518 		if (data->type == SETUP_INDIRECT) {
519 			len += data->len;
520 			early_memunmap(data, sizeof(*data));
521 			data = early_memremap(pa_data, len);
522 			if (!data) {
523 				pr_warn("setup: failed to memremap indirect setup_data\n");
524 				return;
525 			}
526 
527 			indirect = (struct setup_indirect *)data->data;
528 
529 			if (indirect->type != SETUP_INDIRECT)
530 				memblock_reserve(indirect->addr, indirect->len);
531 		}
532 
533 		pa_data = pa_next;
534 		early_memunmap(data, len);
535 	}
536 }
537 
538 static void __init arch_reserve_crashkernel(void)
539 {
540 	unsigned long long crash_base, crash_size, low_size = 0;
541 	char *cmdline = boot_command_line;
542 	bool high = false;
543 	int ret;
544 
545 	if (!IS_ENABLED(CONFIG_CRASH_RESERVE))
546 		return;
547 
548 	ret = parse_crashkernel(cmdline, memblock_phys_mem_size(),
549 				&crash_size, &crash_base,
550 				&low_size, &high);
551 	if (ret)
552 		return;
553 
554 	if (xen_pv_domain()) {
555 		pr_info("Ignoring crashkernel for a Xen PV domain\n");
556 		return;
557 	}
558 
559 	reserve_crashkernel_generic(cmdline, crash_size, crash_base,
560 				    low_size, high);
561 }
562 
563 static struct resource standard_io_resources[] = {
564 	{ .name = "dma1", .start = 0x00, .end = 0x1f,
565 		.flags = IORESOURCE_BUSY | IORESOURCE_IO },
566 	{ .name = "pic1", .start = 0x20, .end = 0x21,
567 		.flags = IORESOURCE_BUSY | IORESOURCE_IO },
568 	{ .name = "timer0", .start = 0x40, .end = 0x43,
569 		.flags = IORESOURCE_BUSY | IORESOURCE_IO },
570 	{ .name = "timer1", .start = 0x50, .end = 0x53,
571 		.flags = IORESOURCE_BUSY | IORESOURCE_IO },
572 	{ .name = "keyboard", .start = 0x60, .end = 0x60,
573 		.flags = IORESOURCE_BUSY | IORESOURCE_IO },
574 	{ .name = "keyboard", .start = 0x64, .end = 0x64,
575 		.flags = IORESOURCE_BUSY | IORESOURCE_IO },
576 	{ .name = "dma page reg", .start = 0x80, .end = 0x8f,
577 		.flags = IORESOURCE_BUSY | IORESOURCE_IO },
578 	{ .name = "pic2", .start = 0xa0, .end = 0xa1,
579 		.flags = IORESOURCE_BUSY | IORESOURCE_IO },
580 	{ .name = "dma2", .start = 0xc0, .end = 0xdf,
581 		.flags = IORESOURCE_BUSY | IORESOURCE_IO },
582 	{ .name = "fpu", .start = 0xf0, .end = 0xff,
583 		.flags = IORESOURCE_BUSY | IORESOURCE_IO }
584 };
585 
586 void __init reserve_standard_io_resources(void)
587 {
588 	int i;
589 
590 	/* request I/O space for devices used on all i[345]86 PCs */
591 	for (i = 0; i < ARRAY_SIZE(standard_io_resources); i++)
592 		request_resource(&ioport_resource, &standard_io_resources[i]);
593 
594 }
595 
596 static bool __init snb_gfx_workaround_needed(void)
597 {
598 #ifdef CONFIG_PCI
599 	int i;
600 	u16 vendor, devid;
601 	static const __initconst u16 snb_ids[] = {
602 		0x0102,
603 		0x0112,
604 		0x0122,
605 		0x0106,
606 		0x0116,
607 		0x0126,
608 		0x010a,
609 	};
610 
611 	/* Assume no if something weird is going on with PCI */
612 	if (!early_pci_allowed())
613 		return false;
614 
615 	vendor = read_pci_config_16(0, 2, 0, PCI_VENDOR_ID);
616 	if (vendor != 0x8086)
617 		return false;
618 
619 	devid = read_pci_config_16(0, 2, 0, PCI_DEVICE_ID);
620 	for (i = 0; i < ARRAY_SIZE(snb_ids); i++)
621 		if (devid == snb_ids[i])
622 			return true;
623 #endif
624 
625 	return false;
626 }
627 
628 /*
629  * Sandy Bridge graphics has trouble with certain ranges, exclude
630  * them from allocation.
631  */
632 static void __init trim_snb_memory(void)
633 {
634 	static const __initconst unsigned long bad_pages[] = {
635 		0x20050000,
636 		0x20110000,
637 		0x20130000,
638 		0x20138000,
639 		0x40004000,
640 	};
641 	int i;
642 
643 	if (!snb_gfx_workaround_needed())
644 		return;
645 
646 	printk(KERN_DEBUG "reserving inaccessible SNB gfx pages\n");
647 
648 	/*
649 	 * SandyBridge integrated graphics devices have a bug that prevents
650 	 * them from accessing certain memory ranges, namely anything below
651 	 * 1M and in the pages listed in bad_pages[] above.
652 	 *
653 	 * To avoid these pages being ever accessed by SNB gfx devices reserve
654 	 * bad_pages that have not already been reserved at boot time.
655 	 * All memory below the 1 MB mark is anyway reserved later during
656 	 * setup_arch(), so there is no need to reserve it here.
657 	 */
658 
659 	for (i = 0; i < ARRAY_SIZE(bad_pages); i++) {
660 		if (memblock_reserve(bad_pages[i], PAGE_SIZE))
661 			printk(KERN_WARNING "failed to reserve 0x%08lx\n",
662 			       bad_pages[i]);
663 	}
664 }
665 
666 static void __init trim_bios_range(void)
667 {
668 	/*
669 	 * A special case is the first 4Kb of memory;
670 	 * This is a BIOS owned area, not kernel ram, but generally
671 	 * not listed as such in the E820 table.
672 	 *
673 	 * This typically reserves additional memory (64KiB by default)
674 	 * since some BIOSes are known to corrupt low memory.  See the
675 	 * Kconfig help text for X86_RESERVE_LOW.
676 	 */
677 	e820__range_update(0, PAGE_SIZE, E820_TYPE_RAM, E820_TYPE_RESERVED);
678 
679 	/*
680 	 * special case: Some BIOSes report the PC BIOS
681 	 * area (640Kb -> 1Mb) as RAM even though it is not.
682 	 * take them out.
683 	 */
684 	e820__range_remove(BIOS_BEGIN, BIOS_END - BIOS_BEGIN, E820_TYPE_RAM, 1);
685 
686 	e820__update_table(e820_table);
687 }
688 
689 /* called before trim_bios_range() to spare extra sanitize */
690 static void __init e820_add_kernel_range(void)
691 {
692 	u64 start = __pa_symbol(_text);
693 	u64 size = __pa_symbol(_end) - start;
694 
695 	/*
696 	 * Complain if .text .data and .bss are not marked as E820_TYPE_RAM and
697 	 * attempt to fix it by adding the range. We may have a confused BIOS,
698 	 * or the user may have used memmap=exactmap or memmap=xxM$yyM to
699 	 * exclude kernel range. If we really are running on top non-RAM,
700 	 * we will crash later anyways.
701 	 */
702 	if (e820__mapped_all(start, start + size, E820_TYPE_RAM))
703 		return;
704 
705 	pr_warn(".text .data .bss are not marked as E820_TYPE_RAM!\n");
706 	e820__range_remove(start, size, E820_TYPE_RAM, 0);
707 	e820__range_add(start, size, E820_TYPE_RAM);
708 }
709 
710 static void __init early_reserve_memory(void)
711 {
712 	/*
713 	 * Reserve the memory occupied by the kernel between _text and
714 	 * __end_of_kernel_reserve symbols. Any kernel sections after the
715 	 * __end_of_kernel_reserve symbol must be explicitly reserved with a
716 	 * separate memblock_reserve() or they will be discarded.
717 	 */
718 	memblock_reserve(__pa_symbol(_text),
719 			 (unsigned long)__end_of_kernel_reserve - (unsigned long)_text);
720 
721 	/*
722 	 * The first 4Kb of memory is a BIOS owned area, but generally it is
723 	 * not listed as such in the E820 table.
724 	 *
725 	 * Reserve the first 64K of memory since some BIOSes are known to
726 	 * corrupt low memory. After the real mode trampoline is allocated the
727 	 * rest of the memory below 640k is reserved.
728 	 *
729 	 * In addition, make sure page 0 is always reserved because on
730 	 * systems with L1TF its contents can be leaked to user processes.
731 	 */
732 	memblock_reserve(0, SZ_64K);
733 
734 	early_reserve_initrd();
735 
736 	memblock_x86_reserve_range_setup_data();
737 
738 	reserve_bios_regions();
739 	trim_snb_memory();
740 }
741 
742 /*
743  * Dump out kernel offset information on panic.
744  */
745 static int
746 dump_kernel_offset(struct notifier_block *self, unsigned long v, void *p)
747 {
748 	if (kaslr_enabled()) {
749 		pr_emerg("Kernel Offset: 0x%lx from 0x%lx (relocation range: 0x%lx-0x%lx)\n",
750 			 kaslr_offset(),
751 			 __START_KERNEL,
752 			 __START_KERNEL_map,
753 			 MODULES_VADDR-1);
754 	} else {
755 		pr_emerg("Kernel Offset: disabled\n");
756 	}
757 
758 	return 0;
759 }
760 
761 void x86_configure_nx(void)
762 {
763 	if (boot_cpu_has(X86_FEATURE_NX))
764 		__supported_pte_mask |= _PAGE_NX;
765 	else
766 		__supported_pte_mask &= ~_PAGE_NX;
767 }
768 
769 static void __init x86_report_nx(void)
770 {
771 	if (!boot_cpu_has(X86_FEATURE_NX)) {
772 		printk(KERN_NOTICE "Notice: NX (Execute Disable) protection "
773 		       "missing in CPU!\n");
774 	} else {
775 #if defined(CONFIG_X86_64) || defined(CONFIG_X86_PAE)
776 		printk(KERN_INFO "NX (Execute Disable) protection: active\n");
777 #else
778 		/* 32bit non-PAE kernel, NX cannot be used */
779 		printk(KERN_NOTICE "Notice: NX (Execute Disable) protection "
780 		       "cannot be enabled: non-PAE kernel!\n");
781 #endif
782 	}
783 }
784 
785 /*
786  * Determine if we were loaded by an EFI loader.  If so, then we have also been
787  * passed the efi memmap, systab, etc., so we should use these data structures
788  * for initialization.  Note, the efi init code path is determined by the
789  * global efi_enabled. This allows the same kernel image to be used on existing
790  * systems (with a traditional BIOS) as well as on EFI systems.
791  */
792 /*
793  * setup_arch - architecture-specific boot-time initializations
794  *
795  * Note: On x86_64, fixmaps are ready for use even before this is called.
796  */
797 
798 void __init setup_arch(char **cmdline_p)
799 {
800 #ifdef CONFIG_X86_32
801 	memcpy(&boot_cpu_data, &new_cpu_data, sizeof(new_cpu_data));
802 
803 	/*
804 	 * copy kernel address range established so far and switch
805 	 * to the proper swapper page table
806 	 */
807 	clone_pgd_range(swapper_pg_dir     + KERNEL_PGD_BOUNDARY,
808 			initial_page_table + KERNEL_PGD_BOUNDARY,
809 			KERNEL_PGD_PTRS);
810 
811 	load_cr3(swapper_pg_dir);
812 	/*
813 	 * Note: Quark X1000 CPUs advertise PGE incorrectly and require
814 	 * a cr3 based tlb flush, so the following __flush_tlb_all()
815 	 * will not flush anything because the CPU quirk which clears
816 	 * X86_FEATURE_PGE has not been invoked yet. Though due to the
817 	 * load_cr3() above the TLB has been flushed already. The
818 	 * quirk is invoked before subsequent calls to __flush_tlb_all()
819 	 * so proper operation is guaranteed.
820 	 */
821 	__flush_tlb_all();
822 #else
823 	printk(KERN_INFO "Command line: %s\n", boot_command_line);
824 	boot_cpu_data.x86_phys_bits = MAX_PHYSMEM_BITS;
825 #endif
826 
827 #ifdef CONFIG_CMDLINE_BOOL
828 #ifdef CONFIG_CMDLINE_OVERRIDE
829 	strscpy(boot_command_line, builtin_cmdline, COMMAND_LINE_SIZE);
830 #else
831 	if (builtin_cmdline[0]) {
832 		/* append boot loader cmdline to builtin */
833 		strlcat(builtin_cmdline, " ", COMMAND_LINE_SIZE);
834 		strlcat(builtin_cmdline, boot_command_line, COMMAND_LINE_SIZE);
835 		strscpy(boot_command_line, builtin_cmdline, COMMAND_LINE_SIZE);
836 	}
837 #endif
838 	builtin_cmdline_added = true;
839 #endif
840 
841 	strscpy(command_line, boot_command_line, COMMAND_LINE_SIZE);
842 	*cmdline_p = command_line;
843 
844 	/*
845 	 * If we have OLPC OFW, we might end up relocating the fixmap due to
846 	 * reserve_top(), so do this before touching the ioremap area.
847 	 */
848 	olpc_ofw_detect();
849 
850 	idt_setup_early_traps();
851 	early_cpu_init();
852 	jump_label_init();
853 	static_call_init();
854 	early_ioremap_init();
855 
856 	setup_olpc_ofw_pgd();
857 
858 	ROOT_DEV = old_decode_dev(boot_params.hdr.root_dev);
859 	screen_info = boot_params.screen_info;
860 	edid_info = boot_params.edid_info;
861 #ifdef CONFIG_X86_32
862 	apm_info.bios = boot_params.apm_bios_info;
863 	ist_info = boot_params.ist_info;
864 #endif
865 	saved_video_mode = boot_params.hdr.vid_mode;
866 	bootloader_type = boot_params.hdr.type_of_loader;
867 	if ((bootloader_type >> 4) == 0xe) {
868 		bootloader_type &= 0xf;
869 		bootloader_type |= (boot_params.hdr.ext_loader_type+0x10) << 4;
870 	}
871 	bootloader_version  = bootloader_type & 0xf;
872 	bootloader_version |= boot_params.hdr.ext_loader_ver << 4;
873 
874 #ifdef CONFIG_BLK_DEV_RAM
875 	rd_image_start = boot_params.hdr.ram_size & RAMDISK_IMAGE_START_MASK;
876 #endif
877 #ifdef CONFIG_EFI
878 	if (!strncmp((char *)&boot_params.efi_info.efi_loader_signature,
879 		     EFI32_LOADER_SIGNATURE, 4)) {
880 		set_bit(EFI_BOOT, &efi.flags);
881 	} else if (!strncmp((char *)&boot_params.efi_info.efi_loader_signature,
882 		     EFI64_LOADER_SIGNATURE, 4)) {
883 		set_bit(EFI_BOOT, &efi.flags);
884 		set_bit(EFI_64BIT, &efi.flags);
885 	}
886 #endif
887 
888 	x86_init.oem.arch_setup();
889 
890 	/*
891 	 * Do some memory reservations *before* memory is added to memblock, so
892 	 * memblock allocations won't overwrite it.
893 	 *
894 	 * After this point, everything still needed from the boot loader or
895 	 * firmware or kernel text should be early reserved or marked not RAM in
896 	 * e820. All other memory is free game.
897 	 *
898 	 * This call needs to happen before e820__memory_setup() which calls the
899 	 * xen_memory_setup() on Xen dom0 which relies on the fact that those
900 	 * early reservations have happened already.
901 	 */
902 	early_reserve_memory();
903 
904 	iomem_resource.end = (1ULL << boot_cpu_data.x86_phys_bits) - 1;
905 	e820__memory_setup();
906 	parse_setup_data();
907 
908 	copy_edd();
909 
910 	if (!boot_params.hdr.root_flags)
911 		root_mountflags &= ~MS_RDONLY;
912 	setup_initial_init_mm(_text, _etext, _edata, (void *)_brk_end);
913 
914 	code_resource.start = __pa_symbol(_text);
915 	code_resource.end = __pa_symbol(_etext)-1;
916 	rodata_resource.start = __pa_symbol(__start_rodata);
917 	rodata_resource.end = __pa_symbol(__end_rodata)-1;
918 	data_resource.start = __pa_symbol(_sdata);
919 	data_resource.end = __pa_symbol(_edata)-1;
920 	bss_resource.start = __pa_symbol(__bss_start);
921 	bss_resource.end = __pa_symbol(__bss_stop)-1;
922 
923 	/*
924 	 * x86_configure_nx() is called before parse_early_param() to detect
925 	 * whether hardware doesn't support NX (so that the early EHCI debug
926 	 * console setup can safely call set_fixmap()).
927 	 */
928 	x86_configure_nx();
929 
930 	parse_early_param();
931 
932 	if (efi_enabled(EFI_BOOT))
933 		efi_memblock_x86_reserve_range();
934 
935 #ifdef CONFIG_MEMORY_HOTPLUG
936 	/*
937 	 * Memory used by the kernel cannot be hot-removed because Linux
938 	 * cannot migrate the kernel pages. When memory hotplug is
939 	 * enabled, we should prevent memblock from allocating memory
940 	 * for the kernel.
941 	 *
942 	 * ACPI SRAT records all hotpluggable memory ranges. But before
943 	 * SRAT is parsed, we don't know about it.
944 	 *
945 	 * The kernel image is loaded into memory at very early time. We
946 	 * cannot prevent this anyway. So on NUMA system, we set any
947 	 * node the kernel resides in as un-hotpluggable.
948 	 *
949 	 * Since on modern servers, one node could have double-digit
950 	 * gigabytes memory, we can assume the memory around the kernel
951 	 * image is also un-hotpluggable. So before SRAT is parsed, just
952 	 * allocate memory near the kernel image to try the best to keep
953 	 * the kernel away from hotpluggable memory.
954 	 */
955 	if (movable_node_is_enabled())
956 		memblock_set_bottom_up(true);
957 #endif
958 
959 	x86_report_nx();
960 
961 	apic_setup_apic_calls();
962 
963 	if (acpi_mps_check()) {
964 #ifdef CONFIG_X86_LOCAL_APIC
965 		apic_is_disabled = true;
966 #endif
967 		setup_clear_cpu_cap(X86_FEATURE_APIC);
968 	}
969 
970 	e820__reserve_setup_data();
971 	e820__finish_early_params();
972 
973 	if (efi_enabled(EFI_BOOT))
974 		efi_init();
975 
976 	reserve_ibft_region();
977 	x86_init.resources.dmi_setup();
978 
979 	/*
980 	 * VMware detection requires dmi to be available, so this
981 	 * needs to be done after dmi_setup(), for the boot CPU.
982 	 * For some guest types (Xen PV, SEV-SNP, TDX) it is required to be
983 	 * called before cache_bp_init() for setting up MTRR state.
984 	 */
985 	init_hypervisor_platform();
986 
987 	tsc_early_init();
988 	x86_init.resources.probe_roms();
989 
990 	/* after parse_early_param, so could debug it */
991 	insert_resource(&iomem_resource, &code_resource);
992 	insert_resource(&iomem_resource, &rodata_resource);
993 	insert_resource(&iomem_resource, &data_resource);
994 	insert_resource(&iomem_resource, &bss_resource);
995 
996 	e820_add_kernel_range();
997 	trim_bios_range();
998 #ifdef CONFIG_X86_32
999 	if (ppro_with_ram_bug()) {
1000 		e820__range_update(0x70000000ULL, 0x40000ULL, E820_TYPE_RAM,
1001 				  E820_TYPE_RESERVED);
1002 		e820__update_table(e820_table);
1003 		printk(KERN_INFO "fixed physical RAM map:\n");
1004 		e820__print_table("bad_ppro");
1005 	}
1006 #else
1007 	early_gart_iommu_check();
1008 #endif
1009 
1010 	/*
1011 	 * partially used pages are not usable - thus
1012 	 * we are rounding upwards:
1013 	 */
1014 	max_pfn = e820__end_of_ram_pfn();
1015 
1016 	/* update e820 for memory not covered by WB MTRRs */
1017 	cache_bp_init();
1018 	if (mtrr_trim_uncached_memory(max_pfn))
1019 		max_pfn = e820__end_of_ram_pfn();
1020 
1021 	max_possible_pfn = max_pfn;
1022 
1023 	/*
1024 	 * Define random base addresses for memory sections after max_pfn is
1025 	 * defined and before each memory section base is used.
1026 	 */
1027 	kernel_randomize_memory();
1028 
1029 #ifdef CONFIG_X86_32
1030 	/* max_low_pfn get updated here */
1031 	find_low_pfn_range();
1032 #else
1033 	check_x2apic();
1034 
1035 	/* How many end-of-memory variables you have, grandma! */
1036 	/* need this before calling reserve_initrd */
1037 	if (max_pfn > (1UL<<(32 - PAGE_SHIFT)))
1038 		max_low_pfn = e820__end_of_low_ram_pfn();
1039 	else
1040 		max_low_pfn = max_pfn;
1041 
1042 	high_memory = (void *)__va(max_pfn * PAGE_SIZE - 1) + 1;
1043 #endif
1044 
1045 	/* Find and reserve MPTABLE area */
1046 	x86_init.mpparse.find_mptable();
1047 
1048 	early_alloc_pgt_buf();
1049 
1050 	/*
1051 	 * Need to conclude brk, before e820__memblock_setup()
1052 	 * it could use memblock_find_in_range, could overlap with
1053 	 * brk area.
1054 	 */
1055 	reserve_brk();
1056 
1057 	cleanup_highmap();
1058 
1059 	memblock_set_current_limit(ISA_END_ADDRESS);
1060 	e820__memblock_setup();
1061 
1062 	/*
1063 	 * Needs to run after memblock setup because it needs the physical
1064 	 * memory size.
1065 	 */
1066 	mem_encrypt_setup_arch();
1067 	cc_random_init();
1068 
1069 	efi_find_mirror();
1070 	efi_esrt_init();
1071 	efi_mokvar_table_init();
1072 
1073 	/*
1074 	 * The EFI specification says that boot service code won't be
1075 	 * called after ExitBootServices(). This is, in fact, a lie.
1076 	 */
1077 	efi_reserve_boot_services();
1078 
1079 	/* preallocate 4k for mptable mpc */
1080 	e820__memblock_alloc_reserved_mpc_new();
1081 
1082 #ifdef CONFIG_X86_CHECK_BIOS_CORRUPTION
1083 	setup_bios_corruption_check();
1084 #endif
1085 
1086 #ifdef CONFIG_X86_32
1087 	printk(KERN_DEBUG "initial memory mapped: [mem 0x00000000-%#010lx]\n",
1088 			(max_pfn_mapped<<PAGE_SHIFT) - 1);
1089 #endif
1090 
1091 	/*
1092 	 * Find free memory for the real mode trampoline and place it there. If
1093 	 * there is not enough free memory under 1M, on EFI-enabled systems
1094 	 * there will be additional attempt to reclaim the memory for the real
1095 	 * mode trampoline at efi_free_boot_services().
1096 	 *
1097 	 * Unconditionally reserve the entire first 1M of RAM because BIOSes
1098 	 * are known to corrupt low memory and several hundred kilobytes are not
1099 	 * worth complex detection what memory gets clobbered. Windows does the
1100 	 * same thing for very similar reasons.
1101 	 *
1102 	 * Moreover, on machines with SandyBridge graphics or in setups that use
1103 	 * crashkernel the entire 1M is reserved anyway.
1104 	 *
1105 	 * Note the host kernel TDX also requires the first 1MB being reserved.
1106 	 */
1107 	x86_platform.realmode_reserve();
1108 
1109 	init_mem_mapping();
1110 
1111 	/*
1112 	 * init_mem_mapping() relies on the early IDT page fault handling.
1113 	 * Now either enable FRED or install the real page fault handler
1114 	 * for 64-bit in the IDT.
1115 	 */
1116 	cpu_init_replace_early_idt();
1117 
1118 	/*
1119 	 * Update mmu_cr4_features (and, indirectly, trampoline_cr4_features)
1120 	 * with the current CR4 value.  This may not be necessary, but
1121 	 * auditing all the early-boot CR4 manipulation would be needed to
1122 	 * rule it out.
1123 	 *
1124 	 * Mask off features that don't work outside long mode (just
1125 	 * PCIDE for now).
1126 	 */
1127 	mmu_cr4_features = __read_cr4() & ~X86_CR4_PCIDE;
1128 
1129 	memblock_set_current_limit(get_max_mapped());
1130 
1131 	/*
1132 	 * NOTE: On x86-32, only from this point on, fixmaps are ready for use.
1133 	 */
1134 
1135 #ifdef CONFIG_PROVIDE_OHCI1394_DMA_INIT
1136 	if (init_ohci1394_dma_early)
1137 		init_ohci1394_dma_on_all_controllers();
1138 #endif
1139 	/* Allocate bigger log buffer */
1140 	setup_log_buf(1);
1141 
1142 	if (efi_enabled(EFI_BOOT)) {
1143 		switch (boot_params.secure_boot) {
1144 		case efi_secureboot_mode_disabled:
1145 			pr_info("Secure boot disabled\n");
1146 			break;
1147 		case efi_secureboot_mode_enabled:
1148 			pr_info("Secure boot enabled\n");
1149 			break;
1150 		default:
1151 			pr_info("Secure boot could not be determined\n");
1152 			break;
1153 		}
1154 	}
1155 
1156 	reserve_initrd();
1157 
1158 	acpi_table_upgrade();
1159 	/* Look for ACPI tables and reserve memory occupied by them. */
1160 	acpi_boot_table_init();
1161 
1162 	vsmp_init();
1163 
1164 	io_delay_init();
1165 
1166 	early_platform_quirks();
1167 
1168 	/* Some platforms need the APIC registered for NUMA configuration */
1169 	early_acpi_boot_init();
1170 	x86_init.mpparse.early_parse_smp_cfg();
1171 
1172 	x86_flattree_get_config();
1173 
1174 	initmem_init();
1175 	dma_contiguous_reserve(max_pfn_mapped << PAGE_SHIFT);
1176 
1177 	if (boot_cpu_has(X86_FEATURE_GBPAGES))
1178 		hugetlb_cma_reserve(PUD_SHIFT - PAGE_SHIFT);
1179 
1180 	/*
1181 	 * Reserve memory for crash kernel after SRAT is parsed so that it
1182 	 * won't consume hotpluggable memory.
1183 	 */
1184 	arch_reserve_crashkernel();
1185 
1186 	if (!early_xdbc_setup_hardware())
1187 		early_xdbc_register_console();
1188 
1189 	x86_init.paging.pagetable_init();
1190 
1191 	kasan_init();
1192 
1193 	/*
1194 	 * Sync back kernel address range.
1195 	 *
1196 	 * FIXME: Can the later sync in setup_cpu_entry_areas() replace
1197 	 * this call?
1198 	 */
1199 	sync_initial_page_table();
1200 
1201 	tboot_probe();
1202 
1203 	map_vsyscall();
1204 
1205 	x86_32_probe_apic();
1206 
1207 	early_quirks();
1208 
1209 	topology_apply_cmdline_limits_early();
1210 
1211 	/*
1212 	 * Parse SMP configuration. Try ACPI first and then the platform
1213 	 * specific parser.
1214 	 */
1215 	acpi_boot_init();
1216 	x86_init.mpparse.parse_smp_cfg();
1217 
1218 	/* Last opportunity to detect and map the local APIC */
1219 	init_apic_mappings();
1220 
1221 	topology_init_possible_cpus();
1222 
1223 	init_cpu_to_node();
1224 	init_gi_nodes();
1225 
1226 	io_apic_init_mappings();
1227 
1228 	x86_init.hyper.guest_late_init();
1229 
1230 	e820__reserve_resources();
1231 	e820__register_nosave_regions(max_pfn);
1232 
1233 	x86_init.resources.reserve_resources();
1234 
1235 	e820__setup_pci_gap();
1236 
1237 #ifdef CONFIG_VT
1238 #if defined(CONFIG_VGA_CONSOLE)
1239 	if (!efi_enabled(EFI_BOOT) || (efi_mem_type(0xa0000) != EFI_CONVENTIONAL_MEMORY))
1240 		vgacon_register_screen(&screen_info);
1241 #endif
1242 #endif
1243 	x86_init.oem.banner();
1244 
1245 	x86_init.timers.wallclock_init();
1246 
1247 	/*
1248 	 * This needs to run before setup_local_APIC() which soft-disables the
1249 	 * local APIC temporarily and that masks the thermal LVT interrupt,
1250 	 * leading to softlockups on machines which have configured SMI
1251 	 * interrupt delivery.
1252 	 */
1253 	therm_lvt_init();
1254 
1255 	mcheck_init();
1256 
1257 	register_refined_jiffies(CLOCK_TICK_RATE);
1258 
1259 #ifdef CONFIG_EFI
1260 	if (efi_enabled(EFI_BOOT))
1261 		efi_apply_memmap_quirks();
1262 #endif
1263 
1264 	unwind_init();
1265 }
1266 
1267 #ifdef CONFIG_X86_32
1268 
1269 static struct resource video_ram_resource = {
1270 	.name	= "Video RAM area",
1271 	.start	= 0xa0000,
1272 	.end	= 0xbffff,
1273 	.flags	= IORESOURCE_BUSY | IORESOURCE_MEM
1274 };
1275 
1276 void __init i386_reserve_resources(void)
1277 {
1278 	request_resource(&iomem_resource, &video_ram_resource);
1279 	reserve_standard_io_resources();
1280 }
1281 
1282 #endif /* CONFIG_X86_32 */
1283 
1284 static struct notifier_block kernel_offset_notifier = {
1285 	.notifier_call = dump_kernel_offset
1286 };
1287 
1288 static int __init register_kernel_offset_dumper(void)
1289 {
1290 	atomic_notifier_chain_register(&panic_notifier_list,
1291 					&kernel_offset_notifier);
1292 	return 0;
1293 }
1294 __initcall(register_kernel_offset_dumper);
1295 
1296 #ifdef CONFIG_HOTPLUG_CPU
1297 bool arch_cpu_is_hotpluggable(int cpu)
1298 {
1299 	return cpu > 0;
1300 }
1301 #endif /* CONFIG_HOTPLUG_CPU */
1302