xref: /kvm-unit-tests/lib/s390x/asm/arch_def.h (revision e36a9c28fc8f8f59e0e30db244af0b17c6d04be9)
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_cr0;			/* 0x0308 */
82 	uint8_t		pad_0x0310[0x11b0 - 0x0310];	/* 0x0310 */
83 	uint64_t	mcck_ext_sa_addr;		/* 0x11b0 */
84 	uint8_t		pad_0x11b8[0x1200 - 0x11b8];	/* 0x11b8 */
85 	uint64_t	fprs_sa[16];			/* 0x1200 */
86 	uint64_t	grs_sa[16];			/* 0x1280 */
87 	struct psw	psw_sa;				/* 0x1300 */
88 	uint8_t		pad_0x1310[0x1318 - 0x1310];	/* 0x1310 */
89 	uint32_t	prefix_sa;			/* 0x1318 */
90 	uint32_t	fpc_sa;				/* 0x131c */
91 	uint8_t		pad_0x1320[0x1324 - 0x1320];	/* 0x1320 */
92 	uint32_t	tod_pr_sa;			/* 0x1324 */
93 	uint64_t	cputm_sa;			/* 0x1328 */
94 	uint64_t	cc_sa;				/* 0x1330 */
95 	uint8_t		pad_0x1338[0x1340 - 0x1338];	/* 0x1338 */
96 	uint32_t	ars_sa[16];			/* 0x1340 */
97 	uint64_t	crs_sa[16];			/* 0x1380 */
98 	uint8_t		pad_0x1400[0x1800 - 0x1400];	/* 0x1400 */
99 	uint8_t		pgm_int_tdb[0x1900 - 0x1800];	/* 0x1800 */
100 } __attribute__ ((__packed__));
101 
102 #define PGM_INT_CODE_OPERATION			0x01
103 #define PGM_INT_CODE_PRIVILEGED_OPERATION	0x02
104 #define PGM_INT_CODE_EXECUTE			0x03
105 #define PGM_INT_CODE_PROTECTION			0x04
106 #define PGM_INT_CODE_ADDRESSING			0x05
107 #define PGM_INT_CODE_SPECIFICATION		0x06
108 #define PGM_INT_CODE_DATA			0x07
109 #define PGM_INT_CODE_FIXED_POINT_OVERFLOW	0x08
110 #define PGM_INT_CODE_FIXED_POINT_DIVIDE		0x09
111 #define PGM_INT_CODE_DECIMAL_OVERFLOW		0x0a
112 #define PGM_INT_CODE_DECIMAL_DIVIDE		0x0b
113 #define PGM_INT_CODE_HFP_EXPONENT_OVERFLOW	0x0c
114 #define PGM_INT_CODE_HFP_EXPONENT_UNDERFLOW	0x0d
115 #define PGM_INT_CODE_HFP_SIGNIFICANCE		0x0e
116 #define PGM_INT_CODE_HFP_DIVIDE			0x0f
117 #define PGM_INT_CODE_SEGMENT_TRANSLATION	0x10
118 #define PGM_INT_CODE_PAGE_TRANSLATION		0x11
119 #define PGM_INT_CODE_TRANSLATION_SPEC		0x12
120 #define PGM_INT_CODE_SPECIAL_OPERATION		0x13
121 #define PGM_INT_CODE_OPERAND			0x15
122 #define PGM_INT_CODE_TRACE_TABLE		0x16
123 #define PGM_INT_CODE_VECTOR_PROCESSING		0x1b
124 #define PGM_INT_CODE_SPACE_SWITCH_EVENT		0x1c
125 #define PGM_INT_CODE_HFP_SQUARE_ROOT		0x1d
126 #define PGM_INT_CODE_PC_TRANSLATION_SPEC	0x1f
127 #define PGM_INT_CODE_AFX_TRANSLATION		0x20
128 #define PGM_INT_CODE_ASX_TRANSLATION		0x21
129 #define PGM_INT_CODE_LX_TRANSLATION		0x22
130 #define PGM_INT_CODE_EX_TRANSLATION		0x23
131 #define PGM_INT_CODE_PRIMARY_AUTHORITY		0x24
132 #define PGM_INT_CODE_SECONDARY_AUTHORITY	0x25
133 #define PGM_INT_CODE_LFX_TRANSLATION		0x26
134 #define PGM_INT_CODE_LSX_TRANSLATION		0x27
135 #define PGM_INT_CODE_ALET_SPECIFICATION		0x28
136 #define PGM_INT_CODE_ALEN_TRANSLATION		0x29
137 #define PGM_INT_CODE_ALE_SEQUENCE		0x2a
138 #define PGM_INT_CODE_ASTE_VALIDITY		0x2b
139 #define PGM_INT_CODE_ASTE_SEQUENCE		0x2c
140 #define PGM_INT_CODE_EXTENDED_AUTHORITY		0x2d
141 #define PGM_INT_CODE_LSTE_SEQUENCE		0x2e
142 #define PGM_INT_CODE_ASTE_INSTANCE		0x2f
143 #define PGM_INT_CODE_STACK_FULL			0x30
144 #define PGM_INT_CODE_STACK_EMPTY		0x31
145 #define PGM_INT_CODE_STACK_SPECIFICATION	0x32
146 #define PGM_INT_CODE_STACK_TYPE			0x33
147 #define PGM_INT_CODE_STACK_OPERATION		0x34
148 #define PGM_INT_CODE_ASCE_TYPE			0x38
149 #define PGM_INT_CODE_REGION_FIRST_TRANS		0x39
150 #define PGM_INT_CODE_REGION_SECOND_TRANS	0x3a
151 #define PGM_INT_CODE_REGION_THIRD_TRANS		0x3b
152 #define PGM_INT_CODE_MONITOR_EVENT		0x40
153 #define PGM_INT_CODE_PER			0x80
154 #define PGM_INT_CODE_CRYPTO_OPERATION		0x119
155 #define PGM_INT_CODE_TX_ABORTED_EVENT		0x200
156 
157 struct cpuid {
158 	uint64_t version : 8;
159 	uint64_t id : 24;
160 	uint64_t type : 16;
161 	uint64_t format : 1;
162 	uint64_t reserved : 15;
163 };
164 
165 static inline unsigned short stap(void)
166 {
167 	unsigned short cpu_address;
168 
169 	asm volatile("stap %0" : "=Q" (cpu_address));
170 	return cpu_address;
171 }
172 
173 static inline int tprot(unsigned long addr)
174 {
175 	int cc;
176 
177 	asm volatile(
178 		"	tprot	0(%1),0\n"
179 		"	ipm	%0\n"
180 		"	srl	%0,28\n"
181 		: "=d" (cc) : "a" (addr) : "cc");
182 	return cc;
183 }
184 
185 static inline void lctlg(int cr, uint64_t value)
186 {
187 	asm volatile(
188 		"	lctlg	%1,%1,%0\n"
189 		: : "Q" (value), "i" (cr));
190 }
191 
192 static inline uint64_t stctg(int cr)
193 {
194 	uint64_t value;
195 
196 	asm volatile(
197 		"	stctg	%1,%1,%0\n"
198 		: "=Q" (value) : "i" (cr) : "memory");
199 	return value;
200 }
201 
202 static inline void ctl_set_bit(int cr, unsigned int bit)
203 {
204         uint64_t reg;
205 
206 	reg = stctg(cr);
207 	reg |= 1UL << bit;
208 	lctlg(cr, reg);
209 }
210 
211 static inline void ctl_clear_bit(int cr, unsigned int bit)
212 {
213         uint64_t reg;
214 
215 	reg = stctg(cr);
216 	reg &= ~(1UL << bit);
217 	lctlg(cr, reg);
218 }
219 
220 static inline uint64_t extract_psw_mask(void)
221 {
222 	uint32_t mask_upper = 0, mask_lower = 0;
223 
224 	asm volatile(
225 		"	epsw	%0,%1\n"
226 		: "+r" (mask_upper), "+r" (mask_lower) : : );
227 
228 	return (uint64_t) mask_upper << 32 | mask_lower;
229 }
230 
231 static inline void load_psw_mask(uint64_t mask)
232 {
233 	struct psw psw = {
234 		.mask = mask,
235 		.addr = 0,
236 	};
237 	uint64_t tmp = 0;
238 
239 	asm volatile(
240 		"	larl	%0,0f\n"
241 		"	stg	%0,8(%1)\n"
242 		"	lpswe	0(%1)\n"
243 		"0:\n"
244 		: "+r" (tmp) :  "a" (&psw) : "memory", "cc" );
245 }
246 
247 static inline void enter_pstate(void)
248 {
249 	uint64_t mask;
250 
251 	mask = extract_psw_mask();
252 	mask |= PSW_MASK_PSTATE;
253 	load_psw_mask(mask);
254 }
255 
256 static inline int stsi(void *addr, int fc, int sel1, int sel2)
257 {
258 	register int r0 asm("0") = (fc << 28) | sel1;
259 	register int r1 asm("1") = sel2;
260 	int cc;
261 
262 	asm volatile(
263 		"stsi	0(%3)\n"
264 		"ipm	%[cc]\n"
265 		"srl	%[cc],28\n"
266 		: "+d" (r0), [cc] "=d" (cc)
267 		: "d" (r1), "a" (addr)
268 		: "cc", "memory");
269 	return cc;
270 }
271 
272 #endif
273