machine_kexec.c 5.8 KB

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