nmi.c 9.96 KB
Newer Older
Linus Torvalds's avatar
Linus Torvalds committed
1
/*
2
 *   Machine check handler
Linus Torvalds's avatar
Linus Torvalds committed
3
 *
4
 *    Copyright IBM Corp. 2000, 2009
5 6 7 8
 *    Author(s): Ingo Adlung <adlung@de.ibm.com>,
 *		 Martin Schwidefsky <schwidefsky@de.ibm.com>,
 *		 Cornelia Huck <cornelia.huck@de.ibm.com>,
 *		 Heiko Carstens <heiko.carstens@de.ibm.com>,
Linus Torvalds's avatar
Linus Torvalds committed
9 10
 */

11
#include <linux/kernel_stat.h>
Linus Torvalds's avatar
Linus Torvalds committed
12 13
#include <linux/init.h>
#include <linux/errno.h>
14
#include <linux/hardirq.h>
15
#include <linux/time.h>
16
#include <linux/module.h>
Linus Torvalds's avatar
Linus Torvalds committed
17
#include <asm/lowcore.h>
18 19
#include <asm/smp.h>
#include <asm/etr.h>
20
#include <asm/cputime.h>
21 22
#include <asm/nmi.h>
#include <asm/crw.h>
23
#include <asm/switch_to.h>
Linus Torvalds's avatar
Linus Torvalds committed
24

25 26 27 28 29 30 31 32 33
struct mcck_struct {
	int kill_task;
	int channel_report;
	int warning;
	unsigned long long mcck_code;
};

static DEFINE_PER_CPU(struct mcck_struct, cpu_mcck);

34
static void s390_handle_damage(char *msg)
35 36 37 38 39 40
{
	smp_send_stop();
	disabled_wait((unsigned long) __builtin_return_address(0));
	while (1);
}

Linus Torvalds's avatar
Linus Torvalds committed
41
/*
42 43
 * Main machine check handler function. Will be called with interrupts enabled
 * or disabled and machine checks enabled or disabled.
Linus Torvalds's avatar
Linus Torvalds committed
44
 */
45
void s390_handle_mcck(void)
Linus Torvalds's avatar
Linus Torvalds committed
46
{
47 48
	unsigned long flags;
	struct mcck_struct mcck;
Linus Torvalds's avatar
Linus Torvalds committed
49

50 51 52 53 54 55 56 57 58
	/*
	 * Disable machine checks and get the current state of accumulated
	 * machine checks. Afterwards delete the old state and enable machine
	 * checks again.
	 */
	local_irq_save(flags);
	local_mcck_disable();
	mcck = __get_cpu_var(cpu_mcck);
	memset(&__get_cpu_var(cpu_mcck), 0, sizeof(struct mcck_struct));
59
	clear_cpu_flag(CIF_MCCK_PENDING);
60 61
	local_mcck_enable();
	local_irq_restore(flags);
Linus Torvalds's avatar
Linus Torvalds committed
62

63
	if (mcck.channel_report)
64
		crw_handle_channel_report();
65 66 67 68 69 70 71 72 73
	/*
	 * A warning may remain for a prolonged period on the bare iron.
	 * (actually until the machine is powered off, or the problem is gone)
	 * So we just stop listening for the WARNING MCH and avoid continuously
	 * being interrupted.  One caveat is however, that we must do this per
	 * processor and cannot use the smp version of ctl_clear_bit().
	 * On VM we only get one interrupt per virtally presented machinecheck.
	 * Though one suffices, we may get one interrupt per (virtual) cpu.
	 */
74
	if (mcck.warning) {	/* WARNING pending ? */
Linus Torvalds's avatar
Linus Torvalds committed
75
		static int mchchk_wng_posted = 0;
76 77

		/* Use single cpu clear, as we cannot handle smp here. */
Linus Torvalds's avatar
Linus Torvalds committed
78 79
		__ctl_clear_bit(14, 24);	/* Disable WARNING MCH */
		if (xchg(&mchchk_wng_posted, 1) == 0)
80
			kill_cad_pid(SIGPWR, 1);
Linus Torvalds's avatar
Linus Torvalds committed
81
	}
82 83 84 85 86 87 88 89 90
	if (mcck.kill_task) {
		local_irq_enable();
		printk(KERN_EMERG "mcck: Terminating task because of machine "
		       "malfunction (code 0x%016llx).\n", mcck.mcck_code);
		printk(KERN_EMERG "mcck: task: %s, pid: %d.\n",
		       current->comm, current->pid);
		do_exit(SIGSEGV);
	}
}
91
EXPORT_SYMBOL_GPL(s390_handle_mcck);
92 93 94 95 96

