| 1 | /* |
| 2 | * S/390 misc helper routines |
| 3 | * |
| 4 | * Copyright (c) 2009 Ulrich Hecht |
| 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.1 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 |
| 18 | * License along with this library; if not, see <http://www.gnu.org/licenses/>. |
| 19 | */ |
| 20 | |
| 21 | #include "qemu/osdep.h" |
| 22 | #include "qemu/cutils.h" |
| 23 | #include "qemu/log.h" |
| 24 | #include "cpu.h" |
| 25 | #include "s390x-internal.h" |
| 26 | #include "qemu/host-utils.h" |
| 27 | #include "exec/helper-proto.h" |
| 28 | #include "qemu/timer.h" |
| 29 | #include "exec/cputlb.h" |
| 30 | #include "accel/tcg/cpu-ldst-common.h" |
| 31 | #include "accel/tcg/cpu-loop.h" |
| 32 | #include "accel/tcg/cpu-mmu-index.h" |
| 33 | #include "exec/target_page.h" |
| 34 | #include "qapi/error.h" |
| 35 | #include "tcg_s390x.h" |
| 36 | #include "s390-tod.h" |
| 37 | |
| 38 | #if !defined(CONFIG_USER_ONLY) |
| 39 | #include "system/cpus.h" |
| 40 | #include "system/system.h" |
| 41 | #include "hw/s390x/ebcdic.h" |
| 42 | #include "hw/s390x/s390-hypercall.h" |
| 43 | #include "hw/s390x/sclp.h" |
| 44 | #include "hw/s390x/s390_flic.h" |
| 45 | #include "hw/s390x/ioinst.h" |
| 46 | #include "hw/s390x/s390-pci-inst.h" |
| 47 | #include "hw/core/boards.h" |
| 48 | #include "hw/s390x/tod.h" |
| 49 | #include CONFIG_DEVICES |
| 50 | #endif |
| 51 | |
| 52 | /* #define DEBUG_HELPER */ |
| 53 | #ifdef DEBUG_HELPER |
| 54 | #define HELPER_LOG(x...) qemu_log(x) |
| 55 | #else |
| 56 | #define HELPER_LOG(x...) |
| 57 | #endif |
| 58 | |
| 59 | /* Raise an exception statically from a TB. */ |
| 60 | void HELPER(exception)(CPUS390XState *env, uint32_t excp) |
| 61 | { |
| 62 | CPUState *cs = env_cpu(env); |
| 63 | |
| 64 | HELPER_LOG("%s: exception %d\n", __func__, excp); |
| 65 | cs->exception_index = excp; |
| 66 | cpu_loop_exit(cs); |
| 67 | } |
| 68 | |
| 69 | /* Store CPU Timer (also used for EXTRACT CPU TIME) */ |
| 70 | uint64_t HELPER(stpt)(CPUS390XState *env) |
| 71 | { |
| 72 | #if defined(CONFIG_USER_ONLY) |
| 73 | /* |
| 74 | * Fake a descending CPU timer. We could get negative values here, |
| 75 | * but we don't care as it is up to the OS when to process that |
| 76 | * interrupt and reset to > 0. |
| 77 | */ |
| 78 | return UINT64_MAX - (uint64_t)cpu_get_host_ticks(); |
| 79 | #else |
| 80 | return time2tod(env->cputm - qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL)); |
| 81 | #endif |
| 82 | } |
| 83 | |
| 84 | /* Store Clock */ |
| 85 | uint64_t HELPER(stck)(CPUS390XState *env) |
| 86 | { |
| 87 | #ifdef CONFIG_USER_ONLY |
| 88 | struct timespec ts; |
| 89 | uint64_t ns; |
| 90 | |
| 91 | clock_gettime(CLOCK_REALTIME, &ts); |
| 92 | ns = ts.tv_sec * NANOSECONDS_PER_SECOND + ts.tv_nsec; |
| 93 | |
| 94 | return TOD_UNIX_EPOCH + time2tod(ns); |
| 95 | #else |
| 96 | S390TODState *td = s390_get_todstate(); |
| 97 | S390TODClass *tdc = S390_TOD_GET_CLASS(td); |
| 98 | S390TOD tod; |
| 99 | |
| 100 | tdc->get(td, &tod, &error_abort); |
| 101 | return tod.low; |
| 102 | #endif |
| 103 | } |
| 104 | |
| 105 | #ifndef CONFIG_USER_ONLY |
| 106 | /* SCLP service call */ |
| 107 | uint32_t HELPER(servc)(CPUS390XState *env, uint64_t r1, uint64_t r2) |
| 108 | { |
| 109 | bql_lock(); |
| 110 | int r = sclp_service_call(env_archcpu(env), r1, r2); |
| 111 | bql_unlock(); |
| 112 | if (r < 0) { |
| 113 | tcg_s390_program_interrupt(env, -r, GETPC()); |
| 114 | } |
| 115 | return r; |
| 116 | } |
| 117 | |
| 118 | void HELPER(diag)(CPUS390XState *env, uint32_t r1, uint32_t r3, uint32_t num) |
| 119 | { |
| 120 | uint64_t r; |
| 121 | |
| 122 | switch (num) { |
| 123 | #ifdef CONFIG_S390_CCW_VIRTIO |
| 124 | case 0x500: |
| 125 | /* QEMU/KVM hypercall */ |
| 126 | bql_lock(); |
| 127 | handle_diag_500(env_archcpu(env), GETPC()); |
| 128 | bql_unlock(); |
| 129 | r = 0; |
| 130 | break; |
| 131 | #endif /* CONFIG_S390_CCW_VIRTIO */ |
| 132 | case 0x44: |
| 133 | /* yield */ |
| 134 | r = 0; |
| 135 | break; |
| 136 | case 0x308: |
| 137 | /* ipl */ |
| 138 | bql_lock(); |
| 139 | if (handle_diag_308(env, r1, r3, GETPC())) { |
| 140 | /* As reset is triggered by the CPU, make sure to exit the loop */ |
| 141 | cpu_loop_exit(CPU(env_archcpu(env))); |
| 142 | } |
| 143 | bql_unlock(); |
| 144 | r = 0; |
| 145 | break; |
| 146 | case 0x288: |
| 147 | /* time bomb (watchdog) */ |
| 148 | r = handle_diag_288(env, r1, r3); |
| 149 | break; |
| 150 | case 0x320: |
| 151 | /* cert store */ |
| 152 | bql_lock(); |
| 153 | handle_diag_320(env, r1, r3, GETPC()); |
| 154 | bql_unlock(); |
| 155 | r = 0; |
| 156 | break; |
| 157 | case 0x508: |
| 158 | /* secure ipl operations */ |
| 159 | bql_lock(); |
| 160 | handle_diag_508(env, r1, r3, GETPC()); |
| 161 | bql_unlock(); |
| 162 | r = 0; |
| 163 | break; |
| 164 | default: |
| 165 | r = -1; |
| 166 | break; |
| 167 | } |
| 168 | |
| 169 | if (r) { |
| 170 | tcg_s390_program_interrupt(env, PGM_SPECIFICATION, GETPC()); |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | /* Set Prefix */ |
| 175 | void HELPER(spx)(CPUS390XState *env, uint64_t a1) |
| 176 | { |
| 177 | const uint32_t prefix = a1 & 0x7fffe000; |
| 178 | const uint32_t old_prefix = env->psa; |
| 179 | CPUState *cs = env_cpu(env); |
| 180 | |
| 181 | if (prefix == old_prefix) { |
| 182 | return; |
| 183 | } |
| 184 | /* |
| 185 | * Since prefix got aligned to 8k and memory increments are a multiple of |
| 186 | * 8k checking the first page is sufficient |
| 187 | */ |
| 188 | if (!mmu_absolute_addr_valid(prefix, true)) { |
| 189 | tcg_s390_program_interrupt(env, PGM_ADDRESSING, GETPC()); |
| 190 | } |
| 191 | |
| 192 | env->psa = prefix; |
| 193 | HELPER_LOG("prefix: %#x\n", prefix); |
| 194 | tlb_flush_page(cs, 0); |
| 195 | tlb_flush_page(cs, TARGET_PAGE_SIZE); |
| 196 | if (prefix != 0) { |
| 197 | tlb_flush_page(cs, prefix); |
| 198 | tlb_flush_page(cs, prefix + TARGET_PAGE_SIZE); |
| 199 | } |
| 200 | if (old_prefix != 0) { |
| 201 | tlb_flush_page(cs, old_prefix); |
| 202 | tlb_flush_page(cs, old_prefix + TARGET_PAGE_SIZE); |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | static void update_ckc_timer(CPUS390XState *env) |
| 207 | { |
| 208 | S390TODState *td = s390_get_todstate(); |
| 209 | uint64_t time; |
| 210 | |
| 211 | /* stop the timer and remove pending CKC IRQs */ |
| 212 | timer_del(env->tod_timer); |
| 213 | g_assert(bql_locked()); |
| 214 | env->pending_int &= ~INTERRUPT_EXT_CLOCK_COMPARATOR; |
| 215 | |
| 216 | /* the tod has to exceed the ckc, this can never happen if ckc is all 1's */ |
| 217 | if (env->ckc == -1ULL) { |
| 218 | return; |
| 219 | } |
| 220 | |
| 221 | if (env->ckc < td->base.low) { |
| 222 | time = 0; |
| 223 | } else { |
| 224 | /* difference between origins */ |
| 225 | time = env->ckc - td->base.low; |
| 226 | |
| 227 | /* nanoseconds */ |
| 228 | time = tod2time(time); |
| 229 | } |
| 230 | |
| 231 | timer_mod(env->tod_timer, time); |
| 232 | } |
| 233 | |
| 234 | /* Set Clock Comparator */ |
| 235 | void HELPER(sckc)(CPUS390XState *env, uint64_t ckc) |
| 236 | { |
| 237 | env->ckc = ckc; |
| 238 | |
| 239 | bql_lock(); |
| 240 | update_ckc_timer(env); |
| 241 | bql_unlock(); |
| 242 | } |
| 243 | |
| 244 | void tcg_s390_tod_updated(CPUState *cs, run_on_cpu_data opaque) |
| 245 | { |
| 246 | update_ckc_timer(cpu_env(cs)); |
| 247 | } |
| 248 | |
| 249 | /* Set Clock */ |
| 250 | uint32_t HELPER(sck)(CPUS390XState *env, uint64_t tod_low) |
| 251 | { |
| 252 | S390TODState *td = s390_get_todstate(); |
| 253 | S390TODClass *tdc = S390_TOD_GET_CLASS(td); |
| 254 | S390TOD tod = { |
| 255 | .high = 0, |
| 256 | .low = tod_low, |
| 257 | }; |
| 258 | |
| 259 | bql_lock(); |
| 260 | tdc->set(td, &tod, &error_abort); |
| 261 | bql_unlock(); |
| 262 | return 0; |
| 263 | } |
| 264 | |
| 265 | /* Set Tod Programmable Field */ |
| 266 | void HELPER(sckpf)(CPUS390XState *env, uint64_t r0) |
| 267 | { |
| 268 | uint32_t val = r0; |
| 269 | |
| 270 | if (val & 0xffff0000) { |
| 271 | tcg_s390_program_interrupt(env, PGM_SPECIFICATION, GETPC()); |
| 272 | } |
| 273 | env->todpr = val; |
| 274 | } |
| 275 | |
| 276 | /* Store Clock Comparator */ |
| 277 | uint64_t HELPER(stckc)(CPUS390XState *env) |
| 278 | { |
| 279 | return env->ckc; |
| 280 | } |
| 281 | |
| 282 | /* Set CPU Timer */ |
| 283 | void HELPER(spt)(CPUS390XState *env, uint64_t time) |
| 284 | { |
| 285 | if (time == -1ULL) { |
| 286 | return; |
| 287 | } |
| 288 | |
| 289 | /* nanoseconds */ |
| 290 | time = tod2time(time); |
| 291 | |
| 292 | env->cputm = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + time; |
| 293 | |
| 294 | timer_mod(env->cpu_timer, env->cputm); |
| 295 | } |
| 296 | |
| 297 | /* Store System Information */ |
| 298 | uint32_t HELPER(stsi)(CPUS390XState *env, uint64_t a0, uint64_t r0, uint64_t r1) |
| 299 | { |
| 300 | const uintptr_t ra = GETPC(); |
| 301 | const uint32_t sel1 = r0 & STSI_R0_SEL1_MASK; |
| 302 | const uint32_t sel2 = r1 & STSI_R1_SEL2_MASK; |
| 303 | const MachineState *ms = MACHINE(qdev_get_machine()); |
| 304 | uint16_t total_cpus = 0, conf_cpus = 0, reserved_cpus = 0; |
| 305 | S390CPU *cpu = env_archcpu(env); |
| 306 | SysIB sysib = { }; |
| 307 | int i, cc = 0; |
| 308 | |
| 309 | if ((r0 & STSI_R0_FC_MASK) > STSI_R0_FC_LEVEL_3) { |
| 310 | /* invalid function code: no other checks are performed */ |
| 311 | return 3; |
| 312 | } |
| 313 | |
| 314 | if ((r0 & STSI_R0_RESERVED_MASK) || (r1 & STSI_R1_RESERVED_MASK)) { |
| 315 | tcg_s390_program_interrupt(env, PGM_SPECIFICATION, ra); |
| 316 | } |
| 317 | |
| 318 | if ((r0 & STSI_R0_FC_MASK) == STSI_R0_FC_CURRENT) { |
| 319 | /* query the current level: no further checks are performed */ |
| 320 | env->regs[0] = STSI_R0_FC_LEVEL_3; |
| 321 | return 0; |
| 322 | } |
| 323 | |
| 324 | if (a0 & ~TARGET_PAGE_MASK) { |
| 325 | tcg_s390_program_interrupt(env, PGM_SPECIFICATION, ra); |
| 326 | } |
| 327 | |
| 328 | /* count the cpus and split them into configured and reserved ones */ |
| 329 | for (i = 0; i < ms->possible_cpus->len; i++) { |
| 330 | total_cpus++; |
| 331 | if (ms->possible_cpus->cpus[i].cpu) { |
| 332 | conf_cpus++; |
| 333 | } else { |
| 334 | reserved_cpus++; |
| 335 | } |
| 336 | } |
| 337 | |
| 338 | /* |
| 339 | * In theory, we could report Level 1 / Level 2 as current. However, |
| 340 | * the Linux kernel will detect this as running under LPAR and assume |
| 341 | * that we have a sclp linemode console (which is always present on |
| 342 | * LPAR, but not the default for QEMU), therefore not displaying boot |
| 343 | * messages and making booting a Linux kernel under TCG harder. |
| 344 | * |
| 345 | * For now we fake the same SMP configuration on all levels. |
| 346 | * |
| 347 | * TODO: We could later make the level configurable via the machine |
| 348 | * and change defaults (linemode console) based on machine type |
| 349 | * and accelerator. |
| 350 | */ |
| 351 | switch (r0 & STSI_R0_FC_MASK) { |
| 352 | case STSI_R0_FC_LEVEL_1: |
| 353 | if ((sel1 == 1) && (sel2 == 1)) { |
| 354 | /* Basic Machine Configuration */ |
| 355 | char type[5] = {}; |
| 356 | |
| 357 | ebcdic_put(sysib.sysib_111.manuf, "QEMU ", 16); |
| 358 | /* same as machine type number in STORE CPU ID, but in EBCDIC */ |
| 359 | snprintf(type, ARRAY_SIZE(type), "%X", cpu->model->def->type); |
| 360 | ebcdic_put(sysib.sysib_111.type, type, 4); |
| 361 | /* model number (not stored in STORE CPU ID for z/Architecture) */ |
| 362 | ebcdic_put(sysib.sysib_111.model, "QEMU ", 16); |
| 363 | ebcdic_put(sysib.sysib_111.sequence, "QEMU ", 16); |
| 364 | ebcdic_put(sysib.sysib_111.plant, "QEMU", 4); |
| 365 | } else if ((sel1 == 2) && (sel2 == 1)) { |
| 366 | /* Basic Machine CPU */ |
| 367 | ebcdic_put(sysib.sysib_121.sequence, "QEMUQEMUQEMUQEMU", 16); |
| 368 | ebcdic_put(sysib.sysib_121.plant, "QEMU", 4); |
| 369 | sysib.sysib_121.cpu_addr = cpu_to_be16(env->core_id); |
| 370 | } else if ((sel1 == 2) && (sel2 == 2)) { |
| 371 | /* Basic Machine CPUs */ |
| 372 | sysib.sysib_122.capability = cpu_to_be32(0x443afc29); |
| 373 | sysib.sysib_122.total_cpus = cpu_to_be16(total_cpus); |
| 374 | sysib.sysib_122.conf_cpus = cpu_to_be16(conf_cpus); |
| 375 | sysib.sysib_122.reserved_cpus = cpu_to_be16(reserved_cpus); |
| 376 | } else { |
| 377 | cc = 3; |
| 378 | } |
| 379 | break; |
| 380 | case STSI_R0_FC_LEVEL_2: |
| 381 | if ((sel1 == 2) && (sel2 == 1)) { |
| 382 | /* LPAR CPU */ |
| 383 | ebcdic_put(sysib.sysib_221.sequence, "QEMUQEMUQEMUQEMU", 16); |
| 384 | ebcdic_put(sysib.sysib_221.plant, "QEMU", 4); |
| 385 | sysib.sysib_221.cpu_addr = cpu_to_be16(env->core_id); |
| 386 | } else if ((sel1 == 2) && (sel2 == 2)) { |
| 387 | /* LPAR CPUs */ |
| 388 | sysib.sysib_222.lcpuc = 0x80; /* dedicated */ |
| 389 | sysib.sysib_222.total_cpus = cpu_to_be16(total_cpus); |
| 390 | sysib.sysib_222.conf_cpus = cpu_to_be16(conf_cpus); |
| 391 | sysib.sysib_222.reserved_cpus = cpu_to_be16(reserved_cpus); |
| 392 | ebcdic_put(sysib.sysib_222.name, "QEMU ", 8); |
| 393 | sysib.sysib_222.caf = cpu_to_be32(1000); |
| 394 | sysib.sysib_222.dedicated_cpus = cpu_to_be16(conf_cpus); |
| 395 | } else { |
| 396 | cc = 3; |
| 397 | } |
| 398 | break; |
| 399 | case STSI_R0_FC_LEVEL_3: |
| 400 | if ((sel1 == 2) && (sel2 == 2)) { |
| 401 | /* VM CPUs */ |
| 402 | sysib.sysib_322.count = 1; |
| 403 | sysib.sysib_322.vm[0].total_cpus = cpu_to_be16(total_cpus); |
| 404 | sysib.sysib_322.vm[0].conf_cpus = cpu_to_be16(conf_cpus); |
| 405 | sysib.sysib_322.vm[0].reserved_cpus = cpu_to_be16(reserved_cpus); |
| 406 | sysib.sysib_322.vm[0].caf = cpu_to_be32(1000); |
| 407 | /* Linux kernel uses this to distinguish us from z/VM */ |
| 408 | ebcdic_put(sysib.sysib_322.vm[0].cpi, "KVM/Linux ", 16); |
| 409 | sysib.sysib_322.vm[0].ext_name_encoding = 2; /* UTF-8 */ |
| 410 | |
| 411 | /* If our VM has a name, use the real name */ |
| 412 | if (qemu_name) { |
| 413 | memset(sysib.sysib_322.vm[0].name, 0x40, |
| 414 | sizeof(sysib.sysib_322.vm[0].name)); |
| 415 | ebcdic_put(sysib.sysib_322.vm[0].name, qemu_name, |
| 416 | MIN(sizeof(sysib.sysib_322.vm[0].name), |
| 417 | strlen(qemu_name))); |
| 418 | strpadcpy((char *)sysib.sysib_322.ext_names[0], |
| 419 | sizeof(sysib.sysib_322.ext_names[0]), |
| 420 | qemu_name, '\0'); |
| 421 | |
| 422 | } else { |
| 423 | ebcdic_put(sysib.sysib_322.vm[0].name, "TCGguest", 8); |
| 424 | strcpy((char *)sysib.sysib_322.ext_names[0], "TCGguest"); |
| 425 | } |
| 426 | |
| 427 | /* add the uuid */ |
| 428 | memcpy(sysib.sysib_322.vm[0].uuid, &qemu_uuid, |
| 429 | sizeof(sysib.sysib_322.vm[0].uuid)); |
| 430 | } else { |
| 431 | cc = 3; |
| 432 | } |
| 433 | break; |
| 434 | } |
| 435 | |
| 436 | if (cc == 0) { |
| 437 | if (s390_cpu_virt_mem_write(cpu, a0, 0, &sysib, sizeof(sysib))) { |
| 438 | s390_cpu_virt_mem_handle_exc(cpu, ra); |
| 439 | } |
| 440 | } |
| 441 | |
| 442 | return cc; |
| 443 | } |
| 444 | |
| 445 | uint32_t HELPER(sigp)(CPUS390XState *env, uint64_t order_code, uint32_t r1, |
| 446 | uint32_t r3) |
| 447 | { |
| 448 | int cc; |
| 449 | |
| 450 | /* TODO: needed to inject interrupts - push further down */ |
| 451 | bql_lock(); |
| 452 | cc = handle_sigp(env, order_code & SIGP_ORDER_MASK, r1, r3); |
| 453 | bql_unlock(); |
| 454 | |
| 455 | return cc; |
| 456 | } |
| 457 | #endif |
| 458 | |
| 459 | #ifndef CONFIG_USER_ONLY |
| 460 | void HELPER(xsch)(CPUS390XState *env, uint64_t r1) |
| 461 | { |
| 462 | S390CPU *cpu = env_archcpu(env); |
| 463 | bql_lock(); |
| 464 | ioinst_handle_xsch(cpu, r1, GETPC()); |
| 465 | bql_unlock(); |
| 466 | } |
| 467 | |
| 468 | void HELPER(csch)(CPUS390XState *env, uint64_t r1) |
| 469 | { |
| 470 | S390CPU *cpu = env_archcpu(env); |
| 471 | bql_lock(); |
| 472 | ioinst_handle_csch(cpu, r1, GETPC()); |
| 473 | bql_unlock(); |
| 474 | } |
| 475 | |
| 476 | void HELPER(hsch)(CPUS390XState *env, uint64_t r1) |
| 477 | { |
| 478 | S390CPU *cpu = env_archcpu(env); |
| 479 | bql_lock(); |
| 480 | ioinst_handle_hsch(cpu, r1, GETPC()); |
| 481 | bql_unlock(); |
| 482 | } |
| 483 | |
| 484 | void HELPER(msch)(CPUS390XState *env, uint64_t r1, uint64_t inst) |
| 485 | { |
| 486 | S390CPU *cpu = env_archcpu(env); |
| 487 | bql_lock(); |
| 488 | ioinst_handle_msch(cpu, r1, inst >> 16, GETPC()); |
| 489 | bql_unlock(); |
| 490 | } |
| 491 | |
| 492 | void HELPER(rchp)(CPUS390XState *env, uint64_t r1) |
| 493 | { |
| 494 | S390CPU *cpu = env_archcpu(env); |
| 495 | bql_lock(); |
| 496 | ioinst_handle_rchp(cpu, r1, GETPC()); |
| 497 | bql_unlock(); |
| 498 | } |
| 499 | |
| 500 | void HELPER(rsch)(CPUS390XState *env, uint64_t r1) |
| 501 | { |
| 502 | S390CPU *cpu = env_archcpu(env); |
| 503 | bql_lock(); |
| 504 | ioinst_handle_rsch(cpu, r1, GETPC()); |
| 505 | bql_unlock(); |
| 506 | } |
| 507 | |
| 508 | void HELPER(sal)(CPUS390XState *env, uint64_t r1) |
| 509 | { |
| 510 | S390CPU *cpu = env_archcpu(env); |
| 511 | |
| 512 | bql_lock(); |
| 513 | ioinst_handle_sal(cpu, r1, GETPC()); |
| 514 | bql_unlock(); |
| 515 | } |
| 516 | |
| 517 | void HELPER(schm)(CPUS390XState *env, uint64_t r1, uint64_t r2, uint64_t inst) |
| 518 | { |
| 519 | S390CPU *cpu = env_archcpu(env); |
| 520 | |
| 521 | bql_lock(); |
| 522 | ioinst_handle_schm(cpu, r1, r2, inst >> 16, GETPC()); |
| 523 | bql_unlock(); |
| 524 | } |
| 525 | |
| 526 | void HELPER(ssch)(CPUS390XState *env, uint64_t r1, uint64_t inst) |
| 527 | { |
| 528 | S390CPU *cpu = env_archcpu(env); |
| 529 | bql_lock(); |
| 530 | ioinst_handle_ssch(cpu, r1, inst >> 16, GETPC()); |
| 531 | bql_unlock(); |
| 532 | } |
| 533 | |
| 534 | void HELPER(stcrw)(CPUS390XState *env, uint64_t inst) |
| 535 | { |
| 536 | S390CPU *cpu = env_archcpu(env); |
| 537 | |
| 538 | bql_lock(); |
| 539 | ioinst_handle_stcrw(cpu, inst >> 16, GETPC()); |
| 540 | bql_unlock(); |
| 541 | } |
| 542 | |
| 543 | void HELPER(stsch)(CPUS390XState *env, uint64_t r1, uint64_t inst) |
| 544 | { |
| 545 | S390CPU *cpu = env_archcpu(env); |
| 546 | bql_lock(); |
| 547 | ioinst_handle_stsch(cpu, r1, inst >> 16, GETPC()); |
| 548 | bql_unlock(); |
| 549 | } |
| 550 | |
| 551 | uint32_t HELPER(tpi)(CPUS390XState *env, uint64_t addr) |
| 552 | { |
| 553 | const uintptr_t ra = GETPC(); |
| 554 | S390CPU *cpu = env_archcpu(env); |
| 555 | QEMUS390FLICState *flic = s390_get_qemu_flic(s390_get_flic()); |
| 556 | QEMUS390FlicIO *io = NULL; |
| 557 | LowCore *lowcore; |
| 558 | |
| 559 | if (addr & 0x3) { |
| 560 | tcg_s390_program_interrupt(env, PGM_SPECIFICATION, ra); |
| 561 | } |
| 562 | |
| 563 | bql_lock(); |
| 564 | io = qemu_s390_flic_dequeue_io(flic, env->cregs[6]); |
| 565 | if (!io) { |
| 566 | bql_unlock(); |
| 567 | return 0; |
| 568 | } |
| 569 | |
| 570 | if (addr) { |
| 571 | struct { |
| 572 | uint16_t id; |
| 573 | uint16_t nr; |
| 574 | uint32_t parm; |
| 575 | } intc = { |
| 576 | .id = cpu_to_be16(io->id), |
| 577 | .nr = cpu_to_be16(io->nr), |
| 578 | .parm = cpu_to_be32(io->parm), |
| 579 | }; |
| 580 | |
| 581 | if (s390_cpu_virt_mem_write(cpu, addr, 0, &intc, sizeof(intc))) { |
| 582 | /* writing failed, reinject and properly clean up */ |
| 583 | s390_io_interrupt(io->id, io->nr, io->parm, io->word); |
| 584 | bql_unlock(); |
| 585 | g_free(io); |
| 586 | s390_cpu_virt_mem_handle_exc(cpu, ra); |
| 587 | return 0; |
| 588 | } |
| 589 | } else { |
| 590 | /* no protection applies */ |
| 591 | lowcore = cpu_map_lowcore(env); |
| 592 | lowcore->subchannel_id = cpu_to_be16(io->id); |
| 593 | lowcore->subchannel_nr = cpu_to_be16(io->nr); |
| 594 | lowcore->io_int_parm = cpu_to_be32(io->parm); |
| 595 | lowcore->io_int_word = cpu_to_be32(io->word); |
| 596 | cpu_unmap_lowcore(env, lowcore); |
| 597 | } |
| 598 | |
| 599 | g_free(io); |
| 600 | bql_unlock(); |
| 601 | return 1; |
| 602 | } |
| 603 | |
| 604 | void HELPER(tsch)(CPUS390XState *env, uint64_t r1, uint64_t inst) |
| 605 | { |
| 606 | S390CPU *cpu = env_archcpu(env); |
| 607 | bql_lock(); |
| 608 | ioinst_handle_tsch(cpu, r1, inst >> 16, GETPC()); |
| 609 | bql_unlock(); |
| 610 | } |
| 611 | |
| 612 | void HELPER(chsc)(CPUS390XState *env, uint64_t inst) |
| 613 | { |
| 614 | S390CPU *cpu = env_archcpu(env); |
| 615 | bql_lock(); |
| 616 | ioinst_handle_chsc(cpu, inst >> 16, GETPC()); |
| 617 | bql_unlock(); |
| 618 | } |
| 619 | #endif |
| 620 | |
| 621 | #ifndef CONFIG_USER_ONLY |
| 622 | static G_NORETURN void per_raise_exception(CPUS390XState *env) |
| 623 | { |
| 624 | trigger_pgm_exception(env, PGM_PER); |
| 625 | cpu_loop_exit(env_cpu(env)); |
| 626 | } |
| 627 | |
| 628 | static G_NORETURN void per_raise_exception_log(CPUS390XState *env) |
| 629 | { |
| 630 | qemu_log_mask(CPU_LOG_INT, "PER interrupt after 0x%" PRIx64 "\n", |
| 631 | env->per_address); |
| 632 | per_raise_exception(env); |
| 633 | } |
| 634 | |
| 635 | void HELPER(per_check_exception)(CPUS390XState *env) |
| 636 | { |
| 637 | /* psw_addr, per_address and int_pgm_ilen are already set. */ |
| 638 | if (unlikely(env->per_perc_atmid)) { |
| 639 | per_raise_exception_log(env); |
| 640 | } |
| 641 | } |
| 642 | |
| 643 | /* Check if an address is within the PER starting address and the PER |
| 644 | ending address. The address range might loop. */ |
| 645 | static inline bool get_per_in_range(CPUS390XState *env, uint64_t addr) |
| 646 | { |
| 647 | if (env->cregs[10] <= env->cregs[11]) { |
| 648 | return env->cregs[10] <= addr && addr <= env->cregs[11]; |
| 649 | } else { |
| 650 | return env->cregs[10] <= addr || addr <= env->cregs[11]; |
| 651 | } |
| 652 | } |
| 653 | |
| 654 | void HELPER(per_branch)(CPUS390XState *env, uint64_t dest, uint32_t ilen) |
| 655 | { |
| 656 | if ((env->cregs[9] & PER_CR9_CONTROL_BRANCH_ADDRESS) |
| 657 | && !get_per_in_range(env, dest)) { |
| 658 | return; |
| 659 | } |
| 660 | |
| 661 | env->psw.addr = dest; |
| 662 | env->int_pgm_ilen = ilen; |
| 663 | env->per_address = env->gbea; |
| 664 | env->per_perc_atmid = PER_CODE_EVENT_BRANCH | get_per_atmid(env); |
| 665 | per_raise_exception_log(env); |
| 666 | } |
| 667 | |
| 668 | void HELPER(per_ifetch)(CPUS390XState *env, uint32_t ilen) |
| 669 | { |
| 670 | if (get_per_in_range(env, env->psw.addr)) { |
| 671 | env->per_address = env->psw.addr; |
| 672 | env->int_pgm_ilen = ilen; |
| 673 | env->per_perc_atmid = PER_CODE_EVENT_IFETCH | get_per_atmid(env); |
| 674 | |
| 675 | /* If the instruction has to be nullified, trigger the |
| 676 | exception immediately. */ |
| 677 | if (env->cregs[9] & PER_CR9_EVENT_IFETCH_NULLIFICATION) { |
| 678 | env->per_perc_atmid |= PER_CODE_EVENT_NULLIFICATION; |
| 679 | qemu_log_mask(CPU_LOG_INT, "PER interrupt before 0x%" PRIx64 "\n", |
| 680 | env->per_address); |
| 681 | per_raise_exception(env); |
| 682 | } |
| 683 | } |
| 684 | } |
| 685 | |
| 686 | void HELPER(per_store_real)(CPUS390XState *env, uint32_t ilen) |
| 687 | { |
| 688 | /* PSW is saved just before calling the helper. */ |
| 689 | env->per_address = env->psw.addr; |
| 690 | env->int_pgm_ilen = ilen; |
| 691 | env->per_perc_atmid = PER_CODE_EVENT_STORE_REAL | get_per_atmid(env); |
| 692 | per_raise_exception_log(env); |
| 693 | } |
| 694 | #endif |
| 695 | |
| 696 | static uint8_t stfl_bytes[2048]; |
| 697 | static unsigned int used_stfl_bytes; |
| 698 | |
| 699 | static void prepare_stfl(void) |
| 700 | { |
| 701 | static bool initialized; |
| 702 | int i; |
| 703 | |
| 704 | /* racy, but we don't care, the same values are always written */ |
| 705 | if (initialized) { |
| 706 | return; |
| 707 | } |
| 708 | |
| 709 | s390_get_feat_block(S390_FEAT_TYPE_STFL, stfl_bytes); |
| 710 | for (i = 0; i < sizeof(stfl_bytes); i++) { |
| 711 | if (stfl_bytes[i]) { |
| 712 | used_stfl_bytes = i + 1; |
| 713 | } |
| 714 | } |
| 715 | initialized = true; |
| 716 | } |
| 717 | |
| 718 | #ifndef CONFIG_USER_ONLY |
| 719 | void HELPER(stfl)(CPUS390XState *env) |
| 720 | { |
| 721 | LowCore *lowcore; |
| 722 | |
| 723 | lowcore = cpu_map_lowcore(env); |
| 724 | prepare_stfl(); |
| 725 | memcpy(&lowcore->stfl_fac_list, stfl_bytes, sizeof(lowcore->stfl_fac_list)); |
| 726 | cpu_unmap_lowcore(env, lowcore); |
| 727 | } |
| 728 | #endif |
| 729 | |
| 730 | uint32_t HELPER(stfle)(CPUS390XState *env, uint64_t addr) |
| 731 | { |
| 732 | const int mmu_idx = cpu_mmu_index(env_cpu(env), false); |
| 733 | const MemOpIdx oi = make_memop_idx(MO_8, mmu_idx); |
| 734 | const uintptr_t ra = GETPC(); |
| 735 | const int count_bytes = ((env->regs[0] & 0xff) + 1) * 8; |
| 736 | int max_bytes; |
| 737 | int i; |
| 738 | |
| 739 | if (addr & 0x7) { |
| 740 | tcg_s390_program_interrupt(env, PGM_SPECIFICATION, ra); |
| 741 | } |
| 742 | |
| 743 | prepare_stfl(); |
| 744 | max_bytes = ROUND_UP(used_stfl_bytes, 8); |
| 745 | |
| 746 | /* |
| 747 | * The PoP says that doublewords beyond the highest-numbered facility |
| 748 | * bit may or may not be stored. However, existing hardware appears to |
| 749 | * not store the words, and existing software depend on that. |
| 750 | */ |
| 751 | for (i = 0; i < MIN(count_bytes, max_bytes); ++i) { |
| 752 | cpu_stb_mmu(env, addr + i, stfl_bytes[i], oi, ra); |
| 753 | } |
| 754 | |
| 755 | env->regs[0] = deposit64(env->regs[0], 0, 8, (max_bytes / 8) - 1); |
| 756 | return count_bytes >= max_bytes ? 0 : 3; |
| 757 | } |
| 758 | |
| 759 | #ifndef CONFIG_USER_ONLY |
| 760 | /* |
| 761 | * Note: we ignore any return code of the functions called for the pci |
| 762 | * instructions, as the only time they return !0 is when the stub is |
| 763 | * called, and in that case we didn't even offer the zpci facility. |
| 764 | * The only exception is SIC, where program checks need to be handled |
| 765 | * by the caller. |
| 766 | */ |
| 767 | void HELPER(clp)(CPUS390XState *env, uint32_t r2) |
| 768 | { |
| 769 | S390CPU *cpu = env_archcpu(env); |
| 770 | |
| 771 | bql_lock(); |
| 772 | clp_service_call(cpu, r2, GETPC()); |
| 773 | bql_unlock(); |
| 774 | } |
| 775 | |
| 776 | void HELPER(pcilg)(CPUS390XState *env, uint32_t r1, uint32_t r2) |
| 777 | { |
| 778 | S390CPU *cpu = env_archcpu(env); |
| 779 | |
| 780 | bql_lock(); |
| 781 | pcilg_service_call(cpu, r1, r2, GETPC()); |
| 782 | bql_unlock(); |
| 783 | } |
| 784 | |
| 785 | void HELPER(pcistg)(CPUS390XState *env, uint32_t r1, uint32_t r2) |
| 786 | { |
| 787 | S390CPU *cpu = env_archcpu(env); |
| 788 | |
| 789 | bql_lock(); |
| 790 | pcistg_service_call(cpu, r1, r2, GETPC()); |
| 791 | bql_unlock(); |
| 792 | } |
| 793 | |
| 794 | void HELPER(stpcifc)(CPUS390XState *env, uint32_t r1, uint64_t fiba, |
| 795 | uint32_t ar) |
| 796 | { |
| 797 | S390CPU *cpu = env_archcpu(env); |
| 798 | |
| 799 | bql_lock(); |
| 800 | stpcifc_service_call(cpu, r1, fiba, ar, GETPC()); |
| 801 | bql_unlock(); |
| 802 | } |
| 803 | |
| 804 | void HELPER(sic)(CPUS390XState *env, uint64_t r1, uint64_t r3) |
| 805 | { |
| 806 | S390CPU *cpu = env_archcpu(env); |
| 807 | int r; |
| 808 | |
| 809 | bql_lock(); |
| 810 | r = css_do_sic(cpu, (r3 >> 27) & 0x7, r1 & 0xffff); |
| 811 | bql_unlock(); |
| 812 | /* css_do_sic() may actually return a PGM_xxx value to inject */ |
| 813 | if (r) { |
| 814 | tcg_s390_program_interrupt(env, -r, GETPC()); |
| 815 | } |
| 816 | } |
| 817 | |
| 818 | void HELPER(rpcit)(CPUS390XState *env, uint32_t r1, uint32_t r2) |
| 819 | { |
| 820 | S390CPU *cpu = env_archcpu(env); |
| 821 | |
| 822 | bql_lock(); |
| 823 | rpcit_service_call(cpu, r1, r2, GETPC()); |
| 824 | bql_unlock(); |
| 825 | } |
| 826 | |
| 827 | void HELPER(pcistb)(CPUS390XState *env, uint32_t r1, uint32_t r3, |
| 828 | uint64_t gaddr, uint32_t ar) |
| 829 | { |
| 830 | S390CPU *cpu = env_archcpu(env); |
| 831 | |
| 832 | bql_lock(); |
| 833 | pcistb_service_call(cpu, r1, r3, gaddr, ar, GETPC()); |
| 834 | bql_unlock(); |
| 835 | } |
| 836 | |
| 837 | void HELPER(mpcifc)(CPUS390XState *env, uint32_t r1, uint64_t fiba, |
| 838 | uint32_t ar) |
| 839 | { |
| 840 | S390CPU *cpu = env_archcpu(env); |
| 841 | |
| 842 | bql_lock(); |
| 843 | mpcifc_service_call(cpu, r1, fiba, ar, GETPC()); |
| 844 | bql_unlock(); |
| 845 | } |
| 846 | #endif |