machine_kexec.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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/os_info.h>
  27. typedef void (*relocate_kernel_t)(kimage_entry_t *, unsigned long);
  28. extern const unsigned char relocate_kernel[];
  29. extern const unsigned long long relocate_kernel_len;
  30. #ifdef CONFIG_CRASH_DUMP
  31. /*
  32. * Create ELF notes for one CPU
  33. */
  34. static void add_elf_notes(int cpu)
  35. {
  36. struct save_area *sa = (void *) 4608 + store_prefix();
  37. void *ptr;
  38. memcpy((void *) (4608UL + sa->pref_reg), sa, sizeof(*sa));
  39. ptr = (u64 *) per_cpu_ptr(crash_notes, cpu);
  40. ptr = fill_cpu_elf_notes(ptr, sa);
  41. memset(ptr, 0, sizeof(struct elf_note));
  42. }
  43. /*
  44. * Initialize CPU ELF notes
  45. */
  46. void setup_regs(void)
  47. {
  48. unsigned long sa = S390_lowcore.prefixreg_save_area + SAVE_AREA_BASE;
  49. int cpu, this_cpu;
  50. this_cpu = smp_find_processor_id(stap());
  51. add_elf_notes(this_cpu);
  52. for_each_online_cpu(cpu) {
  53. if (cpu == this_cpu)
  54. continue;
  55. if (smp_store_status(cpu))
  56. continue;
  57. add_elf_notes(cpu);
  58. }
  59. /* Copy dump CPU store status info to absolute zero */
  60. memcpy((void *) SAVE_AREA_BASE, (void *) sa, sizeof(struct save_area));
  61. }
  62. /*
  63. * PM notifier callback for kdump
  64. */
  65. static int machine_kdump_pm_cb(struct notifier_block *nb, unsigned long action,
  66. void *ptr)
  67. {
  68. switch (action) {
  69. case PM_SUSPEND_PREPARE:
  70. case PM_HIBERNATION_PREPARE:
  71. if (crashk_res.start)
  72. crash_map_reserved_pages();
  73. break;
  74. case PM_POST_SUSPEND:
  75. case PM_POST_HIBERNATION:
  76. if (crashk_res.start)
  77. crash_unmap_reserved_pages();
  78. break;
  79. default:
  80. return NOTIFY_DONE;
  81. }
  82. return NOTIFY_OK;
  83. }
  84. static int __init machine_kdump_pm_init(void)
  85. {
  86. pm_notifier(machine_kdump_pm_cb, 0);
  87. return 0;
  88. }
  89. arch_initcall(machine_kdump_pm_init);
  90. #endif
  91. /*
  92. * Start kdump: We expect here that a store status has been done on our CPU
  93. */
  94. static void __do_machine_kdump(void *image)
  95. {
  96. #ifdef CONFIG_CRASH_DUMP
  97. int (*start_kdump)(int) = (void *)((struct kimage *) image)->start;
  98. setup_regs();
  99. __load_psw_mask(PSW_MASK_BASE | PSW_DEFAULT_KEY | PSW_MASK_EA | PSW_MASK_BA);
  100. start_kdump(1);
  101. #endif
  102. }
  103. /*
  104. * Check if kdump checksums are valid: We call purgatory with parameter "0"
  105. */
  106. static int kdump_csum_valid(struct kimage *image)
  107. {
  108. #ifdef CONFIG_CRASH_DUMP
  109. int (*start_kdump)(int) = (void *)image->start;
  110. int rc;
  111. __arch_local_irq_stnsm(0xfb); /* disable DAT */
  112. rc = start_kdump(0);
  113. __arch_local_irq_stosm(0x04); /* enable DAT */
  114. return rc ? 0 : -EINVAL;
  115. #else
  116. return -EINVAL;
  117. #endif
  118. }
  119. /*
  120. * Map or unmap crashkernel memory
  121. */
  122. static void crash_map_pages(int enable)
  123. {
  124. unsigned long size = resource_size(&crashk_res);
  125. BUG_ON(crashk_res.start % KEXEC_CRASH_MEM_ALIGN ||
  126. size % KEXEC_CRASH_MEM_ALIGN);
  127. if (enable)
  128. vmem_add_mapping(crashk_res.start, size);
  129. else {
  130. vmem_remove_mapping(crashk_res.start, size);
  131. if (size)
  132. os_info_crashkernel_add(crashk_res.start, size);
  133. else
  134. os_info_crashkernel_add(0, 0);
  135. }
  136. }
  137. /*
  138. * Map crashkernel memory
  139. */
  140. void crash_map_reserved_pages(void)
  141. {
  142. crash_map_pages(1);
  143. }
  144. /*
  145. * Unmap crashkernel memory
  146. */
  147. void crash_unmap_reserved_pages(void)
  148. {
  149. crash_map_pages(0);
  150. }
  151. /*
  152. * Give back memory to hypervisor before new kdump is loaded
  153. */
  154. static int machine_kexec_prepare_kdump(void)
  155. {
  156. #ifdef CONFIG_CRASH_DUMP
  157. if (MACHINE_IS_VM)
  158. diag10_range(PFN_DOWN(crashk_res.start),
  159. PFN_DOWN(crashk_res.end - crashk_res.start + 1));
  160. return 0;
  161. #else
  162. return -EINVAL;
  163. #endif
  164. }
  165. int machine_kexec_prepare(struct kimage *image)
  166. {
  167. void *reboot_code_buffer;
  168. /* Can't replace kernel image since it is read-only. */
  169. if (ipl_flags & IPL_NSS_VALID)
  170. return -EOPNOTSUPP;
  171. if (image->type == KEXEC_TYPE_CRASH)
  172. return machine_kexec_prepare_kdump();
  173. /* We don't support anything but the default image type for now. */
  174. if (image->type != KEXEC_TYPE_DEFAULT)
  175. return -EINVAL;
  176. /* Get the destination where the assembler code should be copied to.*/
  177. reboot_code_buffer = (void *) page_to_phys(image->control_code_page);
  178. /* Then copy it */
  179. memcpy(reboot_code_buffer, relocate_kernel, relocate_kernel_len);
  180. return 0;
  181. }
  182. void machine_kexec_cleanup(struct kimage *image)
  183. {
  184. }
  185. void arch_crash_save_vmcoreinfo(void)
  186. {
  187. VMCOREINFO_SYMBOL(lowcore_ptr);
  188. VMCOREINFO_SYMBOL(high_memory);
  189. VMCOREINFO_LENGTH(lowcore_ptr, NR_CPUS);
  190. }
  191. void machine_shutdown(void)
  192. {
  193. }
  194. void machine_crash_shutdown(struct pt_regs *regs)
  195. {
  196. }
  197. /*
  198. * Do normal kexec
  199. */
  200. static void __do_machine_kexec(void *data)
  201. {
  202. relocate_kernel_t data_mover;
  203. struct kimage *image = data;
  204. data_mover = (relocate_kernel_t) page_to_phys(image->control_code_page);
  205. /* Call the moving routine */
  206. (*data_mover)(&image->head, image->start);
  207. }
  208. /*
  209. * Reset system and call either kdump or normal kexec
  210. */
  211. static void __machine_kexec(void *data)
  212. {
  213. struct kimage *image = data;
  214. __arch_local_irq_stosm(0x04); /* enable DAT */
  215. pfault_fini();
  216. tracing_off();
  217. debug_locks_off();
  218. if (image->type == KEXEC_TYPE_CRASH) {
  219. lgr_info_log();
  220. s390_reset_system(__do_machine_kdump, data);
  221. } else {
  222. s390_reset_system(__do_machine_kexec, data);
  223. }
  224. disabled_wait((unsigned long) __builtin_return_address(0));
  225. }
  226. /*
  227. * Do either kdump or normal kexec. In case of kdump we first ask
  228. * purgatory, if kdump checksums are valid.
  229. */
  230. void machine_kexec(struct kimage *image)
  231. {
  232. if (image->type == KEXEC_TYPE_CRASH && !kdump_csum_valid(image))
  233. return;
  234. tracer_disable();
  235. smp_send_stop();
  236. smp_call_ipl_cpu(__machine_kexec, image);
  237. }