]> git.proxmox.com Git - mirror_qemu.git/blame - target/s390x/cpu.c
s390x/css: fix css migration compat handling
[mirror_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
VM
44#endif
45
70bada03
JF
46#define CR0_RESET 0xE0UL
47#define CR14_RESET 0xC2000000UL;
48
f45748f1
AF
49static void s390_cpu_set_pc(CPUState *cs, vaddr value)
50{
51 S390CPU *cpu = S390_CPU(cs);
52
53 cpu->env.psw.addr = value;
54}
55
8c2e1b00
AF
56static bool s390_cpu_has_work(CPUState *cs)
57{
58 S390CPU *cpu = S390_CPU(cs);
59 CPUS390XState *env = &cpu->env;
60
61 return (cs->interrupt_request & CPU_INTERRUPT_HARD) &&
62 (env->psw.mask & PSW_MASK_EXT);
63}
64
29c6157c
CB
65#if !defined(CONFIG_USER_ONLY)
66/* S390CPUClass::load_normal() */
67static void s390_cpu_load_normal(CPUState *s)
68{
69 S390CPU *cpu = S390_CPU(s);
fdfba1a2 70 cpu->env.psw.addr = ldl_phys(s->as, 4) & PSW_MASK_ESA_ADDR;
29c6157c 71 cpu->env.psw.mask = PSW_MASK_32 | PSW_MASK_64;
eb24f7c6 72 s390_cpu_set_state(CPU_STATE_OPERATING, cpu);
29c6157c
CB
73}
74#endif
75
f5ae2a4f 76/* S390CPUClass::cpu_reset() */
29e4bcb2
AF
77static void s390_cpu_reset(CPUState *s)
78{
79 S390CPU *cpu = S390_CPU(s);
80 S390CPUClass *scc = S390_CPU_GET_CLASS(cpu);
81 CPUS390XState *env = &cpu->env;
82
819bd309 83 env->pfault_token = -1UL;
f5ae2a4f 84 scc->parent_reset(s);
18ff9494 85 cpu->env.sigp_order = 0;
eb24f7c6 86 s390_cpu_set_state(CPU_STATE_STOPPED, cpu);
f5ae2a4f
CB
87}
88
89/* S390CPUClass::initial_reset() */
90static void s390_cpu_initial_reset(CPUState *s)
91{
92 S390CPU *cpu = S390_CPU(s);
93 CPUS390XState *env = &cpu->env;
cc0d079d 94 int i;
f5ae2a4f
CB
95
96 s390_cpu_reset(s);
cb4f4bc3
CB
97 /* initial reset does not clear everything! */
98 memset(&env->start_initial_reset_fields, 0,
99 offsetof(CPUS390XState, end_reset_fields) -
100 offsetof(CPUS390XState, start_initial_reset_fields));
f5ae2a4f
CB
101
102 /* architectured initial values for CR 0 and 14 */
103 env->cregs[0] = CR0_RESET;
104 env->cregs[14] = CR14_RESET;
819bd309 105
3da0ab35
AJ
106 /* architectured initial value for Breaking-Event-Address register */
107 env->gbea = 1;
108
819bd309 109 env->pfault_token = -1UL;
7107e5a7 110 env->ext_index = -1;
cc0d079d
AJ
111 for (i = 0; i < ARRAY_SIZE(env->io_index); i++) {
112 env->io_index[i] = -1;
113 }
49f5c9e9 114
4a33565f
AJ
115 /* tininess for underflow is detected before rounding */
116 set_float_detect_tininess(float_tininess_before_rounding,
117 &env->fpu_status);
118
49f5c9e9
TH
119 /* Reset state inside the kernel that we cannot access yet from QEMU. */
120 if (kvm_enabled()) {
99607144 121 kvm_s390_reset_vcpu(cpu);
49f5c9e9 122 }
f5ae2a4f
CB
123}
124
125/* CPUClass:reset() */
126static void s390_cpu_full_reset(CPUState *s)
127{
128 S390CPU *cpu = S390_CPU(s);
129 S390CPUClass *scc = S390_CPU_GET_CLASS(cpu);
130 CPUS390XState *env = &cpu->env;
cc0d079d 131 int i;
f5ae2a4f 132
29e4bcb2 133 scc->parent_reset(s);
18ff9494 134 cpu->env.sigp_order = 0;
eb24f7c6 135 s390_cpu_set_state(CPU_STATE_STOPPED, cpu);
29e4bcb2 136
1f5c00cf 137 memset(env, 0, offsetof(CPUS390XState, end_reset_fields));
70bada03
JF
138
139 /* architectured initial values for CR 0 and 14 */
140 env->cregs[0] = CR0_RESET;
141 env->cregs[14] = CR14_RESET;
819bd309 142
3da0ab35
AJ
143 /* architectured initial value for Breaking-Event-Address register */
144 env->gbea = 1;
145
819bd309 146 env->pfault_token = -1UL;
7107e5a7 147 env->ext_index = -1;
cc0d079d
AJ
148 for (i = 0; i < ARRAY_SIZE(env->io_index); i++) {
149 env->io_index[i] = -1;
150 }
819bd309 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;
2b7ac767 236 static bool inited;
8f22e0df
AF
237#if !defined(CONFIG_USER_ONLY)
238 struct tm tm;
239#endif
240
c05efcb1 241 cs->env_ptr = env;
ef3027af
MR
242 cs->halted = 1;
243 cs->exception_index = EXCP_HLT;
0754f604 244 s390_cpu_model_register_props(obj);
8f22e0df
AF
245#if !defined(CONFIG_USER_ONLY)
246 qemu_get_timedate(&tm, 0);
247 env->tod_offset = TOD_UNIX_EPOCH +
248 (time2tod(mktimegm(&tm)) * 1000000000ULL);
249 env->tod_basetime = 0;
bc72ad67
AB
250 env->tod_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, s390x_tod_timer, cpu);
251 env->cpu_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, s390x_cpu_timer, cpu);
eb24f7c6 252 s390_cpu_set_state(CPU_STATE_STOPPED, cpu);
8f22e0df 253#endif
2b7ac767
AF
254
255 if (tcg_enabled() && !inited) {
256 inited = true;
257 s390x_translate_init();
258 }
8f22e0df
AF
259}
260
d5627ce8
AF
261static void s390_cpu_finalize(Object *obj)
262{
263#if !defined(CONFIG_USER_ONLY)
264 S390CPU *cpu = S390_CPU(obj);
265
266 qemu_unregister_reset(s390_cpu_machine_reset_cb, cpu);
3cda44f7 267 g_free(cpu->irqstate);
d5627ce8
AF
268#endif
269}
270
75973bfe 271#if !defined(CONFIG_USER_ONLY)
eb24f7c6
DH
272static bool disabled_wait(CPUState *cpu)
273{
274 return cpu->halted && !(S390_CPU(cpu)->env.psw.mask &
275 (PSW_MASK_IO | PSW_MASK_EXT | PSW_MASK_MCHECK));
276}
277
75973bfe
DH
278static unsigned s390_count_running_cpus(void)
279{
280 CPUState *cpu;
281 int nr_running = 0;
282
283 CPU_FOREACH(cpu) {
284 uint8_t state = S390_CPU(cpu)->env.cpu_state;
285 if (state == CPU_STATE_OPERATING ||
286 state == CPU_STATE_LOAD) {
eb24f7c6
DH
287 if (!disabled_wait(cpu)) {
288 nr_running++;
289 }
75973bfe
DH
290 }
291 }
292
293 return nr_running;
294}
295
eb24f7c6 296unsigned int s390_cpu_halt(S390CPU *cpu)
75973bfe
DH
297{
298 CPUState *cs = CPU(cpu);
eb24f7c6 299 trace_cpu_halt(cs->cpu_index);
75973bfe 300
eb24f7c6
DH
301 if (!cs->halted) {
302 cs->halted = 1;
303 cs->exception_index = EXCP_HLT;
75973bfe 304 }
eb24f7c6
DH
305
306 return s390_count_running_cpus();
75973bfe
DH
307}
308
eb24f7c6 309void s390_cpu_unhalt(S390CPU *cpu)
75973bfe
DH
310{
311 CPUState *cs = CPU(cpu);
eb24f7c6 312 trace_cpu_unhalt(cs->cpu_index);
75973bfe 313
eb24f7c6
DH
314 if (cs->halted) {
315 cs->halted = 0;
316 cs->exception_index = -1;
317 }
318}
319
320unsigned int s390_cpu_set_state(uint8_t cpu_state, S390CPU *cpu)
321 {
322 trace_cpu_set_state(CPU(cpu)->cpu_index, cpu_state);
323
324 switch (cpu_state) {
325 case CPU_STATE_STOPPED:
326 case CPU_STATE_CHECK_STOP:
327 /* halt the cpu for common infrastructure */
328 s390_cpu_halt(cpu);
329 break;
330 case CPU_STATE_OPERATING:
331 case CPU_STATE_LOAD:
332 /* unhalt the cpu for common infrastructure */
333 s390_cpu_unhalt(cpu);
334 break;
335 default:
336 error_report("Requested CPU state is not a valid S390 CPU state: %u",
337 cpu_state);
338 exit(1);
75973bfe 339 }
c9e659c9
DH
340 if (kvm_enabled() && cpu->env.cpu_state != cpu_state) {
341 kvm_s390_set_cpu_state(cpu, cpu_state);
342 }
eb24f7c6 343 cpu->env.cpu_state = cpu_state;
75973bfe
DH
344
345 return s390_count_running_cpus();
346}
b6089b05
DH
347
348int s390_get_clock(uint8_t *tod_high, uint64_t *tod_low)
349{
350 if (kvm_enabled()) {
351 return kvm_s390_get_clock(tod_high, tod_low);
352 }
353 /* Fixme TCG */
354 *tod_high = 0;
355 *tod_low = 0;
356 return 0;
357}
358
359int s390_set_clock(uint8_t *tod_high, uint64_t *tod_low)
360{
361 if (kvm_enabled()) {
362 return kvm_s390_set_clock(tod_high, tod_low);
363 }
364 /* Fixme TCG */
365 return 0;
366}
367
368int s390_set_memory_limit(uint64_t new_limit, uint64_t *hw_limit)
369{
370 if (kvm_enabled()) {
371 return kvm_s390_set_mem_limit(new_limit, hw_limit);
372 }
373 return 0;
374}
375
376void s390_cmma_reset(void)
377{
378 if (kvm_enabled()) {
379 kvm_s390_cmma_reset();
380 }
381}
382
383int s390_cpu_restart(S390CPU *cpu)
384{
385 if (kvm_enabled()) {
386 return kvm_s390_cpu_restart(cpu);
387 }
388 return -ENOSYS;
389}
390
391int s390_get_memslot_count(void)
392{
393 if (kvm_enabled()) {
394 return kvm_s390_get_memslot_count();
395 } else {
396 return MAX_AVAIL_SLOTS;
397 }
398}
399
400int s390_assign_subch_ioeventfd(EventNotifier *notifier, uint32_t sch_id,
401 int vq, bool assign)
402{
403 if (kvm_enabled()) {
404 return kvm_s390_assign_subch_ioeventfd(notifier, sch_id, vq, assign);
405 } else {
406 return 0;
407 }
408}
409
410void s390_crypto_reset(void)
411{
412 if (kvm_enabled()) {
413 kvm_s390_crypto_reset();
414 }
415}
416
417bool s390_get_squash_mcss(void)
418{
419 if (object_property_get_bool(OBJECT(qdev_get_machine()), "s390-squash-mcss",
420 NULL)) {
421 return true;
422 }
423
424 return false;
425}
5e7164c5
DH
426
427void s390_enable_css_support(S390CPU *cpu)
428{
429 if (kvm_enabled()) {
430 kvm_s390_enable_css_support(cpu);
431 }
432}
75973bfe
DH
433#endif
434
b3820e6c
DH
435static gchar *s390_gdb_arch_name(CPUState *cs)
436{
437 return g_strdup("s390:64-bit");
438}
439
ca5c1457 440static Property s390x_cpu_properties[] = {
1e70ba24 441#if !defined(CONFIG_USER_ONLY)
ca5c1457 442 DEFINE_PROP_UINT32("core-id", S390CPU, env.core_id, 0),
1e70ba24 443#endif
ca5c1457
DH
444 DEFINE_PROP_END_OF_LIST()
445};
446
29e4bcb2
AF
447static void s390_cpu_class_init(ObjectClass *oc, void *data)
448{
449 S390CPUClass *scc = S390_CPU_CLASS(oc);
450 CPUClass *cc = CPU_CLASS(scc);
c7396bbb 451 DeviceClass *dc = DEVICE_CLASS(oc);
29e4bcb2 452
1f136632
AF
453 scc->parent_realize = dc->realize;
454 dc->realize = s390_cpu_realizefn;
ca5c1457 455 dc->props = s390x_cpu_properties;
0347ab84 456 dc->user_creatable = true;
1f136632 457
29e4bcb2 458 scc->parent_reset = cc->reset;
29c6157c
CB
459#if !defined(CONFIG_USER_ONLY)
460 scc->load_normal = s390_cpu_load_normal;
461#endif
f5ae2a4f
CB
462 scc->cpu_reset = s390_cpu_reset;
463 scc->initial_cpu_reset = s390_cpu_initial_reset;
464 cc->reset = s390_cpu_full_reset;
41868f84 465 cc->class_by_name = s390_cpu_class_by_name,
8c2e1b00 466 cc->has_work = s390_cpu_has_work;
b114588c 467#ifdef CONFIG_TCG
97a8ea5a 468 cc->do_interrupt = s390_cpu_do_interrupt;
b114588c 469#endif
878096ee 470 cc->dump_state = s390_cpu_dump_state;
f45748f1 471 cc->set_pc = s390_cpu_set_pc;
5b50e790
AF
472 cc->gdb_read_register = s390_cpu_gdb_read_register;
473 cc->gdb_write_register = s390_cpu_gdb_write_register;
7510454e
AF
474#ifdef CONFIG_USER_ONLY
475 cc->handle_mmu_fault = s390_cpu_handle_mmu_fault;
476#else
00b941e5 477 cc->get_phys_page_debug = s390_cpu_get_phys_page_debug;
ef1df130 478 cc->vmsd = &vmstate_s390_cpu;
9b4f38e1 479 cc->write_elf64_note = s390_cpu_write_elf64_note;
b114588c 480#ifdef CONFIG_TCG
02bb9bbf 481 cc->cpu_exec_interrupt = s390_cpu_exec_interrupt;
311918b9 482 cc->debug_excp_handler = s390x_cpu_debug_excp_handler;
44977a8f 483 cc->do_unaligned_access = s390x_cpu_do_unaligned_access;
b114588c 484#endif
00b941e5 485#endif
dbad6b74
PC
486 cc->disas_set_info = s390_cpu_disas_set_info;
487
73d510c9
DH
488 cc->gdb_num_core_regs = S390_NUM_CORE_REGS;
489 cc->gdb_core_xml_file = "s390x-core64.xml";
b3820e6c 490 cc->gdb_arch_name = s390_gdb_arch_name;
4c315c27 491
6efadc90 492 s390_cpu_model_class_register_props(oc);
29e4bcb2
AF
493}
494
495static const TypeInfo s390_cpu_type_info = {
496 .name = TYPE_S390_CPU,
497 .parent = TYPE_CPU,
498 .instance_size = sizeof(S390CPU),
8f22e0df 499 .instance_init = s390_cpu_initfn,
d5627ce8 500 .instance_finalize = s390_cpu_finalize,
41868f84 501 .abstract = true,
29e4bcb2
AF
502 .class_size = sizeof(S390CPUClass),
503 .class_init = s390_cpu_class_init,
504};
505
506static void s390_cpu_register_types(void)
507{
508 type_register_static(&s390_cpu_type_info);
509}
510
511type_init(s390_cpu_register_types)