From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mout01.posteo.de (mout01.posteo.de [185.67.36.65]) by mx.groups.io with SMTP id smtpd.web11.34191.1628844091344205651 for ; Fri, 13 Aug 2021 01:41:32 -0700 Authentication-Results: mx.groups.io; dkim=fail reason="body hash did not verify" header.i=@posteo.de header.s=2017 header.b=BlxaTltJ; spf=pass (domain: posteo.de, ip: 185.67.36.65, mailfrom: mhaeuser@posteo.de) Received: from submission (posteo.de [89.146.220.130]) by mout01.posteo.de (Postfix) with ESMTPS id AFA3E240029 for ; Fri, 13 Aug 2021 10:41:29 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=posteo.de; s=2017; t=1628844089; bh=idqc419ibbuSRLDnVOmVgF+osw8bK9YrCZNYKuBhSU8=; h=Subject:To:Cc:From:Date:From; b=BlxaTltJNAvrWcKJQZxBm1sFxwnLRQ+yFxw27DSvVcNM4EcBVigHtaP+qaWKTb1+d BSTdfCGRt+RRPjsB3ECRdOBKomtmyMvqlctYCGfeLz8tYskyiQ/T1odv7jXfhVpzkC 1QgjZlTrzE2hcHZ4B0vjSBX4whAR5zjFGVAwdUKmeINuLAxeFZqWrRQPCdDwYDmh8g tv+sVAl4R2f5pjP5zVSrb134G4tbMYwEAw5JbuLd2PRbk8SGTjXoI3COrisxHeelsq JoBqy0YdqNPZ/Z7FIRVqarD12nVtqJinIcG/DT1WWfO+QoptSdit28HWu9lP8dcMOe AjKUYuQxL475w== Received: from customer (localhost [127.0.0.1]) by submission (posteo.de) with ESMTPSA id 4GmH8l2rwHz9rxH; Fri, 13 Aug 2021 10:41:27 +0200 (CEST) Subject: Re: [edk2-devel] [PATCH] MdePkg/Base.h: Introduce various alignment-related macros To: devel@edk2.groups.io, hao.a.wu@intel.com Cc: "Kinney, Michael D" , Liming Gao , "Liu, Zhiguang" , "Wang, Jian J" , "Ni, Ray" , Vitaly Cheptsov References: <5df11a13422732b9c03c120775a2b4dd0a49182f.1628444003.git.mhaeuser@posteo.de> From: =?UTF-8?B?TWFydmluIEjDpHVzZXI=?= Message-ID: <22aacb64-00e3-2c4a-63de-7d7e74179349@posteo.de> Date: Fri, 13 Aug 2021 08:41:27 +0000 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-GB Content-Transfer-Encoding: quoted-printable On 13/08/2021 09:27, Wu, Hao A wrote: >> -----Original Message----- >> From: Marvin H=C3=A4user >> Sent: Monday, August 9, 2021 3:40 AM >> To: devel@edk2.groups.io >> Cc: Kinney, Michael D ; Liming Gao >> ; Liu, Zhiguang ; >> Wang, Jian J ; Wu, Hao A ; Ni= , >> Ray ; Vitaly Cheptsov >> Subject: [PATCH] MdePkg/Base.h: Introduce various alignment-related >> macros >> >> 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 >> --- >> 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(-) > > Suggest to break this into 2 patches: > The first one is for MdePkg for adding the macros. And the second one for= changes in MdeModulePkg. > > For the MdeModulePkg changes, they look good to me: > Reviewed-by: Hao A Wu > > You can add the above 'R-b' tag for your MdeModulePkg patch in the V2 ser= ies. Thank you! The reason I didn't do this is that MdeModulePkg already has=20 "IS_ALIGNED" macros, so basically it would need three patches: 1) Rename MdeModulePkg "IS_ALIGNED" to something else 2) Introduce MdePkg "IS_ALIGNED" 3) Drop newly renamed macros from 1) to use the macros from 2) if you prefer it that way, I'll have a V2 ready soon. Best regards, Marvin > > Best Regards, > Hao Wu > > >> 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_SIZ= E_TO_PAGES >> (TransferLength), AtaPassThru->Mode->IoAligndiff --g= it >> 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 ( >> } IoAlign =3D This->Mode->IoAlign;- if ((IoAlign > 1) && !IS_ALI= GNED >> (Packet->InDataBuffer, IoAlign)) {+ if ((IoAlign > 1) >> && !ADDRESS_IS_ALIGNED (Packet->InDataBuffer, IoAlign)) { return >> EFI_INVALID_PARAMETER; } - if ((IoAlign > 1) && !IS_ALIGNED (Packet- >>> OutDataBuffer, IoAlign)) {+ if ((IoAlign > 1) && !ADDRESS_IS_ALIGNED >> (Packet->OutDataBuffer, IoAlign)) { return EFI_INVALID_PARAMETER; = } - >> if ((IoAlign > 1) && !IS_ALIGNED (Packet->Asb, IoAlign)) {+ if ((IoAlig= n > 1) >> && !ADDRESS_IS_ALIGNED (Packet->Asb, IoAlign)) { return >> EFI_INVALID_PARAMETER; } diff --git >> a/MdeModulePkg/Bus/Ata/AtaAtapiPassThru/AtaAtapiPassThru.c >> b/MdeModulePkg/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 ( >> Instance =3D ATA_PASS_THRU_PRIVATE_DATA_FROM_THIS (This); - if >> ((This->Mode->IoAlign > 1) && !IS_ALIGNED(Packet->InDataBuffer, This- >>> Mode->IoAlign)) {+ if ((This->Mode->IoAlign > 1) >> && !ADDRESS_IS_ALIGNED(Packet->InDataBuffer, This->Mode->IoAlign)) >> { return EFI_INVALID_PARAMETER; } - if ((This->Mode->IoAlign > 1) >> && !IS_ALIGNED(Packet->OutDataBuffer, This->Mode->IoAlign)) {+ if ((Thi= s- >>> Mode->IoAlign > 1) && !ADDRESS_IS_ALIGNED(Packet->OutDataBuffer, >> This->Mode->IoAlign)) { return EFI_INVALID_PARAMETER; } - if ((Th= is- >>> Mode->IoAlign > 1) && !IS_ALIGNED(Packet->Asb, This->Mode->IoAlign)) >> {+ if ((This->Mode->IoAlign > 1) && !ADDRESS_IS_ALIGNED(Packet->Asb, >> This->Mode->IoAlign)) { return EFI_INVALID_PARAMETER; } @@ -2012,1= 5 >> +2012,15 @@ ExtScsiPassThruPassThru ( >> return EFI_INVALID_PARAMETER; } - if ((This->Mode->IoAlign > 1) >> && !IS_ALIGNED(Packet->InDataBuffer, This->Mode->IoAlign)) {+ if ((This= - >>> Mode->IoAlign > 1) && !ADDRESS_IS_ALIGNED(Packet->InDataBuffer, This- >>> Mode->IoAlign)) { return EFI_INVALID_PARAMETER; } - if ((This->M= ode- >>> IoAlign > 1) && !IS_ALIGNED(Packet->OutDataBuffer, This->Mode- >>> IoAlign)) {+ if ((This->Mode->IoAlign > 1) >> && !ADDRESS_IS_ALIGNED(Packet->OutDataBuffer, This->Mode->IoAlign)) >> { return EFI_INVALID_PARAMETER; } - if ((This->Mode->IoAlign > 1) >> && !IS_ALIGNED(Packet->SenseData, This->Mode->IoAlign)) {+ if ((This- >>> Mode->IoAlign > 1) && !ADDRESS_IS_ALIGNED(Packet->SenseData, This- >>> Mode->IoAlign)) { return EFI_INVALID_PARAMETER; } diff --git >> a/MdeModulePkg/Bus/Ata/AtaBusDxe/AtaPassThruExecute.c >> b/MdeModulePkg/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 un= derlying >> ATA PassThru // AtaPassThru =3D AtaDevice->AtaBusDriverData- >>> AtaPassThru;- if ((AtaPassThru->Mode->IoAlign > 1) && !IS_ALIGNED >> (Buffer, AtaPassThru->Mode->IoAlign)) {+ if ((AtaPassThru->Mode- >>> IoAlign > 1) && !ADDRESS_IS_ALIGNED (Buffer, AtaPassThru->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/Bus/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; } - if ((ScsiDiskDevice->ScsiIo->IoAlign > 1) >> && !IS_ALIGNED (PayloadBuffer, ScsiDiskDevice->ScsiIo->IoAlign)) {+ i= f >> ((ScsiDiskDevice->ScsiIo->IoAlign > 1) && !ADDRESS_IS_ALIGNED >> (PayloadBuffer, ScsiDiskDevice->ScsiIo->IoAlign)) { AlignedBuffer = =3D >> AllocateAlignedBuffer (ScsiDiskDevice, PayloadBufferSize); if >> (AlignedBuffer =3D=3D NULL) { Status =3D EFI_OUT_OF_RESOURCES;@@= - >> 2171,7 +2171,7 @@ ScsiDiskSendData ( >> goto Done; } - if ((ScsiDiskDevice->ScsiIo->IoAlign > 1) >> && !IS_ALIGNED (PayloadBuffer, ScsiDiskDevice->ScsiIo->IoAlign)) {+ i= f >> ((ScsiDiskDevice->ScsiIo->IoAlign > 1) && !ADDRESS_IS_ALIGNED >> (PayloadBuffer, ScsiDiskDevice->ScsiIo->IoAlign)) { AlignedBuffer = =3D >> AllocateAlignedBuffer (ScsiDiskDevice, PayloadBufferSize); if >> (AlignedBuffer =3D=3D NULL) { Status =3D EFI_OUT_OF_RESOURCES;di= ff --git >> a/MdeModulePkg/Bus/Ufs/UfsPassThruDxe/UfsPassThru.c >> b/MdeModulePkg/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; } - if ((This->Mode->IoAlign > 1) >> && !IS_ALIGNED(Packet->InDataBuffer, This->Mode->IoAlign)) {+ if ((This= - >>> Mode->IoAlign > 1) && !ADDRESS_IS_ALIGNED(Packet->InDataBuffer, This- >>> Mode->IoAlign)) { return EFI_INVALID_PARAMETER; } - if ((This->M= ode- >>> IoAlign > 1) && !IS_ALIGNED(Packet->OutDataBuffer, This->Mode- >>> IoAlign)) {+ if ((This->Mode->IoAlign > 1) >> && !ADDRESS_IS_ALIGNED(Packet->OutDataBuffer, This->Mode->IoAlign)) >> { return EFI_INVALID_PARAMETER; } - if ((This->Mode->IoAlign > 1) >> && !IS_ALIGNED(Packet->SenseData, This->Mode->IoAlign)) {+ if ((This- >>> Mode->IoAlign > 1) && !ADDRESS_IS_ALIGNED(Packet->SenseData, This- >>> Mode->IoAlign)) { return EFI_INVALID_PARAMETER; } diff --git >> a/MdeModulePkg/Universal/EbcDxe/EbcExecute.c >> b/MdeModulePkg/Universal/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))) {+ i= f >> (!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, si= zeof >> (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, si= zeof >> (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, si= zeof >> (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, si= zeof (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_POR= T_CI >> 0x0038 -#define IS_ALIGNED(addr, size) (((UINTN) (addr) = & (size - 1)) >> =3D=3D 0) #define TIMER_PERIOD_SECONDS(Seconds) >> MultU64x32((UINT64)(Seconds), 10000000) #pragma pack(1)diff --git >> a/MdeModulePkg/Bus/Ata/AtaAtapiPassThru/AtaAtapiPassThru.h >> b/MdeModulePkg/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) - >> #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) // // >> ATA bus data structure for ATA controllerdiff --git >> a/MdeModulePkg/Bus/Scsi/ScsiDiskDxe/ScsiDisk.h >> b/MdeModulePkg/Bus/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 = - >> #define IS_DEVICE_FIXED(a) (a)->FixedDevice ? 1 : 0--#define >> IS_ALIGNED(addr, size) (((UINTN) (addr) & (size - 1)) =3D=3D 0)+#defi= ne >> IS_DEVICE_FIXED(a) (a)->FixedDevice ? 1 : 0 #define UFS_WLUN_R= PMB >> 0xC4 diff --git a/MdeModulePkg/Bus/Ufs/UfsBlockIoPei/UfsBlockIoPei.h >> b/MdeModulePkg/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 { >> #define ROUNDUP8(x) (((x) % 8 =3D=3D 0) ? (x) : ((x) / 8 + 1) * 8) -#= define >> IS_ALIGNED(addr, size) (((UINTN) (addr) & (size - 1)) =3D=3D 0)- #d= efine >> GET_UFS_PEIM_HC_PRIVATE_DATA_FROM_THIS(a) CR (a, >> UFS_PEIM_HC_PRIVATE_DATA, BlkIoPpi, UFS_PEIM_HC_SIG) #define >> GET_UFS_PEIM_HC_PRIVATE_DATA_FROM_THIS2(a) CR (a, >> UFS_PEIM_HC_PRIVATE_DATA, BlkIo2Ppi, UFS_PEIM_HC_SIG) #define >> GET_UFS_PEIM_HC_PRIVATE_DATA_FROM_THIS_NOTIFY(a) CR (a, >> UFS_PEIM_HC_PRIVATE_DATA, EndOfPeiNotifyList, UFS_PEIM_HC_SIG)diff >> --git a/MdeModulePkg/Bus/Ufs/UfsPassThruDxe/UfsPassThru.h >> b/MdeModulePkg/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 { >> #define ROUNDUP8(x) (((x) % 8 =3D=3D 0) ? (x) : ((x) / 8 + 1) * 8) -#= 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/Universal/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)) // // Debug macrodiff --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 >> +/**+ Returns the alignment requirement of a type.++ @param TYPE Th= e >> name of the type to retrieve the alignment requirement 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 MS= VC, >> at least MSVC 2008 and later, support the+ // vendor-extension __aligno= f.+ >> //+ #define ALIGNOF(TYPE) __alignof (TYPE)+#else+ //+ // For compile= rs >> that do not support inbuilt alignof operators, use OFFSET_OF.+ // CHAR8= is >> known to have both a size and an alignment requirement of 1 Byte.+ // A= s >> such, A must be located exactly at the offset equal to its alignment+ /= / >> requirement.+ //+ #define ALIGNOF(TYPE) OFFSET_OF (struct { CHAR8 C; >> TYPE A; }, A)+#endif+ /** Portable definition for compile time asserti= ons. >> Equivalent to C11 static_assert macro from assert.h.@@ -824,6 +853,21 @@ >> STATIC_ASSERT (sizeof (CHAR16) =3D=3D 2, "sizeof (CHAR16) does not meet= UEFI >> Specif >> STATIC_ASSERT (sizeof (L'A') =3D=3D 2, "sizeof (L'A') does not meet= UEFI >> Specification Data Type requirements"); STATIC_ASSERT (sizeof (L"A") = =3D=3D 4, >> "sizeof (L\"A\") does not meet UEFI Specification Data Type requirements= "); >> +STATIC_ASSERT (ALIGNOF (BOOLEAN) =3D=3D sizeof (BOOLEAN), "Alignment of >> BOOLEAN does not meet UEFI Specification Data Type >> requirements");+STATIC_ASSERT (ALIGNOF (INT8) =3D=3D sizeof (INT8), >> "Alignment of INT8 does not meet UEFI Specification Data Type >> requirements");+STATIC_ASSERT (ALIGNOF (UINT8) =3D=3D sizeof (UINT8), >> "Alignment of INT16 does not meet UEFI Specification Data Type >> requirements");+STATIC_ASSERT (ALIGNOF (INT16) =3D=3D sizeof (INT16), >> "Alignment of INT16 does not meet UEFI Specification Data Type >> requirements");+STATIC_ASSERT (ALIGNOF (UINT16) =3D=3D sizeof (UINT16), >> "Alignment of UINT16 does not meet UEFI Specification Data Type >> requirements");+STATIC_ASSERT (ALIGNOF (INT32) =3D=3D sizeof (INT32), >> "Alignment of INT32 does not meet UEFI Specification Data Type >> requirements");+STATIC_ASSERT (ALIGNOF (UINT32) =3D=3D sizeof (UINT32), >> "Alignment of UINT32 does not meet UEFI Specification Data Type >> requirements");+STATIC_ASSERT (ALIGNOF (INT64) =3D=3D sizeof (INT64), >> "Alignment of INT64 does not meet UEFI Specification Data Type >> requirements");+STATIC_ASSERT (ALIGNOF (UINT64) =3D=3D sizeof (UINT64), >> "Alignment of UINT64 does not meet UEFI Specification Data Type >> requirements");+STATIC_ASSERT (ALIGNOF (CHAR8) =3D=3D sizeof (CHAR8), >> "Alignment of CHAR8 does not meet UEFI Specification Data Type >> requirements");+STATIC_ASSERT (ALIGNOF (CHAR16) =3D=3D sizeof (CHAR16), >> "Alignment of CHAR16 does not meet UEFI Specification Data Type >> requirements");+STATIC_ASSERT (ALIGNOF (INTN) =3D=3D sizeof (INTN), >> "Alignment of INTN does not meet UEFI Specification Data Type >> requirements");+STATIC_ASSERT (ALIGNOF (UINTN) =3D=3D sizeof (UINTN), >> "Alignment of UINTN does not meet UEFI Specification Data Type >> requirements");+STATIC_ASSERT (ALIGNOF (VOID *) =3D=3D sizeof (VOID *), >> "Alignment of VOID * 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 Sect= ion >> 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 e= num >> does not meet UEFI Specification Data Type requirements"); STATIC_ASSERT >> (sizeof (__VERIFY_UINT32_ENUM_SIZE) =3D=3D 4, "Size of enum does not mee= t >> UEFI Specification Data Type requirements"); +STATIC_ASSERT (ALIGNOF >> (__VERIFY_UINT8_ENUM_SIZE) =3D=3D sizeof (__VERIFY_UINT8_ENUM_SIZE), >> "Alignment of enum does not meet UEFI Specification Data Type >> requirements");+STATIC_ASSERT (ALIGNOF (__VERIFY_UINT16_ENUM_SIZE) >> =3D=3D sizeof (__VERIFY_UINT16_ENUM_SIZE), "Alignment of enum does not >> meet UEFI Specification Data Type requirements");+STATIC_ASSERT >> (ALIGNOF (__VERIFY_UINT32_ENUM_SIZE) =3D=3D sizeof >> (__VERIFY_UINT32_ENUM_SIZE), "Alignment of enum does not meet UEFI >> Specification Data Type requirements");+ /** Macro that returns a poin= ter >> to the data structure that contains a specified field of that data str= ucture. >> This is a lightweight method to hide information by 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= ) - >> OFFSET_OF (TYPE, Field))) +/**+ 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 whe= ther a >> pointer or address is aligned by a specified alignment.++ @param Addr= ess >> The pointer or address to check.+ @param Alignment The alignment >> boundary used to check against.++ @return Whether Address is aligned b= y >> Alignment.+**/+#define ADDRESS_IS_ALIGNED(Address, Alignment) >> IS_ALIGNED ((UINTN) (Address), Alignment)++/**+ Determines the addend >> to add to a value to round it up to the next boundary 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. @@ -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. **/-#define >> ALIGN_VALUE(Value, Alignment) ((Value) + (((Alignment) - (Value)) & >> ((Alignment) - 1)))+#define ALIGN_VALUE(Value, Alignment) ((Value) + >> ALIGN_VALUE_ADDEND (Value, Alignment)) /** Adjust a pointer by adding >> the minimum offset required for it to be aligned on-- >> 2.31.1 > > >=20 > >