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