kvm.c 71.9 KB
Newer Older
A
aurel32 已提交
1 2 3 4
/*
 * PowerPC implementation of KVM hooks
 *
 * Copyright IBM Corp. 2007
S
Scott Wood 已提交
5
 * Copyright (C) 2011 Freescale Semiconductor, Inc.
A
aurel32 已提交
6 7 8 9 10 11 12 13 14 15 16
 *
 * Authors:
 *  Jerone Young <jyoung5@us.ibm.com>
 *  Christian Ehrhardt <ehrhardt@linux.vnet.ibm.com>
 *  Hollis Blanchard <hollisb@us.ibm.com>
 *
 * This work is licensed under the terms of the GNU GPL, version 2 or later.
 * See the COPYING file in the top-level directory.
 *
 */

P
Peter Maydell 已提交
17
#include "qemu/osdep.h"
18
#include <dirent.h>
A
aurel32 已提交
19
#include <sys/ioctl.h>
20
#include <sys/vfs.h>
A
aurel32 已提交
21 22 23 24

#include <linux/kvm.h>

#include "qemu-common.h"
25
#include "qemu/error-report.h"
26
#include "cpu.h"
27
#include "cpu-models.h"
28
#include "qemu/timer.h"
29
#include "sysemu/sysemu.h"
30
#include "sysemu/hw_accel.h"
A
aurel32 已提交
31
#include "kvm_ppc.h"
32 33
#include "sysemu/cpus.h"
#include "sysemu/device_tree.h"
34
#include "mmu-hash64.h"
A
aurel32 已提交
35

36
#include "hw/sysbus.h"
P
Paolo Bonzini 已提交
37 38
#include "hw/ppc/spapr.h"
#include "hw/ppc/spapr_vio.h"
39
#include "hw/ppc/spapr_cpu_core.h"
40
#include "hw/ppc/ppc.h"
B
Bharat Bhushan 已提交
41
#include "sysemu/watchdog.h"
42
#include "trace.h"
43
#include "exec/gdbstub.h"
44
#include "exec/memattrs.h"
45
#include "exec/ram_addr.h"
46
#include "sysemu/hostmem.h"
47
#include "qemu/cutils.h"
48
#include "qemu/mmap-alloc.h"
49 50 51
#if defined(TARGET_PPC64)
#include "hw/ppc/spapr_cpu_core.h"
#endif
52

A
aurel32 已提交
53 54 55
//#define DEBUG_KVM

