~ubuntu-branches/ubuntu/trusty/qemu/trusty

« back to all changes in this revision

Viewing changes to target-ppc/kvm.c

  • Committer: Package Import Robot
  • Author(s): Serge Hallyn
  • Date: 2013-10-22 22:47:07 UTC
  • mfrom: (1.8.3) (10.1.42 sid)
  • Revision ID: package-import@ubuntu.com-20131022224707-1lya34fw3k3f24tv
Tags: 1.6.0+dfsg-2ubuntu1
* Merge 1.6.0~rc0+dfsg-2exp from debian experimental.  Remaining changes:
  - debian/control
    * update maintainer
    * remove libiscsi, usb-redir, vde, vnc-jpeg, and libssh2-1-dev
      from build-deps
    * enable rbd
    * add qemu-system and qemu-common B/R to qemu-keymaps
    * add D:udev, R:qemu, R:qemu-common and B:qemu-common to
      qemu-system-common
    * qemu-system-arm, qemu-system-ppc, qemu-system-sparc:
      - add qemu-kvm to Provides
      - add qemu-common, qemu-kvm, kvm to B/R
      - remove openbios-sparc from qemu-system-sparc D
      - drop openbios-ppc and openhackware Depends to Suggests (for now)
    * qemu-system-x86:
      - add qemu-common to Breaks/Replaces.
      - add cpu-checker to Recommends.
    * qemu-user: add B/R:qemu-kvm
    * qemu-kvm:
      - add armhf armel powerpc sparc to Architecture
      - C/R/P: qemu-kvm-spice
    * add qemu-common package
    * drop qemu-slof which is not packaged in ubuntu
  - add qemu-system-common.links for tap ifup/down scripts and OVMF link.
  - qemu-system-x86.links:
    * remove pxe rom links which are in kvm-ipxe
    * add symlink for kvm.1 manpage
  - debian/rules
    * add kvm-spice symlink to qemu-kvm
    * call dh_installmodules for qemu-system-x86
    * update dh_installinit to install upstart script
    * run dh_installman (Closes: #709241) (cherrypicked from 1.5.0+dfsg-2)
  - Add qemu-utils.links for kvm-* symlinks.
  - Add qemu-system-x86.qemu-kvm.upstart and .default
  - Add qemu-system-x86.modprobe to set nesting=1
  - Add qemu-system-common.preinst to add kvm group
  - qemu-system-common.postinst: remove bad group acl if there, then have
    udev relabel /dev/kvm.
  - New linaro patches from qemu-linaro rebasing branch
  - Dropped patches:
    * xen-simplify-xen_enabled.patch
    * sparc-linux-user-fix-missing-symbols-in-.rel-.rela.plt-sections.patch
    * main_loop-do-not-set-nonblocking-if-xen_enabled.patch
    * xen_machine_pv-do-not-create-a-dummy-CPU-in-machine-.patch
    * virtio-rng-fix-crash
  - Kept patches:
    * expose_vms_qemu64cpu.patch - updated
    * linaro arm patches from qemu-linaro rebasing branch
  - New patches:
    * fix-pci-add: change CONFIG variable in ifdef to make sure that
      pci_add is defined.
* Add linaro patches
* Add experimental mach-virt patches for arm virtualization.
* qemu-system-common.install: add debian/tmp/usr/lib to install the
  qemu-bridge-helper

Show diffs side-by-side

added added

removed removed

Lines of Context:
30
30
#include "cpu.h"
31
31
#include "sysemu/cpus.h"
32
32
#include "sysemu/device_tree.h"
33
 
#include "hw/sysbus.h"
34
 
#include "hw/ppc/spapr.h"
35
33
#include "mmu-hash64.h"
36
34
 
37
35
#include "hw/sysbus.h"
42
40
//#define DEBUG_KVM
43
41
 
44
42
#ifdef DEBUG_KVM
45
 
#define dprintf(fmt, ...) \
 
43
#define DPRINTF(fmt, ...) \
46
44
    do { fprintf(stderr, fmt, ## __VA_ARGS__); } while (0)
47
45
#else
48
 
#define dprintf(fmt, ...) \
 
46
#define DPRINTF(fmt, ...) \
49
47
    do { } while (0)
50
48
#endif
51
49
 
67
65
static int cap_epr;
68
66
static int cap_ppc_watchdog;
69
67
static int cap_papr;
 
68
static int cap_htab_fd;
70
69
 
71
70
/* XXX We have a race condition where we actually have a level triggered
72
71
 *     interrupt, but the infrastructure can't expose that yet, so the guest
103
102
    cap_ppc_watchdog = kvm_check_extension(s, KVM_CAP_PPC_BOOKE_WATCHDOG);
104
103
    /* Note: we don't set cap_papr here, because this capability is
105
104
     * only activated after this by kvmppc_set_papr() */
 
105
    cap_htab_fd = kvm_check_extension(s, KVM_CAP_PPC_HTAB_FD);
106
106
 
107
107
    if (!cap_interrupt_level) {
108
108
        fprintf(stderr, "KVM: Couldn't find level irq capability. Expect the "
550
550
        reg.addr = (uintptr_t)&fpscr;
551
551
        ret = kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, &reg);
552
552
        if (ret < 0) {
553
 
            dprintf("Unable to set FPSCR to KVM: %s\n", strerror(errno));
 
553
            DPRINTF("Unable to set FPSCR to KVM: %s\n", strerror(errno));
554
554
            return ret;
555
555
        }
556
556
 
564
564
 
565
565
            ret = kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, &reg);
566
566
            if (ret < 0) {
567
 
                dprintf("Unable to set %s%d to KVM: %s\n", vsx ? "VSR" : "FPR",
 
567
                DPRINTF("Unable to set %s%d to KVM: %s\n", vsx ? "VSR" : "FPR",
568
568
                        i, strerror(errno));
569
569
                return ret;
570
570
            }
576
576
        reg.addr = (uintptr_t)&env->vscr;
577
577
        ret = kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, &reg);
578
578
        if (ret < 0) {
579
 
            dprintf("Unable to set VSCR to KVM: %s\n", strerror(errno));
 
579
            DPRINTF("Unable to set VSCR to KVM: %s\n", strerror(errno));
580
580
            return ret;
581
581
        }
582
582
 
585
585
            reg.addr = (uintptr_t)&env->avr[i];
586
586
            ret = kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, &reg);
587
587
            if (ret < 0) {
588
 
                dprintf("Unable to set VR%d to KVM: %s\n", i, strerror(errno));
 
588
                DPRINTF("Unable to set VR%d to KVM: %s\n", i, strerror(errno));
589
589
                return ret;
590
590
            }
591
591
        }
610
610
        reg.addr = (uintptr_t)&fpscr;
611
611
        ret = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, &reg);
