vfio/pci: Convert all MemoryRegion to dynamic alloc and consistent functions
[qemu/ar7.git] / target-s390x / cpu.c
blob73a910d2faebd103c814ff2fd034cbaa586f8d55
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 "cpu.h"
28 #include "qemu-common.h"
29 #include "qemu/timer.h"
30 #include "qemu/error-report.h"
31 #include "hw/hw.h"
32 #include "trace.h"
33 #ifndef CONFIG_USER_ONLY
34 #include "sysemu/arch_init.h"
35 #endif
37 #define CR0_RESET 0xE0UL
38 #define CR14_RESET 0xC2000000UL;
40 /* generate CPU information for cpu -? */
41 void s390_cpu_list(FILE *f, fprintf_function cpu_fprintf)
43 #ifdef CONFIG_KVM
44 (*cpu_fprintf)(f, "s390 %16s\n", "host");
45 #endif
48 #ifndef CONFIG_USER_ONLY
49 CpuDefinitionInfoList *arch_query_cpu_definitions(Error **errp)
51 CpuDefinitionInfoList *entry;
52 CpuDefinitionInfo *info;
54 info = g_malloc0(sizeof(*info));
55 info->name = g_strdup("host");
57 entry = g_malloc0(sizeof(*entry));
58 entry->value = info;
60 return entry;
62 #endif
64 static void s390_cpu_set_pc(CPUState *cs, vaddr value)
66 S390CPU *cpu = S390_CPU(cs);
68 cpu->env.psw.addr = value;
71 static bool s390_cpu_has_work(CPUState *cs)
73 S390CPU *cpu = S390_CPU(cs);
74 CPUS390XState *env = &cpu->env;
76 return (cs->interrupt_request & CPU_INTERRUPT_HARD) &&
77 (env->psw.mask & PSW_MASK_EXT);
80 #if !defined(CONFIG_USER_ONLY)
81 /* S390CPUClass::load_normal() */
82 static void s390_cpu_load_normal(CPUState *s)
84 S390CPU *cpu = S390_CPU(s);
85 cpu->env.psw.addr = ldl_phys(s->as, 4) & PSW_MASK_ESA_ADDR;
86 cpu->env.psw.mask = PSW_MASK_32 | PSW_MASK_64;
87 s390_cpu_set_state(CPU_STATE_OPERATING, cpu);
89 #endif
91 /* S390CPUClass::cpu_reset() */
92 static void s390_cpu_reset(CPUState *s)
94 S390CPU *cpu = S390_CPU(s);
95 S390CPUClass *scc = S390_CPU_GET_CLASS(cpu);
96 CPUS390XState *env = &cpu->env;
98 env->pfault_token = -1UL;
99 scc->parent_reset(s);
100 cpu->env.sigp_order = 0;
101 s390_cpu_set_state(CPU_STATE_STOPPED, cpu);
102 tlb_flush(s, 1);
105 /* S390CPUClass::initial_reset() */
106 static void s390_cpu_initial_reset(CPUState *s)
108 S390CPU *cpu = S390_CPU(s);
109 CPUS390XState *env = &cpu->env;
110 int i;
112 s390_cpu_reset(s);
113 /* initial reset does not touch regs,fregs and aregs */
114 memset(&env->fpc, 0, offsetof(CPUS390XState, cpu_num) -
115 offsetof(CPUS390XState, fpc));
117 /* architectured initial values for CR 0 and 14 */
118 env->cregs[0] = CR0_RESET;
119 env->cregs[14] = CR14_RESET;
121 /* architectured initial value for Breaking-Event-Address register */
122 env->gbea = 1;
124 env->pfault_token = -1UL;
125 env->ext_index = -1;
126 for (i = 0; i < ARRAY_SIZE(env->io_index); i++) {
127 env->io_index[i] = -1;
130 /* tininess for underflow is detected before rounding */
131 set_float_detect_tininess(float_tininess_before_rounding,
132 &env->fpu_status);
134 /* Reset state inside the kernel that we cannot access yet from QEMU. */
135 if (kvm_enabled()) {
136 kvm_s390_reset_vcpu(cpu);
138 tlb_flush(s, 1);
141 /* CPUClass:reset() */
142 static void s390_cpu_full_reset(CPUState *s)
144 S390CPU *cpu = S390_CPU(s);
145 S390CPUClass *scc = S390_CPU_GET_CLASS(cpu);
146 CPUS390XState *env = &cpu->env;
147 int i;
149 scc->parent_reset(s);
150 cpu->env.sigp_order = 0;
151 s390_cpu_set_state(CPU_STATE_STOPPED, cpu);
153 memset(env, 0, offsetof(CPUS390XState, cpu_num));
155 /* architectured initial values for CR 0 and 14 */
156 env->cregs[0] = CR0_RESET;
157 env->cregs[14] = CR14_RESET;
159 /* architectured initial value for Breaking-Event-Address register */
160 env->gbea = 1;
162 env->pfault_token = -1UL;
163 env->ext_index = -1;
164 for (i = 0; i < ARRAY_SIZE(env->io_index); i++) {
165 env->io_index[i] = -1;
168 /* tininess for underflow is detected before rounding */
169 set_float_detect_tininess(float_tininess_before_rounding,
170 &env->fpu_status);
172 /* Reset state inside the kernel that we cannot access yet from QEMU. */
173 if (kvm_enabled()) {
174 kvm_s390_reset_vcpu(cpu);
176 tlb_flush(s, 1);
179 #if !defined(CONFIG_USER_ONLY)
180 static void s390_cpu_machine_reset_cb(void *opaque)
182 S390CPU *cpu = opaque;
184 run_on_cpu(CPU(cpu), s390_do_cpu_full_reset, CPU(cpu));
186 #endif
188 static void s390_cpu_disas_set_info(CPUState *cpu, disassemble_info *info)
190 info->mach = bfd_mach_s390_64;
191 info->print_insn = print_insn_s390;
194 static void s390_cpu_realizefn(DeviceState *dev, Error **errp)
196 CPUState *cs = CPU(dev);
197 S390CPUClass *scc = S390_CPU_GET_CLASS(dev);
199 s390_cpu_gdb_init(cs);
200 qemu_init_vcpu(cs);
201 #if !defined(CONFIG_USER_ONLY)
202 run_on_cpu(cs, s390_do_cpu_full_reset, cs);
203 #else
204 cpu_reset(cs);
205 #endif
207 scc->parent_realize(dev, errp);
210 static void s390_cpu_initfn(Object *obj)
212 CPUState *cs = CPU(obj);
213 S390CPU *cpu = S390_CPU(obj);
214 CPUS390XState *env = &cpu->env;
215 static bool inited;
216 static int cpu_num = 0;
217 #if !defined(CONFIG_USER_ONLY)
218 struct tm tm;
219 #endif
221 cs->env_ptr = env;
222 cpu_exec_init(cs, &error_abort);
223 #if !defined(CONFIG_USER_ONLY)
224 qemu_register_reset(s390_cpu_machine_reset_cb, cpu);
225 qemu_get_timedate(&tm, 0);
226 env->tod_offset = TOD_UNIX_EPOCH +
227 (time2tod(mktimegm(&tm)) * 1000000000ULL);
228 env->tod_basetime = 0;
229 env->tod_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, s390x_tod_timer, cpu);
230 env->cpu_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, s390x_cpu_timer, cpu);
231 s390_cpu_set_state(CPU_STATE_STOPPED, cpu);
232 #endif
233 env->cpu_num = cpu_num++;
235 if (tcg_enabled() && !inited) {
236 inited = true;
237 s390x_translate_init();
241 static void s390_cpu_finalize(Object *obj)
243 #if !defined(CONFIG_USER_ONLY)
244 S390CPU *cpu = S390_CPU(obj);
246 qemu_unregister_reset(s390_cpu_machine_reset_cb, cpu);
247 g_free(cpu->irqstate);
248 #endif
251 #if !defined(CONFIG_USER_ONLY)
252 static bool disabled_wait(CPUState *cpu)
254 return cpu->halted && !(S390_CPU(cpu)->env.psw.mask &
255 (PSW_MASK_IO | PSW_MASK_EXT | PSW_MASK_MCHECK));
258 static unsigned s390_count_running_cpus(void)
260 CPUState *cpu;
261 int nr_running = 0;
263 CPU_FOREACH(cpu) {
264 uint8_t state = S390_CPU(cpu)->env.cpu_state;
265 if (state == CPU_STATE_OPERATING ||
266 state == CPU_STATE_LOAD) {
267 if (!disabled_wait(cpu)) {
268 nr_running++;
273 return nr_running;
276 unsigned int s390_cpu_halt(S390CPU *cpu)
278 CPUState *cs = CPU(cpu);
279 trace_cpu_halt(cs->cpu_index);
281 if (!cs->halted) {
282 cs->halted = 1;
283 cs->exception_index = EXCP_HLT;
286 return s390_count_running_cpus();
289 void s390_cpu_unhalt(S390CPU *cpu)
291 CPUState *cs = CPU(cpu);
292 trace_cpu_unhalt(cs->cpu_index);
294 if (cs->halted) {
295 cs->halted = 0;
296 cs->exception_index = -1;
300 unsigned int s390_cpu_set_state(uint8_t cpu_state, S390CPU *cpu)
302 trace_cpu_set_state(CPU(cpu)->cpu_index, cpu_state);
304 switch (cpu_state) {
305 case CPU_STATE_STOPPED:
306 case CPU_STATE_CHECK_STOP:
307 /* halt the cpu for common infrastructure */
308 s390_cpu_halt(cpu);
309 break;
310 case CPU_STATE_OPERATING:
311 case CPU_STATE_LOAD:
312 /* unhalt the cpu for common infrastructure */
313 s390_cpu_unhalt(cpu);
314 break;
315 default:
316 error_report("Requested CPU state is not a valid S390 CPU state: %u",
317 cpu_state);
318 exit(1);
320 if (kvm_enabled() && cpu->env.cpu_state != cpu_state) {
321 kvm_s390_set_cpu_state(cpu, cpu_state);
323 cpu->env.cpu_state = cpu_state;
325 return s390_count_running_cpus();
327 #endif
329 static gchar *s390_gdb_arch_name(CPUState *cs)
331 return g_strdup("s390:64-bit");
334 static void s390_cpu_class_init(ObjectClass *oc, void *data)
336 S390CPUClass *scc = S390_CPU_CLASS(oc);
337 CPUClass *cc = CPU_CLASS(scc);
338 DeviceClass *dc = DEVICE_CLASS(oc);
340 scc->parent_realize = dc->realize;
341 dc->realize = s390_cpu_realizefn;
343 scc->parent_reset = cc->reset;
344 #if !defined(CONFIG_USER_ONLY)
345 scc->load_normal = s390_cpu_load_normal;
346 #endif
347 scc->cpu_reset = s390_cpu_reset;
348 scc->initial_cpu_reset = s390_cpu_initial_reset;
349 cc->reset = s390_cpu_full_reset;
350 cc->has_work = s390_cpu_has_work;
351 cc->do_interrupt = s390_cpu_do_interrupt;
352 cc->dump_state = s390_cpu_dump_state;
353 cc->set_pc = s390_cpu_set_pc;
354 cc->gdb_read_register = s390_cpu_gdb_read_register;
355 cc->gdb_write_register = s390_cpu_gdb_write_register;
356 #ifdef CONFIG_USER_ONLY
357 cc->handle_mmu_fault = s390_cpu_handle_mmu_fault;
358 #else
359 cc->get_phys_page_debug = s390_cpu_get_phys_page_debug;
360 cc->vmsd = &vmstate_s390_cpu;
361 cc->write_elf64_note = s390_cpu_write_elf64_note;
362 cc->cpu_exec_interrupt = s390_cpu_exec_interrupt;
363 cc->debug_excp_handler = s390x_cpu_debug_excp_handler;
364 #endif
365 cc->disas_set_info = s390_cpu_disas_set_info;
367 cc->gdb_num_core_regs = S390_NUM_CORE_REGS;
368 cc->gdb_core_xml_file = "s390x-core64.xml";
369 cc->gdb_arch_name = s390_gdb_arch_name;
372 * Reason: s390_cpu_initfn() calls cpu_exec_init(), which saves
373 * the object in cpus -> dangling pointer after final
374 * object_unref().
376 dc->cannot_destroy_with_object_finalize_yet = true;
379 static const TypeInfo s390_cpu_type_info = {
380 .name = TYPE_S390_CPU,
381 .parent = TYPE_CPU,
382 .instance_size = sizeof(S390CPU),
383 .instance_init = s390_cpu_initfn,
384 .instance_finalize = s390_cpu_finalize,
385 .abstract = false,
386 .class_size = sizeof(S390CPUClass),
387 .class_init = s390_cpu_class_init,
390 static void s390_cpu_register_types(void)
392 type_register_static(&s390_cpu_type_info);
395 type_init(s390_cpu_register_types)