kexec.c 42 KB

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