Linux 4.11-rc5
[linux/fpc-iii.git] / arch / s390 / kernel / nmi.c
blob9bf8327154eeee8442eafdbf7b2352b4d900922e
1 /*
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>,
9 */
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>
21 #include <asm/smp.h>
22 #include <asm/stp.h>
23 #include <asm/cputime.h>
24 #include <asm/nmi.h>
25 #include <asm/crw.h>
26 #include <asm/switch_to.h>
27 #include <asm/ctl_reg.h>
29 struct mcck_struct {
30 unsigned int kill_task : 1;
31 unsigned int channel_report : 1;
32 unsigned int warning : 1;
33 unsigned int stp_queue : 1;
34 unsigned long mcck_code;
37 static DEFINE_PER_CPU(struct mcck_struct, cpu_mcck);
39 static void s390_handle_damage(void)
41 smp_send_stop();
42 disabled_wait((unsigned long) __builtin_return_address(0));
43 while (1);
47 * Main machine check handler function. Will be called with interrupts enabled
48 * or disabled and machine checks enabled or disabled.
50 void s390_handle_mcck(void)
52 unsigned long flags;
53 struct mcck_struct mcck;
56 * Disable machine checks and get the current state of accumulated
57 * machine checks. Afterwards delete the old state and enable machine
58 * checks again.
60 local_irq_save(flags);
61 local_mcck_disable();
62 mcck = *this_cpu_ptr(&cpu_mcck);
63 memset(this_cpu_ptr(&cpu_mcck), 0, sizeof(mcck));
64 clear_cpu_flag(CIF_MCCK_PENDING);
65 local_mcck_enable();
66 local_irq_restore(flags);
68 if (mcck.channel_report)
69 crw_handle_channel_report();
71 * A warning may remain for a prolonged period on the bare iron.
72 * (actually until the machine is powered off, or the problem is gone)
73 * So we just stop listening for the WARNING MCH and avoid continuously
74 * being interrupted. One caveat is however, that we must do this per
75 * processor and cannot use the smp version of ctl_clear_bit().
76 * On VM we only get one interrupt per virtally presented machinecheck.
77 * Though one suffices, we may get one interrupt per (virtual) cpu.
79 if (mcck.warning) { /* WARNING pending ? */
80 static int mchchk_wng_posted = 0;
82 /* Use single cpu clear, as we cannot handle smp here. */
83 __ctl_clear_bit(14, 24); /* Disable WARNING MCH */
84 if (xchg(&mchchk_wng_posted, 1) == 0)
85 kill_cad_pid(SIGPWR, 1);
87 if (mcck.stp_queue)
88 stp_queue_work();
89 if (mcck.kill_task) {
90 local_irq_enable();
91 printk(KERN_EMERG "mcck: Terminating task because of machine "
92 "malfunction (code 0x%016lx).\n", mcck.mcck_code);
93 printk(KERN_EMERG "mcck: task: %s, pid: %d.\n",
94 current->comm, current->pid);
95 do_exit(SIGSEGV);
98 EXPORT_SYMBOL_GPL(s390_handle_mcck);
101 * returns 0 if all registers could be validated
102 * returns 1 otherwise
104 static int notrace s390_validate_registers(union mci mci, int umode)
106 int kill_task;
107 u64 zero;
108 void *fpt_save_area;
110 kill_task = 0;
111 zero = 0;
113 if (!mci.gr) {
115 * General purpose registers couldn't be restored and have
116 * unknown contents. Stop system or terminate process.
118 if (!umode)
119 s390_handle_damage();
120 kill_task = 1;
122 /* Validate control registers */
123 if (!mci.cr) {
125 * Control registers have unknown contents.
126 * Can't recover and therefore stopping machine.
128 s390_handle_damage();
129 } else {
130 asm volatile(
131 " lctlg 0,15,0(%0)\n"
132 " ptlb\n"
133 : : "a" (&S390_lowcore.cregs_save_area) : "memory");
135 if (!mci.fp) {
137 * Floating point registers can't be restored. If the
138 * kernel currently uses floating point registers the
139 * system is stopped. If the process has its floating
140 * pointer registers loaded it is terminated.
141 * Otherwise just revalidate the registers.
143 if (S390_lowcore.fpu_flags & KERNEL_VXR_V0V7)
144 s390_handle_damage();
145 if (!test_cpu_flag(CIF_FPU))
146 kill_task = 1;
148 fpt_save_area = &S390_lowcore.floating_pt_save_area;
149 if (!mci.fc) {
151 * Floating point control register can't be restored.
152 * If the kernel currently uses the floating pointer
153 * registers and needs the FPC register the system is
154 * stopped. If the process has its floating pointer
155 * registers loaded it is terminated. Otherwiese the
156 * FPC is just revalidated.
158 if (S390_lowcore.fpu_flags & KERNEL_FPC)
159 s390_handle_damage();
160 asm volatile("lfpc %0" : : "Q" (zero));
161 if (!test_cpu_flag(CIF_FPU))
162 kill_task = 1;
163 } else {
164 asm volatile("lfpc %0"
165 : : "Q" (S390_lowcore.fpt_creg_save_area));
168 if (!MACHINE_HAS_VX) {
169 /* Validate floating point registers */
170 asm volatile(
171 " ld 0,0(%0)\n"
172 " ld 1,8(%0)\n"
173 " ld 2,16(%0)\n"
174 " ld 3,24(%0)\n"
175 " ld 4,32(%0)\n"
176 " ld 5,40(%0)\n"
177 " ld 6,48(%0)\n"
178 " ld 7,56(%0)\n"
179 " ld 8,64(%0)\n"
180 " ld 9,72(%0)\n"
181 " ld 10,80(%0)\n"
182 " ld 11,88(%0)\n"
183 " ld 12,96(%0)\n"
184 " ld 13,104(%0)\n"
185 " ld 14,112(%0)\n"
186 " ld 15,120(%0)\n"
187 : : "a" (fpt_save_area) : "memory");
188 } else {
189 /* Validate vector registers */
190 union ctlreg0 cr0;
192 if (!mci.vr) {
194 * Vector registers can't be restored. If the kernel
195 * currently uses vector registers the system is
196 * stopped. If the process has its vector registers
197 * loaded it is terminated. Otherwise just revalidate
198 * the registers.
200 if (S390_lowcore.fpu_flags & KERNEL_VXR)
201 s390_handle_damage();
202 if (!test_cpu_flag(CIF_FPU))
203 kill_task = 1;
205 cr0.val = S390_lowcore.cregs_save_area[0];
206 cr0.afp = cr0.vx = 1;
207 __ctl_load(cr0.val, 0, 0);
208 asm volatile(
209 " la 1,%0\n"
210 " .word 0xe70f,0x1000,0x0036\n" /* vlm 0,15,0(1) */
211 " .word 0xe70f,0x1100,0x0c36\n" /* vlm 16,31,256(1) */
212 : : "Q" (*(struct vx_array *)
213 &S390_lowcore.vector_save_area) : "1");
214 __ctl_load(S390_lowcore.cregs_save_area[0], 0, 0);
216 /* Validate access registers */
217 asm volatile(
218 " lam 0,15,0(%0)"
219 : : "a" (&S390_lowcore.access_regs_save_area));
220 if (!mci.ar) {
222 * Access registers have unknown contents.
223 * Terminating task.
225 kill_task = 1;
228 * We don't even try to validate the TOD register, since we simply
229 * can't write something sensible into that register.
232 * See if we can validate the TOD programmable register with its
233 * old contents (should be zero) otherwise set it to zero.
235 if (!mci.pr)
236 asm volatile(
237 " sr 0,0\n"
238 " sckpf"
239 : : : "0", "cc");
240 else
241 asm volatile(
242 " l 0,%0\n"
243 " sckpf"
244 : : "Q" (S390_lowcore.tod_progreg_save_area)
245 : "0", "cc");
246 /* Validate clock comparator register */
247 set_clock_comparator(S390_lowcore.clock_comparator);
248 /* Check if old PSW is valid */
249 if (!mci.wp)
251 * Can't tell if we come from user or kernel mode
252 * -> stopping machine.
254 s390_handle_damage();
256 if (!mci.ms || !mci.pm || !mci.ia)
257 kill_task = 1;
259 return kill_task;
262 #define MAX_IPD_COUNT 29
263 #define MAX_IPD_TIME (5 * 60 * USEC_PER_SEC) /* 5 minutes */
265 #define ED_STP_ISLAND 6 /* External damage STP island check */
266 #define ED_STP_SYNC 7 /* External damage STP sync check */
269 * machine check handler.
271 void notrace s390_do_machine_check(struct pt_regs *regs)
273 static int ipd_count;
274 static DEFINE_SPINLOCK(ipd_lock);
275 static unsigned long long last_ipd;
276 struct mcck_struct *mcck;
277 unsigned long long tmp;
278 union mci mci;
280 nmi_enter();
281 inc_irq_stat(NMI_NMI);
282 mci.val = S390_lowcore.mcck_interruption_code;
283 mcck = this_cpu_ptr(&cpu_mcck);
285 if (mci.sd) {
286 /* System damage -> stopping machine */
287 s390_handle_damage();
289 if (mci.pd) {
290 if (mci.b) {
291 /* Processing backup -> verify if we can survive this */
292 u64 z_mcic, o_mcic, t_mcic;
293 z_mcic = (1ULL<<63 | 1ULL<<59 | 1ULL<<29);
294 o_mcic = (1ULL<<43 | 1ULL<<42 | 1ULL<<41 | 1ULL<<40 |
295 1ULL<<36 | 1ULL<<35 | 1ULL<<34 | 1ULL<<32 |
296 1ULL<<30 | 1ULL<<21 | 1ULL<<20 | 1ULL<<17 |
297 1ULL<<16);
298 t_mcic = mci.val;
300 if (((t_mcic & z_mcic) != 0) ||
301 ((t_mcic & o_mcic) != o_mcic)) {
302 s390_handle_damage();
306 * Nullifying exigent condition, therefore we might
307 * retry this instruction.
309 spin_lock(&ipd_lock);
310 tmp = get_tod_clock();
311 if (((tmp - last_ipd) >> 12) < MAX_IPD_TIME)
312 ipd_count++;
313 else
314 ipd_count = 1;
315 last_ipd = tmp;
316 if (ipd_count == MAX_IPD_COUNT)
317 s390_handle_damage();
318 spin_unlock(&ipd_lock);
319 } else {
320 /* Processing damage -> stopping machine */
321 s390_handle_damage();
324 if (s390_validate_registers(mci, user_mode(regs))) {
326 * Couldn't restore all register contents for the
327 * user space process -> mark task for termination.
329 mcck->kill_task = 1;
330 mcck->mcck_code = mci.val;
331 set_cpu_flag(CIF_MCCK_PENDING);
333 if (mci.cd) {
334 /* Timing facility damage */
335 s390_handle_damage();
337 if (mci.ed && mci.ec) {
338 /* External damage */
339 if (S390_lowcore.external_damage_code & (1U << ED_STP_SYNC))
340 mcck->stp_queue |= stp_sync_check();
341 if (S390_lowcore.external_damage_code & (1U << ED_STP_ISLAND))
342 mcck->stp_queue |= stp_island_check();
343 if (mcck->stp_queue)
344 set_cpu_flag(CIF_MCCK_PENDING);
346 if (mci.se)
347 /* Storage error uncorrected */
348 s390_handle_damage();
349 if (mci.ke)
350 /* Storage key-error uncorrected */
351 s390_handle_damage();
352 if (mci.ds && mci.fa)
353 /* Storage degradation */
354 s390_handle_damage();
355 if (mci.cp) {
356 /* Channel report word pending */
357 mcck->channel_report = 1;
358 set_cpu_flag(CIF_MCCK_PENDING);
360 if (mci.w) {
361 /* Warning pending */
362 mcck->warning = 1;
363 set_cpu_flag(CIF_MCCK_PENDING);
365 nmi_exit();
368 static int __init machine_check_init(void)
370 ctl_set_bit(14, 25); /* enable external damage MCH */
371 ctl_set_bit(14, 27); /* enable system recovery MCH */
372 ctl_set_bit(14, 24); /* enable warning MCH */
373 return 0;
375 early_initcall(machine_check_init);