kexec.c 39 KB

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