machine_kexec.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /*
  2. * arch/s390/kernel/machine_kexec.c
  3. *
  4. * Copyright IBM Corp. 2005,2011
  5. *
  6. * Author(s): Rolf Adelsberger,
  7. * Heiko Carstens <heiko.carstens@de.ibm.com>
  8. * Michael Holzheu <holzheu@linux.vnet.ibm.com>
  9. */
  10. #include <linux/device.h>
  11. #include <linux/mm.h>
  12. #include <linux/kexec.h>
  13. #include <linux/delay.h>
  14. #include <linux/reboot.h>
  15. #include <linux/ftrace.h>
  16. #include <linux/debug_locks.h>
  17. #include <asm/cio.h>
  18. #include <asm/setup.h>
  19. #include <asm/pgtable.h>
  20. #include <asm/pgalloc.h>
  21. #include <asm/smp.h>
  22. #include <asm/reset.h>
  23. #include <asm/ipl.h>
  24. #include <asm/diag.h>
  25. #include <asm/asm-offsets.h>
  26. typedef void (*relocate_kernel_t)(kimage_entry_t *, unsigned long);
  27. extern const unsigned char relocate_kernel[];
  28. extern const unsigned long long relocate_kernel_len;
  29. #ifdef CONFIG_CRASH_DUMP
  30. void *fill_cpu_elf_notes(void *ptr, struct save_area *sa);
  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. #endif
  63. /*
  64. * Start kdump: We expect here that a store status has been done on our CPU
  65. */
  66. static void __do_machine_kdump(void *image)
  67. {
  68. #ifdef CONFIG_CRASH_DUMP
  69. int (*start_kdump)(int) = (void *)((struct kimage *) image)->start;
  70. __load_psw_mask(PSW_MASK_BASE | PSW_DEFAULT_KEY | PSW_MASK_EA | PSW_MASK_BA);
  71. setup_regs();
  72. start_kdump(1);
  73. #endif
  74. }
  75. /*
  76. * Check if kdump checksums are valid: We call purgatory with parameter "0"
  77. */
  78. static int kdump_csum_valid(struct kimage *image)
  79. {
  80. #ifdef CONFIG_CRASH_DUMP
  81. int (*start_kdump)(int) = (void *)image->start;
  82. int rc;
  83. __arch_local_irq_stnsm(0xfb); /* disable DAT */
  84. rc = start_kdump(0);
  85. __arch_local_irq_stosm(0x04); /* enable DAT */
  86. return rc ? 0 : -EINVAL;
  87. #else
  88. return -EINVAL;
  89. #endif
  90. }
  91. /*
  92. * Map or unmap crashkernel memory
  93. */
  94. static void crash_map_pages(int enable)
  95. {
  96. unsigned long size = resource_size(&crashk_res);
  97. BUG_ON(crashk_res.start % KEXEC_CRASH_MEM_ALIGN ||
  98. size % KEXEC_CRASH_MEM_ALIGN);
  99. if (enable)
  100. vmem_add_mapping(crashk_res.start, size);
  101. else
  102. vmem_remove_mapping(crashk_res.start, size);
  103. }
  104. /*
  105. * Map crashkernel memory
  106. */
  107. void crash_map_reserved_pages(void)
  108. {
  109. crash_map_pages(1);
  110. }
  111. /*
  112. * Unmap crashkernel memory
  113. */
  114. void crash_unmap_reserved_pages(void)
  115. {
  116. crash_map_pages(0);
  117. }
  118. /*
  119. * Give back memory to hypervisor before new kdump is loaded
  120. */
  121. static int machine_kexec_prepare_kdump(void)
  122. {
  123. #ifdef CONFIG_CRASH_DUMP
  124. if (MACHINE_IS_VM)
  125. diag10_range(PFN_DOWN(crashk_res.start),
  126. PFN_DOWN(crashk_res.end - crashk_res.start + 1));
  127. return 0;
  128. #else
  129. return -EINVAL;
  130. #endif
  131. }
  132. int machine_kexec_prepare(struct kimage *image)
  133. {
  134. void *reboot_code_buffer;
  135. /* Can't replace kernel image since it is read-only. */
  136. if (ipl_flags & IPL_NSS_VALID)
  137. return -ENOSYS;
  138. if (image->type == KEXEC_TYPE_CRASH)
  139. return machine_kexec_prepare_kdump();
  140. /* We don't support anything but the default image type for now. */
  141. if (image->type != KEXEC_TYPE_DEFAULT)
  142. return -EINVAL;
  143. /* Get the destination where the assembler code should be copied to.*/
  144. reboot_code_buffer = (void *) page_to_phys(image->control_code_page);
  145. /* Then copy it */
  146. memcpy(reboot_code_buffer, relocate_kernel, relocate_kernel_len);
  147. return 0;
  148. }
  149. void machine_kexec_cleanup(struct kimage *image)
  150. {
  151. }
  152. void arch_crash_save_vmcoreinfo(void)
  153. {
  154. VMCOREINFO_SYMBOL(lowcore_ptr);
  155. VMCOREINFO_SYMBOL(high_memory);
  156. VMCOREINFO_LENGTH(lowcore_ptr, NR_CPUS);
  157. }
  158. void machine_shutdown(void)
  159. {
  160. }
  161. /*
  162. * Do normal kexec
  163. */
  164. static void __do_machine_kexec(void *data)
  165. {
  166. relocate_kernel_t data_mover;
  167. struct kimage *image = data;
  168. data_mover = (relocate_kernel_t) page_to_phys(image->control_code_page);
  169. /* Call the moving routine */
  170. (*data_mover)(&image->head, image->start);
  171. }
  172. /*
  173. * Reset system and call either kdump or normal kexec
  174. */
  175. static void __machine_kexec(void *data)
  176. {
  177. struct kimage *image = data;
  178. pfault_fini();
  179. tracing_off();
  180. debug_locks_off();
  181. if (image->type == KEXEC_TYPE_CRASH) {
  182. lgr_info_log();
  183. s390_reset_system(__do_machine_kdump, data);
  184. } else {
  185. s390_reset_system(__do_machine_kexec, data);
  186. }
  187. disabled_wait((unsigned long) __builtin_return_address(0));
  188. }
  189. /*
  190. * Do either kdump or normal kexec. In case of kdump we first ask
  191. * purgatory, if kdump checksums are valid.
  192. */
  193. void machine_kexec(struct kimage *image)
  194. {
  195. if (image->type == KEXEC_TYPE_CRASH && !kdump_csum_valid(image))
  196. return;
  197. tracer_disable();
  198. smp_send_stop();
  199. smp_call_ipl_cpu(__machine_kexec, image);
  200. }