machine_kexec.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. /*
  2. * kexec for arm64
  3. *
  4. * Copyright (C) Linaro.
  5. * Copyright (C) Huawei Futurewei Technologies.
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. */
  11. #include <linux/interrupt.h>
  12. #include <linux/irq.h>
  13. #include <linux/kernel.h>
  14. #include <linux/kexec.h>
  15. #include <linux/page-flags.h>
  16. #include <linux/smp.h>
  17. #include <asm/cacheflush.h>
  18. #include <asm/cpu_ops.h>
  19. #include <asm/daifflags.h>
  20. #include <asm/memory.h>
  21. #include <asm/mmu.h>
  22. #include <asm/mmu_context.h>
  23. #include <asm/page.h>
  24. #include "cpu-reset.h"
  25. /* Global variables for the arm64_relocate_new_kernel routine. */
  26. extern const unsigned char arm64_relocate_new_kernel[];
  27. extern const unsigned long arm64_relocate_new_kernel_size;
  28. /**
  29. * kexec_image_info - For debugging output.
  30. */
  31. #define kexec_image_info(_i) _kexec_image_info(__func__, __LINE__, _i)
  32. static void _kexec_image_info(const char *func, int line,
  33. const struct kimage *kimage)
  34. {
  35. unsigned long i;
  36. pr_debug("%s:%d:\n", func, line);
  37. pr_debug(" kexec kimage info:\n");
  38. pr_debug(" type: %d\n", kimage->type);
  39. pr_debug(" start: %lx\n", kimage->start);
  40. pr_debug(" head: %lx\n", kimage->head);
  41. pr_debug(" nr_segments: %lu\n", kimage->nr_segments);
  42. for (i = 0; i < kimage->nr_segments; i++) {
  43. pr_debug(" segment[%lu]: %016lx - %016lx, 0x%lx bytes, %lu pages\n",
  44. i,
  45. kimage->segment[i].mem,
  46. kimage->segment[i].mem + kimage->segment[i].memsz,
  47. kimage->segment[i].memsz,
  48. kimage->segment[i].memsz / PAGE_SIZE);
  49. }
  50. }
  51. void machine_kexec_cleanup(struct kimage *kimage)
  52. {
  53. /* Empty routine needed to avoid build errors. */
  54. }
  55. /**
  56. * machine_kexec_prepare - Prepare for a kexec reboot.
  57. *
  58. * Called from the core kexec code when a kernel image is loaded.
  59. * Forbid loading a kexec kernel if we have no way of hotplugging cpus or cpus
  60. * are stuck in the kernel. This avoids a panic once we hit machine_kexec().
  61. */
  62. int machine_kexec_prepare(struct kimage *kimage)
  63. {
  64. kexec_image_info(kimage);
  65. if (kimage->type != KEXEC_TYPE_CRASH && cpus_are_stuck_in_kernel()) {
  66. pr_err("Can't kexec: CPUs are stuck in the kernel.\n");
  67. return -EBUSY;
  68. }
  69. return 0;
  70. }
  71. /**
  72. * kexec_list_flush - Helper to flush the kimage list and source pages to PoC.
  73. */
  74. static void kexec_list_flush(struct kimage *kimage)
  75. {
  76. kimage_entry_t *entry;
  77. for (entry = &kimage->head; ; entry++) {
  78. unsigned int flag;
  79. void *addr;
  80. /* flush the list entries. */
  81. __flush_dcache_area(entry, sizeof(kimage_entry_t));
  82. flag = *entry & IND_FLAGS;
  83. if (flag == IND_DONE)
  84. break;
  85. addr = phys_to_virt(*entry & PAGE_MASK);
  86. switch (flag) {
  87. case IND_INDIRECTION:
  88. /* Set entry point just before the new list page. */
  89. entry = (kimage_entry_t *)addr - 1;
  90. break;
  91. case IND_SOURCE:
  92. /* flush the source pages. */
  93. __flush_dcache_area(addr, PAGE_SIZE);
  94. break;
  95. case IND_DESTINATION:
  96. break;
  97. default:
  98. BUG();
  99. }
  100. }
  101. }
  102. /**
  103. * kexec_segment_flush - Helper to flush the kimage segments to PoC.
  104. */
  105. static void kexec_segment_flush(const struct kimage *kimage)
  106. {
  107. unsigned long i;
  108. pr_debug("%s:\n", __func__);
  109. for (i = 0; i < kimage->nr_segments; i++) {
  110. pr_debug(" segment[%lu]: %016lx - %016lx, 0x%lx bytes, %lu pages\n",
  111. i,
  112. kimage->segment[i].mem,
  113. kimage->segment[i].mem + kimage->segment[i].memsz,
  114. kimage->segment[i].memsz,
  115. kimage->segment[i].memsz / PAGE_SIZE);
  116. __flush_dcache_area(phys_to_virt(kimage->segment[i].mem),
  117. kimage->segment[i].memsz);
  118. }
  119. }
  120. /**
  121. * machine_kexec - Do the kexec reboot.
  122. *
  123. * Called from the core kexec code for a sys_reboot with LINUX_REBOOT_CMD_KEXEC.
  124. */
  125. void machine_kexec(struct kimage *kimage)
  126. {
  127. phys_addr_t reboot_code_buffer_phys;
  128. void *reboot_code_buffer;
  129. bool in_kexec_crash = (kimage == kexec_crash_image);
  130. bool stuck_cpus = cpus_are_stuck_in_kernel();
  131. /*
  132. * New cpus may have become stuck_in_kernel after we loaded the image.
  133. */
  134. BUG_ON(!in_kexec_crash && (stuck_cpus || (num_online_cpus() > 1)));
  135. WARN(in_kexec_crash && (stuck_cpus || smp_crash_stop_failed()),
  136. "Some CPUs may be stale, kdump will be unreliable.\n");
  137. reboot_code_buffer_phys = page_to_phys(kimage->control_code_page);
  138. reboot_code_buffer = phys_to_virt(reboot_code_buffer_phys);
  139. kexec_image_info(kimage);
  140. pr_debug("%s:%d: control_code_page: %p\n", __func__, __LINE__,
  141. kimage->control_code_page);
  142. pr_debug("%s:%d: reboot_code_buffer_phys: %pa\n", __func__, __LINE__,
  143. &reboot_code_buffer_phys);
  144. pr_debug("%s:%d: reboot_code_buffer: %p\n", __func__, __LINE__,
  145. reboot_code_buffer);
  146. pr_debug("%s:%d: relocate_new_kernel: %p\n", __func__, __LINE__,
  147. arm64_relocate_new_kernel);
  148. pr_debug("%s:%d: relocate_new_kernel_size: 0x%lx(%lu) bytes\n",
  149. __func__, __LINE__, arm64_relocate_new_kernel_size,
  150. arm64_relocate_new_kernel_size);
  151. /*
  152. * Copy arm64_relocate_new_kernel to the reboot_code_buffer for use
  153. * after the kernel is shut down.
  154. */
  155. memcpy(reboot_code_buffer, arm64_relocate_new_kernel,
  156. arm64_relocate_new_kernel_size);
  157. /* Flush the reboot_code_buffer in preparation for its execution. */
  158. __flush_dcache_area(reboot_code_buffer, arm64_relocate_new_kernel_size);
  159. /*
  160. * Although we've killed off the secondary CPUs, we don't update
  161. * the online mask if we're handling a crash kernel and consequently
  162. * need to avoid flush_icache_range(), which will attempt to IPI
  163. * the offline CPUs. Therefore, we must use the __* variant here.
  164. */
  165. __flush_icache_range((uintptr_t)reboot_code_buffer,
  166. (uintptr_t)reboot_code_buffer +
  167. arm64_relocate_new_kernel_size);
  168. /* Flush the kimage list and its buffers. */
  169. kexec_list_flush(kimage);
  170. /* Flush the new image if already in place. */
  171. if ((kimage != kexec_crash_image) && (kimage->head & IND_DONE))
  172. kexec_segment_flush(kimage);
  173. pr_info("Bye!\n");
  174. local_daif_mask();
  175. /*
  176. * cpu_soft_restart will shutdown the MMU, disable data caches, then
  177. * transfer control to the reboot_code_buffer which contains a copy of
  178. * the arm64_relocate_new_kernel routine. arm64_relocate_new_kernel
  179. * uses physical addressing to relocate the new image to its final
  180. * position and transfers control to the image entry point when the
  181. * relocation is complete.
  182. */
  183. cpu_soft_restart(reboot_code_buffer_phys, kimage->head, kimage->start, 0);
  184. BUG(); /* Should never get here. */
  185. }
  186. static void machine_kexec_mask_interrupts(void)
  187. {
  188. unsigned int i;
  189. struct irq_desc *desc;
  190. for_each_irq_desc(i, desc) {
  191. struct irq_chip *chip;
  192. int ret;
  193. chip = irq_desc_get_chip(desc);
  194. if (!chip)
  195. continue;
  196. /*
  197. * First try to remove the active state. If this
  198. * fails, try to EOI the interrupt.
  199. */
  200. ret = irq_set_irqchip_state(i, IRQCHIP_STATE_ACTIVE, false);
  201. if (ret && irqd_irq_inprogress(&desc->irq_data) &&
  202. chip->irq_eoi)
  203. chip->irq_eoi(&desc->irq_data);
  204. if (chip->irq_mask)
  205. chip->irq_mask(&desc->irq_data);
  206. if (chip->irq_disable && !irqd_irq_disabled(&desc->irq_data))
  207. chip->irq_disable(&desc->irq_data);
  208. }
  209. }
  210. /**
  211. * machine_crash_shutdown - shutdown non-crashing cpus and save registers
  212. */
  213. void machine_crash_shutdown(struct pt_regs *regs)
  214. {
  215. local_irq_disable();
  216. /* shutdown non-crashing cpus */
  217. crash_smp_send_stop();
  218. /* for crashing cpu */
  219. crash_save_cpu(regs, smp_processor_id());
  220. machine_kexec_mask_interrupts();
  221. pr_info("Starting crashdump kernel...\n");
  222. }
  223. void arch_kexec_protect_crashkres(void)
  224. {
  225. int i;
  226. kexec_segment_flush(kexec_crash_image);
  227. for (i = 0; i < kexec_crash_image->nr_segments; i++)
  228. set_memory_valid(
  229. __phys_to_virt(kexec_crash_image->segment[i].mem),
  230. kexec_crash_image->segment[i].memsz >> PAGE_SHIFT, 0);
  231. }
  232. void arch_kexec_unprotect_crashkres(void)
  233. {
  234. int i;
  235. for (i = 0; i < kexec_crash_image->nr_segments; i++)
  236. set_memory_valid(
  237. __phys_to_virt(kexec_crash_image->segment[i].mem),
  238. kexec_crash_image->segment[i].memsz >> PAGE_SHIFT, 1);
  239. }
  240. #ifdef CONFIG_HIBERNATION
  241. /*
  242. * To preserve the crash dump kernel image, the relevant memory segments
  243. * should be mapped again around the hibernation.
  244. */
  245. void crash_prepare_suspend(void)
  246. {
  247. if (kexec_crash_image)
  248. arch_kexec_unprotect_crashkres();
  249. }
  250. void crash_post_resume(void)
  251. {
  252. if (kexec_crash_image)
  253. arch_kexec_protect_crashkres();
  254. }
  255. /*
  256. * crash_is_nosave
  257. *
  258. * Return true only if a page is part of reserved memory for crash dump kernel,
  259. * but does not hold any data of loaded kernel image.
  260. *
  261. * Note that all the pages in crash dump kernel memory have been initially
  262. * marked as Reserved in kexec_reserve_crashkres_pages().
  263. *
  264. * In hibernation, the pages which are Reserved and yet "nosave" are excluded
  265. * from the hibernation iamge. crash_is_nosave() does thich check for crash
  266. * dump kernel and will reduce the total size of hibernation image.
  267. */
  268. bool crash_is_nosave(unsigned long pfn)
  269. {
  270. int i;
  271. phys_addr_t addr;
  272. if (!crashk_res.end)
  273. return false;
  274. /* in reserved memory? */
  275. addr = __pfn_to_phys(pfn);
  276. if ((addr < crashk_res.start) || (crashk_res.end < addr))
  277. return false;
  278. if (!kexec_crash_image)
  279. return true;
  280. /* not part of loaded kernel image? */
  281. for (i = 0; i < kexec_crash_image->nr_segments; i++)
  282. if (addr >= kexec_crash_image->segment[i].mem &&
  283. addr < (kexec_crash_image->segment[i].mem +
  284. kexec_crash_image->segment[i].memsz))
  285. return false;
  286. return true;
  287. }
  288. void crash_free_reserved_phys_range(unsigned long begin, unsigned long end)
  289. {
  290. unsigned long addr;
  291. struct page *page;
  292. for (addr = begin; addr < end; addr += PAGE_SIZE) {
  293. page = phys_to_page(addr);
  294. ClearPageReserved(page);
  295. free_reserved_page(page);
  296. }
  297. }
  298. #endif /* CONFIG_HIBERNATION */