setup.c 30 KB

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