kexec.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. /*
  2. * kexec.c - kexec_load 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. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  9. #include <linux/capability.h>
  10. #include <linux/mm.h>
  11. #include <linux/file.h>
  12. #include <linux/kexec.h>
  13. #include <linux/mutex.h>
  14. #include <linux/list.h>
  15. #include <linux/syscalls.h>
  16. #include <linux/vmalloc.h>
  17. #include <linux/slab.h>
  18. #include "kexec_internal.h"
  19. static int copy_user_segment_list(struct kimage *image,
  20. unsigned long nr_segments,
  21. struct kexec_segment __user *segments)
  22. {
  23. int ret;
  24. size_t segment_bytes;
  25. /* Read in the segments */
  26. image->nr_segments = nr_segments;
  27. segment_bytes = nr_segments * sizeof(*segments);
  28. ret = copy_from_user(image->segment, segments, segment_bytes);
  29. if (ret)
  30. ret = -EFAULT;
  31. return ret;
  32. }
  33. static int kimage_alloc_init(struct kimage **rimage, unsigned long entry,
  34. unsigned long nr_segments,
  35. struct kexec_segment __user *segments,
  36. unsigned long flags)
  37. {
  38. int ret;
  39. struct kimage *image;
  40. bool kexec_on_panic = flags & KEXEC_ON_CRASH;
  41. if (kexec_on_panic) {
  42. /* Verify we have a valid entry point */
  43. if ((entry < phys_to_boot_phys(crashk_res.start)) ||
  44. (entry > phys_to_boot_phys(crashk_res.end)))
  45. return -EADDRNOTAVAIL;
  46. }
  47. /* Allocate and initialize a controlling structure */
  48. image = do_kimage_alloc_init();
  49. if (!image)
  50. return -ENOMEM;
  51. image->start = entry;
  52. ret = copy_user_segment_list(image, nr_segments, segments);
  53. if (ret)
  54. goto out_free_image;
  55. if (kexec_on_panic) {
  56. /* Enable special crash kernel control page alloc policy. */
  57. image->control_page = crashk_res.start;
  58. image->type = KEXEC_TYPE_CRASH;
  59. }
  60. ret = sanity_check_segment_list(image);
  61. if (ret)
  62. goto out_free_image;
  63. /*
  64. * Find a location for the control code buffer, and add it
  65. * the vector of segments so that it's pages will also be
  66. * counted as destination pages.
  67. */
  68. ret = -ENOMEM;
  69. image->control_code_page = kimage_alloc_control_pages(image,
  70. get_order(KEXEC_CONTROL_PAGE_SIZE));
  71. if (!image->control_code_page) {
  72. pr_err("Could not allocate control_code_buffer\n");
  73. goto out_free_image;
  74. }
  75. if (!kexec_on_panic) {
  76. image->swap_page = kimage_alloc_control_pages(image, 0);
  77. if (!image->swap_page) {
  78. pr_err("Could not allocate swap buffer\n");
  79. goto out_free_control_pages;
  80. }
  81. }
  82. *rimage = image;
  83. return 0;
  84. out_free_control_pages:
  85. kimage_free_page_list(&image->control_pages);
  86. out_free_image:
  87. kfree(image);
  88. return ret;
  89. }
  90. static int do_kexec_load(unsigned long entry, unsigned long nr_segments,
  91. struct kexec_segment __user *segments, unsigned long flags)
  92. {
  93. struct kimage **dest_image, *image;
  94. unsigned long i;
  95. int ret;
  96. if (flags & KEXEC_ON_CRASH) {
  97. dest_image = &kexec_crash_image;
  98. if (kexec_crash_image)
  99. arch_kexec_unprotect_crashkres();
  100. } else {
  101. dest_image = &kexec_image;
  102. }
  103. if (nr_segments == 0) {
  104. /* Uninstall image */
  105. kimage_free(xchg(dest_image, NULL));
  106. return 0;
  107. }
  108. if (flags & KEXEC_ON_CRASH) {
  109. /*
  110. * Loading another kernel to switch to if this one
  111. * crashes. Free any current crash dump kernel before
  112. * we corrupt it.
  113. */
  114. kimage_free(xchg(&kexec_crash_image, NULL));
  115. }
  116. ret = kimage_alloc_init(&image, entry, nr_segments, segments, flags);
  117. if (ret)
  118. return ret;
  119. if (flags & KEXEC_PRESERVE_CONTEXT)
  120. image->preserve_context = 1;
  121. ret = machine_kexec_prepare(image);
  122. if (ret)
  123. goto out;
  124. for (i = 0; i < nr_segments; i++) {
  125. ret = kimage_load_segment(image, &image->segment[i]);
  126. if (ret)
  127. goto out;
  128. }
  129. kimage_terminate(image);
  130. /* Install the new kernel and uninstall the old */
  131. image = xchg(dest_image, image);
  132. out:
  133. if ((flags & KEXEC_ON_CRASH) && kexec_crash_image)
  134. arch_kexec_protect_crashkres();
  135. kimage_free(image);
  136. return ret;
  137. }
  138. /*
  139. * Exec Kernel system call: for obvious reasons only root may call it.
  140. *
  141. * This call breaks up into three pieces.
  142. * - A generic part which loads the new kernel from the current
  143. * address space, and very carefully places the data in the
  144. * allocated pages.
  145. *
  146. * - A generic part that interacts with the kernel and tells all of
  147. * the devices to shut down. Preventing on-going dmas, and placing
  148. * the devices in a consistent state so a later kernel can
  149. * reinitialize them.
  150. *
  151. * - A machine specific part that includes the syscall number
  152. * and then copies the image to it's final destination. And
  153. * jumps into the image at entry.
  154. *
  155. * kexec does not sync, or unmount filesystems so if you need
  156. * that to happen you need to do that yourself.
  157. */
  158. SYSCALL_DEFINE4(kexec_load, unsigned long, entry, unsigned long, nr_segments,
  159. struct kexec_segment __user *, segments, unsigned long, flags)
  160. {
  161. int result;
  162. /* We only trust the superuser with rebooting the system. */
  163. if (!capable(CAP_SYS_BOOT) || kexec_load_disabled)
  164. return -EPERM;
  165. /*
  166. * Verify we have a legal set of flags
  167. * This leaves us room for future extensions.
  168. */
  169. if ((flags & KEXEC_FLAGS) != (flags & ~KEXEC_ARCH_MASK))
  170. return -EINVAL;
  171. /* Verify we are on the appropriate architecture */
  172. if (((flags & KEXEC_ARCH_MASK) != KEXEC_ARCH) &&
  173. ((flags & KEXEC_ARCH_MASK) != KEXEC_ARCH_DEFAULT))
  174. return -EINVAL;
  175. /* Put an artificial cap on the number
  176. * of segments passed to kexec_load.
  177. */
  178. if (nr_segments > KEXEC_SEGMENT_MAX)
  179. return -EINVAL;
  180. /* Because we write directly to the reserved memory
  181. * region when loading crash kernels we need a mutex here to
  182. * prevent multiple crash kernels from attempting to load
  183. * simultaneously, and to prevent a crash kernel from loading
  184. * over the top of a in use crash kernel.
  185. *
  186. * KISS: always take the mutex.
  187. */
  188. if (!mutex_trylock(&kexec_mutex))
  189. return -EBUSY;
  190. result = do_kexec_load(entry, nr_segments, segments, flags);
  191. mutex_unlock(&kexec_mutex);
  192. return result;
  193. }
  194. #ifdef CONFIG_COMPAT
  195. COMPAT_SYSCALL_DEFINE4(kexec_load, compat_ulong_t, entry,
  196. compat_ulong_t, nr_segments,
  197. struct compat_kexec_segment __user *, segments,
  198. compat_ulong_t, flags)
  199. {
  200. struct compat_kexec_segment in;
  201. struct kexec_segment out, __user *ksegments;
  202. unsigned long i, result;
  203. /* Don't allow clients that don't understand the native
  204. * architecture to do anything.
  205. */
  206. if ((flags & KEXEC_ARCH_MASK) == KEXEC_ARCH_DEFAULT)
  207. return -EINVAL;
  208. if (nr_segments > KEXEC_SEGMENT_MAX)
  209. return -EINVAL;
  210. ksegments = compat_alloc_user_space(nr_segments * sizeof(out));
  211. for (i = 0; i < nr_segments; i++) {
  212. result = copy_from_user(&in, &segments[i], sizeof(in));
  213. if (result)
  214. return -EFAULT;
  215. out.buf = compat_ptr(in.buf);
  216. out.bufsz = in.bufsz;
  217. out.mem = in.mem;
  218. out.memsz = in.memsz;
  219. result = copy_to_user(&ksegments[i], &out, sizeof(out));
  220. if (result)
  221. return -EFAULT;
  222. }
  223. return sys_kexec_load(entry, nr_segments, ksegments, flags);
  224. }
  225. #endif