From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mga12.intel.com (mga12.intel.com [192.55.52.136]) by mx.groups.io with SMTP id smtpd.web11.3978.1685428339922288430 for ; Mon, 29 May 2023 23:32:20 -0700 Authentication-Results: mx.groups.io; dkim=fail reason="unable to parse pub key" header.i=@intel.com header.s=intel header.b=FLChopWi; spf=pass (domain: intel.com, ip: 192.55.52.136, mailfrom: ray.ni@intel.com) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1685428339; x=1716964339; h=from:to:cc:subject:date:message-id:references: in-reply-to:content-transfer-encoding:mime-version; bh=SFjLM3zrqtSozA/6x16mkHyN2xRVuEymm5mrPGBJwm4=; b=FLChopWi+b5l4jd3e747fKMrPkXx5kc9yWdbrYj9MJ7mQ+T15vmSiKDF 1ad2+abxLgjmXPXjxOdW9tt7VqHKJiDo3UmcC51EyhxBRSPpLJsioRXEJ yi7/DMCH8ZbKEtedpSxObDGb70XZnMMky0fpg0kBAkkDEnBibSstyBKXv GH5Hwp0WF2eeXf7L93JDx49Lh45jAhf/imb6HE7IG4tMmOGl+ebYjUovK AAS7HlgON/S/nBTLtzlKUbMQYqI5t+aK+W94fy6BLRKHlKG60rmUe+ZdP pGOz4gbDi2f9qnIhVXgJF5TxT/yJhTWqebcuJgIRP2Cg3rnhZIBReINgz Q==; X-IronPort-AV: E=McAfee;i="6600,9927,10725"; a="334451086" X-IronPort-AV: E=Sophos;i="6.00,203,1681196400"; d="scan'208";a="334451086" Received: from orsmga006.jf.intel.com ([10.7.209.51]) by fmsmga106.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 29 May 2023 23:32:18 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10725"; a="683819949" X-IronPort-AV: E=Sophos;i="6.00,203,1681196400"; d="scan'208";a="683819949" Received: from orsmsx602.amr.corp.intel.com ([10.22.229.15]) by orsmga006.jf.intel.com with ESMTP; 29 May 2023 23:32:18 -0700 Received: from orsmsx612.amr.corp.intel.com (10.22.229.25) by ORSMSX602.amr.corp.intel.com (10.22.229.15) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.23; Mon, 29 May 2023 23:32:17 -0700 Received: from orsmsx612.amr.corp.intel.com (10.22.229.25) by ORSMSX612.amr.corp.intel.com (10.22.229.25) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.23; Mon, 29 May 2023 23:32:17 -0700 Received: from orsedg603.ED.cps.intel.com (10.7.248.4) by orsmsx612.amr.corp.intel.com (10.22.229.25) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.23 via Frontend Transport; Mon, 29 May 2023 23:32:17 -0700 Received: from NAM11-CO1-obe.outbound.protection.outlook.com (104.47.56.177) by edgegateway.intel.com (134.134.137.100) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.1.2507.23; Mon, 29 May 2023 23:32:16 -0700 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=biuFxFdC2eBOrWIfpJktmar0UPd1qfKiOsZUGCL/VFNW6FO4W/zpxxsYJpqFC+WUqSimDK4ryMwreLCCNcQJvb6qsffSZKNn0W0S/ZPg2RBovDPDoeD+vZPCRC/1r2ZUQNDxkimm/Kln+RJb17LbfLWW+A1VucSEcxYU3yr0tfCxUbkXPC5cg1El713OJKGE4Ox1GnLA9fPe7w9oYCf8qz2S3TUtqzpRtOEW7VgzlLSb1gDqNcXhJuwElAK9EFtXvXa8aqI13Gkl3CXhLSrs5TIIrJGx+ELxHSQz/8Zi9ggG5FkXCIyEg+Ba1MljxfO3f3O8P0MiQTEChBC0lqh3vQ== 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-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=cuOyrLan25KdKUnA0lJ4AIafMtwq8YaQ/37clsYGAo0=; b=BkVhCzR9BWpQvCDdRcJX5Ler7JEIImqsd0Y5yOfn2ijQyGTQbnQY7jC4T4AErnUmYVpOs0+n4nGboeVZ6JN5ThHNX91hH5gLLrPxm6iRexr/2wa/VQBc0PozNf9BRrOkBVBDPTZUZZvibH31zkUm3CwZcUh80hU7TYPEhCswrCdyyX3axDo026Lxc523jSes0EiUfBvjkJZBx5DwbUa1IoI1WboCo0JD17mKisabnXOqCb1JPxZODlxMJ2qZpOqc6vNDXw7Apkd3iDNJYisp25igHXWiWmu85RAx+IP3KH4MzAFpmyHYZIO8GuvGlBb4ayyOi4zo5J2UynmJY6J+Nw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=intel.com; dmarc=pass action=none header.from=intel.com; dkim=pass header.d=intel.com; arc=none Received: from MN6PR11MB8244.namprd11.prod.outlook.com (2603:10b6:208:470::14) by SJ2PR11MB8539.namprd11.prod.outlook.com (2603:10b6:a03:56e::14) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6433.23; Tue, 30 May 2023 06:32:13 +0000 Received: from MN6PR11MB8244.namprd11.prod.outlook.com ([fe80::892b:b8e6:bab7:635d]) by MN6PR11MB8244.namprd11.prod.outlook.com ([fe80::892b:b8e6:bab7:635d%5]) with mapi id 15.20.6433.018; Tue, 30 May 2023 06:32:12 +0000 From: "Ni, Ray" To: Ard Biesheuvel , "devel@edk2.groups.io" CC: "Yao, Jiewen" , Gerd Hoffmann , Taylor Beebe , Oliver Smith-Denny , "Bi, Dandan" , "Gao, Liming" , "Kinney, Michael D" , Leif Lindholm , Michael Kubacki Subject: Re: [RFC PATCH 07/11] MdeModulePkg/DxeCore: Execute loaded images in place if possible Thread-Topic: [RFC PATCH 07/11] MdeModulePkg/DxeCore: Execute loaded images in place if possible Thread-Index: AQHZkhbZ+Oh+QHgL2kq1gSMziKtpI69yWy4g Date: Tue, 30 May 2023 06:32:11 +0000 Message-ID: References: <20230529101705.2476949-1-ardb@kernel.org> <20230529101705.2476949-8-ardb@kernel.org> In-Reply-To: <20230529101705.2476949-8-ardb@kernel.org> Accept-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: authentication-results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=intel.com; x-ms-publictraffictype: Email x-ms-traffictypediagnostic: MN6PR11MB8244:EE_|SJ2PR11MB8539:EE_ x-ms-office365-filtering-correlation-id: e80c70cf-fb6f-4cbf-ceea-08db60d79998 x-ld-processed: 46c98d88-e344-4ed4-8496-4ed7712e255d,ExtAddr x-ms-exchange-senderadcheck: 1 x-ms-exchange-antispam-relay: 0 x-microsoft-antispam: BCL:0; x-microsoft-antispam-message-info: ponT7NOEirk/FGOqv1vFjAC/i5OECzqRKpdk9O+u7y4sqnb97AIoOC2uaujqBbWN9Ou6gShLJZgbD3zf/CcxXiWGrFUQwaC2Ow8dNFgQ+EyE4C02KANASuC5veNhnhqLqTLZFM0B1p9Bvddk6m9s22Y/3JFZmyioGjplgJEjGqq7TUVLeXdF34C22qus4/ZXn2PVOVCTVW/2I4ggw1eCu7UKKJfMBFpcBVhnuqqKP5arwtqnINmLv3jfgOlHkz8BuYMh8BPlPHLvARq7PDQ5UcrvEHOgGu2fKFZq80M0DhiurUHG/sbeE+gjDLL18bTEdlG2+MtAzvHzefo+172KAQ3hNXnAX1ROnuGx4kRptKd70Hnb81RcJ1CT9hLfevef8mTaMzR5gM9+CIZD+GaPsSGfA4FPJ3wOhvRTXxOxg3z88MN1LHSmM4qTuScHDWksDepTqoZXPaxSQ4HHwhkttei6nYT6C13dlKtE/bC77z6vedTfePStgI/x6eVxosbbMmrOVzkigGABmaGvxZR1F+zuMmae9BtO3sB453i262ywgIZOUWRrmI7UG//iwNScRcdwwjDVWFpjy/FkUpIbHx3t/Q6+cqmlwSV54/ApR5HoHqdguZ37dBZVAem8l9S7 x-forefront-antispam-report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:MN6PR11MB8244.namprd11.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230028)(376002)(396003)(366004)(346002)(136003)(39860400002)(451199021)(82960400001)(122000001)(478600001)(38100700002)(86362001)(38070700005)(66446008)(66946007)(76116006)(66556008)(83380400001)(66476007)(64756008)(54906003)(110136005)(4326008)(71200400001)(2906002)(9686003)(53546011)(7696005)(33656002)(186003)(26005)(316002)(6506007)(41300700001)(5660300002)(52536014)(8676002)(8936002)(55016003);DIR:OUT;SFP:1102; x-ms-exchange-antispam-messagedata-chunkcount: 1 x-ms-exchange-antispam-messagedata-0: =?us-ascii?Q?/F9AEZBtxM5hdRNUFR2A4wtdyE3+nBJ2zsBi7eBAmAO9UTG+iePKifrMZnF8?= =?us-ascii?Q?4PGZrtV5UHsUI6dtjGxWPv8eIXPE2O2WuNHUUfGqyp25E1A/xrHJdA62JJzZ?= =?us-ascii?Q?UXOp29UTL0nwlEIjopo8k94ZirQMGOrg8h0kyMcxrB1O8OALXKVuGpn4GTg9?= =?us-ascii?Q?Xj+dKTcpS2UIRgZfxfR79OScXnjPfCgjA5X5t09A31Pw7Gyttz3FlHLUlzp+?= =?us-ascii?Q?qHPB/Me7wmltlRq6N8txMMnmIaGiOSHaRjcln+65RHBahAkhsgisJf3n36ra?= =?us-ascii?Q?Ss4rHzLi5QwnedOZWMPMAhO4tr+sh3sZfL11DbXnkVIZdIfFP4PlqH/g9dEx?= =?us-ascii?Q?B7LqNnDpwSOBAVNgrDbqSHdkVrnBPhrvv4hcUyY24tmOV4LiBW+ChHlGfDxp?= =?us-ascii?Q?EkvNuUqVFO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lq/i68gfC83phn?= =?us-ascii?Q?9pWzQBN1uxrgy1IZTty2uY3vKypE7E7D+GfIuSJgmO3VOExRHhS94IqEx5pi?= =?us-ascii?Q?2dy01AbEBrELebUImybGfgcA6Pzz5pBKrjTy1B+oBSf4FsB14n9mxZT6X4pa?= =?us-ascii?Q?zwXKm0NUo+woRDbGThrd1WNf6bn/LdEyBhjn1MpkOYtp/aiboVJldBPQZdvG?= =?us-ascii?Q?fvMlNus26emQ4Io2H/LiXjr9SPKmmSATretgl48HOovoQqy9tV6KOxLjr6Nv?= =?us-ascii?Q?W5DKjNhZ9jy4m5NfYYQ=3D?= MIME-Version: 1.0 X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-AuthSource: MN6PR11MB8244.namprd11.prod.outlook.com X-MS-Exchange-CrossTenant-Network-Message-Id: e80c70cf-fb6f-4cbf-ceea-08db60d79998 X-MS-Exchange-CrossTenant-originalarrivaltime: 30 May 2023 06:32:11.4072 (UTC) X-MS-Exchange-CrossTenant-fromentityheader: Hosted X-MS-Exchange-CrossTenant-id: 46c98d88-e344-4ed4-8496-4ed7712e255d X-MS-Exchange-CrossTenant-mailboxtype: HOSTED X-MS-Exchange-CrossTenant-userprincipalname: WV0Iy3q2ZQf74sOkanwbGzh/mgMEfKFbmAgEL9JY6diJgtPCc7giCBOejxnQp6kKF4ngQa+ALo1M3iqzsl0dXw== X-MS-Exchange-Transport-CrossTenantHeadersStamped: SJ2PR11MB8539 Return-Path: ray.ni@intel.com X-OriginatorOrg: intel.com Content-Language: en-US Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: quoted-printable I didn't review the existing logic carefully. Several comments: 1. Assignments of several fields are skipped when executing in place. Do th= ey matter? a). Image->NumberOfPages b). Image->ImageBasePage 2. PeCoffLoaderRelocateImage() is called even for XIP case. But I don't thi= nk it's expected that relocation really happens. Even if the fixed data is = the same as the original data stored in MMIO device, MMIO writing might cau= se unexpected behavior. 3. CoreFreePages() is called when image is not loaded successfully. Is it = expected for XIP case? Thanks, Ray > -----Original Message----- > From: Ard Biesheuvel > Sent: Monday, May 29, 2023 6:17 PM > To: devel@edk2.groups.io > Cc: Ard Biesheuvel ; Ni, Ray ; Yao, Ji= ewen > ; Gerd Hoffmann ; Taylor Beebe > ; Oliver Smith-Denny ; Bi, Dandan > ; Gao, Liming ; Kinney, > Michael D ; Leif Lindholm > ; Michael Kubacki > Subject: [RFC PATCH 07/11] MdeModulePkg/DxeCore: Execute loaded images in > place if possible >=20 > In the image loader, check whether an image has already been relocated > to the address from which it is being loaded. This is not something that > can happen by accident, and so we can assume that this means that the > image was intended to be executed in place. >=20 > This removes a redundant copy of the image contents, and also permits > the image to be mapped with restricted permissions even before the CPU > arch protocol has been dispatched. >=20 > Signed-off-by: Ard Biesheuvel > --- > MdeModulePkg/Core/Dxe/Image/Image.c | 11 +++++++++-- > 1 file changed, 9 insertions(+), 2 deletions(-) >=20 > diff --git a/MdeModulePkg/Core/Dxe/Image/Image.c > b/MdeModulePkg/Core/Dxe/Image/Image.c > index 3dfab4829b3ca17f..621637e869daf62d 100644 > --- a/MdeModulePkg/Core/Dxe/Image/Image.c > +++ b/MdeModulePkg/Core/Dxe/Image/Image.c > @@ -573,7 +573,7 @@ STATIC > EFI_STATUS >=20 > CoreLoadPeImage ( >=20 > IN BOOLEAN BootPolicy, >=20 > - IN VOID *Pe32Handle, >=20 > + IN IMAGE_FILE_HANDLE *Pe32Handle, >=20 > IN LOADED_IMAGE_PRIVATE_DATA *Image, >=20 > IN UINT32 Attribute >=20 > ) >=20 > @@ -630,10 +630,16 @@ CoreLoadPeImage ( > return EFI_UNSUPPORTED; >=20 > } >=20 >=20 >=20 > + // >=20 > + // Check whether the loaded image can be executed in place >=20 > + // >=20 > + if (Image->ImageContext.ImageAddress =3D=3D > (PHYSICAL_ADDRESS)(UINTN)Pe32Handle->Source) { >=20 > + goto ExecuteInPlace; >=20 > + } >=20 > + >=20 > // >=20 > // Allocate Destination Buffer as caller did not pass it in >=20 > // >=20 > - >=20 > if (Image->ImageContext.SectionAlignment > EFI_PAGE_SIZE) { >=20 > Size =3D (UINTN)Image->ImageContext.ImageSize + Image- > >ImageContext.SectionAlignment; >=20 > } else { >=20 > @@ -704,6 +710,7 @@ CoreLoadPeImage ( > // >=20 > // Load the image from the file into the allocated memory >=20 > // >=20 > +ExecuteInPlace: >=20 > Status =3D PeCoffLoaderLoadImage (&Image->ImageContext); >=20 > if (EFI_ERROR (Status)) { >=20 > goto Done; >=20 > -- > 2.39.2