kexec.c 43 KB

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