scsi-disk: move scsi_handle_rw_error earlier
[qemu/ar7.git] / target / s390x / cpu.c
blobd35eb39a1bbd6b9b91587042b7d2f25bedd5a929
1 /*
2 * QEMU S/390 CPU
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.
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.
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.
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/>.
23 #include "qemu/osdep.h"
24 #include "qapi/error.h"
25 #include "cpu.h"
26 #include "internal.h"
27 #include "kvm_s390x.h"
28 #include "sysemu/kvm.h"
29 #include "sysemu/reset.h"
30 #include "qemu/timer.h"
31 #include "qemu/error-report.h"
32 #include "qemu/module.h"
33 #include "trace.h"
34 #include "qapi/visitor.h"
35 #include "qapi/qapi-types-machine.h"
36 #include "qapi/qapi-visit-run-state.h"
37 #include "sysemu/hw_accel.h"
38 #include "hw/qdev-properties.h"
39 #ifndef CONFIG_USER_ONLY
40 #include "hw/s390x/pv.h"
41 #include "hw/boards.h"
42 #include "sysemu/arch_init.h"
43 #include "sysemu/sysemu.h"
44 #include "sysemu/tcg.h"
45 #endif
46 #include "fpu/softfloat-helpers.h"
47 #include "disas/capstone.h"
49 #define CR0_RESET 0xE0UL
50 #define CR14_RESET 0xC2000000UL;
52 static void s390_cpu_set_pc(CPUState *cs, vaddr value)
54 S390CPU *cpu = S390_CPU(cs);
56 cpu->env.psw.addr = value;
59 static bool s390_cpu_has_work(CPUState *cs)
61 S390CPU *cpu = S390_CPU(cs);
63 /* STOPPED cpus can never wake up */
64 if (s390_cpu_get_state(cpu) != S390_CPU_STATE_LOAD &&
65 s390_cpu_get_state(cpu) != S390_CPU_STATE_OPERATING) {
66 return false;
69 if (!(cs->interrupt_request & CPU_INTERRUPT_HARD)) {
70 return false;
73 return s390_cpu_has_int(cpu);
76 #if !defined(CONFIG_USER_ONLY)
77 /* S390CPUClass::load_normal() */
78 static void s390_cpu_load_normal(CPUState *s)
80 S390CPU *cpu = S390_CPU(s);
81 uint64_t spsw;
83 if (!s390_is_pv()) {
84 spsw = ldq_phys(s->as, 0);
85 cpu->env.psw.mask = spsw & PSW_MASK_SHORT_CTRL;
87 * Invert short psw indication, so SIE will report a specification
88 * exception if it was not set.
90 cpu->env.psw.mask ^= PSW_MASK_SHORTPSW;
91 cpu->env.psw.addr = spsw & PSW_MASK_SHORT_ADDR;
92 } else {
94 * Firmware requires us to set the load state before we set
95 * the cpu to operating on protected guests.
97 s390_cpu_set_state(S390_CPU_STATE_LOAD, cpu);
99 s390_cpu_set_state(S390_CPU_STATE_OPERATING, cpu);
101 #endif
103 /* S390CPUClass::reset() */
104 static void s390_cpu_reset(CPUState *s, cpu_reset_type type)
106 S390CPU *cpu = S390_CPU(s);
107 S390CPUClass *scc = S390_CPU_GET_CLASS(cpu);
108 CPUS390XState *env = &cpu->env;
109 DeviceState *dev = DEVICE(s);
111 scc->parent_reset(dev);
112 cpu->env.sigp_order = 0;
113 s390_cpu_set_state(S390_CPU_STATE_STOPPED, cpu);
115 switch (type) {
116 case S390_CPU_RESET_CLEAR:
117 memset(env, 0, offsetof(CPUS390XState, start_initial_reset_fields));
118 /* fall through */
119 case S390_CPU_RESET_INITIAL:
120 /* initial reset does not clear everything! */
121 memset(&env->start_initial_reset_fields, 0,
122 offsetof(CPUS390XState, start_normal_reset_fields) -
123 offsetof(CPUS390XState, start_initial_reset_fields));
125 /* architectured initial value for Breaking-Event-Address register */
126 env->gbea = 1;
128 /* architectured initial values for CR 0 and 14 */
129 env->cregs[0] = CR0_RESET;
130 env->cregs[14] = CR14_RESET;
132 #if defined(CONFIG_USER_ONLY)
133 /* user mode should always be allowed to use the full FPU */
134 env->cregs[0] |= CR0_AFP;
135 if (s390_has_feat(S390_FEAT_VECTOR)) {
136 env->cregs[0] |= CR0_VECTOR;
138 #endif
140 /* tininess for underflow is detected before rounding */
141 set_float_detect_tininess(float_tininess_before_rounding,
142 &env->fpu_status);
143 /* fall through */
144 case S390_CPU_RESET_NORMAL:
145 env->psw.mask &= ~PSW_MASK_RI;
146 memset(&env->start_normal_reset_fields, 0,
147 offsetof(CPUS390XState, end_reset_fields) -
148 offsetof(CPUS390XState, start_normal_reset_fields));
150 env->pfault_token = -1UL;
151 env->bpbc = false;
152 break;
153 default:
154 g_assert_not_reached();
157 /* Reset state inside the kernel that we cannot access yet from QEMU. */
158 if (kvm_enabled()) {
159 switch (type) {
160 case S390_CPU_RESET_CLEAR:
161 kvm_s390_reset_vcpu_clear(cpu);
162 break;
163 case S390_CPU_RESET_INITIAL:
164 kvm_s390_reset_vcpu_initial(cpu);
165 break;
166 case S390_CPU_RESET_NORMAL:
167 kvm_s390_reset_vcpu_normal(cpu);
168 break;
173 #if !defined(CONFIG_USER_ONLY)
174 static void s390_cpu_machine_reset_cb(void *opaque)
176 S390CPU *cpu = opaque;
178 run_on_cpu(CPU(cpu), s390_do_cpu_full_reset, RUN_ON_CPU_NULL);
180 #endif
182 static void s390_cpu_disas_set_info(CPUState *cpu, disassemble_info *info)
184 info->mach = bfd_mach_s390_64;
185 info->print_insn = print_insn_s390;
186 info->cap_arch = CS_ARCH_SYSZ;
187 info->cap_insn_unit = 2;
188 info->cap_insn_split = 6;
191 static void s390_cpu_realizefn(DeviceState *dev, Error **errp)
193 CPUState *cs = CPU(dev);
194 S390CPUClass *scc = S390_CPU_GET_CLASS(dev);
195 #if !defined(CONFIG_USER_ONLY)
196 S390CPU *cpu = S390_CPU(dev);
197 #endif
198 Error *err = NULL;
200 /* the model has to be realized before qemu_init_vcpu() due to kvm */
201 s390_realize_cpu_model(cs, &err);
202 if (err) {
203 goto out;
206 #if !defined(CONFIG_USER_ONLY)
207 MachineState *ms = MACHINE(qdev_get_machine());
208 unsigned int max_cpus = ms->smp.max_cpus;
209 if (cpu->env.core_id >= max_cpus) {
210 error_setg(&err, "Unable to add CPU with core-id: %" PRIu32
211 ", maximum core-id: %d", cpu->env.core_id,
212 max_cpus - 1);
213 goto out;
216 if (cpu_exists(cpu->env.core_id)) {
217 error_setg(&err, "Unable to add CPU with core-id: %" PRIu32
218 ", it already exists", cpu->env.core_id);
219 goto out;
222 /* sync cs->cpu_index and env->core_id. The latter is needed for TCG. */
223 cs->cpu_index = cpu->env.core_id;
224 #endif
226 cpu_exec_realizefn(cs, &err);
227 if (err != NULL) {
228 goto out;
231 #if !defined(CONFIG_USER_ONLY)
232 qemu_register_reset(s390_cpu_machine_reset_cb, cpu);
233 #endif
234 s390_cpu_gdb_init(cs);
235 qemu_init_vcpu(cs);
238 * KVM requires the initial CPU reset ioctl to be executed on the target
239 * CPU thread. CPU hotplug under single-threaded TCG will not work with
240 * run_on_cpu(), as run_on_cpu() will not work properly if called while
241 * the main thread is already running but the CPU hasn't been realized.
243 if (kvm_enabled()) {
244 run_on_cpu(cs, s390_do_cpu_full_reset, RUN_ON_CPU_NULL);
245 } else {
246 cpu_reset(cs);
249 scc->parent_realize(dev, &err);
250 out:
251 error_propagate(errp, err);
254 #if !defined(CONFIG_USER_ONLY)
255 static GuestPanicInformation *s390_cpu_get_crash_info(CPUState *cs)
257 GuestPanicInformation *panic_info;
258 S390CPU *cpu = S390_CPU(cs);
260 cpu_synchronize_state(cs);
261 panic_info = g_malloc0(sizeof(GuestPanicInformation));
263 panic_info->type = GUEST_PANIC_INFORMATION_TYPE_S390;
264 panic_info->u.s390.core = cpu->env.core_id;
265 panic_info->u.s390.psw_mask = cpu->env.psw.mask;
266 panic_info->u.s390.psw_addr = cpu->env.psw.addr;
267 panic_info->u.s390.reason = cpu->env.crash_reason;
269 return panic_info;
272 static void s390_cpu_get_crash_info_qom(Object *obj, Visitor *v,
273 const char *name, void *opaque,
274 Error **errp)
276 CPUState *cs = CPU(obj);
277 GuestPanicInformation *panic_info;
279 if (!cs->crash_occurred) {
280 error_setg(errp, "No crash occurred");
281 return;
284 panic_info = s390_cpu_get_crash_info(cs);
286 visit_type_GuestPanicInformation(v, "crash-information", &panic_info,
287 errp);
288 qapi_free_GuestPanicInformation(panic_info);
290 #endif
292 static void s390_cpu_initfn(Object *obj)
294 CPUState *cs = CPU(obj);
295 S390CPU *cpu = S390_CPU(obj);
297 cpu_set_cpustate_pointers(cpu);
298 cs->exception_index = EXCP_HLT;
299 #if !defined(CONFIG_USER_ONLY)
300 cs->start_powered_off = true;
301 object_property_add(obj, "crash-information", "GuestPanicInformation",
302 s390_cpu_get_crash_info_qom, NULL, NULL, NULL);
303 cpu->env.tod_timer =
304 timer_new_ns(QEMU_CLOCK_VIRTUAL, s390x_tod_timer, cpu);
305 cpu->env.cpu_timer =
306 timer_new_ns(QEMU_CLOCK_VIRTUAL, s390x_cpu_timer, cpu);
307 s390_cpu_set_state(S390_CPU_STATE_STOPPED, cpu);
308 #endif
311 static void s390_cpu_finalize(Object *obj)
313 #if !defined(CONFIG_USER_ONLY)
314 S390CPU *cpu = S390_CPU(obj);
316 timer_free(cpu->env.tod_timer);
317 timer_free(cpu->env.cpu_timer);
319 qemu_unregister_reset(s390_cpu_machine_reset_cb, cpu);
320 g_free(cpu->irqstate);
321 #endif
324 #if !defined(CONFIG_USER_ONLY)
325 static bool disabled_wait(CPUState *cpu)
327 return cpu->halted && !(S390_CPU(cpu)->env.psw.mask &
328 (PSW_MASK_IO | PSW_MASK_EXT | PSW_MASK_MCHECK));
331 static unsigned s390_count_running_cpus(void)
333 CPUState *cpu;
334 int nr_running = 0;
336 CPU_FOREACH(cpu) {
337 uint8_t state = S390_CPU(cpu)->env.cpu_state;
338 if (state == S390_CPU_STATE_OPERATING ||
339 state == S390_CPU_STATE_LOAD) {
340 if (!disabled_wait(cpu)) {
341 nr_running++;
346 return nr_running;
349 unsigned int s390_cpu_halt(S390CPU *cpu)
351 CPUState *cs = CPU(cpu);
352 trace_cpu_halt(cs->cpu_index);
354 if (!cs->halted) {
355 cs->halted = 1;
356 cs->exception_index = EXCP_HLT;
359 return s390_count_running_cpus();
362 void s390_cpu_unhalt(S390CPU *cpu)
364 CPUState *cs = CPU(cpu);
365 trace_cpu_unhalt(cs->cpu_index);
367 if (cs->halted) {
368 cs->halted = 0;
369 cs->exception_index = -1;
373 unsigned int s390_cpu_set_state(uint8_t cpu_state, S390CPU *cpu)
375 trace_cpu_set_state(CPU(cpu)->cpu_index, cpu_state);
377 switch (cpu_state) {
378 case S390_CPU_STATE_STOPPED:
379 case S390_CPU_STATE_CHECK_STOP:
380 /* halt the cpu for common infrastructure */
381 s390_cpu_halt(cpu);
382 break;
383 case S390_CPU_STATE_OPERATING:
384 case S390_CPU_STATE_LOAD:
386 * Starting a CPU with a PSW WAIT bit set:
387 * KVM: handles this internally and triggers another WAIT exit.
388 * TCG: will actually try to continue to run. Don't unhalt, will
389 * be done when the CPU actually has work (an interrupt).
391 if (!tcg_enabled() || !(cpu->env.psw.mask & PSW_MASK_WAIT)) {
392 s390_cpu_unhalt(cpu);
394 break;
395 default:
396 error_report("Requested CPU state is not a valid S390 CPU state: %u",
397 cpu_state);
398 exit(1);
400 if (kvm_enabled() && cpu->env.cpu_state != cpu_state) {
401 kvm_s390_set_cpu_state(cpu, cpu_state);
403 cpu->env.cpu_state = cpu_state;
405 return s390_count_running_cpus();
408 int s390_set_memory_limit(uint64_t new_limit, uint64_t *hw_limit)
410 if (kvm_enabled()) {
411 return kvm_s390_set_mem_limit(new_limit, hw_limit);
413 return 0;
416 void s390_set_max_pagesize(uint64_t pagesize, Error **errp)
418 if (kvm_enabled()) {
419 kvm_s390_set_max_pagesize(pagesize, errp);
423 void s390_cmma_reset(void)
425 if (kvm_enabled()) {
426 kvm_s390_cmma_reset();
430 int s390_assign_subch_ioeventfd(EventNotifier *notifier, uint32_t sch_id,
431 int vq, bool assign)
433 if (kvm_enabled()) {
434 return kvm_s390_assign_subch_ioeventfd(notifier, sch_id, vq, assign);
435 } else {
436 return 0;
440 void s390_crypto_reset(void)
442 if (kvm_enabled()) {
443 kvm_s390_crypto_reset();
447 void s390_enable_css_support(S390CPU *cpu)
449 if (kvm_enabled()) {
450 kvm_s390_enable_css_support(cpu);
454 void s390_do_cpu_set_diag318(CPUState *cs, run_on_cpu_data arg)
456 if (kvm_enabled()) {
457 kvm_s390_set_diag318(cs, arg.host_ulong);
460 #endif
462 static gchar *s390_gdb_arch_name(CPUState *cs)
464 return g_strdup("s390:64-bit");
467 static Property s390x_cpu_properties[] = {
468 #if !defined(CONFIG_USER_ONLY)
469 DEFINE_PROP_UINT32("core-id", S390CPU, env.core_id, 0),
470 #endif
471 DEFINE_PROP_END_OF_LIST()
474 static void s390_cpu_reset_full(DeviceState *dev)
476 CPUState *s = CPU(dev);
477 return s390_cpu_reset(s, S390_CPU_RESET_CLEAR);
480 #ifdef CONFIG_TCG
481 #include "hw/core/tcg-cpu-ops.h"
483 static struct TCGCPUOps s390_tcg_ops = {
484 .initialize = s390x_translate_init,
485 .tlb_fill = s390_cpu_tlb_fill,
487 #if !defined(CONFIG_USER_ONLY)
488 .cpu_exec_interrupt = s390_cpu_exec_interrupt,
489 .do_interrupt = s390_cpu_do_interrupt,
490 .debug_excp_handler = s390x_cpu_debug_excp_handler,
491 .do_unaligned_access = s390x_cpu_do_unaligned_access,
492 #endif /* !CONFIG_USER_ONLY */
494 #endif /* CONFIG_TCG */
496 static void s390_cpu_class_init(ObjectClass *oc, void *data)
498 S390CPUClass *scc = S390_CPU_CLASS(oc);
499 CPUClass *cc = CPU_CLASS(scc);
500 DeviceClass *dc = DEVICE_CLASS(oc);
502 device_class_set_parent_realize(dc, s390_cpu_realizefn,
503 &scc->parent_realize);
504 device_class_set_props(dc, s390x_cpu_properties);
505 dc->user_creatable = true;
507 device_class_set_parent_reset(dc, s390_cpu_reset_full, &scc->parent_reset);
508 #if !defined(CONFIG_USER_ONLY)
509 scc->load_normal = s390_cpu_load_normal;
510 #endif
511 scc->reset = s390_cpu_reset;
512 cc->class_by_name = s390_cpu_class_by_name,
513 cc->has_work = s390_cpu_has_work;
514 cc->dump_state = s390_cpu_dump_state;
515 cc->set_pc = s390_cpu_set_pc;
516 cc->gdb_read_register = s390_cpu_gdb_read_register;
517 cc->gdb_write_register = s390_cpu_gdb_write_register;
518 #ifndef CONFIG_USER_ONLY
519 cc->get_phys_page_debug = s390_cpu_get_phys_page_debug;
520 cc->vmsd = &vmstate_s390_cpu;
521 cc->get_crash_info = s390_cpu_get_crash_info;
522 cc->write_elf64_note = s390_cpu_write_elf64_note;
523 #endif
524 cc->disas_set_info = s390_cpu_disas_set_info;
525 cc->gdb_num_core_regs = S390_NUM_CORE_REGS;
526 cc->gdb_core_xml_file = "s390x-core64.xml";
527 cc->gdb_arch_name = s390_gdb_arch_name;
529 s390_cpu_model_class_register_props(oc);
531 #ifdef CONFIG_TCG
532 cc->tcg_ops = &s390_tcg_ops;
533 #endif /* CONFIG_TCG */
536 static const TypeInfo s390_cpu_type_info = {
537 .name = TYPE_S390_CPU,
538 .parent = TYPE_CPU,
539 .instance_size = sizeof(S390CPU),
540 .instance_align = __alignof__(S390CPU),
541 .instance_init = s390_cpu_initfn,
542 .instance_finalize = s390_cpu_finalize,
543 .abstract = true,
544 .class_size = sizeof(S390CPUClass),
545 .class_init = s390_cpu_class_init,
548 static void s390_cpu_register_types(void)
550 type_register_static(&s390_cpu_type_info);
553 type_init(s390_cpu_register_types)