machine_kexec.c 6.5 KB

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