]>
Commit | Line | Data |
---|---|---|
29e4bcb2 AF |
1 | /* |
2 | * QEMU S/390 CPU | |
3 | * | |
1ac1a749 AF |
4 | * Copyright (c) 2009 Ulrich Hecht |
5 | * Copyright (c) 2011 Alexander Graf | |
29e4bcb2 | 6 | * Copyright (c) 2012 SUSE LINUX Products GmbH |
70bada03 | 7 | * Copyright (c) 2012 IBM Corp. |
29e4bcb2 AF |
8 | * |
9 | * This library is free software; you can redistribute it and/or | |
10 | * modify it under the terms of the GNU Lesser General Public | |
11 | * License as published by the Free Software Foundation; either | |
12 | * version 2.1 of the License, or (at your option) any later version. | |
13 | * | |
14 | * This library 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 | * Lesser General Public License for more details. | |
18 | * | |
19 | * You should have received a copy of the GNU Lesser General Public | |
20 | * License along with this library; if not, see | |
21 | * <http://www.gnu.org/licenses/lgpl-2.1.html> | |
70bada03 JF |
22 | * Contributions after 2012-12-11 are licensed under the terms of the |
23 | * GNU GPL, version 2 or (at your option) any later version. | |
29e4bcb2 AF |
24 | */ |
25 | ||
9615495a | 26 | #include "qemu/osdep.h" |
564b863d | 27 | #include "cpu.h" |
29e4bcb2 | 28 | #include "qemu-common.h" |
1de7afc9 | 29 | #include "qemu/timer.h" |
eb24f7c6 | 30 | #include "qemu/error-report.h" |
70bada03 | 31 | #include "hw/hw.h" |
eb24f7c6 | 32 | #include "trace.h" |
c7396bbb | 33 | #ifndef CONFIG_USER_ONLY |
904e5fd5 VM |
34 | #include "sysemu/arch_init.h" |
35 | #endif | |
36 | ||
70bada03 JF |
37 | #define CR0_RESET 0xE0UL |
38 | #define CR14_RESET 0xC2000000UL; | |
39 | ||
904e5fd5 VM |
40 | /* generate CPU information for cpu -? */ |
41 | void s390_cpu_list(FILE *f, fprintf_function cpu_fprintf) | |
42 | { | |
43 | #ifdef CONFIG_KVM | |
44 | (*cpu_fprintf)(f, "s390 %16s\n", "host"); | |
45 | #endif | |
46 | } | |
29e4bcb2 | 47 | |
904e5fd5 VM |
48 | #ifndef CONFIG_USER_ONLY |
49 | CpuDefinitionInfoList *arch_query_cpu_definitions(Error **errp) | |
50 | { | |
51 | CpuDefinitionInfoList *entry; | |
52 | CpuDefinitionInfo *info; | |
53 | ||
54 | info = g_malloc0(sizeof(*info)); | |
55 | info->name = g_strdup("host"); | |
56 | ||
57 | entry = g_malloc0(sizeof(*entry)); | |
58 | entry->value = info; | |
59 | ||
60 | return entry; | |
61 | } | |
62 | #endif | |
29e4bcb2 | 63 | |
f45748f1 AF |
64 | static void s390_cpu_set_pc(CPUState *cs, vaddr value) |
65 | { | |
66 | S390CPU *cpu = S390_CPU(cs); | |
67 | ||
68 | cpu->env.psw.addr = value; | |
69 | } | |
70 | ||
8c2e1b00 AF |
71 | static bool s390_cpu_has_work(CPUState *cs) |
72 | { | |
73 | S390CPU *cpu = S390_CPU(cs); | |
74 | CPUS390XState *env = &cpu->env; | |
75 | ||
76 | return (cs->interrupt_request & CPU_INTERRUPT_HARD) && | |
77 | (env->psw.mask & PSW_MASK_EXT); | |
78 | } | |
79 | ||
29c6157c CB |
80 | #if !defined(CONFIG_USER_ONLY) |
81 | /* S390CPUClass::load_normal() */ | |
82 | static void s390_cpu_load_normal(CPUState *s) | |
83 | { | |
84 | S390CPU *cpu = S390_CPU(s); | |
fdfba1a2 | 85 | cpu->env.psw.addr = ldl_phys(s->as, 4) & PSW_MASK_ESA_ADDR; |
29c6157c | 86 | cpu->env.psw.mask = PSW_MASK_32 | PSW_MASK_64; |
eb24f7c6 | 87 | s390_cpu_set_state(CPU_STATE_OPERATING, cpu); |
29c6157c CB |
88 | } |
89 | #endif | |
90 | ||
f5ae2a4f | 91 | /* S390CPUClass::cpu_reset() */ |
29e4bcb2 AF |
92 | static void s390_cpu_reset(CPUState *s) |
93 | { | |
94 | S390CPU *cpu = S390_CPU(s); | |
95 | S390CPUClass *scc = S390_CPU_GET_CLASS(cpu); | |
96 | CPUS390XState *env = &cpu->env; | |
97 | ||
819bd309 | 98 | env->pfault_token = -1UL; |
f5ae2a4f | 99 | scc->parent_reset(s); |
18ff9494 | 100 | cpu->env.sigp_order = 0; |
eb24f7c6 | 101 | s390_cpu_set_state(CPU_STATE_STOPPED, cpu); |
00c8cb0a | 102 | tlb_flush(s, 1); |
f5ae2a4f CB |
103 | } |
104 | ||
105 | /* S390CPUClass::initial_reset() */ | |
106 | static void s390_cpu_initial_reset(CPUState *s) | |
107 | { | |
108 | S390CPU *cpu = S390_CPU(s); | |
109 | CPUS390XState *env = &cpu->env; | |
cc0d079d | 110 | int i; |
f5ae2a4f CB |
111 | |
112 | s390_cpu_reset(s); | |
113 | /* initial reset does not touch regs,fregs and aregs */ | |
f0c3c505 | 114 | memset(&env->fpc, 0, offsetof(CPUS390XState, cpu_num) - |
f5ae2a4f CB |
115 | offsetof(CPUS390XState, fpc)); |
116 | ||
117 | /* architectured initial values for CR 0 and 14 */ | |
118 | env->cregs[0] = CR0_RESET; | |
119 | env->cregs[14] = CR14_RESET; | |
819bd309 | 120 | |
3da0ab35 AJ |
121 | /* architectured initial value for Breaking-Event-Address register */ |
122 | env->gbea = 1; | |
123 | ||
819bd309 | 124 | env->pfault_token = -1UL; |
7107e5a7 | 125 | env->ext_index = -1; |
cc0d079d AJ |
126 | for (i = 0; i < ARRAY_SIZE(env->io_index); i++) { |
127 | env->io_index[i] = -1; | |
128 | } | |
49f5c9e9 | 129 | |
4a33565f AJ |
130 | /* tininess for underflow is detected before rounding */ |
131 | set_float_detect_tininess(float_tininess_before_rounding, | |
132 | &env->fpu_status); | |
133 | ||
49f5c9e9 TH |
134 | /* Reset state inside the kernel that we cannot access yet from QEMU. */ |
135 | if (kvm_enabled()) { | |
99607144 | 136 | kvm_s390_reset_vcpu(cpu); |
49f5c9e9 | 137 | } |
cbed0ba7 | 138 | tlb_flush(s, 1); |
f5ae2a4f CB |
139 | } |
140 | ||
141 | /* CPUClass:reset() */ | |
142 | static void s390_cpu_full_reset(CPUState *s) | |
143 | { | |
144 | S390CPU *cpu = S390_CPU(s); | |
145 | S390CPUClass *scc = S390_CPU_GET_CLASS(cpu); | |
146 | CPUS390XState *env = &cpu->env; | |
cc0d079d | 147 | int i; |
f5ae2a4f | 148 | |
29e4bcb2 | 149 | scc->parent_reset(s); |
18ff9494 | 150 | cpu->env.sigp_order = 0; |
eb24f7c6 | 151 | s390_cpu_set_state(CPU_STATE_STOPPED, cpu); |
29e4bcb2 | 152 | |
f0c3c505 | 153 | memset(env, 0, offsetof(CPUS390XState, cpu_num)); |
70bada03 JF |
154 | |
155 | /* architectured initial values for CR 0 and 14 */ | |
156 | env->cregs[0] = CR0_RESET; | |
157 | env->cregs[14] = CR14_RESET; | |
819bd309 | 158 | |
3da0ab35 AJ |
159 | /* architectured initial value for Breaking-Event-Address register */ |
160 | env->gbea = 1; | |
161 | ||
819bd309 | 162 | env->pfault_token = -1UL; |
7107e5a7 | 163 | env->ext_index = -1; |
cc0d079d AJ |
164 | for (i = 0; i < ARRAY_SIZE(env->io_index); i++) { |
165 | env->io_index[i] = -1; | |
166 | } | |
819bd309 | 167 | |
4a33565f AJ |
168 | /* tininess for underflow is detected before rounding */ |
169 | set_float_detect_tininess(float_tininess_before_rounding, | |
170 | &env->fpu_status); | |
171 | ||
99607144 | 172 | /* Reset state inside the kernel that we cannot access yet from QEMU. */ |
50a2c6e5 PB |
173 | if (kvm_enabled()) { |
174 | kvm_s390_reset_vcpu(cpu); | |
175 | } | |
00c8cb0a | 176 | tlb_flush(s, 1); |
29e4bcb2 AF |
177 | } |
178 | ||
70bada03 JF |
179 | #if !defined(CONFIG_USER_ONLY) |
180 | static void s390_cpu_machine_reset_cb(void *opaque) | |
181 | { | |
182 | S390CPU *cpu = opaque; | |
183 | ||
1fad8b3b | 184 | run_on_cpu(CPU(cpu), s390_do_cpu_full_reset, CPU(cpu)); |
70bada03 JF |
185 | } |
186 | #endif | |
187 | ||
dbad6b74 PC |
188 | static void s390_cpu_disas_set_info(CPUState *cpu, disassemble_info *info) |
189 | { | |
190 | info->mach = bfd_mach_s390_64; | |
191 | info->print_insn = print_insn_s390; | |
192 | } | |
193 | ||
1f136632 AF |
194 | static void s390_cpu_realizefn(DeviceState *dev, Error **errp) |
195 | { | |
14a10fc3 | 196 | CPUState *cs = CPU(dev); |
1f136632 AF |
197 | S390CPUClass *scc = S390_CPU_GET_CLASS(dev); |
198 | ||
73d510c9 | 199 | s390_cpu_gdb_init(cs); |
14a10fc3 | 200 | qemu_init_vcpu(cs); |
159855f0 DH |
201 | #if !defined(CONFIG_USER_ONLY) |
202 | run_on_cpu(cs, s390_do_cpu_full_reset, cs); | |
203 | #else | |
14a10fc3 | 204 | cpu_reset(cs); |
159855f0 | 205 | #endif |
1f136632 AF |
206 | |
207 | scc->parent_realize(dev, errp); | |
208 | } | |
209 | ||
8f22e0df AF |
210 | static void s390_cpu_initfn(Object *obj) |
211 | { | |
c05efcb1 | 212 | CPUState *cs = CPU(obj); |
8f22e0df AF |
213 | S390CPU *cpu = S390_CPU(obj); |
214 | CPUS390XState *env = &cpu->env; | |
2b7ac767 | 215 | static bool inited; |
8f22e0df AF |
216 | static int cpu_num = 0; |
217 | #if !defined(CONFIG_USER_ONLY) | |
218 | struct tm tm; | |
219 | #endif | |
220 | ||
c05efcb1 | 221 | cs->env_ptr = env; |
4bad9e39 | 222 | cpu_exec_init(cs, &error_abort); |
8f22e0df | 223 | #if !defined(CONFIG_USER_ONLY) |
70bada03 | 224 | qemu_register_reset(s390_cpu_machine_reset_cb, cpu); |
8f22e0df AF |
225 | qemu_get_timedate(&tm, 0); |
226 | env->tod_offset = TOD_UNIX_EPOCH + | |
227 | (time2tod(mktimegm(&tm)) * 1000000000ULL); | |
228 | env->tod_basetime = 0; | |
bc72ad67 AB |
229 | env->tod_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, s390x_tod_timer, cpu); |
230 | env->cpu_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, s390x_cpu_timer, cpu); | |
eb24f7c6 | 231 | s390_cpu_set_state(CPU_STATE_STOPPED, cpu); |
8f22e0df AF |
232 | #endif |
233 | env->cpu_num = cpu_num++; | |
2b7ac767 AF |
234 | |
235 | if (tcg_enabled() && !inited) { | |
236 | inited = true; | |
237 | s390x_translate_init(); | |
238 | } | |
8f22e0df AF |
239 | } |
240 | ||
d5627ce8 AF |
241 | static void s390_cpu_finalize(Object *obj) |
242 | { | |
243 | #if !defined(CONFIG_USER_ONLY) | |
244 | S390CPU *cpu = S390_CPU(obj); | |
245 | ||
246 | qemu_unregister_reset(s390_cpu_machine_reset_cb, cpu); | |
3cda44f7 | 247 | g_free(cpu->irqstate); |
d5627ce8 AF |
248 | #endif |
249 | } | |
250 | ||
75973bfe | 251 | #if !defined(CONFIG_USER_ONLY) |
eb24f7c6 DH |
252 | static bool disabled_wait(CPUState *cpu) |
253 | { | |
254 | return cpu->halted && !(S390_CPU(cpu)->env.psw.mask & | |
255 | (PSW_MASK_IO | PSW_MASK_EXT | PSW_MASK_MCHECK)); | |
256 | } | |
257 | ||
75973bfe DH |
258 | static unsigned s390_count_running_cpus(void) |
259 | { | |
260 | CPUState *cpu; | |
261 | int nr_running = 0; | |
262 | ||
263 | CPU_FOREACH(cpu) { | |
264 | uint8_t state = S390_CPU(cpu)->env.cpu_state; | |
265 | if (state == CPU_STATE_OPERATING || | |
266 | state == CPU_STATE_LOAD) { | |
eb24f7c6 DH |
267 | if (!disabled_wait(cpu)) { |
268 | nr_running++; | |
269 | } | |
75973bfe DH |
270 | } |
271 | } | |
272 | ||
273 | return nr_running; | |
274 | } | |
275 | ||
eb24f7c6 | 276 | unsigned int s390_cpu_halt(S390CPU *cpu) |
75973bfe DH |
277 | { |
278 | CPUState *cs = CPU(cpu); | |
eb24f7c6 | 279 | trace_cpu_halt(cs->cpu_index); |
75973bfe | 280 | |
eb24f7c6 DH |
281 | if (!cs->halted) { |
282 | cs->halted = 1; | |
283 | cs->exception_index = EXCP_HLT; | |
75973bfe | 284 | } |
eb24f7c6 DH |
285 | |
286 | return s390_count_running_cpus(); | |
75973bfe DH |
287 | } |
288 | ||
eb24f7c6 | 289 | void s390_cpu_unhalt(S390CPU *cpu) |
75973bfe DH |
290 | { |
291 | CPUState *cs = CPU(cpu); | |
eb24f7c6 | 292 | trace_cpu_unhalt(cs->cpu_index); |
75973bfe | 293 | |
eb24f7c6 DH |
294 | if (cs->halted) { |
295 | cs->halted = 0; | |
296 | cs->exception_index = -1; | |
297 | } | |
298 | } | |
299 | ||
300 | unsigned int s390_cpu_set_state(uint8_t cpu_state, S390CPU *cpu) | |
301 | { | |
302 | trace_cpu_set_state(CPU(cpu)->cpu_index, cpu_state); | |
303 | ||
304 | switch (cpu_state) { | |
305 | case CPU_STATE_STOPPED: | |
306 | case CPU_STATE_CHECK_STOP: | |
307 | /* halt the cpu for common infrastructure */ | |
308 | s390_cpu_halt(cpu); | |
309 | break; | |
310 | case CPU_STATE_OPERATING: | |
311 | case CPU_STATE_LOAD: | |
312 | /* unhalt the cpu for common infrastructure */ | |
313 | s390_cpu_unhalt(cpu); | |
314 | break; | |
315 | default: | |
316 | error_report("Requested CPU state is not a valid S390 CPU state: %u", | |
317 | cpu_state); | |
318 | exit(1); | |
75973bfe | 319 | } |
c9e659c9 DH |
320 | if (kvm_enabled() && cpu->env.cpu_state != cpu_state) { |
321 | kvm_s390_set_cpu_state(cpu, cpu_state); | |
322 | } | |
eb24f7c6 | 323 | cpu->env.cpu_state = cpu_state; |
75973bfe DH |
324 | |
325 | return s390_count_running_cpus(); | |
326 | } | |
327 | #endif | |
328 | ||
b3820e6c DH |
329 | static gchar *s390_gdb_arch_name(CPUState *cs) |
330 | { | |
331 | return g_strdup("s390:64-bit"); | |
332 | } | |
333 | ||
29e4bcb2 AF |
334 | static void s390_cpu_class_init(ObjectClass *oc, void *data) |
335 | { | |
336 | S390CPUClass *scc = S390_CPU_CLASS(oc); | |
337 | CPUClass *cc = CPU_CLASS(scc); | |
c7396bbb | 338 | DeviceClass *dc = DEVICE_CLASS(oc); |
29e4bcb2 | 339 | |
1f136632 AF |
340 | scc->parent_realize = dc->realize; |
341 | dc->realize = s390_cpu_realizefn; | |
342 | ||
29e4bcb2 | 343 | scc->parent_reset = cc->reset; |
29c6157c CB |
344 | #if !defined(CONFIG_USER_ONLY) |
345 | scc->load_normal = s390_cpu_load_normal; | |
346 | #endif | |
f5ae2a4f CB |
347 | scc->cpu_reset = s390_cpu_reset; |
348 | scc->initial_cpu_reset = s390_cpu_initial_reset; | |
349 | cc->reset = s390_cpu_full_reset; | |
8c2e1b00 | 350 | cc->has_work = s390_cpu_has_work; |
97a8ea5a | 351 | cc->do_interrupt = s390_cpu_do_interrupt; |
878096ee | 352 | cc->dump_state = s390_cpu_dump_state; |
f45748f1 | 353 | cc->set_pc = s390_cpu_set_pc; |
5b50e790 AF |
354 | cc->gdb_read_register = s390_cpu_gdb_read_register; |
355 | cc->gdb_write_register = s390_cpu_gdb_write_register; | |
7510454e AF |
356 | #ifdef CONFIG_USER_ONLY |
357 | cc->handle_mmu_fault = s390_cpu_handle_mmu_fault; | |
358 | #else | |
00b941e5 | 359 | cc->get_phys_page_debug = s390_cpu_get_phys_page_debug; |
ef1df130 | 360 | cc->vmsd = &vmstate_s390_cpu; |
9b4f38e1 | 361 | cc->write_elf64_note = s390_cpu_write_elf64_note; |
02bb9bbf | 362 | cc->cpu_exec_interrupt = s390_cpu_exec_interrupt; |
311918b9 | 363 | cc->debug_excp_handler = s390x_cpu_debug_excp_handler; |
00b941e5 | 364 | #endif |
dbad6b74 PC |
365 | cc->disas_set_info = s390_cpu_disas_set_info; |
366 | ||
73d510c9 DH |
367 | cc->gdb_num_core_regs = S390_NUM_CORE_REGS; |
368 | cc->gdb_core_xml_file = "s390x-core64.xml"; | |
b3820e6c | 369 | cc->gdb_arch_name = s390_gdb_arch_name; |
4c315c27 MA |
370 | |
371 | /* | |
372 | * Reason: s390_cpu_initfn() calls cpu_exec_init(), which saves | |
373 | * the object in cpus -> dangling pointer after final | |
374 | * object_unref(). | |
375 | */ | |
376 | dc->cannot_destroy_with_object_finalize_yet = true; | |
29e4bcb2 AF |
377 | } |
378 | ||
379 | static const TypeInfo s390_cpu_type_info = { | |
380 | .name = TYPE_S390_CPU, | |
381 | .parent = TYPE_CPU, | |
382 | .instance_size = sizeof(S390CPU), | |
8f22e0df | 383 | .instance_init = s390_cpu_initfn, |
d5627ce8 | 384 | .instance_finalize = s390_cpu_finalize, |
29e4bcb2 AF |
385 | .abstract = false, |
386 | .class_size = sizeof(S390CPUClass), | |
387 | .class_init = s390_cpu_class_init, | |
388 | }; | |
389 | ||
390 | static void s390_cpu_register_types(void) | |
391 | { | |
392 | type_register_static(&s390_cpu_type_info); | |
393 | } | |
394 | ||
395 | type_init(s390_cpu_register_types) |