612
612
        if (ret < 0) {
613
 
            dprintf("Unable to get FPSCR from KVM: %s\n", strerror(errno));
 
613
            DPRINTF("Unable to get FPSCR from KVM: %s\n", strerror(errno));
614
614
            return ret;
615
615
        } else {
616
616
            env->fpscr = fpscr;
624
624
 
625
625
            ret = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, &reg);
626
626
            if (ret < 0) {
627
 
                dprintf("Unable to get %s%d from KVM: %s\n",
 
627
                DPRINTF("Unable to get %s%d from KVM: %s\n",
628
628
                        vsx ? "VSR" : "FPR", i, strerror(errno));
629
629
                return ret;
630
630
            } else {
641
641
        reg.addr = (uintptr_t)&env->vscr;
642
642
        ret = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, &reg);
643
643
        if (ret < 0) {
644
 
            dprintf("Unable to get VSCR from KVM: %s\n", strerror(errno));
 
644
            DPRINTF("Unable to get VSCR from KVM: %s\n", strerror(errno));
645
645
            return ret;
646
646
        }
647
647
 
650
650
            reg.addr = (uintptr_t)&env->avr[i];
651
651
            ret = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, &reg);
652
652
            if (ret < 0) {
653
 
                dprintf("Unable to get VR%d from KVM: %s\n",
 
653
                DPRINTF("Unable to get VR%d from KVM: %s\n",
654
654
                        i, strerror(errno));
655
655
                return ret;
656
656
            }
672
672
    reg.addr = (uintptr_t)&env->vpa_addr;
673
673
    ret = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, &reg);
