machine_kexec_file_64.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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,
  82. int (*func)(struct resource *, void *))
  83. {
  84. int ret = 0;
  85. u64 i;
  86. phys_addr_t mstart, mend;
  87. struct resource res = { };
  88. if (kbuf->top_down) {
  89. for_each_free_mem_range_reverse(i, NUMA_NO_NODE, 0,
  90. &mstart, &mend, NULL) {
  91. /*
  92. * In memblock, end points to the first byte after the
  93. * range while in kexec, end points to the last byte
  94. * in the range.
  95. */
  96. res.start = mstart;
  97. res.end = mend - 1;
  98. ret = func(&res, kbuf);
  99. if (ret)
  100. break;
  101. }
  102. } else {
  103. for_each_free_mem_range(i, NUMA_NO_NODE, 0, &mstart, &mend,
  104. NULL) {
  105. /*
  106. * In memblock, end points to the first byte after the
  107. * range while in kexec, end points to the last byte
  108. * in the range.
  109. */
  110. res.start = mstart;
  111. res.end = mend - 1;
  112. ret = func(&res, kbuf);
  113. if (ret)
  114. break;
  115. }
  116. }
  117. return ret;
  118. }
  119. /**
  120. * setup_purgatory - initialize the purgatory's global variables
  121. * @image: kexec image.
  122. * @slave_code: Slave code for the purgatory.
  123. * @fdt: Flattened device tree for the next kernel.
  124. * @kernel_load_addr: Address where the kernel is loaded.
  125. * @fdt_load_addr: Address where the flattened device tree is loaded.
  126. *
  127. * Return: 0 on success, or negative errno on error.
  128. */
  129. int setup_purgatory(struct kimage *image, const void *slave_code,
  130. const void *fdt, unsigned long kernel_load_addr,
  131. unsigned long fdt_load_addr)
  132. {
  133. unsigned int *slave_code_buf, master_entry;
  134. int ret;
  135. slave_code_buf = kmalloc(SLAVE_CODE_SIZE, GFP_KERNEL);
  136. if (!slave_code_buf)
  137. return -ENOMEM;
  138. /* Get the slave code from the new kernel and put it in purgatory. */
  139. ret = kexec_purgatory_get_set_symbol(image, "purgatory_start",
  140. slave_code_buf, SLAVE_CODE_SIZE,
  141. true);
  142. if (ret) {
  143. kfree(slave_code_buf);
  144. return ret;
  145. }
  146. master_entry = slave_code_buf[0];
  147. memcpy(slave_code_buf, slave_code, SLAVE_CODE_SIZE);
  148. slave_code_buf[0] = master_entry;
  149. ret = kexec_purgatory_get_set_symbol(image, "purgatory_start",
  150. slave_code_buf, SLAVE_CODE_SIZE,
  151. false);
  152. kfree(slave_code_buf);
  153. ret = kexec_purgatory_get_set_symbol(image, "kernel", &kernel_load_addr,
  154. sizeof(kernel_load_addr), false);
  155. if (ret)
  156. return ret;
  157. ret = kexec_purgatory_get_set_symbol(image, "dt_offset", &fdt_load_addr,
  158. sizeof(fdt_load_addr), false);
  159. if (ret)
  160. return ret;
  161. return 0;
  162. }
  163. /**
  164. * delete_fdt_mem_rsv - delete memory reservation with given address and size
  165. *
  166. * Return: 0 on success, or negative errno on error.
  167. */
  168. int delete_fdt_mem_rsv(void *fdt, unsigned long start, unsigned long size)
  169. {
  170. int i, ret, num_rsvs = fdt_num_mem_rsv(fdt);
  171. for (i = 0; i < num_rsvs; i++) {
  172. uint64_t rsv_start, rsv_size;
  173. ret = fdt_get_mem_rsv(fdt, i, &rsv_start, &rsv_size);
  174. if (ret) {
  175. pr_err("Malformed device tree.\n");
  176. return -EINVAL;
  177. }
  178. if (rsv_start == start && rsv_size == size) {
  179. ret = fdt_del_mem_rsv(fdt, i);
  180. if (ret) {
  181. pr_err("Error deleting device tree reservation.\n");
  182. return -EINVAL;
  183. }
  184. return 0;
  185. }
  186. }
  187. return -ENOENT;
  188. }
  189. /*
  190. * setup_new_fdt - modify /chosen and memory reservation for the next kernel
  191. * @image: kexec image being loaded.
  192. * @fdt: Flattened device tree for the next kernel.
  193. * @initrd_load_addr: Address where the next initrd will be loaded.
  194. * @initrd_len: Size of the next initrd, or 0 if there will be none.
  195. * @cmdline: Command line for the next kernel, or NULL if there will
  196. * be none.
  197. *
  198. * Return: 0 on success, or negative errno on error.
  199. */
  200. int setup_new_fdt(const struct kimage *image, void *fdt,
  201. unsigned long initrd_load_addr, unsigned long initrd_len,
  202. const char *cmdline)
  203. {
  204. int ret, chosen_node;
  205. const void *prop;
  206. /* Remove memory reservation for the current device tree. */
  207. ret = delete_fdt_mem_rsv(fdt, __pa(initial_boot_params),
  208. fdt_totalsize(initial_boot_params));
  209. if (ret == 0)
  210. pr_debug("Removed old device tree reservation.\n");
  211. else if (ret != -ENOENT)
  212. return ret;
  213. chosen_node = fdt_path_offset(fdt, "/chosen");
  214. if (chosen_node == -FDT_ERR_NOTFOUND) {
  215. chosen_node = fdt_add_subnode(fdt, fdt_path_offset(fdt, "/"),
  216. "chosen");
  217. if (chosen_node < 0) {
  218. pr_err("Error creating /chosen.\n");
  219. return -EINVAL;
  220. }
  221. } else if (chosen_node < 0) {
  222. pr_err("Malformed device tree: error reading /chosen.\n");
  223. return -EINVAL;
  224. }
  225. /* Did we boot using an initrd? */
  226. prop = fdt_getprop(fdt, chosen_node, "linux,initrd-start", NULL);
  227. if (prop) {
  228. uint64_t tmp_start, tmp_end, tmp_size;
  229. tmp_start = fdt64_to_cpu(*((const fdt64_t *) prop));
  230. prop = fdt_getprop(fdt, chosen_node, "linux,initrd-end", NULL);
  231. if (!prop) {
  232. pr_err("Malformed device tree.\n");
  233. return -EINVAL;
  234. }
  235. tmp_end = fdt64_to_cpu(*((const fdt64_t *) prop));
  236. /*
  237. * kexec reserves exact initrd size, while firmware may
  238. * reserve a multiple of PAGE_SIZE, so check for both.
  239. */
  240. tmp_size = tmp_end - tmp_start;
  241. ret = delete_fdt_mem_rsv(fdt, tmp_start, tmp_size);
  242. if (ret == -ENOENT)
  243. ret = delete_fdt_mem_rsv(fdt, tmp_start,
  244. round_up(tmp_size, PAGE_SIZE));
  245. if (ret == 0)
  246. pr_debug("Removed old initrd reservation.\n");
  247. else if (ret != -ENOENT)
  248. return ret;
  249. /* If there's no new initrd, delete the old initrd's info. */
  250. if (initrd_len == 0) {
  251. ret = fdt_delprop(fdt, chosen_node,
  252. "linux,initrd-start");
  253. if (ret) {
  254. pr_err("Error deleting linux,initrd-start.\n");
  255. return -EINVAL;
  256. }
  257. ret = fdt_delprop(fdt, chosen_node, "linux,initrd-end");
  258. if (ret) {
  259. pr_err("Error deleting linux,initrd-end.\n");
  260. return -EINVAL;
  261. }
  262. }
  263. }
  264. if (initrd_len) {
  265. ret = fdt_setprop_u64(fdt, chosen_node,
  266. "linux,initrd-start",
  267. initrd_load_addr);
  268. if (ret < 0) {
  269. pr_err("Error setting up the new device tree.\n");
  270. return -EINVAL;
  271. }
  272. /* initrd-end is the first address after the initrd image. */
  273. ret = fdt_setprop_u64(fdt, chosen_node, "linux,initrd-end",
  274. initrd_load_addr + initrd_len);
  275. if (ret < 0) {
  276. pr_err("Error setting up the new device tree.\n");
  277. return -EINVAL;
  278. }
  279. ret = fdt_add_mem_rsv(fdt, initrd_load_addr, initrd_len);
  280. if (ret) {
  281. pr_err("Error reserving initrd memory: %s\n",
  282. fdt_strerror(ret));
  283. return -EINVAL;
  284. }
  285. }
  286. if (cmdline != NULL) {
  287. ret = fdt_setprop_string(fdt, chosen_node, "bootargs", cmdline);
  288. if (ret < 0) {
  289. pr_err("Error setting up the new device tree.\n");
  290. return -EINVAL;
  291. }
  292. } else {
  293. ret = fdt_delprop(fdt, chosen_node, "bootargs");
  294. if (ret && ret != -FDT_ERR_NOTFOUND) {
  295. pr_err("Error deleting bootargs.\n");
  296. return -EINVAL;
  297. }
  298. }
  299. ret = setup_ima_buffer(image, fdt, chosen_node);
  300. if (ret) {
  301. pr_err("Error setting up the new device tree.\n");
  302. return ret;
  303. }
  304. ret = fdt_setprop(fdt, chosen_node, "linux,booted-from-kexec", NULL, 0);
  305. if (ret) {
  306. pr_err("Error setting up the new device tree.\n");
  307. return -EINVAL;
  308. }
  309. return 0;
  310. }