machine_kexec.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright IBM Corp. 2005, 2011
  4. *
  5. * Author(s): Rolf Adelsberger,
  6. * Michael Holzheu <holzheu@linux.vnet.ibm.com>
  7. */
  8. #include <linux/device.h>
  9. #include <linux/mm.h>
  10. #include <linux/kexec.h>
  11. #include <linux/delay.h>
  12. #include <linux/reboot.h>
  13. #include <linux/ftrace.h>
  14. #include <linux/debug_locks.h>
  15. #include <asm/guarded_storage.h>
  16. #include <asm/pfault.h>
  17. #include <asm/cio.h>
  18. #include <asm/fpu.h>
  19. #include <asm/setup.h>
  20. #include <asm/smp.h>
  21. #include <asm/ipl.h>
  22. #include <asm/diag.h>
  23. #include <asm/elf.h>
  24. #include <asm/asm-offsets.h>
  25. #include <asm/cacheflush.h>
  26. #include <asm/abs_lowcore.h>
  27. #include <asm/os_info.h>
  28. #include <asm/set_memory.h>
  29. #include <asm/stacktrace.h>
  30. #include <asm/nmi.h>
  31. #include <asm/sclp.h>
  32. typedef void (*relocate_kernel_t)(unsigned long, unsigned long, unsigned long);
  33. typedef int (*purgatory_t)(int);
  34. extern const unsigned char relocate_kernel[];
  35. extern const unsigned long long relocate_kernel_len;
  36. #ifdef CONFIG_CRASH_DUMP
  37. /*
  38. * Reset the system, copy boot CPU registers to absolute zero,
  39. * and jump to the kdump image
  40. */
  41. static void __do_machine_kdump(void *data)
  42. {
  43. struct kimage *image = data;
  44. purgatory_t purgatory;
  45. unsigned long prefix;
  46. purgatory = (purgatory_t)image->start;
  47. /* store_status() saved the prefix register to lowcore */
  48. prefix = (unsigned long)get_lowcore()->prefixreg_save_area;
  49. /* Now do the reset */
  50. s390_reset_system();
  51. /*
  52. * Copy dump CPU store status info to absolute zero.
  53. * This need to be done *after* s390_reset_system set the
  54. * prefix register of this CPU to zero
  55. */
  56. memcpy(absolute_pointer(get_lowcore()->floating_pt_save_area),
  57. phys_to_virt(prefix + __LC_FPREGS_SAVE_AREA), 512);
  58. call_nodat(1, int, purgatory, int, 1);
  59. /* Die if kdump returns */
  60. disabled_wait();
  61. }
  62. /*
  63. * Start kdump: create a LGR log entry, store status of all CPUs and
  64. * branch to __do_machine_kdump.
  65. */
  66. static noinline void __machine_kdump(void *image)
  67. {
  68. struct mcesa *mcesa;
  69. union ctlreg2 cr2_old, cr2_new;
  70. int this_cpu, cpu;
  71. lgr_info_log();
  72. /* Get status of the other CPUs */
  73. this_cpu = smp_find_processor_id(stap());
  74. for_each_online_cpu(cpu) {
  75. if (cpu == this_cpu)
  76. continue;
  77. if (smp_store_status(cpu))
  78. continue;
  79. }
  80. /* Store status of the boot CPU */
  81. mcesa = __va(get_lowcore()->mcesad & MCESA_ORIGIN_MASK);
  82. if (cpu_has_vx())
  83. save_vx_regs((__vector128 *) mcesa->vector_save_area);
  84. if (MACHINE_HAS_GS) {
  85. local_ctl_store(2, &cr2_old.reg);
  86. cr2_new = cr2_old;
  87. cr2_new.gse = 1;
  88. local_ctl_load(2, &cr2_new.reg);
  89. save_gs_cb((struct gs_cb *) mcesa->guarded_storage_save_area);
  90. local_ctl_load(2, &cr2_old.reg);
  91. }
  92. /*
  93. * To create a good backchain for this CPU in the dump store_status
  94. * is passed the address of a function. The address is saved into
  95. * the PSW save area of the boot CPU and the function is invoked as
  96. * a tail call of store_status. The backchain in the dump will look
  97. * like this:
  98. * restart_int_handler -> __machine_kexec -> __do_machine_kdump
  99. * The call to store_status() will not return.
  100. */
  101. store_status(__do_machine_kdump, image);
  102. }
  103. #endif /* CONFIG_CRASH_DUMP */
  104. /*
  105. * Check if kdump checksums are valid: We call purgatory with parameter "0"
  106. */
  107. static bool kdump_csum_valid(struct kimage *image)
  108. {
  109. #ifdef CONFIG_CRASH_DUMP
  110. purgatory_t purgatory = (purgatory_t)image->start;
  111. int rc;
  112. rc = call_nodat(1, int, purgatory, int, 0);
  113. return rc == 0;
  114. #else
  115. return false;
  116. #endif
  117. }
  118. #ifdef CONFIG_CRASH_DUMP
  119. void crash_free_reserved_phys_range(unsigned long begin, unsigned long end)
  120. {
  121. unsigned long addr, size;
  122. for (addr = begin; addr < end; addr += PAGE_SIZE)
  123. free_reserved_page(pfn_to_page(addr >> PAGE_SHIFT));
  124. size = begin - crashk_res.start;
  125. if (size)
  126. os_info_crashkernel_add(crashk_res.start, size);
  127. else
  128. os_info_crashkernel_add(0, 0);
  129. }
  130. static void crash_protect_pages(int protect)
  131. {
  132. unsigned long size;
  133. if (!crashk_res.end)
  134. return;
  135. size = resource_size(&crashk_res);
  136. if (protect)
  137. set_memory_ro(crashk_res.start, size >> PAGE_SHIFT);
  138. else
  139. set_memory_rw(crashk_res.start, size >> PAGE_SHIFT);
  140. }
  141. void arch_kexec_protect_crashkres(void)
  142. {
  143. crash_protect_pages(1);
  144. }
  145. void arch_kexec_unprotect_crashkres(void)
  146. {
  147. crash_protect_pages(0);
  148. }
  149. #endif
  150. /*
  151. * Give back memory to hypervisor before new kdump is loaded
  152. */
  153. static int machine_kexec_prepare_kdump(void)
  154. {
  155. #ifdef CONFIG_CRASH_DUMP
  156. if (MACHINE_IS_VM)
  157. diag10_range(PFN_DOWN(crashk_res.start),
  158. PFN_DOWN(crashk_res.end - crashk_res.start + 1));
  159. return 0;
  160. #else
  161. return -EINVAL;
  162. #endif
  163. }
  164. int machine_kexec_prepare(struct kimage *image)
  165. {
  166. void *reboot_code_buffer;
  167. if (image->type == KEXEC_TYPE_CRASH)
  168. return machine_kexec_prepare_kdump();
  169. /* We don't support anything but the default image type for now. */
  170. if (image->type != KEXEC_TYPE_DEFAULT)
  171. return -EINVAL;
  172. /* Get the destination where the assembler code should be copied to.*/
  173. reboot_code_buffer = page_to_virt(image->control_code_page);
  174. /* Then copy it */
  175. memcpy(reboot_code_buffer, relocate_kernel, relocate_kernel_len);
  176. return 0;
  177. }
  178. void machine_kexec_cleanup(struct kimage *image)
  179. {
  180. }
  181. void machine_shutdown(void)
  182. {
  183. }
  184. void machine_crash_shutdown(struct pt_regs *regs)
  185. {
  186. set_os_info_reipl_block();
  187. }
  188. /*
  189. * Do normal kexec
  190. */
  191. static void __do_machine_kexec(void *data)
  192. {
  193. unsigned long data_mover, entry, diag308_subcode;
  194. struct kimage *image = data;
  195. data_mover = page_to_phys(image->control_code_page);
  196. entry = virt_to_phys(&image->head);
  197. diag308_subcode = DIAG308_CLEAR_RESET;
  198. if (sclp.has_iplcc)
  199. diag308_subcode |= DIAG308_FLAG_EI;
  200. s390_reset_system();
  201. call_nodat(3, void, (relocate_kernel_t)data_mover,
  202. unsigned long, entry,
  203. unsigned long, image->start,
  204. unsigned long, diag308_subcode);
  205. /* Die if kexec returns */
  206. disabled_wait();
  207. }
  208. /*
  209. * Reset system and call either kdump or normal kexec
  210. */
  211. static void __machine_kexec(void *data)
  212. {
  213. pfault_fini();
  214. tracing_off();
  215. debug_locks_off();
  216. #ifdef CONFIG_CRASH_DUMP
  217. if (((struct kimage *) data)->type == KEXEC_TYPE_CRASH)
  218. __machine_kdump(data);
  219. #endif
  220. __do_machine_kexec(data);
  221. }
  222. /*
  223. * Do either kdump or normal kexec. In case of kdump we first ask
  224. * purgatory, if kdump checksums are valid.
  225. */
  226. void machine_kexec(struct kimage *image)
  227. {
  228. if (image->type == KEXEC_TYPE_CRASH && !kdump_csum_valid(image))
  229. return;
  230. tracer_disable();
  231. smp_send_stop();
  232. smp_call_ipl_cpu(__machine_kexec, image);
  233. }