setup.c 31 KB

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