kexec.c 40 KB

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