674
674
    if (ret < 0) {
675
 
        dprintf("Unable to get VPA address from KVM: %s\n", strerror(errno));
 
675
        DPRINTF("Unable to get VPA address from KVM: %s\n", strerror(errno));
676
676
        return ret;
677
677
    }
678
678
 
682
682
    reg.addr = (uintptr_t)&env->slb_shadow_addr;
683
683
    ret = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, &reg);
684
684
    if (ret < 0) {
685
 
        dprintf("Unable to get SLB shadow state from KVM: %s\n",
 
685
        DPRINTF("Unable to get SLB shadow state from KVM: %s\n",
686
686
                strerror(errno));
687
687
        return ret;
688
688
    }
692
692
    reg.addr = (uintptr_t)&env->dtl_addr;
693
693
    ret = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, &reg);
694
694
    if (ret < 0) {
695
 
        dprintf("Unable to get dispatch trace log state from KVM: %s\n",
 
695
        DPRINTF("Unable to get dispatch trace log state from KVM: %s\n",
696
696
                strerror(errno));
697
697
        return ret;
698
698
    }
718
718
        reg.addr = (uintptr_t)&env->vpa_addr;
719
719
        ret = kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, &reg);
720
720
        if (ret < 0) {
721
 
            dprintf("Unable to set VPA address to KVM: %s\n", strerror(errno));
 
721
            DPRINTF("Unable to set VPA address to KVM: %s\n", strerror(errno));
722
722
            return ret;
723
723
        }
724
724
    }
729
729
    reg.addr = (uintptr_t)&env->slb_shadow_addr;
730
730
    ret = kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, &reg);
731
731
    if (ret < 0) {
732
 
        dprintf("Unable to set SLB shadow state to KVM: %s\n", strerror(errno));
 
732
        DPRINTF("Unable to set SLB shadow state to KVM: %s\n", strerror(errno));
733
733
        return ret;
734
734
    }
735
735
 
738
738
    reg.addr = (uintptr_t)&env->dtl_addr;
739
739
    ret = kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, &reg);
740
740
    if (ret < 0) {
741
 
        dprintf("Unable to set dispatch trace log state to KVM: %s\n",
 
741
        DPRINTF("Unable to set dispatch trace log state to KVM: %s\n",
742
742
                strerror(errno));
743
743
        return ret;
744
744
    }
748
748
        reg.addr = (uintptr_t)&env->vpa_addr;
749
749
        ret = kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, &reg);
750
750
        if (ret < 0) {
751
 
            dprintf("Unable to set VPA address to KVM: %s\n", strerror(errno));
 
751
            DPRINTF("Unable to set VPA address to KVM: %s\n", strerror(errno));
752
752
            return ret;
753
753
        }
754
754
    }
793
793
    for (i = 0;i < 32; i++)
794
794
        regs.gpr[i] = env->gpr[i];
795
795
 
 
796
    regs.cr = 0;
 
797
    for (i = 0; i < 8; i++) {
 
798
        regs.cr |= (env->crf[i] & 15) << (4 * (7 - i));
 
799
    }
 
800
 
796
801
    ret = kvm_vcpu_ioctl(cs, KVM_SET_REGS, &regs);
797
802
    if (ret < 0)
798
803
        return ret;
861
866
#ifdef TARGET_PPC64
862
867
        if (cap_papr) {
863
868
            if (kvm_put_vpa(cs) < 0) {
864
 
                dprintf("Warning: Unable to set VPA information to KVM\n");
 
869
                DPRINTF("Warning: Unable to set VPA information to KVM\n");
865
870
            }
866
871
        }
867
872
#endif /* TARGET_PPC64 */
1070
1075
#ifdef TARGET_PPC64
1071
1076
        if (cap_papr) {
1072
1077
            if (kvm_get_vpa(cs) < 0) {
1073
 
                dprintf("Warning: Unable to get VPA information from KVM\n");
 
1078
                DPRINTF("Warning: Unable to get VPA information from KVM\n");
1074
1079
            }
1075
1080
        }
