xref: /kvm-unit-tests/lib/s390x/interrupt.c (revision 6163f75d09a0a96a5c3db82dd768b13f79629c00)
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 #include <sclp.h>
16 
17 static bool pgm_int_expected;
18 static bool ext_int_expected;
19 static struct lowcore *lc;
20 
21 void expect_pgm_int(void)
22 {
23 	pgm_int_expected = true;
24 	lc->pgm_int_code = 0;
25 	mb();
26 }
27 
28 void expect_ext_int(void)
29 {
30 	ext_int_expected = true;
31 	lc->ext_int_code = 0;
32 	mb();
33 }
34 
35 uint16_t clear_pgm_int(void)
36 {
37 	uint16_t code;
38 
39 	mb();
40 	code = lc->pgm_int_code;
41 	lc->pgm_int_code = 0;
42 	pgm_int_expected = false;
43 	return code;
44 }
45 
46 void check_pgm_int_code(uint16_t code)
47 {
48 	mb();
49 	report("Program interrupt: expected(%d) == received(%d)",
50 	       code == lc->pgm_int_code, code, lc->pgm_int_code);
51 }
52 
53 static void fixup_pgm_int(void)
54 {
55 	switch (lc->pgm_int_code) {
56 	case PGM_INT_CODE_PRIVILEGED_OPERATION:
57 		/* Normal operation is in supervisor state, so this exception
58 		 * was produced intentionally and we should return to the
59 		 * supervisor state.
60 		 */
61 		lc->pgm_old_psw.mask &= ~PSW_MASK_PSTATE;
62 		break;
63 	case PGM_INT_CODE_PROTECTION:
64 		/* Handling for iep.c test case. */
65 		if (lc->trans_exc_id & 0x80UL && lc->trans_exc_id & 0x04UL &&
66 		    !(lc->trans_exc_id & 0x08UL))
67 			lc->pgm_old_psw.addr = lc->sw_int_grs[14];
68 		break;
69 	case PGM_INT_CODE_SEGMENT_TRANSLATION:
70 	case PGM_INT_CODE_PAGE_TRANSLATION:
71 	case PGM_INT_CODE_TRACE_TABLE:
72 	case PGM_INT_CODE_AFX_TRANSLATION:
73 	case PGM_INT_CODE_ASX_TRANSLATION:
74 	case PGM_INT_CODE_LX_TRANSLATION:
75 	case PGM_INT_CODE_EX_TRANSLATION:
76 	case PGM_INT_CODE_PRIMARY_AUTHORITY:
77 	case PGM_INT_CODE_SECONDARY_AUTHORITY:
78 	case PGM_INT_CODE_LFX_TRANSLATION:
79 	case PGM_INT_CODE_LSX_TRANSLATION:
80 	case PGM_INT_CODE_ALEN_TRANSLATION:
81 	case PGM_INT_CODE_ALE_SEQUENCE:
82 	case PGM_INT_CODE_ASTE_VALIDITY:
83 	case PGM_INT_CODE_ASTE_SEQUENCE:
84 	case PGM_INT_CODE_EXTENDED_AUTHORITY:
85 	case PGM_INT_CODE_LSTE_SEQUENCE:
86 	case PGM_INT_CODE_ASTE_INSTANCE:
87 	case PGM_INT_CODE_STACK_FULL:
88 	case PGM_INT_CODE_STACK_EMPTY:
89 	case PGM_INT_CODE_STACK_SPECIFICATION:
90 	case PGM_INT_CODE_STACK_TYPE:
91 	case PGM_INT_CODE_STACK_OPERATION:
92 	case PGM_INT_CODE_ASCE_TYPE:
93 	case PGM_INT_CODE_REGION_FIRST_TRANS:
94 	case PGM_INT_CODE_REGION_SECOND_TRANS:
95 	case PGM_INT_CODE_REGION_THIRD_TRANS:
96 	case PGM_INT_CODE_PER:
97 	case PGM_INT_CODE_CRYPTO_OPERATION:
98 		/* The interrupt was nullified, the old PSW points at the
99 		 * responsible instruction. Forward the PSW so we don't loop.
100 		 */
101 		lc->pgm_old_psw.addr += lc->pgm_int_id;
102 	}
103 	/* suppressed/terminated/completed point already at the next address */
104 }
105 
106 void handle_pgm_int(void)
107 {
108 	if (!pgm_int_expected)
109 		report_abort("Unexpected program interrupt: %d at %#lx, ilen %d\n",
110 			     lc->pgm_int_code, lc->pgm_old_psw.addr,
111 			     lc->pgm_int_id);
112 
113 	pgm_int_expected = false;
114 	fixup_pgm_int();
115 }
116 
117 void handle_ext_int(void)
118 {
119 	if (!ext_int_expected &&
120 	    lc->ext_int_code != EXT_IRQ_SERVICE_SIG) {
121 		report_abort("Unexpected external call interrupt (code %#x): at %#lx",
122 			     lc->ext_int_code, lc->ext_old_psw.addr);
123 		return;
124 	}
125 
126 	if (lc->ext_int_code == EXT_IRQ_SERVICE_SIG) {
127 		lc->sw_int_crs[0] &= ~(1UL << 9);
128 		sclp_handle_ext();
129 	} else {
130 		ext_int_expected = false;
131 	}
132 
133 	if (!(lc->sw_int_crs[0] & CR0_EXTM_MASK))
134 		lc->ext_old_psw.mask &= ~PSW_MASK_EXT;
135 }
136 
137 void handle_mcck_int(void)
138 {
139 	report_abort("Unexpected machine check interrupt: at %#lx",
140 		     lc->mcck_old_psw.addr);
141 }
142 
143 void handle_io_int(void)
144 {
145 	report_abort("Unexpected io interrupt: at %#lx",
146 		     lc->io_old_psw.addr);
147 }
148 
149 void handle_svc_int(void)
150 {
151 	report_abort("Unexpected supervisor call interrupt: at %#lx",
152 		     lc->svc_old_psw.addr);
153 }
154