Skip to content

Commit d162fee

Browse files
tangyoulingchenhuacai
authored andcommitted
LoongArch: Add preparatory infrastructure for kexec_file
Add some preparatory infrastructure: - Add command line processing. - Add support for loading other segments. - Other minor modifications. This initrd will be passed to the second kernel via the command line 'initrd=start,size'. The 'kexec_file' command line parameter indicates that the kernel is loaded via kexec_file. Signed-off-by: Youling Tang <tangyouling@kylinos.cn> Signed-off-by: Huacai Chen <chenhuacai@loongson.cn>
1 parent 30ade4f commit d162fee

File tree

5 files changed

+166
-13
lines changed

5 files changed

+166
-13
lines changed

arch/loongarch/Kconfig

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -633,6 +633,15 @@ config CPU_HAS_PREFETCH
633633
config ARCH_SUPPORTS_KEXEC
634634
def_bool y
635635

636+
config ARCH_SUPPORTS_KEXEC_FILE
637+
def_bool 64BIT
638+
639+
config ARCH_SELECTS_KEXEC_FILE
640+
def_bool 64BIT
641+
depends on KEXEC_FILE
642+
select RELOCATABLE
643+
select HAVE_IMA_KEXEC if IMA
644+
636645
config ARCH_SUPPORTS_CRASH_DUMP
637646
def_bool y
638647

arch/loongarch/include/asm/kexec.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,16 @@ struct kimage_arch {
4141
unsigned long systable_ptr;
4242
};
4343

