target-ppc: Add PVR for POWER8NVL processor
[qemu/kevin.git] / target-s390x / cpu.c
blob1cbf70355d25ca9e4e2efea608ef4e68e9b3a4de
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 #include "qapi/visitor.h"
34 #ifndef CONFIG_USER_ONLY
35 #include "sysemu/arch_init.h"
36 #include "sysemu/sysemu.h"
37 #include "hw/s390x/sclp.h"
38 #endif
40 #define CR0_RESET 0xE0UL
41 #define CR14_RESET 0xC2000000UL;
43 /* generate CPU information for cpu -? */
44 void s390_cpu_list(FILE *f, fprintf_function cpu_fprintf)
46 #ifdef CONFIG_KVM
47 (*cpu_fprintf)(f, "s390 %16s\n", "host");
48 #endif
51 #ifndef CONFIG_USER_ONLY
52 CpuDefinitionInfoList *arch_query_cpu_definitions(Error **errp)
54 CpuDefinitionInfoList *entry;
55 CpuDefinitionInfo *info;
57 info = g_malloc0(sizeof(*info));
58 info->name = g_strdup("host");
60 entry = g_malloc0(sizeof(*entry));
61 entry->value = info;
63 return entry;
65 #endif
67 static void s390_cpu_set_pc(CPUState *cs, vaddr value)
69 S390CPU *cpu = S390_CPU(cs);
71 cpu->env.psw.addr = value;
74 static bool s390_cpu_has_work(CPUState *cs)
76 S390CPU *cpu = S390_CPU(cs);
77 CPUS390XState *env = &cpu->env;
79 return (cs->interrupt_request & CPU_INTERRUPT_HARD) &&
80 (env->psw.mask & PSW_MASK_EXT);
83 #if !defined(CONFIG_USER_ONLY)
84 /* S390CPUClass::load_normal() */
85 static void s390_cpu_load_normal(CPUState *s)
87 S390CPU *cpu = S390_CPU(s);
88 cpu->env.psw.addr = ldl_phys(s->as, 4) & PSW_MASK_ESA_ADDR;
89 cpu->env.psw.mask = PSW_MASK_32 | PSW_MASK_64;
90 s390_cpu_set_state(CPU_STATE_OPERATING, cpu);
92 #endif
94 /* S390CPUClass::cpu_reset() */
95 static void s390_cpu_reset(CPUState *s)
97 S390CPU *cpu = S390_CPU(s);
98 S390CPUClass *scc = S390_CPU_GET_CLASS(cpu);
99 CPUS390XState *env = &cpu->env;
101 env->pfault_token = -1UL;
102 scc->parent_reset(s);
103 cpu->env.sigp_order = 0;
104 s390_cpu_set_state(CPU_STATE_STOPPED, cpu);
105 tlb_flush(s, 1);
108 /* S390CPUClass::initial_reset() */
109 static void s390_cpu_initial_reset(CPUState *s)
111 S390CPU *cpu = S390_CPU(s);
112 CPUS390XState *env = &cpu->env;
113 int i;
115 s390_cpu_reset(s);
116 /* initial reset does not touch regs,fregs and aregs */
117 memset(&env->fpc, 0, offsetof(CPUS390XState, cpu_num) -
118 offsetof(CPUS390XState, fpc));
120 /* architectured initial values for CR 0 and 14 */
121 env->cregs[0] = CR0_RESET;
122 env->cregs[14] = CR14_RESET;
124 /* architectured initial value for Breaking-Event-Address register */
125 env->gbea = 1;
127 env->pfault_token = -1UL;
128 env->ext_index = -1;
129 for (i = 0; i < ARRAY_SIZE(env->io_index); i++) {
130 env->io_index[i] = -1;
133 /* tininess for underflow is detected before rounding */
134 set_float_detect_tininess(float_tininess_before_rounding,
135 &env->fpu_status);
137 /* Reset state inside the kernel that we cannot access yet from QEMU. */
138 if (kvm_enabled()) {
139 kvm_s390_reset_vcpu(cpu);
141 tlb_flush(s, 1);
144 /* CPUClass:reset() */
145 static void s390_cpu_full_reset(CPUState *s)
147 S390CPU *cpu = S390_CPU(s);
148 S390CPUClass *scc = S390_CPU_GET_CLASS(cpu);
149 CPUS390XState *env = &cpu->env;
150 int i;
152 scc->parent_reset(s);
153 cpu->env.sigp_order = 0;
154 s390_cpu_set_state(CPU_STATE_STOPPED, cpu);
156 memset(env, 0, offsetof(CPUS390XState, cpu_num));
158 /* architectured initial values for CR 0 and 14 */
159 env->cregs[0] = CR0_RESET;
160 env->cregs[14] = CR14_RESET;
162 /* architectured initial value for Breaking-Event-Address register */
163 env->gbea = 1;
165 env->pfault_token = -1UL;
166 env->ext_index = -1;
167 for (i = 0; i < ARRAY_SIZE(env->io_index); i++) {
168 env->io_index[i] = -1;
171 /* tininess for underflow is detected before rounding */
172 set_float_detect_tininess(float_tininess_before_rounding,
173 &env->fpu_status);
175 /* Reset state inside the kernel that we cannot access yet from QEMU. */
176 if (kvm_enabled()) {
177 kvm_s390_reset_vcpu(cpu);
179 tlb_flush(s, 1);
182 #if !defined(CONFIG_USER_ONLY)
183 static void s390_cpu_machine_reset_cb(void *opaque)
185 S390CPU *cpu = opaque;
187 run_on_cpu(CPU(cpu), s390_do_cpu_full_reset, CPU(cpu));
189 #endif
191 static void s390_cpu_disas_set_info(CPUState *cpu, disassemble_info *info)
193 info->mach = bfd_mach_s390_64;
194 info->print_insn = print_insn_s390;
197 static void s390_cpu_realizefn(DeviceState *dev, Error **errp)
199 CPUState *cs = CPU(dev);
200 S390CPUClass *scc = S390_CPU_GET_CLASS(dev);
201 S390CPU *cpu = S390_CPU(dev);
202 CPUS390XState *env = &cpu->env;
203 Error *err = NULL;
205 #if !defined(CONFIG_USER_ONLY)
206 if (cpu->id >= max_cpus) {
207 error_setg(&err, "Unable to add CPU: %" PRIi64
208 ", max allowed: %d", cpu->id, max_cpus - 1);
209 goto out;
211 #endif
212 if (cpu_exists(cpu->id)) {
213 error_setg(&err, "Unable to add CPU: %" PRIi64
214 ", it already exists", cpu->id);
215 goto out;
217 if (cpu->id != scc->next_cpu_id) {
218 error_setg(&err, "Unable to add CPU: %" PRIi64
219 ", The next available id is %" PRIi64, cpu->id,
220 scc->next_cpu_id);
221 goto out;
224 cpu_exec_init(cs, &err);
225 if (err != NULL) {
226 goto out;
228 scc->next_cpu_id++;
230 #if !defined(CONFIG_USER_ONLY)
231 qemu_register_reset(s390_cpu_machine_reset_cb, cpu);
232 #endif
233 env->cpu_num = cpu->id;
234 s390_cpu_gdb_init(cs);
235 qemu_init_vcpu(cs);
236 #if !defined(CONFIG_USER_ONLY)
237 run_on_cpu(cs, s390_do_cpu_full_reset, cs);
238 #else
239 cpu_reset(cs);
240 #endif
242 scc->parent_realize(dev, &err);
244 #if !defined(CONFIG_USER_ONLY)
245 if (dev->hotplugged) {
246 raise_irq_cpu_hotplug();
248 #endif
250 out:
251 error_propagate(errp, err);
254 static void s390x_cpu_get_id(Object *obj, Visitor *v, const char *name,
255 void *opaque, Error **errp)
257 S390CPU *cpu = S390_CPU(obj);
258 int64_t value = cpu->id;
260 visit_type_int(v, name, &value, errp);
263 static void s390x_cpu_set_id(Object *obj, Visitor *v, const char *name,
264 void *opaque, Error **errp)
266 S390CPU *cpu = S390_CPU(obj);
267 DeviceState *dev = DEVICE(obj);
268 const int64_t min = 0;
269 const int64_t max = UINT32_MAX;
270 Error *err = NULL;
271 int64_t value;
273 if (dev->realized) {
274 error_setg(errp, "Attempt to set property '%s' on '%s' after "
275 "it was realized", name, object_get_typename(obj));
276 return;
279 visit_type_int(v, name, &value, &err);
280 if (err) {
281 error_propagate(errp, err);
282 return;
284 if (value < min || value > max) {
285 error_setg(errp, "Property %s.%s doesn't take value %" PRId64
286 " (minimum: %" PRId64 ", maximum: %" PRId64 ")" ,
287 object_get_typename(obj), name, value, min, max);
288 return;
290 cpu->id = value;
293 static void s390_cpu_initfn(Object *obj)
295 CPUState *cs = CPU(obj);
296 S390CPU *cpu = S390_CPU(obj);
297 CPUS390XState *env = &cpu->env;
298 static bool inited;
299 #if !defined(CONFIG_USER_ONLY)
300 struct tm tm;
301 #endif
303 cs->env_ptr = env;
304 cs->halted = 1;
305 cs->exception_index = EXCP_HLT;
306 object_property_add(OBJECT(cpu), "id", "int64_t", s390x_cpu_get_id,
307 s390x_cpu_set_id, NULL, NULL, NULL);
308 #if !defined(CONFIG_USER_ONLY)
309 qemu_get_timedate(&tm, 0);
310 env->tod_offset = TOD_UNIX_EPOCH +
311 (time2tod(mktimegm(&tm)) * 1000000000ULL);
312 env->tod_basetime = 0;
313 env->tod_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, s390x_tod_timer, cpu);
314 env->cpu_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, s390x_cpu_timer, cpu);
315 s390_cpu_set_state(CPU_STATE_STOPPED, cpu);
316 #endif
318 if (tcg_enabled() && !inited) {
319 inited = true;
320 s390x_translate_init();
324 static void s390_cpu_finalize(Object *obj)
326 #if !defined(CONFIG_USER_ONLY)
327 S390CPU *cpu = S390_CPU(obj);
329 qemu_unregister_reset(s390_cpu_machine_reset_cb, cpu);
330 g_free(cpu->irqstate);
331 #endif
334 #if !defined(CONFIG_USER_ONLY)
335 static bool disabled_wait(CPUState *cpu)
337 return cpu->halted && !(S390_CPU(cpu)->env.psw.mask &
338 (PSW_MASK_IO | PSW_MASK_EXT | PSW_MASK_MCHECK));
341 static unsigned s390_count_running_cpus(void)
343 CPUState *cpu;
344 int nr_running = 0;
346 CPU_FOREACH(cpu) {
347 uint8_t state = S390_CPU(cpu)->env.cpu_state;
348 if (state == CPU_STATE_OPERATING ||
349 state == CPU_STATE_LOAD) {
350 if (!disabled_wait(cpu)) {
351 nr_running++;
356 return nr_running;
359 unsigned int s390_cpu_halt(S390CPU *cpu)
361 CPUState *cs = CPU(cpu);
362 trace_cpu_halt(cs->cpu_index);
364 if (!cs->halted) {
365 cs->halted = 1;
366 cs->exception_index = EXCP_HLT;
369 return s390_count_running_cpus();
372 void s390_cpu_unhalt(S390CPU *cpu)
374 CPUState *cs = CPU(cpu);
375 trace_cpu_unhalt(cs->cpu_index);
377 if (cs->halted) {
378 cs->halted = 0;
379 cs->exception_index = -1;
383 unsigned int s390_cpu_set_state(uint8_t cpu_state, S390CPU *cpu)
385 trace_cpu_set_state(CPU(cpu)->cpu_index, cpu_state);
387 switch (cpu_state) {
388 case CPU_STATE_STOPPED:
389 case CPU_STATE_CHECK_STOP:
390 /* halt the cpu for common infrastructure */
391 s390_cpu_halt(cpu);
392 break;
393 case CPU_STATE_OPERATING:
394 case CPU_STATE_LOAD:
395 /* unhalt the cpu for common infrastructure */
396 s390_cpu_unhalt(cpu);
397 break;
398 default:
399 error_report("Requested CPU state is not a valid S390 CPU state: %u",
400 cpu_state);
401 exit(1);
403 if (kvm_enabled() && cpu->env.cpu_state != cpu_state) {
404 kvm_s390_set_cpu_state(cpu, cpu_state);
406 cpu->env.cpu_state = cpu_state;
408 return s390_count_running_cpus();
410 #endif
412 static gchar *s390_gdb_arch_name(CPUState *cs)
414 return g_strdup("s390:64-bit");
417 static void s390_cpu_class_init(ObjectClass *oc, void *data)
419 S390CPUClass *scc = S390_CPU_CLASS(oc);
420 CPUClass *cc = CPU_CLASS(scc);
421 DeviceClass *dc = DEVICE_CLASS(oc);
423 scc->next_cpu_id = 0;
424 scc->parent_realize = dc->realize;
425 dc->realize = s390_cpu_realizefn;
427 scc->parent_reset = cc->reset;
428 #if !defined(CONFIG_USER_ONLY)
429 scc->load_normal = s390_cpu_load_normal;
430 #endif
431 scc->cpu_reset = s390_cpu_reset;
432 scc->initial_cpu_reset = s390_cpu_initial_reset;
433 cc->reset = s390_cpu_full_reset;
434 cc->has_work = s390_cpu_has_work;
435 cc->do_interrupt = s390_cpu_do_interrupt;
436 cc->dump_state = s390_cpu_dump_state;
437 cc->set_pc = s390_cpu_set_pc;
438 cc->gdb_read_register = s390_cpu_gdb_read_register;
439 cc->gdb_write_register = s390_cpu_gdb_write_register;
440 #ifdef CONFIG_USER_ONLY
441 cc->handle_mmu_fault = s390_cpu_handle_mmu_fault;
442 #else
443 cc->get_phys_page_debug = s390_cpu_get_phys_page_debug;
444 cc->vmsd = &vmstate_s390_cpu;
445 cc->write_elf64_note = s390_cpu_write_elf64_note;
446 cc->cpu_exec_interrupt = s390_cpu_exec_interrupt;
447 cc->debug_excp_handler = s390x_cpu_debug_excp_handler;
448 #endif
449 cc->disas_set_info = s390_cpu_disas_set_info;
451 cc->gdb_num_core_regs = S390_NUM_CORE_REGS;
452 cc->gdb_core_xml_file = "s390x-core64.xml";
453 cc->gdb_arch_name = s390_gdb_arch_name;
456 * Reason: s390_cpu_realizefn() calls cpu_exec_init(), which saves
457 * the object in cpus -> dangling pointer after final
458 * object_unref().
460 dc->cannot_destroy_with_object_finalize_yet = true;
463 static const TypeInfo s390_cpu_type_info = {
464 .name = TYPE_S390_CPU,
465 .parent = TYPE_CPU,
466 .instance_size = sizeof(S390CPU),
467 .instance_init = s390_cpu_initfn,
468 .instance_finalize = s390_cpu_finalize,
469 .abstract = false,
470 .class_size = sizeof(S390CPUClass),
471 .class_init = s390_cpu_class_init,
474 static void s390_cpu_register_types(void)
476 type_register_static(&s390_cpu_type_info);
479 type_init(s390_cpu_register_types)