Message ID | 20230301085545.2373646-1-chenhuacai@loongson.cn |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel-owner@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:5915:0:0:0:0:0 with SMTP id v21csp3516517wrd; Wed, 1 Mar 2023 00:57:19 -0800 (PST) X-Google-Smtp-Source: AK7set+2Bl5M3BhGhEqITap1u3zPhtKpfS9QzJWWn/9X+e/7HZ1F+D3RYdXen/hOUOntBP1+msa/ X-Received: by 2002:aa7:c481:0:b0:4ad:7493:7a98 with SMTP id m1-20020aa7c481000000b004ad74937a98mr6146635edq.3.1677661039009; Wed, 01 Mar 2023 00:57:19 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1677661038; cv=none; d=google.com; s=arc-20160816; b=O8b1S9UrCMc6L80Kb5+2gDECfLXnyUT4szzx9uBhtoeuFYQjoatnVzSQSvmjfB4J9p 3CFfI8CZRPJlT+eaqQeg8m1tjbGkzeJeO3Ri9Lsp5SrWU9kAI0X4Z5iKvSWZAjY3SdPV 6G+peav3QrViu1TbQLX5FQw7ZP7NQnKtyFHAzPwJ6gnedCW/XFBxIh0Fe5M+79yU+qor QWr/mLVHhhx6UEV3qADW0Btk0/+DNhr9B9Y/dCEWZ35mqe5bq8QIfMlNvKCqlnK+oVpu qSu4ReN3Q5MJ3GfUsAGl0xt1rqBEiTqNUdIehKXU5asInvxhjNjxhgVlSrJizodO4piM KL7Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from; bh=zxVj1Y5RVBEGCW3iaEhuN0vHsmYuC+PoaXtqaAhJLGk=; b=vMkeTRED2SFvTMACqBSKAQpKyHqf+5/a1ue56OvNH36RQuNN/+19gFHMpLZ+swUqd0 J0QMrIqgOnKbSJt6wTHZt8CJ6n1/EjEcVx3FiHXpdIbnqCCEh13SrBW2MavppEW8F9mr rhl+nPnndL6mWwCPzwz8gdzS/QTFSfESlQHcoDXEJMJYQj7FBro7hczYDtFxNO5Kh1NI k1f5JuAzn4GhYeaiiPfQhlHHQii+/HYOYxDVGiT2WFPUyXVJvV92nuexwSj+Quo7PzZ7 IbtIjnzmxWTC/ffbGr8RCgWlPPEVJEoYxqCmtIen2NGNBNXnJ4q0+ITmAMLLMxHLLzV7 /g7A== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id a16-20020a50ff10000000b004acd9e5c891si13598656edu.375.2023.03.01.00.56.56; Wed, 01 Mar 2023 00:57:18 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229834AbjCAIz7 (ORCPT <rfc822;dipsyhu@gmail.com> + 99 others); Wed, 1 Mar 2023 03:55:59 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48966 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229796AbjCAIz5 (ORCPT <rfc822;linux-kernel@vger.kernel.org>); Wed, 1 Mar 2023 03:55:57 -0500 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 87948392AC; Wed, 1 Mar 2023 00:55:56 -0800 (PST) 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 C5F1261254; Wed, 1 Mar 2023 08:55:55 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 00B58C433EF; Wed, 1 Mar 2023 08:55:52 +0000 (UTC) From: Huacai Chen <chenhuacai@loongson.cn> To: Ard Biesheuvel <ardb@kernel.org>, Huacai Chen <chenhuacai@kernel.org> Cc: linux-efi@vger.kernel.org, loongarch@lists.linux.dev, Xuefeng Li <lixuefeng@loongson.cn>, Xuerui Wang <kernel@xen0n.name>, linux-kernel@vger.kernel.org, loongson-kernel@lists.loongnix.cn, Huacai Chen <chenhuacai@loongson.cn> Subject: [PATCH] efi/loongarch: Reintroduce efi_relocate_kernel() to relocate kernel Date: Wed, 1 Mar 2023 16:55:45 +0800 Message-Id: <20230301085545.2373646-1-chenhuacai@loongson.cn> X-Mailer: git-send-email 2.39.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-4.0 required=5.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,RCVD_IN_DNSWL_MED,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: <linux-kernel.vger.kernel.org> X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1759155101469922270?= X-GMAIL-MSGID: =?utf-8?q?1759155101469922270?= |
Series |
efi/loongarch: Reintroduce efi_relocate_kernel() to relocate kernel
|
|
Commit Message
Huacai Chen
March 1, 2023, 8:55 a.m. UTC
Since Linux-6.3, LoongArch supports PIE kernel now, so let's reintroduce
efi_relocate_kernel() to relocate the core kernel.
Signed-off-by: Huacai Chen <chenhuacai@loongson.cn>
---
drivers/firmware/efi/libstub/loongarch-stub.c | 24 ++++++-------------
1 file changed, 7 insertions(+), 17 deletions(-)
Comments
On Wed, 1 Mar 2023 at 09:55, Huacai Chen <chenhuacai@loongson.cn> wrote: > > Since Linux-6.3, LoongArch supports PIE kernel now, so let's reintroduce > efi_relocate_kernel() to relocate the core kernel. > Nice! I assume you will need to make a similar change for zboot? > Signed-off-by: Huacai Chen <chenhuacai@loongson.cn> > --- > 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; > } > -- > 2.39.1 >
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 <chenhuacai@loongson.cn> > --- > 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; > } >
Hi, Youling, On Wed, Mar 1, 2023 at 5:21 PM Youling Tang <tangyouling@loongson.cn> 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. 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 <chenhuacai@loongson.cn> > > --- > > 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; > > } > > >
Hi, Ard, On Wed, Mar 1, 2023 at 5:17 PM Ard Biesheuvel <ardb@kernel.org> wrote: > > On Wed, 1 Mar 2023 at 09:55, Huacai Chen <chenhuacai@loongson.cn> wrote: > > > > Since Linux-6.3, LoongArch supports PIE kernel now, so let's reintroduce > > efi_relocate_kernel() to relocate the core kernel. > > > > Nice! > > I assume you will need to make a similar change for zboot? zboot is always PIE (even before 6.3), so I suppose we don't need to do anything? Huacai > > > Signed-off-by: Huacai Chen <chenhuacai@loongson.cn> > > --- > > 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; > > } > > -- > > 2.39.1 > >
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 <tangyouling@loongson.cn> 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 <chenhuacai@loongson.cn> >>> --- >>> 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; >>> } >>> >>
Hi, Youling, On Wed, Mar 1, 2023 at 6:08 PM Youling Tang <tangyouling@loongson.cn> wrote: > > 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 <tangyouling@loongson.cn> 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 We can select RELOCATABLE when we modify EFI_KIMG_PREFERRED_ADDRESS. Before that I prefer to give a chance to build a non-PIE kernel. Huacai > > 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 <chenhuacai@loongson.cn> > >>> --- > >>> 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; > >>> } > >>> > >> > >
Hi, Ard, What is the status of this patch now? Huacai On Wed, Mar 1, 2023 at 8:06 PM Huacai Chen <chenhuacai@kernel.org> wrote: > > Hi, Youling, > > On Wed, Mar 1, 2023 at 6:08 PM Youling Tang <tangyouling@loongson.cn> wrote: > > > > 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 <tangyouling@loongson.cn> 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 > We can select RELOCATABLE when we modify EFI_KIMG_PREFERRED_ADDRESS. > Before that I prefer to give a chance to build a non-PIE kernel. > > Huacai > > > > 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 <chenhuacai@loongson.cn> > > >>> --- > > >>> 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; > > >>> } > > >>> > > >> > > > >
On Wed, 5 Apr 2023 at 10:14, Huacai Chen <chenhuacai@kernel.org> wrote: > > Hi, Ard, > > What is the status of this patch now? > I'd still like to understand how zboot is affected. It has static unsigned long alloc_preferred_address(unsigned long alloc_size) { #ifdef EFI_KIMG_PREFERRED_ADDRESS efi_physical_addr_t efi_addr = EFI_KIMG_PREFERRED_ADDRESS; if (efi_bs_call(allocate_pages, EFI_ALLOCATE_ADDRESS, EFI_LOADER_DATA, alloc_size / EFI_PAGE_SIZE, &efi_addr) == EFI_SUCCESS) return efi_addr; #endif return ULONG_MAX; } and if I understand you correctly, we never needed this in the first place? (I understand this is a separate issue, but it would be nice to get rid of EFI_KIMG_PREFERRED_ADDRESS entirely if we can.) > On Wed, Mar 1, 2023 at 8:06 PM Huacai Chen <chenhuacai@kernel.org> wrote: > > > > Hi, Youling, > > > > On Wed, Mar 1, 2023 at 6:08 PM Youling Tang <tangyouling@loongson.cn> wrote: > > > > > > 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 <tangyouling@loongson.cn> 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 > > We can select RELOCATABLE when we modify EFI_KIMG_PREFERRED_ADDRESS. > > Before that I prefer to give a chance to build a non-PIE kernel. > > > > Huacai > > > > > > 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 <chenhuacai@loongson.cn> > > > >>> --- > > > >>> 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; > > > >>> } > > > >>> > > > >> > > > > > >
Hi, Ard, On Wed, Apr 5, 2023 at 4:20 PM Ard Biesheuvel <ardb@kernel.org> wrote: > > On Wed, 5 Apr 2023 at 10:14, Huacai Chen <chenhuacai@kernel.org> wrote: > > > > Hi, Ard, > > > > What is the status of this patch now? > > > > I'd still like to understand how zboot is affected. It has > > static unsigned long alloc_preferred_address(unsigned long alloc_size) > { > #ifdef EFI_KIMG_PREFERRED_ADDRESS > efi_physical_addr_t efi_addr = EFI_KIMG_PREFERRED_ADDRESS; > > if (efi_bs_call(allocate_pages, EFI_ALLOCATE_ADDRESS, EFI_LOADER_DATA, > alloc_size / EFI_PAGE_SIZE, &efi_addr) == EFI_SUCCESS) > return efi_addr; > #endif > return ULONG_MAX; > } > > and if I understand you correctly, we never needed this in the first place? Technically we don't need this, but I think setting a preferred address as before may be better (keeping the default behavior the same as before can avoid some unexpected problem). Huacai > > (I understand this is a separate issue, but it would be nice to get > rid of EFI_KIMG_PREFERRED_ADDRESS entirely if we can.) > > > > > On Wed, Mar 1, 2023 at 8:06 PM Huacai Chen <chenhuacai@kernel.org> wrote: > > > > > > Hi, Youling, > > > > > > On Wed, Mar 1, 2023 at 6:08 PM Youling Tang <tangyouling@loongson.cn> wrote: > > > > > > > > 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 <tangyouling@loongson.cn> 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 > > > We can select RELOCATABLE when we modify EFI_KIMG_PREFERRED_ADDRESS. > > > Before that I prefer to give a chance to build a non-PIE kernel. > > > > > > Huacai > > > > > > > > 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 <chenhuacai@loongson.cn> > > > > >>> --- > > > > >>> 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; > > > > >>> } > > > > >>> > > > > >> > > > > > > > >
On Wed, 5 Apr 2023 at 11:57, Huacai Chen <chenhuacai@kernel.org> wrote: > > Hi, Ard, > > On Wed, Apr 5, 2023 at 4:20 PM Ard Biesheuvel <ardb@kernel.org> wrote: > > > > On Wed, 5 Apr 2023 at 10:14, Huacai Chen <chenhuacai@kernel.org> wrote: > > > > > > Hi, Ard, > > > > > > What is the status of this patch now? > > > > > > > I'd still like to understand how zboot is affected. It has > > > > static unsigned long alloc_preferred_address(unsigned long alloc_size) > > { > > #ifdef EFI_KIMG_PREFERRED_ADDRESS > > efi_physical_addr_t efi_addr = EFI_KIMG_PREFERRED_ADDRESS; > > > > if (efi_bs_call(allocate_pages, EFI_ALLOCATE_ADDRESS, EFI_LOADER_DATA, > > alloc_size / EFI_PAGE_SIZE, &efi_addr) == EFI_SUCCESS) > > return efi_addr; > > #endif > > return ULONG_MAX; > > } > > > > and if I understand you correctly, we never needed this in the first place? > Technically we don't need this, but I think setting a preferred > address as before may be better (keeping the default behavior the same > as before can avoid some unexpected problem). > OK makes sense I have queued this up now, should appear in -next tomorrow.
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; }