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"
28 #include "qemu-common.h"
29 #include "qemu/timer.h"
30 #include "qemu/error-report.h"
33 #include "qapi/visitor.h"
34 #ifndef CONFIG_USER_ONLY
35 #include "sysemu/arch_init.h"
36 #include "sysemu/sysemu.h"
37 #include "hw/s390x/sclp.h"
40 #define CR0_RESET 0xE0UL
41 #define CR14_RESET 0xC2000000UL;
43 /* generate CPU information for cpu -? */
44 void s390_cpu_list(FILE *f
, fprintf_function cpu_fprintf
)
47 (*cpu_fprintf
)(f
, "s390 %16s\n", "host");
51 #ifndef CONFIG_USER_ONLY
52 CpuDefinitionInfoList
*arch_query_cpu_definitions(Error
**errp
)
54 CpuDefinitionInfoList
*entry
;
55 CpuDefinitionInfo
*info
;
57 info
= g_malloc0(sizeof(*info
));
58 info
->name
= g_strdup("host");
60 entry
= g_malloc0(sizeof(*entry
));
67 static void s390_cpu_set_pc(CPUState
*cs
, vaddr value
)
69 S390CPU
*cpu
= S390_CPU(cs
);
71 cpu
->env
.psw
.addr
= value
;
74 static bool s390_cpu_has_work(CPUState
*cs
)
76 S390CPU
*cpu
= S390_CPU(cs
);
77 CPUS390XState
*env
= &cpu
->env
;
79 return (cs
->interrupt_request
& CPU_INTERRUPT_HARD
) &&
80 (env
->psw
.mask
& PSW_MASK_EXT
);
83 #if !defined(CONFIG_USER_ONLY)
84 /* S390CPUClass::load_normal() */
85 static void s390_cpu_load_normal(CPUState
*s
)
87 S390CPU
*cpu
= S390_CPU(s
);
88 cpu
->env
.psw
.addr
= ldl_phys(s
->as
, 4) & PSW_MASK_ESA_ADDR
;
89 cpu
->env
.psw
.mask
= PSW_MASK_32
| PSW_MASK_64
;
90 s390_cpu_set_state(CPU_STATE_OPERATING
, cpu
);
94 /* S390CPUClass::cpu_reset() */
95 static void s390_cpu_reset(CPUState
*s
)
97 S390CPU
*cpu
= S390_CPU(s
);
98 S390CPUClass
*scc
= S390_CPU_GET_CLASS(cpu
);
99 CPUS390XState
*env
= &cpu
->env
;
101 env
->pfault_token
= -1UL;
102 scc
->parent_reset(s
);
103 cpu
->env
.sigp_order
= 0;
104 s390_cpu_set_state(CPU_STATE_STOPPED
, cpu
);
108 /* S390CPUClass::initial_reset() */
109 static void s390_cpu_initial_reset(CPUState
*s
)
111 S390CPU
*cpu
= S390_CPU(s
);
112 CPUS390XState
*env
= &cpu
->env
;
116 /* initial reset does not touch regs,fregs and aregs */
117 memset(&env
->fpc
, 0, offsetof(CPUS390XState
, cpu_num
) -
118 offsetof(CPUS390XState
, fpc
));
120 /* architectured initial values for CR 0 and 14 */
121 env
->cregs
[0] = CR0_RESET
;
122 env
->cregs
[14] = CR14_RESET
;
124 /* architectured initial value for Breaking-Event-Address register */
127 env
->pfault_token
= -1UL;
129 for (i
= 0; i
< ARRAY_SIZE(env
->io_index
); i
++) {
130 env
->io_index
[i
] = -1;
133 /* tininess for underflow is detected before rounding */
134 set_float_detect_tininess(float_tininess_before_rounding
,
137 /* Reset state inside the kernel that we cannot access yet from QEMU. */
139 kvm_s390_reset_vcpu(cpu
);
144 /* CPUClass:reset() */
145 static void s390_cpu_full_reset(CPUState
*s
)
147 S390CPU
*cpu
= S390_CPU(s
);
148 S390CPUClass
*scc
= S390_CPU_GET_CLASS(cpu
);
149 CPUS390XState
*env
= &cpu
->env
;
152 scc
->parent_reset(s
);
153 cpu
->env
.sigp_order
= 0;
154 s390_cpu_set_state(CPU_STATE_STOPPED
, cpu
);
156 memset(env
, 0, offsetof(CPUS390XState
, cpu_num
));
158 /* architectured initial values for CR 0 and 14 */
159 env
->cregs
[0] = CR0_RESET
;
160 env
->cregs
[14] = CR14_RESET
;
162 /* architectured initial value for Breaking-Event-Address register */
165 env
->pfault_token
= -1UL;
167 for (i
= 0; i
< ARRAY_SIZE(env
->io_index
); i
++) {
168 env
->io_index
[i
] = -1;
171 /* tininess for underflow is detected before rounding */
172 set_float_detect_tininess(float_tininess_before_rounding
,
175 /* Reset state inside the kernel that we cannot access yet from QEMU. */
177 kvm_s390_reset_vcpu(cpu
);
182 #if !defined(CONFIG_USER_ONLY)
183 static void s390_cpu_machine_reset_cb(void *opaque
)
185 S390CPU
*cpu
= opaque
;
187 run_on_cpu(CPU(cpu
), s390_do_cpu_full_reset
, CPU(cpu
));
191 static void s390_cpu_disas_set_info(CPUState
*cpu
, disassemble_info
*info
)
193 info
->mach
= bfd_mach_s390_64
;
194 info
->print_insn
= print_insn_s390
;
197 static void s390_cpu_realizefn(DeviceState
*dev
, Error
**errp
)
199 CPUState
*cs
= CPU(dev
);
200 S390CPUClass
*scc
= S390_CPU_GET_CLASS(dev
);
201 S390CPU
*cpu
= S390_CPU(dev
);
202 CPUS390XState
*env
= &cpu
->env
;
205 #if !defined(CONFIG_USER_ONLY)
206 if (cpu
->id
>= max_cpus
) {
207 error_setg(&err
, "Unable to add CPU: %" PRIi64
208 ", max allowed: %d", cpu
->id
, max_cpus
- 1);
212 if (cpu_exists(cpu
->id
)) {
213 error_setg(&err
, "Unable to add CPU: %" PRIi64
214 ", it already exists", cpu
->id
);
217 if (cpu
->id
!= scc
->next_cpu_id
) {
218 error_setg(&err
, "Unable to add CPU: %" PRIi64
219 ", The next available id is %" PRIi64
, cpu
->id
,
224 cpu_exec_init(cs
, &err
);
230 #if !defined(CONFIG_USER_ONLY)
231 qemu_register_reset(s390_cpu_machine_reset_cb
, cpu
);
233 env
->cpu_num
= cpu
->id
;
234 s390_cpu_gdb_init(cs
);
236 #if !defined(CONFIG_USER_ONLY)
237 run_on_cpu(cs
, s390_do_cpu_full_reset
, cs
);
242 scc
->parent_realize(dev
, &err
);
244 #if !defined(CONFIG_USER_ONLY)
245 if (dev
->hotplugged
) {
246 raise_irq_cpu_hotplug();
251 error_propagate(errp
, err
);
254 static void s390x_cpu_get_id(Object
*obj
, Visitor
*v
, const char *name
,
255 void *opaque
, Error
**errp
)
257 S390CPU
*cpu
= S390_CPU(obj
);
258 int64_t value
= cpu
->id
;
260 visit_type_int(v
, name
, &value
, errp
);
263 static void s390x_cpu_set_id(Object
*obj
, Visitor
*v
, const char *name
,
264 void *opaque
, Error
**errp
)
266 S390CPU
*cpu
= S390_CPU(obj
);
267 DeviceState
*dev
= DEVICE(obj
);
268 const int64_t min
= 0;
269 const int64_t max
= UINT32_MAX
;
274 error_setg(errp
, "Attempt to set property '%s' on '%s' after "
275 "it was realized", name
, object_get_typename(obj
));
279 visit_type_int(v
, name
, &value
, &err
);
281 error_propagate(errp
, err
);
284 if (value
< min
|| value
> max
) {
285 error_setg(errp
, "Property %s.%s doesn't take value %" PRId64
286 " (minimum: %" PRId64
", maximum: %" PRId64
")" ,
287 object_get_typename(obj
), name
, value
, min
, max
);
293 static void s390_cpu_initfn(Object
*obj
)
295 CPUState
*cs
= CPU(obj
);
296 S390CPU
*cpu
= S390_CPU(obj
);
297 CPUS390XState
*env
= &cpu
->env
;
299 #if !defined(CONFIG_USER_ONLY)
305 cs
->exception_index
= EXCP_HLT
;
306 object_property_add(OBJECT(cpu
), "id", "int64_t", s390x_cpu_get_id
,
307 s390x_cpu_set_id
, NULL
, NULL
, NULL
);
308 #if !defined(CONFIG_USER_ONLY)
309 qemu_get_timedate(&tm
, 0);
310 env
->tod_offset
= TOD_UNIX_EPOCH
+
311 (time2tod(mktimegm(&tm
)) * 1000000000ULL);
312 env
->tod_basetime
= 0;
313 env
->tod_timer
= timer_new_ns(QEMU_CLOCK_VIRTUAL
, s390x_tod_timer
, cpu
);
314 env
->cpu_timer
= timer_new_ns(QEMU_CLOCK_VIRTUAL
, s390x_cpu_timer
, cpu
);
315 s390_cpu_set_state(CPU_STATE_STOPPED
, cpu
);
318 if (tcg_enabled() && !inited
) {
320 s390x_translate_init();
324 static void s390_cpu_finalize(Object
*obj
)
326 #if !defined(CONFIG_USER_ONLY)
327 S390CPU
*cpu
= S390_CPU(obj
);
329 qemu_unregister_reset(s390_cpu_machine_reset_cb
, cpu
);
330 g_free(cpu
->irqstate
);
334 #if !defined(CONFIG_USER_ONLY)
335 static bool disabled_wait(CPUState
*cpu
)
337 return cpu
->halted
&& !(S390_CPU(cpu
)->env
.psw
.mask
&
338 (PSW_MASK_IO
| PSW_MASK_EXT
| PSW_MASK_MCHECK
));
341 static unsigned s390_count_running_cpus(void)
347 uint8_t state
= S390_CPU(cpu
)->env
.cpu_state
;
348 if (state
== CPU_STATE_OPERATING
||
349 state
== CPU_STATE_LOAD
) {
350 if (!disabled_wait(cpu
)) {
359 unsigned int s390_cpu_halt(S390CPU
*cpu
)
361 CPUState
*cs
= CPU(cpu
);
362 trace_cpu_halt(cs
->cpu_index
);
366 cs
->exception_index
= EXCP_HLT
;
369 return s390_count_running_cpus();
372 void s390_cpu_unhalt(S390CPU
*cpu
)
374 CPUState
*cs
= CPU(cpu
);
375 trace_cpu_unhalt(cs
->cpu_index
);
379 cs
->exception_index
= -1;
383 unsigned int s390_cpu_set_state(uint8_t cpu_state
, S390CPU
*cpu
)
385 trace_cpu_set_state(CPU(cpu
)->cpu_index
, cpu_state
);
388 case CPU_STATE_STOPPED
:
389 case CPU_STATE_CHECK_STOP
:
390 /* halt the cpu for common infrastructure */
393 case CPU_STATE_OPERATING
:
395 /* unhalt the cpu for common infrastructure */
396 s390_cpu_unhalt(cpu
);
399 error_report("Requested CPU state is not a valid S390 CPU state: %u",
403 if (kvm_enabled() && cpu
->env
.cpu_state
!= cpu_state
) {
404 kvm_s390_set_cpu_state(cpu
, cpu_state
);
406 cpu
->env
.cpu_state
= cpu_state
;
408 return s390_count_running_cpus();
412 static gchar
*s390_gdb_arch_name(CPUState
*cs
)
414 return g_strdup("s390:64-bit");
417 static void s390_cpu_class_init(ObjectClass
*oc
, void *data
)
419 S390CPUClass
*scc
= S390_CPU_CLASS(oc
);
420 CPUClass
*cc
= CPU_CLASS(scc
);
421 DeviceClass
*dc
= DEVICE_CLASS(oc
);
423 scc
->next_cpu_id
= 0;
424 scc
->parent_realize
= dc
->realize
;
425 dc
->realize
= s390_cpu_realizefn
;
427 scc
->parent_reset
= cc
->reset
;
428 #if !defined(CONFIG_USER_ONLY)
429 scc
->load_normal
= s390_cpu_load_normal
;
431 scc
->cpu_reset
= s390_cpu_reset
;
432 scc
->initial_cpu_reset
= s390_cpu_initial_reset
;
433 cc
->reset
= s390_cpu_full_reset
;
434 cc
->has_work
= s390_cpu_has_work
;
435 cc
->do_interrupt
= s390_cpu_do_interrupt
;
436 cc
->dump_state
= s390_cpu_dump_state
;
437 cc
->set_pc
= s390_cpu_set_pc
;
438 cc
->gdb_read_register
= s390_cpu_gdb_read_register
;
439 cc
->gdb_write_register
= s390_cpu_gdb_write_register
;
440 #ifdef CONFIG_USER_ONLY
441 cc
->handle_mmu_fault
= s390_cpu_handle_mmu_fault
;
443 cc
->get_phys_page_debug
= s390_cpu_get_phys_page_debug
;
444 cc
->vmsd
= &vmstate_s390_cpu
;
445 cc
->write_elf64_note
= s390_cpu_write_elf64_note
;
446 cc
->cpu_exec_interrupt
= s390_cpu_exec_interrupt
;
447 cc
->debug_excp_handler
= s390x_cpu_debug_excp_handler
;
449 cc
->disas_set_info
= s390_cpu_disas_set_info
;
451 cc
->gdb_num_core_regs
= S390_NUM_CORE_REGS
;
452 cc
->gdb_core_xml_file
= "s390x-core64.xml";
453 cc
->gdb_arch_name
= s390_gdb_arch_name
;
456 * Reason: s390_cpu_realizefn() calls cpu_exec_init(), which saves
457 * the object in cpus -> dangling pointer after final
460 dc
->cannot_destroy_with_object_finalize_yet
= true;
463 static const TypeInfo s390_cpu_type_info
= {
464 .name
= TYPE_S390_CPU
,
466 .instance_size
= sizeof(S390CPU
),
467 .instance_init
= s390_cpu_initfn
,
468 .instance_finalize
= s390_cpu_finalize
,
470 .class_size
= sizeof(S390CPUClass
),
471 .class_init
= s390_cpu_class_init
,
474 static void s390_cpu_register_types(void)
476 type_register_static(&s390_cpu_type_info
);
479 type_init(s390_cpu_register_types
)