From nobody Sun Apr 28 12:50:48 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) client-ip=66.175.222.108; envelope-from=bounce+27952+78887+1787277+3901457@groups.io; helo=mail02.groups.io; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) smtp.mailfrom=bounce+27952+78887+1787277+3901457@groups.io; dmarc=fail(p=none dis=none) header.from=posteo.de ARC-Seal: i=1; a=rsa-sha256; t=1628451639; cv=none; d=zohomail.com; s=zohoarc; b=CjTB+qUP5YVYmWs4TwXDAxVwvUrMuqIr5UVWwwuGnOu6lZQQ5OufikWITBLHQiJr8OshzxSIZNYOd7tUl05vIbT9QpQZ4TZAzdcGJGSq/nSNPUsAJfBL1k8B3x5aD7/fQ9nbb3ifYReSn9Q3IjP9O/9YpubS+u/kqalSzzG5qEU= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1628451639; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Reply-To:References:Sender:Subject:To; bh=etNkj50roiVHd5Q1keggZiSO8zfVGyKabkodbxF1duo=; b=CcN7irky1G0SVY2P4wKb2pV/6vFFQcwADbdms3nckL0BJo19EgqLGzLSeOmYV1XjSTSqXzr/2Q674M/MNuPLWMt1YSKu5BcC8M0kONmlQhssdYv5eKPLT882Wq/nueh9YZy5LcGd6k7oKE36tMacVk3ZbkjmOCSR8RyUUJOqobM= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) smtp.mailfrom=bounce+27952+78887+1787277+3901457@groups.io; dmarc=fail header.from= (p=none dis=none) Received: from mail02.groups.io (mail02.groups.io [66.175.222.108]) by mx.zohomail.com with SMTPS id 1628451639375377.0092105564405; Sun, 8 Aug 2021 12:40:39 -0700 (PDT) Return-Path: X-Received: by 127.0.0.2 with SMTP id q8DGYY1788612xo0K5YGui2G; Sun, 08 Aug 2021 12:40:39 -0700 X-Received: from mout02.posteo.de (mout02.posteo.de [185.67.36.66]) by mx.groups.io with SMTP id smtpd.web11.13763.1628451638195781372 for ; Sun, 08 Aug 2021 12:40:38 -0700 X-Received: from submission (posteo.de [89.146.220.130]) by mout02.posteo.de (Postfix) with ESMTPS id 846C5240104 for ; Sun, 8 Aug 2021 21:40:36 +0200 (CEST) X-Received: from customer (localhost [127.0.0.1]) by submission (posteo.de) with ESMTPSA id 4GjV1b4ZFNz6tmH; Sun, 8 Aug 2021 21:40:35 +0200 (CEST) From: =?UTF-8?B?TWFydmluIEjDpHVzZXI=?= To: devel@edk2.groups.io Cc: Michael D Kinney , Liming Gao , Zhiguang Liu , Jian J Wang , Hao A Wu , Ray Ni , Vitaly Cheptsov Subject: [edk2-devel] [PATCH] MdePkg/Base.h: Introduce various alignment-related macros Date: Sun, 8 Aug 2021 19:39:45 +0000 Message-Id: In-Reply-To: <5df11a13422732b9c03c120775a2b4dd0a49182f.1628444003.git.mhaeuser@posteo.de> References: <5df11a13422732b9c03c120775a2b4dd0a49182f.1628444003.git.mhaeuser@posteo.de> MIME-Version: 1.0 Precedence: Bulk List-Unsubscribe: List-Subscribe: List-Help: Sender: devel@edk2.groups.io List-Id: Mailing-List: list devel@edk2.groups.io; contact devel+owner@edk2.groups.io Reply-To: devel@edk2.groups.io,mhaeuser@posteo.de X-Gm-Message-State: y9MTaQqT9uIKso3O18HzbW6bx1787277AA= Content-Transfer-Encoding: quoted-printable DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=groups.io; q=dns/txt; s=20140610; t=1628451639; bh=MDIs9JaRB1v7xxmRkHUjUfF605BhiSWy+1n+8JfE3lA=; h=Cc:Content-Type:Date:From:Reply-To:Subject:To; b=Mlq/4rlOmpYbCbKTik877Mo/gAMaVBinzsUGcy+85a9kz4RsCssqlANQnAV3KnaEvCG I/f+eRtAniw1no9/YxqzyC1106QPXypxDGd/0BCVXDapMOtldZVt/2h2T+KA2JvgM/qBI 0MrJPAybsCW9lPOvRcVdUiPKccwCTONTSO0= X-ZohoMail-DKIM: pass (identity @groups.io) X-ZM-MESSAGEID: 1628451639759100046 Content-Type: text/plain; charset="utf-8" ALIGNOF: Determining the alignment requirement of data types is crucial to ensure safe memory accesses when parsing untrusted data. IS_POW2: Determining whether a value is a power of two is important to verify whether untrusted values are valid alignment values. IS_ALIGNED: In combination with ALIGNOF data offsets can be verified. A more general version of IS_ALIGNED defined by several modules. ADDRESS_IS_ALIGNED: Variant of IS_ALIGNED for pointers and addresses. Replaces module-specific definitions throughout the codebase. ALIGN_VALUE_ADDEND: The added to align up can be used to directly determine the required offset for data alignment. Cc: Michael D Kinney Cc: Liming Gao Cc: Zhiguang Liu Cc: Jian J Wang Cc: Hao A Wu Cc: Ray Ni Cc: Vitaly Cheptsov Signed-off-by: Marvin H=C3=A4user Reviewed-by: Hao A Wu --- MdeModulePkg/Bus/Ata/AhciPei/AhciMode.c | 2 +- MdeModulePkg/Bus/Ata/AhciPei/AhciPeiPassThru.c | 6 +- MdeModulePkg/Bus/Ata/AtaAtapiPassThru/AtaAtapiPassThru.c | 12 +-- MdeModulePkg/Bus/Ata/AtaBusDxe/AtaPassThruExecute.c | 2 +- MdeModulePkg/Bus/Scsi/ScsiDiskDxe/ScsiDisk.c | 4 +- MdeModulePkg/Bus/Ufs/UfsPassThruDxe/UfsPassThru.c | 6 +- MdeModulePkg/Universal/EbcDxe/EbcExecute.c | 36 ++++---- MdeModulePkg/Bus/Ata/AhciPei/AhciPei.h | 1 - MdeModulePkg/Bus/Ata/AtaAtapiPassThru/AtaAtapiPassThru.h | 2 - MdeModulePkg/Bus/Ata/AtaBusDxe/AtaBus.h | 1 - MdeModulePkg/Bus/Scsi/ScsiDiskDxe/ScsiDisk.h | 4 +- MdeModulePkg/Bus/Ufs/UfsBlockIoPei/UfsBlockIoPei.h | 2 - MdeModulePkg/Bus/Ufs/UfsPassThruDxe/UfsPassThru.h | 2 - MdeModulePkg/Universal/EbcDxe/EbcExecute.h | 3 +- MdePkg/Include/Base.h | 90 ++++++++++++= +++++++- 15 files changed, 125 insertions(+), 48 deletions(-) diff --git a/MdeModulePkg/Bus/Ata/AhciPei/AhciMode.c b/MdeModulePkg/Bus/Ata= /AhciPei/AhciMode.c index 7636ad27c86c..520197aee752 100644 --- a/MdeModulePkg/Bus/Ata/AhciPei/AhciMode.c +++ b/MdeModulePkg/Bus/Ata/AhciPei/AhciMode.c @@ -2099,7 +2099,7 @@ TrustTransferAtaDevice ( // ATA PassThru PPI. // if ((AtaPassThru->Mode->IoAlign > 1) && - !IS_ALIGNED (Buffer, AtaPassThru->Mode->IoAlign)) { + !ADDRESS_IS_ALIGNED (Buffer, AtaPassThru->Mode->IoAlign)) { NewBuffer =3D AllocateAlignedPages ( EFI_SIZE_TO_PAGES (TransferLength), AtaPassThru->Mode->IoAlign diff --git a/MdeModulePkg/Bus/Ata/AhciPei/AhciPeiPassThru.c b/MdeModulePkg/= Bus/Ata/AhciPei/AhciPeiPassThru.c index 191b78c88541..057ad42d596b 100644 --- a/MdeModulePkg/Bus/Ata/AhciPei/AhciPeiPassThru.c +++ b/MdeModulePkg/Bus/Ata/AhciPei/AhciPeiPassThru.c @@ -193,15 +193,15 @@ AhciAtaPassThruPassThru ( } =20 IoAlign =3D This->Mode->IoAlign; - if ((IoAlign > 1) && !IS_ALIGNED (Packet->InDataBuffer, IoAlign)) { + if ((IoAlign > 1) && !ADDRESS_IS_ALIGNED (Packet->InDataBuffer, IoAlign)= ) { return EFI_INVALID_PARAMETER; } =20 - if ((IoAlign > 1) && !IS_ALIGNED (Packet->OutDataBuffer, IoAlign)) { + if ((IoAlign > 1) && !ADDRESS_IS_ALIGNED (Packet->OutDataBuffer, IoAlign= )) { return EFI_INVALID_PARAMETER; } =20 - if ((IoAlign > 1) && !IS_ALIGNED (Packet->Asb, IoAlign)) { + if ((IoAlign > 1) && !ADDRESS_IS_ALIGNED (Packet->Asb, IoAlign)) { return EFI_INVALID_PARAMETER; } =20 diff --git a/MdeModulePkg/Bus/Ata/AtaAtapiPassThru/AtaAtapiPassThru.c b/Mde= ModulePkg/Bus/Ata/AtaAtapiPassThru/AtaAtapiPassThru.c index 86fe9d954fdb..c7b3cfce1340 100644 --- a/MdeModulePkg/Bus/Ata/AtaAtapiPassThru/AtaAtapiPassThru.c +++ b/MdeModulePkg/Bus/Ata/AtaAtapiPassThru/AtaAtapiPassThru.c @@ -1281,15 +1281,15 @@ AtaPassThruPassThru ( =20 Instance =3D ATA_PASS_THRU_PRIVATE_DATA_FROM_THIS (This); =20 - if ((This->Mode->IoAlign > 1) && !IS_ALIGNED(Packet->InDataBuffer, This-= >Mode->IoAlign)) { + if ((This->Mode->IoAlign > 1) && !ADDRESS_IS_ALIGNED(Packet->InDataBuffe= r, This->Mode->IoAlign)) { return EFI_INVALID_PARAMETER; } =20 - if ((This->Mode->IoAlign > 1) && !IS_ALIGNED(Packet->OutDataBuffer, This= ->Mode->IoAlign)) { + if ((This->Mode->IoAlign > 1) && !ADDRESS_IS_ALIGNED(Packet->OutDataBuff= er, This->Mode->IoAlign)) { return EFI_INVALID_PARAMETER; } =20 - if ((This->Mode->IoAlign > 1) && !IS_ALIGNED(Packet->Asb, This->Mode->Io= Align)) { + if ((This->Mode->IoAlign > 1) && !ADDRESS_IS_ALIGNED(Packet->Asb, This->= Mode->IoAlign)) { return EFI_INVALID_PARAMETER; } =20 @@ -2012,15 +2012,15 @@ ExtScsiPassThruPassThru ( return EFI_INVALID_PARAMETER; } =20 - if ((This->Mode->IoAlign > 1) && !IS_ALIGNED(Packet->InDataBuffer, This-= >Mode->IoAlign)) { + if ((This->Mode->IoAlign > 1) && !ADDRESS_IS_ALIGNED(Packet->InDataBuffe= r, This->Mode->IoAlign)) { return EFI_INVALID_PARAMETER; } =20 - if ((This->Mode->IoAlign > 1) && !IS_ALIGNED(Packet->OutDataBuffer, This= ->Mode->IoAlign)) { + if ((This->Mode->IoAlign > 1) && !ADDRESS_IS_ALIGNED(Packet->OutDataBuff= er, This->Mode->IoAlign)) { return EFI_INVALID_PARAMETER; } =20 - if ((This->Mode->IoAlign > 1) && !IS_ALIGNED(Packet->SenseData, This->Mo= de->IoAlign)) { + if ((This->Mode->IoAlign > 1) && !ADDRESS_IS_ALIGNED(Packet->SenseData, = This->Mode->IoAlign)) { return EFI_INVALID_PARAMETER; } =20 diff --git a/MdeModulePkg/Bus/Ata/AtaBusDxe/AtaPassThruExecute.c b/MdeModul= ePkg/Bus/Ata/AtaBusDxe/AtaPassThruExecute.c index 79026a4a957d..eabab8ac5bc5 100644 --- a/MdeModulePkg/Bus/Ata/AtaBusDxe/AtaPassThruExecute.c +++ b/MdeModulePkg/Bus/Ata/AtaBusDxe/AtaPassThruExecute.c @@ -1036,7 +1036,7 @@ TrustTransferAtaDevice ( // Check the alignment of the incoming buffer prior to invoking underl= ying ATA PassThru // AtaPassThru =3D AtaDevice->AtaBusDriverData->AtaPassThru; - if ((AtaPassThru->Mode->IoAlign > 1) && !IS_ALIGNED (Buffer, AtaPassTh= ru->Mode->IoAlign)) { + if ((AtaPassThru->Mode->IoAlign > 1) && !ADDRESS_IS_ALIGNED (Buffer, A= taPassThru->Mode->IoAlign)) { NewBuffer =3D AllocateAlignedBuffer (AtaDevice, TransferLength); if (NewBuffer =3D=3D NULL) { return EFI_OUT_OF_RESOURCES; diff --git a/MdeModulePkg/Bus/Scsi/ScsiDiskDxe/ScsiDisk.c b/MdeModulePkg/Bu= s/Scsi/ScsiDiskDxe/ScsiDisk.c index c80e78fa8a6b..81db2efd0599 100644 --- a/MdeModulePkg/Bus/Scsi/ScsiDiskDxe/ScsiDisk.c +++ b/MdeModulePkg/Bus/Scsi/ScsiDiskDxe/ScsiDisk.c @@ -1956,7 +1956,7 @@ ScsiDiskReceiveData ( goto Done; } =20 - if ((ScsiDiskDevice->ScsiIo->IoAlign > 1) && !IS_ALIGNED (PayloadBuffe= r, ScsiDiskDevice->ScsiIo->IoAlign)) { + if ((ScsiDiskDevice->ScsiIo->IoAlign > 1) && !ADDRESS_IS_ALIGNED (Payl= oadBuffer, ScsiDiskDevice->ScsiIo->IoAlign)) { AlignedBuffer =3D AllocateAlignedBuffer (ScsiDiskDevice, PayloadBuff= erSize); if (AlignedBuffer =3D=3D NULL) { Status =3D EFI_OUT_OF_RESOURCES; @@ -2171,7 +2171,7 @@ ScsiDiskSendData ( goto Done; } =20 - if ((ScsiDiskDevice->ScsiIo->IoAlign > 1) && !IS_ALIGNED (PayloadBuffe= r, ScsiDiskDevice->ScsiIo->IoAlign)) { + if ((ScsiDiskDevice->ScsiIo->IoAlign > 1) && !ADDRESS_IS_ALIGNED (Payl= oadBuffer, ScsiDiskDevice->ScsiIo->IoAlign)) { AlignedBuffer =3D AllocateAlignedBuffer (ScsiDiskDevice, PayloadBuff= erSize); if (AlignedBuffer =3D=3D NULL) { Status =3D EFI_OUT_OF_RESOURCES; diff --git a/MdeModulePkg/Bus/Ufs/UfsPassThruDxe/UfsPassThru.c b/MdeModuleP= kg/Bus/Ufs/UfsPassThruDxe/UfsPassThru.c index 92ff958f161e..c4d01a20fcbe 100644 --- a/MdeModulePkg/Bus/Ufs/UfsPassThruDxe/UfsPassThru.c +++ b/MdeModulePkg/Bus/Ufs/UfsPassThruDxe/UfsPassThru.c @@ -170,15 +170,15 @@ UfsPassThruPassThru ( return EFI_INVALID_PARAMETER; } =20 - if ((This->Mode->IoAlign > 1) && !IS_ALIGNED(Packet->InDataBuffer, This-= >Mode->IoAlign)) { + if ((This->Mode->IoAlign > 1) && !ADDRESS_IS_ALIGNED(Packet->InDataBuffe= r, This->Mode->IoAlign)) { return EFI_INVALID_PARAMETER; } =20 - if ((This->Mode->IoAlign > 1) && !IS_ALIGNED(Packet->OutDataBuffer, This= ->Mode->IoAlign)) { + if ((This->Mode->IoAlign > 1) && !ADDRESS_IS_ALIGNED(Packet->OutDataBuff= er, This->Mode->IoAlign)) { return EFI_INVALID_PARAMETER; } =20 - if ((This->Mode->IoAlign > 1) && !IS_ALIGNED(Packet->SenseData, This->Mo= de->IoAlign)) { + if ((This->Mode->IoAlign > 1) && !ADDRESS_IS_ALIGNED(Packet->SenseData, = This->Mode->IoAlign)) { return EFI_INVALID_PARAMETER; } =20 diff --git a/MdeModulePkg/Universal/EbcDxe/EbcExecute.c b/MdeModulePkg/Univ= ersal/EbcDxe/EbcExecute.c index 1c4a4f5155c9..ba66f441bcea 100644 --- a/MdeModulePkg/Universal/EbcDxe/EbcExecute.c +++ b/MdeModulePkg/Universal/EbcDxe/EbcExecute.c @@ -2004,7 +2004,7 @@ ExecuteJMP ( // check for alignment, and jump absolute. // Data64 =3D (UINT64) VmReadImmed64 (VmPtr, 2); - if (!IS_ALIGNED ((UINTN) Data64, sizeof (UINT16))) { + if (!ADDRESS_IS_ALIGNED ((UINTN) Data64, sizeof (UINT16))) { EbcDebugSignalException ( EXCEPT_EBC_ALIGNMENT_CHECK, EXCEPTION_FLAG_FATAL, @@ -2059,7 +2059,7 @@ ExecuteJMP ( // Form: JMP32 @Rx {Index32} // Addr =3D VmReadMemN (VmPtr, (UINTN) Data64 + Index32); - if (!IS_ALIGNED ((UINTN) Addr, sizeof (UINT16))) { + if (!ADDRESS_IS_ALIGNED ((UINTN) Addr, sizeof (UINT16))) { EbcDebugSignalException ( EXCEPT_EBC_ALIGNMENT_CHECK, EXCEPTION_FLAG_FATAL, @@ -2082,7 +2082,7 @@ ExecuteJMP ( // Form: JMP32 Rx {Immed32} // Addr =3D (UINTN) (Data64 + Index32); - if (!IS_ALIGNED ((UINTN) Addr, sizeof (UINT16))) { + if (!ADDRESS_IS_ALIGNED ((UINTN) Addr, sizeof (UINT16))) { EbcDebugSignalException ( EXCEPT_EBC_ALIGNMENT_CHECK, EXCEPTION_FLAG_FATAL, @@ -3128,7 +3128,7 @@ ExecuteRET ( // Pull the return address off the VM app's stack and set the IP // to it // - if (!IS_ALIGNED ((UINTN) VmPtr->Gpr[0], sizeof (UINT16))) { + if (!ADDRESS_IS_ALIGNED ((UINTN) VmPtr->Gpr[0], sizeof (UINT16))) { EbcDebugSignalException ( EXCEPT_EBC_ALIGNMENT_CHECK, EXCEPTION_FLAG_FATAL, @@ -4693,7 +4693,7 @@ VmWriteMem16 ( // // Do a simple write if aligned // - if (IS_ALIGNED (Addr, sizeof (UINT16))) { + if (ADDRESS_IS_ALIGNED (Addr, sizeof (UINT16))) { *(UINT16 *) Addr =3D Data; } else { // @@ -4756,7 +4756,7 @@ VmWriteMem32 ( // // Do a simple write if aligned // - if (IS_ALIGNED (Addr, sizeof (UINT32))) { + if (ADDRESS_IS_ALIGNED (Addr, sizeof (UINT32))) { *(UINT32 *) Addr =3D Data; } else { // @@ -4819,7 +4819,7 @@ VmWriteMem64 ( // // Do a simple write if aligned // - if (IS_ALIGNED (Addr, sizeof (UINT64))) { + if (ADDRESS_IS_ALIGNED (Addr, sizeof (UINT64))) { *(UINT64 *) Addr =3D Data; } else { // @@ -4885,7 +4885,7 @@ VmWriteMemN ( // // Do a simple write if aligned // - if (IS_ALIGNED (Addr, sizeof (UINTN))) { + if (ADDRESS_IS_ALIGNED (Addr, sizeof (UINTN))) { *(UINTN *) Addr =3D Data; } else { for (Index =3D 0; Index < sizeof (UINTN) / sizeof (UINT32); Index++) { @@ -4949,7 +4949,7 @@ VmReadImmed16 ( // // Read direct if aligned // - if (IS_ALIGNED ((UINTN) VmPtr->Ip + Offset, sizeof (INT16))) { + if (ADDRESS_IS_ALIGNED ((UINTN) VmPtr->Ip + Offset, sizeof (INT16))) { return * (INT16 *) (VmPtr->Ip + Offset); } else { // @@ -4993,7 +4993,7 @@ VmReadImmed32 ( // // Read direct if aligned // - if (IS_ALIGNED ((UINTN) VmPtr->Ip + Offset, sizeof (UINT32))) { + if (ADDRESS_IS_ALIGNED ((UINTN) VmPtr->Ip + Offset, sizeof (UINT32))) { return * (INT32 *) (VmPtr->Ip + Offset); } // @@ -5032,7 +5032,7 @@ VmReadImmed64 ( // // Read direct if aligned // - if (IS_ALIGNED ((UINTN) VmPtr->Ip + Offset, sizeof (UINT64))) { + if (ADDRESS_IS_ALIGNED ((UINTN) VmPtr->Ip + Offset, sizeof (UINT64))) { return * (UINT64 *) (VmPtr->Ip + Offset); } // @@ -5069,7 +5069,7 @@ VmReadCode16 ( // // Read direct if aligned // - if (IS_ALIGNED ((UINTN) VmPtr->Ip + Offset, sizeof (UINT16))) { + if (ADDRESS_IS_ALIGNED ((UINTN) VmPtr->Ip + Offset, sizeof (UINT16))) { return * (UINT16 *) (VmPtr->Ip + Offset); } else { // @@ -5110,7 +5110,7 @@ VmReadCode32 ( // // Read direct if aligned // - if (IS_ALIGNED ((UINTN) VmPtr->Ip + Offset, sizeof (UINT32))) { + if (ADDRESS_IS_ALIGNED ((UINTN) VmPtr->Ip + Offset, sizeof (UINT32))) { return * (UINT32 *) (VmPtr->Ip + Offset); } // @@ -5147,7 +5147,7 @@ VmReadCode64 ( // // Read direct if aligned // - if (IS_ALIGNED ((UINTN) VmPtr->Ip + Offset, sizeof (UINT64))) { + if (ADDRESS_IS_ALIGNED ((UINTN) VmPtr->Ip + Offset, sizeof (UINT64))) { return * (UINT64 *) (VmPtr->Ip + Offset); } // @@ -5210,7 +5210,7 @@ VmReadMem16 ( // // Read direct if aligned // - if (IS_ALIGNED (Addr, sizeof (UINT16))) { + if (ADDRESS_IS_ALIGNED (Addr, sizeof (UINT16))) { return * (UINT16 *) Addr; } // @@ -5243,7 +5243,7 @@ VmReadMem32 ( // // Read direct if aligned // - if (IS_ALIGNED (Addr, sizeof (UINT32))) { + if (ADDRESS_IS_ALIGNED (Addr, sizeof (UINT32))) { return * (UINT32 *) Addr; } // @@ -5280,7 +5280,7 @@ VmReadMem64 ( // // Read direct if aligned // - if (IS_ALIGNED (Addr, sizeof (UINT64))) { + if (ADDRESS_IS_ALIGNED (Addr, sizeof (UINT64))) { return * (UINT64 *) Addr; } // @@ -5349,7 +5349,7 @@ VmReadMemN ( // // Read direct if aligned // - if (IS_ALIGNED (Addr, sizeof (UINTN))) { + if (ADDRESS_IS_ALIGNED (Addr, sizeof (UINTN))) { return * (UINTN *) Addr; } // diff --git a/MdeModulePkg/Bus/Ata/AhciPei/AhciPei.h b/MdeModulePkg/Bus/Ata/= AhciPei/AhciPei.h index 2be78076bee7..59bb9e5d0bca 100644 --- a/MdeModulePkg/Bus/Ata/AhciPei/AhciPei.h +++ b/MdeModulePkg/Bus/Ata/AhciPei/AhciPei.h @@ -145,7 +145,6 @@ typedef union { #define AHCI_PORT_SERR 0x0030 #define AHCI_PORT_CI 0x0038 =20 -#define IS_ALIGNED(addr, size) (((UINTN) (addr) & (size - = 1)) =3D=3D 0) #define TIMER_PERIOD_SECONDS(Seconds) MultU64x32((UINT64)(Seconds= ), 10000000) =20 #pragma pack(1) diff --git a/MdeModulePkg/Bus/Ata/AtaAtapiPassThru/AtaAtapiPassThru.h b/Mde= ModulePkg/Bus/Ata/AtaAtapiPassThru/AtaAtapiPassThru.h index 5f582b9b3e76..99bbf7d14a17 100644 --- a/MdeModulePkg/Bus/Ata/AtaAtapiPassThru/AtaAtapiPassThru.h +++ b/MdeModulePkg/Bus/Ata/AtaAtapiPassThru/AtaAtapiPassThru.h @@ -147,8 +147,6 @@ struct _ATA_NONBLOCK_TASK { #define ATA_ATAPI_TIMEOUT EFI_TIMER_PERIOD_SECONDS(3) #define ATA_SPINUP_TIMEOUT EFI_TIMER_PERIOD_SECONDS(10) =20 -#define IS_ALIGNED(addr, size) (((UINTN) (addr) & (size - 1)) =3D=3D = 0) - #define ATA_PASS_THRU_PRIVATE_DATA_FROM_THIS(a) \ CR (a, \ ATA_ATAPI_PASS_THRU_INSTANCE, \ diff --git a/MdeModulePkg/Bus/Ata/AtaBusDxe/AtaBus.h b/MdeModulePkg/Bus/Ata= /AtaBusDxe/AtaBus.h index a5a865209942..172d2d61ea6c 100644 --- a/MdeModulePkg/Bus/Ata/AtaBusDxe/AtaBus.h +++ b/MdeModulePkg/Bus/Ata/AtaBusDxe/AtaBus.h @@ -76,7 +76,6 @@ #define ATA_TASK_SIGNATURE SIGNATURE_32 ('A', 'T', 'S', 'K') #define ATA_DEVICE_SIGNATURE SIGNATURE_32 ('A', 'B', 'I', 'D') #define ATA_SUB_TASK_SIGNATURE SIGNATURE_32 ('A', 'S', 'T', 'S') -#define IS_ALIGNED(addr, size) (((UINTN) (addr) & (size - 1)) = =3D=3D 0) =20 // // ATA bus data structure for ATA controller diff --git a/MdeModulePkg/Bus/Scsi/ScsiDiskDxe/ScsiDisk.h b/MdeModulePkg/Bu= s/Scsi/ScsiDiskDxe/ScsiDisk.h index ed9bbd6f8ba8..86ad27b3292f 100644 --- a/MdeModulePkg/Bus/Scsi/ScsiDiskDxe/ScsiDisk.h +++ b/MdeModulePkg/Bus/Scsi/ScsiDiskDxe/ScsiDisk.h @@ -37,9 +37,7 @@ SPDX-License-Identifier: BSD-2-Clause-Patent #include #include =20 -#define IS_DEVICE_FIXED(a) (a)->FixedDevice ? 1 : 0 - -#define IS_ALIGNED(addr, size) (((UINTN) (addr) & (size - 1)) =3D=3D 0) +#define IS_DEVICE_FIXED(a) (a)->FixedDevice ? 1 : 0 =20 #define UFS_WLUN_RPMB 0xC4 =20 diff --git a/MdeModulePkg/Bus/Ufs/UfsBlockIoPei/UfsBlockIoPei.h b/MdeModule= Pkg/Bus/Ufs/UfsBlockIoPei/UfsBlockIoPei.h index 6e2305aa2bc2..7306106a4454 100644 --- a/MdeModulePkg/Bus/Ufs/UfsBlockIoPei/UfsBlockIoPei.h +++ b/MdeModulePkg/Bus/Ufs/UfsBlockIoPei/UfsBlockIoPei.h @@ -133,8 +133,6 @@ typedef struct _UFS_PEIM_HC_PRIVATE_DATA { =20 #define ROUNDUP8(x) (((x) % 8 =3D=3D 0) ? (x) : ((x) / 8 + 1) * 8) =20 -#define IS_ALIGNED(addr, size) (((UINTN) (addr) & (size - 1)) =3D=3D = 0) - #define GET_UFS_PEIM_HC_PRIVATE_DATA_FROM_THIS(a) CR (a, UFS_PEIM_HC_PRIVA= TE_DATA, BlkIoPpi, UFS_PEIM_HC_SIG) #define GET_UFS_PEIM_HC_PRIVATE_DATA_FROM_THIS2(a) CR (a, UFS_PEIM_HC_PRIV= ATE_DATA, BlkIo2Ppi, UFS_PEIM_HC_SIG) #define GET_UFS_PEIM_HC_PRIVATE_DATA_FROM_THIS_NOTIFY(a) CR (a, UFS_PEIM_H= C_PRIVATE_DATA, EndOfPeiNotifyList, UFS_PEIM_HC_SIG) diff --git a/MdeModulePkg/Bus/Ufs/UfsPassThruDxe/UfsPassThru.h b/MdeModuleP= kg/Bus/Ufs/UfsPassThruDxe/UfsPassThru.h index 79b86f7e6b3d..11b5b197b67a 100644 --- a/MdeModulePkg/Bus/Ufs/UfsPassThruDxe/UfsPassThru.h +++ b/MdeModulePkg/Bus/Ufs/UfsPassThruDxe/UfsPassThru.h @@ -105,8 +105,6 @@ typedef struct { =20 #define ROUNDUP8(x) (((x) % 8 =3D=3D 0) ? (x) : ((x) / 8 + 1) * 8) =20 -#define IS_ALIGNED(addr, size) (((UINTN) (addr) & (size - 1)) =3D= =3D 0) - #define UFS_PASS_THRU_PRIVATE_DATA_FROM_THIS(a) \ CR (a, \ UFS_PASS_THRU_PRIVATE_DATA, \ diff --git a/MdeModulePkg/Universal/EbcDxe/EbcExecute.h b/MdeModulePkg/Univ= ersal/EbcDxe/EbcExecute.h index 1cb68bc5385a..858ca7fc86ae 100644 --- a/MdeModulePkg/Universal/EbcDxe/EbcExecute.h +++ b/MdeModulePkg/Universal/EbcDxe/EbcExecute.h @@ -14,8 +14,7 @@ SPDX-License-Identifier: BSD-2-Clause-Patent // // Macros to check and set alignment // -#define ASSERT_ALIGNED(addr, size) ASSERT (!((UINT32) (addr) & (size - 1)= )) -#define IS_ALIGNED(addr, size) !((UINT32) (addr) & (size - 1)) +#define ASSERT_ALIGNED(addr, size) ASSERT (IS_ALIGNED (addr, size)) =20 // // Debug macro diff --git a/MdePkg/Include/Base.h b/MdePkg/Include/Base.h index 2da08b0c787f..32d0e512e05f 100644 --- a/MdePkg/Include/Base.h +++ b/MdePkg/Include/Base.h @@ -789,6 +789,35 @@ typedef UINTN *BASE_LIST; #define OFFSET_OF(TYPE, Field) ((UINTN) &(((TYPE *)0)->Field)) #endif =20 +/** + Returns the alignment requirement of a type. + + @param TYPE The name of the type to retrieve the alignment requiremen= t of. + + @return Alignment requirement, in Bytes, of TYPE. +**/ +#if defined(__GNUC__) || defined(__clang__) || (defined(_MSC_VER) && _MSC_= VER >=3D 1900) + // + // All supported versions of GCC and Clang, as well as MSVC 2015 and lat= er, + // support the standard operator _Alignof. + // + #define ALIGNOF(TYPE) _Alignof (TYPE) +#elif defined(_MSC_VER) + // + // Earlier versions of MSVC, at least MSVC 2008 and later, support the + // vendor-extension __alignof. + // + #define ALIGNOF(TYPE) __alignof (TYPE) +#else + // + // For compilers that do not support inbuilt alignof operators, use OFFS= ET_OF. + // CHAR8 is known to have both a size and an alignment requirement of 1 = Byte. + // As such, A must be located exactly at the offset equal to its alignme= nt + // requirement. + // + #define ALIGNOF(TYPE) OFFSET_OF (struct { CHAR8 C; TYPE A; }, A) +#endif + /** Portable definition for compile time assertions. Equivalent to C11 static_assert macro from assert.h. @@ -824,6 +853,21 @@ STATIC_ASSERT (sizeof (CHAR16) =3D=3D 2, "sizeof (CHA= R16) does not meet UEFI Specif STATIC_ASSERT (sizeof (L'A') =3D=3D 2, "sizeof (L'A') does not meet UEF= I Specification Data Type requirements"); STATIC_ASSERT (sizeof (L"A") =3D=3D 4, "sizeof (L\"A\") does not meet U= EFI Specification Data Type requirements"); =20 +STATIC_ASSERT (ALIGNOF (BOOLEAN) =3D=3D sizeof (BOOLEAN), "Alignment of BO= OLEAN does not meet UEFI Specification Data Type requirements"); +STATIC_ASSERT (ALIGNOF (INT8) =3D=3D sizeof (INT8), "Alignment of IN= T8 does not meet UEFI Specification Data Type requirements"); +STATIC_ASSERT (ALIGNOF (UINT8) =3D=3D sizeof (UINT8), "Alignment of IN= T16 does not meet UEFI Specification Data Type requirements"); +STATIC_ASSERT (ALIGNOF (INT16) =3D=3D sizeof (INT16), "Alignment of IN= T16 does not meet UEFI Specification Data Type requirements"); +STATIC_ASSERT (ALIGNOF (UINT16) =3D=3D sizeof (UINT16), "Alignment of UI= NT16 does not meet UEFI Specification Data Type requirements"); +STATIC_ASSERT (ALIGNOF (INT32) =3D=3D sizeof (INT32), "Alignment of IN= T32 does not meet UEFI Specification Data Type requirements"); +STATIC_ASSERT (ALIGNOF (UINT32) =3D=3D sizeof (UINT32), "Alignment of UI= NT32 does not meet UEFI Specification Data Type requirements"); +STATIC_ASSERT (ALIGNOF (INT64) =3D=3D sizeof (INT64), "Alignment of IN= T64 does not meet UEFI Specification Data Type requirements"); +STATIC_ASSERT (ALIGNOF (UINT64) =3D=3D sizeof (UINT64), "Alignment of UI= NT64 does not meet UEFI Specification Data Type requirements"); +STATIC_ASSERT (ALIGNOF (CHAR8) =3D=3D sizeof (CHAR8), "Alignment of CH= AR8 does not meet UEFI Specification Data Type requirements"); +STATIC_ASSERT (ALIGNOF (CHAR16) =3D=3D sizeof (CHAR16), "Alignment of CH= AR16 does not meet UEFI Specification Data Type requirements"); +STATIC_ASSERT (ALIGNOF (INTN) =3D=3D sizeof (INTN), "Alignment of IN= TN does not meet UEFI Specification Data Type requirements"); +STATIC_ASSERT (ALIGNOF (UINTN) =3D=3D sizeof (UINTN), "Alignment of UI= NTN does not meet UEFI Specification Data Type requirements"); +STATIC_ASSERT (ALIGNOF (VOID *) =3D=3D sizeof (VOID *), "Alignment of VO= ID * does not meet UEFI Specification Data Type requirements"); + // // The following three enum types are used to verify that the compiler // configuration for enum types is compliant with Section 2.3.1 of the @@ -847,6 +891,10 @@ STATIC_ASSERT (sizeof (__VERIFY_UINT8_ENUM_SIZE) =3D= =3D 4, "Size of enum does not me STATIC_ASSERT (sizeof (__VERIFY_UINT16_ENUM_SIZE) =3D=3D 4, "Size of enum = does not meet UEFI Specification Data Type requirements"); STATIC_ASSERT (sizeof (__VERIFY_UINT32_ENUM_SIZE) =3D=3D 4, "Size of enum = does not meet UEFI Specification Data Type requirements"); =20 +STATIC_ASSERT (ALIGNOF (__VERIFY_UINT8_ENUM_SIZE) =3D=3D sizeof (__VERIFY= _UINT8_ENUM_SIZE), "Alignment of enum does not meet UEFI Specification Dat= a Type requirements"); +STATIC_ASSERT (ALIGNOF (__VERIFY_UINT16_ENUM_SIZE) =3D=3D sizeof (__VERIFY= _UINT16_ENUM_SIZE), "Alignment of enum does not meet UEFI Specification Dat= a Type requirements"); +STATIC_ASSERT (ALIGNOF (__VERIFY_UINT32_ENUM_SIZE) =3D=3D sizeof (__VERIFY= _UINT32_ENUM_SIZE), "Alignment of enum does not meet UEFI Specification Dat= a Type requirements"); + /** Macro that returns a pointer to the data structure that contains a speci= fied field of that data structure. This is a lightweight method to hide information b= y placing a @@ -868,6 +916,46 @@ STATIC_ASSERT (sizeof (__VERIFY_UINT32_ENUM_SIZE) =3D= =3D 4, "Size of enum does not m **/ #define BASE_CR(Record, TYPE, Field) ((TYPE *) ((CHAR8 *) (Record) - OFFS= ET_OF (TYPE, Field))) =20 +/** + Checks whether a value is a power of two. + + @param Value The value to check. + + @return Whether Value is a power of two. +**/ +#define IS_POW2(Value) ((Value) !=3D 0U && ((Value) & ((Value) - 1U)) =3D= =3D 0U) + +/** + Checks whether a value is aligned by a specified alignment. + + @param Value The value to check. + @param Alignment The alignment boundary used to check against. + + @return Whether Value is aligned by Alignment. +**/ +#define IS_ALIGNED(Value, Alignment) (((Value) & ((Alignment) - 1U)) =3D= =3D 0U) + +/** + Checks whether a pointer or address is aligned by a specified alignment. + + @param Address The pointer or address to check. + @param Alignment The alignment boundary used to check against. + + @return Whether Address is aligned by Alignment. +**/ +#define ADDRESS_IS_ALIGNED(Address, Alignment) IS_ALIGNED ((UINTN) (Addre= ss), Alignment) + +/** + Determines the addend to add to a value to round it up to the next bound= ary of + a specified alignment. + + @param Value The value to round up. + @param Alignment The alignment boundary used to return the addend. + + @return Addend to round Value up to alignment boundary Alignment. +**/ +#define ALIGN_VALUE_ADDEND(Value, Alignment) (((Alignment) - (Value)) & (= (Alignment) - 1U)) + /** Rounds a value up to the next boundary using a specified alignment. =20 @@ -880,7 +968,7 @@ STATIC_ASSERT (sizeof (__VERIFY_UINT32_ENUM_SIZE) =3D= =3D 4, "Size of enum does not m @return A value up to the next boundary. =20 **/ -#define ALIGN_VALUE(Value, Alignment) ((Value) + (((Alignment) - (Value)) = & ((Alignment) - 1))) +#define ALIGN_VALUE(Value, Alignment) ((Value) + ALIGN_VALUE_ADDEND (Value= , Alignment)) =20 /** Adjust a pointer by adding the minimum offset required for it to be alig= ned on --=20 2.31.1 -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D- Groups.io Links: You receive all messages sent to this group. View/Reply Online (#78887): https://edk2.groups.io/g/devel/message/78887 Mute This Topic: https://groups.io/mt/84754061/1787277 Group Owner: devel+owner@edk2.groups.io Unsubscribe: https://edk2.groups.io/g/devel/unsub [importer@patchew.org] -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-