target-arm: Expose output GPIO line for VCPU maintenance interrupt
[qemu/ar7.git] / target / s390x / cpu.c
blob066dcd17df964b776865912416f5e6cedba3ef34
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 static void s390_cpu_set_pc(CPUState *cs, vaddr value)
49 S390CPU *cpu = S390_CPU(cs);
51 cpu->env.psw.addr = value;
54 static bool s390_cpu_has_work(CPUState *cs)
56 S390CPU *cpu = S390_CPU(cs);
57 CPUS390XState *env = &cpu->env;
59 return (cs->interrupt_request & CPU_INTERRUPT_HARD) &&
60 (env->psw.mask & PSW_MASK_EXT);
63 #if !defined(CONFIG_USER_ONLY)
64 /* S390CPUClass::load_normal() */
65 static void s390_cpu_load_normal(CPUState *s)
67 S390CPU *cpu = S390_CPU(s);
68 cpu->env.psw.addr = ldl_phys(s->as, 4) & PSW_MASK_ESA_ADDR;
69 cpu->env.psw.mask = PSW_MASK_32 | PSW_MASK_64;
70 s390_cpu_set_state(CPU_STATE_OPERATING, cpu);
72 #endif
74 /* S390CPUClass::cpu_reset() */
75 static void s390_cpu_reset(CPUState *s)
77 S390CPU *cpu = S390_CPU(s);
78 S390CPUClass *scc = S390_CPU_GET_CLASS(cpu);
79 CPUS390XState *env = &cpu->env;
81 env->pfault_token = -1UL;
82 scc->parent_reset(s);
83 cpu->env.sigp_order = 0;
84 s390_cpu_set_state(CPU_STATE_STOPPED, cpu);
87 /* S390CPUClass::initial_reset() */
88 static void s390_cpu_initial_reset(CPUState *s)
90 S390CPU *cpu = S390_CPU(s);
91 CPUS390XState *env = &cpu->env;
92 int i;
94 s390_cpu_reset(s);
95 /* initial reset does not touch regs,fregs and aregs */
96 memset(&env->fpc, 0, offsetof(CPUS390XState, end_reset_fields) -
97 offsetof(CPUS390XState, fpc));
99 /* architectured initial values for CR 0 and 14 */
100 env->cregs[0] = CR0_RESET;
101 env->cregs[14] = CR14_RESET;
103 /* architectured initial value for Breaking-Event-Address register */
104 env->gbea = 1;
106 env->pfault_token = -1UL;
107 env->ext_index = -1;
108 for (i = 0; i < ARRAY_SIZE(env->io_index); i++) {
109 env->io_index[i] = -1;
112 /* tininess for underflow is detected before rounding */
113 set_float_detect_tininess(float_tininess_before_rounding,
114 &env->fpu_status);
116 /* Reset state inside the kernel that we cannot access yet from QEMU. */
117 if (kvm_enabled()) {
118 kvm_s390_reset_vcpu(cpu);
122 /* CPUClass:reset() */
123 static void s390_cpu_full_reset(CPUState *s)
125 S390CPU *cpu = S390_CPU(s);
126 S390CPUClass *scc = S390_CPU_GET_CLASS(cpu);
127 CPUS390XState *env = &cpu->env;
128 int i;
130 scc->parent_reset(s);
131 cpu->env.sigp_order = 0;
132 s390_cpu_set_state(CPU_STATE_STOPPED, cpu);
134 memset(env, 0, offsetof(CPUS390XState, end_reset_fields));
136 /* architectured initial values for CR 0 and 14 */
137 env->cregs[0] = CR0_RESET;
138 env->cregs[14] = CR14_RESET;
140 /* architectured initial value for Breaking-Event-Address register */
141 env->gbea = 1;
143 env->pfault_token = -1UL;
144 env->ext_index = -1;
145 for (i = 0; i < ARRAY_SIZE(env->io_index); i++) {
146 env->io_index[i] = -1;
149 /* tininess for underflow is detected before rounding */
150 set_float_detect_tininess(float_tininess_before_rounding,
151 &env->fpu_status);
153 /* Reset state inside the kernel that we cannot access yet from QEMU. */
154 if (kvm_enabled()) {
155 kvm_s390_reset_vcpu(cpu);
159 #if !defined(CONFIG_USER_ONLY)
160 static void s390_cpu_machine_reset_cb(void *opaque)
162 S390CPU *cpu = opaque;
164 run_on_cpu(CPU(cpu), s390_do_cpu_full_reset, RUN_ON_CPU_NULL);
166 #endif
168 static void s390_cpu_disas_set_info(CPUState *cpu, disassemble_info *info)
170 info->mach = bfd_mach_s390_64;
171 info->print_insn = print_insn_s390;
174 static void s390_cpu_realizefn(DeviceState *dev, Error **errp)
176 CPUState *cs = CPU(dev);
177 S390CPUClass *scc = S390_CPU_GET_CLASS(dev);
178 S390CPU *cpu = S390_CPU(dev);
179 CPUS390XState *env = &cpu->env;
180 Error *err = NULL;
182 /* the model has to be realized before qemu_init_vcpu() due to kvm */
183 s390_realize_cpu_model(cs, &err);
184 if (err) {
185 goto out;
188 #if !defined(CONFIG_USER_ONLY)
189 if (cpu->id >= max_cpus) {
190 error_setg(&err, "Unable to add CPU: %" PRIi64
191 ", max allowed: %d", cpu->id, max_cpus - 1);
192 goto out;
194 #endif
195 if (cpu_exists(cpu->id)) {
196 error_setg(&err, "Unable to add CPU: %" PRIi64
197 ", it already exists", cpu->id);
198 goto out;
200 if (cpu->id != scc->next_cpu_id) {
201 error_setg(&err, "Unable to add CPU: %" PRIi64
202 ", The next available id is %" PRIi64, cpu->id,
203 scc->next_cpu_id);
204 goto out;
207 cpu_exec_realizefn(cs, &err);
208 if (err != NULL) {
209 goto out;
211 scc->next_cpu_id++;
213 #if !defined(CONFIG_USER_ONLY)
214 qemu_register_reset(s390_cpu_machine_reset_cb, cpu);
215 #endif
216 env->cpu_num = cpu->id;
217 s390_cpu_gdb_init(cs);
218 qemu_init_vcpu(cs);
219 #if !defined(CONFIG_USER_ONLY)
220 run_on_cpu(cs, s390_do_cpu_full_reset, RUN_ON_CPU_NULL);
221 #else
222 cpu_reset(cs);
223 #endif
225 scc->parent_realize(dev, &err);
227 #if !defined(CONFIG_USER_ONLY)
228 if (dev->hotplugged) {
229 raise_irq_cpu_hotplug();
231 #endif
233 out:
234 error_propagate(errp, err);
237 static void s390x_cpu_get_id(Object *obj, Visitor *v, const char *name,
238 void *opaque, Error **errp)
240 S390CPU *cpu = S390_CPU(obj);
241 int64_t value = cpu->id;
243 visit_type_int(v, name, &value, errp);
246 static void s390x_cpu_set_id(Object *obj, Visitor *v, const char *name,
247 void *opaque, Error **errp)
249 S390CPU *cpu = S390_CPU(obj);
250 DeviceState *dev = DEVICE(obj);
251 const int64_t min = 0;
252 const int64_t max = UINT32_MAX;
253 Error *err = NULL;
254 int64_t value;
256 if (dev->realized) {
257 error_setg(errp, "Attempt to set property '%s' on '%s' after "
258 "it was realized", name, object_get_typename(obj));
259 return;
262 visit_type_int(v, name, &value, &err);
263 if (err) {
264 error_propagate(errp, err);
265 return;
267 if (value < min || value > max) {
268 error_setg(errp, "Property %s.%s doesn't take value %" PRId64
269 " (minimum: %" PRId64 ", maximum: %" PRId64 ")" ,
270 object_get_typename(obj), name, value, min, max);
271 return;
273 cpu->id = value;
276 static void s390_cpu_initfn(Object *obj)
278 CPUState *cs = CPU(obj);
279 S390CPU *cpu = S390_CPU(obj);
280 CPUS390XState *env = &cpu->env;
281 static bool inited;
282 #if !defined(CONFIG_USER_ONLY)
283 struct tm tm;
284 #endif
286 cs->env_ptr = env;
287 cs->halted = 1;
288 cs->exception_index = EXCP_HLT;
289 object_property_add(OBJECT(cpu), "id", "int64_t", s390x_cpu_get_id,
290 s390x_cpu_set_id, NULL, NULL, NULL);
291 s390_cpu_model_register_props(obj);
292 #if !defined(CONFIG_USER_ONLY)
293 qemu_get_timedate(&tm, 0);
294 env->tod_offset = TOD_UNIX_EPOCH +
295 (time2tod(mktimegm(&tm)) * 1000000000ULL);
296 env->tod_basetime = 0;
297 env->tod_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, s390x_tod_timer, cpu);
298 env->cpu_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, s390x_cpu_timer, cpu);
299 s390_cpu_set_state(CPU_STATE_STOPPED, cpu);
300 #endif
302 if (tcg_enabled() && !inited) {
303 inited = true;
304 s390x_translate_init();
308 static void s390_cpu_finalize(Object *obj)
310 #if !defined(CONFIG_USER_ONLY)
311 S390CPU *cpu = S390_CPU(obj);
313 qemu_unregister_reset(s390_cpu_machine_reset_cb, cpu);
314 g_free(cpu->irqstate);
315 #endif
318 #if !defined(CONFIG_USER_ONLY)
319 static bool disabled_wait(CPUState *cpu)
321 return cpu->halted && !(S390_CPU(cpu)->env.psw.mask &
322 (PSW_MASK_IO | PSW_MASK_EXT | PSW_MASK_MCHECK));
325 static unsigned s390_count_running_cpus(void)
327 CPUState *cpu;
328 int nr_running = 0;
330 CPU_FOREACH(cpu) {
331 uint8_t state = S390_CPU(cpu)->env.cpu_state;
332 if (state == CPU_STATE_OPERATING ||
333 state == CPU_STATE_LOAD) {
334 if (!disabled_wait(cpu)) {
335 nr_running++;
340 return nr_running;
343 unsigned int s390_cpu_halt(S390CPU *cpu)
345 CPUState *cs = CPU(cpu);
346 trace_cpu_halt(cs->cpu_index);
348 if (!cs->halted) {
349 cs->halted = 1;
350 cs->exception_index = EXCP_HLT;
353 return s390_count_running_cpus();
356 void s390_cpu_unhalt(S390CPU *cpu)
358 CPUState *cs = CPU(cpu);
359 trace_cpu_unhalt(cs->cpu_index);
361 if (cs->halted) {
362 cs->halted = 0;
363 cs->exception_index = -1;
367 unsigned int s390_cpu_set_state(uint8_t cpu_state, S390CPU *cpu)
369 trace_cpu_set_state(CPU(cpu)->cpu_index, cpu_state);
371 switch (cpu_state) {
372 case CPU_STATE_STOPPED:
373 case CPU_STATE_CHECK_STOP:
374 /* halt the cpu for common infrastructure */
375 s390_cpu_halt(cpu);
376 break;
377 case CPU_STATE_OPERATING:
378 case CPU_STATE_LOAD:
379 /* unhalt the cpu for common infrastructure */
380 s390_cpu_unhalt(cpu);
381 break;
382 default:
383 error_report("Requested CPU state is not a valid S390 CPU state: %u",
384 cpu_state);
385 exit(1);
387 if (kvm_enabled() && cpu->env.cpu_state != cpu_state) {
388 kvm_s390_set_cpu_state(cpu, cpu_state);
390 cpu->env.cpu_state = cpu_state;
392 return s390_count_running_cpus();
394 #endif
396 static gchar *s390_gdb_arch_name(CPUState *cs)
398 return g_strdup("s390:64-bit");
401 static void s390_cpu_class_init(ObjectClass *oc, void *data)
403 S390CPUClass *scc = S390_CPU_CLASS(oc);
404 CPUClass *cc = CPU_CLASS(scc);
405 DeviceClass *dc = DEVICE_CLASS(oc);
407 scc->next_cpu_id = 0;
408 scc->parent_realize = dc->realize;
409 dc->realize = s390_cpu_realizefn;
411 scc->parent_reset = cc->reset;
412 #if !defined(CONFIG_USER_ONLY)
413 scc->load_normal = s390_cpu_load_normal;
414 #endif
415 scc->cpu_reset = s390_cpu_reset;
416 scc->initial_cpu_reset = s390_cpu_initial_reset;
417 cc->reset = s390_cpu_full_reset;
418 cc->class_by_name = s390_cpu_class_by_name,
419 cc->has_work = s390_cpu_has_work;
420 cc->do_interrupt = s390_cpu_do_interrupt;
421 cc->dump_state = s390_cpu_dump_state;
422 cc->set_pc = s390_cpu_set_pc;
423 cc->gdb_read_register = s390_cpu_gdb_read_register;
424 cc->gdb_write_register = s390_cpu_gdb_write_register;
425 #ifdef CONFIG_USER_ONLY
426 cc->handle_mmu_fault = s390_cpu_handle_mmu_fault;
427 #else
428 cc->get_phys_page_debug = s390_cpu_get_phys_page_debug;
429 cc->vmsd = &vmstate_s390_cpu;
430 cc->write_elf64_note = s390_cpu_write_elf64_note;
431 cc->cpu_exec_interrupt = s390_cpu_exec_interrupt;
432 cc->debug_excp_handler = s390x_cpu_debug_excp_handler;
433 #endif
434 cc->disas_set_info = s390_cpu_disas_set_info;
436 cc->gdb_num_core_regs = S390_NUM_CORE_REGS;
437 cc->gdb_core_xml_file = "s390x-core64.xml";
438 cc->gdb_arch_name = s390_gdb_arch_name;
440 s390_cpu_model_class_register_props(oc);
443 static const TypeInfo s390_cpu_type_info = {
444 .name = TYPE_S390_CPU,
445 .parent = TYPE_CPU,
446 .instance_size = sizeof(S390CPU),
447 .instance_init = s390_cpu_initfn,
448 .instance_finalize = s390_cpu_finalize,
449 .abstract = true,
450 .class_size = sizeof(S390CPUClass),
451 .class_init = s390_cpu_class_init,
454 static void s390_cpu_register_types(void)
456 type_register_static(&s390_cpu_type_info);
459 type_init(s390_cpu_register_types)