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"
29 #include "qemu-common.h"
30 #include "qemu/cutils.h"
31 #include "qemu/timer.h"
32 #include "qemu/error-report.h"
34 #include "qapi/visitor.h"
35 #include "migration/vmstate.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 touch regs,fregs and aregs */
96 memset(&env
->fpc
, 0, offsetof(CPUS390XState
, end_reset_fields
) -
97 offsetof(CPUS390XState
, fpc
));
99 /* architectured initial values for CR 0 and 14 */
100 env
->cregs
[0] = CR0_RESET
;
101 env
->cregs
[14] = CR14_RESET
;
103 /* architectured initial value for Breaking-Event-Address register */
106 env
->pfault_token
= -1UL;
108 for (i
= 0; i
< ARRAY_SIZE(env
->io_index
); i
++) {
109 env
->io_index
[i
] = -1;
112 /* tininess for underflow is detected before rounding */
113 set_float_detect_tininess(float_tininess_before_rounding
,
116 /* Reset state inside the kernel that we cannot access yet from QEMU. */
118 kvm_s390_reset_vcpu(cpu
);
122 /* CPUClass:reset() */
123 static void s390_cpu_full_reset(CPUState
*s
)
125 S390CPU
*cpu
= S390_CPU(s
);
126 S390CPUClass
*scc
= S390_CPU_GET_CLASS(cpu
);
127 CPUS390XState
*env
= &cpu
->env
;
130 scc
->parent_reset(s
);
131 cpu
->env
.sigp_order
= 0;
132 s390_cpu_set_state(CPU_STATE_STOPPED
, cpu
);
134 memset(env
, 0, offsetof(CPUS390XState
, end_reset_fields
));
136 /* architectured initial values for CR 0 and 14 */
137 env
->cregs
[0] = CR0_RESET
;
138 env
->cregs
[14] = CR14_RESET
;
140 /* architectured initial value for Breaking-Event-Address register */
143 env
->pfault_token
= -1UL;
145 for (i
= 0; i
< ARRAY_SIZE(env
->io_index
); i
++) {
146 env
->io_index
[i
] = -1;
149 /* tininess for underflow is detected before rounding */
150 set_float_detect_tininess(float_tininess_before_rounding
,
153 /* Reset state inside the kernel that we cannot access yet from QEMU. */
155 kvm_s390_reset_vcpu(cpu
);
159 #if !defined(CONFIG_USER_ONLY)
160 static void s390_cpu_machine_reset_cb(void *opaque
)
162 S390CPU
*cpu
= opaque
;
164 run_on_cpu(CPU(cpu
), s390_do_cpu_full_reset
, RUN_ON_CPU_NULL
);
168 static void s390_cpu_disas_set_info(CPUState
*cpu
, disassemble_info
*info
)
170 info
->mach
= bfd_mach_s390_64
;
171 info
->print_insn
= print_insn_s390
;
174 static void s390_cpu_realizefn(DeviceState
*dev
, Error
**errp
)
176 CPUState
*cs
= CPU(dev
);
177 S390CPUClass
*scc
= S390_CPU_GET_CLASS(dev
);
178 S390CPU
*cpu
= S390_CPU(dev
);
179 CPUS390XState
*env
= &cpu
->env
;
182 /* the model has to be realized before qemu_init_vcpu() due to kvm */
183 s390_realize_cpu_model(cs
, &err
);
188 #if !defined(CONFIG_USER_ONLY)
189 if (cpu
->id
>= max_cpus
) {
190 error_setg(&err
, "Unable to add CPU: %" PRIi64
191 ", max allowed: %d", cpu
->id
, max_cpus
- 1);
195 if (cpu_exists(cpu
->id
)) {
196 error_setg(&err
, "Unable to add CPU: %" PRIi64
197 ", it already exists", cpu
->id
);
200 if (cpu
->id
!= scc
->next_cpu_id
) {
201 error_setg(&err
, "Unable to add CPU: %" PRIi64
202 ", The next available id is %" PRIi64
, cpu
->id
,
207 cpu_exec_realizefn(cs
, &err
);
213 #if !defined(CONFIG_USER_ONLY)
214 qemu_register_reset(s390_cpu_machine_reset_cb
, cpu
);
216 env
->cpu_num
= cpu
->id
;
217 s390_cpu_gdb_init(cs
);
219 #if !defined(CONFIG_USER_ONLY)
220 run_on_cpu(cs
, s390_do_cpu_full_reset
, RUN_ON_CPU_NULL
);
225 scc
->parent_realize(dev
, &err
);
227 #if !defined(CONFIG_USER_ONLY)
228 if (dev
->hotplugged
) {
229 raise_irq_cpu_hotplug();
234 error_propagate(errp
, err
);
237 static void s390x_cpu_get_id(Object
*obj
, Visitor
*v
, const char *name
,
238 void *opaque
, Error
**errp
)
240 S390CPU
*cpu
= S390_CPU(obj
);
241 int64_t value
= cpu
->id
;
243 visit_type_int(v
, name
, &value
, errp
);
246 static void s390x_cpu_set_id(Object
*obj
, Visitor
*v
, const char *name
,
247 void *opaque
, Error
**errp
)
249 S390CPU
*cpu
= S390_CPU(obj
);
250 DeviceState
*dev
= DEVICE(obj
);
251 const int64_t min
= 0;
252 const int64_t max
= UINT32_MAX
;
257 error_setg(errp
, "Attempt to set property '%s' on '%s' after "
258 "it was realized", name
, object_get_typename(obj
));
262 visit_type_int(v
, name
, &value
, &err
);
264 error_propagate(errp
, err
);
267 if (value
< min
|| value
> max
) {
268 error_setg(errp
, "Property %s.%s doesn't take value %" PRId64
269 " (minimum: %" PRId64
", maximum: %" PRId64
")" ,
270 object_get_typename(obj
), name
, value
, min
, max
);
276 static void s390_cpu_initfn(Object
*obj
)
278 CPUState
*cs
= CPU(obj
);
279 S390CPU
*cpu
= S390_CPU(obj
);
280 CPUS390XState
*env
= &cpu
->env
;
282 #if !defined(CONFIG_USER_ONLY)
288 cs
->exception_index
= EXCP_HLT
;
289 object_property_add(OBJECT(cpu
), "id", "int64_t", s390x_cpu_get_id
,
290 s390x_cpu_set_id
, NULL
, NULL
, NULL
);
291 s390_cpu_model_register_props(obj
);
292 #if !defined(CONFIG_USER_ONLY)
293 qemu_get_timedate(&tm
, 0);
294 env
->tod_offset
= TOD_UNIX_EPOCH
+
295 (time2tod(mktimegm(&tm
)) * 1000000000ULL);
296 env
->tod_basetime
= 0;
297 env
->tod_timer
= timer_new_ns(QEMU_CLOCK_VIRTUAL
, s390x_tod_timer
, cpu
);
298 env
->cpu_timer
= timer_new_ns(QEMU_CLOCK_VIRTUAL
, s390x_cpu_timer
, cpu
);
299 s390_cpu_set_state(CPU_STATE_STOPPED
, cpu
);
302 if (tcg_enabled() && !inited
) {
304 s390x_translate_init();
308 static void s390_cpu_finalize(Object
*obj
)
310 #if !defined(CONFIG_USER_ONLY)
311 S390CPU
*cpu
= S390_CPU(obj
);
313 qemu_unregister_reset(s390_cpu_machine_reset_cb
, cpu
);
314 g_free(cpu
->irqstate
);
318 #if !defined(CONFIG_USER_ONLY)
319 static bool disabled_wait(CPUState
*cpu
)
321 return cpu
->halted
&& !(S390_CPU(cpu
)->env
.psw
.mask
&
322 (PSW_MASK_IO
| PSW_MASK_EXT
| PSW_MASK_MCHECK
));
325 static unsigned s390_count_running_cpus(void)
331 uint8_t state
= S390_CPU(cpu
)->env
.cpu_state
;
332 if (state
== CPU_STATE_OPERATING
||
333 state
== CPU_STATE_LOAD
) {
334 if (!disabled_wait(cpu
)) {
343 unsigned int s390_cpu_halt(S390CPU
*cpu
)
345 CPUState
*cs
= CPU(cpu
);
346 trace_cpu_halt(cs
->cpu_index
);
350 cs
->exception_index
= EXCP_HLT
;
353 return s390_count_running_cpus();
356 void s390_cpu_unhalt(S390CPU
*cpu
)
358 CPUState
*cs
= CPU(cpu
);
359 trace_cpu_unhalt(cs
->cpu_index
);
363 cs
->exception_index
= -1;
367 unsigned int s390_cpu_set_state(uint8_t cpu_state
, S390CPU
*cpu
)
369 trace_cpu_set_state(CPU(cpu
)->cpu_index
, cpu_state
);
372 case CPU_STATE_STOPPED
:
373 case CPU_STATE_CHECK_STOP
:
374 /* halt the cpu for common infrastructure */
377 case CPU_STATE_OPERATING
:
379 /* unhalt the cpu for common infrastructure */
380 s390_cpu_unhalt(cpu
);
383 error_report("Requested CPU state is not a valid S390 CPU state: %u",
387 if (kvm_enabled() && cpu
->env
.cpu_state
!= cpu_state
) {
388 kvm_s390_set_cpu_state(cpu
, cpu_state
);
390 cpu
->env
.cpu_state
= cpu_state
;
392 return s390_count_running_cpus();
396 static gchar
*s390_gdb_arch_name(CPUState
*cs
)
398 return g_strdup("s390:64-bit");
401 static void s390_cpu_class_init(ObjectClass
*oc
, void *data
)
403 S390CPUClass
*scc
= S390_CPU_CLASS(oc
);
404 CPUClass
*cc
= CPU_CLASS(scc
);
405 DeviceClass
*dc
= DEVICE_CLASS(oc
);
407 scc
->next_cpu_id
= 0;
408 scc
->parent_realize
= dc
->realize
;
409 dc
->realize
= s390_cpu_realizefn
;
411 scc
->parent_reset
= cc
->reset
;
412 #if !defined(CONFIG_USER_ONLY)
413 scc
->load_normal
= s390_cpu_load_normal
;
415 scc
->cpu_reset
= s390_cpu_reset
;
416 scc
->initial_cpu_reset
= s390_cpu_initial_reset
;
417 cc
->reset
= s390_cpu_full_reset
;
418 cc
->class_by_name
= s390_cpu_class_by_name
,
419 cc
->has_work
= s390_cpu_has_work
;
420 cc
->do_interrupt
= s390_cpu_do_interrupt
;
421 cc
->dump_state
= s390_cpu_dump_state
;
422 cc
->set_pc
= s390_cpu_set_pc
;
423 cc
->gdb_read_register
= s390_cpu_gdb_read_register
;
424 cc
->gdb_write_register
= s390_cpu_gdb_write_register
;
425 #ifdef CONFIG_USER_ONLY
426 cc
->handle_mmu_fault
= s390_cpu_handle_mmu_fault
;
428 cc
->get_phys_page_debug
= s390_cpu_get_phys_page_debug
;
429 cc
->vmsd
= &vmstate_s390_cpu
;
430 cc
->write_elf64_note
= s390_cpu_write_elf64_note
;
431 cc
->cpu_exec_interrupt
= s390_cpu_exec_interrupt
;
432 cc
->debug_excp_handler
= s390x_cpu_debug_excp_handler
;
434 cc
->disas_set_info
= s390_cpu_disas_set_info
;
436 cc
->gdb_num_core_regs
= S390_NUM_CORE_REGS
;
437 cc
->gdb_core_xml_file
= "s390x-core64.xml";
438 cc
->gdb_arch_name
= s390_gdb_arch_name
;
440 s390_cpu_model_class_register_props(oc
);
443 static const TypeInfo s390_cpu_type_info
= {
444 .name
= TYPE_S390_CPU
,
446 .instance_size
= sizeof(S390CPU
),
447 .instance_init
= s390_cpu_initfn
,
448 .instance_finalize
= s390_cpu_finalize
,
450 .class_size
= sizeof(S390CPUClass
),
451 .class_init
= s390_cpu_class_init
,
454 static void s390_cpu_register_types(void)
456 type_register_static(&s390_cpu_type_info
);
459 type_init(s390_cpu_register_types
)