kexec.c 42 KB

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