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