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