spapr: Move commas inside SPAPR_COMPAT_* macros
[qemu/cris-port.git] / target-ppc / kvm.c
blob1da9ea81e5415e940c6cbed25a7300e133309d2c
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 <dirent.h>
18 #include <sys/types.h>
19 #include <sys/ioctl.h>
20 #include <sys/mman.h>
21 #include <sys/vfs.h>
23 #include <linux/kvm.h>
25 #include "qemu-common.h"
26 #include "qemu/timer.h"
27 #include "sysemu/sysemu.h"
28 #include "sysemu/kvm.h"
29 #include "kvm_ppc.h"
30 #include "cpu.h"
31 #include "sysemu/cpus.h"
32 #include "sysemu/device_tree.h"
33 #include "mmu-hash64.h"
35 #include "hw/sysbus.h"
36 #include "hw/ppc/spapr.h"
37 #include "hw/ppc/spapr_vio.h"
38 #include "hw/ppc/ppc.h"
39 #include "sysemu/watchdog.h"
40 #include "trace.h"
41 #include "exec/gdbstub.h"
42 #include "exec/memattrs.h"
44 //#define DEBUG_KVM
46 #ifdef DEBUG_KVM
47 #define DPRINTF(fmt, ...) \
48 do { fprintf(stderr, fmt, ## __VA_ARGS__); } while (0)
49 #else
50 #define DPRINTF(fmt, ...) \
51 do { } while (0)
52 #endif
54 #define PROC_DEVTREE_CPU "/proc/device-tree/cpus/"
56 const KVMCapabilityInfo kvm_arch_required_capabilities[] = {
57 KVM_CAP_LAST_INFO
60 static int cap_interrupt_unset = false;
61 static int cap_interrupt_level = false;
62 static int cap_segstate;
63 static int cap_booke_sregs;
64 static int cap_ppc_smt;
65 static int cap_ppc_rma;
66 static int cap_spapr_tce;
67 static int cap_spapr_multitce;
68 static int cap_spapr_vfio;
69 static int cap_hior;
70 static int cap_one_reg;
71 static int cap_epr;
72 static int cap_ppc_watchdog;
73 static int cap_papr;
74 static int cap_htab_fd;
75 static int cap_fixup_hcalls;
77 static uint32_t debug_inst_opcode;
79 /* XXX We have a race condition where we actually have a level triggered
80 * interrupt, but the infrastructure can't expose that yet, so the guest
81 * takes but ignores it, goes to sleep and never gets notified that there's
82 * still an interrupt pending.
84 * As a quick workaround, let's just wake up again 20 ms after we injected
85 * an interrupt. That way we can assure that we're always reinjecting
86 * interrupts in case the guest swallowed them.
88 static QEMUTimer *idle_timer;
90 static void kvm_kick_cpu(void *opaque)
92 PowerPCCPU *cpu = opaque;
94 qemu_cpu_kick(CPU(cpu));
97 static int kvm_ppc_register_host_cpu_type(void);
99 int kvm_arch_init(MachineState *ms, KVMState *s)
101 cap_interrupt_unset = kvm_check_extension(s, KVM_CAP_PPC_UNSET_IRQ);
102 cap_interrupt_level = kvm_check_extension(s, KVM_CAP_PPC_IRQ_LEVEL);
103 cap_segstate = kvm_check_extension(s, KVM_CAP_PPC_SEGSTATE);
104 cap_booke_sregs = kvm_check_extension(s, KVM_CAP_PPC_BOOKE_SREGS);
105 cap_ppc_smt = kvm_check_extension(s, KVM_CAP_PPC_SMT);
106 cap_ppc_rma = kvm_check_extension(s, KVM_CAP_PPC_RMA);
107 cap_spapr_tce = kvm_check_extension(s, KVM_CAP_SPAPR_TCE);
108 cap_spapr_multitce = kvm_check_extension(s, KVM_CAP_SPAPR_MULTITCE);
109 cap_spapr_vfio = false;
110 cap_one_reg = kvm_check_extension(s, KVM_CAP_ONE_REG);
111 cap_hior = kvm_check_extension(s, KVM_CAP_PPC_HIOR);
112 cap_epr = kvm_check_extension(s, KVM_CAP_PPC_EPR);
113 cap_ppc_watchdog = kvm_check_extension(s, KVM_CAP_PPC_BOOKE_WATCHDOG);
114 /* Note: we don't set cap_papr here, because this capability is
115 * only activated after this by kvmppc_set_papr() */
116 cap_htab_fd = kvm_check_extension(s, KVM_CAP_PPC_HTAB_FD);
117 cap_fixup_hcalls = kvm_check_extension(s, KVM_CAP_PPC_FIXUP_HCALL);
119 if (!cap_interrupt_level) {
120 fprintf(stderr, "KVM: Couldn't find level irq capability. Expect the "
121 "VM to stall at times!\n");
124 kvm_ppc_register_host_cpu_type();
126 return 0;
129 static int kvm_arch_sync_sregs(PowerPCCPU *cpu)
131 CPUPPCState *cenv = &cpu->env;
132 CPUState *cs = CPU(cpu);
133 struct kvm_sregs sregs;
134 int ret;
136 if (cenv->excp_model == POWERPC_EXCP_BOOKE) {
137 /* What we're really trying to say is "if we're on BookE, we use
138 the native PVR for now". This is the only sane way to check
139 it though, so we potentially confuse users that they can run
140 BookE guests on BookS. Let's hope nobody dares enough :) */
141 return 0;
142 } else {
143 if (!cap_segstate) {
144 fprintf(stderr, "kvm error: missing PVR setting capability\n");
145 return -ENOSYS;
149 ret = kvm_vcpu_ioctl(cs, KVM_GET_SREGS, &sregs);
150 if (ret) {
151 return ret;
154 sregs.pvr = cenv->spr[SPR_PVR];
155 return kvm_vcpu_ioctl(cs, KVM_SET_SREGS, &sregs);
158 /* Set up a shared TLB array with KVM */
159 static int kvm_booke206_tlb_init(PowerPCCPU *cpu)
161 CPUPPCState *env = &cpu->env;
162 CPUState *cs = CPU(cpu);
163 struct kvm_book3e_206_tlb_params params = {};
164 struct kvm_config_tlb cfg = {};
165 unsigned int entries = 0;
166 int ret, i;
168 if (!kvm_enabled() ||
169 !kvm_check_extension(cs->kvm_state, KVM_CAP_SW_TLB)) {
170 return 0;
173 assert(ARRAY_SIZE(params.tlb_sizes) == BOOKE206_MAX_TLBN);
175 for (i = 0; i < BOOKE206_MAX_TLBN; i++) {
176 params.tlb_sizes[i] = booke206_tlb_size(env, i);
177 params.tlb_ways[i] = booke206_tlb_ways(env, i);
178 entries += params.tlb_sizes[i];
181 assert(entries == env->nb_tlb);
182 assert(sizeof(struct kvm_book3e_206_tlb_entry) == sizeof(ppcmas_tlb_t));
184 env->tlb_dirty = true;
186 cfg.array = (uintptr_t)env->tlb.tlbm;
187 cfg.array_len = sizeof(ppcmas_tlb_t) * entries;
188 cfg.params = (uintptr_t)&params;
189 cfg.mmu_type = KVM_MMU_FSL_BOOKE_NOHV;
191 ret = kvm_vcpu_enable_cap(cs, KVM_CAP_SW_TLB, 0, (uintptr_t)&cfg);
192 if (ret < 0) {
193 fprintf(stderr, "%s: couldn't enable KVM_CAP_SW_TLB: %s\n",
194 __func__, strerror(-ret));
195 return ret;
198 env->kvm_sw_tlb = true;
199 return 0;
203 #if defined(TARGET_PPC64)
204 static void kvm_get_fallback_smmu_info(PowerPCCPU *cpu,
205 struct kvm_ppc_smmu_info *info)
207 CPUPPCState *env = &cpu->env;
208 CPUState *cs = CPU(cpu);
210 memset(info, 0, sizeof(*info));
212 /* We don't have the new KVM_PPC_GET_SMMU_INFO ioctl, so
213 * need to "guess" what the supported page sizes are.
215 * For that to work we make a few assumptions:
217 * - If KVM_CAP_PPC_GET_PVINFO is supported we are running "PR"
218 * KVM which only supports 4K and 16M pages, but supports them
219 * regardless of the backing store characteritics. We also don't
220 * support 1T segments.
222 * This is safe as if HV KVM ever supports that capability or PR
223 * KVM grows supports for more page/segment sizes, those versions
224 * will have implemented KVM_CAP_PPC_GET_SMMU_INFO and thus we
225 * will not hit this fallback
227 * - Else we are running HV KVM. This means we only support page
228 * sizes that fit in the backing store. Additionally we only
229 * advertize 64K pages if the processor is ARCH 2.06 and we assume
230 * P7 encodings for the SLB and hash table. Here too, we assume
231 * support for any newer processor will mean a kernel that
232 * implements KVM_CAP_PPC_GET_SMMU_INFO and thus doesn't hit
233 * this fallback.
235 if (kvm_check_extension(cs->kvm_state, KVM_CAP_PPC_GET_PVINFO)) {
236 /* No flags */
237 info->flags = 0;
238 info->slb_size = 64;
240 /* Standard 4k base page size segment */
241 info->sps[0].page_shift = 12;
242 info->sps[0].slb_enc = 0;
243 info->sps[0].enc[0].page_shift = 12;
244 info->sps[0].enc[0].pte_enc = 0;
246 /* Standard 16M large page size segment */
247 info->sps[1].page_shift = 24;
248 info->sps[1].slb_enc = SLB_VSID_L;
249 info->sps[1].enc[0].page_shift = 24;
250 info->sps[1].enc[0].pte_enc = 0;
251 } else {
252 int i = 0;
254 /* HV KVM has backing store size restrictions */
255 info->flags = KVM_PPC_PAGE_SIZES_REAL;
257 if (env->mmu_model & POWERPC_MMU_1TSEG) {
258 info->flags |= KVM_PPC_1T_SEGMENTS;
261 if (env->mmu_model == POWERPC_MMU_2_06) {
262 info->slb_size = 32;
263 } else {
264 info->slb_size = 64;
267 /* Standard 4k base page size segment */
268 info->sps[i].page_shift = 12;
269 info->sps[i].slb_enc = 0;
270 info->sps[i].enc[0].page_shift = 12;
271 info->sps[i].enc[0].pte_enc = 0;
272 i++;
274 /* 64K on MMU 2.06 */
275 if (env->mmu_model == POWERPC_MMU_2_06) {
276 info->sps[i].page_shift = 16;
277 info->sps[i].slb_enc = 0x110;
278 info->sps[i].enc[0].page_shift = 16;
279 info->sps[i].enc[0].pte_enc = 1;
280 i++;
283 /* Standard 16M large page size segment */
284 info->sps[i].page_shift = 24;
285 info->sps[i].slb_enc = SLB_VSID_L;
286 info->sps[i].enc[0].page_shift = 24;
287 info->sps[i].enc[0].pte_enc = 0;
291 static void kvm_get_smmu_info(PowerPCCPU *cpu, struct kvm_ppc_smmu_info *info)
293 CPUState *cs = CPU(cpu);
294 int ret;
296 if (kvm_check_extension(cs->kvm_state, KVM_CAP_PPC_GET_SMMU_INFO)) {
297 ret = kvm_vm_ioctl(cs->kvm_state, KVM_PPC_GET_SMMU_INFO, info);
298 if (ret == 0) {
299 return;
303 kvm_get_fallback_smmu_info(cpu, info);
306 static long getrampagesize(void)
308 struct statfs fs;
309 int ret;
311 if (!mem_path) {
312 /* guest RAM is backed by normal anonymous pages */
313 return getpagesize();
316 do {
317 ret = statfs(mem_path, &fs);
318 } while (ret != 0 && errno == EINTR);
320 if (ret != 0) {
321 fprintf(stderr, "Couldn't statfs() memory path: %s\n",
322 strerror(errno));
323 exit(1);
326 #define HUGETLBFS_MAGIC 0x958458f6
328 if (fs.f_type != HUGETLBFS_MAGIC) {
329 /* Explicit mempath, but it's ordinary pages */
330 return getpagesize();
333 /* It's hugepage, return the huge page size */
334 return fs.f_bsize;
337 static bool kvm_valid_page_size(uint32_t flags, long rampgsize, uint32_t shift)
339 if (!(flags & KVM_PPC_PAGE_SIZES_REAL)) {
340 return true;
343 return (1ul << shift) <= rampgsize;
346 static void kvm_fixup_page_sizes(PowerPCCPU *cpu)
348 static struct kvm_ppc_smmu_info smmu_info;
349 static bool has_smmu_info;
350 CPUPPCState *env = &cpu->env;
351 long rampagesize;
352 int iq, ik, jq, jk;
354 /* We only handle page sizes for 64-bit server guests for now */
355 if (!(env->mmu_model & POWERPC_MMU_64)) {
356 return;
359 /* Collect MMU info from kernel if not already */
360 if (!has_smmu_info) {
361 kvm_get_smmu_info(cpu, &smmu_info);
362 has_smmu_info = true;
365 rampagesize = getrampagesize();
367 /* Convert to QEMU form */
368 memset(&env->sps, 0, sizeof(env->sps));
371 * XXX This loop should be an entry wide AND of the capabilities that
372 * the selected CPU has with the capabilities that KVM supports.
374 for (ik = iq = 0; ik < KVM_PPC_PAGE_SIZES_MAX_SZ; ik++) {
375 struct ppc_one_seg_page_size *qsps = &env->sps.sps[iq];
376 struct kvm_ppc_one_seg_page_size *ksps = &smmu_info.sps[ik];
378 if (!kvm_valid_page_size(smmu_info.flags, rampagesize,
379 ksps->page_shift)) {
380 continue;
382 qsps->page_shift = ksps->page_shift;
383 qsps->slb_enc = ksps->slb_enc;
384 for (jk = jq = 0; jk < KVM_PPC_PAGE_SIZES_MAX_SZ; jk++) {
385 if (!kvm_valid_page_size(smmu_info.flags, rampagesize,
386 ksps->enc[jk].page_shift)) {
387 continue;
389 qsps->enc[jq].page_shift = ksps->enc[jk].page_shift;
390 qsps->enc[jq].pte_enc = ksps->enc[jk].pte_enc;
391 if (++jq >= PPC_PAGE_SIZES_MAX_SZ) {
392 break;
395 if (++iq >= PPC_PAGE_SIZES_MAX_SZ) {
396 break;
399 env->slb_nr = smmu_info.slb_size;
400 if (!(smmu_info.flags & KVM_PPC_1T_SEGMENTS)) {
401 env->mmu_model &= ~POWERPC_MMU_1TSEG;
404 #else /* defined (TARGET_PPC64) */
406 static inline void kvm_fixup_page_sizes(PowerPCCPU *cpu)
410 #endif /* !defined (TARGET_PPC64) */
412 unsigned long kvm_arch_vcpu_id(CPUState *cpu)
414 return ppc_get_vcpu_dt_id(POWERPC_CPU(cpu));
417 /* e500 supports 2 h/w breakpoint and 2 watchpoint.
418 * book3s supports only 1 watchpoint, so array size
419 * of 4 is sufficient for now.
421 #define MAX_HW_BKPTS 4
423 static struct HWBreakpoint {
424 target_ulong addr;
425 int type;
426 } hw_debug_points[MAX_HW_BKPTS];
428 static CPUWatchpoint hw_watchpoint;
430 /* Default there is no breakpoint and watchpoint supported */
431 static int max_hw_breakpoint;
432 static int max_hw_watchpoint;
433 static int nb_hw_breakpoint;
434 static int nb_hw_watchpoint;
436 static void kvmppc_hw_debug_points_init(CPUPPCState *cenv)
438 if (cenv->excp_model == POWERPC_EXCP_BOOKE) {
439 max_hw_breakpoint = 2;
440 max_hw_watchpoint = 2;
443 if ((max_hw_breakpoint + max_hw_watchpoint) > MAX_HW_BKPTS) {
444 fprintf(stderr, "Error initializing h/w breakpoints\n");
445 return;
449 int kvm_arch_init_vcpu(CPUState *cs)
451 PowerPCCPU *cpu = POWERPC_CPU(cs);
452 CPUPPCState *cenv = &cpu->env;
453 int ret;
455 /* Gather server mmu info from KVM and update the CPU state */
456 kvm_fixup_page_sizes(cpu);
458 /* Synchronize sregs with kvm */
459 ret = kvm_arch_sync_sregs(cpu);
460 if (ret) {
461 return ret;
464 idle_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, kvm_kick_cpu, cpu);
466 /* Some targets support access to KVM's guest TLB. */
467 switch (cenv->mmu_model) {
468 case POWERPC_MMU_BOOKE206:
469 ret = kvm_booke206_tlb_init(cpu);
470 break;
471 default:
472 break;
475 kvm_get_one_reg(cs, KVM_REG_PPC_DEBUG_INST, &debug_inst_opcode);
476 kvmppc_hw_debug_points_init(cenv);
478 return ret;
481 static void kvm_sw_tlb_put(PowerPCCPU *cpu)
483 CPUPPCState *env = &cpu->env;
484 CPUState *cs = CPU(cpu);
485 struct kvm_dirty_tlb dirty_tlb;
486 unsigned char *bitmap;
487 int ret;
489 if (!env->kvm_sw_tlb) {
490 return;
493 bitmap = g_malloc((env->nb_tlb + 7) / 8);
494 memset(bitmap, 0xFF, (env->nb_tlb + 7) / 8);
496 dirty_tlb.bitmap = (uintptr_t)bitmap;
497 dirty_tlb.num_dirty = env->nb_tlb;
499 ret = kvm_vcpu_ioctl(cs, KVM_DIRTY_TLB, &dirty_tlb);
500 if (ret) {
501 fprintf(stderr, "%s: KVM_DIRTY_TLB: %s\n",
502 __func__, strerror(-ret));
505 g_free(bitmap);
508 static void kvm_get_one_spr(CPUState *cs, uint64_t id, int spr)
510 PowerPCCPU *cpu = POWERPC_CPU(cs);
511 CPUPPCState *env = &cpu->env;
512 union {
513 uint32_t u32;
514 uint64_t u64;
515 } val;
516 struct kvm_one_reg reg = {
517 .id = id,
518 .addr = (uintptr_t) &val,
520 int ret;
522 ret = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, &reg);
523 if (ret != 0) {
524 trace_kvm_failed_spr_get(spr, strerror(errno));
525 } else {
526 switch (id & KVM_REG_SIZE_MASK) {
527 case KVM_REG_SIZE_U32:
528 env->spr[spr] = val.u32;
529 break;
531 case KVM_REG_SIZE_U64:
532 env->spr[spr] = val.u64;
533 break;
535 default:
536 /* Don't handle this size yet */
537 abort();
542 static void kvm_put_one_spr(CPUState *cs, uint64_t id, int spr)
544 PowerPCCPU *cpu = POWERPC_CPU(cs);
545 CPUPPCState *env = &cpu->env;
546 union {
547 uint32_t u32;
548 uint64_t u64;
549 } val;
550 struct kvm_one_reg reg = {
551 .id = id,
552 .addr = (uintptr_t) &val,
554 int ret;
556 switch (id & KVM_REG_SIZE_MASK) {
557 case KVM_REG_SIZE_U32:
558 val.u32 = env->spr[spr];
559 break;
561 case KVM_REG_SIZE_U64:
562 val.u64 = env->spr[spr];
563 break;
565 default:
566 /* Don't handle this size yet */
567 abort();
570 ret = kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, &reg);
571 if (ret != 0) {
572 trace_kvm_failed_spr_set(spr, strerror(errno));
576 static int kvm_put_fp(CPUState *cs)
578 PowerPCCPU *cpu = POWERPC_CPU(cs);
579 CPUPPCState *env = &cpu->env;
580 struct kvm_one_reg reg;
581 int i;
582 int ret;
584 if (env->insns_flags & PPC_FLOAT) {
585 uint64_t fpscr = env->fpscr;
586 bool vsx = !!(env->insns_flags2 & PPC2_VSX);
588 reg.id = KVM_REG_PPC_FPSCR;
589 reg.addr = (uintptr_t)&fpscr;
590 ret = kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, &reg);
591 if (ret < 0) {
592 DPRINTF("Unable to set FPSCR to KVM: %s\n", strerror(errno));
593 return ret;
596 for (i = 0; i < 32; i++) {
597 uint64_t vsr[2];
599 vsr[0] = float64_val(env->fpr[i]);
600 vsr[1] = env->vsr[i];
601 reg.addr = (uintptr_t) &vsr;
602 reg.id = vsx ? KVM_REG_PPC_VSR(i) : KVM_REG_PPC_FPR(i);
604 ret = kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, &reg);
605 if (ret < 0) {
606 DPRINTF("Unable to set %s%d to KVM: %s\n", vsx ? "VSR" : "FPR",
607 i, strerror(errno));
608 return ret;
613 if (env->insns_flags & PPC_ALTIVEC) {
614 reg.id = KVM_REG_PPC_VSCR;
615 reg.addr = (uintptr_t)&env->vscr;
616 ret = kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, &reg);
617 if (ret < 0) {
618 DPRINTF("Unable to set VSCR to KVM: %s\n", strerror(errno));
619 return ret;
622 for (i = 0; i < 32; i++) {
623 reg.id = KVM_REG_PPC_VR(i);
624 reg.addr = (uintptr_t)&env->avr[i];
625 ret = kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, &reg);
626 if (ret < 0) {
627 DPRINTF("Unable to set VR%d to KVM: %s\n", i, strerror(errno));
628 return ret;
633 return 0;
636 static int kvm_get_fp(CPUState *cs)
638 PowerPCCPU *cpu = POWERPC_CPU(cs);
639 CPUPPCState *env = &cpu->env;
640 struct kvm_one_reg reg;
641 int i;
642 int ret;
644 if (env->insns_flags & PPC_FLOAT) {
645 uint64_t fpscr;
646 bool vsx = !!(env->insns_flags2 & PPC2_VSX);
648 reg.id = KVM_REG_PPC_FPSCR;
649 reg.addr = (uintptr_t)&fpscr;
650 ret = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, &reg);
651 if (ret < 0) {
652 DPRINTF("Unable to get FPSCR from KVM: %s\n", strerror(errno));
653 return ret;
654 } else {
655 env->fpscr = fpscr;
658 for (i = 0; i < 32; i++) {
659 uint64_t vsr[2];
661 reg.addr = (uintptr_t) &vsr;
662 reg.id = vsx ? KVM_REG_PPC_VSR(i) : KVM_REG_PPC_FPR(i);
664 ret = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, &reg);
665 if (ret < 0) {
666 DPRINTF("Unable to get %s%d from KVM: %s\n",
667 vsx ? "VSR" : "FPR", i, strerror(errno));
668 return ret;
669 } else {
670 env->fpr[i] = vsr[0];
671 if (vsx) {
672 env->vsr[i] = vsr[1];
678 if (env->insns_flags & PPC_ALTIVEC) {
679 reg.id = KVM_REG_PPC_VSCR;
680 reg.addr = (uintptr_t)&env->vscr;
681 ret = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, &reg);
682 if (ret < 0) {
683 DPRINTF("Unable to get VSCR from KVM: %s\n", strerror(errno));
684 return ret;
687 for (i = 0; i < 32; i++) {
688 reg.id = KVM_REG_PPC_VR(i);
689 reg.addr = (uintptr_t)&env->avr[i];
690 ret = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, &reg);
691 if (ret < 0) {
692 DPRINTF("Unable to get VR%d from KVM: %s\n",
693 i, strerror(errno));
694 return ret;
699 return 0;
702 #if defined(TARGET_PPC64)
703 static int kvm_get_vpa(CPUState *cs)
705 PowerPCCPU *cpu = POWERPC_CPU(cs);
706 CPUPPCState *env = &cpu->env;
707 struct kvm_one_reg reg;
708 int ret;
710 reg.id = KVM_REG_PPC_VPA_ADDR;
711 reg.addr = (uintptr_t)&env->vpa_addr;
712 ret = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, &reg);
713 if (ret < 0) {
714 DPRINTF("Unable to get VPA address from KVM: %s\n", strerror(errno));
715 return ret;
718 assert((uintptr_t)&env->slb_shadow_size
719 == ((uintptr_t)&env->slb_shadow_addr + 8));
720 reg.id = KVM_REG_PPC_VPA_SLB;
721 reg.addr = (uintptr_t)&env->slb_shadow_addr;
722 ret = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, &reg);
723 if (ret < 0) {
724 DPRINTF("Unable to get SLB shadow state from KVM: %s\n",
725 strerror(errno));
726 return ret;
729 assert((uintptr_t)&env->dtl_size == ((uintptr_t)&env->dtl_addr + 8));
730 reg.id = KVM_REG_PPC_VPA_DTL;
731 reg.addr = (uintptr_t)&env->dtl_addr;
732 ret = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, &reg);
733 if (ret < 0) {
734 DPRINTF("Unable to get dispatch trace log state from KVM: %s\n",
735 strerror(errno));
736 return ret;
739 return 0;
742 static int kvm_put_vpa(CPUState *cs)
744 PowerPCCPU *cpu = POWERPC_CPU(cs);
745 CPUPPCState *env = &cpu->env;
746 struct kvm_one_reg reg;
747 int ret;
749 /* SLB shadow or DTL can't be registered unless a master VPA is
750 * registered. That means when restoring state, if a VPA *is*
751 * registered, we need to set that up first. If not, we need to
752 * deregister the others before deregistering the master VPA */
753 assert(env->vpa_addr || !(env->slb_shadow_addr || env->dtl_addr));
755 if (env->vpa_addr) {
756 reg.id = KVM_REG_PPC_VPA_ADDR;
757 reg.addr = (uintptr_t)&env->vpa_addr;
758 ret = kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, &reg);
759 if (ret < 0) {
760 DPRINTF("Unable to set VPA address to KVM: %s\n", strerror(errno));
761 return ret;
765 assert((uintptr_t)&env->slb_shadow_size
766 == ((uintptr_t)&env->slb_shadow_addr + 8));
767 reg.id = KVM_REG_PPC_VPA_SLB;
768 reg.addr = (uintptr_t)&env->slb_shadow_addr;
769 ret = kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, &reg);
770 if (ret < 0) {
771 DPRINTF("Unable to set SLB shadow state to KVM: %s\n", strerror(errno));
772 return ret;
775 assert((uintptr_t)&env->dtl_size == ((uintptr_t)&env->dtl_addr + 8));
776 reg.id = KVM_REG_PPC_VPA_DTL;
777 reg.addr = (uintptr_t)&env->dtl_addr;
778 ret = kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, &reg);
779 if (ret < 0) {
780 DPRINTF("Unable to set dispatch trace log state to KVM: %s\n",
781 strerror(errno));
782 return ret;
785 if (!env->vpa_addr) {
786 reg.id = KVM_REG_PPC_VPA_ADDR;
787 reg.addr = (uintptr_t)&env->vpa_addr;
788 ret = kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, &reg);
789 if (ret < 0) {
790 DPRINTF("Unable to set VPA address to KVM: %s\n", strerror(errno));
791 return ret;
795 return 0;
797 #endif /* TARGET_PPC64 */
799 int kvm_arch_put_registers(CPUState *cs, int level)
801 PowerPCCPU *cpu = POWERPC_CPU(cs);
802 CPUPPCState *env = &cpu->env;
803 struct kvm_regs regs;
804 int ret;
805 int i;
807 ret = kvm_vcpu_ioctl(cs, KVM_GET_REGS, &regs);
808 if (ret < 0) {
809 return ret;
812 regs.ctr = env->ctr;
813 regs.lr = env->lr;
814 regs.xer = cpu_read_xer(env);
815 regs.msr = env->msr;
816 regs.pc = env->nip;
818 regs.srr0 = env->spr[SPR_SRR0];
819 regs.srr1 = env->spr[SPR_SRR1];
821 regs.sprg0 = env->spr[SPR_SPRG0];
822 regs.sprg1 = env->spr[SPR_SPRG1];
823 regs.sprg2 = env->spr[SPR_SPRG2];
824 regs.sprg3 = env->spr[SPR_SPRG3];
825 regs.sprg4 = env->spr[SPR_SPRG4];
826 regs.sprg5 = env->spr[SPR_SPRG5];
827 regs.sprg6 = env->spr[SPR_SPRG6];
828 regs.sprg7 = env->spr[SPR_SPRG7];
830 regs.pid = env->spr[SPR_BOOKE_PID];
832 for (i = 0;i < 32; i++)
833 regs.gpr[i] = env->gpr[i];
835 regs.cr = 0;
836 for (i = 0; i < 8; i++) {
837 regs.cr |= (env->crf[i] & 15) << (4 * (7 - i));
840 ret = kvm_vcpu_ioctl(cs, KVM_SET_REGS, &regs);
841 if (ret < 0)
842 return ret;
844 kvm_put_fp(cs);
846 if (env->tlb_dirty) {
847 kvm_sw_tlb_put(cpu);
848 env->tlb_dirty = false;
851 if (cap_segstate && (level >= KVM_PUT_RESET_STATE)) {
852 struct kvm_sregs sregs;
854 sregs.pvr = env->spr[SPR_PVR];
856 sregs.u.s.sdr1 = env->spr[SPR_SDR1];
858 /* Sync SLB */
859 #ifdef TARGET_PPC64
860 for (i = 0; i < ARRAY_SIZE(env->slb); i++) {
861 sregs.u.s.ppc64.slb[i].slbe = env->slb[i].esid;
862 if (env->slb[i].esid & SLB_ESID_V) {
863 sregs.u.s.ppc64.slb[i].slbe |= i;
865 sregs.u.s.ppc64.slb[i].slbv = env->slb[i].vsid;
867 #endif
869 /* Sync SRs */
870 for (i = 0; i < 16; i++) {
871 sregs.u.s.ppc32.sr[i] = env->sr[i];
874 /* Sync BATs */
875 for (i = 0; i < 8; i++) {
876 /* Beware. We have to swap upper and lower bits here */
877 sregs.u.s.ppc32.dbat[i] = ((uint64_t)env->DBAT[0][i] << 32)
878 | env->DBAT[1][i];
879 sregs.u.s.ppc32.ibat[i] = ((uint64_t)env->IBAT[0][i] << 32)
880 | env->IBAT[1][i];
883 ret = kvm_vcpu_ioctl(cs, KVM_SET_SREGS, &sregs);
884 if (ret) {
885 return ret;
889 if (cap_hior && (level >= KVM_PUT_RESET_STATE)) {
890 kvm_put_one_spr(cs, KVM_REG_PPC_HIOR, SPR_HIOR);
893 if (cap_one_reg) {
894 int i;
896 /* We deliberately ignore errors here, for kernels which have
897 * the ONE_REG calls, but don't support the specific
898 * registers, there's a reasonable chance things will still
899 * work, at least until we try to migrate. */
900 for (i = 0; i < 1024; i++) {
901 uint64_t id = env->spr_cb[i].one_reg_id;
903 if (id != 0) {
904 kvm_put_one_spr(cs, id, i);
908 #ifdef TARGET_PPC64
909 if (msr_ts) {
910 for (i = 0; i < ARRAY_SIZE(env->tm_gpr); i++) {
911 kvm_set_one_reg(cs, KVM_REG_PPC_TM_GPR(i), &env->tm_gpr[i]);
913 for (i = 0; i < ARRAY_SIZE(env->tm_vsr); i++) {
914 kvm_set_one_reg(cs, KVM_REG_PPC_TM_VSR(i), &env->tm_vsr[i]);
916 kvm_set_one_reg(cs, KVM_REG_PPC_TM_CR, &env->tm_cr);
917 kvm_set_one_reg(cs, KVM_REG_PPC_TM_LR, &env->tm_lr);
918 kvm_set_one_reg(cs, KVM_REG_PPC_TM_CTR, &env->tm_ctr);
919 kvm_set_one_reg(cs, KVM_REG_PPC_TM_FPSCR, &env->tm_fpscr);
920 kvm_set_one_reg(cs, KVM_REG_PPC_TM_AMR, &env->tm_amr);
921 kvm_set_one_reg(cs, KVM_REG_PPC_TM_PPR, &env->tm_ppr);
922 kvm_set_one_reg(cs, KVM_REG_PPC_TM_VRSAVE, &env->tm_vrsave);
923 kvm_set_one_reg(cs, KVM_REG_PPC_TM_VSCR, &env->tm_vscr);
924 kvm_set_one_reg(cs, KVM_REG_PPC_TM_DSCR, &env->tm_dscr);
925 kvm_set_one_reg(cs, KVM_REG_PPC_TM_TAR, &env->tm_tar);
928 if (cap_papr) {
929 if (kvm_put_vpa(cs) < 0) {
930 DPRINTF("Warning: Unable to set VPA information to KVM\n");
934 kvm_set_one_reg(cs, KVM_REG_PPC_TB_OFFSET, &env->tb_env->tb_offset);
935 #endif /* TARGET_PPC64 */
938 return ret;
941 static void kvm_sync_excp(CPUPPCState *env, int vector, int ivor)
943 env->excp_vectors[vector] = env->spr[ivor] + env->spr[SPR_BOOKE_IVPR];
946 int kvm_arch_get_registers(CPUState *cs)
948 PowerPCCPU *cpu = POWERPC_CPU(cs);
949 CPUPPCState *env = &cpu->env;
950 struct kvm_regs regs;
951 struct kvm_sregs sregs;
952 uint32_t cr;
953 int i, ret;
955 ret = kvm_vcpu_ioctl(cs, KVM_GET_REGS, &regs);
956 if (ret < 0)
957 return ret;
959 cr = regs.cr;
960 for (i = 7; i >= 0; i--) {
961 env->crf[i] = cr & 15;
962 cr >>= 4;
965 env->ctr = regs.ctr;
966 env->lr = regs.lr;
967 cpu_write_xer(env, regs.xer);
968 env->msr = regs.msr;
969 env->nip = regs.pc;
971 env->spr[SPR_SRR0] = regs.srr0;
972 env->spr[SPR_SRR1] = regs.srr1;
974 env->spr[SPR_SPRG0] = regs.sprg0;
975 env->spr[SPR_SPRG1] = regs.sprg1;
976 env->spr[SPR_SPRG2] = regs.sprg2;
977 env->spr[SPR_SPRG3] = regs.sprg3;
978 env->spr[SPR_SPRG4] = regs.sprg4;
979 env->spr[SPR_SPRG5] = regs.sprg5;
980 env->spr[SPR_SPRG6] = regs.sprg6;
981 env->spr[SPR_SPRG7] = regs.sprg7;
983 env->spr[SPR_BOOKE_PID] = regs.pid;
985 for (i = 0;i < 32; i++)
986 env->gpr[i] = regs.gpr[i];
988 kvm_get_fp(cs);
990 if (cap_booke_sregs) {
991 ret = kvm_vcpu_ioctl(cs, KVM_GET_SREGS, &sregs);
992 if (ret < 0) {
993 return ret;
996 if (sregs.u.e.features & KVM_SREGS_E_BASE) {
997 env->spr[SPR_BOOKE_CSRR0] = sregs.u.e.csrr0;
998 env->spr[SPR_BOOKE_CSRR1] = sregs.u.e.csrr1;
999 env->spr[SPR_BOOKE_ESR] = sregs.u.e.esr;
1000 env->spr[SPR_BOOKE_DEAR] = sregs.u.e.dear;
1001 env->spr[SPR_BOOKE_MCSR] = sregs.u.e.mcsr;
1002 env->spr[SPR_BOOKE_TSR] = sregs.u.e.tsr;
1003 env->spr[SPR_BOOKE_TCR] = sregs.u.e.tcr;
1004 env->spr[SPR_DECR] = sregs.u.e.dec;
1005 env->spr[SPR_TBL] = sregs.u.e.tb & 0xffffffff;
1006 env->spr[SPR_TBU] = sregs.u.e.tb >> 32;
1007 env->spr[SPR_VRSAVE] = sregs.u.e.vrsave;
1010 if (sregs.u.e.features & KVM_SREGS_E_ARCH206) {
1011 env->spr[SPR_BOOKE_PIR] = sregs.u.e.pir;
1012 env->spr[SPR_BOOKE_MCSRR0] = sregs.u.e.mcsrr0;
1013 env->spr[SPR_BOOKE_MCSRR1] = sregs.u.e.mcsrr1;
1014 env->spr[SPR_BOOKE_DECAR] = sregs.u.e.decar;
1015 env->spr[SPR_BOOKE_IVPR] = sregs.u.e.ivpr;
1018 if (sregs.u.e.features & KVM_SREGS_E_64) {
1019 env->spr[SPR_BOOKE_EPCR] = sregs.u.e.epcr;
1022 if (sregs.u.e.features & KVM_SREGS_E_SPRG8) {
1023 env->spr[SPR_BOOKE_SPRG8] = sregs.u.e.sprg8;
1026 if (sregs.u.e.features & KVM_SREGS_E_IVOR) {
1027 env->spr[SPR_BOOKE_IVOR0] = sregs.u.e.ivor_low[0];
1028 kvm_sync_excp(env, POWERPC_EXCP_CRITICAL, SPR_BOOKE_IVOR0);
1029 env->spr[SPR_BOOKE_IVOR1] = sregs.u.e.ivor_low[1];
1030 kvm_sync_excp(env, POWERPC_EXCP_MCHECK, SPR_BOOKE_IVOR1);
1031 env->spr[SPR_BOOKE_IVOR2] = sregs.u.e.ivor_low[2];
1032 kvm_sync_excp(env, POWERPC_EXCP_DSI, SPR_BOOKE_IVOR2);
1033 env->spr[SPR_BOOKE_IVOR3] = sregs.u.e.ivor_low[3];
1034 kvm_sync_excp(env, POWERPC_EXCP_ISI, SPR_BOOKE_IVOR3);
1035 env->spr[SPR_BOOKE_IVOR4] = sregs.u.e.ivor_low[4];
1036 kvm_sync_excp(env, POWERPC_EXCP_EXTERNAL, SPR_BOOKE_IVOR4);
1037 env->spr[SPR_BOOKE_IVOR5] = sregs.u.e.ivor_low[5];
1038 kvm_sync_excp(env, POWERPC_EXCP_ALIGN, SPR_BOOKE_IVOR5);
1039 env->spr[SPR_BOOKE_IVOR6] = sregs.u.e.ivor_low[6];
1040 kvm_sync_excp(env, POWERPC_EXCP_PROGRAM, SPR_BOOKE_IVOR6);
1041 env->spr[SPR_BOOKE_IVOR7] = sregs.u.e.ivor_low[7];
1042 kvm_sync_excp(env, POWERPC_EXCP_FPU, SPR_BOOKE_IVOR7);
1043 env->spr[SPR_BOOKE_IVOR8] = sregs.u.e.ivor_low[8];
1044 kvm_sync_excp(env, POWERPC_EXCP_SYSCALL, SPR_BOOKE_IVOR8);
1045 env->spr[SPR_BOOKE_IVOR9] = sregs.u.e.ivor_low[9];
1046 kvm_sync_excp(env, POWERPC_EXCP_APU, SPR_BOOKE_IVOR9);
1047 env->spr[SPR_BOOKE_IVOR10] = sregs.u.e.ivor_low[10];
1048 kvm_sync_excp(env, POWERPC_EXCP_DECR, SPR_BOOKE_IVOR10);
1049 env->spr[SPR_BOOKE_IVOR11] = sregs.u.e.ivor_low[11];
1050 kvm_sync_excp(env, POWERPC_EXCP_FIT, SPR_BOOKE_IVOR11);
1051 env->spr[SPR_BOOKE_IVOR12] = sregs.u.e.ivor_low[12];
1052 kvm_sync_excp(env, POWERPC_EXCP_WDT, SPR_BOOKE_IVOR12);
1053 env->spr[SPR_BOOKE_IVOR13] = sregs.u.e.ivor_low[13];
1054 kvm_sync_excp(env, POWERPC_EXCP_DTLB, SPR_BOOKE_IVOR13);
1055 env->spr[SPR_BOOKE_IVOR14] = sregs.u.e.ivor_low[14];
1056 kvm_sync_excp(env, POWERPC_EXCP_ITLB, SPR_BOOKE_IVOR14);
1057 env->spr[SPR_BOOKE_IVOR15] = sregs.u.e.ivor_low[15];
1058 kvm_sync_excp(env, POWERPC_EXCP_DEBUG, SPR_BOOKE_IVOR15);
1060 if (sregs.u.e.features & KVM_SREGS_E_SPE) {
1061 env->spr[SPR_BOOKE_IVOR32] = sregs.u.e.ivor_high[0];
1062 kvm_sync_excp(env, POWERPC_EXCP_SPEU, SPR_BOOKE_IVOR32);
1063 env->spr[SPR_BOOKE_IVOR33] = sregs.u.e.ivor_high[1];
1064 kvm_sync_excp(env, POWERPC_EXCP_EFPDI, SPR_BOOKE_IVOR33);
1065 env->spr[SPR_BOOKE_IVOR34] = sregs.u.e.ivor_high[2];
1066 kvm_sync_excp(env, POWERPC_EXCP_EFPRI, SPR_BOOKE_IVOR34);
1069 if (sregs.u.e.features & KVM_SREGS_E_PM) {
1070 env->spr[SPR_BOOKE_IVOR35] = sregs.u.e.ivor_high[3];
1071 kvm_sync_excp(env, POWERPC_EXCP_EPERFM, SPR_BOOKE_IVOR35);
1074 if (sregs.u.e.features & KVM_SREGS_E_PC) {
1075 env->spr[SPR_BOOKE_IVOR36] = sregs.u.e.ivor_high[4];
1076 kvm_sync_excp(env, POWERPC_EXCP_DOORI, SPR_BOOKE_IVOR36);
1077 env->spr[SPR_BOOKE_IVOR37] = sregs.u.e.ivor_high[5];
1078 kvm_sync_excp(env, POWERPC_EXCP_DOORCI, SPR_BOOKE_IVOR37);
1082 if (sregs.u.e.features & KVM_SREGS_E_ARCH206_MMU) {
1083 env->spr[SPR_BOOKE_MAS0] = sregs.u.e.mas0;
1084 env->spr[SPR_BOOKE_MAS1] = sregs.u.e.mas1;
1085 env->spr[SPR_BOOKE_MAS2] = sregs.u.e.mas2;
1086 env->spr[SPR_BOOKE_MAS3] = sregs.u.e.mas7_3 & 0xffffffff;
1087 env->spr[SPR_BOOKE_MAS4] = sregs.u.e.mas4;
1088 env->spr[SPR_BOOKE_MAS6] = sregs.u.e.mas6;
1089 env->spr[SPR_BOOKE_MAS7] = sregs.u.e.mas7_3 >> 32;
1090 env->spr[SPR_MMUCFG] = sregs.u.e.mmucfg;
1091 env->spr[SPR_BOOKE_TLB0CFG] = sregs.u.e.tlbcfg[0];
1092 env->spr[SPR_BOOKE_TLB1CFG] = sregs.u.e.tlbcfg[1];
1095 if (sregs.u.e.features & KVM_SREGS_EXP) {
1096 env->spr[SPR_BOOKE_EPR] = sregs.u.e.epr;
1099 if (sregs.u.e.features & KVM_SREGS_E_PD) {
1100 env->spr[SPR_BOOKE_EPLC] = sregs.u.e.eplc;
1101 env->spr[SPR_BOOKE_EPSC] = sregs.u.e.epsc;
1104 if (sregs.u.e.impl_id == KVM_SREGS_E_IMPL_FSL) {
1105 env->spr[SPR_E500_SVR] = sregs.u.e.impl.fsl.svr;
1106 env->spr[SPR_Exxx_MCAR] = sregs.u.e.impl.fsl.mcar;
1107 env->spr[SPR_HID0] = sregs.u.e.impl.fsl.hid0;
1109 if (sregs.u.e.impl.fsl.features & KVM_SREGS_E_FSL_PIDn) {
1110 env->spr[SPR_BOOKE_PID1] = sregs.u.e.impl.fsl.pid1;
1111 env->spr[SPR_BOOKE_PID2] = sregs.u.e.impl.fsl.pid2;
1116 if (cap_segstate) {
1117 ret = kvm_vcpu_ioctl(cs, KVM_GET_SREGS, &sregs);
1118 if (ret < 0) {
1119 return ret;
1122 if (!env->external_htab) {
1123 ppc_store_sdr1(env, sregs.u.s.sdr1);
1126 /* Sync SLB */
1127 #ifdef TARGET_PPC64
1129 * The packed SLB array we get from KVM_GET_SREGS only contains
1130 * information about valid entries. So we flush our internal
1131 * copy to get rid of stale ones, then put all valid SLB entries
1132 * back in.
1134 memset(env->slb, 0, sizeof(env->slb));
1135 for (i = 0; i < ARRAY_SIZE(env->slb); i++) {
1136 target_ulong rb = sregs.u.s.ppc64.slb[i].slbe;
1137 target_ulong rs = sregs.u.s.ppc64.slb[i].slbv;
1139 * Only restore valid entries
1141 if (rb & SLB_ESID_V) {
1142 ppc_store_slb(env, rb, rs);
1145 #endif
1147 /* Sync SRs */
1148 for (i = 0; i < 16; i++) {
1149 env->sr[i] = sregs.u.s.ppc32.sr[i];
1152 /* Sync BATs */
1153 for (i = 0; i < 8; i++) {
1154 env->DBAT[0][i] = sregs.u.s.ppc32.dbat[i] & 0xffffffff;
1155 env->DBAT[1][i] = sregs.u.s.ppc32.dbat[i] >> 32;
1156 env->IBAT[0][i] = sregs.u.s.ppc32.ibat[i] & 0xffffffff;
1157 env->IBAT[1][i] = sregs.u.s.ppc32.ibat[i] >> 32;
1161 if (cap_hior) {
1162 kvm_get_one_spr(cs, KVM_REG_PPC_HIOR, SPR_HIOR);
1165 if (cap_one_reg) {
1166 int i;
1168 /* We deliberately ignore errors here, for kernels which have
1169 * the ONE_REG calls, but don't support the specific
1170 * registers, there's a reasonable chance things will still
1171 * work, at least until we try to migrate. */
1172 for (i = 0; i < 1024; i++) {
1173 uint64_t id = env->spr_cb[i].one_reg_id;
1175 if (id != 0) {
1176 kvm_get_one_spr(cs, id, i);
1180 #ifdef TARGET_PPC64
1181 if (msr_ts) {
1182 for (i = 0; i < ARRAY_SIZE(env->tm_gpr); i++) {
1183 kvm_get_one_reg(cs, KVM_REG_PPC_TM_GPR(i), &env->tm_gpr[i]);
1185 for (i = 0; i < ARRAY_SIZE(env->tm_vsr); i++) {
1186 kvm_get_one_reg(cs, KVM_REG_PPC_TM_VSR(i), &env->tm_vsr[i]);
1188 kvm_get_one_reg(cs, KVM_REG_PPC_TM_CR, &env->tm_cr);
1189 kvm_get_one_reg(cs, KVM_REG_PPC_TM_LR, &env->tm_lr);
1190 kvm_get_one_reg(cs, KVM_REG_PPC_TM_CTR, &env->tm_ctr);
1191 kvm_get_one_reg(cs, KVM_REG_PPC_TM_FPSCR, &env->tm_fpscr);
1192 kvm_get_one_reg(cs, KVM_REG_PPC_TM_AMR, &env->tm_amr);
1193 kvm_get_one_reg(cs, KVM_REG_PPC_TM_PPR, &env->tm_ppr);
1194 kvm_get_one_reg(cs, KVM_REG_PPC_TM_VRSAVE, &env->tm_vrsave);
1195 kvm_get_one_reg(cs, KVM_REG_PPC_TM_VSCR, &env->tm_vscr);
1196 kvm_get_one_reg(cs, KVM_REG_PPC_TM_DSCR, &env->tm_dscr);
1197 kvm_get_one_reg(cs, KVM_REG_PPC_TM_TAR, &env->tm_tar);
1200 if (cap_papr) {
1201 if (kvm_get_vpa(cs) < 0) {
1202 DPRINTF("Warning: Unable to get VPA information from KVM\n");
1206 kvm_get_one_reg(cs, KVM_REG_PPC_TB_OFFSET, &env->tb_env->tb_offset);
1207 #endif
1210 return 0;
1213 int kvmppc_set_interrupt(PowerPCCPU *cpu, int irq, int level)
1215 unsigned virq = level ? KVM_INTERRUPT_SET_LEVEL : KVM_INTERRUPT_UNSET;
1217 if (irq != PPC_INTERRUPT_EXT) {
1218 return 0;
1221 if (!kvm_enabled() || !cap_interrupt_unset || !cap_interrupt_level) {
1222 return 0;
1225 kvm_vcpu_ioctl(CPU(cpu), KVM_INTERRUPT, &virq);
1227 return 0;
1230 #if defined(TARGET_PPCEMB)
1231 #define PPC_INPUT_INT PPC40x_INPUT_INT
1232 #elif defined(TARGET_PPC64)
1233 #define PPC_INPUT_INT PPC970_INPUT_INT
1234 #else
1235 #define PPC_INPUT_INT PPC6xx_INPUT_INT
1236 #endif
1238 void kvm_arch_pre_run(CPUState *cs, struct kvm_run *run)
1240 PowerPCCPU *cpu = POWERPC_CPU(cs);
1241 CPUPPCState *env = &cpu->env;
1242 int r;
1243 unsigned irq;
1245 /* PowerPC QEMU tracks the various core input pins (interrupt, critical
1246 * interrupt, reset, etc) in PPC-specific env->irq_input_state. */
1247 if (!cap_interrupt_level &&
1248 run->ready_for_interrupt_injection &&
1249 (cs->interrupt_request & CPU_INTERRUPT_HARD) &&
1250 (env->irq_input_state & (1<<PPC_INPUT_INT)))
1252 /* For now KVM disregards the 'irq' argument. However, in the
1253 * future KVM could cache it in-kernel to avoid a heavyweight exit
1254 * when reading the UIC.
1256 irq = KVM_INTERRUPT_SET;
1258 DPRINTF("injected interrupt %d\n", irq);
1259 r = kvm_vcpu_ioctl(cs, KVM_INTERRUPT, &irq);
1260 if (r < 0) {
1261 printf("cpu %d fail inject %x\n", cs->cpu_index, irq);
1264 /* Always wake up soon in case the interrupt was level based */
1265 timer_mod(idle_timer, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) +
1266 (get_ticks_per_sec() / 50));
1269 /* We don't know if there are more interrupts pending after this. However,
1270 * the guest will return to userspace in the course of handling this one
1271 * anyways, so we will get a chance to deliver the rest. */
1274 MemTxAttrs kvm_arch_post_run(CPUState *cs, struct kvm_run *run)
1276 return MEMTXATTRS_UNSPECIFIED;
1279 int kvm_arch_process_async_events(CPUState *cs)
1281 return cs->halted;
1284 static int kvmppc_handle_halt(PowerPCCPU *cpu)
1286 CPUState *cs = CPU(cpu);
1287 CPUPPCState *env = &cpu->env;
1289 if (!(cs->interrupt_request & CPU_INTERRUPT_HARD) && (msr_ee)) {
1290 cs->halted = 1;
1291 cs->exception_index = EXCP_HLT;
1294 return 0;
1297 /* map dcr access to existing qemu dcr emulation */
1298 static int kvmppc_handle_dcr_read(CPUPPCState *env, uint32_t dcrn, uint32_t *data)
1300 if (ppc_dcr_read(env->dcr_env, dcrn, data) < 0)
1301 fprintf(stderr, "Read to unhandled DCR (0x%x)\n", dcrn);
1303 return 0;
1306 static int kvmppc_handle_dcr_write(CPUPPCState *env, uint32_t dcrn, uint32_t data)
1308 if (ppc_dcr_write(env->dcr_env, dcrn, data) < 0)
1309 fprintf(stderr, "Write to unhandled DCR (0x%x)\n", dcrn);
1311 return 0;
1314 int kvm_arch_insert_sw_breakpoint(CPUState *cs, struct kvm_sw_breakpoint *bp)
1316 /* Mixed endian case is not handled */
1317 uint32_t sc = debug_inst_opcode;
1319 if (cpu_memory_rw_debug(cs, bp->pc, (uint8_t *)&bp->saved_insn,
1320 sizeof(sc), 0) ||
1321 cpu_memory_rw_debug(cs, bp->pc, (uint8_t *)&sc, sizeof(sc), 1)) {
1322 return -EINVAL;
1325 return 0;
1328 int kvm_arch_remove_sw_breakpoint(CPUState *cs, struct kvm_sw_breakpoint *bp)
1330 uint32_t sc;
1332 if (cpu_memory_rw_debug(cs, bp->pc, (uint8_t *)&sc, sizeof(sc), 0) ||
1333 sc != debug_inst_opcode ||
1334 cpu_memory_rw_debug(cs, bp->pc, (uint8_t *)&bp->saved_insn,
1335 sizeof(sc), 1)) {
1336 return -EINVAL;
1339 return 0;
1342 static int find_hw_breakpoint(target_ulong addr, int type)
1344 int n;
1346 assert((nb_hw_breakpoint + nb_hw_watchpoint)
1347 <= ARRAY_SIZE(hw_debug_points));
1349 for (n = 0; n < nb_hw_breakpoint + nb_hw_watchpoint; n++) {
1350 if (hw_debug_points[n].addr == addr &&
1351 hw_debug_points[n].type == type) {
1352 return n;
1356 return -1;
1359 static int find_hw_watchpoint(target_ulong addr, int *flag)
1361 int n;
1363 n = find_hw_breakpoint(addr, GDB_WATCHPOINT_ACCESS);
1364 if (n >= 0) {
1365 *flag = BP_MEM_ACCESS;
1366 return n;
1369 n = find_hw_breakpoint(addr, GDB_WATCHPOINT_WRITE);
1370 if (n >= 0) {
1371 *flag = BP_MEM_WRITE;
1372 return n;
1375 n = find_hw_breakpoint(addr, GDB_WATCHPOINT_READ);
1376 if (n >= 0) {
1377 *flag = BP_MEM_READ;
1378 return n;
1381 return -1;
1384 int kvm_arch_insert_hw_breakpoint(target_ulong addr,
1385 target_ulong len, int type)
1387 if ((nb_hw_breakpoint + nb_hw_watchpoint) >= ARRAY_SIZE(hw_debug_points)) {
1388 return -ENOBUFS;
1391 hw_debug_points[nb_hw_breakpoint + nb_hw_watchpoint].addr = addr;
1392 hw_debug_points[nb_hw_breakpoint + nb_hw_watchpoint].type = type;
1394 switch (type) {
1395 case GDB_BREAKPOINT_HW:
1396 if (nb_hw_breakpoint >= max_hw_breakpoint) {
1397 return -ENOBUFS;
1400 if (find_hw_breakpoint(addr, type) >= 0) {
1401 return -EEXIST;
1404 nb_hw_breakpoint++;
1405 break;
1407 case GDB_WATCHPOINT_WRITE:
1408 case GDB_WATCHPOINT_READ:
1409 case GDB_WATCHPOINT_ACCESS:
1410 if (nb_hw_watchpoint >= max_hw_watchpoint) {
1411 return -ENOBUFS;
1414 if (find_hw_breakpoint(addr, type) >= 0) {
1415 return -EEXIST;
1418 nb_hw_watchpoint++;
1419 break;
1421 default:
1422 return -ENOSYS;
1425 return 0;
1428 int kvm_arch_remove_hw_breakpoint(target_ulong addr,
1429 target_ulong len, int type)
1431 int n;
1433 n = find_hw_breakpoint(addr, type);
1434 if (n < 0) {
1435 return -ENOENT;
1438 switch (type) {
1439 case GDB_BREAKPOINT_HW:
1440 nb_hw_breakpoint--;
1441 break;
1443 case GDB_WATCHPOINT_WRITE:
1444 case GDB_WATCHPOINT_READ:
1445 case GDB_WATCHPOINT_ACCESS:
1446 nb_hw_watchpoint--;
1447 break;
1449 default:
1450 return -ENOSYS;
1452 hw_debug_points[n] = hw_debug_points[nb_hw_breakpoint + nb_hw_watchpoint];
1454 return 0;
1457 void kvm_arch_remove_all_hw_breakpoints(void)
1459 nb_hw_breakpoint = nb_hw_watchpoint = 0;
1462 void kvm_arch_update_guest_debug(CPUState *cs, struct kvm_guest_debug *dbg)
1464 int n;
1466 /* Software Breakpoint updates */
1467 if (kvm_sw_breakpoints_active(cs)) {
1468 dbg->control |= KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP;
1471 assert((nb_hw_breakpoint + nb_hw_watchpoint)
1472 <= ARRAY_SIZE(hw_debug_points));
1473 assert((nb_hw_breakpoint + nb_hw_watchpoint) <= ARRAY_SIZE(dbg->arch.bp));
1475 if (nb_hw_breakpoint + nb_hw_watchpoint > 0) {
1476 dbg->control |= KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_HW_BP;
1477 memset(dbg->arch.bp, 0, sizeof(dbg->arch.bp));
1478 for (n = 0; n < nb_hw_breakpoint + nb_hw_watchpoint; n++) {
1479 switch (hw_debug_points[n].type) {
1480 case GDB_BREAKPOINT_HW:
1481 dbg->arch.bp[n].type = KVMPPC_DEBUG_BREAKPOINT;
1482 break;
1483 case GDB_WATCHPOINT_WRITE:
1484 dbg->arch.bp[n].type = KVMPPC_DEBUG_WATCH_WRITE;
1485 break;
1486 case GDB_WATCHPOINT_READ:
1487 dbg->arch.bp[n].type = KVMPPC_DEBUG_WATCH_READ;
1488 break;
1489 case GDB_WATCHPOINT_ACCESS:
1490 dbg->arch.bp[n].type = KVMPPC_DEBUG_WATCH_WRITE |
1491 KVMPPC_DEBUG_WATCH_READ;
1492 break;
1493 default:
1494 cpu_abort(cs, "Unsupported breakpoint type\n");
1496 dbg->arch.bp[n].addr = hw_debug_points[n].addr;
1501 static int kvm_handle_debug(PowerPCCPU *cpu, struct kvm_run *run)
1503 CPUState *cs = CPU(cpu);
1504 CPUPPCState *env = &cpu->env;
1505 struct kvm_debug_exit_arch *arch_info = &run->debug.arch;
1506 int handle = 0;
1507 int n;
1508 int flag = 0;
1510 if (cs->singlestep_enabled) {
1511 handle = 1;
1512 } else if (arch_info->status) {
1513 if (nb_hw_breakpoint + nb_hw_watchpoint > 0) {
1514 if (arch_info->status & KVMPPC_DEBUG_BREAKPOINT) {
1515 n = find_hw_breakpoint(arch_info->address, GDB_BREAKPOINT_HW);
1516 if (n >= 0) {
1517 handle = 1;
1519 } else if (arch_info->status & (KVMPPC_DEBUG_WATCH_READ |
1520 KVMPPC_DEBUG_WATCH_WRITE)) {
1521 n = find_hw_watchpoint(arch_info->address, &flag);
1522 if (n >= 0) {
1523 handle = 1;
1524 cs->watchpoint_hit = &hw_watchpoint;
1525 hw_watchpoint.vaddr = hw_debug_points[n].addr;
1526 hw_watchpoint.flags = flag;
1530 } else if (kvm_find_sw_breakpoint(cs, arch_info->address)) {
1531 handle = 1;
1532 } else {
1533 /* QEMU is not able to handle debug exception, so inject
1534 * program exception to guest;
1535 * Yes program exception NOT debug exception !!
1536 * When QEMU is using debug resources then debug exception must
1537 * be always set. To achieve this we set MSR_DE and also set
1538 * MSRP_DEP so guest cannot change MSR_DE.
1539 * When emulating debug resource for guest we want guest
1540 * to control MSR_DE (enable/disable debug interrupt on need).
1541 * Supporting both configurations are NOT possible.
1542 * So the result is that we cannot share debug resources
1543 * between QEMU and Guest on BOOKE architecture.
1544 * In the current design QEMU gets the priority over guest,
1545 * this means that if QEMU is using debug resources then guest
1546 * cannot use them;
1547 * For software breakpoint QEMU uses a privileged instruction;
1548 * So there cannot be any reason that we are here for guest
1549 * set debug exception, only possibility is guest executed a
1550 * privileged / illegal instruction and that's why we are
1551 * injecting a program interrupt.
1554 cpu_synchronize_state(cs);
1555 /* env->nip is PC, so increment this by 4 to use
1556 * ppc_cpu_do_interrupt(), which set srr0 = env->nip - 4.
1558 env->nip += 4;
1559 cs->exception_index = POWERPC_EXCP_PROGRAM;
1560 env->error_code = POWERPC_EXCP_INVAL;
1561 ppc_cpu_do_interrupt(cs);
1564 return handle;
1567 int kvm_arch_handle_exit(CPUState *cs, struct kvm_run *run)
1569 PowerPCCPU *cpu = POWERPC_CPU(cs);
1570 CPUPPCState *env = &cpu->env;
1571 int ret;
1573 switch (run->exit_reason) {
1574 case KVM_EXIT_DCR:
1575 if (run->dcr.is_write) {
1576 DPRINTF("handle dcr write\n");
1577 ret = kvmppc_handle_dcr_write(env, run->dcr.dcrn, run->dcr.data);
1578 } else {
1579 DPRINTF("handle dcr read\n");
1580 ret = kvmppc_handle_dcr_read(env, run->dcr.dcrn, &run->dcr.data);
1582 break;
1583 case KVM_EXIT_HLT:
1584 DPRINTF("handle halt\n");
1585 ret = kvmppc_handle_halt(cpu);
1586 break;
1587 #if defined(TARGET_PPC64)
1588 case KVM_EXIT_PAPR_HCALL:
1589 DPRINTF("handle PAPR hypercall\n");
1590 run->papr_hcall.ret = spapr_hypercall(cpu,
1591 run->papr_hcall.nr,
1592 run->papr_hcall.args);
1593 ret = 0;
1594 break;
1595 #endif
1596 case KVM_EXIT_EPR:
1597 DPRINTF("handle epr\n");
1598 run->epr.epr = ldl_phys(cs->as, env->mpic_iack);
1599 ret = 0;
1600 break;
1601 case KVM_EXIT_WATCHDOG:
1602 DPRINTF("handle watchdog expiry\n");
1603 watchdog_perform_action();
1604 ret = 0;
1605 break;
1607 case KVM_EXIT_DEBUG:
1608 DPRINTF("handle debug exception\n");
1609 if (kvm_handle_debug(cpu, run)) {
1610 ret = EXCP_DEBUG;
1611 break;
1613 /* re-enter, this exception was guest-internal */
1614 ret = 0;
1615 break;
1617 default:
1618 fprintf(stderr, "KVM: unknown exit reason %d\n", run->exit_reason);
1619 ret = -1;
1620 break;
1623 return ret;
1626 int kvmppc_or_tsr_bits(PowerPCCPU *cpu, uint32_t tsr_bits)
1628 CPUState *cs = CPU(cpu);
1629 uint32_t bits = tsr_bits;
1630 struct kvm_one_reg reg = {
1631 .id = KVM_REG_PPC_OR_TSR,
1632 .addr = (uintptr_t) &bits,
1635 return kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, &reg);
1638 int kvmppc_clear_tsr_bits(PowerPCCPU *cpu, uint32_t tsr_bits)
1641 CPUState *cs = CPU(cpu);
1642 uint32_t bits = tsr_bits;
1643 struct kvm_one_reg reg = {
1644 .id = KVM_REG_PPC_CLEAR_TSR,
1645 .addr = (uintptr_t) &bits,
1648 return kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, &reg);
1651 int kvmppc_set_tcr(PowerPCCPU *cpu)
1653 CPUState *cs = CPU(cpu);
1654 CPUPPCState *env = &cpu->env;
1655 uint32_t tcr = env->spr[SPR_BOOKE_TCR];
1657 struct kvm_one_reg reg = {
1658 .id = KVM_REG_PPC_TCR,
1659 .addr = (uintptr_t) &tcr,
1662 return kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, &reg);
1665 int kvmppc_booke_watchdog_enable(PowerPCCPU *cpu)
1667 CPUState *cs = CPU(cpu);
1668 int ret;
1670 if (!kvm_enabled()) {
1671 return -1;
1674 if (!cap_ppc_watchdog) {
1675 printf("warning: KVM does not support watchdog");
1676 return -1;
1679 ret = kvm_vcpu_enable_cap(cs, KVM_CAP_PPC_BOOKE_WATCHDOG, 0);
1680 if (ret < 0) {
1681 fprintf(stderr, "%s: couldn't enable KVM_CAP_PPC_BOOKE_WATCHDOG: %s\n",
1682 __func__, strerror(-ret));
1683 return ret;
1686 return ret;
1689 static int read_cpuinfo(const char *field, char *value, int len)
1691 FILE *f;
1692 int ret = -1;
1693 int field_len = strlen(field);
1694 char line[512];
1696 f = fopen("/proc/cpuinfo", "r");
1697 if (!f) {
1698 return -1;
1701 do {
1702 if (!fgets(line, sizeof(line), f)) {
1703 break;
1705 if (!strncmp(line, field, field_len)) {
1706 pstrcpy(value, len, line);
1707 ret = 0;
1708 break;
1710 } while(*line);
1712 fclose(f);
1714 return ret;
1717 uint32_t kvmppc_get_tbfreq(void)
1719 char line[512];
1720 char *ns;
1721 uint32_t retval = get_ticks_per_sec();
1723 if (read_cpuinfo("timebase", line, sizeof(line))) {
1724 return retval;
1727 if (!(ns = strchr(line, ':'))) {
1728 return retval;
1731 ns++;
1733 retval = atoi(ns);
1734 return retval;
1737 bool kvmppc_get_host_serial(char **value)
1739 return g_file_get_contents("/proc/device-tree/system-id", value, NULL,
1740 NULL);
1743 bool kvmppc_get_host_model(char **value)
1745 return g_file_get_contents("/proc/device-tree/model", value, NULL, NULL);
1748 /* Try to find a device tree node for a CPU with clock-frequency property */
1749 static int kvmppc_find_cpu_dt(char *buf, int buf_len)
1751 struct dirent *dirp;
1752 DIR *dp;
1754 if ((dp = opendir(PROC_DEVTREE_CPU)) == NULL) {
1755 printf("Can't open directory " PROC_DEVTREE_CPU "\n");
1756 return -1;
1759 buf[0] = '\0';
1760 while ((dirp = readdir(dp)) != NULL) {
1761 FILE *f;
1762 snprintf(buf, buf_len, "%s%s/clock-frequency", PROC_DEVTREE_CPU,
1763 dirp->d_name);
1764 f = fopen(buf, "r");
1765 if (f) {
1766 snprintf(buf, buf_len, "%s%s", PROC_DEVTREE_CPU, dirp->d_name);
1767 fclose(f);
1768 break;
1770 buf[0] = '\0';
1772 closedir(dp);
1773 if (buf[0] == '\0') {
1774 printf("Unknown host!\n");
1775 return -1;
1778 return 0;
1781 /* Read a CPU node property from the host device tree that's a single
1782 * integer (32-bit or 64-bit). Returns 0 if anything goes wrong
1783 * (can't find or open the property, or doesn't understand the
1784 * format) */
1785 static uint64_t kvmppc_read_int_cpu_dt(const char *propname)
1787 char buf[PATH_MAX], *tmp;
1788 union {
1789 uint32_t v32;
1790 uint64_t v64;
1791 } u;
1792 FILE *f;
1793 int len;
1795 if (kvmppc_find_cpu_dt(buf, sizeof(buf))) {
1796 return -1;
1799 tmp = g_strdup_printf("%s/%s", buf, propname);
1801 f = fopen(tmp, "rb");
1802 g_free(tmp);
1803 if (!f) {
1804 return -1;
1807 len = fread(&u, 1, sizeof(u), f);
1808 fclose(f);
1809 switch (len) {
1810 case 4:
1811 /* property is a 32-bit quantity */
1812 return be32_to_cpu(u.v32);
1813 case 8:
1814 return be64_to_cpu(u.v64);
1817 return 0;
1820 uint64_t kvmppc_get_clockfreq(void)
1822 return kvmppc_read_int_cpu_dt("clock-frequency");
1825 uint32_t kvmppc_get_vmx(void)
1827 return kvmppc_read_int_cpu_dt("ibm,vmx");
1830 uint32_t kvmppc_get_dfp(void)
1832 return kvmppc_read_int_cpu_dt("ibm,dfp");
1835 static int kvmppc_get_pvinfo(CPUPPCState *env, struct kvm_ppc_pvinfo *pvinfo)
1837 PowerPCCPU *cpu = ppc_env_get_cpu(env);
1838 CPUState *cs = CPU(cpu);
1840 if (kvm_vm_check_extension(cs->kvm_state, KVM_CAP_PPC_GET_PVINFO) &&
1841 !kvm_vm_ioctl(cs->kvm_state, KVM_PPC_GET_PVINFO, pvinfo)) {
1842 return 0;
1845 return 1;
1848 int kvmppc_get_hasidle(CPUPPCState *env)
1850 struct kvm_ppc_pvinfo pvinfo;
1852 if (!kvmppc_get_pvinfo(env, &pvinfo) &&
1853 (pvinfo.flags & KVM_PPC_PVINFO_FLAGS_EV_IDLE)) {
1854 return 1;
1857 return 0;
1860 int kvmppc_get_hypercall(CPUPPCState *env, uint8_t *buf, int buf_len)
1862 uint32_t *hc = (uint32_t*)buf;
1863 struct kvm_ppc_pvinfo pvinfo;
1865 if (!kvmppc_get_pvinfo(env, &pvinfo)) {
1866 memcpy(buf, pvinfo.hcall, buf_len);
1867 return 0;
1871 * Fallback to always fail hypercalls regardless of endianness:
1873 * tdi 0,r0,72 (becomes b .+8 in wrong endian, nop in good endian)
1874 * li r3, -1
1875 * b .+8 (becomes nop in wrong endian)
1876 * bswap32(li r3, -1)
1879 hc[0] = cpu_to_be32(0x08000048);
1880 hc[1] = cpu_to_be32(0x3860ffff);
1881 hc[2] = cpu_to_be32(0x48000008);
1882 hc[3] = cpu_to_be32(bswap32(0x3860ffff));
1884 return 0;
1887 void kvmppc_set_papr(PowerPCCPU *cpu)
1889 CPUState *cs = CPU(cpu);
1890 int ret;
1892 ret = kvm_vcpu_enable_cap(cs, KVM_CAP_PPC_PAPR, 0);
1893 if (ret) {
1894 cpu_abort(cs, "This KVM version does not support PAPR\n");
1897 /* Update the capability flag so we sync the right information
1898 * with kvm */
1899 cap_papr = 1;
1902 int kvmppc_set_compat(PowerPCCPU *cpu, uint32_t cpu_version)
1904 return kvm_set_one_reg(CPU(cpu), KVM_REG_PPC_ARCH_COMPAT, &cpu_version);
1907 void kvmppc_set_mpic_proxy(PowerPCCPU *cpu, int mpic_proxy)
1909 CPUState *cs = CPU(cpu);
1910 int ret;
1912 ret = kvm_vcpu_enable_cap(cs, KVM_CAP_PPC_EPR, 0, mpic_proxy);
1913 if (ret && mpic_proxy) {
1914 cpu_abort(cs, "This KVM version does not support EPR\n");
1918 int kvmppc_smt_threads(void)
1920 return cap_ppc_smt ? cap_ppc_smt : 1;
1923 #ifdef TARGET_PPC64
1924 off_t kvmppc_alloc_rma(void **rma)
1926 off_t size;
1927 int fd;
1928 struct kvm_allocate_rma ret;
1930 /* If cap_ppc_rma == 0, contiguous RMA allocation is not supported
1931 * if cap_ppc_rma == 1, contiguous RMA allocation is supported, but
1932 * not necessary on this hardware
1933 * if cap_ppc_rma == 2, contiguous RMA allocation is needed on this hardware
1935 * FIXME: We should allow the user to force contiguous RMA
1936 * allocation in the cap_ppc_rma==1 case.
1938 if (cap_ppc_rma < 2) {
1939 return 0;
1942 fd = kvm_vm_ioctl(kvm_state, KVM_ALLOCATE_RMA, &ret);
1943 if (fd < 0) {
1944 fprintf(stderr, "KVM: Error on KVM_ALLOCATE_RMA: %s\n",
1945 strerror(errno));
1946 return -1;
1949 size = MIN(ret.rma_size, 256ul << 20);
1951 *rma = mmap(NULL, size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
1952 if (*rma == MAP_FAILED) {
1953 fprintf(stderr, "KVM: Error mapping RMA: %s\n", strerror(errno));
1954 return -1;
1957 return size;
1960 uint64_t kvmppc_rma_size(uint64_t current_size, unsigned int hash_shift)
1962 struct kvm_ppc_smmu_info info;
1963 long rampagesize, best_page_shift;
1964 int i;
1966 if (cap_ppc_rma >= 2) {
1967 return current_size;
1970 /* Find the largest hardware supported page size that's less than
1971 * or equal to the (logical) backing page size of guest RAM */
1972 kvm_get_smmu_info(POWERPC_CPU(first_cpu), &info);
1973 rampagesize = getrampagesize();
1974 best_page_shift = 0;
1976 for (i = 0; i < KVM_PPC_PAGE_SIZES_MAX_SZ; i++) {
1977 struct kvm_ppc_one_seg_page_size *sps = &info.sps[i];
1979 if (!sps->page_shift) {
1980 continue;
1983 if ((sps->page_shift > best_page_shift)
1984 && ((1UL << sps->page_shift) <= rampagesize)) {
1985 best_page_shift = sps->page_shift;
1989 return MIN(current_size,
1990 1ULL << (best_page_shift + hash_shift - 7));
1992 #endif
1994 bool kvmppc_spapr_use_multitce(void)
1996 return cap_spapr_multitce;
1999 void *kvmppc_create_spapr_tce(uint32_t liobn, uint32_t window_size, int *pfd,
2000 bool vfio_accel)
2002 struct kvm_create_spapr_tce args = {
2003 .liobn = liobn,
2004 .window_size = window_size,
2006 long len;
2007 int fd;
2008 void *table;
2010 /* Must set fd to -1 so we don't try to munmap when called for
2011 * destroying the table, which the upper layers -will- do
2013 *pfd = -1;
2014 if (!cap_spapr_tce || (vfio_accel && !cap_spapr_vfio)) {
2015 return NULL;
2018 fd = kvm_vm_ioctl(kvm_state, KVM_CREATE_SPAPR_TCE, &args);
2019 if (fd < 0) {
2020 fprintf(stderr, "KVM: Failed to create TCE table for liobn 0x%x\n",
2021 liobn);
2022 return NULL;
2025 len = (window_size / SPAPR_TCE_PAGE_SIZE) * sizeof(uint64_t);
2026 /* FIXME: round this up to page size */
2028 table = mmap(NULL, len, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
2029 if (table == MAP_FAILED) {
2030 fprintf(stderr, "KVM: Failed to map TCE table for liobn 0x%x\n",
2031 liobn);
2032 close(fd);
2033 return NULL;
2036 *pfd = fd;
2037 return table;
2040 int kvmppc_remove_spapr_tce(void *table, int fd, uint32_t nb_table)
2042 long len;
2044 if (fd < 0) {
2045 return -1;
2048 len = nb_table * sizeof(uint64_t);
2049 if ((munmap(table, len) < 0) ||
2050 (close(fd) < 0)) {
2051 fprintf(stderr, "KVM: Unexpected error removing TCE table: %s",
2052 strerror(errno));
2053 /* Leak the table */
2056 return 0;
2059 int kvmppc_reset_htab(int shift_hint)
2061 uint32_t shift = shift_hint;
2063 if (!kvm_enabled()) {
2064 /* Full emulation, tell caller to allocate htab itself */
2065 return 0;
2067 if (kvm_check_extension(kvm_state, KVM_CAP_PPC_ALLOC_HTAB)) {
2068 int ret;
2069 ret = kvm_vm_ioctl(kvm_state, KVM_PPC_ALLOCATE_HTAB, &shift);
2070 if (ret == -ENOTTY) {
2071 /* At least some versions of PR KVM advertise the
2072 * capability, but don't implement the ioctl(). Oops.
2073 * Return 0 so that we allocate the htab in qemu, as is
2074 * correct for PR. */
2075 return 0;
2076 } else if (ret < 0) {
2077 return ret;
2079 return shift;
2082 /* We have a kernel that predates the htab reset calls. For PR
2083 * KVM, we need to allocate the htab ourselves, for an HV KVM of
2084 * this era, it has allocated a 16MB fixed size hash table
2085 * already. Kernels of this era have the GET_PVINFO capability
2086 * only on PR, so we use this hack to determine the right
2087 * answer */
2088 if (kvm_check_extension(kvm_state, KVM_CAP_PPC_GET_PVINFO)) {
2089 /* PR - tell caller to allocate htab */
2090 return 0;
2091 } else {
2092 /* HV - assume 16MB kernel allocated htab */
2093 return 24;
2097 static inline uint32_t mfpvr(void)
2099 uint32_t pvr;
2101 asm ("mfpvr %0"
2102 : "=r"(pvr));
2103 return pvr;
2106 static void alter_insns(uint64_t *word, uint64_t flags, bool on)
2108 if (on) {
2109 *word |= flags;
2110 } else {
2111 *word &= ~flags;
2115 static void kvmppc_host_cpu_initfn(Object *obj)
2117 assert(kvm_enabled());
2120 static void kvmppc_host_cpu_class_init(ObjectClass *oc, void *data)
2122 PowerPCCPUClass *pcc = POWERPC_CPU_CLASS(oc);
2123 uint32_t vmx = kvmppc_get_vmx();
2124 uint32_t dfp = kvmppc_get_dfp();
2125 uint32_t dcache_size = kvmppc_read_int_cpu_dt("d-cache-size");
2126 uint32_t icache_size = kvmppc_read_int_cpu_dt("i-cache-size");
2128 /* Now fix up the class with information we can query from the host */
2129 pcc->pvr = mfpvr();
2131 if (vmx != -1) {
2132 /* Only override when we know what the host supports */
2133 alter_insns(&pcc->insns_flags, PPC_ALTIVEC, vmx > 0);
2134 alter_insns(&pcc->insns_flags2, PPC2_VSX, vmx > 1);
2136 if (dfp != -1) {
2137 /* Only override when we know what the host supports */
2138 alter_insns(&pcc->insns_flags2, PPC2_DFP, dfp);
2141 if (dcache_size != -1) {
2142 pcc->l1_dcache_size = dcache_size;
2145 if (icache_size != -1) {
2146 pcc->l1_icache_size = icache_size;
2150 bool kvmppc_has_cap_epr(void)
2152 return cap_epr;
2155 bool kvmppc_has_cap_htab_fd(void)
2157 return cap_htab_fd;
2160 bool kvmppc_has_cap_fixup_hcalls(void)
2162 return cap_fixup_hcalls;
2165 static PowerPCCPUClass *ppc_cpu_get_family_class(PowerPCCPUClass *pcc)
2167 ObjectClass *oc = OBJECT_CLASS(pcc);
2169 while (oc && !object_class_is_abstract(oc)) {
2170 oc = object_class_get_parent(oc);
2172 assert(oc);
2174 return POWERPC_CPU_CLASS(oc);
2177 static int kvm_ppc_register_host_cpu_type(void)
2179 TypeInfo type_info = {
2180 .name = TYPE_HOST_POWERPC_CPU,
2181 .instance_init = kvmppc_host_cpu_initfn,
2182 .class_init = kvmppc_host_cpu_class_init,
2184 uint32_t host_pvr = mfpvr();
2185 PowerPCCPUClass *pvr_pcc;
2186 DeviceClass *dc;
2188 pvr_pcc = ppc_cpu_class_by_pvr(host_pvr);
2189 if (pvr_pcc == NULL) {
2190 pvr_pcc = ppc_cpu_class_by_pvr_mask(host_pvr);
2192 if (pvr_pcc == NULL) {
2193 return -1;
2195 type_info.parent = object_class_get_name(OBJECT_CLASS(pvr_pcc));
2196 type_register(&type_info);
2198 /* Register generic family CPU class for a family */
2199 pvr_pcc = ppc_cpu_get_family_class(pvr_pcc);
2200 dc = DEVICE_CLASS(pvr_pcc);
2201 type_info.parent = object_class_get_name(OBJECT_CLASS(pvr_pcc));
2202 type_info.name = g_strdup_printf("%s-"TYPE_POWERPC_CPU, dc->desc);
2203 type_register(&type_info);
2205 return 0;
2208 int kvmppc_define_rtas_kernel_token(uint32_t token, const char *function)
2210 struct kvm_rtas_token_args args = {
2211 .token = token,
2214 if (!kvm_check_extension(kvm_state, KVM_CAP_PPC_RTAS)) {
2215 return -ENOENT;
2218 strncpy(args.name, function, sizeof(args.name));
2220 return kvm_vm_ioctl(kvm_state, KVM_PPC_RTAS_DEFINE_TOKEN, &args);
2223 int kvmppc_get_htab_fd(bool write)
2225 struct kvm_get_htab_fd s = {
2226 .flags = write ? KVM_GET_HTAB_WRITE : 0,
2227 .start_index = 0,
2230 if (!cap_htab_fd) {
2231 fprintf(stderr, "KVM version doesn't support saving the hash table\n");
2232 return -1;
2235 return kvm_vm_ioctl(kvm_state, KVM_PPC_GET_HTAB_FD, &s);
2238 int kvmppc_save_htab(QEMUFile *f, int fd, size_t bufsize, int64_t max_ns)
2240 int64_t starttime = qemu_clock_get_ns(QEMU_CLOCK_REALTIME);
2241 uint8_t buf[bufsize];
2242 ssize_t rc;
2244 do {
2245 rc = read(fd, buf, bufsize);
2246 if (rc < 0) {
2247 fprintf(stderr, "Error reading data from KVM HTAB fd: %s\n",
2248 strerror(errno));
2249 return rc;
2250 } else if (rc) {
2251 uint8_t *buffer = buf;
2252 ssize_t n = rc;
2253 while (n) {
2254 struct kvm_get_htab_header *head =
2255 (struct kvm_get_htab_header *) buffer;
2256 size_t chunksize = sizeof(*head) +
2257 HASH_PTE_SIZE_64 * head->n_valid;
2259 qemu_put_be32(f, head->index);
2260 qemu_put_be16(f, head->n_valid);
2261 qemu_put_be16(f, head->n_invalid);
2262 qemu_put_buffer(f, (void *)(head + 1),
2263 HASH_PTE_SIZE_64 * head->n_valid);
2265 buffer += chunksize;
2266 n -= chunksize;
2269 } while ((rc != 0)
2270 && ((max_ns < 0)
2271 || ((qemu_clock_get_ns(QEMU_CLOCK_REALTIME) - starttime) < max_ns)));
2273 return (rc == 0) ? 1 : 0;
2276 int kvmppc_load_htab_chunk(QEMUFile *f, int fd, uint32_t index,
2277 uint16_t n_valid, uint16_t n_invalid)
2279 struct kvm_get_htab_header *buf;
2280 size_t chunksize = sizeof(*buf) + n_valid*HASH_PTE_SIZE_64;
2281 ssize_t rc;
2283 buf = alloca(chunksize);
2284 buf->index = index;
2285 buf->n_valid = n_valid;
2286 buf->n_invalid = n_invalid;
2288 qemu_get_buffer(f, (void *)(buf + 1), HASH_PTE_SIZE_64*n_valid);
2290 rc = write(fd, buf, chunksize);
2291 if (rc < 0) {
2292 fprintf(stderr, "Error writing KVM hash table: %s\n",
2293 strerror(errno));
2294 return rc;
2296 if (rc != chunksize) {
2297 /* We should never get a short write on a single chunk */
2298 fprintf(stderr, "Short write, restoring KVM hash table\n");
2299 return -1;
2301 return 0;
2304 bool kvm_arch_stop_on_emulation_error(CPUState *cpu)
2306 return true;
2309 int kvm_arch_on_sigbus_vcpu(CPUState *cpu, int code, void *addr)
2311 return 1;
2314 int kvm_arch_on_sigbus(int code, void *addr)
2316 return 1;
2319 void kvm_arch_init_irq_routing(KVMState *s)
2323 struct kvm_get_htab_buf {
2324 struct kvm_get_htab_header header;
2326 * We require one extra byte for read
2328 target_ulong hpte[(HPTES_PER_GROUP * 2) + 1];
2331 uint64_t kvmppc_hash64_read_pteg(PowerPCCPU *cpu, target_ulong pte_index)
2333 int htab_fd;
2334 struct kvm_get_htab_fd ghf;
2335 struct kvm_get_htab_buf *hpte_buf;
2337 ghf.flags = 0;
2338 ghf.start_index = pte_index;
2339 htab_fd = kvm_vm_ioctl(kvm_state, KVM_PPC_GET_HTAB_FD, &ghf);
2340 if (htab_fd < 0) {
2341 goto error_out;
2344 hpte_buf = g_malloc0(sizeof(*hpte_buf));
2346 * Read the hpte group
2348 if (read(htab_fd, hpte_buf, sizeof(*hpte_buf)) < 0) {
2349 goto out_close;
2352 close(htab_fd);
2353 return (uint64_t)(uintptr_t) hpte_buf->hpte;
2355 out_close:
2356 g_free(hpte_buf);
2357 close(htab_fd);
2358 error_out:
2359 return 0;
2362 void kvmppc_hash64_free_pteg(uint64_t token)
2364 struct kvm_get_htab_buf *htab_buf;
2366 htab_buf = container_of((void *)(uintptr_t) token, struct kvm_get_htab_buf,
2367 hpte);
2368 g_free(htab_buf);
2369 return;
2372 void kvmppc_hash64_write_pte(CPUPPCState *env, target_ulong pte_index,
2373 target_ulong pte0, target_ulong pte1)
2375 int htab_fd;
2376 struct kvm_get_htab_fd ghf;
2377 struct kvm_get_htab_buf hpte_buf;
2379 ghf.flags = 0;
2380 ghf.start_index = 0; /* Ignored */
2381 htab_fd = kvm_vm_ioctl(kvm_state, KVM_PPC_GET_HTAB_FD, &ghf);
2382 if (htab_fd < 0) {
2383 goto error_out;
2386 hpte_buf.header.n_valid = 1;
2387 hpte_buf.header.n_invalid = 0;
2388 hpte_buf.header.index = pte_index;
2389 hpte_buf.hpte[0] = pte0;
2390 hpte_buf.hpte[1] = pte1;
2392 * Write the hpte entry.
2393 * CAUTION: write() has the warn_unused_result attribute. Hence we
2394 * need to check the return value, even though we do nothing.
2396 if (write(htab_fd, &hpte_buf, sizeof(hpte_buf)) < 0) {
2397 goto out_close;
2400 out_close:
2401 close(htab_fd);
2402 return;
2404 error_out:
2405 return;
2408 int kvm_arch_fixup_msi_route(struct kvm_irq_routing_entry *route,
2409 uint64_t address, uint32_t data)
2411 return 0;