machine_kexec.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. /*
  2. * Copyright IBM Corp. 2005, 2011
  3. *
  4. * Author(s): Rolf Adelsberger,
  5. * Heiko Carstens <heiko.carstens@de.ibm.com>
  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 <linux/suspend.h>
  16. #include <asm/cio.h>
  17. #include <asm/setup.h>
  18. #include <asm/pgtable.h>
  19. #include <asm/pgalloc.h>
  20. #include <asm/smp.h>
  21. #include <asm/reset.h>
  22. #include <asm/ipl.h>
  23. #include <asm/diag.h>
  24. #include <asm/elf.h>
  25. #include <asm/asm-offsets.h>
  26. #include <asm/cacheflush.h>
  27. #include <asm/os_info.h>
  28. #include <asm/switch_to.h>
  29. typedef void (*relocate_kernel_t)(kimage_entry_t *, unsigned long);
  30. extern const unsigned char relocate_kernel[];
  31. extern const unsigned long long relocate_kernel_len;
  32. #ifdef CONFIG_CRASH_DUMP
  33. /*
  34. * PM notifier callback for kdump
  35. */
  36. static int machine_kdump_pm_cb(struct notifier_block *nb, unsigned long action,
  37. void *ptr)
  38. {
  39. switch (action) {
  40. case PM_SUSPEND_PREPARE:
  41. case PM_HIBERNATION_PREPARE:
  42. if (kexec_crash_image)
  43. arch_kexec_unprotect_crashkres();
  44. break;
  45. case PM_POST_SUSPEND:
  46. case PM_POST_HIBERNATION:
  47. if (kexec_crash_image)
  48. arch_kexec_protect_crashkres();
  49. break;
  50. default:
  51. return NOTIFY_DONE;
  52. }
  53. return NOTIFY_OK;
  54. }
  55. static int __init machine_kdump_pm_init(void)
  56. {
  57. pm_notifier(machine_kdump_pm_cb, 0);
  58. return 0;
  59. }
  60. arch_initcall(machine_kdump_pm_init);
  61. /*
  62. * Reset the system, copy boot CPU registers to absolute zero,
  63. * and jump to the kdump image
  64. */
  65. static void __do_machine_kdump(void *image)
  66. {
  67. int (*start_kdump)(int);
  68. unsigned long prefix;
  69. /* store_status() saved the prefix register to lowcore */
  70. prefix = (unsigned long) S390_lowcore.prefixreg_save_area;
  71. /* Now do the reset */
  72. s390_reset_system();
  73. /*
  74. * Copy dump CPU store status info to absolute zero.
  75. * This need to be done *after* s390_reset_system set the
  76. * prefix register of this CPU to zero
  77. */
  78. memcpy((void *) __LC_FPREGS_SAVE_AREA,
  79. (void *)(prefix + __LC_FPREGS_SAVE_AREA), 512);
  80. __load_psw_mask(PSW_MASK_BASE | PSW_DEFAULT_KEY | PSW_MASK_EA | PSW_MASK_BA);
  81. start_kdump = (void *)((struct kimage *) image)->start;
  82. start_kdump(1);
  83. /* Die if start_kdump returns */
  84. disabled_wait((unsigned long) __builtin_return_address(0));
  85. }
  86. /*
  87. * Start kdump: create a LGR log entry, store status of all CPUs and
  88. * branch to __do_machine_kdump.
  89. */
  90. static noinline void __machine_kdump(void *image)
  91. {
  92. int this_cpu, cpu;
  93. lgr_info_log();
  94. /* Get status of the other CPUs */
  95. this_cpu = smp_find_processor_id(stap());
  96. for_each_online_cpu(cpu) {
  97. if (cpu == this_cpu)
  98. continue;
  99. if (smp_store_status(cpu))
  100. continue;
  101. }
  102. /* Store status of the boot CPU */
  103. if (MACHINE_HAS_VX)
  104. save_vx_regs((void *) &S390_lowcore.vector_save_area);
  105. /*
  106. * To create a good backchain for this CPU in the dump store_status
  107. * is passed the address of a function. The address is saved into
  108. * the PSW save area of the boot CPU and the function is invoked as
  109. * a tail call of store_status. The backchain in the dump will look
  110. * like this:
  111. * restart_int_handler -> __machine_kexec -> __do_machine_kdump
  112. * The call to store_status() will not return.
  113. */
  114. store_status(__do_machine_kdump, image);
  115. }
  116. #endif
  117. /*
  118. * Check if kdump checksums are valid: We call purgatory with parameter "0"
  119. */
  120. static int kdump_csum_valid(struct kimage *image)
  121. {
  122. #ifdef CONFIG_CRASH_DUMP
  123. int (*start_kdump)(int) = (void *)image->start;
  124. int rc;
  125. __arch_local_irq_stnsm(0xfb); /* disable DAT */
  126. rc = start_kdump(0);
  127. __arch_local_irq_stosm(0x04); /* enable DAT */
  128. return rc ? 0 : -EINVAL;
  129. #else
  130. return -EINVAL;
  131. #endif
  132. }
  133. #ifdef CONFIG_CRASH_DUMP
  134. void crash_free_reserved_phys_range(unsigned long begin, unsigned long end)
  135. {
  136. unsigned long addr, size;
  137. for (addr = begin; addr < end; addr += PAGE_SIZE)
  138. free_reserved_page(pfn_to_page(addr >> PAGE_SHIFT));
  139. size = begin - crashk_res.start;
  140. if (size)
  141. os_info_crashkernel_add(crashk_res.start, size);
  142. else
  143. os_info_crashkernel_add(0, 0);
  144. }
  145. static void crash_protect_pages(int protect)
  146. {
  147. unsigned long size;
  148. if (!crashk_res.end)
  149. return;
  150. size = resource_size(&crashk_res);
  151. if (protect)
  152. set_memory_ro(crashk_res.start, size >> PAGE_SHIFT);
  153. else
  154. set_memory_rw(crashk_res.start, size >> PAGE_SHIFT);
  155. }
  156. void arch_kexec_protect_crashkres(void)
  157. {
  158. crash_protect_pages(1);
  159. }
  160. void arch_kexec_unprotect_crashkres(void)
  161. {
  162. crash_protect_pages(0);
  163. }
  164. #endif
  165. /*
  166. * Give back memory to hypervisor before new kdump is loaded
  167. */
  168. static int machine_kexec_prepare_kdump(void)
  169. {
  170. #ifdef CONFIG_CRASH_DUMP
  171. if (MACHINE_IS_VM)
  172. diag10_range(PFN_DOWN(crashk_res.start),
  173. PFN_DOWN(crashk_res.end - crashk_res.start + 1));
  174. return 0;
  175. #else
  176. return -EINVAL;
  177. #endif
  178. }
  179. int machine_kexec_prepare(struct kimage *image)
  180. {
  181. void *reboot_code_buffer;
  182. /* Can't replace kernel image since it is read-only. */
  183. if (ipl_flags & IPL_NSS_VALID)
  184. return -EOPNOTSUPP;
  185. if (image->type == KEXEC_TYPE_CRASH)
  186. return machine_kexec_prepare_kdump();
  187. /* We don't support anything but the default image type for now. */
  188. if (image->type != KEXEC_TYPE_DEFAULT)
  189. return -EINVAL;
  190. /* Get the destination where the assembler code should be copied to.*/
  191. reboot_code_buffer = (void *) page_to_phys(image->control_code_page);
  192. /* Then copy it */
  193. memcpy(reboot_code_buffer, relocate_kernel, relocate_kernel_len);
  194. return 0;
  195. }
  196. void machine_kexec_cleanup(struct kimage *image)
  197. {
  198. }
  199. void arch_crash_save_vmcoreinfo(void)
  200. {
  201. VMCOREINFO_SYMBOL(lowcore_ptr);
  202. VMCOREINFO_SYMBOL(high_memory);
  203. VMCOREINFO_LENGTH(lowcore_ptr, NR_CPUS);
  204. }
  205. void machine_shutdown(void)
  206. {
  207. }
  208. void machine_crash_shutdown(struct pt_regs *regs)
  209. {
  210. }
  211. /*
  212. * Do normal kexec
  213. */
  214. static void __do_machine_kexec(void *data)
  215. {
  216. relocate_kernel_t data_mover;
  217. struct kimage *image = data;
  218. s390_reset_system();
  219. data_mover = (relocate_kernel_t) page_to_phys(image->control_code_page);
  220. /* Call the moving routine */
  221. (*data_mover)(&image->head, image->start);
  222. /* Die if kexec returns */
  223. disabled_wait((unsigned long) __builtin_return_address(0));
  224. }
  225. /*
  226. * Reset system and call either kdump or normal kexec
  227. */
  228. static void __machine_kexec(void *data)
  229. {
  230. __arch_local_irq_stosm(0x04); /* enable DAT */
  231. pfault_fini();
  232. tracing_off();
  233. debug_locks_off();
  234. #ifdef CONFIG_CRASH_DUMP
  235. if (((struct kimage *) data)->type == KEXEC_TYPE_CRASH)
  236. __machine_kdump(data);
  237. #endif
  238. __do_machine_kexec(data);
  239. }
  240. /*
  241. * Do either kdump or normal kexec. In case of kdump we first ask
  242. * purgatory, if kdump checksums are valid.
  243. */
  244. void machine_kexec(struct kimage *image)
  245. {
  246. if (image->type == KEXEC_TYPE_CRASH && !kdump_csum_valid(image))
  247. return;
  248. tracer_disable();
  249. smp_send_stop();
  250. smp_call_ipl_cpu(__machine_kexec, image);
  251. }