Statistics
| Branch: | Revision:

root / target-s390x / kvm.c @ cd7a0f4c

History | View | Annotate | Download (23.7 kB)

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
const KVMCapabilityInfo kvm_arch_required_capabilities[] = {
86
    KVM_CAP_LAST_INFO
87
};
88

    
89
static int cap_sync_regs;
90

    
91
static void *legacy_s390_alloc(size_t size);
92

    
93
int kvm_arch_init(KVMState *s)
94
{
95
    cap_sync_regs = kvm_check_extension(s, KVM_CAP_SYNC_REGS);
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
    }
100
    return 0;
101
}
102

    
103
unsigned long kvm_arch_vcpu_id(CPUState *cpu)
104
{
105
    return cpu->cpu_index;
106
}
107

    
108
int kvm_arch_init_vcpu(CPUState *cpu)
109
{
110
    /* nothing todo yet */
111
    return 0;
112
}
113

    
114
void kvm_arch_reset_vcpu(CPUState *cpu)
115
{
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) */
121
    if (kvm_vcpu_ioctl(cpu, KVM_S390_INITIAL_RESET, NULL)) {
122
        perror("Can't reset vcpu\n");
123
    }
124
}
125

    
126
int kvm_arch_put_registers(CPUState *cs, int level)
127
{
128
    S390CPU *cpu = S390_CPU(cs);
129
    CPUS390XState *env = &cpu->env;
130
    struct kvm_one_reg reg;
131
    struct kvm_sregs sregs;
132
    struct kvm_regs regs;
133
    int ret;
134
    int i;
135

    
136
    /* always save the PSW  and the GPRS*/
137
    cs->kvm_run->psw_addr = env->psw.addr;
138
    cs->kvm_run->psw_mask = env->psw.mask;
139

    
140
    if (cap_sync_regs && cs->kvm_run->kvm_valid_regs & KVM_SYNC_GPRS) {
141
        for (i = 0; i < 16; i++) {
142
            cs->kvm_run->s.regs.gprs[i] = env->regs[i];
143
            cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_GPRS;
144
        }
145
    } else {
146
        for (i = 0; i < 16; i++) {
147
            regs.gprs[i] = env->regs[i];
148
        }
149
        ret = kvm_vcpu_ioctl(cs, KVM_SET_REGS, &regs);
150
        if (ret < 0) {
151
            return ret;
152
        }
153
    }
154

    
155
    if (env->runtime_reg_dirty_mask == KVM_S390_RUNTIME_DIRTY_FULL) {
156
        reg.id = KVM_REG_S390_CPU_TIMER;
157
        reg.addr = (__u64)&(env->cputm);
158
        ret = kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, &reg);
159
        if (ret < 0) {
160
            return ret;
161
        }
162

    
163
        reg.id = KVM_REG_S390_CLOCK_COMP;
164
        reg.addr = (__u64)&(env->ckc);
165
        ret = kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, &reg);
166
        if (ret < 0) {
167
            return ret;
168
        }
169

    
170
        reg.id = KVM_REG_S390_TODPR;
171
        reg.addr = (__u64)&(env->todpr);
172
        ret = kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, &reg);
173
        if (ret < 0) {
174
            return ret;
175
        }
176
    }
177
    env->runtime_reg_dirty_mask = KVM_S390_RUNTIME_DIRTY_NONE;
178

    
179
    /* Do we need to save more than that? */
180
    if (level == KVM_PUT_RUNTIME_STATE) {
181
        return 0;
182
    }
183

    
184
    if (cap_sync_regs &&
185
        cs->kvm_run->kvm_valid_regs & KVM_SYNC_ACRS &&
186
        cs->kvm_run->kvm_valid_regs & KVM_SYNC_CRS) {
187
        for (i = 0; i < 16; i++) {
188
            cs->kvm_run->s.regs.acrs[i] = env->aregs[i];
189
            cs->kvm_run->s.regs.crs[i] = env->cregs[i];
190
        }
191
        cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_ACRS;
192
        cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_CRS;
193
    } else {
194
        for (i = 0; i < 16; i++) {
195
            sregs.acrs[i] = env->aregs[i];
196
            sregs.crs[i] = env->cregs[i];
197
        }
198
        ret = kvm_vcpu_ioctl(cs, KVM_SET_SREGS, &sregs);
199
        if (ret < 0) {
200
            return ret;
201
        }
202
    }
