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