From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by mx.groups.io with SMTP id smtpd.web11.4811.1685433285312346918 for ; Tue, 30 May 2023 00:54:45 -0700 Authentication-Results: mx.groups.io; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=qWWuQUtd; spf=pass (domain: kernel.org, ip: 139.178.84.217, mailfrom: ardb@kernel.org) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id CFB046236F for ; Tue, 30 May 2023 07:54:44 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 40A0AC4339E for ; Tue, 30 May 2023 07:54:44 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1685433284; bh=tXn0j5+lfLzmE7bmR8IyEcbSaoEXUQELLUH+hJJ38is=; h=References:In-Reply-To:From:Date:Subject:To:Cc:From; b=qWWuQUtdg1ACJgRU6vHb5O8RZkkmuQlzUYg3XZlnDWY9zEsKdvNvfb9lcDHznk+mM 1suyX3zfhbKD9sCY6nG5h5mumNqw9dGBkO1W6Ugv8p5XUREYMuBOYoN4MatLN08Una ktJSeXP3ctbKGdCIUeqXn5WGGHGb1qg2CoOlpqKAJwkM5HfkdS3kpPSQeuYs1yQShD PRxbdXwCs+TZKTXbyMibtln5IQzvc+9v5R5oQb70SIUEOaoMzUhSZIY7MRkVa9i+5D Ef4qLLp5hL05r52V/xNyiRAoStAs585jFPwx38LjL/1nBq6KU9WKteU+huoRYrDfe3 DMP+oEnEklC7g== Received: by mail-lf1-f53.google.com with SMTP id 2adb3069b0e04-4f4f757d575so2931092e87.0 for ; Tue, 30 May 2023 00:54:44 -0700 (PDT) X-Gm-Message-State: AC+VfDx69v/Rt01L1pkPh4Z1gKllq/0B4FzzrJEo5+ltC7nl++XWVmfQ 7P13tK7m0JdguNeb5PZ73QmsvjZQ5/rmPoKypYo= X-Google-Smtp-Source: ACHHUZ5yFEw3qb7dr+CqU4BbFSTGi8gvxUsa8ELrekAo5yMiNX7CYfKVm2OMeBpI92GMSQHgqQWIthzVTovxdMGwypA= X-Received: by 2002:a05:6512:2249:b0:4eb:412f:9e0e with SMTP id i9-20020a056512224900b004eb412f9e0emr3270325lfu.26.1685433282275; Tue, 30 May 2023 00:54:42 -0700 (PDT) MIME-Version: 1.0 References: <20230529101705.2476949-1-ardb@kernel.org> <20230529101705.2476949-8-ardb@kernel.org> In-Reply-To: From: "Ard Biesheuvel" Date: Tue, 30 May 2023 09:54:31 +0200 X-Gmail-Original-Message-ID: Message-ID: Subject: Re: [RFC PATCH 07/11] MdeModulePkg/DxeCore: Execute loaded images in place if possible To: "Ni, Ray" Cc: "devel@edk2.groups.io" , "Yao, Jiewen" , Gerd Hoffmann , Taylor Beebe , Oliver Smith-Denny , "Bi, Dandan" , "Gao, Liming" , "Kinney, Michael D" , Leif Lindholm , Michael Kubacki Content-Type: text/plain; charset="UTF-8" On Tue, 30 May 2023 at 08:32, Ni, Ray wrote: > > I didn't review the existing logic carefully. Several comments: > 1. Assignments of several fields are skipped when executing in place. Do they matter? > a). Image->NumberOfPages > b). Image->ImageBasePage > These are used when freeing the image again, so NumberOfPages should remain 0x0 in this case, so that unloading the image does not free any pages. > 2. PeCoffLoaderRelocateImage() is called even for XIP case. But I don't think 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 cause unexpected behavior. > PeCoffLoaderRelocateImage() currently calculates the adjustment offset, and does nothing to the image if it is 0x0. > 3. CoreFreePages() is called when image is not loaded successfully. Is it expected for XIP case? > Yes, but Image->NumberOfPages will be 0x0. > > > -----Original Message----- > > From: Ard Biesheuvel > > Sent: Monday, May 29, 2023 6:17 PM > > To: devel@edk2.groups.io > > Cc: Ard Biesheuvel ; Ni, Ray ; Yao, Jiewen > > ; 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 > > > > 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. > > > > 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. > > > > Signed-off-by: Ard Biesheuvel > > --- > > MdeModulePkg/Core/Dxe/Image/Image.c | 11 +++++++++-- > > 1 file changed, 9 insertions(+), 2 deletions(-) > > > > 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 > > > > CoreLoadPeImage ( > > > > IN BOOLEAN BootPolicy, > > > > - IN VOID *Pe32Handle, > > > > + IN IMAGE_FILE_HANDLE *Pe32Handle, > > > > IN LOADED_IMAGE_PRIVATE_DATA *Image, > > > > IN UINT32 Attribute > > > > ) > > > > @@ -630,10 +630,16 @@ CoreLoadPeImage ( > > return EFI_UNSUPPORTED; > > > > } > > > > > > > > + // > > > > + // Check whether the loaded image can be executed in place > > > > + // > > > > + if (Image->ImageContext.ImageAddress == > > (PHYSICAL_ADDRESS)(UINTN)Pe32Handle->Source) { > > > > + goto ExecuteInPlace; > > > > + } > > > > + > > > > // > > > > // Allocate Destination Buffer as caller did not pass it in > > > > // > > > > - > > > > if (Image->ImageContext.SectionAlignment > EFI_PAGE_SIZE) { > > > > Size = (UINTN)Image->ImageContext.ImageSize + Image- > > >ImageContext.SectionAlignment; > > > > } else { > > > > @@ -704,6 +710,7 @@ CoreLoadPeImage ( > > // > > > > // Load the image from the file into the allocated memory > > > > // > > > > +ExecuteInPlace: > > > > Status = PeCoffLoaderLoadImage (&Image->ImageContext); > > > > if (EFI_ERROR (Status)) { > > > > goto Done; > > > > -- > > 2.39.2 >