blob: 0c5056dd5b279ae83574887325dc89c473b0856c [file] [log] [blame]
aurel32d76d1652008-12-16 10:43:58 +00001/*
2 * PowerPC implementation of KVM hooks
3 *
4 * Copyright IBM Corp. 2007
Scott Wood90dc8812011-04-29 17:10:23 -05005 * Copyright (C) 2011 Freescale Semiconductor, Inc.
aurel32d76d1652008-12-16 10:43:58 +00006 *
7 * Authors:
8 * Jerone Young <jyoung5@us.ibm.com>
9 * Christian Ehrhardt <ehrhardt@linux.vnet.ibm.com>
10 * Hollis Blanchard <hollisb@us.ibm.com>
11 *
12 * This work is licensed under the terms of the GNU GPL, version 2 or later.
13 * See the COPYING file in the top-level directory.
14 *
15 */
16
Peter Maydell0d755902016-01-26 18:16:58 +000017#include "qemu/osdep.h"
Alexander Grafeadaada2011-07-21 02:29:15 +020018#include <dirent.h>
aurel32d76d1652008-12-16 10:43:58 +000019#include <sys/ioctl.h>
Benjamin Herrenschmidt4656e1f2012-06-18 19:56:25 +000020#include <sys/vfs.h>
aurel32d76d1652008-12-16 10:43:58 +000021
22#include <linux/kvm.h>
23
24#include "qemu-common.h"
David Gibson30f4b052017-05-12 15:46:11 +100025#include "qapi/error.h"
Thomas Huth072ed5f2016-02-18 22:01:38 +010026#include "qemu/error-report.h"
Paolo Bonzini33c11872016-03-15 16:58:45 +010027#include "cpu.h"
Thomas Huth715d4b92017-01-31 14:11:58 +010028#include "cpu-models.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010029#include "qemu/timer.h"
Vincent Palatinb3946622017-01-10 11:59:55 +010030#include "sysemu/hw_accel.h"
aurel32d76d1652008-12-16 10:43:58 +000031#include "kvm_ppc.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010032#include "sysemu/cpus.h"
33#include "sysemu/device_tree.h"
David Gibsond5aea6f2013-03-12 00:31:18 +000034#include "mmu-hash64.h"
aurel32d76d1652008-12-16 10:43:58 +000035
Alexander Graff61b4be2011-08-09 17:57:37 +020036#include "hw/sysbus.h"
Paolo Bonzini0d09e412013-02-05 17:06:20 +010037#include "hw/ppc/spapr.h"
Bharata B Rao7ebaf792016-09-12 13:27:20 +053038#include "hw/ppc/spapr_cpu_core.h"
Markus Armbruster650d1032019-08-12 07:23:48 +020039#include "hw/hw.h"
Alexey Kardashevskiy98a8b522014-05-01 20:37:09 +100040#include "hw/ppc/ppc.h"
Markus Armbrusterca77ee22019-08-12 07:23:39 +020041#include "migration/qemu-file-types.h"
Bharat Bhushan31f2cb82013-02-24 18:16:21 +000042#include "sysemu/watchdog.h"
Alexey Kardashevskiyb36f1002014-02-04 15:12:34 +110043#include "trace.h"
Bharat Bhushan88365d12014-07-14 14:45:37 +053044#include "exec/gdbstub.h"
Paolo Bonzini4c663752015-04-08 13:30:58 +020045#include "exec/memattrs.h"
Alexey Kardashevskiy9c607662017-03-02 13:36:11 +110046#include "exec/ram_addr.h"
Michael Roth2d103aa2015-07-02 15:46:14 -050047#include "sysemu/hostmem.h"
Veronia Bahaaf348b6d2016-03-20 19:16:19 +020048#include "qemu/cutils.h"
Markus Armbrusterdb725812019-08-12 07:23:50 +020049#include "qemu/main-loop.h"
Alexey Kardashevskiy9c607662017-03-02 13:36:11 +110050#include "qemu/mmap-alloc.h"
Sam Bobrofff3d9f302017-03-29 16:01:28 +110051#include "elf.h"
Sam Bobroffc64abd12017-03-20 10:46:43 +110052#include "sysemu/kvm_int.h"
Alexander Graff61b4be2011-08-09 17:57:37 +020053
Alexander Grafeadaada2011-07-21 02:29:15 +020054#define PROC_DEVTREE_CPU "/proc/device-tree/cpus/"
55
Fabiano Rosas6e0552a2020-01-10 12:13:42 -030056#define DEBUG_RETURN_GUEST 0
57#define DEBUG_RETURN_GDB 1
58
Jan Kiszka94a8d392011-01-21 21:48:17 +010059const KVMCapabilityInfo kvm_arch_required_capabilities[] = {
60 KVM_CAP_LAST_INFO
61};
62
David Gibsonc995e942019-03-21 21:47:25 +110063static int cap_interrupt_unset;
Scott Wood90dc8812011-04-29 17:10:23 -050064static int cap_segstate;
Scott Wood90dc8812011-04-29 17:10:23 -050065static int cap_booke_sregs;
David Gibsone97c3632011-09-29 21:39:10 +000066static int cap_ppc_smt;
Sam Bobrofffa98fbf2017-08-18 15:50:22 +100067static int cap_ppc_smt_possible;
David Gibson0f5cb292011-09-29 21:39:12 +000068static int cap_spapr_tce;
Alexey Kardashevskiyd6ee2a72017-03-10 12:41:13 +110069static int cap_spapr_tce_64;
Alexey Kardashevskiyda953242014-05-27 15:36:30 +100070static int cap_spapr_multitce;
Alexey Kardashevskiy9bb62a02014-06-10 15:39:21 +100071static int cap_spapr_vfio;
David Gibsonf1af19d2012-09-12 16:57:09 +000072static int cap_hior;
David Gibsond67d40e2013-02-20 16:41:50 +000073static int cap_one_reg;
Stuart Yoder3b961122013-03-30 06:40:49 +000074static int cap_epr;
Bharat Bhushan31f2cb82013-02-24 18:16:21 +000075static int cap_ppc_watchdog;
David Gibson9b00ea42013-04-07 19:08:22 +000076static int cap_papr;
Alexey Kardashevskiye68cb8b2013-07-18 14:33:03 -050077static int cap_htab_fd;
Alexander Graf87a91de2014-06-04 12:14:08 +020078static int cap_fixup_hcalls;
Thomas Huthbac3bf22016-09-28 13:16:30 +020079static int cap_htm; /* Hardware transactional memory support */
Sam Bobroffcf1c4cc2017-03-20 10:46:44 +110080static int cap_mmu_radix;
81static int cap_mmu_hash_v3;
Cédric Le Goater38afd772019-05-13 10:42:33 +020082static int cap_xive;
David Gibsonb55d2952017-07-12 17:56:55 +100083static int cap_resize_hpt;
Daniel Henrique Barbozac363a372017-08-09 17:43:46 -030084static int cap_ppc_pvr_compat;
Suraj Jitindar Singh8acc2ae2018-01-19 15:59:59 +110085static int cap_ppc_safe_cache;
86static int cap_ppc_safe_bounds_check;
87static int cap_ppc_safe_indirect_branch;
Suraj Jitindar Singh8ff43ee2019-03-01 14:19:12 +110088static int cap_ppc_count_cache_flush_assist;
Suraj Jitindar Singhb9a477b2018-10-08 14:25:39 +110089static int cap_ppc_nested_kvm_hv;
Suraj Jitindar Singh7d050522019-03-01 13:43:16 +110090static int cap_large_decr;
Nicholas Pigginec010c02020-03-26 00:29:03 +100091static int cap_fwnmi;
Alexander Graffc87e182010-08-30 13:49:15 +020092
Bharat Bhushan3c902d42014-07-14 14:45:35 +053093static uint32_t debug_inst_opcode;
94
David Gibsonc995e942019-03-21 21:47:25 +110095/*
David Gibsonc995e942019-03-21 21:47:25 +110096 * Check whether we are running with KVM-PR (instead of KVM-HV). This
Thomas Huth96c9cff2016-09-29 12:48:06 +020097 * should only be used for fallback tests - generally we should use
98 * explicit capabilities for the features we want, rather than
David Gibsonc995e942019-03-21 21:47:25 +110099 * assuming what is/isn't available depending on the KVM variant.
100 */
Thomas Huth96c9cff2016-09-29 12:48:06 +0200101static bool kvmppc_is_pr(KVMState *ks)
102{
103 /* Assume KVM-PR if the GET_PVINFO capability is available */
Greg Kurz70a0c192017-09-14 12:48:04 +0200104 return kvm_vm_check_extension(ks, KVM_CAP_PPC_GET_PVINFO) != 0;
Thomas Huth96c9cff2016-09-29 12:48:06 +0200105}
106
David Gibson165dc3e2019-10-30 17:20:35 +0100107static int kvm_ppc_register_host_cpu_type(void);
Suraj Jitindar Singh8acc2ae2018-01-19 15:59:59 +1100108static void kvmppc_get_cpu_characteristics(KVMState *s);
Suraj Jitindar Singh7d050522019-03-01 13:43:16 +1100109static int kvmppc_get_dec_bits(void);
Andreas Färber5ba45762013-02-23 11:22:12 +0000110
Marcel Apfelbaumb16565b2015-02-04 17:43:51 +0200111int kvm_arch_init(MachineState *ms, KVMState *s)
aurel32d76d1652008-12-16 10:43:58 +0000112{
Alexander Graffc87e182010-08-30 13:49:15 +0200113 cap_interrupt_unset = kvm_check_extension(s, KVM_CAP_PPC_UNSET_IRQ);
Scott Wood90dc8812011-04-29 17:10:23 -0500114 cap_segstate = kvm_check_extension(s, KVM_CAP_PPC_SEGSTATE);
Scott Wood90dc8812011-04-29 17:10:23 -0500115 cap_booke_sregs = kvm_check_extension(s, KVM_CAP_PPC_BOOKE_SREGS);
Greg Kurz6977afd2017-09-14 21:25:43 +0200116 cap_ppc_smt_possible = kvm_vm_check_extension(s, KVM_CAP_PPC_SMT_POSSIBLE);
David Gibson0f5cb292011-09-29 21:39:12 +0000117 cap_spapr_tce = kvm_check_extension(s, KVM_CAP_SPAPR_TCE);
Alexey Kardashevskiyd6ee2a72017-03-10 12:41:13 +1100118 cap_spapr_tce_64 = kvm_check_extension(s, KVM_CAP_SPAPR_TCE_64);
Alexey Kardashevskiyda953242014-05-27 15:36:30 +1000119 cap_spapr_multitce = kvm_check_extension(s, KVM_CAP_SPAPR_MULTITCE);
Alexey Kardashevskiy9ded7802018-02-06 11:08:24 -0700120 cap_spapr_vfio = kvm_vm_check_extension(s, KVM_CAP_SPAPR_TCE_VFIO);
David Gibsond67d40e2013-02-20 16:41:50 +0000121 cap_one_reg = kvm_check_extension(s, KVM_CAP_ONE_REG);
David Gibsonf1af19d2012-09-12 16:57:09 +0000122 cap_hior = kvm_check_extension(s, KVM_CAP_PPC_HIOR);
Stuart Yoder3b961122013-03-30 06:40:49 +0000123 cap_epr = kvm_check_extension(s, KVM_CAP_PPC_EPR);
Bharat Bhushan31f2cb82013-02-24 18:16:21 +0000124 cap_ppc_watchdog = kvm_check_extension(s, KVM_CAP_PPC_BOOKE_WATCHDOG);
David Gibsonc995e942019-03-21 21:47:25 +1100125 /*
126 * Note: we don't set cap_papr here, because this capability is
127 * only activated after this by kvmppc_set_papr()
128 */
Greg Kurz6977afd2017-09-14 21:25:43 +0200129 cap_htab_fd = kvm_vm_check_extension(s, KVM_CAP_PPC_HTAB_FD);
Alexander Graf87a91de2014-06-04 12:14:08 +0200130 cap_fixup_hcalls = kvm_check_extension(s, KVM_CAP_PPC_FIXUP_HCALL);
Sam Bobrofffa98fbf2017-08-18 15:50:22 +1000131 cap_ppc_smt = kvm_vm_check_extension(s, KVM_CAP_PPC_SMT);
Thomas Huthbac3bf22016-09-28 13:16:30 +0200132 cap_htm = kvm_vm_check_extension(s, KVM_CAP_PPC_HTM);
Sam Bobroffcf1c4cc2017-03-20 10:46:44 +1100133 cap_mmu_radix = kvm_vm_check_extension(s, KVM_CAP_PPC_MMU_RADIX);
134 cap_mmu_hash_v3 = kvm_vm_check_extension(s, KVM_CAP_PPC_MMU_HASH_V3);
Cédric Le Goater38afd772019-05-13 10:42:33 +0200135 cap_xive = kvm_vm_check_extension(s, KVM_CAP_PPC_IRQ_XIVE);
David Gibsonb55d2952017-07-12 17:56:55 +1000136 cap_resize_hpt = kvm_vm_check_extension(s, KVM_CAP_SPAPR_RESIZE_HPT);
Suraj Jitindar Singh8acc2ae2018-01-19 15:59:59 +1100137 kvmppc_get_cpu_characteristics(s);
Suraj Jitindar Singhb9a477b2018-10-08 14:25:39 +1100138 cap_ppc_nested_kvm_hv = kvm_vm_check_extension(s, KVM_CAP_PPC_NESTED_HV);
Suraj Jitindar Singh7d050522019-03-01 13:43:16 +1100139 cap_large_decr = kvmppc_get_dec_bits();
Nicholas Pigginec010c02020-03-26 00:29:03 +1000140 cap_fwnmi = kvm_vm_check_extension(s, KVM_CAP_PPC_FWNMI);
Daniel Henrique Barbozac363a372017-08-09 17:43:46 -0300141 /*
142 * Note: setting it to false because there is not such capability
143 * in KVM at this moment.
144 *
145 * TODO: call kvm_vm_check_extension() with the right capability
David Gibsonc995e942019-03-21 21:47:25 +1100146 * after the kernel starts implementing it.
147 */
Daniel Henrique Barbozac363a372017-08-09 17:43:46 -0300148 cap_ppc_pvr_compat = false;
Alexander Graffc87e182010-08-30 13:49:15 +0200149
Shivaprasad G Bhat1e8f51e2019-07-25 09:15:08 -0500150 if (!kvm_check_extension(s, KVM_CAP_PPC_IRQ_LEVEL)) {
151 error_report("KVM: Host kernel doesn't have level irq capability");
152 exit(1);
Alexander Graffc87e182010-08-30 13:49:15 +0200153 }
154
David Gibson165dc3e2019-10-30 17:20:35 +0100155 kvm_ppc_register_host_cpu_type();
Andreas Färber5ba45762013-02-23 11:22:12 +0000156
aurel32d76d1652008-12-16 10:43:58 +0000157 return 0;
158}
159
Paolo Bonzini4376c402019-11-13 11:17:12 +0100160int kvm_arch_irqchip_create(KVMState *s)
Paolo Bonzinid525ffa2016-12-22 16:41:42 +0100161{
162 return 0;
163}
164
Andreas Färber1bc22652012-10-31 06:06:49 +0100165static int kvm_arch_sync_sregs(PowerPCCPU *cpu)
aurel32d76d1652008-12-16 10:43:58 +0000166{
Andreas Färber1bc22652012-10-31 06:06:49 +0100167 CPUPPCState *cenv = &cpu->env;
168 CPUState *cs = CPU(cpu);
Alexander Graf861bbc802009-07-17 13:51:43 +0200169 struct kvm_sregs sregs;
Scott Wood5666ca42011-04-11 18:34:34 -0500170 int ret;
171
172 if (cenv->excp_model == POWERPC_EXCP_BOOKE) {
David Gibsonc995e942019-03-21 21:47:25 +1100173 /*
174 * What we're really trying to say is "if we're on BookE, we
175 * use the native PVR for now". This is the only sane way to
176 * check it though, so we potentially confuse users that they
177 * can run BookE guests on BookS. Let's hope nobody dares
178 * enough :)
179 */
Scott Wood5666ca42011-04-11 18:34:34 -0500180 return 0;
181 } else {
Scott Wood90dc8812011-04-29 17:10:23 -0500182 if (!cap_segstate) {
Alexander Graf64e07be2011-04-16 02:00:36 +0200183 fprintf(stderr, "kvm error: missing PVR setting capability\n");
184 return -ENOSYS;
Scott Wood5666ca42011-04-11 18:34:34 -0500185 }
Scott Wood5666ca42011-04-11 18:34:34 -0500186 }
187
Andreas Färber1bc22652012-10-31 06:06:49 +0100188 ret = kvm_vcpu_ioctl(cs, KVM_GET_SREGS, &sregs);
Scott Wood5666ca42011-04-11 18:34:34 -0500189 if (ret) {
190 return ret;
191 }
Alexander Graf861bbc802009-07-17 13:51:43 +0200192
193 sregs.pvr = cenv->spr[SPR_PVR];
Andreas Färber1bc22652012-10-31 06:06:49 +0100194 return kvm_vcpu_ioctl(cs, KVM_SET_SREGS, &sregs);
Scott Wood5666ca42011-04-11 18:34:34 -0500195}
196
Scott Wood93dd5e82011-08-31 11:26:56 +0000197/* Set up a shared TLB array with KVM */
Andreas Färber1bc22652012-10-31 06:06:49 +0100198static int kvm_booke206_tlb_init(PowerPCCPU *cpu)
Scott Wood93dd5e82011-08-31 11:26:56 +0000199{
Andreas Färber1bc22652012-10-31 06:06:49 +0100200 CPUPPCState *env = &cpu->env;
201 CPUState *cs = CPU(cpu);
Scott Wood93dd5e82011-08-31 11:26:56 +0000202 struct kvm_book3e_206_tlb_params params = {};
203 struct kvm_config_tlb cfg = {};
Scott Wood93dd5e82011-08-31 11:26:56 +0000204 unsigned int entries = 0;
205 int ret, i;
206
207 if (!kvm_enabled() ||
Andreas Färbera60f24b2012-12-01 05:35:08 +0100208 !kvm_check_extension(cs->kvm_state, KVM_CAP_SW_TLB)) {
Scott Wood93dd5e82011-08-31 11:26:56 +0000209 return 0;
210 }
211
212 assert(ARRAY_SIZE(params.tlb_sizes) == BOOKE206_MAX_TLBN);
213
214 for (i = 0; i < BOOKE206_MAX_TLBN; i++) {
215 params.tlb_sizes[i] = booke206_tlb_size(env, i);
216 params.tlb_ways[i] = booke206_tlb_ways(env, i);
217 entries += params.tlb_sizes[i];
218 }
219
220 assert(entries == env->nb_tlb);
221 assert(sizeof(struct kvm_book3e_206_tlb_entry) == sizeof(ppcmas_tlb_t));
222
223 env->tlb_dirty = true;
224
225 cfg.array = (uintptr_t)env->tlb.tlbm;
226 cfg.array_len = sizeof(ppcmas_tlb_t) * entries;
227 cfg.params = (uintptr_t)&params;
228 cfg.mmu_type = KVM_MMU_FSL_BOOKE_NOHV;
229
Cornelia Huck48add812014-04-09 17:21:57 +0200230 ret = kvm_vcpu_enable_cap(cs, KVM_CAP_SW_TLB, 0, (uintptr_t)&cfg);
Scott Wood93dd5e82011-08-31 11:26:56 +0000231 if (ret < 0) {
232 fprintf(stderr, "%s: couldn't enable KVM_CAP_SW_TLB: %s\n",
233 __func__, strerror(-ret));
234 return ret;
235 }
236
237 env->kvm_sw_tlb = true;
238 return 0;
239}
240
Benjamin Herrenschmidt4656e1f2012-06-18 19:56:25 +0000241
242#if defined(TARGET_PPC64)
Greg Kurzab256962018-06-29 11:48:32 +0200243static void kvm_get_smmu_info(struct kvm_ppc_smmu_info *info, Error **errp)
Benjamin Herrenschmidt4656e1f2012-06-18 19:56:25 +0000244{
245 int ret;
246
Greg Kurzab256962018-06-29 11:48:32 +0200247 assert(kvm_state != NULL);
248
249 if (!kvm_check_extension(kvm_state, KVM_CAP_PPC_GET_SMMU_INFO)) {
Greg Kurz71d0f1e2018-06-29 11:48:16 +0200250 error_setg(errp, "KVM doesn't expose the MMU features it supports");
251 error_append_hint(errp, "Consider switching to a newer KVM\n");
252 return;
Benjamin Herrenschmidt4656e1f2012-06-18 19:56:25 +0000253 }
254
Greg Kurzab256962018-06-29 11:48:32 +0200255 ret = kvm_vm_ioctl(kvm_state, KVM_PPC_GET_SMMU_INFO, info);
Greg Kurz71d0f1e2018-06-29 11:48:16 +0200256 if (ret == 0) {
257 return;
258 }
259
260 error_setg_errno(errp, -ret,
261 "KVM failed to provide the MMU features it supports");
Benjamin Herrenschmidt4656e1f2012-06-18 19:56:25 +0000262}
263
Sam Bobroffc64abd12017-03-20 10:46:43 +1100264struct ppc_radix_page_info *kvm_get_radix_page_info(void)
265{
Philippe Mathieu-Daudé4f7f5892020-01-21 12:03:48 +0100266 KVMState *s = KVM_STATE(current_accel());
Sam Bobroffc64abd12017-03-20 10:46:43 +1100267 struct ppc_radix_page_info *radix_page_info;
268 struct kvm_ppc_rmmu_info rmmu_info;
269 int i;
270
271 if (!kvm_check_extension(s, KVM_CAP_PPC_MMU_RADIX)) {
272 return NULL;
273 }
274 if (kvm_vm_ioctl(s, KVM_PPC_GET_RMMU_INFO, &rmmu_info)) {
275 return NULL;
276 }
277 radix_page_info = g_malloc0(sizeof(*radix_page_info));
278 radix_page_info->count = 0;
279 for (i = 0; i < PPC_PAGE_SIZES_MAX_SZ; i++) {
280 if (rmmu_info.ap_encodings[i]) {
281 radix_page_info->entries[i] = rmmu_info.ap_encodings[i];
282 radix_page_info->count++;
283 }
284 }
285 return radix_page_info;
286}
287
Suraj Jitindar Singhb4db5412017-03-20 10:46:46 +1100288target_ulong kvmppc_configure_v3_mmu(PowerPCCPU *cpu,
289 bool radix, bool gtse,
290 uint64_t proc_tbl)
291{
292 CPUState *cs = CPU(cpu);
293 int ret;
294 uint64_t flags = 0;
295 struct kvm_ppc_mmuv3_cfg cfg = {
296 .process_table = proc_tbl,
297 };
298
299 if (radix) {
300 flags |= KVM_PPC_MMUV3_RADIX;
301 }
302 if (gtse) {
303 flags |= KVM_PPC_MMUV3_GTSE;
304 }
305 cfg.flags = flags;
306 ret = kvm_vm_ioctl(cs->kvm_state, KVM_PPC_CONFIGURE_V3_MMU, &cfg);
307 switch (ret) {
308 case 0:
309 return H_SUCCESS;
310 case -EINVAL:
311 return H_PARAMETER;
312 case -ENODEV:
313 return H_NOT_AVAILABLE;
314 default:
315 return H_HARDWARE;
316 }
317}
318
David Gibson24c68632018-06-14 12:11:08 +1000319bool kvmppc_hpt_needs_host_contiguous_pages(void)
320{
David Gibson24c68632018-06-14 12:11:08 +1000321 static struct kvm_ppc_smmu_info smmu_info;
322
323 if (!kvm_enabled()) {
324 return false;
325 }
326
Greg Kurzab256962018-06-29 11:48:32 +0200327 kvm_get_smmu_info(&smmu_info, &error_fatal);
David Gibson24c68632018-06-14 12:11:08 +1000328 return !!(smmu_info.flags & KVM_PPC_PAGE_SIZES_REAL);
329}
330
David Gibsone5ca28e2018-04-16 16:19:52 +1000331void kvm_check_mmu(PowerPCCPU *cpu, Error **errp)
Benjamin Herrenschmidt4656e1f2012-06-18 19:56:25 +0000332{
David Gibsone5ca28e2018-04-16 16:19:52 +1000333 struct kvm_ppc_smmu_info smmu_info;
Benjamin Herrenschmidt4656e1f2012-06-18 19:56:25 +0000334 int iq, ik, jq, jk;
Greg Kurz71d0f1e2018-06-29 11:48:16 +0200335 Error *local_err = NULL;
Benjamin Herrenschmidt4656e1f2012-06-18 19:56:25 +0000336
David Gibsone5ca28e2018-04-16 16:19:52 +1000337 /* For now, we only have anything to check on hash64 MMUs */
338 if (!cpu->hash64_opts || !kvm_enabled()) {
Benjamin Herrenschmidt4656e1f2012-06-18 19:56:25 +0000339 return;
340 }
341
Greg Kurzab256962018-06-29 11:48:32 +0200342 kvm_get_smmu_info(&smmu_info, &local_err);
Greg Kurz71d0f1e2018-06-29 11:48:16 +0200343 if (local_err) {
344 error_propagate(errp, local_err);
345 return;
346 }
David Gibsone5ca28e2018-04-16 16:19:52 +1000347
348 if (ppc_hash64_has(cpu, PPC_HASH64_1TSEG)
349 && !(smmu_info.flags & KVM_PPC_1T_SEGMENTS)) {
350 error_setg(errp,
351 "KVM does not support 1TiB segments which guest expects");
352 return;
Benjamin Herrenschmidt4656e1f2012-06-18 19:56:25 +0000353 }
354
David Gibsone5ca28e2018-04-16 16:19:52 +1000355 if (smmu_info.slb_size < cpu->hash64_opts->slb_size) {
356 error_setg(errp, "KVM only supports %u SLB entries, but guest needs %u",
357 smmu_info.slb_size, cpu->hash64_opts->slb_size);
358 return;
Benjamin Herrenschmidt90da0d52015-10-22 18:30:59 +1100359 }
360
Alexander Graf08215d82014-05-11 18:37:00 +0200361 /*
David Gibsone5ca28e2018-04-16 16:19:52 +1000362 * Verify that every pagesize supported by the cpu model is
363 * supported by KVM with the same encodings
Alexander Graf08215d82014-05-11 18:37:00 +0200364 */
David Gibsone5ca28e2018-04-16 16:19:52 +1000365 for (iq = 0; iq < ARRAY_SIZE(cpu->hash64_opts->sps); iq++) {
David Gibsonb07c59f2018-03-23 13:31:52 +1100366 PPCHash64SegmentPageSizes *qsps = &cpu->hash64_opts->sps[iq];
David Gibsone5ca28e2018-04-16 16:19:52 +1000367 struct kvm_ppc_one_seg_page_size *ksps;
Benjamin Herrenschmidt4656e1f2012-06-18 19:56:25 +0000368
David Gibsone5ca28e2018-04-16 16:19:52 +1000369 for (ik = 0; ik < ARRAY_SIZE(smmu_info.sps); ik++) {
370 if (qsps->page_shift == smmu_info.sps[ik].page_shift) {
Benjamin Herrenschmidt4656e1f2012-06-18 19:56:25 +0000371 break;
372 }
373 }
David Gibsone5ca28e2018-04-16 16:19:52 +1000374 if (ik >= ARRAY_SIZE(smmu_info.sps)) {
375 error_setg(errp, "KVM doesn't support for base page shift %u",
376 qsps->page_shift);
377 return;
378 }
379
380 ksps = &smmu_info.sps[ik];
381 if (ksps->slb_enc != qsps->slb_enc) {
382 error_setg(errp,
383"KVM uses SLB encoding 0x%x for page shift %u, but guest expects 0x%x",
384 ksps->slb_enc, ksps->page_shift, qsps->slb_enc);
385 return;
386 }
387
388 for (jq = 0; jq < ARRAY_SIZE(qsps->enc); jq++) {
389 for (jk = 0; jk < ARRAY_SIZE(ksps->enc); jk++) {
390 if (qsps->enc[jq].page_shift == ksps->enc[jk].page_shift) {
391 break;
392 }
393 }
394
395 if (jk >= ARRAY_SIZE(ksps->enc)) {
396 error_setg(errp, "KVM doesn't support page shift %u/%u",
397 qsps->enc[jq].page_shift, qsps->page_shift);
398 return;
399 }
400 if (qsps->enc[jq].pte_enc != ksps->enc[jk].pte_enc) {
401 error_setg(errp,
402"KVM uses PTE encoding 0x%x for page shift %u/%u, but guest expects 0x%x",
403 ksps->enc[jk].pte_enc, qsps->enc[jq].page_shift,
404 qsps->page_shift, qsps->enc[jq].pte_enc);
405 return;
406 }
Benjamin Herrenschmidt4656e1f2012-06-18 19:56:25 +0000407 }
408 }
David Gibsone5ca28e2018-04-16 16:19:52 +1000409
410 if (ppc_hash64_has(cpu, PPC_HASH64_CI_LARGEPAGE)) {
David Gibsonc995e942019-03-21 21:47:25 +1100411 /*
412 * Mostly what guest pagesizes we can use are related to the
David Gibsone5ca28e2018-04-16 16:19:52 +1000413 * host pages used to map guest RAM, which is handled in the
414 * platform code. Cache-Inhibited largepages (64k) however are
415 * used for I/O, so if they're mapped to the host at all it
416 * will be a normal mapping, not a special hugepage one used
David Gibsonc995e942019-03-21 21:47:25 +1100417 * for RAM.
418 */
Wei Yang038adc22019-10-13 10:11:45 +0800419 if (qemu_real_host_page_size < 0x10000) {
David Gibsone5ca28e2018-04-16 16:19:52 +1000420 error_setg(errp,
421 "KVM can't supply 64kiB CI pages, which guest expects");
422 }
Benjamin Herrenschmidt4656e1f2012-06-18 19:56:25 +0000423 }
424}
Benjamin Herrenschmidt4656e1f2012-06-18 19:56:25 +0000425#endif /* !defined (TARGET_PPC64) */
426
Eduardo Habkostb164e482013-01-22 18:25:01 -0200427unsigned long kvm_arch_vcpu_id(CPUState *cpu)
428{
Sam Bobroff2e886fb2017-08-09 15:38:56 +1000429 return POWERPC_CPU(cpu)->vcpu_id;
Eduardo Habkostb164e482013-01-22 18:25:01 -0200430}
431
David Gibsonc995e942019-03-21 21:47:25 +1100432/*
433 * e500 supports 2 h/w breakpoint and 2 watchpoint. book3s supports
434 * only 1 watchpoint, so array size of 4 is sufficient for now.
Bharat Bhushan88365d12014-07-14 14:45:37 +0530435 */
436#define MAX_HW_BKPTS 4
437
438static struct HWBreakpoint {
439 target_ulong addr;
440 int type;
441} hw_debug_points[MAX_HW_BKPTS];
442
443static CPUWatchpoint hw_watchpoint;
444
445/* Default there is no breakpoint and watchpoint supported */
446static int max_hw_breakpoint;
447static int max_hw_watchpoint;
448static int nb_hw_breakpoint;
449static int nb_hw_watchpoint;
450
451static void kvmppc_hw_debug_points_init(CPUPPCState *cenv)
452{
453 if (cenv->excp_model == POWERPC_EXCP_BOOKE) {
454 max_hw_breakpoint = 2;
455 max_hw_watchpoint = 2;
456 }
457
458 if ((max_hw_breakpoint + max_hw_watchpoint) > MAX_HW_BKPTS) {
459 fprintf(stderr, "Error initializing h/w breakpoints\n");
460 return;
461 }
462}
463
Andreas Färber20d695a2012-10-31 06:57:49 +0100464int kvm_arch_init_vcpu(CPUState *cs)
Scott Wood5666ca42011-04-11 18:34:34 -0500465{
Andreas Färber20d695a2012-10-31 06:57:49 +0100466 PowerPCCPU *cpu = POWERPC_CPU(cs);
467 CPUPPCState *cenv = &cpu->env;
Scott Wood5666ca42011-04-11 18:34:34 -0500468 int ret;
469
Benjamin Herrenschmidt4656e1f2012-06-18 19:56:25 +0000470 /* Synchronize sregs with kvm */
Andreas Färber1bc22652012-10-31 06:06:49 +0100471 ret = kvm_arch_sync_sregs(cpu);
Scott Wood5666ca42011-04-11 18:34:34 -0500472 if (ret) {
Thomas Huth388e47c2016-02-18 22:01:56 +0100473 if (ret == -EINVAL) {
474 error_report("Register sync failed... If you're using kvm-hv.ko,"
475 " only \"-cpu host\" is possible");
476 }
Scott Wood5666ca42011-04-11 18:34:34 -0500477 return ret;
478 }
Alexander Graf861bbc802009-07-17 13:51:43 +0200479
Scott Wood93dd5e82011-08-31 11:26:56 +0000480 switch (cenv->mmu_model) {
481 case POWERPC_MMU_BOOKE206:
Thomas Huth7f516c92016-09-29 12:48:07 +0200482 /* This target supports access to KVM's guest TLB */
Andreas Färber1bc22652012-10-31 06:06:49 +0100483 ret = kvm_booke206_tlb_init(cpu);
Scott Wood93dd5e82011-08-31 11:26:56 +0000484 break;
Thomas Huth7f516c92016-09-29 12:48:07 +0200485 case POWERPC_MMU_2_07:
486 if (!cap_htm && !kvmppc_is_pr(cs->kvm_state)) {
David Gibsonc995e942019-03-21 21:47:25 +1100487 /*
488 * KVM-HV has transactional memory on POWER8 also without
489 * the KVM_CAP_PPC_HTM extension, so enable it here
zhaolichang136fbf62020-10-09 14:44:37 +0800490 * instead as long as it's available to userspace on the
David Gibsonc995e942019-03-21 21:47:25 +1100491 * host.
492 */
Sam Bobrofff3d9f302017-03-29 16:01:28 +1100493 if (qemu_getauxval(AT_HWCAP2) & PPC_FEATURE2_HAS_HTM) {
494 cap_htm = true;
495 }
Thomas Huth7f516c92016-09-29 12:48:07 +0200496 }
497 break;
Scott Wood93dd5e82011-08-31 11:26:56 +0000498 default:
499 break;
500 }
501
Bharat Bhushan3c902d42014-07-14 14:45:35 +0530502 kvm_get_one_reg(cs, KVM_REG_PPC_DEBUG_INST, &debug_inst_opcode);
Bharat Bhushan88365d12014-07-14 14:45:37 +0530503 kvmppc_hw_debug_points_init(cenv);
Bharat Bhushan3c902d42014-07-14 14:45:35 +0530504
Alexander Graf861bbc802009-07-17 13:51:43 +0200505 return ret;
aurel32d76d1652008-12-16 10:43:58 +0000506}
507
Liran Alonb1115c92019-06-19 19:21:32 +0300508int kvm_arch_destroy_vcpu(CPUState *cs)
509{
510 return 0;
511}
512
Andreas Färber1bc22652012-10-31 06:06:49 +0100513static void kvm_sw_tlb_put(PowerPCCPU *cpu)
Scott Wood93dd5e82011-08-31 11:26:56 +0000514{
Andreas Färber1bc22652012-10-31 06:06:49 +0100515 CPUPPCState *env = &cpu->env;
516 CPUState *cs = CPU(cpu);
Scott Wood93dd5e82011-08-31 11:26:56 +0000517 struct kvm_dirty_tlb dirty_tlb;
518 unsigned char *bitmap;
519 int ret;
520
521 if (!env->kvm_sw_tlb) {
522 return;
523 }
524
525 bitmap = g_malloc((env->nb_tlb + 7) / 8);
526 memset(bitmap, 0xFF, (env->nb_tlb + 7) / 8);
527
528 dirty_tlb.bitmap = (uintptr_t)bitmap;
529 dirty_tlb.num_dirty = env->nb_tlb;
530
Andreas Färber1bc22652012-10-31 06:06:49 +0100531 ret = kvm_vcpu_ioctl(cs, KVM_DIRTY_TLB, &dirty_tlb);
Scott Wood93dd5e82011-08-31 11:26:56 +0000532 if (ret) {
533 fprintf(stderr, "%s: KVM_DIRTY_TLB: %s\n",
534 __func__, strerror(-ret));
535 }
536
537 g_free(bitmap);
538}
539
David Gibsond67d40e2013-02-20 16:41:50 +0000540static void kvm_get_one_spr(CPUState *cs, uint64_t id, int spr)
541{
542 PowerPCCPU *cpu = POWERPC_CPU(cs);
543 CPUPPCState *env = &cpu->env;
544 union {
545 uint32_t u32;
546 uint64_t u64;
547 } val;
548 struct kvm_one_reg reg = {
549 .id = id,
550 .addr = (uintptr_t) &val,
551 };
552 int ret;
553
554 ret = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, &reg);
555 if (ret != 0) {
Alexey Kardashevskiyb36f1002014-02-04 15:12:34 +1100556 trace_kvm_failed_spr_get(spr, strerror(errno));
David Gibsond67d40e2013-02-20 16:41:50 +0000557 } else {
558 switch (id & KVM_REG_SIZE_MASK) {
559 case KVM_REG_SIZE_U32:
560 env->spr[spr] = val.u32;
561 break;
562
563 case KVM_REG_SIZE_U64:
564 env->spr[spr] = val.u64;
565 break;
566
567 default:
568 /* Don't handle this size yet */
569 abort();
570 }
571 }
572}
573
574static void kvm_put_one_spr(CPUState *cs, uint64_t id, int spr)
575{
576 PowerPCCPU *cpu = POWERPC_CPU(cs);
577 CPUPPCState *env = &cpu->env;
578 union {
579 uint32_t u32;
580 uint64_t u64;
581 } val;
582 struct kvm_one_reg reg = {
583 .id = id,
584 .addr = (uintptr_t) &val,
585 };
586 int ret;
587
588 switch (id & KVM_REG_SIZE_MASK) {
589 case KVM_REG_SIZE_U32:
590 val.u32 = env->spr[spr];
591 break;
592
593 case KVM_REG_SIZE_U64:
594 val.u64 = env->spr[spr];
595 break;
596
597 default:
598 /* Don't handle this size yet */
599 abort();
600 }
601
602 ret = kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, &reg);
603 if (ret != 0) {
Alexey Kardashevskiyb36f1002014-02-04 15:12:34 +1100604 trace_kvm_failed_spr_set(spr, strerror(errno));
David Gibsond67d40e2013-02-20 16:41:50 +0000605 }
606}
607
David Gibson70b79842013-02-20 16:41:51 +0000608static int kvm_put_fp(CPUState *cs)
609{
610 PowerPCCPU *cpu = POWERPC_CPU(cs);
611 CPUPPCState *env = &cpu->env;
612 struct kvm_one_reg reg;
613 int i;
614 int ret;
615
616 if (env->insns_flags & PPC_FLOAT) {
617 uint64_t fpscr = env->fpscr;
618 bool vsx = !!(env->insns_flags2 & PPC2_VSX);
619
620 reg.id = KVM_REG_PPC_FPSCR;
621 reg.addr = (uintptr_t)&fpscr;
622 ret = kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, &reg);
623 if (ret < 0) {
Greg Kurz8d83cbf2019-04-05 10:05:24 +0200624 trace_kvm_failed_fpscr_set(strerror(errno));
David Gibson70b79842013-02-20 16:41:51 +0000625 return ret;
626 }
627
628 for (i = 0; i < 32; i++) {
629 uint64_t vsr[2];
Mark Cave-Aylandef96e3a2019-01-02 09:14:22 +0000630 uint64_t *fpr = cpu_fpr_ptr(&cpu->env, i);
631 uint64_t *vsrl = cpu_vsrl_ptr(&cpu->env, i);
David Gibson70b79842013-02-20 16:41:51 +0000632
Greg Kurz3a4b7912016-01-15 16:00:12 +0100633#ifdef HOST_WORDS_BIGENDIAN
Mark Cave-Aylandef96e3a2019-01-02 09:14:22 +0000634 vsr[0] = float64_val(*fpr);
635 vsr[1] = *vsrl;
Greg Kurz3a4b7912016-01-15 16:00:12 +0100636#else
Mark Cave-Aylandef96e3a2019-01-02 09:14:22 +0000637 vsr[0] = *vsrl;
638 vsr[1] = float64_val(*fpr);
Greg Kurz3a4b7912016-01-15 16:00:12 +0100639#endif
David Gibson70b79842013-02-20 16:41:51 +0000640 reg.addr = (uintptr_t) &vsr;
641 reg.id = vsx ? KVM_REG_PPC_VSR(i) : KVM_REG_PPC_FPR(i);
642
643 ret = kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, &reg);
644 if (ret < 0) {
Greg Kurz8d83cbf2019-04-05 10:05:24 +0200645 trace_kvm_failed_fp_set(vsx ? "VSR" : "FPR", i,
646 strerror(errno));
David Gibson70b79842013-02-20 16:41:51 +0000647 return ret;
648 }
649 }
650 }
651
652 if (env->insns_flags & PPC_ALTIVEC) {
653 reg.id = KVM_REG_PPC_VSCR;
654 reg.addr = (uintptr_t)&env->vscr;
655 ret = kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, &reg);
656 if (ret < 0) {
Greg Kurz8d83cbf2019-04-05 10:05:24 +0200657 trace_kvm_failed_vscr_set(strerror(errno));
David Gibson70b79842013-02-20 16:41:51 +0000658 return ret;
659 }
660
661 for (i = 0; i < 32; i++) {
662 reg.id = KVM_REG_PPC_VR(i);
Mark Cave-Aylandef96e3a2019-01-02 09:14:22 +0000663 reg.addr = (uintptr_t)cpu_avr_ptr(env, i);
David Gibson70b79842013-02-20 16:41:51 +0000664 ret = kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, &reg);
665 if (ret < 0) {
Greg Kurz8d83cbf2019-04-05 10:05:24 +0200666 trace_kvm_failed_vr_set(i, strerror(errno));
David Gibson70b79842013-02-20 16:41:51 +0000667 return ret;
668 }
669 }
670 }
671
672 return 0;
673}
674
675static int kvm_get_fp(CPUState *cs)
676{
677 PowerPCCPU *cpu = POWERPC_CPU(cs);
678 CPUPPCState *env = &cpu->env;
679 struct kvm_one_reg reg;
680 int i;
681 int ret;
682
683 if (env->insns_flags & PPC_FLOAT) {
684 uint64_t fpscr;
685 bool vsx = !!(env->insns_flags2 & PPC2_VSX);
686
687 reg.id = KVM_REG_PPC_FPSCR;
688 reg.addr = (uintptr_t)&fpscr;
689 ret = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, &reg);
690 if (ret < 0) {
Greg Kurz8d83cbf2019-04-05 10:05:24 +0200691 trace_kvm_failed_fpscr_get(strerror(errno));
David Gibson70b79842013-02-20 16:41:51 +0000692 return ret;
693 } else {
694 env->fpscr = fpscr;
695 }
696
697 for (i = 0; i < 32; i++) {
698 uint64_t vsr[2];
Mark Cave-Aylandef96e3a2019-01-02 09:14:22 +0000699 uint64_t *fpr = cpu_fpr_ptr(&cpu->env, i);
700 uint64_t *vsrl = cpu_vsrl_ptr(&cpu->env, i);
David Gibson70b79842013-02-20 16:41:51 +0000701
702 reg.addr = (uintptr_t) &vsr;
703 reg.id = vsx ? KVM_REG_PPC_VSR(i) : KVM_REG_PPC_FPR(i);
704
705 ret = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, &reg);
706 if (ret < 0) {
Greg Kurz8d83cbf2019-04-05 10:05:24 +0200707 trace_kvm_failed_fp_get(vsx ? "VSR" : "FPR", i,
708 strerror(errno));
David Gibson70b79842013-02-20 16:41:51 +0000709 return ret;
710 } else {
Greg Kurz3a4b7912016-01-15 16:00:12 +0100711#ifdef HOST_WORDS_BIGENDIAN
Mark Cave-Aylandef96e3a2019-01-02 09:14:22 +0000712 *fpr = vsr[0];
David Gibson70b79842013-02-20 16:41:51 +0000713 if (vsx) {
Mark Cave-Aylandef96e3a2019-01-02 09:14:22 +0000714 *vsrl = vsr[1];
David Gibson70b79842013-02-20 16:41:51 +0000715 }
Greg Kurz3a4b7912016-01-15 16:00:12 +0100716#else
Mark Cave-Aylandef96e3a2019-01-02 09:14:22 +0000717 *fpr = vsr[1];
Greg Kurz3a4b7912016-01-15 16:00:12 +0100718 if (vsx) {
Mark Cave-Aylandef96e3a2019-01-02 09:14:22 +0000719 *vsrl = vsr[0];
Greg Kurz3a4b7912016-01-15 16:00:12 +0100720 }
721#endif
David Gibson70b79842013-02-20 16:41:51 +0000722 }
723 }
724 }
725
726 if (env->insns_flags & PPC_ALTIVEC) {
727 reg.id = KVM_REG_PPC_VSCR;
728 reg.addr = (uintptr_t)&env->vscr;
729 ret = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, &reg);
730 if (ret < 0) {
Greg Kurz8d83cbf2019-04-05 10:05:24 +0200731 trace_kvm_failed_vscr_get(strerror(errno));
David Gibson70b79842013-02-20 16:41:51 +0000732 return ret;
733 }
734
735 for (i = 0; i < 32; i++) {
736 reg.id = KVM_REG_PPC_VR(i);
Mark Cave-Aylandef96e3a2019-01-02 09:14:22 +0000737 reg.addr = (uintptr_t)cpu_avr_ptr(env, i);
David Gibson70b79842013-02-20 16:41:51 +0000738 ret = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, &reg);
739 if (ret < 0) {
Greg Kurz8d83cbf2019-04-05 10:05:24 +0200740 trace_kvm_failed_vr_get(i, strerror(errno));
David Gibson70b79842013-02-20 16:41:51 +0000741 return ret;
742 }
743 }
744 }
745
746 return 0;
747}
748
David Gibson9b00ea42013-04-07 19:08:22 +0000749#if defined(TARGET_PPC64)
750static int kvm_get_vpa(CPUState *cs)
751{
752 PowerPCCPU *cpu = POWERPC_CPU(cs);
David Gibsonce2918c2019-03-06 15:35:37 +1100753 SpaprCpuState *spapr_cpu = spapr_cpu_state(cpu);
David Gibson9b00ea42013-04-07 19:08:22 +0000754 struct kvm_one_reg reg;
755 int ret;
756
757 reg.id = KVM_REG_PPC_VPA_ADDR;
David Gibson7388efa2018-06-13 16:22:18 +1000758 reg.addr = (uintptr_t)&spapr_cpu->vpa_addr;
David Gibson9b00ea42013-04-07 19:08:22 +0000759 ret = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, &reg);
760 if (ret < 0) {
Greg Kurz8d83cbf2019-04-05 10:05:24 +0200761 trace_kvm_failed_vpa_addr_get(strerror(errno));
David Gibson9b00ea42013-04-07 19:08:22 +0000762 return ret;
763 }
764
David Gibson7388efa2018-06-13 16:22:18 +1000765 assert((uintptr_t)&spapr_cpu->slb_shadow_size
766 == ((uintptr_t)&spapr_cpu->slb_shadow_addr + 8));
David Gibson9b00ea42013-04-07 19:08:22 +0000767 reg.id = KVM_REG_PPC_VPA_SLB;
David Gibson7388efa2018-06-13 16:22:18 +1000768 reg.addr = (uintptr_t)&spapr_cpu->slb_shadow_addr;
David Gibson9b00ea42013-04-07 19:08:22 +0000769 ret = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, &reg);
770 if (ret < 0) {
Greg Kurz8d83cbf2019-04-05 10:05:24 +0200771 trace_kvm_failed_slb_get(strerror(errno));
David Gibson9b00ea42013-04-07 19:08:22 +0000772 return ret;
773 }
774
David Gibson7388efa2018-06-13 16:22:18 +1000775 assert((uintptr_t)&spapr_cpu->dtl_size
776 == ((uintptr_t)&spapr_cpu->dtl_addr + 8));
David Gibson9b00ea42013-04-07 19:08:22 +0000777 reg.id = KVM_REG_PPC_VPA_DTL;
David Gibson7388efa2018-06-13 16:22:18 +1000778 reg.addr = (uintptr_t)&spapr_cpu->dtl_addr;
David Gibson9b00ea42013-04-07 19:08:22 +0000779 ret = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, &reg);
780 if (ret < 0) {
Greg Kurz8d83cbf2019-04-05 10:05:24 +0200781 trace_kvm_failed_dtl_get(strerror(errno));
David Gibson9b00ea42013-04-07 19:08:22 +0000782 return ret;
783 }
784
785 return 0;
786}
787
788static int kvm_put_vpa(CPUState *cs)
789{
790 PowerPCCPU *cpu = POWERPC_CPU(cs);
David Gibsonce2918c2019-03-06 15:35:37 +1100791 SpaprCpuState *spapr_cpu = spapr_cpu_state(cpu);
David Gibson9b00ea42013-04-07 19:08:22 +0000792 struct kvm_one_reg reg;
793 int ret;
794
David Gibsonc995e942019-03-21 21:47:25 +1100795 /*
796 * SLB shadow or DTL can't be registered unless a master VPA is
David Gibson9b00ea42013-04-07 19:08:22 +0000797 * registered. That means when restoring state, if a VPA *is*
798 * registered, we need to set that up first. If not, we need to
David Gibsonc995e942019-03-21 21:47:25 +1100799 * deregister the others before deregistering the master VPA
800 */
David Gibson7388efa2018-06-13 16:22:18 +1000801 assert(spapr_cpu->vpa_addr
802 || !(spapr_cpu->slb_shadow_addr || spapr_cpu->dtl_addr));
David Gibson9b00ea42013-04-07 19:08:22 +0000803
David Gibson7388efa2018-06-13 16:22:18 +1000804 if (spapr_cpu->vpa_addr) {
David Gibson9b00ea42013-04-07 19:08:22 +0000805 reg.id = KVM_REG_PPC_VPA_ADDR;
David Gibson7388efa2018-06-13 16:22:18 +1000806 reg.addr = (uintptr_t)&spapr_cpu->vpa_addr;
David Gibson9b00ea42013-04-07 19:08:22 +0000807 ret = kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, &reg);
808 if (ret < 0) {
Greg Kurz8d83cbf2019-04-05 10:05:24 +0200809 trace_kvm_failed_vpa_addr_set(strerror(errno));
David Gibson9b00ea42013-04-07 19:08:22 +0000810 return ret;
811 }
812 }
813
David Gibson7388efa2018-06-13 16:22:18 +1000814 assert((uintptr_t)&spapr_cpu->slb_shadow_size
815 == ((uintptr_t)&spapr_cpu->slb_shadow_addr + 8));
David Gibson9b00ea42013-04-07 19:08:22 +0000816 reg.id = KVM_REG_PPC_VPA_SLB;
David Gibson7388efa2018-06-13 16:22:18 +1000817 reg.addr = (uintptr_t)&spapr_cpu->slb_shadow_addr;
David Gibson9b00ea42013-04-07 19:08:22 +0000818 ret = kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, &reg);
819 if (ret < 0) {
Greg Kurz8d83cbf2019-04-05 10:05:24 +0200820 trace_kvm_failed_slb_set(strerror(errno));
David Gibson9b00ea42013-04-07 19:08:22 +0000821 return ret;
822 }
823
David Gibson7388efa2018-06-13 16:22:18 +1000824 assert((uintptr_t)&spapr_cpu->dtl_size
825 == ((uintptr_t)&spapr_cpu->dtl_addr + 8));
David Gibson9b00ea42013-04-07 19:08:22 +0000826 reg.id = KVM_REG_PPC_VPA_DTL;
David Gibson7388efa2018-06-13 16:22:18 +1000827 reg.addr = (uintptr_t)&spapr_cpu->dtl_addr;
David Gibson9b00ea42013-04-07 19:08:22 +0000828 ret = kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, &reg);
829 if (ret < 0) {
Greg Kurz8d83cbf2019-04-05 10:05:24 +0200830 trace_kvm_failed_dtl_set(strerror(errno));
David Gibson9b00ea42013-04-07 19:08:22 +0000831 return ret;
832 }
833
David Gibson7388efa2018-06-13 16:22:18 +1000834 if (!spapr_cpu->vpa_addr) {
David Gibson9b00ea42013-04-07 19:08:22 +0000835 reg.id = KVM_REG_PPC_VPA_ADDR;
David Gibson7388efa2018-06-13 16:22:18 +1000836 reg.addr = (uintptr_t)&spapr_cpu->vpa_addr;
David Gibson9b00ea42013-04-07 19:08:22 +0000837 ret = kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, &reg);
838 if (ret < 0) {
Greg Kurz8d83cbf2019-04-05 10:05:24 +0200839 trace_kvm_failed_null_vpa_addr_set(strerror(errno));
David Gibson9b00ea42013-04-07 19:08:22 +0000840 return ret;
841 }
842 }
843
844 return 0;
845}
846#endif /* TARGET_PPC64 */
847
David Gibsone5c0d3c2016-03-08 11:33:46 +1100848int kvmppc_put_books_sregs(PowerPCCPU *cpu)
David Gibsona7a00a72016-03-09 11:58:33 +1100849{
850 CPUPPCState *env = &cpu->env;
851 struct kvm_sregs sregs;
852 int i;
853
854 sregs.pvr = env->spr[SPR_PVR];
855
Greg Kurz1ec26c72017-09-25 13:00:02 +0200856 if (cpu->vhyp) {
857 PPCVirtualHypervisorClass *vhc =
858 PPC_VIRTUAL_HYPERVISOR_GET_CLASS(cpu->vhyp);
859 sregs.u.s.sdr1 = vhc->encode_hpt_for_kvm_pr(cpu->vhyp);
860 } else {
861 sregs.u.s.sdr1 = env->spr[SPR_SDR1];
862 }
David Gibsona7a00a72016-03-09 11:58:33 +1100863
864 /* Sync SLB */
865#ifdef TARGET_PPC64
866 for (i = 0; i < ARRAY_SIZE(env->slb); i++) {
867 sregs.u.s.ppc64.slb[i].slbe = env->slb[i].esid;
868 if (env->slb[i].esid & SLB_ESID_V) {
869 sregs.u.s.ppc64.slb[i].slbe |= i;
870 }
871 sregs.u.s.ppc64.slb[i].slbv = env->slb[i].vsid;
872 }
873#endif
874
875 /* Sync SRs */
876 for (i = 0; i < 16; i++) {
877 sregs.u.s.ppc32.sr[i] = env->sr[i];
878 }
879
880 /* Sync BATs */
881 for (i = 0; i < 8; i++) {
882 /* Beware. We have to swap upper and lower bits here */
883 sregs.u.s.ppc32.dbat[i] = ((uint64_t)env->DBAT[0][i] << 32)
884 | env->DBAT[1][i];
885 sregs.u.s.ppc32.ibat[i] = ((uint64_t)env->IBAT[0][i] << 32)
886 | env->IBAT[1][i];
887 }
888
889 return kvm_vcpu_ioctl(CPU(cpu), KVM_SET_SREGS, &sregs);
890}
891
Andreas Färber20d695a2012-10-31 06:57:49 +0100892int kvm_arch_put_registers(CPUState *cs, int level)
aurel32d76d1652008-12-16 10:43:58 +0000893{
Andreas Färber20d695a2012-10-31 06:57:49 +0100894 PowerPCCPU *cpu = POWERPC_CPU(cs);
895 CPUPPCState *env = &cpu->env;
aurel32d76d1652008-12-16 10:43:58 +0000896 struct kvm_regs regs;
897 int ret;
898 int i;
899
Andreas Färber1bc22652012-10-31 06:06:49 +0100900 ret = kvm_vcpu_ioctl(cs, KVM_GET_REGS, &regs);
901 if (ret < 0) {
aurel32d76d1652008-12-16 10:43:58 +0000902 return ret;
Andreas Färber1bc22652012-10-31 06:06:49 +0100903 }
aurel32d76d1652008-12-16 10:43:58 +0000904
905 regs.ctr = env->ctr;
906 regs.lr = env->lr;
Richard Hendersonda91a002013-02-19 23:52:13 -0800907 regs.xer = cpu_read_xer(env);
aurel32d76d1652008-12-16 10:43:58 +0000908 regs.msr = env->msr;
909 regs.pc = env->nip;
910
911 regs.srr0 = env->spr[SPR_SRR0];
912 regs.srr1 = env->spr[SPR_SRR1];
913
914 regs.sprg0 = env->spr[SPR_SPRG0];
915 regs.sprg1 = env->spr[SPR_SPRG1];
916 regs.sprg2 = env->spr[SPR_SPRG2];
917 regs.sprg3 = env->spr[SPR_SPRG3];
918 regs.sprg4 = env->spr[SPR_SPRG4];
919 regs.sprg5 = env->spr[SPR_SPRG5];
920 regs.sprg6 = env->spr[SPR_SPRG6];
921 regs.sprg7 = env->spr[SPR_SPRG7];
922
Scott Wood90dc8812011-04-29 17:10:23 -0500923 regs.pid = env->spr[SPR_BOOKE_PID];
924
David Gibsonc995e942019-03-21 21:47:25 +1100925 for (i = 0; i < 32; i++) {
aurel32d76d1652008-12-16 10:43:58 +0000926 regs.gpr[i] = env->gpr[i];
David Gibsonc995e942019-03-21 21:47:25 +1100927 }
aurel32d76d1652008-12-16 10:43:58 +0000928
Alexey Kardashevskiy4bddaf52013-06-15 11:51:51 +1000929 regs.cr = 0;
930 for (i = 0; i < 8; i++) {
931 regs.cr |= (env->crf[i] & 15) << (4 * (7 - i));
932 }
933
Andreas Färber1bc22652012-10-31 06:06:49 +0100934 ret = kvm_vcpu_ioctl(cs, KVM_SET_REGS, &regs);
David Gibsonc995e942019-03-21 21:47:25 +1100935 if (ret < 0) {
aurel32d76d1652008-12-16 10:43:58 +0000936 return ret;
David Gibsonc995e942019-03-21 21:47:25 +1100937 }
aurel32d76d1652008-12-16 10:43:58 +0000938
David Gibson70b79842013-02-20 16:41:51 +0000939 kvm_put_fp(cs);
940
Scott Wood93dd5e82011-08-31 11:26:56 +0000941 if (env->tlb_dirty) {
Andreas Färber1bc22652012-10-31 06:06:49 +0100942 kvm_sw_tlb_put(cpu);
Scott Wood93dd5e82011-08-31 11:26:56 +0000943 env->tlb_dirty = false;
944 }
945
David Gibsonf1af19d2012-09-12 16:57:09 +0000946 if (cap_segstate && (level >= KVM_PUT_RESET_STATE)) {
David Gibsona7a00a72016-03-09 11:58:33 +1100947 ret = kvmppc_put_books_sregs(cpu);
948 if (ret < 0) {
David Gibsonf1af19d2012-09-12 16:57:09 +0000949 return ret;
950 }
951 }
952
953 if (cap_hior && (level >= KVM_PUT_RESET_STATE)) {
David Gibsond67d40e2013-02-20 16:41:50 +0000954 kvm_put_one_spr(cs, KVM_REG_PPC_HIOR, SPR_HIOR);
955 }
David Gibsonf1af19d2012-09-12 16:57:09 +0000956
David Gibsond67d40e2013-02-20 16:41:50 +0000957 if (cap_one_reg) {
958 int i;
959
David Gibsonc995e942019-03-21 21:47:25 +1100960 /*
961 * We deliberately ignore errors here, for kernels which have
David Gibsond67d40e2013-02-20 16:41:50 +0000962 * the ONE_REG calls, but don't support the specific
963 * registers, there's a reasonable chance things will still
David Gibsonc995e942019-03-21 21:47:25 +1100964 * work, at least until we try to migrate.
965 */
David Gibsond67d40e2013-02-20 16:41:50 +0000966 for (i = 0; i < 1024; i++) {
967 uint64_t id = env->spr_cb[i].one_reg_id;
968
969 if (id != 0) {
970 kvm_put_one_spr(cs, id, i);
971 }
David Gibsonf1af19d2012-09-12 16:57:09 +0000972 }
David Gibson9b00ea42013-04-07 19:08:22 +0000973
974#ifdef TARGET_PPC64
Alexey Kardashevskiy80b3f792014-06-04 22:51:00 +1000975 if (msr_ts) {
976 for (i = 0; i < ARRAY_SIZE(env->tm_gpr); i++) {
977 kvm_set_one_reg(cs, KVM_REG_PPC_TM_GPR(i), &env->tm_gpr[i]);
978 }
979 for (i = 0; i < ARRAY_SIZE(env->tm_vsr); i++) {
980 kvm_set_one_reg(cs, KVM_REG_PPC_TM_VSR(i), &env->tm_vsr[i]);
981 }
982 kvm_set_one_reg(cs, KVM_REG_PPC_TM_CR, &env->tm_cr);
983 kvm_set_one_reg(cs, KVM_REG_PPC_TM_LR, &env->tm_lr);
984 kvm_set_one_reg(cs, KVM_REG_PPC_TM_CTR, &env->tm_ctr);
985 kvm_set_one_reg(cs, KVM_REG_PPC_TM_FPSCR, &env->tm_fpscr);
986 kvm_set_one_reg(cs, KVM_REG_PPC_TM_AMR, &env->tm_amr);
987 kvm_set_one_reg(cs, KVM_REG_PPC_TM_PPR, &env->tm_ppr);
988 kvm_set_one_reg(cs, KVM_REG_PPC_TM_VRSAVE, &env->tm_vrsave);
989 kvm_set_one_reg(cs, KVM_REG_PPC_TM_VSCR, &env->tm_vscr);
990 kvm_set_one_reg(cs, KVM_REG_PPC_TM_DSCR, &env->tm_dscr);
991 kvm_set_one_reg(cs, KVM_REG_PPC_TM_TAR, &env->tm_tar);
992 }
993
David Gibson9b00ea42013-04-07 19:08:22 +0000994 if (cap_papr) {
995 if (kvm_put_vpa(cs) < 0) {
Greg Kurz8d83cbf2019-04-05 10:05:24 +0200996 trace_kvm_failed_put_vpa();
David Gibson9b00ea42013-04-07 19:08:22 +0000997 }
998 }
Alexey Kardashevskiy98a8b522014-05-01 20:37:09 +1000999
1000 kvm_set_one_reg(cs, KVM_REG_PPC_TB_OFFSET, &env->tb_env->tb_offset);
Alexey Kardashevskiy972bd572019-09-23 18:41:10 +10001001
1002 if (level > KVM_PUT_RUNTIME_STATE) {
1003 kvm_put_one_spr(cs, KVM_REG_PPC_DPDES, SPR_DPDES);
1004 }
David Gibson9b00ea42013-04-07 19:08:22 +00001005#endif /* TARGET_PPC64 */
David Gibsonf1af19d2012-09-12 16:57:09 +00001006 }
1007
aurel32d76d1652008-12-16 10:43:58 +00001008 return ret;
1009}
1010
Bharat Bhushanc371c2e2014-07-14 14:45:36 +05301011static void kvm_sync_excp(CPUPPCState *env, int vector, int ivor)
1012{
1013 env->excp_vectors[vector] = env->spr[ivor] + env->spr[SPR_BOOKE_IVPR];
1014}
1015
David Gibsona7a00a72016-03-09 11:58:33 +11001016static int kvmppc_get_booke_sregs(PowerPCCPU *cpu)
1017{
1018 CPUPPCState *env = &cpu->env;
1019 struct kvm_sregs sregs;
1020 int ret;
1021
1022 ret = kvm_vcpu_ioctl(CPU(cpu), KVM_GET_SREGS, &sregs);
1023 if (ret < 0) {
1024 return ret;
1025 }
1026
1027 if (sregs.u.e.features & KVM_SREGS_E_BASE) {
1028 env->spr[SPR_BOOKE_CSRR0] = sregs.u.e.csrr0;
1029 env->spr[SPR_BOOKE_CSRR1] = sregs.u.e.csrr1;
1030 env->spr[SPR_BOOKE_ESR] = sregs.u.e.esr;
1031 env->spr[SPR_BOOKE_DEAR] = sregs.u.e.dear;
1032 env->spr[SPR_BOOKE_MCSR] = sregs.u.e.mcsr;
1033 env->spr[SPR_BOOKE_TSR] = sregs.u.e.tsr;
1034 env->spr[SPR_BOOKE_TCR] = sregs.u.e.tcr;
1035 env->spr[SPR_DECR] = sregs.u.e.dec;
1036 env->spr[SPR_TBL] = sregs.u.e.tb & 0xffffffff;
1037 env->spr[SPR_TBU] = sregs.u.e.tb >> 32;
1038 env->spr[SPR_VRSAVE] = sregs.u.e.vrsave;
1039 }
1040
1041 if (sregs.u.e.features & KVM_SREGS_E_ARCH206) {
1042 env->spr[SPR_BOOKE_PIR] = sregs.u.e.pir;
1043 env->spr[SPR_BOOKE_MCSRR0] = sregs.u.e.mcsrr0;
1044 env->spr[SPR_BOOKE_MCSRR1] = sregs.u.e.mcsrr1;
1045 env->spr[SPR_BOOKE_DECAR] = sregs.u.e.decar;
1046 env->spr[SPR_BOOKE_IVPR] = sregs.u.e.ivpr;
1047 }
1048
1049 if (sregs.u.e.features & KVM_SREGS_E_64) {
1050 env->spr[SPR_BOOKE_EPCR] = sregs.u.e.epcr;
1051 }
1052
1053 if (sregs.u.e.features & KVM_SREGS_E_SPRG8) {
1054 env->spr[SPR_BOOKE_SPRG8] = sregs.u.e.sprg8;
1055 }
1056
1057 if (sregs.u.e.features & KVM_SREGS_E_IVOR) {
1058 env->spr[SPR_BOOKE_IVOR0] = sregs.u.e.ivor_low[0];
1059 kvm_sync_excp(env, POWERPC_EXCP_CRITICAL, SPR_BOOKE_IVOR0);
1060 env->spr[SPR_BOOKE_IVOR1] = sregs.u.e.ivor_low[1];
1061 kvm_sync_excp(env, POWERPC_EXCP_MCHECK, SPR_BOOKE_IVOR1);
1062 env->spr[SPR_BOOKE_IVOR2] = sregs.u.e.ivor_low[2];
1063 kvm_sync_excp(env, POWERPC_EXCP_DSI, SPR_BOOKE_IVOR2);
1064 env->spr[SPR_BOOKE_IVOR3] = sregs.u.e.ivor_low[3];
1065 kvm_sync_excp(env, POWERPC_EXCP_ISI, SPR_BOOKE_IVOR3);
1066 env->spr[SPR_BOOKE_IVOR4] = sregs.u.e.ivor_low[4];
1067 kvm_sync_excp(env, POWERPC_EXCP_EXTERNAL, SPR_BOOKE_IVOR4);
1068 env->spr[SPR_BOOKE_IVOR5] = sregs.u.e.ivor_low[5];
1069 kvm_sync_excp(env, POWERPC_EXCP_ALIGN, SPR_BOOKE_IVOR5);
1070 env->spr[SPR_BOOKE_IVOR6] = sregs.u.e.ivor_low[6];
1071 kvm_sync_excp(env, POWERPC_EXCP_PROGRAM, SPR_BOOKE_IVOR6);
1072 env->spr[SPR_BOOKE_IVOR7] = sregs.u.e.ivor_low[7];
1073 kvm_sync_excp(env, POWERPC_EXCP_FPU, SPR_BOOKE_IVOR7);
1074 env->spr[SPR_BOOKE_IVOR8] = sregs.u.e.ivor_low[8];
1075 kvm_sync_excp(env, POWERPC_EXCP_SYSCALL, SPR_BOOKE_IVOR8);
1076 env->spr[SPR_BOOKE_IVOR9] = sregs.u.e.ivor_low[9];
1077 kvm_sync_excp(env, POWERPC_EXCP_APU, SPR_BOOKE_IVOR9);
1078 env->spr[SPR_BOOKE_IVOR10] = sregs.u.e.ivor_low[10];
1079 kvm_sync_excp(env, POWERPC_EXCP_DECR, SPR_BOOKE_IVOR10);
1080 env->spr[SPR_BOOKE_IVOR11] = sregs.u.e.ivor_low[11];
1081 kvm_sync_excp(env, POWERPC_EXCP_FIT, SPR_BOOKE_IVOR11);
1082 env->spr[SPR_BOOKE_IVOR12] = sregs.u.e.ivor_low[12];
1083 kvm_sync_excp(env, POWERPC_EXCP_WDT, SPR_BOOKE_IVOR12);
1084 env->spr[SPR_BOOKE_IVOR13] = sregs.u.e.ivor_low[13];
1085 kvm_sync_excp(env, POWERPC_EXCP_DTLB, SPR_BOOKE_IVOR13);
1086 env->spr[SPR_BOOKE_IVOR14] = sregs.u.e.ivor_low[14];
1087 kvm_sync_excp(env, POWERPC_EXCP_ITLB, SPR_BOOKE_IVOR14);
1088 env->spr[SPR_BOOKE_IVOR15] = sregs.u.e.ivor_low[15];
1089 kvm_sync_excp(env, POWERPC_EXCP_DEBUG, SPR_BOOKE_IVOR15);
1090
1091 if (sregs.u.e.features & KVM_SREGS_E_SPE) {
1092 env->spr[SPR_BOOKE_IVOR32] = sregs.u.e.ivor_high[0];
1093 kvm_sync_excp(env, POWERPC_EXCP_SPEU, SPR_BOOKE_IVOR32);
1094 env->spr[SPR_BOOKE_IVOR33] = sregs.u.e.ivor_high[1];
1095 kvm_sync_excp(env, POWERPC_EXCP_EFPDI, SPR_BOOKE_IVOR33);
1096 env->spr[SPR_BOOKE_IVOR34] = sregs.u.e.ivor_high[2];
1097 kvm_sync_excp(env, POWERPC_EXCP_EFPRI, SPR_BOOKE_IVOR34);
1098 }
1099
1100 if (sregs.u.e.features & KVM_SREGS_E_PM) {
1101 env->spr[SPR_BOOKE_IVOR35] = sregs.u.e.ivor_high[3];
1102 kvm_sync_excp(env, POWERPC_EXCP_EPERFM, SPR_BOOKE_IVOR35);
1103 }
1104
1105 if (sregs.u.e.features & KVM_SREGS_E_PC) {
1106 env->spr[SPR_BOOKE_IVOR36] = sregs.u.e.ivor_high[4];
1107 kvm_sync_excp(env, POWERPC_EXCP_DOORI, SPR_BOOKE_IVOR36);
1108 env->spr[SPR_BOOKE_IVOR37] = sregs.u.e.ivor_high[5];
1109 kvm_sync_excp(env, POWERPC_EXCP_DOORCI, SPR_BOOKE_IVOR37);
1110 }
1111 }
1112
1113 if (sregs.u.e.features & KVM_SREGS_E_ARCH206_MMU) {
1114 env->spr[SPR_BOOKE_MAS0] = sregs.u.e.mas0;
1115 env->spr[SPR_BOOKE_MAS1] = sregs.u.e.mas1;
1116 env->spr[SPR_BOOKE_MAS2] = sregs.u.e.mas2;
1117 env->spr[SPR_BOOKE_MAS3] = sregs.u.e.mas7_3 & 0xffffffff;
1118 env->spr[SPR_BOOKE_MAS4] = sregs.u.e.mas4;
1119 env->spr[SPR_BOOKE_MAS6] = sregs.u.e.mas6;
1120 env->spr[SPR_BOOKE_MAS7] = sregs.u.e.mas7_3 >> 32;
1121 env->spr[SPR_MMUCFG] = sregs.u.e.mmucfg;
1122 env->spr[SPR_BOOKE_TLB0CFG] = sregs.u.e.tlbcfg[0];
1123 env->spr[SPR_BOOKE_TLB1CFG] = sregs.u.e.tlbcfg[1];
1124 }
1125
1126 if (sregs.u.e.features & KVM_SREGS_EXP) {
1127 env->spr[SPR_BOOKE_EPR] = sregs.u.e.epr;
1128 }
1129
1130 if (sregs.u.e.features & KVM_SREGS_E_PD) {
1131 env->spr[SPR_BOOKE_EPLC] = sregs.u.e.eplc;
1132 env->spr[SPR_BOOKE_EPSC] = sregs.u.e.epsc;
1133 }
1134
1135 if (sregs.u.e.impl_id == KVM_SREGS_E_IMPL_FSL) {
1136 env->spr[SPR_E500_SVR] = sregs.u.e.impl.fsl.svr;
1137 env->spr[SPR_Exxx_MCAR] = sregs.u.e.impl.fsl.mcar;
1138 env->spr[SPR_HID0] = sregs.u.e.impl.fsl.hid0;
1139
1140 if (sregs.u.e.impl.fsl.features & KVM_SREGS_E_FSL_PIDn) {
1141 env->spr[SPR_BOOKE_PID1] = sregs.u.e.impl.fsl.pid1;
1142 env->spr[SPR_BOOKE_PID2] = sregs.u.e.impl.fsl.pid2;
1143 }
1144 }
1145
1146 return 0;
1147}
1148
1149static int kvmppc_get_books_sregs(PowerPCCPU *cpu)
1150{
1151 CPUPPCState *env = &cpu->env;
1152 struct kvm_sregs sregs;
1153 int ret;
1154 int i;
1155
1156 ret = kvm_vcpu_ioctl(CPU(cpu), KVM_GET_SREGS, &sregs);
1157 if (ret < 0) {
1158 return ret;
1159 }
1160
David Gibsone57ca752017-02-23 11:39:18 +11001161 if (!cpu->vhyp) {
David Gibsona7a00a72016-03-09 11:58:33 +11001162 ppc_store_sdr1(env, sregs.u.s.sdr1);
1163 }
1164
1165 /* Sync SLB */
1166#ifdef TARGET_PPC64
1167 /*
1168 * The packed SLB array we get from KVM_GET_SREGS only contains
1169 * information about valid entries. So we flush our internal copy
1170 * to get rid of stale ones, then put all valid SLB entries back
1171 * in.
1172 */
1173 memset(env->slb, 0, sizeof(env->slb));
1174 for (i = 0; i < ARRAY_SIZE(env->slb); i++) {
1175 target_ulong rb = sregs.u.s.ppc64.slb[i].slbe;
1176 target_ulong rs = sregs.u.s.ppc64.slb[i].slbv;
1177 /*
1178 * Only restore valid entries
1179 */
1180 if (rb & SLB_ESID_V) {
1181 ppc_store_slb(cpu, rb & 0xfff, rb & ~0xfffULL, rs);
1182 }
1183 }
1184#endif
1185
1186 /* Sync SRs */
1187 for (i = 0; i < 16; i++) {
1188 env->sr[i] = sregs.u.s.ppc32.sr[i];
1189 }
1190
1191 /* Sync BATs */
1192 for (i = 0; i < 8; i++) {
1193 env->DBAT[0][i] = sregs.u.s.ppc32.dbat[i] & 0xffffffff;
1194 env->DBAT[1][i] = sregs.u.s.ppc32.dbat[i] >> 32;
1195 env->IBAT[0][i] = sregs.u.s.ppc32.ibat[i] & 0xffffffff;
1196 env->IBAT[1][i] = sregs.u.s.ppc32.ibat[i] >> 32;
1197 }
1198
1199 return 0;
1200}
1201
Andreas Färber20d695a2012-10-31 06:57:49 +01001202int kvm_arch_get_registers(CPUState *cs)
aurel32d76d1652008-12-16 10:43:58 +00001203{
Andreas Färber20d695a2012-10-31 06:57:49 +01001204 PowerPCCPU *cpu = POWERPC_CPU(cs);
1205 CPUPPCState *env = &cpu->env;
aurel32d76d1652008-12-16 10:43:58 +00001206 struct kvm_regs regs;
Scott Wood90dc8812011-04-29 17:10:23 -05001207 uint32_t cr;
Alexander Graf138b38b2010-11-25 08:20:46 +01001208 int i, ret;
aurel32d76d1652008-12-16 10:43:58 +00001209
Andreas Färber1bc22652012-10-31 06:06:49 +01001210 ret = kvm_vcpu_ioctl(cs, KVM_GET_REGS, &regs);
David Gibsonc995e942019-03-21 21:47:25 +11001211 if (ret < 0) {
aurel32d76d1652008-12-16 10:43:58 +00001212 return ret;
David Gibsonc995e942019-03-21 21:47:25 +11001213 }
aurel32d76d1652008-12-16 10:43:58 +00001214
Scott Wood90dc8812011-04-29 17:10:23 -05001215 cr = regs.cr;
1216 for (i = 7; i >= 0; i--) {
1217 env->crf[i] = cr & 15;
1218 cr >>= 4;
1219 }
Alexander Grafba5e5092009-12-02 23:19:47 +01001220
aurel32d76d1652008-12-16 10:43:58 +00001221 env->ctr = regs.ctr;
1222 env->lr = regs.lr;
Richard Hendersonda91a002013-02-19 23:52:13 -08001223 cpu_write_xer(env, regs.xer);
aurel32d76d1652008-12-16 10:43:58 +00001224 env->msr = regs.msr;
1225 env->nip = regs.pc;
1226
1227 env->spr[SPR_SRR0] = regs.srr0;
1228 env->spr[SPR_SRR1] = regs.srr1;
1229
1230 env->spr[SPR_SPRG0] = regs.sprg0;
1231 env->spr[SPR_SPRG1] = regs.sprg1;
1232 env->spr[SPR_SPRG2] = regs.sprg2;
1233 env->spr[SPR_SPRG3] = regs.sprg3;
1234 env->spr[SPR_SPRG4] = regs.sprg4;
1235 env->spr[SPR_SPRG5] = regs.sprg5;
1236 env->spr[SPR_SPRG6] = regs.sprg6;
1237 env->spr[SPR_SPRG7] = regs.sprg7;
1238
Scott Wood90dc8812011-04-29 17:10:23 -05001239 env->spr[SPR_BOOKE_PID] = regs.pid;
1240
David Gibsonc995e942019-03-21 21:47:25 +11001241 for (i = 0; i < 32; i++) {
aurel32d76d1652008-12-16 10:43:58 +00001242 env->gpr[i] = regs.gpr[i];
David Gibsonc995e942019-03-21 21:47:25 +11001243 }
aurel32d76d1652008-12-16 10:43:58 +00001244
David Gibson70b79842013-02-20 16:41:51 +00001245 kvm_get_fp(cs);
1246
Scott Wood90dc8812011-04-29 17:10:23 -05001247 if (cap_booke_sregs) {
David Gibsona7a00a72016-03-09 11:58:33 +11001248 ret = kvmppc_get_booke_sregs(cpu);
Scott Wood90dc8812011-04-29 17:10:23 -05001249 if (ret < 0) {
1250 return ret;
1251 }
Alexander Graffafc0b62011-05-25 15:04:42 +02001252 }
Scott Wood90dc8812011-04-29 17:10:23 -05001253
Scott Wood90dc8812011-04-29 17:10:23 -05001254 if (cap_segstate) {
David Gibsona7a00a72016-03-09 11:58:33 +11001255 ret = kvmppc_get_books_sregs(cpu);
Scott Wood90dc8812011-04-29 17:10:23 -05001256 if (ret < 0) {
1257 return ret;
1258 }
Alexander Graffafc0b62011-05-25 15:04:42 +02001259 }
Alexander Grafba5e5092009-12-02 23:19:47 +01001260
David Gibsond67d40e2013-02-20 16:41:50 +00001261 if (cap_hior) {
1262 kvm_get_one_spr(cs, KVM_REG_PPC_HIOR, SPR_HIOR);
1263 }
1264
1265 if (cap_one_reg) {
1266 int i;
1267
David Gibsonc995e942019-03-21 21:47:25 +11001268 /*
1269 * We deliberately ignore errors here, for kernels which have
David Gibsond67d40e2013-02-20 16:41:50 +00001270 * the ONE_REG calls, but don't support the specific
1271 * registers, there's a reasonable chance things will still
David Gibsonc995e942019-03-21 21:47:25 +11001272 * work, at least until we try to migrate.
1273 */
David Gibsond67d40e2013-02-20 16:41:50 +00001274 for (i = 0; i < 1024; i++) {
1275 uint64_t id = env->spr_cb[i].one_reg_id;
1276
1277 if (id != 0) {
1278 kvm_get_one_spr(cs, id, i);
1279 }
1280 }
David Gibson9b00ea42013-04-07 19:08:22 +00001281
1282#ifdef TARGET_PPC64
Alexey Kardashevskiy80b3f792014-06-04 22:51:00 +10001283 if (msr_ts) {
1284 for (i = 0; i < ARRAY_SIZE(env->tm_gpr); i++) {
1285 kvm_get_one_reg(cs, KVM_REG_PPC_TM_GPR(i), &env->tm_gpr[i]);
1286 }
1287 for (i = 0; i < ARRAY_SIZE(env->tm_vsr); i++) {
1288 kvm_get_one_reg(cs, KVM_REG_PPC_TM_VSR(i), &env->tm_vsr[i]);
1289 }
1290 kvm_get_one_reg(cs, KVM_REG_PPC_TM_CR, &env->tm_cr);
1291 kvm_get_one_reg(cs, KVM_REG_PPC_TM_LR, &env->tm_lr);
1292 kvm_get_one_reg(cs, KVM_REG_PPC_TM_CTR, &env->tm_ctr);
1293 kvm_get_one_reg(cs, KVM_REG_PPC_TM_FPSCR, &env->tm_fpscr);
1294 kvm_get_one_reg(cs, KVM_REG_PPC_TM_AMR, &env->tm_amr);
1295 kvm_get_one_reg(cs, KVM_REG_PPC_TM_PPR, &env->tm_ppr);
1296 kvm_get_one_reg(cs, KVM_REG_PPC_TM_VRSAVE, &env->tm_vrsave);
1297 kvm_get_one_reg(cs, KVM_REG_PPC_TM_VSCR, &env->tm_vscr);
1298 kvm_get_one_reg(cs, KVM_REG_PPC_TM_DSCR, &env->tm_dscr);
1299 kvm_get_one_reg(cs, KVM_REG_PPC_TM_TAR, &env->tm_tar);
1300 }
1301
David Gibson9b00ea42013-04-07 19:08:22 +00001302 if (cap_papr) {
1303 if (kvm_get_vpa(cs) < 0) {
Greg Kurz8d83cbf2019-04-05 10:05:24 +02001304 trace_kvm_failed_get_vpa();
David Gibson9b00ea42013-04-07 19:08:22 +00001305 }
1306 }
Alexey Kardashevskiy98a8b522014-05-01 20:37:09 +10001307
1308 kvm_get_one_reg(cs, KVM_REG_PPC_TB_OFFSET, &env->tb_env->tb_offset);
Alexey Kardashevskiy972bd572019-09-23 18:41:10 +10001309 kvm_get_one_spr(cs, KVM_REG_PPC_DPDES, SPR_DPDES);
David Gibson9b00ea42013-04-07 19:08:22 +00001310#endif
David Gibsond67d40e2013-02-20 16:41:50 +00001311 }
1312
aurel32d76d1652008-12-16 10:43:58 +00001313 return 0;
1314}
1315
Andreas Färber1bc22652012-10-31 06:06:49 +01001316int kvmppc_set_interrupt(PowerPCCPU *cpu, int irq, int level)
Alexander Graffc87e182010-08-30 13:49:15 +02001317{
1318 unsigned virq = level ? KVM_INTERRUPT_SET_LEVEL : KVM_INTERRUPT_UNSET;
1319
1320 if (irq != PPC_INTERRUPT_EXT) {
1321 return 0;
1322 }
1323
Shivaprasad G Bhat1e8f51e2019-07-25 09:15:08 -05001324 if (!kvm_enabled() || !cap_interrupt_unset) {
Alexander Graffc87e182010-08-30 13:49:15 +02001325 return 0;
1326 }
1327
Andreas Färber1bc22652012-10-31 06:06:49 +01001328 kvm_vcpu_ioctl(CPU(cpu), KVM_INTERRUPT, &virq);
Alexander Graffc87e182010-08-30 13:49:15 +02001329
1330 return 0;
1331}
1332
Andreas Färber20d695a2012-10-31 06:57:49 +01001333void kvm_arch_pre_run(CPUState *cs, struct kvm_run *run)
aurel32d76d1652008-12-16 10:43:58 +00001334{
Shivaprasad G Bhat1e8f51e2019-07-25 09:15:08 -05001335 return;
aurel32d76d1652008-12-16 10:43:58 +00001336}
1337
Paolo Bonzini4c663752015-04-08 13:30:58 +02001338MemTxAttrs kvm_arch_post_run(CPUState *cs, struct kvm_run *run)
aurel32d76d1652008-12-16 10:43:58 +00001339{
Paolo Bonzini4c663752015-04-08 13:30:58 +02001340 return MEMTXATTRS_UNSPECIFIED;
aurel32d76d1652008-12-16 10:43:58 +00001341}
1342
Andreas Färber20d695a2012-10-31 06:57:49 +01001343int kvm_arch_process_async_events(CPUState *cs)
Marcelo Tosatti0af691d2010-05-04 09:45:27 -03001344{
Andreas Färber259186a2013-01-17 18:51:17 +01001345 return cs->halted;
Marcelo Tosatti0af691d2010-05-04 09:45:27 -03001346}
1347
Andreas Färber259186a2013-01-17 18:51:17 +01001348static int kvmppc_handle_halt(PowerPCCPU *cpu)
aurel32d76d1652008-12-16 10:43:58 +00001349{
Andreas Färber259186a2013-01-17 18:51:17 +01001350 CPUState *cs = CPU(cpu);
1351 CPUPPCState *env = &cpu->env;
1352
1353 if (!(cs->interrupt_request & CPU_INTERRUPT_HARD) && (msr_ee)) {
1354 cs->halted = 1;
Andreas Färber27103422013-08-26 08:31:06 +02001355 cs->exception_index = EXCP_HLT;
aurel32d76d1652008-12-16 10:43:58 +00001356 }
1357
Jan Kiszkabb4ea392011-03-15 12:26:28 +01001358 return 0;
aurel32d76d1652008-12-16 10:43:58 +00001359}
1360
1361/* map dcr access to existing qemu dcr emulation */
David Gibsonc995e942019-03-21 21:47:25 +11001362static int kvmppc_handle_dcr_read(CPUPPCState *env,
1363 uint32_t dcrn, uint32_t *data)
aurel32d76d1652008-12-16 10:43:58 +00001364{
David Gibsonc995e942019-03-21 21:47:25 +11001365 if (ppc_dcr_read(env->dcr_env, dcrn, data) < 0) {
aurel32d76d1652008-12-16 10:43:58 +00001366 fprintf(stderr, "Read to unhandled DCR (0x%x)\n", dcrn);
David Gibsonc995e942019-03-21 21:47:25 +11001367 }
aurel32d76d1652008-12-16 10:43:58 +00001368
Jan Kiszkabb4ea392011-03-15 12:26:28 +01001369 return 0;
aurel32d76d1652008-12-16 10:43:58 +00001370}
1371
David Gibsonc995e942019-03-21 21:47:25 +11001372static int kvmppc_handle_dcr_write(CPUPPCState *env,
1373 uint32_t dcrn, uint32_t data)
aurel32d76d1652008-12-16 10:43:58 +00001374{
David Gibsonc995e942019-03-21 21:47:25 +11001375 if (ppc_dcr_write(env->dcr_env, dcrn, data) < 0) {
aurel32d76d1652008-12-16 10:43:58 +00001376 fprintf(stderr, "Write to unhandled DCR (0x%x)\n", dcrn);
David Gibsonc995e942019-03-21 21:47:25 +11001377 }
aurel32d76d1652008-12-16 10:43:58 +00001378
Jan Kiszkabb4ea392011-03-15 12:26:28 +01001379 return 0;
aurel32d76d1652008-12-16 10:43:58 +00001380}
1381
Bharat Bhushan8a0548f2014-07-14 14:45:38 +05301382int kvm_arch_insert_sw_breakpoint(CPUState *cs, struct kvm_sw_breakpoint *bp)
1383{
1384 /* Mixed endian case is not handled */
1385 uint32_t sc = debug_inst_opcode;
1386
1387 if (cpu_memory_rw_debug(cs, bp->pc, (uint8_t *)&bp->saved_insn,
1388 sizeof(sc), 0) ||
1389 cpu_memory_rw_debug(cs, bp->pc, (uint8_t *)&sc, sizeof(sc), 1)) {
1390 return -EINVAL;
1391 }
1392
1393 return 0;
1394}
1395
1396int kvm_arch_remove_sw_breakpoint(CPUState *cs, struct kvm_sw_breakpoint *bp)
1397{
1398 uint32_t sc;
1399
1400 if (cpu_memory_rw_debug(cs, bp->pc, (uint8_t *)&sc, sizeof(sc), 0) ||
1401 sc != debug_inst_opcode ||
1402 cpu_memory_rw_debug(cs, bp->pc, (uint8_t *)&bp->saved_insn,
1403 sizeof(sc), 1)) {
1404 return -EINVAL;
1405 }
1406
1407 return 0;
1408}
1409
Bharat Bhushan88365d12014-07-14 14:45:37 +05301410static int find_hw_breakpoint(target_ulong addr, int type)
1411{
1412 int n;
1413
1414 assert((nb_hw_breakpoint + nb_hw_watchpoint)
1415 <= ARRAY_SIZE(hw_debug_points));
1416
1417 for (n = 0; n < nb_hw_breakpoint + nb_hw_watchpoint; n++) {
1418 if (hw_debug_points[n].addr == addr &&
1419 hw_debug_points[n].type == type) {
1420 return n;
1421 }
1422 }
1423
1424 return -1;
1425}
1426
1427static int find_hw_watchpoint(target_ulong addr, int *flag)
1428{
1429 int n;
1430
1431 n = find_hw_breakpoint(addr, GDB_WATCHPOINT_ACCESS);
1432 if (n >= 0) {
1433 *flag = BP_MEM_ACCESS;
1434 return n;
1435 }
1436
1437 n = find_hw_breakpoint(addr, GDB_WATCHPOINT_WRITE);
1438 if (n >= 0) {
1439 *flag = BP_MEM_WRITE;
1440 return n;
1441 }
1442
1443 n = find_hw_breakpoint(addr, GDB_WATCHPOINT_READ);
1444 if (n >= 0) {
1445 *flag = BP_MEM_READ;
1446 return n;
1447 }
1448
1449 return -1;
1450}
1451
1452int kvm_arch_insert_hw_breakpoint(target_ulong addr,
1453 target_ulong len, int type)
1454{
1455 if ((nb_hw_breakpoint + nb_hw_watchpoint) >= ARRAY_SIZE(hw_debug_points)) {
1456 return -ENOBUFS;
1457 }
1458
1459 hw_debug_points[nb_hw_breakpoint + nb_hw_watchpoint].addr = addr;
1460 hw_debug_points[nb_hw_breakpoint + nb_hw_watchpoint].type = type;
1461
1462 switch (type) {
1463 case GDB_BREAKPOINT_HW:
1464 if (nb_hw_breakpoint >= max_hw_breakpoint) {
1465 return -ENOBUFS;
1466 }
1467
1468 if (find_hw_breakpoint(addr, type) >= 0) {
1469 return -EEXIST;
1470 }
1471
1472 nb_hw_breakpoint++;
1473 break;
1474
1475 case GDB_WATCHPOINT_WRITE:
1476 case GDB_WATCHPOINT_READ:
1477 case GDB_WATCHPOINT_ACCESS:
1478 if (nb_hw_watchpoint >= max_hw_watchpoint) {
1479 return -ENOBUFS;
1480 }
1481
1482 if (find_hw_breakpoint(addr, type) >= 0) {
1483 return -EEXIST;
1484 }
1485
1486 nb_hw_watchpoint++;
1487 break;
1488
1489 default:
1490 return -ENOSYS;
1491 }
1492
1493 return 0;
1494}
1495
1496int kvm_arch_remove_hw_breakpoint(target_ulong addr,
1497 target_ulong len, int type)
1498{
1499 int n;
1500
1501 n = find_hw_breakpoint(addr, type);
1502 if (n < 0) {
1503 return -ENOENT;
1504 }
1505
1506 switch (type) {
1507 case GDB_BREAKPOINT_HW:
1508 nb_hw_breakpoint--;
1509 break;
1510
1511 case GDB_WATCHPOINT_WRITE:
1512 case GDB_WATCHPOINT_READ:
1513 case GDB_WATCHPOINT_ACCESS:
1514 nb_hw_watchpoint--;
1515 break;
1516
1517 default:
1518 return -ENOSYS;
1519 }
1520 hw_debug_points[n] = hw_debug_points[nb_hw_breakpoint + nb_hw_watchpoint];
1521
1522 return 0;
1523}
1524
1525void kvm_arch_remove_all_hw_breakpoints(void)
1526{
1527 nb_hw_breakpoint = nb_hw_watchpoint = 0;
1528}
1529
Bharat Bhushan8a0548f2014-07-14 14:45:38 +05301530void kvm_arch_update_guest_debug(CPUState *cs, struct kvm_guest_debug *dbg)
1531{
Bharat Bhushan88365d12014-07-14 14:45:37 +05301532 int n;
1533
Bharat Bhushan8a0548f2014-07-14 14:45:38 +05301534 /* Software Breakpoint updates */
1535 if (kvm_sw_breakpoints_active(cs)) {
1536 dbg->control |= KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP;
1537 }
Bharat Bhushan88365d12014-07-14 14:45:37 +05301538
1539 assert((nb_hw_breakpoint + nb_hw_watchpoint)
1540 <= ARRAY_SIZE(hw_debug_points));
1541 assert((nb_hw_breakpoint + nb_hw_watchpoint) <= ARRAY_SIZE(dbg->arch.bp));
1542
1543 if (nb_hw_breakpoint + nb_hw_watchpoint > 0) {
1544 dbg->control |= KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_HW_BP;
1545 memset(dbg->arch.bp, 0, sizeof(dbg->arch.bp));
1546 for (n = 0; n < nb_hw_breakpoint + nb_hw_watchpoint; n++) {
1547 switch (hw_debug_points[n].type) {
1548 case GDB_BREAKPOINT_HW:
1549 dbg->arch.bp[n].type = KVMPPC_DEBUG_BREAKPOINT;
1550 break;
1551 case GDB_WATCHPOINT_WRITE:
1552 dbg->arch.bp[n].type = KVMPPC_DEBUG_WATCH_WRITE;
1553 break;
1554 case GDB_WATCHPOINT_READ:
1555 dbg->arch.bp[n].type = KVMPPC_DEBUG_WATCH_READ;
1556 break;
1557 case GDB_WATCHPOINT_ACCESS:
1558 dbg->arch.bp[n].type = KVMPPC_DEBUG_WATCH_WRITE |
1559 KVMPPC_DEBUG_WATCH_READ;
1560 break;
1561 default:
1562 cpu_abort(cs, "Unsupported breakpoint type\n");
1563 }
1564 dbg->arch.bp[n].addr = hw_debug_points[n].addr;
1565 }
1566 }
Bharat Bhushan8a0548f2014-07-14 14:45:38 +05301567}
1568
Fabiano Rosas2cbd1582019-02-28 19:57:57 -03001569static int kvm_handle_hw_breakpoint(CPUState *cs,
1570 struct kvm_debug_exit_arch *arch_info)
1571{
Fabiano Rosas6e0552a2020-01-10 12:13:42 -03001572 int handle = DEBUG_RETURN_GUEST;
Fabiano Rosas2cbd1582019-02-28 19:57:57 -03001573 int n;
1574 int flag = 0;
1575
1576 if (nb_hw_breakpoint + nb_hw_watchpoint > 0) {
1577 if (arch_info->status & KVMPPC_DEBUG_BREAKPOINT) {
1578 n = find_hw_breakpoint(arch_info->address, GDB_BREAKPOINT_HW);
1579 if (n >= 0) {
Fabiano Rosas6e0552a2020-01-10 12:13:42 -03001580 handle = DEBUG_RETURN_GDB;
Fabiano Rosas2cbd1582019-02-28 19:57:57 -03001581 }
1582 } else if (arch_info->status & (KVMPPC_DEBUG_WATCH_READ |
1583 KVMPPC_DEBUG_WATCH_WRITE)) {
1584 n = find_hw_watchpoint(arch_info->address, &flag);
1585 if (n >= 0) {
Fabiano Rosas6e0552a2020-01-10 12:13:42 -03001586 handle = DEBUG_RETURN_GDB;
Fabiano Rosas2cbd1582019-02-28 19:57:57 -03001587 cs->watchpoint_hit = &hw_watchpoint;
1588 hw_watchpoint.vaddr = hw_debug_points[n].addr;
1589 hw_watchpoint.flags = flag;
1590 }
1591 }
1592 }
1593 return handle;
1594}
1595
Fabiano Rosas468e3a12019-02-28 19:57:58 -03001596static int kvm_handle_singlestep(void)
1597{
Fabiano Rosas6e0552a2020-01-10 12:13:42 -03001598 return DEBUG_RETURN_GDB;
Fabiano Rosas468e3a12019-02-28 19:57:58 -03001599}
1600
1601static int kvm_handle_sw_breakpoint(void)
1602{
Fabiano Rosas6e0552a2020-01-10 12:13:42 -03001603 return DEBUG_RETURN_GDB;
Fabiano Rosas468e3a12019-02-28 19:57:58 -03001604}
1605
Bharat Bhushan8a0548f2014-07-14 14:45:38 +05301606static int kvm_handle_debug(PowerPCCPU *cpu, struct kvm_run *run)
1607{
1608 CPUState *cs = CPU(cpu);
1609 CPUPPCState *env = &cpu->env;
1610 struct kvm_debug_exit_arch *arch_info = &run->debug.arch;
Bharat Bhushan8a0548f2014-07-14 14:45:38 +05301611
Bharat Bhushan88365d12014-07-14 14:45:37 +05301612 if (cs->singlestep_enabled) {
Fabiano Rosas468e3a12019-02-28 19:57:58 -03001613 return kvm_handle_singlestep();
Bharat Bhushan8a0548f2014-07-14 14:45:38 +05301614 }
1615
Fabiano Rosas468e3a12019-02-28 19:57:58 -03001616 if (arch_info->status) {
1617 return kvm_handle_hw_breakpoint(cs, arch_info);
1618 }
1619
1620 if (kvm_find_sw_breakpoint(cs, arch_info->address)) {
1621 return kvm_handle_sw_breakpoint();
1622 }
1623
1624 /*
1625 * QEMU is not able to handle debug exception, so inject
1626 * program exception to guest;
1627 * Yes program exception NOT debug exception !!
1628 * When QEMU is using debug resources then debug exception must
1629 * be always set. To achieve this we set MSR_DE and also set
1630 * MSRP_DEP so guest cannot change MSR_DE.
1631 * When emulating debug resource for guest we want guest
1632 * to control MSR_DE (enable/disable debug interrupt on need).
1633 * Supporting both configurations are NOT possible.
1634 * So the result is that we cannot share debug resources
1635 * between QEMU and Guest on BOOKE architecture.
1636 * In the current design QEMU gets the priority over guest,
1637 * this means that if QEMU is using debug resources then guest
1638 * cannot use them;
1639 * For software breakpoint QEMU uses a privileged instruction;
1640 * So there cannot be any reason that we are here for guest
1641 * set debug exception, only possibility is guest executed a
1642 * privileged / illegal instruction and that's why we are
1643 * injecting a program interrupt.
1644 */
1645 cpu_synchronize_state(cs);
1646 /*
1647 * env->nip is PC, so increment this by 4 to use
1648 * ppc_cpu_do_interrupt(), which set srr0 = env->nip - 4.
1649 */
1650 env->nip += 4;
1651 cs->exception_index = POWERPC_EXCP_PROGRAM;
1652 env->error_code = POWERPC_EXCP_INVAL;
1653 ppc_cpu_do_interrupt(cs);
1654
Fabiano Rosas6e0552a2020-01-10 12:13:42 -03001655 return DEBUG_RETURN_GUEST;
Bharat Bhushan8a0548f2014-07-14 14:45:38 +05301656}
1657
Andreas Färber20d695a2012-10-31 06:57:49 +01001658int kvm_arch_handle_exit(CPUState *cs, struct kvm_run *run)
aurel32d76d1652008-12-16 10:43:58 +00001659{
Andreas Färber20d695a2012-10-31 06:57:49 +01001660 PowerPCCPU *cpu = POWERPC_CPU(cs);
1661 CPUPPCState *env = &cpu->env;
Jan Kiszkabb4ea392011-03-15 12:26:28 +01001662 int ret;
aurel32d76d1652008-12-16 10:43:58 +00001663
Jan Kiszka4b8523e2015-06-18 18:47:23 +02001664 qemu_mutex_lock_iothread();
1665
aurel32d76d1652008-12-16 10:43:58 +00001666 switch (run->exit_reason) {
1667 case KVM_EXIT_DCR:
1668 if (run->dcr.is_write) {
Greg Kurz8d83cbf2019-04-05 10:05:24 +02001669 trace_kvm_handle_dcr_write();
aurel32d76d1652008-12-16 10:43:58 +00001670 ret = kvmppc_handle_dcr_write(env, run->dcr.dcrn, run->dcr.data);
1671 } else {
Boxuan Li228152c22019-05-01 01:28:42 +08001672 trace_kvm_handle_dcr_read();
aurel32d76d1652008-12-16 10:43:58 +00001673 ret = kvmppc_handle_dcr_read(env, run->dcr.dcrn, &run->dcr.data);
1674 }
1675 break;
1676 case KVM_EXIT_HLT:
Greg Kurz8d83cbf2019-04-05 10:05:24 +02001677 trace_kvm_handle_halt();
Andreas Färber259186a2013-01-17 18:51:17 +01001678 ret = kvmppc_handle_halt(cpu);
aurel32d76d1652008-12-16 10:43:58 +00001679 break;
David Gibsonc6304a42013-03-13 15:53:27 +00001680#if defined(TARGET_PPC64)
Alexander Graff61b4be2011-08-09 17:57:37 +02001681 case KVM_EXIT_PAPR_HCALL:
Greg Kurz8d83cbf2019-04-05 10:05:24 +02001682 trace_kvm_handle_papr_hcall();
Andreas Färber20d695a2012-10-31 06:57:49 +01001683 run->papr_hcall.ret = spapr_hypercall(cpu,
Andreas Färberaa100fa2012-05-03 06:13:14 +02001684 run->papr_hcall.nr,
Alexander Graff61b4be2011-08-09 17:57:37 +02001685 run->papr_hcall.args);
David Gibson78e8fde2012-08-06 18:44:45 +00001686 ret = 0;
Alexander Graff61b4be2011-08-09 17:57:37 +02001687 break;
1688#endif
Alexander Graf5b95b8b2013-01-17 11:54:38 +01001689 case KVM_EXIT_EPR:
Greg Kurz8d83cbf2019-04-05 10:05:24 +02001690 trace_kvm_handle_epr();
Alexander Graf933b19e2014-02-14 09:15:21 +01001691 run->epr.epr = ldl_phys(cs->as, env->mpic_iack);
Alexander Graf5b95b8b2013-01-17 11:54:38 +01001692 ret = 0;
1693 break;
Bharat Bhushan31f2cb82013-02-24 18:16:21 +00001694 case KVM_EXIT_WATCHDOG:
Greg Kurz8d83cbf2019-04-05 10:05:24 +02001695 trace_kvm_handle_watchdog_expiry();
Bharat Bhushan31f2cb82013-02-24 18:16:21 +00001696 watchdog_perform_action();
1697 ret = 0;
1698 break;
1699
Bharat Bhushan8a0548f2014-07-14 14:45:38 +05301700 case KVM_EXIT_DEBUG:
Greg Kurz8d83cbf2019-04-05 10:05:24 +02001701 trace_kvm_handle_debug_exception();
Bharat Bhushan8a0548f2014-07-14 14:45:38 +05301702 if (kvm_handle_debug(cpu, run)) {
1703 ret = EXCP_DEBUG;
1704 break;
1705 }
1706 /* re-enter, this exception was guest-internal */
1707 ret = 0;
1708 break;
1709
Aravinda Prasad9ac703a2020-01-31 00:14:19 +05301710#if defined(TARGET_PPC64)
1711 case KVM_EXIT_NMI:
1712 trace_kvm_handle_nmi_exception();
1713 ret = kvm_handle_nmi(cpu, run);
1714 break;
1715#endif
1716
Jan Kiszka73aaec42011-01-21 21:48:06 +01001717 default:
1718 fprintf(stderr, "KVM: unknown exit reason %d\n", run->exit_reason);
1719 ret = -1;
1720 break;
aurel32d76d1652008-12-16 10:43:58 +00001721 }
1722
Jan Kiszka4b8523e2015-06-18 18:47:23 +02001723 qemu_mutex_unlock_iothread();
aurel32d76d1652008-12-16 10:43:58 +00001724 return ret;
1725}
1726
Bharat Bhushan31f2cb82013-02-24 18:16:21 +00001727int kvmppc_or_tsr_bits(PowerPCCPU *cpu, uint32_t tsr_bits)
1728{
1729 CPUState *cs = CPU(cpu);
1730 uint32_t bits = tsr_bits;
1731 struct kvm_one_reg reg = {
1732 .id = KVM_REG_PPC_OR_TSR,
1733 .addr = (uintptr_t) &bits,
1734 };
1735
1736 return kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, &reg);
1737}
1738
1739int kvmppc_clear_tsr_bits(PowerPCCPU *cpu, uint32_t tsr_bits)
1740{
1741
1742 CPUState *cs = CPU(cpu);
1743 uint32_t bits = tsr_bits;
1744 struct kvm_one_reg reg = {
1745 .id = KVM_REG_PPC_CLEAR_TSR,
1746 .addr = (uintptr_t) &bits,
1747 };
1748
1749 return kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, &reg);
1750}
1751
1752int kvmppc_set_tcr(PowerPCCPU *cpu)
1753{
1754 CPUState *cs = CPU(cpu);
1755 CPUPPCState *env = &cpu->env;
1756 uint32_t tcr = env->spr[SPR_BOOKE_TCR];
1757
1758 struct kvm_one_reg reg = {
1759 .id = KVM_REG_PPC_TCR,
1760 .addr = (uintptr_t) &tcr,
1761 };
1762
1763 return kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, &reg);
1764}
1765
1766int kvmppc_booke_watchdog_enable(PowerPCCPU *cpu)
1767{
1768 CPUState *cs = CPU(cpu);
Bharat Bhushan31f2cb82013-02-24 18:16:21 +00001769 int ret;
1770
1771 if (!kvm_enabled()) {
1772 return -1;
1773 }
1774
1775 if (!cap_ppc_watchdog) {
1776 printf("warning: KVM does not support watchdog");
1777 return -1;
1778 }
1779
Cornelia Huck48add812014-04-09 17:21:57 +02001780 ret = kvm_vcpu_enable_cap(cs, KVM_CAP_PPC_BOOKE_WATCHDOG, 0);
Bharat Bhushan31f2cb82013-02-24 18:16:21 +00001781 if (ret < 0) {
1782 fprintf(stderr, "%s: couldn't enable KVM_CAP_PPC_BOOKE_WATCHDOG: %s\n",
1783 __func__, strerror(-ret));
1784 return ret;
1785 }
1786
1787 return ret;
1788}
1789
Alexander Grafdc333cd2010-02-09 17:37:05 +01001790static int read_cpuinfo(const char *field, char *value, int len)
1791{
1792 FILE *f;
1793 int ret = -1;
1794 int field_len = strlen(field);
1795 char line[512];
1796
1797 f = fopen("/proc/cpuinfo", "r");
1798 if (!f) {
1799 return -1;
1800 }
1801
1802 do {
Nikunj A Dadhaniaef951442014-07-09 16:08:37 +05301803 if (!fgets(line, sizeof(line), f)) {
Alexander Grafdc333cd2010-02-09 17:37:05 +01001804 break;
1805 }
1806 if (!strncmp(line, field, field_len)) {
Jim Meyeringae215062012-10-04 13:09:52 +02001807 pstrcpy(value, len, line);
Alexander Grafdc333cd2010-02-09 17:37:05 +01001808 ret = 0;
1809 break;
1810 }
David Gibsonc995e942019-03-21 21:47:25 +11001811 } while (*line);
Alexander Grafdc333cd2010-02-09 17:37:05 +01001812
1813 fclose(f);
1814
1815 return ret;
1816}
1817
1818uint32_t kvmppc_get_tbfreq(void)
1819{
1820 char line[512];
1821 char *ns;
Rutuja Shah73bcb242016-03-21 21:32:30 +05301822 uint32_t retval = NANOSECONDS_PER_SECOND;
Alexander Grafdc333cd2010-02-09 17:37:05 +01001823
1824 if (read_cpuinfo("timebase", line, sizeof(line))) {
1825 return retval;
1826 }
1827
David Gibsonc995e942019-03-21 21:47:25 +11001828 ns = strchr(line, ':');
1829 if (!ns) {
Alexander Grafdc333cd2010-02-09 17:37:05 +01001830 return retval;
1831 }
1832
1833 ns++;
1834
Shraddha Barkef9b8e7f2015-09-25 14:07:58 +05301835 return atoi(ns);
Alexander Grafdc333cd2010-02-09 17:37:05 +01001836}
Gleb Natapov4513d922010-05-10 11:21:34 +03001837
Nikunj A Dadhaniaef951442014-07-09 16:08:37 +05301838bool kvmppc_get_host_serial(char **value)
1839{
1840 return g_file_get_contents("/proc/device-tree/system-id", value, NULL,
1841 NULL);
1842}
1843
1844bool kvmppc_get_host_model(char **value)
1845{
1846 return g_file_get_contents("/proc/device-tree/model", value, NULL, NULL);
1847}
1848
Alexander Grafeadaada2011-07-21 02:29:15 +02001849/* Try to find a device tree node for a CPU with clock-frequency property */
1850static int kvmppc_find_cpu_dt(char *buf, int buf_len)
1851{
1852 struct dirent *dirp;
1853 DIR *dp;
1854
David Gibsonc995e942019-03-21 21:47:25 +11001855 dp = opendir(PROC_DEVTREE_CPU);
1856 if (!dp) {
Alexander Grafeadaada2011-07-21 02:29:15 +02001857 printf("Can't open directory " PROC_DEVTREE_CPU "\n");
1858 return -1;
1859 }
1860
1861 buf[0] = '\0';
1862 while ((dirp = readdir(dp)) != NULL) {
1863 FILE *f;
1864 snprintf(buf, buf_len, "%s%s/clock-frequency", PROC_DEVTREE_CPU,
1865 dirp->d_name);
1866 f = fopen(buf, "r");
1867 if (f) {
1868 snprintf(buf, buf_len, "%s%s", PROC_DEVTREE_CPU, dirp->d_name);
1869 fclose(f);
1870 break;
1871 }
1872 buf[0] = '\0';
1873 }
1874 closedir(dp);
1875 if (buf[0] == '\0') {
1876 printf("Unknown host!\n");
1877 return -1;
1878 }
1879
1880 return 0;
1881}
1882
Sukadev Bhattiprolu7d94a302015-11-13 18:13:07 -08001883static uint64_t kvmppc_read_int_dt(const char *filename)
Alexander Grafeadaada2011-07-21 02:29:15 +02001884{
David Gibson9bc884b2011-10-10 18:31:00 +00001885 union {
1886 uint32_t v32;
1887 uint64_t v64;
1888 } u;
Alexander Grafeadaada2011-07-21 02:29:15 +02001889 FILE *f;
1890 int len;
1891
Sukadev Bhattiprolu7d94a302015-11-13 18:13:07 -08001892 f = fopen(filename, "rb");
Alexander Grafeadaada2011-07-21 02:29:15 +02001893 if (!f) {
1894 return -1;
1895 }
1896
David Gibson9bc884b2011-10-10 18:31:00 +00001897 len = fread(&u, 1, sizeof(u), f);
Alexander Grafeadaada2011-07-21 02:29:15 +02001898 fclose(f);
1899 switch (len) {
David Gibson9bc884b2011-10-10 18:31:00 +00001900 case 4:
1901 /* property is a 32-bit quantity */
1902 return be32_to_cpu(u.v32);
1903 case 8:
1904 return be64_to_cpu(u.v64);
Alexander Grafeadaada2011-07-21 02:29:15 +02001905 }
1906
1907 return 0;
1908}
1909
David Gibsonc995e942019-03-21 21:47:25 +11001910/*
1911 * Read a CPU node property from the host device tree that's a single
Sukadev Bhattiprolu7d94a302015-11-13 18:13:07 -08001912 * integer (32-bit or 64-bit). Returns 0 if anything goes wrong
David Gibsonc995e942019-03-21 21:47:25 +11001913 * (can't find or open the property, or doesn't understand the format)
1914 */
Sukadev Bhattiprolu7d94a302015-11-13 18:13:07 -08001915static uint64_t kvmppc_read_int_cpu_dt(const char *propname)
1916{
1917 char buf[PATH_MAX], *tmp;
1918 uint64_t val;
1919
1920 if (kvmppc_find_cpu_dt(buf, sizeof(buf))) {
1921 return -1;
1922 }
1923
1924 tmp = g_strdup_printf("%s/%s", buf, propname);
1925 val = kvmppc_read_int_dt(tmp);
1926 g_free(tmp);
1927
1928 return val;
1929}
1930
David Gibson9bc884b2011-10-10 18:31:00 +00001931uint64_t kvmppc_get_clockfreq(void)
1932{
1933 return kvmppc_read_int_cpu_dt("clock-frequency");
1934}
1935
Suraj Jitindar Singh7d050522019-03-01 13:43:16 +11001936static int kvmppc_get_dec_bits(void)
1937{
1938 int nr_bits = kvmppc_read_int_cpu_dt("ibm,dec-bits");
1939
1940 if (nr_bits > 0) {
1941 return nr_bits;
1942 }
1943 return 0;
1944}
1945
Stuart Yoder1a61a9a2013-01-03 12:37:02 +00001946static int kvmppc_get_pvinfo(CPUPPCState *env, struct kvm_ppc_pvinfo *pvinfo)
Richard Hendersondb70b312019-03-22 19:07:57 -07001947{
1948 CPUState *cs = env_cpu(env);
Alexander Graf45024f02010-08-03 15:22:42 +02001949
Alexander Graf6fd33a72014-07-14 19:17:35 +02001950 if (kvm_vm_check_extension(cs->kvm_state, KVM_CAP_PPC_GET_PVINFO) &&
Stuart Yoder1a61a9a2013-01-03 12:37:02 +00001951 !kvm_vm_ioctl(cs->kvm_state, KVM_PPC_GET_PVINFO, pvinfo)) {
1952 return 0;
1953 }
Alexander Graf45024f02010-08-03 15:22:42 +02001954
Stuart Yoder1a61a9a2013-01-03 12:37:02 +00001955 return 1;
1956}
1957
1958int kvmppc_get_hasidle(CPUPPCState *env)
1959{
1960 struct kvm_ppc_pvinfo pvinfo;
1961
1962 if (!kvmppc_get_pvinfo(env, &pvinfo) &&
1963 (pvinfo.flags & KVM_PPC_PVINFO_FLAGS_EV_IDLE)) {
1964 return 1;
1965 }
1966
1967 return 0;
1968}
1969
1970int kvmppc_get_hypercall(CPUPPCState *env, uint8_t *buf, int buf_len)
1971{
David Gibsonc995e942019-03-21 21:47:25 +11001972 uint32_t *hc = (uint32_t *)buf;
Stuart Yoder1a61a9a2013-01-03 12:37:02 +00001973 struct kvm_ppc_pvinfo pvinfo;
1974
1975 if (!kvmppc_get_pvinfo(env, &pvinfo)) {
1976 memcpy(buf, pvinfo.hcall, buf_len);
Alexander Graf45024f02010-08-03 15:22:42 +02001977 return 0;
1978 }
Alexander Graf45024f02010-08-03 15:22:42 +02001979
1980 /*
Alexander Grafd13fc322014-06-11 12:19:03 +02001981 * Fallback to always fail hypercalls regardless of endianness:
Alexander Graf45024f02010-08-03 15:22:42 +02001982 *
Alexander Grafd13fc322014-06-11 12:19:03 +02001983 * tdi 0,r0,72 (becomes b .+8 in wrong endian, nop in good endian)
Alexander Graf45024f02010-08-03 15:22:42 +02001984 * li r3, -1
Alexander Grafd13fc322014-06-11 12:19:03 +02001985 * b .+8 (becomes nop in wrong endian)
1986 * bswap32(li r3, -1)
Alexander Graf45024f02010-08-03 15:22:42 +02001987 */
1988
Alexander Grafd13fc322014-06-11 12:19:03 +02001989 hc[0] = cpu_to_be32(0x08000048);
1990 hc[1] = cpu_to_be32(0x3860ffff);
1991 hc[2] = cpu_to_be32(0x48000008);
1992 hc[3] = cpu_to_be32(bswap32(0x3860ffff));
Alexander Graf45024f02010-08-03 15:22:42 +02001993
Alexey Kardashevskiy0ddbd052016-03-21 13:14:02 +11001994 return 1;
Alexander Graf45024f02010-08-03 15:22:42 +02001995}
1996
David Gibson026bfd82015-05-07 15:33:59 +10001997static inline int kvmppc_enable_hcall(KVMState *s, target_ulong hcall)
1998{
1999 return kvm_vm_enable_cap(s, KVM_CAP_PPC_ENABLE_HCALL, 0, hcall, 1);
2000}
2001
2002void kvmppc_enable_logical_ci_hcalls(void)
2003{
2004 /*
2005 * FIXME: it would be nice if we could detect the cases where
2006 * we're using a device which requires the in kernel
2007 * implementation of these hcalls, but the kernel lacks them and
2008 * produce a warning.
2009 */
2010 kvmppc_enable_hcall(kvm_state, H_LOGICAL_CI_LOAD);
2011 kvmppc_enable_hcall(kvm_state, H_LOGICAL_CI_STORE);
2012}
2013
Alexey Kardashevskiyef9971d2015-09-08 11:25:13 +10002014void kvmppc_enable_set_mode_hcall(void)
2015{
2016 kvmppc_enable_hcall(kvm_state, H_SET_MODE);
2017}
2018
Nathan Whitehorn5145ad42016-08-30 01:02:47 +00002019void kvmppc_enable_clear_ref_mod_hcalls(void)
2020{
2021 kvmppc_enable_hcall(kvm_state, H_CLEAR_REF);
2022 kvmppc_enable_hcall(kvm_state, H_CLEAR_MOD);
2023}
2024
Suraj Jitindar Singh68f9f702019-03-06 17:06:08 +11002025void kvmppc_enable_h_page_init(void)
2026{
2027 kvmppc_enable_hcall(kvm_state, H_PAGE_INIT);
2028}
2029
Andreas Färber1bc22652012-10-31 06:06:49 +01002030void kvmppc_set_papr(PowerPCCPU *cpu)
Alexander Graff61b4be2011-08-09 17:57:37 +02002031{
Andreas Färber1bc22652012-10-31 06:06:49 +01002032 CPUState *cs = CPU(cpu);
Alexander Graff61b4be2011-08-09 17:57:37 +02002033 int ret;
2034
David Gibsonda20aed2018-04-05 16:02:51 +10002035 if (!kvm_enabled()) {
2036 return;
2037 }
2038
Cornelia Huck48add812014-04-09 17:21:57 +02002039 ret = kvm_vcpu_enable_cap(cs, KVM_CAP_PPC_PAPR, 0);
Alexander Graff61b4be2011-08-09 17:57:37 +02002040 if (ret) {
Thomas Huth072ed5f2016-02-18 22:01:38 +01002041 error_report("This vCPU type or KVM version does not support PAPR");
2042 exit(1);
Alexander Graff61b4be2011-08-09 17:57:37 +02002043 }
David Gibson9b00ea42013-04-07 19:08:22 +00002044
David Gibsonc995e942019-03-21 21:47:25 +11002045 /*
2046 * Update the capability flag so we sync the right information
2047 * with kvm
2048 */
David Gibson9b00ea42013-04-07 19:08:22 +00002049 cap_papr = 1;
Alexander Graff61b4be2011-08-09 17:57:37 +02002050}
2051
David Gibsond6e166c2016-10-28 22:09:37 +11002052int kvmppc_set_compat(PowerPCCPU *cpu, uint32_t compat_pvr)
Alexey Kardashevskiy6db5bb02014-05-23 12:26:58 +10002053{
David Gibsond6e166c2016-10-28 22:09:37 +11002054 return kvm_set_one_reg(CPU(cpu), KVM_REG_PPC_ARCH_COMPAT, &compat_pvr);
Alexey Kardashevskiy6db5bb02014-05-23 12:26:58 +10002055}
2056
Alexander Graf5b95b8b2013-01-17 11:54:38 +01002057void kvmppc_set_mpic_proxy(PowerPCCPU *cpu, int mpic_proxy)
2058{
Alexander Graf5b95b8b2013-01-17 11:54:38 +01002059 CPUState *cs = CPU(cpu);
Alexander Graf5b95b8b2013-01-17 11:54:38 +01002060 int ret;
2061
Cornelia Huck48add812014-04-09 17:21:57 +02002062 ret = kvm_vcpu_enable_cap(cs, KVM_CAP_PPC_EPR, 0, mpic_proxy);
Alexander Graf5b95b8b2013-01-17 11:54:38 +01002063 if (ret && mpic_proxy) {
Thomas Huth072ed5f2016-02-18 22:01:38 +01002064 error_report("This KVM version does not support EPR");
2065 exit(1);
Alexander Graf5b95b8b2013-01-17 11:54:38 +01002066 }
2067}
2068
Nicholas Pigginec010c02020-03-26 00:29:03 +10002069bool kvmppc_get_fwnmi(void)
2070{
2071 return cap_fwnmi;
2072}
2073
Laurent Vivieraef92d82020-07-24 10:35:33 +02002074int kvmppc_set_fwnmi(PowerPCCPU *cpu)
Aravinda Prasad9d953ce2020-01-31 00:14:18 +05302075{
Aravinda Prasad9d953ce2020-01-31 00:14:18 +05302076 CPUState *cs = CPU(cpu);
2077
2078 return kvm_vcpu_enable_cap(cs, KVM_CAP_PPC_FWNMI, 0);
2079}
2080
David Gibsone97c3632011-09-29 21:39:10 +00002081int kvmppc_smt_threads(void)
2082{
2083 return cap_ppc_smt ? cap_ppc_smt : 1;
2084}
2085
Sam Bobrofffa98fbf2017-08-18 15:50:22 +10002086int kvmppc_set_smt_threads(int smt)
2087{
2088 int ret;
2089
2090 ret = kvm_vm_enable_cap(kvm_state, KVM_CAP_PPC_SMT, 0, smt, 0);
2091 if (!ret) {
2092 cap_ppc_smt = smt;
2093 }
2094 return ret;
2095}
2096
Vladimir Sementsov-Ogievskiy0c115682019-12-05 20:46:21 +03002097void kvmppc_error_append_smt_possible_hint(Error *const *errp)
Sam Bobrofffa98fbf2017-08-18 15:50:22 +10002098{
2099 int i;
2100 GString *g;
2101 char *s;
2102
2103 assert(kvm_enabled());
2104 if (cap_ppc_smt_possible) {
2105 g = g_string_new("Available VSMT modes:");
2106 for (i = 63; i >= 0; i--) {
2107 if ((1UL << i) & cap_ppc_smt_possible) {
2108 g_string_append_printf(g, " %lu", (1UL << i));
2109 }
2110 }
2111 s = g_string_free(g, false);
Markus Armbruster1a639fd2019-12-18 08:36:27 +01002112 error_append_hint(errp, "%s.\n", s);
Sam Bobrofffa98fbf2017-08-18 15:50:22 +10002113 g_free(s);
2114 } else {
Markus Armbruster1a639fd2019-12-18 08:36:27 +01002115 error_append_hint(errp,
Sam Bobrofffa98fbf2017-08-18 15:50:22 +10002116 "This KVM seems to be too old to support VSMT.\n");
2117 }
2118}
2119
2120
David Gibson7f763a52012-09-12 16:57:12 +00002121#ifdef TARGET_PPC64
David Gibson6a847372019-11-28 16:12:06 +11002122uint64_t kvmppc_vrma_limit(unsigned int hash_shift)
David Gibson7f763a52012-09-12 16:57:12 +00002123{
David Gibsonf36951c2013-04-07 19:08:18 +00002124 struct kvm_ppc_smmu_info info;
2125 long rampagesize, best_page_shift;
2126 int i;
2127
David Gibsonc995e942019-03-21 21:47:25 +11002128 /*
2129 * Find the largest hardware supported page size that's less than
2130 * or equal to the (logical) backing page size of guest RAM
2131 */
Greg Kurzab256962018-06-29 11:48:32 +02002132 kvm_get_smmu_info(&info, &error_fatal);
David Hildenbrand905b7ee2019-04-17 13:31:43 +02002133 rampagesize = qemu_minrampagesize();
David Gibsonf36951c2013-04-07 19:08:18 +00002134 best_page_shift = 0;
2135
2136 for (i = 0; i < KVM_PPC_PAGE_SIZES_MAX_SZ; i++) {
2137 struct kvm_ppc_one_seg_page_size *sps = &info.sps[i];
2138
2139 if (!sps->page_shift) {
2140 continue;
2141 }
2142
2143 if ((sps->page_shift > best_page_shift)
2144 && ((1UL << sps->page_shift) <= rampagesize)) {
2145 best_page_shift = sps->page_shift;
2146 }
2147 }
2148
David Gibson6a847372019-11-28 16:12:06 +11002149 return 1ULL << (best_page_shift + hash_shift - 7);
David Gibson7f763a52012-09-12 16:57:12 +00002150}
2151#endif
2152
Alexey Kardashevskiyda953242014-05-27 15:36:30 +10002153bool kvmppc_spapr_use_multitce(void)
2154{
2155 return cap_spapr_multitce;
2156}
2157
Alexey Kardashevskiy3dc410a2017-03-27 16:22:19 +11002158int kvmppc_spapr_enable_inkernel_multitce(void)
2159{
2160 int ret;
2161
2162 ret = kvm_vm_enable_cap(kvm_state, KVM_CAP_PPC_ENABLE_HCALL, 0,
2163 H_PUT_TCE_INDIRECT, 1);
2164 if (!ret) {
2165 ret = kvm_vm_enable_cap(kvm_state, KVM_CAP_PPC_ENABLE_HCALL, 0,
2166 H_STUFF_TCE, 1);
2167 }
2168
2169 return ret;
2170}
2171
Alexey Kardashevskiyd6ee2a72017-03-10 12:41:13 +11002172void *kvmppc_create_spapr_tce(uint32_t liobn, uint32_t page_shift,
2173 uint64_t bus_offset, uint32_t nb_table,
2174 int *pfd, bool need_vfio)
David Gibson0f5cb292011-09-29 21:39:12 +00002175{
David Gibson0f5cb292011-09-29 21:39:12 +00002176 long len;
2177 int fd;
2178 void *table;
2179
David Gibsonc995e942019-03-21 21:47:25 +11002180 /*
2181 * Must set fd to -1 so we don't try to munmap when called for
David Gibsonb5aec392012-02-27 17:18:07 +00002182 * destroying the table, which the upper layers -will- do
2183 */
2184 *pfd = -1;
David Gibson6a81dd12015-09-30 13:42:55 +10002185 if (!cap_spapr_tce || (need_vfio && !cap_spapr_vfio)) {
David Gibson0f5cb292011-09-29 21:39:12 +00002186 return NULL;
2187 }
2188
Alexey Kardashevskiyd6ee2a72017-03-10 12:41:13 +11002189 if (cap_spapr_tce_64) {
2190 struct kvm_create_spapr_tce_64 args = {
2191 .liobn = liobn,
2192 .page_shift = page_shift,
2193 .offset = bus_offset >> page_shift,
2194 .size = nb_table,
2195 .flags = 0
2196 };
2197 fd = kvm_vm_ioctl(kvm_state, KVM_CREATE_SPAPR_TCE_64, &args);
2198 if (fd < 0) {
2199 fprintf(stderr,
2200 "KVM: Failed to create TCE64 table for liobn 0x%x\n",
2201 liobn);
2202 return NULL;
2203 }
2204 } else if (cap_spapr_tce) {
2205 uint64_t window_size = (uint64_t) nb_table << page_shift;
2206 struct kvm_create_spapr_tce args = {
2207 .liobn = liobn,
2208 .window_size = window_size,
2209 };
2210 if ((window_size != args.window_size) || bus_offset) {
2211 return NULL;
2212 }
2213 fd = kvm_vm_ioctl(kvm_state, KVM_CREATE_SPAPR_TCE, &args);
2214 if (fd < 0) {
2215 fprintf(stderr, "KVM: Failed to create TCE table for liobn 0x%x\n",
2216 liobn);
2217 return NULL;
2218 }
2219 } else {
David Gibson0f5cb292011-09-29 21:39:12 +00002220 return NULL;
2221 }
2222
Alexey Kardashevskiyd6ee2a72017-03-10 12:41:13 +11002223 len = nb_table * sizeof(uint64_t);
David Gibson0f5cb292011-09-29 21:39:12 +00002224 /* FIXME: round this up to page size */
2225
David Gibsonc995e942019-03-21 21:47:25 +11002226 table = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
David Gibson0f5cb292011-09-29 21:39:12 +00002227 if (table == MAP_FAILED) {
David Gibsonb5aec392012-02-27 17:18:07 +00002228 fprintf(stderr, "KVM: Failed to map TCE table for liobn 0x%x\n",
2229 liobn);
David Gibson0f5cb292011-09-29 21:39:12 +00002230 close(fd);
2231 return NULL;
2232 }
2233
2234 *pfd = fd;
2235 return table;
2236}
2237
Alexey Kardashevskiy523e7b82014-05-27 15:36:35 +10002238int kvmppc_remove_spapr_tce(void *table, int fd, uint32_t nb_table)
David Gibson0f5cb292011-09-29 21:39:12 +00002239{
2240 long len;
2241
2242 if (fd < 0) {
2243 return -1;
2244 }
2245
Alexey Kardashevskiy523e7b82014-05-27 15:36:35 +10002246 len = nb_table * sizeof(uint64_t);
David Gibson0f5cb292011-09-29 21:39:12 +00002247 if ((munmap(table, len) < 0) ||
2248 (close(fd) < 0)) {
David Gibsonb5aec392012-02-27 17:18:07 +00002249 fprintf(stderr, "KVM: Unexpected error removing TCE table: %s",
2250 strerror(errno));
David Gibson0f5cb292011-09-29 21:39:12 +00002251 /* Leak the table */
2252 }
2253
2254 return 0;
2255}
2256
David Gibson7f763a52012-09-12 16:57:12 +00002257int kvmppc_reset_htab(int shift_hint)
2258{
2259 uint32_t shift = shift_hint;
2260
David Gibsonace9a2c2012-09-19 21:08:42 +00002261 if (!kvm_enabled()) {
2262 /* Full emulation, tell caller to allocate htab itself */
2263 return 0;
2264 }
Greg Kurz6977afd2017-09-14 21:25:43 +02002265 if (kvm_vm_check_extension(kvm_state, KVM_CAP_PPC_ALLOC_HTAB)) {
David Gibson7f763a52012-09-12 16:57:12 +00002266 int ret;
2267 ret = kvm_vm_ioctl(kvm_state, KVM_PPC_ALLOCATE_HTAB, &shift);
David Gibsonace9a2c2012-09-19 21:08:42 +00002268 if (ret == -ENOTTY) {
David Gibsonc995e942019-03-21 21:47:25 +11002269 /*
2270 * At least some versions of PR KVM advertise the
David Gibsonace9a2c2012-09-19 21:08:42 +00002271 * capability, but don't implement the ioctl(). Oops.
2272 * Return 0 so that we allocate the htab in qemu, as is
David Gibsonc995e942019-03-21 21:47:25 +11002273 * correct for PR.
2274 */
David Gibsonace9a2c2012-09-19 21:08:42 +00002275 return 0;
2276 } else if (ret < 0) {
David Gibson7f763a52012-09-12 16:57:12 +00002277 return ret;
2278 }
2279 return shift;
2280 }
2281
David Gibsonc995e942019-03-21 21:47:25 +11002282 /*
2283 * We have a kernel that predates the htab reset calls. For PR
David Gibsonace9a2c2012-09-19 21:08:42 +00002284 * KVM, we need to allocate the htab ourselves, for an HV KVM of
David Gibsonc995e942019-03-21 21:47:25 +11002285 * this era, it has allocated a 16MB fixed size hash table
2286 * already.
2287 */
Thomas Huth96c9cff2016-09-29 12:48:06 +02002288 if (kvmppc_is_pr(kvm_state)) {
David Gibsonace9a2c2012-09-19 21:08:42 +00002289 /* PR - tell caller to allocate htab */
2290 return 0;
2291 } else {
2292 /* HV - assume 16MB kernel allocated htab */
2293 return 24;
2294 }
David Gibson7f763a52012-09-12 16:57:12 +00002295}
2296
David Gibsona1e98582011-10-12 22:40:32 +00002297static inline uint32_t mfpvr(void)
2298{
2299 uint32_t pvr;
2300
2301 asm ("mfpvr %0"
2302 : "=r"(pvr));
2303 return pvr;
2304}
2305
David Gibsona7342582011-10-17 18:15:41 +00002306static void alter_insns(uint64_t *word, uint64_t flags, bool on)
2307{
2308 if (on) {
2309 *word |= flags;
2310 } else {
2311 *word &= ~flags;
2312 }
2313}
2314
Andreas Färber2985b862013-01-06 08:31:30 +00002315static void kvmppc_host_cpu_class_init(ObjectClass *oc, void *data)
2316{
2317 PowerPCCPUClass *pcc = POWERPC_CPU_CLASS(oc);
David Gibson0cbad812013-04-07 19:08:19 +00002318 uint32_t dcache_size = kvmppc_read_int_cpu_dt("d-cache-size");
2319 uint32_t icache_size = kvmppc_read_int_cpu_dt("i-cache-size");
David Gibsona1e98582011-10-12 22:40:32 +00002320
Andreas Färbercfe34f42013-02-17 23:16:41 +00002321 /* Now fix up the class with information we can query from the host */
Alexey Kardashevskiy3bc9ccc2013-09-27 18:05:03 +10002322 pcc->pvr = mfpvr();
David Gibsona7342582011-10-17 18:15:41 +00002323
David Gibson3f2ca482017-12-11 17:41:34 +11002324 alter_insns(&pcc->insns_flags, PPC_ALTIVEC,
2325 qemu_getauxval(AT_HWCAP) & PPC_FEATURE_HAS_ALTIVEC);
2326 alter_insns(&pcc->insns_flags2, PPC2_VSX,
2327 qemu_getauxval(AT_HWCAP) & PPC_FEATURE_HAS_VSX);
2328 alter_insns(&pcc->insns_flags2, PPC2_DFP,
2329 qemu_getauxval(AT_HWCAP) & PPC_FEATURE_HAS_DFP);
David Gibson0cbad812013-04-07 19:08:19 +00002330
2331 if (dcache_size != -1) {
2332 pcc->l1_dcache_size = dcache_size;
2333 }
2334
2335 if (icache_size != -1) {
2336 pcc->l1_icache_size = icache_size;
2337 }
Sam Bobroffc64abd12017-03-20 10:46:43 +11002338
2339#if defined(TARGET_PPC64)
2340 pcc->radix_page_info = kvm_get_radix_page_info();
David Gibson5f3066d2017-05-10 11:19:16 +10002341
2342 if ((pcc->pvr & 0xffffff00) == CPU_POWERPC_POWER9_DD1) {
2343 /*
2344 * POWER9 DD1 has some bugs which make it not really ISA 3.00
2345 * compliant. More importantly, advertising ISA 3.00
2346 * architected mode may prevent guests from activating
2347 * necessary DD1 workarounds.
2348 */
2349 pcc->pcr_supported &= ~(PCR_COMPAT_3_00 | PCR_COMPAT_2_07
2350 | PCR_COMPAT_2_06 | PCR_COMPAT_2_05);
2351 }
Sam Bobroffc64abd12017-03-20 10:46:43 +11002352#endif /* defined(TARGET_PPC64) */
David Gibsona1e98582011-10-12 22:40:32 +00002353}
2354
Stuart Yoder3b961122013-03-30 06:40:49 +00002355bool kvmppc_has_cap_epr(void)
2356{
2357 return cap_epr;
2358}
2359
Alexander Graf87a91de2014-06-04 12:14:08 +02002360bool kvmppc_has_cap_fixup_hcalls(void)
2361{
2362 return cap_fixup_hcalls;
2363}
2364
Thomas Huthbac3bf22016-09-28 13:16:30 +02002365bool kvmppc_has_cap_htm(void)
2366{
2367 return cap_htm;
2368}
2369
Sam Bobroffcf1c4cc2017-03-20 10:46:44 +11002370bool kvmppc_has_cap_mmu_radix(void)
2371{
2372 return cap_mmu_radix;
2373}
2374
2375bool kvmppc_has_cap_mmu_hash_v3(void)
2376{
2377 return cap_mmu_hash_v3;
2378}
2379
Suraj Jitindar Singh072f4162018-06-12 15:16:29 +10002380static bool kvmppc_power8_host(void)
2381{
2382 bool ret = false;
2383#ifdef TARGET_PPC64
2384 {
2385 uint32_t base_pvr = CPU_POWERPC_POWER_SERVER_MASK & mfpvr();
2386 ret = (base_pvr == CPU_POWERPC_POWER8E_BASE) ||
2387 (base_pvr == CPU_POWERPC_POWER8NVL_BASE) ||
2388 (base_pvr == CPU_POWERPC_POWER8_BASE);
2389 }
2390#endif /* TARGET_PPC64 */
2391 return ret;
2392}
2393
Suraj Jitindar Singh8fea7042018-05-11 16:25:07 +10002394static int parse_cap_ppc_safe_cache(struct kvm_ppc_cpu_char c)
2395{
Suraj Jitindar Singh072f4162018-06-12 15:16:29 +10002396 bool l1d_thread_priv_req = !kvmppc_power8_host();
2397
Suraj Jitindar Singh8fea7042018-05-11 16:25:07 +10002398 if (~c.behaviour & c.behaviour_mask & H_CPU_BEHAV_L1D_FLUSH_PR) {
2399 return 2;
Suraj Jitindar Singh072f4162018-06-12 15:16:29 +10002400 } else if ((!l1d_thread_priv_req ||
2401 c.character & c.character_mask & H_CPU_CHAR_L1D_THREAD_PRIV) &&
Suraj Jitindar Singh8fea7042018-05-11 16:25:07 +10002402 (c.character & c.character_mask
2403 & (H_CPU_CHAR_L1D_FLUSH_ORI30 | H_CPU_CHAR_L1D_FLUSH_TRIG2))) {
2404 return 1;
2405 }
2406
2407 return 0;
2408}
2409
2410static int parse_cap_ppc_safe_bounds_check(struct kvm_ppc_cpu_char c)
2411{
2412 if (~c.behaviour & c.behaviour_mask & H_CPU_BEHAV_BNDS_CHK_SPEC_BAR) {
2413 return 2;
2414 } else if (c.character & c.character_mask & H_CPU_CHAR_SPEC_BAR_ORI31) {
2415 return 1;
2416 }
2417
2418 return 0;
2419}
2420
2421static int parse_cap_ppc_safe_indirect_branch(struct kvm_ppc_cpu_char c)
2422{
Suraj Jitindar Singh399b2892019-03-01 14:19:11 +11002423 if ((~c.behaviour & c.behaviour_mask & H_CPU_BEHAV_FLUSH_COUNT_CACHE) &&
2424 (~c.character & c.character_mask & H_CPU_CHAR_CACHE_COUNT_DIS) &&
2425 (~c.character & c.character_mask & H_CPU_CHAR_BCCTRL_SERIALISED)) {
2426 return SPAPR_CAP_FIXED_NA;
2427 } else if (c.behaviour & c.behaviour_mask & H_CPU_BEHAV_FLUSH_COUNT_CACHE) {
2428 return SPAPR_CAP_WORKAROUND;
2429 } else if (c.character & c.character_mask & H_CPU_CHAR_CACHE_COUNT_DIS) {
Suraj Jitindar Singh8fea7042018-05-11 16:25:07 +10002430 return SPAPR_CAP_FIXED_CCD;
2431 } else if (c.character & c.character_mask & H_CPU_CHAR_BCCTRL_SERIALISED) {
2432 return SPAPR_CAP_FIXED_IBS;
2433 }
2434
2435 return 0;
2436}
2437
Suraj Jitindar Singh8ff43ee2019-03-01 14:19:12 +11002438static int parse_cap_ppc_count_cache_flush_assist(struct kvm_ppc_cpu_char c)
2439{
2440 if (c.character & c.character_mask & H_CPU_CHAR_BCCTR_FLUSH_ASSIST) {
2441 return 1;
2442 }
2443 return 0;
2444}
2445
Cédric Le Goater38afd772019-05-13 10:42:33 +02002446bool kvmppc_has_cap_xive(void)
2447{
2448 return cap_xive;
2449}
2450
Suraj Jitindar Singh8acc2ae2018-01-19 15:59:59 +11002451static void kvmppc_get_cpu_characteristics(KVMState *s)
2452{
2453 struct kvm_ppc_cpu_char c;
2454 int ret;
2455
2456 /* Assume broken */
2457 cap_ppc_safe_cache = 0;
2458 cap_ppc_safe_bounds_check = 0;
2459 cap_ppc_safe_indirect_branch = 0;
2460
2461 ret = kvm_vm_check_extension(s, KVM_CAP_PPC_GET_CPU_CHAR);
2462 if (!ret) {
2463 return;
2464 }
2465 ret = kvm_vm_ioctl(s, KVM_PPC_GET_CPU_CHAR, &c);
2466 if (ret < 0) {
2467 return;
2468 }
Suraj Jitindar Singh8fea7042018-05-11 16:25:07 +10002469
2470 cap_ppc_safe_cache = parse_cap_ppc_safe_cache(c);
2471 cap_ppc_safe_bounds_check = parse_cap_ppc_safe_bounds_check(c);
2472 cap_ppc_safe_indirect_branch = parse_cap_ppc_safe_indirect_branch(c);
Suraj Jitindar Singh8ff43ee2019-03-01 14:19:12 +11002473 cap_ppc_count_cache_flush_assist =
2474 parse_cap_ppc_count_cache_flush_assist(c);
Suraj Jitindar Singh8acc2ae2018-01-19 15:59:59 +11002475}
2476
2477int kvmppc_get_cap_safe_cache(void)
2478{
2479 return cap_ppc_safe_cache;
2480}
2481
2482int kvmppc_get_cap_safe_bounds_check(void)
2483{
2484 return cap_ppc_safe_bounds_check;
2485}
2486
2487int kvmppc_get_cap_safe_indirect_branch(void)
2488{
2489 return cap_ppc_safe_indirect_branch;
2490}
2491
Suraj Jitindar Singh8ff43ee2019-03-01 14:19:12 +11002492int kvmppc_get_cap_count_cache_flush_assist(void)
2493{
2494 return cap_ppc_count_cache_flush_assist;
2495}
2496
Suraj Jitindar Singhb9a477b2018-10-08 14:25:39 +11002497bool kvmppc_has_cap_nested_kvm_hv(void)
2498{
2499 return !!cap_ppc_nested_kvm_hv;
2500}
2501
2502int kvmppc_set_cap_nested_kvm_hv(int enable)
2503{
2504 return kvm_vm_enable_cap(kvm_state, KVM_CAP_PPC_NESTED_HV, 0, enable);
2505}
2506
Alexey Kardashevskiy9ded7802018-02-06 11:08:24 -07002507bool kvmppc_has_cap_spapr_vfio(void)
2508{
2509 return cap_spapr_vfio;
2510}
2511
Suraj Jitindar Singh7d050522019-03-01 13:43:16 +11002512int kvmppc_get_cap_large_decr(void)
2513{
2514 return cap_large_decr;
2515}
2516
2517int kvmppc_enable_cap_large_decr(PowerPCCPU *cpu, int enable)
2518{
2519 CPUState *cs = CPU(cpu);
2520 uint64_t lpcr;
2521
2522 kvm_get_one_reg(cs, KVM_REG_PPC_LPCR_64, &lpcr);
2523 /* Do we need to modify the LPCR? */
2524 if (!!(lpcr & LPCR_LD) != !!enable) {
2525 if (enable) {
2526 lpcr |= LPCR_LD;
2527 } else {
2528 lpcr &= ~LPCR_LD;
2529 }
2530 kvm_set_one_reg(cs, KVM_REG_PPC_LPCR_64, &lpcr);
2531 kvm_get_one_reg(cs, KVM_REG_PPC_LPCR_64, &lpcr);
2532
2533 if (!!(lpcr & LPCR_LD) != !!enable) {
2534 return -1;
2535 }
2536 }
2537
2538 return 0;
2539}
2540
Thomas Huth52b25192016-06-07 17:39:38 +02002541PowerPCCPUClass *kvm_ppc_get_host_cpu_class(void)
2542{
2543 uint32_t host_pvr = mfpvr();
2544 PowerPCCPUClass *pvr_pcc;
2545
2546 pvr_pcc = ppc_cpu_class_by_pvr(host_pvr);
2547 if (pvr_pcc == NULL) {
2548 pvr_pcc = ppc_cpu_class_by_pvr_mask(host_pvr);
2549 }
2550
2551 return pvr_pcc;
2552}
2553
David Gibson165dc3e2019-10-30 17:20:35 +01002554static void pseries_machine_class_fixup(ObjectClass *oc, void *opaque)
2555{
2556 MachineClass *mc = MACHINE_CLASS(oc);
2557
2558 mc->default_cpu_type = TYPE_HOST_POWERPC_CPU;
2559}
2560
2561static int kvm_ppc_register_host_cpu_type(void)
Andreas Färber5ba45762013-02-23 11:22:12 +00002562{
2563 TypeInfo type_info = {
2564 .name = TYPE_HOST_POWERPC_CPU,
Andreas Färber5ba45762013-02-23 11:22:12 +00002565 .class_init = kvmppc_host_cpu_class_init,
2566 };
Andreas Färber5ba45762013-02-23 11:22:12 +00002567 PowerPCCPUClass *pvr_pcc;
Greg Kurz92e926e2017-07-05 10:49:52 +02002568 ObjectClass *oc;
Alexey Kardashevskiy5b79b1c2014-04-12 03:34:25 +10002569 DeviceClass *dc;
Thomas Huth715d4b92017-01-31 14:11:58 +01002570 int i;
Andreas Färber5ba45762013-02-23 11:22:12 +00002571
Thomas Huth52b25192016-06-07 17:39:38 +02002572 pvr_pcc = kvm_ppc_get_host_cpu_class();
Alexey Kardashevskiy3bc9ccc2013-09-27 18:05:03 +10002573 if (pvr_pcc == NULL) {
Andreas Färber5ba45762013-02-23 11:22:12 +00002574 return -1;
2575 }
2576 type_info.parent = object_class_get_name(OBJECT_CLASS(pvr_pcc));
2577 type_register(&type_info);
David Gibson165dc3e2019-10-30 17:20:35 +01002578 /* override TCG default cpu type with 'host' cpu model */
2579 object_class_foreach(pseries_machine_class_fixup, TYPE_SPAPR_MACHINE,
2580 false, NULL);
Alexey Kardashevskiy5b79b1c2014-04-12 03:34:25 +10002581
Greg Kurz92e926e2017-07-05 10:49:52 +02002582 oc = object_class_by_name(type_info.name);
2583 g_assert(oc);
2584
Thomas Huth715d4b92017-01-31 14:11:58 +01002585 /*
2586 * Update generic CPU family class alias (e.g. on a POWER8NVL host,
2587 * we want "POWER8" to be a "family" alias that points to the current
2588 * host CPU type, too)
2589 */
2590 dc = DEVICE_CLASS(ppc_cpu_get_family_class(pvr_pcc));
2591 for (i = 0; ppc_cpu_aliases[i].alias != NULL; i++) {
Igor Mammedovc5354f52017-08-30 15:24:30 +02002592 if (strcasecmp(ppc_cpu_aliases[i].alias, dc->desc) == 0) {
Thomas Huth715d4b92017-01-31 14:11:58 +01002593 char *suffix;
2594
2595 ppc_cpu_aliases[i].model = g_strdup(object_class_get_name(oc));
Igor Mammedovc9137062017-08-30 15:24:29 +02002596 suffix = strstr(ppc_cpu_aliases[i].model, POWERPC_CPU_TYPE_SUFFIX);
Thomas Huth715d4b92017-01-31 14:11:58 +01002597 if (suffix) {
2598 *suffix = 0;
2599 }
Thomas Huth715d4b92017-01-31 14:11:58 +01002600 break;
2601 }
2602 }
2603
Andreas Färber5ba45762013-02-23 11:22:12 +00002604 return 0;
2605}
2606
David Gibsonfeaa64c2013-09-26 16:18:35 +10002607int kvmppc_define_rtas_kernel_token(uint32_t token, const char *function)
2608{
2609 struct kvm_rtas_token_args args = {
2610 .token = token,
2611 };
2612
2613 if (!kvm_check_extension(kvm_state, KVM_CAP_PPC_RTAS)) {
2614 return -ENOENT;
2615 }
2616
Cédric Le Goater7701aee2019-06-15 10:12:52 +02002617 strncpy(args.name, function, sizeof(args.name) - 1);
David Gibsonfeaa64c2013-09-26 16:18:35 +10002618
2619 return kvm_vm_ioctl(kvm_state, KVM_PPC_RTAS_DEFINE_TOKEN, &args);
2620}
David Gibson12b11432012-04-04 15:02:05 +10002621
Greg Kurz14b0d742017-09-15 15:16:20 +02002622int kvmppc_get_htab_fd(bool write, uint64_t index, Error **errp)
Alexey Kardashevskiye68cb8b2013-07-18 14:33:03 -05002623{
2624 struct kvm_get_htab_fd s = {
2625 .flags = write ? KVM_GET_HTAB_WRITE : 0,
Greg Kurz14b0d742017-09-15 15:16:20 +02002626 .start_index = index,
Alexey Kardashevskiye68cb8b2013-07-18 14:33:03 -05002627 };
Greg Kurz82be8e72017-09-15 15:16:10 +02002628 int ret;
Alexey Kardashevskiye68cb8b2013-07-18 14:33:03 -05002629
2630 if (!cap_htab_fd) {
Greg Kurz14b0d742017-09-15 15:16:20 +02002631 error_setg(errp, "KVM version doesn't support %s the HPT",
2632 write ? "writing" : "reading");
Greg Kurz82be8e72017-09-15 15:16:10 +02002633 return -ENOTSUP;
Alexey Kardashevskiye68cb8b2013-07-18 14:33:03 -05002634 }
2635
Greg Kurz82be8e72017-09-15 15:16:10 +02002636 ret = kvm_vm_ioctl(kvm_state, KVM_PPC_GET_HTAB_FD, &s);
2637 if (ret < 0) {
Greg Kurz14b0d742017-09-15 15:16:20 +02002638 error_setg(errp, "Unable to open fd for %s HPT %s KVM: %s",
2639 write ? "writing" : "reading", write ? "to" : "from",
2640 strerror(errno));
Greg Kurz82be8e72017-09-15 15:16:10 +02002641 return -errno;
2642 }
2643
2644 return ret;
Alexey Kardashevskiye68cb8b2013-07-18 14:33:03 -05002645}
2646
2647int kvmppc_save_htab(QEMUFile *f, int fd, size_t bufsize, int64_t max_ns)
2648{
Alex Blighbc72ad62013-08-21 16:03:08 +01002649 int64_t starttime = qemu_clock_get_ns(QEMU_CLOCK_REALTIME);
Alexey Kardashevskiye68cb8b2013-07-18 14:33:03 -05002650 uint8_t buf[bufsize];
2651 ssize_t rc;
2652
2653 do {
2654 rc = read(fd, buf, bufsize);
2655 if (rc < 0) {
2656 fprintf(stderr, "Error reading data from KVM HTAB fd: %s\n",
2657 strerror(errno));
2658 return rc;
2659 } else if (rc) {
Cédric Le Goatere094c4c2014-11-03 16:14:50 +01002660 uint8_t *buffer = buf;
2661 ssize_t n = rc;
2662 while (n) {
2663 struct kvm_get_htab_header *head =
2664 (struct kvm_get_htab_header *) buffer;
2665 size_t chunksize = sizeof(*head) +
2666 HASH_PTE_SIZE_64 * head->n_valid;
2667
2668 qemu_put_be32(f, head->index);
2669 qemu_put_be16(f, head->n_valid);
2670 qemu_put_be16(f, head->n_invalid);
2671 qemu_put_buffer(f, (void *)(head + 1),
2672 HASH_PTE_SIZE_64 * head->n_valid);
2673
2674 buffer += chunksize;
2675 n -= chunksize;
2676 }
Alexey Kardashevskiye68cb8b2013-07-18 14:33:03 -05002677 }
2678 } while ((rc != 0)
David Gibsonc995e942019-03-21 21:47:25 +11002679 && ((max_ns < 0) ||
2680 ((qemu_clock_get_ns(QEMU_CLOCK_REALTIME) - starttime) < max_ns)));
Alexey Kardashevskiye68cb8b2013-07-18 14:33:03 -05002681
2682 return (rc == 0) ? 1 : 0;
2683}
2684
2685int kvmppc_load_htab_chunk(QEMUFile *f, int fd, uint32_t index,
Greg Kurz0a06e4d2020-10-26 13:40:47 +01002686 uint16_t n_valid, uint16_t n_invalid, Error **errp)
Alexey Kardashevskiye68cb8b2013-07-18 14:33:03 -05002687{
2688 struct kvm_get_htab_header *buf;
David Gibsonc995e942019-03-21 21:47:25 +11002689 size_t chunksize = sizeof(*buf) + n_valid * HASH_PTE_SIZE_64;
Alexey Kardashevskiye68cb8b2013-07-18 14:33:03 -05002690 ssize_t rc;
2691
2692 buf = alloca(chunksize);
Alexey Kardashevskiye68cb8b2013-07-18 14:33:03 -05002693 buf->index = index;
2694 buf->n_valid = n_valid;
2695 buf->n_invalid = n_invalid;
2696
David Gibsonc995e942019-03-21 21:47:25 +11002697 qemu_get_buffer(f, (void *)(buf + 1), HASH_PTE_SIZE_64 * n_valid);
Alexey Kardashevskiye68cb8b2013-07-18 14:33:03 -05002698
2699 rc = write(fd, buf, chunksize);
2700 if (rc < 0) {
Greg Kurz0a06e4d2020-10-26 13:40:47 +01002701 error_setg_errno(errp, errno, "Error writing the KVM hash table");
2702 return -errno;
Alexey Kardashevskiye68cb8b2013-07-18 14:33:03 -05002703 }
2704 if (rc != chunksize) {
2705 /* We should never get a short write on a single chunk */
Greg Kurz0a06e4d2020-10-26 13:40:47 +01002706 error_setg(errp, "Short write while restoring the KVM hash table");
2707 return -ENOSPC;
Alexey Kardashevskiye68cb8b2013-07-18 14:33:03 -05002708 }
2709 return 0;
2710}
2711
Andreas Färber20d695a2012-10-31 06:57:49 +01002712bool kvm_arch_stop_on_emulation_error(CPUState *cpu)
Gleb Natapov4513d922010-05-10 11:21:34 +03002713{
2714 return true;
2715}
Jan Kiszkaa1b87fe2011-02-01 22:15:51 +01002716
Scott Wood82169662013-06-12 17:26:54 +10002717void kvm_arch_init_irq_routing(KVMState *s)
2718{
2719}
Greg Kurzc65f9a02013-12-11 14:15:34 +01002720
David Gibson1ad9f0a2017-02-27 15:34:19 +11002721void kvmppc_read_hptes(ppc_hash_pte64_t *hptes, hwaddr ptex, int n)
Aneesh Kumar K.V7c43bca2014-02-20 18:52:24 +01002722{
David Gibson1ad9f0a2017-02-27 15:34:19 +11002723 int fd, rc;
2724 int i;
Aneesh Kumar K.V7c43bca2014-02-20 18:52:24 +01002725
Greg Kurz14b0d742017-09-15 15:16:20 +02002726 fd = kvmppc_get_htab_fd(false, ptex, &error_abort);
Aneesh Kumar K.V7c43bca2014-02-20 18:52:24 +01002727
David Gibson1ad9f0a2017-02-27 15:34:19 +11002728 i = 0;
2729 while (i < n) {
2730 struct kvm_get_htab_header *hdr;
2731 int m = n < HPTES_PER_GROUP ? n : HPTES_PER_GROUP;
2732 char buf[sizeof(*hdr) + m * HASH_PTE_SIZE_64];
2733
2734 rc = read(fd, buf, sizeof(buf));
2735 if (rc < 0) {
2736 hw_error("kvmppc_read_hptes: Unable to read HPTEs");
2737 }
2738
2739 hdr = (struct kvm_get_htab_header *)buf;
2740 while ((i < n) && ((char *)hdr < (buf + rc))) {
Alexey Kardashevskiya36593e2018-01-11 15:08:32 +11002741 int invalid = hdr->n_invalid, valid = hdr->n_valid;
David Gibson1ad9f0a2017-02-27 15:34:19 +11002742
2743 if (hdr->index != (ptex + i)) {
2744 hw_error("kvmppc_read_hptes: Unexpected HPTE index %"PRIu32
2745 " != (%"HWADDR_PRIu" + %d", hdr->index, ptex, i);
2746 }
2747
Alexey Kardashevskiya36593e2018-01-11 15:08:32 +11002748 if (n - i < valid) {
2749 valid = n - i;
2750 }
2751 memcpy(hptes + i, hdr + 1, HASH_PTE_SIZE_64 * valid);
2752 i += valid;
David Gibson1ad9f0a2017-02-27 15:34:19 +11002753
2754 if ((n - i) < invalid) {
2755 invalid = n - i;
2756 }
2757 memset(hptes + i, 0, invalid * HASH_PTE_SIZE_64);
Alexey Kardashevskiya36593e2018-01-11 15:08:32 +11002758 i += invalid;
David Gibson1ad9f0a2017-02-27 15:34:19 +11002759
2760 hdr = (struct kvm_get_htab_header *)
2761 ((char *)(hdr + 1) + HASH_PTE_SIZE_64 * hdr->n_valid);
2762 }
Aneesh Kumar K.V7c43bca2014-02-20 18:52:24 +01002763 }
2764
David Gibson1ad9f0a2017-02-27 15:34:19 +11002765 close(fd);
Aneesh Kumar K.V7c43bca2014-02-20 18:52:24 +01002766}
2767
David Gibson1ad9f0a2017-02-27 15:34:19 +11002768void kvmppc_write_hpte(hwaddr ptex, uint64_t pte0, uint64_t pte1)
Aneesh Kumar K.V7c43bca2014-02-20 18:52:24 +01002769{
David Gibson1ad9f0a2017-02-27 15:34:19 +11002770 int fd, rc;
David Gibson1ad9f0a2017-02-27 15:34:19 +11002771 struct {
2772 struct kvm_get_htab_header hdr;
2773 uint64_t pte0;
2774 uint64_t pte1;
2775 } buf;
Aneesh Kumar K.Vc1385932014-02-20 18:52:38 +01002776
Greg Kurz14b0d742017-09-15 15:16:20 +02002777 fd = kvmppc_get_htab_fd(true, 0 /* Ignored */, &error_abort);
Aneesh Kumar K.Vc1385932014-02-20 18:52:38 +01002778
David Gibson1ad9f0a2017-02-27 15:34:19 +11002779 buf.hdr.n_valid = 1;
2780 buf.hdr.n_invalid = 0;
2781 buf.hdr.index = ptex;
2782 buf.pte0 = cpu_to_be64(pte0);
2783 buf.pte1 = cpu_to_be64(pte1);
2784
2785 rc = write(fd, &buf, sizeof(buf));
2786 if (rc != sizeof(buf)) {
2787 hw_error("kvmppc_write_hpte: Unable to update KVM HPT");
Aneesh Kumar K.Vc1385932014-02-20 18:52:38 +01002788 }
David Gibson1ad9f0a2017-02-27 15:34:19 +11002789 close(fd);
Aneesh Kumar K.Vc1385932014-02-20 18:52:38 +01002790}
Frank Blaschka9e03a042015-01-09 09:04:40 +01002791
2792int kvm_arch_fixup_msi_route(struct kvm_irq_routing_entry *route,
Pavel Fedindc9f06c2015-10-15 16:44:52 +03002793 uint64_t address, uint32_t data, PCIDevice *dev)
Frank Blaschka9e03a042015-01-09 09:04:40 +01002794{
2795 return 0;
2796}
Eric Auger1850b6b2015-06-02 14:56:23 +01002797
Peter Xu38d87492016-07-14 13:56:31 +08002798int kvm_arch_add_msi_route_post(struct kvm_irq_routing_entry *route,
2799 int vector, PCIDevice *dev)
2800{
2801 return 0;
2802}
2803
2804int kvm_arch_release_virq_post(int virq)
2805{
2806 return 0;
2807}
2808
Eric Auger1850b6b2015-06-02 14:56:23 +01002809int kvm_arch_msi_data_to_gsi(uint32_t data)
2810{
2811 return data & 0xffff;
2812}
Thomas Huth4d9392b2015-09-17 10:49:41 +02002813
Aravinda Prasad9ac703a2020-01-31 00:14:19 +05302814#if defined(TARGET_PPC64)
2815int kvm_handle_nmi(PowerPCCPU *cpu, struct kvm_run *run)
2816{
Ganesh Goudar211a7782020-04-08 22:39:44 +05302817 uint16_t flags = run->flags & KVM_RUN_PPC_NMI_DISP_MASK;
Aravinda Prasad81fe70e2020-01-31 00:14:20 +05302818
Aravinda Prasad9ac703a2020-01-31 00:14:19 +05302819 cpu_synchronize_state(CPU(cpu));
2820
Ganesh Goudar211a7782020-04-08 22:39:44 +05302821 spapr_mce_req_event(cpu, flags == KVM_RUN_PPC_NMI_DISP_FULLY_RECOV);
Aravinda Prasad9ac703a2020-01-31 00:14:19 +05302822
2823 return 0;
2824}
2825#endif
2826
Thomas Huth4d9392b2015-09-17 10:49:41 +02002827int kvmppc_enable_hwrng(void)
2828{
2829 if (!kvm_enabled() || !kvm_check_extension(kvm_state, KVM_CAP_PPC_HWRNG)) {
2830 return -1;
2831 }
2832
2833 return kvmppc_enable_hcall(kvm_state, H_RANDOM);
2834}
David Gibson30f4b052017-05-12 15:46:11 +10002835
2836void kvmppc_check_papr_resize_hpt(Error **errp)
2837{
2838 if (!kvm_enabled()) {
David Gibsonb55d2952017-07-12 17:56:55 +10002839 return; /* No KVM, we're good */
2840 }
2841
2842 if (cap_resize_hpt) {
2843 return; /* Kernel has explicit support, we're good */
2844 }
2845
2846 /* Otherwise fallback on looking for PR KVM */
2847 if (kvmppc_is_pr(kvm_state)) {
David Gibson30f4b052017-05-12 15:46:11 +10002848 return;
2849 }
2850
David Gibson30f4b052017-05-12 15:46:11 +10002851 error_setg(errp,
2852 "Hash page table resizing not available with this KVM version");
2853}
David Gibsonb55d2952017-07-12 17:56:55 +10002854
2855int kvmppc_resize_hpt_prepare(PowerPCCPU *cpu, target_ulong flags, int shift)
2856{
2857 CPUState *cs = CPU(cpu);
2858 struct kvm_ppc_resize_hpt rhpt = {
2859 .flags = flags,
2860 .shift = shift,
2861 };
2862
2863 if (!cap_resize_hpt) {
2864 return -ENOSYS;
2865 }
2866
2867 return kvm_vm_ioctl(cs->kvm_state, KVM_PPC_RESIZE_HPT_PREPARE, &rhpt);
2868}
2869
2870int kvmppc_resize_hpt_commit(PowerPCCPU *cpu, target_ulong flags, int shift)
2871{
2872 CPUState *cs = CPU(cpu);
2873 struct kvm_ppc_resize_hpt rhpt = {
2874 .flags = flags,
2875 .shift = shift,
2876 };
2877
2878 if (!cap_resize_hpt) {
2879 return -ENOSYS;
2880 }
2881
2882 return kvm_vm_ioctl(cs->kvm_state, KVM_PPC_RESIZE_HPT_COMMIT, &rhpt);
2883}
2884
Daniel Henrique Barbozac363a372017-08-09 17:43:46 -03002885/*
2886 * This is a helper function to detect a post migration scenario
2887 * in which a guest, running as KVM-HV, freezes in cpu_post_load because
2888 * the guest kernel can't handle a PVR value other than the actual host
2889 * PVR in KVM_SET_SREGS, even if pvr_match() returns true.
2890 *
2891 * If we don't have cap_ppc_pvr_compat and we're not running in PR
2892 * (so, we're HV), return true. The workaround itself is done in
2893 * cpu_post_load.
2894 *
2895 * The order here is important: we'll only check for KVM PR as a
2896 * fallback if the guest kernel can't handle the situation itself.
2897 * We need to avoid as much as possible querying the running KVM type
2898 * in QEMU level.
2899 */
2900bool kvmppc_pvr_workaround_required(PowerPCCPU *cpu)
2901{
2902 CPUState *cs = CPU(cpu);
2903
2904 if (!kvm_enabled()) {
2905 return false;
2906 }
2907
2908 if (cap_ppc_pvr_compat) {
2909 return false;
2910 }
2911
2912 return !kvmppc_is_pr(cs->kvm_state);
2913}
Nikunj A Dadhaniaa84f7172018-09-04 14:54:18 +05302914
2915void kvmppc_set_reg_ppc_online(PowerPCCPU *cpu, unsigned int online)
2916{
2917 CPUState *cs = CPU(cpu);
2918
2919 if (kvm_enabled()) {
2920 kvm_set_one_reg(cs, KVM_REG_PPC_ONLINE, &online);
2921 }
2922}
Greg Kurz97232952019-06-14 13:09:17 +02002923
2924void kvmppc_set_reg_tb_offset(PowerPCCPU *cpu, int64_t tb_offset)
2925{
2926 CPUState *cs = CPU(cpu);
2927
2928 if (kvm_enabled()) {
2929 kvm_set_one_reg(cs, KVM_REG_PPC_TB_OFFSET, &tb_offset);
2930 }
2931}