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 "qemu/module.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"
41 #include "hw/s390x/cpu-topology.h"
43 #define CR0_RESET 0xE0UL
44 #define CR14_RESET 0xC2000000UL;
46 #ifndef CONFIG_USER_ONLY
47 static bool is_early_exception_psw(uint64_t mask
, uint64_t addr
)
49 if (mask
& PSW_MASK_RESERVED
) {
53 switch (mask
& (PSW_MASK_32
| PSW_MASK_64
)) {
55 return addr
& ~0xffffffULL
;
57 return addr
& ~0x7fffffffULL
;
58 case PSW_MASK_32
| PSW_MASK_64
:
60 default: /* PSW_MASK_64 */
66 void s390_cpu_set_psw(CPUS390XState
*env
, uint64_t mask
, uint64_t addr
)
68 #ifndef CONFIG_USER_ONLY
69 uint64_t old_mask
= env
->psw
.mask
;
75 /* KVM will handle all WAITs and trigger a WAIT exit on disabled_wait */
79 env
->cc_op
= (mask
>> 44) & 3;
81 #ifndef CONFIG_USER_ONLY
82 if (is_early_exception_psw(mask
, addr
)) {
83 env
->int_pgm_ilen
= 0;
84 trigger_pgm_exception(env
, PGM_SPECIFICATION
);
88 if ((old_mask
^ mask
) & PSW_MASK_PER
) {
89 s390_cpu_recompute_watchpoints(env_cpu(env
));
92 if (mask
& PSW_MASK_WAIT
) {
93 s390_handle_wait(env_archcpu(env
));
98 uint64_t s390_cpu_get_psw_mask(CPUS390XState
*env
)
100 uint64_t r
= env
->psw
.mask
;
103 uint64_t cc
= calc_cc(env
, env
->cc_op
, env
->cc_src
,
104 env
->cc_dst
, env
->cc_vr
);
114 static void s390_cpu_set_pc(CPUState
*cs
, vaddr value
)
116 S390CPU
*cpu
= S390_CPU(cs
);
118 cpu
->env
.psw
.addr
= value
;
121 static vaddr
s390_cpu_get_pc(CPUState
*cs
)
123 S390CPU
*cpu
= S390_CPU(cs
);
125 return cpu
->env
.psw
.addr
;
128 static bool s390_cpu_has_work(CPUState
*cs
)
130 S390CPU
*cpu
= S390_CPU(cs
);
132 /* STOPPED cpus can never wake up */
133 if (s390_cpu_get_state(cpu
) != S390_CPU_STATE_LOAD
&&
134 s390_cpu_get_state(cpu
) != S390_CPU_STATE_OPERATING
) {
138 if (!(cs
->interrupt_request
& CPU_INTERRUPT_HARD
)) {
142 return s390_cpu_has_int(cpu
);
145 static void s390_query_cpu_fast(CPUState
*cpu
, CpuInfoFast
*value
)
147 S390CPU
*s390_cpu
= S390_CPU(cpu
);
149 value
->u
.s390x
.cpu_state
= s390_cpu
->env
.cpu_state
;
150 #if !defined(CONFIG_USER_ONLY)
151 if (s390_has_topology()) {
152 value
->u
.s390x
.has_dedicated
= true;
153 value
->u
.s390x
.dedicated
= s390_cpu
->env
.dedicated
;
154 value
->u
.s390x
.has_entitlement
= true;
155 value
->u
.s390x
.entitlement
= s390_cpu
->env
.entitlement
;
160 /* S390CPUClass::reset() */
161 static void s390_cpu_reset(CPUState
*s
, cpu_reset_type type
)
163 S390CPU
*cpu
= S390_CPU(s
);
164 S390CPUClass
*scc
= S390_CPU_GET_CLASS(cpu
);
165 CPUS390XState
*env
= &cpu
->env
;
166 DeviceState
*dev
= DEVICE(s
);
168 scc
->parent_reset(dev
);
169 cpu
->env
.sigp_order
= 0;
170 s390_cpu_set_state(S390_CPU_STATE_STOPPED
, cpu
);
173 case S390_CPU_RESET_CLEAR
:
174 memset(env
, 0, offsetof(CPUS390XState
, start_initial_reset_fields
));
176 case S390_CPU_RESET_INITIAL
:
177 /* initial reset does not clear everything! */
178 memset(&env
->start_initial_reset_fields
, 0,
179 offsetof(CPUS390XState
, start_normal_reset_fields
) -
180 offsetof(CPUS390XState
, start_initial_reset_fields
));
182 /* architectured initial value for Breaking-Event-Address register */
185 /* architectured initial values for CR 0 and 14 */
186 env
->cregs
[0] = CR0_RESET
;
187 env
->cregs
[14] = CR14_RESET
;
189 #if defined(CONFIG_USER_ONLY)
190 /* user mode should always be allowed to use the full FPU */
191 env
->cregs
[0] |= CR0_AFP
;
192 if (s390_has_feat(S390_FEAT_VECTOR
)) {
193 env
->cregs
[0] |= CR0_VECTOR
;
197 /* tininess for underflow is detected before rounding */
198 set_float_detect_tininess(float_tininess_before_rounding
,
201 case S390_CPU_RESET_NORMAL
:
202 env
->psw
.mask
&= ~PSW_MASK_RI
;
203 memset(&env
->start_normal_reset_fields
, 0,
204 offsetof(CPUS390XState
, end_reset_fields
) -
205 offsetof(CPUS390XState
, start_normal_reset_fields
));
207 env
->pfault_token
= -1UL;
211 g_assert_not_reached();
214 /* Reset state inside the kernel that we cannot access yet from QEMU. */
217 case S390_CPU_RESET_CLEAR
:
218 kvm_s390_reset_vcpu_clear(cpu
);
220 case S390_CPU_RESET_INITIAL
:
221 kvm_s390_reset_vcpu_initial(cpu
);
223 case S390_CPU_RESET_NORMAL
:
224 kvm_s390_reset_vcpu_normal(cpu
);
230 static void s390_cpu_disas_set_info(CPUState
*cpu
, disassemble_info
*info
)
232 info
->mach
= bfd_mach_s390_64
;
233 info
->cap_arch
= CS_ARCH_SYSZ
;
234 info
->cap_insn_unit
= 2;
235 info
->cap_insn_split
= 6;
238 static void s390_cpu_realizefn(DeviceState
*dev
, Error
**errp
)
240 CPUState
*cs
= CPU(dev
);
241 S390CPUClass
*scc
= S390_CPU_GET_CLASS(dev
);
244 /* the model has to be realized before qemu_init_vcpu() due to kvm */
245 s390_realize_cpu_model(cs
, &err
);
250 #if !defined(CONFIG_USER_ONLY)
251 if (!s390_cpu_realize_sysemu(dev
, &err
)) {
256 cpu_exec_realizefn(cs
, &err
);
261 #if !defined(CONFIG_USER_ONLY)
262 qemu_register_reset(s390_cpu_machine_reset_cb
, S390_CPU(dev
));
264 s390_cpu_gdb_init(cs
);
268 * KVM requires the initial CPU reset ioctl to be executed on the target
269 * CPU thread. CPU hotplug under single-threaded TCG will not work with
270 * run_on_cpu(), as run_on_cpu() will not work properly if called while
271 * the main thread is already running but the CPU hasn't been realized.
274 run_on_cpu(cs
, s390_do_cpu_full_reset
, RUN_ON_CPU_NULL
);
279 scc
->parent_realize(dev
, &err
);
281 error_propagate(errp
, err
);
284 static void s390_cpu_initfn(Object
*obj
)
286 CPUState
*cs
= CPU(obj
);
288 cs
->exception_index
= EXCP_HLT
;
290 #if !defined(CONFIG_USER_ONLY)
291 s390_cpu_init_sysemu(obj
);
295 static const gchar
*s390_gdb_arch_name(CPUState
*cs
)
297 return "s390:64-bit";
300 static Property s390x_cpu_properties
[] = {
301 #if !defined(CONFIG_USER_ONLY)
302 DEFINE_PROP_UINT32("core-id", S390CPU
, env
.core_id
, 0),
303 DEFINE_PROP_INT32("socket-id", S390CPU
, env
.socket_id
, -1),
304 DEFINE_PROP_INT32("book-id", S390CPU
, env
.book_id
, -1),
305 DEFINE_PROP_INT32("drawer-id", S390CPU
, env
.drawer_id
, -1),
306 DEFINE_PROP_BOOL("dedicated", S390CPU
, env
.dedicated
, false),
307 DEFINE_PROP_CPUS390ENTITLEMENT("entitlement", S390CPU
, env
.entitlement
,
308 S390_CPU_ENTITLEMENT_AUTO
),
310 DEFINE_PROP_END_OF_LIST()
313 static void s390_cpu_reset_full(DeviceState
*dev
)
315 CPUState
*s
= CPU(dev
);
316 return s390_cpu_reset(s
, S390_CPU_RESET_CLEAR
);
320 #include "hw/core/tcg-cpu-ops.h"
322 static const struct TCGCPUOps s390_tcg_ops
= {
323 .initialize
= s390x_translate_init
,
324 .restore_state_to_opc
= s390x_restore_state_to_opc
,
326 #ifdef CONFIG_USER_ONLY
327 .record_sigsegv
= s390_cpu_record_sigsegv
,
328 .record_sigbus
= s390_cpu_record_sigbus
,
330 .tlb_fill
= s390_cpu_tlb_fill
,
331 .cpu_exec_interrupt
= s390_cpu_exec_interrupt
,
332 .do_interrupt
= s390_cpu_do_interrupt
,
333 .debug_excp_handler
= s390x_cpu_debug_excp_handler
,
334 .do_unaligned_access
= s390x_cpu_do_unaligned_access
,
335 #endif /* !CONFIG_USER_ONLY */
337 #endif /* CONFIG_TCG */
339 static void s390_cpu_class_init(ObjectClass
*oc
, void *data
)
341 S390CPUClass
*scc
= S390_CPU_CLASS(oc
);
342 CPUClass
*cc
= CPU_CLASS(scc
);
343 DeviceClass
*dc
= DEVICE_CLASS(oc
);
345 device_class_set_parent_realize(dc
, s390_cpu_realizefn
,
346 &scc
->parent_realize
);
347 device_class_set_props(dc
, s390x_cpu_properties
);
348 dc
->user_creatable
= true;
350 device_class_set_parent_reset(dc
, s390_cpu_reset_full
, &scc
->parent_reset
);
352 scc
->reset
= s390_cpu_reset
;
353 cc
->class_by_name
= s390_cpu_class_by_name
,
354 cc
->has_work
= s390_cpu_has_work
;
355 cc
->dump_state
= s390_cpu_dump_state
;
356 cc
->query_cpu_fast
= s390_query_cpu_fast
;
357 cc
->set_pc
= s390_cpu_set_pc
;
358 cc
->get_pc
= s390_cpu_get_pc
;
359 cc
->gdb_read_register
= s390_cpu_gdb_read_register
;
360 cc
->gdb_write_register
= s390_cpu_gdb_write_register
;
361 #ifndef CONFIG_USER_ONLY
362 s390_cpu_class_init_sysemu(cc
);
364 cc
->disas_set_info
= s390_cpu_disas_set_info
;
365 cc
->gdb_num_core_regs
= S390_NUM_CORE_REGS
;
366 cc
->gdb_core_xml_file
= "s390x-core64.xml";
367 cc
->gdb_arch_name
= s390_gdb_arch_name
;
369 s390_cpu_model_class_register_props(oc
);
372 cc
->tcg_ops
= &s390_tcg_ops
;
373 #endif /* CONFIG_TCG */
376 static const TypeInfo s390_cpu_type_info
= {
377 .name
= TYPE_S390_CPU
,
379 .instance_size
= sizeof(S390CPU
),
380 .instance_align
= __alignof__(S390CPU
),
381 .instance_init
= s390_cpu_initfn
,
383 #ifndef CONFIG_USER_ONLY
384 .instance_finalize
= s390_cpu_finalize
,
385 #endif /* !CONFIG_USER_ONLY */
388 .class_size
= sizeof(S390CPUClass
),
389 .class_init
= s390_cpu_class_init
,
392 static void s390_cpu_register_types(void)
394 type_register_static(&s390_cpu_type_info
);
397 type_init(s390_cpu_register_types
)