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