/*
 * returns 0 if all registers could be validated
 * returns 1 otherwise
 */
97
static int notrace s390_revalidate_registers(struct mci *mci)
98 99 100 101 102 103 104
{
	int kill_task;
	u64 zero;
	void *fpt_save_area, *fpt_creg_save_area;

	kill_task = 0;
	zero = 0;
105 106

	if (!mci->gr) {
107 108 109 110 111
		/*
		 * General purpose registers couldn't be restored and have
		 * unknown contents. Process needs to be terminated.
		 */
		kill_task = 1;
112 113
	}
	if (!mci->fp) {
114 115 116 117 118
		/*
		 * Floating point registers can't be restored and
		 * therefore the process needs to be terminated.
		 */
		kill_task = 1;
119
	}
120
#ifndef CONFIG_64BIT
121 122 123 124 125 126
	asm volatile(
		"	ld	0,0(%0)\n"
		"	ld	2,8(%0)\n"
		"	ld	4,16(%0)\n"
		"	ld	6,24(%0)"
		: : "a" (&S390_lowcore.floating_pt_save_area));
127 128 129
#endif

	if (MACHINE_HAS_IEEE) {
130
#ifdef CONFIG_64BIT
131 132 133 134
		fpt_save_area = &S390_lowcore.floating_pt_save_area;
		fpt_creg_save_area = &S390_lowcore.fpt_creg_save_area;
#else
		fpt_save_area = (void *) S390_lowcore.extended_save_area_addr;
135
		fpt_creg_save_area = fpt_save_area + 128;
136 137 138 139 140 141
#endif
		if (!mci->fc) {
			/*
			 * Floating point control register can't be restored.
			 * Task will be terminated.
			 */
142
			asm volatile("lfpc 0(%0)" : : "a" (&zero), "m" (zero));
143 144
			kill_task = 1;

145 146
		} else
			asm volatile("lfpc 0(%0)" : : "a" (fpt_creg_save_area));
147

148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
		asm volatile(
			"	ld	0,0(%0)\n"
			"	ld	1,8(%0)\n"
			"	ld	2,16(%0)\n"
			"	ld	3,24(%0)\n"
			"	ld	4,32(%0)\n"
			"	ld	5,40(%0)\n"
			"	ld	6,48(%0)\n"
			"	ld	7,56(%0)\n"
			"	ld	8,64(%0)\n"
			"	ld	9,72(%0)\n"
			"	ld	10,80(%0)\n"
			"	ld	11,88(%0)\n"
			"	ld	12,96(%0)\n"
			"	ld	13,104(%0)\n"
			"	ld	14,112(%0)\n"
			"	ld	15,120(%0)\n"
			: : "a" (fpt_save_area));
166
	}
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181

#ifdef CONFIG_64BIT
	/* Revalidate vector registers */
	if (MACHINE_HAS_VX && current->thread.vxrs) {
		if (!mci->vr) {
			/*
			 * Vector registers can't be restored and therefore
			 * the process needs to be terminated.
			 */
			kill_task = 1;
		}
		restore_vx_regs((__vector128 *)
				S390_lowcore.vector_save_area_addr);
	}
#endif
182
	/* Revalidate access registers */
183 184 185
	asm volatile(
		"	lam	0,15,0(%0)"
		: : "a" (&S390_lowcore.access_regs_save_area));
186
	if (!mci->ar) {
187 188 189 190 191
		/*
		 * Access registers have unknown contents.
		 * Terminating task.
		 */
		kill_task = 1;
192
	}
193
	/* Revalidate control registers */
194
	if (!mci->cr) {
195 196 197 198 199
		/*
		 * Control registers have unknown contents.
		 * Can't recover and therefore stopping machine.
		 */
		s390_handle_damage("invalid control registers.");
200
	} else {
201
#ifdef CONFIG_64BIT
202 203 204
		asm volatile(
			"	lctlg	0,15,0(%0)"
			: : "a" (&S390_lowcore.cregs_save_area));
205
#else
206 207 208
		asm volatile(
			"	lctl	0,15,0(%0)"
			: : "a" (&S390_lowcore.cregs_save_area));
209
#endif
210
	}
211 212 213 214
	/*
	 * We don't even try to revalidate the TOD register, since we simply
	 * can't write something sensible into that register.
	 */
215
#ifdef CONFIG_64BIT
216 217 218 219 220
	/*
	 * See if we can revalidate the TOD programmable register with its
	 * old contents (should be zero) otherwise set it to zero.
	 */
	if (!mci->pr)
221 222 223 224
		asm volatile(
			"	sr	0,0\n"
			"	sckpf"
			: : : "0", "cc");
225 226
	else
		asm volatile(
227 228 229 230
			"	l	0,0(%0)\n"
			"	sckpf"
			: : "a" (&S390_lowcore.tod_progreg_save_area)
			: "0", "cc");
231 232
#endif
	/* Revalidate clock comparator register */
233
	set_clock_comparator(S390_lowcore.clock_comparator);
234 235 236 237 238 239 240 241 242 243 244 245 246 247
	/* Check if old PSW is valid */
	if (!mci->wp)
		/*
		 * Can't tell if we come from user or kernel mode
		 * -> stopping machine.
		 */
		s390_handle_damage("old psw invalid.");

	if (!mci->ms || !mci->pm || !mci->ia)
		kill_task = 1;

	return kill_task;
}

