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 library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library 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 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, see
21 * <http://www.gnu.org/licenses/lgpl-2.1.html>
22 * Contributions after 2012-12-11 are licensed under the terms of the
23 * GNU GPL, version 2 or (at your option) any later version.
26 #include "qemu/osdep.h"
27 #include "qapi/error.h"
30 #include "qemu-common.h"
31 #include "qemu/cutils.h"
32 #include "qemu/timer.h"
33 #include "qemu/error-report.h"
35 #include "qapi/visitor.h"
36 #include "exec/exec-all.h"
37 #ifndef CONFIG_USER_ONLY
39 #include "sysemu/arch_init.h"
40 #include "sysemu/sysemu.h"
41 #include "hw/s390x/sclp.h"
44 #define CR0_RESET 0xE0UL
45 #define CR14_RESET 0xC2000000UL;
47 static void s390_cpu_set_pc(CPUState
*cs
, vaddr value
)
49 S390CPU
*cpu
= S390_CPU(cs
);
51 cpu
->env
.psw
.addr
= value
;
54 static bool s390_cpu_has_work(CPUState
*cs
)
56 S390CPU
*cpu
= S390_CPU(cs
);
57 CPUS390XState
*env
= &cpu
->env
;
59 return (cs
->interrupt_request
& CPU_INTERRUPT_HARD
) &&
60 (env
->psw
.mask
& PSW_MASK_EXT
);
63 #if !defined(CONFIG_USER_ONLY)
64 /* S390CPUClass::load_normal() */
65 static void s390_cpu_load_normal(CPUState
*s
)
67 S390CPU
*cpu
= S390_CPU(s
);
68 cpu
->env
.psw
.addr
= ldl_phys(s
->as
, 4) & PSW_MASK_ESA_ADDR
;
69 cpu
->env
.psw
.mask
= PSW_MASK_32
| PSW_MASK_64
;
70 s390_cpu_set_state(CPU_STATE_OPERATING
, cpu
);
74 /* S390CPUClass::cpu_reset() */
75 static void s390_cpu_reset(CPUState
*s
)
77 S390CPU
*cpu
= S390_CPU(s
);
78 S390CPUClass
*scc
= S390_CPU_GET_CLASS(cpu
);
79 CPUS390XState
*env
= &cpu
->env
;
81 env
->pfault_token
= -1UL;
83 cpu
->env
.sigp_order
= 0;
84 s390_cpu_set_state(CPU_STATE_STOPPED
, cpu
);
87 /* S390CPUClass::initial_reset() */
88 static void s390_cpu_initial_reset(CPUState
*s
)
90 S390CPU
*cpu
= S390_CPU(s
);
91 CPUS390XState
*env
= &cpu
->env
;
95 /* initial reset does not clear everything! */
96 memset(&env
->start_initial_reset_fields
, 0,
97 offsetof(CPUS390XState
, end_reset_fields
) -
98 offsetof(CPUS390XState
, start_initial_reset_fields
));
100 /* architectured initial values for CR 0 and 14 */
101 env
->cregs
[0] = CR0_RESET
;
102 env
->cregs
[14] = CR14_RESET
;
104 /* architectured initial value for Breaking-Event-Address register */
107 env
->pfault_token
= -1UL;
109 for (i
= 0; i
< ARRAY_SIZE(env
->io_index
); i
++) {
110 env
->io_index
[i
] = -1;
113 /* tininess for underflow is detected before rounding */
114 set_float_detect_tininess(float_tininess_before_rounding
,
117 /* Reset state inside the kernel that we cannot access yet from QEMU. */
119 kvm_s390_reset_vcpu(cpu
);
123 /* CPUClass:reset() */
124 static void s390_cpu_full_reset(CPUState
*s
)
126 S390CPU
*cpu
= S390_CPU(s
);
127 S390CPUClass
*scc
= S390_CPU_GET_CLASS(cpu
);
128 CPUS390XState
*env
= &cpu
->env
;
131 scc
->parent_reset(s
);
132 cpu
->env
.sigp_order
= 0;
133 s390_cpu_set_state(CPU_STATE_STOPPED
, cpu
);
135 memset(env
, 0, offsetof(CPUS390XState
, end_reset_fields
));
137 /* architectured initial values for CR 0 and 14 */
138 env
->cregs
[0] = CR0_RESET
;
139 env
->cregs
[14] = CR14_RESET
;
141 /* architectured initial value for Breaking-Event-Address register */
144 env
->pfault_token
= -1UL;
146 for (i
= 0; i
< ARRAY_SIZE(env
->io_index
); i
++) {
147 env
->io_index
[i
] = -1;
150 /* tininess for underflow is detected before rounding */
151 set_float_detect_tininess(float_tininess_before_rounding
,
154 /* Reset state inside the kernel that we cannot access yet from QEMU. */
156 kvm_s390_reset_vcpu(cpu
);
160 #if !defined(CONFIG_USER_ONLY)
161 static void s390_cpu_machine_reset_cb(void *opaque
)
163 S390CPU
*cpu
= opaque
;
165 run_on_cpu(CPU(cpu
), s390_do_cpu_full_reset
, RUN_ON_CPU_NULL
);
169 static void s390_cpu_disas_set_info(CPUState
*cpu
, disassemble_info
*info
)
171 info
->mach
= bfd_mach_s390_64
;
172 info
->print_insn
= print_insn_s390
;
175 static void s390_cpu_realizefn(DeviceState
*dev
, Error
**errp
)
177 CPUState
*cs
= CPU(dev
);
178 S390CPUClass
*scc
= S390_CPU_GET_CLASS(dev
);
179 S390CPU
*cpu
= S390_CPU(dev
);
180 CPUS390XState
*env
= &cpu
->env
;
183 /* the model has to be realized before qemu_init_vcpu() due to kvm */
184 s390_realize_cpu_model(cs
, &err
);
189 #if !defined(CONFIG_USER_ONLY)
190 if (cpu
->id
>= max_cpus
) {
191 error_setg(&err
, "Unable to add CPU: %" PRIi64
192 ", max allowed: %d", cpu
->id
, max_cpus
- 1);
196 if (cpu_exists(cpu
->id
)) {
197 error_setg(&err
, "Unable to add CPU: %" PRIi64
198 ", it already exists", cpu
->id
);
201 if (cpu
->id
!= scc
->next_cpu_id
) {
202 error_setg(&err
, "Unable to add CPU: %" PRIi64
203 ", The next available id is %" PRIi64
, cpu
->id
,
208 cpu_exec_realizefn(cs
, &err
);
214 #if !defined(CONFIG_USER_ONLY)
215 qemu_register_reset(s390_cpu_machine_reset_cb
, cpu
);
217 env
->cpu_num
= cpu
->id
;
218 s390_cpu_gdb_init(cs
);
220 #if !defined(CONFIG_USER_ONLY)
221 run_on_cpu(cs
, s390_do_cpu_full_reset
, RUN_ON_CPU_NULL
);
226 scc
->parent_realize(dev
, &err
);
228 #if !defined(CONFIG_USER_ONLY)
229 if (dev
->hotplugged
) {
230 raise_irq_cpu_hotplug();
235 error_propagate(errp
, err
);
238 static void s390x_cpu_get_id(Object
*obj
, Visitor
*v
, const char *name
,
239 void *opaque
, Error
**errp
)
241 S390CPU
*cpu
= S390_CPU(obj
);
242 int64_t value
= cpu
->id
;
244 visit_type_int(v
, name
, &value
, errp
);
247 static void s390x_cpu_set_id(Object
*obj
, Visitor
*v
, const char *name
,
248 void *opaque
, Error
**errp
)
250 S390CPU
*cpu
= S390_CPU(obj
);
251 DeviceState
*dev
= DEVICE(obj
);
252 const int64_t min
= 0;
253 const int64_t max
= UINT32_MAX
;
258 error_setg(errp
, "Attempt to set property '%s' on '%s' after "
259 "it was realized", name
, object_get_typename(obj
));
263 visit_type_int(v
, name
, &value
, &err
);
265 error_propagate(errp
, err
);
268 if (value
< min
|| value
> max
) {
269 error_setg(errp
, "Property %s.%s doesn't take value %" PRId64
270 " (minimum: %" PRId64
", maximum: %" PRId64
")" ,
271 object_get_typename(obj
), name
, value
, min
, max
);
277 static void s390_cpu_initfn(Object
*obj
)
279 CPUState
*cs
= CPU(obj
);
280 S390CPU
*cpu
= S390_CPU(obj
);
281 CPUS390XState
*env
= &cpu
->env
;
283 #if !defined(CONFIG_USER_ONLY)
289 cs
->exception_index
= EXCP_HLT
;
290 object_property_add(OBJECT(cpu
), "id", "int64_t", s390x_cpu_get_id
,
291 s390x_cpu_set_id
, NULL
, NULL
, NULL
);
292 s390_cpu_model_register_props(obj
);
293 #if !defined(CONFIG_USER_ONLY)
294 qemu_get_timedate(&tm
, 0);
295 env
->tod_offset
= TOD_UNIX_EPOCH
+
296 (time2tod(mktimegm(&tm
)) * 1000000000ULL);
297 env
->tod_basetime
= 0;
298 env
->tod_timer
= timer_new_ns(QEMU_CLOCK_VIRTUAL
, s390x_tod_timer
, cpu
);
299 env
->cpu_timer
= timer_new_ns(QEMU_CLOCK_VIRTUAL
, s390x_cpu_timer
, cpu
);
300 s390_cpu_set_state(CPU_STATE_STOPPED
, cpu
);
303 if (tcg_enabled() && !inited
) {
305 s390x_translate_init();
309 static void s390_cpu_finalize(Object
*obj
)
311 #if !defined(CONFIG_USER_ONLY)
312 S390CPU
*cpu
= S390_CPU(obj
);
314 qemu_unregister_reset(s390_cpu_machine_reset_cb
, cpu
);
315 g_free(cpu
->irqstate
);
319 #if !defined(CONFIG_USER_ONLY)
320 static bool disabled_wait(CPUState
*cpu
)
322 return cpu
->halted
&& !(S390_CPU(cpu
)->env
.psw
.mask
&
323 (PSW_MASK_IO
| PSW_MASK_EXT
| PSW_MASK_MCHECK
));
326 static unsigned s390_count_running_cpus(void)
332 uint8_t state
= S390_CPU(cpu
)->env
.cpu_state
;
333 if (state
== CPU_STATE_OPERATING
||
334 state
== CPU_STATE_LOAD
) {
335 if (!disabled_wait(cpu
)) {
344 unsigned int s390_cpu_halt(S390CPU
*cpu
)
346 CPUState
*cs
= CPU(cpu
);
347 trace_cpu_halt(cs
->cpu_index
);
351 cs
->exception_index
= EXCP_HLT
;
354 return s390_count_running_cpus();
357 void s390_cpu_unhalt(S390CPU
*cpu
)
359 CPUState
*cs
= CPU(cpu
);
360 trace_cpu_unhalt(cs
->cpu_index
);
364 cs
->exception_index
= -1;
368 unsigned int s390_cpu_set_state(uint8_t cpu_state
, S390CPU
*cpu
)
370 trace_cpu_set_state(CPU(cpu
)->cpu_index
, cpu_state
);
373 case CPU_STATE_STOPPED
:
374 case CPU_STATE_CHECK_STOP
:
375 /* halt the cpu for common infrastructure */
378 case CPU_STATE_OPERATING
:
380 /* unhalt the cpu for common infrastructure */
381 s390_cpu_unhalt(cpu
);
384 error_report("Requested CPU state is not a valid S390 CPU state: %u",
388 if (kvm_enabled() && cpu
->env
.cpu_state
!= cpu_state
) {
389 kvm_s390_set_cpu_state(cpu
, cpu_state
);
391 cpu
->env
.cpu_state
= cpu_state
;
393 return s390_count_running_cpus();
396 int s390_get_clock(uint8_t *tod_high
, uint64_t *tod_low
)
399 return kvm_s390_get_clock(tod_high
, tod_low
);
407 int s390_set_clock(uint8_t *tod_high
, uint64_t *tod_low
)
410 return kvm_s390_set_clock(tod_high
, tod_low
);
416 int s390_set_memory_limit(uint64_t new_limit
, uint64_t *hw_limit
)
419 return kvm_s390_set_mem_limit(new_limit
, hw_limit
);
424 void s390_cmma_reset(void)
427 kvm_s390_cmma_reset();
431 int s390_cpu_restart(S390CPU
*cpu
)
434 return kvm_s390_cpu_restart(cpu
);
439 int s390_get_memslot_count(void)
442 return kvm_s390_get_memslot_count();
444 return MAX_AVAIL_SLOTS
;
448 int s390_assign_subch_ioeventfd(EventNotifier
*notifier
, uint32_t sch_id
,
452 return kvm_s390_assign_subch_ioeventfd(notifier
, sch_id
, vq
, assign
);
458 void s390_crypto_reset(void)
461 kvm_s390_crypto_reset();
465 bool s390_get_squash_mcss(void)
467 if (object_property_get_bool(OBJECT(qdev_get_machine()), "s390-squash-mcss",
475 void s390_enable_css_support(S390CPU
*cpu
)
478 kvm_s390_enable_css_support(cpu
);
483 static gchar
*s390_gdb_arch_name(CPUState
*cs
)
485 return g_strdup("s390:64-bit");
488 static void s390_cpu_class_init(ObjectClass
*oc
, void *data
)
490 S390CPUClass
*scc
= S390_CPU_CLASS(oc
);
491 CPUClass
*cc
= CPU_CLASS(scc
);
492 DeviceClass
*dc
= DEVICE_CLASS(oc
);
494 scc
->next_cpu_id
= 0;
495 scc
->parent_realize
= dc
->realize
;
496 dc
->realize
= s390_cpu_realizefn
;
498 scc
->parent_reset
= cc
->reset
;
499 #if !defined(CONFIG_USER_ONLY)
500 scc
->load_normal
= s390_cpu_load_normal
;
502 scc
->cpu_reset
= s390_cpu_reset
;
503 scc
->initial_cpu_reset
= s390_cpu_initial_reset
;
504 cc
->reset
= s390_cpu_full_reset
;
505 cc
->class_by_name
= s390_cpu_class_by_name
,
506 cc
->has_work
= s390_cpu_has_work
;
508 cc
->do_interrupt
= s390_cpu_do_interrupt
;
510 cc
->dump_state
= s390_cpu_dump_state
;
511 cc
->set_pc
= s390_cpu_set_pc
;
512 cc
->gdb_read_register
= s390_cpu_gdb_read_register
;
513 cc
->gdb_write_register
= s390_cpu_gdb_write_register
;
514 #ifdef CONFIG_USER_ONLY
515 cc
->handle_mmu_fault
= s390_cpu_handle_mmu_fault
;
517 cc
->get_phys_page_debug
= s390_cpu_get_phys_page_debug
;
518 cc
->vmsd
= &vmstate_s390_cpu
;
519 cc
->write_elf64_note
= s390_cpu_write_elf64_note
;
521 cc
->cpu_exec_interrupt
= s390_cpu_exec_interrupt
;
522 cc
->debug_excp_handler
= s390x_cpu_debug_excp_handler
;
523 cc
->do_unaligned_access
= s390x_cpu_do_unaligned_access
;
526 cc
->disas_set_info
= s390_cpu_disas_set_info
;
528 cc
->gdb_num_core_regs
= S390_NUM_CORE_REGS
;
529 cc
->gdb_core_xml_file
= "s390x-core64.xml";
530 cc
->gdb_arch_name
= s390_gdb_arch_name
;
532 s390_cpu_model_class_register_props(oc
);
535 static const TypeInfo s390_cpu_type_info
= {
536 .name
= TYPE_S390_CPU
,
538 .instance_size
= sizeof(S390CPU
),
539 .instance_init
= s390_cpu_initfn
,
540 .instance_finalize
= s390_cpu_finalize
,
542 .class_size
= sizeof(S390CPUClass
),
543 .class_init
= s390_cpu_class_init
,
546 static void s390_cpu_register_types(void)
548 type_register_static(&s390_cpu_type_info
);
551 type_init(s390_cpu_register_types
)