virtio-serial: Wrap in vmstate
[qemu.git] / target-s390x / cpu.c
blobe43e2d61550be09a754ff06f209577cddc8f52e0
1 /*
2 * QEMU S/390 CPU
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"
28 #include "cpu.h"
29 #include "qemu-common.h"
30 #include "qemu/cutils.h"
31 #include "qemu/timer.h"
32 #include "qemu/error-report.h"
33 #include "trace.h"
34 #include "qapi/visitor.h"
35 #include "migration/vmstate.h"
36 #include "exec/exec-all.h"
37 #ifndef CONFIG_USER_ONLY
38 #include "hw/hw.h"
39 #include "sysemu/arch_init.h"
40 #include "sysemu/sysemu.h"
41 #include "hw/s390x/sclp.h"
42 #endif
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)
50 #ifdef CONFIG_KVM
51 (*cpu_fprintf)(f, "s390 %16s\n", "host");
52 #endif
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));
65 entry->value = info;
67 return entry;
69 #endif
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);
96 #endif
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);
109 tlb_flush(s, 1);
112 /* S390CPUClass::initial_reset() */
113 static void s390_cpu_initial_reset(CPUState *s)
115 S390CPU *cpu = S390_CPU(s);
116 CPUS390XState *env = &cpu->env;
117 int i;
119 s390_cpu_reset(s);
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 */
129 env->gbea = 1;
131 env->pfault_token = -1UL;
132 env->ext_index = -1;
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,
139 &env->fpu_status);
141 /* Reset state inside the kernel that we cannot access yet from QEMU. */
142 if (kvm_enabled()) {
143 kvm_s390_reset_vcpu(cpu);
145 tlb_flush(s, 1);
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;
154 int i;
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 */
167 env->gbea = 1;
169 env->pfault_token = -1UL;
170 env->ext_index = -1;
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,
177 &env->fpu_status);
179 /* Reset state inside the kernel that we cannot access yet from QEMU. */
180 if (kvm_enabled()) {
181 kvm_s390_reset_vcpu(cpu);
183 tlb_flush(s, 1);
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));
193 #endif
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;
207 Error *err = NULL;
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);
213 goto out;
215 #endif
216 if (cpu_exists(cpu->id)) {
217 error_setg(&err, "Unable to add CPU: %" PRIi64
218 ", it already exists", cpu->id);
219 goto out;
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,
224 scc->next_cpu_id);
225 goto out;
228 cpu_exec_init(cs, &err);
229 if (err != NULL) {
230 goto out;
232 scc->next_cpu_id++;
234 #if !defined(CONFIG_USER_ONLY)
235 qemu_register_reset(s390_cpu_machine_reset_cb, cpu);
236 #endif
237 env->cpu_num = cpu->id;
238 s390_cpu_gdb_init(cs);
239 qemu_init_vcpu(cs);
240 #if !defined(CONFIG_USER_ONLY)
241 run_on_cpu(cs, s390_do_cpu_full_reset, cs);
242 #else
243 cpu_reset(cs);
244 #endif
246 scc->parent_realize(dev, &err);
248 #if !defined(CONFIG_USER_ONLY)
249 if (dev->hotplugged) {
250 raise_irq_cpu_hotplug();
252 #endif
254 out:
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;
274 Error *err = NULL;
275 int64_t value;
277 if (dev->realized) {
278 error_setg(errp, "Attempt to set property '%s' on '%s' after "
279 "it was realized", name, object_get_typename(obj));
280 return;
283 visit_type_int(v, name, &value, &err);
284 if (err) {
285 error_propagate(errp, err);
286 return;
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);
292 return;
294 cpu->id = value;
297 static void s390_cpu_initfn(Object *obj)
299 CPUState *cs = CPU(obj);
300 S390CPU *cpu = S390_CPU(obj);
301 CPUS390XState *env = &cpu->env;
302 static bool inited;
303 #if !defined(CONFIG_USER_ONLY)
304 struct tm tm;
305 #endif
307 cs->env_ptr = env;
308 cs->halted = 1;
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);
320 #endif
322 if (tcg_enabled() && !inited) {
323 inited = true;
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);
335 #endif
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)
347 CPUState *cpu;
348 int nr_running = 0;
350 CPU_FOREACH(cpu) {
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)) {
355 nr_running++;
360 return nr_running;
363 unsigned int s390_cpu_halt(S390CPU *cpu)
365 CPUState *cs = CPU(cpu);
366 trace_cpu_halt(cs->cpu_index);
368 if (!cs->halted) {
369 cs->halted = 1;
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);
381 if (cs->halted) {
382 cs->halted = 0;
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);
391 switch (cpu_state) {
392 case CPU_STATE_STOPPED:
393 case CPU_STATE_CHECK_STOP:
394 /* halt the cpu for common infrastructure */
395 s390_cpu_halt(cpu);
396 break;
397 case CPU_STATE_OPERATING:
398 case CPU_STATE_LOAD:
399 /* unhalt the cpu for common infrastructure */
400 s390_cpu_unhalt(cpu);
401 break;
402 default:
403 error_report("Requested CPU state is not a valid S390 CPU state: %u",
404 cpu_state);
405 exit(1);
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();
414 #endif
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;
434 #endif
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;
446 #else
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;
452 #endif
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
462 * object_unref().
464 dc->cannot_destroy_with_object_finalize_yet = true;
467 static const TypeInfo s390_cpu_type_info = {
468 .name = TYPE_S390_CPU,
469 .parent = TYPE_CPU,
470 .instance_size = sizeof(S390CPU),
471 .instance_init = s390_cpu_initfn,
472 .instance_finalize = s390_cpu_finalize,
473 .abstract = false,
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)