203

    
204
    /* Finally the prefix */
205
    if (cap_sync_regs && cs->kvm_run->kvm_valid_regs & KVM_SYNC_PREFIX) {
206
        cs->kvm_run->s.regs.prefix = env->psa;
207
        cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_PREFIX;
208
    } else {
209
        /* prefix is only supported via sync regs */
210
    }
211
    return 0;
212
}
213

    
214
int kvm_arch_get_registers(CPUState *cs)
215
{
216
    S390CPU *cpu = S390_CPU(cs);
217
    CPUS390XState *env = &cpu->env;
218
    struct kvm_one_reg reg;
219
    int r;
220

    
221
    r = kvm_s390_get_registers_partial(cs);
222
    if (r < 0) {
223
        return r;
224
    }
225

    
226
    reg.id = KVM_REG_S390_CPU_TIMER;
227
    reg.addr = (__u64)&(env->cputm);
228
    r = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, &reg);
229
    if (r < 0) {
230
        return r;
231
    }
232

    
233
    reg.id = KVM_REG_S390_CLOCK_COMP;
234
    reg.addr = (__u64)&(env->ckc);
235
    r = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, &reg);
236
    if (r < 0) {
237
        return r;
238
    }
239

    
240
    reg.id = KVM_REG_S390_TODPR;
241
    reg.addr = (__u64)&(env->todpr);
242
    r = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, &reg);
243
    if (r < 0) {
244
        return r;
245
    }
246

    
247
    env->runtime_reg_dirty_mask = KVM_S390_RUNTIME_DIRTY_FULL;
248
    return 0;
249
}
250

    
251
int kvm_s390_get_registers_partial(CPUState *cs)
252
{
253
    S390CPU *cpu = S390_CPU(cs);
254
    CPUS390XState *env = &cpu->env;
255
    struct kvm_sregs sregs;
256
    struct kvm_regs regs;
257
    int ret;
258
    int i;
259

    
260
    if (env->runtime_reg_dirty_mask) {
261
        return 0;
262
    }
263

    
264
    /* get the PSW */
265
    env->psw.addr = cs->kvm_run->psw_addr;
266
    env->psw.mask = cs->kvm_run->psw_mask;
267

    
268
    /* the GPRS */
269
    if (cap_sync_regs && cs->kvm_run->kvm_valid_regs & KVM_SYNC_GPRS) {
270
        for (i = 0; i < 16; i++) {
271
            env->regs[i] = cs->kvm_run->s.regs.gprs[i];
272
        }
273
    } else {
274
        ret = kvm_vcpu_ioctl(cs, KVM_GET_REGS, &regs);
275
        if (ret < 0) {
276
            return ret;
277
        }
278
         for (i = 0; i < 16; i++) {
279
            env->regs[i] = regs.gprs[i];
280
        }
281
    }
282

    
283
    /* The ACRS and CRS */
284
    if (cap_sync_regs &&
285
        cs->kvm_run->kvm_valid_regs & KVM_SYNC_ACRS &&
286
        cs->kvm_run->kvm_valid_regs & KVM_SYNC_CRS) {
287
        for (i = 0; i < 16; i++) {
288
            env->aregs[i] = cs->kvm_run->s.regs.acrs[i];
289
            env->cregs[i] = cs->kvm_run->s.regs.crs[i];
290
        }
291
    } else {
292
        ret = kvm_vcpu_ioctl(cs, KVM_GET_SREGS, &sregs);
293
        if (ret < 0) {
294
            return ret;
295
        }
296
         for (i = 0; i < 16; i++) {
297
            env->aregs[i] = sregs.acrs[i];
298
            env->cregs[i] = sregs.crs[i];
299
        }
300
    }
301

    
302
    /* Finally the prefix */
303
    if (cap_sync_regs && cs->kvm_run->kvm_valid_regs & KVM_SYNC_PREFIX) {
304
        env->psa = cs->kvm_run->s.regs.prefix;
305
    } else {
306
        /* no prefix without sync regs */
307
    }
308

    
309
    env->runtime_reg_dirty_mask = KVM_S390_RUNTIME_DIRTY_PARTIAL;
310
    return 0;
311
}
312

    
313
/*
314
 * Legacy layout for s390:
315
 * Older S390 KVM requires the topmost vma of the RAM to be
316
 * smaller than an system defined value, which is at least 256GB.
317
 * Larger systems have larger values. We put the guest between
318
 * the end of data segment (system break) and this value. We
319
 * use 32GB as a base to have enough room for the system break
320
 * to grow. We also have to use MAP parameters that avoid
321
 * read-only mapping of guest pages.
322
 */
