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 &kexec_efi_ops, 25 &kexec_elf_ops, 26 NULL 27 }; 28 29 int arch_kimage_file_post_load_cleanup(struct kimage *image) 30 { 31 vfree(image->elf_headers); 32 image->elf_headers = NULL; 33 image->elf_headers_sz = 0; 34 35 return kexec_image_post_load_cleanup_default(image); 36 } 37 38 /* Add the "kexec_file" command line parameter to command line. */ 39 static void cmdline_add_loader(unsigned long *cmdline_tmplen, char *modified_cmdline) 40 { 41 int loader_strlen; 42 43 loader_strlen = sprintf(modified_cmdline + (*cmdline_tmplen), "kexec_file "); 44 *cmdline_tmplen += loader_strlen; 45 } 46 47 /* Add the "initrd=start,size" command line parameter to command line. */ 48 static void cmdline_add_initrd(struct kimage *image, unsigned long *cmdline_tmplen, 49 char *modified_cmdline, unsigned long initrd) 50 { 51 int initrd_strlen; 52 53 initrd_strlen = sprintf(modified_cmdline + (*cmdline_tmplen), "initrd=0x%lx,0x%lx ", 54 initrd, image->initrd_buf_len); 55 *cmdline_tmplen += initrd_strlen; 56 } 57 58 #ifdef CONFIG_CRASH_DUMP 59 60 static int prepare_elf_headers(void **addr, unsigned long *sz) 61 { 62 int ret, nr_ranges; 63 uint64_t i; 64 phys_addr_t start, end; 65 struct crash_mem *cmem; 66 67 nr_ranges = 2; /* for exclusion of crashkernel region */ 68 for_each_mem_range(i, &start, &end) 69 nr_ranges++; 70 71 cmem = kmalloc(struct_size(cmem, ranges, nr_ranges), GFP_KERNEL); 72 if (!cmem) 73 return -ENOMEM; 74 75 cmem->max_nr_ranges = nr_ranges; 76 cmem->nr_ranges = 0; 77 for_each_mem_range(i, &start, &end) { 78 cmem->ranges[cmem->nr_ranges].start = start; 79 cmem->ranges[cmem->nr_ranges].end = end - 1; 80 cmem->nr_ranges++; 81 } 82 83 /* Exclude crashkernel region */ 84 ret = crash_exclude_mem_range(cmem, crashk_res.start, crashk_res.end); 85 if (ret < 0) 86 goto out; 87 88 if (crashk_low_res.end) { 89 ret = crash_exclude_mem_range(cmem, crashk_low_res.start, crashk_low_res.end); 90 if (ret < 0) 91 goto out; 92 } 93 94 ret = crash_prepare_elf64_headers(cmem, true, addr, sz); 95 96 out: 97 kfree(cmem); 98 return ret; 99 } 100 101 /* 102 * Add the "mem=size@start" command line parameter to command line, indicating the 103 * memory region the new kernel can use to boot into. 104 */ 105 static void cmdline_add_mem(unsigned long *cmdline_tmplen, char *modified_cmdline) 106 { 107 int mem_strlen = 0; 108 109 mem_strlen = sprintf(modified_cmdline + (*cmdline_tmplen), "mem=0x%llx@0x%llx ", 110 crashk_res.end - crashk_res.start + 1, crashk_res.start); 111 *cmdline_tmplen += mem_strlen; 112 113 if (crashk_low_res.end) { 114 mem_strlen = sprintf(modified_cmdline + (*cmdline_tmplen), "mem=0x%llx@0x%llx ", 115 crashk_low_res.end - crashk_low_res.start + 1, crashk_low_res.start); 116 *cmdline_tmplen += mem_strlen; 117 } 118 } 119 120 /* Add the "elfcorehdr=size@start" command line parameter to command line. */ 121 static void cmdline_add_elfcorehdr(struct kimage *image, unsigned long *cmdline_tmplen, 122 char *modified_cmdline, unsigned long elfcorehdr_sz) 123 { 124 int elfcorehdr_strlen = 0; 125 126 elfcorehdr_strlen = sprintf(modified_cmdline + (*cmdline_tmplen), "elfcorehdr=0x%lx@0x%lx ", 127 elfcorehdr_sz, image->elf_load_addr); 128 *cmdline_tmplen += elfcorehdr_strlen; 129 } 130 131 #endif 132 133 /* 134 * Try to add the initrd to the image. If it is not possible to find valid 135 * locations, this function will undo changes to the image and return non zero. 136 */ 137 int load_other_segments(struct kimage *image, 138 unsigned long kernel_load_addr, unsigned long kernel_size, 139 char *initrd, unsigned long initrd_len, char *cmdline, unsigned long cmdline_len) 140 { 141 int ret = 0; 142 unsigned long cmdline_tmplen = 0; 143 unsigned long initrd_load_addr = 0; 144 unsigned long orig_segments = image->nr_segments; 145 char *modified_cmdline = NULL; 146 struct kexec_buf kbuf; 147 148 kbuf.image = image; 149 /* Don't allocate anything below the kernel */ 150 kbuf.buf_min = kernel_load_addr + kernel_size; 151 152 modified_cmdline = kzalloc(COMMAND_LINE_SIZE, GFP_KERNEL); 153 if (!modified_cmdline) 154 return -EINVAL; 155 156 cmdline_add_loader(&cmdline_tmplen, modified_cmdline); 157 /* Ensure it's null terminated */ 158 modified_cmdline[COMMAND_LINE_SIZE - 1] = '\0'; 159 160 #ifdef CONFIG_CRASH_DUMP 161 /* Load elf core header */ 162 if (image->type == KEXEC_TYPE_CRASH) { 163 void *headers; 164 unsigned long headers_sz; 165 166 ret = prepare_elf_headers(&headers, &headers_sz); 167 if (ret < 0) { 168 pr_err("Preparing elf core header failed\n"); 169 goto out_err; 170 } 171 172 kbuf.buffer = headers; 173 kbuf.bufsz = headers_sz; 174 kbuf.mem = KEXEC_BUF_MEM_UNKNOWN; 175 kbuf.memsz = headers_sz; 176 kbuf.buf_align = SZ_64K; /* largest supported page size */ 177 kbuf.buf_max = ULONG_MAX; 178 kbuf.top_down = true; 179 180 ret = kexec_add_buffer(&kbuf); 181 if (ret < 0) { 182 vfree(headers); 183 goto out_err; 184 } 185 image->elf_headers = headers; 186 image->elf_load_addr = kbuf.mem; 187 image->elf_headers_sz = headers_sz; 188 189 kexec_dprintk("Loaded elf core header at 0x%lx bufsz=0x%lx memsz=0x%lx\n", 190 image->elf_load_addr, kbuf.bufsz, kbuf.memsz); 191 192 /* Add the mem=size@start parameter to the command line */ 193 cmdline_add_mem(&cmdline_tmplen, modified_cmdline); 194 195 /* Add the elfcorehdr=size@start parameter to the command line */ 196 cmdline_add_elfcorehdr(image, &cmdline_tmplen, modified_cmdline, headers_sz); 197 } 198 #endif 199 200 /* Load initrd */ 201 if (initrd) { 202 kbuf.buffer = initrd; 203 kbuf.bufsz = initrd_len; 204 kbuf.mem = KEXEC_BUF_MEM_UNKNOWN; 205 kbuf.memsz = initrd_len; 206 kbuf.buf_align = 0; 207 /* within 1GB-aligned window of up to 32GB in size */ 208 kbuf.buf_max = round_down(kernel_load_addr, SZ_1G) + (unsigned long)SZ_1G * 32; 209 kbuf.top_down = false; 210 211 ret = kexec_add_buffer(&kbuf); 212 if (ret < 0) 213 goto out_err; 214 initrd_load_addr = kbuf.mem; 215 216 kexec_dprintk("Loaded initrd at 0x%lx bufsz=0x%lx memsz=0x%lx\n", 217 initrd_load_addr, kbuf.bufsz, kbuf.memsz); 218 219 /* Add the initrd=start,size parameter to the command line */ 220 cmdline_add_initrd(image, &cmdline_tmplen, modified_cmdline, initrd_load_addr); 221 } 222 223 if (cmdline_len + cmdline_tmplen > COMMAND_LINE_SIZE) { 224 pr_err("Appending command line exceeds COMMAND_LINE_SIZE\n"); 225 ret = -EINVAL; 226 goto out_err; 227 } 228 229 memcpy(modified_cmdline + cmdline_tmplen, cmdline, cmdline_len); 230 cmdline = modified_cmdline; 231 image->arch.cmdline_ptr = (unsigned long)cmdline; 232 233 return 0; 234 235 out_err: 236 image->nr_segments = orig_segments; 237 kfree(modified_cmdline); 238 return ret; 239 } 240