]> Git Repo - qemu.git/blame - target/s390x/cpu.c
maintainers: Add myself as a OpenBSD maintainer
[qemu.git] / target / s390x / cpu.c
CommitLineData
29e4bcb2
AF
1/*
2 * QEMU S/390 CPU
3 *
1ac1a749
AF
4 * Copyright (c) 2009 Ulrich Hecht
5 * Copyright (c) 2011 Alexander Graf
29e4bcb2 6 * Copyright (c) 2012 SUSE LINUX Products GmbH
70bada03 7 * Copyright (c) 2012 IBM Corp.
29e4bcb2
AF
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>
70bada03
JF
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.
29e4bcb2
AF
24 */
25
9615495a 26#include "qemu/osdep.h"
da34e65c 27#include "qapi/error.h"
564b863d 28#include "cpu.h"
4e58b838 29#include "internal.h"
f16bbb9b
DH
30#include "kvm_s390x.h"
31#include "sysemu/kvm.h"
29e4bcb2 32#include "qemu-common.h"
f348b6d1 33#include "qemu/cutils.h"
1de7afc9 34#include "qemu/timer.h"
eb24f7c6 35#include "qemu/error-report.h"
eb24f7c6 36#include "trace.h"
96b1a8bb 37#include "qapi/visitor.h"
63c91552 38#include "exec/exec-all.h"
ca5c1457 39#include "hw/qdev-properties.h"
c7396bbb 40#ifndef CONFIG_USER_ONLY
741da0d3 41#include "hw/hw.h"
904e5fd5 42#include "sysemu/arch_init.h"
96b1a8bb 43#include "sysemu/sysemu.h"
904e5fd5 44#endif
24f91e81 45#include "fpu/softfloat.h"
904e5fd5 46
70bada03
JF
47#define CR0_RESET 0xE0UL
48#define CR14_RESET 0xC2000000UL;
49
f45748f1
AF
50static void s390_cpu_set_pc(CPUState *cs, vaddr value)
51{
52 S390CPU *cpu = S390_CPU(cs);
53
54 cpu->env.psw.addr = value;
55}
56
8c2e1b00
AF
57static bool s390_cpu_has_work(CPUState *cs)
58{
59 S390CPU *cpu = S390_CPU(cs);
8c2e1b00 60
4beab671
DH
61 /* STOPPED cpus can never wake up */
62 if (s390_cpu_get_state(cpu) != CPU_STATE_LOAD &&
63 s390_cpu_get_state(cpu) != CPU_STATE_OPERATING) {
64 return false;
65 }
66
8417f904
DH
67 if (!(cs->interrupt_request & CPU_INTERRUPT_HARD)) {
68 return false;
69 }
70
71 return s390_cpu_has_int(cpu);
8c2e1b00
AF
72}
73
29c6157c
CB
74#if !defined(CONFIG_USER_ONLY)
75/* S390CPUClass::load_normal() */
76static void s390_cpu_load_normal(CPUState *s)
77{
78 S390CPU *cpu = S390_CPU(s);
fdfba1a2 79 cpu->env.psw.addr = ldl_phys(s->as, 4) & PSW_MASK_ESA_ADDR;
29c6157c 80 cpu->env.psw.mask = PSW_MASK_32 | PSW_MASK_64;
eb24f7c6 81 s390_cpu_set_state(CPU_STATE_OPERATING, cpu);
29c6157c
CB
82}
83#endif
84
f5ae2a4f 85/* S390CPUClass::cpu_reset() */
29e4bcb2
AF
86static void s390_cpu_reset(CPUState *s)
87{
88 S390CPU *cpu = S390_CPU(s);
89 S390CPUClass *scc = S390_CPU_GET_CLASS(cpu);
90 CPUS390XState *env = &cpu->env;
91
819bd309 92 env->pfault_token = -1UL;
b073c875 93 env->bpbc = false;
f5ae2a4f 94 scc->parent_reset(s);
18ff9494 95 cpu->env.sigp_order = 0;
eb24f7c6 96 s390_cpu_set_state(CPU_STATE_STOPPED, cpu);
f5ae2a4f
CB
97}
98
99/* S390CPUClass::initial_reset() */
100static void s390_cpu_initial_reset(CPUState *s)
101{
102 S390CPU *cpu = S390_CPU(s);
103 CPUS390XState *env = &cpu->env;
104
105 s390_cpu_reset(s);
cb4f4bc3
CB
106 /* initial reset does not clear everything! */
107 memset(&env->start_initial_reset_fields, 0,
108 offsetof(CPUS390XState, end_reset_fields) -
109 offsetof(CPUS390XState, start_initial_reset_fields));
f5ae2a4f
CB
110
111 /* architectured initial values for CR 0 and 14 */
112 env->cregs[0] = CR0_RESET;
113 env->cregs[14] = CR14_RESET;
819bd309 114
3da0ab35
AJ
115 /* architectured initial value for Breaking-Event-Address register */
116 env->gbea = 1;
117
819bd309 118 env->pfault_token = -1UL;
49f5c9e9 119
4a33565f
AJ
120 /* tininess for underflow is detected before rounding */
121 set_float_detect_tininess(float_tininess_before_rounding,
122 &env->fpu_status);
123
49f5c9e9
TH
124 /* Reset state inside the kernel that we cannot access yet from QEMU. */
125 if (kvm_enabled()) {
99607144 126 kvm_s390_reset_vcpu(cpu);
49f5c9e9 127 }
f5ae2a4f
CB
128}
129
130/* CPUClass:reset() */
131static void s390_cpu_full_reset(CPUState *s)
132{
133 S390CPU *cpu = S390_CPU(s);
134 S390CPUClass *scc = S390_CPU_GET_CLASS(cpu);
135 CPUS390XState *env = &cpu->env;
136
29e4bcb2 137 scc->parent_reset(s);
18ff9494 138 cpu->env.sigp_order = 0;
eb24f7c6 139 s390_cpu_set_state(CPU_STATE_STOPPED, cpu);
29e4bcb2 140
1f5c00cf 141 memset(env, 0, offsetof(CPUS390XState, end_reset_fields));
70bada03
JF
142
143 /* architectured initial values for CR 0 and 14 */
144 env->cregs[0] = CR0_RESET;
145 env->cregs[14] = CR14_RESET;
819bd309 146
3da0ab35
AJ
147 /* architectured initial value for Breaking-Event-Address register */
148 env->gbea = 1;
149
819bd309
DD
150 env->pfault_token = -1UL;
151
4a33565f
AJ
152 /* tininess for underflow is detected before rounding */
153 set_float_detect_tininess(float_tininess_before_rounding,
154 &env->fpu_status);
155
99607144 156 /* Reset state inside the kernel that we cannot access yet from QEMU. */
50a2c6e5
PB
157 if (kvm_enabled()) {
158 kvm_s390_reset_vcpu(cpu);
159 }
29e4bcb2
AF
160}
161
70bada03
JF
162#if !defined(CONFIG_USER_ONLY)
163static void s390_cpu_machine_reset_cb(void *opaque)
164{
165 S390CPU *cpu = opaque;
166
14e6fe12 167 run_on_cpu(CPU(cpu), s390_do_cpu_full_reset, RUN_ON_CPU_NULL);
70bada03
JF
168}
169#endif
170
dbad6b74
PC
171static void s390_cpu_disas_set_info(CPUState *cpu, disassemble_info *info)
172{
173 info->mach = bfd_mach_s390_64;
174 info->print_insn = print_insn_s390;
175}
176
1f136632
AF
177static void s390_cpu_realizefn(DeviceState *dev, Error **errp)
178{
14a10fc3 179 CPUState *cs = CPU(dev);
1f136632 180 S390CPUClass *scc = S390_CPU_GET_CLASS(dev);
1e70ba24 181#if !defined(CONFIG_USER_ONLY)
c6644fc8 182 S390CPU *cpu = S390_CPU(dev);
1e70ba24 183#endif
c6644fc8
MR
184 Error *err = NULL;
185
41868f84
DH
186 /* the model has to be realized before qemu_init_vcpu() due to kvm */
187 s390_realize_cpu_model(cs, &err);
188 if (err) {
189 goto out;
190 }
191
96b1a8bb 192#if !defined(CONFIG_USER_ONLY)
ca5c1457
DH
193 if (cpu->env.core_id >= max_cpus) {
194 error_setg(&err, "Unable to add CPU with core-id: %" PRIu32
195 ", maximum core-id: %d", cpu->env.core_id,
196 max_cpus - 1);
96b1a8bb
MR
197 goto out;
198 }
88556edd 199
ca5c1457
DH
200 if (cpu_exists(cpu->env.core_id)) {
201 error_setg(&err, "Unable to add CPU with core-id: %" PRIu32
202 ", it already exists", cpu->env.core_id);
96b1a8bb
MR
203 goto out;
204 }
96b1a8bb 205
ca5c1457 206 /* sync cs->cpu_index and env->core_id. The latter is needed for TCG. */
1e70ba24
DH
207 cs->cpu_index = cpu->env.core_id;
208#endif
209
ce5b1bbf 210 cpu_exec_realizefn(cs, &err);
c6644fc8 211 if (err != NULL) {
96b1a8bb 212 goto out;
c6644fc8 213 }
1f136632 214
c6644fc8
MR
215#if !defined(CONFIG_USER_ONLY)
216 qemu_register_reset(s390_cpu_machine_reset_cb, cpu);
217#endif
73d510c9 218 s390_cpu_gdb_init(cs);
14a10fc3 219 qemu_init_vcpu(cs);
159855f0 220#if !defined(CONFIG_USER_ONLY)
14e6fe12 221 run_on_cpu(cs, s390_do_cpu_full_reset, RUN_ON_CPU_NULL);
159855f0 222#else
14a10fc3 223 cpu_reset(cs);
159855f0 224#endif
1f136632 225
96b1a8bb 226 scc->parent_realize(dev, &err);
96b1a8bb
MR
227out:
228 error_propagate(errp, err);
229}
230
8f22e0df
AF
231static void s390_cpu_initfn(Object *obj)
232{
c05efcb1 233 CPUState *cs = CPU(obj);
8f22e0df
AF
234 S390CPU *cpu = S390_CPU(obj);
235 CPUS390XState *env = &cpu->env;
8f22e0df
AF
236#if !defined(CONFIG_USER_ONLY)
237 struct tm tm;
238#endif
239
c05efcb1 240 cs->env_ptr = env;
ef3027af
MR
241 cs->halted = 1;
242 cs->exception_index = EXCP_HLT;
0754f604 243 s390_cpu_model_register_props(obj);
8f22e0df
AF
244#if !defined(CONFIG_USER_ONLY)
245 qemu_get_timedate(&tm, 0);
246 env->tod_offset = TOD_UNIX_EPOCH +
247 (time2tod(mktimegm(&tm)) * 1000000000ULL);
248 env->tod_basetime = 0;
bc72ad67
AB
249 env->tod_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, s390x_tod_timer, cpu);
250 env->cpu_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, s390x_cpu_timer, cpu);
eb24f7c6 251 s390_cpu_set_state(CPU_STATE_STOPPED, cpu);
8f22e0df 252#endif
8f22e0df
AF
253}
254
d5627ce8
AF
255static void s390_cpu_finalize(Object *obj)
256{
257#if !defined(CONFIG_USER_ONLY)
258 S390CPU *cpu = S390_CPU(obj);
259
260 qemu_unregister_reset(s390_cpu_machine_reset_cb, cpu);
3cda44f7 261 g_free(cpu->irqstate);
d5627ce8
AF
262#endif
263}
264
75973bfe 265#if !defined(CONFIG_USER_ONLY)
eb24f7c6
DH
266static bool disabled_wait(CPUState *cpu)
267{
268 return cpu->halted && !(S390_CPU(cpu)->env.psw.mask &
269 (PSW_MASK_IO | PSW_MASK_EXT | PSW_MASK_MCHECK));
270}
271
75973bfe
DH
272static unsigned s390_count_running_cpus(void)
273{
274 CPUState *cpu;
275 int nr_running = 0;
276
277 CPU_FOREACH(cpu) {
278 uint8_t state = S390_CPU(cpu)->env.cpu_state;
279 if (state == CPU_STATE_OPERATING ||
280 state == CPU_STATE_LOAD) {
eb24f7c6
DH
281 if (!disabled_wait(cpu)) {
282 nr_running++;
283 }
75973bfe
DH
284 }
285 }
286
287 return nr_running;
288}
289
eb24f7c6 290unsigned int s390_cpu_halt(S390CPU *cpu)
75973bfe
DH
291{
292 CPUState *cs = CPU(cpu);
eb24f7c6 293 trace_cpu_halt(cs->cpu_index);
75973bfe 294
eb24f7c6
DH
295 if (!cs->halted) {
296 cs->halted = 1;
297 cs->exception_index = EXCP_HLT;
75973bfe 298 }
eb24f7c6
DH
299
300 return s390_count_running_cpus();
75973bfe
DH
301}
302
eb24f7c6 303void s390_cpu_unhalt(S390CPU *cpu)
75973bfe
DH
304{
305 CPUState *cs = CPU(cpu);
eb24f7c6 306 trace_cpu_unhalt(cs->cpu_index);
75973bfe 307
eb24f7c6
DH
308 if (cs->halted) {
309 cs->halted = 0;
310 cs->exception_index = -1;
311 }
312}
313
314unsigned int s390_cpu_set_state(uint8_t cpu_state, S390CPU *cpu)
315 {
316 trace_cpu_set_state(CPU(cpu)->cpu_index, cpu_state);
317
318 switch (cpu_state) {
319 case CPU_STATE_STOPPED:
320 case CPU_STATE_CHECK_STOP:
321 /* halt the cpu for common infrastructure */
322 s390_cpu_halt(cpu);
323 break;
324 case CPU_STATE_OPERATING:
325 case CPU_STATE_LOAD:
741a4ec1
DH
326 /*
327 * Starting a CPU with a PSW WAIT bit set:
328 * KVM: handles this internally and triggers another WAIT exit.
329 * TCG: will actually try to continue to run. Don't unhalt, will
330 * be done when the CPU actually has work (an interrupt).
331 */
332 if (!tcg_enabled() || !(cpu->env.psw.mask & PSW_MASK_WAIT)) {
333 s390_cpu_unhalt(cpu);
334 }
eb24f7c6
DH
335 break;
336 default:
337 error_report("Requested CPU state is not a valid S390 CPU state: %u",
338 cpu_state);
339 exit(1);
75973bfe 340 }
c9e659c9
DH
341 if (kvm_enabled() && cpu->env.cpu_state != cpu_state) {
342 kvm_s390_set_cpu_state(cpu, cpu_state);
343 }
eb24f7c6 344 cpu->env.cpu_state = cpu_state;
75973bfe
DH
345
346 return s390_count_running_cpus();
347}
b6089b05
DH
348
349int s390_get_clock(uint8_t *tod_high, uint64_t *tod_low)
350{
7edd4a49
CW
351 int r = 0;
352
b6089b05 353 if (kvm_enabled()) {
7edd4a49
CW
354 r = kvm_s390_get_clock_ext(tod_high, tod_low);
355 if (r == -ENXIO) {
356 return kvm_s390_get_clock(tod_high, tod_low);
357 }
358 } else {
359 /* Fixme TCG */
360 *tod_high = 0;
361 *tod_low = 0;
b6089b05 362 }
7edd4a49
CW
363
364 return r;
b6089b05
DH
365}
366
367int s390_set_clock(uint8_t *tod_high, uint64_t *tod_low)
368{
7edd4a49
CW
369 int r = 0;
370
b6089b05 371 if (kvm_enabled()) {
7edd4a49
CW
372 r = kvm_s390_set_clock_ext(tod_high, tod_low);
373 if (r == -ENXIO) {
374 return kvm_s390_set_clock(tod_high, tod_low);
375 }
b6089b05
DH
376 }
377 /* Fixme TCG */
7edd4a49 378 return r;
b6089b05
DH
379}
380
381int s390_set_memory_limit(uint64_t new_limit, uint64_t *hw_limit)
382{
383 if (kvm_enabled()) {
384 return kvm_s390_set_mem_limit(new_limit, hw_limit);
385 }
386 return 0;
387}
388
389void s390_cmma_reset(void)
390{
391 if (kvm_enabled()) {
392 kvm_s390_cmma_reset();
393 }
394}
395
b6089b05
DH
396int s390_get_memslot_count(void)
397{
398 if (kvm_enabled()) {
399 return kvm_s390_get_memslot_count();
400 } else {
401 return MAX_AVAIL_SLOTS;
402 }
403}
404
405int s390_assign_subch_ioeventfd(EventNotifier *notifier, uint32_t sch_id,
406 int vq, bool assign)
407{
408 if (kvm_enabled()) {
409 return kvm_s390_assign_subch_ioeventfd(notifier, sch_id, vq, assign);
410 } else {
411 return 0;
412 }
413}
414
415void s390_crypto_reset(void)
416{
417 if (kvm_enabled()) {
418 kvm_s390_crypto_reset();
419 }
420}
421
422bool s390_get_squash_mcss(void)
423{
424 if (object_property_get_bool(OBJECT(qdev_get_machine()), "s390-squash-mcss",
425 NULL)) {
426 return true;
427 }
428
429 return false;
430}
5e7164c5
DH
431
432void s390_enable_css_support(S390CPU *cpu)
433{
434 if (kvm_enabled()) {
435 kvm_s390_enable_css_support(cpu);
436 }
437}
75973bfe
DH
438#endif
439
b3820e6c
DH
440static gchar *s390_gdb_arch_name(CPUState *cs)
441{
442 return g_strdup("s390:64-bit");
443}
444
ca5c1457 445static Property s390x_cpu_properties[] = {
1e70ba24 446#if !defined(CONFIG_USER_ONLY)
ca5c1457 447 DEFINE_PROP_UINT32("core-id", S390CPU, env.core_id, 0),
1e70ba24 448#endif
ca5c1457
DH
449 DEFINE_PROP_END_OF_LIST()
450};
451
29e4bcb2
AF
452static void s390_cpu_class_init(ObjectClass *oc, void *data)
453{
454 S390CPUClass *scc = S390_CPU_CLASS(oc);
455 CPUClass *cc = CPU_CLASS(scc);
c7396bbb 456 DeviceClass *dc = DEVICE_CLASS(oc);
29e4bcb2 457
bf853881
PMD
458 device_class_set_parent_realize(dc, s390_cpu_realizefn,
459 &scc->parent_realize);
ca5c1457 460 dc->props = s390x_cpu_properties;
0347ab84 461 dc->user_creatable = true;
1f136632 462
29e4bcb2 463 scc->parent_reset = cc->reset;
29c6157c
CB
464#if !defined(CONFIG_USER_ONLY)
465 scc->load_normal = s390_cpu_load_normal;
466#endif
f5ae2a4f
CB
467 scc->cpu_reset = s390_cpu_reset;
468 scc->initial_cpu_reset = s390_cpu_initial_reset;
469 cc->reset = s390_cpu_full_reset;
41868f84 470 cc->class_by_name = s390_cpu_class_by_name,
8c2e1b00 471 cc->has_work = s390_cpu_has_work;
b114588c 472#ifdef CONFIG_TCG
97a8ea5a 473 cc->do_interrupt = s390_cpu_do_interrupt;
b114588c 474#endif
878096ee 475 cc->dump_state = s390_cpu_dump_state;
f45748f1 476 cc->set_pc = s390_cpu_set_pc;
5b50e790
AF
477 cc->gdb_read_register = s390_cpu_gdb_read_register;
478 cc->gdb_write_register = s390_cpu_gdb_write_register;
7510454e
AF
479#ifdef CONFIG_USER_ONLY
480 cc->handle_mmu_fault = s390_cpu_handle_mmu_fault;
481#else
00b941e5 482 cc->get_phys_page_debug = s390_cpu_get_phys_page_debug;
ef1df130 483 cc->vmsd = &vmstate_s390_cpu;
9b4f38e1 484 cc->write_elf64_note = s390_cpu_write_elf64_note;
b114588c 485#ifdef CONFIG_TCG
02bb9bbf 486 cc->cpu_exec_interrupt = s390_cpu_exec_interrupt;
311918b9 487 cc->debug_excp_handler = s390x_cpu_debug_excp_handler;
44977a8f 488 cc->do_unaligned_access = s390x_cpu_do_unaligned_access;
b114588c 489#endif
00b941e5 490#endif
dbad6b74 491 cc->disas_set_info = s390_cpu_disas_set_info;
74d7fc7f 492#ifdef CONFIG_TCG
55c3ceef 493 cc->tcg_initialize = s390x_translate_init;
74d7fc7f 494#endif
dbad6b74 495
73d510c9
DH
496 cc->gdb_num_core_regs = S390_NUM_CORE_REGS;
497 cc->gdb_core_xml_file = "s390x-core64.xml";
b3820e6c 498 cc->gdb_arch_name = s390_gdb_arch_name;
4c315c27 499
6efadc90 500 s390_cpu_model_class_register_props(oc);
29e4bcb2
AF
501}
502
503static const TypeInfo s390_cpu_type_info = {
504 .name = TYPE_S390_CPU,
505 .parent = TYPE_CPU,
506 .instance_size = sizeof(S390CPU),
8f22e0df 507 .instance_init = s390_cpu_initfn,
d5627ce8 508 .instance_finalize = s390_cpu_finalize,
41868f84 509 .abstract = true,
29e4bcb2
AF
510 .class_size = sizeof(S390CPUClass),
511 .class_init = s390_cpu_class_init,
512};
513
514static void s390_cpu_register_types(void)
515{
516 type_register_static(&s390_cpu_type_info);
517}
518
519type_init(s390_cpu_register_types)
This page took 0.432693 seconds and 4 git commands to generate.