kexec_core.c 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588
  1. /*
  2. * kexec.c - kexec system call core code.
  3. * Copyright (C) 2002-2004 Eric Biederman <ebiederm@xmission.com>
  4. *
  5. * This source code is licensed under the GNU General Public License,
  6. * Version 2. See the file COPYING for more details.
  7. */
  8. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  9. #include <linux/capability.h>
  10. #include <linux/mm.h>
  11. #include <linux/file.h>
  12. #include <linux/slab.h>
  13. #include <linux/fs.h>
  14. #include <linux/kexec.h>
  15. #include <linux/mutex.h>
  16. #include <linux/list.h>
  17. #include <linux/highmem.h>
  18. #include <linux/syscalls.h>
  19. #include <linux/reboot.h>
  20. #include <linux/ioport.h>
  21. #include <linux/hardirq.h>
  22. #include <linux/elf.h>
  23. #include <linux/elfcore.h>
  24. #include <linux/utsname.h>
  25. #include <linux/numa.h>
  26. #include <linux/suspend.h>
  27. #include <linux/device.h>
  28. #include <linux/freezer.h>
  29. #include <linux/pm.h>
  30. #include <linux/cpu.h>
  31. #include <linux/uaccess.h>
  32. #include <linux/io.h>
  33. #include <linux/console.h>
  34. #include <linux/vmalloc.h>
  35. #include <linux/swap.h>
  36. #include <linux/syscore_ops.h>
  37. #include <linux/compiler.h>
  38. #include <linux/hugetlb.h>
  39. #include <linux/frame.h>
  40. #include <asm/page.h>
  41. #include <asm/sections.h>
  42. #include <crypto/hash.h>
  43. #include <crypto/sha.h>
  44. #include "kexec_internal.h"
  45. DEFINE_MUTEX(kexec_mutex);
  46. /* Per cpu memory for storing cpu states in case of system crash. */
  47. note_buf_t __percpu *crash_notes;
  48. /* vmcoreinfo stuff */
  49. static unsigned char vmcoreinfo_data[VMCOREINFO_BYTES];
  50. u32 vmcoreinfo_note[VMCOREINFO_NOTE_SIZE/4];
  51. size_t vmcoreinfo_size;
  52. size_t vmcoreinfo_max_size = sizeof(vmcoreinfo_data);
  53. /* Flag to indicate we are going to kexec a new kernel */
  54. bool kexec_in_progress = false;
  55. /* Location of the reserved area for the crash kernel */
  56. struct resource crashk_res = {
  57. .name = "Crash kernel",
  58. .start = 0,
  59. .end = 0,
  60. .flags = IORESOURCE_BUSY | IORESOURCE_SYSTEM_RAM,
  61. .desc = IORES_DESC_CRASH_KERNEL
  62. };
  63. struct resource crashk_low_res = {
  64. .name = "Crash kernel",
  65. .start = 0,
  66. .end = 0,
  67. .flags = IORESOURCE_BUSY | IORESOURCE_SYSTEM_RAM,
  68. .desc = IORES_DESC_CRASH_KERNEL
  69. };
  70. int kexec_should_crash(struct task_struct *p)
  71. {
  72. /*
  73. * If crash_kexec_post_notifiers is enabled, don't run
  74. * crash_kexec() here yet, which must be run after panic
  75. * notifiers in panic().
  76. */
  77. if (crash_kexec_post_notifiers)
  78. return 0;
  79. /*
  80. * There are 4 panic() calls in do_exit() path, each of which
  81. * corresponds to each of these 4 conditions.
  82. */
  83. if (in_interrupt() || !p->pid || is_global_init(p) || panic_on_oops)
  84. return 1;
  85. return 0;
  86. }
  87. int kexec_crash_loaded(void)
  88. {
  89. return !!kexec_crash_image;
  90. }
  91. EXPORT_SYMBOL_GPL(kexec_crash_loaded);
  92. /*
  93. * When kexec transitions to the new kernel there is a one-to-one
  94. * mapping between physical and virtual addresses. On processors
  95. * where you can disable the MMU this is trivial, and easy. For
  96. * others it is still a simple predictable page table to setup.
  97. *
  98. * In that environment kexec copies the new kernel to its final
  99. * resting place. This means I can only support memory whose
  100. * physical address can fit in an unsigned long. In particular
  101. * addresses where (pfn << PAGE_SHIFT) > ULONG_MAX cannot be handled.
  102. * If the assembly stub has more restrictive requirements
  103. * KEXEC_SOURCE_MEMORY_LIMIT and KEXEC_DEST_MEMORY_LIMIT can be
  104. * defined more restrictively in <asm/kexec.h>.
  105. *
  106. * The code for the transition from the current kernel to the
  107. * the new kernel is placed in the control_code_buffer, whose size
  108. * is given by KEXEC_CONTROL_PAGE_SIZE. In the best case only a single
  109. * page of memory is necessary, but some architectures require more.
  110. * Because this memory must be identity mapped in the transition from
  111. * virtual to physical addresses it must live in the range
  112. * 0 - TASK_SIZE, as only the user space mappings are arbitrarily
  113. * modifiable.
  114. *
  115. * The assembly stub in the control code buffer is passed a linked list
  116. * of descriptor pages detailing the source pages of the new kernel,
  117. * and the destination addresses of those source pages. As this data
  118. * structure is not used in the context of the current OS, it must
  119. * be self-contained.
  120. *
  121. * The code has been made to work with highmem pages and will use a
  122. * destination page in its final resting place (if it happens
  123. * to allocate it). The end product of this is that most of the
  124. * physical address space, and most of RAM can be used.
  125. *
  126. * Future directions include:
  127. * - allocating a page table with the control code buffer identity
  128. * mapped, to simplify machine_kexec and make kexec_on_panic more
  129. * reliable.
  130. */
  131. /*
  132. * KIMAGE_NO_DEST is an impossible destination address..., for
  133. * allocating pages whose destination address we do not care about.
  134. */
  135. #define KIMAGE_NO_DEST (-1UL)
  136. #define PAGE_COUNT(x) (((x) + PAGE_SIZE - 1) >> PAGE_SHIFT)
  137. static struct page *kimage_alloc_page(struct kimage *image,
  138. gfp_t gfp_mask,
  139. unsigned long dest);
  140. int sanity_check_segment_list(struct kimage *image)
  141. {
  142. int i;
  143. unsigned long nr_segments = image->nr_segments;
  144. unsigned long total_pages = 0;
  145. /*
  146. * Verify we have good destination addresses. The caller is
  147. * responsible for making certain we don't attempt to load
  148. * the new image into invalid or reserved areas of RAM. This
  149. * just verifies it is an address we can use.
  150. *
  151. * Since the kernel does everything in page size chunks ensure
  152. * the destination addresses are page aligned. Too many
  153. * special cases crop of when we don't do this. The most
  154. * insidious is getting overlapping destination addresses
  155. * simply because addresses are changed to page size
  156. * granularity.
  157. */
  158. for (i = 0; i < nr_segments; i++) {
  159. unsigned long mstart, mend;
  160. mstart = image->segment[i].mem;
  161. mend = mstart + image->segment[i].memsz;
  162. if (mstart > mend)
  163. return -EADDRNOTAVAIL;
  164. if ((mstart & ~PAGE_MASK) || (mend & ~PAGE_MASK))
  165. return -EADDRNOTAVAIL;
  166. if (mend >= KEXEC_DESTINATION_MEMORY_LIMIT)
  167. return -EADDRNOTAVAIL;
  168. }
  169. /* Verify our destination addresses do not overlap.
  170. * If we alloed overlapping destination addresses
  171. * through very weird things can happen with no
  172. * easy explanation as one segment stops on another.
  173. */
  174. for (i = 0; i < nr_segments; i++) {
  175. unsigned long mstart, mend;
  176. unsigned long j;
  177. mstart = image->segment[i].mem;
  178. mend = mstart + image->segment[i].memsz;
  179. for (j = 0; j < i; j++) {
  180. unsigned long pstart, pend;
  181. pstart = image->segment[j].mem;
  182. pend = pstart + image->segment[j].memsz;
  183. /* Do the segments overlap ? */
  184. if ((mend > pstart) && (mstart < pend))
  185. return -EINVAL;
  186. }
  187. }
  188. /* Ensure our buffer sizes are strictly less than
  189. * our memory sizes. This should always be the case,
  190. * and it is easier to check up front than to be surprised
  191. * later on.
  192. */
  193. for (i = 0; i < nr_segments; i++) {
  194. if (image->segment[i].bufsz > image->segment[i].memsz)
  195. return -EINVAL;
  196. }
  197. /*
  198. * Verify that no more than half of memory will be consumed. If the
  199. * request from userspace is too large, a large amount of time will be
  200. * wasted allocating pages, which can cause a soft lockup.
  201. */
  202. for (i = 0; i < nr_segments; i++) {
  203. if (PAGE_COUNT(image->segment[i].memsz) > totalram_pages / 2)
  204. return -EINVAL;
  205. total_pages += PAGE_COUNT(image->segment[i].memsz);
  206. }
  207. if (total_pages > totalram_pages / 2)
  208. return -EINVAL;
  209. /*
  210. * Verify we have good destination addresses. Normally
  211. * the caller is responsible for making certain we don't
  212. * attempt to load the new image into invalid or reserved
  213. * areas of RAM. But crash kernels are preloaded into a
  214. * reserved area of ram. We must ensure the addresses
  215. * are in the reserved area otherwise preloading the
  216. * kernel could corrupt things.
  217. */
  218. if (image->type == KEXEC_TYPE_CRASH) {
  219. for (i = 0; i < nr_segments; i++) {
  220. unsigned long mstart, mend;
  221. mstart = image->segment[i].mem;
  222. mend = mstart + image->segment[i].memsz - 1;
  223. /* Ensure we are within the crash kernel limits */
  224. if ((mstart < phys_to_boot_phys(crashk_res.start)) ||
  225. (mend > phys_to_boot_phys(crashk_res.end)))
  226. return -EADDRNOTAVAIL;
  227. }
  228. }
  229. return 0;
  230. }
  231. struct kimage *do_kimage_alloc_init(void)
  232. {
  233. struct kimage *image;
  234. /* Allocate a controlling structure */
  235. image = kzalloc(sizeof(*image), GFP_KERNEL);
  236. if (!image)
  237. return NULL;
  238. image->head = 0;
  239. image->entry = &image->head;
  240. image->last_entry = &image->head;
  241. image->control_page = ~0; /* By default this does not apply */
  242. image->type = KEXEC_TYPE_DEFAULT;
  243. /* Initialize the list of control pages */
  244. INIT_LIST_HEAD(&image->control_pages);
  245. /* Initialize the list of destination pages */
  246. INIT_LIST_HEAD(&image->dest_pages);
  247. /* Initialize the list of unusable pages */
  248. INIT_LIST_HEAD(&image->unusable_pages);
  249. return image;
  250. }
  251. int kimage_is_destination_range(struct kimage *image,
  252. unsigned long start,
  253. unsigned long end)
  254. {
  255. unsigned long i;
  256. for (i = 0; i < image->nr_segments; i++) {
  257. unsigned long mstart, mend;
  258. mstart = image->segment[i].mem;
  259. mend = mstart + image->segment[i].memsz;
  260. if ((end > mstart) && (start < mend))
  261. return 1;
  262. }
  263. return 0;
  264. }
  265. static struct page *kimage_alloc_pages(gfp_t gfp_mask, unsigned int order)
  266. {
  267. struct page *pages;
  268. pages = alloc_pages(gfp_mask, order);
  269. if (pages) {
  270. unsigned int count, i;
  271. pages->mapping = NULL;
  272. set_page_private(pages, order);
  273. count = 1 << order;
  274. for (i = 0; i < count; i++)
  275. SetPageReserved(pages + i);
  276. }
  277. return pages;
  278. }
  279. static void kimage_free_pages(struct page *page)
  280. {
  281. unsigned int order, count, i;
  282. order = page_private(page);
  283. count = 1 << order;
  284. for (i = 0; i < count; i++)
  285. ClearPageReserved(page + i);
  286. __free_pages(page, order);
  287. }
  288. void kimage_free_page_list(struct list_head *list)
  289. {
  290. struct page *page, *next;
  291. list_for_each_entry_safe(page, next, list, lru) {
  292. list_del(&page->lru);
  293. kimage_free_pages(page);
  294. }
  295. }
  296. static struct page *kimage_alloc_normal_control_pages(struct kimage *image,
  297. unsigned int order)
  298. {
  299. /* Control pages are special, they are the intermediaries
  300. * that are needed while we copy the rest of the pages
  301. * to their final resting place. As such they must
  302. * not conflict with either the destination addresses
  303. * or memory the kernel is already using.
  304. *
  305. * The only case where we really need more than one of
  306. * these are for architectures where we cannot disable
  307. * the MMU and must instead generate an identity mapped
  308. * page table for all of the memory.
  309. *
  310. * At worst this runs in O(N) of the image size.
  311. */
  312. struct list_head extra_pages;
  313. struct page *pages;
  314. unsigned int count;
  315. count = 1 << order;
  316. INIT_LIST_HEAD(&extra_pages);
  317. /* Loop while I can allocate a page and the page allocated
  318. * is a destination page.
  319. */
  320. do {
  321. unsigned long pfn, epfn, addr, eaddr;
  322. pages = kimage_alloc_pages(KEXEC_CONTROL_MEMORY_GFP, order);
  323. if (!pages)
  324. break;
  325. pfn = page_to_boot_pfn(pages);
  326. epfn = pfn + count;
  327. addr = pfn << PAGE_SHIFT;
  328. eaddr = epfn << PAGE_SHIFT;
  329. if ((epfn >= (KEXEC_CONTROL_MEMORY_LIMIT >> PAGE_SHIFT)) ||
  330. kimage_is_destination_range(image, addr, eaddr)) {
  331. list_add(&pages->lru, &extra_pages);
  332. pages = NULL;
  333. }
  334. } while (!pages);
  335. if (pages) {
  336. /* Remember the allocated page... */
  337. list_add(&pages->lru, &image->control_pages);
  338. /* Because the page is already in it's destination
  339. * location we will never allocate another page at
  340. * that address. Therefore kimage_alloc_pages
  341. * will not return it (again) and we don't need
  342. * to give it an entry in image->segment[].
  343. */
  344. }
  345. /* Deal with the destination pages I have inadvertently allocated.
  346. *
  347. * Ideally I would convert multi-page allocations into single
  348. * page allocations, and add everything to image->dest_pages.
  349. *
  350. * For now it is simpler to just free the pages.
  351. */
  352. kimage_free_page_list(&extra_pages);
  353. return pages;
  354. }
  355. static struct page *kimage_alloc_crash_control_pages(struct kimage *image,
  356. unsigned int order)
  357. {
  358. /* Control pages are special, they are the intermediaries
  359. * that are needed while we copy the rest of the pages
  360. * to their final resting place. As such they must
  361. * not conflict with either the destination addresses
  362. * or memory the kernel is already using.
  363. *
  364. * Control pages are also the only pags we must allocate
  365. * when loading a crash kernel. All of the other pages
  366. * are specified by the segments and we just memcpy
  367. * into them directly.
  368. *
  369. * The only case where we really need more than one of
  370. * these are for architectures where we cannot disable
  371. * the MMU and must instead generate an identity mapped
  372. * page table for all of the memory.
  373. *
  374. * Given the low demand this implements a very simple
  375. * allocator that finds the first hole of the appropriate
  376. * size in the reserved memory region, and allocates all
  377. * of the memory up to and including the hole.
  378. */
  379. unsigned long hole_start, hole_end, size;
  380. struct page *pages;
  381. pages = NULL;
  382. size = (1 << order) << PAGE_SHIFT;
  383. hole_start = (image->control_page + (size - 1)) & ~(size - 1);
  384. hole_end = hole_start + size - 1;
  385. while (hole_end <= crashk_res.end) {
  386. unsigned long i;
  387. if (hole_end > KEXEC_CRASH_CONTROL_MEMORY_LIMIT)
  388. break;
  389. /* See if I overlap any of the segments */
  390. for (i = 0; i < image->nr_segments; i++) {
  391. unsigned long mstart, mend;
  392. mstart = image->segment[i].mem;
  393. mend = mstart + image->segment[i].memsz - 1;
  394. if ((hole_end >= mstart) && (hole_start <= mend)) {
  395. /* Advance the hole to the end of the segment */
  396. hole_start = (mend + (size - 1)) & ~(size - 1);
  397. hole_end = hole_start + size - 1;
  398. break;
  399. }
  400. }
  401. /* If I don't overlap any segments I have found my hole! */
  402. if (i == image->nr_segments) {
  403. pages = pfn_to_page(hole_start >> PAGE_SHIFT);
  404. image->control_page = hole_end;
  405. break;
  406. }
  407. }
  408. return pages;
  409. }
  410. struct page *kimage_alloc_control_pages(struct kimage *image,
  411. unsigned int order)
  412. {
  413. struct page *pages = NULL;
  414. switch (image->type) {
  415. case KEXEC_TYPE_DEFAULT:
  416. pages = kimage_alloc_normal_control_pages(image, order);
  417. break;
  418. case KEXEC_TYPE_CRASH:
  419. pages = kimage_alloc_crash_control_pages(image, order);
  420. break;
  421. }
  422. return pages;
  423. }
  424. static int kimage_add_entry(struct kimage *image, kimage_entry_t entry)
  425. {
  426. if (*image->entry != 0)
  427. image->entry++;
  428. if (image->entry == image->last_entry) {
  429. kimage_entry_t *ind_page;
  430. struct page *page;
  431. page = kimage_alloc_page(image, GFP_KERNEL, KIMAGE_NO_DEST);
  432. if (!page)
  433. return -ENOMEM;
  434. ind_page = page_address(page);
  435. *image->entry = virt_to_boot_phys(ind_page) | IND_INDIRECTION;
  436. image->entry = ind_page;
  437. image->last_entry = ind_page +
  438. ((PAGE_SIZE/sizeof(kimage_entry_t)) - 1);
  439. }
  440. *image->entry = entry;
  441. image->entry++;
  442. *image->entry = 0;
  443. return 0;
  444. }
  445. static int kimage_set_destination(struct kimage *image,
  446. unsigned long destination)
  447. {
  448. int result;
  449. destination &= PAGE_MASK;
  450. result = kimage_add_entry(image, destination | IND_DESTINATION);
  451. return result;
  452. }
  453. static int kimage_add_page(struct kimage *image, unsigned long page)
  454. {
  455. int result;
  456. page &= PAGE_MASK;
  457. result = kimage_add_entry(image, page | IND_SOURCE);
  458. return result;
  459. }
  460. static void kimage_free_extra_pages(struct kimage *image)
  461. {
  462. /* Walk through and free any extra destination pages I may have */
  463. kimage_free_page_list(&image->dest_pages);
  464. /* Walk through and free any unusable pages I have cached */
  465. kimage_free_page_list(&image->unusable_pages);
  466. }
  467. void kimage_terminate(struct kimage *image)
  468. {
  469. if (*image->entry != 0)
  470. image->entry++;
  471. *image->entry = IND_DONE;
  472. }
  473. #define for_each_kimage_entry(image, ptr, entry) \
  474. for (ptr = &image->head; (entry = *ptr) && !(entry & IND_DONE); \
  475. ptr = (entry & IND_INDIRECTION) ? \
  476. boot_phys_to_virt((entry & PAGE_MASK)) : ptr + 1)
  477. static void kimage_free_entry(kimage_entry_t entry)
  478. {
  479. struct page *page;
  480. page = boot_pfn_to_page(entry >> PAGE_SHIFT);
  481. kimage_free_pages(page);
  482. }
  483. void kimage_free(struct kimage *image)
  484. {
  485. kimage_entry_t *ptr, entry;
  486. kimage_entry_t ind = 0;
  487. if (!image)
  488. return;
  489. kimage_free_extra_pages(image);
  490. for_each_kimage_entry(image, ptr, entry) {
  491. if (entry & IND_INDIRECTION) {
  492. /* Free the previous indirection page */
  493. if (ind & IND_INDIRECTION)
  494. kimage_free_entry(ind);
  495. /* Save this indirection page until we are
  496. * done with it.
  497. */
  498. ind = entry;
  499. } else if (entry & IND_SOURCE)
  500. kimage_free_entry(entry);
  501. }
  502. /* Free the final indirection page */
  503. if (ind & IND_INDIRECTION)
  504. kimage_free_entry(ind);
  505. /* Handle any machine specific cleanup */
  506. machine_kexec_cleanup(image);
  507. /* Free the kexec control pages... */
  508. kimage_free_page_list(&image->control_pages);
  509. /*
  510. * Free up any temporary buffers allocated. This might hit if
  511. * error occurred much later after buffer allocation.
  512. */
  513. if (image->file_mode)
  514. kimage_file_post_load_cleanup(image);
  515. kfree(image);
  516. }
  517. static kimage_entry_t *kimage_dst_used(struct kimage *image,
  518. unsigned long page)
  519. {
  520. kimage_entry_t *ptr, entry;
  521. unsigned long destination = 0;
  522. for_each_kimage_entry(image, ptr, entry) {
  523. if (entry & IND_DESTINATION)
  524. destination = entry & PAGE_MASK;
  525. else if (entry & IND_SOURCE) {
  526. if (page == destination)
  527. return ptr;
  528. destination += PAGE_SIZE;
  529. }
  530. }
  531. return NULL;
  532. }
  533. static struct page *kimage_alloc_page(struct kimage *image,
  534. gfp_t gfp_mask,
  535. unsigned long destination)
  536. {
  537. /*
  538. * Here we implement safeguards to ensure that a source page
  539. * is not copied to its destination page before the data on
  540. * the destination page is no longer useful.
  541. *
  542. * To do this we maintain the invariant that a source page is
  543. * either its own destination page, or it is not a
  544. * destination page at all.
  545. *
  546. * That is slightly stronger than required, but the proof
  547. * that no problems will not occur is trivial, and the
  548. * implementation is simply to verify.
  549. *
  550. * When allocating all pages normally this algorithm will run
  551. * in O(N) time, but in the worst case it will run in O(N^2)
  552. * time. If the runtime is a problem the data structures can
  553. * be fixed.
  554. */
  555. struct page *page;
  556. unsigned long addr;
  557. /*
  558. * Walk through the list of destination pages, and see if I
  559. * have a match.
  560. */
  561. list_for_each_entry(page, &image->dest_pages, lru) {
  562. addr = page_to_boot_pfn(page) << PAGE_SHIFT;
  563. if (addr == destination) {
  564. list_del(&page->lru);
  565. return page;
  566. }
  567. }
  568. page = NULL;
  569. while (1) {
  570. kimage_entry_t *old;
  571. /* Allocate a page, if we run out of memory give up */
  572. page = kimage_alloc_pages(gfp_mask, 0);
  573. if (!page)
  574. return NULL;
  575. /* If the page cannot be used file it away */
  576. if (page_to_boot_pfn(page) >
  577. (KEXEC_SOURCE_MEMORY_LIMIT >> PAGE_SHIFT)) {
  578. list_add(&page->lru, &image->unusable_pages);
  579. continue;
  580. }
  581. addr = page_to_boot_pfn(page) << PAGE_SHIFT;
  582. /* If it is the destination page we want use it */
  583. if (addr == destination)
  584. break;
  585. /* If the page is not a destination page use it */
  586. if (!kimage_is_destination_range(image, addr,
  587. addr + PAGE_SIZE))
  588. break;
  589. /*
  590. * I know that the page is someones destination page.
  591. * See if there is already a source page for this
  592. * destination page. And if so swap the source pages.
  593. */
  594. old = kimage_dst_used(image, addr);
  595. if (old) {
  596. /* If so move it */
  597. unsigned long old_addr;
  598. struct page *old_page;
  599. old_addr = *old & PAGE_MASK;
  600. old_page = boot_pfn_to_page(old_addr >> PAGE_SHIFT);
  601. copy_highpage(page, old_page);
  602. *old = addr | (*old & ~PAGE_MASK);
  603. /* The old page I have found cannot be a
  604. * destination page, so return it if it's
  605. * gfp_flags honor the ones passed in.
  606. */
  607. if (!(gfp_mask & __GFP_HIGHMEM) &&
  608. PageHighMem(old_page)) {
  609. kimage_free_pages(old_page);
  610. continue;
  611. }
  612. addr = old_addr;
  613. page = old_page;
  614. break;
  615. }
  616. /* Place the page on the destination list, to be used later */
  617. list_add(&page->lru, &image->dest_pages);
  618. }
  619. return page;
  620. }
  621. static int kimage_load_normal_segment(struct kimage *image,
  622. struct kexec_segment *segment)
  623. {
  624. unsigned long maddr;
  625. size_t ubytes, mbytes;
  626. int result;
  627. unsigned char __user *buf = NULL;
  628. unsigned char *kbuf = NULL;
  629. result = 0;
  630. if (image->file_mode)
  631. kbuf = segment->kbuf;
  632. else
  633. buf = segment->buf;
  634. ubytes = segment->bufsz;
  635. mbytes = segment->memsz;
  636. maddr = segment->mem;
  637. result = kimage_set_destination(image, maddr);
  638. if (result < 0)
  639. goto out;
  640. while (mbytes) {
  641. struct page *page;
  642. char *ptr;
  643. size_t uchunk, mchunk;
  644. page = kimage_alloc_page(image, GFP_HIGHUSER, maddr);
  645. if (!page) {
  646. result = -ENOMEM;
  647. goto out;
  648. }
  649. result = kimage_add_page(image, page_to_boot_pfn(page)
  650. << PAGE_SHIFT);
  651. if (result < 0)
  652. goto out;
  653. ptr = kmap(page);
  654. /* Start with a clear page */
  655. clear_page(ptr);
  656. ptr += maddr & ~PAGE_MASK;
  657. mchunk = min_t(size_t, mbytes,
  658. PAGE_SIZE - (maddr & ~PAGE_MASK));
  659. uchunk = min(ubytes, mchunk);
  660. /* For file based kexec, source pages are in kernel memory */
  661. if (image->file_mode)
  662. memcpy(ptr, kbuf, uchunk);
  663. else
  664. result = copy_from_user(ptr, buf, uchunk);
  665. kunmap(page);
  666. if (result) {
  667. result = -EFAULT;
  668. goto out;
  669. }
  670. ubytes -= uchunk;
  671. maddr += mchunk;
  672. if (image->file_mode)
  673. kbuf += mchunk;
  674. else
  675. buf += mchunk;
  676. mbytes -= mchunk;
  677. }
  678. out:
  679. return result;
  680. }
  681. static int kimage_load_crash_segment(struct kimage *image,
  682. struct kexec_segment *segment)
  683. {
  684. /* For crash dumps kernels we simply copy the data from
  685. * user space to it's destination.
  686. * We do things a page at a time for the sake of kmap.
  687. */
  688. unsigned long maddr;
  689. size_t ubytes, mbytes;
  690. int result;
  691. unsigned char __user *buf = NULL;
  692. unsigned char *kbuf = NULL;
  693. result = 0;
  694. if (image->file_mode)
  695. kbuf = segment->kbuf;
  696. else
  697. buf = segment->buf;
  698. ubytes = segment->bufsz;
  699. mbytes = segment->memsz;
  700. maddr = segment->mem;
  701. while (mbytes) {
  702. struct page *page;
  703. char *ptr;
  704. size_t uchunk, mchunk;
  705. page = boot_pfn_to_page(maddr >> PAGE_SHIFT);
  706. if (!page) {
  707. result = -ENOMEM;
  708. goto out;
  709. }
  710. ptr = kmap(page);
  711. ptr += maddr & ~PAGE_MASK;
  712. mchunk = min_t(size_t, mbytes,
  713. PAGE_SIZE - (maddr & ~PAGE_MASK));
  714. uchunk = min(ubytes, mchunk);
  715. if (mchunk > uchunk) {
  716. /* Zero the trailing part of the page */
  717. memset(ptr + uchunk, 0, mchunk - uchunk);
  718. }
  719. /* For file based kexec, source pages are in kernel memory */
  720. if (image->file_mode)
  721. memcpy(ptr, kbuf, uchunk);
  722. else
  723. result = copy_from_user(ptr, buf, uchunk);
  724. kexec_flush_icache_page(page);
  725. kunmap(page);
  726. if (result) {
  727. result = -EFAULT;
  728. goto out;
  729. }
  730. ubytes -= uchunk;
  731. maddr += mchunk;
  732. if (image->file_mode)
  733. kbuf += mchunk;
  734. else
  735. buf += mchunk;
  736. mbytes -= mchunk;
  737. }
  738. out:
  739. return result;
  740. }
  741. int kimage_load_segment(struct kimage *image,
  742. struct kexec_segment *segment)
  743. {
  744. int result = -ENOMEM;
  745. switch (image->type) {
  746. case KEXEC_TYPE_DEFAULT:
  747. result = kimage_load_normal_segment(image, segment);
  748. break;
  749. case KEXEC_TYPE_CRASH:
  750. result = kimage_load_crash_segment(image, segment);
  751. break;
  752. }
  753. return result;
  754. }
  755. struct kimage *kexec_image;
  756. struct kimage *kexec_crash_image;
  757. int kexec_load_disabled;
  758. /*
  759. * No panic_cpu check version of crash_kexec(). This function is called
  760. * only when panic_cpu holds the current CPU number; this is the only CPU
  761. * which processes crash_kexec routines.
  762. */
  763. void __noclone __crash_kexec(struct pt_regs *regs)
  764. {
  765. /* Take the kexec_mutex here to prevent sys_kexec_load
  766. * running on one cpu from replacing the crash kernel
  767. * we are using after a panic on a different cpu.
  768. *
  769. * If the crash kernel was not located in a fixed area
  770. * of memory the xchg(&kexec_crash_image) would be
  771. * sufficient. But since I reuse the memory...
  772. */
  773. if (mutex_trylock(&kexec_mutex)) {
  774. if (kexec_crash_image) {
  775. struct pt_regs fixed_regs;
  776. crash_setup_regs(&fixed_regs, regs);
  777. crash_save_vmcoreinfo();
  778. machine_crash_shutdown(&fixed_regs);
  779. machine_kexec(kexec_crash_image);
  780. }
  781. mutex_unlock(&kexec_mutex);
  782. }
  783. }
  784. STACK_FRAME_NON_STANDARD(__crash_kexec);
  785. void crash_kexec(struct pt_regs *regs)
  786. {
  787. int old_cpu, this_cpu;
  788. /*
  789. * Only one CPU is allowed to execute the crash_kexec() code as with
  790. * panic(). Otherwise parallel calls of panic() and crash_kexec()
  791. * may stop each other. To exclude them, we use panic_cpu here too.
  792. */
  793. this_cpu = raw_smp_processor_id();
  794. old_cpu = atomic_cmpxchg(&panic_cpu, PANIC_CPU_INVALID, this_cpu);
  795. if (old_cpu == PANIC_CPU_INVALID) {
  796. /* This is the 1st CPU which comes here, so go ahead. */
  797. printk_nmi_flush_on_panic();
  798. __crash_kexec(regs);
  799. /*
  800. * Reset panic_cpu to allow another panic()/crash_kexec()
  801. * call.
  802. */
  803. atomic_set(&panic_cpu, PANIC_CPU_INVALID);
  804. }
  805. }
  806. size_t crash_get_memory_size(void)
  807. {
  808. size_t size = 0;
  809. mutex_lock(&kexec_mutex);
  810. if (crashk_res.end != crashk_res.start)
  811. size = resource_size(&crashk_res);
  812. mutex_unlock(&kexec_mutex);
  813. return size;
  814. }
  815. void __weak crash_free_reserved_phys_range(unsigned long begin,
  816. unsigned long end)
  817. {
  818. unsigned long addr;
  819. for (addr = begin; addr < end; addr += PAGE_SIZE)
  820. free_reserved_page(boot_pfn_to_page(addr >> PAGE_SHIFT));
  821. }
  822. int crash_shrink_memory(unsigned long new_size)
  823. {
  824. int ret = 0;
  825. unsigned long start, end;
  826. unsigned long old_size;
  827. struct resource *ram_res;
  828. mutex_lock(&kexec_mutex);
  829. if (kexec_crash_image) {
  830. ret = -ENOENT;
  831. goto unlock;
  832. }
  833. start = crashk_res.start;
  834. end = crashk_res.end;
  835. old_size = (end == 0) ? 0 : end - start + 1;
  836. if (new_size >= old_size) {
  837. ret = (new_size == old_size) ? 0 : -EINVAL;
  838. goto unlock;
  839. }
  840. ram_res = kzalloc(sizeof(*ram_res), GFP_KERNEL);
  841. if (!ram_res) {
  842. ret = -ENOMEM;
  843. goto unlock;
  844. }
  845. start = roundup(start, KEXEC_CRASH_MEM_ALIGN);
  846. end = roundup(start + new_size, KEXEC_CRASH_MEM_ALIGN);
  847. crash_free_reserved_phys_range(end, crashk_res.end);
  848. if ((start == end) && (crashk_res.parent != NULL))
  849. release_resource(&crashk_res);
  850. ram_res->start = end;
  851. ram_res->end = crashk_res.end;
  852. ram_res->flags = IORESOURCE_BUSY | IORESOURCE_SYSTEM_RAM;
  853. ram_res->name = "System RAM";
  854. crashk_res.end = end - 1;
  855. insert_resource(&iomem_resource, ram_res);
  856. unlock:
  857. mutex_unlock(&kexec_mutex);
  858. return ret;
  859. }
  860. static u32 *append_elf_note(u32 *buf, char *name, unsigned type, void *data,
  861. size_t data_len)
  862. {
  863. struct elf_note note;
  864. note.n_namesz = strlen(name) + 1;
  865. note.n_descsz = data_len;
  866. note.n_type = type;
  867. memcpy(buf, &note, sizeof(note));
  868. buf += (sizeof(note) + 3)/4;
  869. memcpy(buf, name, note.n_namesz);
  870. buf += (note.n_namesz + 3)/4;
  871. memcpy(buf, data, note.n_descsz);
  872. buf += (note.n_descsz + 3)/4;
  873. return buf;
  874. }
  875. static void final_note(u32 *buf)
  876. {
  877. struct elf_note note;
  878. note.n_namesz = 0;
  879. note.n_descsz = 0;
  880. note.n_type = 0;
  881. memcpy(buf, &note, sizeof(note));
  882. }
  883. void crash_save_cpu(struct pt_regs *regs, int cpu)
  884. {
  885. struct elf_prstatus prstatus;
  886. u32 *buf;
  887. if ((cpu < 0) || (cpu >= nr_cpu_ids))
  888. return;
  889. /* Using ELF notes here is opportunistic.
  890. * I need a well defined structure format
  891. * for the data I pass, and I need tags
  892. * on the data to indicate what information I have
  893. * squirrelled away. ELF notes happen to provide
  894. * all of that, so there is no need to invent something new.
  895. */
  896. buf = (u32 *)per_cpu_ptr(crash_notes, cpu);
  897. if (!buf)
  898. return;
  899. memset(&prstatus, 0, sizeof(prstatus));
  900. prstatus.pr_pid = current->pid;
  901. elf_core_copy_kernel_regs(&prstatus.pr_reg, regs);
  902. buf = append_elf_note(buf, KEXEC_CORE_NOTE_NAME, NT_PRSTATUS,
  903. &prstatus, sizeof(prstatus));
  904. final_note(buf);
  905. }
  906. static int __init crash_notes_memory_init(void)
  907. {
  908. /* Allocate memory for saving cpu registers. */
  909. size_t size, align;
  910. /*
  911. * crash_notes could be allocated across 2 vmalloc pages when percpu
  912. * is vmalloc based . vmalloc doesn't guarantee 2 continuous vmalloc
  913. * pages are also on 2 continuous physical pages. In this case the
  914. * 2nd part of crash_notes in 2nd page could be lost since only the
  915. * starting address and size of crash_notes are exported through sysfs.
  916. * Here round up the size of crash_notes to the nearest power of two
  917. * and pass it to __alloc_percpu as align value. This can make sure
  918. * crash_notes is allocated inside one physical page.
  919. */
  920. size = sizeof(note_buf_t);
  921. align = min(roundup_pow_of_two(sizeof(note_buf_t)), PAGE_SIZE);
  922. /*
  923. * Break compile if size is bigger than PAGE_SIZE since crash_notes
  924. * definitely will be in 2 pages with that.
  925. */
  926. BUILD_BUG_ON(size > PAGE_SIZE);
  927. crash_notes = __alloc_percpu(size, align);
  928. if (!crash_notes) {
  929. pr_warn("Memory allocation for saving cpu register states failed\n");
  930. return -ENOMEM;
  931. }
  932. return 0;
  933. }
  934. subsys_initcall(crash_notes_memory_init);
  935. /*
  936. * parsing the "crashkernel" commandline
  937. *
  938. * this code is intended to be called from architecture specific code
  939. */
  940. /*
  941. * This function parses command lines in the format
  942. *
  943. * crashkernel=ramsize-range:size[,...][@offset]
  944. *
  945. * The function returns 0 on success and -EINVAL on failure.
  946. */
  947. static int __init parse_crashkernel_mem(char *cmdline,
  948. unsigned long long system_ram,
  949. unsigned long long *crash_size,
  950. unsigned long long *crash_base)
  951. {
  952. char *cur = cmdline, *tmp;
  953. /* for each entry of the comma-separated list */
  954. do {
  955. unsigned long long start, end = ULLONG_MAX, size;
  956. /* get the start of the range */
  957. start = memparse(cur, &tmp);
  958. if (cur == tmp) {
  959. pr_warn("crashkernel: Memory value expected\n");
  960. return -EINVAL;
  961. }
  962. cur = tmp;
  963. if (*cur != '-') {
  964. pr_warn("crashkernel: '-' expected\n");
  965. return -EINVAL;
  966. }
  967. cur++;
  968. /* if no ':' is here, than we read the end */
  969. if (*cur != ':') {
  970. end = memparse(cur, &tmp);
  971. if (cur == tmp) {
  972. pr_warn("crashkernel: Memory value expected\n");
  973. return -EINVAL;
  974. }
  975. cur = tmp;
  976. if (end <= start) {
  977. pr_warn("crashkernel: end <= start\n");
  978. return -EINVAL;
  979. }
  980. }
  981. if (*cur != ':') {
  982. pr_warn("crashkernel: ':' expected\n");
  983. return -EINVAL;
  984. }
  985. cur++;
  986. size = memparse(cur, &tmp);
  987. if (cur == tmp) {
  988. pr_warn("Memory value expected\n");
  989. return -EINVAL;
  990. }
  991. cur = tmp;
  992. if (size >= system_ram) {
  993. pr_warn("crashkernel: invalid size\n");
  994. return -EINVAL;
  995. }
  996. /* match ? */
  997. if (system_ram >= start && system_ram < end) {
  998. *crash_size = size;
  999. break;
  1000. }
  1001. } while (*cur++ == ',');
  1002. if (*crash_size > 0) {
  1003. while (*cur && *cur != ' ' && *cur != '@')
  1004. cur++;
  1005. if (*cur == '@') {
  1006. cur++;
  1007. *crash_base = memparse(cur, &tmp);
  1008. if (cur == tmp) {
  1009. pr_warn("Memory value expected after '@'\n");
  1010. return -EINVAL;
  1011. }
  1012. }
  1013. }
  1014. return 0;
  1015. }
  1016. /*
  1017. * That function parses "simple" (old) crashkernel command lines like
  1018. *
  1019. * crashkernel=size[@offset]
  1020. *
  1021. * It returns 0 on success and -EINVAL on failure.
  1022. */
  1023. static int __init parse_crashkernel_simple(char *cmdline,
  1024. unsigned long long *crash_size,
  1025. unsigned long long *crash_base)
  1026. {
  1027. char *cur = cmdline;
  1028. *crash_size = memparse(cmdline, &cur);
  1029. if (cmdline == cur) {
  1030. pr_warn("crashkernel: memory value expected\n");
  1031. return -EINVAL;
  1032. }
  1033. if (*cur == '@')
  1034. *crash_base = memparse(cur+1, &cur);
  1035. else if (*cur != ' ' && *cur != '\0') {
  1036. pr_warn("crashkernel: unrecognized char: %c\n", *cur);
  1037. return -EINVAL;
  1038. }
  1039. return 0;
  1040. }
  1041. #define SUFFIX_HIGH 0
  1042. #define SUFFIX_LOW 1
  1043. #define SUFFIX_NULL 2
  1044. static __initdata char *suffix_tbl[] = {
  1045. [SUFFIX_HIGH] = ",high",
  1046. [SUFFIX_LOW] = ",low",
  1047. [SUFFIX_NULL] = NULL,
  1048. };
  1049. /*
  1050. * That function parses "suffix" crashkernel command lines like
  1051. *
  1052. * crashkernel=size,[high|low]
  1053. *
  1054. * It returns 0 on success and -EINVAL on failure.
  1055. */
  1056. static int __init parse_crashkernel_suffix(char *cmdline,
  1057. unsigned long long *crash_size,
  1058. const char *suffix)
  1059. {
  1060. char *cur = cmdline;
  1061. *crash_size = memparse(cmdline, &cur);
  1062. if (cmdline == cur) {
  1063. pr_warn("crashkernel: memory value expected\n");
  1064. return -EINVAL;
  1065. }
  1066. /* check with suffix */
  1067. if (strncmp(cur, suffix, strlen(suffix))) {
  1068. pr_warn("crashkernel: unrecognized char: %c\n", *cur);
  1069. return -EINVAL;
  1070. }
  1071. cur += strlen(suffix);
  1072. if (*cur != ' ' && *cur != '\0') {
  1073. pr_warn("crashkernel: unrecognized char: %c\n", *cur);
  1074. return -EINVAL;
  1075. }
  1076. return 0;
  1077. }
  1078. static __init char *get_last_crashkernel(char *cmdline,
  1079. const char *name,
  1080. const char *suffix)
  1081. {
  1082. char *p = cmdline, *ck_cmdline = NULL;
  1083. /* find crashkernel and use the last one if there are more */
  1084. p = strstr(p, name);
  1085. while (p) {
  1086. char *end_p = strchr(p, ' ');
  1087. char *q;
  1088. if (!end_p)
  1089. end_p = p + strlen(p);
  1090. if (!suffix) {
  1091. int i;
  1092. /* skip the one with any known suffix */
  1093. for (i = 0; suffix_tbl[i]; i++) {
  1094. q = end_p - strlen(suffix_tbl[i]);
  1095. if (!strncmp(q, suffix_tbl[i],
  1096. strlen(suffix_tbl[i])))
  1097. goto next;
  1098. }
  1099. ck_cmdline = p;
  1100. } else {
  1101. q = end_p - strlen(suffix);
  1102. if (!strncmp(q, suffix, strlen(suffix)))
  1103. ck_cmdline = p;
  1104. }
  1105. next:
  1106. p = strstr(p+1, name);
  1107. }
  1108. if (!ck_cmdline)
  1109. return NULL;
  1110. return ck_cmdline;
  1111. }
  1112. static int __init __parse_crashkernel(char *cmdline,
  1113. unsigned long long system_ram,
  1114. unsigned long long *crash_size,
  1115. unsigned long long *crash_base,
  1116. const char *name,
  1117. const char *suffix)
  1118. {
  1119. char *first_colon, *first_space;
  1120. char *ck_cmdline;
  1121. BUG_ON(!crash_size || !crash_base);
  1122. *crash_size = 0;
  1123. *crash_base = 0;
  1124. ck_cmdline = get_last_crashkernel(cmdline, name, suffix);
  1125. if (!ck_cmdline)
  1126. return -EINVAL;
  1127. ck_cmdline += strlen(name);
  1128. if (suffix)
  1129. return parse_crashkernel_suffix(ck_cmdline, crash_size,
  1130. suffix);
  1131. /*
  1132. * if the commandline contains a ':', then that's the extended
  1133. * syntax -- if not, it must be the classic syntax
  1134. */
  1135. first_colon = strchr(ck_cmdline, ':');
  1136. first_space = strchr(ck_cmdline, ' ');
  1137. if (first_colon && (!first_space || first_colon < first_space))
  1138. return parse_crashkernel_mem(ck_cmdline, system_ram,
  1139. crash_size, crash_base);
  1140. return parse_crashkernel_simple(ck_cmdline, crash_size, crash_base);
  1141. }
  1142. /*
  1143. * That function is the entry point for command line parsing and should be
  1144. * called from the arch-specific code.
  1145. */
  1146. int __init parse_crashkernel(char *cmdline,
  1147. unsigned long long system_ram,
  1148. unsigned long long *crash_size,
  1149. unsigned long long *crash_base)
  1150. {
  1151. return __parse_crashkernel(cmdline, system_ram, crash_size, crash_base,
  1152. "crashkernel=", NULL);
  1153. }
  1154. int __init parse_crashkernel_high(char *cmdline,
  1155. unsigned long long system_ram,
  1156. unsigned long long *crash_size,
  1157. unsigned long long *crash_base)
  1158. {
  1159. return __parse_crashkernel(cmdline, system_ram, crash_size, crash_base,
  1160. "crashkernel=", suffix_tbl[SUFFIX_HIGH]);
  1161. }
  1162. int __init parse_crashkernel_low(char *cmdline,
  1163. unsigned long long system_ram,
  1164. unsigned long long *crash_size,
  1165. unsigned long long *crash_base)
  1166. {
  1167. return __parse_crashkernel(cmdline, system_ram, crash_size, crash_base,
  1168. "crashkernel=", suffix_tbl[SUFFIX_LOW]);
  1169. }
  1170. static void update_vmcoreinfo_note(void)
  1171. {
  1172. u32 *buf = vmcoreinfo_note;
  1173. if (!vmcoreinfo_size)
  1174. return;
  1175. buf = append_elf_note(buf, VMCOREINFO_NOTE_NAME, 0, vmcoreinfo_data,
  1176. vmcoreinfo_size);
  1177. final_note(buf);
  1178. }
  1179. void crash_save_vmcoreinfo(void)
  1180. {
  1181. vmcoreinfo_append_str("CRASHTIME=%ld\n", get_seconds());
  1182. update_vmcoreinfo_note();
  1183. }
  1184. void vmcoreinfo_append_str(const char *fmt, ...)
  1185. {
  1186. va_list args;
  1187. char buf[0x50];
  1188. size_t r;
  1189. va_start(args, fmt);
  1190. r = vscnprintf(buf, sizeof(buf), fmt, args);
  1191. va_end(args);
  1192. r = min(r, vmcoreinfo_max_size - vmcoreinfo_size);
  1193. memcpy(&vmcoreinfo_data[vmcoreinfo_size], buf, r);
  1194. vmcoreinfo_size += r;
  1195. }
  1196. /*
  1197. * provide an empty default implementation here -- architecture
  1198. * code may override this
  1199. */
  1200. void __weak arch_crash_save_vmcoreinfo(void)
  1201. {}
  1202. phys_addr_t __weak paddr_vmcoreinfo_note(void)
  1203. {
  1204. return __pa((unsigned long)(char *)&vmcoreinfo_note);
  1205. }
  1206. static int __init crash_save_vmcoreinfo_init(void)
  1207. {
  1208. VMCOREINFO_OSRELEASE(init_uts_ns.name.release);
  1209. VMCOREINFO_PAGESIZE(PAGE_SIZE);
  1210. VMCOREINFO_SYMBOL(init_uts_ns);
  1211. VMCOREINFO_SYMBOL(node_online_map);
  1212. #ifdef CONFIG_MMU
  1213. VMCOREINFO_SYMBOL(swapper_pg_dir);
  1214. #endif
  1215. VMCOREINFO_SYMBOL(_stext);
  1216. VMCOREINFO_SYMBOL(vmap_area_list);
  1217. #ifndef CONFIG_NEED_MULTIPLE_NODES
  1218. VMCOREINFO_SYMBOL(mem_map);
  1219. VMCOREINFO_SYMBOL(contig_page_data);
  1220. #endif
  1221. #ifdef CONFIG_SPARSEMEM
  1222. VMCOREINFO_SYMBOL(mem_section);
  1223. VMCOREINFO_LENGTH(mem_section, NR_SECTION_ROOTS);
  1224. VMCOREINFO_STRUCT_SIZE(mem_section);
  1225. VMCOREINFO_OFFSET(mem_section, section_mem_map);
  1226. #endif
  1227. VMCOREINFO_STRUCT_SIZE(page);
  1228. VMCOREINFO_STRUCT_SIZE(pglist_data);
  1229. VMCOREINFO_STRUCT_SIZE(zone);
  1230. VMCOREINFO_STRUCT_SIZE(free_area);
  1231. VMCOREINFO_STRUCT_SIZE(list_head);
  1232. VMCOREINFO_SIZE(nodemask_t);
  1233. VMCOREINFO_OFFSET(page, flags);
  1234. VMCOREINFO_OFFSET(page, _refcount);
  1235. VMCOREINFO_OFFSET(page, mapping);
  1236. VMCOREINFO_OFFSET(page, lru);
  1237. VMCOREINFO_OFFSET(page, _mapcount);
  1238. VMCOREINFO_OFFSET(page, private);
  1239. VMCOREINFO_OFFSET(page, compound_dtor);
  1240. VMCOREINFO_OFFSET(page, compound_order);
  1241. VMCOREINFO_OFFSET(page, compound_head);
  1242. VMCOREINFO_OFFSET(pglist_data, node_zones);
  1243. VMCOREINFO_OFFSET(pglist_data, nr_zones);
  1244. #ifdef CONFIG_FLAT_NODE_MEM_MAP
  1245. VMCOREINFO_OFFSET(pglist_data, node_mem_map);
  1246. #endif
  1247. VMCOREINFO_OFFSET(pglist_data, node_start_pfn);
  1248. VMCOREINFO_OFFSET(pglist_data, node_spanned_pages);
  1249. VMCOREINFO_OFFSET(pglist_data, node_id);
  1250. VMCOREINFO_OFFSET(zone, free_area);
  1251. VMCOREINFO_OFFSET(zone, vm_stat);
  1252. VMCOREINFO_OFFSET(zone, spanned_pages);
  1253. VMCOREINFO_OFFSET(free_area, free_list);
  1254. VMCOREINFO_OFFSET(list_head, next);
  1255. VMCOREINFO_OFFSET(list_head, prev);
  1256. VMCOREINFO_OFFSET(vmap_area, va_start);
  1257. VMCOREINFO_OFFSET(vmap_area, list);
  1258. VMCOREINFO_LENGTH(zone.free_area, MAX_ORDER);
  1259. log_buf_kexec_setup();
  1260. VMCOREINFO_LENGTH(free_area.free_list, MIGRATE_TYPES);
  1261. VMCOREINFO_NUMBER(NR_FREE_PAGES);
  1262. VMCOREINFO_NUMBER(PG_lru);
  1263. VMCOREINFO_NUMBER(PG_private);
  1264. VMCOREINFO_NUMBER(PG_swapcache);
  1265. VMCOREINFO_NUMBER(PG_slab);
  1266. #ifdef CONFIG_MEMORY_FAILURE
  1267. VMCOREINFO_NUMBER(PG_hwpoison);
  1268. #endif
  1269. VMCOREINFO_NUMBER(PG_head_mask);
  1270. VMCOREINFO_NUMBER(PAGE_BUDDY_MAPCOUNT_VALUE);
  1271. #ifdef CONFIG_X86
  1272. VMCOREINFO_NUMBER(KERNEL_IMAGE_SIZE);
  1273. #endif
  1274. #ifdef CONFIG_HUGETLB_PAGE
  1275. VMCOREINFO_NUMBER(HUGETLB_PAGE_DTOR);
  1276. #endif
  1277. arch_crash_save_vmcoreinfo();
  1278. update_vmcoreinfo_note();
  1279. return 0;
  1280. }
  1281. subsys_initcall(crash_save_vmcoreinfo_init);
  1282. /*
  1283. * Move into place and start executing a preloaded standalone
  1284. * executable. If nothing was preloaded return an error.
  1285. */
  1286. int kernel_kexec(void)
  1287. {
  1288. int error = 0;
  1289. if (!mutex_trylock(&kexec_mutex))
  1290. return -EBUSY;
  1291. if (!kexec_image) {
  1292. error = -EINVAL;
  1293. goto Unlock;
  1294. }
  1295. #ifdef CONFIG_KEXEC_JUMP
  1296. if (kexec_image->preserve_context) {
  1297. lock_system_sleep();
  1298. pm_prepare_console();
  1299. error = freeze_processes();
  1300. if (error) {
  1301. error = -EBUSY;
  1302. goto Restore_console;
  1303. }
  1304. suspend_console();
  1305. error = dpm_suspend_start(PMSG_FREEZE);
  1306. if (error)
  1307. goto Resume_console;
  1308. /* At this point, dpm_suspend_start() has been called,
  1309. * but *not* dpm_suspend_end(). We *must* call
  1310. * dpm_suspend_end() now. Otherwise, drivers for
  1311. * some devices (e.g. interrupt controllers) become
  1312. * desynchronized with the actual state of the
  1313. * hardware at resume time, and evil weirdness ensues.
  1314. */
  1315. error = dpm_suspend_end(PMSG_FREEZE);
  1316. if (error)
  1317. goto Resume_devices;
  1318. error = disable_nonboot_cpus();
  1319. if (error)
  1320. goto Enable_cpus;
  1321. local_irq_disable();
  1322. error = syscore_suspend();
  1323. if (error)
  1324. goto Enable_irqs;
  1325. } else
  1326. #endif
  1327. {
  1328. kexec_in_progress = true;
  1329. kernel_restart_prepare(NULL);
  1330. migrate_to_reboot_cpu();
  1331. /*
  1332. * migrate_to_reboot_cpu() disables CPU hotplug assuming that
  1333. * no further code needs to use CPU hotplug (which is true in
  1334. * the reboot case). However, the kexec path depends on using
  1335. * CPU hotplug again; so re-enable it here.
  1336. */
  1337. cpu_hotplug_enable();
  1338. pr_emerg("Starting new kernel\n");
  1339. machine_shutdown();
  1340. }
  1341. machine_kexec(kexec_image);
  1342. #ifdef CONFIG_KEXEC_JUMP
  1343. if (kexec_image->preserve_context) {
  1344. syscore_resume();
  1345. Enable_irqs:
  1346. local_irq_enable();
  1347. Enable_cpus:
  1348. enable_nonboot_cpus();
  1349. dpm_resume_start(PMSG_RESTORE);
  1350. Resume_devices:
  1351. dpm_resume_end(PMSG_RESTORE);
  1352. Resume_console:
  1353. resume_console();
  1354. thaw_processes();
  1355. Restore_console:
  1356. pm_restore_console();
  1357. unlock_system_sleep();
  1358. }
  1359. #endif
  1360. Unlock:
  1361. mutex_unlock(&kexec_mutex);
  1362. return error;
  1363. }
  1364. /*
  1365. * Protection mechanism for crashkernel reserved memory after
  1366. * the kdump kernel is loaded.
  1367. *
  1368. * Provide an empty default implementation here -- architecture
  1369. * code may override this
  1370. */
  1371. void __weak arch_kexec_protect_crashkres(void)
  1372. {}
  1373. void __weak arch_kexec_unprotect_crashkres(void)
  1374. {}