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/timer.h"
31 #include "qemu/error-report.h"
34 #include "qapi/visitor.h"
35 #ifndef CONFIG_USER_ONLY
36 #include "sysemu/arch_init.h"
37 #include "sysemu/sysemu.h"
38 #include "hw/s390x/sclp.h"
41 #define CR0_RESET 0xE0UL
42 #define CR14_RESET 0xC2000000UL;
44 /* generate CPU information for cpu -? */
45 void s390_cpu_list(FILE *f
, fprintf_function cpu_fprintf
)
48 (*cpu_fprintf
)(f
, "s390 %16s\n", "host");
52 #ifndef CONFIG_USER_ONLY
53 CpuDefinitionInfoList
*arch_query_cpu_definitions(Error
**errp
)
55 CpuDefinitionInfoList
*entry
;
56 CpuDefinitionInfo
*info
;
58 info
= g_malloc0(sizeof(*info
));
59 info
->name
= g_strdup("host");
61 entry
= g_malloc0(sizeof(*entry
));
68 static void s390_cpu_set_pc(CPUState
*cs
, vaddr value
)
70 S390CPU
*cpu
= S390_CPU(cs
);
72 cpu
->env
.psw
.addr
= value
;
75 static bool s390_cpu_has_work(CPUState
*cs
)
77 S390CPU
*cpu
= S390_CPU(cs
);
78 CPUS390XState
*env
= &cpu
->env
;
80 return (cs
->interrupt_request
& CPU_INTERRUPT_HARD
) &&
81 (env
->psw
.mask
& PSW_MASK_EXT
);
84 #if !defined(CONFIG_USER_ONLY)
85 /* S390CPUClass::load_normal() */
86 static void s390_cpu_load_normal(CPUState
*s
)
88 S390CPU
*cpu
= S390_CPU(s
);
89 cpu
->env
.psw
.addr
= ldl_phys(s
->as
, 4) & PSW_MASK_ESA_ADDR
;
90 cpu
->env
.psw
.mask
= PSW_MASK_32
| PSW_MASK_64
;
91 s390_cpu_set_state(CPU_STATE_OPERATING
, cpu
);
95 /* S390CPUClass::cpu_reset() */
96 static void s390_cpu_reset(CPUState
*s
)
98 S390CPU
*cpu
= S390_CPU(s
);
99 S390CPUClass
*scc
= S390_CPU_GET_CLASS(cpu
);
100 CPUS390XState
*env
= &cpu
->env
;
102 env
->pfault_token
= -1UL;
103 scc
->parent_reset(s
);
104 cpu
->env
.sigp_order
= 0;
105 s390_cpu_set_state(CPU_STATE_STOPPED
, cpu
);
109 /* S390CPUClass::initial_reset() */
110 static void s390_cpu_initial_reset(CPUState
*s
)
112 S390CPU
*cpu
= S390_CPU(s
);
113 CPUS390XState
*env
= &cpu
->env
;
117 /* initial reset does not touch regs,fregs and aregs */
118 memset(&env
->fpc
, 0, offsetof(CPUS390XState
, cpu_num
) -
119 offsetof(CPUS390XState
, fpc
));
121 /* architectured initial values for CR 0 and 14 */
122 env
->cregs
[0] = CR0_RESET
;
123 env
->cregs
[14] = CR14_RESET
;
125 /* architectured initial value for Breaking-Event-Address register */
128 env
->pfault_token
= -1UL;
130 for (i
= 0; i
< ARRAY_SIZE(env
->io_index
); i
++) {
131 env
->io_index
[i
] = -1;
134 /* tininess for underflow is detected before rounding */
135 set_float_detect_tininess(float_tininess_before_rounding
,
138 /* Reset state inside the kernel that we cannot access yet from QEMU. */
140 kvm_s390_reset_vcpu(cpu
);
145 /* CPUClass:reset() */
146 static void s390_cpu_full_reset(CPUState
*s
)
148 S390CPU
*cpu
= S390_CPU(s
);
149 S390CPUClass
*scc
= S390_CPU_GET_CLASS(cpu
);
150 CPUS390XState
*env
= &cpu
->env
;
153 scc
->parent_reset(s
);
154 cpu
->env
.sigp_order
= 0;
155 s390_cpu_set_state(CPU_STATE_STOPPED
, cpu
);
157 memset(env
, 0, offsetof(CPUS390XState
, cpu_num
));
159 /* architectured initial values for CR 0 and 14 */
160 env
->cregs
[0] = CR0_RESET
;
161 env
->cregs
[14] = CR14_RESET
;
163 /* architectured initial value for Breaking-Event-Address register */
166 env
->pfault_token
= -1UL;
168 for (i
= 0; i
< ARRAY_SIZE(env
->io_index
); i
++) {
169 env
->io_index
[i
] = -1;
172 /* tininess for underflow is detected before rounding */
173 set_float_detect_tininess(float_tininess_before_rounding
,
176 /* Reset state inside the kernel that we cannot access yet from QEMU. */
178 kvm_s390_reset_vcpu(cpu
);
183 #if !defined(CONFIG_USER_ONLY)
184 static void s390_cpu_machine_reset_cb(void *opaque
)
186 S390CPU
*cpu
= opaque
;
188 run_on_cpu(CPU(cpu
), s390_do_cpu_full_reset
, CPU(cpu
));
192 static void s390_cpu_disas_set_info(CPUState
*cpu
, disassemble_info
*info
)
194 info
->mach
= bfd_mach_s390_64
;
195 info
->print_insn
= print_insn_s390
;
198 static void s390_cpu_realizefn(DeviceState
*dev
, Error
**errp
)
200 CPUState
*cs
= CPU(dev
);
201 S390CPUClass
*scc
= S390_CPU_GET_CLASS(dev
);
202 S390CPU
*cpu
= S390_CPU(dev
);
203 CPUS390XState
*env
= &cpu
->env
;
206 #if !defined(CONFIG_USER_ONLY)
207 if (cpu
->id
>= max_cpus
) {
208 error_setg(&err
, "Unable to add CPU: %" PRIi64
209 ", max allowed: %d", cpu
->id
, max_cpus
- 1);
213 if (cpu_exists(cpu
->id
)) {
214 error_setg(&err
, "Unable to add CPU: %" PRIi64
215 ", it already exists", cpu
->id
);
218 if (cpu
->id
!= scc
->next_cpu_id
) {
219 error_setg(&err
, "Unable to add CPU: %" PRIi64
220 ", The next available id is %" PRIi64
, cpu
->id
,
225 cpu_exec_init(cs
, &err
);
231 #if !defined(CONFIG_USER_ONLY)
232 qemu_register_reset(s390_cpu_machine_reset_cb
, cpu
);
234 env
->cpu_num
= cpu
->id
;
235 s390_cpu_gdb_init(cs
);
237 #if !defined(CONFIG_USER_ONLY)
238 run_on_cpu(cs
, s390_do_cpu_full_reset
, cs
);
243 scc
->parent_realize(dev
, &err
);
245 #if !defined(CONFIG_USER_ONLY)
246 if (dev
->hotplugged
) {
247 raise_irq_cpu_hotplug();
252 error_propagate(errp
, err
);
255 static void s390x_cpu_get_id(Object
*obj
, Visitor
*v
, const char *name
,
256 void *opaque
, Error
**errp
)
258 S390CPU
*cpu
= S390_CPU(obj
);
259 int64_t value
= cpu
->id
;
261 visit_type_int(v
, name
, &value
, errp
);
264 static void s390x_cpu_set_id(Object
*obj
, Visitor
*v
, const char *name
,
265 void *opaque
, Error
**errp
)
267 S390CPU
*cpu
= S390_CPU(obj
);
268 DeviceState
*dev
= DEVICE(obj
);
269 const int64_t min
= 0;
270 const int64_t max
= UINT32_MAX
;
275 error_setg(errp
, "Attempt to set property '%s' on '%s' after "
276 "it was realized", name
, object_get_typename(obj
));
280 visit_type_int(v
, name
, &value
, &err
);
282 error_propagate(errp
, err
);
285 if (value
< min
|| value
> max
) {
286 error_setg(errp
, "Property %s.%s doesn't take value %" PRId64
287 " (minimum: %" PRId64
", maximum: %" PRId64
")" ,
288 object_get_typename(obj
), name
, value
, min
, max
);
294 static void s390_cpu_initfn(Object
*obj
)
296 CPUState
*cs
= CPU(obj
);
297 S390CPU
*cpu
= S390_CPU(obj
);
298 CPUS390XState
*env
= &cpu
->env
;
300 #if !defined(CONFIG_USER_ONLY)
306 cs
->exception_index
= EXCP_HLT
;
307 object_property_add(OBJECT(cpu
), "id", "int64_t", s390x_cpu_get_id
,
308 s390x_cpu_set_id
, NULL
, NULL
, NULL
);
309 #if !defined(CONFIG_USER_ONLY)
310 qemu_get_timedate(&tm
, 0);
311 env
->tod_offset
= TOD_UNIX_EPOCH
+
312 (time2tod(mktimegm(&tm
)) * 1000000000ULL);
313 env
->tod_basetime
= 0;
314 env
->tod_timer
= timer_new_ns(QEMU_CLOCK_VIRTUAL
, s390x_tod_timer
, cpu
);
315 env
->cpu_timer
= timer_new_ns(QEMU_CLOCK_VIRTUAL
, s390x_cpu_timer
, cpu
);
316 s390_cpu_set_state(CPU_STATE_STOPPED
, cpu
);
319 if (tcg_enabled() && !inited
) {
321 s390x_translate_init();
325 static void s390_cpu_finalize(Object
*obj
)
327 #if !defined(CONFIG_USER_ONLY)
328 S390CPU
*cpu
= S390_CPU(obj
);
330 qemu_unregister_reset(s390_cpu_machine_reset_cb
, cpu
);
331 g_free(cpu
->irqstate
);
335 #if !defined(CONFIG_USER_ONLY)
336 static bool disabled_wait(CPUState
*cpu
)
338 return cpu
->halted
&& !(S390_CPU(cpu
)->env
.psw
.mask
&
339 (PSW_MASK_IO
| PSW_MASK_EXT
| PSW_MASK_MCHECK
));
342 static unsigned s390_count_running_cpus(void)
348 uint8_t state
= S390_CPU(cpu
)->env
.cpu_state
;
349 if (state
== CPU_STATE_OPERATING
||
350 state
== CPU_STATE_LOAD
) {
351 if (!disabled_wait(cpu
)) {
360 unsigned int s390_cpu_halt(S390CPU
*cpu
)
362 CPUState
*cs
= CPU(cpu
);
363 trace_cpu_halt(cs
->cpu_index
);
367 cs
->exception_index
= EXCP_HLT
;
370 return s390_count_running_cpus();
373 void s390_cpu_unhalt(S390CPU
*cpu
)
375 CPUState
*cs
= CPU(cpu
);
376 trace_cpu_unhalt(cs
->cpu_index
);
380 cs
->exception_index
= -1;
384 unsigned int s390_cpu_set_state(uint8_t cpu_state
, S390CPU
*cpu
)
386 trace_cpu_set_state(CPU(cpu
)->cpu_index
, cpu_state
);
389 case CPU_STATE_STOPPED
:
390 case CPU_STATE_CHECK_STOP
:
391 /* halt the cpu for common infrastructure */
394 case CPU_STATE_OPERATING
:
396 /* unhalt the cpu for common infrastructure */
397 s390_cpu_unhalt(cpu
);
400 error_report("Requested CPU state is not a valid S390 CPU state: %u",
404 if (kvm_enabled() && cpu
->env
.cpu_state
!= cpu_state
) {
405 kvm_s390_set_cpu_state(cpu
, cpu_state
);
407 cpu
->env
.cpu_state
= cpu_state
;
409 return s390_count_running_cpus();
413 static gchar
*s390_gdb_arch_name(CPUState
*cs
)
415 return g_strdup("s390:64-bit");
418 static void s390_cpu_class_init(ObjectClass
*oc
, void *data
)
420 S390CPUClass
*scc
= S390_CPU_CLASS(oc
);
421 CPUClass
*cc
= CPU_CLASS(scc
);
422 DeviceClass
*dc
= DEVICE_CLASS(oc
);
424 scc
->next_cpu_id
= 0;
425 scc
->parent_realize
= dc
->realize
;
426 dc
->realize
= s390_cpu_realizefn
;
428 scc
->parent_reset
= cc
->reset
;
429 #if !defined(CONFIG_USER_ONLY)
430 scc
->load_normal
= s390_cpu_load_normal
;
432 scc
->cpu_reset
= s390_cpu_reset
;
433 scc
->initial_cpu_reset
= s390_cpu_initial_reset
;
434 cc
->reset
= s390_cpu_full_reset
;
435 cc
->has_work
= s390_cpu_has_work
;
436 cc
->do_interrupt
= s390_cpu_do_interrupt
;
437 cc
->dump_state
= s390_cpu_dump_state
;
438 cc
->set_pc
= s390_cpu_set_pc
;
439 cc
->gdb_read_register
= s390_cpu_gdb_read_register
;
440 cc
->gdb_write_register
= s390_cpu_gdb_write_register
;
441 #ifdef CONFIG_USER_ONLY
442 cc
->handle_mmu_fault
= s390_cpu_handle_mmu_fault
;
444 cc
->get_phys_page_debug
= s390_cpu_get_phys_page_debug
;
445 cc
->vmsd
= &vmstate_s390_cpu
;
446 cc
->write_elf64_note
= s390_cpu_write_elf64_note
;
447 cc
->cpu_exec_interrupt
= s390_cpu_exec_interrupt
;
448 cc
->debug_excp_handler
= s390x_cpu_debug_excp_handler
;
450 cc
->disas_set_info
= s390_cpu_disas_set_info
;
452 cc
->gdb_num_core_regs
= S390_NUM_CORE_REGS
;
453 cc
->gdb_core_xml_file
= "s390x-core64.xml";
454 cc
->gdb_arch_name
= s390_gdb_arch_name
;
457 * Reason: s390_cpu_realizefn() calls cpu_exec_init(), which saves
458 * the object in cpus -> dangling pointer after final
461 dc
->cannot_destroy_with_object_finalize_yet
= true;
464 static const TypeInfo s390_cpu_type_info
= {
465 .name
= TYPE_S390_CPU
,
467 .instance_size
= sizeof(S390CPU
),
468 .instance_init
= s390_cpu_initfn
,
469 .instance_finalize
= s390_cpu_finalize
,
471 .class_size
= sizeof(S390CPUClass
),
472 .class_init
= s390_cpu_class_init
,
475 static void s390_cpu_register_types(void)
477 type_register_static(&s390_cpu_type_info
);
480 type_init(s390_cpu_register_types
)