#ifdef DEBUG_KVM
56
#define DPRINTF(fmt, ...) \
A
aurel32 已提交
57 58
    do { fprintf(stderr, fmt, ## __VA_ARGS__); } while (0)
#else
59
#define DPRINTF(fmt, ...) \
A
aurel32 已提交
60 61 62
    do { } while (0)
#endif

63 64
#define PROC_DEVTREE_CPU      "/proc/device-tree/cpus/"

65 66 67 68
const KVMCapabilityInfo kvm_arch_required_capabilities[] = {
    KVM_CAP_LAST_INFO
};

69 70
static int cap_interrupt_unset = false;
static int cap_interrupt_level = false;
S
Scott Wood 已提交
71 72
static int cap_segstate;
static int cap_booke_sregs;
73
static int cap_ppc_smt;
74
static int cap_ppc_rma;
75
static int cap_spapr_tce;
76
static int cap_spapr_multitce;
77
static int cap_spapr_vfio;
78
static int cap_hior;
79
static int cap_one_reg;
80
static int cap_epr;
B
Bharat Bhushan 已提交
81
static int cap_ppc_watchdog;
82
static int cap_papr;
83
static int cap_htab_fd;
84
static int cap_fixup_hcalls;
85
static int cap_htm;             /* Hardware transactional memory support */
86

87 88
static uint32_t debug_inst_opcode;

A
Alexander Graf 已提交
89 90 91 92
/* XXX We have a race condition where we actually have a level triggered
 *     interrupt, but the infrastructure can't expose that yet, so the guest
 *     takes but ignores it, goes to sleep and never gets notified that there's
 *     still an interrupt pending.
A
Alexander Graf 已提交
93
 *
A
Alexander Graf 已提交
94 95 96
 *     As a quick workaround, let's just wake up again 20 ms after we injected
 *     an interrupt. That way we can assure that we're always reinjecting
 *     interrupts in case the guest swallowed them.
A
Alexander Graf 已提交
97 98 99
 */
static QEMUTimer *idle_timer;

100
static void kvm_kick_cpu(void *opaque)
A
Alexander Graf 已提交
101
{
102 103
    PowerPCCPU *cpu = opaque;

104
    qemu_cpu_kick(CPU(cpu));
A
Alexander Graf 已提交
105 106
}

107 108 109 110 111 112 113 114 115 116
/* Check whether we are running with KVM-PR (instead of KVM-HV).  This
 * should only be used for fallback tests - generally we should use
 * explicit capabilities for the features we want, rather than
 * assuming what is/isn't available depending on the KVM variant. */
static bool kvmppc_is_pr(KVMState *ks)
{
    /* Assume KVM-PR if the GET_PVINFO capability is available */
    return kvm_check_extension(ks, KVM_CAP_PPC_GET_PVINFO) != 0;
}

117 118
static int kvm_ppc_register_host_cpu_type(void);

119
int kvm_arch_init(MachineState *ms, KVMState *s)
A
aurel32 已提交
120
{
121 122
    cap_interrupt_unset = kvm_check_extension(s, KVM_CAP_PPC_UNSET_IRQ);
    cap_interrupt_level = kvm_check_extension(s, KVM_CAP_PPC_IRQ_LEVEL);
S
Scott Wood 已提交
123 124
    cap_segstate = kvm_check_extension(s, KVM_CAP_PPC_SEGSTATE);
    cap_booke_sregs = kvm_check_extension(s, KVM_CAP_PPC_BOOKE_SREGS);
125
    cap_ppc_smt = kvm_check_extension(s, KVM_CAP_PPC_SMT);
126
    cap_ppc_rma = kvm_check_extension(s, KVM_CAP_PPC_RMA);
127
    cap_spapr_tce = kvm_check_extension(s, KVM_CAP_SPAPR_TCE);
128
    cap_spapr_multitce = kvm_check_extension(s, KVM_CAP_SPAPR_MULTITCE);
129
    cap_spapr_vfio = false;
130
    cap_one_reg = kvm_check_extension(s, KVM_CAP_ONE_REG);
131
    cap_hior = kvm_check_extension(s, KVM_CAP_PPC_HIOR);
132
    cap_epr = kvm_check_extension(s, KVM_CAP_PPC_EPR);
B
Bharat Bhushan 已提交
133
    cap_ppc_watchdog = kvm_check_extension(s, KVM_CAP_PPC_BOOKE_WATCHDOG);
134 135
    /* Note: we don't set cap_papr here, because this capability is
     * only activated after this by kvmppc_set_papr() */
136
    cap_htab_fd = kvm_check_extension(s, KVM_CAP_PPC_HTAB_FD);
137
    cap_fixup_hcalls = kvm_check_extension(s, KVM_CAP_PPC_FIXUP_HCALL);
138
    cap_htm = kvm_vm_check_extension(s, KVM_CAP_PPC_HTM);
139 140 141 142 143 144

    if (!cap_interrupt_level) {
        fprintf(stderr, "KVM: Couldn't find level irq capability. Expect the "
                        "VM to stall at times!\n");
    }

145 146
    kvm_ppc_register_host_cpu_type();

A
aurel32 已提交
147 148 149
    return 0;
}

P
Paolo Bonzini 已提交
150 151 152 153 154
int kvm_arch_irqchip_create(MachineState *ms, KVMState *s)
{
    return 0;
}

155
static int kvm_arch_sync_sregs(PowerPCCPU *cpu)
A
aurel32 已提交
156
{
157 158
    CPUPPCState *cenv = &cpu->env;
    CPUState *cs = CPU(cpu);
A
Alexander Graf 已提交
159
    struct kvm_sregs sregs;
160 161 162
    int ret;

    if (cenv->excp_model == POWERPC_EXCP_BOOKE) {
A
Alexander Graf 已提交
163 164 165 166
        /* What we're really trying to say is "if we're on BookE, we use
           the native PVR for now". This is the only sane way to check
           it though, so we potentially confuse users that they can run
           BookE guests on BookS. Let's hope nobody dares enough :) */
167 168
        return 0;
    } else {
S
Scott Wood 已提交
169
        if (!cap_segstate) {
A
Alexander Graf 已提交
170 171
            fprintf(stderr, "kvm error: missing PVR setting capability\n");
            return -ENOSYS;
172 173 174
        }
    }

175
    ret = kvm_vcpu_ioctl(cs, KVM_GET_SREGS, &sregs);
176 177 178
    if (ret) {
        return ret;
    }
A
Alexander Graf 已提交
179 180

    sregs.pvr = cenv->spr[SPR_PVR];
181
    return kvm_vcpu_ioctl(cs, KVM_SET_SREGS, &sregs);
182 183
}

S
Scott Wood 已提交
184
/* Set up a shared TLB array with KVM */
185
static int kvm_booke206_tlb_init(PowerPCCPU *cpu)
S
Scott Wood 已提交
186
{
187 188
    CPUPPCState *env = &cpu->env;
    CPUState *cs = CPU(cpu);
S
Scott Wood 已提交
189 190 191 192 193 194
    struct kvm_book3e_206_tlb_params params = {};
    struct kvm_config_tlb cfg = {};
    unsigned int entries = 0;
    int ret, i;

    if (!kvm_enabled() ||
195
        !kvm_check_extension(cs->kvm_state, KVM_CAP_SW_TLB)) {
S
Scott Wood 已提交
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
        return 0;
    }

    assert(ARRAY_SIZE(params.tlb_sizes) == BOOKE206_MAX_TLBN);

    for (i = 0; i < BOOKE206_MAX_TLBN; i++) {
        params.tlb_sizes[i] = booke206_tlb_size(env, i);
        params.tlb_ways[i] = booke206_tlb_ways(env, i);
        entries += params.tlb_sizes[i];
    }

    assert(entries == env->nb_tlb);
    assert(sizeof(struct kvm_book3e_206_tlb_entry) == sizeof(ppcmas_tlb_t));

    env->tlb_dirty = true;

    cfg.array = (uintptr_t)env->tlb.tlbm;
    cfg.array_len = sizeof(ppcmas_tlb_t) * entries;
    cfg.params = (uintptr_t)&params;
    cfg.mmu_type = KVM_MMU_FSL_BOOKE_NOHV;

C
Cornelia Huck 已提交
217
    ret = kvm_vcpu_enable_cap(cs, KVM_CAP_SW_TLB, 0, (uintptr_t)&cfg);
S
Scott Wood 已提交
218 219 220 221 222 223 224 225 226 227
    if (ret < 0) {
        fprintf(stderr, "%s: couldn't enable KVM_CAP_SW_TLB: %s\n",
                __func__, strerror(-ret));
        return ret;
    }

    env->kvm_sw_tlb = true;
    return 0;
}

228 229

#if defined(TARGET_PPC64)
230
static void kvm_get_fallback_smmu_info(PowerPCCPU *cpu,
231 232
                                       struct kvm_ppc_smmu_info *info)
{
233 234 235
    CPUPPCState *env = &cpu->env;
    CPUState *cs = CPU(cpu);

236 237 238 239 240 241 242
    memset(info, 0, sizeof(*info));

    /* We don't have the new KVM_PPC_GET_SMMU_INFO ioctl, so
     * need to "guess" what the supported page sizes are.
     *
     * For that to work we make a few assumptions:
     *
243 244 245
     * - Check whether we are running "PR" KVM which only supports 4K
     *   and 16M pages, but supports them regardless of the backing
     *   store characteritics. We also don't support 1T segments.
246 247 248 249 250 251 252 253 254 255 256 257 258 259
     *
     *   This is safe as if HV KVM ever supports that capability or PR
     *   KVM grows supports for more page/segment sizes, those versions
     *   will have implemented KVM_CAP_PPC_GET_SMMU_INFO and thus we
     *   will not hit this fallback
     *
     * - Else we are running HV KVM. This means we only support page
     *   sizes that fit in the backing store. Additionally we only
     *   advertize 64K pages if the processor is ARCH 2.06 and we assume
     *   P7 encodings for the SLB and hash table. Here too, we assume
     *   support for any newer processor will mean a kernel that
     *   implements KVM_CAP_PPC_GET_SMMU_INFO and thus doesn't hit
     *   this fallback.
     */
260
    if (kvmppc_is_pr(cs->kvm_state)) {
261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
        /* No flags */
        info->flags = 0;
        info->slb_size = 64;

        /* Standard 4k base page size segment */
        info->sps[0].page_shift = 12;
        info->sps[0].slb_enc = 0;
        info->sps[0].enc[0].page_shift = 12;
        info->sps[0].enc[0].pte_enc = 0;

        /* Standard 16M large page size segment */
        info->sps[1].page_shift = 24;
        info->sps[1].slb_enc = SLB_VSID_L;
        info->sps[1].enc[0].page_shift = 24;
        info->sps[1].enc[0].pte_enc = 0;
    } else {
        int i = 0;

        /* HV KVM has backing store size restrictions */
        info->flags = KVM_PPC_PAGE_SIZES_REAL;

        if (env->mmu_model & POWERPC_MMU_1TSEG) {
            info->flags |= KVM_PPC_1T_SEGMENTS;
        }

286 287
        if (env->mmu_model == POWERPC_MMU_2_06 ||
            env->mmu_model == POWERPC_MMU_2_07) {
288 289 290 291 292 293 294 295 296 297 298 299
            info->slb_size = 32;
        } else {
            info->slb_size = 64;
        }

        /* Standard 4k base page size segment */
        info->sps[i].page_shift = 12;
        info->sps[i].slb_enc = 0;
        info->sps[i].enc[0].page_shift = 12;
        info->sps[i].enc[0].pte_enc = 0;
        i++;

300 301 302
        /* 64K on MMU 2.06 and later */
        if (env->mmu_model == POWERPC_MMU_2_06 ||
            env->mmu_model == POWERPC_MMU_2_07) {
303 304 305 306 307 308 309 310 311 312 313 314 315 316 317
            info->sps[i].page_shift = 16;
            info->sps[i].slb_enc = 0x110;
            info->sps[i].enc[0].page_shift = 16;
            info->sps[i].enc[0].pte_enc = 1;
            i++;
        }

        /* Standard 16M large page size segment */
        info->sps[i].page_shift = 24;
        info->sps[i].slb_enc = SLB_VSID_L;
        info->sps[i].enc[0].page_shift = 24;
        info->sps[i].enc[0].pte_enc = 0;
    }
}

318
static void kvm_get_smmu_info(PowerPCCPU *cpu, struct kvm_ppc_smmu_info *info)
319
{
320
    CPUState *cs = CPU(cpu);
321 322
    int ret;

323 324
    if (kvm_check_extension(cs->kvm_state, KVM_CAP_PPC_GET_SMMU_INFO)) {
        ret = kvm_vm_ioctl(cs->kvm_state, KVM_PPC_GET_SMMU_INFO, info);
325 326 327 328 329
        if (ret == 0) {
            return;
        }
    }

330
    kvm_get_fallback_smmu_info(cpu, info);
331 332 333 334 335 336 337 338 339 340 341
}

static bool kvm_valid_page_size(uint32_t flags, long rampgsize, uint32_t shift)
{
    if (!(flags & KVM_PPC_PAGE_SIZES_REAL)) {
        return true;
    }

    return (1ul << shift) <= rampgsize;
}

342 343
static long max_cpu_page_size;

344
static void kvm_fixup_page_sizes(PowerPCCPU *cpu)
345 346 347
{
    static struct kvm_ppc_smmu_info smmu_info;
    static bool has_smmu_info;
348
    CPUPPCState *env = &cpu->env;
349
    int iq, ik, jq, jk;
350
    bool has_64k_pages = false;
351 352 353 354 355 356 357 358

    /* We only handle page sizes for 64-bit server guests for now */
    if (!(env->mmu_model & POWERPC_MMU_64)) {
        return;
    }

    /* Collect MMU info from kernel if not already */
    if (!has_smmu_info) {
359
        kvm_get_smmu_info(cpu, &smmu_info);
360 361 362
        has_smmu_info = true;
    }

363
    if (!max_cpu_page_size) {
364
        max_cpu_page_size = qemu_getrampagesize();
365
    }
366 367 368 369

    /* Convert to QEMU form */
    memset(&env->sps, 0, sizeof(env->sps));

370 371 372 373 374 375 376
    /* If we have HV KVM, we need to forbid CI large pages if our
     * host page size is smaller than 64K.
     */
    if (smmu_info.flags & KVM_PPC_PAGE_SIZES_REAL) {
        env->ci_large_pages = getpagesize() >= 0x10000;
    }

377 378 379 380
    /*
     * XXX This loop should be an entry wide AND of the capabilities that
     *     the selected CPU has with the capabilities that KVM supports.
     */
381 382 383 384
    for (ik = iq = 0; ik < KVM_PPC_PAGE_SIZES_MAX_SZ; ik++) {
        struct ppc_one_seg_page_size *qsps = &env->sps.sps[iq];
        struct kvm_ppc_one_seg_page_size *ksps = &smmu_info.sps[ik];

385
        if (!kvm_valid_page_size(smmu_info.flags, max_cpu_page_size,
386 387 388 389 390 391
                                 ksps->page_shift)) {
            continue;
        }
        qsps->page_shift = ksps->page_shift;
        qsps->slb_enc = ksps->slb_enc;
        for (jk = jq = 0; jk < KVM_PPC_PAGE_SIZES_MAX_SZ; jk++) {
392
            if (!kvm_valid_page_size(smmu_info.flags, max_cpu_page_size,
393 394 395
                                     ksps->enc[jk].page_shift)) {
                continue;
            }
396 397 398
            if (ksps->enc[jk].page_shift == 16) {
                has_64k_pages = true;
            }
399 400 401 402 403 404 405 406 407 408 409
            qsps->enc[jq].page_shift = ksps->enc[jk].page_shift;
            qsps->enc[jq].pte_enc = ksps->enc[jk].pte_enc;
            if (++jq >= PPC_PAGE_SIZES_MAX_SZ) {
                break;
            }
        }
        if (++iq >= PPC_PAGE_SIZES_MAX_SZ) {
            break;
        }
    }
    env->slb_nr = smmu_info.slb_size;
410
    if (!(smmu_info.flags & KVM_PPC_1T_SEGMENTS)) {
411 412
        env->mmu_model &= ~POWERPC_MMU_1TSEG;
    }
413 414 415
    if (!has_64k_pages) {
        env->mmu_model &= ~POWERPC_MMU_64K;
    }
416
}
417 418 419 420 421 422 423 424

bool kvmppc_is_mem_backend_page_size_ok(char *obj_path)
{
    Object *mem_obj = object_resolve_path(obj_path, NULL);
    char *mempath = object_property_get_str(mem_obj, "mem-path", NULL);
    long pagesize;

    if (mempath) {
425
        pagesize = qemu_mempath_getpagesize(mempath);
426 427 428 429 430 431 432
    } else {
        pagesize = getpagesize();
    }

    return pagesize >= max_cpu_page_size;
}

433 434
#else /* defined (TARGET_PPC64) */

435
static inline void kvm_fixup_page_sizes(PowerPCCPU *cpu)
436 437 438
{
}

439 440 441 442 443
bool kvmppc_is_mem_backend_page_size_ok(char *obj_path)
{
    return true;
}

444 445
#endif /* !defined (TARGET_PPC64) */

446 447
unsigned long kvm_arch_vcpu_id(CPUState *cpu)
{
448
    return ppc_get_vcpu_dt_id(POWERPC_CPU(cpu));
449 450
}

451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482
/* e500 supports 2 h/w breakpoint and 2 watchpoint.
 * book3s supports only 1 watchpoint, so array size
 * of 4 is sufficient for now.
 */
#define MAX_HW_BKPTS 4

static struct HWBreakpoint {
    target_ulong addr;
    int type;
} hw_debug_points[MAX_HW_BKPTS];

static CPUWatchpoint hw_watchpoint;

/* Default there is no breakpoint and watchpoint supported */
static int max_hw_breakpoint;
static int max_hw_watchpoint;
static int nb_hw_breakpoint;
static int nb_hw_watchpoint;

static void kvmppc_hw_debug_points_init(CPUPPCState *cenv)
{
    if (cenv->excp_model == POWERPC_EXCP_BOOKE) {
        max_hw_breakpoint = 2;
        max_hw_watchpoint = 2;
    }

    if ((max_hw_breakpoint + max_hw_watchpoint) > MAX_HW_BKPTS) {
        fprintf(stderr, "Error initializing h/w breakpoints\n");
        return;
    }
}

A
Andreas Färber 已提交
483
int kvm_arch_init_vcpu(CPUState *cs)
484
{
A
Andreas Färber 已提交
485 486
    PowerPCCPU *cpu = POWERPC_CPU(cs);
    CPUPPCState *cenv = &cpu->env;
487 488
    int ret;

489
    /* Gather server mmu info from KVM and update the CPU state */
490
    kvm_fixup_page_sizes(cpu);
491 492

    /* Synchronize sregs with kvm */
493
    ret = kvm_arch_sync_sregs(cpu);
494
    if (ret) {
495 496 497 498
        if (ret == -EINVAL) {
            error_report("Register sync failed... If you're using kvm-hv.ko,"
                         " only \"-cpu host\" is possible");
        }
499 500
        return ret;
    }
A
Alexander Graf 已提交
501

502
    idle_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, kvm_kick_cpu, cpu);
A
Alexander Graf 已提交
503

S
Scott Wood 已提交
504 505
    switch (cenv->mmu_model) {
    case POWERPC_MMU_BOOKE206:
506
        /* This target supports access to KVM's guest TLB */
507
        ret = kvm_booke206_tlb_init(cpu);
S
Scott Wood 已提交
508
        break;
509 510 511 512 513 514 515
    case POWERPC_MMU_2_07:
        if (!cap_htm && !kvmppc_is_pr(cs->kvm_state)) {
            /* KVM-HV has transactional memory on POWER8 also without the
             * KVM_CAP_PPC_HTM extension, so enable it here instead. */
            cap_htm = true;
        }
        break;
S
Scott Wood 已提交
516 517 518 519
    default:
        break;
    }

520
    kvm_get_one_reg(cs, KVM_REG_PPC_DEBUG_INST, &debug_inst_opcode);
521
    kvmppc_hw_debug_points_init(cenv);
522

A
Alexander Graf 已提交
523
    return ret;
A
aurel32 已提交
524 525
}

526
static void kvm_sw_tlb_put(PowerPCCPU *cpu)
S
Scott Wood 已提交
527
{
528 529
    CPUPPCState *env = &cpu->env;
    CPUState *cs = CPU(cpu);
S
Scott Wood 已提交
530 531 532 533 534 535 536 537 538 539 540 541 542 543
    struct kvm_dirty_tlb dirty_tlb;
    unsigned char *bitmap;
    int ret;

    if (!env->kvm_sw_tlb) {
        return;
    }

    bitmap = g_malloc((env->nb_tlb + 7) / 8);
    memset(bitmap, 0xFF, (env->nb_tlb + 7) / 8);

    dirty_tlb.bitmap = (uintptr_t)bitmap;
    dirty_tlb.num_dirty = env->nb_tlb;

544
    ret = kvm_vcpu_ioctl(cs, KVM_DIRTY_TLB, &dirty_tlb);
S
Scott Wood 已提交
545 546 547 548 549 550 551 552
    if (ret) {
        fprintf(stderr, "%s: KVM_DIRTY_TLB: %s\n",
                __func__, strerror(-ret));
    }

    g_free(bitmap);
}

553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568
static void kvm_get_one_spr(CPUState *cs, uint64_t id, int spr)
{
    PowerPCCPU *cpu = POWERPC_CPU(cs);
    CPUPPCState *env = &cpu->env;
    union {
        uint32_t u32;
        uint64_t u64;
    } val;
    struct kvm_one_reg reg = {
        .id = id,
        .addr = (uintptr_t) &val,
    };
    int ret;

    ret = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, &reg);
    if (ret != 0) {
569
        trace_kvm_failed_spr_get(spr, strerror(errno));
570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616
    } else {
        switch (id & KVM_REG_SIZE_MASK) {
        case KVM_REG_SIZE_U32:
            env->spr[spr] = val.u32;
            break;

        case KVM_REG_SIZE_U64:
            env->spr[spr] = val.u64;
            break;

        default:
            /* Don't handle this size yet */
            abort();
        }
    }
}

static void kvm_put_one_spr(CPUState *cs, uint64_t id, int spr)
{
    PowerPCCPU *cpu = POWERPC_CPU(cs);
    CPUPPCState *env = &cpu->env;
    union {
        uint32_t u32;
        uint64_t u64;
    } val;
    struct kvm_one_reg reg = {
        .id = id,
        .addr = (uintptr_t) &val,
    };
    int ret;

    switch (id & KVM_REG_SIZE_MASK) {
    case KVM_REG_SIZE_U32:
        val.u32 = env->spr[spr];
        break;

    case KVM_REG_SIZE_U64:
        val.u64 = env->spr[spr];
        break;

    default:
        /* Don't handle this size yet */
        abort();
    }

    ret = kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, &reg);
    if (ret != 0) {
617
        trace_kvm_failed_spr_set(spr, strerror(errno));
618 619 620
    }
}

