xref: /openbmc/qemu/target/s390x/cpu.c (revision e69b2c67)
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 int s390x_cpu_mmu_index(CPUState *cs, bool ifetch)
146 {
147     return s390x_env_mmu_index(cpu_env(cs), ifetch);
148 }
149 
150 static void s390_query_cpu_fast(CPUState *cpu, CpuInfoFast *value)
151 {
152     S390CPU *s390_cpu = S390_CPU(cpu);
153 
154     value->u.s390x.cpu_state = s390_cpu->env.cpu_state;
155 #if !defined(CONFIG_USER_ONLY)
156     if (s390_has_topology()) {
157         value->u.s390x.has_dedicated = true;
158         value->u.s390x.dedicated = s390_cpu->env.dedicated;
159         value->u.s390x.has_entitlement = true;
160         value->u.s390x.entitlement = s390_cpu->env.entitlement;
161     }
162 #endif
163 }
164 
165 /* S390CPUClass::reset() */
166 static void s390_cpu_reset(CPUState *s, cpu_reset_type type)
167 {
168     S390CPU *cpu = S390_CPU(s);
169     S390CPUClass *scc = S390_CPU_GET_CLASS(cpu);
170     CPUS390XState *env = &cpu->env;
171     DeviceState *dev = DEVICE(s);
172 
173     scc->parent_reset(dev);
174     cpu->env.sigp_order = 0;
175     s390_cpu_set_state(S390_CPU_STATE_STOPPED, cpu);
176 
177     switch (type) {
178     case S390_CPU_RESET_CLEAR:
179         memset(env, 0, offsetof(CPUS390XState, start_initial_reset_fields));
180         /* fall through */
181     case S390_CPU_RESET_INITIAL:
182         /* initial reset does not clear everything! */
183         memset(&env->start_initial_reset_fields, 0,
184                offsetof(CPUS390XState, start_normal_reset_fields) -
185                offsetof(CPUS390XState, start_initial_reset_fields));
186 
187         /* architectured initial value for Breaking-Event-Address register */
188         env->gbea = 1;
189 
190         /* architectured initial values for CR 0 and 14 */
191         env->cregs[0] = CR0_RESET;
192         env->cregs[14] = CR14_RESET;
193 
194 #if defined(CONFIG_USER_ONLY)
195         /* user mode should always be allowed to use the full FPU */
196         env->cregs[0] |= CR0_AFP;
197         if (s390_has_feat(S390_FEAT_VECTOR)) {
198             env->cregs[0] |= CR0_VECTOR;
199         }
200 #endif
201 
202         /* tininess for underflow is detected before rounding */
203         set_float_detect_tininess(float_tininess_before_rounding,
204                                   &env->fpu_status);
205        /* fall through */
206     case S390_CPU_RESET_NORMAL:
207         env->psw.mask &= ~PSW_MASK_RI;
208         memset(&env->start_normal_reset_fields, 0,
209                offsetof(CPUS390XState, end_reset_fields) -
210                offsetof(CPUS390XState, start_normal_reset_fields));
211 
212         env->pfault_token = -1UL;
213         env->bpbc = false;
214         break;
215     default:
216         g_assert_not_reached();
217     }
218 
219     /* Reset state inside the kernel that we cannot access yet from QEMU. */
220     if (kvm_enabled()) {
221         switch (type) {
222         case S390_CPU_RESET_CLEAR:
223             kvm_s390_reset_vcpu_clear(cpu);
224             break;
225         case S390_CPU_RESET_INITIAL:
226             kvm_s390_reset_vcpu_initial(cpu);
227             break;
228         case S390_CPU_RESET_NORMAL:
229             kvm_s390_reset_vcpu_normal(cpu);
230             break;
231         }
232     }
233 }
234 
235 static void s390_cpu_disas_set_info(CPUState *cpu, disassemble_info *info)
236 {
237     info->mach = bfd_mach_s390_64;
238     info->cap_arch = CS_ARCH_SYSZ;
239     info->cap_insn_unit = 2;
240     info->cap_insn_split = 6;
241 }
242 
243 static void s390_cpu_realizefn(DeviceState *dev, Error **errp)
244 {
245     CPUState *cs = CPU(dev);
246     S390CPUClass *scc = S390_CPU_GET_CLASS(dev);
247     Error *err = NULL;
248 
249     /* the model has to be realized before qemu_init_vcpu() due to kvm */
250     s390_realize_cpu_model(cs, &err);
251     if (err) {
252         goto out;
253     }
254 
255 #if !defined(CONFIG_USER_ONLY)
256     if (!s390_cpu_realize_sysemu(dev, &err)) {
257         goto out;
258     }
259 #endif
260 
261     cpu_exec_realizefn(cs, &err);
262     if (err != NULL) {
263         goto out;
264     }
265 
266 #if !defined(CONFIG_USER_ONLY)
267     qemu_register_reset(s390_cpu_machine_reset_cb, S390_CPU(dev));
268 #endif
269     s390_cpu_gdb_init(cs);
270     qemu_init_vcpu(cs);
271 
272     /*
273      * KVM requires the initial CPU reset ioctl to be executed on the target
274      * CPU thread. CPU hotplug under single-threaded TCG will not work with
275      * run_on_cpu(), as run_on_cpu() will not work properly if called while
276      * the main thread is already running but the CPU hasn't been realized.
277      */
278     if (kvm_enabled()) {
279         run_on_cpu(cs, s390_do_cpu_full_reset, RUN_ON_CPU_NULL);
280     } else {
281         cpu_reset(cs);
282     }
283 
284     scc->parent_realize(dev, &err);
285 out:
286     error_propagate(errp, err);
287 }
288 
289 static void s390_cpu_initfn(Object *obj)
290 {
291     CPUState *cs = CPU(obj);
292 
293     cs->exception_index = EXCP_HLT;
294 
295 #if !defined(CONFIG_USER_ONLY)
296     s390_cpu_init_sysemu(obj);
297 #endif
298 }
299 
300 static const gchar *s390_gdb_arch_name(CPUState *cs)
301 {
302     return "s390:64-bit";
303 }
304 
305 static Property s390x_cpu_properties[] = {
306 #if !defined(CONFIG_USER_ONLY)
307     DEFINE_PROP_UINT32("core-id", S390CPU, env.core_id, 0),
308     DEFINE_PROP_INT32("socket-id", S390CPU, env.socket_id, -1),
309     DEFINE_PROP_INT32("book-id", S390CPU, env.book_id, -1),
310     DEFINE_PROP_INT32("drawer-id", S390CPU, env.drawer_id, -1),
311     DEFINE_PROP_BOOL("dedicated", S390CPU, env.dedicated, false),
312     DEFINE_PROP_CPUS390ENTITLEMENT("entitlement", S390CPU, env.entitlement,
313                                    S390_CPU_ENTITLEMENT_AUTO),
314 #endif
315     DEFINE_PROP_END_OF_LIST()
316 };
317 
318 static void s390_cpu_reset_full(DeviceState *dev)
319 {
320     CPUState *s = CPU(dev);
321     return s390_cpu_reset(s, S390_CPU_RESET_CLEAR);
322 }
323 
324 #ifdef CONFIG_TCG
325 #include "hw/core/tcg-cpu-ops.h"
326 
327 void cpu_get_tb_cpu_state(CPUS390XState *env, vaddr *pc,
328                           uint64_t *cs_base, uint32_t *pflags)
329 {
330     uint32_t flags;
331 
332     if (env->psw.addr & 1) {
333         /*
334          * Instructions must be at even addresses.
335          * This needs to be checked before address translation.
336          */
337         env->int_pgm_ilen = 2; /* see s390_cpu_tlb_fill() */
338         tcg_s390_program_interrupt(env, PGM_SPECIFICATION, 0);
339     }
340 
341     *pc = env->psw.addr;
342     *cs_base = env->ex_value;
343 
344     flags = (env->psw.mask >> FLAG_MASK_PSW_SHIFT) & FLAG_MASK_PSW;
345     if (env->psw.mask & PSW_MASK_PER) {
346         flags |= env->cregs[9] & (FLAG_MASK_PER_BRANCH |
347                                   FLAG_MASK_PER_IFETCH |
348                                   FLAG_MASK_PER_IFETCH_NULLIFY);
349         if ((env->cregs[9] & PER_CR9_EVENT_STORE) &&
350             (env->cregs[9] & PER_CR9_EVENT_STORE_REAL)) {
351             flags |= FLAG_MASK_PER_STORE_REAL;
352         }
353     }
354     if (env->cregs[0] & CR0_AFP) {
355         flags |= FLAG_MASK_AFP;
356     }
357     if (env->cregs[0] & CR0_VECTOR) {
358         flags |= FLAG_MASK_VECTOR;
359     }
360     *pflags = flags;
361 }
362 
363 static const TCGCPUOps s390_tcg_ops = {
364     .initialize = s390x_translate_init,
365     .restore_state_to_opc = s390x_restore_state_to_opc,
366 
367 #ifdef CONFIG_USER_ONLY
368     .record_sigsegv = s390_cpu_record_sigsegv,
369     .record_sigbus = s390_cpu_record_sigbus,
370 #else
371     .tlb_fill = s390_cpu_tlb_fill,
372     .cpu_exec_interrupt = s390_cpu_exec_interrupt,
373     .cpu_exec_halt = s390_cpu_has_work,
374     .do_interrupt = s390_cpu_do_interrupt,
375     .debug_excp_handler = s390x_cpu_debug_excp_handler,
376     .do_unaligned_access = s390x_cpu_do_unaligned_access,
377 #endif /* !CONFIG_USER_ONLY */
378 };
379 #endif /* CONFIG_TCG */
380 
381 static void s390_cpu_class_init(ObjectClass *oc, void *data)
382 {
383     S390CPUClass *scc = S390_CPU_CLASS(oc);
384     CPUClass *cc = CPU_CLASS(scc);
385     DeviceClass *dc = DEVICE_CLASS(oc);
386 
387     device_class_set_parent_realize(dc, s390_cpu_realizefn,
388                                     &scc->parent_realize);
389     device_class_set_props(dc, s390x_cpu_properties);
390     dc->user_creatable = true;
391 
392     device_class_set_parent_reset(dc, s390_cpu_reset_full, &scc->parent_reset);
393 
394     scc->reset = s390_cpu_reset;
395     cc->class_by_name = s390_cpu_class_by_name,
396     cc->has_work = s390_cpu_has_work;
397     cc->mmu_index = s390x_cpu_mmu_index;
398     cc->dump_state = s390_cpu_dump_state;
399     cc->query_cpu_fast = s390_query_cpu_fast;
400     cc->set_pc = s390_cpu_set_pc;
401     cc->get_pc = s390_cpu_get_pc;
402     cc->gdb_read_register = s390_cpu_gdb_read_register;
403     cc->gdb_write_register = s390_cpu_gdb_write_register;
404 #ifndef CONFIG_USER_ONLY
405     s390_cpu_class_init_sysemu(cc);
406 #endif
407     cc->disas_set_info = s390_cpu_disas_set_info;
408     cc->gdb_core_xml_file = "s390x-core64.xml";
409     cc->gdb_arch_name = s390_gdb_arch_name;
410 
411     s390_cpu_model_class_register_props(oc);
412 
413 #ifdef CONFIG_TCG
414     cc->tcg_ops = &s390_tcg_ops;
415 #endif /* CONFIG_TCG */
416 }
417 
418 static const TypeInfo s390_cpu_type_info = {
419     .name = TYPE_S390_CPU,
420     .parent = TYPE_CPU,
421     .instance_size = sizeof(S390CPU),
422     .instance_align = __alignof__(S390CPU),
423     .instance_init = s390_cpu_initfn,
424 
425 #ifndef CONFIG_USER_ONLY
426     .instance_finalize = s390_cpu_finalize,
427 #endif /* !CONFIG_USER_ONLY */
428 
429     .abstract = true,
430     .class_size = sizeof(S390CPUClass),
431     .class_init = s390_cpu_class_init,
432 };
433 
434 static void s390_cpu_register_types(void)
435 {
436     type_register_static(&s390_cpu_type_info);
437 }
438 
439 type_init(s390_cpu_register_types)
440