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