kexec.c 40 KB

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