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