fadump.c 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Firmware Assisted dump: A robust mechanism to get reliable kernel crash
  4. * dump with assistance from firmware. This approach does not use kexec,
  5. * instead firmware assists in booting the kdump kernel while preserving
  6. * memory contents. The most of the code implementation has been adapted
  7. * from phyp assisted dump implementation written by Linas Vepstas and
  8. * Manish Ahuja
  9. *
  10. * Copyright 2011 IBM Corporation
  11. * Author: Mahesh Salgaonkar <mahesh@linux.vnet.ibm.com>
  12. */
  13. #undef DEBUG
  14. #define pr_fmt(fmt) "fadump: " fmt
  15. #include <linux/string.h>
  16. #include <linux/memblock.h>
  17. #include <linux/delay.h>
  18. #include <linux/seq_file.h>
  19. #include <linux/crash_dump.h>
  20. #include <linux/kobject.h>
  21. #include <linux/sysfs.h>
  22. #include <linux/slab.h>
  23. #include <linux/cma.h>
  24. #include <linux/hugetlb.h>
  25. #include <linux/debugfs.h>
  26. #include <linux/of.h>
  27. #include <linux/of_fdt.h>
  28. #include <asm/page.h>
  29. #include <asm/fadump.h>
  30. #include <asm/fadump-internal.h>
  31. #include <asm/setup.h>
  32. #include <asm/interrupt.h>
  33. /*
  34. * The CPU who acquired the lock to trigger the fadump crash should
  35. * wait for other CPUs to enter.
  36. *
  37. * The timeout is in milliseconds.
  38. */
  39. #define CRASH_TIMEOUT 500
  40. static struct fw_dump fw_dump;
  41. static void __init fadump_reserve_crash_area(u64 base);
  42. #ifndef CONFIG_PRESERVE_FA_DUMP
  43. static struct kobject *fadump_kobj;
  44. static atomic_t cpus_in_fadump;
  45. static DEFINE_MUTEX(fadump_mutex);
  46. #define RESERVED_RNGS_SZ 16384 /* 16K - 128 entries */
  47. #define RESERVED_RNGS_CNT (RESERVED_RNGS_SZ / \
  48. sizeof(struct fadump_memory_range))
  49. static struct fadump_memory_range rngs[RESERVED_RNGS_CNT];
  50. static struct fadump_mrange_info
  51. reserved_mrange_info = { "reserved", rngs, RESERVED_RNGS_SZ, 0, RESERVED_RNGS_CNT, true };
  52. static void __init early_init_dt_scan_reserved_ranges(unsigned long node);
  53. #ifdef CONFIG_CMA
  54. static struct cma *fadump_cma;
  55. /*
  56. * fadump_cma_init() - Initialize CMA area from a fadump reserved memory
  57. *
  58. * This function initializes CMA area from fadump reserved memory.
  59. * The total size of fadump reserved memory covers for boot memory size
  60. * + cpu data size + hpte size and metadata.
  61. * Initialize only the area equivalent to boot memory size for CMA use.
  62. * The remaining portion of fadump reserved memory will be not given
  63. * to CMA and pages for those will stay reserved. boot memory size is
  64. * aligned per CMA requirement to satisy cma_init_reserved_mem() call.
  65. * But for some reason even if it fails we still have the memory reservation
  66. * with us and we can still continue doing fadump.
  67. */
  68. void __init fadump_cma_init(void)
  69. {
  70. unsigned long long base, size;
  71. int rc;
  72. if (!fw_dump.fadump_supported || !fw_dump.fadump_enabled ||
  73. fw_dump.dump_active)
  74. return;
  75. /*
  76. * Do not use CMA if user has provided fadump=nocma kernel parameter.
  77. */
  78. if (fw_dump.nocma || !fw_dump.boot_memory_size)
  79. return;
  80. base = fw_dump.reserve_dump_area_start;
  81. size = fw_dump.boot_memory_size;
  82. rc = cma_init_reserved_mem(base, size, 0, "fadump_cma", &fadump_cma);
  83. if (rc) {
  84. pr_err("Failed to init cma area for firmware-assisted dump,%d\n", rc);
  85. /*
  86. * Though the CMA init has failed we still have memory
  87. * reservation with us. The reserved memory will be
  88. * blocked from production system usage. Hence return 1,
  89. * so that we can continue with fadump.
  90. */
  91. return;
  92. }
  93. /*
  94. * If CMA activation fails, keep the pages reserved, instead of
  95. * exposing them to buddy allocator. Same as 'fadump=nocma' case.
  96. */
  97. cma_reserve_pages_on_error(fadump_cma);
  98. /*
  99. * So we now have successfully initialized cma area for fadump.
  100. */
  101. pr_info("Initialized 0x%lx bytes cma area at %ldMB from 0x%lx "
  102. "bytes of memory reserved for firmware-assisted dump\n",
  103. cma_get_size(fadump_cma),
  104. (unsigned long)cma_get_base(fadump_cma) >> 20,
  105. fw_dump.reserve_dump_area_size);
  106. }
  107. #endif /* CONFIG_CMA */
  108. /*
  109. * Additional parameters meant for capture kernel are placed in a dedicated area.
  110. * If this is capture kernel boot, append these parameters to bootargs.
  111. */
  112. void __init fadump_append_bootargs(void)
  113. {
  114. char *append_args;
  115. size_t len;
  116. if (!fw_dump.dump_active || !fw_dump.param_area_supported || !fw_dump.param_area)
  117. return;
  118. if (fw_dump.param_area < fw_dump.boot_mem_top) {
  119. if (memblock_reserve(fw_dump.param_area, COMMAND_LINE_SIZE)) {
  120. pr_warn("WARNING: Can't use additional parameters area!\n");
  121. fw_dump.param_area = 0;
  122. return;
  123. }
  124. }
  125. append_args = (char *)fw_dump.param_area;
  126. len = strlen(boot_command_line);
  127. /*
  128. * Too late to fail even if cmdline size exceeds. Truncate additional parameters
  129. * to cmdline size and proceed anyway.
  130. */
  131. if (len + strlen(append_args) >= COMMAND_LINE_SIZE - 1)
  132. pr_warn("WARNING: Appending parameters exceeds cmdline size. Truncating!\n");
  133. pr_debug("Cmdline: %s\n", boot_command_line);
  134. snprintf(boot_command_line + len, COMMAND_LINE_SIZE - len, " %s", append_args);
  135. pr_info("Updated cmdline: %s\n", boot_command_line);
  136. }
  137. /* Scan the Firmware Assisted dump configuration details. */
  138. int __init early_init_dt_scan_fw_dump(unsigned long node, const char *uname,
  139. int depth, void *data)
  140. {
  141. if (depth == 0) {
  142. early_init_dt_scan_reserved_ranges(node);
  143. return 0;
  144. }
  145. if (depth != 1)
  146. return 0;
  147. if (strcmp(uname, "rtas") == 0) {
  148. rtas_fadump_dt_scan(&fw_dump, node);
  149. return 1;
  150. }
  151. if (strcmp(uname, "ibm,opal") == 0) {
  152. opal_fadump_dt_scan(&fw_dump, node);
  153. return 1;
  154. }
  155. return 0;
  156. }
  157. /*
  158. * If fadump is registered, check if the memory provided
  159. * falls within boot memory area and reserved memory area.
  160. */
  161. int is_fadump_memory_area(u64 addr, unsigned long size)
  162. {
  163. u64 d_start, d_end;
  164. if (!fw_dump.dump_registered)
  165. return 0;
  166. if (!size)
  167. return 0;
  168. d_start = fw_dump.reserve_dump_area_start;
  169. d_end = d_start + fw_dump.reserve_dump_area_size;
  170. if (((addr + size) > d_start) && (addr <= d_end))
  171. return 1;
  172. return (addr <= fw_dump.boot_mem_top);
  173. }
  174. int should_fadump_crash(void)
  175. {
  176. if (!fw_dump.dump_registered || !fw_dump.fadumphdr_addr)
  177. return 0;
  178. return 1;
  179. }
  180. int is_fadump_active(void)
  181. {
  182. return fw_dump.dump_active;
  183. }
  184. /*
  185. * Returns true, if there are no holes in memory area between d_start to d_end,
  186. * false otherwise.
  187. */
  188. static bool is_fadump_mem_area_contiguous(u64 d_start, u64 d_end)
  189. {
  190. phys_addr_t reg_start, reg_end;
  191. bool ret = false;
  192. u64 i, start, end;
  193. for_each_mem_range(i, &reg_start, &reg_end) {
  194. start = max_t(u64, d_start, reg_start);
  195. end = min_t(u64, d_end, reg_end);
  196. if (d_start < end) {
  197. /* Memory hole from d_start to start */
  198. if (start > d_start)
  199. break;
  200. if (end == d_end) {
  201. ret = true;
  202. break;
  203. }
  204. d_start = end + 1;
  205. }
  206. }
  207. return ret;
  208. }
  209. /*
  210. * Returns true, if there are no holes in reserved memory area,
  211. * false otherwise.
  212. */
  213. bool is_fadump_reserved_mem_contiguous(void)
  214. {
  215. u64 d_start, d_end;
  216. d_start = fw_dump.reserve_dump_area_start;
  217. d_end = d_start + fw_dump.reserve_dump_area_size;
  218. return is_fadump_mem_area_contiguous(d_start, d_end);
  219. }
  220. /* Print firmware assisted dump configurations for debugging purpose. */
  221. static void __init fadump_show_config(void)
  222. {
  223. int i;
  224. pr_debug("Support for firmware-assisted dump (fadump): %s\n",
  225. (fw_dump.fadump_supported ? "present" : "no support"));
  226. if (!fw_dump.fadump_supported)
  227. return;
  228. pr_debug("Fadump enabled : %s\n",
  229. (fw_dump.fadump_enabled ? "yes" : "no"));
  230. pr_debug("Dump Active : %s\n",
  231. (fw_dump.dump_active ? "yes" : "no"));
  232. pr_debug("Dump section sizes:\n");
  233. pr_debug(" CPU state data size: %lx\n", fw_dump.cpu_state_data_size);
  234. pr_debug(" HPTE region size : %lx\n", fw_dump.hpte_region_size);
  235. pr_debug(" Boot memory size : %lx\n", fw_dump.boot_memory_size);
  236. pr_debug(" Boot memory top : %llx\n", fw_dump.boot_mem_top);
  237. pr_debug("Boot memory regions cnt: %llx\n", fw_dump.boot_mem_regs_cnt);
  238. for (i = 0; i < fw_dump.boot_mem_regs_cnt; i++) {
  239. pr_debug("[%03d] base = %llx, size = %llx\n", i,
  240. fw_dump.boot_mem_addr[i], fw_dump.boot_mem_sz[i]);
  241. }
  242. }
  243. /**
  244. * fadump_calculate_reserve_size(): reserve variable boot area 5% of System RAM
  245. *
  246. * Function to find the largest memory size we need to reserve during early
  247. * boot process. This will be the size of the memory that is required for a
  248. * kernel to boot successfully.
  249. *
  250. * This function has been taken from phyp-assisted dump feature implementation.
  251. *
  252. * returns larger of 256MB or 5% rounded down to multiples of 256MB.
  253. *
  254. * TODO: Come up with better approach to find out more accurate memory size
  255. * that is required for a kernel to boot successfully.
  256. *
  257. */
  258. static __init u64 fadump_calculate_reserve_size(void)
  259. {
  260. u64 base, size, bootmem_min;
  261. int ret;
  262. if (fw_dump.reserve_bootvar)
  263. pr_warn("'fadump_reserve_mem=' parameter is deprecated in favor of 'crashkernel=' parameter.\n");
  264. /*
  265. * Check if the size is specified through crashkernel= cmdline
  266. * option. If yes, then use that but ignore base as fadump reserves
  267. * memory at a predefined offset.
  268. */
  269. ret = parse_crashkernel(boot_command_line, memblock_phys_mem_size(),
  270. &size, &base, NULL, NULL);
  271. if (ret == 0 && size > 0) {
  272. unsigned long max_size;
  273. if (fw_dump.reserve_bootvar)
  274. pr_info("Using 'crashkernel=' parameter for memory reservation.\n");
  275. fw_dump.reserve_bootvar = (unsigned long)size;
  276. /*
  277. * Adjust if the boot memory size specified is above
  278. * the upper limit.
  279. */
  280. max_size = memblock_phys_mem_size() / MAX_BOOT_MEM_RATIO;
  281. if (fw_dump.reserve_bootvar > max_size) {
  282. fw_dump.reserve_bootvar = max_size;
  283. pr_info("Adjusted boot memory size to %luMB\n",
  284. (fw_dump.reserve_bootvar >> 20));
  285. }
  286. return fw_dump.reserve_bootvar;
  287. } else if (fw_dump.reserve_bootvar) {
  288. /*
  289. * 'fadump_reserve_mem=' is being used to reserve memory
  290. * for firmware-assisted dump.
  291. */
  292. return fw_dump.reserve_bootvar;
  293. }
  294. /* divide by 20 to get 5% of value */
  295. size = memblock_phys_mem_size() / 20;
  296. /* round it down in multiples of 256 */
  297. size = size & ~0x0FFFFFFFUL;
  298. /* Truncate to memory_limit. We don't want to over reserve the memory.*/
  299. if (memory_limit && size > memory_limit)
  300. size = memory_limit;
  301. bootmem_min = fw_dump.ops->fadump_get_bootmem_min();
  302. return (size > bootmem_min ? size : bootmem_min);
  303. }
  304. /*
  305. * Calculate the total memory size required to be reserved for
  306. * firmware-assisted dump registration.
  307. */
  308. static unsigned long __init get_fadump_area_size(void)
  309. {
  310. unsigned long size = 0;
  311. size += fw_dump.cpu_state_data_size;
  312. size += fw_dump.hpte_region_size;
  313. /*
  314. * Account for pagesize alignment of boot memory area destination address.
  315. * This faciliates in mmap reading of first kernel's memory.
  316. */
  317. size = PAGE_ALIGN(size);
  318. size += fw_dump.boot_memory_size;
  319. size += sizeof(struct fadump_crash_info_header);
  320. /* This is to hold kernel metadata on platforms that support it */
  321. size += (fw_dump.ops->fadump_get_metadata_size ?
  322. fw_dump.ops->fadump_get_metadata_size() : 0);
  323. return size;
  324. }
  325. static int __init add_boot_mem_region(unsigned long rstart,
  326. unsigned long rsize)
  327. {
  328. int max_boot_mem_rgns = fw_dump.ops->fadump_max_boot_mem_rgns();
  329. int i = fw_dump.boot_mem_regs_cnt++;
  330. if (fw_dump.boot_mem_regs_cnt > max_boot_mem_rgns) {
  331. fw_dump.boot_mem_regs_cnt = max_boot_mem_rgns;
  332. return 0;
  333. }
  334. pr_debug("Added boot memory range[%d] [%#016lx-%#016lx)\n",
  335. i, rstart, (rstart + rsize));
  336. fw_dump.boot_mem_addr[i] = rstart;
  337. fw_dump.boot_mem_sz[i] = rsize;
  338. return 1;
  339. }
  340. /*
  341. * Firmware usually has a hard limit on the data it can copy per region.
  342. * Honour that by splitting a memory range into multiple regions.
  343. */
  344. static int __init add_boot_mem_regions(unsigned long mstart,
  345. unsigned long msize)
  346. {
  347. unsigned long rstart, rsize, max_size;
  348. int ret = 1;
  349. rstart = mstart;
  350. max_size = fw_dump.max_copy_size ? fw_dump.max_copy_size : msize;
  351. while (msize) {
  352. if (msize > max_size)
  353. rsize = max_size;
  354. else
  355. rsize = msize;
  356. ret = add_boot_mem_region(rstart, rsize);
  357. if (!ret)
  358. break;
  359. msize -= rsize;
  360. rstart += rsize;
  361. }
  362. return ret;
  363. }
  364. static int __init fadump_get_boot_mem_regions(void)
  365. {
  366. unsigned long size, cur_size, hole_size, last_end;
  367. unsigned long mem_size = fw_dump.boot_memory_size;
  368. phys_addr_t reg_start, reg_end;
  369. int ret = 1;
  370. u64 i;
  371. fw_dump.boot_mem_regs_cnt = 0;
  372. last_end = 0;
  373. hole_size = 0;
  374. cur_size = 0;
  375. for_each_mem_range(i, &reg_start, &reg_end) {
  376. size = reg_end - reg_start;
  377. hole_size += (reg_start - last_end);
  378. if ((cur_size + size) >= mem_size) {
  379. size = (mem_size - cur_size);
  380. ret = add_boot_mem_regions(reg_start, size);
  381. break;
  382. }
  383. mem_size -= size;
  384. cur_size += size;
  385. ret = add_boot_mem_regions(reg_start, size);
  386. if (!ret)
  387. break;
  388. last_end = reg_end;
  389. }
  390. fw_dump.boot_mem_top = PAGE_ALIGN(fw_dump.boot_memory_size + hole_size);
  391. return ret;
  392. }
  393. /*
  394. * Returns true, if the given range overlaps with reserved memory ranges
  395. * starting at idx. Also, updates idx to index of overlapping memory range
  396. * with the given memory range.
  397. * False, otherwise.
  398. */
  399. static bool __init overlaps_reserved_ranges(u64 base, u64 end, int *idx)
  400. {
  401. bool ret = false;
  402. int i;
  403. for (i = *idx; i < reserved_mrange_info.mem_range_cnt; i++) {
  404. u64 rbase = reserved_mrange_info.mem_ranges[i].base;
  405. u64 rend = rbase + reserved_mrange_info.mem_ranges[i].size;
  406. if (end <= rbase)
  407. break;
  408. if ((end > rbase) && (base < rend)) {
  409. *idx = i;
  410. ret = true;
  411. break;
  412. }
  413. }
  414. return ret;
  415. }
  416. /*
  417. * Locate a suitable memory area to reserve memory for FADump. While at it,
  418. * lookup reserved-ranges & avoid overlap with them, as they are used by F/W.
  419. */
  420. static u64 __init fadump_locate_reserve_mem(u64 base, u64 size)
  421. {
  422. struct fadump_memory_range *mrngs;
  423. phys_addr_t mstart, mend;
  424. int idx = 0;
  425. u64 i, ret = 0;
  426. mrngs = reserved_mrange_info.mem_ranges;
  427. for_each_free_mem_range(i, NUMA_NO_NODE, MEMBLOCK_NONE,
  428. &mstart, &mend, NULL) {
  429. pr_debug("%llu) mstart: %llx, mend: %llx, base: %llx\n",
  430. i, mstart, mend, base);
  431. if (mstart > base)
  432. base = PAGE_ALIGN(mstart);
  433. while ((mend > base) && ((mend - base) >= size)) {
  434. if (!overlaps_reserved_ranges(base, base+size, &idx)) {
  435. ret = base;
  436. goto out;
  437. }
  438. base = mrngs[idx].base + mrngs[idx].size;
  439. base = PAGE_ALIGN(base);
  440. }
  441. }
  442. out:
  443. return ret;
  444. }
  445. int __init fadump_reserve_mem(void)
  446. {
  447. u64 base, size, mem_boundary, bootmem_min;
  448. int ret = 1;
  449. if (!fw_dump.fadump_enabled)
  450. return 0;
  451. if (!fw_dump.fadump_supported) {
  452. pr_info("Firmware-Assisted Dump is not supported on this hardware\n");
  453. goto error_out;
  454. }
  455. /*
  456. * Initialize boot memory size
  457. * If dump is active then we have already calculated the size during
  458. * first kernel.
  459. */
  460. if (!fw_dump.dump_active) {
  461. fw_dump.boot_memory_size =
  462. PAGE_ALIGN(fadump_calculate_reserve_size());
  463. #ifdef CONFIG_CMA
  464. if (!fw_dump.nocma) {
  465. fw_dump.boot_memory_size =
  466. ALIGN(fw_dump.boot_memory_size,
  467. CMA_MIN_ALIGNMENT_BYTES);
  468. }
  469. #endif
  470. bootmem_min = fw_dump.ops->fadump_get_bootmem_min();
  471. if (fw_dump.boot_memory_size < bootmem_min) {
  472. pr_err("Can't enable fadump with boot memory size (0x%lx) less than 0x%llx\n",
  473. fw_dump.boot_memory_size, bootmem_min);
  474. goto error_out;
  475. }
  476. if (!fadump_get_boot_mem_regions()) {
  477. pr_err("Too many holes in boot memory area to enable fadump\n");
  478. goto error_out;
  479. }
  480. }
  481. if (memory_limit)
  482. mem_boundary = memory_limit;
  483. else
  484. mem_boundary = memblock_end_of_DRAM();
  485. base = fw_dump.boot_mem_top;
  486. size = get_fadump_area_size();
  487. fw_dump.reserve_dump_area_size = size;
  488. if (fw_dump.dump_active) {
  489. pr_info("Firmware-assisted dump is active.\n");
  490. #ifdef CONFIG_HUGETLB_PAGE
  491. /*
  492. * FADump capture kernel doesn't care much about hugepages.
  493. * In fact, handling hugepages in capture kernel is asking for
  494. * trouble. So, disable HugeTLB support when fadump is active.
  495. */
  496. hugetlb_disabled = true;
  497. #endif
  498. /*
  499. * If last boot has crashed then reserve all the memory
  500. * above boot memory size so that we don't touch it until
  501. * dump is written to disk by userspace tool. This memory
  502. * can be released for general use by invalidating fadump.
  503. */
  504. fadump_reserve_crash_area(base);
  505. pr_debug("fadumphdr_addr = %#016lx\n", fw_dump.fadumphdr_addr);
  506. pr_debug("Reserve dump area start address: 0x%lx\n",
  507. fw_dump.reserve_dump_area_start);
  508. } else {
  509. /*
  510. * Reserve memory at an offset closer to bottom of the RAM to
  511. * minimize the impact of memory hot-remove operation.
  512. */
  513. base = fadump_locate_reserve_mem(base, size);
  514. if (!base || (base + size > mem_boundary)) {
  515. pr_err("Failed to find memory chunk for reservation!\n");
  516. goto error_out;
  517. }
  518. fw_dump.reserve_dump_area_start = base;
  519. /*
  520. * Calculate the kernel metadata address and register it with
  521. * f/w if the platform supports.
  522. */
  523. if (fw_dump.ops->fadump_setup_metadata &&
  524. (fw_dump.ops->fadump_setup_metadata(&fw_dump) < 0))
  525. goto error_out;
  526. if (memblock_reserve(base, size)) {
  527. pr_err("Failed to reserve memory!\n");
  528. goto error_out;
  529. }
  530. pr_info("Reserved %lldMB of memory at %#016llx (System RAM: %lldMB)\n",
  531. (size >> 20), base, (memblock_phys_mem_size() >> 20));
  532. }
  533. return ret;
  534. error_out:
  535. fw_dump.fadump_enabled = 0;
  536. fw_dump.reserve_dump_area_size = 0;
  537. return 0;
  538. }
  539. /* Look for fadump= cmdline option. */
  540. static int __init early_fadump_param(char *p)
  541. {
  542. if (!p)
  543. return 1;
  544. if (strncmp(p, "on", 2) == 0)
  545. fw_dump.fadump_enabled = 1;
  546. else if (strncmp(p, "off", 3) == 0)
  547. fw_dump.fadump_enabled = 0;
  548. else if (strncmp(p, "nocma", 5) == 0) {
  549. fw_dump.fadump_enabled = 1;
  550. fw_dump.nocma = 1;
  551. }
  552. return 0;
  553. }
  554. early_param("fadump", early_fadump_param);
  555. /*
  556. * Look for fadump_reserve_mem= cmdline option
  557. * TODO: Remove references to 'fadump_reserve_mem=' parameter,
  558. * the sooner 'crashkernel=' parameter is accustomed to.
  559. */
  560. static int __init early_fadump_reserve_mem(char *p)
  561. {
  562. if (p)
  563. fw_dump.reserve_bootvar = memparse(p, &p);
  564. return 0;
  565. }
  566. early_param("fadump_reserve_mem", early_fadump_reserve_mem);
  567. void crash_fadump(struct pt_regs *regs, const char *str)
  568. {
  569. unsigned int msecs;
  570. struct fadump_crash_info_header *fdh = NULL;
  571. int old_cpu, this_cpu;
  572. /* Do not include first CPU */
  573. unsigned int ncpus = num_online_cpus() - 1;
  574. if (!should_fadump_crash())
  575. return;
  576. /*
  577. * old_cpu == -1 means this is the first CPU which has come here,
  578. * go ahead and trigger fadump.
  579. *
  580. * old_cpu != -1 means some other CPU has already on its way
  581. * to trigger fadump, just keep looping here.
  582. */
  583. this_cpu = smp_processor_id();
  584. old_cpu = cmpxchg(&crashing_cpu, -1, this_cpu);
  585. if (old_cpu != -1) {
  586. atomic_inc(&cpus_in_fadump);
  587. /*
  588. * We can't loop here indefinitely. Wait as long as fadump
  589. * is in force. If we race with fadump un-registration this
  590. * loop will break and then we go down to normal panic path
  591. * and reboot. If fadump is in force the first crashing
  592. * cpu will definitely trigger fadump.
  593. */
  594. while (fw_dump.dump_registered)
  595. cpu_relax();
  596. return;
  597. }
  598. fdh = __va(fw_dump.fadumphdr_addr);
  599. fdh->crashing_cpu = crashing_cpu;
  600. crash_save_vmcoreinfo();
  601. if (regs)
  602. fdh->regs = *regs;
  603. else
  604. ppc_save_regs(&fdh->regs);
  605. fdh->cpu_mask = *cpu_online_mask;
  606. /*
  607. * If we came in via system reset, wait a while for the secondary
  608. * CPUs to enter.
  609. */
  610. if (TRAP(&(fdh->regs)) == INTERRUPT_SYSTEM_RESET) {
  611. msecs = CRASH_TIMEOUT;
  612. while ((atomic_read(&cpus_in_fadump) < ncpus) && (--msecs > 0))
  613. mdelay(1);
  614. }
  615. fw_dump.ops->fadump_trigger(fdh, str);
  616. }
  617. u32 *__init fadump_regs_to_elf_notes(u32 *buf, struct pt_regs *regs)
  618. {
  619. struct elf_prstatus prstatus;
  620. memset(&prstatus, 0, sizeof(prstatus));
  621. /*
  622. * FIXME: How do i get PID? Do I really need it?
  623. * prstatus.pr_pid = ????
  624. */
  625. elf_core_copy_regs(&prstatus.pr_reg, regs);
  626. buf = append_elf_note(buf, CRASH_CORE_NOTE_NAME, NT_PRSTATUS,
  627. &prstatus, sizeof(prstatus));
  628. return buf;
  629. }
  630. void __init fadump_update_elfcore_header(char *bufp)
  631. {
  632. struct elf_phdr *phdr;
  633. bufp += sizeof(struct elfhdr);
  634. /* First note is a place holder for cpu notes info. */
  635. phdr = (struct elf_phdr *)bufp;
  636. if (phdr->p_type == PT_NOTE) {
  637. phdr->p_paddr = __pa(fw_dump.cpu_notes_buf_vaddr);
  638. phdr->p_offset = phdr->p_paddr;
  639. phdr->p_filesz = fw_dump.cpu_notes_buf_size;
  640. phdr->p_memsz = fw_dump.cpu_notes_buf_size;
  641. }
  642. return;
  643. }
  644. static void *__init fadump_alloc_buffer(unsigned long size)
  645. {
  646. unsigned long count, i;
  647. struct page *page;
  648. void *vaddr;
  649. vaddr = alloc_pages_exact(size, GFP_KERNEL | __GFP_ZERO);
  650. if (!vaddr)
  651. return NULL;
  652. count = PAGE_ALIGN(size) / PAGE_SIZE;
  653. page = virt_to_page(vaddr);
  654. for (i = 0; i < count; i++)
  655. mark_page_reserved(page + i);
  656. return vaddr;
  657. }
  658. static void fadump_free_buffer(unsigned long vaddr, unsigned long size)
  659. {
  660. free_reserved_area((void *)vaddr, (void *)(vaddr + size), -1, NULL);
  661. }
  662. s32 __init fadump_setup_cpu_notes_buf(u32 num_cpus)
  663. {
  664. /* Allocate buffer to hold cpu crash notes. */
  665. fw_dump.cpu_notes_buf_size = num_cpus * sizeof(note_buf_t);
  666. fw_dump.cpu_notes_buf_size = PAGE_ALIGN(fw_dump.cpu_notes_buf_size);
  667. fw_dump.cpu_notes_buf_vaddr =
  668. (unsigned long)fadump_alloc_buffer(fw_dump.cpu_notes_buf_size);
  669. if (!fw_dump.cpu_notes_buf_vaddr) {
  670. pr_err("Failed to allocate %ld bytes for CPU notes buffer\n",
  671. fw_dump.cpu_notes_buf_size);
  672. return -ENOMEM;
  673. }
  674. pr_debug("Allocated buffer for cpu notes of size %ld at 0x%lx\n",
  675. fw_dump.cpu_notes_buf_size,
  676. fw_dump.cpu_notes_buf_vaddr);
  677. return 0;
  678. }
  679. void fadump_free_cpu_notes_buf(void)
  680. {
  681. if (!fw_dump.cpu_notes_buf_vaddr)
  682. return;
  683. fadump_free_buffer(fw_dump.cpu_notes_buf_vaddr,
  684. fw_dump.cpu_notes_buf_size);
  685. fw_dump.cpu_notes_buf_vaddr = 0;
  686. fw_dump.cpu_notes_buf_size = 0;
  687. }
  688. static void fadump_free_mem_ranges(struct fadump_mrange_info *mrange_info)
  689. {
  690. if (mrange_info->is_static) {
  691. mrange_info->mem_range_cnt = 0;
  692. return;
  693. }
  694. kfree(mrange_info->mem_ranges);
  695. memset((void *)((u64)mrange_info + RNG_NAME_SZ), 0,
  696. (sizeof(struct fadump_mrange_info) - RNG_NAME_SZ));
  697. }
  698. /*
  699. * Allocate or reallocate mem_ranges array in incremental units
  700. * of PAGE_SIZE.
  701. */
  702. static int fadump_alloc_mem_ranges(struct fadump_mrange_info *mrange_info)
  703. {
  704. struct fadump_memory_range *new_array;
  705. u64 new_size;
  706. new_size = mrange_info->mem_ranges_sz + PAGE_SIZE;
  707. pr_debug("Allocating %llu bytes of memory for %s memory ranges\n",
  708. new_size, mrange_info->name);
  709. new_array = krealloc(mrange_info->mem_ranges, new_size, GFP_KERNEL);
  710. if (new_array == NULL) {
  711. pr_err("Insufficient memory for setting up %s memory ranges\n",
  712. mrange_info->name);
  713. fadump_free_mem_ranges(mrange_info);
  714. return -ENOMEM;
  715. }
  716. mrange_info->mem_ranges = new_array;
  717. mrange_info->mem_ranges_sz = new_size;
  718. mrange_info->max_mem_ranges = (new_size /
  719. sizeof(struct fadump_memory_range));
  720. return 0;
  721. }
  722. static inline int fadump_add_mem_range(struct fadump_mrange_info *mrange_info,
  723. u64 base, u64 end)
  724. {
  725. struct fadump_memory_range *mem_ranges = mrange_info->mem_ranges;
  726. bool is_adjacent = false;
  727. u64 start, size;
  728. if (base == end)
  729. return 0;
  730. /*
  731. * Fold adjacent memory ranges to bring down the memory ranges/
  732. * PT_LOAD segments count.
  733. */
  734. if (mrange_info->mem_range_cnt) {
  735. start = mem_ranges[mrange_info->mem_range_cnt - 1].base;
  736. size = mem_ranges[mrange_info->mem_range_cnt - 1].size;
  737. /*
  738. * Boot memory area needs separate PT_LOAD segment(s) as it
  739. * is moved to a different location at the time of crash.
  740. * So, fold only if the region is not boot memory area.
  741. */
  742. if ((start + size) == base && start >= fw_dump.boot_mem_top)
  743. is_adjacent = true;
  744. }
  745. if (!is_adjacent) {
  746. /* resize the array on reaching the limit */
  747. if (mrange_info->mem_range_cnt == mrange_info->max_mem_ranges) {
  748. int ret;
  749. if (mrange_info->is_static) {
  750. pr_err("Reached array size limit for %s memory ranges\n",
  751. mrange_info->name);
  752. return -ENOSPC;
  753. }
  754. ret = fadump_alloc_mem_ranges(mrange_info);
  755. if (ret)
  756. return ret;
  757. /* Update to the new resized array */
  758. mem_ranges = mrange_info->mem_ranges;
  759. }
  760. start = base;
  761. mem_ranges[mrange_info->mem_range_cnt].base = start;
  762. mrange_info->mem_range_cnt++;
  763. }
  764. mem_ranges[mrange_info->mem_range_cnt - 1].size = (end - start);
  765. pr_debug("%s_memory_range[%d] [%#016llx-%#016llx], %#llx bytes\n",
  766. mrange_info->name, (mrange_info->mem_range_cnt - 1),
  767. start, end - 1, (end - start));
  768. return 0;
  769. }
  770. static int fadump_init_elfcore_header(char *bufp)
  771. {
  772. struct elfhdr *elf;
  773. elf = (struct elfhdr *) bufp;
  774. bufp += sizeof(struct elfhdr);
  775. memcpy(elf->e_ident, ELFMAG, SELFMAG);
  776. elf->e_ident[EI_CLASS] = ELF_CLASS;
  777. elf->e_ident[EI_DATA] = ELF_DATA;
  778. elf->e_ident[EI_VERSION] = EV_CURRENT;
  779. elf->e_ident[EI_OSABI] = ELF_OSABI;
  780. memset(elf->e_ident+EI_PAD, 0, EI_NIDENT-EI_PAD);
  781. elf->e_type = ET_CORE;
  782. elf->e_machine = ELF_ARCH;
  783. elf->e_version = EV_CURRENT;
  784. elf->e_entry = 0;
  785. elf->e_phoff = sizeof(struct elfhdr);
  786. elf->e_shoff = 0;
  787. if (IS_ENABLED(CONFIG_PPC64_ELF_ABI_V2))
  788. elf->e_flags = 2;
  789. else if (IS_ENABLED(CONFIG_PPC64_ELF_ABI_V1))
  790. elf->e_flags = 1;
  791. else
  792. elf->e_flags = 0;
  793. elf->e_ehsize = sizeof(struct elfhdr);
  794. elf->e_phentsize = sizeof(struct elf_phdr);
  795. elf->e_phnum = 0;
  796. elf->e_shentsize = 0;
  797. elf->e_shnum = 0;
  798. elf->e_shstrndx = 0;
  799. return 0;
  800. }
  801. /*
  802. * If the given physical address falls within the boot memory region then
  803. * return the relocated address that points to the dump region reserved
  804. * for saving initial boot memory contents.
  805. */
  806. static inline unsigned long fadump_relocate(unsigned long paddr)
  807. {
  808. unsigned long raddr, rstart, rend, rlast, hole_size;
  809. int i;
  810. hole_size = 0;
  811. rlast = 0;
  812. raddr = paddr;
  813. for (i = 0; i < fw_dump.boot_mem_regs_cnt; i++) {
  814. rstart = fw_dump.boot_mem_addr[i];
  815. rend = rstart + fw_dump.boot_mem_sz[i];
  816. hole_size += (rstart - rlast);
  817. if (paddr >= rstart && paddr < rend) {
  818. raddr += fw_dump.boot_mem_dest_addr - hole_size;
  819. break;
  820. }
  821. rlast = rend;
  822. }
  823. pr_debug("vmcoreinfo: paddr = 0x%lx, raddr = 0x%lx\n", paddr, raddr);
  824. return raddr;
  825. }
  826. static void __init populate_elf_pt_load(struct elf_phdr *phdr, u64 start,
  827. u64 size, unsigned long long offset)
  828. {
  829. phdr->p_align = 0;
  830. phdr->p_memsz = size;
  831. phdr->p_filesz = size;
  832. phdr->p_paddr = start;
  833. phdr->p_offset = offset;
  834. phdr->p_type = PT_LOAD;
  835. phdr->p_flags = PF_R|PF_W|PF_X;
  836. phdr->p_vaddr = (unsigned long)__va(start);
  837. }
  838. static void __init fadump_populate_elfcorehdr(struct fadump_crash_info_header *fdh)
  839. {
  840. char *bufp;
  841. struct elfhdr *elf;
  842. struct elf_phdr *phdr;
  843. u64 boot_mem_dest_offset;
  844. unsigned long long i, ra_start, ra_end, ra_size, mstart, mend;
  845. bufp = (char *) fw_dump.elfcorehdr_addr;
  846. fadump_init_elfcore_header(bufp);
  847. elf = (struct elfhdr *)bufp;
  848. bufp += sizeof(struct elfhdr);
  849. /*
  850. * Set up ELF PT_NOTE, a placeholder for CPU notes information.
  851. * The notes info will be populated later by platform-specific code.
  852. * Hence, this PT_NOTE will always be the first ELF note.
  853. *
  854. * NOTE: Any new ELF note addition should be placed after this note.
  855. */
  856. phdr = (struct elf_phdr *)bufp;
  857. bufp += sizeof(struct elf_phdr);
  858. phdr->p_type = PT_NOTE;
  859. phdr->p_flags = 0;
  860. phdr->p_vaddr = 0;
  861. phdr->p_align = 0;
  862. phdr->p_offset = 0;
  863. phdr->p_paddr = 0;
  864. phdr->p_filesz = 0;
  865. phdr->p_memsz = 0;
  866. /* Increment number of program headers. */
  867. (elf->e_phnum)++;
  868. /* setup ELF PT_NOTE for vmcoreinfo */
  869. phdr = (struct elf_phdr *)bufp;
  870. bufp += sizeof(struct elf_phdr);
  871. phdr->p_type = PT_NOTE;
  872. phdr->p_flags = 0;
  873. phdr->p_vaddr = 0;
  874. phdr->p_align = 0;
  875. phdr->p_paddr = phdr->p_offset = fdh->vmcoreinfo_raddr;
  876. phdr->p_memsz = phdr->p_filesz = fdh->vmcoreinfo_size;
  877. /* Increment number of program headers. */
  878. (elf->e_phnum)++;
  879. /*
  880. * Setup PT_LOAD sections. first include boot memory regions
  881. * and then add rest of the memory regions.
  882. */
  883. boot_mem_dest_offset = fw_dump.boot_mem_dest_addr;
  884. for (i = 0; i < fw_dump.boot_mem_regs_cnt; i++) {
  885. phdr = (struct elf_phdr *)bufp;
  886. bufp += sizeof(struct elf_phdr);
  887. populate_elf_pt_load(phdr, fw_dump.boot_mem_addr[i],
  888. fw_dump.boot_mem_sz[i],
  889. boot_mem_dest_offset);
  890. /* Increment number of program headers. */
  891. (elf->e_phnum)++;
  892. boot_mem_dest_offset += fw_dump.boot_mem_sz[i];
  893. }
  894. /* Memory reserved for fadump in first kernel */
  895. ra_start = fw_dump.reserve_dump_area_start;
  896. ra_size = get_fadump_area_size();
  897. ra_end = ra_start + ra_size;
  898. phdr = (struct elf_phdr *)bufp;
  899. for_each_mem_range(i, &mstart, &mend) {
  900. /* Boot memory regions already added, skip them now */
  901. if (mstart < fw_dump.boot_mem_top) {
  902. if (mend > fw_dump.boot_mem_top)
  903. mstart = fw_dump.boot_mem_top;
  904. else
  905. continue;
  906. }
  907. /* Handle memblock regions overlaps with fadump reserved area */
  908. if ((ra_start < mend) && (ra_end > mstart)) {
  909. if ((mstart < ra_start) && (mend > ra_end)) {
  910. populate_elf_pt_load(phdr, mstart, ra_start - mstart, mstart);
  911. /* Increment number of program headers. */
  912. (elf->e_phnum)++;
  913. bufp += sizeof(struct elf_phdr);
  914. phdr = (struct elf_phdr *)bufp;
  915. populate_elf_pt_load(phdr, ra_end, mend - ra_end, ra_end);
  916. } else if (mstart < ra_start) {
  917. populate_elf_pt_load(phdr, mstart, ra_start - mstart, mstart);
  918. } else if (ra_end < mend) {
  919. populate_elf_pt_load(phdr, ra_end, mend - ra_end, ra_end);
  920. }
  921. } else {
  922. /* No overlap with fadump reserved memory region */
  923. populate_elf_pt_load(phdr, mstart, mend - mstart, mstart);
  924. }
  925. /* Increment number of program headers. */
  926. (elf->e_phnum)++;
  927. bufp += sizeof(struct elf_phdr);
  928. phdr = (struct elf_phdr *) bufp;
  929. }
  930. }
  931. static unsigned long init_fadump_header(unsigned long addr)
  932. {
  933. struct fadump_crash_info_header *fdh;
  934. if (!addr)
  935. return 0;
  936. fdh = __va(addr);
  937. addr += sizeof(struct fadump_crash_info_header);
  938. memset(fdh, 0, sizeof(struct fadump_crash_info_header));
  939. fdh->magic_number = FADUMP_CRASH_INFO_MAGIC;
  940. fdh->version = FADUMP_HEADER_VERSION;
  941. /* We will set the crashing cpu id in crash_fadump() during crash. */
  942. fdh->crashing_cpu = FADUMP_CPU_UNKNOWN;
  943. /*
  944. * The physical address and size of vmcoreinfo are required in the
  945. * second kernel to prepare elfcorehdr.
  946. */
  947. fdh->vmcoreinfo_raddr = fadump_relocate(paddr_vmcoreinfo_note());
  948. fdh->vmcoreinfo_size = VMCOREINFO_NOTE_SIZE;
  949. fdh->pt_regs_sz = sizeof(struct pt_regs);
  950. /*
  951. * When LPAR is terminated by PYHP, ensure all possible CPUs'
  952. * register data is processed while exporting the vmcore.
  953. */
  954. fdh->cpu_mask = *cpu_possible_mask;
  955. fdh->cpu_mask_sz = sizeof(struct cpumask);
  956. return addr;
  957. }
  958. static int register_fadump(void)
  959. {
  960. unsigned long addr;
  961. /*
  962. * If no memory is reserved then we can not register for firmware-
  963. * assisted dump.
  964. */
  965. if (!fw_dump.reserve_dump_area_size)
  966. return -ENODEV;
  967. addr = fw_dump.fadumphdr_addr;
  968. /* Initialize fadump crash info header. */
  969. addr = init_fadump_header(addr);
  970. /* register the future kernel dump with firmware. */
  971. pr_debug("Registering for firmware-assisted kernel dump...\n");
  972. return fw_dump.ops->fadump_register(&fw_dump);
  973. }
  974. void fadump_cleanup(void)
  975. {
  976. if (!fw_dump.fadump_supported)
  977. return;
  978. /* Invalidate the registration only if dump is active. */
  979. if (fw_dump.dump_active) {
  980. pr_debug("Invalidating firmware-assisted dump registration\n");
  981. fw_dump.ops->fadump_invalidate(&fw_dump);
  982. } else if (fw_dump.dump_registered) {
  983. /* Un-register Firmware-assisted dump if it was registered. */
  984. fw_dump.ops->fadump_unregister(&fw_dump);
  985. }
  986. if (fw_dump.ops->fadump_cleanup)
  987. fw_dump.ops->fadump_cleanup(&fw_dump);
  988. }
  989. static void fadump_free_reserved_memory(unsigned long start_pfn,
  990. unsigned long end_pfn)
  991. {
  992. unsigned long pfn;
  993. unsigned long time_limit = jiffies + HZ;
  994. pr_info("freeing reserved memory (0x%llx - 0x%llx)\n",
  995. PFN_PHYS(start_pfn), PFN_PHYS(end_pfn));
  996. for (pfn = start_pfn; pfn < end_pfn; pfn++) {
  997. free_reserved_page(pfn_to_page(pfn));
  998. if (time_after(jiffies, time_limit)) {
  999. cond_resched();
  1000. time_limit = jiffies + HZ;
  1001. }
  1002. }
  1003. }
  1004. /*
  1005. * Skip memory holes and free memory that was actually reserved.
  1006. */
  1007. static void fadump_release_reserved_area(u64 start, u64 end)
  1008. {
  1009. unsigned long reg_spfn, reg_epfn;
  1010. u64 tstart, tend, spfn, epfn;
  1011. int i;
  1012. spfn = PHYS_PFN(start);
  1013. epfn = PHYS_PFN(end);
  1014. for_each_mem_pfn_range(i, MAX_NUMNODES, &reg_spfn, &reg_epfn, NULL) {
  1015. tstart = max_t(u64, spfn, reg_spfn);
  1016. tend = min_t(u64, epfn, reg_epfn);
  1017. if (tstart < tend) {
  1018. fadump_free_reserved_memory(tstart, tend);
  1019. if (tend == epfn)
  1020. break;
  1021. spfn = tend;
  1022. }
  1023. }
  1024. }
  1025. /*
  1026. * Sort the mem ranges in-place and merge adjacent ranges
  1027. * to minimize the memory ranges count.
  1028. */
  1029. static void sort_and_merge_mem_ranges(struct fadump_mrange_info *mrange_info)
  1030. {
  1031. struct fadump_memory_range *mem_ranges;
  1032. u64 base, size;
  1033. int i, j, idx;
  1034. if (!reserved_mrange_info.mem_range_cnt)
  1035. return;
  1036. /* Sort the memory ranges */
  1037. mem_ranges = mrange_info->mem_ranges;
  1038. for (i = 0; i < mrange_info->mem_range_cnt; i++) {
  1039. idx = i;
  1040. for (j = (i + 1); j < mrange_info->mem_range_cnt; j++) {
  1041. if (mem_ranges[idx].base > mem_ranges[j].base)
  1042. idx = j;
  1043. }
  1044. if (idx != i)
  1045. swap(mem_ranges[idx], mem_ranges[i]);
  1046. }
  1047. /* Merge adjacent reserved ranges */
  1048. idx = 0;
  1049. for (i = 1; i < mrange_info->mem_range_cnt; i++) {
  1050. base = mem_ranges[i-1].base;
  1051. size = mem_ranges[i-1].size;
  1052. if (mem_ranges[i].base == (base + size))
  1053. mem_ranges[idx].size += mem_ranges[i].size;
  1054. else {
  1055. idx++;
  1056. if (i == idx)
  1057. continue;
  1058. mem_ranges[idx] = mem_ranges[i];
  1059. }
  1060. }
  1061. mrange_info->mem_range_cnt = idx + 1;
  1062. }
  1063. /*
  1064. * Scan reserved-ranges to consider them while reserving/releasing
  1065. * memory for FADump.
  1066. */
  1067. static void __init early_init_dt_scan_reserved_ranges(unsigned long node)
  1068. {
  1069. const __be32 *prop;
  1070. int len, ret = -1;
  1071. unsigned long i;
  1072. /* reserved-ranges already scanned */
  1073. if (reserved_mrange_info.mem_range_cnt != 0)
  1074. return;
  1075. prop = of_get_flat_dt_prop(node, "reserved-ranges", &len);
  1076. if (!prop)
  1077. return;
  1078. /*
  1079. * Each reserved range is an (address,size) pair, 2 cells each,
  1080. * totalling 4 cells per range.
  1081. */
  1082. for (i = 0; i < len / (sizeof(*prop) * 4); i++) {
  1083. u64 base, size;
  1084. base = of_read_number(prop + (i * 4) + 0, 2);
  1085. size = of_read_number(prop + (i * 4) + 2, 2);
  1086. if (size) {
  1087. ret = fadump_add_mem_range(&reserved_mrange_info,
  1088. base, base + size);
  1089. if (ret < 0) {
  1090. pr_warn("some reserved ranges are ignored!\n");
  1091. break;
  1092. }
  1093. }
  1094. }
  1095. /* Compact reserved ranges */
  1096. sort_and_merge_mem_ranges(&reserved_mrange_info);
  1097. }
  1098. /*
  1099. * Release the memory that was reserved during early boot to preserve the
  1100. * crash'ed kernel's memory contents except reserved dump area (permanent
  1101. * reservation) and reserved ranges used by F/W. The released memory will
  1102. * be available for general use.
  1103. */
  1104. static void fadump_release_memory(u64 begin, u64 end)
  1105. {
  1106. u64 ra_start, ra_end, tstart;
  1107. int i, ret;
  1108. ra_start = fw_dump.reserve_dump_area_start;
  1109. ra_end = ra_start + fw_dump.reserve_dump_area_size;
  1110. /*
  1111. * If reserved ranges array limit is hit, overwrite the last reserved
  1112. * memory range with reserved dump area to ensure it is excluded from
  1113. * the memory being released (reused for next FADump registration).
  1114. */
  1115. if (reserved_mrange_info.mem_range_cnt ==
  1116. reserved_mrange_info.max_mem_ranges)
  1117. reserved_mrange_info.mem_range_cnt--;
  1118. ret = fadump_add_mem_range(&reserved_mrange_info, ra_start, ra_end);
  1119. if (ret != 0)
  1120. return;
  1121. /* Get the reserved ranges list in order first. */
  1122. sort_and_merge_mem_ranges(&reserved_mrange_info);
  1123. /* Exclude reserved ranges and release remaining memory */
  1124. tstart = begin;
  1125. for (i = 0; i < reserved_mrange_info.mem_range_cnt; i++) {
  1126. ra_start = reserved_mrange_info.mem_ranges[i].base;
  1127. ra_end = ra_start + reserved_mrange_info.mem_ranges[i].size;
  1128. if (tstart >= ra_end)
  1129. continue;
  1130. if (tstart < ra_start)
  1131. fadump_release_reserved_area(tstart, ra_start);
  1132. tstart = ra_end;
  1133. }
  1134. if (tstart < end)
  1135. fadump_release_reserved_area(tstart, end);
  1136. }
  1137. static void fadump_free_elfcorehdr_buf(void)
  1138. {
  1139. if (fw_dump.elfcorehdr_addr == 0 || fw_dump.elfcorehdr_size == 0)
  1140. return;
  1141. /*
  1142. * Before freeing the memory of `elfcorehdr`, reset the global
  1143. * `elfcorehdr_addr` to prevent modules like `vmcore` from accessing
  1144. * invalid memory.
  1145. */
  1146. elfcorehdr_addr = ELFCORE_ADDR_ERR;
  1147. fadump_free_buffer(fw_dump.elfcorehdr_addr, fw_dump.elfcorehdr_size);
  1148. fw_dump.elfcorehdr_addr = 0;
  1149. fw_dump.elfcorehdr_size = 0;
  1150. }
  1151. static void fadump_invalidate_release_mem(void)
  1152. {
  1153. mutex_lock(&fadump_mutex);
  1154. if (!fw_dump.dump_active) {
  1155. mutex_unlock(&fadump_mutex);
  1156. return;
  1157. }
  1158. fadump_cleanup();
  1159. mutex_unlock(&fadump_mutex);
  1160. fadump_free_elfcorehdr_buf();
  1161. fadump_release_memory(fw_dump.boot_mem_top, memblock_end_of_DRAM());
  1162. fadump_free_cpu_notes_buf();
  1163. /*
  1164. * Setup kernel metadata and initialize the kernel dump
  1165. * memory structure for FADump re-registration.
  1166. */
  1167. if (fw_dump.ops->fadump_setup_metadata &&
  1168. (fw_dump.ops->fadump_setup_metadata(&fw_dump) < 0))
  1169. pr_warn("Failed to setup kernel metadata!\n");
  1170. fw_dump.ops->fadump_init_mem_struct(&fw_dump);
  1171. }
  1172. static ssize_t release_mem_store(struct kobject *kobj,
  1173. struct kobj_attribute *attr,
  1174. const char *buf, size_t count)
  1175. {
  1176. int input = -1;
  1177. if (!fw_dump.dump_active)
  1178. return -EPERM;
  1179. if (kstrtoint(buf, 0, &input))
  1180. return -EINVAL;
  1181. if (input == 1) {
  1182. /*
  1183. * Take away the '/proc/vmcore'. We are releasing the dump
  1184. * memory, hence it will not be valid anymore.
  1185. */
  1186. #ifdef CONFIG_PROC_VMCORE
  1187. vmcore_cleanup();
  1188. #endif
  1189. fadump_invalidate_release_mem();
  1190. } else
  1191. return -EINVAL;
  1192. return count;
  1193. }
  1194. /* Release the reserved memory and disable the FADump */
  1195. static void __init unregister_fadump(void)
  1196. {
  1197. fadump_cleanup();
  1198. fadump_release_memory(fw_dump.reserve_dump_area_start,
  1199. fw_dump.reserve_dump_area_size);
  1200. fw_dump.fadump_enabled = 0;
  1201. kobject_put(fadump_kobj);
  1202. }
  1203. static ssize_t enabled_show(struct kobject *kobj,
  1204. struct kobj_attribute *attr,
  1205. char *buf)
  1206. {
  1207. return sprintf(buf, "%d\n", fw_dump.fadump_enabled);
  1208. }
  1209. /*
  1210. * /sys/kernel/fadump/hotplug_ready sysfs node returns 1, which inidcates
  1211. * to usersapce that fadump re-registration is not required on memory
  1212. * hotplug events.
  1213. */
  1214. static ssize_t hotplug_ready_show(struct kobject *kobj,
  1215. struct kobj_attribute *attr,
  1216. char *buf)
  1217. {
  1218. return sprintf(buf, "%d\n", 1);
  1219. }
  1220. static ssize_t mem_reserved_show(struct kobject *kobj,
  1221. struct kobj_attribute *attr,
  1222. char *buf)
  1223. {
  1224. return sprintf(buf, "%ld\n", fw_dump.reserve_dump_area_size);
  1225. }
  1226. static ssize_t registered_show(struct kobject *kobj,
  1227. struct kobj_attribute *attr,
  1228. char *buf)
  1229. {
  1230. return sprintf(buf, "%d\n", fw_dump.dump_registered);
  1231. }
  1232. static ssize_t bootargs_append_show(struct kobject *kobj,
  1233. struct kobj_attribute *attr,
  1234. char *buf)
  1235. {
  1236. return sprintf(buf, "%s\n", (char *)__va(fw_dump.param_area));
  1237. }
  1238. static ssize_t bootargs_append_store(struct kobject *kobj,
  1239. struct kobj_attribute *attr,
  1240. const char *buf, size_t count)
  1241. {
  1242. char *params;
  1243. if (!fw_dump.fadump_enabled || fw_dump.dump_active)
  1244. return -EPERM;
  1245. if (count >= COMMAND_LINE_SIZE)
  1246. return -EINVAL;
  1247. /*
  1248. * Fail here instead of handling this scenario with
  1249. * some silly workaround in capture kernel.
  1250. */
  1251. if (saved_command_line_len + count >= COMMAND_LINE_SIZE) {
  1252. pr_err("Appending parameters exceeds cmdline size!\n");
  1253. return -ENOSPC;
  1254. }
  1255. params = __va(fw_dump.param_area);
  1256. strscpy_pad(params, buf, COMMAND_LINE_SIZE);
  1257. /* Remove newline character at the end. */
  1258. if (params[count-1] == '\n')
  1259. params[count-1] = '\0';
  1260. return count;
  1261. }
  1262. static ssize_t registered_store(struct kobject *kobj,
  1263. struct kobj_attribute *attr,
  1264. const char *buf, size_t count)
  1265. {
  1266. int ret = 0;
  1267. int input = -1;
  1268. if (!fw_dump.fadump_enabled || fw_dump.dump_active)
  1269. return -EPERM;
  1270. if (kstrtoint(buf, 0, &input))
  1271. return -EINVAL;
  1272. mutex_lock(&fadump_mutex);
  1273. switch (input) {
  1274. case 0:
  1275. if (fw_dump.dump_registered == 0) {
  1276. goto unlock_out;
  1277. }
  1278. /* Un-register Firmware-assisted dump */
  1279. pr_debug("Un-register firmware-assisted dump\n");
  1280. fw_dump.ops->fadump_unregister(&fw_dump);
  1281. break;
  1282. case 1:
  1283. if (fw_dump.dump_registered == 1) {
  1284. /* Un-register Firmware-assisted dump */
  1285. fw_dump.ops->fadump_unregister(&fw_dump);
  1286. }
  1287. /* Register Firmware-assisted dump */
  1288. ret = register_fadump();
  1289. break;
  1290. default:
  1291. ret = -EINVAL;
  1292. break;
  1293. }
  1294. unlock_out:
  1295. mutex_unlock(&fadump_mutex);
  1296. return ret < 0 ? ret : count;
  1297. }
  1298. static int fadump_region_show(struct seq_file *m, void *private)
  1299. {
  1300. if (!fw_dump.fadump_enabled)
  1301. return 0;
  1302. mutex_lock(&fadump_mutex);
  1303. fw_dump.ops->fadump_region_show(&fw_dump, m);
  1304. mutex_unlock(&fadump_mutex);
  1305. return 0;
  1306. }
  1307. static struct kobj_attribute release_attr = __ATTR_WO(release_mem);
  1308. static struct kobj_attribute enable_attr = __ATTR_RO(enabled);
  1309. static struct kobj_attribute register_attr = __ATTR_RW(registered);
  1310. static struct kobj_attribute mem_reserved_attr = __ATTR_RO(mem_reserved);
  1311. static struct kobj_attribute hotplug_ready_attr = __ATTR_RO(hotplug_ready);
  1312. static struct kobj_attribute bootargs_append_attr = __ATTR_RW(bootargs_append);
  1313. static struct attribute *fadump_attrs[] = {
  1314. &enable_attr.attr,
  1315. &register_attr.attr,
  1316. &mem_reserved_attr.attr,
  1317. &hotplug_ready_attr.attr,
  1318. NULL,
  1319. };
  1320. ATTRIBUTE_GROUPS(fadump);
  1321. DEFINE_SHOW_ATTRIBUTE(fadump_region);
  1322. static void __init fadump_init_files(void)
  1323. {
  1324. int rc = 0;
  1325. fadump_kobj = kobject_create_and_add("fadump", kernel_kobj);
  1326. if (!fadump_kobj) {
  1327. pr_err("failed to create fadump kobject\n");
  1328. return;
  1329. }
  1330. if (fw_dump.param_area) {
  1331. rc = sysfs_create_file(fadump_kobj, &bootargs_append_attr.attr);
  1332. if (rc)
  1333. pr_err("unable to create bootargs_append sysfs file (%d)\n", rc);
  1334. }
  1335. debugfs_create_file("fadump_region", 0444, arch_debugfs_dir, NULL,
  1336. &fadump_region_fops);
  1337. if (fw_dump.dump_active) {
  1338. rc = sysfs_create_file(fadump_kobj, &release_attr.attr);
  1339. if (rc)
  1340. pr_err("unable to create release_mem sysfs file (%d)\n",
  1341. rc);
  1342. }
  1343. rc = sysfs_create_groups(fadump_kobj, fadump_groups);
  1344. if (rc) {
  1345. pr_err("sysfs group creation failed (%d), unregistering FADump",
  1346. rc);
  1347. unregister_fadump();
  1348. return;
  1349. }
  1350. /*
  1351. * The FADump sysfs are moved from kernel_kobj to fadump_kobj need to
  1352. * create symlink at old location to maintain backward compatibility.
  1353. *
  1354. * - fadump_enabled -> fadump/enabled
  1355. * - fadump_registered -> fadump/registered
  1356. * - fadump_release_mem -> fadump/release_mem
  1357. */
  1358. rc = compat_only_sysfs_link_entry_to_kobj(kernel_kobj, fadump_kobj,
  1359. "enabled", "fadump_enabled");
  1360. if (rc) {
  1361. pr_err("unable to create fadump_enabled symlink (%d)", rc);
  1362. return;
  1363. }
  1364. rc = compat_only_sysfs_link_entry_to_kobj(kernel_kobj, fadump_kobj,
  1365. "registered",
  1366. "fadump_registered");
  1367. if (rc) {
  1368. pr_err("unable to create fadump_registered symlink (%d)", rc);
  1369. sysfs_remove_link(kernel_kobj, "fadump_enabled");
  1370. return;
  1371. }
  1372. if (fw_dump.dump_active) {
  1373. rc = compat_only_sysfs_link_entry_to_kobj(kernel_kobj,
  1374. fadump_kobj,
  1375. "release_mem",
  1376. "fadump_release_mem");
  1377. if (rc)
  1378. pr_err("unable to create fadump_release_mem symlink (%d)",
  1379. rc);
  1380. }
  1381. return;
  1382. }
  1383. static int __init fadump_setup_elfcorehdr_buf(void)
  1384. {
  1385. int elf_phdr_cnt;
  1386. unsigned long elfcorehdr_size;
  1387. /*
  1388. * Program header for CPU notes comes first, followed by one for
  1389. * vmcoreinfo, and the remaining program headers correspond to
  1390. * memory regions.
  1391. */
  1392. elf_phdr_cnt = 2 + fw_dump.boot_mem_regs_cnt + memblock_num_regions(memory);
  1393. elfcorehdr_size = sizeof(struct elfhdr) + (elf_phdr_cnt * sizeof(struct elf_phdr));
  1394. elfcorehdr_size = PAGE_ALIGN(elfcorehdr_size);
  1395. fw_dump.elfcorehdr_addr = (u64)fadump_alloc_buffer(elfcorehdr_size);
  1396. if (!fw_dump.elfcorehdr_addr) {
  1397. pr_err("Failed to allocate %lu bytes for elfcorehdr\n",
  1398. elfcorehdr_size);
  1399. return -ENOMEM;
  1400. }
  1401. fw_dump.elfcorehdr_size = elfcorehdr_size;
  1402. return 0;
  1403. }
  1404. /*
  1405. * Check if the fadump header of crashed kernel is compatible with fadump kernel.
  1406. *
  1407. * It checks the magic number, endianness, and size of non-primitive type
  1408. * members of fadump header to ensure safe dump collection.
  1409. */
  1410. static bool __init is_fadump_header_compatible(struct fadump_crash_info_header *fdh)
  1411. {
  1412. if (fdh->magic_number == FADUMP_CRASH_INFO_MAGIC_OLD) {
  1413. pr_err("Old magic number, can't process the dump.\n");
  1414. return false;
  1415. }
  1416. if (fdh->magic_number != FADUMP_CRASH_INFO_MAGIC) {
  1417. if (fdh->magic_number == swab64(FADUMP_CRASH_INFO_MAGIC))
  1418. pr_err("Endianness mismatch between the crashed and fadump kernels.\n");
  1419. else
  1420. pr_err("Fadump header is corrupted.\n");
  1421. return false;
  1422. }
  1423. /*
  1424. * Dump collection is not safe if the size of non-primitive type members
  1425. * of the fadump header do not match between crashed and fadump kernel.
  1426. */
  1427. if (fdh->pt_regs_sz != sizeof(struct pt_regs) ||
  1428. fdh->cpu_mask_sz != sizeof(struct cpumask)) {
  1429. pr_err("Fadump header size mismatch.\n");
  1430. return false;
  1431. }
  1432. return true;
  1433. }
  1434. static void __init fadump_process(void)
  1435. {
  1436. struct fadump_crash_info_header *fdh;
  1437. fdh = (struct fadump_crash_info_header *) __va(fw_dump.fadumphdr_addr);
  1438. if (!fdh) {
  1439. pr_err("Crash info header is empty.\n");
  1440. goto err_out;
  1441. }
  1442. /* Avoid processing the dump if fadump header isn't compatible */
  1443. if (!is_fadump_header_compatible(fdh))
  1444. goto err_out;
  1445. /* Allocate buffer for elfcorehdr */
  1446. if (fadump_setup_elfcorehdr_buf())
  1447. goto err_out;
  1448. fadump_populate_elfcorehdr(fdh);
  1449. /* Let platform update the CPU notes in elfcorehdr */
  1450. if (fw_dump.ops->fadump_process(&fw_dump) < 0)
  1451. goto err_out;
  1452. /*
  1453. * elfcorehdr is now ready to be exported.
  1454. *
  1455. * set elfcorehdr_addr so that vmcore module will export the
  1456. * elfcorehdr through '/proc/vmcore'.
  1457. */
  1458. elfcorehdr_addr = virt_to_phys((void *)fw_dump.elfcorehdr_addr);
  1459. return;
  1460. err_out:
  1461. fadump_invalidate_release_mem();
  1462. }
  1463. /*
  1464. * Reserve memory to store additional parameters to be passed
  1465. * for fadump/capture kernel.
  1466. */
  1467. void __init fadump_setup_param_area(void)
  1468. {
  1469. phys_addr_t range_start, range_end;
  1470. if (!fw_dump.param_area_supported || fw_dump.dump_active)
  1471. return;
  1472. /* This memory can't be used by PFW or bootloader as it is shared across kernels */
  1473. if (early_radix_enabled()) {
  1474. /*
  1475. * Anywhere in the upper half should be good enough as all memory
  1476. * is accessible in real mode.
  1477. */
  1478. range_start = memblock_end_of_DRAM() / 2;
  1479. range_end = memblock_end_of_DRAM();
  1480. } else {
  1481. /*
  1482. * Passing additional parameters is supported for hash MMU only
  1483. * if the first memory block size is 768MB or higher.
  1484. */
  1485. if (ppc64_rma_size < 0x30000000)
  1486. return;
  1487. /*
  1488. * 640 MB to 768 MB is not used by PFW/bootloader. So, try reserving
  1489. * memory for passing additional parameters in this range to avoid
  1490. * being stomped on by PFW/bootloader.
  1491. */
  1492. range_start = 0x2A000000;
  1493. range_end = range_start + 0x4000000;
  1494. }
  1495. fw_dump.param_area = memblock_phys_alloc_range(COMMAND_LINE_SIZE,
  1496. COMMAND_LINE_SIZE,
  1497. range_start,
  1498. range_end);
  1499. if (!fw_dump.param_area) {
  1500. pr_warn("WARNING: Could not setup area to pass additional parameters!\n");
  1501. return;
  1502. }
  1503. memset((void *)fw_dump.param_area, 0, COMMAND_LINE_SIZE);
  1504. }
  1505. /*
  1506. * Prepare for firmware-assisted dump.
  1507. */
  1508. int __init setup_fadump(void)
  1509. {
  1510. if (!fw_dump.fadump_supported)
  1511. return 0;
  1512. fadump_init_files();
  1513. fadump_show_config();
  1514. if (!fw_dump.fadump_enabled)
  1515. return 1;
  1516. /*
  1517. * If dump data is available then see if it is valid and prepare for
  1518. * saving it to the disk.
  1519. */
  1520. if (fw_dump.dump_active) {
  1521. fadump_process();
  1522. }
  1523. /* Initialize the kernel dump memory structure and register with f/w */
  1524. else if (fw_dump.reserve_dump_area_size) {
  1525. fw_dump.ops->fadump_init_mem_struct(&fw_dump);
  1526. register_fadump();
  1527. }
  1528. /*
  1529. * In case of panic, fadump is triggered via ppc_panic_event()
  1530. * panic notifier. Setting crash_kexec_post_notifiers to 'true'
  1531. * lets panic() function take crash friendly path before panic
  1532. * notifiers are invoked.
  1533. */
  1534. crash_kexec_post_notifiers = true;
  1535. return 1;
  1536. }
  1537. /*
  1538. * Use subsys_initcall_sync() here because there is dependency with
  1539. * crash_save_vmcoreinfo_init(), which must run first to ensure vmcoreinfo initialization
  1540. * is done before registering with f/w.
  1541. */
  1542. subsys_initcall_sync(setup_fadump);
  1543. #else /* !CONFIG_PRESERVE_FA_DUMP */
  1544. /* Scan the Firmware Assisted dump configuration details. */
  1545. int __init early_init_dt_scan_fw_dump(unsigned long node, const char *uname,
  1546. int depth, void *data)
  1547. {
  1548. if ((depth != 1) || (strcmp(uname, "ibm,opal") != 0))
  1549. return 0;
  1550. opal_fadump_dt_scan(&fw_dump, node);
  1551. return 1;
  1552. }
  1553. /*
  1554. * When dump is active but PRESERVE_FA_DUMP is enabled on the kernel,
  1555. * preserve crash data. The subsequent memory preserving kernel boot
  1556. * is likely to process this crash data.
  1557. */
  1558. int __init fadump_reserve_mem(void)
  1559. {
  1560. if (fw_dump.dump_active) {
  1561. /*
  1562. * If last boot has crashed then reserve all the memory
  1563. * above boot memory to preserve crash data.
  1564. */
  1565. pr_info("Preserving crash data for processing in next boot.\n");
  1566. fadump_reserve_crash_area(fw_dump.boot_mem_top);
  1567. } else
  1568. pr_debug("FADump-aware kernel..\n");
  1569. return 1;
  1570. }
  1571. #endif /* CONFIG_PRESERVE_FA_DUMP */
  1572. /* Preserve everything above the base address */
  1573. static void __init fadump_reserve_crash_area(u64 base)
  1574. {
  1575. u64 i, mstart, mend, msize;
  1576. for_each_mem_range(i, &mstart, &mend) {
  1577. msize = mend - mstart;
  1578. if ((mstart + msize) < base)
  1579. continue;
  1580. if (mstart < base) {
  1581. msize -= (base - mstart);
  1582. mstart = base;
  1583. }
  1584. pr_info("Reserving %lluMB of memory at %#016llx for preserving crash data",
  1585. (msize >> 20), mstart);
  1586. memblock_reserve(mstart, msize);
  1587. }
  1588. }