621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636
static int kvm_put_fp(CPUState *cs)
{
    PowerPCCPU *cpu = POWERPC_CPU(cs);
    CPUPPCState *env = &cpu->env;
    struct kvm_one_reg reg;
    int i;
    int ret;

    if (env->insns_flags & PPC_FLOAT) {
        uint64_t fpscr = env->fpscr;
        bool vsx = !!(env->insns_flags2 & PPC2_VSX);

        reg.id = KVM_REG_PPC_FPSCR;
        reg.addr = (uintptr_t)&fpscr;
        ret = kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, &reg);
        if (ret < 0) {
637
            DPRINTF("Unable to set FPSCR to KVM: %s\n", strerror(errno));
638 639 640 641 642 643
            return ret;
        }

        for (i = 0; i < 32; i++) {
            uint64_t vsr[2];

644
#ifdef HOST_WORDS_BIGENDIAN
645 646
            vsr[0] = float64_val(env->fpr[i]);
            vsr[1] = env->vsr[i];
647 648 649 650
#else
            vsr[0] = env->vsr[i];
            vsr[1] = float64_val(env->fpr[i]);
#endif
651 652 653 654 655
            reg.addr = (uintptr_t) &vsr;
            reg.id = vsx ? KVM_REG_PPC_VSR(i) : KVM_REG_PPC_FPR(i);

            ret = kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, &reg);
            if (ret < 0) {
656
                DPRINTF("Unable to set %s%d to KVM: %s\n", vsx ? "VSR" : "FPR",
657 658 659 660 661 662 663 664 665 666 667
                        i, strerror(errno));
                return ret;
            }
        }
    }

    if (env->insns_flags & PPC_ALTIVEC) {
        reg.id = KVM_REG_PPC_VSCR;
        reg.addr = (uintptr_t)&env->vscr;
        ret = kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, &reg);
        if (ret < 0) {
668
            DPRINTF("Unable to set VSCR to KVM: %s\n", strerror(errno));
669 670 671 672 673 674 675 676
            return ret;
        }

        for (i = 0; i < 32; i++) {
            reg.id = KVM_REG_PPC_VR(i);
            reg.addr = (uintptr_t)&env->avr[i];
            ret = kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, &reg);
            if (ret < 0) {
677
                DPRINTF("Unable to set VR%d to KVM: %s\n", i, strerror(errno));
678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701
                return ret;
            }
        }
    }

    return 0;
}

static int kvm_get_fp(CPUState *cs)
{
    PowerPCCPU *cpu = POWERPC_CPU(cs);
    CPUPPCState *env = &cpu->env;
    struct kvm_one_reg reg;
    int i;
    int ret;

    if (env->insns_flags & PPC_FLOAT) {
        uint64_t fpscr;
        bool vsx = !!(env->insns_flags2 & PPC2_VSX);

        reg.id = KVM_REG_PPC_FPSCR;
        reg.addr = (uintptr_t)&fpscr;
        ret = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, &reg);
        if (ret < 0) {
702
            DPRINTF("Unable to get FPSCR from KVM: %s\n", strerror(errno));
703 704 705 706 707 708 709 710 711 712 713 714 715
            return ret;
        } else {
            env->fpscr = fpscr;
        }

        for (i = 0; i < 32; i++) {
            uint64_t vsr[2];

            reg.addr = (uintptr_t) &vsr;
            reg.id = vsx ? KVM_REG_PPC_VSR(i) : KVM_REG_PPC_FPR(i);

            ret = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, &reg);
            if (ret < 0) {
716
                DPRINTF("Unable to get %s%d from KVM: %s\n",
717 718 719
                        vsx ? "VSR" : "FPR", i, strerror(errno));
                return ret;
            } else {
720
#ifdef HOST_WORDS_BIGENDIAN
721 722 723 724
                env->fpr[i] = vsr[0];
                if (vsx) {
                    env->vsr[i] = vsr[1];
                }
725 726 727 728 729 730
#else
                env->fpr[i] = vsr[1];
                if (vsx) {
                    env->vsr[i] = vsr[0];
                }
#endif
731 732 733 734 735 736 737 738 739
            }
        }
    }

    if (env->insns_flags & PPC_ALTIVEC) {
        reg.id = KVM_REG_PPC_VSCR;
        reg.addr = (uintptr_t)&env->vscr;
        ret = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, &reg);
        if (ret < 0) {
740
            DPRINTF("Unable to get VSCR from KVM: %s\n", strerror(errno));
741 742 743 744 745 746 747 748
            return ret;
        }

        for (i = 0; i < 32; i++) {
            reg.id = KVM_REG_PPC_VR(i);
            reg.addr = (uintptr_t)&env->avr[i];
            ret = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, &reg);
            if (ret < 0) {
749
                DPRINTF("Unable to get VR%d from KVM: %s\n",
750 751 752 753 754 755 756 757 758
                        i, strerror(errno));
                return ret;
            }
        }
    }

    return 0;
}

759 760 761 762 763 764 765 766 767 768 769 770
#if defined(TARGET_PPC64)
static int kvm_get_vpa(CPUState *cs)
{
    PowerPCCPU *cpu = POWERPC_CPU(cs);
    CPUPPCState *env = &cpu->env;
    struct kvm_one_reg reg;
    int ret;

    reg.id = KVM_REG_PPC_VPA_ADDR;
    reg.addr = (uintptr_t)&env->vpa_addr;
    ret = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, &reg);
    if (ret < 0) {
771
        DPRINTF("Unable to get VPA address from KVM: %s\n", strerror(errno));
772 773 774 775 776 777 778 779 780
        return ret;
    }

    assert((uintptr_t)&env->slb_shadow_size
           == ((uintptr_t)&env->slb_shadow_addr + 8));
    reg.id = KVM_REG_PPC_VPA_SLB;
    reg.addr = (uintptr_t)&env->slb_shadow_addr;
    ret = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, &reg);
    if (ret < 0) {
781
        DPRINTF("Unable to get SLB shadow state from KVM: %s\n",
782 783 784 785 786 787 788 789 790
                strerror(errno));
        return ret;
    }

    assert((uintptr_t)&env->dtl_size == ((uintptr_t)&env->dtl_addr + 8));
    reg.id = KVM_REG_PPC_VPA_DTL;
    reg.addr = (uintptr_t)&env->dtl_addr;
    ret = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, &reg);
    if (ret < 0) {
791
        DPRINTF("Unable to get dispatch trace log state from KVM: %s\n",
792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816
                strerror(errno));
        return ret;
    }

    return 0;
}

static int kvm_put_vpa(CPUState *cs)
{
    PowerPCCPU *cpu = POWERPC_CPU(cs);
    CPUPPCState *env = &cpu->env;
    struct kvm_one_reg reg;
    int ret;

    /* SLB shadow or DTL can't be registered unless a master VPA is
     * registered.  That means when restoring state, if a VPA *is*
     * registered, we need to set that up first.  If not, we need to
     * deregister the others before deregistering the master VPA */
    assert(env->vpa_addr || !(env->slb_shadow_addr || env->dtl_addr));

    if (env->vpa_addr) {
        reg.id = KVM_REG_PPC_VPA_ADDR;
        reg.addr = (uintptr_t)&env->vpa_addr;
        ret = kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, &reg);
        if (ret < 0) {
817
            DPRINTF("Unable to set VPA address to KVM: %s\n", strerror(errno));
818 819 820 821 822 823 824 825 826 827
            return ret;
        }
    }

    assert((uintptr_t)&env->slb_shadow_size
           == ((uintptr_t)&env->slb_shadow_addr + 8));
    reg.id = KVM_REG_PPC_VPA_SLB;
    reg.addr = (uintptr_t)&env->slb_shadow_addr;
    ret = kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, &reg);
    if (ret < 0) {
828
        DPRINTF("Unable to set SLB shadow state to KVM: %s\n", strerror(errno));
829 830 831 832 833 834 835 836
        return ret;
    }

    assert((uintptr_t)&env->dtl_size == ((uintptr_t)&env->dtl_addr + 8));
    reg.id = KVM_REG_PPC_VPA_DTL;
    reg.addr = (uintptr_t)&env->dtl_addr;
    ret = kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, &reg);
    if (ret < 0) {
837
        DPRINTF("Unable to set dispatch trace log state to KVM: %s\n",
838 839 840 841 842 843 844 845 846
                strerror(errno));
        return ret;
    }

    if (!env->vpa_addr) {
        reg.id = KVM_REG_PPC_VPA_ADDR;
        reg.addr = (uintptr_t)&env->vpa_addr;
        ret = kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, &reg);
        if (ret < 0) {
847
            DPRINTF("Unable to set VPA address to KVM: %s\n", strerror(errno));
848 849 850 851 852 853 854 855
            return ret;
        }
    }

    return 0;
}
#endif /* TARGET_PPC64 */

856
int kvmppc_put_books_sregs(PowerPCCPU *cpu)
857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893
{
    CPUPPCState *env = &cpu->env;
    struct kvm_sregs sregs;
    int i;

    sregs.pvr = env->spr[SPR_PVR];

    sregs.u.s.sdr1 = env->spr[SPR_SDR1];

    /* Sync SLB */
#ifdef TARGET_PPC64
    for (i = 0; i < ARRAY_SIZE(env->slb); i++) {
        sregs.u.s.ppc64.slb[i].slbe = env->slb[i].esid;
        if (env->slb[i].esid & SLB_ESID_V) {
            sregs.u.s.ppc64.slb[i].slbe |= i;
        }
        sregs.u.s.ppc64.slb[i].slbv = env->slb[i].vsid;
    }
#endif

    /* Sync SRs */
    for (i = 0; i < 16; i++) {
        sregs.u.s.ppc32.sr[i] = env->sr[i];
    }

    /* Sync BATs */
    for (i = 0; i < 8; i++) {
        /* Beware. We have to swap upper and lower bits here */
        sregs.u.s.ppc32.dbat[i] = ((uint64_t)env->DBAT[0][i] << 32)
            | env->DBAT[1][i];
        sregs.u.s.ppc32.ibat[i] = ((uint64_t)env->IBAT[0][i] << 32)
            | env->IBAT[1][i];
    }

    return kvm_vcpu_ioctl(CPU(cpu), KVM_SET_SREGS, &sregs);
}

A
Andreas Färber 已提交
894
int kvm_arch_put_registers(CPUState *cs, int level)
A
aurel32 已提交
895
{
A
Andreas Färber 已提交
896 897
    PowerPCCPU *cpu = POWERPC_CPU(cs);
    CPUPPCState *env = &cpu->env;
A
aurel32 已提交
898 899 900 901
    struct kvm_regs regs;
    int ret;
    int i;

902 903
    ret = kvm_vcpu_ioctl(cs, KVM_GET_REGS, &regs);
    if (ret < 0) {
A
aurel32 已提交
904
        return ret;
905
    }
A
aurel32 已提交
906 907 908

    regs.ctr = env->ctr;
    regs.lr  = env->lr;
909
    regs.xer = cpu_read_xer(env);
A
aurel32 已提交
910 911 912 913 914 915 916 917 918 919 920 921 922 923 924
    regs.msr = env->msr;
    regs.pc = env->nip;

    regs.srr0 = env->spr[SPR_SRR0];
    regs.srr1 = env->spr[SPR_SRR1];

    regs.sprg0 = env->spr[SPR_SPRG0];
    regs.sprg1 = env->spr[SPR_SPRG1];
    regs.sprg2 = env->spr[SPR_SPRG2];
    regs.sprg3 = env->spr[SPR_SPRG3];
    regs.sprg4 = env->spr[SPR_SPRG4];
    regs.sprg5 = env->spr[SPR_SPRG5];
    regs.sprg6 = env->spr[SPR_SPRG6];
    regs.sprg7 = env->spr[SPR_SPRG7];

S
Scott Wood 已提交
925 926
    regs.pid = env->spr[SPR_BOOKE_PID];

A
aurel32 已提交
927 928 929
    for (i = 0;i < 32; i++)
        regs.gpr[i] = env->gpr[i];

930 931 932 933 934
    regs.cr = 0;
    for (i = 0; i < 8; i++) {
        regs.cr |= (env->crf[i] & 15) << (4 * (7 - i));
    }

935
    ret = kvm_vcpu_ioctl(cs, KVM_SET_REGS, &regs);
A
aurel32 已提交
936 937 938
    if (ret < 0)
        return ret;

939 940
    kvm_put_fp(cs);

S
Scott Wood 已提交
941
    if (env->tlb_dirty) {
942
        kvm_sw_tlb_put(cpu);
S
Scott Wood 已提交
943 944 945
        env->tlb_dirty = false;
    }

946
    if (cap_segstate && (level >= KVM_PUT_RESET_STATE)) {
947 948
        ret = kvmppc_put_books_sregs(cpu);
        if (ret < 0) {
949 950 951 952 953
            return ret;
        }
    }

    if (cap_hior && (level >= KVM_PUT_RESET_STATE)) {
954 955
        kvm_put_one_spr(cs, KVM_REG_PPC_HIOR, SPR_HIOR);
    }
956

957 958 959 960 961 962 963 964 965 966 967 968 969
    if (cap_one_reg) {
        int i;

        /* We deliberately ignore errors here, for kernels which have
         * the ONE_REG calls, but don't support the specific
         * registers, there's a reasonable chance things will still
         * work, at least until we try to migrate. */
        for (i = 0; i < 1024; i++) {
            uint64_t id = env->spr_cb[i].one_reg_id;

            if (id != 0) {
                kvm_put_one_spr(cs, id, i);
            }
970
        }
971 972

#ifdef TARGET_PPC64
973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991
        if (msr_ts) {
            for (i = 0; i < ARRAY_SIZE(env->tm_gpr); i++) {
                kvm_set_one_reg(cs, KVM_REG_PPC_TM_GPR(i), &env->tm_gpr[i]);
            }
            for (i = 0; i < ARRAY_SIZE(env->tm_vsr); i++) {
                kvm_set_one_reg(cs, KVM_REG_PPC_TM_VSR(i), &env->tm_vsr[i]);
            }
            kvm_set_one_reg(cs, KVM_REG_PPC_TM_CR, &env->tm_cr);
            kvm_set_one_reg(cs, KVM_REG_PPC_TM_LR, &env->tm_lr);
            kvm_set_one_reg(cs, KVM_REG_PPC_TM_CTR, &env->tm_ctr);
            kvm_set_one_reg(cs, KVM_REG_PPC_TM_FPSCR, &env->tm_fpscr);
            kvm_set_one_reg(cs, KVM_REG_PPC_TM_AMR, &env->tm_amr);
            kvm_set_one_reg(cs, KVM_REG_PPC_TM_PPR, &env->tm_ppr);
            kvm_set_one_reg(cs, KVM_REG_PPC_TM_VRSAVE, &env->tm_vrsave);
            kvm_set_one_reg(cs, KVM_REG_PPC_TM_VSCR, &env->tm_vscr);
            kvm_set_one_reg(cs, KVM_REG_PPC_TM_DSCR, &env->tm_dscr);
            kvm_set_one_reg(cs, KVM_REG_PPC_TM_TAR, &env->tm_tar);
        }

992 993
        if (cap_papr) {
            if (kvm_put_vpa(cs) < 0) {
994
                DPRINTF("Warning: Unable to set VPA information to KVM\n");
995 996
            }
        }
997 998

        kvm_set_one_reg(cs, KVM_REG_PPC_TB_OFFSET, &env->tb_env->tb_offset);
999
#endif /* TARGET_PPC64 */
1000 1001
    }

A
aurel32 已提交
1002 1003 1004
    return ret;
}

