kexec.c 40 KB

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