machine_kexec.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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/kexec.h>
  12. #include <linux/smp.h>
  13. #include <asm/cacheflush.h>
  14. #include <asm/cpu_ops.h>
  15. #include <asm/mmu_context.h>
  16. #include "cpu-reset.h"
  17. /* Global variables for the arm64_relocate_new_kernel routine. */
  18. extern const unsigned char arm64_relocate_new_kernel[];
  19. extern const unsigned long arm64_relocate_new_kernel_size;
  20. static unsigned long kimage_start;
  21. /**
  22. * kexec_image_info - For debugging output.
  23. */
  24. #define kexec_image_info(_i) _kexec_image_info(__func__, __LINE__, _i)
  25. static void _kexec_image_info(const char *func, int line,
  26. const struct kimage *kimage)
  27. {
  28. unsigned long i;
  29. pr_debug("%s:%d:\n", func, line);
  30. pr_debug(" kexec kimage info:\n");
  31. pr_debug(" type: %d\n", kimage->type);
  32. pr_debug(" start: %lx\n", kimage->start);
  33. pr_debug(" head: %lx\n", kimage->head);
  34. pr_debug(" nr_segments: %lu\n", kimage->nr_segments);
  35. for (i = 0; i < kimage->nr_segments; i++) {
  36. pr_debug(" segment[%lu]: %016lx - %016lx, 0x%lx bytes, %lu pages\n",
  37. i,
  38. kimage->segment[i].mem,
  39. kimage->segment[i].mem + kimage->segment[i].memsz,
  40. kimage->segment[i].memsz,
  41. kimage->segment[i].memsz / PAGE_SIZE);
  42. }
  43. }
  44. void machine_kexec_cleanup(struct kimage *kimage)
  45. {
  46. /* Empty routine needed to avoid build errors. */
  47. }
  48. /**
  49. * machine_kexec_prepare - Prepare for a kexec reboot.
  50. *
  51. * Called from the core kexec code when a kernel image is loaded.
  52. * Forbid loading a kexec kernel if we have no way of hotplugging cpus or cpus
  53. * are stuck in the kernel. This avoids a panic once we hit machine_kexec().
  54. */
  55. int machine_kexec_prepare(struct kimage *kimage)
  56. {
  57. kimage_start = kimage->start;
  58. kexec_image_info(kimage);
  59. if (kimage->type != KEXEC_TYPE_CRASH && cpus_are_stuck_in_kernel()) {
  60. pr_err("Can't kexec: CPUs are stuck in the kernel.\n");
  61. return -EBUSY;
  62. }
  63. return 0;
  64. }
  65. /**
  66. * kexec_list_flush - Helper to flush the kimage list and source pages to PoC.
  67. */
  68. static void kexec_list_flush(struct kimage *kimage)
  69. {
  70. kimage_entry_t *entry;
  71. for (entry = &kimage->head; ; entry++) {
  72. unsigned int flag;
  73. void *addr;
  74. /* flush the list entries. */
  75. __flush_dcache_area(entry, sizeof(kimage_entry_t));
  76. flag = *entry & IND_FLAGS;
  77. if (flag == IND_DONE)
  78. break;
  79. addr = phys_to_virt(*entry & PAGE_MASK);
  80. switch (flag) {
  81. case IND_INDIRECTION:
  82. /* Set entry point just before the new list page. */
  83. entry = (kimage_entry_t *)addr - 1;
  84. break;
  85. case IND_SOURCE:
  86. /* flush the source pages. */
  87. __flush_dcache_area(addr, PAGE_SIZE);
  88. break;
  89. case IND_DESTINATION:
  90. break;
  91. default:
  92. BUG();
  93. }
  94. }
  95. }
  96. /**
  97. * kexec_segment_flush - Helper to flush the kimage segments to PoC.
  98. */
  99. static void kexec_segment_flush(const struct kimage *kimage)
  100. {
  101. unsigned long i;
  102. pr_debug("%s:\n", __func__);
  103. for (i = 0; i < kimage->nr_segments; i++) {
  104. pr_debug(" segment[%lu]: %016lx - %016lx, 0x%lx bytes, %lu pages\n",
  105. i,
  106. kimage->segment[i].mem,
  107. kimage->segment[i].mem + kimage->segment[i].memsz,
  108. kimage->segment[i].memsz,
  109. kimage->segment[i].memsz / PAGE_SIZE);
  110. __flush_dcache_area(phys_to_virt(kimage->segment[i].mem),
  111. kimage->segment[i].memsz);
  112. }
  113. }
  114. /**
  115. * machine_kexec - Do the kexec reboot.
  116. *
  117. * Called from the core kexec code for a sys_reboot with LINUX_REBOOT_CMD_KEXEC.
  118. */
  119. void machine_kexec(struct kimage *kimage)
  120. {
  121. phys_addr_t reboot_code_buffer_phys;
  122. void *reboot_code_buffer;
  123. /*
  124. * New cpus may have become stuck_in_kernel after we loaded the image.
  125. */
  126. BUG_ON(cpus_are_stuck_in_kernel() || (num_online_cpus() > 1));
  127. reboot_code_buffer_phys = page_to_phys(kimage->control_code_page);
  128. reboot_code_buffer = phys_to_virt(reboot_code_buffer_phys);
  129. kexec_image_info(kimage);
  130. pr_debug("%s:%d: control_code_page: %p\n", __func__, __LINE__,
  131. kimage->control_code_page);
  132. pr_debug("%s:%d: reboot_code_buffer_phys: %pa\n", __func__, __LINE__,
  133. &reboot_code_buffer_phys);
  134. pr_debug("%s:%d: reboot_code_buffer: %p\n", __func__, __LINE__,
  135. reboot_code_buffer);
  136. pr_debug("%s:%d: relocate_new_kernel: %p\n", __func__, __LINE__,
  137. arm64_relocate_new_kernel);
  138. pr_debug("%s:%d: relocate_new_kernel_size: 0x%lx(%lu) bytes\n",
  139. __func__, __LINE__, arm64_relocate_new_kernel_size,
  140. arm64_relocate_new_kernel_size);
  141. /*
  142. * Copy arm64_relocate_new_kernel to the reboot_code_buffer for use
  143. * after the kernel is shut down.
  144. */
  145. memcpy(reboot_code_buffer, arm64_relocate_new_kernel,
  146. arm64_relocate_new_kernel_size);
  147. /* Flush the reboot_code_buffer in preparation for its execution. */
  148. __flush_dcache_area(reboot_code_buffer, arm64_relocate_new_kernel_size);
  149. flush_icache_range((uintptr_t)reboot_code_buffer,
  150. arm64_relocate_new_kernel_size);
  151. /* Flush the kimage list and its buffers. */
  152. kexec_list_flush(kimage);
  153. /* Flush the new image if already in place. */
  154. if (kimage->head & IND_DONE)
  155. kexec_segment_flush(kimage);
  156. pr_info("Bye!\n");
  157. /* Disable all DAIF exceptions. */
  158. asm volatile ("msr daifset, #0xf" : : : "memory");
  159. /*
  160. * cpu_soft_restart will shutdown the MMU, disable data caches, then
  161. * transfer control to the reboot_code_buffer which contains a copy of
  162. * the arm64_relocate_new_kernel routine. arm64_relocate_new_kernel
  163. * uses physical addressing to relocate the new image to its final
  164. * position and transfers control to the image entry point when the
  165. * relocation is complete.
  166. */
  167. cpu_soft_restart(1, reboot_code_buffer_phys, kimage->head,
  168. kimage_start, 0);
  169. BUG(); /* Should never get here. */
  170. }
  171. void machine_crash_shutdown(struct pt_regs *regs)
  172. {
  173. /* Empty routine needed to avoid build errors. */
  174. }