xref: /openbmc/qemu/target/s390x/cpu.c (revision 70b75667)
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 program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program 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  * General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, see <http://www.gnu.org/licenses/>.
21  */
22 
23 #include "qemu/osdep.h"
24 #include "qapi/error.h"
25 #include "cpu.h"
26 #include "internal.h"
27 #include "kvm_s390x.h"
28 #include "sysemu/kvm.h"
29 #include "sysemu/reset.h"
30 #include "qemu/timer.h"
31 #include "qemu/error-report.h"
32 #include "qemu/module.h"
33 #include "trace.h"
34 #include "qapi/visitor.h"
35 #include "qapi/qapi-types-machine.h"
36 #include "qapi/qapi-visit-run-state.h"
37 #include "sysemu/hw_accel.h"
38 #include "hw/qdev-properties.h"
39 #ifndef CONFIG_USER_ONLY
40 #include "hw/s390x/pv.h"
41 #include "hw/boards.h"
42 #include "sysemu/arch_init.h"
43 #include "sysemu/sysemu.h"
44 #include "sysemu/tcg.h"
45 #endif
46 #include "fpu/softfloat-helpers.h"
47 #include "disas/capstone.h"
48 
49 #define CR0_RESET       0xE0UL
50 #define CR14_RESET      0xC2000000UL;
51 
52 static void s390_cpu_set_pc(CPUState *cs, vaddr value)
53 {
54     S390CPU *cpu = S390_CPU(cs);
55 
56     cpu->env.psw.addr = value;
57 }
58 
59 static bool s390_cpu_has_work(CPUState *cs)
60 {
61     S390CPU *cpu = S390_CPU(cs);
62 
63     /* STOPPED cpus can never wake up */
64     if (s390_cpu_get_state(cpu) != S390_CPU_STATE_LOAD &&
65         s390_cpu_get_state(cpu) != S390_CPU_STATE_OPERATING) {
66         return false;
67     }
68 
69     if (!(cs->interrupt_request & CPU_INTERRUPT_HARD)) {
70         return false;
71     }
72 
73     return s390_cpu_has_int(cpu);
74 }
75 
76 #if !defined(CONFIG_USER_ONLY)
77 /* S390CPUClass::load_normal() */
78 static void s390_cpu_load_normal(CPUState *s)
79 {
80     S390CPU *cpu = S390_CPU(s);
81     uint64_t spsw;
82 
83     if (!s390_is_pv()) {
84         spsw = ldq_phys(s->as, 0);
85         cpu->env.psw.mask = spsw & PSW_MASK_SHORT_CTRL;
86         /*
87          * Invert short psw indication, so SIE will report a specification
88          * exception if it was not set.
89          */
90         cpu->env.psw.mask ^= PSW_MASK_SHORTPSW;
91         cpu->env.psw.addr = spsw & PSW_MASK_SHORT_ADDR;
92     } else {
93         /*
94          * Firmware requires us to set the load state before we set
95          * the cpu to operating on protected guests.
96          */
97         s390_cpu_set_state(S390_CPU_STATE_LOAD, cpu);
98     }
99     s390_cpu_set_state(S390_CPU_STATE_OPERATING, cpu);
100 }
101 #endif
102 
103 /* S390CPUClass::reset() */
104 static void s390_cpu_reset(CPUState *s, cpu_reset_type type)
105 {
106     S390CPU *cpu = S390_CPU(s);
107     S390CPUClass *scc = S390_CPU_GET_CLASS(cpu);
108     CPUS390XState *env = &cpu->env;
109     DeviceState *dev = DEVICE(s);
110 
111     scc->parent_reset(dev);
112     cpu->env.sigp_order = 0;
113     s390_cpu_set_state(S390_CPU_STATE_STOPPED, cpu);
114 
115     switch (type) {
116     case S390_CPU_RESET_CLEAR:
117         memset(env, 0, offsetof(CPUS390XState, start_initial_reset_fields));
118         /* fall through */
119     case S390_CPU_RESET_INITIAL:
120         /* initial reset does not clear everything! */
121         memset(&env->start_initial_reset_fields, 0,
122                offsetof(CPUS390XState, start_normal_reset_fields) -
123                offsetof(CPUS390XState, start_initial_reset_fields));
124 
125         /* architectured initial value for Breaking-Event-Address register */
126         env->gbea = 1;
127 
128         /* architectured initial values for CR 0 and 14 */
129         env->cregs[0] = CR0_RESET;
130         env->cregs[14] = CR14_RESET;
131 
132 #if defined(CONFIG_USER_ONLY)
133         /* user mode should always be allowed to use the full FPU */
134         env->cregs[0] |= CR0_AFP;
135         if (s390_has_feat(S390_FEAT_VECTOR)) {
136             env->cregs[0] |= CR0_VECTOR;
137         }
138 #endif
139 
140         /* tininess for underflow is detected before rounding */
141         set_float_detect_tininess(float_tininess_before_rounding,
142                                   &env->fpu_status);
143        /* fall through */
144     case S390_CPU_RESET_NORMAL:
145         env->psw.mask &= ~PSW_MASK_RI;
146         memset(&env->start_normal_reset_fields, 0,
147                offsetof(CPUS390XState, end_reset_fields) -
148                offsetof(CPUS390XState, start_normal_reset_fields));
149 
150         env->pfault_token = -1UL;
151         env->bpbc = false;
152         break;
153     default:
154         g_assert_not_reached();
155     }
156 
157     /* Reset state inside the kernel that we cannot access yet from QEMU. */
158     if (kvm_enabled()) {
159         switch (type) {
160         case S390_CPU_RESET_CLEAR:
161             kvm_s390_reset_vcpu_clear(cpu);
162             break;
163         case S390_CPU_RESET_INITIAL:
164             kvm_s390_reset_vcpu_initial(cpu);
165             break;
166         case S390_CPU_RESET_NORMAL:
167             kvm_s390_reset_vcpu_normal(cpu);
168             break;
169         }
170     }
171 }
172 
173 #if !defined(CONFIG_USER_ONLY)
174 static void s390_cpu_machine_reset_cb(void *opaque)
175 {
176     S390CPU *cpu = opaque;
177 
178     run_on_cpu(CPU(cpu), s390_do_cpu_full_reset, RUN_ON_CPU_NULL);
179 }
180 #endif
181 
182 static void s390_cpu_disas_set_info(CPUState *cpu, disassemble_info *info)
183 {
184     info->mach = bfd_mach_s390_64;
185     info->print_insn = print_insn_s390;
186     info->cap_arch = CS_ARCH_SYSZ;
187     info->cap_insn_unit = 2;
188     info->cap_insn_split = 6;
189 }
190 
191 static void s390_cpu_realizefn(DeviceState *dev, Error **errp)
192 {
193     CPUState *cs = CPU(dev);
194     S390CPUClass *scc = S390_CPU_GET_CLASS(dev);
195 #if !defined(CONFIG_USER_ONLY)
196     S390CPU *cpu = S390_CPU(dev);
197 #endif
198     Error *err = NULL;
199 
200     /* the model has to be realized before qemu_init_vcpu() due to kvm */
201     s390_realize_cpu_model(cs, &err);
202     if (err) {
203         goto out;
204     }
205 
206 #if !defined(CONFIG_USER_ONLY)
207     MachineState *ms = MACHINE(qdev_get_machine());
208     unsigned int max_cpus = ms->smp.max_cpus;
209     if (cpu->env.core_id >= max_cpus) {
210         error_setg(&err, "Unable to add CPU with core-id: %" PRIu32
211                    ", maximum core-id: %d", cpu->env.core_id,
212                    max_cpus - 1);
213         goto out;
214     }
215 
216     if (cpu_exists(cpu->env.core_id)) {
217         error_setg(&err, "Unable to add CPU with core-id: %" PRIu32
218                    ", it already exists", cpu->env.core_id);
219         goto out;
220     }
221 
222     /* sync cs->cpu_index and env->core_id. The latter is needed for TCG. */
223     cs->cpu_index = cpu->env.core_id;
224 #endif
225 
226     cpu_exec_realizefn(cs, &err);
227     if (err != NULL) {
228         goto out;
229     }
230 
231 #if !defined(CONFIG_USER_ONLY)
232     qemu_register_reset(s390_cpu_machine_reset_cb, cpu);
233 #endif
234     s390_cpu_gdb_init(cs);
235     qemu_init_vcpu(cs);
236 
237     /*
238      * KVM requires the initial CPU reset ioctl to be executed on the target
239      * CPU thread. CPU hotplug under single-threaded TCG will not work with
240      * run_on_cpu(), as run_on_cpu() will not work properly if called while
241      * the main thread is already running but the CPU hasn't been realized.
242      */
243     if (kvm_enabled()) {
244         run_on_cpu(cs, s390_do_cpu_full_reset, RUN_ON_CPU_NULL);
245     } else {
246         cpu_reset(cs);
247     }
248 
249     scc->parent_realize(dev, &err);
250 out:
251     error_propagate(errp, err);
252 }
253 
254 #if !defined(CONFIG_USER_ONLY)
255 static GuestPanicInformation *s390_cpu_get_crash_info(CPUState *cs)
256 {
257     GuestPanicInformation *panic_info;
258     S390CPU *cpu = S390_CPU(cs);
259 
260     cpu_synchronize_state(cs);
261     panic_info = g_malloc0(sizeof(GuestPanicInformation));
262 
263     panic_info->type = GUEST_PANIC_INFORMATION_TYPE_S390;
264     panic_info->u.s390.core = cpu->env.core_id;
265     panic_info->u.s390.psw_mask = cpu->env.psw.mask;
266     panic_info->u.s390.psw_addr = cpu->env.psw.addr;
267     panic_info->u.s390.reason = cpu->env.crash_reason;
268 
269     return panic_info;
270 }
271 
272 static void s390_cpu_get_crash_info_qom(Object *obj, Visitor *v,
273                                         const char *name, void *opaque,
274                                         Error **errp)
275 {
276     CPUState *cs = CPU(obj);
277     GuestPanicInformation *panic_info;
278 
279     if (!cs->crash_occurred) {
280         error_setg(errp, "No crash occurred");
281         return;
282     }
283 
284     panic_info = s390_cpu_get_crash_info(cs);
285 
286     visit_type_GuestPanicInformation(v, "crash-information", &panic_info,
287                                      errp);
288     qapi_free_GuestPanicInformation(panic_info);
289 }
290 #endif
291 
292 static void s390_cpu_initfn(Object *obj)
293 {
294     CPUState *cs = CPU(obj);
295     S390CPU *cpu = S390_CPU(obj);
296 
297     cpu_set_cpustate_pointers(cpu);
298     cs->exception_index = EXCP_HLT;
299 #if !defined(CONFIG_USER_ONLY)
300     cs->start_powered_off = true;
301     object_property_add(obj, "crash-information", "GuestPanicInformation",
302                         s390_cpu_get_crash_info_qom, NULL, NULL, NULL);
303     cpu->env.tod_timer =
304         timer_new_ns(QEMU_CLOCK_VIRTUAL, s390x_tod_timer, cpu);
305     cpu->env.cpu_timer =
306         timer_new_ns(QEMU_CLOCK_VIRTUAL, s390x_cpu_timer, cpu);
307     s390_cpu_set_state(S390_CPU_STATE_STOPPED, cpu);
308 #endif
309 }
310 
311 static void s390_cpu_finalize(Object *obj)
312 {
313 #if !defined(CONFIG_USER_ONLY)
314     S390CPU *cpu = S390_CPU(obj);
315 
316     timer_del(cpu->env.tod_timer);
317     timer_free(cpu->env.tod_timer);
318     timer_del(cpu->env.cpu_timer);
319     timer_free(cpu->env.cpu_timer);
320 
321     qemu_unregister_reset(s390_cpu_machine_reset_cb, cpu);
322     g_free(cpu->irqstate);
323 #endif
324 }
325 
326 #if !defined(CONFIG_USER_ONLY)
327 static bool disabled_wait(CPUState *cpu)
328 {
329     return cpu->halted && !(S390_CPU(cpu)->env.psw.mask &
330                             (PSW_MASK_IO | PSW_MASK_EXT | PSW_MASK_MCHECK));
331 }
332 
333 static unsigned s390_count_running_cpus(void)
334 {
335     CPUState *cpu;
336     int nr_running = 0;
337 
338     CPU_FOREACH(cpu) {
339         uint8_t state = S390_CPU(cpu)->env.cpu_state;
340         if (state == S390_CPU_STATE_OPERATING ||
341             state == S390_CPU_STATE_LOAD) {
342             if (!disabled_wait(cpu)) {
343                 nr_running++;
344             }
345         }
346     }
347 
348     return nr_running;
349 }
350 
351 unsigned int s390_cpu_halt(S390CPU *cpu)
352 {
353     CPUState *cs = CPU(cpu);
354     trace_cpu_halt(cs->cpu_index);
355 
356     if (!cs->halted) {
357         cs->halted = 1;
358         cs->exception_index = EXCP_HLT;
359     }
360 
361     return s390_count_running_cpus();
362 }
363 
364 void s390_cpu_unhalt(S390CPU *cpu)
365 {
366     CPUState *cs = CPU(cpu);
367     trace_cpu_unhalt(cs->cpu_index);
368 
369     if (cs->halted) {
370         cs->halted = 0;
371         cs->exception_index = -1;
372     }
373 }
374 
375 unsigned int s390_cpu_set_state(uint8_t cpu_state, S390CPU *cpu)
376  {
377     trace_cpu_set_state(CPU(cpu)->cpu_index, cpu_state);
378 
379     switch (cpu_state) {
380     case S390_CPU_STATE_STOPPED:
381     case S390_CPU_STATE_CHECK_STOP:
382         /* halt the cpu for common infrastructure */
383         s390_cpu_halt(cpu);
384         break;
385     case S390_CPU_STATE_OPERATING:
386     case S390_CPU_STATE_LOAD:
387         /*
388          * Starting a CPU with a PSW WAIT bit set:
389          * KVM: handles this internally and triggers another WAIT exit.
390          * TCG: will actually try to continue to run. Don't unhalt, will
391          *      be done when the CPU actually has work (an interrupt).
392          */
393         if (!tcg_enabled() || !(cpu->env.psw.mask & PSW_MASK_WAIT)) {
394             s390_cpu_unhalt(cpu);
395         }
396         break;
397     default:
398         error_report("Requested CPU state is not a valid S390 CPU state: %u",
399                      cpu_state);
400         exit(1);
401     }
402     if (kvm_enabled() && cpu->env.cpu_state != cpu_state) {
403         kvm_s390_set_cpu_state(cpu, cpu_state);
404     }
405     cpu->env.cpu_state = cpu_state;
406 
407     return s390_count_running_cpus();
408 }
409 
410 int s390_set_memory_limit(uint64_t new_limit, uint64_t *hw_limit)
411 {
412     if (kvm_enabled()) {
413         return kvm_s390_set_mem_limit(new_limit, hw_limit);
414     }
415     return 0;
416 }
417 
418 void s390_set_max_pagesize(uint64_t pagesize, Error **errp)
419 {
420     if (kvm_enabled()) {
421         kvm_s390_set_max_pagesize(pagesize, errp);
422     }
423 }
424 
425 void s390_cmma_reset(void)
426 {
427     if (kvm_enabled()) {
428         kvm_s390_cmma_reset();
429     }
430 }
431 
432 int s390_assign_subch_ioeventfd(EventNotifier *notifier, uint32_t sch_id,
433                                 int vq, bool assign)
434 {
435     if (kvm_enabled()) {
436         return kvm_s390_assign_subch_ioeventfd(notifier, sch_id, vq, assign);
437     } else {
438         return 0;
439     }
440 }
441 
442 void s390_crypto_reset(void)
443 {
444     if (kvm_enabled()) {
445         kvm_s390_crypto_reset();
446     }
447 }
448 
449 void s390_enable_css_support(S390CPU *cpu)
450 {
451     if (kvm_enabled()) {
452         kvm_s390_enable_css_support(cpu);
453     }
454 }
455 
456 void s390_do_cpu_set_diag318(CPUState *cs, run_on_cpu_data arg)
457 {
458     if (kvm_enabled()) {
459         kvm_s390_set_diag318(cs, arg.host_ulong);
460     }
461 }
462 #endif
463 
464 static gchar *s390_gdb_arch_name(CPUState *cs)
465 {
466     return g_strdup("s390:64-bit");
467 }
468 
469 static Property s390x_cpu_properties[] = {
470 #if !defined(CONFIG_USER_ONLY)
471     DEFINE_PROP_UINT32("core-id", S390CPU, env.core_id, 0),
472 #endif
473     DEFINE_PROP_END_OF_LIST()
474 };
475 
476 static void s390_cpu_reset_full(DeviceState *dev)
477 {
478     CPUState *s = CPU(dev);
479     return s390_cpu_reset(s, S390_CPU_RESET_CLEAR);
480 }
481 
482 static void s390_cpu_class_init(ObjectClass *oc, void *data)
483 {
484     S390CPUClass *scc = S390_CPU_CLASS(oc);
485     CPUClass *cc = CPU_CLASS(scc);
486     DeviceClass *dc = DEVICE_CLASS(oc);
487 
488     device_class_set_parent_realize(dc, s390_cpu_realizefn,
489                                     &scc->parent_realize);
490     device_class_set_props(dc, s390x_cpu_properties);
491     dc->user_creatable = true;
492 
493     device_class_set_parent_reset(dc, s390_cpu_reset_full, &scc->parent_reset);
494 #if !defined(CONFIG_USER_ONLY)
495     scc->load_normal = s390_cpu_load_normal;
496 #endif
497     scc->reset = s390_cpu_reset;
498     cc->class_by_name = s390_cpu_class_by_name,
499     cc->has_work = s390_cpu_has_work;
500 #ifdef CONFIG_TCG
501     cc->do_interrupt = s390_cpu_do_interrupt;
502 #endif
503     cc->dump_state = s390_cpu_dump_state;
504     cc->set_pc = s390_cpu_set_pc;
505     cc->gdb_read_register = s390_cpu_gdb_read_register;
506     cc->gdb_write_register = s390_cpu_gdb_write_register;
507 #ifndef CONFIG_USER_ONLY
508     cc->get_phys_page_debug = s390_cpu_get_phys_page_debug;
509     cc->vmsd = &vmstate_s390_cpu;
510     cc->get_crash_info = s390_cpu_get_crash_info;
511     cc->write_elf64_note = s390_cpu_write_elf64_note;
512 #ifdef CONFIG_TCG
513     cc->cpu_exec_interrupt = s390_cpu_exec_interrupt;
514     cc->debug_excp_handler = s390x_cpu_debug_excp_handler;
515     cc->do_unaligned_access = s390x_cpu_do_unaligned_access;
516 #endif
517 #endif
518     cc->disas_set_info = s390_cpu_disas_set_info;
519 #ifdef CONFIG_TCG
520     cc->tcg_initialize = s390x_translate_init;
521     cc->tlb_fill = s390_cpu_tlb_fill;
522 #endif
523 
524     cc->gdb_num_core_regs = S390_NUM_CORE_REGS;
525     cc->gdb_core_xml_file = "s390x-core64.xml";
526     cc->gdb_arch_name = s390_gdb_arch_name;
527 
528     s390_cpu_model_class_register_props(oc);
529 }
530 
531 static const TypeInfo s390_cpu_type_info = {
532     .name = TYPE_S390_CPU,
533     .parent = TYPE_CPU,
534     .instance_size = sizeof(S390CPU),
535     .instance_align = __alignof__(S390CPU),
536     .instance_init = s390_cpu_initfn,
537     .instance_finalize = s390_cpu_finalize,
538     .abstract = true,
539     .class_size = sizeof(S390CPUClass),
540     .class_init = s390_cpu_class_init,
541 };
542 
543 static void s390_cpu_register_types(void)
544 {
545     type_register_static(&s390_cpu_type_info);
546 }
547 
548 type_init(s390_cpu_register_types)
549