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 B2A11C64EC7 for ; Wed, 1 Mar 2023 10:08:03 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229922AbjCAKIC (ORCPT ); Wed, 1 Mar 2023 05:08:02 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56670 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229486AbjCAKH7 (ORCPT ); Wed, 1 Mar 2023 05:07:59 -0500 Received: from loongson.cn (mail.loongson.cn [114.242.206.163]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id EDB575FFF; Wed, 1 Mar 2023 02:07:56 -0800 (PST) Received: from loongson.cn (unknown [192.168.200.1]) by gateway (Coremail) with SMTP id _____8DxEwz7I_9j+McGAA--.7304S3; Wed, 01 Mar 2023 18:07:55 +0800 (CST) Received: from [0.0.0.0] (unknown [192.168.200.1]) by localhost.localdomain (Coremail) with SMTP id AQAAf8Bxyub5I_9jsG1DAA--.20371S3; Wed, 01 Mar 2023 18:07:54 +0800 (CST) Subject: Re: [PATCH] efi/loongarch: Reintroduce efi_relocate_kernel() to relocate kernel To: Huacai Chen References: <20230301085545.2373646-1-chenhuacai@loongson.cn> Cc: Huacai Chen , Ard Biesheuvel , 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 18:07:53 +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: Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit X-CM-TRANSID: A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Iv67AKxVWUJVW8JwCI42IY6I8E87Iv6xkF7I0E14 v26r4j6r4UJbIYCTnIWIevJa73UjIFyTuYvj4RKtC7UUUUU Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Hi, Huacai On 03/01/2023 05:30 PM, Huacai Chen wrote: > Hi, Youling, > > On Wed, Mar 1, 2023 at 5:21 PM Youling Tang wrote: >> >> 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, > There is no strong rely, efi_relocate_kernel() can also handle the > non-PIE kernel. IMHO, if EFI_KIMG_PREFERRED_ADDRESS is no longer defined as `PHYSADDR(VMLINUX_LOAD_ADDRESS)` (physical link address), we need to relocate through the RELOCATABLE function, so it is recommended to select RELOCATABLE if EFI_STUB is enabled Youling. > > Huacai >> >> --- 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; >>> } >>> >>