1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Copyright (C) 2020 - Google Inc 4 * Author: Andrew Scull <ascull@google.com> 5 */ 6 7 #include <hyp/adjust_pc.h> 8 9 #include <asm/pgtable-types.h> 10 #include <asm/kvm_asm.h> 11 #include <asm/kvm_emulate.h> 12 #include <asm/kvm_host.h> 13 #include <asm/kvm_hyp.h> 14 #include <asm/kvm_mmu.h> 15 16 #include <nvhe/ffa.h> 17 #include <nvhe/mem_protect.h> 18 #include <nvhe/mm.h> 19 #include <nvhe/pkvm.h> 20 #include <nvhe/trap_handler.h> 21 22 DEFINE_PER_CPU(struct kvm_nvhe_init_params, kvm_init_params); 23 24 void __kvm_hyp_host_forward_smc(struct kvm_cpu_context *host_ctxt); 25 26 static void flush_hyp_vcpu(struct pkvm_hyp_vcpu *hyp_vcpu) 27 { 28 struct kvm_vcpu *host_vcpu = hyp_vcpu->host_vcpu; 29 30 hyp_vcpu->vcpu.arch.ctxt = host_vcpu->arch.ctxt; 31 32 hyp_vcpu->vcpu.arch.sve_state = kern_hyp_va(host_vcpu->arch.sve_state); 33 hyp_vcpu->vcpu.arch.sve_max_vl = host_vcpu->arch.sve_max_vl; 34 35 hyp_vcpu->vcpu.arch.hw_mmu = host_vcpu->arch.hw_mmu; 36 37 hyp_vcpu->vcpu.arch.hcr_el2 = host_vcpu->arch.hcr_el2; 38 hyp_vcpu->vcpu.arch.mdcr_el2 = host_vcpu->arch.mdcr_el2; 39 hyp_vcpu->vcpu.arch.cptr_el2 = host_vcpu->arch.cptr_el2; 40 41 hyp_vcpu->vcpu.arch.iflags = host_vcpu->arch.iflags; 42 43 hyp_vcpu->vcpu.arch.debug_ptr = kern_hyp_va(host_vcpu->arch.debug_ptr); 44 45 hyp_vcpu->vcpu.arch.vsesr_el2 = host_vcpu->arch.vsesr_el2; 46 47 hyp_vcpu->vcpu.arch.vgic_cpu.vgic_v3 = host_vcpu->arch.vgic_cpu.vgic_v3; 48 } 49 50 static void sync_hyp_vcpu(struct pkvm_hyp_vcpu *hyp_vcpu) 51 { 52 struct kvm_vcpu *host_vcpu = hyp_vcpu->host_vcpu; 53 struct vgic_v3_cpu_if *hyp_cpu_if = &hyp_vcpu->vcpu.arch.vgic_cpu.vgic_v3; 54 struct vgic_v3_cpu_if *host_cpu_if = &host_vcpu->arch.vgic_cpu.vgic_v3; 55 unsigned int i; 56 57 host_vcpu->arch.ctxt = hyp_vcpu->vcpu.arch.ctxt; 58 59 host_vcpu->arch.hcr_el2 = hyp_vcpu->vcpu.arch.hcr_el2; 60 host_vcpu->arch.cptr_el2 = hyp_vcpu->vcpu.arch.cptr_el2; 61 62 host_vcpu->arch.fault = hyp_vcpu->vcpu.arch.fault; 63 64 host_vcpu->arch.iflags = hyp_vcpu->vcpu.arch.iflags; 65 66 host_cpu_if->vgic_hcr = hyp_cpu_if->vgic_hcr; 67 for (i = 0; i < hyp_cpu_if->used_lrs; ++i) 68 host_cpu_if->vgic_lr[i] = hyp_cpu_if->vgic_lr[i]; 69 } 70 71 static void handle___kvm_vcpu_run(struct kvm_cpu_context *host_ctxt) 72 { 73 DECLARE_REG(struct kvm_vcpu *, host_vcpu, host_ctxt, 1); 74 int ret; 75 76 host_vcpu = kern_hyp_va(host_vcpu); 77 78 if (unlikely(is_protected_kvm_enabled())) { 79 struct pkvm_hyp_vcpu *hyp_vcpu; 80 struct kvm *host_kvm; 81 82 host_kvm = kern_hyp_va(host_vcpu->kvm); 83 hyp_vcpu = pkvm_load_hyp_vcpu(host_kvm->arch.pkvm.handle, 84 host_vcpu->vcpu_idx); 85 if (!hyp_vcpu) { 86 ret = -EINVAL; 87 goto out; 88 } 89 90 flush_hyp_vcpu(hyp_vcpu); 91 92 ret = __kvm_vcpu_run(&hyp_vcpu->vcpu); 93 94 sync_hyp_vcpu(hyp_vcpu); 95 pkvm_put_hyp_vcpu(hyp_vcpu); 96 } else { 97 /* The host is fully trusted, run its vCPU directly. */ 98 ret = __kvm_vcpu_run(host_vcpu); 99 } 100 101 out: 102 cpu_reg(host_ctxt, 1) = ret; 103 } 104 105 static void handle___kvm_adjust_pc(struct kvm_cpu_context *host_ctxt) 106 { 107 DECLARE_REG(struct kvm_vcpu *, vcpu, host_ctxt, 1); 108 109 __kvm_adjust_pc(kern_hyp_va(vcpu)); 110 } 111 112 static void handle___kvm_flush_vm_context(struct kvm_cpu_context *host_ctxt) 113 { 114 __kvm_flush_vm_context(); 115 } 116 117 static void handle___kvm_tlb_flush_vmid_ipa(struct kvm_cpu_context *host_ctxt) 118 { 119 DECLARE_REG(struct kvm_s2_mmu *, mmu, host_ctxt, 1); 120 DECLARE_REG(phys_addr_t, ipa, host_ctxt, 2); 121 DECLARE_REG(int, level, host_ctxt, 3); 122 123 __kvm_tlb_flush_vmid_ipa(kern_hyp_va(mmu), ipa, level); 124 } 125 126 static void handle___kvm_tlb_flush_vmid_ipa_nsh(struct kvm_cpu_context *host_ctxt) 127 { 128 DECLARE_REG(struct kvm_s2_mmu *, mmu, host_ctxt, 1); 129 DECLARE_REG(phys_addr_t, ipa, host_ctxt, 2); 130 DECLARE_REG(int, level, host_ctxt, 3); 131 132 __kvm_tlb_flush_vmid_ipa_nsh(kern_hyp_va(mmu), ipa, level); 133 } 134 135 static void 136 handle___kvm_tlb_flush_vmid_range(struct kvm_cpu_context *host_ctxt) 137 { 138 DECLARE_REG(struct kvm_s2_mmu *, mmu, host_ctxt, 1); 139 DECLARE_REG(phys_addr_t, start, host_ctxt, 2); 140 DECLARE_REG(unsigned long, pages, host_ctxt, 3); 141 142 __kvm_tlb_flush_vmid_range(kern_hyp_va(mmu), start, pages); 143 } 144 145 static void handle___kvm_tlb_flush_vmid(struct kvm_cpu_context *host_ctxt) 146 { 147 DECLARE_REG(struct kvm_s2_mmu *, mmu, host_ctxt, 1); 148 149 __kvm_tlb_flush_vmid(kern_hyp_va(mmu)); 150 } 151 152 static void handle___kvm_flush_cpu_context(struct kvm_cpu_context *host_ctxt) 153 { 154 DECLARE_REG(struct kvm_s2_mmu *, mmu, host_ctxt, 1); 155 156 __kvm_flush_cpu_context(kern_hyp_va(mmu)); 157 } 158 159 static void handle___kvm_timer_set_cntvoff(struct kvm_cpu_context *host_ctxt) 160 { 161 __kvm_timer_set_cntvoff(cpu_reg(host_ctxt, 1)); 162 } 163 164 static void handle___kvm_enable_ssbs(struct kvm_cpu_context *host_ctxt) 165 { 166 u64 tmp; 167 168 tmp = read_sysreg_el2(SYS_SCTLR); 169 tmp |= SCTLR_ELx_DSSBS; 170 write_sysreg_el2(tmp, SYS_SCTLR); 171 } 172 173 static void handle___vgic_v3_get_gic_config(struct kvm_cpu_context *host_ctxt) 174 { 175 cpu_reg(host_ctxt, 1) = __vgic_v3_get_gic_config(); 176 } 177 178 static void handle___vgic_v3_init_lrs(struct kvm_cpu_context *host_ctxt) 179 { 180 __vgic_v3_init_lrs(); 181 } 182 183 static void handle___kvm_get_mdcr_el2(struct kvm_cpu_context *host_ctxt) 184 { 185 cpu_reg(host_ctxt, 1) = __kvm_get_mdcr_el2(); 186 } 187 188 static void handle___vgic_v3_save_vmcr_aprs(struct kvm_cpu_context *host_ctxt) 189 { 190 DECLARE_REG(struct vgic_v3_cpu_if *, cpu_if, host_ctxt, 1); 191 192 __vgic_v3_save_vmcr_aprs(kern_hyp_va(cpu_if)); 193 } 194 195 static void handle___vgic_v3_restore_vmcr_aprs(struct kvm_cpu_context *host_ctxt) 196 { 197 DECLARE_REG(struct vgic_v3_cpu_if *, cpu_if, host_ctxt, 1); 198 199 __vgic_v3_restore_vmcr_aprs(kern_hyp_va(cpu_if)); 200 } 201 202 static void handle___pkvm_init(struct kvm_cpu_context *host_ctxt) 203 { 204 DECLARE_REG(phys_addr_t, phys, host_ctxt, 1); 205 DECLARE_REG(unsigned long, size, host_ctxt, 2); 206 DECLARE_REG(unsigned long, nr_cpus, host_ctxt, 3); 207 DECLARE_REG(unsigned long *, per_cpu_base, host_ctxt, 4); 208 DECLARE_REG(u32, hyp_va_bits, host_ctxt, 5); 209 210 /* 211 * __pkvm_init() will return only if an error occurred, otherwise it 212 * will tail-call in __pkvm_init_finalise() which will have to deal 213 * with the host context directly. 214 */ 215 cpu_reg(host_ctxt, 1) = __pkvm_init(phys, size, nr_cpus, per_cpu_base, 216 hyp_va_bits); 217 } 218 219 static void handle___pkvm_cpu_set_vector(struct kvm_cpu_context *host_ctxt) 220 { 221 DECLARE_REG(enum arm64_hyp_spectre_vector, slot, host_ctxt, 1); 222 223 cpu_reg(host_ctxt, 1) = pkvm_cpu_set_vector(slot); 224 } 225 226 static void handle___pkvm_host_share_hyp(struct kvm_cpu_context *host_ctxt) 227 { 228 DECLARE_REG(u64, pfn, host_ctxt, 1); 229 230 cpu_reg(host_ctxt, 1) = __pkvm_host_share_hyp(pfn); 231 } 232 233 static void handle___pkvm_host_unshare_hyp(struct kvm_cpu_context *host_ctxt) 234 { 235 DECLARE_REG(u64, pfn, host_ctxt, 1); 236 237 cpu_reg(host_ctxt, 1) = __pkvm_host_unshare_hyp(pfn); 238 } 239 240 static void handle___pkvm_create_private_mapping(struct kvm_cpu_context *host_ctxt) 241 { 242 DECLARE_REG(phys_addr_t, phys, host_ctxt, 1); 243 DECLARE_REG(size_t, size, host_ctxt, 2); 244 DECLARE_REG(enum kvm_pgtable_prot, prot, host_ctxt, 3); 245 246 /* 247 * __pkvm_create_private_mapping() populates a pointer with the 248 * hypervisor start address of the allocation. 249 * 250 * However, handle___pkvm_create_private_mapping() hypercall crosses the 251 * EL1/EL2 boundary so the pointer would not be valid in this context. 252 * 253 * Instead pass the allocation address as the return value (or return 254 * ERR_PTR() on failure). 255 */ 256 unsigned long haddr; 257 int err = __pkvm_create_private_mapping(phys, size, prot, &haddr); 258 259 if (err) 260 haddr = (unsigned long)ERR_PTR(err); 261 262 cpu_reg(host_ctxt, 1) = haddr; 263 } 264 265 static void handle___pkvm_prot_finalize(struct kvm_cpu_context *host_ctxt) 266 { 267 cpu_reg(host_ctxt, 1) = __pkvm_prot_finalize(); 268 } 269 270 static void handle___pkvm_vcpu_init_traps(struct kvm_cpu_context *host_ctxt) 271 { 272 DECLARE_REG(struct kvm_vcpu *, vcpu, host_ctxt, 1); 273 274 __pkvm_vcpu_init_traps(kern_hyp_va(vcpu)); 275 } 276 277 static void handle___pkvm_init_vm(struct kvm_cpu_context *host_ctxt) 278 { 279 DECLARE_REG(struct kvm *, host_kvm, host_ctxt, 1); 280 DECLARE_REG(unsigned long, vm_hva, host_ctxt, 2); 281 DECLARE_REG(unsigned long, pgd_hva, host_ctxt, 3); 282 283 host_kvm = kern_hyp_va(host_kvm); 284 cpu_reg(host_ctxt, 1) = __pkvm_init_vm(host_kvm, vm_hva, pgd_hva); 285 } 286 287 static void handle___pkvm_init_vcpu(struct kvm_cpu_context *host_ctxt) 288 { 289 DECLARE_REG(pkvm_handle_t, handle, host_ctxt, 1); 290 DECLARE_REG(struct kvm_vcpu *, host_vcpu, host_ctxt, 2); 291 DECLARE_REG(unsigned long, vcpu_hva, host_ctxt, 3); 292 293 host_vcpu = kern_hyp_va(host_vcpu); 294 cpu_reg(host_ctxt, 1) = __pkvm_init_vcpu(handle, host_vcpu, vcpu_hva); 295 } 296 297 static void handle___pkvm_teardown_vm(struct kvm_cpu_context *host_ctxt) 298 { 299 DECLARE_REG(pkvm_handle_t, handle, host_ctxt, 1); 300 301 cpu_reg(host_ctxt, 1) = __pkvm_teardown_vm(handle); 302 } 303 304 typedef void (*hcall_t)(struct kvm_cpu_context *); 305 306 #define HANDLE_FUNC(x) [__KVM_HOST_SMCCC_FUNC_##x] = (hcall_t)handle_##x 307 308 static const hcall_t host_hcall[] = { 309 /* ___kvm_hyp_init */ 310 HANDLE_FUNC(__kvm_get_mdcr_el2), 311 HANDLE_FUNC(__pkvm_init), 312 HANDLE_FUNC(__pkvm_create_private_mapping), 313 HANDLE_FUNC(__pkvm_cpu_set_vector), 314 HANDLE_FUNC(__kvm_enable_ssbs), 315 HANDLE_FUNC(__vgic_v3_init_lrs), 316 HANDLE_FUNC(__vgic_v3_get_gic_config), 317 HANDLE_FUNC(__pkvm_prot_finalize), 318 319 HANDLE_FUNC(__pkvm_host_share_hyp), 320 HANDLE_FUNC(__pkvm_host_unshare_hyp), 321 HANDLE_FUNC(__kvm_adjust_pc), 322 HANDLE_FUNC(__kvm_vcpu_run), 323 HANDLE_FUNC(__kvm_flush_vm_context), 324 HANDLE_FUNC(__kvm_tlb_flush_vmid_ipa), 325 HANDLE_FUNC(__kvm_tlb_flush_vmid_ipa_nsh), 326 HANDLE_FUNC(__kvm_tlb_flush_vmid), 327 HANDLE_FUNC(__kvm_tlb_flush_vmid_range), 328 HANDLE_FUNC(__kvm_flush_cpu_context), 329 HANDLE_FUNC(__kvm_timer_set_cntvoff), 330 HANDLE_FUNC(__vgic_v3_save_vmcr_aprs), 331 HANDLE_FUNC(__vgic_v3_restore_vmcr_aprs), 332 HANDLE_FUNC(__pkvm_vcpu_init_traps), 333 HANDLE_FUNC(__pkvm_init_vm), 334 HANDLE_FUNC(__pkvm_init_vcpu), 335 HANDLE_FUNC(__pkvm_teardown_vm), 336 }; 337 338 static void handle_host_hcall(struct kvm_cpu_context *host_ctxt) 339 { 340 DECLARE_REG(unsigned long, id, host_ctxt, 0); 341 unsigned long hcall_min = 0; 342 hcall_t hfn; 343 344 /* 345 * If pKVM has been initialised then reject any calls to the 346 * early "privileged" hypercalls. Note that we cannot reject 347 * calls to __pkvm_prot_finalize for two reasons: (1) The static 348 * key used to determine initialisation must be toggled prior to 349 * finalisation and (2) finalisation is performed on a per-CPU 350 * basis. This is all fine, however, since __pkvm_prot_finalize 351 * returns -EPERM after the first call for a given CPU. 352 */ 353 if (static_branch_unlikely(&kvm_protected_mode_initialized)) 354 hcall_min = __KVM_HOST_SMCCC_FUNC___pkvm_prot_finalize; 355 356 id &= ~ARM_SMCCC_CALL_HINTS; 357 id -= KVM_HOST_SMCCC_ID(0); 358 359 if (unlikely(id < hcall_min || id >= ARRAY_SIZE(host_hcall))) 360 goto inval; 361 362 hfn = host_hcall[id]; 363 if (unlikely(!hfn)) 364 goto inval; 365 366 cpu_reg(host_ctxt, 0) = SMCCC_RET_SUCCESS; 367 hfn(host_ctxt); 368 369 return; 370 inval: 371 cpu_reg(host_ctxt, 0) = SMCCC_RET_NOT_SUPPORTED; 372 } 373 374 static void default_host_smc_handler(struct kvm_cpu_context *host_ctxt) 375 { 376 __kvm_hyp_host_forward_smc(host_ctxt); 377 } 378 379 static void handle_host_smc(struct kvm_cpu_context *host_ctxt) 380 { 381 DECLARE_REG(u64, func_id, host_ctxt, 0); 382 bool handled; 383 384 func_id &= ~ARM_SMCCC_CALL_HINTS; 385 386 handled = kvm_host_psci_handler(host_ctxt, func_id); 387 if (!handled) 388 handled = kvm_host_ffa_handler(host_ctxt, func_id); 389 if (!handled) 390 default_host_smc_handler(host_ctxt); 391 392 /* SMC was trapped, move ELR past the current PC. */ 393 kvm_skip_host_instr(); 394 } 395 396 void handle_trap(struct kvm_cpu_context *host_ctxt) 397 { 398 u64 esr = read_sysreg_el2(SYS_ESR); 399 400 switch (ESR_ELx_EC(esr)) { 401 case ESR_ELx_EC_HVC64: 402 handle_host_hcall(host_ctxt); 403 break; 404 case ESR_ELx_EC_SMC64: 405 handle_host_smc(host_ctxt); 406 break; 407 case ESR_ELx_EC_SVE: 408 if (has_hvhe()) 409 sysreg_clear_set(cpacr_el1, 0, (CPACR_EL1_ZEN_EL1EN | 410 CPACR_EL1_ZEN_EL0EN)); 411 else 412 sysreg_clear_set(cptr_el2, CPTR_EL2_TZ, 0); 413 isb(); 414 sve_cond_update_zcr_vq(ZCR_ELx_LEN_MASK, SYS_ZCR_EL2); 415 break; 416 case ESR_ELx_EC_IABT_LOW: 417 case ESR_ELx_EC_DABT_LOW: 418 handle_host_mem_abort(host_ctxt); 419 break; 420 default: 421 BUG(); 422 } 423 } 424