44+
#ifdef CONFIG_KEXEC_FILE
45+
46+
int arch_kimage_file_post_load_cleanup(struct kimage *image);
47+
#define arch_kimage_file_post_load_cleanup arch_kimage_file_post_load_cleanup
48+
49+
extern int load_other_segments(struct kimage *image,
50+
unsigned long kernel_load_addr, unsigned long kernel_size,
51+
char *initrd, unsigned long initrd_len, char *cmdline, unsigned long cmdline_len);
52+
#endif
53+
4454
typedef void (*do_kexec_t)(unsigned long efi_boot,
4555
unsigned long cmdline_ptr,
4656
unsigned long systable_ptr,

arch/loongarch/kernel/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ obj-$(CONFIG_MAGIC_SYSRQ) += sysrq.o
6262
obj-$(CONFIG_RELOCATABLE) += relocate.o
6363

6464
obj-$(CONFIG_KEXEC_CORE) += machine_kexec.o relocate_kernel.o
65+
obj-$(CONFIG_KEXEC_FILE) += machine_kexec_file.o
6566
obj-$(CONFIG_CRASH_DUMP) += crash_dump.o
6667

6768
obj-$(CONFIG_UNWINDER_GUESS) += unwind_guess.o

arch/loongarch/kernel/machine_kexec.c

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -70,18 +70,28 @@ int machine_kexec_prepare(struct kimage *kimage)
7070
kimage->arch.efi_boot = fw_arg0;
7171
kimage->arch.systable_ptr = fw_arg2;
7272

73-
/* Find the command line */
74-
for (i = 0; i < kimage->nr_segments; i++) {
75-
if (!strncmp(bootloader, (char __user *)kimage->segment[i].buf, strlen(bootloader))) {
76-
if (!copy_from_user(cmdline_ptr, kimage->segment[i].buf, COMMAND_LINE_SIZE))
77-
kimage->arch.cmdline_ptr = (unsigned long)cmdline_ptr;
78-
break;
73+
if (kimage->file_mode == 1) {
74+
/*
75+
* kimage->cmdline_buf will be released in kexec_file_load, so copy
76+
* to the KEXEC_CMDLINE_ADDR safe area.
77+
*/
78+
memcpy((void *)KEXEC_CMDLINE_ADDR, (void *)kimage->arch.cmdline_ptr,
79+
strlen((char *)kimage->arch.cmdline_ptr) + 1);
80+
kimage->arch.cmdline_ptr = (unsigned long)KEXEC_CMDLINE_ADDR;
81+
} else {
82+
/* Find the command line */
83+
for (i = 0; i < kimage->nr_segments; i++) {
84+
if (!strncmp(bootloader, (char __user *)kimage->segment[i].buf, strlen(bootloader))) {
85+
if (!copy_from_user(cmdline_ptr, kimage->segment[i].buf, COMMAND_LINE_SIZE))
86+
kimage->arch.cmdline_ptr = (unsigned long)cmdline_ptr;
87+
break;
88+
}
7989
}
80-
}
8190

82-
if (!kimage->arch.cmdline_ptr) {
83-
pr_err("Command line not included in the provided image\n");
84-
return -EINVAL;
91+
if (!kimage->arch.cmdline_ptr) {
92+
pr_err("Command line not included in the provided image\n");
93+
return -EINVAL;
94+
}
8595
}
8696

8797
/* kexec/kdump need a safe page to save reboot_code_buffer */
@@ -287,9 +297,10 @@ void machine_kexec(struct kimage *image)
287297
/* We do not want to be bothered. */
288298
local_irq_disable();
289299

290-
pr_notice("EFI boot flag 0x%lx\n", efi_boot);
291-
pr_notice("Command line at 0x%lx\n", cmdline_ptr);
292-
pr_notice("System table at 0x%lx\n", systable_ptr);
300+
pr_notice("EFI boot flag: 0x%lx\n", efi_boot);
301+
pr_notice("Command line addr: 0x%lx\n", cmdline_ptr);
302+
pr_notice("Command line string: %s\n", (char *)cmdline_ptr);
303+
pr_notice("System table addr: 0x%lx\n", systable_ptr);
293304
pr_notice("We will call new kernel at 0x%lx\n", start_addr);
294305
pr_notice("Bye ...\n");
295306

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
/*
3+
* kexec_file for LoongArch
4+
*
5+
* Author: Youling Tang <tangyouling@kylinos.cn>
6+
* Copyright (C) 2025 KylinSoft Corporation.
7+
*
8+
* Most code is derived from LoongArch port of kexec-tools
9+
*/
10+
11+
#define pr_fmt(fmt) "kexec_file: " fmt
12+
13+
#include <linux/ioport.h>
14+
#include <linux/kernel.h>
15+
#include <linux/kexec.h>
16+
#include <linux/memblock.h>
17+
#include <linux/slab.h>
18+
#include <linux/string.h>
19+
#include <linux/types.h>
20+
#include <linux/vmalloc.h>
21+
#include <asm/bootinfo.h>
22+
23+
const struct kexec_file_ops * const kexec_file_loaders[] = {
24+
NULL
25+
};
26+
27+
int arch_kimage_file_post_load_cleanup(struct kimage *image)
28+
{
29+
vfree(image->elf_headers);
30+
image->elf_headers = NULL;
31+
image->elf_headers_sz = 0;
32+
33+
return kexec_image_post_load_cleanup_default(image);
34+
}
35+
36+
/* Add the "kexec_file" command line parameter to command line. */
37+
static void cmdline_add_loader(unsigned long *cmdline_tmplen, char *modified_cmdline)
38+
{
39+
int loader_strlen;
40+
41+
loader_strlen = sprintf(modified_cmdline + (*cmdline_tmplen), "kexec_file ");
42+
*cmdline_tmplen += loader_strlen;
43+
}
44+
45+
/* Add the "initrd=start,size" command line parameter to command line. */
46+
static void cmdline_add_initrd(struct kimage *image, unsigned long *cmdline_tmplen,
47+
char *modified_cmdline, unsigned long initrd)
48+
{
49+
int initrd_strlen;
50+
51+
initrd_strlen = sprintf(modified_cmdline + (*cmdline_tmplen), "initrd=0x%lx,0x%lx ",
52+
initrd, image->initrd_buf_len);
53+
*cmdline_tmplen += initrd_strlen;
54+
}
55+
56+
/*
57+
* Try to add the initrd to the image. If it is not possible to find valid
58+
* locations, this function will undo changes to the image and return non zero.
59+
*/
60+
int load_other_segments(struct kimage *image,
61+
unsigned long kernel_load_addr, unsigned long kernel_size,
62+
char *initrd, unsigned long initrd_len, char *cmdline, unsigned long cmdline_len)
63+
{
64+
int ret = 0;
65+
unsigned long cmdline_tmplen = 0;
66+
unsigned long initrd_load_addr = 0;
67+
unsigned long orig_segments = image->nr_segments;
68+
char *modified_cmdline = NULL;
69+
struct kexec_buf kbuf;
70+
71+
kbuf.image = image;
72+
/* Don't allocate anything below the kernel */
73+
kbuf.buf_min = kernel_load_addr + kernel_size;
74+
75+
modified_cmdline = kzalloc(COMMAND_LINE_SIZE, GFP_KERNEL);
76+
if (!modified_cmdline)
77+
return -EINVAL;
78+
79+
cmdline_add_loader(&cmdline_tmplen, modified_cmdline);
80+
/* Ensure it's null terminated */
81+
modified_cmdline[COMMAND_LINE_SIZE - 1] = '\0';
82+
83+
/* Load initrd */
84+
if (initrd) {
85+
kbuf.buffer = initrd;
86+
kbuf.bufsz = initrd_len;
87+
kbuf.mem = KEXEC_BUF_MEM_UNKNOWN;
88+
kbuf.memsz = initrd_len;
89+
kbuf.buf_align = 0;
90+
/* within 1GB-aligned window of up to 32GB in size */
91+
kbuf.buf_max = round_down(kernel_load_addr, SZ_1G) + (unsigned long)SZ_1G * 32;
92+
kbuf.top_down = false;
93+
94+
ret = kexec_add_buffer(&kbuf);
95+
if (ret < 0)
96+
goto out_err;
97+
initrd_load_addr = kbuf.mem;
98+
99+
kexec_dprintk("Loaded initrd at 0x%lx bufsz=0x%lx memsz=0x%lx\n",
100+
initrd_load_addr, kbuf.bufsz, kbuf.memsz);
101+
102+
/* Add the initrd=start,size parameter to the command line */
103+
cmdline_add_initrd(image, &cmdline_tmplen, modified_cmdline, initrd_load_addr);
104+
}
105+
106+
if (cmdline_len + cmdline_tmplen > COMMAND_LINE_SIZE) {
107+
pr_err("Appending command line exceeds COMMAND_LINE_SIZE\n");
108+
ret = -EINVAL;
109+
goto out_err;
110+
}
111+
112+
memcpy(modified_cmdline + cmdline_tmplen, cmdline, cmdline_len);
113+
cmdline = modified_cmdline;
114+
image->arch.cmdline_ptr = (unsigned long)cmdline;
115+
116+
return 0;
117+
118+
out_err:
119+
image->nr_segments = orig_segments;
120+
kfree(modified_cmdline);
121+
return ret;
122+
}

0 commit comments

Comments
 (0)