xref: /qemu/target/ppc/kvm.c (revision ca77ee28e03fb5052be01b9237b0a7cef5d90d4a)
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"
309c17d615SPaolo Bonzini #include "sysemu/sysemu.h"
31b3946626SVincent Palatin #include "sysemu/hw_accel.h"
32d76d1650Saurel32 #include "kvm_ppc.h"
339c17d615SPaolo Bonzini #include "sysemu/cpus.h"
349c17d615SPaolo Bonzini #include "sysemu/device_tree.h"
35d5aea6f3SDavid Gibson #include "mmu-hash64.h"
36d76d1650Saurel32 
37f61b4bedSAlexander Graf #include "hw/sysbus.h"
380d09e41aSPaolo Bonzini #include "hw/ppc/spapr.h"
397ebaf795SBharata B Rao #include "hw/ppc/spapr_cpu_core.h"
4098a8b524SAlexey Kardashevskiy #include "hw/ppc/ppc.h"
41*ca77ee28SMarkus 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"
499c607668SAlexey Kardashevskiy #include "qemu/mmap-alloc.h"
50f3d9f303SSam Bobroff #include "elf.h"
51c64abd1fSSam Bobroff #include "sysemu/kvm_int.h"
52f61b4bedSAlexander Graf 
53eadaada1SAlexander Graf #define PROC_DEVTREE_CPU      "/proc/device-tree/cpus/"
54eadaada1SAlexander Graf 
5594a8d39aSJan Kiszka const KVMCapabilityInfo kvm_arch_required_capabilities[] = {
5694a8d39aSJan Kiszka     KVM_CAP_LAST_INFO
5794a8d39aSJan Kiszka };
5894a8d39aSJan Kiszka 
59c995e942SDavid Gibson static int cap_interrupt_unset;
60c995e942SDavid Gibson static int cap_interrupt_level;
6190dc8812SScott Wood static int cap_segstate;
6290dc8812SScott Wood static int cap_booke_sregs;
63e97c3636SDavid Gibson static int cap_ppc_smt;
64fa98fbfcSSam Bobroff static int cap_ppc_smt_possible;
650f5cb298SDavid Gibson static int cap_spapr_tce;
66d6ee2a7cSAlexey Kardashevskiy static int cap_spapr_tce_64;
67da95324eSAlexey Kardashevskiy static int cap_spapr_multitce;
689bb62a07SAlexey Kardashevskiy static int cap_spapr_vfio;
69f1af19d7SDavid Gibson static int cap_hior;
70d67d40eaSDavid Gibson static int cap_one_reg;
713b961124SStuart Yoder static int cap_epr;
7231f2cb8fSBharat Bhushan static int cap_ppc_watchdog;
739b00ea49SDavid Gibson static int cap_papr;
74e68cb8b4SAlexey Kardashevskiy static int cap_htab_fd;
7587a91de6SAlexander Graf static int cap_fixup_hcalls;
76bac3bf28SThomas Huth static int cap_htm;             /* Hardware transactional memory support */
77cf1c4cceSSam Bobroff static int cap_mmu_radix;
78cf1c4cceSSam Bobroff static int cap_mmu_hash_v3;
7938afd772SCédric Le Goater static int cap_xive;
80b55d295eSDavid Gibson static int cap_resize_hpt;
81c363a37aSDaniel Henrique Barboza static int cap_ppc_pvr_compat;
828acc2ae5SSuraj Jitindar Singh static int cap_ppc_safe_cache;
838acc2ae5SSuraj Jitindar Singh static int cap_ppc_safe_bounds_check;
848acc2ae5SSuraj Jitindar Singh static int cap_ppc_safe_indirect_branch;
858ff43ee4SSuraj Jitindar Singh static int cap_ppc_count_cache_flush_assist;
86b9a477b7SSuraj Jitindar Singh static int cap_ppc_nested_kvm_hv;
877d050527SSuraj Jitindar Singh static int cap_large_decr;
88fc87e185SAlexander Graf 
893c902d44SBharat Bhushan static uint32_t debug_inst_opcode;
903c902d44SBharat Bhushan 
91c995e942SDavid Gibson /*
92c995e942SDavid Gibson  * XXX We have a race condition where we actually have a level triggered
93c821c2bdSAlexander Graf  *     interrupt, but the infrastructure can't expose that yet, so the guest
94c821c2bdSAlexander Graf  *     takes but ignores it, goes to sleep and never gets notified that there's
95c821c2bdSAlexander Graf  *     still an interrupt pending.
96c6a94ba5SAlexander Graf  *
97c821c2bdSAlexander Graf  *     As a quick workaround, let's just wake up again 20 ms after we injected
98c821c2bdSAlexander Graf  *     an interrupt. That way we can assure that we're always reinjecting
99c821c2bdSAlexander Graf  *     interrupts in case the guest swallowed them.
100c6a94ba5SAlexander Graf  */
101c6a94ba5SAlexander Graf static QEMUTimer *idle_timer;
102c6a94ba5SAlexander Graf 
103d5a68146SAndreas Färber static void kvm_kick_cpu(void *opaque)
104c6a94ba5SAlexander Graf {
105d5a68146SAndreas Färber     PowerPCCPU *cpu = opaque;
106d5a68146SAndreas Färber 
107c08d7424SAndreas Färber     qemu_cpu_kick(CPU(cpu));
108c6a94ba5SAlexander Graf }
109c6a94ba5SAlexander Graf 
110c995e942SDavid Gibson /*
111c995e942SDavid Gibson  * Check whether we are running with KVM-PR (instead of KVM-HV).  This
11296c9cff0SThomas Huth  * should only be used for fallback tests - generally we should use
11396c9cff0SThomas Huth  * explicit capabilities for the features we want, rather than
114c995e942SDavid Gibson  * assuming what is/isn't available depending on the KVM variant.
115c995e942SDavid Gibson  */
11696c9cff0SThomas Huth static bool kvmppc_is_pr(KVMState *ks)
11796c9cff0SThomas Huth {
11896c9cff0SThomas Huth     /* Assume KVM-PR if the GET_PVINFO capability is available */
11970a0c19eSGreg Kurz     return kvm_vm_check_extension(ks, KVM_CAP_PPC_GET_PVINFO) != 0;
12096c9cff0SThomas Huth }
12196c9cff0SThomas Huth 
1222e9c10ebSIgor Mammedov static int kvm_ppc_register_host_cpu_type(MachineState *ms);
1238acc2ae5SSuraj Jitindar Singh static void kvmppc_get_cpu_characteristics(KVMState *s);
1247d050527SSuraj Jitindar Singh static int kvmppc_get_dec_bits(void);
1255ba4576bSAndreas Färber 
126b16565b3SMarcel Apfelbaum int kvm_arch_init(MachineState *ms, KVMState *s)
127d76d1650Saurel32 {
128fc87e185SAlexander Graf     cap_interrupt_unset = kvm_check_extension(s, KVM_CAP_PPC_UNSET_IRQ);
129fc87e185SAlexander Graf     cap_interrupt_level = kvm_check_extension(s, KVM_CAP_PPC_IRQ_LEVEL);
13090dc8812SScott Wood     cap_segstate = kvm_check_extension(s, KVM_CAP_PPC_SEGSTATE);
13190dc8812SScott Wood     cap_booke_sregs = kvm_check_extension(s, KVM_CAP_PPC_BOOKE_SREGS);
1326977afdaSGreg Kurz     cap_ppc_smt_possible = kvm_vm_check_extension(s, KVM_CAP_PPC_SMT_POSSIBLE);
1330f5cb298SDavid Gibson     cap_spapr_tce = kvm_check_extension(s, KVM_CAP_SPAPR_TCE);
134d6ee2a7cSAlexey Kardashevskiy     cap_spapr_tce_64 = kvm_check_extension(s, KVM_CAP_SPAPR_TCE_64);
135da95324eSAlexey Kardashevskiy     cap_spapr_multitce = kvm_check_extension(s, KVM_CAP_SPAPR_MULTITCE);
1369ded780cSAlexey Kardashevskiy     cap_spapr_vfio = kvm_vm_check_extension(s, KVM_CAP_SPAPR_TCE_VFIO);
137d67d40eaSDavid Gibson     cap_one_reg = kvm_check_extension(s, KVM_CAP_ONE_REG);
138f1af19d7SDavid Gibson     cap_hior = kvm_check_extension(s, KVM_CAP_PPC_HIOR);
1393b961124SStuart Yoder     cap_epr = kvm_check_extension(s, KVM_CAP_PPC_EPR);
14031f2cb8fSBharat Bhushan     cap_ppc_watchdog = kvm_check_extension(s, KVM_CAP_PPC_BOOKE_WATCHDOG);
141c995e942SDavid Gibson     /*
142c995e942SDavid Gibson      * Note: we don't set cap_papr here, because this capability is
143c995e942SDavid Gibson      * only activated after this by kvmppc_set_papr()
144c995e942SDavid Gibson      */
1456977afdaSGreg Kurz     cap_htab_fd = kvm_vm_check_extension(s, KVM_CAP_PPC_HTAB_FD);
14687a91de6SAlexander Graf     cap_fixup_hcalls = kvm_check_extension(s, KVM_CAP_PPC_FIXUP_HCALL);
147fa98fbfcSSam Bobroff     cap_ppc_smt = kvm_vm_check_extension(s, KVM_CAP_PPC_SMT);
148bac3bf28SThomas Huth     cap_htm = kvm_vm_check_extension(s, KVM_CAP_PPC_HTM);
149cf1c4cceSSam Bobroff     cap_mmu_radix = kvm_vm_check_extension(s, KVM_CAP_PPC_MMU_RADIX);
150cf1c4cceSSam Bobroff     cap_mmu_hash_v3 = kvm_vm_check_extension(s, KVM_CAP_PPC_MMU_HASH_V3);
15138afd772SCédric Le Goater     cap_xive = kvm_vm_check_extension(s, KVM_CAP_PPC_IRQ_XIVE);
152b55d295eSDavid Gibson     cap_resize_hpt = kvm_vm_check_extension(s, KVM_CAP_SPAPR_RESIZE_HPT);
1538acc2ae5SSuraj Jitindar Singh     kvmppc_get_cpu_characteristics(s);
154b9a477b7SSuraj Jitindar Singh     cap_ppc_nested_kvm_hv = kvm_vm_check_extension(s, KVM_CAP_PPC_NESTED_HV);
1557d050527SSuraj Jitindar Singh     cap_large_decr = kvmppc_get_dec_bits();
156c363a37aSDaniel Henrique Barboza     /*
157c363a37aSDaniel Henrique Barboza      * Note: setting it to false because there is not such capability
158c363a37aSDaniel Henrique Barboza      * in KVM at this moment.
159c363a37aSDaniel Henrique Barboza      *
160c363a37aSDaniel Henrique Barboza      * TODO: call kvm_vm_check_extension() with the right capability
161c995e942SDavid Gibson      * after the kernel starts implementing it.
162c995e942SDavid Gibson      */
163c363a37aSDaniel Henrique Barboza     cap_ppc_pvr_compat = false;
164fc87e185SAlexander Graf 
165fc87e185SAlexander Graf     if (!cap_interrupt_level) {
166fc87e185SAlexander Graf         fprintf(stderr, "KVM: Couldn't find level irq capability. Expect the "
167fc87e185SAlexander Graf                         "VM to stall at times!\n");
168fc87e185SAlexander Graf     }
169fc87e185SAlexander Graf 
1702e9c10ebSIgor Mammedov     kvm_ppc_register_host_cpu_type(ms);
1715ba4576bSAndreas Färber 
172d76d1650Saurel32     return 0;
173d76d1650Saurel32 }
174d76d1650Saurel32 
175d525ffabSPaolo Bonzini int kvm_arch_irqchip_create(MachineState *ms, KVMState *s)
176d525ffabSPaolo Bonzini {
177d525ffabSPaolo Bonzini     return 0;
178d525ffabSPaolo Bonzini }
179d525ffabSPaolo Bonzini 
1801bc22652SAndreas Färber static int kvm_arch_sync_sregs(PowerPCCPU *cpu)
181d76d1650Saurel32 {
1821bc22652SAndreas Färber     CPUPPCState *cenv = &cpu->env;
1831bc22652SAndreas Färber     CPUState *cs = CPU(cpu);
184861bbc80SAlexander Graf     struct kvm_sregs sregs;
1855666ca4aSScott Wood     int ret;
1865666ca4aSScott Wood 
1875666ca4aSScott Wood     if (cenv->excp_model == POWERPC_EXCP_BOOKE) {
188c995e942SDavid Gibson         /*
189c995e942SDavid Gibson          * What we're really trying to say is "if we're on BookE, we
190c995e942SDavid Gibson          * use the native PVR for now". This is the only sane way to
191c995e942SDavid Gibson          * check it though, so we potentially confuse users that they
192c995e942SDavid Gibson          * can run BookE guests on BookS. Let's hope nobody dares
193c995e942SDavid Gibson          * enough :)
194c995e942SDavid Gibson          */
1955666ca4aSScott Wood         return 0;
1965666ca4aSScott Wood     } else {
19790dc8812SScott Wood         if (!cap_segstate) {
19864e07be5SAlexander Graf             fprintf(stderr, "kvm error: missing PVR setting capability\n");
19964e07be5SAlexander Graf             return -ENOSYS;
2005666ca4aSScott Wood         }
2015666ca4aSScott Wood     }
2025666ca4aSScott Wood 
2031bc22652SAndreas Färber     ret = kvm_vcpu_ioctl(cs, KVM_GET_SREGS, &sregs);
2045666ca4aSScott Wood     if (ret) {
2055666ca4aSScott Wood         return ret;
2065666ca4aSScott Wood     }
207861bbc80SAlexander Graf 
208861bbc80SAlexander Graf     sregs.pvr = cenv->spr[SPR_PVR];
2091bc22652SAndreas Färber     return kvm_vcpu_ioctl(cs, KVM_SET_SREGS, &sregs);
2105666ca4aSScott Wood }
2115666ca4aSScott Wood 
21293dd5e85SScott Wood /* Set up a shared TLB array with KVM */
2131bc22652SAndreas Färber static int kvm_booke206_tlb_init(PowerPCCPU *cpu)
21493dd5e85SScott Wood {
2151bc22652SAndreas Färber     CPUPPCState *env = &cpu->env;
2161bc22652SAndreas Färber     CPUState *cs = CPU(cpu);
21793dd5e85SScott Wood     struct kvm_book3e_206_tlb_params params = {};
21893dd5e85SScott Wood     struct kvm_config_tlb cfg = {};
21993dd5e85SScott Wood     unsigned int entries = 0;
22093dd5e85SScott Wood     int ret, i;
22193dd5e85SScott Wood 
22293dd5e85SScott Wood     if (!kvm_enabled() ||
223a60f24b5SAndreas Färber         !kvm_check_extension(cs->kvm_state, KVM_CAP_SW_TLB)) {
22493dd5e85SScott Wood         return 0;
22593dd5e85SScott Wood     }
22693dd5e85SScott Wood 
22793dd5e85SScott Wood     assert(ARRAY_SIZE(params.tlb_sizes) == BOOKE206_MAX_TLBN);
22893dd5e85SScott Wood 
22993dd5e85SScott Wood     for (i = 0; i < BOOKE206_MAX_TLBN; i++) {
23093dd5e85SScott Wood         params.tlb_sizes[i] = booke206_tlb_size(env, i);
23193dd5e85SScott Wood         params.tlb_ways[i] = booke206_tlb_ways(env, i);
23293dd5e85SScott Wood         entries += params.tlb_sizes[i];
23393dd5e85SScott Wood     }
23493dd5e85SScott Wood 
23593dd5e85SScott Wood     assert(entries == env->nb_tlb);
23693dd5e85SScott Wood     assert(sizeof(struct kvm_book3e_206_tlb_entry) == sizeof(ppcmas_tlb_t));
23793dd5e85SScott Wood 
23893dd5e85SScott Wood     env->tlb_dirty = true;
23993dd5e85SScott Wood 
24093dd5e85SScott Wood     cfg.array = (uintptr_t)env->tlb.tlbm;
24193dd5e85SScott Wood     cfg.array_len = sizeof(ppcmas_tlb_t) * entries;
24293dd5e85SScott Wood     cfg.params = (uintptr_t)&params;
24393dd5e85SScott Wood     cfg.mmu_type = KVM_MMU_FSL_BOOKE_NOHV;
24493dd5e85SScott Wood 
24548add816SCornelia Huck     ret = kvm_vcpu_enable_cap(cs, KVM_CAP_SW_TLB, 0, (uintptr_t)&cfg);
24693dd5e85SScott Wood     if (ret < 0) {
24793dd5e85SScott Wood         fprintf(stderr, "%s: couldn't enable KVM_CAP_SW_TLB: %s\n",
24893dd5e85SScott Wood                 __func__, strerror(-ret));
24993dd5e85SScott Wood         return ret;
25093dd5e85SScott Wood     }
25193dd5e85SScott Wood 
25293dd5e85SScott Wood     env->kvm_sw_tlb = true;
25393dd5e85SScott Wood     return 0;
25493dd5e85SScott Wood }
25593dd5e85SScott Wood 
2564656e1f0SBenjamin Herrenschmidt 
2574656e1f0SBenjamin Herrenschmidt #if defined(TARGET_PPC64)
258ab256960SGreg Kurz static void kvm_get_smmu_info(struct kvm_ppc_smmu_info *info, Error **errp)
2594656e1f0SBenjamin Herrenschmidt {
2604656e1f0SBenjamin Herrenschmidt     int ret;
2614656e1f0SBenjamin Herrenschmidt 
262ab256960SGreg Kurz     assert(kvm_state != NULL);
263ab256960SGreg Kurz 
264ab256960SGreg Kurz     if (!kvm_check_extension(kvm_state, KVM_CAP_PPC_GET_SMMU_INFO)) {
26571d0f1eaSGreg Kurz         error_setg(errp, "KVM doesn't expose the MMU features it supports");
26671d0f1eaSGreg Kurz         error_append_hint(errp, "Consider switching to a newer KVM\n");
26771d0f1eaSGreg Kurz         return;
26871d0f1eaSGreg Kurz     }
26971d0f1eaSGreg Kurz 
270ab256960SGreg Kurz     ret = kvm_vm_ioctl(kvm_state, KVM_PPC_GET_SMMU_INFO, info);
2714656e1f0SBenjamin Herrenschmidt     if (ret == 0) {
2724656e1f0SBenjamin Herrenschmidt         return;
2734656e1f0SBenjamin Herrenschmidt     }
2744656e1f0SBenjamin Herrenschmidt 
27571d0f1eaSGreg Kurz     error_setg_errno(errp, -ret,
27671d0f1eaSGreg Kurz                      "KVM failed to provide the MMU features it supports");
2774656e1f0SBenjamin Herrenschmidt }
2784656e1f0SBenjamin Herrenschmidt 
279c64abd1fSSam Bobroff struct ppc_radix_page_info *kvm_get_radix_page_info(void)
280c64abd1fSSam Bobroff {
281c64abd1fSSam Bobroff     KVMState *s = KVM_STATE(current_machine->accelerator);
282c64abd1fSSam Bobroff     struct ppc_radix_page_info *radix_page_info;
283c64abd1fSSam Bobroff     struct kvm_ppc_rmmu_info rmmu_info;
284c64abd1fSSam Bobroff     int i;
285c64abd1fSSam Bobroff 
286c64abd1fSSam Bobroff     if (!kvm_check_extension(s, KVM_CAP_PPC_MMU_RADIX)) {
287c64abd1fSSam Bobroff         return NULL;
288c64abd1fSSam Bobroff     }
289c64abd1fSSam Bobroff     if (kvm_vm_ioctl(s, KVM_PPC_GET_RMMU_INFO, &rmmu_info)) {
290c64abd1fSSam Bobroff         return NULL;
291c64abd1fSSam Bobroff     }
292c64abd1fSSam Bobroff     radix_page_info = g_malloc0(sizeof(*radix_page_info));
293c64abd1fSSam Bobroff     radix_page_info->count = 0;
294c64abd1fSSam Bobroff     for (i = 0; i < PPC_PAGE_SIZES_MAX_SZ; i++) {
295c64abd1fSSam Bobroff         if (rmmu_info.ap_encodings[i]) {
296c64abd1fSSam Bobroff             radix_page_info->entries[i] = rmmu_info.ap_encodings[i];
297c64abd1fSSam Bobroff             radix_page_info->count++;
298c64abd1fSSam Bobroff         }
299c64abd1fSSam Bobroff     }
300c64abd1fSSam Bobroff     return radix_page_info;
301c64abd1fSSam Bobroff }
302c64abd1fSSam Bobroff 
303b4db5413SSuraj Jitindar Singh target_ulong kvmppc_configure_v3_mmu(PowerPCCPU *cpu,
304b4db5413SSuraj Jitindar Singh                                      bool radix, bool gtse,
305b4db5413SSuraj Jitindar Singh                                      uint64_t proc_tbl)
306b4db5413SSuraj Jitindar Singh {
307b4db5413SSuraj Jitindar Singh     CPUState *cs = CPU(cpu);
308b4db5413SSuraj Jitindar Singh     int ret;
309b4db5413SSuraj Jitindar Singh     uint64_t flags = 0;
310b4db5413SSuraj Jitindar Singh     struct kvm_ppc_mmuv3_cfg cfg = {
311b4db5413SSuraj Jitindar Singh         .process_table = proc_tbl,
312b4db5413SSuraj Jitindar Singh     };
313b4db5413SSuraj Jitindar Singh 
314b4db5413SSuraj Jitindar Singh     if (radix) {
315b4db5413SSuraj Jitindar Singh         flags |= KVM_PPC_MMUV3_RADIX;
316b4db5413SSuraj Jitindar Singh     }
317b4db5413SSuraj Jitindar Singh     if (gtse) {
318b4db5413SSuraj Jitindar Singh         flags |= KVM_PPC_MMUV3_GTSE;
319b4db5413SSuraj Jitindar Singh     }
320b4db5413SSuraj Jitindar Singh     cfg.flags = flags;
321b4db5413SSuraj Jitindar Singh     ret = kvm_vm_ioctl(cs->kvm_state, KVM_PPC_CONFIGURE_V3_MMU, &cfg);
322b4db5413SSuraj Jitindar Singh     switch (ret) {
323b4db5413SSuraj Jitindar Singh     case 0:
324b4db5413SSuraj Jitindar Singh         return H_SUCCESS;
325b4db5413SSuraj Jitindar Singh     case -EINVAL:
326b4db5413SSuraj Jitindar Singh         return H_PARAMETER;
327b4db5413SSuraj Jitindar Singh     case -ENODEV:
328b4db5413SSuraj Jitindar Singh         return H_NOT_AVAILABLE;
329b4db5413SSuraj Jitindar Singh     default:
330b4db5413SSuraj Jitindar Singh         return H_HARDWARE;
331b4db5413SSuraj Jitindar Singh     }
332b4db5413SSuraj Jitindar Singh }
333b4db5413SSuraj Jitindar Singh 
33424c6863cSDavid Gibson bool kvmppc_hpt_needs_host_contiguous_pages(void)
33524c6863cSDavid Gibson {
33624c6863cSDavid Gibson     static struct kvm_ppc_smmu_info smmu_info;
33724c6863cSDavid Gibson 
33824c6863cSDavid Gibson     if (!kvm_enabled()) {
33924c6863cSDavid Gibson         return false;
34024c6863cSDavid Gibson     }
34124c6863cSDavid Gibson 
342ab256960SGreg Kurz     kvm_get_smmu_info(&smmu_info, &error_fatal);
34324c6863cSDavid Gibson     return !!(smmu_info.flags & KVM_PPC_PAGE_SIZES_REAL);
34424c6863cSDavid Gibson }
34524c6863cSDavid Gibson 
346e5ca28ecSDavid Gibson void kvm_check_mmu(PowerPCCPU *cpu, Error **errp)
3474656e1f0SBenjamin Herrenschmidt {
348e5ca28ecSDavid Gibson     struct kvm_ppc_smmu_info smmu_info;
3494656e1f0SBenjamin Herrenschmidt     int iq, ik, jq, jk;
35071d0f1eaSGreg Kurz     Error *local_err = NULL;
3514656e1f0SBenjamin Herrenschmidt 
352e5ca28ecSDavid Gibson     /* For now, we only have anything to check on hash64 MMUs */
353e5ca28ecSDavid Gibson     if (!cpu->hash64_opts || !kvm_enabled()) {
3544656e1f0SBenjamin Herrenschmidt         return;
3554656e1f0SBenjamin Herrenschmidt     }
3564656e1f0SBenjamin Herrenschmidt 
357ab256960SGreg Kurz     kvm_get_smmu_info(&smmu_info, &local_err);
35871d0f1eaSGreg Kurz     if (local_err) {
35971d0f1eaSGreg Kurz         error_propagate(errp, local_err);
36071d0f1eaSGreg Kurz         return;
36171d0f1eaSGreg Kurz     }
362e5ca28ecSDavid Gibson 
363e5ca28ecSDavid Gibson     if (ppc_hash64_has(cpu, PPC_HASH64_1TSEG)
364e5ca28ecSDavid Gibson         && !(smmu_info.flags & KVM_PPC_1T_SEGMENTS)) {
365e5ca28ecSDavid Gibson         error_setg(errp,
366e5ca28ecSDavid Gibson                    "KVM does not support 1TiB segments which guest expects");
367e5ca28ecSDavid Gibson         return;
3684656e1f0SBenjamin Herrenschmidt     }
3694656e1f0SBenjamin Herrenschmidt 
370e5ca28ecSDavid Gibson     if (smmu_info.slb_size < cpu->hash64_opts->slb_size) {
371e5ca28ecSDavid Gibson         error_setg(errp, "KVM only supports %u SLB entries, but guest needs %u",
372e5ca28ecSDavid Gibson                    smmu_info.slb_size, cpu->hash64_opts->slb_size);
373e5ca28ecSDavid Gibson         return;
37490da0d5aSBenjamin Herrenschmidt     }
37590da0d5aSBenjamin Herrenschmidt 
37608215d8fSAlexander Graf     /*
377e5ca28ecSDavid Gibson      * Verify that every pagesize supported by the cpu model is
378e5ca28ecSDavid Gibson      * supported by KVM with the same encodings
37908215d8fSAlexander Graf      */
380e5ca28ecSDavid Gibson     for (iq = 0; iq < ARRAY_SIZE(cpu->hash64_opts->sps); iq++) {
381b07c59f7SDavid Gibson         PPCHash64SegmentPageSizes *qsps = &cpu->hash64_opts->sps[iq];
382e5ca28ecSDavid Gibson         struct kvm_ppc_one_seg_page_size *ksps;
3834656e1f0SBenjamin Herrenschmidt 
384e5ca28ecSDavid Gibson         for (ik = 0; ik < ARRAY_SIZE(smmu_info.sps); ik++) {
385e5ca28ecSDavid Gibson             if (qsps->page_shift == smmu_info.sps[ik].page_shift) {
3864656e1f0SBenjamin Herrenschmidt                 break;
3874656e1f0SBenjamin Herrenschmidt             }
3884656e1f0SBenjamin Herrenschmidt         }
389e5ca28ecSDavid Gibson         if (ik >= ARRAY_SIZE(smmu_info.sps)) {
390e5ca28ecSDavid Gibson             error_setg(errp, "KVM doesn't support for base page shift %u",
391e5ca28ecSDavid Gibson                        qsps->page_shift);
392e5ca28ecSDavid Gibson             return;
393e5ca28ecSDavid Gibson         }
394e5ca28ecSDavid Gibson 
395e5ca28ecSDavid Gibson         ksps = &smmu_info.sps[ik];
396e5ca28ecSDavid Gibson         if (ksps->slb_enc != qsps->slb_enc) {
397e5ca28ecSDavid Gibson             error_setg(errp,
398e5ca28ecSDavid Gibson "KVM uses SLB encoding 0x%x for page shift %u, but guest expects 0x%x",
399e5ca28ecSDavid Gibson                        ksps->slb_enc, ksps->page_shift, qsps->slb_enc);
400e5ca28ecSDavid Gibson             return;
401e5ca28ecSDavid Gibson         }
402e5ca28ecSDavid Gibson 
403e5ca28ecSDavid Gibson         for (jq = 0; jq < ARRAY_SIZE(qsps->enc); jq++) {
404e5ca28ecSDavid Gibson             for (jk = 0; jk < ARRAY_SIZE(ksps->enc); jk++) {
405e5ca28ecSDavid Gibson                 if (qsps->enc[jq].page_shift == ksps->enc[jk].page_shift) {
4064656e1f0SBenjamin Herrenschmidt                     break;
4074656e1f0SBenjamin Herrenschmidt                 }
4084656e1f0SBenjamin Herrenschmidt             }
4094656e1f0SBenjamin Herrenschmidt 
410e5ca28ecSDavid Gibson             if (jk >= ARRAY_SIZE(ksps->enc)) {
411e5ca28ecSDavid Gibson                 error_setg(errp, "KVM doesn't support page shift %u/%u",
412e5ca28ecSDavid Gibson                            qsps->enc[jq].page_shift, qsps->page_shift);
413e5ca28ecSDavid Gibson                 return;
414e5ca28ecSDavid Gibson             }
415e5ca28ecSDavid Gibson             if (qsps->enc[jq].pte_enc != ksps->enc[jk].pte_enc) {
416e5ca28ecSDavid Gibson                 error_setg(errp,
417e5ca28ecSDavid Gibson "KVM uses PTE encoding 0x%x for page shift %u/%u, but guest expects 0x%x",
418e5ca28ecSDavid Gibson                            ksps->enc[jk].pte_enc, qsps->enc[jq].page_shift,
419e5ca28ecSDavid Gibson                            qsps->page_shift, qsps->enc[jq].pte_enc);
420e5ca28ecSDavid Gibson                 return;
421e5ca28ecSDavid Gibson             }
422e5ca28ecSDavid Gibson         }
4234656e1f0SBenjamin Herrenschmidt     }
4244656e1f0SBenjamin Herrenschmidt 
425e5ca28ecSDavid Gibson     if (ppc_hash64_has(cpu, PPC_HASH64_CI_LARGEPAGE)) {
426c995e942SDavid Gibson         /*
427c995e942SDavid Gibson          * Mostly what guest pagesizes we can use are related to the
428e5ca28ecSDavid Gibson          * host pages used to map guest RAM, which is handled in the
429e5ca28ecSDavid Gibson          * platform code. Cache-Inhibited largepages (64k) however are
430e5ca28ecSDavid Gibson          * used for I/O, so if they're mapped to the host at all it
431e5ca28ecSDavid Gibson          * will be a normal mapping, not a special hugepage one used
432c995e942SDavid Gibson          * for RAM.
433c995e942SDavid Gibson          */
434e5ca28ecSDavid Gibson         if (getpagesize() < 0x10000) {
435e5ca28ecSDavid Gibson             error_setg(errp,
436e5ca28ecSDavid Gibson                        "KVM can't supply 64kiB CI pages, which guest expects");
437e5ca28ecSDavid Gibson         }
438e5ca28ecSDavid Gibson     }
439e5ca28ecSDavid Gibson }
4404656e1f0SBenjamin Herrenschmidt #endif /* !defined (TARGET_PPC64) */
4414656e1f0SBenjamin Herrenschmidt 
442b164e48eSEduardo Habkost unsigned long kvm_arch_vcpu_id(CPUState *cpu)
443b164e48eSEduardo Habkost {
4442e886fb3SSam Bobroff     return POWERPC_CPU(cpu)->vcpu_id;
445b164e48eSEduardo Habkost }
446b164e48eSEduardo Habkost 
447c995e942SDavid Gibson /*
448c995e942SDavid Gibson  * e500 supports 2 h/w breakpoint and 2 watchpoint.  book3s supports
449c995e942SDavid Gibson  * only 1 watchpoint, so array size of 4 is sufficient for now.
45088365d17SBharat Bhushan  */
45188365d17SBharat Bhushan #define MAX_HW_BKPTS 4
45288365d17SBharat Bhushan 
45388365d17SBharat Bhushan static struct HWBreakpoint {
45488365d17SBharat Bhushan     target_ulong addr;
45588365d17SBharat Bhushan     int type;
45688365d17SBharat Bhushan } hw_debug_points[MAX_HW_BKPTS];
45788365d17SBharat Bhushan 
45888365d17SBharat Bhushan static CPUWatchpoint hw_watchpoint;
45988365d17SBharat Bhushan 
46088365d17SBharat Bhushan /* Default there is no breakpoint and watchpoint supported */
46188365d17SBharat Bhushan static int max_hw_breakpoint;
46288365d17SBharat Bhushan static int max_hw_watchpoint;
46388365d17SBharat Bhushan static int nb_hw_breakpoint;
46488365d17SBharat Bhushan static int nb_hw_watchpoint;
46588365d17SBharat Bhushan 
46688365d17SBharat Bhushan static void kvmppc_hw_debug_points_init(CPUPPCState *cenv)
46788365d17SBharat Bhushan {
46888365d17SBharat Bhushan     if (cenv->excp_model == POWERPC_EXCP_BOOKE) {
46988365d17SBharat Bhushan         max_hw_breakpoint = 2;
47088365d17SBharat Bhushan         max_hw_watchpoint = 2;
47188365d17SBharat Bhushan     }
47288365d17SBharat Bhushan 
47388365d17SBharat Bhushan     if ((max_hw_breakpoint + max_hw_watchpoint) > MAX_HW_BKPTS) {
47488365d17SBharat Bhushan         fprintf(stderr, "Error initializing h/w breakpoints\n");
47588365d17SBharat Bhushan         return;
47688365d17SBharat Bhushan     }
47788365d17SBharat Bhushan }
47888365d17SBharat Bhushan 
47920d695a9SAndreas Färber int kvm_arch_init_vcpu(CPUState *cs)
4805666ca4aSScott Wood {
48120d695a9SAndreas Färber     PowerPCCPU *cpu = POWERPC_CPU(cs);
48220d695a9SAndreas Färber     CPUPPCState *cenv = &cpu->env;
4835666ca4aSScott Wood     int ret;
4845666ca4aSScott Wood 
4854656e1f0SBenjamin Herrenschmidt     /* Synchronize sregs with kvm */
4861bc22652SAndreas Färber     ret = kvm_arch_sync_sregs(cpu);
4875666ca4aSScott Wood     if (ret) {
488388e47c7SThomas Huth         if (ret == -EINVAL) {
489388e47c7SThomas Huth             error_report("Register sync failed... If you're using kvm-hv.ko,"
490388e47c7SThomas Huth                          " only \"-cpu host\" is possible");
491388e47c7SThomas Huth         }
4925666ca4aSScott Wood         return ret;
4935666ca4aSScott Wood     }
494861bbc80SAlexander Graf 
495bc72ad67SAlex Bligh     idle_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, kvm_kick_cpu, cpu);
496c821c2bdSAlexander Graf 
49793dd5e85SScott Wood     switch (cenv->mmu_model) {
49893dd5e85SScott Wood     case POWERPC_MMU_BOOKE206:
4997f516c96SThomas Huth         /* This target supports access to KVM's guest TLB */
5001bc22652SAndreas Färber         ret = kvm_booke206_tlb_init(cpu);
50193dd5e85SScott Wood         break;
5027f516c96SThomas Huth     case POWERPC_MMU_2_07:
5037f516c96SThomas Huth         if (!cap_htm && !kvmppc_is_pr(cs->kvm_state)) {
504c995e942SDavid Gibson             /*
505c995e942SDavid Gibson              * KVM-HV has transactional memory on POWER8 also without
506c995e942SDavid Gibson              * the KVM_CAP_PPC_HTM extension, so enable it here
507c995e942SDavid Gibson              * instead as long as it's availble to userspace on the
508c995e942SDavid Gibson              * host.
509c995e942SDavid Gibson              */
510f3d9f303SSam Bobroff             if (qemu_getauxval(AT_HWCAP2) & PPC_FEATURE2_HAS_HTM) {
5117f516c96SThomas Huth                 cap_htm = true;
5127f516c96SThomas Huth             }
513f3d9f303SSam Bobroff         }
5147f516c96SThomas Huth         break;
51593dd5e85SScott Wood     default:
51693dd5e85SScott Wood         break;
51793dd5e85SScott Wood     }
51893dd5e85SScott Wood 
5193c902d44SBharat Bhushan     kvm_get_one_reg(cs, KVM_REG_PPC_DEBUG_INST, &debug_inst_opcode);
52088365d17SBharat Bhushan     kvmppc_hw_debug_points_init(cenv);
5213c902d44SBharat Bhushan 
522861bbc80SAlexander Graf     return ret;
523d76d1650Saurel32 }
524d76d1650Saurel32 
525b1115c99SLiran Alon int kvm_arch_destroy_vcpu(CPUState *cs)
526b1115c99SLiran Alon {
527b1115c99SLiran Alon     return 0;
528b1115c99SLiran Alon }
529b1115c99SLiran Alon 
5301bc22652SAndreas Färber static void kvm_sw_tlb_put(PowerPCCPU *cpu)
53193dd5e85SScott Wood {
5321bc22652SAndreas Färber     CPUPPCState *env = &cpu->env;
5331bc22652SAndreas Färber     CPUState *cs = CPU(cpu);
53493dd5e85SScott Wood     struct kvm_dirty_tlb dirty_tlb;
53593dd5e85SScott Wood     unsigned char *bitmap;
53693dd5e85SScott Wood     int ret;
53793dd5e85SScott Wood 
53893dd5e85SScott Wood     if (!env->kvm_sw_tlb) {
53993dd5e85SScott Wood         return;
54093dd5e85SScott Wood     }
54193dd5e85SScott Wood 
54293dd5e85SScott Wood     bitmap = g_malloc((env->nb_tlb + 7) / 8);
54393dd5e85SScott Wood     memset(bitmap, 0xFF, (env->nb_tlb + 7) / 8);
54493dd5e85SScott Wood 
54593dd5e85SScott Wood     dirty_tlb.bitmap = (uintptr_t)bitmap;
54693dd5e85SScott Wood     dirty_tlb.num_dirty = env->nb_tlb;
54793dd5e85SScott Wood 
5481bc22652SAndreas Färber     ret = kvm_vcpu_ioctl(cs, KVM_DIRTY_TLB, &dirty_tlb);
54993dd5e85SScott Wood     if (ret) {
55093dd5e85SScott Wood         fprintf(stderr, "%s: KVM_DIRTY_TLB: %s\n",
55193dd5e85SScott Wood                 __func__, strerror(-ret));
55293dd5e85SScott Wood     }
55393dd5e85SScott Wood 
55493dd5e85SScott Wood     g_free(bitmap);
55593dd5e85SScott Wood }
55693dd5e85SScott Wood 
557d67d40eaSDavid Gibson static void kvm_get_one_spr(CPUState *cs, uint64_t id, int spr)
558d67d40eaSDavid Gibson {
559d67d40eaSDavid Gibson     PowerPCCPU *cpu = POWERPC_CPU(cs);
560d67d40eaSDavid Gibson     CPUPPCState *env = &cpu->env;
561d67d40eaSDavid Gibson     union {
562d67d40eaSDavid Gibson         uint32_t u32;
563d67d40eaSDavid Gibson         uint64_t u64;
564d67d40eaSDavid Gibson     } val;
565d67d40eaSDavid Gibson     struct kvm_one_reg reg = {
566d67d40eaSDavid Gibson         .id = id,
567d67d40eaSDavid Gibson         .addr = (uintptr_t) &val,
568d67d40eaSDavid Gibson     };
569d67d40eaSDavid Gibson     int ret;
570d67d40eaSDavid Gibson 
571d67d40eaSDavid Gibson     ret = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, &reg);
572d67d40eaSDavid Gibson     if (ret != 0) {
573b36f100eSAlexey Kardashevskiy         trace_kvm_failed_spr_get(spr, strerror(errno));
574d67d40eaSDavid Gibson     } else {
575d67d40eaSDavid Gibson         switch (id & KVM_REG_SIZE_MASK) {
576d67d40eaSDavid Gibson         case KVM_REG_SIZE_U32:
577d67d40eaSDavid Gibson             env->spr[spr] = val.u32;
578d67d40eaSDavid Gibson             break;
579d67d40eaSDavid Gibson 
580d67d40eaSDavid Gibson         case KVM_REG_SIZE_U64:
581d67d40eaSDavid Gibson             env->spr[spr] = val.u64;
582d67d40eaSDavid Gibson             break;
583d67d40eaSDavid Gibson 
584d67d40eaSDavid Gibson         default:
585d67d40eaSDavid Gibson             /* Don't handle this size yet */
586d67d40eaSDavid Gibson             abort();
587d67d40eaSDavid Gibson         }
588d67d40eaSDavid Gibson     }
589d67d40eaSDavid Gibson }
590d67d40eaSDavid Gibson 
591d67d40eaSDavid Gibson static void kvm_put_one_spr(CPUState *cs, uint64_t id, int spr)
592d67d40eaSDavid Gibson {
593d67d40eaSDavid Gibson     PowerPCCPU *cpu = POWERPC_CPU(cs);
594d67d40eaSDavid Gibson     CPUPPCState *env = &cpu->env;
595d67d40eaSDavid Gibson     union {
596d67d40eaSDavid Gibson         uint32_t u32;
597d67d40eaSDavid Gibson         uint64_t u64;
598d67d40eaSDavid Gibson     } val;
599d67d40eaSDavid Gibson     struct kvm_one_reg reg = {
600d67d40eaSDavid Gibson         .id = id,
601d67d40eaSDavid Gibson         .addr = (uintptr_t) &val,
602d67d40eaSDavid Gibson     };
603d67d40eaSDavid Gibson     int ret;
604d67d40eaSDavid Gibson 
605d67d40eaSDavid Gibson     switch (id & KVM_REG_SIZE_MASK) {
606d67d40eaSDavid Gibson     case KVM_REG_SIZE_U32:
607d67d40eaSDavid Gibson         val.u32 = env->spr[spr];
608d67d40eaSDavid Gibson         break;
609d67d40eaSDavid Gibson 
610d67d40eaSDavid Gibson     case KVM_REG_SIZE_U64:
611d67d40eaSDavid Gibson         val.u64 = env->spr[spr];
612d67d40eaSDavid Gibson         break;
613d67d40eaSDavid Gibson 
614d67d40eaSDavid Gibson     default:
615d67d40eaSDavid Gibson         /* Don't handle this size yet */
616d67d40eaSDavid Gibson         abort();
617d67d40eaSDavid Gibson     }
618d67d40eaSDavid Gibson 
619d67d40eaSDavid Gibson     ret = kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, &reg);
620d67d40eaSDavid Gibson     if (ret != 0) {
621b36f100eSAlexey Kardashevskiy         trace_kvm_failed_spr_set(spr, strerror(errno));
622d67d40eaSDavid Gibson     }
623d67d40eaSDavid Gibson }
624d67d40eaSDavid Gibson 
62570b79849SDavid Gibson static int kvm_put_fp(CPUState *cs)
62670b79849SDavid Gibson {
62770b79849SDavid Gibson     PowerPCCPU *cpu = POWERPC_CPU(cs);
62870b79849SDavid Gibson     CPUPPCState *env = &cpu->env;
62970b79849SDavid Gibson     struct kvm_one_reg reg;
63070b79849SDavid Gibson     int i;
63170b79849SDavid Gibson     int ret;
63270b79849SDavid Gibson 
63370b79849SDavid Gibson     if (env->insns_flags & PPC_FLOAT) {
63470b79849SDavid Gibson         uint64_t fpscr = env->fpscr;
63570b79849SDavid Gibson         bool vsx = !!(env->insns_flags2 & PPC2_VSX);
63670b79849SDavid Gibson 
63770b79849SDavid Gibson         reg.id = KVM_REG_PPC_FPSCR;
63870b79849SDavid Gibson         reg.addr = (uintptr_t)&fpscr;
63970b79849SDavid Gibson         ret = kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, &reg);
64070b79849SDavid Gibson         if (ret < 0) {
6418d83cbf1SGreg Kurz             trace_kvm_failed_fpscr_set(strerror(errno));
64270b79849SDavid Gibson             return ret;
64370b79849SDavid Gibson         }
64470b79849SDavid Gibson 
64570b79849SDavid Gibson         for (i = 0; i < 32; i++) {
64670b79849SDavid Gibson             uint64_t vsr[2];
647ef96e3aeSMark Cave-Ayland             uint64_t *fpr = cpu_fpr_ptr(&cpu->env, i);
648ef96e3aeSMark Cave-Ayland             uint64_t *vsrl = cpu_vsrl_ptr(&cpu->env, i);
64970b79849SDavid Gibson 
6503a4b791bSGreg Kurz #ifdef HOST_WORDS_BIGENDIAN
651ef96e3aeSMark Cave-Ayland             vsr[0] = float64_val(*fpr);
652ef96e3aeSMark Cave-Ayland             vsr[1] = *vsrl;
6533a4b791bSGreg Kurz #else
654ef96e3aeSMark Cave-Ayland             vsr[0] = *vsrl;
655ef96e3aeSMark Cave-Ayland             vsr[1] = float64_val(*fpr);
6563a4b791bSGreg Kurz #endif
65770b79849SDavid Gibson             reg.addr = (uintptr_t) &vsr;
65870b79849SDavid Gibson             reg.id = vsx ? KVM_REG_PPC_VSR(i) : KVM_REG_PPC_FPR(i);
65970b79849SDavid Gibson 
66070b79849SDavid Gibson             ret = kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, &reg);
66170b79849SDavid Gibson             if (ret < 0) {
6628d83cbf1SGreg Kurz                 trace_kvm_failed_fp_set(vsx ? "VSR" : "FPR", i,
6638d83cbf1SGreg Kurz                                         strerror(errno));
66470b79849SDavid Gibson                 return ret;
66570b79849SDavid Gibson             }
66670b79849SDavid Gibson         }
66770b79849SDavid Gibson     }
66870b79849SDavid Gibson 
66970b79849SDavid Gibson     if (env->insns_flags & PPC_ALTIVEC) {
67070b79849SDavid Gibson         reg.id = KVM_REG_PPC_VSCR;
67170b79849SDavid Gibson         reg.addr = (uintptr_t)&env->vscr;
67270b79849SDavid Gibson         ret = kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, &reg);
67370b79849SDavid Gibson         if (ret < 0) {
6748d83cbf1SGreg Kurz             trace_kvm_failed_vscr_set(strerror(errno));
67570b79849SDavid Gibson             return ret;
67670b79849SDavid Gibson         }
67770b79849SDavid Gibson 
67870b79849SDavid Gibson         for (i = 0; i < 32; i++) {
67970b79849SDavid Gibson             reg.id = KVM_REG_PPC_VR(i);
680ef96e3aeSMark Cave-Ayland             reg.addr = (uintptr_t)cpu_avr_ptr(env, i);
68170b79849SDavid Gibson             ret = kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, &reg);
68270b79849SDavid Gibson             if (ret < 0) {
6838d83cbf1SGreg Kurz                 trace_kvm_failed_vr_set(i, strerror(errno));
68470b79849SDavid Gibson                 return ret;
68570b79849SDavid Gibson             }
68670b79849SDavid Gibson         }
68770b79849SDavid Gibson     }
68870b79849SDavid Gibson 
68970b79849SDavid Gibson     return 0;
69070b79849SDavid Gibson }
69170b79849SDavid Gibson 
69270b79849SDavid Gibson static int kvm_get_fp(CPUState *cs)
69370b79849SDavid Gibson {
69470b79849SDavid Gibson     PowerPCCPU *cpu = POWERPC_CPU(cs);
69570b79849SDavid Gibson     CPUPPCState *env = &cpu->env;
69670b79849SDavid Gibson     struct kvm_one_reg reg;
69770b79849SDavid Gibson     int i;
69870b79849SDavid Gibson     int ret;
69970b79849SDavid Gibson 
70070b79849SDavid Gibson     if (env->insns_flags & PPC_FLOAT) {
70170b79849SDavid Gibson         uint64_t fpscr;
70270b79849SDavid Gibson         bool vsx = !!(env->insns_flags2 & PPC2_VSX);
70370b79849SDavid Gibson 
70470b79849SDavid Gibson         reg.id = KVM_REG_PPC_FPSCR;
70570b79849SDavid Gibson         reg.addr = (uintptr_t)&fpscr;
70670b79849SDavid Gibson         ret = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, &reg);
70770b79849SDavid Gibson         if (ret < 0) {
7088d83cbf1SGreg Kurz             trace_kvm_failed_fpscr_get(strerror(errno));
70970b79849SDavid Gibson             return ret;
71070b79849SDavid Gibson         } else {
71170b79849SDavid Gibson             env->fpscr = fpscr;
71270b79849SDavid Gibson         }
71370b79849SDavid Gibson 
71470b79849SDavid Gibson         for (i = 0; i < 32; i++) {
71570b79849SDavid Gibson             uint64_t vsr[2];
716ef96e3aeSMark Cave-Ayland             uint64_t *fpr = cpu_fpr_ptr(&cpu->env, i);
717ef96e3aeSMark Cave-Ayland             uint64_t *vsrl = cpu_vsrl_ptr(&cpu->env, i);
71870b79849SDavid Gibson 
71970b79849SDavid Gibson             reg.addr = (uintptr_t) &vsr;
72070b79849SDavid Gibson             reg.id = vsx ? KVM_REG_PPC_VSR(i) : KVM_REG_PPC_FPR(i);
72170b79849SDavid Gibson 
72270b79849SDavid Gibson             ret = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, &reg);
72370b79849SDavid Gibson             if (ret < 0) {
7248d83cbf1SGreg Kurz                 trace_kvm_failed_fp_get(vsx ? "VSR" : "FPR", i,
7258d83cbf1SGreg Kurz                                         strerror(errno));
72670b79849SDavid Gibson                 return ret;
72770b79849SDavid Gibson             } else {
7283a4b791bSGreg Kurz #ifdef HOST_WORDS_BIGENDIAN
729ef96e3aeSMark Cave-Ayland                 *fpr = vsr[0];
73070b79849SDavid Gibson                 if (vsx) {
731ef96e3aeSMark Cave-Ayland                     *vsrl = vsr[1];
73270b79849SDavid Gibson                 }
7333a4b791bSGreg Kurz #else
734ef96e3aeSMark Cave-Ayland                 *fpr = vsr[1];
7353a4b791bSGreg Kurz                 if (vsx) {
736ef96e3aeSMark Cave-Ayland                     *vsrl = vsr[0];
7373a4b791bSGreg Kurz                 }
7383a4b791bSGreg Kurz #endif
73970b79849SDavid Gibson             }
74070b79849SDavid Gibson         }
74170b79849SDavid Gibson     }
74270b79849SDavid Gibson 
74370b79849SDavid Gibson     if (env->insns_flags & PPC_ALTIVEC) {
74470b79849SDavid Gibson         reg.id = KVM_REG_PPC_VSCR;
74570b79849SDavid Gibson         reg.addr = (uintptr_t)&env->vscr;
74670b79849SDavid Gibson         ret = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, &reg);
74770b79849SDavid Gibson         if (ret < 0) {
7488d83cbf1SGreg Kurz             trace_kvm_failed_vscr_get(strerror(errno));
74970b79849SDavid Gibson             return ret;
75070b79849SDavid Gibson         }
75170b79849SDavid Gibson 
75270b79849SDavid Gibson         for (i = 0; i < 32; i++) {
75370b79849SDavid Gibson             reg.id = KVM_REG_PPC_VR(i);
754ef96e3aeSMark Cave-Ayland             reg.addr = (uintptr_t)cpu_avr_ptr(env, i);
75570b79849SDavid Gibson             ret = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, &reg);
75670b79849SDavid Gibson             if (ret < 0) {
7578d83cbf1SGreg Kurz                 trace_kvm_failed_vr_get(i, strerror(errno));
75870b79849SDavid Gibson                 return ret;
75970b79849SDavid Gibson             }
76070b79849SDavid Gibson         }
76170b79849SDavid Gibson     }
76270b79849SDavid Gibson 
76370b79849SDavid Gibson     return 0;
76470b79849SDavid Gibson }
76570b79849SDavid Gibson 
7669b00ea49SDavid Gibson #if defined(TARGET_PPC64)
7679b00ea49SDavid Gibson static int kvm_get_vpa(CPUState *cs)
7689b00ea49SDavid Gibson {
7699b00ea49SDavid Gibson     PowerPCCPU *cpu = POWERPC_CPU(cs);
770ce2918cbSDavid Gibson     SpaprCpuState *spapr_cpu = spapr_cpu_state(cpu);
7719b00ea49SDavid Gibson     struct kvm_one_reg reg;
7729b00ea49SDavid Gibson     int ret;
7739b00ea49SDavid Gibson 
7749b00ea49SDavid Gibson     reg.id = KVM_REG_PPC_VPA_ADDR;
7757388efafSDavid Gibson     reg.addr = (uintptr_t)&spapr_cpu->vpa_addr;
7769b00ea49SDavid Gibson     ret = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, &reg);
7779b00ea49SDavid Gibson     if (ret < 0) {
7788d83cbf1SGreg Kurz         trace_kvm_failed_vpa_addr_get(strerror(errno));
7799b00ea49SDavid Gibson         return ret;
7809b00ea49SDavid Gibson     }
7819b00ea49SDavid Gibson 
7827388efafSDavid Gibson     assert((uintptr_t)&spapr_cpu->slb_shadow_size
7837388efafSDavid Gibson            == ((uintptr_t)&spapr_cpu->slb_shadow_addr + 8));
7849b00ea49SDavid Gibson     reg.id = KVM_REG_PPC_VPA_SLB;
7857388efafSDavid Gibson     reg.addr = (uintptr_t)&spapr_cpu->slb_shadow_addr;
7869b00ea49SDavid Gibson     ret = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, &reg);
7879b00ea49SDavid Gibson     if (ret < 0) {
7888d83cbf1SGreg Kurz         trace_kvm_failed_slb_get(strerror(errno));
7899b00ea49SDavid Gibson         return ret;
7909b00ea49SDavid Gibson     }
7919b00ea49SDavid Gibson 
7927388efafSDavid Gibson     assert((uintptr_t)&spapr_cpu->dtl_size
7937388efafSDavid Gibson            == ((uintptr_t)&spapr_cpu->dtl_addr + 8));
7949b00ea49SDavid Gibson     reg.id = KVM_REG_PPC_VPA_DTL;
7957388efafSDavid Gibson     reg.addr = (uintptr_t)&spapr_cpu->dtl_addr;
7969b00ea49SDavid Gibson     ret = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, &reg);
7979b00ea49SDavid Gibson     if (ret < 0) {
7988d83cbf1SGreg Kurz         trace_kvm_failed_dtl_get(strerror(errno));
7999b00ea49SDavid Gibson         return ret;
8009b00ea49SDavid Gibson     }
8019b00ea49SDavid Gibson 
8029b00ea49SDavid Gibson     return 0;
8039b00ea49SDavid Gibson }
8049b00ea49SDavid Gibson 
8059b00ea49SDavid Gibson static int kvm_put_vpa(CPUState *cs)
8069b00ea49SDavid Gibson {
8079b00ea49SDavid Gibson     PowerPCCPU *cpu = POWERPC_CPU(cs);
808ce2918cbSDavid Gibson     SpaprCpuState *spapr_cpu = spapr_cpu_state(cpu);
8099b00ea49SDavid Gibson     struct kvm_one_reg reg;
8109b00ea49SDavid Gibson     int ret;
8119b00ea49SDavid Gibson 
812c995e942SDavid Gibson     /*
813c995e942SDavid Gibson      * SLB shadow or DTL can't be registered unless a master VPA is
8149b00ea49SDavid Gibson      * registered.  That means when restoring state, if a VPA *is*
8159b00ea49SDavid Gibson      * registered, we need to set that up first.  If not, we need to
816c995e942SDavid Gibson      * deregister the others before deregistering the master VPA
817c995e942SDavid Gibson      */
8187388efafSDavid Gibson     assert(spapr_cpu->vpa_addr
8197388efafSDavid Gibson            || !(spapr_cpu->slb_shadow_addr || spapr_cpu->dtl_addr));
8209b00ea49SDavid Gibson 
8217388efafSDavid Gibson     if (spapr_cpu->vpa_addr) {
8229b00ea49SDavid Gibson         reg.id = KVM_REG_PPC_VPA_ADDR;
8237388efafSDavid Gibson         reg.addr = (uintptr_t)&spapr_cpu->vpa_addr;
8249b00ea49SDavid Gibson         ret = kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, &reg);
8259b00ea49SDavid Gibson         if (ret < 0) {
8268d83cbf1SGreg Kurz             trace_kvm_failed_vpa_addr_set(strerror(errno));
8279b00ea49SDavid Gibson             return ret;
8289b00ea49SDavid Gibson         }
8299b00ea49SDavid Gibson     }
8309b00ea49SDavid Gibson 
8317388efafSDavid Gibson     assert((uintptr_t)&spapr_cpu->slb_shadow_size
8327388efafSDavid Gibson            == ((uintptr_t)&spapr_cpu->slb_shadow_addr + 8));
8339b00ea49SDavid Gibson     reg.id = KVM_REG_PPC_VPA_SLB;
8347388efafSDavid Gibson     reg.addr = (uintptr_t)&spapr_cpu->slb_shadow_addr;
8359b00ea49SDavid Gibson     ret = kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, &reg);
8369b00ea49SDavid Gibson     if (ret < 0) {
8378d83cbf1SGreg Kurz         trace_kvm_failed_slb_set(strerror(errno));
8389b00ea49SDavid Gibson         return ret;
8399b00ea49SDavid Gibson     }
8409b00ea49SDavid Gibson 
8417388efafSDavid Gibson     assert((uintptr_t)&spapr_cpu->dtl_size
8427388efafSDavid Gibson            == ((uintptr_t)&spapr_cpu->dtl_addr + 8));
8439b00ea49SDavid Gibson     reg.id = KVM_REG_PPC_VPA_DTL;
8447388efafSDavid Gibson     reg.addr = (uintptr_t)&spapr_cpu->dtl_addr;
8459b00ea49SDavid Gibson     ret = kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, &reg);
8469b00ea49SDavid Gibson     if (ret < 0) {
8478d83cbf1SGreg Kurz         trace_kvm_failed_dtl_set(strerror(errno));
8489b00ea49SDavid Gibson         return ret;
8499b00ea49SDavid Gibson     }
8509b00ea49SDavid Gibson 
8517388efafSDavid Gibson     if (!spapr_cpu->vpa_addr) {
8529b00ea49SDavid Gibson         reg.id = KVM_REG_PPC_VPA_ADDR;
8537388efafSDavid Gibson         reg.addr = (uintptr_t)&spapr_cpu->vpa_addr;
8549b00ea49SDavid Gibson         ret = kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, &reg);
8559b00ea49SDavid Gibson         if (ret < 0) {
8568d83cbf1SGreg Kurz             trace_kvm_failed_null_vpa_addr_set(strerror(errno));
8579b00ea49SDavid Gibson             return ret;
8589b00ea49SDavid Gibson         }
8599b00ea49SDavid Gibson     }
8609b00ea49SDavid Gibson 
8619b00ea49SDavid Gibson     return 0;
8629b00ea49SDavid Gibson }
8639b00ea49SDavid Gibson #endif /* TARGET_PPC64 */
8649b00ea49SDavid Gibson 
865e5c0d3ceSDavid Gibson int kvmppc_put_books_sregs(PowerPCCPU *cpu)
866a7a00a72SDavid Gibson {
867a7a00a72SDavid Gibson     CPUPPCState *env = &cpu->env;
868a7a00a72SDavid Gibson     struct kvm_sregs sregs;
869a7a00a72SDavid Gibson     int i;
870a7a00a72SDavid Gibson 
871a7a00a72SDavid Gibson     sregs.pvr = env->spr[SPR_PVR];
872a7a00a72SDavid Gibson 
8731ec26c75SGreg Kurz     if (cpu->vhyp) {
8741ec26c75SGreg Kurz         PPCVirtualHypervisorClass *vhc =
8751ec26c75SGreg Kurz             PPC_VIRTUAL_HYPERVISOR_GET_CLASS(cpu->vhyp);
8761ec26c75SGreg Kurz         sregs.u.s.sdr1 = vhc->encode_hpt_for_kvm_pr(cpu->vhyp);
8771ec26c75SGreg Kurz     } else {
878a7a00a72SDavid Gibson         sregs.u.s.sdr1 = env->spr[SPR_SDR1];
8791ec26c75SGreg Kurz     }
880a7a00a72SDavid Gibson 
881a7a00a72SDavid Gibson     /* Sync SLB */
882a7a00a72SDavid Gibson #ifdef TARGET_PPC64
883a7a00a72SDavid Gibson     for (i = 0; i < ARRAY_SIZE(env->slb); i++) {
884a7a00a72SDavid Gibson         sregs.u.s.ppc64.slb[i].slbe = env->slb[i].esid;
885a7a00a72SDavid Gibson         if (env->slb[i].esid & SLB_ESID_V) {
886a7a00a72SDavid Gibson             sregs.u.s.ppc64.slb[i].slbe |= i;
887a7a00a72SDavid Gibson         }
888a7a00a72SDavid Gibson         sregs.u.s.ppc64.slb[i].slbv = env->slb[i].vsid;
889a7a00a72SDavid Gibson     }
890a7a00a72SDavid Gibson #endif
891a7a00a72SDavid Gibson 
892a7a00a72SDavid Gibson     /* Sync SRs */
893a7a00a72SDavid Gibson     for (i = 0; i < 16; i++) {
894a7a00a72SDavid Gibson         sregs.u.s.ppc32.sr[i] = env->sr[i];
895a7a00a72SDavid Gibson     }
896a7a00a72SDavid Gibson 
897a7a00a72SDavid Gibson     /* Sync BATs */
898a7a00a72SDavid Gibson     for (i = 0; i < 8; i++) {
899a7a00a72SDavid Gibson         /* Beware. We have to swap upper and lower bits here */
900a7a00a72SDavid Gibson         sregs.u.s.ppc32.dbat[i] = ((uint64_t)env->DBAT[0][i] << 32)
901a7a00a72SDavid Gibson             | env->DBAT[1][i];
902a7a00a72SDavid Gibson         sregs.u.s.ppc32.ibat[i] = ((uint64_t)env->IBAT[0][i] << 32)
903a7a00a72SDavid Gibson             | env->IBAT[1][i];
904a7a00a72SDavid Gibson     }
905a7a00a72SDavid Gibson 
906a7a00a72SDavid Gibson     return kvm_vcpu_ioctl(CPU(cpu), KVM_SET_SREGS, &sregs);
907a7a00a72SDavid Gibson }
908a7a00a72SDavid Gibson 
90920d695a9SAndreas Färber int kvm_arch_put_registers(CPUState *cs, int level)
910d76d1650Saurel32 {
91120d695a9SAndreas Färber     PowerPCCPU *cpu = POWERPC_CPU(cs);
91220d695a9SAndreas Färber     CPUPPCState *env = &cpu->env;
913d76d1650Saurel32     struct kvm_regs regs;
914d76d1650Saurel32     int ret;
915d76d1650Saurel32     int i;
916d76d1650Saurel32 
9171bc22652SAndreas Färber     ret = kvm_vcpu_ioctl(cs, KVM_GET_REGS, &regs);
9181bc22652SAndreas Färber     if (ret < 0) {
919d76d1650Saurel32         return ret;
9201bc22652SAndreas Färber     }
921d76d1650Saurel32 
922d76d1650Saurel32     regs.ctr = env->ctr;
923d76d1650Saurel32     regs.lr  = env->lr;
924da91a00fSRichard Henderson     regs.xer = cpu_read_xer(env);
925d76d1650Saurel32     regs.msr = env->msr;
926d76d1650Saurel32     regs.pc = env->nip;
927d76d1650Saurel32 
928d76d1650Saurel32     regs.srr0 = env->spr[SPR_SRR0];
929d76d1650Saurel32     regs.srr1 = env->spr[SPR_SRR1];
930d76d1650Saurel32 
931d76d1650Saurel32     regs.sprg0 = env->spr[SPR_SPRG0];
932d76d1650Saurel32     regs.sprg1 = env->spr[SPR_SPRG1];
933d76d1650Saurel32     regs.sprg2 = env->spr[SPR_SPRG2];
934d76d1650Saurel32     regs.sprg3 = env->spr[SPR_SPRG3];
935d76d1650Saurel32     regs.sprg4 = env->spr[SPR_SPRG4];
936d76d1650Saurel32     regs.sprg5 = env->spr[SPR_SPRG5];
937d76d1650Saurel32     regs.sprg6 = env->spr[SPR_SPRG6];
938d76d1650Saurel32     regs.sprg7 = env->spr[SPR_SPRG7];
939d76d1650Saurel32 
94090dc8812SScott Wood     regs.pid = env->spr[SPR_BOOKE_PID];
94190dc8812SScott Wood 
942c995e942SDavid Gibson     for (i = 0; i < 32; i++) {
943d76d1650Saurel32         regs.gpr[i] = env->gpr[i];
944c995e942SDavid Gibson     }
945d76d1650Saurel32 
9464bddaf55SAlexey Kardashevskiy     regs.cr = 0;
9474bddaf55SAlexey Kardashevskiy     for (i = 0; i < 8; i++) {
9484bddaf55SAlexey Kardashevskiy         regs.cr |= (env->crf[i] & 15) << (4 * (7 - i));
9494bddaf55SAlexey Kardashevskiy     }
9504bddaf55SAlexey Kardashevskiy 
9511bc22652SAndreas Färber     ret = kvm_vcpu_ioctl(cs, KVM_SET_REGS, &regs);
952c995e942SDavid Gibson     if (ret < 0) {
953d76d1650Saurel32         return ret;
954c995e942SDavid Gibson     }
955d76d1650Saurel32 
95670b79849SDavid Gibson     kvm_put_fp(cs);
95770b79849SDavid Gibson 
95893dd5e85SScott Wood     if (env->tlb_dirty) {
9591bc22652SAndreas Färber         kvm_sw_tlb_put(cpu);
96093dd5e85SScott Wood         env->tlb_dirty = false;
96193dd5e85SScott Wood     }
96293dd5e85SScott Wood 
963f1af19d7SDavid Gibson     if (cap_segstate && (level >= KVM_PUT_RESET_STATE)) {
964a7a00a72SDavid Gibson         ret = kvmppc_put_books_sregs(cpu);
965a7a00a72SDavid Gibson         if (ret < 0) {
966f1af19d7SDavid Gibson             return ret;
967f1af19d7SDavid Gibson         }
968f1af19d7SDavid Gibson     }
969f1af19d7SDavid Gibson 
970f1af19d7SDavid Gibson     if (cap_hior && (level >= KVM_PUT_RESET_STATE)) {
971d67d40eaSDavid Gibson         kvm_put_one_spr(cs, KVM_REG_PPC_HIOR, SPR_HIOR);
972d67d40eaSDavid Gibson     }
973f1af19d7SDavid Gibson 
974d67d40eaSDavid Gibson     if (cap_one_reg) {
975d67d40eaSDavid Gibson         int i;
976d67d40eaSDavid Gibson 
977c995e942SDavid Gibson         /*
978c995e942SDavid Gibson          * We deliberately ignore errors here, for kernels which have
979d67d40eaSDavid Gibson          * the ONE_REG calls, but don't support the specific
980d67d40eaSDavid Gibson          * registers, there's a reasonable chance things will still
981c995e942SDavid Gibson          * work, at least until we try to migrate.
982c995e942SDavid Gibson          */
983d67d40eaSDavid Gibson         for (i = 0; i < 1024; i++) {
984d67d40eaSDavid Gibson             uint64_t id = env->spr_cb[i].one_reg_id;
985d67d40eaSDavid Gibson 
986d67d40eaSDavid Gibson             if (id != 0) {
987d67d40eaSDavid Gibson                 kvm_put_one_spr(cs, id, i);
988d67d40eaSDavid Gibson             }
989f1af19d7SDavid Gibson         }
9909b00ea49SDavid Gibson 
9919b00ea49SDavid Gibson #ifdef TARGET_PPC64
99280b3f79bSAlexey Kardashevskiy         if (msr_ts) {
99380b3f79bSAlexey Kardashevskiy             for (i = 0; i < ARRAY_SIZE(env->tm_gpr); i++) {
99480b3f79bSAlexey Kardashevskiy                 kvm_set_one_reg(cs, KVM_REG_PPC_TM_GPR(i), &env->tm_gpr[i]);
99580b3f79bSAlexey Kardashevskiy             }
99680b3f79bSAlexey Kardashevskiy             for (i = 0; i < ARRAY_SIZE(env->tm_vsr); i++) {
99780b3f79bSAlexey Kardashevskiy                 kvm_set_one_reg(cs, KVM_REG_PPC_TM_VSR(i), &env->tm_vsr[i]);
99880b3f79bSAlexey Kardashevskiy             }
99980b3f79bSAlexey Kardashevskiy             kvm_set_one_reg(cs, KVM_REG_PPC_TM_CR, &env->tm_cr);
100080b3f79bSAlexey Kardashevskiy             kvm_set_one_reg(cs, KVM_REG_PPC_TM_LR, &env->tm_lr);
100180b3f79bSAlexey Kardashevskiy             kvm_set_one_reg(cs, KVM_REG_PPC_TM_CTR, &env->tm_ctr);
100280b3f79bSAlexey Kardashevskiy             kvm_set_one_reg(cs, KVM_REG_PPC_TM_FPSCR, &env->tm_fpscr);
100380b3f79bSAlexey Kardashevskiy             kvm_set_one_reg(cs, KVM_REG_PPC_TM_AMR, &env->tm_amr);
100480b3f79bSAlexey Kardashevskiy             kvm_set_one_reg(cs, KVM_REG_PPC_TM_PPR, &env->tm_ppr);
100580b3f79bSAlexey Kardashevskiy             kvm_set_one_reg(cs, KVM_REG_PPC_TM_VRSAVE, &env->tm_vrsave);
100680b3f79bSAlexey Kardashevskiy             kvm_set_one_reg(cs, KVM_REG_PPC_TM_VSCR, &env->tm_vscr);
100780b3f79bSAlexey Kardashevskiy             kvm_set_one_reg(cs, KVM_REG_PPC_TM_DSCR, &env->tm_dscr);
100880b3f79bSAlexey Kardashevskiy             kvm_set_one_reg(cs, KVM_REG_PPC_TM_TAR, &env->tm_tar);
100980b3f79bSAlexey Kardashevskiy         }
101080b3f79bSAlexey Kardashevskiy 
10119b00ea49SDavid Gibson         if (cap_papr) {
10129b00ea49SDavid Gibson             if (kvm_put_vpa(cs) < 0) {
10138d83cbf1SGreg Kurz                 trace_kvm_failed_put_vpa();
10149b00ea49SDavid Gibson             }
10159b00ea49SDavid Gibson         }
101698a8b524SAlexey Kardashevskiy 
101798a8b524SAlexey Kardashevskiy         kvm_set_one_reg(cs, KVM_REG_PPC_TB_OFFSET, &env->tb_env->tb_offset);
10189b00ea49SDavid Gibson #endif /* TARGET_PPC64 */
1019f1af19d7SDavid Gibson     }
1020f1af19d7SDavid Gibson 
1021d76d1650Saurel32     return ret;
1022d76d1650Saurel32 }
1023d76d1650Saurel32 
1024c371c2e3SBharat Bhushan static void kvm_sync_excp(CPUPPCState *env, int vector, int ivor)
1025c371c2e3SBharat Bhushan {
1026c371c2e3SBharat Bhushan      env->excp_vectors[vector] = env->spr[ivor] + env->spr[SPR_BOOKE_IVPR];
1027c371c2e3SBharat Bhushan }
1028c371c2e3SBharat Bhushan 
1029a7a00a72SDavid Gibson static int kvmppc_get_booke_sregs(PowerPCCPU *cpu)
1030d76d1650Saurel32 {
103120d695a9SAndreas Färber     CPUPPCState *env = &cpu->env;
1032ba5e5090SAlexander Graf     struct kvm_sregs sregs;
1033a7a00a72SDavid Gibson     int ret;
1034d76d1650Saurel32 
1035a7a00a72SDavid Gibson     ret = kvm_vcpu_ioctl(CPU(cpu), KVM_GET_SREGS, &sregs);
103690dc8812SScott Wood     if (ret < 0) {
103790dc8812SScott Wood         return ret;
103890dc8812SScott Wood     }
103990dc8812SScott Wood 
104090dc8812SScott Wood     if (sregs.u.e.features & KVM_SREGS_E_BASE) {
104190dc8812SScott Wood         env->spr[SPR_BOOKE_CSRR0] = sregs.u.e.csrr0;
104290dc8812SScott Wood         env->spr[SPR_BOOKE_CSRR1] = sregs.u.e.csrr1;
104390dc8812SScott Wood         env->spr[SPR_BOOKE_ESR] = sregs.u.e.esr;
104490dc8812SScott Wood         env->spr[SPR_BOOKE_DEAR] = sregs.u.e.dear;
104590dc8812SScott Wood         env->spr[SPR_BOOKE_MCSR] = sregs.u.e.mcsr;
104690dc8812SScott Wood         env->spr[SPR_BOOKE_TSR] = sregs.u.e.tsr;
104790dc8812SScott Wood         env->spr[SPR_BOOKE_TCR] = sregs.u.e.tcr;
104890dc8812SScott Wood         env->spr[SPR_DECR] = sregs.u.e.dec;
104990dc8812SScott Wood         env->spr[SPR_TBL] = sregs.u.e.tb & 0xffffffff;
105090dc8812SScott Wood         env->spr[SPR_TBU] = sregs.u.e.tb >> 32;
105190dc8812SScott Wood         env->spr[SPR_VRSAVE] = sregs.u.e.vrsave;
105290dc8812SScott Wood     }
105390dc8812SScott Wood 
105490dc8812SScott Wood     if (sregs.u.e.features & KVM_SREGS_E_ARCH206) {
105590dc8812SScott Wood         env->spr[SPR_BOOKE_PIR] = sregs.u.e.pir;
105690dc8812SScott Wood         env->spr[SPR_BOOKE_MCSRR0] = sregs.u.e.mcsrr0;
105790dc8812SScott Wood         env->spr[SPR_BOOKE_MCSRR1] = sregs.u.e.mcsrr1;
105890dc8812SScott Wood         env->spr[SPR_BOOKE_DECAR] = sregs.u.e.decar;
105990dc8812SScott Wood         env->spr[SPR_BOOKE_IVPR] = sregs.u.e.ivpr;
106090dc8812SScott Wood     }
106190dc8812SScott Wood 
106290dc8812SScott Wood     if (sregs.u.e.features & KVM_SREGS_E_64) {
106390dc8812SScott Wood         env->spr[SPR_BOOKE_EPCR] = sregs.u.e.epcr;
106490dc8812SScott Wood     }
106590dc8812SScott Wood 
106690dc8812SScott Wood     if (sregs.u.e.features & KVM_SREGS_E_SPRG8) {
106790dc8812SScott Wood         env->spr[SPR_BOOKE_SPRG8] = sregs.u.e.sprg8;
106890dc8812SScott Wood     }
106990dc8812SScott Wood 
107090dc8812SScott Wood     if (sregs.u.e.features & KVM_SREGS_E_IVOR) {
107190dc8812SScott Wood         env->spr[SPR_BOOKE_IVOR0] = sregs.u.e.ivor_low[0];
1072c371c2e3SBharat Bhushan         kvm_sync_excp(env, POWERPC_EXCP_CRITICAL,  SPR_BOOKE_IVOR0);
107390dc8812SScott Wood         env->spr[SPR_BOOKE_IVOR1] = sregs.u.e.ivor_low[1];
1074c371c2e3SBharat Bhushan         kvm_sync_excp(env, POWERPC_EXCP_MCHECK,  SPR_BOOKE_IVOR1);
107590dc8812SScott Wood         env->spr[SPR_BOOKE_IVOR2] = sregs.u.e.ivor_low[2];
1076c371c2e3SBharat Bhushan         kvm_sync_excp(env, POWERPC_EXCP_DSI,  SPR_BOOKE_IVOR2);
107790dc8812SScott Wood         env->spr[SPR_BOOKE_IVOR3] = sregs.u.e.ivor_low[3];
1078c371c2e3SBharat Bhushan         kvm_sync_excp(env, POWERPC_EXCP_ISI,  SPR_BOOKE_IVOR3);
107990dc8812SScott Wood         env->spr[SPR_BOOKE_IVOR4] = sregs.u.e.ivor_low[4];
1080c371c2e3SBharat Bhushan         kvm_sync_excp(env, POWERPC_EXCP_EXTERNAL,  SPR_BOOKE_IVOR4);
108190dc8812SScott Wood         env->spr[SPR_BOOKE_IVOR5] = sregs.u.e.ivor_low[5];
1082c371c2e3SBharat Bhushan         kvm_sync_excp(env, POWERPC_EXCP_ALIGN,  SPR_BOOKE_IVOR5);
108390dc8812SScott Wood         env->spr[SPR_BOOKE_IVOR6] = sregs.u.e.ivor_low[6];
1084c371c2e3SBharat Bhushan         kvm_sync_excp(env, POWERPC_EXCP_PROGRAM,  SPR_BOOKE_IVOR6);
108590dc8812SScott Wood         env->spr[SPR_BOOKE_IVOR7] = sregs.u.e.ivor_low[7];
1086c371c2e3SBharat Bhushan         kvm_sync_excp(env, POWERPC_EXCP_FPU,  SPR_BOOKE_IVOR7);
108790dc8812SScott Wood         env->spr[SPR_BOOKE_IVOR8] = sregs.u.e.ivor_low[8];
1088c371c2e3SBharat Bhushan         kvm_sync_excp(env, POWERPC_EXCP_SYSCALL,  SPR_BOOKE_IVOR8);
108990dc8812SScott Wood         env->spr[SPR_BOOKE_IVOR9] = sregs.u.e.ivor_low[9];
1090c371c2e3SBharat Bhushan         kvm_sync_excp(env, POWERPC_EXCP_APU,  SPR_BOOKE_IVOR9);
109190dc8812SScott Wood         env->spr[SPR_BOOKE_IVOR10] = sregs.u.e.ivor_low[10];
1092c371c2e3SBharat Bhushan         kvm_sync_excp(env, POWERPC_EXCP_DECR,  SPR_BOOKE_IVOR10);
109390dc8812SScott Wood         env->spr[SPR_BOOKE_IVOR11] = sregs.u.e.ivor_low[11];
1094c371c2e3SBharat Bhushan         kvm_sync_excp(env, POWERPC_EXCP_FIT,  SPR_BOOKE_IVOR11);
109590dc8812SScott Wood         env->spr[SPR_BOOKE_IVOR12] = sregs.u.e.ivor_low[12];
1096c371c2e3SBharat Bhushan         kvm_sync_excp(env, POWERPC_EXCP_WDT,  SPR_BOOKE_IVOR12);
109790dc8812SScott Wood         env->spr[SPR_BOOKE_IVOR13] = sregs.u.e.ivor_low[13];
1098c371c2e3SBharat Bhushan         kvm_sync_excp(env, POWERPC_EXCP_DTLB,  SPR_BOOKE_IVOR13);
109990dc8812SScott Wood         env->spr[SPR_BOOKE_IVOR14] = sregs.u.e.ivor_low[14];
1100c371c2e3SBharat Bhushan         kvm_sync_excp(env, POWERPC_EXCP_ITLB,  SPR_BOOKE_IVOR14);
110190dc8812SScott Wood         env->spr[SPR_BOOKE_IVOR15] = sregs.u.e.ivor_low[15];
1102c371c2e3SBharat Bhushan         kvm_sync_excp(env, POWERPC_EXCP_DEBUG,  SPR_BOOKE_IVOR15);
110390dc8812SScott Wood 
110490dc8812SScott Wood         if (sregs.u.e.features & KVM_SREGS_E_SPE) {
110590dc8812SScott Wood             env->spr[SPR_BOOKE_IVOR32] = sregs.u.e.ivor_high[0];
1106c371c2e3SBharat Bhushan             kvm_sync_excp(env, POWERPC_EXCP_SPEU,  SPR_BOOKE_IVOR32);
110790dc8812SScott Wood             env->spr[SPR_BOOKE_IVOR33] = sregs.u.e.ivor_high[1];
1108c371c2e3SBharat Bhushan             kvm_sync_excp(env, POWERPC_EXCP_EFPDI,  SPR_BOOKE_IVOR33);
110990dc8812SScott Wood             env->spr[SPR_BOOKE_IVOR34] = sregs.u.e.ivor_high[2];
1110c371c2e3SBharat Bhushan             kvm_sync_excp(env, POWERPC_EXCP_EFPRI,  SPR_BOOKE_IVOR34);
111190dc8812SScott Wood         }
111290dc8812SScott Wood 
111390dc8812SScott Wood         if (sregs.u.e.features & KVM_SREGS_E_PM) {
111490dc8812SScott Wood             env->spr[SPR_BOOKE_IVOR35] = sregs.u.e.ivor_high[3];
1115c371c2e3SBharat Bhushan             kvm_sync_excp(env, POWERPC_EXCP_EPERFM,  SPR_BOOKE_IVOR35);
111690dc8812SScott Wood         }
111790dc8812SScott Wood 
111890dc8812SScott Wood         if (sregs.u.e.features & KVM_SREGS_E_PC) {
111990dc8812SScott Wood             env->spr[SPR_BOOKE_IVOR36] = sregs.u.e.ivor_high[4];
1120c371c2e3SBharat Bhushan             kvm_sync_excp(env, POWERPC_EXCP_DOORI,  SPR_BOOKE_IVOR36);
112190dc8812SScott Wood             env->spr[SPR_BOOKE_IVOR37] = sregs.u.e.ivor_high[5];
1122c371c2e3SBharat Bhushan             kvm_sync_excp(env, POWERPC_EXCP_DOORCI, SPR_BOOKE_IVOR37);
112390dc8812SScott Wood         }
112490dc8812SScott Wood     }
112590dc8812SScott Wood 
112690dc8812SScott Wood     if (sregs.u.e.features & KVM_SREGS_E_ARCH206_MMU) {
112790dc8812SScott Wood         env->spr[SPR_BOOKE_MAS0] = sregs.u.e.mas0;
112890dc8812SScott Wood         env->spr[SPR_BOOKE_MAS1] = sregs.u.e.mas1;
112990dc8812SScott Wood         env->spr[SPR_BOOKE_MAS2] = sregs.u.e.mas2;
113090dc8812SScott Wood         env->spr[SPR_BOOKE_MAS3] = sregs.u.e.mas7_3 & 0xffffffff;
113190dc8812SScott Wood         env->spr[SPR_BOOKE_MAS4] = sregs.u.e.mas4;
113290dc8812SScott Wood         env->spr[SPR_BOOKE_MAS6] = sregs.u.e.mas6;
113390dc8812SScott Wood         env->spr[SPR_BOOKE_MAS7] = sregs.u.e.mas7_3 >> 32;
113490dc8812SScott Wood         env->spr[SPR_MMUCFG] = sregs.u.e.mmucfg;
113590dc8812SScott Wood         env->spr[SPR_BOOKE_TLB0CFG] = sregs.u.e.tlbcfg[0];
113690dc8812SScott Wood         env->spr[SPR_BOOKE_TLB1CFG] = sregs.u.e.tlbcfg[1];
113790dc8812SScott Wood     }
113890dc8812SScott Wood 
113990dc8812SScott Wood     if (sregs.u.e.features & KVM_SREGS_EXP) {
114090dc8812SScott Wood         env->spr[SPR_BOOKE_EPR] = sregs.u.e.epr;
114190dc8812SScott Wood     }
114290dc8812SScott Wood 
114390dc8812SScott Wood     if (sregs.u.e.features & KVM_SREGS_E_PD) {
114490dc8812SScott Wood         env->spr[SPR_BOOKE_EPLC] = sregs.u.e.eplc;
114590dc8812SScott Wood         env->spr[SPR_BOOKE_EPSC] = sregs.u.e.epsc;
114690dc8812SScott Wood     }
114790dc8812SScott Wood 
114890dc8812SScott Wood     if (sregs.u.e.impl_id == KVM_SREGS_E_IMPL_FSL) {
114990dc8812SScott Wood         env->spr[SPR_E500_SVR] = sregs.u.e.impl.fsl.svr;
115090dc8812SScott Wood         env->spr[SPR_Exxx_MCAR] = sregs.u.e.impl.fsl.mcar;
115190dc8812SScott Wood         env->spr[SPR_HID0] = sregs.u.e.impl.fsl.hid0;
115290dc8812SScott Wood 
115390dc8812SScott Wood         if (sregs.u.e.impl.fsl.features & KVM_SREGS_E_FSL_PIDn) {
115490dc8812SScott Wood             env->spr[SPR_BOOKE_PID1] = sregs.u.e.impl.fsl.pid1;
115590dc8812SScott Wood             env->spr[SPR_BOOKE_PID2] = sregs.u.e.impl.fsl.pid2;
115690dc8812SScott Wood         }
115790dc8812SScott Wood     }
1158a7a00a72SDavid Gibson 
1159a7a00a72SDavid Gibson     return 0;
1160fafc0b6aSAlexander Graf }
116190dc8812SScott Wood 
1162a7a00a72SDavid Gibson static int kvmppc_get_books_sregs(PowerPCCPU *cpu)
1163a7a00a72SDavid Gibson {
1164a7a00a72SDavid Gibson     CPUPPCState *env = &cpu->env;
1165a7a00a72SDavid Gibson     struct kvm_sregs sregs;
1166a7a00a72SDavid Gibson     int ret;
1167a7a00a72SDavid Gibson     int i;
1168a7a00a72SDavid Gibson 
1169a7a00a72SDavid Gibson     ret = kvm_vcpu_ioctl(CPU(cpu), KVM_GET_SREGS, &sregs);
117090dc8812SScott Wood     if (ret < 0) {
117190dc8812SScott Wood         return ret;
117290dc8812SScott Wood     }
117390dc8812SScott Wood 
1174e57ca75cSDavid Gibson     if (!cpu->vhyp) {
1175bb593904SDavid Gibson         ppc_store_sdr1(env, sregs.u.s.sdr1);
1176f3c75d42SAneesh Kumar K.V     }
1177ba5e5090SAlexander Graf 
1178ba5e5090SAlexander Graf     /* Sync SLB */
117982c09f2fSAlexander Graf #ifdef TARGET_PPC64
11804b4d4a21SAneesh Kumar K.V     /*
11814b4d4a21SAneesh Kumar K.V      * The packed SLB array we get from KVM_GET_SREGS only contains
1182a7a00a72SDavid Gibson      * information about valid entries. So we flush our internal copy
1183a7a00a72SDavid Gibson      * to get rid of stale ones, then put all valid SLB entries back
1184a7a00a72SDavid Gibson      * in.
11854b4d4a21SAneesh Kumar K.V      */
11864b4d4a21SAneesh Kumar K.V     memset(env->slb, 0, sizeof(env->slb));
1187d83af167SAneesh Kumar K.V     for (i = 0; i < ARRAY_SIZE(env->slb); i++) {
11884b4d4a21SAneesh Kumar K.V         target_ulong rb = sregs.u.s.ppc64.slb[i].slbe;
11894b4d4a21SAneesh Kumar K.V         target_ulong rs = sregs.u.s.ppc64.slb[i].slbv;
11904b4d4a21SAneesh Kumar K.V         /*
11914b4d4a21SAneesh Kumar K.V          * Only restore valid entries
11924b4d4a21SAneesh Kumar K.V          */
11934b4d4a21SAneesh Kumar K.V         if (rb & SLB_ESID_V) {
1194bcd81230SDavid Gibson             ppc_store_slb(cpu, rb & 0xfff, rb & ~0xfffULL, rs);
11954b4d4a21SAneesh Kumar K.V         }
1196ba5e5090SAlexander Graf     }
119782c09f2fSAlexander Graf #endif
1198ba5e5090SAlexander Graf 
1199ba5e5090SAlexander Graf     /* Sync SRs */
1200ba5e5090SAlexander Graf     for (i = 0; i < 16; i++) {
1201ba5e5090SAlexander Graf         env->sr[i] = sregs.u.s.ppc32.sr[i];
1202ba5e5090SAlexander Graf     }
1203ba5e5090SAlexander Graf 
1204ba5e5090SAlexander Graf     /* Sync BATs */
1205ba5e5090SAlexander Graf     for (i = 0; i < 8; i++) {
1206ba5e5090SAlexander Graf         env->DBAT[0][i] = sregs.u.s.ppc32.dbat[i] & 0xffffffff;
1207ba5e5090SAlexander Graf         env->DBAT[1][i] = sregs.u.s.ppc32.dbat[i] >> 32;
1208ba5e5090SAlexander Graf         env->IBAT[0][i] = sregs.u.s.ppc32.ibat[i] & 0xffffffff;
1209ba5e5090SAlexander Graf         env->IBAT[1][i] = sregs.u.s.ppc32.ibat[i] >> 32;
1210ba5e5090SAlexander Graf     }
1211a7a00a72SDavid Gibson 
1212a7a00a72SDavid Gibson     return 0;
1213a7a00a72SDavid Gibson }
1214a7a00a72SDavid Gibson 
1215a7a00a72SDavid Gibson int kvm_arch_get_registers(CPUState *cs)
1216a7a00a72SDavid Gibson {
1217a7a00a72SDavid Gibson     PowerPCCPU *cpu = POWERPC_CPU(cs);
1218a7a00a72SDavid Gibson     CPUPPCState *env = &cpu->env;
1219a7a00a72SDavid Gibson     struct kvm_regs regs;
1220a7a00a72SDavid Gibson     uint32_t cr;
1221a7a00a72SDavid Gibson     int i, ret;
1222a7a00a72SDavid Gibson 
1223a7a00a72SDavid Gibson     ret = kvm_vcpu_ioctl(cs, KVM_GET_REGS, &regs);
1224c995e942SDavid Gibson     if (ret < 0) {
1225a7a00a72SDavid Gibson         return ret;
1226c995e942SDavid Gibson     }
1227a7a00a72SDavid Gibson 
1228a7a00a72SDavid Gibson     cr = regs.cr;
1229a7a00a72SDavid Gibson     for (i = 7; i >= 0; i--) {
1230a7a00a72SDavid Gibson         env->crf[i] = cr & 15;
1231a7a00a72SDavid Gibson         cr >>= 4;
1232a7a00a72SDavid Gibson     }
1233a7a00a72SDavid Gibson 
1234a7a00a72SDavid Gibson     env->ctr = regs.ctr;
1235a7a00a72SDavid Gibson     env->lr = regs.lr;
1236a7a00a72SDavid Gibson     cpu_write_xer(env, regs.xer);
1237a7a00a72SDavid Gibson     env->msr = regs.msr;
1238a7a00a72SDavid Gibson     env->nip = regs.pc;
1239a7a00a72SDavid Gibson 
1240a7a00a72SDavid Gibson     env->spr[SPR_SRR0] = regs.srr0;
1241a7a00a72SDavid Gibson     env->spr[SPR_SRR1] = regs.srr1;
1242a7a00a72SDavid Gibson 
1243a7a00a72SDavid Gibson     env->spr[SPR_SPRG0] = regs.sprg0;
1244a7a00a72SDavid Gibson     env->spr[SPR_SPRG1] = regs.sprg1;
1245a7a00a72SDavid Gibson     env->spr[SPR_SPRG2] = regs.sprg2;
1246a7a00a72SDavid Gibson     env->spr[SPR_SPRG3] = regs.sprg3;
1247a7a00a72SDavid Gibson     env->spr[SPR_SPRG4] = regs.sprg4;
1248a7a00a72SDavid Gibson     env->spr[SPR_SPRG5] = regs.sprg5;
1249a7a00a72SDavid Gibson     env->spr[SPR_SPRG6] = regs.sprg6;
1250a7a00a72SDavid Gibson     env->spr[SPR_SPRG7] = regs.sprg7;
1251a7a00a72SDavid Gibson 
1252a7a00a72SDavid Gibson     env->spr[SPR_BOOKE_PID] = regs.pid;
1253a7a00a72SDavid Gibson 
1254c995e942SDavid Gibson     for (i = 0; i < 32; i++) {
1255a7a00a72SDavid Gibson         env->gpr[i] = regs.gpr[i];
1256c995e942SDavid Gibson     }
1257a7a00a72SDavid Gibson 
1258a7a00a72SDavid Gibson     kvm_get_fp(cs);
1259a7a00a72SDavid Gibson 
1260a7a00a72SDavid Gibson     if (cap_booke_sregs) {
1261a7a00a72SDavid Gibson         ret = kvmppc_get_booke_sregs(cpu);
1262a7a00a72SDavid Gibson         if (ret < 0) {
1263a7a00a72SDavid Gibson             return ret;
1264a7a00a72SDavid Gibson         }
1265a7a00a72SDavid Gibson     }
1266a7a00a72SDavid Gibson 
1267a7a00a72SDavid Gibson     if (cap_segstate) {
1268a7a00a72SDavid Gibson         ret = kvmppc_get_books_sregs(cpu);
1269a7a00a72SDavid Gibson         if (ret < 0) {
1270a7a00a72SDavid Gibson             return ret;
1271a7a00a72SDavid Gibson         }
1272fafc0b6aSAlexander Graf     }
1273ba5e5090SAlexander Graf 
1274d67d40eaSDavid Gibson     if (cap_hior) {
1275d67d40eaSDavid Gibson         kvm_get_one_spr(cs, KVM_REG_PPC_HIOR, SPR_HIOR);
1276d67d40eaSDavid Gibson     }
1277d67d40eaSDavid Gibson 
1278d67d40eaSDavid Gibson     if (cap_one_reg) {
1279d67d40eaSDavid Gibson         int i;
1280d67d40eaSDavid Gibson 
1281c995e942SDavid Gibson         /*
1282c995e942SDavid Gibson          * We deliberately ignore errors here, for kernels which have
1283d67d40eaSDavid Gibson          * the ONE_REG calls, but don't support the specific
1284d67d40eaSDavid Gibson          * registers, there's a reasonable chance things will still
1285c995e942SDavid Gibson          * work, at least until we try to migrate.
1286c995e942SDavid Gibson          */
1287d67d40eaSDavid Gibson         for (i = 0; i < 1024; i++) {
1288d67d40eaSDavid Gibson             uint64_t id = env->spr_cb[i].one_reg_id;
1289d67d40eaSDavid Gibson 
1290d67d40eaSDavid Gibson             if (id != 0) {
1291d67d40eaSDavid Gibson                 kvm_get_one_spr(cs, id, i);
1292d67d40eaSDavid Gibson             }
1293d67d40eaSDavid Gibson         }
12949b00ea49SDavid Gibson 
12959b00ea49SDavid Gibson #ifdef TARGET_PPC64
129680b3f79bSAlexey Kardashevskiy         if (msr_ts) {
129780b3f79bSAlexey Kardashevskiy             for (i = 0; i < ARRAY_SIZE(env->tm_gpr); i++) {
129880b3f79bSAlexey Kardashevskiy                 kvm_get_one_reg(cs, KVM_REG_PPC_TM_GPR(i), &env->tm_gpr[i]);
129980b3f79bSAlexey Kardashevskiy             }
130080b3f79bSAlexey Kardashevskiy             for (i = 0; i < ARRAY_SIZE(env->tm_vsr); i++) {
130180b3f79bSAlexey Kardashevskiy                 kvm_get_one_reg(cs, KVM_REG_PPC_TM_VSR(i), &env->tm_vsr[i]);
130280b3f79bSAlexey Kardashevskiy             }
130380b3f79bSAlexey Kardashevskiy             kvm_get_one_reg(cs, KVM_REG_PPC_TM_CR, &env->tm_cr);
130480b3f79bSAlexey Kardashevskiy             kvm_get_one_reg(cs, KVM_REG_PPC_TM_LR, &env->tm_lr);
130580b3f79bSAlexey Kardashevskiy             kvm_get_one_reg(cs, KVM_REG_PPC_TM_CTR, &env->tm_ctr);
130680b3f79bSAlexey Kardashevskiy             kvm_get_one_reg(cs, KVM_REG_PPC_TM_FPSCR, &env->tm_fpscr);
130780b3f79bSAlexey Kardashevskiy             kvm_get_one_reg(cs, KVM_REG_PPC_TM_AMR, &env->tm_amr);
130880b3f79bSAlexey Kardashevskiy             kvm_get_one_reg(cs, KVM_REG_PPC_TM_PPR, &env->tm_ppr);
130980b3f79bSAlexey Kardashevskiy             kvm_get_one_reg(cs, KVM_REG_PPC_TM_VRSAVE, &env->tm_vrsave);
131080b3f79bSAlexey Kardashevskiy             kvm_get_one_reg(cs, KVM_REG_PPC_TM_VSCR, &env->tm_vscr);
131180b3f79bSAlexey Kardashevskiy             kvm_get_one_reg(cs, KVM_REG_PPC_TM_DSCR, &env->tm_dscr);
131280b3f79bSAlexey Kardashevskiy             kvm_get_one_reg(cs, KVM_REG_PPC_TM_TAR, &env->tm_tar);
131380b3f79bSAlexey Kardashevskiy         }
131480b3f79bSAlexey Kardashevskiy 
13159b00ea49SDavid Gibson         if (cap_papr) {
13169b00ea49SDavid Gibson             if (kvm_get_vpa(cs) < 0) {
13178d83cbf1SGreg Kurz                 trace_kvm_failed_get_vpa();
13189b00ea49SDavid Gibson             }
13199b00ea49SDavid Gibson         }
132098a8b524SAlexey Kardashevskiy 
132198a8b524SAlexey Kardashevskiy         kvm_get_one_reg(cs, KVM_REG_PPC_TB_OFFSET, &env->tb_env->tb_offset);
13229b00ea49SDavid Gibson #endif
1323d67d40eaSDavid Gibson     }
1324d67d40eaSDavid Gibson 
1325d76d1650Saurel32     return 0;
1326d76d1650Saurel32 }
1327d76d1650Saurel32 
13281bc22652SAndreas Färber int kvmppc_set_interrupt(PowerPCCPU *cpu, int irq, int level)
1329fc87e185SAlexander Graf {
1330fc87e185SAlexander Graf     unsigned virq = level ? KVM_INTERRUPT_SET_LEVEL : KVM_INTERRUPT_UNSET;
1331fc87e185SAlexander Graf 
1332fc87e185SAlexander Graf     if (irq != PPC_INTERRUPT_EXT) {
1333fc87e185SAlexander Graf         return 0;
1334fc87e185SAlexander Graf     }
1335fc87e185SAlexander Graf 
1336fc87e185SAlexander Graf     if (!kvm_enabled() || !cap_interrupt_unset || !cap_interrupt_level) {
1337fc87e185SAlexander Graf         return 0;
1338fc87e185SAlexander Graf     }
1339fc87e185SAlexander Graf 
13401bc22652SAndreas Färber     kvm_vcpu_ioctl(CPU(cpu), KVM_INTERRUPT, &virq);
1341fc87e185SAlexander Graf 
1342fc87e185SAlexander Graf     return 0;
1343fc87e185SAlexander Graf }
1344fc87e185SAlexander Graf 
1345a69dc537SThomas Huth #if defined(TARGET_PPC64)
134616415335SAlexander Graf #define PPC_INPUT_INT PPC970_INPUT_INT
134716415335SAlexander Graf #else
134816415335SAlexander Graf #define PPC_INPUT_INT PPC6xx_INPUT_INT
134916415335SAlexander Graf #endif
135016415335SAlexander Graf 
135120d695a9SAndreas Färber void kvm_arch_pre_run(CPUState *cs, struct kvm_run *run)
1352d76d1650Saurel32 {
135320d695a9SAndreas Färber     PowerPCCPU *cpu = POWERPC_CPU(cs);
135420d695a9SAndreas Färber     CPUPPCState *env = &cpu->env;
1355d76d1650Saurel32     int r;
1356d76d1650Saurel32     unsigned irq;
1357d76d1650Saurel32 
13584b8523eeSJan Kiszka     qemu_mutex_lock_iothread();
13594b8523eeSJan Kiszka 
1360c995e942SDavid Gibson     /*
1361c995e942SDavid Gibson      * PowerPC QEMU tracks the various core input pins (interrupt,
1362c995e942SDavid Gibson      * critical interrupt, reset, etc) in PPC-specific
1363c995e942SDavid Gibson      * env->irq_input_state.
1364c995e942SDavid Gibson      */
1365fc87e185SAlexander Graf     if (!cap_interrupt_level &&
1366fc87e185SAlexander Graf         run->ready_for_interrupt_injection &&
1367259186a7SAndreas Färber         (cs->interrupt_request & CPU_INTERRUPT_HARD) &&
136816415335SAlexander Graf         (env->irq_input_state & (1 << PPC_INPUT_INT)))
1369d76d1650Saurel32     {
1370c995e942SDavid Gibson         /*
1371c995e942SDavid Gibson          * For now KVM disregards the 'irq' argument. However, in the
1372c995e942SDavid Gibson          * future KVM could cache it in-kernel to avoid a heavyweight
1373c995e942SDavid Gibson          * exit when reading the UIC.
1374d76d1650Saurel32          */
1375fc87e185SAlexander Graf         irq = KVM_INTERRUPT_SET;
1376d76d1650Saurel32 
13778d83cbf1SGreg Kurz         trace_kvm_injected_interrupt(irq);
13781bc22652SAndreas Färber         r = kvm_vcpu_ioctl(cs, KVM_INTERRUPT, &irq);
137955e5c285SAndreas Färber         if (r < 0) {
138055e5c285SAndreas Färber             printf("cpu %d fail inject %x\n", cs->cpu_index, irq);
138155e5c285SAndreas Färber         }
1382c821c2bdSAlexander Graf 
1383c821c2bdSAlexander Graf         /* Always wake up soon in case the interrupt was level based */
1384bc72ad67SAlex Bligh         timer_mod(idle_timer, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) +
138573bcb24dSRutuja Shah                        (NANOSECONDS_PER_SECOND / 50));
1386d76d1650Saurel32     }
1387d76d1650Saurel32 
1388c995e942SDavid Gibson     /*
1389c995e942SDavid Gibson      * We don't know if there are more interrupts pending after
1390c995e942SDavid Gibson      * this. However, the guest will return to userspace in the course
1391c995e942SDavid Gibson      * of handling this one anyways, so we will get a chance to
1392c995e942SDavid Gibson      * deliver the rest.
1393c995e942SDavid Gibson      */
13944b8523eeSJan Kiszka 
13954b8523eeSJan Kiszka     qemu_mutex_unlock_iothread();
1396d76d1650Saurel32 }
1397d76d1650Saurel32 
13984c663752SPaolo Bonzini MemTxAttrs kvm_arch_post_run(CPUState *cs, struct kvm_run *run)
1399d76d1650Saurel32 {
14004c663752SPaolo Bonzini     return MEMTXATTRS_UNSPECIFIED;
1401d76d1650Saurel32 }
1402d76d1650Saurel32 
140320d695a9SAndreas Färber int kvm_arch_process_async_events(CPUState *cs)
14040af691d7SMarcelo Tosatti {
1405259186a7SAndreas Färber     return cs->halted;
14060af691d7SMarcelo Tosatti }
14070af691d7SMarcelo Tosatti 
1408259186a7SAndreas Färber static int kvmppc_handle_halt(PowerPCCPU *cpu)
1409d76d1650Saurel32 {
1410259186a7SAndreas Färber     CPUState *cs = CPU(cpu);
1411259186a7SAndreas Färber     CPUPPCState *env = &cpu->env;
1412259186a7SAndreas Färber 
1413259186a7SAndreas Färber     if (!(cs->interrupt_request & CPU_INTERRUPT_HARD) && (msr_ee)) {
1414259186a7SAndreas Färber         cs->halted = 1;
141527103424SAndreas Färber         cs->exception_index = EXCP_HLT;
1416d76d1650Saurel32     }
1417d76d1650Saurel32 
1418bb4ea393SJan Kiszka     return 0;
1419d76d1650Saurel32 }
1420d76d1650Saurel32 
1421d76d1650Saurel32 /* map dcr access to existing qemu dcr emulation */
1422c995e942SDavid Gibson static int kvmppc_handle_dcr_read(CPUPPCState *env,
1423c995e942SDavid Gibson                                   uint32_t dcrn, uint32_t *data)
1424d76d1650Saurel32 {
1425c995e942SDavid Gibson     if (ppc_dcr_read(env->dcr_env, dcrn, data) < 0) {
1426d76d1650Saurel32         fprintf(stderr, "Read to unhandled DCR (0x%x)\n", dcrn);
1427c995e942SDavid Gibson     }
1428d76d1650Saurel32 
1429bb4ea393SJan Kiszka     return 0;
1430d76d1650Saurel32 }
1431d76d1650Saurel32 
1432c995e942SDavid Gibson static int kvmppc_handle_dcr_write(CPUPPCState *env,
1433c995e942SDavid Gibson                                    uint32_t dcrn, uint32_t data)
1434d76d1650Saurel32 {
1435c995e942SDavid Gibson     if (ppc_dcr_write(env->dcr_env, dcrn, data) < 0) {
1436d76d1650Saurel32         fprintf(stderr, "Write to unhandled DCR (0x%x)\n", dcrn);
1437c995e942SDavid Gibson     }
1438d76d1650Saurel32 
1439bb4ea393SJan Kiszka     return 0;
1440d76d1650Saurel32 }
1441d76d1650Saurel32 
14428a0548f9SBharat Bhushan int kvm_arch_insert_sw_breakpoint(CPUState *cs, struct kvm_sw_breakpoint *bp)
14438a0548f9SBharat Bhushan {
14448a0548f9SBharat Bhushan     /* Mixed endian case is not handled */
14458a0548f9SBharat Bhushan     uint32_t sc = debug_inst_opcode;
14468a0548f9SBharat Bhushan 
14478a0548f9SBharat Bhushan     if (cpu_memory_rw_debug(cs, bp->pc, (uint8_t *)&bp->saved_insn,
14488a0548f9SBharat Bhushan                             sizeof(sc), 0) ||
14498a0548f9SBharat Bhushan         cpu_memory_rw_debug(cs, bp->pc, (uint8_t *)&sc, sizeof(sc), 1)) {
14508a0548f9SBharat Bhushan         return -EINVAL;
14518a0548f9SBharat Bhushan     }
14528a0548f9SBharat Bhushan 
14538a0548f9SBharat Bhushan     return 0;
14548a0548f9SBharat Bhushan }
14558a0548f9SBharat Bhushan 
14568a0548f9SBharat Bhushan int kvm_arch_remove_sw_breakpoint(CPUState *cs, struct kvm_sw_breakpoint *bp)
14578a0548f9SBharat Bhushan {
14588a0548f9SBharat Bhushan     uint32_t sc;
14598a0548f9SBharat Bhushan 
14608a0548f9SBharat Bhushan     if (cpu_memory_rw_debug(cs, bp->pc, (uint8_t *)&sc, sizeof(sc), 0) ||
14618a0548f9SBharat Bhushan         sc != debug_inst_opcode ||
14628a0548f9SBharat Bhushan         cpu_memory_rw_debug(cs, bp->pc, (uint8_t *)&bp->saved_insn,
14638a0548f9SBharat Bhushan                             sizeof(sc), 1)) {
14648a0548f9SBharat Bhushan         return -EINVAL;
14658a0548f9SBharat Bhushan     }
14668a0548f9SBharat Bhushan 
14678a0548f9SBharat Bhushan     return 0;
14688a0548f9SBharat Bhushan }
14698a0548f9SBharat Bhushan 
147088365d17SBharat Bhushan static int find_hw_breakpoint(target_ulong addr, int type)
147188365d17SBharat Bhushan {
147288365d17SBharat Bhushan     int n;
147388365d17SBharat Bhushan 
147488365d17SBharat Bhushan     assert((nb_hw_breakpoint + nb_hw_watchpoint)
147588365d17SBharat Bhushan            <= ARRAY_SIZE(hw_debug_points));
147688365d17SBharat Bhushan 
147788365d17SBharat Bhushan     for (n = 0; n < nb_hw_breakpoint + nb_hw_watchpoint; n++) {
147888365d17SBharat Bhushan         if (hw_debug_points[n].addr == addr &&
147988365d17SBharat Bhushan              hw_debug_points[n].type == type) {
148088365d17SBharat Bhushan             return n;
148188365d17SBharat Bhushan         }
148288365d17SBharat Bhushan     }
148388365d17SBharat Bhushan 
148488365d17SBharat Bhushan     return -1;
148588365d17SBharat Bhushan }
148688365d17SBharat Bhushan 
148788365d17SBharat Bhushan static int find_hw_watchpoint(target_ulong addr, int *flag)
148888365d17SBharat Bhushan {
148988365d17SBharat Bhushan     int n;
149088365d17SBharat Bhushan 
149188365d17SBharat Bhushan     n = find_hw_breakpoint(addr, GDB_WATCHPOINT_ACCESS);
149288365d17SBharat Bhushan     if (n >= 0) {
149388365d17SBharat Bhushan         *flag = BP_MEM_ACCESS;
149488365d17SBharat Bhushan         return n;
149588365d17SBharat Bhushan     }
149688365d17SBharat Bhushan 
149788365d17SBharat Bhushan     n = find_hw_breakpoint(addr, GDB_WATCHPOINT_WRITE);
149888365d17SBharat Bhushan     if (n >= 0) {
149988365d17SBharat Bhushan         *flag = BP_MEM_WRITE;
150088365d17SBharat Bhushan         return n;
150188365d17SBharat Bhushan     }
150288365d17SBharat Bhushan 
150388365d17SBharat Bhushan     n = find_hw_breakpoint(addr, GDB_WATCHPOINT_READ);
150488365d17SBharat Bhushan     if (n >= 0) {
150588365d17SBharat Bhushan         *flag = BP_MEM_READ;
150688365d17SBharat Bhushan         return n;
150788365d17SBharat Bhushan     }
150888365d17SBharat Bhushan 
150988365d17SBharat Bhushan     return -1;
151088365d17SBharat Bhushan }
151188365d17SBharat Bhushan 
151288365d17SBharat Bhushan int kvm_arch_insert_hw_breakpoint(target_ulong addr,
151388365d17SBharat Bhushan                                   target_ulong len, int type)
151488365d17SBharat Bhushan {
151588365d17SBharat Bhushan     if ((nb_hw_breakpoint + nb_hw_watchpoint) >= ARRAY_SIZE(hw_debug_points)) {
151688365d17SBharat Bhushan         return -ENOBUFS;
151788365d17SBharat Bhushan     }
151888365d17SBharat Bhushan 
151988365d17SBharat Bhushan     hw_debug_points[nb_hw_breakpoint + nb_hw_watchpoint].addr = addr;
152088365d17SBharat Bhushan     hw_debug_points[nb_hw_breakpoint + nb_hw_watchpoint].type = type;
152188365d17SBharat Bhushan 
152288365d17SBharat Bhushan     switch (type) {
152388365d17SBharat Bhushan     case GDB_BREAKPOINT_HW:
152488365d17SBharat Bhushan         if (nb_hw_breakpoint >= max_hw_breakpoint) {
152588365d17SBharat Bhushan             return -ENOBUFS;
152688365d17SBharat Bhushan         }
152788365d17SBharat Bhushan 
152888365d17SBharat Bhushan         if (find_hw_breakpoint(addr, type) >= 0) {
152988365d17SBharat Bhushan             return -EEXIST;
153088365d17SBharat Bhushan         }
153188365d17SBharat Bhushan 
153288365d17SBharat Bhushan         nb_hw_breakpoint++;
153388365d17SBharat Bhushan         break;
153488365d17SBharat Bhushan 
153588365d17SBharat Bhushan     case GDB_WATCHPOINT_WRITE:
153688365d17SBharat Bhushan     case GDB_WATCHPOINT_READ:
153788365d17SBharat Bhushan     case GDB_WATCHPOINT_ACCESS:
153888365d17SBharat Bhushan         if (nb_hw_watchpoint >= max_hw_watchpoint) {
153988365d17SBharat Bhushan             return -ENOBUFS;
154088365d17SBharat Bhushan         }
154188365d17SBharat Bhushan 
154288365d17SBharat Bhushan         if (find_hw_breakpoint(addr, type) >= 0) {
154388365d17SBharat Bhushan             return -EEXIST;
154488365d17SBharat Bhushan         }
154588365d17SBharat Bhushan 
154688365d17SBharat Bhushan         nb_hw_watchpoint++;
154788365d17SBharat Bhushan         break;
154888365d17SBharat Bhushan 
154988365d17SBharat Bhushan     default:
155088365d17SBharat Bhushan         return -ENOSYS;
155188365d17SBharat Bhushan     }
155288365d17SBharat Bhushan 
155388365d17SBharat Bhushan     return 0;
155488365d17SBharat Bhushan }
155588365d17SBharat Bhushan 
155688365d17SBharat Bhushan int kvm_arch_remove_hw_breakpoint(target_ulong addr,
155788365d17SBharat Bhushan                                   target_ulong len, int type)
155888365d17SBharat Bhushan {
155988365d17SBharat Bhushan     int n;
156088365d17SBharat Bhushan 
156188365d17SBharat Bhushan     n = find_hw_breakpoint(addr, type);
156288365d17SBharat Bhushan     if (n < 0) {
156388365d17SBharat Bhushan         return -ENOENT;
156488365d17SBharat Bhushan     }
156588365d17SBharat Bhushan 
156688365d17SBharat Bhushan     switch (type) {
156788365d17SBharat Bhushan     case GDB_BREAKPOINT_HW:
156888365d17SBharat Bhushan         nb_hw_breakpoint--;
156988365d17SBharat Bhushan         break;
157088365d17SBharat Bhushan 
157188365d17SBharat Bhushan     case GDB_WATCHPOINT_WRITE:
157288365d17SBharat Bhushan     case GDB_WATCHPOINT_READ:
157388365d17SBharat Bhushan     case GDB_WATCHPOINT_ACCESS:
157488365d17SBharat Bhushan         nb_hw_watchpoint--;
157588365d17SBharat Bhushan         break;
157688365d17SBharat Bhushan 
157788365d17SBharat Bhushan     default:
157888365d17SBharat Bhushan         return -ENOSYS;
157988365d17SBharat Bhushan     }
158088365d17SBharat Bhushan     hw_debug_points[n] = hw_debug_points[nb_hw_breakpoint + nb_hw_watchpoint];
158188365d17SBharat Bhushan 
158288365d17SBharat Bhushan     return 0;
158388365d17SBharat Bhushan }
158488365d17SBharat Bhushan 
158588365d17SBharat Bhushan void kvm_arch_remove_all_hw_breakpoints(void)
158688365d17SBharat Bhushan {
158788365d17SBharat Bhushan     nb_hw_breakpoint = nb_hw_watchpoint = 0;
158888365d17SBharat Bhushan }
158988365d17SBharat Bhushan 
15908a0548f9SBharat Bhushan void kvm_arch_update_guest_debug(CPUState *cs, struct kvm_guest_debug *dbg)
15918a0548f9SBharat Bhushan {
159288365d17SBharat Bhushan     int n;
159388365d17SBharat Bhushan 
15948a0548f9SBharat Bhushan     /* Software Breakpoint updates */
15958a0548f9SBharat Bhushan     if (kvm_sw_breakpoints_active(cs)) {
15968a0548f9SBharat Bhushan         dbg->control |= KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP;
15978a0548f9SBharat Bhushan     }
159888365d17SBharat Bhushan 
159988365d17SBharat Bhushan     assert((nb_hw_breakpoint + nb_hw_watchpoint)
160088365d17SBharat Bhushan            <= ARRAY_SIZE(hw_debug_points));
160188365d17SBharat Bhushan     assert((nb_hw_breakpoint + nb_hw_watchpoint) <= ARRAY_SIZE(dbg->arch.bp));
160288365d17SBharat Bhushan 
160388365d17SBharat Bhushan     if (nb_hw_breakpoint + nb_hw_watchpoint > 0) {
160488365d17SBharat Bhushan         dbg->control |= KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_HW_BP;
160588365d17SBharat Bhushan         memset(dbg->arch.bp, 0, sizeof(dbg->arch.bp));
160688365d17SBharat Bhushan         for (n = 0; n < nb_hw_breakpoint + nb_hw_watchpoint; n++) {
160788365d17SBharat Bhushan             switch (hw_debug_points[n].type) {
160888365d17SBharat Bhushan             case GDB_BREAKPOINT_HW:
160988365d17SBharat Bhushan                 dbg->arch.bp[n].type = KVMPPC_DEBUG_BREAKPOINT;
161088365d17SBharat Bhushan                 break;
161188365d17SBharat Bhushan             case GDB_WATCHPOINT_WRITE:
161288365d17SBharat Bhushan                 dbg->arch.bp[n].type = KVMPPC_DEBUG_WATCH_WRITE;
161388365d17SBharat Bhushan                 break;
161488365d17SBharat Bhushan             case GDB_WATCHPOINT_READ:
161588365d17SBharat Bhushan                 dbg->arch.bp[n].type = KVMPPC_DEBUG_WATCH_READ;
161688365d17SBharat Bhushan                 break;
161788365d17SBharat Bhushan             case GDB_WATCHPOINT_ACCESS:
161888365d17SBharat Bhushan                 dbg->arch.bp[n].type = KVMPPC_DEBUG_WATCH_WRITE |
161988365d17SBharat Bhushan                                         KVMPPC_DEBUG_WATCH_READ;
162088365d17SBharat Bhushan                 break;
162188365d17SBharat Bhushan             default:
162288365d17SBharat Bhushan                 cpu_abort(cs, "Unsupported breakpoint type\n");
162388365d17SBharat Bhushan             }
162488365d17SBharat Bhushan             dbg->arch.bp[n].addr = hw_debug_points[n].addr;
162588365d17SBharat Bhushan         }
162688365d17SBharat Bhushan     }
16278a0548f9SBharat Bhushan }
16288a0548f9SBharat Bhushan 
16292cbd1581SFabiano Rosas static int kvm_handle_hw_breakpoint(CPUState *cs,
16302cbd1581SFabiano Rosas                                     struct kvm_debug_exit_arch *arch_info)
16318a0548f9SBharat Bhushan {
16328a0548f9SBharat Bhushan     int handle = 0;
163388365d17SBharat Bhushan     int n;
163488365d17SBharat Bhushan     int flag = 0;
16358a0548f9SBharat Bhushan 
163688365d17SBharat Bhushan     if (nb_hw_breakpoint + nb_hw_watchpoint > 0) {
163788365d17SBharat Bhushan         if (arch_info->status & KVMPPC_DEBUG_BREAKPOINT) {
163888365d17SBharat Bhushan             n = find_hw_breakpoint(arch_info->address, GDB_BREAKPOINT_HW);
163988365d17SBharat Bhushan             if (n >= 0) {
164088365d17SBharat Bhushan                 handle = 1;
164188365d17SBharat Bhushan             }
164288365d17SBharat Bhushan         } else if (arch_info->status & (KVMPPC_DEBUG_WATCH_READ |
164388365d17SBharat Bhushan                                         KVMPPC_DEBUG_WATCH_WRITE)) {
164488365d17SBharat Bhushan             n = find_hw_watchpoint(arch_info->address,  &flag);
164588365d17SBharat Bhushan             if (n >= 0) {
164688365d17SBharat Bhushan                 handle = 1;
164788365d17SBharat Bhushan                 cs->watchpoint_hit = &hw_watchpoint;
164888365d17SBharat Bhushan                 hw_watchpoint.vaddr = hw_debug_points[n].addr;
164988365d17SBharat Bhushan                 hw_watchpoint.flags = flag;
165088365d17SBharat Bhushan             }
165188365d17SBharat Bhushan         }
165288365d17SBharat Bhushan     }
16532cbd1581SFabiano Rosas     return handle;
16542cbd1581SFabiano Rosas }
16552cbd1581SFabiano Rosas 
1656468e3a1aSFabiano Rosas static int kvm_handle_singlestep(void)
1657468e3a1aSFabiano Rosas {
1658468e3a1aSFabiano Rosas     return 1;
1659468e3a1aSFabiano Rosas }
1660468e3a1aSFabiano Rosas 
1661468e3a1aSFabiano Rosas static int kvm_handle_sw_breakpoint(void)
1662468e3a1aSFabiano Rosas {
1663468e3a1aSFabiano Rosas     return 1;
1664468e3a1aSFabiano Rosas }
1665468e3a1aSFabiano Rosas 
16662cbd1581SFabiano Rosas static int kvm_handle_debug(PowerPCCPU *cpu, struct kvm_run *run)
16672cbd1581SFabiano Rosas {
16682cbd1581SFabiano Rosas     CPUState *cs = CPU(cpu);
16692cbd1581SFabiano Rosas     CPUPPCState *env = &cpu->env;
16702cbd1581SFabiano Rosas     struct kvm_debug_exit_arch *arch_info = &run->debug.arch;
16712cbd1581SFabiano Rosas 
16722cbd1581SFabiano Rosas     if (cs->singlestep_enabled) {
1673468e3a1aSFabiano Rosas         return kvm_handle_singlestep();
1674468e3a1aSFabiano Rosas     }
1675468e3a1aSFabiano Rosas 
1676468e3a1aSFabiano Rosas     if (arch_info->status) {
1677468e3a1aSFabiano Rosas         return kvm_handle_hw_breakpoint(cs, arch_info);
1678468e3a1aSFabiano Rosas     }
1679468e3a1aSFabiano Rosas 
1680468e3a1aSFabiano Rosas     if (kvm_find_sw_breakpoint(cs, arch_info->address)) {
1681468e3a1aSFabiano Rosas         return kvm_handle_sw_breakpoint();
1682468e3a1aSFabiano Rosas     }
1683468e3a1aSFabiano Rosas 
1684468e3a1aSFabiano Rosas     /*
1685468e3a1aSFabiano Rosas      * QEMU is not able to handle debug exception, so inject
16868a0548f9SBharat Bhushan      * program exception to guest;
16878a0548f9SBharat Bhushan      * Yes program exception NOT debug exception !!
168888365d17SBharat Bhushan      * When QEMU is using debug resources then debug exception must
168988365d17SBharat Bhushan      * be always set. To achieve this we set MSR_DE and also set
169088365d17SBharat Bhushan      * MSRP_DEP so guest cannot change MSR_DE.
169188365d17SBharat Bhushan      * When emulating debug resource for guest we want guest
169288365d17SBharat Bhushan      * to control MSR_DE (enable/disable debug interrupt on need).
169388365d17SBharat Bhushan      * Supporting both configurations are NOT possible.
169488365d17SBharat Bhushan      * So the result is that we cannot share debug resources
169588365d17SBharat Bhushan      * between QEMU and Guest on BOOKE architecture.
169688365d17SBharat Bhushan      * In the current design QEMU gets the priority over guest,
169788365d17SBharat Bhushan      * this means that if QEMU is using debug resources then guest
169888365d17SBharat Bhushan      * cannot use them;
16998a0548f9SBharat Bhushan      * For software breakpoint QEMU uses a privileged instruction;
17008a0548f9SBharat Bhushan      * So there cannot be any reason that we are here for guest
17018a0548f9SBharat Bhushan      * set debug exception, only possibility is guest executed a
17028a0548f9SBharat Bhushan      * privileged / illegal instruction and that's why we are
17038a0548f9SBharat Bhushan      * injecting a program interrupt.
17048a0548f9SBharat Bhushan      */
17058a0548f9SBharat Bhushan     cpu_synchronize_state(cs);
1706468e3a1aSFabiano Rosas     /*
1707468e3a1aSFabiano Rosas      * env->nip is PC, so increment this by 4 to use
17088a0548f9SBharat Bhushan      * ppc_cpu_do_interrupt(), which set srr0 = env->nip - 4.
17098a0548f9SBharat Bhushan      */
17108a0548f9SBharat Bhushan     env->nip += 4;
17118a0548f9SBharat Bhushan     cs->exception_index = POWERPC_EXCP_PROGRAM;
17128a0548f9SBharat Bhushan     env->error_code = POWERPC_EXCP_INVAL;
17138a0548f9SBharat Bhushan     ppc_cpu_do_interrupt(cs);
17148a0548f9SBharat Bhushan 
1715468e3a1aSFabiano Rosas     return 0;
17168a0548f9SBharat Bhushan }
17178a0548f9SBharat Bhushan 
171820d695a9SAndreas Färber int kvm_arch_handle_exit(CPUState *cs, struct kvm_run *run)
1719d76d1650Saurel32 {
172020d695a9SAndreas Färber     PowerPCCPU *cpu = POWERPC_CPU(cs);
172120d695a9SAndreas Färber     CPUPPCState *env = &cpu->env;
1722bb4ea393SJan Kiszka     int ret;
1723d76d1650Saurel32 
17244b8523eeSJan Kiszka     qemu_mutex_lock_iothread();
17254b8523eeSJan Kiszka 
1726d76d1650Saurel32     switch (run->exit_reason) {
1727d76d1650Saurel32     case KVM_EXIT_DCR:
1728d76d1650Saurel32         if (run->dcr.is_write) {
17298d83cbf1SGreg Kurz             trace_kvm_handle_dcr_write();
1730d76d1650Saurel32             ret = kvmppc_handle_dcr_write(env, run->dcr.dcrn, run->dcr.data);
1731d76d1650Saurel32         } else {
1732228152c2SBoxuan Li             trace_kvm_handle_dcr_read();
1733d76d1650Saurel32             ret = kvmppc_handle_dcr_read(env, run->dcr.dcrn, &run->dcr.data);
1734d76d1650Saurel32         }
1735d76d1650Saurel32         break;
1736d76d1650Saurel32     case KVM_EXIT_HLT:
17378d83cbf1SGreg Kurz         trace_kvm_handle_halt();
1738259186a7SAndreas Färber         ret = kvmppc_handle_halt(cpu);
1739d76d1650Saurel32         break;
1740c6304a4aSDavid Gibson #if defined(TARGET_PPC64)
1741f61b4bedSAlexander Graf     case KVM_EXIT_PAPR_HCALL:
17428d83cbf1SGreg Kurz         trace_kvm_handle_papr_hcall();
174320d695a9SAndreas Färber         run->papr_hcall.ret = spapr_hypercall(cpu,
1744aa100fa4SAndreas Färber                                               run->papr_hcall.nr,
1745f61b4bedSAlexander Graf                                               run->papr_hcall.args);
174678e8fde2SDavid Gibson         ret = 0;
1747f61b4bedSAlexander Graf         break;
1748f61b4bedSAlexander Graf #endif
17495b95b8b9SAlexander Graf     case KVM_EXIT_EPR:
17508d83cbf1SGreg Kurz         trace_kvm_handle_epr();
1751933b19eaSAlexander Graf         run->epr.epr = ldl_phys(cs->as, env->mpic_iack);
17525b95b8b9SAlexander Graf         ret = 0;
17535b95b8b9SAlexander Graf         break;
175431f2cb8fSBharat Bhushan     case KVM_EXIT_WATCHDOG:
17558d83cbf1SGreg Kurz         trace_kvm_handle_watchdog_expiry();
175631f2cb8fSBharat Bhushan         watchdog_perform_action();
175731f2cb8fSBharat Bhushan         ret = 0;
175831f2cb8fSBharat Bhushan         break;
175931f2cb8fSBharat Bhushan 
17608a0548f9SBharat Bhushan     case KVM_EXIT_DEBUG:
17618d83cbf1SGreg Kurz         trace_kvm_handle_debug_exception();
17628a0548f9SBharat Bhushan         if (kvm_handle_debug(cpu, run)) {
17638a0548f9SBharat Bhushan             ret = EXCP_DEBUG;
17648a0548f9SBharat Bhushan             break;
17658a0548f9SBharat Bhushan         }
17668a0548f9SBharat Bhushan         /* re-enter, this exception was guest-internal */
17678a0548f9SBharat Bhushan         ret = 0;
17688a0548f9SBharat Bhushan         break;
17698a0548f9SBharat Bhushan 
177073aaec4aSJan Kiszka     default:
177173aaec4aSJan Kiszka         fprintf(stderr, "KVM: unknown exit reason %d\n", run->exit_reason);
177273aaec4aSJan Kiszka         ret = -1;
177373aaec4aSJan Kiszka         break;
1774d76d1650Saurel32     }
1775d76d1650Saurel32 
17764b8523eeSJan Kiszka     qemu_mutex_unlock_iothread();
1777d76d1650Saurel32     return ret;
1778d76d1650Saurel32 }
1779d76d1650Saurel32 
178031f2cb8fSBharat Bhushan int kvmppc_or_tsr_bits(PowerPCCPU *cpu, uint32_t tsr_bits)
178131f2cb8fSBharat Bhushan {
178231f2cb8fSBharat Bhushan     CPUState *cs = CPU(cpu);
178331f2cb8fSBharat Bhushan     uint32_t bits = tsr_bits;
178431f2cb8fSBharat Bhushan     struct kvm_one_reg reg = {
178531f2cb8fSBharat Bhushan         .id = KVM_REG_PPC_OR_TSR,
178631f2cb8fSBharat Bhushan         .addr = (uintptr_t) &bits,
178731f2cb8fSBharat Bhushan     };
178831f2cb8fSBharat Bhushan 
178931f2cb8fSBharat Bhushan     return kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, &reg);
179031f2cb8fSBharat Bhushan }
179131f2cb8fSBharat Bhushan 
179231f2cb8fSBharat Bhushan int kvmppc_clear_tsr_bits(PowerPCCPU *cpu, uint32_t tsr_bits)
179331f2cb8fSBharat Bhushan {
179431f2cb8fSBharat Bhushan 
179531f2cb8fSBharat Bhushan     CPUState *cs = CPU(cpu);
179631f2cb8fSBharat Bhushan     uint32_t bits = tsr_bits;
179731f2cb8fSBharat Bhushan     struct kvm_one_reg reg = {
179831f2cb8fSBharat Bhushan         .id = KVM_REG_PPC_CLEAR_TSR,
179931f2cb8fSBharat Bhushan         .addr = (uintptr_t) &bits,
180031f2cb8fSBharat Bhushan     };
180131f2cb8fSBharat Bhushan 
180231f2cb8fSBharat Bhushan     return kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, &reg);
180331f2cb8fSBharat Bhushan }
180431f2cb8fSBharat Bhushan 
180531f2cb8fSBharat Bhushan int kvmppc_set_tcr(PowerPCCPU *cpu)
180631f2cb8fSBharat Bhushan {
180731f2cb8fSBharat Bhushan     CPUState *cs = CPU(cpu);
180831f2cb8fSBharat Bhushan     CPUPPCState *env = &cpu->env;
180931f2cb8fSBharat Bhushan     uint32_t tcr = env->spr[SPR_BOOKE_TCR];
181031f2cb8fSBharat Bhushan 
181131f2cb8fSBharat Bhushan     struct kvm_one_reg reg = {
181231f2cb8fSBharat Bhushan         .id = KVM_REG_PPC_TCR,
181331f2cb8fSBharat Bhushan         .addr = (uintptr_t) &tcr,
181431f2cb8fSBharat Bhushan     };
181531f2cb8fSBharat Bhushan 
181631f2cb8fSBharat Bhushan     return kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, &reg);
181731f2cb8fSBharat Bhushan }
181831f2cb8fSBharat Bhushan 
181931f2cb8fSBharat Bhushan int kvmppc_booke_watchdog_enable(PowerPCCPU *cpu)
182031f2cb8fSBharat Bhushan {
182131f2cb8fSBharat Bhushan     CPUState *cs = CPU(cpu);
182231f2cb8fSBharat Bhushan     int ret;
182331f2cb8fSBharat Bhushan 
182431f2cb8fSBharat Bhushan     if (!kvm_enabled()) {
182531f2cb8fSBharat Bhushan         return -1;
182631f2cb8fSBharat Bhushan     }
182731f2cb8fSBharat Bhushan 
182831f2cb8fSBharat Bhushan     if (!cap_ppc_watchdog) {
182931f2cb8fSBharat Bhushan         printf("warning: KVM does not support watchdog");
183031f2cb8fSBharat Bhushan         return -1;
183131f2cb8fSBharat Bhushan     }
183231f2cb8fSBharat Bhushan 
183348add816SCornelia Huck     ret = kvm_vcpu_enable_cap(cs, KVM_CAP_PPC_BOOKE_WATCHDOG, 0);
183431f2cb8fSBharat Bhushan     if (ret < 0) {
183531f2cb8fSBharat Bhushan         fprintf(stderr, "%s: couldn't enable KVM_CAP_PPC_BOOKE_WATCHDOG: %s\n",
183631f2cb8fSBharat Bhushan                 __func__, strerror(-ret));
183731f2cb8fSBharat Bhushan         return ret;
183831f2cb8fSBharat Bhushan     }
183931f2cb8fSBharat Bhushan 
184031f2cb8fSBharat Bhushan     return ret;
184131f2cb8fSBharat Bhushan }
184231f2cb8fSBharat Bhushan 
1843dc333cd6SAlexander Graf static int read_cpuinfo(const char *field, char *value, int len)
1844dc333cd6SAlexander Graf {
1845dc333cd6SAlexander Graf     FILE *f;
1846dc333cd6SAlexander Graf     int ret = -1;
1847dc333cd6SAlexander Graf     int field_len = strlen(field);
1848dc333cd6SAlexander Graf     char line[512];
1849dc333cd6SAlexander Graf 
1850dc333cd6SAlexander Graf     f = fopen("/proc/cpuinfo", "r");
1851dc333cd6SAlexander Graf     if (!f) {
1852dc333cd6SAlexander Graf         return -1;
1853dc333cd6SAlexander Graf     }
1854dc333cd6SAlexander Graf 
1855dc333cd6SAlexander Graf     do {
1856dc333cd6SAlexander Graf         if (!fgets(line, sizeof(line), f)) {
1857dc333cd6SAlexander Graf             break;
1858dc333cd6SAlexander Graf         }
1859dc333cd6SAlexander Graf         if (!strncmp(line, field, field_len)) {
1860ae215068SJim Meyering             pstrcpy(value, len, line);
1861dc333cd6SAlexander Graf             ret = 0;
1862dc333cd6SAlexander Graf             break;
1863dc333cd6SAlexander Graf         }
1864dc333cd6SAlexander Graf     } while (*line);
1865dc333cd6SAlexander Graf 
1866dc333cd6SAlexander Graf     fclose(f);
1867dc333cd6SAlexander Graf 
1868dc333cd6SAlexander Graf     return ret;
1869dc333cd6SAlexander Graf }
1870dc333cd6SAlexander Graf 
1871dc333cd6SAlexander Graf uint32_t kvmppc_get_tbfreq(void)
1872dc333cd6SAlexander Graf {
1873dc333cd6SAlexander Graf     char line[512];
1874dc333cd6SAlexander Graf     char *ns;
187573bcb24dSRutuja Shah     uint32_t retval = NANOSECONDS_PER_SECOND;
1876dc333cd6SAlexander Graf 
1877dc333cd6SAlexander Graf     if (read_cpuinfo("timebase", line, sizeof(line))) {
1878dc333cd6SAlexander Graf         return retval;
1879dc333cd6SAlexander Graf     }
1880dc333cd6SAlexander Graf 
1881c995e942SDavid Gibson     ns = strchr(line, ':');
1882c995e942SDavid Gibson     if (!ns) {
1883dc333cd6SAlexander Graf         return retval;
1884dc333cd6SAlexander Graf     }
1885dc333cd6SAlexander Graf 
1886dc333cd6SAlexander Graf     ns++;
1887dc333cd6SAlexander Graf 
1888f9b8e7f6SShraddha Barke     return atoi(ns);
1889ef951443SNikunj A Dadhania }
1890ef951443SNikunj A Dadhania 
1891ef951443SNikunj A Dadhania bool kvmppc_get_host_serial(char **value)
1892ef951443SNikunj A Dadhania {
1893ef951443SNikunj A Dadhania     return g_file_get_contents("/proc/device-tree/system-id", value, NULL,
1894ef951443SNikunj A Dadhania                                NULL);
1895ef951443SNikunj A Dadhania }
1896ef951443SNikunj A Dadhania 
1897ef951443SNikunj A Dadhania bool kvmppc_get_host_model(char **value)
1898ef951443SNikunj A Dadhania {
1899ef951443SNikunj A Dadhania     return g_file_get_contents("/proc/device-tree/model", value, NULL, NULL);
1900dc333cd6SAlexander Graf }
19014513d923SGleb Natapov 
1902eadaada1SAlexander Graf /* Try to find a device tree node for a CPU with clock-frequency property */
1903eadaada1SAlexander Graf static int kvmppc_find_cpu_dt(char *buf, int buf_len)
1904eadaada1SAlexander Graf {
1905eadaada1SAlexander Graf     struct dirent *dirp;
1906eadaada1SAlexander Graf     DIR *dp;
1907eadaada1SAlexander Graf 
1908c995e942SDavid Gibson     dp = opendir(PROC_DEVTREE_CPU);
1909c995e942SDavid Gibson     if (!dp) {
1910eadaada1SAlexander Graf         printf("Can't open directory " PROC_DEVTREE_CPU "\n");
1911eadaada1SAlexander Graf         return -1;
1912eadaada1SAlexander Graf     }
1913eadaada1SAlexander Graf 
1914eadaada1SAlexander Graf     buf[0] = '\0';
1915eadaada1SAlexander Graf     while ((dirp = readdir(dp)) != NULL) {
1916eadaada1SAlexander Graf         FILE *f;
1917eadaada1SAlexander Graf         snprintf(buf, buf_len, "%s%s/clock-frequency", PROC_DEVTREE_CPU,
1918eadaada1SAlexander Graf                  dirp->d_name);
1919eadaada1SAlexander Graf         f = fopen(buf, "r");
1920eadaada1SAlexander Graf         if (f) {
1921eadaada1SAlexander Graf             snprintf(buf, buf_len, "%s%s", PROC_DEVTREE_CPU, dirp->d_name);
1922eadaada1SAlexander Graf             fclose(f);
1923eadaada1SAlexander Graf             break;
1924eadaada1SAlexander Graf         }
1925eadaada1SAlexander Graf         buf[0] = '\0';
1926eadaada1SAlexander Graf     }
1927eadaada1SAlexander Graf     closedir(dp);
1928eadaada1SAlexander Graf     if (buf[0] == '\0') {
1929eadaada1SAlexander Graf         printf("Unknown host!\n");
1930eadaada1SAlexander Graf         return -1;
1931eadaada1SAlexander Graf     }
1932eadaada1SAlexander Graf 
1933eadaada1SAlexander Graf     return 0;
1934eadaada1SAlexander Graf }
1935eadaada1SAlexander Graf 
19367d94a30bSSukadev Bhattiprolu static uint64_t kvmppc_read_int_dt(const char *filename)
1937eadaada1SAlexander Graf {
19389bc884b7SDavid Gibson     union {
19399bc884b7SDavid Gibson         uint32_t v32;
19409bc884b7SDavid Gibson         uint64_t v64;
19419bc884b7SDavid Gibson     } u;
1942eadaada1SAlexander Graf     FILE *f;
1943eadaada1SAlexander Graf     int len;
1944eadaada1SAlexander Graf 
19457d94a30bSSukadev Bhattiprolu     f = fopen(filename, "rb");
1946eadaada1SAlexander Graf     if (!f) {
1947eadaada1SAlexander Graf         return -1;
1948eadaada1SAlexander Graf     }
1949eadaada1SAlexander Graf 
19509bc884b7SDavid Gibson     len = fread(&u, 1, sizeof(u), f);
1951eadaada1SAlexander Graf     fclose(f);
1952eadaada1SAlexander Graf     switch (len) {
19539bc884b7SDavid Gibson     case 4:
19549bc884b7SDavid Gibson         /* property is a 32-bit quantity */
19559bc884b7SDavid Gibson         return be32_to_cpu(u.v32);
19569bc884b7SDavid Gibson     case 8:
19579bc884b7SDavid Gibson         return be64_to_cpu(u.v64);
1958eadaada1SAlexander Graf     }
1959eadaada1SAlexander Graf 
1960eadaada1SAlexander Graf     return 0;
1961eadaada1SAlexander Graf }
1962eadaada1SAlexander Graf 
1963c995e942SDavid Gibson /*
1964c995e942SDavid Gibson  * Read a CPU node property from the host device tree that's a single
19657d94a30bSSukadev Bhattiprolu  * integer (32-bit or 64-bit).  Returns 0 if anything goes wrong
1966c995e942SDavid Gibson  * (can't find or open the property, or doesn't understand the format)
1967c995e942SDavid Gibson  */
19687d94a30bSSukadev Bhattiprolu static uint64_t kvmppc_read_int_cpu_dt(const char *propname)
19697d94a30bSSukadev Bhattiprolu {
19707d94a30bSSukadev Bhattiprolu     char buf[PATH_MAX], *tmp;
19717d94a30bSSukadev Bhattiprolu     uint64_t val;
19727d94a30bSSukadev Bhattiprolu 
19737d94a30bSSukadev Bhattiprolu     if (kvmppc_find_cpu_dt(buf, sizeof(buf))) {
19747d94a30bSSukadev Bhattiprolu         return -1;
19757d94a30bSSukadev Bhattiprolu     }
19767d94a30bSSukadev Bhattiprolu 
19777d94a30bSSukadev Bhattiprolu     tmp = g_strdup_printf("%s/%s", buf, propname);
19787d94a30bSSukadev Bhattiprolu     val = kvmppc_read_int_dt(tmp);
19797d94a30bSSukadev Bhattiprolu     g_free(tmp);
19807d94a30bSSukadev Bhattiprolu 
19817d94a30bSSukadev Bhattiprolu     return val;
19827d94a30bSSukadev Bhattiprolu }
19837d94a30bSSukadev Bhattiprolu 
19849bc884b7SDavid Gibson uint64_t kvmppc_get_clockfreq(void)
19859bc884b7SDavid Gibson {
19869bc884b7SDavid Gibson     return kvmppc_read_int_cpu_dt("clock-frequency");
19879bc884b7SDavid Gibson }
19889bc884b7SDavid Gibson 
19897d050527SSuraj Jitindar Singh static int kvmppc_get_dec_bits(void)
19907d050527SSuraj Jitindar Singh {
19917d050527SSuraj Jitindar Singh     int nr_bits = kvmppc_read_int_cpu_dt("ibm,dec-bits");
19927d050527SSuraj Jitindar Singh 
19937d050527SSuraj Jitindar Singh     if (nr_bits > 0) {
19947d050527SSuraj Jitindar Singh         return nr_bits;
19957d050527SSuraj Jitindar Singh     }
19967d050527SSuraj Jitindar Singh     return 0;
19977d050527SSuraj Jitindar Singh }
19987d050527SSuraj Jitindar Singh 
19991a61a9aeSStuart Yoder static int kvmppc_get_pvinfo(CPUPPCState *env, struct kvm_ppc_pvinfo *pvinfo)
200045024f09SAlexander Graf {
2001db70b311SRichard Henderson     CPUState *cs = env_cpu(env);
200245024f09SAlexander Graf 
20036fd33a75SAlexander Graf     if (kvm_vm_check_extension(cs->kvm_state, KVM_CAP_PPC_GET_PVINFO) &&
20041a61a9aeSStuart Yoder         !kvm_vm_ioctl(cs->kvm_state, KVM_PPC_GET_PVINFO, pvinfo)) {
20051a61a9aeSStuart Yoder         return 0;
20061a61a9aeSStuart Yoder     }
200745024f09SAlexander Graf 
20081a61a9aeSStuart Yoder     return 1;
20091a61a9aeSStuart Yoder }
20101a61a9aeSStuart Yoder 
20111a61a9aeSStuart Yoder int kvmppc_get_hasidle(CPUPPCState *env)
20121a61a9aeSStuart Yoder {
20131a61a9aeSStuart Yoder     struct kvm_ppc_pvinfo pvinfo;
20141a61a9aeSStuart Yoder 
20151a61a9aeSStuart Yoder     if (!kvmppc_get_pvinfo(env, &pvinfo) &&
20161a61a9aeSStuart Yoder         (pvinfo.flags & KVM_PPC_PVINFO_FLAGS_EV_IDLE)) {
20171a61a9aeSStuart Yoder         return 1;
20181a61a9aeSStuart Yoder     }
20191a61a9aeSStuart Yoder 
20201a61a9aeSStuart Yoder     return 0;
20211a61a9aeSStuart Yoder }
20221a61a9aeSStuart Yoder 
20231a61a9aeSStuart Yoder int kvmppc_get_hypercall(CPUPPCState *env, uint8_t *buf, int buf_len)
20241a61a9aeSStuart Yoder {
20251a61a9aeSStuart Yoder     uint32_t *hc = (uint32_t *)buf;
20261a61a9aeSStuart Yoder     struct kvm_ppc_pvinfo pvinfo;
20271a61a9aeSStuart Yoder 
20281a61a9aeSStuart Yoder     if (!kvmppc_get_pvinfo(env, &pvinfo)) {
20291a61a9aeSStuart Yoder         memcpy(buf, pvinfo.hcall, buf_len);
203045024f09SAlexander Graf         return 0;
203145024f09SAlexander Graf     }
203245024f09SAlexander Graf 
203345024f09SAlexander Graf     /*
2034d13fc32eSAlexander Graf      * Fallback to always fail hypercalls regardless of endianness:
203545024f09SAlexander Graf      *
2036d13fc32eSAlexander Graf      *     tdi 0,r0,72 (becomes b .+8 in wrong endian, nop in good endian)
203745024f09SAlexander Graf      *     li r3, -1
2038d13fc32eSAlexander Graf      *     b .+8       (becomes nop in wrong endian)
2039d13fc32eSAlexander Graf      *     bswap32(li r3, -1)
204045024f09SAlexander Graf      */
204145024f09SAlexander Graf 
2042d13fc32eSAlexander Graf     hc[0] = cpu_to_be32(0x08000048);
2043d13fc32eSAlexander Graf     hc[1] = cpu_to_be32(0x3860ffff);
2044d13fc32eSAlexander Graf     hc[2] = cpu_to_be32(0x48000008);
2045d13fc32eSAlexander Graf     hc[3] = cpu_to_be32(bswap32(0x3860ffff));
204645024f09SAlexander Graf 
20470ddbd053SAlexey Kardashevskiy     return 1;
204845024f09SAlexander Graf }
204945024f09SAlexander Graf 
2050026bfd89SDavid Gibson static inline int kvmppc_enable_hcall(KVMState *s, target_ulong hcall)
2051026bfd89SDavid Gibson {
2052026bfd89SDavid Gibson     return kvm_vm_enable_cap(s, KVM_CAP_PPC_ENABLE_HCALL, 0, hcall, 1);
2053026bfd89SDavid Gibson }
2054026bfd89SDavid Gibson 
2055026bfd89SDavid Gibson void kvmppc_enable_logical_ci_hcalls(void)
2056026bfd89SDavid Gibson {
2057026bfd89SDavid Gibson     /*
2058026bfd89SDavid Gibson      * FIXME: it would be nice if we could detect the cases where
2059026bfd89SDavid Gibson      * we're using a device which requires the in kernel
2060026bfd89SDavid Gibson      * implementation of these hcalls, but the kernel lacks them and
2061026bfd89SDavid Gibson      * produce a warning.
2062026bfd89SDavid Gibson      */
2063026bfd89SDavid Gibson     kvmppc_enable_hcall(kvm_state, H_LOGICAL_CI_LOAD);
2064026bfd89SDavid Gibson     kvmppc_enable_hcall(kvm_state, H_LOGICAL_CI_STORE);
2065026bfd89SDavid Gibson }
2066026bfd89SDavid Gibson 
2067ef9971ddSAlexey Kardashevskiy void kvmppc_enable_set_mode_hcall(void)
2068ef9971ddSAlexey Kardashevskiy {
2069ef9971ddSAlexey Kardashevskiy     kvmppc_enable_hcall(kvm_state, H_SET_MODE);
2070ef9971ddSAlexey Kardashevskiy }
2071ef9971ddSAlexey Kardashevskiy 
20725145ad4fSNathan Whitehorn void kvmppc_enable_clear_ref_mod_hcalls(void)
20735145ad4fSNathan Whitehorn {
20745145ad4fSNathan Whitehorn     kvmppc_enable_hcall(kvm_state, H_CLEAR_REF);
20755145ad4fSNathan Whitehorn     kvmppc_enable_hcall(kvm_state, H_CLEAR_MOD);
20765145ad4fSNathan Whitehorn }
20775145ad4fSNathan Whitehorn 
207868f9f708SSuraj Jitindar Singh void kvmppc_enable_h_page_init(void)
207968f9f708SSuraj Jitindar Singh {
208068f9f708SSuraj Jitindar Singh     kvmppc_enable_hcall(kvm_state, H_PAGE_INIT);
208168f9f708SSuraj Jitindar Singh }
208268f9f708SSuraj Jitindar Singh 
20831bc22652SAndreas Färber void kvmppc_set_papr(PowerPCCPU *cpu)
2084f61b4bedSAlexander Graf {
20851bc22652SAndreas Färber     CPUState *cs = CPU(cpu);
2086f61b4bedSAlexander Graf     int ret;
2087f61b4bedSAlexander Graf 
2088da20aed1SDavid Gibson     if (!kvm_enabled()) {
2089da20aed1SDavid Gibson         return;
2090da20aed1SDavid Gibson     }
2091da20aed1SDavid Gibson 
209248add816SCornelia Huck     ret = kvm_vcpu_enable_cap(cs, KVM_CAP_PPC_PAPR, 0);
2093f61b4bedSAlexander Graf     if (ret) {
2094072ed5f2SThomas Huth         error_report("This vCPU type or KVM version does not support PAPR");
2095072ed5f2SThomas Huth         exit(1);
2096f61b4bedSAlexander Graf     }
20979b00ea49SDavid Gibson 
2098c995e942SDavid Gibson     /*
2099c995e942SDavid Gibson      * Update the capability flag so we sync the right information
2100c995e942SDavid Gibson      * with kvm
2101c995e942SDavid Gibson      */
21029b00ea49SDavid Gibson     cap_papr = 1;
2103f1af19d7SDavid Gibson }
2104f61b4bedSAlexander Graf 
2105d6e166c0SDavid Gibson int kvmppc_set_compat(PowerPCCPU *cpu, uint32_t compat_pvr)
21066db5bb0fSAlexey Kardashevskiy {
2107d6e166c0SDavid Gibson     return kvm_set_one_reg(CPU(cpu), KVM_REG_PPC_ARCH_COMPAT, &compat_pvr);
21086db5bb0fSAlexey Kardashevskiy }
21096db5bb0fSAlexey Kardashevskiy 
21105b95b8b9SAlexander Graf void kvmppc_set_mpic_proxy(PowerPCCPU *cpu, int mpic_proxy)
21115b95b8b9SAlexander Graf {
21125b95b8b9SAlexander Graf     CPUState *cs = CPU(cpu);
21135b95b8b9SAlexander Graf     int ret;
21145b95b8b9SAlexander Graf 
211548add816SCornelia Huck     ret = kvm_vcpu_enable_cap(cs, KVM_CAP_PPC_EPR, 0, mpic_proxy);
21165b95b8b9SAlexander Graf     if (ret && mpic_proxy) {
2117072ed5f2SThomas Huth         error_report("This KVM version does not support EPR");
2118072ed5f2SThomas Huth         exit(1);
21195b95b8b9SAlexander Graf     }
21205b95b8b9SAlexander Graf }
21215b95b8b9SAlexander Graf 
2122e97c3636SDavid Gibson int kvmppc_smt_threads(void)
2123e97c3636SDavid Gibson {
2124e97c3636SDavid Gibson     return cap_ppc_smt ? cap_ppc_smt : 1;
2125e97c3636SDavid Gibson }
2126e97c3636SDavid Gibson 
2127fa98fbfcSSam Bobroff int kvmppc_set_smt_threads(int smt)
2128fa98fbfcSSam Bobroff {
2129fa98fbfcSSam Bobroff     int ret;
2130fa98fbfcSSam Bobroff 
2131fa98fbfcSSam Bobroff     ret = kvm_vm_enable_cap(kvm_state, KVM_CAP_PPC_SMT, 0, smt, 0);
2132fa98fbfcSSam Bobroff     if (!ret) {
2133fa98fbfcSSam Bobroff         cap_ppc_smt = smt;
2134fa98fbfcSSam Bobroff     }
2135fa98fbfcSSam Bobroff     return ret;
2136fa98fbfcSSam Bobroff }
2137fa98fbfcSSam Bobroff 
2138fa98fbfcSSam Bobroff void kvmppc_hint_smt_possible(Error **errp)
2139fa98fbfcSSam Bobroff {
2140fa98fbfcSSam Bobroff     int i;
2141fa98fbfcSSam Bobroff     GString *g;
2142fa98fbfcSSam Bobroff     char *s;
2143fa98fbfcSSam Bobroff 
2144fa98fbfcSSam Bobroff     assert(kvm_enabled());
2145fa98fbfcSSam Bobroff     if (cap_ppc_smt_possible) {
2146fa98fbfcSSam Bobroff         g = g_string_new("Available VSMT modes:");
2147fa98fbfcSSam Bobroff         for (i = 63; i >= 0; i--) {
2148fa98fbfcSSam Bobroff             if ((1UL << i) & cap_ppc_smt_possible) {
2149fa98fbfcSSam Bobroff                 g_string_append_printf(g, " %lu", (1UL << i));
2150fa98fbfcSSam Bobroff             }
2151fa98fbfcSSam Bobroff         }
2152fa98fbfcSSam Bobroff         s = g_string_free(g, false);
2153fa98fbfcSSam Bobroff         error_append_hint(errp, "%s.\n", s);
2154fa98fbfcSSam Bobroff         g_free(s);
2155fa98fbfcSSam Bobroff     } else {
2156fa98fbfcSSam Bobroff         error_append_hint(errp,
2157fa98fbfcSSam Bobroff                           "This KVM seems to be too old to support VSMT.\n");
2158fa98fbfcSSam Bobroff     }
2159fa98fbfcSSam Bobroff }
2160fa98fbfcSSam Bobroff 
2161fa98fbfcSSam Bobroff 
21627f763a5dSDavid Gibson #ifdef TARGET_PPC64
21637f763a5dSDavid Gibson uint64_t kvmppc_rma_size(uint64_t current_size, unsigned int hash_shift)
21647f763a5dSDavid Gibson {
2165f36951c1SDavid Gibson     struct kvm_ppc_smmu_info info;
2166f36951c1SDavid Gibson     long rampagesize, best_page_shift;
2167f36951c1SDavid Gibson     int i;
2168f36951c1SDavid Gibson 
2169c995e942SDavid Gibson     /*
2170c995e942SDavid Gibson      * Find the largest hardware supported page size that's less than
2171c995e942SDavid Gibson      * or equal to the (logical) backing page size of guest RAM
2172c995e942SDavid Gibson      */
2173ab256960SGreg Kurz     kvm_get_smmu_info(&info, &error_fatal);
2174905b7ee4SDavid Hildenbrand     rampagesize = qemu_minrampagesize();
2175f36951c1SDavid Gibson     best_page_shift = 0;
2176f36951c1SDavid Gibson 
2177f36951c1SDavid Gibson     for (i = 0; i < KVM_PPC_PAGE_SIZES_MAX_SZ; i++) {
2178f36951c1SDavid Gibson         struct kvm_ppc_one_seg_page_size *sps = &info.sps[i];
2179f36951c1SDavid Gibson 
2180f36951c1SDavid Gibson         if (!sps->page_shift) {
2181f36951c1SDavid Gibson             continue;
2182f36951c1SDavid Gibson         }
2183f36951c1SDavid Gibson 
2184f36951c1SDavid Gibson         if ((sps->page_shift > best_page_shift)
2185f36951c1SDavid Gibson             && ((1UL << sps->page_shift) <= rampagesize)) {
2186f36951c1SDavid Gibson             best_page_shift = sps->page_shift;
2187f36951c1SDavid Gibson         }
2188f36951c1SDavid Gibson     }
2189f36951c1SDavid Gibson 
21907f763a5dSDavid Gibson     return MIN(current_size,
2191f36951c1SDavid Gibson                1ULL << (best_page_shift + hash_shift - 7));
21927f763a5dSDavid Gibson }
21937f763a5dSDavid Gibson #endif
21947f763a5dSDavid Gibson 
2195da95324eSAlexey Kardashevskiy bool kvmppc_spapr_use_multitce(void)
2196da95324eSAlexey Kardashevskiy {
2197da95324eSAlexey Kardashevskiy     return cap_spapr_multitce;
2198da95324eSAlexey Kardashevskiy }
2199da95324eSAlexey Kardashevskiy 
22003dc410aeSAlexey Kardashevskiy int kvmppc_spapr_enable_inkernel_multitce(void)
22013dc410aeSAlexey Kardashevskiy {
22023dc410aeSAlexey Kardashevskiy     int ret;
22033dc410aeSAlexey Kardashevskiy 
22043dc410aeSAlexey Kardashevskiy     ret = kvm_vm_enable_cap(kvm_state, KVM_CAP_PPC_ENABLE_HCALL, 0,
22053dc410aeSAlexey Kardashevskiy                             H_PUT_TCE_INDIRECT, 1);
22063dc410aeSAlexey Kardashevskiy     if (!ret) {
22073dc410aeSAlexey Kardashevskiy         ret = kvm_vm_enable_cap(kvm_state, KVM_CAP_PPC_ENABLE_HCALL, 0,
22083dc410aeSAlexey Kardashevskiy                                 H_STUFF_TCE, 1);
22093dc410aeSAlexey Kardashevskiy     }
22103dc410aeSAlexey Kardashevskiy 
22113dc410aeSAlexey Kardashevskiy     return ret;
22123dc410aeSAlexey Kardashevskiy }
22133dc410aeSAlexey Kardashevskiy 
2214d6ee2a7cSAlexey Kardashevskiy void *kvmppc_create_spapr_tce(uint32_t liobn, uint32_t page_shift,
2215d6ee2a7cSAlexey Kardashevskiy                               uint64_t bus_offset, uint32_t nb_table,
2216d6ee2a7cSAlexey Kardashevskiy                               int *pfd, bool need_vfio)
22170f5cb298SDavid Gibson {
22180f5cb298SDavid Gibson     long len;
22190f5cb298SDavid Gibson     int fd;
22200f5cb298SDavid Gibson     void *table;
22210f5cb298SDavid Gibson 
2222c995e942SDavid Gibson     /*
2223c995e942SDavid Gibson      * Must set fd to -1 so we don't try to munmap when called for
2224b5aec396SDavid Gibson      * destroying the table, which the upper layers -will- do
2225b5aec396SDavid Gibson      */
2226b5aec396SDavid Gibson     *pfd = -1;
22276a81dd17SDavid Gibson     if (!cap_spapr_tce || (need_vfio && !cap_spapr_vfio)) {
22280f5cb298SDavid Gibson         return NULL;
22290f5cb298SDavid Gibson     }
22300f5cb298SDavid Gibson 
2231d6ee2a7cSAlexey Kardashevskiy     if (cap_spapr_tce_64) {
2232d6ee2a7cSAlexey Kardashevskiy         struct kvm_create_spapr_tce_64 args = {
2233d6ee2a7cSAlexey Kardashevskiy             .liobn = liobn,
2234d6ee2a7cSAlexey Kardashevskiy             .page_shift = page_shift,
2235d6ee2a7cSAlexey Kardashevskiy             .offset = bus_offset >> page_shift,
2236d6ee2a7cSAlexey Kardashevskiy             .size = nb_table,
2237d6ee2a7cSAlexey Kardashevskiy             .flags = 0
2238d6ee2a7cSAlexey Kardashevskiy         };
2239d6ee2a7cSAlexey Kardashevskiy         fd = kvm_vm_ioctl(kvm_state, KVM_CREATE_SPAPR_TCE_64, &args);
2240d6ee2a7cSAlexey Kardashevskiy         if (fd < 0) {
2241d6ee2a7cSAlexey Kardashevskiy             fprintf(stderr,
2242d6ee2a7cSAlexey Kardashevskiy                     "KVM: Failed to create TCE64 table for liobn 0x%x\n",
2243d6ee2a7cSAlexey Kardashevskiy                     liobn);
2244d6ee2a7cSAlexey Kardashevskiy             return NULL;
2245d6ee2a7cSAlexey Kardashevskiy         }
2246d6ee2a7cSAlexey Kardashevskiy     } else if (cap_spapr_tce) {
2247d6ee2a7cSAlexey Kardashevskiy         uint64_t window_size = (uint64_t) nb_table << page_shift;
2248d6ee2a7cSAlexey Kardashevskiy         struct kvm_create_spapr_tce args = {
2249d6ee2a7cSAlexey Kardashevskiy             .liobn = liobn,
2250d6ee2a7cSAlexey Kardashevskiy             .window_size = window_size,
2251d6ee2a7cSAlexey Kardashevskiy         };
2252d6ee2a7cSAlexey Kardashevskiy         if ((window_size != args.window_size) || bus_offset) {
2253d6ee2a7cSAlexey Kardashevskiy             return NULL;
2254d6ee2a7cSAlexey Kardashevskiy         }
22550f5cb298SDavid Gibson         fd = kvm_vm_ioctl(kvm_state, KVM_CREATE_SPAPR_TCE, &args);
22560f5cb298SDavid Gibson         if (fd < 0) {
2257b5aec396SDavid Gibson             fprintf(stderr, "KVM: Failed to create TCE table for liobn 0x%x\n",
2258b5aec396SDavid Gibson                     liobn);
22590f5cb298SDavid Gibson             return NULL;
22600f5cb298SDavid Gibson         }
2261d6ee2a7cSAlexey Kardashevskiy     } else {
2262d6ee2a7cSAlexey Kardashevskiy         return NULL;
2263d6ee2a7cSAlexey Kardashevskiy     }
22640f5cb298SDavid Gibson 
2265d6ee2a7cSAlexey Kardashevskiy     len = nb_table * sizeof(uint64_t);
22660f5cb298SDavid Gibson     /* FIXME: round this up to page size */
22670f5cb298SDavid Gibson 
226874b41e56SDavid Gibson     table = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
22690f5cb298SDavid Gibson     if (table == MAP_FAILED) {
2270b5aec396SDavid Gibson         fprintf(stderr, "KVM: Failed to map TCE table for liobn 0x%x\n",
2271b5aec396SDavid Gibson                 liobn);
22720f5cb298SDavid Gibson         close(fd);
22730f5cb298SDavid Gibson         return NULL;
22740f5cb298SDavid Gibson     }
22750f5cb298SDavid Gibson 
22760f5cb298SDavid Gibson     *pfd = fd;
22770f5cb298SDavid Gibson     return table;
22780f5cb298SDavid Gibson }
22790f5cb298SDavid Gibson 
2280523e7b8aSAlexey Kardashevskiy int kvmppc_remove_spapr_tce(void *table, int fd, uint32_t nb_table)
22810f5cb298SDavid Gibson {
22820f5cb298SDavid Gibson     long len;
22830f5cb298SDavid Gibson 
22840f5cb298SDavid Gibson     if (fd < 0) {
22850f5cb298SDavid Gibson         return -1;
22860f5cb298SDavid Gibson     }
22870f5cb298SDavid Gibson 
2288523e7b8aSAlexey Kardashevskiy     len = nb_table * sizeof(uint64_t);
22890f5cb298SDavid Gibson     if ((munmap(table, len) < 0) ||
22900f5cb298SDavid Gibson         (close(fd) < 0)) {
2291b5aec396SDavid Gibson         fprintf(stderr, "KVM: Unexpected error removing TCE table: %s",
2292b5aec396SDavid Gibson                 strerror(errno));
22930f5cb298SDavid Gibson         /* Leak the table */
22940f5cb298SDavid Gibson     }
22950f5cb298SDavid Gibson 
22960f5cb298SDavid Gibson     return 0;
22970f5cb298SDavid Gibson }
22980f5cb298SDavid Gibson 
22997f763a5dSDavid Gibson int kvmppc_reset_htab(int shift_hint)
23007f763a5dSDavid Gibson {
23017f763a5dSDavid Gibson     uint32_t shift = shift_hint;
23027f763a5dSDavid Gibson 
2303ace9a2cbSDavid Gibson     if (!kvm_enabled()) {
2304ace9a2cbSDavid Gibson         /* Full emulation, tell caller to allocate htab itself */
2305ace9a2cbSDavid Gibson         return 0;
2306ace9a2cbSDavid Gibson     }
23076977afdaSGreg Kurz     if (kvm_vm_check_extension(kvm_state, KVM_CAP_PPC_ALLOC_HTAB)) {
23087f763a5dSDavid Gibson         int ret;
23097f763a5dSDavid Gibson         ret = kvm_vm_ioctl(kvm_state, KVM_PPC_ALLOCATE_HTAB, &shift);
2310ace9a2cbSDavid Gibson         if (ret == -ENOTTY) {
2311c995e942SDavid Gibson             /*
2312c995e942SDavid Gibson              * At least some versions of PR KVM advertise the
2313ace9a2cbSDavid Gibson              * capability, but don't implement the ioctl().  Oops.
2314ace9a2cbSDavid Gibson              * Return 0 so that we allocate the htab in qemu, as is
2315c995e942SDavid Gibson              * correct for PR.
2316c995e942SDavid Gibson              */
2317ace9a2cbSDavid Gibson             return 0;
2318ace9a2cbSDavid Gibson         } else if (ret < 0) {
23197f763a5dSDavid Gibson             return ret;
23207f763a5dSDavid Gibson         }
23217f763a5dSDavid Gibson         return shift;
23227f763a5dSDavid Gibson     }
23237f763a5dSDavid Gibson 
2324c995e942SDavid Gibson     /*
2325c995e942SDavid Gibson      * We have a kernel that predates the htab reset calls.  For PR
2326ace9a2cbSDavid Gibson      * KVM, we need to allocate the htab ourselves, for an HV KVM of
2327c995e942SDavid Gibson      * this era, it has allocated a 16MB fixed size hash table
2328c995e942SDavid Gibson      * already.
2329c995e942SDavid Gibson      */
233096c9cff0SThomas Huth     if (kvmppc_is_pr(kvm_state)) {
2331ace9a2cbSDavid Gibson         /* PR - tell caller to allocate htab */
23327f763a5dSDavid Gibson         return 0;
2333ace9a2cbSDavid Gibson     } else {
2334ace9a2cbSDavid Gibson         /* HV - assume 16MB kernel allocated htab */
2335ace9a2cbSDavid Gibson         return 24;
2336ace9a2cbSDavid Gibson     }
23377f763a5dSDavid Gibson }
23387f763a5dSDavid Gibson 
2339a1e98583SDavid Gibson static inline uint32_t mfpvr(void)
2340a1e98583SDavid Gibson {
2341a1e98583SDavid Gibson     uint32_t pvr;
2342a1e98583SDavid Gibson 
2343a1e98583SDavid Gibson     asm ("mfpvr %0"
2344a1e98583SDavid Gibson          : "=r"(pvr));
2345a1e98583SDavid Gibson     return pvr;
2346a1e98583SDavid Gibson }
2347a1e98583SDavid Gibson 
2348a7342588SDavid Gibson static void alter_insns(uint64_t *word, uint64_t flags, bool on)
2349a7342588SDavid Gibson {
2350a7342588SDavid Gibson     if (on) {
2351a7342588SDavid Gibson         *word |= flags;
2352a7342588SDavid Gibson     } else {
2353a7342588SDavid Gibson         *word &= ~flags;
2354a7342588SDavid Gibson     }
2355a7342588SDavid Gibson }
2356a7342588SDavid Gibson 
23572985b86bSAndreas Färber static void kvmppc_host_cpu_class_init(ObjectClass *oc, void *data)
23582985b86bSAndreas Färber {
23592985b86bSAndreas Färber     PowerPCCPUClass *pcc = POWERPC_CPU_CLASS(oc);
23600cbad81fSDavid Gibson     uint32_t dcache_size = kvmppc_read_int_cpu_dt("d-cache-size");
23610cbad81fSDavid Gibson     uint32_t icache_size = kvmppc_read_int_cpu_dt("i-cache-size");
2362a1e98583SDavid Gibson 
2363cfe34f44SAndreas Färber     /* Now fix up the class with information we can query from the host */
23643bc9ccc0SAlexey Kardashevskiy     pcc->pvr = mfpvr();
2365a7342588SDavid Gibson 
23663f2ca480SDavid Gibson     alter_insns(&pcc->insns_flags, PPC_ALTIVEC,
23673f2ca480SDavid Gibson                 qemu_getauxval(AT_HWCAP) & PPC_FEATURE_HAS_ALTIVEC);
23683f2ca480SDavid Gibson     alter_insns(&pcc->insns_flags2, PPC2_VSX,
23693f2ca480SDavid Gibson                 qemu_getauxval(AT_HWCAP) & PPC_FEATURE_HAS_VSX);
23703f2ca480SDavid Gibson     alter_insns(&pcc->insns_flags2, PPC2_DFP,
23713f2ca480SDavid Gibson                 qemu_getauxval(AT_HWCAP) & PPC_FEATURE_HAS_DFP);
23720cbad81fSDavid Gibson 
23730cbad81fSDavid Gibson     if (dcache_size != -1) {
23740cbad81fSDavid Gibson         pcc->l1_dcache_size = dcache_size;
23750cbad81fSDavid Gibson     }
23760cbad81fSDavid Gibson 
23770cbad81fSDavid Gibson     if (icache_size != -1) {
23780cbad81fSDavid Gibson         pcc->l1_icache_size = icache_size;
23790cbad81fSDavid Gibson     }
2380c64abd1fSSam Bobroff 
2381c64abd1fSSam Bobroff #if defined(TARGET_PPC64)
2382c64abd1fSSam Bobroff     pcc->radix_page_info = kvm_get_radix_page_info();
23835f3066d8SDavid Gibson 
23845f3066d8SDavid Gibson     if ((pcc->pvr & 0xffffff00) == CPU_POWERPC_POWER9_DD1) {
23855f3066d8SDavid Gibson         /*
23865f3066d8SDavid Gibson          * POWER9 DD1 has some bugs which make it not really ISA 3.00
23875f3066d8SDavid Gibson          * compliant.  More importantly, advertising ISA 3.00
23885f3066d8SDavid Gibson          * architected mode may prevent guests from activating
23895f3066d8SDavid Gibson          * necessary DD1 workarounds.
23905f3066d8SDavid Gibson          */
23915f3066d8SDavid Gibson         pcc->pcr_supported &= ~(PCR_COMPAT_3_00 | PCR_COMPAT_2_07
23925f3066d8SDavid Gibson                                 | PCR_COMPAT_2_06 | PCR_COMPAT_2_05);
23935f3066d8SDavid Gibson     }
2394c64abd1fSSam Bobroff #endif /* defined(TARGET_PPC64) */
2395a1e98583SDavid Gibson }
2396a1e98583SDavid Gibson 
23973b961124SStuart Yoder bool kvmppc_has_cap_epr(void)
23983b961124SStuart Yoder {
23993b961124SStuart Yoder     return cap_epr;
24003b961124SStuart Yoder }
24013b961124SStuart Yoder 
240287a91de6SAlexander Graf bool kvmppc_has_cap_fixup_hcalls(void)
240387a91de6SAlexander Graf {
240487a91de6SAlexander Graf     return cap_fixup_hcalls;
240587a91de6SAlexander Graf }
240687a91de6SAlexander Graf 
2407bac3bf28SThomas Huth bool kvmppc_has_cap_htm(void)
2408bac3bf28SThomas Huth {
2409bac3bf28SThomas Huth     return cap_htm;
2410bac3bf28SThomas Huth }
2411bac3bf28SThomas Huth 
2412cf1c4cceSSam Bobroff bool kvmppc_has_cap_mmu_radix(void)
2413cf1c4cceSSam Bobroff {
2414cf1c4cceSSam Bobroff     return cap_mmu_radix;
2415cf1c4cceSSam Bobroff }
2416cf1c4cceSSam Bobroff 
2417cf1c4cceSSam Bobroff bool kvmppc_has_cap_mmu_hash_v3(void)
2418cf1c4cceSSam Bobroff {
2419cf1c4cceSSam Bobroff     return cap_mmu_hash_v3;
2420cf1c4cceSSam Bobroff }
2421cf1c4cceSSam Bobroff 
2422072f416aSSuraj Jitindar Singh static bool kvmppc_power8_host(void)
2423072f416aSSuraj Jitindar Singh {
2424072f416aSSuraj Jitindar Singh     bool ret = false;
2425072f416aSSuraj Jitindar Singh #ifdef TARGET_PPC64
2426072f416aSSuraj Jitindar Singh     {
2427072f416aSSuraj Jitindar Singh         uint32_t base_pvr = CPU_POWERPC_POWER_SERVER_MASK & mfpvr();
2428072f416aSSuraj Jitindar Singh         ret = (base_pvr == CPU_POWERPC_POWER8E_BASE) ||
2429072f416aSSuraj Jitindar Singh               (base_pvr == CPU_POWERPC_POWER8NVL_BASE) ||
2430072f416aSSuraj Jitindar Singh               (base_pvr == CPU_POWERPC_POWER8_BASE);
2431072f416aSSuraj Jitindar Singh     }
2432072f416aSSuraj Jitindar Singh #endif /* TARGET_PPC64 */
2433072f416aSSuraj Jitindar Singh     return ret;
2434072f416aSSuraj Jitindar Singh }
2435072f416aSSuraj Jitindar Singh 
24368fea7044SSuraj Jitindar Singh static int parse_cap_ppc_safe_cache(struct kvm_ppc_cpu_char c)
24378fea7044SSuraj Jitindar Singh {
2438072f416aSSuraj Jitindar Singh     bool l1d_thread_priv_req = !kvmppc_power8_host();
2439072f416aSSuraj Jitindar Singh 
24408fea7044SSuraj Jitindar Singh     if (~c.behaviour & c.behaviour_mask & H_CPU_BEHAV_L1D_FLUSH_PR) {
24418fea7044SSuraj Jitindar Singh         return 2;
2442072f416aSSuraj Jitindar Singh     } else if ((!l1d_thread_priv_req ||
2443072f416aSSuraj Jitindar Singh                 c.character & c.character_mask & H_CPU_CHAR_L1D_THREAD_PRIV) &&
24448fea7044SSuraj Jitindar Singh                (c.character & c.character_mask
24458fea7044SSuraj Jitindar Singh                 & (H_CPU_CHAR_L1D_FLUSH_ORI30 | H_CPU_CHAR_L1D_FLUSH_TRIG2))) {
24468fea7044SSuraj Jitindar Singh         return 1;
24478fea7044SSuraj Jitindar Singh     }
24488fea7044SSuraj Jitindar Singh 
24498fea7044SSuraj Jitindar Singh     return 0;
24508fea7044SSuraj Jitindar Singh }
24518fea7044SSuraj Jitindar Singh 
24528fea7044SSuraj Jitindar Singh static int parse_cap_ppc_safe_bounds_check(struct kvm_ppc_cpu_char c)
24538fea7044SSuraj Jitindar Singh {
24548fea7044SSuraj Jitindar Singh     if (~c.behaviour & c.behaviour_mask & H_CPU_BEHAV_BNDS_CHK_SPEC_BAR) {
24558fea7044SSuraj Jitindar Singh         return 2;
24568fea7044SSuraj Jitindar Singh     } else if (c.character & c.character_mask & H_CPU_CHAR_SPEC_BAR_ORI31) {
24578fea7044SSuraj Jitindar Singh         return 1;
24588fea7044SSuraj Jitindar Singh     }
24598fea7044SSuraj Jitindar Singh 
24608fea7044SSuraj Jitindar Singh     return 0;
24618fea7044SSuraj Jitindar Singh }
24628fea7044SSuraj Jitindar Singh 
24638fea7044SSuraj Jitindar Singh static int parse_cap_ppc_safe_indirect_branch(struct kvm_ppc_cpu_char c)
24648fea7044SSuraj Jitindar Singh {
2465399b2896SSuraj Jitindar Singh     if ((~c.behaviour & c.behaviour_mask & H_CPU_BEHAV_FLUSH_COUNT_CACHE) &&
2466399b2896SSuraj Jitindar Singh         (~c.character & c.character_mask & H_CPU_CHAR_CACHE_COUNT_DIS) &&
2467399b2896SSuraj Jitindar Singh         (~c.character & c.character_mask & H_CPU_CHAR_BCCTRL_SERIALISED)) {
2468399b2896SSuraj Jitindar Singh         return SPAPR_CAP_FIXED_NA;
2469399b2896SSuraj Jitindar Singh     } else if (c.behaviour & c.behaviour_mask & H_CPU_BEHAV_FLUSH_COUNT_CACHE) {
2470399b2896SSuraj Jitindar Singh         return SPAPR_CAP_WORKAROUND;
2471399b2896SSuraj Jitindar Singh     } else if (c.character & c.character_mask & H_CPU_CHAR_CACHE_COUNT_DIS) {
24728fea7044SSuraj Jitindar Singh         return  SPAPR_CAP_FIXED_CCD;
24738fea7044SSuraj Jitindar Singh     } else if (c.character & c.character_mask & H_CPU_CHAR_BCCTRL_SERIALISED) {
24748fea7044SSuraj Jitindar Singh         return SPAPR_CAP_FIXED_IBS;
24758fea7044SSuraj Jitindar Singh     }
24768fea7044SSuraj Jitindar Singh 
24778fea7044SSuraj Jitindar Singh     return 0;
24788fea7044SSuraj Jitindar Singh }
24798fea7044SSuraj Jitindar Singh 
24808ff43ee4SSuraj Jitindar Singh static int parse_cap_ppc_count_cache_flush_assist(struct kvm_ppc_cpu_char c)
24818ff43ee4SSuraj Jitindar Singh {
24828ff43ee4SSuraj Jitindar Singh     if (c.character & c.character_mask & H_CPU_CHAR_BCCTR_FLUSH_ASSIST) {
24838ff43ee4SSuraj Jitindar Singh         return 1;
24848ff43ee4SSuraj Jitindar Singh     }
24858ff43ee4SSuraj Jitindar Singh     return 0;
24868ff43ee4SSuraj Jitindar Singh }
24878ff43ee4SSuraj Jitindar Singh 
248838afd772SCédric Le Goater bool kvmppc_has_cap_xive(void)
248938afd772SCédric Le Goater {
249038afd772SCédric Le Goater     return cap_xive;
249138afd772SCédric Le Goater }
249238afd772SCédric Le Goater 
24938acc2ae5SSuraj Jitindar Singh static void kvmppc_get_cpu_characteristics(KVMState *s)
24948acc2ae5SSuraj Jitindar Singh {
24958acc2ae5SSuraj Jitindar Singh     struct kvm_ppc_cpu_char c;
24968acc2ae5SSuraj Jitindar Singh     int ret;
24978acc2ae5SSuraj Jitindar Singh 
24988acc2ae5SSuraj Jitindar Singh     /* Assume broken */
24998acc2ae5SSuraj Jitindar Singh     cap_ppc_safe_cache = 0;
25008acc2ae5SSuraj Jitindar Singh     cap_ppc_safe_bounds_check = 0;
25018acc2ae5SSuraj Jitindar Singh     cap_ppc_safe_indirect_branch = 0;
25028acc2ae5SSuraj Jitindar Singh 
25038acc2ae5SSuraj Jitindar Singh     ret = kvm_vm_check_extension(s, KVM_CAP_PPC_GET_CPU_CHAR);
25048acc2ae5SSuraj Jitindar Singh     if (!ret) {
25058acc2ae5SSuraj Jitindar Singh         return;
25068acc2ae5SSuraj Jitindar Singh     }
25078acc2ae5SSuraj Jitindar Singh     ret = kvm_vm_ioctl(s, KVM_PPC_GET_CPU_CHAR, &c);
25088acc2ae5SSuraj Jitindar Singh     if (ret < 0) {
25098acc2ae5SSuraj Jitindar Singh         return;
25108acc2ae5SSuraj Jitindar Singh     }
25118fea7044SSuraj Jitindar Singh 
25128fea7044SSuraj Jitindar Singh     cap_ppc_safe_cache = parse_cap_ppc_safe_cache(c);
25138fea7044SSuraj Jitindar Singh     cap_ppc_safe_bounds_check = parse_cap_ppc_safe_bounds_check(c);
25148fea7044SSuraj Jitindar Singh     cap_ppc_safe_indirect_branch = parse_cap_ppc_safe_indirect_branch(c);
25158ff43ee4SSuraj Jitindar Singh     cap_ppc_count_cache_flush_assist =
25168ff43ee4SSuraj Jitindar Singh         parse_cap_ppc_count_cache_flush_assist(c);
25178acc2ae5SSuraj Jitindar Singh }
25188acc2ae5SSuraj Jitindar Singh 
25198acc2ae5SSuraj Jitindar Singh int kvmppc_get_cap_safe_cache(void)
25208acc2ae5SSuraj Jitindar Singh {
25218acc2ae5SSuraj Jitindar Singh     return cap_ppc_safe_cache;
25228acc2ae5SSuraj Jitindar Singh }
25238acc2ae5SSuraj Jitindar Singh 
25248acc2ae5SSuraj Jitindar Singh int kvmppc_get_cap_safe_bounds_check(void)
25258acc2ae5SSuraj Jitindar Singh {
25268acc2ae5SSuraj Jitindar Singh     return cap_ppc_safe_bounds_check;
25278acc2ae5SSuraj Jitindar Singh }
25288acc2ae5SSuraj Jitindar Singh 
25298acc2ae5SSuraj Jitindar Singh int kvmppc_get_cap_safe_indirect_branch(void)
25308acc2ae5SSuraj Jitindar Singh {
25318acc2ae5SSuraj Jitindar Singh     return cap_ppc_safe_indirect_branch;
25328acc2ae5SSuraj Jitindar Singh }
25338acc2ae5SSuraj Jitindar Singh 
25348ff43ee4SSuraj Jitindar Singh int kvmppc_get_cap_count_cache_flush_assist(void)
25358ff43ee4SSuraj Jitindar Singh {
25368ff43ee4SSuraj Jitindar Singh     return cap_ppc_count_cache_flush_assist;
25378ff43ee4SSuraj Jitindar Singh }
25388ff43ee4SSuraj Jitindar Singh 
2539b9a477b7SSuraj Jitindar Singh bool kvmppc_has_cap_nested_kvm_hv(void)
2540b9a477b7SSuraj Jitindar Singh {
2541b9a477b7SSuraj Jitindar Singh     return !!cap_ppc_nested_kvm_hv;
2542b9a477b7SSuraj Jitindar Singh }
2543b9a477b7SSuraj Jitindar Singh 
2544b9a477b7SSuraj Jitindar Singh int kvmppc_set_cap_nested_kvm_hv(int enable)
2545b9a477b7SSuraj Jitindar Singh {
2546b9a477b7SSuraj Jitindar Singh     return kvm_vm_enable_cap(kvm_state, KVM_CAP_PPC_NESTED_HV, 0, enable);
2547b9a477b7SSuraj Jitindar Singh }
2548b9a477b7SSuraj Jitindar Singh 
25499ded780cSAlexey Kardashevskiy bool kvmppc_has_cap_spapr_vfio(void)
25509ded780cSAlexey Kardashevskiy {
25519ded780cSAlexey Kardashevskiy     return cap_spapr_vfio;
25529ded780cSAlexey Kardashevskiy }
25539ded780cSAlexey Kardashevskiy 
25547d050527SSuraj Jitindar Singh int kvmppc_get_cap_large_decr(void)
25557d050527SSuraj Jitindar Singh {
25567d050527SSuraj Jitindar Singh     return cap_large_decr;
25577d050527SSuraj Jitindar Singh }
25587d050527SSuraj Jitindar Singh 
25597d050527SSuraj Jitindar Singh int kvmppc_enable_cap_large_decr(PowerPCCPU *cpu, int enable)
25607d050527SSuraj Jitindar Singh {
25617d050527SSuraj Jitindar Singh     CPUState *cs = CPU(cpu);
25627d050527SSuraj Jitindar Singh     uint64_t lpcr;
25637d050527SSuraj Jitindar Singh 
25647d050527SSuraj Jitindar Singh     kvm_get_one_reg(cs, KVM_REG_PPC_LPCR_64, &lpcr);
25657d050527SSuraj Jitindar Singh     /* Do we need to modify the LPCR? */
25667d050527SSuraj Jitindar Singh     if (!!(lpcr & LPCR_LD) != !!enable) {
25677d050527SSuraj Jitindar Singh         if (enable) {
25687d050527SSuraj Jitindar Singh             lpcr |= LPCR_LD;
25697d050527SSuraj Jitindar Singh         } else {
25707d050527SSuraj Jitindar Singh             lpcr &= ~LPCR_LD;
25717d050527SSuraj Jitindar Singh         }
25727d050527SSuraj Jitindar Singh         kvm_set_one_reg(cs, KVM_REG_PPC_LPCR_64, &lpcr);
25737d050527SSuraj Jitindar Singh         kvm_get_one_reg(cs, KVM_REG_PPC_LPCR_64, &lpcr);
25747d050527SSuraj Jitindar Singh 
25757d050527SSuraj Jitindar Singh         if (!!(lpcr & LPCR_LD) != !!enable) {
25767d050527SSuraj Jitindar Singh             return -1;
25777d050527SSuraj Jitindar Singh         }
25787d050527SSuraj Jitindar Singh     }
25797d050527SSuraj Jitindar Singh 
25807d050527SSuraj Jitindar Singh     return 0;
25817d050527SSuraj Jitindar Singh }
25827d050527SSuraj Jitindar Singh 
258352b2519cSThomas Huth PowerPCCPUClass *kvm_ppc_get_host_cpu_class(void)
258452b2519cSThomas Huth {
258552b2519cSThomas Huth     uint32_t host_pvr = mfpvr();
258652b2519cSThomas Huth     PowerPCCPUClass *pvr_pcc;
258752b2519cSThomas Huth 
258852b2519cSThomas Huth     pvr_pcc = ppc_cpu_class_by_pvr(host_pvr);
258952b2519cSThomas Huth     if (pvr_pcc == NULL) {
259052b2519cSThomas Huth         pvr_pcc = ppc_cpu_class_by_pvr_mask(host_pvr);
259152b2519cSThomas Huth     }
259252b2519cSThomas Huth 
259352b2519cSThomas Huth     return pvr_pcc;
259452b2519cSThomas Huth }
259552b2519cSThomas Huth 
25962e9c10ebSIgor Mammedov static int kvm_ppc_register_host_cpu_type(MachineState *ms)
25975ba4576bSAndreas Färber {
25985ba4576bSAndreas Färber     TypeInfo type_info = {
25995ba4576bSAndreas Färber         .name = TYPE_HOST_POWERPC_CPU,
26005ba4576bSAndreas Färber         .class_init = kvmppc_host_cpu_class_init,
26015ba4576bSAndreas Färber     };
26022e9c10ebSIgor Mammedov     MachineClass *mc = MACHINE_GET_CLASS(ms);
26035ba4576bSAndreas Färber     PowerPCCPUClass *pvr_pcc;
260492e926e1SGreg Kurz     ObjectClass *oc;
26055b79b1caSAlexey Kardashevskiy     DeviceClass *dc;
2606715d4b96SThomas Huth     int i;
26075ba4576bSAndreas Färber 
260852b2519cSThomas Huth     pvr_pcc = kvm_ppc_get_host_cpu_class();
26093bc9ccc0SAlexey Kardashevskiy     if (pvr_pcc == NULL) {
26105ba4576bSAndreas Färber         return -1;
26115ba4576bSAndreas Färber     }
26125ba4576bSAndreas Färber     type_info.parent = object_class_get_name(OBJECT_CLASS(pvr_pcc));
26135ba4576bSAndreas Färber     type_register(&type_info);
26142e9c10ebSIgor Mammedov     if (object_dynamic_cast(OBJECT(ms), TYPE_SPAPR_MACHINE)) {
26152e9c10ebSIgor Mammedov         /* override TCG default cpu type with 'host' cpu model */
26162e9c10ebSIgor Mammedov         mc->default_cpu_type = TYPE_HOST_POWERPC_CPU;
26172e9c10ebSIgor Mammedov     }
26185b79b1caSAlexey Kardashevskiy 
261992e926e1SGreg Kurz     oc = object_class_by_name(type_info.name);
262092e926e1SGreg Kurz     g_assert(oc);
262192e926e1SGreg Kurz 
2622715d4b96SThomas Huth     /*
2623715d4b96SThomas Huth      * Update generic CPU family class alias (e.g. on a POWER8NVL host,
2624715d4b96SThomas Huth      * we want "POWER8" to be a "family" alias that points to the current
2625715d4b96SThomas Huth      * host CPU type, too)
2626715d4b96SThomas Huth      */
2627715d4b96SThomas Huth     dc = DEVICE_CLASS(ppc_cpu_get_family_class(pvr_pcc));
2628715d4b96SThomas Huth     for (i = 0; ppc_cpu_aliases[i].alias != NULL; i++) {
2629c5354f54SIgor Mammedov         if (strcasecmp(ppc_cpu_aliases[i].alias, dc->desc) == 0) {
2630715d4b96SThomas Huth             char *suffix;
2631715d4b96SThomas Huth 
2632715d4b96SThomas Huth             ppc_cpu_aliases[i].model = g_strdup(object_class_get_name(oc));
2633c9137065SIgor Mammedov             suffix = strstr(ppc_cpu_aliases[i].model, POWERPC_CPU_TYPE_SUFFIX);
2634715d4b96SThomas Huth             if (suffix) {
2635715d4b96SThomas Huth                 *suffix = 0;
2636715d4b96SThomas Huth             }
2637715d4b96SThomas Huth             break;
2638715d4b96SThomas Huth         }
2639715d4b96SThomas Huth     }
2640715d4b96SThomas Huth 
26415ba4576bSAndreas Färber     return 0;
26425ba4576bSAndreas Färber }
26435ba4576bSAndreas Färber 
2644feaa64c4SDavid Gibson int kvmppc_define_rtas_kernel_token(uint32_t token, const char *function)
2645feaa64c4SDavid Gibson {
2646feaa64c4SDavid Gibson     struct kvm_rtas_token_args args = {
2647feaa64c4SDavid Gibson         .token = token,
2648feaa64c4SDavid Gibson     };
2649feaa64c4SDavid Gibson 
2650feaa64c4SDavid Gibson     if (!kvm_check_extension(kvm_state, KVM_CAP_PPC_RTAS)) {
2651feaa64c4SDavid Gibson         return -ENOENT;
2652feaa64c4SDavid Gibson     }
2653feaa64c4SDavid Gibson 
26547701aeedSCédric Le Goater     strncpy(args.name, function, sizeof(args.name) - 1);
2655feaa64c4SDavid Gibson 
2656feaa64c4SDavid Gibson     return kvm_vm_ioctl(kvm_state, KVM_PPC_RTAS_DEFINE_TOKEN, &args);
2657feaa64c4SDavid Gibson }
265812b1143bSDavid Gibson 
265914b0d748SGreg Kurz int kvmppc_get_htab_fd(bool write, uint64_t index, Error **errp)
2660e68cb8b4SAlexey Kardashevskiy {
2661e68cb8b4SAlexey Kardashevskiy     struct kvm_get_htab_fd s = {
2662e68cb8b4SAlexey Kardashevskiy         .flags = write ? KVM_GET_HTAB_WRITE : 0,
266314b0d748SGreg Kurz         .start_index = index,
2664e68cb8b4SAlexey Kardashevskiy     };
266582be8e73SGreg Kurz     int ret;
2666e68cb8b4SAlexey Kardashevskiy 
2667e68cb8b4SAlexey Kardashevskiy     if (!cap_htab_fd) {
266814b0d748SGreg Kurz         error_setg(errp, "KVM version doesn't support %s the HPT",
266914b0d748SGreg Kurz                    write ? "writing" : "reading");
267082be8e73SGreg Kurz         return -ENOTSUP;
2671e68cb8b4SAlexey Kardashevskiy     }
2672e68cb8b4SAlexey Kardashevskiy 
267382be8e73SGreg Kurz     ret = kvm_vm_ioctl(kvm_state, KVM_PPC_GET_HTAB_FD, &s);
267482be8e73SGreg Kurz     if (ret < 0) {
267514b0d748SGreg Kurz         error_setg(errp, "Unable to open fd for %s HPT %s KVM: %s",
267614b0d748SGreg Kurz                    write ? "writing" : "reading", write ? "to" : "from",
267714b0d748SGreg Kurz                    strerror(errno));
267882be8e73SGreg Kurz         return -errno;
267982be8e73SGreg Kurz     }
268082be8e73SGreg Kurz 
268182be8e73SGreg Kurz     return ret;
2682e68cb8b4SAlexey Kardashevskiy }
2683e68cb8b4SAlexey Kardashevskiy 
2684e68cb8b4SAlexey Kardashevskiy int kvmppc_save_htab(QEMUFile *f, int fd, size_t bufsize, int64_t max_ns)
2685e68cb8b4SAlexey Kardashevskiy {
2686bc72ad67SAlex Bligh     int64_t starttime = qemu_clock_get_ns(QEMU_CLOCK_REALTIME);
2687e68cb8b4SAlexey Kardashevskiy     uint8_t buf[bufsize];
2688e68cb8b4SAlexey Kardashevskiy     ssize_t rc;
2689e68cb8b4SAlexey Kardashevskiy 
2690e68cb8b4SAlexey Kardashevskiy     do {
2691e68cb8b4SAlexey Kardashevskiy         rc = read(fd, buf, bufsize);
2692e68cb8b4SAlexey Kardashevskiy         if (rc < 0) {
2693e68cb8b4SAlexey Kardashevskiy             fprintf(stderr, "Error reading data from KVM HTAB fd: %s\n",
2694e68cb8b4SAlexey Kardashevskiy                     strerror(errno));
2695e68cb8b4SAlexey Kardashevskiy             return rc;
2696e68cb8b4SAlexey Kardashevskiy         } else if (rc) {
2697e094c4c1SCédric Le Goater             uint8_t *buffer = buf;
2698e094c4c1SCédric Le Goater             ssize_t n = rc;
2699e094c4c1SCédric Le Goater             while (n) {
2700e094c4c1SCédric Le Goater                 struct kvm_get_htab_header *head =
2701e094c4c1SCédric Le Goater                     (struct kvm_get_htab_header *) buffer;
2702e094c4c1SCédric Le Goater                 size_t chunksize = sizeof(*head) +
2703e094c4c1SCédric Le Goater                      HASH_PTE_SIZE_64 * head->n_valid;
2704e094c4c1SCédric Le Goater 
2705e094c4c1SCédric Le Goater                 qemu_put_be32(f, head->index);
2706e094c4c1SCédric Le Goater                 qemu_put_be16(f, head->n_valid);
2707e094c4c1SCédric Le Goater                 qemu_put_be16(f, head->n_invalid);
2708e094c4c1SCédric Le Goater                 qemu_put_buffer(f, (void *)(head + 1),
2709e094c4c1SCédric Le Goater                                 HASH_PTE_SIZE_64 * head->n_valid);
2710e094c4c1SCédric Le Goater 
2711e094c4c1SCédric Le Goater                 buffer += chunksize;
2712e094c4c1SCédric Le Goater                 n -= chunksize;
2713e094c4c1SCédric Le Goater             }
2714e68cb8b4SAlexey Kardashevskiy         }
2715e68cb8b4SAlexey Kardashevskiy     } while ((rc != 0)
2716c995e942SDavid Gibson              && ((max_ns < 0) ||
2717c995e942SDavid Gibson                  ((qemu_clock_get_ns(QEMU_CLOCK_REALTIME) - starttime) < max_ns)));
2718e68cb8b4SAlexey Kardashevskiy 
2719e68cb8b4SAlexey Kardashevskiy     return (rc == 0) ? 1 : 0;
2720e68cb8b4SAlexey Kardashevskiy }
2721e68cb8b4SAlexey Kardashevskiy 
2722e68cb8b4SAlexey Kardashevskiy int kvmppc_load_htab_chunk(QEMUFile *f, int fd, uint32_t index,
2723e68cb8b4SAlexey Kardashevskiy                            uint16_t n_valid, uint16_t n_invalid)
2724e68cb8b4SAlexey Kardashevskiy {
2725e68cb8b4SAlexey Kardashevskiy     struct kvm_get_htab_header *buf;
2726e68cb8b4SAlexey Kardashevskiy     size_t chunksize = sizeof(*buf) + n_valid * HASH_PTE_SIZE_64;
2727e68cb8b4SAlexey Kardashevskiy     ssize_t rc;
2728e68cb8b4SAlexey Kardashevskiy 
2729e68cb8b4SAlexey Kardashevskiy     buf = alloca(chunksize);
2730e68cb8b4SAlexey Kardashevskiy     buf->index = index;
2731e68cb8b4SAlexey Kardashevskiy     buf->n_valid = n_valid;
2732e68cb8b4SAlexey Kardashevskiy     buf->n_invalid = n_invalid;
2733e68cb8b4SAlexey Kardashevskiy 
2734e68cb8b4SAlexey Kardashevskiy     qemu_get_buffer(f, (void *)(buf + 1), HASH_PTE_SIZE_64 * n_valid);
2735e68cb8b4SAlexey Kardashevskiy 
2736e68cb8b4SAlexey Kardashevskiy     rc = write(fd, buf, chunksize);
2737e68cb8b4SAlexey Kardashevskiy     if (rc < 0) {
2738e68cb8b4SAlexey Kardashevskiy         fprintf(stderr, "Error writing KVM hash table: %s\n",
2739e68cb8b4SAlexey Kardashevskiy                 strerror(errno));
2740e68cb8b4SAlexey Kardashevskiy         return rc;
2741e68cb8b4SAlexey Kardashevskiy     }
2742e68cb8b4SAlexey Kardashevskiy     if (rc != chunksize) {
2743e68cb8b4SAlexey Kardashevskiy         /* We should never get a short write on a single chunk */
2744e68cb8b4SAlexey Kardashevskiy         fprintf(stderr, "Short write, restoring KVM hash table\n");
2745e68cb8b4SAlexey Kardashevskiy         return -1;
2746e68cb8b4SAlexey Kardashevskiy     }
2747e68cb8b4SAlexey Kardashevskiy     return 0;
2748e68cb8b4SAlexey Kardashevskiy }
2749e68cb8b4SAlexey Kardashevskiy 
275020d695a9SAndreas Färber bool kvm_arch_stop_on_emulation_error(CPUState *cpu)
27514513d923SGleb Natapov {
27524513d923SGleb Natapov     return true;
27534513d923SGleb Natapov }
2754a1b87fe0SJan Kiszka 
275582169660SScott Wood void kvm_arch_init_irq_routing(KVMState *s)
275682169660SScott Wood {
275782169660SScott Wood }
2758c65f9a07SGreg Kurz 
27591ad9f0a4SDavid Gibson void kvmppc_read_hptes(ppc_hash_pte64_t *hptes, hwaddr ptex, int n)
27601ad9f0a4SDavid Gibson {
27611ad9f0a4SDavid Gibson     int fd, rc;
27621ad9f0a4SDavid Gibson     int i;
27637c43bca0SAneesh Kumar K.V 
276414b0d748SGreg Kurz     fd = kvmppc_get_htab_fd(false, ptex, &error_abort);
27651ad9f0a4SDavid Gibson 
27661ad9f0a4SDavid Gibson     i = 0;
27671ad9f0a4SDavid Gibson     while (i < n) {
27681ad9f0a4SDavid Gibson         struct kvm_get_htab_header *hdr;
27691ad9f0a4SDavid Gibson         int m = n < HPTES_PER_GROUP ? n : HPTES_PER_GROUP;
27701ad9f0a4SDavid Gibson         char buf[sizeof(*hdr) + m * HASH_PTE_SIZE_64];
27711ad9f0a4SDavid Gibson 
27721ad9f0a4SDavid Gibson         rc = read(fd, buf, sizeof(buf));
27731ad9f0a4SDavid Gibson         if (rc < 0) {
27741ad9f0a4SDavid Gibson             hw_error("kvmppc_read_hptes: Unable to read HPTEs");
27751ad9f0a4SDavid Gibson         }
27761ad9f0a4SDavid Gibson 
27771ad9f0a4SDavid Gibson         hdr = (struct kvm_get_htab_header *)buf;
27781ad9f0a4SDavid Gibson         while ((i < n) && ((char *)hdr < (buf + rc))) {
2779a36593e1SAlexey Kardashevskiy             int invalid = hdr->n_invalid, valid = hdr->n_valid;
27801ad9f0a4SDavid Gibson 
27811ad9f0a4SDavid Gibson             if (hdr->index != (ptex + i)) {
27821ad9f0a4SDavid Gibson                 hw_error("kvmppc_read_hptes: Unexpected HPTE index %"PRIu32
27831ad9f0a4SDavid Gibson                          " != (%"HWADDR_PRIu" + %d", hdr->index, ptex, i);
27841ad9f0a4SDavid Gibson             }
27851ad9f0a4SDavid Gibson 
2786a36593e1SAlexey Kardashevskiy             if (n - i < valid) {
2787a36593e1SAlexey Kardashevskiy                 valid = n - i;
2788a36593e1SAlexey Kardashevskiy             }
2789a36593e1SAlexey Kardashevskiy             memcpy(hptes + i, hdr + 1, HASH_PTE_SIZE_64 * valid);
2790a36593e1SAlexey Kardashevskiy             i += valid;
27911ad9f0a4SDavid Gibson 
27921ad9f0a4SDavid Gibson             if ((n - i) < invalid) {
27931ad9f0a4SDavid Gibson                 invalid = n - i;
27941ad9f0a4SDavid Gibson             }
27951ad9f0a4SDavid Gibson             memset(hptes + i, 0, invalid * HASH_PTE_SIZE_64);
2796a36593e1SAlexey Kardashevskiy             i += invalid;
27971ad9f0a4SDavid Gibson 
27981ad9f0a4SDavid Gibson             hdr = (struct kvm_get_htab_header *)
27991ad9f0a4SDavid Gibson                 ((char *)(hdr + 1) + HASH_PTE_SIZE_64 * hdr->n_valid);
28001ad9f0a4SDavid Gibson         }
28011ad9f0a4SDavid Gibson     }
28021ad9f0a4SDavid Gibson 
28031ad9f0a4SDavid Gibson     close(fd);
28041ad9f0a4SDavid Gibson }
28051ad9f0a4SDavid Gibson 
28061ad9f0a4SDavid Gibson void kvmppc_write_hpte(hwaddr ptex, uint64_t pte0, uint64_t pte1)
28077c43bca0SAneesh Kumar K.V {
28081ad9f0a4SDavid Gibson     int fd, rc;
28091ad9f0a4SDavid Gibson     struct {
28101ad9f0a4SDavid Gibson         struct kvm_get_htab_header hdr;
28111ad9f0a4SDavid Gibson         uint64_t pte0;
28121ad9f0a4SDavid Gibson         uint64_t pte1;
28131ad9f0a4SDavid Gibson     } buf;
2814c1385933SAneesh Kumar K.V 
281514b0d748SGreg Kurz     fd = kvmppc_get_htab_fd(true, 0 /* Ignored */, &error_abort);
2816c1385933SAneesh Kumar K.V 
28171ad9f0a4SDavid Gibson     buf.hdr.n_valid = 1;
28181ad9f0a4SDavid Gibson     buf.hdr.n_invalid = 0;
28191ad9f0a4SDavid Gibson     buf.hdr.index = ptex;
28201ad9f0a4SDavid Gibson     buf.pte0 = cpu_to_be64(pte0);
28211ad9f0a4SDavid Gibson     buf.pte1 = cpu_to_be64(pte1);
28221ad9f0a4SDavid Gibson 
28231ad9f0a4SDavid Gibson     rc = write(fd, &buf, sizeof(buf));
28241ad9f0a4SDavid Gibson     if (rc != sizeof(buf)) {
28251ad9f0a4SDavid Gibson         hw_error("kvmppc_write_hpte: Unable to update KVM HPT");
2826c1385933SAneesh Kumar K.V     }
28271ad9f0a4SDavid Gibson     close(fd);
2828c1385933SAneesh Kumar K.V }
28299e03a040SFrank Blaschka 
28309e03a040SFrank Blaschka int kvm_arch_fixup_msi_route(struct kvm_irq_routing_entry *route,
2831dc9f06caSPavel Fedin                              uint64_t address, uint32_t data, PCIDevice *dev)
28329e03a040SFrank Blaschka {
28339e03a040SFrank Blaschka     return 0;
28349e03a040SFrank Blaschka }
28351850b6b7SEric Auger 
283638d87493SPeter Xu int kvm_arch_add_msi_route_post(struct kvm_irq_routing_entry *route,
283738d87493SPeter Xu                                 int vector, PCIDevice *dev)
283838d87493SPeter Xu {
283938d87493SPeter Xu     return 0;
284038d87493SPeter Xu }
284138d87493SPeter Xu 
284238d87493SPeter Xu int kvm_arch_release_virq_post(int virq)
284338d87493SPeter Xu {
284438d87493SPeter Xu     return 0;
284538d87493SPeter Xu }
284638d87493SPeter Xu 
28471850b6b7SEric Auger int kvm_arch_msi_data_to_gsi(uint32_t data)
28481850b6b7SEric Auger {
28491850b6b7SEric Auger     return data & 0xffff;
28501850b6b7SEric Auger }
28514d9392beSThomas Huth 
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 }
2957