1 // SPDX-License-Identifier: GPL-2.0
3 * Machine check handler
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>,
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>
23 #include <linux/export.h>
24 #include <asm/lowcore.h>
27 #include <asm/cputime.h>
30 #include <asm/switch_to.h>
31 #include <asm/ctl_reg.h>
32 #include <asm/asm-offsets.h>
35 #include <linux/kvm_host.h>
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;
45 static DEFINE_PER_CPU(struct mcck_struct, cpu_mcck);
46 static struct kmem_cache *mcesa_cache;
47 static unsigned long mcesa_origin_lc;
49 static inline int nmi_needs_mcesa(void)
51 return MACHINE_HAS_VX || MACHINE_HAS_GS;
54 static inline unsigned long nmi_get_mcesa_size(void)
57 return MCESA_MAX_SIZE;
58 return MCESA_MIN_SIZE;
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.
67 static struct mcesa boot_mcesa __aligned(MCESA_MAX_SIZE);
69 void __init nmi_alloc_mcesa_early(u64 *mcesad)
71 if (!nmi_needs_mcesa())
73 *mcesad = __pa(&boot_mcesa);
75 *mcesad |= ilog2(MCESA_MAX_SIZE);
78 static void __init nmi_alloc_cache(void)
82 if (!nmi_needs_mcesa())
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);
90 panic("Couldn't create nmi save area cache");
93 int __ref nmi_alloc_mcesa(u64 *mcesad)
98 if (!nmi_needs_mcesa())
102 origin = (unsigned long) kmem_cache_alloc(mcesa_cache, GFP_KERNEL);
105 /* The pointer is stored with mcesa_bits ORed in */
106 kmemleak_not_leak((void *) origin);
107 *mcesad = __pa(origin) | mcesa_origin_lc;
111 void nmi_free_mcesa(u64 *mcesad)
113 if (!nmi_needs_mcesa())
115 kmem_cache_free(mcesa_cache, __va(*mcesad & MCESA_ORIGIN_MASK));
118 static notrace void s390_handle_damage(void)
120 smp_emergency_stop();
124 NOKPROBE_SYMBOL(s390_handle_damage);
127 * Main machine check handler function. Will be called with interrupts disabled
128 * and machine checks enabled.
130 void __s390_handle_mcck(void)
132 struct mcck_struct mcck;
135 * Disable machine checks and get the current state of accumulated
136 * machine checks. Afterwards delete the old state and enable machine
139 local_mcck_disable();
140 mcck = *this_cpu_ptr(&cpu_mcck);
141 memset(this_cpu_ptr(&cpu_mcck), 0, sizeof(mcck));
144 if (mcck.channel_report)
145 crw_handle_channel_report();
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.
155 if (mcck.warning) { /* WARNING pending ? */
156 static int mchchk_wng_posted = 0;
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);
165 if (mcck.kill_task) {
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);
175 void noinstr s390_handle_mcck(struct pt_regs *regs)
177 trace_hardirqs_off();
178 pai_kernel_enter(regs);
179 __s390_handle_mcck();
180 pai_kernel_exit(regs);
184 * returns 0 if all required registers are available
185 * returns 1 otherwise
187 static int notrace s390_validate_registers(union mci mci, int umode)
200 * General purpose registers couldn't be restored and have
201 * unknown contents. Stop system or terminate process.
204 s390_handle_damage();
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.
214 if (S390_lowcore.fpu_flags & KERNEL_VXR_V0V7)
215 s390_handle_damage();
216 if (!test_cpu_flag(CIF_FPU))
219 fpt_save_area = &S390_lowcore.floating_pt_save_area;
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.
229 if (S390_lowcore.fpu_flags & KERNEL_FPC)
230 s390_handle_damage();
235 if (!test_cpu_flag(CIF_FPU))
241 : "Q" (S390_lowcore.fpt_creg_save_area));
244 mcesa = __va(S390_lowcore.mcesad & MCESA_ORIGIN_MASK);
245 if (!MACHINE_HAS_VX) {
246 /* Validate floating point registers */
265 : "a" (fpt_save_area)
268 /* Validate vector registers */
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.
278 if (!mci.vr && !test_cpu_flag(CIF_MCCK_GUEST)) {
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
286 if (S390_lowcore.fpu_flags & KERNEL_VXR)
287 s390_handle_damage();
288 if (!test_cpu_flag(CIF_FPU))
291 cr0.val = S390_lowcore.cregs_save_area[0];
292 cr0.afp = cr0.vx = 1;
293 __ctl_load(cr0.val, 0, 0);
296 " .word 0xe70f,0x1000,0x0036\n" /* vlm 0,15,0(1) */
297 " .word 0xe70f,0x1100,0x0c36\n" /* vlm 16,31,256(1) */
299 : "Q" (*(struct vx_array *)mcesa->vector_save_area)
301 __ctl_load(S390_lowcore.cregs_save_area[0], 0, 0);
303 /* Validate access registers */
307 : "a" (&S390_lowcore.access_regs_save_area)
311 * Access registers have unknown contents.
316 /* Validate guarded storage registers */
317 cr2.val = S390_lowcore.cregs_save_area[2];
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.
334 if (!test_cpu_flag(CIF_MCCK_GUEST))
337 load_gs_cb((struct gs_cb *)mcesa->guarded_storage_save_area);
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.
346 set_tod_programmable_field(raw_smp_processor_id());
347 /* Validate clock comparator register */
348 set_clock_comparator(S390_lowcore.clock_comparator);
350 if (!mci.ms || !mci.pm || !mci.ia)
355 NOKPROBE_SYMBOL(s390_validate_registers);
358 * Backup the guest's machine check info to its description block
360 static void notrace s390_backup_mcck_info(struct pt_regs *regs)
362 struct mcck_volatile_info *mcck_backup;
363 struct sie_page *sie_page;
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];
369 if (sie_block == NULL)
370 /* Something's seriously wrong, stop system. */
371 s390_handle_damage();
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;
381 NOKPROBE_SYMBOL(s390_backup_mcck_info);
383 #define MAX_IPD_COUNT 29
384 #define MAX_IPD_TIME (5 * 60 * USEC_PER_SEC) /* 5 minutes */
386 #define ED_STP_ISLAND 6 /* External damage STP island check */
387 #define ED_STP_SYNC 7 /* External damage STP sync check */
389 #define MCCK_CODE_NO_GUEST (MCCK_CODE_CP | MCCK_CODE_EXT_DAMAGE)
392 * machine check handler.
394 int notrace s390_do_machine_check(struct pt_regs *regs)
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;
403 unsigned long mcck_dam_code;
404 int mcck_pending = 0;
406 irq_state = irqentry_nmi_enter(regs);
410 inc_irq_stat(NMI_NMI);
411 mci.val = S390_lowcore.mcck_interruption_code;
412 mcck = this_cpu_ptr(&cpu_mcck);
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.
419 if (mci.pd && !test_cpu_flag(CIF_MCCK_GUEST)) {
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 |
430 if (((t_mcic & z_mcic) != 0) ||
431 ((t_mcic & o_mcic) != o_mcic)) {
432 s390_handle_damage();
436 * Nullifying exigent condition, therefore we might
437 * retry this instruction.
439 spin_lock(&ipd_lock);
440 tmp = get_tod_clock();
441 if (((tmp - last_ipd) >> 12) < MAX_IPD_TIME)
446 if (ipd_count == MAX_IPD_COUNT)
447 s390_handle_damage();
448 spin_unlock(&ipd_lock);
450 /* Processing damage -> stopping machine */
451 s390_handle_damage();
454 if (s390_validate_registers(mci, user_mode(regs))) {
456 * Couldn't restore all register contents for the
457 * user space process -> mark task for termination.
460 mcck->mcck_code = mci.val;
465 * Backup the machine check's info if it happens when the guest
468 if (test_cpu_flag(CIF_MCCK_GUEST))
469 s390_backup_mcck_info(regs);
472 /* Timing facility damage */
473 s390_handle_damage();
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();
485 /* Channel report word pending */
486 mcck->channel_report = 1;
490 /* Warning pending */
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.
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;
506 clear_cpu_flag(CIF_MCCK_GUEST);
508 if (user_mode(regs) && mcck_pending) {
509 irqentry_nmi_exit(regs, irq_state);
514 schedule_mcck_handler();
516 irqentry_nmi_exit(regs, irq_state);
519 NOKPROBE_SYMBOL(s390_do_machine_check);
521 static int __init machine_check_init(void)
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 */
528 early_initcall(machine_check_init);