| 1 | /* |
| 2 | * QEMU S/390 CPU - System-only code |
| 3 | * |
| 4 | * Copyright (c) 2009 Ulrich Hecht |
| 5 | * Copyright (c) 2011 Alexander Graf |
| 6 | * Copyright (c) 2012 SUSE LINUX Products GmbH |
| 7 | * Copyright (c) 2012 IBM Corp. |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU General Public License as published by |
| 11 | * the Free Software Foundation; either version 2 of the License, or |
| 12 | * (at your option) any later version. |
| 13 | * |
| 14 | * This program is distributed in the hope that it will be useful, |
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 17 | * General Public License for more details. |
| 18 | * |
| 19 | * You should have received a copy of the GNU General Public License |
| 20 | * along with this program; if not, see <http://www.gnu.org/licenses/>. |
| 21 | */ |
| 22 | |
| 23 | #include "qemu/osdep.h" |
| 24 | #include "qemu/error-report.h" |
| 25 | #include "qapi/error.h" |
| 26 | #include "cpu.h" |
| 27 | #include "s390x-internal.h" |
| 28 | #include "kvm/kvm_s390x.h" |
| 29 | #include "system/kvm.h" |
| 30 | #include "system/reset.h" |
| 31 | #include "qemu/timer.h" |
| 32 | #include "trace.h" |
| 33 | #include "qapi/qapi-visit-run-state.h" |
| 34 | #include "system/hw_accel.h" |
| 35 | |
| 36 | #include "target/s390x/kvm/pv.h" |
| 37 | #include "hw/core/boards.h" |
| 38 | #include "system/system.h" |
| 39 | #include "system/tcg.h" |
| 40 | #include "hw/core/sysemu-cpu-ops.h" |
| 41 | |
| 42 | bool s390_cpu_has_work(CPUState *cs) |
| 43 | { |
| 44 | S390CPU *cpu = S390_CPU(cs); |
| 45 | |
| 46 | /* STOPPED cpus can never wake up */ |
| 47 | if (s390_cpu_get_state(cpu) != S390_CPU_STATE_LOAD && |
| 48 | s390_cpu_get_state(cpu) != S390_CPU_STATE_OPERATING) { |
| 49 | return false; |
| 50 | } |
| 51 | |
| 52 | if (!cpu_test_interrupt(cs, CPU_INTERRUPT_HARD)) { |
| 53 | return false; |
| 54 | } |
| 55 | |
| 56 | return s390_cpu_has_int(cpu); |
| 57 | } |
| 58 | |
| 59 | /* S390CPUClass::load_normal() */ |
| 60 | static void s390_cpu_load_normal(CPUState *s) |
| 61 | { |
| 62 | S390CPU *cpu = S390_CPU(s); |
| 63 | uint64_t spsw; |
| 64 | |
| 65 | if (!s390_is_pv()) { |
| 66 | spsw = address_space_ldq_be(s->as, 0, MEMTXATTRS_UNSPECIFIED, NULL); |
| 67 | cpu->env.psw.mask = spsw & PSW_MASK_SHORT_CTRL; |
| 68 | /* |
| 69 | * Invert short psw indication, so SIE will report a specification |
| 70 | * exception if it was not set. |
| 71 | */ |
| 72 | cpu->env.psw.mask ^= PSW_MASK_SHORTPSW; |
| 73 | cpu->env.psw.addr = spsw & PSW_MASK_SHORT_ADDR; |
| 74 | } else { |
| 75 | /* |
| 76 | * Firmware requires us to set the load state before we set |
| 77 | * the cpu to operating on protected guests. |
| 78 | */ |
| 79 | s390_cpu_set_state(S390_CPU_STATE_LOAD, cpu); |
| 80 | } |
| 81 | s390_cpu_set_state(S390_CPU_STATE_OPERATING, cpu); |
| 82 | } |
| 83 | |
| 84 | void s390_cpu_machine_reset_cb(void *opaque) |
| 85 | { |
| 86 | S390CPU *cpu = opaque; |
| 87 | |
| 88 | run_on_cpu(CPU(cpu), s390_do_cpu_full_reset, RUN_ON_CPU_NULL); |
| 89 | } |
| 90 | |
| 91 | static GuestPanicInformation *s390_cpu_get_crash_info(CPUState *cs) |
| 92 | { |
| 93 | GuestPanicInformation *panic_info; |
| 94 | S390CPU *cpu = S390_CPU(cs); |
| 95 | |
| 96 | cpu_synchronize_state(cs); |
| 97 | panic_info = g_new0(GuestPanicInformation, 1); |
| 98 | |
| 99 | panic_info->type = GUEST_PANIC_INFORMATION_TYPE_S390; |
| 100 | panic_info->u.s390.core = cpu->env.core_id; |
| 101 | panic_info->u.s390.psw_mask = cpu->env.psw.mask; |
| 102 | panic_info->u.s390.psw_addr = cpu->env.psw.addr; |
| 103 | panic_info->u.s390.reason = cpu->env.crash_reason; |
| 104 | |
| 105 | return panic_info; |
| 106 | } |
| 107 | |
| 108 | static void s390_cpu_get_crash_info_qom(Object *obj, Visitor *v, |
| 109 | const char *name, void *opaque, |
| 110 | Error **errp) |
| 111 | { |
| 112 | CPUState *cs = CPU(obj); |
| 113 | GuestPanicInformation *panic_info; |
| 114 | |
| 115 | if (!cs->crash_occurred) { |
| 116 | error_setg(errp, "No crash occurred"); |
| 117 | return; |
| 118 | } |
| 119 | |
| 120 | panic_info = s390_cpu_get_crash_info(cs); |
| 121 | |
| 122 | visit_type_GuestPanicInformation(v, "crash-information", &panic_info, |
| 123 | errp); |
| 124 | qapi_free_GuestPanicInformation(panic_info); |
| 125 | } |
| 126 | |
| 127 | void s390_cpu_system_init(Object *obj) |
| 128 | { |
| 129 | CPUState *cs = CPU(obj); |
| 130 | S390CPU *cpu = S390_CPU(obj); |
| 131 | |
| 132 | cs->start_powered_off = true; |
| 133 | object_property_add(obj, "crash-information", "GuestPanicInformation", |
| 134 | s390_cpu_get_crash_info_qom, NULL, NULL, NULL); |
| 135 | cpu->env.tod_timer = |
| 136 | timer_new_ns(QEMU_CLOCK_VIRTUAL, s390x_tod_timer, cpu); |
| 137 | cpu->env.cpu_timer = |
| 138 | timer_new_ns(QEMU_CLOCK_VIRTUAL, s390x_cpu_timer, cpu); |
| 139 | s390_cpu_set_state(S390_CPU_STATE_STOPPED, cpu); |
| 140 | } |
| 141 | |
| 142 | bool s390_cpu_system_realize(DeviceState *dev, Error **errp) |
| 143 | { |
| 144 | S390CPU *cpu = S390_CPU(dev); |
| 145 | MachineState *ms = MACHINE(qdev_get_machine()); |
| 146 | unsigned int max_cpus = ms->smp.max_cpus; |
| 147 | |
| 148 | if (cpu->env.core_id >= max_cpus) { |
| 149 | error_setg(errp, "Unable to add CPU with core-id: %" PRIu32 |
| 150 | ", maximum core-id: %d", cpu->env.core_id, |
| 151 | max_cpus - 1); |
| 152 | return false; |
| 153 | } |
| 154 | |
| 155 | if (cpu_exists(cpu->env.core_id)) { |
| 156 | error_setg(errp, "Unable to add CPU with core-id: %" PRIu32 |
| 157 | ", it already exists", cpu->env.core_id); |
| 158 | return false; |
| 159 | } |
| 160 | |
| 161 | /* sync cs->cpu_index and env->core_id. The latter is needed for TCG. */ |
| 162 | CPU(cpu)->cpu_index = cpu->env.core_id; |
| 163 | return true; |
| 164 | } |
| 165 | |
| 166 | void s390_cpu_finalize(Object *obj) |
| 167 | { |
| 168 | S390CPU *cpu = S390_CPU(obj); |
| 169 | |
| 170 | timer_free(cpu->env.tod_timer); |
| 171 | timer_free(cpu->env.cpu_timer); |
| 172 | |
| 173 | qemu_unregister_reset(s390_cpu_machine_reset_cb, cpu); |
| 174 | g_free(cpu->irqstate); |
| 175 | } |
| 176 | |
| 177 | static const struct SysemuCPUOps s390_sysemu_ops = { |
| 178 | .has_work = s390_cpu_has_work, |
| 179 | .get_phys_addr_debug = s390_cpu_get_phys_addr_debug, |
| 180 | .get_crash_info = s390_cpu_get_crash_info, |
| 181 | .write_elf64_note = s390_cpu_write_elf64_note, |
| 182 | .legacy_vmsd = &vmstate_s390_cpu, |
| 183 | }; |
| 184 | |
| 185 | void s390_cpu_system_class_init(CPUClass *cc) |
| 186 | { |
| 187 | S390CPUClass *scc = S390_CPU_CLASS(cc); |
| 188 | |
| 189 | scc->load_normal = s390_cpu_load_normal; |
| 190 | cc->sysemu_ops = &s390_sysemu_ops; |
| 191 | } |
| 192 | |
| 193 | static bool disabled_wait(CPUState *cpu) |
| 194 | { |
| 195 | return cpu->halted && !(S390_CPU(cpu)->env.psw.mask & |
| 196 | (PSW_MASK_IO | PSW_MASK_EXT | PSW_MASK_MCHECK)); |
| 197 | } |
| 198 | |
| 199 | unsigned s390_count_running_cpus(void) |
| 200 | { |
| 201 | CPUState *cpu; |
| 202 | int nr_running = 0; |
| 203 | |
| 204 | CPU_FOREACH(cpu) { |
| 205 | uint8_t state = S390_CPU(cpu)->env.cpu_state; |
| 206 | if (state == S390_CPU_STATE_OPERATING || |
| 207 | state == S390_CPU_STATE_LOAD) { |
| 208 | if (!disabled_wait(cpu)) { |
| 209 | nr_running++; |
| 210 | } |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | return nr_running; |
| 215 | } |
| 216 | |
| 217 | void s390_cpu_halt(S390CPU *cpu) |
| 218 | { |
| 219 | CPUState *cs = CPU(cpu); |
| 220 | trace_cpu_halt(cs->cpu_index); |
| 221 | |
| 222 | if (!cs->halted) { |
| 223 | cs->halted = 1; |
| 224 | cs->exception_index = EXCP_HLT; |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | void s390_cpu_unhalt(S390CPU *cpu) |
| 229 | { |
| 230 | CPUState *cs = CPU(cpu); |
| 231 | trace_cpu_unhalt(cs->cpu_index); |
| 232 | |
| 233 | if (cs->halted) { |
| 234 | cs->halted = 0; |
| 235 | cs->exception_index = -1; |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | unsigned int s390_cpu_set_state(uint8_t cpu_state, S390CPU *cpu) |
| 240 | { |
| 241 | trace_cpu_set_state(CPU(cpu)->cpu_index, cpu_state); |
| 242 | |
| 243 | switch (cpu_state) { |
| 244 | case S390_CPU_STATE_STOPPED: |
| 245 | case S390_CPU_STATE_CHECK_STOP: |
| 246 | /* halt the cpu for common infrastructure */ |
| 247 | s390_cpu_halt(cpu); |
| 248 | break; |
| 249 | case S390_CPU_STATE_OPERATING: |
| 250 | case S390_CPU_STATE_LOAD: |
| 251 | /* |
| 252 | * Starting a CPU with a PSW WAIT bit set: |
| 253 | * KVM: handles this internally and triggers another WAIT exit. |
| 254 | * TCG: will actually try to continue to run. Don't unhalt, will |
| 255 | * be done when the CPU actually has work (an interrupt). |
| 256 | */ |
| 257 | if (!tcg_enabled() || !(cpu->env.psw.mask & PSW_MASK_WAIT)) { |
| 258 | s390_cpu_unhalt(cpu); |
| 259 | } |
| 260 | break; |
| 261 | default: |
| 262 | error_report("Requested CPU state is not a valid S390 CPU state: %u", |
| 263 | cpu_state); |
| 264 | exit(1); |
| 265 | } |
| 266 | if (kvm_enabled() && cpu->env.cpu_state != cpu_state) { |
| 267 | kvm_s390_set_cpu_state(cpu, cpu_state); |
| 268 | } |
| 269 | cpu->env.cpu_state = cpu_state; |
| 270 | |
| 271 | return s390_count_running_cpus(); |
| 272 | } |
| 273 | |
| 274 | void s390_cmma_reset(void) |
| 275 | { |
| 276 | if (kvm_enabled()) { |
| 277 | kvm_s390_cmma_reset(); |
| 278 | } |
| 279 | } |
| 280 | |
| 281 | int s390_assign_subch_ioeventfd(EventNotifier *notifier, uint32_t sch_id, |
| 282 | int vq, bool assign) |
| 283 | { |
| 284 | if (kvm_enabled()) { |
| 285 | return kvm_s390_assign_subch_ioeventfd(notifier, sch_id, vq, assign); |
| 286 | } else { |
| 287 | return 0; |
| 288 | } |
| 289 | } |
| 290 | |
| 291 | void s390_crypto_reset(void) |
| 292 | { |
| 293 | if (kvm_enabled()) { |
| 294 | kvm_s390_crypto_reset(); |
| 295 | } |
| 296 | } |
| 297 | |
| 298 | void s390_enable_css_support(S390CPU *cpu) |
| 299 | { |
| 300 | if (kvm_enabled()) { |
| 301 | kvm_s390_enable_css_support(cpu); |
| 302 | } |
| 303 | } |
| 304 | |
| 305 | void s390_do_cpu_set_diag318(CPUState *cs, run_on_cpu_data arg) |
| 306 | { |
| 307 | if (kvm_enabled()) { |
| 308 | kvm_s390_set_diag318(cs, arg.host_ulong); |
| 309 | } |
| 310 | } |
| 311 | |
| 312 | void s390_cpu_topology_set_changed(bool changed) |
| 313 | { |
| 314 | int ret; |
| 315 | |
| 316 | if (kvm_enabled()) { |
| 317 | ret = kvm_s390_topology_set_mtcr(changed); |
| 318 | if (ret) { |
| 319 | error_report("Failed to set Modified Topology Change Report: %s", |
| 320 | strerror(-ret)); |
| 321 | } |
| 322 | } |
| 323 | } |