xref: /openbmc/qemu/target/s390x/cpu.c (revision fa3673e4)
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 "s390x-internal.h"
27 #include "kvm/kvm_s390x.h"
28 #include "sysemu/kvm.h"
29 #include "qemu/module.h"
30 #include "trace.h"
31 #include "qapi/qapi-types-machine.h"
32 #include "sysemu/hw_accel.h"
33 #include "hw/qdev-properties.h"
34 #include "hw/qdev-properties-system.h"
35 #include "fpu/softfloat-helpers.h"
36 #include "disas/capstone.h"
37 #include "sysemu/tcg.h"
38 #ifndef CONFIG_USER_ONLY
39 #include "sysemu/reset.h"
40 #endif
41 #include "hw/s390x/cpu-topology.h"
42 
43 #define CR0_RESET       0xE0UL
44 #define CR14_RESET      0xC2000000UL;
45 
46 #ifndef CONFIG_USER_ONLY
47 static bool is_early_exception_psw(uint64_t mask, uint64_t addr)
48 {
49     if (mask & PSW_MASK_RESERVED) {
50         return true;
51     }
52 
53     switch (mask & (PSW_MASK_32 | PSW_MASK_64)) {
54     case 0:
55         return addr & ~0xffffffULL;
56     case PSW_MASK_32:
57         return addr & ~0x7fffffffULL;
58     case PSW_MASK_32 | PSW_MASK_64:
59         return false;
60     default: /* PSW_MASK_64 */
61         return true;
62     }
63 }
64 #endif
65 
66 void s390_cpu_set_psw(CPUS390XState *env, uint64_t mask, uint64_t addr)
67 {
68 #ifndef CONFIG_USER_ONLY
69     uint64_t old_mask = env->psw.mask;
70 #endif
71 
72     env->psw.addr = addr;
73     env->psw.mask = mask;
74 
75     /* KVM will handle all WAITs and trigger a WAIT exit on disabled_wait */
76     if (!tcg_enabled()) {
77         return;
78     }
79     env->cc_op = (mask >> 44) & 3;
80 
81 #ifndef CONFIG_USER_ONLY
82     if (is_early_exception_psw(mask, addr)) {
83         env->int_pgm_ilen = 0;
84         trigger_pgm_exception(env, PGM_SPECIFICATION);
85         return;
86     }
87 
88     if ((old_mask ^ mask) & PSW_MASK_PER) {
89         s390_cpu_recompute_watchpoints(env_cpu(env));
90     }
91 
92     if (mask & PSW_MASK_WAIT) {
93         s390_handle_wait(env_archcpu(env));
94     }
95 #endif
96 }
97 
98 uint64_t s390_cpu_get_psw_mask(CPUS390XState *env)
99 {
100     uint64_t r = env->psw.mask;
101 
102     if (tcg_enabled()) {
103         uint64_t cc = calc_cc(env, env->cc_op, env->cc_src,
104                               env->cc_dst, env->cc_vr);
105 
106         assert(cc <= 3);
107         r &= ~PSW_MASK_CC;
108         r |= cc << 44;
109     }
110 
111     return r;
112 }
113 
114 static void s390_cpu_set_pc(CPUState *cs, vaddr value)
115 {
116     S390CPU *cpu = S390_CPU(cs);
117 
118     cpu->env.psw.addr = value;
119 }
120 
121 static vaddr s390_cpu_get_pc(CPUState *cs)
122 {
123     S390CPU *cpu = S390_CPU(cs);
124 
125     return cpu->env.psw.addr;
126 }
127 
128 static bool s390_cpu_has_work(CPUState *cs)
129 {
130     S390CPU *cpu = S390_CPU(cs);
131 
132     /* STOPPED cpus can never wake up */
133     if (s390_cpu_get_state(cpu) != S390_CPU_STATE_LOAD &&
134         s390_cpu_get_state(cpu) != S390_CPU_STATE_OPERATING) {
135         return false;
136     }
137 
138     if (!(cs->interrupt_request & CPU_INTERRUPT_HARD)) {
139         return false;
140     }
141 
142     return s390_cpu_has_int(cpu);
143 }
144 
145 static void s390_query_cpu_fast(CPUState *cpu, CpuInfoFast *value)
146 {
147     S390CPU *s390_cpu = S390_CPU(cpu);
148 
149     value->u.s390x.cpu_state = s390_cpu->env.cpu_state;
150 #if !defined(CONFIG_USER_ONLY)
151     if (s390_has_topology()) {
152         value->u.s390x.has_dedicated = true;
153         value->u.s390x.dedicated = s390_cpu->env.dedicated;
154         value->u.s390x.has_entitlement = true;
155         value->u.s390x.entitlement = s390_cpu->env.entitlement;
156     }
157 #endif
158 }
159 
160 /* S390CPUClass::reset() */
161 static void s390_cpu_reset(CPUState *s, cpu_reset_type type)
162 {
163     S390CPU *cpu = S390_CPU(s);
164     S390CPUClass *scc = S390_CPU_GET_CLASS(cpu);
165     CPUS390XState *env = &cpu->env;
166     DeviceState *dev = DEVICE(s);
167 
168     scc->parent_reset(dev);
169     cpu->env.sigp_order = 0;
170     s390_cpu_set_state(S390_CPU_STATE_STOPPED, cpu);
171 
172     switch (type) {
173     case S390_CPU_RESET_CLEAR:
174         memset(env, 0, offsetof(CPUS390XState, start_initial_reset_fields));
175         /* fall through */
176     case S390_CPU_RESET_INITIAL:
177         /* initial reset does not clear everything! */
178         memset(&env->start_initial_reset_fields, 0,
179                offsetof(CPUS390XState, start_normal_reset_fields) -
180                offsetof(CPUS390XState, start_initial_reset_fields));
181 
182         /* architectured initial value for Breaking-Event-Address register */
183         env->gbea = 1;
184 
185         /* architectured initial values for CR 0 and 14 */
186         env->cregs[0] = CR0_RESET;
187         env->cregs[14] = CR14_RESET;
188 
189 #if defined(CONFIG_USER_ONLY)
190         /* user mode should always be allowed to use the full FPU */
191         env->cregs[0] |= CR0_AFP;
192         if (s390_has_feat(S390_FEAT_VECTOR)) {
193             env->cregs[0] |= CR0_VECTOR;
194         }
195 #endif
196 
197         /* tininess for underflow is detected before rounding */
198         set_float_detect_tininess(float_tininess_before_rounding,
199                                   &env->fpu_status);
200        /* fall through */
201     case S390_CPU_RESET_NORMAL:
202         env->psw.mask &= ~PSW_MASK_RI;
203         memset(&env->start_normal_reset_fields, 0,
204                offsetof(CPUS390XState, end_reset_fields) -
205                offsetof(CPUS390XState, start_normal_reset_fields));
206 
207         env->pfault_token = -1UL;
208         env->bpbc = false;
209         break;
210     default:
211         g_assert_not_reached();
212     }
213 
214     /* Reset state inside the kernel that we cannot access yet from QEMU. */
215     if (kvm_enabled()) {
216         switch (type) {
217         case S390_CPU_RESET_CLEAR:
218             kvm_s390_reset_vcpu_clear(cpu);
219             break;
220         case S390_CPU_RESET_INITIAL:
221             kvm_s390_reset_vcpu_initial(cpu);
222             break;
223         case S390_CPU_RESET_NORMAL:
224             kvm_s390_reset_vcpu_normal(cpu);
225             break;
226         }
227     }
228 }
229 
230 static void s390_cpu_disas_set_info(CPUState *cpu, disassemble_info *info)
231 {
232     info->mach = bfd_mach_s390_64;
233     info->cap_arch = CS_ARCH_SYSZ;
234     info->cap_insn_unit = 2;
235     info->cap_insn_split = 6;
236 }
237 
238 static void s390_cpu_realizefn(DeviceState *dev, Error **errp)
239 {
240     CPUState *cs = CPU(dev);
241     S390CPUClass *scc = S390_CPU_GET_CLASS(dev);
242     Error *err = NULL;
243 
244     /* the model has to be realized before qemu_init_vcpu() due to kvm */
245     s390_realize_cpu_model(cs, &err);
246     if (err) {
247         goto out;
248     }
249 
250 #if !defined(CONFIG_USER_ONLY)
251     if (!s390_cpu_realize_sysemu(dev, &err)) {
252         goto out;
253     }
254 #endif
255 
256     cpu_exec_realizefn(cs, &err);
257     if (err != NULL) {
258         goto out;
259     }
260 
261 #if !defined(CONFIG_USER_ONLY)
262     qemu_register_reset(s390_cpu_machine_reset_cb, S390_CPU(dev));
263 #endif
264     s390_cpu_gdb_init(cs);
265     qemu_init_vcpu(cs);
266 
267     /*
268      * KVM requires the initial CPU reset ioctl to be executed on the target
269      * CPU thread. CPU hotplug under single-threaded TCG will not work with
270      * run_on_cpu(), as run_on_cpu() will not work properly if called while
271      * the main thread is already running but the CPU hasn't been realized.
272      */
273     if (kvm_enabled()) {
274         run_on_cpu(cs, s390_do_cpu_full_reset, RUN_ON_CPU_NULL);
275     } else {
276         cpu_reset(cs);
277     }
278 
279     scc->parent_realize(dev, &err);
280 out:
281     error_propagate(errp, err);
282 }
283 
284 static void s390_cpu_initfn(Object *obj)
285 {
286     CPUState *cs = CPU(obj);
287 
288     cs->exception_index = EXCP_HLT;
289 
290 #if !defined(CONFIG_USER_ONLY)
291     s390_cpu_init_sysemu(obj);
292 #endif
293 }
294 
295 static const gchar *s390_gdb_arch_name(CPUState *cs)
296 {
297     return "s390:64-bit";
298 }
299 
300 static Property s390x_cpu_properties[] = {
301 #if !defined(CONFIG_USER_ONLY)
302     DEFINE_PROP_UINT32("core-id", S390CPU, env.core_id, 0),
303     DEFINE_PROP_INT32("socket-id", S390CPU, env.socket_id, -1),
304     DEFINE_PROP_INT32("book-id", S390CPU, env.book_id, -1),
305     DEFINE_PROP_INT32("drawer-id", S390CPU, env.drawer_id, -1),
306     DEFINE_PROP_BOOL("dedicated", S390CPU, env.dedicated, false),
307     DEFINE_PROP_CPUS390ENTITLEMENT("entitlement", S390CPU, env.entitlement,
308                                    S390_CPU_ENTITLEMENT_AUTO),
309 #endif
310     DEFINE_PROP_END_OF_LIST()
311 };
312 
313 static void s390_cpu_reset_full(DeviceState *dev)
314 {
315     CPUState *s = CPU(dev);
316     return s390_cpu_reset(s, S390_CPU_RESET_CLEAR);
317 }
318 
319 #ifdef CONFIG_TCG
320 #include "hw/core/tcg-cpu-ops.h"
321 
322 static const struct TCGCPUOps s390_tcg_ops = {
323     .initialize = s390x_translate_init,
324     .restore_state_to_opc = s390x_restore_state_to_opc,
325 
326 #ifdef CONFIG_USER_ONLY
327     .record_sigsegv = s390_cpu_record_sigsegv,
328     .record_sigbus = s390_cpu_record_sigbus,
329 #else
330     .tlb_fill = s390_cpu_tlb_fill,
331     .cpu_exec_interrupt = s390_cpu_exec_interrupt,
332     .do_interrupt = s390_cpu_do_interrupt,
333     .debug_excp_handler = s390x_cpu_debug_excp_handler,
334     .do_unaligned_access = s390x_cpu_do_unaligned_access,
335 #endif /* !CONFIG_USER_ONLY */
336 };
337 #endif /* CONFIG_TCG */
338 
339 static void s390_cpu_class_init(ObjectClass *oc, void *data)
340 {
341     S390CPUClass *scc = S390_CPU_CLASS(oc);
342     CPUClass *cc = CPU_CLASS(scc);
343     DeviceClass *dc = DEVICE_CLASS(oc);
344 
345     device_class_set_parent_realize(dc, s390_cpu_realizefn,
346                                     &scc->parent_realize);
347     device_class_set_props(dc, s390x_cpu_properties);
348     dc->user_creatable = true;
349 
350     device_class_set_parent_reset(dc, s390_cpu_reset_full, &scc->parent_reset);
351 
352     scc->reset = s390_cpu_reset;
353     cc->class_by_name = s390_cpu_class_by_name,
354     cc->has_work = s390_cpu_has_work;
355     cc->dump_state = s390_cpu_dump_state;
356     cc->query_cpu_fast = s390_query_cpu_fast;
357     cc->set_pc = s390_cpu_set_pc;
358     cc->get_pc = s390_cpu_get_pc;
359     cc->gdb_read_register = s390_cpu_gdb_read_register;
360     cc->gdb_write_register = s390_cpu_gdb_write_register;
361 #ifndef CONFIG_USER_ONLY
362     s390_cpu_class_init_sysemu(cc);
363 #endif
364     cc->disas_set_info = s390_cpu_disas_set_info;
365     cc->gdb_num_core_regs = S390_NUM_CORE_REGS;
366     cc->gdb_core_xml_file = "s390x-core64.xml";
367     cc->gdb_arch_name = s390_gdb_arch_name;
368 
369     s390_cpu_model_class_register_props(oc);
370 
371 #ifdef CONFIG_TCG
372     cc->tcg_ops = &s390_tcg_ops;
373 #endif /* CONFIG_TCG */
374 }
375 
376 static const TypeInfo s390_cpu_type_info = {
377     .name = TYPE_S390_CPU,
378     .parent = TYPE_CPU,
379     .instance_size = sizeof(S390CPU),
380     .instance_align = __alignof__(S390CPU),
381     .instance_init = s390_cpu_initfn,
382 
383 #ifndef CONFIG_USER_ONLY
384     .instance_finalize = s390_cpu_finalize,
385 #endif /* !CONFIG_USER_ONLY */
386 
387     .abstract = true,
388     .class_size = sizeof(S390CPUClass),
389     .class_init = s390_cpu_class_init,
390 };
391 
392 static void s390_cpu_register_types(void)
393 {
394     type_register_static(&s390_cpu_type_info);
395 }
396 
397 type_init(s390_cpu_register_types)
398