xref: /qemu/target/ppc/kvm.c (revision db70b31144d28a40838f8916a7c02adcdf5d8dcd)
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"
4131f2cb8fSBharat Bhushan #include "sysemu/watchdog.h"
42b36f100eSAlexey Kardashevskiy #include "trace.h"
4388365d17SBharat Bhushan #include "exec/gdbstub.h"
444c663752SPaolo Bonzini #include "exec/memattrs.h"
459c607668SAlexey Kardashevskiy #include "exec/ram_addr.h"
462d103aaeSMichael Roth #include "sysemu/hostmem.h"
47f348b6d1SVeronia Bahaa #include "qemu/cutils.h"
489c607668SAlexey Kardashevskiy #include "qemu/mmap-alloc.h"
49f3d9f303SSam Bobroff #include "elf.h"
50c64abd1fSSam Bobroff #include "sysemu/kvm_int.h"
51f61b4bedSAlexander Graf 
52eadaada1SAlexander Graf #define PROC_DEVTREE_CPU      "/proc/device-tree/cpus/"
53eadaada1SAlexander Graf 
5494a8d39aSJan Kiszka const KVMCapabilityInfo kvm_arch_required_capabilities[] = {
5594a8d39aSJan Kiszka     KVM_CAP_LAST_INFO
5694a8d39aSJan Kiszka };
5794a8d39aSJan Kiszka 
58c995e942SDavid Gibson static int cap_interrupt_unset;
59c995e942SDavid Gibson static int cap_interrupt_level;
6090dc8812SScott Wood static int cap_segstate;
6190dc8812SScott Wood static int cap_booke_sregs;
62e97c3636SDavid Gibson static int cap_ppc_smt;
63fa98fbfcSSam Bobroff static int cap_ppc_smt_possible;
640f5cb298SDavid Gibson static int cap_spapr_tce;
65d6ee2a7cSAlexey Kardashevskiy static int cap_spapr_tce_64;
66da95324eSAlexey Kardashevskiy static int cap_spapr_multitce;
679bb62a07SAlexey Kardashevskiy static int cap_spapr_vfio;
68f1af19d7SDavid Gibson static int cap_hior;
69d67d40eaSDavid Gibson static int cap_one_reg;
703b961124SStuart Yoder static int cap_epr;
7131f2cb8fSBharat Bhushan static int cap_ppc_watchdog;
729b00ea49SDavid Gibson static int cap_papr;
73e68cb8b4SAlexey Kardashevskiy static int cap_htab_fd;
7487a91de6SAlexander Graf static int cap_fixup_hcalls;
75bac3bf28SThomas Huth static int cap_htm;             /* Hardware transactional memory support */
76cf1c4cceSSam Bobroff static int cap_mmu_radix;
77cf1c4cceSSam Bobroff static int cap_mmu_hash_v3;
7838afd772SCédric Le Goater static int cap_xive;
79b55d295eSDavid Gibson static int cap_resize_hpt;
80c363a37aSDaniel Henrique Barboza static int cap_ppc_pvr_compat;
818acc2ae5SSuraj Jitindar Singh static int cap_ppc_safe_cache;
828acc2ae5SSuraj Jitindar Singh static int cap_ppc_safe_bounds_check;
838acc2ae5SSuraj Jitindar Singh static int cap_ppc_safe_indirect_branch;
848ff43ee4SSuraj Jitindar Singh static int cap_ppc_count_cache_flush_assist;
85b9a477b7SSuraj Jitindar Singh static int cap_ppc_nested_kvm_hv;
867d050527SSuraj Jitindar Singh static int cap_large_decr;
87fc87e185SAlexander Graf 
883c902d44SBharat Bhushan static uint32_t debug_inst_opcode;
893c902d44SBharat Bhushan 
90c995e942SDavid Gibson /*
91c995e942SDavid Gibson  * XXX We have a race condition where we actually have a level triggered
92c821c2bdSAlexander Graf  *     interrupt, but the infrastructure can't expose that yet, so the guest
93c821c2bdSAlexander Graf  *     takes but ignores it, goes to sleep and never gets notified that there's
94c821c2bdSAlexander Graf  *     still an interrupt pending.
95c6a94ba5SAlexander Graf  *
96c821c2bdSAlexander Graf  *     As a quick workaround, let's just wake up again 20 ms after we injected
97c821c2bdSAlexander Graf  *     an interrupt. That way we can assure that we're always reinjecting
98c821c2bdSAlexander Graf  *     interrupts in case the guest swallowed them.
99c6a94ba5SAlexander Graf  */
100c6a94ba5SAlexander Graf static QEMUTimer *idle_timer;
101c6a94ba5SAlexander Graf 
102d5a68146SAndreas Färber static void kvm_kick_cpu(void *opaque)
103c6a94ba5SAlexander Graf {
104d5a68146SAndreas Färber     PowerPCCPU *cpu = opaque;
105d5a68146SAndreas Färber 
106c08d7424SAndreas Färber     qemu_cpu_kick(CPU(cpu));
107c6a94ba5SAlexander Graf }
108c6a94ba5SAlexander Graf 
109c995e942SDavid Gibson /*
110c995e942SDavid Gibson  * Check whether we are running with KVM-PR (instead of KVM-HV).  This
11196c9cff0SThomas Huth  * should only be used for fallback tests - generally we should use
11296c9cff0SThomas Huth  * explicit capabilities for the features we want, rather than
113c995e942SDavid Gibson  * assuming what is/isn't available depending on the KVM variant.
114c995e942SDavid Gibson  */
11596c9cff0SThomas Huth static bool kvmppc_is_pr(KVMState *ks)
11696c9cff0SThomas Huth {
11796c9cff0SThomas Huth     /* Assume KVM-PR if the GET_PVINFO capability is available */
11870a0c19eSGreg Kurz     return kvm_vm_check_extension(ks, KVM_CAP_PPC_GET_PVINFO) != 0;
11996c9cff0SThomas Huth }
12096c9cff0SThomas Huth 
1212e9c10ebSIgor Mammedov static int kvm_ppc_register_host_cpu_type(MachineState *ms);
1228acc2ae5SSuraj Jitindar Singh static void kvmppc_get_cpu_characteristics(KVMState *s);
1237d050527SSuraj Jitindar Singh static int kvmppc_get_dec_bits(void);
1245ba4576bSAndreas Färber 
125b16565b3SMarcel Apfelbaum int kvm_arch_init(MachineState *ms, KVMState *s)
126d76d1650Saurel32 {
127fc87e185SAlexander Graf     cap_interrupt_unset = kvm_check_extension(s, KVM_CAP_PPC_UNSET_IRQ);
128fc87e185SAlexander Graf     cap_interrupt_level = kvm_check_extension(s, KVM_CAP_PPC_IRQ_LEVEL);
12990dc8812SScott Wood     cap_segstate = kvm_check_extension(s, KVM_CAP_PPC_SEGSTATE);
13090dc8812SScott Wood     cap_booke_sregs = kvm_check_extension(s, KVM_CAP_PPC_BOOKE_SREGS);
1316977afdaSGreg Kurz     cap_ppc_smt_possible = kvm_vm_check_extension(s, KVM_CAP_PPC_SMT_POSSIBLE);
1320f5cb298SDavid Gibson     cap_spapr_tce = kvm_check_extension(s, KVM_CAP_SPAPR_TCE);
133d6ee2a7cSAlexey Kardashevskiy     cap_spapr_tce_64 = kvm_check_extension(s, KVM_CAP_SPAPR_TCE_64);
134da95324eSAlexey Kardashevskiy     cap_spapr_multitce = kvm_check_extension(s, KVM_CAP_SPAPR_MULTITCE);
1359ded780cSAlexey Kardashevskiy     cap_spapr_vfio = kvm_vm_check_extension(s, KVM_CAP_SPAPR_TCE_VFIO);
136d67d40eaSDavid Gibson     cap_one_reg = kvm_check_extension(s, KVM_CAP_ONE_REG);
137f1af19d7SDavid Gibson     cap_hior = kvm_check_extension(s, KVM_CAP_PPC_HIOR);
1383b961124SStuart Yoder     cap_epr = kvm_check_extension(s, KVM_CAP_PPC_EPR);
13931f2cb8fSBharat Bhushan     cap_ppc_watchdog = kvm_check_extension(s, KVM_CAP_PPC_BOOKE_WATCHDOG);
140c995e942SDavid Gibson     /*
141c995e942SDavid Gibson      * Note: we don't set cap_papr here, because this capability is
142c995e942SDavid Gibson      * only activated after this by kvmppc_set_papr()
143c995e942SDavid Gibson      */
1446977afdaSGreg Kurz     cap_htab_fd = kvm_vm_check_extension(s, KVM_CAP_PPC_HTAB_FD);
14587a91de6SAlexander Graf     cap_fixup_hcalls = kvm_check_extension(s, KVM_CAP_PPC_FIXUP_HCALL);
146fa98fbfcSSam Bobroff     cap_ppc_smt = kvm_vm_check_extension(s, KVM_CAP_PPC_SMT);
147bac3bf28SThomas Huth     cap_htm = kvm_vm_check_extension(s, KVM_CAP_PPC_HTM);
148cf1c4cceSSam Bobroff     cap_mmu_radix = kvm_vm_check_extension(s, KVM_CAP_PPC_MMU_RADIX);
149cf1c4cceSSam Bobroff     cap_mmu_hash_v3 = kvm_vm_check_extension(s, KVM_CAP_PPC_MMU_HASH_V3);
15038afd772SCédric Le Goater     cap_xive = kvm_vm_check_extension(s, KVM_CAP_PPC_IRQ_XIVE);
151b55d295eSDavid Gibson     cap_resize_hpt = kvm_vm_check_extension(s, KVM_CAP_SPAPR_RESIZE_HPT);
1528acc2ae5SSuraj Jitindar Singh     kvmppc_get_cpu_characteristics(s);
153b9a477b7SSuraj Jitindar Singh     cap_ppc_nested_kvm_hv = kvm_vm_check_extension(s, KVM_CAP_PPC_NESTED_HV);
1547d050527SSuraj Jitindar Singh     cap_large_decr = kvmppc_get_dec_bits();
155c363a37aSDaniel Henrique Barboza     /*
156c363a37aSDaniel Henrique Barboza      * Note: setting it to false because there is not such capability
157c363a37aSDaniel Henrique Barboza      * in KVM at this moment.
158c363a37aSDaniel Henrique Barboza      *
159c363a37aSDaniel Henrique Barboza      * TODO: call kvm_vm_check_extension() with the right capability
160c995e942SDavid Gibson      * after the kernel starts implementing it.
161c995e942SDavid Gibson      */
162c363a37aSDaniel Henrique Barboza     cap_ppc_pvr_compat = false;
163fc87e185SAlexander Graf 
164fc87e185SAlexander Graf     if (!cap_interrupt_level) {
165fc87e185SAlexander Graf         fprintf(stderr, "KVM: Couldn't find level irq capability. Expect the "
166fc87e185SAlexander Graf                         "VM to stall at times!\n");
167fc87e185SAlexander Graf     }
168fc87e185SAlexander Graf 
1692e9c10ebSIgor Mammedov     kvm_ppc_register_host_cpu_type(ms);
1705ba4576bSAndreas Färber 
171d76d1650Saurel32     return 0;
172d76d1650Saurel32 }
173d76d1650Saurel32 
174d525ffabSPaolo Bonzini int kvm_arch_irqchip_create(MachineState *ms, KVMState *s)
175d525ffabSPaolo Bonzini {
176d525ffabSPaolo Bonzini     return 0;
177d525ffabSPaolo Bonzini }
178d525ffabSPaolo Bonzini 
1791bc22652SAndreas Färber static int kvm_arch_sync_sregs(PowerPCCPU *cpu)
180d76d1650Saurel32 {
1811bc22652SAndreas Färber     CPUPPCState *cenv = &cpu->env;
1821bc22652SAndreas Färber     CPUState *cs = CPU(cpu);
183861bbc80SAlexander Graf     struct kvm_sregs sregs;
1845666ca4aSScott Wood     int ret;
1855666ca4aSScott Wood 
1865666ca4aSScott Wood     if (cenv->excp_model == POWERPC_EXCP_BOOKE) {
187c995e942SDavid Gibson         /*
188c995e942SDavid Gibson          * What we're really trying to say is "if we're on BookE, we
189c995e942SDavid Gibson          * use the native PVR for now". This is the only sane way to
190c995e942SDavid Gibson          * check it though, so we potentially confuse users that they
191c995e942SDavid Gibson          * can run BookE guests on BookS. Let's hope nobody dares
192c995e942SDavid Gibson          * enough :)
193c995e942SDavid Gibson          */
1945666ca4aSScott Wood         return 0;
1955666ca4aSScott Wood     } else {
19690dc8812SScott Wood         if (!cap_segstate) {
19764e07be5SAlexander Graf             fprintf(stderr, "kvm error: missing PVR setting capability\n");
19864e07be5SAlexander Graf             return -ENOSYS;
1995666ca4aSScott Wood         }
2005666ca4aSScott Wood     }
2015666ca4aSScott Wood 
2021bc22652SAndreas Färber     ret = kvm_vcpu_ioctl(cs, KVM_GET_SREGS, &sregs);
2035666ca4aSScott Wood     if (ret) {
2045666ca4aSScott Wood         return ret;
2055666ca4aSScott Wood     }
206861bbc80SAlexander Graf 
207861bbc80SAlexander Graf     sregs.pvr = cenv->spr[SPR_PVR];
2081bc22652SAndreas Färber     return kvm_vcpu_ioctl(cs, KVM_SET_SREGS, &sregs);
2095666ca4aSScott Wood }
2105666ca4aSScott Wood 
21193dd5e85SScott Wood /* Set up a shared TLB array with KVM */
2121bc22652SAndreas Färber static int kvm_booke206_tlb_init(PowerPCCPU *cpu)
21393dd5e85SScott Wood {
2141bc22652SAndreas Färber     CPUPPCState *env = &cpu->env;
2151bc22652SAndreas Färber     CPUState *cs = CPU(cpu);
21693dd5e85SScott Wood     struct kvm_book3e_206_tlb_params params = {};
21793dd5e85SScott Wood     struct kvm_config_tlb cfg = {};
21893dd5e85SScott Wood     unsigned int entries = 0;
21993dd5e85SScott Wood     int ret, i;
22093dd5e85SScott Wood 
22193dd5e85SScott Wood     if (!kvm_enabled() ||
222a60f24b5SAndreas Färber         !kvm_check_extension(cs->kvm_state, KVM_CAP_SW_TLB)) {
22393dd5e85SScott Wood         return 0;
22493dd5e85SScott Wood     }
22593dd5e85SScott Wood 
22693dd5e85SScott Wood     assert(ARRAY_SIZE(params.tlb_sizes) == BOOKE206_MAX_TLBN);
22793dd5e85SScott Wood 
22893dd5e85SScott Wood     for (i = 0; i < BOOKE206_MAX_TLBN; i++) {
22993dd5e85SScott Wood         params.tlb_sizes[i] = booke206_tlb_size(env, i);
23093dd5e85SScott Wood         params.tlb_ways[i] = booke206_tlb_ways(env, i);
23193dd5e85SScott Wood         entries += params.tlb_sizes[i];
23293dd5e85SScott Wood     }
23393dd5e85SScott Wood 
23493dd5e85SScott Wood     assert(entries == env->nb_tlb);
23593dd5e85SScott Wood     assert(sizeof(struct kvm_book3e_206_tlb_entry) == sizeof(ppcmas_tlb_t));
23693dd5e85SScott Wood 
23793dd5e85SScott Wood     env->tlb_dirty = true;
23893dd5e85SScott Wood 
23993dd5e85SScott Wood     cfg.array = (uintptr_t)env->tlb.tlbm;
24093dd5e85SScott Wood     cfg.array_len = sizeof(ppcmas_tlb_t) * entries;
24193dd5e85SScott Wood     cfg.params = (uintptr_t)&params;
24293dd5e85SScott Wood     cfg.mmu_type = KVM_MMU_FSL_BOOKE_NOHV;
24393dd5e85SScott Wood 
24448add816SCornelia Huck     ret = kvm_vcpu_enable_cap(cs, KVM_CAP_SW_TLB, 0, (uintptr_t)&cfg);
24593dd5e85SScott Wood     if (ret < 0) {
24693dd5e85SScott Wood         fprintf(stderr, "%s: couldn't enable KVM_CAP_SW_TLB: %s\n",
24793dd5e85SScott Wood                 __func__, strerror(-ret));
24893dd5e85SScott Wood         return ret;
24993dd5e85SScott Wood     }
25093dd5e85SScott Wood 
25193dd5e85SScott Wood     env->kvm_sw_tlb = true;
25293dd5e85SScott Wood     return 0;
25393dd5e85SScott Wood }
25493dd5e85SScott Wood 
2554656e1f0SBenjamin Herrenschmidt 
2564656e1f0SBenjamin Herrenschmidt #if defined(TARGET_PPC64)
257ab256960SGreg Kurz static void kvm_get_smmu_info(struct kvm_ppc_smmu_info *info, Error **errp)
2584656e1f0SBenjamin Herrenschmidt {
2594656e1f0SBenjamin Herrenschmidt     int ret;
2604656e1f0SBenjamin Herrenschmidt 
261ab256960SGreg Kurz     assert(kvm_state != NULL);
262ab256960SGreg Kurz 
263ab256960SGreg Kurz     if (!kvm_check_extension(kvm_state, KVM_CAP_PPC_GET_SMMU_INFO)) {
26471d0f1eaSGreg Kurz         error_setg(errp, "KVM doesn't expose the MMU features it supports");
26571d0f1eaSGreg Kurz         error_append_hint(errp, "Consider switching to a newer KVM\n");
26671d0f1eaSGreg Kurz         return;
26771d0f1eaSGreg Kurz     }
26871d0f1eaSGreg Kurz 
269ab256960SGreg Kurz     ret = kvm_vm_ioctl(kvm_state, KVM_PPC_GET_SMMU_INFO, info);
2704656e1f0SBenjamin Herrenschmidt     if (ret == 0) {
2714656e1f0SBenjamin Herrenschmidt         return;
2724656e1f0SBenjamin Herrenschmidt     }
2734656e1f0SBenjamin Herrenschmidt 
27471d0f1eaSGreg Kurz     error_setg_errno(errp, -ret,
27571d0f1eaSGreg Kurz                      "KVM failed to provide the MMU features it supports");
2764656e1f0SBenjamin Herrenschmidt }
2774656e1f0SBenjamin Herrenschmidt 
278c64abd1fSSam Bobroff struct ppc_radix_page_info *kvm_get_radix_page_info(void)
279c64abd1fSSam Bobroff {
280c64abd1fSSam Bobroff     KVMState *s = KVM_STATE(current_machine->accelerator);
281c64abd1fSSam Bobroff     struct ppc_radix_page_info *radix_page_info;
282c64abd1fSSam Bobroff     struct kvm_ppc_rmmu_info rmmu_info;
283c64abd1fSSam Bobroff     int i;
284c64abd1fSSam Bobroff 
285c64abd1fSSam Bobroff     if (!kvm_check_extension(s, KVM_CAP_PPC_MMU_RADIX)) {
286c64abd1fSSam Bobroff         return NULL;
287c64abd1fSSam Bobroff     }
288c64abd1fSSam Bobroff     if (kvm_vm_ioctl(s, KVM_PPC_GET_RMMU_INFO, &rmmu_info)) {
289c64abd1fSSam Bobroff         return NULL;
290c64abd1fSSam Bobroff     }
291c64abd1fSSam Bobroff     radix_page_info = g_malloc0(sizeof(*radix_page_info));
292c64abd1fSSam Bobroff     radix_page_info->count = 0;
293c64abd1fSSam Bobroff     for (i = 0; i < PPC_PAGE_SIZES_MAX_SZ; i++) {
294c64abd1fSSam Bobroff         if (rmmu_info.ap_encodings[i]) {
295c64abd1fSSam Bobroff             radix_page_info->entries[i] = rmmu_info.ap_encodings[i];
296c64abd1fSSam Bobroff             radix_page_info->count++;
297c64abd1fSSam Bobroff         }
298c64abd1fSSam Bobroff     }
299c64abd1fSSam Bobroff     return radix_page_info;
300c64abd1fSSam Bobroff }
301c64abd1fSSam Bobroff 
302b4db5413SSuraj Jitindar Singh target_ulong kvmppc_configure_v3_mmu(PowerPCCPU *cpu,
303b4db5413SSuraj Jitindar Singh                                      bool radix, bool gtse,
304b4db5413SSuraj Jitindar Singh                                      uint64_t proc_tbl)
305b4db5413SSuraj Jitindar Singh {
306b4db5413SSuraj Jitindar Singh     CPUState *cs = CPU(cpu);
307b4db5413SSuraj Jitindar Singh     int ret;
308b4db5413SSuraj Jitindar Singh     uint64_t flags = 0;
309b4db5413SSuraj Jitindar Singh     struct kvm_ppc_mmuv3_cfg cfg = {
310b4db5413SSuraj Jitindar Singh         .process_table = proc_tbl,
311b4db5413SSuraj Jitindar Singh     };
312b4db5413SSuraj Jitindar Singh 
313b4db5413SSuraj Jitindar Singh     if (radix) {
314b4db5413SSuraj Jitindar Singh         flags |= KVM_PPC_MMUV3_RADIX;
315b4db5413SSuraj Jitindar Singh     }
316b4db5413SSuraj Jitindar Singh     if (gtse) {
317b4db5413SSuraj Jitindar Singh         flags |= KVM_PPC_MMUV3_GTSE;
318b4db5413SSuraj Jitindar Singh     }
319b4db5413SSuraj Jitindar Singh     cfg.flags = flags;
320b4db5413SSuraj Jitindar Singh     ret = kvm_vm_ioctl(cs->kvm_state, KVM_PPC_CONFIGURE_V3_MMU, &cfg);
321b4db5413SSuraj Jitindar Singh     switch (ret) {
322b4db5413SSuraj Jitindar Singh     case 0:
323b4db5413SSuraj Jitindar Singh         return H_SUCCESS;
324b4db5413SSuraj Jitindar Singh     case -EINVAL:
325b4db5413SSuraj Jitindar Singh         return H_PARAMETER;
326b4db5413SSuraj Jitindar Singh     case -ENODEV:
327b4db5413SSuraj Jitindar Singh         return H_NOT_AVAILABLE;
328b4db5413SSuraj Jitindar Singh     default:
329b4db5413SSuraj Jitindar Singh         return H_HARDWARE;
330b4db5413SSuraj Jitindar Singh     }
331b4db5413SSuraj Jitindar Singh }
332b4db5413SSuraj Jitindar Singh 
33324c6863cSDavid Gibson bool kvmppc_hpt_needs_host_contiguous_pages(void)
33424c6863cSDavid Gibson {
33524c6863cSDavid Gibson     static struct kvm_ppc_smmu_info smmu_info;
33624c6863cSDavid Gibson 
33724c6863cSDavid Gibson     if (!kvm_enabled()) {
33824c6863cSDavid Gibson         return false;
33924c6863cSDavid Gibson     }
34024c6863cSDavid Gibson 
341ab256960SGreg Kurz     kvm_get_smmu_info(&smmu_info, &error_fatal);
34224c6863cSDavid Gibson     return !!(smmu_info.flags & KVM_PPC_PAGE_SIZES_REAL);
34324c6863cSDavid Gibson }
34424c6863cSDavid Gibson 
345e5ca28ecSDavid Gibson void kvm_check_mmu(PowerPCCPU *cpu, Error **errp)
3464656e1f0SBenjamin Herrenschmidt {
347e5ca28ecSDavid Gibson     struct kvm_ppc_smmu_info smmu_info;
3484656e1f0SBenjamin Herrenschmidt     int iq, ik, jq, jk;
34971d0f1eaSGreg Kurz     Error *local_err = NULL;
3504656e1f0SBenjamin Herrenschmidt 
351e5ca28ecSDavid Gibson     /* For now, we only have anything to check on hash64 MMUs */
352e5ca28ecSDavid Gibson     if (!cpu->hash64_opts || !kvm_enabled()) {
3534656e1f0SBenjamin Herrenschmidt         return;
3544656e1f0SBenjamin Herrenschmidt     }
3554656e1f0SBenjamin Herrenschmidt 
356ab256960SGreg Kurz     kvm_get_smmu_info(&smmu_info, &local_err);
35771d0f1eaSGreg Kurz     if (local_err) {
35871d0f1eaSGreg Kurz         error_propagate(errp, local_err);
35971d0f1eaSGreg Kurz         return;
36071d0f1eaSGreg Kurz     }
361e5ca28ecSDavid Gibson 
362e5ca28ecSDavid Gibson     if (ppc_hash64_has(cpu, PPC_HASH64_1TSEG)
363e5ca28ecSDavid Gibson         && !(smmu_info.flags & KVM_PPC_1T_SEGMENTS)) {
364e5ca28ecSDavid Gibson         error_setg(errp,
365e5ca28ecSDavid Gibson                    "KVM does not support 1TiB segments which guest expects");
366e5ca28ecSDavid Gibson         return;
3674656e1f0SBenjamin Herrenschmidt     }
3684656e1f0SBenjamin Herrenschmidt 
369e5ca28ecSDavid Gibson     if (smmu_info.slb_size < cpu->hash64_opts->slb_size) {
370e5ca28ecSDavid Gibson         error_setg(errp, "KVM only supports %u SLB entries, but guest needs %u",
371e5ca28ecSDavid Gibson                    smmu_info.slb_size, cpu->hash64_opts->slb_size);
372e5ca28ecSDavid Gibson         return;
37390da0d5aSBenjamin Herrenschmidt     }
37490da0d5aSBenjamin Herrenschmidt 
37508215d8fSAlexander Graf     /*
376e5ca28ecSDavid Gibson      * Verify that every pagesize supported by the cpu model is
377e5ca28ecSDavid Gibson      * supported by KVM with the same encodings
37808215d8fSAlexander Graf      */
379e5ca28ecSDavid Gibson     for (iq = 0; iq < ARRAY_SIZE(cpu->hash64_opts->sps); iq++) {
380b07c59f7SDavid Gibson         PPCHash64SegmentPageSizes *qsps = &cpu->hash64_opts->sps[iq];
381e5ca28ecSDavid Gibson         struct kvm_ppc_one_seg_page_size *ksps;
3824656e1f0SBenjamin Herrenschmidt 
383e5ca28ecSDavid Gibson         for (ik = 0; ik < ARRAY_SIZE(smmu_info.sps); ik++) {
384e5ca28ecSDavid Gibson             if (qsps->page_shift == smmu_info.sps[ik].page_shift) {
3854656e1f0SBenjamin Herrenschmidt                 break;
3864656e1f0SBenjamin Herrenschmidt             }
3874656e1f0SBenjamin Herrenschmidt         }
388e5ca28ecSDavid Gibson         if (ik >= ARRAY_SIZE(smmu_info.sps)) {
389e5ca28ecSDavid Gibson             error_setg(errp, "KVM doesn't support for base page shift %u",
390e5ca28ecSDavid Gibson                        qsps->page_shift);
391e5ca28ecSDavid Gibson             return;
392e5ca28ecSDavid Gibson         }
393e5ca28ecSDavid Gibson 
394e5ca28ecSDavid Gibson         ksps = &smmu_info.sps[ik];
395e5ca28ecSDavid Gibson         if (ksps->slb_enc != qsps->slb_enc) {
396e5ca28ecSDavid Gibson             error_setg(errp,
397e5ca28ecSDavid Gibson "KVM uses SLB encoding 0x%x for page shift %u, but guest expects 0x%x",
398e5ca28ecSDavid Gibson                        ksps->slb_enc, ksps->page_shift, qsps->slb_enc);
399e5ca28ecSDavid Gibson             return;
400e5ca28ecSDavid Gibson         }
401e5ca28ecSDavid Gibson 
402e5ca28ecSDavid Gibson         for (jq = 0; jq < ARRAY_SIZE(qsps->enc); jq++) {
403e5ca28ecSDavid Gibson             for (jk = 0; jk < ARRAY_SIZE(ksps->enc); jk++) {
404e5ca28ecSDavid Gibson                 if (qsps->enc[jq].page_shift == ksps->enc[jk].page_shift) {
4054656e1f0SBenjamin Herrenschmidt                     break;
4064656e1f0SBenjamin Herrenschmidt                 }
4074656e1f0SBenjamin Herrenschmidt             }
4084656e1f0SBenjamin Herrenschmidt 
409e5ca28ecSDavid Gibson             if (jk >= ARRAY_SIZE(ksps->enc)) {
410e5ca28ecSDavid Gibson                 error_setg(errp, "KVM doesn't support page shift %u/%u",
411e5ca28ecSDavid Gibson                            qsps->enc[jq].page_shift, qsps->page_shift);
412e5ca28ecSDavid Gibson                 return;
413e5ca28ecSDavid Gibson             }
414e5ca28ecSDavid Gibson             if (qsps->enc[jq].pte_enc != ksps->enc[jk].pte_enc) {
415e5ca28ecSDavid Gibson                 error_setg(errp,
416e5ca28ecSDavid Gibson "KVM uses PTE encoding 0x%x for page shift %u/%u, but guest expects 0x%x",
417e5ca28ecSDavid Gibson                            ksps->enc[jk].pte_enc, qsps->enc[jq].page_shift,
418e5ca28ecSDavid Gibson                            qsps->page_shift, qsps->enc[jq].pte_enc);
419e5ca28ecSDavid Gibson                 return;
420e5ca28ecSDavid Gibson             }
421e5ca28ecSDavid Gibson         }
4224656e1f0SBenjamin Herrenschmidt     }
4234656e1f0SBenjamin Herrenschmidt 
424e5ca28ecSDavid Gibson     if (ppc_hash64_has(cpu, PPC_HASH64_CI_LARGEPAGE)) {
425c995e942SDavid Gibson         /*
426c995e942SDavid Gibson          * Mostly what guest pagesizes we can use are related to the
427e5ca28ecSDavid Gibson          * host pages used to map guest RAM, which is handled in the
428e5ca28ecSDavid Gibson          * platform code. Cache-Inhibited largepages (64k) however are
429e5ca28ecSDavid Gibson          * used for I/O, so if they're mapped to the host at all it
430e5ca28ecSDavid Gibson          * will be a normal mapping, not a special hugepage one used
431c995e942SDavid Gibson          * for RAM.
432c995e942SDavid Gibson          */
433e5ca28ecSDavid Gibson         if (getpagesize() < 0x10000) {
434e5ca28ecSDavid Gibson             error_setg(errp,
435e5ca28ecSDavid Gibson                        "KVM can't supply 64kiB CI pages, which guest expects");
436e5ca28ecSDavid Gibson         }
437e5ca28ecSDavid Gibson     }
438e5ca28ecSDavid Gibson }
4394656e1f0SBenjamin Herrenschmidt #endif /* !defined (TARGET_PPC64) */
4404656e1f0SBenjamin Herrenschmidt 
441b164e48eSEduardo Habkost unsigned long kvm_arch_vcpu_id(CPUState *cpu)
442b164e48eSEduardo Habkost {
4432e886fb3SSam Bobroff     return POWERPC_CPU(cpu)->vcpu_id;
444b164e48eSEduardo Habkost }
445b164e48eSEduardo Habkost 
446c995e942SDavid Gibson /*
447c995e942SDavid Gibson  * e500 supports 2 h/w breakpoint and 2 watchpoint.  book3s supports
448c995e942SDavid Gibson  * only 1 watchpoint, so array size of 4 is sufficient for now.
44988365d17SBharat Bhushan  */
45088365d17SBharat Bhushan #define MAX_HW_BKPTS 4
45188365d17SBharat Bhushan 
45288365d17SBharat Bhushan static struct HWBreakpoint {
45388365d17SBharat Bhushan     target_ulong addr;
45488365d17SBharat Bhushan     int type;
45588365d17SBharat Bhushan } hw_debug_points[MAX_HW_BKPTS];
45688365d17SBharat Bhushan 
45788365d17SBharat Bhushan static CPUWatchpoint hw_watchpoint;
45888365d17SBharat Bhushan 
45988365d17SBharat Bhushan /* Default there is no breakpoint and watchpoint supported */
46088365d17SBharat Bhushan static int max_hw_breakpoint;
46188365d17SBharat Bhushan static int max_hw_watchpoint;
46288365d17SBharat Bhushan static int nb_hw_breakpoint;
46388365d17SBharat Bhushan static int nb_hw_watchpoint;
46488365d17SBharat Bhushan 
46588365d17SBharat Bhushan static void kvmppc_hw_debug_points_init(CPUPPCState *cenv)
46688365d17SBharat Bhushan {
46788365d17SBharat Bhushan     if (cenv->excp_model == POWERPC_EXCP_BOOKE) {
46888365d17SBharat Bhushan         max_hw_breakpoint = 2;
46988365d17SBharat Bhushan         max_hw_watchpoint = 2;
47088365d17SBharat Bhushan     }
47188365d17SBharat Bhushan 
47288365d17SBharat Bhushan     if ((max_hw_breakpoint + max_hw_watchpoint) > MAX_HW_BKPTS) {
47388365d17SBharat Bhushan         fprintf(stderr, "Error initializing h/w breakpoints\n");
47488365d17SBharat Bhushan         return;
47588365d17SBharat Bhushan     }
47688365d17SBharat Bhushan }
47788365d17SBharat Bhushan 
47820d695a9SAndreas Färber int kvm_arch_init_vcpu(CPUState *cs)
4795666ca4aSScott Wood {
48020d695a9SAndreas Färber     PowerPCCPU *cpu = POWERPC_CPU(cs);
48120d695a9SAndreas Färber     CPUPPCState *cenv = &cpu->env;
4825666ca4aSScott Wood     int ret;
4835666ca4aSScott Wood 
4844656e1f0SBenjamin Herrenschmidt     /* Synchronize sregs with kvm */
4851bc22652SAndreas Färber     ret = kvm_arch_sync_sregs(cpu);
4865666ca4aSScott Wood     if (ret) {
487388e47c7SThomas Huth         if (ret == -EINVAL) {
488388e47c7SThomas Huth             error_report("Register sync failed... If you're using kvm-hv.ko,"
489388e47c7SThomas Huth                          " only \"-cpu host\" is possible");
490388e47c7SThomas Huth         }
4915666ca4aSScott Wood         return ret;
4925666ca4aSScott Wood     }
493861bbc80SAlexander Graf 
494bc72ad67SAlex Bligh     idle_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, kvm_kick_cpu, cpu);
495c821c2bdSAlexander Graf 
49693dd5e85SScott Wood     switch (cenv->mmu_model) {
49793dd5e85SScott Wood     case POWERPC_MMU_BOOKE206:
4987f516c96SThomas Huth         /* This target supports access to KVM's guest TLB */
4991bc22652SAndreas Färber         ret = kvm_booke206_tlb_init(cpu);
50093dd5e85SScott Wood         break;
5017f516c96SThomas Huth     case POWERPC_MMU_2_07:
5027f516c96SThomas Huth         if (!cap_htm && !kvmppc_is_pr(cs->kvm_state)) {
503c995e942SDavid Gibson             /*
504c995e942SDavid Gibson              * KVM-HV has transactional memory on POWER8 also without
505c995e942SDavid Gibson              * the KVM_CAP_PPC_HTM extension, so enable it here
506c995e942SDavid Gibson              * instead as long as it's availble to userspace on the
507c995e942SDavid Gibson              * host.
508c995e942SDavid Gibson              */
509f3d9f303SSam Bobroff             if (qemu_getauxval(AT_HWCAP2) & PPC_FEATURE2_HAS_HTM) {
5107f516c96SThomas Huth                 cap_htm = true;
5117f516c96SThomas Huth             }
512f3d9f303SSam Bobroff         }
5137f516c96SThomas Huth         break;
51493dd5e85SScott Wood     default:
51593dd5e85SScott Wood         break;
51693dd5e85SScott Wood     }
51793dd5e85SScott Wood 
5183c902d44SBharat Bhushan     kvm_get_one_reg(cs, KVM_REG_PPC_DEBUG_INST, &debug_inst_opcode);
51988365d17SBharat Bhushan     kvmppc_hw_debug_points_init(cenv);
5203c902d44SBharat Bhushan 
521861bbc80SAlexander Graf     return ret;
522d76d1650Saurel32 }
523d76d1650Saurel32 
5241bc22652SAndreas Färber static void kvm_sw_tlb_put(PowerPCCPU *cpu)
52593dd5e85SScott Wood {
5261bc22652SAndreas Färber     CPUPPCState *env = &cpu->env;
5271bc22652SAndreas Färber     CPUState *cs = CPU(cpu);
52893dd5e85SScott Wood     struct kvm_dirty_tlb dirty_tlb;
52993dd5e85SScott Wood     unsigned char *bitmap;
53093dd5e85SScott Wood     int ret;
53193dd5e85SScott Wood 
53293dd5e85SScott Wood     if (!env->kvm_sw_tlb) {
53393dd5e85SScott Wood         return;
53493dd5e85SScott Wood     }
53593dd5e85SScott Wood 
53693dd5e85SScott Wood     bitmap = g_malloc((env->nb_tlb + 7) / 8);
53793dd5e85SScott Wood     memset(bitmap, 0xFF, (env->nb_tlb + 7) / 8);
53893dd5e85SScott Wood 
53993dd5e85SScott Wood     dirty_tlb.bitmap = (uintptr_t)bitmap;
54093dd5e85SScott Wood     dirty_tlb.num_dirty = env->nb_tlb;
54193dd5e85SScott Wood 
5421bc22652SAndreas Färber     ret = kvm_vcpu_ioctl(cs, KVM_DIRTY_TLB, &dirty_tlb);
54393dd5e85SScott Wood     if (ret) {
54493dd5e85SScott Wood         fprintf(stderr, "%s: KVM_DIRTY_TLB: %s\n",
54593dd5e85SScott Wood                 __func__, strerror(-ret));
54693dd5e85SScott Wood     }
54793dd5e85SScott Wood 
54893dd5e85SScott Wood     g_free(bitmap);
54993dd5e85SScott Wood }
55093dd5e85SScott Wood 
551d67d40eaSDavid Gibson static void kvm_get_one_spr(CPUState *cs, uint64_t id, int spr)
552d67d40eaSDavid Gibson {
553d67d40eaSDavid Gibson     PowerPCCPU *cpu = POWERPC_CPU(cs);
554d67d40eaSDavid Gibson     CPUPPCState *env = &cpu->env;
555d67d40eaSDavid Gibson     union {
556d67d40eaSDavid Gibson         uint32_t u32;
557d67d40eaSDavid Gibson         uint64_t u64;
558d67d40eaSDavid Gibson     } val;
559d67d40eaSDavid Gibson     struct kvm_one_reg reg = {
560d67d40eaSDavid Gibson         .id = id,
561d67d40eaSDavid Gibson         .addr = (uintptr_t) &val,
562d67d40eaSDavid Gibson     };
563d67d40eaSDavid Gibson     int ret;
564d67d40eaSDavid Gibson 
565d67d40eaSDavid Gibson     ret = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, &reg);
566d67d40eaSDavid Gibson     if (ret != 0) {
567b36f100eSAlexey Kardashevskiy         trace_kvm_failed_spr_get(spr, strerror(errno));
568d67d40eaSDavid Gibson     } else {
569d67d40eaSDavid Gibson         switch (id & KVM_REG_SIZE_MASK) {
570d67d40eaSDavid Gibson         case KVM_REG_SIZE_U32:
571d67d40eaSDavid Gibson             env->spr[spr] = val.u32;
572d67d40eaSDavid Gibson             break;
573d67d40eaSDavid Gibson 
574d67d40eaSDavid Gibson         case KVM_REG_SIZE_U64:
575d67d40eaSDavid Gibson             env->spr[spr] = val.u64;
576d67d40eaSDavid Gibson             break;
577d67d40eaSDavid Gibson 
578d67d40eaSDavid Gibson         default:
579d67d40eaSDavid Gibson             /* Don't handle this size yet */
580d67d40eaSDavid Gibson             abort();
581d67d40eaSDavid Gibson         }
582d67d40eaSDavid Gibson     }
583d67d40eaSDavid Gibson }
584d67d40eaSDavid Gibson 
585d67d40eaSDavid Gibson static void kvm_put_one_spr(CPUState *cs, uint64_t id, int spr)
586d67d40eaSDavid Gibson {
587d67d40eaSDavid Gibson     PowerPCCPU *cpu = POWERPC_CPU(cs);
588d67d40eaSDavid Gibson     CPUPPCState *env = &cpu->env;
589d67d40eaSDavid Gibson     union {
590d67d40eaSDavid Gibson         uint32_t u32;
591d67d40eaSDavid Gibson         uint64_t u64;
592d67d40eaSDavid Gibson     } val;
593d67d40eaSDavid Gibson     struct kvm_one_reg reg = {
594d67d40eaSDavid Gibson         .id = id,
595d67d40eaSDavid Gibson         .addr = (uintptr_t) &val,
596d67d40eaSDavid Gibson     };
597d67d40eaSDavid Gibson     int ret;
598d67d40eaSDavid Gibson 
599d67d40eaSDavid Gibson     switch (id & KVM_REG_SIZE_MASK) {
600d67d40eaSDavid Gibson     case KVM_REG_SIZE_U32:
601d67d40eaSDavid Gibson         val.u32 = env->spr[spr];
602d67d40eaSDavid Gibson         break;
603d67d40eaSDavid Gibson 
604d67d40eaSDavid Gibson     case KVM_REG_SIZE_U64:
605d67d40eaSDavid Gibson         val.u64 = env->spr[spr];
606d67d40eaSDavid Gibson         break;
607d67d40eaSDavid Gibson 
608d67d40eaSDavid Gibson     default:
609d67d40eaSDavid Gibson         /* Don't handle this size yet */
610d67d40eaSDavid Gibson         abort();
611d67d40eaSDavid Gibson     }
612d67d40eaSDavid Gibson 
613d67d40eaSDavid Gibson     ret = kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, &reg);
614d67d40eaSDavid Gibson     if (ret != 0) {
615b36f100eSAlexey Kardashevskiy         trace_kvm_failed_spr_set(spr, strerror(errno));
616d67d40eaSDavid Gibson     }
617d67d40eaSDavid Gibson }
618d67d40eaSDavid Gibson 
61970b79849SDavid Gibson static int kvm_put_fp(CPUState *cs)
62070b79849SDavid Gibson {
62170b79849SDavid Gibson     PowerPCCPU *cpu = POWERPC_CPU(cs);
62270b79849SDavid Gibson     CPUPPCState *env = &cpu->env;
62370b79849SDavid Gibson     struct kvm_one_reg reg;
62470b79849SDavid Gibson     int i;
62570b79849SDavid Gibson     int ret;
62670b79849SDavid Gibson 
62770b79849SDavid Gibson     if (env->insns_flags & PPC_FLOAT) {
62870b79849SDavid Gibson         uint64_t fpscr = env->fpscr;
62970b79849SDavid Gibson         bool vsx = !!(env->insns_flags2 & PPC2_VSX);
63070b79849SDavid Gibson 
63170b79849SDavid Gibson         reg.id = KVM_REG_PPC_FPSCR;
63270b79849SDavid Gibson         reg.addr = (uintptr_t)&fpscr;
63370b79849SDavid Gibson         ret = kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, &reg);
63470b79849SDavid Gibson         if (ret < 0) {
6358d83cbf1SGreg Kurz             trace_kvm_failed_fpscr_set(strerror(errno));
63670b79849SDavid Gibson             return ret;
63770b79849SDavid Gibson         }
63870b79849SDavid Gibson 
63970b79849SDavid Gibson         for (i = 0; i < 32; i++) {
64070b79849SDavid Gibson             uint64_t vsr[2];
641ef96e3aeSMark Cave-Ayland             uint64_t *fpr = cpu_fpr_ptr(&cpu->env, i);
642ef96e3aeSMark Cave-Ayland             uint64_t *vsrl = cpu_vsrl_ptr(&cpu->env, i);
64370b79849SDavid Gibson 
6443a4b791bSGreg Kurz #ifdef HOST_WORDS_BIGENDIAN
645ef96e3aeSMark Cave-Ayland             vsr[0] = float64_val(*fpr);
646ef96e3aeSMark Cave-Ayland             vsr[1] = *vsrl;
6473a4b791bSGreg Kurz #else
648ef96e3aeSMark Cave-Ayland             vsr[0] = *vsrl;
649ef96e3aeSMark Cave-Ayland             vsr[1] = float64_val(*fpr);
6503a4b791bSGreg Kurz #endif
65170b79849SDavid Gibson             reg.addr = (uintptr_t) &vsr;
65270b79849SDavid Gibson             reg.id = vsx ? KVM_REG_PPC_VSR(i) : KVM_REG_PPC_FPR(i);
65370b79849SDavid Gibson 
65470b79849SDavid Gibson             ret = kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, &reg);
65570b79849SDavid Gibson             if (ret < 0) {
6568d83cbf1SGreg Kurz                 trace_kvm_failed_fp_set(vsx ? "VSR" : "FPR", i,
6578d83cbf1SGreg Kurz                                         strerror(errno));
65870b79849SDavid Gibson                 return ret;
65970b79849SDavid Gibson             }
66070b79849SDavid Gibson         }
66170b79849SDavid Gibson     }
66270b79849SDavid Gibson 
66370b79849SDavid Gibson     if (env->insns_flags & PPC_ALTIVEC) {
66470b79849SDavid Gibson         reg.id = KVM_REG_PPC_VSCR;
66570b79849SDavid Gibson         reg.addr = (uintptr_t)&env->vscr;
66670b79849SDavid Gibson         ret = kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, &reg);
66770b79849SDavid Gibson         if (ret < 0) {
6688d83cbf1SGreg Kurz             trace_kvm_failed_vscr_set(strerror(errno));
66970b79849SDavid Gibson             return ret;
67070b79849SDavid Gibson         }
67170b79849SDavid Gibson 
67270b79849SDavid Gibson         for (i = 0; i < 32; i++) {
67370b79849SDavid Gibson             reg.id = KVM_REG_PPC_VR(i);
674ef96e3aeSMark Cave-Ayland             reg.addr = (uintptr_t)cpu_avr_ptr(env, i);
67570b79849SDavid Gibson             ret = kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, &reg);
67670b79849SDavid Gibson             if (ret < 0) {
6778d83cbf1SGreg Kurz                 trace_kvm_failed_vr_set(i, strerror(errno));
67870b79849SDavid Gibson                 return ret;
67970b79849SDavid Gibson             }
68070b79849SDavid Gibson         }
68170b79849SDavid Gibson     }
68270b79849SDavid Gibson 
68370b79849SDavid Gibson     return 0;
68470b79849SDavid Gibson }
68570b79849SDavid Gibson 
68670b79849SDavid Gibson static int kvm_get_fp(CPUState *cs)
68770b79849SDavid Gibson {
68870b79849SDavid Gibson     PowerPCCPU *cpu = POWERPC_CPU(cs);
68970b79849SDavid Gibson     CPUPPCState *env = &cpu->env;
69070b79849SDavid Gibson     struct kvm_one_reg reg;
69170b79849SDavid Gibson     int i;
69270b79849SDavid Gibson     int ret;
69370b79849SDavid Gibson 
69470b79849SDavid Gibson     if (env->insns_flags & PPC_FLOAT) {
69570b79849SDavid Gibson         uint64_t fpscr;
69670b79849SDavid Gibson         bool vsx = !!(env->insns_flags2 & PPC2_VSX);
69770b79849SDavid Gibson 
69870b79849SDavid Gibson         reg.id = KVM_REG_PPC_FPSCR;
69970b79849SDavid Gibson         reg.addr = (uintptr_t)&fpscr;
70070b79849SDavid Gibson         ret = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, &reg);
70170b79849SDavid Gibson         if (ret < 0) {
7028d83cbf1SGreg Kurz             trace_kvm_failed_fpscr_get(strerror(errno));
70370b79849SDavid Gibson             return ret;
70470b79849SDavid Gibson         } else {
70570b79849SDavid Gibson             env->fpscr = fpscr;
70670b79849SDavid Gibson         }
70770b79849SDavid Gibson 
70870b79849SDavid Gibson         for (i = 0; i < 32; i++) {
70970b79849SDavid Gibson             uint64_t vsr[2];
710ef96e3aeSMark Cave-Ayland             uint64_t *fpr = cpu_fpr_ptr(&cpu->env, i);
711ef96e3aeSMark Cave-Ayland             uint64_t *vsrl = cpu_vsrl_ptr(&cpu->env, i);
71270b79849SDavid Gibson 
71370b79849SDavid Gibson             reg.addr = (uintptr_t) &vsr;
71470b79849SDavid Gibson             reg.id = vsx ? KVM_REG_PPC_VSR(i) : KVM_REG_PPC_FPR(i);
71570b79849SDavid Gibson 
71670b79849SDavid Gibson             ret = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, &reg);
71770b79849SDavid Gibson             if (ret < 0) {
7188d83cbf1SGreg Kurz                 trace_kvm_failed_fp_get(vsx ? "VSR" : "FPR", i,
7198d83cbf1SGreg Kurz                                         strerror(errno));
72070b79849SDavid Gibson                 return ret;
72170b79849SDavid Gibson             } else {
7223a4b791bSGreg Kurz #ifdef HOST_WORDS_BIGENDIAN
723ef96e3aeSMark Cave-Ayland                 *fpr = vsr[0];
72470b79849SDavid Gibson                 if (vsx) {
725ef96e3aeSMark Cave-Ayland                     *vsrl = vsr[1];
72670b79849SDavid Gibson                 }
7273a4b791bSGreg Kurz #else
728ef96e3aeSMark Cave-Ayland                 *fpr = vsr[1];
7293a4b791bSGreg Kurz                 if (vsx) {
730ef96e3aeSMark Cave-Ayland                     *vsrl = vsr[0];
7313a4b791bSGreg Kurz                 }
7323a4b791bSGreg Kurz #endif
73370b79849SDavid Gibson             }
73470b79849SDavid Gibson         }
73570b79849SDavid Gibson     }
73670b79849SDavid Gibson 
73770b79849SDavid Gibson     if (env->insns_flags & PPC_ALTIVEC) {
73870b79849SDavid Gibson         reg.id = KVM_REG_PPC_VSCR;
73970b79849SDavid Gibson         reg.addr = (uintptr_t)&env->vscr;
74070b79849SDavid Gibson         ret = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, &reg);
74170b79849SDavid Gibson         if (ret < 0) {
7428d83cbf1SGreg Kurz             trace_kvm_failed_vscr_get(strerror(errno));
74370b79849SDavid Gibson             return ret;
74470b79849SDavid Gibson         }
74570b79849SDavid Gibson 
74670b79849SDavid Gibson         for (i = 0; i < 32; i++) {
74770b79849SDavid Gibson             reg.id = KVM_REG_PPC_VR(i);
748ef96e3aeSMark Cave-Ayland             reg.addr = (uintptr_t)cpu_avr_ptr(env, i);
74970b79849SDavid Gibson             ret = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, &reg);
75070b79849SDavid Gibson             if (ret < 0) {
7518d83cbf1SGreg Kurz                 trace_kvm_failed_vr_get(i, strerror(errno));
75270b79849SDavid Gibson                 return ret;
75370b79849SDavid Gibson             }
75470b79849SDavid Gibson         }
75570b79849SDavid Gibson     }
75670b79849SDavid Gibson 
75770b79849SDavid Gibson     return 0;
75870b79849SDavid Gibson }
75970b79849SDavid Gibson 
7609b00ea49SDavid Gibson #if defined(TARGET_PPC64)
7619b00ea49SDavid Gibson static int kvm_get_vpa(CPUState *cs)
7629b00ea49SDavid Gibson {
7639b00ea49SDavid Gibson     PowerPCCPU *cpu = POWERPC_CPU(cs);
764ce2918cbSDavid Gibson     SpaprCpuState *spapr_cpu = spapr_cpu_state(cpu);
7659b00ea49SDavid Gibson     struct kvm_one_reg reg;
7669b00ea49SDavid Gibson     int ret;
7679b00ea49SDavid Gibson 
7689b00ea49SDavid Gibson     reg.id = KVM_REG_PPC_VPA_ADDR;
7697388efafSDavid Gibson     reg.addr = (uintptr_t)&spapr_cpu->vpa_addr;
7709b00ea49SDavid Gibson     ret = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, &reg);
7719b00ea49SDavid Gibson     if (ret < 0) {
7728d83cbf1SGreg Kurz         trace_kvm_failed_vpa_addr_get(strerror(errno));
7739b00ea49SDavid Gibson         return ret;
7749b00ea49SDavid Gibson     }
7759b00ea49SDavid Gibson 
7767388efafSDavid Gibson     assert((uintptr_t)&spapr_cpu->slb_shadow_size
7777388efafSDavid Gibson            == ((uintptr_t)&spapr_cpu->slb_shadow_addr + 8));
7789b00ea49SDavid Gibson     reg.id = KVM_REG_PPC_VPA_SLB;
7797388efafSDavid Gibson     reg.addr = (uintptr_t)&spapr_cpu->slb_shadow_addr;
7809b00ea49SDavid Gibson     ret = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, &reg);
7819b00ea49SDavid Gibson     if (ret < 0) {
7828d83cbf1SGreg Kurz         trace_kvm_failed_slb_get(strerror(errno));
7839b00ea49SDavid Gibson         return ret;
7849b00ea49SDavid Gibson     }
7859b00ea49SDavid Gibson 
7867388efafSDavid Gibson     assert((uintptr_t)&spapr_cpu->dtl_size
7877388efafSDavid Gibson            == ((uintptr_t)&spapr_cpu->dtl_addr + 8));
7889b00ea49SDavid Gibson     reg.id = KVM_REG_PPC_VPA_DTL;
7897388efafSDavid Gibson     reg.addr = (uintptr_t)&spapr_cpu->dtl_addr;
7909b00ea49SDavid Gibson     ret = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, &reg);
7919b00ea49SDavid Gibson     if (ret < 0) {
7928d83cbf1SGreg Kurz         trace_kvm_failed_dtl_get(strerror(errno));
7939b00ea49SDavid Gibson         return ret;
7949b00ea49SDavid Gibson     }
7959b00ea49SDavid Gibson 
7969b00ea49SDavid Gibson     return 0;
7979b00ea49SDavid Gibson }
7989b00ea49SDavid Gibson 
7999b00ea49SDavid Gibson static int kvm_put_vpa(CPUState *cs)
8009b00ea49SDavid Gibson {
8019b00ea49SDavid Gibson     PowerPCCPU *cpu = POWERPC_CPU(cs);
802ce2918cbSDavid Gibson     SpaprCpuState *spapr_cpu = spapr_cpu_state(cpu);
8039b00ea49SDavid Gibson     struct kvm_one_reg reg;
8049b00ea49SDavid Gibson     int ret;
8059b00ea49SDavid Gibson 
806c995e942SDavid Gibson     /*
807c995e942SDavid Gibson      * SLB shadow or DTL can't be registered unless a master VPA is
8089b00ea49SDavid Gibson      * registered.  That means when restoring state, if a VPA *is*
8099b00ea49SDavid Gibson      * registered, we need to set that up first.  If not, we need to
810c995e942SDavid Gibson      * deregister the others before deregistering the master VPA
811c995e942SDavid Gibson      */
8127388efafSDavid Gibson     assert(spapr_cpu->vpa_addr
8137388efafSDavid Gibson            || !(spapr_cpu->slb_shadow_addr || spapr_cpu->dtl_addr));
8149b00ea49SDavid Gibson 
8157388efafSDavid Gibson     if (spapr_cpu->vpa_addr) {
8169b00ea49SDavid Gibson         reg.id = KVM_REG_PPC_VPA_ADDR;
8177388efafSDavid Gibson         reg.addr = (uintptr_t)&spapr_cpu->vpa_addr;
8189b00ea49SDavid Gibson         ret = kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, &reg);
8199b00ea49SDavid Gibson         if (ret < 0) {
8208d83cbf1SGreg Kurz             trace_kvm_failed_vpa_addr_set(strerror(errno));
8219b00ea49SDavid Gibson             return ret;
8229b00ea49SDavid Gibson         }
8239b00ea49SDavid Gibson     }
8249b00ea49SDavid Gibson 
8257388efafSDavid Gibson     assert((uintptr_t)&spapr_cpu->slb_shadow_size
8267388efafSDavid Gibson            == ((uintptr_t)&spapr_cpu->slb_shadow_addr + 8));
8279b00ea49SDavid Gibson     reg.id = KVM_REG_PPC_VPA_SLB;
8287388efafSDavid Gibson     reg.addr = (uintptr_t)&spapr_cpu->slb_shadow_addr;
8299b00ea49SDavid Gibson     ret = kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, &reg);
8309b00ea49SDavid Gibson     if (ret < 0) {
8318d83cbf1SGreg Kurz         trace_kvm_failed_slb_set(strerror(errno));
8329b00ea49SDavid Gibson         return ret;
8339b00ea49SDavid Gibson     }
8349b00ea49SDavid Gibson 
8357388efafSDavid Gibson     assert((uintptr_t)&spapr_cpu->dtl_size
8367388efafSDavid Gibson            == ((uintptr_t)&spapr_cpu->dtl_addr + 8));
8379b00ea49SDavid Gibson     reg.id = KVM_REG_PPC_VPA_DTL;
8387388efafSDavid Gibson     reg.addr = (uintptr_t)&spapr_cpu->dtl_addr;
8399b00ea49SDavid Gibson     ret = kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, &reg);
8409b00ea49SDavid Gibson     if (ret < 0) {
8418d83cbf1SGreg Kurz         trace_kvm_failed_dtl_set(strerror(errno));
8429b00ea49SDavid Gibson         return ret;
8439b00ea49SDavid Gibson     }
8449b00ea49SDavid Gibson 
8457388efafSDavid Gibson     if (!spapr_cpu->vpa_addr) {
8469b00ea49SDavid Gibson         reg.id = KVM_REG_PPC_VPA_ADDR;
8477388efafSDavid Gibson         reg.addr = (uintptr_t)&spapr_cpu->vpa_addr;
8489b00ea49SDavid Gibson         ret = kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, &reg);
8499b00ea49SDavid Gibson         if (ret < 0) {
8508d83cbf1SGreg Kurz             trace_kvm_failed_null_vpa_addr_set(strerror(errno));
8519b00ea49SDavid Gibson             return ret;
8529b00ea49SDavid Gibson         }
8539b00ea49SDavid Gibson     }
8549b00ea49SDavid Gibson 
8559b00ea49SDavid Gibson     return 0;
8569b00ea49SDavid Gibson }
8579b00ea49SDavid Gibson #endif /* TARGET_PPC64 */
8589b00ea49SDavid Gibson 
859e5c0d3ceSDavid Gibson int kvmppc_put_books_sregs(PowerPCCPU *cpu)
860a7a00a72SDavid Gibson {
861a7a00a72SDavid Gibson     CPUPPCState *env = &cpu->env;
862a7a00a72SDavid Gibson     struct kvm_sregs sregs;
863a7a00a72SDavid Gibson     int i;
864a7a00a72SDavid Gibson 
865a7a00a72SDavid Gibson     sregs.pvr = env->spr[SPR_PVR];
866a7a00a72SDavid Gibson 
8671ec26c75SGreg Kurz     if (cpu->vhyp) {
8681ec26c75SGreg Kurz         PPCVirtualHypervisorClass *vhc =
8691ec26c75SGreg Kurz             PPC_VIRTUAL_HYPERVISOR_GET_CLASS(cpu->vhyp);
8701ec26c75SGreg Kurz         sregs.u.s.sdr1 = vhc->encode_hpt_for_kvm_pr(cpu->vhyp);
8711ec26c75SGreg Kurz     } else {
872a7a00a72SDavid Gibson         sregs.u.s.sdr1 = env->spr[SPR_SDR1];
8731ec26c75SGreg Kurz     }
874a7a00a72SDavid Gibson 
875a7a00a72SDavid Gibson     /* Sync SLB */
876a7a00a72SDavid Gibson #ifdef TARGET_PPC64
877a7a00a72SDavid Gibson     for (i = 0; i < ARRAY_SIZE(env->slb); i++) {
878a7a00a72SDavid Gibson         sregs.u.s.ppc64.slb[i].slbe = env->slb[i].esid;
879a7a00a72SDavid Gibson         if (env->slb[i].esid & SLB_ESID_V) {
880a7a00a72SDavid Gibson             sregs.u.s.ppc64.slb[i].slbe |= i;
881a7a00a72SDavid Gibson         }
882a7a00a72SDavid Gibson         sregs.u.s.ppc64.slb[i].slbv = env->slb[i].vsid;
883a7a00a72SDavid Gibson     }
884a7a00a72SDavid Gibson #endif
885a7a00a72SDavid Gibson 
886a7a00a72SDavid Gibson     /* Sync SRs */
887a7a00a72SDavid Gibson     for (i = 0; i < 16; i++) {
888a7a00a72SDavid Gibson         sregs.u.s.ppc32.sr[i] = env->sr[i];
889a7a00a72SDavid Gibson     }
890a7a00a72SDavid Gibson 
891a7a00a72SDavid Gibson     /* Sync BATs */
892a7a00a72SDavid Gibson     for (i = 0; i < 8; i++) {
893a7a00a72SDavid Gibson         /* Beware. We have to swap upper and lower bits here */
894a7a00a72SDavid Gibson         sregs.u.s.ppc32.dbat[i] = ((uint64_t)env->DBAT[0][i] << 32)
895a7a00a72SDavid Gibson             | env->DBAT[1][i];
896a7a00a72SDavid Gibson         sregs.u.s.ppc32.ibat[i] = ((uint64_t)env->IBAT[0][i] << 32)
897a7a00a72SDavid Gibson             | env->IBAT[1][i];
898a7a00a72SDavid Gibson     }
899a7a00a72SDavid Gibson 
900a7a00a72SDavid Gibson     return kvm_vcpu_ioctl(CPU(cpu), KVM_SET_SREGS, &sregs);
901a7a00a72SDavid Gibson }
902a7a00a72SDavid Gibson 
90320d695a9SAndreas Färber int kvm_arch_put_registers(CPUState *cs, int level)
904d76d1650Saurel32 {
90520d695a9SAndreas Färber     PowerPCCPU *cpu = POWERPC_CPU(cs);
90620d695a9SAndreas Färber     CPUPPCState *env = &cpu->env;
907d76d1650Saurel32     struct kvm_regs regs;
908d76d1650Saurel32     int ret;
909d76d1650Saurel32     int i;
910d76d1650Saurel32 
9111bc22652SAndreas Färber     ret = kvm_vcpu_ioctl(cs, KVM_GET_REGS, &regs);
9121bc22652SAndreas Färber     if (ret < 0) {
913d76d1650Saurel32         return ret;
9141bc22652SAndreas Färber     }
915d76d1650Saurel32 
916d76d1650Saurel32     regs.ctr = env->ctr;
917d76d1650Saurel32     regs.lr  = env->lr;
918da91a00fSRichard Henderson     regs.xer = cpu_read_xer(env);
919d76d1650Saurel32     regs.msr = env->msr;
920d76d1650Saurel32     regs.pc = env->nip;
921d76d1650Saurel32 
922d76d1650Saurel32     regs.srr0 = env->spr[SPR_SRR0];
923d76d1650Saurel32     regs.srr1 = env->spr[SPR_SRR1];
924d76d1650Saurel32 
925d76d1650Saurel32     regs.sprg0 = env->spr[SPR_SPRG0];
926d76d1650Saurel32     regs.sprg1 = env->spr[SPR_SPRG1];
927d76d1650Saurel32     regs.sprg2 = env->spr[SPR_SPRG2];
928d76d1650Saurel32     regs.sprg3 = env->spr[SPR_SPRG3];
929d76d1650Saurel32     regs.sprg4 = env->spr[SPR_SPRG4];
930d76d1650Saurel32     regs.sprg5 = env->spr[SPR_SPRG5];
931d76d1650Saurel32     regs.sprg6 = env->spr[SPR_SPRG6];
932d76d1650Saurel32     regs.sprg7 = env->spr[SPR_SPRG7];
933d76d1650Saurel32 
93490dc8812SScott Wood     regs.pid = env->spr[SPR_BOOKE_PID];
93590dc8812SScott Wood 
936c995e942SDavid Gibson     for (i = 0; i < 32; i++) {
937d76d1650Saurel32         regs.gpr[i] = env->gpr[i];
938c995e942SDavid Gibson     }
939d76d1650Saurel32 
9404bddaf55SAlexey Kardashevskiy     regs.cr = 0;
9414bddaf55SAlexey Kardashevskiy     for (i = 0; i < 8; i++) {
9424bddaf55SAlexey Kardashevskiy         regs.cr |= (env->crf[i] & 15) << (4 * (7 - i));
9434bddaf55SAlexey Kardashevskiy     }
9444bddaf55SAlexey Kardashevskiy 
9451bc22652SAndreas Färber     ret = kvm_vcpu_ioctl(cs, KVM_SET_REGS, &regs);
946c995e942SDavid Gibson     if (ret < 0) {
947d76d1650Saurel32         return ret;
948c995e942SDavid Gibson     }
949d76d1650Saurel32 
95070b79849SDavid Gibson     kvm_put_fp(cs);
95170b79849SDavid Gibson 
95293dd5e85SScott Wood     if (env->tlb_dirty) {
9531bc22652SAndreas Färber         kvm_sw_tlb_put(cpu);
95493dd5e85SScott Wood         env->tlb_dirty = false;
95593dd5e85SScott Wood     }
95693dd5e85SScott Wood 
957f1af19d7SDavid Gibson     if (cap_segstate && (level >= KVM_PUT_RESET_STATE)) {
958a7a00a72SDavid Gibson         ret = kvmppc_put_books_sregs(cpu);
959a7a00a72SDavid Gibson         if (ret < 0) {
960f1af19d7SDavid Gibson             return ret;
961f1af19d7SDavid Gibson         }
962f1af19d7SDavid Gibson     }
963f1af19d7SDavid Gibson 
964f1af19d7SDavid Gibson     if (cap_hior && (level >= KVM_PUT_RESET_STATE)) {
965d67d40eaSDavid Gibson         kvm_put_one_spr(cs, KVM_REG_PPC_HIOR, SPR_HIOR);
966d67d40eaSDavid Gibson     }
967f1af19d7SDavid Gibson 
968d67d40eaSDavid Gibson     if (cap_one_reg) {
969d67d40eaSDavid Gibson         int i;
970d67d40eaSDavid Gibson 
971c995e942SDavid Gibson         /*
972c995e942SDavid Gibson          * We deliberately ignore errors here, for kernels which have
973d67d40eaSDavid Gibson          * the ONE_REG calls, but don't support the specific
974d67d40eaSDavid Gibson          * registers, there's a reasonable chance things will still
975c995e942SDavid Gibson          * work, at least until we try to migrate.
976c995e942SDavid Gibson          */
977d67d40eaSDavid Gibson         for (i = 0; i < 1024; i++) {
978d67d40eaSDavid Gibson             uint64_t id = env->spr_cb[i].one_reg_id;
979d67d40eaSDavid Gibson 
980d67d40eaSDavid Gibson             if (id != 0) {
981d67d40eaSDavid Gibson                 kvm_put_one_spr(cs, id, i);
982d67d40eaSDavid Gibson             }
983f1af19d7SDavid Gibson         }
9849b00ea49SDavid Gibson 
9859b00ea49SDavid Gibson #ifdef TARGET_PPC64
98680b3f79bSAlexey Kardashevskiy         if (msr_ts) {
98780b3f79bSAlexey Kardashevskiy             for (i = 0; i < ARRAY_SIZE(env->tm_gpr); i++) {
98880b3f79bSAlexey Kardashevskiy                 kvm_set_one_reg(cs, KVM_REG_PPC_TM_GPR(i), &env->tm_gpr[i]);
98980b3f79bSAlexey Kardashevskiy             }
99080b3f79bSAlexey Kardashevskiy             for (i = 0; i < ARRAY_SIZE(env->tm_vsr); i++) {
99180b3f79bSAlexey Kardashevskiy                 kvm_set_one_reg(cs, KVM_REG_PPC_TM_VSR(i), &env->tm_vsr[i]);
99280b3f79bSAlexey Kardashevskiy             }
99380b3f79bSAlexey Kardashevskiy             kvm_set_one_reg(cs, KVM_REG_PPC_TM_CR, &env->tm_cr);
99480b3f79bSAlexey Kardashevskiy             kvm_set_one_reg(cs, KVM_REG_PPC_TM_LR, &env->tm_lr);
99580b3f79bSAlexey Kardashevskiy             kvm_set_one_reg(cs, KVM_REG_PPC_TM_CTR, &env->tm_ctr);
99680b3f79bSAlexey Kardashevskiy             kvm_set_one_reg(cs, KVM_REG_PPC_TM_FPSCR, &env->tm_fpscr);
99780b3f79bSAlexey Kardashevskiy             kvm_set_one_reg(cs, KVM_REG_PPC_TM_AMR, &env->tm_amr);
99880b3f79bSAlexey Kardashevskiy             kvm_set_one_reg(cs, KVM_REG_PPC_TM_PPR, &env->tm_ppr);
99980b3f79bSAlexey Kardashevskiy             kvm_set_one_reg(cs, KVM_REG_PPC_TM_VRSAVE, &env->tm_vrsave);
100080b3f79bSAlexey Kardashevskiy             kvm_set_one_reg(cs, KVM_REG_PPC_TM_VSCR, &env->tm_vscr);
100180b3f79bSAlexey Kardashevskiy             kvm_set_one_reg(cs, KVM_REG_PPC_TM_DSCR, &env->tm_dscr);
100280b3f79bSAlexey Kardashevskiy             kvm_set_one_reg(cs, KVM_REG_PPC_TM_TAR, &env->tm_tar);
100380b3f79bSAlexey Kardashevskiy         }
100480b3f79bSAlexey Kardashevskiy 
10059b00ea49SDavid Gibson         if (cap_papr) {
10069b00ea49SDavid Gibson             if (kvm_put_vpa(cs) < 0) {
10078d83cbf1SGreg Kurz                 trace_kvm_failed_put_vpa();
10089b00ea49SDavid Gibson             }
10099b00ea49SDavid Gibson         }
101098a8b524SAlexey Kardashevskiy 
101198a8b524SAlexey Kardashevskiy         kvm_set_one_reg(cs, KVM_REG_PPC_TB_OFFSET, &env->tb_env->tb_offset);
10129b00ea49SDavid Gibson #endif /* TARGET_PPC64 */
1013f1af19d7SDavid Gibson     }
1014f1af19d7SDavid Gibson 
1015d76d1650Saurel32     return ret;
1016d76d1650Saurel32 }
1017d76d1650Saurel32 
1018c371c2e3SBharat Bhushan static void kvm_sync_excp(CPUPPCState *env, int vector, int ivor)
1019c371c2e3SBharat Bhushan {
1020c371c2e3SBharat Bhushan      env->excp_vectors[vector] = env->spr[ivor] + env->spr[SPR_BOOKE_IVPR];
1021c371c2e3SBharat Bhushan }
1022c371c2e3SBharat Bhushan 
1023a7a00a72SDavid Gibson static int kvmppc_get_booke_sregs(PowerPCCPU *cpu)
1024d76d1650Saurel32 {
102520d695a9SAndreas Färber     CPUPPCState *env = &cpu->env;
1026ba5e5090SAlexander Graf     struct kvm_sregs sregs;
1027a7a00a72SDavid Gibson     int ret;
1028d76d1650Saurel32 
1029a7a00a72SDavid Gibson     ret = kvm_vcpu_ioctl(CPU(cpu), KVM_GET_SREGS, &sregs);
103090dc8812SScott Wood     if (ret < 0) {
103190dc8812SScott Wood         return ret;
103290dc8812SScott Wood     }
103390dc8812SScott Wood 
103490dc8812SScott Wood     if (sregs.u.e.features & KVM_SREGS_E_BASE) {
103590dc8812SScott Wood         env->spr[SPR_BOOKE_CSRR0] = sregs.u.e.csrr0;
103690dc8812SScott Wood         env->spr[SPR_BOOKE_CSRR1] = sregs.u.e.csrr1;
103790dc8812SScott Wood         env->spr[SPR_BOOKE_ESR] = sregs.u.e.esr;
103890dc8812SScott Wood         env->spr[SPR_BOOKE_DEAR] = sregs.u.e.dear;
103990dc8812SScott Wood         env->spr[SPR_BOOKE_MCSR] = sregs.u.e.mcsr;
104090dc8812SScott Wood         env->spr[SPR_BOOKE_TSR] = sregs.u.e.tsr;
104190dc8812SScott Wood         env->spr[SPR_BOOKE_TCR] = sregs.u.e.tcr;
104290dc8812SScott Wood         env->spr[SPR_DECR] = sregs.u.e.dec;
104390dc8812SScott Wood         env->spr[SPR_TBL] = sregs.u.e.tb & 0xffffffff;
104490dc8812SScott Wood         env->spr[SPR_TBU] = sregs.u.e.tb >> 32;
104590dc8812SScott Wood         env->spr[SPR_VRSAVE] = sregs.u.e.vrsave;
104690dc8812SScott Wood     }
104790dc8812SScott Wood 
104890dc8812SScott Wood     if (sregs.u.e.features & KVM_SREGS_E_ARCH206) {
104990dc8812SScott Wood         env->spr[SPR_BOOKE_PIR] = sregs.u.e.pir;
105090dc8812SScott Wood         env->spr[SPR_BOOKE_MCSRR0] = sregs.u.e.mcsrr0;
105190dc8812SScott Wood         env->spr[SPR_BOOKE_MCSRR1] = sregs.u.e.mcsrr1;
105290dc8812SScott Wood         env->spr[SPR_BOOKE_DECAR] = sregs.u.e.decar;
105390dc8812SScott Wood         env->spr[SPR_BOOKE_IVPR] = sregs.u.e.ivpr;
105490dc8812SScott Wood     }
105590dc8812SScott Wood 
105690dc8812SScott Wood     if (sregs.u.e.features & KVM_SREGS_E_64) {
105790dc8812SScott Wood         env->spr[SPR_BOOKE_EPCR] = sregs.u.e.epcr;
105890dc8812SScott Wood     }
105990dc8812SScott Wood 
106090dc8812SScott Wood     if (sregs.u.e.features & KVM_SREGS_E_SPRG8) {
106190dc8812SScott Wood         env->spr[SPR_BOOKE_SPRG8] = sregs.u.e.sprg8;
106290dc8812SScott Wood     }
106390dc8812SScott Wood 
106490dc8812SScott Wood     if (sregs.u.e.features & KVM_SREGS_E_IVOR) {
106590dc8812SScott Wood         env->spr[SPR_BOOKE_IVOR0] = sregs.u.e.ivor_low[0];
1066c371c2e3SBharat Bhushan         kvm_sync_excp(env, POWERPC_EXCP_CRITICAL,  SPR_BOOKE_IVOR0);
106790dc8812SScott Wood         env->spr[SPR_BOOKE_IVOR1] = sregs.u.e.ivor_low[1];
1068c371c2e3SBharat Bhushan         kvm_sync_excp(env, POWERPC_EXCP_MCHECK,  SPR_BOOKE_IVOR1);
106990dc8812SScott Wood         env->spr[SPR_BOOKE_IVOR2] = sregs.u.e.ivor_low[2];
1070c371c2e3SBharat Bhushan         kvm_sync_excp(env, POWERPC_EXCP_DSI,  SPR_BOOKE_IVOR2);
107190dc8812SScott Wood         env->spr[SPR_BOOKE_IVOR3] = sregs.u.e.ivor_low[3];
1072c371c2e3SBharat Bhushan         kvm_sync_excp(env, POWERPC_EXCP_ISI,  SPR_BOOKE_IVOR3);
107390dc8812SScott Wood         env->spr[SPR_BOOKE_IVOR4] = sregs.u.e.ivor_low[4];
1074c371c2e3SBharat Bhushan         kvm_sync_excp(env, POWERPC_EXCP_EXTERNAL,  SPR_BOOKE_IVOR4);
107590dc8812SScott Wood         env->spr[SPR_BOOKE_IVOR5] = sregs.u.e.ivor_low[5];
1076c371c2e3SBharat Bhushan         kvm_sync_excp(env, POWERPC_EXCP_ALIGN,  SPR_BOOKE_IVOR5);
107790dc8812SScott Wood         env->spr[SPR_BOOKE_IVOR6] = sregs.u.e.ivor_low[6];
1078c371c2e3SBharat Bhushan         kvm_sync_excp(env, POWERPC_EXCP_PROGRAM,  SPR_BOOKE_IVOR6);
107990dc8812SScott Wood         env->spr[SPR_BOOKE_IVOR7] = sregs.u.e.ivor_low[7];
1080c371c2e3SBharat Bhushan         kvm_sync_excp(env, POWERPC_EXCP_FPU,  SPR_BOOKE_IVOR7);
108190dc8812SScott Wood         env->spr[SPR_BOOKE_IVOR8] = sregs.u.e.ivor_low[8];
1082c371c2e3SBharat Bhushan         kvm_sync_excp(env, POWERPC_EXCP_SYSCALL,  SPR_BOOKE_IVOR8);
108390dc8812SScott Wood         env->spr[SPR_BOOKE_IVOR9] = sregs.u.e.ivor_low[9];
1084c371c2e3SBharat Bhushan         kvm_sync_excp(env, POWERPC_EXCP_APU,  SPR_BOOKE_IVOR9);
108590dc8812SScott Wood         env->spr[SPR_BOOKE_IVOR10] = sregs.u.e.ivor_low[10];
1086c371c2e3SBharat Bhushan         kvm_sync_excp(env, POWERPC_EXCP_DECR,  SPR_BOOKE_IVOR10);
108790dc8812SScott Wood         env->spr[SPR_BOOKE_IVOR11] = sregs.u.e.ivor_low[11];
1088c371c2e3SBharat Bhushan         kvm_sync_excp(env, POWERPC_EXCP_FIT,  SPR_BOOKE_IVOR11);
108990dc8812SScott Wood         env->spr[SPR_BOOKE_IVOR12] = sregs.u.e.ivor_low[12];
1090c371c2e3SBharat Bhushan         kvm_sync_excp(env, POWERPC_EXCP_WDT,  SPR_BOOKE_IVOR12);
109190dc8812SScott Wood         env->spr[SPR_BOOKE_IVOR13] = sregs.u.e.ivor_low[13];
1092c371c2e3SBharat Bhushan         kvm_sync_excp(env, POWERPC_EXCP_DTLB,  SPR_BOOKE_IVOR13);
109390dc8812SScott Wood         env->spr[SPR_BOOKE_IVOR14] = sregs.u.e.ivor_low[14];
1094c371c2e3SBharat Bhushan         kvm_sync_excp(env, POWERPC_EXCP_ITLB,  SPR_BOOKE_IVOR14);
109590dc8812SScott Wood         env->spr[SPR_BOOKE_IVOR15] = sregs.u.e.ivor_low[15];
1096c371c2e3SBharat Bhushan         kvm_sync_excp(env, POWERPC_EXCP_DEBUG,  SPR_BOOKE_IVOR15);
109790dc8812SScott Wood 
109890dc8812SScott Wood         if (sregs.u.e.features & KVM_SREGS_E_SPE) {
109990dc8812SScott Wood             env->spr[SPR_BOOKE_IVOR32] = sregs.u.e.ivor_high[0];
1100c371c2e3SBharat Bhushan             kvm_sync_excp(env, POWERPC_EXCP_SPEU,  SPR_BOOKE_IVOR32);
110190dc8812SScott Wood             env->spr[SPR_BOOKE_IVOR33] = sregs.u.e.ivor_high[1];
1102c371c2e3SBharat Bhushan             kvm_sync_excp(env, POWERPC_EXCP_EFPDI,  SPR_BOOKE_IVOR33);
110390dc8812SScott Wood             env->spr[SPR_BOOKE_IVOR34] = sregs.u.e.ivor_high[2];
1104c371c2e3SBharat Bhushan             kvm_sync_excp(env, POWERPC_EXCP_EFPRI,  SPR_BOOKE_IVOR34);
110590dc8812SScott Wood         }
110690dc8812SScott Wood 
110790dc8812SScott Wood         if (sregs.u.e.features & KVM_SREGS_E_PM) {
110890dc8812SScott Wood             env->spr[SPR_BOOKE_IVOR35] = sregs.u.e.ivor_high[3];
1109c371c2e3SBharat Bhushan             kvm_sync_excp(env, POWERPC_EXCP_EPERFM,  SPR_BOOKE_IVOR35);
111090dc8812SScott Wood         }
111190dc8812SScott Wood 
111290dc8812SScott Wood         if (sregs.u.e.features & KVM_SREGS_E_PC) {
111390dc8812SScott Wood             env->spr[SPR_BOOKE_IVOR36] = sregs.u.e.ivor_high[4];
1114c371c2e3SBharat Bhushan             kvm_sync_excp(env, POWERPC_EXCP_DOORI,  SPR_BOOKE_IVOR36);
111590dc8812SScott Wood             env->spr[SPR_BOOKE_IVOR37] = sregs.u.e.ivor_high[5];
1116c371c2e3SBharat Bhushan             kvm_sync_excp(env, POWERPC_EXCP_DOORCI, SPR_BOOKE_IVOR37);
111790dc8812SScott Wood         }
111890dc8812SScott Wood     }
111990dc8812SScott Wood 
112090dc8812SScott Wood     if (sregs.u.e.features & KVM_SREGS_E_ARCH206_MMU) {
112190dc8812SScott Wood         env->spr[SPR_BOOKE_MAS0] = sregs.u.e.mas0;
112290dc8812SScott Wood         env->spr[SPR_BOOKE_MAS1] = sregs.u.e.mas1;
112390dc8812SScott Wood         env->spr[SPR_BOOKE_MAS2] = sregs.u.e.mas2;
112490dc8812SScott Wood         env->spr[SPR_BOOKE_MAS3] = sregs.u.e.mas7_3 & 0xffffffff;
112590dc8812SScott Wood         env->spr[SPR_BOOKE_MAS4] = sregs.u.e.mas4;
112690dc8812SScott Wood         env->spr[SPR_BOOKE_MAS6] = sregs.u.e.mas6;
112790dc8812SScott Wood         env->spr[SPR_BOOKE_MAS7] = sregs.u.e.mas7_3 >> 32;
112890dc8812SScott Wood         env->spr[SPR_MMUCFG] = sregs.u.e.mmucfg;
112990dc8812SScott Wood         env->spr[SPR_BOOKE_TLB0CFG] = sregs.u.e.tlbcfg[0];
113090dc8812SScott Wood         env->spr[SPR_BOOKE_TLB1CFG] = sregs.u.e.tlbcfg[1];
113190dc8812SScott Wood     }
113290dc8812SScott Wood 
113390dc8812SScott Wood     if (sregs.u.e.features & KVM_SREGS_EXP) {
113490dc8812SScott Wood         env->spr[SPR_BOOKE_EPR] = sregs.u.e.epr;
113590dc8812SScott Wood     }
113690dc8812SScott Wood 
113790dc8812SScott Wood     if (sregs.u.e.features & KVM_SREGS_E_PD) {
113890dc8812SScott Wood         env->spr[SPR_BOOKE_EPLC] = sregs.u.e.eplc;
113990dc8812SScott Wood         env->spr[SPR_BOOKE_EPSC] = sregs.u.e.epsc;
114090dc8812SScott Wood     }
114190dc8812SScott Wood 
114290dc8812SScott Wood     if (sregs.u.e.impl_id == KVM_SREGS_E_IMPL_FSL) {
114390dc8812SScott Wood         env->spr[SPR_E500_SVR] = sregs.u.e.impl.fsl.svr;
114490dc8812SScott Wood         env->spr[SPR_Exxx_MCAR] = sregs.u.e.impl.fsl.mcar;
114590dc8812SScott Wood         env->spr[SPR_HID0] = sregs.u.e.impl.fsl.hid0;
114690dc8812SScott Wood 
114790dc8812SScott Wood         if (sregs.u.e.impl.fsl.features & KVM_SREGS_E_FSL_PIDn) {
114890dc8812SScott Wood             env->spr[SPR_BOOKE_PID1] = sregs.u.e.impl.fsl.pid1;
114990dc8812SScott Wood             env->spr[SPR_BOOKE_PID2] = sregs.u.e.impl.fsl.pid2;
115090dc8812SScott Wood         }
115190dc8812SScott Wood     }
1152a7a00a72SDavid Gibson 
1153a7a00a72SDavid Gibson     return 0;
1154fafc0b6aSAlexander Graf }
115590dc8812SScott Wood 
1156a7a00a72SDavid Gibson static int kvmppc_get_books_sregs(PowerPCCPU *cpu)
1157a7a00a72SDavid Gibson {
1158a7a00a72SDavid Gibson     CPUPPCState *env = &cpu->env;
1159a7a00a72SDavid Gibson     struct kvm_sregs sregs;
1160a7a00a72SDavid Gibson     int ret;
1161a7a00a72SDavid Gibson     int i;
1162a7a00a72SDavid Gibson 
1163a7a00a72SDavid Gibson     ret = kvm_vcpu_ioctl(CPU(cpu), KVM_GET_SREGS, &sregs);
116490dc8812SScott Wood     if (ret < 0) {
116590dc8812SScott Wood         return ret;
116690dc8812SScott Wood     }
116790dc8812SScott Wood 
1168e57ca75cSDavid Gibson     if (!cpu->vhyp) {
1169bb593904SDavid Gibson         ppc_store_sdr1(env, sregs.u.s.sdr1);
1170f3c75d42SAneesh Kumar K.V     }
1171ba5e5090SAlexander Graf 
1172ba5e5090SAlexander Graf     /* Sync SLB */
117382c09f2fSAlexander Graf #ifdef TARGET_PPC64
11744b4d4a21SAneesh Kumar K.V     /*
11754b4d4a21SAneesh Kumar K.V      * The packed SLB array we get from KVM_GET_SREGS only contains
1176a7a00a72SDavid Gibson      * information about valid entries. So we flush our internal copy
1177a7a00a72SDavid Gibson      * to get rid of stale ones, then put all valid SLB entries back
1178a7a00a72SDavid Gibson      * in.
11794b4d4a21SAneesh Kumar K.V      */
11804b4d4a21SAneesh Kumar K.V     memset(env->slb, 0, sizeof(env->slb));
1181d83af167SAneesh Kumar K.V     for (i = 0; i < ARRAY_SIZE(env->slb); i++) {
11824b4d4a21SAneesh Kumar K.V         target_ulong rb = sregs.u.s.ppc64.slb[i].slbe;
11834b4d4a21SAneesh Kumar K.V         target_ulong rs = sregs.u.s.ppc64.slb[i].slbv;
11844b4d4a21SAneesh Kumar K.V         /*
11854b4d4a21SAneesh Kumar K.V          * Only restore valid entries
11864b4d4a21SAneesh Kumar K.V          */
11874b4d4a21SAneesh Kumar K.V         if (rb & SLB_ESID_V) {
1188bcd81230SDavid Gibson             ppc_store_slb(cpu, rb & 0xfff, rb & ~0xfffULL, rs);
11894b4d4a21SAneesh Kumar K.V         }
1190ba5e5090SAlexander Graf     }
119182c09f2fSAlexander Graf #endif
1192ba5e5090SAlexander Graf 
1193ba5e5090SAlexander Graf     /* Sync SRs */
1194ba5e5090SAlexander Graf     for (i = 0; i < 16; i++) {
1195ba5e5090SAlexander Graf         env->sr[i] = sregs.u.s.ppc32.sr[i];
1196ba5e5090SAlexander Graf     }
1197ba5e5090SAlexander Graf 
1198ba5e5090SAlexander Graf     /* Sync BATs */
1199ba5e5090SAlexander Graf     for (i = 0; i < 8; i++) {
1200ba5e5090SAlexander Graf         env->DBAT[0][i] = sregs.u.s.ppc32.dbat[i] & 0xffffffff;
1201ba5e5090SAlexander Graf         env->DBAT[1][i] = sregs.u.s.ppc32.dbat[i] >> 32;
1202ba5e5090SAlexander Graf         env->IBAT[0][i] = sregs.u.s.ppc32.ibat[i] & 0xffffffff;
1203ba5e5090SAlexander Graf         env->IBAT[1][i] = sregs.u.s.ppc32.ibat[i] >> 32;
1204ba5e5090SAlexander Graf     }
1205a7a00a72SDavid Gibson 
1206a7a00a72SDavid Gibson     return 0;
1207a7a00a72SDavid Gibson }
1208a7a00a72SDavid Gibson 
1209a7a00a72SDavid Gibson int kvm_arch_get_registers(CPUState *cs)
1210a7a00a72SDavid Gibson {
1211a7a00a72SDavid Gibson     PowerPCCPU *cpu = POWERPC_CPU(cs);
1212a7a00a72SDavid Gibson     CPUPPCState *env = &cpu->env;
1213a7a00a72SDavid Gibson     struct kvm_regs regs;
1214a7a00a72SDavid Gibson     uint32_t cr;
1215a7a00a72SDavid Gibson     int i, ret;
1216a7a00a72SDavid Gibson 
1217a7a00a72SDavid Gibson     ret = kvm_vcpu_ioctl(cs, KVM_GET_REGS, &regs);
1218c995e942SDavid Gibson     if (ret < 0) {
1219a7a00a72SDavid Gibson         return ret;
1220c995e942SDavid Gibson     }
1221a7a00a72SDavid Gibson 
1222a7a00a72SDavid Gibson     cr = regs.cr;
1223a7a00a72SDavid Gibson     for (i = 7; i >= 0; i--) {
1224a7a00a72SDavid Gibson         env->crf[i] = cr & 15;
1225a7a00a72SDavid Gibson         cr >>= 4;
1226a7a00a72SDavid Gibson     }
1227a7a00a72SDavid Gibson 
1228a7a00a72SDavid Gibson     env->ctr = regs.ctr;
1229a7a00a72SDavid Gibson     env->lr = regs.lr;
1230a7a00a72SDavid Gibson     cpu_write_xer(env, regs.xer);
1231a7a00a72SDavid Gibson     env->msr = regs.msr;
1232a7a00a72SDavid Gibson     env->nip = regs.pc;
1233a7a00a72SDavid Gibson 
1234a7a00a72SDavid Gibson     env->spr[SPR_SRR0] = regs.srr0;
1235a7a00a72SDavid Gibson     env->spr[SPR_SRR1] = regs.srr1;
1236a7a00a72SDavid Gibson 
1237a7a00a72SDavid Gibson     env->spr[SPR_SPRG0] = regs.sprg0;
1238a7a00a72SDavid Gibson     env->spr[SPR_SPRG1] = regs.sprg1;
1239a7a00a72SDavid Gibson     env->spr[SPR_SPRG2] = regs.sprg2;
1240a7a00a72SDavid Gibson     env->spr[SPR_SPRG3] = regs.sprg3;
1241a7a00a72SDavid Gibson     env->spr[SPR_SPRG4] = regs.sprg4;
1242a7a00a72SDavid Gibson     env->spr[SPR_SPRG5] = regs.sprg5;
1243a7a00a72SDavid Gibson     env->spr[SPR_SPRG6] = regs.sprg6;
1244a7a00a72SDavid Gibson     env->spr[SPR_SPRG7] = regs.sprg7;
1245a7a00a72SDavid Gibson 
1246a7a00a72SDavid Gibson     env->spr[SPR_BOOKE_PID] = regs.pid;
1247a7a00a72SDavid Gibson 
1248c995e942SDavid Gibson     for (i = 0; i < 32; i++) {
1249a7a00a72SDavid Gibson         env->gpr[i] = regs.gpr[i];
1250c995e942SDavid Gibson     }
1251a7a00a72SDavid Gibson 
1252a7a00a72SDavid Gibson     kvm_get_fp(cs);
1253a7a00a72SDavid Gibson 
1254a7a00a72SDavid Gibson     if (cap_booke_sregs) {
1255a7a00a72SDavid Gibson         ret = kvmppc_get_booke_sregs(cpu);
1256a7a00a72SDavid Gibson         if (ret < 0) {
1257a7a00a72SDavid Gibson             return ret;
1258a7a00a72SDavid Gibson         }
1259a7a00a72SDavid Gibson     }
1260a7a00a72SDavid Gibson 
1261a7a00a72SDavid Gibson     if (cap_segstate) {
1262a7a00a72SDavid Gibson         ret = kvmppc_get_books_sregs(cpu);
1263a7a00a72SDavid Gibson         if (ret < 0) {
1264a7a00a72SDavid Gibson             return ret;
1265a7a00a72SDavid Gibson         }
1266fafc0b6aSAlexander Graf     }
1267ba5e5090SAlexander Graf 
1268d67d40eaSDavid Gibson     if (cap_hior) {
1269d67d40eaSDavid Gibson         kvm_get_one_spr(cs, KVM_REG_PPC_HIOR, SPR_HIOR);
1270d67d40eaSDavid Gibson     }
1271d67d40eaSDavid Gibson 
1272d67d40eaSDavid Gibson     if (cap_one_reg) {
1273d67d40eaSDavid Gibson         int i;
1274d67d40eaSDavid Gibson 
1275c995e942SDavid Gibson         /*
1276c995e942SDavid Gibson          * We deliberately ignore errors here, for kernels which have
1277d67d40eaSDavid Gibson          * the ONE_REG calls, but don't support the specific
1278d67d40eaSDavid Gibson          * registers, there's a reasonable chance things will still
1279c995e942SDavid Gibson          * work, at least until we try to migrate.
1280c995e942SDavid Gibson          */
1281d67d40eaSDavid Gibson         for (i = 0; i < 1024; i++) {
1282d67d40eaSDavid Gibson             uint64_t id = env->spr_cb[i].one_reg_id;
1283d67d40eaSDavid Gibson 
1284d67d40eaSDavid Gibson             if (id != 0) {
1285d67d40eaSDavid Gibson                 kvm_get_one_spr(cs, id, i);
1286d67d40eaSDavid Gibson             }
1287d67d40eaSDavid Gibson         }
12889b00ea49SDavid Gibson 
12899b00ea49SDavid Gibson #ifdef TARGET_PPC64
129080b3f79bSAlexey Kardashevskiy         if (msr_ts) {
129180b3f79bSAlexey Kardashevskiy             for (i = 0; i < ARRAY_SIZE(env->tm_gpr); i++) {
129280b3f79bSAlexey Kardashevskiy                 kvm_get_one_reg(cs, KVM_REG_PPC_TM_GPR(i), &env->tm_gpr[i]);
129380b3f79bSAlexey Kardashevskiy             }
129480b3f79bSAlexey Kardashevskiy             for (i = 0; i < ARRAY_SIZE(env->tm_vsr); i++) {
129580b3f79bSAlexey Kardashevskiy                 kvm_get_one_reg(cs, KVM_REG_PPC_TM_VSR(i), &env->tm_vsr[i]);
129680b3f79bSAlexey Kardashevskiy             }
129780b3f79bSAlexey Kardashevskiy             kvm_get_one_reg(cs, KVM_REG_PPC_TM_CR, &env->tm_cr);
129880b3f79bSAlexey Kardashevskiy             kvm_get_one_reg(cs, KVM_REG_PPC_TM_LR, &env->tm_lr);
129980b3f79bSAlexey Kardashevskiy             kvm_get_one_reg(cs, KVM_REG_PPC_TM_CTR, &env->tm_ctr);
130080b3f79bSAlexey Kardashevskiy             kvm_get_one_reg(cs, KVM_REG_PPC_TM_FPSCR, &env->tm_fpscr);
130180b3f79bSAlexey Kardashevskiy             kvm_get_one_reg(cs, KVM_REG_PPC_TM_AMR, &env->tm_amr);
130280b3f79bSAlexey Kardashevskiy             kvm_get_one_reg(cs, KVM_REG_PPC_TM_PPR, &env->tm_ppr);
130380b3f79bSAlexey Kardashevskiy             kvm_get_one_reg(cs, KVM_REG_PPC_TM_VRSAVE, &env->tm_vrsave);
130480b3f79bSAlexey Kardashevskiy             kvm_get_one_reg(cs, KVM_REG_PPC_TM_VSCR, &env->tm_vscr);
130580b3f79bSAlexey Kardashevskiy             kvm_get_one_reg(cs, KVM_REG_PPC_TM_DSCR, &env->tm_dscr);
130680b3f79bSAlexey Kardashevskiy             kvm_get_one_reg(cs, KVM_REG_PPC_TM_TAR, &env->tm_tar);
130780b3f79bSAlexey Kardashevskiy         }
130880b3f79bSAlexey Kardashevskiy 
13099b00ea49SDavid Gibson         if (cap_papr) {
13109b00ea49SDavid Gibson             if (kvm_get_vpa(cs) < 0) {
13118d83cbf1SGreg Kurz                 trace_kvm_failed_get_vpa();
13129b00ea49SDavid Gibson             }
13139b00ea49SDavid Gibson         }
131498a8b524SAlexey Kardashevskiy 
131598a8b524SAlexey Kardashevskiy         kvm_get_one_reg(cs, KVM_REG_PPC_TB_OFFSET, &env->tb_env->tb_offset);
13169b00ea49SDavid Gibson #endif
1317d67d40eaSDavid Gibson     }
1318d67d40eaSDavid Gibson 
1319d76d1650Saurel32     return 0;
1320d76d1650Saurel32 }
1321d76d1650Saurel32 
13221bc22652SAndreas Färber int kvmppc_set_interrupt(PowerPCCPU *cpu, int irq, int level)
1323fc87e185SAlexander Graf {
1324fc87e185SAlexander Graf     unsigned virq = level ? KVM_INTERRUPT_SET_LEVEL : KVM_INTERRUPT_UNSET;
1325fc87e185SAlexander Graf 
1326fc87e185SAlexander Graf     if (irq != PPC_INTERRUPT_EXT) {
1327fc87e185SAlexander Graf         return 0;
1328fc87e185SAlexander Graf     }
1329fc87e185SAlexander Graf 
1330fc87e185SAlexander Graf     if (!kvm_enabled() || !cap_interrupt_unset || !cap_interrupt_level) {
1331fc87e185SAlexander Graf         return 0;
1332fc87e185SAlexander Graf     }
1333fc87e185SAlexander Graf 
13341bc22652SAndreas Färber     kvm_vcpu_ioctl(CPU(cpu), KVM_INTERRUPT, &virq);
1335fc87e185SAlexander Graf 
1336fc87e185SAlexander Graf     return 0;
1337fc87e185SAlexander Graf }
1338fc87e185SAlexander Graf 
1339a69dc537SThomas Huth #if defined(TARGET_PPC64)
134016415335SAlexander Graf #define PPC_INPUT_INT PPC970_INPUT_INT
134116415335SAlexander Graf #else
134216415335SAlexander Graf #define PPC_INPUT_INT PPC6xx_INPUT_INT
134316415335SAlexander Graf #endif
134416415335SAlexander Graf 
134520d695a9SAndreas Färber void kvm_arch_pre_run(CPUState *cs, struct kvm_run *run)
1346d76d1650Saurel32 {
134720d695a9SAndreas Färber     PowerPCCPU *cpu = POWERPC_CPU(cs);
134820d695a9SAndreas Färber     CPUPPCState *env = &cpu->env;
1349d76d1650Saurel32     int r;
1350d76d1650Saurel32     unsigned irq;
1351d76d1650Saurel32 
13524b8523eeSJan Kiszka     qemu_mutex_lock_iothread();
13534b8523eeSJan Kiszka 
1354c995e942SDavid Gibson     /*
1355c995e942SDavid Gibson      * PowerPC QEMU tracks the various core input pins (interrupt,
1356c995e942SDavid Gibson      * critical interrupt, reset, etc) in PPC-specific
1357c995e942SDavid Gibson      * env->irq_input_state.
1358c995e942SDavid Gibson      */
1359fc87e185SAlexander Graf     if (!cap_interrupt_level &&
1360fc87e185SAlexander Graf         run->ready_for_interrupt_injection &&
1361259186a7SAndreas Färber         (cs->interrupt_request & CPU_INTERRUPT_HARD) &&
136216415335SAlexander Graf         (env->irq_input_state & (1 << PPC_INPUT_INT)))
1363d76d1650Saurel32     {
1364c995e942SDavid Gibson         /*
1365c995e942SDavid Gibson          * For now KVM disregards the 'irq' argument. However, in the
1366c995e942SDavid Gibson          * future KVM could cache it in-kernel to avoid a heavyweight
1367c995e942SDavid Gibson          * exit when reading the UIC.
1368d76d1650Saurel32          */
1369fc87e185SAlexander Graf         irq = KVM_INTERRUPT_SET;
1370d76d1650Saurel32 
13718d83cbf1SGreg Kurz         trace_kvm_injected_interrupt(irq);
13721bc22652SAndreas Färber         r = kvm_vcpu_ioctl(cs, KVM_INTERRUPT, &irq);
137355e5c285SAndreas Färber         if (r < 0) {
137455e5c285SAndreas Färber             printf("cpu %d fail inject %x\n", cs->cpu_index, irq);
137555e5c285SAndreas Färber         }
1376c821c2bdSAlexander Graf 
1377c821c2bdSAlexander Graf         /* Always wake up soon in case the interrupt was level based */
1378bc72ad67SAlex Bligh         timer_mod(idle_timer, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) +
137973bcb24dSRutuja Shah                        (NANOSECONDS_PER_SECOND / 50));
1380d76d1650Saurel32     }
1381d76d1650Saurel32 
1382c995e942SDavid Gibson     /*
1383c995e942SDavid Gibson      * We don't know if there are more interrupts pending after
1384c995e942SDavid Gibson      * this. However, the guest will return to userspace in the course
1385c995e942SDavid Gibson      * of handling this one anyways, so we will get a chance to
1386c995e942SDavid Gibson      * deliver the rest.
1387c995e942SDavid Gibson      */
13884b8523eeSJan Kiszka 
13894b8523eeSJan Kiszka     qemu_mutex_unlock_iothread();
1390d76d1650Saurel32 }
1391d76d1650Saurel32 
13924c663752SPaolo Bonzini MemTxAttrs kvm_arch_post_run(CPUState *cs, struct kvm_run *run)
1393d76d1650Saurel32 {
13944c663752SPaolo Bonzini     return MEMTXATTRS_UNSPECIFIED;
1395d76d1650Saurel32 }
1396d76d1650Saurel32 
139720d695a9SAndreas Färber int kvm_arch_process_async_events(CPUState *cs)
13980af691d7SMarcelo Tosatti {
1399259186a7SAndreas Färber     return cs->halted;
14000af691d7SMarcelo Tosatti }
14010af691d7SMarcelo Tosatti 
1402259186a7SAndreas Färber static int kvmppc_handle_halt(PowerPCCPU *cpu)
1403d76d1650Saurel32 {
1404259186a7SAndreas Färber     CPUState *cs = CPU(cpu);
1405259186a7SAndreas Färber     CPUPPCState *env = &cpu->env;
1406259186a7SAndreas Färber 
1407259186a7SAndreas Färber     if (!(cs->interrupt_request & CPU_INTERRUPT_HARD) && (msr_ee)) {
1408259186a7SAndreas Färber         cs->halted = 1;
140927103424SAndreas Färber         cs->exception_index = EXCP_HLT;
1410d76d1650Saurel32     }
1411d76d1650Saurel32 
1412bb4ea393SJan Kiszka     return 0;
1413d76d1650Saurel32 }
1414d76d1650Saurel32 
1415d76d1650Saurel32 /* map dcr access to existing qemu dcr emulation */
1416c995e942SDavid Gibson static int kvmppc_handle_dcr_read(CPUPPCState *env,
1417c995e942SDavid Gibson                                   uint32_t dcrn, uint32_t *data)
1418d76d1650Saurel32 {
1419c995e942SDavid Gibson     if (ppc_dcr_read(env->dcr_env, dcrn, data) < 0) {
1420d76d1650Saurel32         fprintf(stderr, "Read to unhandled DCR (0x%x)\n", dcrn);
1421c995e942SDavid Gibson     }
1422d76d1650Saurel32 
1423bb4ea393SJan Kiszka     return 0;
1424d76d1650Saurel32 }
1425d76d1650Saurel32 
1426c995e942SDavid Gibson static int kvmppc_handle_dcr_write(CPUPPCState *env,
1427c995e942SDavid Gibson                                    uint32_t dcrn, uint32_t data)
1428d76d1650Saurel32 {
1429c995e942SDavid Gibson     if (ppc_dcr_write(env->dcr_env, dcrn, data) < 0) {
1430d76d1650Saurel32         fprintf(stderr, "Write to unhandled DCR (0x%x)\n", dcrn);
1431c995e942SDavid Gibson     }
1432d76d1650Saurel32 
1433bb4ea393SJan Kiszka     return 0;
1434d76d1650Saurel32 }
1435d76d1650Saurel32 
14368a0548f9SBharat Bhushan int kvm_arch_insert_sw_breakpoint(CPUState *cs, struct kvm_sw_breakpoint *bp)
14378a0548f9SBharat Bhushan {
14388a0548f9SBharat Bhushan     /* Mixed endian case is not handled */
14398a0548f9SBharat Bhushan     uint32_t sc = debug_inst_opcode;
14408a0548f9SBharat Bhushan 
14418a0548f9SBharat Bhushan     if (cpu_memory_rw_debug(cs, bp->pc, (uint8_t *)&bp->saved_insn,
14428a0548f9SBharat Bhushan                             sizeof(sc), 0) ||
14438a0548f9SBharat Bhushan         cpu_memory_rw_debug(cs, bp->pc, (uint8_t *)&sc, sizeof(sc), 1)) {
14448a0548f9SBharat Bhushan         return -EINVAL;
14458a0548f9SBharat Bhushan     }
14468a0548f9SBharat Bhushan 
14478a0548f9SBharat Bhushan     return 0;
14488a0548f9SBharat Bhushan }
14498a0548f9SBharat Bhushan 
14508a0548f9SBharat Bhushan int kvm_arch_remove_sw_breakpoint(CPUState *cs, struct kvm_sw_breakpoint *bp)
14518a0548f9SBharat Bhushan {
14528a0548f9SBharat Bhushan     uint32_t sc;
14538a0548f9SBharat Bhushan 
14548a0548f9SBharat Bhushan     if (cpu_memory_rw_debug(cs, bp->pc, (uint8_t *)&sc, sizeof(sc), 0) ||
14558a0548f9SBharat Bhushan         sc != debug_inst_opcode ||
14568a0548f9SBharat Bhushan         cpu_memory_rw_debug(cs, bp->pc, (uint8_t *)&bp->saved_insn,
14578a0548f9SBharat Bhushan                             sizeof(sc), 1)) {
14588a0548f9SBharat Bhushan         return -EINVAL;
14598a0548f9SBharat Bhushan     }
14608a0548f9SBharat Bhushan 
14618a0548f9SBharat Bhushan     return 0;
14628a0548f9SBharat Bhushan }
14638a0548f9SBharat Bhushan 
146488365d17SBharat Bhushan static int find_hw_breakpoint(target_ulong addr, int type)
146588365d17SBharat Bhushan {
146688365d17SBharat Bhushan     int n;
146788365d17SBharat Bhushan 
146888365d17SBharat Bhushan     assert((nb_hw_breakpoint + nb_hw_watchpoint)
146988365d17SBharat Bhushan            <= ARRAY_SIZE(hw_debug_points));
147088365d17SBharat Bhushan 
147188365d17SBharat Bhushan     for (n = 0; n < nb_hw_breakpoint + nb_hw_watchpoint; n++) {
147288365d17SBharat Bhushan         if (hw_debug_points[n].addr == addr &&
147388365d17SBharat Bhushan              hw_debug_points[n].type == type) {
147488365d17SBharat Bhushan             return n;
147588365d17SBharat Bhushan         }
147688365d17SBharat Bhushan     }
147788365d17SBharat Bhushan 
147888365d17SBharat Bhushan     return -1;
147988365d17SBharat Bhushan }
148088365d17SBharat Bhushan 
148188365d17SBharat Bhushan static int find_hw_watchpoint(target_ulong addr, int *flag)
148288365d17SBharat Bhushan {
148388365d17SBharat Bhushan     int n;
148488365d17SBharat Bhushan 
148588365d17SBharat Bhushan     n = find_hw_breakpoint(addr, GDB_WATCHPOINT_ACCESS);
148688365d17SBharat Bhushan     if (n >= 0) {
148788365d17SBharat Bhushan         *flag = BP_MEM_ACCESS;
148888365d17SBharat Bhushan         return n;
148988365d17SBharat Bhushan     }
149088365d17SBharat Bhushan 
149188365d17SBharat Bhushan     n = find_hw_breakpoint(addr, GDB_WATCHPOINT_WRITE);
149288365d17SBharat Bhushan     if (n >= 0) {
149388365d17SBharat Bhushan         *flag = BP_MEM_WRITE;
149488365d17SBharat Bhushan         return n;
149588365d17SBharat Bhushan     }
149688365d17SBharat Bhushan 
149788365d17SBharat Bhushan     n = find_hw_breakpoint(addr, GDB_WATCHPOINT_READ);
149888365d17SBharat Bhushan     if (n >= 0) {
149988365d17SBharat Bhushan         *flag = BP_MEM_READ;
150088365d17SBharat Bhushan         return n;
150188365d17SBharat Bhushan     }
150288365d17SBharat Bhushan 
150388365d17SBharat Bhushan     return -1;
150488365d17SBharat Bhushan }
150588365d17SBharat Bhushan 
150688365d17SBharat Bhushan int kvm_arch_insert_hw_breakpoint(target_ulong addr,
150788365d17SBharat Bhushan                                   target_ulong len, int type)
150888365d17SBharat Bhushan {
150988365d17SBharat Bhushan     if ((nb_hw_breakpoint + nb_hw_watchpoint) >= ARRAY_SIZE(hw_debug_points)) {
151088365d17SBharat Bhushan         return -ENOBUFS;
151188365d17SBharat Bhushan     }
151288365d17SBharat Bhushan 
151388365d17SBharat Bhushan     hw_debug_points[nb_hw_breakpoint + nb_hw_watchpoint].addr = addr;
151488365d17SBharat Bhushan     hw_debug_points[nb_hw_breakpoint + nb_hw_watchpoint].type = type;
151588365d17SBharat Bhushan 
151688365d17SBharat Bhushan     switch (type) {
151788365d17SBharat Bhushan     case GDB_BREAKPOINT_HW:
151888365d17SBharat Bhushan         if (nb_hw_breakpoint >= max_hw_breakpoint) {
151988365d17SBharat Bhushan             return -ENOBUFS;
152088365d17SBharat Bhushan         }
152188365d17SBharat Bhushan 
152288365d17SBharat Bhushan         if (find_hw_breakpoint(addr, type) >= 0) {
152388365d17SBharat Bhushan             return -EEXIST;
152488365d17SBharat Bhushan         }
152588365d17SBharat Bhushan 
152688365d17SBharat Bhushan         nb_hw_breakpoint++;
152788365d17SBharat Bhushan         break;
152888365d17SBharat Bhushan 
152988365d17SBharat Bhushan     case GDB_WATCHPOINT_WRITE:
153088365d17SBharat Bhushan     case GDB_WATCHPOINT_READ:
153188365d17SBharat Bhushan     case GDB_WATCHPOINT_ACCESS:
153288365d17SBharat Bhushan         if (nb_hw_watchpoint >= max_hw_watchpoint) {
153388365d17SBharat Bhushan             return -ENOBUFS;
153488365d17SBharat Bhushan         }
153588365d17SBharat Bhushan 
153688365d17SBharat Bhushan         if (find_hw_breakpoint(addr, type) >= 0) {
153788365d17SBharat Bhushan             return -EEXIST;
153888365d17SBharat Bhushan         }
153988365d17SBharat Bhushan 
154088365d17SBharat Bhushan         nb_hw_watchpoint++;
154188365d17SBharat Bhushan         break;
154288365d17SBharat Bhushan 
154388365d17SBharat Bhushan     default:
154488365d17SBharat Bhushan         return -ENOSYS;
154588365d17SBharat Bhushan     }
154688365d17SBharat Bhushan 
154788365d17SBharat Bhushan     return 0;
154888365d17SBharat Bhushan }
154988365d17SBharat Bhushan 
155088365d17SBharat Bhushan int kvm_arch_remove_hw_breakpoint(target_ulong addr,
155188365d17SBharat Bhushan                                   target_ulong len, int type)
155288365d17SBharat Bhushan {
155388365d17SBharat Bhushan     int n;
155488365d17SBharat Bhushan 
155588365d17SBharat Bhushan     n = find_hw_breakpoint(addr, type);
155688365d17SBharat Bhushan     if (n < 0) {
155788365d17SBharat Bhushan         return -ENOENT;
155888365d17SBharat Bhushan     }
155988365d17SBharat Bhushan 
156088365d17SBharat Bhushan     switch (type) {
156188365d17SBharat Bhushan     case GDB_BREAKPOINT_HW:
156288365d17SBharat Bhushan         nb_hw_breakpoint--;
156388365d17SBharat Bhushan         break;
156488365d17SBharat Bhushan 
156588365d17SBharat Bhushan     case GDB_WATCHPOINT_WRITE:
156688365d17SBharat Bhushan     case GDB_WATCHPOINT_READ:
156788365d17SBharat Bhushan     case GDB_WATCHPOINT_ACCESS:
156888365d17SBharat Bhushan         nb_hw_watchpoint--;
156988365d17SBharat Bhushan         break;
157088365d17SBharat Bhushan 
157188365d17SBharat Bhushan     default:
157288365d17SBharat Bhushan         return -ENOSYS;
157388365d17SBharat Bhushan     }
157488365d17SBharat Bhushan     hw_debug_points[n] = hw_debug_points[nb_hw_breakpoint + nb_hw_watchpoint];
157588365d17SBharat Bhushan 
157688365d17SBharat Bhushan     return 0;
157788365d17SBharat Bhushan }
157888365d17SBharat Bhushan 
157988365d17SBharat Bhushan void kvm_arch_remove_all_hw_breakpoints(void)
158088365d17SBharat Bhushan {
158188365d17SBharat Bhushan     nb_hw_breakpoint = nb_hw_watchpoint = 0;
158288365d17SBharat Bhushan }
158388365d17SBharat Bhushan 
15848a0548f9SBharat Bhushan void kvm_arch_update_guest_debug(CPUState *cs, struct kvm_guest_debug *dbg)
15858a0548f9SBharat Bhushan {
158688365d17SBharat Bhushan     int n;
158788365d17SBharat Bhushan 
15888a0548f9SBharat Bhushan     /* Software Breakpoint updates */
15898a0548f9SBharat Bhushan     if (kvm_sw_breakpoints_active(cs)) {
15908a0548f9SBharat Bhushan         dbg->control |= KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP;
15918a0548f9SBharat Bhushan     }
159288365d17SBharat Bhushan 
159388365d17SBharat Bhushan     assert((nb_hw_breakpoint + nb_hw_watchpoint)
159488365d17SBharat Bhushan            <= ARRAY_SIZE(hw_debug_points));
159588365d17SBharat Bhushan     assert((nb_hw_breakpoint + nb_hw_watchpoint) <= ARRAY_SIZE(dbg->arch.bp));
159688365d17SBharat Bhushan 
159788365d17SBharat Bhushan     if (nb_hw_breakpoint + nb_hw_watchpoint > 0) {
159888365d17SBharat Bhushan         dbg->control |= KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_HW_BP;
159988365d17SBharat Bhushan         memset(dbg->arch.bp, 0, sizeof(dbg->arch.bp));
160088365d17SBharat Bhushan         for (n = 0; n < nb_hw_breakpoint + nb_hw_watchpoint; n++) {
160188365d17SBharat Bhushan             switch (hw_debug_points[n].type) {
160288365d17SBharat Bhushan             case GDB_BREAKPOINT_HW:
160388365d17SBharat Bhushan                 dbg->arch.bp[n].type = KVMPPC_DEBUG_BREAKPOINT;
160488365d17SBharat Bhushan                 break;
160588365d17SBharat Bhushan             case GDB_WATCHPOINT_WRITE:
160688365d17SBharat Bhushan                 dbg->arch.bp[n].type = KVMPPC_DEBUG_WATCH_WRITE;
160788365d17SBharat Bhushan                 break;
160888365d17SBharat Bhushan             case GDB_WATCHPOINT_READ:
160988365d17SBharat Bhushan                 dbg->arch.bp[n].type = KVMPPC_DEBUG_WATCH_READ;
161088365d17SBharat Bhushan                 break;
161188365d17SBharat Bhushan             case GDB_WATCHPOINT_ACCESS:
161288365d17SBharat Bhushan                 dbg->arch.bp[n].type = KVMPPC_DEBUG_WATCH_WRITE |
161388365d17SBharat Bhushan                                         KVMPPC_DEBUG_WATCH_READ;
161488365d17SBharat Bhushan                 break;
161588365d17SBharat Bhushan             default:
161688365d17SBharat Bhushan                 cpu_abort(cs, "Unsupported breakpoint type\n");
161788365d17SBharat Bhushan             }
161888365d17SBharat Bhushan             dbg->arch.bp[n].addr = hw_debug_points[n].addr;
161988365d17SBharat Bhushan         }
162088365d17SBharat Bhushan     }
16218a0548f9SBharat Bhushan }
16228a0548f9SBharat Bhushan 
16232cbd1581SFabiano Rosas static int kvm_handle_hw_breakpoint(CPUState *cs,
16242cbd1581SFabiano Rosas                                     struct kvm_debug_exit_arch *arch_info)
16258a0548f9SBharat Bhushan {
16268a0548f9SBharat Bhushan     int handle = 0;
162788365d17SBharat Bhushan     int n;
162888365d17SBharat Bhushan     int flag = 0;
16298a0548f9SBharat Bhushan 
163088365d17SBharat Bhushan     if (nb_hw_breakpoint + nb_hw_watchpoint > 0) {
163188365d17SBharat Bhushan         if (arch_info->status & KVMPPC_DEBUG_BREAKPOINT) {
163288365d17SBharat Bhushan             n = find_hw_breakpoint(arch_info->address, GDB_BREAKPOINT_HW);
163388365d17SBharat Bhushan             if (n >= 0) {
163488365d17SBharat Bhushan                 handle = 1;
163588365d17SBharat Bhushan             }
163688365d17SBharat Bhushan         } else if (arch_info->status & (KVMPPC_DEBUG_WATCH_READ |
163788365d17SBharat Bhushan                                         KVMPPC_DEBUG_WATCH_WRITE)) {
163888365d17SBharat Bhushan             n = find_hw_watchpoint(arch_info->address,  &flag);
163988365d17SBharat Bhushan             if (n >= 0) {
164088365d17SBharat Bhushan                 handle = 1;
164188365d17SBharat Bhushan                 cs->watchpoint_hit = &hw_watchpoint;
164288365d17SBharat Bhushan                 hw_watchpoint.vaddr = hw_debug_points[n].addr;
164388365d17SBharat Bhushan                 hw_watchpoint.flags = flag;
164488365d17SBharat Bhushan             }
164588365d17SBharat Bhushan         }
164688365d17SBharat Bhushan     }
16472cbd1581SFabiano Rosas     return handle;
16482cbd1581SFabiano Rosas }
16492cbd1581SFabiano Rosas 
1650468e3a1aSFabiano Rosas static int kvm_handle_singlestep(void)
1651468e3a1aSFabiano Rosas {
1652468e3a1aSFabiano Rosas     return 1;
1653468e3a1aSFabiano Rosas }
1654468e3a1aSFabiano Rosas 
1655468e3a1aSFabiano Rosas static int kvm_handle_sw_breakpoint(void)
1656468e3a1aSFabiano Rosas {
1657468e3a1aSFabiano Rosas     return 1;
1658468e3a1aSFabiano Rosas }
1659468e3a1aSFabiano Rosas 
16602cbd1581SFabiano Rosas static int kvm_handle_debug(PowerPCCPU *cpu, struct kvm_run *run)
16612cbd1581SFabiano Rosas {
16622cbd1581SFabiano Rosas     CPUState *cs = CPU(cpu);
16632cbd1581SFabiano Rosas     CPUPPCState *env = &cpu->env;
16642cbd1581SFabiano Rosas     struct kvm_debug_exit_arch *arch_info = &run->debug.arch;
16652cbd1581SFabiano Rosas 
16662cbd1581SFabiano Rosas     if (cs->singlestep_enabled) {
1667468e3a1aSFabiano Rosas         return kvm_handle_singlestep();
1668468e3a1aSFabiano Rosas     }
1669468e3a1aSFabiano Rosas 
1670468e3a1aSFabiano Rosas     if (arch_info->status) {
1671468e3a1aSFabiano Rosas         return kvm_handle_hw_breakpoint(cs, arch_info);
1672468e3a1aSFabiano Rosas     }
1673468e3a1aSFabiano Rosas 
1674468e3a1aSFabiano Rosas     if (kvm_find_sw_breakpoint(cs, arch_info->address)) {
1675468e3a1aSFabiano Rosas         return kvm_handle_sw_breakpoint();
1676468e3a1aSFabiano Rosas     }
1677468e3a1aSFabiano Rosas 
1678468e3a1aSFabiano Rosas     /*
1679468e3a1aSFabiano Rosas      * QEMU is not able to handle debug exception, so inject
16808a0548f9SBharat Bhushan      * program exception to guest;
16818a0548f9SBharat Bhushan      * Yes program exception NOT debug exception !!
168288365d17SBharat Bhushan      * When QEMU is using debug resources then debug exception must
168388365d17SBharat Bhushan      * be always set. To achieve this we set MSR_DE and also set
168488365d17SBharat Bhushan      * MSRP_DEP so guest cannot change MSR_DE.
168588365d17SBharat Bhushan      * When emulating debug resource for guest we want guest
168688365d17SBharat Bhushan      * to control MSR_DE (enable/disable debug interrupt on need).
168788365d17SBharat Bhushan      * Supporting both configurations are NOT possible.
168888365d17SBharat Bhushan      * So the result is that we cannot share debug resources
168988365d17SBharat Bhushan      * between QEMU and Guest on BOOKE architecture.
169088365d17SBharat Bhushan      * In the current design QEMU gets the priority over guest,
169188365d17SBharat Bhushan      * this means that if QEMU is using debug resources then guest
169288365d17SBharat Bhushan      * cannot use them;
16938a0548f9SBharat Bhushan      * For software breakpoint QEMU uses a privileged instruction;
16948a0548f9SBharat Bhushan      * So there cannot be any reason that we are here for guest
16958a0548f9SBharat Bhushan      * set debug exception, only possibility is guest executed a
16968a0548f9SBharat Bhushan      * privileged / illegal instruction and that's why we are
16978a0548f9SBharat Bhushan      * injecting a program interrupt.
16988a0548f9SBharat Bhushan      */
16998a0548f9SBharat Bhushan     cpu_synchronize_state(cs);
1700468e3a1aSFabiano Rosas     /*
1701468e3a1aSFabiano Rosas      * env->nip is PC, so increment this by 4 to use
17028a0548f9SBharat Bhushan      * ppc_cpu_do_interrupt(), which set srr0 = env->nip - 4.
17038a0548f9SBharat Bhushan      */
17048a0548f9SBharat Bhushan     env->nip += 4;
17058a0548f9SBharat Bhushan     cs->exception_index = POWERPC_EXCP_PROGRAM;
17068a0548f9SBharat Bhushan     env->error_code = POWERPC_EXCP_INVAL;
17078a0548f9SBharat Bhushan     ppc_cpu_do_interrupt(cs);
17088a0548f9SBharat Bhushan 
1709468e3a1aSFabiano Rosas     return 0;
17108a0548f9SBharat Bhushan }
17118a0548f9SBharat Bhushan 
171220d695a9SAndreas Färber int kvm_arch_handle_exit(CPUState *cs, struct kvm_run *run)
1713d76d1650Saurel32 {
171420d695a9SAndreas Färber     PowerPCCPU *cpu = POWERPC_CPU(cs);
171520d695a9SAndreas Färber     CPUPPCState *env = &cpu->env;
1716bb4ea393SJan Kiszka     int ret;
1717d76d1650Saurel32 
17184b8523eeSJan Kiszka     qemu_mutex_lock_iothread();
17194b8523eeSJan Kiszka 
1720d76d1650Saurel32     switch (run->exit_reason) {
1721d76d1650Saurel32     case KVM_EXIT_DCR:
1722d76d1650Saurel32         if (run->dcr.is_write) {
17238d83cbf1SGreg Kurz             trace_kvm_handle_dcr_write();
1724d76d1650Saurel32             ret = kvmppc_handle_dcr_write(env, run->dcr.dcrn, run->dcr.data);
1725d76d1650Saurel32         } else {
1726228152c2SBoxuan Li             trace_kvm_handle_dcr_read();
1727d76d1650Saurel32             ret = kvmppc_handle_dcr_read(env, run->dcr.dcrn, &run->dcr.data);
1728d76d1650Saurel32         }
1729d76d1650Saurel32         break;
1730d76d1650Saurel32     case KVM_EXIT_HLT:
17318d83cbf1SGreg Kurz         trace_kvm_handle_halt();
1732259186a7SAndreas Färber         ret = kvmppc_handle_halt(cpu);
1733d76d1650Saurel32         break;
1734c6304a4aSDavid Gibson #if defined(TARGET_PPC64)
1735f61b4bedSAlexander Graf     case KVM_EXIT_PAPR_HCALL:
17368d83cbf1SGreg Kurz         trace_kvm_handle_papr_hcall();
173720d695a9SAndreas Färber         run->papr_hcall.ret = spapr_hypercall(cpu,
1738aa100fa4SAndreas Färber                                               run->papr_hcall.nr,
1739f61b4bedSAlexander Graf                                               run->papr_hcall.args);
174078e8fde2SDavid Gibson         ret = 0;
1741f61b4bedSAlexander Graf         break;
1742f61b4bedSAlexander Graf #endif
17435b95b8b9SAlexander Graf     case KVM_EXIT_EPR:
17448d83cbf1SGreg Kurz         trace_kvm_handle_epr();
1745933b19eaSAlexander Graf         run->epr.epr = ldl_phys(cs->as, env->mpic_iack);
17465b95b8b9SAlexander Graf         ret = 0;
17475b95b8b9SAlexander Graf         break;
174831f2cb8fSBharat Bhushan     case KVM_EXIT_WATCHDOG:
17498d83cbf1SGreg Kurz         trace_kvm_handle_watchdog_expiry();
175031f2cb8fSBharat Bhushan         watchdog_perform_action();
175131f2cb8fSBharat Bhushan         ret = 0;
175231f2cb8fSBharat Bhushan         break;
175331f2cb8fSBharat Bhushan 
17548a0548f9SBharat Bhushan     case KVM_EXIT_DEBUG:
17558d83cbf1SGreg Kurz         trace_kvm_handle_debug_exception();
17568a0548f9SBharat Bhushan         if (kvm_handle_debug(cpu, run)) {
17578a0548f9SBharat Bhushan             ret = EXCP_DEBUG;
17588a0548f9SBharat Bhushan             break;
17598a0548f9SBharat Bhushan         }
17608a0548f9SBharat Bhushan         /* re-enter, this exception was guest-internal */
17618a0548f9SBharat Bhushan         ret = 0;
17628a0548f9SBharat Bhushan         break;
17638a0548f9SBharat Bhushan 
176473aaec4aSJan Kiszka     default:
176573aaec4aSJan Kiszka         fprintf(stderr, "KVM: unknown exit reason %d\n", run->exit_reason);
176673aaec4aSJan Kiszka         ret = -1;
176773aaec4aSJan Kiszka         break;
1768d76d1650Saurel32     }
1769d76d1650Saurel32 
17704b8523eeSJan Kiszka     qemu_mutex_unlock_iothread();
1771d76d1650Saurel32     return ret;
1772d76d1650Saurel32 }
1773d76d1650Saurel32 
177431f2cb8fSBharat Bhushan int kvmppc_or_tsr_bits(PowerPCCPU *cpu, uint32_t tsr_bits)
177531f2cb8fSBharat Bhushan {
177631f2cb8fSBharat Bhushan     CPUState *cs = CPU(cpu);
177731f2cb8fSBharat Bhushan     uint32_t bits = tsr_bits;
177831f2cb8fSBharat Bhushan     struct kvm_one_reg reg = {
177931f2cb8fSBharat Bhushan         .id = KVM_REG_PPC_OR_TSR,
178031f2cb8fSBharat Bhushan         .addr = (uintptr_t) &bits,
178131f2cb8fSBharat Bhushan     };
178231f2cb8fSBharat Bhushan 
178331f2cb8fSBharat Bhushan     return kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, &reg);
178431f2cb8fSBharat Bhushan }
178531f2cb8fSBharat Bhushan 
178631f2cb8fSBharat Bhushan int kvmppc_clear_tsr_bits(PowerPCCPU *cpu, uint32_t tsr_bits)
178731f2cb8fSBharat Bhushan {
178831f2cb8fSBharat Bhushan 
178931f2cb8fSBharat Bhushan     CPUState *cs = CPU(cpu);
179031f2cb8fSBharat Bhushan     uint32_t bits = tsr_bits;
179131f2cb8fSBharat Bhushan     struct kvm_one_reg reg = {
179231f2cb8fSBharat Bhushan         .id = KVM_REG_PPC_CLEAR_TSR,
179331f2cb8fSBharat Bhushan         .addr = (uintptr_t) &bits,
179431f2cb8fSBharat Bhushan     };
179531f2cb8fSBharat Bhushan 
179631f2cb8fSBharat Bhushan     return kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, &reg);
179731f2cb8fSBharat Bhushan }
179831f2cb8fSBharat Bhushan 
179931f2cb8fSBharat Bhushan int kvmppc_set_tcr(PowerPCCPU *cpu)
180031f2cb8fSBharat Bhushan {
180131f2cb8fSBharat Bhushan     CPUState *cs = CPU(cpu);
180231f2cb8fSBharat Bhushan     CPUPPCState *env = &cpu->env;
180331f2cb8fSBharat Bhushan     uint32_t tcr = env->spr[SPR_BOOKE_TCR];
180431f2cb8fSBharat Bhushan 
180531f2cb8fSBharat Bhushan     struct kvm_one_reg reg = {
180631f2cb8fSBharat Bhushan         .id = KVM_REG_PPC_TCR,
180731f2cb8fSBharat Bhushan         .addr = (uintptr_t) &tcr,
180831f2cb8fSBharat Bhushan     };
180931f2cb8fSBharat Bhushan 
181031f2cb8fSBharat Bhushan     return kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, &reg);
181131f2cb8fSBharat Bhushan }
181231f2cb8fSBharat Bhushan 
181331f2cb8fSBharat Bhushan int kvmppc_booke_watchdog_enable(PowerPCCPU *cpu)
181431f2cb8fSBharat Bhushan {
181531f2cb8fSBharat Bhushan     CPUState *cs = CPU(cpu);
181631f2cb8fSBharat Bhushan     int ret;
181731f2cb8fSBharat Bhushan 
181831f2cb8fSBharat Bhushan     if (!kvm_enabled()) {
181931f2cb8fSBharat Bhushan         return -1;
182031f2cb8fSBharat Bhushan     }
182131f2cb8fSBharat Bhushan 
182231f2cb8fSBharat Bhushan     if (!cap_ppc_watchdog) {
182331f2cb8fSBharat Bhushan         printf("warning: KVM does not support watchdog");
182431f2cb8fSBharat Bhushan         return -1;
182531f2cb8fSBharat Bhushan     }
182631f2cb8fSBharat Bhushan 
182748add816SCornelia Huck     ret = kvm_vcpu_enable_cap(cs, KVM_CAP_PPC_BOOKE_WATCHDOG, 0);
182831f2cb8fSBharat Bhushan     if (ret < 0) {
182931f2cb8fSBharat Bhushan         fprintf(stderr, "%s: couldn't enable KVM_CAP_PPC_BOOKE_WATCHDOG: %s\n",
183031f2cb8fSBharat Bhushan                 __func__, strerror(-ret));
183131f2cb8fSBharat Bhushan         return ret;
183231f2cb8fSBharat Bhushan     }
183331f2cb8fSBharat Bhushan 
183431f2cb8fSBharat Bhushan     return ret;
183531f2cb8fSBharat Bhushan }
183631f2cb8fSBharat Bhushan 
1837dc333cd6SAlexander Graf static int read_cpuinfo(const char *field, char *value, int len)
1838dc333cd6SAlexander Graf {
1839dc333cd6SAlexander Graf     FILE *f;
1840dc333cd6SAlexander Graf     int ret = -1;
1841dc333cd6SAlexander Graf     int field_len = strlen(field);
1842dc333cd6SAlexander Graf     char line[512];
1843dc333cd6SAlexander Graf 
1844dc333cd6SAlexander Graf     f = fopen("/proc/cpuinfo", "r");
1845dc333cd6SAlexander Graf     if (!f) {
1846dc333cd6SAlexander Graf         return -1;
1847dc333cd6SAlexander Graf     }
1848dc333cd6SAlexander Graf 
1849dc333cd6SAlexander Graf     do {
1850dc333cd6SAlexander Graf         if (!fgets(line, sizeof(line), f)) {
1851dc333cd6SAlexander Graf             break;
1852dc333cd6SAlexander Graf         }
1853dc333cd6SAlexander Graf         if (!strncmp(line, field, field_len)) {
1854ae215068SJim Meyering             pstrcpy(value, len, line);
1855dc333cd6SAlexander Graf             ret = 0;
1856dc333cd6SAlexander Graf             break;
1857dc333cd6SAlexander Graf         }
1858dc333cd6SAlexander Graf     } while (*line);
1859dc333cd6SAlexander Graf 
1860dc333cd6SAlexander Graf     fclose(f);
1861dc333cd6SAlexander Graf 
1862dc333cd6SAlexander Graf     return ret;
1863dc333cd6SAlexander Graf }
1864dc333cd6SAlexander Graf 
1865dc333cd6SAlexander Graf uint32_t kvmppc_get_tbfreq(void)
1866dc333cd6SAlexander Graf {
1867dc333cd6SAlexander Graf     char line[512];
1868dc333cd6SAlexander Graf     char *ns;
186973bcb24dSRutuja Shah     uint32_t retval = NANOSECONDS_PER_SECOND;
1870dc333cd6SAlexander Graf 
1871dc333cd6SAlexander Graf     if (read_cpuinfo("timebase", line, sizeof(line))) {
1872dc333cd6SAlexander Graf         return retval;
1873dc333cd6SAlexander Graf     }
1874dc333cd6SAlexander Graf 
1875c995e942SDavid Gibson     ns = strchr(line, ':');
1876c995e942SDavid Gibson     if (!ns) {
1877dc333cd6SAlexander Graf         return retval;
1878dc333cd6SAlexander Graf     }
1879dc333cd6SAlexander Graf 
1880dc333cd6SAlexander Graf     ns++;
1881dc333cd6SAlexander Graf 
1882f9b8e7f6SShraddha Barke     return atoi(ns);
1883ef951443SNikunj A Dadhania }
1884ef951443SNikunj A Dadhania 
1885ef951443SNikunj A Dadhania bool kvmppc_get_host_serial(char **value)
1886ef951443SNikunj A Dadhania {
1887ef951443SNikunj A Dadhania     return g_file_get_contents("/proc/device-tree/system-id", value, NULL,
1888ef951443SNikunj A Dadhania                                NULL);
1889ef951443SNikunj A Dadhania }
1890ef951443SNikunj A Dadhania 
1891ef951443SNikunj A Dadhania bool kvmppc_get_host_model(char **value)
1892ef951443SNikunj A Dadhania {
1893ef951443SNikunj A Dadhania     return g_file_get_contents("/proc/device-tree/model", value, NULL, NULL);
1894dc333cd6SAlexander Graf }
18954513d923SGleb Natapov 
1896eadaada1SAlexander Graf /* Try to find a device tree node for a CPU with clock-frequency property */
1897eadaada1SAlexander Graf static int kvmppc_find_cpu_dt(char *buf, int buf_len)
1898eadaada1SAlexander Graf {
1899eadaada1SAlexander Graf     struct dirent *dirp;
1900eadaada1SAlexander Graf     DIR *dp;
1901eadaada1SAlexander Graf 
1902c995e942SDavid Gibson     dp = opendir(PROC_DEVTREE_CPU);
1903c995e942SDavid Gibson     if (!dp) {
1904eadaada1SAlexander Graf         printf("Can't open directory " PROC_DEVTREE_CPU "\n");
1905eadaada1SAlexander Graf         return -1;
1906eadaada1SAlexander Graf     }
1907eadaada1SAlexander Graf 
1908eadaada1SAlexander Graf     buf[0] = '\0';
1909eadaada1SAlexander Graf     while ((dirp = readdir(dp)) != NULL) {
1910eadaada1SAlexander Graf         FILE *f;
1911eadaada1SAlexander Graf         snprintf(buf, buf_len, "%s%s/clock-frequency", PROC_DEVTREE_CPU,
1912eadaada1SAlexander Graf                  dirp->d_name);
1913eadaada1SAlexander Graf         f = fopen(buf, "r");
1914eadaada1SAlexander Graf         if (f) {
1915eadaada1SAlexander Graf             snprintf(buf, buf_len, "%s%s", PROC_DEVTREE_CPU, dirp->d_name);
1916eadaada1SAlexander Graf             fclose(f);
1917eadaada1SAlexander Graf             break;
1918eadaada1SAlexander Graf         }
1919eadaada1SAlexander Graf         buf[0] = '\0';
1920eadaada1SAlexander Graf     }
1921eadaada1SAlexander Graf     closedir(dp);
1922eadaada1SAlexander Graf     if (buf[0] == '\0') {
1923eadaada1SAlexander Graf         printf("Unknown host!\n");
1924eadaada1SAlexander Graf         return -1;
1925eadaada1SAlexander Graf     }
1926eadaada1SAlexander Graf 
1927eadaada1SAlexander Graf     return 0;
1928eadaada1SAlexander Graf }
1929eadaada1SAlexander Graf 
19307d94a30bSSukadev Bhattiprolu static uint64_t kvmppc_read_int_dt(const char *filename)
1931eadaada1SAlexander Graf {
19329bc884b7SDavid Gibson     union {
19339bc884b7SDavid Gibson         uint32_t v32;
19349bc884b7SDavid Gibson         uint64_t v64;
19359bc884b7SDavid Gibson     } u;
1936eadaada1SAlexander Graf     FILE *f;
1937eadaada1SAlexander Graf     int len;
1938eadaada1SAlexander Graf 
19397d94a30bSSukadev Bhattiprolu     f = fopen(filename, "rb");
1940eadaada1SAlexander Graf     if (!f) {
1941eadaada1SAlexander Graf         return -1;
1942eadaada1SAlexander Graf     }
1943eadaada1SAlexander Graf 
19449bc884b7SDavid Gibson     len = fread(&u, 1, sizeof(u), f);
1945eadaada1SAlexander Graf     fclose(f);
1946eadaada1SAlexander Graf     switch (len) {
19479bc884b7SDavid Gibson     case 4:
19489bc884b7SDavid Gibson         /* property is a 32-bit quantity */
19499bc884b7SDavid Gibson         return be32_to_cpu(u.v32);
19509bc884b7SDavid Gibson     case 8:
19519bc884b7SDavid Gibson         return be64_to_cpu(u.v64);
1952eadaada1SAlexander Graf     }
1953eadaada1SAlexander Graf 
1954eadaada1SAlexander Graf     return 0;
1955eadaada1SAlexander Graf }
1956eadaada1SAlexander Graf 
1957c995e942SDavid Gibson /*
1958c995e942SDavid Gibson  * Read a CPU node property from the host device tree that's a single
19597d94a30bSSukadev Bhattiprolu  * integer (32-bit or 64-bit).  Returns 0 if anything goes wrong
1960c995e942SDavid Gibson  * (can't find or open the property, or doesn't understand the format)
1961c995e942SDavid Gibson  */
19627d94a30bSSukadev Bhattiprolu static uint64_t kvmppc_read_int_cpu_dt(const char *propname)
19637d94a30bSSukadev Bhattiprolu {
19647d94a30bSSukadev Bhattiprolu     char buf[PATH_MAX], *tmp;
19657d94a30bSSukadev Bhattiprolu     uint64_t val;
19667d94a30bSSukadev Bhattiprolu 
19677d94a30bSSukadev Bhattiprolu     if (kvmppc_find_cpu_dt(buf, sizeof(buf))) {
19687d94a30bSSukadev Bhattiprolu         return -1;
19697d94a30bSSukadev Bhattiprolu     }
19707d94a30bSSukadev Bhattiprolu 
19717d94a30bSSukadev Bhattiprolu     tmp = g_strdup_printf("%s/%s", buf, propname);
19727d94a30bSSukadev Bhattiprolu     val = kvmppc_read_int_dt(tmp);
19737d94a30bSSukadev Bhattiprolu     g_free(tmp);
19747d94a30bSSukadev Bhattiprolu 
19757d94a30bSSukadev Bhattiprolu     return val;
19767d94a30bSSukadev Bhattiprolu }
19777d94a30bSSukadev Bhattiprolu 
19789bc884b7SDavid Gibson uint64_t kvmppc_get_clockfreq(void)
19799bc884b7SDavid Gibson {
19809bc884b7SDavid Gibson     return kvmppc_read_int_cpu_dt("clock-frequency");
19819bc884b7SDavid Gibson }
19829bc884b7SDavid Gibson 
19837d050527SSuraj Jitindar Singh static int kvmppc_get_dec_bits(void)
19847d050527SSuraj Jitindar Singh {
19857d050527SSuraj Jitindar Singh     int nr_bits = kvmppc_read_int_cpu_dt("ibm,dec-bits");
19867d050527SSuraj Jitindar Singh 
19877d050527SSuraj Jitindar Singh     if (nr_bits > 0) {
19887d050527SSuraj Jitindar Singh         return nr_bits;
19897d050527SSuraj Jitindar Singh     }
19907d050527SSuraj Jitindar Singh     return 0;
19917d050527SSuraj Jitindar Singh }
19927d050527SSuraj Jitindar Singh 
19931a61a9aeSStuart Yoder static int kvmppc_get_pvinfo(CPUPPCState *env, struct kvm_ppc_pvinfo *pvinfo)
199445024f09SAlexander Graf {
1995*db70b311SRichard Henderson     CPUState *cs = env_cpu(env);
199645024f09SAlexander Graf 
19976fd33a75SAlexander Graf     if (kvm_vm_check_extension(cs->kvm_state, KVM_CAP_PPC_GET_PVINFO) &&
19981a61a9aeSStuart Yoder         !kvm_vm_ioctl(cs->kvm_state, KVM_PPC_GET_PVINFO, pvinfo)) {
19991a61a9aeSStuart Yoder         return 0;
20001a61a9aeSStuart Yoder     }
200145024f09SAlexander Graf 
20021a61a9aeSStuart Yoder     return 1;
20031a61a9aeSStuart Yoder }
20041a61a9aeSStuart Yoder 
20051a61a9aeSStuart Yoder int kvmppc_get_hasidle(CPUPPCState *env)
20061a61a9aeSStuart Yoder {
20071a61a9aeSStuart Yoder     struct kvm_ppc_pvinfo pvinfo;
20081a61a9aeSStuart Yoder 
20091a61a9aeSStuart Yoder     if (!kvmppc_get_pvinfo(env, &pvinfo) &&
20101a61a9aeSStuart Yoder         (pvinfo.flags & KVM_PPC_PVINFO_FLAGS_EV_IDLE)) {
20111a61a9aeSStuart Yoder         return 1;
20121a61a9aeSStuart Yoder     }
20131a61a9aeSStuart Yoder 
20141a61a9aeSStuart Yoder     return 0;
20151a61a9aeSStuart Yoder }
20161a61a9aeSStuart Yoder 
20171a61a9aeSStuart Yoder int kvmppc_get_hypercall(CPUPPCState *env, uint8_t *buf, int buf_len)
20181a61a9aeSStuart Yoder {
20191a61a9aeSStuart Yoder     uint32_t *hc = (uint32_t *)buf;
20201a61a9aeSStuart Yoder     struct kvm_ppc_pvinfo pvinfo;
20211a61a9aeSStuart Yoder 
20221a61a9aeSStuart Yoder     if (!kvmppc_get_pvinfo(env, &pvinfo)) {
20231a61a9aeSStuart Yoder         memcpy(buf, pvinfo.hcall, buf_len);
202445024f09SAlexander Graf         return 0;
202545024f09SAlexander Graf     }
202645024f09SAlexander Graf 
202745024f09SAlexander Graf     /*
2028d13fc32eSAlexander Graf      * Fallback to always fail hypercalls regardless of endianness:
202945024f09SAlexander Graf      *
2030d13fc32eSAlexander Graf      *     tdi 0,r0,72 (becomes b .+8 in wrong endian, nop in good endian)
203145024f09SAlexander Graf      *     li r3, -1
2032d13fc32eSAlexander Graf      *     b .+8       (becomes nop in wrong endian)
2033d13fc32eSAlexander Graf      *     bswap32(li r3, -1)
203445024f09SAlexander Graf      */
203545024f09SAlexander Graf 
2036d13fc32eSAlexander Graf     hc[0] = cpu_to_be32(0x08000048);
2037d13fc32eSAlexander Graf     hc[1] = cpu_to_be32(0x3860ffff);
2038d13fc32eSAlexander Graf     hc[2] = cpu_to_be32(0x48000008);
2039d13fc32eSAlexander Graf     hc[3] = cpu_to_be32(bswap32(0x3860ffff));
204045024f09SAlexander Graf 
20410ddbd053SAlexey Kardashevskiy     return 1;
204245024f09SAlexander Graf }
204345024f09SAlexander Graf 
2044026bfd89SDavid Gibson static inline int kvmppc_enable_hcall(KVMState *s, target_ulong hcall)
2045026bfd89SDavid Gibson {
2046026bfd89SDavid Gibson     return kvm_vm_enable_cap(s, KVM_CAP_PPC_ENABLE_HCALL, 0, hcall, 1);
2047026bfd89SDavid Gibson }
2048026bfd89SDavid Gibson 
2049026bfd89SDavid Gibson void kvmppc_enable_logical_ci_hcalls(void)
2050026bfd89SDavid Gibson {
2051026bfd89SDavid Gibson     /*
2052026bfd89SDavid Gibson      * FIXME: it would be nice if we could detect the cases where
2053026bfd89SDavid Gibson      * we're using a device which requires the in kernel
2054026bfd89SDavid Gibson      * implementation of these hcalls, but the kernel lacks them and
2055026bfd89SDavid Gibson      * produce a warning.
2056026bfd89SDavid Gibson      */
2057026bfd89SDavid Gibson     kvmppc_enable_hcall(kvm_state, H_LOGICAL_CI_LOAD);
2058026bfd89SDavid Gibson     kvmppc_enable_hcall(kvm_state, H_LOGICAL_CI_STORE);
2059026bfd89SDavid Gibson }
2060026bfd89SDavid Gibson 
2061ef9971ddSAlexey Kardashevskiy void kvmppc_enable_set_mode_hcall(void)
2062ef9971ddSAlexey Kardashevskiy {
2063ef9971ddSAlexey Kardashevskiy     kvmppc_enable_hcall(kvm_state, H_SET_MODE);
2064ef9971ddSAlexey Kardashevskiy }
2065ef9971ddSAlexey Kardashevskiy 
20665145ad4fSNathan Whitehorn void kvmppc_enable_clear_ref_mod_hcalls(void)
20675145ad4fSNathan Whitehorn {
20685145ad4fSNathan Whitehorn     kvmppc_enable_hcall(kvm_state, H_CLEAR_REF);
20695145ad4fSNathan Whitehorn     kvmppc_enable_hcall(kvm_state, H_CLEAR_MOD);
20705145ad4fSNathan Whitehorn }
20715145ad4fSNathan Whitehorn 
207268f9f708SSuraj Jitindar Singh void kvmppc_enable_h_page_init(void)
207368f9f708SSuraj Jitindar Singh {
207468f9f708SSuraj Jitindar Singh     kvmppc_enable_hcall(kvm_state, H_PAGE_INIT);
207568f9f708SSuraj Jitindar Singh }
207668f9f708SSuraj Jitindar Singh 
20771bc22652SAndreas Färber void kvmppc_set_papr(PowerPCCPU *cpu)
2078f61b4bedSAlexander Graf {
20791bc22652SAndreas Färber     CPUState *cs = CPU(cpu);
2080f61b4bedSAlexander Graf     int ret;
2081f61b4bedSAlexander Graf 
2082da20aed1SDavid Gibson     if (!kvm_enabled()) {
2083da20aed1SDavid Gibson         return;
2084da20aed1SDavid Gibson     }
2085da20aed1SDavid Gibson 
208648add816SCornelia Huck     ret = kvm_vcpu_enable_cap(cs, KVM_CAP_PPC_PAPR, 0);
2087f61b4bedSAlexander Graf     if (ret) {
2088072ed5f2SThomas Huth         error_report("This vCPU type or KVM version does not support PAPR");
2089072ed5f2SThomas Huth         exit(1);
2090f61b4bedSAlexander Graf     }
20919b00ea49SDavid Gibson 
2092c995e942SDavid Gibson     /*
2093c995e942SDavid Gibson      * Update the capability flag so we sync the right information
2094c995e942SDavid Gibson      * with kvm
2095c995e942SDavid Gibson      */
20969b00ea49SDavid Gibson     cap_papr = 1;
2097f1af19d7SDavid Gibson }
2098f61b4bedSAlexander Graf 
2099d6e166c0SDavid Gibson int kvmppc_set_compat(PowerPCCPU *cpu, uint32_t compat_pvr)
21006db5bb0fSAlexey Kardashevskiy {
2101d6e166c0SDavid Gibson     return kvm_set_one_reg(CPU(cpu), KVM_REG_PPC_ARCH_COMPAT, &compat_pvr);
21026db5bb0fSAlexey Kardashevskiy }
21036db5bb0fSAlexey Kardashevskiy 
21045b95b8b9SAlexander Graf void kvmppc_set_mpic_proxy(PowerPCCPU *cpu, int mpic_proxy)
21055b95b8b9SAlexander Graf {
21065b95b8b9SAlexander Graf     CPUState *cs = CPU(cpu);
21075b95b8b9SAlexander Graf     int ret;
21085b95b8b9SAlexander Graf 
210948add816SCornelia Huck     ret = kvm_vcpu_enable_cap(cs, KVM_CAP_PPC_EPR, 0, mpic_proxy);
21105b95b8b9SAlexander Graf     if (ret && mpic_proxy) {
2111072ed5f2SThomas Huth         error_report("This KVM version does not support EPR");
2112072ed5f2SThomas Huth         exit(1);
21135b95b8b9SAlexander Graf     }
21145b95b8b9SAlexander Graf }
21155b95b8b9SAlexander Graf 
2116e97c3636SDavid Gibson int kvmppc_smt_threads(void)
2117e97c3636SDavid Gibson {
2118e97c3636SDavid Gibson     return cap_ppc_smt ? cap_ppc_smt : 1;
2119e97c3636SDavid Gibson }
2120e97c3636SDavid Gibson 
2121fa98fbfcSSam Bobroff int kvmppc_set_smt_threads(int smt)
2122fa98fbfcSSam Bobroff {
2123fa98fbfcSSam Bobroff     int ret;
2124fa98fbfcSSam Bobroff 
2125fa98fbfcSSam Bobroff     ret = kvm_vm_enable_cap(kvm_state, KVM_CAP_PPC_SMT, 0, smt, 0);
2126fa98fbfcSSam Bobroff     if (!ret) {
2127fa98fbfcSSam Bobroff         cap_ppc_smt = smt;
2128fa98fbfcSSam Bobroff     }
2129fa98fbfcSSam Bobroff     return ret;
2130fa98fbfcSSam Bobroff }
2131fa98fbfcSSam Bobroff 
2132fa98fbfcSSam Bobroff void kvmppc_hint_smt_possible(Error **errp)
2133fa98fbfcSSam Bobroff {
2134fa98fbfcSSam Bobroff     int i;
2135fa98fbfcSSam Bobroff     GString *g;
2136fa98fbfcSSam Bobroff     char *s;
2137fa98fbfcSSam Bobroff 
2138fa98fbfcSSam Bobroff     assert(kvm_enabled());
2139fa98fbfcSSam Bobroff     if (cap_ppc_smt_possible) {
2140fa98fbfcSSam Bobroff         g = g_string_new("Available VSMT modes:");
2141fa98fbfcSSam Bobroff         for (i = 63; i >= 0; i--) {
2142fa98fbfcSSam Bobroff             if ((1UL << i) & cap_ppc_smt_possible) {
2143fa98fbfcSSam Bobroff                 g_string_append_printf(g, " %lu", (1UL << i));
2144fa98fbfcSSam Bobroff             }
2145fa98fbfcSSam Bobroff         }
2146fa98fbfcSSam Bobroff         s = g_string_free(g, false);
2147fa98fbfcSSam Bobroff         error_append_hint(errp, "%s.\n", s);
2148fa98fbfcSSam Bobroff         g_free(s);
2149fa98fbfcSSam Bobroff     } else {
2150fa98fbfcSSam Bobroff         error_append_hint(errp,
2151fa98fbfcSSam Bobroff                           "This KVM seems to be too old to support VSMT.\n");
2152fa98fbfcSSam Bobroff     }
2153fa98fbfcSSam Bobroff }
2154fa98fbfcSSam Bobroff 
2155fa98fbfcSSam Bobroff 
21567f763a5dSDavid Gibson #ifdef TARGET_PPC64
21577f763a5dSDavid Gibson uint64_t kvmppc_rma_size(uint64_t current_size, unsigned int hash_shift)
21587f763a5dSDavid Gibson {
2159f36951c1SDavid Gibson     struct kvm_ppc_smmu_info info;
2160f36951c1SDavid Gibson     long rampagesize, best_page_shift;
2161f36951c1SDavid Gibson     int i;
2162f36951c1SDavid Gibson 
2163c995e942SDavid Gibson     /*
2164c995e942SDavid Gibson      * Find the largest hardware supported page size that's less than
2165c995e942SDavid Gibson      * or equal to the (logical) backing page size of guest RAM
2166c995e942SDavid Gibson      */
2167ab256960SGreg Kurz     kvm_get_smmu_info(&info, &error_fatal);
2168905b7ee4SDavid Hildenbrand     rampagesize = qemu_minrampagesize();
2169f36951c1SDavid Gibson     best_page_shift = 0;
2170f36951c1SDavid Gibson 
2171f36951c1SDavid Gibson     for (i = 0; i < KVM_PPC_PAGE_SIZES_MAX_SZ; i++) {
2172f36951c1SDavid Gibson         struct kvm_ppc_one_seg_page_size *sps = &info.sps[i];
2173f36951c1SDavid Gibson 
2174f36951c1SDavid Gibson         if (!sps->page_shift) {
2175f36951c1SDavid Gibson             continue;
2176f36951c1SDavid Gibson         }
2177f36951c1SDavid Gibson 
2178f36951c1SDavid Gibson         if ((sps->page_shift > best_page_shift)
2179f36951c1SDavid Gibson             && ((1UL << sps->page_shift) <= rampagesize)) {
2180f36951c1SDavid Gibson             best_page_shift = sps->page_shift;
2181f36951c1SDavid Gibson         }
2182f36951c1SDavid Gibson     }
2183f36951c1SDavid Gibson 
21847f763a5dSDavid Gibson     return MIN(current_size,
2185f36951c1SDavid Gibson                1ULL << (best_page_shift + hash_shift - 7));
21867f763a5dSDavid Gibson }
21877f763a5dSDavid Gibson #endif
21887f763a5dSDavid Gibson 
2189da95324eSAlexey Kardashevskiy bool kvmppc_spapr_use_multitce(void)
2190da95324eSAlexey Kardashevskiy {
2191da95324eSAlexey Kardashevskiy     return cap_spapr_multitce;
2192da95324eSAlexey Kardashevskiy }
2193da95324eSAlexey Kardashevskiy 
21943dc410aeSAlexey Kardashevskiy int kvmppc_spapr_enable_inkernel_multitce(void)
21953dc410aeSAlexey Kardashevskiy {
21963dc410aeSAlexey Kardashevskiy     int ret;
21973dc410aeSAlexey Kardashevskiy 
21983dc410aeSAlexey Kardashevskiy     ret = kvm_vm_enable_cap(kvm_state, KVM_CAP_PPC_ENABLE_HCALL, 0,
21993dc410aeSAlexey Kardashevskiy                             H_PUT_TCE_INDIRECT, 1);
22003dc410aeSAlexey Kardashevskiy     if (!ret) {
22013dc410aeSAlexey Kardashevskiy         ret = kvm_vm_enable_cap(kvm_state, KVM_CAP_PPC_ENABLE_HCALL, 0,
22023dc410aeSAlexey Kardashevskiy                                 H_STUFF_TCE, 1);
22033dc410aeSAlexey Kardashevskiy     }
22043dc410aeSAlexey Kardashevskiy 
22053dc410aeSAlexey Kardashevskiy     return ret;
22063dc410aeSAlexey Kardashevskiy }
22073dc410aeSAlexey Kardashevskiy 
2208d6ee2a7cSAlexey Kardashevskiy void *kvmppc_create_spapr_tce(uint32_t liobn, uint32_t page_shift,
2209d6ee2a7cSAlexey Kardashevskiy                               uint64_t bus_offset, uint32_t nb_table,
2210d6ee2a7cSAlexey Kardashevskiy                               int *pfd, bool need_vfio)
22110f5cb298SDavid Gibson {
22120f5cb298SDavid Gibson     long len;
22130f5cb298SDavid Gibson     int fd;
22140f5cb298SDavid Gibson     void *table;
22150f5cb298SDavid Gibson 
2216c995e942SDavid Gibson     /*
2217c995e942SDavid Gibson      * Must set fd to -1 so we don't try to munmap when called for
2218b5aec396SDavid Gibson      * destroying the table, which the upper layers -will- do
2219b5aec396SDavid Gibson      */
2220b5aec396SDavid Gibson     *pfd = -1;
22216a81dd17SDavid Gibson     if (!cap_spapr_tce || (need_vfio && !cap_spapr_vfio)) {
22220f5cb298SDavid Gibson         return NULL;
22230f5cb298SDavid Gibson     }
22240f5cb298SDavid Gibson 
2225d6ee2a7cSAlexey Kardashevskiy     if (cap_spapr_tce_64) {
2226d6ee2a7cSAlexey Kardashevskiy         struct kvm_create_spapr_tce_64 args = {
2227d6ee2a7cSAlexey Kardashevskiy             .liobn = liobn,
2228d6ee2a7cSAlexey Kardashevskiy             .page_shift = page_shift,
2229d6ee2a7cSAlexey Kardashevskiy             .offset = bus_offset >> page_shift,
2230d6ee2a7cSAlexey Kardashevskiy             .size = nb_table,
2231d6ee2a7cSAlexey Kardashevskiy             .flags = 0
2232d6ee2a7cSAlexey Kardashevskiy         };
2233d6ee2a7cSAlexey Kardashevskiy         fd = kvm_vm_ioctl(kvm_state, KVM_CREATE_SPAPR_TCE_64, &args);
2234d6ee2a7cSAlexey Kardashevskiy         if (fd < 0) {
2235d6ee2a7cSAlexey Kardashevskiy             fprintf(stderr,
2236d6ee2a7cSAlexey Kardashevskiy                     "KVM: Failed to create TCE64 table for liobn 0x%x\n",
2237d6ee2a7cSAlexey Kardashevskiy                     liobn);
2238d6ee2a7cSAlexey Kardashevskiy             return NULL;
2239d6ee2a7cSAlexey Kardashevskiy         }
2240d6ee2a7cSAlexey Kardashevskiy     } else if (cap_spapr_tce) {
2241d6ee2a7cSAlexey Kardashevskiy         uint64_t window_size = (uint64_t) nb_table << page_shift;
2242d6ee2a7cSAlexey Kardashevskiy         struct kvm_create_spapr_tce args = {
2243d6ee2a7cSAlexey Kardashevskiy             .liobn = liobn,
2244d6ee2a7cSAlexey Kardashevskiy             .window_size = window_size,
2245d6ee2a7cSAlexey Kardashevskiy         };
2246d6ee2a7cSAlexey Kardashevskiy         if ((window_size != args.window_size) || bus_offset) {
2247d6ee2a7cSAlexey Kardashevskiy             return NULL;
2248d6ee2a7cSAlexey Kardashevskiy         }
22490f5cb298SDavid Gibson         fd = kvm_vm_ioctl(kvm_state, KVM_CREATE_SPAPR_TCE, &args);
22500f5cb298SDavid Gibson         if (fd < 0) {
2251b5aec396SDavid Gibson             fprintf(stderr, "KVM: Failed to create TCE table for liobn 0x%x\n",
2252b5aec396SDavid Gibson                     liobn);
22530f5cb298SDavid Gibson             return NULL;
22540f5cb298SDavid Gibson         }
2255d6ee2a7cSAlexey Kardashevskiy     } else {
2256d6ee2a7cSAlexey Kardashevskiy         return NULL;
2257d6ee2a7cSAlexey Kardashevskiy     }
22580f5cb298SDavid Gibson 
2259d6ee2a7cSAlexey Kardashevskiy     len = nb_table * sizeof(uint64_t);
22600f5cb298SDavid Gibson     /* FIXME: round this up to page size */
22610f5cb298SDavid Gibson 
226274b41e56SDavid Gibson     table = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
22630f5cb298SDavid Gibson     if (table == MAP_FAILED) {
2264b5aec396SDavid Gibson         fprintf(stderr, "KVM: Failed to map TCE table for liobn 0x%x\n",
2265b5aec396SDavid Gibson                 liobn);
22660f5cb298SDavid Gibson         close(fd);
22670f5cb298SDavid Gibson         return NULL;
22680f5cb298SDavid Gibson     }
22690f5cb298SDavid Gibson 
22700f5cb298SDavid Gibson     *pfd = fd;
22710f5cb298SDavid Gibson     return table;
22720f5cb298SDavid Gibson }
22730f5cb298SDavid Gibson 
2274523e7b8aSAlexey Kardashevskiy int kvmppc_remove_spapr_tce(void *table, int fd, uint32_t nb_table)
22750f5cb298SDavid Gibson {
22760f5cb298SDavid Gibson     long len;
22770f5cb298SDavid Gibson 
22780f5cb298SDavid Gibson     if (fd < 0) {
22790f5cb298SDavid Gibson         return -1;
22800f5cb298SDavid Gibson     }
22810f5cb298SDavid Gibson 
2282523e7b8aSAlexey Kardashevskiy     len = nb_table * sizeof(uint64_t);
22830f5cb298SDavid Gibson     if ((munmap(table, len) < 0) ||
22840f5cb298SDavid Gibson         (close(fd) < 0)) {
2285b5aec396SDavid Gibson         fprintf(stderr, "KVM: Unexpected error removing TCE table: %s",
2286b5aec396SDavid Gibson                 strerror(errno));
22870f5cb298SDavid Gibson         /* Leak the table */
22880f5cb298SDavid Gibson     }
22890f5cb298SDavid Gibson 
22900f5cb298SDavid Gibson     return 0;
22910f5cb298SDavid Gibson }
22920f5cb298SDavid Gibson 
22937f763a5dSDavid Gibson int kvmppc_reset_htab(int shift_hint)
22947f763a5dSDavid Gibson {
22957f763a5dSDavid Gibson     uint32_t shift = shift_hint;
22967f763a5dSDavid Gibson 
2297ace9a2cbSDavid Gibson     if (!kvm_enabled()) {
2298ace9a2cbSDavid Gibson         /* Full emulation, tell caller to allocate htab itself */
2299ace9a2cbSDavid Gibson         return 0;
2300ace9a2cbSDavid Gibson     }
23016977afdaSGreg Kurz     if (kvm_vm_check_extension(kvm_state, KVM_CAP_PPC_ALLOC_HTAB)) {
23027f763a5dSDavid Gibson         int ret;
23037f763a5dSDavid Gibson         ret = kvm_vm_ioctl(kvm_state, KVM_PPC_ALLOCATE_HTAB, &shift);
2304ace9a2cbSDavid Gibson         if (ret == -ENOTTY) {
2305c995e942SDavid Gibson             /*
2306c995e942SDavid Gibson              * At least some versions of PR KVM advertise the
2307ace9a2cbSDavid Gibson              * capability, but don't implement the ioctl().  Oops.
2308ace9a2cbSDavid Gibson              * Return 0 so that we allocate the htab in qemu, as is
2309c995e942SDavid Gibson              * correct for PR.
2310c995e942SDavid Gibson              */
2311ace9a2cbSDavid Gibson             return 0;
2312ace9a2cbSDavid Gibson         } else if (ret < 0) {
23137f763a5dSDavid Gibson             return ret;
23147f763a5dSDavid Gibson         }
23157f763a5dSDavid Gibson         return shift;
23167f763a5dSDavid Gibson     }
23177f763a5dSDavid Gibson 
2318c995e942SDavid Gibson     /*
2319c995e942SDavid Gibson      * We have a kernel that predates the htab reset calls.  For PR
2320ace9a2cbSDavid Gibson      * KVM, we need to allocate the htab ourselves, for an HV KVM of
2321c995e942SDavid Gibson      * this era, it has allocated a 16MB fixed size hash table
2322c995e942SDavid Gibson      * already.
2323c995e942SDavid Gibson      */
232496c9cff0SThomas Huth     if (kvmppc_is_pr(kvm_state)) {
2325ace9a2cbSDavid Gibson         /* PR - tell caller to allocate htab */
23267f763a5dSDavid Gibson         return 0;
2327ace9a2cbSDavid Gibson     } else {
2328ace9a2cbSDavid Gibson         /* HV - assume 16MB kernel allocated htab */
2329ace9a2cbSDavid Gibson         return 24;
2330ace9a2cbSDavid Gibson     }
23317f763a5dSDavid Gibson }
23327f763a5dSDavid Gibson 
2333a1e98583SDavid Gibson static inline uint32_t mfpvr(void)
2334a1e98583SDavid Gibson {
2335a1e98583SDavid Gibson     uint32_t pvr;
2336a1e98583SDavid Gibson 
2337a1e98583SDavid Gibson     asm ("mfpvr %0"
2338a1e98583SDavid Gibson          : "=r"(pvr));
2339a1e98583SDavid Gibson     return pvr;
2340a1e98583SDavid Gibson }
2341a1e98583SDavid Gibson 
2342a7342588SDavid Gibson static void alter_insns(uint64_t *word, uint64_t flags, bool on)
2343a7342588SDavid Gibson {
2344a7342588SDavid Gibson     if (on) {
2345a7342588SDavid Gibson         *word |= flags;
2346a7342588SDavid Gibson     } else {
2347a7342588SDavid Gibson         *word &= ~flags;
2348a7342588SDavid Gibson     }
2349a7342588SDavid Gibson }
2350a7342588SDavid Gibson 
23512985b86bSAndreas Färber static void kvmppc_host_cpu_class_init(ObjectClass *oc, void *data)
23522985b86bSAndreas Färber {
23532985b86bSAndreas Färber     PowerPCCPUClass *pcc = POWERPC_CPU_CLASS(oc);
23540cbad81fSDavid Gibson     uint32_t dcache_size = kvmppc_read_int_cpu_dt("d-cache-size");
23550cbad81fSDavid Gibson     uint32_t icache_size = kvmppc_read_int_cpu_dt("i-cache-size");
2356a1e98583SDavid Gibson 
2357cfe34f44SAndreas Färber     /* Now fix up the class with information we can query from the host */
23583bc9ccc0SAlexey Kardashevskiy     pcc->pvr = mfpvr();
2359a7342588SDavid Gibson 
23603f2ca480SDavid Gibson     alter_insns(&pcc->insns_flags, PPC_ALTIVEC,
23613f2ca480SDavid Gibson                 qemu_getauxval(AT_HWCAP) & PPC_FEATURE_HAS_ALTIVEC);
23623f2ca480SDavid Gibson     alter_insns(&pcc->insns_flags2, PPC2_VSX,
23633f2ca480SDavid Gibson                 qemu_getauxval(AT_HWCAP) & PPC_FEATURE_HAS_VSX);
23643f2ca480SDavid Gibson     alter_insns(&pcc->insns_flags2, PPC2_DFP,
23653f2ca480SDavid Gibson                 qemu_getauxval(AT_HWCAP) & PPC_FEATURE_HAS_DFP);
23660cbad81fSDavid Gibson 
23670cbad81fSDavid Gibson     if (dcache_size != -1) {
23680cbad81fSDavid Gibson         pcc->l1_dcache_size = dcache_size;
23690cbad81fSDavid Gibson     }
23700cbad81fSDavid Gibson 
23710cbad81fSDavid Gibson     if (icache_size != -1) {
23720cbad81fSDavid Gibson         pcc->l1_icache_size = icache_size;
23730cbad81fSDavid Gibson     }
2374c64abd1fSSam Bobroff 
2375c64abd1fSSam Bobroff #if defined(TARGET_PPC64)
2376c64abd1fSSam Bobroff     pcc->radix_page_info = kvm_get_radix_page_info();
23775f3066d8SDavid Gibson 
23785f3066d8SDavid Gibson     if ((pcc->pvr & 0xffffff00) == CPU_POWERPC_POWER9_DD1) {
23795f3066d8SDavid Gibson         /*
23805f3066d8SDavid Gibson          * POWER9 DD1 has some bugs which make it not really ISA 3.00
23815f3066d8SDavid Gibson          * compliant.  More importantly, advertising ISA 3.00
23825f3066d8SDavid Gibson          * architected mode may prevent guests from activating
23835f3066d8SDavid Gibson          * necessary DD1 workarounds.
23845f3066d8SDavid Gibson          */
23855f3066d8SDavid Gibson         pcc->pcr_supported &= ~(PCR_COMPAT_3_00 | PCR_COMPAT_2_07
23865f3066d8SDavid Gibson                                 | PCR_COMPAT_2_06 | PCR_COMPAT_2_05);
23875f3066d8SDavid Gibson     }
2388c64abd1fSSam Bobroff #endif /* defined(TARGET_PPC64) */
2389a1e98583SDavid Gibson }
2390a1e98583SDavid Gibson 
23913b961124SStuart Yoder bool kvmppc_has_cap_epr(void)
23923b961124SStuart Yoder {
23933b961124SStuart Yoder     return cap_epr;
23943b961124SStuart Yoder }
23953b961124SStuart Yoder 
239687a91de6SAlexander Graf bool kvmppc_has_cap_fixup_hcalls(void)
239787a91de6SAlexander Graf {
239887a91de6SAlexander Graf     return cap_fixup_hcalls;
239987a91de6SAlexander Graf }
240087a91de6SAlexander Graf 
2401bac3bf28SThomas Huth bool kvmppc_has_cap_htm(void)
2402bac3bf28SThomas Huth {
2403bac3bf28SThomas Huth     return cap_htm;
2404bac3bf28SThomas Huth }
2405bac3bf28SThomas Huth 
2406cf1c4cceSSam Bobroff bool kvmppc_has_cap_mmu_radix(void)
2407cf1c4cceSSam Bobroff {
2408cf1c4cceSSam Bobroff     return cap_mmu_radix;
2409cf1c4cceSSam Bobroff }
2410cf1c4cceSSam Bobroff 
2411cf1c4cceSSam Bobroff bool kvmppc_has_cap_mmu_hash_v3(void)
2412cf1c4cceSSam Bobroff {
2413cf1c4cceSSam Bobroff     return cap_mmu_hash_v3;
2414cf1c4cceSSam Bobroff }
2415cf1c4cceSSam Bobroff 
2416072f416aSSuraj Jitindar Singh static bool kvmppc_power8_host(void)
2417072f416aSSuraj Jitindar Singh {
2418072f416aSSuraj Jitindar Singh     bool ret = false;
2419072f416aSSuraj Jitindar Singh #ifdef TARGET_PPC64
2420072f416aSSuraj Jitindar Singh     {
2421072f416aSSuraj Jitindar Singh         uint32_t base_pvr = CPU_POWERPC_POWER_SERVER_MASK & mfpvr();
2422072f416aSSuraj Jitindar Singh         ret = (base_pvr == CPU_POWERPC_POWER8E_BASE) ||
2423072f416aSSuraj Jitindar Singh               (base_pvr == CPU_POWERPC_POWER8NVL_BASE) ||
2424072f416aSSuraj Jitindar Singh               (base_pvr == CPU_POWERPC_POWER8_BASE);
2425072f416aSSuraj Jitindar Singh     }
2426072f416aSSuraj Jitindar Singh #endif /* TARGET_PPC64 */
2427072f416aSSuraj Jitindar Singh     return ret;
2428072f416aSSuraj Jitindar Singh }
2429072f416aSSuraj Jitindar Singh 
24308fea7044SSuraj Jitindar Singh static int parse_cap_ppc_safe_cache(struct kvm_ppc_cpu_char c)
24318fea7044SSuraj Jitindar Singh {
2432072f416aSSuraj Jitindar Singh     bool l1d_thread_priv_req = !kvmppc_power8_host();
2433072f416aSSuraj Jitindar Singh 
24348fea7044SSuraj Jitindar Singh     if (~c.behaviour & c.behaviour_mask & H_CPU_BEHAV_L1D_FLUSH_PR) {
24358fea7044SSuraj Jitindar Singh         return 2;
2436072f416aSSuraj Jitindar Singh     } else if ((!l1d_thread_priv_req ||
2437072f416aSSuraj Jitindar Singh                 c.character & c.character_mask & H_CPU_CHAR_L1D_THREAD_PRIV) &&
24388fea7044SSuraj Jitindar Singh                (c.character & c.character_mask
24398fea7044SSuraj Jitindar Singh                 & (H_CPU_CHAR_L1D_FLUSH_ORI30 | H_CPU_CHAR_L1D_FLUSH_TRIG2))) {
24408fea7044SSuraj Jitindar Singh         return 1;
24418fea7044SSuraj Jitindar Singh     }
24428fea7044SSuraj Jitindar Singh 
24438fea7044SSuraj Jitindar Singh     return 0;
24448fea7044SSuraj Jitindar Singh }
24458fea7044SSuraj Jitindar Singh 
24468fea7044SSuraj Jitindar Singh static int parse_cap_ppc_safe_bounds_check(struct kvm_ppc_cpu_char c)
24478fea7044SSuraj Jitindar Singh {
24488fea7044SSuraj Jitindar Singh     if (~c.behaviour & c.behaviour_mask & H_CPU_BEHAV_BNDS_CHK_SPEC_BAR) {
24498fea7044SSuraj Jitindar Singh         return 2;
24508fea7044SSuraj Jitindar Singh     } else if (c.character & c.character_mask & H_CPU_CHAR_SPEC_BAR_ORI31) {
24518fea7044SSuraj Jitindar Singh         return 1;
24528fea7044SSuraj Jitindar Singh     }
24538fea7044SSuraj Jitindar Singh 
24548fea7044SSuraj Jitindar Singh     return 0;
24558fea7044SSuraj Jitindar Singh }
24568fea7044SSuraj Jitindar Singh 
24578fea7044SSuraj Jitindar Singh static int parse_cap_ppc_safe_indirect_branch(struct kvm_ppc_cpu_char c)
24588fea7044SSuraj Jitindar Singh {
2459399b2896SSuraj Jitindar Singh     if ((~c.behaviour & c.behaviour_mask & H_CPU_BEHAV_FLUSH_COUNT_CACHE) &&
2460399b2896SSuraj Jitindar Singh         (~c.character & c.character_mask & H_CPU_CHAR_CACHE_COUNT_DIS) &&
2461399b2896SSuraj Jitindar Singh         (~c.character & c.character_mask & H_CPU_CHAR_BCCTRL_SERIALISED)) {
2462399b2896SSuraj Jitindar Singh         return SPAPR_CAP_FIXED_NA;
2463399b2896SSuraj Jitindar Singh     } else if (c.behaviour & c.behaviour_mask & H_CPU_BEHAV_FLUSH_COUNT_CACHE) {
2464399b2896SSuraj Jitindar Singh         return SPAPR_CAP_WORKAROUND;
2465399b2896SSuraj Jitindar Singh     } else if (c.character & c.character_mask & H_CPU_CHAR_CACHE_COUNT_DIS) {
24668fea7044SSuraj Jitindar Singh         return  SPAPR_CAP_FIXED_CCD;
24678fea7044SSuraj Jitindar Singh     } else if (c.character & c.character_mask & H_CPU_CHAR_BCCTRL_SERIALISED) {
24688fea7044SSuraj Jitindar Singh         return SPAPR_CAP_FIXED_IBS;
24698fea7044SSuraj Jitindar Singh     }
24708fea7044SSuraj Jitindar Singh 
24718fea7044SSuraj Jitindar Singh     return 0;
24728fea7044SSuraj Jitindar Singh }
24738fea7044SSuraj Jitindar Singh 
24748ff43ee4SSuraj Jitindar Singh static int parse_cap_ppc_count_cache_flush_assist(struct kvm_ppc_cpu_char c)
24758ff43ee4SSuraj Jitindar Singh {
24768ff43ee4SSuraj Jitindar Singh     if (c.character & c.character_mask & H_CPU_CHAR_BCCTR_FLUSH_ASSIST) {
24778ff43ee4SSuraj Jitindar Singh         return 1;
24788ff43ee4SSuraj Jitindar Singh     }
24798ff43ee4SSuraj Jitindar Singh     return 0;
24808ff43ee4SSuraj Jitindar Singh }
24818ff43ee4SSuraj Jitindar Singh 
248238afd772SCédric Le Goater bool kvmppc_has_cap_xive(void)
248338afd772SCédric Le Goater {
248438afd772SCédric Le Goater     return cap_xive;
248538afd772SCédric Le Goater }
248638afd772SCédric Le Goater 
24878acc2ae5SSuraj Jitindar Singh static void kvmppc_get_cpu_characteristics(KVMState *s)
24888acc2ae5SSuraj Jitindar Singh {
24898acc2ae5SSuraj Jitindar Singh     struct kvm_ppc_cpu_char c;
24908acc2ae5SSuraj Jitindar Singh     int ret;
24918acc2ae5SSuraj Jitindar Singh 
24928acc2ae5SSuraj Jitindar Singh     /* Assume broken */
24938acc2ae5SSuraj Jitindar Singh     cap_ppc_safe_cache = 0;
24948acc2ae5SSuraj Jitindar Singh     cap_ppc_safe_bounds_check = 0;
24958acc2ae5SSuraj Jitindar Singh     cap_ppc_safe_indirect_branch = 0;
24968acc2ae5SSuraj Jitindar Singh 
24978acc2ae5SSuraj Jitindar Singh     ret = kvm_vm_check_extension(s, KVM_CAP_PPC_GET_CPU_CHAR);
24988acc2ae5SSuraj Jitindar Singh     if (!ret) {
24998acc2ae5SSuraj Jitindar Singh         return;
25008acc2ae5SSuraj Jitindar Singh     }
25018acc2ae5SSuraj Jitindar Singh     ret = kvm_vm_ioctl(s, KVM_PPC_GET_CPU_CHAR, &c);
25028acc2ae5SSuraj Jitindar Singh     if (ret < 0) {
25038acc2ae5SSuraj Jitindar Singh         return;
25048acc2ae5SSuraj Jitindar Singh     }
25058fea7044SSuraj Jitindar Singh 
25068fea7044SSuraj Jitindar Singh     cap_ppc_safe_cache = parse_cap_ppc_safe_cache(c);
25078fea7044SSuraj Jitindar Singh     cap_ppc_safe_bounds_check = parse_cap_ppc_safe_bounds_check(c);
25088fea7044SSuraj Jitindar Singh     cap_ppc_safe_indirect_branch = parse_cap_ppc_safe_indirect_branch(c);
25098ff43ee4SSuraj Jitindar Singh     cap_ppc_count_cache_flush_assist =
25108ff43ee4SSuraj Jitindar Singh         parse_cap_ppc_count_cache_flush_assist(c);
25118acc2ae5SSuraj Jitindar Singh }
25128acc2ae5SSuraj Jitindar Singh 
25138acc2ae5SSuraj Jitindar Singh int kvmppc_get_cap_safe_cache(void)
25148acc2ae5SSuraj Jitindar Singh {
25158acc2ae5SSuraj Jitindar Singh     return cap_ppc_safe_cache;
25168acc2ae5SSuraj Jitindar Singh }
25178acc2ae5SSuraj Jitindar Singh 
25188acc2ae5SSuraj Jitindar Singh int kvmppc_get_cap_safe_bounds_check(void)
25198acc2ae5SSuraj Jitindar Singh {
25208acc2ae5SSuraj Jitindar Singh     return cap_ppc_safe_bounds_check;
25218acc2ae5SSuraj Jitindar Singh }
25228acc2ae5SSuraj Jitindar Singh 
25238acc2ae5SSuraj Jitindar Singh int kvmppc_get_cap_safe_indirect_branch(void)
25248acc2ae5SSuraj Jitindar Singh {
25258acc2ae5SSuraj Jitindar Singh     return cap_ppc_safe_indirect_branch;
25268acc2ae5SSuraj Jitindar Singh }
25278acc2ae5SSuraj Jitindar Singh 
25288ff43ee4SSuraj Jitindar Singh int kvmppc_get_cap_count_cache_flush_assist(void)
25298ff43ee4SSuraj Jitindar Singh {
25308ff43ee4SSuraj Jitindar Singh     return cap_ppc_count_cache_flush_assist;
25318ff43ee4SSuraj Jitindar Singh }
25328ff43ee4SSuraj Jitindar Singh 
2533b9a477b7SSuraj Jitindar Singh bool kvmppc_has_cap_nested_kvm_hv(void)
2534b9a477b7SSuraj Jitindar Singh {
2535b9a477b7SSuraj Jitindar Singh     return !!cap_ppc_nested_kvm_hv;
2536b9a477b7SSuraj Jitindar Singh }
2537b9a477b7SSuraj Jitindar Singh 
2538b9a477b7SSuraj Jitindar Singh int kvmppc_set_cap_nested_kvm_hv(int enable)
2539b9a477b7SSuraj Jitindar Singh {
2540b9a477b7SSuraj Jitindar Singh     return kvm_vm_enable_cap(kvm_state, KVM_CAP_PPC_NESTED_HV, 0, enable);
2541b9a477b7SSuraj Jitindar Singh }
2542b9a477b7SSuraj Jitindar Singh 
25439ded780cSAlexey Kardashevskiy bool kvmppc_has_cap_spapr_vfio(void)
25449ded780cSAlexey Kardashevskiy {
25459ded780cSAlexey Kardashevskiy     return cap_spapr_vfio;
25469ded780cSAlexey Kardashevskiy }
25479ded780cSAlexey Kardashevskiy 
25487d050527SSuraj Jitindar Singh int kvmppc_get_cap_large_decr(void)
25497d050527SSuraj Jitindar Singh {
25507d050527SSuraj Jitindar Singh     return cap_large_decr;
25517d050527SSuraj Jitindar Singh }
25527d050527SSuraj Jitindar Singh 
25537d050527SSuraj Jitindar Singh int kvmppc_enable_cap_large_decr(PowerPCCPU *cpu, int enable)
25547d050527SSuraj Jitindar Singh {
25557d050527SSuraj Jitindar Singh     CPUState *cs = CPU(cpu);
25567d050527SSuraj Jitindar Singh     uint64_t lpcr;
25577d050527SSuraj Jitindar Singh 
25587d050527SSuraj Jitindar Singh     kvm_get_one_reg(cs, KVM_REG_PPC_LPCR_64, &lpcr);
25597d050527SSuraj Jitindar Singh     /* Do we need to modify the LPCR? */
25607d050527SSuraj Jitindar Singh     if (!!(lpcr & LPCR_LD) != !!enable) {
25617d050527SSuraj Jitindar Singh         if (enable) {
25627d050527SSuraj Jitindar Singh             lpcr |= LPCR_LD;
25637d050527SSuraj Jitindar Singh         } else {
25647d050527SSuraj Jitindar Singh             lpcr &= ~LPCR_LD;
25657d050527SSuraj Jitindar Singh         }
25667d050527SSuraj Jitindar Singh         kvm_set_one_reg(cs, KVM_REG_PPC_LPCR_64, &lpcr);
25677d050527SSuraj Jitindar Singh         kvm_get_one_reg(cs, KVM_REG_PPC_LPCR_64, &lpcr);
25687d050527SSuraj Jitindar Singh 
25697d050527SSuraj Jitindar Singh         if (!!(lpcr & LPCR_LD) != !!enable) {
25707d050527SSuraj Jitindar Singh             return -1;
25717d050527SSuraj Jitindar Singh         }
25727d050527SSuraj Jitindar Singh     }
25737d050527SSuraj Jitindar Singh 
25747d050527SSuraj Jitindar Singh     return 0;
25757d050527SSuraj Jitindar Singh }
25767d050527SSuraj Jitindar Singh 
257752b2519cSThomas Huth PowerPCCPUClass *kvm_ppc_get_host_cpu_class(void)
257852b2519cSThomas Huth {
257952b2519cSThomas Huth     uint32_t host_pvr = mfpvr();
258052b2519cSThomas Huth     PowerPCCPUClass *pvr_pcc;
258152b2519cSThomas Huth 
258252b2519cSThomas Huth     pvr_pcc = ppc_cpu_class_by_pvr(host_pvr);
258352b2519cSThomas Huth     if (pvr_pcc == NULL) {
258452b2519cSThomas Huth         pvr_pcc = ppc_cpu_class_by_pvr_mask(host_pvr);
258552b2519cSThomas Huth     }
258652b2519cSThomas Huth 
258752b2519cSThomas Huth     return pvr_pcc;
258852b2519cSThomas Huth }
258952b2519cSThomas Huth 
25902e9c10ebSIgor Mammedov static int kvm_ppc_register_host_cpu_type(MachineState *ms)
25915ba4576bSAndreas Färber {
25925ba4576bSAndreas Färber     TypeInfo type_info = {
25935ba4576bSAndreas Färber         .name = TYPE_HOST_POWERPC_CPU,
25945ba4576bSAndreas Färber         .class_init = kvmppc_host_cpu_class_init,
25955ba4576bSAndreas Färber     };
25962e9c10ebSIgor Mammedov     MachineClass *mc = MACHINE_GET_CLASS(ms);
25975ba4576bSAndreas Färber     PowerPCCPUClass *pvr_pcc;
259892e926e1SGreg Kurz     ObjectClass *oc;
25995b79b1caSAlexey Kardashevskiy     DeviceClass *dc;
2600715d4b96SThomas Huth     int i;
26015ba4576bSAndreas Färber 
260252b2519cSThomas Huth     pvr_pcc = kvm_ppc_get_host_cpu_class();
26033bc9ccc0SAlexey Kardashevskiy     if (pvr_pcc == NULL) {
26045ba4576bSAndreas Färber         return -1;
26055ba4576bSAndreas Färber     }
26065ba4576bSAndreas Färber     type_info.parent = object_class_get_name(OBJECT_CLASS(pvr_pcc));
26075ba4576bSAndreas Färber     type_register(&type_info);
26082e9c10ebSIgor Mammedov     if (object_dynamic_cast(OBJECT(ms), TYPE_SPAPR_MACHINE)) {
26092e9c10ebSIgor Mammedov         /* override TCG default cpu type with 'host' cpu model */
26102e9c10ebSIgor Mammedov         mc->default_cpu_type = TYPE_HOST_POWERPC_CPU;
26112e9c10ebSIgor Mammedov     }
26125b79b1caSAlexey Kardashevskiy 
261392e926e1SGreg Kurz     oc = object_class_by_name(type_info.name);
261492e926e1SGreg Kurz     g_assert(oc);
261592e926e1SGreg Kurz 
2616715d4b96SThomas Huth     /*
2617715d4b96SThomas Huth      * Update generic CPU family class alias (e.g. on a POWER8NVL host,
2618715d4b96SThomas Huth      * we want "POWER8" to be a "family" alias that points to the current
2619715d4b96SThomas Huth      * host CPU type, too)
2620715d4b96SThomas Huth      */
2621715d4b96SThomas Huth     dc = DEVICE_CLASS(ppc_cpu_get_family_class(pvr_pcc));
2622715d4b96SThomas Huth     for (i = 0; ppc_cpu_aliases[i].alias != NULL; i++) {
2623c5354f54SIgor Mammedov         if (strcasecmp(ppc_cpu_aliases[i].alias, dc->desc) == 0) {
2624715d4b96SThomas Huth             char *suffix;
2625715d4b96SThomas Huth 
2626715d4b96SThomas Huth             ppc_cpu_aliases[i].model = g_strdup(object_class_get_name(oc));
2627c9137065SIgor Mammedov             suffix = strstr(ppc_cpu_aliases[i].model, POWERPC_CPU_TYPE_SUFFIX);
2628715d4b96SThomas Huth             if (suffix) {
2629715d4b96SThomas Huth                 *suffix = 0;
2630715d4b96SThomas Huth             }
2631715d4b96SThomas Huth             break;
2632715d4b96SThomas Huth         }
2633715d4b96SThomas Huth     }
2634715d4b96SThomas Huth 
26355ba4576bSAndreas Färber     return 0;
26365ba4576bSAndreas Färber }
26375ba4576bSAndreas Färber 
2638feaa64c4SDavid Gibson int kvmppc_define_rtas_kernel_token(uint32_t token, const char *function)
2639feaa64c4SDavid Gibson {
2640feaa64c4SDavid Gibson     struct kvm_rtas_token_args args = {
2641feaa64c4SDavid Gibson         .token = token,
2642feaa64c4SDavid Gibson     };
2643feaa64c4SDavid Gibson 
2644feaa64c4SDavid Gibson     if (!kvm_check_extension(kvm_state, KVM_CAP_PPC_RTAS)) {
2645feaa64c4SDavid Gibson         return -ENOENT;
2646feaa64c4SDavid Gibson     }
2647feaa64c4SDavid Gibson 
2648feaa64c4SDavid Gibson     strncpy(args.name, function, sizeof(args.name));
2649feaa64c4SDavid Gibson 
2650feaa64c4SDavid Gibson     return kvm_vm_ioctl(kvm_state, KVM_PPC_RTAS_DEFINE_TOKEN, &args);
2651feaa64c4SDavid Gibson }
265212b1143bSDavid Gibson 
265314b0d748SGreg Kurz int kvmppc_get_htab_fd(bool write, uint64_t index, Error **errp)
2654e68cb8b4SAlexey Kardashevskiy {
2655e68cb8b4SAlexey Kardashevskiy     struct kvm_get_htab_fd s = {
2656e68cb8b4SAlexey Kardashevskiy         .flags = write ? KVM_GET_HTAB_WRITE : 0,
265714b0d748SGreg Kurz         .start_index = index,
2658e68cb8b4SAlexey Kardashevskiy     };
265982be8e73SGreg Kurz     int ret;
2660e68cb8b4SAlexey Kardashevskiy 
2661e68cb8b4SAlexey Kardashevskiy     if (!cap_htab_fd) {
266214b0d748SGreg Kurz         error_setg(errp, "KVM version doesn't support %s the HPT",
266314b0d748SGreg Kurz                    write ? "writing" : "reading");
266482be8e73SGreg Kurz         return -ENOTSUP;
2665e68cb8b4SAlexey Kardashevskiy     }
2666e68cb8b4SAlexey Kardashevskiy 
266782be8e73SGreg Kurz     ret = kvm_vm_ioctl(kvm_state, KVM_PPC_GET_HTAB_FD, &s);
266882be8e73SGreg Kurz     if (ret < 0) {
266914b0d748SGreg Kurz         error_setg(errp, "Unable to open fd for %s HPT %s KVM: %s",
267014b0d748SGreg Kurz                    write ? "writing" : "reading", write ? "to" : "from",
267114b0d748SGreg Kurz                    strerror(errno));
267282be8e73SGreg Kurz         return -errno;
267382be8e73SGreg Kurz     }
267482be8e73SGreg Kurz 
267582be8e73SGreg Kurz     return ret;
2676e68cb8b4SAlexey Kardashevskiy }
2677e68cb8b4SAlexey Kardashevskiy 
2678e68cb8b4SAlexey Kardashevskiy int kvmppc_save_htab(QEMUFile *f, int fd, size_t bufsize, int64_t max_ns)
2679e68cb8b4SAlexey Kardashevskiy {
2680bc72ad67SAlex Bligh     int64_t starttime = qemu_clock_get_ns(QEMU_CLOCK_REALTIME);
2681e68cb8b4SAlexey Kardashevskiy     uint8_t buf[bufsize];
2682e68cb8b4SAlexey Kardashevskiy     ssize_t rc;
2683e68cb8b4SAlexey Kardashevskiy 
2684e68cb8b4SAlexey Kardashevskiy     do {
2685e68cb8b4SAlexey Kardashevskiy         rc = read(fd, buf, bufsize);
2686e68cb8b4SAlexey Kardashevskiy         if (rc < 0) {
2687e68cb8b4SAlexey Kardashevskiy             fprintf(stderr, "Error reading data from KVM HTAB fd: %s\n",
2688e68cb8b4SAlexey Kardashevskiy                     strerror(errno));
2689e68cb8b4SAlexey Kardashevskiy             return rc;
2690e68cb8b4SAlexey Kardashevskiy         } else if (rc) {
2691e094c4c1SCédric Le Goater             uint8_t *buffer = buf;
2692e094c4c1SCédric Le Goater             ssize_t n = rc;
2693e094c4c1SCédric Le Goater             while (n) {
2694e094c4c1SCédric Le Goater                 struct kvm_get_htab_header *head =
2695e094c4c1SCédric Le Goater                     (struct kvm_get_htab_header *) buffer;
2696e094c4c1SCédric Le Goater                 size_t chunksize = sizeof(*head) +
2697e094c4c1SCédric Le Goater                      HASH_PTE_SIZE_64 * head->n_valid;
2698e094c4c1SCédric Le Goater 
2699e094c4c1SCédric Le Goater                 qemu_put_be32(f, head->index);
2700e094c4c1SCédric Le Goater                 qemu_put_be16(f, head->n_valid);
2701e094c4c1SCédric Le Goater                 qemu_put_be16(f, head->n_invalid);
2702e094c4c1SCédric Le Goater                 qemu_put_buffer(f, (void *)(head + 1),
2703e094c4c1SCédric Le Goater                                 HASH_PTE_SIZE_64 * head->n_valid);
2704e094c4c1SCédric Le Goater 
2705e094c4c1SCédric Le Goater                 buffer += chunksize;
2706e094c4c1SCédric Le Goater                 n -= chunksize;
2707e094c4c1SCédric Le Goater             }
2708e68cb8b4SAlexey Kardashevskiy         }
2709e68cb8b4SAlexey Kardashevskiy     } while ((rc != 0)
2710c995e942SDavid Gibson              && ((max_ns < 0) ||
2711c995e942SDavid Gibson                  ((qemu_clock_get_ns(QEMU_CLOCK_REALTIME) - starttime) < max_ns)));
2712e68cb8b4SAlexey Kardashevskiy 
2713e68cb8b4SAlexey Kardashevskiy     return (rc == 0) ? 1 : 0;
2714e68cb8b4SAlexey Kardashevskiy }
2715e68cb8b4SAlexey Kardashevskiy 
2716e68cb8b4SAlexey Kardashevskiy int kvmppc_load_htab_chunk(QEMUFile *f, int fd, uint32_t index,
2717e68cb8b4SAlexey Kardashevskiy                            uint16_t n_valid, uint16_t n_invalid)
2718e68cb8b4SAlexey Kardashevskiy {
2719e68cb8b4SAlexey Kardashevskiy     struct kvm_get_htab_header *buf;
2720e68cb8b4SAlexey Kardashevskiy     size_t chunksize = sizeof(*buf) + n_valid * HASH_PTE_SIZE_64;
2721e68cb8b4SAlexey Kardashevskiy     ssize_t rc;
2722e68cb8b4SAlexey Kardashevskiy 
2723e68cb8b4SAlexey Kardashevskiy     buf = alloca(chunksize);
2724e68cb8b4SAlexey Kardashevskiy     buf->index = index;
2725e68cb8b4SAlexey Kardashevskiy     buf->n_valid = n_valid;
2726e68cb8b4SAlexey Kardashevskiy     buf->n_invalid = n_invalid;
2727e68cb8b4SAlexey Kardashevskiy 
2728e68cb8b4SAlexey Kardashevskiy     qemu_get_buffer(f, (void *)(buf + 1), HASH_PTE_SIZE_64 * n_valid);
2729e68cb8b4SAlexey Kardashevskiy 
2730e68cb8b4SAlexey Kardashevskiy     rc = write(fd, buf, chunksize);
2731e68cb8b4SAlexey Kardashevskiy     if (rc < 0) {
2732e68cb8b4SAlexey Kardashevskiy         fprintf(stderr, "Error writing KVM hash table: %s\n",
2733e68cb8b4SAlexey Kardashevskiy                 strerror(errno));
2734e68cb8b4SAlexey Kardashevskiy         return rc;
2735e68cb8b4SAlexey Kardashevskiy     }
2736e68cb8b4SAlexey Kardashevskiy     if (rc != chunksize) {
2737e68cb8b4SAlexey Kardashevskiy         /* We should never get a short write on a single chunk */
2738e68cb8b4SAlexey Kardashevskiy         fprintf(stderr, "Short write, restoring KVM hash table\n");
2739e68cb8b4SAlexey Kardashevskiy         return -1;
2740e68cb8b4SAlexey Kardashevskiy     }
2741e68cb8b4SAlexey Kardashevskiy     return 0;
2742e68cb8b4SAlexey Kardashevskiy }
2743e68cb8b4SAlexey Kardashevskiy 
274420d695a9SAndreas Färber bool kvm_arch_stop_on_emulation_error(CPUState *cpu)
27454513d923SGleb Natapov {
27464513d923SGleb Natapov     return true;
27474513d923SGleb Natapov }
2748a1b87fe0SJan Kiszka 
274982169660SScott Wood void kvm_arch_init_irq_routing(KVMState *s)
275082169660SScott Wood {
275182169660SScott Wood }
2752c65f9a07SGreg Kurz 
27531ad9f0a4SDavid Gibson void kvmppc_read_hptes(ppc_hash_pte64_t *hptes, hwaddr ptex, int n)
27541ad9f0a4SDavid Gibson {
27551ad9f0a4SDavid Gibson     int fd, rc;
27561ad9f0a4SDavid Gibson     int i;
27577c43bca0SAneesh Kumar K.V 
275814b0d748SGreg Kurz     fd = kvmppc_get_htab_fd(false, ptex, &error_abort);
27591ad9f0a4SDavid Gibson 
27601ad9f0a4SDavid Gibson     i = 0;
27611ad9f0a4SDavid Gibson     while (i < n) {
27621ad9f0a4SDavid Gibson         struct kvm_get_htab_header *hdr;
27631ad9f0a4SDavid Gibson         int m = n < HPTES_PER_GROUP ? n : HPTES_PER_GROUP;
27641ad9f0a4SDavid Gibson         char buf[sizeof(*hdr) + m * HASH_PTE_SIZE_64];
27651ad9f0a4SDavid Gibson 
27661ad9f0a4SDavid Gibson         rc = read(fd, buf, sizeof(buf));
27671ad9f0a4SDavid Gibson         if (rc < 0) {
27681ad9f0a4SDavid Gibson             hw_error("kvmppc_read_hptes: Unable to read HPTEs");
27691ad9f0a4SDavid Gibson         }
27701ad9f0a4SDavid Gibson 
27711ad9f0a4SDavid Gibson         hdr = (struct kvm_get_htab_header *)buf;
27721ad9f0a4SDavid Gibson         while ((i < n) && ((char *)hdr < (buf + rc))) {
2773a36593e1SAlexey Kardashevskiy             int invalid = hdr->n_invalid, valid = hdr->n_valid;
27741ad9f0a4SDavid Gibson 
27751ad9f0a4SDavid Gibson             if (hdr->index != (ptex + i)) {
27761ad9f0a4SDavid Gibson                 hw_error("kvmppc_read_hptes: Unexpected HPTE index %"PRIu32
27771ad9f0a4SDavid Gibson                          " != (%"HWADDR_PRIu" + %d", hdr->index, ptex, i);
27781ad9f0a4SDavid Gibson             }
27791ad9f0a4SDavid Gibson 
2780a36593e1SAlexey Kardashevskiy             if (n - i < valid) {
2781a36593e1SAlexey Kardashevskiy                 valid = n - i;
2782a36593e1SAlexey Kardashevskiy             }
2783a36593e1SAlexey Kardashevskiy             memcpy(hptes + i, hdr + 1, HASH_PTE_SIZE_64 * valid);
2784a36593e1SAlexey Kardashevskiy             i += valid;
27851ad9f0a4SDavid Gibson 
27861ad9f0a4SDavid Gibson             if ((n - i) < invalid) {
27871ad9f0a4SDavid Gibson                 invalid = n - i;
27881ad9f0a4SDavid Gibson             }
27891ad9f0a4SDavid Gibson             memset(hptes + i, 0, invalid * HASH_PTE_SIZE_64);
2790a36593e1SAlexey Kardashevskiy             i += invalid;
27911ad9f0a4SDavid Gibson 
27921ad9f0a4SDavid Gibson             hdr = (struct kvm_get_htab_header *)
27931ad9f0a4SDavid Gibson                 ((char *)(hdr + 1) + HASH_PTE_SIZE_64 * hdr->n_valid);
27941ad9f0a4SDavid Gibson         }
27951ad9f0a4SDavid Gibson     }
27961ad9f0a4SDavid Gibson 
27971ad9f0a4SDavid Gibson     close(fd);
27981ad9f0a4SDavid Gibson }
27991ad9f0a4SDavid Gibson 
28001ad9f0a4SDavid Gibson void kvmppc_write_hpte(hwaddr ptex, uint64_t pte0, uint64_t pte1)
28017c43bca0SAneesh Kumar K.V {
28021ad9f0a4SDavid Gibson     int fd, rc;
28031ad9f0a4SDavid Gibson     struct {
28041ad9f0a4SDavid Gibson         struct kvm_get_htab_header hdr;
28051ad9f0a4SDavid Gibson         uint64_t pte0;
28061ad9f0a4SDavid Gibson         uint64_t pte1;
28071ad9f0a4SDavid Gibson     } buf;
2808c1385933SAneesh Kumar K.V 
280914b0d748SGreg Kurz     fd = kvmppc_get_htab_fd(true, 0 /* Ignored */, &error_abort);
2810c1385933SAneesh Kumar K.V 
28111ad9f0a4SDavid Gibson     buf.hdr.n_valid = 1;
28121ad9f0a4SDavid Gibson     buf.hdr.n_invalid = 0;
28131ad9f0a4SDavid Gibson     buf.hdr.index = ptex;
28141ad9f0a4SDavid Gibson     buf.pte0 = cpu_to_be64(pte0);
28151ad9f0a4SDavid Gibson     buf.pte1 = cpu_to_be64(pte1);
28161ad9f0a4SDavid Gibson 
28171ad9f0a4SDavid Gibson     rc = write(fd, &buf, sizeof(buf));
28181ad9f0a4SDavid Gibson     if (rc != sizeof(buf)) {
28191ad9f0a4SDavid Gibson         hw_error("kvmppc_write_hpte: Unable to update KVM HPT");
2820c1385933SAneesh Kumar K.V     }
28211ad9f0a4SDavid Gibson     close(fd);
2822c1385933SAneesh Kumar K.V }
28239e03a040SFrank Blaschka 
28249e03a040SFrank Blaschka int kvm_arch_fixup_msi_route(struct kvm_irq_routing_entry *route,
2825dc9f06caSPavel Fedin                              uint64_t address, uint32_t data, PCIDevice *dev)
28269e03a040SFrank Blaschka {
28279e03a040SFrank Blaschka     return 0;
28289e03a040SFrank Blaschka }
28291850b6b7SEric Auger 
283038d87493SPeter Xu int kvm_arch_add_msi_route_post(struct kvm_irq_routing_entry *route,
283138d87493SPeter Xu                                 int vector, PCIDevice *dev)
283238d87493SPeter Xu {
283338d87493SPeter Xu     return 0;
283438d87493SPeter Xu }
283538d87493SPeter Xu 
283638d87493SPeter Xu int kvm_arch_release_virq_post(int virq)
283738d87493SPeter Xu {
283838d87493SPeter Xu     return 0;
283938d87493SPeter Xu }
284038d87493SPeter Xu 
28411850b6b7SEric Auger int kvm_arch_msi_data_to_gsi(uint32_t data)
28421850b6b7SEric Auger {
28431850b6b7SEric Auger     return data & 0xffff;
28441850b6b7SEric Auger }
28454d9392beSThomas Huth 
28464d9392beSThomas Huth int kvmppc_enable_hwrng(void)
28474d9392beSThomas Huth {
28484d9392beSThomas Huth     if (!kvm_enabled() || !kvm_check_extension(kvm_state, KVM_CAP_PPC_HWRNG)) {
28494d9392beSThomas Huth         return -1;
28504d9392beSThomas Huth     }
28514d9392beSThomas Huth 
28524d9392beSThomas Huth     return kvmppc_enable_hcall(kvm_state, H_RANDOM);
28534d9392beSThomas Huth }
285430f4b05bSDavid Gibson 
285530f4b05bSDavid Gibson void kvmppc_check_papr_resize_hpt(Error **errp)
285630f4b05bSDavid Gibson {
285730f4b05bSDavid Gibson     if (!kvm_enabled()) {
2858b55d295eSDavid Gibson         return; /* No KVM, we're good */
2859b55d295eSDavid Gibson     }
2860b55d295eSDavid Gibson 
2861b55d295eSDavid Gibson     if (cap_resize_hpt) {
2862b55d295eSDavid Gibson         return; /* Kernel has explicit support, we're good */
2863b55d295eSDavid Gibson     }
2864b55d295eSDavid Gibson 
2865b55d295eSDavid Gibson     /* Otherwise fallback on looking for PR KVM */
2866b55d295eSDavid Gibson     if (kvmppc_is_pr(kvm_state)) {
286730f4b05bSDavid Gibson         return;
286830f4b05bSDavid Gibson     }
286930f4b05bSDavid Gibson 
287030f4b05bSDavid Gibson     error_setg(errp,
287130f4b05bSDavid Gibson                "Hash page table resizing not available with this KVM version");
287230f4b05bSDavid Gibson }
2873b55d295eSDavid Gibson 
2874b55d295eSDavid Gibson int kvmppc_resize_hpt_prepare(PowerPCCPU *cpu, target_ulong flags, int shift)
2875b55d295eSDavid Gibson {
2876b55d295eSDavid Gibson     CPUState *cs = CPU(cpu);
2877b55d295eSDavid Gibson     struct kvm_ppc_resize_hpt rhpt = {
2878b55d295eSDavid Gibson         .flags = flags,
2879b55d295eSDavid Gibson         .shift = shift,
2880b55d295eSDavid Gibson     };
2881b55d295eSDavid Gibson 
2882b55d295eSDavid Gibson     if (!cap_resize_hpt) {
2883b55d295eSDavid Gibson         return -ENOSYS;
2884b55d295eSDavid Gibson     }
2885b55d295eSDavid Gibson 
2886b55d295eSDavid Gibson     return kvm_vm_ioctl(cs->kvm_state, KVM_PPC_RESIZE_HPT_PREPARE, &rhpt);
2887b55d295eSDavid Gibson }
2888b55d295eSDavid Gibson 
2889b55d295eSDavid Gibson int kvmppc_resize_hpt_commit(PowerPCCPU *cpu, target_ulong flags, int shift)
2890b55d295eSDavid Gibson {
2891b55d295eSDavid Gibson     CPUState *cs = CPU(cpu);
2892b55d295eSDavid Gibson     struct kvm_ppc_resize_hpt rhpt = {
2893b55d295eSDavid Gibson         .flags = flags,
2894b55d295eSDavid Gibson         .shift = shift,
2895b55d295eSDavid Gibson     };
2896b55d295eSDavid Gibson 
2897b55d295eSDavid Gibson     if (!cap_resize_hpt) {
2898b55d295eSDavid Gibson         return -ENOSYS;
2899b55d295eSDavid Gibson     }
2900b55d295eSDavid Gibson 
2901b55d295eSDavid Gibson     return kvm_vm_ioctl(cs->kvm_state, KVM_PPC_RESIZE_HPT_COMMIT, &rhpt);
2902b55d295eSDavid Gibson }
2903b55d295eSDavid Gibson 
2904c363a37aSDaniel Henrique Barboza /*
2905c363a37aSDaniel Henrique Barboza  * This is a helper function to detect a post migration scenario
2906c363a37aSDaniel Henrique Barboza  * in which a guest, running as KVM-HV, freezes in cpu_post_load because
2907c363a37aSDaniel Henrique Barboza  * the guest kernel can't handle a PVR value other than the actual host
2908c363a37aSDaniel Henrique Barboza  * PVR in KVM_SET_SREGS, even if pvr_match() returns true.
2909c363a37aSDaniel Henrique Barboza  *
2910c363a37aSDaniel Henrique Barboza  * If we don't have cap_ppc_pvr_compat and we're not running in PR
2911c363a37aSDaniel Henrique Barboza  * (so, we're HV), return true. The workaround itself is done in
2912c363a37aSDaniel Henrique Barboza  * cpu_post_load.
2913c363a37aSDaniel Henrique Barboza  *
2914c363a37aSDaniel Henrique Barboza  * The order here is important: we'll only check for KVM PR as a
2915c363a37aSDaniel Henrique Barboza  * fallback if the guest kernel can't handle the situation itself.
2916c363a37aSDaniel Henrique Barboza  * We need to avoid as much as possible querying the running KVM type
2917c363a37aSDaniel Henrique Barboza  * in QEMU level.
2918c363a37aSDaniel Henrique Barboza  */
2919c363a37aSDaniel Henrique Barboza bool kvmppc_pvr_workaround_required(PowerPCCPU *cpu)
2920c363a37aSDaniel Henrique Barboza {
2921c363a37aSDaniel Henrique Barboza     CPUState *cs = CPU(cpu);
2922c363a37aSDaniel Henrique Barboza 
2923c363a37aSDaniel Henrique Barboza     if (!kvm_enabled()) {
2924c363a37aSDaniel Henrique Barboza         return false;
2925c363a37aSDaniel Henrique Barboza     }
2926c363a37aSDaniel Henrique Barboza 
2927c363a37aSDaniel Henrique Barboza     if (cap_ppc_pvr_compat) {
2928c363a37aSDaniel Henrique Barboza         return false;
2929c363a37aSDaniel Henrique Barboza     }
2930c363a37aSDaniel Henrique Barboza 
2931c363a37aSDaniel Henrique Barboza     return !kvmppc_is_pr(cs->kvm_state);
2932c363a37aSDaniel Henrique Barboza }
2933a84f7179SNikunj A Dadhania 
2934a84f7179SNikunj A Dadhania void kvmppc_set_reg_ppc_online(PowerPCCPU *cpu, unsigned int online)
2935a84f7179SNikunj A Dadhania {
2936a84f7179SNikunj A Dadhania     CPUState *cs = CPU(cpu);
2937a84f7179SNikunj A Dadhania 
2938a84f7179SNikunj A Dadhania     if (kvm_enabled()) {
2939a84f7179SNikunj A Dadhania         kvm_set_one_reg(cs, KVM_REG_PPC_ONLINE, &online);
2940a84f7179SNikunj A Dadhania     }
2941a84f7179SNikunj A Dadhania }
2942