]>
Commit | Line | Data |
---|---|---|
0e60a699 AG |
1 | /* |
2 | * QEMU S390x KVM implementation | |
3 | * | |
4 | * Copyright (c) 2009 Alexander Graf <agraf@suse.de> | |
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 | |
e67137c6 | 43 | #define DPRINTF(fmt, ...) \ |
0e60a699 AG |
44 | do { fprintf(stderr, fmt, ## __VA_ARGS__); } while (0) |
45 | #else | |
e67137c6 | 46 | #define DPRINTF(fmt, ...) \ |
0e60a699 AG |
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 | |
268846ba | 75 | #define DIAG_IPL 0x308 |
0e60a699 AG |
76 | #define DIAG_KVM_HYPERCALL 0x500 |
77 | #define DIAG_KVM_BREAKPOINT 0x501 | |
78 | ||
0e60a699 AG |
79 | #define ICPT_INSTRUCTION 0x04 |
80 | #define ICPT_WAITPSW 0x1c | |
81 | #define ICPT_SOFT_INTERCEPT 0x24 | |
82 | #define ICPT_CPU_STOP 0x28 | |
83 | #define ICPT_IO 0x40 | |
84 | ||
94a8d39a JK |
85 | const KVMCapabilityInfo kvm_arch_required_capabilities[] = { |
86 | KVM_CAP_LAST_INFO | |
87 | }; | |
88 | ||
5b08b344 CB |
89 | static int cap_sync_regs; |
90 | ||
575ddeb4 | 91 | static void *legacy_s390_alloc(size_t size); |
91138037 | 92 | |
cad1e282 | 93 | int kvm_arch_init(KVMState *s) |
0e60a699 | 94 | { |
5b08b344 | 95 | cap_sync_regs = kvm_check_extension(s, KVM_CAP_SYNC_REGS); |
91138037 MA |
96 | if (!kvm_check_extension(s, KVM_CAP_S390_GMAP) |
97 | || !kvm_check_extension(s, KVM_CAP_S390_COW)) { | |
98 | phys_mem_set_alloc(legacy_s390_alloc); | |
99 | } | |
0e60a699 AG |
100 | return 0; |
101 | } | |
102 | ||
b164e48e EH |
103 | unsigned long kvm_arch_vcpu_id(CPUState *cpu) |
104 | { | |
105 | return cpu->cpu_index; | |
106 | } | |
107 | ||
20d695a9 | 108 | int kvm_arch_init_vcpu(CPUState *cpu) |
0e60a699 | 109 | { |
1c9d2a1d CB |
110 | /* nothing todo yet */ |
111 | return 0; | |
0e60a699 AG |
112 | } |
113 | ||
20d695a9 | 114 | void kvm_arch_reset_vcpu(CPUState *cpu) |
0e60a699 | 115 | { |
419831d7 AG |
116 | /* The initial reset call is needed here to reset in-kernel |
117 | * vcpu data that we can't access directly from QEMU | |
118 | * (i.e. with older kernels which don't support sync_regs/ONE_REG). | |
119 | * Before this ioctl cpu_synchronize_state() is called in common kvm | |
120 | * code (kvm-all) */ | |
70bada03 JF |
121 | if (kvm_vcpu_ioctl(cpu, KVM_S390_INITIAL_RESET, NULL)) { |
122 | perror("Can't reset vcpu\n"); | |
123 | } | |
0e60a699 AG |
124 | } |
125 | ||
20d695a9 | 126 | int kvm_arch_put_registers(CPUState *cs, int level) |
0e60a699 | 127 | { |
20d695a9 AF |
128 | S390CPU *cpu = S390_CPU(cs); |
129 | CPUS390XState *env = &cpu->env; | |
420840e5 | 130 | struct kvm_one_reg reg; |
5b08b344 | 131 | struct kvm_sregs sregs; |
0e60a699 AG |
132 | struct kvm_regs regs; |
133 | int ret; | |
134 | int i; | |
135 | ||
5b08b344 | 136 | /* always save the PSW and the GPRS*/ |
f7575c96 AF |
137 | cs->kvm_run->psw_addr = env->psw.addr; |
138 | cs->kvm_run->psw_mask = env->psw.mask; | |
0e60a699 | 139 | |
f7575c96 | 140 | if (cap_sync_regs && cs->kvm_run->kvm_valid_regs & KVM_SYNC_GPRS) { |
5b08b344 | 141 | for (i = 0; i < 16; i++) { |
f7575c96 AF |
142 | cs->kvm_run->s.regs.gprs[i] = env->regs[i]; |
143 | cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_GPRS; | |
5b08b344 CB |
144 | } |
145 | } else { | |
146 | for (i = 0; i < 16; i++) { | |
147 | regs.gprs[i] = env->regs[i]; | |
148 | } | |
1bc22652 | 149 | ret = kvm_vcpu_ioctl(cs, KVM_SET_REGS, ®s); |
5b08b344 CB |
150 | if (ret < 0) { |
151 | return ret; | |
152 | } | |
0e60a699 AG |
153 | } |
154 | ||
44c68de0 DD |
155 | /* Do we need to save more than that? */ |
156 | if (level == KVM_PUT_RUNTIME_STATE) { | |
157 | return 0; | |
158 | } | |
420840e5 | 159 | |
44c68de0 DD |
160 | reg.id = KVM_REG_S390_CPU_TIMER; |
161 | reg.addr = (__u64)&(env->cputm); | |
162 | ret = kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, ®); | |
163 | if (ret < 0) { | |
164 | return ret; | |
165 | } | |
420840e5 | 166 | |
44c68de0 DD |
167 | reg.id = KVM_REG_S390_CLOCK_COMP; |
168 | reg.addr = (__u64)&(env->ckc); | |
169 | ret = kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, ®); | |
170 | if (ret < 0) { | |
171 | return ret; | |
420840e5 | 172 | } |
420840e5 | 173 | |
44c68de0 DD |
174 | reg.id = KVM_REG_S390_TODPR; |
175 | reg.addr = (__u64)&(env->todpr); | |
176 | ret = kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, ®); | |
177 | if (ret < 0) { | |
178 | return ret; | |
0e60a699 AG |
179 | } |
180 | ||
5b08b344 | 181 | if (cap_sync_regs && |
f7575c96 AF |
182 | cs->kvm_run->kvm_valid_regs & KVM_SYNC_ACRS && |
183 | cs->kvm_run->kvm_valid_regs & KVM_SYNC_CRS) { | |
5b08b344 | 184 | for (i = 0; i < 16; i++) { |
f7575c96 AF |
185 | cs->kvm_run->s.regs.acrs[i] = env->aregs[i]; |
186 | cs->kvm_run->s.regs.crs[i] = env->cregs[i]; | |
5b08b344 | 187 | } |
f7575c96 AF |
188 | cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_ACRS; |
189 | cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_CRS; | |
5b08b344 CB |
190 | } else { |
191 | for (i = 0; i < 16; i++) { | |
192 | sregs.acrs[i] = env->aregs[i]; | |
193 | sregs.crs[i] = env->cregs[i]; | |
194 | } | |
1bc22652 | 195 | ret = kvm_vcpu_ioctl(cs, KVM_SET_SREGS, &sregs); |
5b08b344 CB |
196 | if (ret < 0) { |
197 | return ret; | |
198 | } | |
199 | } | |
0e60a699 | 200 | |
5b08b344 | 201 | /* Finally the prefix */ |
f7575c96 AF |
202 | if (cap_sync_regs && cs->kvm_run->kvm_valid_regs & KVM_SYNC_PREFIX) { |
203 | cs->kvm_run->s.regs.prefix = env->psa; | |
204 | cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_PREFIX; | |
5b08b344 CB |
205 | } else { |
206 | /* prefix is only supported via sync regs */ | |
207 | } | |
208 | return 0; | |
0e60a699 AG |
209 | } |
210 | ||
20d695a9 | 211 | int kvm_arch_get_registers(CPUState *cs) |
420840e5 JH |
212 | { |
213 | S390CPU *cpu = S390_CPU(cs); | |
214 | CPUS390XState *env = &cpu->env; | |
215 | struct kvm_one_reg reg; | |
5b08b344 | 216 | struct kvm_sregs sregs; |
0e60a699 | 217 | struct kvm_regs regs; |
44c68de0 | 218 | int i, r; |
420840e5 | 219 | |
5b08b344 | 220 | /* get the PSW */ |
f7575c96 AF |
221 | env->psw.addr = cs->kvm_run->psw_addr; |
222 | env->psw.mask = cs->kvm_run->psw_mask; | |
5b08b344 CB |
223 | |
224 | /* the GPRS */ | |
f7575c96 | 225 | if (cap_sync_regs && cs->kvm_run->kvm_valid_regs & KVM_SYNC_GPRS) { |
5b08b344 | 226 | for (i = 0; i < 16; i++) { |
f7575c96 | 227 | env->regs[i] = cs->kvm_run->s.regs.gprs[i]; |
5b08b344 CB |
228 | } |
229 | } else { | |
44c68de0 DD |
230 | r = kvm_vcpu_ioctl(cs, KVM_GET_REGS, ®s); |
231 | if (r < 0) { | |
232 | return r; | |
5b08b344 CB |
233 | } |
234 | for (i = 0; i < 16; i++) { | |
235 | env->regs[i] = regs.gprs[i]; | |
236 | } | |
0e60a699 AG |
237 | } |
238 | ||
5b08b344 CB |
239 | /* The ACRS and CRS */ |
240 | if (cap_sync_regs && | |
f7575c96 AF |
241 | cs->kvm_run->kvm_valid_regs & KVM_SYNC_ACRS && |
242 | cs->kvm_run->kvm_valid_regs & KVM_SYNC_CRS) { | |
5b08b344 | 243 | for (i = 0; i < 16; i++) { |
f7575c96 AF |
244 | env->aregs[i] = cs->kvm_run->s.regs.acrs[i]; |
245 | env->cregs[i] = cs->kvm_run->s.regs.crs[i]; | |
5b08b344 CB |
246 | } |
247 | } else { | |
44c68de0 DD |
248 | r = kvm_vcpu_ioctl(cs, KVM_GET_SREGS, &sregs); |
249 | if (r < 0) { | |
250 | return r; | |
5b08b344 CB |
251 | } |
252 | for (i = 0; i < 16; i++) { | |
253 | env->aregs[i] = sregs.acrs[i]; | |
254 | env->cregs[i] = sregs.crs[i]; | |
255 | } | |
0e60a699 AG |
256 | } |
257 | ||
44c68de0 | 258 | /* The prefix */ |
f7575c96 AF |
259 | if (cap_sync_regs && cs->kvm_run->kvm_valid_regs & KVM_SYNC_PREFIX) { |
260 | env->psa = cs->kvm_run->s.regs.prefix; | |
5b08b344 | 261 | } |
0e60a699 | 262 | |
44c68de0 DD |
263 | /* One Regs */ |
264 | reg.id = KVM_REG_S390_CPU_TIMER; | |
265 | reg.addr = (__u64)&(env->cputm); | |
266 | r = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, ®); | |
267 | if (r < 0) { | |
268 | return r; | |
269 | } | |
270 | ||
271 | reg.id = KVM_REG_S390_CLOCK_COMP; | |
272 | reg.addr = (__u64)&(env->ckc); | |
273 | r = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, ®); | |
274 | if (r < 0) { | |
275 | return r; | |
276 | } | |
277 | ||
278 | reg.id = KVM_REG_S390_TODPR; | |
279 | reg.addr = (__u64)&(env->todpr); | |
280 | r = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, ®); | |
281 | if (r < 0) { | |
282 | return r; | |
283 | } | |
284 | ||
0e60a699 AG |
285 | return 0; |
286 | } | |
287 | ||
fdec9918 CB |
288 | /* |
289 | * Legacy layout for s390: | |
290 | * Older S390 KVM requires the topmost vma of the RAM to be | |
291 | * smaller than an system defined value, which is at least 256GB. | |
292 | * Larger systems have larger values. We put the guest between | |
293 | * the end of data segment (system break) and this value. We | |
294 | * use 32GB as a base to have enough room for the system break | |
295 | * to grow. We also have to use MAP parameters that avoid | |
296 | * read-only mapping of guest pages. | |
297 | */ | |
575ddeb4 | 298 | static void *legacy_s390_alloc(size_t size) |
fdec9918 CB |
299 | { |
300 | void *mem; | |
301 | ||
302 | mem = mmap((void *) 0x800000000ULL, size, | |
303 | PROT_EXEC|PROT_READ|PROT_WRITE, | |
304 | MAP_SHARED | MAP_ANONYMOUS | MAP_FIXED, -1, 0); | |
39228250 | 305 | return mem == MAP_FAILED ? NULL : mem; |
fdec9918 CB |
306 | } |
307 | ||
20d695a9 | 308 | int kvm_arch_insert_sw_breakpoint(CPUState *cs, struct kvm_sw_breakpoint *bp) |
0e60a699 AG |
309 | { |
310 | static const uint8_t diag_501[] = {0x83, 0x24, 0x05, 0x01}; | |
311 | ||
9282b73a CB |
312 | if (cpu_memory_rw_debug(cs, bp->pc, (uint8_t *)&bp->saved_insn, 4, 0) || |
313 | cpu_memory_rw_debug(cs, bp->pc, (uint8_t *)diag_501, 4, 1)) { | |
0e60a699 AG |
314 | return -EINVAL; |
315 | } | |
316 | return 0; | |
317 | } | |
318 | ||
20d695a9 | 319 | int kvm_arch_remove_sw_breakpoint(CPUState *cs, struct kvm_sw_breakpoint *bp) |
0e60a699 AG |
320 | { |
321 | uint8_t t[4]; | |
322 | static const uint8_t diag_501[] = {0x83, 0x24, 0x05, 0x01}; | |
323 | ||
9282b73a | 324 | if (cpu_memory_rw_debug(cs, bp->pc, t, 4, 0)) { |
0e60a699 AG |
325 | return -EINVAL; |
326 | } else if (memcmp(t, diag_501, 4)) { | |
327 | return -EINVAL; | |
9282b73a | 328 | } else if (cpu_memory_rw_debug(cs, bp->pc, (uint8_t *)&bp->saved_insn, 1, 1)) { |
0e60a699 AG |
329 | return -EINVAL; |
330 | } | |
331 | ||
332 | return 0; | |
333 | } | |
334 | ||
20d695a9 | 335 | void kvm_arch_pre_run(CPUState *cpu, struct kvm_run *run) |
0e60a699 | 336 | { |
0e60a699 AG |
337 | } |
338 | ||
20d695a9 | 339 | void kvm_arch_post_run(CPUState *cpu, struct kvm_run *run) |
0e60a699 | 340 | { |
0e60a699 AG |
341 | } |
342 | ||
20d695a9 | 343 | int kvm_arch_process_async_events(CPUState *cs) |
0af691d7 | 344 | { |
225dc991 | 345 | return cs->halted; |
0af691d7 MT |
346 | } |
347 | ||
1bc22652 | 348 | void kvm_s390_interrupt_internal(S390CPU *cpu, int type, uint32_t parm, |
bcec36ea | 349 | uint64_t parm64, int vm) |
0e60a699 | 350 | { |
1bc22652 | 351 | CPUState *cs = CPU(cpu); |
0e60a699 AG |
352 | struct kvm_s390_interrupt kvmint; |
353 | int r; | |
354 | ||
a60f24b5 | 355 | if (!cs->kvm_state) { |
0e60a699 AG |
356 | return; |
357 | } | |
358 | ||
0e60a699 AG |
359 | kvmint.type = type; |
360 | kvmint.parm = parm; | |
361 | kvmint.parm64 = parm64; | |
362 | ||
363 | if (vm) { | |
a60f24b5 | 364 | r = kvm_vm_ioctl(cs->kvm_state, KVM_S390_INTERRUPT, &kvmint); |
0e60a699 | 365 | } else { |
1bc22652 | 366 | r = kvm_vcpu_ioctl(cs, KVM_S390_INTERRUPT, &kvmint); |
0e60a699 AG |
367 | } |
368 | ||
369 | if (r < 0) { | |
370 | fprintf(stderr, "KVM failed to inject interrupt\n"); | |
371 | exit(1); | |
372 | } | |
373 | } | |
374 | ||
1bc22652 | 375 | void kvm_s390_virtio_irq(S390CPU *cpu, int config_change, uint64_t token) |
0e60a699 | 376 | { |
1bc22652 | 377 | kvm_s390_interrupt_internal(cpu, KVM_S390_INT_VIRTIO, config_change, |
0e60a699 AG |
378 | token, 1); |
379 | } | |
380 | ||
1bc22652 | 381 | void kvm_s390_interrupt(S390CPU *cpu, int type, uint32_t code) |
0e60a699 | 382 | { |
1bc22652 | 383 | kvm_s390_interrupt_internal(cpu, type, code, 0, 0); |
0e60a699 AG |
384 | } |
385 | ||
1bc22652 | 386 | static void enter_pgmcheck(S390CPU *cpu, uint16_t code) |
0e60a699 | 387 | { |
1bc22652 | 388 | kvm_s390_interrupt(cpu, KVM_S390_PROGRAM_INT, code); |
0e60a699 AG |
389 | } |
390 | ||
1bc22652 | 391 | static int kvm_sclp_service_call(S390CPU *cpu, struct kvm_run *run, |
bcec36ea | 392 | uint16_t ipbh0) |
0e60a699 | 393 | { |
1bc22652 | 394 | CPUS390XState *env = &cpu->env; |
0e60a699 AG |
395 | uint32_t sccb; |
396 | uint64_t code; | |
397 | int r = 0; | |
398 | ||
cb446eca | 399 | cpu_synchronize_state(CPU(cpu)); |
3ac85fb6 TH |
400 | if (env->psw.mask & PSW_MASK_PSTATE) { |
401 | enter_pgmcheck(cpu, PGM_PRIVILEGED); | |
402 | return 0; | |
403 | } | |
0e60a699 AG |
404 | sccb = env->regs[ipbh0 & 0xf]; |
405 | code = env->regs[(ipbh0 & 0xf0) >> 4]; | |
406 | ||
f6c98f92 | 407 | r = sclp_service_call(sccb, code); |
9abf567d | 408 | if (r < 0) { |
1bc22652 | 409 | enter_pgmcheck(cpu, -r); |
0e60a699 | 410 | } |
f7575c96 | 411 | setcc(cpu, r); |
81f7c56c | 412 | |
0e60a699 AG |
413 | return 0; |
414 | } | |
415 | ||
09b99878 CH |
416 | static int kvm_handle_css_inst(S390CPU *cpu, struct kvm_run *run, |
417 | uint8_t ipa0, uint8_t ipa1, uint8_t ipb) | |
418 | { | |
09b99878 CH |
419 | CPUS390XState *env = &cpu->env; |
420 | ||
421 | if (ipa0 != 0xb2) { | |
422 | /* Not handled for now. */ | |
423 | return -1; | |
424 | } | |
3474b679 | 425 | |
44c68de0 | 426 | cpu_synchronize_state(CPU(cpu)); |
3474b679 | 427 | |
09b99878 CH |
428 | switch (ipa1) { |
429 | case PRIV_XSCH: | |
5d9bf1c0 | 430 | ioinst_handle_xsch(cpu, env->regs[1]); |
09b99878 CH |
431 | break; |
432 | case PRIV_CSCH: | |
5d9bf1c0 | 433 | ioinst_handle_csch(cpu, env->regs[1]); |
09b99878 CH |
434 | break; |
435 | case PRIV_HSCH: | |
5d9bf1c0 | 436 | ioinst_handle_hsch(cpu, env->regs[1]); |
09b99878 CH |
437 | break; |
438 | case PRIV_MSCH: | |
5d9bf1c0 | 439 | ioinst_handle_msch(cpu, env->regs[1], run->s390_sieic.ipb); |
09b99878 CH |
440 | break; |
441 | case PRIV_SSCH: | |
5d9bf1c0 | 442 | ioinst_handle_ssch(cpu, env->regs[1], run->s390_sieic.ipb); |
09b99878 CH |
443 | break; |
444 | case PRIV_STCRW: | |
5d9bf1c0 | 445 | ioinst_handle_stcrw(cpu, run->s390_sieic.ipb); |
09b99878 CH |
446 | break; |
447 | case PRIV_STSCH: | |
5d9bf1c0 | 448 | ioinst_handle_stsch(cpu, env->regs[1], run->s390_sieic.ipb); |
09b99878 CH |
449 | break; |
450 | case PRIV_TSCH: | |
451 | /* We should only get tsch via KVM_EXIT_S390_TSCH. */ | |
452 | fprintf(stderr, "Spurious tsch intercept\n"); | |
453 | break; | |
454 | case PRIV_CHSC: | |
5d9bf1c0 | 455 | ioinst_handle_chsc(cpu, run->s390_sieic.ipb); |
09b99878 CH |
456 | break; |
457 | case PRIV_TPI: | |
458 | /* This should have been handled by kvm already. */ | |
459 | fprintf(stderr, "Spurious tpi intercept\n"); | |
460 | break; | |
461 | case PRIV_SCHM: | |
5d9bf1c0 TH |
462 | ioinst_handle_schm(cpu, env->regs[1], env->regs[2], |
463 | run->s390_sieic.ipb); | |
09b99878 CH |
464 | break; |
465 | case PRIV_RSCH: | |
5d9bf1c0 | 466 | ioinst_handle_rsch(cpu, env->regs[1]); |
09b99878 CH |
467 | break; |
468 | case PRIV_RCHP: | |
5d9bf1c0 | 469 | ioinst_handle_rchp(cpu, env->regs[1]); |
09b99878 CH |
470 | break; |
471 | case PRIV_STCPS: | |
472 | /* We do not provide this instruction, it is suppressed. */ | |
09b99878 CH |
473 | break; |
474 | case PRIV_SAL: | |
5d9bf1c0 | 475 | ioinst_handle_sal(cpu, env->regs[1]); |
09b99878 | 476 | break; |
c1e8dfb5 TH |
477 | case PRIV_SIGA: |
478 | /* Not provided, set CC = 3 for subchannel not operational */ | |
5d9bf1c0 | 479 | setcc(cpu, 3); |
09b99878 | 480 | break; |
c1e8dfb5 TH |
481 | default: |
482 | return -1; | |
09b99878 CH |
483 | } |
484 | ||
c1e8dfb5 | 485 | return 0; |
09b99878 CH |
486 | } |
487 | ||
488 | static int handle_priv(S390CPU *cpu, struct kvm_run *run, | |
489 | uint8_t ipa0, uint8_t ipa1) | |
0e60a699 AG |
490 | { |
491 | int r = 0; | |
492 | uint16_t ipbh0 = (run->s390_sieic.ipb & 0xffff0000) >> 16; | |
09b99878 | 493 | uint8_t ipb = run->s390_sieic.ipb & 0xff; |
0e60a699 | 494 | |
e67137c6 | 495 | DPRINTF("KVM: PRIV: %d\n", ipa1); |
0e60a699 AG |
496 | switch (ipa1) { |
497 | case PRIV_SCLP_CALL: | |
1bc22652 | 498 | r = kvm_sclp_service_call(cpu, run, ipbh0); |
0e60a699 AG |
499 | break; |
500 | default: | |
c1e8dfb5 TH |
501 | r = kvm_handle_css_inst(cpu, run, ipa0, ipa1, ipb); |
502 | if (r == -1) { | |
503 | DPRINTF("KVM: unhandled PRIV: 0x%x\n", ipa1); | |
09b99878 | 504 | } |
0e60a699 AG |
505 | break; |
506 | } | |
507 | ||
508 | return r; | |
509 | } | |
510 | ||
4fd6dd06 | 511 | static int handle_hypercall(S390CPU *cpu, struct kvm_run *run) |
0e60a699 | 512 | { |
4fd6dd06 | 513 | CPUS390XState *env = &cpu->env; |
3474b679 | 514 | |
44c68de0 | 515 | cpu_synchronize_state(CPU(cpu)); |
28e942f8 | 516 | env->regs[2] = s390_virtio_hypercall(env); |
0e60a699 | 517 | |
bcec36ea | 518 | return 0; |
0e60a699 AG |
519 | } |
520 | ||
268846ba ED |
521 | static void kvm_handle_diag_308(S390CPU *cpu, struct kvm_run *run) |
522 | { | |
523 | uint64_t r1, r3; | |
524 | ||
525 | cpu_synchronize_state(CPU(cpu)); | |
526 | r1 = (run->s390_sieic.ipa & 0x00f0) >> 8; | |
527 | r3 = run->s390_sieic.ipa & 0x000f; | |
528 | handle_diag_308(&cpu->env, r1, r3); | |
529 | } | |
530 | ||
638129ff CH |
531 | #define DIAG_KVM_CODE_MASK 0x000000000000ffff |
532 | ||
533 | static int handle_diag(S390CPU *cpu, struct kvm_run *run, uint32_t ipb) | |
0e60a699 AG |
534 | { |
535 | int r = 0; | |
638129ff CH |
536 | uint16_t func_code; |
537 | ||
538 | /* | |
539 | * For any diagnose call we support, bits 48-63 of the resulting | |
540 | * address specify the function code; the remainder is ignored. | |
541 | */ | |
542 | func_code = decode_basedisp_rs(&cpu->env, ipb) & DIAG_KVM_CODE_MASK; | |
543 | switch (func_code) { | |
268846ba ED |
544 | case DIAG_IPL: |
545 | kvm_handle_diag_308(cpu, run); | |
546 | break; | |
39fbc5c6 CB |
547 | case DIAG_KVM_HYPERCALL: |
548 | r = handle_hypercall(cpu, run); | |
549 | break; | |
550 | case DIAG_KVM_BREAKPOINT: | |
551 | sleep(10); | |
552 | break; | |
553 | default: | |
638129ff | 554 | DPRINTF("KVM: unknown DIAG: 0x%x\n", func_code); |
39fbc5c6 CB |
555 | r = -1; |
556 | break; | |
0e60a699 AG |
557 | } |
558 | ||
559 | return r; | |
560 | } | |
561 | ||
b20a461f TH |
562 | static int kvm_s390_cpu_start(S390CPU *cpu) |
563 | { | |
564 | s390_add_running_cpu(cpu); | |
565 | qemu_cpu_kick(CPU(cpu)); | |
566 | DPRINTF("DONE: KVM cpu start: %p\n", &cpu->env); | |
567 | return 0; | |
568 | } | |
569 | ||
7f7f9752 | 570 | int kvm_s390_cpu_restart(S390CPU *cpu) |
0e60a699 | 571 | { |
1bc22652 | 572 | kvm_s390_interrupt(cpu, KVM_S390_RESTART, 0); |
49e15878 | 573 | s390_add_running_cpu(cpu); |
c08d7424 | 574 | qemu_cpu_kick(CPU(cpu)); |
7f7f9752 | 575 | DPRINTF("DONE: KVM cpu restart: %p\n", &cpu->env); |
0e60a699 AG |
576 | return 0; |
577 | } | |
578 | ||
1bc22652 | 579 | static int s390_cpu_initial_reset(S390CPU *cpu) |
0e60a699 | 580 | { |
cb446eca | 581 | CPUState *cs = CPU(cpu); |
1bc22652 | 582 | CPUS390XState *env = &cpu->env; |
d5900813 AG |
583 | int i; |
584 | ||
49e15878 | 585 | s390_del_running_cpu(cpu); |
cb446eca | 586 | if (kvm_vcpu_ioctl(cs, KVM_S390_INITIAL_RESET, NULL) < 0) { |
d5900813 AG |
587 | perror("cannot init reset vcpu"); |
588 | } | |
589 | ||
590 | /* Manually zero out all registers */ | |
cb446eca | 591 | cpu_synchronize_state(cs); |
d5900813 AG |
592 | for (i = 0; i < 16; i++) { |
593 | env->regs[i] = 0; | |
594 | } | |
595 | ||
e67137c6 | 596 | DPRINTF("DONE: SIGP initial reset: %p\n", env); |
d5900813 | 597 | return 0; |
0e60a699 AG |
598 | } |
599 | ||
b8031adb TH |
600 | #define SIGP_ORDER_MASK 0x000000ff |
601 | ||
f7575c96 | 602 | static int handle_sigp(S390CPU *cpu, struct kvm_run *run, uint8_t ipa1) |
0e60a699 | 603 | { |
f7575c96 | 604 | CPUS390XState *env = &cpu->env; |
0e60a699 | 605 | uint8_t order_code; |
0e60a699 | 606 | uint16_t cpu_addr; |
45fa769b | 607 | S390CPU *target_cpu; |
3796f0e1 TH |
608 | uint64_t *statusreg = &env->regs[ipa1 >> 4]; |
609 | int cc; | |
0e60a699 | 610 | |
cb446eca | 611 | cpu_synchronize_state(CPU(cpu)); |
0e60a699 AG |
612 | |
613 | /* get order code */ | |
b8031adb | 614 | order_code = decode_basedisp_rs(env, run->s390_sieic.ipb) & SIGP_ORDER_MASK; |
0e60a699 | 615 | |
0e60a699 | 616 | cpu_addr = env->regs[ipa1 & 0x0f]; |
45fa769b AF |
617 | target_cpu = s390_cpu_addr2state(cpu_addr); |
618 | if (target_cpu == NULL) { | |
3796f0e1 | 619 | cc = 3; /* not operational */ |
0e60a699 AG |
620 | goto out; |
621 | } | |
622 | ||
623 | switch (order_code) { | |
b20a461f | 624 | case SIGP_START: |
3796f0e1 | 625 | cc = kvm_s390_cpu_start(target_cpu); |
b20a461f | 626 | break; |
0b9972a2 | 627 | case SIGP_RESTART: |
3796f0e1 | 628 | cc = kvm_s390_cpu_restart(target_cpu); |
0b9972a2 TH |
629 | break; |
630 | case SIGP_SET_ARCH: | |
631 | /* make the caller panic */ | |
632 | return -1; | |
633 | case SIGP_INITIAL_CPU_RESET: | |
3796f0e1 | 634 | cc = s390_cpu_initial_reset(target_cpu); |
0b9972a2 TH |
635 | break; |
636 | default: | |
3796f0e1 TH |
637 | DPRINTF("KVM: unknown SIGP: 0x%x\n", order_code); |
638 | *statusreg &= 0xffffffff00000000UL; | |
639 | *statusreg |= SIGP_STAT_INVALID_ORDER; | |
640 | cc = 1; /* status stored */ | |
0b9972a2 | 641 | break; |
0e60a699 AG |
642 | } |
643 | ||
644 | out: | |
3796f0e1 | 645 | setcc(cpu, cc); |
0e60a699 AG |
646 | return 0; |
647 | } | |
648 | ||
d2ee7746 | 649 | static void handle_instruction(S390CPU *cpu, struct kvm_run *run) |
0e60a699 AG |
650 | { |
651 | unsigned int ipa0 = (run->s390_sieic.ipa & 0xff00); | |
652 | uint8_t ipa1 = run->s390_sieic.ipa & 0x00ff; | |
d7963c43 | 653 | int r = -1; |
0e60a699 | 654 | |
e67137c6 PM |
655 | DPRINTF("handle_instruction 0x%x 0x%x\n", |
656 | run->s390_sieic.ipa, run->s390_sieic.ipb); | |
0e60a699 | 657 | switch (ipa0) { |
09b99878 CH |
658 | case IPA0_B2: |
659 | case IPA0_B9: | |
660 | case IPA0_EB: | |
661 | r = handle_priv(cpu, run, ipa0 >> 8, ipa1); | |
662 | break; | |
663 | case IPA0_DIAG: | |
638129ff | 664 | r = handle_diag(cpu, run, run->s390_sieic.ipb); |
09b99878 CH |
665 | break; |
666 | case IPA0_SIGP: | |
667 | r = handle_sigp(cpu, run, ipa1); | |
668 | break; | |
0e60a699 AG |
669 | } |
670 | ||
671 | if (r < 0) { | |
1bc22652 | 672 | enter_pgmcheck(cpu, 0x0001); |
0e60a699 | 673 | } |
0e60a699 AG |
674 | } |
675 | ||
f7575c96 | 676 | static bool is_special_wait_psw(CPUState *cs) |
eca3ed03 CB |
677 | { |
678 | /* signal quiesce */ | |
f7575c96 | 679 | return cs->kvm_run->psw_addr == 0xfffUL; |
eca3ed03 CB |
680 | } |
681 | ||
1bc22652 | 682 | static int handle_intercept(S390CPU *cpu) |
0e60a699 | 683 | { |
f7575c96 AF |
684 | CPUState *cs = CPU(cpu); |
685 | struct kvm_run *run = cs->kvm_run; | |
0e60a699 AG |
686 | int icpt_code = run->s390_sieic.icptcode; |
687 | int r = 0; | |
688 | ||
e67137c6 | 689 | DPRINTF("intercept: 0x%x (at 0x%lx)\n", icpt_code, |
f7575c96 | 690 | (long)cs->kvm_run->psw_addr); |
0e60a699 AG |
691 | switch (icpt_code) { |
692 | case ICPT_INSTRUCTION: | |
d2ee7746 | 693 | handle_instruction(cpu, run); |
0e60a699 AG |
694 | break; |
695 | case ICPT_WAITPSW: | |
08eb8c85 CB |
696 | /* disabled wait, since enabled wait is handled in kernel */ |
697 | if (s390_del_running_cpu(cpu) == 0) { | |
698 | if (is_special_wait_psw(cs)) { | |
699 | qemu_system_shutdown_request(); | |
700 | } else { | |
701 | QObject *data; | |
702 | ||
703 | data = qobject_from_jsonf("{ 'action': %s }", "pause"); | |
704 | monitor_protocol_event(QEVENT_GUEST_PANICKED, data); | |
705 | qobject_decref(data); | |
706 | vm_stop(RUN_STATE_GUEST_PANICKED); | |
707 | } | |
eca3ed03 CB |
708 | } |
709 | r = EXCP_HALTED; | |
710 | break; | |
854e42f3 | 711 | case ICPT_CPU_STOP: |
49e15878 | 712 | if (s390_del_running_cpu(cpu) == 0) { |
854e42f3 CB |
713 | qemu_system_shutdown_request(); |
714 | } | |
715 | r = EXCP_HALTED; | |
0e60a699 AG |
716 | break; |
717 | case ICPT_SOFT_INTERCEPT: | |
718 | fprintf(stderr, "KVM unimplemented icpt SOFT\n"); | |
719 | exit(1); | |
720 | break; | |
0e60a699 AG |
721 | case ICPT_IO: |
722 | fprintf(stderr, "KVM unimplemented icpt IO\n"); | |
723 | exit(1); | |
724 | break; | |
725 | default: | |
726 | fprintf(stderr, "Unknown intercept code: %d\n", icpt_code); | |
727 | exit(1); | |
728 | break; | |
729 | } | |
730 | ||
731 | return r; | |
732 | } | |
733 | ||
09b99878 CH |
734 | static int handle_tsch(S390CPU *cpu) |
735 | { | |
736 | CPUS390XState *env = &cpu->env; | |
737 | CPUState *cs = CPU(cpu); | |
738 | struct kvm_run *run = cs->kvm_run; | |
739 | int ret; | |
740 | ||
44c68de0 | 741 | cpu_synchronize_state(cs); |
3474b679 | 742 | |
09b99878 CH |
743 | ret = ioinst_handle_tsch(env, env->regs[1], run->s390_tsch.ipb); |
744 | if (ret >= 0) { | |
745 | /* Success; set condition code. */ | |
746 | setcc(cpu, ret); | |
747 | ret = 0; | |
748 | } else if (ret < -1) { | |
749 | /* | |
750 | * Failure. | |
751 | * If an I/O interrupt had been dequeued, we have to reinject it. | |
752 | */ | |
753 | if (run->s390_tsch.dequeued) { | |
754 | uint16_t subchannel_id = run->s390_tsch.subchannel_id; | |
755 | uint16_t subchannel_nr = run->s390_tsch.subchannel_nr; | |
756 | uint32_t io_int_parm = run->s390_tsch.io_int_parm; | |
757 | uint32_t io_int_word = run->s390_tsch.io_int_word; | |
758 | uint32_t type = ((subchannel_id & 0xff00) << 24) | | |
759 | ((subchannel_id & 0x00060) << 22) | (subchannel_nr << 16); | |
760 | ||
761 | kvm_s390_interrupt_internal(cpu, type, | |
762 | ((uint32_t)subchannel_id << 16) | |
763 | | subchannel_nr, | |
764 | ((uint64_t)io_int_parm << 32) | |
765 | | io_int_word, 1); | |
766 | } | |
767 | ret = 0; | |
768 | } | |
769 | return ret; | |
770 | } | |
771 | ||
20d695a9 | 772 | int kvm_arch_handle_exit(CPUState *cs, struct kvm_run *run) |
0e60a699 | 773 | { |
20d695a9 | 774 | S390CPU *cpu = S390_CPU(cs); |
0e60a699 AG |
775 | int ret = 0; |
776 | ||
777 | switch (run->exit_reason) { | |
778 | case KVM_EXIT_S390_SIEIC: | |
1bc22652 | 779 | ret = handle_intercept(cpu); |
0e60a699 AG |
780 | break; |
781 | case KVM_EXIT_S390_RESET: | |
add142e0 | 782 | qemu_system_reset_request(); |
0e60a699 | 783 | break; |
09b99878 CH |
784 | case KVM_EXIT_S390_TSCH: |
785 | ret = handle_tsch(cpu); | |
786 | break; | |
0e60a699 AG |
787 | default: |
788 | fprintf(stderr, "Unknown KVM exit: %d\n", run->exit_reason); | |
789 | break; | |
790 | } | |
791 | ||
bb4ea393 JK |
792 | if (ret == 0) { |
793 | ret = EXCP_INTERRUPT; | |
bb4ea393 | 794 | } |
0e60a699 AG |
795 | return ret; |
796 | } | |
4513d923 | 797 | |
20d695a9 | 798 | bool kvm_arch_stop_on_emulation_error(CPUState *cpu) |
4513d923 GN |
799 | { |
800 | return true; | |
801 | } | |
a1b87fe0 | 802 | |
20d695a9 | 803 | int kvm_arch_on_sigbus_vcpu(CPUState *cpu, int code, void *addr) |
a1b87fe0 JK |
804 | { |
805 | return 1; | |
806 | } | |
807 | ||
808 | int kvm_arch_on_sigbus(int code, void *addr) | |
809 | { | |
810 | return 1; | |
811 | } | |
09b99878 CH |
812 | |
813 | void kvm_s390_io_interrupt(S390CPU *cpu, uint16_t subchannel_id, | |
814 | uint16_t subchannel_nr, uint32_t io_int_parm, | |
815 | uint32_t io_int_word) | |
816 | { | |
817 | uint32_t type; | |
818 | ||
819 | type = ((subchannel_id & 0xff00) << 24) | | |
820 | ((subchannel_id & 0x00060) << 22) | (subchannel_nr << 16); | |
821 | kvm_s390_interrupt_internal(cpu, type, | |
822 | ((uint32_t)subchannel_id << 16) | subchannel_nr, | |
823 | ((uint64_t)io_int_parm << 32) | io_int_word, 1); | |
824 | } | |
825 | ||
826 | void kvm_s390_crw_mchk(S390CPU *cpu) | |
827 | { | |
828 | kvm_s390_interrupt_internal(cpu, KVM_S390_MCHK, 1 << 28, | |
829 | 0x00400f1d40330000, 1); | |
830 | } | |
831 | ||
832 | void kvm_s390_enable_css_support(S390CPU *cpu) | |
833 | { | |
834 | struct kvm_enable_cap cap = {}; | |
835 | int r; | |
836 | ||
837 | /* Activate host kernel channel subsystem support. */ | |
838 | cap.cap = KVM_CAP_S390_CSS_SUPPORT; | |
839 | r = kvm_vcpu_ioctl(CPU(cpu), KVM_ENABLE_CAP, &cap); | |
840 | assert(r == 0); | |
841 | } | |
48475e14 AK |
842 | |
843 | void kvm_arch_init_irq_routing(KVMState *s) | |
844 | { | |
845 | } | |
b4436a0b | 846 | |
cc3ac9c4 CH |
847 | int kvm_s390_assign_subch_ioeventfd(EventNotifier *notifier, uint32_t sch, |
848 | int vq, bool assign) | |
b4436a0b CH |
849 | { |
850 | struct kvm_ioeventfd kick = { | |
851 | .flags = KVM_IOEVENTFD_FLAG_VIRTIO_CCW_NOTIFY | | |
852 | KVM_IOEVENTFD_FLAG_DATAMATCH, | |
cc3ac9c4 | 853 | .fd = event_notifier_get_fd(notifier), |
b4436a0b CH |
854 | .datamatch = vq, |
855 | .addr = sch, | |
856 | .len = 8, | |
857 | }; | |
858 | if (!kvm_check_extension(kvm_state, KVM_CAP_IOEVENTFD)) { | |
859 | return -ENOSYS; | |
860 | } | |
861 | if (!assign) { | |
862 | kick.flags |= KVM_IOEVENTFD_FLAG_DEASSIGN; | |
863 | } | |
864 | return kvm_vm_ioctl(kvm_state, KVM_IOEVENTFD, &kick); | |
865 | } |