kexec.c 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722
  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 <linux/hugetlb.h>
  36. #include <asm/page.h>
  37. #include <asm/uaccess.h>
  38. #include <asm/io.h>
  39. #include <asm/sections.h>
  40. /* Per cpu memory for storing cpu states in case of system crash. */
  41. note_buf_t __percpu *crash_notes;
  42. /* vmcoreinfo stuff */
  43. static unsigned char vmcoreinfo_data[VMCOREINFO_BYTES];
  44. u32 vmcoreinfo_note[VMCOREINFO_NOTE_SIZE/4];
  45. size_t vmcoreinfo_size;
  46. size_t vmcoreinfo_max_size = sizeof(vmcoreinfo_data);
  47. /* Flag to indicate we are going to kexec a new kernel */
  48. bool kexec_in_progress = false;
  49. /* Location of the reserved area for the crash kernel */
  50. struct resource crashk_res = {
  51. .name = "Crash kernel",
  52. .start = 0,
  53. .end = 0,
  54. .flags = IORESOURCE_BUSY | IORESOURCE_MEM
  55. };
  56. struct resource crashk_low_res = {
  57. .name = "Crash kernel",
  58. .start = 0,
  59. .end = 0,
  60. .flags = IORESOURCE_BUSY | IORESOURCE_MEM
  61. };
  62. int kexec_should_crash(struct task_struct *p)
  63. {
  64. if (in_interrupt() || !p->pid || is_global_init(p) || panic_on_oops)
  65. return 1;
  66. return 0;
  67. }
  68. /*
  69. * When kexec transitions to the new kernel there is a one-to-one
  70. * mapping between physical and virtual addresses. On processors
  71. * where you can disable the MMU this is trivial, and easy. For
  72. * others it is still a simple predictable page table to setup.
  73. *
  74. * In that environment kexec copies the new kernel to its final
  75. * resting place. This means I can only support memory whose
  76. * physical address can fit in an unsigned long. In particular
  77. * addresses where (pfn << PAGE_SHIFT) > ULONG_MAX cannot be handled.
  78. * If the assembly stub has more restrictive requirements
  79. * KEXEC_SOURCE_MEMORY_LIMIT and KEXEC_DEST_MEMORY_LIMIT can be
  80. * defined more restrictively in <asm/kexec.h>.
  81. *
  82. * The code for the transition from the current kernel to the
  83. * the new kernel is placed in the control_code_buffer, whose size
  84. * is given by KEXEC_CONTROL_PAGE_SIZE. In the best case only a single
  85. * page of memory is necessary, but some architectures require more.
  86. * Because this memory must be identity mapped in the transition from
  87. * virtual to physical addresses it must live in the range
  88. * 0 - TASK_SIZE, as only the user space mappings are arbitrarily
  89. * modifiable.
  90. *
  91. * The assembly stub in the control code buffer is passed a linked list
  92. * of descriptor pages detailing the source pages of the new kernel,
  93. * and the destination addresses of those source pages. As this data
  94. * structure is not used in the context of the current OS, it must
  95. * be self-contained.
  96. *
  97. * The code has been made to work with highmem pages and will use a
  98. * destination page in its final resting place (if it happens
  99. * to allocate it). The end product of this is that most of the
  100. * physical address space, and most of RAM can be used.
  101. *
  102. * Future directions include:
  103. * - allocating a page table with the control code buffer identity
  104. * mapped, to simplify machine_kexec and make kexec_on_panic more
  105. * reliable.
  106. */
  107. /*
  108. * KIMAGE_NO_DEST is an impossible destination address..., for
  109. * allocating pages whose destination address we do not care about.
  110. */
  111. #define KIMAGE_NO_DEST (-1UL)
  112. static int kimage_is_destination_range(struct kimage *image,
  113. unsigned long start, unsigned long end);
  114. static struct page *kimage_alloc_page(struct kimage *image,
  115. gfp_t gfp_mask,
  116. unsigned long dest);
  117. static int do_kimage_alloc(struct kimage **rimage, unsigned long entry,
  118. unsigned long nr_segments,
  119. struct kexec_segment __user *segments)
  120. {
  121. size_t segment_bytes;
  122. struct kimage *image;
  123. unsigned long i;
  124. int result;
  125. /* Allocate a controlling structure */
  126. result = -ENOMEM;
  127. image = kzalloc(sizeof(*image), GFP_KERNEL);
  128. if (!image)
  129. goto out;
  130. image->head = 0;
  131. image->entry = &image->head;
  132. image->last_entry = &image->head;
  133. image->control_page = ~0; /* By default this does not apply */
  134. image->start = entry;
  135. image->type = KEXEC_TYPE_DEFAULT;
  136. /* Initialize the list of control pages */
  137. INIT_LIST_HEAD(&image->control_pages);
  138. /* Initialize the list of destination pages */
  139. INIT_LIST_HEAD(&image->dest_pages);
  140. /* Initialize the list of unusable pages */
  141. INIT_LIST_HEAD(&image->unuseable_pages);
  142. /* Read in the segments */
  143. image->nr_segments = nr_segments;
  144. segment_bytes = nr_segments * sizeof(*segments);
  145. result = copy_from_user(image->segment, segments, segment_bytes);
  146. if (result) {
  147. result = -EFAULT;
  148. goto out;
  149. }
  150. /*
  151. * Verify we have good destination addresses. The caller is
  152. * responsible for making certain we don't attempt to load
  153. * the new image into invalid or reserved areas of RAM. This
  154. * just verifies it is an address we can use.
  155. *
  156. * Since the kernel does everything in page size chunks ensure
  157. * the destination addresses are page aligned. Too many
  158. * special cases crop of when we don't do this. The most
  159. * insidious is getting overlapping destination addresses
  160. * simply because addresses are changed to page size
  161. * granularity.
  162. */
  163. result = -EADDRNOTAVAIL;
  164. for (i = 0; i < nr_segments; i++) {
  165. unsigned long mstart, mend;
  166. mstart = image->segment[i].mem;
  167. mend = mstart + image->segment[i].memsz;
  168. if ((mstart & ~PAGE_MASK) || (mend & ~PAGE_MASK))
  169. goto out;
  170. if (mend >= KEXEC_DESTINATION_MEMORY_LIMIT)
  171. goto out;
  172. }
  173. /* Verify our destination addresses do not overlap.
  174. * If we alloed overlapping destination addresses
  175. * through very weird things can happen with no
  176. * easy explanation as one segment stops on another.
  177. */
  178. result = -EINVAL;
  179. for (i = 0; i < nr_segments; i++) {
  180. unsigned long mstart, mend;
  181. unsigned long j;
  182. mstart = image->segment[i].mem;
  183. mend = mstart + image->segment[i].memsz;
  184. for (j = 0; j < i; j++) {
  185. unsigned long pstart, pend;
  186. pstart = image->segment[j].mem;
  187. pend = pstart + image->segment[j].memsz;
  188. /* Do the segments overlap ? */
  189. if ((mend > pstart) && (mstart < pend))
  190. goto out;
  191. }
  192. }
  193. /* Ensure our buffer sizes are strictly less than
  194. * our memory sizes. This should always be the case,
  195. * and it is easier to check up front than to be surprised
  196. * later on.
  197. */
  198. result = -EINVAL;
  199. for (i = 0; i < nr_segments; i++) {
  200. if (image->segment[i].bufsz > image->segment[i].memsz)
  201. goto out;
  202. }
  203. result = 0;
  204. out:
  205. if (result == 0)
  206. *rimage = image;
  207. else
  208. kfree(image);
  209. return result;
  210. }
  211. static void kimage_free_page_list(struct list_head *list);
  212. static int kimage_normal_alloc(struct kimage **rimage, unsigned long entry,
  213. unsigned long nr_segments,
  214. struct kexec_segment __user *segments)
  215. {
  216. int result;
  217. struct kimage *image;
  218. /* Allocate and initialize a controlling structure */
  219. image = NULL;
  220. result = do_kimage_alloc(&image, entry, nr_segments, segments);
  221. if (result)
  222. goto out;
  223. /*
  224. * Find a location for the control code buffer, and add it
  225. * the vector of segments so that it's pages will also be
  226. * counted as destination pages.
  227. */
  228. result = -ENOMEM;
  229. image->control_code_page = kimage_alloc_control_pages(image,
  230. get_order(KEXEC_CONTROL_PAGE_SIZE));
  231. if (!image->control_code_page) {
  232. pr_err("Could not allocate control_code_buffer\n");
  233. goto out_free;
  234. }
  235. image->swap_page = kimage_alloc_control_pages(image, 0);
  236. if (!image->swap_page) {
  237. pr_err("Could not allocate swap buffer\n");
  238. goto out_free;
  239. }
  240. *rimage = image;
  241. return 0;
  242. out_free:
  243. kimage_free_page_list(&image->control_pages);
  244. kfree(image);
  245. out:
  246. return result;
  247. }
  248. static int kimage_crash_alloc(struct kimage **rimage, unsigned long entry,
  249. unsigned long nr_segments,
  250. struct kexec_segment __user *segments)
  251. {
  252. int result;
  253. struct kimage *image;
  254. unsigned long i;
  255. image = NULL;
  256. /* Verify we have a valid entry point */
  257. if ((entry < crashk_res.start) || (entry > crashk_res.end)) {
  258. result = -EADDRNOTAVAIL;
  259. goto out;
  260. }
  261. /* Allocate and initialize a controlling structure */
  262. result = do_kimage_alloc(&image, entry, nr_segments, segments);
  263. if (result)
  264. goto out;
  265. /* Enable the special crash kernel control page
  266. * allocation policy.
  267. */
  268. image->control_page = crashk_res.start;
  269. image->type = KEXEC_TYPE_CRASH;
  270. /*
  271. * Verify we have good destination addresses. Normally
  272. * the caller is responsible for making certain we don't
  273. * attempt to load the new image into invalid or reserved
  274. * areas of RAM. But crash kernels are preloaded into a
  275. * reserved area of ram. We must ensure the addresses
  276. * are in the reserved area otherwise preloading the
  277. * kernel could corrupt things.
  278. */
  279. result = -EADDRNOTAVAIL;
  280. for (i = 0; i < nr_segments; i++) {
  281. unsigned long mstart, mend;
  282. mstart = image->segment[i].mem;
  283. mend = mstart + image->segment[i].memsz - 1;
  284. /* Ensure we are within the crash kernel limits */
  285. if ((mstart < crashk_res.start) || (mend > crashk_res.end))
  286. goto out_free;
  287. }
  288. /*
  289. * Find a location for the control code buffer, and add
  290. * the vector of segments so that it's pages will also be
  291. * counted as destination pages.
  292. */
  293. result = -ENOMEM;
  294. image->control_code_page = kimage_alloc_control_pages(image,
  295. get_order(KEXEC_CONTROL_PAGE_SIZE));
  296. if (!image->control_code_page) {
  297. pr_err("Could not allocate control_code_buffer\n");
  298. goto out_free;
  299. }
  300. *rimage = image;
  301. return 0;
  302. out_free:
  303. kfree(image);
  304. out:
  305. return result;
  306. }
  307. static int kimage_is_destination_range(struct kimage *image,
  308. unsigned long start,
  309. unsigned long end)
  310. {
  311. unsigned long i;
  312. for (i = 0; i < image->nr_segments; i++) {
  313. unsigned long mstart, mend;
  314. mstart = image->segment[i].mem;
  315. mend = mstart + image->segment[i].memsz;
  316. if ((end > mstart) && (start < mend))
  317. return 1;
  318. }
  319. return 0;
  320. }
  321. static struct page *kimage_alloc_pages(gfp_t gfp_mask, unsigned int order)
  322. {
  323. struct page *pages;
  324. pages = alloc_pages(gfp_mask, order);
  325. if (pages) {
  326. unsigned int count, i;
  327. pages->mapping = NULL;
  328. set_page_private(pages, order);
  329. count = 1 << order;
  330. for (i = 0; i < count; i++)
  331. SetPageReserved(pages + i);
  332. }
  333. return pages;
  334. }
  335. static void kimage_free_pages(struct page *page)
  336. {
  337. unsigned int order, count, i;
  338. order = page_private(page);
  339. count = 1 << order;
  340. for (i = 0; i < count; i++)
  341. ClearPageReserved(page + i);
  342. __free_pages(page, order);
  343. }
  344. static void kimage_free_page_list(struct list_head *list)
  345. {
  346. struct list_head *pos, *next;
  347. list_for_each_safe(pos, next, list) {
  348. struct page *page;
  349. page = list_entry(pos, struct page, lru);
  350. list_del(&page->lru);
  351. kimage_free_pages(page);
  352. }
  353. }
  354. static struct page *kimage_alloc_normal_control_pages(struct kimage *image,
  355. unsigned int order)
  356. {
  357. /* Control pages are special, they are the intermediaries
  358. * that are needed while we copy the rest of the pages
  359. * to their final resting place. As such they must
  360. * not conflict with either the destination addresses
  361. * or memory the kernel is already using.
  362. *
  363. * The only case where we really need more than one of
  364. * these are for architectures where we cannot disable
  365. * the MMU and must instead generate an identity mapped
  366. * page table for all of the memory.
  367. *
  368. * At worst this runs in O(N) of the image size.
  369. */
  370. struct list_head extra_pages;
  371. struct page *pages;
  372. unsigned int count;
  373. count = 1 << order;
  374. INIT_LIST_HEAD(&extra_pages);
  375. /* Loop while I can allocate a page and the page allocated
  376. * is a destination page.
  377. */
  378. do {
  379. unsigned long pfn, epfn, addr, eaddr;
  380. pages = kimage_alloc_pages(GFP_KERNEL, order);
  381. if (!pages)
  382. break;
  383. pfn = page_to_pfn(pages);
  384. epfn = pfn + count;
  385. addr = pfn << PAGE_SHIFT;
  386. eaddr = epfn << PAGE_SHIFT;
  387. if ((epfn >= (KEXEC_CONTROL_MEMORY_LIMIT >> PAGE_SHIFT)) ||
  388. kimage_is_destination_range(image, addr, eaddr)) {
  389. list_add(&pages->lru, &extra_pages);
  390. pages = NULL;
  391. }
  392. } while (!pages);
  393. if (pages) {
  394. /* Remember the allocated page... */
  395. list_add(&pages->lru, &image->control_pages);
  396. /* Because the page is already in it's destination
  397. * location we will never allocate another page at
  398. * that address. Therefore kimage_alloc_pages
  399. * will not return it (again) and we don't need
  400. * to give it an entry in image->segment[].
  401. */
  402. }
  403. /* Deal with the destination pages I have inadvertently allocated.
  404. *
  405. * Ideally I would convert multi-page allocations into single
  406. * page allocations, and add everything to image->dest_pages.
  407. *
  408. * For now it is simpler to just free the pages.
  409. */
  410. kimage_free_page_list(&extra_pages);
  411. return pages;
  412. }
  413. static struct page *kimage_alloc_crash_control_pages(struct kimage *image,
  414. unsigned int order)
  415. {
  416. /* Control pages are special, they are the intermediaries
  417. * that are needed while we copy the rest of the pages
  418. * to their final resting place. As such they must
  419. * not conflict with either the destination addresses
  420. * or memory the kernel is already using.
  421. *
  422. * Control pages are also the only pags we must allocate
  423. * when loading a crash kernel. All of the other pages
  424. * are specified by the segments and we just memcpy
  425. * into them directly.
  426. *
  427. * The only case where we really need more than one of
  428. * these are for architectures where we cannot disable
  429. * the MMU and must instead generate an identity mapped
  430. * page table for all of the memory.
  431. *
  432. * Given the low demand this implements a very simple
  433. * allocator that finds the first hole of the appropriate
  434. * size in the reserved memory region, and allocates all
  435. * of the memory up to and including the hole.
  436. */
  437. unsigned long hole_start, hole_end, size;
  438. struct page *pages;
  439. pages = NULL;
  440. size = (1 << order) << PAGE_SHIFT;
  441. hole_start = (image->control_page + (size - 1)) & ~(size - 1);
  442. hole_end = hole_start + size - 1;
  443. while (hole_end <= crashk_res.end) {
  444. unsigned long i;
  445. if (hole_end > KEXEC_CRASH_CONTROL_MEMORY_LIMIT)
  446. break;
  447. /* See if I overlap any of the segments */
  448. for (i = 0; i < image->nr_segments; i++) {
  449. unsigned long mstart, mend;
  450. mstart = image->segment[i].mem;
  451. mend = mstart + image->segment[i].memsz - 1;
  452. if ((hole_end >= mstart) && (hole_start <= mend)) {
  453. /* Advance the hole to the end of the segment */
  454. hole_start = (mend + (size - 1)) & ~(size - 1);
  455. hole_end = hole_start + size - 1;
  456. break;
  457. }
  458. }
  459. /* If I don't overlap any segments I have found my hole! */
  460. if (i == image->nr_segments) {
  461. pages = pfn_to_page(hole_start >> PAGE_SHIFT);
  462. break;
  463. }
  464. }
  465. if (pages)
  466. image->control_page = hole_end;
  467. return pages;
  468. }
  469. struct page *kimage_alloc_control_pages(struct kimage *image,
  470. unsigned int order)
  471. {
  472. struct page *pages = NULL;
  473. switch (image->type) {
  474. case KEXEC_TYPE_DEFAULT:
  475. pages = kimage_alloc_normal_control_pages(image, order);
  476. break;
  477. case KEXEC_TYPE_CRASH:
  478. pages = kimage_alloc_crash_control_pages(image, order);
  479. break;
  480. }
  481. return pages;
  482. }
  483. static int kimage_add_entry(struct kimage *image, kimage_entry_t entry)
  484. {
  485. if (*image->entry != 0)
  486. image->entry++;
  487. if (image->entry == image->last_entry) {
  488. kimage_entry_t *ind_page;
  489. struct page *page;
  490. page = kimage_alloc_page(image, GFP_KERNEL, KIMAGE_NO_DEST);
  491. if (!page)
  492. return -ENOMEM;
  493. ind_page = page_address(page);
  494. *image->entry = virt_to_phys(ind_page) | IND_INDIRECTION;
  495. image->entry = ind_page;
  496. image->last_entry = ind_page +
  497. ((PAGE_SIZE/sizeof(kimage_entry_t)) - 1);
  498. }
  499. *image->entry = entry;
  500. image->entry++;
  501. *image->entry = 0;
  502. return 0;
  503. }
  504. static int kimage_set_destination(struct kimage *image,
  505. unsigned long destination)
  506. {
  507. int result;
  508. destination &= PAGE_MASK;
  509. result = kimage_add_entry(image, destination | IND_DESTINATION);
  510. if (result == 0)
  511. image->destination = destination;
  512. return result;
  513. }
  514. static int kimage_add_page(struct kimage *image, unsigned long page)
  515. {
  516. int result;
  517. page &= PAGE_MASK;
  518. result = kimage_add_entry(image, page | IND_SOURCE);
  519. if (result == 0)
  520. image->destination += PAGE_SIZE;
  521. return result;
  522. }
  523. static void kimage_free_extra_pages(struct kimage *image)
  524. {
  525. /* Walk through and free any extra destination pages I may have */
  526. kimage_free_page_list(&image->dest_pages);
  527. /* Walk through and free any unusable pages I have cached */
  528. kimage_free_page_list(&image->unuseable_pages);
  529. }
  530. static void kimage_terminate(struct kimage *image)
  531. {
  532. if (*image->entry != 0)
  533. image->entry++;
  534. *image->entry = IND_DONE;
  535. }
  536. #define for_each_kimage_entry(image, ptr, entry) \
  537. for (ptr = &image->head; (entry = *ptr) && !(entry & IND_DONE); \
  538. ptr = (entry & IND_INDIRECTION) ? \
  539. phys_to_virt((entry & PAGE_MASK)) : ptr + 1)
  540. static void kimage_free_entry(kimage_entry_t entry)
  541. {
  542. struct page *page;
  543. page = pfn_to_page(entry >> PAGE_SHIFT);
  544. kimage_free_pages(page);
  545. }
  546. static void kimage_free(struct kimage *image)
  547. {
  548. kimage_entry_t *ptr, entry;
  549. kimage_entry_t ind = 0;
  550. if (!image)
  551. return;
  552. kimage_free_extra_pages(image);
  553. for_each_kimage_entry(image, ptr, entry) {
  554. if (entry & IND_INDIRECTION) {
  555. /* Free the previous indirection page */
  556. if (ind & IND_INDIRECTION)
  557. kimage_free_entry(ind);
  558. /* Save this indirection page until we are
  559. * done with it.
  560. */
  561. ind = entry;
  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. } else {
  673. /* Place the page on the destination list I
  674. * will use it later.
  675. */
  676. list_add(&page->lru, &image->dest_pages);
  677. }
  678. }
  679. return page;
  680. }
  681. static int kimage_load_normal_segment(struct kimage *image,
  682. struct kexec_segment *segment)
  683. {
  684. unsigned long maddr;
  685. size_t ubytes, mbytes;
  686. int result;
  687. unsigned char __user *buf;
  688. result = 0;
  689. buf = segment->buf;
  690. ubytes = segment->bufsz;
  691. mbytes = segment->memsz;
  692. maddr = segment->mem;
  693. result = kimage_set_destination(image, maddr);
  694. if (result < 0)
  695. goto out;
  696. while (mbytes) {
  697. struct page *page;
  698. char *ptr;
  699. size_t uchunk, mchunk;
  700. page = kimage_alloc_page(image, GFP_HIGHUSER, maddr);
  701. if (!page) {
  702. result = -ENOMEM;
  703. goto out;
  704. }
  705. result = kimage_add_page(image, page_to_pfn(page)
  706. << PAGE_SHIFT);
  707. if (result < 0)
  708. goto out;
  709. ptr = kmap(page);
  710. /* Start with a clear page */
  711. clear_page(ptr);
  712. ptr += maddr & ~PAGE_MASK;
  713. mchunk = min_t(size_t, mbytes,
  714. PAGE_SIZE - (maddr & ~PAGE_MASK));
  715. uchunk = min(ubytes, mchunk);
  716. result = copy_from_user(ptr, buf, uchunk);
  717. kunmap(page);
  718. if (result) {
  719. result = -EFAULT;
  720. goto out;
  721. }
  722. ubytes -= uchunk;
  723. maddr += mchunk;
  724. buf += mchunk;
  725. mbytes -= mchunk;
  726. }
  727. out:
  728. return result;
  729. }
  730. static int kimage_load_crash_segment(struct kimage *image,
  731. struct kexec_segment *segment)
  732. {
  733. /* For crash dumps kernels we simply copy the data from
  734. * user space to it's destination.
  735. * We do things a page at a time for the sake of kmap.
  736. */
  737. unsigned long maddr;
  738. size_t ubytes, mbytes;
  739. int result;
  740. unsigned char __user *buf;
  741. result = 0;
  742. buf = segment->buf;
  743. ubytes = segment->bufsz;
  744. mbytes = segment->memsz;
  745. maddr = segment->mem;
  746. while (mbytes) {
  747. struct page *page;
  748. char *ptr;
  749. size_t uchunk, mchunk;
  750. page = pfn_to_page(maddr >> PAGE_SHIFT);
  751. if (!page) {
  752. result = -ENOMEM;
  753. goto out;
  754. }
  755. ptr = kmap(page);
  756. ptr += maddr & ~PAGE_MASK;
  757. mchunk = min_t(size_t, mbytes,
  758. PAGE_SIZE - (maddr & ~PAGE_MASK));
  759. uchunk = min(ubytes, mchunk);
  760. if (mchunk > uchunk) {
  761. /* Zero the trailing part of the page */
  762. memset(ptr + uchunk, 0, mchunk - uchunk);
  763. }
  764. result = copy_from_user(ptr, buf, uchunk);
  765. kexec_flush_icache_page(page);
  766. kunmap(page);
  767. if (result) {
  768. result = -EFAULT;
  769. goto out;
  770. }
  771. ubytes -= uchunk;
  772. maddr += mchunk;
  773. buf += mchunk;
  774. mbytes -= mchunk;
  775. }
  776. out:
  777. return result;
  778. }
  779. static int kimage_load_segment(struct kimage *image,
  780. struct kexec_segment *segment)
  781. {
  782. int result = -ENOMEM;
  783. switch (image->type) {
  784. case KEXEC_TYPE_DEFAULT:
  785. result = kimage_load_normal_segment(image, segment);
  786. break;
  787. case KEXEC_TYPE_CRASH:
  788. result = kimage_load_crash_segment(image, segment);
  789. break;
  790. }
  791. return result;
  792. }
  793. /*
  794. * Exec Kernel system call: for obvious reasons only root may call it.
  795. *
  796. * This call breaks up into three pieces.
  797. * - A generic part which loads the new kernel from the current
  798. * address space, and very carefully places the data in the
  799. * allocated pages.
  800. *
  801. * - A generic part that interacts with the kernel and tells all of
  802. * the devices to shut down. Preventing on-going dmas, and placing
  803. * the devices in a consistent state so a later kernel can
  804. * reinitialize them.
  805. *
  806. * - A machine specific part that includes the syscall number
  807. * and then copies the image to it's final destination. And
  808. * jumps into the image at entry.
  809. *
  810. * kexec does not sync, or unmount filesystems so if you need
  811. * that to happen you need to do that yourself.
  812. */
  813. struct kimage *kexec_image;
  814. struct kimage *kexec_crash_image;
  815. int kexec_load_disabled;
  816. static DEFINE_MUTEX(kexec_mutex);
  817. SYSCALL_DEFINE4(kexec_load, unsigned long, entry, unsigned long, nr_segments,
  818. struct kexec_segment __user *, segments, unsigned long, flags)
  819. {
  820. struct kimage **dest_image, *image;
  821. int result;
  822. /* We only trust the superuser with rebooting the system. */
  823. if (!capable(CAP_SYS_BOOT) || kexec_load_disabled)
  824. return -EPERM;
  825. /*
  826. * Verify we have a legal set of flags
  827. * This leaves us room for future extensions.
  828. */
  829. if ((flags & KEXEC_FLAGS) != (flags & ~KEXEC_ARCH_MASK))
  830. return -EINVAL;
  831. /* Verify we are on the appropriate architecture */
  832. if (((flags & KEXEC_ARCH_MASK) != KEXEC_ARCH) &&
  833. ((flags & KEXEC_ARCH_MASK) != KEXEC_ARCH_DEFAULT))
  834. return -EINVAL;
  835. /* Put an artificial cap on the number
  836. * of segments passed to kexec_load.
  837. */
  838. if (nr_segments > KEXEC_SEGMENT_MAX)
  839. return -EINVAL;
  840. image = NULL;
  841. result = 0;
  842. /* Because we write directly to the reserved memory
  843. * region when loading crash kernels we need a mutex here to
  844. * prevent multiple crash kernels from attempting to load
  845. * simultaneously, and to prevent a crash kernel from loading
  846. * over the top of a in use crash kernel.
  847. *
  848. * KISS: always take the mutex.
  849. */
  850. if (!mutex_trylock(&kexec_mutex))
  851. return -EBUSY;
  852. dest_image = &kexec_image;
  853. if (flags & KEXEC_ON_CRASH)
  854. dest_image = &kexec_crash_image;
  855. if (nr_segments > 0) {
  856. unsigned long i;
  857. /* Loading another kernel to reboot into */
  858. if ((flags & KEXEC_ON_CRASH) == 0)
  859. result = kimage_normal_alloc(&image, entry,
  860. nr_segments, segments);
  861. /* Loading another kernel to switch to if this one crashes */
  862. else if (flags & KEXEC_ON_CRASH) {
  863. /* Free any current crash dump kernel before
  864. * we corrupt it.
  865. */
  866. kimage_free(xchg(&kexec_crash_image, NULL));
  867. result = kimage_crash_alloc(&image, entry,
  868. nr_segments, segments);
  869. crash_map_reserved_pages();
  870. }
  871. if (result)
  872. goto out;
  873. if (flags & KEXEC_PRESERVE_CONTEXT)
  874. image->preserve_context = 1;
  875. result = machine_kexec_prepare(image);
  876. if (result)
  877. goto out;
  878. for (i = 0; i < nr_segments; i++) {
  879. result = kimage_load_segment(image, &image->segment[i]);
  880. if (result)
  881. goto out;
  882. }
  883. kimage_terminate(image);
  884. if (flags & KEXEC_ON_CRASH)
  885. crash_unmap_reserved_pages();
  886. }
  887. /* Install the new kernel, and Uninstall the old */
  888. image = xchg(dest_image, image);
  889. out:
  890. mutex_unlock(&kexec_mutex);
  891. kimage_free(image);
  892. return result;
  893. }
  894. /*
  895. * Add and remove page tables for crashkernel memory
  896. *
  897. * Provide an empty default implementation here -- architecture
  898. * code may override this
  899. */
  900. void __weak crash_map_reserved_pages(void)
  901. {}
  902. void __weak crash_unmap_reserved_pages(void)
  903. {}
  904. #ifdef CONFIG_COMPAT
  905. COMPAT_SYSCALL_DEFINE4(kexec_load, compat_ulong_t, entry,
  906. compat_ulong_t, nr_segments,
  907. struct compat_kexec_segment __user *, segments,
  908. compat_ulong_t, flags)
  909. {
  910. struct compat_kexec_segment in;
  911. struct kexec_segment out, __user *ksegments;
  912. unsigned long i, result;
  913. /* Don't allow clients that don't understand the native
  914. * architecture to do anything.
  915. */
  916. if ((flags & KEXEC_ARCH_MASK) == KEXEC_ARCH_DEFAULT)
  917. return -EINVAL;
  918. if (nr_segments > KEXEC_SEGMENT_MAX)
  919. return -EINVAL;
  920. ksegments = compat_alloc_user_space(nr_segments * sizeof(out));
  921. for (i = 0; i < nr_segments; i++) {
  922. result = copy_from_user(&in, &segments[i], sizeof(in));
  923. if (result)
  924. return -EFAULT;
  925. out.buf = compat_ptr(in.buf);
  926. out.bufsz = in.bufsz;
  927. out.mem = in.mem;
  928. out.memsz = in.memsz;
  929. result = copy_to_user(&ksegments[i], &out, sizeof(out));
  930. if (result)
  931. return -EFAULT;
  932. }
  933. return sys_kexec_load(entry, nr_segments, ksegments, flags);
  934. }
  935. #endif
  936. void crash_kexec(struct pt_regs *regs)
  937. {
  938. /* Take the kexec_mutex here to prevent sys_kexec_load
  939. * running on one cpu from replacing the crash kernel
  940. * we are using after a panic on a different cpu.
  941. *
  942. * If the crash kernel was not located in a fixed area
  943. * of memory the xchg(&kexec_crash_image) would be
  944. * sufficient. But since I reuse the memory...
  945. */
  946. if (mutex_trylock(&kexec_mutex)) {
  947. if (kexec_crash_image) {
  948. struct pt_regs fixed_regs;
  949. crash_setup_regs(&fixed_regs, regs);
  950. crash_save_vmcoreinfo();
  951. machine_crash_shutdown(&fixed_regs);
  952. machine_kexec(kexec_crash_image);
  953. }
  954. mutex_unlock(&kexec_mutex);
  955. }
  956. }
  957. size_t crash_get_memory_size(void)
  958. {
  959. size_t size = 0;
  960. mutex_lock(&kexec_mutex);
  961. if (crashk_res.end != crashk_res.start)
  962. size = resource_size(&crashk_res);
  963. mutex_unlock(&kexec_mutex);
  964. return size;
  965. }
  966. void __weak crash_free_reserved_phys_range(unsigned long begin,
  967. unsigned long end)
  968. {
  969. unsigned long addr;
  970. for (addr = begin; addr < end; addr += PAGE_SIZE)
  971. free_reserved_page(pfn_to_page(addr >> PAGE_SHIFT));
  972. }
  973. int crash_shrink_memory(unsigned long new_size)
  974. {
  975. int ret = 0;
  976. unsigned long start, end;
  977. unsigned long old_size;
  978. struct resource *ram_res;
  979. mutex_lock(&kexec_mutex);
  980. if (kexec_crash_image) {
  981. ret = -ENOENT;
  982. goto unlock;
  983. }
  984. start = crashk_res.start;
  985. end = crashk_res.end;
  986. old_size = (end == 0) ? 0 : end - start + 1;
  987. if (new_size >= old_size) {
  988. ret = (new_size == old_size) ? 0 : -EINVAL;
  989. goto unlock;
  990. }
  991. ram_res = kzalloc(sizeof(*ram_res), GFP_KERNEL);
  992. if (!ram_res) {
  993. ret = -ENOMEM;
  994. goto unlock;
  995. }
  996. start = roundup(start, KEXEC_CRASH_MEM_ALIGN);
  997. end = roundup(start + new_size, KEXEC_CRASH_MEM_ALIGN);
  998. crash_map_reserved_pages();
  999. crash_free_reserved_phys_range(end, crashk_res.end);
  1000. if ((start == end) && (crashk_res.parent != NULL))
  1001. release_resource(&crashk_res);
  1002. ram_res->start = end;
  1003. ram_res->end = crashk_res.end;
  1004. ram_res->flags = IORESOURCE_BUSY | IORESOURCE_MEM;
  1005. ram_res->name = "System RAM";
  1006. crashk_res.end = end - 1;
  1007. insert_resource(&iomem_resource, ram_res);
  1008. crash_unmap_reserved_pages();
  1009. unlock:
  1010. mutex_unlock(&kexec_mutex);
  1011. return ret;
  1012. }
  1013. static u32 *append_elf_note(u32 *buf, char *name, unsigned type, void *data,
  1014. size_t data_len)
  1015. {
  1016. struct elf_note note;
  1017. note.n_namesz = strlen(name) + 1;
  1018. note.n_descsz = data_len;
  1019. note.n_type = type;
  1020. memcpy(buf, &note, sizeof(note));
  1021. buf += (sizeof(note) + 3)/4;
  1022. memcpy(buf, name, note.n_namesz);
  1023. buf += (note.n_namesz + 3)/4;
  1024. memcpy(buf, data, note.n_descsz);
  1025. buf += (note.n_descsz + 3)/4;
  1026. return buf;
  1027. }
  1028. static void final_note(u32 *buf)
  1029. {
  1030. struct elf_note note;
  1031. note.n_namesz = 0;
  1032. note.n_descsz = 0;
  1033. note.n_type = 0;
  1034. memcpy(buf, &note, sizeof(note));
  1035. }
  1036. void crash_save_cpu(struct pt_regs *regs, int cpu)
  1037. {
  1038. struct elf_prstatus prstatus;
  1039. u32 *buf;
  1040. if ((cpu < 0) || (cpu >= nr_cpu_ids))
  1041. return;
  1042. /* Using ELF notes here is opportunistic.
  1043. * I need a well defined structure format
  1044. * for the data I pass, and I need tags
  1045. * on the data to indicate what information I have
  1046. * squirrelled away. ELF notes happen to provide
  1047. * all of that, so there is no need to invent something new.
  1048. */
  1049. buf = (u32 *)per_cpu_ptr(crash_notes, cpu);
  1050. if (!buf)
  1051. return;
  1052. memset(&prstatus, 0, sizeof(prstatus));
  1053. prstatus.pr_pid = current->pid;
  1054. elf_core_copy_kernel_regs(&prstatus.pr_reg, regs);
  1055. buf = append_elf_note(buf, KEXEC_CORE_NOTE_NAME, NT_PRSTATUS,
  1056. &prstatus, sizeof(prstatus));
  1057. final_note(buf);
  1058. }
  1059. static int __init crash_notes_memory_init(void)
  1060. {
  1061. /* Allocate memory for saving cpu registers. */
  1062. crash_notes = alloc_percpu(note_buf_t);
  1063. if (!crash_notes) {
  1064. pr_warn("Kexec: Memory allocation for saving cpu register states failed\n");
  1065. return -ENOMEM;
  1066. }
  1067. return 0;
  1068. }
  1069. subsys_initcall(crash_notes_memory_init);
  1070. /*
  1071. * parsing the "crashkernel" commandline
  1072. *
  1073. * this code is intended to be called from architecture specific code
  1074. */
  1075. /*
  1076. * This function parses command lines in the format
  1077. *
  1078. * crashkernel=ramsize-range:size[,...][@offset]
  1079. *
  1080. * The function returns 0 on success and -EINVAL on failure.
  1081. */
  1082. static int __init parse_crashkernel_mem(char *cmdline,
  1083. unsigned long long system_ram,
  1084. unsigned long long *crash_size,
  1085. unsigned long long *crash_base)
  1086. {
  1087. char *cur = cmdline, *tmp;
  1088. /* for each entry of the comma-separated list */
  1089. do {
  1090. unsigned long long start, end = ULLONG_MAX, size;
  1091. /* get the start of the range */
  1092. start = memparse(cur, &tmp);
  1093. if (cur == tmp) {
  1094. pr_warn("crashkernel: Memory value expected\n");
  1095. return -EINVAL;
  1096. }
  1097. cur = tmp;
  1098. if (*cur != '-') {
  1099. pr_warn("crashkernel: '-' expected\n");
  1100. return -EINVAL;
  1101. }
  1102. cur++;
  1103. /* if no ':' is here, than we read the end */
  1104. if (*cur != ':') {
  1105. end = memparse(cur, &tmp);
  1106. if (cur == tmp) {
  1107. pr_warn("crashkernel: Memory value expected\n");
  1108. return -EINVAL;
  1109. }
  1110. cur = tmp;
  1111. if (end <= start) {
  1112. pr_warn("crashkernel: end <= start\n");
  1113. return -EINVAL;
  1114. }
  1115. }
  1116. if (*cur != ':') {
  1117. pr_warn("crashkernel: ':' expected\n");
  1118. return -EINVAL;
  1119. }
  1120. cur++;
  1121. size = memparse(cur, &tmp);
  1122. if (cur == tmp) {
  1123. pr_warn("Memory value expected\n");
  1124. return -EINVAL;
  1125. }
  1126. cur = tmp;
  1127. if (size >= system_ram) {
  1128. pr_warn("crashkernel: invalid size\n");
  1129. return -EINVAL;
  1130. }
  1131. /* match ? */
  1132. if (system_ram >= start && system_ram < end) {
  1133. *crash_size = size;
  1134. break;
  1135. }
  1136. } while (*cur++ == ',');
  1137. if (*crash_size > 0) {
  1138. while (*cur && *cur != ' ' && *cur != '@')
  1139. cur++;
  1140. if (*cur == '@') {
  1141. cur++;
  1142. *crash_base = memparse(cur, &tmp);
  1143. if (cur == tmp) {
  1144. pr_warn("Memory value expected 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_warn("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_warn("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 = vscnprintf(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 __weak arch_crash_save_vmcoreinfo(void)
  1337. {}
  1338. unsigned long __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(PG_head_mask);
  1403. VMCOREINFO_NUMBER(PAGE_BUDDY_MAPCOUNT_VALUE);
  1404. #ifdef CONFIG_HUGETLBFS
  1405. VMCOREINFO_SYMBOL(free_huge_page);
  1406. #endif
  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. /*
  1462. * migrate_to_reboot_cpu() disables CPU hotplug assuming that
  1463. * no further code needs to use CPU hotplug (which is true in
  1464. * the reboot case). However, the kexec path depends on using
  1465. * CPU hotplug again; so re-enable it here.
  1466. */
  1467. cpu_hotplug_enable();
  1468. pr_emerg("Starting new kernel\n");
  1469. machine_shutdown();
  1470. }
  1471. machine_kexec(kexec_image);
  1472. #ifdef CONFIG_KEXEC_JUMP
  1473. if (kexec_image->preserve_context) {
  1474. syscore_resume();
  1475. Enable_irqs:
  1476. local_irq_enable();
  1477. Enable_cpus:
  1478. enable_nonboot_cpus();
  1479. dpm_resume_start(PMSG_RESTORE);
  1480. Resume_devices:
  1481. dpm_resume_end(PMSG_RESTORE);
  1482. Resume_console:
  1483. resume_console();
  1484. thaw_processes();
  1485. Restore_console:
  1486. pm_restore_console();
  1487. unlock_system_sleep();
  1488. }
  1489. #endif
  1490. Unlock:
  1491. mutex_unlock(&kexec_mutex);
  1492. return error;
  1493. }