kexec.c 6.6 KB

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