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