kexec.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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. SYSCALL_DEFINE4(kexec_load, unsigned long, entry, unsigned long, nr_segments,
  166. struct kexec_segment __user *, segments, unsigned long, flags)
  167. {
  168. int result;
  169. /* We only trust the superuser with rebooting the system. */
  170. if (!capable(CAP_SYS_BOOT) || kexec_load_disabled)
  171. return -EPERM;
  172. /*
  173. * Verify we have a legal set of flags
  174. * This leaves us room for future extensions.
  175. */
  176. if ((flags & KEXEC_FLAGS) != (flags & ~KEXEC_ARCH_MASK))
  177. return -EINVAL;
  178. /* Verify we are on the appropriate architecture */
  179. if (((flags & KEXEC_ARCH_MASK) != KEXEC_ARCH) &&
  180. ((flags & KEXEC_ARCH_MASK) != KEXEC_ARCH_DEFAULT))
  181. return -EINVAL;
  182. /* Put an artificial cap on the number
  183. * of segments passed to kexec_load.
  184. */
  185. if (nr_segments > KEXEC_SEGMENT_MAX)
  186. return -EINVAL;
  187. /* Because we write directly to the reserved memory
  188. * region when loading crash kernels we need a mutex here to
  189. * prevent multiple crash kernels from attempting to load
  190. * simultaneously, and to prevent a crash kernel from loading
  191. * over the top of a in use crash kernel.
  192. *
  193. * KISS: always take the mutex.
  194. */
  195. if (!mutex_trylock(&kexec_mutex))
  196. return -EBUSY;
  197. result = do_kexec_load(entry, nr_segments, segments, flags);
  198. mutex_unlock(&kexec_mutex);
  199. return result;
  200. }
  201. #ifdef CONFIG_COMPAT
  202. COMPAT_SYSCALL_DEFINE4(kexec_load, compat_ulong_t, entry,
  203. compat_ulong_t, nr_segments,
  204. struct compat_kexec_segment __user *, segments,
  205. compat_ulong_t, flags)
  206. {
  207. struct compat_kexec_segment in;
  208. struct kexec_segment out, __user *ksegments;
  209. unsigned long i, result;
  210. /* Don't allow clients that don't understand the native
  211. * architecture to do anything.
  212. */
  213. if ((flags & KEXEC_ARCH_MASK) == KEXEC_ARCH_DEFAULT)
  214. return -EINVAL;
  215. if (nr_segments > KEXEC_SEGMENT_MAX)
  216. return -EINVAL;
  217. ksegments = compat_alloc_user_space(nr_segments * sizeof(out));
  218. for (i = 0; i < nr_segments; i++) {
  219. result = copy_from_user(&in, &segments[i], sizeof(in));
  220. if (result)
  221. return -EFAULT;
  222. out.buf = compat_ptr(in.buf);
  223. out.bufsz = in.bufsz;
  224. out.mem = in.mem;
  225. out.memsz = in.memsz;
  226. result = copy_to_user(&ksegments[i], &out, sizeof(out));
  227. if (result)
  228. return -EFAULT;
  229. }
  230. return sys_kexec_load(entry, nr_segments, ksegments, flags);
  231. }
  232. #endif