public inbox for devel@edk2.groups.io
 help / color / mirror / Atom feed
From: "Sami Mujawar" <sami.mujawar@arm.com>
To: <devel@edk2.groups.io>
Cc: Sami Mujawar <sami.mujawar@arm.com>, <ardb+tianocore@kernel.org>,
	<quic_llindhol@quicinc.com>, <kraxel@redhat.com>,
	<Matteo.Carlini@arm.com>, <Akanksha.Jain2@arm.com>,
	<Sibel.Allinson@arm.com>, <nd@arm.com>
Subject: [edk2-devel] [PATCH v2 12/45] ArmVirtPkg: Add library for Arm CCA helper functions
Date: Fri, 12 Apr 2024 15:32:49 +0100	[thread overview]
Message-ID: <20240412143322.5244-13-sami.mujawar@arm.com> (raw)
In-Reply-To: <20240412143322.5244-1-sami.mujawar@arm.com>

Introduce ArmCcaLib library that implements helper
functions to:
- probe if the code is executing in a Realm context
- configure the protection attribute in page tables
  for the memory regions shared with the host
- get the IPA width of the Realm which was stored in
  the GUID HOB gArmCcaIpaWidthGuid.

Cc: Ard Biesheuvel <ardb+tianocore@kernel.org>
Cc: Leif Lindholm <quic_llindhol@quicinc.com>
Cc: Gerd Hoffmann <kraxel@redhat.com>
Signed-off-by: Sami Mujawar <sami.mujawar@arm.com>
---
 ArmVirtPkg/ArmVirtPkg.dec                  |   1 +
 ArmVirtPkg/Include/Library/ArmCcaLib.h     | 114 +++++++++++++
 ArmVirtPkg/Library/ArmCcaLib/ArmCcaLib.c   | 174 ++++++++++++++++++++
 ArmVirtPkg/Library/ArmCcaLib/ArmCcaLib.inf |  34 ++++
 4 files changed, 323 insertions(+)

diff --git a/ArmVirtPkg/ArmVirtPkg.dec b/ArmVirtPkg/ArmVirtPkg.dec
index 23179c89704db789ab649de81873924c3958ab2a..d521e107ddff15e6dc585e3a63567e3ad817189f 100644
--- a/ArmVirtPkg/ArmVirtPkg.dec
+++ b/ArmVirtPkg/ArmVirtPkg.dec
@@ -27,6 +27,7 @@ [Includes.common]
 
 [LibraryClasses]
   ArmCcaInitPeiLib|Include/Library/ArmCcaInitPeiLib.h
+  ArmCcaLib|Include/Library/ArmCcaLib.h
   ArmCcaRsiLib|Include/Library/ArmCcaRsiLib.h
   ArmVirtMemInfoLib|Include/Library/ArmVirtMemInfoLib.h
 
