linux-coco.lists.linux.dev archive mirror
 help / color / mirror / Atom feed
From: Aneesh Kumar K.V <aneesh.kumar@kernel.org>
To: Will Deacon <will@kernel.org>, linux-arm-kernel@lists.infradead.org
Cc: Will Deacon <will@kernel.org>,
	Sudeep Holla <sudeep.holla@arm.com>,
	Catalin Marinas <catalin.marinas@arm.com>,
	Lorenzo Pieralisi <lpieralisi@kernel.org>,
	Suzuki Poulose <suzuki.poulose@arm.com>,
	Steven Price <steven.price@arm.com>,
	Oliver Upton <oliver.upton@linux.dev>,
	Marc Zyngier <maz@kernel.org>,
	linux-coco@lists.linux.dev
Subject: Re: [PATCH 6/6] drivers/virt: pkvm: Intercept ioremap using pKVM MMIO_GUARD hypercall
Date: Wed, 31 Jul 2024 18:54:30 +0530	[thread overview]
Message-ID: <yq5awml1u229.fsf@kernel.org> (raw)
In-Reply-To: <20240730151113.1497-7-will@kernel.org>

Will Deacon <will@kernel.org> writes:

> Hook up pKVM's MMIO_GUARD hypercall so that ioremap() and friends will
> register the target physical address as MMIO with the hypervisor,
> allowing guest exits to that page to be emulated by the host with full
> syndrome information.
>
> Signed-off-by: Will Deacon <will@kernel.org>
> ---
>  Documentation/virt/kvm/arm/hypercalls.rst     | 26 ++++++++++++++
>  drivers/virt/coco/pkvm-guest/arm-pkvm-guest.c | 35 +++++++++++++++++++
>  include/linux/arm-smccc.h                     |  7 ++++
>  3 files changed, 68 insertions(+)
>
> diff --git a/Documentation/virt/kvm/arm/hypercalls.rst b/Documentation/virt/kvm/arm/hypercalls.rst
> index c42580e71bf8..af7bc2c2e0cb 100644
> --- a/Documentation/virt/kvm/arm/hypercalls.rst
> +++ b/Documentation/virt/kvm/arm/hypercalls.rst
> @@ -116,3 +116,29 @@ memory protection granule advertised by ``ARM_SMCCC_KVM_FUNC_HYP_MEMINFO``.
>  |                     |          |    +---------------------------------------------+
>  |                     |          |    | ``INVALID_PARAMETER (-3)``                  |
>  +---------------------+----------+----+---------------------------------------------+
> +
> +``ARM_SMCCC_KVM_FUNC_MMIO_GUARD``
> +----------------------------------
> +
> +Request that a given memory region is handled as MMIO by the hypervisor,
> +allowing accesses to this region to be emulated by the KVM host. The size of the
> +region is equal to the memory protection granule advertised by
> +``ARM_SMCCC_KVM_FUNC_HYP_MEMINFO``.
> +
> ++---------------------+-------------------------------------------------------------+
> +| Presence:           | Optional; pKVM protected guests only.                       |
> ++---------------------+-------------------------------------------------------------+
> +| Calling convention: | HVC64                                                       |
> ++---------------------+----------+--------------------------------------------------+
> +| Function ID:        | (uint32) | 0xC6000007                                       |
> ++---------------------+----------+----+---------------------------------------------+
> +| Arguments:          | (uint64) | R1 | Base IPA of MMIO memory region              |
> +|                     +----------+----+---------------------------------------------+
> +|                     | (uint64) | R2 | Reserved / Must be zero                     |
> +|                     +----------+----+---------------------------------------------+
> +|                     | (uint64) | R3 | Reserved / Must be zero                     |
> ++---------------------+----------+----+---------------------------------------------+
> +| Return Values:      | (int64)  | R0 | ``SUCCESS (0)``                             |
> +|                     |          |    +---------------------------------------------+
> +|                     |          |    | ``INVALID_PARAMETER (-3)``                  |
> ++---------------------+----------+----+---------------------------------------------+
>

Ok you need a SMCCC call not just a pgprot_t update. 

