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