kexec.c 36 KB

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