setup.c 32 KB

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