]>
Commit | Line | Data |
---|---|---|
10ec5117 | 1 | /* |
aea1e885 | 2 | * S/390 misc helper routines |
10ec5117 | 3 | * |
defb0e31 | 4 | * Copyright (c) 2009 Ulrich Hecht |
10ec5117 AG |
5 | * Copyright (c) 2009 Alexander Graf |
6 | * | |
7 | * This library is free software; you can redistribute it and/or | |
8 | * modify it under the terms of the GNU Lesser General Public | |
9 | * License as published by the Free Software Foundation; either | |
10 | * version 2 of the License, or (at your option) any later version. | |
11 | * | |
12 | * This library is distributed in the hope that it will be useful, | |
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
15 | * Lesser General Public License for more details. | |
16 | * | |
17 | * You should have received a copy of the GNU Lesser General Public | |
70539e18 | 18 | * License along with this library; if not, see <http://www.gnu.org/licenses/>. |
10ec5117 AG |
19 | */ |
20 | ||
9615495a | 21 | #include "qemu/osdep.h" |
278f5e98 | 22 | #include "qemu/main-loop.h" |
3e457172 | 23 | #include "cpu.h" |
4e58b838 | 24 | #include "internal.h" |
022c62cb | 25 | #include "exec/memory.h" |
1de7afc9 | 26 | #include "qemu/host-utils.h" |
2ef6175a | 27 | #include "exec/helper-proto.h" |
1de7afc9 | 28 | #include "qemu/timer.h" |
df75a4e2 | 29 | #include "exec/address-spaces.h" |
63c91552 | 30 | #include "exec/exec-all.h" |
f08b6170 | 31 | #include "exec/cpu_ldst.h" |
10ec5117 | 32 | |
71e47088 | 33 | #if !defined(CONFIG_USER_ONLY) |
f0778475 | 34 | #include "sysemu/cpus.h" |
9c17d615 | 35 | #include "sysemu/sysemu.h" |
40fa5264 | 36 | #include "hw/s390x/ebcdic.h" |
10ec5117 | 37 | #endif |
d5a43964 | 38 | |
defb0e31 AG |
39 | /* #define DEBUG_HELPER */ |
40 | #ifdef DEBUG_HELPER | |
41 | #define HELPER_LOG(x...) qemu_log(x) | |
42 | #else | |
43 | #define HELPER_LOG(x...) | |
44 | #endif | |
45 | ||
b4e2bd35 RH |
46 | /* Raise an exception dynamically from a helper function. */ |
47 | void QEMU_NORETURN runtime_exception(CPUS390XState *env, int excp, | |
48 | uintptr_t retaddr) | |
49 | { | |
27103424 | 50 | CPUState *cs = CPU(s390_env_get_cpu(env)); |
b4e2bd35 | 51 | |
27103424 | 52 | cs->exception_index = EXCP_PGM; |
b4e2bd35 | 53 | env->int_pgm_code = excp; |
becf8217 | 54 | env->int_pgm_ilen = ILEN_AUTO; |
b4e2bd35 RH |
55 | |
56 | /* Use the (ultimate) callers address to find the insn that trapped. */ | |
3f38f309 | 57 | cpu_restore_state(cs, retaddr); |
b4e2bd35 | 58 | |
5638d180 | 59 | cpu_loop_exit(cs); |
b4e2bd35 RH |
60 | } |
61 | ||
d5a103cd | 62 | /* Raise an exception statically from a TB. */ |
089f5c06 | 63 | void HELPER(exception)(CPUS390XState *env, uint32_t excp) |
defb0e31 | 64 | { |
27103424 AF |
65 | CPUState *cs = CPU(s390_env_get_cpu(env)); |
66 | ||
71e47088 | 67 | HELPER_LOG("%s: exception %d\n", __func__, excp); |
27103424 | 68 | cs->exception_index = excp; |
5638d180 | 69 | cpu_loop_exit(cs); |
defb0e31 AG |
70 | } |
71 | ||
31006af3 AJ |
72 | #ifndef CONFIG_USER_ONLY |
73 | ||
defb0e31 | 74 | /* SCLP service call */ |
dc458df9 | 75 | uint32_t HELPER(servc)(CPUS390XState *env, uint64_t r1, uint64_t r2) |
defb0e31 | 76 | { |
8d04fb55 | 77 | qemu_mutex_lock_iothread(); |
6e252802 | 78 | int r = sclp_service_call(env, r1, r2); |
9abf567d CB |
79 | if (r < 0) { |
80 | program_interrupt(env, -r, 4); | |
8d04fb55 | 81 | r = 0; |
9abf567d | 82 | } |
8d04fb55 | 83 | qemu_mutex_unlock_iothread(); |
9abf567d | 84 | return r; |
defb0e31 AG |
85 | } |
86 | ||
8df7eef3 | 87 | void HELPER(diag)(CPUS390XState *env, uint32_t r1, uint32_t r3, uint32_t num) |
defb0e31 AG |
88 | { |
89 | uint64_t r; | |
90 | ||
91 | switch (num) { | |
92 | case 0x500: | |
93 | /* KVM hypercall */ | |
2cf9953b | 94 | qemu_mutex_lock_iothread(); |
28e942f8 | 95 | r = s390_virtio_hypercall(env); |
2cf9953b | 96 | qemu_mutex_unlock_iothread(); |
defb0e31 AG |
97 | break; |
98 | case 0x44: | |
99 | /* yield */ | |
100 | r = 0; | |
101 | break; | |
102 | case 0x308: | |
103 | /* ipl */ | |
8df7eef3 | 104 | handle_diag_308(env, r1, r3); |
defb0e31 AG |
105 | r = 0; |
106 | break; | |
eb569af8 CH |
107 | case 0x288: |
108 | /* time bomb (watchdog) */ | |
109 | r = handle_diag_288(env, r1, r3); | |
110 | break; | |
defb0e31 AG |
111 | default: |
112 | r = -1; | |
113 | break; | |
114 | } | |
115 | ||
116 | if (r) { | |
a8aec856 | 117 | program_interrupt(env, PGM_SPECIFICATION, ILEN_AUTO); |
defb0e31 | 118 | } |
defb0e31 AG |
119 | } |
120 | ||
defb0e31 | 121 | /* Set Prefix */ |
089f5c06 | 122 | void HELPER(spx)(CPUS390XState *env, uint64_t a1) |
defb0e31 | 123 | { |
31b030d4 | 124 | CPUState *cs = CPU(s390_env_get_cpu(env)); |
e805a0d3 | 125 | uint32_t prefix = a1 & 0x7fffe000; |
31b030d4 | 126 | |
e805a0d3 | 127 | env->psa = prefix; |
aafcf80e | 128 | HELPER_LOG("prefix: %#x\n", prefix); |
31b030d4 AF |
129 | tlb_flush_page(cs, 0); |
130 | tlb_flush_page(cs, TARGET_PAGE_SIZE); | |
defb0e31 AG |
131 | } |
132 | ||
d9d55f11 AJ |
133 | /* Store Clock */ |
134 | uint64_t HELPER(stck)(CPUS390XState *env) | |
defb0e31 AG |
135 | { |
136 | uint64_t time; | |
137 | ||
138 | time = env->tod_offset + | |
bc72ad67 | 139 | time2tod(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) - env->tod_basetime); |
defb0e31 AG |
140 | |
141 | return time; | |
142 | } | |
143 | ||
defb0e31 | 144 | /* Set Clock Comparator */ |
dd3eb7b5 | 145 | void HELPER(sckc)(CPUS390XState *env, uint64_t time) |
defb0e31 | 146 | { |
defb0e31 AG |
147 | if (time == -1ULL) { |
148 | return; | |
149 | } | |
150 | ||
aa9e14e6 AJ |
151 | env->ckc = time; |
152 | ||
c941f074 AJ |
153 | /* difference between origins */ |
154 | time -= env->tod_offset; | |
155 | ||
defb0e31 | 156 | /* nanoseconds */ |
9cb32c44 | 157 | time = tod2time(time); |
defb0e31 | 158 | |
c941f074 | 159 | timer_mod(env->tod_timer, env->tod_basetime + time); |
defb0e31 AG |
160 | } |
161 | ||
162 | /* Store Clock Comparator */ | |
dd3eb7b5 | 163 | uint64_t HELPER(stckc)(CPUS390XState *env) |
defb0e31 | 164 | { |
aa9e14e6 | 165 | return env->ckc; |
defb0e31 AG |
166 | } |
167 | ||
168 | /* Set CPU Timer */ | |
c4f0a863 | 169 | void HELPER(spt)(CPUS390XState *env, uint64_t time) |
defb0e31 | 170 | { |
defb0e31 AG |
171 | if (time == -1ULL) { |
172 | return; | |
173 | } | |
174 | ||
175 | /* nanoseconds */ | |
9cb32c44 | 176 | time = tod2time(time); |
defb0e31 | 177 | |
b8ae94bd AJ |
178 | env->cputm = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + time; |
179 | ||
180 | timer_mod(env->cpu_timer, env->cputm); | |
defb0e31 AG |
181 | } |
182 | ||
183 | /* Store CPU Timer */ | |
c4f0a863 | 184 | uint64_t HELPER(stpt)(CPUS390XState *env) |
defb0e31 | 185 | { |
b8ae94bd | 186 | return time2tod(env->cputm - qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL)); |
defb0e31 AG |
187 | } |
188 | ||
189 | /* Store System Information */ | |
d14b3e09 RH |
190 | uint32_t HELPER(stsi)(CPUS390XState *env, uint64_t a0, |
191 | uint64_t r0, uint64_t r1) | |
defb0e31 | 192 | { |
076d4d39 | 193 | S390CPU *cpu = s390_env_get_cpu(env); |
defb0e31 AG |
194 | int cc = 0; |
195 | int sel1, sel2; | |
196 | ||
197 | if ((r0 & STSI_LEVEL_MASK) <= STSI_LEVEL_3 && | |
198 | ((r0 & STSI_R0_RESERVED_MASK) || (r1 & STSI_R1_RESERVED_MASK))) { | |
199 | /* valid function code, invalid reserved bits */ | |
031631c3 | 200 | program_interrupt(env, PGM_SPECIFICATION, 4); |
defb0e31 AG |
201 | } |
202 | ||
203 | sel1 = r0 & STSI_R0_SEL1_MASK; | |
204 | sel2 = r1 & STSI_R1_SEL2_MASK; | |
205 | ||
206 | /* XXX: spec exception if sysib is not 4k-aligned */ | |
207 | ||
208 | switch (r0 & STSI_LEVEL_MASK) { | |
209 | case STSI_LEVEL_1: | |
210 | if ((sel1 == 1) && (sel2 == 1)) { | |
211 | /* Basic Machine Configuration */ | |
212 | struct sysib_111 sysib; | |
076d4d39 | 213 | char type[5] = {}; |
defb0e31 AG |
214 | |
215 | memset(&sysib, 0, sizeof(sysib)); | |
216 | ebcdic_put(sysib.manuf, "QEMU ", 16); | |
076d4d39 DH |
217 | /* same as machine type number in STORE CPU ID, but in EBCDIC */ |
218 | snprintf(type, ARRAY_SIZE(type), "%X", cpu->model->def->type); | |
219 | ebcdic_put(sysib.type, type, 4); | |
220 | /* model number (not stored in STORE CPU ID for z/Architecure) */ | |
defb0e31 AG |
221 | ebcdic_put(sysib.model, "QEMU ", 16); |
222 | ebcdic_put(sysib.sequence, "QEMU ", 16); | |
223 | ebcdic_put(sysib.plant, "QEMU", 4); | |
eb6282f2 | 224 | cpu_physical_memory_write(a0, &sysib, sizeof(sysib)); |
defb0e31 AG |
225 | } else if ((sel1 == 2) && (sel2 == 1)) { |
226 | /* Basic Machine CPU */ | |
227 | struct sysib_121 sysib; | |
228 | ||
229 | memset(&sysib, 0, sizeof(sysib)); | |
230 | /* XXX make different for different CPUs? */ | |
231 | ebcdic_put(sysib.sequence, "QEMUQEMUQEMUQEMU", 16); | |
232 | ebcdic_put(sysib.plant, "QEMU", 4); | |
233 | stw_p(&sysib.cpu_addr, env->cpu_num); | |
eb6282f2 | 234 | cpu_physical_memory_write(a0, &sysib, sizeof(sysib)); |
defb0e31 AG |
235 | } else if ((sel1 == 2) && (sel2 == 2)) { |
236 | /* Basic Machine CPUs */ | |
237 | struct sysib_122 sysib; | |
238 | ||
239 | memset(&sysib, 0, sizeof(sysib)); | |
240 | stl_p(&sysib.capability, 0x443afc29); | |
241 | /* XXX change when SMP comes */ | |
242 | stw_p(&sysib.total_cpus, 1); | |
243 | stw_p(&sysib.active_cpus, 1); | |
244 | stw_p(&sysib.standby_cpus, 0); | |
245 | stw_p(&sysib.reserved_cpus, 0); | |
eb6282f2 | 246 | cpu_physical_memory_write(a0, &sysib, sizeof(sysib)); |
defb0e31 AG |
247 | } else { |
248 | cc = 3; | |
249 | } | |
250 | break; | |
251 | case STSI_LEVEL_2: | |
71e47088 BS |
252 | { |
253 | if ((sel1 == 2) && (sel2 == 1)) { | |
254 | /* LPAR CPU */ | |
255 | struct sysib_221 sysib; | |
256 | ||
257 | memset(&sysib, 0, sizeof(sysib)); | |
258 | /* XXX make different for different CPUs? */ | |
259 | ebcdic_put(sysib.sequence, "QEMUQEMUQEMUQEMU", 16); | |
260 | ebcdic_put(sysib.plant, "QEMU", 4); | |
261 | stw_p(&sysib.cpu_addr, env->cpu_num); | |
262 | stw_p(&sysib.cpu_id, 0); | |
eb6282f2 | 263 | cpu_physical_memory_write(a0, &sysib, sizeof(sysib)); |
71e47088 BS |
264 | } else if ((sel1 == 2) && (sel2 == 2)) { |
265 | /* LPAR CPUs */ | |
266 | struct sysib_222 sysib; | |
267 | ||
268 | memset(&sysib, 0, sizeof(sysib)); | |
269 | stw_p(&sysib.lpar_num, 0); | |
270 | sysib.lcpuc = 0; | |
271 | /* XXX change when SMP comes */ | |
272 | stw_p(&sysib.total_cpus, 1); | |
273 | stw_p(&sysib.conf_cpus, 1); | |
274 | stw_p(&sysib.standby_cpus, 0); | |
275 | stw_p(&sysib.reserved_cpus, 0); | |
276 | ebcdic_put(sysib.name, "QEMU ", 8); | |
277 | stl_p(&sysib.caf, 1000); | |
278 | stw_p(&sysib.dedicated_cpus, 0); | |
279 | stw_p(&sysib.shared_cpus, 0); | |
eb6282f2 | 280 | cpu_physical_memory_write(a0, &sysib, sizeof(sysib)); |
71e47088 BS |
281 | } else { |
282 | cc = 3; | |
283 | } | |
284 | break; | |
defb0e31 | 285 | } |
defb0e31 | 286 | case STSI_LEVEL_3: |
71e47088 BS |
287 | { |
288 | if ((sel1 == 2) && (sel2 == 2)) { | |
289 | /* VM CPUs */ | |
290 | struct sysib_322 sysib; | |
291 | ||
292 | memset(&sysib, 0, sizeof(sysib)); | |
293 | sysib.count = 1; | |
294 | /* XXX change when SMP comes */ | |
295 | stw_p(&sysib.vm[0].total_cpus, 1); | |
296 | stw_p(&sysib.vm[0].conf_cpus, 1); | |
297 | stw_p(&sysib.vm[0].standby_cpus, 0); | |
298 | stw_p(&sysib.vm[0].reserved_cpus, 0); | |
299 | ebcdic_put(sysib.vm[0].name, "KVMguest", 8); | |
300 | stl_p(&sysib.vm[0].caf, 1000); | |
301 | ebcdic_put(sysib.vm[0].cpi, "KVM/Linux ", 16); | |
eb6282f2 | 302 | cpu_physical_memory_write(a0, &sysib, sizeof(sysib)); |
71e47088 BS |
303 | } else { |
304 | cc = 3; | |
305 | } | |
306 | break; | |
defb0e31 | 307 | } |
defb0e31 AG |
308 | case STSI_LEVEL_CURRENT: |
309 | env->regs[0] = STSI_LEVEL_3; | |
310 | break; | |
311 | default: | |
312 | cc = 3; | |
313 | break; | |
314 | } | |
315 | ||
316 | return cc; | |
317 | } | |
318 | ||
089f5c06 BS |
319 | uint32_t HELPER(sigp)(CPUS390XState *env, uint64_t order_code, uint32_t r1, |
320 | uint64_t cpu_addr) | |
defb0e31 | 321 | { |
5172b780 | 322 | int cc = SIGP_CC_ORDER_CODE_ACCEPTED; |
defb0e31 AG |
323 | |
324 | HELPER_LOG("%s: %016" PRIx64 " %08x %016" PRIx64 "\n", | |
71e47088 | 325 | __func__, order_code, r1, cpu_addr); |
defb0e31 | 326 | |
71e47088 | 327 | /* Remember: Use "R1 or R1 + 1, whichever is the odd-numbered register" |
defb0e31 AG |
328 | as parameter (input). Status (output) is always R1. */ |
329 | ||
a7c1fadf | 330 | switch (order_code & SIGP_ORDER_MASK) { |
defb0e31 AG |
331 | case SIGP_SET_ARCH: |
332 | /* switch arch */ | |
333 | break; | |
334 | case SIGP_SENSE: | |
335 | /* enumerate CPU status */ | |
336 | if (cpu_addr) { | |
337 | /* XXX implement when SMP comes */ | |
338 | return 3; | |
339 | } | |
340 | env->regs[r1] &= 0xffffffff00000000ULL; | |
341 | cc = 1; | |
342 | break; | |
71e47088 | 343 | #if !defined(CONFIG_USER_ONLY) |
1864b94a | 344 | case SIGP_RESTART: |
cf83f140 | 345 | qemu_system_reset_request(SHUTDOWN_CAUSE_GUEST_RESET); |
5638d180 | 346 | cpu_loop_exit(CPU(s390_env_get_cpu(env))); |
1864b94a AG |
347 | break; |
348 | case SIGP_STOP: | |
cf83f140 | 349 | qemu_system_shutdown_request(SHUTDOWN_CAUSE_GUEST_SHUTDOWN); |
5638d180 | 350 | cpu_loop_exit(CPU(s390_env_get_cpu(env))); |
1864b94a AG |
351 | break; |
352 | #endif | |
defb0e31 AG |
353 | default: |
354 | /* unknown sigp */ | |
355 | fprintf(stderr, "XXX unknown sigp: 0x%" PRIx64 "\n", order_code); | |
5172b780 | 356 | cc = SIGP_CC_NOT_OPERATIONAL; |
defb0e31 AG |
357 | } |
358 | ||
359 | return cc; | |
360 | } | |
defb0e31 | 361 | #endif |
ad8a4570 AG |
362 | |
363 | #ifndef CONFIG_USER_ONLY | |
364 | void HELPER(xsch)(CPUS390XState *env, uint64_t r1) | |
365 | { | |
366 | S390CPU *cpu = s390_env_get_cpu(env); | |
278f5e98 | 367 | qemu_mutex_lock_iothread(); |
ad8a4570 | 368 | ioinst_handle_xsch(cpu, r1); |
278f5e98 | 369 | qemu_mutex_unlock_iothread(); |
ad8a4570 AG |
370 | } |
371 | ||
372 | void HELPER(csch)(CPUS390XState *env, uint64_t r1) | |
373 | { | |
374 | S390CPU *cpu = s390_env_get_cpu(env); | |
278f5e98 | 375 | qemu_mutex_lock_iothread(); |
ad8a4570 | 376 | ioinst_handle_csch(cpu, r1); |
278f5e98 | 377 | qemu_mutex_unlock_iothread(); |
ad8a4570 AG |
378 | } |
379 | ||
380 | void HELPER(hsch)(CPUS390XState *env, uint64_t r1) | |
381 | { | |
382 | S390CPU *cpu = s390_env_get_cpu(env); | |
278f5e98 | 383 | qemu_mutex_lock_iothread(); |
ad8a4570 | 384 | ioinst_handle_hsch(cpu, r1); |
278f5e98 | 385 | qemu_mutex_unlock_iothread(); |
ad8a4570 AG |
386 | } |
387 | ||
388 | void HELPER(msch)(CPUS390XState *env, uint64_t r1, uint64_t inst) | |
389 | { | |
390 | S390CPU *cpu = s390_env_get_cpu(env); | |
278f5e98 | 391 | qemu_mutex_lock_iothread(); |
ad8a4570 | 392 | ioinst_handle_msch(cpu, r1, inst >> 16); |
278f5e98 | 393 | qemu_mutex_unlock_iothread(); |
ad8a4570 AG |
394 | } |
395 | ||
396 | void HELPER(rchp)(CPUS390XState *env, uint64_t r1) | |
397 | { | |
398 | S390CPU *cpu = s390_env_get_cpu(env); | |
278f5e98 | 399 | qemu_mutex_lock_iothread(); |
ad8a4570 | 400 | ioinst_handle_rchp(cpu, r1); |
278f5e98 | 401 | qemu_mutex_unlock_iothread(); |
ad8a4570 AG |
402 | } |
403 | ||
404 | void HELPER(rsch)(CPUS390XState *env, uint64_t r1) | |
405 | { | |
406 | S390CPU *cpu = s390_env_get_cpu(env); | |
278f5e98 | 407 | qemu_mutex_lock_iothread(); |
ad8a4570 | 408 | ioinst_handle_rsch(cpu, r1); |
278f5e98 | 409 | qemu_mutex_unlock_iothread(); |
ad8a4570 AG |
410 | } |
411 | ||
412 | void HELPER(ssch)(CPUS390XState *env, uint64_t r1, uint64_t inst) | |
413 | { | |
414 | S390CPU *cpu = s390_env_get_cpu(env); | |
278f5e98 | 415 | qemu_mutex_lock_iothread(); |
ad8a4570 | 416 | ioinst_handle_ssch(cpu, r1, inst >> 16); |
278f5e98 | 417 | qemu_mutex_unlock_iothread(); |
ad8a4570 AG |
418 | } |
419 | ||
420 | void HELPER(stsch)(CPUS390XState *env, uint64_t r1, uint64_t inst) | |
421 | { | |
422 | S390CPU *cpu = s390_env_get_cpu(env); | |
278f5e98 | 423 | qemu_mutex_lock_iothread(); |
ad8a4570 | 424 | ioinst_handle_stsch(cpu, r1, inst >> 16); |
278f5e98 | 425 | qemu_mutex_unlock_iothread(); |
ad8a4570 AG |
426 | } |
427 | ||
428 | void HELPER(tsch)(CPUS390XState *env, uint64_t r1, uint64_t inst) | |
429 | { | |
430 | S390CPU *cpu = s390_env_get_cpu(env); | |
278f5e98 | 431 | qemu_mutex_lock_iothread(); |
ad8a4570 | 432 | ioinst_handle_tsch(cpu, r1, inst >> 16); |
278f5e98 | 433 | qemu_mutex_unlock_iothread(); |
ad8a4570 AG |
434 | } |
435 | ||
436 | void HELPER(chsc)(CPUS390XState *env, uint64_t inst) | |
437 | { | |
438 | S390CPU *cpu = s390_env_get_cpu(env); | |
278f5e98 | 439 | qemu_mutex_lock_iothread(); |
ad8a4570 | 440 | ioinst_handle_chsc(cpu, inst >> 16); |
278f5e98 | 441 | qemu_mutex_unlock_iothread(); |
ad8a4570 AG |
442 | } |
443 | #endif | |
777c98c3 AJ |
444 | |
445 | #ifndef CONFIG_USER_ONLY | |
446 | void HELPER(per_check_exception)(CPUS390XState *env) | |
447 | { | |
448 | CPUState *cs = CPU(s390_env_get_cpu(env)); | |
449 | ||
450 | if (env->per_perc_atmid) { | |
451 | env->int_pgm_code = PGM_PER; | |
452 | env->int_pgm_ilen = get_ilen(cpu_ldub_code(env, env->per_address)); | |
453 | ||
454 | cs->exception_index = EXCP_PGM; | |
455 | cpu_loop_exit(cs); | |
456 | } | |
457 | } | |
2c2275eb | 458 | |
d9b8daf9 DH |
459 | /* Check if an address is within the PER starting address and the PER |
460 | ending address. The address range might loop. */ | |
461 | static inline bool get_per_in_range(CPUS390XState *env, uint64_t addr) | |
462 | { | |
463 | if (env->cregs[10] <= env->cregs[11]) { | |
464 | return env->cregs[10] <= addr && addr <= env->cregs[11]; | |
465 | } else { | |
466 | return env->cregs[10] <= addr || addr <= env->cregs[11]; | |
467 | } | |
468 | } | |
469 | ||
2c2275eb AJ |
470 | void HELPER(per_branch)(CPUS390XState *env, uint64_t from, uint64_t to) |
471 | { | |
472 | if ((env->cregs[9] & PER_CR9_EVENT_BRANCH)) { | |
473 | if (!(env->cregs[9] & PER_CR9_CONTROL_BRANCH_ADDRESS) | |
474 | || get_per_in_range(env, to)) { | |
475 | env->per_address = from; | |
476 | env->per_perc_atmid = PER_CODE_EVENT_BRANCH | get_per_atmid(env); | |
477 | } | |
478 | } | |
479 | } | |
f0e0d817 AJ |
480 | |
481 | void HELPER(per_ifetch)(CPUS390XState *env, uint64_t addr) | |
482 | { | |
483 | if ((env->cregs[9] & PER_CR9_EVENT_IFETCH) && get_per_in_range(env, addr)) { | |
484 | env->per_address = addr; | |
485 | env->per_perc_atmid = PER_CODE_EVENT_IFETCH | get_per_atmid(env); | |
83bb1612 AJ |
486 | |
487 | /* If the instruction has to be nullified, trigger the | |
488 | exception immediately. */ | |
489 | if (env->cregs[9] & PER_CR9_EVENT_NULLIFICATION) { | |
490 | CPUState *cs = CPU(s390_env_get_cpu(env)); | |
491 | ||
465aec46 | 492 | env->per_perc_atmid |= PER_CODE_EVENT_NULLIFICATION; |
83bb1612 AJ |
493 | env->int_pgm_code = PGM_PER; |
494 | env->int_pgm_ilen = get_ilen(cpu_ldub_code(env, addr)); | |
495 | ||
496 | cs->exception_index = EXCP_PGM; | |
497 | cpu_loop_exit(cs); | |
498 | } | |
f0e0d817 AJ |
499 | } |
500 | } | |
777c98c3 | 501 | #endif |
5bf83628 RH |
502 | |
503 | /* The maximum bit defined at the moment is 129. */ | |
504 | #define MAX_STFL_WORDS 3 | |
505 | ||
506 | /* Canonicalize the current cpu's features into the 64-bit words required | |
507 | by STFLE. Return the index-1 of the max word that is non-zero. */ | |
508 | static unsigned do_stfle(CPUS390XState *env, uint64_t words[MAX_STFL_WORDS]) | |
509 | { | |
510 | S390CPU *cpu = s390_env_get_cpu(env); | |
511 | const unsigned long *features = cpu->model->features; | |
512 | unsigned max_bit = 0; | |
513 | S390Feat feat; | |
514 | ||
515 | memset(words, 0, sizeof(uint64_t) * MAX_STFL_WORDS); | |
516 | ||
517 | if (test_bit(S390_FEAT_ZARCH, features)) { | |
518 | /* z/Architecture is always active if around */ | |
519 | words[0] = 1ull << (63 - 2); | |
520 | } | |
521 | ||
522 | for (feat = find_first_bit(features, S390_FEAT_MAX); | |
523 | feat < S390_FEAT_MAX; | |
524 | feat = find_next_bit(features, S390_FEAT_MAX, feat + 1)) { | |
525 | const S390FeatDef *def = s390_feat_def(feat); | |
526 | if (def->type == S390_FEAT_TYPE_STFL) { | |
527 | unsigned bit = def->bit; | |
528 | if (bit > max_bit) { | |
529 | max_bit = bit; | |
530 | } | |
531 | assert(bit / 64 < MAX_STFL_WORDS); | |
532 | words[bit / 64] |= 1ULL << (63 - bit % 64); | |
533 | } | |
534 | } | |
535 | ||
536 | return max_bit / 64; | |
537 | } | |
538 | ||
539 | void HELPER(stfl)(CPUS390XState *env) | |
540 | { | |
541 | uint64_t words[MAX_STFL_WORDS]; | |
542 | ||
543 | do_stfle(env, words); | |
544 | cpu_stl_data(env, 200, words[0] >> 32); | |
545 | } | |
546 | ||
547 | uint32_t HELPER(stfle)(CPUS390XState *env, uint64_t addr) | |
548 | { | |
549 | uint64_t words[MAX_STFL_WORDS]; | |
550 | unsigned count_m1 = env->regs[0] & 0xff; | |
551 | unsigned max_m1 = do_stfle(env, words); | |
552 | unsigned i; | |
553 | ||
554 | for (i = 0; i <= count_m1; ++i) { | |
555 | cpu_stq_data(env, addr + 8 * i, words[i]); | |
556 | } | |
557 | ||
558 | env->regs[0] = deposit64(env->regs[0], 0, 8, max_m1); | |
559 | return (count_m1 >= max_m1 ? 0 : 3); | |
560 | } |