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.
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.
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.
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/>.
23 #include "qemu/osdep.h"
24 #include "qapi/error.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"
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"
39 #define CR0_RESET 0xE0UL
40 #define CR14_RESET 0xC2000000UL;
42 void s390_cpu_set_psw(CPUS390XState
*env
, uint64_t mask
, uint64_t addr
)
44 #ifndef CONFIG_USER_ONLY
45 uint64_t old_mask
= env
->psw
.mask
;
51 /* KVM will handle all WAITs and trigger a WAIT exit on disabled_wait */
55 env
->cc_op
= (mask
>> 44) & 3;
57 #ifndef CONFIG_USER_ONLY
58 if ((old_mask
^ mask
) & PSW_MASK_PER
) {
59 s390_cpu_recompute_watchpoints(env_cpu(env
));
62 if (mask
& PSW_MASK_WAIT
) {
63 s390_handle_wait(env_archcpu(env
));
68 uint64_t s390_cpu_get_psw_mask(CPUS390XState
*env
)
70 uint64_t r
= env
->psw
.mask
;
73 uint64_t cc
= calc_cc(env
, env
->cc_op
, env
->cc_src
,
74 env
->cc_dst
, env
->cc_vr
);
84 static void s390_cpu_set_pc(CPUState
*cs
, vaddr value
)
86 S390CPU
*cpu
= S390_CPU(cs
);
88 cpu
->env
.psw
.addr
= value
;
91 static bool s390_cpu_has_work(CPUState
*cs
)
93 S390CPU
*cpu
= S390_CPU(cs
);
95 /* STOPPED cpus can never wake up */
96 if (s390_cpu_get_state(cpu
) != S390_CPU_STATE_LOAD
&&
97 s390_cpu_get_state(cpu
) != S390_CPU_STATE_OPERATING
) {
101 if (!(cs
->interrupt_request
& CPU_INTERRUPT_HARD
)) {
105 return s390_cpu_has_int(cpu
);
108 /* S390CPUClass::reset() */
109 static void s390_cpu_reset(CPUState
*s
, cpu_reset_type type
)
111 S390CPU
*cpu
= S390_CPU(s
);
112 S390CPUClass
*scc
= S390_CPU_GET_CLASS(cpu
);
113 CPUS390XState
*env
= &cpu
->env
;
114 DeviceState
*dev
= DEVICE(s
);
116 scc
->parent_reset(dev
);
117 cpu
->env
.sigp_order
= 0;
118 s390_cpu_set_state(S390_CPU_STATE_STOPPED
, cpu
);
121 case S390_CPU_RESET_CLEAR
:
122 memset(env
, 0, offsetof(CPUS390XState
, start_initial_reset_fields
));
124 case S390_CPU_RESET_INITIAL
:
125 /* initial reset does not clear everything! */
126 memset(&env
->start_initial_reset_fields
, 0,
127 offsetof(CPUS390XState
, start_normal_reset_fields
) -
128 offsetof(CPUS390XState
, start_initial_reset_fields
));
130 /* architectured initial value for Breaking-Event-Address register */
133 /* architectured initial values for CR 0 and 14 */
134 env
->cregs
[0] = CR0_RESET
;
135 env
->cregs
[14] = CR14_RESET
;
137 #if defined(CONFIG_USER_ONLY)
138 /* user mode should always be allowed to use the full FPU */
139 env
->cregs
[0] |= CR0_AFP
;
140 if (s390_has_feat(S390_FEAT_VECTOR
)) {
141 env
->cregs
[0] |= CR0_VECTOR
;
145 /* tininess for underflow is detected before rounding */
146 set_float_detect_tininess(float_tininess_before_rounding
,
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
));
155 env
->pfault_token
= -1UL;
159 g_assert_not_reached();
162 /* Reset state inside the kernel that we cannot access yet from QEMU. */
165 case S390_CPU_RESET_CLEAR
:
166 kvm_s390_reset_vcpu_clear(cpu
);
168 case S390_CPU_RESET_INITIAL
:
169 kvm_s390_reset_vcpu_initial(cpu
);
171 case S390_CPU_RESET_NORMAL
:
172 kvm_s390_reset_vcpu_normal(cpu
);
178 static void s390_cpu_disas_set_info(CPUState
*cpu
, disassemble_info
*info
)
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;
187 static void s390_cpu_realizefn(DeviceState
*dev
, Error
**errp
)
189 CPUState
*cs
= CPU(dev
);
190 S390CPUClass
*scc
= S390_CPU_GET_CLASS(dev
);
193 /* the model has to be realized before qemu_init_vcpu() due to kvm */
194 s390_realize_cpu_model(cs
, &err
);
199 #if !defined(CONFIG_USER_ONLY)
200 if (!s390_cpu_realize_sysemu(dev
, &err
)) {
205 cpu_exec_realizefn(cs
, &err
);
210 #if !defined(CONFIG_USER_ONLY)
211 qemu_register_reset(s390_cpu_machine_reset_cb
, S390_CPU(dev
));
213 s390_cpu_gdb_init(cs
);
217 * KVM requires the initial CPU reset ioctl to be executed on the target
218 * CPU thread. CPU hotplug under single-threaded TCG will not work with
219 * run_on_cpu(), as run_on_cpu() will not work properly if called while
220 * the main thread is already running but the CPU hasn't been realized.
223 run_on_cpu(cs
, s390_do_cpu_full_reset
, RUN_ON_CPU_NULL
);
228 scc
->parent_realize(dev
, &err
);
230 error_propagate(errp
, err
);
233 static void s390_cpu_initfn(Object
*obj
)
235 CPUState
*cs
= CPU(obj
);
236 S390CPU
*cpu
= S390_CPU(obj
);
238 cpu_set_cpustate_pointers(cpu
);
239 cs
->exception_index
= EXCP_HLT
;
241 #if !defined(CONFIG_USER_ONLY)
242 s390_cpu_init_sysemu(obj
);
246 static gchar
*s390_gdb_arch_name(CPUState
*cs
)
248 return g_strdup("s390:64-bit");
251 static Property s390x_cpu_properties
[] = {
252 #if !defined(CONFIG_USER_ONLY)
253 DEFINE_PROP_UINT32("core-id", S390CPU
, env
.core_id
, 0),
255 DEFINE_PROP_END_OF_LIST()
258 static void s390_cpu_reset_full(DeviceState
*dev
)
260 CPUState
*s
= CPU(dev
);
261 return s390_cpu_reset(s
, S390_CPU_RESET_CLEAR
);
265 #include "hw/core/tcg-cpu-ops.h"
267 static const struct TCGCPUOps s390_tcg_ops
= {
268 .initialize
= s390x_translate_init
,
269 .tlb_fill
= s390_cpu_tlb_fill
,
271 #if !defined(CONFIG_USER_ONLY)
272 .cpu_exec_interrupt
= s390_cpu_exec_interrupt
,
273 .do_interrupt
= s390_cpu_do_interrupt
,
274 .debug_excp_handler
= s390x_cpu_debug_excp_handler
,
275 .do_unaligned_access
= s390x_cpu_do_unaligned_access
,
276 #endif /* !CONFIG_USER_ONLY */
278 #endif /* CONFIG_TCG */
280 static void s390_cpu_class_init(ObjectClass
*oc
, void *data
)
282 S390CPUClass
*scc
= S390_CPU_CLASS(oc
);
283 CPUClass
*cc
= CPU_CLASS(scc
);
284 DeviceClass
*dc
= DEVICE_CLASS(oc
);
286 device_class_set_parent_realize(dc
, s390_cpu_realizefn
,
287 &scc
->parent_realize
);
288 device_class_set_props(dc
, s390x_cpu_properties
);
289 dc
->user_creatable
= true;
291 device_class_set_parent_reset(dc
, s390_cpu_reset_full
, &scc
->parent_reset
);
293 scc
->reset
= s390_cpu_reset
;
294 cc
->class_by_name
= s390_cpu_class_by_name
,
295 cc
->has_work
= s390_cpu_has_work
;
296 cc
->dump_state
= s390_cpu_dump_state
;
297 cc
->set_pc
= s390_cpu_set_pc
;
298 cc
->gdb_read_register
= s390_cpu_gdb_read_register
;
299 cc
->gdb_write_register
= s390_cpu_gdb_write_register
;
300 #ifndef CONFIG_USER_ONLY
301 s390_cpu_class_init_sysemu(cc
);
303 cc
->disas_set_info
= s390_cpu_disas_set_info
;
304 cc
->gdb_num_core_regs
= S390_NUM_CORE_REGS
;
305 cc
->gdb_core_xml_file
= "s390x-core64.xml";
306 cc
->gdb_arch_name
= s390_gdb_arch_name
;
308 s390_cpu_model_class_register_props(oc
);
311 cc
->tcg_ops
= &s390_tcg_ops
;
312 #endif /* CONFIG_TCG */
315 static const TypeInfo s390_cpu_type_info
= {
316 .name
= TYPE_S390_CPU
,
318 .instance_size
= sizeof(S390CPU
),
319 .instance_align
= __alignof__(S390CPU
),
320 .instance_init
= s390_cpu_initfn
,
322 #ifndef CONFIG_USER_ONLY
323 .instance_finalize
= s390_cpu_finalize
,
324 #endif /* !CONFIG_USER_ONLY */
327 .class_size
= sizeof(S390CPUClass
),
328 .class_init
= s390_cpu_class_init
,
331 static void s390_cpu_register_types(void)
333 type_register_static(&s390_cpu_type_info
);
336 type_init(s390_cpu_register_types
)