kexec.c 40 KB

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