kexec_core.c 39 KB

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