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