From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from NAM11-DM6-obe.outbound.protection.outlook.com (NAM11-DM6-obe.outbound.protection.outlook.com [40.107.223.138]) by mx.groups.io with SMTP id smtpd.web11.1029.1628026829865112944 for ; Tue, 03 Aug 2021 14:40:30 -0700 Authentication-Results: mx.groups.io; dkim=fail reason="no key for verify" header.i=@amperemail.onmicrosoft.com header.s=selector1-amperemail-onmicrosoft-com header.b=KpAQp0hA; spf=pass (domain: os.amperecomputing.com, ip: 40.107.223.138, mailfrom: tuanphan@os.amperecomputing.com) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=lh/YTKoSfsnGWZ3BRQjxJCcrqv/NIZ55DP6MVeX6RKd/bHrMhgwuH8NVBFpPQ88gOrxY0uZJXwQvBnIioLo1gCEz7W6rXWQW/Q91oW1qFLTdukg+xYzJ9sVGzl647cy7xMAL6+dicfxkbnQLGixegV349PYVImv0KpMbXGiKdkw+CXmSSqFutHhJwHikMeFLg4HZtTYPveC/AxVu/UlLVo6GKtlLYKzlonuzxB/ccwa42ul/la5hy0n3L8EfJOKjFB/37fAy6c6+twIXRS4xqrs/Ho1FBVtza71hoqXETnTrYDsZy6krr742+A+EOP4M9tbvZnA5T2tWhh1xGLgFuw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=X08FhFVyaPKEGR90QJiLFckd57tU/MzIumC376NDnms=; b=VhCN0BOvHEywANxhaFhWg4NxyRumy7QH4PbBBa8addU+xbKPnnDezFr5mRO/wUk5vtx2+zWYgQDafddN9ytPsD8LAxJVf7y4u29BxivJqVoQpTX90ReMP2Rkrfr0fpp1DQ9yR9BXVg2TZ+irI72Os9CUzlf8tqUE6hXVrprVferiBFObKa2PaWSfyh19Ut37YqUTseMjri2Shtw4yVEPKWmvzX4A6UlBLBZHGYaVPbACEk+S/xpDmKxRzH88sRf6zbS292M3FHACe4061TwcrzVvtReXj1UuUxEfWi6lgySXhxwJhX8GyrsrsQ/Guk+PS0XSnthATbqTN0fqvad8zg== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=os.amperecomputing.com; dmarc=pass action=none header.from=amperemail.onmicrosoft.com; dkim=pass header.d=amperemail.onmicrosoft.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amperemail.onmicrosoft.com; s=selector1-amperemail-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=X08FhFVyaPKEGR90QJiLFckd57tU/MzIumC376NDnms=; b=KpAQp0hACbdZYJzA/HgQFULi233L+GJqeh+RjmDHGSbClWqU8v0DYmIrGMXZuFWoecyTucCF/T1AuRRT7ThB5xWNXa/DOr1WiEHsQSU9a4MQp3GTATdFP6HFhcQaOKqUopRRDLkwuDmlthVkJD+Z6SruNS2gPzonGcfSZt7kpLU= Authentication-Results: edk2.groups.io; dkim=none (message not signed) header.d=none;edk2.groups.io; dmarc=none action=none header.from=amperemail.onmicrosoft.com; Received: from BYAPR01MB4598.prod.exchangelabs.com (2603:10b6:a03:8a::18) by BYAPR01MB4503.prod.exchangelabs.com (2603:10b6:a03:a1::20) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4373.18; Tue, 3 Aug 2021 21:40:26 +0000 Received: from BYAPR01MB4598.prod.exchangelabs.com ([fe80::1583:53b:e766:78b2]) by BYAPR01MB4598.prod.exchangelabs.com ([fe80::1583:53b:e766:78b2%4]) with mapi id 15.20.4373.026; Tue, 3 Aug 2021 21:40:25 +0000 From: tuanphan@os.amperecomputing.com Message-Id: <553E56A0-3016-4B22-88E6-AEBF0F0B17EF@amperemail.onmicrosoft.com> Subject: Re: [edk2-devel] [edk2-test][PATCH v1 1/1] uefi-sct/SctPkg: Update page alignment calculations Date: Tue, 3 Aug 2021 14:40:22 -0700 In-Reply-To: <20210719080759.824-1-Sunny.Wang@arm.com> Cc: Samer El-Haj-Mahmoud , G Edhaya Chandran , Barton Gao To: devel@edk2.groups.io, Sunny Wang References: <20210719080759.824-1-Sunny.Wang@arm.com> X-Mailer: Apple Mail (2.3654.120.0.1.13) X-ClientProxiedBy: CY4PR02CA0048.namprd02.prod.outlook.com (2603:10b6:903:117::34) To BYAPR01MB4598.prod.exchangelabs.com (2603:10b6:a03:8a::18) Return-Path: tuanphan@amperemail.onmicrosoft.com MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from smtpclient.apple (76.20.23.24) by CY4PR02CA0048.namprd02.prod.outlook.com (2603:10b6:903:117::34) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4394.15 via Frontend Transport; Tue, 3 Aug 2021 21:40:24 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: f1c291e5-f562-4f0f-889b-08d956c74dd6 X-MS-TrafficTypeDiagnostic: BYAPR01MB4503: X-LD-Processed: 3bc2b170-fd94-476d-b0ce-4229bdc904a7,ExtAddr X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:7219; X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: 2WmWZJgKS6IKn2ZaQ81/YFtQVKcV0CyLRo2ORxRK1c1qJXCO0zR5KBAjV3+hgbLP/GzCByuYDsChgqA/jZY0tl6NL0oysB7XWqgf+aydOHsxvKpweBxzEQSu5ZI1mgUfkJS8OdVqP51nxaDf/rZBhXpTmwocVs9JS021Me7HGeISow8Q3F4RHI4v6ae+iC8AY4KQDfwniF0gmkz5U5NjimBOiF5Za7flC7Zi4fQKFTLalnNEs2GND/9VGetabmeE0tgH/9tfJuvLmFyZtg4GAe/0Bv81lUGLmctyMVarq/a9ULYrdozrQpcd5/NvmxXJBZi+CwVJvfnzfckcBMYXWngIqEXhkYvZWL8Rgat0rz25jiG9q4dZG0pMhaj6e15b+J1IiJKCGuVrPKReBL3B2TXF3f5qhDJ9T+ddaebnvChh9hM1oHALMaBBDmid/3dJnIAR//+fajOKfHOIIS1O83Y0bH7ihTTlVVH9A/UxOdt+oti70O8ANV14RFuaV983w2YYg6uP8NN1nWoZ2RPbshmr0I2LLOtUKqFEiQpVfucGRS0OjnL8CwXPnpNDVpJnKa0T+JgzJZhA79t765bV0TvZ6YbpRuqZ4gV2DRjTsJByerctbmQmm9oRDAA2JVC6x/X1QcAu/CZsig0nwEaIdif8xjdXRNdqZPrvEPk3pHggTLOArLXuS0sIDDsYem4SgH7WvE5UhP2DVf3EBv9yxjRZkq1MXeB7q1mOROWEttRdYM3UhSMUVbKC+9rSNNLcZqF8bH/7BGuP3tjfmoQiMZ/EUTUF/m7Lu9OdTAyyLiiCKPyBo6GhZ0G5HEBUDxGy X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:BYAPR01MB4598.prod.exchangelabs.com;PTR:;CAT:NONE;SFS:(4636009)(366004)(396003)(39850400004)(376002)(346002)(136003)(83170400001)(316002)(5660300002)(4326008)(166002)(83380400001)(42882007)(26005)(53546011)(30864003)(186003)(8676002)(66476007)(66556008)(33656002)(8936002)(6506007)(66946007)(478600001)(966005)(15650500001)(2906002)(6916009)(6512007)(52116002)(38350700002)(38100700002)(54906003)(2616005)(956004)(6486002)(45980500001);DIR:OUT;SFP:1102; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?us-ascii?Q?gv2uoHpJjyevzxUzW+AZbm/lHqx/ZgjygRLOmPc0ZzaVN6++/k+E8y4Bzvo3?= =?us-ascii?Q?5txEZD7OKzToXCA5s2/g1IVVv1Mthpra8EA5O/qnfUc0bVILe1/V6iSXXU0h?= =?us-ascii?Q?EKjaybRASCJJGdl72edbxN+mBg610Ltgst0f+vhhsfj5NKV5/Gpmutw9CM8J?= =?us-ascii?Q?juBgE+BWWY8l2EAAcD80nRrNYuPcL/mMLFJTntgvRkZlOyXztPnP+lL1Gj2y?= =?us-ascii?Q?ZABPi+aRCAJ4tRHwaTdRogB3TWoPJkQ5WLrrVxenKJAD4IAD2e1RzoPCUV5I?= =?us-ascii?Q?2VkKHGZSik2J6H5FEdkCyBaMEhGUx1ooYfOfWVq06NgMZqaVQiDDTXZZj5lm?= =?us-ascii?Q?tmRVsgJVUUaqVN7+aHVtImTkzvWZDZZVgcHefmThTJiErB0G474prIkqbPQl?= =?us-ascii?Q?Ue3nvJrR9h5MFN1Ckis4YFon9NaQWb3zUllS5kLA0gtKsp9DV6vZXqlpgat4?= =?us-ascii?Q?5uo9DHK4KMrHED/hwjt5SkPguwt1oi1IVpUlfOKwkLmR0zFnWOZPfSk/IsfD?= =?us-ascii?Q?Zws0N9t7BL8pwjT5Ciw6EnlvH8sPK5B7fWdJScawSufRFx2qHX3gg5ryby3a?= =?us-ascii?Q?6A3OKJSeLXCrO7myZbAvhD4cWtRYc5hWF8/WeuTzpD7RLl1ZXCA9AE18UzVM?= =?us-ascii?Q?5/EM4PYVueGspHHTg2LvESldlJZ0OIJjXf7N8yJoTXpZeJYWA8Ud9Iz4jsuV?= =?us-ascii?Q?BLTU8KkH5y1fSLrrNThx46Yg++HSV5y4ZhknDH+XOGKO7ZYYCQTzNEu1JhrA?= =?us-ascii?Q?0kwBK7dSAyNymVqkkVLD2rmCmAiOvoqhgUcnUi2SqhJkSC2f62H3iGbGc9vE?= =?us-ascii?Q?Z6NT/ZuZNphfbIOST0I9kbALMre6MDA+5SoDDgZz7bnaOMp7s1Kpxsr80A2E?= =?us-ascii?Q?LItXgTru3BvXUayCwwGQBNLWGuWTfIpqyMOmznO9G+uzIcjr/0U4pXguzfD5?= =?us-ascii?Q?oE13xsJwxS2YQF6sKq+GVBimBmN80IjUBWZAARn6BHQAavzJQnmetaoxJ0sm?= =?us-ascii?Q?xbzEkj0CIqjZU9m//9CKi9UfKOEK0/7jMA+KmXmY5asJMtkCQD36qLiefmLl?= =?us-ascii?Q?Yea4hZHwpmfjFtHWHOK3naS8vCHbl8qtOLTVuya3CM5YjfMfb0Xa3yPl92kh?= =?us-ascii?Q?j6qjugIwp36O/i9JgNtJBRcdcclaKimuLiLcNRwqHm65kaM4Zh9JGKM1NeVQ?= =?us-ascii?Q?GhwfPLTYYvcQvfrcS7EyDjdJmCFfoAvUv6mBSjkoqMgpaVF6gOhCvV20meuS?= =?us-ascii?Q?Nihk2UrCh4jNc+DUJzmZlvq5xS5wdqGW0T4yBDXwRqKfPnpl+hIySZ2p0dfk?= =?us-ascii?Q?hSbe5ZJI3msQnWWnHsMY6rp1?= X-MS-Exchange-Transport-Forked: True X-OriginatorOrg: amperemail.onmicrosoft.com X-MS-Exchange-CrossTenant-Network-Message-Id: f1c291e5-f562-4f0f-889b-08d956c74dd6 X-MS-Exchange-CrossTenant-AuthSource: BYAPR01MB4598.prod.exchangelabs.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 03 Aug 2021 21:40:25.6870 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 3bc2b170-fd94-476d-b0ce-4229bdc904a7 X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: qUJV2GaXD/l0s6EY906zKlKul6KCn/NQyJlJNa00da9Mc12rRnJegm8svVbQYak/09xKk0yhLwN4vaIfFFQFyBbTAn2Vpl0wfFjI9IXIlZzcJqEJ0LAic/OeqLUela7D X-MS-Exchange-Transport-CrossTenantHeadersStamped: BYAPR01MB4503 Content-Type: multipart/alternative; boundary="Apple-Mail=_2A71B675-0D49-408E-8878-676045C6AA6A" --Apple-Mail=_2A71B675-0D49-408E-8878-676045C6AA6A Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=us-ascii Reviewed-By: Tuan Phan > On Jul 19, 2021, at 1:07 AM, Sunny Wang via groups.io wrote: >=20 > This is to fix the SCT BS.AllocatePages failures (not found) with the > case that the Start address is not aligned to 64k. > For example, > The following is available memory region for testing: > 0000000082012000-00000000EB6D9FFF 00000000000696C8 > With the current page alignment calculation, we will get: > Start address is 0x82020000 > PageNum is 0x696B8 > In BS.AllocatePages, it will make the end address align with 64k, > so PageNum will be changed from 0x696B8 to 0x696C0. Therefore, the > end address will become 0xEB6E0000 which is larger than 0xEB6D9FFF, > so we get not found error in the end. >=20 > Therefore, the calculation for getting the PageNum should be updated > to PageNum - (2 * EFI_SIZE_TO_PAGES(0x10000)) so that we won't get a > wrong PageNum to allocate a memory with a size larger than available > space's size. >=20 > With this solution, the example above will get 0x696A8 as calculated > PageNum. Then, in BS.AllocatePages, the PageNum will be changed from > 0x696A8 to 0x696B0. Therefore, the end address will become 0xEB6D0000 > that is smaller than 0xEB6D9FFF, so we get not found error in the end. >=20 > I also tested this solution on two ARM platforms (NXP1046A and RPi4). >=20 > Cc: Samer El-Haj-Mahmoud > Cc: G Edhaya Chandran > Cc: Barton Gao > Signed-off-by: Sunny Wang > --- > .../MemoryAllocationServicesBBTestFunction.c | 110 +++++++++++------- > 1 file changed, 66 insertions(+), 44 deletions(-) >=20 > diff --git a/uefi-sct/SctPkg/TestCase/UEFI/EFI/BootServices/MemoryAlloca= tionServices/BlackBoxTest/MemoryAllocationServicesBBTestFunction.c b/uefi-s= ct/SctPkg/TestCase/UEFI/EFI/BootServices/MemoryAllocationServices/BlackBoxT= est/MemoryAllocationServicesBBTestFunction.c > index bf8cd3b3..cdfac992 100644 > --- a/uefi-sct/SctPkg/TestCase/UEFI/EFI/BootServices/MemoryAllocationSer= vices/BlackBoxTest/MemoryAllocationServicesBBTestFunction.c > +++ b/uefi-sct/SctPkg/TestCase/UEFI/EFI/BootServices/MemoryAllocationSer= vices/BlackBoxTest/MemoryAllocationServicesBBTestFunction.c > @@ -2,6 +2,7 @@ >=20 > Copyright 2006 - 2013 Unified EFI, Inc.
> Copyright (c) 2010 - 2013, Intel Corporation. All rights reserved.
> + Copyright (c) 2021, ARM Limited. All rights reserved. >=20 > This program and the accompanying materials > are licensed and made available under the terms and conditions of the = BSD License > @@ -24,7 +25,7 @@ Abstract: >=20 > --*/ >=20 > -#include "SctLib.h" > +#include "SctLib.h" > #include "MemoryAllocationServicesBBTestMain.h" >=20 > #define ALLOCATEPAGES_MEMORYTYPE_NUM 16 > @@ -700,14 +701,17 @@ BBTestAllocatePagesInterfaceTest ( > PageNum =3D (UINTN)Descriptor.NumberOfPages; > Start =3D Descriptor.PhysicalStart; >=20 > - // > - // Some memory types need more alignment than 4K, so > - // > - if (PageNum <=3D 0x10) { > + // > + // Calculate New Start address and PageNum with 64k alignment t= o > + // cover the case that some memory types' alignment is more tha= n > + // 4k. If the available memory is less than 192k, the memory > + // allocation call will be skipped. > + // > + if (PageNum < (3 * EFI_SIZE_TO_PAGES(0x10000))) { > break; > } > - Start =3D (Start + 0xFFFF) & 0xFFFFFFFFFFFF0000; > - PageNum =3D PageNum - EFI_SIZE_TO_PAGES(0x10000); > + Start =3D (Start + 0xFFFF) & 0xFFFFFFFFFFFF0000; > + PageNum =3D PageNum - (2 * EFI_SIZE_TO_PAGES(0x10000)); >=20 > Memory =3D Start; >=20 > @@ -830,14 +834,17 @@ BBTestAllocatePagesInterfaceTest ( > PageNum =3D (UINTN)Descriptor.NumberOfPages; > Start =3D Descriptor.PhysicalStart; >=20 > - // > - // Some memory types need more alignment than 4K, so > - // > - if (PageNum <=3D 0x10) { > + // > + // Calculate New Start address and PageNum with 64k alignment t= o > + // cover the case that some memory types' alignment is more tha= n > + // 4k. If the available memory is less than 192k, the memory > + // allocation call will be skipped. > + // > + if (PageNum < (3 * EFI_SIZE_TO_PAGES(0x10000))) { > break; > } > - Start =3D (Start + 0xFFFF) & 0xFFFFFFFFFFFF0000; > - PageNum =3D PageNum - EFI_SIZE_TO_PAGES(0x10000); > + Start =3D (Start + 0xFFFF) & 0xFFFFFFFFFFFF0000; > + PageNum =3D PageNum - (2 * EFI_SIZE_TO_PAGES(0x10000)); >=20 > Memory =3D Start; >=20 > @@ -953,14 +960,17 @@ BBTestAllocatePagesInterfaceTest ( > PageNum =3D (UINTN)Descriptor.NumberOfPages; > Start =3D Descriptor.PhysicalStart; >=20 > - // > - // Some memory types need more alignment than 4K, so > - // > - if (PageNum <=3D 0x10) { > + // > + // Calculate New Start address and PageNum with 64k alignment t= o > + // cover the case that some memory types' alignment is more tha= n > + // 4k. If the available memory is less than 192k, the memory > + // allocation call will be skipped. > + // > + if (PageNum < (3 * EFI_SIZE_TO_PAGES(0x10000))) { > break; > } > - Start =3D (Start + 0xFFFF) & 0xFFFFFFFFFFFF0000; > - PageNum =3D PageNum - EFI_SIZE_TO_PAGES(0x10000); > + Start =3D (Start + 0xFFFF) & 0xFFFFFFFFFFFF0000; > + PageNum =3D PageNum - (2 * EFI_SIZE_TO_PAGES(0x10000)); >=20 > Memory =3D Start + (SctLShiftU64 (PageNum/3, EFI_PAGE_SHIFT) & 0= xFFFFFFFFFFFF0000); >=20 > @@ -1076,14 +1086,17 @@ BBTestAllocatePagesInterfaceTest ( > PageNum =3D (UINTN)Descriptor.NumberOfPages; > Start =3D Descriptor.PhysicalStart; >=20 > - // > - // Some memory types need more alignment than 4K, so > - // > - if (PageNum <=3D 0x10) { > + // > + // Calculate New Start address and PageNum with 64k alignment t= o > + // cover the case that some memory types' alignment is more tha= n > + // 4k. If the available memory is less than 192k, the memory > + // allocation call will be skipped. > + // > + if (PageNum < (3 * EFI_SIZE_TO_PAGES(0x10000))) { > break; > } > - Start =3D (Start + 0xFFFF) & 0xFFFFFFFFFFFF0000; > - PageNum =3D PageNum - EFI_SIZE_TO_PAGES(0x10000); > + Start =3D (Start + 0xFFFF) & 0xFFFFFFFFFFFF0000; > + PageNum =3D PageNum - (2 * EFI_SIZE_TO_PAGES(0x10000)); >=20 > Memory =3D Start + (SctLShiftU64 (PageNum * 2 / 3, EFI_PAGE_SHI= FT) & 0xFFFFFFFFFFFF0000); >=20 > @@ -1206,14 +1219,17 @@ BBTestAllocatePagesInterfaceTest ( > PageNum =3D (UINTN)Descriptor.NumberOfPages; > Start =3D Descriptor.PhysicalStart; >=20 > - // > - // Some memory types need more alignment than 4K, so > - // > - if (PageNum <=3D 0x10) { > + // > + // Calculate New Start address and PageNum with 64k alignment t= o > + // cover the case that some memory types' alignment is more tha= n > + // 4k. If the available memory is less than 192k, the memory > + // allocation call will be skipped. > + // > + if (PageNum < (3 * EFI_SIZE_TO_PAGES(0x10000))) { > break; > } > - Start =3D (Start + 0xFFFF) & 0xFFFFFFFFFFFF0000; > - PageNum =3D PageNum - EFI_SIZE_TO_PAGES(0x10000); > + Start =3D (Start + 0xFFFF) & 0xFFFFFFFFFFFF0000; > + PageNum =3D PageNum - (2 * EFI_SIZE_TO_PAGES(0x10000)); >=20 > Memory =3D Start; >=20 > @@ -1329,14 +1345,17 @@ BBTestAllocatePagesInterfaceTest ( > PageNum =3D (UINTN)Descriptor.NumberOfPages; > Start =3D Descriptor.PhysicalStart; >=20 > - // > - // Some memory types need more alignment than 4K, so > - // > - if (PageNum <=3D 0x10) { > + // > + // Calculate New Start address and PageNum with 64k alignment t= o > + // cover the case that some memory types' alignment is more tha= n > + // 4k. If the available memory is less than 192k, the memory > + // allocation call will be skipped. > + // > + if (PageNum < (3 * EFI_SIZE_TO_PAGES(0x10000))) { > break; > } > - Start =3D (Start + 0xFFFF) & 0xFFFFFFFFFFFF0000; > - PageNum =3D PageNum - EFI_SIZE_TO_PAGES(0x10000); > + Start =3D (Start + 0xFFFF) & 0xFFFFFFFFFFFF0000; > + PageNum =3D PageNum - (2 * EFI_SIZE_TO_PAGES(0x10000)); >=20 > Memory =3D Start; >=20 > @@ -1468,14 +1487,17 @@ BBTestAllocatePagesInterfaceTest ( > PageNum =3D (UINTN)Descriptor.NumberOfPages; > Start =3D Descriptor.PhysicalStart; >=20 > - // > - // Some memory types need more alignment than 4K, so > - // > - if (PageNum <=3D 0x10) { > + // > + // Calculate New Start address and PageNum with 64k alignment t= o > + // cover the case that some memory types' alignment is more tha= n > + // 4k. If the available memory is less than 192k, the memory > + // allocation call will be skipped. > + // > + if (PageNum < (3 * EFI_SIZE_TO_PAGES(0x10000))) { > break; > } > - Start =3D (Start + 0xFFFF) & 0xFFFFFFFFFFFF0000; > - PageNum =3D PageNum - EFI_SIZE_TO_PAGES(0x10000); > + Start =3D (Start + 0xFFFF) & 0xFFFFFFFFFFFF0000; > + PageNum =3D PageNum - (2 * EFI_SIZE_TO_PAGES(0x10000)); >=20 > Memory =3D Start; >=20 > @@ -1923,4 +1945,4 @@ BBTestFreePoolInterfaceTest ( >=20 > FreeMemoryMap (); > return EFI_SUCCESS; > -} > +} > --=20 > 2.31.0.windows.1 >=20 >=20 >=20 >=20 >=20 >=20 --Apple-Mail=_2A71B675-0D49-408E-8878-676045C6AA6A Content-Transfer-Encoding: quoted-printable Content-Type: text/html; charset=us-ascii Reviewed-By: Tuan = Phan <tuan= phan@os.amperecomputing.com>

