]> Git Repo - qemu.git/blob - target-ppc/kvm.c
kvm: add support for memory transaction attributes
[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 <[email protected]>
9  *  Christian Ehrhardt <[email protected]>
10  *  Hollis Blanchard <[email protected]>
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 "hw/ppc/ppc.h"
39 #include "sysemu/watchdog.h"
40 #include "trace.h"
41 #include "exec/gdbstub.h"
42 #include "exec/memattrs.h"
43
44 //#define DEBUG_KVM
45
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
53
54 #define PROC_DEVTREE_CPU      "/proc/device-tree/cpus/"
55
56 const KVMCapabilityInfo kvm_arch_required_capabilities[] = {
57     KVM_CAP_LAST_INFO
58 };
59
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;
76
77 static uint32_t debug_inst_opcode;
78
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.
83  *
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.
87  */
88 static QEMUTimer *idle_timer;
89
90 static void kvm_kick_cpu(void *opaque)
91 {
92     PowerPCCPU *cpu = opaque;
93
94     qemu_cpu_kick(CPU(cpu));
95 }
96
97 static int kvm_ppc_register_host_cpu_type(void);
98
99 int kvm_arch_init(MachineState *ms, KVMState *s)
100 {
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);
118
119     if (!cap_interrupt_level) {
120         fprintf(stderr, "KVM: Couldn't find level irq capability. Expect the "
121                         "VM to stall at times!\n");
122     }
123
124     kvm_ppc_register_host_cpu_type();
125
126     return 0;
127 }
128
129 static int kvm_arch_sync_sregs(PowerPCCPU *cpu)
130 {
131     CPUPPCState *cenv = &cpu->env;
132     CPUState *cs = CPU(cpu);
133     struct kvm_sregs sregs;
134     int ret;
135
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;
146         }
147     }
148
149     ret = kvm_vcpu_ioctl(cs, KVM_GET_SREGS, &sregs);
150     if (ret) {
151         return ret;
152     }
153
154     sregs.pvr = cenv->spr[SPR_PVR];
155     return kvm_vcpu_ioctl(cs, KVM_SET_SREGS, &sregs);
156 }
157
158 /* Set up a shared TLB array with KVM */
159 static int kvm_booke206_tlb_init(PowerPCCPU *cpu)
160 {
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;
167
168     if (!kvm_enabled() ||
169         !kvm_check_extension(cs->kvm_state, KVM_CAP_SW_TLB)) {
170         return 0;
171     }
172
173     assert(ARRAY_SIZE(params.tlb_sizes) == BOOKE206_MAX_TLBN);
174
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];
179     }
180
181     assert(entries == env->nb_tlb);
182     assert(sizeof(struct kvm_book3e_206_tlb_entry) == sizeof(ppcmas_tlb_t));
183
184     env->tlb_dirty = true;
185
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;
190
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;
196     }
197
198     env->kvm_sw_tlb = true;
199     return 0;
200 }
201
202
203 #if defined(TARGET_PPC64)
204 static void kvm_get_fallback_smmu_info(PowerPCCPU *cpu,
205                                        struct kvm_ppc_smmu_info *info)
206 {
207     CPUPPCState *env = &cpu->env;
208     CPUState *cs = CPU(cpu);
209
210     memset(info, 0, sizeof(*info));
211
212     /* We don't have the new KVM_PPC_GET_SMMU_INFO ioctl, so
213      * need to "guess" what the supported page sizes are.
214      *
215      * For that to work we make a few assumptions:
216      *
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.
221      *
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
226      *
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.
234      */
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;
239
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;
245
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;
253
254         /* HV KVM has backing store size restrictions */
255         info->flags = KVM_PPC_PAGE_SIZES_REAL;
256
257         if (env->mmu_model & POWERPC_MMU_1TSEG) {
258             info->flags |= KVM_PPC_1T_SEGMENTS;
259         }
260
261         if (env->mmu_model == POWERPC_MMU_2_06) {
262             info->slb_size = 32;
263         } else {
264             info->slb_size = 64;
265         }
266
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++;
273
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++;
281         }
282
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;
288     }
289 }
290
291 static void kvm_get_smmu_info(PowerPCCPU *cpu, struct kvm_ppc_smmu_info *info)
292 {
293     CPUState *cs = CPU(cpu);
294     int ret;
295
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;
300         }
301     }
302
303     kvm_get_fallback_smmu_info(cpu, info);
304 }
305
306 static long getrampagesize(void)
307 {
308     struct statfs fs;
309     int ret;
310
311     if (!mem_path) {
312         /* guest RAM is backed by normal anonymous pages */
313         return getpagesize();
314     }
315
316     do {
317         ret = statfs(mem_path, &fs);
318     } while (ret != 0 && errno == EINTR);
319
320     if (ret != 0) {
321         fprintf(stderr, "Couldn't statfs() memory path: %s\n",
322                 strerror(errno));
323         exit(1);
324     }
325
326 #define HUGETLBFS_MAGIC       0x958458f6
327
328     if (fs.f_type != HUGETLBFS_MAGIC) {
329         /* Explicit mempath, but it's ordinary pages */
330         return getpagesize();
331     }
332
333     /* It's hugepage, return the huge page size */
334     return fs.f_bsize;
335 }
336
337 static bool kvm_valid_page_size(uint32_t flags, long rampgsize, uint32_t shift)
338 {
339     if (!(flags & KVM_PPC_PAGE_SIZES_REAL)) {
340         return true;
341     }
342
343     return (1ul << shift) <= rampgsize;
344 }
345
346 static void kvm_fixup_page_sizes(PowerPCCPU *cpu)
347 {
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;
353
354     /* We only handle page sizes for 64-bit server guests for now */
355     if (!(env->mmu_model & POWERPC_MMU_64)) {
356         return;
357     }
358
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;
363     }
364
365     rampagesize = getrampagesize();
366
367     /* Convert to QEMU form */
368     memset(&env->sps, 0, sizeof(env->sps));
369
370     /*
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.
373      */
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];
377
378         if (!kvm_valid_page_size(smmu_info.flags, rampagesize,
379                                  ksps->page_shift)) {
380             continue;
381         }
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;
388             }
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;
393             }
394         }
395         if (++iq >= PPC_PAGE_SIZES_MAX_SZ) {
396             break;
397         }
398     }
399     env->slb_nr = smmu_info.slb_size;
400     if (!(smmu_info.flags & KVM_PPC_1T_SEGMENTS)) {
401         env->mmu_model &= ~POWERPC_MMU_1TSEG;
402     }
403 }
404 #else /* defined (TARGET_PPC64) */
405
406 static inline void kvm_fixup_page_sizes(PowerPCCPU *cpu)
407 {
408 }
409
410 #endif /* !defined (TARGET_PPC64) */
411
412 unsigned long kvm_arch_vcpu_id(CPUState *cpu)
413 {
414     return ppc_get_vcpu_dt_id(POWERPC_CPU(cpu));
415 }
416
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.
420  */
421 #define MAX_HW_BKPTS 4
422
423 static struct HWBreakpoint {
424     target_ulong addr;
425     int type;
426 } hw_debug_points[MAX_HW_BKPTS];
427
428 static CPUWatchpoint hw_watchpoint;
429
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;
435
436 static void kvmppc_hw_debug_points_init(CPUPPCState *cenv)
437 {
438     if (cenv->excp_model == POWERPC_EXCP_BOOKE) {
439         max_hw_breakpoint = 2;
440         max_hw_watchpoint = 2;
441     }
442
443     if ((max_hw_breakpoint + max_hw_watchpoint) > MAX_HW_BKPTS) {
444         fprintf(stderr, "Error initializing h/w breakpoints\n");
445         return;
446     }
447 }
448
449 int kvm_arch_init_vcpu(CPUState *cs)
450 {
451     PowerPCCPU *cpu = POWERPC_CPU(cs);
452     CPUPPCState *cenv = &cpu->env;
453     int ret;
454
455     /* Gather server mmu info from KVM and update the CPU state */
456     kvm_fixup_page_sizes(cpu);
457
458     /* Synchronize sregs with kvm */
459     ret = kvm_arch_sync_sregs(cpu);
460     if (ret) {
461         return ret;
462     }
463
464     idle_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, kvm_kick_cpu, cpu);
465
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;
473     }
474
475     kvm_get_one_reg(cs, KVM_REG_PPC_DEBUG_INST, &debug_inst_opcode);
476     kvmppc_hw_debug_points_init(cenv);
477
478     return ret;
479 }
480
481 static void kvm_sw_tlb_put(PowerPCCPU *cpu)
482 {
483     CPUPPCState *env = &cpu->env;
484     CPUState *cs = CPU(cpu);
485     struct kvm_dirty_tlb dirty_tlb;
486     unsigned char *bitmap;
487     int ret;
488
489     if (!env->kvm_sw_tlb) {
490         return;
491     }
492
493     bitmap = g_malloc((env->nb_tlb + 7) / 8);
494     memset(bitmap, 0xFF, (env->nb_tlb + 7) / 8);
495
496     dirty_tlb.bitmap = (uintptr_t)bitmap;
497     dirty_tlb.num_dirty = env->nb_tlb;
498
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));
503     }
504
505     g_free(bitmap);
506 }
507
508 static void kvm_get_one_spr(CPUState *cs, uint64_t id, int spr)
509 {
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,
519     };
520     int ret;
521
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;
530
531         case KVM_REG_SIZE_U64:
532             env->spr[spr] = val.u64;
533             break;
534
535         default:
536             /* Don't handle this size yet */
537             abort();
538         }
539     }
540 }
541
542 static void kvm_put_one_spr(CPUState *cs, uint64_t id, int spr)
543 {
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,
553     };
554     int ret;
555
556     switch (id & KVM_REG_SIZE_MASK) {
557     case KVM_REG_SIZE_U32:
558         val.u32 = env->spr[spr];
559         break;
560
561     case KVM_REG_SIZE_U64:
562         val.u64 = env->spr[spr];
563         break;
564
565     default:
566         /* Don't handle this size yet */
567         abort();
568     }
569
570     ret = kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, &reg);
571     if (ret != 0) {
572         trace_kvm_failed_spr_set(spr, strerror(errno));
573     }
574 }
575
576 static int kvm_put_fp(CPUState *cs)
577 {
578     PowerPCCPU *cpu = POWERPC_CPU(cs);
579     CPUPPCState *env = &cpu->env;
580     struct kvm_one_reg reg;
581     int i;
582     int ret;
583
584     if (env->insns_flags & PPC_FLOAT) {
585         uint64_t fpscr = env->fpscr;
586         bool vsx = !!(env->insns_flags2 & PPC2_VSX);
587
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;
594         }
595
596         for (i = 0; i < 32; i++) {
597             uint64_t vsr[2];
598
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);
603
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;
609             }
610         }
611     }
612
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;
620         }
621
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;
629             }
630         }
631     }
632
633     return 0;
634 }
635
636 static int kvm_get_fp(CPUState *cs)
637 {
638     PowerPCCPU *cpu = POWERPC_CPU(cs);
639     CPUPPCState *env = &cpu->env;
640     struct kvm_one_reg reg;
641     int i;
642     int ret;
643
644     if (env->insns_flags & PPC_FLOAT) {
645         uint64_t fpscr;
646         bool vsx = !!(env->insns_flags2 & PPC2_VSX);
647
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;
656         }
657
658         for (i = 0; i < 32; i++) {
659             uint64_t vsr[2];
660
661             reg.addr = (uintptr_t) &vsr;
662             reg.id = vsx ? KVM_REG_PPC_VSR(i) : KVM_REG_PPC_FPR(i);
663
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];
673                 }
674             }
675         }
676     }
677
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;
685         }
686
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;
695             }
696         }
697     }
698
699     return 0;
700 }
701
702 #if defined(TARGET_PPC64)
703 static int kvm_get_vpa(CPUState *cs)
704 {
705     PowerPCCPU *cpu = POWERPC_CPU(cs);
706     CPUPPCState *env = &cpu->env;
707     struct kvm_one_reg reg;
708     int ret;
709
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;
716     }
717
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;
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_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;
737     }
738
739     return 0;
740 }
741
742 static int kvm_put_vpa(CPUState *cs)
743 {
744     PowerPCCPU *cpu = POWERPC_CPU(cs);
745     CPUPPCState *env = &cpu->env;
746     struct kvm_one_reg reg;
747     int ret;
748
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));
754
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;
762         }
763     }
764
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;
773     }
774
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;
783     }
784
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;
792         }
793     }
794
795     return 0;
796 }
797 #endif /* TARGET_PPC64 */
798
799 int kvm_arch_put_registers(CPUState *cs, int level)
800 {
801     PowerPCCPU *cpu = POWERPC_CPU(cs);
802     CPUPPCState *env = &cpu->env;
803     struct kvm_regs regs;
804     int ret;
805     int i;
806
807     ret = kvm_vcpu_ioctl(cs, KVM_GET_REGS, &regs);
808     if (ret < 0) {
809         return ret;
810     }
811
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;
817
818     regs.srr0 = env->spr[SPR_SRR0];
819     regs.srr1 = env->spr[SPR_SRR1];
820
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];
829
830     regs.pid = env->spr[SPR_BOOKE_PID];
831
832     for (i = 0;i < 32; i++)
833         regs.gpr[i] = env->gpr[i];
834
835     regs.cr = 0;
836     for (i = 0; i < 8; i++) {
837         regs.cr |= (env->crf[i] & 15) << (4 * (7 - i));
838     }
839
840     ret = kvm_vcpu_ioctl(cs, KVM_SET_REGS, &regs);
841     if (ret < 0)
842         return ret;
843
844     kvm_put_fp(cs);
845
846     if (env->tlb_dirty) {
847         kvm_sw_tlb_put(cpu);
848         env->tlb_dirty = false;
849     }
850
851     if (cap_segstate && (level >= KVM_PUT_RESET_STATE)) {
852         struct kvm_sregs sregs;
853
854         sregs.pvr = env->spr[SPR_PVR];
855
856         sregs.u.s.sdr1 = env->spr[SPR_SDR1];
857
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;
864             }
865             sregs.u.s.ppc64.slb[i].slbv = env->slb[i].vsid;
866         }
867 #endif
868
869         /* Sync SRs */
870         for (i = 0; i < 16; i++) {
871             sregs.u.s.ppc32.sr[i] = env->sr[i];
872         }
873
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];
881         }
882
883         ret = kvm_vcpu_ioctl(cs, KVM_SET_SREGS, &sregs);
884         if (ret) {
885             return ret;
886         }
887     }
888
889     if (cap_hior && (level >= KVM_PUT_RESET_STATE)) {
890         kvm_put_one_spr(cs, KVM_REG_PPC_HIOR, SPR_HIOR);
891     }
892
893     if (cap_one_reg) {
894         int i;
895
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;
902
903             if (id != 0) {
904                 kvm_put_one_spr(cs, id, i);
905             }
906         }
907
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]);
912             }
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]);
915             }
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);
926         }
927
928         if (cap_papr) {
929             if (kvm_put_vpa(cs) < 0) {
930                 DPRINTF("Warning: Unable to set VPA information to KVM\n");
931             }
932         }
933
934         kvm_set_one_reg(cs, KVM_REG_PPC_TB_OFFSET, &env->tb_env->tb_offset);
935 #endif /* TARGET_PPC64 */
936     }
937
938     return ret;
939 }
940
941 static void kvm_sync_excp(CPUPPCState *env, int vector, int ivor)
942 {
943      env->excp_vectors[vector] = env->spr[ivor] + env->spr[SPR_BOOKE_IVPR];
944 }
945
946 int kvm_arch_get_registers(CPUState *cs)
947 {
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;
954
955     ret = kvm_vcpu_ioctl(cs, KVM_GET_REGS, &regs);
956     if (ret < 0)
957         return ret;
958
959     cr = regs.cr;
960     for (i = 7; i >= 0; i--) {
961         env->crf[i] = cr & 15;
962         cr >>= 4;
963     }
964
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;
970
971     env->spr[SPR_SRR0] = regs.srr0;
972     env->spr[SPR_SRR1] = regs.srr1;
973
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;
982
983     env->spr[SPR_BOOKE_PID] = regs.pid;
984
985     for (i = 0;i < 32; i++)
986         env->gpr[i] = regs.gpr[i];
987
988     kvm_get_fp(cs);
989
990     if (cap_booke_sregs) {
991         ret = kvm_vcpu_ioctl(cs, KVM_GET_SREGS, &sregs);
992         if (ret < 0) {
993             return ret;
994         }
995
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;
1008         }
1009
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;
1016         }
1017
1018         if (sregs.u.e.features & KVM_SREGS_E_64) {
1019             env->spr[SPR_BOOKE_EPCR] = sregs.u.e.epcr;
1020         }
1021
1022         if (sregs.u.e.features & KVM_SREGS_E_SPRG8) {
1023             env->spr[SPR_BOOKE_SPRG8] = sregs.u.e.sprg8;
1024         }
1025
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);
1059
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);
1067             }
1068
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);
1072             }
1073
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);
1079             }
1080         }
1081
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];
1093         }
1094
1095         if (sregs.u.e.features & KVM_SREGS_EXP) {
1096             env->spr[SPR_BOOKE_EPR] = sregs.u.e.epr;
1097         }
1098
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;
1102         }
1103
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;
1108
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;
1112             }
1113         }
1114     }
1115
1116     if (cap_segstate) {
1117         ret = kvm_vcpu_ioctl(cs, KVM_GET_SREGS, &sregs);
1118         if (ret < 0) {
1119             return ret;
1120         }
1121
1122         if (!env->external_htab) {
1123             ppc_store_sdr1(env, sregs.u.s.sdr1);
1124         }
1125
1126         /* Sync SLB */
1127 #ifdef TARGET_PPC64
1128         /*
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.
1133          */
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;
1138             /*
1139              * Only restore valid entries
1140              */
1141             if (rb & SLB_ESID_V) {
1142                 ppc_store_slb(env, rb, rs);
1143             }
1144         }
1145 #endif
1146
1147         /* Sync SRs */
1148         for (i = 0; i < 16; i++) {
1149             env->sr[i] = sregs.u.s.ppc32.sr[i];
1150         }
1151
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;
1158         }
1159     }
1160
1161     if (cap_hior) {
1162         kvm_get_one_spr(cs, KVM_REG_PPC_HIOR, SPR_HIOR);
1163     }
1164
1165     if (cap_one_reg) {
1166         int i;
1167
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;
1174
1175             if (id != 0) {
1176                 kvm_get_one_spr(cs, id, i);
1177             }
1178         }
1179
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]);
1184             }
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]);
1187             }
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);
1198         }
1199
1200         if (cap_papr) {
1201             if (kvm_get_vpa(cs) < 0) {
1202                 DPRINTF("Warning: Unable to get VPA information from KVM\n");
1203             }
1204         }
1205
1206         kvm_get_one_reg(cs, KVM_REG_PPC_TB_OFFSET, &env->tb_env->tb_offset);
1207 #endif
1208     }
1209
1210     return 0;
1211 }
1212
1213 int kvmppc_set_interrupt(PowerPCCPU *cpu, int irq, int level)
1214 {
1215     unsigned virq = level ? KVM_INTERRUPT_SET_LEVEL : KVM_INTERRUPT_UNSET;
1216
1217     if (irq != PPC_INTERRUPT_EXT) {
1218         return 0;
1219     }
1220
1221     if (!kvm_enabled() || !cap_interrupt_unset || !cap_interrupt_level) {
1222         return 0;
1223     }
1224
1225     kvm_vcpu_ioctl(CPU(cpu), KVM_INTERRUPT, &virq);
1226
1227     return 0;
1228 }
1229
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
1237
1238 void kvm_arch_pre_run(CPUState *cs, struct kvm_run *run)
1239 {
1240     PowerPCCPU *cpu = POWERPC_CPU(cs);
1241     CPUPPCState *env = &cpu->env;
1242     int r;
1243     unsigned irq;
1244
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)))
1251     {
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.
1255          */
1256         irq = KVM_INTERRUPT_SET;
1257
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);
1262         }
1263
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));
1267     }
1268
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. */
1272 }
1273
1274 MemTxAttrs kvm_arch_post_run(CPUState *cs, struct kvm_run *run)
1275 {
1276     return MEMTXATTRS_UNSPECIFIED;
1277 }
1278
1279 int kvm_arch_process_async_events(CPUState *cs)
1280 {
1281     return cs->halted;
1282 }
1283
1284 static int kvmppc_handle_halt(PowerPCCPU *cpu)
1285 {
1286     CPUState *cs = CPU(cpu);
1287     CPUPPCState *env = &cpu->env;
1288
1289     if (!(cs->interrupt_request & CPU_INTERRUPT_HARD) && (msr_ee)) {
1290         cs->halted = 1;
1291         cs->exception_index = EXCP_HLT;
1292     }
1293
1294     return 0;
1295 }
1296
1297 /* map dcr access to existing qemu dcr emulation */
1298 static int kvmppc_handle_dcr_read(CPUPPCState *env, uint32_t dcrn, uint32_t *data)
1299 {
1300     if (ppc_dcr_read(env->dcr_env, dcrn, data) < 0)
1301         fprintf(stderr, "Read to unhandled DCR (0x%x)\n", dcrn);
1302
1303     return 0;
1304 }
1305
1306 static int kvmppc_handle_dcr_write(CPUPPCState *env, uint32_t dcrn, uint32_t data)
1307 {
1308     if (ppc_dcr_write(env->dcr_env, dcrn, data) < 0)
1309         fprintf(stderr, "Write to unhandled DCR (0x%x)\n", dcrn);
1310
1311     return 0;
1312 }
1313
1314 int kvm_arch_insert_sw_breakpoint(CPUState *cs, struct kvm_sw_breakpoint *bp)
1315 {
1316     /* Mixed endian case is not handled */
1317     uint32_t sc = debug_inst_opcode;
1318
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;
1323     }
1324
1325     return 0;
1326 }
1327
1328 int kvm_arch_remove_sw_breakpoint(CPUState *cs, struct kvm_sw_breakpoint *bp)
1329 {
1330     uint32_t sc;
1331
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;
1337     }
1338
1339     return 0;
1340 }
1341
1342 static int find_hw_breakpoint(target_ulong addr, int type)
1343 {
1344     int n;
1345
1346     assert((nb_hw_breakpoint + nb_hw_watchpoint)
1347            <= ARRAY_SIZE(hw_debug_points));
1348
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;
1353         }
1354     }
1355
1356     return -1;
1357 }
1358
1359 static int find_hw_watchpoint(target_ulong addr, int *flag)
1360 {
1361     int n;
1362
1363     n = find_hw_breakpoint(addr, GDB_WATCHPOINT_ACCESS);
1364     if (n >= 0) {
1365         *flag = BP_MEM_ACCESS;
1366         return n;
1367     }
1368
1369     n = find_hw_breakpoint(addr, GDB_WATCHPOINT_WRITE);
1370     if (n >= 0) {
1371         *flag = BP_MEM_WRITE;
1372         return n;
1373     }
1374
1375     n = find_hw_breakpoint(addr, GDB_WATCHPOINT_READ);
1376     if (n >= 0) {
1377         *flag = BP_MEM_READ;
1378         return n;
1379     }
1380
1381     return -1;
1382 }
1383
1384 int kvm_arch_insert_hw_breakpoint(target_ulong addr,
1385                                   target_ulong len, int type)
1386 {
1387     if ((nb_hw_breakpoint + nb_hw_watchpoint) >= ARRAY_SIZE(hw_debug_points)) {
1388         return -ENOBUFS;
1389     }
1390
1391     hw_debug_points[nb_hw_breakpoint + nb_hw_watchpoint].addr = addr;
1392     hw_debug_points[nb_hw_breakpoint + nb_hw_watchpoint].type = type;
1393
1394     switch (type) {
1395     case GDB_BREAKPOINT_HW:
1396         if (nb_hw_breakpoint >= max_hw_breakpoint) {
1397             return -ENOBUFS;
1398         }
1399
1400         if (find_hw_breakpoint(addr, type) >= 0) {
1401             return -EEXIST;
1402         }
1403
1404         nb_hw_breakpoint++;
1405         break;
1406
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;
1412         }
1413
1414         if (find_hw_breakpoint(addr, type) >= 0) {
1415             return -EEXIST;
1416         }
1417
1418         nb_hw_watchpoint++;
1419         break;
1420
1421     default:
1422         return -ENOSYS;
1423     }
1424
1425     return 0;
1426 }
1427
1428 int kvm_arch_remove_hw_breakpoint(target_ulong addr,
1429                                   target_ulong len, int type)
1430 {
1431     int n;
1432
1433     n = find_hw_breakpoint(addr, type);
1434     if (n < 0) {
1435         return -ENOENT;
1436     }
1437
1438     switch (type) {
1439     case GDB_BREAKPOINT_HW:
1440         nb_hw_breakpoint--;
1441         break;
1442
1443     case GDB_WATCHPOINT_WRITE:
1444     case GDB_WATCHPOINT_READ:
1445     case GDB_WATCHPOINT_ACCESS:
1446         nb_hw_watchpoint--;
1447         break;
1448
1449     default:
1450         return -ENOSYS;
1451     }
1452     hw_debug_points[n] = hw_debug_points[nb_hw_breakpoint + nb_hw_watchpoint];
1453
1454     return 0;
1455 }
1456
1457 void kvm_arch_remove_all_hw_breakpoints(void)
1458 {
1459     nb_hw_breakpoint = nb_hw_watchpoint = 0;
1460 }
1461
1462 void kvm_arch_update_guest_debug(CPUState *cs, struct kvm_guest_debug *dbg)
1463 {
1464     int n;
1465
1466     /* Software Breakpoint updates */
1467     if (kvm_sw_breakpoints_active(cs)) {
1468         dbg->control |= KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP;
1469     }
1470
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));
1474
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");
1495             }
1496             dbg->arch.bp[n].addr = hw_debug_points[n].addr;
1497         }
1498     }
1499 }
1500
1501 static int kvm_handle_debug(PowerPCCPU *cpu, struct kvm_run *run)
1502 {
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;
1509
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;
1518                 }
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;
1527                 }
1528             }
1529         }
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.
1552          */
1553
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.
1557          */
1558         env->nip += 4;
1559         cs->exception_index = POWERPC_EXCP_PROGRAM;
1560         env->error_code = POWERPC_EXCP_INVAL;
1561         ppc_cpu_do_interrupt(cs);
1562     }
1563
1564     return handle;
1565 }
1566
1567 int kvm_arch_handle_exit(CPUState *cs, struct kvm_run *run)
1568 {
1569     PowerPCCPU *cpu = POWERPC_CPU(cs);
1570     CPUPPCState *env = &cpu->env;
1571     int ret;
1572
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);
1581         }
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;
1606
1607     case KVM_EXIT_DEBUG:
1608         DPRINTF("handle debug exception\n");
1609         if (kvm_handle_debug(cpu, run)) {
1610             ret = EXCP_DEBUG;
1611             break;
1612         }
1613         /* re-enter, this exception was guest-internal */
1614         ret = 0;
1615         break;
1616
1617     default:
1618         fprintf(stderr, "KVM: unknown exit reason %d\n", run->exit_reason);
1619         ret = -1;
1620         break;
1621     }
1622
1623     return ret;
1624 }
1625
1626 int kvmppc_or_tsr_bits(PowerPCCPU *cpu, uint32_t tsr_bits)
1627 {
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,
1633     };
1634
1635     return kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, &reg);
1636 }
1637
1638 int kvmppc_clear_tsr_bits(PowerPCCPU *cpu, uint32_t tsr_bits)
1639 {
1640
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,
1646     };
1647
1648     return kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, &reg);
1649 }
1650
1651 int kvmppc_set_tcr(PowerPCCPU *cpu)
1652 {
1653     CPUState *cs = CPU(cpu);
1654     CPUPPCState *env = &cpu->env;
1655     uint32_t tcr = env->spr[SPR_BOOKE_TCR];
1656
1657     struct kvm_one_reg reg = {
1658         .id = KVM_REG_PPC_TCR,
1659         .addr = (uintptr_t) &tcr,
1660     };
1661
1662     return kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, &reg);
1663 }
1664
1665 int kvmppc_booke_watchdog_enable(PowerPCCPU *cpu)
1666 {
1667     CPUState *cs = CPU(cpu);
1668     int ret;
1669
1670     if (!kvm_enabled()) {
1671         return -1;
1672     }
1673
1674     if (!cap_ppc_watchdog) {
1675         printf("warning: KVM does not support watchdog");
1676         return -1;
1677     }
1678
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;
1684     }
1685
1686     return ret;
1687 }
1688
1689 static int read_cpuinfo(const char *field, char *value, int len)
1690 {
1691     FILE *f;
1692     int ret = -1;
1693     int field_len = strlen(field);
1694     char line[512];
1695
1696     f = fopen("/proc/cpuinfo", "r");
1697     if (!f) {
1698         return -1;
1699     }
1700
1701     do {
1702         if (!fgets(line, sizeof(line), f)) {
1703             break;
1704         }
1705         if (!strncmp(line, field, field_len)) {
1706             pstrcpy(value, len, line);
1707             ret = 0;
1708             break;
1709         }
1710     } while(*line);
1711
1712     fclose(f);
1713
1714     return ret;
1715 }
1716
1717 uint32_t kvmppc_get_tbfreq(void)
1718 {
1719     char line[512];
1720     char *ns;
1721     uint32_t retval = get_ticks_per_sec();
1722
1723     if (read_cpuinfo("timebase", line, sizeof(line))) {
1724         return retval;
1725     }
1726
1727     if (!(ns = strchr(line, ':'))) {
1728         return retval;
1729     }
1730
1731     ns++;
1732
1733     retval = atoi(ns);
1734     return retval;
1735 }
1736
1737 bool kvmppc_get_host_serial(char **value)
1738 {
1739     return g_file_get_contents("/proc/device-tree/system-id", value, NULL,
1740                                NULL);
1741 }
1742
1743 bool kvmppc_get_host_model(char **value)
1744 {
1745     return g_file_get_contents("/proc/device-tree/model", value, NULL, NULL);
1746 }
1747
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)
1750 {
1751     struct dirent *dirp;
1752     DIR *dp;
1753
1754     if ((dp = opendir(PROC_DEVTREE_CPU)) == NULL) {
1755         printf("Can't open directory " PROC_DEVTREE_CPU "\n");
1756         return -1;
1757     }
1758
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;
1769         }
1770         buf[0] = '\0';
1771     }
1772     closedir(dp);
1773     if (buf[0] == '\0') {
1774         printf("Unknown host!\n");
1775         return -1;
1776     }
1777
1778     return 0;
1779 }
1780
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)
1786 {
1787     char buf[PATH_MAX], *tmp;
1788     union {
1789         uint32_t v32;
1790         uint64_t v64;
1791     } u;
1792     FILE *f;
1793     int len;
1794
1795     if (kvmppc_find_cpu_dt(buf, sizeof(buf))) {
1796         return -1;
1797     }
1798
1799     tmp = g_strdup_printf("%s/%s", buf, propname);
1800
1801     f = fopen(tmp, "rb");
1802     g_free(tmp);
1803     if (!f) {
1804         return -1;
1805     }
1806
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);
1815     }
1816
1817     return 0;
1818 }
1819
1820 uint64_t kvmppc_get_clockfreq(void)
1821 {
1822     return kvmppc_read_int_cpu_dt("clock-frequency");
1823 }
1824
1825 uint32_t kvmppc_get_vmx(void)
1826 {
1827     return kvmppc_read_int_cpu_dt("ibm,vmx");
1828 }
1829
1830 uint32_t kvmppc_get_dfp(void)
1831 {
1832     return kvmppc_read_int_cpu_dt("ibm,dfp");
1833 }
1834
1835 static int kvmppc_get_pvinfo(CPUPPCState *env, struct kvm_ppc_pvinfo *pvinfo)
1836  {
1837      PowerPCCPU *cpu = ppc_env_get_cpu(env);
1838      CPUState *cs = CPU(cpu);
1839
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;
1843     }
1844
1845     return 1;
1846 }
1847
1848 int kvmppc_get_hasidle(CPUPPCState *env)
1849 {
1850     struct kvm_ppc_pvinfo pvinfo;
1851
1852     if (!kvmppc_get_pvinfo(env, &pvinfo) &&
1853         (pvinfo.flags & KVM_PPC_PVINFO_FLAGS_EV_IDLE)) {
1854         return 1;
1855     }
1856
1857     return 0;
1858 }
1859
1860 int kvmppc_get_hypercall(CPUPPCState *env, uint8_t *buf, int buf_len)
1861 {
1862     uint32_t *hc = (uint32_t*)buf;
1863     struct kvm_ppc_pvinfo pvinfo;
1864
1865     if (!kvmppc_get_pvinfo(env, &pvinfo)) {
1866         memcpy(buf, pvinfo.hcall, buf_len);
1867         return 0;
1868     }
1869
1870     /*
1871      * Fallback to always fail hypercalls regardless of endianness:
1872      *
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)
1877      */
1878
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));
1883
1884     return 0;
1885 }
1886
1887 void kvmppc_set_papr(PowerPCCPU *cpu)
1888 {
1889     CPUState *cs = CPU(cpu);
1890     int ret;
1891
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");
1895     }
1896
1897     /* Update the capability flag so we sync the right information
1898      * with kvm */
1899     cap_papr = 1;
1900 }
1901
1902 int kvmppc_set_compat(PowerPCCPU *cpu, uint32_t cpu_version)
1903 {
1904     return kvm_set_one_reg(CPU(cpu), KVM_REG_PPC_ARCH_COMPAT, &cpu_version);
1905 }
1906
1907 void kvmppc_set_mpic_proxy(PowerPCCPU *cpu, int mpic_proxy)
1908 {
1909     CPUState *cs = CPU(cpu);
1910     int ret;
1911
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");
1915     }
1916 }
1917
1918 int kvmppc_smt_threads(void)
1919 {
1920     return cap_ppc_smt ? cap_ppc_smt : 1;
1921 }
1922
1923 #ifdef TARGET_PPC64
1924 off_t kvmppc_alloc_rma(void **rma)
1925 {
1926     off_t size;
1927     int fd;
1928     struct kvm_allocate_rma ret;
1929
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
1934      *
1935      * FIXME: We should allow the user to force contiguous RMA
1936      * allocation in the cap_ppc_rma==1 case.
1937      */
1938     if (cap_ppc_rma < 2) {
1939         return 0;
1940     }
1941
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;
1947     }
1948
1949     size = MIN(ret.rma_size, 256ul << 20);
1950
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;
1955     };
1956
1957     return size;
1958 }
1959
1960 uint64_t kvmppc_rma_size(uint64_t current_size, unsigned int hash_shift)
1961 {
1962     struct kvm_ppc_smmu_info info;
1963     long rampagesize, best_page_shift;
1964     int i;
1965
1966     if (cap_ppc_rma >= 2) {
1967         return current_size;
1968     }
1969
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;
1975
1976     for (i = 0; i < KVM_PPC_PAGE_SIZES_MAX_SZ; i++) {
1977         struct kvm_ppc_one_seg_page_size *sps = &info.sps[i];
1978
1979         if (!sps->page_shift) {
1980             continue;
1981         }
1982
1983         if ((sps->page_shift > best_page_shift)
1984             && ((1UL << sps->page_shift) <= rampagesize)) {
1985             best_page_shift = sps->page_shift;
1986         }
1987     }
1988
1989     return MIN(current_size,
1990                1ULL << (best_page_shift + hash_shift - 7));
1991 }
1992 #endif
1993
1994 bool kvmppc_spapr_use_multitce(void)
1995 {
1996     return cap_spapr_multitce;
1997 }
1998
1999 void *kvmppc_create_spapr_tce(uint32_t liobn, uint32_t window_size, int *pfd,
2000                               bool vfio_accel)
2001 {
2002     struct kvm_create_spapr_tce args = {
2003         .liobn = liobn,
2004         .window_size = window_size,
2005     };
2006     long len;
2007     int fd;
2008     void *table;
2009
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
2012      */
2013     *pfd = -1;
2014     if (!cap_spapr_tce || (vfio_accel && !cap_spapr_vfio)) {
2015         return NULL;
2016     }
2017
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;
2023     }
2024
2025     len = (window_size / SPAPR_TCE_PAGE_SIZE) * sizeof(uint64_t);
2026     /* FIXME: round this up to page size */
2027
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;
2034     }
2035
2036     *pfd = fd;
2037     return table;
2038 }
2039
2040 int kvmppc_remove_spapr_tce(void *table, int fd, uint32_t nb_table)
2041 {
2042     long len;
2043
2044     if (fd < 0) {
2045         return -1;
2046     }
2047
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 */
2054     }
2055
2056     return 0;
2057 }
2058
2059 int kvmppc_reset_htab(int shift_hint)
2060 {
2061     uint32_t shift = shift_hint;
2062
2063     if (!kvm_enabled()) {
2064         /* Full emulation, tell caller to allocate htab itself */
2065         return 0;
2066     }
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;
2078         }
2079         return shift;
2080     }
2081
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;
2094     }
2095 }
2096
2097 static inline uint32_t mfpvr(void)
2098 {
2099     uint32_t pvr;
2100
2101     asm ("mfpvr %0"
2102          : "=r"(pvr));
2103     return pvr;
2104 }
2105
2106 static void alter_insns(uint64_t *word, uint64_t flags, bool on)
2107 {
2108     if (on) {
2109         *word |= flags;
2110     } else {
2111         *word &= ~flags;
2112     }
2113 }
2114
2115 static void kvmppc_host_cpu_initfn(Object *obj)
2116 {
2117     assert(kvm_enabled());
2118 }
2119
2120 static void kvmppc_host_cpu_class_init(ObjectClass *oc, void *data)
2121 {
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");
2127
2128     /* Now fix up the class with information we can query from the host */
2129     pcc->pvr = mfpvr();
2130
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);
2135     }
2136     if (dfp != -1) {
2137         /* Only override when we know what the host supports */
2138         alter_insns(&pcc->insns_flags2, PPC2_DFP, dfp);
2139     }
2140
2141     if (dcache_size != -1) {
2142         pcc->l1_dcache_size = dcache_size;
2143     }
2144
2145     if (icache_size != -1) {
2146         pcc->l1_icache_size = icache_size;
2147     }
2148 }
2149
2150 bool kvmppc_has_cap_epr(void)
2151 {
2152     return cap_epr;
2153 }
2154
2155 bool kvmppc_has_cap_htab_fd(void)
2156 {
2157     return cap_htab_fd;
2158 }
2159
2160 bool kvmppc_has_cap_fixup_hcalls(void)
2161 {
2162     return cap_fixup_hcalls;
2163 }
2164
2165 static PowerPCCPUClass *ppc_cpu_get_family_class(PowerPCCPUClass *pcc)
2166 {
2167     ObjectClass *oc = OBJECT_CLASS(pcc);
2168
2169     while (oc && !object_class_is_abstract(oc)) {
2170         oc = object_class_get_parent(oc);
2171     }
2172     assert(oc);
2173
2174     return POWERPC_CPU_CLASS(oc);
2175 }
2176
2177 static int kvm_ppc_register_host_cpu_type(void)
2178 {
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,
2183     };
2184     uint32_t host_pvr = mfpvr();
2185     PowerPCCPUClass *pvr_pcc;
2186     DeviceClass *dc;
2187
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);
2191     }
2192     if (pvr_pcc == NULL) {
2193         return -1;
2194     }
2195     type_info.parent = object_class_get_name(OBJECT_CLASS(pvr_pcc));
2196     type_register(&type_info);
2197
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);
2204
2205     return 0;
2206 }
2207
2208 int kvmppc_define_rtas_kernel_token(uint32_t token, const char *function)
2209 {
2210     struct kvm_rtas_token_args args = {
2211         .token = token,
2212     };
2213
2214     if (!kvm_check_extension(kvm_state, KVM_CAP_PPC_RTAS)) {
2215         return -ENOENT;
2216     }
2217
2218     strncpy(args.name, function, sizeof(args.name));
2219
2220     return kvm_vm_ioctl(kvm_state, KVM_PPC_RTAS_DEFINE_TOKEN, &args);
2221 }
2222
2223 int kvmppc_get_htab_fd(bool write)
2224 {
2225     struct kvm_get_htab_fd s = {
2226         .flags = write ? KVM_GET_HTAB_WRITE : 0,
2227         .start_index = 0,
2228     };
2229
2230     if (!cap_htab_fd) {
2231         fprintf(stderr, "KVM version doesn't support saving the hash table\n");
2232         return -1;
2233     }
2234
2235     return kvm_vm_ioctl(kvm_state, KVM_PPC_GET_HTAB_FD, &s);
2236 }
2237
2238 int kvmppc_save_htab(QEMUFile *f, int fd, size_t bufsize, int64_t max_ns)
2239 {
2240     int64_t starttime = qemu_clock_get_ns(QEMU_CLOCK_REALTIME);
2241     uint8_t buf[bufsize];
2242     ssize_t rc;
2243
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;
2258
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);
2264
2265                 buffer += chunksize;
2266                 n -= chunksize;
2267             }
2268         }
2269     } while ((rc != 0)
2270              && ((max_ns < 0)
2271                  || ((qemu_clock_get_ns(QEMU_CLOCK_REALTIME) - starttime) < max_ns)));
2272
2273     return (rc == 0) ? 1 : 0;
2274 }
2275
2276 int kvmppc_load_htab_chunk(QEMUFile *f, int fd, uint32_t index,
2277                            uint16_t n_valid, uint16_t n_invalid)
2278 {
2279     struct kvm_get_htab_header *buf;
2280     size_t chunksize = sizeof(*buf) + n_valid*HASH_PTE_SIZE_64;
2281     ssize_t rc;
2282
2283     buf = alloca(chunksize);
2284     buf->index = index;
2285     buf->n_valid = n_valid;
2286     buf->n_invalid = n_invalid;
2287
2288     qemu_get_buffer(f, (void *)(buf + 1), HASH_PTE_SIZE_64*n_valid);
2289
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;
2295     }
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;
2300     }
2301     return 0;
2302 }
2303
2304 bool kvm_arch_stop_on_emulation_error(CPUState *cpu)
2305 {
2306     return true;
2307 }
2308
2309 int kvm_arch_on_sigbus_vcpu(CPUState *cpu, int code, void *addr)
2310 {
2311     return 1;
2312 }
2313
2314 int kvm_arch_on_sigbus(int code, void *addr)
2315 {
2316     return 1;
2317 }
2318
2319 void kvm_arch_init_irq_routing(KVMState *s)
2320 {
2321 }
2322
2323 struct kvm_get_htab_buf {
2324     struct kvm_get_htab_header header;
2325     /*
2326      * We require one extra byte for read
2327      */
2328     target_ulong hpte[(HPTES_PER_GROUP * 2) + 1];
2329 };
2330
2331 uint64_t kvmppc_hash64_read_pteg(PowerPCCPU *cpu, target_ulong pte_index)
2332 {
2333     int htab_fd;
2334     struct kvm_get_htab_fd ghf;
2335     struct kvm_get_htab_buf  *hpte_buf;
2336
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;
2342     }
2343
2344     hpte_buf = g_malloc0(sizeof(*hpte_buf));
2345     /*
2346      * Read the hpte group
2347      */
2348     if (read(htab_fd, hpte_buf, sizeof(*hpte_buf)) < 0) {
2349         goto out_close;
2350     }
2351
2352     close(htab_fd);
2353     return (uint64_t)(uintptr_t) hpte_buf->hpte;
2354
2355 out_close:
2356     g_free(hpte_buf);
2357     close(htab_fd);
2358 error_out:
2359     return 0;
2360 }
2361
2362 void kvmppc_hash64_free_pteg(uint64_t token)
2363 {
2364     struct kvm_get_htab_buf *htab_buf;
2365
2366     htab_buf = container_of((void *)(uintptr_t) token, struct kvm_get_htab_buf,
2367                             hpte);
2368     g_free(htab_buf);
2369     return;
2370 }
2371
2372 void kvmppc_hash64_write_pte(CPUPPCState *env, target_ulong pte_index,
2373                              target_ulong pte0, target_ulong pte1)
2374 {
2375     int htab_fd;
2376     struct kvm_get_htab_fd ghf;
2377     struct kvm_get_htab_buf hpte_buf;
2378
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;
2384     }
2385
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;
2391     /*
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.
2395      */
2396     if (write(htab_fd, &hpte_buf, sizeof(hpte_buf)) < 0) {
2397         goto out_close;
2398     }
2399
2400 out_close:
2401     close(htab_fd);
2402     return;
2403
2404 error_out:
2405     return;
2406 }
2407
2408 int kvm_arch_fixup_msi_route(struct kvm_irq_routing_entry *route,
2409                              uint64_t address, uint32_t data)
2410 {
2411     return 0;
2412 }
This page took 0.166153 seconds and 4 git commands to generate.