2 * Machine check handler
4 * Copyright IBM Corp. 2000, 2009
5 * Author(s): Ingo Adlung <adlung@de.ibm.com>,
6 * Martin Schwidefsky <schwidefsky@de.ibm.com>,
7 * Cornelia Huck <cornelia.huck@de.ibm.com>,
8 * Heiko Carstens <heiko.carstens@de.ibm.com>,
11 #include <linux/kernel_stat.h>
12 #include <linux/init.h>
13 #include <linux/errno.h>
14 #include <linux/hardirq.h>
15 #include <linux/time.h>
16 #include <linux/module.h>
17 #include <linux/sched/signal.h>
19 #include <linux/export.h>
20 #include <asm/lowcore.h>
23 #include <asm/cputime.h>
26 #include <asm/switch_to.h>
27 #include <asm/ctl_reg.h>
28 #include <asm/asm-offsets.h>
29 #include <linux/kvm_host.h>
32 unsigned int kill_task
: 1;
33 unsigned int channel_report
: 1;
34 unsigned int warning
: 1;
35 unsigned int stp_queue
: 1;
36 unsigned long mcck_code
;
39 static DEFINE_PER_CPU(struct mcck_struct
, cpu_mcck
);
41 static void s390_handle_damage(void)
44 disabled_wait((unsigned long) __builtin_return_address(0));
49 * Main machine check handler function. Will be called with interrupts enabled
50 * or disabled and machine checks enabled or disabled.
52 void s390_handle_mcck(void)
55 struct mcck_struct mcck
;
58 * Disable machine checks and get the current state of accumulated
59 * machine checks. Afterwards delete the old state and enable machine
62 local_irq_save(flags
);
64 mcck
= *this_cpu_ptr(&cpu_mcck
);
65 memset(this_cpu_ptr(&cpu_mcck
), 0, sizeof(mcck
));
66 clear_cpu_flag(CIF_MCCK_PENDING
);
68 local_irq_restore(flags
);
70 if (mcck
.channel_report
)
71 crw_handle_channel_report();
73 * A warning may remain for a prolonged period on the bare iron.
74 * (actually until the machine is powered off, or the problem is gone)
75 * So we just stop listening for the WARNING MCH and avoid continuously
76 * being interrupted. One caveat is however, that we must do this per
77 * processor and cannot use the smp version of ctl_clear_bit().
78 * On VM we only get one interrupt per virtally presented machinecheck.
79 * Though one suffices, we may get one interrupt per (virtual) cpu.
81 if (mcck
.warning
) { /* WARNING pending ? */
82 static int mchchk_wng_posted
= 0;
84 /* Use single cpu clear, as we cannot handle smp here. */
85 __ctl_clear_bit(14, 24); /* Disable WARNING MCH */
86 if (xchg(&mchchk_wng_posted
, 1) == 0)
87 kill_cad_pid(SIGPWR
, 1);
93 printk(KERN_EMERG
"mcck: Terminating task because of machine "
94 "malfunction (code 0x%016lx).\n", mcck
.mcck_code
);
95 printk(KERN_EMERG
"mcck: task: %s, pid: %d.\n",
96 current
->comm
, current
->pid
);
100 EXPORT_SYMBOL_GPL(s390_handle_mcck
);
103 * returns 0 if all registers could be validated
104 * returns 1 otherwise
106 static int notrace
s390_validate_registers(union mci mci
, int umode
)
118 * General purpose registers couldn't be restored and have
119 * unknown contents. Stop system or terminate process.
122 s390_handle_damage();
125 /* Validate control registers */
128 * Control registers have unknown contents.
129 * Can't recover and therefore stopping machine.
131 s390_handle_damage();
134 " lctlg 0,15,0(%0)\n"
136 : : "a" (&S390_lowcore
.cregs_save_area
) : "memory");
140 * Floating point registers can't be restored. If the
141 * kernel currently uses floating point registers the
142 * system is stopped. If the process has its floating
143 * pointer registers loaded it is terminated.
144 * Otherwise just revalidate the registers.
146 if (S390_lowcore
.fpu_flags
& KERNEL_VXR_V0V7
)
147 s390_handle_damage();
148 if (!test_cpu_flag(CIF_FPU
))
151 fpt_save_area
= &S390_lowcore
.floating_pt_save_area
;
154 * Floating point control register can't be restored.
155 * If the kernel currently uses the floating pointer
156 * registers and needs the FPC register the system is
157 * stopped. If the process has its floating pointer
158 * registers loaded it is terminated. Otherwiese the
159 * FPC is just revalidated.
161 if (S390_lowcore
.fpu_flags
& KERNEL_FPC
)
162 s390_handle_damage();
163 asm volatile("lfpc %0" : : "Q" (zero
));
164 if (!test_cpu_flag(CIF_FPU
))
167 asm volatile("lfpc %0"
168 : : "Q" (S390_lowcore
.fpt_creg_save_area
));
171 mcesa
= (struct mcesa
*)(S390_lowcore
.mcesad
& MCESA_ORIGIN_MASK
);
172 if (!MACHINE_HAS_VX
) {
173 /* Validate floating point registers */
191 : : "a" (fpt_save_area
) : "memory");
193 /* Validate vector registers */
198 * Vector registers can't be restored. If the kernel
199 * currently uses vector registers the system is
200 * stopped. If the process has its vector registers
201 * loaded it is terminated. Otherwise just revalidate
204 if (S390_lowcore
.fpu_flags
& KERNEL_VXR
)
205 s390_handle_damage();
206 if (!test_cpu_flag(CIF_FPU
))
209 cr0
.val
= S390_lowcore
.cregs_save_area
[0];
210 cr0
.afp
= cr0
.vx
= 1;
211 __ctl_load(cr0
.val
, 0, 0);
214 " .word 0xe70f,0x1000,0x0036\n" /* vlm 0,15,0(1) */
215 " .word 0xe70f,0x1100,0x0c36\n" /* vlm 16,31,256(1) */
216 : : "Q" (*(struct vx_array
*) mcesa
->vector_save_area
)
218 __ctl_load(S390_lowcore
.cregs_save_area
[0], 0, 0);
220 /* Validate access registers */
223 : : "a" (&S390_lowcore
.access_regs_save_area
));
226 * Access registers have unknown contents.
231 /* Validate guarded storage registers */
232 if (MACHINE_HAS_GS
&& (S390_lowcore
.cregs_save_area
[2] & (1UL << 4))) {
235 * Guarded storage register can't be restored and
236 * the current processes uses guarded storage.
237 * It has to be terminated.
241 load_gs_cb((struct gs_cb
*)
242 mcesa
->guarded_storage_save_area
);
245 * We don't even try to validate the TOD register, since we simply
246 * can't write something sensible into that register.
249 * See if we can validate the TOD programmable register with its
250 * old contents (should be zero) otherwise set it to zero.
261 : : "Q" (S390_lowcore
.tod_progreg_save_area
)
263 /* Validate clock comparator register */
264 set_clock_comparator(S390_lowcore
.clock_comparator
);
265 /* Check if old PSW is valid */
268 * Can't tell if we come from user or kernel mode
269 * -> stopping machine.
271 s390_handle_damage();
273 if (!mci
.ms
|| !mci
.pm
|| !mci
.ia
)
280 * Backup the guest's machine check info to its description block
282 static void notrace
s390_backup_mcck_info(struct pt_regs
*regs
)
284 struct mcck_volatile_info
*mcck_backup
;
285 struct sie_page
*sie_page
;
287 /* r14 contains the sie block, which was set in sie64a */
288 struct kvm_s390_sie_block
*sie_block
=
289 (struct kvm_s390_sie_block
*) regs
->gprs
[14];
291 if (sie_block
== NULL
)
292 /* Something's seriously wrong, stop system. */
293 s390_handle_damage();
295 sie_page
= container_of(sie_block
, struct sie_page
, sie_block
);
296 mcck_backup
= &sie_page
->mcck_info
;
297 mcck_backup
->mcic
= S390_lowcore
.mcck_interruption_code
&
298 ~(MCCK_CODE_CP
| MCCK_CODE_EXT_DAMAGE
);
299 mcck_backup
->ext_damage_code
= S390_lowcore
.external_damage_code
;
300 mcck_backup
->failing_storage_address
301 = S390_lowcore
.failing_storage_address
;
304 #define MAX_IPD_COUNT 29
305 #define MAX_IPD_TIME (5 * 60 * USEC_PER_SEC) /* 5 minutes */
307 #define ED_STP_ISLAND 6 /* External damage STP island check */
308 #define ED_STP_SYNC 7 /* External damage STP sync check */
310 #define MCCK_CODE_NO_GUEST (MCCK_CODE_CP | MCCK_CODE_EXT_DAMAGE)
313 * machine check handler.
315 void notrace
s390_do_machine_check(struct pt_regs
*regs
)
317 static int ipd_count
;
318 static DEFINE_SPINLOCK(ipd_lock
);
319 static unsigned long long last_ipd
;
320 struct mcck_struct
*mcck
;
321 unsigned long long tmp
;
323 unsigned long mcck_dam_code
;
326 inc_irq_stat(NMI_NMI
);
327 mci
.val
= S390_lowcore
.mcck_interruption_code
;
328 mcck
= this_cpu_ptr(&cpu_mcck
);
331 /* System damage -> stopping machine */
332 s390_handle_damage();
336 * Reinject the instruction processing damages' machine checks
337 * including Delayed Access Exception into the guest
338 * instead of damaging the host if they happen in the guest.
340 if (mci
.pd
&& !test_cpu_flag(CIF_MCCK_GUEST
)) {
342 /* Processing backup -> verify if we can survive this */
343 u64 z_mcic
, o_mcic
, t_mcic
;
344 z_mcic
= (1ULL<<63 | 1ULL<<59 | 1ULL<<29);
345 o_mcic
= (1ULL<<43 | 1ULL<<42 | 1ULL<<41 | 1ULL<<40 |
346 1ULL<<36 | 1ULL<<35 | 1ULL<<34 | 1ULL<<32 |
347 1ULL<<30 | 1ULL<<21 | 1ULL<<20 | 1ULL<<17 |
351 if (((t_mcic
& z_mcic
) != 0) ||
352 ((t_mcic
& o_mcic
) != o_mcic
)) {
353 s390_handle_damage();
357 * Nullifying exigent condition, therefore we might
358 * retry this instruction.
360 spin_lock(&ipd_lock
);
361 tmp
= get_tod_clock();
362 if (((tmp
- last_ipd
) >> 12) < MAX_IPD_TIME
)
367 if (ipd_count
== MAX_IPD_COUNT
)
368 s390_handle_damage();
369 spin_unlock(&ipd_lock
);
371 /* Processing damage -> stopping machine */
372 s390_handle_damage();
375 if (s390_validate_registers(mci
, user_mode(regs
))) {
377 * Couldn't restore all register contents for the
378 * user space process -> mark task for termination.
381 mcck
->mcck_code
= mci
.val
;
382 set_cpu_flag(CIF_MCCK_PENDING
);
386 * Backup the machine check's info if it happens when the guest
389 if (test_cpu_flag(CIF_MCCK_GUEST
))
390 s390_backup_mcck_info(regs
);
393 /* Timing facility damage */
394 s390_handle_damage();
396 if (mci
.ed
&& mci
.ec
) {
397 /* External damage */
398 if (S390_lowcore
.external_damage_code
& (1U << ED_STP_SYNC
))
399 mcck
->stp_queue
|= stp_sync_check();
400 if (S390_lowcore
.external_damage_code
& (1U << ED_STP_ISLAND
))
401 mcck
->stp_queue
|= stp_island_check();
403 set_cpu_flag(CIF_MCCK_PENDING
);
407 * Reinject storage related machine checks into the guest if they
408 * happen when the guest is running.
410 if (!test_cpu_flag(CIF_MCCK_GUEST
)) {
412 /* Storage error uncorrected */
413 s390_handle_damage();
415 /* Storage key-error uncorrected */
416 s390_handle_damage();
417 if (mci
.ds
&& mci
.fa
)
418 /* Storage degradation */
419 s390_handle_damage();
422 /* Channel report word pending */
423 mcck
->channel_report
= 1;
424 set_cpu_flag(CIF_MCCK_PENDING
);
427 /* Warning pending */
429 set_cpu_flag(CIF_MCCK_PENDING
);
433 * If there are only Channel Report Pending and External Damage
434 * machine checks, they will not be reinjected into the guest
435 * because they refer to host conditions only.
437 mcck_dam_code
= (mci
.val
& MCIC_SUBCLASS_MASK
);
438 if (test_cpu_flag(CIF_MCCK_GUEST
) &&
439 (mcck_dam_code
& MCCK_CODE_NO_GUEST
) != mcck_dam_code
) {
440 /* Set exit reason code for host's later handling */
441 *((long *)(regs
->gprs
[15] + __SF_SIE_REASON
)) = -EINTR
;
443 clear_cpu_flag(CIF_MCCK_GUEST
);
447 static int __init
machine_check_init(void)
449 ctl_set_bit(14, 25); /* enable external damage MCH */
450 ctl_set_bit(14, 27); /* enable system recovery MCH */
451 ctl_set_bit(14, 24); /* enable warning MCH */
454 early_initcall(machine_check_init
);