kexec.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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. /*
  125. * Some architecture(like S390) may touch the crash memory before
  126. * machine_kexec_prepare(), we must copy vmcoreinfo data after it.
  127. */
  128. ret = kimage_crash_copy_vmcoreinfo(image);
  129. if (ret)
  130. goto out;
  131. for (i = 0; i < nr_segments; i++) {
  132. ret = kimage_load_segment(image, &image->segment[i]);
  133. if (ret)
  134. goto out;
  135. }
  136. kimage_terminate(image);
  137. /* Install the new kernel and uninstall the old */
  138. image = xchg(dest_image, image);
  139. out:
  140. if ((flags & KEXEC_ON_CRASH) && kexec_crash_image)
  141. arch_kexec_protect_crashkres();
  142. kimage_free(image);
  143. return ret;
  144. }
  145. /*
  146. * Exec Kernel system call: for obvious reasons only root may call it.
  147. *
  148. * This call breaks up into three pieces.
  149. * - A generic part which loads the new kernel from the current
  150. * address space, and very carefully places the data in the
  151. * allocated pages.
  152. *
  153. * - A generic part that interacts with the kernel and tells all of
  154. * the devices to shut down. Preventing on-going dmas, and placing
  155. * the devices in a consistent state so a later kernel can
  156. * reinitialize them.
  157. *
  158. * - A machine specific part that includes the syscall number
  159. * and then copies the image to it's final destination. And
  160. * jumps into the image at entry.
  161. *
  162. * kexec does not sync, or unmount filesystems so if you need
  163. * that to happen you need to do that yourself.
  164. */
  165. static inline int kexec_load_check(unsigned long nr_segments,
  166. unsigned long flags)
  167. {
  168. /* We only trust the superuser with rebooting the system. */
  169. if (!capable(CAP_SYS_BOOT) || kexec_load_disabled)
  170. return -EPERM;
  171. /*
  172. * Verify we have a legal set of flags
  173. * This leaves us room for future extensions.
  174. */
  175. if ((flags & KEXEC_FLAGS) != (flags & ~KEXEC_ARCH_MASK))
  176. return -EINVAL;
  177. /* Put an artificial cap on the number
  178. * of segments passed to kexec_load.
  179. */
  180. if (nr_segments > KEXEC_SEGMENT_MAX)
  181. return -EINVAL;
  182. return 0;
  183. }
  184. SYSCALL_DEFINE4(kexec_load, unsigned long, entry, unsigned long, nr_segments,
  185. struct kexec_segment __user *, segments, unsigned long, flags)
  186. {
  187. int result;
  188. result = kexec_load_check(nr_segments, flags);
  189. if (result)
  190. return result;
  191. /* Verify we are on the appropriate architecture */
  192. if (((flags & KEXEC_ARCH_MASK) != KEXEC_ARCH) &&
  193. ((flags & KEXEC_ARCH_MASK) != KEXEC_ARCH_DEFAULT))
  194. return -EINVAL;
  195. /* Because we write directly to the reserved memory
  196. * region when loading crash kernels we need a mutex here to
  197. * prevent multiple crash kernels from attempting to load
  198. * simultaneously, and to prevent a crash kernel from loading
  199. * over the top of a in use crash kernel.
  200. *
  201. * KISS: always take the mutex.
  202. */
  203. if (!mutex_trylock(&kexec_mutex))
  204. return -EBUSY;
  205. result = do_kexec_load(entry, nr_segments, segments, flags);
  206. mutex_unlock(&kexec_mutex);
  207. return result;
  208. }
  209. #ifdef CONFIG_COMPAT
  210. COMPAT_SYSCALL_DEFINE4(kexec_load, compat_ulong_t, entry,
  211. compat_ulong_t, nr_segments,
  212. struct compat_kexec_segment __user *, segments,
  213. compat_ulong_t, flags)
  214. {
  215. struct compat_kexec_segment in;
  216. struct kexec_segment out, __user *ksegments;
  217. unsigned long i, result;
  218. result = kexec_load_check(nr_segments, flags);
  219. if (result)
  220. return result;
  221. /* Don't allow clients that don't understand the native
  222. * architecture to do anything.
  223. */
  224. if ((flags & KEXEC_ARCH_MASK) == KEXEC_ARCH_DEFAULT)
  225. return -EINVAL;
  226. ksegments = compat_alloc_user_space(nr_segments * sizeof(out));
  227. for (i = 0; i < nr_segments; i++) {
  228. result = copy_from_user(&in, &segments[i], sizeof(in));
  229. if (result)
  230. return -EFAULT;
  231. out.buf = compat_ptr(in.buf);
  232. out.bufsz = in.bufsz;
  233. out.mem = in.mem;
  234. out.memsz = in.memsz;
  235. result = copy_to_user(&ksegments[i], &out, sizeof(out));
  236. if (result)
  237. return -EFAULT;
  238. }
  239. /* Because we write directly to the reserved memory
  240. * region when loading crash kernels we need a mutex here to
  241. * prevent multiple crash kernels from attempting to load
  242. * simultaneously, and to prevent a crash kernel from loading
  243. * over the top of a in use crash kernel.
  244. *
  245. * KISS: always take the mutex.
  246. */
  247. if (!mutex_trylock(&kexec_mutex))
  248. return -EBUSY;
  249. result = do_kexec_load(entry, nr_segments, ksegments, flags);
  250. mutex_unlock(&kexec_mutex);
  251. return result;
  252. }
  253. #endif