xref: /kvm-unit-tests/lib/s390x/interrupt.c (revision 2352e986e4599bc4842c225762c78fa49f18648d)
1 /*
2  * s390x interrupt handling
3  *
4  * Copyright (c) 2017 Red Hat Inc
5  *
6  * Authors:
7  *  David Hildenbrand <david@redhat.com>
8  *
9  * This code is free software; you can redistribute it and/or modify it
10  * under the terms of the GNU Library General Public License version 2.
11  */
12 #include <libcflat.h>
13 #include <asm/interrupt.h>
14 #include <asm/barrier.h>
15 
16 static bool pgm_int_expected;
17 static struct lowcore *lc;
18 
19 void expect_pgm_int(void)
20 {
21 	pgm_int_expected = true;
22 	lc->pgm_int_code = 0;
23 	mb();
24 }
25 
26 uint16_t clear_pgm_int(void)
27 {
28 	uint16_t code;
29 
30 	mb();
31 	code = lc->pgm_int_code;
32 	lc->pgm_int_code = 0;
33 	pgm_int_expected = false;
34 	return code;
35 }
36 
37 void check_pgm_int_code(uint16_t code)
38 {
39 	mb();
40 	report("Program interrupt: expected(%d) == received(%d)",
41 	       code == lc->pgm_int_code, code, lc->pgm_int_code);
42 }
43 
44 static void fixup_pgm_int(void)
45 {
46 	switch (lc->pgm_int_code) {
47 	case PGM_INT_CODE_PRIVILEGED_OPERATION:
48 		/* Normal operation is in supervisor state, so this exception
49 		 * was produced intentionally and we should return to the
50 		 * supervisor state.
51 		 */
52 		lc->pgm_old_psw.mask &= ~PSW_MASK_PSTATE;
53 		break;
54 	case PGM_INT_CODE_SEGMENT_TRANSLATION:
55 	case PGM_INT_CODE_PAGE_TRANSLATION:
56 	case PGM_INT_CODE_TRACE_TABLE:
57 	case PGM_INT_CODE_AFX_TRANSLATION:
58 	case PGM_INT_CODE_ASX_TRANSLATION:
59 	case PGM_INT_CODE_LX_TRANSLATION:
60 	case PGM_INT_CODE_EX_TRANSLATION:
61 	case PGM_INT_CODE_PRIMARY_AUTHORITY:
62 	case PGM_INT_CODE_SECONDARY_AUTHORITY:
63 	case PGM_INT_CODE_LFX_TRANSLATION:
64 	case PGM_INT_CODE_LSX_TRANSLATION:
65 	case PGM_INT_CODE_ALEN_TRANSLATION:
66 	case PGM_INT_CODE_ALE_SEQUENCE:
67 	case PGM_INT_CODE_ASTE_VALIDITY:
68 	case PGM_INT_CODE_ASTE_SEQUENCE:
69 	case PGM_INT_CODE_EXTENDED_AUTHORITY:
70 	case PGM_INT_CODE_LSTE_SEQUENCE:
71 	case PGM_INT_CODE_ASTE_INSTANCE:
72 	case PGM_INT_CODE_STACK_FULL:
73 	case PGM_INT_CODE_STACK_EMPTY:
74 	case PGM_INT_CODE_STACK_SPECIFICATION:
75 	case PGM_INT_CODE_STACK_TYPE:
76 	case PGM_INT_CODE_STACK_OPERATION:
77 	case PGM_INT_CODE_ASCE_TYPE:
78 	case PGM_INT_CODE_REGION_FIRST_TRANS:
79 	case PGM_INT_CODE_REGION_SECOND_TRANS:
80 	case PGM_INT_CODE_REGION_THIRD_TRANS:
81 	case PGM_INT_CODE_PER:
82 	case PGM_INT_CODE_CRYPTO_OPERATION:
83 		/* The interrupt was nullified, the old PSW points at the
84 		 * responsible instruction. Forward the PSW so we don't loop.
85 		 */
86 		lc->pgm_old_psw.addr += lc->pgm_int_id;
87 	}
88 	/* suppressed/terminated/completed point already at the next address */
89 }
90 
91 void handle_pgm_int(void)
92 {
93 	if (!pgm_int_expected)
94 		report_abort("Unexpected program interrupt: %d at %#lx, ilen %d\n",
95 			     lc->pgm_int_code, lc->pgm_old_psw.addr,
96 			     lc->pgm_int_id);
97 
98 	pgm_int_expected = false;
99 	fixup_pgm_int();
100 }
101