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
nmi_needs_mcesa(void)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
nmi_alloc_mcesa_early(u64 * mcesad)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
nmi_alloc_mcesa(u64 * mcesad)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
nmi_free_mcesa(u64 * mcesad)90 void nmi_free_mcesa(u64 *mcesad)
91 {
92 if (!nmi_needs_mcesa())
93 return;
94 kfree(__va(*mcesad & MCESA_ORIGIN_MASK));
95 }
96
nmi_puts(char * dest,const char * src)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
u64_to_hex(char * dest,u64 val)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
nmi_print_info(void)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
s390_handle_damage(void)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 */
s390_handle_mcck(void)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 */
nmi_registers_valid(union mci mci)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 */
s390_backup_mcck_info(struct pt_regs * regs)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 & ~MCCK_CODE_NO_GUEST;
348 mcck_backup->ext_damage_code = get_lowcore()->external_damage_code;
349 mcck_backup->failing_storage_address = get_lowcore()->failing_storage_address;
350 }
351 NOKPROBE_SYMBOL(s390_backup_mcck_info);
352
353 #define MAX_IPD_COUNT 29
354 #define MAX_IPD_TIME (5 * 60 * USEC_PER_SEC) /* 5 minutes */
355
356 #define ED_STP_ISLAND 6 /* External damage STP island check */
357 #define ED_STP_SYNC 7 /* External damage STP sync check */
358
359 /*
360 * machine check handler.
361 */
s390_do_machine_check(struct pt_regs * regs)362 void notrace s390_do_machine_check(struct pt_regs *regs)
363 {
364 static int ipd_count;
365 static DEFINE_SPINLOCK(ipd_lock);
366 static unsigned long long last_ipd;
367 struct lowcore *lc = get_lowcore();
368 struct mcck_struct *mcck;
369 unsigned long long tmp;
370 irqentry_state_t irq_state;
371 union mci mci;
372 unsigned long mcck_dam_code;
373 int mcck_pending = 0;
374
375 percpu_entry(regs);
376 irq_state = irqentry_nmi_enter(regs);
377
378 if (user_mode(regs))
379 update_timer_mcck();
380 inc_irq_stat(NMI_NMI);
381 mci.val = lc->mcck_interruption_code;
382 mcck = this_cpu_ptr(&cpu_mcck);
383
384 /*
385 * Reinject the instruction processing damages' machine checks
386 * including Delayed Access Exception into the guest
387 * instead of damaging the host if they happen in the guest.
388 */
389 if (mci.pd && !test_cpu_flag(CIF_MCCK_GUEST)) {
390 if (mci.b) {
391 /* Processing backup -> verify if we can survive this */
392 u64 z_mcic, o_mcic, t_mcic;
393 z_mcic = (1ULL<<63 | 1ULL<<59 | 1ULL<<29);
394 o_mcic = (1ULL<<43 | 1ULL<<42 | 1ULL<<41 | 1ULL<<40 |
395 1ULL<<36 | 1ULL<<35 | 1ULL<<34 | 1ULL<<32 |
396 1ULL<<30 | 1ULL<<21 | 1ULL<<20 | 1ULL<<17 |
397 1ULL<<16);
398 t_mcic = mci.val;
399
400 if (((t_mcic & z_mcic) != 0) ||
401 ((t_mcic & o_mcic) != o_mcic)) {
402 s390_handle_damage();
403 }
404
405 /*
406 * Nullifying exigent condition, therefore we might
407 * retry this instruction.
408 */
409 spin_lock(&ipd_lock);
410 tmp = get_tod_clock();
411 if (((tmp - last_ipd) >> 12) < MAX_IPD_TIME)
412 ipd_count++;
413 else
414 ipd_count = 1;
415 last_ipd = tmp;
416 if (ipd_count == MAX_IPD_COUNT)
417 s390_handle_damage();
418 spin_unlock(&ipd_lock);
419 } else {
420 /* Processing damage -> stopping machine */
421 s390_handle_damage();
422 }
423 }
424 if (!nmi_registers_valid(mci)) {
425 if (!user_mode(regs))
426 s390_handle_damage();
427 /*
428 * Couldn't restore all register contents for the
429 * user space process -> mark task for termination.
430 */
431 mcck->kill_task = 1;
432 mcck->mcck_code = mci.val;
433 mcck_pending = 1;
434 }
435
436 /*
437 * Backup the machine check's info if it happens when the guest
438 * is running.
439 */
440 if (test_cpu_flag(CIF_MCCK_GUEST))
441 s390_backup_mcck_info(regs);
442
443 if (mci.cd) {
444 /* Timing facility damage */
445 s390_handle_damage();
446 }
447 if (mci.ed && mci.ec) {
448 /* External damage */
449 if (lc->external_damage_code & (1U << ED_STP_SYNC))
450 mcck->stp_queue |= stp_sync_check();
451 if (lc->external_damage_code & (1U << ED_STP_ISLAND))
452 mcck->stp_queue |= stp_island_check();
453 mcck_pending = 1;
454 }
455 /*
456 * Reinject storage related machine checks into the guest if they
457 * happen when the guest is running.
458 */
459 if (!test_cpu_flag(CIF_MCCK_GUEST)) {
460 /* Storage error uncorrected */
461 if (mci.se)
462 s390_handle_damage();
463 /* Storage key-error uncorrected */
464 if (mci.ke)
465 s390_handle_damage();
466 /* Storage degradation */
467 if (mci.ds && mci.fa)
468 s390_handle_damage();
469 }
470 if (mci.cp) {
471 /* Channel report word pending */
472 mcck->channel_report = 1;
473 mcck_pending = 1;
474 }
475 if (mci.w) {
476 /* Warning pending */
477 mcck->warning = 1;
478 mcck_pending = 1;
479 }
480
481 /*
482 * If there are only Channel Report Pending and External Damage
483 * machine checks, they will not be reinjected into the guest
484 * because they refer to host conditions only.
485 */
486 mcck_dam_code = (mci.val & MCIC_SUBCLASS_MASK);
487 if (test_cpu_flag(CIF_MCCK_GUEST) &&
488 (mcck_dam_code & MCCK_CODE_NO_GUEST) != mcck_dam_code) {
489 /* Set sie return code for host's later handling */
490 ((struct stack_frame *)regs->gprs[15])->sie_return = SIE64_RETURN_MCCK;
491 }
492 clear_cpu_flag(CIF_MCCK_GUEST);
493
494 if (mcck_pending)
495 schedule_mcck_handler();
496
497 irqentry_nmi_exit(regs, irq_state);
498 percpu_exit(regs);
499 }
500 NOKPROBE_SYMBOL(s390_do_machine_check);
501
machine_check_init(void)502 static int __init machine_check_init(void)
503 {
504 system_ctl_set_bit(14, CR14_EXTERNAL_DAMAGE_SUBMASK_BIT);
505 system_ctl_set_bit(14, CR14_RECOVERY_SUBMASK_BIT);
506 system_ctl_set_bit(14, CR14_WARNING_SUBMASK_BIT);
507 return 0;
508 }
509 early_initcall(machine_check_init);
510