1005 1006 1007 1008 1009
static void kvm_sync_excp(CPUPPCState *env, int vector, int ivor)
{
     env->excp_vectors[vector] = env->spr[ivor] + env->spr[SPR_BOOKE_IVPR];
}

1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154
static int kvmppc_get_booke_sregs(PowerPCCPU *cpu)
{
    CPUPPCState *env = &cpu->env;
    struct kvm_sregs sregs;
    int ret;

    ret = kvm_vcpu_ioctl(CPU(cpu), KVM_GET_SREGS, &sregs);
    if (ret < 0) {
        return ret;
    }

    if (sregs.u.e.features & KVM_SREGS_E_BASE) {
        env->spr[SPR_BOOKE_CSRR0] = sregs.u.e.csrr0;
        env->spr[SPR_BOOKE_CSRR1] = sregs.u.e.csrr1;
        env->spr[SPR_BOOKE_ESR] = sregs.u.e.esr;
        env->spr[SPR_BOOKE_DEAR] = sregs.u.e.dear;
        env->spr[SPR_BOOKE_MCSR] = sregs.u.e.mcsr;
        env->spr[SPR_BOOKE_TSR] = sregs.u.e.tsr;
        env->spr[SPR_BOOKE_TCR] = sregs.u.e.tcr;
        env->spr[SPR_DECR] = sregs.u.e.dec;
        env->spr[SPR_TBL] = sregs.u.e.tb & 0xffffffff;
        env->spr[SPR_TBU] = sregs.u.e.tb >> 32;
        env->spr[SPR_VRSAVE] = sregs.u.e.vrsave;
    }

    if (sregs.u.e.features & KVM_SREGS_E_ARCH206) {
        env->spr[SPR_BOOKE_PIR] = sregs.u.e.pir;
        env->spr[SPR_BOOKE_MCSRR0] = sregs.u.e.mcsrr0;
        env->spr[SPR_BOOKE_MCSRR1] = sregs.u.e.mcsrr1;
        env->spr[SPR_BOOKE_DECAR] = sregs.u.e.decar;
        env->spr[SPR_BOOKE_IVPR] = sregs.u.e.ivpr;
    }

    if (sregs.u.e.features & KVM_SREGS_E_64) {
        env->spr[SPR_BOOKE_EPCR] = sregs.u.e.epcr;
    }

    if (sregs.u.e.features & KVM_SREGS_E_SPRG8) {
        env->spr[SPR_BOOKE_SPRG8] = sregs.u.e.sprg8;
    }

    if (sregs.u.e.features & KVM_SREGS_E_IVOR) {
        env->spr[SPR_BOOKE_IVOR0] = sregs.u.e.ivor_low[0];
        kvm_sync_excp(env, POWERPC_EXCP_CRITICAL,  SPR_BOOKE_IVOR0);
        env->spr[SPR_BOOKE_IVOR1] = sregs.u.e.ivor_low[1];
        kvm_sync_excp(env, POWERPC_EXCP_MCHECK,  SPR_BOOKE_IVOR1);
        env->spr[SPR_BOOKE_IVOR2] = sregs.u.e.ivor_low[2];
        kvm_sync_excp(env, POWERPC_EXCP_DSI,  SPR_BOOKE_IVOR2);
        env->spr[SPR_BOOKE_IVOR3] = sregs.u.e.ivor_low[3];
        kvm_sync_excp(env, POWERPC_EXCP_ISI,  SPR_BOOKE_IVOR3);
        env->spr[SPR_BOOKE_IVOR4] = sregs.u.e.ivor_low[4];
        kvm_sync_excp(env, POWERPC_EXCP_EXTERNAL,  SPR_BOOKE_IVOR4);
        env->spr[SPR_BOOKE_IVOR5] = sregs.u.e.ivor_low[5];
        kvm_sync_excp(env, POWERPC_EXCP_ALIGN,  SPR_BOOKE_IVOR5);
        env->spr[SPR_BOOKE_IVOR6] = sregs.u.e.ivor_low[6];
        kvm_sync_excp(env, POWERPC_EXCP_PROGRAM,  SPR_BOOKE_IVOR6);
        env->spr[SPR_BOOKE_IVOR7] = sregs.u.e.ivor_low[7];
        kvm_sync_excp(env, POWERPC_EXCP_FPU,  SPR_BOOKE_IVOR7);
        env->spr[SPR_BOOKE_IVOR8] = sregs.u.e.ivor_low[8];
        kvm_sync_excp(env, POWERPC_EXCP_SYSCALL,  SPR_BOOKE_IVOR8);
        env->spr[SPR_BOOKE_IVOR9] = sregs.u.e.ivor_low[9];
        kvm_sync_excp(env, POWERPC_EXCP_APU,  SPR_BOOKE_IVOR9);
        env->spr[SPR_BOOKE_IVOR10] = sregs.u.e.ivor_low[10];
        kvm_sync_excp(env, POWERPC_EXCP_DECR,  SPR_BOOKE_IVOR10);
        env->spr[SPR_BOOKE_IVOR11] = sregs.u.e.ivor_low[11];
        kvm_sync_excp(env, POWERPC_EXCP_FIT,  SPR_BOOKE_IVOR11);
        env->spr[SPR_BOOKE_IVOR12] = sregs.u.e.ivor_low[12];
        kvm_sync_excp(env, POWERPC_EXCP_WDT,  SPR_BOOKE_IVOR12);
        env->spr[SPR_BOOKE_IVOR13] = sregs.u.e.ivor_low[13];
        kvm_sync_excp(env, POWERPC_EXCP_DTLB,  SPR_BOOKE_IVOR13);
        env->spr[SPR_BOOKE_IVOR14] = sregs.u.e.ivor_low[14];
        kvm_sync_excp(env, POWERPC_EXCP_ITLB,  SPR_BOOKE_IVOR14);
        env->spr[SPR_BOOKE_IVOR15] = sregs.u.e.ivor_low[15];
        kvm_sync_excp(env, POWERPC_EXCP_DEBUG,  SPR_BOOKE_IVOR15);

        if (sregs.u.e.features & KVM_SREGS_E_SPE) {
            env->spr[SPR_BOOKE_IVOR32] = sregs.u.e.ivor_high[0];
            kvm_sync_excp(env, POWERPC_EXCP_SPEU,  SPR_BOOKE_IVOR32);
            env->spr[SPR_BOOKE_IVOR33] = sregs.u.e.ivor_high[1];
            kvm_sync_excp(env, POWERPC_EXCP_EFPDI,  SPR_BOOKE_IVOR33);
            env->spr[SPR_BOOKE_IVOR34] = sregs.u.e.ivor_high[2];
            kvm_sync_excp(env, POWERPC_EXCP_EFPRI,  SPR_BOOKE_IVOR34);
        }

        if (sregs.u.e.features & KVM_SREGS_E_PM) {
            env->spr[SPR_BOOKE_IVOR35] = sregs.u.e.ivor_high[3];
            kvm_sync_excp(env, POWERPC_EXCP_EPERFM,  SPR_BOOKE_IVOR35);
        }

        if (sregs.u.e.features & KVM_SREGS_E_PC) {
            env->spr[SPR_BOOKE_IVOR36] = sregs.u.e.ivor_high[4];
            kvm_sync_excp(env, POWERPC_EXCP_DOORI,  SPR_BOOKE_IVOR36);
            env->spr[SPR_BOOKE_IVOR37] = sregs.u.e.ivor_high[5];
            kvm_sync_excp(env, POWERPC_EXCP_DOORCI, SPR_BOOKE_IVOR37);
        }
    }

    if (sregs.u.e.features & KVM_SREGS_E_ARCH206_MMU) {
        env->spr[SPR_BOOKE_MAS0] = sregs.u.e.mas0;
        env->spr[SPR_BOOKE_MAS1] = sregs.u.e.mas1;
        env->spr[SPR_BOOKE_MAS2] = sregs.u.e.mas2;
        env->spr[SPR_BOOKE_MAS3] = sregs.u.e.mas7_3 & 0xffffffff;
        env->spr[SPR_BOOKE_MAS4] = sregs.u.e.mas4;
        env->spr[SPR_BOOKE_MAS6] = sregs.u.e.mas6;
        env->spr[SPR_BOOKE_MAS7] = sregs.u.e.mas7_3 >> 32;
        env->spr[SPR_MMUCFG] = sregs.u.e.mmucfg;
        env->spr[SPR_BOOKE_TLB0CFG] = sregs.u.e.tlbcfg[0];
        env->spr[SPR_BOOKE_TLB1CFG] = sregs.u.e.tlbcfg[1];
    }

    if (sregs.u.e.features & KVM_SREGS_EXP) {
        env->spr[SPR_BOOKE_EPR] = sregs.u.e.epr;
    }

    if (sregs.u.e.features & KVM_SREGS_E_PD) {
        env->spr[SPR_BOOKE_EPLC] = sregs.u.e.eplc;
        env->spr[SPR_BOOKE_EPSC] = sregs.u.e.epsc;
    }

    if (sregs.u.e.impl_id == KVM_SREGS_E_IMPL_FSL) {
        env->spr[SPR_E500_SVR] = sregs.u.e.impl.fsl.svr;
        env->spr[SPR_Exxx_MCAR] = sregs.u.e.impl.fsl.mcar;
        env->spr[SPR_HID0] = sregs.u.e.impl.fsl.hid0;

        if (sregs.u.e.impl.fsl.features & KVM_SREGS_E_FSL_PIDn) {
            env->spr[SPR_BOOKE_PID1] = sregs.u.e.impl.fsl.pid1;
            env->spr[SPR_BOOKE_PID2] = sregs.u.e.impl.fsl.pid2;
        }
    }

    return 0;
}

static int kvmppc_get_books_sregs(PowerPCCPU *cpu)
{
    CPUPPCState *env = &cpu->env;
    struct kvm_sregs sregs;
    int ret;
    int i;

    ret = kvm_vcpu_ioctl(CPU(cpu), KVM_GET_SREGS, &sregs);
    if (ret < 0) {
        return ret;
    }

1155
    if (!cpu->vhyp) {
1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195
        ppc_store_sdr1(env, sregs.u.s.sdr1);
    }

    /* Sync SLB */
#ifdef TARGET_PPC64
    /*
     * The packed SLB array we get from KVM_GET_SREGS only contains
     * information about valid entries. So we flush our internal copy
     * to get rid of stale ones, then put all valid SLB entries back
     * in.
     */
    memset(env->slb, 0, sizeof(env->slb));
    for (i = 0; i < ARRAY_SIZE(env->slb); i++) {
        target_ulong rb = sregs.u.s.ppc64.slb[i].slbe;
        target_ulong rs = sregs.u.s.ppc64.slb[i].slbv;
        /*
         * Only restore valid entries
         */
        if (rb & SLB_ESID_V) {
            ppc_store_slb(cpu, rb & 0xfff, rb & ~0xfffULL, rs);
        }
    }
#endif

    /* Sync SRs */
    for (i = 0; i < 16; i++) {
        env->sr[i] = sregs.u.s.ppc32.sr[i];
    }

    /* Sync BATs */
    for (i = 0; i < 8; i++) {
        env->DBAT[0][i] = sregs.u.s.ppc32.dbat[i] & 0xffffffff;
        env->DBAT[1][i] = sregs.u.s.ppc32.dbat[i] >> 32;
        env->IBAT[0][i] = sregs.u.s.ppc32.ibat[i] & 0xffffffff;
        env->IBAT[1][i] = sregs.u.s.ppc32.ibat[i] >> 32;
    }

    return 0;
}

