| 1 |
// SPDX-License-Identifier: GPL-2.0-only |
| 2 |
/* |
| 3 |
* Copyright (C) 2012 - Virtual Open Systems and Columbia University |
| 4 |
* Author: Christoffer Dall <[email protected]> |
| 5 |
*/ |
| 6 |
|
| 7 |
#include <linux/bug.h> |
| 8 |
#include <linux/cpu_pm.h> |
| 9 |
#include <linux/errno.h> |
| 10 |
#include <linux/err.h> |
| 11 |
#include <linux/kvm_host.h> |
| 12 |
#include <linux/list.h> |
| 13 |
#include <linux/module.h> |
| 14 |
#include <linux/vmalloc.h> |
| 15 |
#include <linux/fs.h> |
| 16 |
#include <linux/mman.h> |
| 17 |
#include <linux/sched.h> |
| 18 |
#include <linux/kvm.h> |
| 19 |
#include <linux/kvm_irqfd.h> |
| 20 |
#include <linux/irqbypass.h> |
| 21 |
#include <linux/sched/stat.h> |
| 22 |
#include <linux/psci.h> |
| 23 |
#include <trace/events/kvm.h> |
| 24 |
|
| 25 |
#define CREATE_TRACE_POINTS |
| 26 |
#include "trace_arm.h" |
| 27 |
|
| 28 |
#include <linux/uaccess.h> |
| 29 |
#include <asm/ptrace.h> |
| 30 |
#include <asm/mman.h> |
| 31 |
#include <asm/tlbflush.h> |
| 32 |
#include <asm/cacheflush.h> |
| 33 |
#include <asm/cpufeature.h> |
| 34 |
#include <asm/virt.h> |
| 35 |
#include <asm/kvm_arm.h> |
| 36 |
#include <asm/kvm_asm.h> |
| 37 |
#include <asm/kvm_emulate.h> |
| 38 |
#include <asm/kvm_mmu.h> |
| 39 |
#include <asm/kvm_nested.h> |
| 40 |
#include <asm/kvm_pkvm.h> |
| 41 |
#include <asm/kvm_ptrauth.h> |
| 42 |
#include <asm/sections.h> |
| 43 |
#include <asm/stacktrace/nvhe.h> |
| 44 |
|
| 45 |
#include <kvm/arm_hypercalls.h> |
| 46 |
#include <kvm/arm_pmu.h> |
| 47 |
#include <kvm/arm_psci.h> |
| 48 |
|
| 49 |
#include "sys_regs.h" |
| 50 |
|
| 51 |
static enum kvm_mode kvm_mode = KVM_MODE_DEFAULT; |
| 52 |
|
| 53 |
enum kvm_wfx_trap_policy { |
| 54 |
KVM_WFX_NOTRAP_SINGLE_TASK, /* Default option */ |
| 55 |
KVM_WFX_NOTRAP, |
| 56 |
KVM_WFX_TRAP, |
| 57 |
}; |
| 58 |
|
| 59 |
static enum kvm_wfx_trap_policy kvm_wfi_trap_policy __read_mostly = KVM_WFX_NOTRAP_SINGLE_TASK; |
| 60 |
static enum kvm_wfx_trap_policy kvm_wfe_trap_policy __read_mostly = KVM_WFX_NOTRAP_SINGLE_TASK; |
| 61 |
|
| 62 |
/* |
| 63 |
* Tracks KVM IOCTLs and their associated KVM capabilities. |
| 64 |
*/ |
| 65 |
struct kvm_ioctl_cap_map { |
| 66 |
unsigned int ioctl; |
| 67 |
long ext; |
| 68 |
}; |
| 69 |
|
| 70 |
/* Make KVM_CAP_NR_VCPUS the reference for features we always supported */ |
| 71 |
#define KVM_CAP_ARM_BASIC KVM_CAP_NR_VCPUS |
| 72 |
|
| 73 |
/* |
| 74 |
* Sorted by ioctl to allow for potential binary search, |
| 75 |
* though linear scan is sufficient for this size. |
| 76 |
*/ |
| 77 |
static const struct kvm_ioctl_cap_map vm_ioctl_caps[] = { |
| 78 |
{ KVM_CREATE_IRQCHIP, KVM_CAP_IRQCHIP }, |
| 79 |
{ KVM_ARM_SET_DEVICE_ADDR, KVM_CAP_ARM_SET_DEVICE_ADDR }, |
| 80 |
{ KVM_ARM_MTE_COPY_TAGS, KVM_CAP_ARM_MTE }, |
| 81 |
{ KVM_SET_DEVICE_ATTR, KVM_CAP_DEVICE_CTRL }, |
| 82 |
{ KVM_GET_DEVICE_ATTR, KVM_CAP_DEVICE_CTRL }, |
| 83 |
{ KVM_HAS_DEVICE_ATTR, KVM_CAP_DEVICE_CTRL }, |
| 84 |
{ KVM_ARM_SET_COUNTER_OFFSET, KVM_CAP_COUNTER_OFFSET }, |
| 85 |
{ KVM_ARM_GET_REG_WRITABLE_MASKS, KVM_CAP_ARM_SUPPORTED_REG_MASK_RANGES }, |
| 86 |
{ KVM_ARM_PREFERRED_TARGET, KVM_CAP_ARM_BASIC }, |
| 87 |
}; |
| 88 |
|
| 89 |
/* |
| 90 |
* Set *ext to the capability. |
| 91 |
* Return 0 if found, or -EINVAL if no IOCTL matches. |
| 92 |
*/ |
| 93 |
long kvm_get_cap_for_kvm_ioctl(unsigned int ioctl, long *ext) |
| 94 |
{ |
| 95 |
int i; |
| 96 |
|
| 97 |
for (i = 0; i < ARRAY_SIZE(vm_ioctl_caps); i++) { |
| 98 |
if (vm_ioctl_caps[i].ioctl == ioctl) { |
| 99 |
*ext = vm_ioctl_caps[i].ext; |
| 100 |
return 0; |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
return -EINVAL; |
| 105 |
} |
| 106 |
|
| 107 |
DECLARE_KVM_HYP_PER_CPU(unsigned long, kvm_hyp_vector); |
| 108 |
|
| 109 |
DEFINE_PER_CPU(unsigned long, kvm_arm_hyp_stack_base); |
| 110 |
DECLARE_KVM_NVHE_PER_CPU(struct kvm_nvhe_init_params, kvm_init_params); |
| 111 |
|
| 112 |
DECLARE_KVM_NVHE_PER_CPU(struct kvm_cpu_context, kvm_hyp_ctxt); |
| 113 |
|
| 114 |
static bool vgic_present, kvm_arm_initialised; |
| 115 |
|
| 116 |
static DEFINE_PER_CPU(unsigned char, kvm_hyp_initialized); |
| 117 |
|
| 118 |
bool is_kvm_arm_initialised(void) |
| 119 |
{ |
| 120 |
return kvm_arm_initialised; |
| 121 |
} |
| 122 |
|
| 123 |
int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu) |
| 124 |
{ |
| 125 |
return kvm_vcpu_exiting_guest_mode(vcpu) == IN_GUEST_MODE; |
| 126 |
} |
| 127 |
|
| 128 |
int kvm_vm_ioctl_enable_cap(struct kvm *kvm, |
| 129 |
struct kvm_enable_cap *cap) |
| 130 |
{ |
| 131 |
int r = -EINVAL; |
| 132 |
|
| 133 |
if (cap->flags) |
| 134 |
return -EINVAL; |
| 135 |
|
| 136 |
if (is_protected_kvm_enabled() && !kvm_pkvm_ext_allowed(kvm, cap->cap)) |
| 137 |
return -EINVAL; |
| 138 |
|
| 139 |
switch (cap->cap) { |
| 140 |
case KVM_CAP_ARM_NISV_TO_USER: |
| 141 |
r = 0; |
| 142 |
set_bit(KVM_ARCH_FLAG_RETURN_NISV_IO_ABORT_TO_USER, |
| 143 |
&kvm->arch.flags); |
| 144 |
break; |
| 145 |
case KVM_CAP_ARM_MTE: |
| 146 |
mutex_lock(&kvm->lock); |
| 147 |
if (system_supports_mte() && !kvm->created_vcpus) { |
| 148 |
r = 0; |
| 149 |
set_bit(KVM_ARCH_FLAG_MTE_ENABLED, &kvm->arch.flags); |
| 150 |
} |
| 151 |
mutex_unlock(&kvm->lock); |
| 152 |
break; |
| 153 |
case KVM_CAP_ARM_SYSTEM_SUSPEND: |
| 154 |
r = 0; |
| 155 |
set_bit(KVM_ARCH_FLAG_SYSTEM_SUSPEND_ENABLED, &kvm->arch.flags); |
| 156 |
break; |
| 157 |
case KVM_CAP_ARM_EAGER_SPLIT_CHUNK_SIZE: |
| 158 |
mutex_lock(&kvm->slots_lock); |
| 159 |
/* |
| 160 |
* To keep things simple, allow changing the chunk |
| 161 |
* size only when no memory slots have been created. |
| 162 |
*/ |
| 163 |
if (kvm_are_all_memslots_empty(kvm)) { |
| 164 |
u64 new_cap = cap->args[0]; |
| 165 |
|
| 166 |
if (!new_cap || kvm_is_block_size_supported(new_cap)) { |
| 167 |
r = 0; |
| 168 |
kvm->arch.mmu.split_page_chunk_size = new_cap; |
| 169 |
} |
| 170 |
} |
| 171 |
mutex_unlock(&kvm->slots_lock); |
| 172 |
break; |
| 173 |
case KVM_CAP_ARM_WRITABLE_IMP_ID_REGS: |
| 174 |
mutex_lock(&kvm->lock); |
| 175 |
if (!kvm->created_vcpus) { |
| 176 |
r = 0; |
| 177 |
set_bit(KVM_ARCH_FLAG_WRITABLE_IMP_ID_REGS, &kvm->arch.flags); |
| 178 |
} |
| 179 |
mutex_unlock(&kvm->lock); |
| 180 |
break; |
| 181 |
case KVM_CAP_ARM_SEA_TO_USER: |
| 182 |
r = 0; |
| 183 |
set_bit(KVM_ARCH_FLAG_EXIT_SEA, &kvm->arch.flags); |
| 184 |
break; |
| 185 |
default: |
| 186 |
break; |
| 187 |
} |
| 188 |
|
| 189 |
return r; |
| 190 |
} |
| 191 |
|
| 192 |
static int kvm_arm_default_max_vcpus(void) |
| 193 |
{ |
| 194 |
return vgic_present ? kvm_vgic_get_max_vcpus() : KVM_MAX_VCPUS; |
| 195 |
} |
| 196 |
|
| 197 |
/** |
| 198 |
* kvm_arch_init_vm - initializes a VM data structure |
| 199 |
* @kvm: pointer to the KVM struct |
| 200 |
* @type: kvm device type |
| 201 |
*/ |
| 202 |
int kvm_arch_init_vm(struct kvm *kvm, unsigned long type) |
| 203 |
{ |
| 204 |
int ret; |
| 205 |
|
| 206 |
mutex_init(&kvm->arch.config_lock); |
| 207 |
|
| 208 |
#ifdef CONFIG_LOCKDEP |
| 209 |
/* Clue in lockdep that the config_lock must be taken inside kvm->lock */ |
| 210 |
mutex_lock(&kvm->lock); |
| 211 |
mutex_lock(&kvm->arch.config_lock); |
| 212 |
mutex_unlock(&kvm->arch.config_lock); |
| 213 |
mutex_unlock(&kvm->lock); |
| 214 |
#endif |
| 215 |
|
| 216 |
kvm_init_nested(kvm); |
| 217 |
|
| 218 |
ret = kvm_share_hyp(kvm, kvm + 1); |
| 219 |
if (ret) |
| 220 |
return ret; |
| 221 |
|
| 222 |
if (!zalloc_cpumask_var(&kvm->arch.supported_cpus, GFP_KERNEL_ACCOUNT)) { |
| 223 |
ret = -ENOMEM; |
| 224 |
goto err_unshare_kvm; |
| 225 |
} |
| 226 |
cpumask_copy(kvm->arch.supported_cpus, cpu_possible_mask); |
| 227 |
|
| 228 |
ret = kvm_init_stage2_mmu(kvm, &kvm->arch.mmu, type); |
| 229 |
if (ret) |
| 230 |
goto err_free_cpumask; |
| 231 |
|
| 232 |
if (is_protected_kvm_enabled()) { |
| 233 |
/* |
| 234 |
* If any failures occur after this is successful, make sure to |
| 235 |
* call __pkvm_unreserve_vm to unreserve the VM in hyp. |
| 236 |
*/ |
| 237 |
ret = pkvm_init_host_vm(kvm); |
| 238 |
if (ret) |
| 239 |
goto err_free_cpumask; |
| 240 |
} |
| 241 |
|
| 242 |
kvm_vgic_early_init(kvm); |
| 243 |
|
| 244 |
kvm_timer_init_vm(kvm); |
| 245 |
|
| 246 |
/* The maximum number of VCPUs is limited by the host's GIC model */ |
| 247 |
kvm->max_vcpus = kvm_arm_default_max_vcpus(); |
| 248 |
|
| 249 |
kvm_arm_init_hypercalls(kvm); |
| 250 |
|
| 251 |
bitmap_zero(kvm->arch.vcpu_features, KVM_VCPU_MAX_FEATURES); |
| 252 |
|
| 253 |
return 0; |
| 254 |
|
| 255 |
err_free_cpumask: |
| 256 |
free_cpumask_var(kvm->arch.supported_cpus); |
| 257 |
err_unshare_kvm: |
| 258 |
kvm_unshare_hyp(kvm, kvm + 1); |
| 259 |
return ret; |
| 260 |
} |
| 261 |
|
| 262 |
vm_fault_t kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf) |
| 263 |
{ |
| 264 |
return VM_FAULT_SIGBUS; |
| 265 |
} |
| 266 |
|
| 267 |
void kvm_arch_create_vm_debugfs(struct kvm *kvm) |
| 268 |
{ |
| 269 |
kvm_sys_regs_create_debugfs(kvm); |
| 270 |
kvm_s2_ptdump_create_debugfs(kvm); |
| 271 |
} |
| 272 |
|
| 273 |
static void kvm_destroy_mpidr_data(struct kvm *kvm) |
| 274 |
{ |
| 275 |
struct kvm_mpidr_data *data; |
| 276 |
|
| 277 |
mutex_lock(&kvm->arch.config_lock); |
| 278 |
|
| 279 |
data = rcu_dereference_protected(kvm->arch.mpidr_data, |
| 280 |
lockdep_is_held(&kvm->arch.config_lock)); |
| 281 |
if (data) { |
| 282 |
rcu_assign_pointer(kvm->arch.mpidr_data, NULL); |
| 283 |
synchronize_rcu(); |
| 284 |
kfree(data); |
| 285 |
} |
| 286 |
|
| 287 |
mutex_unlock(&kvm->arch.config_lock); |
| 288 |
} |
| 289 |
|
| 290 |
/** |
| 291 |
* kvm_arch_destroy_vm - destroy the VM data structure |
| 292 |
* @kvm: pointer to the KVM struct |
| 293 |
*/ |
| 294 |
void kvm_arch_destroy_vm(struct kvm *kvm) |
| 295 |
{ |
| 296 |
bitmap_free(kvm->arch.pmu_filter); |
| 297 |
free_cpumask_var(kvm->arch.supported_cpus); |
| 298 |
|
| 299 |
kvm_vgic_destroy(kvm); |
| 300 |
|
| 301 |
if (is_protected_kvm_enabled()) |
| 302 |
pkvm_destroy_hyp_vm(kvm); |
| 303 |
|
| 304 |
kvm_destroy_mpidr_data(kvm); |
| 305 |
|
| 306 |
kfree(kvm->arch.sysreg_masks); |
| 307 |
kvm_destroy_vcpus(kvm); |
| 308 |
|
| 309 |
kvm_unshare_hyp(kvm, kvm + 1); |
| 310 |
|
| 311 |
kvm_arm_teardown_hypercalls(kvm); |
| 312 |
} |
| 313 |
|
| 314 |
static bool kvm_has_full_ptr_auth(void) |
| 315 |
{ |
| 316 |
bool apa, gpa, api, gpi, apa3, gpa3; |
| 317 |
u64 isar1, isar2, val; |
| 318 |
|
| 319 |
/* |
| 320 |
* Check that: |
| 321 |
* |
| 322 |
* - both Address and Generic auth are implemented for a given |
| 323 |
* algorithm (Q5, IMPDEF or Q3) |
| 324 |
* - only a single algorithm is implemented. |
| 325 |
*/ |
| 326 |
if (!system_has_full_ptr_auth()) |
| 327 |
return false; |
| 328 |
|
| 329 |
isar1 = read_sanitised_ftr_reg(SYS_ID_AA64ISAR1_EL1); |
| 330 |
isar2 = read_sanitised_ftr_reg(SYS_ID_AA64ISAR2_EL1); |
| 331 |
|
| 332 |
apa = !!FIELD_GET(ID_AA64ISAR1_EL1_APA_MASK, isar1); |
| 333 |
val = FIELD_GET(ID_AA64ISAR1_EL1_GPA_MASK, isar1); |
| 334 |
gpa = (val == ID_AA64ISAR1_EL1_GPA_IMP); |
| 335 |
|
| 336 |
api = !!FIELD_GET(ID_AA64ISAR1_EL1_API_MASK, isar1); |
| 337 |
val = FIELD_GET(ID_AA64ISAR1_EL1_GPI_MASK, isar1); |
| 338 |
gpi = (val == ID_AA64ISAR1_EL1_GPI_IMP); |
| 339 |
|
| 340 |
apa3 = !!FIELD_GET(ID_AA64ISAR2_EL1_APA3_MASK, isar2); |
| 341 |
val = FIELD_GET(ID_AA64ISAR2_EL1_GPA3_MASK, isar2); |
| 342 |
gpa3 = (val == ID_AA64ISAR2_EL1_GPA3_IMP); |
| 343 |
|
| 344 |
return (apa == gpa && api == gpi && apa3 == gpa3 && |
| 345 |
(apa + api + apa3) == 1); |
| 346 |
} |
| 347 |
|
| 348 |
int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext) |
| 349 |
{ |
| 350 |
int r; |
| 351 |
|
| 352 |
if (is_protected_kvm_enabled() && !kvm_pkvm_ext_allowed(kvm, ext)) |
| 353 |
return 0; |
| 354 |
|
| 355 |
switch (ext) { |
| 356 |
case KVM_CAP_IRQCHIP: |
| 357 |
r = vgic_present; |
| 358 |
break; |
| 359 |
case KVM_CAP_IOEVENTFD: |
| 360 |
case KVM_CAP_USER_MEMORY: |
| 361 |
case KVM_CAP_DESTROY_MEMORY_REGION_WORKS: |
| 362 |
case KVM_CAP_ONE_REG: |
| 363 |
case KVM_CAP_ARM_PSCI: |
| 364 |
case KVM_CAP_ARM_PSCI_0_2: |
| 365 |
case KVM_CAP_READONLY_MEM: |
| 366 |
case KVM_CAP_MP_STATE: |
| 367 |
case KVM_CAP_IMMEDIATE_EXIT: |
| 368 |
case KVM_CAP_VCPU_EVENTS: |
| 369 |
case KVM_CAP_ARM_IRQ_LINE_LAYOUT_2: |
| 370 |
case KVM_CAP_ARM_NISV_TO_USER: |
| 371 |
case KVM_CAP_ARM_INJECT_EXT_DABT: |
| 372 |
case KVM_CAP_SET_GUEST_DEBUG: |
| 373 |
case KVM_CAP_VCPU_ATTRIBUTES: |
| 374 |
case KVM_CAP_PTP_KVM: |
| 375 |
case KVM_CAP_ARM_SYSTEM_SUSPEND: |
| 376 |
case KVM_CAP_IRQFD_RESAMPLE: |
| 377 |
case KVM_CAP_COUNTER_OFFSET: |
| 378 |
case KVM_CAP_ARM_WRITABLE_IMP_ID_REGS: |
| 379 |
case KVM_CAP_ARM_SEA_TO_USER: |
| 380 |
r = 1; |
| 381 |
break; |
| 382 |
case KVM_CAP_SET_GUEST_DEBUG2: |
| 383 |
return KVM_GUESTDBG_VALID_MASK; |
| 384 |
case KVM_CAP_ARM_SET_DEVICE_ADDR: |
| 385 |
r = 1; |
| 386 |
break; |
| 387 |
case KVM_CAP_NR_VCPUS: |
| 388 |
/* |
| 389 |
* ARM64 treats KVM_CAP_NR_CPUS differently from all other |
| 390 |
* architectures, as it does not always bound it to |
| 391 |
* KVM_CAP_MAX_VCPUS. It should not matter much because |
| 392 |
* this is just an advisory value. |
| 393 |
*/ |
| 394 |
r = min_t(unsigned int, num_online_cpus(), |
| 395 |
kvm_arm_default_max_vcpus()); |
| 396 |
break; |
| 397 |
case KVM_CAP_MAX_VCPUS: |
| 398 |
case KVM_CAP_MAX_VCPU_ID: |
| 399 |
if (kvm) |
| 400 |
r = kvm->max_vcpus; |
| 401 |
else |
| 402 |
r = kvm_arm_default_max_vcpus(); |
| 403 |
break; |
| 404 |
case KVM_CAP_MSI_DEVID: |
| 405 |
if (!kvm) |
| 406 |
r = -EINVAL; |
| 407 |
else |
| 408 |
r = kvm->arch.vgic.msis_require_devid; |
| 409 |
break; |
| 410 |
case KVM_CAP_ARM_USER_IRQ: |
| 411 |
/* |
| 412 |
* 1: EL1_VTIMER, EL1_PTIMER, and PMU. |
| 413 |
* (bump this number if adding more devices) |
| 414 |
*/ |
| 415 |
r = 1; |
| 416 |
break; |
| 417 |
case KVM_CAP_ARM_MTE: |
| 418 |
r = system_supports_mte(); |
| 419 |
break; |
| 420 |
case KVM_CAP_STEAL_TIME: |
| 421 |
r = kvm_arm_pvtime_supported(); |
| 422 |
break; |
| 423 |
case KVM_CAP_ARM_EL1_32BIT: |
| 424 |
r = cpus_have_final_cap(ARM64_HAS_32BIT_EL1); |
| 425 |
break; |
| 426 |
case KVM_CAP_ARM_EL2: |
| 427 |
r = cpus_have_final_cap(ARM64_HAS_NESTED_VIRT); |
| 428 |
break; |
| 429 |
case KVM_CAP_ARM_EL2_E2H0: |
| 430 |
r = cpus_have_final_cap(ARM64_HAS_HCR_NV1); |
| 431 |
break; |
| 432 |
case KVM_CAP_GUEST_DEBUG_HW_BPS: |
| 433 |
r = get_num_brps(); |
| 434 |
break; |
| 435 |
case KVM_CAP_GUEST_DEBUG_HW_WPS: |
| 436 |
r = get_num_wrps(); |
| 437 |
break; |
| 438 |
case KVM_CAP_ARM_PMU_V3: |
| 439 |
r = kvm_supports_guest_pmuv3(); |
| 440 |
break; |
| 441 |
case KVM_CAP_ARM_INJECT_SERROR_ESR: |
| 442 |
r = cpus_have_final_cap(ARM64_HAS_RAS_EXTN); |
| 443 |
break; |
| 444 |
case KVM_CAP_ARM_VM_IPA_SIZE: |
| 445 |
r = get_kvm_ipa_limit(); |
| 446 |
break; |
| 447 |
case KVM_CAP_ARM_SVE: |
| 448 |
r = system_supports_sve(); |
| 449 |
break; |
| 450 |
case KVM_CAP_ARM_PTRAUTH_ADDRESS: |
| 451 |
case KVM_CAP_ARM_PTRAUTH_GENERIC: |
| 452 |
r = kvm_has_full_ptr_auth(); |
| 453 |
break; |
| 454 |
case KVM_CAP_ARM_EAGER_SPLIT_CHUNK_SIZE: |
| 455 |
if (kvm) |
| 456 |
r = kvm->arch.mmu.split_page_chunk_size; |
| 457 |
else |
| 458 |
r = KVM_ARM_EAGER_SPLIT_CHUNK_SIZE_DEFAULT; |
| 459 |
break; |
| 460 |
case KVM_CAP_ARM_SUPPORTED_BLOCK_SIZES: |
| 461 |
r = kvm_supported_block_sizes(); |
| 462 |
break; |
| 463 |
case KVM_CAP_ARM_SUPPORTED_REG_MASK_RANGES: |
| 464 |
r = BIT(0); |
| 465 |
break; |
| 466 |
case KVM_CAP_ARM_CACHEABLE_PFNMAP_SUPPORTED: |
| 467 |
if (!kvm) |
| 468 |
r = -EINVAL; |
| 469 |
else |
| 470 |
r = kvm_supports_cacheable_pfnmap(); |
| 471 |
break; |
| 472 |
|
| 473 |
default: |
| 474 |
r = 0; |
| 475 |
} |
| 476 |
|
| 477 |
return r; |
| 478 |
} |
| 479 |
|
| 480 |
long kvm_arch_dev_ioctl(struct file *filp, |
| 481 |
unsigned int ioctl, unsigned long arg) |
| 482 |
{ |
| 483 |
return -EINVAL; |
| 484 |
} |
| 485 |
|
| 486 |
struct kvm *kvm_arch_alloc_vm(void) |
| 487 |
{ |
| 488 |
size_t sz = sizeof(struct kvm); |
| 489 |
|
| 490 |
if (!has_vhe()) |
| 491 |
return kzalloc(sz, GFP_KERNEL_ACCOUNT); |
| 492 |
|
| 493 |
return kvzalloc(sz, GFP_KERNEL_ACCOUNT); |
| 494 |
} |
| 495 |
|
| 496 |
int kvm_arch_vcpu_precreate(struct kvm *kvm, unsigned int id) |
| 497 |
{ |
| 498 |
if (irqchip_in_kernel(kvm) && vgic_initialized(kvm)) |
| 499 |
return -EBUSY; |
| 500 |
|
| 501 |
if (id >= kvm->max_vcpus) |
| 502 |
return -EINVAL; |
| 503 |
|
| 504 |
return 0; |
| 505 |
} |
| 506 |
|
| 507 |
int kvm_arch_vcpu_create(struct kvm_vcpu *vcpu) |
| 508 |
{ |
| 509 |
int err; |
| 510 |
|
| 511 |
spin_lock_init(&vcpu->arch.mp_state_lock); |
| 512 |
|
| 513 |
#ifdef CONFIG_LOCKDEP |
| 514 |
/* Inform lockdep that the config_lock is acquired after vcpu->mutex */ |
| 515 |
mutex_lock(&vcpu->mutex); |
| 516 |
mutex_lock(&vcpu->kvm->arch.config_lock); |
| 517 |
mutex_unlock(&vcpu->kvm->arch.config_lock); |
| 518 |
mutex_unlock(&vcpu->mutex); |
| 519 |
#endif |
| 520 |
|
| 521 |
/* Force users to call KVM_ARM_VCPU_INIT */ |
| 522 |
vcpu_clear_flag(vcpu, VCPU_INITIALIZED); |
| 523 |
|
| 524 |
vcpu->arch.mmu_page_cache.gfp_zero = __GFP_ZERO; |
| 525 |
|
| 526 |
/* Set up the timer */ |
| 527 |
kvm_timer_vcpu_init(vcpu); |
| 528 |
|
| 529 |
kvm_pmu_vcpu_init(vcpu); |
| 530 |
|
| 531 |
kvm_arm_pvtime_vcpu_init(&vcpu->arch); |
| 532 |
|
| 533 |
vcpu->arch.hw_mmu = &vcpu->kvm->arch.mmu; |
| 534 |
|
| 535 |
/* |
| 536 |
* This vCPU may have been created after mpidr_data was initialized. |
| 537 |
* Throw out the pre-computed mappings if that is the case which forces |
| 538 |
* KVM to fall back to iteratively searching the vCPUs. |
| 539 |
*/ |
| 540 |
kvm_destroy_mpidr_data(vcpu->kvm); |
| 541 |
|
| 542 |
err = kvm_vgic_vcpu_init(vcpu); |
| 543 |
if (err) { |
| 544 |
kvm_vgic_vcpu_destroy(vcpu); |
| 545 |
return err; |
| 546 |
} |
| 547 |
|
| 548 |
err = kvm_share_hyp(vcpu, vcpu + 1); |
| 549 |
if (err) |
| 550 |
kvm_vgic_vcpu_destroy(vcpu); |
| 551 |
|
| 552 |
return err; |
| 553 |
} |
| 554 |
|
| 555 |
void kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu) |
| 556 |
{ |
| 557 |
} |
| 558 |
|
| 559 |
void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu) |
| 560 |
{ |
| 561 |
if (!is_protected_kvm_enabled()) |
| 562 |
kvm_mmu_free_memory_cache(&vcpu->arch.mmu_page_cache); |
| 563 |
else |
| 564 |
free_hyp_memcache(&vcpu->arch.pkvm_memcache); |
| 565 |
kvm_timer_vcpu_terminate(vcpu); |
| 566 |
kvm_pmu_vcpu_destroy(vcpu); |
| 567 |
kvm_vgic_vcpu_destroy(vcpu); |
| 568 |
kvm_arm_vcpu_destroy(vcpu); |
| 569 |
} |
| 570 |
|
| 571 |
void kvm_arch_vcpu_blocking(struct kvm_vcpu *vcpu) |
| 572 |
{ |
| 573 |
|
| 574 |
} |
| 575 |
|
| 576 |
void kvm_arch_vcpu_unblocking(struct kvm_vcpu *vcpu) |
| 577 |
{ |
| 578 |
|
| 579 |
} |
| 580 |
|
| 581 |
static void vcpu_set_pauth_traps(struct kvm_vcpu *vcpu) |
| 582 |
{ |
| 583 |
if (vcpu_has_ptrauth(vcpu) && !is_protected_kvm_enabled()) { |
| 584 |
/* |
| 585 |
* Either we're running an L2 guest, and the API/APK bits come |
| 586 |
* from L1's HCR_EL2, or API/APK are both set. |
| 587 |
*/ |
| 588 |
if (unlikely(is_nested_ctxt(vcpu))) { |
| 589 |
u64 val; |
| 590 |
|
| 591 |
val = __vcpu_sys_reg(vcpu, HCR_EL2); |
| 592 |
val &= (HCR_API | HCR_APK); |
| 593 |
vcpu->arch.hcr_el2 &= ~(HCR_API | HCR_APK); |
| 594 |
vcpu->arch.hcr_el2 |= val; |
| 595 |
} else { |
| 596 |
vcpu->arch.hcr_el2 |= (HCR_API | HCR_APK); |
| 597 |
} |
| 598 |
|
| 599 |
/* |
| 600 |
* Save the host keys if there is any chance for the guest |
| 601 |
* to use pauth, as the entry code will reload the guest |
| 602 |
* keys in that case. |
| 603 |
*/ |
| 604 |
if (vcpu->arch.hcr_el2 & (HCR_API | HCR_APK)) { |
| 605 |
struct kvm_cpu_context *ctxt; |
| 606 |
|
| 607 |
ctxt = this_cpu_ptr_hyp_sym(kvm_hyp_ctxt); |
| 608 |
ptrauth_save_keys(ctxt); |
| 609 |
} |
| 610 |
} |
| 611 |
} |
| 612 |
|
| 613 |
static bool kvm_vcpu_should_clear_twi(struct kvm_vcpu *vcpu) |
| 614 |
{ |
| 615 |
if (unlikely(kvm_wfi_trap_policy != KVM_WFX_NOTRAP_SINGLE_TASK)) |
| 616 |
return kvm_wfi_trap_policy == KVM_WFX_NOTRAP; |
| 617 |
|
| 618 |
return single_task_running() && |
| 619 |
vcpu->kvm->arch.vgic.vgic_model == KVM_DEV_TYPE_ARM_VGIC_V3 && |
| 620 |
(atomic_read(&vcpu->arch.vgic_cpu.vgic_v3.its_vpe.vlpi_count) || |
| 621 |
vcpu->kvm->arch.vgic.nassgireq); |
| 622 |
} |
| 623 |
|
| 624 |
static bool kvm_vcpu_should_clear_twe(struct kvm_vcpu *vcpu) |
| 625 |
{ |
| 626 |
if (unlikely(kvm_wfe_trap_policy != KVM_WFX_NOTRAP_SINGLE_TASK)) |
| 627 |
return kvm_wfe_trap_policy == KVM_WFX_NOTRAP; |
| 628 |
|
| 629 |
return single_task_running(); |
| 630 |
} |
| 631 |
|
| 632 |
void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu) |
| 633 |
{ |
| 634 |
struct kvm_s2_mmu *mmu; |
| 635 |
int *last_ran; |
| 636 |
|
| 637 |
if (is_protected_kvm_enabled()) |
| 638 |
goto nommu; |
| 639 |
|
| 640 |
if (vcpu_has_nv(vcpu)) |
| 641 |
kvm_vcpu_load_hw_mmu(vcpu); |
| 642 |
|
| 643 |
mmu = vcpu->arch.hw_mmu; |
| 644 |
last_ran = this_cpu_ptr(mmu->last_vcpu_ran); |
| 645 |
|
| 646 |
/* |
| 647 |
* Ensure a VMID is allocated for the MMU before programming VTTBR_EL2, |
| 648 |
* which happens eagerly in VHE. |
| 649 |
* |
| 650 |
* Also, the VMID allocator only preserves VMIDs that are active at the |
| 651 |
* time of rollover, so KVM might need to grab a new VMID for the MMU if |
| 652 |
* this is called from kvm_sched_in(). |
| 653 |
*/ |
| 654 |
kvm_arm_vmid_update(&mmu->vmid); |
| 655 |
|
| 656 |
/* |
| 657 |
* We guarantee that both TLBs and I-cache are private to each |
| 658 |
* vcpu. If detecting that a vcpu from the same VM has |
| 659 |
* previously run on the same physical CPU, call into the |
| 660 |
* hypervisor code to nuke the relevant contexts. |
| 661 |
* |
| 662 |
* We might get preempted before the vCPU actually runs, but |
| 663 |
* over-invalidation doesn't affect correctness. |
| 664 |
*/ |
| 665 |
if (*last_ran != vcpu->vcpu_idx) { |
| 666 |
kvm_call_hyp(__kvm_flush_cpu_context, mmu); |
| 667 |
*last_ran = vcpu->vcpu_idx; |
| 668 |
} |
| 669 |
|
| 670 |
nommu: |
| 671 |
vcpu->cpu = cpu; |
| 672 |
|
| 673 |
/* |
| 674 |
* The timer must be loaded before the vgic to correctly set up physical |
| 675 |
* interrupt deactivation in nested state (e.g. timer interrupt). |
| 676 |
*/ |
| 677 |
kvm_timer_vcpu_load(vcpu); |
| 678 |
kvm_vgic_load(vcpu); |
| 679 |
kvm_vcpu_load_debug(vcpu); |
| 680 |
kvm_vcpu_load_fgt(vcpu); |
| 681 |
if (has_vhe()) |
| 682 |
kvm_vcpu_load_vhe(vcpu); |
| 683 |
kvm_arch_vcpu_load_fp(vcpu); |
| 684 |
kvm_vcpu_pmu_restore_guest(vcpu); |
| 685 |
if (kvm_arm_is_pvtime_enabled(&vcpu->arch)) |
| 686 |
kvm_make_request(KVM_REQ_RECORD_STEAL, vcpu); |
| 687 |
|
| 688 |
if (kvm_vcpu_should_clear_twe(vcpu)) |
| 689 |
vcpu->arch.hcr_el2 &= ~HCR_TWE; |
| 690 |
else |
| 691 |
vcpu->arch.hcr_el2 |= HCR_TWE; |
| 692 |
|
| 693 |
if (kvm_vcpu_should_clear_twi(vcpu)) |
| 694 |
vcpu->arch.hcr_el2 &= ~HCR_TWI; |
| 695 |
else |
| 696 |
vcpu->arch.hcr_el2 |= HCR_TWI; |
| 697 |
|
| 698 |
vcpu_set_pauth_traps(vcpu); |
| 699 |
|
| 700 |
if (is_protected_kvm_enabled()) { |
| 701 |
kvm_call_hyp_nvhe(__pkvm_vcpu_load, |
| 702 |
vcpu->kvm->arch.pkvm.handle, |
| 703 |
vcpu->vcpu_idx, vcpu->arch.hcr_el2); |
| 704 |
kvm_call_hyp(__vgic_v3_restore_vmcr_aprs, |
| 705 |
&vcpu->arch.vgic_cpu.vgic_v3); |
| 706 |
} |
| 707 |
|
| 708 |
if (!cpumask_test_cpu(cpu, vcpu->kvm->arch.supported_cpus)) |
| 709 |
vcpu_set_on_unsupported_cpu(vcpu); |
| 710 |
} |
| 711 |
|
| 712 |
void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu) |
| 713 |
{ |
| 714 |
if (is_protected_kvm_enabled()) { |
| 715 |
kvm_call_hyp(__vgic_v3_save_aprs, &vcpu->arch.vgic_cpu.vgic_v3); |
| 716 |
kvm_call_hyp_nvhe(__pkvm_vcpu_put); |
| 717 |
} |
| 718 |
|
| 719 |
kvm_vcpu_put_debug(vcpu); |
| 720 |
kvm_arch_vcpu_put_fp(vcpu); |
| 721 |
if (has_vhe()) |
| 722 |
kvm_vcpu_put_vhe(vcpu); |
| 723 |
kvm_timer_vcpu_put(vcpu); |
| 724 |
kvm_vgic_put(vcpu); |
| 725 |
kvm_vcpu_pmu_restore_host(vcpu); |
| 726 |
if (vcpu_has_nv(vcpu)) |
| 727 |
kvm_vcpu_put_hw_mmu(vcpu); |
| 728 |
kvm_arm_vmid_clear_active(); |
| 729 |
|
| 730 |
vcpu_clear_on_unsupported_cpu(vcpu); |
| 731 |
vcpu->cpu = -1; |
| 732 |
} |
| 733 |
|
| 734 |
static void __kvm_arm_vcpu_power_off(struct kvm_vcpu *vcpu) |
| 735 |
{ |
| 736 |
WRITE_ONCE(vcpu->arch.mp_state.mp_state, KVM_MP_STATE_STOPPED); |
| 737 |
kvm_make_request(KVM_REQ_SLEEP, vcpu); |
| 738 |
kvm_vcpu_kick(vcpu); |
| 739 |
} |
| 740 |
|
| 741 |
void kvm_arm_vcpu_power_off(struct kvm_vcpu *vcpu) |
| 742 |
{ |
| 743 |
spin_lock(&vcpu->arch.mp_state_lock); |
| 744 |
__kvm_arm_vcpu_power_off(vcpu); |
| 745 |
spin_unlock(&vcpu->arch.mp_state_lock); |
| 746 |
} |
| 747 |
|
| 748 |
bool kvm_arm_vcpu_stopped(struct kvm_vcpu *vcpu) |
| 749 |
{ |
| 750 |
return READ_ONCE(vcpu->arch.mp_state.mp_state) == KVM_MP_STATE_STOPPED; |
| 751 |
} |
| 752 |
|
| 753 |
static void kvm_arm_vcpu_suspend(struct kvm_vcpu *vcpu) |
| 754 |
{ |
| 755 |
WRITE_ONCE(vcpu->arch.mp_state.mp_state, KVM_MP_STATE_SUSPENDED); |
| 756 |
kvm_make_request(KVM_REQ_SUSPEND, vcpu); |
| 757 |
kvm_vcpu_kick(vcpu); |
| 758 |
} |
| 759 |
|
| 760 |
static bool kvm_arm_vcpu_suspended(struct kvm_vcpu *vcpu) |
| 761 |
{ |
| 762 |
return READ_ONCE(vcpu->arch.mp_state.mp_state) == KVM_MP_STATE_SUSPENDED; |
| 763 |
} |
| 764 |
|
| 765 |
int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu, |
| 766 |
struct kvm_mp_state *mp_state) |
| 767 |
{ |
| 768 |
*mp_state = READ_ONCE(vcpu->arch.mp_state); |
| 769 |
|
| 770 |
return 0; |
| 771 |
} |
| 772 |
|
| 773 |
int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu, |
| 774 |
struct kvm_mp_state *mp_state) |
| 775 |
{ |
| 776 |
int ret = 0; |
| 777 |
|
| 778 |
spin_lock(&vcpu->arch.mp_state_lock); |
| 779 |
|
| 780 |
switch (mp_state->mp_state) { |
| 781 |
case KVM_MP_STATE_RUNNABLE: |
| 782 |
WRITE_ONCE(vcpu->arch.mp_state, *mp_state); |
| 783 |
break; |
| 784 |
case KVM_MP_STATE_STOPPED: |
| 785 |
__kvm_arm_vcpu_power_off(vcpu); |
| 786 |
break; |
| 787 |
case KVM_MP_STATE_SUSPENDED: |
| 788 |
kvm_arm_vcpu_suspend(vcpu); |
| 789 |
break; |
| 790 |
default: |
| 791 |
ret = -EINVAL; |
| 792 |
} |
| 793 |
|
| 794 |
spin_unlock(&vcpu->arch.mp_state_lock); |
| 795 |
|
| 796 |
return ret; |
| 797 |
} |
| 798 |
|
| 799 |
/** |
| 800 |
* kvm_arch_vcpu_runnable - determine if the vcpu can be scheduled |
| 801 |
* @v: The VCPU pointer |
| 802 |
* |
| 803 |
* If the guest CPU is not waiting for interrupts or an interrupt line is |
| 804 |
* asserted, the CPU is by definition runnable. |
| 805 |
*/ |
| 806 |
int kvm_arch_vcpu_runnable(struct kvm_vcpu *v) |
| 807 |
{ |
| 808 |
bool irq_lines = *vcpu_hcr(v) & (HCR_VI | HCR_VF | HCR_VSE); |
| 809 |
|
| 810 |
irq_lines |= (!irqchip_in_kernel(v->kvm) && |
| 811 |
(kvm_timer_should_notify_user(v) || |
| 812 |
kvm_pmu_should_notify_user(v))); |
| 813 |
|
| 814 |
return ((irq_lines || kvm_vgic_vcpu_pending_irq(v)) |
| 815 |
&& !kvm_arm_vcpu_stopped(v) && !v->arch.pause); |
| 816 |
} |
| 817 |
|
| 818 |
bool kvm_arch_vcpu_in_kernel(struct kvm_vcpu *vcpu) |
| 819 |
{ |
| 820 |
return vcpu_mode_priv(vcpu); |
| 821 |
} |
| 822 |
|
| 823 |
#ifdef CONFIG_GUEST_PERF_EVENTS |
| 824 |
unsigned long kvm_arch_vcpu_get_ip(struct kvm_vcpu *vcpu) |
| 825 |
{ |
| 826 |
return *vcpu_pc(vcpu); |
| 827 |
} |
| 828 |
#endif |
| 829 |
|
| 830 |
static void kvm_init_mpidr_data(struct kvm *kvm) |
| 831 |
{ |
| 832 |
struct kvm_mpidr_data *data = NULL; |
| 833 |
unsigned long c, mask, nr_entries; |
| 834 |
u64 aff_set = 0, aff_clr = ~0UL; |
| 835 |
struct kvm_vcpu *vcpu; |
| 836 |
|
| 837 |
mutex_lock(&kvm->arch.config_lock); |
| 838 |
|
| 839 |
if (rcu_access_pointer(kvm->arch.mpidr_data) || |
| 840 |
atomic_read(&kvm->online_vcpus) == 1) |
| 841 |
goto out; |
| 842 |
|
| 843 |
kvm_for_each_vcpu(c, vcpu, kvm) { |
| 844 |
u64 aff = kvm_vcpu_get_mpidr_aff(vcpu); |
| 845 |
aff_set |= aff; |
| 846 |
aff_clr &= aff; |
| 847 |
} |
| 848 |
|
| 849 |
/* |
| 850 |
* A significant bit can be either 0 or 1, and will only appear in |
| 851 |
* aff_set. Use aff_clr to weed out the useless stuff. |
| 852 |
*/ |
| 853 |
mask = aff_set ^ aff_clr; |
| 854 |
nr_entries = BIT_ULL(hweight_long(mask)); |
| 855 |
|
| 856 |
/* |
| 857 |
* Don't let userspace fool us. If we need more than a single page |
| 858 |
* to describe the compressed MPIDR array, just fall back to the |
| 859 |
* iterative method. Single vcpu VMs do not need this either. |
| 860 |
*/ |
| 861 |
if (struct_size(data, cmpidr_to_idx, nr_entries) <= PAGE_SIZE) |
| 862 |
data = kzalloc_flex(*data, cmpidr_to_idx, nr_entries, |
| 863 |
GFP_KERNEL_ACCOUNT); |
| 864 |
|
| 865 |
if (!data) |
| 866 |
goto out; |
| 867 |
|
| 868 |
data->mpidr_mask = mask; |
| 869 |
|
| 870 |
kvm_for_each_vcpu(c, vcpu, kvm) { |
| 871 |
u64 aff = kvm_vcpu_get_mpidr_aff(vcpu); |
| 872 |
u16 index = kvm_mpidr_index(data, aff); |
| 873 |
|
| 874 |
data->cmpidr_to_idx[index] = c; |
| 875 |
} |
| 876 |
|
| 877 |
rcu_assign_pointer(kvm->arch.mpidr_data, data); |
| 878 |
out: |
| 879 |
mutex_unlock(&kvm->arch.config_lock); |
| 880 |
} |
| 881 |
|
| 882 |
/* |
| 883 |
* Handle both the initialisation that is being done when the vcpu is |
| 884 |
* run for the first time, as well as the updates that must be |
| 885 |
* performed each time we get a new thread dealing with this vcpu. |
| 886 |
*/ |
| 887 |
int kvm_arch_vcpu_run_pid_change(struct kvm_vcpu *vcpu) |
| 888 |
{ |
| 889 |
struct kvm *kvm = vcpu->kvm; |
| 890 |
int ret; |
| 891 |
|
| 892 |
if (!kvm_vcpu_initialized(vcpu)) |
| 893 |
return -ENOEXEC; |
| 894 |
|
| 895 |
if (!kvm_arm_vcpu_is_finalized(vcpu)) |
| 896 |
return -EPERM; |
| 897 |
|
| 898 |
if (likely(vcpu_has_run_once(vcpu))) |
| 899 |
return 0; |
| 900 |
|
| 901 |
kvm_init_mpidr_data(kvm); |
| 902 |
|
| 903 |
if (likely(irqchip_in_kernel(kvm))) { |
| 904 |
/* |
| 905 |
* Map the VGIC hardware resources before running a vcpu the |
| 906 |
* first time on this VM. |
| 907 |
*/ |
| 908 |
ret = kvm_vgic_map_resources(kvm); |
| 909 |
if (ret) |
| 910 |
return ret; |
| 911 |
} |
| 912 |
|
| 913 |
ret = kvm_finalize_sys_regs(vcpu); |
| 914 |
if (ret) |
| 915 |
return ret; |
| 916 |
|
| 917 |
if (vcpu_has_nv(vcpu)) { |
| 918 |
ret = kvm_vcpu_allocate_vncr_tlb(vcpu); |
| 919 |
if (ret) |
| 920 |
return ret; |
| 921 |
|
| 922 |
ret = kvm_vgic_vcpu_nv_init(vcpu); |
| 923 |
if (ret) |
| 924 |
return ret; |
| 925 |
} |
| 926 |
|
| 927 |
/* |
| 928 |
* This needs to happen after any restriction has been applied |
| 929 |
* to the feature set. |
| 930 |
*/ |
| 931 |
kvm_calculate_traps(vcpu); |
| 932 |
|
| 933 |
ret = kvm_timer_enable(vcpu); |
| 934 |
if (ret) |
| 935 |
return ret; |
| 936 |
|
| 937 |
if (kvm_vcpu_has_pmu(vcpu)) { |
| 938 |
ret = kvm_arm_pmu_v3_enable(vcpu); |
| 939 |
if (ret) |
| 940 |
return ret; |
| 941 |
} |
| 942 |
|
| 943 |
if (is_protected_kvm_enabled()) { |
| 944 |
ret = pkvm_create_hyp_vm(kvm); |
| 945 |
if (ret) |
| 946 |
return ret; |
| 947 |
|
| 948 |
ret = pkvm_create_hyp_vcpu(vcpu); |
| 949 |
if (ret) |
| 950 |
return ret; |
| 951 |
} |
| 952 |
|
| 953 |
mutex_lock(&kvm->arch.config_lock); |
| 954 |
set_bit(KVM_ARCH_FLAG_HAS_RAN_ONCE, &kvm->arch.flags); |
| 955 |
mutex_unlock(&kvm->arch.config_lock); |
| 956 |
|
| 957 |
return ret; |
| 958 |
} |
| 959 |
|
| 960 |
bool kvm_arch_intc_initialized(struct kvm *kvm) |
| 961 |
{ |
| 962 |
return vgic_initialized(kvm); |
| 963 |
} |
| 964 |
|
| 965 |
void kvm_arm_halt_guest(struct kvm *kvm) |
| 966 |
{ |
| 967 |
unsigned long i; |
| 968 |
struct kvm_vcpu *vcpu; |
| 969 |
|
| 970 |
kvm_for_each_vcpu(i, vcpu, kvm) |
| 971 |
vcpu->arch.pause = true; |
| 972 |
kvm_make_all_cpus_request(kvm, KVM_REQ_SLEEP); |
| 973 |
} |
| 974 |
|
| 975 |
void kvm_arm_resume_guest(struct kvm *kvm) |
| 976 |
{ |
| 977 |
unsigned long i; |
| 978 |
struct kvm_vcpu *vcpu; |
| 979 |
|
| 980 |
kvm_for_each_vcpu(i, vcpu, kvm) { |
| 981 |
vcpu->arch.pause = false; |
| 982 |
__kvm_vcpu_wake_up(vcpu); |
| 983 |
} |
| 984 |
} |
| 985 |
|
| 986 |
static void kvm_vcpu_sleep(struct kvm_vcpu *vcpu) |
| 987 |
{ |
| 988 |
struct rcuwait *wait = kvm_arch_vcpu_get_wait(vcpu); |
| 989 |
|
| 990 |
rcuwait_wait_event(wait, |
| 991 |
(!kvm_arm_vcpu_stopped(vcpu)) && (!vcpu->arch.pause), |
| 992 |
TASK_INTERRUPTIBLE); |
| 993 |
|
| 994 |
if (kvm_arm_vcpu_stopped(vcpu) || vcpu->arch.pause) { |
| 995 |
/* Awaken to handle a signal, request we sleep again later. */ |
| 996 |
kvm_make_request(KVM_REQ_SLEEP, vcpu); |
| 997 |
} |
| 998 |
|
| 999 |
/* |
| 1000 |
* Make sure we will observe a potential reset request if we've |
| 1001 |
* observed a change to the power state. Pairs with the smp_wmb() in |
| 1002 |
* kvm_psci_vcpu_on(). |
| 1003 |
*/ |
| 1004 |
smp_rmb(); |
| 1005 |
} |
| 1006 |
|
| 1007 |
/** |
| 1008 |
* kvm_vcpu_wfi - emulate Wait-For-Interrupt behavior |
| 1009 |
* @vcpu: The VCPU pointer |
| 1010 |
* |
| 1011 |
* Suspend execution of a vCPU until a valid wake event is detected, i.e. until |
| 1012 |
* the vCPU is runnable. The vCPU may or may not be scheduled out, depending |
| 1013 |
* on when a wake event arrives, e.g. there may already be a pending wake event. |
| 1014 |
*/ |
| 1015 |
void kvm_vcpu_wfi(struct kvm_vcpu *vcpu) |
| 1016 |
{ |
| 1017 |
/* |
| 1018 |
* Sync back the state of the GIC CPU interface so that we have |
| 1019 |
* the latest PMR and group enables. This ensures that |
| 1020 |
* kvm_arch_vcpu_runnable has up-to-date data to decide whether |
| 1021 |
* we have pending interrupts, e.g. when determining if the |
| 1022 |
* vCPU should block. |
| 1023 |
* |
| 1024 |
* For the same reason, we want to tell GICv4 that we need |
| 1025 |
* doorbells to be signalled, should an interrupt become pending. |
| 1026 |
*/ |
| 1027 |
preempt_disable(); |
| 1028 |
vcpu_set_flag(vcpu, IN_WFI); |
| 1029 |
kvm_vgic_put(vcpu); |
| 1030 |
preempt_enable(); |
| 1031 |
|
| 1032 |
kvm_vcpu_halt(vcpu); |
| 1033 |
vcpu_clear_flag(vcpu, IN_WFIT); |
| 1034 |
|
| 1035 |
preempt_disable(); |
| 1036 |
vcpu_clear_flag(vcpu, IN_WFI); |
| 1037 |
kvm_vgic_load(vcpu); |
| 1038 |
preempt_enable(); |
| 1039 |
} |
| 1040 |
|
| 1041 |
static int kvm_vcpu_suspend(struct kvm_vcpu *vcpu) |
| 1042 |
{ |
| 1043 |
if (!kvm_arm_vcpu_suspended(vcpu)) |
| 1044 |
return 1; |
| 1045 |
|
| 1046 |
kvm_vcpu_wfi(vcpu); |
| 1047 |
|
| 1048 |
/* |
| 1049 |
* The suspend state is sticky; we do not leave it until userspace |
| 1050 |
* explicitly marks the vCPU as runnable. Request that we suspend again |
| 1051 |
* later. |
| 1052 |
*/ |
| 1053 |
kvm_make_request(KVM_REQ_SUSPEND, vcpu); |
| 1054 |
|
| 1055 |
/* |
| 1056 |
* Check to make sure the vCPU is actually runnable. If so, exit to |
| 1057 |
* userspace informing it of the wakeup condition. |
| 1058 |
*/ |
| 1059 |
if (kvm_arch_vcpu_runnable(vcpu)) { |
| 1060 |
memset(&vcpu->run->system_event, 0, sizeof(vcpu->run->system_event)); |
| 1061 |
vcpu->run->system_event.type = KVM_SYSTEM_EVENT_WAKEUP; |
| 1062 |
vcpu->run->exit_reason = KVM_EXIT_SYSTEM_EVENT; |
| 1063 |
return 0; |
| 1064 |
} |
| 1065 |
|
| 1066 |
/* |
| 1067 |
* Otherwise, we were unblocked to process a different event, such as a |
| 1068 |
* pending signal. Return 1 and allow kvm_arch_vcpu_ioctl_run() to |
| 1069 |
* process the event. |
| 1070 |
*/ |
| 1071 |
return 1; |
| 1072 |
} |
| 1073 |
|
| 1074 |
/** |
| 1075 |
* check_vcpu_requests - check and handle pending vCPU requests |
| 1076 |
* @vcpu: the VCPU pointer |
| 1077 |
* |
| 1078 |
* Return: 1 if we should enter the guest |
| 1079 |
* 0 if we should exit to userspace |
| 1080 |
* < 0 if we should exit to userspace, where the return value indicates |
| 1081 |
* an error |
| 1082 |
*/ |
| 1083 |
static int check_vcpu_requests(struct kvm_vcpu *vcpu) |
| 1084 |
{ |
| 1085 |
if (kvm_request_pending(vcpu)) { |
| 1086 |
if (kvm_check_request(KVM_REQ_VM_DEAD, vcpu)) |
| 1087 |
return -EIO; |
| 1088 |
|
| 1089 |
if (kvm_check_request(KVM_REQ_SLEEP, vcpu)) |
| 1090 |
kvm_vcpu_sleep(vcpu); |
| 1091 |
|
| 1092 |
if (kvm_check_request(KVM_REQ_VCPU_RESET, vcpu)) |
| 1093 |
kvm_reset_vcpu(vcpu); |
| 1094 |
|
| 1095 |
/* |
| 1096 |
* Clear IRQ_PENDING requests that were made to guarantee |
| 1097 |
* that a VCPU sees new virtual interrupts. |
| 1098 |
*/ |
| 1099 |
kvm_check_request(KVM_REQ_IRQ_PENDING, vcpu); |
| 1100 |
|
| 1101 |
/* Process interrupts deactivated through a trap */ |
| 1102 |
if (kvm_check_request(KVM_REQ_VGIC_PROCESS_UPDATE, vcpu)) |
| 1103 |
kvm_vgic_process_async_update(vcpu); |
| 1104 |
|
| 1105 |
if (kvm_check_request(KVM_REQ_RECORD_STEAL, vcpu)) |
| 1106 |
kvm_update_stolen_time(vcpu); |
| 1107 |
|
| 1108 |
if (kvm_check_request(KVM_REQ_RELOAD_GICv4, vcpu)) { |
| 1109 |
/* The distributor enable bits were changed */ |
| 1110 |
preempt_disable(); |
| 1111 |
vgic_v4_put(vcpu); |
| 1112 |
vgic_v4_load(vcpu); |
| 1113 |
preempt_enable(); |
| 1114 |
} |
| 1115 |
|
| 1116 |
if (kvm_check_request(KVM_REQ_RELOAD_PMU, vcpu)) |
| 1117 |
kvm_vcpu_reload_pmu(vcpu); |
| 1118 |
|
| 1119 |
if (kvm_check_request(KVM_REQ_RESYNC_PMU_EL0, vcpu)) |
| 1120 |
kvm_vcpu_pmu_restore_guest(vcpu); |
| 1121 |
|
| 1122 |
if (kvm_check_request(KVM_REQ_SUSPEND, vcpu)) |
| 1123 |
return kvm_vcpu_suspend(vcpu); |
| 1124 |
|
| 1125 |
if (kvm_dirty_ring_check_request(vcpu)) |
| 1126 |
return 0; |
| 1127 |
|
| 1128 |
check_nested_vcpu_requests(vcpu); |
| 1129 |
} |
| 1130 |
|
| 1131 |
return 1; |
| 1132 |
} |
| 1133 |
|
| 1134 |
static bool vcpu_mode_is_bad_32bit(struct kvm_vcpu *vcpu) |
| 1135 |
{ |
| 1136 |
if (likely(!vcpu_mode_is_32bit(vcpu))) |
| 1137 |
return false; |
| 1138 |
|
| 1139 |
if (vcpu_has_nv(vcpu)) |
| 1140 |
return true; |
| 1141 |
|
| 1142 |
return !kvm_supports_32bit_el0(); |
| 1143 |
} |
| 1144 |
|
| 1145 |
/** |
| 1146 |
* kvm_vcpu_exit_request - returns true if the VCPU should *not* enter the guest |
| 1147 |
* @vcpu: The VCPU pointer |
| 1148 |
* @ret: Pointer to write optional return code |
| 1149 |
* |
| 1150 |
* Returns: true if the VCPU needs to return to a preemptible + interruptible |
| 1151 |
* and skip guest entry. |
| 1152 |
* |
| 1153 |
* This function disambiguates between two different types of exits: exits to a |
| 1154 |
* preemptible + interruptible kernel context and exits to userspace. For an |
| 1155 |
* exit to userspace, this function will write the return code to ret and return |
| 1156 |
* true. For an exit to preemptible + interruptible kernel context (i.e. check |
| 1157 |
* for pending work and re-enter), return true without writing to ret. |
| 1158 |
*/ |
| 1159 |
static bool kvm_vcpu_exit_request(struct kvm_vcpu *vcpu, int *ret) |
| 1160 |
{ |
| 1161 |
struct kvm_run *run = vcpu->run; |
| 1162 |
|
| 1163 |
/* |
| 1164 |
* If we're using a userspace irqchip, then check if we need |
| 1165 |
* to tell a userspace irqchip about timer or PMU level |
| 1166 |
* changes and if so, exit to userspace (the actual level |
| 1167 |
* state gets updated in kvm_timer_update_run and |
| 1168 |
* kvm_pmu_update_run below). |
| 1169 |
*/ |
| 1170 |
if (unlikely(!irqchip_in_kernel(vcpu->kvm))) { |
| 1171 |
if (kvm_timer_should_notify_user(vcpu) || |
| 1172 |
kvm_pmu_should_notify_user(vcpu)) { |
| 1173 |
*ret = -EINTR; |
| 1174 |
run->exit_reason = KVM_EXIT_INTR; |
| 1175 |
return true; |
| 1176 |
} |
| 1177 |
} |
| 1178 |
|
| 1179 |
if (unlikely(vcpu_on_unsupported_cpu(vcpu))) { |
| 1180 |
run->exit_reason = KVM_EXIT_FAIL_ENTRY; |
| 1181 |
run->fail_entry.hardware_entry_failure_reason = KVM_EXIT_FAIL_ENTRY_CPU_UNSUPPORTED; |
| 1182 |
run->fail_entry.cpu = smp_processor_id(); |
| 1183 |
*ret = 0; |
| 1184 |
return true; |
| 1185 |
} |
| 1186 |
|
| 1187 |
return kvm_request_pending(vcpu) || |
| 1188 |
xfer_to_guest_mode_work_pending(); |
| 1189 |
} |
| 1190 |
|
| 1191 |
/* |
| 1192 |
* Actually run the vCPU, entering an RCU extended quiescent state (EQS) while |
| 1193 |
* the vCPU is running. |
| 1194 |
* |
| 1195 |
* This must be noinstr as instrumentation may make use of RCU, and this is not |
| 1196 |
* safe during the EQS. |
| 1197 |
*/ |
| 1198 |
static int noinstr kvm_arm_vcpu_enter_exit(struct kvm_vcpu *vcpu) |
| 1199 |
{ |
| 1200 |
int ret; |
| 1201 |
|
| 1202 |
guest_state_enter_irqoff(); |
| 1203 |
ret = kvm_call_hyp_ret(__kvm_vcpu_run, vcpu); |
| 1204 |
guest_state_exit_irqoff(); |
| 1205 |
|
| 1206 |
return ret; |
| 1207 |
} |
| 1208 |
|
| 1209 |
/** |
| 1210 |
* kvm_arch_vcpu_ioctl_run - the main VCPU run function to execute guest code |
| 1211 |
* @vcpu: The VCPU pointer |
| 1212 |
* |
| 1213 |
* This function is called through the VCPU_RUN ioctl called from user space. It |
| 1214 |
* will execute VM code in a loop until the time slice for the process is used |
| 1215 |
* or some emulation is needed from user space in which case the function will |
| 1216 |
* return with return value 0 and with the kvm_run structure filled in with the |
| 1217 |
* required data for the requested emulation. |
| 1218 |
*/ |
| 1219 |
int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu) |
| 1220 |
{ |
| 1221 |
struct kvm_run *run = vcpu->run; |
| 1222 |
int ret; |
| 1223 |
|
| 1224 |
if (run->exit_reason == KVM_EXIT_MMIO) { |
| 1225 |
ret = kvm_handle_mmio_return(vcpu); |
| 1226 |
if (ret <= 0) |
| 1227 |
return ret; |
| 1228 |
} |
| 1229 |
|
| 1230 |
vcpu_load(vcpu); |
| 1231 |
|
| 1232 |
if (!vcpu->wants_to_run) { |
| 1233 |
ret = -EINTR; |
| 1234 |
goto out; |
| 1235 |
} |
| 1236 |
|
| 1237 |
kvm_sigset_activate(vcpu); |
| 1238 |
|
| 1239 |
ret = 1; |
| 1240 |
run->exit_reason = KVM_EXIT_UNKNOWN; |
| 1241 |
run->flags = 0; |
| 1242 |
while (ret > 0) { |
| 1243 |
/* |
| 1244 |
* Check conditions before entering the guest |
| 1245 |
*/ |
| 1246 |
ret = kvm_xfer_to_guest_mode_handle_work(vcpu); |
| 1247 |
if (!ret) |
| 1248 |
ret = 1; |
| 1249 |
|
| 1250 |
if (ret > 0) |
| 1251 |
ret = check_vcpu_requests(vcpu); |
| 1252 |
|
| 1253 |
/* |
| 1254 |
* Preparing the interrupts to be injected also |
| 1255 |
* involves poking the GIC, which must be done in a |
| 1256 |
* non-preemptible context. |
| 1257 |
*/ |
| 1258 |
preempt_disable(); |
| 1259 |
|
| 1260 |
kvm_nested_flush_hwstate(vcpu); |
| 1261 |
|
| 1262 |
if (kvm_vcpu_has_pmu(vcpu)) |
| 1263 |
kvm_pmu_flush_hwstate(vcpu); |
| 1264 |
|
| 1265 |
local_irq_disable(); |
| 1266 |
|
| 1267 |
kvm_vgic_flush_hwstate(vcpu); |
| 1268 |
|
| 1269 |
kvm_pmu_update_vcpu_events(vcpu); |
| 1270 |
|
| 1271 |
/* |
| 1272 |
* Ensure we set mode to IN_GUEST_MODE after we disable |
| 1273 |
* interrupts and before the final VCPU requests check. |
| 1274 |
* See the comment in kvm_vcpu_exiting_guest_mode() and |
| 1275 |
* Documentation/virt/kvm/vcpu-requests.rst |
| 1276 |
*/ |
| 1277 |
smp_store_mb(vcpu->mode, IN_GUEST_MODE); |
| 1278 |
|
| 1279 |
if (ret <= 0 || kvm_vcpu_exit_request(vcpu, &ret)) { |
| 1280 |
vcpu->mode = OUTSIDE_GUEST_MODE; |
| 1281 |
isb(); /* Ensure work in x_flush_hwstate is committed */ |
| 1282 |
if (kvm_vcpu_has_pmu(vcpu)) |
| 1283 |
kvm_pmu_sync_hwstate(vcpu); |
| 1284 |
if (unlikely(!irqchip_in_kernel(vcpu->kvm))) |
| 1285 |
kvm_timer_sync_user(vcpu); |
| 1286 |
kvm_vgic_sync_hwstate(vcpu); |
| 1287 |
local_irq_enable(); |
| 1288 |
preempt_enable(); |
| 1289 |
continue; |
| 1290 |
} |
| 1291 |
|
| 1292 |
kvm_arch_vcpu_ctxflush_fp(vcpu); |
| 1293 |
|
| 1294 |
/************************************************************** |
| 1295 |
* Enter the guest |
| 1296 |
*/ |
| 1297 |
trace_kvm_entry(*vcpu_pc(vcpu)); |
| 1298 |
guest_timing_enter_irqoff(); |
| 1299 |
|
| 1300 |
ret = kvm_arm_vcpu_enter_exit(vcpu); |
| 1301 |
|
| 1302 |
vcpu->mode = OUTSIDE_GUEST_MODE; |
| 1303 |
vcpu->stat.exits++; |
| 1304 |
/* |
| 1305 |
* Back from guest |
| 1306 |
*************************************************************/ |
| 1307 |
|
| 1308 |
/* |
| 1309 |
* We must sync the PMU state before the vgic state so |
| 1310 |
* that the vgic can properly sample the updated state of the |
| 1311 |
* interrupt line. |
| 1312 |
*/ |
| 1313 |
if (kvm_vcpu_has_pmu(vcpu)) |
| 1314 |
kvm_pmu_sync_hwstate(vcpu); |
| 1315 |
|
| 1316 |
/* |
| 1317 |
* Sync the vgic state before syncing the timer state because |
| 1318 |
* the timer code needs to know if the virtual timer |
| 1319 |
* interrupts are active. |
| 1320 |
*/ |
| 1321 |
kvm_vgic_sync_hwstate(vcpu); |
| 1322 |
|
| 1323 |
/* |
| 1324 |
* Sync the timer hardware state before enabling interrupts as |
| 1325 |
* we don't want vtimer interrupts to race with syncing the |
| 1326 |
* timer virtual interrupt state. |
| 1327 |
*/ |
| 1328 |
if (unlikely(!irqchip_in_kernel(vcpu->kvm))) |
| 1329 |
kvm_timer_sync_user(vcpu); |
| 1330 |
|
| 1331 |
if (is_hyp_ctxt(vcpu)) |
| 1332 |
kvm_timer_sync_nested(vcpu); |
| 1333 |
|
| 1334 |
kvm_arch_vcpu_ctxsync_fp(vcpu); |
| 1335 |
|
| 1336 |
/* |
| 1337 |
* We must ensure that any pending interrupts are taken before |
| 1338 |
* we exit guest timing so that timer ticks are accounted as |
| 1339 |
* guest time. Transiently unmask interrupts so that any |
| 1340 |
* pending interrupts are taken. |
| 1341 |
* |
| 1342 |
* Per ARM DDI 0487G.b section D1.13.4, an ISB (or other |
| 1343 |
* context synchronization event) is necessary to ensure that |
| 1344 |
* pending interrupts are taken. |
| 1345 |
*/ |
| 1346 |
if (ARM_EXCEPTION_CODE(ret) == ARM_EXCEPTION_IRQ) { |
| 1347 |
local_irq_enable(); |
| 1348 |
isb(); |
| 1349 |
local_irq_disable(); |
| 1350 |
} |
| 1351 |
|
| 1352 |
guest_timing_exit_irqoff(); |
| 1353 |
|
| 1354 |
local_irq_enable(); |
| 1355 |
|
| 1356 |
trace_kvm_exit(ret, kvm_vcpu_trap_get_class(vcpu), *vcpu_pc(vcpu)); |
| 1357 |
|
| 1358 |
/* Exit types that need handling before we can be preempted */ |
| 1359 |
handle_exit_early(vcpu, ret); |
| 1360 |
|
| 1361 |
kvm_nested_sync_hwstate(vcpu); |
| 1362 |
|
| 1363 |
preempt_enable(); |
| 1364 |
|
| 1365 |
/* |
| 1366 |
* The ARMv8 architecture doesn't give the hypervisor |
| 1367 |
* a mechanism to prevent a guest from dropping to AArch32 EL0 |
| 1368 |
* if implemented by the CPU. If we spot the guest in such |
| 1369 |
* state and that we decided it wasn't supposed to do so (like |
| 1370 |
* with the asymmetric AArch32 case), return to userspace with |
| 1371 |
* a fatal error. |
| 1372 |
*/ |
| 1373 |
if (vcpu_mode_is_bad_32bit(vcpu)) { |
| 1374 |
/* |
| 1375 |
* As we have caught the guest red-handed, decide that |
| 1376 |
* it isn't fit for purpose anymore by making the vcpu |
| 1377 |
* invalid. The VMM can try and fix it by issuing a |
| 1378 |
* KVM_ARM_VCPU_INIT if it really wants to. |
| 1379 |
*/ |
| 1380 |
vcpu_clear_flag(vcpu, VCPU_INITIALIZED); |
| 1381 |
ret = ARM_EXCEPTION_IL; |
| 1382 |
} |
| 1383 |
|
| 1384 |
ret = handle_exit(vcpu, ret); |
| 1385 |
} |
| 1386 |
|
| 1387 |
/* Tell userspace about in-kernel device output levels */ |
| 1388 |
if (unlikely(!irqchip_in_kernel(vcpu->kvm))) { |
| 1389 |
kvm_timer_update_run(vcpu); |
| 1390 |
kvm_pmu_update_run(vcpu); |
| 1391 |
} |
| 1392 |
|
| 1393 |
kvm_sigset_deactivate(vcpu); |
| 1394 |
|
| 1395 |
out: |
| 1396 |
/* |
| 1397 |
* In the unlikely event that we are returning to userspace |
| 1398 |
* with pending exceptions or PC adjustment, commit these |
| 1399 |
* adjustments in order to give userspace a consistent view of |
| 1400 |
* the vcpu state. Note that this relies on __kvm_adjust_pc() |
| 1401 |
* being preempt-safe on VHE. |
| 1402 |
*/ |
| 1403 |
if (unlikely(vcpu_get_flag(vcpu, PENDING_EXCEPTION) || |
| 1404 |
vcpu_get_flag(vcpu, INCREMENT_PC))) |
| 1405 |
kvm_call_hyp(__kvm_adjust_pc, vcpu); |
| 1406 |
|
| 1407 |
vcpu_put(vcpu); |
| 1408 |
return ret; |
| 1409 |
} |
| 1410 |
|
| 1411 |
static int vcpu_interrupt_line(struct kvm_vcpu *vcpu, int number, bool level) |
| 1412 |
{ |
| 1413 |
int bit_index; |
| 1414 |
bool set; |
| 1415 |
unsigned long *hcr; |
| 1416 |
|
| 1417 |
if (number == KVM_ARM_IRQ_CPU_IRQ) |
| 1418 |
bit_index = __ffs(HCR_VI); |
| 1419 |
else /* KVM_ARM_IRQ_CPU_FIQ */ |
| 1420 |
bit_index = __ffs(HCR_VF); |
| 1421 |
|
| 1422 |
hcr = vcpu_hcr(vcpu); |
| 1423 |
if (level) |
| 1424 |
set = test_and_set_bit(bit_index, hcr); |
| 1425 |
else |
| 1426 |
set = test_and_clear_bit(bit_index, hcr); |
| 1427 |
|
| 1428 |
/* |
| 1429 |
* If we didn't change anything, no need to wake up or kick other CPUs |
| 1430 |
*/ |
| 1431 |
if (set == level) |
| 1432 |
return 0; |
| 1433 |
|
| 1434 |
/* |
| 1435 |
* The vcpu irq_lines field was updated, wake up sleeping VCPUs and |
| 1436 |
* trigger a world-switch round on the running physical CPU to set the |
| 1437 |
* virtual IRQ/FIQ fields in the HCR appropriately. |
| 1438 |
*/ |
| 1439 |
kvm_make_request(KVM_REQ_IRQ_PENDING, vcpu); |
| 1440 |
kvm_vcpu_kick(vcpu); |
| 1441 |
|
| 1442 |
return 0; |
| 1443 |
} |
| 1444 |
|
| 1445 |
int kvm_vm_ioctl_irq_line(struct kvm *kvm, struct kvm_irq_level *irq_level, |
| 1446 |
bool line_status) |
| 1447 |
{ |
| 1448 |
u32 irq = irq_level->irq; |
| 1449 |
unsigned int irq_type, vcpu_id, irq_num; |
| 1450 |
struct kvm_vcpu *vcpu = NULL; |
| 1451 |
bool level = irq_level->level; |
| 1452 |
|
| 1453 |
irq_type = (irq >> KVM_ARM_IRQ_TYPE_SHIFT) & KVM_ARM_IRQ_TYPE_MASK; |
| 1454 |
vcpu_id = (irq >> KVM_ARM_IRQ_VCPU_SHIFT) & KVM_ARM_IRQ_VCPU_MASK; |
| 1455 |
vcpu_id += ((irq >> KVM_ARM_IRQ_VCPU2_SHIFT) & KVM_ARM_IRQ_VCPU2_MASK) * (KVM_ARM_IRQ_VCPU_MASK + 1); |
| 1456 |
irq_num = (irq >> KVM_ARM_IRQ_NUM_SHIFT) & KVM_ARM_IRQ_NUM_MASK; |
| 1457 |
|
| 1458 |
trace_kvm_irq_line(irq_type, vcpu_id, irq_num, irq_level->level); |
| 1459 |
|
| 1460 |
switch (irq_type) { |
| 1461 |
case KVM_ARM_IRQ_TYPE_CPU: |
| 1462 |
if (irqchip_in_kernel(kvm)) |
| 1463 |
return -ENXIO; |
| 1464 |
|
| 1465 |
vcpu = kvm_get_vcpu_by_id(kvm, vcpu_id); |
| 1466 |
if (!vcpu) |
| 1467 |
return -EINVAL; |
| 1468 |
|
| 1469 |
if (irq_num > KVM_ARM_IRQ_CPU_FIQ) |
| 1470 |
return -EINVAL; |
| 1471 |
|
| 1472 |
return vcpu_interrupt_line(vcpu, irq_num, level); |
| 1473 |
case KVM_ARM_IRQ_TYPE_PPI: |
| 1474 |
if (!irqchip_in_kernel(kvm)) |
| 1475 |
return -ENXIO; |
| 1476 |
|
| 1477 |
vcpu = kvm_get_vcpu_by_id(kvm, vcpu_id); |
| 1478 |
if (!vcpu) |
| 1479 |
return -EINVAL; |
| 1480 |
|
| 1481 |
if (irq_num < VGIC_NR_SGIS || irq_num >= VGIC_NR_PRIVATE_IRQS) |
| 1482 |
return -EINVAL; |
| 1483 |
|
| 1484 |
return kvm_vgic_inject_irq(kvm, vcpu, irq_num, level, NULL); |
| 1485 |
case KVM_ARM_IRQ_TYPE_SPI: |
| 1486 |
if (!irqchip_in_kernel(kvm)) |
| 1487 |
return -ENXIO; |
| 1488 |
|
| 1489 |
if (irq_num < VGIC_NR_PRIVATE_IRQS) |
| 1490 |
return -EINVAL; |
| 1491 |
|
| 1492 |
return kvm_vgic_inject_irq(kvm, NULL, irq_num, level, NULL); |
| 1493 |
} |
| 1494 |
|
| 1495 |
return -EINVAL; |
| 1496 |
} |
| 1497 |
|
| 1498 |
static unsigned long system_supported_vcpu_features(void) |
| 1499 |
{ |
| 1500 |
unsigned long features = KVM_VCPU_VALID_FEATURES; |
| 1501 |
|
| 1502 |
if (!cpus_have_final_cap(ARM64_HAS_32BIT_EL1)) |
| 1503 |
clear_bit(KVM_ARM_VCPU_EL1_32BIT, &features); |
| 1504 |
|
| 1505 |
if (!kvm_supports_guest_pmuv3()) |
| 1506 |
clear_bit(KVM_ARM_VCPU_PMU_V3, &features); |
| 1507 |
|
| 1508 |
if (!system_supports_sve()) |
| 1509 |
clear_bit(KVM_ARM_VCPU_SVE, &features); |
| 1510 |
|
| 1511 |
if (!kvm_has_full_ptr_auth()) { |
| 1512 |
clear_bit(KVM_ARM_VCPU_PTRAUTH_ADDRESS, &features); |
| 1513 |
clear_bit(KVM_ARM_VCPU_PTRAUTH_GENERIC, &features); |
| 1514 |
} |
| 1515 |
|
| 1516 |
if (!cpus_have_final_cap(ARM64_HAS_NESTED_VIRT)) |
| 1517 |
clear_bit(KVM_ARM_VCPU_HAS_EL2, &features); |
| 1518 |
|
| 1519 |
return features; |
| 1520 |
} |
| 1521 |
|
| 1522 |
static int kvm_vcpu_init_check_features(struct kvm_vcpu *vcpu, |
| 1523 |
const struct kvm_vcpu_init *init) |
| 1524 |
{ |
| 1525 |
unsigned long features = init->features[0]; |
| 1526 |
int i; |
| 1527 |
|
| 1528 |
if (features & ~KVM_VCPU_VALID_FEATURES) |
| 1529 |
return -ENOENT; |
| 1530 |
|
| 1531 |
for (i = 1; i < ARRAY_SIZE(init->features); i++) { |
| 1532 |
if (init->features[i]) |
| 1533 |
return -ENOENT; |
| 1534 |
} |
| 1535 |
|
| 1536 |
if (features & ~system_supported_vcpu_features()) |
| 1537 |
return -EINVAL; |
| 1538 |
|
| 1539 |
/* |
| 1540 |
* For now make sure that both address/generic pointer authentication |
| 1541 |
* features are requested by the userspace together. |
| 1542 |
*/ |
| 1543 |
if (test_bit(KVM_ARM_VCPU_PTRAUTH_ADDRESS, &features) != |
| 1544 |
test_bit(KVM_ARM_VCPU_PTRAUTH_GENERIC, &features)) |
| 1545 |
return -EINVAL; |
| 1546 |
|
| 1547 |
if (!test_bit(KVM_ARM_VCPU_EL1_32BIT, &features)) |
| 1548 |
return 0; |
| 1549 |
|
| 1550 |
/* MTE is incompatible with AArch32 */ |
| 1551 |
if (kvm_has_mte(vcpu->kvm)) |
| 1552 |
return -EINVAL; |
| 1553 |
|
| 1554 |
/* NV is incompatible with AArch32 */ |
| 1555 |
if (test_bit(KVM_ARM_VCPU_HAS_EL2, &features)) |
| 1556 |
return -EINVAL; |
| 1557 |
|
| 1558 |
return 0; |
| 1559 |
} |
| 1560 |
|
| 1561 |
static bool kvm_vcpu_init_changed(struct kvm_vcpu *vcpu, |
| 1562 |
const struct kvm_vcpu_init *init) |
| 1563 |
{ |
| 1564 |
unsigned long features = init->features[0]; |
| 1565 |
|
| 1566 |
return !bitmap_equal(vcpu->kvm->arch.vcpu_features, &features, |
| 1567 |
KVM_VCPU_MAX_FEATURES); |
| 1568 |
} |
| 1569 |
|
| 1570 |
static int kvm_setup_vcpu(struct kvm_vcpu *vcpu) |
| 1571 |
{ |
| 1572 |
struct kvm *kvm = vcpu->kvm; |
| 1573 |
int ret = 0; |
| 1574 |
|
| 1575 |
/* |
| 1576 |
* When the vCPU has a PMU, but no PMU is set for the guest |
| 1577 |
* yet, set the default one. |
| 1578 |
*/ |
| 1579 |
if (kvm_vcpu_has_pmu(vcpu) && !kvm->arch.arm_pmu) |
| 1580 |
ret = kvm_arm_set_default_pmu(kvm); |
| 1581 |
|
| 1582 |
/* Prepare for nested if required */ |
| 1583 |
if (!ret && vcpu_has_nv(vcpu)) |
| 1584 |
ret = kvm_vcpu_init_nested(vcpu); |
| 1585 |
|
| 1586 |
return ret; |
| 1587 |
} |
| 1588 |
|
| 1589 |
static int __kvm_vcpu_set_target(struct kvm_vcpu *vcpu, |
| 1590 |
const struct kvm_vcpu_init *init) |
| 1591 |
{ |
| 1592 |
unsigned long features = init->features[0]; |
| 1593 |
struct kvm *kvm = vcpu->kvm; |
| 1594 |
int ret = -EINVAL; |
| 1595 |
|
| 1596 |
mutex_lock(&kvm->arch.config_lock); |
| 1597 |
|
| 1598 |
if (test_bit(KVM_ARCH_FLAG_VCPU_FEATURES_CONFIGURED, &kvm->arch.flags) && |
| 1599 |
kvm_vcpu_init_changed(vcpu, init)) |
| 1600 |
goto out_unlock; |
| 1601 |
|
| 1602 |
bitmap_copy(kvm->arch.vcpu_features, &features, KVM_VCPU_MAX_FEATURES); |
| 1603 |
|
| 1604 |
ret = kvm_setup_vcpu(vcpu); |
| 1605 |
if (ret) |
| 1606 |
goto out_unlock; |
| 1607 |
|
| 1608 |
/* Now we know what it is, we can reset it. */ |
| 1609 |
kvm_reset_vcpu(vcpu); |
| 1610 |
|
| 1611 |
set_bit(KVM_ARCH_FLAG_VCPU_FEATURES_CONFIGURED, &kvm->arch.flags); |
| 1612 |
vcpu_set_flag(vcpu, VCPU_INITIALIZED); |
| 1613 |
ret = 0; |
| 1614 |
out_unlock: |
| 1615 |
mutex_unlock(&kvm->arch.config_lock); |
| 1616 |
return ret; |
| 1617 |
} |
| 1618 |
|
| 1619 |
static int kvm_vcpu_set_target(struct kvm_vcpu *vcpu, |
| 1620 |
const struct kvm_vcpu_init *init) |
| 1621 |
{ |
| 1622 |
int ret; |
| 1623 |
|
| 1624 |
if (init->target != KVM_ARM_TARGET_GENERIC_V8 && |
| 1625 |
init->target != kvm_target_cpu()) |
| 1626 |
return -EINVAL; |
| 1627 |
|
| 1628 |
ret = kvm_vcpu_init_check_features(vcpu, init); |
| 1629 |
if (ret) |
| 1630 |
return ret; |
| 1631 |
|
| 1632 |
if (!kvm_vcpu_initialized(vcpu)) |
| 1633 |
return __kvm_vcpu_set_target(vcpu, init); |
| 1634 |
|
| 1635 |
if (kvm_vcpu_init_changed(vcpu, init)) |
| 1636 |
return -EINVAL; |
| 1637 |
|
| 1638 |
kvm_reset_vcpu(vcpu); |
| 1639 |
return 0; |
| 1640 |
} |
| 1641 |
|
| 1642 |
static int kvm_arch_vcpu_ioctl_vcpu_init(struct kvm_vcpu *vcpu, |
| 1643 |
struct kvm_vcpu_init *init) |
| 1644 |
{ |
| 1645 |
bool power_off = false; |
| 1646 |
int ret; |
| 1647 |
|
| 1648 |
/* |
| 1649 |
* Treat the power-off vCPU feature as ephemeral. Clear the bit to avoid |
| 1650 |
* reflecting it in the finalized feature set, thus limiting its scope |
| 1651 |
* to a single KVM_ARM_VCPU_INIT call. |
| 1652 |
*/ |
| 1653 |
if (init->features[0] & BIT(KVM_ARM_VCPU_POWER_OFF)) { |
| 1654 |
init->features[0] &= ~BIT(KVM_ARM_VCPU_POWER_OFF); |
| 1655 |
power_off = true; |
| 1656 |
} |
| 1657 |
|
| 1658 |
ret = kvm_vcpu_set_target(vcpu, init); |
| 1659 |
if (ret) |
| 1660 |
return ret; |
| 1661 |
|
| 1662 |
/* |
| 1663 |
* Ensure a rebooted VM will fault in RAM pages and detect if the |
| 1664 |
* guest MMU is turned off and flush the caches as needed. |
| 1665 |
* |
| 1666 |
* S2FWB enforces all memory accesses to RAM being cacheable, |
| 1667 |
* ensuring that the data side is always coherent. We still |
| 1668 |
* need to invalidate the I-cache though, as FWB does *not* |
| 1669 |
* imply CTR_EL0.DIC. |
| 1670 |
*/ |
| 1671 |
if (vcpu_has_run_once(vcpu)) { |
| 1672 |
if (!cpus_have_final_cap(ARM64_HAS_STAGE2_FWB)) |
| 1673 |
stage2_unmap_vm(vcpu->kvm); |
| 1674 |
else |
| 1675 |
icache_inval_all_pou(); |
| 1676 |
} |
| 1677 |
|
| 1678 |
vcpu_reset_hcr(vcpu); |
| 1679 |
|
| 1680 |
/* |
| 1681 |
* Handle the "start in power-off" case. |
| 1682 |
*/ |
| 1683 |
spin_lock(&vcpu->arch.mp_state_lock); |
| 1684 |
|
| 1685 |
if (power_off) |
| 1686 |
__kvm_arm_vcpu_power_off(vcpu); |
| 1687 |
else |
| 1688 |
WRITE_ONCE(vcpu->arch.mp_state.mp_state, KVM_MP_STATE_RUNNABLE); |
| 1689 |
|
| 1690 |
spin_unlock(&vcpu->arch.mp_state_lock); |
| 1691 |
|
| 1692 |
return 0; |
| 1693 |
} |
| 1694 |
|
| 1695 |
static int kvm_arm_vcpu_set_attr(struct kvm_vcpu *vcpu, |
| 1696 |
struct kvm_device_attr *attr) |
| 1697 |
{ |
| 1698 |
int ret = -ENXIO; |
| 1699 |
|
| 1700 |
switch (attr->group) { |
| 1701 |
default: |
| 1702 |
ret = kvm_arm_vcpu_arch_set_attr(vcpu, attr); |
| 1703 |
break; |
| 1704 |
} |
| 1705 |
|
| 1706 |
return ret; |
| 1707 |
} |
| 1708 |
|
| 1709 |
static int kvm_arm_vcpu_get_attr(struct kvm_vcpu *vcpu, |
| 1710 |
struct kvm_device_attr *attr) |
| 1711 |
{ |
| 1712 |
int ret = -ENXIO; |
| 1713 |
|
| 1714 |
switch (attr->group) { |
| 1715 |
default: |
| 1716 |
ret = kvm_arm_vcpu_arch_get_attr(vcpu, attr); |
| 1717 |
break; |
| 1718 |
} |
| 1719 |
|
| 1720 |
return ret; |
| 1721 |
} |
| 1722 |
|
| 1723 |
static int kvm_arm_vcpu_has_attr(struct kvm_vcpu *vcpu, |
| 1724 |
struct kvm_device_attr *attr) |
| 1725 |
{ |
| 1726 |
int ret = -ENXIO; |
| 1727 |
|
| 1728 |
switch (attr->group) { |
| 1729 |
default: |
| 1730 |
ret = kvm_arm_vcpu_arch_has_attr(vcpu, attr); |
| 1731 |
break; |
| 1732 |
} |
| 1733 |
|
| 1734 |
return ret; |
| 1735 |
} |
| 1736 |
|
| 1737 |
static int kvm_arm_vcpu_get_events(struct kvm_vcpu *vcpu, |
| 1738 |
struct kvm_vcpu_events *events) |
| 1739 |
{ |
| 1740 |
memset(events, 0, sizeof(*events)); |
| 1741 |
|
| 1742 |
return __kvm_arm_vcpu_get_events(vcpu, events); |
| 1743 |
} |
| 1744 |
|
| 1745 |
static int kvm_arm_vcpu_set_events(struct kvm_vcpu *vcpu, |
| 1746 |
struct kvm_vcpu_events *events) |
| 1747 |
{ |
| 1748 |
int i; |
| 1749 |
|
| 1750 |
/* check whether the reserved field is zero */ |
| 1751 |
for (i = 0; i < ARRAY_SIZE(events->reserved); i++) |
| 1752 |
if (events->reserved[i]) |
| 1753 |
return -EINVAL; |
| 1754 |
|
| 1755 |
/* check whether the pad field is zero */ |
| 1756 |
for (i = 0; i < ARRAY_SIZE(events->exception.pad); i++) |
| 1757 |
if (events->exception.pad[i]) |
| 1758 |
return -EINVAL; |
| 1759 |
|
| 1760 |
return __kvm_arm_vcpu_set_events(vcpu, events); |
| 1761 |
} |
| 1762 |
|
| 1763 |
long kvm_arch_vcpu_ioctl(struct file *filp, |
| 1764 |
unsigned int ioctl, unsigned long arg) |
| 1765 |
{ |
| 1766 |
struct kvm_vcpu *vcpu = filp->private_data; |
| 1767 |
void __user *argp = (void __user *)arg; |
| 1768 |
struct kvm_device_attr attr; |
| 1769 |
long r; |
| 1770 |
|
| 1771 |
switch (ioctl) { |
| 1772 |
case KVM_ARM_VCPU_INIT: { |
| 1773 |
struct kvm_vcpu_init init; |
| 1774 |
|
| 1775 |
r = -EFAULT; |
| 1776 |
if (copy_from_user(&init, argp, sizeof(init))) |
| 1777 |
break; |
| 1778 |
|
| 1779 |
r = kvm_arch_vcpu_ioctl_vcpu_init(vcpu, &init); |
| 1780 |
break; |
| 1781 |
} |
| 1782 |
case KVM_SET_ONE_REG: |
| 1783 |
case KVM_GET_ONE_REG: { |
| 1784 |
struct kvm_one_reg reg; |
| 1785 |
|
| 1786 |
r = -ENOEXEC; |
| 1787 |
if (unlikely(!kvm_vcpu_initialized(vcpu))) |
| 1788 |
break; |
| 1789 |
|
| 1790 |
r = -EFAULT; |
| 1791 |
if (copy_from_user(®, argp, sizeof(reg))) |
| 1792 |
break; |
| 1793 |
|
| 1794 |
/* |
| 1795 |
* We could owe a reset due to PSCI. Handle the pending reset |
| 1796 |
* here to ensure userspace register accesses are ordered after |
| 1797 |
* the reset. |
| 1798 |
*/ |
| 1799 |
if (kvm_check_request(KVM_REQ_VCPU_RESET, vcpu)) |
| 1800 |
kvm_reset_vcpu(vcpu); |
| 1801 |
|
| 1802 |
if (ioctl == KVM_SET_ONE_REG) |
| 1803 |
r = kvm_arm_set_reg(vcpu, ®); |
| 1804 |
else |
| 1805 |
r = kvm_arm_get_reg(vcpu, ®); |
| 1806 |
break; |
| 1807 |
} |
| 1808 |
case KVM_GET_REG_LIST: { |
| 1809 |
struct kvm_reg_list __user *user_list = argp; |
| 1810 |
struct kvm_reg_list reg_list; |
| 1811 |
unsigned n; |
| 1812 |
|
| 1813 |
r = -ENOEXEC; |
| 1814 |
if (unlikely(!kvm_vcpu_initialized(vcpu))) |
| 1815 |
break; |
| 1816 |
|
| 1817 |
r = -EPERM; |
| 1818 |
if (!kvm_arm_vcpu_is_finalized(vcpu)) |
| 1819 |
break; |
| 1820 |
|
| 1821 |
r = -EFAULT; |
| 1822 |
if (copy_from_user(®_list, user_list, sizeof(reg_list))) |
| 1823 |
break; |
| 1824 |
n = reg_list.n; |
| 1825 |
reg_list.n = kvm_arm_num_regs(vcpu); |
| 1826 |
if (copy_to_user(user_list, ®_list, sizeof(reg_list))) |
| 1827 |
break; |
| 1828 |
r = -E2BIG; |
| 1829 |
if (n < reg_list.n) |
| 1830 |
break; |
| 1831 |
r = kvm_arm_copy_reg_indices(vcpu, user_list->reg); |
| 1832 |
break; |
| 1833 |
} |
| 1834 |
case KVM_SET_DEVICE_ATTR: { |
| 1835 |
r = -EFAULT; |
| 1836 |
if (copy_from_user(&attr, argp, sizeof(attr))) |
| 1837 |
break; |
| 1838 |
r = kvm_arm_vcpu_set_attr(vcpu, &attr); |
| 1839 |
break; |
| 1840 |
} |
| 1841 |
case KVM_GET_DEVICE_ATTR: { |
| 1842 |
r = -EFAULT; |
| 1843 |
if (copy_from_user(&attr, argp, sizeof(attr))) |
| 1844 |
break; |
| 1845 |
r = kvm_arm_vcpu_get_attr(vcpu, &attr); |
| 1846 |
break; |
| 1847 |
} |
| 1848 |
case KVM_HAS_DEVICE_ATTR: { |
| 1849 |
r = -EFAULT; |
| 1850 |
if (copy_from_user(&attr, argp, sizeof(attr))) |
| 1851 |
break; |
| 1852 |
r = kvm_arm_vcpu_has_attr(vcpu, &attr); |
| 1853 |
break; |
| 1854 |
} |
| 1855 |
case KVM_GET_VCPU_EVENTS: { |
| 1856 |
struct kvm_vcpu_events events; |
| 1857 |
|
| 1858 |
if (!kvm_vcpu_initialized(vcpu)) |
| 1859 |
return -ENOEXEC; |
| 1860 |
|
| 1861 |
if (kvm_arm_vcpu_get_events(vcpu, &events)) |
| 1862 |
return -EINVAL; |
| 1863 |
|
| 1864 |
if (copy_to_user(argp, &events, sizeof(events))) |
| 1865 |
return -EFAULT; |
| 1866 |
|
| 1867 |
return 0; |
| 1868 |
} |
| 1869 |
case KVM_SET_VCPU_EVENTS: { |
| 1870 |
struct kvm_vcpu_events events; |
| 1871 |
|
| 1872 |
if (!kvm_vcpu_initialized(vcpu)) |
| 1873 |
return -ENOEXEC; |
| 1874 |
|
| 1875 |
if (copy_from_user(&events, argp, sizeof(events))) |
| 1876 |
return -EFAULT; |
| 1877 |
|
| 1878 |
return kvm_arm_vcpu_set_events(vcpu, &events); |
| 1879 |
} |
| 1880 |
case KVM_ARM_VCPU_FINALIZE: { |
| 1881 |
int what; |
| 1882 |
|
| 1883 |
if (!kvm_vcpu_initialized(vcpu)) |
| 1884 |
return -ENOEXEC; |
| 1885 |
|
| 1886 |
if (get_user(what, (const int __user *)argp)) |
| 1887 |
return -EFAULT; |
| 1888 |
|
| 1889 |
return kvm_arm_vcpu_finalize(vcpu, what); |
| 1890 |
} |
| 1891 |
default: |
| 1892 |
r = -EINVAL; |
| 1893 |
} |
| 1894 |
|
| 1895 |
return r; |
| 1896 |
} |
| 1897 |
|
| 1898 |
long kvm_arch_vcpu_unlocked_ioctl(struct file *filp, unsigned int ioctl, |
| 1899 |
unsigned long arg) |
| 1900 |
{ |
| 1901 |
return -ENOIOCTLCMD; |
| 1902 |
} |
| 1903 |
|
| 1904 |
void kvm_arch_sync_dirty_log(struct kvm *kvm, struct kvm_memory_slot *memslot) |
| 1905 |
{ |
| 1906 |
|
| 1907 |
} |
| 1908 |
|
| 1909 |
static int kvm_vm_ioctl_set_device_addr(struct kvm *kvm, |
| 1910 |
struct kvm_arm_device_addr *dev_addr) |
| 1911 |
{ |
| 1912 |
switch (FIELD_GET(KVM_ARM_DEVICE_ID_MASK, dev_addr->id)) { |
| 1913 |
case KVM_ARM_DEVICE_VGIC_V2: |
| 1914 |
if (!vgic_present) |
| 1915 |
return -ENXIO; |
| 1916 |
return kvm_set_legacy_vgic_v2_addr(kvm, dev_addr); |
| 1917 |
default: |
| 1918 |
return -ENODEV; |
| 1919 |
} |
| 1920 |
} |
| 1921 |
|
| 1922 |
static int kvm_vm_has_attr(struct kvm *kvm, struct kvm_device_attr *attr) |
| 1923 |
{ |
| 1924 |
switch (attr->group) { |
| 1925 |
case KVM_ARM_VM_SMCCC_CTRL: |
| 1926 |
return kvm_vm_smccc_has_attr(kvm, attr); |
| 1927 |
default: |
| 1928 |
return -ENXIO; |
| 1929 |
} |
| 1930 |
} |
| 1931 |
|
| 1932 |
static int kvm_vm_set_attr(struct kvm *kvm, struct kvm_device_attr *attr) |
| 1933 |
{ |
| 1934 |
switch (attr->group) { |
| 1935 |
case KVM_ARM_VM_SMCCC_CTRL: |
| 1936 |
return kvm_vm_smccc_set_attr(kvm, attr); |
| 1937 |
default: |
| 1938 |
return -ENXIO; |
| 1939 |
} |
| 1940 |
} |
| 1941 |
|
| 1942 |
int kvm_arch_vm_ioctl(struct file *filp, unsigned int ioctl, unsigned long arg) |
| 1943 |
{ |
| 1944 |
struct kvm *kvm = filp->private_data; |
| 1945 |
void __user *argp = (void __user *)arg; |
| 1946 |
struct kvm_device_attr attr; |
| 1947 |
|
| 1948 |
if (is_protected_kvm_enabled() && !kvm_pkvm_ioctl_allowed(kvm, ioctl)) |
| 1949 |
return -EINVAL; |
| 1950 |
|
| 1951 |
switch (ioctl) { |
| 1952 |
case KVM_CREATE_IRQCHIP: { |
| 1953 |
int ret; |
| 1954 |
if (!vgic_present) |
| 1955 |
return -ENXIO; |
| 1956 |
mutex_lock(&kvm->lock); |
| 1957 |
ret = kvm_vgic_create(kvm, KVM_DEV_TYPE_ARM_VGIC_V2); |
| 1958 |
mutex_unlock(&kvm->lock); |
| 1959 |
return ret; |
| 1960 |
} |
| 1961 |
case KVM_ARM_SET_DEVICE_ADDR: { |
| 1962 |
struct kvm_arm_device_addr dev_addr; |
| 1963 |
|
| 1964 |
if (copy_from_user(&dev_addr, argp, sizeof(dev_addr))) |
| 1965 |
return -EFAULT; |
| 1966 |
return kvm_vm_ioctl_set_device_addr(kvm, &dev_addr); |
| 1967 |
} |
| 1968 |
case KVM_ARM_PREFERRED_TARGET: { |
| 1969 |
struct kvm_vcpu_init init = { |
| 1970 |
.target = KVM_ARM_TARGET_GENERIC_V8, |
| 1971 |
}; |
| 1972 |
|
| 1973 |
if (copy_to_user(argp, &init, sizeof(init))) |
| 1974 |
return -EFAULT; |
| 1975 |
|
| 1976 |
return 0; |
| 1977 |
} |
| 1978 |
case KVM_ARM_MTE_COPY_TAGS: { |
| 1979 |
struct kvm_arm_copy_mte_tags copy_tags; |
| 1980 |
|
| 1981 |
if (copy_from_user(©_tags, argp, sizeof(copy_tags))) |
| 1982 |
return -EFAULT; |
| 1983 |
return kvm_vm_ioctl_mte_copy_tags(kvm, ©_tags); |
| 1984 |
} |
| 1985 |
case KVM_ARM_SET_COUNTER_OFFSET: { |
| 1986 |
struct kvm_arm_counter_offset offset; |
| 1987 |
|
| 1988 |
if (copy_from_user(&offset, argp, sizeof(offset))) |
| 1989 |
return -EFAULT; |
| 1990 |
return kvm_vm_ioctl_set_counter_offset(kvm, &offset); |
| 1991 |
} |
| 1992 |
case KVM_HAS_DEVICE_ATTR: { |
| 1993 |
if (copy_from_user(&attr, argp, sizeof(attr))) |
| 1994 |
return -EFAULT; |
| 1995 |
|
| 1996 |
return kvm_vm_has_attr(kvm, &attr); |
| 1997 |
} |
| 1998 |
case KVM_SET_DEVICE_ATTR: { |
| 1999 |
if (copy_from_user(&attr, argp, sizeof(attr))) |
| 2000 |
return -EFAULT; |
| 2001 |
|
| 2002 |
return kvm_vm_set_attr(kvm, &attr); |
| 2003 |
} |
| 2004 |
case KVM_ARM_GET_REG_WRITABLE_MASKS: { |
| 2005 |
struct reg_mask_range range; |
| 2006 |
|
| 2007 |
if (copy_from_user(&range, argp, sizeof(range))) |
| 2008 |
return -EFAULT; |
| 2009 |
return kvm_vm_ioctl_get_reg_writable_masks(kvm, &range); |
| 2010 |
} |
| 2011 |
default: |
| 2012 |
return -EINVAL; |
| 2013 |
} |
| 2014 |
} |
| 2015 |
|
| 2016 |
static unsigned long nvhe_percpu_size(void) |
| 2017 |
{ |
| 2018 |
return (unsigned long)CHOOSE_NVHE_SYM(__per_cpu_end) - |
| 2019 |
(unsigned long)CHOOSE_NVHE_SYM(__per_cpu_start); |
| 2020 |
} |
| 2021 |
|
| 2022 |
static unsigned long nvhe_percpu_order(void) |
| 2023 |
{ |
| 2024 |
unsigned long size = nvhe_percpu_size(); |
| 2025 |
|
| 2026 |
return size ? get_order(size) : 0; |
| 2027 |
} |
| 2028 |
|
| 2029 |
static size_t pkvm_host_sve_state_order(void) |
| 2030 |
{ |
| 2031 |
return get_order(pkvm_host_sve_state_size()); |
| 2032 |
} |
| 2033 |
|
| 2034 |
/* A lookup table holding the hypervisor VA for each vector slot */ |
| 2035 |
static void *hyp_spectre_vector_selector[BP_HARDEN_EL2_SLOTS]; |
| 2036 |
|
| 2037 |
static void kvm_init_vector_slot(void *base, enum arm64_hyp_spectre_vector slot) |
| 2038 |
{ |
| 2039 |
hyp_spectre_vector_selector[slot] = __kvm_vector_slot2addr(base, slot); |
| 2040 |
} |
| 2041 |
|
| 2042 |
static int kvm_init_vector_slots(void) |
| 2043 |
{ |
| 2044 |
int err; |
| 2045 |
void *base; |
| 2046 |
|
| 2047 |
base = kern_hyp_va(kvm_ksym_ref(__kvm_hyp_vector)); |
| 2048 |
kvm_init_vector_slot(base, HYP_VECTOR_DIRECT); |
| 2049 |
|
| 2050 |
base = kern_hyp_va(kvm_ksym_ref(__bp_harden_hyp_vecs)); |
| 2051 |
kvm_init_vector_slot(base, HYP_VECTOR_SPECTRE_DIRECT); |
| 2052 |
|
| 2053 |
if (kvm_system_needs_idmapped_vectors() && |
| 2054 |
!is_protected_kvm_enabled()) { |
| 2055 |
err = create_hyp_exec_mappings(__pa_symbol(__bp_harden_hyp_vecs), |
| 2056 |
__BP_HARDEN_HYP_VECS_SZ, &base); |
| 2057 |
if (err) |
| 2058 |
return err; |
| 2059 |
} |
| 2060 |
|
| 2061 |
kvm_init_vector_slot(base, HYP_VECTOR_INDIRECT); |
| 2062 |
kvm_init_vector_slot(base, HYP_VECTOR_SPECTRE_INDIRECT); |
| 2063 |
return 0; |
| 2064 |
} |
| 2065 |
|
| 2066 |
static void __init cpu_prepare_hyp_mode(int cpu, u32 hyp_va_bits) |
| 2067 |
{ |
| 2068 |
struct kvm_nvhe_init_params *params = per_cpu_ptr_nvhe_sym(kvm_init_params, cpu); |
| 2069 |
unsigned long tcr; |
| 2070 |
|
| 2071 |
/* |
| 2072 |
* Calculate the raw per-cpu offset without a translation from the |
| 2073 |
* kernel's mapping to the linear mapping, and store it in tpidr_el2 |
| 2074 |
* so that we can use adr_l to access per-cpu variables in EL2. |
| 2075 |
* Also drop the KASAN tag which gets in the way... |
| 2076 |
*/ |
| 2077 |
params->tpidr_el2 = (unsigned long)kasan_reset_tag(per_cpu_ptr_nvhe_sym(__per_cpu_start, cpu)) - |
| 2078 |
(unsigned long)kvm_ksym_ref(CHOOSE_NVHE_SYM(__per_cpu_start)); |
| 2079 |
|
| 2080 |
params->mair_el2 = read_sysreg(mair_el1); |
| 2081 |
|
| 2082 |
tcr = read_sysreg(tcr_el1); |
| 2083 |
if (cpus_have_final_cap(ARM64_KVM_HVHE)) { |
| 2084 |
tcr &= ~(TCR_HD | TCR_HA | TCR_A1 | TCR_T0SZ_MASK); |
| 2085 |
tcr |= TCR_EPD1_MASK; |
| 2086 |
} else { |
| 2087 |
unsigned long ips = FIELD_GET(TCR_IPS_MASK, tcr); |
| 2088 |
|
| 2089 |
tcr &= TCR_EL2_MASK; |
| 2090 |
tcr |= TCR_EL2_RES1 | FIELD_PREP(TCR_EL2_PS_MASK, ips); |
| 2091 |
if (lpa2_is_enabled()) |
| 2092 |
tcr |= TCR_EL2_DS; |
| 2093 |
} |
| 2094 |
tcr |= TCR_T0SZ(hyp_va_bits); |
| 2095 |
params->tcr_el2 = tcr; |
| 2096 |
|
| 2097 |
params->pgd_pa = kvm_mmu_get_httbr(); |
| 2098 |
if (is_protected_kvm_enabled()) |
| 2099 |
params->hcr_el2 = HCR_HOST_NVHE_PROTECTED_FLAGS; |
| 2100 |
else |
| 2101 |
params->hcr_el2 = HCR_HOST_NVHE_FLAGS; |
| 2102 |
|
| 2103 |
if (system_supports_mte()) |
| 2104 |
params->hcr_el2 |= HCR_ATA; |
| 2105 |
else |
| 2106 |
params->hcr_el2 |= HCR_TID5; |
| 2107 |
|
| 2108 |
if (cpus_have_final_cap(ARM64_KVM_HVHE)) |
| 2109 |
params->hcr_el2 |= HCR_E2H; |
| 2110 |
params->vttbr = params->vtcr = 0; |
| 2111 |
|
| 2112 |
/* |
| 2113 |
* Flush the init params from the data cache because the struct will |
| 2114 |
* be read while the MMU is off. |
| 2115 |
*/ |
| 2116 |
kvm_flush_dcache_to_poc(params, sizeof(*params)); |
| 2117 |
} |
| 2118 |
|
| 2119 |
static void hyp_install_host_vector(void) |
| 2120 |
{ |
| 2121 |
struct kvm_nvhe_init_params *params; |
| 2122 |
struct arm_smccc_res res; |
| 2123 |
|
| 2124 |
/* Switch from the HYP stub to our own HYP init vector */ |
| 2125 |
__hyp_set_vectors(kvm_get_idmap_vector()); |
| 2126 |
|
| 2127 |
/* |
| 2128 |
* Call initialization code, and switch to the full blown HYP code. |
| 2129 |
* If the cpucaps haven't been finalized yet, something has gone very |
| 2130 |
* wrong, and hyp will crash and burn when it uses any |
| 2131 |
* cpus_have_*_cap() wrapper. |
| 2132 |
*/ |
| 2133 |
BUG_ON(!system_capabilities_finalized()); |
| 2134 |
params = this_cpu_ptr_nvhe_sym(kvm_init_params); |
| 2135 |
arm_smccc_1_1_hvc(KVM_HOST_SMCCC_FUNC(__kvm_hyp_init), virt_to_phys(params), &res); |
| 2136 |
WARN_ON(res.a0 != SMCCC_RET_SUCCESS); |
| 2137 |
} |
| 2138 |
|
| 2139 |
static void cpu_init_hyp_mode(void) |
| 2140 |
{ |
| 2141 |
hyp_install_host_vector(); |
| 2142 |
|
| 2143 |
/* |
| 2144 |
* Disabling SSBD on a non-VHE system requires us to enable SSBS |
| 2145 |
* at EL2. |
| 2146 |
*/ |
| 2147 |
if (this_cpu_has_cap(ARM64_SSBS) && |
| 2148 |
arm64_get_spectre_v4_state() == SPECTRE_VULNERABLE) { |
| 2149 |
kvm_call_hyp_nvhe(__kvm_enable_ssbs); |
| 2150 |
} |
| 2151 |
} |
| 2152 |
|
| 2153 |
static void cpu_hyp_reset(void) |
| 2154 |
{ |
| 2155 |
if (!is_kernel_in_hyp_mode()) |
| 2156 |
__hyp_reset_vectors(); |
| 2157 |
} |
| 2158 |
|
| 2159 |
/* |
| 2160 |
* EL2 vectors can be mapped and rerouted in a number of ways, |
| 2161 |
* depending on the kernel configuration and CPU present: |
| 2162 |
* |
| 2163 |
* - If the CPU is affected by Spectre-v2, the hardening sequence is |
| 2164 |
* placed in one of the vector slots, which is executed before jumping |
| 2165 |
* to the real vectors. |
| 2166 |
* |
| 2167 |
* - If the CPU also has the ARM64_SPECTRE_V3A cap, the slot |
| 2168 |
* containing the hardening sequence is mapped next to the idmap page, |
| 2169 |
* and executed before jumping to the real vectors. |
| 2170 |
* |
| 2171 |
* - If the CPU only has the ARM64_SPECTRE_V3A cap, then an |
| 2172 |
* empty slot is selected, mapped next to the idmap page, and |
| 2173 |
* executed before jumping to the real vectors. |
| 2174 |
* |
| 2175 |
* Note that ARM64_SPECTRE_V3A is somewhat incompatible with |
| 2176 |
* VHE, as we don't have hypervisor-specific mappings. If the system |
| 2177 |
* is VHE and yet selects this capability, it will be ignored. |
| 2178 |
*/ |
| 2179 |
static void cpu_set_hyp_vector(void) |
| 2180 |
{ |
| 2181 |
struct bp_hardening_data *data = this_cpu_ptr(&bp_hardening_data); |
| 2182 |
void *vector = hyp_spectre_vector_selector[data->slot]; |
| 2183 |
|
| 2184 |
if (!is_protected_kvm_enabled()) |
| 2185 |
*this_cpu_ptr_hyp_sym(kvm_hyp_vector) = (unsigned long)vector; |
| 2186 |
else |
| 2187 |
kvm_call_hyp_nvhe(__pkvm_cpu_set_vector, data->slot); |
| 2188 |
} |
| 2189 |
|
| 2190 |
static void cpu_hyp_init_context(void) |
| 2191 |
{ |
| 2192 |
kvm_init_host_cpu_context(host_data_ptr(host_ctxt)); |
| 2193 |
kvm_init_host_debug_data(); |
| 2194 |
|
| 2195 |
if (!is_kernel_in_hyp_mode()) |
| 2196 |
cpu_init_hyp_mode(); |
| 2197 |
} |
| 2198 |
|
| 2199 |
static void cpu_hyp_init_features(void) |
| 2200 |
{ |
| 2201 |
cpu_set_hyp_vector(); |
| 2202 |
|
| 2203 |
if (is_kernel_in_hyp_mode()) { |
| 2204 |
kvm_timer_init_vhe(); |
| 2205 |
kvm_debug_init_vhe(); |
| 2206 |
} |
| 2207 |
|
| 2208 |
if (vgic_present) |
| 2209 |
kvm_vgic_init_cpu_hardware(); |
| 2210 |
} |
| 2211 |
|
| 2212 |
static void cpu_hyp_reinit(void) |
| 2213 |
{ |
| 2214 |
cpu_hyp_reset(); |
| 2215 |
cpu_hyp_init_context(); |
| 2216 |
cpu_hyp_init_features(); |
| 2217 |
} |
| 2218 |
|
| 2219 |
static void cpu_hyp_init(void *discard) |
| 2220 |
{ |
| 2221 |
if (!__this_cpu_read(kvm_hyp_initialized)) { |
| 2222 |
cpu_hyp_reinit(); |
| 2223 |
__this_cpu_write(kvm_hyp_initialized, 1); |
| 2224 |
} |
| 2225 |
} |
| 2226 |
|
| 2227 |
static void cpu_hyp_uninit(void *discard) |
| 2228 |
{ |
| 2229 |
if (!is_protected_kvm_enabled() && __this_cpu_read(kvm_hyp_initialized)) { |
| 2230 |
cpu_hyp_reset(); |
| 2231 |
__this_cpu_write(kvm_hyp_initialized, 0); |
| 2232 |
} |
| 2233 |
} |
| 2234 |
|
| 2235 |
int kvm_arch_enable_virtualization_cpu(void) |
| 2236 |
{ |
| 2237 |
/* |
| 2238 |
* Most calls to this function are made with migration |
| 2239 |
* disabled, but not with preemption disabled. The former is |
| 2240 |
* enough to ensure correctness, but most of the helpers |
| 2241 |
* expect the later and will throw a tantrum otherwise. |
| 2242 |
*/ |
| 2243 |
preempt_disable(); |
| 2244 |
|
| 2245 |
cpu_hyp_init(NULL); |
| 2246 |
|
| 2247 |
kvm_vgic_cpu_up(); |
| 2248 |
kvm_timer_cpu_up(); |
| 2249 |
|
| 2250 |
preempt_enable(); |
| 2251 |
|
| 2252 |
return 0; |
| 2253 |
} |
| 2254 |
|
| 2255 |
void kvm_arch_disable_virtualization_cpu(void) |
| 2256 |
{ |
| 2257 |
kvm_timer_cpu_down(); |
| 2258 |
kvm_vgic_cpu_down(); |
| 2259 |
|
| 2260 |
if (!is_protected_kvm_enabled()) |
| 2261 |
cpu_hyp_uninit(NULL); |
| 2262 |
} |
| 2263 |
|
| 2264 |
#ifdef CONFIG_CPU_PM |
| 2265 |
static int hyp_init_cpu_pm_notifier(struct notifier_block *self, |
| 2266 |
unsigned long cmd, |
| 2267 |
void *v) |
| 2268 |
{ |
| 2269 |
/* |
| 2270 |
* kvm_hyp_initialized is left with its old value over |
| 2271 |
* PM_ENTER->PM_EXIT. It is used to indicate PM_EXIT should |
| 2272 |
* re-enable hyp. |
| 2273 |
*/ |
| 2274 |
switch (cmd) { |
| 2275 |
case CPU_PM_ENTER: |
| 2276 |
if (__this_cpu_read(kvm_hyp_initialized)) |
| 2277 |
/* |
| 2278 |
* don't update kvm_hyp_initialized here |
| 2279 |
* so that the hyp will be re-enabled |
| 2280 |
* when we resume. See below. |
| 2281 |
*/ |
| 2282 |
cpu_hyp_reset(); |
| 2283 |
|
| 2284 |
return NOTIFY_OK; |
| 2285 |
case CPU_PM_ENTER_FAILED: |
| 2286 |
case CPU_PM_EXIT: |
| 2287 |
if (__this_cpu_read(kvm_hyp_initialized)) |
| 2288 |
/* The hyp was enabled before suspend. */ |
| 2289 |
cpu_hyp_reinit(); |
| 2290 |
|
| 2291 |
return NOTIFY_OK; |
| 2292 |
|
| 2293 |
default: |
| 2294 |
return NOTIFY_DONE; |
| 2295 |
} |
| 2296 |
} |
| 2297 |
|
| 2298 |
static struct notifier_block hyp_init_cpu_pm_nb = { |
| 2299 |
.notifier_call = hyp_init_cpu_pm_notifier, |
| 2300 |
}; |
| 2301 |
|
| 2302 |
static void __init hyp_cpu_pm_init(void) |
| 2303 |
{ |
| 2304 |
if (!is_protected_kvm_enabled()) |
| 2305 |
cpu_pm_register_notifier(&hyp_init_cpu_pm_nb); |
| 2306 |
} |
| 2307 |
static void __init hyp_cpu_pm_exit(void) |
| 2308 |
{ |
| 2309 |
if (!is_protected_kvm_enabled()) |
| 2310 |
cpu_pm_unregister_notifier(&hyp_init_cpu_pm_nb); |
| 2311 |
} |
| 2312 |
#else |
| 2313 |
static inline void __init hyp_cpu_pm_init(void) |
| 2314 |
{ |
| 2315 |
} |
| 2316 |
static inline void __init hyp_cpu_pm_exit(void) |
| 2317 |
{ |
| 2318 |
} |
| 2319 |
#endif |
| 2320 |
|
| 2321 |
static void __init init_cpu_logical_map(void) |
| 2322 |
{ |
| 2323 |
unsigned int cpu; |
| 2324 |
|
| 2325 |
/* |
| 2326 |
* Copy the MPIDR <-> logical CPU ID mapping to hyp. |
| 2327 |
* Only copy the set of online CPUs whose features have been checked |
| 2328 |
* against the finalized system capabilities. The hypervisor will not |
| 2329 |
* allow any other CPUs from the `possible` set to boot. |
| 2330 |
*/ |
| 2331 |
for_each_online_cpu(cpu) |
| 2332 |
hyp_cpu_logical_map[cpu] = cpu_logical_map(cpu); |
| 2333 |
} |
| 2334 |
|
| 2335 |
#define init_psci_0_1_impl_state(config, what) \ |
| 2336 |
config.psci_0_1_ ## what ## _implemented = psci_ops.what |
| 2337 |
|
| 2338 |
static bool __init init_psci_relay(void) |
| 2339 |
{ |
| 2340 |
/* |
| 2341 |
* If PSCI has not been initialized, protected KVM cannot install |
| 2342 |
* itself on newly booted CPUs. |
| 2343 |
*/ |
| 2344 |
if (!psci_ops.get_version) { |
| 2345 |
kvm_err("Cannot initialize protected mode without PSCI\n"); |
| 2346 |
return false; |
| 2347 |
} |
| 2348 |
|
| 2349 |
kvm_host_psci_config.version = psci_ops.get_version(); |
| 2350 |
kvm_host_psci_config.smccc_version = arm_smccc_get_version(); |
| 2351 |
|
| 2352 |
if (kvm_host_psci_config.version == PSCI_VERSION(0, 1)) { |
| 2353 |
kvm_host_psci_config.function_ids_0_1 = get_psci_0_1_function_ids(); |
| 2354 |
init_psci_0_1_impl_state(kvm_host_psci_config, cpu_suspend); |
| 2355 |
init_psci_0_1_impl_state(kvm_host_psci_config, cpu_on); |
| 2356 |
init_psci_0_1_impl_state(kvm_host_psci_config, cpu_off); |
| 2357 |
init_psci_0_1_impl_state(kvm_host_psci_config, migrate); |
| 2358 |
} |
| 2359 |
return true; |
| 2360 |
} |
| 2361 |
|
| 2362 |
static int __init init_subsystems(void) |
| 2363 |
{ |
| 2364 |
int err = 0; |
| 2365 |
|
| 2366 |
/* |
| 2367 |
* Enable hardware so that subsystem initialisation can access EL2. |
| 2368 |
*/ |
| 2369 |
on_each_cpu(cpu_hyp_init, NULL, 1); |
| 2370 |
|
| 2371 |
/* |
| 2372 |
* Register CPU lower-power notifier |
| 2373 |
*/ |
| 2374 |
hyp_cpu_pm_init(); |
| 2375 |
|
| 2376 |
/* |
| 2377 |
* Init HYP view of VGIC |
| 2378 |
*/ |
| 2379 |
err = kvm_vgic_hyp_init(); |
| 2380 |
switch (err) { |
| 2381 |
case 0: |
| 2382 |
vgic_present = true; |
| 2383 |
break; |
| 2384 |
case -ENODEV: |
| 2385 |
case -ENXIO: |
| 2386 |
/* |
| 2387 |
* No VGIC? No pKVM for you. |
| 2388 |
* |
| 2389 |
* Protected mode assumes that VGICv3 is present, so no point |
| 2390 |
* in trying to hobble along if vgic initialization fails. |
| 2391 |
*/ |
| 2392 |
if (is_protected_kvm_enabled()) |
| 2393 |
goto out; |
| 2394 |
|
| 2395 |
/* |
| 2396 |
* Otherwise, userspace could choose to implement a GIC for its |
| 2397 |
* guest on non-cooperative hardware. |
| 2398 |
*/ |
| 2399 |
vgic_present = false; |
| 2400 |
err = 0; |
| 2401 |
break; |
| 2402 |
default: |
| 2403 |
goto out; |
| 2404 |
} |
| 2405 |
|
| 2406 |
if (kvm_mode == KVM_MODE_NV && |
| 2407 |
!(vgic_present && (kvm_vgic_global_state.type == VGIC_V3 || |
| 2408 |
kvm_vgic_global_state.has_gcie_v3_compat))) { |
| 2409 |
kvm_err("NV support requires GICv3 or GICv5 with legacy support, giving up\n"); |
| 2410 |
err = -EINVAL; |
| 2411 |
goto out; |
| 2412 |
} |
| 2413 |
|
| 2414 |
/* |
| 2415 |
* Init HYP architected timer support |
| 2416 |
*/ |
| 2417 |
err = kvm_timer_hyp_init(vgic_present); |
| 2418 |
if (err) |
| 2419 |
goto out; |
| 2420 |
|
| 2421 |
kvm_register_perf_callbacks(); |
| 2422 |
|
| 2423 |
out: |
| 2424 |
if (err) |
| 2425 |
hyp_cpu_pm_exit(); |
| 2426 |
|
| 2427 |
if (err || !is_protected_kvm_enabled()) |
| 2428 |
on_each_cpu(cpu_hyp_uninit, NULL, 1); |
| 2429 |
|
| 2430 |
return err; |
| 2431 |
} |
| 2432 |
|
| 2433 |
static void __init teardown_subsystems(void) |
| 2434 |
{ |
| 2435 |
kvm_unregister_perf_callbacks(); |
| 2436 |
hyp_cpu_pm_exit(); |
| 2437 |
} |
| 2438 |
|
| 2439 |
static void __init teardown_hyp_mode(void) |
| 2440 |
{ |
| 2441 |
bool free_sve = system_supports_sve() && is_protected_kvm_enabled(); |
| 2442 |
int cpu; |
| 2443 |
|
| 2444 |
free_hyp_pgds(); |
| 2445 |
for_each_possible_cpu(cpu) { |
| 2446 |
if (per_cpu(kvm_hyp_initialized, cpu)) |
| 2447 |
continue; |
| 2448 |
|
| 2449 |
free_pages(per_cpu(kvm_arm_hyp_stack_base, cpu), NVHE_STACK_SHIFT - PAGE_SHIFT); |
| 2450 |
|
| 2451 |
if (!kvm_nvhe_sym(kvm_arm_hyp_percpu_base)[cpu]) |
| 2452 |
continue; |
| 2453 |
|
| 2454 |
if (free_sve) { |
| 2455 |
struct cpu_sve_state *sve_state; |
| 2456 |
|
| 2457 |
sve_state = per_cpu_ptr_nvhe_sym(kvm_host_data, cpu)->sve_state; |
| 2458 |
free_pages((unsigned long) sve_state, pkvm_host_sve_state_order()); |
| 2459 |
} |
| 2460 |
|
| 2461 |
free_pages(kvm_nvhe_sym(kvm_arm_hyp_percpu_base)[cpu], nvhe_percpu_order()); |
| 2462 |
|
| 2463 |
} |
| 2464 |
} |
| 2465 |
|
| 2466 |
static int __init do_pkvm_init(u32 hyp_va_bits) |
| 2467 |
{ |
| 2468 |
void *per_cpu_base = kvm_ksym_ref(kvm_nvhe_sym(kvm_arm_hyp_percpu_base)); |
| 2469 |
int ret; |
| 2470 |
|
| 2471 |
preempt_disable(); |
| 2472 |
cpu_hyp_init_context(); |
| 2473 |
ret = kvm_call_hyp_nvhe(__pkvm_init, hyp_mem_base, hyp_mem_size, |
| 2474 |
num_possible_cpus(), kern_hyp_va(per_cpu_base), |
| 2475 |
hyp_va_bits); |
| 2476 |
cpu_hyp_init_features(); |
| 2477 |
|
| 2478 |
/* |
| 2479 |
* The stub hypercalls are now disabled, so set our local flag to |
| 2480 |
* prevent a later re-init attempt in kvm_arch_enable_virtualization_cpu(). |
| 2481 |
*/ |
| 2482 |
__this_cpu_write(kvm_hyp_initialized, 1); |
| 2483 |
preempt_enable(); |
| 2484 |
|
| 2485 |
return ret; |
| 2486 |
} |
| 2487 |
|
| 2488 |
static u64 get_hyp_id_aa64pfr0_el1(void) |
| 2489 |
{ |
| 2490 |
/* |
| 2491 |
* Track whether the system isn't affected by spectre/meltdown in the |
| 2492 |
* hypervisor's view of id_aa64pfr0_el1, used for protected VMs. |
| 2493 |
* Although this is per-CPU, we make it global for simplicity, e.g., not |
| 2494 |
* to have to worry about vcpu migration. |
| 2495 |
* |
| 2496 |
* Unlike for non-protected VMs, userspace cannot override this for |
| 2497 |
* protected VMs. |
| 2498 |
*/ |
| 2499 |
u64 val = read_sanitised_ftr_reg(SYS_ID_AA64PFR0_EL1); |
| 2500 |
|
| 2501 |
val &= ~(ID_AA64PFR0_EL1_CSV2 | |
| 2502 |
ID_AA64PFR0_EL1_CSV3); |
| 2503 |
|
| 2504 |
val |= FIELD_PREP(ID_AA64PFR0_EL1_CSV2, |
| 2505 |
arm64_get_spectre_v2_state() == SPECTRE_UNAFFECTED); |
| 2506 |
val |= FIELD_PREP(ID_AA64PFR0_EL1_CSV3, |
| 2507 |
arm64_get_meltdown_state() == SPECTRE_UNAFFECTED); |
| 2508 |
|
| 2509 |
return val; |
| 2510 |
} |
| 2511 |
|
| 2512 |
static void kvm_hyp_init_symbols(void) |
| 2513 |
{ |
| 2514 |
kvm_nvhe_sym(id_aa64pfr0_el1_sys_val) = get_hyp_id_aa64pfr0_el1(); |
| 2515 |
kvm_nvhe_sym(id_aa64pfr1_el1_sys_val) = read_sanitised_ftr_reg(SYS_ID_AA64PFR1_EL1); |
| 2516 |
kvm_nvhe_sym(id_aa64isar0_el1_sys_val) = read_sanitised_ftr_reg(SYS_ID_AA64ISAR0_EL1); |
| 2517 |
kvm_nvhe_sym(id_aa64isar1_el1_sys_val) = read_sanitised_ftr_reg(SYS_ID_AA64ISAR1_EL1); |
| 2518 |
kvm_nvhe_sym(id_aa64isar2_el1_sys_val) = read_sanitised_ftr_reg(SYS_ID_AA64ISAR2_EL1); |
| 2519 |
kvm_nvhe_sym(id_aa64mmfr0_el1_sys_val) = read_sanitised_ftr_reg(SYS_ID_AA64MMFR0_EL1); |
| 2520 |
kvm_nvhe_sym(id_aa64mmfr1_el1_sys_val) = read_sanitised_ftr_reg(SYS_ID_AA64MMFR1_EL1); |
| 2521 |
kvm_nvhe_sym(id_aa64mmfr2_el1_sys_val) = read_sanitised_ftr_reg(SYS_ID_AA64MMFR2_EL1); |
| 2522 |
kvm_nvhe_sym(id_aa64smfr0_el1_sys_val) = read_sanitised_ftr_reg(SYS_ID_AA64SMFR0_EL1); |
| 2523 |
kvm_nvhe_sym(__icache_flags) = __icache_flags; |
| 2524 |
kvm_nvhe_sym(kvm_arm_vmid_bits) = kvm_arm_vmid_bits; |
| 2525 |
|
| 2526 |
/* Propagate the FGT state to the nVHE side */ |
| 2527 |
kvm_nvhe_sym(hfgrtr_masks) = hfgrtr_masks; |
| 2528 |
kvm_nvhe_sym(hfgwtr_masks) = hfgwtr_masks; |
| 2529 |
kvm_nvhe_sym(hfgitr_masks) = hfgitr_masks; |
| 2530 |
kvm_nvhe_sym(hdfgrtr_masks) = hdfgrtr_masks; |
| 2531 |
kvm_nvhe_sym(hdfgwtr_masks) = hdfgwtr_masks; |
| 2532 |
kvm_nvhe_sym(hafgrtr_masks) = hafgrtr_masks; |
| 2533 |
kvm_nvhe_sym(hfgrtr2_masks) = hfgrtr2_masks; |
| 2534 |
kvm_nvhe_sym(hfgwtr2_masks) = hfgwtr2_masks; |
| 2535 |
kvm_nvhe_sym(hfgitr2_masks) = hfgitr2_masks; |
| 2536 |
kvm_nvhe_sym(hdfgrtr2_masks)= hdfgrtr2_masks; |
| 2537 |
kvm_nvhe_sym(hdfgwtr2_masks)= hdfgwtr2_masks; |
| 2538 |
|
| 2539 |
/* |
| 2540 |
* Flush entire BSS since part of its data containing init symbols is read |
| 2541 |
* while the MMU is off. |
| 2542 |
*/ |
| 2543 |
kvm_flush_dcache_to_poc(kvm_ksym_ref(__hyp_bss_start), |
| 2544 |
kvm_ksym_ref(__hyp_bss_end) - kvm_ksym_ref(__hyp_bss_start)); |
| 2545 |
} |
| 2546 |
|
| 2547 |
static int __init kvm_hyp_init_protection(u32 hyp_va_bits) |
| 2548 |
{ |
| 2549 |
void *addr = phys_to_virt(hyp_mem_base); |
| 2550 |
int ret; |
| 2551 |
|
| 2552 |
ret = create_hyp_mappings(addr, addr + hyp_mem_size, PAGE_HYP); |
| 2553 |
if (ret) |
| 2554 |
return ret; |
| 2555 |
|
| 2556 |
ret = do_pkvm_init(hyp_va_bits); |
| 2557 |
if (ret) |
| 2558 |
return ret; |
| 2559 |
|
| 2560 |
free_hyp_pgds(); |
| 2561 |
|
| 2562 |
return 0; |
| 2563 |
} |
| 2564 |
|
| 2565 |
static int init_pkvm_host_sve_state(void) |
| 2566 |
{ |
| 2567 |
int cpu; |
| 2568 |
|
| 2569 |
if (!system_supports_sve()) |
| 2570 |
return 0; |
| 2571 |
|
| 2572 |
/* Allocate pages for host sve state in protected mode. */ |
| 2573 |
for_each_possible_cpu(cpu) { |
| 2574 |
struct page *page = alloc_pages(GFP_KERNEL, pkvm_host_sve_state_order()); |
| 2575 |
|
| 2576 |
if (!page) |
| 2577 |
return -ENOMEM; |
| 2578 |
|
| 2579 |
per_cpu_ptr_nvhe_sym(kvm_host_data, cpu)->sve_state = page_address(page); |
| 2580 |
} |
| 2581 |
|
| 2582 |
/* |
| 2583 |
* Don't map the pages in hyp since these are only used in protected |
| 2584 |
* mode, which will (re)create its own mapping when initialized. |
| 2585 |
*/ |
| 2586 |
|
| 2587 |
return 0; |
| 2588 |
} |
| 2589 |
|
| 2590 |
/* |
| 2591 |
* Finalizes the initialization of hyp mode, once everything else is initialized |
| 2592 |
* and the initialziation process cannot fail. |
| 2593 |
*/ |
| 2594 |
static void finalize_init_hyp_mode(void) |
| 2595 |
{ |
| 2596 |
int cpu; |
| 2597 |
|
| 2598 |
if (system_supports_sve() && is_protected_kvm_enabled()) { |
| 2599 |
for_each_possible_cpu(cpu) { |
| 2600 |
struct cpu_sve_state *sve_state; |
| 2601 |
|
| 2602 |
sve_state = per_cpu_ptr_nvhe_sym(kvm_host_data, cpu)->sve_state; |
| 2603 |
per_cpu_ptr_nvhe_sym(kvm_host_data, cpu)->sve_state = |
| 2604 |
kern_hyp_va(sve_state); |
| 2605 |
} |
| 2606 |
} |
| 2607 |
} |
| 2608 |
|
| 2609 |
static void pkvm_hyp_init_ptrauth(void) |
| 2610 |
{ |
| 2611 |
struct kvm_cpu_context *hyp_ctxt; |
| 2612 |
int cpu; |
| 2613 |
|
| 2614 |
for_each_possible_cpu(cpu) { |
| 2615 |
hyp_ctxt = per_cpu_ptr_nvhe_sym(kvm_hyp_ctxt, cpu); |
| 2616 |
hyp_ctxt->sys_regs[APIAKEYLO_EL1] = get_random_long(); |
| 2617 |
hyp_ctxt->sys_regs[APIAKEYHI_EL1] = get_random_long(); |
| 2618 |
hyp_ctxt->sys_regs[APIBKEYLO_EL1] = get_random_long(); |
| 2619 |
hyp_ctxt->sys_regs[APIBKEYHI_EL1] = get_random_long(); |
| 2620 |
hyp_ctxt->sys_regs[APDAKEYLO_EL1] = get_random_long(); |
| 2621 |
hyp_ctxt->sys_regs[APDAKEYHI_EL1] = get_random_long(); |
| 2622 |
hyp_ctxt->sys_regs[APDBKEYLO_EL1] = get_random_long(); |
| 2623 |
hyp_ctxt->sys_regs[APDBKEYHI_EL1] = get_random_long(); |
| 2624 |
hyp_ctxt->sys_regs[APGAKEYLO_EL1] = get_random_long(); |
| 2625 |
hyp_ctxt->sys_regs[APGAKEYHI_EL1] = get_random_long(); |
| 2626 |
} |
| 2627 |
} |
| 2628 |
|
| 2629 |
/* Inits Hyp-mode on all online CPUs */ |
| 2630 |
static int __init init_hyp_mode(void) |
| 2631 |
{ |
| 2632 |
u32 hyp_va_bits = kvm_hyp_va_bits(); |
| 2633 |
int cpu; |
| 2634 |
int err = -ENOMEM; |
| 2635 |
|
| 2636 |
/* |
| 2637 |
* The protected Hyp-mode cannot be initialized if the memory pool |
| 2638 |
* allocation has failed. |
| 2639 |
*/ |
| 2640 |
if (is_protected_kvm_enabled() && !hyp_mem_base) |
| 2641 |
goto out_err; |
| 2642 |
|
| 2643 |
/* |
| 2644 |
* Allocate Hyp PGD and setup Hyp identity mapping |
| 2645 |
*/ |
| 2646 |
err = kvm_mmu_init(hyp_va_bits); |
| 2647 |
if (err) |
| 2648 |
goto out_err; |
| 2649 |
|
| 2650 |
/* |
| 2651 |
* Allocate stack pages for Hypervisor-mode |
| 2652 |
*/ |
| 2653 |
for_each_possible_cpu(cpu) { |
| 2654 |
unsigned long stack_base; |
| 2655 |
|
| 2656 |
stack_base = __get_free_pages(GFP_KERNEL, NVHE_STACK_SHIFT - PAGE_SHIFT); |
| 2657 |
if (!stack_base) { |
| 2658 |
err = -ENOMEM; |
| 2659 |
goto out_err; |
| 2660 |
} |
| 2661 |
|
| 2662 |
per_cpu(kvm_arm_hyp_stack_base, cpu) = stack_base; |
| 2663 |
} |
| 2664 |
|
| 2665 |
/* |
| 2666 |
* Allocate and initialize pages for Hypervisor-mode percpu regions. |
| 2667 |
*/ |
| 2668 |
for_each_possible_cpu(cpu) { |
| 2669 |
struct page *page; |
| 2670 |
void *page_addr; |
| 2671 |
|
| 2672 |
page = alloc_pages(GFP_KERNEL, nvhe_percpu_order()); |
| 2673 |
if (!page) { |
| 2674 |
err = -ENOMEM; |
| 2675 |
goto out_err; |
| 2676 |
} |
| 2677 |
|
| 2678 |
page_addr = page_address(page); |
| 2679 |
memcpy(page_addr, CHOOSE_NVHE_SYM(__per_cpu_start), nvhe_percpu_size()); |
| 2680 |
kvm_nvhe_sym(kvm_arm_hyp_percpu_base)[cpu] = (unsigned long)page_addr; |
| 2681 |
} |
| 2682 |
|
| 2683 |
/* |
| 2684 |
* Map the Hyp-code called directly from the host |
| 2685 |
*/ |
| 2686 |
err = create_hyp_mappings(kvm_ksym_ref(__hyp_text_start), |
| 2687 |
kvm_ksym_ref(__hyp_text_end), PAGE_HYP_EXEC); |
| 2688 |
if (err) { |
| 2689 |
kvm_err("Cannot map world-switch code\n"); |
| 2690 |
goto out_err; |
| 2691 |
} |
| 2692 |
|
| 2693 |
err = create_hyp_mappings(kvm_ksym_ref(__hyp_data_start), |
| 2694 |
kvm_ksym_ref(__hyp_data_end), PAGE_HYP); |
| 2695 |
if (err) { |
| 2696 |
kvm_err("Cannot map .hyp.data section\n"); |
| 2697 |
goto out_err; |
| 2698 |
} |
| 2699 |
|
| 2700 |
err = create_hyp_mappings(kvm_ksym_ref(__hyp_rodata_start), |
| 2701 |
kvm_ksym_ref(__hyp_rodata_end), PAGE_HYP_RO); |
| 2702 |
if (err) { |
| 2703 |
kvm_err("Cannot map .hyp.rodata section\n"); |
| 2704 |
goto out_err; |
| 2705 |
} |
| 2706 |
|
| 2707 |
err = create_hyp_mappings(kvm_ksym_ref(__start_rodata), |
| 2708 |
kvm_ksym_ref(__end_rodata), PAGE_HYP_RO); |
| 2709 |
if (err) { |
| 2710 |
kvm_err("Cannot map rodata section\n"); |
| 2711 |
goto out_err; |
| 2712 |
} |
| 2713 |
|
| 2714 |
/* |
| 2715 |
* .hyp.bss is guaranteed to be placed at the beginning of the .bss |
| 2716 |
* section thanks to an assertion in the linker script. Map it RW and |
| 2717 |
* the rest of .bss RO. |
| 2718 |
*/ |
| 2719 |
err = create_hyp_mappings(kvm_ksym_ref(__hyp_bss_start), |
| 2720 |
kvm_ksym_ref(__hyp_bss_end), PAGE_HYP); |
| 2721 |
if (err) { |
| 2722 |
kvm_err("Cannot map hyp bss section: %d\n", err); |
| 2723 |
goto out_err; |
| 2724 |
} |
| 2725 |
|
| 2726 |
err = create_hyp_mappings(kvm_ksym_ref(__hyp_bss_end), |
| 2727 |
kvm_ksym_ref(__bss_stop), PAGE_HYP_RO); |
| 2728 |
if (err) { |
| 2729 |
kvm_err("Cannot map bss section\n"); |
| 2730 |
goto out_err; |
| 2731 |
} |
| 2732 |
|
| 2733 |
/* |
| 2734 |
* Map the Hyp stack pages |
| 2735 |
*/ |
| 2736 |
for_each_possible_cpu(cpu) { |
| 2737 |
struct kvm_nvhe_init_params *params = per_cpu_ptr_nvhe_sym(kvm_init_params, cpu); |
| 2738 |
char *stack_base = (char *)per_cpu(kvm_arm_hyp_stack_base, cpu); |
| 2739 |
|
| 2740 |
err = create_hyp_stack(__pa(stack_base), ¶ms->stack_hyp_va); |
| 2741 |
if (err) { |
| 2742 |
kvm_err("Cannot map hyp stack\n"); |
| 2743 |
goto out_err; |
| 2744 |
} |
| 2745 |
|
| 2746 |
/* |
| 2747 |
* Save the stack PA in nvhe_init_params. This will be needed |
| 2748 |
* to recreate the stack mapping in protected nVHE mode. |
| 2749 |
* __hyp_pa() won't do the right thing there, since the stack |
| 2750 |
* has been mapped in the flexible private VA space. |
| 2751 |
*/ |
| 2752 |
params->stack_pa = __pa(stack_base); |
| 2753 |
} |
| 2754 |
|
| 2755 |
for_each_possible_cpu(cpu) { |
| 2756 |
char *percpu_begin = (char *)kvm_nvhe_sym(kvm_arm_hyp_percpu_base)[cpu]; |
| 2757 |
char *percpu_end = percpu_begin + nvhe_percpu_size(); |
| 2758 |
|
| 2759 |
/* Map Hyp percpu pages */ |
| 2760 |
err = create_hyp_mappings(percpu_begin, percpu_end, PAGE_HYP); |
| 2761 |
if (err) { |
| 2762 |
kvm_err("Cannot map hyp percpu region\n"); |
| 2763 |
goto out_err; |
| 2764 |
} |
| 2765 |
|
| 2766 |
/* Prepare the CPU initialization parameters */ |
| 2767 |
cpu_prepare_hyp_mode(cpu, hyp_va_bits); |
| 2768 |
} |
| 2769 |
|
| 2770 |
kvm_hyp_init_symbols(); |
| 2771 |
|
| 2772 |
if (is_protected_kvm_enabled()) { |
| 2773 |
if (IS_ENABLED(CONFIG_ARM64_PTR_AUTH_KERNEL) && |
| 2774 |
cpus_have_final_cap(ARM64_HAS_ADDRESS_AUTH)) |
| 2775 |
pkvm_hyp_init_ptrauth(); |
| 2776 |
|
| 2777 |
init_cpu_logical_map(); |
| 2778 |
|
| 2779 |
if (!init_psci_relay()) { |
| 2780 |
err = -ENODEV; |
| 2781 |
goto out_err; |
| 2782 |
} |
| 2783 |
|
| 2784 |
err = init_pkvm_host_sve_state(); |
| 2785 |
if (err) |
| 2786 |
goto out_err; |
| 2787 |
|
| 2788 |
err = kvm_hyp_init_protection(hyp_va_bits); |
| 2789 |
if (err) { |
| 2790 |
kvm_err("Failed to init hyp memory protection\n"); |
| 2791 |
goto out_err; |
| 2792 |
} |
| 2793 |
} |
| 2794 |
|
| 2795 |
return 0; |
| 2796 |
|
| 2797 |
out_err: |
| 2798 |
teardown_hyp_mode(); |
| 2799 |
kvm_err("error initializing Hyp mode: %d\n", err); |
| 2800 |
return err; |
| 2801 |
} |
| 2802 |
|
| 2803 |
struct kvm_vcpu *kvm_mpidr_to_vcpu(struct kvm *kvm, unsigned long mpidr) |
| 2804 |
{ |
| 2805 |
struct kvm_vcpu *vcpu = NULL; |
| 2806 |
struct kvm_mpidr_data *data; |
| 2807 |
unsigned long i; |
| 2808 |
|
| 2809 |
mpidr &= MPIDR_HWID_BITMASK; |
| 2810 |
|
| 2811 |
rcu_read_lock(); |
| 2812 |
data = rcu_dereference(kvm->arch.mpidr_data); |
| 2813 |
|
| 2814 |
if (data) { |
| 2815 |
u16 idx = kvm_mpidr_index(data, mpidr); |
| 2816 |
|
| 2817 |
vcpu = kvm_get_vcpu(kvm, data->cmpidr_to_idx[idx]); |
| 2818 |
if (mpidr != kvm_vcpu_get_mpidr_aff(vcpu)) |
| 2819 |
vcpu = NULL; |
| 2820 |
} |
| 2821 |
|
| 2822 |
rcu_read_unlock(); |
| 2823 |
|
| 2824 |
if (vcpu) |
| 2825 |
return vcpu; |
| 2826 |
|
| 2827 |
kvm_for_each_vcpu(i, vcpu, kvm) { |
| 2828 |
if (mpidr == kvm_vcpu_get_mpidr_aff(vcpu)) |
| 2829 |
return vcpu; |
| 2830 |
} |
| 2831 |
return NULL; |
| 2832 |
} |
| 2833 |
|
| 2834 |
bool kvm_arch_irqchip_in_kernel(struct kvm *kvm) |
| 2835 |
{ |
| 2836 |
return irqchip_in_kernel(kvm); |
| 2837 |
} |
| 2838 |
|
| 2839 |
int kvm_arch_irq_bypass_add_producer(struct irq_bypass_consumer *cons, |
| 2840 |
struct irq_bypass_producer *prod) |
| 2841 |
{ |
| 2842 |
struct kvm_kernel_irqfd *irqfd = |
| 2843 |
container_of(cons, struct kvm_kernel_irqfd, consumer); |
| 2844 |
struct kvm_kernel_irq_routing_entry *irq_entry = &irqfd->irq_entry; |
| 2845 |
|
| 2846 |
/* |
| 2847 |
* The only thing we have a chance of directly-injecting is LPIs. Maybe |
| 2848 |
* one day... |
| 2849 |
*/ |
| 2850 |
if (irq_entry->type != KVM_IRQ_ROUTING_MSI) |
| 2851 |
return 0; |
| 2852 |
|
| 2853 |
return kvm_vgic_v4_set_forwarding(irqfd->kvm, prod->irq, |
| 2854 |
&irqfd->irq_entry); |
| 2855 |
} |
| 2856 |
|
| 2857 |
void kvm_arch_irq_bypass_del_producer(struct irq_bypass_consumer *cons, |
| 2858 |
struct irq_bypass_producer *prod) |
| 2859 |
{ |
| 2860 |
struct kvm_kernel_irqfd *irqfd = |
| 2861 |
container_of(cons, struct kvm_kernel_irqfd, consumer); |
| 2862 |
struct kvm_kernel_irq_routing_entry *irq_entry = &irqfd->irq_entry; |
| 2863 |
|
| 2864 |
if (irq_entry->type != KVM_IRQ_ROUTING_MSI) |
| 2865 |
return; |
| 2866 |
|
| 2867 |
kvm_vgic_v4_unset_forwarding(irqfd->kvm, prod->irq); |
| 2868 |
} |
| 2869 |
|
| 2870 |
void kvm_arch_update_irqfd_routing(struct kvm_kernel_irqfd *irqfd, |
| 2871 |
struct kvm_kernel_irq_routing_entry *old, |
| 2872 |
struct kvm_kernel_irq_routing_entry *new) |
| 2873 |
{ |
| 2874 |
if (old->type == KVM_IRQ_ROUTING_MSI && |
| 2875 |
new->type == KVM_IRQ_ROUTING_MSI && |
| 2876 |
!memcmp(&old->msi, &new->msi, sizeof(new->msi))) |
| 2877 |
return; |
| 2878 |
|
| 2879 |
/* |
| 2880 |
* Remapping the vLPI requires taking the its_lock mutex to resolve |
| 2881 |
* the new translation. We're in spinlock land at this point, so no |
| 2882 |
* chance of resolving the translation. |
| 2883 |
* |
| 2884 |
* Unmap the vLPI and fall back to software LPI injection. |
| 2885 |
*/ |
| 2886 |
return kvm_vgic_v4_unset_forwarding(irqfd->kvm, irqfd->producer->irq); |
| 2887 |
} |
| 2888 |
|
| 2889 |
void kvm_arch_irq_bypass_stop(struct irq_bypass_consumer *cons) |
| 2890 |
{ |
| 2891 |
struct kvm_kernel_irqfd *irqfd = |
| 2892 |
container_of(cons, struct kvm_kernel_irqfd, consumer); |
| 2893 |
|
| 2894 |
kvm_arm_halt_guest(irqfd->kvm); |
| 2895 |
} |
| 2896 |
|
| 2897 |
void kvm_arch_irq_bypass_start(struct irq_bypass_consumer *cons) |
| 2898 |
{ |
| 2899 |
struct kvm_kernel_irqfd *irqfd = |
| 2900 |
container_of(cons, struct kvm_kernel_irqfd, consumer); |
| 2901 |
|
| 2902 |
kvm_arm_resume_guest(irqfd->kvm); |
| 2903 |
} |
| 2904 |
|
| 2905 |
/* Initialize Hyp-mode and memory mappings on all CPUs */ |
| 2906 |
static __init int kvm_arm_init(void) |
| 2907 |
{ |
| 2908 |
int err; |
| 2909 |
bool in_hyp_mode; |
| 2910 |
|
| 2911 |
if (!is_hyp_mode_available()) { |
| 2912 |
kvm_info("HYP mode not available\n"); |
| 2913 |
return -ENODEV; |
| 2914 |
} |
| 2915 |
|
| 2916 |
if (kvm_get_mode() == KVM_MODE_NONE) { |
| 2917 |
kvm_info("KVM disabled from command line\n"); |
| 2918 |
return -ENODEV; |
| 2919 |
} |
| 2920 |
|
| 2921 |
err = kvm_sys_reg_table_init(); |
| 2922 |
if (err) { |
| 2923 |
kvm_info("Error initializing system register tables"); |
| 2924 |
return err; |
| 2925 |
} |
| 2926 |
|
| 2927 |
in_hyp_mode = is_kernel_in_hyp_mode(); |
| 2928 |
|
| 2929 |
if (cpus_have_final_cap(ARM64_WORKAROUND_DEVICE_LOAD_ACQUIRE) || |
| 2930 |
cpus_have_final_cap(ARM64_WORKAROUND_1508412)) |
| 2931 |
kvm_info("Guests without required CPU erratum workarounds can deadlock system!\n" \ |
| 2932 |
"Only trusted guests should be used on this system.\n"); |
| 2933 |
|
| 2934 |
err = kvm_set_ipa_limit(); |
| 2935 |
if (err) |
| 2936 |
return err; |
| 2937 |
|
| 2938 |
err = kvm_arm_init_sve(); |
| 2939 |
if (err) |
| 2940 |
return err; |
| 2941 |
|
| 2942 |
err = kvm_arm_vmid_alloc_init(); |
| 2943 |
if (err) { |
| 2944 |
kvm_err("Failed to initialize VMID allocator.\n"); |
| 2945 |
return err; |
| 2946 |
} |
| 2947 |
|
| 2948 |
if (!in_hyp_mode) { |
| 2949 |
err = init_hyp_mode(); |
| 2950 |
if (err) |
| 2951 |
goto out_err; |
| 2952 |
} |
| 2953 |
|
| 2954 |
err = kvm_init_vector_slots(); |
| 2955 |
if (err) { |
| 2956 |
kvm_err("Cannot initialise vector slots\n"); |
| 2957 |
goto out_hyp; |
| 2958 |
} |
| 2959 |
|
| 2960 |
err = init_subsystems(); |
| 2961 |
if (err) |
| 2962 |
goto out_hyp; |
| 2963 |
|
| 2964 |
kvm_info("%s%sVHE%s mode initialized successfully\n", |
| 2965 |
in_hyp_mode ? "" : (is_protected_kvm_enabled() ? |
| 2966 |
"Protected " : "Hyp "), |
| 2967 |
in_hyp_mode ? "" : (cpus_have_final_cap(ARM64_KVM_HVHE) ? |
| 2968 |
"h" : "n"), |
| 2969 |
cpus_have_final_cap(ARM64_HAS_NESTED_VIRT) ? "+NV2": ""); |
| 2970 |
|
| 2971 |
/* |
| 2972 |
* FIXME: Do something reasonable if kvm_init() fails after pKVM |
| 2973 |
* hypervisor protection is finalized. |
| 2974 |
*/ |
| 2975 |
err = kvm_init(sizeof(struct kvm_vcpu), 0, THIS_MODULE); |
| 2976 |
if (err) |
| 2977 |
goto out_subs; |
| 2978 |
|
| 2979 |
/* |
| 2980 |
* This should be called after initialization is done and failure isn't |
| 2981 |
* possible anymore. |
| 2982 |
*/ |
| 2983 |
if (!in_hyp_mode) |
| 2984 |
finalize_init_hyp_mode(); |
| 2985 |
|
| 2986 |
kvm_arm_initialised = true; |
| 2987 |
|
| 2988 |
return 0; |
| 2989 |
|
| 2990 |
out_subs: |
| 2991 |
teardown_subsystems(); |
| 2992 |
out_hyp: |
| 2993 |
if (!in_hyp_mode) |
| 2994 |
teardown_hyp_mode(); |
| 2995 |
out_err: |
| 2996 |
kvm_arm_vmid_alloc_free(); |
| 2997 |
return err; |
| 2998 |
} |
| 2999 |
|
| 3000 |
static int __init early_kvm_mode_cfg(char *arg) |
| 3001 |
{ |
| 3002 |
if (!arg) |
| 3003 |
return -EINVAL; |
| 3004 |
|
| 3005 |
if (strcmp(arg, "none") == 0) { |
| 3006 |
kvm_mode = KVM_MODE_NONE; |
| 3007 |
return 0; |
| 3008 |
} |
| 3009 |
|
| 3010 |
if (!is_hyp_mode_available()) { |
| 3011 |
pr_warn_once("KVM is not available. Ignoring kvm-arm.mode\n"); |
| 3012 |
return 0; |
| 3013 |
} |
| 3014 |
|
| 3015 |
if (strcmp(arg, "protected") == 0) { |
| 3016 |
if (!is_kernel_in_hyp_mode()) |
| 3017 |
kvm_mode = KVM_MODE_PROTECTED; |
| 3018 |
else |
| 3019 |
pr_warn_once("Protected KVM not available with VHE\n"); |
| 3020 |
|
| 3021 |
return 0; |
| 3022 |
} |
| 3023 |
|
| 3024 |
if (strcmp(arg, "nvhe") == 0 && !WARN_ON(is_kernel_in_hyp_mode())) { |
| 3025 |
kvm_mode = KVM_MODE_DEFAULT; |
| 3026 |
return 0; |
| 3027 |
} |
| 3028 |
|
| 3029 |
if (strcmp(arg, "nested") == 0 && !WARN_ON(!is_kernel_in_hyp_mode())) { |
| 3030 |
kvm_mode = KVM_MODE_NV; |
| 3031 |
return 0; |
| 3032 |
} |
| 3033 |
|
| 3034 |
return -EINVAL; |
| 3035 |
} |
| 3036 |
early_param("kvm-arm.mode", early_kvm_mode_cfg); |
| 3037 |
|
| 3038 |
static int __init early_kvm_wfx_trap_policy_cfg(char *arg, enum kvm_wfx_trap_policy *p) |
| 3039 |
{ |
| 3040 |
if (!arg) |
| 3041 |
return -EINVAL; |
| 3042 |
|
| 3043 |
if (strcmp(arg, "trap") == 0) { |
| 3044 |
*p = KVM_WFX_TRAP; |
| 3045 |
return 0; |
| 3046 |
} |
| 3047 |
|
| 3048 |
if (strcmp(arg, "notrap") == 0) { |
| 3049 |
*p = KVM_WFX_NOTRAP; |
| 3050 |
return 0; |
| 3051 |
} |
| 3052 |
|
| 3053 |
return -EINVAL; |
| 3054 |
} |
| 3055 |
|
| 3056 |
static int __init early_kvm_wfi_trap_policy_cfg(char *arg) |
| 3057 |
{ |
| 3058 |
return early_kvm_wfx_trap_policy_cfg(arg, &kvm_wfi_trap_policy); |
| 3059 |
} |
| 3060 |
early_param("kvm-arm.wfi_trap_policy", early_kvm_wfi_trap_policy_cfg); |
| 3061 |
|
| 3062 |
static int __init early_kvm_wfe_trap_policy_cfg(char *arg) |
| 3063 |
{ |
| 3064 |
return early_kvm_wfx_trap_policy_cfg(arg, &kvm_wfe_trap_policy); |
| 3065 |
} |
| 3066 |
early_param("kvm-arm.wfe_trap_policy", early_kvm_wfe_trap_policy_cfg); |
| 3067 |
|
| 3068 |
enum kvm_mode kvm_get_mode(void) |
| 3069 |
{ |
| 3070 |
return kvm_mode; |
| 3071 |
} |
| 3072 |
|
| 3073 |
module_init(kvm_arm_init); |