Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 15A9FC7EE30 for ; Wed, 1 Mar 2023 09:22:13 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229744AbjCAJWL (ORCPT ); Wed, 1 Mar 2023 04:22:11 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45146 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229649AbjCAJV4 (ORCPT ); Wed, 1 Mar 2023 04:21:56 -0500 Received: from loongson.cn (mail.loongson.cn [114.242.206.163]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 0D08D3028F; Wed, 1 Mar 2023 01:21:53 -0800 (PST) Received: from loongson.cn (unknown [192.168.200.1]) by gateway (Coremail) with SMTP id _____8DxUOUdGf9j2MQGAA--.7280S3; Wed, 01 Mar 2023 17:21:33 +0800 (CST) Received: from [0.0.0.0] (unknown [192.168.200.1]) by localhost.localdomain (Coremail) with SMTP id AQAAf8DxaL3CGP9jtlJDAA--.53207S3; Wed, 01 Mar 2023 17:20:03 +0800 (CST) Subject: Re: [PATCH] efi/loongarch: Reintroduce efi_relocate_kernel() to relocate kernel To: Huacai Chen , Ard Biesheuvel , Huacai Chen References: <20230301085545.2373646-1-chenhuacai@loongson.cn> Cc: linux-efi@vger.kernel.org, loongarch@lists.linux.dev, Xuefeng Li , Xuerui Wang , linux-kernel@vger.kernel.org, loongson-kernel@lists.loongnix.cn From: Youling Tang Message-ID: Date: Wed, 1 Mar 2023 17:20:02 +0800 User-Agent: Mozilla/5.0 (X11; Linux mips64; rv:45.0) Gecko/20100101 Thunderbird/45.4.0 MIME-Version: 1.0 In-Reply-To: <20230301085545.2373646-1-chenhuacai@loongson.cn> Content-Type: text/plain; charset=windows-1252; format=fl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8VAv wI8IcIk0rVWUJVWUCwCI42IY6I8E87Iv67AKxVWUJVW8JwCI42IY6I8E87Iv6xkF7I0E14 v26r1j6r4UYxBIdaVFxhVjvjDU0xZFpf9x0zRByIUUUUUU= Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Hi, Huacai On 03/01/2023 04:55 PM, Huacai Chen wrote: > Since Linux-6.3, LoongArch supports PIE kernel now, so let's reintroduce > efi_relocate_kernel() to relocate the core kernel. If we strongly rely on the PIE feature after the modification, we need to modify it as follows, --- a/arch/loongarch/Kconfig +++ b/arch/loongarch/Kconfig @@ -137,6 +137,7 @@ config LOONGARCH select PCI_MSI_ARCH_FALLBACKS select PCI_QUIRKS select PERF_USE_VMALLOC + select RELOCATABLE select RTC_LIB select SMP select SPARSE_IRQ or: --- a/arch/loongarch/Kconfig +++ b/arch/loongarch/Kconfig @@ -360,6 +360,7 @@ config EFI_STUB default y depends on EFI select EFI_GENERIC_STUB + select RELOCATABLE help Youling. > > Signed-off-by: Huacai Chen > --- > drivers/firmware/efi/libstub/loongarch-stub.c | 24 ++++++------------- > 1 file changed, 7 insertions(+), 17 deletions(-) > > diff --git a/drivers/firmware/efi/libstub/loongarch-stub.c b/drivers/firmware/efi/libstub/loongarch-stub.c > index eee7ed43cdfb..72c71ae201f0 100644 > --- a/drivers/firmware/efi/libstub/loongarch-stub.c > +++ b/drivers/firmware/efi/libstub/loongarch-stub.c > @@ -21,26 +21,16 @@ efi_status_t handle_kernel_image(unsigned long *image_addr, > efi_loaded_image_t *image, > efi_handle_t image_handle) > { > - int nr_pages = round_up(kernel_asize, EFI_ALLOC_ALIGN) / EFI_PAGE_SIZE; > - efi_physical_addr_t kernel_addr = EFI_KIMG_PREFERRED_ADDRESS; > efi_status_t status; > + unsigned long kernel_addr = 0; > > - /* > - * Allocate space for the kernel image at the preferred offset. This is > - * the only location in memory from where we can execute the image, so > - * no point in falling back to another allocation. > - */ > - status = efi_bs_call(allocate_pages, EFI_ALLOCATE_ADDRESS, > - EFI_LOADER_DATA, nr_pages, &kernel_addr); > - if (status != EFI_SUCCESS) > - return status; > - > - *image_addr = EFI_KIMG_PREFERRED_ADDRESS; > - *image_size = kernel_asize; > + kernel_addr = (unsigned long)&kernel_offset - kernel_offset; > + > + status = efi_relocate_kernel(&kernel_addr, kernel_fsize, kernel_asize, > + EFI_KIMG_PREFERRED_ADDRESS, efi_get_kimg_min_align(), 0x0); > > - memcpy((void *)EFI_KIMG_PREFERRED_ADDRESS, > - (void *)&kernel_offset - kernel_offset, > - kernel_fsize); > + *image_addr = kernel_addr; > + *image_size = kernel_asize; > > return status; > } >