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