xref: /qemu/target/s390x/cpu-sysemu.c (revision 83ecdb18)
1 /*
2  * QEMU S/390 CPU - System Emulation-only code
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 "qemu/error-report.h"
25 #include "qapi/error.h"
26 #include "cpu.h"
27 #include "s390x-internal.h"
28 #include "kvm/kvm_s390x.h"
29 #include "sysemu/kvm.h"
30 #include "sysemu/reset.h"
31 #include "qemu/timer.h"
32 #include "trace.h"
33 #include "qapi/qapi-visit-run-state.h"
34 #include "sysemu/hw_accel.h"
35 
36 #include "hw/s390x/pv.h"
37 #include "hw/boards.h"
38 #include "sysemu/sysemu.h"
39 #include "sysemu/tcg.h"
40 #include "hw/core/sysemu-cpu-ops.h"
41 
42 /* S390CPUClass::load_normal() */
43 static void s390_cpu_load_normal(CPUState *s)
44 {
45     S390CPU *cpu = S390_CPU(s);
46     uint64_t spsw;
47 
48     if (!s390_is_pv()) {
49         spsw = ldq_phys(s->as, 0);
50         cpu->env.psw.mask = spsw & PSW_MASK_SHORT_CTRL;
51         /*
52          * Invert short psw indication, so SIE will report a specification
53          * exception if it was not set.
54          */
55         cpu->env.psw.mask ^= PSW_MASK_SHORTPSW;
56         cpu->env.psw.addr = spsw & PSW_MASK_SHORT_ADDR;
57     } else {
58         /*
59          * Firmware requires us to set the load state before we set
60          * the cpu to operating on protected guests.
61          */
62         s390_cpu_set_state(S390_CPU_STATE_LOAD, cpu);
63     }
64     s390_cpu_set_state(S390_CPU_STATE_OPERATING, cpu);
65 }
66 
67 void s390_cpu_machine_reset_cb(void *opaque)
68 {
69     S390CPU *cpu = opaque;
70 
71     run_on_cpu(CPU(cpu), s390_do_cpu_full_reset, RUN_ON_CPU_NULL);
72 }
73 
74 static GuestPanicInformation *s390_cpu_get_crash_info(CPUState *cs)
75 {
76     GuestPanicInformation *panic_info;
77     S390CPU *cpu = S390_CPU(cs);
78 
79     cpu_synchronize_state(cs);
80     panic_info = g_new0(GuestPanicInformation, 1);
81 
82     panic_info->type = GUEST_PANIC_INFORMATION_TYPE_S390;
83     panic_info->u.s390.core = cpu->env.core_id;
84     panic_info->u.s390.psw_mask = cpu->env.psw.mask;
85     panic_info->u.s390.psw_addr = cpu->env.psw.addr;
86     panic_info->u.s390.reason = cpu->env.crash_reason;
87 
88     return panic_info;
89 }
90 
91 static void s390_cpu_get_crash_info_qom(Object *obj, Visitor *v,
92                                         const char *name, void *opaque,
93                                         Error **errp)
94 {
95     CPUState *cs = CPU(obj);
96     GuestPanicInformation *panic_info;
97 
98     if (!cs->crash_occurred) {
99         error_setg(errp, "No crash occurred");
100         return;
101     }
102 
103     panic_info = s390_cpu_get_crash_info(cs);
104 
105     visit_type_GuestPanicInformation(v, "crash-information", &panic_info,
106                                      errp);
107     qapi_free_GuestPanicInformation(panic_info);
108 }
109 
110 void s390_cpu_init_sysemu(Object *obj)
111 {
112     CPUState *cs = CPU(obj);
113     S390CPU *cpu = S390_CPU(obj);
114 
115     cs->start_powered_off = true;
116     object_property_add(obj, "crash-information", "GuestPanicInformation",
117                         s390_cpu_get_crash_info_qom, NULL, NULL, NULL);
118     cpu->env.tod_timer =
119         timer_new_ns(QEMU_CLOCK_VIRTUAL, s390x_tod_timer, cpu);
120     cpu->env.cpu_timer =
121         timer_new_ns(QEMU_CLOCK_VIRTUAL, s390x_cpu_timer, cpu);
122     s390_cpu_set_state(S390_CPU_STATE_STOPPED, cpu);
123 }
124 
125 bool s390_cpu_realize_sysemu(DeviceState *dev, Error **errp)
126 {
127     S390CPU *cpu = S390_CPU(dev);
128     MachineState *ms = MACHINE(qdev_get_machine());
129     unsigned int max_cpus = ms->smp.max_cpus;
130 
131     if (cpu->env.core_id >= max_cpus) {
132         error_setg(errp, "Unable to add CPU with core-id: %" PRIu32
133                    ", maximum core-id: %d", cpu->env.core_id,
134                    max_cpus - 1);
135         return false;
136     }
137 
138     if (cpu_exists(cpu->env.core_id)) {
139         error_setg(errp, "Unable to add CPU with core-id: %" PRIu32
140                    ", it already exists", cpu->env.core_id);
141         return false;
142     }
143 
144     /* sync cs->cpu_index and env->core_id. The latter is needed for TCG. */
145     CPU(cpu)->cpu_index = cpu->env.core_id;
146     return true;
147 }
148 
149 void s390_cpu_finalize(Object *obj)
150 {
151     S390CPU *cpu = S390_CPU(obj);
152 
153     timer_free(cpu->env.tod_timer);
154     timer_free(cpu->env.cpu_timer);
155 
156     qemu_unregister_reset(s390_cpu_machine_reset_cb, cpu);
157     g_free(cpu->irqstate);
158 }
159 
160 static const struct SysemuCPUOps s390_sysemu_ops = {
161     .get_phys_page_debug = s390_cpu_get_phys_page_debug,
162     .get_crash_info = s390_cpu_get_crash_info,
163     .write_elf64_note = s390_cpu_write_elf64_note,
164     .legacy_vmsd = &vmstate_s390_cpu,
165 };
166 
167 void s390_cpu_class_init_sysemu(CPUClass *cc)
168 {
169     S390CPUClass *scc = S390_CPU_CLASS(cc);
170 
171     scc->load_normal = s390_cpu_load_normal;
172     cc->sysemu_ops = &s390_sysemu_ops;
173 }
174 
175 static bool disabled_wait(CPUState *cpu)
176 {
177     return cpu->halted && !(S390_CPU(cpu)->env.psw.mask &
178                             (PSW_MASK_IO | PSW_MASK_EXT | PSW_MASK_MCHECK));
179 }
180 
181 static unsigned s390_count_running_cpus(void)
182 {
183     CPUState *cpu;
184     int nr_running = 0;
185 
186     CPU_FOREACH(cpu) {
187         uint8_t state = S390_CPU(cpu)->env.cpu_state;
188         if (state == S390_CPU_STATE_OPERATING ||
189             state == S390_CPU_STATE_LOAD) {
190             if (!disabled_wait(cpu)) {
191                 nr_running++;
192             }
193         }
194     }
195 
196     return nr_running;
197 }
198 
199 unsigned int s390_cpu_halt(S390CPU *cpu)
200 {
201     CPUState *cs = CPU(cpu);
202     trace_cpu_halt(cs->cpu_index);
203 
204     if (!cs->halted) {
205         cs->halted = 1;
206         cs->exception_index = EXCP_HLT;
207     }
208 
209     return s390_count_running_cpus();
210 }
211 
212 void s390_cpu_unhalt(S390CPU *cpu)
213 {
214     CPUState *cs = CPU(cpu);
215     trace_cpu_unhalt(cs->cpu_index);
216 
217     if (cs->halted) {
218         cs->halted = 0;
219         cs->exception_index = -1;
220     }
221 }
222 
223 unsigned int s390_cpu_set_state(uint8_t cpu_state, S390CPU *cpu)
224  {
225     trace_cpu_set_state(CPU(cpu)->cpu_index, cpu_state);
226 
227     switch (cpu_state) {
228     case S390_CPU_STATE_STOPPED:
229     case S390_CPU_STATE_CHECK_STOP:
230         /* halt the cpu for common infrastructure */
231         s390_cpu_halt(cpu);
232         break;
233     case S390_CPU_STATE_OPERATING:
234     case S390_CPU_STATE_LOAD:
235         /*
236          * Starting a CPU with a PSW WAIT bit set:
237          * KVM: handles this internally and triggers another WAIT exit.
238          * TCG: will actually try to continue to run. Don't unhalt, will
239          *      be done when the CPU actually has work (an interrupt).
240          */
241         if (!tcg_enabled() || !(cpu->env.psw.mask & PSW_MASK_WAIT)) {
242             s390_cpu_unhalt(cpu);
243         }
244         break;
245     default:
246         error_report("Requested CPU state is not a valid S390 CPU state: %u",
247                      cpu_state);
248         exit(1);
249     }
250     if (kvm_enabled() && cpu->env.cpu_state != cpu_state) {
251         kvm_s390_set_cpu_state(cpu, cpu_state);
252     }
253     cpu->env.cpu_state = cpu_state;
254 
255     return s390_count_running_cpus();
256 }
257 
258 int s390_set_memory_limit(uint64_t new_limit, uint64_t *hw_limit)
259 {
260     if (kvm_enabled()) {
261         return kvm_s390_set_mem_limit(new_limit, hw_limit);
262     }
263     return 0;
264 }
265 
266 void s390_set_max_pagesize(uint64_t pagesize, Error **errp)
267 {
268     if (kvm_enabled()) {
269         kvm_s390_set_max_pagesize(pagesize, errp);
270     }
271 }
272 
273 void s390_cmma_reset(void)
274 {
275     if (kvm_enabled()) {
276         kvm_s390_cmma_reset();
277     }
278 }
279 
280 int s390_assign_subch_ioeventfd(EventNotifier *notifier, uint32_t sch_id,
281                                 int vq, bool assign)
282 {
283     if (kvm_enabled()) {
284         return kvm_s390_assign_subch_ioeventfd(notifier, sch_id, vq, assign);
285     } else {
286         return 0;
287     }
288 }
289 
290 void s390_crypto_reset(void)
291 {
292     if (kvm_enabled()) {
293         kvm_s390_crypto_reset();
294     }
295 }
296 
297 void s390_enable_css_support(S390CPU *cpu)
298 {
299     if (kvm_enabled()) {
300         kvm_s390_enable_css_support(cpu);
301     }
302 }
303 
304 void s390_do_cpu_set_diag318(CPUState *cs, run_on_cpu_data arg)
305 {
306     if (kvm_enabled()) {
307         kvm_s390_set_diag318(cs, arg.host_ulong);
308     }
309 }
310