On Jul 19, 2021, at = 1:07 AM, Sunny Wang via groups.io <Sunny.W= ang=3Darm.com@groups.io> wrote:

This is to fix the SCT BS.Allocate= Pages failures (not found) with the
case that the Start addre= ss is not aligned to 64k.
For example,
 T= he following is available memory region for testing:
 &= nbsp; 0000000082012000-00000000EB6D9FFF 00000000000696C8
 With the current page alignment calculation, we will get:
   Start address is 0x82020000
 =   PageNum is 0x696B8
 In BS.AllocatePages, it= will make the end address align with 64k,
 so PageNum = will be changed from 0x696B8 to 0x696C0. Therefore, the
&nbs= p;end address will become 0xEB6E0000 which is larger than 0xEB6D9FFF,
 so we get not found error in the end.

Therefore, the calculation for getting the PageNum should be update= d
to PageNum - (2 * EFI_SIZE_TO_PAGES(0x10000)) so that we wo= n't get a
wrong PageNum to allocate a memory with a size larg= er than available
space's size.

= With this solution, the example above will get 0x696A8 as calculated
PageNum. Then, in BS.AllocatePages, the PageNum will be changed fro= m
0x696A8 to 0x696B0. Therefore, the end address will become = 0xEB6D0000
that is smaller than 0xEB6D9FFF, so we get not fou= nd error in the end.

