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"
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"
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"
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
) {
69 if (!(cs
->interrupt_request
& CPU_INTERRUPT_HARD
)) {
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
);
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
;
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
);
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
);
116 case S390_CPU_RESET_CLEAR
:
117 memset(env
, 0, offsetof(CPUS390XState
, start_initial_reset_fields
));
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 */
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
;
140 /* tininess for underflow is detected before rounding */
141 set_float_detect_tininess(float_tininess_before_rounding
,
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;
154 g_assert_not_reached();
157 /* Reset state inside the kernel that we cannot access yet from QEMU. */
160 case S390_CPU_RESET_CLEAR
:
161 kvm_s390_reset_vcpu_clear(cpu
);
163 case S390_CPU_RESET_INITIAL
:
164 kvm_s390_reset_vcpu_initial(cpu
);
166 case S390_CPU_RESET_NORMAL
:
167 kvm_s390_reset_vcpu_normal(cpu
);
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
);
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
);
200 /* the model has to be realized before qemu_init_vcpu() due to kvm */
201 s390_realize_cpu_model(cs
, &err
);
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
,
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
);
222 /* sync cs->cpu_index and env->core_id. The latter is needed for TCG. */
223 cs
->cpu_index
= cpu
->env
.core_id
;
226 cpu_exec_realizefn(cs
, &err
);
231 #if !defined(CONFIG_USER_ONLY)
232 qemu_register_reset(s390_cpu_machine_reset_cb
, cpu
);
234 s390_cpu_gdb_init(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.
244 run_on_cpu(cs
, s390_do_cpu_full_reset
, RUN_ON_CPU_NULL
);
249 scc
->parent_realize(dev
, &err
);
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
;
272 static void s390_cpu_get_crash_info_qom(Object
*obj
, Visitor
*v
,
273 const char *name
, void *opaque
,
276 CPUState
*cs
= CPU(obj
);
277 GuestPanicInformation
*panic_info
;
279 if (!cs
->crash_occurred
) {
280 error_setg(errp
, "No crash occurred");
284 panic_info
= s390_cpu_get_crash_info(cs
);
286 visit_type_GuestPanicInformation(v
, "crash-information", &panic_info
,
288 qapi_free_GuestPanicInformation(panic_info
);
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
);
304 timer_new_ns(QEMU_CLOCK_VIRTUAL
, s390x_tod_timer
, cpu
);
306 timer_new_ns(QEMU_CLOCK_VIRTUAL
, s390x_cpu_timer
, cpu
);
307 s390_cpu_set_state(S390_CPU_STATE_STOPPED
, cpu
);
311 static void s390_cpu_finalize(Object
*obj
)
313 #if !defined(CONFIG_USER_ONLY)
314 S390CPU
*cpu
= S390_CPU(obj
);
316 timer_del(cpu
->env
.tod_timer
);
317 timer_free(cpu
->env
.tod_timer
);
318 timer_del(cpu
->env
.cpu_timer
);
319 timer_free(cpu
->env
.cpu_timer
);
321 qemu_unregister_reset(s390_cpu_machine_reset_cb
, cpu
);
322 g_free(cpu
->irqstate
);
326 #if !defined(CONFIG_USER_ONLY)
327 static bool disabled_wait(CPUState
*cpu
)
329 return cpu
->halted
&& !(S390_CPU(cpu
)->env
.psw
.mask
&
330 (PSW_MASK_IO
| PSW_MASK_EXT
| PSW_MASK_MCHECK
));
333 static unsigned s390_count_running_cpus(void)
339 uint8_t state
= S390_CPU(cpu
)->env
.cpu_state
;
340 if (state
== S390_CPU_STATE_OPERATING
||
341 state
== S390_CPU_STATE_LOAD
) {
342 if (!disabled_wait(cpu
)) {
351 unsigned int s390_cpu_halt(S390CPU
*cpu
)
353 CPUState
*cs
= CPU(cpu
);
354 trace_cpu_halt(cs
->cpu_index
);
358 cs
->exception_index
= EXCP_HLT
;
361 return s390_count_running_cpus();
364 void s390_cpu_unhalt(S390CPU
*cpu
)
366 CPUState
*cs
= CPU(cpu
);
367 trace_cpu_unhalt(cs
->cpu_index
);
371 cs
->exception_index
= -1;
375 unsigned int s390_cpu_set_state(uint8_t cpu_state
, S390CPU
*cpu
)
377 trace_cpu_set_state(CPU(cpu
)->cpu_index
, cpu_state
);
380 case S390_CPU_STATE_STOPPED
:
381 case S390_CPU_STATE_CHECK_STOP
:
382 /* halt the cpu for common infrastructure */
385 case S390_CPU_STATE_OPERATING
:
386 case S390_CPU_STATE_LOAD
:
388 * Starting a CPU with a PSW WAIT bit set:
389 * KVM: handles this internally and triggers another WAIT exit.
390 * TCG: will actually try to continue to run. Don't unhalt, will
391 * be done when the CPU actually has work (an interrupt).
393 if (!tcg_enabled() || !(cpu
->env
.psw
.mask
& PSW_MASK_WAIT
)) {
394 s390_cpu_unhalt(cpu
);
398 error_report("Requested CPU state is not a valid S390 CPU state: %u",
402 if (kvm_enabled() && cpu
->env
.cpu_state
!= cpu_state
) {
403 kvm_s390_set_cpu_state(cpu
, cpu_state
);
405 cpu
->env
.cpu_state
= cpu_state
;
407 return s390_count_running_cpus();
410 int s390_set_memory_limit(uint64_t new_limit
, uint64_t *hw_limit
)
413 return kvm_s390_set_mem_limit(new_limit
, hw_limit
);
418 void s390_set_max_pagesize(uint64_t pagesize
, Error
**errp
)
421 kvm_s390_set_max_pagesize(pagesize
, errp
);
425 void s390_cmma_reset(void)
428 kvm_s390_cmma_reset();
432 int s390_assign_subch_ioeventfd(EventNotifier
*notifier
, uint32_t sch_id
,
436 return kvm_s390_assign_subch_ioeventfd(notifier
, sch_id
, vq
, assign
);
442 void s390_crypto_reset(void)
445 kvm_s390_crypto_reset();
449 void s390_enable_css_support(S390CPU
*cpu
)
452 kvm_s390_enable_css_support(cpu
);
456 void s390_do_cpu_set_diag318(CPUState
*cs
, run_on_cpu_data arg
)
459 kvm_s390_set_diag318(cs
, arg
.host_ulong
);
464 static gchar
*s390_gdb_arch_name(CPUState
*cs
)
466 return g_strdup("s390:64-bit");
469 static Property s390x_cpu_properties
[] = {
470 #if !defined(CONFIG_USER_ONLY)
471 DEFINE_PROP_UINT32("core-id", S390CPU
, env
.core_id
, 0),
473 DEFINE_PROP_END_OF_LIST()
476 static void s390_cpu_reset_full(DeviceState
*dev
)
478 CPUState
*s
= CPU(dev
);
479 return s390_cpu_reset(s
, S390_CPU_RESET_CLEAR
);
482 static void s390_cpu_class_init(ObjectClass
*oc
, void *data
)
484 S390CPUClass
*scc
= S390_CPU_CLASS(oc
);
485 CPUClass
*cc
= CPU_CLASS(scc
);
486 DeviceClass
*dc
= DEVICE_CLASS(oc
);
488 device_class_set_parent_realize(dc
, s390_cpu_realizefn
,
489 &scc
->parent_realize
);
490 device_class_set_props(dc
, s390x_cpu_properties
);
491 dc
->user_creatable
= true;
493 device_class_set_parent_reset(dc
, s390_cpu_reset_full
, &scc
->parent_reset
);
494 #if !defined(CONFIG_USER_ONLY)
495 scc
->load_normal
= s390_cpu_load_normal
;
497 scc
->reset
= s390_cpu_reset
;
498 cc
->class_by_name
= s390_cpu_class_by_name
,
499 cc
->has_work
= s390_cpu_has_work
;
501 cc
->do_interrupt
= s390_cpu_do_interrupt
;
503 cc
->dump_state
= s390_cpu_dump_state
;
504 cc
->set_pc
= s390_cpu_set_pc
;
505 cc
->gdb_read_register
= s390_cpu_gdb_read_register
;
506 cc
->gdb_write_register
= s390_cpu_gdb_write_register
;
507 #ifndef CONFIG_USER_ONLY
508 cc
->get_phys_page_debug
= s390_cpu_get_phys_page_debug
;
509 cc
->vmsd
= &vmstate_s390_cpu
;
510 cc
->get_crash_info
= s390_cpu_get_crash_info
;
511 cc
->write_elf64_note
= s390_cpu_write_elf64_note
;
513 cc
->cpu_exec_interrupt
= s390_cpu_exec_interrupt
;
514 cc
->debug_excp_handler
= s390x_cpu_debug_excp_handler
;
515 cc
->do_unaligned_access
= s390x_cpu_do_unaligned_access
;
518 cc
->disas_set_info
= s390_cpu_disas_set_info
;
520 cc
->tcg_initialize
= s390x_translate_init
;
521 cc
->tlb_fill
= s390_cpu_tlb_fill
;
524 cc
->gdb_num_core_regs
= S390_NUM_CORE_REGS
;
525 cc
->gdb_core_xml_file
= "s390x-core64.xml";
526 cc
->gdb_arch_name
= s390_gdb_arch_name
;
528 s390_cpu_model_class_register_props(oc
);
531 static const TypeInfo s390_cpu_type_info
= {
532 .name
= TYPE_S390_CPU
,
534 .instance_size
= sizeof(S390CPU
),
535 .instance_align
= __alignof__(S390CPU
),
536 .instance_init
= s390_cpu_initfn
,
537 .instance_finalize
= s390_cpu_finalize
,
539 .class_size
= sizeof(S390CPUClass
),
540 .class_init
= s390_cpu_class_init
,
543 static void s390_cpu_register_types(void)
545 type_register_static(&s390_cpu_type_info
);
548 type_init(s390_cpu_register_types
)