]> Git Repo - qemu.git/blob - target-s390x/cpu.c
s390x/kvm: implement handling of new SIGP orders
[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 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>
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.
24  */
25
26 #include "cpu.h"
27 #include "qemu-common.h"
28 #include "qemu/timer.h"
29 #include "qemu/error-report.h"
30 #include "hw/hw.h"
31 #include "trace.h"
32 #ifndef CONFIG_USER_ONLY
33 #include "sysemu/arch_init.h"
34 #endif
35
36 #define CR0_RESET       0xE0UL
37 #define CR14_RESET      0xC2000000UL;
38
39 /* generate CPU information for cpu -? */
40 void 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 }
46
47 #ifndef CONFIG_USER_ONLY
48 CpuDefinitionInfoList *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
62
63 static 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
70 static 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
79 #if !defined(CONFIG_USER_ONLY)
80 /* S390CPUClass::load_normal() */
81 static void s390_cpu_load_normal(CPUState *s)
82 {
83     S390CPU *cpu = S390_CPU(s);
84     cpu->env.psw.addr = ldl_phys(s->as, 4) & PSW_MASK_ESA_ADDR;
85     cpu->env.psw.mask = PSW_MASK_32 | PSW_MASK_64;
86     s390_cpu_set_state(CPU_STATE_OPERATING, cpu);
87 }
88 #endif
89
90 /* S390CPUClass::cpu_reset() */
91 static 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
97     env->pfault_token = -1UL;
98     scc->parent_reset(s);
99     cpu->env.sigp_order = 0;
100     s390_cpu_set_state(CPU_STATE_STOPPED, cpu);
101     tlb_flush(s, 1);
102 }
103
104 /* S390CPUClass::initial_reset() */
105 static void s390_cpu_initial_reset(CPUState *s)
106 {
107     S390CPU *cpu = S390_CPU(s);
108     CPUS390XState *env = &cpu->env;
109
110     s390_cpu_reset(s);
111     /* initial reset does not touch regs,fregs and aregs */
112     memset(&env->fpc, 0, offsetof(CPUS390XState, cpu_num) -
113                          offsetof(CPUS390XState, fpc));
114
115     /* architectured initial values for CR 0 and 14 */
116     env->cregs[0] = CR0_RESET;
117     env->cregs[14] = CR14_RESET;
118
119     env->pfault_token = -1UL;
120
121     /* Reset state inside the kernel that we cannot access yet from QEMU. */
122     if (kvm_enabled()) {
123         kvm_s390_reset_vcpu(cpu);
124     }
125 }
126
127 /* CPUClass:reset() */
128 static void s390_cpu_full_reset(CPUState *s)
129 {
130     S390CPU *cpu = S390_CPU(s);
131     S390CPUClass *scc = S390_CPU_GET_CLASS(cpu);
132     CPUS390XState *env = &cpu->env;
133
134     scc->parent_reset(s);
135     cpu->env.sigp_order = 0;
136     s390_cpu_set_state(CPU_STATE_STOPPED, cpu);
137
138     memset(env, 0, offsetof(CPUS390XState, cpu_num));
139
140     /* architectured initial values for CR 0 and 14 */
141     env->cregs[0] = CR0_RESET;
142     env->cregs[14] = CR14_RESET;
143
144     env->pfault_token = -1UL;
145
146     /* Reset state inside the kernel that we cannot access yet from QEMU. */
147     if (kvm_enabled()) {
148         kvm_s390_reset_vcpu(cpu);
149     }
150     tlb_flush(s, 1);
151 }
152
153 #if !defined(CONFIG_USER_ONLY)
154 static void s390_cpu_machine_reset_cb(void *opaque)
155 {
156     S390CPU *cpu = opaque;
157
158     run_on_cpu(CPU(cpu), s390_do_cpu_full_reset, CPU(cpu));
159 }
160 #endif
161
162 static void s390_cpu_realizefn(DeviceState *dev, Error **errp)
163 {
164     CPUState *cs = CPU(dev);
165     S390CPUClass *scc = S390_CPU_GET_CLASS(dev);
166
167     s390_cpu_gdb_init(cs);
168     qemu_init_vcpu(cs);
169 #if !defined(CONFIG_USER_ONLY)
170     run_on_cpu(cs, s390_do_cpu_full_reset, cs);
171 #else
172     cpu_reset(cs);
173 #endif
174
175     scc->parent_realize(dev, errp);
176 }
177
178 static void s390_cpu_initfn(Object *obj)
179 {
180     CPUState *cs = CPU(obj);
181     S390CPU *cpu = S390_CPU(obj);
182     CPUS390XState *env = &cpu->env;
183     static bool inited;
184     static int cpu_num = 0;
185 #if !defined(CONFIG_USER_ONLY)
186     struct tm tm;
187 #endif
188
189     cs->env_ptr = env;
190     cpu_exec_init(env);
191 #if !defined(CONFIG_USER_ONLY)
192     qemu_register_reset(s390_cpu_machine_reset_cb, cpu);
193     qemu_get_timedate(&tm, 0);
194     env->tod_offset = TOD_UNIX_EPOCH +
195                       (time2tod(mktimegm(&tm)) * 1000000000ULL);
196     env->tod_basetime = 0;
197     env->tod_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, s390x_tod_timer, cpu);
198     env->cpu_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, s390x_cpu_timer, cpu);
199     s390_cpu_set_state(CPU_STATE_STOPPED, cpu);
200 #endif
201     env->cpu_num = cpu_num++;
202     env->ext_index = -1;
203
204     if (tcg_enabled() && !inited) {
205         inited = true;
206         s390x_translate_init();
207     }
208 }
209
210 static void s390_cpu_finalize(Object *obj)
211 {
212 #if !defined(CONFIG_USER_ONLY)
213     S390CPU *cpu = S390_CPU(obj);
214
215     qemu_unregister_reset(s390_cpu_machine_reset_cb, cpu);
216 #endif
217 }
218
219 #if !defined(CONFIG_USER_ONLY)
220 static bool disabled_wait(CPUState *cpu)
221 {
222     return cpu->halted && !(S390_CPU(cpu)->env.psw.mask &
223                             (PSW_MASK_IO | PSW_MASK_EXT | PSW_MASK_MCHECK));
224 }
225
226 static unsigned s390_count_running_cpus(void)
227 {
228     CPUState *cpu;
229     int nr_running = 0;
230
231     CPU_FOREACH(cpu) {
232         uint8_t state = S390_CPU(cpu)->env.cpu_state;
233         if (state == CPU_STATE_OPERATING ||
234             state == CPU_STATE_LOAD) {
235             if (!disabled_wait(cpu)) {
236                 nr_running++;
237             }
238         }
239     }
240
241     return nr_running;
242 }
243
244 unsigned int s390_cpu_halt(S390CPU *cpu)
245 {
246     CPUState *cs = CPU(cpu);
247     trace_cpu_halt(cs->cpu_index);
248
249     if (!cs->halted) {
250         cs->halted = 1;
251         cs->exception_index = EXCP_HLT;
252     }
253
254     return s390_count_running_cpus();
255 }
256
257 void s390_cpu_unhalt(S390CPU *cpu)
258 {
259     CPUState *cs = CPU(cpu);
260     trace_cpu_unhalt(cs->cpu_index);
261
262     if (cs->halted) {
263         cs->halted = 0;
264         cs->exception_index = -1;
265     }
266 }
267
268 unsigned int s390_cpu_set_state(uint8_t cpu_state, S390CPU *cpu)
269  {
270     trace_cpu_set_state(CPU(cpu)->cpu_index, cpu_state);
271
272     switch (cpu_state) {
273     case CPU_STATE_STOPPED:
274     case CPU_STATE_CHECK_STOP:
275         /* halt the cpu for common infrastructure */
276         s390_cpu_halt(cpu);
277         break;
278     case CPU_STATE_OPERATING:
279     case CPU_STATE_LOAD:
280         /* unhalt the cpu for common infrastructure */
281         s390_cpu_unhalt(cpu);
282         break;
283     default:
284         error_report("Requested CPU state is not a valid S390 CPU state: %u",
285                      cpu_state);
286         exit(1);
287     }
288     if (kvm_enabled() && cpu->env.cpu_state != cpu_state) {
289         kvm_s390_set_cpu_state(cpu, cpu_state);
290     }
291     cpu->env.cpu_state = cpu_state;
292
293     return s390_count_running_cpus();
294 }
295 #endif
296
297 static void s390_cpu_class_init(ObjectClass *oc, void *data)
298 {
299     S390CPUClass *scc = S390_CPU_CLASS(oc);
300     CPUClass *cc = CPU_CLASS(scc);
301     DeviceClass *dc = DEVICE_CLASS(oc);
302
303     scc->parent_realize = dc->realize;
304     dc->realize = s390_cpu_realizefn;
305
306     scc->parent_reset = cc->reset;
307 #if !defined(CONFIG_USER_ONLY)
308     scc->load_normal = s390_cpu_load_normal;
309 #endif
310     scc->cpu_reset = s390_cpu_reset;
311     scc->initial_cpu_reset = s390_cpu_initial_reset;
312     cc->reset = s390_cpu_full_reset;
313     cc->has_work = s390_cpu_has_work;
314     cc->do_interrupt = s390_cpu_do_interrupt;
315     cc->dump_state = s390_cpu_dump_state;
316     cc->set_pc = s390_cpu_set_pc;
317     cc->gdb_read_register = s390_cpu_gdb_read_register;
318     cc->gdb_write_register = s390_cpu_gdb_write_register;
319 #ifdef CONFIG_USER_ONLY
320     cc->handle_mmu_fault = s390_cpu_handle_mmu_fault;
321 #else
322     cc->get_phys_page_debug = s390_cpu_get_phys_page_debug;
323     cc->vmsd = &vmstate_s390_cpu;
324     cc->write_elf64_note = s390_cpu_write_elf64_note;
325     cc->write_elf64_qemunote = s390_cpu_write_elf64_qemunote;
326     cc->cpu_exec_interrupt = s390_cpu_exec_interrupt;
327 #endif
328     cc->gdb_num_core_regs = S390_NUM_CORE_REGS;
329     cc->gdb_core_xml_file = "s390x-core64.xml";
330 }
331
332 static const TypeInfo s390_cpu_type_info = {
333     .name = TYPE_S390_CPU,
334     .parent = TYPE_CPU,
335     .instance_size = sizeof(S390CPU),
336     .instance_init = s390_cpu_initfn,
337     .instance_finalize = s390_cpu_finalize,
338     .abstract = false,
339     .class_size = sizeof(S390CPUClass),
340     .class_init = s390_cpu_class_init,
341 };
342
343 static void s390_cpu_register_types(void)
344 {
345     type_register_static(&s390_cpu_type_info);
346 }
347
348 type_init(s390_cpu_register_types)
This page took 0.042249 seconds and 4 git commands to generate.