diff --git a/ArmVirtPkg/Include/Library/ArmCcaLib.h b/ArmVirtPkg/Include/Library/ArmCcaLib.h
new file mode 100644
index 0000000000000000000000000000000000000000..a47e14b507f1bfd1feece636063eb2ba83357a5b
--- /dev/null
+++ b/ArmVirtPkg/Include/Library/ArmCcaLib.h
@@ -0,0 +1,114 @@
+/** @file
+  Library that implements the Arm CCA helper functions.
+
+  Copyright (c) 2022 - 2023, Arm Ltd. All rights reserved.<BR>
+  SPDX-License-Identifier: BSD-2-Clause-Patent
+
+    - Rsi or RSI   - Realm Service Interface
+    - IPA          - Intermediate Physical Address
+    - RIPAS        - Realm IPA state
+**/
+
+#ifndef ARM_CCA_LIB_
+#define ARM_CCA_LIB_
+
+#include <Base.h>
+#include <Uefi/UefiBaseType.h>
+
+/**
+  Check if running in a Realm.
+
+    @retval TRUE    The execution is within the context of a Realm.
+    @retval FALSE   The execution is not within the context of a Realm.
+**/
+BOOLEAN
+EFIAPI
+IsRealm (
+  VOID
+  );
+
+/**
+  Configure the protection attribute for the page tables
+  describing the memory region.
+
+  The IPA space of a Realm is divided into two halves:
+    - Protected IPA space and
+    - Unprotected IPA space.
+
+  Software in a Realm should treat the most significant bit of an
+  IPA as a protection attribute.
+
+  A Protected IPA is an address in the lower half of a Realms IPA
+  space. The most significant bit of a Protected IPA is 0.
+
+  An Unprotected IPA is an address in the upper half of a Realms
+  IPA space. The most significant bit of an Unprotected IPA is 1.
+
+  Note:
+  - Configuring the memory region as Unprotected IPA enables the
+    Realm to share the memory region with the Host.
+  - This function updates the page table entries to reflect the
+    protection attribute.
+  - A separate call to transition the memory range using the Realm
+    Service Interface (RSI) RSI_IPA_STATE_SET command is additionally
+    required and is expected to be done outside this function.
+
+    @param [in]  BaseAddress  Base address of the memory region.
+    @param [in]  Length       Length of the memory region.
+    @param [in]  IpaWidth     IPA width of the Realm.
+    @param [in]  Share        If TRUE, set the most significant
+                              bit of the IPA to configure the memory
+                              region as Unprotected IPA.
+                              If FALSE, clear the most significant
+                              bit of the IPA to configure the memory
+                              region as Protected IPA.
+
+    @retval RETURN_SUCCESS            IPA protection attribute updated.
+    @retval RETURN_INVALID_PARAMETER  A parameter is invalid.
+    @retval RETURN_UNSUPPORTED        The request is not initiated in a
+                                      Realm.
+**/
+RETURN_STATUS
+EFIAPI
+ArmCcaSetMemoryProtectAttribute (
+  IN EFI_PHYSICAL_ADDRESS  BaseAddress,
+  IN UINT64                Length,
+  IN UINT64                IpaWidth,
+  IN BOOLEAN               Share
+  );
+
+/**
+  Return the IPA width of the Realm.
+
+  The IPA width of the Realm is used to configure the protection attribute
+  for memory regions, see ArmCcaSetMemoryProtectAttribute().
+
+  The IPA width of the Realm is present in the Realm config which is read
+  when the ArmCcaInitPeiLib library hook function ArmCcaInitialize () is
+  called in the PrePi phase. ArmCcaInitialize () stores the IPA width of
+  the Realm in a GUID HOB gArmCcaIpaWidthGuid.
+
+  This function searches the GUID HOB gArmCcaIpaWidthGuid and returns the
+  IPA width value stored therein.
+
+  Note:
+  - This function must only be called after ArmCcaInitialize () has setup
+    the GUID HOB gArmCcaIpaWidthGuid.
+
+    @param [out] IpaWidth  IPA width of the Realm.
+
+    @retval RETURN_SUCCESS            Success.
+    @retval RETURN_INVALID_PARAMETER  A parameter is invalid.
+    @retval RETURN_NOT_FOUND          The GUID HOB gArmCcaIpaWidthGuid is not
+                                      found and could mean that this function
+                                      was called before ArmCcaInitialize ()
+                                      has created and initialised the GUID
+                                      HOB gArmCcaIpaWidthGuid.
+**/
+RETURN_STATUS
+EFIAPI
+GetIpaWidth (
+  OUT UINT64  *IpaWidth
+  );
+
+#endif // ARM_CCA_LIB_
diff --git a/ArmVirtPkg/Library/ArmCcaLib/ArmCcaLib.c b/ArmVirtPkg/Library/ArmCcaLib/ArmCcaLib.c
new file mode 100644
index 0000000000000000000000000000000000000000..57b05f308377cf931c5f43fc7793c260dfdc36fb
--- /dev/null
+++ b/ArmVirtPkg/Library/ArmCcaLib/ArmCcaLib.c
@@ -0,0 +1,174 @@
+/** @file
+  Library that implements the Arm CCA helper functions.
+
+  Copyright (c) 2022 - 2023, Arm Limited. All rights reserved.<BR>
+  SPDX-License-Identifier: BSD-2-Clause-Patent
+
+  @par Glossary:
+    - Rsi or RSI   - Realm Service Interface
+    - IPA          - Intermediate Physical Address
+    - RIPAS        - Realm IPA state
+**/
+#include <Base.h>
+
+#include <Chipset/AArch64.h>
+#include <Library/ArmCcaLib.h>
+#include <Library/ArmCcaRsiLib.h>
+#include <Library/ArmMmuLib.h>
+#include <Library/BaseLib.h>
+
+#include <Uefi/UefiMultiPhase.h>
+#include <Uefi/UefiSpec.h>
+#include <Pi/PiBootMode.h>
+#include <Pi/PiHob.h>
+#include <Library/HobLib.h>
+
+/**
+  Check if running in a Realm.
+
+    @retval TRUE    The execution is within the context of a Realm.
+    @retval FALSE   The execution is not within the context of a Realm.
+**/
+BOOLEAN
+EFIAPI
+IsRealm (
+  VOID
+  )
+{
+  RETURN_STATUS  Status;
+  UINT16         Major;
+  UINT16         Minor;
+
+  if (ArmHasRme ()) {
+    Status = RsiGetVersion (&Major, &Minor);
+    if (!RETURN_ERROR (Status)) {
+      return TRUE;
+    }
+  }
+
+  return FALSE;
+}
+
+/**
+  Configure the protection attribute for the page tables
+  describing the memory region.
+
+  The IPA space of a Realm is divided into two halves:
+    - Protected IPA space and
+    - Unprotected IPA space.
+
+  Software in a Realm should treat the most significant bit of an
+  IPA as a protection attribute.
+
+  A Protected IPA is an address in the lower half of a Realms IPA
+  space. The most significant bit of a Protected IPA is 0.
+
+  An Unprotected IPA is an address in the upper half of a Realms
+  IPA space. The most significant bit of an Unprotected IPA is 1.
+
+  Note:
+  - Configuring the memory region as Unprotected IPA enables the
+    Realm to share the memory region with the Host.
+  - This function updates the page table entries to reflect the
+    protection attribute.
+  - A separate call to transition the memory range using the Realm
+    Service Interface (RSI) RSI_IPA_STATE_SET command is additionally
+    required and is expected to be done outside this function.
+
+    @param [in]  BaseAddress  Base address of the memory region.
+    @param [in]  Length       Length of the memory region.
+    @param [in]  IpaWidth     IPA width of the Realm.
+    @param [in]  Share        If TRUE, set the most significant
+                              bit of the IPA to configure the memory
+                              region as Unprotected IPA.
+                              If FALSE, clear the most significant
+                              bit of the IPA to configure the memory
+                              region as Protected IPA.
+
+    @retval RETURN_SUCCESS            IPA protection attribute updated.
+    @retval RETURN_INVALID_PARAMETER  A parameter is invalid.
+    @retval RETURN_UNSUPPORTED        The request is not initiated in a
+                                      Realm.
+**/
+RETURN_STATUS
+EFIAPI
+ArmCcaSetMemoryProtectAttribute (
+  IN EFI_PHYSICAL_ADDRESS  BaseAddress,
+  IN UINT64                Length,
+  IN UINT64                IpaWidth,
+  IN BOOLEAN               Share
+  )
+{
+  if (!IsRealm ()) {
+    return RETURN_UNSUPPORTED;
+  }
+
+  if ((Length == 0) || (IpaWidth == 0)) {
+    return RETURN_INVALID_PARAMETER;
+  }
+
+  return SetMemoryProtectionAttribute (
+           BaseAddress,
+           Length,
+           IpaWidth,
+           Share
+           );
+}
+
+/**
+  Return the IPA width of the Realm.
+
+  The IPA width of the Realm is used to configure the protection attribute
+  for memory regions, see ArmCcaSetMemoryProtectAttribute().
+
+  The IPA width of the Realm is present in the Realm config which is read
+  when the ArmCcaInitPeiLib library hook function ArmCcaInitialize () is
+  called in the PrePi phase. ArmCcaInitialize () stores the IPA width of
+  the Realm in a GUID HOB gArmCcaIpaWidthGuid.
+
+  This function searches the GUID HOB gArmCcaIpaWidthGuid and returns the
+  IPA width value stored therein.
+
+  Note:
+  - This function must only be called after ArmCcaInitialize () has setup
+    the GUID HOB gArmCcaIpaWidthGuid.
+
+    @param [out] IpaWidth  IPA width of the Realm.
+
+    @retval RETURN_SUCCESS            Success.
+    @retval RETURN_INVALID_PARAMETER  A parameter is invalid.
+    @retval RETURN_NOT_FOUND          The GUID HOB gArmCcaIpaWidthGuid is not
+                                      found and could mean that this function
+                                      was called before ArmCcaInitialize ()
+                                      has created and initialised the GUID
+                                      HOB gArmCcaIpaWidthGuid.
+**/
+RETURN_STATUS
+EFIAPI
+GetIpaWidth (
+  OUT UINT64  *IpaWidth
+  )
+{
+  VOID    *Hob;
+  UINT64  *CcaIpaWidth;
+
+  if (IpaWidth == NULL) {
+    return RETURN_INVALID_PARAMETER;
+  }
+
+  Hob = GetFirstGuidHob (&gArmCcaIpaWidthGuid);
+  if ((Hob == NULL) ||
+      (GET_GUID_HOB_DATA_SIZE (Hob) != sizeof (UINT64)))
+  {
+    return RETURN_NOT_FOUND;
+  }
+
+  CcaIpaWidth = GET_GUID_HOB_DATA (Hob);
+  if ((UINT64)*CcaIpaWidth == 0) {
+    return RETURN_NOT_FOUND;
+  }
+
+  *IpaWidth = *CcaIpaWidth;
+
+  return RETURN_SUCCESS;
+}
diff --git a/ArmVirtPkg/Library/ArmCcaLib/ArmCcaLib.inf b/ArmVirtPkg/Library/ArmCcaLib/ArmCcaLib.inf
new file mode 100644
index 0000000000000000000000000000000000000000..7d90b4535d69c12672af5de3d7cab63a3cd528a6
--- /dev/null
+++ b/ArmVirtPkg/Library/ArmCcaLib/ArmCcaLib.inf
@@ -0,0 +1,34 @@
+## @file
+#  Library that implements the Arm CCA helper functions.
+#
+#  Copyright (c) 2022 - 2023, Arm Limited. All rights reserved.<BR>
+#
+#  SPDX-License-Identifier: BSD-2-Clause-Patent
+#
+##
+
+[Defines]
+  INF_VERSION                    = 0x0001001B
+  BASE_NAME                      = ArmCcaLib
+  FILE_GUID                      = 11C18743-52F9-405E-B35B-D7BE91A26F9F
+  MODULE_TYPE                    = BASE
+  VERSION_STRING                 = 1.0
+  LIBRARY_CLASS                  = ArmCcaLib
+
+[Sources]
+  ArmCcaLib.c
+
+[Packages]
+  ArmPkg/ArmPkg.dec
+  ArmVirtPkg/ArmVirtPkg.dec
+  MdePkg/MdePkg.dec
+
+[LibraryClasses]
+  ArmCcaRsiLib
+  ArmLib
+  ArmMmuLib
+  BaseLib
+  HobLib
+
+[Guids]
+  gArmCcaIpaWidthGuid
-- 
'Guid(CE165669-3EF3-493F-B85D-6190EE5B9759)'



