]> git.proxmox.com Git - mirror_qemu.git/blame - target-s390x/cpu.c
s390x/kvm: proper use of the cpu states OPERATING and STOPPED
[mirror_qemu.git] / target-s390x / cpu.c
CommitLineData
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
564b863d 26#include "cpu.h"
29e4bcb2 27#include "qemu-common.h"
1de7afc9 28#include "qemu/timer.h"
eb24f7c6 29#include "qemu/error-report.h"
70bada03 30#include "hw/hw.h"
eb24f7c6 31#include "trace.h"
c7396bbb 32#ifndef CONFIG_USER_ONLY
904e5fd5
VM
33#include "sysemu/arch_init.h"
34#endif
35
70bada03
JF
36#define CR0_RESET 0xE0UL
37#define CR14_RESET 0xC2000000UL;
38
904e5fd5
VM
39/* generate CPU information for cpu -? */
40void s390_cpu_list(FILE *f, fprintf_function cpu_fprintf)
41{
42#ifdef CONFIG_KVM
43 (*cpu_fprintf)(f, "s390 %16s\n", "host");
44#endif
45}
29e4bcb2 46
904e5fd5
VM
47#ifndef CONFIG_USER_ONLY
48CpuDefinitionInfoList *arch_query_cpu_definitions(Error **errp)
49{
50 CpuDefinitionInfoList *entry;
51 CpuDefinitionInfo *info;
52
53 info = g_malloc0(sizeof(*info));
54 info->name = g_strdup("host");
55
56 entry = g_malloc0(sizeof(*entry));
57 entry->value = info;
58
59 return entry;
60}
61#endif
29e4bcb2 62
f45748f1
AF
63static void s390_cpu_set_pc(CPUState *cs, vaddr value)
64{
65 S390CPU *cpu = S390_CPU(cs);
66
67 cpu->env.psw.addr = value;
68}
69
8c2e1b00
AF
70static bool s390_cpu_has_work(CPUState *cs)
71{
72 S390CPU *cpu = S390_CPU(cs);
73 CPUS390XState *env = &cpu->env;
74
75 return (cs->interrupt_request & CPU_INTERRUPT_HARD) &&
76 (env->psw.mask & PSW_MASK_EXT);
77}
78
29c6157c
CB
79#if !defined(CONFIG_USER_ONLY)
80/* S390CPUClass::load_normal() */
81static void s390_cpu_load_normal(CPUState *s)
82{
83 S390CPU *cpu = S390_CPU(s);
fdfba1a2 84 cpu->env.psw.addr = ldl_phys(s->as, 4) & PSW_MASK_ESA_ADDR;
29c6157c 85 cpu->env.psw.mask = PSW_MASK_32 | PSW_MASK_64;
eb24f7c6 86 s390_cpu_set_state(CPU_STATE_OPERATING, cpu);
29c6157c
CB
87}
88#endif
89
f5ae2a4f 90/* S390CPUClass::cpu_reset() */
29e4bcb2
AF
91static void s390_cpu_reset(CPUState *s)
92{
93 S390CPU *cpu = S390_CPU(s);
94 S390CPUClass *scc = S390_CPU_GET_CLASS(cpu);
95 CPUS390XState *env = &cpu->env;
96
819bd309 97 env->pfault_token = -1UL;
f5ae2a4f 98 scc->parent_reset(s);
eb24f7c6 99 s390_cpu_set_state(CPU_STATE_STOPPED, cpu);
00c8cb0a 100 tlb_flush(s, 1);
f5ae2a4f
CB
101}
102
103/* S390CPUClass::initial_reset() */
104static void s390_cpu_initial_reset(CPUState *s)
105{
106 S390CPU *cpu = S390_CPU(s);
107 CPUS390XState *env = &cpu->env;
108
109 s390_cpu_reset(s);
110 /* initial reset does not touch regs,fregs and aregs */
f0c3c505 111 memset(&env->fpc, 0, offsetof(CPUS390XState, cpu_num) -
f5ae2a4f
CB
112 offsetof(CPUS390XState, fpc));
113
114 /* architectured initial values for CR 0 and 14 */
115 env->cregs[0] = CR0_RESET;
116 env->cregs[14] = CR14_RESET;
819bd309
DD
117
118 env->pfault_token = -1UL;
49f5c9e9
TH
119
120#if defined(CONFIG_KVM)
121 /* Reset state inside the kernel that we cannot access yet from QEMU. */
122 if (kvm_enabled()) {
123 if (kvm_vcpu_ioctl(s, KVM_S390_INITIAL_RESET, NULL)) {
124 perror("Initial CPU reset failed");
125 }
126 }
127#endif
f5ae2a4f
CB
128}
129
130/* CPUClass:reset() */
131static void s390_cpu_full_reset(CPUState *s)
132{
133 S390CPU *cpu = S390_CPU(s);
134 S390CPUClass *scc = S390_CPU_GET_CLASS(cpu);
135 CPUS390XState *env = &cpu->env;
136
29e4bcb2 137 scc->parent_reset(s);
eb24f7c6 138 s390_cpu_set_state(CPU_STATE_STOPPED, cpu);
29e4bcb2 139
f0c3c505 140 memset(env, 0, offsetof(CPUS390XState, cpu_num));
70bada03
JF
141
142 /* architectured initial values for CR 0 and 14 */
143 env->cregs[0] = CR0_RESET;
144 env->cregs[14] = CR14_RESET;
819bd309
DD
145
146 env->pfault_token = -1UL;
147
70bada03 148#if !defined(CONFIG_USER_ONLY)
50a2c6e5
PB
149 if (kvm_enabled()) {
150 kvm_s390_reset_vcpu(cpu);
151 }
70bada03 152#endif
00c8cb0a 153 tlb_flush(s, 1);
29e4bcb2
AF
154}
155
70bada03
JF
156#if !defined(CONFIG_USER_ONLY)
157static void s390_cpu_machine_reset_cb(void *opaque)
158{
159 S390CPU *cpu = opaque;
160
1fad8b3b 161 run_on_cpu(CPU(cpu), s390_do_cpu_full_reset, CPU(cpu));
70bada03
JF
162}
163#endif
164
1f136632
AF
165static void s390_cpu_realizefn(DeviceState *dev, Error **errp)
166{
14a10fc3 167 CPUState *cs = CPU(dev);
1f136632
AF
168 S390CPUClass *scc = S390_CPU_GET_CLASS(dev);
169
73d510c9 170 s390_cpu_gdb_init(cs);
14a10fc3 171 qemu_init_vcpu(cs);
159855f0
DH
172#if !defined(CONFIG_USER_ONLY)
173 run_on_cpu(cs, s390_do_cpu_full_reset, cs);
174#else
14a10fc3 175 cpu_reset(cs);
159855f0 176#endif
1f136632
AF
177
178 scc->parent_realize(dev, errp);
179}
180
8f22e0df
AF
181static void s390_cpu_initfn(Object *obj)
182{
c05efcb1 183 CPUState *cs = CPU(obj);
8f22e0df
AF
184 S390CPU *cpu = S390_CPU(obj);
185 CPUS390XState *env = &cpu->env;
2b7ac767 186 static bool inited;
8f22e0df
AF
187 static int cpu_num = 0;
188#if !defined(CONFIG_USER_ONLY)
189 struct tm tm;
190#endif
191
c05efcb1 192 cs->env_ptr = env;
8f22e0df
AF
193 cpu_exec_init(env);
194#if !defined(CONFIG_USER_ONLY)
70bada03 195 qemu_register_reset(s390_cpu_machine_reset_cb, cpu);
8f22e0df
AF
196 qemu_get_timedate(&tm, 0);
197 env->tod_offset = TOD_UNIX_EPOCH +
198 (time2tod(mktimegm(&tm)) * 1000000000ULL);
199 env->tod_basetime = 0;
bc72ad67
AB
200 env->tod_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, s390x_tod_timer, cpu);
201 env->cpu_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, s390x_cpu_timer, cpu);
eb24f7c6 202 s390_cpu_set_state(CPU_STATE_STOPPED, cpu);
8f22e0df
AF
203#endif
204 env->cpu_num = cpu_num++;
205 env->ext_index = -1;
2b7ac767
AF
206
207 if (tcg_enabled() && !inited) {
208 inited = true;
209 s390x_translate_init();
210 }
8f22e0df
AF
211}
212
d5627ce8
AF
213static void s390_cpu_finalize(Object *obj)
214{
215#if !defined(CONFIG_USER_ONLY)
216 S390CPU *cpu = S390_CPU(obj);
217
218 qemu_unregister_reset(s390_cpu_machine_reset_cb, cpu);
219#endif
220}
221
75973bfe 222#if !defined(CONFIG_USER_ONLY)
eb24f7c6
DH
223static bool disabled_wait(CPUState *cpu)
224{
225 return cpu->halted && !(S390_CPU(cpu)->env.psw.mask &
226 (PSW_MASK_IO | PSW_MASK_EXT | PSW_MASK_MCHECK));
227}
228
75973bfe
DH
229static unsigned s390_count_running_cpus(void)
230{
231 CPUState *cpu;
232 int nr_running = 0;
233
234 CPU_FOREACH(cpu) {
235 uint8_t state = S390_CPU(cpu)->env.cpu_state;
236 if (state == CPU_STATE_OPERATING ||
237 state == CPU_STATE_LOAD) {
eb24f7c6
DH
238 if (!disabled_wait(cpu)) {
239 nr_running++;
240 }
75973bfe
DH
241 }
242 }
243
244 return nr_running;
245}
246
eb24f7c6 247unsigned int s390_cpu_halt(S390CPU *cpu)
75973bfe
DH
248{
249 CPUState *cs = CPU(cpu);
eb24f7c6 250 trace_cpu_halt(cs->cpu_index);
75973bfe 251
eb24f7c6
DH
252 if (!cs->halted) {
253 cs->halted = 1;
254 cs->exception_index = EXCP_HLT;
75973bfe 255 }
eb24f7c6
DH
256
257 return s390_count_running_cpus();
75973bfe
DH
258}
259
eb24f7c6 260void s390_cpu_unhalt(S390CPU *cpu)
75973bfe
DH
261{
262 CPUState *cs = CPU(cpu);
eb24f7c6 263 trace_cpu_unhalt(cs->cpu_index);
75973bfe 264
eb24f7c6
DH
265 if (cs->halted) {
266 cs->halted = 0;
267 cs->exception_index = -1;
268 }
269}
270
271unsigned int s390_cpu_set_state(uint8_t cpu_state, S390CPU *cpu)
272 {
273 trace_cpu_set_state(CPU(cpu)->cpu_index, cpu_state);
274
275 switch (cpu_state) {
276 case CPU_STATE_STOPPED:
277 case CPU_STATE_CHECK_STOP:
278 /* halt the cpu for common infrastructure */
279 s390_cpu_halt(cpu);
280 break;
281 case CPU_STATE_OPERATING:
282 case CPU_STATE_LOAD:
283 /* unhalt the cpu for common infrastructure */
284 s390_cpu_unhalt(cpu);
285 break;
286 default:
287 error_report("Requested CPU state is not a valid S390 CPU state: %u",
288 cpu_state);
289 exit(1);
75973bfe 290 }
eb24f7c6 291 cpu->env.cpu_state = cpu_state;
75973bfe
DH
292
293 return s390_count_running_cpus();
294}
295#endif
296
c7396bbb
AF
297static const VMStateDescription vmstate_s390_cpu = {
298 .name = "cpu",
299 .unmigratable = 1,
300};
301
29e4bcb2
AF
302static void s390_cpu_class_init(ObjectClass *oc, void *data)
303{
304 S390CPUClass *scc = S390_CPU_CLASS(oc);
305 CPUClass *cc = CPU_CLASS(scc);
c7396bbb 306 DeviceClass *dc = DEVICE_CLASS(oc);
29e4bcb2 307
1f136632
AF
308 scc->parent_realize = dc->realize;
309 dc->realize = s390_cpu_realizefn;
310
29e4bcb2 311 scc->parent_reset = cc->reset;
29c6157c
CB
312#if !defined(CONFIG_USER_ONLY)
313 scc->load_normal = s390_cpu_load_normal;
314#endif
f5ae2a4f
CB
315 scc->cpu_reset = s390_cpu_reset;
316 scc->initial_cpu_reset = s390_cpu_initial_reset;
317 cc->reset = s390_cpu_full_reset;
8c2e1b00 318 cc->has_work = s390_cpu_has_work;
97a8ea5a 319 cc->do_interrupt = s390_cpu_do_interrupt;
878096ee 320 cc->dump_state = s390_cpu_dump_state;
f45748f1 321 cc->set_pc = s390_cpu_set_pc;
5b50e790
AF
322 cc->gdb_read_register = s390_cpu_gdb_read_register;
323 cc->gdb_write_register = s390_cpu_gdb_write_register;
7510454e
AF
324#ifdef CONFIG_USER_ONLY
325 cc->handle_mmu_fault = s390_cpu_handle_mmu_fault;
326#else
00b941e5 327 cc->get_phys_page_debug = s390_cpu_get_phys_page_debug;
9b4f38e1
ET
328 cc->write_elf64_note = s390_cpu_write_elf64_note;
329 cc->write_elf64_qemunote = s390_cpu_write_elf64_qemunote;
02bb9bbf 330 cc->cpu_exec_interrupt = s390_cpu_exec_interrupt;
00b941e5 331#endif
c7396bbb 332 dc->vmsd = &vmstate_s390_cpu;
73d510c9
DH
333 cc->gdb_num_core_regs = S390_NUM_CORE_REGS;
334 cc->gdb_core_xml_file = "s390x-core64.xml";
29e4bcb2
AF
335}
336
337static const TypeInfo s390_cpu_type_info = {
338 .name = TYPE_S390_CPU,
339 .parent = TYPE_CPU,
340 .instance_size = sizeof(S390CPU),
8f22e0df 341 .instance_init = s390_cpu_initfn,
d5627ce8 342 .instance_finalize = s390_cpu_finalize,
29e4bcb2
AF
343 .abstract = false,
344 .class_size = sizeof(S390CPUClass),
345 .class_init = s390_cpu_class_init,
346};
347
348static void s390_cpu_register_types(void)
349{
350 type_register_static(&s390_cpu_type_info);
351}
352
353type_init(s390_cpu_register_types)