A
Andreas Färber 已提交
1196
int kvm_arch_get_registers(CPUState *cs)
A
aurel32 已提交
1197
{
A
Andreas Färber 已提交
1198 1199
    PowerPCCPU *cpu = POWERPC_CPU(cs);
    CPUPPCState *env = &cpu->env;
A
aurel32 已提交
1200
    struct kvm_regs regs;
S
Scott Wood 已提交
1201
    uint32_t cr;
A
Alexander Graf 已提交
1202
    int i, ret;
A
aurel32 已提交
1203

1204
    ret = kvm_vcpu_ioctl(cs, KVM_GET_REGS, &regs);
A
aurel32 已提交
1205 1206 1207
    if (ret < 0)
        return ret;

S
Scott Wood 已提交
1208 1209 1210 1211 1212
    cr = regs.cr;
    for (i = 7; i >= 0; i--) {
        env->crf[i] = cr & 15;
        cr >>= 4;
    }
1213

A
aurel32 已提交
1214 1215
    env->ctr = regs.ctr;
    env->lr = regs.lr;
1216
    cpu_write_xer(env, regs.xer);
A
aurel32 已提交
1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231
    env->msr = regs.msr;
    env->nip = regs.pc;

    env->spr[SPR_SRR0] = regs.srr0;
    env->spr[SPR_SRR1] = regs.srr1;

    env->spr[SPR_SPRG0] = regs.sprg0;
    env->spr[SPR_SPRG1] = regs.sprg1;
    env->spr[SPR_SPRG2] = regs.sprg2;
    env->spr[SPR_SPRG3] = regs.sprg3;
    env->spr[SPR_SPRG4] = regs.sprg4;
    env->spr[SPR_SPRG5] = regs.sprg5;
    env->spr[SPR_SPRG6] = regs.sprg6;
    env->spr[SPR_SPRG7] = regs.sprg7;

S
Scott Wood 已提交
1232 1233
    env->spr[SPR_BOOKE_PID] = regs.pid;

A
aurel32 已提交
1234 1235 1236
    for (i = 0;i < 32; i++)
        env->gpr[i] = regs.gpr[i];

1237 1238
    kvm_get_fp(cs);

S
Scott Wood 已提交
1239
    if (cap_booke_sregs) {
1240
        ret = kvmppc_get_booke_sregs(cpu);
S
Scott Wood 已提交
1241 1242 1243
        if (ret < 0) {
            return ret;
        }
A
Alexander Graf 已提交
1244
    }
S
Scott Wood 已提交
1245 1246

    if (cap_segstate) {
1247
        ret = kvmppc_get_books_sregs(cpu);
S
Scott Wood 已提交
1248 1249 1250
        if (ret < 0) {
            return ret;
        }
A
Alexander Graf 已提交
1251
    }
1252

1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270
    if (cap_hior) {
        kvm_get_one_spr(cs, KVM_REG_PPC_HIOR, SPR_HIOR);
    }

    if (cap_one_reg) {
        int i;

        /* We deliberately ignore errors here, for kernels which have
         * the ONE_REG calls, but don't support the specific
         * registers, there's a reasonable chance things will still
         * work, at least until we try to migrate. */
        for (i = 0; i < 1024; i++) {
            uint64_t id = env->spr_cb[i].one_reg_id;

            if (id != 0) {
                kvm_get_one_spr(cs, id, i);
            }
        }
1271 1272

#ifdef TARGET_PPC64
1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291
        if (msr_ts) {
            for (i = 0; i < ARRAY_SIZE(env->tm_gpr); i++) {
                kvm_get_one_reg(cs, KVM_REG_PPC_TM_GPR(i), &env->tm_gpr[i]);
            }
            for (i = 0; i < ARRAY_SIZE(env->tm_vsr); i++) {
                kvm_get_one_reg(cs, KVM_REG_PPC_TM_VSR(i), &env->tm_vsr[i]);
            }
            kvm_get_one_reg(cs, KVM_REG_PPC_TM_CR, &env->tm_cr);
            kvm_get_one_reg(cs, KVM_REG_PPC_TM_LR, &env->tm_lr);
            kvm_get_one_reg(cs, KVM_REG_PPC_TM_CTR, &env->tm_ctr);
            kvm_get_one_reg(cs, KVM_REG_PPC_TM_FPSCR, &env->tm_fpscr);
            kvm_get_one_reg(cs, KVM_REG_PPC_TM_AMR, &env->tm_amr);
            kvm_get_one_reg(cs, KVM_REG_PPC_TM_PPR, &env->tm_ppr);
            kvm_get_one_reg(cs, KVM_REG_PPC_TM_VRSAVE, &env->tm_vrsave);
            kvm_get_one_reg(cs, KVM_REG_PPC_TM_VSCR, &env->tm_vscr);
            kvm_get_one_reg(cs, KVM_REG_PPC_TM_DSCR, &env->tm_dscr);
            kvm_get_one_reg(cs, KVM_REG_PPC_TM_TAR, &env->tm_tar);
        }

1292 1293
        if (cap_papr) {
            if (kvm_get_vpa(cs) < 0) {
1294
                DPRINTF("Warning: Unable to get VPA information from KVM\n");
1295 1296
            }
        }
1297 1298

        kvm_get_one_reg(cs, KVM_REG_PPC_TB_OFFSET, &env->tb_env->tb_offset);
1299
#endif
1300 1301
    }

A
aurel32 已提交
1302 1303 1304
    return 0;
}

1305
int kvmppc_set_interrupt(PowerPCCPU *cpu, int irq, int level)
1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316
{
    unsigned virq = level ? KVM_INTERRUPT_SET_LEVEL : KVM_INTERRUPT_UNSET;

    if (irq != PPC_INTERRUPT_EXT) {
        return 0;
    }

    if (!kvm_enabled() || !cap_interrupt_unset || !cap_interrupt_level) {
        return 0;
    }

1317
    kvm_vcpu_ioctl(CPU(cpu), KVM_INTERRUPT, &virq);
1318 1319 1320 1321

    return 0;
}

A
Alexander Graf 已提交
1322 1323 1324 1325 1326 1327 1328 1329
#if defined(TARGET_PPCEMB)
#define PPC_INPUT_INT PPC40x_INPUT_INT
#elif defined(TARGET_PPC64)
#define PPC_INPUT_INT PPC970_INPUT_INT
#else
#define PPC_INPUT_INT PPC6xx_INPUT_INT
#endif

A
Andreas Färber 已提交
1330
void kvm_arch_pre_run(CPUState *cs, struct kvm_run *run)
A
aurel32 已提交
1331
{
A
Andreas Färber 已提交
1332 1333
    PowerPCCPU *cpu = POWERPC_CPU(cs);
    CPUPPCState *env = &cpu->env;
A
aurel32 已提交
1334 1335 1336
    int r;
    unsigned irq;

1337 1338
    qemu_mutex_lock_iothread();

S
Stefan Weil 已提交
1339
    /* PowerPC QEMU tracks the various core input pins (interrupt, critical
A
aurel32 已提交
1340
     * interrupt, reset, etc) in PPC-specific env->irq_input_state. */
1341 1342
    if (!cap_interrupt_level &&
        run->ready_for_interrupt_injection &&
1343
        (cs->interrupt_request & CPU_INTERRUPT_HARD) &&
A
Alexander Graf 已提交
1344
        (env->irq_input_state & (1<<PPC_INPUT_INT)))
A
aurel32 已提交
1345 1346 1347 1348 1349
    {
        /* For now KVM disregards the 'irq' argument. However, in the
         * future KVM could cache it in-kernel to avoid a heavyweight exit
         * when reading the UIC.
         */
1350
        irq = KVM_INTERRUPT_SET;
A
aurel32 已提交
1351

1352
        DPRINTF("injected interrupt %d\n", irq);
1353
        r = kvm_vcpu_ioctl(cs, KVM_INTERRUPT, &irq);
1354 1355 1356
        if (r < 0) {
            printf("cpu %d fail inject %x\n", cs->cpu_index, irq);
        }
A
Alexander Graf 已提交
1357 1358

        /* Always wake up soon in case the interrupt was level based */
1359
        timer_mod(idle_timer, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) +
1360
                       (NANOSECONDS_PER_SECOND / 50));
A
aurel32 已提交
1361 1362 1363 1364 1365
    }

    /* We don't know if there are more interrupts pending after this. However,
     * the guest will return to userspace in the course of handling this one
     * anyways, so we will get a chance to deliver the rest. */
1366 1367

    qemu_mutex_unlock_iothread();
A
aurel32 已提交
1368 1369
}

1370
MemTxAttrs kvm_arch_post_run(CPUState *cs, struct kvm_run *run)
A
aurel32 已提交
1371
{
1372
    return MEMTXATTRS_UNSPECIFIED;
A
aurel32 已提交
1373 1374
}

A
Andreas Färber 已提交
1375
int kvm_arch_process_async_events(CPUState *cs)
M
Marcelo Tosatti 已提交
1376
{
1377
    return cs->halted;
M
Marcelo Tosatti 已提交
1378 1379
}

1380
static int kvmppc_handle_halt(PowerPCCPU *cpu)
A
aurel32 已提交
1381
{
1382 1383 1384 1385 1386
    CPUState *cs = CPU(cpu);
    CPUPPCState *env = &cpu->env;

    if (!(cs->interrupt_request & CPU_INTERRUPT_HARD) && (msr_ee)) {
        cs->halted = 1;
1387
        cs->exception_index = EXCP_HLT;
A
aurel32 已提交
1388 1389
    }

1390
    return 0;
A
aurel32 已提交
1391 1392 1393
}

/* map dcr access to existing qemu dcr emulation */
1394
static int kvmppc_handle_dcr_read(CPUPPCState *env, uint32_t dcrn, uint32_t *data)
A
aurel32 已提交
1395 1396 1397 1398
{
    if (ppc_dcr_read(env->dcr_env, dcrn, data) < 0)
        fprintf(stderr, "Read to unhandled DCR (0x%x)\n", dcrn);

1399
    return 0;
A
aurel32 已提交
1400 1401
}

1402
static int kvmppc_handle_dcr_write(CPUPPCState *env, uint32_t dcrn, uint32_t data)
A
aurel32 已提交
1403 1404 1405 1406
{
    if (ppc_dcr_write(env->dcr_env, dcrn, data) < 0)
        fprintf(stderr, "Write to unhandled DCR (0x%x)\n", dcrn);

1407
    return 0;
A
aurel32 已提交
1408 1409
}

1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437
int kvm_arch_insert_sw_breakpoint(CPUState *cs, struct kvm_sw_breakpoint *bp)
{
    /* Mixed endian case is not handled */
    uint32_t sc = debug_inst_opcode;

    if (cpu_memory_rw_debug(cs, bp->pc, (uint8_t *)&bp->saved_insn,
                            sizeof(sc), 0) ||
        cpu_memory_rw_debug(cs, bp->pc, (uint8_t *)&sc, sizeof(sc), 1)) {
        return -EINVAL;
    }

    return 0;
}

int kvm_arch_remove_sw_breakpoint(CPUState *cs, struct kvm_sw_breakpoint *bp)
{
    uint32_t sc;

    if (cpu_memory_rw_debug(cs, bp->pc, (uint8_t *)&sc, sizeof(sc), 0) ||
        sc != debug_inst_opcode ||
        cpu_memory_rw_debug(cs, bp->pc, (uint8_t *)&bp->saved_insn,
                            sizeof(sc), 1)) {
        return -EINVAL;
    }

    return 0;
}

1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557
static int find_hw_breakpoint(target_ulong addr, int type)
{
    int n;

    assert((nb_hw_breakpoint + nb_hw_watchpoint)
           <= ARRAY_SIZE(hw_debug_points));

    for (n = 0; n < nb_hw_breakpoint + nb_hw_watchpoint; n++) {
        if (hw_debug_points[n].addr == addr &&
             hw_debug_points[n].type == type) {
            return n;
        }
    }

    return -1;
}

static int find_hw_watchpoint(target_ulong addr, int *flag)
{
    int n;

    n = find_hw_breakpoint(addr, GDB_WATCHPOINT_ACCESS);
    if (n >= 0) {
        *flag = BP_MEM_ACCESS;
        return n;
    }

    n = find_hw_breakpoint(addr, GDB_WATCHPOINT_WRITE);
    if (n >= 0) {
        *flag = BP_MEM_WRITE;
        return n;
    }

    n = find_hw_breakpoint(addr, GDB_WATCHPOINT_READ);
    if (n >= 0) {
        *flag = BP_MEM_READ;
        return n;
    }

    return -1;
}

int kvm_arch_insert_hw_breakpoint(target_ulong addr,
                                  target_ulong len, int type)
{
    if ((nb_hw_breakpoint + nb_hw_watchpoint) >= ARRAY_SIZE(hw_debug_points)) {
        return -ENOBUFS;
    }

    hw_debug_points[nb_hw_breakpoint + nb_hw_watchpoint].addr = addr;
    hw_debug_points[nb_hw_breakpoint + nb_hw_watchpoint].type = type;

    switch (type) {
    case GDB_BREAKPOINT_HW:
        if (nb_hw_breakpoint >= max_hw_breakpoint) {
            return -ENOBUFS;
        }

        if (find_hw_breakpoint(addr, type) >= 0) {
            return -EEXIST;
        }

        nb_hw_breakpoint++;
        break;

    case GDB_WATCHPOINT_WRITE:
    case GDB_WATCHPOINT_READ:
    case GDB_WATCHPOINT_ACCESS:
        if (nb_hw_watchpoint >= max_hw_watchpoint) {
            return -ENOBUFS;
        }

        if (find_hw_breakpoint(addr, type) >= 0) {
            return -EEXIST;
        }

        nb_hw_watchpoint++;
        break;

    default:
        return -ENOSYS;
    }

    return 0;
}