I also tested this soluti= on on two ARM platforms (NXP1046A and RPi4).

C= c: Samer El-Haj-Mahmoud <samer.el-haj-mahmoud@arm.com>
Cc: G Edhaya= Chandran <edhaya.= chandran@arm.com>
Cc: Barton Gao <gaojie@byosoft.com.cn>
Signed-off-by: Sunny Wang <sunny.wang@arm.com>
---
.../= MemoryAllocationServicesBBTestFunction.c  | 110 +++++++++++-------
1 file changed, 66 insertions(+), 44 deletions(-)

diff --git a/uefi-sct/SctPkg/TestCase/UEFI/EFI/BootServices= /MemoryAllocationServices/BlackBoxTest/MemoryAllocationServicesBBTestFuncti= on.c b/uefi-sct/SctPkg/TestCase/UEFI/EFI/BootServices/MemoryAllocationServi= ces/BlackBoxTest/MemoryAllocationServicesBBTestFunction.c
ind= ex bf8cd3b3..cdfac992 100644
--- a/uefi-sct/SctPkg/TestCase/U= EFI/EFI/BootServices/MemoryAllocationServices/BlackBoxTest/MemoryAllocation= ServicesBBTestFunction.c
+++ b/uefi-sct/SctPkg/TestCase/UEFI/= EFI/BootServices/MemoryAllocationServices/BlackBoxTest/MemoryAllocationServ= icesBBTestFunction.c
@@ -2,6 +2,7 @@

  Copyright 2006 - 2013 Unified EFI, Inc.<BR>
  Copyright (c) 2010 - 2013, Intel Corporation. All rig= hts reserved.<BR>
