kexec_core.c 40 KB

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