int kvm_arch_remove_hw_breakpoint(target_ulong addr,
                                  target_ulong len, int type)
{
    int n;

    n = find_hw_breakpoint(addr, type);
    if (n < 0) {
        return -ENOENT;
    }

    switch (type) {
    case GDB_BREAKPOINT_HW:
        nb_hw_breakpoint--;
        break;

    case GDB_WATCHPOINT_WRITE:
    case GDB_WATCHPOINT_READ:
    case GDB_WATCHPOINT_ACCESS:
        nb_hw_watchpoint--;
        break;

    default:
        return -ENOSYS;
    }
    hw_debug_points[n] = hw_debug_points[nb_hw_breakpoint + nb_hw_watchpoint];

    return 0;
}

void kvm_arch_remove_all_hw_breakpoints(void)
{
    nb_hw_breakpoint = nb_hw_watchpoint = 0;
}

1558 1559
void kvm_arch_update_guest_debug(CPUState *cs, struct kvm_guest_debug *dbg)
{
1560 1561
    int n;

1562 1563 1564 1565
    /* Software Breakpoint updates */
    if (kvm_sw_breakpoints_active(cs)) {
        dbg->control |= KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP;
    }
1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594

    assert((nb_hw_breakpoint + nb_hw_watchpoint)
           <= ARRAY_SIZE(hw_debug_points));
    assert((nb_hw_breakpoint + nb_hw_watchpoint) <= ARRAY_SIZE(dbg->arch.bp));

    if (nb_hw_breakpoint + nb_hw_watchpoint > 0) {
        dbg->control |= KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_HW_BP;
        memset(dbg->arch.bp, 0, sizeof(dbg->arch.bp));
        for (n = 0; n < nb_hw_breakpoint + nb_hw_watchpoint; n++) {
            switch (hw_debug_points[n].type) {
            case GDB_BREAKPOINT_HW:
                dbg->arch.bp[n].type = KVMPPC_DEBUG_BREAKPOINT;
                break;
            case GDB_WATCHPOINT_WRITE:
                dbg->arch.bp[n].type = KVMPPC_DEBUG_WATCH_WRITE;
                break;
            case GDB_WATCHPOINT_READ:
                dbg->arch.bp[n].type = KVMPPC_DEBUG_WATCH_READ;
                break;
            case GDB_WATCHPOINT_ACCESS:
                dbg->arch.bp[n].type = KVMPPC_DEBUG_WATCH_WRITE |
                                        KVMPPC_DEBUG_WATCH_READ;
                break;
            default:
                cpu_abort(cs, "Unsupported breakpoint type\n");
            }
            dbg->arch.bp[n].addr = hw_debug_points[n].addr;
        }
    }
1595 1596 1597 1598 1599 1600 1601 1602
}

static int kvm_handle_debug(PowerPCCPU *cpu, struct kvm_run *run)
{
    CPUState *cs = CPU(cpu);
    CPUPPCState *env = &cpu->env;
    struct kvm_debug_exit_arch *arch_info = &run->debug.arch;
    int handle = 0;
1603 1604
    int n;
    int flag = 0;
1605

1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626
    if (cs->singlestep_enabled) {
        handle = 1;
    } else if (arch_info->status) {
        if (nb_hw_breakpoint + nb_hw_watchpoint > 0) {
            if (arch_info->status & KVMPPC_DEBUG_BREAKPOINT) {
                n = find_hw_breakpoint(arch_info->address, GDB_BREAKPOINT_HW);
                if (n >= 0) {
                    handle = 1;
                }
            } else if (arch_info->status & (KVMPPC_DEBUG_WATCH_READ |
                                            KVMPPC_DEBUG_WATCH_WRITE)) {
                n = find_hw_watchpoint(arch_info->address,  &flag);
                if (n >= 0) {
                    handle = 1;
                    cs->watchpoint_hit = &hw_watchpoint;
                    hw_watchpoint.vaddr = hw_debug_points[n].addr;
                    hw_watchpoint.flags = flag;
                }
            }
        }
    } else if (kvm_find_sw_breakpoint(cs, arch_info->address)) {
1627 1628 1629 1630 1631
        handle = 1;
    } else {
        /* QEMU is not able to handle debug exception, so inject
         * program exception to guest;
         * Yes program exception NOT debug exception !!
1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642
         * When QEMU is using debug resources then debug exception must
         * be always set. To achieve this we set MSR_DE and also set
         * MSRP_DEP so guest cannot change MSR_DE.
         * When emulating debug resource for guest we want guest
         * to control MSR_DE (enable/disable debug interrupt on need).
         * Supporting both configurations are NOT possible.
         * So the result is that we cannot share debug resources
         * between QEMU and Guest on BOOKE architecture.
         * In the current design QEMU gets the priority over guest,
         * this means that if QEMU is using debug resources then guest
         * cannot use them;
1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662
         * For software breakpoint QEMU uses a privileged instruction;
         * So there cannot be any reason that we are here for guest
         * set debug exception, only possibility is guest executed a
         * privileged / illegal instruction and that's why we are
         * injecting a program interrupt.
         */

        cpu_synchronize_state(cs);
        /* env->nip is PC, so increment this by 4 to use
         * ppc_cpu_do_interrupt(), which set srr0 = env->nip - 4.
         */
        env->nip += 4;
        cs->exception_index = POWERPC_EXCP_PROGRAM;
        env->error_code = POWERPC_EXCP_INVAL;
        ppc_cpu_do_interrupt(cs);
    }

    return handle;
}

A
Andreas Färber 已提交
1663
int kvm_arch_handle_exit(CPUState *cs, struct kvm_run *run)
A
aurel32 已提交
1664
{
A
Andreas Färber 已提交
1665 1666
    PowerPCCPU *cpu = POWERPC_CPU(cs);
    CPUPPCState *env = &cpu->env;
1667
    int ret;
A
aurel32 已提交
1668

1669 1670
    qemu_mutex_lock_iothread();

A
aurel32 已提交
1671 1672 1673
    switch (run->exit_reason) {
    case KVM_EXIT_DCR:
        if (run->dcr.is_write) {
1674
            DPRINTF("handle dcr write\n");
A
aurel32 已提交
1675 1676
            ret = kvmppc_handle_dcr_write(env, run->dcr.dcrn, run->dcr.data);
        } else {
1677
            DPRINTF("handle dcr read\n");
A
aurel32 已提交
1678 1679 1680 1681
            ret = kvmppc_handle_dcr_read(env, run->dcr.dcrn, &run->dcr.data);
        }
        break;
    case KVM_EXIT_HLT:
1682
        DPRINTF("handle halt\n");
1683
        ret = kvmppc_handle_halt(cpu);
A
aurel32 已提交
1684
        break;
1685
#if defined(TARGET_PPC64)
1686
    case KVM_EXIT_PAPR_HCALL:
1687
        DPRINTF("handle PAPR hypercall\n");
A
Andreas Färber 已提交
1688
        run->papr_hcall.ret = spapr_hypercall(cpu,
1689
                                              run->papr_hcall.nr,
1690
                                              run->papr_hcall.args);
1691
        ret = 0;
1692 1693
        break;
#endif
1694
    case KVM_EXIT_EPR:
1695
        DPRINTF("handle epr\n");
1696
        run->epr.epr = ldl_phys(cs->as, env->mpic_iack);
1697 1698
        ret = 0;
        break;
B
Bharat Bhushan 已提交
1699
    case KVM_EXIT_WATCHDOG:
1700
        DPRINTF("handle watchdog expiry\n");
B
Bharat Bhushan 已提交
1701 1702 1703 1704
        watchdog_perform_action();
        ret = 0;
        break;

1705 1706 1707 1708 1709 1710 1711 1712 1713 1714
    case KVM_EXIT_DEBUG:
        DPRINTF("handle debug exception\n");
        if (kvm_handle_debug(cpu, run)) {
            ret = EXCP_DEBUG;
            break;
        }
        /* re-enter, this exception was guest-internal */
        ret = 0;
        break;

J
Jan Kiszka 已提交
1715 1716 1717 1718
    default:
        fprintf(stderr, "KVM: unknown exit reason %d\n", run->exit_reason);
        ret = -1;
        break;
A
aurel32 已提交
1719 1720
    }

1721
    qemu_mutex_unlock_iothread();
A
aurel32 已提交
1722 1723 1724
    return ret;
}

B
Bharat Bhushan 已提交
1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777
int kvmppc_or_tsr_bits(PowerPCCPU *cpu, uint32_t tsr_bits)
{
    CPUState *cs = CPU(cpu);
    uint32_t bits = tsr_bits;
    struct kvm_one_reg reg = {
        .id = KVM_REG_PPC_OR_TSR,
        .addr = (uintptr_t) &bits,
    };

    return kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, &reg);
}

int kvmppc_clear_tsr_bits(PowerPCCPU *cpu, uint32_t tsr_bits)
{

    CPUState *cs = CPU(cpu);
    uint32_t bits = tsr_bits;
    struct kvm_one_reg reg = {
        .id = KVM_REG_PPC_CLEAR_TSR,
        .addr = (uintptr_t) &bits,
    };

    return kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, &reg);
}

int kvmppc_set_tcr(PowerPCCPU *cpu)
{
    CPUState *cs = CPU(cpu);
    CPUPPCState *env = &cpu->env;
    uint32_t tcr = env->spr[SPR_BOOKE_TCR];

    struct kvm_one_reg reg = {
        .id = KVM_REG_PPC_TCR,
        .addr = (uintptr_t) &tcr,
    };

    return kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, &reg);
}

int kvmppc_booke_watchdog_enable(PowerPCCPU *cpu)
{
    CPUState *cs = CPU(cpu);
    int ret;

    if (!kvm_enabled()) {
        return -1;
    }

    if (!cap_ppc_watchdog) {
        printf("warning: KVM does not support watchdog");
        return -1;
    }

C
Cornelia Huck 已提交
1778
    ret = kvm_vcpu_enable_cap(cs, KVM_CAP_PPC_BOOKE_WATCHDOG, 0);
B
Bharat Bhushan 已提交
1779 1780 1781 1782 1783 1784 1785 1786 1787
    if (ret < 0) {
        fprintf(stderr, "%s: couldn't enable KVM_CAP_PPC_BOOKE_WATCHDOG: %s\n",
                __func__, strerror(-ret));
        return ret;
    }

    return ret;
}

1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800
static int read_cpuinfo(const char *field, char *value, int len)
{
    FILE *f;
    int ret = -1;
    int field_len = strlen(field);
    char line[512];

    f = fopen("/proc/cpuinfo", "r");
    if (!f) {
        return -1;
    }

    do {
1801
        if (!fgets(line, sizeof(line), f)) {
1802 1803 1804
            break;
        }
        if (!strncmp(line, field, field_len)) {
1805
            pstrcpy(value, len, line);
1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819
            ret = 0;
            break;
        }
    } while(*line);

    fclose(f);

    return ret;
}

uint32_t kvmppc_get_tbfreq(void)
{
    char line[512];
    char *ns;
1820
    uint32_t retval = NANOSECONDS_PER_SECOND;
1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831

    if (read_cpuinfo("timebase", line, sizeof(line))) {
        return retval;
    }

    if (!(ns = strchr(line, ':'))) {
        return retval;
    }

    ns++;

1832
    return atoi(ns);
1833
}
1834

1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845
bool kvmppc_get_host_serial(char **value)
{
    return g_file_get_contents("/proc/device-tree/system-id", value, NULL,
                               NULL);
}

bool kvmppc_get_host_model(char **value)
{
    return g_file_get_contents("/proc/device-tree/model", value, NULL, NULL);
}

1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878
/* Try to find a device tree node for a CPU with clock-frequency property */
static int kvmppc_find_cpu_dt(char *buf, int buf_len)
{
    struct dirent *dirp;
    DIR *dp;

    if ((dp = opendir(PROC_DEVTREE_CPU)) == NULL) {
        printf("Can't open directory " PROC_DEVTREE_CPU "\n");
        return -1;
    }

    buf[0] = '\0';
    while ((dirp = readdir(dp)) != NULL) {
        FILE *f;
        snprintf(buf, buf_len, "%s%s/clock-frequency", PROC_DEVTREE_CPU,
                 dirp->d_name);
        f = fopen(buf, "r");
        if (f) {
            snprintf(buf, buf_len, "%s%s", PROC_DEVTREE_CPU, dirp->d_name);
            fclose(f);
            break;
        }
        buf[0] = '\0';
    }
    closedir(dp);
    if (buf[0] == '\0') {
        printf("Unknown host!\n");
        return -1;
    }

    return 0;
}

