blob: 49b596db5631e83ad216f737d97ec2c308a08967 [file] [log] [blame]
Thomas Gleixner457c8992019-05-19 13:08:55 +01001// SPDX-License-Identifier: GPL-2.0-only
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * Copyright (C) 1995 Linus Torvalds
4 *
Ingo Molnar12609012019-11-18 15:49:22 +01005 * This file contains the setup_arch() code, which handles the architecture-dependent
6 * parts of early kernel initialization.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 */
Nathan Chancellorea7b4242021-09-01 09:07:01 -07008#include <linux/acpi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#include <linux/console.h>
Ingo Molnar12609012019-11-18 15:49:22 +010010#include <linux/crash_dump.h>
Christoph Hellwig0b1abd12020-09-11 10:56:52 +020011#include <linux/dma-map-ops.h>
Andi Kleene9928672006-01-11 22:43:33 +010012#include <linux/dmi.h>
Ingo Molnar12609012019-11-18 15:49:22 +010013#include <linux/efi.h>
Bernhard Kaindlf212ec42008-01-30 13:34:11 +010014#include <linux/init_ohci1394_dma.h>
Ingo Molnar12609012019-11-18 15:49:22 +010015#include <linux/initrd.h>
16#include <linux/iscsi_ibft.h>
17#include <linux/memblock.h>
Andy Shevchenkof39650d2021-06-30 18:54:59 -070018#include <linux/panic_notifier.h>
Ingo Molnar12609012019-11-18 15:49:22 +010019#include <linux/pci.h>
20#include <linux/root_dev.h>
Roman Gushchincf11e852020-04-10 14:32:45 -070021#include <linux/hugetlb.h>
Ingo Molnar12609012019-11-18 15:49:22 +010022#include <linux/tboot.h>
23#include <linux/usb/xhci-dbgp.h>
Peter Zijlstraa945c832020-08-18 15:57:51 +020024#include <linux/static_call.h>
Christoph Hellwig0a0f0d82020-09-22 15:31:03 +020025#include <linux/swiotlb.h>
Ingo Molnar12609012019-11-18 15:49:22 +010026
David Howellse262e32d2018-11-01 23:07:23 +000027#include <uapi/linux/mount.h>
Eric W. Biederman1bc3b912005-06-25 14:58:01 -070028
Ingo Molnar12609012019-11-18 15:49:22 +010029#include <xen/xen.h>
Yinghai Lu46d671b2008-06-25 17:51:29 -070030
Eric W. Biederman9635b472005-06-25 14:57:41 -070031#include <asm/apic.h>
Ingo Molnar13c01132020-08-06 14:34:32 +020032#include <asm/numa.h>
Alexey Starikovskiyce3fe6b2008-03-17 22:08:17 +030033#include <asm/bios_ebda.h>
Yinghai Lucc9f7a02008-06-16 16:11:08 -070034#include <asm/bugs.h>
Jaswinder Singh Rajput6e5385d2009-01-07 18:11:35 +053035#include <asm/cpu.h>
Ingo Molnar12609012019-11-18 15:49:22 +010036#include <asm/efi.h>
Joerg Roedel1d9b16d2008-11-27 18:39:15 +010037#include <asm/gart.h>
Alok Kataria88b094f2008-10-27 10:41:46 -070038#include <asm/hypervisor.h>
Ingo Molnar12609012019-11-18 15:49:22 +010039#include <asm/io_apic.h>
40#include <asm/kasan.h>
Thomas Garnier0483e1f2016-06-21 17:47:02 -070041#include <asm/kaslr.h>
Ingo Molnar12609012019-11-18 15:49:22 +010042#include <asm/mce.h>
43#include <asm/mtrr.h>
Sean Christophersonca947b72019-11-26 08:54:07 -080044#include <asm/realmode.h>
Ingo Molnar12609012019-11-18 15:49:22 +010045#include <asm/olpc_ofw.h>
46#include <asm/pci-direct.h>
47#include <asm/prom.h>
48#include <asm/proto.h>
Borislav Petkov9a90ed02021-05-27 11:02:26 +020049#include <asm/thermal.h>
Josh Poimboeufee9f8fc2017-07-24 18:36:57 -050050#include <asm/unwind.h>
Ingo Molnar12609012019-11-18 15:49:22 +010051#include <asm/vsyscall.h>
Ingo Molnar186525b2019-11-29 08:17:25 +010052#include <linux/vmalloc.h>
Yinghai Lu46d671b2008-06-25 17:51:29 -070053
Pekka Enberg2b723942009-04-28 16:00:49 +030054/*
Ingo Molnar360db4a2019-11-18 16:03:39 +010055 * max_low_pfn_mapped: highest directly mapped pfn < 4 GB
56 * max_pfn_mapped: highest directly mapped pfn > 4 GB
Jacob Shin66520eb2012-11-16 19:38:52 -080057 *
Ingo Molnar09821ff2017-01-28 17:09:33 +010058 * The direct mapping only covers E820_TYPE_RAM regions, so the ranges and gaps are
Ingo Molnar360db4a2019-11-18 16:03:39 +010059 * represented by pfn_mapped[].
Pekka Enberg2b723942009-04-28 16:00:49 +030060 */
61unsigned long max_low_pfn_mapped;
62unsigned long max_pfn_mapped;
63
Thadeu Lima de Souza Cascardoe808bae2010-02-09 21:38:45 -020064#ifdef CONFIG_DMI
Jeremy Fitzhardinge796216a2009-03-12 16:09:49 -070065RESERVE_BRK(dmi_alloc, 65536);
Thadeu Lima de Souza Cascardoe808bae2010-02-09 21:38:45 -020066#endif
Jeremy Fitzhardinge796216a2009-03-12 16:09:49 -070067
Ingo Molnarc0b58422009-01-27 17:13:05 +010068
Ingo Molnar360db4a2019-11-18 16:03:39 +010069/*
70 * Range of the BSS area. The size of the BSS area is determined
Cao jin81519f72021-03-11 16:39:19 +080071 * at link time, with RESERVE_BRK() facility reserving additional
Ingo Molnar360db4a2019-11-18 16:03:39 +010072 * chunks.
73 */
Ingo Molnar360db4a2019-11-18 16:03:39 +010074unsigned long _brk_start = (unsigned long)__brk_base;
75unsigned long _brk_end = (unsigned long)__brk_base;
Jeremy Fitzhardinge93dbda72009-02-26 17:35:44 -080076
Yinghai Lu217b8ce2008-06-25 17:55:20 -070077struct boot_params boot_params;
Yinghai Lu217b8ce2008-06-25 17:55:20 -070078
Linus Torvalds4046d6e2016-04-14 11:18:57 -070079/*
Ingo Molnar360db4a2019-11-18 16:03:39 +010080 * These are the four main kernel memory regions, we put them into
81 * the resource tree so that kdump tools and other debugging tools
82 * recover it:
Linus Torvalds4046d6e2016-04-14 11:18:57 -070083 */
Ingo Molnar360db4a2019-11-18 16:03:39 +010084
Kees Cooka3299752019-10-29 14:13:50 -070085static struct resource rodata_resource = {
86 .name = "Kernel rodata",
87 .start = 0,
88 .end = 0,
89 .flags = IORESOURCE_BUSY | IORESOURCE_SYSTEM_RAM
90};
91
Linus Torvalds4046d6e2016-04-14 11:18:57 -070092static struct resource data_resource = {
93 .name = "Kernel data",
94 .start = 0,
95 .end = 0,
96 .flags = IORESOURCE_BUSY | IORESOURCE_SYSTEM_RAM
97};
98
99static struct resource code_resource = {
100 .name = "Kernel code",
101 .start = 0,
102 .end = 0,
103 .flags = IORESOURCE_BUSY | IORESOURCE_SYSTEM_RAM
104};
105
106static struct resource bss_resource = {
107 .name = "Kernel bss",
108 .start = 0,
109 .end = 0,
110 .flags = IORESOURCE_BUSY | IORESOURCE_SYSTEM_RAM
111};
112
113
Yinghai Lu7dea23e2008-06-25 17:50:06 -0700114#ifdef CONFIG_X86_32
Ingo Molnar360db4a2019-11-18 16:03:39 +0100115/* CPU data as detected by the assembly code in head_32.S */
Mathias Krause64158132017-02-12 22:12:08 +0100116struct cpuinfo_x86 new_cpu_data;
117
Ingo Molnar360db4a2019-11-18 16:03:39 +0100118/* Common CPU data for all CPUs */
Mathias Krause64158132017-02-12 22:12:08 +0100119struct cpuinfo_x86 boot_cpu_data __read_mostly;
Alexey Dobriyan129f6942005-06-23 00:08:33 -0700120EXPORT_SYMBOL(boot_cpu_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121
Alexey Starikovskiy0c254e32008-03-27 23:55:04 +0300122unsigned int def_to_bigsmp;
123
Yinghai Lu7dea23e2008-06-25 17:50:06 -0700124struct apm_info apm_info;
125EXPORT_SYMBOL(apm_info);
126
127#if defined(CONFIG_X86_SPEEDSTEP_SMI) || \
128 defined(CONFIG_X86_SPEEDSTEP_SMI_MODULE)
129struct ist_info ist_info;
130EXPORT_SYMBOL(ist_info);
131#else
132struct ist_info ist_info;
133#endif
134
135#else
Kirill A. Shutemov162434e2018-02-14 14:16:54 +0300136struct cpuinfo_x86 boot_cpu_data __read_mostly;
Yinghai Lu7dea23e2008-06-25 17:50:06 -0700137EXPORT_SYMBOL(boot_cpu_data);
138#endif
139
140
141#if !defined(CONFIG_X86_PAE) || defined(CONFIG_X86_64)
Kees Cook404f6aa2016-08-08 16:29:06 -0700142__visible unsigned long mmu_cr4_features __ro_after_init;
Yinghai Lu7dea23e2008-06-25 17:50:06 -0700143#else
Kees Cook404f6aa2016-08-08 16:29:06 -0700144__visible unsigned long mmu_cr4_features __ro_after_init = X86_CR4_PAE;
Yinghai Lu7dea23e2008-06-25 17:50:06 -0700145#endif
146
H. Peter Anvin50312962009-05-07 16:54:11 -0700147/* Boot loader ID and version as integers, for the benefit of proc_dointvec */
148int bootloader_type, bootloader_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150/*
151 * Setup options
152 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153struct screen_info screen_info;
Alexey Dobriyan129f6942005-06-23 00:08:33 -0700154EXPORT_SYMBOL(screen_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155struct edid_info edid_info;
Antonino A. Daplas5e518d72005-09-09 13:04:34 -0700156EXPORT_SYMBOL_GPL(edid_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158extern int root_mountflags;
159
Pavel Macheke44b7b72008-04-10 23:28:10 +0200160unsigned long saved_video_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161
H. Peter Anvincf8fa922008-01-30 13:32:51 +0100162#define RAMDISK_IMAGE_START_MASK 0x07FF
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163#define RAMDISK_PROMPT_FLAG 0x8000
H. Peter Anvincf8fa922008-01-30 13:32:51 +0100164#define RAMDISK_LOAD_FLAG 0x4000
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165
Alon Bar-Lev4e498b62007-02-12 00:54:11 -0800166static char __initdata command_line[COMMAND_LINE_SIZE];
Tim Bird516cbf32008-08-12 12:52:36 -0700167#ifdef CONFIG_CMDLINE_BOOL
168static char __initdata builtin_cmdline[COMMAND_LINE_SIZE] = CONFIG_CMDLINE;
169#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171#if defined(CONFIG_EDD) || defined(CONFIG_EDD_MODULE)
172struct edd edd;
173#ifdef CONFIG_EDD_MODULE
174EXPORT_SYMBOL(edd);
175#endif
176/**
177 * copy_edd() - Copy the BIOS EDD information
178 * from boot_params into a safe place.
179 *
180 */
Helight.Xu9eaa1922009-11-30 18:33:51 +0800181static inline void __init copy_edd(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182{
H. Peter Anvin30c82642007-10-15 17:13:22 -0700183 memcpy(edd.mbr_signature, boot_params.edd_mbr_sig_buffer,
184 sizeof(edd.mbr_signature));
185 memcpy(edd.edd_info, boot_params.eddbuf, sizeof(edd.edd_info));
186 edd.mbr_signature_nr = boot_params.edd_mbr_sig_buf_entries;
187 edd.edd_info_nr = boot_params.eddbuf_entries;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188}
189#else
Helight.Xu9eaa1922009-11-30 18:33:51 +0800190static inline void __init copy_edd(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191{
192}
193#endif
194
H. Peter Anvin5368a2b2009-03-14 17:19:51 -0700195void * __init extend_brk(size_t size, size_t align)
196{
197 size_t mask = align - 1;
198 void *ret;
199
200 BUG_ON(_brk_start == 0);
201 BUG_ON(align & mask);
202
203 _brk_end = (_brk_end + mask) & ~mask;
204 BUG_ON((char *)(_brk_end + size) > __brk_limit);
205
206 ret = (void *)_brk_end;
207 _brk_end += size;
208
209 memset(ret, 0, size);
210
211 return ret;
212}
213
Yinghai Lu148b2092012-11-16 19:39:08 -0800214#ifdef CONFIG_X86_32
Yinghai Lue5f15b42011-02-18 11:30:30 +0000215static void __init cleanup_highmap(void)
Yinghai Luf005fe12010-12-27 16:48:32 -0800216{
217}
Pekka J Enberg854c8792009-06-22 17:39:41 +0300218#endif
219
H. Peter Anvin5368a2b2009-03-14 17:19:51 -0700220static void __init reserve_brk(void)
221{
222 if (_brk_end > _brk_start)
Alexander Duyckfc8d7822012-11-16 13:57:13 -0800223 memblock_reserve(__pa_symbol(_brk_start),
224 _brk_end - _brk_start);
H. Peter Anvin5368a2b2009-03-14 17:19:51 -0700225
226 /* Mark brk area as locked down and no longer taking any
227 new allocations */
228 _brk_start = 0;
229}
230
Borislav Petkov5aa3d712013-12-04 20:50:42 +0100231u64 relocated_ramdisk;
232
H. Peter Anvincf8fa922008-01-30 13:32:51 +0100233#ifdef CONFIG_BLK_DEV_INITRD
234
Yinghai Lua8a51a82013-01-24 12:19:56 -0800235static u64 __init get_ramdisk_image(void)
236{
237 u64 ramdisk_image = boot_params.hdr.ramdisk_image;
238
Yinghai Luee92d812013-01-28 20:16:44 -0800239 ramdisk_image |= (u64)boot_params.ext_ramdisk_image << 32;
240
Ronald G. Minnich694cfd82020-04-25 18:10:21 -0700241 if (ramdisk_image == 0)
242 ramdisk_image = phys_initrd_start;
243
Yinghai Lua8a51a82013-01-24 12:19:56 -0800244 return ramdisk_image;
245}
246static u64 __init get_ramdisk_size(void)
247{
248 u64 ramdisk_size = boot_params.hdr.ramdisk_size;
249
Yinghai Luee92d812013-01-28 20:16:44 -0800250 ramdisk_size |= (u64)boot_params.ext_ramdisk_size << 32;
251
Ronald G. Minnich694cfd82020-04-25 18:10:21 -0700252 if (ramdisk_size == 0)
253 ramdisk_size = phys_initrd_size;
254
Yinghai Lua8a51a82013-01-24 12:19:56 -0800255 return ramdisk_size;
256}
257
Yinghai Lueb1379c2008-06-25 17:49:26 -0700258static void __init relocate_initrd(void)
H. Peter Anvincf8fa922008-01-30 13:32:51 +0100259{
Yinghai Luc967da62010-03-28 19:42:55 -0700260 /* Assume only end is not page aligned */
Yinghai Lua8a51a82013-01-24 12:19:56 -0800261 u64 ramdisk_image = get_ramdisk_image();
262 u64 ramdisk_size = get_ramdisk_size();
Yinghai Luc967da62010-03-28 19:42:55 -0700263 u64 area_size = PAGE_ALIGN(ramdisk_size);
H. Peter Anvincf8fa922008-01-30 13:32:51 +0100264
Yinghai Lue8c57d42012-11-16 19:38:51 -0800265 /* We need to move the initrd down into directly mapped mem */
Mike Rapoport3c45ee62020-10-13 16:58:12 -0700266 relocated_ramdisk = memblock_phys_alloc_range(area_size, PAGE_SIZE, 0,
267 PFN_PHYS(max_pfn_mapped));
Borislav Petkov5aa3d712013-12-04 20:50:42 +0100268 if (!relocated_ramdisk)
Yinghai Lu3945e2c2008-05-25 10:00:09 -0700269 panic("Cannot find place for new RAMDISK of size %lld\n",
Borislav Petkov5aa3d712013-12-04 20:50:42 +0100270 ramdisk_size);
Yinghai Lu3945e2c2008-05-25 10:00:09 -0700271
Borislav Petkov5aa3d712013-12-04 20:50:42 +0100272 initrd_start = relocated_ramdisk + PAGE_OFFSET;
H. Peter Anvincf8fa922008-01-30 13:32:51 +0100273 initrd_end = initrd_start + ramdisk_size;
Bjorn Helgaas365811d2012-05-29 15:06:29 -0700274 printk(KERN_INFO "Allocated new RAMDISK: [mem %#010llx-%#010llx]\n",
Borislav Petkov5aa3d712013-12-04 20:50:42 +0100275 relocated_ramdisk, relocated_ramdisk + ramdisk_size - 1);
H. Peter Anvincf8fa922008-01-30 13:32:51 +0100276
Mark Salter5dd2c4b2015-09-08 15:03:07 -0700277 copy_from_early_mem((void *)initrd_start, ramdisk_image, ramdisk_size);
H. Peter Anvincf8fa922008-01-30 13:32:51 +0100278
Bjorn Helgaas365811d2012-05-29 15:06:29 -0700279 printk(KERN_INFO "Move RAMDISK from [mem %#010llx-%#010llx] to"
280 " [mem %#010llx-%#010llx]\n",
Yinghai Luba5b14cc2008-05-21 18:40:18 -0700281 ramdisk_image, ramdisk_image + ramdisk_size - 1,
Borislav Petkov5aa3d712013-12-04 20:50:42 +0100282 relocated_ramdisk, relocated_ramdisk + ramdisk_size - 1);
Yinghai Lueb1379c2008-06-25 17:49:26 -0700283}
Yinghai Lu9a27f5c2008-06-13 20:07:03 -0700284
Yinghai Lu1b8c78b2013-01-24 12:19:55 -0800285static void __init early_reserve_initrd(void)
286{
287 /* Assume only end is not page aligned */
Yinghai Lua8a51a82013-01-24 12:19:56 -0800288 u64 ramdisk_image = get_ramdisk_image();
289 u64 ramdisk_size = get_ramdisk_size();
Yinghai Lu1b8c78b2013-01-24 12:19:55 -0800290 u64 ramdisk_end = PAGE_ALIGN(ramdisk_image + ramdisk_size);
291
292 if (!boot_params.hdr.type_of_loader ||
293 !ramdisk_image || !ramdisk_size)
294 return; /* No initrd provided by bootloader */
295
296 memblock_reserve(ramdisk_image, ramdisk_end - ramdisk_image);
297}
Mike Rapoport3c45ee62020-10-13 16:58:12 -0700298
Yinghai Lueb1379c2008-06-25 17:49:26 -0700299static void __init reserve_initrd(void)
300{
Yinghai Luc967da62010-03-28 19:42:55 -0700301 /* Assume only end is not page aligned */
Yinghai Lua8a51a82013-01-24 12:19:56 -0800302 u64 ramdisk_image = get_ramdisk_image();
303 u64 ramdisk_size = get_ramdisk_size();
Yinghai Luc967da62010-03-28 19:42:55 -0700304 u64 ramdisk_end = PAGE_ALIGN(ramdisk_image + ramdisk_size);
Yinghai Lueb1379c2008-06-25 17:49:26 -0700305
306 if (!boot_params.hdr.type_of_loader ||
307 !ramdisk_image || !ramdisk_size)
308 return; /* No initrd provided by bootloader */
309
310 initrd_start = 0;
311
Bjorn Helgaas365811d2012-05-29 15:06:29 -0700312 printk(KERN_INFO "RAMDISK: [mem %#010llx-%#010llx]\n", ramdisk_image,
313 ramdisk_end - 1);
Yinghai Lueb1379c2008-06-25 17:49:26 -0700314
Yinghai Lu74f27652012-11-16 19:38:53 -0800315 if (pfn_range_is_mapped(PFN_DOWN(ramdisk_image),
Yinghai Lue8c57d42012-11-16 19:38:51 -0800316 PFN_DOWN(ramdisk_end))) {
317 /* All are mapped, easy case */
Yinghai Lueb1379c2008-06-25 17:49:26 -0700318 initrd_start = ramdisk_image + PAGE_OFFSET;
319 initrd_end = initrd_start + ramdisk_size;
320 return;
321 }
322
Yinghai Lueb1379c2008-06-25 17:49:26 -0700323 relocate_initrd();
Yinghai Lu8c5dd8f2009-06-04 19:14:22 -0700324
Mike Rapoport3ecc6832021-11-05 13:43:19 -0700325 memblock_phys_free(ramdisk_image, ramdisk_end - ramdisk_image);
H. Peter Anvincf8fa922008-01-30 13:32:51 +0100326}
Lv Zhengaf06f8b2016-04-11 10:13:27 +0800327
Yinghai Lu225c37d2008-06-22 02:46:58 -0700328#else
Yinghai Lu1b8c78b2013-01-24 12:19:55 -0800329static void __init early_reserve_initrd(void)
330{
331}
Yinghai Lueb1379c2008-06-25 17:49:26 -0700332static void __init reserve_initrd(void)
Yinghai Lu225c37d2008-06-22 02:46:58 -0700333{
334}
H. Peter Anvincf8fa922008-01-30 13:32:51 +0100335#endif /* CONFIG_BLK_DEV_INITRD */
336
Yinghai Lu29f784e2008-06-25 18:00:22 -0700337static void __init parse_setup_data(void)
Yinghai Lu257b0fd2008-06-25 17:56:22 -0700338{
339 struct setup_data *data;
Linn Crosetto30e46b52013-08-13 15:46:41 -0600340 u64 pa_data, pa_next;
Yinghai Lu257b0fd2008-06-25 17:56:22 -0700341
Yinghai Lu257b0fd2008-06-25 17:56:22 -0700342 pa_data = boot_params.hdr.setup_data;
343 while (pa_data) {
WANG Chao73898822015-01-07 18:55:48 +0800344 u32 data_len, data_type;
Sebastian Andrzej Siewiorf1c2b352011-02-22 21:07:36 +0100345
WANG Chao73898822015-01-07 18:55:48 +0800346 data = early_memremap(pa_data, sizeof(*data));
Sebastian Andrzej Siewiorf1c2b352011-02-22 21:07:36 +0100347 data_len = data->len + sizeof(struct setup_data);
Linn Crosetto30e46b52013-08-13 15:46:41 -0600348 data_type = data->type;
349 pa_next = data->next;
Juergen Gross8d4a40b2015-02-24 10:13:28 +0100350 early_memunmap(data, sizeof(*data));
Sebastian Andrzej Siewiorf1c2b352011-02-22 21:07:36 +0100351
Linn Crosetto30e46b52013-08-13 15:46:41 -0600352 switch (data_type) {
Yinghai Lu257b0fd2008-06-25 17:56:22 -0700353 case SETUP_E820_EXT:
Ingo Molnar914053c2017-01-28 13:18:40 +0100354 e820__memory_setup_extended(pa_data, data_len);
Yinghai Lu257b0fd2008-06-25 17:56:22 -0700355 break;
Sebastian Andrzej Siewiorda6b7372011-02-22 21:07:37 +0100356 case SETUP_DTB:
357 add_dtb(pa_data);
Yinghai Lu257b0fd2008-06-25 17:56:22 -0700358 break;
Dave Young1fec0532013-12-20 18:02:19 +0800359 case SETUP_EFI:
360 parse_efi_setup(pa_data, data_len);
361 break;
Yinghai Lu257b0fd2008-06-25 17:56:22 -0700362 default:
363 break;
364 }
Linn Crosetto30e46b52013-08-13 15:46:41 -0600365 pa_data = pa_next;
Yinghai Lu257b0fd2008-06-25 17:56:22 -0700366 }
367}
368
Yinghai Lua9ce6bc2010-08-25 13:39:17 -0700369static void __init memblock_x86_reserve_range_setup_data(void)
Yinghai Lua0a0bec2008-07-03 11:37:13 -0700370{
371 struct setup_data *data;
372 u64 pa_data;
Yinghai Lua0a0bec2008-07-03 11:37:13 -0700373
Yinghai Lua0a0bec2008-07-03 11:37:13 -0700374 pa_data = boot_params.hdr.setup_data;
375 while (pa_data) {
Jeremy Fitzhardinge88b4c142008-09-07 15:21:16 -0700376 data = early_memremap(pa_data, sizeof(*data));
Tejun Heo24aa0782011-07-12 11:16:06 +0200377 memblock_reserve(pa_data, sizeof(*data) + data->len);
Daniel Kiperb3c72fc2019-11-12 14:46:40 +0100378
379 if (data->type == SETUP_INDIRECT &&
380 ((struct setup_indirect *)data->data)->type != SETUP_INDIRECT)
381 memblock_reserve(((struct setup_indirect *)data->data)->addr,
382 ((struct setup_indirect *)data->data)->len);
383
Yinghai Lua0a0bec2008-07-03 11:37:13 -0700384 pa_data = data->next;
Juergen Gross8d4a40b2015-02-24 10:13:28 +0100385 early_memunmap(data, sizeof(*data));
Yinghai Lua0a0bec2008-07-03 11:37:13 -0700386 }
387}
388
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389/*
Yinghai Luccb4def2008-06-25 17:57:13 -0700390 * --------- Crashkernel reservation ------------------------------
391 */
392
Dave Young2965faa2015-09-09 15:38:55 -0700393#ifdef CONFIG_KEXEC_CORE
Bernhard Walle32105f72008-06-26 21:54:08 +0200394
Borislav Petkov606134f2015-10-19 11:17:44 +0200395/* 16M alignment for crash kernel regions */
Dave Young9ca5c8e2019-04-21 11:50:59 +0800396#define CRASH_ALIGN SZ_16M
Borislav Petkov606134f2015-10-19 11:17:44 +0200397
H. Peter Anvin7f8595b2010-12-16 19:20:41 -0800398/*
Baoquan He8ff80fb2019-05-24 15:38:10 +0800399 * Keep the crash kernel below this limit.
400 *
Ingo Molnar360db4a2019-11-18 16:03:39 +0100401 * Earlier 32-bits kernels would limit the kernel to the low 512 MB range
Baoquan He8ff80fb2019-05-24 15:38:10 +0800402 * due to mapping restrictions.
403 *
Ingo Molnar360db4a2019-11-18 16:03:39 +0100404 * 64-bit kdump kernels need to be restricted to be under 64 TB, which is
Cao jin11a98f32019-11-18 15:00:12 +0800405 * the upper limit of system RAM in 4-level paging mode. Since the kdump
Ingo Molnar360db4a2019-11-18 16:03:39 +0100406 * jump could be from 5-level paging to 4-level paging, the jump will fail if
407 * the kernel is put above 64 TB, and during the 1st kernel bootup there's
408 * no good way to detect the paging mode of the target kernel which will be
409 * loaded for dumping.
H. Peter Anvin7f8595b2010-12-16 19:20:41 -0800410 */
411#ifdef CONFIG_X86_32
Dave Young9ca5c8e2019-04-21 11:50:59 +0800412# define CRASH_ADDR_LOW_MAX SZ_512M
413# define CRASH_ADDR_HIGH_MAX SZ_512M
H. Peter Anvin7f8595b2010-12-16 19:20:41 -0800414#else
Dave Young9ca5c8e2019-04-21 11:50:59 +0800415# define CRASH_ADDR_LOW_MAX SZ_4G
Baoquan He8ff80fb2019-05-24 15:38:10 +0800416# define CRASH_ADDR_HIGH_MAX SZ_64T
H. Peter Anvin7f8595b2010-12-16 19:20:41 -0800417#endif
418
Baoquan Heeb6db832015-10-19 11:17:41 +0200419static int __init reserve_crashkernel_low(void)
Yinghai Lu0212f912013-01-24 12:20:11 -0800420{
421#ifdef CONFIG_X86_64
Borislav Petkovf56d5572015-10-19 11:17:45 +0200422 unsigned long long base, low_base = 0, low_size = 0;
Mike Rapoport6120cdc2020-10-13 16:58:16 -0700423 unsigned long low_mem_limit;
Yinghai Lu0212f912013-01-24 12:20:11 -0800424 int ret;
425
Mike Rapoport6120cdc2020-10-13 16:58:16 -0700426 low_mem_limit = min(memblock_phys_mem_size(), CRASH_ADDR_LOW_MAX);
Borislav Petkov97eac212015-10-19 11:17:43 +0200427
Yinghai Luadbc7422013-04-15 22:23:48 -0700428 /* crashkernel=Y,low */
Mike Rapoport6120cdc2020-10-13 16:58:16 -0700429 ret = parse_crashkernel_low(boot_command_line, low_mem_limit, &low_size, &base);
Borislav Petkovf56d5572015-10-19 11:17:45 +0200430 if (ret) {
Yinghai Luc729de82013-04-15 22:23:45 -0700431 /*
Andy Shevchenko392e8792019-06-19 17:19:55 +0300432 * two parts from kernel/dma/swiotlb.c:
Joerg Roedel94fb9332015-06-10 17:49:42 +0200433 * -swiotlb size: user-specified with swiotlb= or default.
434 *
435 * -swiotlb overflow buffer: now hardcoded to 32k. We round it
436 * to 8M for other buffers that may need to stay low too. Also
437 * make sure we allocate enough extra low memory so that we
438 * don't run out of DMA buffers for 32-bit devices.
Yinghai Luc729de82013-04-15 22:23:45 -0700439 */
Borislav Petkov97eac212015-10-19 11:17:43 +0200440 low_size = max(swiotlb_size_or_default() + (8UL << 20), 256UL << 20);
Yinghai Luc729de82013-04-15 22:23:45 -0700441 } else {
Yinghai Luadbc7422013-04-15 22:23:48 -0700442 /* passed with crashkernel=0,low ? */
Yinghai Luc729de82013-04-15 22:23:45 -0700443 if (!low_size)
Baoquan Heeb6db832015-10-19 11:17:41 +0200444 return 0;
Yinghai Luc729de82013-04-15 22:23:45 -0700445 }
Yinghai Lu0212f912013-01-24 12:20:11 -0800446
Mike Rapoport6120cdc2020-10-13 16:58:16 -0700447 low_base = memblock_phys_alloc_range(low_size, CRASH_ALIGN, 0, CRASH_ADDR_LOW_MAX);
Yinghai Lu0212f912013-01-24 12:20:11 -0800448 if (!low_base) {
Baoquan Heeb6db832015-10-19 11:17:41 +0200449 pr_err("Cannot reserve %ldMB crashkernel low memory, please try smaller size.\n",
450 (unsigned long)(low_size >> 20));
451 return -ENOMEM;
Yinghai Lu0212f912013-01-24 12:20:11 -0800452 }
453
Mike Rapoport6120cdc2020-10-13 16:58:16 -0700454 pr_info("Reserving %ldMB of low memory at %ldMB for crashkernel (low RAM limit: %ldMB)\n",
Borislav Petkov97eac212015-10-19 11:17:43 +0200455 (unsigned long)(low_size >> 20),
456 (unsigned long)(low_base >> 20),
Mike Rapoport6120cdc2020-10-13 16:58:16 -0700457 (unsigned long)(low_mem_limit >> 20));
Borislav Petkov97eac212015-10-19 11:17:43 +0200458
Yinghai Lu0212f912013-01-24 12:20:11 -0800459 crashk_low_res.start = low_base;
460 crashk_low_res.end = low_base + low_size - 1;
461 insert_resource(&iomem_resource, &crashk_low_res);
462#endif
Baoquan Heeb6db832015-10-19 11:17:41 +0200463 return 0;
Yinghai Lu0212f912013-01-24 12:20:11 -0800464}
465
Yinghai Lu29f784e2008-06-25 18:00:22 -0700466static void __init reserve_crashkernel(void)
Yinghai Luccb4def2008-06-25 17:57:13 -0700467{
Borislav Petkovf56d5572015-10-19 11:17:45 +0200468 unsigned long long crash_size, crash_base, total_mem;
Yinghai Lu55a20ee2013-04-15 22:23:47 -0700469 bool high = false;
Yinghai Luccb4def2008-06-25 17:57:13 -0700470 int ret;
471
Dave Young09c71bf2012-03-28 14:42:47 -0700472 total_mem = memblock_phys_mem_size();
Yinghai Luccb4def2008-06-25 17:57:13 -0700473
Yinghai Lu55a20ee2013-04-15 22:23:47 -0700474 /* crashkernel=XM */
Borislav Petkov97eac212015-10-19 11:17:43 +0200475 ret = parse_crashkernel(boot_command_line, total_mem, &crash_size, &crash_base);
Yinghai Lu55a20ee2013-04-15 22:23:47 -0700476 if (ret != 0 || crash_size <= 0) {
Yinghai Luadbc7422013-04-15 22:23:48 -0700477 /* crashkernel=X,high */
Yinghai Lu55a20ee2013-04-15 22:23:47 -0700478 ret = parse_crashkernel_high(boot_command_line, total_mem,
Borislav Petkov97eac212015-10-19 11:17:43 +0200479 &crash_size, &crash_base);
Yinghai Lu55a20ee2013-04-15 22:23:47 -0700480 if (ret != 0 || crash_size <= 0)
481 return;
482 high = true;
483 }
Bernhard Walle32105f72008-06-26 21:54:08 +0200484
Petr Tesarik3db3eb22018-04-25 12:08:35 +0200485 if (xen_pv_domain()) {
486 pr_info("Ignoring crashkernel for a Xen PV domain\n");
487 return;
488 }
489
Bernhard Walle32105f72008-06-26 21:54:08 +0200490 /* 0 means: find the address automatically */
Dave Youngb9ac3842019-04-22 11:19:05 +0800491 if (!crash_base) {
Yinghai Lu9f4c1392010-10-05 16:05:14 -0700492 /*
Xunlei Panga8d4c822017-01-23 14:48:23 +0800493 * Set CRASH_ADDR_LOW_MAX upper bound for crash memory,
Dave Youngb9ac3842019-04-22 11:19:05 +0800494 * crashkernel=x,high reserves memory over 4G, also allocates
495 * 256M extra low memory for DMA buffers and swiotlb.
496 * But the extra memory is not required for all machines.
497 * So try low memory first and fall back to high memory
498 * unless "crashkernel=size[KMG],high" is specified.
Yinghai Lu9f4c1392010-10-05 16:05:14 -0700499 */
Dave Youngb9ac3842019-04-22 11:19:05 +0800500 if (!high)
Mike Rapoport6120cdc2020-10-13 16:58:16 -0700501 crash_base = memblock_phys_alloc_range(crash_size,
502 CRASH_ALIGN, CRASH_ALIGN,
503 CRASH_ADDR_LOW_MAX);
Dave Youngb9ac3842019-04-22 11:19:05 +0800504 if (!crash_base)
Mike Rapoport6120cdc2020-10-13 16:58:16 -0700505 crash_base = memblock_phys_alloc_range(crash_size,
506 CRASH_ALIGN, CRASH_ALIGN,
507 CRASH_ADDR_HIGH_MAX);
Tejun Heo1f5026a2011-07-12 09:58:09 +0200508 if (!crash_base) {
Yinghai Lu44280732009-11-22 17:18:49 -0800509 pr_info("crashkernel reservation failed - No suitable area found.\n");
Yinghai Luccb4def2008-06-25 17:57:13 -0700510 return;
511 }
Bernhard Walle32105f72008-06-26 21:54:08 +0200512 } else {
Yinghai Lu44280732009-11-22 17:18:49 -0800513 unsigned long long start;
514
Mike Rapoport6120cdc2020-10-13 16:58:16 -0700515 start = memblock_phys_alloc_range(crash_size, SZ_1M, crash_base,
516 crash_base + crash_size);
Yinghai Lu44280732009-11-22 17:18:49 -0800517 if (start != crash_base) {
518 pr_info("crashkernel reservation failed - memory is in use.\n");
Yinghai Luccb4def2008-06-25 17:57:13 -0700519 return;
520 }
Yinghai Luccb4def2008-06-25 17:57:13 -0700521 }
Bernhard Walle32105f72008-06-26 21:54:08 +0200522
Baoquan Heeb6db832015-10-19 11:17:41 +0200523 if (crash_base >= (1ULL << 32) && reserve_crashkernel_low()) {
Mike Rapoport3ecc6832021-11-05 13:43:19 -0700524 memblock_phys_free(crash_base, crash_size);
Baoquan Heeb6db832015-10-19 11:17:41 +0200525 return;
526 }
527
Borislav Petkovf56d5572015-10-19 11:17:45 +0200528 pr_info("Reserving %ldMB of memory at %ldMB for crashkernel (System RAM: %ldMB)\n",
529 (unsigned long)(crash_size >> 20),
530 (unsigned long)(crash_base >> 20),
531 (unsigned long)(total_mem >> 20));
Bernhard Walle32105f72008-06-26 21:54:08 +0200532
533 crashk_res.start = crash_base;
534 crashk_res.end = crash_base + crash_size - 1;
535 insert_resource(&iomem_resource, &crashk_res);
Yinghai Luccb4def2008-06-25 17:57:13 -0700536}
537#else
Yinghai Lu29f784e2008-06-25 18:00:22 -0700538static void __init reserve_crashkernel(void)
Yinghai Luccb4def2008-06-25 17:57:13 -0700539{
540}
541#endif
542
Yinghai Lubdba0e72008-06-25 17:58:02 -0700543static struct resource standard_io_resources[] = {
544 { .name = "dma1", .start = 0x00, .end = 0x1f,
545 .flags = IORESOURCE_BUSY | IORESOURCE_IO },
546 { .name = "pic1", .start = 0x20, .end = 0x21,
547 .flags = IORESOURCE_BUSY | IORESOURCE_IO },
548 { .name = "timer0", .start = 0x40, .end = 0x43,
549 .flags = IORESOURCE_BUSY | IORESOURCE_IO },
550 { .name = "timer1", .start = 0x50, .end = 0x53,
551 .flags = IORESOURCE_BUSY | IORESOURCE_IO },
552 { .name = "keyboard", .start = 0x60, .end = 0x60,
553 .flags = IORESOURCE_BUSY | IORESOURCE_IO },
554 { .name = "keyboard", .start = 0x64, .end = 0x64,
555 .flags = IORESOURCE_BUSY | IORESOURCE_IO },
556 { .name = "dma page reg", .start = 0x80, .end = 0x8f,
557 .flags = IORESOURCE_BUSY | IORESOURCE_IO },
558 { .name = "pic2", .start = 0xa0, .end = 0xa1,
559 .flags = IORESOURCE_BUSY | IORESOURCE_IO },
560 { .name = "dma2", .start = 0xc0, .end = 0xdf,
561 .flags = IORESOURCE_BUSY | IORESOURCE_IO },
562 { .name = "fpu", .start = 0xf0, .end = 0xff,
563 .flags = IORESOURCE_BUSY | IORESOURCE_IO }
564};
565
Thomas Gleixner8fee6972009-08-19 14:55:50 +0200566void __init reserve_standard_io_resources(void)
Yinghai Lubdba0e72008-06-25 17:58:02 -0700567{
568 int i;
569
570 /* request I/O space for devices used on all i[345]86 PCs */
571 for (i = 0; i < ARRAY_SIZE(standard_io_resources); i++)
572 request_resource(&ioport_resource, &standard_io_resources[i]);
573
574}
575
Jesse Barnesa9acc532012-11-14 20:43:31 +0000576static bool __init snb_gfx_workaround_needed(void)
577{
H. Peter Anvine43b3ce2013-01-13 20:56:41 -0800578#ifdef CONFIG_PCI
Jesse Barnesa9acc532012-11-14 20:43:31 +0000579 int i;
580 u16 vendor, devid;
H. Peter Anvinab3cd862013-01-13 20:36:39 -0800581 static const __initconst u16 snb_ids[] = {
Jesse Barnesa9acc532012-11-14 20:43:31 +0000582 0x0102,
583 0x0112,
584 0x0122,
585 0x0106,
586 0x0116,
587 0x0126,
588 0x010a,
589 };
590
591 /* Assume no if something weird is going on with PCI */
592 if (!early_pci_allowed())
593 return false;
594
595 vendor = read_pci_config_16(0, 2, 0, PCI_VENDOR_ID);
596 if (vendor != 0x8086)
597 return false;
598
599 devid = read_pci_config_16(0, 2, 0, PCI_DEVICE_ID);
600 for (i = 0; i < ARRAY_SIZE(snb_ids); i++)
601 if (devid == snb_ids[i])
602 return true;
H. Peter Anvine43b3ce2013-01-13 20:56:41 -0800603#endif
Jesse Barnesa9acc532012-11-14 20:43:31 +0000604
605 return false;
606}
607
608/*
609 * Sandy Bridge graphics has trouble with certain ranges, exclude
610 * them from allocation.
611 */
612static void __init trim_snb_memory(void)
613{
H. Peter Anvinab3cd862013-01-13 20:36:39 -0800614 static const __initconst unsigned long bad_pages[] = {
Jesse Barnesa9acc532012-11-14 20:43:31 +0000615 0x20050000,
616 0x20110000,
617 0x20130000,
618 0x20138000,
619 0x40004000,
620 };
621 int i;
622
623 if (!snb_gfx_workaround_needed())
624 return;
625
626 printk(KERN_DEBUG "reserving inaccessible SNB gfx pages\n");
627
628 /*
Mike Rapoportc361e5d2021-04-13 21:08:39 +0300629 * SandyBridge integrated graphics devices have a bug that prevents
630 * them from accessing certain memory ranges, namely anything below
631 * 1M and in the pages listed in bad_pages[] above.
632 *
Mike Rapoportf1d4d472021-06-01 10:53:52 +0300633 * To avoid these pages being ever accessed by SNB gfx devices reserve
634 * bad_pages that have not already been reserved at boot time.
635 * All memory below the 1 MB mark is anyway reserved later during
636 * setup_arch(), so there is no need to reserve it here.
Jesse Barnesa9acc532012-11-14 20:43:31 +0000637 */
Mike Rapoportc361e5d2021-04-13 21:08:39 +0300638
Jesse Barnesa9acc532012-11-14 20:43:31 +0000639 for (i = 0; i < ARRAY_SIZE(bad_pages); i++) {
640 if (memblock_reserve(bad_pages[i], PAGE_SIZE))
641 printk(KERN_WARNING "failed to reserve 0x%08lx\n",
642 bad_pages[i]);
643 }
644}
645
Yinghai Lu1b5576e2010-01-22 11:21:04 +0800646static void __init trim_bios_range(void)
647{
648 /*
Mike Rapoport5c279c42021-02-04 20:12:37 +0200649 * A special case is the first 4Kb of memory;
650 * This is a BIOS owned area, not kernel ram, but generally
651 * not listed as such in the E820 table.
652 *
653 * This typically reserves additional memory (64KiB by default)
654 * since some BIOSes are known to corrupt low memory. See the
655 * Kconfig help text for X86_RESERVE_LOW.
656 */
657 e820__range_update(0, PAGE_SIZE, E820_TYPE_RAM, E820_TYPE_RESERVED);
658
659 /*
Cao jin11a98f32019-11-18 15:00:12 +0800660 * special case: Some BIOSes report the PC BIOS
661 * area (640Kb -> 1Mb) as RAM even though it is not.
Yinghai Lu1b5576e2010-01-22 11:21:04 +0800662 * take them out.
663 */
Ingo Molnar09821ff2017-01-28 17:09:33 +0100664 e820__range_remove(BIOS_BEGIN, BIOS_END - BIOS_BEGIN, E820_TYPE_RAM, 1);
Jesse Barnesa9acc532012-11-14 20:43:31 +0000665
Ingo Molnarf9748fa2017-01-28 18:00:35 +0100666 e820__update_table(e820_table);
Yinghai Lu1b5576e2010-01-22 11:21:04 +0800667}
668
Yinghai Lub422a302013-01-24 12:19:45 -0800669/* called before trim_bios_range() to spare extra sanitize */
670static void __init e820_add_kernel_range(void)
671{
672 u64 start = __pa_symbol(_text);
673 u64 size = __pa_symbol(_end) - start;
674
675 /*
Ingo Molnar09821ff2017-01-28 17:09:33 +0100676 * Complain if .text .data and .bss are not marked as E820_TYPE_RAM and
Yinghai Lub422a302013-01-24 12:19:45 -0800677 * attempt to fix it by adding the range. We may have a confused BIOS,
678 * or the user may have used memmap=exactmap or memmap=xxM$yyM to
679 * exclude kernel range. If we really are running on top non-RAM,
680 * we will crash later anyways.
681 */
Ingo Molnar09821ff2017-01-28 17:09:33 +0100682 if (e820__mapped_all(start, start + size, E820_TYPE_RAM))
Yinghai Lub422a302013-01-24 12:19:45 -0800683 return;
684
Ingo Molnar09821ff2017-01-28 17:09:33 +0100685 pr_warn(".text .data .bss are not marked as E820_TYPE_RAM!\n");
686 e820__range_remove(start, size, E820_TYPE_RAM, 0);
687 e820__range_add(start, size, E820_TYPE_RAM);
Yinghai Lub422a302013-01-24 12:19:45 -0800688}
689
Mike Rapoporta799c2b2021-03-02 12:04:05 +0200690static void __init early_reserve_memory(void)
H. Peter Anvin95c96082013-02-14 14:02:52 -0800691{
Mike Rapoporta799c2b2021-03-02 12:04:05 +0200692 /*
693 * Reserve the memory occupied by the kernel between _text and
694 * __end_of_kernel_reserve symbols. Any kernel sections after the
695 * __end_of_kernel_reserve symbol must be explicitly reserved with a
696 * separate memblock_reserve() or they will be discarded.
697 */
698 memblock_reserve(__pa_symbol(_text),
699 (unsigned long)__end_of_kernel_reserve - (unsigned long)_text);
700
701 /*
Mike Rapoport4c674482021-03-02 12:04:06 +0200702 * The first 4Kb of memory is a BIOS owned area, but generally it is
703 * not listed as such in the E820 table.
704 *
Mike Rapoportf1d4d472021-06-01 10:53:52 +0300705 * Reserve the first 64K of memory since some BIOSes are known to
706 * corrupt low memory. After the real mode trampoline is allocated the
707 * rest of the memory below 640k is reserved.
Mike Rapoport4c674482021-03-02 12:04:06 +0200708 *
709 * In addition, make sure page 0 is always reserved because on
710 * systems with L1TF its contents can be leaked to user processes.
Mike Rapoporta799c2b2021-03-02 12:04:05 +0200711 */
Mike Rapoportf1d4d472021-06-01 10:53:52 +0300712 memblock_reserve(0, SZ_64K);
Mike Rapoporta799c2b2021-03-02 12:04:05 +0200713
714 early_reserve_initrd();
715
716 if (efi_enabled(EFI_BOOT))
717 efi_memblock_x86_reserve_range();
718
719 memblock_x86_reserve_range_setup_data();
720
721 reserve_ibft_region();
722 reserve_bios_regions();
Mike Rapoportf1d4d472021-06-01 10:53:52 +0300723 trim_snb_memory();
H. Peter Anvin95c96082013-02-14 14:02:52 -0800724}
Mike Rapoporta799c2b2021-03-02 12:04:05 +0200725
Jeremy Fitzhardinge5394f802008-09-07 01:51:32 -0700726/*
Kees Cookf32360e2013-10-10 17:18:17 -0700727 * Dump out kernel offset information on panic.
728 */
729static int
730dump_kernel_offset(struct notifier_block *self, unsigned long v, void *p)
731{
Borislav Petkov78cac482015-04-01 12:49:52 +0200732 if (kaslr_enabled()) {
733 pr_emerg("Kernel Offset: 0x%lx from 0x%lx (relocation range: 0x%lx-0x%lx)\n",
Jiri Kosina4545c892015-04-27 13:17:19 +0200734 kaslr_offset(),
Borislav Petkov78cac482015-04-01 12:49:52 +0200735 __START_KERNEL,
736 __START_KERNEL_map,
737 MODULES_VADDR-1);
738 } else {
739 pr_emerg("Kernel Offset: disabled\n");
740 }
Kees Cookf32360e2013-10-10 17:18:17 -0700741
742 return 0;
743}
744
745/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 * Determine if we were loaded by an EFI loader. If so, then we have also been
747 * passed the efi memmap, systab, etc., so we should use these data structures
748 * for initialization. Note, the efi init code path is determined by the
749 * global efi_enabled. This allows the same kernel image to be used on existing
750 * systems (with a traditional BIOS) as well as on EFI systems.
751 */
Yinghai Lu76934ed2008-06-25 17:52:35 -0700752/*
753 * setup_arch - architecture-specific boot-time initializations
754 *
755 * Note: On x86_64, fixmaps are ready for use even before this is called.
756 */
757
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758void __init setup_arch(char **cmdline_p)
759{
Yinghai Lu76934ed2008-06-25 17:52:35 -0700760#ifdef CONFIG_X86_32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 memcpy(&boot_cpu_data, &new_cpu_data, sizeof(new_cpu_data));
Borislav Petkovb40827fa2010-08-28 15:58:33 +0200762
763 /*
764 * copy kernel address range established so far and switch
765 * to the proper swapper page table
766 */
767 clone_pgd_range(swapper_pg_dir + KERNEL_PGD_BOUNDARY,
768 initial_page_table + KERNEL_PGD_BOUNDARY,
769 KERNEL_PGD_PTRS);
770
771 load_cr3(swapper_pg_dir);
Bryan O'Donoghue20752442014-10-07 01:19:48 +0100772 /*
773 * Note: Quark X1000 CPUs advertise PGE incorrectly and require
774 * a cr3 based tlb flush, so the following __flush_tlb_all()
Ingo Molnar360db4a2019-11-18 16:03:39 +0100775 * will not flush anything because the CPU quirk which clears
Bryan O'Donoghue20752442014-10-07 01:19:48 +0100776 * X86_FEATURE_PGE has not been invoked yet. Though due to the
777 * load_cr3() above the TLB has been flushed already. The
778 * quirk is invoked before subsequent calls to __flush_tlb_all()
779 * so proper operation is guaranteed.
780 */
Borislav Petkovb40827fa2010-08-28 15:58:33 +0200781 __flush_tlb_all();
Yinghai Lu76934ed2008-06-25 17:52:35 -0700782#else
783 printk(KERN_INFO "Command line: %s\n", boot_command_line);
Kirill A. Shutemov162434e2018-02-14 14:16:54 +0300784 boot_cpu_data.x86_phys_bits = MAX_PHYSMEM_BITS;
Yinghai Lu76934ed2008-06-25 17:52:35 -0700785#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786
Alok Kataria9863c902010-08-23 14:49:11 -0700787 /*
788 * If we have OLPC OFW, we might end up relocating the fixmap due to
789 * reserve_top(), so do this before touching the ioremap area.
790 */
Andres Salomonfd699c72010-06-18 17:46:53 -0400791 olpc_ofw_detect();
792
Thomas Gleixner433f8922017-08-28 08:47:50 +0200793 idt_setup_early_traps();
Jeremy Fitzhardinge9e882c92008-07-21 16:49:54 -0700794 early_cpu_init();
Pavel Tatashin8990cac2018-07-19 16:55:28 -0400795 jump_label_init();
Peter Zijlstraa945c832020-08-18 15:57:51 +0200796 static_call_init();
Jeremy Fitzhardinge1a98fd12008-06-29 20:02:44 -0700797 early_ioremap_init();
798
Andres Salomonfd699c72010-06-18 17:46:53 -0400799 setup_olpc_ofw_pgd();
800
H. Peter Anvin30c82642007-10-15 17:13:22 -0700801 ROOT_DEV = old_decode_dev(boot_params.hdr.root_dev);
802 screen_info = boot_params.screen_info;
803 edid_info = boot_params.edid_info;
Yinghai Lu76934ed2008-06-25 17:52:35 -0700804#ifdef CONFIG_X86_32
H. Peter Anvin30c82642007-10-15 17:13:22 -0700805 apm_info.bios = boot_params.apm_bios_info;
806 ist_info = boot_params.ist_info;
Yinghai Lu76934ed2008-06-25 17:52:35 -0700807#endif
808 saved_video_mode = boot_params.hdr.vid_mode;
H. Peter Anvin30c82642007-10-15 17:13:22 -0700809 bootloader_type = boot_params.hdr.type_of_loader;
H. Peter Anvin50312962009-05-07 16:54:11 -0700810 if ((bootloader_type >> 4) == 0xe) {
811 bootloader_type &= 0xf;
812 bootloader_type |= (boot_params.hdr.ext_loader_type+0x10) << 4;
813 }
814 bootloader_version = bootloader_type & 0xf;
815 bootloader_version |= boot_params.hdr.ext_loader_ver << 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816
817#ifdef CONFIG_BLK_DEV_RAM
H. Peter Anvin30c82642007-10-15 17:13:22 -0700818 rd_image_start = boot_params.hdr.ram_size & RAMDISK_IMAGE_START_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819#endif
Yinghai Lu74652522008-06-23 19:53:33 -0700820#ifdef CONFIG_EFI
821 if (!strncmp((char *)&boot_params.efi_info.efi_loader_signature,
Daniel Kiper94029732014-06-30 19:53:03 +0200822 EFI32_LOADER_SIGNATURE, 4)) {
Matt Fleming3e909592014-01-15 13:21:22 +0000823 set_bit(EFI_BOOT, &efi.flags);
Olof Johansson1adbfa32012-02-12 13:24:29 -0800824 } else if (!strncmp((char *)&boot_params.efi_info.efi_loader_signature,
Daniel Kiper94029732014-06-30 19:53:03 +0200825 EFI64_LOADER_SIGNATURE, 4)) {
Matt Fleming3e909592014-01-15 13:21:22 +0000826 set_bit(EFI_BOOT, &efi.flags);
827 set_bit(EFI_64BIT, &efi.flags);
Yinghai Lu74652522008-06-23 19:53:33 -0700828 }
829#endif
830
Thomas Gleixner42bbdb42009-08-20 13:04:10 +0200831 x86_init.oem.arch_setup();
Huang, Ying2215e692008-01-30 13:31:19 +0100832
Juergen Gross8aa83e62021-09-20 14:04:21 +0200833 /*
834 * Do some memory reservations *before* memory is added to memblock, so
835 * memblock allocations won't overwrite it.
836 *
837 * After this point, everything still needed from the boot loader or
838 * firmware or kernel text should be early reserved or marked not RAM in
839 * e820. All other memory is free game.
840 *
841 * This call needs to happen before e820__memory_setup() which calls the
842 * xen_memory_setup() on Xen dom0 which relies on the fact that those
843 * early reservations have happened already.
844 */
845 early_reserve_memory();
846
Bjorn Helgaas419afdf2010-10-26 15:41:49 -0600847 iomem_resource.end = (1ULL << boot_cpu_data.x86_phys_bits) - 1;
Ingo Molnar103e2062017-01-28 09:58:49 +0100848 e820__memory_setup();
Yinghai Lu28bb2232008-06-30 16:20:54 -0700849 parse_setup_data();
850
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 copy_edd();
852
H. Peter Anvin30c82642007-10-15 17:13:22 -0700853 if (!boot_params.hdr.root_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 root_mountflags &= ~MS_RDONLY;
Kefeng Wang30120d72021-07-07 18:09:03 -0700855 setup_initial_init_mm(_text, _etext, _edata, (void *)_brk_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856
Linus Torvalds4046d6e2016-04-14 11:18:57 -0700857 code_resource.start = __pa_symbol(_text);
858 code_resource.end = __pa_symbol(_etext)-1;
Kees Cooka3299752019-10-29 14:13:50 -0700859 rodata_resource.start = __pa_symbol(__start_rodata);
860 rodata_resource.end = __pa_symbol(__end_rodata)-1;
861 data_resource.start = __pa_symbol(_sdata);
Linus Torvalds4046d6e2016-04-14 11:18:57 -0700862 data_resource.end = __pa_symbol(_edata)-1;
863 bss_resource.start = __pa_symbol(__bss_start);
864 bss_resource.end = __pa_symbol(__bss_stop)-1;
865
Tim Bird516cbf32008-08-12 12:52:36 -0700866#ifdef CONFIG_CMDLINE_BOOL
867#ifdef CONFIG_CMDLINE_OVERRIDE
868 strlcpy(boot_command_line, builtin_cmdline, COMMAND_LINE_SIZE);
869#else
870 if (builtin_cmdline[0]) {
871 /* append boot loader cmdline to builtin */
872 strlcat(builtin_cmdline, " ", COMMAND_LINE_SIZE);
873 strlcat(builtin_cmdline, boot_command_line, COMMAND_LINE_SIZE);
874 strlcpy(boot_command_line, builtin_cmdline, COMMAND_LINE_SIZE);
875 }
876#endif
877#endif
878
Yinghai Lueda6da92009-09-19 11:07:57 -0700879 strlcpy(command_line, boot_command_line, COMMAND_LINE_SIZE);
880 *cmdline_p = command_line;
881
Yinghai Lueda6da92009-09-19 11:07:57 -0700882 /*
Kees Cook4b0f3b82009-11-13 15:28:17 -0800883 * x86_configure_nx() is called before parse_early_param() to detect
884 * whether hardware doesn't support NX (so that the early EHCI debug
885 * console setup can safely call set_fixmap()). It may then be called
886 * again from within noexec_setup() during parsing early parameters
887 * to honor the respective command line option.
Yinghai Lueda6da92009-09-19 11:07:57 -0700888 */
H. Peter Anvin4763ed42009-11-13 15:28:16 -0800889 x86_configure_nx();
Yinghai Lueda6da92009-09-19 11:07:57 -0700890
891 parse_early_param();
892
Reza Arbab39fa104d2016-12-12 16:42:55 -0800893#ifdef CONFIG_MEMORY_HOTPLUG
894 /*
895 * Memory used by the kernel cannot be hot-removed because Linux
896 * cannot migrate the kernel pages. When memory hotplug is
897 * enabled, we should prevent memblock from allocating memory
898 * for the kernel.
899 *
900 * ACPI SRAT records all hotpluggable memory ranges. But before
901 * SRAT is parsed, we don't know about it.
902 *
903 * The kernel image is loaded into memory at very early time. We
904 * cannot prevent this anyway. So on NUMA system, we set any
905 * node the kernel resides in as un-hotpluggable.
906 *
907 * Since on modern servers, one node could have double-digit
908 * gigabytes memory, we can assume the memory around the kernel
909 * image is also un-hotpluggable. So before SRAT is parsed, just
910 * allocate memory near the kernel image to try the best to keep
911 * the kernel away from hotpluggable memory.
912 */
913 if (movable_node_is_enabled())
914 memblock_set_bottom_up(true);
915#endif
916
Kees Cook4b0f3b82009-11-13 15:28:17 -0800917 x86_report_nx();
Jeremy Fitzhardinge0ad5bce2008-09-11 16:42:00 -0700918
Yinghai Lu76934ed2008-06-25 17:52:35 -0700919 if (acpi_mps_check()) {
Ingo Molnar3eb11ed2008-06-23 22:19:22 +0200920#ifdef CONFIG_X86_LOCAL_APIC
Yinghai Lu76934ed2008-06-25 17:52:35 -0700921 disable_apic = 1;
922#endif
Yinghai Lu988781d2008-07-21 11:21:43 -0700923 setup_clear_cpu_cap(X86_FEATURE_APIC);
Yinghai Lu3c999f12008-06-20 16:11:20 -0700924 }
925
Ingo Molnar1a127032017-01-28 22:27:28 +0100926 e820__reserve_setup_data();
Ingo Molnar9641bda2017-01-28 13:37:17 +0100927 e820__finish_early_params();
Rusty Russell1a3f2392006-09-26 10:52:32 +0200928
Matt Fleming83e68182012-11-14 09:42:35 +0000929 if (efi_enabled(EFI_BOOT))
Brian Malyff0c0872009-03-03 21:55:31 -0500930 efi_init();
931
Robert Richter0fca0812019-03-28 20:34:28 +0100932 dmi_setup();
Yinghai Lu2216d192008-09-22 02:52:26 -0700933
Alok Kataria88b094f2008-10-27 10:41:46 -0700934 /*
935 * VMware detection requires dmi to be available, so this
Robert Richter0fca0812019-03-28 20:34:28 +0100936 * needs to be done after dmi_setup(), for the boot CPU.
Alok Kataria88b094f2008-10-27 10:41:46 -0700937 */
Thomas Gleixner2d826402009-08-20 17:06:25 +0200938 init_hypervisor_platform();
Alok Kataria88b094f2008-10-27 10:41:46 -0700939
Pavel Tatashincf7a63e2018-07-19 16:55:38 -0400940 tsc_early_init();
Thomas Gleixnerf7cf5a52009-08-19 14:43:56 +0200941 x86_init.resources.probe_roms();
Yinghai Lu41c094f2008-06-16 13:03:31 -0700942
Linus Torvalds4046d6e2016-04-14 11:18:57 -0700943 /* after parse_early_param, so could debug it */
944 insert_resource(&iomem_resource, &code_resource);
Kees Cooka3299752019-10-29 14:13:50 -0700945 insert_resource(&iomem_resource, &rodata_resource);
Linus Torvalds4046d6e2016-04-14 11:18:57 -0700946 insert_resource(&iomem_resource, &data_resource);
947 insert_resource(&iomem_resource, &bss_resource);
948
Yinghai Lub422a302013-01-24 12:19:45 -0800949 e820_add_kernel_range();
Yinghai Lu1b5576e2010-01-22 11:21:04 +0800950 trim_bios_range();
Yinghai Lu76934ed2008-06-25 17:52:35 -0700951#ifdef CONFIG_X86_32
Yinghai Lucc9f7a02008-06-16 16:11:08 -0700952 if (ppro_with_ram_bug()) {
Ingo Molnar09821ff2017-01-28 17:09:33 +0100953 e820__range_update(0x70000000ULL, 0x40000ULL, E820_TYPE_RAM,
954 E820_TYPE_RESERVED);
Ingo Molnarf9748fa2017-01-28 18:00:35 +0100955 e820__update_table(e820_table);
Yinghai Lucc9f7a02008-06-16 16:11:08 -0700956 printk(KERN_INFO "fixed physical RAM map:\n");
Ingo Molnarbe0c3f02017-01-28 14:24:02 +0100957 e820__print_table("bad_ppro");
Yinghai Lucc9f7a02008-06-16 16:11:08 -0700958 }
Yinghai Lu76934ed2008-06-25 17:52:35 -0700959#else
960 early_gart_iommu_check();
961#endif
Yinghai Lucc9f7a02008-06-16 16:11:08 -0700962
Yinghai Lu7b2a0a62008-06-03 19:35:04 -0700963 /*
964 * partially used pages are not usable - thus
965 * we are rounding upwards:
966 */
Ingo Molnar0c6fc112017-01-28 22:52:16 +0100967 max_pfn = e820__end_of_ram_pfn();
Yinghai Lu7b2a0a62008-06-03 19:35:04 -0700968
Yinghai Lu093af8d2008-01-30 13:33:32 +0100969 /* update e820 for memory not covered by WB MTRRs */
970 mtrr_bp_init();
Yinghai Lu2dc807b2008-07-08 18:56:38 -0700971 if (mtrr_trim_uncached_memory(max_pfn))
Ingo Molnar0c6fc112017-01-28 22:52:16 +0100972 max_pfn = e820__end_of_ram_pfn();
Yinghai Lu76c324182008-03-23 00:16:49 -0700973
Igor Mammedov8dd33032015-12-04 14:07:05 +0100974 max_possible_pfn = max_pfn;
975
Thomas Garnierc7d23612016-08-09 10:11:04 -0700976 /*
Mikulas Patocka99c13b82017-07-04 19:04:23 -0400977 * This call is required when the CPU does not support PAT. If
978 * mtrr_bp_init() invoked it already via pat_init() the call has no
979 * effect.
980 */
981 init_cache_modes();
982
983 /*
Thomas Garnierc7d23612016-08-09 10:11:04 -0700984 * Define random base addresses for memory sections after max_pfn is
985 * defined and before each memory section base is used.
986 */
987 kernel_randomize_memory();
988
Yinghai Lu76934ed2008-06-25 17:52:35 -0700989#ifdef CONFIG_X86_32
Yinghai Lu4e296842008-06-24 12:18:14 -0700990 /* max_low_pfn get updated here */
Yinghai Lu2ec65f82008-06-23 03:05:30 -0700991 find_low_pfn_range();
Yinghai Lu76934ed2008-06-25 17:52:35 -0700992#else
Yinghai Lu06cd9a72009-02-16 17:29:58 -0800993 check_x2apic();
Yinghai Lu76934ed2008-06-25 17:52:35 -0700994
995 /* How many end-of-memory variables you have, grandma! */
996 /* need this before calling reserve_initrd */
Yinghai Luf361a452008-07-10 20:38:26 -0700997 if (max_pfn > (1UL<<(32 - PAGE_SHIFT)))
Ingo Molnar0c6fc112017-01-28 22:52:16 +0100998 max_low_pfn = e820__end_of_low_ram_pfn();
Yinghai Luf361a452008-07-10 20:38:26 -0700999 else
1000 max_low_pfn = max_pfn;
1001
Yinghai Lu76934ed2008-06-25 17:52:35 -07001002 high_memory = (void *)__va(max_pfn * PAGE_SIZE - 1) + 1;
1003#endif
Yinghai Lu2ec65f82008-06-23 03:05:30 -07001004
Yinghai Lu893f38d2009-12-10 13:07:22 -08001005 /*
1006 * Find and reserve possible boot-time SMP configuration:
1007 */
1008 find_smp_config();
1009
Yinghai Lu8d574702012-11-16 19:38:58 -08001010 early_alloc_pgt_buf();
1011
Yinghai Lu72d7c3b2010-08-25 13:39:17 -07001012 /*
Ingo Molnar4918e222017-01-28 11:37:42 +01001013 * Need to conclude brk, before e820__memblock_setup()
Cao jin81519f72021-03-11 16:39:19 +08001014 * it could use memblock_find_in_range, could overlap with
1015 * brk area.
Yinghai Lu72d7c3b2010-08-25 13:39:17 -07001016 */
1017 reserve_brk();
1018
Yinghai Lue5f15b42011-02-18 11:30:30 +00001019 cleanup_highmap();
1020
Tang Chen2449f342013-08-14 11:44:04 +08001021 memblock_set_current_limit(ISA_END_ADDRESS);
Ingo Molnar4918e222017-01-28 11:37:42 +01001022 e820__memblock_setup();
Yinghai Lu72d7c3b2010-08-25 13:39:17 -07001023
Ashish Kalrae9988792020-12-10 01:25:15 +00001024 /*
1025 * Needs to run after memblock setup because it needs the physical
1026 * memory size.
1027 */
1028 sev_setup_arch();
1029
Dan Williams6950e312019-11-06 17:43:05 -08001030 efi_fake_memmap();
1031 efi_find_mirror();
1032 efi_esrt_init();
Lenny Szubowicz58c90902020-09-04 21:31:05 -04001033 efi_mokvar_table_init();
Matt Fleming49715312016-06-21 23:11:38 +01001034
Dan Williams6950e312019-11-06 17:43:05 -08001035 /*
1036 * The EFI specification says that boot service code won't be
1037 * called after ExitBootServices(). This is, in fact, a lie.
1038 */
1039 efi_reserve_boot_services();
Matthew Garrett916f6762011-05-25 09:53:13 -04001040
Yinghai Lu72d7c3b2010-08-25 13:39:17 -07001041 /* preallocate 4k for mptable mpc */
Ingo Molnar5da217c2017-01-28 13:46:28 +01001042 e820__memblock_alloc_reserved_mpc_new();
Yinghai Lu72d7c3b2010-08-25 13:39:17 -07001043
1044#ifdef CONFIG_X86_CHECK_BIOS_CORRUPTION
1045 setup_bios_corruption_check();
1046#endif
1047
Yinghai Lu10054232013-01-24 12:19:54 -08001048#ifdef CONFIG_X86_32
Bjorn Helgaas365811d2012-05-29 15:06:29 -07001049 printk(KERN_DEBUG "initial memory mapped: [mem 0x00000000-%#010lx]\n",
1050 (max_pfn_mapped<<PAGE_SHIFT) - 1);
Yinghai Lu10054232013-01-24 12:19:54 -08001051#endif
Yinghai Lu72d7c3b2010-08-25 13:39:17 -07001052
Mike Rapoportc361e5d2021-04-13 21:08:39 +03001053 /*
Borislav Petkovec35d1d2021-06-08 22:17:10 +02001054 * Find free memory for the real mode trampoline and place it there. If
1055 * there is not enough free memory under 1M, on EFI-enabled systems
1056 * there will be additional attempt to reclaim the memory for the real
1057 * mode trampoline at efi_free_boot_services().
Mike Rapoportf1d4d472021-06-01 10:53:52 +03001058 *
Borislav Petkovec35d1d2021-06-08 22:17:10 +02001059 * Unconditionally reserve the entire first 1M of RAM because BIOSes
1060 * are known to corrupt low memory and several hundred kilobytes are not
1061 * worth complex detection what memory gets clobbered. Windows does the
1062 * same thing for very similar reasons.
1063 *
1064 * Moreover, on machines with SandyBridge graphics or in setups that use
1065 * crashkernel the entire 1M is reserved anyway.
Mike Rapoportc361e5d2021-04-13 21:08:39 +03001066 */
Mike Rapoportf1d4d472021-06-01 10:53:52 +03001067 reserve_real_mode();
Jesse Barnesa9acc532012-11-14 20:43:31 +00001068
Yinghai Lu22ddfca2012-11-16 19:38:41 -08001069 init_mem_mapping();
Pekka J Enberg854c8792009-06-22 17:39:41 +03001070
Thomas Gleixner433f8922017-08-28 08:47:50 +02001071 idt_setup_early_pf();
Jarkko Sakkinencda846f2012-05-08 21:22:46 +03001072
Andy Lutomirski18bc7bd2016-08-10 02:29:14 -07001073 /*
1074 * Update mmu_cr4_features (and, indirectly, trampoline_cr4_features)
1075 * with the current CR4 value. This may not be necessary, but
1076 * auditing all the early-boot CR4 manipulation would be needed to
1077 * rule it out.
Andy Lutomirskic7ad5ad2017-09-10 17:48:27 -07001078 *
1079 * Mask off features that don't work outside long mode (just
1080 * PCIDE for now).
Andy Lutomirski18bc7bd2016-08-10 02:29:14 -07001081 */
Andy Lutomirskic7ad5ad2017-09-10 17:48:27 -07001082 mmu_cr4_features = __read_cr4() & ~X86_CR4_PCIDE;
Andy Lutomirski18bc7bd2016-08-10 02:29:14 -07001083
Yinghai Lu4ce7a862014-01-27 17:06:50 -08001084 memblock_set_current_limit(get_max_mapped());
Yinghai Lue7b37892008-06-25 21:51:28 -07001085
1086 /*
1087 * NOTE: On x86-32, only from this point on, fixmaps are ready for use.
1088 */
1089
1090#ifdef CONFIG_PROVIDE_OHCI1394_DMA_INIT
1091 if (init_ohci1394_dma_early)
Mike Travis162a7e72011-05-24 17:13:20 -07001092 init_ohci1394_dma_on_all_controllers();
1093#endif
Yinghai Lue7b37892008-06-25 21:51:28 -07001094 /* Allocate bigger log buffer */
Yinghai Lu2ec65f82008-06-23 03:05:30 -07001095 setup_log_buf(1);
1096
David Howells9661b332017-02-06 11:22:45 +00001097 if (efi_enabled(EFI_BOOT)) {
1098 switch (boot_params.secure_boot) {
1099 case efi_secureboot_mode_disabled:
1100 pr_info("Secure boot disabled\n");
1101 break;
1102 case efi_secureboot_mode_enabled:
1103 pr_info("Secure boot enabled\n");
1104 break;
1105 default:
1106 pr_info("Secure boot could not be determined\n");
1107 break;
1108 }
1109 }
1110
Yinghai Lu44280732009-11-22 17:18:49 -08001111 reserve_initrd();
1112
Aleksey Makarovda3d3f92016-06-20 13:56:10 +03001113 acpi_table_upgrade();
Rafael J. Wysocki6998a882021-04-13 16:01:00 +02001114 /* Look for ACPI tables and reserve memory occupied by them. */
1115 acpi_boot_table_init();
Yinghai Lu1c6e5502008-06-17 15:41:45 -07001116
Yinghai Lu1c6e5502008-06-17 15:41:45 -07001117 vsmp_init();
1118
Yinghai Lu1c6e5502008-06-17 15:41:45 -07001119 io_delay_init();
1120
Lukas Wunner630b3af2017-08-01 14:10:41 +02001121 early_platform_quirks();
1122
Yinghai Lu20e69262013-03-01 14:51:27 -08001123 early_acpi_boot_init();
1124
Bernhard Walle91467bd2008-07-18 19:07:53 +02001125 initmem_init();
Weijie Yang3c325f82014-10-24 17:00:34 +08001126 dma_contiguous_reserve(max_pfn_mapped << PAGE_SHIFT);
Tang Chenfa591c42013-11-12 15:08:07 -08001127
Roman Gushchincf11e852020-04-10 14:32:45 -07001128 if (boot_cpu_has(X86_FEATURE_GBPAGES))
1129 hugetlb_cma_reserve(PUD_SHIFT - PAGE_SHIFT);
1130
Tang Chenfa591c42013-11-12 15:08:07 -08001131 /*
1132 * Reserve memory for crash kernel after SRAT is parsed so that it
1133 * won't consume hotpluggable memory.
1134 */
1135 reserve_crashkernel();
1136
Glauber de Oliveira Costa790c73f2008-02-15 17:52:48 -02001137 memblock_find_dma_reserve();
1138
Boris Ostrovskyccb64942017-09-11 14:51:11 -04001139 if (!early_xdbc_setup_hardware())
1140 early_xdbc_register_console();
1141
Thomas Gleixner030cb6c2009-08-20 14:30:02 +02001142 x86_init.paging.pagetable_init();
Bernhard Kaindlf212ec42008-01-30 13:34:11 +01001143
Andrey Ryabininef7f0d62015-02-13 14:39:25 -08001144 kasan_init();
1145
Andy Lutomirskid2b6dc62017-05-08 17:09:10 -07001146 /*
Thomas Gleixner945fd172018-02-28 21:14:26 +01001147 * Sync back kernel address range.
1148 *
1149 * FIXME: Can the later sync in setup_cpu_entry_areas() replace
1150 * this call?
Andy Lutomirskid2b6dc62017-05-08 17:09:10 -07001151 */
Thomas Gleixner945fd172018-02-28 21:14:26 +01001152 sync_initial_page_table();
Andy Lutomirskid2b6dc62017-05-08 17:09:10 -07001153
Joseph Cihula31625342009-06-30 19:30:59 -07001154 tboot_probe();
1155
Yinghai Lu76934ed2008-06-25 17:52:35 -07001156 map_vsyscall();
Yinghai Lu76934ed2008-06-25 17:52:35 -07001157
Rusty Russell1a3f2392006-09-26 10:52:32 +02001158 generic_apic_probe();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159
Andi Kleen54ef3402007-10-19 20:35:03 +02001160 early_quirks();
Andy Curridd44647b2006-06-08 00:43:38 -07001161
Yinghai Lu295deae2008-06-23 19:55:05 -07001162 /*
1163 * Read APIC and some other early information from ACPI tables.
1164 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 acpi_boot_init();
Thomas Gleixnera906fda2011-02-25 16:09:31 +01001166 x86_dtb_init();
Feng Tangefafc8b2009-08-14 15:23:29 -04001167
Yinghai Lu295deae2008-06-23 19:55:05 -07001168 /*
1169 * get boot-time SMP configuration:
1170 */
Baoquan Hea91bf712016-08-12 14:57:12 +08001171 get_smp_config();
Yinghai Lu76934ed2008-06-25 17:52:35 -07001172
Thomas Gleixner1e90a132016-10-29 13:42:42 +02001173 /*
1174 * Systems w/o ACPI and mptables might not have it mapped the local
1175 * APIC yet, but prefill_possible_map() might need to access it.
1176 */
1177 init_apic_mappings();
1178
Yinghai Lu329513a2008-07-02 18:54:40 -07001179 prefill_possible_map();
Yinghai Lu301e6192008-08-19 20:50:02 -07001180
Yinghai Lu5f4765f2008-07-02 18:53:44 -07001181 init_cpu_to_node();
Jonathan Cameron73bf7382020-09-30 22:05:43 +08001182 init_gi_nodes();
Yinghai Lu5f4765f2008-07-02 18:53:44 -07001183
Thomas Gleixnerca1b8862015-04-24 13:57:48 +02001184 io_apic_init_mappings();
Yinghai Lu9d6a4d02008-08-19 20:50:52 -07001185
Juergen Grossf3614642017-11-09 14:27:38 +01001186 x86_init.hyper.guest_late_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187
Ingo Molnar1506c8d2017-01-28 22:41:14 +01001188 e820__reserve_resources();
Zhimin Gucc55f752018-09-21 14:26:24 +08001189 e820__register_nosave_regions(max_pfn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190
Thomas Gleixner8fee6972009-08-19 14:55:50 +02001191 x86_init.resources.reserve_resources();
Yinghai Lu41c094f2008-06-16 13:03:31 -07001192
Ingo Molnar2df908b2017-01-28 14:16:38 +01001193 e820__setup_pci_gap();
Yinghai Lu41c094f2008-06-16 13:03:31 -07001194
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195#ifdef CONFIG_VT
1196#if defined(CONFIG_VGA_CONSOLE)
Matt Fleming83e68182012-11-14 09:42:35 +00001197 if (!efi_enabled(EFI_BOOT) || (efi_mem_type(0xa0000) != EFI_CONVENTIONAL_MEMORY))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 conswitchp = &vga_con;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199#endif
1200#endif
Thomas Gleixner6f30c1a2009-08-20 13:19:57 +02001201 x86_init.oem.banner();
Yong Wanga2202aa2009-11-10 09:38:24 +08001202
Feng Tang6b617e22011-02-15 00:13:31 +08001203 x86_init.timers.wallclock_init();
1204
Borislav Petkov9a90ed02021-05-27 11:02:26 +02001205 /*
1206 * This needs to run before setup_local_APIC() which soft-disables the
1207 * local APIC temporarily and that masks the thermal LVT interrupt,
1208 * leading to softlockups on machines which have configured SMI
1209 * interrupt delivery.
1210 */
1211 therm_lvt_init();
1212
Yong Wanga2202aa2009-11-10 09:38:24 +08001213 mcheck_init();
Jason Baronf49aa442010-09-17 11:08:51 -04001214
John Stultzb3c869d2012-09-04 12:42:27 -04001215 register_refined_jiffies(CLOCK_TICK_RATE);
Olof Johansson5189c2a2012-10-24 10:00:44 -07001216
1217#ifdef CONFIG_EFI
Borislav Petkova5d90c92014-03-04 17:02:17 +01001218 if (efi_enabled(EFI_BOOT))
1219 efi_apply_memmap_quirks();
Olof Johansson5189c2a2012-10-24 10:00:44 -07001220#endif
Josh Poimboeufee9f8fc2017-07-24 18:36:57 -05001221
1222 unwind_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223}
Ingo Molnar5649b7c2008-09-16 09:29:09 +02001224
Ingo Molnar9be1b562009-02-17 23:12:48 +01001225#ifdef CONFIG_X86_32
Ingo Molnar5649b7c2008-09-16 09:29:09 +02001226
Thomas Gleixner8fee6972009-08-19 14:55:50 +02001227static struct resource video_ram_resource = {
1228 .name = "Video RAM area",
1229 .start = 0xa0000,
1230 .end = 0xbffff,
1231 .flags = IORESOURCE_BUSY | IORESOURCE_MEM
Ingo Molnar9be1b562009-02-17 23:12:48 +01001232};
1233
Thomas Gleixner8fee6972009-08-19 14:55:50 +02001234void __init i386_reserve_resources(void)
Ingo Molnar9be1b562009-02-17 23:12:48 +01001235{
Thomas Gleixner8fee6972009-08-19 14:55:50 +02001236 request_resource(&iomem_resource, &video_ram_resource);
1237 reserve_standard_io_resources();
Ingo Molnar9be1b562009-02-17 23:12:48 +01001238}
1239
Ingo Molnar9be1b562009-02-17 23:12:48 +01001240#endif /* CONFIG_X86_32 */
Kees Cookf32360e2013-10-10 17:18:17 -07001241
1242static struct notifier_block kernel_offset_notifier = {
1243 .notifier_call = dump_kernel_offset
1244};
1245
1246static int __init register_kernel_offset_dumper(void)
1247{
1248 atomic_notifier_chain_register(&panic_notifier_list,
1249 &kernel_offset_notifier);
1250 return 0;
1251}
1252__initcall(register_kernel_offset_dumper);