From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-wm1-f71.google.com (mail-wm1-f71.google.com [209.85.128.71]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 89A264746B7 for ; Fri, 7 Aug 2026 16:43:44 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.71 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786121026; cv=none; b=BAXnQCS0bq7wGt9j+Xs2k4sLW5G7MSRDRLNt3WbHbz5f7FMs8yyB1Xeaaak5yYAVMMrE6ALDCbOtZsY23u8VOfIABwGlRk0m8P92C6dWej5nBYcrEiufbP0S5QJvwcAVx5XyWVEpvjZbrSDw+Ob/HohmOJw0uOlWj2WW59IxYKw= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786121026; c=relaxed/simple; bh=/+oBPRPor12nAw5j501NP9Ed7vddxDnJfcm2D2u6JbQ=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=GpfxZpavWSr7nrcHdxpOfSS2naGlpdSOIjUtXiyO7FSU5xpebWUXv+QFH7pJnHxz+HVQbo/0pR3yZlL3HyDkGJOyrQkP1/eJIfxweNcwO1Vf0xSGWn8pv82+osYlDaX66tZl18ZyL4g1M528kMFBzA1VoGNcyod7/pkNY5xckxs= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--sebastianene.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=wZpKwYCN; arc=none smtp.client-ip=209.85.128.71 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--sebastianene.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="wZpKwYCN" Received: by mail-wm1-f71.google.com with SMTP id 5b1f17b1804b1-4955fd77c18so20719205e9.2 for ; Fri, 07 Aug 2026 09:43:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1786121023; x=1786725823; darn=vger.kernel.org; h=content-transfer-encoding:content-type:cc:to:from:subject :message-id:references:mime-version:in-reply-to:date:from:to:cc :subject:date:message-id:reply-to:content-type; bh=DRnRzIglH4bre6fymvnhTQIWpGXgBbzyHJfeoNchwKg=; b=wZpKwYCNSPjspAKsdx43bgj3qQsKB4uHevb6bGYRv7P6S8lzb1l7rhVSwzpTngVIb3 RtvMqoWvZKi4CzJGYANZzmyvTUiX6yY68/e1475xl8olcnGqOxu2g8+CWW9vo6/pfcd0 sJUt6ut97pdULwJ5GIR89poDIJbdcdRIfeWv4bkkv/1Z+/dq2xRUmrazP2fhp1arcjQH ro/URIxyJTl6D0XC8nFlbMLD2i2zOtyu0aCx9z0MkMNqwarYFwmb0aNlanqy/w1wpnG4 JJzZJt5tYTSr/QmDuP8m7i+Gu+oOwszZqE10y2fLjT3xf5V9CTBqPgGxbwnKXDwCGPcw 3BBw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1786121023; x=1786725823; h=content-transfer-encoding:content-type:cc:to:from:subject :message-id:references:mime-version:in-reply-to:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to :content-type; bh=DRnRzIglH4bre6fymvnhTQIWpGXgBbzyHJfeoNchwKg=; b=dbat1CtaAT5rzEWmlaKxBeAoc4K8EOSFfd0rJPA0E673e0bznHMNLJla4uuQbNcdrt loU6Wlhw7kxoHG/PlIeZq6HaVGDi8KhlLLMurj9wC7eWxSybdteP5cXR0PldMVvayPYZ X7nLaCLvNlpafD4gyrxIMyUlwG9FX7sH2qXzvzenCeCtZIqa8+PwRJJyiwIn2MdAYSEJ ZT5ORfPbbq5x1baJkolySsmm8nYANn/u1n8L8kVAnsIvz9X6KEc4qb1hARGLrK3UoFDm 7PRiovxnCtd4t92sKMKxq84sIRAIJJ6a+N8IokgRO4k90E71rAbbAYxAswe2u6NGUFq3 Vy2A== X-Forwarded-Encrypted: i=1; AHgh+RoQo0UrT+zpzSk08vcjIKQF3F/DLm4L2tXOTVG82ioYvgzyTEbNUBeB6yIH4x2AZuqlI+GRW2lqc5DC9Xo=@vger.kernel.org X-Gm-Message-State: AOJu0YwX74BZPSD7GQNlPyjsREJY/EQn4NnrQn2zXrNlA/H5b7iLi2Mr MrD0+c+1F/0LntfXnhE7KoHAj1LgSOQxLT5BgryhF2dz1z+n9/mquQUIje30hPUY4Qu5O9zPaoI R3OQRicrsj/uZnrocb1SPXrcVOX/cog== X-Received: from wmbhu19.prod.google.com ([2002:a05:600c:a293:b0:493:f83f:e304]) (user=sebastianene job=prod-delivery.src-stubby-dispatcher) by 2002:a05:600c:c297:b0:494:596e:e8c4 with SMTP id 5b1f17b1804b1-4994e7d3395mr238197545e9.17.1786121022494; Fri, 07 Aug 2026 09:43:42 -0700 (PDT) Date: Fri, 7 Aug 2026 16:43:13 +0000 In-Reply-To: <20260807164322.2970811-2-sebastianene@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260807164322.2970811-2-sebastianene@google.com> X-Mailer: git-send-email 2.55.0.654.g21b8a5bc05-goog Message-ID: <20260807164322.2970811-5-sebastianene@google.com> Subject: [PATCH v2 03/13] KVM: arm64: Support host MMIO trap handlers for unmapped devices From: Sebastian Ene To: catalin.marinas@arm.com, fuad.tabba@linux.dev, joey.gouly@arm.com, mark.rutland@arm.com, maz@kernel.org, oupton@kernel.org, rananta@google.com, Sascha.Bischoff@arm.com, suzuki.poulose@arm.com, will@kernel.org Cc: kvmarm@lists.linux.dev, android-kvm@google.com, bgrzesik@google.com, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, nathan@kernel.org, perlarsen@google.com, sebastianene@google.com, seiden@linux.ibm.com, smostafa@google.com, tglx@kernel.org, vdonnefort@google.com, vladimir.murzin@arm.com, yuzenghui@huawei.com, zenghui.yu@linux.dev Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Hook a handler to the host mem abort so that the hypervisor can intercept host accesses to unmapped memory regions. When a Stage-2 fault occurs on a registered device region, the hypervisor will look if there is any registered function that can handle the access. On the back of this, mediate host accesses to devices and emulate them in pKVM. Signed-off-by: Sebastian Ene Signed-off-by: Bart=C5=82omiej Grzesik --- arch/arm64/include/asm/kvm_arm.h | 2 ++ arch/arm64/include/asm/kvm_pkvm.h | 4 +++ arch/arm64/kvm/hyp/nvhe/mem_protect.c | 49 +++++++++++++++++++++++++++ arch/arm64/kvm/hyp/nvhe/setup.c | 3 ++ 4 files changed, 58 insertions(+) diff --git a/arch/arm64/include/asm/kvm_arm.h b/arch/arm64/include/asm/kvm_= arm.h index 3f9233b5a130..6360c90f9855 100644 --- a/arch/arm64/include/asm/kvm_arm.h +++ b/arch/arm64/include/asm/kvm_arm.h @@ -304,6 +304,8 @@ =20 /* Hyp Prefetch Fault Address Register (HPFAR/HDFAR) */ #define HPFAR_MASK (~UL(0xf)) +#define FAR_MASK GENMASK_ULL(11, 0) + /* * We have * PAR [PA_Shift - 1 : 12] =3D PA [PA_Shift - 1 : 12] diff --git a/arch/arm64/include/asm/kvm_pkvm.h b/arch/arm64/include/asm/kvm= _pkvm.h index ab26bec079d6..0a471564be00 100644 --- a/arch/arm64/include/asm/kvm_pkvm.h +++ b/arch/arm64/include/asm/kvm_pkvm.h @@ -20,9 +20,13 @@ /* The maximum number of hypervisor protected regions from the host */ #define PKVM_PROTECTED_REGS_NUM 8 =20 +struct pkvm_protected_reg; +typedef void(pkvm_emulate_handler)(struct pkvm_protected_reg *region, u64 = offset, + bool write, u64 *reg, u8 reg_size); struct pkvm_protected_reg { u64 pfn; u64 nr_pages; + pkvm_emulate_handler *cb; }; =20 extern struct pkvm_protected_reg kvm_nvhe_sym(pkvm_protected_regs)[]; diff --git a/arch/arm64/kvm/hyp/nvhe/mem_protect.c b/arch/arm64/kvm/hyp/nvh= e/mem_protect.c index 500c18c2fd48..7e978e0c44b9 100644 --- a/arch/arm64/kvm/hyp/nvhe/mem_protect.c +++ b/arch/arm64/kvm/hyp/nvhe/mem_protect.c @@ -14,6 +14,7 @@ #include =20 #include +#include =20 #include #include @@ -752,6 +753,50 @@ static void host_inject_mem_abort(struct kvm_cpu_conte= xt *host_ctxt) inject_host_exception(esr); } =20 +static bool handle_host_mmio_trap(struct kvm_cpu_context *host_ctxt, u64 e= sr, u64 addr) +{ + u64 offset, reg_value =3D 0, start, end; + u8 reg_size, reg_index; + bool write; + int i; + + for (i =3D 0; i < num_protected_reg; i++) { + if (!pkvm_protected_regs[i].pfn || !pkvm_protected_regs[i].nr_pages || + !pkvm_protected_regs[i].cb) + continue; + + start =3D PFN_PHYS(pkvm_protected_regs[i].pfn); + end =3D start + PFN_PHYS(pkvm_protected_regs[i].nr_pages); + reg_size =3D BIT((esr & ESR_ELx_SAS) >> ESR_ELx_SAS_SHIFT); + + if (start > addr || addr + reg_size > end) + continue; + + reg_index =3D (esr & ESR_ELx_SRT_MASK) >> ESR_ELx_SRT_SHIFT; + write =3D (esr & ESR_ELx_WNR) =3D=3D ESR_ELx_WNR; + offset =3D addr - start; + + if (write && reg_index !=3D 31) + reg_value =3D host_ctxt->regs.regs[reg_index]; + + pkvm_protected_regs[i].cb(&pkvm_protected_regs[i], offset, write, + ®_value, reg_size); + + if (!write && reg_index !=3D 31) + host_ctxt->regs.regs[reg_index] =3D reg_value; + + kvm_skip_host_instr(); + return true; + } + + return false; +} + +static bool is_dabt(u64 esr) +{ + return (ESR_ELx_EC(esr) =3D=3D ESR_ELx_EC_DABT_LOW) && (esr & ESR_ELx_ISV= ); +} + void handle_host_mem_abort(struct kvm_cpu_context *host_ctxt) { struct kvm_vcpu_fault_info fault; @@ -774,6 +819,10 @@ void handle_host_mem_abort(struct kvm_cpu_context *hos= t_ctxt) BUG_ON(!(fault.hpfar_el2 & HPFAR_EL2_NS)); addr =3D FIELD_GET(HPFAR_EL2_FIPA, fault.hpfar_el2) << 12; =20 + if (is_dabt(esr) && !addr_is_memory(addr) && + handle_host_mmio_trap(host_ctxt, esr, addr | (fault.far_el2 & FAR_MAS= K))) + return; + switch (host_stage2_idmap(addr)) { case -EPERM: host_inject_mem_abort(host_ctxt); diff --git a/arch/arm64/kvm/hyp/nvhe/setup.c b/arch/arm64/kvm/hyp/nvhe/setu= p.c index 64c0290da888..4395595b7f7e 100644 --- a/arch/arm64/kvm/hyp/nvhe/setup.c +++ b/arch/arm64/kvm/hyp/nvhe/setup.c @@ -294,6 +294,9 @@ static int donate_protected_mmio_regions(void) pkvm_protected_regs[i].nr_pages << PAGE_SHIFT); if (ret) goto err_setup; + + if (pkvm_protected_regs[i].cb) + pkvm_protected_regs[i].cb =3D kern_hyp_va(pkvm_protected_regs[i].cb); } =20 return 0; --=20 2.55.0.654.g21b8a5bc05-goog