kexec.c 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497
  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 if it's
  653. * gfp_flags honor the ones passed in.
  654. */
  655. if (!(gfp_mask & __GFP_HIGHMEM) &&
  656. PageHighMem(old_page)) {
  657. kimage_free_pages(old_page);
  658. continue;
  659. }
  660. addr = old_addr;
  661. page = old_page;
  662. break;
  663. }
  664. else {
  665. /* Place the page on the destination list I
  666. * will use it later.
  667. */
  668. list_add(&page->lru, &image->dest_pages);
  669. }
  670. }
  671. return page;
  672. }
  673. static int kimage_load_normal_segment(struct kimage *image,
  674. struct kexec_segment *segment)
  675. {
  676. unsigned long maddr;
  677. unsigned long ubytes, mbytes;
  678. int result;
  679. unsigned char __user *buf;
  680. result = 0;
  681. buf = segment->buf;
  682. ubytes = segment->bufsz;
  683. mbytes = segment->memsz;
  684. maddr = segment->mem;
  685. result = kimage_set_destination(image, maddr);
  686. if (result < 0)
  687. goto out;
  688. while (mbytes) {
  689. struct page *page;
  690. char *ptr;
  691. size_t uchunk, mchunk;
  692. page = kimage_alloc_page(image, GFP_HIGHUSER, maddr);
  693. if (!page) {
  694. result = -ENOMEM;
  695. goto out;
  696. }
  697. result = kimage_add_page(image, page_to_pfn(page)
  698. << PAGE_SHIFT);
  699. if (result < 0)
  700. goto out;
  701. ptr = kmap(page);
  702. /* Start with a clear page */
  703. memset(ptr, 0, PAGE_SIZE);
  704. ptr += maddr & ~PAGE_MASK;
  705. mchunk = PAGE_SIZE - (maddr & ~PAGE_MASK);
  706. if (mchunk > mbytes)
  707. mchunk = mbytes;
  708. uchunk = mchunk;
  709. if (uchunk > ubytes)
  710. uchunk = ubytes;
  711. result = copy_from_user(ptr, buf, uchunk);
  712. kunmap(page);
  713. if (result) {
  714. result = (result < 0) ? result : -EIO;
  715. goto out;
  716. }
  717. ubytes -= uchunk;
  718. maddr += mchunk;
  719. buf += mchunk;
  720. mbytes -= mchunk;
  721. }
  722. out:
  723. return result;
  724. }
  725. static int kimage_load_crash_segment(struct kimage *image,
  726. struct kexec_segment *segment)
  727. {
  728. /* For crash dumps kernels we simply copy the data from
  729. * user space to it's destination.
  730. * We do things a page at a time for the sake of kmap.
  731. */
  732. unsigned long maddr;
  733. unsigned long ubytes, mbytes;
  734. int result;
  735. unsigned char __user *buf;
  736. result = 0;
  737. buf = segment->buf;
  738. ubytes = segment->bufsz;
  739. mbytes = segment->memsz;
  740. maddr = segment->mem;
  741. while (mbytes) {
  742. struct page *page;
  743. char *ptr;
  744. size_t uchunk, mchunk;
  745. page = pfn_to_page(maddr >> PAGE_SHIFT);
  746. if (!page) {
  747. result = -ENOMEM;
  748. goto out;
  749. }
  750. ptr = kmap(page);
  751. ptr += maddr & ~PAGE_MASK;
  752. mchunk = PAGE_SIZE - (maddr & ~PAGE_MASK);
  753. if (mchunk > mbytes)
  754. mchunk = mbytes;
  755. uchunk = mchunk;
  756. if (uchunk > ubytes) {
  757. uchunk = ubytes;
  758. /* Zero the trailing part of the page */
  759. memset(ptr + uchunk, 0, mchunk - uchunk);
  760. }
  761. result = copy_from_user(ptr, buf, uchunk);
  762. kexec_flush_icache_page(page);
  763. kunmap(page);
  764. if (result) {
  765. result = (result < 0) ? result : -EIO;
  766. goto out;
  767. }
  768. ubytes -= uchunk;
  769. maddr += mchunk;
  770. buf += mchunk;
  771. mbytes -= mchunk;
  772. }
  773. out:
  774. return result;
  775. }
  776. static int kimage_load_segment(struct kimage *image,
  777. struct kexec_segment *segment)
  778. {
  779. int result = -ENOMEM;
  780. switch (image->type) {
  781. case KEXEC_TYPE_DEFAULT:
  782. result = kimage_load_normal_segment(image, segment);
  783. break;
  784. case KEXEC_TYPE_CRASH:
  785. result = kimage_load_crash_segment(image, segment);
  786. break;
  787. }
  788. return result;
  789. }
  790. /*
  791. * Exec Kernel system call: for obvious reasons only root may call it.
  792. *
  793. * This call breaks up into three pieces.
  794. * - A generic part which loads the new kernel from the current
  795. * address space, and very carefully places the data in the
  796. * allocated pages.
  797. *
  798. * - A generic part that interacts with the kernel and tells all of
  799. * the devices to shut down. Preventing on-going dmas, and placing
  800. * the devices in a consistent state so a later kernel can
  801. * reinitialize them.
  802. *
  803. * - A machine specific part that includes the syscall number
  804. * and the copies the image to it's final destination. And
  805. * jumps into the image at entry.
  806. *
  807. * kexec does not sync, or unmount filesystems so if you need
  808. * that to happen you need to do that yourself.
  809. */
  810. struct kimage *kexec_image;
  811. struct kimage *kexec_crash_image;
  812. static DEFINE_MUTEX(kexec_mutex);
  813. asmlinkage long sys_kexec_load(unsigned long entry, unsigned long nr_segments,
  814. struct kexec_segment __user *segments,
  815. unsigned long flags)
  816. {
  817. struct kimage **dest_image, *image;
  818. int result;
  819. /* We only trust the superuser with rebooting the system. */
  820. if (!capable(CAP_SYS_BOOT))
  821. return -EPERM;
  822. /*
  823. * Verify we have a legal set of flags
  824. * This leaves us room for future extensions.
  825. */
  826. if ((flags & KEXEC_FLAGS) != (flags & ~KEXEC_ARCH_MASK))
  827. return -EINVAL;
  828. /* Verify we are on the appropriate architecture */
  829. if (((flags & KEXEC_ARCH_MASK) != KEXEC_ARCH) &&
  830. ((flags & KEXEC_ARCH_MASK) != KEXEC_ARCH_DEFAULT))
  831. return -EINVAL;
  832. /* Put an artificial cap on the number
  833. * of segments passed to kexec_load.
  834. */
  835. if (nr_segments > KEXEC_SEGMENT_MAX)
  836. return -EINVAL;
  837. image = NULL;
  838. result = 0;
  839. /* Because we write directly to the reserved memory
  840. * region when loading crash kernels we need a mutex here to
  841. * prevent multiple crash kernels from attempting to load
  842. * simultaneously, and to prevent a crash kernel from loading
  843. * over the top of a in use crash kernel.
  844. *
  845. * KISS: always take the mutex.
  846. */
  847. if (!mutex_trylock(&kexec_mutex))
  848. return -EBUSY;
  849. dest_image = &kexec_image;
  850. if (flags & KEXEC_ON_CRASH)
  851. dest_image = &kexec_crash_image;
  852. if (nr_segments > 0) {
  853. unsigned long i;
  854. /* Loading another kernel to reboot into */
  855. if ((flags & KEXEC_ON_CRASH) == 0)
  856. result = kimage_normal_alloc(&image, entry,
  857. nr_segments, segments);
  858. /* Loading another kernel to switch to if this one crashes */
  859. else if (flags & KEXEC_ON_CRASH) {
  860. /* Free any current crash dump kernel before
  861. * we corrupt it.
  862. */
  863. kimage_free(xchg(&kexec_crash_image, NULL));
  864. result = kimage_crash_alloc(&image, entry,
  865. nr_segments, segments);
  866. }
  867. if (result)
  868. goto out;
  869. if (flags & KEXEC_PRESERVE_CONTEXT)
  870. image->preserve_context = 1;
  871. result = machine_kexec_prepare(image);
  872. if (result)
  873. goto out;
  874. for (i = 0; i < nr_segments; i++) {
  875. result = kimage_load_segment(image, &image->segment[i]);
  876. if (result)
  877. goto out;
  878. }
  879. kimage_terminate(image);
  880. }
  881. /* Install the new kernel, and Uninstall the old */
  882. image = xchg(dest_image, image);
  883. out:
  884. mutex_unlock(&kexec_mutex);
  885. kimage_free(image);
  886. return result;
  887. }
  888. #ifdef CONFIG_COMPAT
  889. asmlinkage long compat_sys_kexec_load(unsigned long entry,
  890. unsigned long nr_segments,
  891. struct compat_kexec_segment __user *segments,
  892. unsigned long flags)
  893. {
  894. struct compat_kexec_segment in;
  895. struct kexec_segment out, __user *ksegments;
  896. unsigned long i, result;
  897. /* Don't allow clients that don't understand the native
  898. * architecture to do anything.
  899. */
  900. if ((flags & KEXEC_ARCH_MASK) == KEXEC_ARCH_DEFAULT)
  901. return -EINVAL;
  902. if (nr_segments > KEXEC_SEGMENT_MAX)
  903. return -EINVAL;
  904. ksegments = compat_alloc_user_space(nr_segments * sizeof(out));
  905. for (i=0; i < nr_segments; i++) {
  906. result = copy_from_user(&in, &segments[i], sizeof(in));
  907. if (result)
  908. return -EFAULT;
  909. out.buf = compat_ptr(in.buf);
  910. out.bufsz = in.bufsz;
  911. out.mem = in.mem;
  912. out.memsz = in.memsz;
  913. result = copy_to_user(&ksegments[i], &out, sizeof(out));
  914. if (result)
  915. return -EFAULT;
  916. }
  917. return sys_kexec_load(entry, nr_segments, ksegments, flags);
  918. }
  919. #endif
  920. void crash_kexec(struct pt_regs *regs)
  921. {
  922. /* Take the kexec_mutex here to prevent sys_kexec_load
  923. * running on one cpu from replacing the crash kernel
  924. * we are using after a panic on a different cpu.
  925. *
  926. * If the crash kernel was not located in a fixed area
  927. * of memory the xchg(&kexec_crash_image) would be
  928. * sufficient. But since I reuse the memory...
  929. */
  930. if (mutex_trylock(&kexec_mutex)) {
  931. if (kexec_crash_image) {
  932. struct pt_regs fixed_regs;
  933. crash_setup_regs(&fixed_regs, regs);
  934. crash_save_vmcoreinfo();
  935. machine_crash_shutdown(&fixed_regs);
  936. machine_kexec(kexec_crash_image);
  937. }
  938. mutex_unlock(&kexec_mutex);
  939. }
  940. }
  941. static u32 *append_elf_note(u32 *buf, char *name, unsigned type, void *data,
  942. size_t data_len)
  943. {
  944. struct elf_note note;
  945. note.n_namesz = strlen(name) + 1;
  946. note.n_descsz = data_len;
  947. note.n_type = type;
  948. memcpy(buf, &note, sizeof(note));
  949. buf += (sizeof(note) + 3)/4;
  950. memcpy(buf, name, note.n_namesz);
  951. buf += (note.n_namesz + 3)/4;
  952. memcpy(buf, data, note.n_descsz);
  953. buf += (note.n_descsz + 3)/4;
  954. return buf;
  955. }
  956. static void final_note(u32 *buf)
  957. {
  958. struct elf_note note;
  959. note.n_namesz = 0;
  960. note.n_descsz = 0;
  961. note.n_type = 0;
  962. memcpy(buf, &note, sizeof(note));
  963. }
  964. void crash_save_cpu(struct pt_regs *regs, int cpu)
  965. {
  966. struct elf_prstatus prstatus;
  967. u32 *buf;
  968. if ((cpu < 0) || (cpu >= NR_CPUS))
  969. return;
  970. /* Using ELF notes here is opportunistic.
  971. * I need a well defined structure format
  972. * for the data I pass, and I need tags
  973. * on the data to indicate what information I have
  974. * squirrelled away. ELF notes happen to provide
  975. * all of that, so there is no need to invent something new.
  976. */
  977. buf = (u32*)per_cpu_ptr(crash_notes, cpu);
  978. if (!buf)
  979. return;
  980. memset(&prstatus, 0, sizeof(prstatus));
  981. prstatus.pr_pid = current->pid;
  982. elf_core_copy_regs(&prstatus.pr_reg, regs);
  983. buf = append_elf_note(buf, KEXEC_CORE_NOTE_NAME, NT_PRSTATUS,
  984. &prstatus, sizeof(prstatus));
  985. final_note(buf);
  986. }
  987. static int __init crash_notes_memory_init(void)
  988. {
  989. /* Allocate memory for saving cpu registers. */
  990. crash_notes = alloc_percpu(note_buf_t);
  991. if (!crash_notes) {
  992. printk("Kexec: Memory allocation for saving cpu register"
  993. " states failed\n");
  994. return -ENOMEM;
  995. }
  996. return 0;
  997. }
  998. module_init(crash_notes_memory_init)
  999. /*
  1000. * parsing the "crashkernel" commandline
  1001. *
  1002. * this code is intended to be called from architecture specific code
  1003. */
  1004. /*
  1005. * This function parses command lines in the format
  1006. *
  1007. * crashkernel=ramsize-range:size[,...][@offset]
  1008. *
  1009. * The function returns 0 on success and -EINVAL on failure.
  1010. */
  1011. static int __init parse_crashkernel_mem(char *cmdline,
  1012. unsigned long long system_ram,
  1013. unsigned long long *crash_size,
  1014. unsigned long long *crash_base)
  1015. {
  1016. char *cur = cmdline, *tmp;
  1017. /* for each entry of the comma-separated list */
  1018. do {
  1019. unsigned long long start, end = ULLONG_MAX, size;
  1020. /* get the start of the range */
  1021. start = memparse(cur, &tmp);
  1022. if (cur == tmp) {
  1023. pr_warning("crashkernel: Memory value expected\n");
  1024. return -EINVAL;
  1025. }
  1026. cur = tmp;
  1027. if (*cur != '-') {
  1028. pr_warning("crashkernel: '-' expected\n");
  1029. return -EINVAL;
  1030. }
  1031. cur++;
  1032. /* if no ':' is here, than we read the end */
  1033. if (*cur != ':') {
  1034. end = memparse(cur, &tmp);
  1035. if (cur == tmp) {
  1036. pr_warning("crashkernel: Memory "
  1037. "value expected\n");
  1038. return -EINVAL;
  1039. }
  1040. cur = tmp;
  1041. if (end <= start) {
  1042. pr_warning("crashkernel: end <= start\n");
  1043. return -EINVAL;
  1044. }
  1045. }
  1046. if (*cur != ':') {
  1047. pr_warning("crashkernel: ':' expected\n");
  1048. return -EINVAL;
  1049. }
  1050. cur++;
  1051. size = memparse(cur, &tmp);
  1052. if (cur == tmp) {
  1053. pr_warning("Memory value expected\n");
  1054. return -EINVAL;
  1055. }
  1056. cur = tmp;
  1057. if (size >= system_ram) {
  1058. pr_warning("crashkernel: invalid size\n");
  1059. return -EINVAL;
  1060. }
  1061. /* match ? */
  1062. if (system_ram >= start && system_ram < end) {
  1063. *crash_size = size;
  1064. break;
  1065. }
  1066. } while (*cur++ == ',');
  1067. if (*crash_size > 0) {
  1068. while (*cur != ' ' && *cur != '@')
  1069. cur++;
  1070. if (*cur == '@') {
  1071. cur++;
  1072. *crash_base = memparse(cur, &tmp);
  1073. if (cur == tmp) {
  1074. pr_warning("Memory value expected "
  1075. "after '@'\n");
  1076. return -EINVAL;
  1077. }
  1078. }
  1079. }
  1080. return 0;
  1081. }
  1082. /*
  1083. * That function parses "simple" (old) crashkernel command lines like
  1084. *
  1085. * crashkernel=size[@offset]
  1086. *
  1087. * It returns 0 on success and -EINVAL on failure.
  1088. */
  1089. static int __init parse_crashkernel_simple(char *cmdline,
  1090. unsigned long long *crash_size,
  1091. unsigned long long *crash_base)
  1092. {
  1093. char *cur = cmdline;
  1094. *crash_size = memparse(cmdline, &cur);
  1095. if (cmdline == cur) {
  1096. pr_warning("crashkernel: memory value expected\n");
  1097. return -EINVAL;
  1098. }
  1099. if (*cur == '@')
  1100. *crash_base = memparse(cur+1, &cur);
  1101. return 0;
  1102. }
  1103. /*
  1104. * That function is the entry point for command line parsing and should be
  1105. * called from the arch-specific code.
  1106. */
  1107. int __init parse_crashkernel(char *cmdline,
  1108. unsigned long long system_ram,
  1109. unsigned long long *crash_size,
  1110. unsigned long long *crash_base)
  1111. {
  1112. char *p = cmdline, *ck_cmdline = NULL;
  1113. char *first_colon, *first_space;
  1114. BUG_ON(!crash_size || !crash_base);
  1115. *crash_size = 0;
  1116. *crash_base = 0;
  1117. /* find crashkernel and use the last one if there are more */
  1118. p = strstr(p, "crashkernel=");
  1119. while (p) {
  1120. ck_cmdline = p;
  1121. p = strstr(p+1, "crashkernel=");
  1122. }
  1123. if (!ck_cmdline)
  1124. return -EINVAL;
  1125. ck_cmdline += 12; /* strlen("crashkernel=") */
  1126. /*
  1127. * if the commandline contains a ':', then that's the extended
  1128. * syntax -- if not, it must be the classic syntax
  1129. */
  1130. first_colon = strchr(ck_cmdline, ':');
  1131. first_space = strchr(ck_cmdline, ' ');
  1132. if (first_colon && (!first_space || first_colon < first_space))
  1133. return parse_crashkernel_mem(ck_cmdline, system_ram,
  1134. crash_size, crash_base);
  1135. else
  1136. return parse_crashkernel_simple(ck_cmdline, crash_size,
  1137. crash_base);
  1138. return 0;
  1139. }
  1140. void crash_save_vmcoreinfo(void)
  1141. {
  1142. u32 *buf;
  1143. if (!vmcoreinfo_size)
  1144. return;
  1145. vmcoreinfo_append_str("CRASHTIME=%ld", get_seconds());
  1146. buf = (u32 *)vmcoreinfo_note;
  1147. buf = append_elf_note(buf, VMCOREINFO_NOTE_NAME, 0, vmcoreinfo_data,
  1148. vmcoreinfo_size);
  1149. final_note(buf);
  1150. }
  1151. void vmcoreinfo_append_str(const char *fmt, ...)
  1152. {
  1153. va_list args;
  1154. char buf[0x50];
  1155. int r;
  1156. va_start(args, fmt);
  1157. r = vsnprintf(buf, sizeof(buf), fmt, args);
  1158. va_end(args);
  1159. if (r + vmcoreinfo_size > vmcoreinfo_max_size)
  1160. r = vmcoreinfo_max_size - vmcoreinfo_size;
  1161. memcpy(&vmcoreinfo_data[vmcoreinfo_size], buf, r);
  1162. vmcoreinfo_size += r;
  1163. }
  1164. /*
  1165. * provide an empty default implementation here -- architecture
  1166. * code may override this
  1167. */
  1168. void __attribute__ ((weak)) arch_crash_save_vmcoreinfo(void)
  1169. {}
  1170. unsigned long __attribute__ ((weak)) paddr_vmcoreinfo_note(void)
  1171. {
  1172. return __pa((unsigned long)(char *)&vmcoreinfo_note);
  1173. }
  1174. static int __init crash_save_vmcoreinfo_init(void)
  1175. {
  1176. VMCOREINFO_OSRELEASE(init_uts_ns.name.release);
  1177. VMCOREINFO_PAGESIZE(PAGE_SIZE);
  1178. VMCOREINFO_SYMBOL(init_uts_ns);
  1179. VMCOREINFO_SYMBOL(node_online_map);
  1180. VMCOREINFO_SYMBOL(swapper_pg_dir);
  1181. VMCOREINFO_SYMBOL(_stext);
  1182. #ifndef CONFIG_NEED_MULTIPLE_NODES
  1183. VMCOREINFO_SYMBOL(mem_map);
  1184. VMCOREINFO_SYMBOL(contig_page_data);
  1185. #endif
  1186. #ifdef CONFIG_SPARSEMEM
  1187. VMCOREINFO_SYMBOL(mem_section);
  1188. VMCOREINFO_LENGTH(mem_section, NR_SECTION_ROOTS);
  1189. VMCOREINFO_STRUCT_SIZE(mem_section);
  1190. VMCOREINFO_OFFSET(mem_section, section_mem_map);
  1191. #endif
  1192. VMCOREINFO_STRUCT_SIZE(page);
  1193. VMCOREINFO_STRUCT_SIZE(pglist_data);
  1194. VMCOREINFO_STRUCT_SIZE(zone);
  1195. VMCOREINFO_STRUCT_SIZE(free_area);
  1196. VMCOREINFO_STRUCT_SIZE(list_head);
  1197. VMCOREINFO_SIZE(nodemask_t);
  1198. VMCOREINFO_OFFSET(page, flags);
  1199. VMCOREINFO_OFFSET(page, _count);
  1200. VMCOREINFO_OFFSET(page, mapping);
  1201. VMCOREINFO_OFFSET(page, lru);
  1202. VMCOREINFO_OFFSET(pglist_data, node_zones);
  1203. VMCOREINFO_OFFSET(pglist_data, nr_zones);
  1204. #ifdef CONFIG_FLAT_NODE_MEM_MAP
  1205. VMCOREINFO_OFFSET(pglist_data, node_mem_map);
  1206. #endif
  1207. VMCOREINFO_OFFSET(pglist_data, node_start_pfn);
  1208. VMCOREINFO_OFFSET(pglist_data, node_spanned_pages);
  1209. VMCOREINFO_OFFSET(pglist_data, node_id);
  1210. VMCOREINFO_OFFSET(zone, free_area);
  1211. VMCOREINFO_OFFSET(zone, vm_stat);
  1212. VMCOREINFO_OFFSET(zone, spanned_pages);
  1213. VMCOREINFO_OFFSET(free_area, free_list);
  1214. VMCOREINFO_OFFSET(list_head, next);
  1215. VMCOREINFO_OFFSET(list_head, prev);
  1216. VMCOREINFO_LENGTH(zone.free_area, MAX_ORDER);
  1217. VMCOREINFO_LENGTH(free_area.free_list, MIGRATE_TYPES);
  1218. VMCOREINFO_NUMBER(NR_FREE_PAGES);
  1219. VMCOREINFO_NUMBER(PG_lru);
  1220. VMCOREINFO_NUMBER(PG_private);
  1221. VMCOREINFO_NUMBER(PG_swapcache);
  1222. arch_crash_save_vmcoreinfo();
  1223. return 0;
  1224. }
  1225. module_init(crash_save_vmcoreinfo_init)
  1226. /*
  1227. * Move into place and start executing a preloaded standalone
  1228. * executable. If nothing was preloaded return an error.
  1229. */
  1230. int kernel_kexec(void)
  1231. {
  1232. int error = 0;
  1233. if (!mutex_trylock(&kexec_mutex))
  1234. return -EBUSY;
  1235. if (!kexec_image) {
  1236. error = -EINVAL;
  1237. goto Unlock;
  1238. }
  1239. #ifdef CONFIG_KEXEC_JUMP
  1240. if (kexec_image->preserve_context) {
  1241. mutex_lock(&pm_mutex);
  1242. pm_prepare_console();
  1243. error = freeze_processes();
  1244. if (error) {
  1245. error = -EBUSY;
  1246. goto Restore_console;
  1247. }
  1248. suspend_console();
  1249. error = device_suspend(PMSG_FREEZE);
  1250. if (error)
  1251. goto Resume_console;
  1252. error = disable_nonboot_cpus();
  1253. if (error)
  1254. goto Resume_devices;
  1255. device_pm_lock();
  1256. local_irq_disable();
  1257. /* At this point, device_suspend() has been called,
  1258. * but *not* device_power_down(). We *must*
  1259. * device_power_down() now. Otherwise, drivers for
  1260. * some devices (e.g. interrupt controllers) become
  1261. * desynchronized with the actual state of the
  1262. * hardware at resume time, and evil weirdness ensues.
  1263. */
  1264. error = device_power_down(PMSG_FREEZE);
  1265. if (error)
  1266. goto Enable_irqs;
  1267. } else
  1268. #endif
  1269. {
  1270. kernel_restart_prepare(NULL);
  1271. printk(KERN_EMERG "Starting new kernel\n");
  1272. machine_shutdown();
  1273. }
  1274. machine_kexec(kexec_image);
  1275. #ifdef CONFIG_KEXEC_JUMP
  1276. if (kexec_image->preserve_context) {
  1277. device_power_up(PMSG_RESTORE);
  1278. Enable_irqs:
  1279. local_irq_enable();
  1280. device_pm_unlock();
  1281. enable_nonboot_cpus();
  1282. Resume_devices:
  1283. device_resume(PMSG_RESTORE);
  1284. Resume_console:
  1285. resume_console();
  1286. thaw_processes();
  1287. Restore_console:
  1288. pm_restore_console();
  1289. mutex_unlock(&pm_mutex);
  1290. }
  1291. #endif
  1292. Unlock:
  1293. mutex_unlock(&kexec_mutex);
  1294. return error;
  1295. }