bsd-user: add necessary includes to fix warnings
[qemu.git] / target-s390x / cpu.c
blob4bfff341dc3702abbe5992349338a6621be4eb79
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 "hw/hw.h"
34 #include "trace.h"
35 #include "qapi/visitor.h"
36 #ifndef CONFIG_USER_ONLY
37 #include "sysemu/arch_init.h"
38 #include "sysemu/sysemu.h"
39 #include "hw/s390x/sclp.h"
40 #endif
42 #define CR0_RESET 0xE0UL
43 #define CR14_RESET 0xC2000000UL;
45 /* generate CPU information for cpu -? */
46 void s390_cpu_list(FILE *f, fprintf_function cpu_fprintf)
48 #ifdef CONFIG_KVM
49 (*cpu_fprintf)(f, "s390 %16s\n", "host");
50 #endif
53 #ifndef CONFIG_USER_ONLY
54 CpuDefinitionInfoList *arch_query_cpu_definitions(Error **errp)
56 CpuDefinitionInfoList *entry;
57 CpuDefinitionInfo *info;
59 info = g_malloc0(sizeof(*info));
60 info->name = g_strdup("host");
62 entry = g_malloc0(sizeof(*entry));
63 entry->value = info;
65 return entry;
67 #endif
69 static void s390_cpu_set_pc(CPUState *cs, vaddr value)
71 S390CPU *cpu = S390_CPU(cs);
73 cpu->env.psw.addr = value;
76 static bool s390_cpu_has_work(CPUState *cs)
78 S390CPU *cpu = S390_CPU(cs);
79 CPUS390XState *env = &cpu->env;
81 return (cs->interrupt_request & CPU_INTERRUPT_HARD) &&
82 (env->psw.mask & PSW_MASK_EXT);
85 #if !defined(CONFIG_USER_ONLY)
86 /* S390CPUClass::load_normal() */
87 static void s390_cpu_load_normal(CPUState *s)
89 S390CPU *cpu = S390_CPU(s);
90 cpu->env.psw.addr = ldl_phys(s->as, 4) & PSW_MASK_ESA_ADDR;
91 cpu->env.psw.mask = PSW_MASK_32 | PSW_MASK_64;
92 s390_cpu_set_state(CPU_STATE_OPERATING, cpu);
94 #endif
96 /* S390CPUClass::cpu_reset() */
97 static void s390_cpu_reset(CPUState *s)
99 S390CPU *cpu = S390_CPU(s);
100 S390CPUClass *scc = S390_CPU_GET_CLASS(cpu);
101 CPUS390XState *env = &cpu->env;
103 env->pfault_token = -1UL;
104 scc->parent_reset(s);
105 cpu->env.sigp_order = 0;
106 s390_cpu_set_state(CPU_STATE_STOPPED, cpu);
107 tlb_flush(s, 1);
110 /* S390CPUClass::initial_reset() */
111 static void s390_cpu_initial_reset(CPUState *s)
113 S390CPU *cpu = S390_CPU(s);
114 CPUS390XState *env = &cpu->env;
115 int i;
117 s390_cpu_reset(s);
118 /* initial reset does not touch regs,fregs and aregs */
119 memset(&env->fpc, 0, offsetof(CPUS390XState, cpu_num) -
120 offsetof(CPUS390XState, fpc));
122 /* architectured initial values for CR 0 and 14 */
123 env->cregs[0] = CR0_RESET;
124 env->cregs[14] = CR14_RESET;
126 /* architectured initial value for Breaking-Event-Address register */
127 env->gbea = 1;
129 env->pfault_token = -1UL;
130 env->ext_index = -1;
131 for (i = 0; i < ARRAY_SIZE(env->io_index); i++) {
132 env->io_index[i] = -1;
135 /* tininess for underflow is detected before rounding */
136 set_float_detect_tininess(float_tininess_before_rounding,
137 &env->fpu_status);
139 /* Reset state inside the kernel that we cannot access yet from QEMU. */
140 if (kvm_enabled()) {
141 kvm_s390_reset_vcpu(cpu);
143 tlb_flush(s, 1);
146 /* CPUClass:reset() */
147 static void s390_cpu_full_reset(CPUState *s)
149 S390CPU *cpu = S390_CPU(s);
150 S390CPUClass *scc = S390_CPU_GET_CLASS(cpu);
151 CPUS390XState *env = &cpu->env;
152 int i;
154 scc->parent_reset(s);
155 cpu->env.sigp_order = 0;
156 s390_cpu_set_state(CPU_STATE_STOPPED, cpu);
158 memset(env, 0, offsetof(CPUS390XState, cpu_num));
160 /* architectured initial values for CR 0 and 14 */
161 env->cregs[0] = CR0_RESET;
162 env->cregs[14] = CR14_RESET;
164 /* architectured initial value for Breaking-Event-Address register */
165 env->gbea = 1;
167 env->pfault_token = -1UL;
168 env->ext_index = -1;
169 for (i = 0; i < ARRAY_SIZE(env->io_index); i++) {
170 env->io_index[i] = -1;
173 /* tininess for underflow is detected before rounding */
174 set_float_detect_tininess(float_tininess_before_rounding,
175 &env->fpu_status);
177 /* Reset state inside the kernel that we cannot access yet from QEMU. */
178 if (kvm_enabled()) {
179 kvm_s390_reset_vcpu(cpu);
181 tlb_flush(s, 1);
184 #if !defined(CONFIG_USER_ONLY)
185 static void s390_cpu_machine_reset_cb(void *opaque)
187 S390CPU *cpu = opaque;
189 run_on_cpu(CPU(cpu), s390_do_cpu_full_reset, CPU(cpu));
191 #endif
193 static void s390_cpu_disas_set_info(CPUState *cpu, disassemble_info *info)
195 info->mach = bfd_mach_s390_64;
196 info->print_insn = print_insn_s390;
199 static void s390_cpu_realizefn(DeviceState *dev, Error **errp)
201 CPUState *cs = CPU(dev);
202 S390CPUClass *scc = S390_CPU_GET_CLASS(dev);
203 S390CPU *cpu = S390_CPU(dev);
204 CPUS390XState *env = &cpu->env;
205 Error *err = NULL;
207 #if !defined(CONFIG_USER_ONLY)
208 if (cpu->id >= max_cpus) {
209 error_setg(&err, "Unable to add CPU: %" PRIi64
210 ", max allowed: %d", cpu->id, max_cpus - 1);
211 goto out;
213 #endif
214 if (cpu_exists(cpu->id)) {
215 error_setg(&err, "Unable to add CPU: %" PRIi64
216 ", it already exists", cpu->id);
217 goto out;
219 if (cpu->id != scc->next_cpu_id) {
220 error_setg(&err, "Unable to add CPU: %" PRIi64
221 ", The next available id is %" PRIi64, cpu->id,
222 scc->next_cpu_id);
223 goto out;
226 cpu_exec_init(cs, &err);
227 if (err != NULL) {
228 goto out;
230 scc->next_cpu_id++;
232 #if !defined(CONFIG_USER_ONLY)
233 qemu_register_reset(s390_cpu_machine_reset_cb, cpu);
234 #endif
235 env->cpu_num = cpu->id;
236 s390_cpu_gdb_init(cs);
237 qemu_init_vcpu(cs);
238 #if !defined(CONFIG_USER_ONLY)
239 run_on_cpu(cs, s390_do_cpu_full_reset, cs);
240 #else
241 cpu_reset(cs);
242 #endif
244 scc->parent_realize(dev, &err);
246 #if !defined(CONFIG_USER_ONLY)
247 if (dev->hotplugged) {
248 raise_irq_cpu_hotplug();
250 #endif
252 out:
253 error_propagate(errp, err);
256 static void s390x_cpu_get_id(Object *obj, Visitor *v, const char *name,
257 void *opaque, Error **errp)
259 S390CPU *cpu = S390_CPU(obj);
260 int64_t value = cpu->id;
262 visit_type_int(v, name, &value, errp);
265 static void s390x_cpu_set_id(Object *obj, Visitor *v, const char *name,
266 void *opaque, Error **errp)
268 S390CPU *cpu = S390_CPU(obj);
269 DeviceState *dev = DEVICE(obj);
270 const int64_t min = 0;
271 const int64_t max = UINT32_MAX;
272 Error *err = NULL;
273 int64_t value;
275 if (dev->realized) {
276 error_setg(errp, "Attempt to set property '%s' on '%s' after "
277 "it was realized", name, object_get_typename(obj));
278 return;
281 visit_type_int(v, name, &value, &err);
282 if (err) {
283 error_propagate(errp, err);
284 return;
286 if (value < min || value > max) {
287 error_setg(errp, "Property %s.%s doesn't take value %" PRId64
288 " (minimum: %" PRId64 ", maximum: %" PRId64 ")" ,
289 object_get_typename(obj), name, value, min, max);
290 return;
292 cpu->id = value;
295 static void s390_cpu_initfn(Object *obj)
297 CPUState *cs = CPU(obj);
298 S390CPU *cpu = S390_CPU(obj);
299 CPUS390XState *env = &cpu->env;
300 static bool inited;
301 #if !defined(CONFIG_USER_ONLY)
302 struct tm tm;
303 #endif
305 cs->env_ptr = env;
306 cs->halted = 1;
307 cs->exception_index = EXCP_HLT;
308 object_property_add(OBJECT(cpu), "id", "int64_t", s390x_cpu_get_id,
309 s390x_cpu_set_id, NULL, NULL, NULL);
310 #if !defined(CONFIG_USER_ONLY)
311 qemu_get_timedate(&tm, 0);
312 env->tod_offset = TOD_UNIX_EPOCH +
313 (time2tod(mktimegm(&tm)) * 1000000000ULL);
314 env->tod_basetime = 0;
315 env->tod_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, s390x_tod_timer, cpu);
316 env->cpu_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, s390x_cpu_timer, cpu);
317 s390_cpu_set_state(CPU_STATE_STOPPED, cpu);
318 #endif
320 if (tcg_enabled() && !inited) {
321 inited = true;
322 s390x_translate_init();
326 static void s390_cpu_finalize(Object *obj)
328 #if !defined(CONFIG_USER_ONLY)
329 S390CPU *cpu = S390_CPU(obj);
331 qemu_unregister_reset(s390_cpu_machine_reset_cb, cpu);
332 g_free(cpu->irqstate);
333 #endif
336 #if !defined(CONFIG_USER_ONLY)
337 static bool disabled_wait(CPUState *cpu)
339 return cpu->halted && !(S390_CPU(cpu)->env.psw.mask &
340 (PSW_MASK_IO | PSW_MASK_EXT | PSW_MASK_MCHECK));
343 static unsigned s390_count_running_cpus(void)
345 CPUState *cpu;
346 int nr_running = 0;
348 CPU_FOREACH(cpu) {
349 uint8_t state = S390_CPU(cpu)->env.cpu_state;
350 if (state == CPU_STATE_OPERATING ||
351 state == CPU_STATE_LOAD) {
352 if (!disabled_wait(cpu)) {
353 nr_running++;
358 return nr_running;
361 unsigned int s390_cpu_halt(S390CPU *cpu)
363 CPUState *cs = CPU(cpu);
364 trace_cpu_halt(cs->cpu_index);
366 if (!cs->halted) {
367 cs->halted = 1;
368 cs->exception_index = EXCP_HLT;
371 return s390_count_running_cpus();
374 void s390_cpu_unhalt(S390CPU *cpu)
376 CPUState *cs = CPU(cpu);
377 trace_cpu_unhalt(cs->cpu_index);
379 if (cs->halted) {
380 cs->halted = 0;
381 cs->exception_index = -1;
385 unsigned int s390_cpu_set_state(uint8_t cpu_state, S390CPU *cpu)
387 trace_cpu_set_state(CPU(cpu)->cpu_index, cpu_state);
389 switch (cpu_state) {
390 case CPU_STATE_STOPPED:
391 case CPU_STATE_CHECK_STOP:
392 /* halt the cpu for common infrastructure */
393 s390_cpu_halt(cpu);
394 break;
395 case CPU_STATE_OPERATING:
396 case CPU_STATE_LOAD:
397 /* unhalt the cpu for common infrastructure */
398 s390_cpu_unhalt(cpu);
399 break;
400 default:
401 error_report("Requested CPU state is not a valid S390 CPU state: %u",
402 cpu_state);
403 exit(1);
405 if (kvm_enabled() && cpu->env.cpu_state != cpu_state) {
406 kvm_s390_set_cpu_state(cpu, cpu_state);
408 cpu->env.cpu_state = cpu_state;
410 return s390_count_running_cpus();
412 #endif
414 static gchar *s390_gdb_arch_name(CPUState *cs)
416 return g_strdup("s390:64-bit");
419 static void s390_cpu_class_init(ObjectClass *oc, void *data)
421 S390CPUClass *scc = S390_CPU_CLASS(oc);
422 CPUClass *cc = CPU_CLASS(scc);
423 DeviceClass *dc = DEVICE_CLASS(oc);
425 scc->next_cpu_id = 0;
426 scc->parent_realize = dc->realize;
427 dc->realize = s390_cpu_realizefn;
429 scc->parent_reset = cc->reset;
430 #if !defined(CONFIG_USER_ONLY)
431 scc->load_normal = s390_cpu_load_normal;
432 #endif
433 scc->cpu_reset = s390_cpu_reset;
434 scc->initial_cpu_reset = s390_cpu_initial_reset;
435 cc->reset = s390_cpu_full_reset;
436 cc->has_work = s390_cpu_has_work;
437 cc->do_interrupt = s390_cpu_do_interrupt;
438 cc->dump_state = s390_cpu_dump_state;
439 cc->set_pc = s390_cpu_set_pc;
440 cc->gdb_read_register = s390_cpu_gdb_read_register;
441 cc->gdb_write_register = s390_cpu_gdb_write_register;
442 #ifdef CONFIG_USER_ONLY
443 cc->handle_mmu_fault = s390_cpu_handle_mmu_fault;
444 #else
445 cc->get_phys_page_debug = s390_cpu_get_phys_page_debug;
446 cc->vmsd = &vmstate_s390_cpu;
447 cc->write_elf64_note = s390_cpu_write_elf64_note;
448 cc->cpu_exec_interrupt = s390_cpu_exec_interrupt;
449 cc->debug_excp_handler = s390x_cpu_debug_excp_handler;
450 #endif
451 cc->disas_set_info = s390_cpu_disas_set_info;
453 cc->gdb_num_core_regs = S390_NUM_CORE_REGS;
454 cc->gdb_core_xml_file = "s390x-core64.xml";
455 cc->gdb_arch_name = s390_gdb_arch_name;
458 * Reason: s390_cpu_realizefn() calls cpu_exec_init(), which saves
459 * the object in cpus -> dangling pointer after final
460 * object_unref().
462 dc->cannot_destroy_with_object_finalize_yet = true;
465 static const TypeInfo s390_cpu_type_info = {
466 .name = TYPE_S390_CPU,
467 .parent = TYPE_CPU,
468 .instance_size = sizeof(S390CPU),
469 .instance_init = s390_cpu_initfn,
470 .instance_finalize = s390_cpu_finalize,
471 .abstract = false,
472 .class_size = sizeof(S390CPUClass),
473 .class_init = s390_cpu_class_init,
476 static void s390_cpu_register_types(void)
478 type_register_static(&s390_cpu_type_info);
481 type_init(s390_cpu_register_types)