> diff --git a/drivers/virt/coco/pkvm-guest/arm-pkvm-guest.c b/drivers/virt/coco/pkvm-guest/arm-pkvm-guest.c
> index 8256cf68fd76..56a3859dda8a 100644
> --- a/drivers/virt/coco/pkvm-guest/arm-pkvm-guest.c
> +++ b/drivers/virt/coco/pkvm-guest/arm-pkvm-guest.c
> @@ -9,8 +9,10 @@
>  
>  #include <linux/arm-smccc.h>
>  #include <linux/array_size.h>
> +#include <linux/io.h>
>  #include <linux/mem_encrypt.h>
>  #include <linux/mm.h>
> +#include <linux/pgtable.h>
>  
>  #include <asm/hypervisor.h>
>  
> @@ -67,6 +69,36 @@ static const struct arm64_mem_crypt_ops pkvm_crypt_ops = {
>  	.decrypt	= pkvm_set_memory_decrypted,
>  };
>  
> +static int mmio_guard_ioremap_hook(phys_addr_t phys, size_t size,
> +				   pgprot_t *prot)
> +{
> +	phys_addr_t end;
> +	pteval_t protval = pgprot_val(*prot);
> +
> +	/*
> +	 * We only expect MMIO emulation for regions mapped with device
> +	 * attributes.
> +	 */
> +	if (protval != PROT_DEVICE_nGnRE && protval != PROT_DEVICE_nGnRnE)
> +		return 0;
> +
> +	phys = PAGE_ALIGN_DOWN(phys);
> +	end = phys + PAGE_ALIGN(size);
> +
> +	while (phys < end) {
> +		const int func_id = ARM_SMCCC_VENDOR_HYP_KVM_MMIO_GUARD_FUNC_ID;
> +		int err;
> +
> +		err = arm_smccc_do_one_page(func_id, phys);
> +		if (err)
> +			return err;
> +
> +		phys += PAGE_SIZE;
> +	}
> +
> +	return 0;
> +}
> +
>  void pkvm_init_hyp_services(void)
>  {
>  	int i;
> @@ -89,4 +121,7 @@ void pkvm_init_hyp_services(void)
>  
>  	pkvm_granule = res.a0;
>  	arm64_mem_crypt_ops_register(&pkvm_crypt_ops);
> +
> +	if (kvm_arm_hyp_service_available(ARM_SMCCC_KVM_FUNC_MMIO_GUARD))
> +		arm64_ioremap_prot_hook_register(&mmio_guard_ioremap_hook);
>  }
> diff --git a/include/linux/arm-smccc.h b/include/linux/arm-smccc.h
> index 9cb7c95920b0..e93c1f7cea70 100644
> --- a/include/linux/arm-smccc.h
> +++ b/include/linux/arm-smccc.h
> @@ -118,6 +118,7 @@
>  #define ARM_SMCCC_KVM_FUNC_HYP_MEMINFO		2
>  #define ARM_SMCCC_KVM_FUNC_MEM_SHARE		3
>  #define ARM_SMCCC_KVM_FUNC_MEM_UNSHARE		4
> +#define ARM_SMCCC_KVM_FUNC_MMIO_GUARD		7
>  #define ARM_SMCCC_KVM_FUNC_FEATURES_2		127
>  #define ARM_SMCCC_KVM_NUM_FUNCS			128
>  
> @@ -158,6 +159,12 @@
>  			   ARM_SMCCC_OWNER_VENDOR_HYP,			\
>  			   ARM_SMCCC_KVM_FUNC_MEM_UNSHARE)
>  
> +#define ARM_SMCCC_VENDOR_HYP_KVM_MMIO_GUARD_FUNC_ID			\
> +	ARM_SMCCC_CALL_VAL(ARM_SMCCC_FAST_CALL,				\
> +			   ARM_SMCCC_SMC_64,				\
> +			   ARM_SMCCC_OWNER_VENDOR_HYP,			\
> +			   ARM_SMCCC_KVM_FUNC_MMIO_GUARD)
> +
>  /* ptp_kvm counter type ID */
>  #define KVM_PTP_VIRT_COUNTER			0
>  #define KVM_PTP_PHYS_COUNTER			1
> -- 
> 2.46.0.rc1.232.g9752f9e123-goog

  reply	other threads:[~2024-07-31 13:24 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-07-30 15:11 [PATCH 0/6] Support for running as a pKVM protected guest Will Deacon
2024-07-30 15:11 ` [PATCH 1/6] firmware/smccc: Call arch-specific hook on discovering KVM services Will Deacon
2024-07-31 14:41   ` Aneesh Kumar K.V
2024-07-31 15:50     ` Will Deacon
2024-07-31 15:53       ` Aneesh Kumar K.V
2024-07-31 15:56         ` Aneesh Kumar K.V
2024-08-02 15:44           ` Catalin Marinas
2024-08-02 16:16             ` Aneesh Kumar K.V
2024-08-02 15:30       ` Suzuki K Poulose
2024-08-07 12:43         ` Suzuki K Poulose
2024-08-23 13:13         ` Will Deacon
2024-08-02 15:13     ` Catalin Marinas
2024-07-30 15:11 ` [PATCH 2/6] drivers/virt: pkvm: Add initial support for running as a protected guest Will Deacon
2024-07-30 15:11 ` [PATCH 3/6] arm64: mm: Add top-level dispatcher for internal mem_encrypt API Will Deacon
2024-07-30 15:11 ` [PATCH 4/6] drivers/virt: pkvm: Hook up mem_encrypt API using pKVM hypercalls Will Deacon
2024-08-21 16:49   ` Marc Zyngier
2024-08-23 15:41     ` Will Deacon
2024-08-23 16:53       ` Marc Zyngier
2024-07-30 15:11 ` [PATCH 5/6] arm64: mm: Add confidential computing hook to ioremap_prot() Will Deacon
2024-07-30 15:11 ` [PATCH 6/6] drivers/virt: pkvm: Intercept ioremap using pKVM MMIO_GUARD hypercall Will Deacon
2024-07-31 13:24   ` Aneesh Kumar K.V [this message]
2024-07-31 13:55 ` [PATCH 0/6] Support for running as a pKVM protected guest Suzuki K Poulose
2024-07-31 15:52   ` Will Deacon

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=yq5awml1u229.fsf@kernel.org \
    --to=aneesh.kumar@kernel.org \
    --cc=catalin.marinas@arm.com \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-coco@lists.linux.dev \
    --cc=lpieralisi@kernel.org \
    --cc=maz@kernel.org \
    --cc=oliver.upton@linux.dev \
    --cc=steven.price@arm.com \
    --cc=sudeep.holla@arm.com \
    --cc=suzuki.poulose@arm.com \
    --cc=will@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).