fadump.c 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527
  1. /*
  2. * Firmware Assisted dump: A robust mechanism to get reliable kernel crash
  3. * dump with assistance from firmware. This approach does not use kexec,
  4. * instead firmware assists in booting the kdump kernel while preserving
  5. * memory contents. The most of the code implementation has been adapted
  6. * from phyp assisted dump implementation written by Linas Vepstas and
  7. * Manish Ahuja
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  22. *
  23. * Copyright 2011 IBM Corporation
  24. * Author: Mahesh Salgaonkar <mahesh@linux.vnet.ibm.com>
  25. */
  26. #undef DEBUG
  27. #define pr_fmt(fmt) "fadump: " fmt
  28. #include <linux/string.h>
  29. #include <linux/memblock.h>
  30. #include <linux/delay.h>
  31. #include <linux/seq_file.h>
  32. #include <linux/crash_dump.h>
  33. #include <linux/kobject.h>
  34. #include <linux/sysfs.h>
  35. #include <asm/debugfs.h>
  36. #include <asm/page.h>
  37. #include <asm/prom.h>
  38. #include <asm/rtas.h>
  39. #include <asm/fadump.h>
  40. #include <asm/setup.h>
  41. static struct fw_dump fw_dump;
  42. static struct fadump_mem_struct fdm;
  43. static const struct fadump_mem_struct *fdm_active;
  44. static DEFINE_MUTEX(fadump_mutex);
  45. struct fad_crash_memory_ranges crash_memory_ranges[INIT_CRASHMEM_RANGES];
  46. int crash_mem_ranges;
  47. /* Scan the Firmware Assisted dump configuration details. */
  48. int __init early_init_dt_scan_fw_dump(unsigned long node,
  49. const char *uname, int depth, void *data)
  50. {
  51. const __be32 *sections;
  52. int i, num_sections;
  53. int size;
  54. const __be32 *token;
  55. if (depth != 1 || strcmp(uname, "rtas") != 0)
  56. return 0;
  57. /*
  58. * Check if Firmware Assisted dump is supported. if yes, check
  59. * if dump has been initiated on last reboot.
  60. */
  61. token = of_get_flat_dt_prop(node, "ibm,configure-kernel-dump", NULL);
  62. if (!token)
  63. return 1;
  64. fw_dump.fadump_supported = 1;
  65. fw_dump.ibm_configure_kernel_dump = be32_to_cpu(*token);
  66. /*
  67. * The 'ibm,kernel-dump' rtas node is present only if there is
  68. * dump data waiting for us.
  69. */
  70. fdm_active = of_get_flat_dt_prop(node, "ibm,kernel-dump", NULL);
  71. if (fdm_active)
  72. fw_dump.dump_active = 1;
  73. /* Get the sizes required to store dump data for the firmware provided
  74. * dump sections.
  75. * For each dump section type supported, a 32bit cell which defines
  76. * the ID of a supported section followed by two 32 bit cells which
  77. * gives teh size of the section in bytes.
  78. */
  79. sections = of_get_flat_dt_prop(node, "ibm,configure-kernel-dump-sizes",
  80. &size);
  81. if (!sections)
  82. return 1;
  83. num_sections = size / (3 * sizeof(u32));
  84. for (i = 0; i < num_sections; i++, sections += 3) {
  85. u32 type = (u32)of_read_number(sections, 1);
  86. switch (type) {
  87. case FADUMP_CPU_STATE_DATA:
  88. fw_dump.cpu_state_data_size =
  89. of_read_ulong(&sections[1], 2);
  90. break;
  91. case FADUMP_HPTE_REGION:
  92. fw_dump.hpte_region_size =
  93. of_read_ulong(&sections[1], 2);
  94. break;
  95. }
  96. }
  97. return 1;
  98. }
  99. /*
  100. * If fadump is registered, check if the memory provided
  101. * falls within boot memory area.
  102. */
  103. int is_fadump_boot_memory_area(u64 addr, ulong size)
  104. {
  105. if (!fw_dump.dump_registered)
  106. return 0;
  107. return (addr + size) > RMA_START && addr <= fw_dump.boot_memory_size;
  108. }
  109. int should_fadump_crash(void)
  110. {
  111. if (!fw_dump.dump_registered || !fw_dump.fadumphdr_addr)
  112. return 0;
  113. return 1;
  114. }
  115. int is_fadump_active(void)
  116. {
  117. return fw_dump.dump_active;
  118. }
  119. /*
  120. * Returns 1, if there are no holes in boot memory area,
  121. * 0 otherwise.
  122. */
  123. static int is_boot_memory_area_contiguous(void)
  124. {
  125. struct memblock_region *reg;
  126. unsigned long tstart, tend;
  127. unsigned long start_pfn = PHYS_PFN(RMA_START);
  128. unsigned long end_pfn = PHYS_PFN(RMA_START + fw_dump.boot_memory_size);
  129. unsigned int ret = 0;
  130. for_each_memblock(memory, reg) {
  131. tstart = max(start_pfn, memblock_region_memory_base_pfn(reg));
  132. tend = min(end_pfn, memblock_region_memory_end_pfn(reg));
  133. if (tstart < tend) {
  134. /* Memory hole from start_pfn to tstart */
  135. if (tstart > start_pfn)
  136. break;
  137. if (tend == end_pfn) {
  138. ret = 1;
  139. break;
  140. }
  141. start_pfn = tend + 1;
  142. }
  143. }
  144. return ret;
  145. }
  146. /* Print firmware assisted dump configurations for debugging purpose. */
  147. static void fadump_show_config(void)
  148. {
  149. pr_debug("Support for firmware-assisted dump (fadump): %s\n",
  150. (fw_dump.fadump_supported ? "present" : "no support"));
  151. if (!fw_dump.fadump_supported)
  152. return;
  153. pr_debug("Fadump enabled : %s\n",
  154. (fw_dump.fadump_enabled ? "yes" : "no"));
  155. pr_debug("Dump Active : %s\n",
  156. (fw_dump.dump_active ? "yes" : "no"));
  157. pr_debug("Dump section sizes:\n");
  158. pr_debug(" CPU state data size: %lx\n", fw_dump.cpu_state_data_size);
  159. pr_debug(" HPTE region size : %lx\n", fw_dump.hpte_region_size);
  160. pr_debug("Boot memory size : %lx\n", fw_dump.boot_memory_size);
  161. }
  162. static unsigned long init_fadump_mem_struct(struct fadump_mem_struct *fdm,
  163. unsigned long addr)
  164. {
  165. if (!fdm)
  166. return 0;
  167. memset(fdm, 0, sizeof(struct fadump_mem_struct));
  168. addr = addr & PAGE_MASK;
  169. fdm->header.dump_format_version = cpu_to_be32(0x00000001);
  170. fdm->header.dump_num_sections = cpu_to_be16(3);
  171. fdm->header.dump_status_flag = 0;
  172. fdm->header.offset_first_dump_section =
  173. cpu_to_be32((u32)offsetof(struct fadump_mem_struct, cpu_state_data));
  174. /*
  175. * Fields for disk dump option.
  176. * We are not using disk dump option, hence set these fields to 0.
  177. */
  178. fdm->header.dd_block_size = 0;
  179. fdm->header.dd_block_offset = 0;
  180. fdm->header.dd_num_blocks = 0;
  181. fdm->header.dd_offset_disk_path = 0;
  182. /* set 0 to disable an automatic dump-reboot. */
  183. fdm->header.max_time_auto = 0;
  184. /* Kernel dump sections */
  185. /* cpu state data section. */
  186. fdm->cpu_state_data.request_flag = cpu_to_be32(FADUMP_REQUEST_FLAG);
  187. fdm->cpu_state_data.source_data_type = cpu_to_be16(FADUMP_CPU_STATE_DATA);
  188. fdm->cpu_state_data.source_address = 0;
  189. fdm->cpu_state_data.source_len = cpu_to_be64(fw_dump.cpu_state_data_size);
  190. fdm->cpu_state_data.destination_address = cpu_to_be64(addr);
  191. addr += fw_dump.cpu_state_data_size;
  192. /* hpte region section */
  193. fdm->hpte_region.request_flag = cpu_to_be32(FADUMP_REQUEST_FLAG);
  194. fdm->hpte_region.source_data_type = cpu_to_be16(FADUMP_HPTE_REGION);
  195. fdm->hpte_region.source_address = 0;
  196. fdm->hpte_region.source_len = cpu_to_be64(fw_dump.hpte_region_size);
  197. fdm->hpte_region.destination_address = cpu_to_be64(addr);
  198. addr += fw_dump.hpte_region_size;
  199. /* RMA region section */
  200. fdm->rmr_region.request_flag = cpu_to_be32(FADUMP_REQUEST_FLAG);
  201. fdm->rmr_region.source_data_type = cpu_to_be16(FADUMP_REAL_MODE_REGION);
  202. fdm->rmr_region.source_address = cpu_to_be64(RMA_START);
  203. fdm->rmr_region.source_len = cpu_to_be64(fw_dump.boot_memory_size);
  204. fdm->rmr_region.destination_address = cpu_to_be64(addr);
  205. addr += fw_dump.boot_memory_size;
  206. return addr;
  207. }
  208. /**
  209. * fadump_calculate_reserve_size(): reserve variable boot area 5% of System RAM
  210. *
  211. * Function to find the largest memory size we need to reserve during early
  212. * boot process. This will be the size of the memory that is required for a
  213. * kernel to boot successfully.
  214. *
  215. * This function has been taken from phyp-assisted dump feature implementation.
  216. *
  217. * returns larger of 256MB or 5% rounded down to multiples of 256MB.
  218. *
  219. * TODO: Come up with better approach to find out more accurate memory size
  220. * that is required for a kernel to boot successfully.
  221. *
  222. */
  223. static inline unsigned long fadump_calculate_reserve_size(void)
  224. {
  225. int ret;
  226. unsigned long long base, size;
  227. if (fw_dump.reserve_bootvar)
  228. pr_warn("'fadump_reserve_mem=' parameter is deprecated in favor of 'crashkernel=' parameter.\n");
  229. /*
  230. * Check if the size is specified through crashkernel= cmdline
  231. * option. If yes, then use that but ignore base as fadump reserves
  232. * memory at a predefined offset.
  233. */
  234. ret = parse_crashkernel(boot_command_line, memblock_phys_mem_size(),
  235. &size, &base);
  236. if (ret == 0 && size > 0) {
  237. unsigned long max_size;
  238. if (fw_dump.reserve_bootvar)
  239. pr_info("Using 'crashkernel=' parameter for memory reservation.\n");
  240. fw_dump.reserve_bootvar = (unsigned long)size;
  241. /*
  242. * Adjust if the boot memory size specified is above
  243. * the upper limit.
  244. */
  245. max_size = memblock_phys_mem_size() / MAX_BOOT_MEM_RATIO;
  246. if (fw_dump.reserve_bootvar > max_size) {
  247. fw_dump.reserve_bootvar = max_size;
  248. pr_info("Adjusted boot memory size to %luMB\n",
  249. (fw_dump.reserve_bootvar >> 20));
  250. }
  251. return fw_dump.reserve_bootvar;
  252. } else if (fw_dump.reserve_bootvar) {
  253. /*
  254. * 'fadump_reserve_mem=' is being used to reserve memory
  255. * for firmware-assisted dump.
  256. */
  257. return fw_dump.reserve_bootvar;
  258. }
  259. /* divide by 20 to get 5% of value */
  260. size = memblock_phys_mem_size() / 20;
  261. /* round it down in multiples of 256 */
  262. size = size & ~0x0FFFFFFFUL;
  263. /* Truncate to memory_limit. We don't want to over reserve the memory.*/
  264. if (memory_limit && size > memory_limit)
  265. size = memory_limit;
  266. return (size > MIN_BOOT_MEM ? size : MIN_BOOT_MEM);
  267. }
  268. /*
  269. * Calculate the total memory size required to be reserved for
  270. * firmware-assisted dump registration.
  271. */
  272. static unsigned long get_fadump_area_size(void)
  273. {
  274. unsigned long size = 0;
  275. size += fw_dump.cpu_state_data_size;
  276. size += fw_dump.hpte_region_size;
  277. size += fw_dump.boot_memory_size;
  278. size += sizeof(struct fadump_crash_info_header);
  279. size += sizeof(struct elfhdr); /* ELF core header.*/
  280. size += sizeof(struct elf_phdr); /* place holder for cpu notes */
  281. /* Program headers for crash memory regions. */
  282. size += sizeof(struct elf_phdr) * (memblock_num_regions(memory) + 2);
  283. size = PAGE_ALIGN(size);
  284. return size;
  285. }
  286. static void __init fadump_reserve_crash_area(unsigned long base,
  287. unsigned long size)
  288. {
  289. struct memblock_region *reg;
  290. unsigned long mstart, mend, msize;
  291. for_each_memblock(memory, reg) {
  292. mstart = max_t(unsigned long, base, reg->base);
  293. mend = reg->base + reg->size;
  294. mend = min(base + size, mend);
  295. if (mstart < mend) {
  296. msize = mend - mstart;
  297. memblock_reserve(mstart, msize);
  298. pr_info("Reserved %ldMB of memory at %#016lx for saving crash dump\n",
  299. (msize >> 20), mstart);
  300. }
  301. }
  302. }
  303. int __init fadump_reserve_mem(void)
  304. {
  305. unsigned long base, size, memory_boundary;
  306. if (!fw_dump.fadump_enabled)
  307. return 0;
  308. if (!fw_dump.fadump_supported) {
  309. printk(KERN_INFO "Firmware-assisted dump is not supported on"
  310. " this hardware\n");
  311. fw_dump.fadump_enabled = 0;
  312. return 0;
  313. }
  314. /*
  315. * Initialize boot memory size
  316. * If dump is active then we have already calculated the size during
  317. * first kernel.
  318. */
  319. if (fdm_active)
  320. fw_dump.boot_memory_size = be64_to_cpu(fdm_active->rmr_region.source_len);
  321. else
  322. fw_dump.boot_memory_size = fadump_calculate_reserve_size();
  323. /*
  324. * Calculate the memory boundary.
  325. * If memory_limit is less than actual memory boundary then reserve
  326. * the memory for fadump beyond the memory_limit and adjust the
  327. * memory_limit accordingly, so that the running kernel can run with
  328. * specified memory_limit.
  329. */
  330. if (memory_limit && memory_limit < memblock_end_of_DRAM()) {
  331. size = get_fadump_area_size();
  332. if ((memory_limit + size) < memblock_end_of_DRAM())
  333. memory_limit += size;
  334. else
  335. memory_limit = memblock_end_of_DRAM();
  336. printk(KERN_INFO "Adjusted memory_limit for firmware-assisted"
  337. " dump, now %#016llx\n", memory_limit);
  338. }
  339. if (memory_limit)
  340. memory_boundary = memory_limit;
  341. else
  342. memory_boundary = memblock_end_of_DRAM();
  343. if (fw_dump.dump_active) {
  344. pr_info("Firmware-assisted dump is active.\n");
  345. #ifdef CONFIG_HUGETLB_PAGE
  346. /*
  347. * FADump capture kernel doesn't care much about hugepages.
  348. * In fact, handling hugepages in capture kernel is asking for
  349. * trouble. So, disable HugeTLB support when fadump is active.
  350. */
  351. hugetlb_disabled = true;
  352. #endif
  353. /*
  354. * If last boot has crashed then reserve all the memory
  355. * above boot_memory_size so that we don't touch it until
  356. * dump is written to disk by userspace tool. This memory
  357. * will be released for general use once the dump is saved.
  358. */
  359. base = fw_dump.boot_memory_size;
  360. size = memory_boundary - base;
  361. fadump_reserve_crash_area(base, size);
  362. fw_dump.fadumphdr_addr =
  363. be64_to_cpu(fdm_active->rmr_region.destination_address) +
  364. be64_to_cpu(fdm_active->rmr_region.source_len);
  365. pr_debug("fadumphdr_addr = %p\n",
  366. (void *) fw_dump.fadumphdr_addr);
  367. } else {
  368. size = get_fadump_area_size();
  369. /*
  370. * Reserve memory at an offset closer to bottom of the RAM to
  371. * minimize the impact of memory hot-remove operation. We can't
  372. * use memblock_find_in_range() here since it doesn't allocate
  373. * from bottom to top.
  374. */
  375. for (base = fw_dump.boot_memory_size;
  376. base <= (memory_boundary - size);
  377. base += size) {
  378. if (memblock_is_region_memory(base, size) &&
  379. !memblock_is_region_reserved(base, size))
  380. break;
  381. }
  382. if ((base > (memory_boundary - size)) ||
  383. memblock_reserve(base, size)) {
  384. pr_err("Failed to reserve memory\n");
  385. return 0;
  386. }
  387. pr_info("Reserved %ldMB of memory at %ldMB for firmware-"
  388. "assisted dump (System RAM: %ldMB)\n",
  389. (unsigned long)(size >> 20),
  390. (unsigned long)(base >> 20),
  391. (unsigned long)(memblock_phys_mem_size() >> 20));
  392. }
  393. fw_dump.reserve_dump_area_start = base;
  394. fw_dump.reserve_dump_area_size = size;
  395. return 1;
  396. }
  397. unsigned long __init arch_reserved_kernel_pages(void)
  398. {
  399. return memblock_reserved_size() / PAGE_SIZE;
  400. }
  401. /* Look for fadump= cmdline option. */
  402. static int __init early_fadump_param(char *p)
  403. {
  404. if (!p)
  405. return 1;
  406. if (strncmp(p, "on", 2) == 0)
  407. fw_dump.fadump_enabled = 1;
  408. else if (strncmp(p, "off", 3) == 0)
  409. fw_dump.fadump_enabled = 0;
  410. return 0;
  411. }
  412. early_param("fadump", early_fadump_param);
  413. /*
  414. * Look for fadump_reserve_mem= cmdline option
  415. * TODO: Remove references to 'fadump_reserve_mem=' parameter,
  416. * the sooner 'crashkernel=' parameter is accustomed to.
  417. */
  418. static int __init early_fadump_reserve_mem(char *p)
  419. {
  420. if (p)
  421. fw_dump.reserve_bootvar = memparse(p, &p);
  422. return 0;
  423. }
  424. early_param("fadump_reserve_mem", early_fadump_reserve_mem);
  425. static int register_fw_dump(struct fadump_mem_struct *fdm)
  426. {
  427. int rc, err;
  428. unsigned int wait_time;
  429. pr_debug("Registering for firmware-assisted kernel dump...\n");
  430. /* TODO: Add upper time limit for the delay */
  431. do {
  432. rc = rtas_call(fw_dump.ibm_configure_kernel_dump, 3, 1, NULL,
  433. FADUMP_REGISTER, fdm,
  434. sizeof(struct fadump_mem_struct));
  435. wait_time = rtas_busy_delay_time(rc);
  436. if (wait_time)
  437. mdelay(wait_time);
  438. } while (wait_time);
  439. err = -EIO;
  440. switch (rc) {
  441. default:
  442. pr_err("Failed to register. Unknown Error(%d).\n", rc);
  443. break;
  444. case -1:
  445. printk(KERN_ERR "Failed to register firmware-assisted kernel"
  446. " dump. Hardware Error(%d).\n", rc);
  447. break;
  448. case -3:
  449. if (!is_boot_memory_area_contiguous())
  450. pr_err("Can't have holes in boot memory area while "
  451. "registering fadump\n");
  452. printk(KERN_ERR "Failed to register firmware-assisted kernel"
  453. " dump. Parameter Error(%d).\n", rc);
  454. err = -EINVAL;
  455. break;
  456. case -9:
  457. printk(KERN_ERR "firmware-assisted kernel dump is already "
  458. " registered.");
  459. fw_dump.dump_registered = 1;
  460. err = -EEXIST;
  461. break;
  462. case 0:
  463. printk(KERN_INFO "firmware-assisted kernel dump registration"
  464. " is successful\n");
  465. fw_dump.dump_registered = 1;
  466. err = 0;
  467. break;
  468. }
  469. return err;
  470. }
  471. void crash_fadump(struct pt_regs *regs, const char *str)
  472. {
  473. struct fadump_crash_info_header *fdh = NULL;
  474. int old_cpu, this_cpu;
  475. if (!should_fadump_crash())
  476. return;
  477. /*
  478. * old_cpu == -1 means this is the first CPU which has come here,
  479. * go ahead and trigger fadump.
  480. *
  481. * old_cpu != -1 means some other CPU has already on it's way
  482. * to trigger fadump, just keep looping here.
  483. */
  484. this_cpu = smp_processor_id();
  485. old_cpu = cmpxchg(&crashing_cpu, -1, this_cpu);
  486. if (old_cpu != -1) {
  487. /*
  488. * We can't loop here indefinitely. Wait as long as fadump
  489. * is in force. If we race with fadump un-registration this
  490. * loop will break and then we go down to normal panic path
  491. * and reboot. If fadump is in force the first crashing
  492. * cpu will definitely trigger fadump.
  493. */
  494. while (fw_dump.dump_registered)
  495. cpu_relax();
  496. return;
  497. }
  498. fdh = __va(fw_dump.fadumphdr_addr);
  499. fdh->crashing_cpu = crashing_cpu;
  500. crash_save_vmcoreinfo();
  501. if (regs)
  502. fdh->regs = *regs;
  503. else
  504. ppc_save_regs(&fdh->regs);
  505. fdh->online_mask = *cpu_online_mask;
  506. /* Call ibm,os-term rtas call to trigger firmware assisted dump */
  507. rtas_os_term((char *)str);
  508. }
  509. #define GPR_MASK 0xffffff0000000000
  510. static inline int fadump_gpr_index(u64 id)
  511. {
  512. int i = -1;
  513. char str[3];
  514. if ((id & GPR_MASK) == REG_ID("GPR")) {
  515. /* get the digits at the end */
  516. id &= ~GPR_MASK;
  517. id >>= 24;
  518. str[2] = '\0';
  519. str[1] = id & 0xff;
  520. str[0] = (id >> 8) & 0xff;
  521. sscanf(str, "%d", &i);
  522. if (i > 31)
  523. i = -1;
  524. }
  525. return i;
  526. }
  527. static inline void fadump_set_regval(struct pt_regs *regs, u64 reg_id,
  528. u64 reg_val)
  529. {
  530. int i;
  531. i = fadump_gpr_index(reg_id);
  532. if (i >= 0)
  533. regs->gpr[i] = (unsigned long)reg_val;
  534. else if (reg_id == REG_ID("NIA"))
  535. regs->nip = (unsigned long)reg_val;
  536. else if (reg_id == REG_ID("MSR"))
  537. regs->msr = (unsigned long)reg_val;
  538. else if (reg_id == REG_ID("CTR"))
  539. regs->ctr = (unsigned long)reg_val;
  540. else if (reg_id == REG_ID("LR"))
  541. regs->link = (unsigned long)reg_val;
  542. else if (reg_id == REG_ID("XER"))
  543. regs->xer = (unsigned long)reg_val;
  544. else if (reg_id == REG_ID("CR"))
  545. regs->ccr = (unsigned long)reg_val;
  546. else if (reg_id == REG_ID("DAR"))
  547. regs->dar = (unsigned long)reg_val;
  548. else if (reg_id == REG_ID("DSISR"))
  549. regs->dsisr = (unsigned long)reg_val;
  550. }
  551. static struct fadump_reg_entry*
  552. fadump_read_registers(struct fadump_reg_entry *reg_entry, struct pt_regs *regs)
  553. {
  554. memset(regs, 0, sizeof(struct pt_regs));
  555. while (be64_to_cpu(reg_entry->reg_id) != REG_ID("CPUEND")) {
  556. fadump_set_regval(regs, be64_to_cpu(reg_entry->reg_id),
  557. be64_to_cpu(reg_entry->reg_value));
  558. reg_entry++;
  559. }
  560. reg_entry++;
  561. return reg_entry;
  562. }
  563. static u32 *fadump_regs_to_elf_notes(u32 *buf, struct pt_regs *regs)
  564. {
  565. struct elf_prstatus prstatus;
  566. memset(&prstatus, 0, sizeof(prstatus));
  567. /*
  568. * FIXME: How do i get PID? Do I really need it?
  569. * prstatus.pr_pid = ????
  570. */
  571. elf_core_copy_kernel_regs(&prstatus.pr_reg, regs);
  572. buf = append_elf_note(buf, CRASH_CORE_NOTE_NAME, NT_PRSTATUS,
  573. &prstatus, sizeof(prstatus));
  574. return buf;
  575. }
  576. static void fadump_update_elfcore_header(char *bufp)
  577. {
  578. struct elfhdr *elf;
  579. struct elf_phdr *phdr;
  580. elf = (struct elfhdr *)bufp;
  581. bufp += sizeof(struct elfhdr);
  582. /* First note is a place holder for cpu notes info. */
  583. phdr = (struct elf_phdr *)bufp;
  584. if (phdr->p_type == PT_NOTE) {
  585. phdr->p_paddr = fw_dump.cpu_notes_buf;
  586. phdr->p_offset = phdr->p_paddr;
  587. phdr->p_filesz = fw_dump.cpu_notes_buf_size;
  588. phdr->p_memsz = fw_dump.cpu_notes_buf_size;
  589. }
  590. return;
  591. }
  592. static void *fadump_cpu_notes_buf_alloc(unsigned long size)
  593. {
  594. void *vaddr;
  595. struct page *page;
  596. unsigned long order, count, i;
  597. order = get_order(size);
  598. vaddr = (void *)__get_free_pages(GFP_KERNEL|__GFP_ZERO, order);
  599. if (!vaddr)
  600. return NULL;
  601. count = 1 << order;
  602. page = virt_to_page(vaddr);
  603. for (i = 0; i < count; i++)
  604. SetPageReserved(page + i);
  605. return vaddr;
  606. }
  607. static void fadump_cpu_notes_buf_free(unsigned long vaddr, unsigned long size)
  608. {
  609. struct page *page;
  610. unsigned long order, count, i;
  611. order = get_order(size);
  612. count = 1 << order;
  613. page = virt_to_page(vaddr);
  614. for (i = 0; i < count; i++)
  615. ClearPageReserved(page + i);
  616. __free_pages(page, order);
  617. }
  618. /*
  619. * Read CPU state dump data and convert it into ELF notes.
  620. * The CPU dump starts with magic number "REGSAVE". NumCpusOffset should be
  621. * used to access the data to allow for additional fields to be added without
  622. * affecting compatibility. Each list of registers for a CPU starts with
  623. * "CPUSTRT" and ends with "CPUEND". Each register entry is of 16 bytes,
  624. * 8 Byte ASCII identifier and 8 Byte register value. The register entry
  625. * with identifier "CPUSTRT" and "CPUEND" contains 4 byte cpu id as part
  626. * of register value. For more details refer to PAPR document.
  627. *
  628. * Only for the crashing cpu we ignore the CPU dump data and get exact
  629. * state from fadump crash info structure populated by first kernel at the
  630. * time of crash.
  631. */
  632. static int __init fadump_build_cpu_notes(const struct fadump_mem_struct *fdm)
  633. {
  634. struct fadump_reg_save_area_header *reg_header;
  635. struct fadump_reg_entry *reg_entry;
  636. struct fadump_crash_info_header *fdh = NULL;
  637. void *vaddr;
  638. unsigned long addr;
  639. u32 num_cpus, *note_buf;
  640. struct pt_regs regs;
  641. int i, rc = 0, cpu = 0;
  642. if (!fdm->cpu_state_data.bytes_dumped)
  643. return -EINVAL;
  644. addr = be64_to_cpu(fdm->cpu_state_data.destination_address);
  645. vaddr = __va(addr);
  646. reg_header = vaddr;
  647. if (be64_to_cpu(reg_header->magic_number) != REGSAVE_AREA_MAGIC) {
  648. printk(KERN_ERR "Unable to read register save area.\n");
  649. return -ENOENT;
  650. }
  651. pr_debug("--------CPU State Data------------\n");
  652. pr_debug("Magic Number: %llx\n", be64_to_cpu(reg_header->magic_number));
  653. pr_debug("NumCpuOffset: %x\n", be32_to_cpu(reg_header->num_cpu_offset));
  654. vaddr += be32_to_cpu(reg_header->num_cpu_offset);
  655. num_cpus = be32_to_cpu(*((__be32 *)(vaddr)));
  656. pr_debug("NumCpus : %u\n", num_cpus);
  657. vaddr += sizeof(u32);
  658. reg_entry = (struct fadump_reg_entry *)vaddr;
  659. /* Allocate buffer to hold cpu crash notes. */
  660. fw_dump.cpu_notes_buf_size = num_cpus * sizeof(note_buf_t);
  661. fw_dump.cpu_notes_buf_size = PAGE_ALIGN(fw_dump.cpu_notes_buf_size);
  662. note_buf = fadump_cpu_notes_buf_alloc(fw_dump.cpu_notes_buf_size);
  663. if (!note_buf) {
  664. printk(KERN_ERR "Failed to allocate 0x%lx bytes for "
  665. "cpu notes buffer\n", fw_dump.cpu_notes_buf_size);
  666. return -ENOMEM;
  667. }
  668. fw_dump.cpu_notes_buf = __pa(note_buf);
  669. pr_debug("Allocated buffer for cpu notes of size %ld at %p\n",
  670. (num_cpus * sizeof(note_buf_t)), note_buf);
  671. if (fw_dump.fadumphdr_addr)
  672. fdh = __va(fw_dump.fadumphdr_addr);
  673. for (i = 0; i < num_cpus; i++) {
  674. if (be64_to_cpu(reg_entry->reg_id) != REG_ID("CPUSTRT")) {
  675. printk(KERN_ERR "Unable to read CPU state data\n");
  676. rc = -ENOENT;
  677. goto error_out;
  678. }
  679. /* Lower 4 bytes of reg_value contains logical cpu id */
  680. cpu = be64_to_cpu(reg_entry->reg_value) & FADUMP_CPU_ID_MASK;
  681. if (fdh && !cpumask_test_cpu(cpu, &fdh->online_mask)) {
  682. SKIP_TO_NEXT_CPU(reg_entry);
  683. continue;
  684. }
  685. pr_debug("Reading register data for cpu %d...\n", cpu);
  686. if (fdh && fdh->crashing_cpu == cpu) {
  687. regs = fdh->regs;
  688. note_buf = fadump_regs_to_elf_notes(note_buf, &regs);
  689. SKIP_TO_NEXT_CPU(reg_entry);
  690. } else {
  691. reg_entry++;
  692. reg_entry = fadump_read_registers(reg_entry, &regs);
  693. note_buf = fadump_regs_to_elf_notes(note_buf, &regs);
  694. }
  695. }
  696. final_note(note_buf);
  697. if (fdh) {
  698. pr_debug("Updating elfcore header (%llx) with cpu notes\n",
  699. fdh->elfcorehdr_addr);
  700. fadump_update_elfcore_header((char *)__va(fdh->elfcorehdr_addr));
  701. }
  702. return 0;
  703. error_out:
  704. fadump_cpu_notes_buf_free((unsigned long)__va(fw_dump.cpu_notes_buf),
  705. fw_dump.cpu_notes_buf_size);
  706. fw_dump.cpu_notes_buf = 0;
  707. fw_dump.cpu_notes_buf_size = 0;
  708. return rc;
  709. }
  710. /*
  711. * Validate and process the dump data stored by firmware before exporting
  712. * it through '/proc/vmcore'.
  713. */
  714. static int __init process_fadump(const struct fadump_mem_struct *fdm_active)
  715. {
  716. struct fadump_crash_info_header *fdh;
  717. int rc = 0;
  718. if (!fdm_active || !fw_dump.fadumphdr_addr)
  719. return -EINVAL;
  720. /* Check if the dump data is valid. */
  721. if ((be16_to_cpu(fdm_active->header.dump_status_flag) == FADUMP_ERROR_FLAG) ||
  722. (fdm_active->cpu_state_data.error_flags != 0) ||
  723. (fdm_active->rmr_region.error_flags != 0)) {
  724. printk(KERN_ERR "Dump taken by platform is not valid\n");
  725. return -EINVAL;
  726. }
  727. if ((fdm_active->rmr_region.bytes_dumped !=
  728. fdm_active->rmr_region.source_len) ||
  729. !fdm_active->cpu_state_data.bytes_dumped) {
  730. printk(KERN_ERR "Dump taken by platform is incomplete\n");
  731. return -EINVAL;
  732. }
  733. /* Validate the fadump crash info header */
  734. fdh = __va(fw_dump.fadumphdr_addr);
  735. if (fdh->magic_number != FADUMP_CRASH_INFO_MAGIC) {
  736. printk(KERN_ERR "Crash info header is not valid.\n");
  737. return -EINVAL;
  738. }
  739. rc = fadump_build_cpu_notes(fdm_active);
  740. if (rc)
  741. return rc;
  742. /*
  743. * We are done validating dump info and elfcore header is now ready
  744. * to be exported. set elfcorehdr_addr so that vmcore module will
  745. * export the elfcore header through '/proc/vmcore'.
  746. */
  747. elfcorehdr_addr = fdh->elfcorehdr_addr;
  748. return 0;
  749. }
  750. static inline void fadump_add_crash_memory(unsigned long long base,
  751. unsigned long long end)
  752. {
  753. if (base == end)
  754. return;
  755. pr_debug("crash_memory_range[%d] [%#016llx-%#016llx], %#llx bytes\n",
  756. crash_mem_ranges, base, end - 1, (end - base));
  757. crash_memory_ranges[crash_mem_ranges].base = base;
  758. crash_memory_ranges[crash_mem_ranges].size = end - base;
  759. crash_mem_ranges++;
  760. }
  761. static void fadump_exclude_reserved_area(unsigned long long start,
  762. unsigned long long end)
  763. {
  764. unsigned long long ra_start, ra_end;
  765. ra_start = fw_dump.reserve_dump_area_start;
  766. ra_end = ra_start + fw_dump.reserve_dump_area_size;
  767. if ((ra_start < end) && (ra_end > start)) {
  768. if ((start < ra_start) && (end > ra_end)) {
  769. fadump_add_crash_memory(start, ra_start);
  770. fadump_add_crash_memory(ra_end, end);
  771. } else if (start < ra_start) {
  772. fadump_add_crash_memory(start, ra_start);
  773. } else if (ra_end < end) {
  774. fadump_add_crash_memory(ra_end, end);
  775. }
  776. } else
  777. fadump_add_crash_memory(start, end);
  778. }
  779. static int fadump_init_elfcore_header(char *bufp)
  780. {
  781. struct elfhdr *elf;
  782. elf = (struct elfhdr *) bufp;
  783. bufp += sizeof(struct elfhdr);
  784. memcpy(elf->e_ident, ELFMAG, SELFMAG);
  785. elf->e_ident[EI_CLASS] = ELF_CLASS;
  786. elf->e_ident[EI_DATA] = ELF_DATA;
  787. elf->e_ident[EI_VERSION] = EV_CURRENT;
  788. elf->e_ident[EI_OSABI] = ELF_OSABI;
  789. memset(elf->e_ident+EI_PAD, 0, EI_NIDENT-EI_PAD);
  790. elf->e_type = ET_CORE;
  791. elf->e_machine = ELF_ARCH;
  792. elf->e_version = EV_CURRENT;
  793. elf->e_entry = 0;
  794. elf->e_phoff = sizeof(struct elfhdr);
  795. elf->e_shoff = 0;
  796. #if defined(_CALL_ELF)
  797. elf->e_flags = _CALL_ELF;
  798. #else
  799. elf->e_flags = 0;
  800. #endif
  801. elf->e_ehsize = sizeof(struct elfhdr);
  802. elf->e_phentsize = sizeof(struct elf_phdr);
  803. elf->e_phnum = 0;
  804. elf->e_shentsize = 0;
  805. elf->e_shnum = 0;
  806. elf->e_shstrndx = 0;
  807. return 0;
  808. }
  809. /*
  810. * Traverse through memblock structure and setup crash memory ranges. These
  811. * ranges will be used create PT_LOAD program headers in elfcore header.
  812. */
  813. static void fadump_setup_crash_memory_ranges(void)
  814. {
  815. struct memblock_region *reg;
  816. unsigned long long start, end;
  817. pr_debug("Setup crash memory ranges.\n");
  818. crash_mem_ranges = 0;
  819. /*
  820. * add the first memory chunk (RMA_START through boot_memory_size) as
  821. * a separate memory chunk. The reason is, at the time crash firmware
  822. * will move the content of this memory chunk to different location
  823. * specified during fadump registration. We need to create a separate
  824. * program header for this chunk with the correct offset.
  825. */
  826. fadump_add_crash_memory(RMA_START, fw_dump.boot_memory_size);
  827. for_each_memblock(memory, reg) {
  828. start = (unsigned long long)reg->base;
  829. end = start + (unsigned long long)reg->size;
  830. /*
  831. * skip the first memory chunk that is already added (RMA_START
  832. * through boot_memory_size). This logic needs a relook if and
  833. * when RMA_START changes to a non-zero value.
  834. */
  835. BUILD_BUG_ON(RMA_START != 0);
  836. if (start < fw_dump.boot_memory_size) {
  837. if (end > fw_dump.boot_memory_size)
  838. start = fw_dump.boot_memory_size;
  839. else
  840. continue;
  841. }
  842. /* add this range excluding the reserved dump area. */
  843. fadump_exclude_reserved_area(start, end);
  844. }
  845. }
  846. /*
  847. * If the given physical address falls within the boot memory region then
  848. * return the relocated address that points to the dump region reserved
  849. * for saving initial boot memory contents.
  850. */
  851. static inline unsigned long fadump_relocate(unsigned long paddr)
  852. {
  853. if (paddr > RMA_START && paddr < fw_dump.boot_memory_size)
  854. return be64_to_cpu(fdm.rmr_region.destination_address) + paddr;
  855. else
  856. return paddr;
  857. }
  858. static int fadump_create_elfcore_headers(char *bufp)
  859. {
  860. struct elfhdr *elf;
  861. struct elf_phdr *phdr;
  862. int i;
  863. fadump_init_elfcore_header(bufp);
  864. elf = (struct elfhdr *)bufp;
  865. bufp += sizeof(struct elfhdr);
  866. /*
  867. * setup ELF PT_NOTE, place holder for cpu notes info. The notes info
  868. * will be populated during second kernel boot after crash. Hence
  869. * this PT_NOTE will always be the first elf note.
  870. *
  871. * NOTE: Any new ELF note addition should be placed after this note.
  872. */
  873. phdr = (struct elf_phdr *)bufp;
  874. bufp += sizeof(struct elf_phdr);
  875. phdr->p_type = PT_NOTE;
  876. phdr->p_flags = 0;
  877. phdr->p_vaddr = 0;
  878. phdr->p_align = 0;
  879. phdr->p_offset = 0;
  880. phdr->p_paddr = 0;
  881. phdr->p_filesz = 0;
  882. phdr->p_memsz = 0;
  883. (elf->e_phnum)++;
  884. /* setup ELF PT_NOTE for vmcoreinfo */
  885. phdr = (struct elf_phdr *)bufp;
  886. bufp += sizeof(struct elf_phdr);
  887. phdr->p_type = PT_NOTE;
  888. phdr->p_flags = 0;
  889. phdr->p_vaddr = 0;
  890. phdr->p_align = 0;
  891. phdr->p_paddr = fadump_relocate(paddr_vmcoreinfo_note());
  892. phdr->p_offset = phdr->p_paddr;
  893. phdr->p_memsz = phdr->p_filesz = VMCOREINFO_NOTE_SIZE;
  894. /* Increment number of program headers. */
  895. (elf->e_phnum)++;
  896. /* setup PT_LOAD sections. */
  897. for (i = 0; i < crash_mem_ranges; i++) {
  898. unsigned long long mbase, msize;
  899. mbase = crash_memory_ranges[i].base;
  900. msize = crash_memory_ranges[i].size;
  901. if (!msize)
  902. continue;
  903. phdr = (struct elf_phdr *)bufp;
  904. bufp += sizeof(struct elf_phdr);
  905. phdr->p_type = PT_LOAD;
  906. phdr->p_flags = PF_R|PF_W|PF_X;
  907. phdr->p_offset = mbase;
  908. if (mbase == RMA_START) {
  909. /*
  910. * The entire RMA region will be moved by firmware
  911. * to the specified destination_address. Hence set
  912. * the correct offset.
  913. */
  914. phdr->p_offset = be64_to_cpu(fdm.rmr_region.destination_address);
  915. }
  916. phdr->p_paddr = mbase;
  917. phdr->p_vaddr = (unsigned long)__va(mbase);
  918. phdr->p_filesz = msize;
  919. phdr->p_memsz = msize;
  920. phdr->p_align = 0;
  921. /* Increment number of program headers. */
  922. (elf->e_phnum)++;
  923. }
  924. return 0;
  925. }
  926. static unsigned long init_fadump_header(unsigned long addr)
  927. {
  928. struct fadump_crash_info_header *fdh;
  929. if (!addr)
  930. return 0;
  931. fw_dump.fadumphdr_addr = addr;
  932. fdh = __va(addr);
  933. addr += sizeof(struct fadump_crash_info_header);
  934. memset(fdh, 0, sizeof(struct fadump_crash_info_header));
  935. fdh->magic_number = FADUMP_CRASH_INFO_MAGIC;
  936. fdh->elfcorehdr_addr = addr;
  937. /* We will set the crashing cpu id in crash_fadump() during crash. */
  938. fdh->crashing_cpu = CPU_UNKNOWN;
  939. return addr;
  940. }
  941. static int register_fadump(void)
  942. {
  943. unsigned long addr;
  944. void *vaddr;
  945. /*
  946. * If no memory is reserved then we can not register for firmware-
  947. * assisted dump.
  948. */
  949. if (!fw_dump.reserve_dump_area_size)
  950. return -ENODEV;
  951. fadump_setup_crash_memory_ranges();
  952. addr = be64_to_cpu(fdm.rmr_region.destination_address) + be64_to_cpu(fdm.rmr_region.source_len);
  953. /* Initialize fadump crash info header. */
  954. addr = init_fadump_header(addr);
  955. vaddr = __va(addr);
  956. pr_debug("Creating ELF core headers at %#016lx\n", addr);
  957. fadump_create_elfcore_headers(vaddr);
  958. /* register the future kernel dump with firmware. */
  959. return register_fw_dump(&fdm);
  960. }
  961. static int fadump_unregister_dump(struct fadump_mem_struct *fdm)
  962. {
  963. int rc = 0;
  964. unsigned int wait_time;
  965. pr_debug("Un-register firmware-assisted dump\n");
  966. /* TODO: Add upper time limit for the delay */
  967. do {
  968. rc = rtas_call(fw_dump.ibm_configure_kernel_dump, 3, 1, NULL,
  969. FADUMP_UNREGISTER, fdm,
  970. sizeof(struct fadump_mem_struct));
  971. wait_time = rtas_busy_delay_time(rc);
  972. if (wait_time)
  973. mdelay(wait_time);
  974. } while (wait_time);
  975. if (rc) {
  976. printk(KERN_ERR "Failed to un-register firmware-assisted dump."
  977. " unexpected error(%d).\n", rc);
  978. return rc;
  979. }
  980. fw_dump.dump_registered = 0;
  981. return 0;
  982. }
  983. static int fadump_invalidate_dump(struct fadump_mem_struct *fdm)
  984. {
  985. int rc = 0;
  986. unsigned int wait_time;
  987. pr_debug("Invalidating firmware-assisted dump registration\n");
  988. /* TODO: Add upper time limit for the delay */
  989. do {
  990. rc = rtas_call(fw_dump.ibm_configure_kernel_dump, 3, 1, NULL,
  991. FADUMP_INVALIDATE, fdm,
  992. sizeof(struct fadump_mem_struct));
  993. wait_time = rtas_busy_delay_time(rc);
  994. if (wait_time)
  995. mdelay(wait_time);
  996. } while (wait_time);
  997. if (rc) {
  998. pr_err("Failed to invalidate firmware-assisted dump registration. Unexpected error (%d).\n", rc);
  999. return rc;
  1000. }
  1001. fw_dump.dump_active = 0;
  1002. fdm_active = NULL;
  1003. return 0;
  1004. }
  1005. void fadump_cleanup(void)
  1006. {
  1007. /* Invalidate the registration only if dump is active. */
  1008. if (fw_dump.dump_active) {
  1009. init_fadump_mem_struct(&fdm,
  1010. be64_to_cpu(fdm_active->cpu_state_data.destination_address));
  1011. fadump_invalidate_dump(&fdm);
  1012. } else if (fw_dump.dump_registered) {
  1013. /* Un-register Firmware-assisted dump if it was registered. */
  1014. fadump_unregister_dump(&fdm);
  1015. }
  1016. }
  1017. static void fadump_free_reserved_memory(unsigned long start_pfn,
  1018. unsigned long end_pfn)
  1019. {
  1020. unsigned long pfn;
  1021. unsigned long time_limit = jiffies + HZ;
  1022. pr_info("freeing reserved memory (0x%llx - 0x%llx)\n",
  1023. PFN_PHYS(start_pfn), PFN_PHYS(end_pfn));
  1024. for (pfn = start_pfn; pfn < end_pfn; pfn++) {
  1025. free_reserved_page(pfn_to_page(pfn));
  1026. if (time_after(jiffies, time_limit)) {
  1027. cond_resched();
  1028. time_limit = jiffies + HZ;
  1029. }
  1030. }
  1031. }
  1032. /*
  1033. * Skip memory holes and free memory that was actually reserved.
  1034. */
  1035. static void fadump_release_reserved_area(unsigned long start, unsigned long end)
  1036. {
  1037. struct memblock_region *reg;
  1038. unsigned long tstart, tend;
  1039. unsigned long start_pfn = PHYS_PFN(start);
  1040. unsigned long end_pfn = PHYS_PFN(end);
  1041. for_each_memblock(memory, reg) {
  1042. tstart = max(start_pfn, memblock_region_memory_base_pfn(reg));
  1043. tend = min(end_pfn, memblock_region_memory_end_pfn(reg));
  1044. if (tstart < tend) {
  1045. fadump_free_reserved_memory(tstart, tend);
  1046. if (tend == end_pfn)
  1047. break;
  1048. start_pfn = tend + 1;
  1049. }
  1050. }
  1051. }
  1052. /*
  1053. * Release the memory that was reserved in early boot to preserve the memory
  1054. * contents. The released memory will be available for general use.
  1055. */
  1056. static void fadump_release_memory(unsigned long begin, unsigned long end)
  1057. {
  1058. unsigned long ra_start, ra_end;
  1059. ra_start = fw_dump.reserve_dump_area_start;
  1060. ra_end = ra_start + fw_dump.reserve_dump_area_size;
  1061. /*
  1062. * exclude the dump reserve area. Will reuse it for next
  1063. * fadump registration.
  1064. */
  1065. if (begin < ra_end && end > ra_start) {
  1066. if (begin < ra_start)
  1067. fadump_release_reserved_area(begin, ra_start);
  1068. if (end > ra_end)
  1069. fadump_release_reserved_area(ra_end, end);
  1070. } else
  1071. fadump_release_reserved_area(begin, end);
  1072. }
  1073. static void fadump_invalidate_release_mem(void)
  1074. {
  1075. unsigned long reserved_area_start, reserved_area_end;
  1076. unsigned long destination_address;
  1077. mutex_lock(&fadump_mutex);
  1078. if (!fw_dump.dump_active) {
  1079. mutex_unlock(&fadump_mutex);
  1080. return;
  1081. }
  1082. destination_address = be64_to_cpu(fdm_active->cpu_state_data.destination_address);
  1083. fadump_cleanup();
  1084. mutex_unlock(&fadump_mutex);
  1085. /*
  1086. * Save the current reserved memory bounds we will require them
  1087. * later for releasing the memory for general use.
  1088. */
  1089. reserved_area_start = fw_dump.reserve_dump_area_start;
  1090. reserved_area_end = reserved_area_start +
  1091. fw_dump.reserve_dump_area_size;
  1092. /*
  1093. * Setup reserve_dump_area_start and its size so that we can
  1094. * reuse this reserved memory for Re-registration.
  1095. */
  1096. fw_dump.reserve_dump_area_start = destination_address;
  1097. fw_dump.reserve_dump_area_size = get_fadump_area_size();
  1098. fadump_release_memory(reserved_area_start, reserved_area_end);
  1099. if (fw_dump.cpu_notes_buf) {
  1100. fadump_cpu_notes_buf_free(
  1101. (unsigned long)__va(fw_dump.cpu_notes_buf),
  1102. fw_dump.cpu_notes_buf_size);
  1103. fw_dump.cpu_notes_buf = 0;
  1104. fw_dump.cpu_notes_buf_size = 0;
  1105. }
  1106. /* Initialize the kernel dump memory structure for FAD registration. */
  1107. init_fadump_mem_struct(&fdm, fw_dump.reserve_dump_area_start);
  1108. }
  1109. static ssize_t fadump_release_memory_store(struct kobject *kobj,
  1110. struct kobj_attribute *attr,
  1111. const char *buf, size_t count)
  1112. {
  1113. int input = -1;
  1114. if (!fw_dump.dump_active)
  1115. return -EPERM;
  1116. if (kstrtoint(buf, 0, &input))
  1117. return -EINVAL;
  1118. if (input == 1) {
  1119. /*
  1120. * Take away the '/proc/vmcore'. We are releasing the dump
  1121. * memory, hence it will not be valid anymore.
  1122. */
  1123. #ifdef CONFIG_PROC_VMCORE
  1124. vmcore_cleanup();
  1125. #endif
  1126. fadump_invalidate_release_mem();
  1127. } else
  1128. return -EINVAL;
  1129. return count;
  1130. }
  1131. static ssize_t fadump_enabled_show(struct kobject *kobj,
  1132. struct kobj_attribute *attr,
  1133. char *buf)
  1134. {
  1135. return sprintf(buf, "%d\n", fw_dump.fadump_enabled);
  1136. }
  1137. static ssize_t fadump_register_show(struct kobject *kobj,
  1138. struct kobj_attribute *attr,
  1139. char *buf)
  1140. {
  1141. return sprintf(buf, "%d\n", fw_dump.dump_registered);
  1142. }
  1143. static ssize_t fadump_register_store(struct kobject *kobj,
  1144. struct kobj_attribute *attr,
  1145. const char *buf, size_t count)
  1146. {
  1147. int ret = 0;
  1148. int input = -1;
  1149. if (!fw_dump.fadump_enabled || fdm_active)
  1150. return -EPERM;
  1151. if (kstrtoint(buf, 0, &input))
  1152. return -EINVAL;
  1153. mutex_lock(&fadump_mutex);
  1154. switch (input) {
  1155. case 0:
  1156. if (fw_dump.dump_registered == 0) {
  1157. goto unlock_out;
  1158. }
  1159. /* Un-register Firmware-assisted dump */
  1160. fadump_unregister_dump(&fdm);
  1161. break;
  1162. case 1:
  1163. if (fw_dump.dump_registered == 1) {
  1164. ret = -EEXIST;
  1165. goto unlock_out;
  1166. }
  1167. /* Register Firmware-assisted dump */
  1168. ret = register_fadump();
  1169. break;
  1170. default:
  1171. ret = -EINVAL;
  1172. break;
  1173. }
  1174. unlock_out:
  1175. mutex_unlock(&fadump_mutex);
  1176. return ret < 0 ? ret : count;
  1177. }
  1178. static int fadump_region_show(struct seq_file *m, void *private)
  1179. {
  1180. const struct fadump_mem_struct *fdm_ptr;
  1181. if (!fw_dump.fadump_enabled)
  1182. return 0;
  1183. mutex_lock(&fadump_mutex);
  1184. if (fdm_active)
  1185. fdm_ptr = fdm_active;
  1186. else {
  1187. mutex_unlock(&fadump_mutex);
  1188. fdm_ptr = &fdm;
  1189. }
  1190. seq_printf(m,
  1191. "CPU : [%#016llx-%#016llx] %#llx bytes, "
  1192. "Dumped: %#llx\n",
  1193. be64_to_cpu(fdm_ptr->cpu_state_data.destination_address),
  1194. be64_to_cpu(fdm_ptr->cpu_state_data.destination_address) +
  1195. be64_to_cpu(fdm_ptr->cpu_state_data.source_len) - 1,
  1196. be64_to_cpu(fdm_ptr->cpu_state_data.source_len),
  1197. be64_to_cpu(fdm_ptr->cpu_state_data.bytes_dumped));
  1198. seq_printf(m,
  1199. "HPTE: [%#016llx-%#016llx] %#llx bytes, "
  1200. "Dumped: %#llx\n",
  1201. be64_to_cpu(fdm_ptr->hpte_region.destination_address),
  1202. be64_to_cpu(fdm_ptr->hpte_region.destination_address) +
  1203. be64_to_cpu(fdm_ptr->hpte_region.source_len) - 1,
  1204. be64_to_cpu(fdm_ptr->hpte_region.source_len),
  1205. be64_to_cpu(fdm_ptr->hpte_region.bytes_dumped));
  1206. seq_printf(m,
  1207. "DUMP: [%#016llx-%#016llx] %#llx bytes, "
  1208. "Dumped: %#llx\n",
  1209. be64_to_cpu(fdm_ptr->rmr_region.destination_address),
  1210. be64_to_cpu(fdm_ptr->rmr_region.destination_address) +
  1211. be64_to_cpu(fdm_ptr->rmr_region.source_len) - 1,
  1212. be64_to_cpu(fdm_ptr->rmr_region.source_len),
  1213. be64_to_cpu(fdm_ptr->rmr_region.bytes_dumped));
  1214. if (!fdm_active ||
  1215. (fw_dump.reserve_dump_area_start ==
  1216. be64_to_cpu(fdm_ptr->cpu_state_data.destination_address)))
  1217. goto out;
  1218. /* Dump is active. Show reserved memory region. */
  1219. seq_printf(m,
  1220. " : [%#016llx-%#016llx] %#llx bytes, "
  1221. "Dumped: %#llx\n",
  1222. (unsigned long long)fw_dump.reserve_dump_area_start,
  1223. be64_to_cpu(fdm_ptr->cpu_state_data.destination_address) - 1,
  1224. be64_to_cpu(fdm_ptr->cpu_state_data.destination_address) -
  1225. fw_dump.reserve_dump_area_start,
  1226. be64_to_cpu(fdm_ptr->cpu_state_data.destination_address) -
  1227. fw_dump.reserve_dump_area_start);
  1228. out:
  1229. if (fdm_active)
  1230. mutex_unlock(&fadump_mutex);
  1231. return 0;
  1232. }
  1233. static struct kobj_attribute fadump_release_attr = __ATTR(fadump_release_mem,
  1234. 0200, NULL,
  1235. fadump_release_memory_store);
  1236. static struct kobj_attribute fadump_attr = __ATTR(fadump_enabled,
  1237. 0444, fadump_enabled_show,
  1238. NULL);
  1239. static struct kobj_attribute fadump_register_attr = __ATTR(fadump_registered,
  1240. 0644, fadump_register_show,
  1241. fadump_register_store);
  1242. static int fadump_region_open(struct inode *inode, struct file *file)
  1243. {
  1244. return single_open(file, fadump_region_show, inode->i_private);
  1245. }
  1246. static const struct file_operations fadump_region_fops = {
  1247. .open = fadump_region_open,
  1248. .read = seq_read,
  1249. .llseek = seq_lseek,
  1250. .release = single_release,
  1251. };
  1252. static void fadump_init_files(void)
  1253. {
  1254. struct dentry *debugfs_file;
  1255. int rc = 0;
  1256. rc = sysfs_create_file(kernel_kobj, &fadump_attr.attr);
  1257. if (rc)
  1258. printk(KERN_ERR "fadump: unable to create sysfs file"
  1259. " fadump_enabled (%d)\n", rc);
  1260. rc = sysfs_create_file(kernel_kobj, &fadump_register_attr.attr);
  1261. if (rc)
  1262. printk(KERN_ERR "fadump: unable to create sysfs file"
  1263. " fadump_registered (%d)\n", rc);
  1264. debugfs_file = debugfs_create_file("fadump_region", 0444,
  1265. powerpc_debugfs_root, NULL,
  1266. &fadump_region_fops);
  1267. if (!debugfs_file)
  1268. printk(KERN_ERR "fadump: unable to create debugfs file"
  1269. " fadump_region\n");
  1270. if (fw_dump.dump_active) {
  1271. rc = sysfs_create_file(kernel_kobj, &fadump_release_attr.attr);
  1272. if (rc)
  1273. printk(KERN_ERR "fadump: unable to create sysfs file"
  1274. " fadump_release_mem (%d)\n", rc);
  1275. }
  1276. return;
  1277. }
  1278. /*
  1279. * Prepare for firmware-assisted dump.
  1280. */
  1281. int __init setup_fadump(void)
  1282. {
  1283. if (!fw_dump.fadump_enabled)
  1284. return 0;
  1285. if (!fw_dump.fadump_supported) {
  1286. printk(KERN_ERR "Firmware-assisted dump is not supported on"
  1287. " this hardware\n");
  1288. return 0;
  1289. }
  1290. fadump_show_config();
  1291. /*
  1292. * If dump data is available then see if it is valid and prepare for
  1293. * saving it to the disk.
  1294. */
  1295. if (fw_dump.dump_active) {
  1296. /*
  1297. * if dump process fails then invalidate the registration
  1298. * and release memory before proceeding for re-registration.
  1299. */
  1300. if (process_fadump(fdm_active) < 0)
  1301. fadump_invalidate_release_mem();
  1302. }
  1303. /* Initialize the kernel dump memory structure for FAD registration. */
  1304. else if (fw_dump.reserve_dump_area_size)
  1305. init_fadump_mem_struct(&fdm, fw_dump.reserve_dump_area_start);
  1306. fadump_init_files();
  1307. return 1;
  1308. }
  1309. subsys_initcall(setup_fadump);