]>
Commit | Line | Data |
---|---|---|
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 <[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 | ||
0d75590d | 17 | #include "qemu/osdep.h" |
eadaada1 | 18 | #include <dirent.h> |
d76d1650 | 19 | #include <sys/ioctl.h> |
4656e1f0 | 20 | #include <sys/vfs.h> |
d76d1650 AJ |
21 | |
22 | #include <linux/kvm.h> | |
23 | ||
24 | #include "qemu-common.h" | |
072ed5f2 | 25 | #include "qemu/error-report.h" |
33c11879 | 26 | #include "cpu.h" |
715d4b96 | 27 | #include "cpu-models.h" |
1de7afc9 | 28 | #include "qemu/timer.h" |
9c17d615 | 29 | #include "sysemu/sysemu.h" |
b3946626 | 30 | #include "sysemu/hw_accel.h" |
d76d1650 | 31 | #include "kvm_ppc.h" |
9c17d615 PB |
32 | #include "sysemu/cpus.h" |
33 | #include "sysemu/device_tree.h" | |
d5aea6f3 | 34 | #include "mmu-hash64.h" |
d76d1650 | 35 | |
f61b4bed | 36 | #include "hw/sysbus.h" |
0d09e41a PB |
37 | #include "hw/ppc/spapr.h" |
38 | #include "hw/ppc/spapr_vio.h" | |
7ebaf795 | 39 | #include "hw/ppc/spapr_cpu_core.h" |
98a8b524 | 40 | #include "hw/ppc/ppc.h" |
31f2cb8f | 41 | #include "sysemu/watchdog.h" |
b36f100e | 42 | #include "trace.h" |
88365d17 | 43 | #include "exec/gdbstub.h" |
4c663752 | 44 | #include "exec/memattrs.h" |
9c607668 | 45 | #include "exec/ram_addr.h" |
2d103aae | 46 | #include "sysemu/hostmem.h" |
f348b6d1 | 47 | #include "qemu/cutils.h" |
9c607668 | 48 | #include "qemu/mmap-alloc.h" |
3b542549 BR |
49 | #if defined(TARGET_PPC64) |
50 | #include "hw/ppc/spapr_cpu_core.h" | |
51 | #endif | |
f3d9f303 | 52 | #include "elf.h" |
c64abd1f | 53 | #include "sysemu/kvm_int.h" |
f61b4bed | 54 | |
d76d1650 AJ |
55 | //#define DEBUG_KVM |
56 | ||
57 | #ifdef DEBUG_KVM | |
da56ff91 | 58 | #define DPRINTF(fmt, ...) \ |
d76d1650 AJ |
59 | do { fprintf(stderr, fmt, ## __VA_ARGS__); } while (0) |
60 | #else | |
da56ff91 | 61 | #define DPRINTF(fmt, ...) \ |
d76d1650 AJ |
62 | do { } while (0) |
63 | #endif | |
64 | ||
eadaada1 AG |
65 | #define PROC_DEVTREE_CPU "/proc/device-tree/cpus/" |
66 | ||
94a8d39a JK |
67 | const KVMCapabilityInfo kvm_arch_required_capabilities[] = { |
68 | KVM_CAP_LAST_INFO | |
69 | }; | |
70 | ||
fc87e185 AG |
71 | static int cap_interrupt_unset = false; |
72 | static int cap_interrupt_level = false; | |
90dc8812 | 73 | static int cap_segstate; |
90dc8812 | 74 | static int cap_booke_sregs; |
e97c3636 | 75 | static int cap_ppc_smt; |
354ac20a | 76 | static int cap_ppc_rma; |
0f5cb298 | 77 | static int cap_spapr_tce; |
d6ee2a7c | 78 | static int cap_spapr_tce_64; |
da95324e | 79 | static int cap_spapr_multitce; |
9bb62a07 | 80 | static int cap_spapr_vfio; |
f1af19d7 | 81 | static int cap_hior; |
d67d40ea | 82 | static int cap_one_reg; |
3b961124 | 83 | static int cap_epr; |
31f2cb8f | 84 | static int cap_ppc_watchdog; |
9b00ea49 | 85 | static int cap_papr; |
e68cb8b4 | 86 | static int cap_htab_fd; |
87a91de6 | 87 | static int cap_fixup_hcalls; |
bac3bf28 | 88 | static int cap_htm; /* Hardware transactional memory support */ |
cf1c4cce SB |
89 | static int cap_mmu_radix; |
90 | static int cap_mmu_hash_v3; | |
fc87e185 | 91 | |
3c902d44 BB |
92 | static uint32_t debug_inst_opcode; |
93 | ||
c821c2bd AG |
94 | /* XXX We have a race condition where we actually have a level triggered |
95 | * interrupt, but the infrastructure can't expose that yet, so the guest | |
96 | * takes but ignores it, goes to sleep and never gets notified that there's | |
97 | * still an interrupt pending. | |
c6a94ba5 | 98 | * |
c821c2bd AG |
99 | * As a quick workaround, let's just wake up again 20 ms after we injected |
100 | * an interrupt. That way we can assure that we're always reinjecting | |
101 | * interrupts in case the guest swallowed them. | |
c6a94ba5 AG |
102 | */ |
103 | static QEMUTimer *idle_timer; | |
104 | ||
d5a68146 | 105 | static void kvm_kick_cpu(void *opaque) |
c6a94ba5 | 106 | { |
d5a68146 | 107 | PowerPCCPU *cpu = opaque; |
d5a68146 | 108 | |
c08d7424 | 109 | qemu_cpu_kick(CPU(cpu)); |
c6a94ba5 AG |
110 | } |
111 | ||
96c9cff0 TH |
112 | /* Check whether we are running with KVM-PR (instead of KVM-HV). This |
113 | * should only be used for fallback tests - generally we should use | |
114 | * explicit capabilities for the features we want, rather than | |
115 | * assuming what is/isn't available depending on the KVM variant. */ | |
116 | static bool kvmppc_is_pr(KVMState *ks) | |
117 | { | |
118 | /* Assume KVM-PR if the GET_PVINFO capability is available */ | |
119 | return kvm_check_extension(ks, KVM_CAP_PPC_GET_PVINFO) != 0; | |
120 | } | |
121 | ||
5ba4576b AF |
122 | static int kvm_ppc_register_host_cpu_type(void); |
123 | ||
b16565b3 | 124 | int kvm_arch_init(MachineState *ms, KVMState *s) |
d76d1650 | 125 | { |
fc87e185 | 126 | cap_interrupt_unset = kvm_check_extension(s, KVM_CAP_PPC_UNSET_IRQ); |
fc87e185 | 127 | cap_interrupt_level = kvm_check_extension(s, KVM_CAP_PPC_IRQ_LEVEL); |
90dc8812 | 128 | cap_segstate = kvm_check_extension(s, KVM_CAP_PPC_SEGSTATE); |
90dc8812 | 129 | cap_booke_sregs = kvm_check_extension(s, KVM_CAP_PPC_BOOKE_SREGS); |
e97c3636 | 130 | cap_ppc_smt = kvm_check_extension(s, KVM_CAP_PPC_SMT); |
354ac20a | 131 | cap_ppc_rma = kvm_check_extension(s, KVM_CAP_PPC_RMA); |
0f5cb298 | 132 | cap_spapr_tce = kvm_check_extension(s, KVM_CAP_SPAPR_TCE); |
d6ee2a7c | 133 | cap_spapr_tce_64 = kvm_check_extension(s, KVM_CAP_SPAPR_TCE_64); |
da95324e | 134 | cap_spapr_multitce = kvm_check_extension(s, KVM_CAP_SPAPR_MULTITCE); |
9bb62a07 | 135 | cap_spapr_vfio = false; |
d67d40ea | 136 | cap_one_reg = kvm_check_extension(s, KVM_CAP_ONE_REG); |
f1af19d7 | 137 | cap_hior = kvm_check_extension(s, KVM_CAP_PPC_HIOR); |
3b961124 | 138 | cap_epr = kvm_check_extension(s, KVM_CAP_PPC_EPR); |
31f2cb8f | 139 | cap_ppc_watchdog = kvm_check_extension(s, KVM_CAP_PPC_BOOKE_WATCHDOG); |
9b00ea49 DG |
140 | /* Note: we don't set cap_papr here, because this capability is |
141 | * only activated after this by kvmppc_set_papr() */ | |
e68cb8b4 | 142 | cap_htab_fd = kvm_check_extension(s, KVM_CAP_PPC_HTAB_FD); |
87a91de6 | 143 | cap_fixup_hcalls = kvm_check_extension(s, KVM_CAP_PPC_FIXUP_HCALL); |
bac3bf28 | 144 | cap_htm = kvm_vm_check_extension(s, KVM_CAP_PPC_HTM); |
cf1c4cce SB |
145 | cap_mmu_radix = kvm_vm_check_extension(s, KVM_CAP_PPC_MMU_RADIX); |
146 | cap_mmu_hash_v3 = kvm_vm_check_extension(s, KVM_CAP_PPC_MMU_HASH_V3); | |
fc87e185 AG |
147 | |
148 | if (!cap_interrupt_level) { | |
149 | fprintf(stderr, "KVM: Couldn't find level irq capability. Expect the " | |
150 | "VM to stall at times!\n"); | |
151 | } | |
152 | ||
5ba4576b AF |
153 | kvm_ppc_register_host_cpu_type(); |
154 | ||
d76d1650 AJ |
155 | return 0; |
156 | } | |
157 | ||
d525ffab PB |
158 | int kvm_arch_irqchip_create(MachineState *ms, KVMState *s) |
159 | { | |
160 | return 0; | |
161 | } | |
162 | ||
1bc22652 | 163 | static int kvm_arch_sync_sregs(PowerPCCPU *cpu) |
d76d1650 | 164 | { |
1bc22652 AF |
165 | CPUPPCState *cenv = &cpu->env; |
166 | CPUState *cs = CPU(cpu); | |
861bbc80 | 167 | struct kvm_sregs sregs; |
5666ca4a SW |
168 | int ret; |
169 | ||
170 | if (cenv->excp_model == POWERPC_EXCP_BOOKE) { | |
64e07be5 AG |
171 | /* What we're really trying to say is "if we're on BookE, we use |
172 | the native PVR for now". This is the only sane way to check | |
173 | it though, so we potentially confuse users that they can run | |
174 | BookE guests on BookS. Let's hope nobody dares enough :) */ | |
5666ca4a SW |
175 | return 0; |
176 | } else { | |
90dc8812 | 177 | if (!cap_segstate) { |
64e07be5 AG |
178 | fprintf(stderr, "kvm error: missing PVR setting capability\n"); |
179 | return -ENOSYS; | |
5666ca4a | 180 | } |
5666ca4a SW |
181 | } |
182 | ||
1bc22652 | 183 | ret = kvm_vcpu_ioctl(cs, KVM_GET_SREGS, &sregs); |
5666ca4a SW |
184 | if (ret) { |
185 | return ret; | |
186 | } | |
861bbc80 AG |
187 | |
188 | sregs.pvr = cenv->spr[SPR_PVR]; | |
1bc22652 | 189 | return kvm_vcpu_ioctl(cs, KVM_SET_SREGS, &sregs); |
5666ca4a SW |
190 | } |
191 | ||
93dd5e85 | 192 | /* Set up a shared TLB array with KVM */ |
1bc22652 | 193 | static int kvm_booke206_tlb_init(PowerPCCPU *cpu) |
93dd5e85 | 194 | { |
1bc22652 AF |
195 | CPUPPCState *env = &cpu->env; |
196 | CPUState *cs = CPU(cpu); | |
93dd5e85 SW |
197 | struct kvm_book3e_206_tlb_params params = {}; |
198 | struct kvm_config_tlb cfg = {}; | |
93dd5e85 SW |
199 | unsigned int entries = 0; |
200 | int ret, i; | |
201 | ||
202 | if (!kvm_enabled() || | |
a60f24b5 | 203 | !kvm_check_extension(cs->kvm_state, KVM_CAP_SW_TLB)) { |
93dd5e85 SW |
204 | return 0; |
205 | } | |
206 | ||
207 | assert(ARRAY_SIZE(params.tlb_sizes) == BOOKE206_MAX_TLBN); | |
208 | ||
209 | for (i = 0; i < BOOKE206_MAX_TLBN; i++) { | |
210 | params.tlb_sizes[i] = booke206_tlb_size(env, i); | |
211 | params.tlb_ways[i] = booke206_tlb_ways(env, i); | |
212 | entries += params.tlb_sizes[i]; | |
213 | } | |
214 | ||
215 | assert(entries == env->nb_tlb); | |
216 | assert(sizeof(struct kvm_book3e_206_tlb_entry) == sizeof(ppcmas_tlb_t)); | |
217 | ||
218 | env->tlb_dirty = true; | |
219 | ||
220 | cfg.array = (uintptr_t)env->tlb.tlbm; | |
221 | cfg.array_len = sizeof(ppcmas_tlb_t) * entries; | |
222 | cfg.params = (uintptr_t)¶ms; | |
223 | cfg.mmu_type = KVM_MMU_FSL_BOOKE_NOHV; | |
224 | ||
48add816 | 225 | ret = kvm_vcpu_enable_cap(cs, KVM_CAP_SW_TLB, 0, (uintptr_t)&cfg); |
93dd5e85 SW |
226 | if (ret < 0) { |
227 | fprintf(stderr, "%s: couldn't enable KVM_CAP_SW_TLB: %s\n", | |
228 | __func__, strerror(-ret)); | |
229 | return ret; | |
230 | } | |
231 | ||
232 | env->kvm_sw_tlb = true; | |
233 | return 0; | |
234 | } | |
235 | ||
4656e1f0 BH |
236 | |
237 | #if defined(TARGET_PPC64) | |
a60f24b5 | 238 | static void kvm_get_fallback_smmu_info(PowerPCCPU *cpu, |
4656e1f0 BH |
239 | struct kvm_ppc_smmu_info *info) |
240 | { | |
a60f24b5 AF |
241 | CPUPPCState *env = &cpu->env; |
242 | CPUState *cs = CPU(cpu); | |
243 | ||
4656e1f0 BH |
244 | memset(info, 0, sizeof(*info)); |
245 | ||
246 | /* We don't have the new KVM_PPC_GET_SMMU_INFO ioctl, so | |
247 | * need to "guess" what the supported page sizes are. | |
248 | * | |
249 | * For that to work we make a few assumptions: | |
250 | * | |
96c9cff0 TH |
251 | * - Check whether we are running "PR" KVM which only supports 4K |
252 | * and 16M pages, but supports them regardless of the backing | |
253 | * store characteritics. We also don't support 1T segments. | |
4656e1f0 BH |
254 | * |
255 | * This is safe as if HV KVM ever supports that capability or PR | |
256 | * KVM grows supports for more page/segment sizes, those versions | |
257 | * will have implemented KVM_CAP_PPC_GET_SMMU_INFO and thus we | |
258 | * will not hit this fallback | |
259 | * | |
260 | * - Else we are running HV KVM. This means we only support page | |
261 | * sizes that fit in the backing store. Additionally we only | |
262 | * advertize 64K pages if the processor is ARCH 2.06 and we assume | |
263 | * P7 encodings for the SLB and hash table. Here too, we assume | |
264 | * support for any newer processor will mean a kernel that | |
265 | * implements KVM_CAP_PPC_GET_SMMU_INFO and thus doesn't hit | |
266 | * this fallback. | |
267 | */ | |
96c9cff0 | 268 | if (kvmppc_is_pr(cs->kvm_state)) { |
4656e1f0 BH |
269 | /* No flags */ |
270 | info->flags = 0; | |
271 | info->slb_size = 64; | |
272 | ||
273 | /* Standard 4k base page size segment */ | |
274 | info->sps[0].page_shift = 12; | |
275 | info->sps[0].slb_enc = 0; | |
276 | info->sps[0].enc[0].page_shift = 12; | |
277 | info->sps[0].enc[0].pte_enc = 0; | |
278 | ||
279 | /* Standard 16M large page size segment */ | |
280 | info->sps[1].page_shift = 24; | |
281 | info->sps[1].slb_enc = SLB_VSID_L; | |
282 | info->sps[1].enc[0].page_shift = 24; | |
283 | info->sps[1].enc[0].pte_enc = 0; | |
284 | } else { | |
285 | int i = 0; | |
286 | ||
287 | /* HV KVM has backing store size restrictions */ | |
288 | info->flags = KVM_PPC_PAGE_SIZES_REAL; | |
289 | ||
290 | if (env->mmu_model & POWERPC_MMU_1TSEG) { | |
291 | info->flags |= KVM_PPC_1T_SEGMENTS; | |
292 | } | |
293 | ||
ec975e83 SB |
294 | if (POWERPC_MMU_VER(env->mmu_model) == POWERPC_MMU_VER_2_06 || |
295 | POWERPC_MMU_VER(env->mmu_model) == POWERPC_MMU_VER_2_07) { | |
4656e1f0 BH |
296 | info->slb_size = 32; |
297 | } else { | |
298 | info->slb_size = 64; | |
299 | } | |
300 | ||
301 | /* Standard 4k base page size segment */ | |
302 | info->sps[i].page_shift = 12; | |
303 | info->sps[i].slb_enc = 0; | |
304 | info->sps[i].enc[0].page_shift = 12; | |
305 | info->sps[i].enc[0].pte_enc = 0; | |
306 | i++; | |
307 | ||
aa4bb587 | 308 | /* 64K on MMU 2.06 and later */ |
ec975e83 SB |
309 | if (POWERPC_MMU_VER(env->mmu_model) == POWERPC_MMU_VER_2_06 || |
310 | POWERPC_MMU_VER(env->mmu_model) == POWERPC_MMU_VER_2_07) { | |
4656e1f0 BH |
311 | info->sps[i].page_shift = 16; |
312 | info->sps[i].slb_enc = 0x110; | |
313 | info->sps[i].enc[0].page_shift = 16; | |
314 | info->sps[i].enc[0].pte_enc = 1; | |
315 | i++; | |
316 | } | |
317 | ||
318 | /* Standard 16M large page size segment */ | |
319 | info->sps[i].page_shift = 24; | |
320 | info->sps[i].slb_enc = SLB_VSID_L; | |
321 | info->sps[i].enc[0].page_shift = 24; | |
322 | info->sps[i].enc[0].pte_enc = 0; | |
323 | } | |
324 | } | |
325 | ||
a60f24b5 | 326 | static void kvm_get_smmu_info(PowerPCCPU *cpu, struct kvm_ppc_smmu_info *info) |
4656e1f0 | 327 | { |
a60f24b5 | 328 | CPUState *cs = CPU(cpu); |
4656e1f0 BH |
329 | int ret; |
330 | ||
a60f24b5 AF |
331 | if (kvm_check_extension(cs->kvm_state, KVM_CAP_PPC_GET_SMMU_INFO)) { |
332 | ret = kvm_vm_ioctl(cs->kvm_state, KVM_PPC_GET_SMMU_INFO, info); | |
4656e1f0 BH |
333 | if (ret == 0) { |
334 | return; | |
335 | } | |
336 | } | |
337 | ||
a60f24b5 | 338 | kvm_get_fallback_smmu_info(cpu, info); |
4656e1f0 BH |
339 | } |
340 | ||
c64abd1f SB |
341 | struct ppc_radix_page_info *kvm_get_radix_page_info(void) |
342 | { | |
343 | KVMState *s = KVM_STATE(current_machine->accelerator); | |
344 | struct ppc_radix_page_info *radix_page_info; | |
345 | struct kvm_ppc_rmmu_info rmmu_info; | |
346 | int i; | |
347 | ||
348 | if (!kvm_check_extension(s, KVM_CAP_PPC_MMU_RADIX)) { | |
349 | return NULL; | |
350 | } | |
351 | if (kvm_vm_ioctl(s, KVM_PPC_GET_RMMU_INFO, &rmmu_info)) { | |
352 | return NULL; | |
353 | } | |
354 | radix_page_info = g_malloc0(sizeof(*radix_page_info)); | |
355 | radix_page_info->count = 0; | |
356 | for (i = 0; i < PPC_PAGE_SIZES_MAX_SZ; i++) { | |
357 | if (rmmu_info.ap_encodings[i]) { | |
358 | radix_page_info->entries[i] = rmmu_info.ap_encodings[i]; | |
359 | radix_page_info->count++; | |
360 | } | |
361 | } | |
362 | return radix_page_info; | |
363 | } | |
364 | ||
b4db5413 SJS |
365 | target_ulong kvmppc_configure_v3_mmu(PowerPCCPU *cpu, |
366 | bool radix, bool gtse, | |
367 | uint64_t proc_tbl) | |
368 | { | |
369 | CPUState *cs = CPU(cpu); | |
370 | int ret; | |
371 | uint64_t flags = 0; | |
372 | struct kvm_ppc_mmuv3_cfg cfg = { | |
373 | .process_table = proc_tbl, | |
374 | }; | |
375 | ||
376 | if (radix) { | |
377 | flags |= KVM_PPC_MMUV3_RADIX; | |
378 | } | |
379 | if (gtse) { | |
380 | flags |= KVM_PPC_MMUV3_GTSE; | |
381 | } | |
382 | cfg.flags = flags; | |
383 | ret = kvm_vm_ioctl(cs->kvm_state, KVM_PPC_CONFIGURE_V3_MMU, &cfg); | |
384 | switch (ret) { | |
385 | case 0: | |
386 | return H_SUCCESS; | |
387 | case -EINVAL: | |
388 | return H_PARAMETER; | |
389 | case -ENODEV: | |
390 | return H_NOT_AVAILABLE; | |
391 | default: | |
392 | return H_HARDWARE; | |
393 | } | |
394 | } | |
395 | ||
4656e1f0 BH |
396 | static bool kvm_valid_page_size(uint32_t flags, long rampgsize, uint32_t shift) |
397 | { | |
398 | if (!(flags & KVM_PPC_PAGE_SIZES_REAL)) { | |
399 | return true; | |
400 | } | |
401 | ||
402 | return (1ul << shift) <= rampgsize; | |
403 | } | |
404 | ||
df587133 TH |
405 | static long max_cpu_page_size; |
406 | ||
a60f24b5 | 407 | static void kvm_fixup_page_sizes(PowerPCCPU *cpu) |
4656e1f0 BH |
408 | { |
409 | static struct kvm_ppc_smmu_info smmu_info; | |
410 | static bool has_smmu_info; | |
a60f24b5 | 411 | CPUPPCState *env = &cpu->env; |
4656e1f0 | 412 | int iq, ik, jq, jk; |
0d594f55 | 413 | bool has_64k_pages = false; |
4656e1f0 BH |
414 | |
415 | /* We only handle page sizes for 64-bit server guests for now */ | |
416 | if (!(env->mmu_model & POWERPC_MMU_64)) { | |
417 | return; | |
418 | } | |
419 | ||
420 | /* Collect MMU info from kernel if not already */ | |
421 | if (!has_smmu_info) { | |
a60f24b5 | 422 | kvm_get_smmu_info(cpu, &smmu_info); |
4656e1f0 BH |
423 | has_smmu_info = true; |
424 | } | |
425 | ||
df587133 | 426 | if (!max_cpu_page_size) { |
9c607668 | 427 | max_cpu_page_size = qemu_getrampagesize(); |
df587133 | 428 | } |
4656e1f0 BH |
429 | |
430 | /* Convert to QEMU form */ | |
431 | memset(&env->sps, 0, sizeof(env->sps)); | |
432 | ||
90da0d5a BH |
433 | /* If we have HV KVM, we need to forbid CI large pages if our |
434 | * host page size is smaller than 64K. | |
435 | */ | |
436 | if (smmu_info.flags & KVM_PPC_PAGE_SIZES_REAL) { | |
437 | env->ci_large_pages = getpagesize() >= 0x10000; | |
438 | } | |
439 | ||
08215d8f AG |
440 | /* |
441 | * XXX This loop should be an entry wide AND of the capabilities that | |
442 | * the selected CPU has with the capabilities that KVM supports. | |
443 | */ | |
4656e1f0 BH |
444 | for (ik = iq = 0; ik < KVM_PPC_PAGE_SIZES_MAX_SZ; ik++) { |
445 | struct ppc_one_seg_page_size *qsps = &env->sps.sps[iq]; | |
446 | struct kvm_ppc_one_seg_page_size *ksps = &smmu_info.sps[ik]; | |
447 | ||
df587133 | 448 | if (!kvm_valid_page_size(smmu_info.flags, max_cpu_page_size, |
4656e1f0 BH |
449 | ksps->page_shift)) { |
450 | continue; | |
451 | } | |
452 | qsps->page_shift = ksps->page_shift; | |
453 | qsps->slb_enc = ksps->slb_enc; | |
454 | for (jk = jq = 0; jk < KVM_PPC_PAGE_SIZES_MAX_SZ; jk++) { | |
df587133 | 455 | if (!kvm_valid_page_size(smmu_info.flags, max_cpu_page_size, |
4656e1f0 BH |
456 | ksps->enc[jk].page_shift)) { |
457 | continue; | |
458 | } | |
0d594f55 TH |
459 | if (ksps->enc[jk].page_shift == 16) { |
460 | has_64k_pages = true; | |
461 | } | |
4656e1f0 BH |
462 | qsps->enc[jq].page_shift = ksps->enc[jk].page_shift; |
463 | qsps->enc[jq].pte_enc = ksps->enc[jk].pte_enc; | |
464 | if (++jq >= PPC_PAGE_SIZES_MAX_SZ) { | |
465 | break; | |
466 | } | |
467 | } | |
468 | if (++iq >= PPC_PAGE_SIZES_MAX_SZ) { | |
469 | break; | |
470 | } | |
471 | } | |
472 | env->slb_nr = smmu_info.slb_size; | |
08215d8f | 473 | if (!(smmu_info.flags & KVM_PPC_1T_SEGMENTS)) { |
4656e1f0 BH |
474 | env->mmu_model &= ~POWERPC_MMU_1TSEG; |
475 | } | |
0d594f55 TH |
476 | if (!has_64k_pages) { |
477 | env->mmu_model &= ~POWERPC_MMU_64K; | |
478 | } | |
4656e1f0 | 479 | } |
df587133 | 480 | |
ec69355b | 481 | bool kvmppc_is_mem_backend_page_size_ok(const char *obj_path) |
df587133 TH |
482 | { |
483 | Object *mem_obj = object_resolve_path(obj_path, NULL); | |
484 | char *mempath = object_property_get_str(mem_obj, "mem-path", NULL); | |
485 | long pagesize; | |
486 | ||
487 | if (mempath) { | |
9c607668 | 488 | pagesize = qemu_mempath_getpagesize(mempath); |
2d3e302e | 489 | g_free(mempath); |
df587133 TH |
490 | } else { |
491 | pagesize = getpagesize(); | |
492 | } | |
493 | ||
494 | return pagesize >= max_cpu_page_size; | |
495 | } | |
496 | ||
4656e1f0 BH |
497 | #else /* defined (TARGET_PPC64) */ |
498 | ||
a60f24b5 | 499 | static inline void kvm_fixup_page_sizes(PowerPCCPU *cpu) |
4656e1f0 BH |
500 | { |
501 | } | |
502 | ||
ec69355b | 503 | bool kvmppc_is_mem_backend_page_size_ok(const char *obj_path) |
df587133 TH |
504 | { |
505 | return true; | |
506 | } | |
507 | ||
4656e1f0 BH |
508 | #endif /* !defined (TARGET_PPC64) */ |
509 | ||
b164e48e EH |
510 | unsigned long kvm_arch_vcpu_id(CPUState *cpu) |
511 | { | |
0f20ba62 | 512 | return ppc_get_vcpu_dt_id(POWERPC_CPU(cpu)); |
b164e48e EH |
513 | } |
514 | ||
88365d17 BB |
515 | /* e500 supports 2 h/w breakpoint and 2 watchpoint. |
516 | * book3s supports only 1 watchpoint, so array size | |
517 | * of 4 is sufficient for now. | |
518 | */ | |
519 | #define MAX_HW_BKPTS 4 | |
520 | ||
521 | static struct HWBreakpoint { | |
522 | target_ulong addr; | |
523 | int type; | |
524 | } hw_debug_points[MAX_HW_BKPTS]; | |
525 | ||
526 | static CPUWatchpoint hw_watchpoint; | |
527 | ||
528 | /* Default there is no breakpoint and watchpoint supported */ | |
529 | static int max_hw_breakpoint; | |
530 | static int max_hw_watchpoint; | |
531 | static int nb_hw_breakpoint; | |
532 | static int nb_hw_watchpoint; | |
533 | ||
534 | static void kvmppc_hw_debug_points_init(CPUPPCState *cenv) | |
535 | { | |
536 | if (cenv->excp_model == POWERPC_EXCP_BOOKE) { | |
537 | max_hw_breakpoint = 2; | |
538 | max_hw_watchpoint = 2; | |
539 | } | |
540 | ||
541 | if ((max_hw_breakpoint + max_hw_watchpoint) > MAX_HW_BKPTS) { | |
542 | fprintf(stderr, "Error initializing h/w breakpoints\n"); | |
543 | return; | |
544 | } | |
545 | } | |
546 | ||
20d695a9 | 547 | int kvm_arch_init_vcpu(CPUState *cs) |
5666ca4a | 548 | { |
20d695a9 AF |
549 | PowerPCCPU *cpu = POWERPC_CPU(cs); |
550 | CPUPPCState *cenv = &cpu->env; | |
5666ca4a SW |
551 | int ret; |
552 | ||
4656e1f0 | 553 | /* Gather server mmu info from KVM and update the CPU state */ |
a60f24b5 | 554 | kvm_fixup_page_sizes(cpu); |
4656e1f0 BH |
555 | |
556 | /* Synchronize sregs with kvm */ | |
1bc22652 | 557 | ret = kvm_arch_sync_sregs(cpu); |
5666ca4a | 558 | if (ret) { |
388e47c7 TH |
559 | if (ret == -EINVAL) { |
560 | error_report("Register sync failed... If you're using kvm-hv.ko," | |
561 | " only \"-cpu host\" is possible"); | |
562 | } | |
5666ca4a SW |
563 | return ret; |
564 | } | |
861bbc80 | 565 | |
bc72ad67 | 566 | idle_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, kvm_kick_cpu, cpu); |
c821c2bd | 567 | |
93dd5e85 SW |
568 | switch (cenv->mmu_model) { |
569 | case POWERPC_MMU_BOOKE206: | |
7f516c96 | 570 | /* This target supports access to KVM's guest TLB */ |
1bc22652 | 571 | ret = kvm_booke206_tlb_init(cpu); |
93dd5e85 | 572 | break; |
7f516c96 TH |
573 | case POWERPC_MMU_2_07: |
574 | if (!cap_htm && !kvmppc_is_pr(cs->kvm_state)) { | |
575 | /* KVM-HV has transactional memory on POWER8 also without the | |
f3d9f303 SB |
576 | * KVM_CAP_PPC_HTM extension, so enable it here instead as |
577 | * long as it's availble to userspace on the host. */ | |
578 | if (qemu_getauxval(AT_HWCAP2) & PPC_FEATURE2_HAS_HTM) { | |
579 | cap_htm = true; | |
580 | } | |
7f516c96 TH |
581 | } |
582 | break; | |
93dd5e85 SW |
583 | default: |
584 | break; | |
585 | } | |
586 | ||
3c902d44 | 587 | kvm_get_one_reg(cs, KVM_REG_PPC_DEBUG_INST, &debug_inst_opcode); |
88365d17 | 588 | kvmppc_hw_debug_points_init(cenv); |
3c902d44 | 589 | |
861bbc80 | 590 | return ret; |
d76d1650 AJ |
591 | } |
592 | ||
1bc22652 | 593 | static void kvm_sw_tlb_put(PowerPCCPU *cpu) |
93dd5e85 | 594 | { |
1bc22652 AF |
595 | CPUPPCState *env = &cpu->env; |
596 | CPUState *cs = CPU(cpu); | |
93dd5e85 SW |
597 | struct kvm_dirty_tlb dirty_tlb; |
598 | unsigned char *bitmap; | |
599 | int ret; | |
600 | ||
601 | if (!env->kvm_sw_tlb) { | |
602 | return; | |
603 | } | |
604 | ||
605 | bitmap = g_malloc((env->nb_tlb + 7) / 8); | |
606 | memset(bitmap, 0xFF, (env->nb_tlb + 7) / 8); | |
607 | ||
608 | dirty_tlb.bitmap = (uintptr_t)bitmap; | |
609 | dirty_tlb.num_dirty = env->nb_tlb; | |
610 | ||
1bc22652 | 611 | ret = kvm_vcpu_ioctl(cs, KVM_DIRTY_TLB, &dirty_tlb); |
93dd5e85 SW |
612 | if (ret) { |
613 | fprintf(stderr, "%s: KVM_DIRTY_TLB: %s\n", | |
614 | __func__, strerror(-ret)); | |
615 | } | |
616 | ||
617 | g_free(bitmap); | |
618 | } | |
619 | ||
d67d40ea DG |
620 | static void kvm_get_one_spr(CPUState *cs, uint64_t id, int spr) |
621 | { | |
622 | PowerPCCPU *cpu = POWERPC_CPU(cs); | |
623 | CPUPPCState *env = &cpu->env; | |
624 | union { | |
625 | uint32_t u32; | |
626 | uint64_t u64; | |
627 | } val; | |
628 | struct kvm_one_reg reg = { | |
629 | .id = id, | |
630 | .addr = (uintptr_t) &val, | |
631 | }; | |
632 | int ret; | |
633 | ||
634 | ret = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, ®); | |
635 | if (ret != 0) { | |
b36f100e | 636 | trace_kvm_failed_spr_get(spr, strerror(errno)); |
d67d40ea DG |
637 | } else { |
638 | switch (id & KVM_REG_SIZE_MASK) { | |
639 | case KVM_REG_SIZE_U32: | |
640 | env->spr[spr] = val.u32; | |
641 | break; | |
642 | ||
643 | case KVM_REG_SIZE_U64: | |
644 | env->spr[spr] = val.u64; | |
645 | break; | |
646 | ||
647 | default: | |
648 | /* Don't handle this size yet */ | |
649 | abort(); | |
650 | } | |
651 | } | |
652 | } | |
653 | ||
654 | static void kvm_put_one_spr(CPUState *cs, uint64_t id, int spr) | |
655 | { | |
656 | PowerPCCPU *cpu = POWERPC_CPU(cs); | |
657 | CPUPPCState *env = &cpu->env; | |
658 | union { | |
659 | uint32_t u32; | |
660 | uint64_t u64; | |
661 | } val; | |
662 | struct kvm_one_reg reg = { | |
663 | .id = id, | |
664 | .addr = (uintptr_t) &val, | |
665 | }; | |
666 | int ret; | |
667 | ||
668 | switch (id & KVM_REG_SIZE_MASK) { | |
669 | case KVM_REG_SIZE_U32: | |
670 | val.u32 = env->spr[spr]; | |
671 | break; | |
672 | ||
673 | case KVM_REG_SIZE_U64: | |
674 | val.u64 = env->spr[spr]; | |
675 | break; | |
676 | ||
677 | default: | |
678 | /* Don't handle this size yet */ | |
679 | abort(); | |
680 | } | |
681 | ||
682 | ret = kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, ®); | |
683 | if (ret != 0) { | |
b36f100e | 684 | trace_kvm_failed_spr_set(spr, strerror(errno)); |
d67d40ea DG |
685 | } |
686 | } | |
687 | ||
70b79849 DG |
688 | static int kvm_put_fp(CPUState *cs) |
689 | { | |
690 | PowerPCCPU *cpu = POWERPC_CPU(cs); | |
691 | CPUPPCState *env = &cpu->env; | |
692 | struct kvm_one_reg reg; | |
693 | int i; | |
694 | int ret; | |
695 | ||
696 | if (env->insns_flags & PPC_FLOAT) { | |
697 | uint64_t fpscr = env->fpscr; | |
698 | bool vsx = !!(env->insns_flags2 & PPC2_VSX); | |
699 | ||
700 | reg.id = KVM_REG_PPC_FPSCR; | |
701 | reg.addr = (uintptr_t)&fpscr; | |
702 | ret = kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, ®); | |
703 | if (ret < 0) { | |
da56ff91 | 704 | DPRINTF("Unable to set FPSCR to KVM: %s\n", strerror(errno)); |
70b79849 DG |
705 | return ret; |
706 | } | |
707 | ||
708 | for (i = 0; i < 32; i++) { | |
709 | uint64_t vsr[2]; | |
710 | ||
3a4b791b | 711 | #ifdef HOST_WORDS_BIGENDIAN |
70b79849 DG |
712 | vsr[0] = float64_val(env->fpr[i]); |
713 | vsr[1] = env->vsr[i]; | |
3a4b791b GK |
714 | #else |
715 | vsr[0] = env->vsr[i]; | |
716 | vsr[1] = float64_val(env->fpr[i]); | |
717 | #endif | |
70b79849 DG |
718 | reg.addr = (uintptr_t) &vsr; |
719 | reg.id = vsx ? KVM_REG_PPC_VSR(i) : KVM_REG_PPC_FPR(i); | |
720 | ||
721 | ret = kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, ®); | |
722 | if (ret < 0) { | |
da56ff91 | 723 | DPRINTF("Unable to set %s%d to KVM: %s\n", vsx ? "VSR" : "FPR", |
70b79849 DG |
724 | i, strerror(errno)); |
725 | return ret; | |
726 | } | |
727 | } | |
728 | } | |
729 | ||
730 | if (env->insns_flags & PPC_ALTIVEC) { | |
731 | reg.id = KVM_REG_PPC_VSCR; | |
732 | reg.addr = (uintptr_t)&env->vscr; | |
733 | ret = kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, ®); | |
734 | if (ret < 0) { | |
da56ff91 | 735 | DPRINTF("Unable to set VSCR to KVM: %s\n", strerror(errno)); |
70b79849 DG |
736 | return ret; |
737 | } | |
738 | ||
739 | for (i = 0; i < 32; i++) { | |
740 | reg.id = KVM_REG_PPC_VR(i); | |
741 | reg.addr = (uintptr_t)&env->avr[i]; | |
742 | ret = kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, ®); | |
743 | if (ret < 0) { | |
da56ff91 | 744 | DPRINTF("Unable to set VR%d to KVM: %s\n", i, strerror(errno)); |
70b79849 DG |
745 | return ret; |
746 | } | |
747 | } | |
748 | } | |
749 | ||
750 | return 0; | |
751 | } | |
752 | ||
753 | static int kvm_get_fp(CPUState *cs) | |
754 | { | |
755 | PowerPCCPU *cpu = POWERPC_CPU(cs); | |
756 | CPUPPCState *env = &cpu->env; | |
757 | struct kvm_one_reg reg; | |
758 | int i; | |
759 | int ret; | |
760 | ||
761 | if (env->insns_flags & PPC_FLOAT) { | |
762 | uint64_t fpscr; | |
763 | bool vsx = !!(env->insns_flags2 & PPC2_VSX); | |
764 | ||
765 | reg.id = KVM_REG_PPC_FPSCR; | |
766 | reg.addr = (uintptr_t)&fpscr; | |
767 | ret = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, ®); | |
768 | if (ret < 0) { | |
da56ff91 | 769 | DPRINTF("Unable to get FPSCR from KVM: %s\n", strerror(errno)); |
70b79849 DG |
770 | return ret; |
771 | } else { | |
772 | env->fpscr = fpscr; | |
773 | } | |
774 | ||
775 | for (i = 0; i < 32; i++) { | |
776 | uint64_t vsr[2]; | |
777 | ||
778 | reg.addr = (uintptr_t) &vsr; | |
779 | reg.id = vsx ? KVM_REG_PPC_VSR(i) : KVM_REG_PPC_FPR(i); | |
780 | ||
781 | ret = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, ®); | |
782 | if (ret < 0) { | |
da56ff91 | 783 | DPRINTF("Unable to get %s%d from KVM: %s\n", |
70b79849 DG |
784 | vsx ? "VSR" : "FPR", i, strerror(errno)); |
785 | return ret; | |
786 | } else { | |
3a4b791b | 787 | #ifdef HOST_WORDS_BIGENDIAN |
70b79849 DG |
788 | env->fpr[i] = vsr[0]; |
789 | if (vsx) { | |
790 | env->vsr[i] = vsr[1]; | |
791 | } | |
3a4b791b GK |
792 | #else |
793 | env->fpr[i] = vsr[1]; | |
794 | if (vsx) { | |
795 | env->vsr[i] = vsr[0]; | |
796 | } | |
797 | #endif | |
70b79849 DG |
798 | } |
799 | } | |
800 | } | |
801 | ||
802 | if (env->insns_flags & PPC_ALTIVEC) { | |
803 | reg.id = KVM_REG_PPC_VSCR; | |
804 | reg.addr = (uintptr_t)&env->vscr; | |
805 | ret = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, ®); | |
806 | if (ret < 0) { | |
da56ff91 | 807 | DPRINTF("Unable to get VSCR from KVM: %s\n", strerror(errno)); |
70b79849 DG |
808 | return ret; |
809 | } | |
810 | ||
811 | for (i = 0; i < 32; i++) { | |
812 | reg.id = KVM_REG_PPC_VR(i); | |
813 | reg.addr = (uintptr_t)&env->avr[i]; | |
814 | ret = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, ®); | |
815 | if (ret < 0) { | |
da56ff91 | 816 | DPRINTF("Unable to get VR%d from KVM: %s\n", |
70b79849 DG |
817 | i, strerror(errno)); |
818 | return ret; | |
819 | } | |
820 | } | |
821 | } | |
822 | ||
823 | return 0; | |
824 | } | |
825 | ||
9b00ea49 DG |
826 | #if defined(TARGET_PPC64) |
827 | static int kvm_get_vpa(CPUState *cs) | |
828 | { | |
829 | PowerPCCPU *cpu = POWERPC_CPU(cs); | |
830 | CPUPPCState *env = &cpu->env; | |
831 | struct kvm_one_reg reg; | |
832 | int ret; | |
833 | ||
834 | reg.id = KVM_REG_PPC_VPA_ADDR; | |
835 | reg.addr = (uintptr_t)&env->vpa_addr; | |
836 | ret = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, ®); | |
837 | if (ret < 0) { | |
da56ff91 | 838 | DPRINTF("Unable to get VPA address from KVM: %s\n", strerror(errno)); |
9b00ea49 DG |
839 | return ret; |
840 | } | |
841 | ||
842 | assert((uintptr_t)&env->slb_shadow_size | |
843 | == ((uintptr_t)&env->slb_shadow_addr + 8)); | |
844 | reg.id = KVM_REG_PPC_VPA_SLB; | |
845 | reg.addr = (uintptr_t)&env->slb_shadow_addr; | |
846 | ret = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, ®); | |
847 | if (ret < 0) { | |
da56ff91 | 848 | DPRINTF("Unable to get SLB shadow state from KVM: %s\n", |
9b00ea49 DG |
849 | strerror(errno)); |
850 | return ret; | |
851 | } | |
852 | ||
853 | assert((uintptr_t)&env->dtl_size == ((uintptr_t)&env->dtl_addr + 8)); | |
854 | reg.id = KVM_REG_PPC_VPA_DTL; | |
855 | reg.addr = (uintptr_t)&env->dtl_addr; | |
856 | ret = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, ®); | |
857 | if (ret < 0) { | |
da56ff91 | 858 | DPRINTF("Unable to get dispatch trace log state from KVM: %s\n", |
9b00ea49 DG |
859 | strerror(errno)); |
860 | return ret; | |
861 | } | |
862 | ||
863 | return 0; | |
864 | } | |
865 | ||
866 | static int kvm_put_vpa(CPUState *cs) | |
867 | { | |
868 | PowerPCCPU *cpu = POWERPC_CPU(cs); | |
869 | CPUPPCState *env = &cpu->env; | |
870 | struct kvm_one_reg reg; | |
871 | int ret; | |
872 | ||
873 | /* SLB shadow or DTL can't be registered unless a master VPA is | |
874 | * registered. That means when restoring state, if a VPA *is* | |
875 | * registered, we need to set that up first. If not, we need to | |
876 | * deregister the others before deregistering the master VPA */ | |
877 | assert(env->vpa_addr || !(env->slb_shadow_addr || env->dtl_addr)); | |
878 | ||
879 | if (env->vpa_addr) { | |
880 | reg.id = KVM_REG_PPC_VPA_ADDR; | |
881 | reg.addr = (uintptr_t)&env->vpa_addr; | |
882 | ret = kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, ®); | |
883 | if (ret < 0) { | |
da56ff91 | 884 | DPRINTF("Unable to set VPA address to KVM: %s\n", strerror(errno)); |
9b00ea49 DG |
885 | return ret; |
886 | } | |
887 | } | |
888 | ||
889 | assert((uintptr_t)&env->slb_shadow_size | |
890 | == ((uintptr_t)&env->slb_shadow_addr + 8)); | |
891 | reg.id = KVM_REG_PPC_VPA_SLB; | |
892 | reg.addr = (uintptr_t)&env->slb_shadow_addr; | |
893 | ret = kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, ®); | |
894 | if (ret < 0) { | |
da56ff91 | 895 | DPRINTF("Unable to set SLB shadow state to KVM: %s\n", strerror(errno)); |
9b00ea49 DG |
896 | return ret; |
897 | } | |
898 | ||
899 | assert((uintptr_t)&env->dtl_size == ((uintptr_t)&env->dtl_addr + 8)); | |
900 | reg.id = KVM_REG_PPC_VPA_DTL; | |
901 | reg.addr = (uintptr_t)&env->dtl_addr; | |
902 | ret = kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, ®); | |
903 | if (ret < 0) { | |
da56ff91 | 904 | DPRINTF("Unable to set dispatch trace log state to KVM: %s\n", |
9b00ea49 DG |
905 | strerror(errno)); |
906 | return ret; | |
907 | } | |
908 | ||
909 | if (!env->vpa_addr) { | |
910 | reg.id = KVM_REG_PPC_VPA_ADDR; | |
911 | reg.addr = (uintptr_t)&env->vpa_addr; | |
912 | ret = kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, ®); | |
913 | if (ret < 0) { | |
da56ff91 | 914 | DPRINTF("Unable to set VPA address to KVM: %s\n", strerror(errno)); |
9b00ea49 DG |
915 | return ret; |
916 | } | |
917 | } | |
918 | ||
919 | return 0; | |
920 | } | |
921 | #endif /* TARGET_PPC64 */ | |
922 | ||
e5c0d3ce | 923 | int kvmppc_put_books_sregs(PowerPCCPU *cpu) |
a7a00a72 DG |
924 | { |
925 | CPUPPCState *env = &cpu->env; | |
926 | struct kvm_sregs sregs; | |
927 | int i; | |
928 | ||
929 | sregs.pvr = env->spr[SPR_PVR]; | |
930 | ||
931 | sregs.u.s.sdr1 = env->spr[SPR_SDR1]; | |
932 | ||
933 | /* Sync SLB */ | |
934 | #ifdef TARGET_PPC64 | |
935 | for (i = 0; i < ARRAY_SIZE(env->slb); i++) { | |
936 | sregs.u.s.ppc64.slb[i].slbe = env->slb[i].esid; | |
937 | if (env->slb[i].esid & SLB_ESID_V) { | |
938 | sregs.u.s.ppc64.slb[i].slbe |= i; | |
939 | } | |
940 | sregs.u.s.ppc64.slb[i].slbv = env->slb[i].vsid; | |
941 | } | |
942 | #endif | |
943 | ||
944 | /* Sync SRs */ | |
945 | for (i = 0; i < 16; i++) { | |
946 | sregs.u.s.ppc32.sr[i] = env->sr[i]; | |
947 | } | |
948 | ||
949 | /* Sync BATs */ | |
950 | for (i = 0; i < 8; i++) { | |
951 | /* Beware. We have to swap upper and lower bits here */ | |
952 | sregs.u.s.ppc32.dbat[i] = ((uint64_t)env->DBAT[0][i] << 32) | |
953 | | env->DBAT[1][i]; | |
954 | sregs.u.s.ppc32.ibat[i] = ((uint64_t)env->IBAT[0][i] << 32) | |
955 | | env->IBAT[1][i]; | |
956 | } | |
957 | ||
958 | return kvm_vcpu_ioctl(CPU(cpu), KVM_SET_SREGS, &sregs); | |
959 | } | |
960 | ||
20d695a9 | 961 | int kvm_arch_put_registers(CPUState *cs, int level) |
d76d1650 | 962 | { |
20d695a9 AF |
963 | PowerPCCPU *cpu = POWERPC_CPU(cs); |
964 | CPUPPCState *env = &cpu->env; | |
d76d1650 AJ |
965 | struct kvm_regs regs; |
966 | int ret; | |
967 | int i; | |
968 | ||
1bc22652 AF |
969 | ret = kvm_vcpu_ioctl(cs, KVM_GET_REGS, ®s); |
970 | if (ret < 0) { | |
d76d1650 | 971 | return ret; |
1bc22652 | 972 | } |
d76d1650 AJ |
973 | |
974 | regs.ctr = env->ctr; | |
975 | regs.lr = env->lr; | |
da91a00f | 976 | regs.xer = cpu_read_xer(env); |
d76d1650 AJ |
977 | regs.msr = env->msr; |
978 | regs.pc = env->nip; | |
979 | ||
980 | regs.srr0 = env->spr[SPR_SRR0]; | |
981 | regs.srr1 = env->spr[SPR_SRR1]; | |
982 | ||
983 | regs.sprg0 = env->spr[SPR_SPRG0]; | |
984 | regs.sprg1 = env->spr[SPR_SPRG1]; | |
985 | regs.sprg2 = env->spr[SPR_SPRG2]; | |
986 | regs.sprg3 = env->spr[SPR_SPRG3]; | |
987 | regs.sprg4 = env->spr[SPR_SPRG4]; | |
988 | regs.sprg5 = env->spr[SPR_SPRG5]; | |
989 | regs.sprg6 = env->spr[SPR_SPRG6]; | |
990 | regs.sprg7 = env->spr[SPR_SPRG7]; | |
991 | ||
90dc8812 SW |
992 | regs.pid = env->spr[SPR_BOOKE_PID]; |
993 | ||
d76d1650 AJ |
994 | for (i = 0;i < 32; i++) |
995 | regs.gpr[i] = env->gpr[i]; | |
996 | ||
4bddaf55 AK |
997 | regs.cr = 0; |
998 | for (i = 0; i < 8; i++) { | |
999 | regs.cr |= (env->crf[i] & 15) << (4 * (7 - i)); | |
1000 | } | |
1001 | ||
1bc22652 | 1002 | ret = kvm_vcpu_ioctl(cs, KVM_SET_REGS, ®s); |
d76d1650 AJ |
1003 | if (ret < 0) |
1004 | return ret; | |
1005 | ||
70b79849 DG |
1006 | kvm_put_fp(cs); |
1007 | ||
93dd5e85 | 1008 | if (env->tlb_dirty) { |
1bc22652 | 1009 | kvm_sw_tlb_put(cpu); |
93dd5e85 SW |
1010 | env->tlb_dirty = false; |
1011 | } | |
1012 | ||
f1af19d7 | 1013 | if (cap_segstate && (level >= KVM_PUT_RESET_STATE)) { |
a7a00a72 DG |
1014 | ret = kvmppc_put_books_sregs(cpu); |
1015 | if (ret < 0) { | |
f1af19d7 DG |
1016 | return ret; |
1017 | } | |
1018 | } | |
1019 | ||
1020 | if (cap_hior && (level >= KVM_PUT_RESET_STATE)) { | |
d67d40ea DG |
1021 | kvm_put_one_spr(cs, KVM_REG_PPC_HIOR, SPR_HIOR); |
1022 | } | |
f1af19d7 | 1023 | |
d67d40ea DG |
1024 | if (cap_one_reg) { |
1025 | int i; | |
1026 | ||
1027 | /* We deliberately ignore errors here, for kernels which have | |
1028 | * the ONE_REG calls, but don't support the specific | |
1029 | * registers, there's a reasonable chance things will still | |
1030 | * work, at least until we try to migrate. */ | |
1031 | for (i = 0; i < 1024; i++) { | |
1032 | uint64_t id = env->spr_cb[i].one_reg_id; | |
1033 | ||
1034 | if (id != 0) { | |
1035 | kvm_put_one_spr(cs, id, i); | |
1036 | } | |
f1af19d7 | 1037 | } |
9b00ea49 DG |
1038 | |
1039 | #ifdef TARGET_PPC64 | |
80b3f79b AK |
1040 | if (msr_ts) { |
1041 | for (i = 0; i < ARRAY_SIZE(env->tm_gpr); i++) { | |
1042 | kvm_set_one_reg(cs, KVM_REG_PPC_TM_GPR(i), &env->tm_gpr[i]); | |
1043 | } | |
1044 | for (i = 0; i < ARRAY_SIZE(env->tm_vsr); i++) { | |
1045 | kvm_set_one_reg(cs, KVM_REG_PPC_TM_VSR(i), &env->tm_vsr[i]); | |
1046 | } | |
1047 | kvm_set_one_reg(cs, KVM_REG_PPC_TM_CR, &env->tm_cr); | |
1048 | kvm_set_one_reg(cs, KVM_REG_PPC_TM_LR, &env->tm_lr); | |
1049 | kvm_set_one_reg(cs, KVM_REG_PPC_TM_CTR, &env->tm_ctr); | |
1050 | kvm_set_one_reg(cs, KVM_REG_PPC_TM_FPSCR, &env->tm_fpscr); | |
1051 | kvm_set_one_reg(cs, KVM_REG_PPC_TM_AMR, &env->tm_amr); | |
1052 | kvm_set_one_reg(cs, KVM_REG_PPC_TM_PPR, &env->tm_ppr); | |
1053 | kvm_set_one_reg(cs, KVM_REG_PPC_TM_VRSAVE, &env->tm_vrsave); | |
1054 | kvm_set_one_reg(cs, KVM_REG_PPC_TM_VSCR, &env->tm_vscr); | |
1055 | kvm_set_one_reg(cs, KVM_REG_PPC_TM_DSCR, &env->tm_dscr); | |
1056 | kvm_set_one_reg(cs, KVM_REG_PPC_TM_TAR, &env->tm_tar); | |
1057 | } | |
1058 | ||
9b00ea49 DG |
1059 | if (cap_papr) { |
1060 | if (kvm_put_vpa(cs) < 0) { | |
da56ff91 | 1061 | DPRINTF("Warning: Unable to set VPA information to KVM\n"); |
9b00ea49 DG |
1062 | } |
1063 | } | |
98a8b524 AK |
1064 | |
1065 | kvm_set_one_reg(cs, KVM_REG_PPC_TB_OFFSET, &env->tb_env->tb_offset); | |
9b00ea49 | 1066 | #endif /* TARGET_PPC64 */ |
f1af19d7 DG |
1067 | } |
1068 | ||
d76d1650 AJ |
1069 | return ret; |
1070 | } | |
1071 | ||
c371c2e3 BB |
1072 | static void kvm_sync_excp(CPUPPCState *env, int vector, int ivor) |
1073 | { | |
1074 | env->excp_vectors[vector] = env->spr[ivor] + env->spr[SPR_BOOKE_IVPR]; | |
1075 | } | |
1076 | ||
a7a00a72 DG |
1077 | static int kvmppc_get_booke_sregs(PowerPCCPU *cpu) |
1078 | { | |
1079 | CPUPPCState *env = &cpu->env; | |
1080 | struct kvm_sregs sregs; | |
1081 | int ret; | |
1082 | ||
1083 | ret = kvm_vcpu_ioctl(CPU(cpu), KVM_GET_SREGS, &sregs); | |
1084 | if (ret < 0) { | |
1085 | return ret; | |
1086 | } | |
1087 | ||
1088 | if (sregs.u.e.features & KVM_SREGS_E_BASE) { | |
1089 | env->spr[SPR_BOOKE_CSRR0] = sregs.u.e.csrr0; | |
1090 | env->spr[SPR_BOOKE_CSRR1] = sregs.u.e.csrr1; | |
1091 | env->spr[SPR_BOOKE_ESR] = sregs.u.e.esr; | |
1092 | env->spr[SPR_BOOKE_DEAR] = sregs.u.e.dear; | |
1093 | env->spr[SPR_BOOKE_MCSR] = sregs.u.e.mcsr; | |
1094 | env->spr[SPR_BOOKE_TSR] = sregs.u.e.tsr; | |
1095 | env->spr[SPR_BOOKE_TCR] = sregs.u.e.tcr; | |
1096 | env->spr[SPR_DECR] = sregs.u.e.dec; | |
1097 | env->spr[SPR_TBL] = sregs.u.e.tb & 0xffffffff; | |
1098 | env->spr[SPR_TBU] = sregs.u.e.tb >> 32; | |
1099 | env->spr[SPR_VRSAVE] = sregs.u.e.vrsave; | |
1100 | } | |
1101 | ||
1102 | if (sregs.u.e.features & KVM_SREGS_E_ARCH206) { | |
1103 | env->spr[SPR_BOOKE_PIR] = sregs.u.e.pir; | |
1104 | env->spr[SPR_BOOKE_MCSRR0] = sregs.u.e.mcsrr0; | |
1105 | env->spr[SPR_BOOKE_MCSRR1] = sregs.u.e.mcsrr1; | |
1106 | env->spr[SPR_BOOKE_DECAR] = sregs.u.e.decar; | |
1107 | env->spr[SPR_BOOKE_IVPR] = sregs.u.e.ivpr; | |
1108 | } | |
1109 | ||
1110 | if (sregs.u.e.features & KVM_SREGS_E_64) { | |
1111 | env->spr[SPR_BOOKE_EPCR] = sregs.u.e.epcr; | |
1112 | } | |
1113 | ||
1114 | if (sregs.u.e.features & KVM_SREGS_E_SPRG8) { | |
1115 | env->spr[SPR_BOOKE_SPRG8] = sregs.u.e.sprg8; | |
1116 | } | |
1117 | ||
1118 | if (sregs.u.e.features & KVM_SREGS_E_IVOR) { | |
1119 | env->spr[SPR_BOOKE_IVOR0] = sregs.u.e.ivor_low[0]; | |
1120 | kvm_sync_excp(env, POWERPC_EXCP_CRITICAL, SPR_BOOKE_IVOR0); | |
1121 | env->spr[SPR_BOOKE_IVOR1] = sregs.u.e.ivor_low[1]; | |
1122 | kvm_sync_excp(env, POWERPC_EXCP_MCHECK, SPR_BOOKE_IVOR1); | |
1123 | env->spr[SPR_BOOKE_IVOR2] = sregs.u.e.ivor_low[2]; | |
1124 | kvm_sync_excp(env, POWERPC_EXCP_DSI, SPR_BOOKE_IVOR2); | |
1125 | env->spr[SPR_BOOKE_IVOR3] = sregs.u.e.ivor_low[3]; | |
1126 | kvm_sync_excp(env, POWERPC_EXCP_ISI, SPR_BOOKE_IVOR3); | |
1127 | env->spr[SPR_BOOKE_IVOR4] = sregs.u.e.ivor_low[4]; | |
1128 | kvm_sync_excp(env, POWERPC_EXCP_EXTERNAL, SPR_BOOKE_IVOR4); | |
1129 | env->spr[SPR_BOOKE_IVOR5] = sregs.u.e.ivor_low[5]; | |
1130 | kvm_sync_excp(env, POWERPC_EXCP_ALIGN, SPR_BOOKE_IVOR5); | |
1131 | env->spr[SPR_BOOKE_IVOR6] = sregs.u.e.ivor_low[6]; | |
1132 | kvm_sync_excp(env, POWERPC_EXCP_PROGRAM, SPR_BOOKE_IVOR6); | |
1133 | env->spr[SPR_BOOKE_IVOR7] = sregs.u.e.ivor_low[7]; | |
1134 | kvm_sync_excp(env, POWERPC_EXCP_FPU, SPR_BOOKE_IVOR7); | |
1135 | env->spr[SPR_BOOKE_IVOR8] = sregs.u.e.ivor_low[8]; | |
1136 | kvm_sync_excp(env, POWERPC_EXCP_SYSCALL, SPR_BOOKE_IVOR8); | |
1137 | env->spr[SPR_BOOKE_IVOR9] = sregs.u.e.ivor_low[9]; | |
1138 | kvm_sync_excp(env, POWERPC_EXCP_APU, SPR_BOOKE_IVOR9); | |
1139 | env->spr[SPR_BOOKE_IVOR10] = sregs.u.e.ivor_low[10]; | |
1140 | kvm_sync_excp(env, POWERPC_EXCP_DECR, SPR_BOOKE_IVOR10); | |
1141 | env->spr[SPR_BOOKE_IVOR11] = sregs.u.e.ivor_low[11]; | |
1142 | kvm_sync_excp(env, POWERPC_EXCP_FIT, SPR_BOOKE_IVOR11); | |
1143 | env->spr[SPR_BOOKE_IVOR12] = sregs.u.e.ivor_low[12]; | |
1144 | kvm_sync_excp(env, POWERPC_EXCP_WDT, SPR_BOOKE_IVOR12); | |
1145 | env->spr[SPR_BOOKE_IVOR13] = sregs.u.e.ivor_low[13]; | |
1146 | kvm_sync_excp(env, POWERPC_EXCP_DTLB, SPR_BOOKE_IVOR13); | |
1147 | env->spr[SPR_BOOKE_IVOR14] = sregs.u.e.ivor_low[14]; | |
1148 | kvm_sync_excp(env, POWERPC_EXCP_ITLB, SPR_BOOKE_IVOR14); | |
1149 | env->spr[SPR_BOOKE_IVOR15] = sregs.u.e.ivor_low[15]; | |
1150 | kvm_sync_excp(env, POWERPC_EXCP_DEBUG, SPR_BOOKE_IVOR15); | |
1151 | ||
1152 | if (sregs.u.e.features & KVM_SREGS_E_SPE) { | |
1153 | env->spr[SPR_BOOKE_IVOR32] = sregs.u.e.ivor_high[0]; | |
1154 | kvm_sync_excp(env, POWERPC_EXCP_SPEU, SPR_BOOKE_IVOR32); | |
1155 | env->spr[SPR_BOOKE_IVOR33] = sregs.u.e.ivor_high[1]; | |
1156 | kvm_sync_excp(env, POWERPC_EXCP_EFPDI, SPR_BOOKE_IVOR33); | |
1157 | env->spr[SPR_BOOKE_IVOR34] = sregs.u.e.ivor_high[2]; | |
1158 | kvm_sync_excp(env, POWERPC_EXCP_EFPRI, SPR_BOOKE_IVOR34); | |
1159 | } | |
1160 | ||
1161 | if (sregs.u.e.features & KVM_SREGS_E_PM) { | |
1162 | env->spr[SPR_BOOKE_IVOR35] = sregs.u.e.ivor_high[3]; | |
1163 | kvm_sync_excp(env, POWERPC_EXCP_EPERFM, SPR_BOOKE_IVOR35); | |
1164 | } | |
1165 | ||
1166 | if (sregs.u.e.features & KVM_SREGS_E_PC) { | |
1167 | env->spr[SPR_BOOKE_IVOR36] = sregs.u.e.ivor_high[4]; | |
1168 | kvm_sync_excp(env, POWERPC_EXCP_DOORI, SPR_BOOKE_IVOR36); | |
1169 | env->spr[SPR_BOOKE_IVOR37] = sregs.u.e.ivor_high[5]; | |
1170 | kvm_sync_excp(env, POWERPC_EXCP_DOORCI, SPR_BOOKE_IVOR37); | |
1171 | } | |
1172 | } | |
1173 | ||
1174 | if (sregs.u.e.features & KVM_SREGS_E_ARCH206_MMU) { | |
1175 | env->spr[SPR_BOOKE_MAS0] = sregs.u.e.mas0; | |
1176 | env->spr[SPR_BOOKE_MAS1] = sregs.u.e.mas1; | |
1177 | env->spr[SPR_BOOKE_MAS2] = sregs.u.e.mas2; | |
1178 | env->spr[SPR_BOOKE_MAS3] = sregs.u.e.mas7_3 & 0xffffffff; | |
1179 | env->spr[SPR_BOOKE_MAS4] = sregs.u.e.mas4; | |
1180 | env->spr[SPR_BOOKE_MAS6] = sregs.u.e.mas6; | |
1181 | env->spr[SPR_BOOKE_MAS7] = sregs.u.e.mas7_3 >> 32; | |
1182 | env->spr[SPR_MMUCFG] = sregs.u.e.mmucfg; | |
1183 | env->spr[SPR_BOOKE_TLB0CFG] = sregs.u.e.tlbcfg[0]; | |
1184 | env->spr[SPR_BOOKE_TLB1CFG] = sregs.u.e.tlbcfg[1]; | |
1185 | } | |
1186 | ||
1187 | if (sregs.u.e.features & KVM_SREGS_EXP) { | |
1188 | env->spr[SPR_BOOKE_EPR] = sregs.u.e.epr; | |
1189 | } | |
1190 | ||
1191 | if (sregs.u.e.features & KVM_SREGS_E_PD) { | |
1192 | env->spr[SPR_BOOKE_EPLC] = sregs.u.e.eplc; | |
1193 | env->spr[SPR_BOOKE_EPSC] = sregs.u.e.epsc; | |
1194 | } | |
1195 | ||
1196 | if (sregs.u.e.impl_id == KVM_SREGS_E_IMPL_FSL) { | |
1197 | env->spr[SPR_E500_SVR] = sregs.u.e.impl.fsl.svr; | |
1198 | env->spr[SPR_Exxx_MCAR] = sregs.u.e.impl.fsl.mcar; | |
1199 | env->spr[SPR_HID0] = sregs.u.e.impl.fsl.hid0; | |
1200 | ||
1201 | if (sregs.u.e.impl.fsl.features & KVM_SREGS_E_FSL_PIDn) { | |
1202 | env->spr[SPR_BOOKE_PID1] = sregs.u.e.impl.fsl.pid1; | |
1203 | env->spr[SPR_BOOKE_PID2] = sregs.u.e.impl.fsl.pid2; | |
1204 | } | |
1205 | } | |
1206 | ||
1207 | return 0; | |
1208 | } | |
1209 | ||
1210 | static int kvmppc_get_books_sregs(PowerPCCPU *cpu) | |
1211 | { | |
1212 | CPUPPCState *env = &cpu->env; | |
1213 | struct kvm_sregs sregs; | |
1214 | int ret; | |
1215 | int i; | |
1216 | ||
1217 | ret = kvm_vcpu_ioctl(CPU(cpu), KVM_GET_SREGS, &sregs); | |
1218 | if (ret < 0) { | |
1219 | return ret; | |
1220 | } | |
1221 | ||
e57ca75c | 1222 | if (!cpu->vhyp) { |
a7a00a72 DG |
1223 | ppc_store_sdr1(env, sregs.u.s.sdr1); |
1224 | } | |
1225 | ||
1226 | /* Sync SLB */ | |
1227 | #ifdef TARGET_PPC64 | |
1228 | /* | |
1229 | * The packed SLB array we get from KVM_GET_SREGS only contains | |
1230 | * information about valid entries. So we flush our internal copy | |
1231 | * to get rid of stale ones, then put all valid SLB entries back | |
1232 | * in. | |
1233 | */ | |
1234 | memset(env->slb, 0, sizeof(env->slb)); | |
1235 | for (i = 0; i < ARRAY_SIZE(env->slb); i++) { | |
1236 | target_ulong rb = sregs.u.s.ppc64.slb[i].slbe; | |
1237 | target_ulong rs = sregs.u.s.ppc64.slb[i].slbv; | |
1238 | /* | |
1239 | * Only restore valid entries | |
1240 | */ | |
1241 | if (rb & SLB_ESID_V) { | |
1242 | ppc_store_slb(cpu, rb & 0xfff, rb & ~0xfffULL, rs); | |
1243 | } | |
1244 | } | |
1245 | #endif | |
1246 | ||
1247 | /* Sync SRs */ | |
1248 | for (i = 0; i < 16; i++) { | |
1249 | env->sr[i] = sregs.u.s.ppc32.sr[i]; | |
1250 | } | |
1251 | ||
1252 | /* Sync BATs */ | |
1253 | for (i = 0; i < 8; i++) { | |
1254 | env->DBAT[0][i] = sregs.u.s.ppc32.dbat[i] & 0xffffffff; | |
1255 | env->DBAT[1][i] = sregs.u.s.ppc32.dbat[i] >> 32; | |
1256 | env->IBAT[0][i] = sregs.u.s.ppc32.ibat[i] & 0xffffffff; | |
1257 | env->IBAT[1][i] = sregs.u.s.ppc32.ibat[i] >> 32; | |
1258 | } | |
1259 | ||
1260 | return 0; | |
1261 | } | |
1262 | ||
20d695a9 | 1263 | int kvm_arch_get_registers(CPUState *cs) |
d76d1650 | 1264 | { |
20d695a9 AF |
1265 | PowerPCCPU *cpu = POWERPC_CPU(cs); |
1266 | CPUPPCState *env = &cpu->env; | |
d76d1650 | 1267 | struct kvm_regs regs; |
90dc8812 | 1268 | uint32_t cr; |
138b38b6 | 1269 | int i, ret; |
d76d1650 | 1270 | |
1bc22652 | 1271 | ret = kvm_vcpu_ioctl(cs, KVM_GET_REGS, ®s); |
d76d1650 AJ |
1272 | if (ret < 0) |
1273 | return ret; | |
1274 | ||
90dc8812 SW |
1275 | cr = regs.cr; |
1276 | for (i = 7; i >= 0; i--) { | |
1277 | env->crf[i] = cr & 15; | |
1278 | cr >>= 4; | |
1279 | } | |
ba5e5090 | 1280 | |
d76d1650 AJ |
1281 | env->ctr = regs.ctr; |
1282 | env->lr = regs.lr; | |
da91a00f | 1283 | cpu_write_xer(env, regs.xer); |
d76d1650 AJ |
1284 | env->msr = regs.msr; |
1285 | env->nip = regs.pc; | |
1286 | ||
1287 | env->spr[SPR_SRR0] = regs.srr0; | |
1288 | env->spr[SPR_SRR1] = regs.srr1; | |
1289 | ||
1290 | env->spr[SPR_SPRG0] = regs.sprg0; | |
1291 | env->spr[SPR_SPRG1] = regs.sprg1; | |
1292 | env->spr[SPR_SPRG2] = regs.sprg2; | |
1293 | env->spr[SPR_SPRG3] = regs.sprg3; | |
1294 | env->spr[SPR_SPRG4] = regs.sprg4; | |
1295 | env->spr[SPR_SPRG5] = regs.sprg5; | |
1296 | env->spr[SPR_SPRG6] = regs.sprg6; | |
1297 | env->spr[SPR_SPRG7] = regs.sprg7; | |
1298 | ||
90dc8812 SW |
1299 | env->spr[SPR_BOOKE_PID] = regs.pid; |
1300 | ||
d76d1650 AJ |
1301 | for (i = 0;i < 32; i++) |
1302 | env->gpr[i] = regs.gpr[i]; | |
1303 | ||
70b79849 DG |
1304 | kvm_get_fp(cs); |
1305 | ||
90dc8812 | 1306 | if (cap_booke_sregs) { |
a7a00a72 | 1307 | ret = kvmppc_get_booke_sregs(cpu); |
90dc8812 SW |
1308 | if (ret < 0) { |
1309 | return ret; | |
1310 | } | |
fafc0b6a | 1311 | } |
90dc8812 | 1312 | |
90dc8812 | 1313 | if (cap_segstate) { |
a7a00a72 | 1314 | ret = kvmppc_get_books_sregs(cpu); |
90dc8812 SW |
1315 | if (ret < 0) { |
1316 | return ret; | |
1317 | } | |
fafc0b6a | 1318 | } |
ba5e5090 | 1319 | |
d67d40ea DG |
1320 | if (cap_hior) { |
1321 | kvm_get_one_spr(cs, KVM_REG_PPC_HIOR, SPR_HIOR); | |
1322 | } | |
1323 | ||
1324 | if (cap_one_reg) { | |
1325 | int i; | |
1326 | ||
1327 | /* We deliberately ignore errors here, for kernels which have | |
1328 | * the ONE_REG calls, but don't support the specific | |
1329 | * registers, there's a reasonable chance things will still | |
1330 | * work, at least until we try to migrate. */ | |
1331 | for (i = 0; i < 1024; i++) { | |
1332 | uint64_t id = env->spr_cb[i].one_reg_id; | |
1333 | ||
1334 | if (id != 0) { | |
1335 | kvm_get_one_spr(cs, id, i); | |
1336 | } | |
1337 | } | |
9b00ea49 DG |
1338 | |
1339 | #ifdef TARGET_PPC64 | |
80b3f79b AK |
1340 | if (msr_ts) { |
1341 | for (i = 0; i < ARRAY_SIZE(env->tm_gpr); i++) { | |
1342 | kvm_get_one_reg(cs, KVM_REG_PPC_TM_GPR(i), &env->tm_gpr[i]); | |
1343 | } | |
1344 | for (i = 0; i < ARRAY_SIZE(env->tm_vsr); i++) { | |
1345 | kvm_get_one_reg(cs, KVM_REG_PPC_TM_VSR(i), &env->tm_vsr[i]); | |
1346 | } | |
1347 | kvm_get_one_reg(cs, KVM_REG_PPC_TM_CR, &env->tm_cr); | |
1348 | kvm_get_one_reg(cs, KVM_REG_PPC_TM_LR, &env->tm_lr); | |
1349 | kvm_get_one_reg(cs, KVM_REG_PPC_TM_CTR, &env->tm_ctr); | |
1350 | kvm_get_one_reg(cs, KVM_REG_PPC_TM_FPSCR, &env->tm_fpscr); | |
1351 | kvm_get_one_reg(cs, KVM_REG_PPC_TM_AMR, &env->tm_amr); | |
1352 | kvm_get_one_reg(cs, KVM_REG_PPC_TM_PPR, &env->tm_ppr); | |
1353 | kvm_get_one_reg(cs, KVM_REG_PPC_TM_VRSAVE, &env->tm_vrsave); | |
1354 | kvm_get_one_reg(cs, KVM_REG_PPC_TM_VSCR, &env->tm_vscr); | |
1355 | kvm_get_one_reg(cs, KVM_REG_PPC_TM_DSCR, &env->tm_dscr); | |
1356 | kvm_get_one_reg(cs, KVM_REG_PPC_TM_TAR, &env->tm_tar); | |
1357 | } | |
1358 | ||
9b00ea49 DG |
1359 | if (cap_papr) { |
1360 | if (kvm_get_vpa(cs) < 0) { | |
da56ff91 | 1361 | DPRINTF("Warning: Unable to get VPA information from KVM\n"); |
9b00ea49 DG |
1362 | } |
1363 | } | |
98a8b524 AK |
1364 | |
1365 | kvm_get_one_reg(cs, KVM_REG_PPC_TB_OFFSET, &env->tb_env->tb_offset); | |
9b00ea49 | 1366 | #endif |
d67d40ea DG |
1367 | } |
1368 | ||
d76d1650 AJ |
1369 | return 0; |
1370 | } | |
1371 | ||
1bc22652 | 1372 | int kvmppc_set_interrupt(PowerPCCPU *cpu, int irq, int level) |
fc87e185 AG |
1373 | { |
1374 | unsigned virq = level ? KVM_INTERRUPT_SET_LEVEL : KVM_INTERRUPT_UNSET; | |
1375 | ||
1376 | if (irq != PPC_INTERRUPT_EXT) { | |
1377 | return 0; | |
1378 | } | |
1379 | ||
1380 | if (!kvm_enabled() || !cap_interrupt_unset || !cap_interrupt_level) { | |
1381 | return 0; | |
1382 | } | |
1383 | ||
1bc22652 | 1384 | kvm_vcpu_ioctl(CPU(cpu), KVM_INTERRUPT, &virq); |
fc87e185 AG |
1385 | |
1386 | return 0; | |
1387 | } | |
1388 | ||
16415335 AG |
1389 | #if defined(TARGET_PPCEMB) |
1390 | #define PPC_INPUT_INT PPC40x_INPUT_INT | |
1391 | #elif defined(TARGET_PPC64) | |
1392 | #define PPC_INPUT_INT PPC970_INPUT_INT | |
1393 | #else | |
1394 | #define PPC_INPUT_INT PPC6xx_INPUT_INT | |
1395 | #endif | |
1396 | ||
20d695a9 | 1397 | void kvm_arch_pre_run(CPUState *cs, struct kvm_run *run) |
d76d1650 | 1398 | { |
20d695a9 AF |
1399 | PowerPCCPU *cpu = POWERPC_CPU(cs); |
1400 | CPUPPCState *env = &cpu->env; | |
d76d1650 AJ |
1401 | int r; |
1402 | unsigned irq; | |
1403 | ||
4b8523ee JK |
1404 | qemu_mutex_lock_iothread(); |
1405 | ||
5cbdb3a3 | 1406 | /* PowerPC QEMU tracks the various core input pins (interrupt, critical |
d76d1650 | 1407 | * interrupt, reset, etc) in PPC-specific env->irq_input_state. */ |
fc87e185 AG |
1408 | if (!cap_interrupt_level && |
1409 | run->ready_for_interrupt_injection && | |
259186a7 | 1410 | (cs->interrupt_request & CPU_INTERRUPT_HARD) && |
16415335 | 1411 | (env->irq_input_state & (1<<PPC_INPUT_INT))) |
d76d1650 AJ |
1412 | { |
1413 | /* For now KVM disregards the 'irq' argument. However, in the | |
1414 | * future KVM could cache it in-kernel to avoid a heavyweight exit | |
1415 | * when reading the UIC. | |
1416 | */ | |
fc87e185 | 1417 | irq = KVM_INTERRUPT_SET; |
d76d1650 | 1418 | |
da56ff91 | 1419 | DPRINTF("injected interrupt %d\n", irq); |
1bc22652 | 1420 | r = kvm_vcpu_ioctl(cs, KVM_INTERRUPT, &irq); |
55e5c285 AF |
1421 | if (r < 0) { |
1422 | printf("cpu %d fail inject %x\n", cs->cpu_index, irq); | |
1423 | } | |
c821c2bd AG |
1424 | |
1425 | /* Always wake up soon in case the interrupt was level based */ | |
bc72ad67 | 1426 | timer_mod(idle_timer, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + |
73bcb24d | 1427 | (NANOSECONDS_PER_SECOND / 50)); |
d76d1650 AJ |
1428 | } |
1429 | ||
1430 | /* We don't know if there are more interrupts pending after this. However, | |
1431 | * the guest will return to userspace in the course of handling this one | |
1432 | * anyways, so we will get a chance to deliver the rest. */ | |
4b8523ee JK |
1433 | |
1434 | qemu_mutex_unlock_iothread(); | |
d76d1650 AJ |
1435 | } |
1436 | ||
4c663752 | 1437 | MemTxAttrs kvm_arch_post_run(CPUState *cs, struct kvm_run *run) |
d76d1650 | 1438 | { |
4c663752 | 1439 | return MEMTXATTRS_UNSPECIFIED; |
d76d1650 AJ |
1440 | } |
1441 | ||
20d695a9 | 1442 | int kvm_arch_process_async_events(CPUState *cs) |
0af691d7 | 1443 | { |
259186a7 | 1444 | return cs->halted; |
0af691d7 MT |
1445 | } |
1446 | ||
259186a7 | 1447 | static int kvmppc_handle_halt(PowerPCCPU *cpu) |
d76d1650 | 1448 | { |
259186a7 AF |
1449 | CPUState *cs = CPU(cpu); |
1450 | CPUPPCState *env = &cpu->env; | |
1451 | ||
1452 | if (!(cs->interrupt_request & CPU_INTERRUPT_HARD) && (msr_ee)) { | |
1453 | cs->halted = 1; | |
27103424 | 1454 | cs->exception_index = EXCP_HLT; |
d76d1650 AJ |
1455 | } |
1456 | ||
bb4ea393 | 1457 | return 0; |
d76d1650 AJ |
1458 | } |
1459 | ||
1460 | /* map dcr access to existing qemu dcr emulation */ | |
1328c2bf | 1461 | static int kvmppc_handle_dcr_read(CPUPPCState *env, uint32_t dcrn, uint32_t *data) |
d76d1650 AJ |
1462 | { |
1463 | if (ppc_dcr_read(env->dcr_env, dcrn, data) < 0) | |
1464 | fprintf(stderr, "Read to unhandled DCR (0x%x)\n", dcrn); | |
1465 | ||
bb4ea393 | 1466 | return 0; |
d76d1650 AJ |
1467 | } |
1468 | ||
1328c2bf | 1469 | static int kvmppc_handle_dcr_write(CPUPPCState *env, uint32_t dcrn, uint32_t data) |
d76d1650 AJ |
1470 | { |
1471 | if (ppc_dcr_write(env->dcr_env, dcrn, data) < 0) | |
1472 | fprintf(stderr, "Write to unhandled DCR (0x%x)\n", dcrn); | |
1473 | ||
bb4ea393 | 1474 | return 0; |
d76d1650 AJ |
1475 | } |
1476 | ||
8a0548f9 BB |
1477 | int kvm_arch_insert_sw_breakpoint(CPUState *cs, struct kvm_sw_breakpoint *bp) |
1478 | { | |
1479 | /* Mixed endian case is not handled */ | |
1480 | uint32_t sc = debug_inst_opcode; | |
1481 | ||
1482 | if (cpu_memory_rw_debug(cs, bp->pc, (uint8_t *)&bp->saved_insn, | |
1483 | sizeof(sc), 0) || | |
1484 | cpu_memory_rw_debug(cs, bp->pc, (uint8_t *)&sc, sizeof(sc), 1)) { | |
1485 | return -EINVAL; | |
1486 | } | |
1487 | ||
1488 | return 0; | |
1489 | } | |
1490 | ||
1491 | int kvm_arch_remove_sw_breakpoint(CPUState *cs, struct kvm_sw_breakpoint *bp) | |
1492 | { | |
1493 | uint32_t sc; | |
1494 | ||
1495 | if (cpu_memory_rw_debug(cs, bp->pc, (uint8_t *)&sc, sizeof(sc), 0) || | |
1496 | sc != debug_inst_opcode || | |
1497 | cpu_memory_rw_debug(cs, bp->pc, (uint8_t *)&bp->saved_insn, | |
1498 | sizeof(sc), 1)) { | |
1499 | return -EINVAL; | |
1500 | } | |
1501 | ||
1502 | return 0; | |
1503 | } | |
1504 | ||
88365d17 BB |
1505 | static int find_hw_breakpoint(target_ulong addr, int type) |
1506 | { | |
1507 | int n; | |
1508 | ||
1509 | assert((nb_hw_breakpoint + nb_hw_watchpoint) | |
1510 | <= ARRAY_SIZE(hw_debug_points)); | |
1511 | ||
1512 | for (n = 0; n < nb_hw_breakpoint + nb_hw_watchpoint; n++) { | |
1513 | if (hw_debug_points[n].addr == addr && | |
1514 | hw_debug_points[n].type == type) { | |
1515 | return n; | |
1516 | } | |
1517 | } | |
1518 | ||
1519 | return -1; | |
1520 | } | |
1521 | ||
1522 | static int find_hw_watchpoint(target_ulong addr, int *flag) | |
1523 | { | |
1524 | int n; | |
1525 | ||
1526 | n = find_hw_breakpoint(addr, GDB_WATCHPOINT_ACCESS); | |
1527 | if (n >= 0) { | |
1528 | *flag = BP_MEM_ACCESS; | |
1529 | return n; | |
1530 | } | |
1531 | ||
1532 | n = find_hw_breakpoint(addr, GDB_WATCHPOINT_WRITE); | |
1533 | if (n >= 0) { | |
1534 | *flag = BP_MEM_WRITE; | |
1535 | return n; | |
1536 | } | |
1537 | ||
1538 | n = find_hw_breakpoint(addr, GDB_WATCHPOINT_READ); | |
1539 | if (n >= 0) { | |
1540 | *flag = BP_MEM_READ; | |
1541 | return n; | |
1542 | } | |
1543 | ||
1544 | return -1; | |
1545 | } | |
1546 | ||
1547 | int kvm_arch_insert_hw_breakpoint(target_ulong addr, | |
1548 | target_ulong len, int type) | |
1549 | { | |
1550 | if ((nb_hw_breakpoint + nb_hw_watchpoint) >= ARRAY_SIZE(hw_debug_points)) { | |
1551 | return -ENOBUFS; | |
1552 | } | |
1553 | ||
1554 | hw_debug_points[nb_hw_breakpoint + nb_hw_watchpoint].addr = addr; | |
1555 | hw_debug_points[nb_hw_breakpoint + nb_hw_watchpoint].type = type; | |
1556 | ||
1557 | switch (type) { | |
1558 | case GDB_BREAKPOINT_HW: | |
1559 | if (nb_hw_breakpoint >= max_hw_breakpoint) { | |
1560 | return -ENOBUFS; | |
1561 | } | |
1562 | ||
1563 | if (find_hw_breakpoint(addr, type) >= 0) { | |
1564 | return -EEXIST; | |
1565 | } | |
1566 | ||
1567 | nb_hw_breakpoint++; | |
1568 | break; | |
1569 | ||
1570 | case GDB_WATCHPOINT_WRITE: | |
1571 | case GDB_WATCHPOINT_READ: | |
1572 | case GDB_WATCHPOINT_ACCESS: | |
1573 | if (nb_hw_watchpoint >= max_hw_watchpoint) { | |
1574 | return -ENOBUFS; | |
1575 | } | |
1576 | ||
1577 | if (find_hw_breakpoint(addr, type) >= 0) { | |
1578 | return -EEXIST; | |
1579 | } | |
1580 | ||
1581 | nb_hw_watchpoint++; | |
1582 | break; | |
1583 | ||
1584 | default: | |
1585 | return -ENOSYS; | |
1586 | } | |
1587 | ||
1588 | return 0; | |
1589 | } | |
1590 | ||
1591 | int kvm_arch_remove_hw_breakpoint(target_ulong addr, | |
1592 | target_ulong len, int type) | |
1593 | { | |
1594 | int n; | |
1595 | ||
1596 | n = find_hw_breakpoint(addr, type); | |
1597 | if (n < 0) { | |
1598 | return -ENOENT; | |
1599 | } | |
1600 | ||
1601 | switch (type) { | |
1602 | case GDB_BREAKPOINT_HW: | |
1603 | nb_hw_breakpoint--; | |
1604 | break; | |
1605 | ||
1606 | case GDB_WATCHPOINT_WRITE: | |
1607 | case GDB_WATCHPOINT_READ: | |
1608 | case GDB_WATCHPOINT_ACCESS: | |
1609 | nb_hw_watchpoint--; | |
1610 | break; | |
1611 | ||
1612 | default: | |
1613 | return -ENOSYS; | |
1614 | } | |
1615 | hw_debug_points[n] = hw_debug_points[nb_hw_breakpoint + nb_hw_watchpoint]; | |
1616 | ||
1617 | return 0; | |
1618 | } | |
1619 | ||
1620 | void kvm_arch_remove_all_hw_breakpoints(void) | |
1621 | { | |
1622 | nb_hw_breakpoint = nb_hw_watchpoint = 0; | |
1623 | } | |
1624 | ||
8a0548f9 BB |
1625 | void kvm_arch_update_guest_debug(CPUState *cs, struct kvm_guest_debug *dbg) |
1626 | { | |
88365d17 BB |
1627 | int n; |
1628 | ||
8a0548f9 BB |
1629 | /* Software Breakpoint updates */ |
1630 | if (kvm_sw_breakpoints_active(cs)) { | |
1631 | dbg->control |= KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP; | |
1632 | } | |
88365d17 BB |
1633 | |
1634 | assert((nb_hw_breakpoint + nb_hw_watchpoint) | |
1635 | <= ARRAY_SIZE(hw_debug_points)); | |
1636 | assert((nb_hw_breakpoint + nb_hw_watchpoint) <= ARRAY_SIZE(dbg->arch.bp)); | |
1637 | ||
1638 | if (nb_hw_breakpoint + nb_hw_watchpoint > 0) { | |
1639 | dbg->control |= KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_HW_BP; | |
1640 | memset(dbg->arch.bp, 0, sizeof(dbg->arch.bp)); | |
1641 | for (n = 0; n < nb_hw_breakpoint + nb_hw_watchpoint; n++) { | |
1642 | switch (hw_debug_points[n].type) { | |
1643 | case GDB_BREAKPOINT_HW: | |
1644 | dbg->arch.bp[n].type = KVMPPC_DEBUG_BREAKPOINT; | |
1645 | break; | |
1646 | case GDB_WATCHPOINT_WRITE: | |
1647 | dbg->arch.bp[n].type = KVMPPC_DEBUG_WATCH_WRITE; | |
1648 | break; | |
1649 | case GDB_WATCHPOINT_READ: | |
1650 | dbg->arch.bp[n].type = KVMPPC_DEBUG_WATCH_READ; | |
1651 | break; | |
1652 | case GDB_WATCHPOINT_ACCESS: | |
1653 | dbg->arch.bp[n].type = KVMPPC_DEBUG_WATCH_WRITE | | |
1654 | KVMPPC_DEBUG_WATCH_READ; | |
1655 | break; | |
1656 | default: | |
1657 | cpu_abort(cs, "Unsupported breakpoint type\n"); | |
1658 | } | |
1659 | dbg->arch.bp[n].addr = hw_debug_points[n].addr; | |
1660 | } | |
1661 | } | |
8a0548f9 BB |
1662 | } |
1663 | ||
1664 | static int kvm_handle_debug(PowerPCCPU *cpu, struct kvm_run *run) | |
1665 | { | |
1666 | CPUState *cs = CPU(cpu); | |
1667 | CPUPPCState *env = &cpu->env; | |
1668 | struct kvm_debug_exit_arch *arch_info = &run->debug.arch; | |
1669 | int handle = 0; | |
88365d17 BB |
1670 | int n; |
1671 | int flag = 0; | |
8a0548f9 | 1672 | |
88365d17 BB |
1673 | if (cs->singlestep_enabled) { |
1674 | handle = 1; | |
1675 | } else if (arch_info->status) { | |
1676 | if (nb_hw_breakpoint + nb_hw_watchpoint > 0) { | |
1677 | if (arch_info->status & KVMPPC_DEBUG_BREAKPOINT) { | |
1678 | n = find_hw_breakpoint(arch_info->address, GDB_BREAKPOINT_HW); | |
1679 | if (n >= 0) { | |
1680 | handle = 1; | |
1681 | } | |
1682 | } else if (arch_info->status & (KVMPPC_DEBUG_WATCH_READ | | |
1683 | KVMPPC_DEBUG_WATCH_WRITE)) { | |
1684 | n = find_hw_watchpoint(arch_info->address, &flag); | |
1685 | if (n >= 0) { | |
1686 | handle = 1; | |
1687 | cs->watchpoint_hit = &hw_watchpoint; | |
1688 | hw_watchpoint.vaddr = hw_debug_points[n].addr; | |
1689 | hw_watchpoint.flags = flag; | |
1690 | } | |
1691 | } | |
1692 | } | |
1693 | } else if (kvm_find_sw_breakpoint(cs, arch_info->address)) { | |
8a0548f9 BB |
1694 | handle = 1; |
1695 | } else { | |
1696 | /* QEMU is not able to handle debug exception, so inject | |
1697 | * program exception to guest; | |
1698 | * Yes program exception NOT debug exception !! | |
88365d17 BB |
1699 | * When QEMU is using debug resources then debug exception must |
1700 | * be always set. To achieve this we set MSR_DE and also set | |
1701 | * MSRP_DEP so guest cannot change MSR_DE. | |
1702 | * When emulating debug resource for guest we want guest | |
1703 | * to control MSR_DE (enable/disable debug interrupt on need). | |
1704 | * Supporting both configurations are NOT possible. | |
1705 | * So the result is that we cannot share debug resources | |
1706 | * between QEMU and Guest on BOOKE architecture. | |
1707 | * In the current design QEMU gets the priority over guest, | |
1708 | * this means that if QEMU is using debug resources then guest | |
1709 | * cannot use them; | |
8a0548f9 BB |
1710 | * For software breakpoint QEMU uses a privileged instruction; |
1711 | * So there cannot be any reason that we are here for guest | |
1712 | * set debug exception, only possibility is guest executed a | |
1713 | * privileged / illegal instruction and that's why we are | |
1714 | * injecting a program interrupt. | |
1715 | */ | |
1716 | ||
1717 | cpu_synchronize_state(cs); | |
1718 | /* env->nip is PC, so increment this by 4 to use | |
1719 | * ppc_cpu_do_interrupt(), which set srr0 = env->nip - 4. | |
1720 | */ | |
1721 | env->nip += 4; | |
1722 | cs->exception_index = POWERPC_EXCP_PROGRAM; | |
1723 | env->error_code = POWERPC_EXCP_INVAL; | |
1724 | ppc_cpu_do_interrupt(cs); | |
1725 | } | |
1726 | ||
1727 | return handle; | |
1728 | } | |
1729 | ||
20d695a9 | 1730 | int kvm_arch_handle_exit(CPUState *cs, struct kvm_run *run) |
d76d1650 | 1731 | { |
20d695a9 AF |
1732 | PowerPCCPU *cpu = POWERPC_CPU(cs); |
1733 | CPUPPCState *env = &cpu->env; | |
bb4ea393 | 1734 | int ret; |
d76d1650 | 1735 | |
4b8523ee JK |
1736 | qemu_mutex_lock_iothread(); |
1737 | ||
d76d1650 AJ |
1738 | switch (run->exit_reason) { |
1739 | case KVM_EXIT_DCR: | |
1740 | if (run->dcr.is_write) { | |
da56ff91 | 1741 | DPRINTF("handle dcr write\n"); |
d76d1650 AJ |
1742 | ret = kvmppc_handle_dcr_write(env, run->dcr.dcrn, run->dcr.data); |
1743 | } else { | |
da56ff91 | 1744 | DPRINTF("handle dcr read\n"); |
d76d1650 AJ |
1745 | ret = kvmppc_handle_dcr_read(env, run->dcr.dcrn, &run->dcr.data); |
1746 | } | |
1747 | break; | |
1748 | case KVM_EXIT_HLT: | |
da56ff91 | 1749 | DPRINTF("handle halt\n"); |
259186a7 | 1750 | ret = kvmppc_handle_halt(cpu); |
d76d1650 | 1751 | break; |
c6304a4a | 1752 | #if defined(TARGET_PPC64) |
f61b4bed | 1753 | case KVM_EXIT_PAPR_HCALL: |
da56ff91 | 1754 | DPRINTF("handle PAPR hypercall\n"); |
20d695a9 | 1755 | run->papr_hcall.ret = spapr_hypercall(cpu, |
aa100fa4 | 1756 | run->papr_hcall.nr, |
f61b4bed | 1757 | run->papr_hcall.args); |
78e8fde2 | 1758 | ret = 0; |
f61b4bed AG |
1759 | break; |
1760 | #endif | |
5b95b8b9 | 1761 | case KVM_EXIT_EPR: |
da56ff91 | 1762 | DPRINTF("handle epr\n"); |
933b19ea | 1763 | run->epr.epr = ldl_phys(cs->as, env->mpic_iack); |
5b95b8b9 AG |
1764 | ret = 0; |
1765 | break; | |
31f2cb8f | 1766 | case KVM_EXIT_WATCHDOG: |
da56ff91 | 1767 | DPRINTF("handle watchdog expiry\n"); |
31f2cb8f BB |
1768 | watchdog_perform_action(); |
1769 | ret = 0; | |
1770 | break; | |
1771 | ||
8a0548f9 BB |
1772 | case KVM_EXIT_DEBUG: |
1773 | DPRINTF("handle debug exception\n"); | |
1774 | if (kvm_handle_debug(cpu, run)) { | |
1775 | ret = EXCP_DEBUG; | |
1776 | break; | |
1777 | } | |
1778 | /* re-enter, this exception was guest-internal */ | |
1779 | ret = 0; | |
1780 | break; | |
1781 | ||
73aaec4a JK |
1782 | default: |
1783 | fprintf(stderr, "KVM: unknown exit reason %d\n", run->exit_reason); | |
1784 | ret = -1; | |
1785 | break; | |
d76d1650 AJ |
1786 | } |
1787 | ||
4b8523ee | 1788 | qemu_mutex_unlock_iothread(); |
d76d1650 AJ |
1789 | return ret; |
1790 | } | |
1791 | ||
31f2cb8f BB |
1792 | int kvmppc_or_tsr_bits(PowerPCCPU *cpu, uint32_t tsr_bits) |
1793 | { | |
1794 | CPUState *cs = CPU(cpu); | |
1795 | uint32_t bits = tsr_bits; | |
1796 | struct kvm_one_reg reg = { | |
1797 | .id = KVM_REG_PPC_OR_TSR, | |
1798 | .addr = (uintptr_t) &bits, | |
1799 | }; | |
1800 | ||
1801 | return kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, ®); | |
1802 | } | |
1803 | ||
1804 | int kvmppc_clear_tsr_bits(PowerPCCPU *cpu, uint32_t tsr_bits) | |
1805 | { | |
1806 | ||
1807 | CPUState *cs = CPU(cpu); | |
1808 | uint32_t bits = tsr_bits; | |
1809 | struct kvm_one_reg reg = { | |
1810 | .id = KVM_REG_PPC_CLEAR_TSR, | |
1811 | .addr = (uintptr_t) &bits, | |
1812 | }; | |
1813 | ||
1814 | return kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, ®); | |
1815 | } | |
1816 | ||
1817 | int kvmppc_set_tcr(PowerPCCPU *cpu) | |
1818 | { | |
1819 | CPUState *cs = CPU(cpu); | |
1820 | CPUPPCState *env = &cpu->env; | |
1821 | uint32_t tcr = env->spr[SPR_BOOKE_TCR]; | |
1822 | ||
1823 | struct kvm_one_reg reg = { | |
1824 | .id = KVM_REG_PPC_TCR, | |
1825 | .addr = (uintptr_t) &tcr, | |
1826 | }; | |
1827 | ||
1828 | return kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, ®); | |
1829 | } | |
1830 | ||
1831 | int kvmppc_booke_watchdog_enable(PowerPCCPU *cpu) | |
1832 | { | |
1833 | CPUState *cs = CPU(cpu); | |
31f2cb8f BB |
1834 | int ret; |
1835 | ||
1836 | if (!kvm_enabled()) { | |
1837 | return -1; | |
1838 | } | |
1839 | ||
1840 | if (!cap_ppc_watchdog) { | |
1841 | printf("warning: KVM does not support watchdog"); | |
1842 | return -1; | |
1843 | } | |
1844 | ||
48add816 | 1845 | ret = kvm_vcpu_enable_cap(cs, KVM_CAP_PPC_BOOKE_WATCHDOG, 0); |
31f2cb8f BB |
1846 | if (ret < 0) { |
1847 | fprintf(stderr, "%s: couldn't enable KVM_CAP_PPC_BOOKE_WATCHDOG: %s\n", | |
1848 | __func__, strerror(-ret)); | |
1849 | return ret; | |
1850 | } | |
1851 | ||
1852 | return ret; | |
1853 | } | |
1854 | ||
dc333cd6 AG |
1855 | static int read_cpuinfo(const char *field, char *value, int len) |
1856 | { | |
1857 | FILE *f; | |
1858 | int ret = -1; | |
1859 | int field_len = strlen(field); | |
1860 | char line[512]; | |
1861 | ||
1862 | f = fopen("/proc/cpuinfo", "r"); | |
1863 | if (!f) { | |
1864 | return -1; | |
1865 | } | |
1866 | ||
1867 | do { | |
ef951443 | 1868 | if (!fgets(line, sizeof(line), f)) { |
dc333cd6 AG |
1869 | break; |
1870 | } | |
1871 | if (!strncmp(line, field, field_len)) { | |
ae215068 | 1872 | pstrcpy(value, len, line); |
dc333cd6 AG |
1873 | ret = 0; |
1874 | break; | |
1875 | } | |
1876 | } while(*line); | |
1877 | ||
1878 | fclose(f); | |
1879 | ||
1880 | return ret; | |
1881 | } | |
1882 | ||
1883 | uint32_t kvmppc_get_tbfreq(void) | |
1884 | { | |
1885 | char line[512]; | |
1886 | char *ns; | |
73bcb24d | 1887 | uint32_t retval = NANOSECONDS_PER_SECOND; |
dc333cd6 AG |
1888 | |
1889 | if (read_cpuinfo("timebase", line, sizeof(line))) { | |
1890 | return retval; | |
1891 | } | |
1892 | ||
1893 | if (!(ns = strchr(line, ':'))) { | |
1894 | return retval; | |
1895 | } | |
1896 | ||
1897 | ns++; | |
1898 | ||
f9b8e7f6 | 1899 | return atoi(ns); |
dc333cd6 | 1900 | } |
4513d923 | 1901 | |
ef951443 ND |
1902 | bool kvmppc_get_host_serial(char **value) |
1903 | { | |
1904 | return g_file_get_contents("/proc/device-tree/system-id", value, NULL, | |
1905 | NULL); | |
1906 | } | |
1907 | ||
1908 | bool kvmppc_get_host_model(char **value) | |
1909 | { | |
1910 | return g_file_get_contents("/proc/device-tree/model", value, NULL, NULL); | |
1911 | } | |
1912 | ||
eadaada1 AG |
1913 | /* Try to find a device tree node for a CPU with clock-frequency property */ |
1914 | static int kvmppc_find_cpu_dt(char *buf, int buf_len) | |
1915 | { | |
1916 | struct dirent *dirp; | |
1917 | DIR *dp; | |
1918 | ||
1919 | if ((dp = opendir(PROC_DEVTREE_CPU)) == NULL) { | |
1920 | printf("Can't open directory " PROC_DEVTREE_CPU "\n"); | |
1921 | return -1; | |
1922 | } | |
1923 | ||
1924 | buf[0] = '\0'; | |
1925 | while ((dirp = readdir(dp)) != NULL) { | |
1926 | FILE *f; | |
1927 | snprintf(buf, buf_len, "%s%s/clock-frequency", PROC_DEVTREE_CPU, | |
1928 | dirp->d_name); | |
1929 | f = fopen(buf, "r"); | |
1930 | if (f) { | |
1931 | snprintf(buf, buf_len, "%s%s", PROC_DEVTREE_CPU, dirp->d_name); | |
1932 | fclose(f); | |
1933 | break; | |
1934 | } | |
1935 | buf[0] = '\0'; | |
1936 | } | |
1937 | closedir(dp); | |
1938 | if (buf[0] == '\0') { | |
1939 | printf("Unknown host!\n"); | |
1940 | return -1; | |
1941 | } | |
1942 | ||
1943 | return 0; | |
1944 | } | |
1945 | ||
7d94a30b | 1946 | static uint64_t kvmppc_read_int_dt(const char *filename) |
eadaada1 | 1947 | { |
9bc884b7 DG |
1948 | union { |
1949 | uint32_t v32; | |
1950 | uint64_t v64; | |
1951 | } u; | |
eadaada1 AG |
1952 | FILE *f; |
1953 | int len; | |
1954 | ||
7d94a30b | 1955 | f = fopen(filename, "rb"); |
eadaada1 AG |
1956 | if (!f) { |
1957 | return -1; | |
1958 | } | |
1959 | ||
9bc884b7 | 1960 | len = fread(&u, 1, sizeof(u), f); |
eadaada1 AG |
1961 | fclose(f); |
1962 | switch (len) { | |
9bc884b7 DG |
1963 | case 4: |
1964 | /* property is a 32-bit quantity */ | |
1965 | return be32_to_cpu(u.v32); | |
1966 | case 8: | |
1967 | return be64_to_cpu(u.v64); | |
eadaada1 AG |
1968 | } |
1969 | ||
1970 | return 0; | |
1971 | } | |
1972 | ||
7d94a30b SB |
1973 | /* Read a CPU node property from the host device tree that's a single |
1974 | * integer (32-bit or 64-bit). Returns 0 if anything goes wrong | |
1975 | * (can't find or open the property, or doesn't understand the | |
1976 | * format) */ | |
1977 | static uint64_t kvmppc_read_int_cpu_dt(const char *propname) | |
1978 | { | |
1979 | char buf[PATH_MAX], *tmp; | |
1980 | uint64_t val; | |
1981 | ||
1982 | if (kvmppc_find_cpu_dt(buf, sizeof(buf))) { | |
1983 | return -1; | |
1984 | } | |
1985 | ||
1986 | tmp = g_strdup_printf("%s/%s", buf, propname); | |
1987 | val = kvmppc_read_int_dt(tmp); | |
1988 | g_free(tmp); | |
1989 | ||
1990 | return val; | |
1991 | } | |
1992 | ||
9bc884b7 DG |
1993 | uint64_t kvmppc_get_clockfreq(void) |
1994 | { | |
1995 | return kvmppc_read_int_cpu_dt("clock-frequency"); | |
1996 | } | |
1997 | ||
6659394f DG |
1998 | uint32_t kvmppc_get_vmx(void) |
1999 | { | |
2000 | return kvmppc_read_int_cpu_dt("ibm,vmx"); | |
2001 | } | |
2002 | ||
2003 | uint32_t kvmppc_get_dfp(void) | |
2004 | { | |
2005 | return kvmppc_read_int_cpu_dt("ibm,dfp"); | |
2006 | } | |
2007 | ||
1a61a9ae SY |
2008 | static int kvmppc_get_pvinfo(CPUPPCState *env, struct kvm_ppc_pvinfo *pvinfo) |
2009 | { | |
2010 | PowerPCCPU *cpu = ppc_env_get_cpu(env); | |
2011 | CPUState *cs = CPU(cpu); | |
2012 | ||
6fd33a75 | 2013 | if (kvm_vm_check_extension(cs->kvm_state, KVM_CAP_PPC_GET_PVINFO) && |
1a61a9ae SY |
2014 | !kvm_vm_ioctl(cs->kvm_state, KVM_PPC_GET_PVINFO, pvinfo)) { |
2015 | return 0; | |
2016 | } | |
2017 | ||
2018 | return 1; | |
2019 | } | |
2020 | ||
2021 | int kvmppc_get_hasidle(CPUPPCState *env) | |
2022 | { | |
2023 | struct kvm_ppc_pvinfo pvinfo; | |
2024 | ||
2025 | if (!kvmppc_get_pvinfo(env, &pvinfo) && | |
2026 | (pvinfo.flags & KVM_PPC_PVINFO_FLAGS_EV_IDLE)) { | |
2027 | return 1; | |
2028 | } | |
2029 | ||
2030 | return 0; | |
2031 | } | |
2032 | ||
1328c2bf | 2033 | int kvmppc_get_hypercall(CPUPPCState *env, uint8_t *buf, int buf_len) |
45024f09 AG |
2034 | { |
2035 | uint32_t *hc = (uint32_t*)buf; | |
45024f09 AG |
2036 | struct kvm_ppc_pvinfo pvinfo; |
2037 | ||
1a61a9ae | 2038 | if (!kvmppc_get_pvinfo(env, &pvinfo)) { |
45024f09 | 2039 | memcpy(buf, pvinfo.hcall, buf_len); |
45024f09 AG |
2040 | return 0; |
2041 | } | |
45024f09 AG |
2042 | |
2043 | /* | |
d13fc32e | 2044 | * Fallback to always fail hypercalls regardless of endianness: |
45024f09 | 2045 | * |
d13fc32e | 2046 | * tdi 0,r0,72 (becomes b .+8 in wrong endian, nop in good endian) |
45024f09 | 2047 | * li r3, -1 |
d13fc32e AG |
2048 | * b .+8 (becomes nop in wrong endian) |
2049 | * bswap32(li r3, -1) | |
45024f09 AG |
2050 | */ |
2051 | ||
d13fc32e AG |
2052 | hc[0] = cpu_to_be32(0x08000048); |
2053 | hc[1] = cpu_to_be32(0x3860ffff); | |
2054 | hc[2] = cpu_to_be32(0x48000008); | |
2055 | hc[3] = cpu_to_be32(bswap32(0x3860ffff)); | |
45024f09 | 2056 | |
0ddbd053 | 2057 | return 1; |
45024f09 AG |
2058 | } |
2059 | ||
026bfd89 DG |
2060 | static inline int kvmppc_enable_hcall(KVMState *s, target_ulong hcall) |
2061 | { | |
2062 | return kvm_vm_enable_cap(s, KVM_CAP_PPC_ENABLE_HCALL, 0, hcall, 1); | |
2063 | } | |
2064 | ||
2065 | void kvmppc_enable_logical_ci_hcalls(void) | |
2066 | { | |
2067 | /* | |
2068 | * FIXME: it would be nice if we could detect the cases where | |
2069 | * we're using a device which requires the in kernel | |
2070 | * implementation of these hcalls, but the kernel lacks them and | |
2071 | * produce a warning. | |
2072 | */ | |
2073 | kvmppc_enable_hcall(kvm_state, H_LOGICAL_CI_LOAD); | |
2074 | kvmppc_enable_hcall(kvm_state, H_LOGICAL_CI_STORE); | |
2075 | } | |
2076 | ||
ef9971dd AK |
2077 | void kvmppc_enable_set_mode_hcall(void) |
2078 | { | |
2079 | kvmppc_enable_hcall(kvm_state, H_SET_MODE); | |
2080 | } | |
2081 | ||
5145ad4f NW |
2082 | void kvmppc_enable_clear_ref_mod_hcalls(void) |
2083 | { | |
2084 | kvmppc_enable_hcall(kvm_state, H_CLEAR_REF); | |
2085 | kvmppc_enable_hcall(kvm_state, H_CLEAR_MOD); | |
2086 | } | |
2087 | ||
1bc22652 | 2088 | void kvmppc_set_papr(PowerPCCPU *cpu) |
f61b4bed | 2089 | { |
1bc22652 | 2090 | CPUState *cs = CPU(cpu); |
f61b4bed AG |
2091 | int ret; |
2092 | ||
48add816 | 2093 | ret = kvm_vcpu_enable_cap(cs, KVM_CAP_PPC_PAPR, 0); |
f61b4bed | 2094 | if (ret) { |
072ed5f2 TH |
2095 | error_report("This vCPU type or KVM version does not support PAPR"); |
2096 | exit(1); | |
94135e81 | 2097 | } |
9b00ea49 DG |
2098 | |
2099 | /* Update the capability flag so we sync the right information | |
2100 | * with kvm */ | |
2101 | cap_papr = 1; | |
f61b4bed AG |
2102 | } |
2103 | ||
d6e166c0 | 2104 | int kvmppc_set_compat(PowerPCCPU *cpu, uint32_t compat_pvr) |
6db5bb0f | 2105 | { |
d6e166c0 | 2106 | return kvm_set_one_reg(CPU(cpu), KVM_REG_PPC_ARCH_COMPAT, &compat_pvr); |
6db5bb0f AK |
2107 | } |
2108 | ||
5b95b8b9 AG |
2109 | void kvmppc_set_mpic_proxy(PowerPCCPU *cpu, int mpic_proxy) |
2110 | { | |
5b95b8b9 | 2111 | CPUState *cs = CPU(cpu); |
5b95b8b9 AG |
2112 | int ret; |
2113 | ||
48add816 | 2114 | ret = kvm_vcpu_enable_cap(cs, KVM_CAP_PPC_EPR, 0, mpic_proxy); |
5b95b8b9 | 2115 | if (ret && mpic_proxy) { |
072ed5f2 TH |
2116 | error_report("This KVM version does not support EPR"); |
2117 | exit(1); | |
5b95b8b9 AG |
2118 | } |
2119 | } | |
2120 | ||
e97c3636 DG |
2121 | int kvmppc_smt_threads(void) |
2122 | { | |
2123 | return cap_ppc_smt ? cap_ppc_smt : 1; | |
2124 | } | |
2125 | ||
7f763a5d | 2126 | #ifdef TARGET_PPC64 |
658fa66b | 2127 | off_t kvmppc_alloc_rma(void **rma) |
354ac20a | 2128 | { |
354ac20a DG |
2129 | off_t size; |
2130 | int fd; | |
2131 | struct kvm_allocate_rma ret; | |
354ac20a DG |
2132 | |
2133 | /* If cap_ppc_rma == 0, contiguous RMA allocation is not supported | |
2134 | * if cap_ppc_rma == 1, contiguous RMA allocation is supported, but | |
2135 | * not necessary on this hardware | |
2136 | * if cap_ppc_rma == 2, contiguous RMA allocation is needed on this hardware | |
2137 | * | |
2138 | * FIXME: We should allow the user to force contiguous RMA | |
2139 | * allocation in the cap_ppc_rma==1 case. | |
2140 | */ | |
2141 | if (cap_ppc_rma < 2) { | |
2142 | return 0; | |
2143 | } | |
2144 | ||
2145 | fd = kvm_vm_ioctl(kvm_state, KVM_ALLOCATE_RMA, &ret); | |
2146 | if (fd < 0) { | |
2147 | fprintf(stderr, "KVM: Error on KVM_ALLOCATE_RMA: %s\n", | |
2148 | strerror(errno)); | |
2149 | return -1; | |
2150 | } | |
2151 | ||
2152 | size = MIN(ret.rma_size, 256ul << 20); | |
2153 | ||
658fa66b AK |
2154 | *rma = mmap(NULL, size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0); |
2155 | if (*rma == MAP_FAILED) { | |
354ac20a DG |
2156 | fprintf(stderr, "KVM: Error mapping RMA: %s\n", strerror(errno)); |
2157 | return -1; | |
2158 | }; | |
2159 | ||
354ac20a DG |
2160 | return size; |
2161 | } | |
2162 | ||
7f763a5d DG |
2163 | uint64_t kvmppc_rma_size(uint64_t current_size, unsigned int hash_shift) |
2164 | { | |
f36951c1 DG |
2165 | struct kvm_ppc_smmu_info info; |
2166 | long rampagesize, best_page_shift; | |
2167 | int i; | |
2168 | ||
7f763a5d DG |
2169 | if (cap_ppc_rma >= 2) { |
2170 | return current_size; | |
2171 | } | |
f36951c1 DG |
2172 | |
2173 | /* Find the largest hardware supported page size that's less than | |
2174 | * or equal to the (logical) backing page size of guest RAM */ | |
182735ef | 2175 | kvm_get_smmu_info(POWERPC_CPU(first_cpu), &info); |
9c607668 | 2176 | rampagesize = qemu_getrampagesize(); |
f36951c1 DG |
2177 | best_page_shift = 0; |
2178 | ||
2179 | for (i = 0; i < KVM_PPC_PAGE_SIZES_MAX_SZ; i++) { | |
2180 | struct kvm_ppc_one_seg_page_size *sps = &info.sps[i]; | |
2181 | ||
2182 | if (!sps->page_shift) { | |
2183 | continue; | |
2184 | } | |
2185 | ||
2186 | if ((sps->page_shift > best_page_shift) | |
2187 | && ((1UL << sps->page_shift) <= rampagesize)) { | |
2188 | best_page_shift = sps->page_shift; | |
2189 | } | |
2190 | } | |
2191 | ||
7f763a5d | 2192 | return MIN(current_size, |
f36951c1 | 2193 | 1ULL << (best_page_shift + hash_shift - 7)); |
7f763a5d DG |
2194 | } |
2195 | #endif | |
2196 | ||
da95324e AK |
2197 | bool kvmppc_spapr_use_multitce(void) |
2198 | { | |
2199 | return cap_spapr_multitce; | |
2200 | } | |
2201 | ||
3dc410ae AK |
2202 | int kvmppc_spapr_enable_inkernel_multitce(void) |
2203 | { | |
2204 | int ret; | |
2205 | ||
2206 | ret = kvm_vm_enable_cap(kvm_state, KVM_CAP_PPC_ENABLE_HCALL, 0, | |
2207 | H_PUT_TCE_INDIRECT, 1); | |
2208 | if (!ret) { | |
2209 | ret = kvm_vm_enable_cap(kvm_state, KVM_CAP_PPC_ENABLE_HCALL, 0, | |
2210 | H_STUFF_TCE, 1); | |
2211 | } | |
2212 | ||
2213 | return ret; | |
2214 | } | |
2215 | ||
d6ee2a7c AK |
2216 | void *kvmppc_create_spapr_tce(uint32_t liobn, uint32_t page_shift, |
2217 | uint64_t bus_offset, uint32_t nb_table, | |
2218 | int *pfd, bool need_vfio) | |
0f5cb298 | 2219 | { |
0f5cb298 DG |
2220 | long len; |
2221 | int fd; | |
2222 | void *table; | |
2223 | ||
b5aec396 DG |
2224 | /* Must set fd to -1 so we don't try to munmap when called for |
2225 | * destroying the table, which the upper layers -will- do | |
2226 | */ | |
2227 | *pfd = -1; | |
6a81dd17 | 2228 | if (!cap_spapr_tce || (need_vfio && !cap_spapr_vfio)) { |
0f5cb298 DG |
2229 | return NULL; |
2230 | } | |
2231 | ||
d6ee2a7c AK |
2232 | if (cap_spapr_tce_64) { |
2233 | struct kvm_create_spapr_tce_64 args = { | |
2234 | .liobn = liobn, | |
2235 | .page_shift = page_shift, | |
2236 | .offset = bus_offset >> page_shift, | |
2237 | .size = nb_table, | |
2238 | .flags = 0 | |
2239 | }; | |
2240 | fd = kvm_vm_ioctl(kvm_state, KVM_CREATE_SPAPR_TCE_64, &args); | |
2241 | if (fd < 0) { | |
2242 | fprintf(stderr, | |
2243 | "KVM: Failed to create TCE64 table for liobn 0x%x\n", | |
2244 | liobn); | |
2245 | return NULL; | |
2246 | } | |
2247 | } else if (cap_spapr_tce) { | |
2248 | uint64_t window_size = (uint64_t) nb_table << page_shift; | |
2249 | struct kvm_create_spapr_tce args = { | |
2250 | .liobn = liobn, | |
2251 | .window_size = window_size, | |
2252 | }; | |
2253 | if ((window_size != args.window_size) || bus_offset) { | |
2254 | return NULL; | |
2255 | } | |
2256 | fd = kvm_vm_ioctl(kvm_state, KVM_CREATE_SPAPR_TCE, &args); | |
2257 | if (fd < 0) { | |
2258 | fprintf(stderr, "KVM: Failed to create TCE table for liobn 0x%x\n", | |
2259 | liobn); | |
2260 | return NULL; | |
2261 | } | |
2262 | } else { | |
0f5cb298 DG |
2263 | return NULL; |
2264 | } | |
2265 | ||
d6ee2a7c | 2266 | len = nb_table * sizeof(uint64_t); |
0f5cb298 DG |
2267 | /* FIXME: round this up to page size */ |
2268 | ||
74b41e56 | 2269 | table = mmap(NULL, len, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0); |
0f5cb298 | 2270 | if (table == MAP_FAILED) { |
b5aec396 DG |
2271 | fprintf(stderr, "KVM: Failed to map TCE table for liobn 0x%x\n", |
2272 | liobn); | |
0f5cb298 DG |
2273 | close(fd); |
2274 | return NULL; | |
2275 | } | |
2276 | ||
2277 | *pfd = fd; | |
2278 | return table; | |
2279 | } | |
2280 | ||
523e7b8a | 2281 | int kvmppc_remove_spapr_tce(void *table, int fd, uint32_t nb_table) |
0f5cb298 DG |
2282 | { |
2283 | long len; | |
2284 | ||
2285 | if (fd < 0) { | |
2286 | return -1; | |
2287 | } | |
2288 | ||
523e7b8a | 2289 | len = nb_table * sizeof(uint64_t); |
0f5cb298 DG |
2290 | if ((munmap(table, len) < 0) || |
2291 | (close(fd) < 0)) { | |
b5aec396 DG |
2292 | fprintf(stderr, "KVM: Unexpected error removing TCE table: %s", |
2293 | strerror(errno)); | |
0f5cb298 DG |
2294 | /* Leak the table */ |
2295 | } | |
2296 | ||
2297 | return 0; | |
2298 | } | |
2299 | ||
7f763a5d DG |
2300 | int kvmppc_reset_htab(int shift_hint) |
2301 | { | |
2302 | uint32_t shift = shift_hint; | |
2303 | ||
ace9a2cb DG |
2304 | if (!kvm_enabled()) { |
2305 | /* Full emulation, tell caller to allocate htab itself */ | |
2306 | return 0; | |
2307 | } | |
2308 | if (kvm_check_extension(kvm_state, KVM_CAP_PPC_ALLOC_HTAB)) { | |
7f763a5d DG |
2309 | int ret; |
2310 | ret = kvm_vm_ioctl(kvm_state, KVM_PPC_ALLOCATE_HTAB, &shift); | |
ace9a2cb DG |
2311 | if (ret == -ENOTTY) { |
2312 | /* At least some versions of PR KVM advertise the | |
2313 | * capability, but don't implement the ioctl(). Oops. | |
2314 | * Return 0 so that we allocate the htab in qemu, as is | |
2315 | * correct for PR. */ | |
2316 | return 0; | |
2317 | } else if (ret < 0) { | |
7f763a5d DG |
2318 | return ret; |
2319 | } | |
2320 | return shift; | |
2321 | } | |
2322 | ||
ace9a2cb DG |
2323 | /* We have a kernel that predates the htab reset calls. For PR |
2324 | * KVM, we need to allocate the htab ourselves, for an HV KVM of | |
96c9cff0 TH |
2325 | * this era, it has allocated a 16MB fixed size hash table already. */ |
2326 | if (kvmppc_is_pr(kvm_state)) { | |
ace9a2cb DG |
2327 | /* PR - tell caller to allocate htab */ |
2328 | return 0; | |
2329 | } else { | |
2330 | /* HV - assume 16MB kernel allocated htab */ | |
2331 | return 24; | |
2332 | } | |
7f763a5d DG |
2333 | } |
2334 | ||
a1e98583 DG |
2335 | static inline uint32_t mfpvr(void) |
2336 | { | |
2337 | uint32_t pvr; | |
2338 | ||
2339 | asm ("mfpvr %0" | |
2340 | : "=r"(pvr)); | |
2341 | return pvr; | |
2342 | } | |
2343 | ||
a7342588 DG |
2344 | static void alter_insns(uint64_t *word, uint64_t flags, bool on) |
2345 | { | |
2346 | if (on) { | |
2347 | *word |= flags; | |
2348 | } else { | |
2349 | *word &= ~flags; | |
2350 | } | |
2351 | } | |
2352 | ||
2985b86b AF |
2353 | static void kvmppc_host_cpu_class_init(ObjectClass *oc, void *data) |
2354 | { | |
2355 | PowerPCCPUClass *pcc = POWERPC_CPU_CLASS(oc); | |
a7342588 DG |
2356 | uint32_t vmx = kvmppc_get_vmx(); |
2357 | uint32_t dfp = kvmppc_get_dfp(); | |
0cbad81f DG |
2358 | uint32_t dcache_size = kvmppc_read_int_cpu_dt("d-cache-size"); |
2359 | uint32_t icache_size = kvmppc_read_int_cpu_dt("i-cache-size"); | |
a1e98583 | 2360 | |
cfe34f44 | 2361 | /* Now fix up the class with information we can query from the host */ |
3bc9ccc0 | 2362 | pcc->pvr = mfpvr(); |
a7342588 | 2363 | |
70bca53f AG |
2364 | if (vmx != -1) { |
2365 | /* Only override when we know what the host supports */ | |
cfe34f44 AF |
2366 | alter_insns(&pcc->insns_flags, PPC_ALTIVEC, vmx > 0); |
2367 | alter_insns(&pcc->insns_flags2, PPC2_VSX, vmx > 1); | |
70bca53f AG |
2368 | } |
2369 | if (dfp != -1) { | |
2370 | /* Only override when we know what the host supports */ | |
cfe34f44 | 2371 | alter_insns(&pcc->insns_flags2, PPC2_DFP, dfp); |
70bca53f | 2372 | } |
0cbad81f DG |
2373 | |
2374 | if (dcache_size != -1) { | |
2375 | pcc->l1_dcache_size = dcache_size; | |
2376 | } | |
2377 | ||
2378 | if (icache_size != -1) { | |
2379 | pcc->l1_icache_size = icache_size; | |
2380 | } | |
c64abd1f SB |
2381 | |
2382 | #if defined(TARGET_PPC64) | |
2383 | pcc->radix_page_info = kvm_get_radix_page_info(); | |
5f3066d8 DG |
2384 | |
2385 | if ((pcc->pvr & 0xffffff00) == CPU_POWERPC_POWER9_DD1) { | |
2386 | /* | |
2387 | * POWER9 DD1 has some bugs which make it not really ISA 3.00 | |
2388 | * compliant. More importantly, advertising ISA 3.00 | |
2389 | * architected mode may prevent guests from activating | |
2390 | * necessary DD1 workarounds. | |
2391 | */ | |
2392 | pcc->pcr_supported &= ~(PCR_COMPAT_3_00 | PCR_COMPAT_2_07 | |
2393 | | PCR_COMPAT_2_06 | PCR_COMPAT_2_05); | |
2394 | } | |
c64abd1f | 2395 | #endif /* defined(TARGET_PPC64) */ |
a1e98583 DG |
2396 | } |
2397 | ||
3b961124 SY |
2398 | bool kvmppc_has_cap_epr(void) |
2399 | { | |
2400 | return cap_epr; | |
2401 | } | |
2402 | ||
7c43bca0 AK |
2403 | bool kvmppc_has_cap_htab_fd(void) |
2404 | { | |
2405 | return cap_htab_fd; | |
2406 | } | |
2407 | ||
87a91de6 AG |
2408 | bool kvmppc_has_cap_fixup_hcalls(void) |
2409 | { | |
2410 | return cap_fixup_hcalls; | |
2411 | } | |
2412 | ||
bac3bf28 TH |
2413 | bool kvmppc_has_cap_htm(void) |
2414 | { | |
2415 | return cap_htm; | |
2416 | } | |
2417 | ||
cf1c4cce SB |
2418 | bool kvmppc_has_cap_mmu_radix(void) |
2419 | { | |
2420 | return cap_mmu_radix; | |
2421 | } | |
2422 | ||
2423 | bool kvmppc_has_cap_mmu_hash_v3(void) | |
2424 | { | |
2425 | return cap_mmu_hash_v3; | |
2426 | } | |
2427 | ||
52b2519c TH |
2428 | PowerPCCPUClass *kvm_ppc_get_host_cpu_class(void) |
2429 | { | |
2430 | uint32_t host_pvr = mfpvr(); | |
2431 | PowerPCCPUClass *pvr_pcc; | |
2432 | ||
2433 | pvr_pcc = ppc_cpu_class_by_pvr(host_pvr); | |
2434 | if (pvr_pcc == NULL) { | |
2435 | pvr_pcc = ppc_cpu_class_by_pvr_mask(host_pvr); | |
2436 | } | |
2437 | ||
2438 | return pvr_pcc; | |
2439 | } | |
2440 | ||
5ba4576b AF |
2441 | static int kvm_ppc_register_host_cpu_type(void) |
2442 | { | |
2443 | TypeInfo type_info = { | |
2444 | .name = TYPE_HOST_POWERPC_CPU, | |
5ba4576b AF |
2445 | .class_init = kvmppc_host_cpu_class_init, |
2446 | }; | |
5ba4576b | 2447 | PowerPCCPUClass *pvr_pcc; |
5b79b1ca | 2448 | DeviceClass *dc; |
715d4b96 | 2449 | int i; |
5ba4576b | 2450 | |
52b2519c | 2451 | pvr_pcc = kvm_ppc_get_host_cpu_class(); |
5ba4576b AF |
2452 | if (pvr_pcc == NULL) { |
2453 | return -1; | |
2454 | } | |
2455 | type_info.parent = object_class_get_name(OBJECT_CLASS(pvr_pcc)); | |
2456 | type_register(&type_info); | |
5b79b1ca | 2457 | |
3b542549 BR |
2458 | #if defined(TARGET_PPC64) |
2459 | type_info.name = g_strdup_printf("%s-"TYPE_SPAPR_CPU_CORE, "host"); | |
2460 | type_info.parent = TYPE_SPAPR_CPU_CORE, | |
7ebaf795 BR |
2461 | type_info.instance_size = sizeof(sPAPRCPUCore); |
2462 | type_info.instance_init = NULL; | |
2463 | type_info.class_init = spapr_cpu_core_class_init; | |
2464 | type_info.class_data = (void *) "host"; | |
3b542549 BR |
2465 | type_register(&type_info); |
2466 | g_free((void *)type_info.name); | |
3b542549 BR |
2467 | #endif |
2468 | ||
715d4b96 TH |
2469 | /* |
2470 | * Update generic CPU family class alias (e.g. on a POWER8NVL host, | |
2471 | * we want "POWER8" to be a "family" alias that points to the current | |
2472 | * host CPU type, too) | |
2473 | */ | |
2474 | dc = DEVICE_CLASS(ppc_cpu_get_family_class(pvr_pcc)); | |
2475 | for (i = 0; ppc_cpu_aliases[i].alias != NULL; i++) { | |
2476 | if (strcmp(ppc_cpu_aliases[i].alias, dc->desc) == 0) { | |
2477 | ObjectClass *oc = OBJECT_CLASS(pvr_pcc); | |
2478 | char *suffix; | |
2479 | ||
2480 | ppc_cpu_aliases[i].model = g_strdup(object_class_get_name(oc)); | |
2481 | suffix = strstr(ppc_cpu_aliases[i].model, "-"TYPE_POWERPC_CPU); | |
2482 | if (suffix) { | |
2483 | *suffix = 0; | |
2484 | } | |
2485 | ppc_cpu_aliases[i].oc = oc; | |
2486 | break; | |
2487 | } | |
2488 | } | |
2489 | ||
5ba4576b AF |
2490 | return 0; |
2491 | } | |
2492 | ||
feaa64c4 DG |
2493 | int kvmppc_define_rtas_kernel_token(uint32_t token, const char *function) |
2494 | { | |
2495 | struct kvm_rtas_token_args args = { | |
2496 | .token = token, | |
2497 | }; | |
2498 | ||
2499 | if (!kvm_check_extension(kvm_state, KVM_CAP_PPC_RTAS)) { | |
2500 | return -ENOENT; | |
2501 | } | |
2502 | ||
2503 | strncpy(args.name, function, sizeof(args.name)); | |
2504 | ||
2505 | return kvm_vm_ioctl(kvm_state, KVM_PPC_RTAS_DEFINE_TOKEN, &args); | |
2506 | } | |
12b1143b | 2507 | |
e68cb8b4 AK |
2508 | int kvmppc_get_htab_fd(bool write) |
2509 | { | |
2510 | struct kvm_get_htab_fd s = { | |
2511 | .flags = write ? KVM_GET_HTAB_WRITE : 0, | |
2512 | .start_index = 0, | |
2513 | }; | |
2514 | ||
2515 | if (!cap_htab_fd) { | |
2516 | fprintf(stderr, "KVM version doesn't support saving the hash table\n"); | |
2517 | return -1; | |
2518 | } | |
2519 | ||
2520 | return kvm_vm_ioctl(kvm_state, KVM_PPC_GET_HTAB_FD, &s); | |
2521 | } | |
2522 | ||
2523 | int kvmppc_save_htab(QEMUFile *f, int fd, size_t bufsize, int64_t max_ns) | |
2524 | { | |
bc72ad67 | 2525 | int64_t starttime = qemu_clock_get_ns(QEMU_CLOCK_REALTIME); |
e68cb8b4 AK |
2526 | uint8_t buf[bufsize]; |
2527 | ssize_t rc; | |
2528 | ||
2529 | do { | |
2530 | rc = read(fd, buf, bufsize); | |
2531 | if (rc < 0) { | |
2532 | fprintf(stderr, "Error reading data from KVM HTAB fd: %s\n", | |
2533 | strerror(errno)); | |
2534 | return rc; | |
2535 | } else if (rc) { | |
e094c4c1 CLG |
2536 | uint8_t *buffer = buf; |
2537 | ssize_t n = rc; | |
2538 | while (n) { | |
2539 | struct kvm_get_htab_header *head = | |
2540 | (struct kvm_get_htab_header *) buffer; | |
2541 | size_t chunksize = sizeof(*head) + | |
2542 | HASH_PTE_SIZE_64 * head->n_valid; | |
2543 | ||
2544 | qemu_put_be32(f, head->index); | |
2545 | qemu_put_be16(f, head->n_valid); | |
2546 | qemu_put_be16(f, head->n_invalid); | |
2547 | qemu_put_buffer(f, (void *)(head + 1), | |
2548 | HASH_PTE_SIZE_64 * head->n_valid); | |
2549 | ||
2550 | buffer += chunksize; | |
2551 | n -= chunksize; | |
2552 | } | |
e68cb8b4 AK |
2553 | } |
2554 | } while ((rc != 0) | |
2555 | && ((max_ns < 0) | |
bc72ad67 | 2556 | || ((qemu_clock_get_ns(QEMU_CLOCK_REALTIME) - starttime) < max_ns))); |
e68cb8b4 AK |
2557 | |
2558 | return (rc == 0) ? 1 : 0; | |
2559 | } | |
2560 | ||
2561 | int kvmppc_load_htab_chunk(QEMUFile *f, int fd, uint32_t index, | |
2562 | uint16_t n_valid, uint16_t n_invalid) | |
2563 | { | |
2564 | struct kvm_get_htab_header *buf; | |
2565 | size_t chunksize = sizeof(*buf) + n_valid*HASH_PTE_SIZE_64; | |
2566 | ssize_t rc; | |
2567 | ||
2568 | buf = alloca(chunksize); | |
e68cb8b4 AK |
2569 | buf->index = index; |
2570 | buf->n_valid = n_valid; | |
2571 | buf->n_invalid = n_invalid; | |
2572 | ||
2573 | qemu_get_buffer(f, (void *)(buf + 1), HASH_PTE_SIZE_64*n_valid); | |
2574 | ||
2575 | rc = write(fd, buf, chunksize); | |
2576 | if (rc < 0) { | |
2577 | fprintf(stderr, "Error writing KVM hash table: %s\n", | |
2578 | strerror(errno)); | |
2579 | return rc; | |
2580 | } | |
2581 | if (rc != chunksize) { | |
2582 | /* We should never get a short write on a single chunk */ | |
2583 | fprintf(stderr, "Short write, restoring KVM hash table\n"); | |
2584 | return -1; | |
2585 | } | |
2586 | return 0; | |
2587 | } | |
2588 | ||
20d695a9 | 2589 | bool kvm_arch_stop_on_emulation_error(CPUState *cpu) |
4513d923 GN |
2590 | { |
2591 | return true; | |
2592 | } | |
a1b87fe0 | 2593 | |
82169660 SW |
2594 | void kvm_arch_init_irq_routing(KVMState *s) |
2595 | { | |
2596 | } | |
c65f9a07 | 2597 | |
1ad9f0a4 | 2598 | void kvmppc_read_hptes(ppc_hash_pte64_t *hptes, hwaddr ptex, int n) |
7c43bca0 | 2599 | { |
1ad9f0a4 DG |
2600 | struct kvm_get_htab_fd ghf = { |
2601 | .flags = 0, | |
2602 | .start_index = ptex, | |
2603 | }; | |
2604 | int fd, rc; | |
2605 | int i; | |
7c43bca0 | 2606 | |
1ad9f0a4 DG |
2607 | fd = kvm_vm_ioctl(kvm_state, KVM_PPC_GET_HTAB_FD, &ghf); |
2608 | if (fd < 0) { | |
2609 | hw_error("kvmppc_read_hptes: Unable to open HPT fd"); | |
7c43bca0 AK |
2610 | } |
2611 | ||
1ad9f0a4 DG |
2612 | i = 0; |
2613 | while (i < n) { | |
2614 | struct kvm_get_htab_header *hdr; | |
2615 | int m = n < HPTES_PER_GROUP ? n : HPTES_PER_GROUP; | |
2616 | char buf[sizeof(*hdr) + m * HASH_PTE_SIZE_64]; | |
7c43bca0 | 2617 | |
1ad9f0a4 DG |
2618 | rc = read(fd, buf, sizeof(buf)); |
2619 | if (rc < 0) { | |
2620 | hw_error("kvmppc_read_hptes: Unable to read HPTEs"); | |
2621 | } | |
7c43bca0 | 2622 | |
1ad9f0a4 DG |
2623 | hdr = (struct kvm_get_htab_header *)buf; |
2624 | while ((i < n) && ((char *)hdr < (buf + rc))) { | |
2625 | int invalid = hdr->n_invalid; | |
7c43bca0 | 2626 | |
1ad9f0a4 DG |
2627 | if (hdr->index != (ptex + i)) { |
2628 | hw_error("kvmppc_read_hptes: Unexpected HPTE index %"PRIu32 | |
2629 | " != (%"HWADDR_PRIu" + %d", hdr->index, ptex, i); | |
2630 | } | |
2631 | ||
2632 | memcpy(hptes + i, hdr + 1, HASH_PTE_SIZE_64 * hdr->n_valid); | |
2633 | i += hdr->n_valid; | |
7c43bca0 | 2634 | |
1ad9f0a4 DG |
2635 | if ((n - i) < invalid) { |
2636 | invalid = n - i; | |
2637 | } | |
2638 | memset(hptes + i, 0, invalid * HASH_PTE_SIZE_64); | |
2639 | i += hdr->n_invalid; | |
2640 | ||
2641 | hdr = (struct kvm_get_htab_header *) | |
2642 | ((char *)(hdr + 1) + HASH_PTE_SIZE_64 * hdr->n_valid); | |
2643 | } | |
2644 | } | |
2645 | ||
2646 | close(fd); | |
7c43bca0 | 2647 | } |
c1385933 | 2648 | |
1ad9f0a4 | 2649 | void kvmppc_write_hpte(hwaddr ptex, uint64_t pte0, uint64_t pte1) |
c1385933 | 2650 | { |
1ad9f0a4 | 2651 | int fd, rc; |
c1385933 | 2652 | struct kvm_get_htab_fd ghf; |
1ad9f0a4 DG |
2653 | struct { |
2654 | struct kvm_get_htab_header hdr; | |
2655 | uint64_t pte0; | |
2656 | uint64_t pte1; | |
2657 | } buf; | |
c1385933 AK |
2658 | |
2659 | ghf.flags = 0; | |
2660 | ghf.start_index = 0; /* Ignored */ | |
1ad9f0a4 DG |
2661 | fd = kvm_vm_ioctl(kvm_state, KVM_PPC_GET_HTAB_FD, &ghf); |
2662 | if (fd < 0) { | |
2663 | hw_error("kvmppc_write_hpte: Unable to open HPT fd"); | |
c1385933 AK |
2664 | } |
2665 | ||
1ad9f0a4 DG |
2666 | buf.hdr.n_valid = 1; |
2667 | buf.hdr.n_invalid = 0; | |
2668 | buf.hdr.index = ptex; | |
2669 | buf.pte0 = cpu_to_be64(pte0); | |
2670 | buf.pte1 = cpu_to_be64(pte1); | |
c1385933 | 2671 | |
1ad9f0a4 DG |
2672 | rc = write(fd, &buf, sizeof(buf)); |
2673 | if (rc != sizeof(buf)) { | |
2674 | hw_error("kvmppc_write_hpte: Unable to update KVM HPT"); | |
2675 | } | |
2676 | close(fd); | |
c1385933 | 2677 | } |
9e03a040 FB |
2678 | |
2679 | int kvm_arch_fixup_msi_route(struct kvm_irq_routing_entry *route, | |
dc9f06ca | 2680 | uint64_t address, uint32_t data, PCIDevice *dev) |
9e03a040 FB |
2681 | { |
2682 | return 0; | |
2683 | } | |
1850b6b7 | 2684 | |
38d87493 PX |
2685 | int kvm_arch_add_msi_route_post(struct kvm_irq_routing_entry *route, |
2686 | int vector, PCIDevice *dev) | |
2687 | { | |
2688 | return 0; | |
2689 | } | |
2690 | ||
2691 | int kvm_arch_release_virq_post(int virq) | |
2692 | { | |
2693 | return 0; | |
2694 | } | |
2695 | ||
1850b6b7 EA |
2696 | int kvm_arch_msi_data_to_gsi(uint32_t data) |
2697 | { | |
2698 | return data & 0xffff; | |
2699 | } | |
4d9392be TH |
2700 | |
2701 | int kvmppc_enable_hwrng(void) | |
2702 | { | |
2703 | if (!kvm_enabled() || !kvm_check_extension(kvm_state, KVM_CAP_PPC_HWRNG)) { | |
2704 | return -1; | |
2705 | } | |
2706 | ||
2707 | return kvmppc_enable_hcall(kvm_state, H_RANDOM); | |
2708 | } |