kexec_core.c 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584
  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. cond_resched();
  387. if (hole_end > KEXEC_CRASH_CONTROL_MEMORY_LIMIT)
  388. break;
  389. /* See if I overlap any of the segments */
  390. for (i = 0; i < image->nr_segments; i++) {
  391. unsigned long mstart, mend;
  392. mstart = image->segment[i].mem;
  393. mend = mstart + image->segment[i].memsz - 1;
  394. if ((hole_end >= mstart) && (hole_start <= mend)) {
  395. /* Advance the hole to the end of the segment */
  396. hole_start = (mend + (size - 1)) & ~(size - 1);
  397. hole_end = hole_start + size - 1;
  398. break;
  399. }
  400. }
  401. /* If I don't overlap any segments I have found my hole! */
  402. if (i == image->nr_segments) {
  403. pages = pfn_to_page(hole_start >> PAGE_SHIFT);
  404. image->control_page = hole_end;
  405. break;
  406. }
  407. }
  408. return pages;
  409. }
  410. struct page *kimage_alloc_control_pages(struct kimage *image,
  411. unsigned int order)
  412. {
  413. struct page *pages = NULL;
  414. switch (image->type) {
  415. case KEXEC_TYPE_DEFAULT:
  416. pages = kimage_alloc_normal_control_pages(image, order);
  417. break;
  418. case KEXEC_TYPE_CRASH:
  419. pages = kimage_alloc_crash_control_pages(image, order);
  420. break;
  421. }
  422. return pages;
  423. }
  424. static int kimage_add_entry(struct kimage *image, kimage_entry_t entry)
  425. {
  426. if (*image->entry != 0)
  427. image->entry++;
  428. if (image->entry == image->last_entry) {
  429. kimage_entry_t *ind_page;
  430. struct page *page;
  431. page = kimage_alloc_page(image, GFP_KERNEL, KIMAGE_NO_DEST);
  432. if (!page)
  433. return -ENOMEM;
  434. ind_page = page_address(page);
  435. *image->entry = virt_to_boot_phys(ind_page) | IND_INDIRECTION;
  436. image->entry = ind_page;
  437. image->last_entry = ind_page +
  438. ((PAGE_SIZE/sizeof(kimage_entry_t)) - 1);
  439. }
  440. *image->entry = entry;
  441. image->entry++;
  442. *image->entry = 0;
  443. return 0;
  444. }
  445. static int kimage_set_destination(struct kimage *image,
  446. unsigned long destination)
  447. {
  448. int result;
  449. destination &= PAGE_MASK;
  450. result = kimage_add_entry(image, destination | IND_DESTINATION);
  451. return result;
  452. }
  453. static int kimage_add_page(struct kimage *image, unsigned long page)
  454. {
  455. int result;
  456. page &= PAGE_MASK;
  457. result = kimage_add_entry(image, page | IND_SOURCE);
  458. return result;
  459. }
  460. static void kimage_free_extra_pages(struct kimage *image)
  461. {
  462. /* Walk through and free any extra destination pages I may have */
  463. kimage_free_page_list(&image->dest_pages);
  464. /* Walk through and free any unusable pages I have cached */
  465. kimage_free_page_list(&image->unusable_pages);
  466. }
  467. void kimage_terminate(struct kimage *image)
  468. {
  469. if (*image->entry != 0)
  470. image->entry++;
  471. *image->entry = IND_DONE;
  472. }
  473. #define for_each_kimage_entry(image, ptr, entry) \
  474. for (ptr = &image->head; (entry = *ptr) && !(entry & IND_DONE); \
  475. ptr = (entry & IND_INDIRECTION) ? \
  476. boot_phys_to_virt((entry & PAGE_MASK)) : ptr + 1)
  477. static void kimage_free_entry(kimage_entry_t entry)
  478. {
  479. struct page *page;
  480. page = boot_pfn_to_page(entry >> PAGE_SHIFT);
  481. kimage_free_pages(page);
  482. }
  483. void kimage_free(struct kimage *image)
  484. {
  485. kimage_entry_t *ptr, entry;
  486. kimage_entry_t ind = 0;
  487. if (!image)
  488. return;
  489. kimage_free_extra_pages(image);
  490. for_each_kimage_entry(image, ptr, entry) {
  491. if (entry & IND_INDIRECTION) {
  492. /* Free the previous indirection page */
  493. if (ind & IND_INDIRECTION)
  494. kimage_free_entry(ind);
  495. /* Save this indirection page until we are
  496. * done with it.
  497. */
  498. ind = entry;
  499. } else if (entry & IND_SOURCE)
  500. kimage_free_entry(entry);
  501. }
  502. /* Free the final indirection page */
  503. if (ind & IND_INDIRECTION)
  504. kimage_free_entry(ind);
  505. /* Handle any machine specific cleanup */
  506. machine_kexec_cleanup(image);
  507. /* Free the kexec control pages... */
  508. kimage_free_page_list(&image->control_pages);
  509. /*
  510. * Free up any temporary buffers allocated. This might hit if
  511. * error occurred much later after buffer allocation.
  512. */
  513. if (image->file_mode)
  514. kimage_file_post_load_cleanup(image);
  515. kfree(image);
  516. }
  517. static kimage_entry_t *kimage_dst_used(struct kimage *image,
  518. unsigned long page)
  519. {
  520. kimage_entry_t *ptr, entry;
  521. unsigned long destination = 0;
  522. for_each_kimage_entry(image, ptr, entry) {
  523. if (entry & IND_DESTINATION)
  524. destination = entry & PAGE_MASK;
  525. else if (entry & IND_SOURCE) {
  526. if (page == destination)
  527. return ptr;
  528. destination += PAGE_SIZE;
  529. }
  530. }
  531. return NULL;
  532. }
  533. static struct page *kimage_alloc_page(struct kimage *image,
  534. gfp_t gfp_mask,
  535. unsigned long destination)
  536. {
  537. /*
  538. * Here we implement safeguards to ensure that a source page
  539. * is not copied to its destination page before the data on
  540. * the destination page is no longer useful.
  541. *
  542. * To do this we maintain the invariant that a source page is
  543. * either its own destination page, or it is not a
  544. * destination page at all.
  545. *
  546. * That is slightly stronger than required, but the proof
  547. * that no problems will not occur is trivial, and the
  548. * implementation is simply to verify.
  549. *
  550. * When allocating all pages normally this algorithm will run
  551. * in O(N) time, but in the worst case it will run in O(N^2)
  552. * time. If the runtime is a problem the data structures can
  553. * be fixed.
  554. */
  555. struct page *page;
  556. unsigned long addr;
  557. /*
  558. * Walk through the list of destination pages, and see if I
  559. * have a match.
  560. */
  561. list_for_each_entry(page, &image->dest_pages, lru) {
  562. addr = page_to_boot_pfn(page) << PAGE_SHIFT;
  563. if (addr == destination) {
  564. list_del(&page->lru);
  565. return page;
  566. }
  567. }
  568. page = NULL;
  569. while (1) {
  570. kimage_entry_t *old;
  571. /* Allocate a page, if we run out of memory give up */
  572. page = kimage_alloc_pages(gfp_mask, 0);
  573. if (!page)
  574. return NULL;
  575. /* If the page cannot be used file it away */
  576. if (page_to_boot_pfn(page) >
  577. (KEXEC_SOURCE_MEMORY_LIMIT >> PAGE_SHIFT)) {
  578. list_add(&page->lru, &image->unusable_pages);
  579. continue;
  580. }
  581. addr = page_to_boot_pfn(page) << PAGE_SHIFT;
  582. /* If it is the destination page we want use it */
  583. if (addr == destination)
  584. break;
  585. /* If the page is not a destination page use it */
  586. if (!kimage_is_destination_range(image, addr,
  587. addr + PAGE_SIZE))
  588. break;
  589. /*
  590. * I know that the page is someones destination page.
  591. * See if there is already a source page for this
  592. * destination page. And if so swap the source pages.
  593. */
  594. old = kimage_dst_used(image, addr);
  595. if (old) {
  596. /* If so move it */
  597. unsigned long old_addr;
  598. struct page *old_page;
  599. old_addr = *old & PAGE_MASK;
  600. old_page = boot_pfn_to_page(old_addr >> PAGE_SHIFT);
  601. copy_highpage(page, old_page);
  602. *old = addr | (*old & ~PAGE_MASK);
  603. /* The old page I have found cannot be a
  604. * destination page, so return it if it's
  605. * gfp_flags honor the ones passed in.
  606. */
  607. if (!(gfp_mask & __GFP_HIGHMEM) &&
  608. PageHighMem(old_page)) {
  609. kimage_free_pages(old_page);
  610. continue;
  611. }
  612. addr = old_addr;
  613. page = old_page;
  614. break;
  615. }
  616. /* Place the page on the destination list, to be used later */
  617. list_add(&page->lru, &image->dest_pages);
  618. }
  619. return page;
  620. }
  621. static int kimage_load_normal_segment(struct kimage *image,
  622. struct kexec_segment *segment)
  623. {
  624. unsigned long maddr;
  625. size_t ubytes, mbytes;
  626. int result;
  627. unsigned char __user *buf = NULL;
  628. unsigned char *kbuf = NULL;
  629. result = 0;
  630. if (image->file_mode)
  631. kbuf = segment->kbuf;
  632. else
  633. buf = segment->buf;
  634. ubytes = segment->bufsz;
  635. mbytes = segment->memsz;
  636. maddr = segment->mem;
  637. result = kimage_set_destination(image, maddr);
  638. if (result < 0)
  639. goto out;
  640. while (mbytes) {
  641. struct page *page;
  642. char *ptr;
  643. size_t uchunk, mchunk;
  644. page = kimage_alloc_page(image, GFP_HIGHUSER, maddr);
  645. if (!page) {
  646. result = -ENOMEM;
  647. goto out;
  648. }
  649. result = kimage_add_page(image, page_to_boot_pfn(page)
  650. << PAGE_SHIFT);
  651. if (result < 0)
  652. goto out;
  653. ptr = kmap(page);
  654. /* Start with a clear page */
  655. clear_page(ptr);
  656. ptr += maddr & ~PAGE_MASK;
  657. mchunk = min_t(size_t, mbytes,
  658. PAGE_SIZE - (maddr & ~PAGE_MASK));
  659. uchunk = min(ubytes, mchunk);
  660. /* For file based kexec, source pages are in kernel memory */
  661. if (image->file_mode)
  662. memcpy(ptr, kbuf, uchunk);
  663. else
  664. result = copy_from_user(ptr, buf, uchunk);
  665. kunmap(page);
  666. if (result) {
  667. result = -EFAULT;
  668. goto out;
  669. }
  670. ubytes -= uchunk;
  671. maddr += mchunk;
  672. if (image->file_mode)
  673. kbuf += mchunk;
  674. else
  675. buf += mchunk;
  676. mbytes -= mchunk;
  677. }
  678. out:
  679. return result;
  680. }
  681. static int kimage_load_crash_segment(struct kimage *image,
  682. struct kexec_segment *segment)
  683. {
  684. /* For crash dumps kernels we simply copy the data from
  685. * user space to it's destination.
  686. * We do things a page at a time for the sake of kmap.
  687. */
  688. unsigned long maddr;
  689. size_t ubytes, mbytes;
  690. int result;
  691. unsigned char __user *buf = NULL;
  692. unsigned char *kbuf = NULL;
  693. result = 0;
  694. if (image->file_mode)
  695. kbuf = segment->kbuf;
  696. else
  697. buf = segment->buf;
  698. ubytes = segment->bufsz;
  699. mbytes = segment->memsz;
  700. maddr = segment->mem;
  701. while (mbytes) {
  702. struct page *page;
  703. char *ptr;
  704. size_t uchunk, mchunk;
  705. page = boot_pfn_to_page(maddr >> PAGE_SHIFT);
  706. if (!page) {
  707. result = -ENOMEM;
  708. goto out;
  709. }
  710. ptr = kmap(page);
  711. ptr += maddr & ~PAGE_MASK;
  712. mchunk = min_t(size_t, mbytes,
  713. PAGE_SIZE - (maddr & ~PAGE_MASK));
  714. uchunk = min(ubytes, mchunk);
  715. if (mchunk > uchunk) {
  716. /* Zero the trailing part of the page */
  717. memset(ptr + uchunk, 0, mchunk - uchunk);
  718. }
  719. /* For file based kexec, source pages are in kernel memory */
  720. if (image->file_mode)
  721. memcpy(ptr, kbuf, uchunk);
  722. else
  723. result = copy_from_user(ptr, buf, uchunk);
  724. kexec_flush_icache_page(page);
  725. kunmap(page);
  726. if (result) {
  727. result = -EFAULT;
  728. goto out;
  729. }
  730. ubytes -= uchunk;
  731. maddr += mchunk;
  732. if (image->file_mode)
  733. kbuf += mchunk;
  734. else
  735. buf += mchunk;
  736. mbytes -= mchunk;
  737. }
  738. out:
  739. return result;
  740. }
  741. int kimage_load_segment(struct kimage *image,
  742. struct kexec_segment *segment)
  743. {
  744. int result = -ENOMEM;
  745. switch (image->type) {
  746. case KEXEC_TYPE_DEFAULT:
  747. result = kimage_load_normal_segment(image, segment);
  748. break;
  749. case KEXEC_TYPE_CRASH:
  750. result = kimage_load_crash_segment(image, segment);
  751. break;
  752. }
  753. return result;
  754. }
  755. struct kimage *kexec_image;
  756. struct kimage *kexec_crash_image;
  757. int kexec_load_disabled;
  758. /*
  759. * No panic_cpu check version of crash_kexec(). This function is called
  760. * only when panic_cpu holds the current CPU number; this is the only CPU
  761. * which processes crash_kexec routines.
  762. */
  763. void __crash_kexec(struct pt_regs *regs)
  764. {
  765. /* Take the kexec_mutex here to prevent sys_kexec_load
  766. * running on one cpu from replacing the crash kernel
  767. * we are using after a panic on a different cpu.
  768. *
  769. * If the crash kernel was not located in a fixed area
  770. * of memory the xchg(&kexec_crash_image) would be
  771. * sufficient. But since I reuse the memory...
  772. */
  773. if (mutex_trylock(&kexec_mutex)) {
  774. if (kexec_crash_image) {
  775. struct pt_regs fixed_regs;
  776. crash_setup_regs(&fixed_regs, regs);
  777. crash_save_vmcoreinfo();
  778. machine_crash_shutdown(&fixed_regs);
  779. machine_kexec(kexec_crash_image);
  780. }
  781. mutex_unlock(&kexec_mutex);
  782. }
  783. }
  784. void crash_kexec(struct pt_regs *regs)
  785. {
  786. int old_cpu, this_cpu;
  787. /*
  788. * Only one CPU is allowed to execute the crash_kexec() code as with
  789. * panic(). Otherwise parallel calls of panic() and crash_kexec()
  790. * may stop each other. To exclude them, we use panic_cpu here too.
  791. */
  792. this_cpu = raw_smp_processor_id();
  793. old_cpu = atomic_cmpxchg(&panic_cpu, PANIC_CPU_INVALID, this_cpu);
  794. if (old_cpu == PANIC_CPU_INVALID) {
  795. /* This is the 1st CPU which comes here, so go ahead. */
  796. printk_nmi_flush_on_panic();
  797. __crash_kexec(regs);
  798. /*
  799. * Reset panic_cpu to allow another panic()/crash_kexec()
  800. * call.
  801. */
  802. atomic_set(&panic_cpu, PANIC_CPU_INVALID);
  803. }
  804. }
  805. size_t crash_get_memory_size(void)
  806. {
  807. size_t size = 0;
  808. mutex_lock(&kexec_mutex);
  809. if (crashk_res.end != crashk_res.start)
  810. size = resource_size(&crashk_res);
  811. mutex_unlock(&kexec_mutex);
  812. return size;
  813. }
  814. void __weak crash_free_reserved_phys_range(unsigned long begin,
  815. unsigned long end)
  816. {
  817. unsigned long addr;
  818. for (addr = begin; addr < end; addr += PAGE_SIZE)
  819. free_reserved_page(boot_pfn_to_page(addr >> PAGE_SHIFT));
  820. }
  821. int crash_shrink_memory(unsigned long new_size)
  822. {
  823. int ret = 0;
  824. unsigned long start, end;
  825. unsigned long old_size;
  826. struct resource *ram_res;
  827. mutex_lock(&kexec_mutex);
  828. if (kexec_crash_image) {
  829. ret = -ENOENT;
  830. goto unlock;
  831. }
  832. start = crashk_res.start;
  833. end = crashk_res.end;
  834. old_size = (end == 0) ? 0 : end - start + 1;
  835. if (new_size >= old_size) {
  836. ret = (new_size == old_size) ? 0 : -EINVAL;
  837. goto unlock;
  838. }
  839. ram_res = kzalloc(sizeof(*ram_res), GFP_KERNEL);
  840. if (!ram_res) {
  841. ret = -ENOMEM;
  842. goto unlock;
  843. }
  844. start = roundup(start, KEXEC_CRASH_MEM_ALIGN);
  845. end = roundup(start + new_size, KEXEC_CRASH_MEM_ALIGN);
  846. crash_free_reserved_phys_range(end, crashk_res.end);
  847. if ((start == end) && (crashk_res.parent != NULL))
  848. release_resource(&crashk_res);
  849. ram_res->start = end;
  850. ram_res->end = crashk_res.end;
  851. ram_res->flags = IORESOURCE_BUSY | IORESOURCE_SYSTEM_RAM;
  852. ram_res->name = "System RAM";
  853. crashk_res.end = end - 1;
  854. insert_resource(&iomem_resource, ram_res);
  855. unlock:
  856. mutex_unlock(&kexec_mutex);
  857. return ret;
  858. }
  859. static u32 *append_elf_note(u32 *buf, char *name, unsigned type, void *data,
  860. size_t data_len)
  861. {
  862. struct elf_note note;
  863. note.n_namesz = strlen(name) + 1;
  864. note.n_descsz = data_len;
  865. note.n_type = type;
  866. memcpy(buf, &note, sizeof(note));
  867. buf += (sizeof(note) + 3)/4;
  868. memcpy(buf, name, note.n_namesz);
  869. buf += (note.n_namesz + 3)/4;
  870. memcpy(buf, data, note.n_descsz);
  871. buf += (note.n_descsz + 3)/4;
  872. return buf;
  873. }
  874. static void final_note(u32 *buf)
  875. {
  876. struct elf_note note;
  877. note.n_namesz = 0;
  878. note.n_descsz = 0;
  879. note.n_type = 0;
  880. memcpy(buf, &note, sizeof(note));
  881. }
  882. void crash_save_cpu(struct pt_regs *regs, int cpu)
  883. {
  884. struct elf_prstatus prstatus;
  885. u32 *buf;
  886. if ((cpu < 0) || (cpu >= nr_cpu_ids))
  887. return;
  888. /* Using ELF notes here is opportunistic.
  889. * I need a well defined structure format
  890. * for the data I pass, and I need tags
  891. * on the data to indicate what information I have
  892. * squirrelled away. ELF notes happen to provide
  893. * all of that, so there is no need to invent something new.
  894. */
  895. buf = (u32 *)per_cpu_ptr(crash_notes, cpu);
  896. if (!buf)
  897. return;
  898. memset(&prstatus, 0, sizeof(prstatus));
  899. prstatus.pr_pid = current->pid;
  900. elf_core_copy_kernel_regs(&prstatus.pr_reg, regs);
  901. buf = append_elf_note(buf, KEXEC_CORE_NOTE_NAME, NT_PRSTATUS,
  902. &prstatus, sizeof(prstatus));
  903. final_note(buf);
  904. }
  905. static int __init crash_notes_memory_init(void)
  906. {
  907. /* Allocate memory for saving cpu registers. */
  908. size_t size, align;
  909. /*
  910. * crash_notes could be allocated across 2 vmalloc pages when percpu
  911. * is vmalloc based . vmalloc doesn't guarantee 2 continuous vmalloc
  912. * pages are also on 2 continuous physical pages. In this case the
  913. * 2nd part of crash_notes in 2nd page could be lost since only the
  914. * starting address and size of crash_notes are exported through sysfs.
  915. * Here round up the size of crash_notes to the nearest power of two
  916. * and pass it to __alloc_percpu as align value. This can make sure
  917. * crash_notes is allocated inside one physical page.
  918. */
  919. size = sizeof(note_buf_t);
  920. align = min(roundup_pow_of_two(sizeof(note_buf_t)), PAGE_SIZE);
  921. /*
  922. * Break compile if size is bigger than PAGE_SIZE since crash_notes
  923. * definitely will be in 2 pages with that.
  924. */
  925. BUILD_BUG_ON(size > PAGE_SIZE);
  926. crash_notes = __alloc_percpu(size, align);
  927. if (!crash_notes) {
  928. pr_warn("Memory allocation for saving cpu register states failed\n");
  929. return -ENOMEM;
  930. }
  931. return 0;
  932. }
  933. subsys_initcall(crash_notes_memory_init);
  934. /*
  935. * parsing the "crashkernel" commandline
  936. *
  937. * this code is intended to be called from architecture specific code
  938. */
  939. /*
  940. * This function parses command lines in the format
  941. *
  942. * crashkernel=ramsize-range:size[,...][@offset]
  943. *
  944. * The function returns 0 on success and -EINVAL on failure.
  945. */
  946. static int __init parse_crashkernel_mem(char *cmdline,
  947. unsigned long long system_ram,
  948. unsigned long long *crash_size,
  949. unsigned long long *crash_base)
  950. {
  951. char *cur = cmdline, *tmp;
  952. /* for each entry of the comma-separated list */
  953. do {
  954. unsigned long long start, end = ULLONG_MAX, size;
  955. /* get the start of the range */
  956. start = memparse(cur, &tmp);
  957. if (cur == tmp) {
  958. pr_warn("crashkernel: Memory value expected\n");
  959. return -EINVAL;
  960. }
  961. cur = tmp;
  962. if (*cur != '-') {
  963. pr_warn("crashkernel: '-' expected\n");
  964. return -EINVAL;
  965. }
  966. cur++;
  967. /* if no ':' is here, than we read the end */
  968. if (*cur != ':') {
  969. end = memparse(cur, &tmp);
  970. if (cur == tmp) {
  971. pr_warn("crashkernel: Memory value expected\n");
  972. return -EINVAL;
  973. }
  974. cur = tmp;
  975. if (end <= start) {
  976. pr_warn("crashkernel: end <= start\n");
  977. return -EINVAL;
  978. }
  979. }
  980. if (*cur != ':') {
  981. pr_warn("crashkernel: ':' expected\n");
  982. return -EINVAL;
  983. }
  984. cur++;
  985. size = memparse(cur, &tmp);
  986. if (cur == tmp) {
  987. pr_warn("Memory value expected\n");
  988. return -EINVAL;
  989. }
  990. cur = tmp;
  991. if (size >= system_ram) {
  992. pr_warn("crashkernel: invalid size\n");
  993. return -EINVAL;
  994. }
  995. /* match ? */
  996. if (system_ram >= start && system_ram < end) {
  997. *crash_size = size;
  998. break;
  999. }
  1000. } while (*cur++ == ',');
  1001. if (*crash_size > 0) {
  1002. while (*cur && *cur != ' ' && *cur != '@')
  1003. cur++;
  1004. if (*cur == '@') {
  1005. cur++;
  1006. *crash_base = memparse(cur, &tmp);
  1007. if (cur == tmp) {
  1008. pr_warn("Memory value expected after '@'\n");
  1009. return -EINVAL;
  1010. }
  1011. }
  1012. }
  1013. return 0;
  1014. }
  1015. /*
  1016. * That function parses "simple" (old) crashkernel command lines like
  1017. *
  1018. * crashkernel=size[@offset]
  1019. *
  1020. * It returns 0 on success and -EINVAL on failure.
  1021. */
  1022. static int __init parse_crashkernel_simple(char *cmdline,
  1023. unsigned long long *crash_size,
  1024. unsigned long long *crash_base)
  1025. {
  1026. char *cur = cmdline;
  1027. *crash_size = memparse(cmdline, &cur);
  1028. if (cmdline == cur) {
  1029. pr_warn("crashkernel: memory value expected\n");
  1030. return -EINVAL;
  1031. }
  1032. if (*cur == '@')
  1033. *crash_base = memparse(cur+1, &cur);
  1034. else if (*cur != ' ' && *cur != '\0') {
  1035. pr_warn("crashkernel: unrecognized char: %c\n", *cur);
  1036. return -EINVAL;
  1037. }
  1038. return 0;
  1039. }
  1040. #define SUFFIX_HIGH 0
  1041. #define SUFFIX_LOW 1
  1042. #define SUFFIX_NULL 2
  1043. static __initdata char *suffix_tbl[] = {
  1044. [SUFFIX_HIGH] = ",high",
  1045. [SUFFIX_LOW] = ",low",
  1046. [SUFFIX_NULL] = NULL,
  1047. };
  1048. /*
  1049. * That function parses "suffix" crashkernel command lines like
  1050. *
  1051. * crashkernel=size,[high|low]
  1052. *
  1053. * It returns 0 on success and -EINVAL on failure.
  1054. */
  1055. static int __init parse_crashkernel_suffix(char *cmdline,
  1056. unsigned long long *crash_size,
  1057. const char *suffix)
  1058. {
  1059. char *cur = cmdline;
  1060. *crash_size = memparse(cmdline, &cur);
  1061. if (cmdline == cur) {
  1062. pr_warn("crashkernel: memory value expected\n");
  1063. return -EINVAL;
  1064. }
  1065. /* check with suffix */
  1066. if (strncmp(cur, suffix, strlen(suffix))) {
  1067. pr_warn("crashkernel: unrecognized char: %c\n", *cur);
  1068. return -EINVAL;
  1069. }
  1070. cur += strlen(suffix);
  1071. if (*cur != ' ' && *cur != '\0') {
  1072. pr_warn("crashkernel: unrecognized char: %c\n", *cur);
  1073. return -EINVAL;
  1074. }
  1075. return 0;
  1076. }
  1077. static __init char *get_last_crashkernel(char *cmdline,
  1078. const char *name,
  1079. const char *suffix)
  1080. {
  1081. char *p = cmdline, *ck_cmdline = NULL;
  1082. /* find crashkernel and use the last one if there are more */
  1083. p = strstr(p, name);
  1084. while (p) {
  1085. char *end_p = strchr(p, ' ');
  1086. char *q;
  1087. if (!end_p)
  1088. end_p = p + strlen(p);
  1089. if (!suffix) {
  1090. int i;
  1091. /* skip the one with any known suffix */
  1092. for (i = 0; suffix_tbl[i]; i++) {
  1093. q = end_p - strlen(suffix_tbl[i]);
  1094. if (!strncmp(q, suffix_tbl[i],
  1095. strlen(suffix_tbl[i])))
  1096. goto next;
  1097. }
  1098. ck_cmdline = p;
  1099. } else {
  1100. q = end_p - strlen(suffix);
  1101. if (!strncmp(q, suffix, strlen(suffix)))
  1102. ck_cmdline = p;
  1103. }
  1104. next:
  1105. p = strstr(p+1, name);
  1106. }
  1107. if (!ck_cmdline)
  1108. return NULL;
  1109. return ck_cmdline;
  1110. }
  1111. static int __init __parse_crashkernel(char *cmdline,
  1112. unsigned long long system_ram,
  1113. unsigned long long *crash_size,
  1114. unsigned long long *crash_base,
  1115. const char *name,
  1116. const char *suffix)
  1117. {
  1118. char *first_colon, *first_space;
  1119. char *ck_cmdline;
  1120. BUG_ON(!crash_size || !crash_base);
  1121. *crash_size = 0;
  1122. *crash_base = 0;
  1123. ck_cmdline = get_last_crashkernel(cmdline, name, suffix);
  1124. if (!ck_cmdline)
  1125. return -EINVAL;
  1126. ck_cmdline += strlen(name);
  1127. if (suffix)
  1128. return parse_crashkernel_suffix(ck_cmdline, crash_size,
  1129. suffix);
  1130. /*
  1131. * if the commandline contains a ':', then that's the extended
  1132. * syntax -- if not, it must be the classic syntax
  1133. */
  1134. first_colon = strchr(ck_cmdline, ':');
  1135. first_space = strchr(ck_cmdline, ' ');
  1136. if (first_colon && (!first_space || first_colon < first_space))
  1137. return parse_crashkernel_mem(ck_cmdline, system_ram,
  1138. crash_size, crash_base);
  1139. return parse_crashkernel_simple(ck_cmdline, crash_size, crash_base);
  1140. }
  1141. /*
  1142. * That function is the entry point for command line parsing and should be
  1143. * called from the arch-specific code.
  1144. */
  1145. int __init parse_crashkernel(char *cmdline,
  1146. unsigned long long system_ram,
  1147. unsigned long long *crash_size,
  1148. unsigned long long *crash_base)
  1149. {
  1150. return __parse_crashkernel(cmdline, system_ram, crash_size, crash_base,
  1151. "crashkernel=", NULL);
  1152. }
  1153. int __init parse_crashkernel_high(char *cmdline,
  1154. unsigned long long system_ram,
  1155. unsigned long long *crash_size,
  1156. unsigned long long *crash_base)
  1157. {
  1158. return __parse_crashkernel(cmdline, system_ram, crash_size, crash_base,
  1159. "crashkernel=", suffix_tbl[SUFFIX_HIGH]);
  1160. }
  1161. int __init parse_crashkernel_low(char *cmdline,
  1162. unsigned long long system_ram,
  1163. unsigned long long *crash_size,
  1164. unsigned long long *crash_base)
  1165. {
  1166. return __parse_crashkernel(cmdline, system_ram, crash_size, crash_base,
  1167. "crashkernel=", suffix_tbl[SUFFIX_LOW]);
  1168. }
  1169. static void update_vmcoreinfo_note(void)
  1170. {
  1171. u32 *buf = vmcoreinfo_note;
  1172. if (!vmcoreinfo_size)
  1173. return;
  1174. buf = append_elf_note(buf, VMCOREINFO_NOTE_NAME, 0, vmcoreinfo_data,
  1175. vmcoreinfo_size);
  1176. final_note(buf);
  1177. }
  1178. void crash_save_vmcoreinfo(void)
  1179. {
  1180. vmcoreinfo_append_str("CRASHTIME=%ld\n", get_seconds());
  1181. update_vmcoreinfo_note();
  1182. }
  1183. void vmcoreinfo_append_str(const char *fmt, ...)
  1184. {
  1185. va_list args;
  1186. char buf[0x50];
  1187. size_t r;
  1188. va_start(args, fmt);
  1189. r = vscnprintf(buf, sizeof(buf), fmt, args);
  1190. va_end(args);
  1191. r = min(r, vmcoreinfo_max_size - vmcoreinfo_size);
  1192. memcpy(&vmcoreinfo_data[vmcoreinfo_size], buf, r);
  1193. vmcoreinfo_size += r;
  1194. }
  1195. /*
  1196. * provide an empty default implementation here -- architecture
  1197. * code may override this
  1198. */
  1199. void __weak arch_crash_save_vmcoreinfo(void)
  1200. {}
  1201. phys_addr_t __weak paddr_vmcoreinfo_note(void)
  1202. {
  1203. return __pa((unsigned long)(char *)&vmcoreinfo_note);
  1204. }
  1205. static int __init crash_save_vmcoreinfo_init(void)
  1206. {
  1207. VMCOREINFO_OSRELEASE(init_uts_ns.name.release);
  1208. VMCOREINFO_PAGESIZE(PAGE_SIZE);
  1209. VMCOREINFO_SYMBOL(init_uts_ns);
  1210. VMCOREINFO_SYMBOL(node_online_map);
  1211. #ifdef CONFIG_MMU
  1212. VMCOREINFO_SYMBOL(swapper_pg_dir);
  1213. #endif
  1214. VMCOREINFO_SYMBOL(_stext);
  1215. VMCOREINFO_SYMBOL(vmap_area_list);
  1216. #ifndef CONFIG_NEED_MULTIPLE_NODES
  1217. VMCOREINFO_SYMBOL(mem_map);
  1218. VMCOREINFO_SYMBOL(contig_page_data);
  1219. #endif
  1220. #ifdef CONFIG_SPARSEMEM
  1221. VMCOREINFO_SYMBOL(mem_section);
  1222. VMCOREINFO_LENGTH(mem_section, NR_SECTION_ROOTS);
  1223. VMCOREINFO_STRUCT_SIZE(mem_section);
  1224. VMCOREINFO_OFFSET(mem_section, section_mem_map);
  1225. #endif
  1226. VMCOREINFO_STRUCT_SIZE(page);
  1227. VMCOREINFO_STRUCT_SIZE(pglist_data);
  1228. VMCOREINFO_STRUCT_SIZE(zone);
  1229. VMCOREINFO_STRUCT_SIZE(free_area);
  1230. VMCOREINFO_STRUCT_SIZE(list_head);
  1231. VMCOREINFO_SIZE(nodemask_t);
  1232. VMCOREINFO_OFFSET(page, flags);
  1233. VMCOREINFO_OFFSET(page, _refcount);
  1234. VMCOREINFO_OFFSET(page, mapping);
  1235. VMCOREINFO_OFFSET(page, lru);
  1236. VMCOREINFO_OFFSET(page, _mapcount);
  1237. VMCOREINFO_OFFSET(page, private);
  1238. VMCOREINFO_OFFSET(page, compound_dtor);
  1239. VMCOREINFO_OFFSET(page, compound_order);
  1240. VMCOREINFO_OFFSET(page, compound_head);
  1241. VMCOREINFO_OFFSET(pglist_data, node_zones);
  1242. VMCOREINFO_OFFSET(pglist_data, nr_zones);
  1243. #ifdef CONFIG_FLAT_NODE_MEM_MAP
  1244. VMCOREINFO_OFFSET(pglist_data, node_mem_map);
  1245. #endif
  1246. VMCOREINFO_OFFSET(pglist_data, node_start_pfn);
  1247. VMCOREINFO_OFFSET(pglist_data, node_spanned_pages);
  1248. VMCOREINFO_OFFSET(pglist_data, node_id);
  1249. VMCOREINFO_OFFSET(zone, free_area);
  1250. VMCOREINFO_OFFSET(zone, vm_stat);
  1251. VMCOREINFO_OFFSET(zone, spanned_pages);
  1252. VMCOREINFO_OFFSET(free_area, free_list);
  1253. VMCOREINFO_OFFSET(list_head, next);
  1254. VMCOREINFO_OFFSET(list_head, prev);
  1255. VMCOREINFO_OFFSET(vmap_area, va_start);
  1256. VMCOREINFO_OFFSET(vmap_area, list);
  1257. VMCOREINFO_LENGTH(zone.free_area, MAX_ORDER);
  1258. log_buf_kexec_setup();
  1259. VMCOREINFO_LENGTH(free_area.free_list, MIGRATE_TYPES);
  1260. VMCOREINFO_NUMBER(NR_FREE_PAGES);
  1261. VMCOREINFO_NUMBER(PG_lru);
  1262. VMCOREINFO_NUMBER(PG_private);
  1263. VMCOREINFO_NUMBER(PG_swapcache);
  1264. VMCOREINFO_NUMBER(PG_slab);
  1265. #ifdef CONFIG_MEMORY_FAILURE
  1266. VMCOREINFO_NUMBER(PG_hwpoison);
  1267. #endif
  1268. VMCOREINFO_NUMBER(PG_head_mask);
  1269. VMCOREINFO_NUMBER(PAGE_BUDDY_MAPCOUNT_VALUE);
  1270. #ifdef CONFIG_HUGETLB_PAGE
  1271. VMCOREINFO_NUMBER(HUGETLB_PAGE_DTOR);
  1272. #endif
  1273. arch_crash_save_vmcoreinfo();
  1274. update_vmcoreinfo_note();
  1275. return 0;
  1276. }
  1277. subsys_initcall(crash_save_vmcoreinfo_init);
  1278. /*
  1279. * Move into place and start executing a preloaded standalone
  1280. * executable. If nothing was preloaded return an error.
  1281. */
  1282. int kernel_kexec(void)
  1283. {
  1284. int error = 0;
  1285. if (!mutex_trylock(&kexec_mutex))
  1286. return -EBUSY;
  1287. if (!kexec_image) {
  1288. error = -EINVAL;
  1289. goto Unlock;
  1290. }
  1291. #ifdef CONFIG_KEXEC_JUMP
  1292. if (kexec_image->preserve_context) {
  1293. lock_system_sleep();
  1294. pm_prepare_console();
  1295. error = freeze_processes();
  1296. if (error) {
  1297. error = -EBUSY;
  1298. goto Restore_console;
  1299. }
  1300. suspend_console();
  1301. error = dpm_suspend_start(PMSG_FREEZE);
  1302. if (error)
  1303. goto Resume_console;
  1304. /* At this point, dpm_suspend_start() has been called,
  1305. * but *not* dpm_suspend_end(). We *must* call
  1306. * dpm_suspend_end() now. Otherwise, drivers for
  1307. * some devices (e.g. interrupt controllers) become
  1308. * desynchronized with the actual state of the
  1309. * hardware at resume time, and evil weirdness ensues.
  1310. */
  1311. error = dpm_suspend_end(PMSG_FREEZE);
  1312. if (error)
  1313. goto Resume_devices;
  1314. error = disable_nonboot_cpus();
  1315. if (error)
  1316. goto Enable_cpus;
  1317. local_irq_disable();
  1318. error = syscore_suspend();
  1319. if (error)
  1320. goto Enable_irqs;
  1321. } else
  1322. #endif
  1323. {
  1324. kexec_in_progress = true;
  1325. kernel_restart_prepare(NULL);
  1326. migrate_to_reboot_cpu();
  1327. /*
  1328. * migrate_to_reboot_cpu() disables CPU hotplug assuming that
  1329. * no further code needs to use CPU hotplug (which is true in
  1330. * the reboot case). However, the kexec path depends on using
  1331. * CPU hotplug again; so re-enable it here.
  1332. */
  1333. cpu_hotplug_enable();
  1334. pr_emerg("Starting new kernel\n");
  1335. machine_shutdown();
  1336. }
  1337. machine_kexec(kexec_image);
  1338. #ifdef CONFIG_KEXEC_JUMP
  1339. if (kexec_image->preserve_context) {
  1340. syscore_resume();
  1341. Enable_irqs:
  1342. local_irq_enable();
  1343. Enable_cpus:
  1344. enable_nonboot_cpus();
  1345. dpm_resume_start(PMSG_RESTORE);
  1346. Resume_devices:
  1347. dpm_resume_end(PMSG_RESTORE);
  1348. Resume_console:
  1349. resume_console();
  1350. thaw_processes();
  1351. Restore_console:
  1352. pm_restore_console();
  1353. unlock_system_sleep();
  1354. }
  1355. #endif
  1356. Unlock:
  1357. mutex_unlock(&kexec_mutex);
  1358. return error;
  1359. }
  1360. /*
  1361. * Protection mechanism for crashkernel reserved memory after
  1362. * the kdump kernel is loaded.
  1363. *
  1364. * Provide an empty default implementation here -- architecture
  1365. * code may override this
  1366. */
  1367. void __weak arch_kexec_protect_crashkres(void)
  1368. {}
  1369. void __weak arch_kexec_unprotect_crashkres(void)
  1370. {}