setup.c 32 KB

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