1879
static uint64_t kvmppc_read_int_dt(const char *filename)
1880
{
1881 1882 1883 1884
    union {
        uint32_t v32;
        uint64_t v64;
    } u;
1885 1886 1887
    FILE *f;
    int len;

1888
    f = fopen(filename, "rb");
1889 1890 1891 1892
    if (!f) {
        return -1;
    }

1893
    len = fread(&u, 1, sizeof(u), f);
1894 1895
    fclose(f);
    switch (len) {
1896 1897 1898 1899 1900
    case 4:
        /* property is a 32-bit quantity */
        return be32_to_cpu(u.v32);
    case 8:
        return be64_to_cpu(u.v64);
1901 1902 1903 1904 1905
    }

    return 0;
}

1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925
/* Read a CPU node property from the host device tree that's a single
 * integer (32-bit or 64-bit).  Returns 0 if anything goes wrong
 * (can't find or open the property, or doesn't understand the
 * format) */
static uint64_t kvmppc_read_int_cpu_dt(const char *propname)
{
    char buf[PATH_MAX], *tmp;
    uint64_t val;

    if (kvmppc_find_cpu_dt(buf, sizeof(buf))) {
        return -1;
    }

    tmp = g_strdup_printf("%s/%s", buf, propname);
    val = kvmppc_read_int_dt(tmp);
    g_free(tmp);

    return val;
}

1926 1927 1928 1929 1930
uint64_t kvmppc_get_clockfreq(void)
{
    return kvmppc_read_int_cpu_dt("clock-frequency");
}

1931 1932 1933 1934 1935 1936 1937 1938 1939 1940
uint32_t kvmppc_get_vmx(void)
{
    return kvmppc_read_int_cpu_dt("ibm,vmx");
}

uint32_t kvmppc_get_dfp(void)
{
    return kvmppc_read_int_cpu_dt("ibm,dfp");
}

1941 1942 1943 1944 1945
static int kvmppc_get_pvinfo(CPUPPCState *env, struct kvm_ppc_pvinfo *pvinfo)
 {
     PowerPCCPU *cpu = ppc_env_get_cpu(env);
     CPUState *cs = CPU(cpu);

1946
    if (kvm_vm_check_extension(cs->kvm_state, KVM_CAP_PPC_GET_PVINFO) &&
1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965
        !kvm_vm_ioctl(cs->kvm_state, KVM_PPC_GET_PVINFO, pvinfo)) {
        return 0;
    }

    return 1;
}

int kvmppc_get_hasidle(CPUPPCState *env)
{
    struct kvm_ppc_pvinfo pvinfo;

    if (!kvmppc_get_pvinfo(env, &pvinfo) &&
        (pvinfo.flags & KVM_PPC_PVINFO_FLAGS_EV_IDLE)) {
        return 1;
    }

    return 0;
}

1966
int kvmppc_get_hypercall(CPUPPCState *env, uint8_t *buf, int buf_len)
1967 1968 1969 1970
{
    uint32_t *hc = (uint32_t*)buf;
    struct kvm_ppc_pvinfo pvinfo;

1971
    if (!kvmppc_get_pvinfo(env, &pvinfo)) {
1972 1973 1974 1975 1976
        memcpy(buf, pvinfo.hcall, buf_len);
        return 0;
    }

    /*
1977
     * Fallback to always fail hypercalls regardless of endianness:
1978
     *
1979
     *     tdi 0,r0,72 (becomes b .+8 in wrong endian, nop in good endian)
1980
     *     li r3, -1
1981 1982
     *     b .+8       (becomes nop in wrong endian)
     *     bswap32(li r3, -1)
1983 1984
     */

1985 1986 1987 1988
    hc[0] = cpu_to_be32(0x08000048);
    hc[1] = cpu_to_be32(0x3860ffff);
    hc[2] = cpu_to_be32(0x48000008);
    hc[3] = cpu_to_be32(bswap32(0x3860ffff));
1989

1990
    return 1;
1991 1992
}

1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009
static inline int kvmppc_enable_hcall(KVMState *s, target_ulong hcall)
{
    return kvm_vm_enable_cap(s, KVM_CAP_PPC_ENABLE_HCALL, 0, hcall, 1);
}

void kvmppc_enable_logical_ci_hcalls(void)
{
    /*
     * FIXME: it would be nice if we could detect the cases where
     * we're using a device which requires the in kernel
     * implementation of these hcalls, but the kernel lacks them and
     * produce a warning.
     */
    kvmppc_enable_hcall(kvm_state, H_LOGICAL_CI_LOAD);
    kvmppc_enable_hcall(kvm_state, H_LOGICAL_CI_STORE);
}

2010 2011 2012 2013 2014
void kvmppc_enable_set_mode_hcall(void)
{
    kvmppc_enable_hcall(kvm_state, H_SET_MODE);
}

2015 2016 2017 2018 2019 2020
void kvmppc_enable_clear_ref_mod_hcalls(void)
{
    kvmppc_enable_hcall(kvm_state, H_CLEAR_REF);
    kvmppc_enable_hcall(kvm_state, H_CLEAR_MOD);
}

2021
void kvmppc_set_papr(PowerPCCPU *cpu)
2022
{
2023
    CPUState *cs = CPU(cpu);
2024 2025
    int ret;

C
Cornelia Huck 已提交
2026
    ret = kvm_vcpu_enable_cap(cs, KVM_CAP_PPC_PAPR, 0);
2027
    if (ret) {
2028 2029
        error_report("This vCPU type or KVM version does not support PAPR");
        exit(1);
2030
    }
2031 2032 2033 2034

    /* Update the capability flag so we sync the right information
     * with kvm */
    cap_papr = 1;
2035 2036
}

2037
int kvmppc_set_compat(PowerPCCPU *cpu, uint32_t compat_pvr)
2038
{
2039
    return kvm_set_one_reg(CPU(cpu), KVM_REG_PPC_ARCH_COMPAT, &compat_pvr);
2040 2041
}

2042 2043 2044 2045 2046
void kvmppc_set_mpic_proxy(PowerPCCPU *cpu, int mpic_proxy)
{
    CPUState *cs = CPU(cpu);
    int ret;

C
Cornelia Huck 已提交
2047
    ret = kvm_vcpu_enable_cap(cs, KVM_CAP_PPC_EPR, 0, mpic_proxy);
2048
    if (ret && mpic_proxy) {
2049 2050
        error_report("This KVM version does not support EPR");
        exit(1);
2051 2052 2053
    }
}

2054 2055 2056 2057 2058
int kvmppc_smt_threads(void)
{
    return cap_ppc_smt ? cap_ppc_smt : 1;
}

