setup.c 31 KB

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