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