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 /* generate CPU information for cpu -? */
48 void s390_cpu_list(FILE *f
, fprintf_function cpu_fprintf
)
51 (*cpu_fprintf
)(f
, "s390 %16s\n", "host");
55 #ifndef CONFIG_USER_ONLY
56 CpuDefinitionInfoList
*arch_query_cpu_definitions(Error
**errp
)
58 CpuDefinitionInfoList
*entry
;
59 CpuDefinitionInfo
*info
;
61 info
= g_malloc0(sizeof(*info
));
62 info
->name
= g_strdup("host");
64 entry
= g_malloc0(sizeof(*entry
));
71 static void s390_cpu_set_pc(CPUState
*cs
, vaddr value
)
73 S390CPU
*cpu
= S390_CPU(cs
);
75 cpu
->env
.psw
.addr
= value
;
78 static bool s390_cpu_has_work(CPUState
*cs
)
80 S390CPU
*cpu
= S390_CPU(cs
);
81 CPUS390XState
*env
= &cpu
->env
;
83 return (cs
->interrupt_request
& CPU_INTERRUPT_HARD
) &&
84 (env
->psw
.mask
& PSW_MASK_EXT
);
87 #if !defined(CONFIG_USER_ONLY)
88 /* S390CPUClass::load_normal() */
89 static void s390_cpu_load_normal(CPUState
*s
)
91 S390CPU
*cpu
= S390_CPU(s
);
92 cpu
->env
.psw
.addr
= ldl_phys(s
->as
, 4) & PSW_MASK_ESA_ADDR
;
93 cpu
->env
.psw
.mask
= PSW_MASK_32
| PSW_MASK_64
;
94 s390_cpu_set_state(CPU_STATE_OPERATING
, cpu
);
98 /* S390CPUClass::cpu_reset() */
99 static void s390_cpu_reset(CPUState
*s
)
101 S390CPU
*cpu
= S390_CPU(s
);
102 S390CPUClass
*scc
= S390_CPU_GET_CLASS(cpu
);
103 CPUS390XState
*env
= &cpu
->env
;
105 env
->pfault_token
= -1UL;
106 scc
->parent_reset(s
);
107 cpu
->env
.sigp_order
= 0;
108 s390_cpu_set_state(CPU_STATE_STOPPED
, cpu
);
112 /* S390CPUClass::initial_reset() */
113 static void s390_cpu_initial_reset(CPUState
*s
)
115 S390CPU
*cpu
= S390_CPU(s
);
116 CPUS390XState
*env
= &cpu
->env
;
120 /* initial reset does not touch regs,fregs and aregs */
121 memset(&env
->fpc
, 0, offsetof(CPUS390XState
, cpu_num
) -
122 offsetof(CPUS390XState
, fpc
));
124 /* architectured initial values for CR 0 and 14 */
125 env
->cregs
[0] = CR0_RESET
;
126 env
->cregs
[14] = CR14_RESET
;
128 /* architectured initial value for Breaking-Event-Address register */
131 env
->pfault_token
= -1UL;
133 for (i
= 0; i
< ARRAY_SIZE(env
->io_index
); i
++) {
134 env
->io_index
[i
] = -1;
137 /* tininess for underflow is detected before rounding */
138 set_float_detect_tininess(float_tininess_before_rounding
,
141 /* Reset state inside the kernel that we cannot access yet from QEMU. */
143 kvm_s390_reset_vcpu(cpu
);
148 /* CPUClass:reset() */
149 static void s390_cpu_full_reset(CPUState
*s
)
151 S390CPU
*cpu
= S390_CPU(s
);
152 S390CPUClass
*scc
= S390_CPU_GET_CLASS(cpu
);
153 CPUS390XState
*env
= &cpu
->env
;
156 scc
->parent_reset(s
);
157 cpu
->env
.sigp_order
= 0;
158 s390_cpu_set_state(CPU_STATE_STOPPED
, cpu
);
160 memset(env
, 0, offsetof(CPUS390XState
, cpu_num
));
162 /* architectured initial values for CR 0 and 14 */
163 env
->cregs
[0] = CR0_RESET
;
164 env
->cregs
[14] = CR14_RESET
;
166 /* architectured initial value for Breaking-Event-Address register */
169 env
->pfault_token
= -1UL;
171 for (i
= 0; i
< ARRAY_SIZE(env
->io_index
); i
++) {
172 env
->io_index
[i
] = -1;
175 /* tininess for underflow is detected before rounding */
176 set_float_detect_tininess(float_tininess_before_rounding
,
179 /* Reset state inside the kernel that we cannot access yet from QEMU. */
181 kvm_s390_reset_vcpu(cpu
);
186 #if !defined(CONFIG_USER_ONLY)
187 static void s390_cpu_machine_reset_cb(void *opaque
)
189 S390CPU
*cpu
= opaque
;
191 run_on_cpu(CPU(cpu
), s390_do_cpu_full_reset
, CPU(cpu
));
195 static void s390_cpu_disas_set_info(CPUState
*cpu
, disassemble_info
*info
)
197 info
->mach
= bfd_mach_s390_64
;
198 info
->print_insn
= print_insn_s390
;
201 static void s390_cpu_realizefn(DeviceState
*dev
, Error
**errp
)
203 CPUState
*cs
= CPU(dev
);
204 S390CPUClass
*scc
= S390_CPU_GET_CLASS(dev
);
205 S390CPU
*cpu
= S390_CPU(dev
);
206 CPUS390XState
*env
= &cpu
->env
;
209 #if !defined(CONFIG_USER_ONLY)
210 if (cpu
->id
>= max_cpus
) {
211 error_setg(&err
, "Unable to add CPU: %" PRIi64
212 ", max allowed: %d", cpu
->id
, max_cpus
- 1);
216 if (cpu_exists(cpu
->id
)) {
217 error_setg(&err
, "Unable to add CPU: %" PRIi64
218 ", it already exists", cpu
->id
);
221 if (cpu
->id
!= scc
->next_cpu_id
) {
222 error_setg(&err
, "Unable to add CPU: %" PRIi64
223 ", The next available id is %" PRIi64
, cpu
->id
,
228 cpu_exec_init(cs
, &err
);
234 #if !defined(CONFIG_USER_ONLY)
235 qemu_register_reset(s390_cpu_machine_reset_cb
, cpu
);
237 env
->cpu_num
= cpu
->id
;
238 s390_cpu_gdb_init(cs
);
240 #if !defined(CONFIG_USER_ONLY)
241 run_on_cpu(cs
, s390_do_cpu_full_reset
, cs
);
246 scc
->parent_realize(dev
, &err
);
248 #if !defined(CONFIG_USER_ONLY)
249 if (dev
->hotplugged
) {
250 raise_irq_cpu_hotplug();
255 error_propagate(errp
, err
);
258 static void s390x_cpu_get_id(Object
*obj
, Visitor
*v
, const char *name
,
259 void *opaque
, Error
**errp
)
261 S390CPU
*cpu
= S390_CPU(obj
);
262 int64_t value
= cpu
->id
;
264 visit_type_int(v
, name
, &value
, errp
);
267 static void s390x_cpu_set_id(Object
*obj
, Visitor
*v
, const char *name
,
268 void *opaque
, Error
**errp
)
270 S390CPU
*cpu
= S390_CPU(obj
);
271 DeviceState
*dev
= DEVICE(obj
);
272 const int64_t min
= 0;
273 const int64_t max
= UINT32_MAX
;
278 error_setg(errp
, "Attempt to set property '%s' on '%s' after "
279 "it was realized", name
, object_get_typename(obj
));
283 visit_type_int(v
, name
, &value
, &err
);
285 error_propagate(errp
, err
);
288 if (value
< min
|| value
> max
) {
289 error_setg(errp
, "Property %s.%s doesn't take value %" PRId64
290 " (minimum: %" PRId64
", maximum: %" PRId64
")" ,
291 object_get_typename(obj
), name
, value
, min
, max
);
297 static void s390_cpu_initfn(Object
*obj
)
299 CPUState
*cs
= CPU(obj
);
300 S390CPU
*cpu
= S390_CPU(obj
);
301 CPUS390XState
*env
= &cpu
->env
;
303 #if !defined(CONFIG_USER_ONLY)
309 cs
->exception_index
= EXCP_HLT
;
310 object_property_add(OBJECT(cpu
), "id", "int64_t", s390x_cpu_get_id
,
311 s390x_cpu_set_id
, NULL
, NULL
, NULL
);
312 #if !defined(CONFIG_USER_ONLY)
313 qemu_get_timedate(&tm
, 0);
314 env
->tod_offset
= TOD_UNIX_EPOCH
+
315 (time2tod(mktimegm(&tm
)) * 1000000000ULL);
316 env
->tod_basetime
= 0;
317 env
->tod_timer
= timer_new_ns(QEMU_CLOCK_VIRTUAL
, s390x_tod_timer
, cpu
);
318 env
->cpu_timer
= timer_new_ns(QEMU_CLOCK_VIRTUAL
, s390x_cpu_timer
, cpu
);
319 s390_cpu_set_state(CPU_STATE_STOPPED
, cpu
);
322 if (tcg_enabled() && !inited
) {
324 s390x_translate_init();
328 static void s390_cpu_finalize(Object
*obj
)
330 #if !defined(CONFIG_USER_ONLY)
331 S390CPU
*cpu
= S390_CPU(obj
);
333 qemu_unregister_reset(s390_cpu_machine_reset_cb
, cpu
);
334 g_free(cpu
->irqstate
);
338 #if !defined(CONFIG_USER_ONLY)
339 static bool disabled_wait(CPUState
*cpu
)
341 return cpu
->halted
&& !(S390_CPU(cpu
)->env
.psw
.mask
&
342 (PSW_MASK_IO
| PSW_MASK_EXT
| PSW_MASK_MCHECK
));
345 static unsigned s390_count_running_cpus(void)
351 uint8_t state
= S390_CPU(cpu
)->env
.cpu_state
;
352 if (state
== CPU_STATE_OPERATING
||
353 state
== CPU_STATE_LOAD
) {
354 if (!disabled_wait(cpu
)) {
363 unsigned int s390_cpu_halt(S390CPU
*cpu
)
365 CPUState
*cs
= CPU(cpu
);
366 trace_cpu_halt(cs
->cpu_index
);
370 cs
->exception_index
= EXCP_HLT
;
373 return s390_count_running_cpus();
376 void s390_cpu_unhalt(S390CPU
*cpu
)
378 CPUState
*cs
= CPU(cpu
);
379 trace_cpu_unhalt(cs
->cpu_index
);
383 cs
->exception_index
= -1;
387 unsigned int s390_cpu_set_state(uint8_t cpu_state
, S390CPU
*cpu
)
389 trace_cpu_set_state(CPU(cpu
)->cpu_index
, cpu_state
);
392 case CPU_STATE_STOPPED
:
393 case CPU_STATE_CHECK_STOP
:
394 /* halt the cpu for common infrastructure */
397 case CPU_STATE_OPERATING
:
399 /* unhalt the cpu for common infrastructure */
400 s390_cpu_unhalt(cpu
);
403 error_report("Requested CPU state is not a valid S390 CPU state: %u",
407 if (kvm_enabled() && cpu
->env
.cpu_state
!= cpu_state
) {
408 kvm_s390_set_cpu_state(cpu
, cpu_state
);
410 cpu
->env
.cpu_state
= cpu_state
;
412 return s390_count_running_cpus();
416 static gchar
*s390_gdb_arch_name(CPUState
*cs
)
418 return g_strdup("s390:64-bit");
421 static void s390_cpu_class_init(ObjectClass
*oc
, void *data
)
423 S390CPUClass
*scc
= S390_CPU_CLASS(oc
);
424 CPUClass
*cc
= CPU_CLASS(scc
);
425 DeviceClass
*dc
= DEVICE_CLASS(oc
);
427 scc
->next_cpu_id
= 0;
428 scc
->parent_realize
= dc
->realize
;
429 dc
->realize
= s390_cpu_realizefn
;
431 scc
->parent_reset
= cc
->reset
;
432 #if !defined(CONFIG_USER_ONLY)
433 scc
->load_normal
= s390_cpu_load_normal
;
435 scc
->cpu_reset
= s390_cpu_reset
;
436 scc
->initial_cpu_reset
= s390_cpu_initial_reset
;
437 cc
->reset
= s390_cpu_full_reset
;
438 cc
->has_work
= s390_cpu_has_work
;
439 cc
->do_interrupt
= s390_cpu_do_interrupt
;
440 cc
->dump_state
= s390_cpu_dump_state
;
441 cc
->set_pc
= s390_cpu_set_pc
;
442 cc
->gdb_read_register
= s390_cpu_gdb_read_register
;
443 cc
->gdb_write_register
= s390_cpu_gdb_write_register
;
444 #ifdef CONFIG_USER_ONLY
445 cc
->handle_mmu_fault
= s390_cpu_handle_mmu_fault
;
447 cc
->get_phys_page_debug
= s390_cpu_get_phys_page_debug
;
448 cc
->vmsd
= &vmstate_s390_cpu
;
449 cc
->write_elf64_note
= s390_cpu_write_elf64_note
;
450 cc
->cpu_exec_interrupt
= s390_cpu_exec_interrupt
;
451 cc
->debug_excp_handler
= s390x_cpu_debug_excp_handler
;
453 cc
->disas_set_info
= s390_cpu_disas_set_info
;
455 cc
->gdb_num_core_regs
= S390_NUM_CORE_REGS
;
456 cc
->gdb_core_xml_file
= "s390x-core64.xml";
457 cc
->gdb_arch_name
= s390_gdb_arch_name
;
460 * Reason: s390_cpu_realizefn() calls cpu_exec_init(), which saves
461 * the object in cpus -> dangling pointer after final
464 dc
->cannot_destroy_with_object_finalize_yet
= true;
467 static const TypeInfo s390_cpu_type_info
= {
468 .name
= TYPE_S390_CPU
,
470 .instance_size
= sizeof(S390CPU
),
471 .instance_init
= s390_cpu_initfn
,
472 .instance_finalize
= s390_cpu_finalize
,
474 .class_size
= sizeof(S390CPUClass
),
475 .class_init
= s390_cpu_class_init
,
478 static void s390_cpu_register_types(void)
480 type_register_static(&s390_cpu_type_info
);
483 type_init(s390_cpu_register_types
)