]> git.proxmox.com Git - mirror_qemu.git/blob - target/s390x/cpu.c
target/s390x: move sysemu-only code out to cpu-sysemu.c
[mirror_qemu.git] / target / s390x / cpu.c
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_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
38 #define CR0_RESET 0xE0UL
39 #define CR14_RESET 0xC2000000UL;
40
41 static void s390_cpu_set_pc(CPUState *cs, vaddr value)
42 {
43 S390CPU *cpu = S390_CPU(cs);
44
45 cpu->env.psw.addr = value;
46 }
47
48 static bool s390_cpu_has_work(CPUState *cs)
49 {
50 S390CPU *cpu = S390_CPU(cs);
51
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) {
55 return false;
56 }
57
58 if (!(cs->interrupt_request & CPU_INTERRUPT_HARD)) {
59 return false;
60 }
61
62 return s390_cpu_has_int(cpu);
63 }
64
65 /* S390CPUClass::reset() */
66 static void s390_cpu_reset(CPUState *s, cpu_reset_type type)
67 {
68 S390CPU *cpu = S390_CPU(s);
69 S390CPUClass *scc = S390_CPU_GET_CLASS(cpu);
70 CPUS390XState *env = &cpu->env;
71 DeviceState *dev = DEVICE(s);
72
73 scc->parent_reset(dev);
74 cpu->env.sigp_order = 0;
75 s390_cpu_set_state(S390_CPU_STATE_STOPPED, cpu);
76
77 switch (type) {
78 case S390_CPU_RESET_CLEAR:
79 memset(env, 0, offsetof(CPUS390XState, start_initial_reset_fields));
80 /* fall through */
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));
86
87 /* architectured initial value for Breaking-Event-Address register */
88 env->gbea = 1;
89
90 /* architectured initial values for CR 0 and 14 */
91 env->cregs[0] = CR0_RESET;
92 env->cregs[14] = CR14_RESET;
93
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;
99 }
100 #endif
101
102 /* tininess for underflow is detected before rounding */
103 set_float_detect_tininess(float_tininess_before_rounding,
104 &env->fpu_status);
105 /* fall through */
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));
111
112 env->pfault_token = -1UL;
113 env->bpbc = false;
114 break;
115 default:
116 g_assert_not_reached();
117 }
118
119 /* Reset state inside the kernel that we cannot access yet from QEMU. */
120 if (kvm_enabled()) {
121 switch (type) {
122 case S390_CPU_RESET_CLEAR:
123 kvm_s390_reset_vcpu_clear(cpu);
124 break;
125 case S390_CPU_RESET_INITIAL:
126 kvm_s390_reset_vcpu_initial(cpu);
127 break;
128 case S390_CPU_RESET_NORMAL:
129 kvm_s390_reset_vcpu_normal(cpu);
130 break;
131 }
132 }
133 }
134
135 static void s390_cpu_disas_set_info(CPUState *cpu, disassemble_info *info)
136 {
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;
142 }
143
144 static void s390_cpu_realizefn(DeviceState *dev, Error **errp)
145 {
146 CPUState *cs = CPU(dev);
147 S390CPUClass *scc = S390_CPU_GET_CLASS(dev);
148 Error *err = NULL;
149
150 /* the model has to be realized before qemu_init_vcpu() due to kvm */
151 s390_realize_cpu_model(cs, &err);
152 if (err) {
153 goto out;
154 }
155
156 #if !defined(CONFIG_USER_ONLY)
157 if (!s390_cpu_realize_sysemu(dev, &err)) {
158 goto out;
159 }
160 #endif
161
162 cpu_exec_realizefn(cs, &err);
163 if (err != NULL) {
164 goto out;
165 }
166
167 #if !defined(CONFIG_USER_ONLY)
168 qemu_register_reset(s390_cpu_machine_reset_cb, S390_CPU(dev));
169 #endif
170 s390_cpu_gdb_init(cs);
171 qemu_init_vcpu(cs);
172
173 /*
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.
178 */
179 if (kvm_enabled()) {
180 run_on_cpu(cs, s390_do_cpu_full_reset, RUN_ON_CPU_NULL);
181 } else {
182 cpu_reset(cs);
183 }
184
185 scc->parent_realize(dev, &err);
186 out:
187 error_propagate(errp, err);
188 }
189
190 static void s390_cpu_initfn(Object *obj)
191 {
192 CPUState *cs = CPU(obj);
193 S390CPU *cpu = S390_CPU(obj);
194
195 cpu_set_cpustate_pointers(cpu);
196 cs->exception_index = EXCP_HLT;
197
198 #if !defined(CONFIG_USER_ONLY)
199 s390_cpu_init_sysemu(obj);
200 #endif
201 }
202
203 static gchar *s390_gdb_arch_name(CPUState *cs)
204 {
205 return g_strdup("s390:64-bit");
206 }
207
208 static Property s390x_cpu_properties[] = {
209 #if !defined(CONFIG_USER_ONLY)
210 DEFINE_PROP_UINT32("core-id", S390CPU, env.core_id, 0),
211 #endif
212 DEFINE_PROP_END_OF_LIST()
213 };
214
215 static void s390_cpu_reset_full(DeviceState *dev)
216 {
217 CPUState *s = CPU(dev);
218 return s390_cpu_reset(s, S390_CPU_RESET_CLEAR);
219 }
220
221 #ifdef CONFIG_TCG
222 #include "hw/core/tcg-cpu-ops.h"
223
224 static const struct TCGCPUOps s390_tcg_ops = {
225 .initialize = s390x_translate_init,
226 .tlb_fill = s390_cpu_tlb_fill,
227
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 */
234 };
235 #endif /* CONFIG_TCG */
236
237 static void s390_cpu_class_init(ObjectClass *oc, void *data)
238 {
239 S390CPUClass *scc = S390_CPU_CLASS(oc);
240 CPUClass *cc = CPU_CLASS(scc);
241 DeviceClass *dc = DEVICE_CLASS(oc);
242
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;
247
248 device_class_set_parent_reset(dc, s390_cpu_reset_full, &scc->parent_reset);
249
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);
259 #endif
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;
264
265 s390_cpu_model_class_register_props(oc);
266
267 #ifdef CONFIG_TCG
268 cc->tcg_ops = &s390_tcg_ops;
269 #endif /* CONFIG_TCG */
270 }
271
272 static const TypeInfo s390_cpu_type_info = {
273 .name = TYPE_S390_CPU,
274 .parent = TYPE_CPU,
275 .instance_size = sizeof(S390CPU),
276 .instance_align = __alignof__(S390CPU),
277 .instance_init = s390_cpu_initfn,
278
279 #ifndef CONFIG_USER_ONLY
280 .instance_finalize = s390_cpu_finalize,
281 #endif /* !CONFIG_USER_ONLY */
282
283 .abstract = true,
284 .class_size = sizeof(S390CPUClass),
285 .class_init = s390_cpu_class_init,
286 };
287
288 static void s390_cpu_register_types(void)
289 {
290 type_register_static(&s390_cpu_type_info);
291 }
292
293 type_init(s390_cpu_register_types)