323
static void *legacy_s390_alloc(size_t size)
324
{
325
    void *mem;
326

    
327
    mem = mmap((void *) 0x800000000ULL, size,
328
               PROT_EXEC|PROT_READ|PROT_WRITE,
329
               MAP_SHARED | MAP_ANONYMOUS | MAP_FIXED, -1, 0);
330
    return mem == MAP_FAILED ? NULL : mem;
331
}
332

    
333
int kvm_arch_insert_sw_breakpoint(CPUState *cs, struct kvm_sw_breakpoint *bp)
334
{
335
    static const uint8_t diag_501[] = {0x83, 0x24, 0x05, 0x01};
336

    
337
    if (cpu_memory_rw_debug(cs, bp->pc, (uint8_t *)&bp->saved_insn, 4, 0) ||
338
        cpu_memory_rw_debug(cs, bp->pc, (uint8_t *)diag_501, 4, 1)) {
339
        return -EINVAL;
340
    }
341
    return 0;
342
}
343

    
344
int kvm_arch_remove_sw_breakpoint(CPUState *cs, struct kvm_sw_breakpoint *bp)
345
{
346
    uint8_t t[4];
347
    static const uint8_t diag_501[] = {0x83, 0x24, 0x05, 0x01};
348

    
349
    if (cpu_memory_rw_debug(cs, bp->pc, t, 4, 0)) {
350
        return -EINVAL;
351
    } else if (memcmp(t, diag_501, 4)) {
352
        return -EINVAL;
353
    } else if (cpu_memory_rw_debug(cs, bp->pc, (uint8_t *)&bp->saved_insn, 1, 1)) {
354
        return -EINVAL;
355
    }
356

    
357
    return 0;
358
}
359

    
360
void kvm_arch_pre_run(CPUState *cpu, struct kvm_run *run)
361
{
362
}
363

    
364
void kvm_arch_post_run(CPUState *cpu, struct kvm_run *run)
365
{
366
}
367

    
368
int kvm_arch_process_async_events(CPUState *cs)
369
{
370
    return cs->halted;
371
}
372

    
373
void kvm_s390_interrupt_internal(S390CPU *cpu, int type, uint32_t parm,
374
                                 uint64_t parm64, int vm)
375
{
376
    CPUState *cs = CPU(cpu);
377
    struct kvm_s390_interrupt kvmint;
378
    int r;
379

    
380
    if (!cs->kvm_state) {
381
        return;
382
    }
383

    
384
    kvmint.type = type;
385
    kvmint.parm = parm;
386
    kvmint.parm64 = parm64;
387

    
388
    if (vm) {
389
        r = kvm_vm_ioctl(cs->kvm_state, KVM_S390_INTERRUPT, &kvmint);
390
    } else {
391
        r = kvm_vcpu_ioctl(cs, KVM_S390_INTERRUPT, &kvmint);
392
    }
393

    
394
    if (r < 0) {
395
        fprintf(stderr, "KVM failed to inject interrupt\n");
396
        exit(1);
397
    }
398
}
399

    
400
void kvm_s390_virtio_irq(S390CPU *cpu, int config_change, uint64_t token)
401
{
402
    kvm_s390_interrupt_internal(cpu, KVM_S390_INT_VIRTIO, config_change,
403
                                token, 1);
404
}
405

    
406
void kvm_s390_interrupt(S390CPU *cpu, int type, uint32_t code)
407
{
408
    kvm_s390_interrupt_internal(cpu, type, code, 0, 0);
409
}
410

    
411
static void enter_pgmcheck(S390CPU *cpu, uint16_t code)
412
{
413
    kvm_s390_interrupt(cpu, KVM_S390_PROGRAM_INT, code);
414
}
415

    
416
static int kvm_sclp_service_call(S390CPU *cpu, struct kvm_run *run,
417
                                 uint16_t ipbh0)
