kexec.c 6.6 KB

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