1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Machine check handler 4 * 5 * Copyright IBM Corp. 2000, 2009 6 * Author(s): Ingo Adlung <adlung@de.ibm.com>, 7 * Martin Schwidefsky <schwidefsky@de.ibm.com>, 8 * Cornelia Huck <cornelia.huck@de.ibm.com>, 9 */ 10 11 #include <linux/kernel_stat.h> 12 #include <linux/utsname.h> 13 #include <linux/cpufeature.h> 14 #include <linux/init.h> 15 #include <linux/errno.h> 16 #include <linux/entry-common.h> 17 #include <linux/hardirq.h> 18 #include <linux/log2.h> 19 #include <linux/kprobes.h> 20 #include <linux/kmemleak.h> 21 #include <linux/time.h> 22 #include <linux/module.h> 23 #include <linux/sched/signal.h> 24 #include <linux/kvm_host.h> 25 #include <asm/entry-percpu.h> 26 #include <asm/lowcore.h> 27 #include <asm/ctlreg.h> 28 #include <asm/fpu.h> 29 #include <asm/smp.h> 30 #include <asm/stp.h> 31 #include <asm/cputime.h> 32 #include <asm/nmi.h> 33 #include <asm/crw.h> 34 #include <asm/asm-offsets.h> 35 #include <asm/pai.h> 36 #include <asm/vtime.h> 37 38 struct mcck_struct { 39 unsigned int kill_task : 1; 40 unsigned int channel_report : 1; 41 unsigned int warning : 1; 42 unsigned int stp_queue : 1; 43 unsigned long mcck_code; 44 }; 45 46 static DEFINE_PER_CPU(struct mcck_struct, cpu_mcck); 47 48 static inline int nmi_needs_mcesa(void) 49 { 50 return cpu_has_vx() || cpu_has_gs(); 51 } 52 53 /* 54 * The initial machine check extended save area for the boot CPU. 55 * It will be replaced on the boot CPU reinit with an allocated 56 * structure. The structure is required for machine check happening 57 * early in the boot process. 58 */ 59 static struct mcesa boot_mcesa __aligned(MCESA_MAX_SIZE); 60 61 void __init nmi_alloc_mcesa_early(u64 *mcesad) 62 { 63 if (!nmi_needs_mcesa()) 64 return; 65 *mcesad = __pa(&boot_mcesa); 66 if (cpu_has_gs()) 67 *mcesad |= ilog2(MCESA_MAX_SIZE); 68 } 69 70 int nmi_alloc_mcesa(u64 *mcesad) 71 { 72 unsigned long size; 73 void *origin; 74 75 *mcesad = 0; 76 if (!nmi_needs_mcesa()) 77 return 0; 78 size = cpu_has_gs() ? MCESA_MAX_SIZE : MCESA_MIN_SIZE; 79 origin = kmalloc(size, GFP_KERNEL); 80 if (!origin) 81 return -ENOMEM; 82 /* The pointer is stored with mcesa_bits ORed in */ 83 kmemleak_not_leak(origin); 84 *mcesad = __pa(origin); 85 if (cpu_has_gs()) 86 *mcesad |= ilog2(MCESA_MAX_SIZE); 87 return 0; 88 } 89 90 void nmi_free_mcesa(u64 *mcesad) 91 { 92 if (!nmi_needs_mcesa()) 93 return; 94 kfree(__va(*mcesad & MCESA_ORIGIN_MASK)); 95 } 96 97 static __always_inline char *nmi_puts(char *dest, const char *src) 98 { 99 while (*src) 100 *dest++ = *src++; 101 *dest = 0; 102 return dest; 103 } 104 105 static __always_inline char *u64_to_hex(char *dest, u64 val) 106 { 107 int i, num; 108 109 for (i = 1; i <= 16; i++) { 110 num = (val >> (64 - 4 * i)) & 0xf; 111 if (num >= 10) 112 *dest++ = 'A' + num - 10; 113 else 114 *dest++ = '0' + num; 115 } 116 *dest = 0; 117 return dest; 118 } 119 120 static notrace void nmi_print_info(void) 121 { 122 struct lowcore *lc = get_lowcore(); 123 char message[100]; 124 char *ptr; 125 int i; 126 127 ptr = nmi_puts(message, "Unrecoverable machine check, code: "); 128 ptr = u64_to_hex(ptr, lc->mcck_interruption_code); 129 ptr = nmi_puts(ptr, "\n"); 130 sclp_emergency_printk(message); 131 132 ptr = nmi_puts(message, init_utsname()->release); 133 ptr = nmi_puts(ptr, "\n"); 134 sclp_emergency_printk(message); 135 136 ptr = nmi_puts(message, arch_hw_string); 137 ptr = nmi_puts(ptr, "\n"); 138 sclp_emergency_printk(message); 139 140 ptr = nmi_puts(message, "PSW: "); 141 ptr = u64_to_hex(ptr, lc->mcck_old_psw.mask); 142 ptr = nmi_puts(ptr, " "); 143 ptr = u64_to_hex(ptr, lc->mcck_old_psw.addr); 144 ptr = nmi_puts(ptr, " PFX: "); 145 ptr = u64_to_hex(ptr, (u64)get_lowcore()); 146 ptr = nmi_puts(ptr, "\n"); 147 sclp_emergency_printk(message); 148 149 ptr = nmi_puts(message, "LBA: "); 150 ptr = u64_to_hex(ptr, lc->last_break_save_area); 151 ptr = nmi_puts(ptr, " EDC: "); 152 ptr = u64_to_hex(ptr, lc->external_damage_code); 153 ptr = nmi_puts(ptr, " FSA: "); 154 ptr = u64_to_hex(ptr, lc->failing_storage_address); 155 ptr = nmi_puts(ptr, "\n"); 156 sclp_emergency_printk(message); 157 158 ptr = nmi_puts(message, "CRS:\n"); 159 sclp_emergency_printk(message); 160 ptr = message; 161 for (i = 0; i < 16; i++) { 162 ptr = u64_to_hex(ptr, lc->cregs_save_area[i].val); 163 ptr = nmi_puts(ptr, " "); 164 if ((i + 1) % 4 == 0) { 165 ptr = nmi_puts(ptr, "\n"); 166 sclp_emergency_printk(message); 167 ptr = message; 168 } 169 } 170 171 ptr = nmi_puts(message, "GPRS:\n"); 172 sclp_emergency_printk(message); 173 ptr = message; 174 for (i = 0; i < 16; i++) { 175 ptr = u64_to_hex(ptr, lc->gpregs_save_area[i]); 176 ptr = nmi_puts(ptr, " "); 177 if ((i + 1) % 4 == 0) { 178 ptr = nmi_puts(ptr, "\n"); 179 sclp_emergency_printk(message); 180 ptr = message; 181 } 182 } 183 184 ptr = nmi_puts(message, "System stopped\n"); 185 sclp_emergency_printk(message); 186 } 187 188 static notrace void __noreturn s390_handle_damage(void) 189 { 190 struct lowcore *lc = get_lowcore(); 191 union ctlreg0 cr0, cr0_new; 192 psw_t psw_save; 193 194 smp_emergency_stop(); 195 diag_amode31_ops.diag308_reset(); 196 197 /* 198 * Disable low address protection and make machine check new PSW a 199 * disabled wait PSW. Any additional machine check cannot be handled. 200 */ 201 local_ctl_store(0, &cr0.reg); 202 cr0_new = cr0; 203 cr0_new.lap = 0; 204 local_ctl_load(0, &cr0_new.reg); 205 psw_save = lc->mcck_new_psw; 206 psw_bits(lc->mcck_new_psw).io = 0; 207 psw_bits(lc->mcck_new_psw).ext = 0; 208 psw_bits(lc->mcck_new_psw).wait = 1; 209 nmi_print_info(); 210 211 /* 212 * Restore machine check new PSW and control register 0 to original 213 * values. This makes possible system dump analysis easier. 214 */ 215 lc->mcck_new_psw = psw_save; 216 local_ctl_load(0, &cr0.reg); 217 disabled_wait(); 218 } 219 NOKPROBE_SYMBOL(s390_handle_damage); 220 221 /* 222 * Main machine check handler function. Will be called with interrupts disabled 223 * and machine checks enabled. 224 */ 225 void s390_handle_mcck(void) 226 { 227 struct mcck_struct mcck; 228 unsigned long mflags; 229 230 /* 231 * Disable machine checks and get the current state of accumulated 232 * machine checks. Afterwards delete the old state and enable machine 233 * checks again. 234 */ 235 local_mcck_save(mflags); 236 mcck = *this_cpu_ptr(&cpu_mcck); 237 memset(this_cpu_ptr(&cpu_mcck), 0, sizeof(mcck)); 238 local_mcck_restore(mflags); 239 240 if (mcck.channel_report) 241 crw_handle_channel_report(); 242 /* 243 * A warning may remain for a prolonged period on the bare iron. 244 * (actually until the machine is powered off, or the problem is gone) 245 * So we just stop listening for the WARNING MCH and avoid continuously 246 * being interrupted. One caveat is however, that we must do this per 247 * processor and cannot use the smp version of ctl_clear_bit(). 248 * On VM we only get one interrupt per virtally presented machinecheck. 249 * Though one suffices, we may get one interrupt per (virtual) cpu. 250 */ 251 if (mcck.warning) { /* WARNING pending ? */ 252 static int mchchk_wng_posted = 0; 253 254 /* Use single cpu clear, as we cannot handle smp here. */ 255 local_ctl_clear_bit(14, CR14_WARNING_SUBMASK_BIT); 256 if (xchg(&mchchk_wng_posted, 1) == 0) 257 kill_cad_pid(SIGPWR, 1); 258 } 259 if (mcck.stp_queue) 260 stp_queue_work(); 261 if (mcck.kill_task) { 262 printk(KERN_EMERG "mcck: Terminating task because of machine " 263 "malfunction (code 0x%016lx).\n", mcck.mcck_code); 264 printk(KERN_EMERG "mcck: task: %s, pid: %d.\n", 265 current->comm, current->pid); 266 if (is_global_init(current)) 267 panic("mcck: Attempting to kill init!\n"); 268 do_send_sig_info(SIGKILL, SEND_SIG_PRIV, current, PIDTYPE_PID); 269 } 270 } 271 272 /** 273 * nmi_registers_valid - verify if registers are valid 274 * @mci: machine check interruption code 275 * 276 * Inspect a machine check interruption code and verify if all required 277 * registers are valid. For some registers the corresponding validity bit is 278 * ignored and the registers are set to the expected value. 279 * Returns true if all registers are valid, otherwise false. 280 */ 281 static bool notrace nmi_registers_valid(union mci mci) 282 { 283 union ctlreg2 cr2; 284 285 /* 286 * The getcpu vdso syscall reads the CPU number from the programmable 287 * field of the TOD clock. Disregard the TOD programmable register 288 * validity bit and load the CPU number into the TOD programmable field 289 * unconditionally. 290 */ 291 set_tod_programmable_field(raw_smp_processor_id()); 292 /* 293 * Set the clock comparator register to the next expected value. 294 */ 295 set_clock_comparator(get_lowcore()->clock_comparator); 296 if (!mci.gr || !mci.fp || !mci.fc) 297 return false; 298 /* 299 * The vector validity must only be checked if not running a 300 * KVM guest. For KVM guests the machine check is forwarded by 301 * KVM and it is the responsibility of the guest to take 302 * appropriate actions. The host vector or FPU values have been 303 * saved by KVM and will be restored by KVM. 304 */ 305 if (!mci.vr && !test_cpu_flag(CIF_MCCK_GUEST)) 306 return false; 307 if (!mci.ar) 308 return false; 309 /* 310 * Two cases for guarded storage registers: 311 * - machine check in kernel or userspace 312 * - machine check while running SIE (KVM guest) 313 * For kernel or userspace the userspace values of guarded storage 314 * control can not be recreated, the process must be terminated. 315 * For SIE the guest values of guarded storage can not be recreated. 316 * This is either due to a bug or due to GS being disabled in the 317 * guest. The guest will be notified by KVM code and the guests machine 318 * check handling must take care of this. The host values are saved by 319 * KVM and are not affected. 320 */ 321 cr2.reg = get_lowcore()->cregs_save_area[2]; 322 if (cr2.gse && !mci.gs && !test_cpu_flag(CIF_MCCK_GUEST)) 323 return false; 324 if (!mci.ms || !mci.pm || !mci.ia) 325 return false; 326 return true; 327 } 328 NOKPROBE_SYMBOL(nmi_registers_valid); 329 330 /* 331 * Backup the guest's machine check info to its description block 332 */ 333 static void notrace s390_backup_mcck_info(struct pt_regs *regs) 334 { 335 struct mcck_volatile_info *mcck_backup; 336 struct sie_page *sie_page; 337 338 /* r14 contains the sie block, which was set in sie64a */ 339 struct kvm_s390_sie_block *sie_block = phys_to_virt(regs->gprs[14]); 340 341 if (sie_block == NULL) 342 /* Something's seriously wrong, stop system. */ 343 s390_handle_damage(); 344 345 sie_page = container_of(sie_block, struct sie_page, sie_block); 346 mcck_backup = &sie_page->mcck_info; 347 mcck_backup->mcic = get_lowcore()->mcck_interruption_code & 348 ~(MCCK_CODE_CP | MCCK_CODE_EXT_DAMAGE); 349 mcck_backup->ext_damage_code = get_lowcore()->external_damage_code; 350 mcck_backup->failing_storage_address = get_lowcore()->failing_storage_address; 351 } 352 NOKPROBE_SYMBOL(s390_backup_mcck_info); 353 354 #define MAX_IPD_COUNT 29 355 #define MAX_IPD_TIME (5 * 60 * USEC_PER_SEC) /* 5 minutes */ 356 357 #define ED_STP_ISLAND 6 /* External damage STP island check */ 358 #define ED_STP_SYNC 7 /* External damage STP sync check */ 359 360 #define MCCK_CODE_NO_GUEST (MCCK_CODE_CP | MCCK_CODE_EXT_DAMAGE) 361 362 /* 363 * machine check handler. 364 */ 365 void notrace s390_do_machine_check(struct pt_regs *regs) 366 { 367 bool percpu_needs_fixup; 368 static int ipd_count; 369 static DEFINE_SPINLOCK(ipd_lock); 370 static unsigned long long last_ipd; 371 struct lowcore *lc = get_lowcore(); 372 struct mcck_struct *mcck; 373 unsigned long long tmp; 374 irqentry_state_t irq_state; 375 union mci mci; 376 unsigned long mcck_dam_code; 377 int mcck_pending = 0; 378 379 percpu_entry(regs); 380 irq_state = irqentry_nmi_enter(regs); 381 382 if (user_mode(regs)) 383 update_timer_mcck(); 384 inc_irq_stat(NMI_NMI); 385 mci.val = lc->mcck_interruption_code; 386 mcck = this_cpu_ptr(&cpu_mcck); 387 388 /* 389 * Reinject the instruction processing damages' machine checks 390 * including Delayed Access Exception into the guest 391 * instead of damaging the host if they happen in the guest. 392 */ 393 if (mci.pd && !test_cpu_flag(CIF_MCCK_GUEST)) { 394 if (mci.b) { 395 /* Processing backup -> verify if we can survive this */ 396 u64 z_mcic, o_mcic, t_mcic; 397 z_mcic = (1ULL<<63 | 1ULL<<59 | 1ULL<<29); 398 o_mcic = (1ULL<<43 | 1ULL<<42 | 1ULL<<41 | 1ULL<<40 | 399 1ULL<<36 | 1ULL<<35 | 1ULL<<34 | 1ULL<<32 | 400 1ULL<<30 | 1ULL<<21 | 1ULL<<20 | 1ULL<<17 | 401 1ULL<<16); 402 t_mcic = mci.val; 403 404 if (((t_mcic & z_mcic) != 0) || 405 ((t_mcic & o_mcic) != o_mcic)) { 406 s390_handle_damage(); 407 } 408 409 /* 410 * Nullifying exigent condition, therefore we might 411 * retry this instruction. 412 */ 413 spin_lock(&ipd_lock); 414 tmp = get_tod_clock(); 415 if (((tmp - last_ipd) >> 12) < MAX_IPD_TIME) 416 ipd_count++; 417 else 418 ipd_count = 1; 419 last_ipd = tmp; 420 if (ipd_count == MAX_IPD_COUNT) 421 s390_handle_damage(); 422 spin_unlock(&ipd_lock); 423 } else { 424 /* Processing damage -> stopping machine */ 425 s390_handle_damage(); 426 } 427 } 428 if (!nmi_registers_valid(mci)) { 429 if (!user_mode(regs)) 430 s390_handle_damage(); 431 /* 432 * Couldn't restore all register contents for the 433 * user space process -> mark task for termination. 434 */ 435 mcck->kill_task = 1; 436 mcck->mcck_code = mci.val; 437 mcck_pending = 1; 438 } 439 440 /* 441 * Backup the machine check's info if it happens when the guest 442 * is running. 443 */ 444 if (test_cpu_flag(CIF_MCCK_GUEST)) 445 s390_backup_mcck_info(regs); 446 447 if (mci.cd) { 448 /* Timing facility damage */ 449 s390_handle_damage(); 450 } 451 if (mci.ed && mci.ec) { 452 /* External damage */ 453 if (lc->external_damage_code & (1U << ED_STP_SYNC)) 454 mcck->stp_queue |= stp_sync_check(); 455 if (lc->external_damage_code & (1U << ED_STP_ISLAND)) 456 mcck->stp_queue |= stp_island_check(); 457 mcck_pending = 1; 458 } 459 /* 460 * Reinject storage related machine checks into the guest if they 461 * happen when the guest is running. 462 */ 463 if (!test_cpu_flag(CIF_MCCK_GUEST)) { 464 /* Storage error uncorrected */ 465 if (mci.se) 466 s390_handle_damage(); 467 /* Storage key-error uncorrected */ 468 if (mci.ke) 469 s390_handle_damage(); 470 /* Storage degradation */ 471 if (mci.ds && mci.fa) 472 s390_handle_damage(); 473 } 474 if (mci.cp) { 475 /* Channel report word pending */ 476 mcck->channel_report = 1; 477 mcck_pending = 1; 478 } 479 if (mci.w) { 480 /* Warning pending */ 481 mcck->warning = 1; 482 mcck_pending = 1; 483 } 484 485 /* 486 * If there are only Channel Report Pending and External Damage 487 * machine checks, they will not be reinjected into the guest 488 * because they refer to host conditions only. 489 */ 490 mcck_dam_code = (mci.val & MCIC_SUBCLASS_MASK); 491 if (test_cpu_flag(CIF_MCCK_GUEST) && 492 (mcck_dam_code & MCCK_CODE_NO_GUEST) != mcck_dam_code) { 493 /* Set sie return code for host's later handling */ 494 ((struct stack_frame *)regs->gprs[15])->sie_return = SIE64_RETURN_MCCK; 495 } 496 clear_cpu_flag(CIF_MCCK_GUEST); 497 498 if (mcck_pending) 499 schedule_mcck_handler(); 500 501 percpu_needs_fixup = percpu_code_check(regs); 502 irqentry_nmi_exit(regs, irq_state); 503 percpu_exit(regs, percpu_needs_fixup); 504 } 505 NOKPROBE_SYMBOL(s390_do_machine_check); 506 507 static int __init machine_check_init(void) 508 { 509 system_ctl_set_bit(14, CR14_EXTERNAL_DAMAGE_SUBMASK_BIT); 510 system_ctl_set_bit(14, CR14_RECOVERY_SUBMASK_BIT); 511 system_ctl_set_bit(14, CR14_WARNING_SUBMASK_BIT); 512 return 0; 513 } 514 early_initcall(machine_check_init); 515