xref: /openbmc/qemu/target/s390x/cpu.c (revision 042cea27)
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 "internal.h"
30 #include "kvm_s390x.h"
31 #include "sysemu/kvm.h"
32 #include "qemu-common.h"
33 #include "qemu/cutils.h"
34 #include "qemu/timer.h"
35 #include "qemu/error-report.h"
36 #include "trace.h"
37 #include "qapi/visitor.h"
38 #include "exec/exec-all.h"
39 #include "hw/qdev-properties.h"
40 #ifndef CONFIG_USER_ONLY
41 #include "hw/hw.h"
42 #include "sysemu/arch_init.h"
43 #include "sysemu/sysemu.h"
44 #endif
45 #include "fpu/softfloat.h"
46 
47 #define CR0_RESET       0xE0UL
48 #define CR14_RESET      0xC2000000UL;
49 
50 static 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 
57 static bool s390_cpu_has_work(CPUState *cs)
58 {
59     S390CPU *cpu = S390_CPU(cs);
60 
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 
67     if (!(cs->interrupt_request & CPU_INTERRUPT_HARD)) {
68         return false;
69     }
70 
71     return s390_cpu_has_int(cpu);
72 }
73 
74 #if !defined(CONFIG_USER_ONLY)
75 /* S390CPUClass::load_normal() */
76 static void s390_cpu_load_normal(CPUState *s)
77 {
78     S390CPU *cpu = S390_CPU(s);
79     cpu->env.psw.addr = ldl_phys(s->as, 4) & PSW_MASK_ESA_ADDR;
80     cpu->env.psw.mask = PSW_MASK_32 | PSW_MASK_64;
81     s390_cpu_set_state(CPU_STATE_OPERATING, cpu);
82 }
83 #endif
84 
85 /* S390CPUClass::cpu_reset() */
86 static 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 
92     env->pfault_token = -1UL;
93     env->bpbc = false;
94     scc->parent_reset(s);
95     cpu->env.sigp_order = 0;
96     s390_cpu_set_state(CPU_STATE_STOPPED, cpu);
97 }
98 
99 /* S390CPUClass::initial_reset() */
100 static 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);
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));
110 
111     /* architectured initial values for CR 0 and 14 */
112     env->cregs[0] = CR0_RESET;
113     env->cregs[14] = CR14_RESET;
114 
115     /* architectured initial value for Breaking-Event-Address register */
116     env->gbea = 1;
117 
118     env->pfault_token = -1UL;
119 
120     /* tininess for underflow is detected before rounding */
121     set_float_detect_tininess(float_tininess_before_rounding,
122                               &env->fpu_status);
123 
124     /* Reset state inside the kernel that we cannot access yet from QEMU. */
125     if (kvm_enabled()) {
126         kvm_s390_reset_vcpu(cpu);
127     }
128 }
129 
130 /* CPUClass:reset() */
131 static 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 
137     scc->parent_reset(s);
138     cpu->env.sigp_order = 0;
139     s390_cpu_set_state(CPU_STATE_STOPPED, cpu);
140 
141     memset(env, 0, offsetof(CPUS390XState, end_reset_fields));
142 
143     /* architectured initial values for CR 0 and 14 */
144     env->cregs[0] = CR0_RESET;
145     env->cregs[14] = CR14_RESET;
146 
147     /* architectured initial value for Breaking-Event-Address register */
148     env->gbea = 1;
149 
150     env->pfault_token = -1UL;
151 
152     /* tininess for underflow is detected before rounding */
153     set_float_detect_tininess(float_tininess_before_rounding,
154                               &env->fpu_status);
155 
156     /* Reset state inside the kernel that we cannot access yet from QEMU. */
157     if (kvm_enabled()) {
158         kvm_s390_reset_vcpu(cpu);
159     }
160 }
161 
162 #if !defined(CONFIG_USER_ONLY)
163 static void s390_cpu_machine_reset_cb(void *opaque)
164 {
165     S390CPU *cpu = opaque;
166 
167     run_on_cpu(CPU(cpu), s390_do_cpu_full_reset, RUN_ON_CPU_NULL);
168 }
169 #endif
170 
171 static 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 
177 static void s390_cpu_realizefn(DeviceState *dev, Error **errp)
178 {
179     CPUState *cs = CPU(dev);
180     S390CPUClass *scc = S390_CPU_GET_CLASS(dev);
181 #if !defined(CONFIG_USER_ONLY)
182     S390CPU *cpu = S390_CPU(dev);
183 #endif
184     Error *err = NULL;
185 
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 
192 #if !defined(CONFIG_USER_ONLY)
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);
197         goto out;
198     }
199 
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);
203         goto out;
204     }
205 
206     /* sync cs->cpu_index and env->core_id. The latter is needed for TCG. */
207     cs->cpu_index = cpu->env.core_id;
208 #endif
209 
210     cpu_exec_realizefn(cs, &err);
211     if (err != NULL) {
212         goto out;
213     }
214 
215 #if !defined(CONFIG_USER_ONLY)
216     qemu_register_reset(s390_cpu_machine_reset_cb, cpu);
217 #endif
218     s390_cpu_gdb_init(cs);
219     qemu_init_vcpu(cs);
220 #if !defined(CONFIG_USER_ONLY)
221     run_on_cpu(cs, s390_do_cpu_full_reset, RUN_ON_CPU_NULL);
222 #else
223     cpu_reset(cs);
224 #endif
225 
226     scc->parent_realize(dev, &err);
227 out:
228     error_propagate(errp, err);
229 }
230 
231 static void s390_cpu_initfn(Object *obj)
232 {
233     CPUState *cs = CPU(obj);
234     S390CPU *cpu = S390_CPU(obj);
235     CPUS390XState *env = &cpu->env;
236 #if !defined(CONFIG_USER_ONLY)
237     struct tm tm;
238 #endif
239 
240     cs->env_ptr = env;
241     cs->halted = 1;
242     cs->exception_index = EXCP_HLT;
243     s390_cpu_model_register_props(obj);
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;
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);
251     s390_cpu_set_state(CPU_STATE_STOPPED, cpu);
252 #endif
253 }
254 
255 static 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);
261     g_free(cpu->irqstate);
262 #endif
263 }
264 
265 #if !defined(CONFIG_USER_ONLY)
266 static 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 
272 static 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) {
281             if (!disabled_wait(cpu)) {
282                 nr_running++;
283             }
284         }
285     }
286 
287     return nr_running;
288 }
289 
290 unsigned int s390_cpu_halt(S390CPU *cpu)
291 {
292     CPUState *cs = CPU(cpu);
293     trace_cpu_halt(cs->cpu_index);
294 
295     if (!cs->halted) {
296         cs->halted = 1;
297         cs->exception_index = EXCP_HLT;
298     }
299 
300     return s390_count_running_cpus();
301 }
302 
303 void s390_cpu_unhalt(S390CPU *cpu)
304 {
305     CPUState *cs = CPU(cpu);
306     trace_cpu_unhalt(cs->cpu_index);
307 
308     if (cs->halted) {
309         cs->halted = 0;
310         cs->exception_index = -1;
311     }
312 }
313 
314 unsigned 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:
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         }
335         break;
336     default:
337         error_report("Requested CPU state is not a valid S390 CPU state: %u",
338                      cpu_state);
339         exit(1);
340     }
341     if (kvm_enabled() && cpu->env.cpu_state != cpu_state) {
342         kvm_s390_set_cpu_state(cpu, cpu_state);
343     }
344     cpu->env.cpu_state = cpu_state;
345 
346     return s390_count_running_cpus();
347 }
348 
349 int s390_get_clock(uint8_t *tod_high, uint64_t *tod_low)
350 {
351     int r = 0;
352 
353     if (kvm_enabled()) {
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;
362     }
363 
364     return r;
365 }
366 
367 int s390_set_clock(uint8_t *tod_high, uint64_t *tod_low)
368 {
369     int r = 0;
370 
371     if (kvm_enabled()) {
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         }
376     }
377     /* Fixme TCG */
378     return r;
379 }
380 
381 int 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 
389 void s390_cmma_reset(void)
390 {
391     if (kvm_enabled()) {
392         kvm_s390_cmma_reset();
393     }
394 }
395 
396 int 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 
405 int 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 
415 void s390_crypto_reset(void)
416 {
417     if (kvm_enabled()) {
418         kvm_s390_crypto_reset();
419     }
420 }
421 
422 bool 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 }
431 
432 void s390_enable_css_support(S390CPU *cpu)
433 {
434     if (kvm_enabled()) {
435         kvm_s390_enable_css_support(cpu);
436     }
437 }
438 #endif
439 
440 static gchar *s390_gdb_arch_name(CPUState *cs)
441 {
442     return g_strdup("s390:64-bit");
443 }
444 
445 static Property s390x_cpu_properties[] = {
446 #if !defined(CONFIG_USER_ONLY)
447     DEFINE_PROP_UINT32("core-id", S390CPU, env.core_id, 0),
448 #endif
449     DEFINE_PROP_END_OF_LIST()
450 };
451 
452 static void s390_cpu_class_init(ObjectClass *oc, void *data)
453 {
454     S390CPUClass *scc = S390_CPU_CLASS(oc);
455     CPUClass *cc = CPU_CLASS(scc);
456     DeviceClass *dc = DEVICE_CLASS(oc);
457 
458     device_class_set_parent_realize(dc, s390_cpu_realizefn,
459                                     &scc->parent_realize);
460     dc->props = s390x_cpu_properties;
461     dc->user_creatable = true;
462 
463     scc->parent_reset = cc->reset;
464 #if !defined(CONFIG_USER_ONLY)
465     scc->load_normal = s390_cpu_load_normal;
466 #endif
467     scc->cpu_reset = s390_cpu_reset;
468     scc->initial_cpu_reset = s390_cpu_initial_reset;
469     cc->reset = s390_cpu_full_reset;
470     cc->class_by_name = s390_cpu_class_by_name,
471     cc->has_work = s390_cpu_has_work;
472 #ifdef CONFIG_TCG
473     cc->do_interrupt = s390_cpu_do_interrupt;
474 #endif
475     cc->dump_state = s390_cpu_dump_state;
476     cc->set_pc = s390_cpu_set_pc;
477     cc->gdb_read_register = s390_cpu_gdb_read_register;
478     cc->gdb_write_register = s390_cpu_gdb_write_register;
479 #ifdef CONFIG_USER_ONLY
480     cc->handle_mmu_fault = s390_cpu_handle_mmu_fault;
481 #else
482     cc->get_phys_page_debug = s390_cpu_get_phys_page_debug;
483     cc->vmsd = &vmstate_s390_cpu;
484     cc->write_elf64_note = s390_cpu_write_elf64_note;
485 #ifdef CONFIG_TCG
486     cc->cpu_exec_interrupt = s390_cpu_exec_interrupt;
487     cc->debug_excp_handler = s390x_cpu_debug_excp_handler;
488     cc->do_unaligned_access = s390x_cpu_do_unaligned_access;
489 #endif
490 #endif
491     cc->disas_set_info = s390_cpu_disas_set_info;
492 #ifdef CONFIG_TCG
493     cc->tcg_initialize = s390x_translate_init;
494 #endif
495 
496     cc->gdb_num_core_regs = S390_NUM_CORE_REGS;
497     cc->gdb_core_xml_file = "s390x-core64.xml";
498     cc->gdb_arch_name = s390_gdb_arch_name;
499 
500     s390_cpu_model_class_register_props(oc);
501 }
502 
503 static const TypeInfo s390_cpu_type_info = {
504     .name = TYPE_S390_CPU,
505     .parent = TYPE_CPU,
506     .instance_size = sizeof(S390CPU),
507     .instance_init = s390_cpu_initfn,
508     .instance_finalize = s390_cpu_finalize,
509     .abstract = true,
510     .class_size = sizeof(S390CPUClass),
511     .class_init = s390_cpu_class_init,
512 };
513 
514 static void s390_cpu_register_types(void)
515 {
516     type_register_static(&s390_cpu_type_info);
517 }
518 
519 type_init(s390_cpu_register_types)
520