418
{
419
    CPUS390XState *env = &cpu->env;
420
    uint32_t sccb;
421
    uint64_t code;
422
    int r = 0;
423

    
424
    cpu_synchronize_state(CPU(cpu));
425
    if (env->psw.mask & PSW_MASK_PSTATE) {
426
        enter_pgmcheck(cpu, PGM_PRIVILEGED);
427
        return 0;
428
    }
429
    sccb = env->regs[ipbh0 & 0xf];
430
    code = env->regs[(ipbh0 & 0xf0) >> 4];
431

    
432
    r = sclp_service_call(sccb, code);
433
    if (r < 0) {
434
        enter_pgmcheck(cpu, -r);
435
    }
436
    setcc(cpu, r);
437

    
438
    return 0;
439
}
440

    
441
static int kvm_handle_css_inst(S390CPU *cpu, struct kvm_run *run,
442
                               uint8_t ipa0, uint8_t ipa1, uint8_t ipb)
443
{
444
    CPUS390XState *env = &cpu->env;
445
    CPUState *cs = CPU(cpu);
446

    
447
    if (ipa0 != 0xb2) {
448
        /* Not handled for now. */
449
        return -1;
450
    }
451

    
452
    kvm_s390_get_registers_partial(cs);
453
    cs->kvm_vcpu_dirty = true;
454

    
455
    switch (ipa1) {
456
    case PRIV_XSCH:
457
        ioinst_handle_xsch(cpu, env->regs[1]);
458
        break;
459
    case PRIV_CSCH:
460
        ioinst_handle_csch(cpu, env->regs[1]);
461
        break;
462
    case PRIV_HSCH:
463
        ioinst_handle_hsch(cpu, env->regs[1]);
464
        break;
465
    case PRIV_MSCH:
466
        ioinst_handle_msch(cpu, env->regs[1], run->s390_sieic.ipb);
467
        break;
468
    case PRIV_SSCH:
469
        ioinst_handle_ssch(cpu, env->regs[1], run->s390_sieic.ipb);
470
        break;
471
    case PRIV_STCRW:
472
        ioinst_handle_stcrw(cpu, run->s390_sieic.ipb);
473
        break;
474
    case PRIV_STSCH:
475
        ioinst_handle_stsch(cpu, env->regs[1], run->s390_sieic.ipb);
476
        break;
477
    case PRIV_TSCH:
478
        /* We should only get tsch via KVM_EXIT_S390_TSCH. */
479
        fprintf(stderr, "Spurious tsch intercept\n");
480
        break;
481
    case PRIV_CHSC:
482
        ioinst_handle_chsc(cpu, run->s390_sieic.ipb);
483
        break;
484
    case PRIV_TPI:
485
        /* This should have been handled by kvm already. */
486
        fprintf(stderr, "Spurious tpi intercept\n");
487
        break;
488
    case PRIV_SCHM:
489
        ioinst_handle_schm(cpu, env->regs[1], env->regs[2],
490
                           run->s390_sieic.ipb);
491
        break;
492
    case PRIV_RSCH:
493
        ioinst_handle_rsch(cpu, env->regs[1]);
494
        break;
495
    case PRIV_RCHP:
496
        ioinst_handle_rchp(cpu, env->regs[1]);
497
        break;
498
    case PRIV_STCPS:
499
        /* We do not provide this instruction, it is suppressed. */
500
        break;
501
    case PRIV_SAL:
502
        ioinst_handle_sal(cpu, env->regs[1]);
503
        break;
504
    case PRIV_SIGA:
505
        /* Not provided, set CC = 3 for subchannel not operational */
506
        setcc(cpu, 3);
507
        break;
508
    default:
509
        return -1;
510
    }
511

    
512
    return 0;
513
}
514

    
515
static int handle_priv(S390CPU *cpu, struct kvm_run *run,
516
                       uint8_t ipa0, uint8_t ipa1)
