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/init.h>
13 #include <linux/errno.h>
14 #include <linux/entry-common.h>
15 #include <linux/hardirq.h>
16 #include <linux/log2.h>
17 #include <linux/kprobes.h>
18 #include <linux/kmemleak.h>
19 #include <linux/time.h>
20 #include <linux/module.h>
21 #include <linux/sched/signal.h>
22
23 #include <linux/export.h>
24 #include <asm/lowcore.h>
25 #include <asm/smp.h>
26 #include <asm/stp.h>
27 #include <asm/cputime.h>
28 #include <asm/nmi.h>
29 #include <asm/crw.h>
30 #include <asm/switch_to.h>
31 #include <asm/ctl_reg.h>
32 #include <asm/asm-offsets.h>
33 #include <asm/pai.h>
34
35 #include <linux/kvm_host.h>
36
37 struct mcck_struct {
38 unsigned int kill_task : 1;
39 unsigned int channel_report : 1;
40 unsigned int warning : 1;
41 unsigned int stp_queue : 1;
42 unsigned long mcck_code;
43 };
44
45 static DEFINE_PER_CPU(struct mcck_struct, cpu_mcck);
46 static struct kmem_cache *mcesa_cache;
47 static unsigned long mcesa_origin_lc;
48
nmi_needs_mcesa(void)49 static inline int nmi_needs_mcesa(void)
50 {
51 return MACHINE_HAS_VX || MACHINE_HAS_GS;
52 }
53
nmi_get_mcesa_size(void)54 static inline unsigned long nmi_get_mcesa_size(void)
55 {
56 if (MACHINE_HAS_GS)
57 return MCESA_MAX_SIZE;
58 return MCESA_MIN_SIZE;
59 }
60
61 /*
62 * The initial machine check extended save area for the boot CPU.
63 * It will be replaced on the boot CPU reinit with an allocated
64 * structure. The structure is required for machine check happening
65 * early in the boot process.
66 */
67 static struct mcesa boot_mcesa __aligned(MCESA_MAX_SIZE);
68
nmi_alloc_mcesa_early(u64 * mcesad)69 void __init nmi_alloc_mcesa_early(u64 *mcesad)
70 {
71 if (!nmi_needs_mcesa())
72 return;
73 *mcesad = __pa(&boot_mcesa);
74 if (MACHINE_HAS_GS)
75 *mcesad |= ilog2(MCESA_MAX_SIZE);
76 }
77
nmi_alloc_cache(void)78 static void __init nmi_alloc_cache(void)
79 {
80 unsigned long size;
81
82 if (!nmi_needs_mcesa())
83 return;
84 size = nmi_get_mcesa_size();
85 if (size > MCESA_MIN_SIZE)
86 mcesa_origin_lc = ilog2(size);
87 /* create slab cache for the machine-check-extended-save-areas */
88 mcesa_cache = kmem_cache_create("nmi_save_areas", size, size, 0, NULL);
89 if (!mcesa_cache)
90 panic("Couldn't create nmi save area cache");
91 }
92
nmi_alloc_mcesa(u64 * mcesad)93 int __ref nmi_alloc_mcesa(u64 *mcesad)
94 {
95 unsigned long origin;
96
97 *mcesad = 0;
98 if (!nmi_needs_mcesa())
99 return 0;
100 if (!mcesa_cache)
101 nmi_alloc_cache();
102 origin = (unsigned long) kmem_cache_alloc(mcesa_cache, GFP_KERNEL);
103 if (!origin)
104 return -ENOMEM;
105 /* The pointer is stored with mcesa_bits ORed in */
106 kmemleak_not_leak((void *) origin);
107 *mcesad = __pa(origin) | mcesa_origin_lc;
108 return 0;
109 }
110
nmi_free_mcesa(u64 * mcesad)111 void nmi_free_mcesa(u64 *mcesad)
112 {
113 if (!nmi_needs_mcesa())
114 return;
115 kmem_cache_free(mcesa_cache, __va(*mcesad & MCESA_ORIGIN_MASK));
116 }
117
s390_handle_damage(void)118 static notrace void s390_handle_damage(void)
119 {
120 smp_emergency_stop();
121 disabled_wait();
122 while (1);
123 }
124 NOKPROBE_SYMBOL(s390_handle_damage);
125
126 /*
127 * Main machine check handler function. Will be called with interrupts disabled
128 * and machine checks enabled.
129 */
__s390_handle_mcck(void)130 void __s390_handle_mcck(void)
131 {
132 struct mcck_struct mcck;
133
134 /*
135 * Disable machine checks and get the current state of accumulated
136 * machine checks. Afterwards delete the old state and enable machine
137 * checks again.
138 */
139 local_mcck_disable();
140 mcck = *this_cpu_ptr(&cpu_mcck);
141 memset(this_cpu_ptr(&cpu_mcck), 0, sizeof(mcck));
142 local_mcck_enable();
143
144 if (mcck.channel_report)
145 crw_handle_channel_report();
146 /*
147 * A warning may remain for a prolonged period on the bare iron.
148 * (actually until the machine is powered off, or the problem is gone)
149 * So we just stop listening for the WARNING MCH and avoid continuously
150 * being interrupted. One caveat is however, that we must do this per
151 * processor and cannot use the smp version of ctl_clear_bit().
152 * On VM we only get one interrupt per virtally presented machinecheck.
153 * Though one suffices, we may get one interrupt per (virtual) cpu.
154 */
155 if (mcck.warning) { /* WARNING pending ? */
156 static int mchchk_wng_posted = 0;
157
158 /* Use single cpu clear, as we cannot handle smp here. */
159 __ctl_clear_bit(14, 24); /* Disable WARNING MCH */
160 if (xchg(&mchchk_wng_posted, 1) == 0)
161 kill_cad_pid(SIGPWR, 1);
162 }
163 if (mcck.stp_queue)
164 stp_queue_work();
165 if (mcck.kill_task) {
166 local_irq_enable();
167 printk(KERN_EMERG "mcck: Terminating task because of machine "
168 "malfunction (code 0x%016lx).\n", mcck.mcck_code);
169 printk(KERN_EMERG "mcck: task: %s, pid: %d.\n",
170 current->comm, current->pid);
171 make_task_dead(SIGSEGV);
172 }
173 }
174
s390_handle_mcck(struct pt_regs * regs)175 void noinstr s390_handle_mcck(struct pt_regs *regs)
176 {
177 trace_hardirqs_off();
178 pai_kernel_enter(regs);
179 __s390_handle_mcck();
180 pai_kernel_exit(regs);
181 trace_hardirqs_on();
182 }
183 /*
184 * returns 0 if all required registers are available
185 * returns 1 otherwise
186 */
s390_validate_registers(union mci mci,int umode)187 static int notrace s390_validate_registers(union mci mci, int umode)
188 {
189 struct mcesa *mcesa;
190 void *fpt_save_area;
191 union ctlreg2 cr2;
192 int kill_task;
193 u64 zero;
194
195 kill_task = 0;
196 zero = 0;
197
198 if (!mci.gr) {
199 /*
200 * General purpose registers couldn't be restored and have
201 * unknown contents. Stop system or terminate process.
202 */
203 if (!umode)
204 s390_handle_damage();
205 kill_task = 1;
206 }
207 if (!mci.fp) {
208 /*
209 * Floating point registers can't be restored. If the
210 * kernel currently uses floating point registers the
211 * system is stopped. If the process has its floating
212 * pointer registers loaded it is terminated.
213 */
214 if (S390_lowcore.fpu_flags & KERNEL_VXR_V0V7)
215 s390_handle_damage();
216 if (!test_cpu_flag(CIF_FPU))
217 kill_task = 1;
218 }
219 fpt_save_area = &S390_lowcore.floating_pt_save_area;
220 if (!mci.fc) {
221 /*
222 * Floating point control register can't be restored.
223 * If the kernel currently uses the floating pointer
224 * registers and needs the FPC register the system is
225 * stopped. If the process has its floating pointer
226 * registers loaded it is terminated. Otherwise the
227 * FPC is just validated.
228 */
229 if (S390_lowcore.fpu_flags & KERNEL_FPC)
230 s390_handle_damage();
231 asm volatile(
232 " lfpc %0\n"
233 :
234 : "Q" (zero));
235 if (!test_cpu_flag(CIF_FPU))
236 kill_task = 1;
237 } else {
238 asm volatile(
239 " lfpc %0\n"
240 :
241 : "Q" (S390_lowcore.fpt_creg_save_area));
242 }
243
244 mcesa = __va(S390_lowcore.mcesad & MCESA_ORIGIN_MASK);
245 if (!MACHINE_HAS_VX) {
246 /* Validate floating point registers */
247 asm volatile(
248 " ld 0,0(%0)\n"
249 " ld 1,8(%0)\n"
250 " ld 2,16(%0)\n"
251 " ld 3,24(%0)\n"
252 " ld 4,32(%0)\n"
253 " ld 5,40(%0)\n"
254 " ld 6,48(%0)\n"
255 " ld 7,56(%0)\n"
256 " ld 8,64(%0)\n"
257 " ld 9,72(%0)\n"
258 " ld 10,80(%0)\n"
259 " ld 11,88(%0)\n"
260 " ld 12,96(%0)\n"
261 " ld 13,104(%0)\n"
262 " ld 14,112(%0)\n"
263 " ld 15,120(%0)\n"
264 :
265 : "a" (fpt_save_area)
266 : "memory");
267 } else {
268 /* Validate vector registers */
269 union ctlreg0 cr0;
270
271 /*
272 * The vector validity must only be checked if not running a
273 * KVM guest. For KVM guests the machine check is forwarded by
274 * KVM and it is the responsibility of the guest to take
275 * appropriate actions. The host vector or FPU values have been
276 * saved by KVM and will be restored by KVM.
277 */
278 if (!mci.vr && !test_cpu_flag(CIF_MCCK_GUEST)) {
279 /*
280 * Vector registers can't be restored. If the kernel
281 * currently uses vector registers the system is
282 * stopped. If the process has its vector registers
283 * loaded it is terminated. Otherwise just validate
284 * the registers.
285 */
286 if (S390_lowcore.fpu_flags & KERNEL_VXR)
287 s390_handle_damage();
288 if (!test_cpu_flag(CIF_FPU))
289 kill_task = 1;
290 }
291 cr0.val = S390_lowcore.cregs_save_area[0];
292 cr0.afp = cr0.vx = 1;
293 __ctl_load(cr0.val, 0, 0);
294 asm volatile(
295 " la 1,%0\n"
296 " .word 0xe70f,0x1000,0x0036\n" /* vlm 0,15,0(1) */
297 " .word 0xe70f,0x1100,0x0c36\n" /* vlm 16,31,256(1) */
298 :
299 : "Q" (*(struct vx_array *)mcesa->vector_save_area)
300 : "1");
301 __ctl_load(S390_lowcore.cregs_save_area[0], 0, 0);
302 }
303 /* Validate access registers */
304 asm volatile(
305 " lam 0,15,0(%0)\n"
306 :
307 : "a" (&S390_lowcore.access_regs_save_area)
308 : "memory");
309 if (!mci.ar) {
310 /*
311 * Access registers have unknown contents.
312 * Terminating task.
313 */
314 kill_task = 1;
315 }
316 /* Validate guarded storage registers */
317 cr2.val = S390_lowcore.cregs_save_area[2];
318 if (cr2.gse) {
319 if (!mci.gs) {
320 /*
321 * 2 cases:
322 * - machine check in kernel or userspace
323 * - machine check while running SIE (KVM guest)
324 * For kernel or userspace the userspace values of
325 * guarded storage control can not be recreated, the
326 * process must be terminated.
327 * For SIE the guest values of guarded storage can not
328 * be recreated. This is either due to a bug or due to
329 * GS being disabled in the guest. The guest will be
330 * notified by KVM code and the guests machine check
331 * handling must take care of this. The host values
332 * are saved by KVM and are not affected.
333 */
334 if (!test_cpu_flag(CIF_MCCK_GUEST))
335 kill_task = 1;
336 } else {
337 load_gs_cb((struct gs_cb *)mcesa->guarded_storage_save_area);
338 }
339 }
340 /*
341 * The getcpu vdso syscall reads CPU number from the programmable
342 * field of the TOD clock. Disregard the TOD programmable register
343 * validity bit and load the CPU number into the TOD programmable
344 * field unconditionally.
345 */
346 set_tod_programmable_field(raw_smp_processor_id());
347 /* Validate clock comparator register */
348 set_clock_comparator(S390_lowcore.clock_comparator);
349
350 if (!mci.ms || !mci.pm || !mci.ia)
351 kill_task = 1;
352
353 return kill_task;
354 }
355 NOKPROBE_SYMBOL(s390_validate_registers);
356
357 /*
358 * Backup the guest's machine check info to its description block
359 */
s390_backup_mcck_info(struct pt_regs * regs)360 static void notrace s390_backup_mcck_info(struct pt_regs *regs)
361 {
362 struct mcck_volatile_info *mcck_backup;
363 struct sie_page *sie_page;
364
365 /* r14 contains the sie block, which was set in sie64a */
366 struct kvm_s390_sie_block *sie_block =
367 (struct kvm_s390_sie_block *) regs->gprs[14];
368
369 if (sie_block == NULL)
370 /* Something's seriously wrong, stop system. */
371 s390_handle_damage();
372
373 sie_page = container_of(sie_block, struct sie_page, sie_block);
374 mcck_backup = &sie_page->mcck_info;
375 mcck_backup->mcic = S390_lowcore.mcck_interruption_code &
376 ~(MCCK_CODE_CP | MCCK_CODE_EXT_DAMAGE);
377 mcck_backup->ext_damage_code = S390_lowcore.external_damage_code;
378 mcck_backup->failing_storage_address
379 = S390_lowcore.failing_storage_address;
380 }
381 NOKPROBE_SYMBOL(s390_backup_mcck_info);
382
383 #define MAX_IPD_COUNT 29
384 #define MAX_IPD_TIME (5 * 60 * USEC_PER_SEC) /* 5 minutes */
385
386 #define ED_STP_ISLAND 6 /* External damage STP island check */
387 #define ED_STP_SYNC 7 /* External damage STP sync check */
388
389 #define MCCK_CODE_NO_GUEST (MCCK_CODE_CP | MCCK_CODE_EXT_DAMAGE)
390
391 /*
392 * machine check handler.
393 */
s390_do_machine_check(struct pt_regs * regs)394 int notrace s390_do_machine_check(struct pt_regs *regs)
395 {
396 static int ipd_count;
397 static DEFINE_SPINLOCK(ipd_lock);
398 static unsigned long long last_ipd;
399 struct mcck_struct *mcck;
400 unsigned long long tmp;
401 irqentry_state_t irq_state;
402 union mci mci;
403 unsigned long mcck_dam_code;
404 int mcck_pending = 0;
405
406 irq_state = irqentry_nmi_enter(regs);
407
408 if (user_mode(regs))
409 update_timer_mcck();
410 inc_irq_stat(NMI_NMI);
411 mci.val = S390_lowcore.mcck_interruption_code;
412 mcck = this_cpu_ptr(&cpu_mcck);
413
414 /*
415 * Reinject the instruction processing damages' machine checks
416 * including Delayed Access Exception into the guest
417 * instead of damaging the host if they happen in the guest.
418 */
419 if (mci.pd && !test_cpu_flag(CIF_MCCK_GUEST)) {
420 if (mci.b) {
421 /* Processing backup -> verify if we can survive this */
422 u64 z_mcic, o_mcic, t_mcic;
423 z_mcic = (1ULL<<63 | 1ULL<<59 | 1ULL<<29);
424 o_mcic = (1ULL<<43 | 1ULL<<42 | 1ULL<<41 | 1ULL<<40 |
425 1ULL<<36 | 1ULL<<35 | 1ULL<<34 | 1ULL<<32 |
426 1ULL<<30 | 1ULL<<21 | 1ULL<<20 | 1ULL<<17 |
427 1ULL<<16);
428 t_mcic = mci.val;
429
430 if (((t_mcic & z_mcic) != 0) ||
431 ((t_mcic & o_mcic) != o_mcic)) {
432 s390_handle_damage();
433 }
434
435 /*
436 * Nullifying exigent condition, therefore we might
437 * retry this instruction.
438 */
439 spin_lock(&ipd_lock);
440 tmp = get_tod_clock();
441 if (((tmp - last_ipd) >> 12) < MAX_IPD_TIME)
442 ipd_count++;
443 else
444 ipd_count = 1;
445 last_ipd = tmp;
446 if (ipd_count == MAX_IPD_COUNT)
447 s390_handle_damage();
448 spin_unlock(&ipd_lock);
449 } else {
450 /* Processing damage -> stopping machine */
451 s390_handle_damage();
452 }
453 }
454 if (s390_validate_registers(mci, user_mode(regs))) {
455 /*
456 * Couldn't restore all register contents for the
457 * user space process -> mark task for termination.
458 */
459 mcck->kill_task = 1;
460 mcck->mcck_code = mci.val;
461 mcck_pending = 1;
462 }
463
464 /*
465 * Backup the machine check's info if it happens when the guest
466 * is running.
467 */
468 if (test_cpu_flag(CIF_MCCK_GUEST))
469 s390_backup_mcck_info(regs);
470
471 if (mci.cd) {
472 /* Timing facility damage */
473 s390_handle_damage();
474 }
475 if (mci.ed && mci.ec) {
476 /* External damage */
477 if (S390_lowcore.external_damage_code & (1U << ED_STP_SYNC))
478 mcck->stp_queue |= stp_sync_check();
479 if (S390_lowcore.external_damage_code & (1U << ED_STP_ISLAND))
480 mcck->stp_queue |= stp_island_check();
481 mcck_pending = 1;
482 }
483
484 if (mci.cp) {
485 /* Channel report word pending */
486 mcck->channel_report = 1;
487 mcck_pending = 1;
488 }
489 if (mci.w) {
490 /* Warning pending */
491 mcck->warning = 1;
492 mcck_pending = 1;
493 }
494
495 /*
496 * If there are only Channel Report Pending and External Damage
497 * machine checks, they will not be reinjected into the guest
498 * because they refer to host conditions only.
499 */
500 mcck_dam_code = (mci.val & MCIC_SUBCLASS_MASK);
501 if (test_cpu_flag(CIF_MCCK_GUEST) &&
502 (mcck_dam_code & MCCK_CODE_NO_GUEST) != mcck_dam_code) {
503 /* Set exit reason code for host's later handling */
504 *((long *)(regs->gprs[15] + __SF_SIE_REASON)) = -EINTR;
505 }
506 clear_cpu_flag(CIF_MCCK_GUEST);
507
508 if (user_mode(regs) && mcck_pending) {
509 irqentry_nmi_exit(regs, irq_state);
510 return 1;
511 }
512
513 if (mcck_pending)
514 schedule_mcck_handler();
515
516 irqentry_nmi_exit(regs, irq_state);
517 return 0;
518 }
519 NOKPROBE_SYMBOL(s390_do_machine_check);
520
machine_check_init(void)521 static int __init machine_check_init(void)
522 {
523 ctl_set_bit(14, 25); /* enable external damage MCH */
524 ctl_set_bit(14, 27); /* enable system recovery MCH */
525 ctl_set_bit(14, 24); /* enable warning MCH */
526 return 0;
527 }
528 early_initcall(machine_check_init);
529