xref: /kvm-unit-tests/lib/s390x/interrupt.c (revision fedfd11207221d778fb7b80923d11d649b98db45)
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3  * s390x interrupt handling
4  *
5  * Copyright (c) 2017 Red Hat Inc
6  *
7  * Authors:
8  *  David Hildenbrand <david@redhat.com>
9  */
10 #include <libcflat.h>
11 #include <asm/barrier.h>
12 #include <asm/mem.h>
13 #include <asm/asm-offsets.h>
14 #include <sclp.h>
15 #include <interrupt.h>
16 #include <sie.h>
17 #include <fault.h>
18 #include <asm/page.h>
19 #include "smp.h"
20 
21 /**
22  * expect_pgm_int - Expect a program interrupt on the current CPU.
23  */
24 void expect_pgm_int(void)
25 {
26 	THIS_CPU->pgm_int_expected = true;
27 	lowcore.pgm_int_code = 0;
28 	lowcore.trans_exc_id = 0;
29 	mb();
30 }
31 
32 /**
33  * expect_ext_int - Expect an external interrupt on the current CPU.
34  */
35 void expect_ext_int(void)
36 {
37 	THIS_CPU->ext_int_expected = true;
38 	lowcore.ext_int_code = 0;
39 	mb();
40 }
41 
42 /**
43  * clear_pgm_int - Clear program interrupt information
44  *
45  * Clear program interrupt information, including the expected program
46  * interrupt flag.
47  * No program interrupts are expected after calling this function.
48  *
49  * Return: the program interrupt code before clearing
50  */
51 uint16_t clear_pgm_int(void)
52 {
53 	uint16_t code;
54 
55 	mb();
56 	code = lowcore.pgm_int_code;
57 	lowcore.pgm_int_code = 0;
58 	lowcore.trans_exc_id = 0;
59 	THIS_CPU->pgm_int_expected = false;
60 	return code;
61 }
62 
63 /**
64  * check_pgm_int_code - Check the program interrupt code on the current CPU.
65  * @code the expected program interrupt code on the current CPU
66  *
67  * Check and report if the program interrupt on the current CPU matches the
68  * expected one.
69  */
70 void check_pgm_int_code(uint16_t code)
71 {
72 	mb();
73 	report(code == lowcore.pgm_int_code,
74 	       "Program interrupt: expected(%d) == received(%d)", code,
75 	       lowcore.pgm_int_code);
76 }
77 
78 /**
79  * register_pgm_cleanup_func - Register a cleanup function for progam
80  * interrupts for the current CPU.
81  * @f the cleanup function to be registered on the current CPU
82  *
83  * Register a cleanup function to be called at the end of the normal
84  * interrupt handling for program interrupts for this CPU.
85  *
86  * Pass NULL to unregister a previously registered cleanup function.
87  */
88 void register_pgm_cleanup_func(void (*f)(struct stack_frame_int *))
89 {
90 	THIS_CPU->pgm_cleanup_func = f;
91 }
92 
93 /**
94  * register_ext_cleanup_func - Register a cleanup function for external
95  * interrupts for the current CPU.
96  * @f the cleanup function to be registered on the current CPU
97  *
98  * Register a cleanup function to be called at the end of the normal
99  * interrupt handling for external interrupts for this CPU.
100  *
101  * Pass NULL to unregister a previously registered cleanup function.
102  */
103 void register_ext_cleanup_func(void (*f)(struct stack_frame_int *))
104 {
105 	THIS_CPU->ext_cleanup_func = f;
106 }
107 
108 /**
109  * irq_set_dat_mode - Set the DAT mode of all interrupt handlers, except for
110  * restart.
111  * @use_dat: specifies whether to use DAT or not
112  * @as: specifies the address space mode to use. Not set if use_dat is false.
113  *
114  * This will update the DAT mode and address space mode of all interrupt new
115  * PSWs.
116  *
117  * Since enabling DAT needs initialized CRs and the restart new PSW is often used
118  * to initialize CRs, the restart new PSW is never touched to avoid the chicken
119  * and egg situation.
120  */
121 void irq_set_dat_mode(bool use_dat, enum address_space as)
122 {
123 	struct psw* irq_psws[] = {
124 		OPAQUE_PTR(GEN_LC_EXT_NEW_PSW),
125 		OPAQUE_PTR(GEN_LC_SVC_NEW_PSW),
126 		OPAQUE_PTR(GEN_LC_PGM_NEW_PSW),
127 		OPAQUE_PTR(GEN_LC_MCCK_NEW_PSW),
128 		OPAQUE_PTR(GEN_LC_IO_NEW_PSW),
129 	};
130 	struct psw *psw;
131 
132 	assert(as == AS_PRIM || as == AS_ACCR || as == AS_SECN || as == AS_HOME);
133 
134 	for (size_t i = 0; i < ARRAY_SIZE(irq_psws); i++) {
135 		psw = irq_psws[i];
136 		psw->dat = use_dat;
137 		if (use_dat)
138 			psw->as = as;
139 	}
140 }
141 
142 static void fixup_pgm_int(struct stack_frame_int *stack)
143 {
144 	/* If we have an error on SIE we directly move to sie_exit */
145 	if (lowcore.pgm_old_psw.addr >= (uint64_t)&sie_entry &&
146 	    lowcore.pgm_old_psw.addr <= (uint64_t)&sie_exit) {
147 		lowcore.pgm_old_psw.addr = (uint64_t)&sie_exit;
148 	}
149 
150 	switch (lowcore.pgm_int_code) {
151 	case PGM_INT_CODE_PRIVILEGED_OPERATION:
152 		/* Normal operation is in supervisor state, so this exception
153 		 * was produced intentionally and we should return to the
154 		 * supervisor state.
155 		 */
156 		lowcore.pgm_old_psw.mask &= ~PSW_MASK_PSTATE;
157 		break;
158 	case PGM_INT_CODE_PROTECTION:
159 		/* Handling for iep.c test case. */
160 		if (prot_is_iep((union teid) { .val = lowcore.trans_exc_id }))
161 			/*
162 			 * We branched to the instruction that caused
163 			 * the exception so we can use the return
164 			 * address in GR14 to jump back and continue
165 			 * executing test code.
166 			 */
167 			lowcore.pgm_old_psw.addr = stack->grs0[12];
168 		break;
169 	case PGM_INT_CODE_SEGMENT_TRANSLATION:
170 	case PGM_INT_CODE_PAGE_TRANSLATION:
171 	case PGM_INT_CODE_TRACE_TABLE:
172 	case PGM_INT_CODE_AFX_TRANSLATION:
173 	case PGM_INT_CODE_ASX_TRANSLATION:
174 	case PGM_INT_CODE_LX_TRANSLATION:
175 	case PGM_INT_CODE_EX_TRANSLATION:
176 	case PGM_INT_CODE_PRIMARY_AUTHORITY:
177 	case PGM_INT_CODE_SECONDARY_AUTHORITY:
178 	case PGM_INT_CODE_LFX_TRANSLATION:
179 	case PGM_INT_CODE_LSX_TRANSLATION:
180 	case PGM_INT_CODE_ALEN_TRANSLATION:
181 	case PGM_INT_CODE_ALE_SEQUENCE:
182 	case PGM_INT_CODE_ASTE_VALIDITY:
183 	case PGM_INT_CODE_ASTE_SEQUENCE:
184 	case PGM_INT_CODE_EXTENDED_AUTHORITY:
185 	case PGM_INT_CODE_LSTE_SEQUENCE:
186 	case PGM_INT_CODE_ASTE_INSTANCE:
187 	case PGM_INT_CODE_STACK_FULL:
188 	case PGM_INT_CODE_STACK_EMPTY:
189 	case PGM_INT_CODE_STACK_SPECIFICATION:
190 	case PGM_INT_CODE_STACK_TYPE:
191 	case PGM_INT_CODE_STACK_OPERATION:
192 	case PGM_INT_CODE_ASCE_TYPE:
193 	case PGM_INT_CODE_REGION_FIRST_TRANS:
194 	case PGM_INT_CODE_REGION_SECOND_TRANS:
195 	case PGM_INT_CODE_REGION_THIRD_TRANS:
196 	case PGM_INT_CODE_PER:
197 	case PGM_INT_CODE_CRYPTO_OPERATION:
198 	case PGM_INT_CODE_SECURE_STOR_ACCESS:
199 	case PGM_INT_CODE_NON_SECURE_STOR_ACCESS:
200 	case PGM_INT_CODE_SECURE_STOR_VIOLATION:
201 		/* The interrupt was nullified, the old PSW points at the
202 		 * responsible instruction. Forward the PSW so we don't loop.
203 		 */
204 		lowcore.pgm_old_psw.addr += lowcore.pgm_int_id;
205 	}
206 	/* suppressed/terminated/completed point already at the next address */
207 }
208 
209 static void print_storage_exception_information(void)
210 {
211 	switch (lowcore.pgm_int_code) {
212 	case PGM_INT_CODE_PROTECTION:
213 	case PGM_INT_CODE_PAGE_TRANSLATION:
214 	case PGM_INT_CODE_SEGMENT_TRANSLATION:
215 	case PGM_INT_CODE_ASCE_TYPE:
216 	case PGM_INT_CODE_REGION_FIRST_TRANS:
217 	case PGM_INT_CODE_REGION_SECOND_TRANS:
218 	case PGM_INT_CODE_REGION_THIRD_TRANS:
219 	case PGM_INT_CODE_SECURE_STOR_ACCESS:
220 	case PGM_INT_CODE_NON_SECURE_STOR_ACCESS:
221 	case PGM_INT_CODE_SECURE_STOR_VIOLATION:
222 		print_decode_teid(lowcore.trans_exc_id);
223 		break;
224 	}
225 }
226 
227 static void print_int_regs(struct stack_frame_int *stack, bool sie)
228 {
229 	struct kvm_s390_sie_block *sblk;
230 
231 	printf("\n");
232 	printf("%s\n", sie ? "Guest registers:" : "Host registers:");
233 	printf("GPRS:\n");
234 	printf("%016lx %016lx %016lx %016lx\n",
235 	       stack->grs1[0], stack->grs1[1], stack->grs0[0], stack->grs0[1]);
236 	printf("%016lx %016lx %016lx %016lx\n",
237 	       stack->grs0[2], stack->grs0[3], stack->grs0[4], stack->grs0[5]);
238 	printf("%016lx %016lx %016lx %016lx\n",
239 	       stack->grs0[6], stack->grs0[7], stack->grs0[8], stack->grs0[9]);
240 
241 	if (sie) {
242 		sblk = (struct kvm_s390_sie_block *)stack->grs0[12];
243 		printf("%016lx %016lx %016lx %016lx\n",
244 		       stack->grs0[10], stack->grs0[11], sblk->gg14, sblk->gg15);
245 	} else {
246 		printf("%016lx %016lx %016lx %016lx\n",
247 		       stack->grs0[10], stack->grs0[11], stack->grs0[12], stack->grs0[13]);
248 	}
249 
250 	printf("\n");
251 }
252 
253 static void print_pgm_info(struct stack_frame_int *stack)
254 
255 {
256 	bool in_sie, in_sie_gregs;
257 	struct vm_save_area *vregs;
258 
259 	in_sie = (lowcore.pgm_old_psw.addr >= (uintptr_t)sie_entry &&
260 		  lowcore.pgm_old_psw.addr <= (uintptr_t)sie_exit);
261 	in_sie_gregs = (lowcore.pgm_old_psw.addr >= (uintptr_t)sie_entry_gregs &&
262 			lowcore.pgm_old_psw.addr < (uintptr_t)sie_exit_gregs);
263 
264 	printf("\n");
265 	printf("Unexpected program interrupt %s: %#x on cpu %d at %#lx, ilen %d\n",
266 	       in_sie ? "in SIE" : "",
267 	       lowcore.pgm_int_code, stap(), lowcore.pgm_old_psw.addr, lowcore.pgm_int_id);
268 
269 	/*
270 	 * If we fall out of SIE before loading the host registers,
271 	 * then we need to do it here so we print the host registers
272 	 * and not the guest registers.
273 	 *
274 	 * Back tracing is actually not a problem since SIE restores gr15.
275 	 */
276 	if (in_sie_gregs) {
277 		print_int_regs(stack, true);
278 		vregs = *((struct vm_save_area **)(stack->grs0[13] + __SF_SIE_SAVEAREA));
279 
280 		/*
281 		 * The grs are not linear on the interrupt stack frame.
282 		 * We copy 0 and 1 here and 2 - 15 with the memcopy below.
283 		 */
284 		stack->grs1[0] = vregs->host.grs[0];
285 		stack->grs1[1] = vregs->host.grs[1];
286 		/*  2 - 15 */
287 		memcpy(stack->grs0, &vregs->host.grs[2], sizeof(stack->grs0) - 8);
288 	}
289 	print_int_regs(stack, false);
290 	dump_stack();
291 
292 	/* Dump stack doesn't end with a \n so we add it here instead */
293 	printf("\n");
294 	print_storage_exception_information();
295 	report_summary();
296 	abort();
297 }
298 
299 void handle_pgm_int(struct stack_frame_int *stack)
300 {
301 	if (THIS_CPU->in_interrupt_handler) {
302 		/* Something went very wrong, stop everything now without printing anything */
303 		smp_teardown();
304 		disabled_wait(0xfa12edbad21);
305 	}
306 	if (!THIS_CPU->pgm_int_expected) {
307 		/* Force sclp_busy to false, otherwise we will loop forever */
308 		sclp_handle_ext();
309 		print_pgm_info(stack);
310 	}
311 
312 	THIS_CPU->pgm_int_expected = false;
313 	THIS_CPU->in_interrupt_handler = true;
314 
315 	if (THIS_CPU->pgm_cleanup_func)
316 		THIS_CPU->pgm_cleanup_func(stack);
317 	else
318 		fixup_pgm_int(stack);
319 	THIS_CPU->in_interrupt_handler = false;
320 }
321 
322 void handle_ext_int(struct stack_frame_int *stack)
323 {
324 	THIS_CPU->in_interrupt_handler = true;
325 	if (!THIS_CPU->ext_int_expected && lowcore.ext_int_code != EXT_IRQ_SERVICE_SIG) {
326 		report_abort("Unexpected external call interrupt (code %#x): on cpu %d at %#lx",
327 			     lowcore.ext_int_code, stap(), lowcore.ext_old_psw.addr);
328 		return;
329 	}
330 
331 	if (lowcore.ext_int_code == EXT_IRQ_SERVICE_SIG) {
332 		stack->crs[0] &= ~(1UL << 9);
333 		sclp_handle_ext();
334 	} else {
335 		THIS_CPU->ext_int_expected = false;
336 	}
337 
338 	if (!(stack->crs[0] & CR0_EXTM_MASK))
339 		lowcore.ext_old_psw.mask &= ~PSW_MASK_EXT;
340 
341 	if (THIS_CPU->ext_cleanup_func)
342 		THIS_CPU->ext_cleanup_func(stack);
343 	THIS_CPU->in_interrupt_handler = false;
344 }
345 
346 void handle_mcck_int(void)
347 {
348 	report_abort("Unexpected machine check interrupt: on cpu %d at %#lx",
349 		     stap(), lowcore.mcck_old_psw.addr);
350 }
351 
352 static void (*io_int_func)(void);
353 
354 void handle_io_int(void)
355 {
356 	THIS_CPU->in_interrupt_handler = true;
357 	if (io_int_func)
358 		io_int_func();
359 	else
360 		report_abort("Unexpected io interrupt: on cpu %d at %#lx",
361 			     stap(), lowcore.io_old_psw.addr);
362 	THIS_CPU->in_interrupt_handler = false;
363 }
364 
365 int register_io_int_func(void (*f)(void))
366 {
367 	if (io_int_func)
368 		return -1;
369 	io_int_func = f;
370 	return 0;
371 }
372 
373 int unregister_io_int_func(void (*f)(void))
374 {
375 	if (io_int_func != f)
376 		return -1;
377 	io_int_func = NULL;
378 	return 0;
379 }
380 
381 void handle_svc_int(void)
382 {
383 	uint16_t code = lowcore.svc_int_code;
384 
385 	switch (code) {
386 	case SVC_LEAVE_PSTATE:
387 		lowcore.svc_old_psw.mask &= ~PSW_MASK_PSTATE;
388 		break;
389 	default:
390 		report_abort("Unexpected supervisor call interrupt: code %#x on cpu %d at %#lx",
391 			      code, stap(), lowcore.svc_old_psw.addr);
392 	}
393 }
394