xref: /kvm-unit-tests/lib/s390x/asm/arch_def.h (revision 6ca8c283190847bd2ff648232f98f70e4e6d3d0d)
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_DAT			0x0400000000000000UL
19 #define PSW_MASK_PSTATE			0x0001000000000000UL
20 
21 struct lowcore {
22 	uint8_t		pad_0x0000[0x0080 - 0x0000];	/* 0x0000 */
23 	uint32_t	ext_int_param;			/* 0x0080 */
24 	uint16_t	cpu_addr;			/* 0x0084 */
25 	uint16_t	ext_int_code;			/* 0x0086 */
26 	uint16_t	svc_int_id;			/* 0x0088 */
27 	uint16_t	svc_int_code;			/* 0x008a */
28 	uint16_t	pgm_int_id;			/* 0x008c */
29 	uint16_t	pgm_int_code;			/* 0x008e */
30 	uint32_t	dxc_vxc;			/* 0x0090 */
31 	uint16_t	mon_class_nb;			/* 0x0094 */
32 	uint8_t		per_code;			/* 0x0096 */
33 	uint8_t		per_atmid;			/* 0x0097 */
34 	uint64_t	per_addr;			/* 0x0098 */
35 	uint8_t		exc_acc_id;			/* 0x00a0 */
36 	uint8_t		per_acc_id;			/* 0x00a1 */
37 	uint8_t		op_acc_id;			/* 0x00a2 */
38 	uint8_t		arch_mode_id;			/* 0x00a3 */
39 	uint8_t		pad_0x00a4[0x00a8 - 0x00a4];	/* 0x00a4 */
40 	uint64_t	trans_exc_id;			/* 0x00a8 */
41 	uint64_t	mon_code;			/* 0x00b0 */
42 	uint32_t	subsys_id_word;			/* 0x00b8 */
43 	uint32_t	io_int_param;			/* 0x00bc */
44 	uint32_t	io_int_word;			/* 0x00c0 */
45 	uint8_t		pad_0x00c4[0x00c8 - 0x00c4];	/* 0x00c4 */
46 	uint32_t	stfl;				/* 0x00c8 */
47 	uint8_t		pad_0x00cc[0x00e8 - 0x00cc];	/* 0x00cc */
48 	uint64_t	mcck_int_code;			/* 0x00e8 */
49 	uint8_t		pad_0x00f0[0x00f4 - 0x00f0];	/* 0x00f0 */
50 	uint32_t	ext_damage_code;		/* 0x00f4 */
51 	uint64_t	failing_storage_addr;		/* 0x00f8 */
52 	uint64_t	emon_ca_origin;			/* 0x0100 */
53 	uint32_t	emon_ca_size;			/* 0x0108 */
54 	uint32_t	emon_exc_count;			/* 0x010c */
55 	uint64_t	breaking_event_addr;		/* 0x0110 */
56 	uint8_t		pad_0x0118[0x0120 - 0x0118];	/* 0x0118 */
57 	struct psw	restart_old_psw;		/* 0x0120 */
58 	struct psw	ext_old_psw;			/* 0x0130 */
59 	struct psw	svc_old_psw;			/* 0x0140 */
60 	struct psw	pgm_old_psw;			/* 0x0150 */
61 	struct psw	mcck_old_psw;			/* 0x0160 */
62 	struct psw	io_old_psw;			/* 0x0170 */
63 	uint8_t		pad_0x0180[0x01a0 - 0x0180];	/* 0x0180 */
64 	struct psw	restart_new_psw;		/* 0x01a0 */
65 	struct psw	ext_new_psw;			/* 0x01b0 */
66 	struct psw	svc_new_psw;			/* 0x01c0 */
67 	struct psw	pgm_new_psw;			/* 0x01d0 */
68 	struct psw	mcck_new_psw;			/* 0x01e0 */
69 	struct psw	io_new_psw;			/* 0x01f0 */
70 	/* sw definition: save area for registers in interrupt handlers */
71 	uint64_t	sw_int_grs[16];			/* 0x0200 */
72 	uint64_t	sw_int_fprs[16];		/* 0x0280 */
73 	uint32_t	sw_int_fpc;			/* 0x0300 */
74 	uint8_t		pad_0x0304[0x0308 - 0x0304];	/* 0x0304 */
75 	uint64_t	sw_int_cr0;			/* 0x0308 */
76 	uint8_t		pad_0x0310[0x11b0 - 0x0310];	/* 0x0310 */
77 	uint64_t	mcck_ext_sa_addr;		/* 0x11b0 */
78 	uint8_t		pad_0x11b8[0x1200 - 0x11b8];	/* 0x11b8 */
79 	uint64_t	fprs_sa[16];			/* 0x1200 */
80 	uint64_t	grs_sa[16];			/* 0x1280 */
81 	struct psw	psw_sa;				/* 0x1300 */
82 	uint8_t		pad_0x1310[0x1318 - 0x1310];	/* 0x1310 */
83 	uint32_t	prefix_sa;			/* 0x1318 */
84 	uint32_t	fpc_sa;				/* 0x131c */
85 	uint8_t		pad_0x1320[0x1324 - 0x1320];	/* 0x1320 */
86 	uint32_t	tod_pr_sa;			/* 0x1324 */
87 	uint64_t	cputm_sa;			/* 0x1328 */
88 	uint64_t	cc_sa;				/* 0x1330 */
89 	uint8_t		pad_0x1338[0x1340 - 0x1338];	/* 0x1338 */
90 	uint32_t	ars_sa[16];			/* 0x1340 */
91 	uint64_t	crs_sa[16];			/* 0x1380 */
92 	uint8_t		pad_0x1400[0x1800 - 0x1400];	/* 0x1400 */
93 	uint8_t		pgm_int_tdb[0x1900 - 0x1800];	/* 0x1800 */
94 } __attribute__ ((__packed__));
95 
96 #define PGM_INT_CODE_OPERATION			0x01
97 #define PGM_INT_CODE_PRIVILEGED_OPERATION	0x02
98 #define PGM_INT_CODE_EXECUTE			0x03
99 #define PGM_INT_CODE_PROTECTION			0x04
100 #define PGM_INT_CODE_ADDRESSING			0x05
101 #define PGM_INT_CODE_SPECIFICATION		0x06
102 #define PGM_INT_CODE_DATA			0x07
103 #define PGM_INT_CODE_FIXED_POINT_OVERFLOW	0x08
104 #define PGM_INT_CODE_FIXED_POINT_DIVIDE		0x09
105 #define PGM_INT_CODE_DECIMAL_OVERFLOW		0x0a
106 #define PGM_INT_CODE_DECIMAL_DIVIDE		0x0b
107 #define PGM_INT_CODE_HFP_EXPONENT_OVERFLOW	0x0c
108 #define PGM_INT_CODE_HFP_EXPONENT_UNDERFLOW	0x0d
109 #define PGM_INT_CODE_HFP_SIGNIFICANCE		0x0e
110 #define PGM_INT_CODE_HFP_DIVIDE			0x0f
111 #define PGM_INT_CODE_SEGMENT_TRANSLATION	0x10
112 #define PGM_INT_CODE_PAGE_TRANSLATION		0x11
113 #define PGM_INT_CODE_TRANSLATION_SPEC		0x12
114 #define PGM_INT_CODE_SPECIAL_OPERATION		0x13
115 #define PGM_INT_CODE_OPERAND			0x15
116 #define PGM_INT_CODE_TRACE_TABLE		0x16
117 #define PGM_INT_CODE_VECTOR_PROCESSING		0x1b
118 #define PGM_INT_CODE_SPACE_SWITCH_EVENT		0x1c
119 #define PGM_INT_CODE_HFP_SQUARE_ROOT		0x1d
120 #define PGM_INT_CODE_PC_TRANSLATION_SPEC	0x1f
121 #define PGM_INT_CODE_AFX_TRANSLATION		0x20
122 #define PGM_INT_CODE_ASX_TRANSLATION		0x21
123 #define PGM_INT_CODE_LX_TRANSLATION		0x22
124 #define PGM_INT_CODE_EX_TRANSLATION		0x23
125 #define PGM_INT_CODE_PRIMARY_AUTHORITY		0x24
126 #define PGM_INT_CODE_SECONDARY_AUTHORITY	0x25
127 #define PGM_INT_CODE_LFX_TRANSLATION		0x26
128 #define PGM_INT_CODE_LSX_TRANSLATION		0x27
129 #define PGM_INT_CODE_ALET_SPECIFICATION		0x28
130 #define PGM_INT_CODE_ALEN_TRANSLATION		0x29
131 #define PGM_INT_CODE_ALE_SEQUENCE		0x2a
132 #define PGM_INT_CODE_ASTE_VALIDITY		0x2b
133 #define PGM_INT_CODE_ASTE_SEQUENCE		0x2c
134 #define PGM_INT_CODE_EXTENDED_AUTHORITY		0x2d
135 #define PGM_INT_CODE_LSTE_SEQUENCE		0x2e
136 #define PGM_INT_CODE_ASTE_INSTANCE		0x2f
137 #define PGM_INT_CODE_STACK_FULL			0x30
138 #define PGM_INT_CODE_STACK_EMPTY		0x31
139 #define PGM_INT_CODE_STACK_SPECIFICATION	0x32
140 #define PGM_INT_CODE_STACK_TYPE			0x33
141 #define PGM_INT_CODE_STACK_OPERATION		0x34
142 #define PGM_INT_CODE_ASCE_TYPE			0x38
143 #define PGM_INT_CODE_REGION_FIRST_TRANS		0x39
144 #define PGM_INT_CODE_REGION_SECOND_TRANS	0x3a
145 #define PGM_INT_CODE_REGION_THIRD_TRANS		0x3b
146 #define PGM_INT_CODE_MONITOR_EVENT		0x40
147 #define PGM_INT_CODE_PER			0x80
148 #define PGM_INT_CODE_CRYPTO_OPERATION		0x119
149 #define PGM_INT_CODE_TX_ABORTED_EVENT		0x200
150 
151 struct cpuid {
152 	uint64_t version : 8;
153 	uint64_t id : 24;
154 	uint64_t type : 16;
155 	uint64_t format : 1;
156 	uint64_t reserved : 15;
157 };
158 
159 static inline int tprot(unsigned long addr)
160 {
161 	int cc;
162 
163 	asm volatile(
164 		"	tprot	0(%1),0\n"
165 		"	ipm	%0\n"
166 		"	srl	%0,28\n"
167 		: "=d" (cc) : "a" (addr) : "cc");
168 	return cc;
169 }
170 
171 static inline void lctlg(int cr, uint64_t value)
172 {
173 	asm volatile(
174 		"	lctlg	%1,%1,%0\n"
175 		: : "Q" (value), "i" (cr));
176 }
177 
178 static inline uint64_t stctg(int cr)
179 {
180 	uint64_t value;
181 
182 	asm volatile(
183 		"	stctg	%1,%1,%0\n"
184 		: "=Q" (value) : "i" (cr) : "memory");
185 	return value;
186 }
187 
188 static inline void ctl_set_bit(int cr, unsigned int bit)
189 {
190         uint64_t reg;
191 
192 	reg = stctg(cr);
193 	reg |= 1UL << bit;
194 	lctlg(cr, reg);
195 }
196 
197 static inline void ctl_clear_bit(int cr, unsigned int bit)
198 {
199         uint64_t reg;
200 
201 	reg = stctg(cr);
202 	reg &= ~(1UL << bit);
203 	lctlg(cr, reg);
204 }
205 
206 static inline uint64_t extract_psw_mask(void)
207 {
208 	uint32_t mask_upper = 0, mask_lower = 0;
209 
210 	asm volatile(
211 		"	epsw	%0,%1\n"
212 		: "+r" (mask_upper), "+r" (mask_lower) : : );
213 
214 	return (uint64_t) mask_upper << 32 | mask_lower;
215 }
216 
217 static inline void load_psw_mask(uint64_t mask)
218 {
219 	struct psw psw = {
220 		.mask = mask,
221 		.addr = 0,
222 	};
223 	uint64_t tmp = 0;
224 
225 	asm volatile(
226 		"	larl	%0,0f\n"
227 		"	stg	%0,8(%1)\n"
228 		"	lpswe	0(%1)\n"
229 		"0:\n"
230 		: "+r" (tmp) :  "a" (&psw) : "memory", "cc" );
231 }
232 
233 static inline void enter_pstate(void)
234 {
235 	uint64_t mask;
236 
237 	mask = extract_psw_mask();
238 	mask |= PSW_MASK_PSTATE;
239 	load_psw_mask(mask);
240 }
241 
242 #endif
243