machine_kexec.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2019 FORTH-ICS/CARV
  4. * Nick Kossifidis <[email protected]>
  5. */
  6. #include <linux/kexec.h>
  7. #include <asm/kexec.h> /* For riscv_kexec_* symbol defines */
  8. #include <linux/smp.h> /* For smp_send_stop () */
  9. #include <asm/cacheflush.h> /* For local_flush_icache_all() */
  10. #include <asm/barrier.h> /* For smp_wmb() */
  11. #include <asm/page.h> /* For PAGE_MASK */
  12. #include <linux/libfdt.h> /* For fdt_check_header() */
  13. #include <asm/set_memory.h> /* For set_memory_x() */
  14. #include <linux/compiler.h> /* For unreachable() */
  15. #include <linux/cpu.h> /* For cpu_down() */
  16. #include <linux/reboot.h>
  17. #include <linux/interrupt.h>
  18. #include <linux/irq.h>
  19. /*
  20. * kexec_image_info - Print received image details
  21. */
  22. static void
  23. kexec_image_info(const struct kimage *image)
  24. {
  25. unsigned long i;
  26. pr_debug("Kexec image info:\n");
  27. pr_debug("\ttype: %d\n", image->type);
  28. pr_debug("\tstart: %lx\n", image->start);
  29. pr_debug("\thead: %lx\n", image->head);
  30. pr_debug("\tnr_segments: %lu\n", image->nr_segments);
  31. for (i = 0; i < image->nr_segments; i++) {
  32. pr_debug("\t segment[%lu]: %016lx - %016lx", i,
  33. image->segment[i].mem,
  34. image->segment[i].mem + image->segment[i].memsz);
  35. pr_debug("\t\t0x%lx bytes, %lu pages\n",
  36. (unsigned long) image->segment[i].memsz,
  37. (unsigned long) image->segment[i].memsz / PAGE_SIZE);
  38. }
  39. }
  40. /*
  41. * machine_kexec_prepare - Initialize kexec
  42. *
  43. * This function is called from do_kexec_load, when the user has
  44. * provided us with an image to be loaded. Its goal is to validate
  45. * the image and prepare the control code buffer as needed.
  46. * Note that kimage_alloc_init has already been called and the
  47. * control buffer has already been allocated.
  48. */
  49. int
  50. machine_kexec_prepare(struct kimage *image)
  51. {
  52. struct kimage_arch *internal = &image->arch;
  53. struct fdt_header fdt = {0};
  54. void *control_code_buffer = NULL;
  55. unsigned int control_code_buffer_sz = 0;
  56. int i = 0;
  57. kexec_image_info(image);
  58. /* Find the Flattened Device Tree and save its physical address */
  59. for (i = 0; i < image->nr_segments; i++) {
  60. if (image->segment[i].memsz <= sizeof(fdt))
  61. continue;
  62. if (image->file_mode)
  63. memcpy(&fdt, image->segment[i].buf, sizeof(fdt));
  64. else if (copy_from_user(&fdt, image->segment[i].buf, sizeof(fdt)))
  65. continue;
  66. if (fdt_check_header(&fdt))
  67. continue;
  68. internal->fdt_addr = (unsigned long) image->segment[i].mem;
  69. break;
  70. }
  71. if (!internal->fdt_addr) {
  72. pr_err("Device tree not included in the provided image\n");
  73. return -EINVAL;
  74. }
  75. /* Copy the assembler code for relocation to the control page */
  76. if (image->type != KEXEC_TYPE_CRASH) {
  77. control_code_buffer = page_address(image->control_code_page);
  78. control_code_buffer_sz = page_size(image->control_code_page);
  79. if (unlikely(riscv_kexec_relocate_size > control_code_buffer_sz)) {
  80. pr_err("Relocation code doesn't fit within a control page\n");
  81. return -EINVAL;
  82. }
  83. memcpy(control_code_buffer, riscv_kexec_relocate,
  84. riscv_kexec_relocate_size);
  85. /* Mark the control page executable */
  86. set_memory_x((unsigned long) control_code_buffer, 1);
  87. }
  88. return 0;
  89. }
  90. /*
  91. * machine_kexec_cleanup - Cleanup any leftovers from
  92. * machine_kexec_prepare
  93. *
  94. * This function is called by kimage_free to handle any arch-specific
  95. * allocations done on machine_kexec_prepare. Since we didn't do any
  96. * allocations there, this is just an empty function. Note that the
  97. * control buffer is freed by kimage_free.
  98. */
  99. void
  100. machine_kexec_cleanup(struct kimage *image)
  101. {
  102. }
  103. /*
  104. * machine_shutdown - Prepare for a kexec reboot
  105. *
  106. * This function is called by kernel_kexec just before machine_kexec
  107. * below. Its goal is to prepare the rest of the system (the other
  108. * harts and possibly devices etc) for a kexec reboot.
  109. */
  110. void machine_shutdown(void)
  111. {
  112. /*
  113. * No more interrupts on this hart
  114. * until we are back up.
  115. */
  116. local_irq_disable();
  117. #if defined(CONFIG_HOTPLUG_CPU)
  118. smp_shutdown_nonboot_cpus(smp_processor_id());
  119. #endif
  120. }
  121. static void machine_kexec_mask_interrupts(void)
  122. {
  123. unsigned int i;
  124. struct irq_desc *desc;
  125. for_each_irq_desc(i, desc) {
  126. struct irq_chip *chip;
  127. int ret;
  128. chip = irq_desc_get_chip(desc);
  129. if (!chip)
  130. continue;
  131. /*
  132. * First try to remove the active state. If this
  133. * fails, try to EOI the interrupt.
  134. */
  135. ret = irq_set_irqchip_state(i, IRQCHIP_STATE_ACTIVE, false);
  136. if (ret && irqd_irq_inprogress(&desc->irq_data) &&
  137. chip->irq_eoi)
  138. chip->irq_eoi(&desc->irq_data);
  139. if (chip->irq_mask)
  140. chip->irq_mask(&desc->irq_data);
  141. if (chip->irq_disable && !irqd_irq_disabled(&desc->irq_data))
  142. chip->irq_disable(&desc->irq_data);
  143. }
  144. }
  145. /*
  146. * machine_crash_shutdown - Prepare to kexec after a kernel crash
  147. *
  148. * This function is called by crash_kexec just before machine_kexec
  149. * and its goal is to shutdown non-crashing cpus and save registers.
  150. */
  151. void
  152. machine_crash_shutdown(struct pt_regs *regs)
  153. {
  154. local_irq_disable();
  155. /* shutdown non-crashing cpus */
  156. crash_smp_send_stop();
  157. crash_save_cpu(regs, smp_processor_id());
  158. machine_kexec_mask_interrupts();
  159. pr_info("Starting crashdump kernel...\n");
  160. }
  161. /*
  162. * machine_kexec - Jump to the loaded kimage
  163. *
  164. * This function is called by kernel_kexec which is called by the
  165. * reboot system call when the reboot cmd is LINUX_REBOOT_CMD_KEXEC,
  166. * or by crash_kernel which is called by the kernel's arch-specific
  167. * trap handler in case of a kernel panic. It's the final stage of
  168. * the kexec process where the pre-loaded kimage is ready to be
  169. * executed. We assume at this point that all other harts are
  170. * suspended and this hart will be the new boot hart.
  171. */
  172. void __noreturn
  173. machine_kexec(struct kimage *image)
  174. {
  175. struct kimage_arch *internal = &image->arch;
  176. unsigned long jump_addr = (unsigned long) image->start;
  177. unsigned long first_ind_entry = (unsigned long) &image->head;
  178. unsigned long this_cpu_id = __smp_processor_id();
  179. unsigned long this_hart_id = cpuid_to_hartid_map(this_cpu_id);
  180. unsigned long fdt_addr = internal->fdt_addr;
  181. void *control_code_buffer = page_address(image->control_code_page);
  182. riscv_kexec_method kexec_method = NULL;
  183. #ifdef CONFIG_SMP
  184. WARN(smp_crash_stop_failed(),
  185. "Some CPUs may be stale, kdump will be unreliable.\n");
  186. #endif
  187. if (image->type != KEXEC_TYPE_CRASH)
  188. kexec_method = control_code_buffer;
  189. else
  190. kexec_method = (riscv_kexec_method) &riscv_kexec_norelocate;
  191. pr_notice("Will call new kernel at %08lx from hart id %lx\n",
  192. jump_addr, this_hart_id);
  193. pr_notice("FDT image at %08lx\n", fdt_addr);
  194. /* Make sure the relocation code is visible to the hart */
  195. local_flush_icache_all();
  196. /* Jump to the relocation code */
  197. pr_notice("Bye...\n");
  198. kexec_method(first_ind_entry, jump_addr, fdt_addr,
  199. this_hart_id, kernel_map.va_pa_offset);
  200. unreachable();
  201. }