vhost-user-test: fix up rhel6 build
[qemu/ar7.git] / target-s390x / cpu.c
blob189a2afc0fe1f5878cb9a7a9e4b1d63850581c92
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 "cpu.h"
27 #include "qemu-common.h"
28 #include "qemu/timer.h"
29 #include "qemu/error-report.h"
30 #include "hw/hw.h"
31 #include "trace.h"
32 #ifndef CONFIG_USER_ONLY
33 #include "sysemu/arch_init.h"
34 #endif
36 #define CR0_RESET 0xE0UL
37 #define CR14_RESET 0xC2000000UL;
39 /* generate CPU information for cpu -? */
40 void s390_cpu_list(FILE *f, fprintf_function cpu_fprintf)
42 #ifdef CONFIG_KVM
43 (*cpu_fprintf)(f, "s390 %16s\n", "host");
44 #endif
47 #ifndef CONFIG_USER_ONLY
48 CpuDefinitionInfoList *arch_query_cpu_definitions(Error **errp)
50 CpuDefinitionInfoList *entry;
51 CpuDefinitionInfo *info;
53 info = g_malloc0(sizeof(*info));
54 info->name = g_strdup("host");
56 entry = g_malloc0(sizeof(*entry));
57 entry->value = info;
59 return entry;
61 #endif
63 static void s390_cpu_set_pc(CPUState *cs, vaddr value)
65 S390CPU *cpu = S390_CPU(cs);
67 cpu->env.psw.addr = value;
70 static bool s390_cpu_has_work(CPUState *cs)
72 S390CPU *cpu = S390_CPU(cs);
73 CPUS390XState *env = &cpu->env;
75 return (cs->interrupt_request & CPU_INTERRUPT_HARD) &&
76 (env->psw.mask & PSW_MASK_EXT);
79 #if !defined(CONFIG_USER_ONLY)
80 /* S390CPUClass::load_normal() */
81 static void s390_cpu_load_normal(CPUState *s)
83 S390CPU *cpu = S390_CPU(s);
84 cpu->env.psw.addr = ldl_phys(s->as, 4) & PSW_MASK_ESA_ADDR;
85 cpu->env.psw.mask = PSW_MASK_32 | PSW_MASK_64;
86 s390_cpu_set_state(CPU_STATE_OPERATING, cpu);
88 #endif
90 /* S390CPUClass::cpu_reset() */
91 static void s390_cpu_reset(CPUState *s)
93 S390CPU *cpu = S390_CPU(s);
94 S390CPUClass *scc = S390_CPU_GET_CLASS(cpu);
95 CPUS390XState *env = &cpu->env;
97 env->pfault_token = -1UL;
98 scc->parent_reset(s);
99 cpu->env.sigp_order = 0;
100 s390_cpu_set_state(CPU_STATE_STOPPED, cpu);
101 tlb_flush(s, 1);
104 /* S390CPUClass::initial_reset() */
105 static void s390_cpu_initial_reset(CPUState *s)
107 S390CPU *cpu = S390_CPU(s);
108 CPUS390XState *env = &cpu->env;
109 int i;
111 s390_cpu_reset(s);
112 /* initial reset does not touch regs,fregs and aregs */
113 memset(&env->fpc, 0, offsetof(CPUS390XState, cpu_num) -
114 offsetof(CPUS390XState, fpc));
116 /* architectured initial values for CR 0 and 14 */
117 env->cregs[0] = CR0_RESET;
118 env->cregs[14] = CR14_RESET;
120 /* architectured initial value for Breaking-Event-Address register */
121 env->gbea = 1;
123 env->pfault_token = -1UL;
124 env->ext_index = -1;
125 for (i = 0; i < ARRAY_SIZE(env->io_index); i++) {
126 env->io_index[i] = -1;
129 /* tininess for underflow is detected before rounding */
130 set_float_detect_tininess(float_tininess_before_rounding,
131 &env->fpu_status);
133 /* Reset state inside the kernel that we cannot access yet from QEMU. */
134 if (kvm_enabled()) {
135 kvm_s390_reset_vcpu(cpu);
137 tlb_flush(s, 1);
140 /* CPUClass:reset() */
141 static void s390_cpu_full_reset(CPUState *s)
143 S390CPU *cpu = S390_CPU(s);
144 S390CPUClass *scc = S390_CPU_GET_CLASS(cpu);
145 CPUS390XState *env = &cpu->env;
146 int i;
148 scc->parent_reset(s);
149 cpu->env.sigp_order = 0;
150 s390_cpu_set_state(CPU_STATE_STOPPED, cpu);
152 memset(env, 0, offsetof(CPUS390XState, cpu_num));
154 /* architectured initial values for CR 0 and 14 */
155 env->cregs[0] = CR0_RESET;
156 env->cregs[14] = CR14_RESET;
158 /* architectured initial value for Breaking-Event-Address register */
159 env->gbea = 1;
161 env->pfault_token = -1UL;
162 env->ext_index = -1;
163 for (i = 0; i < ARRAY_SIZE(env->io_index); i++) {
164 env->io_index[i] = -1;
167 /* tininess for underflow is detected before rounding */
168 set_float_detect_tininess(float_tininess_before_rounding,
169 &env->fpu_status);
171 /* Reset state inside the kernel that we cannot access yet from QEMU. */
172 if (kvm_enabled()) {
173 kvm_s390_reset_vcpu(cpu);
175 tlb_flush(s, 1);
178 #if !defined(CONFIG_USER_ONLY)
179 static void s390_cpu_machine_reset_cb(void *opaque)
181 S390CPU *cpu = opaque;
183 run_on_cpu(CPU(cpu), s390_do_cpu_full_reset, CPU(cpu));
185 #endif
187 static void s390_cpu_disas_set_info(CPUState *cpu, disassemble_info *info)
189 info->mach = bfd_mach_s390_64;
190 info->print_insn = print_insn_s390;
193 static void s390_cpu_realizefn(DeviceState *dev, Error **errp)
195 CPUState *cs = CPU(dev);
196 S390CPUClass *scc = S390_CPU_GET_CLASS(dev);
198 s390_cpu_gdb_init(cs);
199 qemu_init_vcpu(cs);
200 #if !defined(CONFIG_USER_ONLY)
201 run_on_cpu(cs, s390_do_cpu_full_reset, cs);
202 #else
203 cpu_reset(cs);
204 #endif
206 scc->parent_realize(dev, errp);
209 static void s390_cpu_initfn(Object *obj)
211 CPUState *cs = CPU(obj);
212 S390CPU *cpu = S390_CPU(obj);
213 CPUS390XState *env = &cpu->env;
214 static bool inited;
215 static int cpu_num = 0;
216 #if !defined(CONFIG_USER_ONLY)
217 struct tm tm;
218 #endif
220 cs->env_ptr = env;
221 cpu_exec_init(cs, &error_abort);
222 #if !defined(CONFIG_USER_ONLY)
223 qemu_register_reset(s390_cpu_machine_reset_cb, cpu);
224 qemu_get_timedate(&tm, 0);
225 env->tod_offset = TOD_UNIX_EPOCH +
226 (time2tod(mktimegm(&tm)) * 1000000000ULL);
227 env->tod_basetime = 0;
228 env->tod_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, s390x_tod_timer, cpu);
229 env->cpu_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, s390x_cpu_timer, cpu);
230 s390_cpu_set_state(CPU_STATE_STOPPED, cpu);
231 #endif
232 env->cpu_num = cpu_num++;
234 if (tcg_enabled() && !inited) {
235 inited = true;
236 s390x_translate_init();
240 static void s390_cpu_finalize(Object *obj)
242 #if !defined(CONFIG_USER_ONLY)
243 S390CPU *cpu = S390_CPU(obj);
245 qemu_unregister_reset(s390_cpu_machine_reset_cb, cpu);
246 g_free(cpu->irqstate);
247 #endif
250 #if !defined(CONFIG_USER_ONLY)
251 static bool disabled_wait(CPUState *cpu)
253 return cpu->halted && !(S390_CPU(cpu)->env.psw.mask &
254 (PSW_MASK_IO | PSW_MASK_EXT | PSW_MASK_MCHECK));
257 static unsigned s390_count_running_cpus(void)
259 CPUState *cpu;
260 int nr_running = 0;
262 CPU_FOREACH(cpu) {
263 uint8_t state = S390_CPU(cpu)->env.cpu_state;
264 if (state == CPU_STATE_OPERATING ||
265 state == CPU_STATE_LOAD) {
266 if (!disabled_wait(cpu)) {
267 nr_running++;
272 return nr_running;
275 unsigned int s390_cpu_halt(S390CPU *cpu)
277 CPUState *cs = CPU(cpu);
278 trace_cpu_halt(cs->cpu_index);
280 if (!cs->halted) {
281 cs->halted = 1;
282 cs->exception_index = EXCP_HLT;
285 return s390_count_running_cpus();
288 void s390_cpu_unhalt(S390CPU *cpu)
290 CPUState *cs = CPU(cpu);
291 trace_cpu_unhalt(cs->cpu_index);
293 if (cs->halted) {
294 cs->halted = 0;
295 cs->exception_index = -1;
299 unsigned int s390_cpu_set_state(uint8_t cpu_state, S390CPU *cpu)
301 trace_cpu_set_state(CPU(cpu)->cpu_index, cpu_state);
303 switch (cpu_state) {
304 case CPU_STATE_STOPPED:
305 case CPU_STATE_CHECK_STOP:
306 /* halt the cpu for common infrastructure */
307 s390_cpu_halt(cpu);
308 break;
309 case CPU_STATE_OPERATING:
310 case CPU_STATE_LOAD:
311 /* unhalt the cpu for common infrastructure */
312 s390_cpu_unhalt(cpu);
313 break;
314 default:
315 error_report("Requested CPU state is not a valid S390 CPU state: %u",
316 cpu_state);
317 exit(1);
319 if (kvm_enabled() && cpu->env.cpu_state != cpu_state) {
320 kvm_s390_set_cpu_state(cpu, cpu_state);
322 cpu->env.cpu_state = cpu_state;
324 return s390_count_running_cpus();
326 #endif
328 static void s390_cpu_class_init(ObjectClass *oc, void *data)
330 S390CPUClass *scc = S390_CPU_CLASS(oc);
331 CPUClass *cc = CPU_CLASS(scc);
332 DeviceClass *dc = DEVICE_CLASS(oc);
334 scc->parent_realize = dc->realize;
335 dc->realize = s390_cpu_realizefn;
337 scc->parent_reset = cc->reset;
338 #if !defined(CONFIG_USER_ONLY)
339 scc->load_normal = s390_cpu_load_normal;
340 #endif
341 scc->cpu_reset = s390_cpu_reset;
342 scc->initial_cpu_reset = s390_cpu_initial_reset;
343 cc->reset = s390_cpu_full_reset;
344 cc->has_work = s390_cpu_has_work;
345 cc->do_interrupt = s390_cpu_do_interrupt;
346 cc->dump_state = s390_cpu_dump_state;
347 cc->set_pc = s390_cpu_set_pc;
348 cc->gdb_read_register = s390_cpu_gdb_read_register;
349 cc->gdb_write_register = s390_cpu_gdb_write_register;
350 #ifdef CONFIG_USER_ONLY
351 cc->handle_mmu_fault = s390_cpu_handle_mmu_fault;
352 #else
353 cc->get_phys_page_debug = s390_cpu_get_phys_page_debug;
354 cc->vmsd = &vmstate_s390_cpu;
355 cc->write_elf64_note = s390_cpu_write_elf64_note;
356 cc->write_elf64_qemunote = s390_cpu_write_elf64_qemunote;
357 cc->cpu_exec_interrupt = s390_cpu_exec_interrupt;
358 cc->debug_excp_handler = s390x_cpu_debug_excp_handler;
359 #endif
360 cc->disas_set_info = s390_cpu_disas_set_info;
362 cc->gdb_num_core_regs = S390_NUM_CORE_REGS;
363 cc->gdb_core_xml_file = "s390x-core64.xml";
366 * Reason: s390_cpu_initfn() calls cpu_exec_init(), which saves
367 * the object in cpus -> dangling pointer after final
368 * object_unref().
370 dc->cannot_destroy_with_object_finalize_yet = true;
373 static const TypeInfo s390_cpu_type_info = {
374 .name = TYPE_S390_CPU,
375 .parent = TYPE_CPU,
376 .instance_size = sizeof(S390CPU),
377 .instance_init = s390_cpu_initfn,
378 .instance_finalize = s390_cpu_finalize,
379 .abstract = false,
380 .class_size = sizeof(S390CPUClass),
381 .class_init = s390_cpu_class_init,
384 static void s390_cpu_register_types(void)
386 type_register_static(&s390_cpu_type_info);
389 type_init(s390_cpu_register_types)