fadump.c 43 KB

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