2059
#ifdef TARGET_PPC64
2060
off_t kvmppc_alloc_rma(void **rma)
2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086
{
    off_t size;
    int fd;
    struct kvm_allocate_rma ret;

    /* If cap_ppc_rma == 0, contiguous RMA allocation is not supported
     * if cap_ppc_rma == 1, contiguous RMA allocation is supported, but
     *                      not necessary on this hardware
     * if cap_ppc_rma == 2, contiguous RMA allocation is needed on this hardware
     *
     * FIXME: We should allow the user to force contiguous RMA
     * allocation in the cap_ppc_rma==1 case.
     */
    if (cap_ppc_rma < 2) {
        return 0;
    }

    fd = kvm_vm_ioctl(kvm_state, KVM_ALLOCATE_RMA, &ret);
    if (fd < 0) {
        fprintf(stderr, "KVM: Error on KVM_ALLOCATE_RMA: %s\n",
                strerror(errno));
        return -1;
    }

    size = MIN(ret.rma_size, 256ul << 20);

2087 2088
    *rma = mmap(NULL, size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
    if (*rma == MAP_FAILED) {
2089 2090 2091 2092 2093 2094 2095
        fprintf(stderr, "KVM: Error mapping RMA: %s\n", strerror(errno));
        return -1;
    };

    return size;
}

2096 2097
uint64_t kvmppc_rma_size(uint64_t current_size, unsigned int hash_shift)
{
2098 2099 2100 2101
    struct kvm_ppc_smmu_info info;
    long rampagesize, best_page_shift;
    int i;

2102 2103 2104
    if (cap_ppc_rma >= 2) {
        return current_size;
    }
2105 2106 2107

    /* Find the largest hardware supported page size that's less than
     * or equal to the (logical) backing page size of guest RAM */
2108
    kvm_get_smmu_info(POWERPC_CPU(first_cpu), &info);
2109
    rampagesize = qemu_getrampagesize();
2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124
    best_page_shift = 0;

    for (i = 0; i < KVM_PPC_PAGE_SIZES_MAX_SZ; i++) {
        struct kvm_ppc_one_seg_page_size *sps = &info.sps[i];

        if (!sps->page_shift) {
            continue;
        }

        if ((sps->page_shift > best_page_shift)
            && ((1UL << sps->page_shift) <= rampagesize)) {
            best_page_shift = sps->page_shift;
        }
    }

2125
    return MIN(current_size,
2126
               1ULL << (best_page_shift + hash_shift - 7));
2127 2128 2129
}
#endif

2130 2131 2132 2133 2134
bool kvmppc_spapr_use_multitce(void)
{
    return cap_spapr_multitce;
}

2135
void *kvmppc_create_spapr_tce(uint32_t liobn, uint32_t window_size, int *pfd,
2136
                              bool need_vfio)
2137 2138 2139 2140 2141 2142 2143 2144 2145
{
    struct kvm_create_spapr_tce args = {
        .liobn = liobn,
        .window_size = window_size,
    };
    long len;
    int fd;
    void *table;

2146 2147 2148 2149
    /* Must set fd to -1 so we don't try to munmap when called for
     * destroying the table, which the upper layers -will- do
     */
    *pfd = -1;
2150
    if (!cap_spapr_tce || (need_vfio && !cap_spapr_vfio)) {
2151 2152 2153 2154 2155
        return NULL;
    }

    fd = kvm_vm_ioctl(kvm_state, KVM_CREATE_SPAPR_TCE, &args);
    if (fd < 0) {
2156 2157
        fprintf(stderr, "KVM: Failed to create TCE table for liobn 0x%x\n",
                liobn);
2158 2159 2160
        return NULL;
    }

2161
    len = (window_size / SPAPR_TCE_PAGE_SIZE) * sizeof(uint64_t);
2162 2163
    /* FIXME: round this up to page size */

2164
    table = mmap(NULL, len, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
2165
    if (table == MAP_FAILED) {
2166 2167
        fprintf(stderr, "KVM: Failed to map TCE table for liobn 0x%x\n",
                liobn);
2168 2169 2170 2171 2172 2173 2174 2175
        close(fd);
        return NULL;
    }

    *pfd = fd;
    return table;
}

2176
int kvmppc_remove_spapr_tce(void *table, int fd, uint32_t nb_table)
2177 2178 2179 2180 2181 2182 2183
{
    long len;

    if (fd < 0) {
        return -1;
    }

2184
    len = nb_table * sizeof(uint64_t);
2185 2186
    if ((munmap(table, len) < 0) ||
        (close(fd) < 0)) {
2187 2188
        fprintf(stderr, "KVM: Unexpected error removing TCE table: %s",
                strerror(errno));
2189 2190 2191 2192 2193 2194
        /* Leak the table */
    }

    return 0;
}

2195 2196 2197 2198
int kvmppc_reset_htab(int shift_hint)
{
    uint32_t shift = shift_hint;

2199 2200 2201 2202 2203
    if (!kvm_enabled()) {
        /* Full emulation, tell caller to allocate htab itself */
        return 0;
    }
    if (kvm_check_extension(kvm_state, KVM_CAP_PPC_ALLOC_HTAB)) {
2204 2205
        int ret;
        ret = kvm_vm_ioctl(kvm_state, KVM_PPC_ALLOCATE_HTAB, &shift);
2206 2207 2208 2209 2210 2211 2212
        if (ret == -ENOTTY) {
            /* At least some versions of PR KVM advertise the
             * capability, but don't implement the ioctl().  Oops.
             * Return 0 so that we allocate the htab in qemu, as is
             * correct for PR. */
            return 0;
        } else if (ret < 0) {
2213 2214 2215 2216 2217
            return ret;
        }
        return shift;
    }

2218 2219
    /* We have a kernel that predates the htab reset calls.  For PR
     * KVM, we need to allocate the htab ourselves, for an HV KVM of
2220 2221
     * this era, it has allocated a 16MB fixed size hash table already. */
    if (kvmppc_is_pr(kvm_state)) {
2222 2223 2224 2225 2226 2227
        /* PR - tell caller to allocate htab */
        return 0;
    } else {
        /* HV - assume 16MB kernel allocated htab */
        return 24;
    }
2228 2229
}

2230 2231 2232 2233 2234 2235 2236 2237 2238
static inline uint32_t mfpvr(void)
{
    uint32_t pvr;

    asm ("mfpvr %0"
         : "=r"(pvr));
    return pvr;
}

2239 2240 2241 2242 2243 2244 2245 2246 2247
static void alter_insns(uint64_t *word, uint64_t flags, bool on)
{
    if (on) {
        *word |= flags;
    } else {
        *word &= ~flags;
    }
}

2248
static void kvmppc_host_cpu_initfn(Object *obj)
2249
{
2250 2251 2252 2253 2254
    assert(kvm_enabled());
}

static void kvmppc_host_cpu_class_init(ObjectClass *oc, void *data)
{
2255
    DeviceClass *dc = DEVICE_CLASS(oc);
2256
    PowerPCCPUClass *pcc = POWERPC_CPU_CLASS(oc);
2257 2258
    uint32_t vmx = kvmppc_get_vmx();
    uint32_t dfp = kvmppc_get_dfp();
2259 2260
    uint32_t dcache_size = kvmppc_read_int_cpu_dt("d-cache-size");
    uint32_t icache_size = kvmppc_read_int_cpu_dt("i-cache-size");
2261

2262
    /* Now fix up the class with information we can query from the host */
2263
    pcc->pvr = mfpvr();
2264

2265 2266
    if (vmx != -1) {
        /* Only override when we know what the host supports */
2267 2268
        alter_insns(&pcc->insns_flags, PPC_ALTIVEC, vmx > 0);
        alter_insns(&pcc->insns_flags2, PPC2_VSX, vmx > 1);
2269 2270 2271
    }
    if (dfp != -1) {
        /* Only override when we know what the host supports */
2272
        alter_insns(&pcc->insns_flags2, PPC2_DFP, dfp);
2273
    }
2274 2275 2276 2277 2278 2279 2280 2281

    if (dcache_size != -1) {
        pcc->l1_dcache_size = dcache_size;
    }

    if (icache_size != -1) {
        pcc->l1_icache_size = icache_size;
    }
2282 2283 2284

    /* Reason: kvmppc_host_cpu_initfn() dies when !kvm_enabled() */
    dc->cannot_destroy_with_object_finalize_yet = true;
2285 2286
}

2287 2288 2289 2290 2291
bool kvmppc_has_cap_epr(void)
{
    return cap_epr;
}

2292 2293 2294 2295 2296
bool kvmppc_has_cap_htab_fd(void)
{
    return cap_htab_fd;
}

2297 2298 2299 2300 2301
bool kvmppc_has_cap_fixup_hcalls(void)
{
    return cap_fixup_hcalls;
}

2302 2303 2304 2305 2306
bool kvmppc_has_cap_htm(void)
{
    return cap_htm;
}

2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318
static PowerPCCPUClass *ppc_cpu_get_family_class(PowerPCCPUClass *pcc)
{
    ObjectClass *oc = OBJECT_CLASS(pcc);

    while (oc && !object_class_is_abstract(oc)) {
        oc = object_class_get_parent(oc);
    }
    assert(oc);

    return POWERPC_CPU_CLASS(oc);
}

2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331
PowerPCCPUClass *kvm_ppc_get_host_cpu_class(void)
{
    uint32_t host_pvr = mfpvr();
    PowerPCCPUClass *pvr_pcc;

    pvr_pcc = ppc_cpu_class_by_pvr(host_pvr);
    if (pvr_pcc == NULL) {
        pvr_pcc = ppc_cpu_class_by_pvr_mask(host_pvr);
    }

    return pvr_pcc;
}

2332 2333 2334 2335 2336 2337 2338 2339
static int kvm_ppc_register_host_cpu_type(void)
{
    TypeInfo type_info = {
        .name = TYPE_HOST_POWERPC_CPU,
        .instance_init = kvmppc_host_cpu_initfn,
        .class_init = kvmppc_host_cpu_class_init,
    };
    PowerPCCPUClass *pvr_pcc;
2340
    DeviceClass *dc;
2341
    int i;
2342

2343
    pvr_pcc = kvm_ppc_get_host_cpu_class();
2344 2345 2346 2347 2348
    if (pvr_pcc == NULL) {
        return -1;
    }
    type_info.parent = object_class_get_name(OBJECT_CLASS(pvr_pcc));
    type_register(&type_info);
2349

2350 2351 2352
#if defined(TARGET_PPC64)
    type_info.name = g_strdup_printf("%s-"TYPE_SPAPR_CPU_CORE, "host");
    type_info.parent = TYPE_SPAPR_CPU_CORE,
2353 2354 2355 2356
    type_info.instance_size = sizeof(sPAPRCPUCore);
    type_info.instance_init = NULL;
    type_info.class_init = spapr_cpu_core_class_init;
    type_info.class_data = (void *) "host";
2357 2358 2359 2360
    type_register(&type_info);
    g_free((void *)type_info.name);
#endif

2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381
    /*
     * Update generic CPU family class alias (e.g. on a POWER8NVL host,
     * we want "POWER8" to be a "family" alias that points to the current
     * host CPU type, too)
     */
    dc = DEVICE_CLASS(ppc_cpu_get_family_class(pvr_pcc));
    for (i = 0; ppc_cpu_aliases[i].alias != NULL; i++) {
        if (strcmp(ppc_cpu_aliases[i].alias, dc->desc) == 0) {
            ObjectClass *oc = OBJECT_CLASS(pvr_pcc);
            char *suffix;

            ppc_cpu_aliases[i].model = g_strdup(object_class_get_name(oc));
            suffix = strstr(ppc_cpu_aliases[i].model, "-"TYPE_POWERPC_CPU);
            if (suffix) {
                *suffix = 0;
            }
            ppc_cpu_aliases[i].oc = oc;
            break;
        }
    }

2382 2383 2384
    return 0;
}

2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398
int kvmppc_define_rtas_kernel_token(uint32_t token, const char *function)
{
    struct kvm_rtas_token_args args = {
        .token = token,
    };

    if (!kvm_check_extension(kvm_state, KVM_CAP_PPC_RTAS)) {
        return -ENOENT;
    }

    strncpy(args.name, function, sizeof(args.name));

    return kvm_vm_ioctl(kvm_state, KVM_PPC_RTAS_DEFINE_TOKEN, &args);
}
2399

2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416
int kvmppc_get_htab_fd(bool write)
{
    struct kvm_get_htab_fd s = {
        .flags = write ? KVM_GET_HTAB_WRITE : 0,
        .start_index = 0,
    };

    if (!cap_htab_fd) {
        fprintf(stderr, "KVM version doesn't support saving the hash table\n");
        return -1;
    }

    return kvm_vm_ioctl(kvm_state, KVM_PPC_GET_HTAB_FD, &s);
}

int kvmppc_save_htab(QEMUFile *f, int fd, size_t bufsize, int64_t max_ns)
{
2417
    int64_t starttime = qemu_clock_get_ns(QEMU_CLOCK_REALTIME);
2418 2419 2420 2421 2422 2423 2424 2425 2426 2427
    uint8_t buf[bufsize];
    ssize_t rc;

    do {
        rc = read(fd, buf, bufsize);
        if (rc < 0) {
            fprintf(stderr, "Error reading data from KVM HTAB fd: %s\n",
                    strerror(errno));
            return rc;
        } else if (rc) {
2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444
            uint8_t *buffer = buf;
            ssize_t n = rc;
            while (n) {
                struct kvm_get_htab_header *head =
                    (struct kvm_get_htab_header *) buffer;
                size_t chunksize = sizeof(*head) +
                     HASH_PTE_SIZE_64 * head->n_valid;

                qemu_put_be32(f, head->index);
                qemu_put_be16(f, head->n_valid);
                qemu_put_be16(f, head->n_invalid);
                qemu_put_buffer(f, (void *)(head + 1),
                                HASH_PTE_SIZE_64 * head->n_valid);

                buffer += chunksize;
                n -= chunksize;
            }
2445 2446 2447
        }
    } while ((rc != 0)
             && ((max_ns < 0)
2448
                 || ((qemu_clock_get_ns(QEMU_CLOCK_REALTIME) - starttime) < max_ns)));
2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480

    return (rc == 0) ? 1 : 0;
}

int kvmppc_load_htab_chunk(QEMUFile *f, int fd, uint32_t index,
                           uint16_t n_valid, uint16_t n_invalid)
{
    struct kvm_get_htab_header *buf;
    size_t chunksize = sizeof(*buf) + n_valid*HASH_PTE_SIZE_64;
    ssize_t rc;

    buf = alloca(chunksize);
    buf->index = index;
    buf->n_valid = n_valid;
    buf->n_invalid = n_invalid;

    qemu_get_buffer(f, (void *)(buf + 1), HASH_PTE_SIZE_64*n_valid);

    rc = write(fd, buf, chunksize);
    if (rc < 0) {
        fprintf(stderr, "Error writing KVM hash table: %s\n",
                strerror(errno));
        return rc;
    }
    if (rc != chunksize) {
        /* We should never get a short write on a single chunk */
        fprintf(stderr, "Short write, restoring KVM hash table\n");
        return -1;
    }
    return 0;
}

A
Andreas Färber 已提交
2481
bool kvm_arch_stop_on_emulation_error(CPUState *cpu)
2482 2483 2484
{
    return true;
}
2485

A
Andreas Färber 已提交
2486
int kvm_arch_on_sigbus_vcpu(CPUState *cpu, int code, void *addr)
2487 2488 2489 2490 2491 2492 2493 2494
{
    return 1;
}

int kvm_arch_on_sigbus(int code, void *addr)
{
    return 1;
}
2495 2496 2497 2498

void kvm_arch_init_irq_routing(KVMState *s)
{
}
2499

2500
void kvmppc_read_hptes(ppc_hash_pte64_t *hptes, hwaddr ptex, int n)
2501
{
2502 2503 2504 2505 2506 2507
    struct kvm_get_htab_fd ghf = {
        .flags = 0,
        .start_index = ptex,
    };
    int fd, rc;
    int i;
2508

2509 2510 2511
    fd = kvm_vm_ioctl(kvm_state, KVM_PPC_GET_HTAB_FD, &ghf);
    if (fd < 0) {
        hw_error("kvmppc_read_hptes: Unable to open HPT fd");
2512 2513
    }

2514 2515 2516 2517 2518
    i = 0;
    while (i < n) {
        struct kvm_get_htab_header *hdr;
        int m = n < HPTES_PER_GROUP ? n : HPTES_PER_GROUP;
        char buf[sizeof(*hdr) + m * HASH_PTE_SIZE_64];
2519

2520 2521 2522 2523
        rc = read(fd, buf, sizeof(buf));
        if (rc < 0) {
            hw_error("kvmppc_read_hptes: Unable to read HPTEs");
        }
2524

2525 2526 2527
        hdr = (struct kvm_get_htab_header *)buf;
        while ((i < n) && ((char *)hdr < (buf + rc))) {
            int invalid = hdr->n_invalid;
2528

2529 2530 2531 2532 2533 2534 2535
            if (hdr->index != (ptex + i)) {
                hw_error("kvmppc_read_hptes: Unexpected HPTE index %"PRIu32
                         " != (%"HWADDR_PRIu" + %d", hdr->index, ptex, i);
            }

            memcpy(hptes + i, hdr + 1, HASH_PTE_SIZE_64 * hdr->n_valid);
            i += hdr->n_valid;
2536

2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548
            if ((n - i) < invalid) {
                invalid = n - i;
            }
            memset(hptes + i, 0, invalid * HASH_PTE_SIZE_64);
            i += hdr->n_invalid;

            hdr = (struct kvm_get_htab_header *)
                ((char *)(hdr + 1) + HASH_PTE_SIZE_64 * hdr->n_valid);
        }
    }

    close(fd);
2549
}
2550

2551
void kvmppc_write_hpte(hwaddr ptex, uint64_t pte0, uint64_t pte1)
2552
{
2553
    int fd, rc;
2554
    struct kvm_get_htab_fd ghf;
2555 2556 2557 2558 2559
    struct {
        struct kvm_get_htab_header hdr;
        uint64_t pte0;
        uint64_t pte1;
    } buf;
2560 2561 2562

    ghf.flags = 0;
    ghf.start_index = 0;     /* Ignored */
2563 2564 2565
    fd = kvm_vm_ioctl(kvm_state, KVM_PPC_GET_HTAB_FD, &ghf);
    if (fd < 0) {
        hw_error("kvmppc_write_hpte: Unable to open HPT fd");
2566 2567
    }

2568 2569 2570 2571 2572
    buf.hdr.n_valid = 1;
    buf.hdr.n_invalid = 0;
    buf.hdr.index = ptex;
    buf.pte0 = cpu_to_be64(pte0);
    buf.pte1 = cpu_to_be64(pte1);
2573

2574 2575 2576 2577 2578
    rc = write(fd, &buf, sizeof(buf));
    if (rc != sizeof(buf)) {
        hw_error("kvmppc_write_hpte: Unable to update KVM HPT");
    }
    close(fd);
2579
}
2580 2581

int kvm_arch_fixup_msi_route(struct kvm_irq_routing_entry *route,
2582
                             uint64_t address, uint32_t data, PCIDevice *dev)
2583 2584 2585
{
    return 0;
}
2586

2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597
int kvm_arch_add_msi_route_post(struct kvm_irq_routing_entry *route,
                                int vector, PCIDevice *dev)
{
    return 0;
}

int kvm_arch_release_virq_post(int virq)
{
    return 0;
}

2598 2599 2600 2601
int kvm_arch_msi_data_to_gsi(uint32_t data)
{
    return data & 0xffff;
}
2602 2603 2604 2605 2606 2607 2608 2609 2610

int kvmppc_enable_hwrng(void)
{
    if (!kvm_enabled() || !kvm_check_extension(kvm_state, KVM_CAP_PPC_HWRNG)) {
        return -1;
    }

    return kvmppc_enable_hcall(kvm_state, H_RANDOM);
}