tcg: Avoid including 'trace-tcg.h' in target translate.c
[qemu/ar7.git] / target / ppc / kvm.c
blob104a308abb5700b2fe075397271f314d7f607543
1 /*
2 * PowerPC implementation of KVM hooks
4 * Copyright IBM Corp. 2007
5 * Copyright (C) 2011 Freescale Semiconductor, Inc.
7 * Authors:
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"
18 #include <dirent.h>
19 #include <sys/ioctl.h>
20 #include <sys/vfs.h>
22 #include <linux/kvm.h>
24 #include "qemu-common.h"
25 #include "qapi/error.h"
26 #include "qemu/error-report.h"
27 #include "cpu.h"
28 #include "cpu-models.h"
29 #include "qemu/timer.h"
30 #include "sysemu/hw_accel.h"
31 #include "kvm_ppc.h"
32 #include "sysemu/cpus.h"
33 #include "sysemu/device_tree.h"
34 #include "mmu-hash64.h"
36 #include "hw/sysbus.h"
37 #include "hw/ppc/spapr.h"
38 #include "hw/ppc/spapr_cpu_core.h"
39 #include "hw/hw.h"
40 #include "hw/ppc/ppc.h"
41 #include "migration/qemu-file-types.h"
42 #include "sysemu/watchdog.h"
43 #include "trace.h"
44 #include "exec/gdbstub.h"
45 #include "exec/memattrs.h"
46 #include "exec/ram_addr.h"
47 #include "sysemu/hostmem.h"
48 #include "qemu/cutils.h"
49 #include "qemu/main-loop.h"
50 #include "qemu/mmap-alloc.h"
51 #include "elf.h"
52 #include "sysemu/kvm_int.h"
54 #define PROC_DEVTREE_CPU "/proc/device-tree/cpus/"
56 #define DEBUG_RETURN_GUEST 0
57 #define DEBUG_RETURN_GDB 1
59 const KVMCapabilityInfo kvm_arch_required_capabilities[] = {
60 KVM_CAP_LAST_INFO
63 static int cap_interrupt_unset;
64 static int cap_segstate;
65 static int cap_booke_sregs;
66 static int cap_ppc_smt;
67 static int cap_ppc_smt_possible;
68 static int cap_spapr_tce;
69 static int cap_spapr_tce_64;
70 static int cap_spapr_multitce;
71 static int cap_spapr_vfio;
72 static int cap_hior;
73 static int cap_one_reg;
74 static int cap_epr;
75 static int cap_ppc_watchdog;
76 static int cap_papr;
77 static int cap_htab_fd;
78 static int cap_fixup_hcalls;
79 static int cap_htm; /* Hardware transactional memory support */
80 static int cap_mmu_radix;
81 static int cap_mmu_hash_v3;
82 static int cap_xive;
83 static int cap_resize_hpt;
84 static int cap_ppc_pvr_compat;
85 static int cap_ppc_safe_cache;
86 static int cap_ppc_safe_bounds_check;
87 static int cap_ppc_safe_indirect_branch;
88 static int cap_ppc_count_cache_flush_assist;
89 static int cap_ppc_nested_kvm_hv;
90 static int cap_large_decr;
91 static int cap_fwnmi;
93 static uint32_t debug_inst_opcode;
96 * Check whether we are running with KVM-PR (instead of KVM-HV). This
97 * should only be used for fallback tests - generally we should use
98 * explicit capabilities for the features we want, rather than
99 * assuming what is/isn't available depending on the KVM variant.
101 static bool kvmppc_is_pr(KVMState *ks)
103 /* Assume KVM-PR if the GET_PVINFO capability is available */
104 return kvm_vm_check_extension(ks, KVM_CAP_PPC_GET_PVINFO) != 0;
107 static int kvm_ppc_register_host_cpu_type(void);
108 static void kvmppc_get_cpu_characteristics(KVMState *s);
109 static int kvmppc_get_dec_bits(void);
111 int kvm_arch_init(MachineState *ms, KVMState *s)
113 cap_interrupt_unset = kvm_check_extension(s, KVM_CAP_PPC_UNSET_IRQ);
114 cap_segstate = kvm_check_extension(s, KVM_CAP_PPC_SEGSTATE);
115 cap_booke_sregs = kvm_check_extension(s, KVM_CAP_PPC_BOOKE_SREGS);
116 cap_ppc_smt_possible = kvm_vm_check_extension(s, KVM_CAP_PPC_SMT_POSSIBLE);
117 cap_spapr_tce = kvm_check_extension(s, KVM_CAP_SPAPR_TCE);
118 cap_spapr_tce_64 = kvm_check_extension(s, KVM_CAP_SPAPR_TCE_64);
119 cap_spapr_multitce = kvm_check_extension(s, KVM_CAP_SPAPR_MULTITCE);
120 cap_spapr_vfio = kvm_vm_check_extension(s, KVM_CAP_SPAPR_TCE_VFIO);
121 cap_one_reg = kvm_check_extension(s, KVM_CAP_ONE_REG);
122 cap_hior = kvm_check_extension(s, KVM_CAP_PPC_HIOR);
123 cap_epr = kvm_check_extension(s, KVM_CAP_PPC_EPR);
124 cap_ppc_watchdog = kvm_check_extension(s, KVM_CAP_PPC_BOOKE_WATCHDOG);
126 * Note: we don't set cap_papr here, because this capability is
127 * only activated after this by kvmppc_set_papr()
129 cap_htab_fd = kvm_vm_check_extension(s, KVM_CAP_PPC_HTAB_FD);
130 cap_fixup_hcalls = kvm_check_extension(s, KVM_CAP_PPC_FIXUP_HCALL);
131 cap_ppc_smt = kvm_vm_check_extension(s, KVM_CAP_PPC_SMT);
132 cap_htm = kvm_vm_check_extension(s, KVM_CAP_PPC_HTM);
133 cap_mmu_radix = kvm_vm_check_extension(s, KVM_CAP_PPC_MMU_RADIX);
134 cap_mmu_hash_v3 = kvm_vm_check_extension(s, KVM_CAP_PPC_MMU_HASH_V3);
135 cap_xive = kvm_vm_check_extension(s, KVM_CAP_PPC_IRQ_XIVE);
136 cap_resize_hpt = kvm_vm_check_extension(s, KVM_CAP_SPAPR_RESIZE_HPT);
137 kvmppc_get_cpu_characteristics(s);
138 cap_ppc_nested_kvm_hv = kvm_vm_check_extension(s, KVM_CAP_PPC_NESTED_HV);
139 cap_large_decr = kvmppc_get_dec_bits();
140 cap_fwnmi = kvm_vm_check_extension(s, KVM_CAP_PPC_FWNMI);
142 * Note: setting it to false because there is not such capability
143 * in KVM at this moment.
145 * TODO: call kvm_vm_check_extension() with the right capability
146 * after the kernel starts implementing it.
148 cap_ppc_pvr_compat = false;
150 if (!kvm_check_extension(s, KVM_CAP_PPC_IRQ_LEVEL)) {
151 error_report("KVM: Host kernel doesn't have level irq capability");
152 exit(1);
155 kvm_ppc_register_host_cpu_type();
157 return 0;
160 int kvm_arch_irqchip_create(KVMState *s)
162 return 0;
165 static int kvm_arch_sync_sregs(PowerPCCPU *cpu)
167 CPUPPCState *cenv = &cpu->env;
168 CPUState *cs = CPU(cpu);
169 struct kvm_sregs sregs;
170 int ret;
172 if (cenv->excp_model == POWERPC_EXCP_BOOKE) {
174 * What we're really trying to say is "if we're on BookE, we
175 * use the native PVR for now". This is the only sane way to
176 * check it though, so we potentially confuse users that they
177 * can run BookE guests on BookS. Let's hope nobody dares
178 * enough :)
180 return 0;
181 } else {
182 if (!cap_segstate) {
183 fprintf(stderr, "kvm error: missing PVR setting capability\n");
184 return -ENOSYS;
188 ret = kvm_vcpu_ioctl(cs, KVM_GET_SREGS, &sregs);
189 if (ret) {
190 return ret;
193 sregs.pvr = cenv->spr[SPR_PVR];
194 return kvm_vcpu_ioctl(cs, KVM_SET_SREGS, &sregs);
197 /* Set up a shared TLB array with KVM */
198 static int kvm_booke206_tlb_init(PowerPCCPU *cpu)
200 CPUPPCState *env = &cpu->env;
201 CPUState *cs = CPU(cpu);
202 struct kvm_book3e_206_tlb_params params = {};
203 struct kvm_config_tlb cfg = {};
204 unsigned int entries = 0;
205 int ret, i;
207 if (!kvm_enabled() ||
208 !kvm_check_extension(cs->kvm_state, KVM_CAP_SW_TLB)) {
209 return 0;
212 assert(ARRAY_SIZE(params.tlb_sizes) == BOOKE206_MAX_TLBN);
214 for (i = 0; i < BOOKE206_MAX_TLBN; i++) {
215 params.tlb_sizes[i] = booke206_tlb_size(env, i);
216 params.tlb_ways[i] = booke206_tlb_ways(env, i);
217 entries += params.tlb_sizes[i];
220 assert(entries == env->nb_tlb);
221 assert(sizeof(struct kvm_book3e_206_tlb_entry) == sizeof(ppcmas_tlb_t));
223 env->tlb_dirty = true;
225 cfg.array = (uintptr_t)env->tlb.tlbm;
226 cfg.array_len = sizeof(ppcmas_tlb_t) * entries;
227 cfg.params = (uintptr_t)&params;
228 cfg.mmu_type = KVM_MMU_FSL_BOOKE_NOHV;
230 ret = kvm_vcpu_enable_cap(cs, KVM_CAP_SW_TLB, 0, (uintptr_t)&cfg);
231 if (ret < 0) {
232 fprintf(stderr, "%s: couldn't enable KVM_CAP_SW_TLB: %s\n",
233 __func__, strerror(-ret));
234 return ret;
237 env->kvm_sw_tlb = true;
238 return 0;
242 #if defined(TARGET_PPC64)
243 static void kvm_get_smmu_info(struct kvm_ppc_smmu_info *info, Error **errp)
245 int ret;
247 assert(kvm_state != NULL);
249 if (!kvm_check_extension(kvm_state, KVM_CAP_PPC_GET_SMMU_INFO)) {
250 error_setg(errp, "KVM doesn't expose the MMU features it supports");
251 error_append_hint(errp, "Consider switching to a newer KVM\n");
252 return;
255 ret = kvm_vm_ioctl(kvm_state, KVM_PPC_GET_SMMU_INFO, info);
256 if (ret == 0) {
257 return;
260 error_setg_errno(errp, -ret,
261 "KVM failed to provide the MMU features it supports");
264 struct ppc_radix_page_info *kvm_get_radix_page_info(void)
266 KVMState *s = KVM_STATE(current_accel());
267 struct ppc_radix_page_info *radix_page_info;
268 struct kvm_ppc_rmmu_info rmmu_info;
269 int i;
271 if (!kvm_check_extension(s, KVM_CAP_PPC_MMU_RADIX)) {
272 return NULL;
274 if (kvm_vm_ioctl(s, KVM_PPC_GET_RMMU_INFO, &rmmu_info)) {
275 return NULL;
277 radix_page_info = g_malloc0(sizeof(*radix_page_info));
278 radix_page_info->count = 0;
279 for (i = 0; i < PPC_PAGE_SIZES_MAX_SZ; i++) {
280 if (rmmu_info.ap_encodings[i]) {
281 radix_page_info->entries[i] = rmmu_info.ap_encodings[i];
282 radix_page_info->count++;
285 return radix_page_info;
288 target_ulong kvmppc_configure_v3_mmu(PowerPCCPU *cpu,
289 bool radix, bool gtse,
290 uint64_t proc_tbl)
292 CPUState *cs = CPU(cpu);
293 int ret;
294 uint64_t flags = 0;
295 struct kvm_ppc_mmuv3_cfg cfg = {
296 .process_table = proc_tbl,
299 if (radix) {
300 flags |= KVM_PPC_MMUV3_RADIX;
302 if (gtse) {
303 flags |= KVM_PPC_MMUV3_GTSE;
305 cfg.flags = flags;
306 ret = kvm_vm_ioctl(cs->kvm_state, KVM_PPC_CONFIGURE_V3_MMU, &cfg);
307 switch (ret) {
308 case 0:
309 return H_SUCCESS;
310 case -EINVAL:
311 return H_PARAMETER;
312 case -ENODEV:
313 return H_NOT_AVAILABLE;
314 default:
315 return H_HARDWARE;
319 bool kvmppc_hpt_needs_host_contiguous_pages(void)
321 static struct kvm_ppc_smmu_info smmu_info;
323 if (!kvm_enabled()) {
324 return false;
327 kvm_get_smmu_info(&smmu_info, &error_fatal);
328 return !!(smmu_info.flags & KVM_PPC_PAGE_SIZES_REAL);
331 void kvm_check_mmu(PowerPCCPU *cpu, Error **errp)
333 struct kvm_ppc_smmu_info smmu_info;
334 int iq, ik, jq, jk;
335 Error *local_err = NULL;
337 /* For now, we only have anything to check on hash64 MMUs */
338 if (!cpu->hash64_opts || !kvm_enabled()) {
339 return;
342 kvm_get_smmu_info(&smmu_info, &local_err);
343 if (local_err) {
344 error_propagate(errp, local_err);
345 return;
348 if (ppc_hash64_has(cpu, PPC_HASH64_1TSEG)
349 && !(smmu_info.flags & KVM_PPC_1T_SEGMENTS)) {
350 error_setg(errp,
351 "KVM does not support 1TiB segments which guest expects");
352 return;
355 if (smmu_info.slb_size < cpu->hash64_opts->slb_size) {
356 error_setg(errp, "KVM only supports %u SLB entries, but guest needs %u",
357 smmu_info.slb_size, cpu->hash64_opts->slb_size);
358 return;
362 * Verify that every pagesize supported by the cpu model is
363 * supported by KVM with the same encodings
365 for (iq = 0; iq < ARRAY_SIZE(cpu->hash64_opts->sps); iq++) {
366 PPCHash64SegmentPageSizes *qsps = &cpu->hash64_opts->sps[iq];
367 struct kvm_ppc_one_seg_page_size *ksps;
369 for (ik = 0; ik < ARRAY_SIZE(smmu_info.sps); ik++) {
370 if (qsps->page_shift == smmu_info.sps[ik].page_shift) {
371 break;
374 if (ik >= ARRAY_SIZE(smmu_info.sps)) {
375 error_setg(errp, "KVM doesn't support for base page shift %u",
376 qsps->page_shift);
377 return;
380 ksps = &smmu_info.sps[ik];
381 if (ksps->slb_enc != qsps->slb_enc) {
382 error_setg(errp,
383 "KVM uses SLB encoding 0x%x for page shift %u, but guest expects 0x%x",
384 ksps->slb_enc, ksps->page_shift, qsps->slb_enc);
385 return;
388 for (jq = 0; jq < ARRAY_SIZE(qsps->enc); jq++) {
389 for (jk = 0; jk < ARRAY_SIZE(ksps->enc); jk++) {
390 if (qsps->enc[jq].page_shift == ksps->enc[jk].page_shift) {
391 break;
395 if (jk >= ARRAY_SIZE(ksps->enc)) {
396 error_setg(errp, "KVM doesn't support page shift %u/%u",
397 qsps->enc[jq].page_shift, qsps->page_shift);
398 return;
400 if (qsps->enc[jq].pte_enc != ksps->enc[jk].pte_enc) {
401 error_setg(errp,
402 "KVM uses PTE encoding 0x%x for page shift %u/%u, but guest expects 0x%x",
403 ksps->enc[jk].pte_enc, qsps->enc[jq].page_shift,
404 qsps->page_shift, qsps->enc[jq].pte_enc);
405 return;
410 if (ppc_hash64_has(cpu, PPC_HASH64_CI_LARGEPAGE)) {
412 * Mostly what guest pagesizes we can use are related to the
413 * host pages used to map guest RAM, which is handled in the
414 * platform code. Cache-Inhibited largepages (64k) however are
415 * used for I/O, so if they're mapped to the host at all it
416 * will be a normal mapping, not a special hugepage one used
417 * for RAM.
419 if (qemu_real_host_page_size < 0x10000) {
420 error_setg(errp,
421 "KVM can't supply 64kiB CI pages, which guest expects");
425 #endif /* !defined (TARGET_PPC64) */
427 unsigned long kvm_arch_vcpu_id(CPUState *cpu)
429 return POWERPC_CPU(cpu)->vcpu_id;
433 * e500 supports 2 h/w breakpoint and 2 watchpoint. book3s supports
434 * only 1 watchpoint, so array size of 4 is sufficient for now.
436 #define MAX_HW_BKPTS 4
438 static struct HWBreakpoint {
439 target_ulong addr;
440 int type;
441 } hw_debug_points[MAX_HW_BKPTS];
443 static CPUWatchpoint hw_watchpoint;
445 /* Default there is no breakpoint and watchpoint supported */
446 static int max_hw_breakpoint;
447 static int max_hw_watchpoint;
448 static int nb_hw_breakpoint;
449 static int nb_hw_watchpoint;
451 static void kvmppc_hw_debug_points_init(CPUPPCState *cenv)
453 if (cenv->excp_model == POWERPC_EXCP_BOOKE) {
454 max_hw_breakpoint = 2;
455 max_hw_watchpoint = 2;
458 if ((max_hw_breakpoint + max_hw_watchpoint) > MAX_HW_BKPTS) {
459 fprintf(stderr, "Error initializing h/w breakpoints\n");
460 return;
464 int kvm_arch_init_vcpu(CPUState *cs)
466 PowerPCCPU *cpu = POWERPC_CPU(cs);
467 CPUPPCState *cenv = &cpu->env;
468 int ret;
470 /* Synchronize sregs with kvm */
471 ret = kvm_arch_sync_sregs(cpu);
472 if (ret) {
473 if (ret == -EINVAL) {
474 error_report("Register sync failed... If you're using kvm-hv.ko,"
475 " only \"-cpu host\" is possible");
477 return ret;
480 switch (cenv->mmu_model) {
481 case POWERPC_MMU_BOOKE206:
482 /* This target supports access to KVM's guest TLB */
483 ret = kvm_booke206_tlb_init(cpu);
484 break;
485 case POWERPC_MMU_2_07:
486 if (!cap_htm && !kvmppc_is_pr(cs->kvm_state)) {
488 * KVM-HV has transactional memory on POWER8 also without
489 * the KVM_CAP_PPC_HTM extension, so enable it here
490 * instead as long as it's available to userspace on the
491 * host.
493 if (qemu_getauxval(AT_HWCAP2) & PPC_FEATURE2_HAS_HTM) {
494 cap_htm = true;
497 break;
498 default:
499 break;
502 kvm_get_one_reg(cs, KVM_REG_PPC_DEBUG_INST, &debug_inst_opcode);
503 kvmppc_hw_debug_points_init(cenv);
505 return ret;
508 int kvm_arch_destroy_vcpu(CPUState *cs)
510 return 0;
513 static void kvm_sw_tlb_put(PowerPCCPU *cpu)
515 CPUPPCState *env = &cpu->env;
516 CPUState *cs = CPU(cpu);
517 struct kvm_dirty_tlb dirty_tlb;
518 unsigned char *bitmap;
519 int ret;
521 if (!env->kvm_sw_tlb) {
522 return;
525 bitmap = g_malloc((env->nb_tlb + 7) / 8);
526 memset(bitmap, 0xFF, (env->nb_tlb + 7) / 8);
528 dirty_tlb.bitmap = (uintptr_t)bitmap;
529 dirty_tlb.num_dirty = env->nb_tlb;
531 ret = kvm_vcpu_ioctl(cs, KVM_DIRTY_TLB, &dirty_tlb);
532 if (ret) {
533 fprintf(stderr, "%s: KVM_DIRTY_TLB: %s\n",
534 __func__, strerror(-ret));
537 g_free(bitmap);
540 static void kvm_get_one_spr(CPUState *cs, uint64_t id, int spr)
542 PowerPCCPU *cpu = POWERPC_CPU(cs);
543 CPUPPCState *env = &cpu->env;
544 union {
545 uint32_t u32;
546 uint64_t u64;
547 } val;
548 struct kvm_one_reg reg = {
549 .id = id,
550 .addr = (uintptr_t) &val,
552 int ret;
554 ret = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, &reg);
555 if (ret != 0) {
556 trace_kvm_failed_spr_get(spr, strerror(errno));
557 } else {
558 switch (id & KVM_REG_SIZE_MASK) {
559 case KVM_REG_SIZE_U32:
560 env->spr[spr] = val.u32;
561 break;
563 case KVM_REG_SIZE_U64:
564 env->spr[spr] = val.u64;
565 break;
567 default:
568 /* Don't handle this size yet */
569 abort();
574 static void kvm_put_one_spr(CPUState *cs, uint64_t id, int spr)
576 PowerPCCPU *cpu = POWERPC_CPU(cs);
577 CPUPPCState *env = &cpu->env;
578 union {
579 uint32_t u32;
580 uint64_t u64;
581 } val;
582 struct kvm_one_reg reg = {
583 .id = id,
584 .addr = (uintptr_t) &val,
586 int ret;
588 switch (id & KVM_REG_SIZE_MASK) {
589 case KVM_REG_SIZE_U32:
590 val.u32 = env->spr[spr];
591 break;
593 case KVM_REG_SIZE_U64:
594 val.u64 = env->spr[spr];
595 break;
597 default:
598 /* Don't handle this size yet */
599 abort();
602 ret = kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, &reg);
603 if (ret != 0) {
604 trace_kvm_failed_spr_set(spr, strerror(errno));
608 static int kvm_put_fp(CPUState *cs)
610 PowerPCCPU *cpu = POWERPC_CPU(cs);
611 CPUPPCState *env = &cpu->env;
612 struct kvm_one_reg reg;
613 int i;
614 int ret;
616 if (env->insns_flags & PPC_FLOAT) {
617 uint64_t fpscr = env->fpscr;
618 bool vsx = !!(env->insns_flags2 & PPC2_VSX);
620 reg.id = KVM_REG_PPC_FPSCR;
621 reg.addr = (uintptr_t)&fpscr;
622 ret = kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, &reg);
623 if (ret < 0) {
624 trace_kvm_failed_fpscr_set(strerror(errno));
625 return ret;
628 for (i = 0; i < 32; i++) {
629 uint64_t vsr[2];
630 uint64_t *fpr = cpu_fpr_ptr(&cpu->env, i);
631 uint64_t *vsrl = cpu_vsrl_ptr(&cpu->env, i);
633 #ifdef HOST_WORDS_BIGENDIAN
634 vsr[0] = float64_val(*fpr);
635 vsr[1] = *vsrl;
636 #else
637 vsr[0] = *vsrl;
638 vsr[1] = float64_val(*fpr);
639 #endif
640 reg.addr = (uintptr_t) &vsr;
641 reg.id = vsx ? KVM_REG_PPC_VSR(i) : KVM_REG_PPC_FPR(i);
643 ret = kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, &reg);
644 if (ret < 0) {
645 trace_kvm_failed_fp_set(vsx ? "VSR" : "FPR", i,
646 strerror(errno));
647 return ret;
652 if (env->insns_flags & PPC_ALTIVEC) {
653 reg.id = KVM_REG_PPC_VSCR;
654 reg.addr = (uintptr_t)&env->vscr;
655 ret = kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, &reg);
656 if (ret < 0) {
657 trace_kvm_failed_vscr_set(strerror(errno));
658 return ret;
661 for (i = 0; i < 32; i++) {
662 reg.id = KVM_REG_PPC_VR(i);
663 reg.addr = (uintptr_t)cpu_avr_ptr(env, i);
664 ret = kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, &reg);
665 if (ret < 0) {
666 trace_kvm_failed_vr_set(i, strerror(errno));
667 return ret;
672 return 0;
675 static int kvm_get_fp(CPUState *cs)
677 PowerPCCPU *cpu = POWERPC_CPU(cs);
678 CPUPPCState *env = &cpu->env;
679 struct kvm_one_reg reg;
680 int i;
681 int ret;
683 if (env->insns_flags & PPC_FLOAT) {
684 uint64_t fpscr;
685 bool vsx = !!(env->insns_flags2 & PPC2_VSX);
687 reg.id = KVM_REG_PPC_FPSCR;
688 reg.addr = (uintptr_t)&fpscr;
689 ret = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, &reg);
690 if (ret < 0) {
691 trace_kvm_failed_fpscr_get(strerror(errno));
692 return ret;
693 } else {
694 env->fpscr = fpscr;
697 for (i = 0; i < 32; i++) {
698 uint64_t vsr[2];
699 uint64_t *fpr = cpu_fpr_ptr(&cpu->env, i);
700 uint64_t *vsrl = cpu_vsrl_ptr(&cpu->env, i);
702 reg.addr = (uintptr_t) &vsr;
703 reg.id = vsx ? KVM_REG_PPC_VSR(i) : KVM_REG_PPC_FPR(i);
705 ret = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, &reg);
706 if (ret < 0) {
707 trace_kvm_failed_fp_get(vsx ? "VSR" : "FPR", i,
708 strerror(errno));
709 return ret;
710 } else {
711 #ifdef HOST_WORDS_BIGENDIAN
712 *fpr = vsr[0];
713 if (vsx) {
714 *vsrl = vsr[1];
716 #else
717 *fpr = vsr[1];
718 if (vsx) {
719 *vsrl = vsr[0];
721 #endif
726 if (env->insns_flags & PPC_ALTIVEC) {
727 reg.id = KVM_REG_PPC_VSCR;
728 reg.addr = (uintptr_t)&env->vscr;
729 ret = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, &reg);
730 if (ret < 0) {
731 trace_kvm_failed_vscr_get(strerror(errno));
732 return ret;
735 for (i = 0; i < 32; i++) {
736 reg.id = KVM_REG_PPC_VR(i);
737 reg.addr = (uintptr_t)cpu_avr_ptr(env, i);
738 ret = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, &reg);
739 if (ret < 0) {
740 trace_kvm_failed_vr_get(i, strerror(errno));
741 return ret;
746 return 0;
749 #if defined(TARGET_PPC64)
750 static int kvm_get_vpa(CPUState *cs)
752 PowerPCCPU *cpu = POWERPC_CPU(cs);
753 SpaprCpuState *spapr_cpu = spapr_cpu_state(cpu);
754 struct kvm_one_reg reg;
755 int ret;
757 reg.id = KVM_REG_PPC_VPA_ADDR;
758 reg.addr = (uintptr_t)&spapr_cpu->vpa_addr;
759 ret = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, &reg);
760 if (ret < 0) {
761 trace_kvm_failed_vpa_addr_get(strerror(errno));
762 return ret;
765 assert((uintptr_t)&spapr_cpu->slb_shadow_size
766 == ((uintptr_t)&spapr_cpu->slb_shadow_addr + 8));
767 reg.id = KVM_REG_PPC_VPA_SLB;
768 reg.addr = (uintptr_t)&spapr_cpu->slb_shadow_addr;
769 ret = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, &reg);
770 if (ret < 0) {
771 trace_kvm_failed_slb_get(strerror(errno));
772 return ret;
775 assert((uintptr_t)&spapr_cpu->dtl_size
776 == ((uintptr_t)&spapr_cpu->dtl_addr + 8));
777 reg.id = KVM_REG_PPC_VPA_DTL;
778 reg.addr = (uintptr_t)&spapr_cpu->dtl_addr;
779 ret = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, &reg);
780 if (ret < 0) {
781 trace_kvm_failed_dtl_get(strerror(errno));
782 return ret;
785 return 0;
788 static int kvm_put_vpa(CPUState *cs)
790 PowerPCCPU *cpu = POWERPC_CPU(cs);
791 SpaprCpuState *spapr_cpu = spapr_cpu_state(cpu);
792 struct kvm_one_reg reg;
793 int ret;
796 * SLB shadow or DTL can't be registered unless a master VPA is
797 * registered. That means when restoring state, if a VPA *is*
798 * registered, we need to set that up first. If not, we need to
799 * deregister the others before deregistering the master VPA
801 assert(spapr_cpu->vpa_addr
802 || !(spapr_cpu->slb_shadow_addr || spapr_cpu->dtl_addr));
804 if (spapr_cpu->vpa_addr) {
805 reg.id = KVM_REG_PPC_VPA_ADDR;
806 reg.addr = (uintptr_t)&spapr_cpu->vpa_addr;
807 ret = kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, &reg);
808 if (ret < 0) {
809 trace_kvm_failed_vpa_addr_set(strerror(errno));
810 return ret;
814 assert((uintptr_t)&spapr_cpu->slb_shadow_size
815 == ((uintptr_t)&spapr_cpu->slb_shadow_addr + 8));
816 reg.id = KVM_REG_PPC_VPA_SLB;
817 reg.addr = (uintptr_t)&spapr_cpu->slb_shadow_addr;
818 ret = kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, &reg);
819 if (ret < 0) {
820 trace_kvm_failed_slb_set(strerror(errno));
821 return ret;
824 assert((uintptr_t)&spapr_cpu->dtl_size
825 == ((uintptr_t)&spapr_cpu->dtl_addr + 8));
826 reg.id = KVM_REG_PPC_VPA_DTL;
827 reg.addr = (uintptr_t)&spapr_cpu->dtl_addr;
828 ret = kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, &reg);
829 if (ret < 0) {
830 trace_kvm_failed_dtl_set(strerror(errno));
831 return ret;
834 if (!spapr_cpu->vpa_addr) {
835 reg.id = KVM_REG_PPC_VPA_ADDR;
836 reg.addr = (uintptr_t)&spapr_cpu->vpa_addr;
837 ret = kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, &reg);
838 if (ret < 0) {
839 trace_kvm_failed_null_vpa_addr_set(strerror(errno));
840 return ret;
844 return 0;
846 #endif /* TARGET_PPC64 */
848 int kvmppc_put_books_sregs(PowerPCCPU *cpu)
850 CPUPPCState *env = &cpu->env;
851 struct kvm_sregs sregs;
852 int i;
854 sregs.pvr = env->spr[SPR_PVR];
856 if (cpu->vhyp) {
857 PPCVirtualHypervisorClass *vhc =
858 PPC_VIRTUAL_HYPERVISOR_GET_CLASS(cpu->vhyp);
859 sregs.u.s.sdr1 = vhc->encode_hpt_for_kvm_pr(cpu->vhyp);
860 } else {
861 sregs.u.s.sdr1 = env->spr[SPR_SDR1];
864 /* Sync SLB */
865 #ifdef TARGET_PPC64
866 for (i = 0; i < ARRAY_SIZE(env->slb); i++) {
867 sregs.u.s.ppc64.slb[i].slbe = env->slb[i].esid;
868 if (env->slb[i].esid & SLB_ESID_V) {
869 sregs.u.s.ppc64.slb[i].slbe |= i;
871 sregs.u.s.ppc64.slb[i].slbv = env->slb[i].vsid;
873 #endif
875 /* Sync SRs */
876 for (i = 0; i < 16; i++) {
877 sregs.u.s.ppc32.sr[i] = env->sr[i];
880 /* Sync BATs */
881 for (i = 0; i < 8; i++) {
882 /* Beware. We have to swap upper and lower bits here */
883 sregs.u.s.ppc32.dbat[i] = ((uint64_t)env->DBAT[0][i] << 32)
884 | env->DBAT[1][i];
885 sregs.u.s.ppc32.ibat[i] = ((uint64_t)env->IBAT[0][i] << 32)
886 | env->IBAT[1][i];
889 return kvm_vcpu_ioctl(CPU(cpu), KVM_SET_SREGS, &sregs);
892 int kvm_arch_put_registers(CPUState *cs, int level)
894 PowerPCCPU *cpu = POWERPC_CPU(cs);
895 CPUPPCState *env = &cpu->env;
896 struct kvm_regs regs;
897 int ret;
898 int i;
900 ret = kvm_vcpu_ioctl(cs, KVM_GET_REGS, &regs);
901 if (ret < 0) {
902 return ret;
905 regs.ctr = env->ctr;
906 regs.lr = env->lr;
907 regs.xer = cpu_read_xer(env);
908 regs.msr = env->msr;
909 regs.pc = env->nip;
911 regs.srr0 = env->spr[SPR_SRR0];
912 regs.srr1 = env->spr[SPR_SRR1];
914 regs.sprg0 = env->spr[SPR_SPRG0];
915 regs.sprg1 = env->spr[SPR_SPRG1];
916 regs.sprg2 = env->spr[SPR_SPRG2];
917 regs.sprg3 = env->spr[SPR_SPRG3];
918 regs.sprg4 = env->spr[SPR_SPRG4];
919 regs.sprg5 = env->spr[SPR_SPRG5];
920 regs.sprg6 = env->spr[SPR_SPRG6];
921 regs.sprg7 = env->spr[SPR_SPRG7];
923 regs.pid = env->spr[SPR_BOOKE_PID];
925 for (i = 0; i < 32; i++) {
926 regs.gpr[i] = env->gpr[i];
929 regs.cr = 0;
930 for (i = 0; i < 8; i++) {
931 regs.cr |= (env->crf[i] & 15) << (4 * (7 - i));
934 ret = kvm_vcpu_ioctl(cs, KVM_SET_REGS, &regs);
935 if (ret < 0) {
936 return ret;
939 kvm_put_fp(cs);
941 if (env->tlb_dirty) {
942 kvm_sw_tlb_put(cpu);
943 env->tlb_dirty = false;
946 if (cap_segstate && (level >= KVM_PUT_RESET_STATE)) {
947 ret = kvmppc_put_books_sregs(cpu);
948 if (ret < 0) {
949 return ret;
953 if (cap_hior && (level >= KVM_PUT_RESET_STATE)) {
954 kvm_put_one_spr(cs, KVM_REG_PPC_HIOR, SPR_HIOR);
957 if (cap_one_reg) {
958 int i;
961 * We deliberately ignore errors here, for kernels which have
962 * the ONE_REG calls, but don't support the specific
963 * registers, there's a reasonable chance things will still
964 * work, at least until we try to migrate.
966 for (i = 0; i < 1024; i++) {
967 uint64_t id = env->spr_cb[i].one_reg_id;
969 if (id != 0) {
970 kvm_put_one_spr(cs, id, i);
974 #ifdef TARGET_PPC64
975 if (msr_ts) {
976 for (i = 0; i < ARRAY_SIZE(env->tm_gpr); i++) {
977 kvm_set_one_reg(cs, KVM_REG_PPC_TM_GPR(i), &env->tm_gpr[i]);
979 for (i = 0; i < ARRAY_SIZE(env->tm_vsr); i++) {
980 kvm_set_one_reg(cs, KVM_REG_PPC_TM_VSR(i), &env->tm_vsr[i]);
982 kvm_set_one_reg(cs, KVM_REG_PPC_TM_CR, &env->tm_cr);
983 kvm_set_one_reg(cs, KVM_REG_PPC_TM_LR, &env->tm_lr);
984 kvm_set_one_reg(cs, KVM_REG_PPC_TM_CTR, &env->tm_ctr);
985 kvm_set_one_reg(cs, KVM_REG_PPC_TM_FPSCR, &env->tm_fpscr);
986 kvm_set_one_reg(cs, KVM_REG_PPC_TM_AMR, &env->tm_amr);
987 kvm_set_one_reg(cs, KVM_REG_PPC_TM_PPR, &env->tm_ppr);
988 kvm_set_one_reg(cs, KVM_REG_PPC_TM_VRSAVE, &env->tm_vrsave);
989 kvm_set_one_reg(cs, KVM_REG_PPC_TM_VSCR, &env->tm_vscr);
990 kvm_set_one_reg(cs, KVM_REG_PPC_TM_DSCR, &env->tm_dscr);
991 kvm_set_one_reg(cs, KVM_REG_PPC_TM_TAR, &env->tm_tar);
994 if (cap_papr) {
995 if (kvm_put_vpa(cs) < 0) {
996 trace_kvm_failed_put_vpa();
1000 kvm_set_one_reg(cs, KVM_REG_PPC_TB_OFFSET, &env->tb_env->tb_offset);
1002 if (level > KVM_PUT_RUNTIME_STATE) {
1003 kvm_put_one_spr(cs, KVM_REG_PPC_DPDES, SPR_DPDES);
1005 #endif /* TARGET_PPC64 */
1008 return ret;
1011 static void kvm_sync_excp(CPUPPCState *env, int vector, int ivor)
1013 env->excp_vectors[vector] = env->spr[ivor] + env->spr[SPR_BOOKE_IVPR];
1016 static int kvmppc_get_booke_sregs(PowerPCCPU *cpu)
1018 CPUPPCState *env = &cpu->env;
1019 struct kvm_sregs sregs;
1020 int ret;
1022 ret = kvm_vcpu_ioctl(CPU(cpu), KVM_GET_SREGS, &sregs);
1023 if (ret < 0) {
1024 return ret;
1027 if (sregs.u.e.features & KVM_SREGS_E_BASE) {
1028 env->spr[SPR_BOOKE_CSRR0] = sregs.u.e.csrr0;
1029 env->spr[SPR_BOOKE_CSRR1] = sregs.u.e.csrr1;
1030 env->spr[SPR_BOOKE_ESR] = sregs.u.e.esr;
1031 env->spr[SPR_BOOKE_DEAR] = sregs.u.e.dear;
1032 env->spr[SPR_BOOKE_MCSR] = sregs.u.e.mcsr;
1033 env->spr[SPR_BOOKE_TSR] = sregs.u.e.tsr;
1034 env->spr[SPR_BOOKE_TCR] = sregs.u.e.tcr;
1035 env->spr[SPR_DECR] = sregs.u.e.dec;
1036 env->spr[SPR_TBL] = sregs.u.e.tb & 0xffffffff;
1037 env->spr[SPR_TBU] = sregs.u.e.tb >> 32;
1038 env->spr[SPR_VRSAVE] = sregs.u.e.vrsave;
1041 if (sregs.u.e.features & KVM_SREGS_E_ARCH206) {
1042 env->spr[SPR_BOOKE_PIR] = sregs.u.e.pir;
1043 env->spr[SPR_BOOKE_MCSRR0] = sregs.u.e.mcsrr0;
1044 env->spr[SPR_BOOKE_MCSRR1] = sregs.u.e.mcsrr1;
1045 env->spr[SPR_BOOKE_DECAR] = sregs.u.e.decar;
1046 env->spr[SPR_BOOKE_IVPR] = sregs.u.e.ivpr;
1049 if (sregs.u.e.features & KVM_SREGS_E_64) {
1050 env->spr[SPR_BOOKE_EPCR] = sregs.u.e.epcr;
1053 if (sregs.u.e.features & KVM_SREGS_E_SPRG8) {
1054 env->spr[SPR_BOOKE_SPRG8] = sregs.u.e.sprg8;
1057 if (sregs.u.e.features & KVM_SREGS_E_IVOR) {
1058 env->spr[SPR_BOOKE_IVOR0] = sregs.u.e.ivor_low[0];
1059 kvm_sync_excp(env, POWERPC_EXCP_CRITICAL, SPR_BOOKE_IVOR0);
1060 env->spr[SPR_BOOKE_IVOR1] = sregs.u.e.ivor_low[1];
1061 kvm_sync_excp(env, POWERPC_EXCP_MCHECK, SPR_BOOKE_IVOR1);
1062 env->spr[SPR_BOOKE_IVOR2] = sregs.u.e.ivor_low[2];
1063 kvm_sync_excp(env, POWERPC_EXCP_DSI, SPR_BOOKE_IVOR2);
1064 env->spr[SPR_BOOKE_IVOR3] = sregs.u.e.ivor_low[3];
1065 kvm_sync_excp(env, POWERPC_EXCP_ISI, SPR_BOOKE_IVOR3);
1066 env->spr[SPR_BOOKE_IVOR4] = sregs.u.e.ivor_low[4];
1067 kvm_sync_excp(env, POWERPC_EXCP_EXTERNAL, SPR_BOOKE_IVOR4);
1068 env->spr[SPR_BOOKE_IVOR5] = sregs.u.e.ivor_low[5];
1069 kvm_sync_excp(env, POWERPC_EXCP_ALIGN, SPR_BOOKE_IVOR5);
1070 env->spr[SPR_BOOKE_IVOR6] = sregs.u.e.ivor_low[6];
1071 kvm_sync_excp(env, POWERPC_EXCP_PROGRAM, SPR_BOOKE_IVOR6);
1072 env->spr[SPR_BOOKE_IVOR7] = sregs.u.e.ivor_low[7];
1073 kvm_sync_excp(env, POWERPC_EXCP_FPU, SPR_BOOKE_IVOR7);
1074 env->spr[SPR_BOOKE_IVOR8] = sregs.u.e.ivor_low[8];
1075 kvm_sync_excp(env, POWERPC_EXCP_SYSCALL, SPR_BOOKE_IVOR8);
1076 env->spr[SPR_BOOKE_IVOR9] = sregs.u.e.ivor_low[9];
1077 kvm_sync_excp(env, POWERPC_EXCP_APU, SPR_BOOKE_IVOR9);
1078 env->spr[SPR_BOOKE_IVOR10] = sregs.u.e.ivor_low[10];
1079 kvm_sync_excp(env, POWERPC_EXCP_DECR, SPR_BOOKE_IVOR10);
1080 env->spr[SPR_BOOKE_IVOR11] = sregs.u.e.ivor_low[11];
1081 kvm_sync_excp(env, POWERPC_EXCP_FIT, SPR_BOOKE_IVOR11);
1082 env->spr[SPR_BOOKE_IVOR12] = sregs.u.e.ivor_low[12];
1083 kvm_sync_excp(env, POWERPC_EXCP_WDT, SPR_BOOKE_IVOR12);
1084 env->spr[SPR_BOOKE_IVOR13] = sregs.u.e.ivor_low[13];
1085 kvm_sync_excp(env, POWERPC_EXCP_DTLB, SPR_BOOKE_IVOR13);
1086 env->spr[SPR_BOOKE_IVOR14] = sregs.u.e.ivor_low[14];
1087 kvm_sync_excp(env, POWERPC_EXCP_ITLB, SPR_BOOKE_IVOR14);
1088 env->spr[SPR_BOOKE_IVOR15] = sregs.u.e.ivor_low[15];
1089 kvm_sync_excp(env, POWERPC_EXCP_DEBUG, SPR_BOOKE_IVOR15);
1091 if (sregs.u.e.features & KVM_SREGS_E_SPE) {
1092 env->spr[SPR_BOOKE_IVOR32] = sregs.u.e.ivor_high[0];
1093 kvm_sync_excp(env, POWERPC_EXCP_SPEU, SPR_BOOKE_IVOR32);
1094 env->spr[SPR_BOOKE_IVOR33] = sregs.u.e.ivor_high[1];
1095 kvm_sync_excp(env, POWERPC_EXCP_EFPDI, SPR_BOOKE_IVOR33);
1096 env->spr[SPR_BOOKE_IVOR34] = sregs.u.e.ivor_high[2];
1097 kvm_sync_excp(env, POWERPC_EXCP_EFPRI, SPR_BOOKE_IVOR34);
1100 if (sregs.u.e.features & KVM_SREGS_E_PM) {
1101 env->spr[SPR_BOOKE_IVOR35] = sregs.u.e.ivor_high[3];
1102 kvm_sync_excp(env, POWERPC_EXCP_EPERFM, SPR_BOOKE_IVOR35);
1105 if (sregs.u.e.features & KVM_SREGS_E_PC) {
1106 env->spr[SPR_BOOKE_IVOR36] = sregs.u.e.ivor_high[4];
1107 kvm_sync_excp(env, POWERPC_EXCP_DOORI, SPR_BOOKE_IVOR36);
1108 env->spr[SPR_BOOKE_IVOR37] = sregs.u.e.ivor_high[5];
1109 kvm_sync_excp(env, POWERPC_EXCP_DOORCI, SPR_BOOKE_IVOR37);
1113 if (sregs.u.e.features & KVM_SREGS_E_ARCH206_MMU) {
1114 env->spr[SPR_BOOKE_MAS0] = sregs.u.e.mas0;
1115 env->spr[SPR_BOOKE_MAS1] = sregs.u.e.mas1;
1116 env->spr[SPR_BOOKE_MAS2] = sregs.u.e.mas2;
1117 env->spr[SPR_BOOKE_MAS3] = sregs.u.e.mas7_3 & 0xffffffff;
1118 env->spr[SPR_BOOKE_MAS4] = sregs.u.e.mas4;
1119 env->spr[SPR_BOOKE_MAS6] = sregs.u.e.mas6;
1120 env->spr[SPR_BOOKE_MAS7] = sregs.u.e.mas7_3 >> 32;
1121 env->spr[SPR_MMUCFG] = sregs.u.e.mmucfg;
1122 env->spr[SPR_BOOKE_TLB0CFG] = sregs.u.e.tlbcfg[0];
1123 env->spr[SPR_BOOKE_TLB1CFG] = sregs.u.e.tlbcfg[1];
1126 if (sregs.u.e.features & KVM_SREGS_EXP) {
1127 env->spr[SPR_BOOKE_EPR] = sregs.u.e.epr;
1130 if (sregs.u.e.features & KVM_SREGS_E_PD) {
1131 env->spr[SPR_BOOKE_EPLC] = sregs.u.e.eplc;
1132 env->spr[SPR_BOOKE_EPSC] = sregs.u.e.epsc;
1135 if (sregs.u.e.impl_id == KVM_SREGS_E_IMPL_FSL) {
1136 env->spr[SPR_E500_SVR] = sregs.u.e.impl.fsl.svr;
1137 env->spr[SPR_Exxx_MCAR] = sregs.u.e.impl.fsl.mcar;
1138 env->spr[SPR_HID0] = sregs.u.e.impl.fsl.hid0;
1140 if (sregs.u.e.impl.fsl.features & KVM_SREGS_E_FSL_PIDn) {
1141 env->spr[SPR_BOOKE_PID1] = sregs.u.e.impl.fsl.pid1;
1142 env->spr[SPR_BOOKE_PID2] = sregs.u.e.impl.fsl.pid2;
1146 return 0;
1149 static int kvmppc_get_books_sregs(PowerPCCPU *cpu)
1151 CPUPPCState *env = &cpu->env;
1152 struct kvm_sregs sregs;
1153 int ret;
1154 int i;
1156 ret = kvm_vcpu_ioctl(CPU(cpu), KVM_GET_SREGS, &sregs);
1157 if (ret < 0) {
1158 return ret;
1161 if (!cpu->vhyp) {
1162 ppc_store_sdr1(env, sregs.u.s.sdr1);
1165 /* Sync SLB */
1166 #ifdef TARGET_PPC64
1168 * The packed SLB array we get from KVM_GET_SREGS only contains
1169 * information about valid entries. So we flush our internal copy
1170 * to get rid of stale ones, then put all valid SLB entries back
1171 * in.
1173 memset(env->slb, 0, sizeof(env->slb));
1174 for (i = 0; i < ARRAY_SIZE(env->slb); i++) {
1175 target_ulong rb = sregs.u.s.ppc64.slb[i].slbe;
1176 target_ulong rs = sregs.u.s.ppc64.slb[i].slbv;
1178 * Only restore valid entries
1180 if (rb & SLB_ESID_V) {
1181 ppc_store_slb(cpu, rb & 0xfff, rb & ~0xfffULL, rs);
1184 #endif
1186 /* Sync SRs */
1187 for (i = 0; i < 16; i++) {
1188 env->sr[i] = sregs.u.s.ppc32.sr[i];
1191 /* Sync BATs */
1192 for (i = 0; i < 8; i++) {
1193 env->DBAT[0][i] = sregs.u.s.ppc32.dbat[i] & 0xffffffff;
1194 env->DBAT[1][i] = sregs.u.s.ppc32.dbat[i] >> 32;
1195 env->IBAT[0][i] = sregs.u.s.ppc32.ibat[i] & 0xffffffff;
1196 env->IBAT[1][i] = sregs.u.s.ppc32.ibat[i] >> 32;
1199 return 0;
1202 int kvm_arch_get_registers(CPUState *cs)
1204 PowerPCCPU *cpu = POWERPC_CPU(cs);
1205 CPUPPCState *env = &cpu->env;
1206 struct kvm_regs regs;
1207 uint32_t cr;
1208 int i, ret;
1210 ret = kvm_vcpu_ioctl(cs, KVM_GET_REGS, &regs);
1211 if (ret < 0) {
1212 return ret;
1215 cr = regs.cr;
1216 for (i = 7; i >= 0; i--) {
1217 env->crf[i] = cr & 15;
1218 cr >>= 4;
1221 env->ctr = regs.ctr;
1222 env->lr = regs.lr;
1223 cpu_write_xer(env, regs.xer);
1224 env->msr = regs.msr;
1225 env->nip = regs.pc;
1227 env->spr[SPR_SRR0] = regs.srr0;
1228 env->spr[SPR_SRR1] = regs.srr1;
1230 env->spr[SPR_SPRG0] = regs.sprg0;
1231 env->spr[SPR_SPRG1] = regs.sprg1;
1232 env->spr[SPR_SPRG2] = regs.sprg2;
1233 env->spr[SPR_SPRG3] = regs.sprg3;
1234 env->spr[SPR_SPRG4] = regs.sprg4;
1235 env->spr[SPR_SPRG5] = regs.sprg5;
1236 env->spr[SPR_SPRG6] = regs.sprg6;
1237 env->spr[SPR_SPRG7] = regs.sprg7;
1239 env->spr[SPR_BOOKE_PID] = regs.pid;
1241 for (i = 0; i < 32; i++) {
1242 env->gpr[i] = regs.gpr[i];
1245 kvm_get_fp(cs);
1247 if (cap_booke_sregs) {
1248 ret = kvmppc_get_booke_sregs(cpu);
1249 if (ret < 0) {
1250 return ret;
1254 if (cap_segstate) {
1255 ret = kvmppc_get_books_sregs(cpu);
1256 if (ret < 0) {
1257 return ret;
1261 if (cap_hior) {
1262 kvm_get_one_spr(cs, KVM_REG_PPC_HIOR, SPR_HIOR);
1265 if (cap_one_reg) {
1266 int i;
1269 * We deliberately ignore errors here, for kernels which have
1270 * the ONE_REG calls, but don't support the specific
1271 * registers, there's a reasonable chance things will still
1272 * work, at least until we try to migrate.
1274 for (i = 0; i < 1024; i++) {
1275 uint64_t id = env->spr_cb[i].one_reg_id;
1277 if (id != 0) {
1278 kvm_get_one_spr(cs, id, i);
1282 #ifdef TARGET_PPC64
1283 if (msr_ts) {
1284 for (i = 0; i < ARRAY_SIZE(env->tm_gpr); i++) {
1285 kvm_get_one_reg(cs, KVM_REG_PPC_TM_GPR(i), &env->tm_gpr[i]);
1287 for (i = 0; i < ARRAY_SIZE(env->tm_vsr); i++) {
1288 kvm_get_one_reg(cs, KVM_REG_PPC_TM_VSR(i), &env->tm_vsr[i]);
1290 kvm_get_one_reg(cs, KVM_REG_PPC_TM_CR, &env->tm_cr);
1291 kvm_get_one_reg(cs, KVM_REG_PPC_TM_LR, &env->tm_lr);
1292 kvm_get_one_reg(cs, KVM_REG_PPC_TM_CTR, &env->tm_ctr);
1293 kvm_get_one_reg(cs, KVM_REG_PPC_TM_FPSCR, &env->tm_fpscr);
1294 kvm_get_one_reg(cs, KVM_REG_PPC_TM_AMR, &env->tm_amr);
1295 kvm_get_one_reg(cs, KVM_REG_PPC_TM_PPR, &env->tm_ppr);
1296 kvm_get_one_reg(cs, KVM_REG_PPC_TM_VRSAVE, &env->tm_vrsave);
1297 kvm_get_one_reg(cs, KVM_REG_PPC_TM_VSCR, &env->tm_vscr);
1298 kvm_get_one_reg(cs, KVM_REG_PPC_TM_DSCR, &env->tm_dscr);
1299 kvm_get_one_reg(cs, KVM_REG_PPC_TM_TAR, &env->tm_tar);
1302 if (cap_papr) {
1303 if (kvm_get_vpa(cs) < 0) {
1304 trace_kvm_failed_get_vpa();
1308 kvm_get_one_reg(cs, KVM_REG_PPC_TB_OFFSET, &env->tb_env->tb_offset);
1309 kvm_get_one_spr(cs, KVM_REG_PPC_DPDES, SPR_DPDES);
1310 #endif
1313 return 0;
1316 int kvmppc_set_interrupt(PowerPCCPU *cpu, int irq, int level)
1318 unsigned virq = level ? KVM_INTERRUPT_SET_LEVEL : KVM_INTERRUPT_UNSET;
1320 if (irq != PPC_INTERRUPT_EXT) {
1321 return 0;
1324 if (!kvm_enabled() || !cap_interrupt_unset) {
1325 return 0;
1328 kvm_vcpu_ioctl(CPU(cpu), KVM_INTERRUPT, &virq);
1330 return 0;
1333 void kvm_arch_pre_run(CPUState *cs, struct kvm_run *run)
1335 return;
1338 MemTxAttrs kvm_arch_post_run(CPUState *cs, struct kvm_run *run)
1340 return MEMTXATTRS_UNSPECIFIED;
1343 int kvm_arch_process_async_events(CPUState *cs)
1345 return cs->halted;
1348 static int kvmppc_handle_halt(PowerPCCPU *cpu)
1350 CPUState *cs = CPU(cpu);
1351 CPUPPCState *env = &cpu->env;
1353 if (!(cs->interrupt_request & CPU_INTERRUPT_HARD) && (msr_ee)) {
1354 cs->halted = 1;
1355 cs->exception_index = EXCP_HLT;
1358 return 0;
1361 /* map dcr access to existing qemu dcr emulation */
1362 static int kvmppc_handle_dcr_read(CPUPPCState *env,
1363 uint32_t dcrn, uint32_t *data)
1365 if (ppc_dcr_read(env->dcr_env, dcrn, data) < 0) {
1366 fprintf(stderr, "Read to unhandled DCR (0x%x)\n", dcrn);
1369 return 0;
1372 static int kvmppc_handle_dcr_write(CPUPPCState *env,
1373 uint32_t dcrn, uint32_t data)
1375 if (ppc_dcr_write(env->dcr_env, dcrn, data) < 0) {
1376 fprintf(stderr, "Write to unhandled DCR (0x%x)\n", dcrn);
1379 return 0;
1382 int kvm_arch_insert_sw_breakpoint(CPUState *cs, struct kvm_sw_breakpoint *bp)
1384 /* Mixed endian case is not handled */
1385 uint32_t sc = debug_inst_opcode;
1387 if (cpu_memory_rw_debug(cs, bp->pc, (uint8_t *)&bp->saved_insn,
1388 sizeof(sc), 0) ||
1389 cpu_memory_rw_debug(cs, bp->pc, (uint8_t *)&sc, sizeof(sc), 1)) {
1390 return -EINVAL;
1393 return 0;
1396 int kvm_arch_remove_sw_breakpoint(CPUState *cs, struct kvm_sw_breakpoint *bp)
1398 uint32_t sc;
1400 if (cpu_memory_rw_debug(cs, bp->pc, (uint8_t *)&sc, sizeof(sc), 0) ||
1401 sc != debug_inst_opcode ||
1402 cpu_memory_rw_debug(cs, bp->pc, (uint8_t *)&bp->saved_insn,
1403 sizeof(sc), 1)) {
1404 return -EINVAL;
1407 return 0;
1410 static int find_hw_breakpoint(target_ulong addr, int type)
1412 int n;
1414 assert((nb_hw_breakpoint + nb_hw_watchpoint)
1415 <= ARRAY_SIZE(hw_debug_points));
1417 for (n = 0; n < nb_hw_breakpoint + nb_hw_watchpoint; n++) {
1418 if (hw_debug_points[n].addr == addr &&
1419 hw_debug_points[n].type == type) {
1420 return n;
1424 return -1;
1427 static int find_hw_watchpoint(target_ulong addr, int *flag)
1429 int n;
1431 n = find_hw_breakpoint(addr, GDB_WATCHPOINT_ACCESS);
1432 if (n >= 0) {
1433 *flag = BP_MEM_ACCESS;
1434 return n;
1437 n = find_hw_breakpoint(addr, GDB_WATCHPOINT_WRITE);
1438 if (n >= 0) {
1439 *flag = BP_MEM_WRITE;
1440 return n;
1443 n = find_hw_breakpoint(addr, GDB_WATCHPOINT_READ);
1444 if (n >= 0) {
1445 *flag = BP_MEM_READ;
1446 return n;
1449 return -1;
1452 int kvm_arch_insert_hw_breakpoint(target_ulong addr,
1453 target_ulong len, int type)
1455 if ((nb_hw_breakpoint + nb_hw_watchpoint) >= ARRAY_SIZE(hw_debug_points)) {
1456 return -ENOBUFS;
1459 hw_debug_points[nb_hw_breakpoint + nb_hw_watchpoint].addr = addr;
1460 hw_debug_points[nb_hw_breakpoint + nb_hw_watchpoint].type = type;
1462 switch (type) {
1463 case GDB_BREAKPOINT_HW:
1464 if (nb_hw_breakpoint >= max_hw_breakpoint) {
1465 return -ENOBUFS;
1468 if (find_hw_breakpoint(addr, type) >= 0) {
1469 return -EEXIST;
1472 nb_hw_breakpoint++;
1473 break;
1475 case GDB_WATCHPOINT_WRITE:
1476 case GDB_WATCHPOINT_READ:
1477 case GDB_WATCHPOINT_ACCESS:
1478 if (nb_hw_watchpoint >= max_hw_watchpoint) {
1479 return -ENOBUFS;
1482 if (find_hw_breakpoint(addr, type) >= 0) {
1483 return -EEXIST;
1486 nb_hw_watchpoint++;
1487 break;
1489 default:
1490 return -ENOSYS;
1493 return 0;
1496 int kvm_arch_remove_hw_breakpoint(target_ulong addr,
1497 target_ulong len, int type)
1499 int n;
1501 n = find_hw_breakpoint(addr, type);
1502 if (n < 0) {
1503 return -ENOENT;
1506 switch (type) {
1507 case GDB_BREAKPOINT_HW:
1508 nb_hw_breakpoint--;
1509 break;
1511 case GDB_WATCHPOINT_WRITE:
1512 case GDB_WATCHPOINT_READ:
1513 case GDB_WATCHPOINT_ACCESS:
1514 nb_hw_watchpoint--;
1515 break;
1517 default:
1518 return -ENOSYS;
1520 hw_debug_points[n] = hw_debug_points[nb_hw_breakpoint + nb_hw_watchpoint];
1522 return 0;
1525 void kvm_arch_remove_all_hw_breakpoints(void)
1527 nb_hw_breakpoint = nb_hw_watchpoint = 0;
1530 void kvm_arch_update_guest_debug(CPUState *cs, struct kvm_guest_debug *dbg)
1532 int n;
1534 /* Software Breakpoint updates */
1535 if (kvm_sw_breakpoints_active(cs)) {
1536 dbg->control |= KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP;
1539 assert((nb_hw_breakpoint + nb_hw_watchpoint)
1540 <= ARRAY_SIZE(hw_debug_points));
1541 assert((nb_hw_breakpoint + nb_hw_watchpoint) <= ARRAY_SIZE(dbg->arch.bp));
1543 if (nb_hw_breakpoint + nb_hw_watchpoint > 0) {
1544 dbg->control |= KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_HW_BP;
1545 memset(dbg->arch.bp, 0, sizeof(dbg->arch.bp));
1546 for (n = 0; n < nb_hw_breakpoint + nb_hw_watchpoint; n++) {
1547 switch (hw_debug_points[n].type) {
1548 case GDB_BREAKPOINT_HW:
1549 dbg->arch.bp[n].type = KVMPPC_DEBUG_BREAKPOINT;
1550 break;
1551 case GDB_WATCHPOINT_WRITE:
1552 dbg->arch.bp[n].type = KVMPPC_DEBUG_WATCH_WRITE;
1553 break;
1554 case GDB_WATCHPOINT_READ:
1555 dbg->arch.bp[n].type = KVMPPC_DEBUG_WATCH_READ;
1556 break;
1557 case GDB_WATCHPOINT_ACCESS:
1558 dbg->arch.bp[n].type = KVMPPC_DEBUG_WATCH_WRITE |
1559 KVMPPC_DEBUG_WATCH_READ;
1560 break;
1561 default:
1562 cpu_abort(cs, "Unsupported breakpoint type\n");
1564 dbg->arch.bp[n].addr = hw_debug_points[n].addr;
1569 static int kvm_handle_hw_breakpoint(CPUState *cs,
1570 struct kvm_debug_exit_arch *arch_info)
1572 int handle = DEBUG_RETURN_GUEST;
1573 int n;
1574 int flag = 0;
1576 if (nb_hw_breakpoint + nb_hw_watchpoint > 0) {
1577 if (arch_info->status & KVMPPC_DEBUG_BREAKPOINT) {
1578 n = find_hw_breakpoint(arch_info->address, GDB_BREAKPOINT_HW);
1579 if (n >= 0) {
1580 handle = DEBUG_RETURN_GDB;
1582 } else if (arch_info->status & (KVMPPC_DEBUG_WATCH_READ |
1583 KVMPPC_DEBUG_WATCH_WRITE)) {
1584 n = find_hw_watchpoint(arch_info->address, &flag);
1585 if (n >= 0) {
1586 handle = DEBUG_RETURN_GDB;
1587 cs->watchpoint_hit = &hw_watchpoint;
1588 hw_watchpoint.vaddr = hw_debug_points[n].addr;
1589 hw_watchpoint.flags = flag;
1593 return handle;
1596 static int kvm_handle_singlestep(void)
1598 return DEBUG_RETURN_GDB;
1601 static int kvm_handle_sw_breakpoint(void)
1603 return DEBUG_RETURN_GDB;
1606 static int kvm_handle_debug(PowerPCCPU *cpu, struct kvm_run *run)
1608 CPUState *cs = CPU(cpu);
1609 CPUPPCState *env = &cpu->env;
1610 struct kvm_debug_exit_arch *arch_info = &run->debug.arch;
1612 if (cs->singlestep_enabled) {
1613 return kvm_handle_singlestep();
1616 if (arch_info->status) {
1617 return kvm_handle_hw_breakpoint(cs, arch_info);
1620 if (kvm_find_sw_breakpoint(cs, arch_info->address)) {
1621 return kvm_handle_sw_breakpoint();
1625 * QEMU is not able to handle debug exception, so inject
1626 * program exception to guest;
1627 * Yes program exception NOT debug exception !!
1628 * When QEMU is using debug resources then debug exception must
1629 * be always set. To achieve this we set MSR_DE and also set
1630 * MSRP_DEP so guest cannot change MSR_DE.
1631 * When emulating debug resource for guest we want guest
1632 * to control MSR_DE (enable/disable debug interrupt on need).
1633 * Supporting both configurations are NOT possible.
1634 * So the result is that we cannot share debug resources
1635 * between QEMU and Guest on BOOKE architecture.
1636 * In the current design QEMU gets the priority over guest,
1637 * this means that if QEMU is using debug resources then guest
1638 * cannot use them;
1639 * For software breakpoint QEMU uses a privileged instruction;
1640 * So there cannot be any reason that we are here for guest
1641 * set debug exception, only possibility is guest executed a
1642 * privileged / illegal instruction and that's why we are
1643 * injecting a program interrupt.
1645 cpu_synchronize_state(cs);
1647 * env->nip is PC, so increment this by 4 to use
1648 * ppc_cpu_do_interrupt(), which set srr0 = env->nip - 4.
1650 env->nip += 4;
1651 cs->exception_index = POWERPC_EXCP_PROGRAM;
1652 env->error_code = POWERPC_EXCP_INVAL;
1653 ppc_cpu_do_interrupt(cs);
1655 return DEBUG_RETURN_GUEST;
1658 int kvm_arch_handle_exit(CPUState *cs, struct kvm_run *run)
1660 PowerPCCPU *cpu = POWERPC_CPU(cs);
1661 CPUPPCState *env = &cpu->env;
1662 int ret;
1664 qemu_mutex_lock_iothread();
1666 switch (run->exit_reason) {
1667 case KVM_EXIT_DCR:
1668 if (run->dcr.is_write) {
1669 trace_kvm_handle_dcr_write();
1670 ret = kvmppc_handle_dcr_write(env, run->dcr.dcrn, run->dcr.data);
1671 } else {
1672 trace_kvm_handle_dcr_read();
1673 ret = kvmppc_handle_dcr_read(env, run->dcr.dcrn, &run->dcr.data);
1675 break;
1676 case KVM_EXIT_HLT:
1677 trace_kvm_handle_halt();
1678 ret = kvmppc_handle_halt(cpu);
1679 break;
1680 #if defined(TARGET_PPC64)
1681 case KVM_EXIT_PAPR_HCALL:
1682 trace_kvm_handle_papr_hcall();
1683 run->papr_hcall.ret = spapr_hypercall(cpu,
1684 run->papr_hcall.nr,
1685 run->papr_hcall.args);
1686 ret = 0;
1687 break;
1688 #endif
1689 case KVM_EXIT_EPR:
1690 trace_kvm_handle_epr();
1691 run->epr.epr = ldl_phys(cs->as, env->mpic_iack);
1692 ret = 0;
1693 break;
1694 case KVM_EXIT_WATCHDOG:
1695 trace_kvm_handle_watchdog_expiry();
1696 watchdog_perform_action();
1697 ret = 0;
1698 break;
1700 case KVM_EXIT_DEBUG:
1701 trace_kvm_handle_debug_exception();
1702 if (kvm_handle_debug(cpu, run)) {
1703 ret = EXCP_DEBUG;
1704 break;
1706 /* re-enter, this exception was guest-internal */
1707 ret = 0;
1708 break;
1710 #if defined(TARGET_PPC64)
1711 case KVM_EXIT_NMI:
1712 trace_kvm_handle_nmi_exception();
1713 ret = kvm_handle_nmi(cpu, run);
1714 break;
1715 #endif
1717 default:
1718 fprintf(stderr, "KVM: unknown exit reason %d\n", run->exit_reason);
1719 ret = -1;
1720 break;
1723 qemu_mutex_unlock_iothread();
1724 return ret;
1727 int kvmppc_or_tsr_bits(PowerPCCPU *cpu, uint32_t tsr_bits)
1729 CPUState *cs = CPU(cpu);
1730 uint32_t bits = tsr_bits;
1731 struct kvm_one_reg reg = {
1732 .id = KVM_REG_PPC_OR_TSR,
1733 .addr = (uintptr_t) &bits,
1736 return kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, &reg);
1739 int kvmppc_clear_tsr_bits(PowerPCCPU *cpu, uint32_t tsr_bits)
1742 CPUState *cs = CPU(cpu);
1743 uint32_t bits = tsr_bits;
1744 struct kvm_one_reg reg = {
1745 .id = KVM_REG_PPC_CLEAR_TSR,
1746 .addr = (uintptr_t) &bits,
1749 return kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, &reg);
1752 int kvmppc_set_tcr(PowerPCCPU *cpu)
1754 CPUState *cs = CPU(cpu);
1755 CPUPPCState *env = &cpu->env;
1756 uint32_t tcr = env->spr[SPR_BOOKE_TCR];
1758 struct kvm_one_reg reg = {
1759 .id = KVM_REG_PPC_TCR,
1760 .addr = (uintptr_t) &tcr,
1763 return kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, &reg);
1766 int kvmppc_booke_watchdog_enable(PowerPCCPU *cpu)
1768 CPUState *cs = CPU(cpu);
1769 int ret;
1771 if (!kvm_enabled()) {
1772 return -1;
1775 if (!cap_ppc_watchdog) {
1776 printf("warning: KVM does not support watchdog");
1777 return -1;
1780 ret = kvm_vcpu_enable_cap(cs, KVM_CAP_PPC_BOOKE_WATCHDOG, 0);
1781 if (ret < 0) {
1782 fprintf(stderr, "%s: couldn't enable KVM_CAP_PPC_BOOKE_WATCHDOG: %s\n",
1783 __func__, strerror(-ret));
1784 return ret;
1787 return ret;
1790 static int read_cpuinfo(const char *field, char *value, int len)
1792 FILE *f;
1793 int ret = -1;
1794 int field_len = strlen(field);
1795 char line[512];
1797 f = fopen("/proc/cpuinfo", "r");
1798 if (!f) {
1799 return -1;
1802 do {
1803 if (!fgets(line, sizeof(line), f)) {
1804 break;
1806 if (!strncmp(line, field, field_len)) {
1807 pstrcpy(value, len, line);
1808 ret = 0;
1809 break;
1811 } while (*line);
1813 fclose(f);
1815 return ret;
1818 static uint32_t kvmppc_get_tbfreq_procfs(void)
1820 char line[512];
1821 char *ns;
1822 uint32_t tbfreq_fallback = NANOSECONDS_PER_SECOND;
1823 uint32_t tbfreq_procfs;
1825 if (read_cpuinfo("timebase", line, sizeof(line))) {
1826 return tbfreq_fallback;
1829 ns = strchr(line, ':');
1830 if (!ns) {
1831 return tbfreq_fallback;
1834 tbfreq_procfs = atoi(++ns);
1836 /* 0 is certainly not acceptable by the guest, return fallback value */
1837 return tbfreq_procfs ? tbfreq_procfs : tbfreq_fallback;
1840 uint32_t kvmppc_get_tbfreq(void)
1842 static uint32_t cached_tbfreq;
1844 if (!cached_tbfreq) {
1845 cached_tbfreq = kvmppc_get_tbfreq_procfs();
1848 return cached_tbfreq;
1851 bool kvmppc_get_host_serial(char **value)
1853 return g_file_get_contents("/proc/device-tree/system-id", value, NULL,
1854 NULL);
1857 bool kvmppc_get_host_model(char **value)
1859 return g_file_get_contents("/proc/device-tree/model", value, NULL, NULL);
1862 /* Try to find a device tree node for a CPU with clock-frequency property */
1863 static int kvmppc_find_cpu_dt(char *buf, int buf_len)
1865 struct dirent *dirp;
1866 DIR *dp;
1868 dp = opendir(PROC_DEVTREE_CPU);
1869 if (!dp) {
1870 printf("Can't open directory " PROC_DEVTREE_CPU "\n");
1871 return -1;
1874 buf[0] = '\0';
1875 while ((dirp = readdir(dp)) != NULL) {
1876 FILE *f;
1877 snprintf(buf, buf_len, "%s%s/clock-frequency", PROC_DEVTREE_CPU,
1878 dirp->d_name);
1879 f = fopen(buf, "r");
1880 if (f) {
1881 snprintf(buf, buf_len, "%s%s", PROC_DEVTREE_CPU, dirp->d_name);
1882 fclose(f);
1883 break;
1885 buf[0] = '\0';
1887 closedir(dp);
1888 if (buf[0] == '\0') {
1889 printf("Unknown host!\n");
1890 return -1;
1893 return 0;
1896 static uint64_t kvmppc_read_int_dt(const char *filename)
1898 union {
1899 uint32_t v32;
1900 uint64_t v64;
1901 } u;
1902 FILE *f;
1903 int len;
1905 f = fopen(filename, "rb");
1906 if (!f) {
1907 return -1;
1910 len = fread(&u, 1, sizeof(u), f);
1911 fclose(f);
1912 switch (len) {
1913 case 4:
1914 /* property is a 32-bit quantity */
1915 return be32_to_cpu(u.v32);
1916 case 8:
1917 return be64_to_cpu(u.v64);
1920 return 0;
1924 * Read a CPU node property from the host device tree that's a single
1925 * integer (32-bit or 64-bit). Returns 0 if anything goes wrong
1926 * (can't find or open the property, or doesn't understand the format)
1928 static uint64_t kvmppc_read_int_cpu_dt(const char *propname)
1930 char buf[PATH_MAX], *tmp;
1931 uint64_t val;
1933 if (kvmppc_find_cpu_dt(buf, sizeof(buf))) {
1934 return -1;
1937 tmp = g_strdup_printf("%s/%s", buf, propname);
1938 val = kvmppc_read_int_dt(tmp);
1939 g_free(tmp);
1941 return val;
1944 uint64_t kvmppc_get_clockfreq(void)
1946 return kvmppc_read_int_cpu_dt("clock-frequency");
1949 static int kvmppc_get_dec_bits(void)
1951 int nr_bits = kvmppc_read_int_cpu_dt("ibm,dec-bits");
1953 if (nr_bits > 0) {
1954 return nr_bits;
1956 return 0;
1959 static int kvmppc_get_pvinfo(CPUPPCState *env, struct kvm_ppc_pvinfo *pvinfo)
1961 CPUState *cs = env_cpu(env);
1963 if (kvm_vm_check_extension(cs->kvm_state, KVM_CAP_PPC_GET_PVINFO) &&
1964 !kvm_vm_ioctl(cs->kvm_state, KVM_PPC_GET_PVINFO, pvinfo)) {
1965 return 0;
1968 return 1;
1971 int kvmppc_get_hasidle(CPUPPCState *env)
1973 struct kvm_ppc_pvinfo pvinfo;
1975 if (!kvmppc_get_pvinfo(env, &pvinfo) &&
1976 (pvinfo.flags & KVM_PPC_PVINFO_FLAGS_EV_IDLE)) {
1977 return 1;
1980 return 0;
1983 int kvmppc_get_hypercall(CPUPPCState *env, uint8_t *buf, int buf_len)
1985 uint32_t *hc = (uint32_t *)buf;
1986 struct kvm_ppc_pvinfo pvinfo;
1988 if (!kvmppc_get_pvinfo(env, &pvinfo)) {
1989 memcpy(buf, pvinfo.hcall, buf_len);
1990 return 0;
1994 * Fallback to always fail hypercalls regardless of endianness:
1996 * tdi 0,r0,72 (becomes b .+8 in wrong endian, nop in good endian)
1997 * li r3, -1
1998 * b .+8 (becomes nop in wrong endian)
1999 * bswap32(li r3, -1)
2002 hc[0] = cpu_to_be32(0x08000048);
2003 hc[1] = cpu_to_be32(0x3860ffff);
2004 hc[2] = cpu_to_be32(0x48000008);
2005 hc[3] = cpu_to_be32(bswap32(0x3860ffff));
2007 return 1;
2010 static inline int kvmppc_enable_hcall(KVMState *s, target_ulong hcall)
2012 return kvm_vm_enable_cap(s, KVM_CAP_PPC_ENABLE_HCALL, 0, hcall, 1);
2015 void kvmppc_enable_logical_ci_hcalls(void)
2018 * FIXME: it would be nice if we could detect the cases where
2019 * we're using a device which requires the in kernel
2020 * implementation of these hcalls, but the kernel lacks them and
2021 * produce a warning.
2023 kvmppc_enable_hcall(kvm_state, H_LOGICAL_CI_LOAD);
2024 kvmppc_enable_hcall(kvm_state, H_LOGICAL_CI_STORE);
2027 void kvmppc_enable_set_mode_hcall(void)
2029 kvmppc_enable_hcall(kvm_state, H_SET_MODE);
2032 void kvmppc_enable_clear_ref_mod_hcalls(void)
2034 kvmppc_enable_hcall(kvm_state, H_CLEAR_REF);
2035 kvmppc_enable_hcall(kvm_state, H_CLEAR_MOD);
2038 void kvmppc_enable_h_page_init(void)
2040 kvmppc_enable_hcall(kvm_state, H_PAGE_INIT);
2043 void kvmppc_set_papr(PowerPCCPU *cpu)
2045 CPUState *cs = CPU(cpu);
2046 int ret;
2048 if (!kvm_enabled()) {
2049 return;
2052 ret = kvm_vcpu_enable_cap(cs, KVM_CAP_PPC_PAPR, 0);
2053 if (ret) {
2054 error_report("This vCPU type or KVM version does not support PAPR");
2055 exit(1);
2059 * Update the capability flag so we sync the right information
2060 * with kvm
2062 cap_papr = 1;
2065 int kvmppc_set_compat(PowerPCCPU *cpu, uint32_t compat_pvr)
2067 return kvm_set_one_reg(CPU(cpu), KVM_REG_PPC_ARCH_COMPAT, &compat_pvr);
2070 void kvmppc_set_mpic_proxy(PowerPCCPU *cpu, int mpic_proxy)
2072 CPUState *cs = CPU(cpu);
2073 int ret;
2075 ret = kvm_vcpu_enable_cap(cs, KVM_CAP_PPC_EPR, 0, mpic_proxy);
2076 if (ret && mpic_proxy) {
2077 error_report("This KVM version does not support EPR");
2078 exit(1);
2082 bool kvmppc_get_fwnmi(void)
2084 return cap_fwnmi;
2087 int kvmppc_set_fwnmi(PowerPCCPU *cpu)
2089 CPUState *cs = CPU(cpu);
2091 return kvm_vcpu_enable_cap(cs, KVM_CAP_PPC_FWNMI, 0);
2094 int kvmppc_smt_threads(void)
2096 return cap_ppc_smt ? cap_ppc_smt : 1;
2099 int kvmppc_set_smt_threads(int smt)
2101 int ret;
2103 ret = kvm_vm_enable_cap(kvm_state, KVM_CAP_PPC_SMT, 0, smt, 0);
2104 if (!ret) {
2105 cap_ppc_smt = smt;
2107 return ret;
2110 void kvmppc_error_append_smt_possible_hint(Error *const *errp)
2112 int i;
2113 GString *g;
2114 char *s;
2116 assert(kvm_enabled());
2117 if (cap_ppc_smt_possible) {
2118 g = g_string_new("Available VSMT modes:");
2119 for (i = 63; i >= 0; i--) {
2120 if ((1UL << i) & cap_ppc_smt_possible) {
2121 g_string_append_printf(g, " %lu", (1UL << i));
2124 s = g_string_free(g, false);
2125 error_append_hint(errp, "%s.\n", s);
2126 g_free(s);
2127 } else {
2128 error_append_hint(errp,
2129 "This KVM seems to be too old to support VSMT.\n");
2134 #ifdef TARGET_PPC64
2135 uint64_t kvmppc_vrma_limit(unsigned int hash_shift)
2137 struct kvm_ppc_smmu_info info;
2138 long rampagesize, best_page_shift;
2139 int i;
2142 * Find the largest hardware supported page size that's less than
2143 * or equal to the (logical) backing page size of guest RAM
2145 kvm_get_smmu_info(&info, &error_fatal);
2146 rampagesize = qemu_minrampagesize();
2147 best_page_shift = 0;
2149 for (i = 0; i < KVM_PPC_PAGE_SIZES_MAX_SZ; i++) {
2150 struct kvm_ppc_one_seg_page_size *sps = &info.sps[i];
2152 if (!sps->page_shift) {
2153 continue;
2156 if ((sps->page_shift > best_page_shift)
2157 && ((1UL << sps->page_shift) <= rampagesize)) {
2158 best_page_shift = sps->page_shift;
2162 return 1ULL << (best_page_shift + hash_shift - 7);
2164 #endif
2166 bool kvmppc_spapr_use_multitce(void)
2168 return cap_spapr_multitce;
2171 int kvmppc_spapr_enable_inkernel_multitce(void)
2173 int ret;
2175 ret = kvm_vm_enable_cap(kvm_state, KVM_CAP_PPC_ENABLE_HCALL, 0,
2176 H_PUT_TCE_INDIRECT, 1);
2177 if (!ret) {
2178 ret = kvm_vm_enable_cap(kvm_state, KVM_CAP_PPC_ENABLE_HCALL, 0,
2179 H_STUFF_TCE, 1);
2182 return ret;
2185 void *kvmppc_create_spapr_tce(uint32_t liobn, uint32_t page_shift,
2186 uint64_t bus_offset, uint32_t nb_table,
2187 int *pfd, bool need_vfio)
2189 long len;
2190 int fd;
2191 void *table;
2194 * Must set fd to -1 so we don't try to munmap when called for
2195 * destroying the table, which the upper layers -will- do
2197 *pfd = -1;
2198 if (!cap_spapr_tce || (need_vfio && !cap_spapr_vfio)) {
2199 return NULL;
2202 if (cap_spapr_tce_64) {
2203 struct kvm_create_spapr_tce_64 args = {
2204 .liobn = liobn,
2205 .page_shift = page_shift,
2206 .offset = bus_offset >> page_shift,
2207 .size = nb_table,
2208 .flags = 0
2210 fd = kvm_vm_ioctl(kvm_state, KVM_CREATE_SPAPR_TCE_64, &args);
2211 if (fd < 0) {
2212 fprintf(stderr,
2213 "KVM: Failed to create TCE64 table for liobn 0x%x\n",
2214 liobn);
2215 return NULL;
2217 } else if (cap_spapr_tce) {
2218 uint64_t window_size = (uint64_t) nb_table << page_shift;
2219 struct kvm_create_spapr_tce args = {
2220 .liobn = liobn,
2221 .window_size = window_size,
2223 if ((window_size != args.window_size) || bus_offset) {
2224 return NULL;
2226 fd = kvm_vm_ioctl(kvm_state, KVM_CREATE_SPAPR_TCE, &args);
2227 if (fd < 0) {
2228 fprintf(stderr, "KVM: Failed to create TCE table for liobn 0x%x\n",
2229 liobn);
2230 return NULL;
2232 } else {
2233 return NULL;
2236 len = nb_table * sizeof(uint64_t);
2237 /* FIXME: round this up to page size */
2239 table = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
2240 if (table == MAP_FAILED) {
2241 fprintf(stderr, "KVM: Failed to map TCE table for liobn 0x%x\n",
2242 liobn);
2243 close(fd);
2244 return NULL;
2247 *pfd = fd;
2248 return table;
2251 int kvmppc_remove_spapr_tce(void *table, int fd, uint32_t nb_table)
2253 long len;
2255 if (fd < 0) {
2256 return -1;
2259 len = nb_table * sizeof(uint64_t);
2260 if ((munmap(table, len) < 0) ||
2261 (close(fd) < 0)) {
2262 fprintf(stderr, "KVM: Unexpected error removing TCE table: %s",
2263 strerror(errno));
2264 /* Leak the table */
2267 return 0;
2270 int kvmppc_reset_htab(int shift_hint)
2272 uint32_t shift = shift_hint;
2274 if (!kvm_enabled()) {
2275 /* Full emulation, tell caller to allocate htab itself */
2276 return 0;
2278 if (kvm_vm_check_extension(kvm_state, KVM_CAP_PPC_ALLOC_HTAB)) {
2279 int ret;
2280 ret = kvm_vm_ioctl(kvm_state, KVM_PPC_ALLOCATE_HTAB, &shift);
2281 if (ret == -ENOTTY) {
2283 * At least some versions of PR KVM advertise the
2284 * capability, but don't implement the ioctl(). Oops.
2285 * Return 0 so that we allocate the htab in qemu, as is
2286 * correct for PR.
2288 return 0;
2289 } else if (ret < 0) {
2290 return ret;
2292 return shift;
2296 * We have a kernel that predates the htab reset calls. For PR
2297 * KVM, we need to allocate the htab ourselves, for an HV KVM of
2298 * this era, it has allocated a 16MB fixed size hash table
2299 * already.
2301 if (kvmppc_is_pr(kvm_state)) {
2302 /* PR - tell caller to allocate htab */
2303 return 0;
2304 } else {
2305 /* HV - assume 16MB kernel allocated htab */
2306 return 24;
2310 static inline uint32_t mfpvr(void)
2312 uint32_t pvr;
2314 asm ("mfpvr %0"
2315 : "=r"(pvr));
2316 return pvr;
2319 static void alter_insns(uint64_t *word, uint64_t flags, bool on)
2321 if (on) {
2322 *word |= flags;
2323 } else {
2324 *word &= ~flags;
2328 static void kvmppc_host_cpu_class_init(ObjectClass *oc, void *data)
2330 PowerPCCPUClass *pcc = POWERPC_CPU_CLASS(oc);
2331 uint32_t dcache_size = kvmppc_read_int_cpu_dt("d-cache-size");
2332 uint32_t icache_size = kvmppc_read_int_cpu_dt("i-cache-size");
2334 /* Now fix up the class with information we can query from the host */
2335 pcc->pvr = mfpvr();
2337 alter_insns(&pcc->insns_flags, PPC_ALTIVEC,
2338 qemu_getauxval(AT_HWCAP) & PPC_FEATURE_HAS_ALTIVEC);
2339 alter_insns(&pcc->insns_flags2, PPC2_VSX,
2340 qemu_getauxval(AT_HWCAP) & PPC_FEATURE_HAS_VSX);
2341 alter_insns(&pcc->insns_flags2, PPC2_DFP,
2342 qemu_getauxval(AT_HWCAP) & PPC_FEATURE_HAS_DFP);
2344 if (dcache_size != -1) {
2345 pcc->l1_dcache_size = dcache_size;
2348 if (icache_size != -1) {
2349 pcc->l1_icache_size = icache_size;
2352 #if defined(TARGET_PPC64)
2353 pcc->radix_page_info = kvm_get_radix_page_info();
2355 if ((pcc->pvr & 0xffffff00) == CPU_POWERPC_POWER9_DD1) {
2357 * POWER9 DD1 has some bugs which make it not really ISA 3.00
2358 * compliant. More importantly, advertising ISA 3.00
2359 * architected mode may prevent guests from activating
2360 * necessary DD1 workarounds.
2362 pcc->pcr_supported &= ~(PCR_COMPAT_3_00 | PCR_COMPAT_2_07
2363 | PCR_COMPAT_2_06 | PCR_COMPAT_2_05);
2365 #endif /* defined(TARGET_PPC64) */
2368 bool kvmppc_has_cap_epr(void)
2370 return cap_epr;
2373 bool kvmppc_has_cap_fixup_hcalls(void)
2375 return cap_fixup_hcalls;
2378 bool kvmppc_has_cap_htm(void)
2380 return cap_htm;
2383 bool kvmppc_has_cap_mmu_radix(void)
2385 return cap_mmu_radix;
2388 bool kvmppc_has_cap_mmu_hash_v3(void)
2390 return cap_mmu_hash_v3;
2393 static bool kvmppc_power8_host(void)
2395 bool ret = false;
2396 #ifdef TARGET_PPC64
2398 uint32_t base_pvr = CPU_POWERPC_POWER_SERVER_MASK & mfpvr();
2399 ret = (base_pvr == CPU_POWERPC_POWER8E_BASE) ||
2400 (base_pvr == CPU_POWERPC_POWER8NVL_BASE) ||
2401 (base_pvr == CPU_POWERPC_POWER8_BASE);
2403 #endif /* TARGET_PPC64 */
2404 return ret;
2407 static int parse_cap_ppc_safe_cache(struct kvm_ppc_cpu_char c)
2409 bool l1d_thread_priv_req = !kvmppc_power8_host();
2411 if (~c.behaviour & c.behaviour_mask & H_CPU_BEHAV_L1D_FLUSH_PR) {
2412 return 2;
2413 } else if ((!l1d_thread_priv_req ||
2414 c.character & c.character_mask & H_CPU_CHAR_L1D_THREAD_PRIV) &&
2415 (c.character & c.character_mask
2416 & (H_CPU_CHAR_L1D_FLUSH_ORI30 | H_CPU_CHAR_L1D_FLUSH_TRIG2))) {
2417 return 1;
2420 return 0;
2423 static int parse_cap_ppc_safe_bounds_check(struct kvm_ppc_cpu_char c)
2425 if (~c.behaviour & c.behaviour_mask & H_CPU_BEHAV_BNDS_CHK_SPEC_BAR) {
2426 return 2;
2427 } else if (c.character & c.character_mask & H_CPU_CHAR_SPEC_BAR_ORI31) {
2428 return 1;
2431 return 0;
2434 static int parse_cap_ppc_safe_indirect_branch(struct kvm_ppc_cpu_char c)
2436 if ((~c.behaviour & c.behaviour_mask & H_CPU_BEHAV_FLUSH_COUNT_CACHE) &&
2437 (~c.character & c.character_mask & H_CPU_CHAR_CACHE_COUNT_DIS) &&
2438 (~c.character & c.character_mask & H_CPU_CHAR_BCCTRL_SERIALISED)) {
2439 return SPAPR_CAP_FIXED_NA;
2440 } else if (c.behaviour & c.behaviour_mask & H_CPU_BEHAV_FLUSH_COUNT_CACHE) {
2441 return SPAPR_CAP_WORKAROUND;
2442 } else if (c.character & c.character_mask & H_CPU_CHAR_CACHE_COUNT_DIS) {
2443 return SPAPR_CAP_FIXED_CCD;
2444 } else if (c.character & c.character_mask & H_CPU_CHAR_BCCTRL_SERIALISED) {
2445 return SPAPR_CAP_FIXED_IBS;
2448 return 0;
2451 static int parse_cap_ppc_count_cache_flush_assist(struct kvm_ppc_cpu_char c)
2453 if (c.character & c.character_mask & H_CPU_CHAR_BCCTR_FLUSH_ASSIST) {
2454 return 1;
2456 return 0;
2459 bool kvmppc_has_cap_xive(void)
2461 return cap_xive;
2464 static void kvmppc_get_cpu_characteristics(KVMState *s)
2466 struct kvm_ppc_cpu_char c;
2467 int ret;
2469 /* Assume broken */
2470 cap_ppc_safe_cache = 0;
2471 cap_ppc_safe_bounds_check = 0;
2472 cap_ppc_safe_indirect_branch = 0;
2474 ret = kvm_vm_check_extension(s, KVM_CAP_PPC_GET_CPU_CHAR);
2475 if (!ret) {
2476 return;
2478 ret = kvm_vm_ioctl(s, KVM_PPC_GET_CPU_CHAR, &c);
2479 if (ret < 0) {
2480 return;
2483 cap_ppc_safe_cache = parse_cap_ppc_safe_cache(c);
2484 cap_ppc_safe_bounds_check = parse_cap_ppc_safe_bounds_check(c);
2485 cap_ppc_safe_indirect_branch = parse_cap_ppc_safe_indirect_branch(c);
2486 cap_ppc_count_cache_flush_assist =
2487 parse_cap_ppc_count_cache_flush_assist(c);
2490 int kvmppc_get_cap_safe_cache(void)
2492 return cap_ppc_safe_cache;
2495 int kvmppc_get_cap_safe_bounds_check(void)
2497 return cap_ppc_safe_bounds_check;
2500 int kvmppc_get_cap_safe_indirect_branch(void)
2502 return cap_ppc_safe_indirect_branch;
2505 int kvmppc_get_cap_count_cache_flush_assist(void)
2507 return cap_ppc_count_cache_flush_assist;
2510 bool kvmppc_has_cap_nested_kvm_hv(void)
2512 return !!cap_ppc_nested_kvm_hv;
2515 int kvmppc_set_cap_nested_kvm_hv(int enable)
2517 return kvm_vm_enable_cap(kvm_state, KVM_CAP_PPC_NESTED_HV, 0, enable);
2520 bool kvmppc_has_cap_spapr_vfio(void)
2522 return cap_spapr_vfio;
2525 int kvmppc_get_cap_large_decr(void)
2527 return cap_large_decr;
2530 int kvmppc_enable_cap_large_decr(PowerPCCPU *cpu, int enable)
2532 CPUState *cs = CPU(cpu);
2533 uint64_t lpcr;
2535 kvm_get_one_reg(cs, KVM_REG_PPC_LPCR_64, &lpcr);
2536 /* Do we need to modify the LPCR? */
2537 if (!!(lpcr & LPCR_LD) != !!enable) {
2538 if (enable) {
2539 lpcr |= LPCR_LD;
2540 } else {
2541 lpcr &= ~LPCR_LD;
2543 kvm_set_one_reg(cs, KVM_REG_PPC_LPCR_64, &lpcr);
2544 kvm_get_one_reg(cs, KVM_REG_PPC_LPCR_64, &lpcr);
2546 if (!!(lpcr & LPCR_LD) != !!enable) {
2547 return -1;
2551 return 0;
2554 PowerPCCPUClass *kvm_ppc_get_host_cpu_class(void)
2556 uint32_t host_pvr = mfpvr();
2557 PowerPCCPUClass *pvr_pcc;
2559 pvr_pcc = ppc_cpu_class_by_pvr(host_pvr);
2560 if (pvr_pcc == NULL) {
2561 pvr_pcc = ppc_cpu_class_by_pvr_mask(host_pvr);
2564 return pvr_pcc;
2567 static void pseries_machine_class_fixup(ObjectClass *oc, void *opaque)
2569 MachineClass *mc = MACHINE_CLASS(oc);
2571 mc->default_cpu_type = TYPE_HOST_POWERPC_CPU;
2574 static int kvm_ppc_register_host_cpu_type(void)
2576 TypeInfo type_info = {
2577 .name = TYPE_HOST_POWERPC_CPU,
2578 .class_init = kvmppc_host_cpu_class_init,
2580 PowerPCCPUClass *pvr_pcc;
2581 ObjectClass *oc;
2582 DeviceClass *dc;
2583 int i;
2585 pvr_pcc = kvm_ppc_get_host_cpu_class();
2586 if (pvr_pcc == NULL) {
2587 return -1;
2589 type_info.parent = object_class_get_name(OBJECT_CLASS(pvr_pcc));
2590 type_register(&type_info);
2591 /* override TCG default cpu type with 'host' cpu model */
2592 object_class_foreach(pseries_machine_class_fixup, TYPE_SPAPR_MACHINE,
2593 false, NULL);
2595 oc = object_class_by_name(type_info.name);
2596 g_assert(oc);
2599 * Update generic CPU family class alias (e.g. on a POWER8NVL host,
2600 * we want "POWER8" to be a "family" alias that points to the current
2601 * host CPU type, too)
2603 dc = DEVICE_CLASS(ppc_cpu_get_family_class(pvr_pcc));
2604 for (i = 0; ppc_cpu_aliases[i].alias != NULL; i++) {
2605 if (strcasecmp(ppc_cpu_aliases[i].alias, dc->desc) == 0) {
2606 char *suffix;
2608 ppc_cpu_aliases[i].model = g_strdup(object_class_get_name(oc));
2609 suffix = strstr(ppc_cpu_aliases[i].model, POWERPC_CPU_TYPE_SUFFIX);
2610 if (suffix) {
2611 *suffix = 0;
2613 break;
2617 return 0;
2620 int kvmppc_define_rtas_kernel_token(uint32_t token, const char *function)
2622 struct kvm_rtas_token_args args = {
2623 .token = token,
2626 if (!kvm_check_extension(kvm_state, KVM_CAP_PPC_RTAS)) {
2627 return -ENOENT;
2630 strncpy(args.name, function, sizeof(args.name) - 1);
2632 return kvm_vm_ioctl(kvm_state, KVM_PPC_RTAS_DEFINE_TOKEN, &args);
2635 int kvmppc_get_htab_fd(bool write, uint64_t index, Error **errp)
2637 struct kvm_get_htab_fd s = {
2638 .flags = write ? KVM_GET_HTAB_WRITE : 0,
2639 .start_index = index,
2641 int ret;
2643 if (!cap_htab_fd) {
2644 error_setg(errp, "KVM version doesn't support %s the HPT",
2645 write ? "writing" : "reading");
2646 return -ENOTSUP;
2649 ret = kvm_vm_ioctl(kvm_state, KVM_PPC_GET_HTAB_FD, &s);
2650 if (ret < 0) {
2651 error_setg(errp, "Unable to open fd for %s HPT %s KVM: %s",
2652 write ? "writing" : "reading", write ? "to" : "from",
2653 strerror(errno));
2654 return -errno;
2657 return ret;
2660 int kvmppc_save_htab(QEMUFile *f, int fd, size_t bufsize, int64_t max_ns)
2662 int64_t starttime = qemu_clock_get_ns(QEMU_CLOCK_REALTIME);
2663 uint8_t buf[bufsize];
2664 ssize_t rc;
2666 do {
2667 rc = read(fd, buf, bufsize);
2668 if (rc < 0) {
2669 fprintf(stderr, "Error reading data from KVM HTAB fd: %s\n",
2670 strerror(errno));
2671 return rc;
2672 } else if (rc) {
2673 uint8_t *buffer = buf;
2674 ssize_t n = rc;
2675 while (n) {
2676 struct kvm_get_htab_header *head =
2677 (struct kvm_get_htab_header *) buffer;
2678 size_t chunksize = sizeof(*head) +
2679 HASH_PTE_SIZE_64 * head->n_valid;
2681 qemu_put_be32(f, head->index);
2682 qemu_put_be16(f, head->n_valid);
2683 qemu_put_be16(f, head->n_invalid);
2684 qemu_put_buffer(f, (void *)(head + 1),
2685 HASH_PTE_SIZE_64 * head->n_valid);
2687 buffer += chunksize;
2688 n -= chunksize;
2691 } while ((rc != 0)
2692 && ((max_ns < 0) ||
2693 ((qemu_clock_get_ns(QEMU_CLOCK_REALTIME) - starttime) < max_ns)));
2695 return (rc == 0) ? 1 : 0;
2698 int kvmppc_load_htab_chunk(QEMUFile *f, int fd, uint32_t index,
2699 uint16_t n_valid, uint16_t n_invalid, Error **errp)
2701 struct kvm_get_htab_header *buf;
2702 size_t chunksize = sizeof(*buf) + n_valid * HASH_PTE_SIZE_64;
2703 ssize_t rc;
2705 buf = alloca(chunksize);
2706 buf->index = index;
2707 buf->n_valid = n_valid;
2708 buf->n_invalid = n_invalid;
2710 qemu_get_buffer(f, (void *)(buf + 1), HASH_PTE_SIZE_64 * n_valid);
2712 rc = write(fd, buf, chunksize);
2713 if (rc < 0) {
2714 error_setg_errno(errp, errno, "Error writing the KVM hash table");
2715 return -errno;
2717 if (rc != chunksize) {
2718 /* We should never get a short write on a single chunk */
2719 error_setg(errp, "Short write while restoring the KVM hash table");
2720 return -ENOSPC;
2722 return 0;
2725 bool kvm_arch_stop_on_emulation_error(CPUState *cpu)
2727 return true;
2730 void kvm_arch_init_irq_routing(KVMState *s)
2734 void kvmppc_read_hptes(ppc_hash_pte64_t *hptes, hwaddr ptex, int n)
2736 int fd, rc;
2737 int i;
2739 fd = kvmppc_get_htab_fd(false, ptex, &error_abort);
2741 i = 0;
2742 while (i < n) {
2743 struct kvm_get_htab_header *hdr;
2744 int m = n < HPTES_PER_GROUP ? n : HPTES_PER_GROUP;
2745 char buf[sizeof(*hdr) + m * HASH_PTE_SIZE_64];
2747 rc = read(fd, buf, sizeof(buf));
2748 if (rc < 0) {
2749 hw_error("kvmppc_read_hptes: Unable to read HPTEs");
2752 hdr = (struct kvm_get_htab_header *)buf;
2753 while ((i < n) && ((char *)hdr < (buf + rc))) {
2754 int invalid = hdr->n_invalid, valid = hdr->n_valid;
2756 if (hdr->index != (ptex + i)) {
2757 hw_error("kvmppc_read_hptes: Unexpected HPTE index %"PRIu32
2758 " != (%"HWADDR_PRIu" + %d", hdr->index, ptex, i);
2761 if (n - i < valid) {
2762 valid = n - i;
2764 memcpy(hptes + i, hdr + 1, HASH_PTE_SIZE_64 * valid);
2765 i += valid;
2767 if ((n - i) < invalid) {
2768 invalid = n - i;
2770 memset(hptes + i, 0, invalid * HASH_PTE_SIZE_64);
2771 i += invalid;
2773 hdr = (struct kvm_get_htab_header *)
2774 ((char *)(hdr + 1) + HASH_PTE_SIZE_64 * hdr->n_valid);
2778 close(fd);
2781 void kvmppc_write_hpte(hwaddr ptex, uint64_t pte0, uint64_t pte1)
2783 int fd, rc;
2784 struct {
2785 struct kvm_get_htab_header hdr;
2786 uint64_t pte0;
2787 uint64_t pte1;
2788 } buf;
2790 fd = kvmppc_get_htab_fd(true, 0 /* Ignored */, &error_abort);
2792 buf.hdr.n_valid = 1;
2793 buf.hdr.n_invalid = 0;
2794 buf.hdr.index = ptex;
2795 buf.pte0 = cpu_to_be64(pte0);
2796 buf.pte1 = cpu_to_be64(pte1);
2798 rc = write(fd, &buf, sizeof(buf));
2799 if (rc != sizeof(buf)) {
2800 hw_error("kvmppc_write_hpte: Unable to update KVM HPT");
2802 close(fd);
2805 int kvm_arch_fixup_msi_route(struct kvm_irq_routing_entry *route,
2806 uint64_t address, uint32_t data, PCIDevice *dev)
2808 return 0;
2811 int kvm_arch_add_msi_route_post(struct kvm_irq_routing_entry *route,
2812 int vector, PCIDevice *dev)
2814 return 0;
2817 int kvm_arch_release_virq_post(int virq)
2819 return 0;
2822 int kvm_arch_msi_data_to_gsi(uint32_t data)
2824 return data & 0xffff;
2827 #if defined(TARGET_PPC64)
2828 int kvm_handle_nmi(PowerPCCPU *cpu, struct kvm_run *run)
2830 uint16_t flags = run->flags & KVM_RUN_PPC_NMI_DISP_MASK;
2832 cpu_synchronize_state(CPU(cpu));
2834 spapr_mce_req_event(cpu, flags == KVM_RUN_PPC_NMI_DISP_FULLY_RECOV);
2836 return 0;
2838 #endif
2840 int kvmppc_enable_hwrng(void)
2842 if (!kvm_enabled() || !kvm_check_extension(kvm_state, KVM_CAP_PPC_HWRNG)) {
2843 return -1;
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)) {
2861 return;
2864 error_setg(errp,
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 = {
2872 .flags = flags,
2873 .shift = shift,
2876 if (!cap_resize_hpt) {
2877 return -ENOSYS;
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 = {
2887 .flags = flags,
2888 .shift = shift,
2891 if (!cap_resize_hpt) {
2892 return -ENOSYS;
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
2906 * cpu_post_load.
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
2911 * in QEMU level.
2913 bool kvmppc_pvr_workaround_required(PowerPCCPU *cpu)
2915 CPUState *cs = CPU(cpu);
2917 if (!kvm_enabled()) {
2918 return false;
2921 if (cap_ppc_pvr_compat) {
2922 return false;
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);
2937 void kvmppc_set_reg_tb_offset(PowerPCCPU *cpu, int64_t tb_offset)
2939 CPUState *cs = CPU(cpu);
2941 if (kvm_enabled()) {
2942 kvm_set_one_reg(cs, KVM_REG_PPC_TB_OFFSET, &tb_offset);
2946 bool kvm_arch_cpu_check_are_resettable(void)
2948 return true;