248
#define MAX_IPD_COUNT	29
249
#define MAX_IPD_TIME	(5 * 60 * USEC_PER_SEC) /* 5 minutes */
250

251 252 253 254 255
#define ED_STP_ISLAND	6	/* External damage STP island check */
#define ED_STP_SYNC	7	/* External damage STP sync check */
#define ED_ETR_SYNC	12	/* External damage ETR sync check */
#define ED_ETR_SWITCH	13	/* External damage ETR switch to local */

256 257 258
/*
 * machine check handler.
 */
259
void notrace s390_do_machine_check(struct pt_regs *regs)
260
{
261
	static int ipd_count;
262 263
	static DEFINE_SPINLOCK(ipd_lock);
	static unsigned long long last_ipd;
264
	struct mcck_struct *mcck;
265
	unsigned long long tmp;
266 267 268
	struct mci *mci;
	int umode;

269
	nmi_enter();
270
	inc_irq_stat(NMI_NMI);
271 272 273 274
	mci = (struct mci *) &S390_lowcore.mcck_interruption_code;
	mcck = &__get_cpu_var(cpu_mcck);
	umode = user_mode(regs);

275
	if (mci->sd) {
276 277
		/* System damage -> stopping machine */
		s390_handle_damage("received system damage machine check.");
278
	}
279 280 281 282
	if (mci->pd) {
		if (mci->b) {
			/* Processing backup -> verify if we can survive this */
			u64 z_mcic, o_mcic, t_mcic;
283
#ifdef CONFIG_64BIT
284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
			z_mcic = (1ULL<<63 | 1ULL<<59 | 1ULL<<29);
			o_mcic = (1ULL<<43 | 1ULL<<42 | 1ULL<<41 | 1ULL<<40 |
				  1ULL<<36 | 1ULL<<35 | 1ULL<<34 | 1ULL<<32 |
				  1ULL<<30 | 1ULL<<21 | 1ULL<<20 | 1ULL<<17 |
				  1ULL<<16);
#else
			z_mcic = (1ULL<<63 | 1ULL<<59 | 1ULL<<57 | 1ULL<<50 |
				  1ULL<<29);
			o_mcic = (1ULL<<43 | 1ULL<<42 | 1ULL<<41 | 1ULL<<40 |
				  1ULL<<36 | 1ULL<<35 | 1ULL<<34 | 1ULL<<32 |
				  1ULL<<30 | 1ULL<<20 | 1ULL<<17 | 1ULL<<16);
#endif
			t_mcic = *(u64 *)mci;

			if (((t_mcic & z_mcic) != 0) ||
			    ((t_mcic & o_mcic) != o_mcic)) {
				s390_handle_damage("processing backup machine "
						   "check with damage.");
			}
303 304 305 306 307 308

			/*
			 * Nullifying exigent condition, therefore we might
			 * retry this instruction.
			 */
			spin_lock(&ipd_lock);
309
			tmp = get_tod_clock();
310 311 312 313 314 315 316 317
			if (((tmp - last_ipd) >> 12) < MAX_IPD_TIME)
				ipd_count++;
			else
				ipd_count = 1;
			last_ipd = tmp;
			if (ipd_count == MAX_IPD_COUNT)
				s390_handle_damage("too many ipd retries.");
			spin_unlock(&ipd_lock);
318
		} else {
319 320 321 322 323 324 325 326 327 328 329 330 331
			/* Processing damage -> stopping machine */
			s390_handle_damage("received instruction processing "
					   "damage machine check.");
		}
	}
	if (s390_revalidate_registers(mci)) {
		if (umode) {
			/*
			 * Couldn't restore all register contents while in
			 * user mode -> mark task for termination.
			 */
			mcck->kill_task = 1;
			mcck->mcck_code = *(unsigned long long *) mci;
332
			set_cpu_flag(CIF_MCCK_PENDING);
333
		} else {
334 335 336 337 338
			/*
			 * Couldn't restore all register contents while in
			 * kernel mode -> stopping machine.
			 */
			s390_handle_damage("unable to revalidate registers.");
339
		}
340
	}
Martin Schwidefsky's avatar
Martin Schwidefsky committed
341 342 343 344 345 346 347 348 349 350
	if (mci->cd) {
		/* Timing facility damage */
		s390_handle_damage("TOD clock damaged");
	}
	if (mci->ed && mci->ec) {
		/* External damage */
		if (S390_lowcore.external_damage_code & (1U << ED_ETR_SYNC))
			etr_sync_check();
		if (S390_lowcore.external_damage_code & (1U << ED_ETR_SWITCH))
			etr_switch_to_local();
Martin Schwidefsky's avatar
Martin Schwidefsky committed
351 352 353 354
		if (S390_lowcore.external_damage_code & (1U << ED_STP_SYNC))
			stp_sync_check();
		if (S390_lowcore.external_damage_code & (1U << ED_STP_ISLAND))
			stp_island_check();
Martin Schwidefsky's avatar
Martin Schwidefsky committed
355
	}
356 357 358 359 360 361 362 363 364 365 366 367 368 369 370
	if (mci->se)
		/* Storage error uncorrected */
		s390_handle_damage("received storage error uncorrected "
				   "machine check.");
	if (mci->ke)
		/* Storage key-error uncorrected */
		s390_handle_damage("received storage key-error uncorrected "
				   "machine check.");
	if (mci->ds && mci->fa)
		/* Storage degradation */
		s390_handle_damage("received storage degradation machine "
				   "check.");
	if (mci->cp) {
		/* Channel report word pending */
		mcck->channel_report = 1;
371
		set_cpu_flag(CIF_MCCK_PENDING);
372 373 374 375
	}
	if (mci->w) {
		/* Warning pending */
		mcck->warning = 1;
376
		set_cpu_flag(CIF_MCCK_PENDING);
377
	}
378
	nmi_exit();
Linus Torvalds's avatar
Linus Torvalds committed
379 380
}

381
static int __init machine_check_init(void)
Linus Torvalds's avatar
Linus Torvalds committed
382
{
Martin Schwidefsky's avatar
Martin Schwidefsky committed
383
	ctl_set_bit(14, 25);	/* enable external damage MCH */
384
	ctl_set_bit(14, 27);	/* enable system recovery MCH */
Linus Torvalds's avatar
Linus Torvalds committed
385 386 387 388
	ctl_set_bit(14, 24);	/* enable warning MCH */
	return 0;
}
arch_initcall(machine_check_init);