517
{
518
    int r = 0;
519
    uint16_t ipbh0 = (run->s390_sieic.ipb & 0xffff0000) >> 16;
520
    uint8_t ipb = run->s390_sieic.ipb & 0xff;
521

    
522
    DPRINTF("KVM: PRIV: %d\n", ipa1);
523
    switch (ipa1) {
524
        case PRIV_SCLP_CALL:
525
            r = kvm_sclp_service_call(cpu, run, ipbh0);
526
            break;
527
        default:
528
            r = kvm_handle_css_inst(cpu, run, ipa0, ipa1, ipb);
529
            if (r == -1) {
530
                DPRINTF("KVM: unhandled PRIV: 0x%x\n", ipa1);
531
            }
532
            break;
533
    }
534

    
535
    return r;
536
}
537

    
538
static int handle_hypercall(S390CPU *cpu, struct kvm_run *run)
539
{
540
    CPUState *cs = CPU(cpu);
541
    CPUS390XState *env = &cpu->env;
542

    
543
    kvm_s390_get_registers_partial(cs);
544
    cs->kvm_vcpu_dirty = true;
545
    env->regs[2] = s390_virtio_hypercall(env);
546

    
547
    return 0;
548
}
549

    
550
static void kvm_handle_diag_308(S390CPU *cpu, struct kvm_run *run)
551
{
552
    uint64_t r1, r3;
553

    
554
    cpu_synchronize_state(CPU(cpu));
555
    r1 = (run->s390_sieic.ipa & 0x00f0) >> 8;
556
    r3 = run->s390_sieic.ipa & 0x000f;
557
    handle_diag_308(&cpu->env, r1, r3);
558
}
559

    
560
#define DIAG_KVM_CODE_MASK 0x000000000000ffff
561

    
562
static int handle_diag(S390CPU *cpu, struct kvm_run *run, uint32_t ipb)
563
{
564
    int r = 0;
565
    uint16_t func_code;
566

    
567
    /*
568
     * For any diagnose call we support, bits 48-63 of the resulting
569
     * address specify the function code; the remainder is ignored.
570
     */
571
    func_code = decode_basedisp_rs(&cpu->env, ipb) & DIAG_KVM_CODE_MASK;
572
    switch (func_code) {
573
    case DIAG_IPL:
574
        kvm_handle_diag_308(cpu, run);
575
        break;
576
    case DIAG_KVM_HYPERCALL:
577
        r = handle_hypercall(cpu, run);
578
        break;
579
    case DIAG_KVM_BREAKPOINT:
580
        sleep(10);
581
        break;
582
    default:
583
        DPRINTF("KVM: unknown DIAG: 0x%x\n", func_code);
584
        r = -1;
585
        break;
586
    }
587

    
588
    return r;
589
}
590

    
591
int kvm_s390_cpu_restart(S390CPU *cpu)
592
{
593
    kvm_s390_interrupt(cpu, KVM_S390_RESTART, 0);
594
    s390_add_running_cpu(cpu);
595
    qemu_cpu_kick(CPU(cpu));
596
    DPRINTF("DONE: KVM cpu restart: %p\n", &cpu->env);
597
    return 0;
598
}
599

    
600
static int s390_cpu_initial_reset(S390CPU *cpu)
601
{
602
    CPUState *cs = CPU(cpu);
603
    CPUS390XState *env = &cpu->env;
604
    int i;
605

    
606
    s390_del_running_cpu(cpu);
607
    if (kvm_vcpu_ioctl(cs, KVM_S390_INITIAL_RESET, NULL) < 0) {
608
        perror("cannot init reset vcpu");
609
    }
610

    
611
    /* Manually zero out all registers */
612
    cpu_synchronize_state(cs);
613
    for (i = 0; i < 16; i++) {
614
        env->regs[i] = 0;
615
    }
616

    
617
    DPRINTF("DONE: SIGP initial reset: %p\n", env);
618
    return 0;
619
}
620

    
621
static int handle_sigp(S390CPU *cpu, struct kvm_run *run, uint8_t ipa1)
622
{
623
    CPUS390XState *env = &cpu->env;
624
    uint8_t order_code;
625
    uint16_t cpu_addr;
626
    int r = -1;
627
    S390CPU *target_cpu;
628

    
629
    cpu_synchronize_state(CPU(cpu));
630

    
631
    /* get order code */
632
    order_code = run->s390_sieic.ipb >> 28;
633
    if (order_code > 0) {
634
        order_code = env->regs[order_code];
635
    }
636
    order_code += (run->s390_sieic.ipb & 0x0fff0000) >> 16;
637

    
638
    cpu_addr = env->regs[ipa1 & 0x0f];
639
    target_cpu = s390_cpu_addr2state(cpu_addr);
640
    if (target_cpu == NULL) {
641
        goto out;
642
    }
643

    
644
    switch (order_code) {
645
        case SIGP_RESTART:
646
            r = kvm_s390_cpu_restart(target_cpu);
647
            break;
648
        case SIGP_SET_ARCH:
649
            /* make the caller panic */
650
            return -1;
651
        case SIGP_INITIAL_CPU_RESET:
652
            r = s390_cpu_initial_reset(target_cpu);
653
            break;
654
        default:
655
            fprintf(stderr, "KVM: unknown SIGP: 0x%x\n", order_code);
656
            break;
657
    }
658

    
659
out:
660
    setcc(cpu, r ? 3 : 0);
661
    return 0;
662
}
663

    
664
static void handle_instruction(S390CPU *cpu, struct kvm_run *run)
665
{
666
    unsigned int ipa0 = (run->s390_sieic.ipa & 0xff00);
667
    uint8_t ipa1 = run->s390_sieic.ipa & 0x00ff;
668
    int r = -1;
669

    
670
    DPRINTF("handle_instruction 0x%x 0x%x\n",
671
            run->s390_sieic.ipa, run->s390_sieic.ipb);
672
    switch (ipa0) {
673
    case IPA0_B2:
674
    case IPA0_B9:
675
    case IPA0_EB:
676
        r = handle_priv(cpu, run, ipa0 >> 8, ipa1);
677
        break;
678
    case IPA0_DIAG:
679
        r = handle_diag(cpu, run, run->s390_sieic.ipb);
680
        break;
681
    case IPA0_SIGP:
682
        r = handle_sigp(cpu, run, ipa1);
683
        break;
684
    }
685

    
686
    if (r < 0) {
687
        enter_pgmcheck(cpu, 0x0001);
688
    }
689
}
690

    
691
static bool is_special_wait_psw(CPUState *cs)
692
{
693
    /* signal quiesce */
694
    return cs->kvm_run->psw_addr == 0xfffUL;
695
}
696

    
697
static int handle_intercept(S390CPU *cpu)
698
{
699
    CPUState *cs = CPU(cpu);
700
    struct kvm_run *run = cs->kvm_run;
701
    int icpt_code = run->s390_sieic.icptcode;
702
    int r = 0;
703

    
704
    DPRINTF("intercept: 0x%x (at 0x%lx)\n", icpt_code,
705
            (long)cs->kvm_run->psw_addr);
706
    switch (icpt_code) {
707
        case ICPT_INSTRUCTION:
708
            handle_instruction(cpu, run);
709
            break;
710
        case ICPT_WAITPSW:
711
            /* disabled wait, since enabled wait is handled in kernel */
712
            if (s390_del_running_cpu(cpu) == 0) {
713
                if (is_special_wait_psw(cs)) {
714
                    qemu_system_shutdown_request();
715
                } else {
716
                    QObject *data;
717

    
718
                    data = qobject_from_jsonf("{ 'action': %s }", "pause");
719
                    monitor_protocol_event(QEVENT_GUEST_PANICKED, data);
720
                    qobject_decref(data);
721
                    vm_stop(RUN_STATE_GUEST_PANICKED);
722
                }
723
            }
724
            r = EXCP_HALTED;
725
            break;
726
        case ICPT_CPU_STOP:
727
            if (s390_del_running_cpu(cpu) == 0) {
728
                qemu_system_shutdown_request();
729
            }
730
            r = EXCP_HALTED;
731
            break;
732
        case ICPT_SOFT_INTERCEPT:
733
            fprintf(stderr, "KVM unimplemented icpt SOFT\n");
734
            exit(1);
735
            break;
736
        case ICPT_IO:
737
            fprintf(stderr, "KVM unimplemented icpt IO\n");
738
            exit(1);
739
            break;
740
        default:
741
            fprintf(stderr, "Unknown intercept code: %d\n", icpt_code);
742
            exit(1);
743
            break;
744
    }
745

    
746
    return r;
747
}
748

    
749
static int handle_tsch(S390CPU *cpu)
750
{
751
    CPUS390XState *env = &cpu->env;
752
    CPUState *cs = CPU(cpu);
753
    struct kvm_run *run = cs->kvm_run;
754
    int ret;
755

    
756
    kvm_s390_get_registers_partial(cs);
757
    cs->kvm_vcpu_dirty = true;
758

    
759
    ret = ioinst_handle_tsch(env, env->regs[1], run->s390_tsch.ipb);
760
    if (ret >= 0) {
761
        /* Success; set condition code. */
762
        setcc(cpu, ret);
763
        ret = 0;
764
    } else if (ret < -1) {
765
        /*
766
         * Failure.
767
         * If an I/O interrupt had been dequeued, we have to reinject it.
768
         */
769
        if (run->s390_tsch.dequeued) {
770
            uint16_t subchannel_id = run->s390_tsch.subchannel_id;
771
            uint16_t subchannel_nr = run->s390_tsch.subchannel_nr;
772
            uint32_t io_int_parm = run->s390_tsch.io_int_parm;
773
            uint32_t io_int_word = run->s390_tsch.io_int_word;
774
            uint32_t type = ((subchannel_id & 0xff00) << 24) |
775
                ((subchannel_id & 0x00060) << 22) | (subchannel_nr << 16);
776

    
777
            kvm_s390_interrupt_internal(cpu, type,
778
                                        ((uint32_t)subchannel_id << 16)
779
                                        | subchannel_nr,
780
                                        ((uint64_t)io_int_parm << 32)
781
                                        | io_int_word, 1);
782
        }
783
        ret = 0;
784
    }
785
    return ret;
786
}
787

    
788
int kvm_arch_handle_exit(CPUState *cs, struct kvm_run *run)
789
{
790
    S390CPU *cpu = S390_CPU(cs);
791
    int ret = 0;
792

    
793
    switch (run->exit_reason) {
794
        case KVM_EXIT_S390_SIEIC:
795
            ret = handle_intercept(cpu);
796
            break;
797
        case KVM_EXIT_S390_RESET:
798
            qemu_system_reset_request();
799
            break;
800
        case KVM_EXIT_S390_TSCH:
801
            ret = handle_tsch(cpu);
802
            break;
803
        default:
804
            fprintf(stderr, "Unknown KVM exit: %d\n", run->exit_reason);
805
            break;
806
    }
807

    
808
    if (ret == 0) {
809
        ret = EXCP_INTERRUPT;
810
    }
811
    return ret;
812
}
813

    
814
bool kvm_arch_stop_on_emulation_error(CPUState *cpu)
815
{
816
    return true;
817
}
818

    
819
int kvm_arch_on_sigbus_vcpu(CPUState *cpu, int code, void *addr)
820
{
821
    return 1;
822
}
823

    
824
int kvm_arch_on_sigbus(int code, void *addr)
825
{
826
    return 1;
827
}
828

    
829
void kvm_s390_io_interrupt(S390CPU *cpu, uint16_t subchannel_id,
830
                           uint16_t subchannel_nr, uint32_t io_int_parm,
831
                           uint32_t io_int_word)
