kexec.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079
  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/spinlock.h>
  15. #include <linux/list.h>
  16. #include <linux/highmem.h>
  17. #include <linux/syscalls.h>
  18. #include <linux/reboot.h>
  19. #include <linux/syscalls.h>
  20. #include <linux/ioport.h>
  21. #include <linux/hardirq.h>
  22. #include <asm/page.h>
  23. #include <asm/uaccess.h>
  24. #include <asm/io.h>
  25. #include <asm/system.h>
  26. #include <asm/semaphore.h>
  27. /* Per cpu memory for storing cpu states in case of system crash. */
  28. note_buf_t* crash_notes;
  29. /* Location of the reserved area for the crash kernel */
  30. struct resource crashk_res = {
  31. .name = "Crash kernel",
  32. .start = 0,
  33. .end = 0,
  34. .flags = IORESOURCE_BUSY | IORESOURCE_MEM
  35. };
  36. int kexec_should_crash(struct task_struct *p)
  37. {
  38. if (in_interrupt() || !p->pid || p->pid == 1 || panic_on_oops)
  39. return 1;
  40. return 0;
  41. }
  42. /*
  43. * When kexec transitions to the new kernel there is a one-to-one
  44. * mapping between physical and virtual addresses. On processors
  45. * where you can disable the MMU this is trivial, and easy. For
  46. * others it is still a simple predictable page table to setup.
  47. *
  48. * In that environment kexec copies the new kernel to its final
  49. * resting place. This means I can only support memory whose
  50. * physical address can fit in an unsigned long. In particular
  51. * addresses where (pfn << PAGE_SHIFT) > ULONG_MAX cannot be handled.
  52. * If the assembly stub has more restrictive requirements
  53. * KEXEC_SOURCE_MEMORY_LIMIT and KEXEC_DEST_MEMORY_LIMIT can be
  54. * defined more restrictively in <asm/kexec.h>.
  55. *
  56. * The code for the transition from the current kernel to the
  57. * the new kernel is placed in the control_code_buffer, whose size
  58. * is given by KEXEC_CONTROL_CODE_SIZE. In the best case only a single
  59. * page of memory is necessary, but some architectures require more.
  60. * Because this memory must be identity mapped in the transition from
  61. * virtual to physical addresses it must live in the range
  62. * 0 - TASK_SIZE, as only the user space mappings are arbitrarily
  63. * modifiable.
  64. *
  65. * The assembly stub in the control code buffer is passed a linked list
  66. * of descriptor pages detailing the source pages of the new kernel,
  67. * and the destination addresses of those source pages. As this data
  68. * structure is not used in the context of the current OS, it must
  69. * be self-contained.
  70. *
  71. * The code has been made to work with highmem pages and will use a
  72. * destination page in its final resting place (if it happens
  73. * to allocate it). The end product of this is that most of the
  74. * physical address space, and most of RAM can be used.
  75. *
  76. * Future directions include:
  77. * - allocating a page table with the control code buffer identity
  78. * mapped, to simplify machine_kexec and make kexec_on_panic more
  79. * reliable.
  80. */
  81. /*
  82. * KIMAGE_NO_DEST is an impossible destination address..., for
  83. * allocating pages whose destination address we do not care about.
  84. */
  85. #define KIMAGE_NO_DEST (-1UL)
  86. static int kimage_is_destination_range(struct kimage *image,
  87. unsigned long start, unsigned long end);
  88. static struct page *kimage_alloc_page(struct kimage *image,
  89. gfp_t gfp_mask,
  90. unsigned long dest);
  91. static int do_kimage_alloc(struct kimage **rimage, unsigned long entry,
  92. unsigned long nr_segments,
  93. struct kexec_segment __user *segments)
  94. {
  95. size_t segment_bytes;
  96. struct kimage *image;
  97. unsigned long i;
  98. int result;
  99. /* Allocate a controlling structure */
  100. result = -ENOMEM;
  101. image = kmalloc(sizeof(*image), GFP_KERNEL);
  102. if (!image)
  103. goto out;
  104. memset(image, 0, sizeof(*image));
  105. image->head = 0;
  106. image->entry = &image->head;
  107. image->last_entry = &image->head;
  108. image->control_page = ~0; /* By default this does not apply */
  109. image->start = entry;
  110. image->type = KEXEC_TYPE_DEFAULT;
  111. /* Initialize the list of control pages */
  112. INIT_LIST_HEAD(&image->control_pages);
  113. /* Initialize the list of destination pages */
  114. INIT_LIST_HEAD(&image->dest_pages);
  115. /* Initialize the list of unuseable pages */
  116. INIT_LIST_HEAD(&image->unuseable_pages);
  117. /* Read in the segments */
  118. image->nr_segments = nr_segments;
  119. segment_bytes = nr_segments * sizeof(*segments);
  120. result = copy_from_user(image->segment, segments, segment_bytes);
  121. if (result)
  122. goto out;
  123. /*
  124. * Verify we have good destination addresses. The caller is
  125. * responsible for making certain we don't attempt to load
  126. * the new image into invalid or reserved areas of RAM. This
  127. * just verifies it is an address we can use.
  128. *
  129. * Since the kernel does everything in page size chunks ensure
  130. * the destination addreses are page aligned. Too many
  131. * special cases crop of when we don't do this. The most
  132. * insidious is getting overlapping destination addresses
  133. * simply because addresses are changed to page size
  134. * granularity.
  135. */
  136. result = -EADDRNOTAVAIL;
  137. for (i = 0; i < nr_segments; i++) {
  138. unsigned long mstart, mend;
  139. mstart = image->segment[i].mem;
  140. mend = mstart + image->segment[i].memsz;
  141. if ((mstart & ~PAGE_MASK) || (mend & ~PAGE_MASK))
  142. goto out;
  143. if (mend >= KEXEC_DESTINATION_MEMORY_LIMIT)
  144. goto out;
  145. }
  146. /* Verify our destination addresses do not overlap.
  147. * If we alloed overlapping destination addresses
  148. * through very weird things can happen with no
  149. * easy explanation as one segment stops on another.
  150. */
  151. result = -EINVAL;
  152. for (i = 0; i < nr_segments; i++) {
  153. unsigned long mstart, mend;
  154. unsigned long j;
  155. mstart = image->segment[i].mem;
  156. mend = mstart + image->segment[i].memsz;
  157. for (j = 0; j < i; j++) {
  158. unsigned long pstart, pend;
  159. pstart = image->segment[j].mem;
  160. pend = pstart + image->segment[j].memsz;
  161. /* Do the segments overlap ? */
  162. if ((mend > pstart) && (mstart < pend))
  163. goto out;
  164. }
  165. }
  166. /* Ensure our buffer sizes are strictly less than
  167. * our memory sizes. This should always be the case,
  168. * and it is easier to check up front than to be surprised
  169. * later on.
  170. */
  171. result = -EINVAL;
  172. for (i = 0; i < nr_segments; i++) {
  173. if (image->segment[i].bufsz > image->segment[i].memsz)
  174. goto out;
  175. }
  176. result = 0;
  177. out:
  178. if (result == 0)
  179. *rimage = image;
  180. else
  181. kfree(image);
  182. return result;
  183. }
  184. static int kimage_normal_alloc(struct kimage **rimage, unsigned long entry,
  185. unsigned long nr_segments,
  186. struct kexec_segment __user *segments)
  187. {
  188. int result;
  189. struct kimage *image;
  190. /* Allocate and initialize a controlling structure */
  191. image = NULL;
  192. result = do_kimage_alloc(&image, entry, nr_segments, segments);
  193. if (result)
  194. goto out;
  195. *rimage = image;
  196. /*
  197. * Find a location for the control code buffer, and add it
  198. * the vector of segments so that it's pages will also be
  199. * counted as destination pages.
  200. */
  201. result = -ENOMEM;
  202. image->control_code_page = kimage_alloc_control_pages(image,
  203. get_order(KEXEC_CONTROL_CODE_SIZE));
  204. if (!image->control_code_page) {
  205. printk(KERN_ERR "Could not allocate control_code_buffer\n");
  206. goto out;
  207. }
  208. result = 0;
  209. out:
  210. if (result == 0)
  211. *rimage = image;
  212. else
  213. kfree(image);
  214. return result;
  215. }
  216. static int kimage_crash_alloc(struct kimage **rimage, unsigned long entry,
  217. unsigned long nr_segments,
  218. struct kexec_segment __user *segments)
  219. {
  220. int result;
  221. struct kimage *image;
  222. unsigned long i;
  223. image = NULL;
  224. /* Verify we have a valid entry point */
  225. if ((entry < crashk_res.start) || (entry > crashk_res.end)) {
  226. result = -EADDRNOTAVAIL;
  227. goto out;
  228. }
  229. /* Allocate and initialize a controlling structure */
  230. result = do_kimage_alloc(&image, entry, nr_segments, segments);
  231. if (result)
  232. goto out;
  233. /* Enable the special crash kernel control page
  234. * allocation policy.
  235. */
  236. image->control_page = crashk_res.start;
  237. image->type = KEXEC_TYPE_CRASH;
  238. /*
  239. * Verify we have good destination addresses. Normally
  240. * the caller is responsible for making certain we don't
  241. * attempt to load the new image into invalid or reserved
  242. * areas of RAM. But crash kernels are preloaded into a
  243. * reserved area of ram. We must ensure the addresses
  244. * are in the reserved area otherwise preloading the
  245. * kernel could corrupt things.
  246. */
  247. result = -EADDRNOTAVAIL;
  248. for (i = 0; i < nr_segments; i++) {
  249. unsigned long mstart, mend;
  250. mstart = image->segment[i].mem;
  251. mend = mstart + image->segment[i].memsz - 1;
  252. /* Ensure we are within the crash kernel limits */
  253. if ((mstart < crashk_res.start) || (mend > crashk_res.end))
  254. goto out;
  255. }
  256. /*
  257. * Find a location for the control code buffer, and add
  258. * the vector of segments so that it's pages will also be
  259. * counted as destination pages.
  260. */
  261. result = -ENOMEM;
  262. image->control_code_page = kimage_alloc_control_pages(image,
  263. get_order(KEXEC_CONTROL_CODE_SIZE));
  264. if (!image->control_code_page) {
  265. printk(KERN_ERR "Could not allocate control_code_buffer\n");
  266. goto out;
  267. }
  268. result = 0;
  269. out:
  270. if (result == 0)
  271. *rimage = image;
  272. else
  273. kfree(image);
  274. return result;
  275. }
  276. static int kimage_is_destination_range(struct kimage *image,
  277. unsigned long start,
  278. unsigned long end)
  279. {
  280. unsigned long i;
  281. for (i = 0; i < image->nr_segments; i++) {
  282. unsigned long mstart, mend;
  283. mstart = image->segment[i].mem;
  284. mend = mstart + image->segment[i].memsz;
  285. if ((end > mstart) && (start < mend))
  286. return 1;
  287. }
  288. return 0;
  289. }
  290. static struct page *kimage_alloc_pages(gfp_t gfp_mask, unsigned int order)
  291. {
  292. struct page *pages;
  293. pages = alloc_pages(gfp_mask, order);
  294. if (pages) {
  295. unsigned int count, i;
  296. pages->mapping = NULL;
  297. set_page_private(pages, order);
  298. count = 1 << order;
  299. for (i = 0; i < count; i++)
  300. SetPageReserved(pages + i);
  301. }
  302. return pages;
  303. }
  304. static void kimage_free_pages(struct page *page)
  305. {
  306. unsigned int order, count, i;
  307. order = page_private(page);
  308. count = 1 << order;
  309. for (i = 0; i < count; i++)
  310. ClearPageReserved(page + i);
  311. __free_pages(page, order);
  312. }
  313. static void kimage_free_page_list(struct list_head *list)
  314. {
  315. struct list_head *pos, *next;
  316. list_for_each_safe(pos, next, list) {
  317. struct page *page;
  318. page = list_entry(pos, struct page, lru);
  319. list_del(&page->lru);
  320. kimage_free_pages(page);
  321. }
  322. }
  323. static struct page *kimage_alloc_normal_control_pages(struct kimage *image,
  324. unsigned int order)
  325. {
  326. /* Control pages are special, they are the intermediaries
  327. * that are needed while we copy the rest of the pages
  328. * to their final resting place. As such they must
  329. * not conflict with either the destination addresses
  330. * or memory the kernel is already using.
  331. *
  332. * The only case where we really need more than one of
  333. * these are for architectures where we cannot disable
  334. * the MMU and must instead generate an identity mapped
  335. * page table for all of the memory.
  336. *
  337. * At worst this runs in O(N) of the image size.
  338. */
  339. struct list_head extra_pages;
  340. struct page *pages;
  341. unsigned int count;
  342. count = 1 << order;
  343. INIT_LIST_HEAD(&extra_pages);
  344. /* Loop while I can allocate a page and the page allocated
  345. * is a destination page.
  346. */
  347. do {
  348. unsigned long pfn, epfn, addr, eaddr;
  349. pages = kimage_alloc_pages(GFP_KERNEL, order);
  350. if (!pages)
  351. break;
  352. pfn = page_to_pfn(pages);
  353. epfn = pfn + count;
  354. addr = pfn << PAGE_SHIFT;
  355. eaddr = epfn << PAGE_SHIFT;
  356. if ((epfn >= (KEXEC_CONTROL_MEMORY_LIMIT >> PAGE_SHIFT)) ||
  357. kimage_is_destination_range(image, addr, eaddr)) {
  358. list_add(&pages->lru, &extra_pages);
  359. pages = NULL;
  360. }
  361. } while (!pages);
  362. if (pages) {
  363. /* Remember the allocated page... */
  364. list_add(&pages->lru, &image->control_pages);
  365. /* Because the page is already in it's destination
  366. * location we will never allocate another page at
  367. * that address. Therefore kimage_alloc_pages
  368. * will not return it (again) and we don't need
  369. * to give it an entry in image->segment[].
  370. */
  371. }
  372. /* Deal with the destination pages I have inadvertently allocated.
  373. *
  374. * Ideally I would convert multi-page allocations into single
  375. * page allocations, and add everyting to image->dest_pages.
  376. *
  377. * For now it is simpler to just free the pages.
  378. */
  379. kimage_free_page_list(&extra_pages);
  380. return pages;
  381. }
  382. static struct page *kimage_alloc_crash_control_pages(struct kimage *image,
  383. unsigned int order)
  384. {
  385. /* Control pages are special, they are the intermediaries
  386. * that are needed while we copy the rest of the pages
  387. * to their final resting place. As such they must
  388. * not conflict with either the destination addresses
  389. * or memory the kernel is already using.
  390. *
  391. * Control pages are also the only pags we must allocate
  392. * when loading a crash kernel. All of the other pages
  393. * are specified by the segments and we just memcpy
  394. * into them directly.
  395. *
  396. * The only case where we really need more than one of
  397. * these are for architectures where we cannot disable
  398. * the MMU and must instead generate an identity mapped
  399. * page table for all of the memory.
  400. *
  401. * Given the low demand this implements a very simple
  402. * allocator that finds the first hole of the appropriate
  403. * size in the reserved memory region, and allocates all
  404. * of the memory up to and including the hole.
  405. */
  406. unsigned long hole_start, hole_end, size;
  407. struct page *pages;
  408. pages = NULL;
  409. size = (1 << order) << PAGE_SHIFT;
  410. hole_start = (image->control_page + (size - 1)) & ~(size - 1);
  411. hole_end = hole_start + size - 1;
  412. while (hole_end <= crashk_res.end) {
  413. unsigned long i;
  414. if (hole_end > KEXEC_CONTROL_MEMORY_LIMIT)
  415. break;
  416. if (hole_end > crashk_res.end)
  417. break;
  418. /* See if I overlap any of the segments */
  419. for (i = 0; i < image->nr_segments; i++) {
  420. unsigned long mstart, mend;
  421. mstart = image->segment[i].mem;
  422. mend = mstart + image->segment[i].memsz - 1;
  423. if ((hole_end >= mstart) && (hole_start <= mend)) {
  424. /* Advance the hole to the end of the segment */
  425. hole_start = (mend + (size - 1)) & ~(size - 1);
  426. hole_end = hole_start + size - 1;
  427. break;
  428. }
  429. }
  430. /* If I don't overlap any segments I have found my hole! */
  431. if (i == image->nr_segments) {
  432. pages = pfn_to_page(hole_start >> PAGE_SHIFT);
  433. break;
  434. }
  435. }
  436. if (pages)
  437. image->control_page = hole_end;
  438. return pages;
  439. }
  440. struct page *kimage_alloc_control_pages(struct kimage *image,
  441. unsigned int order)
  442. {
  443. struct page *pages = NULL;
  444. switch (image->type) {
  445. case KEXEC_TYPE_DEFAULT:
  446. pages = kimage_alloc_normal_control_pages(image, order);
  447. break;
  448. case KEXEC_TYPE_CRASH:
  449. pages = kimage_alloc_crash_control_pages(image, order);
  450. break;
  451. }
  452. return pages;
  453. }
  454. static int kimage_add_entry(struct kimage *image, kimage_entry_t entry)
  455. {
  456. if (*image->entry != 0)
  457. image->entry++;
  458. if (image->entry == image->last_entry) {
  459. kimage_entry_t *ind_page;
  460. struct page *page;
  461. page = kimage_alloc_page(image, GFP_KERNEL, KIMAGE_NO_DEST);
  462. if (!page)
  463. return -ENOMEM;
  464. ind_page = page_address(page);
  465. *image->entry = virt_to_phys(ind_page) | IND_INDIRECTION;
  466. image->entry = ind_page;
  467. image->last_entry = ind_page +
  468. ((PAGE_SIZE/sizeof(kimage_entry_t)) - 1);
  469. }
  470. *image->entry = entry;
  471. image->entry++;
  472. *image->entry = 0;
  473. return 0;
  474. }
  475. static int kimage_set_destination(struct kimage *image,
  476. unsigned long destination)
  477. {
  478. int result;
  479. destination &= PAGE_MASK;
  480. result = kimage_add_entry(image, destination | IND_DESTINATION);
  481. if (result == 0)
  482. image->destination = destination;
  483. return result;
  484. }
  485. static int kimage_add_page(struct kimage *image, unsigned long page)
  486. {
  487. int result;
  488. page &= PAGE_MASK;
  489. result = kimage_add_entry(image, page | IND_SOURCE);
  490. if (result == 0)
  491. image->destination += PAGE_SIZE;
  492. return result;
  493. }
  494. static void kimage_free_extra_pages(struct kimage *image)
  495. {
  496. /* Walk through and free any extra destination pages I may have */
  497. kimage_free_page_list(&image->dest_pages);
  498. /* Walk through and free any unuseable pages I have cached */
  499. kimage_free_page_list(&image->unuseable_pages);
  500. }
  501. static int kimage_terminate(struct kimage *image)
  502. {
  503. if (*image->entry != 0)
  504. image->entry++;
  505. *image->entry = IND_DONE;
  506. return 0;
  507. }
  508. #define for_each_kimage_entry(image, ptr, entry) \
  509. for (ptr = &image->head; (entry = *ptr) && !(entry & IND_DONE); \
  510. ptr = (entry & IND_INDIRECTION)? \
  511. phys_to_virt((entry & PAGE_MASK)): ptr +1)
  512. static void kimage_free_entry(kimage_entry_t entry)
  513. {
  514. struct page *page;
  515. page = pfn_to_page(entry >> PAGE_SHIFT);
  516. kimage_free_pages(page);
  517. }
  518. static void kimage_free(struct kimage *image)
  519. {
  520. kimage_entry_t *ptr, entry;
  521. kimage_entry_t ind = 0;
  522. if (!image)
  523. return;
  524. kimage_free_extra_pages(image);
  525. for_each_kimage_entry(image, ptr, entry) {
  526. if (entry & IND_INDIRECTION) {
  527. /* Free the previous indirection page */
  528. if (ind & IND_INDIRECTION)
  529. kimage_free_entry(ind);
  530. /* Save this indirection page until we are
  531. * done with it.
  532. */
  533. ind = entry;
  534. }
  535. else if (entry & IND_SOURCE)
  536. kimage_free_entry(entry);
  537. }
  538. /* Free the final indirection page */
  539. if (ind & IND_INDIRECTION)
  540. kimage_free_entry(ind);
  541. /* Handle any machine specific cleanup */
  542. machine_kexec_cleanup(image);
  543. /* Free the kexec control pages... */
  544. kimage_free_page_list(&image->control_pages);
  545. kfree(image);
  546. }
  547. static kimage_entry_t *kimage_dst_used(struct kimage *image,
  548. unsigned long page)
  549. {
  550. kimage_entry_t *ptr, entry;
  551. unsigned long destination = 0;
  552. for_each_kimage_entry(image, ptr, entry) {
  553. if (entry & IND_DESTINATION)
  554. destination = entry & PAGE_MASK;
  555. else if (entry & IND_SOURCE) {
  556. if (page == destination)
  557. return ptr;
  558. destination += PAGE_SIZE;
  559. }
  560. }
  561. return NULL;
  562. }
  563. static struct page *kimage_alloc_page(struct kimage *image,
  564. gfp_t gfp_mask,
  565. unsigned long destination)
  566. {
  567. /*
  568. * Here we implement safeguards to ensure that a source page
  569. * is not copied to its destination page before the data on
  570. * the destination page is no longer useful.
  571. *
  572. * To do this we maintain the invariant that a source page is
  573. * either its own destination page, or it is not a
  574. * destination page at all.
  575. *
  576. * That is slightly stronger than required, but the proof
  577. * that no problems will not occur is trivial, and the
  578. * implementation is simply to verify.
  579. *
  580. * When allocating all pages normally this algorithm will run
  581. * in O(N) time, but in the worst case it will run in O(N^2)
  582. * time. If the runtime is a problem the data structures can
  583. * be fixed.
  584. */
  585. struct page *page;
  586. unsigned long addr;
  587. /*
  588. * Walk through the list of destination pages, and see if I
  589. * have a match.
  590. */
  591. list_for_each_entry(page, &image->dest_pages, lru) {
  592. addr = page_to_pfn(page) << PAGE_SHIFT;
  593. if (addr == destination) {
  594. list_del(&page->lru);
  595. return page;
  596. }
  597. }
  598. page = NULL;
  599. while (1) {
  600. kimage_entry_t *old;
  601. /* Allocate a page, if we run out of memory give up */
  602. page = kimage_alloc_pages(gfp_mask, 0);
  603. if (!page)
  604. return NULL;
  605. /* If the page cannot be used file it away */
  606. if (page_to_pfn(page) >
  607. (KEXEC_SOURCE_MEMORY_LIMIT >> PAGE_SHIFT)) {
  608. list_add(&page->lru, &image->unuseable_pages);
  609. continue;
  610. }
  611. addr = page_to_pfn(page) << PAGE_SHIFT;
  612. /* If it is the destination page we want use it */
  613. if (addr == destination)
  614. break;
  615. /* If the page is not a destination page use it */
  616. if (!kimage_is_destination_range(image, addr,
  617. addr + PAGE_SIZE))
  618. break;
  619. /*
  620. * I know that the page is someones destination page.
  621. * See if there is already a source page for this
  622. * destination page. And if so swap the source pages.
  623. */
  624. old = kimage_dst_used(image, addr);
  625. if (old) {
  626. /* If so move it */
  627. unsigned long old_addr;
  628. struct page *old_page;
  629. old_addr = *old & PAGE_MASK;
  630. old_page = pfn_to_page(old_addr >> PAGE_SHIFT);
  631. copy_highpage(page, old_page);
  632. *old = addr | (*old & ~PAGE_MASK);
  633. /* The old page I have found cannot be a
  634. * destination page, so return it.
  635. */
  636. addr = old_addr;
  637. page = old_page;
  638. break;
  639. }
  640. else {
  641. /* Place the page on the destination list I
  642. * will use it later.
  643. */
  644. list_add(&page->lru, &image->dest_pages);
  645. }
  646. }
  647. return page;
  648. }
  649. static int kimage_load_normal_segment(struct kimage *image,
  650. struct kexec_segment *segment)
  651. {
  652. unsigned long maddr;
  653. unsigned long ubytes, mbytes;
  654. int result;
  655. unsigned char __user *buf;
  656. result = 0;
  657. buf = segment->buf;
  658. ubytes = segment->bufsz;
  659. mbytes = segment->memsz;
  660. maddr = segment->mem;
  661. result = kimage_set_destination(image, maddr);
  662. if (result < 0)
  663. goto out;
  664. while (mbytes) {
  665. struct page *page;
  666. char *ptr;
  667. size_t uchunk, mchunk;
  668. page = kimage_alloc_page(image, GFP_HIGHUSER, maddr);
  669. if (page == 0) {
  670. result = -ENOMEM;
  671. goto out;
  672. }
  673. result = kimage_add_page(image, page_to_pfn(page)
  674. << PAGE_SHIFT);
  675. if (result < 0)
  676. goto out;
  677. ptr = kmap(page);
  678. /* Start with a clear page */
  679. memset(ptr, 0, PAGE_SIZE);
  680. ptr += maddr & ~PAGE_MASK;
  681. mchunk = PAGE_SIZE - (maddr & ~PAGE_MASK);
  682. if (mchunk > mbytes)
  683. mchunk = mbytes;
  684. uchunk = mchunk;
  685. if (uchunk > ubytes)
  686. uchunk = ubytes;
  687. result = copy_from_user(ptr, buf, uchunk);
  688. kunmap(page);
  689. if (result) {
  690. result = (result < 0) ? result : -EIO;
  691. goto out;
  692. }
  693. ubytes -= uchunk;
  694. maddr += mchunk;
  695. buf += mchunk;
  696. mbytes -= mchunk;
  697. }
  698. out:
  699. return result;
  700. }
  701. static int kimage_load_crash_segment(struct kimage *image,
  702. struct kexec_segment *segment)
  703. {
  704. /* For crash dumps kernels we simply copy the data from
  705. * user space to it's destination.
  706. * We do things a page at a time for the sake of kmap.
  707. */
  708. unsigned long maddr;
  709. unsigned long ubytes, mbytes;
  710. int result;
  711. unsigned char __user *buf;
  712. result = 0;
  713. buf = segment->buf;
  714. ubytes = segment->bufsz;
  715. mbytes = segment->memsz;
  716. maddr = segment->mem;
  717. while (mbytes) {
  718. struct page *page;
  719. char *ptr;
  720. size_t uchunk, mchunk;
  721. page = pfn_to_page(maddr >> PAGE_SHIFT);
  722. if (page == 0) {
  723. result = -ENOMEM;
  724. goto out;
  725. }
  726. ptr = kmap(page);
  727. ptr += maddr & ~PAGE_MASK;
  728. mchunk = PAGE_SIZE - (maddr & ~PAGE_MASK);
  729. if (mchunk > mbytes)
  730. mchunk = mbytes;
  731. uchunk = mchunk;
  732. if (uchunk > ubytes) {
  733. uchunk = ubytes;
  734. /* Zero the trailing part of the page */
  735. memset(ptr + uchunk, 0, mchunk - uchunk);
  736. }
  737. result = copy_from_user(ptr, buf, uchunk);
  738. kunmap(page);
  739. if (result) {
  740. result = (result < 0) ? result : -EIO;
  741. goto out;
  742. }
  743. ubytes -= uchunk;
  744. maddr += mchunk;
  745. buf += mchunk;
  746. mbytes -= mchunk;
  747. }
  748. out:
  749. return result;
  750. }
  751. static int kimage_load_segment(struct kimage *image,
  752. struct kexec_segment *segment)
  753. {
  754. int result = -ENOMEM;
  755. switch (image->type) {
  756. case KEXEC_TYPE_DEFAULT:
  757. result = kimage_load_normal_segment(image, segment);
  758. break;
  759. case KEXEC_TYPE_CRASH:
  760. result = kimage_load_crash_segment(image, segment);
  761. break;
  762. }
  763. return result;
  764. }
  765. /*
  766. * Exec Kernel system call: for obvious reasons only root may call it.
  767. *
  768. * This call breaks up into three pieces.
  769. * - A generic part which loads the new kernel from the current
  770. * address space, and very carefully places the data in the
  771. * allocated pages.
  772. *
  773. * - A generic part that interacts with the kernel and tells all of
  774. * the devices to shut down. Preventing on-going dmas, and placing
  775. * the devices in a consistent state so a later kernel can
  776. * reinitialize them.
  777. *
  778. * - A machine specific part that includes the syscall number
  779. * and the copies the image to it's final destination. And
  780. * jumps into the image at entry.
  781. *
  782. * kexec does not sync, or unmount filesystems so if you need
  783. * that to happen you need to do that yourself.
  784. */
  785. struct kimage *kexec_image;
  786. struct kimage *kexec_crash_image;
  787. /*
  788. * A home grown binary mutex.
  789. * Nothing can wait so this mutex is safe to use
  790. * in interrupt context :)
  791. */
  792. static int kexec_lock;
  793. asmlinkage long sys_kexec_load(unsigned long entry, unsigned long nr_segments,
  794. struct kexec_segment __user *segments,
  795. unsigned long flags)
  796. {
  797. struct kimage **dest_image, *image;
  798. int locked;
  799. int result;
  800. /* We only trust the superuser with rebooting the system. */
  801. if (!capable(CAP_SYS_BOOT))
  802. return -EPERM;
  803. /*
  804. * Verify we have a legal set of flags
  805. * This leaves us room for future extensions.
  806. */
  807. if ((flags & KEXEC_FLAGS) != (flags & ~KEXEC_ARCH_MASK))
  808. return -EINVAL;
  809. /* Verify we are on the appropriate architecture */
  810. if (((flags & KEXEC_ARCH_MASK) != KEXEC_ARCH) &&
  811. ((flags & KEXEC_ARCH_MASK) != KEXEC_ARCH_DEFAULT))
  812. return -EINVAL;
  813. /* Put an artificial cap on the number
  814. * of segments passed to kexec_load.
  815. */
  816. if (nr_segments > KEXEC_SEGMENT_MAX)
  817. return -EINVAL;
  818. image = NULL;
  819. result = 0;
  820. /* Because we write directly to the reserved memory
  821. * region when loading crash kernels we need a mutex here to
  822. * prevent multiple crash kernels from attempting to load
  823. * simultaneously, and to prevent a crash kernel from loading
  824. * over the top of a in use crash kernel.
  825. *
  826. * KISS: always take the mutex.
  827. */
  828. locked = xchg(&kexec_lock, 1);
  829. if (locked)
  830. return -EBUSY;
  831. dest_image = &kexec_image;
  832. if (flags & KEXEC_ON_CRASH)
  833. dest_image = &kexec_crash_image;
  834. if (nr_segments > 0) {
  835. unsigned long i;
  836. /* Loading another kernel to reboot into */
  837. if ((flags & KEXEC_ON_CRASH) == 0)
  838. result = kimage_normal_alloc(&image, entry,
  839. nr_segments, segments);
  840. /* Loading another kernel to switch to if this one crashes */
  841. else if (flags & KEXEC_ON_CRASH) {
  842. /* Free any current crash dump kernel before
  843. * we corrupt it.
  844. */
  845. kimage_free(xchg(&kexec_crash_image, NULL));
  846. result = kimage_crash_alloc(&image, entry,
  847. nr_segments, segments);
  848. }
  849. if (result)
  850. goto out;
  851. result = machine_kexec_prepare(image);
  852. if (result)
  853. goto out;
  854. for (i = 0; i < nr_segments; i++) {
  855. result = kimage_load_segment(image, &image->segment[i]);
  856. if (result)
  857. goto out;
  858. }
  859. result = kimage_terminate(image);
  860. if (result)
  861. goto out;
  862. }
  863. /* Install the new kernel, and Uninstall the old */
  864. image = xchg(dest_image, image);
  865. out:
  866. xchg(&kexec_lock, 0); /* Release the mutex */
  867. kimage_free(image);
  868. return result;
  869. }
  870. #ifdef CONFIG_COMPAT
  871. asmlinkage long compat_sys_kexec_load(unsigned long entry,
  872. unsigned long nr_segments,
  873. struct compat_kexec_segment __user *segments,
  874. unsigned long flags)
  875. {
  876. struct compat_kexec_segment in;
  877. struct kexec_segment out, __user *ksegments;
  878. unsigned long i, result;
  879. /* Don't allow clients that don't understand the native
  880. * architecture to do anything.
  881. */
  882. if ((flags & KEXEC_ARCH_MASK) == KEXEC_ARCH_DEFAULT)
  883. return -EINVAL;
  884. if (nr_segments > KEXEC_SEGMENT_MAX)
  885. return -EINVAL;
  886. ksegments = compat_alloc_user_space(nr_segments * sizeof(out));
  887. for (i=0; i < nr_segments; i++) {
  888. result = copy_from_user(&in, &segments[i], sizeof(in));
  889. if (result)
  890. return -EFAULT;
  891. out.buf = compat_ptr(in.buf);
  892. out.bufsz = in.bufsz;
  893. out.mem = in.mem;
  894. out.memsz = in.memsz;
  895. result = copy_to_user(&ksegments[i], &out, sizeof(out));
  896. if (result)
  897. return -EFAULT;
  898. }
  899. return sys_kexec_load(entry, nr_segments, ksegments, flags);
  900. }
  901. #endif
  902. void crash_kexec(struct pt_regs *regs)
  903. {
  904. int locked;
  905. /* Take the kexec_lock here to prevent sys_kexec_load
  906. * running on one cpu from replacing the crash kernel
  907. * we are using after a panic on a different cpu.
  908. *
  909. * If the crash kernel was not located in a fixed area
  910. * of memory the xchg(&kexec_crash_image) would be
  911. * sufficient. But since I reuse the memory...
  912. */
  913. locked = xchg(&kexec_lock, 1);
  914. if (!locked) {
  915. if (kexec_crash_image) {
  916. struct pt_regs fixed_regs;
  917. crash_setup_regs(&fixed_regs, regs);
  918. machine_crash_shutdown(&fixed_regs);
  919. machine_kexec(kexec_crash_image);
  920. }
  921. xchg(&kexec_lock, 0);
  922. }
  923. }
  924. static int __init crash_notes_memory_init(void)
  925. {
  926. /* Allocate memory for saving cpu registers. */
  927. crash_notes = alloc_percpu(note_buf_t);
  928. if (!crash_notes) {
  929. printk("Kexec: Memory allocation for saving cpu register"
  930. " states failed\n");
  931. return -ENOMEM;
  932. }
  933. return 0;
  934. }
  935. module_init(crash_notes_memory_init)