PageRenderTime 1401ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/kernel/kexec.c

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