+  Copyright (c) 2021, ARM Limit= ed. All rights reserved.

  This pro= gram and the accompanying materials
  are licensed= and made available under the terms and conditions of the BSD License
@@ -24,7 +25,7 @@ Abstract:

--*/

-#include "SctLib.h"
+#in= clude "SctLib.h"
#include "MemoryAllocationSe= rvicesBBTestMain.h"

#define ALLOCATEPAGE= S_MEMORYTYPE_NUM 16
@@ -700,14 +701,17 @@ BBTestAllocatePages= InterfaceTest (
       &n= bsp;PageNum =3D (UINTN)Descriptor.NumberOfPages;
  = ;      Start   =3D Descriptor.Physi= calStart;

-      &nbs= p; //
-        // Som= e memory types need more alignment than 4K, so
-   =      //
-     &n= bsp;  if (PageNum <=3D 0x10) {
+   &nb= sp;    //
+      = ;  // Calculate New Start address and PageNum with 64k alignment = to
+        // cover the c= ase that some memory types' alignment is more than
+  &n= bsp;     // 4k. If the available memory is less th= an 192k, the memory
+       &nb= sp;// allocation call will be skipped.
+    &n= bsp;   //
+      &nbs= p; if (PageNum < (3 * EFI_SIZE_TO_PAGES(0x10000))) {
=           break;
        }
- &= nbsp;      Start   =3D (Start + 0xF= FFF) & 0xFFFFFFFFFFFF0000;
-     &nbs= p;  PageNum =3D PageNum - EFI_SIZE_TO_PAGES(0x10000);
+        Start   =3D (Start= + 0xFFFF) & 0xFFFFFFFFFFFF0000;
+    &nbs= p;   PageNum =3D PageNum - (2 * EFI_SIZE_TO_PAGES(0x10000));=

       &n= bsp;Memory  =3D Start;

@@ -830,14 +834,17= @@ BBTestAllocatePagesInterfaceTest (
   &nb= sp;    PageNum =3D (UINTN)Descriptor.NumberOfPages;
        Start  &nb= sp;=3D Descriptor.PhysicalStart;

-   = ;     //
-     &= nbsp;  // Some memory types need more alignment than 4K, so
-        //
- &nb= sp;      if (PageNum <=3D 0x10) {
+        //
+  = ;      // Calculate New Start address and Pag= eNum with 64k alignment to
+      &n= bsp; // cover the case that some memory types' alignment is more than<= br class=3D"">+        // 4k. If the ava= ilable memory is less than 192k, the memory
+   &nb= sp;    // allocation call will be skipped.
+        //
+  &nbs= p;     if (PageNum < (3 * EFI_SIZE_TO_PAGES(0x1= 0000))) {
        &n= bsp; break;
       &= nbsp;}
-        Start &nbs= p; =3D (Start + 0xFFFF) & 0xFFFFFFFFFFFF0000;
- &nbs= p;      PageNum =3D PageNum - EFI_SIZE_TO_PAG= ES(0x10000);
+        Star= t   =3D (Start + 0xFFFF) & 0xFFFFFFFFFFFF0000;
= +        PageNum =3D PageNum - (2 * EFI_= SIZE_TO_PAGES(0x10000));

   &n= bsp;    Memory  =3D Start;

@@ -953,14 +960,17 @@ BBTestAllocatePagesInterfaceTest (
        PageNum =3D (UINTN)Desc= riptor.NumberOfPages;
      &n= bsp; Start   =3D Descriptor.PhysicalStart;
-        //
- =        // Some memory types need more al= ignment than 4K, so
-       &nb= sp;//
-        if (PageNum= <=3D 0x10) {
+        = //
+        // Calculate N= ew Start address and PageNum with 64k alignment to
+  &n= bsp;     // cover the case that some memory types'= alignment is more than
+       = ; // 4k. If the available memory is less than 192k, the memory
+        // allocation call will = be skipped.
+        //+        if (PageNum < (3= * EFI_SIZE_TO_PAGES(0x10000))) {
    &n= bsp;     break;
   &= nbsp;    }
-     &nbs= p;  Start   =3D (Start + 0xFFFF) & 0xFFFFFFFFFFFF00= 00;
-        PageNum =3D P= ageNum - EFI_SIZE_TO_PAGES(0x10000);
+    &nbs= p;   Start   =3D (Start + 0xFFFF) & 0xFFFFFFFF= FFFF0000;
+        PageNum= =3D PageNum - (2 * EFI_SIZE_TO_PAGES(0x10000));

        Memory =3D Start + (Sct= LShiftU64 (PageNum/3, EFI_PAGE_SHIFT) & 0xFFFFFFFFFFFF0000);

@@ -1076,14 +1086,17 @@ BBTestAllocatePagesInterfaceT= est (
        PageNu= m =3D (UINTN)Descriptor.NumberOfPages;
   &nb= sp;    Start   =3D Descriptor.PhysicalStart;<= br class=3D"">
-        //=
-        // Some memory t= ypes need more alignment than 4K, so
-    &nbs= p;   //
-       =  if (PageNum <=3D 0x10) {
+     &= nbsp;  //
+       &nb= sp;// Calculate New Start address and PageNum with 64k alignment to
+        // cover the case that s= ome memory types' alignment is more than
+    =     // 4k. If the available memory is less than 192k, t= he memory
+        // allo= cation call will be skipped.
+      =   //
+        if= (PageNum < (3 * EFI_SIZE_TO_PAGES(0x10000))) {
 &nb= sp;        break;
&n= bsp;       }
-   = ;     Start   =3D (Start + 0xFFFF) &= 0xFFFFFFFFFFFF0000;
-       &n= bsp;PageNum =3D PageNum - EFI_SIZE_TO_PAGES(0x10000);
+  = ;      Start   =3D (Start + 0xFFFF)= & 0xFFFFFFFFFFFF0000;
+      &n= bsp; PageNum =3D PageNum - (2 * EFI_SIZE_TO_PAGES(0x10000));

        Memo= ry  =3D Start + (SctLShiftU64 (PageNum * 2 / 3, EFI_PAGE_SHIFT) & = 0xFFFFFFFFFFFF0000);

@@ -1206,14 +1219,17 @@ B= BTestAllocatePagesInterfaceTest (
    &n= bsp;   PageNum =3D (UINTN)Descriptor.NumberOfPages;
        Start   = =3D Descriptor.PhysicalStart;

-   &= nbsp;    //
-     &nb= sp;  // Some memory types need more alignment than 4K, so
-        //
-  = ;      if (PageNum <=3D 0x10) {
+        //
+  = ;      // Calculate New Start address and Pag= eNum with 64k alignment to
+      &n= bsp; // cover the case that some memory types' alignment is more than<= br class=3D"">+        // 4k. If the ava= ilable memory is less than 192k, the memory
+   &nb= sp;    // allocation call will be skipped.
+        //
+  &nbs= p;     if (PageNum < (3 * EFI_SIZE_TO_PAGES(0x1= 0000))) {
        &n= bsp; break;
       &= nbsp;}
-        Start &nbs= p; =3D (Start + 0xFFFF) & 0xFFFFFFFFFFFF0000;
- &nbs= p;      PageNum =3D PageNum - EFI_SIZE_TO_PAG= ES(0x10000);
+        Star= t   =3D (Start + 0xFFFF) & 0xFFFFFFFFFFFF0000;
= +        PageNum =3D PageNum - (2 * EFI_= SIZE_TO_PAGES(0x10000));

   &n= bsp;    Memory  =3D Start;

@@ -1329,14 +1345,17 @@ BBTestAllocatePagesInterfaceTest (
        PageNum =3D (UINTN)= Descriptor.NumberOfPages;
     &nbs= p;  Start   =3D Descriptor.PhysicalStart;

-        //
-        // Some memory types need mor= e alignment than 4K, so
-       = ; //
-        if (Pag= eNum <=3D 0x10) {
+       &n= bsp;//
+        // Calcula= te New Start address and PageNum with 64k alignment to
+ &nbs= p;      // cover the case that some memory ty= pes' alignment is more than
+      &= nbsp; // 4k. If the available memory is less than 192k, the memory
+        // allocation call w= ill be skipped.
+        /= /
+        if (PageNum <= ; (3 * EFI_SIZE_TO_PAGES(0x10000))) {
   &nbs= p;      break;
  &nb= sp;     }
-     =    Start   =3D (Start + 0xFFFF) & 0xFFFFFFFFFF= FF0000;
-        PageNum = =3D PageNum - EFI_SIZE_TO_PAGES(0x10000);
+   &nbs= p;    Start   =3D (Start + 0xFFFF) & 0xFF= FFFFFFFFFF0000;
+        P= ageNum =3D PageNum - (2 * EFI_SIZE_TO_PAGES(0x10000));

        Memory  =3D = Start;

@@ -1468,14 +1487,17 @@ BBTestAllocateP= agesInterfaceTest (
      &nbs= p; PageNum =3D (UINTN)Descriptor.NumberOfPages;
 &= nbsp;      Start   =3D Descriptor.P= hysicalStart;

-      =   //
-        //= Some memory types need more alignment than 4K, so
-  &n= bsp;     //
-    &nbs= p;   if (PageNum <=3D 0x10) {
+   = ;     //
+     &= nbsp;  // Calculate New Start address and PageNum with 64k alignm= ent to
+        // cover t= he case that some memory types' alignment is more than
+ &nbs= p;      // 4k. If the available memory is les= s than 192k, the memory
+       = ; // allocation call will be skipped.
+   &nbs= p;    //
+      =   if (PageNum < (3 * EFI_SIZE_TO_PAGES(0x10000))) {
          break;<= br class=3D"">         }
-        Start   =3D (S= tart + 0xFFFF) & 0xFFFFFFFFFFFF0000;
-    =     PageNum =3D PageNum - EFI_SIZE_TO_PAGES(0x10000);+        Start   = = =3D (Start + 0xFFFF) & 0xFFFFFFFFFFFF0000;
+   = ;     PageNum =3D PageNum - (2 * EFI_SIZE_TO_PAGES= (0x10000));

     &nb= sp;  Memory  =3D Start;

@@ -192= 3,4 +1945,4 @@ BBTestFreePoolInterfaceTest (

=   FreeMemoryMap ();
  return EFI_SUCCESS= ;
-}
+}
--
2.31.0.= windows.1







--Apple-Mail=_2A71B675-0D49-408E-8878-676045C6AA6A--