fadump.c 43 KB

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