1076
1081
#endif
1124
1129
         */
1125
1130
        irq = KVM_INTERRUPT_SET;
1126
1131
 
1127
 
        dprintf("injected interrupt %d\n", irq);
 
1132
        DPRINTF("injected interrupt %d\n", irq);
1128
1133
        r = kvm_vcpu_ioctl(cs, KVM_INTERRUPT, &irq);
1129
1134
        if (r < 0) {
1130
1135
            printf("cpu %d fail inject %x\n", cs->cpu_index, irq);
1188
1193
    switch (run->exit_reason) {
1189
1194
    case KVM_EXIT_DCR:
1190
1195
        if (run->dcr.is_write) {
1191
 
            dprintf("handle dcr write\n");
 
1196
            DPRINTF("handle dcr write\n");
1192
1197
            ret = kvmppc_handle_dcr_write(env, run->dcr.dcrn, run->dcr.data);
1193
1198
        } else {
1194
 
            dprintf("handle dcr read\n");
 
1199
            DPRINTF("handle dcr read\n");
1195
1200
            ret = kvmppc_handle_dcr_read(env, run->dcr.dcrn, &run->dcr.data);
1196
1201
        }
1197
1202
        break;
1198
1203
    case KVM_EXIT_HLT:
1199
 
        dprintf("handle halt\n");
 
1204
        DPRINTF("handle halt\n");
1200
1205
        ret = kvmppc_handle_halt(cpu);
1201
1206
        break;
1202
1207
#if defined(TARGET_PPC64)
1203
1208
    case KVM_EXIT_PAPR_HCALL:
1204
 
        dprintf("handle PAPR hypercall\n");
 
1209
        DPRINTF("handle PAPR hypercall\n");
1205
1210
        run->papr_hcall.ret = spapr_hypercall(cpu,
1206
1211
                                              run->papr_hcall.nr,
1207
1212
                                              run->papr_hcall.args);
1209
1214
        break;
1210
1215
#endif
1211
1216
    case KVM_EXIT_EPR:
1212
 
        dprintf("handle epr\n");
 
1217
        DPRINTF("handle epr\n");
1213
1218
        run->epr.epr = ldl_phys(env->mpic_iack);
1214
1219
        ret = 0;
1215
1220
        break;
1216
1221
    case KVM_EXIT_WATCHDOG:
1217
 
        dprintf("handle watchdog expiry\n");
 
1222
        DPRINTF("handle watchdog expiry\n");
1218
1223
        watchdog_perform_action();
1219
1224
        ret = 0;
1220
1225
        break;
1557
1562
    };
1558
1563
 
1559
1564
    rma_region = g_new(MemoryRegion, 1);
1560
 
    memory_region_init_ram_ptr(rma_region, name, size, rma);
 
1565
    memory_region_init_ram_ptr(rma_region, NULL, name, size, rma);
1561
1566
    vmstate_register_ram_global(rma_region);
1562
1567
    memory_region_add_subregion(sysmem, 0, rma_region);
1563
1568
 
1576
1581
 
1577
1582
    /* Find the largest hardware supported page size that's less than
1578
1583
     * or equal to the (logical) backing page size of guest RAM */
1579
 
    kvm_get_smmu_info(ppc_env_get_cpu(first_cpu), &info);
 
1584
    kvm_get_smmu_info(POWERPC_CPU(first_cpu), &info);
1580
1585
    rampagesize = getrampagesize();
1581
1586
    best_page_shift = 0;
1582
1587
 
1623
1628
        return NULL;
1624
1629
    }
1625
1630
 
1626
 
    len = (window_size / SPAPR_TCE_PAGE_SIZE) * sizeof(sPAPRTCE);
 
1631
    len = (window_size / SPAPR_TCE_PAGE_SIZE) * sizeof(uint64_t);
1627
1632
    /* FIXME: round this up to page size */
1628
1633
 
