kexec.c 37 KB

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