kexec.c 6.8 KB

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