-=-=-=-=-=-=-=-=-=-=-=-
Groups.io Links: You receive all messages sent to this group.
View/Reply Online (#117682): https://edk2.groups.io/g/devel/message/117682
Mute This Topic: https://groups.io/mt/105483420/7686176
Group Owner: devel+owner@edk2.groups.io
Unsubscribe: https://edk2.groups.io/g/devel/unsub [rebecca@openfw.io]
-=-=-=-=-=-=-=-=-=-=-=-



  parent reply	other threads:[~2024-04-12 14:33 UTC|newest]

Thread overview: 40+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <20240412143322.5244-1-sami.mujawar@arm.com>
2024-04-12 14:32 ` [edk2-devel] [PATCH v2 01/45] ArmPkg: Add helper function to detect RME Sami Mujawar
2024-04-12 14:32 ` [edk2-devel] [PATCH v2 02/45] ArmPkg: Introduce SetMemoryProtectionAttribute() for Realms Sami Mujawar
2024-04-12 14:32 ` [edk2-devel] [PATCH v2 03/45] ArmPkg: Extend number of parameter registers in SMC call Sami Mujawar
2024-04-12 14:32 ` [edk2-devel] [PATCH v2 04/45] ArmVirtPkg: Add Arm CCA Realm Service Interface Library Sami Mujawar
2024-04-12 14:32 ` [edk2-devel] [PATCH v2 05/45] ArmVirtPkg: ArmCcaRsiLib: Add interfaces to manage the Realm IPA state Sami Mujawar
2024-04-12 14:32 ` [edk2-devel] [PATCH v2 06/45] ArmVirtPkg: ArmCcaRsiLib: Add an interface to get an attestation token Sami Mujawar
2024-04-12 14:32 ` [edk2-devel] [PATCH v2 07/45] ArmVirtPkg: ArmCcaRsiLib: Add interfaces to get/extend REMs Sami Mujawar
2024-04-12 14:32 ` [edk2-devel] [PATCH v2 08/45] ArmVirtPkg: ArmCcaRsiLib: Add an interface to make a RSI Host Call Sami Mujawar
2024-04-12 14:32 ` [edk2-devel] [PATCH v2 09/45] ArmVirtPkg: Define a GUID HOB for IPA width of a Realm Sami Mujawar
2024-04-12 14:32 ` [edk2-devel] [PATCH v2 10/45] ArmVirtPkg: Add library for Arm CCA initialisation in PEI Sami Mujawar
2024-04-12 14:32 ` [edk2-devel] [PATCH v2 11/45] ArmVirtPkg: Add NULL instance of ArmCcaInitPeiLib Sami Mujawar
2024-04-12 14:32 ` Sami Mujawar [this message]
2024-04-12 14:32 ` [edk2-devel] [PATCH v2 13/45] ArmVirtPkg: Add Null instance of ArmCcaLib Sami Mujawar
2024-04-12 14:32 ` [edk2-devel] [PATCH v2 14/45] ArmVirtPkg: Define an interface to configure MMIO regions for Arm CCA Sami Mujawar
2024-04-12 14:32 ` [edk2-devel] [PATCH v2 15/45] ArmVirtPkg: CloudHv: Add a NULL implementation of ArmCcaConfigureMmio Sami Mujawar
2024-04-12 14:32 ` [edk2-devel] [PATCH v2 16/45] ArmVirtPkg: Qemu: " Sami Mujawar
2024-04-12 14:32 ` [edk2-devel] [PATCH v2 17/45] ArmVirtPkg: Xen: " Sami Mujawar
2024-04-12 14:32 ` [edk2-devel] [PATCH v2 18/45] ArmVirtPkg: Configure the MMIO regions for Arm CCA Sami Mujawar
2024-04-12 14:32 ` [edk2-devel] [PATCH v2 19/45] ArmVirtPkg: Kvmtool: Use Null version of DebugLib in PrePi Sami Mujawar
2024-04-12 14:32 ` [edk2-devel] [PATCH v2 20/45] ArmVirtPkg: Introduce ArmVirtMonitorLib library Sami Mujawar
2024-04-12 14:32 ` [edk2-devel] [PATCH v2 21/45] ArmVirtPkg: Kvmtool: Use ArmVirt instance of ArmMonitorLib Sami Mujawar
2024-04-12 14:32 ` [edk2-devel] [PATCH v2 22/45] ArmVirtPkg: Add Arm CCA libraries for Kvmtool guest firmware Sami Mujawar
2024-04-12 14:33 ` [edk2-devel] [PATCH v2 23/45] ArmVirtPkg: Arm CCA configure system memory in early Pei Sami Mujawar
2024-04-12 14:33 ` [edk2-devel] [PATCH v2 24/45] ArmVirtPkg: Perform Arm CCA initialisation in the Pei phase Sami Mujawar
2024-04-12 14:33 ` [edk2-devel] [PATCH v2 25/45] ArmVirtPkg: Introduce Realm Aperture Management Protocol Sami Mujawar
2024-04-12 14:33 ` [edk2-devel] [PATCH v2 26/45] ArmVirtPkg: IoMMU driver to DMA from Realms Sami Mujawar
2024-04-12 14:33 ` [edk2-devel] [PATCH v2 27/45] ArmVirtPkg: Enable Virtio communication for Arm CCA Sami Mujawar
2024-04-12 14:33 ` [edk2-devel] [PATCH v2 28/45] MdePkg: Warn if AArch64 RNDR instruction is not supported Sami Mujawar
2024-04-12 14:33 ` [edk2-devel] [PATCH v2 29/45] ArmVirtPkg: Kvmtool: Switch to use BaseRng for AArch64 Sami Mujawar
2024-04-12 14:33 ` [edk2-devel] [PATCH v2 31/45] ArmVirtPkg: ArmCcaRsiLib: Fix size of Imm field in HostCallArgs Sami Mujawar
2024-04-12 14:33 ` [edk2-devel] [PATCH v2 32/45] ArmVirtPkg: RMM 1.0-bet1 - Update width of RSI host call struct Sami Mujawar
2024-04-12 14:33 ` [edk2-devel] [PATCH v2 33/45] ArmVirtPkg: RMM 1.0-bet2 - Increase number of RSI host call args Sami Mujawar
2024-04-12 14:33 ` [edk2-devel] [PATCH v2 34/45] ArmVirtPkg: RMM 1.0-eac0 - Update RsiSetIpaState parameter usage Sami Mujawar
2024-04-12 14:33 ` [edk2-devel] [PATCH v2 35/45] ArmVirtPkg: RMM 1.0-eac1 - Relax alignment of RSI host call arg Sami Mujawar
2024-04-12 14:33 ` [edk2-devel] [PATCH v2 36/45] ArmVirtPkg: RMM 1.0-eac2 - Update RsiRealmConfig structure Sami Mujawar
2024-04-12 14:33 ` [edk2-devel] [PATCH v2 37/45] ArmVirtPkg: RMM 1.0-eac2 - Add RIPAS DESTROYED state Sami Mujawar
2024-04-12 14:33 ` [edk2-devel] [PATCH v2 38/45] ArmVirtPkg: RMM 1.0-eac2 - Add RsiRipasChangeFlags definitions Sami Mujawar
2024-04-12 14:33 ` [edk2-devel] [PATCH v2 39/45] ArmVirtPkg: RMM 1.0-eac2 - Add Flags to RsiSetIpaState() Sami Mujawar
2024-04-12 14:33 ` [edk2-devel] [PATCH v2 40/45] ArmVirtPkg: RMM 1.0-eac3 - Handle RsiSetIpaState() response Sami Mujawar
2024-04-12 14:33 ` [edk2-devel] [PATCH v2 42/45] ArmVirtPkg: RMM 1.0-eac5 - Attestation token API updates Sami Mujawar

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-list 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=20240412143322.5244-13-sami.mujawar@arm.com \
    --to=devel@edk2.groups.io \
    /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