]> git.proxmox.com Git - mirror_qemu.git/blob - target-ppc/kvm.c
KVM: PPC: Don't secretly add 1T segment feature to CPU
[mirror_qemu.git] / target-ppc / kvm.c
1 /*
2 * PowerPC implementation of KVM hooks
3 *
4 * Copyright IBM Corp. 2007
5 * Copyright (C) 2011 Freescale Semiconductor, Inc.
6 *
7 * Authors:
8 * Jerone Young <jyoung5@us.ibm.com>
9 * Christian Ehrhardt <ehrhardt@linux.vnet.ibm.com>
10 * Hollis Blanchard <hollisb@us.ibm.com>
11 *
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.
14 *
15 */
16
17 #include <dirent.h>
18 #include <sys/types.h>
19 #include <sys/ioctl.h>
20 #include <sys/mman.h>
21 #include <sys/vfs.h>
22
23 #include <linux/kvm.h>
24
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"
34
35 #include "hw/sysbus.h"
36 #include "hw/ppc/spapr.h"
37 #include "hw/ppc/spapr_vio.h"
38 #include "sysemu/watchdog.h"
39 #include "trace.h"
40
41 //#define DEBUG_KVM
42
43 #ifdef DEBUG_KVM
44 #define DPRINTF(fmt, ...) \
45 do { fprintf(stderr, fmt, ## __VA_ARGS__); } while (0)
46 #else
47 #define DPRINTF(fmt, ...) \
48 do { } while (0)
49 #endif
50
51 #define PROC_DEVTREE_CPU "/proc/device-tree/cpus/"
52
53 const KVMCapabilityInfo kvm_arch_required_capabilities[] = {
54 KVM_CAP_LAST_INFO
55 };
56
57 static int cap_interrupt_unset = false;
58 static int cap_interrupt_level = false;
59 static int cap_segstate;
60 static int cap_booke_sregs;
61 static int cap_ppc_smt;
62 static int cap_ppc_rma;
63 static int cap_spapr_tce;
64 static int cap_hior;
65 static int cap_one_reg;
66 static int cap_epr;
67 static int cap_ppc_watchdog;
68 static int cap_papr;
69 static int cap_htab_fd;
70
71 /* XXX We have a race condition where we actually have a level triggered
72 * interrupt, but the infrastructure can't expose that yet, so the guest
73 * takes but ignores it, goes to sleep and never gets notified that there's
74 * still an interrupt pending.
75 *
76 * As a quick workaround, let's just wake up again 20 ms after we injected
77 * an interrupt. That way we can assure that we're always reinjecting
78 * interrupts in case the guest swallowed them.
79 */
80 static QEMUTimer *idle_timer;
81
82 static void kvm_kick_cpu(void *opaque)
83 {
84 PowerPCCPU *cpu = opaque;
85
86 qemu_cpu_kick(CPU(cpu));
87 }
88
89 static int kvm_ppc_register_host_cpu_type(void);
90
91 int kvm_arch_init(KVMState *s)
92 {
93 cap_interrupt_unset = kvm_check_extension(s, KVM_CAP_PPC_UNSET_IRQ);
94 cap_interrupt_level = kvm_check_extension(s, KVM_CAP_PPC_IRQ_LEVEL);
95 cap_segstate = kvm_check_extension(s, KVM_CAP_PPC_SEGSTATE);
96 cap_booke_sregs = kvm_check_extension(s, KVM_CAP_PPC_BOOKE_SREGS);
97 cap_ppc_smt = kvm_check_extension(s, KVM_CAP_PPC_SMT);
98 cap_ppc_rma = kvm_check_extension(s, KVM_CAP_PPC_RMA);
99 cap_spapr_tce = kvm_check_extension(s, KVM_CAP_SPAPR_TCE);
100 cap_one_reg = kvm_check_extension(s, KVM_CAP_ONE_REG);
101 cap_hior = kvm_check_extension(s, KVM_CAP_PPC_HIOR);
102 cap_epr = kvm_check_extension(s, KVM_CAP_PPC_EPR);
103 cap_ppc_watchdog = kvm_check_extension(s, KVM_CAP_PPC_BOOKE_WATCHDOG);
104 /* Note: we don't set cap_papr here, because this capability is
105 * only activated after this by kvmppc_set_papr() */
106 cap_htab_fd = kvm_check_extension(s, KVM_CAP_PPC_HTAB_FD);
107
108 if (!cap_interrupt_level) {
109 fprintf(stderr, "KVM: Couldn't find level irq capability. Expect the "
110 "VM to stall at times!\n");
111 }
112
113 kvm_ppc_register_host_cpu_type();
114
115 return 0;
116 }
117
118 static int kvm_arch_sync_sregs(PowerPCCPU *cpu)
119 {
120 CPUPPCState *cenv = &cpu->env;
121 CPUState *cs = CPU(cpu);
122 struct kvm_sregs sregs;
123 int ret;
124
125 if (cenv->excp_model == POWERPC_EXCP_BOOKE) {
126 /* What we're really trying to say is "if we're on BookE, we use
127 the native PVR for now". This is the only sane way to check
128 it though, so we potentially confuse users that they can run
129 BookE guests on BookS. Let's hope nobody dares enough :) */
130 return 0;
131 } else {
132 if (!cap_segstate) {
133 fprintf(stderr, "kvm error: missing PVR setting capability\n");
134 return -ENOSYS;
135 }
136 }
137
138 ret = kvm_vcpu_ioctl(cs, KVM_GET_SREGS, &sregs);
139 if (ret) {
140 return ret;
141 }
142
143 sregs.pvr = cenv->spr[SPR_PVR];
144 return kvm_vcpu_ioctl(cs, KVM_SET_SREGS, &sregs);
145 }
146
147 /* Set up a shared TLB array with KVM */
148 static int kvm_booke206_tlb_init(PowerPCCPU *cpu)
149 {
150 CPUPPCState *env = &cpu->env;
151 CPUState *cs = CPU(cpu);
152 struct kvm_book3e_206_tlb_params params = {};
153 struct kvm_config_tlb cfg = {};
154 unsigned int entries = 0;
155 int ret, i;
156
157 if (!kvm_enabled() ||
158 !kvm_check_extension(cs->kvm_state, KVM_CAP_SW_TLB)) {
159 return 0;
160 }
161
162 assert(ARRAY_SIZE(params.tlb_sizes) == BOOKE206_MAX_TLBN);
163
164 for (i = 0; i < BOOKE206_MAX_TLBN; i++) {
165 params.tlb_sizes[i] = booke206_tlb_size(env, i);
166 params.tlb_ways[i] = booke206_tlb_ways(env, i);
167 entries += params.tlb_sizes[i];
168 }
169
170 assert(entries == env->nb_tlb);
171 assert(sizeof(struct kvm_book3e_206_tlb_entry) == sizeof(ppcmas_tlb_t));
172
173 env->tlb_dirty = true;
174
175 cfg.array = (uintptr_t)env->tlb.tlbm;
176 cfg.array_len = sizeof(ppcmas_tlb_t) * entries;
177 cfg.params = (uintptr_t)&params;
178 cfg.mmu_type = KVM_MMU_FSL_BOOKE_NOHV;
179
180 ret = kvm_vcpu_enable_cap(cs, KVM_CAP_SW_TLB, 0, (uintptr_t)&cfg);
181 if (ret < 0) {
182 fprintf(stderr, "%s: couldn't enable KVM_CAP_SW_TLB: %s\n",
183 __func__, strerror(-ret));
184 return ret;
185 }
186
187 env->kvm_sw_tlb = true;
188 return 0;
189 }
190
191
192 #if defined(TARGET_PPC64)
193 static void kvm_get_fallback_smmu_info(PowerPCCPU *cpu,
194 struct kvm_ppc_smmu_info *info)
195 {
196 CPUPPCState *env = &cpu->env;
197 CPUState *cs = CPU(cpu);
198
199 memset(info, 0, sizeof(*info));
200
201 /* We don't have the new KVM_PPC_GET_SMMU_INFO ioctl, so
202 * need to "guess" what the supported page sizes are.
203 *
204 * For that to work we make a few assumptions:
205 *
206 * - If KVM_CAP_PPC_GET_PVINFO is supported we are running "PR"
207 * KVM which only supports 4K and 16M pages, but supports them
208 * regardless of the backing store characteritics. We also don't
209 * support 1T segments.
210 *
211 * This is safe as if HV KVM ever supports that capability or PR
212 * KVM grows supports for more page/segment sizes, those versions
213 * will have implemented KVM_CAP_PPC_GET_SMMU_INFO and thus we
214 * will not hit this fallback
215 *
216 * - Else we are running HV KVM. This means we only support page
217 * sizes that fit in the backing store. Additionally we only
218 * advertize 64K pages if the processor is ARCH 2.06 and we assume
219 * P7 encodings for the SLB and hash table. Here too, we assume
220 * support for any newer processor will mean a kernel that
221 * implements KVM_CAP_PPC_GET_SMMU_INFO and thus doesn't hit
222 * this fallback.
223 */
224 if (kvm_check_extension(cs->kvm_state, KVM_CAP_PPC_GET_PVINFO)) {
225 /* No flags */
226 info->flags = 0;
227 info->slb_size = 64;
228
229 /* Standard 4k base page size segment */
230 info->sps[0].page_shift = 12;
231 info->sps[0].slb_enc = 0;
232 info->sps[0].enc[0].page_shift = 12;
233 info->sps[0].enc[0].pte_enc = 0;
234
235 /* Standard 16M large page size segment */
236 info->sps[1].page_shift = 24;
237 info->sps[1].slb_enc = SLB_VSID_L;
238 info->sps[1].enc[0].page_shift = 24;
239 info->sps[1].enc[0].pte_enc = 0;
240 } else {
241 int i = 0;
242
243 /* HV KVM has backing store size restrictions */
244 info->flags = KVM_PPC_PAGE_SIZES_REAL;
245
246 if (env->mmu_model & POWERPC_MMU_1TSEG) {
247 info->flags |= KVM_PPC_1T_SEGMENTS;
248 }
249
250 if (env->mmu_model == POWERPC_MMU_2_06) {
251 info->slb_size = 32;
252 } else {
253 info->slb_size = 64;
254 }
255
256 /* Standard 4k base page size segment */
257 info->sps[i].page_shift = 12;
258 info->sps[i].slb_enc = 0;
259 info->sps[i].enc[0].page_shift = 12;
260 info->sps[i].enc[0].pte_enc = 0;
261 i++;
262
263 /* 64K on MMU 2.06 */
264 if (env->mmu_model == POWERPC_MMU_2_06) {
265 info->sps[i].page_shift = 16;
266 info->sps[i].slb_enc = 0x110;
267 info->sps[i].enc[0].page_shift = 16;
268 info->sps[i].enc[0].pte_enc = 1;
269 i++;
270 }
271
272 /* Standard 16M large page size segment */
273 info->sps[i].page_shift = 24;
274 info->sps[i].slb_enc = SLB_VSID_L;
275 info->sps[i].enc[0].page_shift = 24;
276 info->sps[i].enc[0].pte_enc = 0;
277 }
278 }
279
280 static void kvm_get_smmu_info(PowerPCCPU *cpu, struct kvm_ppc_smmu_info *info)
281 {
282 CPUState *cs = CPU(cpu);
283 int ret;
284
285 if (kvm_check_extension(cs->kvm_state, KVM_CAP_PPC_GET_SMMU_INFO)) {
286 ret = kvm_vm_ioctl(cs->kvm_state, KVM_PPC_GET_SMMU_INFO, info);
287 if (ret == 0) {
288 return;
289 }
290 }
291
292 kvm_get_fallback_smmu_info(cpu, info);
293 }
294
295 static long getrampagesize(void)
296 {
297 struct statfs fs;
298 int ret;
299
300 if (!mem_path) {
301 /* guest RAM is backed by normal anonymous pages */
302 return getpagesize();
303 }
304
305 do {
306 ret = statfs(mem_path, &fs);
307 } while (ret != 0 && errno == EINTR);
308
309 if (ret != 0) {
310 fprintf(stderr, "Couldn't statfs() memory path: %s\n",
311 strerror(errno));
312 exit(1);
313 }
314
315 #define HUGETLBFS_MAGIC 0x958458f6
316
317 if (fs.f_type != HUGETLBFS_MAGIC) {
318 /* Explicit mempath, but it's ordinary pages */
319 return getpagesize();
320 }
321
322 /* It's hugepage, return the huge page size */
323 return fs.f_bsize;
324 }
325
326 static bool kvm_valid_page_size(uint32_t flags, long rampgsize, uint32_t shift)
327 {
328 if (!(flags & KVM_PPC_PAGE_SIZES_REAL)) {
329 return true;
330 }
331
332 return (1ul << shift) <= rampgsize;
333 }
334
335 static void kvm_fixup_page_sizes(PowerPCCPU *cpu)
336 {
337 static struct kvm_ppc_smmu_info smmu_info;
338 static bool has_smmu_info;
339 CPUPPCState *env = &cpu->env;
340 long rampagesize;
341 int iq, ik, jq, jk;
342
343 /* We only handle page sizes for 64-bit server guests for now */
344 if (!(env->mmu_model & POWERPC_MMU_64)) {
345 return;
346 }
347
348 /* Collect MMU info from kernel if not already */
349 if (!has_smmu_info) {
350 kvm_get_smmu_info(cpu, &smmu_info);
351 has_smmu_info = true;
352 }
353
354 rampagesize = getrampagesize();
355
356 /* Convert to QEMU form */
357 memset(&env->sps, 0, sizeof(env->sps));
358
359 /*
360 * XXX This loop should be an entry wide AND of the capabilities that
361 * the selected CPU has with the capabilities that KVM supports.
362 */
363 for (ik = iq = 0; ik < KVM_PPC_PAGE_SIZES_MAX_SZ; ik++) {
364 struct ppc_one_seg_page_size *qsps = &env->sps.sps[iq];
365 struct kvm_ppc_one_seg_page_size *ksps = &smmu_info.sps[ik];
366
367 if (!kvm_valid_page_size(smmu_info.flags, rampagesize,
368 ksps->page_shift)) {
369 continue;
370 }
371 qsps->page_shift = ksps->page_shift;
372 qsps->slb_enc = ksps->slb_enc;
373 for (jk = jq = 0; jk < KVM_PPC_PAGE_SIZES_MAX_SZ; jk++) {
374 if (!kvm_valid_page_size(smmu_info.flags, rampagesize,
375 ksps->enc[jk].page_shift)) {
376 continue;
377 }
378 qsps->enc[jq].page_shift = ksps->enc[jk].page_shift;
379 qsps->enc[jq].pte_enc = ksps->enc[jk].pte_enc;
380 if (++jq >= PPC_PAGE_SIZES_MAX_SZ) {
381 break;
382 }
383 }
384 if (++iq >= PPC_PAGE_SIZES_MAX_SZ) {
385 break;
386 }
387 }
388 env->slb_nr = smmu_info.slb_size;
389 if (!(smmu_info.flags & KVM_PPC_1T_SEGMENTS)) {
390 env->mmu_model &= ~POWERPC_MMU_1TSEG;
391 }
392 }
393 #else /* defined (TARGET_PPC64) */
394
395 static inline void kvm_fixup_page_sizes(PowerPCCPU *cpu)
396 {
397 }
398
399 #endif /* !defined (TARGET_PPC64) */
400
401 unsigned long kvm_arch_vcpu_id(CPUState *cpu)
402 {
403 return ppc_get_vcpu_dt_id(POWERPC_CPU(cpu));
404 }
405
406 int kvm_arch_init_vcpu(CPUState *cs)
407 {
408 PowerPCCPU *cpu = POWERPC_CPU(cs);
409 CPUPPCState *cenv = &cpu->env;
410 int ret;
411
412 /* Gather server mmu info from KVM and update the CPU state */
413 kvm_fixup_page_sizes(cpu);
414
415 /* Synchronize sregs with kvm */
416 ret = kvm_arch_sync_sregs(cpu);
417 if (ret) {
418 return ret;
419 }
420
421 idle_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, kvm_kick_cpu, cpu);
422
423 /* Some targets support access to KVM's guest TLB. */
424 switch (cenv->mmu_model) {
425 case POWERPC_MMU_BOOKE206:
426 ret = kvm_booke206_tlb_init(cpu);
427 break;
428 default:
429 break;
430 }
431
432 return ret;
433 }
434
435 static void kvm_sw_tlb_put(PowerPCCPU *cpu)
436 {
437 CPUPPCState *env = &cpu->env;
438 CPUState *cs = CPU(cpu);
439 struct kvm_dirty_tlb dirty_tlb;
440 unsigned char *bitmap;
441 int ret;
442
443 if (!env->kvm_sw_tlb) {
444 return;
445 }
446
447 bitmap = g_malloc((env->nb_tlb + 7) / 8);
448 memset(bitmap, 0xFF, (env->nb_tlb + 7) / 8);
449
450 dirty_tlb.bitmap = (uintptr_t)bitmap;
451 dirty_tlb.num_dirty = env->nb_tlb;
452
453 ret = kvm_vcpu_ioctl(cs, KVM_DIRTY_TLB, &dirty_tlb);
454 if (ret) {
455 fprintf(stderr, "%s: KVM_DIRTY_TLB: %s\n",
456 __func__, strerror(-ret));
457 }
458
459 g_free(bitmap);
460 }
461
462 static void kvm_get_one_spr(CPUState *cs, uint64_t id, int spr)
463 {
464 PowerPCCPU *cpu = POWERPC_CPU(cs);
465 CPUPPCState *env = &cpu->env;
466 union {
467 uint32_t u32;
468 uint64_t u64;
469 } val;
470 struct kvm_one_reg reg = {
471 .id = id,
472 .addr = (uintptr_t) &val,
473 };
474 int ret;
475
476 ret = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, &reg);
477 if (ret != 0) {
478 trace_kvm_failed_spr_get(spr, strerror(errno));
479 } else {
480 switch (id & KVM_REG_SIZE_MASK) {
481 case KVM_REG_SIZE_U32:
482 env->spr[spr] = val.u32;
483 break;
484
485 case KVM_REG_SIZE_U64:
486 env->spr[spr] = val.u64;
487 break;
488
489 default:
490 /* Don't handle this size yet */
491 abort();
492 }
493 }
494 }
495
496 static void kvm_put_one_spr(CPUState *cs, uint64_t id, int spr)
497 {
498 PowerPCCPU *cpu = POWERPC_CPU(cs);
499 CPUPPCState *env = &cpu->env;
500 union {
501 uint32_t u32;
502 uint64_t u64;
503 } val;
504 struct kvm_one_reg reg = {
505 .id = id,
506 .addr = (uintptr_t) &val,
507 };
508 int ret;
509
510 switch (id & KVM_REG_SIZE_MASK) {
511 case KVM_REG_SIZE_U32:
512 val.u32 = env->spr[spr];
513 break;
514
515 case KVM_REG_SIZE_U64:
516 val.u64 = env->spr[spr];
517 break;
518
519 default:
520 /* Don't handle this size yet */
521 abort();
522 }
523
524 ret = kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, &reg);
525 if (ret != 0) {
526 trace_kvm_failed_spr_set(spr, strerror(errno));
527 }
528 }
529
530 static int kvm_put_fp(CPUState *cs)
531 {
532 PowerPCCPU *cpu = POWERPC_CPU(cs);
533 CPUPPCState *env = &cpu->env;
534 struct kvm_one_reg reg;
535 int i;
536 int ret;
537
538 if (env->insns_flags & PPC_FLOAT) {
539 uint64_t fpscr = env->fpscr;
540 bool vsx = !!(env->insns_flags2 & PPC2_VSX);
541
542 reg.id = KVM_REG_PPC_FPSCR;
543 reg.addr = (uintptr_t)&fpscr;
544 ret = kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, &reg);
545 if (ret < 0) {
546 DPRINTF("Unable to set FPSCR to KVM: %s\n", strerror(errno));
547 return ret;
548 }
549
550 for (i = 0; i < 32; i++) {
551 uint64_t vsr[2];
552
553 vsr[0] = float64_val(env->fpr[i]);
554 vsr[1] = env->vsr[i];
555 reg.addr = (uintptr_t) &vsr;
556 reg.id = vsx ? KVM_REG_PPC_VSR(i) : KVM_REG_PPC_FPR(i);
557
558 ret = kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, &reg);
559 if (ret < 0) {
560 DPRINTF("Unable to set %s%d to KVM: %s\n", vsx ? "VSR" : "FPR",
561 i, strerror(errno));
562 return ret;
563 }
564 }
565 }
566
567 if (env->insns_flags & PPC_ALTIVEC) {
568 reg.id = KVM_REG_PPC_VSCR;
569 reg.addr = (uintptr_t)&env->vscr;
570 ret = kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, &reg);
571 if (ret < 0) {
572 DPRINTF("Unable to set VSCR to KVM: %s\n", strerror(errno));
573 return ret;
574 }
575
576 for (i = 0; i < 32; i++) {
577 reg.id = KVM_REG_PPC_VR(i);
578 reg.addr = (uintptr_t)&env->avr[i];
579 ret = kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, &reg);
580 if (ret < 0) {
581 DPRINTF("Unable to set VR%d to KVM: %s\n", i, strerror(errno));
582 return ret;
583 }
584 }
585 }
586
587 return 0;
588 }
589
590 static int kvm_get_fp(CPUState *cs)
591 {
592 PowerPCCPU *cpu = POWERPC_CPU(cs);
593 CPUPPCState *env = &cpu->env;
594 struct kvm_one_reg reg;
595 int i;
596 int ret;
597
598 if (env->insns_flags & PPC_FLOAT) {
599 uint64_t fpscr;
600 bool vsx = !!(env->insns_flags2 & PPC2_VSX);
601
602 reg.id = KVM_REG_PPC_FPSCR;
603 reg.addr = (uintptr_t)&fpscr;
604 ret = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, &reg);
605 if (ret < 0) {
606 DPRINTF("Unable to get FPSCR from KVM: %s\n", strerror(errno));
607 return ret;
608 } else {
609 env->fpscr = fpscr;
610 }
611
612 for (i = 0; i < 32; i++) {
613 uint64_t vsr[2];
614
615 reg.addr = (uintptr_t) &vsr;
616 reg.id = vsx ? KVM_REG_PPC_VSR(i) : KVM_REG_PPC_FPR(i);
617
618 ret = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, &reg);
619 if (ret < 0) {
620 DPRINTF("Unable to get %s%d from KVM: %s\n",
621 vsx ? "VSR" : "FPR", i, strerror(errno));
622 return ret;
623 } else {
624 env->fpr[i] = vsr[0];
625 if (vsx) {
626 env->vsr[i] = vsr[1];
627 }
628 }
629 }
630 }
631
632 if (env->insns_flags & PPC_ALTIVEC) {
633 reg.id = KVM_REG_PPC_VSCR;
634 reg.addr = (uintptr_t)&env->vscr;
635 ret = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, &reg);
636 if (ret < 0) {
637 DPRINTF("Unable to get VSCR from KVM: %s\n", strerror(errno));
638 return ret;
639 }
640
641 for (i = 0; i < 32; i++) {
642 reg.id = KVM_REG_PPC_VR(i);
643 reg.addr = (uintptr_t)&env->avr[i];
644 ret = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, &reg);
645 if (ret < 0) {
646 DPRINTF("Unable to get VR%d from KVM: %s\n",
647 i, strerror(errno));
648 return ret;
649 }
650 }
651 }
652
653 return 0;
654 }
655
656 #if defined(TARGET_PPC64)
657 static int kvm_get_vpa(CPUState *cs)
658 {
659 PowerPCCPU *cpu = POWERPC_CPU(cs);
660 CPUPPCState *env = &cpu->env;
661 struct kvm_one_reg reg;
662 int ret;
663
664 reg.id = KVM_REG_PPC_VPA_ADDR;
665 reg.addr = (uintptr_t)&env->vpa_addr;
666 ret = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, &reg);
667 if (ret < 0) {
668 DPRINTF("Unable to get VPA address from KVM: %s\n", strerror(errno));
669 return ret;
670 }
671
672 assert((uintptr_t)&env->slb_shadow_size
673 == ((uintptr_t)&env->slb_shadow_addr + 8));
674 reg.id = KVM_REG_PPC_VPA_SLB;
675 reg.addr = (uintptr_t)&env->slb_shadow_addr;
676 ret = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, &reg);
677 if (ret < 0) {
678 DPRINTF("Unable to get SLB shadow state from KVM: %s\n",
679 strerror(errno));
680 return ret;
681 }
682
683 assert((uintptr_t)&env->dtl_size == ((uintptr_t)&env->dtl_addr + 8));
684 reg.id = KVM_REG_PPC_VPA_DTL;
685 reg.addr = (uintptr_t)&env->dtl_addr;
686 ret = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, &reg);
687 if (ret < 0) {
688 DPRINTF("Unable to get dispatch trace log state from KVM: %s\n",
689 strerror(errno));
690 return ret;
691 }
692
693 return 0;
694 }
695
696 static int kvm_put_vpa(CPUState *cs)
697 {
698 PowerPCCPU *cpu = POWERPC_CPU(cs);
699 CPUPPCState *env = &cpu->env;
700 struct kvm_one_reg reg;
701 int ret;
702
703 /* SLB shadow or DTL can't be registered unless a master VPA is
704 * registered. That means when restoring state, if a VPA *is*
705 * registered, we need to set that up first. If not, we need to
706 * deregister the others before deregistering the master VPA */
707 assert(env->vpa_addr || !(env->slb_shadow_addr || env->dtl_addr));
708
709 if (env->vpa_addr) {
710 reg.id = KVM_REG_PPC_VPA_ADDR;
711 reg.addr = (uintptr_t)&env->vpa_addr;
712 ret = kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, &reg);
713 if (ret < 0) {
714 DPRINTF("Unable to set VPA address to KVM: %s\n", strerror(errno));
715 return ret;
716 }
717 }
718
719 assert((uintptr_t)&env->slb_shadow_size
720 == ((uintptr_t)&env->slb_shadow_addr + 8));
721 reg.id = KVM_REG_PPC_VPA_SLB;
722 reg.addr = (uintptr_t)&env->slb_shadow_addr;
723 ret = kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, &reg);
724 if (ret < 0) {
725 DPRINTF("Unable to set SLB shadow state to KVM: %s\n", strerror(errno));
726 return ret;
727 }
728
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_SET_ONE_REG, &reg);
733 if (ret < 0) {
734 DPRINTF("Unable to set dispatch trace log state to KVM: %s\n",
735 strerror(errno));
736 return ret;
737 }
738
739 if (!env->vpa_addr) {
740 reg.id = KVM_REG_PPC_VPA_ADDR;
741 reg.addr = (uintptr_t)&env->vpa_addr;
742 ret = kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, &reg);
743 if (ret < 0) {
744 DPRINTF("Unable to set VPA address to KVM: %s\n", strerror(errno));
745 return ret;
746 }
747 }
748
749 return 0;
750 }
751 #endif /* TARGET_PPC64 */
752
753 int kvm_arch_put_registers(CPUState *cs, int level)
754 {
755 PowerPCCPU *cpu = POWERPC_CPU(cs);
756 CPUPPCState *env = &cpu->env;
757 struct kvm_regs regs;
758 int ret;
759 int i;
760
761 ret = kvm_vcpu_ioctl(cs, KVM_GET_REGS, &regs);
762 if (ret < 0) {
763 return ret;
764 }
765
766 regs.ctr = env->ctr;
767 regs.lr = env->lr;
768 regs.xer = cpu_read_xer(env);
769 regs.msr = env->msr;
770 regs.pc = env->nip;
771
772 regs.srr0 = env->spr[SPR_SRR0];
773 regs.srr1 = env->spr[SPR_SRR1];
774
775 regs.sprg0 = env->spr[SPR_SPRG0];
776 regs.sprg1 = env->spr[SPR_SPRG1];
777 regs.sprg2 = env->spr[SPR_SPRG2];
778 regs.sprg3 = env->spr[SPR_SPRG3];
779 regs.sprg4 = env->spr[SPR_SPRG4];
780 regs.sprg5 = env->spr[SPR_SPRG5];
781 regs.sprg6 = env->spr[SPR_SPRG6];
782 regs.sprg7 = env->spr[SPR_SPRG7];
783
784 regs.pid = env->spr[SPR_BOOKE_PID];
785
786 for (i = 0;i < 32; i++)
787 regs.gpr[i] = env->gpr[i];
788
789 regs.cr = 0;
790 for (i = 0; i < 8; i++) {
791 regs.cr |= (env->crf[i] & 15) << (4 * (7 - i));
792 }
793
794 ret = kvm_vcpu_ioctl(cs, KVM_SET_REGS, &regs);
795 if (ret < 0)
796 return ret;
797
798 kvm_put_fp(cs);
799
800 if (env->tlb_dirty) {
801 kvm_sw_tlb_put(cpu);
802 env->tlb_dirty = false;
803 }
804
805 if (cap_segstate && (level >= KVM_PUT_RESET_STATE)) {
806 struct kvm_sregs sregs;
807
808 sregs.pvr = env->spr[SPR_PVR];
809
810 sregs.u.s.sdr1 = env->spr[SPR_SDR1];
811
812 /* Sync SLB */
813 #ifdef TARGET_PPC64
814 for (i = 0; i < ARRAY_SIZE(env->slb); i++) {
815 sregs.u.s.ppc64.slb[i].slbe = env->slb[i].esid;
816 if (env->slb[i].esid & SLB_ESID_V) {
817 sregs.u.s.ppc64.slb[i].slbe |= i;
818 }
819 sregs.u.s.ppc64.slb[i].slbv = env->slb[i].vsid;
820 }
821 #endif
822
823 /* Sync SRs */
824 for (i = 0; i < 16; i++) {
825 sregs.u.s.ppc32.sr[i] = env->sr[i];
826 }
827
828 /* Sync BATs */
829 for (i = 0; i < 8; i++) {
830 /* Beware. We have to swap upper and lower bits here */
831 sregs.u.s.ppc32.dbat[i] = ((uint64_t)env->DBAT[0][i] << 32)
832 | env->DBAT[1][i];
833 sregs.u.s.ppc32.ibat[i] = ((uint64_t)env->IBAT[0][i] << 32)
834 | env->IBAT[1][i];
835 }
836
837 ret = kvm_vcpu_ioctl(cs, KVM_SET_SREGS, &sregs);
838 if (ret) {
839 return ret;
840 }
841 }
842
843 if (cap_hior && (level >= KVM_PUT_RESET_STATE)) {
844 kvm_put_one_spr(cs, KVM_REG_PPC_HIOR, SPR_HIOR);
845 }
846
847 if (cap_one_reg) {
848 int i;
849
850 /* We deliberately ignore errors here, for kernels which have
851 * the ONE_REG calls, but don't support the specific
852 * registers, there's a reasonable chance things will still
853 * work, at least until we try to migrate. */
854 for (i = 0; i < 1024; i++) {
855 uint64_t id = env->spr_cb[i].one_reg_id;
856
857 if (id != 0) {
858 kvm_put_one_spr(cs, id, i);
859 }
860 }
861
862 #ifdef TARGET_PPC64
863 if (cap_papr) {
864 if (kvm_put_vpa(cs) < 0) {
865 DPRINTF("Warning: Unable to set VPA information to KVM\n");
866 }
867 }
868 #endif /* TARGET_PPC64 */
869 }
870
871 return ret;
872 }
873
874 int kvm_arch_get_registers(CPUState *cs)
875 {
876 PowerPCCPU *cpu = POWERPC_CPU(cs);
877 CPUPPCState *env = &cpu->env;
878 struct kvm_regs regs;
879 struct kvm_sregs sregs;
880 uint32_t cr;
881 int i, ret;
882
883 ret = kvm_vcpu_ioctl(cs, KVM_GET_REGS, &regs);
884 if (ret < 0)
885 return ret;
886
887 cr = regs.cr;
888 for (i = 7; i >= 0; i--) {
889 env->crf[i] = cr & 15;
890 cr >>= 4;
891 }
892
893 env->ctr = regs.ctr;
894 env->lr = regs.lr;
895 cpu_write_xer(env, regs.xer);
896 env->msr = regs.msr;
897 env->nip = regs.pc;
898
899 env->spr[SPR_SRR0] = regs.srr0;
900 env->spr[SPR_SRR1] = regs.srr1;
901
902 env->spr[SPR_SPRG0] = regs.sprg0;
903 env->spr[SPR_SPRG1] = regs.sprg1;
904 env->spr[SPR_SPRG2] = regs.sprg2;
905 env->spr[SPR_SPRG3] = regs.sprg3;
906 env->spr[SPR_SPRG4] = regs.sprg4;
907 env->spr[SPR_SPRG5] = regs.sprg5;
908 env->spr[SPR_SPRG6] = regs.sprg6;
909 env->spr[SPR_SPRG7] = regs.sprg7;
910
911 env->spr[SPR_BOOKE_PID] = regs.pid;
912
913 for (i = 0;i < 32; i++)
914 env->gpr[i] = regs.gpr[i];
915
916 kvm_get_fp(cs);
917
918 if (cap_booke_sregs) {
919 ret = kvm_vcpu_ioctl(cs, KVM_GET_SREGS, &sregs);
920 if (ret < 0) {
921 return ret;
922 }
923
924 if (sregs.u.e.features & KVM_SREGS_E_BASE) {
925 env->spr[SPR_BOOKE_CSRR0] = sregs.u.e.csrr0;
926 env->spr[SPR_BOOKE_CSRR1] = sregs.u.e.csrr1;
927 env->spr[SPR_BOOKE_ESR] = sregs.u.e.esr;
928 env->spr[SPR_BOOKE_DEAR] = sregs.u.e.dear;
929 env->spr[SPR_BOOKE_MCSR] = sregs.u.e.mcsr;
930 env->spr[SPR_BOOKE_TSR] = sregs.u.e.tsr;
931 env->spr[SPR_BOOKE_TCR] = sregs.u.e.tcr;
932 env->spr[SPR_DECR] = sregs.u.e.dec;
933 env->spr[SPR_TBL] = sregs.u.e.tb & 0xffffffff;
934 env->spr[SPR_TBU] = sregs.u.e.tb >> 32;
935 env->spr[SPR_VRSAVE] = sregs.u.e.vrsave;
936 }
937
938 if (sregs.u.e.features & KVM_SREGS_E_ARCH206) {
939 env->spr[SPR_BOOKE_PIR] = sregs.u.e.pir;
940 env->spr[SPR_BOOKE_MCSRR0] = sregs.u.e.mcsrr0;
941 env->spr[SPR_BOOKE_MCSRR1] = sregs.u.e.mcsrr1;
942 env->spr[SPR_BOOKE_DECAR] = sregs.u.e.decar;
943 env->spr[SPR_BOOKE_IVPR] = sregs.u.e.ivpr;
944 }
945
946 if (sregs.u.e.features & KVM_SREGS_E_64) {
947 env->spr[SPR_BOOKE_EPCR] = sregs.u.e.epcr;
948 }
949
950 if (sregs.u.e.features & KVM_SREGS_E_SPRG8) {
951 env->spr[SPR_BOOKE_SPRG8] = sregs.u.e.sprg8;
952 }
953
954 if (sregs.u.e.features & KVM_SREGS_E_IVOR) {
955 env->spr[SPR_BOOKE_IVOR0] = sregs.u.e.ivor_low[0];
956 env->spr[SPR_BOOKE_IVOR1] = sregs.u.e.ivor_low[1];
957 env->spr[SPR_BOOKE_IVOR2] = sregs.u.e.ivor_low[2];
958 env->spr[SPR_BOOKE_IVOR3] = sregs.u.e.ivor_low[3];
959 env->spr[SPR_BOOKE_IVOR4] = sregs.u.e.ivor_low[4];
960 env->spr[SPR_BOOKE_IVOR5] = sregs.u.e.ivor_low[5];
961 env->spr[SPR_BOOKE_IVOR6] = sregs.u.e.ivor_low[6];
962 env->spr[SPR_BOOKE_IVOR7] = sregs.u.e.ivor_low[7];
963 env->spr[SPR_BOOKE_IVOR8] = sregs.u.e.ivor_low[8];
964 env->spr[SPR_BOOKE_IVOR9] = sregs.u.e.ivor_low[9];
965 env->spr[SPR_BOOKE_IVOR10] = sregs.u.e.ivor_low[10];
966 env->spr[SPR_BOOKE_IVOR11] = sregs.u.e.ivor_low[11];
967 env->spr[SPR_BOOKE_IVOR12] = sregs.u.e.ivor_low[12];
968 env->spr[SPR_BOOKE_IVOR13] = sregs.u.e.ivor_low[13];
969 env->spr[SPR_BOOKE_IVOR14] = sregs.u.e.ivor_low[14];
970 env->spr[SPR_BOOKE_IVOR15] = sregs.u.e.ivor_low[15];
971
972 if (sregs.u.e.features & KVM_SREGS_E_SPE) {
973 env->spr[SPR_BOOKE_IVOR32] = sregs.u.e.ivor_high[0];
974 env->spr[SPR_BOOKE_IVOR33] = sregs.u.e.ivor_high[1];
975 env->spr[SPR_BOOKE_IVOR34] = sregs.u.e.ivor_high[2];
976 }
977
978 if (sregs.u.e.features & KVM_SREGS_E_PM) {
979 env->spr[SPR_BOOKE_IVOR35] = sregs.u.e.ivor_high[3];
980 }
981
982 if (sregs.u.e.features & KVM_SREGS_E_PC) {
983 env->spr[SPR_BOOKE_IVOR36] = sregs.u.e.ivor_high[4];
984 env->spr[SPR_BOOKE_IVOR37] = sregs.u.e.ivor_high[5];
985 }
986 }
987
988 if (sregs.u.e.features & KVM_SREGS_E_ARCH206_MMU) {
989 env->spr[SPR_BOOKE_MAS0] = sregs.u.e.mas0;
990 env->spr[SPR_BOOKE_MAS1] = sregs.u.e.mas1;
991 env->spr[SPR_BOOKE_MAS2] = sregs.u.e.mas2;
992 env->spr[SPR_BOOKE_MAS3] = sregs.u.e.mas7_3 & 0xffffffff;
993 env->spr[SPR_BOOKE_MAS4] = sregs.u.e.mas4;
994 env->spr[SPR_BOOKE_MAS6] = sregs.u.e.mas6;
995 env->spr[SPR_BOOKE_MAS7] = sregs.u.e.mas7_3 >> 32;
996 env->spr[SPR_MMUCFG] = sregs.u.e.mmucfg;
997 env->spr[SPR_BOOKE_TLB0CFG] = sregs.u.e.tlbcfg[0];
998 env->spr[SPR_BOOKE_TLB1CFG] = sregs.u.e.tlbcfg[1];
999 }
1000
1001 if (sregs.u.e.features & KVM_SREGS_EXP) {
1002 env->spr[SPR_BOOKE_EPR] = sregs.u.e.epr;
1003 }
1004
1005 if (sregs.u.e.features & KVM_SREGS_E_PD) {
1006 env->spr[SPR_BOOKE_EPLC] = sregs.u.e.eplc;
1007 env->spr[SPR_BOOKE_EPSC] = sregs.u.e.epsc;
1008 }
1009
1010 if (sregs.u.e.impl_id == KVM_SREGS_E_IMPL_FSL) {
1011 env->spr[SPR_E500_SVR] = sregs.u.e.impl.fsl.svr;
1012 env->spr[SPR_Exxx_MCAR] = sregs.u.e.impl.fsl.mcar;
1013 env->spr[SPR_HID0] = sregs.u.e.impl.fsl.hid0;
1014
1015 if (sregs.u.e.impl.fsl.features & KVM_SREGS_E_FSL_PIDn) {
1016 env->spr[SPR_BOOKE_PID1] = sregs.u.e.impl.fsl.pid1;
1017 env->spr[SPR_BOOKE_PID2] = sregs.u.e.impl.fsl.pid2;
1018 }
1019 }
1020 }
1021
1022 if (cap_segstate) {
1023 ret = kvm_vcpu_ioctl(cs, KVM_GET_SREGS, &sregs);
1024 if (ret < 0) {
1025 return ret;
1026 }
1027
1028 if (!env->external_htab) {
1029 ppc_store_sdr1(env, sregs.u.s.sdr1);
1030 }
1031
1032 /* Sync SLB */
1033 #ifdef TARGET_PPC64
1034 /*
1035 * The packed SLB array we get from KVM_GET_SREGS only contains
1036 * information about valid entries. So we flush our internal
1037 * copy to get rid of stale ones, then put all valid SLB entries
1038 * back in.
1039 */
1040 memset(env->slb, 0, sizeof(env->slb));
1041 for (i = 0; i < ARRAY_SIZE(env->slb); i++) {
1042 target_ulong rb = sregs.u.s.ppc64.slb[i].slbe;
1043 target_ulong rs = sregs.u.s.ppc64.slb[i].slbv;
1044 /*
1045 * Only restore valid entries
1046 */
1047 if (rb & SLB_ESID_V) {
1048 ppc_store_slb(env, rb, rs);
1049 }
1050 }
1051 #endif
1052
1053 /* Sync SRs */
1054 for (i = 0; i < 16; i++) {
1055 env->sr[i] = sregs.u.s.ppc32.sr[i];
1056 }
1057
1058 /* Sync BATs */
1059 for (i = 0; i < 8; i++) {
1060 env->DBAT[0][i] = sregs.u.s.ppc32.dbat[i] & 0xffffffff;
1061 env->DBAT[1][i] = sregs.u.s.ppc32.dbat[i] >> 32;
1062 env->IBAT[0][i] = sregs.u.s.ppc32.ibat[i] & 0xffffffff;
1063 env->IBAT[1][i] = sregs.u.s.ppc32.ibat[i] >> 32;
1064 }
1065 }
1066
1067 if (cap_hior) {
1068 kvm_get_one_spr(cs, KVM_REG_PPC_HIOR, SPR_HIOR);
1069 }
1070
1071 if (cap_one_reg) {
1072 int i;
1073
1074 /* We deliberately ignore errors here, for kernels which have
1075 * the ONE_REG calls, but don't support the specific
1076 * registers, there's a reasonable chance things will still
1077 * work, at least until we try to migrate. */
1078 for (i = 0; i < 1024; i++) {
1079 uint64_t id = env->spr_cb[i].one_reg_id;
1080
1081 if (id != 0) {
1082 kvm_get_one_spr(cs, id, i);
1083 }
1084 }
1085
1086 #ifdef TARGET_PPC64
1087 if (cap_papr) {
1088 if (kvm_get_vpa(cs) < 0) {
1089 DPRINTF("Warning: Unable to get VPA information from KVM\n");
1090 }
1091 }
1092 #endif
1093 }
1094
1095 return 0;
1096 }
1097
1098 int kvmppc_set_interrupt(PowerPCCPU *cpu, int irq, int level)
1099 {
1100 unsigned virq = level ? KVM_INTERRUPT_SET_LEVEL : KVM_INTERRUPT_UNSET;
1101
1102 if (irq != PPC_INTERRUPT_EXT) {
1103 return 0;
1104 }
1105
1106 if (!kvm_enabled() || !cap_interrupt_unset || !cap_interrupt_level) {
1107 return 0;
1108 }
1109
1110 kvm_vcpu_ioctl(CPU(cpu), KVM_INTERRUPT, &virq);
1111
1112 return 0;
1113 }
1114
1115 #if defined(TARGET_PPCEMB)
1116 #define PPC_INPUT_INT PPC40x_INPUT_INT
1117 #elif defined(TARGET_PPC64)
1118 #define PPC_INPUT_INT PPC970_INPUT_INT
1119 #else
1120 #define PPC_INPUT_INT PPC6xx_INPUT_INT
1121 #endif
1122
1123 void kvm_arch_pre_run(CPUState *cs, struct kvm_run *run)
1124 {
1125 PowerPCCPU *cpu = POWERPC_CPU(cs);
1126 CPUPPCState *env = &cpu->env;
1127 int r;
1128 unsigned irq;
1129
1130 /* PowerPC QEMU tracks the various core input pins (interrupt, critical
1131 * interrupt, reset, etc) in PPC-specific env->irq_input_state. */
1132 if (!cap_interrupt_level &&
1133 run->ready_for_interrupt_injection &&
1134 (cs->interrupt_request & CPU_INTERRUPT_HARD) &&
1135 (env->irq_input_state & (1<<PPC_INPUT_INT)))
1136 {
1137 /* For now KVM disregards the 'irq' argument. However, in the
1138 * future KVM could cache it in-kernel to avoid a heavyweight exit
1139 * when reading the UIC.
1140 */
1141 irq = KVM_INTERRUPT_SET;
1142
1143 DPRINTF("injected interrupt %d\n", irq);
1144 r = kvm_vcpu_ioctl(cs, KVM_INTERRUPT, &irq);
1145 if (r < 0) {
1146 printf("cpu %d fail inject %x\n", cs->cpu_index, irq);
1147 }
1148
1149 /* Always wake up soon in case the interrupt was level based */
1150 timer_mod(idle_timer, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) +
1151 (get_ticks_per_sec() / 50));
1152 }
1153
1154 /* We don't know if there are more interrupts pending after this. However,
1155 * the guest will return to userspace in the course of handling this one
1156 * anyways, so we will get a chance to deliver the rest. */
1157 }
1158
1159 void kvm_arch_post_run(CPUState *cpu, struct kvm_run *run)
1160 {
1161 }
1162
1163 int kvm_arch_process_async_events(CPUState *cs)
1164 {
1165 return cs->halted;
1166 }
1167
1168 static int kvmppc_handle_halt(PowerPCCPU *cpu)
1169 {
1170 CPUState *cs = CPU(cpu);
1171 CPUPPCState *env = &cpu->env;
1172
1173 if (!(cs->interrupt_request & CPU_INTERRUPT_HARD) && (msr_ee)) {
1174 cs->halted = 1;
1175 cs->exception_index = EXCP_HLT;
1176 }
1177
1178 return 0;
1179 }
1180
1181 /* map dcr access to existing qemu dcr emulation */
1182 static int kvmppc_handle_dcr_read(CPUPPCState *env, uint32_t dcrn, uint32_t *data)
1183 {
1184 if (ppc_dcr_read(env->dcr_env, dcrn, data) < 0)
1185 fprintf(stderr, "Read to unhandled DCR (0x%x)\n", dcrn);
1186
1187 return 0;
1188 }
1189
1190 static int kvmppc_handle_dcr_write(CPUPPCState *env, uint32_t dcrn, uint32_t data)
1191 {
1192 if (ppc_dcr_write(env->dcr_env, dcrn, data) < 0)
1193 fprintf(stderr, "Write to unhandled DCR (0x%x)\n", dcrn);
1194
1195 return 0;
1196 }
1197
1198 int kvm_arch_handle_exit(CPUState *cs, struct kvm_run *run)
1199 {
1200 PowerPCCPU *cpu = POWERPC_CPU(cs);
1201 CPUPPCState *env = &cpu->env;
1202 int ret;
1203
1204 switch (run->exit_reason) {
1205 case KVM_EXIT_DCR:
1206 if (run->dcr.is_write) {
1207 DPRINTF("handle dcr write\n");
1208 ret = kvmppc_handle_dcr_write(env, run->dcr.dcrn, run->dcr.data);
1209 } else {
1210 DPRINTF("handle dcr read\n");
1211 ret = kvmppc_handle_dcr_read(env, run->dcr.dcrn, &run->dcr.data);
1212 }
1213 break;
1214 case KVM_EXIT_HLT:
1215 DPRINTF("handle halt\n");
1216 ret = kvmppc_handle_halt(cpu);
1217 break;
1218 #if defined(TARGET_PPC64)
1219 case KVM_EXIT_PAPR_HCALL:
1220 DPRINTF("handle PAPR hypercall\n");
1221 run->papr_hcall.ret = spapr_hypercall(cpu,
1222 run->papr_hcall.nr,
1223 run->papr_hcall.args);
1224 ret = 0;
1225 break;
1226 #endif
1227 case KVM_EXIT_EPR:
1228 DPRINTF("handle epr\n");
1229 run->epr.epr = ldl_phys(cs->as, env->mpic_iack);
1230 ret = 0;
1231 break;
1232 case KVM_EXIT_WATCHDOG:
1233 DPRINTF("handle watchdog expiry\n");
1234 watchdog_perform_action();
1235 ret = 0;
1236 break;
1237
1238 default:
1239 fprintf(stderr, "KVM: unknown exit reason %d\n", run->exit_reason);
1240 ret = -1;
1241 break;
1242 }
1243
1244 return ret;
1245 }
1246
1247 int kvmppc_or_tsr_bits(PowerPCCPU *cpu, uint32_t tsr_bits)
1248 {
1249 CPUState *cs = CPU(cpu);
1250 uint32_t bits = tsr_bits;
1251 struct kvm_one_reg reg = {
1252 .id = KVM_REG_PPC_OR_TSR,
1253 .addr = (uintptr_t) &bits,
1254 };
1255
1256 return kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, &reg);
1257 }
1258
1259 int kvmppc_clear_tsr_bits(PowerPCCPU *cpu, uint32_t tsr_bits)
1260 {
1261
1262 CPUState *cs = CPU(cpu);
1263 uint32_t bits = tsr_bits;
1264 struct kvm_one_reg reg = {
1265 .id = KVM_REG_PPC_CLEAR_TSR,
1266 .addr = (uintptr_t) &bits,
1267 };
1268
1269 return kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, &reg);
1270 }
1271
1272 int kvmppc_set_tcr(PowerPCCPU *cpu)
1273 {
1274 CPUState *cs = CPU(cpu);
1275 CPUPPCState *env = &cpu->env;
1276 uint32_t tcr = env->spr[SPR_BOOKE_TCR];
1277
1278 struct kvm_one_reg reg = {
1279 .id = KVM_REG_PPC_TCR,
1280 .addr = (uintptr_t) &tcr,
1281 };
1282
1283 return kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, &reg);
1284 }
1285
1286 int kvmppc_booke_watchdog_enable(PowerPCCPU *cpu)
1287 {
1288 CPUState *cs = CPU(cpu);
1289 int ret;
1290
1291 if (!kvm_enabled()) {
1292 return -1;
1293 }
1294
1295 if (!cap_ppc_watchdog) {
1296 printf("warning: KVM does not support watchdog");
1297 return -1;
1298 }
1299
1300 ret = kvm_vcpu_enable_cap(cs, KVM_CAP_PPC_BOOKE_WATCHDOG, 0);
1301 if (ret < 0) {
1302 fprintf(stderr, "%s: couldn't enable KVM_CAP_PPC_BOOKE_WATCHDOG: %s\n",
1303 __func__, strerror(-ret));
1304 return ret;
1305 }
1306
1307 return ret;
1308 }
1309
1310 static int read_cpuinfo(const char *field, char *value, int len)
1311 {
1312 FILE *f;
1313 int ret = -1;
1314 int field_len = strlen(field);
1315 char line[512];
1316
1317 f = fopen("/proc/cpuinfo", "r");
1318 if (!f) {
1319 return -1;
1320 }
1321
1322 do {
1323 if(!fgets(line, sizeof(line), f)) {
1324 break;
1325 }
1326 if (!strncmp(line, field, field_len)) {
1327 pstrcpy(value, len, line);
1328 ret = 0;
1329 break;
1330 }
1331 } while(*line);
1332
1333 fclose(f);
1334
1335 return ret;
1336 }
1337
1338 uint32_t kvmppc_get_tbfreq(void)
1339 {
1340 char line[512];
1341 char *ns;
1342 uint32_t retval = get_ticks_per_sec();
1343
1344 if (read_cpuinfo("timebase", line, sizeof(line))) {
1345 return retval;
1346 }
1347
1348 if (!(ns = strchr(line, ':'))) {
1349 return retval;
1350 }
1351
1352 ns++;
1353
1354 retval = atoi(ns);
1355 return retval;
1356 }
1357
1358 /* Try to find a device tree node for a CPU with clock-frequency property */
1359 static int kvmppc_find_cpu_dt(char *buf, int buf_len)
1360 {
1361 struct dirent *dirp;
1362 DIR *dp;
1363
1364 if ((dp = opendir(PROC_DEVTREE_CPU)) == NULL) {
1365 printf("Can't open directory " PROC_DEVTREE_CPU "\n");
1366 return -1;
1367 }
1368
1369 buf[0] = '\0';
1370 while ((dirp = readdir(dp)) != NULL) {
1371 FILE *f;
1372 snprintf(buf, buf_len, "%s%s/clock-frequency", PROC_DEVTREE_CPU,
1373 dirp->d_name);
1374 f = fopen(buf, "r");
1375 if (f) {
1376 snprintf(buf, buf_len, "%s%s", PROC_DEVTREE_CPU, dirp->d_name);
1377 fclose(f);
1378 break;
1379 }
1380 buf[0] = '\0';
1381 }
1382 closedir(dp);
1383 if (buf[0] == '\0') {
1384 printf("Unknown host!\n");
1385 return -1;
1386 }
1387
1388 return 0;
1389 }
1390
1391 /* Read a CPU node property from the host device tree that's a single
1392 * integer (32-bit or 64-bit). Returns 0 if anything goes wrong
1393 * (can't find or open the property, or doesn't understand the
1394 * format) */
1395 static uint64_t kvmppc_read_int_cpu_dt(const char *propname)
1396 {
1397 char buf[PATH_MAX];
1398 union {
1399 uint32_t v32;
1400 uint64_t v64;
1401 } u;
1402 FILE *f;
1403 int len;
1404
1405 if (kvmppc_find_cpu_dt(buf, sizeof(buf))) {
1406 return -1;
1407 }
1408
1409 strncat(buf, "/", sizeof(buf) - strlen(buf));
1410 strncat(buf, propname, sizeof(buf) - strlen(buf));
1411
1412 f = fopen(buf, "rb");
1413 if (!f) {
1414 return -1;
1415 }
1416
1417 len = fread(&u, 1, sizeof(u), f);
1418 fclose(f);
1419 switch (len) {
1420 case 4:
1421 /* property is a 32-bit quantity */
1422 return be32_to_cpu(u.v32);
1423 case 8:
1424 return be64_to_cpu(u.v64);
1425 }
1426
1427 return 0;
1428 }
1429
1430 uint64_t kvmppc_get_clockfreq(void)
1431 {
1432 return kvmppc_read_int_cpu_dt("clock-frequency");
1433 }
1434
1435 uint32_t kvmppc_get_vmx(void)
1436 {
1437 return kvmppc_read_int_cpu_dt("ibm,vmx");
1438 }
1439
1440 uint32_t kvmppc_get_dfp(void)
1441 {
1442 return kvmppc_read_int_cpu_dt("ibm,dfp");
1443 }
1444
1445 static int kvmppc_get_pvinfo(CPUPPCState *env, struct kvm_ppc_pvinfo *pvinfo)
1446 {
1447 PowerPCCPU *cpu = ppc_env_get_cpu(env);
1448 CPUState *cs = CPU(cpu);
1449
1450 if (kvm_check_extension(cs->kvm_state, KVM_CAP_PPC_GET_PVINFO) &&
1451 !kvm_vm_ioctl(cs->kvm_state, KVM_PPC_GET_PVINFO, pvinfo)) {
1452 return 0;
1453 }
1454
1455 return 1;
1456 }
1457
1458 int kvmppc_get_hasidle(CPUPPCState *env)
1459 {
1460 struct kvm_ppc_pvinfo pvinfo;
1461
1462 if (!kvmppc_get_pvinfo(env, &pvinfo) &&
1463 (pvinfo.flags & KVM_PPC_PVINFO_FLAGS_EV_IDLE)) {
1464 return 1;
1465 }
1466
1467 return 0;
1468 }
1469
1470 int kvmppc_get_hypercall(CPUPPCState *env, uint8_t *buf, int buf_len)
1471 {
1472 uint32_t *hc = (uint32_t*)buf;
1473 struct kvm_ppc_pvinfo pvinfo;
1474
1475 if (!kvmppc_get_pvinfo(env, &pvinfo)) {
1476 memcpy(buf, pvinfo.hcall, buf_len);
1477 return 0;
1478 }
1479
1480 /*
1481 * Fallback to always fail hypercalls:
1482 *
1483 * li r3, -1
1484 * nop
1485 * nop
1486 * nop
1487 */
1488
1489 hc[0] = 0x3860ffff;
1490 hc[1] = 0x60000000;
1491 hc[2] = 0x60000000;
1492 hc[3] = 0x60000000;
1493
1494 return 0;
1495 }
1496
1497 void kvmppc_set_papr(PowerPCCPU *cpu)
1498 {
1499 CPUState *cs = CPU(cpu);
1500 int ret;
1501
1502 ret = kvm_vcpu_enable_cap(cs, KVM_CAP_PPC_PAPR, 0);
1503 if (ret) {
1504 cpu_abort(cs, "This KVM version does not support PAPR\n");
1505 }
1506
1507 /* Update the capability flag so we sync the right information
1508 * with kvm */
1509 cap_papr = 1;
1510 }
1511
1512 void kvmppc_set_mpic_proxy(PowerPCCPU *cpu, int mpic_proxy)
1513 {
1514 CPUState *cs = CPU(cpu);
1515 int ret;
1516
1517 ret = kvm_vcpu_enable_cap(cs, KVM_CAP_PPC_EPR, 0, mpic_proxy);
1518 if (ret && mpic_proxy) {
1519 cpu_abort(cs, "This KVM version does not support EPR\n");
1520 }
1521 }
1522
1523 int kvmppc_smt_threads(void)
1524 {
1525 return cap_ppc_smt ? cap_ppc_smt : 1;
1526 }
1527
1528 #ifdef TARGET_PPC64
1529 off_t kvmppc_alloc_rma(const char *name, MemoryRegion *sysmem)
1530 {
1531 void *rma;
1532 off_t size;
1533 int fd;
1534 struct kvm_allocate_rma ret;
1535 MemoryRegion *rma_region;
1536
1537 /* If cap_ppc_rma == 0, contiguous RMA allocation is not supported
1538 * if cap_ppc_rma == 1, contiguous RMA allocation is supported, but
1539 * not necessary on this hardware
1540 * if cap_ppc_rma == 2, contiguous RMA allocation is needed on this hardware
1541 *
1542 * FIXME: We should allow the user to force contiguous RMA
1543 * allocation in the cap_ppc_rma==1 case.
1544 */
1545 if (cap_ppc_rma < 2) {
1546 return 0;
1547 }
1548
1549 fd = kvm_vm_ioctl(kvm_state, KVM_ALLOCATE_RMA, &ret);
1550 if (fd < 0) {
1551 fprintf(stderr, "KVM: Error on KVM_ALLOCATE_RMA: %s\n",
1552 strerror(errno));
1553 return -1;
1554 }
1555
1556 size = MIN(ret.rma_size, 256ul << 20);
1557
1558 rma = mmap(NULL, size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
1559 if (rma == MAP_FAILED) {
1560 fprintf(stderr, "KVM: Error mapping RMA: %s\n", strerror(errno));
1561 return -1;
1562 };
1563
1564 rma_region = g_new(MemoryRegion, 1);
1565 memory_region_init_ram_ptr(rma_region, NULL, name, size, rma);
1566 vmstate_register_ram_global(rma_region);
1567 memory_region_add_subregion(sysmem, 0, rma_region);
1568
1569 return size;
1570 }
1571
1572 uint64_t kvmppc_rma_size(uint64_t current_size, unsigned int hash_shift)
1573 {
1574 struct kvm_ppc_smmu_info info;
1575 long rampagesize, best_page_shift;
1576 int i;
1577
1578 if (cap_ppc_rma >= 2) {
1579 return current_size;
1580 }
1581
1582 /* Find the largest hardware supported page size that's less than
1583 * or equal to the (logical) backing page size of guest RAM */
1584 kvm_get_smmu_info(POWERPC_CPU(first_cpu), &info);
1585 rampagesize = getrampagesize();
1586 best_page_shift = 0;
1587
1588 for (i = 0; i < KVM_PPC_PAGE_SIZES_MAX_SZ; i++) {
1589 struct kvm_ppc_one_seg_page_size *sps = &info.sps[i];
1590
1591 if (!sps->page_shift) {
1592 continue;
1593 }
1594
1595 if ((sps->page_shift > best_page_shift)
1596 && ((1UL << sps->page_shift) <= rampagesize)) {
1597 best_page_shift = sps->page_shift;
1598 }
1599 }
1600
1601 return MIN(current_size,
1602 1ULL << (best_page_shift + hash_shift - 7));
1603 }
1604 #endif
1605
1606 void *kvmppc_create_spapr_tce(uint32_t liobn, uint32_t window_size, int *pfd)
1607 {
1608 struct kvm_create_spapr_tce args = {
1609 .liobn = liobn,
1610 .window_size = window_size,
1611 };
1612 long len;
1613 int fd;
1614 void *table;
1615
1616 /* Must set fd to -1 so we don't try to munmap when called for
1617 * destroying the table, which the upper layers -will- do
1618 */
1619 *pfd = -1;
1620 if (!cap_spapr_tce) {
1621 return NULL;
1622 }
1623
1624 fd = kvm_vm_ioctl(kvm_state, KVM_CREATE_SPAPR_TCE, &args);
1625 if (fd < 0) {
1626 fprintf(stderr, "KVM: Failed to create TCE table for liobn 0x%x\n",
1627 liobn);
1628 return NULL;
1629 }
1630
1631 len = (window_size / SPAPR_TCE_PAGE_SIZE) * sizeof(uint64_t);
1632 /* FIXME: round this up to page size */
1633
1634 table = mmap(NULL, len, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
1635 if (table == MAP_FAILED) {
1636 fprintf(stderr, "KVM: Failed to map TCE table for liobn 0x%x\n",
1637 liobn);
1638 close(fd);
1639 return NULL;
1640 }
1641
1642 *pfd = fd;
1643 return table;
1644 }
1645
1646 int kvmppc_remove_spapr_tce(void *table, int fd, uint32_t window_size)
1647 {
1648 long len;
1649
1650 if (fd < 0) {
1651 return -1;
1652 }
1653
1654 len = (window_size / SPAPR_TCE_PAGE_SIZE)*sizeof(uint64_t);
1655 if ((munmap(table, len) < 0) ||
1656 (close(fd) < 0)) {
1657 fprintf(stderr, "KVM: Unexpected error removing TCE table: %s",
1658 strerror(errno));
1659 /* Leak the table */
1660 }
1661
1662 return 0;
1663 }
1664
1665 int kvmppc_reset_htab(int shift_hint)
1666 {
1667 uint32_t shift = shift_hint;
1668
1669 if (!kvm_enabled()) {
1670 /* Full emulation, tell caller to allocate htab itself */
1671 return 0;
1672 }
1673 if (kvm_check_extension(kvm_state, KVM_CAP_PPC_ALLOC_HTAB)) {
1674 int ret;
1675 ret = kvm_vm_ioctl(kvm_state, KVM_PPC_ALLOCATE_HTAB, &shift);
1676 if (ret == -ENOTTY) {
1677 /* At least some versions of PR KVM advertise the
1678 * capability, but don't implement the ioctl(). Oops.
1679 * Return 0 so that we allocate the htab in qemu, as is
1680 * correct for PR. */
1681 return 0;
1682 } else if (ret < 0) {
1683 return ret;
1684 }
1685 return shift;
1686 }
1687
1688 /* We have a kernel that predates the htab reset calls. For PR
1689 * KVM, we need to allocate the htab ourselves, for an HV KVM of
1690 * this era, it has allocated a 16MB fixed size hash table
1691 * already. Kernels of this era have the GET_PVINFO capability
1692 * only on PR, so we use this hack to determine the right
1693 * answer */
1694 if (kvm_check_extension(kvm_state, KVM_CAP_PPC_GET_PVINFO)) {
1695 /* PR - tell caller to allocate htab */
1696 return 0;
1697 } else {
1698 /* HV - assume 16MB kernel allocated htab */
1699 return 24;
1700 }
1701 }
1702
1703 static inline uint32_t mfpvr(void)
1704 {
1705 uint32_t pvr;
1706
1707 asm ("mfpvr %0"
1708 : "=r"(pvr));
1709 return pvr;
1710 }
1711
1712 static void alter_insns(uint64_t *word, uint64_t flags, bool on)
1713 {
1714 if (on) {
1715 *word |= flags;
1716 } else {
1717 *word &= ~flags;
1718 }
1719 }
1720
1721 static void kvmppc_host_cpu_initfn(Object *obj)
1722 {
1723 assert(kvm_enabled());
1724 }
1725
1726 static void kvmppc_host_cpu_class_init(ObjectClass *oc, void *data)
1727 {
1728 PowerPCCPUClass *pcc = POWERPC_CPU_CLASS(oc);
1729 uint32_t vmx = kvmppc_get_vmx();
1730 uint32_t dfp = kvmppc_get_dfp();
1731 uint32_t dcache_size = kvmppc_read_int_cpu_dt("d-cache-size");
1732 uint32_t icache_size = kvmppc_read_int_cpu_dt("i-cache-size");
1733
1734 /* Now fix up the class with information we can query from the host */
1735 pcc->pvr = mfpvr();
1736
1737 if (vmx != -1) {
1738 /* Only override when we know what the host supports */
1739 alter_insns(&pcc->insns_flags, PPC_ALTIVEC, vmx > 0);
1740 alter_insns(&pcc->insns_flags2, PPC2_VSX, vmx > 1);
1741 }
1742 if (dfp != -1) {
1743 /* Only override when we know what the host supports */
1744 alter_insns(&pcc->insns_flags2, PPC2_DFP, dfp);
1745 }
1746
1747 if (dcache_size != -1) {
1748 pcc->l1_dcache_size = dcache_size;
1749 }
1750
1751 if (icache_size != -1) {
1752 pcc->l1_icache_size = icache_size;
1753 }
1754 }
1755
1756 bool kvmppc_has_cap_epr(void)
1757 {
1758 return cap_epr;
1759 }
1760
1761 bool kvmppc_has_cap_htab_fd(void)
1762 {
1763 return cap_htab_fd;
1764 }
1765
1766 static PowerPCCPUClass *ppc_cpu_get_family_class(PowerPCCPUClass *pcc)
1767 {
1768 ObjectClass *oc = OBJECT_CLASS(pcc);
1769
1770 while (oc && !object_class_is_abstract(oc)) {
1771 oc = object_class_get_parent(oc);
1772 }
1773 assert(oc);
1774
1775 return POWERPC_CPU_CLASS(oc);
1776 }
1777
1778 static int kvm_ppc_register_host_cpu_type(void)
1779 {
1780 TypeInfo type_info = {
1781 .name = TYPE_HOST_POWERPC_CPU,
1782 .instance_init = kvmppc_host_cpu_initfn,
1783 .class_init = kvmppc_host_cpu_class_init,
1784 };
1785 uint32_t host_pvr = mfpvr();
1786 PowerPCCPUClass *pvr_pcc;
1787 DeviceClass *dc;
1788
1789 pvr_pcc = ppc_cpu_class_by_pvr(host_pvr);
1790 if (pvr_pcc == NULL) {
1791 pvr_pcc = ppc_cpu_class_by_pvr_mask(host_pvr);
1792 }
1793 if (pvr_pcc == NULL) {
1794 return -1;
1795 }
1796 type_info.parent = object_class_get_name(OBJECT_CLASS(pvr_pcc));
1797 type_register(&type_info);
1798
1799 /* Register generic family CPU class for a family */
1800 pvr_pcc = ppc_cpu_get_family_class(pvr_pcc);
1801 dc = DEVICE_CLASS(pvr_pcc);
1802 type_info.parent = object_class_get_name(OBJECT_CLASS(pvr_pcc));
1803 type_info.name = g_strdup_printf("%s-"TYPE_POWERPC_CPU, dc->desc);
1804 type_register(&type_info);
1805
1806 return 0;
1807 }
1808
1809 int kvmppc_define_rtas_kernel_token(uint32_t token, const char *function)
1810 {
1811 struct kvm_rtas_token_args args = {
1812 .token = token,
1813 };
1814
1815 if (!kvm_check_extension(kvm_state, KVM_CAP_PPC_RTAS)) {
1816 return -ENOENT;
1817 }
1818
1819 strncpy(args.name, function, sizeof(args.name));
1820
1821 return kvm_vm_ioctl(kvm_state, KVM_PPC_RTAS_DEFINE_TOKEN, &args);
1822 }
1823
1824 int kvmppc_get_htab_fd(bool write)
1825 {
1826 struct kvm_get_htab_fd s = {
1827 .flags = write ? KVM_GET_HTAB_WRITE : 0,
1828 .start_index = 0,
1829 };
1830
1831 if (!cap_htab_fd) {
1832 fprintf(stderr, "KVM version doesn't support saving the hash table\n");
1833 return -1;
1834 }
1835
1836 return kvm_vm_ioctl(kvm_state, KVM_PPC_GET_HTAB_FD, &s);
1837 }
1838
1839 int kvmppc_save_htab(QEMUFile *f, int fd, size_t bufsize, int64_t max_ns)
1840 {
1841 int64_t starttime = qemu_clock_get_ns(QEMU_CLOCK_REALTIME);
1842 uint8_t buf[bufsize];
1843 ssize_t rc;
1844
1845 do {
1846 rc = read(fd, buf, bufsize);
1847 if (rc < 0) {
1848 fprintf(stderr, "Error reading data from KVM HTAB fd: %s\n",
1849 strerror(errno));
1850 return rc;
1851 } else if (rc) {
1852 /* Kernel already retuns data in BE format for the file */
1853 qemu_put_buffer(f, buf, rc);
1854 }
1855 } while ((rc != 0)
1856 && ((max_ns < 0)
1857 || ((qemu_clock_get_ns(QEMU_CLOCK_REALTIME) - starttime) < max_ns)));
1858
1859 return (rc == 0) ? 1 : 0;
1860 }
1861
1862 int kvmppc_load_htab_chunk(QEMUFile *f, int fd, uint32_t index,
1863 uint16_t n_valid, uint16_t n_invalid)
1864 {
1865 struct kvm_get_htab_header *buf;
1866 size_t chunksize = sizeof(*buf) + n_valid*HASH_PTE_SIZE_64;
1867 ssize_t rc;
1868
1869 buf = alloca(chunksize);
1870 /* This is KVM on ppc, so this is all big-endian */
1871 buf->index = index;
1872 buf->n_valid = n_valid;
1873 buf->n_invalid = n_invalid;
1874
1875 qemu_get_buffer(f, (void *)(buf + 1), HASH_PTE_SIZE_64*n_valid);
1876
1877 rc = write(fd, buf, chunksize);
1878 if (rc < 0) {
1879 fprintf(stderr, "Error writing KVM hash table: %s\n",
1880 strerror(errno));
1881 return rc;
1882 }
1883 if (rc != chunksize) {
1884 /* We should never get a short write on a single chunk */
1885 fprintf(stderr, "Short write, restoring KVM hash table\n");
1886 return -1;
1887 }
1888 return 0;
1889 }
1890
1891 bool kvm_arch_stop_on_emulation_error(CPUState *cpu)
1892 {
1893 return true;
1894 }
1895
1896 int kvm_arch_on_sigbus_vcpu(CPUState *cpu, int code, void *addr)
1897 {
1898 return 1;
1899 }
1900
1901 int kvm_arch_on_sigbus(int code, void *addr)
1902 {
1903 return 1;
1904 }
1905
1906 void kvm_arch_init_irq_routing(KVMState *s)
1907 {
1908 }
1909
1910 int kvm_arch_insert_sw_breakpoint(CPUState *cpu, struct kvm_sw_breakpoint *bp)
1911 {
1912 return -EINVAL;
1913 }
1914
1915 int kvm_arch_remove_sw_breakpoint(CPUState *cpu, struct kvm_sw_breakpoint *bp)
1916 {
1917 return -EINVAL;
1918 }
1919
1920 int kvm_arch_insert_hw_breakpoint(target_ulong addr, target_ulong len, int type)
1921 {
1922 return -EINVAL;
1923 }
1924
1925 int kvm_arch_remove_hw_breakpoint(target_ulong addr, target_ulong len, int type)
1926 {
1927 return -EINVAL;
1928 }
1929
1930 void kvm_arch_remove_all_hw_breakpoints(void)
1931 {
1932 }
1933
1934 void kvm_arch_update_guest_debug(CPUState *cpu, struct kvm_guest_debug *dbg)
1935 {
1936 }
1937
1938 struct kvm_get_htab_buf {
1939 struct kvm_get_htab_header header;
1940 /*
1941 * We require one extra byte for read
1942 */
1943 target_ulong hpte[(HPTES_PER_GROUP * 2) + 1];
1944 };
1945
1946 uint64_t kvmppc_hash64_read_pteg(PowerPCCPU *cpu, target_ulong pte_index)
1947 {
1948 int htab_fd;
1949 struct kvm_get_htab_fd ghf;
1950 struct kvm_get_htab_buf *hpte_buf;
1951
1952 ghf.flags = 0;
1953 ghf.start_index = pte_index;
1954 htab_fd = kvm_vm_ioctl(kvm_state, KVM_PPC_GET_HTAB_FD, &ghf);
1955 if (htab_fd < 0) {
1956 goto error_out;
1957 }
1958
1959 hpte_buf = g_malloc0(sizeof(*hpte_buf));
1960 /*
1961 * Read the hpte group
1962 */
1963 if (read(htab_fd, hpte_buf, sizeof(*hpte_buf)) < 0) {
1964 goto out_close;
1965 }
1966
1967 close(htab_fd);
1968 return (uint64_t)(uintptr_t) hpte_buf->hpte;
1969
1970 out_close:
1971 g_free(hpte_buf);
1972 close(htab_fd);
1973 error_out:
1974 return 0;
1975 }
1976
1977 void kvmppc_hash64_free_pteg(uint64_t token)
1978 {
1979 struct kvm_get_htab_buf *htab_buf;
1980
1981 htab_buf = container_of((void *)(uintptr_t) token, struct kvm_get_htab_buf,
1982 hpte);
1983 g_free(htab_buf);
1984 return;
1985 }
1986
1987 void kvmppc_hash64_write_pte(CPUPPCState *env, target_ulong pte_index,
1988 target_ulong pte0, target_ulong pte1)
1989 {
1990 int htab_fd;
1991 struct kvm_get_htab_fd ghf;
1992 struct kvm_get_htab_buf hpte_buf;
1993
1994 ghf.flags = 0;
1995 ghf.start_index = 0; /* Ignored */
1996 htab_fd = kvm_vm_ioctl(kvm_state, KVM_PPC_GET_HTAB_FD, &ghf);
1997 if (htab_fd < 0) {
1998 goto error_out;
1999 }
2000
2001 hpte_buf.header.n_valid = 1;
2002 hpte_buf.header.n_invalid = 0;
2003 hpte_buf.header.index = pte_index;
2004 hpte_buf.hpte[0] = pte0;
2005 hpte_buf.hpte[1] = pte1;
2006 /*
2007 * Write the hpte entry.
2008 * CAUTION: write() has the warn_unused_result attribute. Hence we
2009 * need to check the return value, even though we do nothing.
2010 */
2011 if (write(htab_fd, &hpte_buf, sizeof(hpte_buf)) < 0) {
2012 goto out_close;
2013 }
2014
2015 out_close:
2016 close(htab_fd);
2017 return;
2018
2019 error_out:
2020 return;
2021 }