xref: /qemu/target/ppc/kvm.c (revision 9723295a720ca26fe31a1e374555d83bccc051f4)
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 
524b1115c99SLiran Alon int kvm_arch_destroy_vcpu(CPUState *cs)
525b1115c99SLiran Alon {
526b1115c99SLiran Alon     return 0;
527b1115c99SLiran Alon }
528b1115c99SLiran Alon 
5291bc22652SAndreas Färber static void kvm_sw_tlb_put(PowerPCCPU *cpu)
53093dd5e85SScott Wood {
5311bc22652SAndreas Färber     CPUPPCState *env = &cpu->env;
5321bc22652SAndreas Färber     CPUState *cs = CPU(cpu);
53393dd5e85SScott Wood     struct kvm_dirty_tlb dirty_tlb;
53493dd5e85SScott Wood     unsigned char *bitmap;
53593dd5e85SScott Wood     int ret;
53693dd5e85SScott Wood 
53793dd5e85SScott Wood     if (!env->kvm_sw_tlb) {
53893dd5e85SScott Wood         return;
53993dd5e85SScott Wood     }
54093dd5e85SScott Wood 
54193dd5e85SScott Wood     bitmap = g_malloc((env->nb_tlb + 7) / 8);
54293dd5e85SScott Wood     memset(bitmap, 0xFF, (env->nb_tlb + 7) / 8);
54393dd5e85SScott Wood 
54493dd5e85SScott Wood     dirty_tlb.bitmap = (uintptr_t)bitmap;
54593dd5e85SScott Wood     dirty_tlb.num_dirty = env->nb_tlb;
54693dd5e85SScott Wood 
5471bc22652SAndreas Färber     ret = kvm_vcpu_ioctl(cs, KVM_DIRTY_TLB, &dirty_tlb);
54893dd5e85SScott Wood     if (ret) {
54993dd5e85SScott Wood         fprintf(stderr, "%s: KVM_DIRTY_TLB: %s\n",
55093dd5e85SScott Wood                 __func__, strerror(-ret));
55193dd5e85SScott Wood     }
55293dd5e85SScott Wood 
55393dd5e85SScott Wood     g_free(bitmap);
55493dd5e85SScott Wood }
55593dd5e85SScott Wood 
556d67d40eaSDavid Gibson static void kvm_get_one_spr(CPUState *cs, uint64_t id, int spr)
557d67d40eaSDavid Gibson {
558d67d40eaSDavid Gibson     PowerPCCPU *cpu = POWERPC_CPU(cs);
559d67d40eaSDavid Gibson     CPUPPCState *env = &cpu->env;
560d67d40eaSDavid Gibson     union {
561d67d40eaSDavid Gibson         uint32_t u32;
562d67d40eaSDavid Gibson         uint64_t u64;
563d67d40eaSDavid Gibson     } val;
564d67d40eaSDavid Gibson     struct kvm_one_reg reg = {
565d67d40eaSDavid Gibson         .id = id,
566d67d40eaSDavid Gibson         .addr = (uintptr_t) &val,
567d67d40eaSDavid Gibson     };
568d67d40eaSDavid Gibson     int ret;
569d67d40eaSDavid Gibson 
570d67d40eaSDavid Gibson     ret = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, &reg);
571d67d40eaSDavid Gibson     if (ret != 0) {
572b36f100eSAlexey Kardashevskiy         trace_kvm_failed_spr_get(spr, strerror(errno));
573d67d40eaSDavid Gibson     } else {
574d67d40eaSDavid Gibson         switch (id & KVM_REG_SIZE_MASK) {
575d67d40eaSDavid Gibson         case KVM_REG_SIZE_U32:
576d67d40eaSDavid Gibson             env->spr[spr] = val.u32;
577d67d40eaSDavid Gibson             break;
578d67d40eaSDavid Gibson 
579d67d40eaSDavid Gibson         case KVM_REG_SIZE_U64:
580d67d40eaSDavid Gibson             env->spr[spr] = val.u64;
581d67d40eaSDavid Gibson             break;
582d67d40eaSDavid Gibson 
583d67d40eaSDavid Gibson         default:
584d67d40eaSDavid Gibson             /* Don't handle this size yet */
585d67d40eaSDavid Gibson             abort();
586d67d40eaSDavid Gibson         }
587d67d40eaSDavid Gibson     }
588d67d40eaSDavid Gibson }
589d67d40eaSDavid Gibson 
590d67d40eaSDavid Gibson static void kvm_put_one_spr(CPUState *cs, uint64_t id, int spr)
591d67d40eaSDavid Gibson {
592d67d40eaSDavid Gibson     PowerPCCPU *cpu = POWERPC_CPU(cs);
593d67d40eaSDavid Gibson     CPUPPCState *env = &cpu->env;
594d67d40eaSDavid Gibson     union {
595d67d40eaSDavid Gibson         uint32_t u32;
596d67d40eaSDavid Gibson         uint64_t u64;
597d67d40eaSDavid Gibson     } val;
598d67d40eaSDavid Gibson     struct kvm_one_reg reg = {
599d67d40eaSDavid Gibson         .id = id,
600d67d40eaSDavid Gibson         .addr = (uintptr_t) &val,
601d67d40eaSDavid Gibson     };
602d67d40eaSDavid Gibson     int ret;
603d67d40eaSDavid Gibson 
604d67d40eaSDavid Gibson     switch (id & KVM_REG_SIZE_MASK) {
605d67d40eaSDavid Gibson     case KVM_REG_SIZE_U32:
606d67d40eaSDavid Gibson         val.u32 = env->spr[spr];
607d67d40eaSDavid Gibson         break;
608d67d40eaSDavid Gibson 
609d67d40eaSDavid Gibson     case KVM_REG_SIZE_U64:
610d67d40eaSDavid Gibson         val.u64 = env->spr[spr];
611d67d40eaSDavid Gibson         break;
612d67d40eaSDavid Gibson 
613d67d40eaSDavid Gibson     default:
614d67d40eaSDavid Gibson         /* Don't handle this size yet */
615d67d40eaSDavid Gibson         abort();
616d67d40eaSDavid Gibson     }
617d67d40eaSDavid Gibson 
618d67d40eaSDavid Gibson     ret = kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, &reg);
619d67d40eaSDavid Gibson     if (ret != 0) {
620b36f100eSAlexey Kardashevskiy         trace_kvm_failed_spr_set(spr, strerror(errno));
621d67d40eaSDavid Gibson     }
622d67d40eaSDavid Gibson }
623d67d40eaSDavid Gibson 
62470b79849SDavid Gibson static int kvm_put_fp(CPUState *cs)
62570b79849SDavid Gibson {
62670b79849SDavid Gibson     PowerPCCPU *cpu = POWERPC_CPU(cs);
62770b79849SDavid Gibson     CPUPPCState *env = &cpu->env;
62870b79849SDavid Gibson     struct kvm_one_reg reg;
62970b79849SDavid Gibson     int i;
63070b79849SDavid Gibson     int ret;
63170b79849SDavid Gibson 
63270b79849SDavid Gibson     if (env->insns_flags & PPC_FLOAT) {
63370b79849SDavid Gibson         uint64_t fpscr = env->fpscr;
63470b79849SDavid Gibson         bool vsx = !!(env->insns_flags2 & PPC2_VSX);
63570b79849SDavid Gibson 
63670b79849SDavid Gibson         reg.id = KVM_REG_PPC_FPSCR;
63770b79849SDavid Gibson         reg.addr = (uintptr_t)&fpscr;
63870b79849SDavid Gibson         ret = kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, &reg);
63970b79849SDavid Gibson         if (ret < 0) {
6408d83cbf1SGreg Kurz             trace_kvm_failed_fpscr_set(strerror(errno));
64170b79849SDavid Gibson             return ret;
64270b79849SDavid Gibson         }
64370b79849SDavid Gibson 
64470b79849SDavid Gibson         for (i = 0; i < 32; i++) {
64570b79849SDavid Gibson             uint64_t vsr[2];
646ef96e3aeSMark Cave-Ayland             uint64_t *fpr = cpu_fpr_ptr(&cpu->env, i);
647ef96e3aeSMark Cave-Ayland             uint64_t *vsrl = cpu_vsrl_ptr(&cpu->env, i);
64870b79849SDavid Gibson 
6493a4b791bSGreg Kurz #ifdef HOST_WORDS_BIGENDIAN
650ef96e3aeSMark Cave-Ayland             vsr[0] = float64_val(*fpr);
651ef96e3aeSMark Cave-Ayland             vsr[1] = *vsrl;
6523a4b791bSGreg Kurz #else
653ef96e3aeSMark Cave-Ayland             vsr[0] = *vsrl;
654ef96e3aeSMark Cave-Ayland             vsr[1] = float64_val(*fpr);
6553a4b791bSGreg Kurz #endif
65670b79849SDavid Gibson             reg.addr = (uintptr_t) &vsr;
65770b79849SDavid Gibson             reg.id = vsx ? KVM_REG_PPC_VSR(i) : KVM_REG_PPC_FPR(i);
65870b79849SDavid Gibson 
65970b79849SDavid Gibson             ret = kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, &reg);
66070b79849SDavid Gibson             if (ret < 0) {
6618d83cbf1SGreg Kurz                 trace_kvm_failed_fp_set(vsx ? "VSR" : "FPR", i,
6628d83cbf1SGreg Kurz                                         strerror(errno));
66370b79849SDavid Gibson                 return ret;
66470b79849SDavid Gibson             }
66570b79849SDavid Gibson         }
66670b79849SDavid Gibson     }
66770b79849SDavid Gibson 
66870b79849SDavid Gibson     if (env->insns_flags & PPC_ALTIVEC) {
66970b79849SDavid Gibson         reg.id = KVM_REG_PPC_VSCR;
67070b79849SDavid Gibson         reg.addr = (uintptr_t)&env->vscr;
67170b79849SDavid Gibson         ret = kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, &reg);
67270b79849SDavid Gibson         if (ret < 0) {
6738d83cbf1SGreg Kurz             trace_kvm_failed_vscr_set(strerror(errno));
67470b79849SDavid Gibson             return ret;
67570b79849SDavid Gibson         }
67670b79849SDavid Gibson 
67770b79849SDavid Gibson         for (i = 0; i < 32; i++) {
67870b79849SDavid Gibson             reg.id = KVM_REG_PPC_VR(i);
679ef96e3aeSMark Cave-Ayland             reg.addr = (uintptr_t)cpu_avr_ptr(env, i);
68070b79849SDavid Gibson             ret = kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, &reg);
68170b79849SDavid Gibson             if (ret < 0) {
6828d83cbf1SGreg Kurz                 trace_kvm_failed_vr_set(i, strerror(errno));
68370b79849SDavid Gibson                 return ret;
68470b79849SDavid Gibson             }
68570b79849SDavid Gibson         }
68670b79849SDavid Gibson     }
68770b79849SDavid Gibson 
68870b79849SDavid Gibson     return 0;
68970b79849SDavid Gibson }
69070b79849SDavid Gibson 
69170b79849SDavid Gibson static int kvm_get_fp(CPUState *cs)
69270b79849SDavid Gibson {
69370b79849SDavid Gibson     PowerPCCPU *cpu = POWERPC_CPU(cs);
69470b79849SDavid Gibson     CPUPPCState *env = &cpu->env;
69570b79849SDavid Gibson     struct kvm_one_reg reg;
69670b79849SDavid Gibson     int i;
69770b79849SDavid Gibson     int ret;
69870b79849SDavid Gibson 
69970b79849SDavid Gibson     if (env->insns_flags & PPC_FLOAT) {
70070b79849SDavid Gibson         uint64_t fpscr;
70170b79849SDavid Gibson         bool vsx = !!(env->insns_flags2 & PPC2_VSX);
70270b79849SDavid Gibson 
70370b79849SDavid Gibson         reg.id = KVM_REG_PPC_FPSCR;
70470b79849SDavid Gibson         reg.addr = (uintptr_t)&fpscr;
70570b79849SDavid Gibson         ret = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, &reg);
70670b79849SDavid Gibson         if (ret < 0) {
7078d83cbf1SGreg Kurz             trace_kvm_failed_fpscr_get(strerror(errno));
70870b79849SDavid Gibson             return ret;
70970b79849SDavid Gibson         } else {
71070b79849SDavid Gibson             env->fpscr = fpscr;
71170b79849SDavid Gibson         }
71270b79849SDavid Gibson 
71370b79849SDavid Gibson         for (i = 0; i < 32; i++) {
71470b79849SDavid Gibson             uint64_t vsr[2];
715ef96e3aeSMark Cave-Ayland             uint64_t *fpr = cpu_fpr_ptr(&cpu->env, i);
716ef96e3aeSMark Cave-Ayland             uint64_t *vsrl = cpu_vsrl_ptr(&cpu->env, i);
71770b79849SDavid Gibson 
71870b79849SDavid Gibson             reg.addr = (uintptr_t) &vsr;
71970b79849SDavid Gibson             reg.id = vsx ? KVM_REG_PPC_VSR(i) : KVM_REG_PPC_FPR(i);
72070b79849SDavid Gibson 
72170b79849SDavid Gibson             ret = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, &reg);
72270b79849SDavid Gibson             if (ret < 0) {
7238d83cbf1SGreg Kurz                 trace_kvm_failed_fp_get(vsx ? "VSR" : "FPR", i,
7248d83cbf1SGreg Kurz                                         strerror(errno));
72570b79849SDavid Gibson                 return ret;
72670b79849SDavid Gibson             } else {
7273a4b791bSGreg Kurz #ifdef HOST_WORDS_BIGENDIAN
728ef96e3aeSMark Cave-Ayland                 *fpr = vsr[0];
72970b79849SDavid Gibson                 if (vsx) {
730ef96e3aeSMark Cave-Ayland                     *vsrl = vsr[1];
73170b79849SDavid Gibson                 }
7323a4b791bSGreg Kurz #else
733ef96e3aeSMark Cave-Ayland                 *fpr = vsr[1];
7343a4b791bSGreg Kurz                 if (vsx) {
735ef96e3aeSMark Cave-Ayland                     *vsrl = vsr[0];
7363a4b791bSGreg Kurz                 }
7373a4b791bSGreg Kurz #endif
73870b79849SDavid Gibson             }
73970b79849SDavid Gibson         }
74070b79849SDavid Gibson     }
74170b79849SDavid Gibson 
74270b79849SDavid Gibson     if (env->insns_flags & PPC_ALTIVEC) {
74370b79849SDavid Gibson         reg.id = KVM_REG_PPC_VSCR;
74470b79849SDavid Gibson         reg.addr = (uintptr_t)&env->vscr;
74570b79849SDavid Gibson         ret = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, &reg);
74670b79849SDavid Gibson         if (ret < 0) {
7478d83cbf1SGreg Kurz             trace_kvm_failed_vscr_get(strerror(errno));
74870b79849SDavid Gibson             return ret;
74970b79849SDavid Gibson         }
75070b79849SDavid Gibson 
75170b79849SDavid Gibson         for (i = 0; i < 32; i++) {
75270b79849SDavid Gibson             reg.id = KVM_REG_PPC_VR(i);
753ef96e3aeSMark Cave-Ayland             reg.addr = (uintptr_t)cpu_avr_ptr(env, i);
75470b79849SDavid Gibson             ret = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, &reg);
75570b79849SDavid Gibson             if (ret < 0) {
7568d83cbf1SGreg Kurz                 trace_kvm_failed_vr_get(i, strerror(errno));
75770b79849SDavid Gibson                 return ret;
75870b79849SDavid Gibson             }
75970b79849SDavid Gibson         }
76070b79849SDavid Gibson     }
76170b79849SDavid Gibson 
76270b79849SDavid Gibson     return 0;
76370b79849SDavid Gibson }
76470b79849SDavid Gibson 
7659b00ea49SDavid Gibson #if defined(TARGET_PPC64)
7669b00ea49SDavid Gibson static int kvm_get_vpa(CPUState *cs)
7679b00ea49SDavid Gibson {
7689b00ea49SDavid Gibson     PowerPCCPU *cpu = POWERPC_CPU(cs);
769ce2918cbSDavid Gibson     SpaprCpuState *spapr_cpu = spapr_cpu_state(cpu);
7709b00ea49SDavid Gibson     struct kvm_one_reg reg;
7719b00ea49SDavid Gibson     int ret;
7729b00ea49SDavid Gibson 
7739b00ea49SDavid Gibson     reg.id = KVM_REG_PPC_VPA_ADDR;
7747388efafSDavid Gibson     reg.addr = (uintptr_t)&spapr_cpu->vpa_addr;
7759b00ea49SDavid Gibson     ret = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, &reg);
7769b00ea49SDavid Gibson     if (ret < 0) {
7778d83cbf1SGreg Kurz         trace_kvm_failed_vpa_addr_get(strerror(errno));
7789b00ea49SDavid Gibson         return ret;
7799b00ea49SDavid Gibson     }
7809b00ea49SDavid Gibson 
7817388efafSDavid Gibson     assert((uintptr_t)&spapr_cpu->slb_shadow_size
7827388efafSDavid Gibson            == ((uintptr_t)&spapr_cpu->slb_shadow_addr + 8));
7839b00ea49SDavid Gibson     reg.id = KVM_REG_PPC_VPA_SLB;
7847388efafSDavid Gibson     reg.addr = (uintptr_t)&spapr_cpu->slb_shadow_addr;
7859b00ea49SDavid Gibson     ret = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, &reg);
7869b00ea49SDavid Gibson     if (ret < 0) {
7878d83cbf1SGreg Kurz         trace_kvm_failed_slb_get(strerror(errno));
7889b00ea49SDavid Gibson         return ret;
7899b00ea49SDavid Gibson     }
7909b00ea49SDavid Gibson 
7917388efafSDavid Gibson     assert((uintptr_t)&spapr_cpu->dtl_size
7927388efafSDavid Gibson            == ((uintptr_t)&spapr_cpu->dtl_addr + 8));
7939b00ea49SDavid Gibson     reg.id = KVM_REG_PPC_VPA_DTL;
7947388efafSDavid Gibson     reg.addr = (uintptr_t)&spapr_cpu->dtl_addr;
7959b00ea49SDavid Gibson     ret = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, &reg);
7969b00ea49SDavid Gibson     if (ret < 0) {
7978d83cbf1SGreg Kurz         trace_kvm_failed_dtl_get(strerror(errno));
7989b00ea49SDavid Gibson         return ret;
7999b00ea49SDavid Gibson     }
8009b00ea49SDavid Gibson 
8019b00ea49SDavid Gibson     return 0;
8029b00ea49SDavid Gibson }
8039b00ea49SDavid Gibson 
8049b00ea49SDavid Gibson static int kvm_put_vpa(CPUState *cs)
8059b00ea49SDavid Gibson {
8069b00ea49SDavid Gibson     PowerPCCPU *cpu = POWERPC_CPU(cs);
807ce2918cbSDavid Gibson     SpaprCpuState *spapr_cpu = spapr_cpu_state(cpu);
8089b00ea49SDavid Gibson     struct kvm_one_reg reg;
8099b00ea49SDavid Gibson     int ret;
8109b00ea49SDavid Gibson 
811c995e942SDavid Gibson     /*
812c995e942SDavid Gibson      * SLB shadow or DTL can't be registered unless a master VPA is
8139b00ea49SDavid Gibson      * registered.  That means when restoring state, if a VPA *is*
8149b00ea49SDavid Gibson      * registered, we need to set that up first.  If not, we need to
815c995e942SDavid Gibson      * deregister the others before deregistering the master VPA
816c995e942SDavid Gibson      */
8177388efafSDavid Gibson     assert(spapr_cpu->vpa_addr
8187388efafSDavid Gibson            || !(spapr_cpu->slb_shadow_addr || spapr_cpu->dtl_addr));
8199b00ea49SDavid Gibson 
8207388efafSDavid Gibson     if (spapr_cpu->vpa_addr) {
8219b00ea49SDavid Gibson         reg.id = KVM_REG_PPC_VPA_ADDR;
8227388efafSDavid Gibson         reg.addr = (uintptr_t)&spapr_cpu->vpa_addr;
8239b00ea49SDavid Gibson         ret = kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, &reg);
8249b00ea49SDavid Gibson         if (ret < 0) {
8258d83cbf1SGreg Kurz             trace_kvm_failed_vpa_addr_set(strerror(errno));
8269b00ea49SDavid Gibson             return ret;
8279b00ea49SDavid Gibson         }
8289b00ea49SDavid Gibson     }
8299b00ea49SDavid Gibson 
8307388efafSDavid Gibson     assert((uintptr_t)&spapr_cpu->slb_shadow_size
8317388efafSDavid Gibson            == ((uintptr_t)&spapr_cpu->slb_shadow_addr + 8));
8329b00ea49SDavid Gibson     reg.id = KVM_REG_PPC_VPA_SLB;
8337388efafSDavid Gibson     reg.addr = (uintptr_t)&spapr_cpu->slb_shadow_addr;
8349b00ea49SDavid Gibson     ret = kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, &reg);
8359b00ea49SDavid Gibson     if (ret < 0) {
8368d83cbf1SGreg Kurz         trace_kvm_failed_slb_set(strerror(errno));
8379b00ea49SDavid Gibson         return ret;
8389b00ea49SDavid Gibson     }
8399b00ea49SDavid Gibson 
8407388efafSDavid Gibson     assert((uintptr_t)&spapr_cpu->dtl_size
8417388efafSDavid Gibson            == ((uintptr_t)&spapr_cpu->dtl_addr + 8));
8429b00ea49SDavid Gibson     reg.id = KVM_REG_PPC_VPA_DTL;
8437388efafSDavid Gibson     reg.addr = (uintptr_t)&spapr_cpu->dtl_addr;
8449b00ea49SDavid Gibson     ret = kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, &reg);
8459b00ea49SDavid Gibson     if (ret < 0) {
8468d83cbf1SGreg Kurz         trace_kvm_failed_dtl_set(strerror(errno));
8479b00ea49SDavid Gibson         return ret;
8489b00ea49SDavid Gibson     }
8499b00ea49SDavid Gibson 
8507388efafSDavid Gibson     if (!spapr_cpu->vpa_addr) {
8519b00ea49SDavid Gibson         reg.id = KVM_REG_PPC_VPA_ADDR;
8527388efafSDavid Gibson         reg.addr = (uintptr_t)&spapr_cpu->vpa_addr;
8539b00ea49SDavid Gibson         ret = kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, &reg);
8549b00ea49SDavid Gibson         if (ret < 0) {
8558d83cbf1SGreg Kurz             trace_kvm_failed_null_vpa_addr_set(strerror(errno));
8569b00ea49SDavid Gibson             return ret;
8579b00ea49SDavid Gibson         }
8589b00ea49SDavid Gibson     }
8599b00ea49SDavid Gibson 
8609b00ea49SDavid Gibson     return 0;
8619b00ea49SDavid Gibson }
8629b00ea49SDavid Gibson #endif /* TARGET_PPC64 */
8639b00ea49SDavid Gibson 
864e5c0d3ceSDavid Gibson int kvmppc_put_books_sregs(PowerPCCPU *cpu)
865a7a00a72SDavid Gibson {
866a7a00a72SDavid Gibson     CPUPPCState *env = &cpu->env;
867a7a00a72SDavid Gibson     struct kvm_sregs sregs;
868a7a00a72SDavid Gibson     int i;
869a7a00a72SDavid Gibson 
870a7a00a72SDavid Gibson     sregs.pvr = env->spr[SPR_PVR];
871a7a00a72SDavid Gibson 
8721ec26c75SGreg Kurz     if (cpu->vhyp) {
8731ec26c75SGreg Kurz         PPCVirtualHypervisorClass *vhc =
8741ec26c75SGreg Kurz             PPC_VIRTUAL_HYPERVISOR_GET_CLASS(cpu->vhyp);
8751ec26c75SGreg Kurz         sregs.u.s.sdr1 = vhc->encode_hpt_for_kvm_pr(cpu->vhyp);
8761ec26c75SGreg Kurz     } else {
877a7a00a72SDavid Gibson         sregs.u.s.sdr1 = env->spr[SPR_SDR1];
8781ec26c75SGreg Kurz     }
879a7a00a72SDavid Gibson 
880a7a00a72SDavid Gibson     /* Sync SLB */
881a7a00a72SDavid Gibson #ifdef TARGET_PPC64
882a7a00a72SDavid Gibson     for (i = 0; i < ARRAY_SIZE(env->slb); i++) {
883a7a00a72SDavid Gibson         sregs.u.s.ppc64.slb[i].slbe = env->slb[i].esid;
884a7a00a72SDavid Gibson         if (env->slb[i].esid & SLB_ESID_V) {
885a7a00a72SDavid Gibson             sregs.u.s.ppc64.slb[i].slbe |= i;
886a7a00a72SDavid Gibson         }
887a7a00a72SDavid Gibson         sregs.u.s.ppc64.slb[i].slbv = env->slb[i].vsid;
888a7a00a72SDavid Gibson     }
889a7a00a72SDavid Gibson #endif
890a7a00a72SDavid Gibson 
891a7a00a72SDavid Gibson     /* Sync SRs */
892a7a00a72SDavid Gibson     for (i = 0; i < 16; i++) {
893a7a00a72SDavid Gibson         sregs.u.s.ppc32.sr[i] = env->sr[i];
894a7a00a72SDavid Gibson     }
895a7a00a72SDavid Gibson 
896a7a00a72SDavid Gibson     /* Sync BATs */
897a7a00a72SDavid Gibson     for (i = 0; i < 8; i++) {
898a7a00a72SDavid Gibson         /* Beware. We have to swap upper and lower bits here */
899a7a00a72SDavid Gibson         sregs.u.s.ppc32.dbat[i] = ((uint64_t)env->DBAT[0][i] << 32)
900a7a00a72SDavid Gibson             | env->DBAT[1][i];
901a7a00a72SDavid Gibson         sregs.u.s.ppc32.ibat[i] = ((uint64_t)env->IBAT[0][i] << 32)
902a7a00a72SDavid Gibson             | env->IBAT[1][i];
903a7a00a72SDavid Gibson     }
904a7a00a72SDavid Gibson 
905a7a00a72SDavid Gibson     return kvm_vcpu_ioctl(CPU(cpu), KVM_SET_SREGS, &sregs);
906a7a00a72SDavid Gibson }
907a7a00a72SDavid Gibson 
90820d695a9SAndreas Färber int kvm_arch_put_registers(CPUState *cs, int level)
909d76d1650Saurel32 {
91020d695a9SAndreas Färber     PowerPCCPU *cpu = POWERPC_CPU(cs);
91120d695a9SAndreas Färber     CPUPPCState *env = &cpu->env;
912d76d1650Saurel32     struct kvm_regs regs;
913d76d1650Saurel32     int ret;
914d76d1650Saurel32     int i;
915d76d1650Saurel32 
9161bc22652SAndreas Färber     ret = kvm_vcpu_ioctl(cs, KVM_GET_REGS, &regs);
9171bc22652SAndreas Färber     if (ret < 0) {
918d76d1650Saurel32         return ret;
9191bc22652SAndreas Färber     }
920d76d1650Saurel32 
921d76d1650Saurel32     regs.ctr = env->ctr;
922d76d1650Saurel32     regs.lr  = env->lr;
923da91a00fSRichard Henderson     regs.xer = cpu_read_xer(env);
924d76d1650Saurel32     regs.msr = env->msr;
925d76d1650Saurel32     regs.pc = env->nip;
926d76d1650Saurel32 
927d76d1650Saurel32     regs.srr0 = env->spr[SPR_SRR0];
928d76d1650Saurel32     regs.srr1 = env->spr[SPR_SRR1];
929d76d1650Saurel32 
930d76d1650Saurel32     regs.sprg0 = env->spr[SPR_SPRG0];
931d76d1650Saurel32     regs.sprg1 = env->spr[SPR_SPRG1];
932d76d1650Saurel32     regs.sprg2 = env->spr[SPR_SPRG2];
933d76d1650Saurel32     regs.sprg3 = env->spr[SPR_SPRG3];
934d76d1650Saurel32     regs.sprg4 = env->spr[SPR_SPRG4];
935d76d1650Saurel32     regs.sprg5 = env->spr[SPR_SPRG5];
936d76d1650Saurel32     regs.sprg6 = env->spr[SPR_SPRG6];
937d76d1650Saurel32     regs.sprg7 = env->spr[SPR_SPRG7];
938d76d1650Saurel32 
93990dc8812SScott Wood     regs.pid = env->spr[SPR_BOOKE_PID];
94090dc8812SScott Wood 
941c995e942SDavid Gibson     for (i = 0; i < 32; i++) {
942d76d1650Saurel32         regs.gpr[i] = env->gpr[i];
943c995e942SDavid Gibson     }
944d76d1650Saurel32 
9454bddaf55SAlexey Kardashevskiy     regs.cr = 0;
9464bddaf55SAlexey Kardashevskiy     for (i = 0; i < 8; i++) {
9474bddaf55SAlexey Kardashevskiy         regs.cr |= (env->crf[i] & 15) << (4 * (7 - i));
9484bddaf55SAlexey Kardashevskiy     }
9494bddaf55SAlexey Kardashevskiy 
9501bc22652SAndreas Färber     ret = kvm_vcpu_ioctl(cs, KVM_SET_REGS, &regs);
951c995e942SDavid Gibson     if (ret < 0) {
952d76d1650Saurel32         return ret;
953c995e942SDavid Gibson     }
954d76d1650Saurel32 
95570b79849SDavid Gibson     kvm_put_fp(cs);
95670b79849SDavid Gibson 
95793dd5e85SScott Wood     if (env->tlb_dirty) {
9581bc22652SAndreas Färber         kvm_sw_tlb_put(cpu);
95993dd5e85SScott Wood         env->tlb_dirty = false;
96093dd5e85SScott Wood     }
96193dd5e85SScott Wood 
962f1af19d7SDavid Gibson     if (cap_segstate && (level >= KVM_PUT_RESET_STATE)) {
963a7a00a72SDavid Gibson         ret = kvmppc_put_books_sregs(cpu);
964a7a00a72SDavid Gibson         if (ret < 0) {
965f1af19d7SDavid Gibson             return ret;
966f1af19d7SDavid Gibson         }
967f1af19d7SDavid Gibson     }
968f1af19d7SDavid Gibson 
969f1af19d7SDavid Gibson     if (cap_hior && (level >= KVM_PUT_RESET_STATE)) {
970d67d40eaSDavid Gibson         kvm_put_one_spr(cs, KVM_REG_PPC_HIOR, SPR_HIOR);
971d67d40eaSDavid Gibson     }
972f1af19d7SDavid Gibson 
973d67d40eaSDavid Gibson     if (cap_one_reg) {
974d67d40eaSDavid Gibson         int i;
975d67d40eaSDavid Gibson 
976c995e942SDavid Gibson         /*
977c995e942SDavid Gibson          * We deliberately ignore errors here, for kernels which have
978d67d40eaSDavid Gibson          * the ONE_REG calls, but don't support the specific
979d67d40eaSDavid Gibson          * registers, there's a reasonable chance things will still
980c995e942SDavid Gibson          * work, at least until we try to migrate.
981c995e942SDavid Gibson          */
982d67d40eaSDavid Gibson         for (i = 0; i < 1024; i++) {
983d67d40eaSDavid Gibson             uint64_t id = env->spr_cb[i].one_reg_id;
984d67d40eaSDavid Gibson 
985d67d40eaSDavid Gibson             if (id != 0) {
986d67d40eaSDavid Gibson                 kvm_put_one_spr(cs, id, i);
987d67d40eaSDavid Gibson             }
988f1af19d7SDavid Gibson         }
9899b00ea49SDavid Gibson 
9909b00ea49SDavid Gibson #ifdef TARGET_PPC64
99180b3f79bSAlexey Kardashevskiy         if (msr_ts) {
99280b3f79bSAlexey Kardashevskiy             for (i = 0; i < ARRAY_SIZE(env->tm_gpr); i++) {
99380b3f79bSAlexey Kardashevskiy                 kvm_set_one_reg(cs, KVM_REG_PPC_TM_GPR(i), &env->tm_gpr[i]);
99480b3f79bSAlexey Kardashevskiy             }
99580b3f79bSAlexey Kardashevskiy             for (i = 0; i < ARRAY_SIZE(env->tm_vsr); i++) {
99680b3f79bSAlexey Kardashevskiy                 kvm_set_one_reg(cs, KVM_REG_PPC_TM_VSR(i), &env->tm_vsr[i]);
99780b3f79bSAlexey Kardashevskiy             }
99880b3f79bSAlexey Kardashevskiy             kvm_set_one_reg(cs, KVM_REG_PPC_TM_CR, &env->tm_cr);
99980b3f79bSAlexey Kardashevskiy             kvm_set_one_reg(cs, KVM_REG_PPC_TM_LR, &env->tm_lr);
100080b3f79bSAlexey Kardashevskiy             kvm_set_one_reg(cs, KVM_REG_PPC_TM_CTR, &env->tm_ctr);
100180b3f79bSAlexey Kardashevskiy             kvm_set_one_reg(cs, KVM_REG_PPC_TM_FPSCR, &env->tm_fpscr);
100280b3f79bSAlexey Kardashevskiy             kvm_set_one_reg(cs, KVM_REG_PPC_TM_AMR, &env->tm_amr);
100380b3f79bSAlexey Kardashevskiy             kvm_set_one_reg(cs, KVM_REG_PPC_TM_PPR, &env->tm_ppr);
100480b3f79bSAlexey Kardashevskiy             kvm_set_one_reg(cs, KVM_REG_PPC_TM_VRSAVE, &env->tm_vrsave);
100580b3f79bSAlexey Kardashevskiy             kvm_set_one_reg(cs, KVM_REG_PPC_TM_VSCR, &env->tm_vscr);
100680b3f79bSAlexey Kardashevskiy             kvm_set_one_reg(cs, KVM_REG_PPC_TM_DSCR, &env->tm_dscr);
100780b3f79bSAlexey Kardashevskiy             kvm_set_one_reg(cs, KVM_REG_PPC_TM_TAR, &env->tm_tar);
100880b3f79bSAlexey Kardashevskiy         }
100980b3f79bSAlexey Kardashevskiy 
10109b00ea49SDavid Gibson         if (cap_papr) {
10119b00ea49SDavid Gibson             if (kvm_put_vpa(cs) < 0) {
10128d83cbf1SGreg Kurz                 trace_kvm_failed_put_vpa();
10139b00ea49SDavid Gibson             }
10149b00ea49SDavid Gibson         }
101598a8b524SAlexey Kardashevskiy 
101698a8b524SAlexey Kardashevskiy         kvm_set_one_reg(cs, KVM_REG_PPC_TB_OFFSET, &env->tb_env->tb_offset);
10179b00ea49SDavid Gibson #endif /* TARGET_PPC64 */
1018f1af19d7SDavid Gibson     }
1019f1af19d7SDavid Gibson 
1020d76d1650Saurel32     return ret;
1021d76d1650Saurel32 }
1022d76d1650Saurel32 
1023c371c2e3SBharat Bhushan static void kvm_sync_excp(CPUPPCState *env, int vector, int ivor)
1024c371c2e3SBharat Bhushan {
1025c371c2e3SBharat Bhushan      env->excp_vectors[vector] = env->spr[ivor] + env->spr[SPR_BOOKE_IVPR];
1026c371c2e3SBharat Bhushan }
1027c371c2e3SBharat Bhushan 
1028a7a00a72SDavid Gibson static int kvmppc_get_booke_sregs(PowerPCCPU *cpu)
1029d76d1650Saurel32 {
103020d695a9SAndreas Färber     CPUPPCState *env = &cpu->env;
1031ba5e5090SAlexander Graf     struct kvm_sregs sregs;
1032a7a00a72SDavid Gibson     int ret;
1033d76d1650Saurel32 
1034a7a00a72SDavid Gibson     ret = kvm_vcpu_ioctl(CPU(cpu), KVM_GET_SREGS, &sregs);
103590dc8812SScott Wood     if (ret < 0) {
103690dc8812SScott Wood         return ret;
103790dc8812SScott Wood     }
103890dc8812SScott Wood 
103990dc8812SScott Wood     if (sregs.u.e.features & KVM_SREGS_E_BASE) {
104090dc8812SScott Wood         env->spr[SPR_BOOKE_CSRR0] = sregs.u.e.csrr0;
104190dc8812SScott Wood         env->spr[SPR_BOOKE_CSRR1] = sregs.u.e.csrr1;
104290dc8812SScott Wood         env->spr[SPR_BOOKE_ESR] = sregs.u.e.esr;
104390dc8812SScott Wood         env->spr[SPR_BOOKE_DEAR] = sregs.u.e.dear;
104490dc8812SScott Wood         env->spr[SPR_BOOKE_MCSR] = sregs.u.e.mcsr;
104590dc8812SScott Wood         env->spr[SPR_BOOKE_TSR] = sregs.u.e.tsr;
104690dc8812SScott Wood         env->spr[SPR_BOOKE_TCR] = sregs.u.e.tcr;
104790dc8812SScott Wood         env->spr[SPR_DECR] = sregs.u.e.dec;
104890dc8812SScott Wood         env->spr[SPR_TBL] = sregs.u.e.tb & 0xffffffff;
104990dc8812SScott Wood         env->spr[SPR_TBU] = sregs.u.e.tb >> 32;
105090dc8812SScott Wood         env->spr[SPR_VRSAVE] = sregs.u.e.vrsave;
105190dc8812SScott Wood     }
105290dc8812SScott Wood 
105390dc8812SScott Wood     if (sregs.u.e.features & KVM_SREGS_E_ARCH206) {
105490dc8812SScott Wood         env->spr[SPR_BOOKE_PIR] = sregs.u.e.pir;
105590dc8812SScott Wood         env->spr[SPR_BOOKE_MCSRR0] = sregs.u.e.mcsrr0;
105690dc8812SScott Wood         env->spr[SPR_BOOKE_MCSRR1] = sregs.u.e.mcsrr1;
105790dc8812SScott Wood         env->spr[SPR_BOOKE_DECAR] = sregs.u.e.decar;
105890dc8812SScott Wood         env->spr[SPR_BOOKE_IVPR] = sregs.u.e.ivpr;
105990dc8812SScott Wood     }
106090dc8812SScott Wood 
106190dc8812SScott Wood     if (sregs.u.e.features & KVM_SREGS_E_64) {
106290dc8812SScott Wood         env->spr[SPR_BOOKE_EPCR] = sregs.u.e.epcr;
106390dc8812SScott Wood     }
106490dc8812SScott Wood 
106590dc8812SScott Wood     if (sregs.u.e.features & KVM_SREGS_E_SPRG8) {
106690dc8812SScott Wood         env->spr[SPR_BOOKE_SPRG8] = sregs.u.e.sprg8;
106790dc8812SScott Wood     }
106890dc8812SScott Wood 
106990dc8812SScott Wood     if (sregs.u.e.features & KVM_SREGS_E_IVOR) {
107090dc8812SScott Wood         env->spr[SPR_BOOKE_IVOR0] = sregs.u.e.ivor_low[0];
1071c371c2e3SBharat Bhushan         kvm_sync_excp(env, POWERPC_EXCP_CRITICAL,  SPR_BOOKE_IVOR0);
107290dc8812SScott Wood         env->spr[SPR_BOOKE_IVOR1] = sregs.u.e.ivor_low[1];
1073c371c2e3SBharat Bhushan         kvm_sync_excp(env, POWERPC_EXCP_MCHECK,  SPR_BOOKE_IVOR1);
107490dc8812SScott Wood         env->spr[SPR_BOOKE_IVOR2] = sregs.u.e.ivor_low[2];
1075c371c2e3SBharat Bhushan         kvm_sync_excp(env, POWERPC_EXCP_DSI,  SPR_BOOKE_IVOR2);
107690dc8812SScott Wood         env->spr[SPR_BOOKE_IVOR3] = sregs.u.e.ivor_low[3];
1077c371c2e3SBharat Bhushan         kvm_sync_excp(env, POWERPC_EXCP_ISI,  SPR_BOOKE_IVOR3);
107890dc8812SScott Wood         env->spr[SPR_BOOKE_IVOR4] = sregs.u.e.ivor_low[4];
1079c371c2e3SBharat Bhushan         kvm_sync_excp(env, POWERPC_EXCP_EXTERNAL,  SPR_BOOKE_IVOR4);
108090dc8812SScott Wood         env->spr[SPR_BOOKE_IVOR5] = sregs.u.e.ivor_low[5];
1081c371c2e3SBharat Bhushan         kvm_sync_excp(env, POWERPC_EXCP_ALIGN,  SPR_BOOKE_IVOR5);
108290dc8812SScott Wood         env->spr[SPR_BOOKE_IVOR6] = sregs.u.e.ivor_low[6];
1083c371c2e3SBharat Bhushan         kvm_sync_excp(env, POWERPC_EXCP_PROGRAM,  SPR_BOOKE_IVOR6);
108490dc8812SScott Wood         env->spr[SPR_BOOKE_IVOR7] = sregs.u.e.ivor_low[7];
1085c371c2e3SBharat Bhushan         kvm_sync_excp(env, POWERPC_EXCP_FPU,  SPR_BOOKE_IVOR7);
108690dc8812SScott Wood         env->spr[SPR_BOOKE_IVOR8] = sregs.u.e.ivor_low[8];
1087c371c2e3SBharat Bhushan         kvm_sync_excp(env, POWERPC_EXCP_SYSCALL,  SPR_BOOKE_IVOR8);
108890dc8812SScott Wood         env->spr[SPR_BOOKE_IVOR9] = sregs.u.e.ivor_low[9];
1089c371c2e3SBharat Bhushan         kvm_sync_excp(env, POWERPC_EXCP_APU,  SPR_BOOKE_IVOR9);
109090dc8812SScott Wood         env->spr[SPR_BOOKE_IVOR10] = sregs.u.e.ivor_low[10];
1091c371c2e3SBharat Bhushan         kvm_sync_excp(env, POWERPC_EXCP_DECR,  SPR_BOOKE_IVOR10);
109290dc8812SScott Wood         env->spr[SPR_BOOKE_IVOR11] = sregs.u.e.ivor_low[11];
1093c371c2e3SBharat Bhushan         kvm_sync_excp(env, POWERPC_EXCP_FIT,  SPR_BOOKE_IVOR11);
109490dc8812SScott Wood         env->spr[SPR_BOOKE_IVOR12] = sregs.u.e.ivor_low[12];
1095c371c2e3SBharat Bhushan         kvm_sync_excp(env, POWERPC_EXCP_WDT,  SPR_BOOKE_IVOR12);
109690dc8812SScott Wood         env->spr[SPR_BOOKE_IVOR13] = sregs.u.e.ivor_low[13];
1097c371c2e3SBharat Bhushan         kvm_sync_excp(env, POWERPC_EXCP_DTLB,  SPR_BOOKE_IVOR13);
109890dc8812SScott Wood         env->spr[SPR_BOOKE_IVOR14] = sregs.u.e.ivor_low[14];
1099c371c2e3SBharat Bhushan         kvm_sync_excp(env, POWERPC_EXCP_ITLB,  SPR_BOOKE_IVOR14);
110090dc8812SScott Wood         env->spr[SPR_BOOKE_IVOR15] = sregs.u.e.ivor_low[15];
1101c371c2e3SBharat Bhushan         kvm_sync_excp(env, POWERPC_EXCP_DEBUG,  SPR_BOOKE_IVOR15);
110290dc8812SScott Wood 
110390dc8812SScott Wood         if (sregs.u.e.features & KVM_SREGS_E_SPE) {
110490dc8812SScott Wood             env->spr[SPR_BOOKE_IVOR32] = sregs.u.e.ivor_high[0];
1105c371c2e3SBharat Bhushan             kvm_sync_excp(env, POWERPC_EXCP_SPEU,  SPR_BOOKE_IVOR32);
110690dc8812SScott Wood             env->spr[SPR_BOOKE_IVOR33] = sregs.u.e.ivor_high[1];
1107c371c2e3SBharat Bhushan             kvm_sync_excp(env, POWERPC_EXCP_EFPDI,  SPR_BOOKE_IVOR33);
110890dc8812SScott Wood             env->spr[SPR_BOOKE_IVOR34] = sregs.u.e.ivor_high[2];
1109c371c2e3SBharat Bhushan             kvm_sync_excp(env, POWERPC_EXCP_EFPRI,  SPR_BOOKE_IVOR34);
111090dc8812SScott Wood         }
111190dc8812SScott Wood 
111290dc8812SScott Wood         if (sregs.u.e.features & KVM_SREGS_E_PM) {
111390dc8812SScott Wood             env->spr[SPR_BOOKE_IVOR35] = sregs.u.e.ivor_high[3];
1114c371c2e3SBharat Bhushan             kvm_sync_excp(env, POWERPC_EXCP_EPERFM,  SPR_BOOKE_IVOR35);
111590dc8812SScott Wood         }
111690dc8812SScott Wood 
111790dc8812SScott Wood         if (sregs.u.e.features & KVM_SREGS_E_PC) {
111890dc8812SScott Wood             env->spr[SPR_BOOKE_IVOR36] = sregs.u.e.ivor_high[4];
1119c371c2e3SBharat Bhushan             kvm_sync_excp(env, POWERPC_EXCP_DOORI,  SPR_BOOKE_IVOR36);
112090dc8812SScott Wood             env->spr[SPR_BOOKE_IVOR37] = sregs.u.e.ivor_high[5];
1121c371c2e3SBharat Bhushan             kvm_sync_excp(env, POWERPC_EXCP_DOORCI, SPR_BOOKE_IVOR37);
112290dc8812SScott Wood         }
112390dc8812SScott Wood     }
112490dc8812SScott Wood 
112590dc8812SScott Wood     if (sregs.u.e.features & KVM_SREGS_E_ARCH206_MMU) {
112690dc8812SScott Wood         env->spr[SPR_BOOKE_MAS0] = sregs.u.e.mas0;
112790dc8812SScott Wood         env->spr[SPR_BOOKE_MAS1] = sregs.u.e.mas1;
112890dc8812SScott Wood         env->spr[SPR_BOOKE_MAS2] = sregs.u.e.mas2;
112990dc8812SScott Wood         env->spr[SPR_BOOKE_MAS3] = sregs.u.e.mas7_3 & 0xffffffff;
113090dc8812SScott Wood         env->spr[SPR_BOOKE_MAS4] = sregs.u.e.mas4;
113190dc8812SScott Wood         env->spr[SPR_BOOKE_MAS6] = sregs.u.e.mas6;
113290dc8812SScott Wood         env->spr[SPR_BOOKE_MAS7] = sregs.u.e.mas7_3 >> 32;
113390dc8812SScott Wood         env->spr[SPR_MMUCFG] = sregs.u.e.mmucfg;
113490dc8812SScott Wood         env->spr[SPR_BOOKE_TLB0CFG] = sregs.u.e.tlbcfg[0];
113590dc8812SScott Wood         env->spr[SPR_BOOKE_TLB1CFG] = sregs.u.e.tlbcfg[1];
113690dc8812SScott Wood     }
113790dc8812SScott Wood 
113890dc8812SScott Wood     if (sregs.u.e.features & KVM_SREGS_EXP) {
113990dc8812SScott Wood         env->spr[SPR_BOOKE_EPR] = sregs.u.e.epr;
114090dc8812SScott Wood     }
114190dc8812SScott Wood 
114290dc8812SScott Wood     if (sregs.u.e.features & KVM_SREGS_E_PD) {
114390dc8812SScott Wood         env->spr[SPR_BOOKE_EPLC] = sregs.u.e.eplc;
114490dc8812SScott Wood         env->spr[SPR_BOOKE_EPSC] = sregs.u.e.epsc;
114590dc8812SScott Wood     }
114690dc8812SScott Wood 
114790dc8812SScott Wood     if (sregs.u.e.impl_id == KVM_SREGS_E_IMPL_FSL) {
114890dc8812SScott Wood         env->spr[SPR_E500_SVR] = sregs.u.e.impl.fsl.svr;
114990dc8812SScott Wood         env->spr[SPR_Exxx_MCAR] = sregs.u.e.impl.fsl.mcar;
115090dc8812SScott Wood         env->spr[SPR_HID0] = sregs.u.e.impl.fsl.hid0;
115190dc8812SScott Wood 
115290dc8812SScott Wood         if (sregs.u.e.impl.fsl.features & KVM_SREGS_E_FSL_PIDn) {
115390dc8812SScott Wood             env->spr[SPR_BOOKE_PID1] = sregs.u.e.impl.fsl.pid1;
115490dc8812SScott Wood             env->spr[SPR_BOOKE_PID2] = sregs.u.e.impl.fsl.pid2;
115590dc8812SScott Wood         }
115690dc8812SScott Wood     }
1157a7a00a72SDavid Gibson 
1158a7a00a72SDavid Gibson     return 0;
1159fafc0b6aSAlexander Graf }
116090dc8812SScott Wood 
1161a7a00a72SDavid Gibson static int kvmppc_get_books_sregs(PowerPCCPU *cpu)
1162a7a00a72SDavid Gibson {
1163a7a00a72SDavid Gibson     CPUPPCState *env = &cpu->env;
1164a7a00a72SDavid Gibson     struct kvm_sregs sregs;
1165a7a00a72SDavid Gibson     int ret;
1166a7a00a72SDavid Gibson     int i;
1167a7a00a72SDavid Gibson 
1168a7a00a72SDavid Gibson     ret = kvm_vcpu_ioctl(CPU(cpu), KVM_GET_SREGS, &sregs);
116990dc8812SScott Wood     if (ret < 0) {
117090dc8812SScott Wood         return ret;
117190dc8812SScott Wood     }
117290dc8812SScott Wood 
1173e57ca75cSDavid Gibson     if (!cpu->vhyp) {
1174bb593904SDavid Gibson         ppc_store_sdr1(env, sregs.u.s.sdr1);
1175f3c75d42SAneesh Kumar K.V     }
1176ba5e5090SAlexander Graf 
1177ba5e5090SAlexander Graf     /* Sync SLB */
117882c09f2fSAlexander Graf #ifdef TARGET_PPC64
11794b4d4a21SAneesh Kumar K.V     /*
11804b4d4a21SAneesh Kumar K.V      * The packed SLB array we get from KVM_GET_SREGS only contains
1181a7a00a72SDavid Gibson      * information about valid entries. So we flush our internal copy
1182a7a00a72SDavid Gibson      * to get rid of stale ones, then put all valid SLB entries back
1183a7a00a72SDavid Gibson      * in.
11844b4d4a21SAneesh Kumar K.V      */
11854b4d4a21SAneesh Kumar K.V     memset(env->slb, 0, sizeof(env->slb));
1186d83af167SAneesh Kumar K.V     for (i = 0; i < ARRAY_SIZE(env->slb); i++) {
11874b4d4a21SAneesh Kumar K.V         target_ulong rb = sregs.u.s.ppc64.slb[i].slbe;
11884b4d4a21SAneesh Kumar K.V         target_ulong rs = sregs.u.s.ppc64.slb[i].slbv;
11894b4d4a21SAneesh Kumar K.V         /*
11904b4d4a21SAneesh Kumar K.V          * Only restore valid entries
11914b4d4a21SAneesh Kumar K.V          */
11924b4d4a21SAneesh Kumar K.V         if (rb & SLB_ESID_V) {
1193bcd81230SDavid Gibson             ppc_store_slb(cpu, rb & 0xfff, rb & ~0xfffULL, rs);
11944b4d4a21SAneesh Kumar K.V         }
1195ba5e5090SAlexander Graf     }
119682c09f2fSAlexander Graf #endif
1197ba5e5090SAlexander Graf 
1198ba5e5090SAlexander Graf     /* Sync SRs */
1199ba5e5090SAlexander Graf     for (i = 0; i < 16; i++) {
1200ba5e5090SAlexander Graf         env->sr[i] = sregs.u.s.ppc32.sr[i];
1201ba5e5090SAlexander Graf     }
1202ba5e5090SAlexander Graf 
1203ba5e5090SAlexander Graf     /* Sync BATs */
1204ba5e5090SAlexander Graf     for (i = 0; i < 8; i++) {
1205ba5e5090SAlexander Graf         env->DBAT[0][i] = sregs.u.s.ppc32.dbat[i] & 0xffffffff;
1206ba5e5090SAlexander Graf         env->DBAT[1][i] = sregs.u.s.ppc32.dbat[i] >> 32;
1207ba5e5090SAlexander Graf         env->IBAT[0][i] = sregs.u.s.ppc32.ibat[i] & 0xffffffff;
1208ba5e5090SAlexander Graf         env->IBAT[1][i] = sregs.u.s.ppc32.ibat[i] >> 32;
1209ba5e5090SAlexander Graf     }
1210a7a00a72SDavid Gibson 
1211a7a00a72SDavid Gibson     return 0;
1212a7a00a72SDavid Gibson }
1213a7a00a72SDavid Gibson 
1214a7a00a72SDavid Gibson int kvm_arch_get_registers(CPUState *cs)
1215a7a00a72SDavid Gibson {
1216a7a00a72SDavid Gibson     PowerPCCPU *cpu = POWERPC_CPU(cs);
1217a7a00a72SDavid Gibson     CPUPPCState *env = &cpu->env;
1218a7a00a72SDavid Gibson     struct kvm_regs regs;
1219a7a00a72SDavid Gibson     uint32_t cr;
1220a7a00a72SDavid Gibson     int i, ret;
1221a7a00a72SDavid Gibson 
1222a7a00a72SDavid Gibson     ret = kvm_vcpu_ioctl(cs, KVM_GET_REGS, &regs);
1223c995e942SDavid Gibson     if (ret < 0) {
1224a7a00a72SDavid Gibson         return ret;
1225c995e942SDavid Gibson     }
1226a7a00a72SDavid Gibson 
1227a7a00a72SDavid Gibson     cr = regs.cr;
1228a7a00a72SDavid Gibson     for (i = 7; i >= 0; i--) {
1229a7a00a72SDavid Gibson         env->crf[i] = cr & 15;
1230a7a00a72SDavid Gibson         cr >>= 4;
1231a7a00a72SDavid Gibson     }
1232a7a00a72SDavid Gibson 
1233a7a00a72SDavid Gibson     env->ctr = regs.ctr;
1234a7a00a72SDavid Gibson     env->lr = regs.lr;
1235a7a00a72SDavid Gibson     cpu_write_xer(env, regs.xer);
1236a7a00a72SDavid Gibson     env->msr = regs.msr;
1237a7a00a72SDavid Gibson     env->nip = regs.pc;
1238a7a00a72SDavid Gibson 
1239a7a00a72SDavid Gibson     env->spr[SPR_SRR0] = regs.srr0;
1240a7a00a72SDavid Gibson     env->spr[SPR_SRR1] = regs.srr1;
1241a7a00a72SDavid Gibson 
1242a7a00a72SDavid Gibson     env->spr[SPR_SPRG0] = regs.sprg0;
1243a7a00a72SDavid Gibson     env->spr[SPR_SPRG1] = regs.sprg1;
1244a7a00a72SDavid Gibson     env->spr[SPR_SPRG2] = regs.sprg2;
1245a7a00a72SDavid Gibson     env->spr[SPR_SPRG3] = regs.sprg3;
1246a7a00a72SDavid Gibson     env->spr[SPR_SPRG4] = regs.sprg4;
1247a7a00a72SDavid Gibson     env->spr[SPR_SPRG5] = regs.sprg5;
1248a7a00a72SDavid Gibson     env->spr[SPR_SPRG6] = regs.sprg6;
1249a7a00a72SDavid Gibson     env->spr[SPR_SPRG7] = regs.sprg7;
1250a7a00a72SDavid Gibson 
1251a7a00a72SDavid Gibson     env->spr[SPR_BOOKE_PID] = regs.pid;
1252a7a00a72SDavid Gibson 
1253c995e942SDavid Gibson     for (i = 0; i < 32; i++) {
1254a7a00a72SDavid Gibson         env->gpr[i] = regs.gpr[i];
1255c995e942SDavid Gibson     }
1256a7a00a72SDavid Gibson 
1257a7a00a72SDavid Gibson     kvm_get_fp(cs);
1258a7a00a72SDavid Gibson 
1259a7a00a72SDavid Gibson     if (cap_booke_sregs) {
1260a7a00a72SDavid Gibson         ret = kvmppc_get_booke_sregs(cpu);
1261a7a00a72SDavid Gibson         if (ret < 0) {
1262a7a00a72SDavid Gibson             return ret;
1263a7a00a72SDavid Gibson         }
1264a7a00a72SDavid Gibson     }
1265a7a00a72SDavid Gibson 
1266a7a00a72SDavid Gibson     if (cap_segstate) {
1267a7a00a72SDavid Gibson         ret = kvmppc_get_books_sregs(cpu);
1268a7a00a72SDavid Gibson         if (ret < 0) {
1269a7a00a72SDavid Gibson             return ret;
1270a7a00a72SDavid Gibson         }
1271fafc0b6aSAlexander Graf     }
1272ba5e5090SAlexander Graf 
1273d67d40eaSDavid Gibson     if (cap_hior) {
1274d67d40eaSDavid Gibson         kvm_get_one_spr(cs, KVM_REG_PPC_HIOR, SPR_HIOR);
1275d67d40eaSDavid Gibson     }
1276d67d40eaSDavid Gibson 
1277d67d40eaSDavid Gibson     if (cap_one_reg) {
1278d67d40eaSDavid Gibson         int i;
1279d67d40eaSDavid Gibson 
1280c995e942SDavid Gibson         /*
1281c995e942SDavid Gibson          * We deliberately ignore errors here, for kernels which have
1282d67d40eaSDavid Gibson          * the ONE_REG calls, but don't support the specific
1283d67d40eaSDavid Gibson          * registers, there's a reasonable chance things will still
1284c995e942SDavid Gibson          * work, at least until we try to migrate.
1285c995e942SDavid Gibson          */
1286d67d40eaSDavid Gibson         for (i = 0; i < 1024; i++) {
1287d67d40eaSDavid Gibson             uint64_t id = env->spr_cb[i].one_reg_id;
1288d67d40eaSDavid Gibson 
1289d67d40eaSDavid Gibson             if (id != 0) {
1290d67d40eaSDavid Gibson                 kvm_get_one_spr(cs, id, i);
1291d67d40eaSDavid Gibson             }
1292d67d40eaSDavid Gibson         }
12939b00ea49SDavid Gibson 
12949b00ea49SDavid Gibson #ifdef TARGET_PPC64
129580b3f79bSAlexey Kardashevskiy         if (msr_ts) {
129680b3f79bSAlexey Kardashevskiy             for (i = 0; i < ARRAY_SIZE(env->tm_gpr); i++) {
129780b3f79bSAlexey Kardashevskiy                 kvm_get_one_reg(cs, KVM_REG_PPC_TM_GPR(i), &env->tm_gpr[i]);
129880b3f79bSAlexey Kardashevskiy             }
129980b3f79bSAlexey Kardashevskiy             for (i = 0; i < ARRAY_SIZE(env->tm_vsr); i++) {
130080b3f79bSAlexey Kardashevskiy                 kvm_get_one_reg(cs, KVM_REG_PPC_TM_VSR(i), &env->tm_vsr[i]);
130180b3f79bSAlexey Kardashevskiy             }
130280b3f79bSAlexey Kardashevskiy             kvm_get_one_reg(cs, KVM_REG_PPC_TM_CR, &env->tm_cr);
130380b3f79bSAlexey Kardashevskiy             kvm_get_one_reg(cs, KVM_REG_PPC_TM_LR, &env->tm_lr);
130480b3f79bSAlexey Kardashevskiy             kvm_get_one_reg(cs, KVM_REG_PPC_TM_CTR, &env->tm_ctr);
130580b3f79bSAlexey Kardashevskiy             kvm_get_one_reg(cs, KVM_REG_PPC_TM_FPSCR, &env->tm_fpscr);
130680b3f79bSAlexey Kardashevskiy             kvm_get_one_reg(cs, KVM_REG_PPC_TM_AMR, &env->tm_amr);
130780b3f79bSAlexey Kardashevskiy             kvm_get_one_reg(cs, KVM_REG_PPC_TM_PPR, &env->tm_ppr);
130880b3f79bSAlexey Kardashevskiy             kvm_get_one_reg(cs, KVM_REG_PPC_TM_VRSAVE, &env->tm_vrsave);
130980b3f79bSAlexey Kardashevskiy             kvm_get_one_reg(cs, KVM_REG_PPC_TM_VSCR, &env->tm_vscr);
131080b3f79bSAlexey Kardashevskiy             kvm_get_one_reg(cs, KVM_REG_PPC_TM_DSCR, &env->tm_dscr);
131180b3f79bSAlexey Kardashevskiy             kvm_get_one_reg(cs, KVM_REG_PPC_TM_TAR, &env->tm_tar);
131280b3f79bSAlexey Kardashevskiy         }
131380b3f79bSAlexey Kardashevskiy 
13149b00ea49SDavid Gibson         if (cap_papr) {
13159b00ea49SDavid Gibson             if (kvm_get_vpa(cs) < 0) {
13168d83cbf1SGreg Kurz                 trace_kvm_failed_get_vpa();
13179b00ea49SDavid Gibson             }
13189b00ea49SDavid Gibson         }
131998a8b524SAlexey Kardashevskiy 
132098a8b524SAlexey Kardashevskiy         kvm_get_one_reg(cs, KVM_REG_PPC_TB_OFFSET, &env->tb_env->tb_offset);
13219b00ea49SDavid Gibson #endif
1322d67d40eaSDavid Gibson     }
1323d67d40eaSDavid Gibson 
1324d76d1650Saurel32     return 0;
1325d76d1650Saurel32 }
1326d76d1650Saurel32 
13271bc22652SAndreas Färber int kvmppc_set_interrupt(PowerPCCPU *cpu, int irq, int level)
1328fc87e185SAlexander Graf {
1329fc87e185SAlexander Graf     unsigned virq = level ? KVM_INTERRUPT_SET_LEVEL : KVM_INTERRUPT_UNSET;
1330fc87e185SAlexander Graf 
1331fc87e185SAlexander Graf     if (irq != PPC_INTERRUPT_EXT) {
1332fc87e185SAlexander Graf         return 0;
1333fc87e185SAlexander Graf     }
1334fc87e185SAlexander Graf 
1335fc87e185SAlexander Graf     if (!kvm_enabled() || !cap_interrupt_unset || !cap_interrupt_level) {
1336fc87e185SAlexander Graf         return 0;
1337fc87e185SAlexander Graf     }
1338fc87e185SAlexander Graf 
13391bc22652SAndreas Färber     kvm_vcpu_ioctl(CPU(cpu), KVM_INTERRUPT, &virq);
1340fc87e185SAlexander Graf 
1341fc87e185SAlexander Graf     return 0;
1342fc87e185SAlexander Graf }
1343fc87e185SAlexander Graf 
1344a69dc537SThomas Huth #if defined(TARGET_PPC64)
134516415335SAlexander Graf #define PPC_INPUT_INT PPC970_INPUT_INT
134616415335SAlexander Graf #else
134716415335SAlexander Graf #define PPC_INPUT_INT PPC6xx_INPUT_INT
134816415335SAlexander Graf #endif
134916415335SAlexander Graf 
135020d695a9SAndreas Färber void kvm_arch_pre_run(CPUState *cs, struct kvm_run *run)
1351d76d1650Saurel32 {
135220d695a9SAndreas Färber     PowerPCCPU *cpu = POWERPC_CPU(cs);
135320d695a9SAndreas Färber     CPUPPCState *env = &cpu->env;
1354d76d1650Saurel32     int r;
1355d76d1650Saurel32     unsigned irq;
1356d76d1650Saurel32 
13574b8523eeSJan Kiszka     qemu_mutex_lock_iothread();
13584b8523eeSJan Kiszka 
1359c995e942SDavid Gibson     /*
1360c995e942SDavid Gibson      * PowerPC QEMU tracks the various core input pins (interrupt,
1361c995e942SDavid Gibson      * critical interrupt, reset, etc) in PPC-specific
1362c995e942SDavid Gibson      * env->irq_input_state.
1363c995e942SDavid Gibson      */
1364fc87e185SAlexander Graf     if (!cap_interrupt_level &&
1365fc87e185SAlexander Graf         run->ready_for_interrupt_injection &&
1366259186a7SAndreas Färber         (cs->interrupt_request & CPU_INTERRUPT_HARD) &&
136716415335SAlexander Graf         (env->irq_input_state & (1 << PPC_INPUT_INT)))
1368d76d1650Saurel32     {
1369c995e942SDavid Gibson         /*
1370c995e942SDavid Gibson          * For now KVM disregards the 'irq' argument. However, in the
1371c995e942SDavid Gibson          * future KVM could cache it in-kernel to avoid a heavyweight
1372c995e942SDavid Gibson          * exit when reading the UIC.
1373d76d1650Saurel32          */
1374fc87e185SAlexander Graf         irq = KVM_INTERRUPT_SET;
1375d76d1650Saurel32 
13768d83cbf1SGreg Kurz         trace_kvm_injected_interrupt(irq);
13771bc22652SAndreas Färber         r = kvm_vcpu_ioctl(cs, KVM_INTERRUPT, &irq);
137855e5c285SAndreas Färber         if (r < 0) {
137955e5c285SAndreas Färber             printf("cpu %d fail inject %x\n", cs->cpu_index, irq);
138055e5c285SAndreas Färber         }
1381c821c2bdSAlexander Graf 
1382c821c2bdSAlexander Graf         /* Always wake up soon in case the interrupt was level based */
1383bc72ad67SAlex Bligh         timer_mod(idle_timer, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) +
138473bcb24dSRutuja Shah                        (NANOSECONDS_PER_SECOND / 50));
1385d76d1650Saurel32     }
1386d76d1650Saurel32 
1387c995e942SDavid Gibson     /*
1388c995e942SDavid Gibson      * We don't know if there are more interrupts pending after
1389c995e942SDavid Gibson      * this. However, the guest will return to userspace in the course
1390c995e942SDavid Gibson      * of handling this one anyways, so we will get a chance to
1391c995e942SDavid Gibson      * deliver the rest.
1392c995e942SDavid Gibson      */
13934b8523eeSJan Kiszka 
13944b8523eeSJan Kiszka     qemu_mutex_unlock_iothread();
1395d76d1650Saurel32 }
1396d76d1650Saurel32 
13974c663752SPaolo Bonzini MemTxAttrs kvm_arch_post_run(CPUState *cs, struct kvm_run *run)
1398d76d1650Saurel32 {
13994c663752SPaolo Bonzini     return MEMTXATTRS_UNSPECIFIED;
1400d76d1650Saurel32 }
1401d76d1650Saurel32 
140220d695a9SAndreas Färber int kvm_arch_process_async_events(CPUState *cs)
14030af691d7SMarcelo Tosatti {
1404259186a7SAndreas Färber     return cs->halted;
14050af691d7SMarcelo Tosatti }
14060af691d7SMarcelo Tosatti 
1407259186a7SAndreas Färber static int kvmppc_handle_halt(PowerPCCPU *cpu)
1408d76d1650Saurel32 {
1409259186a7SAndreas Färber     CPUState *cs = CPU(cpu);
1410259186a7SAndreas Färber     CPUPPCState *env = &cpu->env;
1411259186a7SAndreas Färber 
1412259186a7SAndreas Färber     if (!(cs->interrupt_request & CPU_INTERRUPT_HARD) && (msr_ee)) {
1413259186a7SAndreas Färber         cs->halted = 1;
141427103424SAndreas Färber         cs->exception_index = EXCP_HLT;
1415d76d1650Saurel32     }
1416d76d1650Saurel32 
1417bb4ea393SJan Kiszka     return 0;
1418d76d1650Saurel32 }
1419d76d1650Saurel32 
1420d76d1650Saurel32 /* map dcr access to existing qemu dcr emulation */
1421c995e942SDavid Gibson static int kvmppc_handle_dcr_read(CPUPPCState *env,
1422c995e942SDavid Gibson                                   uint32_t dcrn, uint32_t *data)
1423d76d1650Saurel32 {
1424c995e942SDavid Gibson     if (ppc_dcr_read(env->dcr_env, dcrn, data) < 0) {
1425d76d1650Saurel32         fprintf(stderr, "Read to unhandled DCR (0x%x)\n", dcrn);
1426c995e942SDavid Gibson     }
1427d76d1650Saurel32 
1428bb4ea393SJan Kiszka     return 0;
1429d76d1650Saurel32 }
1430d76d1650Saurel32 
1431c995e942SDavid Gibson static int kvmppc_handle_dcr_write(CPUPPCState *env,
1432c995e942SDavid Gibson                                    uint32_t dcrn, uint32_t data)
1433d76d1650Saurel32 {
1434c995e942SDavid Gibson     if (ppc_dcr_write(env->dcr_env, dcrn, data) < 0) {
1435d76d1650Saurel32         fprintf(stderr, "Write to unhandled DCR (0x%x)\n", dcrn);
1436c995e942SDavid Gibson     }
1437d76d1650Saurel32 
1438bb4ea393SJan Kiszka     return 0;
1439d76d1650Saurel32 }
1440d76d1650Saurel32 
14418a0548f9SBharat Bhushan int kvm_arch_insert_sw_breakpoint(CPUState *cs, struct kvm_sw_breakpoint *bp)
14428a0548f9SBharat Bhushan {
14438a0548f9SBharat Bhushan     /* Mixed endian case is not handled */
14448a0548f9SBharat Bhushan     uint32_t sc = debug_inst_opcode;
14458a0548f9SBharat Bhushan 
14468a0548f9SBharat Bhushan     if (cpu_memory_rw_debug(cs, bp->pc, (uint8_t *)&bp->saved_insn,
14478a0548f9SBharat Bhushan                             sizeof(sc), 0) ||
14488a0548f9SBharat Bhushan         cpu_memory_rw_debug(cs, bp->pc, (uint8_t *)&sc, sizeof(sc), 1)) {
14498a0548f9SBharat Bhushan         return -EINVAL;
14508a0548f9SBharat Bhushan     }
14518a0548f9SBharat Bhushan 
14528a0548f9SBharat Bhushan     return 0;
14538a0548f9SBharat Bhushan }
14548a0548f9SBharat Bhushan 
14558a0548f9SBharat Bhushan int kvm_arch_remove_sw_breakpoint(CPUState *cs, struct kvm_sw_breakpoint *bp)
14568a0548f9SBharat Bhushan {
14578a0548f9SBharat Bhushan     uint32_t sc;
14588a0548f9SBharat Bhushan 
14598a0548f9SBharat Bhushan     if (cpu_memory_rw_debug(cs, bp->pc, (uint8_t *)&sc, sizeof(sc), 0) ||
14608a0548f9SBharat Bhushan         sc != debug_inst_opcode ||
14618a0548f9SBharat Bhushan         cpu_memory_rw_debug(cs, bp->pc, (uint8_t *)&bp->saved_insn,
14628a0548f9SBharat Bhushan                             sizeof(sc), 1)) {
14638a0548f9SBharat Bhushan         return -EINVAL;
14648a0548f9SBharat Bhushan     }
14658a0548f9SBharat Bhushan 
14668a0548f9SBharat Bhushan     return 0;
14678a0548f9SBharat Bhushan }
14688a0548f9SBharat Bhushan 
146988365d17SBharat Bhushan static int find_hw_breakpoint(target_ulong addr, int type)
147088365d17SBharat Bhushan {
147188365d17SBharat Bhushan     int n;
147288365d17SBharat Bhushan 
147388365d17SBharat Bhushan     assert((nb_hw_breakpoint + nb_hw_watchpoint)
147488365d17SBharat Bhushan            <= ARRAY_SIZE(hw_debug_points));
147588365d17SBharat Bhushan 
147688365d17SBharat Bhushan     for (n = 0; n < nb_hw_breakpoint + nb_hw_watchpoint; n++) {
147788365d17SBharat Bhushan         if (hw_debug_points[n].addr == addr &&
147888365d17SBharat Bhushan              hw_debug_points[n].type == type) {
147988365d17SBharat Bhushan             return n;
148088365d17SBharat Bhushan         }
148188365d17SBharat Bhushan     }
148288365d17SBharat Bhushan 
148388365d17SBharat Bhushan     return -1;
148488365d17SBharat Bhushan }
148588365d17SBharat Bhushan 
148688365d17SBharat Bhushan static int find_hw_watchpoint(target_ulong addr, int *flag)
148788365d17SBharat Bhushan {
148888365d17SBharat Bhushan     int n;
148988365d17SBharat Bhushan 
149088365d17SBharat Bhushan     n = find_hw_breakpoint(addr, GDB_WATCHPOINT_ACCESS);
149188365d17SBharat Bhushan     if (n >= 0) {
149288365d17SBharat Bhushan         *flag = BP_MEM_ACCESS;
149388365d17SBharat Bhushan         return n;
149488365d17SBharat Bhushan     }
149588365d17SBharat Bhushan 
149688365d17SBharat Bhushan     n = find_hw_breakpoint(addr, GDB_WATCHPOINT_WRITE);
149788365d17SBharat Bhushan     if (n >= 0) {
149888365d17SBharat Bhushan         *flag = BP_MEM_WRITE;
149988365d17SBharat Bhushan         return n;
150088365d17SBharat Bhushan     }
150188365d17SBharat Bhushan 
150288365d17SBharat Bhushan     n = find_hw_breakpoint(addr, GDB_WATCHPOINT_READ);
150388365d17SBharat Bhushan     if (n >= 0) {
150488365d17SBharat Bhushan         *flag = BP_MEM_READ;
150588365d17SBharat Bhushan         return n;
150688365d17SBharat Bhushan     }
150788365d17SBharat Bhushan 
150888365d17SBharat Bhushan     return -1;
150988365d17SBharat Bhushan }
151088365d17SBharat Bhushan 
151188365d17SBharat Bhushan int kvm_arch_insert_hw_breakpoint(target_ulong addr,
151288365d17SBharat Bhushan                                   target_ulong len, int type)
151388365d17SBharat Bhushan {
151488365d17SBharat Bhushan     if ((nb_hw_breakpoint + nb_hw_watchpoint) >= ARRAY_SIZE(hw_debug_points)) {
151588365d17SBharat Bhushan         return -ENOBUFS;
151688365d17SBharat Bhushan     }
151788365d17SBharat Bhushan 
151888365d17SBharat Bhushan     hw_debug_points[nb_hw_breakpoint + nb_hw_watchpoint].addr = addr;
151988365d17SBharat Bhushan     hw_debug_points[nb_hw_breakpoint + nb_hw_watchpoint].type = type;
152088365d17SBharat Bhushan 
152188365d17SBharat Bhushan     switch (type) {
152288365d17SBharat Bhushan     case GDB_BREAKPOINT_HW:
152388365d17SBharat Bhushan         if (nb_hw_breakpoint >= max_hw_breakpoint) {
152488365d17SBharat Bhushan             return -ENOBUFS;
152588365d17SBharat Bhushan         }
152688365d17SBharat Bhushan 
152788365d17SBharat Bhushan         if (find_hw_breakpoint(addr, type) >= 0) {
152888365d17SBharat Bhushan             return -EEXIST;
152988365d17SBharat Bhushan         }
153088365d17SBharat Bhushan 
153188365d17SBharat Bhushan         nb_hw_breakpoint++;
153288365d17SBharat Bhushan         break;
153388365d17SBharat Bhushan 
153488365d17SBharat Bhushan     case GDB_WATCHPOINT_WRITE:
153588365d17SBharat Bhushan     case GDB_WATCHPOINT_READ:
153688365d17SBharat Bhushan     case GDB_WATCHPOINT_ACCESS:
153788365d17SBharat Bhushan         if (nb_hw_watchpoint >= max_hw_watchpoint) {
153888365d17SBharat Bhushan             return -ENOBUFS;
153988365d17SBharat Bhushan         }
154088365d17SBharat Bhushan 
154188365d17SBharat Bhushan         if (find_hw_breakpoint(addr, type) >= 0) {
154288365d17SBharat Bhushan             return -EEXIST;
154388365d17SBharat Bhushan         }
154488365d17SBharat Bhushan 
154588365d17SBharat Bhushan         nb_hw_watchpoint++;
154688365d17SBharat Bhushan         break;
154788365d17SBharat Bhushan 
154888365d17SBharat Bhushan     default:
154988365d17SBharat Bhushan         return -ENOSYS;
155088365d17SBharat Bhushan     }
155188365d17SBharat Bhushan 
155288365d17SBharat Bhushan     return 0;
155388365d17SBharat Bhushan }
155488365d17SBharat Bhushan 
155588365d17SBharat Bhushan int kvm_arch_remove_hw_breakpoint(target_ulong addr,
155688365d17SBharat Bhushan                                   target_ulong len, int type)
155788365d17SBharat Bhushan {
155888365d17SBharat Bhushan     int n;
155988365d17SBharat Bhushan 
156088365d17SBharat Bhushan     n = find_hw_breakpoint(addr, type);
156188365d17SBharat Bhushan     if (n < 0) {
156288365d17SBharat Bhushan         return -ENOENT;
156388365d17SBharat Bhushan     }
156488365d17SBharat Bhushan 
156588365d17SBharat Bhushan     switch (type) {
156688365d17SBharat Bhushan     case GDB_BREAKPOINT_HW:
156788365d17SBharat Bhushan         nb_hw_breakpoint--;
156888365d17SBharat Bhushan         break;
156988365d17SBharat Bhushan 
157088365d17SBharat Bhushan     case GDB_WATCHPOINT_WRITE:
157188365d17SBharat Bhushan     case GDB_WATCHPOINT_READ:
157288365d17SBharat Bhushan     case GDB_WATCHPOINT_ACCESS:
157388365d17SBharat Bhushan         nb_hw_watchpoint--;
157488365d17SBharat Bhushan         break;
157588365d17SBharat Bhushan 
157688365d17SBharat Bhushan     default:
157788365d17SBharat Bhushan         return -ENOSYS;
157888365d17SBharat Bhushan     }
157988365d17SBharat Bhushan     hw_debug_points[n] = hw_debug_points[nb_hw_breakpoint + nb_hw_watchpoint];
158088365d17SBharat Bhushan 
158188365d17SBharat Bhushan     return 0;
158288365d17SBharat Bhushan }
158388365d17SBharat Bhushan 
158488365d17SBharat Bhushan void kvm_arch_remove_all_hw_breakpoints(void)
158588365d17SBharat Bhushan {
158688365d17SBharat Bhushan     nb_hw_breakpoint = nb_hw_watchpoint = 0;
158788365d17SBharat Bhushan }
158888365d17SBharat Bhushan 
15898a0548f9SBharat Bhushan void kvm_arch_update_guest_debug(CPUState *cs, struct kvm_guest_debug *dbg)
15908a0548f9SBharat Bhushan {
159188365d17SBharat Bhushan     int n;
159288365d17SBharat Bhushan 
15938a0548f9SBharat Bhushan     /* Software Breakpoint updates */
15948a0548f9SBharat Bhushan     if (kvm_sw_breakpoints_active(cs)) {
15958a0548f9SBharat Bhushan         dbg->control |= KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP;
15968a0548f9SBharat Bhushan     }
159788365d17SBharat Bhushan 
159888365d17SBharat Bhushan     assert((nb_hw_breakpoint + nb_hw_watchpoint)
159988365d17SBharat Bhushan            <= ARRAY_SIZE(hw_debug_points));
160088365d17SBharat Bhushan     assert((nb_hw_breakpoint + nb_hw_watchpoint) <= ARRAY_SIZE(dbg->arch.bp));
160188365d17SBharat Bhushan 
160288365d17SBharat Bhushan     if (nb_hw_breakpoint + nb_hw_watchpoint > 0) {
160388365d17SBharat Bhushan         dbg->control |= KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_HW_BP;
160488365d17SBharat Bhushan         memset(dbg->arch.bp, 0, sizeof(dbg->arch.bp));
160588365d17SBharat Bhushan         for (n = 0; n < nb_hw_breakpoint + nb_hw_watchpoint; n++) {
160688365d17SBharat Bhushan             switch (hw_debug_points[n].type) {
160788365d17SBharat Bhushan             case GDB_BREAKPOINT_HW:
160888365d17SBharat Bhushan                 dbg->arch.bp[n].type = KVMPPC_DEBUG_BREAKPOINT;
160988365d17SBharat Bhushan                 break;
161088365d17SBharat Bhushan             case GDB_WATCHPOINT_WRITE:
161188365d17SBharat Bhushan                 dbg->arch.bp[n].type = KVMPPC_DEBUG_WATCH_WRITE;
161288365d17SBharat Bhushan                 break;
161388365d17SBharat Bhushan             case GDB_WATCHPOINT_READ:
161488365d17SBharat Bhushan                 dbg->arch.bp[n].type = KVMPPC_DEBUG_WATCH_READ;
161588365d17SBharat Bhushan                 break;
161688365d17SBharat Bhushan             case GDB_WATCHPOINT_ACCESS:
161788365d17SBharat Bhushan                 dbg->arch.bp[n].type = KVMPPC_DEBUG_WATCH_WRITE |
161888365d17SBharat Bhushan                                         KVMPPC_DEBUG_WATCH_READ;
161988365d17SBharat Bhushan                 break;
162088365d17SBharat Bhushan             default:
162188365d17SBharat Bhushan                 cpu_abort(cs, "Unsupported breakpoint type\n");
162288365d17SBharat Bhushan             }
162388365d17SBharat Bhushan             dbg->arch.bp[n].addr = hw_debug_points[n].addr;
162488365d17SBharat Bhushan         }
162588365d17SBharat Bhushan     }
16268a0548f9SBharat Bhushan }
16278a0548f9SBharat Bhushan 
16282cbd1581SFabiano Rosas static int kvm_handle_hw_breakpoint(CPUState *cs,
16292cbd1581SFabiano Rosas                                     struct kvm_debug_exit_arch *arch_info)
16308a0548f9SBharat Bhushan {
16318a0548f9SBharat Bhushan     int handle = 0;
163288365d17SBharat Bhushan     int n;
163388365d17SBharat Bhushan     int flag = 0;
16348a0548f9SBharat Bhushan 
163588365d17SBharat Bhushan     if (nb_hw_breakpoint + nb_hw_watchpoint > 0) {
163688365d17SBharat Bhushan         if (arch_info->status & KVMPPC_DEBUG_BREAKPOINT) {
163788365d17SBharat Bhushan             n = find_hw_breakpoint(arch_info->address, GDB_BREAKPOINT_HW);
163888365d17SBharat Bhushan             if (n >= 0) {
163988365d17SBharat Bhushan                 handle = 1;
164088365d17SBharat Bhushan             }
164188365d17SBharat Bhushan         } else if (arch_info->status & (KVMPPC_DEBUG_WATCH_READ |
164288365d17SBharat Bhushan                                         KVMPPC_DEBUG_WATCH_WRITE)) {
164388365d17SBharat Bhushan             n = find_hw_watchpoint(arch_info->address,  &flag);
164488365d17SBharat Bhushan             if (n >= 0) {
164588365d17SBharat Bhushan                 handle = 1;
164688365d17SBharat Bhushan                 cs->watchpoint_hit = &hw_watchpoint;
164788365d17SBharat Bhushan                 hw_watchpoint.vaddr = hw_debug_points[n].addr;
164888365d17SBharat Bhushan                 hw_watchpoint.flags = flag;
164988365d17SBharat Bhushan             }
165088365d17SBharat Bhushan         }
165188365d17SBharat Bhushan     }
16522cbd1581SFabiano Rosas     return handle;
16532cbd1581SFabiano Rosas }
16542cbd1581SFabiano Rosas 
1655468e3a1aSFabiano Rosas static int kvm_handle_singlestep(void)
1656468e3a1aSFabiano Rosas {
1657468e3a1aSFabiano Rosas     return 1;
1658468e3a1aSFabiano Rosas }
1659468e3a1aSFabiano Rosas 
1660468e3a1aSFabiano Rosas static int kvm_handle_sw_breakpoint(void)
1661468e3a1aSFabiano Rosas {
1662468e3a1aSFabiano Rosas     return 1;
1663468e3a1aSFabiano Rosas }
1664468e3a1aSFabiano Rosas 
16652cbd1581SFabiano Rosas static int kvm_handle_debug(PowerPCCPU *cpu, struct kvm_run *run)
16662cbd1581SFabiano Rosas {
16672cbd1581SFabiano Rosas     CPUState *cs = CPU(cpu);
16682cbd1581SFabiano Rosas     CPUPPCState *env = &cpu->env;
16692cbd1581SFabiano Rosas     struct kvm_debug_exit_arch *arch_info = &run->debug.arch;
16702cbd1581SFabiano Rosas 
16712cbd1581SFabiano Rosas     if (cs->singlestep_enabled) {
1672468e3a1aSFabiano Rosas         return kvm_handle_singlestep();
1673468e3a1aSFabiano Rosas     }
1674468e3a1aSFabiano Rosas 
1675468e3a1aSFabiano Rosas     if (arch_info->status) {
1676468e3a1aSFabiano Rosas         return kvm_handle_hw_breakpoint(cs, arch_info);
1677468e3a1aSFabiano Rosas     }
1678468e3a1aSFabiano Rosas 
1679468e3a1aSFabiano Rosas     if (kvm_find_sw_breakpoint(cs, arch_info->address)) {
1680468e3a1aSFabiano Rosas         return kvm_handle_sw_breakpoint();
1681468e3a1aSFabiano Rosas     }
1682468e3a1aSFabiano Rosas 
1683468e3a1aSFabiano Rosas     /*
1684468e3a1aSFabiano Rosas      * QEMU is not able to handle debug exception, so inject
16858a0548f9SBharat Bhushan      * program exception to guest;
16868a0548f9SBharat Bhushan      * Yes program exception NOT debug exception !!
168788365d17SBharat Bhushan      * When QEMU is using debug resources then debug exception must
168888365d17SBharat Bhushan      * be always set. To achieve this we set MSR_DE and also set
168988365d17SBharat Bhushan      * MSRP_DEP so guest cannot change MSR_DE.
169088365d17SBharat Bhushan      * When emulating debug resource for guest we want guest
169188365d17SBharat Bhushan      * to control MSR_DE (enable/disable debug interrupt on need).
169288365d17SBharat Bhushan      * Supporting both configurations are NOT possible.
169388365d17SBharat Bhushan      * So the result is that we cannot share debug resources
169488365d17SBharat Bhushan      * between QEMU and Guest on BOOKE architecture.
169588365d17SBharat Bhushan      * In the current design QEMU gets the priority over guest,
169688365d17SBharat Bhushan      * this means that if QEMU is using debug resources then guest
169788365d17SBharat Bhushan      * cannot use them;
16988a0548f9SBharat Bhushan      * For software breakpoint QEMU uses a privileged instruction;
16998a0548f9SBharat Bhushan      * So there cannot be any reason that we are here for guest
17008a0548f9SBharat Bhushan      * set debug exception, only possibility is guest executed a
17018a0548f9SBharat Bhushan      * privileged / illegal instruction and that's why we are
17028a0548f9SBharat Bhushan      * injecting a program interrupt.
17038a0548f9SBharat Bhushan      */
17048a0548f9SBharat Bhushan     cpu_synchronize_state(cs);
1705468e3a1aSFabiano Rosas     /*
1706468e3a1aSFabiano Rosas      * env->nip is PC, so increment this by 4 to use
17078a0548f9SBharat Bhushan      * ppc_cpu_do_interrupt(), which set srr0 = env->nip - 4.
17088a0548f9SBharat Bhushan      */
17098a0548f9SBharat Bhushan     env->nip += 4;
17108a0548f9SBharat Bhushan     cs->exception_index = POWERPC_EXCP_PROGRAM;
17118a0548f9SBharat Bhushan     env->error_code = POWERPC_EXCP_INVAL;
17128a0548f9SBharat Bhushan     ppc_cpu_do_interrupt(cs);
17138a0548f9SBharat Bhushan 
1714468e3a1aSFabiano Rosas     return 0;
17158a0548f9SBharat Bhushan }
17168a0548f9SBharat Bhushan 
171720d695a9SAndreas Färber int kvm_arch_handle_exit(CPUState *cs, struct kvm_run *run)
1718d76d1650Saurel32 {
171920d695a9SAndreas Färber     PowerPCCPU *cpu = POWERPC_CPU(cs);
172020d695a9SAndreas Färber     CPUPPCState *env = &cpu->env;
1721bb4ea393SJan Kiszka     int ret;
1722d76d1650Saurel32 
17234b8523eeSJan Kiszka     qemu_mutex_lock_iothread();
17244b8523eeSJan Kiszka 
1725d76d1650Saurel32     switch (run->exit_reason) {
1726d76d1650Saurel32     case KVM_EXIT_DCR:
1727d76d1650Saurel32         if (run->dcr.is_write) {
17288d83cbf1SGreg Kurz             trace_kvm_handle_dcr_write();
1729d76d1650Saurel32             ret = kvmppc_handle_dcr_write(env, run->dcr.dcrn, run->dcr.data);
1730d76d1650Saurel32         } else {
1731228152c2SBoxuan Li             trace_kvm_handle_dcr_read();
1732d76d1650Saurel32             ret = kvmppc_handle_dcr_read(env, run->dcr.dcrn, &run->dcr.data);
1733d76d1650Saurel32         }
1734d76d1650Saurel32         break;
1735d76d1650Saurel32     case KVM_EXIT_HLT:
17368d83cbf1SGreg Kurz         trace_kvm_handle_halt();
1737259186a7SAndreas Färber         ret = kvmppc_handle_halt(cpu);
1738d76d1650Saurel32         break;
1739c6304a4aSDavid Gibson #if defined(TARGET_PPC64)
1740f61b4bedSAlexander Graf     case KVM_EXIT_PAPR_HCALL:
17418d83cbf1SGreg Kurz         trace_kvm_handle_papr_hcall();
174220d695a9SAndreas Färber         run->papr_hcall.ret = spapr_hypercall(cpu,
1743aa100fa4SAndreas Färber                                               run->papr_hcall.nr,
1744f61b4bedSAlexander Graf                                               run->papr_hcall.args);
174578e8fde2SDavid Gibson         ret = 0;
1746f61b4bedSAlexander Graf         break;
1747f61b4bedSAlexander Graf #endif
17485b95b8b9SAlexander Graf     case KVM_EXIT_EPR:
17498d83cbf1SGreg Kurz         trace_kvm_handle_epr();
1750933b19eaSAlexander Graf         run->epr.epr = ldl_phys(cs->as, env->mpic_iack);
17515b95b8b9SAlexander Graf         ret = 0;
17525b95b8b9SAlexander Graf         break;
175331f2cb8fSBharat Bhushan     case KVM_EXIT_WATCHDOG:
17548d83cbf1SGreg Kurz         trace_kvm_handle_watchdog_expiry();
175531f2cb8fSBharat Bhushan         watchdog_perform_action();
175631f2cb8fSBharat Bhushan         ret = 0;
175731f2cb8fSBharat Bhushan         break;
175831f2cb8fSBharat Bhushan 
17598a0548f9SBharat Bhushan     case KVM_EXIT_DEBUG:
17608d83cbf1SGreg Kurz         trace_kvm_handle_debug_exception();
17618a0548f9SBharat Bhushan         if (kvm_handle_debug(cpu, run)) {
17628a0548f9SBharat Bhushan             ret = EXCP_DEBUG;
17638a0548f9SBharat Bhushan             break;
17648a0548f9SBharat Bhushan         }
17658a0548f9SBharat Bhushan         /* re-enter, this exception was guest-internal */
17668a0548f9SBharat Bhushan         ret = 0;
17678a0548f9SBharat Bhushan         break;
17688a0548f9SBharat Bhushan 
176973aaec4aSJan Kiszka     default:
177073aaec4aSJan Kiszka         fprintf(stderr, "KVM: unknown exit reason %d\n", run->exit_reason);
177173aaec4aSJan Kiszka         ret = -1;
177273aaec4aSJan Kiszka         break;
1773d76d1650Saurel32     }
1774d76d1650Saurel32 
17754b8523eeSJan Kiszka     qemu_mutex_unlock_iothread();
1776d76d1650Saurel32     return ret;
1777d76d1650Saurel32 }
1778d76d1650Saurel32 
177931f2cb8fSBharat Bhushan int kvmppc_or_tsr_bits(PowerPCCPU *cpu, uint32_t tsr_bits)
178031f2cb8fSBharat Bhushan {
178131f2cb8fSBharat Bhushan     CPUState *cs = CPU(cpu);
178231f2cb8fSBharat Bhushan     uint32_t bits = tsr_bits;
178331f2cb8fSBharat Bhushan     struct kvm_one_reg reg = {
178431f2cb8fSBharat Bhushan         .id = KVM_REG_PPC_OR_TSR,
178531f2cb8fSBharat Bhushan         .addr = (uintptr_t) &bits,
178631f2cb8fSBharat Bhushan     };
178731f2cb8fSBharat Bhushan 
178831f2cb8fSBharat Bhushan     return kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, &reg);
178931f2cb8fSBharat Bhushan }
179031f2cb8fSBharat Bhushan 
179131f2cb8fSBharat Bhushan int kvmppc_clear_tsr_bits(PowerPCCPU *cpu, uint32_t tsr_bits)
179231f2cb8fSBharat Bhushan {
179331f2cb8fSBharat Bhushan 
179431f2cb8fSBharat Bhushan     CPUState *cs = CPU(cpu);
179531f2cb8fSBharat Bhushan     uint32_t bits = tsr_bits;
179631f2cb8fSBharat Bhushan     struct kvm_one_reg reg = {
179731f2cb8fSBharat Bhushan         .id = KVM_REG_PPC_CLEAR_TSR,
179831f2cb8fSBharat Bhushan         .addr = (uintptr_t) &bits,
179931f2cb8fSBharat Bhushan     };
180031f2cb8fSBharat Bhushan 
180131f2cb8fSBharat Bhushan     return kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, &reg);
180231f2cb8fSBharat Bhushan }
180331f2cb8fSBharat Bhushan 
180431f2cb8fSBharat Bhushan int kvmppc_set_tcr(PowerPCCPU *cpu)
180531f2cb8fSBharat Bhushan {
180631f2cb8fSBharat Bhushan     CPUState *cs = CPU(cpu);
180731f2cb8fSBharat Bhushan     CPUPPCState *env = &cpu->env;
180831f2cb8fSBharat Bhushan     uint32_t tcr = env->spr[SPR_BOOKE_TCR];
180931f2cb8fSBharat Bhushan 
181031f2cb8fSBharat Bhushan     struct kvm_one_reg reg = {
181131f2cb8fSBharat Bhushan         .id = KVM_REG_PPC_TCR,
181231f2cb8fSBharat Bhushan         .addr = (uintptr_t) &tcr,
181331f2cb8fSBharat Bhushan     };
181431f2cb8fSBharat Bhushan 
181531f2cb8fSBharat Bhushan     return kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, &reg);
181631f2cb8fSBharat Bhushan }
181731f2cb8fSBharat Bhushan 
181831f2cb8fSBharat Bhushan int kvmppc_booke_watchdog_enable(PowerPCCPU *cpu)
181931f2cb8fSBharat Bhushan {
182031f2cb8fSBharat Bhushan     CPUState *cs = CPU(cpu);
182131f2cb8fSBharat Bhushan     int ret;
182231f2cb8fSBharat Bhushan 
182331f2cb8fSBharat Bhushan     if (!kvm_enabled()) {
182431f2cb8fSBharat Bhushan         return -1;
182531f2cb8fSBharat Bhushan     }
182631f2cb8fSBharat Bhushan 
182731f2cb8fSBharat Bhushan     if (!cap_ppc_watchdog) {
182831f2cb8fSBharat Bhushan         printf("warning: KVM does not support watchdog");
182931f2cb8fSBharat Bhushan         return -1;
183031f2cb8fSBharat Bhushan     }
183131f2cb8fSBharat Bhushan 
183248add816SCornelia Huck     ret = kvm_vcpu_enable_cap(cs, KVM_CAP_PPC_BOOKE_WATCHDOG, 0);
183331f2cb8fSBharat Bhushan     if (ret < 0) {
183431f2cb8fSBharat Bhushan         fprintf(stderr, "%s: couldn't enable KVM_CAP_PPC_BOOKE_WATCHDOG: %s\n",
183531f2cb8fSBharat Bhushan                 __func__, strerror(-ret));
183631f2cb8fSBharat Bhushan         return ret;
183731f2cb8fSBharat Bhushan     }
183831f2cb8fSBharat Bhushan 
183931f2cb8fSBharat Bhushan     return ret;
184031f2cb8fSBharat Bhushan }
184131f2cb8fSBharat Bhushan 
1842dc333cd6SAlexander Graf static int read_cpuinfo(const char *field, char *value, int len)
1843dc333cd6SAlexander Graf {
1844dc333cd6SAlexander Graf     FILE *f;
1845dc333cd6SAlexander Graf     int ret = -1;
1846dc333cd6SAlexander Graf     int field_len = strlen(field);
1847dc333cd6SAlexander Graf     char line[512];
1848dc333cd6SAlexander Graf 
1849dc333cd6SAlexander Graf     f = fopen("/proc/cpuinfo", "r");
1850dc333cd6SAlexander Graf     if (!f) {
1851dc333cd6SAlexander Graf         return -1;
1852dc333cd6SAlexander Graf     }
1853dc333cd6SAlexander Graf 
1854dc333cd6SAlexander Graf     do {
1855dc333cd6SAlexander Graf         if (!fgets(line, sizeof(line), f)) {
1856dc333cd6SAlexander Graf             break;
1857dc333cd6SAlexander Graf         }
1858dc333cd6SAlexander Graf         if (!strncmp(line, field, field_len)) {
1859ae215068SJim Meyering             pstrcpy(value, len, line);
1860dc333cd6SAlexander Graf             ret = 0;
1861dc333cd6SAlexander Graf             break;
1862dc333cd6SAlexander Graf         }
1863dc333cd6SAlexander Graf     } while (*line);
1864dc333cd6SAlexander Graf 
1865dc333cd6SAlexander Graf     fclose(f);
1866dc333cd6SAlexander Graf 
1867dc333cd6SAlexander Graf     return ret;
1868dc333cd6SAlexander Graf }
1869dc333cd6SAlexander Graf 
1870dc333cd6SAlexander Graf uint32_t kvmppc_get_tbfreq(void)
1871dc333cd6SAlexander Graf {
1872dc333cd6SAlexander Graf     char line[512];
1873dc333cd6SAlexander Graf     char *ns;
187473bcb24dSRutuja Shah     uint32_t retval = NANOSECONDS_PER_SECOND;
1875dc333cd6SAlexander Graf 
1876dc333cd6SAlexander Graf     if (read_cpuinfo("timebase", line, sizeof(line))) {
1877dc333cd6SAlexander Graf         return retval;
1878dc333cd6SAlexander Graf     }
1879dc333cd6SAlexander Graf 
1880c995e942SDavid Gibson     ns = strchr(line, ':');
1881c995e942SDavid Gibson     if (!ns) {
1882dc333cd6SAlexander Graf         return retval;
1883dc333cd6SAlexander Graf     }
1884dc333cd6SAlexander Graf 
1885dc333cd6SAlexander Graf     ns++;
1886dc333cd6SAlexander Graf 
1887f9b8e7f6SShraddha Barke     return atoi(ns);
1888ef951443SNikunj A Dadhania }
1889ef951443SNikunj A Dadhania 
1890ef951443SNikunj A Dadhania bool kvmppc_get_host_serial(char **value)
1891ef951443SNikunj A Dadhania {
1892ef951443SNikunj A Dadhania     return g_file_get_contents("/proc/device-tree/system-id", value, NULL,
1893ef951443SNikunj A Dadhania                                NULL);
1894ef951443SNikunj A Dadhania }
1895ef951443SNikunj A Dadhania 
1896ef951443SNikunj A Dadhania bool kvmppc_get_host_model(char **value)
1897ef951443SNikunj A Dadhania {
1898ef951443SNikunj A Dadhania     return g_file_get_contents("/proc/device-tree/model", value, NULL, NULL);
1899dc333cd6SAlexander Graf }
19004513d923SGleb Natapov 
1901eadaada1SAlexander Graf /* Try to find a device tree node for a CPU with clock-frequency property */
1902eadaada1SAlexander Graf static int kvmppc_find_cpu_dt(char *buf, int buf_len)
1903eadaada1SAlexander Graf {
1904eadaada1SAlexander Graf     struct dirent *dirp;
1905eadaada1SAlexander Graf     DIR *dp;
1906eadaada1SAlexander Graf 
1907c995e942SDavid Gibson     dp = opendir(PROC_DEVTREE_CPU);
1908c995e942SDavid Gibson     if (!dp) {
1909eadaada1SAlexander Graf         printf("Can't open directory " PROC_DEVTREE_CPU "\n");
1910eadaada1SAlexander Graf         return -1;
1911eadaada1SAlexander Graf     }
1912eadaada1SAlexander Graf 
1913eadaada1SAlexander Graf     buf[0] = '\0';
1914eadaada1SAlexander Graf     while ((dirp = readdir(dp)) != NULL) {
1915eadaada1SAlexander Graf         FILE *f;
1916eadaada1SAlexander Graf         snprintf(buf, buf_len, "%s%s/clock-frequency", PROC_DEVTREE_CPU,
1917eadaada1SAlexander Graf                  dirp->d_name);
1918eadaada1SAlexander Graf         f = fopen(buf, "r");
1919eadaada1SAlexander Graf         if (f) {
1920eadaada1SAlexander Graf             snprintf(buf, buf_len, "%s%s", PROC_DEVTREE_CPU, dirp->d_name);
1921eadaada1SAlexander Graf             fclose(f);
1922eadaada1SAlexander Graf             break;
1923eadaada1SAlexander Graf         }
1924eadaada1SAlexander Graf         buf[0] = '\0';
1925eadaada1SAlexander Graf     }
1926eadaada1SAlexander Graf     closedir(dp);
1927eadaada1SAlexander Graf     if (buf[0] == '\0') {
1928eadaada1SAlexander Graf         printf("Unknown host!\n");
1929eadaada1SAlexander Graf         return -1;
1930eadaada1SAlexander Graf     }
1931eadaada1SAlexander Graf 
1932eadaada1SAlexander Graf     return 0;
1933eadaada1SAlexander Graf }
1934eadaada1SAlexander Graf 
19357d94a30bSSukadev Bhattiprolu static uint64_t kvmppc_read_int_dt(const char *filename)
1936eadaada1SAlexander Graf {
19379bc884b7SDavid Gibson     union {
19389bc884b7SDavid Gibson         uint32_t v32;
19399bc884b7SDavid Gibson         uint64_t v64;
19409bc884b7SDavid Gibson     } u;
1941eadaada1SAlexander Graf     FILE *f;
1942eadaada1SAlexander Graf     int len;
1943eadaada1SAlexander Graf 
19447d94a30bSSukadev Bhattiprolu     f = fopen(filename, "rb");
1945eadaada1SAlexander Graf     if (!f) {
1946eadaada1SAlexander Graf         return -1;
1947eadaada1SAlexander Graf     }
1948eadaada1SAlexander Graf 
19499bc884b7SDavid Gibson     len = fread(&u, 1, sizeof(u), f);
1950eadaada1SAlexander Graf     fclose(f);
1951eadaada1SAlexander Graf     switch (len) {
19529bc884b7SDavid Gibson     case 4:
19539bc884b7SDavid Gibson         /* property is a 32-bit quantity */
19549bc884b7SDavid Gibson         return be32_to_cpu(u.v32);
19559bc884b7SDavid Gibson     case 8:
19569bc884b7SDavid Gibson         return be64_to_cpu(u.v64);
1957eadaada1SAlexander Graf     }
1958eadaada1SAlexander Graf 
1959eadaada1SAlexander Graf     return 0;
1960eadaada1SAlexander Graf }
1961eadaada1SAlexander Graf 
1962c995e942SDavid Gibson /*
1963c995e942SDavid Gibson  * Read a CPU node property from the host device tree that's a single
19647d94a30bSSukadev Bhattiprolu  * integer (32-bit or 64-bit).  Returns 0 if anything goes wrong
1965c995e942SDavid Gibson  * (can't find or open the property, or doesn't understand the format)
1966c995e942SDavid Gibson  */
19677d94a30bSSukadev Bhattiprolu static uint64_t kvmppc_read_int_cpu_dt(const char *propname)
19687d94a30bSSukadev Bhattiprolu {
19697d94a30bSSukadev Bhattiprolu     char buf[PATH_MAX], *tmp;
19707d94a30bSSukadev Bhattiprolu     uint64_t val;
19717d94a30bSSukadev Bhattiprolu 
19727d94a30bSSukadev Bhattiprolu     if (kvmppc_find_cpu_dt(buf, sizeof(buf))) {
19737d94a30bSSukadev Bhattiprolu         return -1;
19747d94a30bSSukadev Bhattiprolu     }
19757d94a30bSSukadev Bhattiprolu 
19767d94a30bSSukadev Bhattiprolu     tmp = g_strdup_printf("%s/%s", buf, propname);
19777d94a30bSSukadev Bhattiprolu     val = kvmppc_read_int_dt(tmp);
19787d94a30bSSukadev Bhattiprolu     g_free(tmp);
19797d94a30bSSukadev Bhattiprolu 
19807d94a30bSSukadev Bhattiprolu     return val;
19817d94a30bSSukadev Bhattiprolu }
19827d94a30bSSukadev Bhattiprolu 
19839bc884b7SDavid Gibson uint64_t kvmppc_get_clockfreq(void)
19849bc884b7SDavid Gibson {
19859bc884b7SDavid Gibson     return kvmppc_read_int_cpu_dt("clock-frequency");
19869bc884b7SDavid Gibson }
19879bc884b7SDavid Gibson 
19887d050527SSuraj Jitindar Singh static int kvmppc_get_dec_bits(void)
19897d050527SSuraj Jitindar Singh {
19907d050527SSuraj Jitindar Singh     int nr_bits = kvmppc_read_int_cpu_dt("ibm,dec-bits");
19917d050527SSuraj Jitindar Singh 
19927d050527SSuraj Jitindar Singh     if (nr_bits > 0) {
19937d050527SSuraj Jitindar Singh         return nr_bits;
19947d050527SSuraj Jitindar Singh     }
19957d050527SSuraj Jitindar Singh     return 0;
19967d050527SSuraj Jitindar Singh }
19977d050527SSuraj Jitindar Singh 
19981a61a9aeSStuart Yoder static int kvmppc_get_pvinfo(CPUPPCState *env, struct kvm_ppc_pvinfo *pvinfo)
199945024f09SAlexander Graf {
2000db70b311SRichard Henderson     CPUState *cs = env_cpu(env);
200145024f09SAlexander Graf 
20026fd33a75SAlexander Graf     if (kvm_vm_check_extension(cs->kvm_state, KVM_CAP_PPC_GET_PVINFO) &&
20031a61a9aeSStuart Yoder         !kvm_vm_ioctl(cs->kvm_state, KVM_PPC_GET_PVINFO, pvinfo)) {
20041a61a9aeSStuart Yoder         return 0;
20051a61a9aeSStuart Yoder     }
200645024f09SAlexander Graf 
20071a61a9aeSStuart Yoder     return 1;
20081a61a9aeSStuart Yoder }
20091a61a9aeSStuart Yoder 
20101a61a9aeSStuart Yoder int kvmppc_get_hasidle(CPUPPCState *env)
20111a61a9aeSStuart Yoder {
20121a61a9aeSStuart Yoder     struct kvm_ppc_pvinfo pvinfo;
20131a61a9aeSStuart Yoder 
20141a61a9aeSStuart Yoder     if (!kvmppc_get_pvinfo(env, &pvinfo) &&
20151a61a9aeSStuart Yoder         (pvinfo.flags & KVM_PPC_PVINFO_FLAGS_EV_IDLE)) {
20161a61a9aeSStuart Yoder         return 1;
20171a61a9aeSStuart Yoder     }
20181a61a9aeSStuart Yoder 
20191a61a9aeSStuart Yoder     return 0;
20201a61a9aeSStuart Yoder }
20211a61a9aeSStuart Yoder 
20221a61a9aeSStuart Yoder int kvmppc_get_hypercall(CPUPPCState *env, uint8_t *buf, int buf_len)
20231a61a9aeSStuart Yoder {
20241a61a9aeSStuart Yoder     uint32_t *hc = (uint32_t *)buf;
20251a61a9aeSStuart Yoder     struct kvm_ppc_pvinfo pvinfo;
20261a61a9aeSStuart Yoder 
20271a61a9aeSStuart Yoder     if (!kvmppc_get_pvinfo(env, &pvinfo)) {
20281a61a9aeSStuart Yoder         memcpy(buf, pvinfo.hcall, buf_len);
202945024f09SAlexander Graf         return 0;
203045024f09SAlexander Graf     }
203145024f09SAlexander Graf 
203245024f09SAlexander Graf     /*
2033d13fc32eSAlexander Graf      * Fallback to always fail hypercalls regardless of endianness:
203445024f09SAlexander Graf      *
2035d13fc32eSAlexander Graf      *     tdi 0,r0,72 (becomes b .+8 in wrong endian, nop in good endian)
203645024f09SAlexander Graf      *     li r3, -1
2037d13fc32eSAlexander Graf      *     b .+8       (becomes nop in wrong endian)
2038d13fc32eSAlexander Graf      *     bswap32(li r3, -1)
203945024f09SAlexander Graf      */
204045024f09SAlexander Graf 
2041d13fc32eSAlexander Graf     hc[0] = cpu_to_be32(0x08000048);
2042d13fc32eSAlexander Graf     hc[1] = cpu_to_be32(0x3860ffff);
2043d13fc32eSAlexander Graf     hc[2] = cpu_to_be32(0x48000008);
2044d13fc32eSAlexander Graf     hc[3] = cpu_to_be32(bswap32(0x3860ffff));
204545024f09SAlexander Graf 
20460ddbd053SAlexey Kardashevskiy     return 1;
204745024f09SAlexander Graf }
204845024f09SAlexander Graf 
2049026bfd89SDavid Gibson static inline int kvmppc_enable_hcall(KVMState *s, target_ulong hcall)
2050026bfd89SDavid Gibson {
2051026bfd89SDavid Gibson     return kvm_vm_enable_cap(s, KVM_CAP_PPC_ENABLE_HCALL, 0, hcall, 1);
2052026bfd89SDavid Gibson }
2053026bfd89SDavid Gibson 
2054026bfd89SDavid Gibson void kvmppc_enable_logical_ci_hcalls(void)
2055026bfd89SDavid Gibson {
2056026bfd89SDavid Gibson     /*
2057026bfd89SDavid Gibson      * FIXME: it would be nice if we could detect the cases where
2058026bfd89SDavid Gibson      * we're using a device which requires the in kernel
2059026bfd89SDavid Gibson      * implementation of these hcalls, but the kernel lacks them and
2060026bfd89SDavid Gibson      * produce a warning.
2061026bfd89SDavid Gibson      */
2062026bfd89SDavid Gibson     kvmppc_enable_hcall(kvm_state, H_LOGICAL_CI_LOAD);
2063026bfd89SDavid Gibson     kvmppc_enable_hcall(kvm_state, H_LOGICAL_CI_STORE);
2064026bfd89SDavid Gibson }
2065026bfd89SDavid Gibson 
2066ef9971ddSAlexey Kardashevskiy void kvmppc_enable_set_mode_hcall(void)
2067ef9971ddSAlexey Kardashevskiy {
2068ef9971ddSAlexey Kardashevskiy     kvmppc_enable_hcall(kvm_state, H_SET_MODE);
2069ef9971ddSAlexey Kardashevskiy }
2070ef9971ddSAlexey Kardashevskiy 
20715145ad4fSNathan Whitehorn void kvmppc_enable_clear_ref_mod_hcalls(void)
20725145ad4fSNathan Whitehorn {
20735145ad4fSNathan Whitehorn     kvmppc_enable_hcall(kvm_state, H_CLEAR_REF);
20745145ad4fSNathan Whitehorn     kvmppc_enable_hcall(kvm_state, H_CLEAR_MOD);
20755145ad4fSNathan Whitehorn }
20765145ad4fSNathan Whitehorn 
207768f9f708SSuraj Jitindar Singh void kvmppc_enable_h_page_init(void)
207868f9f708SSuraj Jitindar Singh {
207968f9f708SSuraj Jitindar Singh     kvmppc_enable_hcall(kvm_state, H_PAGE_INIT);
208068f9f708SSuraj Jitindar Singh }
208168f9f708SSuraj Jitindar Singh 
20821bc22652SAndreas Färber void kvmppc_set_papr(PowerPCCPU *cpu)
2083f61b4bedSAlexander Graf {
20841bc22652SAndreas Färber     CPUState *cs = CPU(cpu);
2085f61b4bedSAlexander Graf     int ret;
2086f61b4bedSAlexander Graf 
2087da20aed1SDavid Gibson     if (!kvm_enabled()) {
2088da20aed1SDavid Gibson         return;
2089da20aed1SDavid Gibson     }
2090da20aed1SDavid Gibson 
209148add816SCornelia Huck     ret = kvm_vcpu_enable_cap(cs, KVM_CAP_PPC_PAPR, 0);
2092f61b4bedSAlexander Graf     if (ret) {
2093072ed5f2SThomas Huth         error_report("This vCPU type or KVM version does not support PAPR");
2094072ed5f2SThomas Huth         exit(1);
2095f61b4bedSAlexander Graf     }
20969b00ea49SDavid Gibson 
2097c995e942SDavid Gibson     /*
2098c995e942SDavid Gibson      * Update the capability flag so we sync the right information
2099c995e942SDavid Gibson      * with kvm
2100c995e942SDavid Gibson      */
21019b00ea49SDavid Gibson     cap_papr = 1;
2102f1af19d7SDavid Gibson }
2103f61b4bedSAlexander Graf 
2104d6e166c0SDavid Gibson int kvmppc_set_compat(PowerPCCPU *cpu, uint32_t compat_pvr)
21056db5bb0fSAlexey Kardashevskiy {
2106d6e166c0SDavid Gibson     return kvm_set_one_reg(CPU(cpu), KVM_REG_PPC_ARCH_COMPAT, &compat_pvr);
21076db5bb0fSAlexey Kardashevskiy }
21086db5bb0fSAlexey Kardashevskiy 
21095b95b8b9SAlexander Graf void kvmppc_set_mpic_proxy(PowerPCCPU *cpu, int mpic_proxy)
21105b95b8b9SAlexander Graf {
21115b95b8b9SAlexander Graf     CPUState *cs = CPU(cpu);
21125b95b8b9SAlexander Graf     int ret;
21135b95b8b9SAlexander Graf 
211448add816SCornelia Huck     ret = kvm_vcpu_enable_cap(cs, KVM_CAP_PPC_EPR, 0, mpic_proxy);
21155b95b8b9SAlexander Graf     if (ret && mpic_proxy) {
2116072ed5f2SThomas Huth         error_report("This KVM version does not support EPR");
2117072ed5f2SThomas Huth         exit(1);
21185b95b8b9SAlexander Graf     }
21195b95b8b9SAlexander Graf }
21205b95b8b9SAlexander Graf 
2121e97c3636SDavid Gibson int kvmppc_smt_threads(void)
2122e97c3636SDavid Gibson {
2123e97c3636SDavid Gibson     return cap_ppc_smt ? cap_ppc_smt : 1;
2124e97c3636SDavid Gibson }
2125e97c3636SDavid Gibson 
2126fa98fbfcSSam Bobroff int kvmppc_set_smt_threads(int smt)
2127fa98fbfcSSam Bobroff {
2128fa98fbfcSSam Bobroff     int ret;
2129fa98fbfcSSam Bobroff 
2130fa98fbfcSSam Bobroff     ret = kvm_vm_enable_cap(kvm_state, KVM_CAP_PPC_SMT, 0, smt, 0);
2131fa98fbfcSSam Bobroff     if (!ret) {
2132fa98fbfcSSam Bobroff         cap_ppc_smt = smt;
2133fa98fbfcSSam Bobroff     }
2134fa98fbfcSSam Bobroff     return ret;
2135fa98fbfcSSam Bobroff }
2136fa98fbfcSSam Bobroff 
2137fa98fbfcSSam Bobroff void kvmppc_hint_smt_possible(Error **errp)
2138fa98fbfcSSam Bobroff {
2139fa98fbfcSSam Bobroff     int i;
2140fa98fbfcSSam Bobroff     GString *g;
2141fa98fbfcSSam Bobroff     char *s;
2142fa98fbfcSSam Bobroff 
2143fa98fbfcSSam Bobroff     assert(kvm_enabled());
2144fa98fbfcSSam Bobroff     if (cap_ppc_smt_possible) {
2145fa98fbfcSSam Bobroff         g = g_string_new("Available VSMT modes:");
2146fa98fbfcSSam Bobroff         for (i = 63; i >= 0; i--) {
2147fa98fbfcSSam Bobroff             if ((1UL << i) & cap_ppc_smt_possible) {
2148fa98fbfcSSam Bobroff                 g_string_append_printf(g, " %lu", (1UL << i));
2149fa98fbfcSSam Bobroff             }
2150fa98fbfcSSam Bobroff         }
2151fa98fbfcSSam Bobroff         s = g_string_free(g, false);
2152fa98fbfcSSam Bobroff         error_append_hint(errp, "%s.\n", s);
2153fa98fbfcSSam Bobroff         g_free(s);
2154fa98fbfcSSam Bobroff     } else {
2155fa98fbfcSSam Bobroff         error_append_hint(errp,
2156fa98fbfcSSam Bobroff                           "This KVM seems to be too old to support VSMT.\n");
2157fa98fbfcSSam Bobroff     }
2158fa98fbfcSSam Bobroff }
2159fa98fbfcSSam Bobroff 
2160fa98fbfcSSam Bobroff 
21617f763a5dSDavid Gibson #ifdef TARGET_PPC64
21627f763a5dSDavid Gibson uint64_t kvmppc_rma_size(uint64_t current_size, unsigned int hash_shift)
21637f763a5dSDavid Gibson {
2164f36951c1SDavid Gibson     struct kvm_ppc_smmu_info info;
2165f36951c1SDavid Gibson     long rampagesize, best_page_shift;
2166f36951c1SDavid Gibson     int i;
2167f36951c1SDavid Gibson 
2168c995e942SDavid Gibson     /*
2169c995e942SDavid Gibson      * Find the largest hardware supported page size that's less than
2170c995e942SDavid Gibson      * or equal to the (logical) backing page size of guest RAM
2171c995e942SDavid Gibson      */
2172ab256960SGreg Kurz     kvm_get_smmu_info(&info, &error_fatal);
2173905b7ee4SDavid Hildenbrand     rampagesize = qemu_minrampagesize();
2174f36951c1SDavid Gibson     best_page_shift = 0;
2175f36951c1SDavid Gibson 
2176f36951c1SDavid Gibson     for (i = 0; i < KVM_PPC_PAGE_SIZES_MAX_SZ; i++) {
2177f36951c1SDavid Gibson         struct kvm_ppc_one_seg_page_size *sps = &info.sps[i];
2178f36951c1SDavid Gibson 
2179f36951c1SDavid Gibson         if (!sps->page_shift) {
2180f36951c1SDavid Gibson             continue;
2181f36951c1SDavid Gibson         }
2182f36951c1SDavid Gibson 
2183f36951c1SDavid Gibson         if ((sps->page_shift > best_page_shift)
2184f36951c1SDavid Gibson             && ((1UL << sps->page_shift) <= rampagesize)) {
2185f36951c1SDavid Gibson             best_page_shift = sps->page_shift;
2186f36951c1SDavid Gibson         }
2187f36951c1SDavid Gibson     }
2188f36951c1SDavid Gibson 
21897f763a5dSDavid Gibson     return MIN(current_size,
2190f36951c1SDavid Gibson                1ULL << (best_page_shift + hash_shift - 7));
21917f763a5dSDavid Gibson }
21927f763a5dSDavid Gibson #endif
21937f763a5dSDavid Gibson 
2194da95324eSAlexey Kardashevskiy bool kvmppc_spapr_use_multitce(void)
2195da95324eSAlexey Kardashevskiy {
2196da95324eSAlexey Kardashevskiy     return cap_spapr_multitce;
2197da95324eSAlexey Kardashevskiy }
2198da95324eSAlexey Kardashevskiy 
21993dc410aeSAlexey Kardashevskiy int kvmppc_spapr_enable_inkernel_multitce(void)
22003dc410aeSAlexey Kardashevskiy {
22013dc410aeSAlexey Kardashevskiy     int ret;
22023dc410aeSAlexey Kardashevskiy 
22033dc410aeSAlexey Kardashevskiy     ret = kvm_vm_enable_cap(kvm_state, KVM_CAP_PPC_ENABLE_HCALL, 0,
22043dc410aeSAlexey Kardashevskiy                             H_PUT_TCE_INDIRECT, 1);
22053dc410aeSAlexey Kardashevskiy     if (!ret) {
22063dc410aeSAlexey Kardashevskiy         ret = kvm_vm_enable_cap(kvm_state, KVM_CAP_PPC_ENABLE_HCALL, 0,
22073dc410aeSAlexey Kardashevskiy                                 H_STUFF_TCE, 1);
22083dc410aeSAlexey Kardashevskiy     }
22093dc410aeSAlexey Kardashevskiy 
22103dc410aeSAlexey Kardashevskiy     return ret;
22113dc410aeSAlexey Kardashevskiy }
22123dc410aeSAlexey Kardashevskiy 
2213d6ee2a7cSAlexey Kardashevskiy void *kvmppc_create_spapr_tce(uint32_t liobn, uint32_t page_shift,
2214d6ee2a7cSAlexey Kardashevskiy                               uint64_t bus_offset, uint32_t nb_table,
2215d6ee2a7cSAlexey Kardashevskiy                               int *pfd, bool need_vfio)
22160f5cb298SDavid Gibson {
22170f5cb298SDavid Gibson     long len;
22180f5cb298SDavid Gibson     int fd;
22190f5cb298SDavid Gibson     void *table;
22200f5cb298SDavid Gibson 
2221c995e942SDavid Gibson     /*
2222c995e942SDavid Gibson      * Must set fd to -1 so we don't try to munmap when called for
2223b5aec396SDavid Gibson      * destroying the table, which the upper layers -will- do
2224b5aec396SDavid Gibson      */
2225b5aec396SDavid Gibson     *pfd = -1;
22266a81dd17SDavid Gibson     if (!cap_spapr_tce || (need_vfio && !cap_spapr_vfio)) {
22270f5cb298SDavid Gibson         return NULL;
22280f5cb298SDavid Gibson     }
22290f5cb298SDavid Gibson 
2230d6ee2a7cSAlexey Kardashevskiy     if (cap_spapr_tce_64) {
2231d6ee2a7cSAlexey Kardashevskiy         struct kvm_create_spapr_tce_64 args = {
2232d6ee2a7cSAlexey Kardashevskiy             .liobn = liobn,
2233d6ee2a7cSAlexey Kardashevskiy             .page_shift = page_shift,
2234d6ee2a7cSAlexey Kardashevskiy             .offset = bus_offset >> page_shift,
2235d6ee2a7cSAlexey Kardashevskiy             .size = nb_table,
2236d6ee2a7cSAlexey Kardashevskiy             .flags = 0
2237d6ee2a7cSAlexey Kardashevskiy         };
2238d6ee2a7cSAlexey Kardashevskiy         fd = kvm_vm_ioctl(kvm_state, KVM_CREATE_SPAPR_TCE_64, &args);
2239d6ee2a7cSAlexey Kardashevskiy         if (fd < 0) {
2240d6ee2a7cSAlexey Kardashevskiy             fprintf(stderr,
2241d6ee2a7cSAlexey Kardashevskiy                     "KVM: Failed to create TCE64 table for liobn 0x%x\n",
2242d6ee2a7cSAlexey Kardashevskiy                     liobn);
2243d6ee2a7cSAlexey Kardashevskiy             return NULL;
2244d6ee2a7cSAlexey Kardashevskiy         }
2245d6ee2a7cSAlexey Kardashevskiy     } else if (cap_spapr_tce) {
2246d6ee2a7cSAlexey Kardashevskiy         uint64_t window_size = (uint64_t) nb_table << page_shift;
2247d6ee2a7cSAlexey Kardashevskiy         struct kvm_create_spapr_tce args = {
2248d6ee2a7cSAlexey Kardashevskiy             .liobn = liobn,
2249d6ee2a7cSAlexey Kardashevskiy             .window_size = window_size,
2250d6ee2a7cSAlexey Kardashevskiy         };
2251d6ee2a7cSAlexey Kardashevskiy         if ((window_size != args.window_size) || bus_offset) {
2252d6ee2a7cSAlexey Kardashevskiy             return NULL;
2253d6ee2a7cSAlexey Kardashevskiy         }
22540f5cb298SDavid Gibson         fd = kvm_vm_ioctl(kvm_state, KVM_CREATE_SPAPR_TCE, &args);
22550f5cb298SDavid Gibson         if (fd < 0) {
2256b5aec396SDavid Gibson             fprintf(stderr, "KVM: Failed to create TCE table for liobn 0x%x\n",
2257b5aec396SDavid Gibson                     liobn);
22580f5cb298SDavid Gibson             return NULL;
22590f5cb298SDavid Gibson         }
2260d6ee2a7cSAlexey Kardashevskiy     } else {
2261d6ee2a7cSAlexey Kardashevskiy         return NULL;
2262d6ee2a7cSAlexey Kardashevskiy     }
22630f5cb298SDavid Gibson 
2264d6ee2a7cSAlexey Kardashevskiy     len = nb_table * sizeof(uint64_t);
22650f5cb298SDavid Gibson     /* FIXME: round this up to page size */
22660f5cb298SDavid Gibson 
226774b41e56SDavid Gibson     table = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
22680f5cb298SDavid Gibson     if (table == MAP_FAILED) {
2269b5aec396SDavid Gibson         fprintf(stderr, "KVM: Failed to map TCE table for liobn 0x%x\n",
2270b5aec396SDavid Gibson                 liobn);
22710f5cb298SDavid Gibson         close(fd);
22720f5cb298SDavid Gibson         return NULL;
22730f5cb298SDavid Gibson     }
22740f5cb298SDavid Gibson 
22750f5cb298SDavid Gibson     *pfd = fd;
22760f5cb298SDavid Gibson     return table;
22770f5cb298SDavid Gibson }
22780f5cb298SDavid Gibson 
2279523e7b8aSAlexey Kardashevskiy int kvmppc_remove_spapr_tce(void *table, int fd, uint32_t nb_table)
22800f5cb298SDavid Gibson {
22810f5cb298SDavid Gibson     long len;
22820f5cb298SDavid Gibson 
22830f5cb298SDavid Gibson     if (fd < 0) {
22840f5cb298SDavid Gibson         return -1;
22850f5cb298SDavid Gibson     }
22860f5cb298SDavid Gibson 
2287523e7b8aSAlexey Kardashevskiy     len = nb_table * sizeof(uint64_t);
22880f5cb298SDavid Gibson     if ((munmap(table, len) < 0) ||
22890f5cb298SDavid Gibson         (close(fd) < 0)) {
2290b5aec396SDavid Gibson         fprintf(stderr, "KVM: Unexpected error removing TCE table: %s",
2291b5aec396SDavid Gibson                 strerror(errno));
22920f5cb298SDavid Gibson         /* Leak the table */
22930f5cb298SDavid Gibson     }
22940f5cb298SDavid Gibson 
22950f5cb298SDavid Gibson     return 0;
22960f5cb298SDavid Gibson }
22970f5cb298SDavid Gibson 
22987f763a5dSDavid Gibson int kvmppc_reset_htab(int shift_hint)
22997f763a5dSDavid Gibson {
23007f763a5dSDavid Gibson     uint32_t shift = shift_hint;
23017f763a5dSDavid Gibson 
2302ace9a2cbSDavid Gibson     if (!kvm_enabled()) {
2303ace9a2cbSDavid Gibson         /* Full emulation, tell caller to allocate htab itself */
2304ace9a2cbSDavid Gibson         return 0;
2305ace9a2cbSDavid Gibson     }
23066977afdaSGreg Kurz     if (kvm_vm_check_extension(kvm_state, KVM_CAP_PPC_ALLOC_HTAB)) {
23077f763a5dSDavid Gibson         int ret;
23087f763a5dSDavid Gibson         ret = kvm_vm_ioctl(kvm_state, KVM_PPC_ALLOCATE_HTAB, &shift);
2309ace9a2cbSDavid Gibson         if (ret == -ENOTTY) {
2310c995e942SDavid Gibson             /*
2311c995e942SDavid Gibson              * At least some versions of PR KVM advertise the
2312ace9a2cbSDavid Gibson              * capability, but don't implement the ioctl().  Oops.
2313ace9a2cbSDavid Gibson              * Return 0 so that we allocate the htab in qemu, as is
2314c995e942SDavid Gibson              * correct for PR.
2315c995e942SDavid Gibson              */
2316ace9a2cbSDavid Gibson             return 0;
2317ace9a2cbSDavid Gibson         } else if (ret < 0) {
23187f763a5dSDavid Gibson             return ret;
23197f763a5dSDavid Gibson         }
23207f763a5dSDavid Gibson         return shift;
23217f763a5dSDavid Gibson     }
23227f763a5dSDavid Gibson 
2323c995e942SDavid Gibson     /*
2324c995e942SDavid Gibson      * We have a kernel that predates the htab reset calls.  For PR
2325ace9a2cbSDavid Gibson      * KVM, we need to allocate the htab ourselves, for an HV KVM of
2326c995e942SDavid Gibson      * this era, it has allocated a 16MB fixed size hash table
2327c995e942SDavid Gibson      * already.
2328c995e942SDavid Gibson      */
232996c9cff0SThomas Huth     if (kvmppc_is_pr(kvm_state)) {
2330ace9a2cbSDavid Gibson         /* PR - tell caller to allocate htab */
23317f763a5dSDavid Gibson         return 0;
2332ace9a2cbSDavid Gibson     } else {
2333ace9a2cbSDavid Gibson         /* HV - assume 16MB kernel allocated htab */
2334ace9a2cbSDavid Gibson         return 24;
2335ace9a2cbSDavid Gibson     }
23367f763a5dSDavid Gibson }
23377f763a5dSDavid Gibson 
2338a1e98583SDavid Gibson static inline uint32_t mfpvr(void)
2339a1e98583SDavid Gibson {
2340a1e98583SDavid Gibson     uint32_t pvr;
2341a1e98583SDavid Gibson 
2342a1e98583SDavid Gibson     asm ("mfpvr %0"
2343a1e98583SDavid Gibson          : "=r"(pvr));
2344a1e98583SDavid Gibson     return pvr;
2345a1e98583SDavid Gibson }
2346a1e98583SDavid Gibson 
2347a7342588SDavid Gibson static void alter_insns(uint64_t *word, uint64_t flags, bool on)
2348a7342588SDavid Gibson {
2349a7342588SDavid Gibson     if (on) {
2350a7342588SDavid Gibson         *word |= flags;
2351a7342588SDavid Gibson     } else {
2352a7342588SDavid Gibson         *word &= ~flags;
2353a7342588SDavid Gibson     }
2354a7342588SDavid Gibson }
2355a7342588SDavid Gibson 
23562985b86bSAndreas Färber static void kvmppc_host_cpu_class_init(ObjectClass *oc, void *data)
23572985b86bSAndreas Färber {
23582985b86bSAndreas Färber     PowerPCCPUClass *pcc = POWERPC_CPU_CLASS(oc);
23590cbad81fSDavid Gibson     uint32_t dcache_size = kvmppc_read_int_cpu_dt("d-cache-size");
23600cbad81fSDavid Gibson     uint32_t icache_size = kvmppc_read_int_cpu_dt("i-cache-size");
2361a1e98583SDavid Gibson 
2362cfe34f44SAndreas Färber     /* Now fix up the class with information we can query from the host */
23633bc9ccc0SAlexey Kardashevskiy     pcc->pvr = mfpvr();
2364a7342588SDavid Gibson 
23653f2ca480SDavid Gibson     alter_insns(&pcc->insns_flags, PPC_ALTIVEC,
23663f2ca480SDavid Gibson                 qemu_getauxval(AT_HWCAP) & PPC_FEATURE_HAS_ALTIVEC);
23673f2ca480SDavid Gibson     alter_insns(&pcc->insns_flags2, PPC2_VSX,
23683f2ca480SDavid Gibson                 qemu_getauxval(AT_HWCAP) & PPC_FEATURE_HAS_VSX);
23693f2ca480SDavid Gibson     alter_insns(&pcc->insns_flags2, PPC2_DFP,
23703f2ca480SDavid Gibson                 qemu_getauxval(AT_HWCAP) & PPC_FEATURE_HAS_DFP);
23710cbad81fSDavid Gibson 
23720cbad81fSDavid Gibson     if (dcache_size != -1) {
23730cbad81fSDavid Gibson         pcc->l1_dcache_size = dcache_size;
23740cbad81fSDavid Gibson     }
23750cbad81fSDavid Gibson 
23760cbad81fSDavid Gibson     if (icache_size != -1) {
23770cbad81fSDavid Gibson         pcc->l1_icache_size = icache_size;
23780cbad81fSDavid Gibson     }
2379c64abd1fSSam Bobroff 
2380c64abd1fSSam Bobroff #if defined(TARGET_PPC64)
2381c64abd1fSSam Bobroff     pcc->radix_page_info = kvm_get_radix_page_info();
23825f3066d8SDavid Gibson 
23835f3066d8SDavid Gibson     if ((pcc->pvr & 0xffffff00) == CPU_POWERPC_POWER9_DD1) {
23845f3066d8SDavid Gibson         /*
23855f3066d8SDavid Gibson          * POWER9 DD1 has some bugs which make it not really ISA 3.00
23865f3066d8SDavid Gibson          * compliant.  More importantly, advertising ISA 3.00
23875f3066d8SDavid Gibson          * architected mode may prevent guests from activating
23885f3066d8SDavid Gibson          * necessary DD1 workarounds.
23895f3066d8SDavid Gibson          */
23905f3066d8SDavid Gibson         pcc->pcr_supported &= ~(PCR_COMPAT_3_00 | PCR_COMPAT_2_07
23915f3066d8SDavid Gibson                                 | PCR_COMPAT_2_06 | PCR_COMPAT_2_05);
23925f3066d8SDavid Gibson     }
2393c64abd1fSSam Bobroff #endif /* defined(TARGET_PPC64) */
2394a1e98583SDavid Gibson }
2395a1e98583SDavid Gibson 
23963b961124SStuart Yoder bool kvmppc_has_cap_epr(void)
23973b961124SStuart Yoder {
23983b961124SStuart Yoder     return cap_epr;
23993b961124SStuart Yoder }
24003b961124SStuart Yoder 
240187a91de6SAlexander Graf bool kvmppc_has_cap_fixup_hcalls(void)
240287a91de6SAlexander Graf {
240387a91de6SAlexander Graf     return cap_fixup_hcalls;
240487a91de6SAlexander Graf }
240587a91de6SAlexander Graf 
2406bac3bf28SThomas Huth bool kvmppc_has_cap_htm(void)
2407bac3bf28SThomas Huth {
2408bac3bf28SThomas Huth     return cap_htm;
2409bac3bf28SThomas Huth }
2410bac3bf28SThomas Huth 
2411cf1c4cceSSam Bobroff bool kvmppc_has_cap_mmu_radix(void)
2412cf1c4cceSSam Bobroff {
2413cf1c4cceSSam Bobroff     return cap_mmu_radix;
2414cf1c4cceSSam Bobroff }
2415cf1c4cceSSam Bobroff 
2416cf1c4cceSSam Bobroff bool kvmppc_has_cap_mmu_hash_v3(void)
2417cf1c4cceSSam Bobroff {
2418cf1c4cceSSam Bobroff     return cap_mmu_hash_v3;
2419cf1c4cceSSam Bobroff }
2420cf1c4cceSSam Bobroff 
2421072f416aSSuraj Jitindar Singh static bool kvmppc_power8_host(void)
2422072f416aSSuraj Jitindar Singh {
2423072f416aSSuraj Jitindar Singh     bool ret = false;
2424072f416aSSuraj Jitindar Singh #ifdef TARGET_PPC64
2425072f416aSSuraj Jitindar Singh     {
2426072f416aSSuraj Jitindar Singh         uint32_t base_pvr = CPU_POWERPC_POWER_SERVER_MASK & mfpvr();
2427072f416aSSuraj Jitindar Singh         ret = (base_pvr == CPU_POWERPC_POWER8E_BASE) ||
2428072f416aSSuraj Jitindar Singh               (base_pvr == CPU_POWERPC_POWER8NVL_BASE) ||
2429072f416aSSuraj Jitindar Singh               (base_pvr == CPU_POWERPC_POWER8_BASE);
2430072f416aSSuraj Jitindar Singh     }
2431072f416aSSuraj Jitindar Singh #endif /* TARGET_PPC64 */
2432072f416aSSuraj Jitindar Singh     return ret;
2433072f416aSSuraj Jitindar Singh }
2434072f416aSSuraj Jitindar Singh 
24358fea7044SSuraj Jitindar Singh static int parse_cap_ppc_safe_cache(struct kvm_ppc_cpu_char c)
24368fea7044SSuraj Jitindar Singh {
2437072f416aSSuraj Jitindar Singh     bool l1d_thread_priv_req = !kvmppc_power8_host();
2438072f416aSSuraj Jitindar Singh 
24398fea7044SSuraj Jitindar Singh     if (~c.behaviour & c.behaviour_mask & H_CPU_BEHAV_L1D_FLUSH_PR) {
24408fea7044SSuraj Jitindar Singh         return 2;
2441072f416aSSuraj Jitindar Singh     } else if ((!l1d_thread_priv_req ||
2442072f416aSSuraj Jitindar Singh                 c.character & c.character_mask & H_CPU_CHAR_L1D_THREAD_PRIV) &&
24438fea7044SSuraj Jitindar Singh                (c.character & c.character_mask
24448fea7044SSuraj Jitindar Singh                 & (H_CPU_CHAR_L1D_FLUSH_ORI30 | H_CPU_CHAR_L1D_FLUSH_TRIG2))) {
24458fea7044SSuraj Jitindar Singh         return 1;
24468fea7044SSuraj Jitindar Singh     }
24478fea7044SSuraj Jitindar Singh 
24488fea7044SSuraj Jitindar Singh     return 0;
24498fea7044SSuraj Jitindar Singh }
24508fea7044SSuraj Jitindar Singh 
24518fea7044SSuraj Jitindar Singh static int parse_cap_ppc_safe_bounds_check(struct kvm_ppc_cpu_char c)
24528fea7044SSuraj Jitindar Singh {
24538fea7044SSuraj Jitindar Singh     if (~c.behaviour & c.behaviour_mask & H_CPU_BEHAV_BNDS_CHK_SPEC_BAR) {
24548fea7044SSuraj Jitindar Singh         return 2;
24558fea7044SSuraj Jitindar Singh     } else if (c.character & c.character_mask & H_CPU_CHAR_SPEC_BAR_ORI31) {
24568fea7044SSuraj Jitindar Singh         return 1;
24578fea7044SSuraj Jitindar Singh     }
24588fea7044SSuraj Jitindar Singh 
24598fea7044SSuraj Jitindar Singh     return 0;
24608fea7044SSuraj Jitindar Singh }
24618fea7044SSuraj Jitindar Singh 
24628fea7044SSuraj Jitindar Singh static int parse_cap_ppc_safe_indirect_branch(struct kvm_ppc_cpu_char c)
24638fea7044SSuraj Jitindar Singh {
2464399b2896SSuraj Jitindar Singh     if ((~c.behaviour & c.behaviour_mask & H_CPU_BEHAV_FLUSH_COUNT_CACHE) &&
2465399b2896SSuraj Jitindar Singh         (~c.character & c.character_mask & H_CPU_CHAR_CACHE_COUNT_DIS) &&
2466399b2896SSuraj Jitindar Singh         (~c.character & c.character_mask & H_CPU_CHAR_BCCTRL_SERIALISED)) {
2467399b2896SSuraj Jitindar Singh         return SPAPR_CAP_FIXED_NA;
2468399b2896SSuraj Jitindar Singh     } else if (c.behaviour & c.behaviour_mask & H_CPU_BEHAV_FLUSH_COUNT_CACHE) {
2469399b2896SSuraj Jitindar Singh         return SPAPR_CAP_WORKAROUND;
2470399b2896SSuraj Jitindar Singh     } else if (c.character & c.character_mask & H_CPU_CHAR_CACHE_COUNT_DIS) {
24718fea7044SSuraj Jitindar Singh         return  SPAPR_CAP_FIXED_CCD;
24728fea7044SSuraj Jitindar Singh     } else if (c.character & c.character_mask & H_CPU_CHAR_BCCTRL_SERIALISED) {
24738fea7044SSuraj Jitindar Singh         return SPAPR_CAP_FIXED_IBS;
24748fea7044SSuraj Jitindar Singh     }
24758fea7044SSuraj Jitindar Singh 
24768fea7044SSuraj Jitindar Singh     return 0;
24778fea7044SSuraj Jitindar Singh }
24788fea7044SSuraj Jitindar Singh 
24798ff43ee4SSuraj Jitindar Singh static int parse_cap_ppc_count_cache_flush_assist(struct kvm_ppc_cpu_char c)
24808ff43ee4SSuraj Jitindar Singh {
24818ff43ee4SSuraj Jitindar Singh     if (c.character & c.character_mask & H_CPU_CHAR_BCCTR_FLUSH_ASSIST) {
24828ff43ee4SSuraj Jitindar Singh         return 1;
24838ff43ee4SSuraj Jitindar Singh     }
24848ff43ee4SSuraj Jitindar Singh     return 0;
24858ff43ee4SSuraj Jitindar Singh }
24868ff43ee4SSuraj Jitindar Singh 
248738afd772SCédric Le Goater bool kvmppc_has_cap_xive(void)
248838afd772SCédric Le Goater {
248938afd772SCédric Le Goater     return cap_xive;
249038afd772SCédric Le Goater }
249138afd772SCédric Le Goater 
24928acc2ae5SSuraj Jitindar Singh static void kvmppc_get_cpu_characteristics(KVMState *s)
24938acc2ae5SSuraj Jitindar Singh {
24948acc2ae5SSuraj Jitindar Singh     struct kvm_ppc_cpu_char c;
24958acc2ae5SSuraj Jitindar Singh     int ret;
24968acc2ae5SSuraj Jitindar Singh 
24978acc2ae5SSuraj Jitindar Singh     /* Assume broken */
24988acc2ae5SSuraj Jitindar Singh     cap_ppc_safe_cache = 0;
24998acc2ae5SSuraj Jitindar Singh     cap_ppc_safe_bounds_check = 0;
25008acc2ae5SSuraj Jitindar Singh     cap_ppc_safe_indirect_branch = 0;
25018acc2ae5SSuraj Jitindar Singh 
25028acc2ae5SSuraj Jitindar Singh     ret = kvm_vm_check_extension(s, KVM_CAP_PPC_GET_CPU_CHAR);
25038acc2ae5SSuraj Jitindar Singh     if (!ret) {
25048acc2ae5SSuraj Jitindar Singh         return;
25058acc2ae5SSuraj Jitindar Singh     }
25068acc2ae5SSuraj Jitindar Singh     ret = kvm_vm_ioctl(s, KVM_PPC_GET_CPU_CHAR, &c);
25078acc2ae5SSuraj Jitindar Singh     if (ret < 0) {
25088acc2ae5SSuraj Jitindar Singh         return;
25098acc2ae5SSuraj Jitindar Singh     }
25108fea7044SSuraj Jitindar Singh 
25118fea7044SSuraj Jitindar Singh     cap_ppc_safe_cache = parse_cap_ppc_safe_cache(c);
25128fea7044SSuraj Jitindar Singh     cap_ppc_safe_bounds_check = parse_cap_ppc_safe_bounds_check(c);
25138fea7044SSuraj Jitindar Singh     cap_ppc_safe_indirect_branch = parse_cap_ppc_safe_indirect_branch(c);
25148ff43ee4SSuraj Jitindar Singh     cap_ppc_count_cache_flush_assist =
25158ff43ee4SSuraj Jitindar Singh         parse_cap_ppc_count_cache_flush_assist(c);
25168acc2ae5SSuraj Jitindar Singh }
25178acc2ae5SSuraj Jitindar Singh 
25188acc2ae5SSuraj Jitindar Singh int kvmppc_get_cap_safe_cache(void)
25198acc2ae5SSuraj Jitindar Singh {
25208acc2ae5SSuraj Jitindar Singh     return cap_ppc_safe_cache;
25218acc2ae5SSuraj Jitindar Singh }
25228acc2ae5SSuraj Jitindar Singh 
25238acc2ae5SSuraj Jitindar Singh int kvmppc_get_cap_safe_bounds_check(void)
25248acc2ae5SSuraj Jitindar Singh {
25258acc2ae5SSuraj Jitindar Singh     return cap_ppc_safe_bounds_check;
25268acc2ae5SSuraj Jitindar Singh }
25278acc2ae5SSuraj Jitindar Singh 
25288acc2ae5SSuraj Jitindar Singh int kvmppc_get_cap_safe_indirect_branch(void)
25298acc2ae5SSuraj Jitindar Singh {
25308acc2ae5SSuraj Jitindar Singh     return cap_ppc_safe_indirect_branch;
25318acc2ae5SSuraj Jitindar Singh }
25328acc2ae5SSuraj Jitindar Singh 
25338ff43ee4SSuraj Jitindar Singh int kvmppc_get_cap_count_cache_flush_assist(void)
25348ff43ee4SSuraj Jitindar Singh {
25358ff43ee4SSuraj Jitindar Singh     return cap_ppc_count_cache_flush_assist;
25368ff43ee4SSuraj Jitindar Singh }
25378ff43ee4SSuraj Jitindar Singh 
2538b9a477b7SSuraj Jitindar Singh bool kvmppc_has_cap_nested_kvm_hv(void)
2539b9a477b7SSuraj Jitindar Singh {
2540b9a477b7SSuraj Jitindar Singh     return !!cap_ppc_nested_kvm_hv;
2541b9a477b7SSuraj Jitindar Singh }
2542b9a477b7SSuraj Jitindar Singh 
2543b9a477b7SSuraj Jitindar Singh int kvmppc_set_cap_nested_kvm_hv(int enable)
2544b9a477b7SSuraj Jitindar Singh {
2545b9a477b7SSuraj Jitindar Singh     return kvm_vm_enable_cap(kvm_state, KVM_CAP_PPC_NESTED_HV, 0, enable);
2546b9a477b7SSuraj Jitindar Singh }
2547b9a477b7SSuraj Jitindar Singh 
25489ded780cSAlexey Kardashevskiy bool kvmppc_has_cap_spapr_vfio(void)
25499ded780cSAlexey Kardashevskiy {
25509ded780cSAlexey Kardashevskiy     return cap_spapr_vfio;
25519ded780cSAlexey Kardashevskiy }
25529ded780cSAlexey Kardashevskiy 
25537d050527SSuraj Jitindar Singh int kvmppc_get_cap_large_decr(void)
25547d050527SSuraj Jitindar Singh {
25557d050527SSuraj Jitindar Singh     return cap_large_decr;
25567d050527SSuraj Jitindar Singh }
25577d050527SSuraj Jitindar Singh 
25587d050527SSuraj Jitindar Singh int kvmppc_enable_cap_large_decr(PowerPCCPU *cpu, int enable)
25597d050527SSuraj Jitindar Singh {
25607d050527SSuraj Jitindar Singh     CPUState *cs = CPU(cpu);
25617d050527SSuraj Jitindar Singh     uint64_t lpcr;
25627d050527SSuraj Jitindar Singh 
25637d050527SSuraj Jitindar Singh     kvm_get_one_reg(cs, KVM_REG_PPC_LPCR_64, &lpcr);
25647d050527SSuraj Jitindar Singh     /* Do we need to modify the LPCR? */
25657d050527SSuraj Jitindar Singh     if (!!(lpcr & LPCR_LD) != !!enable) {
25667d050527SSuraj Jitindar Singh         if (enable) {
25677d050527SSuraj Jitindar Singh             lpcr |= LPCR_LD;
25687d050527SSuraj Jitindar Singh         } else {
25697d050527SSuraj Jitindar Singh             lpcr &= ~LPCR_LD;
25707d050527SSuraj Jitindar Singh         }
25717d050527SSuraj Jitindar Singh         kvm_set_one_reg(cs, KVM_REG_PPC_LPCR_64, &lpcr);
25727d050527SSuraj Jitindar Singh         kvm_get_one_reg(cs, KVM_REG_PPC_LPCR_64, &lpcr);
25737d050527SSuraj Jitindar Singh 
25747d050527SSuraj Jitindar Singh         if (!!(lpcr & LPCR_LD) != !!enable) {
25757d050527SSuraj Jitindar Singh             return -1;
25767d050527SSuraj Jitindar Singh         }
25777d050527SSuraj Jitindar Singh     }
25787d050527SSuraj Jitindar Singh 
25797d050527SSuraj Jitindar Singh     return 0;
25807d050527SSuraj Jitindar Singh }
25817d050527SSuraj Jitindar Singh 
258252b2519cSThomas Huth PowerPCCPUClass *kvm_ppc_get_host_cpu_class(void)
258352b2519cSThomas Huth {
258452b2519cSThomas Huth     uint32_t host_pvr = mfpvr();
258552b2519cSThomas Huth     PowerPCCPUClass *pvr_pcc;
258652b2519cSThomas Huth 
258752b2519cSThomas Huth     pvr_pcc = ppc_cpu_class_by_pvr(host_pvr);
258852b2519cSThomas Huth     if (pvr_pcc == NULL) {
258952b2519cSThomas Huth         pvr_pcc = ppc_cpu_class_by_pvr_mask(host_pvr);
259052b2519cSThomas Huth     }
259152b2519cSThomas Huth 
259252b2519cSThomas Huth     return pvr_pcc;
259352b2519cSThomas Huth }
259452b2519cSThomas Huth 
25952e9c10ebSIgor Mammedov static int kvm_ppc_register_host_cpu_type(MachineState *ms)
25965ba4576bSAndreas Färber {
25975ba4576bSAndreas Färber     TypeInfo type_info = {
25985ba4576bSAndreas Färber         .name = TYPE_HOST_POWERPC_CPU,
25995ba4576bSAndreas Färber         .class_init = kvmppc_host_cpu_class_init,
26005ba4576bSAndreas Färber     };
26012e9c10ebSIgor Mammedov     MachineClass *mc = MACHINE_GET_CLASS(ms);
26025ba4576bSAndreas Färber     PowerPCCPUClass *pvr_pcc;
260392e926e1SGreg Kurz     ObjectClass *oc;
26045b79b1caSAlexey Kardashevskiy     DeviceClass *dc;
2605715d4b96SThomas Huth     int i;
26065ba4576bSAndreas Färber 
260752b2519cSThomas Huth     pvr_pcc = kvm_ppc_get_host_cpu_class();
26083bc9ccc0SAlexey Kardashevskiy     if (pvr_pcc == NULL) {
26095ba4576bSAndreas Färber         return -1;
26105ba4576bSAndreas Färber     }
26115ba4576bSAndreas Färber     type_info.parent = object_class_get_name(OBJECT_CLASS(pvr_pcc));
26125ba4576bSAndreas Färber     type_register(&type_info);
26132e9c10ebSIgor Mammedov     if (object_dynamic_cast(OBJECT(ms), TYPE_SPAPR_MACHINE)) {
26142e9c10ebSIgor Mammedov         /* override TCG default cpu type with 'host' cpu model */
26152e9c10ebSIgor Mammedov         mc->default_cpu_type = TYPE_HOST_POWERPC_CPU;
26162e9c10ebSIgor Mammedov     }
26175b79b1caSAlexey Kardashevskiy 
261892e926e1SGreg Kurz     oc = object_class_by_name(type_info.name);
261992e926e1SGreg Kurz     g_assert(oc);
262092e926e1SGreg Kurz 
2621715d4b96SThomas Huth     /*
2622715d4b96SThomas Huth      * Update generic CPU family class alias (e.g. on a POWER8NVL host,
2623715d4b96SThomas Huth      * we want "POWER8" to be a "family" alias that points to the current
2624715d4b96SThomas Huth      * host CPU type, too)
2625715d4b96SThomas Huth      */
2626715d4b96SThomas Huth     dc = DEVICE_CLASS(ppc_cpu_get_family_class(pvr_pcc));
2627715d4b96SThomas Huth     for (i = 0; ppc_cpu_aliases[i].alias != NULL; i++) {
2628c5354f54SIgor Mammedov         if (strcasecmp(ppc_cpu_aliases[i].alias, dc->desc) == 0) {
2629715d4b96SThomas Huth             char *suffix;
2630715d4b96SThomas Huth 
2631715d4b96SThomas Huth             ppc_cpu_aliases[i].model = g_strdup(object_class_get_name(oc));
2632c9137065SIgor Mammedov             suffix = strstr(ppc_cpu_aliases[i].model, POWERPC_CPU_TYPE_SUFFIX);
2633715d4b96SThomas Huth             if (suffix) {
2634715d4b96SThomas Huth                 *suffix = 0;
2635715d4b96SThomas Huth             }
2636715d4b96SThomas Huth             break;
2637715d4b96SThomas Huth         }
2638715d4b96SThomas Huth     }
2639715d4b96SThomas Huth 
26405ba4576bSAndreas Färber     return 0;
26415ba4576bSAndreas Färber }
26425ba4576bSAndreas Färber 
2643feaa64c4SDavid Gibson int kvmppc_define_rtas_kernel_token(uint32_t token, const char *function)
2644feaa64c4SDavid Gibson {
2645feaa64c4SDavid Gibson     struct kvm_rtas_token_args args = {
2646feaa64c4SDavid Gibson         .token = token,
2647feaa64c4SDavid Gibson     };
2648feaa64c4SDavid Gibson 
2649feaa64c4SDavid Gibson     if (!kvm_check_extension(kvm_state, KVM_CAP_PPC_RTAS)) {
2650feaa64c4SDavid Gibson         return -ENOENT;
2651feaa64c4SDavid Gibson     }
2652feaa64c4SDavid Gibson 
26537701aeedSCédric Le Goater     strncpy(args.name, function, sizeof(args.name) - 1);
2654feaa64c4SDavid Gibson 
2655feaa64c4SDavid Gibson     return kvm_vm_ioctl(kvm_state, KVM_PPC_RTAS_DEFINE_TOKEN, &args);
2656feaa64c4SDavid Gibson }
265712b1143bSDavid Gibson 
265814b0d748SGreg Kurz int kvmppc_get_htab_fd(bool write, uint64_t index, Error **errp)
2659e68cb8b4SAlexey Kardashevskiy {
2660e68cb8b4SAlexey Kardashevskiy     struct kvm_get_htab_fd s = {
2661e68cb8b4SAlexey Kardashevskiy         .flags = write ? KVM_GET_HTAB_WRITE : 0,
266214b0d748SGreg Kurz         .start_index = index,
2663e68cb8b4SAlexey Kardashevskiy     };
266482be8e73SGreg Kurz     int ret;
2665e68cb8b4SAlexey Kardashevskiy 
2666e68cb8b4SAlexey Kardashevskiy     if (!cap_htab_fd) {
266714b0d748SGreg Kurz         error_setg(errp, "KVM version doesn't support %s the HPT",
266814b0d748SGreg Kurz                    write ? "writing" : "reading");
266982be8e73SGreg Kurz         return -ENOTSUP;
2670e68cb8b4SAlexey Kardashevskiy     }
2671e68cb8b4SAlexey Kardashevskiy 
267282be8e73SGreg Kurz     ret = kvm_vm_ioctl(kvm_state, KVM_PPC_GET_HTAB_FD, &s);
267382be8e73SGreg Kurz     if (ret < 0) {
267414b0d748SGreg Kurz         error_setg(errp, "Unable to open fd for %s HPT %s KVM: %s",
267514b0d748SGreg Kurz                    write ? "writing" : "reading", write ? "to" : "from",
267614b0d748SGreg Kurz                    strerror(errno));
267782be8e73SGreg Kurz         return -errno;
267882be8e73SGreg Kurz     }
267982be8e73SGreg Kurz 
268082be8e73SGreg Kurz     return ret;
2681e68cb8b4SAlexey Kardashevskiy }
2682e68cb8b4SAlexey Kardashevskiy 
2683e68cb8b4SAlexey Kardashevskiy int kvmppc_save_htab(QEMUFile *f, int fd, size_t bufsize, int64_t max_ns)
2684e68cb8b4SAlexey Kardashevskiy {
2685bc72ad67SAlex Bligh     int64_t starttime = qemu_clock_get_ns(QEMU_CLOCK_REALTIME);
2686e68cb8b4SAlexey Kardashevskiy     uint8_t buf[bufsize];
2687e68cb8b4SAlexey Kardashevskiy     ssize_t rc;
2688e68cb8b4SAlexey Kardashevskiy 
2689e68cb8b4SAlexey Kardashevskiy     do {
2690e68cb8b4SAlexey Kardashevskiy         rc = read(fd, buf, bufsize);
2691e68cb8b4SAlexey Kardashevskiy         if (rc < 0) {
2692e68cb8b4SAlexey Kardashevskiy             fprintf(stderr, "Error reading data from KVM HTAB fd: %s\n",
2693e68cb8b4SAlexey Kardashevskiy                     strerror(errno));
2694e68cb8b4SAlexey Kardashevskiy             return rc;
2695e68cb8b4SAlexey Kardashevskiy         } else if (rc) {
2696e094c4c1SCédric Le Goater             uint8_t *buffer = buf;
2697e094c4c1SCédric Le Goater             ssize_t n = rc;
2698e094c4c1SCédric Le Goater             while (n) {
2699e094c4c1SCédric Le Goater                 struct kvm_get_htab_header *head =
2700e094c4c1SCédric Le Goater                     (struct kvm_get_htab_header *) buffer;
2701e094c4c1SCédric Le Goater                 size_t chunksize = sizeof(*head) +
2702e094c4c1SCédric Le Goater                      HASH_PTE_SIZE_64 * head->n_valid;
2703e094c4c1SCédric Le Goater 
2704e094c4c1SCédric Le Goater                 qemu_put_be32(f, head->index);
2705e094c4c1SCédric Le Goater                 qemu_put_be16(f, head->n_valid);
2706e094c4c1SCédric Le Goater                 qemu_put_be16(f, head->n_invalid);
2707e094c4c1SCédric Le Goater                 qemu_put_buffer(f, (void *)(head + 1),
2708e094c4c1SCédric Le Goater                                 HASH_PTE_SIZE_64 * head->n_valid);
2709e094c4c1SCédric Le Goater 
2710e094c4c1SCédric Le Goater                 buffer += chunksize;
2711e094c4c1SCédric Le Goater                 n -= chunksize;
2712e094c4c1SCédric Le Goater             }
2713e68cb8b4SAlexey Kardashevskiy         }
2714e68cb8b4SAlexey Kardashevskiy     } while ((rc != 0)
2715c995e942SDavid Gibson              && ((max_ns < 0) ||
2716c995e942SDavid Gibson                  ((qemu_clock_get_ns(QEMU_CLOCK_REALTIME) - starttime) < max_ns)));
2717e68cb8b4SAlexey Kardashevskiy 
2718e68cb8b4SAlexey Kardashevskiy     return (rc == 0) ? 1 : 0;
2719e68cb8b4SAlexey Kardashevskiy }
2720e68cb8b4SAlexey Kardashevskiy 
2721e68cb8b4SAlexey Kardashevskiy int kvmppc_load_htab_chunk(QEMUFile *f, int fd, uint32_t index,
2722e68cb8b4SAlexey Kardashevskiy                            uint16_t n_valid, uint16_t n_invalid)
2723e68cb8b4SAlexey Kardashevskiy {
2724e68cb8b4SAlexey Kardashevskiy     struct kvm_get_htab_header *buf;
2725e68cb8b4SAlexey Kardashevskiy     size_t chunksize = sizeof(*buf) + n_valid * HASH_PTE_SIZE_64;
2726e68cb8b4SAlexey Kardashevskiy     ssize_t rc;
2727e68cb8b4SAlexey Kardashevskiy 
2728e68cb8b4SAlexey Kardashevskiy     buf = alloca(chunksize);
2729e68cb8b4SAlexey Kardashevskiy     buf->index = index;
2730e68cb8b4SAlexey Kardashevskiy     buf->n_valid = n_valid;
2731e68cb8b4SAlexey Kardashevskiy     buf->n_invalid = n_invalid;
2732e68cb8b4SAlexey Kardashevskiy 
2733e68cb8b4SAlexey Kardashevskiy     qemu_get_buffer(f, (void *)(buf + 1), HASH_PTE_SIZE_64 * n_valid);
2734e68cb8b4SAlexey Kardashevskiy 
2735e68cb8b4SAlexey Kardashevskiy     rc = write(fd, buf, chunksize);
2736e68cb8b4SAlexey Kardashevskiy     if (rc < 0) {
2737e68cb8b4SAlexey Kardashevskiy         fprintf(stderr, "Error writing KVM hash table: %s\n",
2738e68cb8b4SAlexey Kardashevskiy                 strerror(errno));
2739e68cb8b4SAlexey Kardashevskiy         return rc;
2740e68cb8b4SAlexey Kardashevskiy     }
2741e68cb8b4SAlexey Kardashevskiy     if (rc != chunksize) {
2742e68cb8b4SAlexey Kardashevskiy         /* We should never get a short write on a single chunk */
2743e68cb8b4SAlexey Kardashevskiy         fprintf(stderr, "Short write, restoring KVM hash table\n");
2744e68cb8b4SAlexey Kardashevskiy         return -1;
2745e68cb8b4SAlexey Kardashevskiy     }
2746e68cb8b4SAlexey Kardashevskiy     return 0;
2747e68cb8b4SAlexey Kardashevskiy }
2748e68cb8b4SAlexey Kardashevskiy 
274920d695a9SAndreas Färber bool kvm_arch_stop_on_emulation_error(CPUState *cpu)
27504513d923SGleb Natapov {
27514513d923SGleb Natapov     return true;
27524513d923SGleb Natapov }
2753a1b87fe0SJan Kiszka 
275482169660SScott Wood void kvm_arch_init_irq_routing(KVMState *s)
275582169660SScott Wood {
275682169660SScott Wood }
2757c65f9a07SGreg Kurz 
27581ad9f0a4SDavid Gibson void kvmppc_read_hptes(ppc_hash_pte64_t *hptes, hwaddr ptex, int n)
27591ad9f0a4SDavid Gibson {
27601ad9f0a4SDavid Gibson     int fd, rc;
27611ad9f0a4SDavid Gibson     int i;
27627c43bca0SAneesh Kumar K.V 
276314b0d748SGreg Kurz     fd = kvmppc_get_htab_fd(false, ptex, &error_abort);
27641ad9f0a4SDavid Gibson 
27651ad9f0a4SDavid Gibson     i = 0;
27661ad9f0a4SDavid Gibson     while (i < n) {
27671ad9f0a4SDavid Gibson         struct kvm_get_htab_header *hdr;
27681ad9f0a4SDavid Gibson         int m = n < HPTES_PER_GROUP ? n : HPTES_PER_GROUP;
27691ad9f0a4SDavid Gibson         char buf[sizeof(*hdr) + m * HASH_PTE_SIZE_64];
27701ad9f0a4SDavid Gibson 
27711ad9f0a4SDavid Gibson         rc = read(fd, buf, sizeof(buf));
27721ad9f0a4SDavid Gibson         if (rc < 0) {
27731ad9f0a4SDavid Gibson             hw_error("kvmppc_read_hptes: Unable to read HPTEs");
27741ad9f0a4SDavid Gibson         }
27751ad9f0a4SDavid Gibson 
27761ad9f0a4SDavid Gibson         hdr = (struct kvm_get_htab_header *)buf;
27771ad9f0a4SDavid Gibson         while ((i < n) && ((char *)hdr < (buf + rc))) {
2778a36593e1SAlexey Kardashevskiy             int invalid = hdr->n_invalid, valid = hdr->n_valid;
27791ad9f0a4SDavid Gibson 
27801ad9f0a4SDavid Gibson             if (hdr->index != (ptex + i)) {
27811ad9f0a4SDavid Gibson                 hw_error("kvmppc_read_hptes: Unexpected HPTE index %"PRIu32
27821ad9f0a4SDavid Gibson                          " != (%"HWADDR_PRIu" + %d", hdr->index, ptex, i);
27831ad9f0a4SDavid Gibson             }
27841ad9f0a4SDavid Gibson 
2785a36593e1SAlexey Kardashevskiy             if (n - i < valid) {
2786a36593e1SAlexey Kardashevskiy                 valid = n - i;
2787a36593e1SAlexey Kardashevskiy             }
2788a36593e1SAlexey Kardashevskiy             memcpy(hptes + i, hdr + 1, HASH_PTE_SIZE_64 * valid);
2789a36593e1SAlexey Kardashevskiy             i += valid;
27901ad9f0a4SDavid Gibson 
27911ad9f0a4SDavid Gibson             if ((n - i) < invalid) {
27921ad9f0a4SDavid Gibson                 invalid = n - i;
27931ad9f0a4SDavid Gibson             }
27941ad9f0a4SDavid Gibson             memset(hptes + i, 0, invalid * HASH_PTE_SIZE_64);
2795a36593e1SAlexey Kardashevskiy             i += invalid;
27961ad9f0a4SDavid Gibson 
27971ad9f0a4SDavid Gibson             hdr = (struct kvm_get_htab_header *)
27981ad9f0a4SDavid Gibson                 ((char *)(hdr + 1) + HASH_PTE_SIZE_64 * hdr->n_valid);
27991ad9f0a4SDavid Gibson         }
28001ad9f0a4SDavid Gibson     }
28011ad9f0a4SDavid Gibson 
28021ad9f0a4SDavid Gibson     close(fd);
28031ad9f0a4SDavid Gibson }
28041ad9f0a4SDavid Gibson 
28051ad9f0a4SDavid Gibson void kvmppc_write_hpte(hwaddr ptex, uint64_t pte0, uint64_t pte1)
28067c43bca0SAneesh Kumar K.V {
28071ad9f0a4SDavid Gibson     int fd, rc;
28081ad9f0a4SDavid Gibson     struct {
28091ad9f0a4SDavid Gibson         struct kvm_get_htab_header hdr;
28101ad9f0a4SDavid Gibson         uint64_t pte0;
28111ad9f0a4SDavid Gibson         uint64_t pte1;
28121ad9f0a4SDavid Gibson     } buf;
2813c1385933SAneesh Kumar K.V 
281414b0d748SGreg Kurz     fd = kvmppc_get_htab_fd(true, 0 /* Ignored */, &error_abort);
2815c1385933SAneesh Kumar K.V 
28161ad9f0a4SDavid Gibson     buf.hdr.n_valid = 1;
28171ad9f0a4SDavid Gibson     buf.hdr.n_invalid = 0;
28181ad9f0a4SDavid Gibson     buf.hdr.index = ptex;
28191ad9f0a4SDavid Gibson     buf.pte0 = cpu_to_be64(pte0);
28201ad9f0a4SDavid Gibson     buf.pte1 = cpu_to_be64(pte1);
28211ad9f0a4SDavid Gibson 
28221ad9f0a4SDavid Gibson     rc = write(fd, &buf, sizeof(buf));
28231ad9f0a4SDavid Gibson     if (rc != sizeof(buf)) {
28241ad9f0a4SDavid Gibson         hw_error("kvmppc_write_hpte: Unable to update KVM HPT");
2825c1385933SAneesh Kumar K.V     }
28261ad9f0a4SDavid Gibson     close(fd);
2827c1385933SAneesh Kumar K.V }
28289e03a040SFrank Blaschka 
28299e03a040SFrank Blaschka int kvm_arch_fixup_msi_route(struct kvm_irq_routing_entry *route,
2830dc9f06caSPavel Fedin                              uint64_t address, uint32_t data, PCIDevice *dev)
28319e03a040SFrank Blaschka {
28329e03a040SFrank Blaschka     return 0;
28339e03a040SFrank Blaschka }
28341850b6b7SEric Auger 
283538d87493SPeter Xu int kvm_arch_add_msi_route_post(struct kvm_irq_routing_entry *route,
283638d87493SPeter Xu                                 int vector, PCIDevice *dev)
283738d87493SPeter Xu {
283838d87493SPeter Xu     return 0;
283938d87493SPeter Xu }
284038d87493SPeter Xu 
284138d87493SPeter Xu int kvm_arch_release_virq_post(int virq)
284238d87493SPeter Xu {
284338d87493SPeter Xu     return 0;
284438d87493SPeter Xu }
284538d87493SPeter Xu 
28461850b6b7SEric Auger int kvm_arch_msi_data_to_gsi(uint32_t data)
28471850b6b7SEric Auger {
28481850b6b7SEric Auger     return data & 0xffff;
28491850b6b7SEric Auger }
28504d9392beSThomas Huth 
28514d9392beSThomas Huth int kvmppc_enable_hwrng(void)
28524d9392beSThomas Huth {
28534d9392beSThomas Huth     if (!kvm_enabled() || !kvm_check_extension(kvm_state, KVM_CAP_PPC_HWRNG)) {
28544d9392beSThomas Huth         return -1;
28554d9392beSThomas Huth     }
28564d9392beSThomas Huth 
28574d9392beSThomas Huth     return kvmppc_enable_hcall(kvm_state, H_RANDOM);
28584d9392beSThomas Huth }
285930f4b05bSDavid Gibson 
286030f4b05bSDavid Gibson void kvmppc_check_papr_resize_hpt(Error **errp)
286130f4b05bSDavid Gibson {
286230f4b05bSDavid Gibson     if (!kvm_enabled()) {
2863b55d295eSDavid Gibson         return; /* No KVM, we're good */
2864b55d295eSDavid Gibson     }
2865b55d295eSDavid Gibson 
2866b55d295eSDavid Gibson     if (cap_resize_hpt) {
2867b55d295eSDavid Gibson         return; /* Kernel has explicit support, we're good */
2868b55d295eSDavid Gibson     }
2869b55d295eSDavid Gibson 
2870b55d295eSDavid Gibson     /* Otherwise fallback on looking for PR KVM */
2871b55d295eSDavid Gibson     if (kvmppc_is_pr(kvm_state)) {
287230f4b05bSDavid Gibson         return;
287330f4b05bSDavid Gibson     }
287430f4b05bSDavid Gibson 
287530f4b05bSDavid Gibson     error_setg(errp,
287630f4b05bSDavid Gibson                "Hash page table resizing not available with this KVM version");
287730f4b05bSDavid Gibson }
2878b55d295eSDavid Gibson 
2879b55d295eSDavid Gibson int kvmppc_resize_hpt_prepare(PowerPCCPU *cpu, target_ulong flags, int shift)
2880b55d295eSDavid Gibson {
2881b55d295eSDavid Gibson     CPUState *cs = CPU(cpu);
2882b55d295eSDavid Gibson     struct kvm_ppc_resize_hpt rhpt = {
2883b55d295eSDavid Gibson         .flags = flags,
2884b55d295eSDavid Gibson         .shift = shift,
2885b55d295eSDavid Gibson     };
2886b55d295eSDavid Gibson 
2887b55d295eSDavid Gibson     if (!cap_resize_hpt) {
2888b55d295eSDavid Gibson         return -ENOSYS;
2889b55d295eSDavid Gibson     }
2890b55d295eSDavid Gibson 
2891b55d295eSDavid Gibson     return kvm_vm_ioctl(cs->kvm_state, KVM_PPC_RESIZE_HPT_PREPARE, &rhpt);
2892b55d295eSDavid Gibson }
2893b55d295eSDavid Gibson 
2894b55d295eSDavid Gibson int kvmppc_resize_hpt_commit(PowerPCCPU *cpu, target_ulong flags, int shift)
2895b55d295eSDavid Gibson {
2896b55d295eSDavid Gibson     CPUState *cs = CPU(cpu);
2897b55d295eSDavid Gibson     struct kvm_ppc_resize_hpt rhpt = {
2898b55d295eSDavid Gibson         .flags = flags,
2899b55d295eSDavid Gibson         .shift = shift,
2900b55d295eSDavid Gibson     };
2901b55d295eSDavid Gibson 
2902b55d295eSDavid Gibson     if (!cap_resize_hpt) {
2903b55d295eSDavid Gibson         return -ENOSYS;
2904b55d295eSDavid Gibson     }
2905b55d295eSDavid Gibson 
2906b55d295eSDavid Gibson     return kvm_vm_ioctl(cs->kvm_state, KVM_PPC_RESIZE_HPT_COMMIT, &rhpt);
2907b55d295eSDavid Gibson }
2908b55d295eSDavid Gibson 
2909c363a37aSDaniel Henrique Barboza /*
2910c363a37aSDaniel Henrique Barboza  * This is a helper function to detect a post migration scenario
2911c363a37aSDaniel Henrique Barboza  * in which a guest, running as KVM-HV, freezes in cpu_post_load because
2912c363a37aSDaniel Henrique Barboza  * the guest kernel can't handle a PVR value other than the actual host
2913c363a37aSDaniel Henrique Barboza  * PVR in KVM_SET_SREGS, even if pvr_match() returns true.
2914c363a37aSDaniel Henrique Barboza  *
2915c363a37aSDaniel Henrique Barboza  * If we don't have cap_ppc_pvr_compat and we're not running in PR
2916c363a37aSDaniel Henrique Barboza  * (so, we're HV), return true. The workaround itself is done in
2917c363a37aSDaniel Henrique Barboza  * cpu_post_load.
2918c363a37aSDaniel Henrique Barboza  *
2919c363a37aSDaniel Henrique Barboza  * The order here is important: we'll only check for KVM PR as a
2920c363a37aSDaniel Henrique Barboza  * fallback if the guest kernel can't handle the situation itself.
2921c363a37aSDaniel Henrique Barboza  * We need to avoid as much as possible querying the running KVM type
2922c363a37aSDaniel Henrique Barboza  * in QEMU level.
2923c363a37aSDaniel Henrique Barboza  */
2924c363a37aSDaniel Henrique Barboza bool kvmppc_pvr_workaround_required(PowerPCCPU *cpu)
2925c363a37aSDaniel Henrique Barboza {
2926c363a37aSDaniel Henrique Barboza     CPUState *cs = CPU(cpu);
2927c363a37aSDaniel Henrique Barboza 
2928c363a37aSDaniel Henrique Barboza     if (!kvm_enabled()) {
2929c363a37aSDaniel Henrique Barboza         return false;
2930c363a37aSDaniel Henrique Barboza     }
2931c363a37aSDaniel Henrique Barboza 
2932c363a37aSDaniel Henrique Barboza     if (cap_ppc_pvr_compat) {
2933c363a37aSDaniel Henrique Barboza         return false;
2934c363a37aSDaniel Henrique Barboza     }
2935c363a37aSDaniel Henrique Barboza 
2936c363a37aSDaniel Henrique Barboza     return !kvmppc_is_pr(cs->kvm_state);
2937c363a37aSDaniel Henrique Barboza }
2938a84f7179SNikunj A Dadhania 
2939a84f7179SNikunj A Dadhania void kvmppc_set_reg_ppc_online(PowerPCCPU *cpu, unsigned int online)
2940a84f7179SNikunj A Dadhania {
2941a84f7179SNikunj A Dadhania     CPUState *cs = CPU(cpu);
2942a84f7179SNikunj A Dadhania 
2943a84f7179SNikunj A Dadhania     if (kvm_enabled()) {
2944a84f7179SNikunj A Dadhania         kvm_set_one_reg(cs, KVM_REG_PPC_ONLINE, &online);
2945a84f7179SNikunj A Dadhania     }
2946a84f7179SNikunj A Dadhania }
2947*9723295aSGreg Kurz 
2948*9723295aSGreg Kurz void kvmppc_set_reg_tb_offset(PowerPCCPU *cpu, int64_t tb_offset)
2949*9723295aSGreg Kurz {
2950*9723295aSGreg Kurz     CPUState *cs = CPU(cpu);
2951*9723295aSGreg Kurz 
2952*9723295aSGreg Kurz     if (kvm_enabled()) {
2953*9723295aSGreg Kurz         kvm_set_one_reg(cs, KVM_REG_PPC_TB_OFFSET, &tb_offset);
2954*9723295aSGreg Kurz     }
2955*9723295aSGreg Kurz }
2956