From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from NAM11-CO1-obe.outbound.protection.outlook.com (NAM11-CO1-obe.outbound.protection.outlook.com [40.107.220.130]) by mx.groups.io with SMTP id smtpd.web08.4055.1607505879409350968 for ; Wed, 09 Dec 2020 01:24:39 -0800 Authentication-Results: mx.groups.io; dkim=pass header.i=@os.amperecomputing.com header.s=selector2 header.b=JrEddu5E; spf=pass (domain: os.amperecomputing.com, ip: 40.107.220.130, mailfrom: nhi@os.amperecomputing.com) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=HwJd7UZLn/NYoD4QXGqbFnaSo+HYS0P7FHzO6snHBg+qI1FxG+qV9di71EIzFFkqppqbbDgp1ukIqAzyLmsD5s0dwMr4XfLYNz7qYfTTlQn0WimljPvqa2E2dGaHZEWcKagKTGneMkHBcFInvmot8O2I1EnEthr4bTQamXsUDUrp+yjCEaADRpMiw5YdmLF4nva8Ees7BVPfACRkHRLIAiF0MrTWCKXRjtmXLAAJtnYIATQFDQaz0EI3nrV59a63/HySllQowqovG49UWcu91t52c3m0qKE2zu8nld3oNqlvL2xbRdJ8RoE5XXb/0GyP+Llbi2EOxnqiVpyEEtDM1Q== 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=XyyARwMQeZCxK70n6oHgoqiUqO8ReyU1wbErxqUrlK8=; b=YhanaZAMcJl3rUBGuisQlV4P4ar0p+dSCahIc7EodcrHSk0LrtHZnrDcTTRLc7O85iwpl8zGGBeOmTZxQW/3ZN3hPu12TfcprLnTBIImRcGFu1mYF2HfbtdjrOEJWrRSw4dwQcTOxZKE9/8iO+EFO4ya/BtUiMXSGZu0fQZ1qAhgxD1wJB0m/DHHagFrpDOxwqFeFP0nI7vVDXFbrCEvnxsntJodA6djQ15vz/ifo4P9hYxkLuVGN9n9oGeOGiWOsO+q1vazpnOYFGN3DLHm1hBcZzyAoN5OphrORV03lqTynvftjN0hfRCnqRXihLZKXRqG4yHBZncg9JfqKVCf5g== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=os.amperecomputing.com; dmarc=pass action=none header.from=os.amperecomputing.com; dkim=pass header.d=os.amperecomputing.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=os.amperecomputing.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=XyyARwMQeZCxK70n6oHgoqiUqO8ReyU1wbErxqUrlK8=; b=JrEddu5EQhD1auUhsjltewHHoohPSspe4XqfrY/a4FgoV7Loc+T3Fpymo5hQ2aVmGg7sRdm9rzZPv0YBc7sPprf9S04JPwzizEeMz4xW9jSUOYibJz+tTu6JoUhDzIZ2jG9699fzVAGUkgxWTb5y+dA4TL399QnVHL36p8gzfD4= Authentication-Results: edk2.groups.io; dkim=none (message not signed) header.d=none;edk2.groups.io; dmarc=none action=none header.from=os.amperecomputing.com; Received: from DM6PR01MB5849.prod.exchangelabs.com (2603:10b6:5:205::20) by DM6PR01MB5609.prod.exchangelabs.com (2603:10b6:5:157::25) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3632.21; Wed, 9 Dec 2020 09:24:38 +0000 Received: from DM6PR01MB5849.prod.exchangelabs.com ([fe80::c814:9a08:5c2e:4076]) by DM6PR01MB5849.prod.exchangelabs.com ([fe80::c814:9a08:5c2e:4076%5]) with mapi id 15.20.3632.023; Wed, 9 Dec 2020 09:24:38 +0000 From: "Nhi Pham" To: devel@edk2.groups.io Cc: Nhi Pham Subject: [edk2-platforms][PATCH 18/34] Silicon/Ampere: Fixup runtime memory attribute Date: Wed, 9 Dec 2020 16:25:15 +0700 Message-Id: <20201209092531.30867-19-nhi@os.amperecomputing.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20201209092531.30867-1-nhi@os.amperecomputing.com> References: <20201209092531.30867-1-nhi@os.amperecomputing.com> X-Originating-IP: [118.69.219.201] X-ClientProxiedBy: HK0PR01CA0054.apcprd01.prod.exchangelabs.com (2603:1096:203:a6::18) To DM6PR01MB5849.prod.exchangelabs.com (2603:10b6:5:205::20) Return-Path: nhi@os.amperecomputing.com MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from sw004.amperecomputing.com (118.69.219.201) by HK0PR01CA0054.apcprd01.prod.exchangelabs.com (2603:1096:203:a6::18) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3654.12 via Frontend Transport; Wed, 9 Dec 2020 09:24:37 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 6423462f-45b5-4df4-fda9-08d89c244037 X-MS-TrafficTypeDiagnostic: DM6PR01MB5609: X-MS-Exchange-Transport-Forked: True X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:9508; X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: JhX84rSuegOICemZVTIkIqktI8V+RZANya8guyt7cs8ehteh++m4Rzle9uiA/JlJRA05JdDKpIODXcs//gmraJ2XNS9b0THMAHrhrLn+G2g4X3ALuqd9YXclIqmKuTusBSRTHxnWEtxQLlML1TgDSzuCRB5u5j5pEtwyObwWM+bXsFu4lCdhU/Z13ac3d78QNzOhLD1l7KPjI+UP3e72jRh++lG6WFxEFnl4VDvIu6X020d324BmJCqZlAe4oFYpYZkfl+qK/crEnq9O7Ixw5WvroyFJTV+wDFS5QQEzaNpWG+MeVEsF2ZTEyrkvnPH+auzD8UN0+vdVyydhPBG59Zhn/SBqR8XUHF20FoCyMW8DC+SFADEXrSKUusOGx1A2 X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:DM6PR01MB5849.prod.exchangelabs.com;PTR:;CAT:NONE;SFS:(4636009)(376002)(136003)(346002)(366004)(5660300002)(956004)(508600001)(52116002)(66946007)(6916009)(2616005)(1076003)(19627235002)(86362001)(6512007)(66476007)(26005)(8936002)(16526019)(107886003)(6666004)(186003)(6486002)(8676002)(6506007)(2906002)(34490700003)(66556008)(4326008);DIR:OUT;SFP:1102; X-MS-Exchange-AntiSpam-MessageData: =?us-ascii?Q?1BQ/+7P69j9q9OdWwZBJuTkKwHR9cS5tpu+0IKpib1YlKpxr/ptuWYG+YedO?= =?us-ascii?Q?JXXmiFrD4CadW8psAw9lly1mIFrMLNdvv22IYmxeLSbZr0Vwj4ORz6itvOXt?= =?us-ascii?Q?B0Wi7L7VngUiVnaVBaexBaDuBap1b2ETxDe4fG0scQnKeD4N5TUXnBHbcPCc?= =?us-ascii?Q?Z/fty0mpsISOHEVkngEFvixVi4mlEBHqss6AXxYVIRUImIGJzwp+5OusZzN3?= =?us-ascii?Q?3FM/1iPlaFic1aPOxGiHGzI0IfX46uynjOTmnaTMCks7gOP34W8gMy7VmzC+?= =?us-ascii?Q?4SIK8J5QKJEtGbgEYcX2O0B9GHtX/gdn4cUXORhXInQmxwXzl7luWTOQ5+EH?= =?us-ascii?Q?pU+QwSVNmo023eehXtHWk7exCPWalK8hDRW6KmIoDNdKTonE/bdFASEqQM2a?= =?us-ascii?Q?96BdTcBQ3VT+aKi/0nXDMM6S1V+4voHoKm48tl/kiIlz2UZPH0sLZqn2Fgpt?= =?us-ascii?Q?7RFfG20OAehwFT2/qTAROPgI/BRUavM1StjO7etvSlPWWgkhxyEqzlUcxYL4?= =?us-ascii?Q?+MX0ME77ynZoc09dpvS0i+5urYJBjspVHfI60d7u0zK64F79nUpT8ejo6mOR?= =?us-ascii?Q?UrscL99+SFEuLtM7ge2Tlw3WZHdj5FWLllwX1FCtWkl10I/rU5mUFoH9vA4z?= =?us-ascii?Q?4xTpei1+zhoFNQNHQe9TjuD0haBCkzA34ieiPfkPogvzqq7AXPUtJlnUq1vY?= =?us-ascii?Q?FiGgYuqztysFUaWmrACmkB2mpXdDXZefiG1DC4tIhm0vmkKdznRjLwE/A5sO?= =?us-ascii?Q?sy9JbEEg7dUiX9kc4+9oZAV+DVeh+sJ9pIpM/MCUhgvM7H/aglswYuhqCrRU?= =?us-ascii?Q?+pXLNNkDzm/ahUn4H2khBOuH5tBwuKPSyOh4RjA9DYpkn2LHpMga9Xq+B0v7?= =?us-ascii?Q?RAhHSprnXOpgdA33cEa/gBKiwewbNT2SaYv+If10wW1aP0EpvwNd2K7z5OhM?= =?us-ascii?Q?DuORs6q+S01OjLbyVBpFfnfbqAYNRkIDF21Ic8JiuqmUcpwvvRrWt9cqKkst?= =?us-ascii?Q?t3xl?= X-OriginatorOrg: os.amperecomputing.com X-MS-Exchange-CrossTenant-AuthSource: DM6PR01MB5849.prod.exchangelabs.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 09 Dec 2020 09:24:38.2302 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 3bc2b170-fd94-476d-b0ce-4229bdc904a7 X-MS-Exchange-CrossTenant-Network-Message-Id: 6423462f-45b5-4df4-fda9-08d89c244037 X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: MMKhqwyOs7ijrbC/5U0FZ4qAkLwu835fLaGrjCdjjw2xFOAMoby4EZ2trSUxbn6W3z+dUlhKfV4pipXxkLUtsznhjDk6Hy+lcn4eDTwxYL0= X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM6PR01MB5609 Content-Type: text/plain This adds new FixupMemoryMap module to scan the runtime memory and apply the XP memory attribute as OS does not complain about missing RO and XP attributes. Signed-off-by: Nhi Pham --- Silicon/Ampere/AmpereAltraPkg/Ac01Pkg.dsc.inc | 5 ++ Platform/Ampere/JadePkg/Jade.fdf | 5 ++ Silicon/Ampere/Drivers/FixupMemoryMap/FixupMemoryMap.inf | 41 +++++++++ Silicon/Ampere/Drivers/FixupMemoryMap/FixupMemoryMap.c | 93 ++++++++++++++++++++ 4 files changed, 144 insertions(+) diff --git a/Silicon/Ampere/AmpereAltraPkg/Ac01Pkg.dsc.inc b/Silicon/Ampere/AmpereAltraPkg/Ac01Pkg.dsc.inc index cb03f7fc076f..746db5052c16 100755 --- a/Silicon/Ampere/AmpereAltraPkg/Ac01Pkg.dsc.inc +++ b/Silicon/Ampere/AmpereAltraPkg/Ac01Pkg.dsc.inc @@ -574,6 +574,11 @@ [Components.common] EmbeddedPkg/MetronomeDxe/MetronomeDxe.inf Silicon/Ampere/AmpereAltraPkg/Drivers/MmCommunicationDxe/MmCommunication.inf + # + # Fix Memory Map + # + Silicon/Ampere/Drivers/FixupMemoryMap/FixupMemoryMap.inf + # # Environment Variables Protocol # diff --git a/Platform/Ampere/JadePkg/Jade.fdf b/Platform/Ampere/JadePkg/Jade.fdf index f826b14d67d0..100d5bf6f80c 100755 --- a/Platform/Ampere/JadePkg/Jade.fdf +++ b/Platform/Ampere/JadePkg/Jade.fdf @@ -234,6 +234,11 @@ [FV.FvMain] INF MdeModulePkg/Universal/HiiDatabaseDxe/HiiDatabaseDxe.inf INF Silicon/Ampere/AmpereAltraPkg/Drivers/MmCommunicationDxe/MmCommunication.inf + # + # Fixup memory map + # + INF Silicon/Ampere/Drivers/FixupMemoryMap/FixupMemoryMap.inf + # # Environment Variables Protocol # diff --git a/Silicon/Ampere/Drivers/FixupMemoryMap/FixupMemoryMap.inf b/Silicon/Ampere/Drivers/FixupMemoryMap/FixupMemoryMap.inf new file mode 100644 index 000000000000..773e0dcd64ec --- /dev/null +++ b/Silicon/Ampere/Drivers/FixupMemoryMap/FixupMemoryMap.inf @@ -0,0 +1,41 @@ +## @file +# +# Copyright (c) 2020, Ampere Computing LLC. All rights reserved.
+# +# SPDX-License-Identifier: BSD-2-Clause-Patent +# +## + +[Defines] + INF_VERSION = 0x0001001B + BASE_NAME = FixupMemoryMap + FILE_GUID = 183CEBEC-1229-4F59-B4F9-3BA1EC27B168 + MODULE_TYPE = DXE_DRIVER + VERSION_STRING = 1.0 + ENTRY_POINT = FixupMemoryMapInitialize + +[Sources.common] + FixupMemoryMap.c + +[Packages] + MdePkg/MdePkg.dec + MdeModulePkg/MdeModulePkg.dec + ArmPkg/ArmPkg.dec + ArmPlatformPkg/ArmPlatformPkg.dec + +[LibraryClasses] + BaseLib + DebugLib + UefiLib + UefiDriverEntryPoint + UefiBootServicesTableLib + DxeServicesTableLib + +[Guids] + gEfiMemoryAttributesTableGuid ## CONSUMES ## SystemTable + +[Protocols] + gEfiCpuArchProtocolGuid + +[Depex] + TRUE diff --git a/Silicon/Ampere/Drivers/FixupMemoryMap/FixupMemoryMap.c b/Silicon/Ampere/Drivers/FixupMemoryMap/FixupMemoryMap.c new file mode 100644 index 000000000000..9fb7d025c64c --- /dev/null +++ b/Silicon/Ampere/Drivers/FixupMemoryMap/FixupMemoryMap.c @@ -0,0 +1,93 @@ +/** @file + + Copyright (c) 2020, Ampere Computing LLC. All rights reserved.
+ + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +EFI_CPU_ARCH_PROTOCOL *mCpu; + +/** + Notify function for event group EVT_SIGNAL_EXIT_BOOT_SERVICES. + + @param[in] Event The Event that is being processed. + @param[in] Context The Event Context. + +**/ +VOID +EFIAPI +OnExitBootServices( + IN EFI_EVENT Event, + IN VOID *Context + ) +{ + EFI_STATUS Status; + EFI_MEMORY_ATTRIBUTES_TABLE *MemoryAttributesTable; + EFI_MEMORY_DESCRIPTOR *Desc; + UINTN Index; + + DEBUG ((DEBUG_INFO, "%a:%d +\n", __FUNCTION__, __LINE__)); + + Status = gBS->LocateProtocol (&gEfiCpuArchProtocolGuid, NULL, (VOID **) &mCpu); + if (EFI_ERROR (Status)) { + DEBUG ((DEBUG_INFO, "%a:%d -\n", __FUNCTION__, __LINE__)); + return; + } + + Status = EfiGetSystemConfigurationTable (&gEfiMemoryAttributesTableGuid, (VOID **) &MemoryAttributesTable); + if (EFI_ERROR (Status) || MemoryAttributesTable == NULL) { + DEBUG ((DEBUG_INFO, "%a:%d -\n", __FUNCTION__, __LINE__)); + return; + } + + Desc = (EFI_MEMORY_DESCRIPTOR *) ((UINT64) MemoryAttributesTable + sizeof (EFI_MEMORY_ATTRIBUTES_TABLE)); + for (Index = 0; Index < MemoryAttributesTable->NumberOfEntries; Index++) { + if (Desc->Type != EfiRuntimeServicesCode && Desc->Type != EfiRuntimeServicesData) { + Desc = (EFI_MEMORY_DESCRIPTOR *)((UINT64) Desc + MemoryAttributesTable->DescriptorSize); + continue; + } + + if (!(Desc->Attribute & (EFI_MEMORY_RO | EFI_MEMORY_XP))) { + Desc->Attribute |= EFI_MEMORY_XP; + mCpu->SetMemoryAttributes (mCpu, Desc->PhysicalStart, EFI_PAGES_TO_SIZE (Desc->NumberOfPages), Desc->Attribute); + DEBUG ((DEBUG_INFO, "%a: Set memory attribute, Desc->PhysicalStart=0x%X, size=%d, Attributes=0x%X\n", + __FUNCTION__, Desc->PhysicalStart, EFI_PAGES_TO_SIZE (Desc->NumberOfPages), Desc->Attribute)); + } + Desc = (EFI_MEMORY_DESCRIPTOR *)((UINT64) Desc + MemoryAttributesTable->DescriptorSize); + } + + DEBUG ((DEBUG_INFO, "%a:%d -\n", __FUNCTION__, __LINE__)); +} + +EFI_STATUS +EFIAPI +FixupMemoryMapInitialize ( + IN EFI_HANDLE ImageHandle, + IN EFI_SYSTEM_TABLE *SystemTable + ) +{ + EFI_EVENT ExitBootServicesEvent; + EFI_STATUS Status; + + Status = gBS->CreateEvent ( + EVT_SIGNAL_EXIT_BOOT_SERVICES, + TPL_NOTIFY, + OnExitBootServices, + NULL, + &ExitBootServicesEvent); + ASSERT_EFI_ERROR (Status); + + return EFI_SUCCESS; +} -- 2.17.1