kexec.c 41 KB

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