]>
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" |
2c98a6c1 | 37 | #include "hw/s390x/s390-virtio-hcall.h" |
53d8e91d | 38 | #include "hw/s390x/sclp.h" |
6a253de3 | 39 | #include "hw/s390x/s390_flic.h" |
79947862 DH |
40 | #include "hw/s390x/ioinst.h" |
41 | #include "hw/boards.h" | |
10ec5117 | 42 | #endif |
d5a43964 | 43 | |
defb0e31 AG |
44 | /* #define DEBUG_HELPER */ |
45 | #ifdef DEBUG_HELPER | |
46 | #define HELPER_LOG(x...) qemu_log(x) | |
47 | #else | |
48 | #define HELPER_LOG(x...) | |
49 | #endif | |
50 | ||
d5a103cd | 51 | /* Raise an exception statically from a TB. */ |
089f5c06 | 52 | void HELPER(exception)(CPUS390XState *env, uint32_t excp) |
defb0e31 | 53 | { |
27103424 AF |
54 | CPUState *cs = CPU(s390_env_get_cpu(env)); |
55 | ||
71e47088 | 56 | HELPER_LOG("%s: exception %d\n", __func__, excp); |
27103424 | 57 | cs->exception_index = excp; |
5638d180 | 58 | cpu_loop_exit(cs); |
defb0e31 AG |
59 | } |
60 | ||
4bac52f5 DH |
61 | /* Store CPU Timer (also used for EXTRACT CPU TIME) */ |
62 | uint64_t HELPER(stpt)(CPUS390XState *env) | |
63 | { | |
64 | #if defined(CONFIG_USER_ONLY) | |
65 | /* | |
66 | * Fake a descending CPU timer. We could get negative values here, | |
67 | * but we don't care as it is up to the OS when to process that | |
68 | * interrupt and reset to > 0. | |
69 | */ | |
70 | return UINT64_MAX - (uint64_t)cpu_get_host_ticks(); | |
71 | #else | |
72 | return time2tod(env->cputm - qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL)); | |
73 | #endif | |
74 | } | |
75 | ||
31006af3 AJ |
76 | #ifndef CONFIG_USER_ONLY |
77 | ||
defb0e31 | 78 | /* SCLP service call */ |
dc458df9 | 79 | uint32_t HELPER(servc)(CPUS390XState *env, uint64_t r1, uint64_t r2) |
defb0e31 | 80 | { |
8d04fb55 | 81 | qemu_mutex_lock_iothread(); |
6e252802 | 82 | int r = sclp_service_call(env, r1, r2); |
1a38921a | 83 | qemu_mutex_unlock_iothread(); |
9abf567d | 84 | if (r < 0) { |
1a38921a | 85 | s390_program_interrupt(env, -r, 4, GETPC()); |
9abf567d CB |
86 | } |
87 | return r; | |
defb0e31 AG |
88 | } |
89 | ||
8df7eef3 | 90 | void HELPER(diag)(CPUS390XState *env, uint32_t r1, uint32_t r3, uint32_t num) |
defb0e31 AG |
91 | { |
92 | uint64_t r; | |
93 | ||
94 | switch (num) { | |
95 | case 0x500: | |
96 | /* KVM hypercall */ | |
2cf9953b | 97 | qemu_mutex_lock_iothread(); |
28e942f8 | 98 | r = s390_virtio_hypercall(env); |
2cf9953b | 99 | qemu_mutex_unlock_iothread(); |
defb0e31 AG |
100 | break; |
101 | case 0x44: | |
102 | /* yield */ | |
103 | r = 0; | |
104 | break; | |
105 | case 0x308: | |
106 | /* ipl */ | |
7337c6eb | 107 | qemu_mutex_lock_iothread(); |
968db419 | 108 | handle_diag_308(env, r1, r3, GETPC()); |
7337c6eb | 109 | qemu_mutex_unlock_iothread(); |
defb0e31 AG |
110 | r = 0; |
111 | break; | |
eb569af8 CH |
112 | case 0x288: |
113 | /* time bomb (watchdog) */ | |
114 | r = handle_diag_288(env, r1, r3); | |
115 | break; | |
defb0e31 AG |
116 | default: |
117 | r = -1; | |
118 | break; | |
119 | } | |
120 | ||
121 | if (r) { | |
277b156d | 122 | s390_program_interrupt(env, PGM_SPECIFICATION, ILEN_AUTO, GETPC()); |
defb0e31 | 123 | } |
defb0e31 AG |
124 | } |
125 | ||
defb0e31 | 126 | /* Set Prefix */ |
089f5c06 | 127 | void HELPER(spx)(CPUS390XState *env, uint64_t a1) |
defb0e31 | 128 | { |
31b030d4 | 129 | CPUState *cs = CPU(s390_env_get_cpu(env)); |
e805a0d3 | 130 | uint32_t prefix = a1 & 0x7fffe000; |
31b030d4 | 131 | |
e805a0d3 | 132 | env->psa = prefix; |
aafcf80e | 133 | HELPER_LOG("prefix: %#x\n", prefix); |
31b030d4 AF |
134 | tlb_flush_page(cs, 0); |
135 | tlb_flush_page(cs, TARGET_PAGE_SIZE); | |
defb0e31 AG |
136 | } |
137 | ||
d9d55f11 AJ |
138 | /* Store Clock */ |
139 | uint64_t HELPER(stck)(CPUS390XState *env) | |
defb0e31 AG |
140 | { |
141 | uint64_t time; | |
142 | ||
143 | time = env->tod_offset + | |
bc72ad67 | 144 | time2tod(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) - env->tod_basetime); |
defb0e31 AG |
145 | |
146 | return time; | |
147 | } | |
148 | ||
defb0e31 | 149 | /* Set Clock Comparator */ |
dd3eb7b5 | 150 | void HELPER(sckc)(CPUS390XState *env, uint64_t time) |
defb0e31 | 151 | { |
defb0e31 AG |
152 | if (time == -1ULL) { |
153 | return; | |
154 | } | |
155 | ||
aa9e14e6 AJ |
156 | env->ckc = time; |
157 | ||
c941f074 AJ |
158 | /* difference between origins */ |
159 | time -= env->tod_offset; | |
160 | ||
defb0e31 | 161 | /* nanoseconds */ |
9cb32c44 | 162 | time = tod2time(time); |
defb0e31 | 163 | |
c941f074 | 164 | timer_mod(env->tod_timer, env->tod_basetime + time); |
defb0e31 AG |
165 | } |
166 | ||
257a119e DH |
167 | /* Set Tod Programmable Field */ |
168 | void HELPER(sckpf)(CPUS390XState *env, uint64_t r0) | |
169 | { | |
170 | uint32_t val = r0; | |
171 | ||
172 | if (val & 0xffff0000) { | |
173 | s390_program_interrupt(env, PGM_SPECIFICATION, 2, GETPC()); | |
174 | } | |
175 | env->todpr = val; | |
176 | } | |
177 | ||
defb0e31 | 178 | /* Store Clock Comparator */ |
dd3eb7b5 | 179 | uint64_t HELPER(stckc)(CPUS390XState *env) |
defb0e31 | 180 | { |
aa9e14e6 | 181 | return env->ckc; |
defb0e31 AG |
182 | } |
183 | ||
184 | /* Set CPU Timer */ | |
c4f0a863 | 185 | void HELPER(spt)(CPUS390XState *env, uint64_t time) |
defb0e31 | 186 | { |
defb0e31 AG |
187 | if (time == -1ULL) { |
188 | return; | |
189 | } | |
190 | ||
191 | /* nanoseconds */ | |
9cb32c44 | 192 | time = tod2time(time); |
defb0e31 | 193 | |
b8ae94bd AJ |
194 | env->cputm = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + time; |
195 | ||
196 | timer_mod(env->cpu_timer, env->cputm); | |
defb0e31 AG |
197 | } |
198 | ||
defb0e31 | 199 | /* Store System Information */ |
79947862 | 200 | uint32_t HELPER(stsi)(CPUS390XState *env, uint64_t a0, uint64_t r0, uint64_t r1) |
defb0e31 | 201 | { |
79947862 DH |
202 | const uintptr_t ra = GETPC(); |
203 | const uint32_t sel1 = r0 & STSI_R0_SEL1_MASK; | |
204 | const uint32_t sel2 = r1 & STSI_R1_SEL2_MASK; | |
205 | const MachineState *ms = MACHINE(qdev_get_machine()); | |
206 | uint16_t total_cpus = 0, conf_cpus = 0, reserved_cpus = 0; | |
076d4d39 | 207 | S390CPU *cpu = s390_env_get_cpu(env); |
79947862 DH |
208 | SysIB sysib = { 0 }; |
209 | int i, cc = 0; | |
defb0e31 | 210 | |
79947862 DH |
211 | if ((r0 & STSI_R0_FC_MASK) > STSI_R0_FC_LEVEL_3) { |
212 | /* invalid function code: no other checks are performed */ | |
213 | return 3; | |
defb0e31 AG |
214 | } |
215 | ||
79947862 DH |
216 | if ((r0 & STSI_R0_RESERVED_MASK) || (r1 & STSI_R1_RESERVED_MASK)) { |
217 | s390_program_interrupt(env, PGM_SPECIFICATION, 4, ra); | |
218 | } | |
219 | ||
220 | if ((r0 & STSI_R0_FC_MASK) == STSI_R0_FC_CURRENT) { | |
221 | /* query the current level: no further checks are performed */ | |
222 | env->regs[0] = STSI_R0_FC_LEVEL_3; | |
223 | return 0; | |
224 | } | |
defb0e31 | 225 | |
79947862 DH |
226 | if (a0 & ~TARGET_PAGE_MASK) { |
227 | s390_program_interrupt(env, PGM_SPECIFICATION, 4, ra); | |
228 | } | |
229 | ||
230 | /* count the cpus and split them into configured and reserved ones */ | |
231 | for (i = 0; i < ms->possible_cpus->len; i++) { | |
232 | total_cpus++; | |
233 | if (ms->possible_cpus->cpus[i].cpu) { | |
234 | conf_cpus++; | |
235 | } else { | |
236 | reserved_cpus++; | |
237 | } | |
238 | } | |
defb0e31 | 239 | |
79947862 DH |
240 | /* |
241 | * In theory, we could report Level 1 / Level 2 as current. However, | |
242 | * the Linux kernel will detect this as running under LPAR and assume | |
243 | * that we have a sclp linemode console (which is always present on | |
244 | * LPAR, but not the default for QEMU), therefore not displaying boot | |
245 | * messages and making booting a Linux kernel under TCG harder. | |
246 | * | |
247 | * For now we fake the same SMP configuration on all levels. | |
248 | * | |
249 | * TODO: We could later make the level configurable via the machine | |
250 | * and change defaults (linemode console) based on machine type | |
251 | * and accelerator. | |
252 | */ | |
253 | switch (r0 & STSI_R0_FC_MASK) { | |
254 | case STSI_R0_FC_LEVEL_1: | |
defb0e31 AG |
255 | if ((sel1 == 1) && (sel2 == 1)) { |
256 | /* Basic Machine Configuration */ | |
076d4d39 | 257 | char type[5] = {}; |
defb0e31 | 258 | |
79947862 | 259 | ebcdic_put(sysib.sysib_111.manuf, "QEMU ", 16); |
076d4d39 DH |
260 | /* same as machine type number in STORE CPU ID, but in EBCDIC */ |
261 | snprintf(type, ARRAY_SIZE(type), "%X", cpu->model->def->type); | |
79947862 | 262 | ebcdic_put(sysib.sysib_111.type, type, 4); |
076d4d39 | 263 | /* model number (not stored in STORE CPU ID for z/Architecure) */ |
79947862 DH |
264 | ebcdic_put(sysib.sysib_111.model, "QEMU ", 16); |
265 | ebcdic_put(sysib.sysib_111.sequence, "QEMU ", 16); | |
266 | ebcdic_put(sysib.sysib_111.plant, "QEMU", 4); | |
defb0e31 AG |
267 | } else if ((sel1 == 2) && (sel2 == 1)) { |
268 | /* Basic Machine CPU */ | |
79947862 DH |
269 | ebcdic_put(sysib.sysib_121.sequence, "QEMUQEMUQEMUQEMU", 16); |
270 | ebcdic_put(sysib.sysib_121.plant, "QEMU", 4); | |
271 | sysib.sysib_121.cpu_addr = cpu_to_be16(env->core_id); | |
defb0e31 AG |
272 | } else if ((sel1 == 2) && (sel2 == 2)) { |
273 | /* Basic Machine CPUs */ | |
79947862 DH |
274 | sysib.sysib_122.capability = cpu_to_be32(0x443afc29); |
275 | sysib.sysib_122.total_cpus = cpu_to_be16(total_cpus); | |
276 | sysib.sysib_122.conf_cpus = cpu_to_be16(conf_cpus); | |
277 | sysib.sysib_122.reserved_cpus = cpu_to_be16(reserved_cpus); | |
defb0e31 AG |
278 | } else { |
279 | cc = 3; | |
280 | } | |
281 | break; | |
79947862 DH |
282 | case STSI_R0_FC_LEVEL_2: |
283 | if ((sel1 == 2) && (sel2 == 1)) { | |
284 | /* LPAR CPU */ | |
285 | ebcdic_put(sysib.sysib_221.sequence, "QEMUQEMUQEMUQEMU", 16); | |
286 | ebcdic_put(sysib.sysib_221.plant, "QEMU", 4); | |
287 | sysib.sysib_221.cpu_addr = cpu_to_be16(env->core_id); | |
288 | } else if ((sel1 == 2) && (sel2 == 2)) { | |
289 | /* LPAR CPUs */ | |
290 | sysib.sysib_222.lcpuc = 0x80; /* dedicated */ | |
291 | sysib.sysib_222.total_cpus = cpu_to_be16(total_cpus); | |
292 | sysib.sysib_222.conf_cpus = cpu_to_be16(conf_cpus); | |
293 | sysib.sysib_222.reserved_cpus = cpu_to_be16(reserved_cpus); | |
294 | ebcdic_put(sysib.sysib_222.name, "QEMU ", 8); | |
295 | sysib.sysib_222.caf = cpu_to_be32(1000); | |
296 | sysib.sysib_222.dedicated_cpus = cpu_to_be16(conf_cpus); | |
297 | } else { | |
298 | cc = 3; | |
defb0e31 | 299 | } |
79947862 DH |
300 | break; |
301 | case STSI_R0_FC_LEVEL_3: | |
302 | if ((sel1 == 2) && (sel2 == 2)) { | |
303 | /* VM CPUs */ | |
304 | sysib.sysib_322.count = 1; | |
305 | sysib.sysib_322.vm[0].total_cpus = cpu_to_be16(total_cpus); | |
306 | sysib.sysib_322.vm[0].conf_cpus = cpu_to_be16(conf_cpus); | |
307 | sysib.sysib_322.vm[0].reserved_cpus = cpu_to_be16(reserved_cpus); | |
308 | sysib.sysib_322.vm[0].caf = cpu_to_be32(1000); | |
309 | /* Linux kernel uses this to distinguish us from z/VM */ | |
310 | ebcdic_put(sysib.sysib_322.vm[0].cpi, "KVM/Linux ", 16); | |
311 | sysib.sysib_322.vm[0].ext_name_encoding = 2; /* UTF-8 */ | |
312 | ||
313 | /* If our VM has a name, use the real name */ | |
314 | if (qemu_name) { | |
315 | memset(sysib.sysib_322.vm[0].name, 0x40, | |
316 | sizeof(sysib.sysib_322.vm[0].name)); | |
317 | ebcdic_put(sysib.sysib_322.vm[0].name, qemu_name, | |
318 | MIN(sizeof(sysib.sysib_322.vm[0].name), | |
319 | strlen(qemu_name))); | |
320 | strncpy((char *)sysib.sysib_322.ext_names[0], qemu_name, | |
321 | sizeof(sysib.sysib_322.ext_names[0])); | |
71e47088 | 322 | } else { |
79947862 DH |
323 | ebcdic_put(sysib.sysib_322.vm[0].name, "TCGguest", 8); |
324 | strcpy((char *)sysib.sysib_322.ext_names[0], "TCGguest"); | |
71e47088 | 325 | } |
79947862 DH |
326 | |
327 | /* add the uuid */ | |
328 | memcpy(sysib.sysib_322.vm[0].uuid, &qemu_uuid, | |
329 | sizeof(sysib.sysib_322.vm[0].uuid)); | |
330 | } else { | |
331 | cc = 3; | |
defb0e31 | 332 | } |
defb0e31 AG |
333 | break; |
334 | } | |
335 | ||
79947862 DH |
336 | if (cc == 0) { |
337 | if (s390_cpu_virt_mem_write(cpu, a0, 0, &sysib, sizeof(sysib))) { | |
338 | s390_cpu_virt_mem_handle_exc(cpu, ra); | |
339 | } | |
340 | } | |
341 | ||
defb0e31 AG |
342 | return cc; |
343 | } | |
344 | ||
089f5c06 | 345 | uint32_t HELPER(sigp)(CPUS390XState *env, uint64_t order_code, uint32_t r1, |
11b0079c | 346 | uint32_t r3) |
defb0e31 | 347 | { |
11b0079c | 348 | int cc; |
defb0e31 | 349 | |
11b0079c DH |
350 | /* TODO: needed to inject interrupts - push further down */ |
351 | qemu_mutex_lock_iothread(); | |
352 | cc = handle_sigp(env, order_code & SIGP_ORDER_MASK, r1, r3); | |
353 | qemu_mutex_unlock_iothread(); | |
defb0e31 AG |
354 | |
355 | return cc; | |
356 | } | |
defb0e31 | 357 | #endif |
ad8a4570 AG |
358 | |
359 | #ifndef CONFIG_USER_ONLY | |
360 | void HELPER(xsch)(CPUS390XState *env, uint64_t r1) | |
361 | { | |
362 | S390CPU *cpu = s390_env_get_cpu(env); | |
278f5e98 | 363 | qemu_mutex_lock_iothread(); |
1b98fb99 | 364 | ioinst_handle_xsch(cpu, r1, GETPC()); |
278f5e98 | 365 | qemu_mutex_unlock_iothread(); |
ad8a4570 AG |
366 | } |
367 | ||
368 | void HELPER(csch)(CPUS390XState *env, uint64_t r1) | |
369 | { | |
370 | S390CPU *cpu = s390_env_get_cpu(env); | |
278f5e98 | 371 | qemu_mutex_lock_iothread(); |
1b98fb99 | 372 | ioinst_handle_csch(cpu, r1, GETPC()); |
278f5e98 | 373 | qemu_mutex_unlock_iothread(); |
ad8a4570 AG |
374 | } |
375 | ||
376 | void HELPER(hsch)(CPUS390XState *env, uint64_t r1) | |
377 | { | |
378 | S390CPU *cpu = s390_env_get_cpu(env); | |
278f5e98 | 379 | qemu_mutex_lock_iothread(); |
1b98fb99 | 380 | ioinst_handle_hsch(cpu, r1, GETPC()); |
278f5e98 | 381 | qemu_mutex_unlock_iothread(); |
ad8a4570 AG |
382 | } |
383 | ||
384 | void HELPER(msch)(CPUS390XState *env, uint64_t r1, uint64_t inst) | |
385 | { | |
386 | S390CPU *cpu = s390_env_get_cpu(env); | |
278f5e98 | 387 | qemu_mutex_lock_iothread(); |
1b98fb99 | 388 | ioinst_handle_msch(cpu, r1, inst >> 16, GETPC()); |
278f5e98 | 389 | qemu_mutex_unlock_iothread(); |
ad8a4570 AG |
390 | } |
391 | ||
392 | void HELPER(rchp)(CPUS390XState *env, uint64_t r1) | |
393 | { | |
394 | S390CPU *cpu = s390_env_get_cpu(env); | |
278f5e98 | 395 | qemu_mutex_lock_iothread(); |
1b98fb99 | 396 | ioinst_handle_rchp(cpu, r1, GETPC()); |
278f5e98 | 397 | qemu_mutex_unlock_iothread(); |
ad8a4570 AG |
398 | } |
399 | ||
400 | void HELPER(rsch)(CPUS390XState *env, uint64_t r1) | |
401 | { | |
402 | S390CPU *cpu = s390_env_get_cpu(env); | |
278f5e98 | 403 | qemu_mutex_lock_iothread(); |
1b98fb99 | 404 | ioinst_handle_rsch(cpu, r1, GETPC()); |
278f5e98 | 405 | qemu_mutex_unlock_iothread(); |
ad8a4570 AG |
406 | } |
407 | ||
86c34633 DH |
408 | void HELPER(sal)(CPUS390XState *env, uint64_t r1) |
409 | { | |
410 | S390CPU *cpu = s390_env_get_cpu(env); | |
411 | ||
412 | qemu_mutex_lock_iothread(); | |
413 | ioinst_handle_sal(cpu, r1, GETPC()); | |
414 | qemu_mutex_unlock_iothread(); | |
415 | } | |
416 | ||
a9de75a0 DH |
417 | void HELPER(schm)(CPUS390XState *env, uint64_t r1, uint64_t r2, uint64_t inst) |
418 | { | |
419 | S390CPU *cpu = s390_env_get_cpu(env); | |
420 | ||
421 | qemu_mutex_lock_iothread(); | |
422 | ioinst_handle_schm(cpu, r1, r2, inst >> 16, GETPC()); | |
423 | qemu_mutex_unlock_iothread(); | |
424 | } | |
425 | ||
ad8a4570 AG |
426 | void HELPER(ssch)(CPUS390XState *env, uint64_t r1, uint64_t inst) |
427 | { | |
428 | S390CPU *cpu = s390_env_get_cpu(env); | |
278f5e98 | 429 | qemu_mutex_lock_iothread(); |
1b98fb99 | 430 | ioinst_handle_ssch(cpu, r1, inst >> 16, GETPC()); |
278f5e98 | 431 | qemu_mutex_unlock_iothread(); |
5a59bc1d DH |
432 | } |
433 | ||
434 | void HELPER(stcrw)(CPUS390XState *env, uint64_t inst) | |
435 | { | |
436 | S390CPU *cpu = s390_env_get_cpu(env); | |
437 | ||
438 | qemu_mutex_lock_iothread(); | |
439 | ioinst_handle_stcrw(cpu, inst >> 16, GETPC()); | |
440 | qemu_mutex_unlock_iothread(); | |
ad8a4570 AG |
441 | } |
442 | ||
443 | void HELPER(stsch)(CPUS390XState *env, uint64_t r1, uint64_t inst) | |
444 | { | |
445 | S390CPU *cpu = s390_env_get_cpu(env); | |
278f5e98 | 446 | qemu_mutex_lock_iothread(); |
1b98fb99 | 447 | ioinst_handle_stsch(cpu, r1, inst >> 16, GETPC()); |
278f5e98 | 448 | qemu_mutex_unlock_iothread(); |
ad8a4570 AG |
449 | } |
450 | ||
6a253de3 DH |
451 | uint32_t HELPER(tpi)(CPUS390XState *env, uint64_t addr) |
452 | { | |
453 | const uintptr_t ra = GETPC(); | |
454 | S390CPU *cpu = s390_env_get_cpu(env); | |
455 | QEMUS390FLICState *flic = QEMU_S390_FLIC(s390_get_flic()); | |
456 | QEMUS390FlicIO *io = NULL; | |
457 | LowCore *lowcore; | |
458 | ||
459 | if (addr & 0x3) { | |
460 | s390_program_interrupt(env, PGM_SPECIFICATION, 4, ra); | |
461 | } | |
462 | ||
463 | qemu_mutex_lock_iothread(); | |
464 | io = qemu_s390_flic_dequeue_io(flic, env->cregs[6]); | |
465 | if (!io) { | |
466 | qemu_mutex_unlock_iothread(); | |
467 | return 0; | |
468 | } | |
469 | ||
470 | if (addr) { | |
471 | struct { | |
472 | uint16_t id; | |
473 | uint16_t nr; | |
474 | uint32_t parm; | |
475 | } intc = { | |
476 | .id = cpu_to_be16(io->id), | |
477 | .nr = cpu_to_be16(io->nr), | |
478 | .parm = cpu_to_be32(io->parm), | |
479 | }; | |
480 | ||
481 | if (s390_cpu_virt_mem_write(cpu, addr, 0, &intc, sizeof(intc))) { | |
482 | /* writing failed, reinject and properly clean up */ | |
483 | s390_io_interrupt(io->id, io->nr, io->parm, io->word); | |
484 | qemu_mutex_unlock_iothread(); | |
485 | g_free(io); | |
486 | s390_cpu_virt_mem_handle_exc(cpu, ra); | |
487 | return 0; | |
488 | } | |
489 | } else { | |
490 | /* no protection applies */ | |
491 | lowcore = cpu_map_lowcore(env); | |
492 | lowcore->subchannel_id = cpu_to_be16(io->id); | |
493 | lowcore->subchannel_nr = cpu_to_be16(io->nr); | |
494 | lowcore->io_int_parm = cpu_to_be32(io->parm); | |
495 | lowcore->io_int_word = cpu_to_be32(io->word); | |
496 | cpu_unmap_lowcore(lowcore); | |
497 | } | |
498 | ||
499 | g_free(io); | |
500 | qemu_mutex_unlock_iothread(); | |
501 | return 1; | |
502 | } | |
503 | ||
ad8a4570 AG |
504 | void HELPER(tsch)(CPUS390XState *env, uint64_t r1, uint64_t inst) |
505 | { | |
506 | S390CPU *cpu = s390_env_get_cpu(env); | |
278f5e98 | 507 | qemu_mutex_lock_iothread(); |
1b98fb99 | 508 | ioinst_handle_tsch(cpu, r1, inst >> 16, GETPC()); |
278f5e98 | 509 | qemu_mutex_unlock_iothread(); |
ad8a4570 AG |
510 | } |
511 | ||
512 | void HELPER(chsc)(CPUS390XState *env, uint64_t inst) | |
513 | { | |
514 | S390CPU *cpu = s390_env_get_cpu(env); | |
278f5e98 | 515 | qemu_mutex_lock_iothread(); |
1b98fb99 | 516 | ioinst_handle_chsc(cpu, inst >> 16, GETPC()); |
278f5e98 | 517 | qemu_mutex_unlock_iothread(); |
ad8a4570 AG |
518 | } |
519 | #endif | |
777c98c3 AJ |
520 | |
521 | #ifndef CONFIG_USER_ONLY | |
522 | void HELPER(per_check_exception)(CPUS390XState *env) | |
523 | { | |
e0b1a8a1 | 524 | uint32_t ilen; |
777c98c3 AJ |
525 | |
526 | if (env->per_perc_atmid) { | |
e0b1a8a1 DH |
527 | /* |
528 | * FIXME: ILEN_AUTO is most probably the right thing to use. ilen | |
529 | * always has to match the instruction referenced in the PSW. E.g. | |
530 | * if a PER interrupt is triggered via EXECUTE, we have to use ilen | |
531 | * of EXECUTE, while per_address contains the target of EXECUTE. | |
532 | */ | |
533 | ilen = get_ilen(cpu_ldub_code(env, env->per_address)); | |
88083382 | 534 | s390_program_interrupt(env, PGM_PER, ilen, GETPC()); |
777c98c3 AJ |
535 | } |
536 | } | |
2c2275eb | 537 | |
d9b8daf9 DH |
538 | /* Check if an address is within the PER starting address and the PER |
539 | ending address. The address range might loop. */ | |
540 | static inline bool get_per_in_range(CPUS390XState *env, uint64_t addr) | |
541 | { | |
542 | if (env->cregs[10] <= env->cregs[11]) { | |
543 | return env->cregs[10] <= addr && addr <= env->cregs[11]; | |
544 | } else { | |
545 | return env->cregs[10] <= addr || addr <= env->cregs[11]; | |
546 | } | |
547 | } | |
548 | ||
2c2275eb AJ |
549 | void HELPER(per_branch)(CPUS390XState *env, uint64_t from, uint64_t to) |
550 | { | |
551 | if ((env->cregs[9] & PER_CR9_EVENT_BRANCH)) { | |
552 | if (!(env->cregs[9] & PER_CR9_CONTROL_BRANCH_ADDRESS) | |
553 | || get_per_in_range(env, to)) { | |
554 | env->per_address = from; | |
555 | env->per_perc_atmid = PER_CODE_EVENT_BRANCH | get_per_atmid(env); | |
556 | } | |
557 | } | |
558 | } | |
f0e0d817 AJ |
559 | |
560 | void HELPER(per_ifetch)(CPUS390XState *env, uint64_t addr) | |
561 | { | |
562 | if ((env->cregs[9] & PER_CR9_EVENT_IFETCH) && get_per_in_range(env, addr)) { | |
563 | env->per_address = addr; | |
564 | env->per_perc_atmid = PER_CODE_EVENT_IFETCH | get_per_atmid(env); | |
83bb1612 AJ |
565 | |
566 | /* If the instruction has to be nullified, trigger the | |
567 | exception immediately. */ | |
568 | if (env->cregs[9] & PER_CR9_EVENT_NULLIFICATION) { | |
569 | CPUState *cs = CPU(s390_env_get_cpu(env)); | |
570 | ||
465aec46 | 571 | env->per_perc_atmid |= PER_CODE_EVENT_NULLIFICATION; |
83bb1612 AJ |
572 | env->int_pgm_code = PGM_PER; |
573 | env->int_pgm_ilen = get_ilen(cpu_ldub_code(env, addr)); | |
574 | ||
575 | cs->exception_index = EXCP_PGM; | |
576 | cpu_loop_exit(cs); | |
577 | } | |
f0e0d817 AJ |
578 | } |
579 | } | |
777c98c3 | 580 | #endif |
5bf83628 | 581 | |
f74990a5 DH |
582 | static uint8_t stfl_bytes[2048]; |
583 | static unsigned int used_stfl_bytes; | |
5bf83628 | 584 | |
f74990a5 | 585 | static void prepare_stfl(void) |
5bf83628 | 586 | { |
f74990a5 DH |
587 | static bool initialized; |
588 | int i; | |
5bf83628 | 589 | |
f74990a5 DH |
590 | /* racy, but we don't care, the same values are always written */ |
591 | if (initialized) { | |
592 | return; | |
5bf83628 RH |
593 | } |
594 | ||
f74990a5 DH |
595 | s390_get_feat_block(S390_FEAT_TYPE_STFL, stfl_bytes); |
596 | for (i = 0; i < sizeof(stfl_bytes); i++) { | |
597 | if (stfl_bytes[i]) { | |
598 | used_stfl_bytes = i + 1; | |
5bf83628 RH |
599 | } |
600 | } | |
f74990a5 | 601 | initialized = true; |
5bf83628 RH |
602 | } |
603 | ||
86b5ab39 | 604 | #ifndef CONFIG_USER_ONLY |
5bf83628 RH |
605 | void HELPER(stfl)(CPUS390XState *env) |
606 | { | |
86b5ab39 | 607 | LowCore *lowcore; |
5bf83628 | 608 | |
86b5ab39 | 609 | lowcore = cpu_map_lowcore(env); |
f74990a5 DH |
610 | prepare_stfl(); |
611 | memcpy(&lowcore->stfl_fac_list, stfl_bytes, sizeof(lowcore->stfl_fac_list)); | |
86b5ab39 | 612 | cpu_unmap_lowcore(lowcore); |
5bf83628 | 613 | } |
86b5ab39 | 614 | #endif |
5bf83628 RH |
615 | |
616 | uint32_t HELPER(stfle)(CPUS390XState *env, uint64_t addr) | |
617 | { | |
f74990a5 DH |
618 | const uintptr_t ra = GETPC(); |
619 | const int count_bytes = ((env->regs[0] & 0xff) + 1) * 8; | |
620 | const int max_bytes = ROUND_UP(used_stfl_bytes, 8); | |
621 | int i; | |
622 | ||
623 | if (addr & 0x7) { | |
8d2f850a | 624 | s390_program_interrupt(env, PGM_SPECIFICATION, 4, ra); |
f74990a5 | 625 | } |
5bf83628 | 626 | |
f74990a5 DH |
627 | prepare_stfl(); |
628 | for (i = 0; i < count_bytes; ++i) { | |
629 | cpu_stb_data_ra(env, addr + i, stfl_bytes[i], ra); | |
5bf83628 RH |
630 | } |
631 | ||
f74990a5 DH |
632 | env->regs[0] = deposit64(env->regs[0], 0, 8, (max_bytes / 8) - 1); |
633 | return count_bytes >= max_bytes ? 0 : 3; | |
5bf83628 | 634 | } |