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/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 (!(cs->interrupt_request & 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 = ldq_phys(s->as, 0); 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_page_debug = s390_cpu_get_phys_page_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 static 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 unsigned int 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 return s390_count_running_cpus(); 228 } 229 230 void s390_cpu_unhalt(S390CPU *cpu) 231 { 232 CPUState *cs = CPU(cpu); 233 trace_cpu_unhalt(cs->cpu_index); 234 235 if (cs->halted) { 236 cs->halted = 0; 237 cs->exception_index = -1; 238 } 239 } 240 241 unsigned int s390_cpu_set_state(uint8_t cpu_state, S390CPU *cpu) 242 { 243 trace_cpu_set_state(CPU(cpu)->cpu_index, cpu_state); 244 245 switch (cpu_state) { 246 case S390_CPU_STATE_STOPPED: 247 case S390_CPU_STATE_CHECK_STOP: 248 /* halt the cpu for common infrastructure */ 249 s390_cpu_halt(cpu); 250 break; 251 case S390_CPU_STATE_OPERATING: 252 case S390_CPU_STATE_LOAD: 253 /* 254 * Starting a CPU with a PSW WAIT bit set: 255 * KVM: handles this internally and triggers another WAIT exit. 256 * TCG: will actually try to continue to run. Don't unhalt, will 257 * be done when the CPU actually has work (an interrupt). 258 */ 259 if (!tcg_enabled() || !(cpu->env.psw.mask & PSW_MASK_WAIT)) { 260 s390_cpu_unhalt(cpu); 261 } 262 break; 263 default: 264 error_report("Requested CPU state is not a valid S390 CPU state: %u", 265 cpu_state); 266 exit(1); 267 } 268 if (kvm_enabled() && cpu->env.cpu_state != cpu_state) { 269 kvm_s390_set_cpu_state(cpu, cpu_state); 270 } 271 cpu->env.cpu_state = cpu_state; 272 273 return s390_count_running_cpus(); 274 } 275 276 void s390_cmma_reset(void) 277 { 278 if (kvm_enabled()) { 279 kvm_s390_cmma_reset(); 280 } 281 } 282 283 int s390_assign_subch_ioeventfd(EventNotifier *notifier, uint32_t sch_id, 284 int vq, bool assign) 285 { 286 if (kvm_enabled()) { 287 return kvm_s390_assign_subch_ioeventfd(notifier, sch_id, vq, assign); 288 } else { 289 return 0; 290 } 291 } 292 293 void s390_crypto_reset(void) 294 { 295 if (kvm_enabled()) { 296 kvm_s390_crypto_reset(); 297 } 298 } 299 300 void s390_enable_css_support(S390CPU *cpu) 301 { 302 if (kvm_enabled()) { 303 kvm_s390_enable_css_support(cpu); 304 } 305 } 306 307 void s390_do_cpu_set_diag318(CPUState *cs, run_on_cpu_data arg) 308 { 309 if (kvm_enabled()) { 310 kvm_s390_set_diag318(cs, arg.host_ulong); 311 } 312 } 313 314 void s390_cpu_topology_set_changed(bool changed) 315 { 316 int ret; 317 318 if (kvm_enabled()) { 319 ret = kvm_s390_topology_set_mtcr(changed); 320 if (ret) { 321 error_report("Failed to set Modified Topology Change Report: %s", 322 strerror(-ret)); 323 } 324 } 325 } 326