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_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"
38 #define CR0_RESET 0xE0UL
39 #define CR14_RESET 0xC2000000UL;
41 static void s390_cpu_set_pc(CPUState
*cs
, vaddr value
)
43 S390CPU
*cpu
= S390_CPU(cs
);
45 cpu
->env
.psw
.addr
= value
;
48 static bool s390_cpu_has_work(CPUState
*cs
)
50 S390CPU
*cpu
= S390_CPU(cs
);
52 /* STOPPED cpus can never wake up */
53 if (s390_cpu_get_state(cpu
) != S390_CPU_STATE_LOAD
&&
54 s390_cpu_get_state(cpu
) != S390_CPU_STATE_OPERATING
) {
58 if (!(cs
->interrupt_request
& CPU_INTERRUPT_HARD
)) {
62 return s390_cpu_has_int(cpu
);
65 /* S390CPUClass::reset() */
66 static void s390_cpu_reset(CPUState
*s
, cpu_reset_type type
)
68 S390CPU
*cpu
= S390_CPU(s
);
69 S390CPUClass
*scc
= S390_CPU_GET_CLASS(cpu
);
70 CPUS390XState
*env
= &cpu
->env
;
71 DeviceState
*dev
= DEVICE(s
);
73 scc
->parent_reset(dev
);
74 cpu
->env
.sigp_order
= 0;
75 s390_cpu_set_state(S390_CPU_STATE_STOPPED
, cpu
);
78 case S390_CPU_RESET_CLEAR
:
79 memset(env
, 0, offsetof(CPUS390XState
, start_initial_reset_fields
));
81 case S390_CPU_RESET_INITIAL
:
82 /* initial reset does not clear everything! */
83 memset(&env
->start_initial_reset_fields
, 0,
84 offsetof(CPUS390XState
, start_normal_reset_fields
) -
85 offsetof(CPUS390XState
, start_initial_reset_fields
));
87 /* architectured initial value for Breaking-Event-Address register */
90 /* architectured initial values for CR 0 and 14 */
91 env
->cregs
[0] = CR0_RESET
;
92 env
->cregs
[14] = CR14_RESET
;
94 #if defined(CONFIG_USER_ONLY)
95 /* user mode should always be allowed to use the full FPU */
96 env
->cregs
[0] |= CR0_AFP
;
97 if (s390_has_feat(S390_FEAT_VECTOR
)) {
98 env
->cregs
[0] |= CR0_VECTOR
;
102 /* tininess for underflow is detected before rounding */
103 set_float_detect_tininess(float_tininess_before_rounding
,
106 case S390_CPU_RESET_NORMAL
:
107 env
->psw
.mask
&= ~PSW_MASK_RI
;
108 memset(&env
->start_normal_reset_fields
, 0,
109 offsetof(CPUS390XState
, end_reset_fields
) -
110 offsetof(CPUS390XState
, start_normal_reset_fields
));
112 env
->pfault_token
= -1UL;
116 g_assert_not_reached();
119 /* Reset state inside the kernel that we cannot access yet from QEMU. */
122 case S390_CPU_RESET_CLEAR
:
123 kvm_s390_reset_vcpu_clear(cpu
);
125 case S390_CPU_RESET_INITIAL
:
126 kvm_s390_reset_vcpu_initial(cpu
);
128 case S390_CPU_RESET_NORMAL
:
129 kvm_s390_reset_vcpu_normal(cpu
);
135 static void s390_cpu_disas_set_info(CPUState
*cpu
, disassemble_info
*info
)
137 info
->mach
= bfd_mach_s390_64
;
138 info
->print_insn
= print_insn_s390
;
139 info
->cap_arch
= CS_ARCH_SYSZ
;
140 info
->cap_insn_unit
= 2;
141 info
->cap_insn_split
= 6;
144 static void s390_cpu_realizefn(DeviceState
*dev
, Error
**errp
)
146 CPUState
*cs
= CPU(dev
);
147 S390CPUClass
*scc
= S390_CPU_GET_CLASS(dev
);
150 /* the model has to be realized before qemu_init_vcpu() due to kvm */
151 s390_realize_cpu_model(cs
, &err
);
156 #if !defined(CONFIG_USER_ONLY)
157 if (!s390_cpu_realize_sysemu(dev
, &err
)) {
162 cpu_exec_realizefn(cs
, &err
);
167 #if !defined(CONFIG_USER_ONLY)
168 qemu_register_reset(s390_cpu_machine_reset_cb
, S390_CPU(dev
));
170 s390_cpu_gdb_init(cs
);
174 * KVM requires the initial CPU reset ioctl to be executed on the target
175 * CPU thread. CPU hotplug under single-threaded TCG will not work with
176 * run_on_cpu(), as run_on_cpu() will not work properly if called while
177 * the main thread is already running but the CPU hasn't been realized.
180 run_on_cpu(cs
, s390_do_cpu_full_reset
, RUN_ON_CPU_NULL
);
185 scc
->parent_realize(dev
, &err
);
187 error_propagate(errp
, err
);
190 static void s390_cpu_initfn(Object
*obj
)
192 CPUState
*cs
= CPU(obj
);
193 S390CPU
*cpu
= S390_CPU(obj
);
195 cpu_set_cpustate_pointers(cpu
);
196 cs
->exception_index
= EXCP_HLT
;
198 #if !defined(CONFIG_USER_ONLY)
199 s390_cpu_init_sysemu(obj
);
203 static gchar
*s390_gdb_arch_name(CPUState
*cs
)
205 return g_strdup("s390:64-bit");
208 static Property s390x_cpu_properties
[] = {
209 #if !defined(CONFIG_USER_ONLY)
210 DEFINE_PROP_UINT32("core-id", S390CPU
, env
.core_id
, 0),
212 DEFINE_PROP_END_OF_LIST()
215 static void s390_cpu_reset_full(DeviceState
*dev
)
217 CPUState
*s
= CPU(dev
);
218 return s390_cpu_reset(s
, S390_CPU_RESET_CLEAR
);
222 #include "hw/core/tcg-cpu-ops.h"
224 static const struct TCGCPUOps s390_tcg_ops
= {
225 .initialize
= s390x_translate_init
,
226 .tlb_fill
= s390_cpu_tlb_fill
,
228 #if !defined(CONFIG_USER_ONLY)
229 .cpu_exec_interrupt
= s390_cpu_exec_interrupt
,
230 .do_interrupt
= s390_cpu_do_interrupt
,
231 .debug_excp_handler
= s390x_cpu_debug_excp_handler
,
232 .do_unaligned_access
= s390x_cpu_do_unaligned_access
,
233 #endif /* !CONFIG_USER_ONLY */
235 #endif /* CONFIG_TCG */
237 static void s390_cpu_class_init(ObjectClass
*oc
, void *data
)
239 S390CPUClass
*scc
= S390_CPU_CLASS(oc
);
240 CPUClass
*cc
= CPU_CLASS(scc
);
241 DeviceClass
*dc
= DEVICE_CLASS(oc
);
243 device_class_set_parent_realize(dc
, s390_cpu_realizefn
,
244 &scc
->parent_realize
);
245 device_class_set_props(dc
, s390x_cpu_properties
);
246 dc
->user_creatable
= true;
248 device_class_set_parent_reset(dc
, s390_cpu_reset_full
, &scc
->parent_reset
);
250 scc
->reset
= s390_cpu_reset
;
251 cc
->class_by_name
= s390_cpu_class_by_name
,
252 cc
->has_work
= s390_cpu_has_work
;
253 cc
->dump_state
= s390_cpu_dump_state
;
254 cc
->set_pc
= s390_cpu_set_pc
;
255 cc
->gdb_read_register
= s390_cpu_gdb_read_register
;
256 cc
->gdb_write_register
= s390_cpu_gdb_write_register
;
257 #ifndef CONFIG_USER_ONLY
258 s390_cpu_class_init_sysemu(cc
);
260 cc
->disas_set_info
= s390_cpu_disas_set_info
;
261 cc
->gdb_num_core_regs
= S390_NUM_CORE_REGS
;
262 cc
->gdb_core_xml_file
= "s390x-core64.xml";
263 cc
->gdb_arch_name
= s390_gdb_arch_name
;
265 s390_cpu_model_class_register_props(oc
);
268 cc
->tcg_ops
= &s390_tcg_ops
;
269 #endif /* CONFIG_TCG */
272 static const TypeInfo s390_cpu_type_info
= {
273 .name
= TYPE_S390_CPU
,
275 .instance_size
= sizeof(S390CPU
),
276 .instance_align
= __alignof__(S390CPU
),
277 .instance_init
= s390_cpu_initfn
,
279 #ifndef CONFIG_USER_ONLY
280 .instance_finalize
= s390_cpu_finalize
,
281 #endif /* !CONFIG_USER_ONLY */
284 .class_size
= sizeof(S390CPUClass
),
285 .class_init
= s390_cpu_class_init
,
288 static void s390_cpu_register_types(void)
290 type_register_static(&s390_cpu_type_info
);
293 type_init(s390_cpu_register_types
)