832
{
833
    uint32_t type;
834

    
835
    type = ((subchannel_id & 0xff00) << 24) |
836
        ((subchannel_id & 0x00060) << 22) | (subchannel_nr << 16);
837
    kvm_s390_interrupt_internal(cpu, type,
838
                                ((uint32_t)subchannel_id << 16) | subchannel_nr,
839
                                ((uint64_t)io_int_parm << 32) | io_int_word, 1);
840
}
841

    
842
void kvm_s390_crw_mchk(S390CPU *cpu)
843
{
844
    kvm_s390_interrupt_internal(cpu, KVM_S390_MCHK, 1 << 28,
845
                                0x00400f1d40330000, 1);
846
}
847

    
848
void kvm_s390_enable_css_support(S390CPU *cpu)
849
{
850
    struct kvm_enable_cap cap = {};
851
    int r;
852

    
853
    /* Activate host kernel channel subsystem support. */
854
    cap.cap = KVM_CAP_S390_CSS_SUPPORT;
855
    r = kvm_vcpu_ioctl(CPU(cpu), KVM_ENABLE_CAP, &cap);
856
    assert(r == 0);
857
}
858

    
859
void kvm_arch_init_irq_routing(KVMState *s)
860
{
861
}
862

    
863
int kvm_s390_assign_subch_ioeventfd(EventNotifier *notifier, uint32_t sch,
864
                                    int vq, bool assign)
865
{
866
    struct kvm_ioeventfd kick = {
867
        .flags = KVM_IOEVENTFD_FLAG_VIRTIO_CCW_NOTIFY |
868
        KVM_IOEVENTFD_FLAG_DATAMATCH,
869
        .fd = event_notifier_get_fd(notifier),
870
        .datamatch = vq,
871
        .addr = sch,
872
        .len = 8,
873
    };
874
    if (!kvm_check_extension(kvm_state, KVM_CAP_IOEVENTFD)) {
875
        return -ENOSYS;
876
    }
877
    if (!assign) {
878
        kick.flags |= KVM_IOEVENTFD_FLAG_DEASSIGN;
879
    }
880
    return kvm_vm_ioctl(kvm_state, KVM_IOEVENTFD, &kick);
881
}