1629
1634
    table = mmap(NULL, len, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
1646
1651
        return -1;
1647
1652
    }
1648
1653
 
1649
 
    len = (window_size / SPAPR_TCE_PAGE_SIZE)*sizeof(sPAPRTCE);
 
1654
    len = (window_size / SPAPR_TCE_PAGE_SIZE)*sizeof(uint64_t);
1650
1655
    if ((munmap(table, len) < 0) ||
1651
1656
        (close(fd) < 0)) {
1652
1657
        fprintf(stderr, "KVM: Unexpected error removing TCE table: %s",
1785
1790
}
1786
1791
 
1787
1792
 
 
1793
int kvmppc_get_htab_fd(bool write)
 
1794
{
 
1795
    struct kvm_get_htab_fd s = {
 
1796
        .flags = write ? KVM_GET_HTAB_WRITE : 0,
 
1797
        .start_index = 0,
 
1798
    };
 
1799
 
 
1800
    if (!cap_htab_fd) {
 
1801
        fprintf(stderr, "KVM version doesn't support saving the hash table\n");
 
1802
        return -1;
 
1803
    }
 
1804
 
 
1805
    return kvm_vm_ioctl(kvm_state, KVM_PPC_GET_HTAB_FD, &s);
 
1806
}
 
1807
 
 
1808
int kvmppc_save_htab(QEMUFile *f, int fd, size_t bufsize, int64_t max_ns)
 
1809
{
 
1810
    int64_t starttime = qemu_get_clock_ns(rt_clock);
 
1811
    uint8_t buf[bufsize];
 
1812
    ssize_t rc;
 
1813
 
 
1814
    do {
 
1815
        rc = read(fd, buf, bufsize);
 
1816
        if (rc < 0) {
 
1817
            fprintf(stderr, "Error reading data from KVM HTAB fd: %s\n",
 
1818
                    strerror(errno));
 
1819
            return rc;
 
1820
        } else if (rc) {
 
1821
            /* Kernel already retuns data in BE format for the file */
 
1822
            qemu_put_buffer(f, buf, rc);
 
1823
        }
 
1824
    } while ((rc != 0)
 
1825
             && ((max_ns < 0)
 
1826
                 || ((qemu_get_clock_ns(rt_clock) - starttime) < max_ns)));
 
1827
 
 
1828
    return (rc == 0) ? 1 : 0;
 
1829
}
 
1830
 
 
1831
int kvmppc_load_htab_chunk(QEMUFile *f, int fd, uint32_t index,
 
1832
                           uint16_t n_valid, uint16_t n_invalid)
 
1833
{
 
1834
    struct kvm_get_htab_header *buf;
 
1835
    size_t chunksize = sizeof(*buf) + n_valid*HASH_PTE_SIZE_64;
 
1836
    ssize_t rc;
 
1837
 
 
1838
    buf = alloca(chunksize);
 
1839
    /* This is KVM on ppc, so this is all big-endian */
 
1840
    buf->index = index;
 
1841
    buf->n_valid = n_valid;
 
1842
    buf->n_invalid = n_invalid;
 
1843
 
 
1844
    qemu_get_buffer(f, (void *)(buf + 1), HASH_PTE_SIZE_64*n_valid);
 
1845
 
 
1846
    rc = write(fd, buf, chunksize);
 
1847
    if (rc < 0) {
 
1848
        fprintf(stderr, "Error writing KVM hash table: %s\n",
 
1849
                strerror(errno));
 
1850
        return rc;
 
1851
    }
 
1852
    if (rc != chunksize) {
 
1853
        /* We should never get a short write on a single chunk */
 
1854
        fprintf(stderr, "Short write, restoring KVM hash table\n");
 
1855
        return -1;
 
1856
    }
 
1857
    return 0;
 
1858
}
 
1859
 
1788
1860
bool kvm_arch_stop_on_emulation_error(CPUState *cpu)
1789
1861
{
1790
1862
    return true;
1799
1871
{
1800
1872
    return 1;
1801
1873
}
 
1874
 
 
1875
void kvm_arch_init_irq_routing(KVMState *s)
 
1876
{
 
1877
}