]>
Commit | Line | Data |
---|---|---|
0e60a699 AG |
1 | /* |
2 | * QEMU S390x KVM implementation | |
3 | * | |
4 | * Copyright (c) 2009 Alexander Graf <[email protected]> | |
ccb084d3 | 5 | * Copyright IBM Corp. 2012 |
0e60a699 AG |
6 | * |
7 | * This library is free software; you can redistribute it and/or | |
8 | * modify it under the terms of the GNU Lesser General Public | |
9 | * License as published by the Free Software Foundation; either | |
10 | * version 2 of the License, or (at your option) any later version. | |
11 | * | |
12 | * This library is distributed in the hope that it will be useful, | |
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
15 | * Lesser General Public License for more details. | |
16 | * | |
ccb084d3 CB |
17 | * Contributions after 2012-10-29 are licensed under the terms of the |
18 | * GNU GPL, version 2 or (at your option) any later version. | |
19 | * | |
20 | * You should have received a copy of the GNU (Lesser) General Public | |
0e60a699 AG |
21 | * License along with this library; if not, see <http://www.gnu.org/licenses/>. |
22 | */ | |
23 | ||
24 | #include <sys/types.h> | |
25 | #include <sys/ioctl.h> | |
26 | #include <sys/mman.h> | |
27 | ||
28 | #include <linux/kvm.h> | |
29 | #include <asm/ptrace.h> | |
30 | ||
31 | #include "qemu-common.h" | |
1de7afc9 | 32 | #include "qemu/timer.h" |
9c17d615 PB |
33 | #include "sysemu/sysemu.h" |
34 | #include "sysemu/kvm.h" | |
0e60a699 | 35 | #include "cpu.h" |
9c17d615 | 36 | #include "sysemu/device_tree.h" |
08eb8c85 CB |
37 | #include "qapi/qmp/qjson.h" |
38 | #include "monitor/monitor.h" | |
0e60a699 AG |
39 | |
40 | /* #define DEBUG_KVM */ | |
41 | ||
42 | #ifdef DEBUG_KVM | |
43 | #define dprintf(fmt, ...) \ | |
44 | do { fprintf(stderr, fmt, ## __VA_ARGS__); } while (0) | |
45 | #else | |
46 | #define dprintf(fmt, ...) \ | |
47 | do { } while (0) | |
48 | #endif | |
49 | ||
50 | #define IPA0_DIAG 0x8300 | |
51 | #define IPA0_SIGP 0xae00 | |
09b99878 CH |
52 | #define IPA0_B2 0xb200 |
53 | #define IPA0_B9 0xb900 | |
54 | #define IPA0_EB 0xeb00 | |
0e60a699 AG |
55 | |
56 | #define PRIV_SCLP_CALL 0x20 | |
09b99878 CH |
57 | #define PRIV_CSCH 0x30 |
58 | #define PRIV_HSCH 0x31 | |
59 | #define PRIV_MSCH 0x32 | |
60 | #define PRIV_SSCH 0x33 | |
61 | #define PRIV_STSCH 0x34 | |
62 | #define PRIV_TSCH 0x35 | |
63 | #define PRIV_TPI 0x36 | |
64 | #define PRIV_SAL 0x37 | |
65 | #define PRIV_RSCH 0x38 | |
66 | #define PRIV_STCRW 0x39 | |
67 | #define PRIV_STCPS 0x3a | |
68 | #define PRIV_RCHP 0x3b | |
69 | #define PRIV_SCHM 0x3c | |
70 | #define PRIV_CHSC 0x5f | |
71 | #define PRIV_SIGA 0x74 | |
72 | #define PRIV_XSCH 0x76 | |
73 | #define PRIV_SQBS 0x8a | |
74 | #define PRIV_EQBS 0x9c | |
0e60a699 AG |
75 | #define DIAG_KVM_HYPERCALL 0x500 |
76 | #define DIAG_KVM_BREAKPOINT 0x501 | |
77 | ||
0e60a699 AG |
78 | #define ICPT_INSTRUCTION 0x04 |
79 | #define ICPT_WAITPSW 0x1c | |
80 | #define ICPT_SOFT_INTERCEPT 0x24 | |
81 | #define ICPT_CPU_STOP 0x28 | |
82 | #define ICPT_IO 0x40 | |
83 | ||
84 | #define SIGP_RESTART 0x06 | |
85 | #define SIGP_INITIAL_CPU_RESET 0x0b | |
86 | #define SIGP_STORE_STATUS_ADDR 0x0e | |
87 | #define SIGP_SET_ARCH 0x12 | |
88 | ||
94a8d39a JK |
89 | const KVMCapabilityInfo kvm_arch_required_capabilities[] = { |
90 | KVM_CAP_LAST_INFO | |
91 | }; | |
92 | ||
5b08b344 CB |
93 | static int cap_sync_regs; |
94 | ||
cad1e282 | 95 | int kvm_arch_init(KVMState *s) |
0e60a699 | 96 | { |
5b08b344 | 97 | cap_sync_regs = kvm_check_extension(s, KVM_CAP_SYNC_REGS); |
0e60a699 AG |
98 | return 0; |
99 | } | |
100 | ||
b164e48e EH |
101 | unsigned long kvm_arch_vcpu_id(CPUState *cpu) |
102 | { | |
103 | return cpu->cpu_index; | |
104 | } | |
105 | ||
20d695a9 | 106 | int kvm_arch_init_vcpu(CPUState *cpu) |
0e60a699 | 107 | { |
1c9d2a1d CB |
108 | /* nothing todo yet */ |
109 | return 0; | |
0e60a699 AG |
110 | } |
111 | ||
20d695a9 | 112 | void kvm_arch_reset_vcpu(CPUState *cpu) |
0e60a699 | 113 | { |
419831d7 AG |
114 | /* The initial reset call is needed here to reset in-kernel |
115 | * vcpu data that we can't access directly from QEMU | |
116 | * (i.e. with older kernels which don't support sync_regs/ONE_REG). | |
117 | * Before this ioctl cpu_synchronize_state() is called in common kvm | |
118 | * code (kvm-all) */ | |
70bada03 JF |
119 | if (kvm_vcpu_ioctl(cpu, KVM_S390_INITIAL_RESET, NULL)) { |
120 | perror("Can't reset vcpu\n"); | |
121 | } | |
0e60a699 AG |
122 | } |
123 | ||
20d695a9 | 124 | int kvm_arch_put_registers(CPUState *cs, int level) |
0e60a699 | 125 | { |
20d695a9 AF |
126 | S390CPU *cpu = S390_CPU(cs); |
127 | CPUS390XState *env = &cpu->env; | |
420840e5 | 128 | struct kvm_one_reg reg; |
5b08b344 | 129 | struct kvm_sregs sregs; |
0e60a699 AG |
130 | struct kvm_regs regs; |
131 | int ret; | |
132 | int i; | |
133 | ||
5b08b344 | 134 | /* always save the PSW and the GPRS*/ |
f7575c96 AF |
135 | cs->kvm_run->psw_addr = env->psw.addr; |
136 | cs->kvm_run->psw_mask = env->psw.mask; | |
0e60a699 | 137 | |
f7575c96 | 138 | if (cap_sync_regs && cs->kvm_run->kvm_valid_regs & KVM_SYNC_GPRS) { |
5b08b344 | 139 | for (i = 0; i < 16; i++) { |
f7575c96 AF |
140 | cs->kvm_run->s.regs.gprs[i] = env->regs[i]; |
141 | cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_GPRS; | |
5b08b344 CB |
142 | } |
143 | } else { | |
144 | for (i = 0; i < 16; i++) { | |
145 | regs.gprs[i] = env->regs[i]; | |
146 | } | |
1bc22652 | 147 | ret = kvm_vcpu_ioctl(cs, KVM_SET_REGS, ®s); |
5b08b344 CB |
148 | if (ret < 0) { |
149 | return ret; | |
150 | } | |
0e60a699 AG |
151 | } |
152 | ||
420840e5 JH |
153 | if (env->runtime_reg_dirty_mask == KVM_S390_RUNTIME_DIRTY_FULL) { |
154 | reg.id = KVM_REG_S390_CPU_TIMER; | |
155 | reg.addr = (__u64)&(env->cputm); | |
156 | ret = kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, ®); | |
157 | if (ret < 0) { | |
158 | return ret; | |
159 | } | |
160 | ||
161 | reg.id = KVM_REG_S390_CLOCK_COMP; | |
162 | reg.addr = (__u64)&(env->ckc); | |
163 | ret = kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, ®); | |
164 | if (ret < 0) { | |
165 | return ret; | |
166 | } | |
167 | ||
168 | reg.id = KVM_REG_S390_TODPR; | |
169 | reg.addr = (__u64)&(env->todpr); | |
170 | ret = kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, ®); | |
171 | if (ret < 0) { | |
172 | return ret; | |
173 | } | |
174 | } | |
175 | env->runtime_reg_dirty_mask = KVM_S390_RUNTIME_DIRTY_NONE; | |
176 | ||
5b08b344 CB |
177 | /* Do we need to save more than that? */ |
178 | if (level == KVM_PUT_RUNTIME_STATE) { | |
179 | return 0; | |
0e60a699 AG |
180 | } |
181 | ||
5b08b344 | 182 | if (cap_sync_regs && |
f7575c96 AF |
183 | cs->kvm_run->kvm_valid_regs & KVM_SYNC_ACRS && |
184 | cs->kvm_run->kvm_valid_regs & KVM_SYNC_CRS) { | |
5b08b344 | 185 | for (i = 0; i < 16; i++) { |
f7575c96 AF |
186 | cs->kvm_run->s.regs.acrs[i] = env->aregs[i]; |
187 | cs->kvm_run->s.regs.crs[i] = env->cregs[i]; | |
5b08b344 | 188 | } |
f7575c96 AF |
189 | cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_ACRS; |
190 | cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_CRS; | |
5b08b344 CB |
191 | } else { |
192 | for (i = 0; i < 16; i++) { | |
193 | sregs.acrs[i] = env->aregs[i]; | |
194 | sregs.crs[i] = env->cregs[i]; | |
195 | } | |
1bc22652 | 196 | ret = kvm_vcpu_ioctl(cs, KVM_SET_SREGS, &sregs); |
5b08b344 CB |
197 | if (ret < 0) { |
198 | return ret; | |
199 | } | |
200 | } | |
0e60a699 | 201 | |
5b08b344 | 202 | /* Finally the prefix */ |
f7575c96 AF |
203 | if (cap_sync_regs && cs->kvm_run->kvm_valid_regs & KVM_SYNC_PREFIX) { |
204 | cs->kvm_run->s.regs.prefix = env->psa; | |
205 | cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_PREFIX; | |
5b08b344 CB |
206 | } else { |
207 | /* prefix is only supported via sync regs */ | |
208 | } | |
209 | return 0; | |
0e60a699 AG |
210 | } |
211 | ||
20d695a9 | 212 | int kvm_arch_get_registers(CPUState *cs) |
420840e5 JH |
213 | { |
214 | S390CPU *cpu = S390_CPU(cs); | |
215 | CPUS390XState *env = &cpu->env; | |
216 | struct kvm_one_reg reg; | |
217 | int r; | |
218 | ||
219 | r = kvm_s390_get_registers_partial(cs); | |
220 | if (r < 0) { | |
221 | return r; | |
222 | } | |
223 | ||
224 | reg.id = KVM_REG_S390_CPU_TIMER; | |
225 | reg.addr = (__u64)&(env->cputm); | |
226 | r = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, ®); | |
227 | if (r < 0) { | |
228 | return r; | |
229 | } | |
230 | ||
231 | reg.id = KVM_REG_S390_CLOCK_COMP; | |
232 | reg.addr = (__u64)&(env->ckc); | |
233 | r = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, ®); | |
234 | if (r < 0) { | |
235 | return r; | |
236 | } | |
237 | ||
238 | reg.id = KVM_REG_S390_TODPR; | |
239 | reg.addr = (__u64)&(env->todpr); | |
240 | r = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, ®); | |
241 | if (r < 0) { | |
242 | return r; | |
243 | } | |
244 | ||
245 | env->runtime_reg_dirty_mask = KVM_S390_RUNTIME_DIRTY_FULL; | |
246 | return 0; | |
247 | } | |
248 | ||
249 | int kvm_s390_get_registers_partial(CPUState *cs) | |
0e60a699 | 250 | { |
20d695a9 AF |
251 | S390CPU *cpu = S390_CPU(cs); |
252 | CPUS390XState *env = &cpu->env; | |
5b08b344 | 253 | struct kvm_sregs sregs; |
0e60a699 | 254 | struct kvm_regs regs; |
5b08b344 | 255 | int ret; |
0e60a699 AG |
256 | int i; |
257 | ||
420840e5 JH |
258 | if (env->runtime_reg_dirty_mask) { |
259 | return 0; | |
260 | } | |
261 | ||
5b08b344 | 262 | /* get the PSW */ |
f7575c96 AF |
263 | env->psw.addr = cs->kvm_run->psw_addr; |
264 | env->psw.mask = cs->kvm_run->psw_mask; | |
5b08b344 CB |
265 | |
266 | /* the GPRS */ | |
f7575c96 | 267 | if (cap_sync_regs && cs->kvm_run->kvm_valid_regs & KVM_SYNC_GPRS) { |
5b08b344 | 268 | for (i = 0; i < 16; i++) { |
f7575c96 | 269 | env->regs[i] = cs->kvm_run->s.regs.gprs[i]; |
5b08b344 CB |
270 | } |
271 | } else { | |
1bc22652 | 272 | ret = kvm_vcpu_ioctl(cs, KVM_GET_REGS, ®s); |
5b08b344 CB |
273 | if (ret < 0) { |
274 | return ret; | |
275 | } | |
276 | for (i = 0; i < 16; i++) { | |
277 | env->regs[i] = regs.gprs[i]; | |
278 | } | |
0e60a699 AG |
279 | } |
280 | ||
5b08b344 CB |
281 | /* The ACRS and CRS */ |
282 | if (cap_sync_regs && | |
f7575c96 AF |
283 | cs->kvm_run->kvm_valid_regs & KVM_SYNC_ACRS && |
284 | cs->kvm_run->kvm_valid_regs & KVM_SYNC_CRS) { | |
5b08b344 | 285 | for (i = 0; i < 16; i++) { |
f7575c96 AF |
286 | env->aregs[i] = cs->kvm_run->s.regs.acrs[i]; |
287 | env->cregs[i] = cs->kvm_run->s.regs.crs[i]; | |
5b08b344 CB |
288 | } |
289 | } else { | |
1bc22652 | 290 | ret = kvm_vcpu_ioctl(cs, KVM_GET_SREGS, &sregs); |
5b08b344 CB |
291 | if (ret < 0) { |
292 | return ret; | |
293 | } | |
294 | for (i = 0; i < 16; i++) { | |
295 | env->aregs[i] = sregs.acrs[i]; | |
296 | env->cregs[i] = sregs.crs[i]; | |
297 | } | |
0e60a699 AG |
298 | } |
299 | ||
5b08b344 | 300 | /* Finally the prefix */ |
f7575c96 AF |
301 | if (cap_sync_regs && cs->kvm_run->kvm_valid_regs & KVM_SYNC_PREFIX) { |
302 | env->psa = cs->kvm_run->s.regs.prefix; | |
5b08b344 CB |
303 | } else { |
304 | /* no prefix without sync regs */ | |
305 | } | |
0e60a699 | 306 | |
420840e5 | 307 | env->runtime_reg_dirty_mask = KVM_S390_RUNTIME_DIRTY_PARTIAL; |
0e60a699 AG |
308 | return 0; |
309 | } | |
310 | ||
fdec9918 CB |
311 | /* |
312 | * Legacy layout for s390: | |
313 | * Older S390 KVM requires the topmost vma of the RAM to be | |
314 | * smaller than an system defined value, which is at least 256GB. | |
315 | * Larger systems have larger values. We put the guest between | |
316 | * the end of data segment (system break) and this value. We | |
317 | * use 32GB as a base to have enough room for the system break | |
318 | * to grow. We also have to use MAP parameters that avoid | |
319 | * read-only mapping of guest pages. | |
320 | */ | |
321 | static void *legacy_s390_alloc(ram_addr_t size) | |
322 | { | |
323 | void *mem; | |
324 | ||
325 | mem = mmap((void *) 0x800000000ULL, size, | |
326 | PROT_EXEC|PROT_READ|PROT_WRITE, | |
327 | MAP_SHARED | MAP_ANONYMOUS | MAP_FIXED, -1, 0); | |
328 | if (mem == MAP_FAILED) { | |
329 | fprintf(stderr, "Allocating RAM failed\n"); | |
330 | abort(); | |
331 | } | |
332 | return mem; | |
333 | } | |
334 | ||
6eebf958 | 335 | void *kvm_arch_ram_alloc(ram_addr_t size) |
fdec9918 CB |
336 | { |
337 | /* Can we use the standard allocation ? */ | |
338 | if (kvm_check_extension(kvm_state, KVM_CAP_S390_GMAP) && | |
339 | kvm_check_extension(kvm_state, KVM_CAP_S390_COW)) { | |
340 | return NULL; | |
341 | } else { | |
342 | return legacy_s390_alloc(size); | |
343 | } | |
344 | } | |
345 | ||
20d695a9 | 346 | int kvm_arch_insert_sw_breakpoint(CPUState *cs, struct kvm_sw_breakpoint *bp) |
0e60a699 | 347 | { |
20d695a9 AF |
348 | S390CPU *cpu = S390_CPU(cs); |
349 | CPUS390XState *env = &cpu->env; | |
0e60a699 AG |
350 | static const uint8_t diag_501[] = {0x83, 0x24, 0x05, 0x01}; |
351 | ||
352 | if (cpu_memory_rw_debug(env, bp->pc, (uint8_t *)&bp->saved_insn, 4, 0) || | |
353 | cpu_memory_rw_debug(env, bp->pc, (uint8_t *)diag_501, 4, 1)) { | |
354 | return -EINVAL; | |
355 | } | |
356 | return 0; | |
357 | } | |
358 | ||
20d695a9 | 359 | int kvm_arch_remove_sw_breakpoint(CPUState *cs, struct kvm_sw_breakpoint *bp) |
0e60a699 | 360 | { |
20d695a9 AF |
361 | S390CPU *cpu = S390_CPU(cs); |
362 | CPUS390XState *env = &cpu->env; | |
0e60a699 AG |
363 | uint8_t t[4]; |
364 | static const uint8_t diag_501[] = {0x83, 0x24, 0x05, 0x01}; | |
365 | ||
366 | if (cpu_memory_rw_debug(env, bp->pc, t, 4, 0)) { | |
367 | return -EINVAL; | |
368 | } else if (memcmp(t, diag_501, 4)) { | |
369 | return -EINVAL; | |
370 | } else if (cpu_memory_rw_debug(env, bp->pc, (uint8_t *)&bp->saved_insn, 1, 1)) { | |
371 | return -EINVAL; | |
372 | } | |
373 | ||
374 | return 0; | |
375 | } | |
376 | ||
20d695a9 | 377 | void kvm_arch_pre_run(CPUState *cpu, struct kvm_run *run) |
0e60a699 | 378 | { |
0e60a699 AG |
379 | } |
380 | ||
20d695a9 | 381 | void kvm_arch_post_run(CPUState *cpu, struct kvm_run *run) |
0e60a699 | 382 | { |
0e60a699 AG |
383 | } |
384 | ||
20d695a9 | 385 | int kvm_arch_process_async_events(CPUState *cs) |
0af691d7 | 386 | { |
225dc991 | 387 | return cs->halted; |
0af691d7 MT |
388 | } |
389 | ||
1bc22652 | 390 | void kvm_s390_interrupt_internal(S390CPU *cpu, int type, uint32_t parm, |
bcec36ea | 391 | uint64_t parm64, int vm) |
0e60a699 | 392 | { |
1bc22652 | 393 | CPUState *cs = CPU(cpu); |
0e60a699 AG |
394 | struct kvm_s390_interrupt kvmint; |
395 | int r; | |
396 | ||
a60f24b5 | 397 | if (!cs->kvm_state) { |
0e60a699 AG |
398 | return; |
399 | } | |
400 | ||
0e60a699 AG |
401 | kvmint.type = type; |
402 | kvmint.parm = parm; | |
403 | kvmint.parm64 = parm64; | |
404 | ||
405 | if (vm) { | |
a60f24b5 | 406 | r = kvm_vm_ioctl(cs->kvm_state, KVM_S390_INTERRUPT, &kvmint); |
0e60a699 | 407 | } else { |
1bc22652 | 408 | r = kvm_vcpu_ioctl(cs, KVM_S390_INTERRUPT, &kvmint); |
0e60a699 AG |
409 | } |
410 | ||
411 | if (r < 0) { | |
412 | fprintf(stderr, "KVM failed to inject interrupt\n"); | |
413 | exit(1); | |
414 | } | |
415 | } | |
416 | ||
1bc22652 | 417 | void kvm_s390_virtio_irq(S390CPU *cpu, int config_change, uint64_t token) |
0e60a699 | 418 | { |
1bc22652 | 419 | kvm_s390_interrupt_internal(cpu, KVM_S390_INT_VIRTIO, config_change, |
0e60a699 AG |
420 | token, 1); |
421 | } | |
422 | ||
1bc22652 | 423 | void kvm_s390_interrupt(S390CPU *cpu, int type, uint32_t code) |
0e60a699 | 424 | { |
1bc22652 | 425 | kvm_s390_interrupt_internal(cpu, type, code, 0, 0); |
0e60a699 AG |
426 | } |
427 | ||
1bc22652 | 428 | static void enter_pgmcheck(S390CPU *cpu, uint16_t code) |
0e60a699 | 429 | { |
1bc22652 | 430 | kvm_s390_interrupt(cpu, KVM_S390_PROGRAM_INT, code); |
0e60a699 AG |
431 | } |
432 | ||
f7575c96 | 433 | static inline void setcc(S390CPU *cpu, uint64_t cc) |
0e60a699 | 434 | { |
f7575c96 AF |
435 | CPUS390XState *env = &cpu->env; |
436 | CPUState *cs = CPU(cpu); | |
437 | ||
438 | cs->kvm_run->psw_mask &= ~(3ull << 44); | |
439 | cs->kvm_run->psw_mask |= (cc & 3) << 44; | |
0e60a699 AG |
440 | |
441 | env->psw.mask &= ~(3ul << 44); | |
442 | env->psw.mask |= (cc & 3) << 44; | |
443 | } | |
444 | ||
1bc22652 | 445 | static int kvm_sclp_service_call(S390CPU *cpu, struct kvm_run *run, |
bcec36ea | 446 | uint16_t ipbh0) |
0e60a699 | 447 | { |
1bc22652 | 448 | CPUS390XState *env = &cpu->env; |
0e60a699 AG |
449 | uint32_t sccb; |
450 | uint64_t code; | |
451 | int r = 0; | |
452 | ||
453 | cpu_synchronize_state(env); | |
454 | sccb = env->regs[ipbh0 & 0xf]; | |
455 | code = env->regs[(ipbh0 & 0xf0) >> 4]; | |
456 | ||
f6c98f92 | 457 | r = sclp_service_call(sccb, code); |
9abf567d | 458 | if (r < 0) { |
1bc22652 | 459 | enter_pgmcheck(cpu, -r); |
0e60a699 | 460 | } |
f7575c96 | 461 | setcc(cpu, r); |
81f7c56c | 462 | |
0e60a699 AG |
463 | return 0; |
464 | } | |
465 | ||
09b99878 CH |
466 | static int kvm_handle_css_inst(S390CPU *cpu, struct kvm_run *run, |
467 | uint8_t ipa0, uint8_t ipa1, uint8_t ipb) | |
468 | { | |
469 | int r = 0; | |
470 | int no_cc = 0; | |
471 | CPUS390XState *env = &cpu->env; | |
3474b679 | 472 | CPUState *cs = ENV_GET_CPU(env); |
09b99878 CH |
473 | |
474 | if (ipa0 != 0xb2) { | |
475 | /* Not handled for now. */ | |
476 | return -1; | |
477 | } | |
3474b679 JH |
478 | |
479 | kvm_s390_get_registers_partial(cs); | |
480 | cs->kvm_vcpu_dirty = true; | |
481 | ||
09b99878 CH |
482 | switch (ipa1) { |
483 | case PRIV_XSCH: | |
484 | r = ioinst_handle_xsch(env, env->regs[1]); | |
485 | break; | |
486 | case PRIV_CSCH: | |
487 | r = ioinst_handle_csch(env, env->regs[1]); | |
488 | break; | |
489 | case PRIV_HSCH: | |
490 | r = ioinst_handle_hsch(env, env->regs[1]); | |
491 | break; | |
492 | case PRIV_MSCH: | |
493 | r = ioinst_handle_msch(env, env->regs[1], run->s390_sieic.ipb); | |
494 | break; | |
495 | case PRIV_SSCH: | |
496 | r = ioinst_handle_ssch(env, env->regs[1], run->s390_sieic.ipb); | |
497 | break; | |
498 | case PRIV_STCRW: | |
499 | r = ioinst_handle_stcrw(env, run->s390_sieic.ipb); | |
500 | break; | |
501 | case PRIV_STSCH: | |
502 | r = ioinst_handle_stsch(env, env->regs[1], run->s390_sieic.ipb); | |
503 | break; | |
504 | case PRIV_TSCH: | |
505 | /* We should only get tsch via KVM_EXIT_S390_TSCH. */ | |
506 | fprintf(stderr, "Spurious tsch intercept\n"); | |
507 | break; | |
508 | case PRIV_CHSC: | |
509 | r = ioinst_handle_chsc(env, run->s390_sieic.ipb); | |
510 | break; | |
511 | case PRIV_TPI: | |
512 | /* This should have been handled by kvm already. */ | |
513 | fprintf(stderr, "Spurious tpi intercept\n"); | |
514 | break; | |
515 | case PRIV_SCHM: | |
516 | no_cc = 1; | |
517 | r = ioinst_handle_schm(env, env->regs[1], env->regs[2], | |
518 | run->s390_sieic.ipb); | |
519 | break; | |
520 | case PRIV_RSCH: | |
521 | r = ioinst_handle_rsch(env, env->regs[1]); | |
522 | break; | |
523 | case PRIV_RCHP: | |
524 | r = ioinst_handle_rchp(env, env->regs[1]); | |
525 | break; | |
526 | case PRIV_STCPS: | |
527 | /* We do not provide this instruction, it is suppressed. */ | |
528 | no_cc = 1; | |
529 | r = 0; | |
530 | break; | |
531 | case PRIV_SAL: | |
532 | no_cc = 1; | |
533 | r = ioinst_handle_sal(env, env->regs[1]); | |
534 | break; | |
535 | default: | |
536 | r = -1; | |
537 | break; | |
538 | } | |
539 | ||
540 | if (r >= 0) { | |
541 | if (!no_cc) { | |
542 | setcc(cpu, r); | |
543 | } | |
544 | r = 0; | |
545 | } else if (r < -1) { | |
546 | r = 0; | |
547 | } | |
548 | return r; | |
549 | } | |
550 | ||
551 | static int is_ioinst(uint8_t ipa0, uint8_t ipa1, uint8_t ipb) | |
552 | { | |
553 | int ret = 0; | |
554 | uint16_t ipa = (ipa0 << 8) | ipa1; | |
555 | ||
556 | switch (ipa) { | |
557 | case IPA0_B2 | PRIV_CSCH: | |
558 | case IPA0_B2 | PRIV_HSCH: | |
559 | case IPA0_B2 | PRIV_MSCH: | |
560 | case IPA0_B2 | PRIV_SSCH: | |
561 | case IPA0_B2 | PRIV_STSCH: | |
562 | case IPA0_B2 | PRIV_TPI: | |
563 | case IPA0_B2 | PRIV_SAL: | |
564 | case IPA0_B2 | PRIV_RSCH: | |
565 | case IPA0_B2 | PRIV_STCRW: | |
566 | case IPA0_B2 | PRIV_STCPS: | |
567 | case IPA0_B2 | PRIV_RCHP: | |
568 | case IPA0_B2 | PRIV_SCHM: | |
569 | case IPA0_B2 | PRIV_CHSC: | |
570 | case IPA0_B2 | PRIV_SIGA: | |
571 | case IPA0_B2 | PRIV_XSCH: | |
572 | case IPA0_B9 | PRIV_EQBS: | |
573 | case IPA0_EB | PRIV_SQBS: | |
574 | ret = 1; | |
575 | break; | |
576 | } | |
577 | ||
578 | return ret; | |
579 | } | |
580 | ||
581 | static int handle_priv(S390CPU *cpu, struct kvm_run *run, | |
582 | uint8_t ipa0, uint8_t ipa1) | |
0e60a699 AG |
583 | { |
584 | int r = 0; | |
585 | uint16_t ipbh0 = (run->s390_sieic.ipb & 0xffff0000) >> 16; | |
09b99878 | 586 | uint8_t ipb = run->s390_sieic.ipb & 0xff; |
0e60a699 AG |
587 | |
588 | dprintf("KVM: PRIV: %d\n", ipa1); | |
589 | switch (ipa1) { | |
590 | case PRIV_SCLP_CALL: | |
1bc22652 | 591 | r = kvm_sclp_service_call(cpu, run, ipbh0); |
0e60a699 AG |
592 | break; |
593 | default: | |
09b99878 CH |
594 | if (is_ioinst(ipa0, ipa1, ipb)) { |
595 | r = kvm_handle_css_inst(cpu, run, ipa0, ipa1, ipb); | |
596 | if (r == -1) { | |
597 | setcc(cpu, 3); | |
598 | r = 0; | |
599 | } | |
600 | } else { | |
601 | dprintf("KVM: unknown PRIV: 0x%x\n", ipa1); | |
602 | r = -1; | |
603 | } | |
0e60a699 AG |
604 | break; |
605 | } | |
606 | ||
607 | return r; | |
608 | } | |
609 | ||
a4e3ad19 | 610 | static int handle_hypercall(CPUS390XState *env, struct kvm_run *run) |
0e60a699 | 611 | { |
3474b679 JH |
612 | CPUState *cs = ENV_GET_CPU(env); |
613 | ||
614 | kvm_s390_get_registers_partial(cs); | |
615 | cs->kvm_vcpu_dirty = true; | |
28e942f8 | 616 | env->regs[2] = s390_virtio_hypercall(env); |
0e60a699 | 617 | |
bcec36ea | 618 | return 0; |
0e60a699 AG |
619 | } |
620 | ||
a4e3ad19 | 621 | static int handle_diag(CPUS390XState *env, struct kvm_run *run, int ipb_code) |
0e60a699 AG |
622 | { |
623 | int r = 0; | |
624 | ||
625 | switch (ipb_code) { | |
626 | case DIAG_KVM_HYPERCALL: | |
627 | r = handle_hypercall(env, run); | |
628 | break; | |
629 | case DIAG_KVM_BREAKPOINT: | |
630 | sleep(10); | |
631 | break; | |
632 | default: | |
633 | dprintf("KVM: unknown DIAG: 0x%x\n", ipb_code); | |
634 | r = -1; | |
635 | break; | |
636 | } | |
637 | ||
638 | return r; | |
639 | } | |
640 | ||
3edb8f92 | 641 | static int s390_cpu_restart(S390CPU *cpu) |
0e60a699 | 642 | { |
1bc22652 | 643 | kvm_s390_interrupt(cpu, KVM_S390_RESTART, 0); |
49e15878 | 644 | s390_add_running_cpu(cpu); |
c08d7424 | 645 | qemu_cpu_kick(CPU(cpu)); |
49e15878 | 646 | dprintf("DONE: SIGP cpu restart: %p\n", &cpu->env); |
0e60a699 AG |
647 | return 0; |
648 | } | |
649 | ||
a4e3ad19 | 650 | static int s390_store_status(CPUS390XState *env, uint32_t parameter) |
0e60a699 AG |
651 | { |
652 | /* XXX */ | |
653 | fprintf(stderr, "XXX SIGP store status\n"); | |
654 | return -1; | |
655 | } | |
656 | ||
1bc22652 | 657 | static int s390_cpu_initial_reset(S390CPU *cpu) |
0e60a699 | 658 | { |
1bc22652 | 659 | CPUS390XState *env = &cpu->env; |
d5900813 AG |
660 | int i; |
661 | ||
49e15878 | 662 | s390_del_running_cpu(cpu); |
1bc22652 | 663 | if (kvm_vcpu_ioctl(CPU(cpu), KVM_S390_INITIAL_RESET, NULL) < 0) { |
d5900813 AG |
664 | perror("cannot init reset vcpu"); |
665 | } | |
666 | ||
667 | /* Manually zero out all registers */ | |
668 | cpu_synchronize_state(env); | |
669 | for (i = 0; i < 16; i++) { | |
670 | env->regs[i] = 0; | |
671 | } | |
672 | ||
673 | dprintf("DONE: SIGP initial reset: %p\n", env); | |
674 | return 0; | |
0e60a699 AG |
675 | } |
676 | ||
f7575c96 | 677 | static int handle_sigp(S390CPU *cpu, struct kvm_run *run, uint8_t ipa1) |
0e60a699 | 678 | { |
f7575c96 | 679 | CPUS390XState *env = &cpu->env; |
0e60a699 AG |
680 | uint8_t order_code; |
681 | uint32_t parameter; | |
682 | uint16_t cpu_addr; | |
683 | uint8_t t; | |
684 | int r = -1; | |
45fa769b | 685 | S390CPU *target_cpu; |
a4e3ad19 | 686 | CPUS390XState *target_env; |
0e60a699 AG |
687 | |
688 | cpu_synchronize_state(env); | |
689 | ||
690 | /* get order code */ | |
691 | order_code = run->s390_sieic.ipb >> 28; | |
692 | if (order_code > 0) { | |
693 | order_code = env->regs[order_code]; | |
694 | } | |
695 | order_code += (run->s390_sieic.ipb & 0x0fff0000) >> 16; | |
696 | ||
697 | /* get parameters */ | |
698 | t = (ipa1 & 0xf0) >> 4; | |
699 | if (!(t % 2)) { | |
700 | t++; | |
701 | } | |
702 | ||
703 | parameter = env->regs[t] & 0x7ffffe00; | |
704 | cpu_addr = env->regs[ipa1 & 0x0f]; | |
705 | ||
45fa769b AF |
706 | target_cpu = s390_cpu_addr2state(cpu_addr); |
707 | if (target_cpu == NULL) { | |
0e60a699 AG |
708 | goto out; |
709 | } | |
45fa769b | 710 | target_env = &target_cpu->env; |
0e60a699 AG |
711 | |
712 | switch (order_code) { | |
713 | case SIGP_RESTART: | |
3edb8f92 | 714 | r = s390_cpu_restart(target_cpu); |
0e60a699 AG |
715 | break; |
716 | case SIGP_STORE_STATUS_ADDR: | |
717 | r = s390_store_status(target_env, parameter); | |
718 | break; | |
719 | case SIGP_SET_ARCH: | |
720 | /* make the caller panic */ | |
721 | return -1; | |
722 | case SIGP_INITIAL_CPU_RESET: | |
1bc22652 | 723 | r = s390_cpu_initial_reset(target_cpu); |
0e60a699 AG |
724 | break; |
725 | default: | |
a74cdab4 | 726 | fprintf(stderr, "KVM: unknown SIGP: 0x%x\n", order_code); |
0e60a699 AG |
727 | break; |
728 | } | |
729 | ||
730 | out: | |
f7575c96 | 731 | setcc(cpu, r ? 3 : 0); |
0e60a699 AG |
732 | return 0; |
733 | } | |
734 | ||
1bc22652 | 735 | static int handle_instruction(S390CPU *cpu, struct kvm_run *run) |
0e60a699 | 736 | { |
1bc22652 | 737 | CPUS390XState *env = &cpu->env; |
0e60a699 AG |
738 | unsigned int ipa0 = (run->s390_sieic.ipa & 0xff00); |
739 | uint8_t ipa1 = run->s390_sieic.ipa & 0x00ff; | |
740 | int ipb_code = (run->s390_sieic.ipb & 0x0fff0000) >> 16; | |
d7963c43 | 741 | int r = -1; |
0e60a699 AG |
742 | |
743 | dprintf("handle_instruction 0x%x 0x%x\n", run->s390_sieic.ipa, run->s390_sieic.ipb); | |
744 | switch (ipa0) { | |
09b99878 CH |
745 | case IPA0_B2: |
746 | case IPA0_B9: | |
747 | case IPA0_EB: | |
748 | r = handle_priv(cpu, run, ipa0 >> 8, ipa1); | |
749 | break; | |
750 | case IPA0_DIAG: | |
751 | r = handle_diag(env, run, ipb_code); | |
752 | break; | |
753 | case IPA0_SIGP: | |
754 | r = handle_sigp(cpu, run, ipa1); | |
755 | break; | |
0e60a699 AG |
756 | } |
757 | ||
758 | if (r < 0) { | |
1bc22652 | 759 | enter_pgmcheck(cpu, 0x0001); |
0e60a699 | 760 | } |
359507ee | 761 | return 0; |
0e60a699 AG |
762 | } |
763 | ||
f7575c96 | 764 | static bool is_special_wait_psw(CPUState *cs) |
eca3ed03 CB |
765 | { |
766 | /* signal quiesce */ | |
f7575c96 | 767 | return cs->kvm_run->psw_addr == 0xfffUL; |
eca3ed03 CB |
768 | } |
769 | ||
1bc22652 | 770 | static int handle_intercept(S390CPU *cpu) |
0e60a699 | 771 | { |
f7575c96 AF |
772 | CPUState *cs = CPU(cpu); |
773 | struct kvm_run *run = cs->kvm_run; | |
0e60a699 AG |
774 | int icpt_code = run->s390_sieic.icptcode; |
775 | int r = 0; | |
776 | ||
81f7c56c | 777 | dprintf("intercept: 0x%x (at 0x%lx)\n", icpt_code, |
f7575c96 | 778 | (long)cs->kvm_run->psw_addr); |
0e60a699 AG |
779 | switch (icpt_code) { |
780 | case ICPT_INSTRUCTION: | |
1bc22652 | 781 | r = handle_instruction(cpu, run); |
0e60a699 AG |
782 | break; |
783 | case ICPT_WAITPSW: | |
08eb8c85 CB |
784 | /* disabled wait, since enabled wait is handled in kernel */ |
785 | if (s390_del_running_cpu(cpu) == 0) { | |
786 | if (is_special_wait_psw(cs)) { | |
787 | qemu_system_shutdown_request(); | |
788 | } else { | |
789 | QObject *data; | |
790 | ||
791 | data = qobject_from_jsonf("{ 'action': %s }", "pause"); | |
792 | monitor_protocol_event(QEVENT_GUEST_PANICKED, data); | |
793 | qobject_decref(data); | |
794 | vm_stop(RUN_STATE_GUEST_PANICKED); | |
795 | } | |
eca3ed03 CB |
796 | } |
797 | r = EXCP_HALTED; | |
798 | break; | |
854e42f3 | 799 | case ICPT_CPU_STOP: |
49e15878 | 800 | if (s390_del_running_cpu(cpu) == 0) { |
854e42f3 CB |
801 | qemu_system_shutdown_request(); |
802 | } | |
803 | r = EXCP_HALTED; | |
0e60a699 AG |
804 | break; |
805 | case ICPT_SOFT_INTERCEPT: | |
806 | fprintf(stderr, "KVM unimplemented icpt SOFT\n"); | |
807 | exit(1); | |
808 | break; | |
0e60a699 AG |
809 | case ICPT_IO: |
810 | fprintf(stderr, "KVM unimplemented icpt IO\n"); | |
811 | exit(1); | |
812 | break; | |
813 | default: | |
814 | fprintf(stderr, "Unknown intercept code: %d\n", icpt_code); | |
815 | exit(1); | |
816 | break; | |
817 | } | |
818 | ||
819 | return r; | |
820 | } | |
821 | ||
09b99878 CH |
822 | static int handle_tsch(S390CPU *cpu) |
823 | { | |
824 | CPUS390XState *env = &cpu->env; | |
825 | CPUState *cs = CPU(cpu); | |
826 | struct kvm_run *run = cs->kvm_run; | |
827 | int ret; | |
828 | ||
3474b679 JH |
829 | kvm_s390_get_registers_partial(cs); |
830 | cs->kvm_vcpu_dirty = true; | |
831 | ||
09b99878 CH |
832 | ret = ioinst_handle_tsch(env, env->regs[1], run->s390_tsch.ipb); |
833 | if (ret >= 0) { | |
834 | /* Success; set condition code. */ | |
835 | setcc(cpu, ret); | |
836 | ret = 0; | |
837 | } else if (ret < -1) { | |
838 | /* | |
839 | * Failure. | |
840 | * If an I/O interrupt had been dequeued, we have to reinject it. | |
841 | */ | |
842 | if (run->s390_tsch.dequeued) { | |
843 | uint16_t subchannel_id = run->s390_tsch.subchannel_id; | |
844 | uint16_t subchannel_nr = run->s390_tsch.subchannel_nr; | |
845 | uint32_t io_int_parm = run->s390_tsch.io_int_parm; | |
846 | uint32_t io_int_word = run->s390_tsch.io_int_word; | |
847 | uint32_t type = ((subchannel_id & 0xff00) << 24) | | |
848 | ((subchannel_id & 0x00060) << 22) | (subchannel_nr << 16); | |
849 | ||
850 | kvm_s390_interrupt_internal(cpu, type, | |
851 | ((uint32_t)subchannel_id << 16) | |
852 | | subchannel_nr, | |
853 | ((uint64_t)io_int_parm << 32) | |
854 | | io_int_word, 1); | |
855 | } | |
856 | ret = 0; | |
857 | } | |
858 | return ret; | |
859 | } | |
860 | ||
20d695a9 | 861 | int kvm_arch_handle_exit(CPUState *cs, struct kvm_run *run) |
0e60a699 | 862 | { |
20d695a9 | 863 | S390CPU *cpu = S390_CPU(cs); |
0e60a699 AG |
864 | int ret = 0; |
865 | ||
866 | switch (run->exit_reason) { | |
867 | case KVM_EXIT_S390_SIEIC: | |
1bc22652 | 868 | ret = handle_intercept(cpu); |
0e60a699 AG |
869 | break; |
870 | case KVM_EXIT_S390_RESET: | |
add142e0 | 871 | qemu_system_reset_request(); |
0e60a699 | 872 | break; |
09b99878 CH |
873 | case KVM_EXIT_S390_TSCH: |
874 | ret = handle_tsch(cpu); | |
875 | break; | |
0e60a699 AG |
876 | default: |
877 | fprintf(stderr, "Unknown KVM exit: %d\n", run->exit_reason); | |
878 | break; | |
879 | } | |
880 | ||
bb4ea393 JK |
881 | if (ret == 0) { |
882 | ret = EXCP_INTERRUPT; | |
bb4ea393 | 883 | } |
0e60a699 AG |
884 | return ret; |
885 | } | |
4513d923 | 886 | |
20d695a9 | 887 | bool kvm_arch_stop_on_emulation_error(CPUState *cpu) |
4513d923 GN |
888 | { |
889 | return true; | |
890 | } | |
a1b87fe0 | 891 | |
20d695a9 | 892 | int kvm_arch_on_sigbus_vcpu(CPUState *cpu, int code, void *addr) |
a1b87fe0 JK |
893 | { |
894 | return 1; | |
895 | } | |
896 | ||
897 | int kvm_arch_on_sigbus(int code, void *addr) | |
898 | { | |
899 | return 1; | |
900 | } | |
09b99878 CH |
901 | |
902 | void kvm_s390_io_interrupt(S390CPU *cpu, uint16_t subchannel_id, | |
903 | uint16_t subchannel_nr, uint32_t io_int_parm, | |
904 | uint32_t io_int_word) | |
905 | { | |
906 | uint32_t type; | |
907 | ||
908 | type = ((subchannel_id & 0xff00) << 24) | | |
909 | ((subchannel_id & 0x00060) << 22) | (subchannel_nr << 16); | |
910 | kvm_s390_interrupt_internal(cpu, type, | |
911 | ((uint32_t)subchannel_id << 16) | subchannel_nr, | |
912 | ((uint64_t)io_int_parm << 32) | io_int_word, 1); | |
913 | } | |
914 | ||
915 | void kvm_s390_crw_mchk(S390CPU *cpu) | |
916 | { | |
917 | kvm_s390_interrupt_internal(cpu, KVM_S390_MCHK, 1 << 28, | |
918 | 0x00400f1d40330000, 1); | |
919 | } | |
920 | ||
921 | void kvm_s390_enable_css_support(S390CPU *cpu) | |
922 | { | |
923 | struct kvm_enable_cap cap = {}; | |
924 | int r; | |
925 | ||
926 | /* Activate host kernel channel subsystem support. */ | |
927 | cap.cap = KVM_CAP_S390_CSS_SUPPORT; | |
928 | r = kvm_vcpu_ioctl(CPU(cpu), KVM_ENABLE_CAP, &cap); | |
929 | assert(r == 0); | |
930 | } |