2 * PowerPC implementation of KVM hooks
4 * Copyright IBM Corp. 2007
5 * Copyright (C) 2011 Freescale Semiconductor, Inc.
8 * Jerone Young <jyoung5@us.ibm.com>
9 * Christian Ehrhardt <ehrhardt@linux.vnet.ibm.com>
10 * Hollis Blanchard <hollisb@us.ibm.com>
12 * This work is licensed under the terms of the GNU GPL, version 2 or later.
13 * See the COPYING file in the top-level directory.
17 #include "qemu/osdep.h"
19 #include <sys/ioctl.h>
22 #include <linux/kvm.h>
24 #include "qemu-common.h"
25 #include "qapi/error.h"
26 #include "qemu/error-report.h"
28 #include "cpu-models.h"
29 #include "qemu/timer.h"
30 #include "sysemu/sysemu.h"
31 #include "sysemu/hw_accel.h"
33 #include "sysemu/cpus.h"
34 #include "sysemu/device_tree.h"
35 #include "mmu-hash64.h"
37 #include "hw/sysbus.h"
38 #include "hw/ppc/spapr.h"
39 #include "hw/ppc/spapr_cpu_core.h"
40 #include "hw/ppc/ppc.h"
41 #include "sysemu/watchdog.h"
43 #include "exec/gdbstub.h"
44 #include "exec/memattrs.h"
45 #include "exec/ram_addr.h"
46 #include "sysemu/hostmem.h"
47 #include "qemu/cutils.h"
48 #include "qemu/mmap-alloc.h"
50 #include "sysemu/kvm_int.h"
52 #define PROC_DEVTREE_CPU "/proc/device-tree/cpus/"
54 const KVMCapabilityInfo kvm_arch_required_capabilities
[] = {
58 static int cap_interrupt_unset
;
59 static int cap_interrupt_level
;
60 static int cap_segstate
;
61 static int cap_booke_sregs
;
62 static int cap_ppc_smt
;
63 static int cap_ppc_smt_possible
;
64 static int cap_spapr_tce
;
65 static int cap_spapr_tce_64
;
66 static int cap_spapr_multitce
;
67 static int cap_spapr_vfio
;
69 static int cap_one_reg
;
71 static int cap_ppc_watchdog
;
73 static int cap_htab_fd
;
74 static int cap_fixup_hcalls
;
75 static int cap_htm
; /* Hardware transactional memory support */
76 static int cap_mmu_radix
;
77 static int cap_mmu_hash_v3
;
78 static int cap_resize_hpt
;
79 static int cap_ppc_pvr_compat
;
80 static int cap_ppc_safe_cache
;
81 static int cap_ppc_safe_bounds_check
;
82 static int cap_ppc_safe_indirect_branch
;
83 static int cap_ppc_count_cache_flush_assist
;
84 static int cap_ppc_nested_kvm_hv
;
85 static int cap_large_decr
;
87 static uint32_t debug_inst_opcode
;
90 * XXX We have a race condition where we actually have a level triggered
91 * interrupt, but the infrastructure can't expose that yet, so the guest
92 * takes but ignores it, goes to sleep and never gets notified that there's
93 * still an interrupt pending.
95 * As a quick workaround, let's just wake up again 20 ms after we injected
96 * an interrupt. That way we can assure that we're always reinjecting
97 * interrupts in case the guest swallowed them.
99 static QEMUTimer
*idle_timer
;
101 static void kvm_kick_cpu(void *opaque
)
103 PowerPCCPU
*cpu
= opaque
;
105 qemu_cpu_kick(CPU(cpu
));
109 * Check whether we are running with KVM-PR (instead of KVM-HV). This
110 * should only be used for fallback tests - generally we should use
111 * explicit capabilities for the features we want, rather than
112 * assuming what is/isn't available depending on the KVM variant.
114 static bool kvmppc_is_pr(KVMState
*ks
)
116 /* Assume KVM-PR if the GET_PVINFO capability is available */
117 return kvm_vm_check_extension(ks
, KVM_CAP_PPC_GET_PVINFO
) != 0;
120 static int kvm_ppc_register_host_cpu_type(MachineState
*ms
);
121 static void kvmppc_get_cpu_characteristics(KVMState
*s
);
122 static int kvmppc_get_dec_bits(void);
124 int kvm_arch_init(MachineState
*ms
, KVMState
*s
)
126 cap_interrupt_unset
= kvm_check_extension(s
, KVM_CAP_PPC_UNSET_IRQ
);
127 cap_interrupt_level
= kvm_check_extension(s
, KVM_CAP_PPC_IRQ_LEVEL
);
128 cap_segstate
= kvm_check_extension(s
, KVM_CAP_PPC_SEGSTATE
);
129 cap_booke_sregs
= kvm_check_extension(s
, KVM_CAP_PPC_BOOKE_SREGS
);
130 cap_ppc_smt_possible
= kvm_vm_check_extension(s
, KVM_CAP_PPC_SMT_POSSIBLE
);
131 cap_spapr_tce
= kvm_check_extension(s
, KVM_CAP_SPAPR_TCE
);
132 cap_spapr_tce_64
= kvm_check_extension(s
, KVM_CAP_SPAPR_TCE_64
);
133 cap_spapr_multitce
= kvm_check_extension(s
, KVM_CAP_SPAPR_MULTITCE
);
134 cap_spapr_vfio
= kvm_vm_check_extension(s
, KVM_CAP_SPAPR_TCE_VFIO
);
135 cap_one_reg
= kvm_check_extension(s
, KVM_CAP_ONE_REG
);
136 cap_hior
= kvm_check_extension(s
, KVM_CAP_PPC_HIOR
);
137 cap_epr
= kvm_check_extension(s
, KVM_CAP_PPC_EPR
);
138 cap_ppc_watchdog
= kvm_check_extension(s
, KVM_CAP_PPC_BOOKE_WATCHDOG
);
140 * Note: we don't set cap_papr here, because this capability is
141 * only activated after this by kvmppc_set_papr()
143 cap_htab_fd
= kvm_vm_check_extension(s
, KVM_CAP_PPC_HTAB_FD
);
144 cap_fixup_hcalls
= kvm_check_extension(s
, KVM_CAP_PPC_FIXUP_HCALL
);
145 cap_ppc_smt
= kvm_vm_check_extension(s
, KVM_CAP_PPC_SMT
);
146 cap_htm
= kvm_vm_check_extension(s
, KVM_CAP_PPC_HTM
);
147 cap_mmu_radix
= kvm_vm_check_extension(s
, KVM_CAP_PPC_MMU_RADIX
);
148 cap_mmu_hash_v3
= kvm_vm_check_extension(s
, KVM_CAP_PPC_MMU_HASH_V3
);
149 cap_resize_hpt
= kvm_vm_check_extension(s
, KVM_CAP_SPAPR_RESIZE_HPT
);
150 kvmppc_get_cpu_characteristics(s
);
151 cap_ppc_nested_kvm_hv
= kvm_vm_check_extension(s
, KVM_CAP_PPC_NESTED_HV
);
152 cap_large_decr
= kvmppc_get_dec_bits();
154 * Note: setting it to false because there is not such capability
155 * in KVM at this moment.
157 * TODO: call kvm_vm_check_extension() with the right capability
158 * after the kernel starts implementing it.
160 cap_ppc_pvr_compat
= false;
162 if (!cap_interrupt_level
) {
163 fprintf(stderr
, "KVM: Couldn't find level irq capability. Expect the "
164 "VM to stall at times!\n");
167 kvm_ppc_register_host_cpu_type(ms
);
172 int kvm_arch_irqchip_create(MachineState
*ms
, KVMState
*s
)
177 static int kvm_arch_sync_sregs(PowerPCCPU
*cpu
)
179 CPUPPCState
*cenv
= &cpu
->env
;
180 CPUState
*cs
= CPU(cpu
);
181 struct kvm_sregs sregs
;
184 if (cenv
->excp_model
== POWERPC_EXCP_BOOKE
) {
186 * What we're really trying to say is "if we're on BookE, we
187 * use the native PVR for now". This is the only sane way to
188 * check it though, so we potentially confuse users that they
189 * can run BookE guests on BookS. Let's hope nobody dares
195 fprintf(stderr
, "kvm error: missing PVR setting capability\n");
200 ret
= kvm_vcpu_ioctl(cs
, KVM_GET_SREGS
, &sregs
);
205 sregs
.pvr
= cenv
->spr
[SPR_PVR
];
206 return kvm_vcpu_ioctl(cs
, KVM_SET_SREGS
, &sregs
);
209 /* Set up a shared TLB array with KVM */
210 static int kvm_booke206_tlb_init(PowerPCCPU
*cpu
)
212 CPUPPCState
*env
= &cpu
->env
;
213 CPUState
*cs
= CPU(cpu
);
214 struct kvm_book3e_206_tlb_params params
= {};
215 struct kvm_config_tlb cfg
= {};
216 unsigned int entries
= 0;
219 if (!kvm_enabled() ||
220 !kvm_check_extension(cs
->kvm_state
, KVM_CAP_SW_TLB
)) {
224 assert(ARRAY_SIZE(params
.tlb_sizes
) == BOOKE206_MAX_TLBN
);
226 for (i
= 0; i
< BOOKE206_MAX_TLBN
; i
++) {
227 params
.tlb_sizes
[i
] = booke206_tlb_size(env
, i
);
228 params
.tlb_ways
[i
] = booke206_tlb_ways(env
, i
);
229 entries
+= params
.tlb_sizes
[i
];
232 assert(entries
== env
->nb_tlb
);
233 assert(sizeof(struct kvm_book3e_206_tlb_entry
) == sizeof(ppcmas_tlb_t
));
235 env
->tlb_dirty
= true;
237 cfg
.array
= (uintptr_t)env
->tlb
.tlbm
;
238 cfg
.array_len
= sizeof(ppcmas_tlb_t
) * entries
;
239 cfg
.params
= (uintptr_t)¶ms
;
240 cfg
.mmu_type
= KVM_MMU_FSL_BOOKE_NOHV
;
242 ret
= kvm_vcpu_enable_cap(cs
, KVM_CAP_SW_TLB
, 0, (uintptr_t)&cfg
);
244 fprintf(stderr
, "%s: couldn't enable KVM_CAP_SW_TLB: %s\n",
245 __func__
, strerror(-ret
));
249 env
->kvm_sw_tlb
= true;
254 #if defined(TARGET_PPC64)
255 static void kvm_get_smmu_info(struct kvm_ppc_smmu_info
*info
, Error
**errp
)
259 assert(kvm_state
!= NULL
);
261 if (!kvm_check_extension(kvm_state
, KVM_CAP_PPC_GET_SMMU_INFO
)) {
262 error_setg(errp
, "KVM doesn't expose the MMU features it supports");
263 error_append_hint(errp
, "Consider switching to a newer KVM\n");
267 ret
= kvm_vm_ioctl(kvm_state
, KVM_PPC_GET_SMMU_INFO
, info
);
272 error_setg_errno(errp
, -ret
,
273 "KVM failed to provide the MMU features it supports");
276 struct ppc_radix_page_info
*kvm_get_radix_page_info(void)
278 KVMState
*s
= KVM_STATE(current_machine
->accelerator
);
279 struct ppc_radix_page_info
*radix_page_info
;
280 struct kvm_ppc_rmmu_info rmmu_info
;
283 if (!kvm_check_extension(s
, KVM_CAP_PPC_MMU_RADIX
)) {
286 if (kvm_vm_ioctl(s
, KVM_PPC_GET_RMMU_INFO
, &rmmu_info
)) {
289 radix_page_info
= g_malloc0(sizeof(*radix_page_info
));
290 radix_page_info
->count
= 0;
291 for (i
= 0; i
< PPC_PAGE_SIZES_MAX_SZ
; i
++) {
292 if (rmmu_info
.ap_encodings
[i
]) {
293 radix_page_info
->entries
[i
] = rmmu_info
.ap_encodings
[i
];
294 radix_page_info
->count
++;
297 return radix_page_info
;
300 target_ulong
kvmppc_configure_v3_mmu(PowerPCCPU
*cpu
,
301 bool radix
, bool gtse
,
304 CPUState
*cs
= CPU(cpu
);
307 struct kvm_ppc_mmuv3_cfg cfg
= {
308 .process_table
= proc_tbl
,
312 flags
|= KVM_PPC_MMUV3_RADIX
;
315 flags
|= KVM_PPC_MMUV3_GTSE
;
318 ret
= kvm_vm_ioctl(cs
->kvm_state
, KVM_PPC_CONFIGURE_V3_MMU
, &cfg
);
325 return H_NOT_AVAILABLE
;
331 bool kvmppc_hpt_needs_host_contiguous_pages(void)
333 static struct kvm_ppc_smmu_info smmu_info
;
335 if (!kvm_enabled()) {
339 kvm_get_smmu_info(&smmu_info
, &error_fatal
);
340 return !!(smmu_info
.flags
& KVM_PPC_PAGE_SIZES_REAL
);
343 void kvm_check_mmu(PowerPCCPU
*cpu
, Error
**errp
)
345 struct kvm_ppc_smmu_info smmu_info
;
347 Error
*local_err
= NULL
;
349 /* For now, we only have anything to check on hash64 MMUs */
350 if (!cpu
->hash64_opts
|| !kvm_enabled()) {
354 kvm_get_smmu_info(&smmu_info
, &local_err
);
356 error_propagate(errp
, local_err
);
360 if (ppc_hash64_has(cpu
, PPC_HASH64_1TSEG
)
361 && !(smmu_info
.flags
& KVM_PPC_1T_SEGMENTS
)) {
363 "KVM does not support 1TiB segments which guest expects");
367 if (smmu_info
.slb_size
< cpu
->hash64_opts
->slb_size
) {
368 error_setg(errp
, "KVM only supports %u SLB entries, but guest needs %u",
369 smmu_info
.slb_size
, cpu
->hash64_opts
->slb_size
);
374 * Verify that every pagesize supported by the cpu model is
375 * supported by KVM with the same encodings
377 for (iq
= 0; iq
< ARRAY_SIZE(cpu
->hash64_opts
->sps
); iq
++) {
378 PPCHash64SegmentPageSizes
*qsps
= &cpu
->hash64_opts
->sps
[iq
];
379 struct kvm_ppc_one_seg_page_size
*ksps
;
381 for (ik
= 0; ik
< ARRAY_SIZE(smmu_info
.sps
); ik
++) {
382 if (qsps
->page_shift
== smmu_info
.sps
[ik
].page_shift
) {
386 if (ik
>= ARRAY_SIZE(smmu_info
.sps
)) {
387 error_setg(errp
, "KVM doesn't support for base page shift %u",
392 ksps
= &smmu_info
.sps
[ik
];
393 if (ksps
->slb_enc
!= qsps
->slb_enc
) {
395 "KVM uses SLB encoding 0x%x for page shift %u, but guest expects 0x%x",
396 ksps
->slb_enc
, ksps
->page_shift
, qsps
->slb_enc
);
400 for (jq
= 0; jq
< ARRAY_SIZE(qsps
->enc
); jq
++) {
401 for (jk
= 0; jk
< ARRAY_SIZE(ksps
->enc
); jk
++) {
402 if (qsps
->enc
[jq
].page_shift
== ksps
->enc
[jk
].page_shift
) {
407 if (jk
>= ARRAY_SIZE(ksps
->enc
)) {
408 error_setg(errp
, "KVM doesn't support page shift %u/%u",
409 qsps
->enc
[jq
].page_shift
, qsps
->page_shift
);
412 if (qsps
->enc
[jq
].pte_enc
!= ksps
->enc
[jk
].pte_enc
) {
414 "KVM uses PTE encoding 0x%x for page shift %u/%u, but guest expects 0x%x",
415 ksps
->enc
[jk
].pte_enc
, qsps
->enc
[jq
].page_shift
,
416 qsps
->page_shift
, qsps
->enc
[jq
].pte_enc
);
422 if (ppc_hash64_has(cpu
, PPC_HASH64_CI_LARGEPAGE
)) {
424 * Mostly what guest pagesizes we can use are related to the
425 * host pages used to map guest RAM, which is handled in the
426 * platform code. Cache-Inhibited largepages (64k) however are
427 * used for I/O, so if they're mapped to the host at all it
428 * will be a normal mapping, not a special hugepage one used
431 if (getpagesize() < 0x10000) {
433 "KVM can't supply 64kiB CI pages, which guest expects");
437 #endif /* !defined (TARGET_PPC64) */
439 unsigned long kvm_arch_vcpu_id(CPUState
*cpu
)
441 return POWERPC_CPU(cpu
)->vcpu_id
;
445 * e500 supports 2 h/w breakpoint and 2 watchpoint. book3s supports
446 * only 1 watchpoint, so array size of 4 is sufficient for now.
448 #define MAX_HW_BKPTS 4
450 static struct HWBreakpoint
{
453 } hw_debug_points
[MAX_HW_BKPTS
];
455 static CPUWatchpoint hw_watchpoint
;
457 /* Default there is no breakpoint and watchpoint supported */
458 static int max_hw_breakpoint
;
459 static int max_hw_watchpoint
;
460 static int nb_hw_breakpoint
;
461 static int nb_hw_watchpoint
;
463 static void kvmppc_hw_debug_points_init(CPUPPCState
*cenv
)
465 if (cenv
->excp_model
== POWERPC_EXCP_BOOKE
) {
466 max_hw_breakpoint
= 2;
467 max_hw_watchpoint
= 2;
470 if ((max_hw_breakpoint
+ max_hw_watchpoint
) > MAX_HW_BKPTS
) {
471 fprintf(stderr
, "Error initializing h/w breakpoints\n");
476 int kvm_arch_init_vcpu(CPUState
*cs
)
478 PowerPCCPU
*cpu
= POWERPC_CPU(cs
);
479 CPUPPCState
*cenv
= &cpu
->env
;
482 /* Synchronize sregs with kvm */
483 ret
= kvm_arch_sync_sregs(cpu
);
485 if (ret
== -EINVAL
) {
486 error_report("Register sync failed... If you're using kvm-hv.ko,"
487 " only \"-cpu host\" is possible");
492 idle_timer
= timer_new_ns(QEMU_CLOCK_VIRTUAL
, kvm_kick_cpu
, cpu
);
494 switch (cenv
->mmu_model
) {
495 case POWERPC_MMU_BOOKE206
:
496 /* This target supports access to KVM's guest TLB */
497 ret
= kvm_booke206_tlb_init(cpu
);
499 case POWERPC_MMU_2_07
:
500 if (!cap_htm
&& !kvmppc_is_pr(cs
->kvm_state
)) {
502 * KVM-HV has transactional memory on POWER8 also without
503 * the KVM_CAP_PPC_HTM extension, so enable it here
504 * instead as long as it's availble to userspace on the
507 if (qemu_getauxval(AT_HWCAP2
) & PPC_FEATURE2_HAS_HTM
) {
516 kvm_get_one_reg(cs
, KVM_REG_PPC_DEBUG_INST
, &debug_inst_opcode
);
517 kvmppc_hw_debug_points_init(cenv
);
522 static void kvm_sw_tlb_put(PowerPCCPU
*cpu
)
524 CPUPPCState
*env
= &cpu
->env
;
525 CPUState
*cs
= CPU(cpu
);
526 struct kvm_dirty_tlb dirty_tlb
;
527 unsigned char *bitmap
;
530 if (!env
->kvm_sw_tlb
) {
534 bitmap
= g_malloc((env
->nb_tlb
+ 7) / 8);
535 memset(bitmap
, 0xFF, (env
->nb_tlb
+ 7) / 8);
537 dirty_tlb
.bitmap
= (uintptr_t)bitmap
;
538 dirty_tlb
.num_dirty
= env
->nb_tlb
;
540 ret
= kvm_vcpu_ioctl(cs
, KVM_DIRTY_TLB
, &dirty_tlb
);
542 fprintf(stderr
, "%s: KVM_DIRTY_TLB: %s\n",
543 __func__
, strerror(-ret
));
549 static void kvm_get_one_spr(CPUState
*cs
, uint64_t id
, int spr
)
551 PowerPCCPU
*cpu
= POWERPC_CPU(cs
);
552 CPUPPCState
*env
= &cpu
->env
;
557 struct kvm_one_reg reg
= {
559 .addr
= (uintptr_t) &val
,
563 ret
= kvm_vcpu_ioctl(cs
, KVM_GET_ONE_REG
, ®
);
565 trace_kvm_failed_spr_get(spr
, strerror(errno
));
567 switch (id
& KVM_REG_SIZE_MASK
) {
568 case KVM_REG_SIZE_U32
:
569 env
->spr
[spr
] = val
.u32
;
572 case KVM_REG_SIZE_U64
:
573 env
->spr
[spr
] = val
.u64
;
577 /* Don't handle this size yet */
583 static void kvm_put_one_spr(CPUState
*cs
, uint64_t id
, int spr
)
585 PowerPCCPU
*cpu
= POWERPC_CPU(cs
);
586 CPUPPCState
*env
= &cpu
->env
;
591 struct kvm_one_reg reg
= {
593 .addr
= (uintptr_t) &val
,
597 switch (id
& KVM_REG_SIZE_MASK
) {
598 case KVM_REG_SIZE_U32
:
599 val
.u32
= env
->spr
[spr
];
602 case KVM_REG_SIZE_U64
:
603 val
.u64
= env
->spr
[spr
];
607 /* Don't handle this size yet */
611 ret
= kvm_vcpu_ioctl(cs
, KVM_SET_ONE_REG
, ®
);
613 trace_kvm_failed_spr_set(spr
, strerror(errno
));
617 static int kvm_put_fp(CPUState
*cs
)
619 PowerPCCPU
*cpu
= POWERPC_CPU(cs
);
620 CPUPPCState
*env
= &cpu
->env
;
621 struct kvm_one_reg reg
;
625 if (env
->insns_flags
& PPC_FLOAT
) {
626 uint64_t fpscr
= env
->fpscr
;
627 bool vsx
= !!(env
->insns_flags2
& PPC2_VSX
);
629 reg
.id
= KVM_REG_PPC_FPSCR
;
630 reg
.addr
= (uintptr_t)&fpscr
;
631 ret
= kvm_vcpu_ioctl(cs
, KVM_SET_ONE_REG
, ®
);
633 trace_kvm_failed_fpscr_set(strerror(errno
));
637 for (i
= 0; i
< 32; i
++) {
639 uint64_t *fpr
= cpu_fpr_ptr(&cpu
->env
, i
);
640 uint64_t *vsrl
= cpu_vsrl_ptr(&cpu
->env
, i
);
642 #ifdef HOST_WORDS_BIGENDIAN
643 vsr
[0] = float64_val(*fpr
);
647 vsr
[1] = float64_val(*fpr
);
649 reg
.addr
= (uintptr_t) &vsr
;
650 reg
.id
= vsx
? KVM_REG_PPC_VSR(i
) : KVM_REG_PPC_FPR(i
);
652 ret
= kvm_vcpu_ioctl(cs
, KVM_SET_ONE_REG
, ®
);
654 trace_kvm_failed_fp_set(vsx
? "VSR" : "FPR", i
,
661 if (env
->insns_flags
& PPC_ALTIVEC
) {
662 reg
.id
= KVM_REG_PPC_VSCR
;
663 reg
.addr
= (uintptr_t)&env
->vscr
;
664 ret
= kvm_vcpu_ioctl(cs
, KVM_SET_ONE_REG
, ®
);
666 trace_kvm_failed_vscr_set(strerror(errno
));
670 for (i
= 0; i
< 32; i
++) {
671 reg
.id
= KVM_REG_PPC_VR(i
);
672 reg
.addr
= (uintptr_t)cpu_avr_ptr(env
, i
);
673 ret
= kvm_vcpu_ioctl(cs
, KVM_SET_ONE_REG
, ®
);
675 trace_kvm_failed_vr_set(i
, strerror(errno
));
684 static int kvm_get_fp(CPUState
*cs
)
686 PowerPCCPU
*cpu
= POWERPC_CPU(cs
);
687 CPUPPCState
*env
= &cpu
->env
;
688 struct kvm_one_reg reg
;
692 if (env
->insns_flags
& PPC_FLOAT
) {
694 bool vsx
= !!(env
->insns_flags2
& PPC2_VSX
);
696 reg
.id
= KVM_REG_PPC_FPSCR
;
697 reg
.addr
= (uintptr_t)&fpscr
;
698 ret
= kvm_vcpu_ioctl(cs
, KVM_GET_ONE_REG
, ®
);
700 trace_kvm_failed_fpscr_get(strerror(errno
));
706 for (i
= 0; i
< 32; i
++) {
708 uint64_t *fpr
= cpu_fpr_ptr(&cpu
->env
, i
);
709 uint64_t *vsrl
= cpu_vsrl_ptr(&cpu
->env
, i
);
711 reg
.addr
= (uintptr_t) &vsr
;
712 reg
.id
= vsx
? KVM_REG_PPC_VSR(i
) : KVM_REG_PPC_FPR(i
);
714 ret
= kvm_vcpu_ioctl(cs
, KVM_GET_ONE_REG
, ®
);
716 trace_kvm_failed_fp_get(vsx
? "VSR" : "FPR", i
,
720 #ifdef HOST_WORDS_BIGENDIAN
735 if (env
->insns_flags
& PPC_ALTIVEC
) {
736 reg
.id
= KVM_REG_PPC_VSCR
;
737 reg
.addr
= (uintptr_t)&env
->vscr
;
738 ret
= kvm_vcpu_ioctl(cs
, KVM_GET_ONE_REG
, ®
);
740 trace_kvm_failed_vscr_get(strerror(errno
));
744 for (i
= 0; i
< 32; i
++) {
745 reg
.id
= KVM_REG_PPC_VR(i
);
746 reg
.addr
= (uintptr_t)cpu_avr_ptr(env
, i
);
747 ret
= kvm_vcpu_ioctl(cs
, KVM_GET_ONE_REG
, ®
);
749 trace_kvm_failed_vr_get(i
, strerror(errno
));
758 #if defined(TARGET_PPC64)
759 static int kvm_get_vpa(CPUState
*cs
)
761 PowerPCCPU
*cpu
= POWERPC_CPU(cs
);
762 SpaprCpuState
*spapr_cpu
= spapr_cpu_state(cpu
);
763 struct kvm_one_reg reg
;
766 reg
.id
= KVM_REG_PPC_VPA_ADDR
;
767 reg
.addr
= (uintptr_t)&spapr_cpu
->vpa_addr
;
768 ret
= kvm_vcpu_ioctl(cs
, KVM_GET_ONE_REG
, ®
);
770 trace_kvm_failed_vpa_addr_get(strerror(errno
));
774 assert((uintptr_t)&spapr_cpu
->slb_shadow_size
775 == ((uintptr_t)&spapr_cpu
->slb_shadow_addr
+ 8));
776 reg
.id
= KVM_REG_PPC_VPA_SLB
;
777 reg
.addr
= (uintptr_t)&spapr_cpu
->slb_shadow_addr
;
778 ret
= kvm_vcpu_ioctl(cs
, KVM_GET_ONE_REG
, ®
);
780 trace_kvm_failed_slb_get(strerror(errno
));
784 assert((uintptr_t)&spapr_cpu
->dtl_size
785 == ((uintptr_t)&spapr_cpu
->dtl_addr
+ 8));
786 reg
.id
= KVM_REG_PPC_VPA_DTL
;
787 reg
.addr
= (uintptr_t)&spapr_cpu
->dtl_addr
;
788 ret
= kvm_vcpu_ioctl(cs
, KVM_GET_ONE_REG
, ®
);
790 trace_kvm_failed_dtl_get(strerror(errno
));
797 static int kvm_put_vpa(CPUState
*cs
)
799 PowerPCCPU
*cpu
= POWERPC_CPU(cs
);
800 SpaprCpuState
*spapr_cpu
= spapr_cpu_state(cpu
);
801 struct kvm_one_reg reg
;
805 * SLB shadow or DTL can't be registered unless a master VPA is
806 * registered. That means when restoring state, if a VPA *is*
807 * registered, we need to set that up first. If not, we need to
808 * deregister the others before deregistering the master VPA
810 assert(spapr_cpu
->vpa_addr
811 || !(spapr_cpu
->slb_shadow_addr
|| spapr_cpu
->dtl_addr
));
813 if (spapr_cpu
->vpa_addr
) {
814 reg
.id
= KVM_REG_PPC_VPA_ADDR
;
815 reg
.addr
= (uintptr_t)&spapr_cpu
->vpa_addr
;
816 ret
= kvm_vcpu_ioctl(cs
, KVM_SET_ONE_REG
, ®
);
818 trace_kvm_failed_vpa_addr_set(strerror(errno
));
823 assert((uintptr_t)&spapr_cpu
->slb_shadow_size
824 == ((uintptr_t)&spapr_cpu
->slb_shadow_addr
+ 8));
825 reg
.id
= KVM_REG_PPC_VPA_SLB
;
826 reg
.addr
= (uintptr_t)&spapr_cpu
->slb_shadow_addr
;
827 ret
= kvm_vcpu_ioctl(cs
, KVM_SET_ONE_REG
, ®
);
829 trace_kvm_failed_slb_set(strerror(errno
));
833 assert((uintptr_t)&spapr_cpu
->dtl_size
834 == ((uintptr_t)&spapr_cpu
->dtl_addr
+ 8));
835 reg
.id
= KVM_REG_PPC_VPA_DTL
;
836 reg
.addr
= (uintptr_t)&spapr_cpu
->dtl_addr
;
837 ret
= kvm_vcpu_ioctl(cs
, KVM_SET_ONE_REG
, ®
);
839 trace_kvm_failed_dtl_set(strerror(errno
));
843 if (!spapr_cpu
->vpa_addr
) {
844 reg
.id
= KVM_REG_PPC_VPA_ADDR
;
845 reg
.addr
= (uintptr_t)&spapr_cpu
->vpa_addr
;
846 ret
= kvm_vcpu_ioctl(cs
, KVM_SET_ONE_REG
, ®
);
848 trace_kvm_failed_null_vpa_addr_set(strerror(errno
));
855 #endif /* TARGET_PPC64 */
857 int kvmppc_put_books_sregs(PowerPCCPU
*cpu
)
859 CPUPPCState
*env
= &cpu
->env
;
860 struct kvm_sregs sregs
;
863 sregs
.pvr
= env
->spr
[SPR_PVR
];
866 PPCVirtualHypervisorClass
*vhc
=
867 PPC_VIRTUAL_HYPERVISOR_GET_CLASS(cpu
->vhyp
);
868 sregs
.u
.s
.sdr1
= vhc
->encode_hpt_for_kvm_pr(cpu
->vhyp
);
870 sregs
.u
.s
.sdr1
= env
->spr
[SPR_SDR1
];
875 for (i
= 0; i
< ARRAY_SIZE(env
->slb
); i
++) {
876 sregs
.u
.s
.ppc64
.slb
[i
].slbe
= env
->slb
[i
].esid
;
877 if (env
->slb
[i
].esid
& SLB_ESID_V
) {
878 sregs
.u
.s
.ppc64
.slb
[i
].slbe
|= i
;
880 sregs
.u
.s
.ppc64
.slb
[i
].slbv
= env
->slb
[i
].vsid
;
885 for (i
= 0; i
< 16; i
++) {
886 sregs
.u
.s
.ppc32
.sr
[i
] = env
->sr
[i
];
890 for (i
= 0; i
< 8; i
++) {
891 /* Beware. We have to swap upper and lower bits here */
892 sregs
.u
.s
.ppc32
.dbat
[i
] = ((uint64_t)env
->DBAT
[0][i
] << 32)
894 sregs
.u
.s
.ppc32
.ibat
[i
] = ((uint64_t)env
->IBAT
[0][i
] << 32)
898 return kvm_vcpu_ioctl(CPU(cpu
), KVM_SET_SREGS
, &sregs
);
901 int kvm_arch_put_registers(CPUState
*cs
, int level
)
903 PowerPCCPU
*cpu
= POWERPC_CPU(cs
);
904 CPUPPCState
*env
= &cpu
->env
;
905 struct kvm_regs regs
;
909 ret
= kvm_vcpu_ioctl(cs
, KVM_GET_REGS
, ®s
);
916 regs
.xer
= cpu_read_xer(env
);
920 regs
.srr0
= env
->spr
[SPR_SRR0
];
921 regs
.srr1
= env
->spr
[SPR_SRR1
];
923 regs
.sprg0
= env
->spr
[SPR_SPRG0
];
924 regs
.sprg1
= env
->spr
[SPR_SPRG1
];
925 regs
.sprg2
= env
->spr
[SPR_SPRG2
];
926 regs
.sprg3
= env
->spr
[SPR_SPRG3
];
927 regs
.sprg4
= env
->spr
[SPR_SPRG4
];
928 regs
.sprg5
= env
->spr
[SPR_SPRG5
];
929 regs
.sprg6
= env
->spr
[SPR_SPRG6
];
930 regs
.sprg7
= env
->spr
[SPR_SPRG7
];
932 regs
.pid
= env
->spr
[SPR_BOOKE_PID
];
934 for (i
= 0; i
< 32; i
++) {
935 regs
.gpr
[i
] = env
->gpr
[i
];
939 for (i
= 0; i
< 8; i
++) {
940 regs
.cr
|= (env
->crf
[i
] & 15) << (4 * (7 - i
));
943 ret
= kvm_vcpu_ioctl(cs
, KVM_SET_REGS
, ®s
);
950 if (env
->tlb_dirty
) {
952 env
->tlb_dirty
= false;
955 if (cap_segstate
&& (level
>= KVM_PUT_RESET_STATE
)) {
956 ret
= kvmppc_put_books_sregs(cpu
);
962 if (cap_hior
&& (level
>= KVM_PUT_RESET_STATE
)) {
963 kvm_put_one_spr(cs
, KVM_REG_PPC_HIOR
, SPR_HIOR
);
970 * We deliberately ignore errors here, for kernels which have
971 * the ONE_REG calls, but don't support the specific
972 * registers, there's a reasonable chance things will still
973 * work, at least until we try to migrate.
975 for (i
= 0; i
< 1024; i
++) {
976 uint64_t id
= env
->spr_cb
[i
].one_reg_id
;
979 kvm_put_one_spr(cs
, id
, i
);
985 for (i
= 0; i
< ARRAY_SIZE(env
->tm_gpr
); i
++) {
986 kvm_set_one_reg(cs
, KVM_REG_PPC_TM_GPR(i
), &env
->tm_gpr
[i
]);
988 for (i
= 0; i
< ARRAY_SIZE(env
->tm_vsr
); i
++) {
989 kvm_set_one_reg(cs
, KVM_REG_PPC_TM_VSR(i
), &env
->tm_vsr
[i
]);
991 kvm_set_one_reg(cs
, KVM_REG_PPC_TM_CR
, &env
->tm_cr
);
992 kvm_set_one_reg(cs
, KVM_REG_PPC_TM_LR
, &env
->tm_lr
);
993 kvm_set_one_reg(cs
, KVM_REG_PPC_TM_CTR
, &env
->tm_ctr
);
994 kvm_set_one_reg(cs
, KVM_REG_PPC_TM_FPSCR
, &env
->tm_fpscr
);
995 kvm_set_one_reg(cs
, KVM_REG_PPC_TM_AMR
, &env
->tm_amr
);
996 kvm_set_one_reg(cs
, KVM_REG_PPC_TM_PPR
, &env
->tm_ppr
);
997 kvm_set_one_reg(cs
, KVM_REG_PPC_TM_VRSAVE
, &env
->tm_vrsave
);
998 kvm_set_one_reg(cs
, KVM_REG_PPC_TM_VSCR
, &env
->tm_vscr
);
999 kvm_set_one_reg(cs
, KVM_REG_PPC_TM_DSCR
, &env
->tm_dscr
);
1000 kvm_set_one_reg(cs
, KVM_REG_PPC_TM_TAR
, &env
->tm_tar
);
1004 if (kvm_put_vpa(cs
) < 0) {
1005 trace_kvm_failed_put_vpa();
1009 kvm_set_one_reg(cs
, KVM_REG_PPC_TB_OFFSET
, &env
->tb_env
->tb_offset
);
1010 #endif /* TARGET_PPC64 */
1016 static void kvm_sync_excp(CPUPPCState
*env
, int vector
, int ivor
)
1018 env
->excp_vectors
[vector
] = env
->spr
[ivor
] + env
->spr
[SPR_BOOKE_IVPR
];
1021 static int kvmppc_get_booke_sregs(PowerPCCPU
*cpu
)
1023 CPUPPCState
*env
= &cpu
->env
;
1024 struct kvm_sregs sregs
;
1027 ret
= kvm_vcpu_ioctl(CPU(cpu
), KVM_GET_SREGS
, &sregs
);
1032 if (sregs
.u
.e
.features
& KVM_SREGS_E_BASE
) {
1033 env
->spr
[SPR_BOOKE_CSRR0
] = sregs
.u
.e
.csrr0
;
1034 env
->spr
[SPR_BOOKE_CSRR1
] = sregs
.u
.e
.csrr1
;
1035 env
->spr
[SPR_BOOKE_ESR
] = sregs
.u
.e
.esr
;
1036 env
->spr
[SPR_BOOKE_DEAR
] = sregs
.u
.e
.dear
;
1037 env
->spr
[SPR_BOOKE_MCSR
] = sregs
.u
.e
.mcsr
;
1038 env
->spr
[SPR_BOOKE_TSR
] = sregs
.u
.e
.tsr
;
1039 env
->spr
[SPR_BOOKE_TCR
] = sregs
.u
.e
.tcr
;
1040 env
->spr
[SPR_DECR
] = sregs
.u
.e
.dec
;
1041 env
->spr
[SPR_TBL
] = sregs
.u
.e
.tb
& 0xffffffff;
1042 env
->spr
[SPR_TBU
] = sregs
.u
.e
.tb
>> 32;
1043 env
->spr
[SPR_VRSAVE
] = sregs
.u
.e
.vrsave
;
1046 if (sregs
.u
.e
.features
& KVM_SREGS_E_ARCH206
) {
1047 env
->spr
[SPR_BOOKE_PIR
] = sregs
.u
.e
.pir
;
1048 env
->spr
[SPR_BOOKE_MCSRR0
] = sregs
.u
.e
.mcsrr0
;
1049 env
->spr
[SPR_BOOKE_MCSRR1
] = sregs
.u
.e
.mcsrr1
;
1050 env
->spr
[SPR_BOOKE_DECAR
] = sregs
.u
.e
.decar
;
1051 env
->spr
[SPR_BOOKE_IVPR
] = sregs
.u
.e
.ivpr
;
1054 if (sregs
.u
.e
.features
& KVM_SREGS_E_64
) {
1055 env
->spr
[SPR_BOOKE_EPCR
] = sregs
.u
.e
.epcr
;
1058 if (sregs
.u
.e
.features
& KVM_SREGS_E_SPRG8
) {
1059 env
->spr
[SPR_BOOKE_SPRG8
] = sregs
.u
.e
.sprg8
;
1062 if (sregs
.u
.e
.features
& KVM_SREGS_E_IVOR
) {
1063 env
->spr
[SPR_BOOKE_IVOR0
] = sregs
.u
.e
.ivor_low
[0];
1064 kvm_sync_excp(env
, POWERPC_EXCP_CRITICAL
, SPR_BOOKE_IVOR0
);
1065 env
->spr
[SPR_BOOKE_IVOR1
] = sregs
.u
.e
.ivor_low
[1];
1066 kvm_sync_excp(env
, POWERPC_EXCP_MCHECK
, SPR_BOOKE_IVOR1
);
1067 env
->spr
[SPR_BOOKE_IVOR2
] = sregs
.u
.e
.ivor_low
[2];
1068 kvm_sync_excp(env
, POWERPC_EXCP_DSI
, SPR_BOOKE_IVOR2
);
1069 env
->spr
[SPR_BOOKE_IVOR3
] = sregs
.u
.e
.ivor_low
[3];
1070 kvm_sync_excp(env
, POWERPC_EXCP_ISI
, SPR_BOOKE_IVOR3
);
1071 env
->spr
[SPR_BOOKE_IVOR4
] = sregs
.u
.e
.ivor_low
[4];
1072 kvm_sync_excp(env
, POWERPC_EXCP_EXTERNAL
, SPR_BOOKE_IVOR4
);
1073 env
->spr
[SPR_BOOKE_IVOR5
] = sregs
.u
.e
.ivor_low
[5];
1074 kvm_sync_excp(env
, POWERPC_EXCP_ALIGN
, SPR_BOOKE_IVOR5
);
1075 env
->spr
[SPR_BOOKE_IVOR6
] = sregs
.u
.e
.ivor_low
[6];
1076 kvm_sync_excp(env
, POWERPC_EXCP_PROGRAM
, SPR_BOOKE_IVOR6
);
1077 env
->spr
[SPR_BOOKE_IVOR7
] = sregs
.u
.e
.ivor_low
[7];
1078 kvm_sync_excp(env
, POWERPC_EXCP_FPU
, SPR_BOOKE_IVOR7
);
1079 env
->spr
[SPR_BOOKE_IVOR8
] = sregs
.u
.e
.ivor_low
[8];
1080 kvm_sync_excp(env
, POWERPC_EXCP_SYSCALL
, SPR_BOOKE_IVOR8
);
1081 env
->spr
[SPR_BOOKE_IVOR9
] = sregs
.u
.e
.ivor_low
[9];
1082 kvm_sync_excp(env
, POWERPC_EXCP_APU
, SPR_BOOKE_IVOR9
);
1083 env
->spr
[SPR_BOOKE_IVOR10
] = sregs
.u
.e
.ivor_low
[10];
1084 kvm_sync_excp(env
, POWERPC_EXCP_DECR
, SPR_BOOKE_IVOR10
);
1085 env
->spr
[SPR_BOOKE_IVOR11
] = sregs
.u
.e
.ivor_low
[11];
1086 kvm_sync_excp(env
, POWERPC_EXCP_FIT
, SPR_BOOKE_IVOR11
);
1087 env
->spr
[SPR_BOOKE_IVOR12
] = sregs
.u
.e
.ivor_low
[12];
1088 kvm_sync_excp(env
, POWERPC_EXCP_WDT
, SPR_BOOKE_IVOR12
);
1089 env
->spr
[SPR_BOOKE_IVOR13
] = sregs
.u
.e
.ivor_low
[13];
1090 kvm_sync_excp(env
, POWERPC_EXCP_DTLB
, SPR_BOOKE_IVOR13
);
1091 env
->spr
[SPR_BOOKE_IVOR14
] = sregs
.u
.e
.ivor_low
[14];
1092 kvm_sync_excp(env
, POWERPC_EXCP_ITLB
, SPR_BOOKE_IVOR14
);
1093 env
->spr
[SPR_BOOKE_IVOR15
] = sregs
.u
.e
.ivor_low
[15];
1094 kvm_sync_excp(env
, POWERPC_EXCP_DEBUG
, SPR_BOOKE_IVOR15
);
1096 if (sregs
.u
.e
.features
& KVM_SREGS_E_SPE
) {
1097 env
->spr
[SPR_BOOKE_IVOR32
] = sregs
.u
.e
.ivor_high
[0];
1098 kvm_sync_excp(env
, POWERPC_EXCP_SPEU
, SPR_BOOKE_IVOR32
);
1099 env
->spr
[SPR_BOOKE_IVOR33
] = sregs
.u
.e
.ivor_high
[1];
1100 kvm_sync_excp(env
, POWERPC_EXCP_EFPDI
, SPR_BOOKE_IVOR33
);
1101 env
->spr
[SPR_BOOKE_IVOR34
] = sregs
.u
.e
.ivor_high
[2];
1102 kvm_sync_excp(env
, POWERPC_EXCP_EFPRI
, SPR_BOOKE_IVOR34
);
1105 if (sregs
.u
.e
.features
& KVM_SREGS_E_PM
) {
1106 env
->spr
[SPR_BOOKE_IVOR35
] = sregs
.u
.e
.ivor_high
[3];
1107 kvm_sync_excp(env
, POWERPC_EXCP_EPERFM
, SPR_BOOKE_IVOR35
);
1110 if (sregs
.u
.e
.features
& KVM_SREGS_E_PC
) {
1111 env
->spr
[SPR_BOOKE_IVOR36
] = sregs
.u
.e
.ivor_high
[4];
1112 kvm_sync_excp(env
, POWERPC_EXCP_DOORI
, SPR_BOOKE_IVOR36
);
1113 env
->spr
[SPR_BOOKE_IVOR37
] = sregs
.u
.e
.ivor_high
[5];
1114 kvm_sync_excp(env
, POWERPC_EXCP_DOORCI
, SPR_BOOKE_IVOR37
);
1118 if (sregs
.u
.e
.features
& KVM_SREGS_E_ARCH206_MMU
) {
1119 env
->spr
[SPR_BOOKE_MAS0
] = sregs
.u
.e
.mas0
;
1120 env
->spr
[SPR_BOOKE_MAS1
] = sregs
.u
.e
.mas1
;
1121 env
->spr
[SPR_BOOKE_MAS2
] = sregs
.u
.e
.mas2
;
1122 env
->spr
[SPR_BOOKE_MAS3
] = sregs
.u
.e
.mas7_3
& 0xffffffff;
1123 env
->spr
[SPR_BOOKE_MAS4
] = sregs
.u
.e
.mas4
;
1124 env
->spr
[SPR_BOOKE_MAS6
] = sregs
.u
.e
.mas6
;
1125 env
->spr
[SPR_BOOKE_MAS7
] = sregs
.u
.e
.mas7_3
>> 32;
1126 env
->spr
[SPR_MMUCFG
] = sregs
.u
.e
.mmucfg
;
1127 env
->spr
[SPR_BOOKE_TLB0CFG
] = sregs
.u
.e
.tlbcfg
[0];
1128 env
->spr
[SPR_BOOKE_TLB1CFG
] = sregs
.u
.e
.tlbcfg
[1];
1131 if (sregs
.u
.e
.features
& KVM_SREGS_EXP
) {
1132 env
->spr
[SPR_BOOKE_EPR
] = sregs
.u
.e
.epr
;
1135 if (sregs
.u
.e
.features
& KVM_SREGS_E_PD
) {
1136 env
->spr
[SPR_BOOKE_EPLC
] = sregs
.u
.e
.eplc
;
1137 env
->spr
[SPR_BOOKE_EPSC
] = sregs
.u
.e
.epsc
;
1140 if (sregs
.u
.e
.impl_id
== KVM_SREGS_E_IMPL_FSL
) {
1141 env
->spr
[SPR_E500_SVR
] = sregs
.u
.e
.impl
.fsl
.svr
;
1142 env
->spr
[SPR_Exxx_MCAR
] = sregs
.u
.e
.impl
.fsl
.mcar
;
1143 env
->spr
[SPR_HID0
] = sregs
.u
.e
.impl
.fsl
.hid0
;
1145 if (sregs
.u
.e
.impl
.fsl
.features
& KVM_SREGS_E_FSL_PIDn
) {
1146 env
->spr
[SPR_BOOKE_PID1
] = sregs
.u
.e
.impl
.fsl
.pid1
;
1147 env
->spr
[SPR_BOOKE_PID2
] = sregs
.u
.e
.impl
.fsl
.pid2
;
1154 static int kvmppc_get_books_sregs(PowerPCCPU
*cpu
)
1156 CPUPPCState
*env
= &cpu
->env
;
1157 struct kvm_sregs sregs
;
1161 ret
= kvm_vcpu_ioctl(CPU(cpu
), KVM_GET_SREGS
, &sregs
);
1167 ppc_store_sdr1(env
, sregs
.u
.s
.sdr1
);
1173 * The packed SLB array we get from KVM_GET_SREGS only contains
1174 * information about valid entries. So we flush our internal copy
1175 * to get rid of stale ones, then put all valid SLB entries back
1178 memset(env
->slb
, 0, sizeof(env
->slb
));
1179 for (i
= 0; i
< ARRAY_SIZE(env
->slb
); i
++) {
1180 target_ulong rb
= sregs
.u
.s
.ppc64
.slb
[i
].slbe
;
1181 target_ulong rs
= sregs
.u
.s
.ppc64
.slb
[i
].slbv
;
1183 * Only restore valid entries
1185 if (rb
& SLB_ESID_V
) {
1186 ppc_store_slb(cpu
, rb
& 0xfff, rb
& ~0xfffULL
, rs
);
1192 for (i
= 0; i
< 16; i
++) {
1193 env
->sr
[i
] = sregs
.u
.s
.ppc32
.sr
[i
];
1197 for (i
= 0; i
< 8; i
++) {
1198 env
->DBAT
[0][i
] = sregs
.u
.s
.ppc32
.dbat
[i
] & 0xffffffff;
1199 env
->DBAT
[1][i
] = sregs
.u
.s
.ppc32
.dbat
[i
] >> 32;
1200 env
->IBAT
[0][i
] = sregs
.u
.s
.ppc32
.ibat
[i
] & 0xffffffff;
1201 env
->IBAT
[1][i
] = sregs
.u
.s
.ppc32
.ibat
[i
] >> 32;
1207 int kvm_arch_get_registers(CPUState
*cs
)
1209 PowerPCCPU
*cpu
= POWERPC_CPU(cs
);
1210 CPUPPCState
*env
= &cpu
->env
;
1211 struct kvm_regs regs
;
1215 ret
= kvm_vcpu_ioctl(cs
, KVM_GET_REGS
, ®s
);
1221 for (i
= 7; i
>= 0; i
--) {
1222 env
->crf
[i
] = cr
& 15;
1226 env
->ctr
= regs
.ctr
;
1228 cpu_write_xer(env
, regs
.xer
);
1229 env
->msr
= regs
.msr
;
1232 env
->spr
[SPR_SRR0
] = regs
.srr0
;
1233 env
->spr
[SPR_SRR1
] = regs
.srr1
;
1235 env
->spr
[SPR_SPRG0
] = regs
.sprg0
;
1236 env
->spr
[SPR_SPRG1
] = regs
.sprg1
;
1237 env
->spr
[SPR_SPRG2
] = regs
.sprg2
;
1238 env
->spr
[SPR_SPRG3
] = regs
.sprg3
;
1239 env
->spr
[SPR_SPRG4
] = regs
.sprg4
;
1240 env
->spr
[SPR_SPRG5
] = regs
.sprg5
;
1241 env
->spr
[SPR_SPRG6
] = regs
.sprg6
;
1242 env
->spr
[SPR_SPRG7
] = regs
.sprg7
;
1244 env
->spr
[SPR_BOOKE_PID
] = regs
.pid
;
1246 for (i
= 0; i
< 32; i
++) {
1247 env
->gpr
[i
] = regs
.gpr
[i
];
1252 if (cap_booke_sregs
) {
1253 ret
= kvmppc_get_booke_sregs(cpu
);
1260 ret
= kvmppc_get_books_sregs(cpu
);
1267 kvm_get_one_spr(cs
, KVM_REG_PPC_HIOR
, SPR_HIOR
);
1274 * We deliberately ignore errors here, for kernels which have
1275 * the ONE_REG calls, but don't support the specific
1276 * registers, there's a reasonable chance things will still
1277 * work, at least until we try to migrate.
1279 for (i
= 0; i
< 1024; i
++) {
1280 uint64_t id
= env
->spr_cb
[i
].one_reg_id
;
1283 kvm_get_one_spr(cs
, id
, i
);
1289 for (i
= 0; i
< ARRAY_SIZE(env
->tm_gpr
); i
++) {
1290 kvm_get_one_reg(cs
, KVM_REG_PPC_TM_GPR(i
), &env
->tm_gpr
[i
]);
1292 for (i
= 0; i
< ARRAY_SIZE(env
->tm_vsr
); i
++) {
1293 kvm_get_one_reg(cs
, KVM_REG_PPC_TM_VSR(i
), &env
->tm_vsr
[i
]);
1295 kvm_get_one_reg(cs
, KVM_REG_PPC_TM_CR
, &env
->tm_cr
);
1296 kvm_get_one_reg(cs
, KVM_REG_PPC_TM_LR
, &env
->tm_lr
);
1297 kvm_get_one_reg(cs
, KVM_REG_PPC_TM_CTR
, &env
->tm_ctr
);
1298 kvm_get_one_reg(cs
, KVM_REG_PPC_TM_FPSCR
, &env
->tm_fpscr
);
1299 kvm_get_one_reg(cs
, KVM_REG_PPC_TM_AMR
, &env
->tm_amr
);
1300 kvm_get_one_reg(cs
, KVM_REG_PPC_TM_PPR
, &env
->tm_ppr
);
1301 kvm_get_one_reg(cs
, KVM_REG_PPC_TM_VRSAVE
, &env
->tm_vrsave
);
1302 kvm_get_one_reg(cs
, KVM_REG_PPC_TM_VSCR
, &env
->tm_vscr
);
1303 kvm_get_one_reg(cs
, KVM_REG_PPC_TM_DSCR
, &env
->tm_dscr
);
1304 kvm_get_one_reg(cs
, KVM_REG_PPC_TM_TAR
, &env
->tm_tar
);
1308 if (kvm_get_vpa(cs
) < 0) {
1309 trace_kvm_failed_get_vpa();
1313 kvm_get_one_reg(cs
, KVM_REG_PPC_TB_OFFSET
, &env
->tb_env
->tb_offset
);
1320 int kvmppc_set_interrupt(PowerPCCPU
*cpu
, int irq
, int level
)
1322 unsigned virq
= level
? KVM_INTERRUPT_SET_LEVEL
: KVM_INTERRUPT_UNSET
;
1324 if (irq
!= PPC_INTERRUPT_EXT
) {
1328 if (!kvm_enabled() || !cap_interrupt_unset
|| !cap_interrupt_level
) {
1332 kvm_vcpu_ioctl(CPU(cpu
), KVM_INTERRUPT
, &virq
);
1337 #if defined(TARGET_PPC64)
1338 #define PPC_INPUT_INT PPC970_INPUT_INT
1340 #define PPC_INPUT_INT PPC6xx_INPUT_INT
1343 void kvm_arch_pre_run(CPUState
*cs
, struct kvm_run
*run
)
1345 PowerPCCPU
*cpu
= POWERPC_CPU(cs
);
1346 CPUPPCState
*env
= &cpu
->env
;
1350 qemu_mutex_lock_iothread();
1353 * PowerPC QEMU tracks the various core input pins (interrupt,
1354 * critical interrupt, reset, etc) in PPC-specific
1355 * env->irq_input_state.
1357 if (!cap_interrupt_level
&&
1358 run
->ready_for_interrupt_injection
&&
1359 (cs
->interrupt_request
& CPU_INTERRUPT_HARD
) &&
1360 (env
->irq_input_state
& (1 << PPC_INPUT_INT
)))
1363 * For now KVM disregards the 'irq' argument. However, in the
1364 * future KVM could cache it in-kernel to avoid a heavyweight
1365 * exit when reading the UIC.
1367 irq
= KVM_INTERRUPT_SET
;
1369 trace_kvm_injected_interrupt(irq
);
1370 r
= kvm_vcpu_ioctl(cs
, KVM_INTERRUPT
, &irq
);
1372 printf("cpu %d fail inject %x\n", cs
->cpu_index
, irq
);
1375 /* Always wake up soon in case the interrupt was level based */
1376 timer_mod(idle_timer
, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
) +
1377 (NANOSECONDS_PER_SECOND
/ 50));
1381 * We don't know if there are more interrupts pending after
1382 * this. However, the guest will return to userspace in the course
1383 * of handling this one anyways, so we will get a chance to
1387 qemu_mutex_unlock_iothread();
1390 MemTxAttrs
kvm_arch_post_run(CPUState
*cs
, struct kvm_run
*run
)
1392 return MEMTXATTRS_UNSPECIFIED
;
1395 int kvm_arch_process_async_events(CPUState
*cs
)
1400 static int kvmppc_handle_halt(PowerPCCPU
*cpu
)
1402 CPUState
*cs
= CPU(cpu
);
1403 CPUPPCState
*env
= &cpu
->env
;
1405 if (!(cs
->interrupt_request
& CPU_INTERRUPT_HARD
) && (msr_ee
)) {
1407 cs
->exception_index
= EXCP_HLT
;
1413 /* map dcr access to existing qemu dcr emulation */
1414 static int kvmppc_handle_dcr_read(CPUPPCState
*env
,
1415 uint32_t dcrn
, uint32_t *data
)
1417 if (ppc_dcr_read(env
->dcr_env
, dcrn
, data
) < 0) {
1418 fprintf(stderr
, "Read to unhandled DCR (0x%x)\n", dcrn
);
1424 static int kvmppc_handle_dcr_write(CPUPPCState
*env
,
1425 uint32_t dcrn
, uint32_t data
)
1427 if (ppc_dcr_write(env
->dcr_env
, dcrn
, data
) < 0) {
1428 fprintf(stderr
, "Write to unhandled DCR (0x%x)\n", dcrn
);
1434 int kvm_arch_insert_sw_breakpoint(CPUState
*cs
, struct kvm_sw_breakpoint
*bp
)
1436 /* Mixed endian case is not handled */
1437 uint32_t sc
= debug_inst_opcode
;
1439 if (cpu_memory_rw_debug(cs
, bp
->pc
, (uint8_t *)&bp
->saved_insn
,
1441 cpu_memory_rw_debug(cs
, bp
->pc
, (uint8_t *)&sc
, sizeof(sc
), 1)) {
1448 int kvm_arch_remove_sw_breakpoint(CPUState
*cs
, struct kvm_sw_breakpoint
*bp
)
1452 if (cpu_memory_rw_debug(cs
, bp
->pc
, (uint8_t *)&sc
, sizeof(sc
), 0) ||
1453 sc
!= debug_inst_opcode
||
1454 cpu_memory_rw_debug(cs
, bp
->pc
, (uint8_t *)&bp
->saved_insn
,
1462 static int find_hw_breakpoint(target_ulong addr
, int type
)
1466 assert((nb_hw_breakpoint
+ nb_hw_watchpoint
)
1467 <= ARRAY_SIZE(hw_debug_points
));
1469 for (n
= 0; n
< nb_hw_breakpoint
+ nb_hw_watchpoint
; n
++) {
1470 if (hw_debug_points
[n
].addr
== addr
&&
1471 hw_debug_points
[n
].type
== type
) {
1479 static int find_hw_watchpoint(target_ulong addr
, int *flag
)
1483 n
= find_hw_breakpoint(addr
, GDB_WATCHPOINT_ACCESS
);
1485 *flag
= BP_MEM_ACCESS
;
1489 n
= find_hw_breakpoint(addr
, GDB_WATCHPOINT_WRITE
);
1491 *flag
= BP_MEM_WRITE
;
1495 n
= find_hw_breakpoint(addr
, GDB_WATCHPOINT_READ
);
1497 *flag
= BP_MEM_READ
;
1504 int kvm_arch_insert_hw_breakpoint(target_ulong addr
,
1505 target_ulong len
, int type
)
1507 if ((nb_hw_breakpoint
+ nb_hw_watchpoint
) >= ARRAY_SIZE(hw_debug_points
)) {
1511 hw_debug_points
[nb_hw_breakpoint
+ nb_hw_watchpoint
].addr
= addr
;
1512 hw_debug_points
[nb_hw_breakpoint
+ nb_hw_watchpoint
].type
= type
;
1515 case GDB_BREAKPOINT_HW
:
1516 if (nb_hw_breakpoint
>= max_hw_breakpoint
) {
1520 if (find_hw_breakpoint(addr
, type
) >= 0) {
1527 case GDB_WATCHPOINT_WRITE
:
1528 case GDB_WATCHPOINT_READ
:
1529 case GDB_WATCHPOINT_ACCESS
:
1530 if (nb_hw_watchpoint
>= max_hw_watchpoint
) {
1534 if (find_hw_breakpoint(addr
, type
) >= 0) {
1548 int kvm_arch_remove_hw_breakpoint(target_ulong addr
,
1549 target_ulong len
, int type
)
1553 n
= find_hw_breakpoint(addr
, type
);
1559 case GDB_BREAKPOINT_HW
:
1563 case GDB_WATCHPOINT_WRITE
:
1564 case GDB_WATCHPOINT_READ
:
1565 case GDB_WATCHPOINT_ACCESS
:
1572 hw_debug_points
[n
] = hw_debug_points
[nb_hw_breakpoint
+ nb_hw_watchpoint
];
1577 void kvm_arch_remove_all_hw_breakpoints(void)
1579 nb_hw_breakpoint
= nb_hw_watchpoint
= 0;
1582 void kvm_arch_update_guest_debug(CPUState
*cs
, struct kvm_guest_debug
*dbg
)
1586 /* Software Breakpoint updates */
1587 if (kvm_sw_breakpoints_active(cs
)) {
1588 dbg
->control
|= KVM_GUESTDBG_ENABLE
| KVM_GUESTDBG_USE_SW_BP
;
1591 assert((nb_hw_breakpoint
+ nb_hw_watchpoint
)
1592 <= ARRAY_SIZE(hw_debug_points
));
1593 assert((nb_hw_breakpoint
+ nb_hw_watchpoint
) <= ARRAY_SIZE(dbg
->arch
.bp
));
1595 if (nb_hw_breakpoint
+ nb_hw_watchpoint
> 0) {
1596 dbg
->control
|= KVM_GUESTDBG_ENABLE
| KVM_GUESTDBG_USE_HW_BP
;
1597 memset(dbg
->arch
.bp
, 0, sizeof(dbg
->arch
.bp
));
1598 for (n
= 0; n
< nb_hw_breakpoint
+ nb_hw_watchpoint
; n
++) {
1599 switch (hw_debug_points
[n
].type
) {
1600 case GDB_BREAKPOINT_HW
:
1601 dbg
->arch
.bp
[n
].type
= KVMPPC_DEBUG_BREAKPOINT
;
1603 case GDB_WATCHPOINT_WRITE
:
1604 dbg
->arch
.bp
[n
].type
= KVMPPC_DEBUG_WATCH_WRITE
;
1606 case GDB_WATCHPOINT_READ
:
1607 dbg
->arch
.bp
[n
].type
= KVMPPC_DEBUG_WATCH_READ
;
1609 case GDB_WATCHPOINT_ACCESS
:
1610 dbg
->arch
.bp
[n
].type
= KVMPPC_DEBUG_WATCH_WRITE
|
1611 KVMPPC_DEBUG_WATCH_READ
;
1614 cpu_abort(cs
, "Unsupported breakpoint type\n");
1616 dbg
->arch
.bp
[n
].addr
= hw_debug_points
[n
].addr
;
1621 static int kvm_handle_hw_breakpoint(CPUState
*cs
,
1622 struct kvm_debug_exit_arch
*arch_info
)
1628 if (nb_hw_breakpoint
+ nb_hw_watchpoint
> 0) {
1629 if (arch_info
->status
& KVMPPC_DEBUG_BREAKPOINT
) {
1630 n
= find_hw_breakpoint(arch_info
->address
, GDB_BREAKPOINT_HW
);
1634 } else if (arch_info
->status
& (KVMPPC_DEBUG_WATCH_READ
|
1635 KVMPPC_DEBUG_WATCH_WRITE
)) {
1636 n
= find_hw_watchpoint(arch_info
->address
, &flag
);
1639 cs
->watchpoint_hit
= &hw_watchpoint
;
1640 hw_watchpoint
.vaddr
= hw_debug_points
[n
].addr
;
1641 hw_watchpoint
.flags
= flag
;
1648 static int kvm_handle_singlestep(void)
1653 static int kvm_handle_sw_breakpoint(void)
1658 static int kvm_handle_debug(PowerPCCPU
*cpu
, struct kvm_run
*run
)
1660 CPUState
*cs
= CPU(cpu
);
1661 CPUPPCState
*env
= &cpu
->env
;
1662 struct kvm_debug_exit_arch
*arch_info
= &run
->debug
.arch
;
1664 if (cs
->singlestep_enabled
) {
1665 return kvm_handle_singlestep();
1668 if (arch_info
->status
) {
1669 return kvm_handle_hw_breakpoint(cs
, arch_info
);
1672 if (kvm_find_sw_breakpoint(cs
, arch_info
->address
)) {
1673 return kvm_handle_sw_breakpoint();
1677 * QEMU is not able to handle debug exception, so inject
1678 * program exception to guest;
1679 * Yes program exception NOT debug exception !!
1680 * When QEMU is using debug resources then debug exception must
1681 * be always set. To achieve this we set MSR_DE and also set
1682 * MSRP_DEP so guest cannot change MSR_DE.
1683 * When emulating debug resource for guest we want guest
1684 * to control MSR_DE (enable/disable debug interrupt on need).
1685 * Supporting both configurations are NOT possible.
1686 * So the result is that we cannot share debug resources
1687 * between QEMU and Guest on BOOKE architecture.
1688 * In the current design QEMU gets the priority over guest,
1689 * this means that if QEMU is using debug resources then guest
1691 * For software breakpoint QEMU uses a privileged instruction;
1692 * So there cannot be any reason that we are here for guest
1693 * set debug exception, only possibility is guest executed a
1694 * privileged / illegal instruction and that's why we are
1695 * injecting a program interrupt.
1697 cpu_synchronize_state(cs
);
1699 * env->nip is PC, so increment this by 4 to use
1700 * ppc_cpu_do_interrupt(), which set srr0 = env->nip - 4.
1703 cs
->exception_index
= POWERPC_EXCP_PROGRAM
;
1704 env
->error_code
= POWERPC_EXCP_INVAL
;
1705 ppc_cpu_do_interrupt(cs
);
1710 int kvm_arch_handle_exit(CPUState
*cs
, struct kvm_run
*run
)
1712 PowerPCCPU
*cpu
= POWERPC_CPU(cs
);
1713 CPUPPCState
*env
= &cpu
->env
;
1716 qemu_mutex_lock_iothread();
1718 switch (run
->exit_reason
) {
1720 if (run
->dcr
.is_write
) {
1721 trace_kvm_handle_dcr_write();
1722 ret
= kvmppc_handle_dcr_write(env
, run
->dcr
.dcrn
, run
->dcr
.data
);
1724 trace_kvm_handle_drc_read();
1725 ret
= kvmppc_handle_dcr_read(env
, run
->dcr
.dcrn
, &run
->dcr
.data
);
1729 trace_kvm_handle_halt();
1730 ret
= kvmppc_handle_halt(cpu
);
1732 #if defined(TARGET_PPC64)
1733 case KVM_EXIT_PAPR_HCALL
:
1734 trace_kvm_handle_papr_hcall();
1735 run
->papr_hcall
.ret
= spapr_hypercall(cpu
,
1737 run
->papr_hcall
.args
);
1742 trace_kvm_handle_epr();
1743 run
->epr
.epr
= ldl_phys(cs
->as
, env
->mpic_iack
);
1746 case KVM_EXIT_WATCHDOG
:
1747 trace_kvm_handle_watchdog_expiry();
1748 watchdog_perform_action();
1752 case KVM_EXIT_DEBUG
:
1753 trace_kvm_handle_debug_exception();
1754 if (kvm_handle_debug(cpu
, run
)) {
1758 /* re-enter, this exception was guest-internal */
1763 fprintf(stderr
, "KVM: unknown exit reason %d\n", run
->exit_reason
);
1768 qemu_mutex_unlock_iothread();
1772 int kvmppc_or_tsr_bits(PowerPCCPU
*cpu
, uint32_t tsr_bits
)
1774 CPUState
*cs
= CPU(cpu
);
1775 uint32_t bits
= tsr_bits
;
1776 struct kvm_one_reg reg
= {
1777 .id
= KVM_REG_PPC_OR_TSR
,
1778 .addr
= (uintptr_t) &bits
,
1781 return kvm_vcpu_ioctl(cs
, KVM_SET_ONE_REG
, ®
);
1784 int kvmppc_clear_tsr_bits(PowerPCCPU
*cpu
, uint32_t tsr_bits
)
1787 CPUState
*cs
= CPU(cpu
);
1788 uint32_t bits
= tsr_bits
;
1789 struct kvm_one_reg reg
= {
1790 .id
= KVM_REG_PPC_CLEAR_TSR
,
1791 .addr
= (uintptr_t) &bits
,
1794 return kvm_vcpu_ioctl(cs
, KVM_SET_ONE_REG
, ®
);
1797 int kvmppc_set_tcr(PowerPCCPU
*cpu
)
1799 CPUState
*cs
= CPU(cpu
);
1800 CPUPPCState
*env
= &cpu
->env
;
1801 uint32_t tcr
= env
->spr
[SPR_BOOKE_TCR
];
1803 struct kvm_one_reg reg
= {
1804 .id
= KVM_REG_PPC_TCR
,
1805 .addr
= (uintptr_t) &tcr
,
1808 return kvm_vcpu_ioctl(cs
, KVM_SET_ONE_REG
, ®
);
1811 int kvmppc_booke_watchdog_enable(PowerPCCPU
*cpu
)
1813 CPUState
*cs
= CPU(cpu
);
1816 if (!kvm_enabled()) {
1820 if (!cap_ppc_watchdog
) {
1821 printf("warning: KVM does not support watchdog");
1825 ret
= kvm_vcpu_enable_cap(cs
, KVM_CAP_PPC_BOOKE_WATCHDOG
, 0);
1827 fprintf(stderr
, "%s: couldn't enable KVM_CAP_PPC_BOOKE_WATCHDOG: %s\n",
1828 __func__
, strerror(-ret
));
1835 static int read_cpuinfo(const char *field
, char *value
, int len
)
1839 int field_len
= strlen(field
);
1842 f
= fopen("/proc/cpuinfo", "r");
1848 if (!fgets(line
, sizeof(line
), f
)) {
1851 if (!strncmp(line
, field
, field_len
)) {
1852 pstrcpy(value
, len
, line
);
1863 uint32_t kvmppc_get_tbfreq(void)
1867 uint32_t retval
= NANOSECONDS_PER_SECOND
;
1869 if (read_cpuinfo("timebase", line
, sizeof(line
))) {
1873 ns
= strchr(line
, ':');
1883 bool kvmppc_get_host_serial(char **value
)
1885 return g_file_get_contents("/proc/device-tree/system-id", value
, NULL
,
1889 bool kvmppc_get_host_model(char **value
)
1891 return g_file_get_contents("/proc/device-tree/model", value
, NULL
, NULL
);
1894 /* Try to find a device tree node for a CPU with clock-frequency property */
1895 static int kvmppc_find_cpu_dt(char *buf
, int buf_len
)
1897 struct dirent
*dirp
;
1900 dp
= opendir(PROC_DEVTREE_CPU
);
1902 printf("Can't open directory " PROC_DEVTREE_CPU
"\n");
1907 while ((dirp
= readdir(dp
)) != NULL
) {
1909 snprintf(buf
, buf_len
, "%s%s/clock-frequency", PROC_DEVTREE_CPU
,
1911 f
= fopen(buf
, "r");
1913 snprintf(buf
, buf_len
, "%s%s", PROC_DEVTREE_CPU
, dirp
->d_name
);
1920 if (buf
[0] == '\0') {
1921 printf("Unknown host!\n");
1928 static uint64_t kvmppc_read_int_dt(const char *filename
)
1937 f
= fopen(filename
, "rb");
1942 len
= fread(&u
, 1, sizeof(u
), f
);
1946 /* property is a 32-bit quantity */
1947 return be32_to_cpu(u
.v32
);
1949 return be64_to_cpu(u
.v64
);
1956 * Read a CPU node property from the host device tree that's a single
1957 * integer (32-bit or 64-bit). Returns 0 if anything goes wrong
1958 * (can't find or open the property, or doesn't understand the format)
1960 static uint64_t kvmppc_read_int_cpu_dt(const char *propname
)
1962 char buf
[PATH_MAX
], *tmp
;
1965 if (kvmppc_find_cpu_dt(buf
, sizeof(buf
))) {
1969 tmp
= g_strdup_printf("%s/%s", buf
, propname
);
1970 val
= kvmppc_read_int_dt(tmp
);
1976 uint64_t kvmppc_get_clockfreq(void)
1978 return kvmppc_read_int_cpu_dt("clock-frequency");
1981 static int kvmppc_get_dec_bits(void)
1983 int nr_bits
= kvmppc_read_int_cpu_dt("ibm,dec-bits");
1991 static int kvmppc_get_pvinfo(CPUPPCState
*env
, struct kvm_ppc_pvinfo
*pvinfo
)
1993 PowerPCCPU
*cpu
= ppc_env_get_cpu(env
);
1994 CPUState
*cs
= CPU(cpu
);
1996 if (kvm_vm_check_extension(cs
->kvm_state
, KVM_CAP_PPC_GET_PVINFO
) &&
1997 !kvm_vm_ioctl(cs
->kvm_state
, KVM_PPC_GET_PVINFO
, pvinfo
)) {
2004 int kvmppc_get_hasidle(CPUPPCState
*env
)
2006 struct kvm_ppc_pvinfo pvinfo
;
2008 if (!kvmppc_get_pvinfo(env
, &pvinfo
) &&
2009 (pvinfo
.flags
& KVM_PPC_PVINFO_FLAGS_EV_IDLE
)) {
2016 int kvmppc_get_hypercall(CPUPPCState
*env
, uint8_t *buf
, int buf_len
)
2018 uint32_t *hc
= (uint32_t *)buf
;
2019 struct kvm_ppc_pvinfo pvinfo
;
2021 if (!kvmppc_get_pvinfo(env
, &pvinfo
)) {
2022 memcpy(buf
, pvinfo
.hcall
, buf_len
);
2027 * Fallback to always fail hypercalls regardless of endianness:
2029 * tdi 0,r0,72 (becomes b .+8 in wrong endian, nop in good endian)
2031 * b .+8 (becomes nop in wrong endian)
2032 * bswap32(li r3, -1)
2035 hc
[0] = cpu_to_be32(0x08000048);
2036 hc
[1] = cpu_to_be32(0x3860ffff);
2037 hc
[2] = cpu_to_be32(0x48000008);
2038 hc
[3] = cpu_to_be32(bswap32(0x3860ffff));
2043 static inline int kvmppc_enable_hcall(KVMState
*s
, target_ulong hcall
)
2045 return kvm_vm_enable_cap(s
, KVM_CAP_PPC_ENABLE_HCALL
, 0, hcall
, 1);
2048 void kvmppc_enable_logical_ci_hcalls(void)
2051 * FIXME: it would be nice if we could detect the cases where
2052 * we're using a device which requires the in kernel
2053 * implementation of these hcalls, but the kernel lacks them and
2054 * produce a warning.
2056 kvmppc_enable_hcall(kvm_state
, H_LOGICAL_CI_LOAD
);
2057 kvmppc_enable_hcall(kvm_state
, H_LOGICAL_CI_STORE
);
2060 void kvmppc_enable_set_mode_hcall(void)
2062 kvmppc_enable_hcall(kvm_state
, H_SET_MODE
);
2065 void kvmppc_enable_clear_ref_mod_hcalls(void)
2067 kvmppc_enable_hcall(kvm_state
, H_CLEAR_REF
);
2068 kvmppc_enable_hcall(kvm_state
, H_CLEAR_MOD
);
2071 void kvmppc_enable_h_page_init(void)
2073 kvmppc_enable_hcall(kvm_state
, H_PAGE_INIT
);
2076 void kvmppc_set_papr(PowerPCCPU
*cpu
)
2078 CPUState
*cs
= CPU(cpu
);
2081 if (!kvm_enabled()) {
2085 ret
= kvm_vcpu_enable_cap(cs
, KVM_CAP_PPC_PAPR
, 0);
2087 error_report("This vCPU type or KVM version does not support PAPR");
2092 * Update the capability flag so we sync the right information
2098 int kvmppc_set_compat(PowerPCCPU
*cpu
, uint32_t compat_pvr
)
2100 return kvm_set_one_reg(CPU(cpu
), KVM_REG_PPC_ARCH_COMPAT
, &compat_pvr
);
2103 void kvmppc_set_mpic_proxy(PowerPCCPU
*cpu
, int mpic_proxy
)
2105 CPUState
*cs
= CPU(cpu
);
2108 ret
= kvm_vcpu_enable_cap(cs
, KVM_CAP_PPC_EPR
, 0, mpic_proxy
);
2109 if (ret
&& mpic_proxy
) {
2110 error_report("This KVM version does not support EPR");
2115 int kvmppc_smt_threads(void)
2117 return cap_ppc_smt
? cap_ppc_smt
: 1;
2120 int kvmppc_set_smt_threads(int smt
)
2124 ret
= kvm_vm_enable_cap(kvm_state
, KVM_CAP_PPC_SMT
, 0, smt
, 0);
2131 void kvmppc_hint_smt_possible(Error
**errp
)
2137 assert(kvm_enabled());
2138 if (cap_ppc_smt_possible
) {
2139 g
= g_string_new("Available VSMT modes:");
2140 for (i
= 63; i
>= 0; i
--) {
2141 if ((1UL << i
) & cap_ppc_smt_possible
) {
2142 g_string_append_printf(g
, " %lu", (1UL << i
));
2145 s
= g_string_free(g
, false);
2146 error_append_hint(errp
, "%s.\n", s
);
2149 error_append_hint(errp
,
2150 "This KVM seems to be too old to support VSMT.\n");
2156 uint64_t kvmppc_rma_size(uint64_t current_size
, unsigned int hash_shift
)
2158 struct kvm_ppc_smmu_info info
;
2159 long rampagesize
, best_page_shift
;
2163 * Find the largest hardware supported page size that's less than
2164 * or equal to the (logical) backing page size of guest RAM
2166 kvm_get_smmu_info(&info
, &error_fatal
);
2167 rampagesize
= qemu_minrampagesize();
2168 best_page_shift
= 0;
2170 for (i
= 0; i
< KVM_PPC_PAGE_SIZES_MAX_SZ
; i
++) {
2171 struct kvm_ppc_one_seg_page_size
*sps
= &info
.sps
[i
];
2173 if (!sps
->page_shift
) {
2177 if ((sps
->page_shift
> best_page_shift
)
2178 && ((1UL << sps
->page_shift
) <= rampagesize
)) {
2179 best_page_shift
= sps
->page_shift
;
2183 return MIN(current_size
,
2184 1ULL << (best_page_shift
+ hash_shift
- 7));
2188 bool kvmppc_spapr_use_multitce(void)
2190 return cap_spapr_multitce
;
2193 int kvmppc_spapr_enable_inkernel_multitce(void)
2197 ret
= kvm_vm_enable_cap(kvm_state
, KVM_CAP_PPC_ENABLE_HCALL
, 0,
2198 H_PUT_TCE_INDIRECT
, 1);
2200 ret
= kvm_vm_enable_cap(kvm_state
, KVM_CAP_PPC_ENABLE_HCALL
, 0,
2207 void *kvmppc_create_spapr_tce(uint32_t liobn
, uint32_t page_shift
,
2208 uint64_t bus_offset
, uint32_t nb_table
,
2209 int *pfd
, bool need_vfio
)
2216 * Must set fd to -1 so we don't try to munmap when called for
2217 * destroying the table, which the upper layers -will- do
2220 if (!cap_spapr_tce
|| (need_vfio
&& !cap_spapr_vfio
)) {
2224 if (cap_spapr_tce_64
) {
2225 struct kvm_create_spapr_tce_64 args
= {
2227 .page_shift
= page_shift
,
2228 .offset
= bus_offset
>> page_shift
,
2232 fd
= kvm_vm_ioctl(kvm_state
, KVM_CREATE_SPAPR_TCE_64
, &args
);
2235 "KVM: Failed to create TCE64 table for liobn 0x%x\n",
2239 } else if (cap_spapr_tce
) {
2240 uint64_t window_size
= (uint64_t) nb_table
<< page_shift
;
2241 struct kvm_create_spapr_tce args
= {
2243 .window_size
= window_size
,
2245 if ((window_size
!= args
.window_size
) || bus_offset
) {
2248 fd
= kvm_vm_ioctl(kvm_state
, KVM_CREATE_SPAPR_TCE
, &args
);
2250 fprintf(stderr
, "KVM: Failed to create TCE table for liobn 0x%x\n",
2258 len
= nb_table
* sizeof(uint64_t);
2259 /* FIXME: round this up to page size */
2261 table
= mmap(NULL
, len
, PROT_READ
| PROT_WRITE
, MAP_SHARED
, fd
, 0);
2262 if (table
== MAP_FAILED
) {
2263 fprintf(stderr
, "KVM: Failed to map TCE table for liobn 0x%x\n",
2273 int kvmppc_remove_spapr_tce(void *table
, int fd
, uint32_t nb_table
)
2281 len
= nb_table
* sizeof(uint64_t);
2282 if ((munmap(table
, len
) < 0) ||
2284 fprintf(stderr
, "KVM: Unexpected error removing TCE table: %s",
2286 /* Leak the table */
2292 int kvmppc_reset_htab(int shift_hint
)
2294 uint32_t shift
= shift_hint
;
2296 if (!kvm_enabled()) {
2297 /* Full emulation, tell caller to allocate htab itself */
2300 if (kvm_vm_check_extension(kvm_state
, KVM_CAP_PPC_ALLOC_HTAB
)) {
2302 ret
= kvm_vm_ioctl(kvm_state
, KVM_PPC_ALLOCATE_HTAB
, &shift
);
2303 if (ret
== -ENOTTY
) {
2305 * At least some versions of PR KVM advertise the
2306 * capability, but don't implement the ioctl(). Oops.
2307 * Return 0 so that we allocate the htab in qemu, as is
2311 } else if (ret
< 0) {
2318 * We have a kernel that predates the htab reset calls. For PR
2319 * KVM, we need to allocate the htab ourselves, for an HV KVM of
2320 * this era, it has allocated a 16MB fixed size hash table
2323 if (kvmppc_is_pr(kvm_state
)) {
2324 /* PR - tell caller to allocate htab */
2327 /* HV - assume 16MB kernel allocated htab */
2332 static inline uint32_t mfpvr(void)
2341 static void alter_insns(uint64_t *word
, uint64_t flags
, bool on
)
2350 static void kvmppc_host_cpu_class_init(ObjectClass
*oc
, void *data
)
2352 PowerPCCPUClass
*pcc
= POWERPC_CPU_CLASS(oc
);
2353 uint32_t dcache_size
= kvmppc_read_int_cpu_dt("d-cache-size");
2354 uint32_t icache_size
= kvmppc_read_int_cpu_dt("i-cache-size");
2356 /* Now fix up the class with information we can query from the host */
2359 alter_insns(&pcc
->insns_flags
, PPC_ALTIVEC
,
2360 qemu_getauxval(AT_HWCAP
) & PPC_FEATURE_HAS_ALTIVEC
);
2361 alter_insns(&pcc
->insns_flags2
, PPC2_VSX
,
2362 qemu_getauxval(AT_HWCAP
) & PPC_FEATURE_HAS_VSX
);
2363 alter_insns(&pcc
->insns_flags2
, PPC2_DFP
,
2364 qemu_getauxval(AT_HWCAP
) & PPC_FEATURE_HAS_DFP
);
2366 if (dcache_size
!= -1) {
2367 pcc
->l1_dcache_size
= dcache_size
;
2370 if (icache_size
!= -1) {
2371 pcc
->l1_icache_size
= icache_size
;
2374 #if defined(TARGET_PPC64)
2375 pcc
->radix_page_info
= kvm_get_radix_page_info();
2377 if ((pcc
->pvr
& 0xffffff00) == CPU_POWERPC_POWER9_DD1
) {
2379 * POWER9 DD1 has some bugs which make it not really ISA 3.00
2380 * compliant. More importantly, advertising ISA 3.00
2381 * architected mode may prevent guests from activating
2382 * necessary DD1 workarounds.
2384 pcc
->pcr_supported
&= ~(PCR_COMPAT_3_00
| PCR_COMPAT_2_07
2385 | PCR_COMPAT_2_06
| PCR_COMPAT_2_05
);
2387 #endif /* defined(TARGET_PPC64) */
2390 bool kvmppc_has_cap_epr(void)
2395 bool kvmppc_has_cap_fixup_hcalls(void)
2397 return cap_fixup_hcalls
;
2400 bool kvmppc_has_cap_htm(void)
2405 bool kvmppc_has_cap_mmu_radix(void)
2407 return cap_mmu_radix
;
2410 bool kvmppc_has_cap_mmu_hash_v3(void)
2412 return cap_mmu_hash_v3
;
2415 static bool kvmppc_power8_host(void)
2420 uint32_t base_pvr
= CPU_POWERPC_POWER_SERVER_MASK
& mfpvr();
2421 ret
= (base_pvr
== CPU_POWERPC_POWER8E_BASE
) ||
2422 (base_pvr
== CPU_POWERPC_POWER8NVL_BASE
) ||
2423 (base_pvr
== CPU_POWERPC_POWER8_BASE
);
2425 #endif /* TARGET_PPC64 */
2429 static int parse_cap_ppc_safe_cache(struct kvm_ppc_cpu_char c
)
2431 bool l1d_thread_priv_req
= !kvmppc_power8_host();
2433 if (~c
.behaviour
& c
.behaviour_mask
& H_CPU_BEHAV_L1D_FLUSH_PR
) {
2435 } else if ((!l1d_thread_priv_req
||
2436 c
.character
& c
.character_mask
& H_CPU_CHAR_L1D_THREAD_PRIV
) &&
2437 (c
.character
& c
.character_mask
2438 & (H_CPU_CHAR_L1D_FLUSH_ORI30
| H_CPU_CHAR_L1D_FLUSH_TRIG2
))) {
2445 static int parse_cap_ppc_safe_bounds_check(struct kvm_ppc_cpu_char c
)
2447 if (~c
.behaviour
& c
.behaviour_mask
& H_CPU_BEHAV_BNDS_CHK_SPEC_BAR
) {
2449 } else if (c
.character
& c
.character_mask
& H_CPU_CHAR_SPEC_BAR_ORI31
) {
2456 static int parse_cap_ppc_safe_indirect_branch(struct kvm_ppc_cpu_char c
)
2458 if ((~c
.behaviour
& c
.behaviour_mask
& H_CPU_BEHAV_FLUSH_COUNT_CACHE
) &&
2459 (~c
.character
& c
.character_mask
& H_CPU_CHAR_CACHE_COUNT_DIS
) &&
2460 (~c
.character
& c
.character_mask
& H_CPU_CHAR_BCCTRL_SERIALISED
)) {
2461 return SPAPR_CAP_FIXED_NA
;
2462 } else if (c
.behaviour
& c
.behaviour_mask
& H_CPU_BEHAV_FLUSH_COUNT_CACHE
) {
2463 return SPAPR_CAP_WORKAROUND
;
2464 } else if (c
.character
& c
.character_mask
& H_CPU_CHAR_CACHE_COUNT_DIS
) {
2465 return SPAPR_CAP_FIXED_CCD
;
2466 } else if (c
.character
& c
.character_mask
& H_CPU_CHAR_BCCTRL_SERIALISED
) {
2467 return SPAPR_CAP_FIXED_IBS
;
2473 static int parse_cap_ppc_count_cache_flush_assist(struct kvm_ppc_cpu_char c
)
2475 if (c
.character
& c
.character_mask
& H_CPU_CHAR_BCCTR_FLUSH_ASSIST
) {
2481 static void kvmppc_get_cpu_characteristics(KVMState
*s
)
2483 struct kvm_ppc_cpu_char c
;
2487 cap_ppc_safe_cache
= 0;
2488 cap_ppc_safe_bounds_check
= 0;
2489 cap_ppc_safe_indirect_branch
= 0;
2491 ret
= kvm_vm_check_extension(s
, KVM_CAP_PPC_GET_CPU_CHAR
);
2495 ret
= kvm_vm_ioctl(s
, KVM_PPC_GET_CPU_CHAR
, &c
);
2500 cap_ppc_safe_cache
= parse_cap_ppc_safe_cache(c
);
2501 cap_ppc_safe_bounds_check
= parse_cap_ppc_safe_bounds_check(c
);
2502 cap_ppc_safe_indirect_branch
= parse_cap_ppc_safe_indirect_branch(c
);
2503 cap_ppc_count_cache_flush_assist
=
2504 parse_cap_ppc_count_cache_flush_assist(c
);
2507 int kvmppc_get_cap_safe_cache(void)
2509 return cap_ppc_safe_cache
;
2512 int kvmppc_get_cap_safe_bounds_check(void)
2514 return cap_ppc_safe_bounds_check
;
2517 int kvmppc_get_cap_safe_indirect_branch(void)
2519 return cap_ppc_safe_indirect_branch
;
2522 int kvmppc_get_cap_count_cache_flush_assist(void)
2524 return cap_ppc_count_cache_flush_assist
;
2527 bool kvmppc_has_cap_nested_kvm_hv(void)
2529 return !!cap_ppc_nested_kvm_hv
;
2532 int kvmppc_set_cap_nested_kvm_hv(int enable
)
2534 return kvm_vm_enable_cap(kvm_state
, KVM_CAP_PPC_NESTED_HV
, 0, enable
);
2537 bool kvmppc_has_cap_spapr_vfio(void)
2539 return cap_spapr_vfio
;
2542 int kvmppc_get_cap_large_decr(void)
2544 return cap_large_decr
;
2547 int kvmppc_enable_cap_large_decr(PowerPCCPU
*cpu
, int enable
)
2549 CPUState
*cs
= CPU(cpu
);
2552 kvm_get_one_reg(cs
, KVM_REG_PPC_LPCR_64
, &lpcr
);
2553 /* Do we need to modify the LPCR? */
2554 if (!!(lpcr
& LPCR_LD
) != !!enable
) {
2560 kvm_set_one_reg(cs
, KVM_REG_PPC_LPCR_64
, &lpcr
);
2561 kvm_get_one_reg(cs
, KVM_REG_PPC_LPCR_64
, &lpcr
);
2563 if (!!(lpcr
& LPCR_LD
) != !!enable
) {
2571 PowerPCCPUClass
*kvm_ppc_get_host_cpu_class(void)
2573 uint32_t host_pvr
= mfpvr();
2574 PowerPCCPUClass
*pvr_pcc
;
2576 pvr_pcc
= ppc_cpu_class_by_pvr(host_pvr
);
2577 if (pvr_pcc
== NULL
) {
2578 pvr_pcc
= ppc_cpu_class_by_pvr_mask(host_pvr
);
2584 static int kvm_ppc_register_host_cpu_type(MachineState
*ms
)
2586 TypeInfo type_info
= {
2587 .name
= TYPE_HOST_POWERPC_CPU
,
2588 .class_init
= kvmppc_host_cpu_class_init
,
2590 MachineClass
*mc
= MACHINE_GET_CLASS(ms
);
2591 PowerPCCPUClass
*pvr_pcc
;
2596 pvr_pcc
= kvm_ppc_get_host_cpu_class();
2597 if (pvr_pcc
== NULL
) {
2600 type_info
.parent
= object_class_get_name(OBJECT_CLASS(pvr_pcc
));
2601 type_register(&type_info
);
2602 if (object_dynamic_cast(OBJECT(ms
), TYPE_SPAPR_MACHINE
)) {
2603 /* override TCG default cpu type with 'host' cpu model */
2604 mc
->default_cpu_type
= TYPE_HOST_POWERPC_CPU
;
2607 oc
= object_class_by_name(type_info
.name
);
2611 * Update generic CPU family class alias (e.g. on a POWER8NVL host,
2612 * we want "POWER8" to be a "family" alias that points to the current
2613 * host CPU type, too)
2615 dc
= DEVICE_CLASS(ppc_cpu_get_family_class(pvr_pcc
));
2616 for (i
= 0; ppc_cpu_aliases
[i
].alias
!= NULL
; i
++) {
2617 if (strcasecmp(ppc_cpu_aliases
[i
].alias
, dc
->desc
) == 0) {
2620 ppc_cpu_aliases
[i
].model
= g_strdup(object_class_get_name(oc
));
2621 suffix
= strstr(ppc_cpu_aliases
[i
].model
, POWERPC_CPU_TYPE_SUFFIX
);
2632 int kvmppc_define_rtas_kernel_token(uint32_t token
, const char *function
)
2634 struct kvm_rtas_token_args args
= {
2638 if (!kvm_check_extension(kvm_state
, KVM_CAP_PPC_RTAS
)) {
2642 strncpy(args
.name
, function
, sizeof(args
.name
));
2644 return kvm_vm_ioctl(kvm_state
, KVM_PPC_RTAS_DEFINE_TOKEN
, &args
);
2647 int kvmppc_get_htab_fd(bool write
, uint64_t index
, Error
**errp
)
2649 struct kvm_get_htab_fd s
= {
2650 .flags
= write
? KVM_GET_HTAB_WRITE
: 0,
2651 .start_index
= index
,
2656 error_setg(errp
, "KVM version doesn't support %s the HPT",
2657 write
? "writing" : "reading");
2661 ret
= kvm_vm_ioctl(kvm_state
, KVM_PPC_GET_HTAB_FD
, &s
);
2663 error_setg(errp
, "Unable to open fd for %s HPT %s KVM: %s",
2664 write
? "writing" : "reading", write
? "to" : "from",
2672 int kvmppc_save_htab(QEMUFile
*f
, int fd
, size_t bufsize
, int64_t max_ns
)
2674 int64_t starttime
= qemu_clock_get_ns(QEMU_CLOCK_REALTIME
);
2675 uint8_t buf
[bufsize
];
2679 rc
= read(fd
, buf
, bufsize
);
2681 fprintf(stderr
, "Error reading data from KVM HTAB fd: %s\n",
2685 uint8_t *buffer
= buf
;
2688 struct kvm_get_htab_header
*head
=
2689 (struct kvm_get_htab_header
*) buffer
;
2690 size_t chunksize
= sizeof(*head
) +
2691 HASH_PTE_SIZE_64
* head
->n_valid
;
2693 qemu_put_be32(f
, head
->index
);
2694 qemu_put_be16(f
, head
->n_valid
);
2695 qemu_put_be16(f
, head
->n_invalid
);
2696 qemu_put_buffer(f
, (void *)(head
+ 1),
2697 HASH_PTE_SIZE_64
* head
->n_valid
);
2699 buffer
+= chunksize
;
2705 ((qemu_clock_get_ns(QEMU_CLOCK_REALTIME
) - starttime
) < max_ns
)));
2707 return (rc
== 0) ? 1 : 0;
2710 int kvmppc_load_htab_chunk(QEMUFile
*f
, int fd
, uint32_t index
,
2711 uint16_t n_valid
, uint16_t n_invalid
)
2713 struct kvm_get_htab_header
*buf
;
2714 size_t chunksize
= sizeof(*buf
) + n_valid
* HASH_PTE_SIZE_64
;
2717 buf
= alloca(chunksize
);
2719 buf
->n_valid
= n_valid
;
2720 buf
->n_invalid
= n_invalid
;
2722 qemu_get_buffer(f
, (void *)(buf
+ 1), HASH_PTE_SIZE_64
* n_valid
);
2724 rc
= write(fd
, buf
, chunksize
);
2726 fprintf(stderr
, "Error writing KVM hash table: %s\n",
2730 if (rc
!= chunksize
) {
2731 /* We should never get a short write on a single chunk */
2732 fprintf(stderr
, "Short write, restoring KVM hash table\n");
2738 bool kvm_arch_stop_on_emulation_error(CPUState
*cpu
)
2743 void kvm_arch_init_irq_routing(KVMState
*s
)
2747 void kvmppc_read_hptes(ppc_hash_pte64_t
*hptes
, hwaddr ptex
, int n
)
2752 fd
= kvmppc_get_htab_fd(false, ptex
, &error_abort
);
2756 struct kvm_get_htab_header
*hdr
;
2757 int m
= n
< HPTES_PER_GROUP
? n
: HPTES_PER_GROUP
;
2758 char buf
[sizeof(*hdr
) + m
* HASH_PTE_SIZE_64
];
2760 rc
= read(fd
, buf
, sizeof(buf
));
2762 hw_error("kvmppc_read_hptes: Unable to read HPTEs");
2765 hdr
= (struct kvm_get_htab_header
*)buf
;
2766 while ((i
< n
) && ((char *)hdr
< (buf
+ rc
))) {
2767 int invalid
= hdr
->n_invalid
, valid
= hdr
->n_valid
;
2769 if (hdr
->index
!= (ptex
+ i
)) {
2770 hw_error("kvmppc_read_hptes: Unexpected HPTE index %"PRIu32
2771 " != (%"HWADDR_PRIu
" + %d", hdr
->index
, ptex
, i
);
2774 if (n
- i
< valid
) {
2777 memcpy(hptes
+ i
, hdr
+ 1, HASH_PTE_SIZE_64
* valid
);
2780 if ((n
- i
) < invalid
) {
2783 memset(hptes
+ i
, 0, invalid
* HASH_PTE_SIZE_64
);
2786 hdr
= (struct kvm_get_htab_header
*)
2787 ((char *)(hdr
+ 1) + HASH_PTE_SIZE_64
* hdr
->n_valid
);
2794 void kvmppc_write_hpte(hwaddr ptex
, uint64_t pte0
, uint64_t pte1
)
2798 struct kvm_get_htab_header hdr
;
2803 fd
= kvmppc_get_htab_fd(true, 0 /* Ignored */, &error_abort
);
2805 buf
.hdr
.n_valid
= 1;
2806 buf
.hdr
.n_invalid
= 0;
2807 buf
.hdr
.index
= ptex
;
2808 buf
.pte0
= cpu_to_be64(pte0
);
2809 buf
.pte1
= cpu_to_be64(pte1
);
2811 rc
= write(fd
, &buf
, sizeof(buf
));
2812 if (rc
!= sizeof(buf
)) {
2813 hw_error("kvmppc_write_hpte: Unable to update KVM HPT");
2818 int kvm_arch_fixup_msi_route(struct kvm_irq_routing_entry
*route
,
2819 uint64_t address
, uint32_t data
, PCIDevice
*dev
)
2824 int kvm_arch_add_msi_route_post(struct kvm_irq_routing_entry
*route
,
2825 int vector
, PCIDevice
*dev
)
2830 int kvm_arch_release_virq_post(int virq
)
2835 int kvm_arch_msi_data_to_gsi(uint32_t data
)
2837 return data
& 0xffff;
2840 int kvmppc_enable_hwrng(void)
2842 if (!kvm_enabled() || !kvm_check_extension(kvm_state
, KVM_CAP_PPC_HWRNG
)) {
2846 return kvmppc_enable_hcall(kvm_state
, H_RANDOM
);
2849 void kvmppc_check_papr_resize_hpt(Error
**errp
)
2851 if (!kvm_enabled()) {
2852 return; /* No KVM, we're good */
2855 if (cap_resize_hpt
) {
2856 return; /* Kernel has explicit support, we're good */
2859 /* Otherwise fallback on looking for PR KVM */
2860 if (kvmppc_is_pr(kvm_state
)) {
2865 "Hash page table resizing not available with this KVM version");
2868 int kvmppc_resize_hpt_prepare(PowerPCCPU
*cpu
, target_ulong flags
, int shift
)
2870 CPUState
*cs
= CPU(cpu
);
2871 struct kvm_ppc_resize_hpt rhpt
= {
2876 if (!cap_resize_hpt
) {
2880 return kvm_vm_ioctl(cs
->kvm_state
, KVM_PPC_RESIZE_HPT_PREPARE
, &rhpt
);
2883 int kvmppc_resize_hpt_commit(PowerPCCPU
*cpu
, target_ulong flags
, int shift
)
2885 CPUState
*cs
= CPU(cpu
);
2886 struct kvm_ppc_resize_hpt rhpt
= {
2891 if (!cap_resize_hpt
) {
2895 return kvm_vm_ioctl(cs
->kvm_state
, KVM_PPC_RESIZE_HPT_COMMIT
, &rhpt
);
2899 * This is a helper function to detect a post migration scenario
2900 * in which a guest, running as KVM-HV, freezes in cpu_post_load because
2901 * the guest kernel can't handle a PVR value other than the actual host
2902 * PVR in KVM_SET_SREGS, even if pvr_match() returns true.
2904 * If we don't have cap_ppc_pvr_compat and we're not running in PR
2905 * (so, we're HV), return true. The workaround itself is done in
2908 * The order here is important: we'll only check for KVM PR as a
2909 * fallback if the guest kernel can't handle the situation itself.
2910 * We need to avoid as much as possible querying the running KVM type
2913 bool kvmppc_pvr_workaround_required(PowerPCCPU
*cpu
)
2915 CPUState
*cs
= CPU(cpu
);
2917 if (!kvm_enabled()) {
2921 if (cap_ppc_pvr_compat
) {
2925 return !kvmppc_is_pr(cs
->kvm_state
);
2928 void kvmppc_set_reg_ppc_online(PowerPCCPU
*cpu
, unsigned int online
)
2930 CPUState
*cs
= CPU(cpu
);
2932 if (kvm_enabled()) {
2933 kvm_set_one_reg(cs
, KVM_REG_PPC_ONLINE
, &online
);