machine_kexec_file_64.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. /*
  2. * ppc64 code to implement the kexec_file_load syscall
  3. *
  4. * Copyright (C) 2004 Adam Litke (agl@us.ibm.com)
  5. * Copyright (C) 2004 IBM Corp.
  6. * Copyright (C) 2004,2005 Milton D Miller II, IBM Corporation
  7. * Copyright (C) 2005 R Sharada (sharada@in.ibm.com)
  8. * Copyright (C) 2006 Mohan Kumar M (mohan@in.ibm.com)
  9. * Copyright (C) 2016 IBM Corporation
  10. *
  11. * Based on kexec-tools' kexec-elf-ppc64.c, fs2dt.c.
  12. * Heavily modified for the kernel by
  13. * Thiago Jung Bauermann <bauerman@linux.vnet.ibm.com>.
  14. *
  15. * This program is free software; you can redistribute it and/or modify
  16. * it under the terms of the GNU General Public License as published by
  17. * the Free Software Foundation (version 2 of the License).
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU General Public License for more details.
  23. */
  24. #include <linux/slab.h>
  25. #include <linux/kexec.h>
  26. #include <linux/memblock.h>
  27. #include <linux/of_fdt.h>
  28. #include <linux/libfdt.h>
  29. #include <asm/ima.h>
  30. #define SLAVE_CODE_SIZE 256
  31. static struct kexec_file_ops *kexec_file_loaders[] = {
  32. &kexec_elf64_ops,
  33. };
  34. int arch_kexec_kernel_image_probe(struct kimage *image, void *buf,
  35. unsigned long buf_len)
  36. {
  37. int i, ret = -ENOEXEC;
  38. struct kexec_file_ops *fops;
  39. /* We don't support crash kernels yet. */
  40. if (image->type == KEXEC_TYPE_CRASH)
  41. return -ENOTSUPP;
  42. for (i = 0; i < ARRAY_SIZE(kexec_file_loaders); i++) {
  43. fops = kexec_file_loaders[i];
  44. if (!fops || !fops->probe)
  45. continue;
  46. ret = fops->probe(buf, buf_len);
  47. if (!ret) {
  48. image->fops = fops;
  49. return ret;
  50. }
  51. }
  52. return ret;
  53. }
  54. void *arch_kexec_kernel_image_load(struct kimage *image)
  55. {
  56. if (!image->fops || !image->fops->load)
  57. return ERR_PTR(-ENOEXEC);
  58. return image->fops->load(image, image->kernel_buf,
  59. image->kernel_buf_len, image->initrd_buf,
  60. image->initrd_buf_len, image->cmdline_buf,
  61. image->cmdline_buf_len);
  62. }
  63. int arch_kimage_file_post_load_cleanup(struct kimage *image)
  64. {
  65. if (!image->fops || !image->fops->cleanup)
  66. return 0;
  67. return image->fops->cleanup(image->image_loader_data);
  68. }
  69. /**
  70. * arch_kexec_walk_mem - call func(data) for each unreserved memory block
  71. * @kbuf: Context info for the search. Also passed to @func.
  72. * @func: Function to call for each memory block.
  73. *
  74. * This function is used by kexec_add_buffer and kexec_locate_mem_hole
  75. * to find unreserved memory to load kexec segments into.
  76. *
  77. * Return: The memory walk will stop when func returns a non-zero value
  78. * and that value will be returned. If all free regions are visited without
  79. * func returning non-zero, then zero will be returned.
  80. */
  81. int arch_kexec_walk_mem(struct kexec_buf *kbuf, int (*func)(u64, u64, void *))
  82. {
  83. int ret = 0;
  84. u64 i;
  85. phys_addr_t mstart, mend;
  86. if (kbuf->top_down) {
  87. for_each_free_mem_range_reverse(i, NUMA_NO_NODE, 0,
  88. &mstart, &mend, NULL) {
  89. /*
  90. * In memblock, end points to the first byte after the
  91. * range while in kexec, end points to the last byte
  92. * in the range.
  93. */
  94. ret = func(mstart, mend - 1, kbuf);
  95. if (ret)
  96. break;
  97. }
  98. } else {
  99. for_each_free_mem_range(i, NUMA_NO_NODE, 0, &mstart, &mend,
  100. NULL) {
  101. /*
  102. * In memblock, end points to the first byte after the
  103. * range while in kexec, end points to the last byte
  104. * in the range.
  105. */
  106. ret = func(mstart, mend - 1, kbuf);
  107. if (ret)
  108. break;
  109. }
  110. }
  111. return ret;
  112. }
  113. /**
  114. * setup_purgatory - initialize the purgatory's global variables
  115. * @image: kexec image.
  116. * @slave_code: Slave code for the purgatory.
  117. * @fdt: Flattened device tree for the next kernel.
  118. * @kernel_load_addr: Address where the kernel is loaded.
  119. * @fdt_load_addr: Address where the flattened device tree is loaded.
  120. *
  121. * Return: 0 on success, or negative errno on error.
  122. */
  123. int setup_purgatory(struct kimage *image, const void *slave_code,
  124. const void *fdt, unsigned long kernel_load_addr,
  125. unsigned long fdt_load_addr)
  126. {
  127. unsigned int *slave_code_buf, master_entry;
  128. int ret;
  129. slave_code_buf = kmalloc(SLAVE_CODE_SIZE, GFP_KERNEL);
  130. if (!slave_code_buf)
  131. return -ENOMEM;
  132. /* Get the slave code from the new kernel and put it in purgatory. */
  133. ret = kexec_purgatory_get_set_symbol(image, "purgatory_start",
  134. slave_code_buf, SLAVE_CODE_SIZE,
  135. true);
  136. if (ret) {
  137. kfree(slave_code_buf);
  138. return ret;
  139. }
  140. master_entry = slave_code_buf[0];
  141. memcpy(slave_code_buf, slave_code, SLAVE_CODE_SIZE);
  142. slave_code_buf[0] = master_entry;
  143. ret = kexec_purgatory_get_set_symbol(image, "purgatory_start",
  144. slave_code_buf, SLAVE_CODE_SIZE,
  145. false);
  146. kfree(slave_code_buf);
  147. ret = kexec_purgatory_get_set_symbol(image, "kernel", &kernel_load_addr,
  148. sizeof(kernel_load_addr), false);
  149. if (ret)
  150. return ret;
  151. ret = kexec_purgatory_get_set_symbol(image, "dt_offset", &fdt_load_addr,
  152. sizeof(fdt_load_addr), false);
  153. if (ret)
  154. return ret;
  155. return 0;
  156. }
  157. /**
  158. * delete_fdt_mem_rsv - delete memory reservation with given address and size
  159. *
  160. * Return: 0 on success, or negative errno on error.
  161. */
  162. int delete_fdt_mem_rsv(void *fdt, unsigned long start, unsigned long size)
  163. {
  164. int i, ret, num_rsvs = fdt_num_mem_rsv(fdt);
  165. for (i = 0; i < num_rsvs; i++) {
  166. uint64_t rsv_start, rsv_size;
  167. ret = fdt_get_mem_rsv(fdt, i, &rsv_start, &rsv_size);
  168. if (ret) {
  169. pr_err("Malformed device tree.\n");
  170. return -EINVAL;
  171. }
  172. if (rsv_start == start && rsv_size == size) {
  173. ret = fdt_del_mem_rsv(fdt, i);
  174. if (ret) {
  175. pr_err("Error deleting device tree reservation.\n");
  176. return -EINVAL;
  177. }
  178. return 0;
  179. }
  180. }
  181. return -ENOENT;
  182. }
  183. /*
  184. * setup_new_fdt - modify /chosen and memory reservation for the next kernel
  185. * @image: kexec image being loaded.
  186. * @fdt: Flattened device tree for the next kernel.
  187. * @initrd_load_addr: Address where the next initrd will be loaded.
  188. * @initrd_len: Size of the next initrd, or 0 if there will be none.
  189. * @cmdline: Command line for the next kernel, or NULL if there will
  190. * be none.
  191. *
  192. * Return: 0 on success, or negative errno on error.
  193. */
  194. int setup_new_fdt(const struct kimage *image, void *fdt,
  195. unsigned long initrd_load_addr, unsigned long initrd_len,
  196. const char *cmdline)
  197. {
  198. int ret, chosen_node;
  199. const void *prop;
  200. /* Remove memory reservation for the current device tree. */
  201. ret = delete_fdt_mem_rsv(fdt, __pa(initial_boot_params),
  202. fdt_totalsize(initial_boot_params));
  203. if (ret == 0)
  204. pr_debug("Removed old device tree reservation.\n");
  205. else if (ret != -ENOENT)
  206. return ret;
  207. chosen_node = fdt_path_offset(fdt, "/chosen");
  208. if (chosen_node == -FDT_ERR_NOTFOUND) {
  209. chosen_node = fdt_add_subnode(fdt, fdt_path_offset(fdt, "/"),
  210. "chosen");
  211. if (chosen_node < 0) {
  212. pr_err("Error creating /chosen.\n");
  213. return -EINVAL;
  214. }
  215. } else if (chosen_node < 0) {
  216. pr_err("Malformed device tree: error reading /chosen.\n");
  217. return -EINVAL;
  218. }
  219. /* Did we boot using an initrd? */
  220. prop = fdt_getprop(fdt, chosen_node, "linux,initrd-start", NULL);
  221. if (prop) {
  222. uint64_t tmp_start, tmp_end, tmp_size;
  223. tmp_start = fdt64_to_cpu(*((const fdt64_t *) prop));
  224. prop = fdt_getprop(fdt, chosen_node, "linux,initrd-end", NULL);
  225. if (!prop) {
  226. pr_err("Malformed device tree.\n");
  227. return -EINVAL;
  228. }
  229. tmp_end = fdt64_to_cpu(*((const fdt64_t *) prop));
  230. /*
  231. * kexec reserves exact initrd size, while firmware may
  232. * reserve a multiple of PAGE_SIZE, so check for both.
  233. */
  234. tmp_size = tmp_end - tmp_start;
  235. ret = delete_fdt_mem_rsv(fdt, tmp_start, tmp_size);
  236. if (ret == -ENOENT)
  237. ret = delete_fdt_mem_rsv(fdt, tmp_start,
  238. round_up(tmp_size, PAGE_SIZE));
  239. if (ret == 0)
  240. pr_debug("Removed old initrd reservation.\n");
  241. else if (ret != -ENOENT)
  242. return ret;
  243. /* If there's no new initrd, delete the old initrd's info. */
  244. if (initrd_len == 0) {
  245. ret = fdt_delprop(fdt, chosen_node,
  246. "linux,initrd-start");
  247. if (ret) {
  248. pr_err("Error deleting linux,initrd-start.\n");
  249. return -EINVAL;
  250. }
  251. ret = fdt_delprop(fdt, chosen_node, "linux,initrd-end");
  252. if (ret) {
  253. pr_err("Error deleting linux,initrd-end.\n");
  254. return -EINVAL;
  255. }
  256. }
  257. }
  258. if (initrd_len) {
  259. ret = fdt_setprop_u64(fdt, chosen_node,
  260. "linux,initrd-start",
  261. initrd_load_addr);
  262. if (ret < 0) {
  263. pr_err("Error setting up the new device tree.\n");
  264. return -EINVAL;
  265. }
  266. /* initrd-end is the first address after the initrd image. */
  267. ret = fdt_setprop_u64(fdt, chosen_node, "linux,initrd-end",
  268. initrd_load_addr + initrd_len);
  269. if (ret < 0) {
  270. pr_err("Error setting up the new device tree.\n");
  271. return -EINVAL;
  272. }
  273. ret = fdt_add_mem_rsv(fdt, initrd_load_addr, initrd_len);
  274. if (ret) {
  275. pr_err("Error reserving initrd memory: %s\n",
  276. fdt_strerror(ret));
  277. return -EINVAL;
  278. }
  279. }
  280. if (cmdline != NULL) {
  281. ret = fdt_setprop_string(fdt, chosen_node, "bootargs", cmdline);
  282. if (ret < 0) {
  283. pr_err("Error setting up the new device tree.\n");
  284. return -EINVAL;
  285. }
  286. } else {
  287. ret = fdt_delprop(fdt, chosen_node, "bootargs");
  288. if (ret && ret != -FDT_ERR_NOTFOUND) {
  289. pr_err("Error deleting bootargs.\n");
  290. return -EINVAL;
  291. }
  292. }
  293. ret = setup_ima_buffer(image, fdt, chosen_node);
  294. if (ret) {
  295. pr_err("Error setting up the new device tree.\n");
  296. return ret;
  297. }
  298. ret = fdt_setprop(fdt, chosen_node, "linux,booted-from-kexec", NULL, 0);
  299. if (ret) {
  300. pr_err("Error setting up the new device tree.\n");
  301. return -EINVAL;
  302. }
  303. return 0;
  304. }