xref: /kvm-unit-tests/lib/s390x/asm/arch_def.h (revision 5b70cbdb7bc2ea65096b51565c75815cc95945b8)
1 /*
2  * Copyright (c) 2017 Red Hat Inc
3  *
4  * Authors:
5  *  David Hildenbrand <david@redhat.com>
6  *
7  * This code is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU Library General Public License version 2.
9  */
10 #ifndef _ASM_S390X_ARCH_DEF_H_
11 #define _ASM_S390X_ARCH_DEF_H_
12 
13 struct psw {
14 	uint64_t	mask;
15 	uint64_t	addr;
16 };
17 
18 #define PSW_MASK_EXT			0x0100000000000000UL
19 #define PSW_MASK_DAT			0x0400000000000000UL
20 #define PSW_MASK_PSTATE			0x0001000000000000UL
21 
22 #define CR0_EXTM_SCLP			0x0000000000000200UL
23 #define CR0_EXTM_EXTC			0x0000000000002000UL
24 #define CR0_EXTM_EMGC			0x0000000000004000UL
25 #define CR0_EXTM_MASK			0x0000000000006200UL
26 
27 struct lowcore {
28 	uint8_t		pad_0x0000[0x0080 - 0x0000];	/* 0x0000 */
29 	uint32_t	ext_int_param;			/* 0x0080 */
30 	uint16_t	cpu_addr;			/* 0x0084 */
31 	uint16_t	ext_int_code;			/* 0x0086 */
32 	uint16_t	svc_int_id;			/* 0x0088 */
33 	uint16_t	svc_int_code;			/* 0x008a */
34 	uint16_t	pgm_int_id;			/* 0x008c */
35 	uint16_t	pgm_int_code;			/* 0x008e */
36 	uint32_t	dxc_vxc;			/* 0x0090 */
37 	uint16_t	mon_class_nb;			/* 0x0094 */
38 	uint8_t		per_code;			/* 0x0096 */
39 	uint8_t		per_atmid;			/* 0x0097 */
40 	uint64_t	per_addr;			/* 0x0098 */
41 	uint8_t		exc_acc_id;			/* 0x00a0 */
42 	uint8_t		per_acc_id;			/* 0x00a1 */
43 	uint8_t		op_acc_id;			/* 0x00a2 */
44 	uint8_t		arch_mode_id;			/* 0x00a3 */
45 	uint8_t		pad_0x00a4[0x00a8 - 0x00a4];	/* 0x00a4 */
46 	uint64_t	trans_exc_id;			/* 0x00a8 */
47 	uint64_t	mon_code;			/* 0x00b0 */
48 	uint32_t	subsys_id_word;			/* 0x00b8 */
49 	uint32_t	io_int_param;			/* 0x00bc */
50 	uint32_t	io_int_word;			/* 0x00c0 */
51 	uint8_t		pad_0x00c4[0x00c8 - 0x00c4];	/* 0x00c4 */
52 	uint32_t	stfl;				/* 0x00c8 */
53 	uint8_t		pad_0x00cc[0x00e8 - 0x00cc];	/* 0x00cc */
54 	uint64_t	mcck_int_code;			/* 0x00e8 */
55 	uint8_t		pad_0x00f0[0x00f4 - 0x00f0];	/* 0x00f0 */
56 	uint32_t	ext_damage_code;		/* 0x00f4 */
57 	uint64_t	failing_storage_addr;		/* 0x00f8 */
58 	uint64_t	emon_ca_origin;			/* 0x0100 */
59 	uint32_t	emon_ca_size;			/* 0x0108 */
60 	uint32_t	emon_exc_count;			/* 0x010c */
61 	uint64_t	breaking_event_addr;		/* 0x0110 */
62 	uint8_t		pad_0x0118[0x0120 - 0x0118];	/* 0x0118 */
63 	struct psw	restart_old_psw;		/* 0x0120 */
64 	struct psw	ext_old_psw;			/* 0x0130 */
65 	struct psw	svc_old_psw;			/* 0x0140 */
66 	struct psw	pgm_old_psw;			/* 0x0150 */
67 	struct psw	mcck_old_psw;			/* 0x0160 */
68 	struct psw	io_old_psw;			/* 0x0170 */
69 	uint8_t		pad_0x0180[0x01a0 - 0x0180];	/* 0x0180 */
70 	struct psw	restart_new_psw;		/* 0x01a0 */
71 	struct psw	ext_new_psw;			/* 0x01b0 */
72 	struct psw	svc_new_psw;			/* 0x01c0 */
73 	struct psw	pgm_new_psw;			/* 0x01d0 */
74 	struct psw	mcck_new_psw;			/* 0x01e0 */
75 	struct psw	io_new_psw;			/* 0x01f0 */
76 	/* sw definition: save area for registers in interrupt handlers */
77 	uint64_t	sw_int_grs[16];			/* 0x0200 */
78 	uint64_t	sw_int_fprs[16];		/* 0x0280 */
79 	uint32_t	sw_int_fpc;			/* 0x0300 */
80 	uint8_t		pad_0x0304[0x0308 - 0x0304];	/* 0x0304 */
81 	uint64_t	sw_int_crs[16];			/* 0x0308 */
82 	struct psw	sw_int_psw;			/* 0x0388 */
83 	uint8_t		pad_0x0310[0x11b0 - 0x0398];	/* 0x0398 */
84 	uint64_t	mcck_ext_sa_addr;		/* 0x11b0 */
85 	uint8_t		pad_0x11b8[0x1200 - 0x11b8];	/* 0x11b8 */
86 	uint64_t	fprs_sa[16];			/* 0x1200 */
87 	uint64_t	grs_sa[16];			/* 0x1280 */
88 	struct psw	psw_sa;				/* 0x1300 */
89 	uint8_t		pad_0x1310[0x1318 - 0x1310];	/* 0x1310 */
90 	uint32_t	prefix_sa;			/* 0x1318 */
91 	uint32_t	fpc_sa;				/* 0x131c */
92 	uint8_t		pad_0x1320[0x1324 - 0x1320];	/* 0x1320 */
93 	uint32_t	tod_pr_sa;			/* 0x1324 */
94 	uint64_t	cputm_sa;			/* 0x1328 */
95 	uint64_t	cc_sa;				/* 0x1330 */
96 	uint8_t		pad_0x1338[0x1340 - 0x1338];	/* 0x1338 */
97 	uint32_t	ars_sa[16];			/* 0x1340 */
98 	uint64_t	crs_sa[16];			/* 0x1380 */
99 	uint8_t		pad_0x1400[0x1800 - 0x1400];	/* 0x1400 */
100 	uint8_t		pgm_int_tdb[0x1900 - 0x1800];	/* 0x1800 */
101 } __attribute__ ((__packed__));
102 _Static_assert(sizeof(struct lowcore) == 0x1900, "Lowcore size");
103 
104 #define PGM_INT_CODE_OPERATION			0x01
105 #define PGM_INT_CODE_PRIVILEGED_OPERATION	0x02
106 #define PGM_INT_CODE_EXECUTE			0x03
107 #define PGM_INT_CODE_PROTECTION			0x04
108 #define PGM_INT_CODE_ADDRESSING			0x05
109 #define PGM_INT_CODE_SPECIFICATION		0x06
110 #define PGM_INT_CODE_DATA			0x07
111 #define PGM_INT_CODE_FIXED_POINT_OVERFLOW	0x08
112 #define PGM_INT_CODE_FIXED_POINT_DIVIDE		0x09
113 #define PGM_INT_CODE_DECIMAL_OVERFLOW		0x0a
114 #define PGM_INT_CODE_DECIMAL_DIVIDE		0x0b
115 #define PGM_INT_CODE_HFP_EXPONENT_OVERFLOW	0x0c
116 #define PGM_INT_CODE_HFP_EXPONENT_UNDERFLOW	0x0d
117 #define PGM_INT_CODE_HFP_SIGNIFICANCE		0x0e
118 #define PGM_INT_CODE_HFP_DIVIDE			0x0f
119 #define PGM_INT_CODE_SEGMENT_TRANSLATION	0x10
120 #define PGM_INT_CODE_PAGE_TRANSLATION		0x11
121 #define PGM_INT_CODE_TRANSLATION_SPEC		0x12
122 #define PGM_INT_CODE_SPECIAL_OPERATION		0x13
123 #define PGM_INT_CODE_OPERAND			0x15
124 #define PGM_INT_CODE_TRACE_TABLE		0x16
125 #define PGM_INT_CODE_VECTOR_PROCESSING		0x1b
126 #define PGM_INT_CODE_SPACE_SWITCH_EVENT		0x1c
127 #define PGM_INT_CODE_HFP_SQUARE_ROOT		0x1d
128 #define PGM_INT_CODE_PC_TRANSLATION_SPEC	0x1f
129 #define PGM_INT_CODE_AFX_TRANSLATION		0x20
130 #define PGM_INT_CODE_ASX_TRANSLATION		0x21
131 #define PGM_INT_CODE_LX_TRANSLATION		0x22
132 #define PGM_INT_CODE_EX_TRANSLATION		0x23
133 #define PGM_INT_CODE_PRIMARY_AUTHORITY		0x24
134 #define PGM_INT_CODE_SECONDARY_AUTHORITY	0x25
135 #define PGM_INT_CODE_LFX_TRANSLATION		0x26
136 #define PGM_INT_CODE_LSX_TRANSLATION		0x27
137 #define PGM_INT_CODE_ALET_SPECIFICATION		0x28
138 #define PGM_INT_CODE_ALEN_TRANSLATION		0x29
139 #define PGM_INT_CODE_ALE_SEQUENCE		0x2a
140 #define PGM_INT_CODE_ASTE_VALIDITY		0x2b
141 #define PGM_INT_CODE_ASTE_SEQUENCE		0x2c
142 #define PGM_INT_CODE_EXTENDED_AUTHORITY		0x2d
143 #define PGM_INT_CODE_LSTE_SEQUENCE		0x2e
144 #define PGM_INT_CODE_ASTE_INSTANCE		0x2f
145 #define PGM_INT_CODE_STACK_FULL			0x30
146 #define PGM_INT_CODE_STACK_EMPTY		0x31
147 #define PGM_INT_CODE_STACK_SPECIFICATION	0x32
148 #define PGM_INT_CODE_STACK_TYPE			0x33
149 #define PGM_INT_CODE_STACK_OPERATION		0x34
150 #define PGM_INT_CODE_ASCE_TYPE			0x38
151 #define PGM_INT_CODE_REGION_FIRST_TRANS		0x39
152 #define PGM_INT_CODE_REGION_SECOND_TRANS	0x3a
153 #define PGM_INT_CODE_REGION_THIRD_TRANS		0x3b
154 #define PGM_INT_CODE_MONITOR_EVENT		0x40
155 #define PGM_INT_CODE_PER			0x80
156 #define PGM_INT_CODE_CRYPTO_OPERATION		0x119
157 #define PGM_INT_CODE_TX_ABORTED_EVENT		0x200
158 
159 struct cpuid {
160 	uint64_t version : 8;
161 	uint64_t id : 24;
162 	uint64_t type : 16;
163 	uint64_t format : 1;
164 	uint64_t reserved : 15;
165 };
166 
167 static inline unsigned short stap(void)
168 {
169 	unsigned short cpu_address;
170 
171 	asm volatile("stap %0" : "=Q" (cpu_address));
172 	return cpu_address;
173 }
174 
175 static inline int tprot(unsigned long addr)
176 {
177 	int cc;
178 
179 	asm volatile(
180 		"	tprot	0(%1),0\n"
181 		"	ipm	%0\n"
182 		"	srl	%0,28\n"
183 		: "=d" (cc) : "a" (addr) : "cc");
184 	return cc;
185 }
186 
187 static inline void lctlg(int cr, uint64_t value)
188 {
189 	asm volatile(
190 		"	lctlg	%1,%1,%0\n"
191 		: : "Q" (value), "i" (cr));
192 }
193 
194 static inline uint64_t stctg(int cr)
195 {
196 	uint64_t value;
197 
198 	asm volatile(
199 		"	stctg	%1,%1,%0\n"
200 		: "=Q" (value) : "i" (cr) : "memory");
201 	return value;
202 }
203 
204 static inline void ctl_set_bit(int cr, unsigned int bit)
205 {
206         uint64_t reg;
207 
208 	reg = stctg(cr);
209 	reg |= 1UL << bit;
210 	lctlg(cr, reg);
211 }
212 
213 static inline void ctl_clear_bit(int cr, unsigned int bit)
214 {
215         uint64_t reg;
216 
217 	reg = stctg(cr);
218 	reg &= ~(1UL << bit);
219 	lctlg(cr, reg);
220 }
221 
222 static inline uint64_t extract_psw_mask(void)
223 {
224 	uint32_t mask_upper = 0, mask_lower = 0;
225 
226 	asm volatile(
227 		"	epsw	%0,%1\n"
228 		: "+r" (mask_upper), "+r" (mask_lower) : : );
229 
230 	return (uint64_t) mask_upper << 32 | mask_lower;
231 }
232 
233 static inline void load_psw_mask(uint64_t mask)
234 {
235 	struct psw psw = {
236 		.mask = mask,
237 		.addr = 0,
238 	};
239 	uint64_t tmp = 0;
240 
241 	asm volatile(
242 		"	larl	%0,0f\n"
243 		"	stg	%0,8(%1)\n"
244 		"	lpswe	0(%1)\n"
245 		"0:\n"
246 		: "+r" (tmp) :  "a" (&psw) : "memory", "cc" );
247 }
248 
249 static inline void enter_pstate(void)
250 {
251 	uint64_t mask;
252 
253 	mask = extract_psw_mask();
254 	mask |= PSW_MASK_PSTATE;
255 	load_psw_mask(mask);
256 }
257 
258 static inline int stsi(void *addr, int fc, int sel1, int sel2)
259 {
260 	register int r0 asm("0") = (fc << 28) | sel1;
261 	register int r1 asm("1") = sel2;
262 	int cc;
263 
264 	asm volatile(
265 		"stsi	0(%3)\n"
266 		"ipm	%[cc]\n"
267 		"srl	%[cc],28\n"
268 		: "+d" (r0), [cc] "=d" (cc)
269 		: "d" (r1), "a" (addr)
270 		: "cc", "memory");
271 	return cc;
272 }
273 
274 static inline int servc(uint32_t command, unsigned long sccb)
275 {
276 	int cc;
277 
278 	asm volatile(
279 		"       .insn   rre,0xb2200000,%1,%2\n"  /* servc %1,%2 */
280 		"       ipm     %0\n"
281 		"       srl     %0,28"
282 		: "=&d" (cc) : "d" (command), "a" (sccb)
283 		: "cc", "memory");
284 	return cc;
285 }
286 
287 static inline void set_prefix(uint32_t new_prefix)
288 {
289 	asm volatile("	spx %0" : : "Q" (new_prefix) : "memory");
290 }
291 
292 static inline uint32_t get_prefix(void)
293 {
294 	uint32_t current_prefix;
295 
296 	asm volatile("	stpx %0" : "=Q" (current_prefix));
297 	return current_prefix;
298 }
299 
300 #endif
301