ia64/kvm: compilation fix. export account_system_vtime.
[pv_ops_mirror.git] / drivers / s390 / s390mach.c
blob5080f343ad7482ebac5addfc19768ac5adfeeae2
1 /*
2 * drivers/s390/s390mach.c
3 * S/390 machine check handler
5 * S390 version
6 * Copyright (C) 2000 IBM Deutschland Entwicklung GmbH, IBM Corporation
7 * Author(s): Ingo Adlung (adlung@de.ibm.com)
8 * Martin Schwidefsky (schwidefsky@de.ibm.com)
9 */
11 #include <linux/init.h>
12 #include <linux/sched.h>
13 #include <linux/errno.h>
14 #include <linux/workqueue.h>
15 #include <linux/time.h>
16 #include <linux/device.h>
17 #include <linux/kthread.h>
18 #include <asm/etr.h>
19 #include <asm/lowcore.h>
20 #include <asm/cio.h>
21 #include "cio/cio.h"
22 #include "cio/chsc.h"
23 #include "cio/css.h"
24 #include "cio/chp.h"
25 #include "s390mach.h"
27 static struct semaphore m_sem;
29 static NORET_TYPE void
30 s390_handle_damage(char *msg)
32 #ifdef CONFIG_SMP
33 smp_send_stop();
34 #endif
35 disabled_wait((unsigned long) __builtin_return_address(0));
36 for(;;);
40 * Retrieve CRWs and call function to handle event.
42 * Note : we currently process CRWs for io and chsc subchannels only
44 static int
45 s390_collect_crw_info(void *param)
47 struct crw crw[2];
48 int ccode;
49 struct semaphore *sem;
50 unsigned int chain;
51 int ignore;
53 sem = (struct semaphore *)param;
54 repeat:
55 ignore = down_interruptible(sem);
56 chain = 0;
57 while (1) {
58 if (unlikely(chain > 1)) {
59 struct crw tmp_crw;
61 printk(KERN_WARNING"%s: Code does not support more "
62 "than two chained crws; please report to "
63 "linux390@de.ibm.com!\n", __func__);
64 ccode = stcrw(&tmp_crw);
65 printk(KERN_WARNING"%s: crw reports slct=%d, oflw=%d, "
66 "chn=%d, rsc=%X, anc=%d, erc=%X, rsid=%X\n",
67 __func__, tmp_crw.slct, tmp_crw.oflw,
68 tmp_crw.chn, tmp_crw.rsc, tmp_crw.anc,
69 tmp_crw.erc, tmp_crw.rsid);
70 printk(KERN_WARNING"%s: This was crw number %x in the "
71 "chain\n", __func__, chain);
72 if (ccode != 0)
73 break;
74 chain = tmp_crw.chn ? chain + 1 : 0;
75 continue;
77 ccode = stcrw(&crw[chain]);
78 if (ccode != 0)
79 break;
80 printk(KERN_DEBUG "crw_info : CRW reports slct=%d, oflw=%d, "
81 "chn=%d, rsc=%X, anc=%d, erc=%X, rsid=%X\n",
82 crw[chain].slct, crw[chain].oflw, crw[chain].chn,
83 crw[chain].rsc, crw[chain].anc, crw[chain].erc,
84 crw[chain].rsid);
85 /* Check for overflows. */
86 if (crw[chain].oflw) {
87 pr_debug("%s: crw overflow detected!\n", __func__);
88 css_schedule_eval_all();
89 chain = 0;
90 continue;
92 switch (crw[chain].rsc) {
93 case CRW_RSC_SCH:
94 if (crw[0].chn && !chain)
95 break;
96 pr_debug("source is subchannel %04X\n", crw[0].rsid);
97 css_process_crw(crw[0].rsid, chain ? crw[1].rsid : 0);
98 break;
99 case CRW_RSC_MONITOR:
100 pr_debug("source is monitoring facility\n");
101 break;
102 case CRW_RSC_CPATH:
103 pr_debug("source is channel path %02X\n", crw[0].rsid);
105 * Check for solicited machine checks. These are
106 * created by reset channel path and need not be
107 * reported to the common I/O layer.
109 if (crw[chain].slct) {
110 pr_debug("solicited machine check for "
111 "channel path %02X\n", crw[0].rsid);
112 break;
114 switch (crw[0].erc) {
115 case CRW_ERC_IPARM: /* Path has come. */
116 chp_process_crw(crw[0].rsid, 1);
117 break;
118 case CRW_ERC_PERRI: /* Path has gone. */
119 case CRW_ERC_PERRN:
120 chp_process_crw(crw[0].rsid, 0);
121 break;
122 default:
123 pr_debug("Don't know how to handle erc=%x\n",
124 crw[0].erc);
126 break;
127 case CRW_RSC_CONFIG:
128 pr_debug("source is configuration-alert facility\n");
129 break;
130 case CRW_RSC_CSS:
131 pr_debug("source is channel subsystem\n");
132 chsc_process_crw();
133 break;
134 default:
135 pr_debug("unknown source\n");
136 break;
138 /* chain is always 0 or 1 here. */
139 chain = crw[chain].chn ? chain + 1 : 0;
141 goto repeat;
142 return 0;
145 struct mcck_struct {
146 int kill_task;
147 int channel_report;
148 int warning;
149 unsigned long long mcck_code;
152 static DEFINE_PER_CPU(struct mcck_struct, cpu_mcck);
155 * Main machine check handler function. Will be called with interrupts enabled
156 * or disabled and machine checks enabled or disabled.
158 void
159 s390_handle_mcck(void)
161 unsigned long flags;
162 struct mcck_struct mcck;
165 * Disable machine checks and get the current state of accumulated
166 * machine checks. Afterwards delete the old state and enable machine
167 * checks again.
169 local_irq_save(flags);
170 local_mcck_disable();
171 mcck = __get_cpu_var(cpu_mcck);
172 memset(&__get_cpu_var(cpu_mcck), 0, sizeof(struct mcck_struct));
173 clear_thread_flag(TIF_MCCK_PENDING);
174 local_mcck_enable();
175 local_irq_restore(flags);
177 if (mcck.channel_report)
178 up(&m_sem);
180 #ifdef CONFIG_MACHCHK_WARNING
182 * The warning may remain for a prolonged period on the bare iron.
183 * (actually till the machine is powered off, or until the problem is gone)
184 * So we just stop listening for the WARNING MCH and prevent continuously
185 * being interrupted. One caveat is however, that we must do this per
186 * processor and cannot use the smp version of ctl_clear_bit().
187 * On VM we only get one interrupt per virtally presented machinecheck.
188 * Though one suffices, we may get one interrupt per (virtual) processor.
190 if (mcck.warning) { /* WARNING pending ? */
191 static int mchchk_wng_posted = 0;
193 * Use single machine clear, as we cannot handle smp right now
195 __ctl_clear_bit(14, 24); /* Disable WARNING MCH */
196 if (xchg(&mchchk_wng_posted, 1) == 0)
197 kill_cad_pid(SIGPWR, 1);
199 #endif
201 if (mcck.kill_task) {
202 local_irq_enable();
203 printk(KERN_EMERG "mcck: Terminating task because of machine "
204 "malfunction (code 0x%016llx).\n", mcck.mcck_code);
205 printk(KERN_EMERG "mcck: task: %s, pid: %d.\n",
206 current->comm, current->pid);
207 do_exit(SIGSEGV);
212 * returns 0 if all registers could be validated
213 * returns 1 otherwise
215 static int
216 s390_revalidate_registers(struct mci *mci)
218 int kill_task;
219 u64 tmpclock;
220 u64 zero;
221 void *fpt_save_area, *fpt_creg_save_area;
223 kill_task = 0;
224 zero = 0;
225 /* General purpose registers */
226 if (!mci->gr)
228 * General purpose registers couldn't be restored and have
229 * unknown contents. Process needs to be terminated.
231 kill_task = 1;
233 /* Revalidate floating point registers */
234 if (!mci->fp)
236 * Floating point registers can't be restored and
237 * therefore the process needs to be terminated.
239 kill_task = 1;
241 #ifndef CONFIG_64BIT
242 asm volatile(
243 " ld 0,0(%0)\n"
244 " ld 2,8(%0)\n"
245 " ld 4,16(%0)\n"
246 " ld 6,24(%0)"
247 : : "a" (&S390_lowcore.floating_pt_save_area));
248 #endif
250 if (MACHINE_HAS_IEEE) {
251 #ifdef CONFIG_64BIT
252 fpt_save_area = &S390_lowcore.floating_pt_save_area;
253 fpt_creg_save_area = &S390_lowcore.fpt_creg_save_area;
254 #else
255 fpt_save_area = (void *) S390_lowcore.extended_save_area_addr;
256 fpt_creg_save_area = fpt_save_area+128;
257 #endif
258 /* Floating point control register */
259 if (!mci->fc) {
261 * Floating point control register can't be restored.
262 * Task will be terminated.
264 asm volatile("lfpc 0(%0)" : : "a" (&zero), "m" (zero));
265 kill_task = 1;
267 } else
268 asm volatile("lfpc 0(%0)" : : "a" (fpt_creg_save_area));
270 asm volatile(
271 " ld 0,0(%0)\n"
272 " ld 1,8(%0)\n"
273 " ld 2,16(%0)\n"
274 " ld 3,24(%0)\n"
275 " ld 4,32(%0)\n"
276 " ld 5,40(%0)\n"
277 " ld 6,48(%0)\n"
278 " ld 7,56(%0)\n"
279 " ld 8,64(%0)\n"
280 " ld 9,72(%0)\n"
281 " ld 10,80(%0)\n"
282 " ld 11,88(%0)\n"
283 " ld 12,96(%0)\n"
284 " ld 13,104(%0)\n"
285 " ld 14,112(%0)\n"
286 " ld 15,120(%0)\n"
287 : : "a" (fpt_save_area));
290 /* Revalidate access registers */
291 asm volatile(
292 " lam 0,15,0(%0)"
293 : : "a" (&S390_lowcore.access_regs_save_area));
294 if (!mci->ar)
296 * Access registers have unknown contents.
297 * Terminating task.
299 kill_task = 1;
301 /* Revalidate control registers */
302 if (!mci->cr)
304 * Control registers have unknown contents.
305 * Can't recover and therefore stopping machine.
307 s390_handle_damage("invalid control registers.");
308 else
309 #ifdef CONFIG_64BIT
310 asm volatile(
311 " lctlg 0,15,0(%0)"
312 : : "a" (&S390_lowcore.cregs_save_area));
313 #else
314 asm volatile(
315 " lctl 0,15,0(%0)"
316 : : "a" (&S390_lowcore.cregs_save_area));
317 #endif
320 * We don't even try to revalidate the TOD register, since we simply
321 * can't write something sensible into that register.
324 #ifdef CONFIG_64BIT
326 * See if we can revalidate the TOD programmable register with its
327 * old contents (should be zero) otherwise set it to zero.
329 if (!mci->pr)
330 asm volatile(
331 " sr 0,0\n"
332 " sckpf"
333 : : : "0", "cc");
334 else
335 asm volatile(
336 " l 0,0(%0)\n"
337 " sckpf"
338 : : "a" (&S390_lowcore.tod_progreg_save_area)
339 : "0", "cc");
340 #endif
342 /* Revalidate clock comparator register */
343 asm volatile(
344 " stck 0(%1)\n"
345 " sckc 0(%1)"
346 : "=m" (tmpclock) : "a" (&(tmpclock)) : "cc", "memory");
348 /* Check if old PSW is valid */
349 if (!mci->wp)
351 * Can't tell if we come from user or kernel mode
352 * -> stopping machine.
354 s390_handle_damage("old psw invalid.");
356 if (!mci->ms || !mci->pm || !mci->ia)
357 kill_task = 1;
359 return kill_task;
362 #define MAX_IPD_COUNT 29
363 #define MAX_IPD_TIME (5 * 60 * USEC_PER_SEC) /* 5 minutes */
366 * machine check handler.
368 void
369 s390_do_machine_check(struct pt_regs *regs)
371 static DEFINE_SPINLOCK(ipd_lock);
372 static unsigned long long last_ipd;
373 static int ipd_count;
374 unsigned long long tmp;
375 struct mci *mci;
376 struct mcck_struct *mcck;
377 int umode;
379 lockdep_off();
381 mci = (struct mci *) &S390_lowcore.mcck_interruption_code;
382 mcck = &__get_cpu_var(cpu_mcck);
383 umode = user_mode(regs);
385 if (mci->sd)
386 /* System damage -> stopping machine */
387 s390_handle_damage("received system damage machine check.");
389 if (mci->pd) {
390 if (mci->b) {
391 /* Processing backup -> verify if we can survive this */
392 u64 z_mcic, o_mcic, t_mcic;
393 #ifdef CONFIG_64BIT
394 z_mcic = (1ULL<<63 | 1ULL<<59 | 1ULL<<29);
395 o_mcic = (1ULL<<43 | 1ULL<<42 | 1ULL<<41 | 1ULL<<40 |
396 1ULL<<36 | 1ULL<<35 | 1ULL<<34 | 1ULL<<32 |
397 1ULL<<30 | 1ULL<<21 | 1ULL<<20 | 1ULL<<17 |
398 1ULL<<16);
399 #else
400 z_mcic = (1ULL<<63 | 1ULL<<59 | 1ULL<<57 | 1ULL<<50 |
401 1ULL<<29);
402 o_mcic = (1ULL<<43 | 1ULL<<42 | 1ULL<<41 | 1ULL<<40 |
403 1ULL<<36 | 1ULL<<35 | 1ULL<<34 | 1ULL<<32 |
404 1ULL<<30 | 1ULL<<20 | 1ULL<<17 | 1ULL<<16);
405 #endif
406 t_mcic = *(u64 *)mci;
408 if (((t_mcic & z_mcic) != 0) ||
409 ((t_mcic & o_mcic) != o_mcic)) {
410 s390_handle_damage("processing backup machine "
411 "check with damage.");
415 * Nullifying exigent condition, therefore we might
416 * retry this instruction.
419 spin_lock(&ipd_lock);
421 tmp = get_clock();
423 if (((tmp - last_ipd) >> 12) < MAX_IPD_TIME)
424 ipd_count++;
425 else
426 ipd_count = 1;
428 last_ipd = tmp;
430 if (ipd_count == MAX_IPD_COUNT)
431 s390_handle_damage("too many ipd retries.");
433 spin_unlock(&ipd_lock);
435 else {
436 /* Processing damage -> stopping machine */
437 s390_handle_damage("received instruction processing "
438 "damage machine check.");
441 if (s390_revalidate_registers(mci)) {
442 if (umode) {
444 * Couldn't restore all register contents while in
445 * user mode -> mark task for termination.
447 mcck->kill_task = 1;
448 mcck->mcck_code = *(unsigned long long *) mci;
449 set_thread_flag(TIF_MCCK_PENDING);
451 else
453 * Couldn't restore all register contents while in
454 * kernel mode -> stopping machine.
456 s390_handle_damage("unable to revalidate registers.");
459 if (mci->cd) {
460 /* Timing facility damage */
461 s390_handle_damage("TOD clock damaged");
464 if (mci->ed && mci->ec) {
465 /* External damage */
466 if (S390_lowcore.external_damage_code & (1U << ED_ETR_SYNC))
467 etr_sync_check();
468 if (S390_lowcore.external_damage_code & (1U << ED_ETR_SWITCH))
469 etr_switch_to_local();
472 if (mci->se)
473 /* Storage error uncorrected */
474 s390_handle_damage("received storage error uncorrected "
475 "machine check.");
477 if (mci->ke)
478 /* Storage key-error uncorrected */
479 s390_handle_damage("received storage key-error uncorrected "
480 "machine check.");
482 if (mci->ds && mci->fa)
483 /* Storage degradation */
484 s390_handle_damage("received storage degradation machine "
485 "check.");
487 if (mci->cp) {
488 /* Channel report word pending */
489 mcck->channel_report = 1;
490 set_thread_flag(TIF_MCCK_PENDING);
493 if (mci->w) {
494 /* Warning pending */
495 mcck->warning = 1;
496 set_thread_flag(TIF_MCCK_PENDING);
498 lockdep_on();
502 * s390_init_machine_check
504 * initialize machine check handling
506 static int
507 machine_check_init(void)
509 init_MUTEX_LOCKED(&m_sem);
510 ctl_set_bit(14, 25); /* enable external damage MCH */
511 ctl_set_bit(14, 27); /* enable system recovery MCH */
512 #ifdef CONFIG_MACHCHK_WARNING
513 ctl_set_bit(14, 24); /* enable warning MCH */
514 #endif
515 return 0;
519 * Initialize the machine check handler really early to be able to
520 * catch all machine checks that happen during boot
522 arch_initcall(machine_check_init);
525 * Machine checks for the channel subsystem must be enabled
526 * after the channel subsystem is initialized
528 static int __init
529 machine_check_crw_init (void)
531 struct task_struct *task;
533 task = kthread_run(s390_collect_crw_info, &m_sem, "kmcheck");
534 if (IS_ERR(task))
535 return PTR_ERR(task);
536 ctl_set_bit(14, 28); /* enable channel report MCH */
537 return 0;
540 device_initcall (machine_check_crw_init);