From mboxrd@z Thu Jan 1 00:00:00 1970 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 Authentication-Results: mx.groups.io; dkim=pass header.i=@posteo.de header.s=2017 header.b=QgIH14gF; spf=pass (domain: posteo.de, ip: 185.67.36.66, mailfrom: mhaeuser@posteo.de) 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) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=posteo.de; s=2017; t=1628451636; bh=IBy4w3GE66zx4X2LNvmQsyqf0Q5GX+QIUtUoi0hK2IE=; h=From:To:Cc:Subject:Date:From; b=QgIH14gFPo4tiyPzayUDjmk0OH4hVQQsOvA5R4kjaduzywq97zYX+6bXULMVAf9xW gu/gZWGHk08/qNf6+Afz/2rM7ncBQMwJ/mRwfqA4b4NZVJYE58Lux/e5Ixwu+wX2lh cUU2Wl08FVYCvzHSPlFjwVWyXcx0vgFs0yeFWVsox3As8N4ZG/j1kyOVfKwXKG/oMB jxfHnGEpHkyH0ZrBtWeCETWM8xgRb1VoWnx8UCPx5o32ihwdVkqZD8vIpjPtxClTfc d2xq8XD7kx7E2y9P6bKnlJfLpyX7jjpjfa6E6JL4rADo79Q59obIUK5XG0zPpYuG9L j0Dc8eFtc5TjA== 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: [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 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable 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(-) 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.=0D //=0D if ((AtaPassThru->Mode->IoAlign > 1) &&=0D - !IS_ALIGNED (Buffer, AtaPassThru->Mode->IoAlign)) {=0D + !ADDRESS_IS_ALIGNED (Buffer, AtaPassThru->Mode->IoAlign)) {=0D NewBuffer =3D AllocateAlignedPages (=0D EFI_SIZE_TO_PAGES (TransferLength),=0D AtaPassThru->Mode->IoAlign=0D 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 ( }=0D =0D IoAlign =3D This->Mode->IoAlign;=0D - if ((IoAlign > 1) && !IS_ALIGNED (Packet->InDataBuffer, IoAlign)) {=0D + if ((IoAlign > 1) && !ADDRESS_IS_ALIGNED (Packet->InDataBuffer, IoAlign)= ) {=0D return EFI_INVALID_PARAMETER;=0D }=0D =0D - if ((IoAlign > 1) && !IS_ALIGNED (Packet->OutDataBuffer, IoAlign)) {=0D + if ((IoAlign > 1) && !ADDRESS_IS_ALIGNED (Packet->OutDataBuffer, IoAlign= )) {=0D return EFI_INVALID_PARAMETER;=0D }=0D =0D - if ((IoAlign > 1) && !IS_ALIGNED (Packet->Asb, IoAlign)) {=0D + if ((IoAlign > 1) && !ADDRESS_IS_ALIGNED (Packet->Asb, IoAlign)) {=0D return EFI_INVALID_PARAMETER;=0D }=0D =0D 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 ( =0D Instance =3D ATA_PASS_THRU_PRIVATE_DATA_FROM_THIS (This);=0D =0D - if ((This->Mode->IoAlign > 1) && !IS_ALIGNED(Packet->InDataBuffer, This-= >Mode->IoAlign)) {=0D + if ((This->Mode->IoAlign > 1) && !ADDRESS_IS_ALIGNED(Packet->InDataBuffe= r, This->Mode->IoAlign)) {=0D return EFI_INVALID_PARAMETER;=0D }=0D =0D - if ((This->Mode->IoAlign > 1) && !IS_ALIGNED(Packet->OutDataBuffer, This= ->Mode->IoAlign)) {=0D + if ((This->Mode->IoAlign > 1) && !ADDRESS_IS_ALIGNED(Packet->OutDataBuff= er, This->Mode->IoAlign)) {=0D return EFI_INVALID_PARAMETER;=0D }=0D =0D - if ((This->Mode->IoAlign > 1) && !IS_ALIGNED(Packet->Asb, This->Mode->Io= Align)) {=0D + if ((This->Mode->IoAlign > 1) && !ADDRESS_IS_ALIGNED(Packet->Asb, This->= Mode->IoAlign)) {=0D return EFI_INVALID_PARAMETER;=0D }=0D =0D @@ -2012,15 +2012,15 @@ ExtScsiPassThruPassThru ( return EFI_INVALID_PARAMETER;=0D }=0D =0D - if ((This->Mode->IoAlign > 1) && !IS_ALIGNED(Packet->InDataBuffer, This-= >Mode->IoAlign)) {=0D + if ((This->Mode->IoAlign > 1) && !ADDRESS_IS_ALIGNED(Packet->InDataBuffe= r, This->Mode->IoAlign)) {=0D return EFI_INVALID_PARAMETER;=0D }=0D =0D - if ((This->Mode->IoAlign > 1) && !IS_ALIGNED(Packet->OutDataBuffer, This= ->Mode->IoAlign)) {=0D + if ((This->Mode->IoAlign > 1) && !ADDRESS_IS_ALIGNED(Packet->OutDataBuff= er, This->Mode->IoAlign)) {=0D return EFI_INVALID_PARAMETER;=0D }=0D =0D - if ((This->Mode->IoAlign > 1) && !IS_ALIGNED(Packet->SenseData, This->Mo= de->IoAlign)) {=0D + if ((This->Mode->IoAlign > 1) && !ADDRESS_IS_ALIGNED(Packet->SenseData, = This->Mode->IoAlign)) {=0D return EFI_INVALID_PARAMETER;=0D }=0D =0D 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=0D //=0D AtaPassThru =3D AtaDevice->AtaBusDriverData->AtaPassThru;=0D - if ((AtaPassThru->Mode->IoAlign > 1) && !IS_ALIGNED (Buffer, AtaPassTh= ru->Mode->IoAlign)) {=0D + if ((AtaPassThru->Mode->IoAlign > 1) && !ADDRESS_IS_ALIGNED (Buffer, A= taPassThru->Mode->IoAlign)) {=0D NewBuffer =3D AllocateAlignedBuffer (AtaDevice, TransferLength);=0D if (NewBuffer =3D=3D NULL) {=0D return EFI_OUT_OF_RESOURCES;=0D 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;=0D }=0D =0D - if ((ScsiDiskDevice->ScsiIo->IoAlign > 1) && !IS_ALIGNED (PayloadBuffe= r, ScsiDiskDevice->ScsiIo->IoAlign)) {=0D + if ((ScsiDiskDevice->ScsiIo->IoAlign > 1) && !ADDRESS_IS_ALIGNED (Payl= oadBuffer, ScsiDiskDevice->ScsiIo->IoAlign)) {=0D AlignedBuffer =3D AllocateAlignedBuffer (ScsiDiskDevice, PayloadBuff= erSize);=0D if (AlignedBuffer =3D=3D NULL) {=0D Status =3D EFI_OUT_OF_RESOURCES;=0D @@ -2171,7 +2171,7 @@ ScsiDiskSendData ( goto Done;=0D }=0D =0D - if ((ScsiDiskDevice->ScsiIo->IoAlign > 1) && !IS_ALIGNED (PayloadBuffe= r, ScsiDiskDevice->ScsiIo->IoAlign)) {=0D + if ((ScsiDiskDevice->ScsiIo->IoAlign > 1) && !ADDRESS_IS_ALIGNED (Payl= oadBuffer, ScsiDiskDevice->ScsiIo->IoAlign)) {=0D AlignedBuffer =3D AllocateAlignedBuffer (ScsiDiskDevice, PayloadBuff= erSize);=0D if (AlignedBuffer =3D=3D NULL) {=0D Status =3D EFI_OUT_OF_RESOURCES;=0D 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;=0D }=0D =0D - if ((This->Mode->IoAlign > 1) && !IS_ALIGNED(Packet->InDataBuffer, This-= >Mode->IoAlign)) {=0D + if ((This->Mode->IoAlign > 1) && !ADDRESS_IS_ALIGNED(Packet->InDataBuffe= r, This->Mode->IoAlign)) {=0D return EFI_INVALID_PARAMETER;=0D }=0D =0D - if ((This->Mode->IoAlign > 1) && !IS_ALIGNED(Packet->OutDataBuffer, This= ->Mode->IoAlign)) {=0D + if ((This->Mode->IoAlign > 1) && !ADDRESS_IS_ALIGNED(Packet->OutDataBuff= er, This->Mode->IoAlign)) {=0D return EFI_INVALID_PARAMETER;=0D }=0D =0D - if ((This->Mode->IoAlign > 1) && !IS_ALIGNED(Packet->SenseData, This->Mo= de->IoAlign)) {=0D + if ((This->Mode->IoAlign > 1) && !ADDRESS_IS_ALIGNED(Packet->SenseData, = This->Mode->IoAlign)) {=0D return EFI_INVALID_PARAMETER;=0D }=0D =0D 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.=0D //=0D Data64 =3D (UINT64) VmReadImmed64 (VmPtr, 2);=0D - if (!IS_ALIGNED ((UINTN) Data64, sizeof (UINT16))) {=0D + if (!ADDRESS_IS_ALIGNED ((UINTN) Data64, sizeof (UINT16))) {=0D EbcDebugSignalException (=0D EXCEPT_EBC_ALIGNMENT_CHECK,=0D EXCEPTION_FLAG_FATAL,=0D @@ -2059,7 +2059,7 @@ ExecuteJMP ( // Form: JMP32 @Rx {Index32}=0D //=0D Addr =3D VmReadMemN (VmPtr, (UINTN) Data64 + Index32);=0D - if (!IS_ALIGNED ((UINTN) Addr, sizeof (UINT16))) {=0D + if (!ADDRESS_IS_ALIGNED ((UINTN) Addr, sizeof (UINT16))) {=0D EbcDebugSignalException (=0D EXCEPT_EBC_ALIGNMENT_CHECK,=0D EXCEPTION_FLAG_FATAL,=0D @@ -2082,7 +2082,7 @@ ExecuteJMP ( // Form: JMP32 Rx {Immed32}=0D //=0D Addr =3D (UINTN) (Data64 + Index32);=0D - if (!IS_ALIGNED ((UINTN) Addr, sizeof (UINT16))) {=0D + if (!ADDRESS_IS_ALIGNED ((UINTN) Addr, sizeof (UINT16))) {=0D EbcDebugSignalException (=0D EXCEPT_EBC_ALIGNMENT_CHECK,=0D EXCEPTION_FLAG_FATAL,=0D @@ -3128,7 +3128,7 @@ ExecuteRET ( // Pull the return address off the VM app's stack and set the IP=0D // to it=0D //=0D - if (!IS_ALIGNED ((UINTN) VmPtr->Gpr[0], sizeof (UINT16))) {=0D + if (!ADDRESS_IS_ALIGNED ((UINTN) VmPtr->Gpr[0], sizeof (UINT16))) {=0D EbcDebugSignalException (=0D EXCEPT_EBC_ALIGNMENT_CHECK,=0D EXCEPTION_FLAG_FATAL,=0D @@ -4693,7 +4693,7 @@ VmWriteMem16 ( //=0D // Do a simple write if aligned=0D //=0D - if (IS_ALIGNED (Addr, sizeof (UINT16))) {=0D + if (ADDRESS_IS_ALIGNED (Addr, sizeof (UINT16))) {=0D *(UINT16 *) Addr =3D Data;=0D } else {=0D //=0D @@ -4756,7 +4756,7 @@ VmWriteMem32 ( //=0D // Do a simple write if aligned=0D //=0D - if (IS_ALIGNED (Addr, sizeof (UINT32))) {=0D + if (ADDRESS_IS_ALIGNED (Addr, sizeof (UINT32))) {=0D *(UINT32 *) Addr =3D Data;=0D } else {=0D //=0D @@ -4819,7 +4819,7 @@ VmWriteMem64 ( //=0D // Do a simple write if aligned=0D //=0D - if (IS_ALIGNED (Addr, sizeof (UINT64))) {=0D + if (ADDRESS_IS_ALIGNED (Addr, sizeof (UINT64))) {=0D *(UINT64 *) Addr =3D Data;=0D } else {=0D //=0D @@ -4885,7 +4885,7 @@ VmWriteMemN ( //=0D // Do a simple write if aligned=0D //=0D - if (IS_ALIGNED (Addr, sizeof (UINTN))) {=0D + if (ADDRESS_IS_ALIGNED (Addr, sizeof (UINTN))) {=0D *(UINTN *) Addr =3D Data;=0D } else {=0D for (Index =3D 0; Index < sizeof (UINTN) / sizeof (UINT32); Index++) {= =0D @@ -4949,7 +4949,7 @@ VmReadImmed16 ( //=0D // Read direct if aligned=0D //=0D - if (IS_ALIGNED ((UINTN) VmPtr->Ip + Offset, sizeof (INT16))) {=0D + if (ADDRESS_IS_ALIGNED ((UINTN) VmPtr->Ip + Offset, sizeof (INT16))) {=0D return * (INT16 *) (VmPtr->Ip + Offset);=0D } else {=0D //=0D @@ -4993,7 +4993,7 @@ VmReadImmed32 ( //=0D // Read direct if aligned=0D //=0D - if (IS_ALIGNED ((UINTN) VmPtr->Ip + Offset, sizeof (UINT32))) {=0D + if (ADDRESS_IS_ALIGNED ((UINTN) VmPtr->Ip + Offset, sizeof (UINT32))) {= =0D return * (INT32 *) (VmPtr->Ip + Offset);=0D }=0D //=0D @@ -5032,7 +5032,7 @@ VmReadImmed64 ( //=0D // Read direct if aligned=0D //=0D - if (IS_ALIGNED ((UINTN) VmPtr->Ip + Offset, sizeof (UINT64))) {=0D + if (ADDRESS_IS_ALIGNED ((UINTN) VmPtr->Ip + Offset, sizeof (UINT64))) {= =0D return * (UINT64 *) (VmPtr->Ip + Offset);=0D }=0D //=0D @@ -5069,7 +5069,7 @@ VmReadCode16 ( //=0D // Read direct if aligned=0D //=0D - if (IS_ALIGNED ((UINTN) VmPtr->Ip + Offset, sizeof (UINT16))) {=0D + if (ADDRESS_IS_ALIGNED ((UINTN) VmPtr->Ip + Offset, sizeof (UINT16))) {= =0D return * (UINT16 *) (VmPtr->Ip + Offset);=0D } else {=0D //=0D @@ -5110,7 +5110,7 @@ VmReadCode32 ( //=0D // Read direct if aligned=0D //=0D - if (IS_ALIGNED ((UINTN) VmPtr->Ip + Offset, sizeof (UINT32))) {=0D + if (ADDRESS_IS_ALIGNED ((UINTN) VmPtr->Ip + Offset, sizeof (UINT32))) {= =0D return * (UINT32 *) (VmPtr->Ip + Offset);=0D }=0D //=0D @@ -5147,7 +5147,7 @@ VmReadCode64 ( //=0D // Read direct if aligned=0D //=0D - if (IS_ALIGNED ((UINTN) VmPtr->Ip + Offset, sizeof (UINT64))) {=0D + if (ADDRESS_IS_ALIGNED ((UINTN) VmPtr->Ip + Offset, sizeof (UINT64))) {= =0D return * (UINT64 *) (VmPtr->Ip + Offset);=0D }=0D //=0D @@ -5210,7 +5210,7 @@ VmReadMem16 ( //=0D // Read direct if aligned=0D //=0D - if (IS_ALIGNED (Addr, sizeof (UINT16))) {=0D + if (ADDRESS_IS_ALIGNED (Addr, sizeof (UINT16))) {=0D return * (UINT16 *) Addr;=0D }=0D //=0D @@ -5243,7 +5243,7 @@ VmReadMem32 ( //=0D // Read direct if aligned=0D //=0D - if (IS_ALIGNED (Addr, sizeof (UINT32))) {=0D + if (ADDRESS_IS_ALIGNED (Addr, sizeof (UINT32))) {=0D return * (UINT32 *) Addr;=0D }=0D //=0D @@ -5280,7 +5280,7 @@ VmReadMem64 ( //=0D // Read direct if aligned=0D //=0D - if (IS_ALIGNED (Addr, sizeof (UINT64))) {=0D + if (ADDRESS_IS_ALIGNED (Addr, sizeof (UINT64))) {=0D return * (UINT64 *) Addr;=0D }=0D //=0D @@ -5349,7 +5349,7 @@ VmReadMemN ( //=0D // Read direct if aligned=0D //=0D - if (IS_ALIGNED (Addr, sizeof (UINTN))) {=0D + if (ADDRESS_IS_ALIGNED (Addr, sizeof (UINTN))) {=0D return * (UINTN *) Addr;=0D }=0D //=0D 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=0D #define AHCI_PORT_CI 0x0038=0D =0D -#define IS_ALIGNED(addr, size) (((UINTN) (addr) & (size - = 1)) =3D=3D 0)=0D #define TIMER_PERIOD_SECONDS(Seconds) MultU64x32((UINT64)(Seconds= ), 10000000)=0D =0D #pragma pack(1)=0D 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)=0D #define ATA_SPINUP_TIMEOUT EFI_TIMER_PERIOD_SECONDS(10)=0D =0D -#define IS_ALIGNED(addr, size) (((UINTN) (addr) & (size - 1)) =3D=3D = 0)=0D -=0D #define ATA_PASS_THRU_PRIVATE_DATA_FROM_THIS(a) \=0D CR (a, \=0D ATA_ATAPI_PASS_THRU_INSTANCE, \=0D 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'= )=0D #define ATA_DEVICE_SIGNATURE SIGNATURE_32 ('A', 'B', 'I', 'D'= )=0D #define ATA_SUB_TASK_SIGNATURE SIGNATURE_32 ('A', 'S', 'T', 'S'= )=0D -#define IS_ALIGNED(addr, size) (((UINTN) (addr) & (size - 1)) = =3D=3D 0)=0D =0D //=0D // ATA bus data structure for ATA controller=0D 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 =0D #include =0D =0D -#define IS_DEVICE_FIXED(a) (a)->FixedDevice ? 1 : 0=0D -=0D -#define IS_ALIGNED(addr, size) (((UINTN) (addr) & (size - 1)) =3D=3D 0)= =0D +#define IS_DEVICE_FIXED(a) (a)->FixedDevice ? 1 : 0=0D =0D #define UFS_WLUN_RPMB 0xC4=0D =0D 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 { =0D #define ROUNDUP8(x) (((x) % 8 =3D=3D 0) ? (x) : ((x) / 8 + 1) * 8)=0D =0D -#define IS_ALIGNED(addr, size) (((UINTN) (addr) & (size - 1)) =3D=3D = 0)=0D -=0D #define GET_UFS_PEIM_HC_PRIVATE_DATA_FROM_THIS(a) CR (a, UFS_PEIM_HC_PRIVA= TE_DATA, BlkIoPpi, UFS_PEIM_HC_SIG)=0D #define GET_UFS_PEIM_HC_PRIVATE_DATA_FROM_THIS2(a) CR (a, UFS_PEIM_HC_PRIV= ATE_DATA, BlkIo2Ppi, UFS_PEIM_HC_SIG)=0D #define GET_UFS_PEIM_HC_PRIVATE_DATA_FROM_THIS_NOTIFY(a) CR (a, UFS_PEIM_H= C_PRIVATE_DATA, EndOfPeiNotifyList, UFS_PEIM_HC_SIG)=0D 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 { =0D #define ROUNDUP8(x) (((x) % 8 =3D=3D 0) ? (x) : ((x) / 8 + 1) * 8)=0D =0D -#define IS_ALIGNED(addr, size) (((UINTN) (addr) & (size - 1)) =3D= =3D 0)=0D -=0D #define UFS_PASS_THRU_PRIVATE_DATA_FROM_THIS(a) \=0D CR (a, \=0D UFS_PASS_THRU_PRIVATE_DATA, \=0D 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 //=0D // Macros to check and set alignment=0D //=0D -#define ASSERT_ALIGNED(addr, size) ASSERT (!((UINT32) (addr) & (size - 1)= ))=0D -#define IS_ALIGNED(addr, size) !((UINT32) (addr) & (size - 1))=0D +#define ASSERT_ALIGNED(addr, size) ASSERT (IS_ALIGNED (addr, size))=0D =0D //=0D // Debug macro=0D 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))=0D #endif=0D =0D +/**=0D + Returns the alignment requirement of a type.=0D +=0D + @param TYPE The name of the type to retrieve the alignment requiremen= t of.=0D +=0D + @return Alignment requirement, in Bytes, of TYPE.=0D +**/=0D +#if defined(__GNUC__) || defined(__clang__) || (defined(_MSC_VER) && _MSC_= VER >=3D 1900)=0D + //=0D + // All supported versions of GCC and Clang, as well as MSVC 2015 and lat= er,=0D + // support the standard operator _Alignof.=0D + //=0D + #define ALIGNOF(TYPE) _Alignof (TYPE)=0D +#elif defined(_MSC_VER)=0D + //=0D + // Earlier versions of MSVC, at least MSVC 2008 and later, support the=0D + // vendor-extension __alignof.=0D + //=0D + #define ALIGNOF(TYPE) __alignof (TYPE)=0D +#else=0D + //=0D + // For compilers that do not support inbuilt alignof operators, use OFFS= ET_OF.=0D + // CHAR8 is known to have both a size and an alignment requirement of 1 = Byte.=0D + // As such, A must be located exactly at the offset equal to its alignme= nt=0D + // requirement.=0D + //=0D + #define ALIGNOF(TYPE) OFFSET_OF (struct { CHAR8 C; TYPE A; }, A)=0D +#endif=0D +=0D /**=0D Portable definition for compile time assertions.=0D Equivalent to C11 static_assert macro from assert.h.=0D @@ -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");=0D STATIC_ASSERT (sizeof (L"A") =3D=3D 4, "sizeof (L\"A\") does not meet U= EFI Specification Data Type requirements");=0D =0D +STATIC_ASSERT (ALIGNOF (BOOLEAN) =3D=3D sizeof (BOOLEAN), "Alignment of BO= OLEAN does not meet UEFI Specification Data Type requirements");=0D +STATIC_ASSERT (ALIGNOF (INT8) =3D=3D sizeof (INT8), "Alignment of IN= T8 does not meet UEFI Specification Data Type requirements");=0D +STATIC_ASSERT (ALIGNOF (UINT8) =3D=3D sizeof (UINT8), "Alignment of IN= T16 does not meet UEFI Specification Data Type requirements");=0D +STATIC_ASSERT (ALIGNOF (INT16) =3D=3D sizeof (INT16), "Alignment of IN= T16 does not meet UEFI Specification Data Type requirements");=0D +STATIC_ASSERT (ALIGNOF (UINT16) =3D=3D sizeof (UINT16), "Alignment of UI= NT16 does not meet UEFI Specification Data Type requirements");=0D +STATIC_ASSERT (ALIGNOF (INT32) =3D=3D sizeof (INT32), "Alignment of IN= T32 does not meet UEFI Specification Data Type requirements");=0D +STATIC_ASSERT (ALIGNOF (UINT32) =3D=3D sizeof (UINT32), "Alignment of UI= NT32 does not meet UEFI Specification Data Type requirements");=0D +STATIC_ASSERT (ALIGNOF (INT64) =3D=3D sizeof (INT64), "Alignment of IN= T64 does not meet UEFI Specification Data Type requirements");=0D +STATIC_ASSERT (ALIGNOF (UINT64) =3D=3D sizeof (UINT64), "Alignment of UI= NT64 does not meet UEFI Specification Data Type requirements");=0D +STATIC_ASSERT (ALIGNOF (CHAR8) =3D=3D sizeof (CHAR8), "Alignment of CH= AR8 does not meet UEFI Specification Data Type requirements");=0D +STATIC_ASSERT (ALIGNOF (CHAR16) =3D=3D sizeof (CHAR16), "Alignment of CH= AR16 does not meet UEFI Specification Data Type requirements");=0D +STATIC_ASSERT (ALIGNOF (INTN) =3D=3D sizeof (INTN), "Alignment of IN= TN does not meet UEFI Specification Data Type requirements");=0D +STATIC_ASSERT (ALIGNOF (UINTN) =3D=3D sizeof (UINTN), "Alignment of UI= NTN does not meet UEFI Specification Data Type requirements");=0D +STATIC_ASSERT (ALIGNOF (VOID *) =3D=3D sizeof (VOID *), "Alignment of VO= ID * does not meet UEFI Specification Data Type requirements");=0D +=0D //=0D // The following three enum types are used to verify that the compiler=0D // configuration for enum types is compliant with Section 2.3.1 of the=0D @@ -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");=0D STATIC_ASSERT (sizeof (__VERIFY_UINT32_ENUM_SIZE) =3D=3D 4, "Size of enum = does not meet UEFI Specification Data Type requirements");=0D =0D +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");=0D +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");=0D +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");=0D +=0D /**=0D Macro that returns a pointer to the data structure that contains a speci= fied field of=0D that data structure. This is a lightweight method to hide information b= y placing a=0D @@ -868,6 +916,46 @@ STATIC_ASSERT (sizeof (__VERIFY_UINT32_ENUM_SIZE) =3D= =3D 4, "Size of enum does not m **/=0D #define BASE_CR(Record, TYPE, Field) ((TYPE *) ((CHAR8 *) (Record) - OFFS= ET_OF (TYPE, Field)))=0D =0D +/**=0D + Checks whether a value is a power of two.=0D +=0D + @param Value The value to check.=0D +=0D + @return Whether Value is a power of two.=0D +**/=0D +#define IS_POW2(Value) ((Value) !=3D 0U && ((Value) & ((Value) - 1U)) =3D= =3D 0U)=0D +=0D +/**=0D + Checks whether a value is aligned by a specified alignment.=0D +=0D + @param Value The value to check.=0D + @param Alignment The alignment boundary used to check against.=0D +=0D + @return Whether Value is aligned by Alignment.=0D +**/=0D +#define IS_ALIGNED(Value, Alignment) (((Value) & ((Alignment) - 1U)) =3D= =3D 0U)=0D +=0D +/**=0D + Checks whether a pointer or address is aligned by a specified alignment.= =0D +=0D + @param Address The pointer or address to check.=0D + @param Alignment The alignment boundary used to check against.=0D +=0D + @return Whether Address is aligned by Alignment.=0D +**/=0D +#define ADDRESS_IS_ALIGNED(Address, Alignment) IS_ALIGNED ((UINTN) (Addre= ss), Alignment)=0D +=0D +/**=0D + Determines the addend to add to a value to round it up to the next bound= ary of=0D + a specified alignment.=0D +=0D + @param Value The value to round up.=0D + @param Alignment The alignment boundary used to return the addend.=0D +=0D + @return Addend to round Value up to alignment boundary Alignment.=0D +**/=0D +#define ALIGN_VALUE_ADDEND(Value, Alignment) (((Alignment) - (Value)) & (= (Alignment) - 1U))=0D +=0D /**=0D Rounds a value up to the next boundary using a specified alignment.=0D =0D @@ -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.=0D =0D **/=0D -#define ALIGN_VALUE(Value, Alignment) ((Value) + (((Alignment) - (Value)) = & ((Alignment) - 1)))=0D +#define ALIGN_VALUE(Value, Alignment) ((Value) + ALIGN_VALUE_ADDEND (Value= , Alignment))=0D =0D /**=0D Adjust a pointer by adding the minimum offset required for it to be alig= ned on=0D --=20 2.31.1