1d76d1650Saurel32 /* 2d76d1650Saurel32 * PowerPC implementation of KVM hooks 3d76d1650Saurel32 * 4d76d1650Saurel32 * Copyright IBM Corp. 2007 590dc8812SScott Wood * Copyright (C) 2011 Freescale Semiconductor, Inc. 6d76d1650Saurel32 * 7d76d1650Saurel32 * Authors: 8d76d1650Saurel32 * Jerone Young <jyoung5@us.ibm.com> 9d76d1650Saurel32 * Christian Ehrhardt <ehrhardt@linux.vnet.ibm.com> 10d76d1650Saurel32 * Hollis Blanchard <hollisb@us.ibm.com> 11d76d1650Saurel32 * 12d76d1650Saurel32 * This work is licensed under the terms of the GNU GPL, version 2 or later. 13d76d1650Saurel32 * See the COPYING file in the top-level directory. 14d76d1650Saurel32 * 15d76d1650Saurel32 */ 16d76d1650Saurel32 170d75590dSPeter Maydell #include "qemu/osdep.h" 18eadaada1SAlexander Graf #include <dirent.h> 19d76d1650Saurel32 #include <sys/ioctl.h> 204656e1f0SBenjamin Herrenschmidt #include <sys/vfs.h> 21d76d1650Saurel32 22d76d1650Saurel32 #include <linux/kvm.h> 23d76d1650Saurel32 24d76d1650Saurel32 #include "qemu-common.h" 2530f4b05bSDavid Gibson #include "qapi/error.h" 26072ed5f2SThomas Huth #include "qemu/error-report.h" 2733c11879SPaolo Bonzini #include "cpu.h" 28715d4b96SThomas Huth #include "cpu-models.h" 291de7afc9SPaolo Bonzini #include "qemu/timer.h" 30b3946626SVincent Palatin #include "sysemu/hw_accel.h" 31d76d1650Saurel32 #include "kvm_ppc.h" 329c17d615SPaolo Bonzini #include "sysemu/cpus.h" 339c17d615SPaolo Bonzini #include "sysemu/device_tree.h" 34d5aea6f3SDavid Gibson #include "mmu-hash64.h" 35d76d1650Saurel32 36f61b4bedSAlexander Graf #include "hw/sysbus.h" 370d09e41aSPaolo Bonzini #include "hw/ppc/spapr.h" 387ebaf795SBharata B Rao #include "hw/ppc/spapr_cpu_core.h" 39650d103dSMarkus Armbruster #include "hw/hw.h" 4098a8b524SAlexey Kardashevskiy #include "hw/ppc/ppc.h" 41ca77ee28SMarkus Armbruster #include "migration/qemu-file-types.h" 4231f2cb8fSBharat Bhushan #include "sysemu/watchdog.h" 43b36f100eSAlexey Kardashevskiy #include "trace.h" 4488365d17SBharat Bhushan #include "exec/gdbstub.h" 454c663752SPaolo Bonzini #include "exec/memattrs.h" 469c607668SAlexey Kardashevskiy #include "exec/ram_addr.h" 472d103aaeSMichael Roth #include "sysemu/hostmem.h" 48f348b6d1SVeronia Bahaa #include "qemu/cutils.h" 49db725815SMarkus Armbruster #include "qemu/main-loop.h" 509c607668SAlexey Kardashevskiy #include "qemu/mmap-alloc.h" 51f3d9f303SSam Bobroff #include "elf.h" 52c64abd1fSSam Bobroff #include "sysemu/kvm_int.h" 53f61b4bedSAlexander Graf 54eadaada1SAlexander Graf #define PROC_DEVTREE_CPU "/proc/device-tree/cpus/" 55eadaada1SAlexander Graf 566e0552a3SFabiano Rosas #define DEBUG_RETURN_GUEST 0 576e0552a3SFabiano Rosas #define DEBUG_RETURN_GDB 1 586e0552a3SFabiano Rosas 5994a8d39aSJan Kiszka const KVMCapabilityInfo kvm_arch_required_capabilities[] = { 6094a8d39aSJan Kiszka KVM_CAP_LAST_INFO 6194a8d39aSJan Kiszka }; 6294a8d39aSJan Kiszka 63c995e942SDavid Gibson static int cap_interrupt_unset; 6490dc8812SScott Wood static int cap_segstate; 6590dc8812SScott Wood static int cap_booke_sregs; 66e97c3636SDavid Gibson static int cap_ppc_smt; 67fa98fbfcSSam Bobroff static int cap_ppc_smt_possible; 680f5cb298SDavid Gibson static int cap_spapr_tce; 69d6ee2a7cSAlexey Kardashevskiy static int cap_spapr_tce_64; 70da95324eSAlexey Kardashevskiy static int cap_spapr_multitce; 719bb62a07SAlexey Kardashevskiy static int cap_spapr_vfio; 72f1af19d7SDavid Gibson static int cap_hior; 73d67d40eaSDavid Gibson static int cap_one_reg; 743b961124SStuart Yoder static int cap_epr; 7531f2cb8fSBharat Bhushan static int cap_ppc_watchdog; 769b00ea49SDavid Gibson static int cap_papr; 77e68cb8b4SAlexey Kardashevskiy static int cap_htab_fd; 7887a91de6SAlexander Graf static int cap_fixup_hcalls; 79bac3bf28SThomas Huth static int cap_htm; /* Hardware transactional memory support */ 80cf1c4cceSSam Bobroff static int cap_mmu_radix; 81cf1c4cceSSam Bobroff static int cap_mmu_hash_v3; 8238afd772SCédric Le Goater static int cap_xive; 83b55d295eSDavid Gibson static int cap_resize_hpt; 84c363a37aSDaniel Henrique Barboza static int cap_ppc_pvr_compat; 858acc2ae5SSuraj Jitindar Singh static int cap_ppc_safe_cache; 868acc2ae5SSuraj Jitindar Singh static int cap_ppc_safe_bounds_check; 878acc2ae5SSuraj Jitindar Singh static int cap_ppc_safe_indirect_branch; 888ff43ee4SSuraj Jitindar Singh static int cap_ppc_count_cache_flush_assist; 89b9a477b7SSuraj Jitindar Singh static int cap_ppc_nested_kvm_hv; 907d050527SSuraj Jitindar Singh static int cap_large_decr; 91ec010c00SNicholas Piggin static int cap_fwnmi; 92*82123b75SBharata B Rao static int cap_rpt_invalidate; 93fc87e185SAlexander Graf 943c902d44SBharat Bhushan static uint32_t debug_inst_opcode; 953c902d44SBharat Bhushan 96c995e942SDavid Gibson /* 97c995e942SDavid Gibson * Check whether we are running with KVM-PR (instead of KVM-HV). This 9896c9cff0SThomas Huth * should only be used for fallback tests - generally we should use 9996c9cff0SThomas Huth * explicit capabilities for the features we want, rather than 100c995e942SDavid Gibson * assuming what is/isn't available depending on the KVM variant. 101c995e942SDavid Gibson */ 10296c9cff0SThomas Huth static bool kvmppc_is_pr(KVMState *ks) 10396c9cff0SThomas Huth { 10496c9cff0SThomas Huth /* Assume KVM-PR if the GET_PVINFO capability is available */ 10570a0c19eSGreg Kurz return kvm_vm_check_extension(ks, KVM_CAP_PPC_GET_PVINFO) != 0; 10696c9cff0SThomas Huth } 10796c9cff0SThomas Huth 108165dc3edSDavid Gibson static int kvm_ppc_register_host_cpu_type(void); 1098acc2ae5SSuraj Jitindar Singh static void kvmppc_get_cpu_characteristics(KVMState *s); 1107d050527SSuraj Jitindar Singh static int kvmppc_get_dec_bits(void); 1115ba4576bSAndreas Färber 112b16565b3SMarcel Apfelbaum int kvm_arch_init(MachineState *ms, KVMState *s) 113d76d1650Saurel32 { 114fc87e185SAlexander Graf cap_interrupt_unset = kvm_check_extension(s, KVM_CAP_PPC_UNSET_IRQ); 11590dc8812SScott Wood cap_segstate = kvm_check_extension(s, KVM_CAP_PPC_SEGSTATE); 11690dc8812SScott Wood cap_booke_sregs = kvm_check_extension(s, KVM_CAP_PPC_BOOKE_SREGS); 1176977afdaSGreg Kurz cap_ppc_smt_possible = kvm_vm_check_extension(s, KVM_CAP_PPC_SMT_POSSIBLE); 1180f5cb298SDavid Gibson cap_spapr_tce = kvm_check_extension(s, KVM_CAP_SPAPR_TCE); 119d6ee2a7cSAlexey Kardashevskiy cap_spapr_tce_64 = kvm_check_extension(s, KVM_CAP_SPAPR_TCE_64); 120da95324eSAlexey Kardashevskiy cap_spapr_multitce = kvm_check_extension(s, KVM_CAP_SPAPR_MULTITCE); 1219ded780cSAlexey Kardashevskiy cap_spapr_vfio = kvm_vm_check_extension(s, KVM_CAP_SPAPR_TCE_VFIO); 122d67d40eaSDavid Gibson cap_one_reg = kvm_check_extension(s, KVM_CAP_ONE_REG); 123f1af19d7SDavid Gibson cap_hior = kvm_check_extension(s, KVM_CAP_PPC_HIOR); 1243b961124SStuart Yoder cap_epr = kvm_check_extension(s, KVM_CAP_PPC_EPR); 12531f2cb8fSBharat Bhushan cap_ppc_watchdog = kvm_check_extension(s, KVM_CAP_PPC_BOOKE_WATCHDOG); 126c995e942SDavid Gibson /* 127c995e942SDavid Gibson * Note: we don't set cap_papr here, because this capability is 128c995e942SDavid Gibson * only activated after this by kvmppc_set_papr() 129c995e942SDavid Gibson */ 1306977afdaSGreg Kurz cap_htab_fd = kvm_vm_check_extension(s, KVM_CAP_PPC_HTAB_FD); 13187a91de6SAlexander Graf cap_fixup_hcalls = kvm_check_extension(s, KVM_CAP_PPC_FIXUP_HCALL); 132fa98fbfcSSam Bobroff cap_ppc_smt = kvm_vm_check_extension(s, KVM_CAP_PPC_SMT); 133bac3bf28SThomas Huth cap_htm = kvm_vm_check_extension(s, KVM_CAP_PPC_HTM); 134cf1c4cceSSam Bobroff cap_mmu_radix = kvm_vm_check_extension(s, KVM_CAP_PPC_MMU_RADIX); 135cf1c4cceSSam Bobroff cap_mmu_hash_v3 = kvm_vm_check_extension(s, KVM_CAP_PPC_MMU_HASH_V3); 13638afd772SCédric Le Goater cap_xive = kvm_vm_check_extension(s, KVM_CAP_PPC_IRQ_XIVE); 137b55d295eSDavid Gibson cap_resize_hpt = kvm_vm_check_extension(s, KVM_CAP_SPAPR_RESIZE_HPT); 1388acc2ae5SSuraj Jitindar Singh kvmppc_get_cpu_characteristics(s); 139b9a477b7SSuraj Jitindar Singh cap_ppc_nested_kvm_hv = kvm_vm_check_extension(s, KVM_CAP_PPC_NESTED_HV); 1407d050527SSuraj Jitindar Singh cap_large_decr = kvmppc_get_dec_bits(); 141ec010c00SNicholas Piggin cap_fwnmi = kvm_vm_check_extension(s, KVM_CAP_PPC_FWNMI); 142c363a37aSDaniel Henrique Barboza /* 143c363a37aSDaniel Henrique Barboza * Note: setting it to false because there is not such capability 144c363a37aSDaniel Henrique Barboza * in KVM at this moment. 145c363a37aSDaniel Henrique Barboza * 146c363a37aSDaniel Henrique Barboza * TODO: call kvm_vm_check_extension() with the right capability 147c995e942SDavid Gibson * after the kernel starts implementing it. 148c995e942SDavid Gibson */ 149c363a37aSDaniel Henrique Barboza cap_ppc_pvr_compat = false; 150fc87e185SAlexander Graf 1511e8f51e8SShivaprasad G Bhat if (!kvm_check_extension(s, KVM_CAP_PPC_IRQ_LEVEL)) { 1521e8f51e8SShivaprasad G Bhat error_report("KVM: Host kernel doesn't have level irq capability"); 1531e8f51e8SShivaprasad G Bhat exit(1); 154fc87e185SAlexander Graf } 155fc87e185SAlexander Graf 156*82123b75SBharata B Rao cap_rpt_invalidate = kvm_vm_check_extension(s, KVM_CAP_PPC_RPT_INVALIDATE); 157165dc3edSDavid Gibson kvm_ppc_register_host_cpu_type(); 1585ba4576bSAndreas Färber 159d76d1650Saurel32 return 0; 160d76d1650Saurel32 } 161d76d1650Saurel32 1624376c40dSPaolo Bonzini int kvm_arch_irqchip_create(KVMState *s) 163d525ffabSPaolo Bonzini { 164d525ffabSPaolo Bonzini return 0; 165d525ffabSPaolo Bonzini } 166d525ffabSPaolo Bonzini 1671bc22652SAndreas Färber static int kvm_arch_sync_sregs(PowerPCCPU *cpu) 168d76d1650Saurel32 { 1691bc22652SAndreas Färber CPUPPCState *cenv = &cpu->env; 1701bc22652SAndreas Färber CPUState *cs = CPU(cpu); 171861bbc80SAlexander Graf struct kvm_sregs sregs; 1725666ca4aSScott Wood int ret; 1735666ca4aSScott Wood 1745666ca4aSScott Wood if (cenv->excp_model == POWERPC_EXCP_BOOKE) { 175c995e942SDavid Gibson /* 176c995e942SDavid Gibson * What we're really trying to say is "if we're on BookE, we 177c995e942SDavid Gibson * use the native PVR for now". This is the only sane way to 178c995e942SDavid Gibson * check it though, so we potentially confuse users that they 179c995e942SDavid Gibson * can run BookE guests on BookS. Let's hope nobody dares 180c995e942SDavid Gibson * enough :) 181c995e942SDavid Gibson */ 1825666ca4aSScott Wood return 0; 1835666ca4aSScott Wood } else { 18490dc8812SScott Wood if (!cap_segstate) { 18564e07be5SAlexander Graf fprintf(stderr, "kvm error: missing PVR setting capability\n"); 18664e07be5SAlexander Graf return -ENOSYS; 1875666ca4aSScott Wood } 1885666ca4aSScott Wood } 1895666ca4aSScott Wood 1901bc22652SAndreas Färber ret = kvm_vcpu_ioctl(cs, KVM_GET_SREGS, &sregs); 1915666ca4aSScott Wood if (ret) { 1925666ca4aSScott Wood return ret; 1935666ca4aSScott Wood } 194861bbc80SAlexander Graf 195861bbc80SAlexander Graf sregs.pvr = cenv->spr[SPR_PVR]; 1961bc22652SAndreas Färber return kvm_vcpu_ioctl(cs, KVM_SET_SREGS, &sregs); 1975666ca4aSScott Wood } 1985666ca4aSScott Wood 19993dd5e85SScott Wood /* Set up a shared TLB array with KVM */ 2001bc22652SAndreas Färber static int kvm_booke206_tlb_init(PowerPCCPU *cpu) 20193dd5e85SScott Wood { 2021bc22652SAndreas Färber CPUPPCState *env = &cpu->env; 2031bc22652SAndreas Färber CPUState *cs = CPU(cpu); 20493dd5e85SScott Wood struct kvm_book3e_206_tlb_params params = {}; 20593dd5e85SScott Wood struct kvm_config_tlb cfg = {}; 20693dd5e85SScott Wood unsigned int entries = 0; 20793dd5e85SScott Wood int ret, i; 20893dd5e85SScott Wood 20993dd5e85SScott Wood if (!kvm_enabled() || 210a60f24b5SAndreas Färber !kvm_check_extension(cs->kvm_state, KVM_CAP_SW_TLB)) { 21193dd5e85SScott Wood return 0; 21293dd5e85SScott Wood } 21393dd5e85SScott Wood 21493dd5e85SScott Wood assert(ARRAY_SIZE(params.tlb_sizes) == BOOKE206_MAX_TLBN); 21593dd5e85SScott Wood 21693dd5e85SScott Wood for (i = 0; i < BOOKE206_MAX_TLBN; i++) { 21793dd5e85SScott Wood params.tlb_sizes[i] = booke206_tlb_size(env, i); 21893dd5e85SScott Wood params.tlb_ways[i] = booke206_tlb_ways(env, i); 21993dd5e85SScott Wood entries += params.tlb_sizes[i]; 22093dd5e85SScott Wood } 22193dd5e85SScott Wood 22293dd5e85SScott Wood assert(entries == env->nb_tlb); 22393dd5e85SScott Wood assert(sizeof(struct kvm_book3e_206_tlb_entry) == sizeof(ppcmas_tlb_t)); 22493dd5e85SScott Wood 22593dd5e85SScott Wood env->tlb_dirty = true; 22693dd5e85SScott Wood 22793dd5e85SScott Wood cfg.array = (uintptr_t)env->tlb.tlbm; 22893dd5e85SScott Wood cfg.array_len = sizeof(ppcmas_tlb_t) * entries; 22993dd5e85SScott Wood cfg.params = (uintptr_t)¶ms; 23093dd5e85SScott Wood cfg.mmu_type = KVM_MMU_FSL_BOOKE_NOHV; 23193dd5e85SScott Wood 23248add816SCornelia Huck ret = kvm_vcpu_enable_cap(cs, KVM_CAP_SW_TLB, 0, (uintptr_t)&cfg); 23393dd5e85SScott Wood if (ret < 0) { 23493dd5e85SScott Wood fprintf(stderr, "%s: couldn't enable KVM_CAP_SW_TLB: %s\n", 23593dd5e85SScott Wood __func__, strerror(-ret)); 23693dd5e85SScott Wood return ret; 23793dd5e85SScott Wood } 23893dd5e85SScott Wood 23993dd5e85SScott Wood env->kvm_sw_tlb = true; 24093dd5e85SScott Wood return 0; 24193dd5e85SScott Wood } 24293dd5e85SScott Wood 2434656e1f0SBenjamin Herrenschmidt 2444656e1f0SBenjamin Herrenschmidt #if defined(TARGET_PPC64) 245ab256960SGreg Kurz static void kvm_get_smmu_info(struct kvm_ppc_smmu_info *info, Error **errp) 2464656e1f0SBenjamin Herrenschmidt { 2474656e1f0SBenjamin Herrenschmidt int ret; 2484656e1f0SBenjamin Herrenschmidt 249ab256960SGreg Kurz assert(kvm_state != NULL); 250ab256960SGreg Kurz 251ab256960SGreg Kurz if (!kvm_check_extension(kvm_state, KVM_CAP_PPC_GET_SMMU_INFO)) { 25271d0f1eaSGreg Kurz error_setg(errp, "KVM doesn't expose the MMU features it supports"); 25371d0f1eaSGreg Kurz error_append_hint(errp, "Consider switching to a newer KVM\n"); 25471d0f1eaSGreg Kurz return; 25571d0f1eaSGreg Kurz } 25671d0f1eaSGreg Kurz 257ab256960SGreg Kurz ret = kvm_vm_ioctl(kvm_state, KVM_PPC_GET_SMMU_INFO, info); 2584656e1f0SBenjamin Herrenschmidt if (ret == 0) { 2594656e1f0SBenjamin Herrenschmidt return; 2604656e1f0SBenjamin Herrenschmidt } 2614656e1f0SBenjamin Herrenschmidt 26271d0f1eaSGreg Kurz error_setg_errno(errp, -ret, 26371d0f1eaSGreg Kurz "KVM failed to provide the MMU features it supports"); 2644656e1f0SBenjamin Herrenschmidt } 2654656e1f0SBenjamin Herrenschmidt 266c64abd1fSSam Bobroff struct ppc_radix_page_info *kvm_get_radix_page_info(void) 267c64abd1fSSam Bobroff { 2684f7f5893SPhilippe Mathieu-Daudé KVMState *s = KVM_STATE(current_accel()); 269c64abd1fSSam Bobroff struct ppc_radix_page_info *radix_page_info; 270c64abd1fSSam Bobroff struct kvm_ppc_rmmu_info rmmu_info; 271c64abd1fSSam Bobroff int i; 272c64abd1fSSam Bobroff 273c64abd1fSSam Bobroff if (!kvm_check_extension(s, KVM_CAP_PPC_MMU_RADIX)) { 274c64abd1fSSam Bobroff return NULL; 275c64abd1fSSam Bobroff } 276c64abd1fSSam Bobroff if (kvm_vm_ioctl(s, KVM_PPC_GET_RMMU_INFO, &rmmu_info)) { 277c64abd1fSSam Bobroff return NULL; 278c64abd1fSSam Bobroff } 279c64abd1fSSam Bobroff radix_page_info = g_malloc0(sizeof(*radix_page_info)); 280c64abd1fSSam Bobroff radix_page_info->count = 0; 281c64abd1fSSam Bobroff for (i = 0; i < PPC_PAGE_SIZES_MAX_SZ; i++) { 282c64abd1fSSam Bobroff if (rmmu_info.ap_encodings[i]) { 283c64abd1fSSam Bobroff radix_page_info->entries[i] = rmmu_info.ap_encodings[i]; 284c64abd1fSSam Bobroff radix_page_info->count++; 285c64abd1fSSam Bobroff } 286c64abd1fSSam Bobroff } 287c64abd1fSSam Bobroff return radix_page_info; 288c64abd1fSSam Bobroff } 289c64abd1fSSam Bobroff 290b4db5413SSuraj Jitindar Singh target_ulong kvmppc_configure_v3_mmu(PowerPCCPU *cpu, 291b4db5413SSuraj Jitindar Singh bool radix, bool gtse, 292b4db5413SSuraj Jitindar Singh uint64_t proc_tbl) 293b4db5413SSuraj Jitindar Singh { 294b4db5413SSuraj Jitindar Singh CPUState *cs = CPU(cpu); 295b4db5413SSuraj Jitindar Singh int ret; 296b4db5413SSuraj Jitindar Singh uint64_t flags = 0; 297b4db5413SSuraj Jitindar Singh struct kvm_ppc_mmuv3_cfg cfg = { 298b4db5413SSuraj Jitindar Singh .process_table = proc_tbl, 299b4db5413SSuraj Jitindar Singh }; 300b4db5413SSuraj Jitindar Singh 301b4db5413SSuraj Jitindar Singh if (radix) { 302b4db5413SSuraj Jitindar Singh flags |= KVM_PPC_MMUV3_RADIX; 303b4db5413SSuraj Jitindar Singh } 304b4db5413SSuraj Jitindar Singh if (gtse) { 305b4db5413SSuraj Jitindar Singh flags |= KVM_PPC_MMUV3_GTSE; 306b4db5413SSuraj Jitindar Singh } 307b4db5413SSuraj Jitindar Singh cfg.flags = flags; 308b4db5413SSuraj Jitindar Singh ret = kvm_vm_ioctl(cs->kvm_state, KVM_PPC_CONFIGURE_V3_MMU, &cfg); 309b4db5413SSuraj Jitindar Singh switch (ret) { 310b4db5413SSuraj Jitindar Singh case 0: 311b4db5413SSuraj Jitindar Singh return H_SUCCESS; 312b4db5413SSuraj Jitindar Singh case -EINVAL: 313b4db5413SSuraj Jitindar Singh return H_PARAMETER; 314b4db5413SSuraj Jitindar Singh case -ENODEV: 315b4db5413SSuraj Jitindar Singh return H_NOT_AVAILABLE; 316b4db5413SSuraj Jitindar Singh default: 317b4db5413SSuraj Jitindar Singh return H_HARDWARE; 318b4db5413SSuraj Jitindar Singh } 319b4db5413SSuraj Jitindar Singh } 320b4db5413SSuraj Jitindar Singh 32124c6863cSDavid Gibson bool kvmppc_hpt_needs_host_contiguous_pages(void) 32224c6863cSDavid Gibson { 32324c6863cSDavid Gibson static struct kvm_ppc_smmu_info smmu_info; 32424c6863cSDavid Gibson 32524c6863cSDavid Gibson if (!kvm_enabled()) { 32624c6863cSDavid Gibson return false; 32724c6863cSDavid Gibson } 32824c6863cSDavid Gibson 329ab256960SGreg Kurz kvm_get_smmu_info(&smmu_info, &error_fatal); 33024c6863cSDavid Gibson return !!(smmu_info.flags & KVM_PPC_PAGE_SIZES_REAL); 33124c6863cSDavid Gibson } 33224c6863cSDavid Gibson 333e5ca28ecSDavid Gibson void kvm_check_mmu(PowerPCCPU *cpu, Error **errp) 3344656e1f0SBenjamin Herrenschmidt { 335e5ca28ecSDavid Gibson struct kvm_ppc_smmu_info smmu_info; 3364656e1f0SBenjamin Herrenschmidt int iq, ik, jq, jk; 33771d0f1eaSGreg Kurz Error *local_err = NULL; 3384656e1f0SBenjamin Herrenschmidt 339e5ca28ecSDavid Gibson /* For now, we only have anything to check on hash64 MMUs */ 340e5ca28ecSDavid Gibson if (!cpu->hash64_opts || !kvm_enabled()) { 3414656e1f0SBenjamin Herrenschmidt return; 3424656e1f0SBenjamin Herrenschmidt } 3434656e1f0SBenjamin Herrenschmidt 344ab256960SGreg Kurz kvm_get_smmu_info(&smmu_info, &local_err); 34571d0f1eaSGreg Kurz if (local_err) { 34671d0f1eaSGreg Kurz error_propagate(errp, local_err); 34771d0f1eaSGreg Kurz return; 34871d0f1eaSGreg Kurz } 349e5ca28ecSDavid Gibson 350e5ca28ecSDavid Gibson if (ppc_hash64_has(cpu, PPC_HASH64_1TSEG) 351e5ca28ecSDavid Gibson && !(smmu_info.flags & KVM_PPC_1T_SEGMENTS)) { 352e5ca28ecSDavid Gibson error_setg(errp, 353e5ca28ecSDavid Gibson "KVM does not support 1TiB segments which guest expects"); 354e5ca28ecSDavid Gibson return; 3554656e1f0SBenjamin Herrenschmidt } 3564656e1f0SBenjamin Herrenschmidt 357e5ca28ecSDavid Gibson if (smmu_info.slb_size < cpu->hash64_opts->slb_size) { 358e5ca28ecSDavid Gibson error_setg(errp, "KVM only supports %u SLB entries, but guest needs %u", 359e5ca28ecSDavid Gibson smmu_info.slb_size, cpu->hash64_opts->slb_size); 360e5ca28ecSDavid Gibson return; 36190da0d5aSBenjamin Herrenschmidt } 36290da0d5aSBenjamin Herrenschmidt 36308215d8fSAlexander Graf /* 364e5ca28ecSDavid Gibson * Verify that every pagesize supported by the cpu model is 365e5ca28ecSDavid Gibson * supported by KVM with the same encodings 36608215d8fSAlexander Graf */ 367e5ca28ecSDavid Gibson for (iq = 0; iq < ARRAY_SIZE(cpu->hash64_opts->sps); iq++) { 368b07c59f7SDavid Gibson PPCHash64SegmentPageSizes *qsps = &cpu->hash64_opts->sps[iq]; 369e5ca28ecSDavid Gibson struct kvm_ppc_one_seg_page_size *ksps; 3704656e1f0SBenjamin Herrenschmidt 371e5ca28ecSDavid Gibson for (ik = 0; ik < ARRAY_SIZE(smmu_info.sps); ik++) { 372e5ca28ecSDavid Gibson if (qsps->page_shift == smmu_info.sps[ik].page_shift) { 3734656e1f0SBenjamin Herrenschmidt break; 3744656e1f0SBenjamin Herrenschmidt } 3754656e1f0SBenjamin Herrenschmidt } 376e5ca28ecSDavid Gibson if (ik >= ARRAY_SIZE(smmu_info.sps)) { 377e5ca28ecSDavid Gibson error_setg(errp, "KVM doesn't support for base page shift %u", 378e5ca28ecSDavid Gibson qsps->page_shift); 379e5ca28ecSDavid Gibson return; 380e5ca28ecSDavid Gibson } 381e5ca28ecSDavid Gibson 382e5ca28ecSDavid Gibson ksps = &smmu_info.sps[ik]; 383e5ca28ecSDavid Gibson if (ksps->slb_enc != qsps->slb_enc) { 384e5ca28ecSDavid Gibson error_setg(errp, 385e5ca28ecSDavid Gibson "KVM uses SLB encoding 0x%x for page shift %u, but guest expects 0x%x", 386e5ca28ecSDavid Gibson ksps->slb_enc, ksps->page_shift, qsps->slb_enc); 387e5ca28ecSDavid Gibson return; 388e5ca28ecSDavid Gibson } 389e5ca28ecSDavid Gibson 390e5ca28ecSDavid Gibson for (jq = 0; jq < ARRAY_SIZE(qsps->enc); jq++) { 391e5ca28ecSDavid Gibson for (jk = 0; jk < ARRAY_SIZE(ksps->enc); jk++) { 392e5ca28ecSDavid Gibson if (qsps->enc[jq].page_shift == ksps->enc[jk].page_shift) { 3934656e1f0SBenjamin Herrenschmidt break; 3944656e1f0SBenjamin Herrenschmidt } 3954656e1f0SBenjamin Herrenschmidt } 3964656e1f0SBenjamin Herrenschmidt 397e5ca28ecSDavid Gibson if (jk >= ARRAY_SIZE(ksps->enc)) { 398e5ca28ecSDavid Gibson error_setg(errp, "KVM doesn't support page shift %u/%u", 399e5ca28ecSDavid Gibson qsps->enc[jq].page_shift, qsps->page_shift); 400e5ca28ecSDavid Gibson return; 401e5ca28ecSDavid Gibson } 402e5ca28ecSDavid Gibson if (qsps->enc[jq].pte_enc != ksps->enc[jk].pte_enc) { 403e5ca28ecSDavid Gibson error_setg(errp, 404e5ca28ecSDavid Gibson "KVM uses PTE encoding 0x%x for page shift %u/%u, but guest expects 0x%x", 405e5ca28ecSDavid Gibson ksps->enc[jk].pte_enc, qsps->enc[jq].page_shift, 406e5ca28ecSDavid Gibson qsps->page_shift, qsps->enc[jq].pte_enc); 407e5ca28ecSDavid Gibson return; 408e5ca28ecSDavid Gibson } 409e5ca28ecSDavid Gibson } 4104656e1f0SBenjamin Herrenschmidt } 4114656e1f0SBenjamin Herrenschmidt 412e5ca28ecSDavid Gibson if (ppc_hash64_has(cpu, PPC_HASH64_CI_LARGEPAGE)) { 413c995e942SDavid Gibson /* 414c995e942SDavid Gibson * Mostly what guest pagesizes we can use are related to the 415e5ca28ecSDavid Gibson * host pages used to map guest RAM, which is handled in the 416e5ca28ecSDavid Gibson * platform code. Cache-Inhibited largepages (64k) however are 417e5ca28ecSDavid Gibson * used for I/O, so if they're mapped to the host at all it 418e5ca28ecSDavid Gibson * will be a normal mapping, not a special hugepage one used 419c995e942SDavid Gibson * for RAM. 420c995e942SDavid Gibson */ 421038adc2fSWei Yang if (qemu_real_host_page_size < 0x10000) { 422e5ca28ecSDavid Gibson error_setg(errp, 423e5ca28ecSDavid Gibson "KVM can't supply 64kiB CI pages, which guest expects"); 424e5ca28ecSDavid Gibson } 425e5ca28ecSDavid Gibson } 426e5ca28ecSDavid Gibson } 4274656e1f0SBenjamin Herrenschmidt #endif /* !defined (TARGET_PPC64) */ 4284656e1f0SBenjamin Herrenschmidt 429b164e48eSEduardo Habkost unsigned long kvm_arch_vcpu_id(CPUState *cpu) 430b164e48eSEduardo Habkost { 4312e886fb3SSam Bobroff return POWERPC_CPU(cpu)->vcpu_id; 432b164e48eSEduardo Habkost } 433b164e48eSEduardo Habkost 434c995e942SDavid Gibson /* 435c995e942SDavid Gibson * e500 supports 2 h/w breakpoint and 2 watchpoint. book3s supports 436c995e942SDavid Gibson * only 1 watchpoint, so array size of 4 is sufficient for now. 43788365d17SBharat Bhushan */ 43888365d17SBharat Bhushan #define MAX_HW_BKPTS 4 43988365d17SBharat Bhushan 44088365d17SBharat Bhushan static struct HWBreakpoint { 44188365d17SBharat Bhushan target_ulong addr; 44288365d17SBharat Bhushan int type; 44388365d17SBharat Bhushan } hw_debug_points[MAX_HW_BKPTS]; 44488365d17SBharat Bhushan 44588365d17SBharat Bhushan static CPUWatchpoint hw_watchpoint; 44688365d17SBharat Bhushan 44788365d17SBharat Bhushan /* Default there is no breakpoint and watchpoint supported */ 44888365d17SBharat Bhushan static int max_hw_breakpoint; 44988365d17SBharat Bhushan static int max_hw_watchpoint; 45088365d17SBharat Bhushan static int nb_hw_breakpoint; 45188365d17SBharat Bhushan static int nb_hw_watchpoint; 45288365d17SBharat Bhushan 45388365d17SBharat Bhushan static void kvmppc_hw_debug_points_init(CPUPPCState *cenv) 45488365d17SBharat Bhushan { 45588365d17SBharat Bhushan if (cenv->excp_model == POWERPC_EXCP_BOOKE) { 45688365d17SBharat Bhushan max_hw_breakpoint = 2; 45788365d17SBharat Bhushan max_hw_watchpoint = 2; 45888365d17SBharat Bhushan } 45988365d17SBharat Bhushan 46088365d17SBharat Bhushan if ((max_hw_breakpoint + max_hw_watchpoint) > MAX_HW_BKPTS) { 46188365d17SBharat Bhushan fprintf(stderr, "Error initializing h/w breakpoints\n"); 46288365d17SBharat Bhushan return; 46388365d17SBharat Bhushan } 46488365d17SBharat Bhushan } 46588365d17SBharat Bhushan 46620d695a9SAndreas Färber int kvm_arch_init_vcpu(CPUState *cs) 4675666ca4aSScott Wood { 46820d695a9SAndreas Färber PowerPCCPU *cpu = POWERPC_CPU(cs); 46920d695a9SAndreas Färber CPUPPCState *cenv = &cpu->env; 4705666ca4aSScott Wood int ret; 4715666ca4aSScott Wood 4724656e1f0SBenjamin Herrenschmidt /* Synchronize sregs with kvm */ 4731bc22652SAndreas Färber ret = kvm_arch_sync_sregs(cpu); 4745666ca4aSScott Wood if (ret) { 475388e47c7SThomas Huth if (ret == -EINVAL) { 476388e47c7SThomas Huth error_report("Register sync failed... If you're using kvm-hv.ko," 477388e47c7SThomas Huth " only \"-cpu host\" is possible"); 478388e47c7SThomas Huth } 4795666ca4aSScott Wood return ret; 4805666ca4aSScott Wood } 481861bbc80SAlexander Graf 48293dd5e85SScott Wood switch (cenv->mmu_model) { 48393dd5e85SScott Wood case POWERPC_MMU_BOOKE206: 4847f516c96SThomas Huth /* This target supports access to KVM's guest TLB */ 4851bc22652SAndreas Färber ret = kvm_booke206_tlb_init(cpu); 48693dd5e85SScott Wood break; 4877f516c96SThomas Huth case POWERPC_MMU_2_07: 4887f516c96SThomas Huth if (!cap_htm && !kvmppc_is_pr(cs->kvm_state)) { 489c995e942SDavid Gibson /* 490c995e942SDavid Gibson * KVM-HV has transactional memory on POWER8 also without 491c995e942SDavid Gibson * the KVM_CAP_PPC_HTM extension, so enable it here 492136fbf65Szhaolichang * instead as long as it's available to userspace on the 493c995e942SDavid Gibson * host. 494c995e942SDavid Gibson */ 495f3d9f303SSam Bobroff if (qemu_getauxval(AT_HWCAP2) & PPC_FEATURE2_HAS_HTM) { 4967f516c96SThomas Huth cap_htm = true; 4977f516c96SThomas Huth } 498f3d9f303SSam Bobroff } 4997f516c96SThomas Huth break; 50093dd5e85SScott Wood default: 50193dd5e85SScott Wood break; 50293dd5e85SScott Wood } 50393dd5e85SScott Wood 5043c902d44SBharat Bhushan kvm_get_one_reg(cs, KVM_REG_PPC_DEBUG_INST, &debug_inst_opcode); 50588365d17SBharat Bhushan kvmppc_hw_debug_points_init(cenv); 5063c902d44SBharat Bhushan 507861bbc80SAlexander Graf return ret; 508d76d1650Saurel32 } 509d76d1650Saurel32 510b1115c99SLiran Alon int kvm_arch_destroy_vcpu(CPUState *cs) 511b1115c99SLiran Alon { 512b1115c99SLiran Alon return 0; 513b1115c99SLiran Alon } 514b1115c99SLiran Alon 5151bc22652SAndreas Färber static void kvm_sw_tlb_put(PowerPCCPU *cpu) 51693dd5e85SScott Wood { 5171bc22652SAndreas Färber CPUPPCState *env = &cpu->env; 5181bc22652SAndreas Färber CPUState *cs = CPU(cpu); 51993dd5e85SScott Wood struct kvm_dirty_tlb dirty_tlb; 52093dd5e85SScott Wood unsigned char *bitmap; 52193dd5e85SScott Wood int ret; 52293dd5e85SScott Wood 52393dd5e85SScott Wood if (!env->kvm_sw_tlb) { 52493dd5e85SScott Wood return; 52593dd5e85SScott Wood } 52693dd5e85SScott Wood 52793dd5e85SScott Wood bitmap = g_malloc((env->nb_tlb + 7) / 8); 52893dd5e85SScott Wood memset(bitmap, 0xFF, (env->nb_tlb + 7) / 8); 52993dd5e85SScott Wood 53093dd5e85SScott Wood dirty_tlb.bitmap = (uintptr_t)bitmap; 53193dd5e85SScott Wood dirty_tlb.num_dirty = env->nb_tlb; 53293dd5e85SScott Wood 5331bc22652SAndreas Färber ret = kvm_vcpu_ioctl(cs, KVM_DIRTY_TLB, &dirty_tlb); 53493dd5e85SScott Wood if (ret) { 53593dd5e85SScott Wood fprintf(stderr, "%s: KVM_DIRTY_TLB: %s\n", 53693dd5e85SScott Wood __func__, strerror(-ret)); 53793dd5e85SScott Wood } 53893dd5e85SScott Wood 53993dd5e85SScott Wood g_free(bitmap); 54093dd5e85SScott Wood } 54193dd5e85SScott Wood 542d67d40eaSDavid Gibson static void kvm_get_one_spr(CPUState *cs, uint64_t id, int spr) 543d67d40eaSDavid Gibson { 544d67d40eaSDavid Gibson PowerPCCPU *cpu = POWERPC_CPU(cs); 545d67d40eaSDavid Gibson CPUPPCState *env = &cpu->env; 546d67d40eaSDavid Gibson union { 547d67d40eaSDavid Gibson uint32_t u32; 548d67d40eaSDavid Gibson uint64_t u64; 549d67d40eaSDavid Gibson } val; 550d67d40eaSDavid Gibson struct kvm_one_reg reg = { 551d67d40eaSDavid Gibson .id = id, 552d67d40eaSDavid Gibson .addr = (uintptr_t) &val, 553d67d40eaSDavid Gibson }; 554d67d40eaSDavid Gibson int ret; 555d67d40eaSDavid Gibson 556d67d40eaSDavid Gibson ret = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, ®); 557d67d40eaSDavid Gibson if (ret != 0) { 558b36f100eSAlexey Kardashevskiy trace_kvm_failed_spr_get(spr, strerror(errno)); 559d67d40eaSDavid Gibson } else { 560d67d40eaSDavid Gibson switch (id & KVM_REG_SIZE_MASK) { 561d67d40eaSDavid Gibson case KVM_REG_SIZE_U32: 562d67d40eaSDavid Gibson env->spr[spr] = val.u32; 563d67d40eaSDavid Gibson break; 564d67d40eaSDavid Gibson 565d67d40eaSDavid Gibson case KVM_REG_SIZE_U64: 566d67d40eaSDavid Gibson env->spr[spr] = val.u64; 567d67d40eaSDavid Gibson break; 568d67d40eaSDavid Gibson 569d67d40eaSDavid Gibson default: 570d67d40eaSDavid Gibson /* Don't handle this size yet */ 571d67d40eaSDavid Gibson abort(); 572d67d40eaSDavid Gibson } 573d67d40eaSDavid Gibson } 574d67d40eaSDavid Gibson } 575d67d40eaSDavid Gibson 576d67d40eaSDavid Gibson static void kvm_put_one_spr(CPUState *cs, uint64_t id, int spr) 577d67d40eaSDavid Gibson { 578d67d40eaSDavid Gibson PowerPCCPU *cpu = POWERPC_CPU(cs); 579d67d40eaSDavid Gibson CPUPPCState *env = &cpu->env; 580d67d40eaSDavid Gibson union { 581d67d40eaSDavid Gibson uint32_t u32; 582d67d40eaSDavid Gibson uint64_t u64; 583d67d40eaSDavid Gibson } val; 584d67d40eaSDavid Gibson struct kvm_one_reg reg = { 585d67d40eaSDavid Gibson .id = id, 586d67d40eaSDavid Gibson .addr = (uintptr_t) &val, 587d67d40eaSDavid Gibson }; 588d67d40eaSDavid Gibson int ret; 589d67d40eaSDavid Gibson 590d67d40eaSDavid Gibson switch (id & KVM_REG_SIZE_MASK) { 591d67d40eaSDavid Gibson case KVM_REG_SIZE_U32: 592d67d40eaSDavid Gibson val.u32 = env->spr[spr]; 593d67d40eaSDavid Gibson break; 594d67d40eaSDavid Gibson 595d67d40eaSDavid Gibson case KVM_REG_SIZE_U64: 596d67d40eaSDavid Gibson val.u64 = env->spr[spr]; 597d67d40eaSDavid Gibson break; 598d67d40eaSDavid Gibson 599d67d40eaSDavid Gibson default: 600d67d40eaSDavid Gibson /* Don't handle this size yet */ 601d67d40eaSDavid Gibson abort(); 602d67d40eaSDavid Gibson } 603d67d40eaSDavid Gibson 604d67d40eaSDavid Gibson ret = kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, ®); 605d67d40eaSDavid Gibson if (ret != 0) { 606b36f100eSAlexey Kardashevskiy trace_kvm_failed_spr_set(spr, strerror(errno)); 607d67d40eaSDavid Gibson } 608d67d40eaSDavid Gibson } 609d67d40eaSDavid Gibson 61070b79849SDavid Gibson static int kvm_put_fp(CPUState *cs) 61170b79849SDavid Gibson { 61270b79849SDavid Gibson PowerPCCPU *cpu = POWERPC_CPU(cs); 61370b79849SDavid Gibson CPUPPCState *env = &cpu->env; 61470b79849SDavid Gibson struct kvm_one_reg reg; 61570b79849SDavid Gibson int i; 61670b79849SDavid Gibson int ret; 61770b79849SDavid Gibson 61870b79849SDavid Gibson if (env->insns_flags & PPC_FLOAT) { 61970b79849SDavid Gibson uint64_t fpscr = env->fpscr; 62070b79849SDavid Gibson bool vsx = !!(env->insns_flags2 & PPC2_VSX); 62170b79849SDavid Gibson 62270b79849SDavid Gibson reg.id = KVM_REG_PPC_FPSCR; 62370b79849SDavid Gibson reg.addr = (uintptr_t)&fpscr; 62470b79849SDavid Gibson ret = kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, ®); 62570b79849SDavid Gibson if (ret < 0) { 6268d83cbf1SGreg Kurz trace_kvm_failed_fpscr_set(strerror(errno)); 62770b79849SDavid Gibson return ret; 62870b79849SDavid Gibson } 62970b79849SDavid Gibson 63070b79849SDavid Gibson for (i = 0; i < 32; i++) { 63170b79849SDavid Gibson uint64_t vsr[2]; 632ef96e3aeSMark Cave-Ayland uint64_t *fpr = cpu_fpr_ptr(&cpu->env, i); 633ef96e3aeSMark Cave-Ayland uint64_t *vsrl = cpu_vsrl_ptr(&cpu->env, i); 63470b79849SDavid Gibson 6353a4b791bSGreg Kurz #ifdef HOST_WORDS_BIGENDIAN 636ef96e3aeSMark Cave-Ayland vsr[0] = float64_val(*fpr); 637ef96e3aeSMark Cave-Ayland vsr[1] = *vsrl; 6383a4b791bSGreg Kurz #else 639ef96e3aeSMark Cave-Ayland vsr[0] = *vsrl; 640ef96e3aeSMark Cave-Ayland vsr[1] = float64_val(*fpr); 6413a4b791bSGreg Kurz #endif 64270b79849SDavid Gibson reg.addr = (uintptr_t) &vsr; 64370b79849SDavid Gibson reg.id = vsx ? KVM_REG_PPC_VSR(i) : KVM_REG_PPC_FPR(i); 64470b79849SDavid Gibson 64570b79849SDavid Gibson ret = kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, ®); 64670b79849SDavid Gibson if (ret < 0) { 6478d83cbf1SGreg Kurz trace_kvm_failed_fp_set(vsx ? "VSR" : "FPR", i, 6488d83cbf1SGreg Kurz strerror(errno)); 64970b79849SDavid Gibson return ret; 65070b79849SDavid Gibson } 65170b79849SDavid Gibson } 65270b79849SDavid Gibson } 65370b79849SDavid Gibson 65470b79849SDavid Gibson if (env->insns_flags & PPC_ALTIVEC) { 65570b79849SDavid Gibson reg.id = KVM_REG_PPC_VSCR; 65670b79849SDavid Gibson reg.addr = (uintptr_t)&env->vscr; 65770b79849SDavid Gibson ret = kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, ®); 65870b79849SDavid Gibson if (ret < 0) { 6598d83cbf1SGreg Kurz trace_kvm_failed_vscr_set(strerror(errno)); 66070b79849SDavid Gibson return ret; 66170b79849SDavid Gibson } 66270b79849SDavid Gibson 66370b79849SDavid Gibson for (i = 0; i < 32; i++) { 66470b79849SDavid Gibson reg.id = KVM_REG_PPC_VR(i); 665ef96e3aeSMark Cave-Ayland reg.addr = (uintptr_t)cpu_avr_ptr(env, i); 66670b79849SDavid Gibson ret = kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, ®); 66770b79849SDavid Gibson if (ret < 0) { 6688d83cbf1SGreg Kurz trace_kvm_failed_vr_set(i, strerror(errno)); 66970b79849SDavid Gibson return ret; 67070b79849SDavid Gibson } 67170b79849SDavid Gibson } 67270b79849SDavid Gibson } 67370b79849SDavid Gibson 67470b79849SDavid Gibson return 0; 67570b79849SDavid Gibson } 67670b79849SDavid Gibson 67770b79849SDavid Gibson static int kvm_get_fp(CPUState *cs) 67870b79849SDavid Gibson { 67970b79849SDavid Gibson PowerPCCPU *cpu = POWERPC_CPU(cs); 68070b79849SDavid Gibson CPUPPCState *env = &cpu->env; 68170b79849SDavid Gibson struct kvm_one_reg reg; 68270b79849SDavid Gibson int i; 68370b79849SDavid Gibson int ret; 68470b79849SDavid Gibson 68570b79849SDavid Gibson if (env->insns_flags & PPC_FLOAT) { 68670b79849SDavid Gibson uint64_t fpscr; 68770b79849SDavid Gibson bool vsx = !!(env->insns_flags2 & PPC2_VSX); 68870b79849SDavid Gibson 68970b79849SDavid Gibson reg.id = KVM_REG_PPC_FPSCR; 69070b79849SDavid Gibson reg.addr = (uintptr_t)&fpscr; 69170b79849SDavid Gibson ret = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, ®); 69270b79849SDavid Gibson if (ret < 0) { 6938d83cbf1SGreg Kurz trace_kvm_failed_fpscr_get(strerror(errno)); 69470b79849SDavid Gibson return ret; 69570b79849SDavid Gibson } else { 69670b79849SDavid Gibson env->fpscr = fpscr; 69770b79849SDavid Gibson } 69870b79849SDavid Gibson 69970b79849SDavid Gibson for (i = 0; i < 32; i++) { 70070b79849SDavid Gibson uint64_t vsr[2]; 701ef96e3aeSMark Cave-Ayland uint64_t *fpr = cpu_fpr_ptr(&cpu->env, i); 702ef96e3aeSMark Cave-Ayland uint64_t *vsrl = cpu_vsrl_ptr(&cpu->env, i); 70370b79849SDavid Gibson 70470b79849SDavid Gibson reg.addr = (uintptr_t) &vsr; 70570b79849SDavid Gibson reg.id = vsx ? KVM_REG_PPC_VSR(i) : KVM_REG_PPC_FPR(i); 70670b79849SDavid Gibson 70770b79849SDavid Gibson ret = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, ®); 70870b79849SDavid Gibson if (ret < 0) { 7098d83cbf1SGreg Kurz trace_kvm_failed_fp_get(vsx ? "VSR" : "FPR", i, 7108d83cbf1SGreg Kurz strerror(errno)); 71170b79849SDavid Gibson return ret; 71270b79849SDavid Gibson } else { 7133a4b791bSGreg Kurz #ifdef HOST_WORDS_BIGENDIAN 714ef96e3aeSMark Cave-Ayland *fpr = vsr[0]; 71570b79849SDavid Gibson if (vsx) { 716ef96e3aeSMark Cave-Ayland *vsrl = vsr[1]; 71770b79849SDavid Gibson } 7183a4b791bSGreg Kurz #else 719ef96e3aeSMark Cave-Ayland *fpr = vsr[1]; 7203a4b791bSGreg Kurz if (vsx) { 721ef96e3aeSMark Cave-Ayland *vsrl = vsr[0]; 7223a4b791bSGreg Kurz } 7233a4b791bSGreg Kurz #endif 72470b79849SDavid Gibson } 72570b79849SDavid Gibson } 72670b79849SDavid Gibson } 72770b79849SDavid Gibson 72870b79849SDavid Gibson if (env->insns_flags & PPC_ALTIVEC) { 72970b79849SDavid Gibson reg.id = KVM_REG_PPC_VSCR; 73070b79849SDavid Gibson reg.addr = (uintptr_t)&env->vscr; 73170b79849SDavid Gibson ret = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, ®); 73270b79849SDavid Gibson if (ret < 0) { 7338d83cbf1SGreg Kurz trace_kvm_failed_vscr_get(strerror(errno)); 73470b79849SDavid Gibson return ret; 73570b79849SDavid Gibson } 73670b79849SDavid Gibson 73770b79849SDavid Gibson for (i = 0; i < 32; i++) { 73870b79849SDavid Gibson reg.id = KVM_REG_PPC_VR(i); 739ef96e3aeSMark Cave-Ayland reg.addr = (uintptr_t)cpu_avr_ptr(env, i); 74070b79849SDavid Gibson ret = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, ®); 74170b79849SDavid Gibson if (ret < 0) { 7428d83cbf1SGreg Kurz trace_kvm_failed_vr_get(i, strerror(errno)); 74370b79849SDavid Gibson return ret; 74470b79849SDavid Gibson } 74570b79849SDavid Gibson } 74670b79849SDavid Gibson } 74770b79849SDavid Gibson 74870b79849SDavid Gibson return 0; 74970b79849SDavid Gibson } 75070b79849SDavid Gibson 7519b00ea49SDavid Gibson #if defined(TARGET_PPC64) 7529b00ea49SDavid Gibson static int kvm_get_vpa(CPUState *cs) 7539b00ea49SDavid Gibson { 7549b00ea49SDavid Gibson PowerPCCPU *cpu = POWERPC_CPU(cs); 755ce2918cbSDavid Gibson SpaprCpuState *spapr_cpu = spapr_cpu_state(cpu); 7569b00ea49SDavid Gibson struct kvm_one_reg reg; 7579b00ea49SDavid Gibson int ret; 7589b00ea49SDavid Gibson 7599b00ea49SDavid Gibson reg.id = KVM_REG_PPC_VPA_ADDR; 7607388efafSDavid Gibson reg.addr = (uintptr_t)&spapr_cpu->vpa_addr; 7619b00ea49SDavid Gibson ret = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, ®); 7629b00ea49SDavid Gibson if (ret < 0) { 7638d83cbf1SGreg Kurz trace_kvm_failed_vpa_addr_get(strerror(errno)); 7649b00ea49SDavid Gibson return ret; 7659b00ea49SDavid Gibson } 7669b00ea49SDavid Gibson 7677388efafSDavid Gibson assert((uintptr_t)&spapr_cpu->slb_shadow_size 7687388efafSDavid Gibson == ((uintptr_t)&spapr_cpu->slb_shadow_addr + 8)); 7699b00ea49SDavid Gibson reg.id = KVM_REG_PPC_VPA_SLB; 7707388efafSDavid Gibson reg.addr = (uintptr_t)&spapr_cpu->slb_shadow_addr; 7719b00ea49SDavid Gibson ret = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, ®); 7729b00ea49SDavid Gibson if (ret < 0) { 7738d83cbf1SGreg Kurz trace_kvm_failed_slb_get(strerror(errno)); 7749b00ea49SDavid Gibson return ret; 7759b00ea49SDavid Gibson } 7769b00ea49SDavid Gibson 7777388efafSDavid Gibson assert((uintptr_t)&spapr_cpu->dtl_size 7787388efafSDavid Gibson == ((uintptr_t)&spapr_cpu->dtl_addr + 8)); 7799b00ea49SDavid Gibson reg.id = KVM_REG_PPC_VPA_DTL; 7807388efafSDavid Gibson reg.addr = (uintptr_t)&spapr_cpu->dtl_addr; 7819b00ea49SDavid Gibson ret = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, ®); 7829b00ea49SDavid Gibson if (ret < 0) { 7838d83cbf1SGreg Kurz trace_kvm_failed_dtl_get(strerror(errno)); 7849b00ea49SDavid Gibson return ret; 7859b00ea49SDavid Gibson } 7869b00ea49SDavid Gibson 7879b00ea49SDavid Gibson return 0; 7889b00ea49SDavid Gibson } 7899b00ea49SDavid Gibson 7909b00ea49SDavid Gibson static int kvm_put_vpa(CPUState *cs) 7919b00ea49SDavid Gibson { 7929b00ea49SDavid Gibson PowerPCCPU *cpu = POWERPC_CPU(cs); 793ce2918cbSDavid Gibson SpaprCpuState *spapr_cpu = spapr_cpu_state(cpu); 7949b00ea49SDavid Gibson struct kvm_one_reg reg; 7959b00ea49SDavid Gibson int ret; 7969b00ea49SDavid Gibson 797c995e942SDavid Gibson /* 798c995e942SDavid Gibson * SLB shadow or DTL can't be registered unless a master VPA is 7999b00ea49SDavid Gibson * registered. That means when restoring state, if a VPA *is* 8009b00ea49SDavid Gibson * registered, we need to set that up first. If not, we need to 801c995e942SDavid Gibson * deregister the others before deregistering the master VPA 802c995e942SDavid Gibson */ 8037388efafSDavid Gibson assert(spapr_cpu->vpa_addr 8047388efafSDavid Gibson || !(spapr_cpu->slb_shadow_addr || spapr_cpu->dtl_addr)); 8059b00ea49SDavid Gibson 8067388efafSDavid Gibson if (spapr_cpu->vpa_addr) { 8079b00ea49SDavid Gibson reg.id = KVM_REG_PPC_VPA_ADDR; 8087388efafSDavid Gibson reg.addr = (uintptr_t)&spapr_cpu->vpa_addr; 8099b00ea49SDavid Gibson ret = kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, ®); 8109b00ea49SDavid Gibson if (ret < 0) { 8118d83cbf1SGreg Kurz trace_kvm_failed_vpa_addr_set(strerror(errno)); 8129b00ea49SDavid Gibson return ret; 8139b00ea49SDavid Gibson } 8149b00ea49SDavid Gibson } 8159b00ea49SDavid Gibson 8167388efafSDavid Gibson assert((uintptr_t)&spapr_cpu->slb_shadow_size 8177388efafSDavid Gibson == ((uintptr_t)&spapr_cpu->slb_shadow_addr + 8)); 8189b00ea49SDavid Gibson reg.id = KVM_REG_PPC_VPA_SLB; 8197388efafSDavid Gibson reg.addr = (uintptr_t)&spapr_cpu->slb_shadow_addr; 8209b00ea49SDavid Gibson ret = kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, ®); 8219b00ea49SDavid Gibson if (ret < 0) { 8228d83cbf1SGreg Kurz trace_kvm_failed_slb_set(strerror(errno)); 8239b00ea49SDavid Gibson return ret; 8249b00ea49SDavid Gibson } 8259b00ea49SDavid Gibson 8267388efafSDavid Gibson assert((uintptr_t)&spapr_cpu->dtl_size 8277388efafSDavid Gibson == ((uintptr_t)&spapr_cpu->dtl_addr + 8)); 8289b00ea49SDavid Gibson reg.id = KVM_REG_PPC_VPA_DTL; 8297388efafSDavid Gibson reg.addr = (uintptr_t)&spapr_cpu->dtl_addr; 8309b00ea49SDavid Gibson ret = kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, ®); 8319b00ea49SDavid Gibson if (ret < 0) { 8328d83cbf1SGreg Kurz trace_kvm_failed_dtl_set(strerror(errno)); 8339b00ea49SDavid Gibson return ret; 8349b00ea49SDavid Gibson } 8359b00ea49SDavid Gibson 8367388efafSDavid Gibson if (!spapr_cpu->vpa_addr) { 8379b00ea49SDavid Gibson reg.id = KVM_REG_PPC_VPA_ADDR; 8387388efafSDavid Gibson reg.addr = (uintptr_t)&spapr_cpu->vpa_addr; 8399b00ea49SDavid Gibson ret = kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, ®); 8409b00ea49SDavid Gibson if (ret < 0) { 8418d83cbf1SGreg Kurz trace_kvm_failed_null_vpa_addr_set(strerror(errno)); 8429b00ea49SDavid Gibson return ret; 8439b00ea49SDavid Gibson } 8449b00ea49SDavid Gibson } 8459b00ea49SDavid Gibson 8469b00ea49SDavid Gibson return 0; 8479b00ea49SDavid Gibson } 8489b00ea49SDavid Gibson #endif /* TARGET_PPC64 */ 8499b00ea49SDavid Gibson 850e5c0d3ceSDavid Gibson int kvmppc_put_books_sregs(PowerPCCPU *cpu) 851a7a00a72SDavid Gibson { 852a7a00a72SDavid Gibson CPUPPCState *env = &cpu->env; 853a7a00a72SDavid Gibson struct kvm_sregs sregs; 854a7a00a72SDavid Gibson int i; 855a7a00a72SDavid Gibson 856a7a00a72SDavid Gibson sregs.pvr = env->spr[SPR_PVR]; 857a7a00a72SDavid Gibson 8581ec26c75SGreg Kurz if (cpu->vhyp) { 8591ec26c75SGreg Kurz PPCVirtualHypervisorClass *vhc = 8601ec26c75SGreg Kurz PPC_VIRTUAL_HYPERVISOR_GET_CLASS(cpu->vhyp); 8611ec26c75SGreg Kurz sregs.u.s.sdr1 = vhc->encode_hpt_for_kvm_pr(cpu->vhyp); 8621ec26c75SGreg Kurz } else { 863a7a00a72SDavid Gibson sregs.u.s.sdr1 = env->spr[SPR_SDR1]; 8641ec26c75SGreg Kurz } 865a7a00a72SDavid Gibson 866a7a00a72SDavid Gibson /* Sync SLB */ 867a7a00a72SDavid Gibson #ifdef TARGET_PPC64 868a7a00a72SDavid Gibson for (i = 0; i < ARRAY_SIZE(env->slb); i++) { 869a7a00a72SDavid Gibson sregs.u.s.ppc64.slb[i].slbe = env->slb[i].esid; 870a7a00a72SDavid Gibson if (env->slb[i].esid & SLB_ESID_V) { 871a7a00a72SDavid Gibson sregs.u.s.ppc64.slb[i].slbe |= i; 872a7a00a72SDavid Gibson } 873a7a00a72SDavid Gibson sregs.u.s.ppc64.slb[i].slbv = env->slb[i].vsid; 874a7a00a72SDavid Gibson } 875a7a00a72SDavid Gibson #endif 876a7a00a72SDavid Gibson 877a7a00a72SDavid Gibson /* Sync SRs */ 878a7a00a72SDavid Gibson for (i = 0; i < 16; i++) { 879a7a00a72SDavid Gibson sregs.u.s.ppc32.sr[i] = env->sr[i]; 880a7a00a72SDavid Gibson } 881a7a00a72SDavid Gibson 882a7a00a72SDavid Gibson /* Sync BATs */ 883a7a00a72SDavid Gibson for (i = 0; i < 8; i++) { 884a7a00a72SDavid Gibson /* Beware. We have to swap upper and lower bits here */ 885a7a00a72SDavid Gibson sregs.u.s.ppc32.dbat[i] = ((uint64_t)env->DBAT[0][i] << 32) 886a7a00a72SDavid Gibson | env->DBAT[1][i]; 887a7a00a72SDavid Gibson sregs.u.s.ppc32.ibat[i] = ((uint64_t)env->IBAT[0][i] << 32) 888a7a00a72SDavid Gibson | env->IBAT[1][i]; 889a7a00a72SDavid Gibson } 890a7a00a72SDavid Gibson 891a7a00a72SDavid Gibson return kvm_vcpu_ioctl(CPU(cpu), KVM_SET_SREGS, &sregs); 892a7a00a72SDavid Gibson } 893a7a00a72SDavid Gibson 89420d695a9SAndreas Färber int kvm_arch_put_registers(CPUState *cs, int level) 895d76d1650Saurel32 { 89620d695a9SAndreas Färber PowerPCCPU *cpu = POWERPC_CPU(cs); 89720d695a9SAndreas Färber CPUPPCState *env = &cpu->env; 898d76d1650Saurel32 struct kvm_regs regs; 899d76d1650Saurel32 int ret; 900d76d1650Saurel32 int i; 901d76d1650Saurel32 9021bc22652SAndreas Färber ret = kvm_vcpu_ioctl(cs, KVM_GET_REGS, ®s); 9031bc22652SAndreas Färber if (ret < 0) { 904d76d1650Saurel32 return ret; 9051bc22652SAndreas Färber } 906d76d1650Saurel32 907d76d1650Saurel32 regs.ctr = env->ctr; 908d76d1650Saurel32 regs.lr = env->lr; 909da91a00fSRichard Henderson regs.xer = cpu_read_xer(env); 910d76d1650Saurel32 regs.msr = env->msr; 911d76d1650Saurel32 regs.pc = env->nip; 912d76d1650Saurel32 913d76d1650Saurel32 regs.srr0 = env->spr[SPR_SRR0]; 914d76d1650Saurel32 regs.srr1 = env->spr[SPR_SRR1]; 915d76d1650Saurel32 916d76d1650Saurel32 regs.sprg0 = env->spr[SPR_SPRG0]; 917d76d1650Saurel32 regs.sprg1 = env->spr[SPR_SPRG1]; 918d76d1650Saurel32 regs.sprg2 = env->spr[SPR_SPRG2]; 919d76d1650Saurel32 regs.sprg3 = env->spr[SPR_SPRG3]; 920d76d1650Saurel32 regs.sprg4 = env->spr[SPR_SPRG4]; 921d76d1650Saurel32 regs.sprg5 = env->spr[SPR_SPRG5]; 922d76d1650Saurel32 regs.sprg6 = env->spr[SPR_SPRG6]; 923d76d1650Saurel32 regs.sprg7 = env->spr[SPR_SPRG7]; 924d76d1650Saurel32 92590dc8812SScott Wood regs.pid = env->spr[SPR_BOOKE_PID]; 92690dc8812SScott Wood 927c995e942SDavid Gibson for (i = 0; i < 32; i++) { 928d76d1650Saurel32 regs.gpr[i] = env->gpr[i]; 929c995e942SDavid Gibson } 930d76d1650Saurel32 9314bddaf55SAlexey Kardashevskiy regs.cr = 0; 9324bddaf55SAlexey Kardashevskiy for (i = 0; i < 8; i++) { 9334bddaf55SAlexey Kardashevskiy regs.cr |= (env->crf[i] & 15) << (4 * (7 - i)); 9344bddaf55SAlexey Kardashevskiy } 9354bddaf55SAlexey Kardashevskiy 9361bc22652SAndreas Färber ret = kvm_vcpu_ioctl(cs, KVM_SET_REGS, ®s); 937c995e942SDavid Gibson if (ret < 0) { 938d76d1650Saurel32 return ret; 939c995e942SDavid Gibson } 940d76d1650Saurel32 94170b79849SDavid Gibson kvm_put_fp(cs); 94270b79849SDavid Gibson 94393dd5e85SScott Wood if (env->tlb_dirty) { 9441bc22652SAndreas Färber kvm_sw_tlb_put(cpu); 94593dd5e85SScott Wood env->tlb_dirty = false; 94693dd5e85SScott Wood } 94793dd5e85SScott Wood 948f1af19d7SDavid Gibson if (cap_segstate && (level >= KVM_PUT_RESET_STATE)) { 949a7a00a72SDavid Gibson ret = kvmppc_put_books_sregs(cpu); 950a7a00a72SDavid Gibson if (ret < 0) { 951f1af19d7SDavid Gibson return ret; 952f1af19d7SDavid Gibson } 953f1af19d7SDavid Gibson } 954f1af19d7SDavid Gibson 955f1af19d7SDavid Gibson if (cap_hior && (level >= KVM_PUT_RESET_STATE)) { 956d67d40eaSDavid Gibson kvm_put_one_spr(cs, KVM_REG_PPC_HIOR, SPR_HIOR); 957d67d40eaSDavid Gibson } 958f1af19d7SDavid Gibson 959d67d40eaSDavid Gibson if (cap_one_reg) { 960d67d40eaSDavid Gibson int i; 961d67d40eaSDavid Gibson 962c995e942SDavid Gibson /* 963c995e942SDavid Gibson * We deliberately ignore errors here, for kernels which have 964d67d40eaSDavid Gibson * the ONE_REG calls, but don't support the specific 965d67d40eaSDavid Gibson * registers, there's a reasonable chance things will still 966c995e942SDavid Gibson * work, at least until we try to migrate. 967c995e942SDavid Gibson */ 968d67d40eaSDavid Gibson for (i = 0; i < 1024; i++) { 969d67d40eaSDavid Gibson uint64_t id = env->spr_cb[i].one_reg_id; 970d67d40eaSDavid Gibson 971d67d40eaSDavid Gibson if (id != 0) { 972d67d40eaSDavid Gibson kvm_put_one_spr(cs, id, i); 973d67d40eaSDavid Gibson } 974f1af19d7SDavid Gibson } 9759b00ea49SDavid Gibson 9769b00ea49SDavid Gibson #ifdef TARGET_PPC64 97780b3f79bSAlexey Kardashevskiy if (msr_ts) { 97880b3f79bSAlexey Kardashevskiy for (i = 0; i < ARRAY_SIZE(env->tm_gpr); i++) { 97980b3f79bSAlexey Kardashevskiy kvm_set_one_reg(cs, KVM_REG_PPC_TM_GPR(i), &env->tm_gpr[i]); 98080b3f79bSAlexey Kardashevskiy } 98180b3f79bSAlexey Kardashevskiy for (i = 0; i < ARRAY_SIZE(env->tm_vsr); i++) { 98280b3f79bSAlexey Kardashevskiy kvm_set_one_reg(cs, KVM_REG_PPC_TM_VSR(i), &env->tm_vsr[i]); 98380b3f79bSAlexey Kardashevskiy } 98480b3f79bSAlexey Kardashevskiy kvm_set_one_reg(cs, KVM_REG_PPC_TM_CR, &env->tm_cr); 98580b3f79bSAlexey Kardashevskiy kvm_set_one_reg(cs, KVM_REG_PPC_TM_LR, &env->tm_lr); 98680b3f79bSAlexey Kardashevskiy kvm_set_one_reg(cs, KVM_REG_PPC_TM_CTR, &env->tm_ctr); 98780b3f79bSAlexey Kardashevskiy kvm_set_one_reg(cs, KVM_REG_PPC_TM_FPSCR, &env->tm_fpscr); 98880b3f79bSAlexey Kardashevskiy kvm_set_one_reg(cs, KVM_REG_PPC_TM_AMR, &env->tm_amr); 98980b3f79bSAlexey Kardashevskiy kvm_set_one_reg(cs, KVM_REG_PPC_TM_PPR, &env->tm_ppr); 99080b3f79bSAlexey Kardashevskiy kvm_set_one_reg(cs, KVM_REG_PPC_TM_VRSAVE, &env->tm_vrsave); 99180b3f79bSAlexey Kardashevskiy kvm_set_one_reg(cs, KVM_REG_PPC_TM_VSCR, &env->tm_vscr); 99280b3f79bSAlexey Kardashevskiy kvm_set_one_reg(cs, KVM_REG_PPC_TM_DSCR, &env->tm_dscr); 99380b3f79bSAlexey Kardashevskiy kvm_set_one_reg(cs, KVM_REG_PPC_TM_TAR, &env->tm_tar); 99480b3f79bSAlexey Kardashevskiy } 99580b3f79bSAlexey Kardashevskiy 9969b00ea49SDavid Gibson if (cap_papr) { 9979b00ea49SDavid Gibson if (kvm_put_vpa(cs) < 0) { 9988d83cbf1SGreg Kurz trace_kvm_failed_put_vpa(); 9999b00ea49SDavid Gibson } 10009b00ea49SDavid Gibson } 100198a8b524SAlexey Kardashevskiy 100298a8b524SAlexey Kardashevskiy kvm_set_one_reg(cs, KVM_REG_PPC_TB_OFFSET, &env->tb_env->tb_offset); 1003972bd576SAlexey Kardashevskiy 1004972bd576SAlexey Kardashevskiy if (level > KVM_PUT_RUNTIME_STATE) { 1005972bd576SAlexey Kardashevskiy kvm_put_one_spr(cs, KVM_REG_PPC_DPDES, SPR_DPDES); 1006972bd576SAlexey Kardashevskiy } 10079b00ea49SDavid Gibson #endif /* TARGET_PPC64 */ 1008f1af19d7SDavid Gibson } 1009f1af19d7SDavid Gibson 1010d76d1650Saurel32 return ret; 1011d76d1650Saurel32 } 1012d76d1650Saurel32 1013c371c2e3SBharat Bhushan static void kvm_sync_excp(CPUPPCState *env, int vector, int ivor) 1014c371c2e3SBharat Bhushan { 1015c371c2e3SBharat Bhushan env->excp_vectors[vector] = env->spr[ivor] + env->spr[SPR_BOOKE_IVPR]; 1016c371c2e3SBharat Bhushan } 1017c371c2e3SBharat Bhushan 1018a7a00a72SDavid Gibson static int kvmppc_get_booke_sregs(PowerPCCPU *cpu) 1019d76d1650Saurel32 { 102020d695a9SAndreas Färber CPUPPCState *env = &cpu->env; 1021ba5e5090SAlexander Graf struct kvm_sregs sregs; 1022a7a00a72SDavid Gibson int ret; 1023d76d1650Saurel32 1024a7a00a72SDavid Gibson ret = kvm_vcpu_ioctl(CPU(cpu), KVM_GET_SREGS, &sregs); 102590dc8812SScott Wood if (ret < 0) { 102690dc8812SScott Wood return ret; 102790dc8812SScott Wood } 102890dc8812SScott Wood 102990dc8812SScott Wood if (sregs.u.e.features & KVM_SREGS_E_BASE) { 103090dc8812SScott Wood env->spr[SPR_BOOKE_CSRR0] = sregs.u.e.csrr0; 103190dc8812SScott Wood env->spr[SPR_BOOKE_CSRR1] = sregs.u.e.csrr1; 103290dc8812SScott Wood env->spr[SPR_BOOKE_ESR] = sregs.u.e.esr; 103390dc8812SScott Wood env->spr[SPR_BOOKE_DEAR] = sregs.u.e.dear; 103490dc8812SScott Wood env->spr[SPR_BOOKE_MCSR] = sregs.u.e.mcsr; 103590dc8812SScott Wood env->spr[SPR_BOOKE_TSR] = sregs.u.e.tsr; 103690dc8812SScott Wood env->spr[SPR_BOOKE_TCR] = sregs.u.e.tcr; 103790dc8812SScott Wood env->spr[SPR_DECR] = sregs.u.e.dec; 103890dc8812SScott Wood env->spr[SPR_TBL] = sregs.u.e.tb & 0xffffffff; 103990dc8812SScott Wood env->spr[SPR_TBU] = sregs.u.e.tb >> 32; 104090dc8812SScott Wood env->spr[SPR_VRSAVE] = sregs.u.e.vrsave; 104190dc8812SScott Wood } 104290dc8812SScott Wood 104390dc8812SScott Wood if (sregs.u.e.features & KVM_SREGS_E_ARCH206) { 104490dc8812SScott Wood env->spr[SPR_BOOKE_PIR] = sregs.u.e.pir; 104590dc8812SScott Wood env->spr[SPR_BOOKE_MCSRR0] = sregs.u.e.mcsrr0; 104690dc8812SScott Wood env->spr[SPR_BOOKE_MCSRR1] = sregs.u.e.mcsrr1; 104790dc8812SScott Wood env->spr[SPR_BOOKE_DECAR] = sregs.u.e.decar; 104890dc8812SScott Wood env->spr[SPR_BOOKE_IVPR] = sregs.u.e.ivpr; 104990dc8812SScott Wood } 105090dc8812SScott Wood 105190dc8812SScott Wood if (sregs.u.e.features & KVM_SREGS_E_64) { 105290dc8812SScott Wood env->spr[SPR_BOOKE_EPCR] = sregs.u.e.epcr; 105390dc8812SScott Wood } 105490dc8812SScott Wood 105590dc8812SScott Wood if (sregs.u.e.features & KVM_SREGS_E_SPRG8) { 105690dc8812SScott Wood env->spr[SPR_BOOKE_SPRG8] = sregs.u.e.sprg8; 105790dc8812SScott Wood } 105890dc8812SScott Wood 105990dc8812SScott Wood if (sregs.u.e.features & KVM_SREGS_E_IVOR) { 106090dc8812SScott Wood env->spr[SPR_BOOKE_IVOR0] = sregs.u.e.ivor_low[0]; 1061c371c2e3SBharat Bhushan kvm_sync_excp(env, POWERPC_EXCP_CRITICAL, SPR_BOOKE_IVOR0); 106290dc8812SScott Wood env->spr[SPR_BOOKE_IVOR1] = sregs.u.e.ivor_low[1]; 1063c371c2e3SBharat Bhushan kvm_sync_excp(env, POWERPC_EXCP_MCHECK, SPR_BOOKE_IVOR1); 106490dc8812SScott Wood env->spr[SPR_BOOKE_IVOR2] = sregs.u.e.ivor_low[2]; 1065c371c2e3SBharat Bhushan kvm_sync_excp(env, POWERPC_EXCP_DSI, SPR_BOOKE_IVOR2); 106690dc8812SScott Wood env->spr[SPR_BOOKE_IVOR3] = sregs.u.e.ivor_low[3]; 1067c371c2e3SBharat Bhushan kvm_sync_excp(env, POWERPC_EXCP_ISI, SPR_BOOKE_IVOR3); 106890dc8812SScott Wood env->spr[SPR_BOOKE_IVOR4] = sregs.u.e.ivor_low[4]; 1069c371c2e3SBharat Bhushan kvm_sync_excp(env, POWERPC_EXCP_EXTERNAL, SPR_BOOKE_IVOR4); 107090dc8812SScott Wood env->spr[SPR_BOOKE_IVOR5] = sregs.u.e.ivor_low[5]; 1071c371c2e3SBharat Bhushan kvm_sync_excp(env, POWERPC_EXCP_ALIGN, SPR_BOOKE_IVOR5); 107290dc8812SScott Wood env->spr[SPR_BOOKE_IVOR6] = sregs.u.e.ivor_low[6]; 1073c371c2e3SBharat Bhushan kvm_sync_excp(env, POWERPC_EXCP_PROGRAM, SPR_BOOKE_IVOR6); 107490dc8812SScott Wood env->spr[SPR_BOOKE_IVOR7] = sregs.u.e.ivor_low[7]; 1075c371c2e3SBharat Bhushan kvm_sync_excp(env, POWERPC_EXCP_FPU, SPR_BOOKE_IVOR7); 107690dc8812SScott Wood env->spr[SPR_BOOKE_IVOR8] = sregs.u.e.ivor_low[8]; 1077c371c2e3SBharat Bhushan kvm_sync_excp(env, POWERPC_EXCP_SYSCALL, SPR_BOOKE_IVOR8); 107890dc8812SScott Wood env->spr[SPR_BOOKE_IVOR9] = sregs.u.e.ivor_low[9]; 1079c371c2e3SBharat Bhushan kvm_sync_excp(env, POWERPC_EXCP_APU, SPR_BOOKE_IVOR9); 108090dc8812SScott Wood env->spr[SPR_BOOKE_IVOR10] = sregs.u.e.ivor_low[10]; 1081c371c2e3SBharat Bhushan kvm_sync_excp(env, POWERPC_EXCP_DECR, SPR_BOOKE_IVOR10); 108290dc8812SScott Wood env->spr[SPR_BOOKE_IVOR11] = sregs.u.e.ivor_low[11]; 1083c371c2e3SBharat Bhushan kvm_sync_excp(env, POWERPC_EXCP_FIT, SPR_BOOKE_IVOR11); 108490dc8812SScott Wood env->spr[SPR_BOOKE_IVOR12] = sregs.u.e.ivor_low[12]; 1085c371c2e3SBharat Bhushan kvm_sync_excp(env, POWERPC_EXCP_WDT, SPR_BOOKE_IVOR12); 108690dc8812SScott Wood env->spr[SPR_BOOKE_IVOR13] = sregs.u.e.ivor_low[13]; 1087c371c2e3SBharat Bhushan kvm_sync_excp(env, POWERPC_EXCP_DTLB, SPR_BOOKE_IVOR13); 108890dc8812SScott Wood env->spr[SPR_BOOKE_IVOR14] = sregs.u.e.ivor_low[14]; 1089c371c2e3SBharat Bhushan kvm_sync_excp(env, POWERPC_EXCP_ITLB, SPR_BOOKE_IVOR14); 109090dc8812SScott Wood env->spr[SPR_BOOKE_IVOR15] = sregs.u.e.ivor_low[15]; 1091c371c2e3SBharat Bhushan kvm_sync_excp(env, POWERPC_EXCP_DEBUG, SPR_BOOKE_IVOR15); 109290dc8812SScott Wood 109390dc8812SScott Wood if (sregs.u.e.features & KVM_SREGS_E_SPE) { 109490dc8812SScott Wood env->spr[SPR_BOOKE_IVOR32] = sregs.u.e.ivor_high[0]; 1095c371c2e3SBharat Bhushan kvm_sync_excp(env, POWERPC_EXCP_SPEU, SPR_BOOKE_IVOR32); 109690dc8812SScott Wood env->spr[SPR_BOOKE_IVOR33] = sregs.u.e.ivor_high[1]; 1097c371c2e3SBharat Bhushan kvm_sync_excp(env, POWERPC_EXCP_EFPDI, SPR_BOOKE_IVOR33); 109890dc8812SScott Wood env->spr[SPR_BOOKE_IVOR34] = sregs.u.e.ivor_high[2]; 1099c371c2e3SBharat Bhushan kvm_sync_excp(env, POWERPC_EXCP_EFPRI, SPR_BOOKE_IVOR34); 110090dc8812SScott Wood } 110190dc8812SScott Wood 110290dc8812SScott Wood if (sregs.u.e.features & KVM_SREGS_E_PM) { 110390dc8812SScott Wood env->spr[SPR_BOOKE_IVOR35] = sregs.u.e.ivor_high[3]; 1104c371c2e3SBharat Bhushan kvm_sync_excp(env, POWERPC_EXCP_EPERFM, SPR_BOOKE_IVOR35); 110590dc8812SScott Wood } 110690dc8812SScott Wood 110790dc8812SScott Wood if (sregs.u.e.features & KVM_SREGS_E_PC) { 110890dc8812SScott Wood env->spr[SPR_BOOKE_IVOR36] = sregs.u.e.ivor_high[4]; 1109c371c2e3SBharat Bhushan kvm_sync_excp(env, POWERPC_EXCP_DOORI, SPR_BOOKE_IVOR36); 111090dc8812SScott Wood env->spr[SPR_BOOKE_IVOR37] = sregs.u.e.ivor_high[5]; 1111c371c2e3SBharat Bhushan kvm_sync_excp(env, POWERPC_EXCP_DOORCI, SPR_BOOKE_IVOR37); 111290dc8812SScott Wood } 111390dc8812SScott Wood } 111490dc8812SScott Wood 111590dc8812SScott Wood if (sregs.u.e.features & KVM_SREGS_E_ARCH206_MMU) { 111690dc8812SScott Wood env->spr[SPR_BOOKE_MAS0] = sregs.u.e.mas0; 111790dc8812SScott Wood env->spr[SPR_BOOKE_MAS1] = sregs.u.e.mas1; 111890dc8812SScott Wood env->spr[SPR_BOOKE_MAS2] = sregs.u.e.mas2; 111990dc8812SScott Wood env->spr[SPR_BOOKE_MAS3] = sregs.u.e.mas7_3 & 0xffffffff; 112090dc8812SScott Wood env->spr[SPR_BOOKE_MAS4] = sregs.u.e.mas4; 112190dc8812SScott Wood env->spr[SPR_BOOKE_MAS6] = sregs.u.e.mas6; 112290dc8812SScott Wood env->spr[SPR_BOOKE_MAS7] = sregs.u.e.mas7_3 >> 32; 112390dc8812SScott Wood env->spr[SPR_MMUCFG] = sregs.u.e.mmucfg; 112490dc8812SScott Wood env->spr[SPR_BOOKE_TLB0CFG] = sregs.u.e.tlbcfg[0]; 112590dc8812SScott Wood env->spr[SPR_BOOKE_TLB1CFG] = sregs.u.e.tlbcfg[1]; 112690dc8812SScott Wood } 112790dc8812SScott Wood 112890dc8812SScott Wood if (sregs.u.e.features & KVM_SREGS_EXP) { 112990dc8812SScott Wood env->spr[SPR_BOOKE_EPR] = sregs.u.e.epr; 113090dc8812SScott Wood } 113190dc8812SScott Wood 113290dc8812SScott Wood if (sregs.u.e.features & KVM_SREGS_E_PD) { 113390dc8812SScott Wood env->spr[SPR_BOOKE_EPLC] = sregs.u.e.eplc; 113490dc8812SScott Wood env->spr[SPR_BOOKE_EPSC] = sregs.u.e.epsc; 113590dc8812SScott Wood } 113690dc8812SScott Wood 113790dc8812SScott Wood if (sregs.u.e.impl_id == KVM_SREGS_E_IMPL_FSL) { 113890dc8812SScott Wood env->spr[SPR_E500_SVR] = sregs.u.e.impl.fsl.svr; 113990dc8812SScott Wood env->spr[SPR_Exxx_MCAR] = sregs.u.e.impl.fsl.mcar; 114090dc8812SScott Wood env->spr[SPR_HID0] = sregs.u.e.impl.fsl.hid0; 114190dc8812SScott Wood 114290dc8812SScott Wood if (sregs.u.e.impl.fsl.features & KVM_SREGS_E_FSL_PIDn) { 114390dc8812SScott Wood env->spr[SPR_BOOKE_PID1] = sregs.u.e.impl.fsl.pid1; 114490dc8812SScott Wood env->spr[SPR_BOOKE_PID2] = sregs.u.e.impl.fsl.pid2; 114590dc8812SScott Wood } 114690dc8812SScott Wood } 1147a7a00a72SDavid Gibson 1148a7a00a72SDavid Gibson return 0; 1149fafc0b6aSAlexander Graf } 115090dc8812SScott Wood 1151a7a00a72SDavid Gibson static int kvmppc_get_books_sregs(PowerPCCPU *cpu) 1152a7a00a72SDavid Gibson { 1153a7a00a72SDavid Gibson CPUPPCState *env = &cpu->env; 1154a7a00a72SDavid Gibson struct kvm_sregs sregs; 1155a7a00a72SDavid Gibson int ret; 1156a7a00a72SDavid Gibson int i; 1157a7a00a72SDavid Gibson 1158a7a00a72SDavid Gibson ret = kvm_vcpu_ioctl(CPU(cpu), KVM_GET_SREGS, &sregs); 115990dc8812SScott Wood if (ret < 0) { 116090dc8812SScott Wood return ret; 116190dc8812SScott Wood } 116290dc8812SScott Wood 1163e57ca75cSDavid Gibson if (!cpu->vhyp) { 1164bb593904SDavid Gibson ppc_store_sdr1(env, sregs.u.s.sdr1); 1165f3c75d42SAneesh Kumar K.V } 1166ba5e5090SAlexander Graf 1167ba5e5090SAlexander Graf /* Sync SLB */ 116882c09f2fSAlexander Graf #ifdef TARGET_PPC64 11694b4d4a21SAneesh Kumar K.V /* 11704b4d4a21SAneesh Kumar K.V * The packed SLB array we get from KVM_GET_SREGS only contains 1171a7a00a72SDavid Gibson * information about valid entries. So we flush our internal copy 1172a7a00a72SDavid Gibson * to get rid of stale ones, then put all valid SLB entries back 1173a7a00a72SDavid Gibson * in. 11744b4d4a21SAneesh Kumar K.V */ 11754b4d4a21SAneesh Kumar K.V memset(env->slb, 0, sizeof(env->slb)); 1176d83af167SAneesh Kumar K.V for (i = 0; i < ARRAY_SIZE(env->slb); i++) { 11774b4d4a21SAneesh Kumar K.V target_ulong rb = sregs.u.s.ppc64.slb[i].slbe; 11784b4d4a21SAneesh Kumar K.V target_ulong rs = sregs.u.s.ppc64.slb[i].slbv; 11794b4d4a21SAneesh Kumar K.V /* 11804b4d4a21SAneesh Kumar K.V * Only restore valid entries 11814b4d4a21SAneesh Kumar K.V */ 11824b4d4a21SAneesh Kumar K.V if (rb & SLB_ESID_V) { 1183bcd81230SDavid Gibson ppc_store_slb(cpu, rb & 0xfff, rb & ~0xfffULL, rs); 11844b4d4a21SAneesh Kumar K.V } 1185ba5e5090SAlexander Graf } 118682c09f2fSAlexander Graf #endif 1187ba5e5090SAlexander Graf 1188ba5e5090SAlexander Graf /* Sync SRs */ 1189ba5e5090SAlexander Graf for (i = 0; i < 16; i++) { 1190ba5e5090SAlexander Graf env->sr[i] = sregs.u.s.ppc32.sr[i]; 1191ba5e5090SAlexander Graf } 1192ba5e5090SAlexander Graf 1193ba5e5090SAlexander Graf /* Sync BATs */ 1194ba5e5090SAlexander Graf for (i = 0; i < 8; i++) { 1195ba5e5090SAlexander Graf env->DBAT[0][i] = sregs.u.s.ppc32.dbat[i] & 0xffffffff; 1196ba5e5090SAlexander Graf env->DBAT[1][i] = sregs.u.s.ppc32.dbat[i] >> 32; 1197ba5e5090SAlexander Graf env->IBAT[0][i] = sregs.u.s.ppc32.ibat[i] & 0xffffffff; 1198ba5e5090SAlexander Graf env->IBAT[1][i] = sregs.u.s.ppc32.ibat[i] >> 32; 1199ba5e5090SAlexander Graf } 1200a7a00a72SDavid Gibson 1201a7a00a72SDavid Gibson return 0; 1202a7a00a72SDavid Gibson } 1203a7a00a72SDavid Gibson 1204a7a00a72SDavid Gibson int kvm_arch_get_registers(CPUState *cs) 1205a7a00a72SDavid Gibson { 1206a7a00a72SDavid Gibson PowerPCCPU *cpu = POWERPC_CPU(cs); 1207a7a00a72SDavid Gibson CPUPPCState *env = &cpu->env; 1208a7a00a72SDavid Gibson struct kvm_regs regs; 1209a7a00a72SDavid Gibson uint32_t cr; 1210a7a00a72SDavid Gibson int i, ret; 1211a7a00a72SDavid Gibson 1212a7a00a72SDavid Gibson ret = kvm_vcpu_ioctl(cs, KVM_GET_REGS, ®s); 1213c995e942SDavid Gibson if (ret < 0) { 1214a7a00a72SDavid Gibson return ret; 1215c995e942SDavid Gibson } 1216a7a00a72SDavid Gibson 1217a7a00a72SDavid Gibson cr = regs.cr; 1218a7a00a72SDavid Gibson for (i = 7; i >= 0; i--) { 1219a7a00a72SDavid Gibson env->crf[i] = cr & 15; 1220a7a00a72SDavid Gibson cr >>= 4; 1221a7a00a72SDavid Gibson } 1222a7a00a72SDavid Gibson 1223a7a00a72SDavid Gibson env->ctr = regs.ctr; 1224a7a00a72SDavid Gibson env->lr = regs.lr; 1225a7a00a72SDavid Gibson cpu_write_xer(env, regs.xer); 1226a7a00a72SDavid Gibson env->msr = regs.msr; 1227a7a00a72SDavid Gibson env->nip = regs.pc; 1228a7a00a72SDavid Gibson 1229a7a00a72SDavid Gibson env->spr[SPR_SRR0] = regs.srr0; 1230a7a00a72SDavid Gibson env->spr[SPR_SRR1] = regs.srr1; 1231a7a00a72SDavid Gibson 1232a7a00a72SDavid Gibson env->spr[SPR_SPRG0] = regs.sprg0; 1233a7a00a72SDavid Gibson env->spr[SPR_SPRG1] = regs.sprg1; 1234a7a00a72SDavid Gibson env->spr[SPR_SPRG2] = regs.sprg2; 1235a7a00a72SDavid Gibson env->spr[SPR_SPRG3] = regs.sprg3; 1236a7a00a72SDavid Gibson env->spr[SPR_SPRG4] = regs.sprg4; 1237a7a00a72SDavid Gibson env->spr[SPR_SPRG5] = regs.sprg5; 1238a7a00a72SDavid Gibson env->spr[SPR_SPRG6] = regs.sprg6; 1239a7a00a72SDavid Gibson env->spr[SPR_SPRG7] = regs.sprg7; 1240a7a00a72SDavid Gibson 1241a7a00a72SDavid Gibson env->spr[SPR_BOOKE_PID] = regs.pid; 1242a7a00a72SDavid Gibson 1243c995e942SDavid Gibson for (i = 0; i < 32; i++) { 1244a7a00a72SDavid Gibson env->gpr[i] = regs.gpr[i]; 1245c995e942SDavid Gibson } 1246a7a00a72SDavid Gibson 1247a7a00a72SDavid Gibson kvm_get_fp(cs); 1248a7a00a72SDavid Gibson 1249a7a00a72SDavid Gibson if (cap_booke_sregs) { 1250a7a00a72SDavid Gibson ret = kvmppc_get_booke_sregs(cpu); 1251a7a00a72SDavid Gibson if (ret < 0) { 1252a7a00a72SDavid Gibson return ret; 1253a7a00a72SDavid Gibson } 1254a7a00a72SDavid Gibson } 1255a7a00a72SDavid Gibson 1256a7a00a72SDavid Gibson if (cap_segstate) { 1257a7a00a72SDavid Gibson ret = kvmppc_get_books_sregs(cpu); 1258a7a00a72SDavid Gibson if (ret < 0) { 1259a7a00a72SDavid Gibson return ret; 1260a7a00a72SDavid Gibson } 1261fafc0b6aSAlexander Graf } 1262ba5e5090SAlexander Graf 1263d67d40eaSDavid Gibson if (cap_hior) { 1264d67d40eaSDavid Gibson kvm_get_one_spr(cs, KVM_REG_PPC_HIOR, SPR_HIOR); 1265d67d40eaSDavid Gibson } 1266d67d40eaSDavid Gibson 1267d67d40eaSDavid Gibson if (cap_one_reg) { 1268d67d40eaSDavid Gibson int i; 1269d67d40eaSDavid Gibson 1270c995e942SDavid Gibson /* 1271c995e942SDavid Gibson * We deliberately ignore errors here, for kernels which have 1272d67d40eaSDavid Gibson * the ONE_REG calls, but don't support the specific 1273d67d40eaSDavid Gibson * registers, there's a reasonable chance things will still 1274c995e942SDavid Gibson * work, at least until we try to migrate. 1275c995e942SDavid Gibson */ 1276d67d40eaSDavid Gibson for (i = 0; i < 1024; i++) { 1277d67d40eaSDavid Gibson uint64_t id = env->spr_cb[i].one_reg_id; 1278d67d40eaSDavid Gibson 1279d67d40eaSDavid Gibson if (id != 0) { 1280d67d40eaSDavid Gibson kvm_get_one_spr(cs, id, i); 1281d67d40eaSDavid Gibson } 1282d67d40eaSDavid Gibson } 12839b00ea49SDavid Gibson 12849b00ea49SDavid Gibson #ifdef TARGET_PPC64 128580b3f79bSAlexey Kardashevskiy if (msr_ts) { 128680b3f79bSAlexey Kardashevskiy for (i = 0; i < ARRAY_SIZE(env->tm_gpr); i++) { 128780b3f79bSAlexey Kardashevskiy kvm_get_one_reg(cs, KVM_REG_PPC_TM_GPR(i), &env->tm_gpr[i]); 128880b3f79bSAlexey Kardashevskiy } 128980b3f79bSAlexey Kardashevskiy for (i = 0; i < ARRAY_SIZE(env->tm_vsr); i++) { 129080b3f79bSAlexey Kardashevskiy kvm_get_one_reg(cs, KVM_REG_PPC_TM_VSR(i), &env->tm_vsr[i]); 129180b3f79bSAlexey Kardashevskiy } 129280b3f79bSAlexey Kardashevskiy kvm_get_one_reg(cs, KVM_REG_PPC_TM_CR, &env->tm_cr); 129380b3f79bSAlexey Kardashevskiy kvm_get_one_reg(cs, KVM_REG_PPC_TM_LR, &env->tm_lr); 129480b3f79bSAlexey Kardashevskiy kvm_get_one_reg(cs, KVM_REG_PPC_TM_CTR, &env->tm_ctr); 129580b3f79bSAlexey Kardashevskiy kvm_get_one_reg(cs, KVM_REG_PPC_TM_FPSCR, &env->tm_fpscr); 129680b3f79bSAlexey Kardashevskiy kvm_get_one_reg(cs, KVM_REG_PPC_TM_AMR, &env->tm_amr); 129780b3f79bSAlexey Kardashevskiy kvm_get_one_reg(cs, KVM_REG_PPC_TM_PPR, &env->tm_ppr); 129880b3f79bSAlexey Kardashevskiy kvm_get_one_reg(cs, KVM_REG_PPC_TM_VRSAVE, &env->tm_vrsave); 129980b3f79bSAlexey Kardashevskiy kvm_get_one_reg(cs, KVM_REG_PPC_TM_VSCR, &env->tm_vscr); 130080b3f79bSAlexey Kardashevskiy kvm_get_one_reg(cs, KVM_REG_PPC_TM_DSCR, &env->tm_dscr); 130180b3f79bSAlexey Kardashevskiy kvm_get_one_reg(cs, KVM_REG_PPC_TM_TAR, &env->tm_tar); 130280b3f79bSAlexey Kardashevskiy } 130380b3f79bSAlexey Kardashevskiy 13049b00ea49SDavid Gibson if (cap_papr) { 13059b00ea49SDavid Gibson if (kvm_get_vpa(cs) < 0) { 13068d83cbf1SGreg Kurz trace_kvm_failed_get_vpa(); 13079b00ea49SDavid Gibson } 13089b00ea49SDavid Gibson } 130998a8b524SAlexey Kardashevskiy 131098a8b524SAlexey Kardashevskiy kvm_get_one_reg(cs, KVM_REG_PPC_TB_OFFSET, &env->tb_env->tb_offset); 1311972bd576SAlexey Kardashevskiy kvm_get_one_spr(cs, KVM_REG_PPC_DPDES, SPR_DPDES); 13129b00ea49SDavid Gibson #endif 1313d67d40eaSDavid Gibson } 1314d67d40eaSDavid Gibson 1315d76d1650Saurel32 return 0; 1316d76d1650Saurel32 } 1317d76d1650Saurel32 13181bc22652SAndreas Färber int kvmppc_set_interrupt(PowerPCCPU *cpu, int irq, int level) 1319fc87e185SAlexander Graf { 1320fc87e185SAlexander Graf unsigned virq = level ? KVM_INTERRUPT_SET_LEVEL : KVM_INTERRUPT_UNSET; 1321fc87e185SAlexander Graf 1322fc87e185SAlexander Graf if (irq != PPC_INTERRUPT_EXT) { 1323fc87e185SAlexander Graf return 0; 1324fc87e185SAlexander Graf } 1325fc87e185SAlexander Graf 13261e8f51e8SShivaprasad G Bhat if (!kvm_enabled() || !cap_interrupt_unset) { 1327fc87e185SAlexander Graf return 0; 1328fc87e185SAlexander Graf } 1329fc87e185SAlexander Graf 13301bc22652SAndreas Färber kvm_vcpu_ioctl(CPU(cpu), KVM_INTERRUPT, &virq); 1331fc87e185SAlexander Graf 1332fc87e185SAlexander Graf return 0; 1333fc87e185SAlexander Graf } 1334fc87e185SAlexander Graf 133520d695a9SAndreas Färber void kvm_arch_pre_run(CPUState *cs, struct kvm_run *run) 1336d76d1650Saurel32 { 13371e8f51e8SShivaprasad G Bhat return; 1338d76d1650Saurel32 } 1339d76d1650Saurel32 13404c663752SPaolo Bonzini MemTxAttrs kvm_arch_post_run(CPUState *cs, struct kvm_run *run) 1341d76d1650Saurel32 { 13424c663752SPaolo Bonzini return MEMTXATTRS_UNSPECIFIED; 1343d76d1650Saurel32 } 1344d76d1650Saurel32 134520d695a9SAndreas Färber int kvm_arch_process_async_events(CPUState *cs) 13460af691d7SMarcelo Tosatti { 1347259186a7SAndreas Färber return cs->halted; 13480af691d7SMarcelo Tosatti } 13490af691d7SMarcelo Tosatti 1350259186a7SAndreas Färber static int kvmppc_handle_halt(PowerPCCPU *cpu) 1351d76d1650Saurel32 { 1352259186a7SAndreas Färber CPUState *cs = CPU(cpu); 1353259186a7SAndreas Färber CPUPPCState *env = &cpu->env; 1354259186a7SAndreas Färber 1355259186a7SAndreas Färber if (!(cs->interrupt_request & CPU_INTERRUPT_HARD) && (msr_ee)) { 1356259186a7SAndreas Färber cs->halted = 1; 135727103424SAndreas Färber cs->exception_index = EXCP_HLT; 1358d76d1650Saurel32 } 1359d76d1650Saurel32 1360bb4ea393SJan Kiszka return 0; 1361d76d1650Saurel32 } 1362d76d1650Saurel32 1363d76d1650Saurel32 /* map dcr access to existing qemu dcr emulation */ 1364c995e942SDavid Gibson static int kvmppc_handle_dcr_read(CPUPPCState *env, 1365c995e942SDavid Gibson uint32_t dcrn, uint32_t *data) 1366d76d1650Saurel32 { 1367c995e942SDavid Gibson if (ppc_dcr_read(env->dcr_env, dcrn, data) < 0) { 1368d76d1650Saurel32 fprintf(stderr, "Read to unhandled DCR (0x%x)\n", dcrn); 1369c995e942SDavid Gibson } 1370d76d1650Saurel32 1371bb4ea393SJan Kiszka return 0; 1372d76d1650Saurel32 } 1373d76d1650Saurel32 1374c995e942SDavid Gibson static int kvmppc_handle_dcr_write(CPUPPCState *env, 1375c995e942SDavid Gibson uint32_t dcrn, uint32_t data) 1376d76d1650Saurel32 { 1377c995e942SDavid Gibson if (ppc_dcr_write(env->dcr_env, dcrn, data) < 0) { 1378d76d1650Saurel32 fprintf(stderr, "Write to unhandled DCR (0x%x)\n", dcrn); 1379c995e942SDavid Gibson } 1380d76d1650Saurel32 1381bb4ea393SJan Kiszka return 0; 1382d76d1650Saurel32 } 1383d76d1650Saurel32 13848a0548f9SBharat Bhushan int kvm_arch_insert_sw_breakpoint(CPUState *cs, struct kvm_sw_breakpoint *bp) 13858a0548f9SBharat Bhushan { 13868a0548f9SBharat Bhushan /* Mixed endian case is not handled */ 13878a0548f9SBharat Bhushan uint32_t sc = debug_inst_opcode; 13888a0548f9SBharat Bhushan 13898a0548f9SBharat Bhushan if (cpu_memory_rw_debug(cs, bp->pc, (uint8_t *)&bp->saved_insn, 13908a0548f9SBharat Bhushan sizeof(sc), 0) || 13918a0548f9SBharat Bhushan cpu_memory_rw_debug(cs, bp->pc, (uint8_t *)&sc, sizeof(sc), 1)) { 13928a0548f9SBharat Bhushan return -EINVAL; 13938a0548f9SBharat Bhushan } 13948a0548f9SBharat Bhushan 13958a0548f9SBharat Bhushan return 0; 13968a0548f9SBharat Bhushan } 13978a0548f9SBharat Bhushan 13988a0548f9SBharat Bhushan int kvm_arch_remove_sw_breakpoint(CPUState *cs, struct kvm_sw_breakpoint *bp) 13998a0548f9SBharat Bhushan { 14008a0548f9SBharat Bhushan uint32_t sc; 14018a0548f9SBharat Bhushan 14028a0548f9SBharat Bhushan if (cpu_memory_rw_debug(cs, bp->pc, (uint8_t *)&sc, sizeof(sc), 0) || 14038a0548f9SBharat Bhushan sc != debug_inst_opcode || 14048a0548f9SBharat Bhushan cpu_memory_rw_debug(cs, bp->pc, (uint8_t *)&bp->saved_insn, 14058a0548f9SBharat Bhushan sizeof(sc), 1)) { 14068a0548f9SBharat Bhushan return -EINVAL; 14078a0548f9SBharat Bhushan } 14088a0548f9SBharat Bhushan 14098a0548f9SBharat Bhushan return 0; 14108a0548f9SBharat Bhushan } 14118a0548f9SBharat Bhushan 141288365d17SBharat Bhushan static int find_hw_breakpoint(target_ulong addr, int type) 141388365d17SBharat Bhushan { 141488365d17SBharat Bhushan int n; 141588365d17SBharat Bhushan 141688365d17SBharat Bhushan assert((nb_hw_breakpoint + nb_hw_watchpoint) 141788365d17SBharat Bhushan <= ARRAY_SIZE(hw_debug_points)); 141888365d17SBharat Bhushan 141988365d17SBharat Bhushan for (n = 0; n < nb_hw_breakpoint + nb_hw_watchpoint; n++) { 142088365d17SBharat Bhushan if (hw_debug_points[n].addr == addr && 142188365d17SBharat Bhushan hw_debug_points[n].type == type) { 142288365d17SBharat Bhushan return n; 142388365d17SBharat Bhushan } 142488365d17SBharat Bhushan } 142588365d17SBharat Bhushan 142688365d17SBharat Bhushan return -1; 142788365d17SBharat Bhushan } 142888365d17SBharat Bhushan 142988365d17SBharat Bhushan static int find_hw_watchpoint(target_ulong addr, int *flag) 143088365d17SBharat Bhushan { 143188365d17SBharat Bhushan int n; 143288365d17SBharat Bhushan 143388365d17SBharat Bhushan n = find_hw_breakpoint(addr, GDB_WATCHPOINT_ACCESS); 143488365d17SBharat Bhushan if (n >= 0) { 143588365d17SBharat Bhushan *flag = BP_MEM_ACCESS; 143688365d17SBharat Bhushan return n; 143788365d17SBharat Bhushan } 143888365d17SBharat Bhushan 143988365d17SBharat Bhushan n = find_hw_breakpoint(addr, GDB_WATCHPOINT_WRITE); 144088365d17SBharat Bhushan if (n >= 0) { 144188365d17SBharat Bhushan *flag = BP_MEM_WRITE; 144288365d17SBharat Bhushan return n; 144388365d17SBharat Bhushan } 144488365d17SBharat Bhushan 144588365d17SBharat Bhushan n = find_hw_breakpoint(addr, GDB_WATCHPOINT_READ); 144688365d17SBharat Bhushan if (n >= 0) { 144788365d17SBharat Bhushan *flag = BP_MEM_READ; 144888365d17SBharat Bhushan return n; 144988365d17SBharat Bhushan } 145088365d17SBharat Bhushan 145188365d17SBharat Bhushan return -1; 145288365d17SBharat Bhushan } 145388365d17SBharat Bhushan 145488365d17SBharat Bhushan int kvm_arch_insert_hw_breakpoint(target_ulong addr, 145588365d17SBharat Bhushan target_ulong len, int type) 145688365d17SBharat Bhushan { 145788365d17SBharat Bhushan if ((nb_hw_breakpoint + nb_hw_watchpoint) >= ARRAY_SIZE(hw_debug_points)) { 145888365d17SBharat Bhushan return -ENOBUFS; 145988365d17SBharat Bhushan } 146088365d17SBharat Bhushan 146188365d17SBharat Bhushan hw_debug_points[nb_hw_breakpoint + nb_hw_watchpoint].addr = addr; 146288365d17SBharat Bhushan hw_debug_points[nb_hw_breakpoint + nb_hw_watchpoint].type = type; 146388365d17SBharat Bhushan 146488365d17SBharat Bhushan switch (type) { 146588365d17SBharat Bhushan case GDB_BREAKPOINT_HW: 146688365d17SBharat Bhushan if (nb_hw_breakpoint >= max_hw_breakpoint) { 146788365d17SBharat Bhushan return -ENOBUFS; 146888365d17SBharat Bhushan } 146988365d17SBharat Bhushan 147088365d17SBharat Bhushan if (find_hw_breakpoint(addr, type) >= 0) { 147188365d17SBharat Bhushan return -EEXIST; 147288365d17SBharat Bhushan } 147388365d17SBharat Bhushan 147488365d17SBharat Bhushan nb_hw_breakpoint++; 147588365d17SBharat Bhushan break; 147688365d17SBharat Bhushan 147788365d17SBharat Bhushan case GDB_WATCHPOINT_WRITE: 147888365d17SBharat Bhushan case GDB_WATCHPOINT_READ: 147988365d17SBharat Bhushan case GDB_WATCHPOINT_ACCESS: 148088365d17SBharat Bhushan if (nb_hw_watchpoint >= max_hw_watchpoint) { 148188365d17SBharat Bhushan return -ENOBUFS; 148288365d17SBharat Bhushan } 148388365d17SBharat Bhushan 148488365d17SBharat Bhushan if (find_hw_breakpoint(addr, type) >= 0) { 148588365d17SBharat Bhushan return -EEXIST; 148688365d17SBharat Bhushan } 148788365d17SBharat Bhushan 148888365d17SBharat Bhushan nb_hw_watchpoint++; 148988365d17SBharat Bhushan break; 149088365d17SBharat Bhushan 149188365d17SBharat Bhushan default: 149288365d17SBharat Bhushan return -ENOSYS; 149388365d17SBharat Bhushan } 149488365d17SBharat Bhushan 149588365d17SBharat Bhushan return 0; 149688365d17SBharat Bhushan } 149788365d17SBharat Bhushan 149888365d17SBharat Bhushan int kvm_arch_remove_hw_breakpoint(target_ulong addr, 149988365d17SBharat Bhushan target_ulong len, int type) 150088365d17SBharat Bhushan { 150188365d17SBharat Bhushan int n; 150288365d17SBharat Bhushan 150388365d17SBharat Bhushan n = find_hw_breakpoint(addr, type); 150488365d17SBharat Bhushan if (n < 0) { 150588365d17SBharat Bhushan return -ENOENT; 150688365d17SBharat Bhushan } 150788365d17SBharat Bhushan 150888365d17SBharat Bhushan switch (type) { 150988365d17SBharat Bhushan case GDB_BREAKPOINT_HW: 151088365d17SBharat Bhushan nb_hw_breakpoint--; 151188365d17SBharat Bhushan break; 151288365d17SBharat Bhushan 151388365d17SBharat Bhushan case GDB_WATCHPOINT_WRITE: 151488365d17SBharat Bhushan case GDB_WATCHPOINT_READ: 151588365d17SBharat Bhushan case GDB_WATCHPOINT_ACCESS: 151688365d17SBharat Bhushan nb_hw_watchpoint--; 151788365d17SBharat Bhushan break; 151888365d17SBharat Bhushan 151988365d17SBharat Bhushan default: 152088365d17SBharat Bhushan return -ENOSYS; 152188365d17SBharat Bhushan } 152288365d17SBharat Bhushan hw_debug_points[n] = hw_debug_points[nb_hw_breakpoint + nb_hw_watchpoint]; 152388365d17SBharat Bhushan 152488365d17SBharat Bhushan return 0; 152588365d17SBharat Bhushan } 152688365d17SBharat Bhushan 152788365d17SBharat Bhushan void kvm_arch_remove_all_hw_breakpoints(void) 152888365d17SBharat Bhushan { 152988365d17SBharat Bhushan nb_hw_breakpoint = nb_hw_watchpoint = 0; 153088365d17SBharat Bhushan } 153188365d17SBharat Bhushan 15328a0548f9SBharat Bhushan void kvm_arch_update_guest_debug(CPUState *cs, struct kvm_guest_debug *dbg) 15338a0548f9SBharat Bhushan { 153488365d17SBharat Bhushan int n; 153588365d17SBharat Bhushan 15368a0548f9SBharat Bhushan /* Software Breakpoint updates */ 15378a0548f9SBharat Bhushan if (kvm_sw_breakpoints_active(cs)) { 15388a0548f9SBharat Bhushan dbg->control |= KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP; 15398a0548f9SBharat Bhushan } 154088365d17SBharat Bhushan 154188365d17SBharat Bhushan assert((nb_hw_breakpoint + nb_hw_watchpoint) 154288365d17SBharat Bhushan <= ARRAY_SIZE(hw_debug_points)); 154388365d17SBharat Bhushan assert((nb_hw_breakpoint + nb_hw_watchpoint) <= ARRAY_SIZE(dbg->arch.bp)); 154488365d17SBharat Bhushan 154588365d17SBharat Bhushan if (nb_hw_breakpoint + nb_hw_watchpoint > 0) { 154688365d17SBharat Bhushan dbg->control |= KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_HW_BP; 154788365d17SBharat Bhushan memset(dbg->arch.bp, 0, sizeof(dbg->arch.bp)); 154888365d17SBharat Bhushan for (n = 0; n < nb_hw_breakpoint + nb_hw_watchpoint; n++) { 154988365d17SBharat Bhushan switch (hw_debug_points[n].type) { 155088365d17SBharat Bhushan case GDB_BREAKPOINT_HW: 155188365d17SBharat Bhushan dbg->arch.bp[n].type = KVMPPC_DEBUG_BREAKPOINT; 155288365d17SBharat Bhushan break; 155388365d17SBharat Bhushan case GDB_WATCHPOINT_WRITE: 155488365d17SBharat Bhushan dbg->arch.bp[n].type = KVMPPC_DEBUG_WATCH_WRITE; 155588365d17SBharat Bhushan break; 155688365d17SBharat Bhushan case GDB_WATCHPOINT_READ: 155788365d17SBharat Bhushan dbg->arch.bp[n].type = KVMPPC_DEBUG_WATCH_READ; 155888365d17SBharat Bhushan break; 155988365d17SBharat Bhushan case GDB_WATCHPOINT_ACCESS: 156088365d17SBharat Bhushan dbg->arch.bp[n].type = KVMPPC_DEBUG_WATCH_WRITE | 156188365d17SBharat Bhushan KVMPPC_DEBUG_WATCH_READ; 156288365d17SBharat Bhushan break; 156388365d17SBharat Bhushan default: 156488365d17SBharat Bhushan cpu_abort(cs, "Unsupported breakpoint type\n"); 156588365d17SBharat Bhushan } 156688365d17SBharat Bhushan dbg->arch.bp[n].addr = hw_debug_points[n].addr; 156788365d17SBharat Bhushan } 156888365d17SBharat Bhushan } 15698a0548f9SBharat Bhushan } 15708a0548f9SBharat Bhushan 15712cbd1581SFabiano Rosas static int kvm_handle_hw_breakpoint(CPUState *cs, 15722cbd1581SFabiano Rosas struct kvm_debug_exit_arch *arch_info) 15738a0548f9SBharat Bhushan { 15746e0552a3SFabiano Rosas int handle = DEBUG_RETURN_GUEST; 157588365d17SBharat Bhushan int n; 157688365d17SBharat Bhushan int flag = 0; 15778a0548f9SBharat Bhushan 157888365d17SBharat Bhushan if (nb_hw_breakpoint + nb_hw_watchpoint > 0) { 157988365d17SBharat Bhushan if (arch_info->status & KVMPPC_DEBUG_BREAKPOINT) { 158088365d17SBharat Bhushan n = find_hw_breakpoint(arch_info->address, GDB_BREAKPOINT_HW); 158188365d17SBharat Bhushan if (n >= 0) { 15826e0552a3SFabiano Rosas handle = DEBUG_RETURN_GDB; 158388365d17SBharat Bhushan } 158488365d17SBharat Bhushan } else if (arch_info->status & (KVMPPC_DEBUG_WATCH_READ | 158588365d17SBharat Bhushan KVMPPC_DEBUG_WATCH_WRITE)) { 158688365d17SBharat Bhushan n = find_hw_watchpoint(arch_info->address, &flag); 158788365d17SBharat Bhushan if (n >= 0) { 15886e0552a3SFabiano Rosas handle = DEBUG_RETURN_GDB; 158988365d17SBharat Bhushan cs->watchpoint_hit = &hw_watchpoint; 159088365d17SBharat Bhushan hw_watchpoint.vaddr = hw_debug_points[n].addr; 159188365d17SBharat Bhushan hw_watchpoint.flags = flag; 159288365d17SBharat Bhushan } 159388365d17SBharat Bhushan } 159488365d17SBharat Bhushan } 15952cbd1581SFabiano Rosas return handle; 15962cbd1581SFabiano Rosas } 15972cbd1581SFabiano Rosas 1598468e3a1aSFabiano Rosas static int kvm_handle_singlestep(void) 1599468e3a1aSFabiano Rosas { 16006e0552a3SFabiano Rosas return DEBUG_RETURN_GDB; 1601468e3a1aSFabiano Rosas } 1602468e3a1aSFabiano Rosas 1603468e3a1aSFabiano Rosas static int kvm_handle_sw_breakpoint(void) 1604468e3a1aSFabiano Rosas { 16056e0552a3SFabiano Rosas return DEBUG_RETURN_GDB; 1606468e3a1aSFabiano Rosas } 1607468e3a1aSFabiano Rosas 16082cbd1581SFabiano Rosas static int kvm_handle_debug(PowerPCCPU *cpu, struct kvm_run *run) 16092cbd1581SFabiano Rosas { 16102cbd1581SFabiano Rosas CPUState *cs = CPU(cpu); 16112cbd1581SFabiano Rosas CPUPPCState *env = &cpu->env; 16122cbd1581SFabiano Rosas struct kvm_debug_exit_arch *arch_info = &run->debug.arch; 16132cbd1581SFabiano Rosas 16142cbd1581SFabiano Rosas if (cs->singlestep_enabled) { 1615468e3a1aSFabiano Rosas return kvm_handle_singlestep(); 1616468e3a1aSFabiano Rosas } 1617468e3a1aSFabiano Rosas 1618468e3a1aSFabiano Rosas if (arch_info->status) { 1619468e3a1aSFabiano Rosas return kvm_handle_hw_breakpoint(cs, arch_info); 1620468e3a1aSFabiano Rosas } 1621468e3a1aSFabiano Rosas 1622468e3a1aSFabiano Rosas if (kvm_find_sw_breakpoint(cs, arch_info->address)) { 1623468e3a1aSFabiano Rosas return kvm_handle_sw_breakpoint(); 1624468e3a1aSFabiano Rosas } 1625468e3a1aSFabiano Rosas 1626468e3a1aSFabiano Rosas /* 1627468e3a1aSFabiano Rosas * QEMU is not able to handle debug exception, so inject 16288a0548f9SBharat Bhushan * program exception to guest; 16298a0548f9SBharat Bhushan * Yes program exception NOT debug exception !! 163088365d17SBharat Bhushan * When QEMU is using debug resources then debug exception must 163188365d17SBharat Bhushan * be always set. To achieve this we set MSR_DE and also set 163288365d17SBharat Bhushan * MSRP_DEP so guest cannot change MSR_DE. 163388365d17SBharat Bhushan * When emulating debug resource for guest we want guest 163488365d17SBharat Bhushan * to control MSR_DE (enable/disable debug interrupt on need). 163588365d17SBharat Bhushan * Supporting both configurations are NOT possible. 163688365d17SBharat Bhushan * So the result is that we cannot share debug resources 163788365d17SBharat Bhushan * between QEMU and Guest on BOOKE architecture. 163888365d17SBharat Bhushan * In the current design QEMU gets the priority over guest, 163988365d17SBharat Bhushan * this means that if QEMU is using debug resources then guest 164088365d17SBharat Bhushan * cannot use them; 16418a0548f9SBharat Bhushan * For software breakpoint QEMU uses a privileged instruction; 16428a0548f9SBharat Bhushan * So there cannot be any reason that we are here for guest 16438a0548f9SBharat Bhushan * set debug exception, only possibility is guest executed a 16448a0548f9SBharat Bhushan * privileged / illegal instruction and that's why we are 16458a0548f9SBharat Bhushan * injecting a program interrupt. 16468a0548f9SBharat Bhushan */ 16478a0548f9SBharat Bhushan cpu_synchronize_state(cs); 1648468e3a1aSFabiano Rosas /* 1649468e3a1aSFabiano Rosas * env->nip is PC, so increment this by 4 to use 16508a0548f9SBharat Bhushan * ppc_cpu_do_interrupt(), which set srr0 = env->nip - 4. 16518a0548f9SBharat Bhushan */ 16528a0548f9SBharat Bhushan env->nip += 4; 16538a0548f9SBharat Bhushan cs->exception_index = POWERPC_EXCP_PROGRAM; 16548a0548f9SBharat Bhushan env->error_code = POWERPC_EXCP_INVAL; 16558a0548f9SBharat Bhushan ppc_cpu_do_interrupt(cs); 16568a0548f9SBharat Bhushan 16576e0552a3SFabiano Rosas return DEBUG_RETURN_GUEST; 16588a0548f9SBharat Bhushan } 16598a0548f9SBharat Bhushan 166020d695a9SAndreas Färber int kvm_arch_handle_exit(CPUState *cs, struct kvm_run *run) 1661d76d1650Saurel32 { 166220d695a9SAndreas Färber PowerPCCPU *cpu = POWERPC_CPU(cs); 166320d695a9SAndreas Färber CPUPPCState *env = &cpu->env; 1664bb4ea393SJan Kiszka int ret; 1665d76d1650Saurel32 16664b8523eeSJan Kiszka qemu_mutex_lock_iothread(); 16674b8523eeSJan Kiszka 1668d76d1650Saurel32 switch (run->exit_reason) { 1669d76d1650Saurel32 case KVM_EXIT_DCR: 1670d76d1650Saurel32 if (run->dcr.is_write) { 16718d83cbf1SGreg Kurz trace_kvm_handle_dcr_write(); 1672d76d1650Saurel32 ret = kvmppc_handle_dcr_write(env, run->dcr.dcrn, run->dcr.data); 1673d76d1650Saurel32 } else { 1674228152c2SBoxuan Li trace_kvm_handle_dcr_read(); 1675d76d1650Saurel32 ret = kvmppc_handle_dcr_read(env, run->dcr.dcrn, &run->dcr.data); 1676d76d1650Saurel32 } 1677d76d1650Saurel32 break; 1678d76d1650Saurel32 case KVM_EXIT_HLT: 16798d83cbf1SGreg Kurz trace_kvm_handle_halt(); 1680259186a7SAndreas Färber ret = kvmppc_handle_halt(cpu); 1681d76d1650Saurel32 break; 1682c6304a4aSDavid Gibson #if defined(TARGET_PPC64) 1683f61b4bedSAlexander Graf case KVM_EXIT_PAPR_HCALL: 16848d83cbf1SGreg Kurz trace_kvm_handle_papr_hcall(); 168520d695a9SAndreas Färber run->papr_hcall.ret = spapr_hypercall(cpu, 1686aa100fa4SAndreas Färber run->papr_hcall.nr, 1687f61b4bedSAlexander Graf run->papr_hcall.args); 168878e8fde2SDavid Gibson ret = 0; 1689f61b4bedSAlexander Graf break; 1690f61b4bedSAlexander Graf #endif 16915b95b8b9SAlexander Graf case KVM_EXIT_EPR: 16928d83cbf1SGreg Kurz trace_kvm_handle_epr(); 1693933b19eaSAlexander Graf run->epr.epr = ldl_phys(cs->as, env->mpic_iack); 16945b95b8b9SAlexander Graf ret = 0; 16955b95b8b9SAlexander Graf break; 169631f2cb8fSBharat Bhushan case KVM_EXIT_WATCHDOG: 16978d83cbf1SGreg Kurz trace_kvm_handle_watchdog_expiry(); 169831f2cb8fSBharat Bhushan watchdog_perform_action(); 169931f2cb8fSBharat Bhushan ret = 0; 170031f2cb8fSBharat Bhushan break; 170131f2cb8fSBharat Bhushan 17028a0548f9SBharat Bhushan case KVM_EXIT_DEBUG: 17038d83cbf1SGreg Kurz trace_kvm_handle_debug_exception(); 17048a0548f9SBharat Bhushan if (kvm_handle_debug(cpu, run)) { 17058a0548f9SBharat Bhushan ret = EXCP_DEBUG; 17068a0548f9SBharat Bhushan break; 17078a0548f9SBharat Bhushan } 17088a0548f9SBharat Bhushan /* re-enter, this exception was guest-internal */ 17098a0548f9SBharat Bhushan ret = 0; 17108a0548f9SBharat Bhushan break; 17118a0548f9SBharat Bhushan 17129ac703acSAravinda Prasad #if defined(TARGET_PPC64) 17139ac703acSAravinda Prasad case KVM_EXIT_NMI: 17149ac703acSAravinda Prasad trace_kvm_handle_nmi_exception(); 17159ac703acSAravinda Prasad ret = kvm_handle_nmi(cpu, run); 17169ac703acSAravinda Prasad break; 17179ac703acSAravinda Prasad #endif 17189ac703acSAravinda Prasad 171973aaec4aSJan Kiszka default: 172073aaec4aSJan Kiszka fprintf(stderr, "KVM: unknown exit reason %d\n", run->exit_reason); 172173aaec4aSJan Kiszka ret = -1; 172273aaec4aSJan Kiszka break; 1723d76d1650Saurel32 } 1724d76d1650Saurel32 17254b8523eeSJan Kiszka qemu_mutex_unlock_iothread(); 1726d76d1650Saurel32 return ret; 1727d76d1650Saurel32 } 1728d76d1650Saurel32 172931f2cb8fSBharat Bhushan int kvmppc_or_tsr_bits(PowerPCCPU *cpu, uint32_t tsr_bits) 173031f2cb8fSBharat Bhushan { 173131f2cb8fSBharat Bhushan CPUState *cs = CPU(cpu); 173231f2cb8fSBharat Bhushan uint32_t bits = tsr_bits; 173331f2cb8fSBharat Bhushan struct kvm_one_reg reg = { 173431f2cb8fSBharat Bhushan .id = KVM_REG_PPC_OR_TSR, 173531f2cb8fSBharat Bhushan .addr = (uintptr_t) &bits, 173631f2cb8fSBharat Bhushan }; 173731f2cb8fSBharat Bhushan 173831f2cb8fSBharat Bhushan return kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, ®); 173931f2cb8fSBharat Bhushan } 174031f2cb8fSBharat Bhushan 174131f2cb8fSBharat Bhushan int kvmppc_clear_tsr_bits(PowerPCCPU *cpu, uint32_t tsr_bits) 174231f2cb8fSBharat Bhushan { 174331f2cb8fSBharat Bhushan 174431f2cb8fSBharat Bhushan CPUState *cs = CPU(cpu); 174531f2cb8fSBharat Bhushan uint32_t bits = tsr_bits; 174631f2cb8fSBharat Bhushan struct kvm_one_reg reg = { 174731f2cb8fSBharat Bhushan .id = KVM_REG_PPC_CLEAR_TSR, 174831f2cb8fSBharat Bhushan .addr = (uintptr_t) &bits, 174931f2cb8fSBharat Bhushan }; 175031f2cb8fSBharat Bhushan 175131f2cb8fSBharat Bhushan return kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, ®); 175231f2cb8fSBharat Bhushan } 175331f2cb8fSBharat Bhushan 175431f2cb8fSBharat Bhushan int kvmppc_set_tcr(PowerPCCPU *cpu) 175531f2cb8fSBharat Bhushan { 175631f2cb8fSBharat Bhushan CPUState *cs = CPU(cpu); 175731f2cb8fSBharat Bhushan CPUPPCState *env = &cpu->env; 175831f2cb8fSBharat Bhushan uint32_t tcr = env->spr[SPR_BOOKE_TCR]; 175931f2cb8fSBharat Bhushan 176031f2cb8fSBharat Bhushan struct kvm_one_reg reg = { 176131f2cb8fSBharat Bhushan .id = KVM_REG_PPC_TCR, 176231f2cb8fSBharat Bhushan .addr = (uintptr_t) &tcr, 176331f2cb8fSBharat Bhushan }; 176431f2cb8fSBharat Bhushan 176531f2cb8fSBharat Bhushan return kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, ®); 176631f2cb8fSBharat Bhushan } 176731f2cb8fSBharat Bhushan 176831f2cb8fSBharat Bhushan int kvmppc_booke_watchdog_enable(PowerPCCPU *cpu) 176931f2cb8fSBharat Bhushan { 177031f2cb8fSBharat Bhushan CPUState *cs = CPU(cpu); 177131f2cb8fSBharat Bhushan int ret; 177231f2cb8fSBharat Bhushan 177331f2cb8fSBharat Bhushan if (!kvm_enabled()) { 177431f2cb8fSBharat Bhushan return -1; 177531f2cb8fSBharat Bhushan } 177631f2cb8fSBharat Bhushan 177731f2cb8fSBharat Bhushan if (!cap_ppc_watchdog) { 177831f2cb8fSBharat Bhushan printf("warning: KVM does not support watchdog"); 177931f2cb8fSBharat Bhushan return -1; 178031f2cb8fSBharat Bhushan } 178131f2cb8fSBharat Bhushan 178248add816SCornelia Huck ret = kvm_vcpu_enable_cap(cs, KVM_CAP_PPC_BOOKE_WATCHDOG, 0); 178331f2cb8fSBharat Bhushan if (ret < 0) { 178431f2cb8fSBharat Bhushan fprintf(stderr, "%s: couldn't enable KVM_CAP_PPC_BOOKE_WATCHDOG: %s\n", 178531f2cb8fSBharat Bhushan __func__, strerror(-ret)); 178631f2cb8fSBharat Bhushan return ret; 178731f2cb8fSBharat Bhushan } 178831f2cb8fSBharat Bhushan 178931f2cb8fSBharat Bhushan return ret; 179031f2cb8fSBharat Bhushan } 179131f2cb8fSBharat Bhushan 1792dc333cd6SAlexander Graf static int read_cpuinfo(const char *field, char *value, int len) 1793dc333cd6SAlexander Graf { 1794dc333cd6SAlexander Graf FILE *f; 1795dc333cd6SAlexander Graf int ret = -1; 1796dc333cd6SAlexander Graf int field_len = strlen(field); 1797dc333cd6SAlexander Graf char line[512]; 1798dc333cd6SAlexander Graf 1799dc333cd6SAlexander Graf f = fopen("/proc/cpuinfo", "r"); 1800dc333cd6SAlexander Graf if (!f) { 1801dc333cd6SAlexander Graf return -1; 1802dc333cd6SAlexander Graf } 1803dc333cd6SAlexander Graf 1804dc333cd6SAlexander Graf do { 1805dc333cd6SAlexander Graf if (!fgets(line, sizeof(line), f)) { 1806dc333cd6SAlexander Graf break; 1807dc333cd6SAlexander Graf } 1808dc333cd6SAlexander Graf if (!strncmp(line, field, field_len)) { 1809ae215068SJim Meyering pstrcpy(value, len, line); 1810dc333cd6SAlexander Graf ret = 0; 1811dc333cd6SAlexander Graf break; 1812dc333cd6SAlexander Graf } 1813dc333cd6SAlexander Graf } while (*line); 1814dc333cd6SAlexander Graf 1815dc333cd6SAlexander Graf fclose(f); 1816dc333cd6SAlexander Graf 1817dc333cd6SAlexander Graf return ret; 1818dc333cd6SAlexander Graf } 1819dc333cd6SAlexander Graf 18209cbcfb59SGreg Kurz static uint32_t kvmppc_get_tbfreq_procfs(void) 1821dc333cd6SAlexander Graf { 1822dc333cd6SAlexander Graf char line[512]; 1823dc333cd6SAlexander Graf char *ns; 18249cbcfb59SGreg Kurz uint32_t tbfreq_fallback = NANOSECONDS_PER_SECOND; 18259cbcfb59SGreg Kurz uint32_t tbfreq_procfs; 1826dc333cd6SAlexander Graf 1827dc333cd6SAlexander Graf if (read_cpuinfo("timebase", line, sizeof(line))) { 18289cbcfb59SGreg Kurz return tbfreq_fallback; 1829dc333cd6SAlexander Graf } 1830dc333cd6SAlexander Graf 1831c995e942SDavid Gibson ns = strchr(line, ':'); 1832c995e942SDavid Gibson if (!ns) { 18339cbcfb59SGreg Kurz return tbfreq_fallback; 1834dc333cd6SAlexander Graf } 1835dc333cd6SAlexander Graf 18369cbcfb59SGreg Kurz tbfreq_procfs = atoi(++ns); 1837dc333cd6SAlexander Graf 18389cbcfb59SGreg Kurz /* 0 is certainly not acceptable by the guest, return fallback value */ 18399cbcfb59SGreg Kurz return tbfreq_procfs ? tbfreq_procfs : tbfreq_fallback; 18409cbcfb59SGreg Kurz } 18419cbcfb59SGreg Kurz 18429cbcfb59SGreg Kurz uint32_t kvmppc_get_tbfreq(void) 18439cbcfb59SGreg Kurz { 18449cbcfb59SGreg Kurz static uint32_t cached_tbfreq; 18459cbcfb59SGreg Kurz 18469cbcfb59SGreg Kurz if (!cached_tbfreq) { 18479cbcfb59SGreg Kurz cached_tbfreq = kvmppc_get_tbfreq_procfs(); 18489cbcfb59SGreg Kurz } 18499cbcfb59SGreg Kurz 18509cbcfb59SGreg Kurz return cached_tbfreq; 1851ef951443SNikunj A Dadhania } 1852ef951443SNikunj A Dadhania 1853ef951443SNikunj A Dadhania bool kvmppc_get_host_serial(char **value) 1854ef951443SNikunj A Dadhania { 1855ef951443SNikunj A Dadhania return g_file_get_contents("/proc/device-tree/system-id", value, NULL, 1856ef951443SNikunj A Dadhania NULL); 1857ef951443SNikunj A Dadhania } 1858ef951443SNikunj A Dadhania 1859ef951443SNikunj A Dadhania bool kvmppc_get_host_model(char **value) 1860ef951443SNikunj A Dadhania { 1861ef951443SNikunj A Dadhania return g_file_get_contents("/proc/device-tree/model", value, NULL, NULL); 1862dc333cd6SAlexander Graf } 18634513d923SGleb Natapov 1864eadaada1SAlexander Graf /* Try to find a device tree node for a CPU with clock-frequency property */ 1865eadaada1SAlexander Graf static int kvmppc_find_cpu_dt(char *buf, int buf_len) 1866eadaada1SAlexander Graf { 1867eadaada1SAlexander Graf struct dirent *dirp; 1868eadaada1SAlexander Graf DIR *dp; 1869eadaada1SAlexander Graf 1870c995e942SDavid Gibson dp = opendir(PROC_DEVTREE_CPU); 1871c995e942SDavid Gibson if (!dp) { 1872eadaada1SAlexander Graf printf("Can't open directory " PROC_DEVTREE_CPU "\n"); 1873eadaada1SAlexander Graf return -1; 1874eadaada1SAlexander Graf } 1875eadaada1SAlexander Graf 1876eadaada1SAlexander Graf buf[0] = '\0'; 1877eadaada1SAlexander Graf while ((dirp = readdir(dp)) != NULL) { 1878eadaada1SAlexander Graf FILE *f; 1879eadaada1SAlexander Graf snprintf(buf, buf_len, "%s%s/clock-frequency", PROC_DEVTREE_CPU, 1880eadaada1SAlexander Graf dirp->d_name); 1881eadaada1SAlexander Graf f = fopen(buf, "r"); 1882eadaada1SAlexander Graf if (f) { 1883eadaada1SAlexander Graf snprintf(buf, buf_len, "%s%s", PROC_DEVTREE_CPU, dirp->d_name); 1884eadaada1SAlexander Graf fclose(f); 1885eadaada1SAlexander Graf break; 1886eadaada1SAlexander Graf } 1887eadaada1SAlexander Graf buf[0] = '\0'; 1888eadaada1SAlexander Graf } 1889eadaada1SAlexander Graf closedir(dp); 1890eadaada1SAlexander Graf if (buf[0] == '\0') { 1891eadaada1SAlexander Graf printf("Unknown host!\n"); 1892eadaada1SAlexander Graf return -1; 1893eadaada1SAlexander Graf } 1894eadaada1SAlexander Graf 1895eadaada1SAlexander Graf return 0; 1896eadaada1SAlexander Graf } 1897eadaada1SAlexander Graf 18987d94a30bSSukadev Bhattiprolu static uint64_t kvmppc_read_int_dt(const char *filename) 1899eadaada1SAlexander Graf { 19009bc884b7SDavid Gibson union { 19019bc884b7SDavid Gibson uint32_t v32; 19029bc884b7SDavid Gibson uint64_t v64; 19039bc884b7SDavid Gibson } u; 1904eadaada1SAlexander Graf FILE *f; 1905eadaada1SAlexander Graf int len; 1906eadaada1SAlexander Graf 19077d94a30bSSukadev Bhattiprolu f = fopen(filename, "rb"); 1908eadaada1SAlexander Graf if (!f) { 1909eadaada1SAlexander Graf return -1; 1910eadaada1SAlexander Graf } 1911eadaada1SAlexander Graf 19129bc884b7SDavid Gibson len = fread(&u, 1, sizeof(u), f); 1913eadaada1SAlexander Graf fclose(f); 1914eadaada1SAlexander Graf switch (len) { 19159bc884b7SDavid Gibson case 4: 19169bc884b7SDavid Gibson /* property is a 32-bit quantity */ 19179bc884b7SDavid Gibson return be32_to_cpu(u.v32); 19189bc884b7SDavid Gibson case 8: 19199bc884b7SDavid Gibson return be64_to_cpu(u.v64); 1920eadaada1SAlexander Graf } 1921eadaada1SAlexander Graf 1922eadaada1SAlexander Graf return 0; 1923eadaada1SAlexander Graf } 1924eadaada1SAlexander Graf 1925c995e942SDavid Gibson /* 1926c995e942SDavid Gibson * Read a CPU node property from the host device tree that's a single 19277d94a30bSSukadev Bhattiprolu * integer (32-bit or 64-bit). Returns 0 if anything goes wrong 1928c995e942SDavid Gibson * (can't find or open the property, or doesn't understand the format) 1929c995e942SDavid Gibson */ 19307d94a30bSSukadev Bhattiprolu static uint64_t kvmppc_read_int_cpu_dt(const char *propname) 19317d94a30bSSukadev Bhattiprolu { 19327d94a30bSSukadev Bhattiprolu char buf[PATH_MAX], *tmp; 19337d94a30bSSukadev Bhattiprolu uint64_t val; 19347d94a30bSSukadev Bhattiprolu 19357d94a30bSSukadev Bhattiprolu if (kvmppc_find_cpu_dt(buf, sizeof(buf))) { 19367d94a30bSSukadev Bhattiprolu return -1; 19377d94a30bSSukadev Bhattiprolu } 19387d94a30bSSukadev Bhattiprolu 19397d94a30bSSukadev Bhattiprolu tmp = g_strdup_printf("%s/%s", buf, propname); 19407d94a30bSSukadev Bhattiprolu val = kvmppc_read_int_dt(tmp); 19417d94a30bSSukadev Bhattiprolu g_free(tmp); 19427d94a30bSSukadev Bhattiprolu 19437d94a30bSSukadev Bhattiprolu return val; 19447d94a30bSSukadev Bhattiprolu } 19457d94a30bSSukadev Bhattiprolu 19469bc884b7SDavid Gibson uint64_t kvmppc_get_clockfreq(void) 19479bc884b7SDavid Gibson { 19489bc884b7SDavid Gibson return kvmppc_read_int_cpu_dt("clock-frequency"); 19499bc884b7SDavid Gibson } 19509bc884b7SDavid Gibson 19517d050527SSuraj Jitindar Singh static int kvmppc_get_dec_bits(void) 19527d050527SSuraj Jitindar Singh { 19537d050527SSuraj Jitindar Singh int nr_bits = kvmppc_read_int_cpu_dt("ibm,dec-bits"); 19547d050527SSuraj Jitindar Singh 19557d050527SSuraj Jitindar Singh if (nr_bits > 0) { 19567d050527SSuraj Jitindar Singh return nr_bits; 19577d050527SSuraj Jitindar Singh } 19587d050527SSuraj Jitindar Singh return 0; 19597d050527SSuraj Jitindar Singh } 19607d050527SSuraj Jitindar Singh 19611a61a9aeSStuart Yoder static int kvmppc_get_pvinfo(CPUPPCState *env, struct kvm_ppc_pvinfo *pvinfo) 196245024f09SAlexander Graf { 1963db70b311SRichard Henderson CPUState *cs = env_cpu(env); 196445024f09SAlexander Graf 19656fd33a75SAlexander Graf if (kvm_vm_check_extension(cs->kvm_state, KVM_CAP_PPC_GET_PVINFO) && 19661a61a9aeSStuart Yoder !kvm_vm_ioctl(cs->kvm_state, KVM_PPC_GET_PVINFO, pvinfo)) { 19671a61a9aeSStuart Yoder return 0; 19681a61a9aeSStuart Yoder } 196945024f09SAlexander Graf 19701a61a9aeSStuart Yoder return 1; 19711a61a9aeSStuart Yoder } 19721a61a9aeSStuart Yoder 19731a61a9aeSStuart Yoder int kvmppc_get_hasidle(CPUPPCState *env) 19741a61a9aeSStuart Yoder { 19751a61a9aeSStuart Yoder struct kvm_ppc_pvinfo pvinfo; 19761a61a9aeSStuart Yoder 19771a61a9aeSStuart Yoder if (!kvmppc_get_pvinfo(env, &pvinfo) && 19781a61a9aeSStuart Yoder (pvinfo.flags & KVM_PPC_PVINFO_FLAGS_EV_IDLE)) { 19791a61a9aeSStuart Yoder return 1; 19801a61a9aeSStuart Yoder } 19811a61a9aeSStuart Yoder 19821a61a9aeSStuart Yoder return 0; 19831a61a9aeSStuart Yoder } 19841a61a9aeSStuart Yoder 19851a61a9aeSStuart Yoder int kvmppc_get_hypercall(CPUPPCState *env, uint8_t *buf, int buf_len) 19861a61a9aeSStuart Yoder { 19871a61a9aeSStuart Yoder uint32_t *hc = (uint32_t *)buf; 19881a61a9aeSStuart Yoder struct kvm_ppc_pvinfo pvinfo; 19891a61a9aeSStuart Yoder 19901a61a9aeSStuart Yoder if (!kvmppc_get_pvinfo(env, &pvinfo)) { 19911a61a9aeSStuart Yoder memcpy(buf, pvinfo.hcall, buf_len); 199245024f09SAlexander Graf return 0; 199345024f09SAlexander Graf } 199445024f09SAlexander Graf 199545024f09SAlexander Graf /* 1996d13fc32eSAlexander Graf * Fallback to always fail hypercalls regardless of endianness: 199745024f09SAlexander Graf * 1998d13fc32eSAlexander Graf * tdi 0,r0,72 (becomes b .+8 in wrong endian, nop in good endian) 199945024f09SAlexander Graf * li r3, -1 2000d13fc32eSAlexander Graf * b .+8 (becomes nop in wrong endian) 2001d13fc32eSAlexander Graf * bswap32(li r3, -1) 200245024f09SAlexander Graf */ 200345024f09SAlexander Graf 2004d13fc32eSAlexander Graf hc[0] = cpu_to_be32(0x08000048); 2005d13fc32eSAlexander Graf hc[1] = cpu_to_be32(0x3860ffff); 2006d13fc32eSAlexander Graf hc[2] = cpu_to_be32(0x48000008); 2007d13fc32eSAlexander Graf hc[3] = cpu_to_be32(bswap32(0x3860ffff)); 200845024f09SAlexander Graf 20090ddbd053SAlexey Kardashevskiy return 1; 201045024f09SAlexander Graf } 201145024f09SAlexander Graf 2012026bfd89SDavid Gibson static inline int kvmppc_enable_hcall(KVMState *s, target_ulong hcall) 2013026bfd89SDavid Gibson { 2014026bfd89SDavid Gibson return kvm_vm_enable_cap(s, KVM_CAP_PPC_ENABLE_HCALL, 0, hcall, 1); 2015026bfd89SDavid Gibson } 2016026bfd89SDavid Gibson 2017026bfd89SDavid Gibson void kvmppc_enable_logical_ci_hcalls(void) 2018026bfd89SDavid Gibson { 2019026bfd89SDavid Gibson /* 2020026bfd89SDavid Gibson * FIXME: it would be nice if we could detect the cases where 2021026bfd89SDavid Gibson * we're using a device which requires the in kernel 2022026bfd89SDavid Gibson * implementation of these hcalls, but the kernel lacks them and 2023026bfd89SDavid Gibson * produce a warning. 2024026bfd89SDavid Gibson */ 2025026bfd89SDavid Gibson kvmppc_enable_hcall(kvm_state, H_LOGICAL_CI_LOAD); 2026026bfd89SDavid Gibson kvmppc_enable_hcall(kvm_state, H_LOGICAL_CI_STORE); 2027026bfd89SDavid Gibson } 2028026bfd89SDavid Gibson 2029ef9971ddSAlexey Kardashevskiy void kvmppc_enable_set_mode_hcall(void) 2030ef9971ddSAlexey Kardashevskiy { 2031ef9971ddSAlexey Kardashevskiy kvmppc_enable_hcall(kvm_state, H_SET_MODE); 2032ef9971ddSAlexey Kardashevskiy } 2033ef9971ddSAlexey Kardashevskiy 20345145ad4fSNathan Whitehorn void kvmppc_enable_clear_ref_mod_hcalls(void) 20355145ad4fSNathan Whitehorn { 20365145ad4fSNathan Whitehorn kvmppc_enable_hcall(kvm_state, H_CLEAR_REF); 20375145ad4fSNathan Whitehorn kvmppc_enable_hcall(kvm_state, H_CLEAR_MOD); 20385145ad4fSNathan Whitehorn } 20395145ad4fSNathan Whitehorn 204068f9f708SSuraj Jitindar Singh void kvmppc_enable_h_page_init(void) 204168f9f708SSuraj Jitindar Singh { 204268f9f708SSuraj Jitindar Singh kvmppc_enable_hcall(kvm_state, H_PAGE_INIT); 204368f9f708SSuraj Jitindar Singh } 204468f9f708SSuraj Jitindar Singh 2045*82123b75SBharata B Rao void kvmppc_enable_h_rpt_invalidate(void) 2046*82123b75SBharata B Rao { 2047*82123b75SBharata B Rao kvmppc_enable_hcall(kvm_state, H_RPT_INVALIDATE); 2048*82123b75SBharata B Rao } 2049*82123b75SBharata B Rao 20501bc22652SAndreas Färber void kvmppc_set_papr(PowerPCCPU *cpu) 2051f61b4bedSAlexander Graf { 20521bc22652SAndreas Färber CPUState *cs = CPU(cpu); 2053f61b4bedSAlexander Graf int ret; 2054f61b4bedSAlexander Graf 2055da20aed1SDavid Gibson if (!kvm_enabled()) { 2056da20aed1SDavid Gibson return; 2057da20aed1SDavid Gibson } 2058da20aed1SDavid Gibson 205948add816SCornelia Huck ret = kvm_vcpu_enable_cap(cs, KVM_CAP_PPC_PAPR, 0); 2060f61b4bedSAlexander Graf if (ret) { 2061072ed5f2SThomas Huth error_report("This vCPU type or KVM version does not support PAPR"); 2062072ed5f2SThomas Huth exit(1); 2063f61b4bedSAlexander Graf } 20649b00ea49SDavid Gibson 2065c995e942SDavid Gibson /* 2066c995e942SDavid Gibson * Update the capability flag so we sync the right information 2067c995e942SDavid Gibson * with kvm 2068c995e942SDavid Gibson */ 20699b00ea49SDavid Gibson cap_papr = 1; 2070f1af19d7SDavid Gibson } 2071f61b4bedSAlexander Graf 2072d6e166c0SDavid Gibson int kvmppc_set_compat(PowerPCCPU *cpu, uint32_t compat_pvr) 20736db5bb0fSAlexey Kardashevskiy { 2074d6e166c0SDavid Gibson return kvm_set_one_reg(CPU(cpu), KVM_REG_PPC_ARCH_COMPAT, &compat_pvr); 20756db5bb0fSAlexey Kardashevskiy } 20766db5bb0fSAlexey Kardashevskiy 20775b95b8b9SAlexander Graf void kvmppc_set_mpic_proxy(PowerPCCPU *cpu, int mpic_proxy) 20785b95b8b9SAlexander Graf { 20795b95b8b9SAlexander Graf CPUState *cs = CPU(cpu); 20805b95b8b9SAlexander Graf int ret; 20815b95b8b9SAlexander Graf 208248add816SCornelia Huck ret = kvm_vcpu_enable_cap(cs, KVM_CAP_PPC_EPR, 0, mpic_proxy); 20835b95b8b9SAlexander Graf if (ret && mpic_proxy) { 2084072ed5f2SThomas Huth error_report("This KVM version does not support EPR"); 2085072ed5f2SThomas Huth exit(1); 20865b95b8b9SAlexander Graf } 20875b95b8b9SAlexander Graf } 20885b95b8b9SAlexander Graf 2089ec010c00SNicholas Piggin bool kvmppc_get_fwnmi(void) 2090ec010c00SNicholas Piggin { 2091ec010c00SNicholas Piggin return cap_fwnmi; 2092ec010c00SNicholas Piggin } 2093ec010c00SNicholas Piggin 2094aef92d87SLaurent Vivier int kvmppc_set_fwnmi(PowerPCCPU *cpu) 20959d953ce4SAravinda Prasad { 20969d953ce4SAravinda Prasad CPUState *cs = CPU(cpu); 20979d953ce4SAravinda Prasad 20989d953ce4SAravinda Prasad return kvm_vcpu_enable_cap(cs, KVM_CAP_PPC_FWNMI, 0); 20999d953ce4SAravinda Prasad } 21009d953ce4SAravinda Prasad 2101e97c3636SDavid Gibson int kvmppc_smt_threads(void) 2102e97c3636SDavid Gibson { 2103e97c3636SDavid Gibson return cap_ppc_smt ? cap_ppc_smt : 1; 2104e97c3636SDavid Gibson } 2105e97c3636SDavid Gibson 2106fa98fbfcSSam Bobroff int kvmppc_set_smt_threads(int smt) 2107fa98fbfcSSam Bobroff { 2108fa98fbfcSSam Bobroff int ret; 2109fa98fbfcSSam Bobroff 2110fa98fbfcSSam Bobroff ret = kvm_vm_enable_cap(kvm_state, KVM_CAP_PPC_SMT, 0, smt, 0); 2111fa98fbfcSSam Bobroff if (!ret) { 2112fa98fbfcSSam Bobroff cap_ppc_smt = smt; 2113fa98fbfcSSam Bobroff } 2114fa98fbfcSSam Bobroff return ret; 2115fa98fbfcSSam Bobroff } 2116fa98fbfcSSam Bobroff 21170c115681SVladimir Sementsov-Ogievskiy void kvmppc_error_append_smt_possible_hint(Error *const *errp) 2118fa98fbfcSSam Bobroff { 2119fa98fbfcSSam Bobroff int i; 2120fa98fbfcSSam Bobroff GString *g; 2121fa98fbfcSSam Bobroff char *s; 2122fa98fbfcSSam Bobroff 2123fa98fbfcSSam Bobroff assert(kvm_enabled()); 2124fa98fbfcSSam Bobroff if (cap_ppc_smt_possible) { 2125fa98fbfcSSam Bobroff g = g_string_new("Available VSMT modes:"); 2126fa98fbfcSSam Bobroff for (i = 63; i >= 0; i--) { 2127fa98fbfcSSam Bobroff if ((1UL << i) & cap_ppc_smt_possible) { 2128fa98fbfcSSam Bobroff g_string_append_printf(g, " %lu", (1UL << i)); 2129fa98fbfcSSam Bobroff } 2130fa98fbfcSSam Bobroff } 2131fa98fbfcSSam Bobroff s = g_string_free(g, false); 21321a639fdfSMarkus Armbruster error_append_hint(errp, "%s.\n", s); 2133fa98fbfcSSam Bobroff g_free(s); 2134fa98fbfcSSam Bobroff } else { 21351a639fdfSMarkus Armbruster error_append_hint(errp, 2136fa98fbfcSSam Bobroff "This KVM seems to be too old to support VSMT.\n"); 2137fa98fbfcSSam Bobroff } 2138fa98fbfcSSam Bobroff } 2139fa98fbfcSSam Bobroff 2140fa98fbfcSSam Bobroff 21417f763a5dSDavid Gibson #ifdef TARGET_PPC64 21426a84737cSDavid Gibson uint64_t kvmppc_vrma_limit(unsigned int hash_shift) 21437f763a5dSDavid Gibson { 2144f36951c1SDavid Gibson struct kvm_ppc_smmu_info info; 2145f36951c1SDavid Gibson long rampagesize, best_page_shift; 2146f36951c1SDavid Gibson int i; 2147f36951c1SDavid Gibson 2148c995e942SDavid Gibson /* 2149c995e942SDavid Gibson * Find the largest hardware supported page size that's less than 2150c995e942SDavid Gibson * or equal to the (logical) backing page size of guest RAM 2151c995e942SDavid Gibson */ 2152ab256960SGreg Kurz kvm_get_smmu_info(&info, &error_fatal); 2153905b7ee4SDavid Hildenbrand rampagesize = qemu_minrampagesize(); 2154f36951c1SDavid Gibson best_page_shift = 0; 2155f36951c1SDavid Gibson 2156f36951c1SDavid Gibson for (i = 0; i < KVM_PPC_PAGE_SIZES_MAX_SZ; i++) { 2157f36951c1SDavid Gibson struct kvm_ppc_one_seg_page_size *sps = &info.sps[i]; 2158f36951c1SDavid Gibson 2159f36951c1SDavid Gibson if (!sps->page_shift) { 2160f36951c1SDavid Gibson continue; 2161f36951c1SDavid Gibson } 2162f36951c1SDavid Gibson 2163f36951c1SDavid Gibson if ((sps->page_shift > best_page_shift) 2164f36951c1SDavid Gibson && ((1UL << sps->page_shift) <= rampagesize)) { 2165f36951c1SDavid Gibson best_page_shift = sps->page_shift; 2166f36951c1SDavid Gibson } 2167f36951c1SDavid Gibson } 2168f36951c1SDavid Gibson 21696a84737cSDavid Gibson return 1ULL << (best_page_shift + hash_shift - 7); 21707f763a5dSDavid Gibson } 21717f763a5dSDavid Gibson #endif 21727f763a5dSDavid Gibson 2173da95324eSAlexey Kardashevskiy bool kvmppc_spapr_use_multitce(void) 2174da95324eSAlexey Kardashevskiy { 2175da95324eSAlexey Kardashevskiy return cap_spapr_multitce; 2176da95324eSAlexey Kardashevskiy } 2177da95324eSAlexey Kardashevskiy 21783dc410aeSAlexey Kardashevskiy int kvmppc_spapr_enable_inkernel_multitce(void) 21793dc410aeSAlexey Kardashevskiy { 21803dc410aeSAlexey Kardashevskiy int ret; 21813dc410aeSAlexey Kardashevskiy 21823dc410aeSAlexey Kardashevskiy ret = kvm_vm_enable_cap(kvm_state, KVM_CAP_PPC_ENABLE_HCALL, 0, 21833dc410aeSAlexey Kardashevskiy H_PUT_TCE_INDIRECT, 1); 21843dc410aeSAlexey Kardashevskiy if (!ret) { 21853dc410aeSAlexey Kardashevskiy ret = kvm_vm_enable_cap(kvm_state, KVM_CAP_PPC_ENABLE_HCALL, 0, 21863dc410aeSAlexey Kardashevskiy H_STUFF_TCE, 1); 21873dc410aeSAlexey Kardashevskiy } 21883dc410aeSAlexey Kardashevskiy 21893dc410aeSAlexey Kardashevskiy return ret; 21903dc410aeSAlexey Kardashevskiy } 21913dc410aeSAlexey Kardashevskiy 2192d6ee2a7cSAlexey Kardashevskiy void *kvmppc_create_spapr_tce(uint32_t liobn, uint32_t page_shift, 2193d6ee2a7cSAlexey Kardashevskiy uint64_t bus_offset, uint32_t nb_table, 2194d6ee2a7cSAlexey Kardashevskiy int *pfd, bool need_vfio) 21950f5cb298SDavid Gibson { 21960f5cb298SDavid Gibson long len; 21970f5cb298SDavid Gibson int fd; 21980f5cb298SDavid Gibson void *table; 21990f5cb298SDavid Gibson 2200c995e942SDavid Gibson /* 2201c995e942SDavid Gibson * Must set fd to -1 so we don't try to munmap when called for 2202b5aec396SDavid Gibson * destroying the table, which the upper layers -will- do 2203b5aec396SDavid Gibson */ 2204b5aec396SDavid Gibson *pfd = -1; 22056a81dd17SDavid Gibson if (!cap_spapr_tce || (need_vfio && !cap_spapr_vfio)) { 22060f5cb298SDavid Gibson return NULL; 22070f5cb298SDavid Gibson } 22080f5cb298SDavid Gibson 2209d6ee2a7cSAlexey Kardashevskiy if (cap_spapr_tce_64) { 2210d6ee2a7cSAlexey Kardashevskiy struct kvm_create_spapr_tce_64 args = { 2211d6ee2a7cSAlexey Kardashevskiy .liobn = liobn, 2212d6ee2a7cSAlexey Kardashevskiy .page_shift = page_shift, 2213d6ee2a7cSAlexey Kardashevskiy .offset = bus_offset >> page_shift, 2214d6ee2a7cSAlexey Kardashevskiy .size = nb_table, 2215d6ee2a7cSAlexey Kardashevskiy .flags = 0 2216d6ee2a7cSAlexey Kardashevskiy }; 2217d6ee2a7cSAlexey Kardashevskiy fd = kvm_vm_ioctl(kvm_state, KVM_CREATE_SPAPR_TCE_64, &args); 2218d6ee2a7cSAlexey Kardashevskiy if (fd < 0) { 2219d6ee2a7cSAlexey Kardashevskiy fprintf(stderr, 2220d6ee2a7cSAlexey Kardashevskiy "KVM: Failed to create TCE64 table for liobn 0x%x\n", 2221d6ee2a7cSAlexey Kardashevskiy liobn); 2222d6ee2a7cSAlexey Kardashevskiy return NULL; 2223d6ee2a7cSAlexey Kardashevskiy } 2224d6ee2a7cSAlexey Kardashevskiy } else if (cap_spapr_tce) { 2225d6ee2a7cSAlexey Kardashevskiy uint64_t window_size = (uint64_t) nb_table << page_shift; 2226d6ee2a7cSAlexey Kardashevskiy struct kvm_create_spapr_tce args = { 2227d6ee2a7cSAlexey Kardashevskiy .liobn = liobn, 2228d6ee2a7cSAlexey Kardashevskiy .window_size = window_size, 2229d6ee2a7cSAlexey Kardashevskiy }; 2230d6ee2a7cSAlexey Kardashevskiy if ((window_size != args.window_size) || bus_offset) { 2231d6ee2a7cSAlexey Kardashevskiy return NULL; 2232d6ee2a7cSAlexey Kardashevskiy } 22330f5cb298SDavid Gibson fd = kvm_vm_ioctl(kvm_state, KVM_CREATE_SPAPR_TCE, &args); 22340f5cb298SDavid Gibson if (fd < 0) { 2235b5aec396SDavid Gibson fprintf(stderr, "KVM: Failed to create TCE table for liobn 0x%x\n", 2236b5aec396SDavid Gibson liobn); 22370f5cb298SDavid Gibson return NULL; 22380f5cb298SDavid Gibson } 2239d6ee2a7cSAlexey Kardashevskiy } else { 2240d6ee2a7cSAlexey Kardashevskiy return NULL; 2241d6ee2a7cSAlexey Kardashevskiy } 22420f5cb298SDavid Gibson 2243d6ee2a7cSAlexey Kardashevskiy len = nb_table * sizeof(uint64_t); 22440f5cb298SDavid Gibson /* FIXME: round this up to page size */ 22450f5cb298SDavid Gibson 224674b41e56SDavid Gibson table = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); 22470f5cb298SDavid Gibson if (table == MAP_FAILED) { 2248b5aec396SDavid Gibson fprintf(stderr, "KVM: Failed to map TCE table for liobn 0x%x\n", 2249b5aec396SDavid Gibson liobn); 22500f5cb298SDavid Gibson close(fd); 22510f5cb298SDavid Gibson return NULL; 22520f5cb298SDavid Gibson } 22530f5cb298SDavid Gibson 22540f5cb298SDavid Gibson *pfd = fd; 22550f5cb298SDavid Gibson return table; 22560f5cb298SDavid Gibson } 22570f5cb298SDavid Gibson 2258523e7b8aSAlexey Kardashevskiy int kvmppc_remove_spapr_tce(void *table, int fd, uint32_t nb_table) 22590f5cb298SDavid Gibson { 22600f5cb298SDavid Gibson long len; 22610f5cb298SDavid Gibson 22620f5cb298SDavid Gibson if (fd < 0) { 22630f5cb298SDavid Gibson return -1; 22640f5cb298SDavid Gibson } 22650f5cb298SDavid Gibson 2266523e7b8aSAlexey Kardashevskiy len = nb_table * sizeof(uint64_t); 22670f5cb298SDavid Gibson if ((munmap(table, len) < 0) || 22680f5cb298SDavid Gibson (close(fd) < 0)) { 2269b5aec396SDavid Gibson fprintf(stderr, "KVM: Unexpected error removing TCE table: %s", 2270b5aec396SDavid Gibson strerror(errno)); 22710f5cb298SDavid Gibson /* Leak the table */ 22720f5cb298SDavid Gibson } 22730f5cb298SDavid Gibson 22740f5cb298SDavid Gibson return 0; 22750f5cb298SDavid Gibson } 22760f5cb298SDavid Gibson 22777f763a5dSDavid Gibson int kvmppc_reset_htab(int shift_hint) 22787f763a5dSDavid Gibson { 22797f763a5dSDavid Gibson uint32_t shift = shift_hint; 22807f763a5dSDavid Gibson 2281ace9a2cbSDavid Gibson if (!kvm_enabled()) { 2282ace9a2cbSDavid Gibson /* Full emulation, tell caller to allocate htab itself */ 2283ace9a2cbSDavid Gibson return 0; 2284ace9a2cbSDavid Gibson } 22856977afdaSGreg Kurz if (kvm_vm_check_extension(kvm_state, KVM_CAP_PPC_ALLOC_HTAB)) { 22867f763a5dSDavid Gibson int ret; 22877f763a5dSDavid Gibson ret = kvm_vm_ioctl(kvm_state, KVM_PPC_ALLOCATE_HTAB, &shift); 2288ace9a2cbSDavid Gibson if (ret == -ENOTTY) { 2289c995e942SDavid Gibson /* 2290c995e942SDavid Gibson * At least some versions of PR KVM advertise the 2291ace9a2cbSDavid Gibson * capability, but don't implement the ioctl(). Oops. 2292ace9a2cbSDavid Gibson * Return 0 so that we allocate the htab in qemu, as is 2293c995e942SDavid Gibson * correct for PR. 2294c995e942SDavid Gibson */ 2295ace9a2cbSDavid Gibson return 0; 2296ace9a2cbSDavid Gibson } else if (ret < 0) { 22977f763a5dSDavid Gibson return ret; 22987f763a5dSDavid Gibson } 22997f763a5dSDavid Gibson return shift; 23007f763a5dSDavid Gibson } 23017f763a5dSDavid Gibson 2302c995e942SDavid Gibson /* 2303c995e942SDavid Gibson * We have a kernel that predates the htab reset calls. For PR 2304ace9a2cbSDavid Gibson * KVM, we need to allocate the htab ourselves, for an HV KVM of 2305c995e942SDavid Gibson * this era, it has allocated a 16MB fixed size hash table 2306c995e942SDavid Gibson * already. 2307c995e942SDavid Gibson */ 230896c9cff0SThomas Huth if (kvmppc_is_pr(kvm_state)) { 2309ace9a2cbSDavid Gibson /* PR - tell caller to allocate htab */ 23107f763a5dSDavid Gibson return 0; 2311ace9a2cbSDavid Gibson } else { 2312ace9a2cbSDavid Gibson /* HV - assume 16MB kernel allocated htab */ 2313ace9a2cbSDavid Gibson return 24; 2314ace9a2cbSDavid Gibson } 23157f763a5dSDavid Gibson } 23167f763a5dSDavid Gibson 2317a1e98583SDavid Gibson static inline uint32_t mfpvr(void) 2318a1e98583SDavid Gibson { 2319a1e98583SDavid Gibson uint32_t pvr; 2320a1e98583SDavid Gibson 2321a1e98583SDavid Gibson asm ("mfpvr %0" 2322a1e98583SDavid Gibson : "=r"(pvr)); 2323a1e98583SDavid Gibson return pvr; 2324a1e98583SDavid Gibson } 2325a1e98583SDavid Gibson 2326a7342588SDavid Gibson static void alter_insns(uint64_t *word, uint64_t flags, bool on) 2327a7342588SDavid Gibson { 2328a7342588SDavid Gibson if (on) { 2329a7342588SDavid Gibson *word |= flags; 2330a7342588SDavid Gibson } else { 2331a7342588SDavid Gibson *word &= ~flags; 2332a7342588SDavid Gibson } 2333a7342588SDavid Gibson } 2334a7342588SDavid Gibson 23352985b86bSAndreas Färber static void kvmppc_host_cpu_class_init(ObjectClass *oc, void *data) 23362985b86bSAndreas Färber { 23372985b86bSAndreas Färber PowerPCCPUClass *pcc = POWERPC_CPU_CLASS(oc); 23380cbad81fSDavid Gibson uint32_t dcache_size = kvmppc_read_int_cpu_dt("d-cache-size"); 23390cbad81fSDavid Gibson uint32_t icache_size = kvmppc_read_int_cpu_dt("i-cache-size"); 2340a1e98583SDavid Gibson 2341cfe34f44SAndreas Färber /* Now fix up the class with information we can query from the host */ 23423bc9ccc0SAlexey Kardashevskiy pcc->pvr = mfpvr(); 2343a7342588SDavid Gibson 23443f2ca480SDavid Gibson alter_insns(&pcc->insns_flags, PPC_ALTIVEC, 23453f2ca480SDavid Gibson qemu_getauxval(AT_HWCAP) & PPC_FEATURE_HAS_ALTIVEC); 23463f2ca480SDavid Gibson alter_insns(&pcc->insns_flags2, PPC2_VSX, 23473f2ca480SDavid Gibson qemu_getauxval(AT_HWCAP) & PPC_FEATURE_HAS_VSX); 23483f2ca480SDavid Gibson alter_insns(&pcc->insns_flags2, PPC2_DFP, 23493f2ca480SDavid Gibson qemu_getauxval(AT_HWCAP) & PPC_FEATURE_HAS_DFP); 23500cbad81fSDavid Gibson 23510cbad81fSDavid Gibson if (dcache_size != -1) { 23520cbad81fSDavid Gibson pcc->l1_dcache_size = dcache_size; 23530cbad81fSDavid Gibson } 23540cbad81fSDavid Gibson 23550cbad81fSDavid Gibson if (icache_size != -1) { 23560cbad81fSDavid Gibson pcc->l1_icache_size = icache_size; 23570cbad81fSDavid Gibson } 2358c64abd1fSSam Bobroff 2359c64abd1fSSam Bobroff #if defined(TARGET_PPC64) 2360c64abd1fSSam Bobroff pcc->radix_page_info = kvm_get_radix_page_info(); 23615f3066d8SDavid Gibson 23625f3066d8SDavid Gibson if ((pcc->pvr & 0xffffff00) == CPU_POWERPC_POWER9_DD1) { 23635f3066d8SDavid Gibson /* 23645f3066d8SDavid Gibson * POWER9 DD1 has some bugs which make it not really ISA 3.00 23655f3066d8SDavid Gibson * compliant. More importantly, advertising ISA 3.00 23665f3066d8SDavid Gibson * architected mode may prevent guests from activating 23675f3066d8SDavid Gibson * necessary DD1 workarounds. 23685f3066d8SDavid Gibson */ 23695f3066d8SDavid Gibson pcc->pcr_supported &= ~(PCR_COMPAT_3_00 | PCR_COMPAT_2_07 23705f3066d8SDavid Gibson | PCR_COMPAT_2_06 | PCR_COMPAT_2_05); 23715f3066d8SDavid Gibson } 2372c64abd1fSSam Bobroff #endif /* defined(TARGET_PPC64) */ 2373a1e98583SDavid Gibson } 2374a1e98583SDavid Gibson 23753b961124SStuart Yoder bool kvmppc_has_cap_epr(void) 23763b961124SStuart Yoder { 23773b961124SStuart Yoder return cap_epr; 23783b961124SStuart Yoder } 23793b961124SStuart Yoder 238087a91de6SAlexander Graf bool kvmppc_has_cap_fixup_hcalls(void) 238187a91de6SAlexander Graf { 238287a91de6SAlexander Graf return cap_fixup_hcalls; 238387a91de6SAlexander Graf } 238487a91de6SAlexander Graf 2385bac3bf28SThomas Huth bool kvmppc_has_cap_htm(void) 2386bac3bf28SThomas Huth { 2387bac3bf28SThomas Huth return cap_htm; 2388bac3bf28SThomas Huth } 2389bac3bf28SThomas Huth 2390cf1c4cceSSam Bobroff bool kvmppc_has_cap_mmu_radix(void) 2391cf1c4cceSSam Bobroff { 2392cf1c4cceSSam Bobroff return cap_mmu_radix; 2393cf1c4cceSSam Bobroff } 2394cf1c4cceSSam Bobroff 2395cf1c4cceSSam Bobroff bool kvmppc_has_cap_mmu_hash_v3(void) 2396cf1c4cceSSam Bobroff { 2397cf1c4cceSSam Bobroff return cap_mmu_hash_v3; 2398cf1c4cceSSam Bobroff } 2399cf1c4cceSSam Bobroff 2400072f416aSSuraj Jitindar Singh static bool kvmppc_power8_host(void) 2401072f416aSSuraj Jitindar Singh { 2402072f416aSSuraj Jitindar Singh bool ret = false; 2403072f416aSSuraj Jitindar Singh #ifdef TARGET_PPC64 2404072f416aSSuraj Jitindar Singh { 2405072f416aSSuraj Jitindar Singh uint32_t base_pvr = CPU_POWERPC_POWER_SERVER_MASK & mfpvr(); 2406072f416aSSuraj Jitindar Singh ret = (base_pvr == CPU_POWERPC_POWER8E_BASE) || 2407072f416aSSuraj Jitindar Singh (base_pvr == CPU_POWERPC_POWER8NVL_BASE) || 2408072f416aSSuraj Jitindar Singh (base_pvr == CPU_POWERPC_POWER8_BASE); 2409072f416aSSuraj Jitindar Singh } 2410072f416aSSuraj Jitindar Singh #endif /* TARGET_PPC64 */ 2411072f416aSSuraj Jitindar Singh return ret; 2412072f416aSSuraj Jitindar Singh } 2413072f416aSSuraj Jitindar Singh 24148fea7044SSuraj Jitindar Singh static int parse_cap_ppc_safe_cache(struct kvm_ppc_cpu_char c) 24158fea7044SSuraj Jitindar Singh { 2416072f416aSSuraj Jitindar Singh bool l1d_thread_priv_req = !kvmppc_power8_host(); 2417072f416aSSuraj Jitindar Singh 24188fea7044SSuraj Jitindar Singh if (~c.behaviour & c.behaviour_mask & H_CPU_BEHAV_L1D_FLUSH_PR) { 24198fea7044SSuraj Jitindar Singh return 2; 2420072f416aSSuraj Jitindar Singh } else if ((!l1d_thread_priv_req || 2421072f416aSSuraj Jitindar Singh c.character & c.character_mask & H_CPU_CHAR_L1D_THREAD_PRIV) && 24228fea7044SSuraj Jitindar Singh (c.character & c.character_mask 24238fea7044SSuraj Jitindar Singh & (H_CPU_CHAR_L1D_FLUSH_ORI30 | H_CPU_CHAR_L1D_FLUSH_TRIG2))) { 24248fea7044SSuraj Jitindar Singh return 1; 24258fea7044SSuraj Jitindar Singh } 24268fea7044SSuraj Jitindar Singh 24278fea7044SSuraj Jitindar Singh return 0; 24288fea7044SSuraj Jitindar Singh } 24298fea7044SSuraj Jitindar Singh 24308fea7044SSuraj Jitindar Singh static int parse_cap_ppc_safe_bounds_check(struct kvm_ppc_cpu_char c) 24318fea7044SSuraj Jitindar Singh { 24328fea7044SSuraj Jitindar Singh if (~c.behaviour & c.behaviour_mask & H_CPU_BEHAV_BNDS_CHK_SPEC_BAR) { 24338fea7044SSuraj Jitindar Singh return 2; 24348fea7044SSuraj Jitindar Singh } else if (c.character & c.character_mask & H_CPU_CHAR_SPEC_BAR_ORI31) { 24358fea7044SSuraj Jitindar Singh return 1; 24368fea7044SSuraj Jitindar Singh } 24378fea7044SSuraj Jitindar Singh 24388fea7044SSuraj Jitindar Singh return 0; 24398fea7044SSuraj Jitindar Singh } 24408fea7044SSuraj Jitindar Singh 24418fea7044SSuraj Jitindar Singh static int parse_cap_ppc_safe_indirect_branch(struct kvm_ppc_cpu_char c) 24428fea7044SSuraj Jitindar Singh { 2443399b2896SSuraj Jitindar Singh if ((~c.behaviour & c.behaviour_mask & H_CPU_BEHAV_FLUSH_COUNT_CACHE) && 2444399b2896SSuraj Jitindar Singh (~c.character & c.character_mask & H_CPU_CHAR_CACHE_COUNT_DIS) && 2445399b2896SSuraj Jitindar Singh (~c.character & c.character_mask & H_CPU_CHAR_BCCTRL_SERIALISED)) { 2446399b2896SSuraj Jitindar Singh return SPAPR_CAP_FIXED_NA; 2447399b2896SSuraj Jitindar Singh } else if (c.behaviour & c.behaviour_mask & H_CPU_BEHAV_FLUSH_COUNT_CACHE) { 2448399b2896SSuraj Jitindar Singh return SPAPR_CAP_WORKAROUND; 2449399b2896SSuraj Jitindar Singh } else if (c.character & c.character_mask & H_CPU_CHAR_CACHE_COUNT_DIS) { 24508fea7044SSuraj Jitindar Singh return SPAPR_CAP_FIXED_CCD; 24518fea7044SSuraj Jitindar Singh } else if (c.character & c.character_mask & H_CPU_CHAR_BCCTRL_SERIALISED) { 24528fea7044SSuraj Jitindar Singh return SPAPR_CAP_FIXED_IBS; 24538fea7044SSuraj Jitindar Singh } 24548fea7044SSuraj Jitindar Singh 24558fea7044SSuraj Jitindar Singh return 0; 24568fea7044SSuraj Jitindar Singh } 24578fea7044SSuraj Jitindar Singh 24588ff43ee4SSuraj Jitindar Singh static int parse_cap_ppc_count_cache_flush_assist(struct kvm_ppc_cpu_char c) 24598ff43ee4SSuraj Jitindar Singh { 24608ff43ee4SSuraj Jitindar Singh if (c.character & c.character_mask & H_CPU_CHAR_BCCTR_FLUSH_ASSIST) { 24618ff43ee4SSuraj Jitindar Singh return 1; 24628ff43ee4SSuraj Jitindar Singh } 24638ff43ee4SSuraj Jitindar Singh return 0; 24648ff43ee4SSuraj Jitindar Singh } 24658ff43ee4SSuraj Jitindar Singh 246638afd772SCédric Le Goater bool kvmppc_has_cap_xive(void) 246738afd772SCédric Le Goater { 246838afd772SCédric Le Goater return cap_xive; 246938afd772SCédric Le Goater } 247038afd772SCédric Le Goater 24718acc2ae5SSuraj Jitindar Singh static void kvmppc_get_cpu_characteristics(KVMState *s) 24728acc2ae5SSuraj Jitindar Singh { 24738acc2ae5SSuraj Jitindar Singh struct kvm_ppc_cpu_char c; 24748acc2ae5SSuraj Jitindar Singh int ret; 24758acc2ae5SSuraj Jitindar Singh 24768acc2ae5SSuraj Jitindar Singh /* Assume broken */ 24778acc2ae5SSuraj Jitindar Singh cap_ppc_safe_cache = 0; 24788acc2ae5SSuraj Jitindar Singh cap_ppc_safe_bounds_check = 0; 24798acc2ae5SSuraj Jitindar Singh cap_ppc_safe_indirect_branch = 0; 24808acc2ae5SSuraj Jitindar Singh 24818acc2ae5SSuraj Jitindar Singh ret = kvm_vm_check_extension(s, KVM_CAP_PPC_GET_CPU_CHAR); 24828acc2ae5SSuraj Jitindar Singh if (!ret) { 24838acc2ae5SSuraj Jitindar Singh return; 24848acc2ae5SSuraj Jitindar Singh } 24858acc2ae5SSuraj Jitindar Singh ret = kvm_vm_ioctl(s, KVM_PPC_GET_CPU_CHAR, &c); 24868acc2ae5SSuraj Jitindar Singh if (ret < 0) { 24878acc2ae5SSuraj Jitindar Singh return; 24888acc2ae5SSuraj Jitindar Singh } 24898fea7044SSuraj Jitindar Singh 24908fea7044SSuraj Jitindar Singh cap_ppc_safe_cache = parse_cap_ppc_safe_cache(c); 24918fea7044SSuraj Jitindar Singh cap_ppc_safe_bounds_check = parse_cap_ppc_safe_bounds_check(c); 24928fea7044SSuraj Jitindar Singh cap_ppc_safe_indirect_branch = parse_cap_ppc_safe_indirect_branch(c); 24938ff43ee4SSuraj Jitindar Singh cap_ppc_count_cache_flush_assist = 24948ff43ee4SSuraj Jitindar Singh parse_cap_ppc_count_cache_flush_assist(c); 24958acc2ae5SSuraj Jitindar Singh } 24968acc2ae5SSuraj Jitindar Singh 24978acc2ae5SSuraj Jitindar Singh int kvmppc_get_cap_safe_cache(void) 24988acc2ae5SSuraj Jitindar Singh { 24998acc2ae5SSuraj Jitindar Singh return cap_ppc_safe_cache; 25008acc2ae5SSuraj Jitindar Singh } 25018acc2ae5SSuraj Jitindar Singh 25028acc2ae5SSuraj Jitindar Singh int kvmppc_get_cap_safe_bounds_check(void) 25038acc2ae5SSuraj Jitindar Singh { 25048acc2ae5SSuraj Jitindar Singh return cap_ppc_safe_bounds_check; 25058acc2ae5SSuraj Jitindar Singh } 25068acc2ae5SSuraj Jitindar Singh 25078acc2ae5SSuraj Jitindar Singh int kvmppc_get_cap_safe_indirect_branch(void) 25088acc2ae5SSuraj Jitindar Singh { 25098acc2ae5SSuraj Jitindar Singh return cap_ppc_safe_indirect_branch; 25108acc2ae5SSuraj Jitindar Singh } 25118acc2ae5SSuraj Jitindar Singh 25128ff43ee4SSuraj Jitindar Singh int kvmppc_get_cap_count_cache_flush_assist(void) 25138ff43ee4SSuraj Jitindar Singh { 25148ff43ee4SSuraj Jitindar Singh return cap_ppc_count_cache_flush_assist; 25158ff43ee4SSuraj Jitindar Singh } 25168ff43ee4SSuraj Jitindar Singh 2517b9a477b7SSuraj Jitindar Singh bool kvmppc_has_cap_nested_kvm_hv(void) 2518b9a477b7SSuraj Jitindar Singh { 2519b9a477b7SSuraj Jitindar Singh return !!cap_ppc_nested_kvm_hv; 2520b9a477b7SSuraj Jitindar Singh } 2521b9a477b7SSuraj Jitindar Singh 2522b9a477b7SSuraj Jitindar Singh int kvmppc_set_cap_nested_kvm_hv(int enable) 2523b9a477b7SSuraj Jitindar Singh { 2524b9a477b7SSuraj Jitindar Singh return kvm_vm_enable_cap(kvm_state, KVM_CAP_PPC_NESTED_HV, 0, enable); 2525b9a477b7SSuraj Jitindar Singh } 2526b9a477b7SSuraj Jitindar Singh 25279ded780cSAlexey Kardashevskiy bool kvmppc_has_cap_spapr_vfio(void) 25289ded780cSAlexey Kardashevskiy { 25299ded780cSAlexey Kardashevskiy return cap_spapr_vfio; 25309ded780cSAlexey Kardashevskiy } 25319ded780cSAlexey Kardashevskiy 25327d050527SSuraj Jitindar Singh int kvmppc_get_cap_large_decr(void) 25337d050527SSuraj Jitindar Singh { 25347d050527SSuraj Jitindar Singh return cap_large_decr; 25357d050527SSuraj Jitindar Singh } 25367d050527SSuraj Jitindar Singh 25377d050527SSuraj Jitindar Singh int kvmppc_enable_cap_large_decr(PowerPCCPU *cpu, int enable) 25387d050527SSuraj Jitindar Singh { 25397d050527SSuraj Jitindar Singh CPUState *cs = CPU(cpu); 25407d050527SSuraj Jitindar Singh uint64_t lpcr; 25417d050527SSuraj Jitindar Singh 25427d050527SSuraj Jitindar Singh kvm_get_one_reg(cs, KVM_REG_PPC_LPCR_64, &lpcr); 25437d050527SSuraj Jitindar Singh /* Do we need to modify the LPCR? */ 25447d050527SSuraj Jitindar Singh if (!!(lpcr & LPCR_LD) != !!enable) { 25457d050527SSuraj Jitindar Singh if (enable) { 25467d050527SSuraj Jitindar Singh lpcr |= LPCR_LD; 25477d050527SSuraj Jitindar Singh } else { 25487d050527SSuraj Jitindar Singh lpcr &= ~LPCR_LD; 25497d050527SSuraj Jitindar Singh } 25507d050527SSuraj Jitindar Singh kvm_set_one_reg(cs, KVM_REG_PPC_LPCR_64, &lpcr); 25517d050527SSuraj Jitindar Singh kvm_get_one_reg(cs, KVM_REG_PPC_LPCR_64, &lpcr); 25527d050527SSuraj Jitindar Singh 25537d050527SSuraj Jitindar Singh if (!!(lpcr & LPCR_LD) != !!enable) { 25547d050527SSuraj Jitindar Singh return -1; 25557d050527SSuraj Jitindar Singh } 25567d050527SSuraj Jitindar Singh } 25577d050527SSuraj Jitindar Singh 25587d050527SSuraj Jitindar Singh return 0; 25597d050527SSuraj Jitindar Singh } 25607d050527SSuraj Jitindar Singh 2561*82123b75SBharata B Rao int kvmppc_has_cap_rpt_invalidate(void) 2562*82123b75SBharata B Rao { 2563*82123b75SBharata B Rao return cap_rpt_invalidate; 2564*82123b75SBharata B Rao } 2565*82123b75SBharata B Rao 256652b2519cSThomas Huth PowerPCCPUClass *kvm_ppc_get_host_cpu_class(void) 256752b2519cSThomas Huth { 256852b2519cSThomas Huth uint32_t host_pvr = mfpvr(); 256952b2519cSThomas Huth PowerPCCPUClass *pvr_pcc; 257052b2519cSThomas Huth 257152b2519cSThomas Huth pvr_pcc = ppc_cpu_class_by_pvr(host_pvr); 257252b2519cSThomas Huth if (pvr_pcc == NULL) { 257352b2519cSThomas Huth pvr_pcc = ppc_cpu_class_by_pvr_mask(host_pvr); 257452b2519cSThomas Huth } 257552b2519cSThomas Huth 257652b2519cSThomas Huth return pvr_pcc; 257752b2519cSThomas Huth } 257852b2519cSThomas Huth 2579165dc3edSDavid Gibson static void pseries_machine_class_fixup(ObjectClass *oc, void *opaque) 2580165dc3edSDavid Gibson { 2581165dc3edSDavid Gibson MachineClass *mc = MACHINE_CLASS(oc); 2582165dc3edSDavid Gibson 2583165dc3edSDavid Gibson mc->default_cpu_type = TYPE_HOST_POWERPC_CPU; 2584165dc3edSDavid Gibson } 2585165dc3edSDavid Gibson 2586165dc3edSDavid Gibson static int kvm_ppc_register_host_cpu_type(void) 25875ba4576bSAndreas Färber { 25885ba4576bSAndreas Färber TypeInfo type_info = { 25895ba4576bSAndreas Färber .name = TYPE_HOST_POWERPC_CPU, 25905ba4576bSAndreas Färber .class_init = kvmppc_host_cpu_class_init, 25915ba4576bSAndreas Färber }; 25925ba4576bSAndreas Färber PowerPCCPUClass *pvr_pcc; 259392e926e1SGreg Kurz ObjectClass *oc; 25945b79b1caSAlexey Kardashevskiy DeviceClass *dc; 2595715d4b96SThomas Huth int i; 25965ba4576bSAndreas Färber 259752b2519cSThomas Huth pvr_pcc = kvm_ppc_get_host_cpu_class(); 25983bc9ccc0SAlexey Kardashevskiy if (pvr_pcc == NULL) { 25995ba4576bSAndreas Färber return -1; 26005ba4576bSAndreas Färber } 26015ba4576bSAndreas Färber type_info.parent = object_class_get_name(OBJECT_CLASS(pvr_pcc)); 26025ba4576bSAndreas Färber type_register(&type_info); 26032e9c10ebSIgor Mammedov /* override TCG default cpu type with 'host' cpu model */ 2604165dc3edSDavid Gibson object_class_foreach(pseries_machine_class_fixup, TYPE_SPAPR_MACHINE, 2605165dc3edSDavid Gibson false, NULL); 26065b79b1caSAlexey Kardashevskiy 260792e926e1SGreg Kurz oc = object_class_by_name(type_info.name); 260892e926e1SGreg Kurz g_assert(oc); 260992e926e1SGreg Kurz 2610715d4b96SThomas Huth /* 2611715d4b96SThomas Huth * Update generic CPU family class alias (e.g. on a POWER8NVL host, 2612715d4b96SThomas Huth * we want "POWER8" to be a "family" alias that points to the current 2613715d4b96SThomas Huth * host CPU type, too) 2614715d4b96SThomas Huth */ 2615715d4b96SThomas Huth dc = DEVICE_CLASS(ppc_cpu_get_family_class(pvr_pcc)); 2616715d4b96SThomas Huth for (i = 0; ppc_cpu_aliases[i].alias != NULL; i++) { 2617c5354f54SIgor Mammedov if (strcasecmp(ppc_cpu_aliases[i].alias, dc->desc) == 0) { 2618715d4b96SThomas Huth char *suffix; 2619715d4b96SThomas Huth 2620715d4b96SThomas Huth ppc_cpu_aliases[i].model = g_strdup(object_class_get_name(oc)); 2621c9137065SIgor Mammedov suffix = strstr(ppc_cpu_aliases[i].model, POWERPC_CPU_TYPE_SUFFIX); 2622715d4b96SThomas Huth if (suffix) { 2623715d4b96SThomas Huth *suffix = 0; 2624715d4b96SThomas Huth } 2625715d4b96SThomas Huth break; 2626715d4b96SThomas Huth } 2627715d4b96SThomas Huth } 2628715d4b96SThomas Huth 26295ba4576bSAndreas Färber return 0; 26305ba4576bSAndreas Färber } 26315ba4576bSAndreas Färber 2632feaa64c4SDavid Gibson int kvmppc_define_rtas_kernel_token(uint32_t token, const char *function) 2633feaa64c4SDavid Gibson { 2634feaa64c4SDavid Gibson struct kvm_rtas_token_args args = { 2635feaa64c4SDavid Gibson .token = token, 2636feaa64c4SDavid Gibson }; 2637feaa64c4SDavid Gibson 2638feaa64c4SDavid Gibson if (!kvm_check_extension(kvm_state, KVM_CAP_PPC_RTAS)) { 2639feaa64c4SDavid Gibson return -ENOENT; 2640feaa64c4SDavid Gibson } 2641feaa64c4SDavid Gibson 26427701aeedSCédric Le Goater strncpy(args.name, function, sizeof(args.name) - 1); 2643feaa64c4SDavid Gibson 2644feaa64c4SDavid Gibson return kvm_vm_ioctl(kvm_state, KVM_PPC_RTAS_DEFINE_TOKEN, &args); 2645feaa64c4SDavid Gibson } 264612b1143bSDavid Gibson 264714b0d748SGreg Kurz int kvmppc_get_htab_fd(bool write, uint64_t index, Error **errp) 2648e68cb8b4SAlexey Kardashevskiy { 2649e68cb8b4SAlexey Kardashevskiy struct kvm_get_htab_fd s = { 2650e68cb8b4SAlexey Kardashevskiy .flags = write ? KVM_GET_HTAB_WRITE : 0, 265114b0d748SGreg Kurz .start_index = index, 2652e68cb8b4SAlexey Kardashevskiy }; 265382be8e73SGreg Kurz int ret; 2654e68cb8b4SAlexey Kardashevskiy 2655e68cb8b4SAlexey Kardashevskiy if (!cap_htab_fd) { 265614b0d748SGreg Kurz error_setg(errp, "KVM version doesn't support %s the HPT", 265714b0d748SGreg Kurz write ? "writing" : "reading"); 265882be8e73SGreg Kurz return -ENOTSUP; 2659e68cb8b4SAlexey Kardashevskiy } 2660e68cb8b4SAlexey Kardashevskiy 266182be8e73SGreg Kurz ret = kvm_vm_ioctl(kvm_state, KVM_PPC_GET_HTAB_FD, &s); 266282be8e73SGreg Kurz if (ret < 0) { 266314b0d748SGreg Kurz error_setg(errp, "Unable to open fd for %s HPT %s KVM: %s", 266414b0d748SGreg Kurz write ? "writing" : "reading", write ? "to" : "from", 266514b0d748SGreg Kurz strerror(errno)); 266682be8e73SGreg Kurz return -errno; 266782be8e73SGreg Kurz } 266882be8e73SGreg Kurz 266982be8e73SGreg Kurz return ret; 2670e68cb8b4SAlexey Kardashevskiy } 2671e68cb8b4SAlexey Kardashevskiy 2672e68cb8b4SAlexey Kardashevskiy int kvmppc_save_htab(QEMUFile *f, int fd, size_t bufsize, int64_t max_ns) 2673e68cb8b4SAlexey Kardashevskiy { 2674bc72ad67SAlex Bligh int64_t starttime = qemu_clock_get_ns(QEMU_CLOCK_REALTIME); 2675e68cb8b4SAlexey Kardashevskiy uint8_t buf[bufsize]; 2676e68cb8b4SAlexey Kardashevskiy ssize_t rc; 2677e68cb8b4SAlexey Kardashevskiy 2678e68cb8b4SAlexey Kardashevskiy do { 2679e68cb8b4SAlexey Kardashevskiy rc = read(fd, buf, bufsize); 2680e68cb8b4SAlexey Kardashevskiy if (rc < 0) { 2681e68cb8b4SAlexey Kardashevskiy fprintf(stderr, "Error reading data from KVM HTAB fd: %s\n", 2682e68cb8b4SAlexey Kardashevskiy strerror(errno)); 2683e68cb8b4SAlexey Kardashevskiy return rc; 2684e68cb8b4SAlexey Kardashevskiy } else if (rc) { 2685e094c4c1SCédric Le Goater uint8_t *buffer = buf; 2686e094c4c1SCédric Le Goater ssize_t n = rc; 2687e094c4c1SCédric Le Goater while (n) { 2688e094c4c1SCédric Le Goater struct kvm_get_htab_header *head = 2689e094c4c1SCédric Le Goater (struct kvm_get_htab_header *) buffer; 2690e094c4c1SCédric Le Goater size_t chunksize = sizeof(*head) + 2691e094c4c1SCédric Le Goater HASH_PTE_SIZE_64 * head->n_valid; 2692e094c4c1SCédric Le Goater 2693e094c4c1SCédric Le Goater qemu_put_be32(f, head->index); 2694e094c4c1SCédric Le Goater qemu_put_be16(f, head->n_valid); 2695e094c4c1SCédric Le Goater qemu_put_be16(f, head->n_invalid); 2696e094c4c1SCédric Le Goater qemu_put_buffer(f, (void *)(head + 1), 2697e094c4c1SCédric Le Goater HASH_PTE_SIZE_64 * head->n_valid); 2698e094c4c1SCédric Le Goater 2699e094c4c1SCédric Le Goater buffer += chunksize; 2700e094c4c1SCédric Le Goater n -= chunksize; 2701e094c4c1SCédric Le Goater } 2702e68cb8b4SAlexey Kardashevskiy } 2703e68cb8b4SAlexey Kardashevskiy } while ((rc != 0) 2704c995e942SDavid Gibson && ((max_ns < 0) || 2705c995e942SDavid Gibson ((qemu_clock_get_ns(QEMU_CLOCK_REALTIME) - starttime) < max_ns))); 2706e68cb8b4SAlexey Kardashevskiy 2707e68cb8b4SAlexey Kardashevskiy return (rc == 0) ? 1 : 0; 2708e68cb8b4SAlexey Kardashevskiy } 2709e68cb8b4SAlexey Kardashevskiy 2710e68cb8b4SAlexey Kardashevskiy int kvmppc_load_htab_chunk(QEMUFile *f, int fd, uint32_t index, 27110a06e4d6SGreg Kurz uint16_t n_valid, uint16_t n_invalid, Error **errp) 2712e68cb8b4SAlexey Kardashevskiy { 2713e68cb8b4SAlexey Kardashevskiy struct kvm_get_htab_header *buf; 2714e68cb8b4SAlexey Kardashevskiy size_t chunksize = sizeof(*buf) + n_valid * HASH_PTE_SIZE_64; 2715e68cb8b4SAlexey Kardashevskiy ssize_t rc; 2716e68cb8b4SAlexey Kardashevskiy 2717e68cb8b4SAlexey Kardashevskiy buf = alloca(chunksize); 2718e68cb8b4SAlexey Kardashevskiy buf->index = index; 2719e68cb8b4SAlexey Kardashevskiy buf->n_valid = n_valid; 2720e68cb8b4SAlexey Kardashevskiy buf->n_invalid = n_invalid; 2721e68cb8b4SAlexey Kardashevskiy 2722e68cb8b4SAlexey Kardashevskiy qemu_get_buffer(f, (void *)(buf + 1), HASH_PTE_SIZE_64 * n_valid); 2723e68cb8b4SAlexey Kardashevskiy 2724e68cb8b4SAlexey Kardashevskiy rc = write(fd, buf, chunksize); 2725e68cb8b4SAlexey Kardashevskiy if (rc < 0) { 27260a06e4d6SGreg Kurz error_setg_errno(errp, errno, "Error writing the KVM hash table"); 27270a06e4d6SGreg Kurz return -errno; 2728e68cb8b4SAlexey Kardashevskiy } 2729e68cb8b4SAlexey Kardashevskiy if (rc != chunksize) { 2730e68cb8b4SAlexey Kardashevskiy /* We should never get a short write on a single chunk */ 27310a06e4d6SGreg Kurz error_setg(errp, "Short write while restoring the KVM hash table"); 27320a06e4d6SGreg Kurz return -ENOSPC; 2733e68cb8b4SAlexey Kardashevskiy } 2734e68cb8b4SAlexey Kardashevskiy return 0; 2735e68cb8b4SAlexey Kardashevskiy } 2736e68cb8b4SAlexey Kardashevskiy 273720d695a9SAndreas Färber bool kvm_arch_stop_on_emulation_error(CPUState *cpu) 27384513d923SGleb Natapov { 27394513d923SGleb Natapov return true; 27404513d923SGleb Natapov } 2741a1b87fe0SJan Kiszka 274282169660SScott Wood void kvm_arch_init_irq_routing(KVMState *s) 274382169660SScott Wood { 274482169660SScott Wood } 2745c65f9a07SGreg Kurz 27461ad9f0a4SDavid Gibson void kvmppc_read_hptes(ppc_hash_pte64_t *hptes, hwaddr ptex, int n) 27471ad9f0a4SDavid Gibson { 27481ad9f0a4SDavid Gibson int fd, rc; 27491ad9f0a4SDavid Gibson int i; 27507c43bca0SAneesh Kumar K.V 275114b0d748SGreg Kurz fd = kvmppc_get_htab_fd(false, ptex, &error_abort); 27521ad9f0a4SDavid Gibson 27531ad9f0a4SDavid Gibson i = 0; 27541ad9f0a4SDavid Gibson while (i < n) { 27551ad9f0a4SDavid Gibson struct kvm_get_htab_header *hdr; 27561ad9f0a4SDavid Gibson int m = n < HPTES_PER_GROUP ? n : HPTES_PER_GROUP; 27571ad9f0a4SDavid Gibson char buf[sizeof(*hdr) + m * HASH_PTE_SIZE_64]; 27581ad9f0a4SDavid Gibson 27591ad9f0a4SDavid Gibson rc = read(fd, buf, sizeof(buf)); 27601ad9f0a4SDavid Gibson if (rc < 0) { 27611ad9f0a4SDavid Gibson hw_error("kvmppc_read_hptes: Unable to read HPTEs"); 27621ad9f0a4SDavid Gibson } 27631ad9f0a4SDavid Gibson 27641ad9f0a4SDavid Gibson hdr = (struct kvm_get_htab_header *)buf; 27651ad9f0a4SDavid Gibson while ((i < n) && ((char *)hdr < (buf + rc))) { 2766a36593e1SAlexey Kardashevskiy int invalid = hdr->n_invalid, valid = hdr->n_valid; 27671ad9f0a4SDavid Gibson 27681ad9f0a4SDavid Gibson if (hdr->index != (ptex + i)) { 27691ad9f0a4SDavid Gibson hw_error("kvmppc_read_hptes: Unexpected HPTE index %"PRIu32 27701ad9f0a4SDavid Gibson " != (%"HWADDR_PRIu" + %d", hdr->index, ptex, i); 27711ad9f0a4SDavid Gibson } 27721ad9f0a4SDavid Gibson 2773a36593e1SAlexey Kardashevskiy if (n - i < valid) { 2774a36593e1SAlexey Kardashevskiy valid = n - i; 2775a36593e1SAlexey Kardashevskiy } 2776a36593e1SAlexey Kardashevskiy memcpy(hptes + i, hdr + 1, HASH_PTE_SIZE_64 * valid); 2777a36593e1SAlexey Kardashevskiy i += valid; 27781ad9f0a4SDavid Gibson 27791ad9f0a4SDavid Gibson if ((n - i) < invalid) { 27801ad9f0a4SDavid Gibson invalid = n - i; 27811ad9f0a4SDavid Gibson } 27821ad9f0a4SDavid Gibson memset(hptes + i, 0, invalid * HASH_PTE_SIZE_64); 2783a36593e1SAlexey Kardashevskiy i += invalid; 27841ad9f0a4SDavid Gibson 27851ad9f0a4SDavid Gibson hdr = (struct kvm_get_htab_header *) 27861ad9f0a4SDavid Gibson ((char *)(hdr + 1) + HASH_PTE_SIZE_64 * hdr->n_valid); 27871ad9f0a4SDavid Gibson } 27881ad9f0a4SDavid Gibson } 27891ad9f0a4SDavid Gibson 27901ad9f0a4SDavid Gibson close(fd); 27911ad9f0a4SDavid Gibson } 27921ad9f0a4SDavid Gibson 27931ad9f0a4SDavid Gibson void kvmppc_write_hpte(hwaddr ptex, uint64_t pte0, uint64_t pte1) 27947c43bca0SAneesh Kumar K.V { 27951ad9f0a4SDavid Gibson int fd, rc; 27961ad9f0a4SDavid Gibson struct { 27971ad9f0a4SDavid Gibson struct kvm_get_htab_header hdr; 27981ad9f0a4SDavid Gibson uint64_t pte0; 27991ad9f0a4SDavid Gibson uint64_t pte1; 28001ad9f0a4SDavid Gibson } buf; 2801c1385933SAneesh Kumar K.V 280214b0d748SGreg Kurz fd = kvmppc_get_htab_fd(true, 0 /* Ignored */, &error_abort); 2803c1385933SAneesh Kumar K.V 28041ad9f0a4SDavid Gibson buf.hdr.n_valid = 1; 28051ad9f0a4SDavid Gibson buf.hdr.n_invalid = 0; 28061ad9f0a4SDavid Gibson buf.hdr.index = ptex; 28071ad9f0a4SDavid Gibson buf.pte0 = cpu_to_be64(pte0); 28081ad9f0a4SDavid Gibson buf.pte1 = cpu_to_be64(pte1); 28091ad9f0a4SDavid Gibson 28101ad9f0a4SDavid Gibson rc = write(fd, &buf, sizeof(buf)); 28111ad9f0a4SDavid Gibson if (rc != sizeof(buf)) { 28121ad9f0a4SDavid Gibson hw_error("kvmppc_write_hpte: Unable to update KVM HPT"); 2813c1385933SAneesh Kumar K.V } 28141ad9f0a4SDavid Gibson close(fd); 2815c1385933SAneesh Kumar K.V } 28169e03a040SFrank Blaschka 28179e03a040SFrank Blaschka int kvm_arch_fixup_msi_route(struct kvm_irq_routing_entry *route, 2818dc9f06caSPavel Fedin uint64_t address, uint32_t data, PCIDevice *dev) 28199e03a040SFrank Blaschka { 28209e03a040SFrank Blaschka return 0; 28219e03a040SFrank Blaschka } 28221850b6b7SEric Auger 282338d87493SPeter Xu int kvm_arch_add_msi_route_post(struct kvm_irq_routing_entry *route, 282438d87493SPeter Xu int vector, PCIDevice *dev) 282538d87493SPeter Xu { 282638d87493SPeter Xu return 0; 282738d87493SPeter Xu } 282838d87493SPeter Xu 282938d87493SPeter Xu int kvm_arch_release_virq_post(int virq) 283038d87493SPeter Xu { 283138d87493SPeter Xu return 0; 283238d87493SPeter Xu } 283338d87493SPeter Xu 28341850b6b7SEric Auger int kvm_arch_msi_data_to_gsi(uint32_t data) 28351850b6b7SEric Auger { 28361850b6b7SEric Auger return data & 0xffff; 28371850b6b7SEric Auger } 28384d9392beSThomas Huth 28399ac703acSAravinda Prasad #if defined(TARGET_PPC64) 28409ac703acSAravinda Prasad int kvm_handle_nmi(PowerPCCPU *cpu, struct kvm_run *run) 28419ac703acSAravinda Prasad { 2842211a7784SGanesh Goudar uint16_t flags = run->flags & KVM_RUN_PPC_NMI_DISP_MASK; 284381fe70e4SAravinda Prasad 28449ac703acSAravinda Prasad cpu_synchronize_state(CPU(cpu)); 28459ac703acSAravinda Prasad 2846211a7784SGanesh Goudar spapr_mce_req_event(cpu, flags == KVM_RUN_PPC_NMI_DISP_FULLY_RECOV); 28479ac703acSAravinda Prasad 28489ac703acSAravinda Prasad return 0; 28499ac703acSAravinda Prasad } 28509ac703acSAravinda Prasad #endif 28519ac703acSAravinda Prasad 28524d9392beSThomas Huth int kvmppc_enable_hwrng(void) 28534d9392beSThomas Huth { 28544d9392beSThomas Huth if (!kvm_enabled() || !kvm_check_extension(kvm_state, KVM_CAP_PPC_HWRNG)) { 28554d9392beSThomas Huth return -1; 28564d9392beSThomas Huth } 28574d9392beSThomas Huth 28584d9392beSThomas Huth return kvmppc_enable_hcall(kvm_state, H_RANDOM); 28594d9392beSThomas Huth } 286030f4b05bSDavid Gibson 286130f4b05bSDavid Gibson void kvmppc_check_papr_resize_hpt(Error **errp) 286230f4b05bSDavid Gibson { 286330f4b05bSDavid Gibson if (!kvm_enabled()) { 2864b55d295eSDavid Gibson return; /* No KVM, we're good */ 2865b55d295eSDavid Gibson } 2866b55d295eSDavid Gibson 2867b55d295eSDavid Gibson if (cap_resize_hpt) { 2868b55d295eSDavid Gibson return; /* Kernel has explicit support, we're good */ 2869b55d295eSDavid Gibson } 2870b55d295eSDavid Gibson 2871b55d295eSDavid Gibson /* Otherwise fallback on looking for PR KVM */ 2872b55d295eSDavid Gibson if (kvmppc_is_pr(kvm_state)) { 287330f4b05bSDavid Gibson return; 287430f4b05bSDavid Gibson } 287530f4b05bSDavid Gibson 287630f4b05bSDavid Gibson error_setg(errp, 287730f4b05bSDavid Gibson "Hash page table resizing not available with this KVM version"); 287830f4b05bSDavid Gibson } 2879b55d295eSDavid Gibson 2880b55d295eSDavid Gibson int kvmppc_resize_hpt_prepare(PowerPCCPU *cpu, target_ulong flags, int shift) 2881b55d295eSDavid Gibson { 2882b55d295eSDavid Gibson CPUState *cs = CPU(cpu); 2883b55d295eSDavid Gibson struct kvm_ppc_resize_hpt rhpt = { 2884b55d295eSDavid Gibson .flags = flags, 2885b55d295eSDavid Gibson .shift = shift, 2886b55d295eSDavid Gibson }; 2887b55d295eSDavid Gibson 2888b55d295eSDavid Gibson if (!cap_resize_hpt) { 2889b55d295eSDavid Gibson return -ENOSYS; 2890b55d295eSDavid Gibson } 2891b55d295eSDavid Gibson 2892b55d295eSDavid Gibson return kvm_vm_ioctl(cs->kvm_state, KVM_PPC_RESIZE_HPT_PREPARE, &rhpt); 2893b55d295eSDavid Gibson } 2894b55d295eSDavid Gibson 2895b55d295eSDavid Gibson int kvmppc_resize_hpt_commit(PowerPCCPU *cpu, target_ulong flags, int shift) 2896b55d295eSDavid Gibson { 2897b55d295eSDavid Gibson CPUState *cs = CPU(cpu); 2898b55d295eSDavid Gibson struct kvm_ppc_resize_hpt rhpt = { 2899b55d295eSDavid Gibson .flags = flags, 2900b55d295eSDavid Gibson .shift = shift, 2901b55d295eSDavid Gibson }; 2902b55d295eSDavid Gibson 2903b55d295eSDavid Gibson if (!cap_resize_hpt) { 2904b55d295eSDavid Gibson return -ENOSYS; 2905b55d295eSDavid Gibson } 2906b55d295eSDavid Gibson 2907b55d295eSDavid Gibson return kvm_vm_ioctl(cs->kvm_state, KVM_PPC_RESIZE_HPT_COMMIT, &rhpt); 2908b55d295eSDavid Gibson } 2909b55d295eSDavid Gibson 2910c363a37aSDaniel Henrique Barboza /* 2911c363a37aSDaniel Henrique Barboza * This is a helper function to detect a post migration scenario 2912c363a37aSDaniel Henrique Barboza * in which a guest, running as KVM-HV, freezes in cpu_post_load because 2913c363a37aSDaniel Henrique Barboza * the guest kernel can't handle a PVR value other than the actual host 2914c363a37aSDaniel Henrique Barboza * PVR in KVM_SET_SREGS, even if pvr_match() returns true. 2915c363a37aSDaniel Henrique Barboza * 2916c363a37aSDaniel Henrique Barboza * If we don't have cap_ppc_pvr_compat and we're not running in PR 2917c363a37aSDaniel Henrique Barboza * (so, we're HV), return true. The workaround itself is done in 2918c363a37aSDaniel Henrique Barboza * cpu_post_load. 2919c363a37aSDaniel Henrique Barboza * 2920c363a37aSDaniel Henrique Barboza * The order here is important: we'll only check for KVM PR as a 2921c363a37aSDaniel Henrique Barboza * fallback if the guest kernel can't handle the situation itself. 2922c363a37aSDaniel Henrique Barboza * We need to avoid as much as possible querying the running KVM type 2923c363a37aSDaniel Henrique Barboza * in QEMU level. 2924c363a37aSDaniel Henrique Barboza */ 2925c363a37aSDaniel Henrique Barboza bool kvmppc_pvr_workaround_required(PowerPCCPU *cpu) 2926c363a37aSDaniel Henrique Barboza { 2927c363a37aSDaniel Henrique Barboza CPUState *cs = CPU(cpu); 2928c363a37aSDaniel Henrique Barboza 2929c363a37aSDaniel Henrique Barboza if (!kvm_enabled()) { 2930c363a37aSDaniel Henrique Barboza return false; 2931c363a37aSDaniel Henrique Barboza } 2932c363a37aSDaniel Henrique Barboza 2933c363a37aSDaniel Henrique Barboza if (cap_ppc_pvr_compat) { 2934c363a37aSDaniel Henrique Barboza return false; 2935c363a37aSDaniel Henrique Barboza } 2936c363a37aSDaniel Henrique Barboza 2937c363a37aSDaniel Henrique Barboza return !kvmppc_is_pr(cs->kvm_state); 2938c363a37aSDaniel Henrique Barboza } 2939a84f7179SNikunj A Dadhania 2940a84f7179SNikunj A Dadhania void kvmppc_set_reg_ppc_online(PowerPCCPU *cpu, unsigned int online) 2941a84f7179SNikunj A Dadhania { 2942a84f7179SNikunj A Dadhania CPUState *cs = CPU(cpu); 2943a84f7179SNikunj A Dadhania 2944a84f7179SNikunj A Dadhania if (kvm_enabled()) { 2945a84f7179SNikunj A Dadhania kvm_set_one_reg(cs, KVM_REG_PPC_ONLINE, &online); 2946a84f7179SNikunj A Dadhania } 2947a84f7179SNikunj A Dadhania } 29489723295aSGreg Kurz 29499723295aSGreg Kurz void kvmppc_set_reg_tb_offset(PowerPCCPU *cpu, int64_t tb_offset) 29509723295aSGreg Kurz { 29519723295aSGreg Kurz CPUState *cs = CPU(cpu); 29529723295aSGreg Kurz 29539723295aSGreg Kurz if (kvm_enabled()) { 29549723295aSGreg Kurz kvm_set_one_reg(cs, KVM_REG_PPC_TB_OFFSET, &tb_offset); 29559723295aSGreg Kurz } 29569723295aSGreg Kurz } 295792a5199bSTom Lendacky 295892a5199bSTom Lendacky bool kvm_arch_cpu_check_are_resettable(void) 295992a5199bSTom Lendacky { 296092a5199bSTom Lendacky return true; 296192a5199bSTom Lendacky } 2962