kexec_core.c 39 KB

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