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