From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id BD18B2DA749 for ; Thu, 28 May 2026 04:39:19 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779943161; cv=none; b=OB0X4XQ8eQhM/qpuI5bUewIqgd148xjLj9+fsP+daibexPXXQflYb6FjPz0DKnR+EQgiHzObXyFOT+CCaSP38N97r+Mguv0IPgq/WhCeop2j1xkMrLnCRMUXfbkFXTHqExUCHmgznTmtD0bP3VU+WfGLv5HnuQj/mnJKbhd3b/0= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779943161; c=relaxed/simple; bh=WL8oPC0tQPNO9RBReVPyEOAMrt26615ac7FRki2rZ9c=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=Anj9+gfGxXHv5lk4VtDe++MdxI4/TUxICZIGl3g/5Fwvvp5zWlyiUjjoA+KnXBSiAOzezAq93EYd0Zmw692XMn8rWPaKdyBxKrvedh3NSu3Yoo0HGOtUoZZ+8bFW6shHRg427JeJ/SKwRTLDp498UaOnIw9oBNqkEnm1dTxrG4E= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=VJthGscX; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="VJthGscX" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1779943158; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=jXQOc51jw9CbTzkn1dp+Gluq7g3RzHfKmC/71EKuA3w=; b=VJthGscX0BE6S+AB0V3WzT/sLf+HL953a+4t+eujk5fQgg+x4UzQT2zHpe8qMf7xue94vr 55KhETWRbmrJBgdO6o3Ald0yVpb+yuo7U/ekQ5u7+rsaUYyKoM/kCoTElzNtU0Hmtry9mx rjo02SgcfXBI47MicoHtuSQvcfSylD0= Received: from mail-pj1-f72.google.com (mail-pj1-f72.google.com [209.85.216.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-526-Cc5Y_aQNMXuhDK-HHCTnuA-1; Thu, 28 May 2026 00:39:16 -0400 X-MC-Unique: Cc5Y_aQNMXuhDK-HHCTnuA-1 X-Mimecast-MFC-AGG-ID: Cc5Y_aQNMXuhDK-HHCTnuA_1779943155 Received: by mail-pj1-f72.google.com with SMTP id 98e67ed59e1d1-367cb6de61aso6959788a91.1 for ; Wed, 27 May 2026 21:39:16 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1779943155; x=1780547955; h=content-transfer-encoding:in-reply-to:from:content-language :references:cc:to:subject:user-agent:mime-version:date:message-id :x-gm-gg:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=jXQOc51jw9CbTzkn1dp+Gluq7g3RzHfKmC/71EKuA3w=; b=oMDX8fbAYHyy+fmBIBLiLipYji6Sfy+kAkWY/yvFui5vgmZYRWYTvNVYsFws3VH/cX ZN32IVxCLMwBcCpXrF7TnEbdawq9xp58Iaroh0SPByQmZPZsp3Gghy+XAwK0aB0kCy5d f3u4j3+viCB0Rgi7mqc+ln4WYJfzmT6wvQOyHxUCtfMwKofLiN29hPTBf335hUrJSBNT dxRea/R19D+KuEiAPWykFcc8P8bToz3P4tYDsnoS5lw2ouMfvsqUQm6aePmDfgCGpY5u CYBgbrWkFrwXslucJdwS1qaM/hSb3TOiu25EuPUbk6hEB+hw22yIgEswLFFAzRGeFTx/ zE0w== X-Forwarded-Encrypted: i=1; AFNElJ9/1xSawfOrpTKCeYWzdis6P9E9QzgzFYGqLhBxZQqpH5moGa44vBK46T+Zpl7dZf2HVAg85Z1WpnDS@lists.linux.dev X-Gm-Message-State: AOJu0Yyx33BAfQbP80rz+Fthl7IqJvcI28KiSKY3wzN4ND2J7SbgEkGb Xkwp014sm3fGbvhs9Ee1iESpsDImd1Yy80g7TQjMd7lM7IxHqa5eWswSBavQG0Ltl6EO7zi1/SZ fNU3+Y2y4ptjSQs6Sx3ZbrWx/HkcwxqQ8ZAaWJdX2Sw9m2k1W7zOOsQYzYmk861k= X-Gm-Gg: Acq92OHF3FexyzyGJlBaLdmXRZwdtYWq4c14mySQa1AFca4sW3l1BNLONJ06QFbAKAs HI6HZ0fMp0elqI6Uv7i593TvqWwY0mb+WvusvOMkhISfBScRc5sSMgMHaW+81DdQ6wTtY8Or+dN XwwF6RBQMpFmmwdzxQRhsSFkR5ef3aPVMcceMAJrca4agnwUe2Jm9EVBVMx4wZmR7fIxHRVhpJw gKWqsfovxclbd3zIhxMOqO0736LIBazZr8MSpoVFoF00CZADFSQMBTUds4BKnVrwFYLEHGB4PHc bjt4Jn3mznl0xzuGSN+hXckT+SxoNzvt3MXIE/NY05HtMivJdCgsM/kb1+h2VHqihvz0bfCyd4y OyYmqUemfkyhGSgSlPaG4RlA4DpQOfpxp+S1YoPyzC3S9REsCzV4/6L4CXTvQzOfwDF7P7koG75 o= X-Received: by 2002:a17:90b:2e8c:b0:368:a297:bd38 with SMTP id 98e67ed59e1d1-36a67704b2cmr24847545a91.7.1779943154538; Wed, 27 May 2026 21:39:14 -0700 (PDT) X-Received: by 2002:a17:90b:2e8c:b0:368:a297:bd38 with SMTP id 98e67ed59e1d1-36a67704b2cmr24847511a91.7.1779943153881; Wed, 27 May 2026 21:39:13 -0700 (PDT) Received: from [192.168.68.51] (n175-34-8-244.mrk21.qld.optusnet.com.au. [175.34.8.244]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-36b90d23ef2sm463249a91.17.2026.05.27.21.39.01 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 27 May 2026 21:39:13 -0700 (PDT) Message-ID: Date: Thu, 28 May 2026 14:38:59 +1000 Precedence: bulk X-Mailing-List: linux-coco@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH v14 22/44] arm64: RMI: Handle realm enter/exit To: Steven Price , kvm@vger.kernel.org, kvmarm@lists.linux.dev Cc: Catalin Marinas , Marc Zyngier , Will Deacon , James Morse , Oliver Upton , Suzuki K Poulose , Zenghui Yu , linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, Joey Gouly , Alexandru Elisei , Christoffer Dall , Fuad Tabba , linux-coco@lists.linux.dev, Ganapatrao Kulkarni , Shanker Donthineni , Alper Gun , "Aneesh Kumar K . V" , Emi Kisanuki , Vishal Annapurve , WeiLin.Chang@arm.com, Lorenzo.Pieralisi2@arm.com References: <20260513131757.116630-1-steven.price@arm.com> <20260513131757.116630-23-steven.price@arm.com> From: Gavin Shan In-Reply-To: <20260513131757.116630-23-steven.price@arm.com> X-Mimecast-Spam-Score: 0 X-Mimecast-MFC-PROC-ID: CteTyB_JlZTggK48AhwLJHlQAOSiT22INHj9WiMKFoo_1779943155 X-Mimecast-Originator: redhat.com Content-Language: en-US Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Hi Steve, On 5/13/26 11:17 PM, Steven Price wrote: > Entering a realm is done using a SMC call to the RMM. On exit the > exit-codes need to be handled slightly differently to the normal KVM > path so define our own functions for realm enter/exit and hook them > in if the guest is a realm guest. > > Signed-off-by: Steven Price > Reviewed-by: Gavin Shan > --- > Chanegs since v13: > * The RMM is now required to provide an ESR value with the correct > information to emulate MMIO, so we no longer need to hardcode 0s in > rec_exit_sys_reg(). > * The PSCI changes mean that there is a potential race when turning on > a VCPU which can cause a RMI_ERROR_REC return. Exit to user space > with -EAGAIN in this case. > Changes since v12: > * Call guest_state_{enter,exit}_irqoff() around rmi_rec_enter(). > * Add handling of the IRQ exception case where IRQs need to be briefly > enabled before exiting guest timing. > Changes since v8: > * Introduce kvm_rec_pre_enter() called before entering an atomic > section to handle operations that might require memory allocation > (specifically completing a RIPAS change introduced in a later patch). > * Updates to align with upstream changes to hpfar_el2 which now (ab)uses > HPFAR_EL2_NS as a valid flag. > * Fix exit reason when racing with PSCI shutdown to return > KVM_EXIT_SHUTDOWN rather than KVM_EXIT_UNKNOWN. > Changes since v7: > * A return of 0 from kvm_handle_sys_reg() doesn't mean the register has > been read (although that can never happen in the current code). Tidy > up the condition to handle any future refactoring. > Changes since v6: > * Use vcpu_err() rather than pr_err/kvm_err when there is an associated > vcpu to the error. > * Return -EFAULT for KVM_EXIT_MEMORY_FAULT as per the documentation for > this exit type. > * Split code handling a RIPAS change triggered by the guest to the > following patch. > Changes since v5: > * For a RIPAS_CHANGE request from the guest perform the actual RIPAS > change on next entry rather than immediately on the exit. This allows > the VMM to 'reject' a RIPAS change by refusing to continue > scheduling. > Changes since v4: > * Rename handle_rme_exit() to handle_rec_exit() > * Move the loop to copy registers into the REC enter structure from the > to rec_exit_handlers callbacks to kvm_rec_enter(). This fixes a bug > where the handler exits to user space and user space wants to modify > the GPRS. > * Some code rearrangement in rec_exit_ripas_change(). > Changes since v2: > * realm_set_ipa_state() now provides an output parameter for the > top_iap that was changed. Use this to signal the VMM with the correct > range that has been transitioned. > * Adapt to previous patch changes. > --- > arch/arm64/include/asm/kvm_rmi.h | 4 + > arch/arm64/kvm/Makefile | 2 +- > arch/arm64/kvm/arm.c | 26 ++++- > arch/arm64/kvm/rmi-exit.c | 186 +++++++++++++++++++++++++++++++ > arch/arm64/kvm/rmi.c | 42 +++++++ > 5 files changed, 254 insertions(+), 6 deletions(-) > create mode 100644 arch/arm64/kvm/rmi-exit.c > > diff --git a/arch/arm64/include/asm/kvm_rmi.h b/arch/arm64/include/asm/kvm_rmi.h > index d99bf4fc3c39..feb534a6678e 100644 > --- a/arch/arm64/include/asm/kvm_rmi.h > +++ b/arch/arm64/include/asm/kvm_rmi.h > @@ -84,6 +84,10 @@ void kvm_destroy_realm(struct kvm *kvm); > void kvm_realm_destroy_rtts(struct kvm *kvm); > void kvm_destroy_rec(struct kvm_vcpu *vcpu); > > +int kvm_rec_enter(struct kvm_vcpu *vcpu); > +int kvm_rec_pre_enter(struct kvm_vcpu *vcpu); > +int handle_rec_exit(struct kvm_vcpu *vcpu, int rec_run_status); > + > static inline bool kvm_realm_is_private_address(struct realm *realm, > unsigned long addr) > { > diff --git a/arch/arm64/kvm/Makefile b/arch/arm64/kvm/Makefile > index ed3cf30eb06e..4a2d52fdb6a2 100644 > --- a/arch/arm64/kvm/Makefile > +++ b/arch/arm64/kvm/Makefile > @@ -16,7 +16,7 @@ CFLAGS_handle_exit.o += -Wno-override-init > kvm-y += arm.o mmu.o mmio.o psci.o hypercalls.o pvtime.o \ > inject_fault.o va_layout.o handle_exit.o config.o \ > guest.o debug.o reset.o sys_regs.o stacktrace.o \ > - vgic-sys-reg-v3.o fpsimd.o pkvm.o rmi.o \ > + vgic-sys-reg-v3.o fpsimd.o pkvm.o rmi.o rmi-exit.o \ > arch_timer.o trng.o vmid.o emulate-nested.o nested.o at.o \ > vgic/vgic.o vgic/vgic-init.o \ > vgic/vgic-irqfd.o vgic/vgic-v2.o \ > diff --git a/arch/arm64/kvm/arm.c b/arch/arm64/kvm/arm.c > index 21d9dfdb1ea0..ed88a203b892 100644 > --- a/arch/arm64/kvm/arm.c > +++ b/arch/arm64/kvm/arm.c > @@ -1331,6 +1331,9 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu) > if (ret > 0) > ret = check_vcpu_requests(vcpu); > > + if (ret > 0 && vcpu_is_rec(vcpu)) > + ret = kvm_rec_pre_enter(vcpu); > + > /* > * Preparing the interrupts to be injected also > * involves poking the GIC, which must be done in a > @@ -1378,7 +1381,10 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu) > trace_kvm_entry(*vcpu_pc(vcpu)); > guest_timing_enter_irqoff(); > > - ret = kvm_arm_vcpu_enter_exit(vcpu); > + if (vcpu_is_rec(vcpu)) > + ret = kvm_rec_enter(vcpu); > + else > + ret = kvm_arm_vcpu_enter_exit(vcpu); > > vcpu->mode = OUTSIDE_GUEST_MODE; > vcpu->stat.exits++; > @@ -1424,7 +1430,9 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu) > * context synchronization event) is necessary to ensure that > * pending interrupts are taken. > */ > - if (ARM_EXCEPTION_CODE(ret) == ARM_EXCEPTION_IRQ) { > + if (ARM_EXCEPTION_CODE(ret) == ARM_EXCEPTION_IRQ || > + (vcpu_is_rec(vcpu) && > + vcpu->arch.rec.run->exit.exit_reason == RMI_EXIT_IRQ)) { > local_irq_enable(); > isb(); > local_irq_disable(); The condition could be posssibly imprecise because ARM_EXCEPTION_CODE(ret) can be ARM_EXCEPTION_IRQ even for a REC. So the precise condition would be: if ((!vcpu_is_rec(vcpu) && ARM_EXCEPTION_CODE(ret) == ARM_EXCEPTION_IRQ) || (vcpu_is_rec(vcpu) && vcpu->arch.rec.run->exit.exit_reason == RMI_EXIT_IRQ)) { > @@ -1436,8 +1444,13 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu) > > trace_kvm_exit(ret, kvm_vcpu_trap_get_class(vcpu), *vcpu_pc(vcpu)); > > - /* Exit types that need handling before we can be preempted */ > - handle_exit_early(vcpu, ret); > + if (!vcpu_is_rec(vcpu)) { > + /* > + * Exit types that need handling before we can be > + * preempted > + */ > + handle_exit_early(vcpu, ret); > + } > > kvm_nested_sync_hwstate(vcpu); > > @@ -1462,7 +1475,10 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu) > ret = ARM_EXCEPTION_IL; > } > > - ret = handle_exit(vcpu, ret); > + if (vcpu_is_rec(vcpu)) > + ret = handle_rec_exit(vcpu, ret); > + else > + ret = handle_exit(vcpu, ret); > } > > /* Tell userspace about in-kernel device output levels */ > diff --git a/arch/arm64/kvm/rmi-exit.c b/arch/arm64/kvm/rmi-exit.c > new file mode 100644 > index 000000000000..e7c51b6cf6ce > --- /dev/null > +++ b/arch/arm64/kvm/rmi-exit.c > @@ -0,0 +1,186 @@ > +// SPDX-License-Identifier: GPL-2.0-only > +/* > + * Copyright (C) 2023 ARM Ltd. > + */ > + > +#include > +#include > +#include > + > +#include > +#include > +#include > +#include > + > +typedef int (*exit_handler_fn)(struct kvm_vcpu *vcpu); > + > +static int rec_exit_reason_notimpl(struct kvm_vcpu *vcpu) > +{ > + struct realm_rec *rec = &vcpu->arch.rec; > + > + vcpu_err(vcpu, "Unhandled exit reason from realm (ESR: %#llx)\n", > + rec->run->exit.esr); > + return -ENXIO; > +} > + s/rec->run->exit.esr/kvm_vcpu_get_esr(vcpu), rec->run->exit.esr has been copied to the storage space pointed by kvm_vcpu_get_esr() in its caller. > +static int rec_exit_sync_dabt(struct kvm_vcpu *vcpu) > +{ > + return kvm_handle_guest_abort(vcpu); > +} > + > +static int rec_exit_sync_iabt(struct kvm_vcpu *vcpu) > +{ > + struct realm_rec *rec = &vcpu->arch.rec; > + > + vcpu_err(vcpu, "Unhandled instruction abort (ESR: %#llx).\n", > + rec->run->exit.esr); > + return -ENXIO; > +} > + s/rec->run->exit.esr/kvm_vcpu_get_esr(vcpu) > +static int rec_exit_sys_reg(struct kvm_vcpu *vcpu) > +{ > + struct realm_rec *rec = &vcpu->arch.rec; > + unsigned long esr = kvm_vcpu_get_esr(vcpu); > + int rt = kvm_vcpu_sys_get_rt(vcpu); > + bool is_write = (esr & ESR_ELx_SYS64_ISS_DIR_MASK) == ESR_ELx_SYS64_ISS_DIR_WRITE; > + int ret; > + > + if (is_write) > + vcpu_set_reg(vcpu, rt, rec->run->exit.gprs[rt]); > + > + ret = kvm_handle_sys_reg(vcpu); > + if (!is_write) > + rec->run->enter.gprs[rt] = vcpu_get_reg(vcpu, rt); > + > + return ret; > +} > + > +static exit_handler_fn rec_exit_handlers[] = { > + [0 ... ESR_ELx_EC_MAX] = rec_exit_reason_notimpl, > + [ESR_ELx_EC_SYS64] = rec_exit_sys_reg, > + [ESR_ELx_EC_DABT_LOW] = rec_exit_sync_dabt, > + [ESR_ELx_EC_IABT_LOW] = rec_exit_sync_iabt > +}; > + > +static int rec_exit_psci(struct kvm_vcpu *vcpu) > +{ > + struct realm_rec *rec = &vcpu->arch.rec; > + int i; > + > + for (i = 0; i < REC_RUN_GPRS; i++) > + vcpu_set_reg(vcpu, i, rec->run->exit.gprs[i]); > + > + return kvm_smccc_call_handler(vcpu); > +} > + > +static int rec_exit_ripas_change(struct kvm_vcpu *vcpu) > +{ > + struct kvm *kvm = vcpu->kvm; > + struct realm *realm = &kvm->arch.realm; > + struct realm_rec *rec = &vcpu->arch.rec; > + unsigned long base = rec->run->exit.ripas_base; > + unsigned long top = rec->run->exit.ripas_top; > + unsigned long ripas = rec->run->exit.ripas_value; > + > + if (!kvm_realm_is_private_address(realm, base) || > + !kvm_realm_is_private_address(realm, top - 1)) { > + vcpu_err(vcpu, "Invalid RIPAS_CHANGE for %#lx - %#lx, ripas: %#lx\n", > + base, top, ripas); > + /* Set RMI_REJECT bit */ > + rec->run->enter.flags = REC_ENTER_FLAG_RIPAS_RESPONSE; > + return -EINVAL; > + } I doubt if the flag (REC_ENTER_FLAG_RIPAS_RESPONSE) will be handed over to RMM since the negative return value forces we're exiting to VMM like QEMU where how this problematic case can be handled is TBD. > + > + /* Exit to VMM, the actual RIPAS change is done on next entry */ > + kvm_prepare_memory_fault_exit(vcpu, base, top - base, false, false, > + ripas == RMI_RAM); > + > + /* > + * KVM_EXIT_MEMORY_FAULT requires an return code of -EFAULT, see the > + * API documentation > + */ > + return -EFAULT; > +} > + > +static void update_arch_timer_irq_lines(struct kvm_vcpu *vcpu) > +{ > + struct realm_rec *rec = &vcpu->arch.rec; > + > + __vcpu_assign_sys_reg(vcpu, CNTV_CTL_EL0, rec->run->exit.cntv_ctl); > + __vcpu_assign_sys_reg(vcpu, CNTV_CVAL_EL0, rec->run->exit.cntv_cval); > + __vcpu_assign_sys_reg(vcpu, CNTP_CTL_EL0, rec->run->exit.cntp_ctl); > + __vcpu_assign_sys_reg(vcpu, CNTP_CVAL_EL0, rec->run->exit.cntp_cval); > + > + kvm_realm_timers_update(vcpu); > +} > + > +/* > + * Return > 0 to return to guest, < 0 on error, 0 (and set exit_reason) on > + * proper exit to userspace. > + */ > +int handle_rec_exit(struct kvm_vcpu *vcpu, int rec_run_ret) > +{ > + struct realm_rec *rec = &vcpu->arch.rec; > + u8 esr_ec = ESR_ELx_EC(rec->run->exit.esr); > + unsigned long status, index; > + > + status = RMI_RETURN_STATUS(rec_run_ret); > + index = RMI_RETURN_INDEX(rec_run_ret); > + > + /* > + * If a PSCI_SYSTEM_OFF request raced with a vcpu executing, we might > + * see the following status code and index indicating an attempt to run > + * a REC when the RD state is SYSTEM_OFF. In this case, we just need to > + * return to user space which can deal with the system event or will try > + * to run the KVM VCPU again, at which point we will no longer attempt > + * to enter the Realm because we will have a sleep request pending on > + * the VCPU as a result of KVM's PSCI handling. > + */ > + if (status == RMI_ERROR_REALM) { > + vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN; > + return 0; > + } > + > + /* > + * If a VCPU has been turned on, but the REC state hasn't been updated > + * we may experience RMI_ERROR_REC. Exit to the userspace with -EAGAIN > + * for a retry. > + */ > + if (status == RMI_ERROR_REC) > + return -EAGAIN; > + if (rec_run_ret) > + return -ENXIO; > + > + vcpu->arch.fault.esr_el2 = rec->run->exit.esr; > + vcpu->arch.fault.far_el2 = rec->run->exit.far; > + /* HPFAR_EL2 is only valid for RMI_EXIT_SYNC */ > + vcpu->arch.fault.hpfar_el2 = 0; > + > + update_arch_timer_irq_lines(vcpu); > + > + /* Reset the emulation flags for the next run of the REC */ > + rec->run->enter.flags = 0; > + > + switch (rec->run->exit.exit_reason) { > + case RMI_EXIT_SYNC: > + /* > + * HPFAR_EL2_NS is hijacked to indicate a valid HPFAR value, > + * see __get_fault_info() > + */ > + vcpu->arch.fault.hpfar_el2 = rec->run->exit.hpfar | HPFAR_EL2_NS; > + return rec_exit_handlers[esr_ec](vcpu); > + case RMI_EXIT_IRQ: > + case RMI_EXIT_FIQ: > + case RMI_EXIT_SERROR: > + return 1; > + case RMI_EXIT_PSCI: > + return rec_exit_psci(vcpu); > + case RMI_EXIT_RIPAS_CHANGE: > + return rec_exit_ripas_change(vcpu); > + } > + > + kvm_pr_unimpl("Unsupported exit reason: %u\n", > + rec->run->exit.exit_reason); > + vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR; > + return 0; > +} > diff --git a/arch/arm64/kvm/rmi.c b/arch/arm64/kvm/rmi.c > index 353a5ca45e78..d8a5fb12db2d 100644 > --- a/arch/arm64/kvm/rmi.c > +++ b/arch/arm64/kvm/rmi.c > @@ -173,6 +173,48 @@ static int realm_ensure_created(struct kvm *kvm) > return -ENXIO; > } > > +/* > + * kvm_rec_pre_enter - Complete operations before entering a REC > + * > + * Some operations require work to be completed before entering a realm. That > + * work may require memory allocation so cannot be done in the kvm_rec_enter() > + * call. > + * > + * Return: 1 if we should enter the guest > + * 0 if we should exit to userspace > + * < 0 if we should exit to userspace, where the return value indicates > + * an error > + */ > +int kvm_rec_pre_enter(struct kvm_vcpu *vcpu) > +{ > + struct realm_rec *rec = &vcpu->arch.rec; > + > + if (kvm_realm_state(vcpu->kvm) != REALM_STATE_ACTIVE) > + return -EINVAL; > + > + switch (rec->run->exit.exit_reason) { > + case RMI_EXIT_HOST_CALL: > + for (int i = 0; i < REC_RUN_GPRS; i++) > + rec->run->enter.gprs[i] = vcpu_get_reg(vcpu, i); > + break; > + } > + > + return 1; > +} > + > +int noinstr kvm_rec_enter(struct kvm_vcpu *vcpu) > +{ > + struct realm_rec *rec = &vcpu->arch.rec; > + int ret; > + > + guest_state_enter_irqoff(); > + ret = rmi_rec_enter(virt_to_phys(rec->rec_page), > + virt_to_phys(rec->run)); > + guest_state_exit_irqoff(); > + > + return ret; > +} > + > static int kvm_create_rec(struct kvm_vcpu *vcpu) > { > struct user_pt_regs *vcpu_regs = vcpu_gp_regs(vcpu); Thanks, Gavin