16261586eSLiao Chang // SPDX-License-Identifier: GPL-2.0-only
26261586eSLiao Chang /*
36261586eSLiao Chang * Load ELF vmlinux file for the kexec_file_load syscall.
46261586eSLiao Chang *
56261586eSLiao Chang * Copyright (C) 2021 Huawei Technologies Co, Ltd.
66261586eSLiao Chang *
76261586eSLiao Chang * Author: Liao Chang (liaochang1@huawei.com)
86261586eSLiao Chang *
96261586eSLiao Chang * Based on kexec-tools' kexec-elf-riscv.c, heavily modified
106261586eSLiao Chang * for kernel.
116261586eSLiao Chang */
126261586eSLiao Chang
136261586eSLiao Chang #define pr_fmt(fmt) "kexec_image: " fmt
146261586eSLiao Chang
156261586eSLiao Chang #include <linux/elf.h>
166261586eSLiao Chang #include <linux/kexec.h>
176261586eSLiao Chang #include <linux/slab.h>
186261586eSLiao Chang #include <linux/of.h>
196261586eSLiao Chang #include <linux/libfdt.h>
206261586eSLiao Chang #include <linux/types.h>
218acea455SLi Zhengyu #include <linux/memblock.h>
220069455bSKent Overstreet #include <linux/vmalloc.h>
238acea455SLi Zhengyu #include <asm/setup.h>
246261586eSLiao Chang
arch_kimage_file_post_load_cleanup(struct kimage * image)2596df59b1SLi Huafei int arch_kimage_file_post_load_cleanup(struct kimage *image)
2696df59b1SLi Huafei {
2796df59b1SLi Huafei kvfree(image->arch.fdt);
2896df59b1SLi Huafei image->arch.fdt = NULL;
2996df59b1SLi Huafei
30cbc32023SLi Huafei vfree(image->elf_headers);
31cbc32023SLi Huafei image->elf_headers = NULL;
32cbc32023SLi Huafei image->elf_headers_sz = 0;
33cbc32023SLi Huafei
3496df59b1SLi Huafei return kexec_image_post_load_cleanup_default(image);
3596df59b1SLi Huafei }
3696df59b1SLi Huafei
riscv_kexec_elf_load(struct kimage * image,struct elfhdr * ehdr,struct kexec_elf_info * elf_info,unsigned long old_pbase,unsigned long new_pbase)376261586eSLiao Chang static int riscv_kexec_elf_load(struct kimage *image, struct elfhdr *ehdr,
386261586eSLiao Chang struct kexec_elf_info *elf_info, unsigned long old_pbase,
396261586eSLiao Chang unsigned long new_pbase)
406261586eSLiao Chang {
416261586eSLiao Chang int i;
426261586eSLiao Chang int ret = 0;
436261586eSLiao Chang size_t size;
446261586eSLiao Chang struct kexec_buf kbuf;
456261586eSLiao Chang const struct elf_phdr *phdr;
466261586eSLiao Chang
476261586eSLiao Chang kbuf.image = image;
486261586eSLiao Chang
496261586eSLiao Chang for (i = 0; i < ehdr->e_phnum; i++) {
506261586eSLiao Chang phdr = &elf_info->proghdrs[i];
516261586eSLiao Chang if (phdr->p_type != PT_LOAD)
526261586eSLiao Chang continue;
536261586eSLiao Chang
546261586eSLiao Chang size = phdr->p_filesz;
556261586eSLiao Chang if (size > phdr->p_memsz)
566261586eSLiao Chang size = phdr->p_memsz;
576261586eSLiao Chang
586261586eSLiao Chang kbuf.buffer = (void *) elf_info->buffer + phdr->p_offset;
596261586eSLiao Chang kbuf.bufsz = size;
606261586eSLiao Chang kbuf.buf_align = phdr->p_align;
616261586eSLiao Chang kbuf.mem = phdr->p_paddr - old_pbase + new_pbase;
626261586eSLiao Chang kbuf.memsz = phdr->p_memsz;
636261586eSLiao Chang kbuf.top_down = false;
646261586eSLiao Chang ret = kexec_add_buffer(&kbuf);
656261586eSLiao Chang if (ret)
666261586eSLiao Chang break;
676261586eSLiao Chang }
686261586eSLiao Chang
696261586eSLiao Chang return ret;
706261586eSLiao Chang }
716261586eSLiao Chang
726261586eSLiao Chang /*
736261586eSLiao Chang * Go through the available phsyical memory regions and find one that hold
746261586eSLiao Chang * an image of the specified size.
756261586eSLiao Chang */
elf_find_pbase(struct kimage * image,unsigned long kernel_len,struct elfhdr * ehdr,struct kexec_elf_info * elf_info,unsigned long * old_pbase,unsigned long * new_pbase)766261586eSLiao Chang static int elf_find_pbase(struct kimage *image, unsigned long kernel_len,
776261586eSLiao Chang struct elfhdr *ehdr, struct kexec_elf_info *elf_info,
786261586eSLiao Chang unsigned long *old_pbase, unsigned long *new_pbase)
796261586eSLiao Chang {
806261586eSLiao Chang int i;
816261586eSLiao Chang int ret;
826261586eSLiao Chang struct kexec_buf kbuf;
836261586eSLiao Chang const struct elf_phdr *phdr;
846261586eSLiao Chang unsigned long lowest_paddr = ULONG_MAX;
856261586eSLiao Chang unsigned long lowest_vaddr = ULONG_MAX;
866261586eSLiao Chang
876261586eSLiao Chang for (i = 0; i < ehdr->e_phnum; i++) {
886261586eSLiao Chang phdr = &elf_info->proghdrs[i];
896261586eSLiao Chang if (phdr->p_type != PT_LOAD)
906261586eSLiao Chang continue;
916261586eSLiao Chang
926261586eSLiao Chang if (lowest_paddr > phdr->p_paddr)
936261586eSLiao Chang lowest_paddr = phdr->p_paddr;
946261586eSLiao Chang
956261586eSLiao Chang if (lowest_vaddr > phdr->p_vaddr)
966261586eSLiao Chang lowest_vaddr = phdr->p_vaddr;
976261586eSLiao Chang }
986261586eSLiao Chang
996261586eSLiao Chang kbuf.image = image;
1006261586eSLiao Chang kbuf.buf_min = lowest_paddr;
1016261586eSLiao Chang kbuf.buf_max = ULONG_MAX;
1021bfb2b61SSong Shuai
1031bfb2b61SSong Shuai /*
1041bfb2b61SSong Shuai * Current riscv boot protocol requires 2MB alignment for
1051bfb2b61SSong Shuai * RV64 and 4MB alignment for RV32
1061bfb2b61SSong Shuai *
1071bfb2b61SSong Shuai */
1081bfb2b61SSong Shuai kbuf.buf_align = PMD_SIZE;
1096261586eSLiao Chang kbuf.mem = KEXEC_BUF_MEM_UNKNOWN;
1106261586eSLiao Chang kbuf.memsz = ALIGN(kernel_len, PAGE_SIZE);
1116261586eSLiao Chang kbuf.top_down = false;
1126261586eSLiao Chang ret = arch_kexec_locate_mem_hole(&kbuf);
1136261586eSLiao Chang if (!ret) {
1146261586eSLiao Chang *old_pbase = lowest_paddr;
1156261586eSLiao Chang *new_pbase = kbuf.mem;
1166261586eSLiao Chang image->start = ehdr->e_entry - lowest_vaddr + kbuf.mem;
1176261586eSLiao Chang }
1186261586eSLiao Chang return ret;
1196261586eSLiao Chang }
1206261586eSLiao Chang
1210978a63fSBaoquan He #ifdef CONFIG_CRASH_DUMP
get_nr_ram_ranges_callback(struct resource * res,void * arg)1228acea455SLi Zhengyu static int get_nr_ram_ranges_callback(struct resource *res, void *arg)
1238acea455SLi Zhengyu {
1248acea455SLi Zhengyu unsigned int *nr_ranges = arg;
1258acea455SLi Zhengyu
1268acea455SLi Zhengyu (*nr_ranges)++;
1278acea455SLi Zhengyu return 0;
1288acea455SLi Zhengyu }
1298acea455SLi Zhengyu
prepare_elf64_ram_headers_callback(struct resource * res,void * arg)1308acea455SLi Zhengyu static int prepare_elf64_ram_headers_callback(struct resource *res, void *arg)
1318acea455SLi Zhengyu {
1328acea455SLi Zhengyu struct crash_mem *cmem = arg;
1338acea455SLi Zhengyu
1348acea455SLi Zhengyu cmem->ranges[cmem->nr_ranges].start = res->start;
1358acea455SLi Zhengyu cmem->ranges[cmem->nr_ranges].end = res->end;
1368acea455SLi Zhengyu cmem->nr_ranges++;
1378acea455SLi Zhengyu
1388acea455SLi Zhengyu return 0;
1398acea455SLi Zhengyu }
1408acea455SLi Zhengyu
prepare_elf_headers(void ** addr,unsigned long * sz)1418acea455SLi Zhengyu static int prepare_elf_headers(void **addr, unsigned long *sz)
1428acea455SLi Zhengyu {
1438acea455SLi Zhengyu struct crash_mem *cmem;
1448acea455SLi Zhengyu unsigned int nr_ranges;
1458acea455SLi Zhengyu int ret;
1468acea455SLi Zhengyu
1478acea455SLi Zhengyu nr_ranges = 1; /* For exclusion of crashkernel region */
1488acea455SLi Zhengyu walk_system_ram_res(0, -1, &nr_ranges, get_nr_ram_ranges_callback);
1498acea455SLi Zhengyu
1508acea455SLi Zhengyu cmem = kmalloc(struct_size(cmem, ranges, nr_ranges), GFP_KERNEL);
1518acea455SLi Zhengyu if (!cmem)
1528acea455SLi Zhengyu return -ENOMEM;
1538acea455SLi Zhengyu
1548acea455SLi Zhengyu cmem->max_nr_ranges = nr_ranges;
1558acea455SLi Zhengyu cmem->nr_ranges = 0;
1568acea455SLi Zhengyu ret = walk_system_ram_res(0, -1, cmem, prepare_elf64_ram_headers_callback);
1578acea455SLi Zhengyu if (ret)
1588acea455SLi Zhengyu goto out;
1598acea455SLi Zhengyu
1608acea455SLi Zhengyu /* Exclude crashkernel region */
1618acea455SLi Zhengyu ret = crash_exclude_mem_range(cmem, crashk_res.start, crashk_res.end);
1628acea455SLi Zhengyu if (!ret)
1638acea455SLi Zhengyu ret = crash_prepare_elf64_headers(cmem, true, addr, sz);
1648acea455SLi Zhengyu
1658acea455SLi Zhengyu out:
1668acea455SLi Zhengyu kfree(cmem);
1678acea455SLi Zhengyu return ret;
1688acea455SLi Zhengyu }
1698acea455SLi Zhengyu
setup_kdump_cmdline(struct kimage * image,char * cmdline,unsigned long cmdline_len)1708acea455SLi Zhengyu static char *setup_kdump_cmdline(struct kimage *image, char *cmdline,
1718acea455SLi Zhengyu unsigned long cmdline_len)
1728acea455SLi Zhengyu {
1738acea455SLi Zhengyu int elfcorehdr_strlen;
1748acea455SLi Zhengyu char *cmdline_ptr;
1758acea455SLi Zhengyu
1768acea455SLi Zhengyu cmdline_ptr = kzalloc(COMMAND_LINE_SIZE, GFP_KERNEL);
1778acea455SLi Zhengyu if (!cmdline_ptr)
1788acea455SLi Zhengyu return NULL;
1798acea455SLi Zhengyu
1808acea455SLi Zhengyu elfcorehdr_strlen = sprintf(cmdline_ptr, "elfcorehdr=0x%lx ",
1818acea455SLi Zhengyu image->elf_load_addr);
1828acea455SLi Zhengyu
1838acea455SLi Zhengyu if (elfcorehdr_strlen + cmdline_len > COMMAND_LINE_SIZE) {
1848acea455SLi Zhengyu pr_err("Appending elfcorehdr=<addr> exceeds cmdline size\n");
1858acea455SLi Zhengyu kfree(cmdline_ptr);
1868acea455SLi Zhengyu return NULL;
1878acea455SLi Zhengyu }
1888acea455SLi Zhengyu
1898acea455SLi Zhengyu memcpy(cmdline_ptr + elfcorehdr_strlen, cmdline, cmdline_len);
1908acea455SLi Zhengyu /* Ensure it's nul terminated */
1918acea455SLi Zhengyu cmdline_ptr[COMMAND_LINE_SIZE - 1] = '\0';
1928acea455SLi Zhengyu return cmdline_ptr;
1938acea455SLi Zhengyu }
1940978a63fSBaoquan He #endif
1958acea455SLi Zhengyu
elf_kexec_load(struct kimage * image,char * kernel_buf,unsigned long kernel_len,char * initrd,unsigned long initrd_len,char * cmdline,unsigned long cmdline_len)1966261586eSLiao Chang static void *elf_kexec_load(struct kimage *image, char *kernel_buf,
1976261586eSLiao Chang unsigned long kernel_len, char *initrd,
1986261586eSLiao Chang unsigned long initrd_len, char *cmdline,
1996261586eSLiao Chang unsigned long cmdline_len)
2006261586eSLiao Chang {
2016261586eSLiao Chang int ret;
2020978a63fSBaoquan He void *fdt;
2036261586eSLiao Chang unsigned long old_kernel_pbase = ULONG_MAX;
2046261586eSLiao Chang unsigned long new_kernel_pbase = 0UL;
2056261586eSLiao Chang unsigned long initrd_pbase = 0UL;
206838b3e28SLi Zhengyu unsigned long kernel_start;
2076261586eSLiao Chang struct elfhdr ehdr;
2086261586eSLiao Chang struct kexec_buf kbuf;
2096261586eSLiao Chang struct kexec_elf_info elf_info;
2108acea455SLi Zhengyu char *modified_cmdline = NULL;
2116261586eSLiao Chang
2126261586eSLiao Chang ret = kexec_build_elf_info(kernel_buf, kernel_len, &ehdr, &elf_info);
2136261586eSLiao Chang if (ret)
2146261586eSLiao Chang return ERR_PTR(ret);
2156261586eSLiao Chang
2166261586eSLiao Chang ret = elf_find_pbase(image, kernel_len, &ehdr, &elf_info,
2176261586eSLiao Chang &old_kernel_pbase, &new_kernel_pbase);
2186261586eSLiao Chang if (ret)
2196261586eSLiao Chang goto out;
220838b3e28SLi Zhengyu kernel_start = image->start;
2216261586eSLiao Chang
2226261586eSLiao Chang /* Add the kernel binary to the image */
2236261586eSLiao Chang ret = riscv_kexec_elf_load(image, &ehdr, &elf_info,
2246261586eSLiao Chang old_kernel_pbase, new_kernel_pbase);
2256261586eSLiao Chang if (ret)
2266261586eSLiao Chang goto out;
2276261586eSLiao Chang
2286261586eSLiao Chang kbuf.image = image;
2296261586eSLiao Chang kbuf.buf_min = new_kernel_pbase + kernel_len;
2306261586eSLiao Chang kbuf.buf_max = ULONG_MAX;
2318acea455SLi Zhengyu
2320978a63fSBaoquan He #ifdef CONFIG_CRASH_DUMP
2338acea455SLi Zhengyu /* Add elfcorehdr */
2348acea455SLi Zhengyu if (image->type == KEXEC_TYPE_CRASH) {
2350978a63fSBaoquan He void *headers;
2360978a63fSBaoquan He unsigned long headers_sz;
2378acea455SLi Zhengyu ret = prepare_elf_headers(&headers, &headers_sz);
2388acea455SLi Zhengyu if (ret) {
2398acea455SLi Zhengyu pr_err("Preparing elf core header failed\n");
2408acea455SLi Zhengyu goto out;
2418acea455SLi Zhengyu }
2428acea455SLi Zhengyu
2438acea455SLi Zhengyu kbuf.buffer = headers;
2448acea455SLi Zhengyu kbuf.bufsz = headers_sz;
2458acea455SLi Zhengyu kbuf.mem = KEXEC_BUF_MEM_UNKNOWN;
2468acea455SLi Zhengyu kbuf.memsz = headers_sz;
2478acea455SLi Zhengyu kbuf.buf_align = ELF_CORE_HEADER_ALIGN;
2488acea455SLi Zhengyu kbuf.top_down = true;
2498acea455SLi Zhengyu
2508acea455SLi Zhengyu ret = kexec_add_buffer(&kbuf);
2518acea455SLi Zhengyu if (ret) {
2528acea455SLi Zhengyu vfree(headers);
2538acea455SLi Zhengyu goto out;
2548acea455SLi Zhengyu }
2558acea455SLi Zhengyu image->elf_headers = headers;
2568acea455SLi Zhengyu image->elf_load_addr = kbuf.mem;
2578acea455SLi Zhengyu image->elf_headers_sz = headers_sz;
2588acea455SLi Zhengyu
259eb7622d9SBaoquan He kexec_dprintk("Loaded elf core header at 0x%lx bufsz=0x%lx memsz=0x%lx\n",
2608acea455SLi Zhengyu image->elf_load_addr, kbuf.bufsz, kbuf.memsz);
2618acea455SLi Zhengyu
2628acea455SLi Zhengyu /* Setup cmdline for kdump kernel case */
2638acea455SLi Zhengyu modified_cmdline = setup_kdump_cmdline(image, cmdline,
2648acea455SLi Zhengyu cmdline_len);
2658acea455SLi Zhengyu if (!modified_cmdline) {
2668acea455SLi Zhengyu pr_err("Setting up cmdline for kdump kernel failed\n");
2678acea455SLi Zhengyu ret = -EINVAL;
2688acea455SLi Zhengyu goto out;
2698acea455SLi Zhengyu }
2708acea455SLi Zhengyu cmdline = modified_cmdline;
2718acea455SLi Zhengyu }
2720978a63fSBaoquan He #endif
2738acea455SLi Zhengyu
274e6265fe7SEric DeVolder #ifdef CONFIG_ARCH_SUPPORTS_KEXEC_PURGATORY
275838b3e28SLi Zhengyu /* Add purgatory to the image */
276838b3e28SLi Zhengyu kbuf.top_down = true;
277838b3e28SLi Zhengyu kbuf.mem = KEXEC_BUF_MEM_UNKNOWN;
278838b3e28SLi Zhengyu ret = kexec_load_purgatory(image, &kbuf);
279838b3e28SLi Zhengyu if (ret) {
280838b3e28SLi Zhengyu pr_err("Error loading purgatory ret=%d\n", ret);
281838b3e28SLi Zhengyu goto out;
282838b3e28SLi Zhengyu }
283eb7622d9SBaoquan He kexec_dprintk("Loaded purgatory at 0x%lx\n", kbuf.mem);
284eb7622d9SBaoquan He
285838b3e28SLi Zhengyu ret = kexec_purgatory_get_set_symbol(image, "riscv_kernel_entry",
286838b3e28SLi Zhengyu &kernel_start,
287838b3e28SLi Zhengyu sizeof(kernel_start), 0);
288838b3e28SLi Zhengyu if (ret)
289838b3e28SLi Zhengyu pr_err("Error update purgatory ret=%d\n", ret);
290e6265fe7SEric DeVolder #endif /* CONFIG_ARCH_SUPPORTS_KEXEC_PURGATORY */
291838b3e28SLi Zhengyu
2926261586eSLiao Chang /* Add the initrd to the image */
2936261586eSLiao Chang if (initrd != NULL) {
2946261586eSLiao Chang kbuf.buffer = initrd;
2956261586eSLiao Chang kbuf.bufsz = kbuf.memsz = initrd_len;
2966261586eSLiao Chang kbuf.buf_align = PAGE_SIZE;
29749af7a2cSTorsten Duwe kbuf.top_down = true;
2986261586eSLiao Chang kbuf.mem = KEXEC_BUF_MEM_UNKNOWN;
2996261586eSLiao Chang ret = kexec_add_buffer(&kbuf);
3006261586eSLiao Chang if (ret)
3016261586eSLiao Chang goto out;
3026261586eSLiao Chang initrd_pbase = kbuf.mem;
303eb7622d9SBaoquan He kexec_dprintk("Loaded initrd at 0x%lx\n", initrd_pbase);
3046261586eSLiao Chang }
3056261586eSLiao Chang
3066261586eSLiao Chang /* Add the DTB to the image */
3076261586eSLiao Chang fdt = of_kexec_alloc_and_setup_fdt(image, initrd_pbase,
3086261586eSLiao Chang initrd_len, cmdline, 0);
3096261586eSLiao Chang if (!fdt) {
3106261586eSLiao Chang pr_err("Error setting up the new device tree.\n");
3116261586eSLiao Chang ret = -EINVAL;
3126261586eSLiao Chang goto out;
3136261586eSLiao Chang }
3146261586eSLiao Chang
3156261586eSLiao Chang fdt_pack(fdt);
3166261586eSLiao Chang kbuf.buffer = fdt;
3176261586eSLiao Chang kbuf.bufsz = kbuf.memsz = fdt_totalsize(fdt);
3186261586eSLiao Chang kbuf.buf_align = PAGE_SIZE;
3196261586eSLiao Chang kbuf.mem = KEXEC_BUF_MEM_UNKNOWN;
3206261586eSLiao Chang kbuf.top_down = true;
3216261586eSLiao Chang ret = kexec_add_buffer(&kbuf);
3226261586eSLiao Chang if (ret) {
3236261586eSLiao Chang pr_err("Error add DTB kbuf ret=%d\n", ret);
3246261586eSLiao Chang goto out_free_fdt;
3256261586eSLiao Chang }
32696df59b1SLi Huafei /* Cache the fdt buffer address for memory cleanup */
32796df59b1SLi Huafei image->arch.fdt = fdt;
328eb7622d9SBaoquan He kexec_dprintk("Loaded device tree at 0x%lx\n", kbuf.mem);
3296261586eSLiao Chang goto out;
3306261586eSLiao Chang
3316261586eSLiao Chang out_free_fdt:
3326261586eSLiao Chang kvfree(fdt);
3336261586eSLiao Chang out:
3348acea455SLi Zhengyu kfree(modified_cmdline);
3356261586eSLiao Chang kexec_free_elf_info(&elf_info);
3366261586eSLiao Chang return ret ? ERR_PTR(ret) : NULL;
3376261586eSLiao Chang }
3386261586eSLiao Chang
339838b3e28SLi Zhengyu #define RV_X(x, s, n) (((x) >> (s)) & ((1 << (n)) - 1))
340838b3e28SLi Zhengyu #define RISCV_IMM_BITS 12
341838b3e28SLi Zhengyu #define RISCV_IMM_REACH (1LL << RISCV_IMM_BITS)
342838b3e28SLi Zhengyu #define RISCV_CONST_HIGH_PART(x) \
343838b3e28SLi Zhengyu (((x) + (RISCV_IMM_REACH >> 1)) & ~(RISCV_IMM_REACH - 1))
344838b3e28SLi Zhengyu #define RISCV_CONST_LOW_PART(x) ((x) - RISCV_CONST_HIGH_PART(x))
345838b3e28SLi Zhengyu
346838b3e28SLi Zhengyu #define ENCODE_ITYPE_IMM(x) \
347838b3e28SLi Zhengyu (RV_X(x, 0, 12) << 20)
348838b3e28SLi Zhengyu #define ENCODE_BTYPE_IMM(x) \
349838b3e28SLi Zhengyu ((RV_X(x, 1, 4) << 8) | (RV_X(x, 5, 6) << 25) | \
350838b3e28SLi Zhengyu (RV_X(x, 11, 1) << 7) | (RV_X(x, 12, 1) << 31))
351838b3e28SLi Zhengyu #define ENCODE_UTYPE_IMM(x) \
352838b3e28SLi Zhengyu (RV_X(x, 12, 20) << 12)
353838b3e28SLi Zhengyu #define ENCODE_JTYPE_IMM(x) \
354838b3e28SLi Zhengyu ((RV_X(x, 1, 10) << 21) | (RV_X(x, 11, 1) << 20) | \
355838b3e28SLi Zhengyu (RV_X(x, 12, 8) << 12) | (RV_X(x, 20, 1) << 31))
356838b3e28SLi Zhengyu #define ENCODE_CBTYPE_IMM(x) \
357838b3e28SLi Zhengyu ((RV_X(x, 1, 2) << 3) | (RV_X(x, 3, 2) << 10) | (RV_X(x, 5, 1) << 2) | \
358838b3e28SLi Zhengyu (RV_X(x, 6, 2) << 5) | (RV_X(x, 8, 1) << 12))
359838b3e28SLi Zhengyu #define ENCODE_CJTYPE_IMM(x) \
360838b3e28SLi Zhengyu ((RV_X(x, 1, 3) << 3) | (RV_X(x, 4, 1) << 11) | (RV_X(x, 5, 1) << 2) | \
361838b3e28SLi Zhengyu (RV_X(x, 6, 1) << 7) | (RV_X(x, 7, 1) << 6) | (RV_X(x, 8, 2) << 9) | \
362838b3e28SLi Zhengyu (RV_X(x, 10, 1) << 8) | (RV_X(x, 11, 1) << 12))
363838b3e28SLi Zhengyu #define ENCODE_UJTYPE_IMM(x) \
364838b3e28SLi Zhengyu (ENCODE_UTYPE_IMM(RISCV_CONST_HIGH_PART(x)) | \
365838b3e28SLi Zhengyu (ENCODE_ITYPE_IMM(RISCV_CONST_LOW_PART(x)) << 32))
366838b3e28SLi Zhengyu #define ENCODE_UITYPE_IMM(x) \
367838b3e28SLi Zhengyu (ENCODE_UTYPE_IMM(x) | (ENCODE_ITYPE_IMM(x) << 32))
368838b3e28SLi Zhengyu
369838b3e28SLi Zhengyu #define CLEAN_IMM(type, x) \
370838b3e28SLi Zhengyu ((~ENCODE_##type##_IMM((uint64_t)(-1))) & (x))
371838b3e28SLi Zhengyu
arch_kexec_apply_relocations_add(struct purgatory_info * pi,Elf_Shdr * section,const Elf_Shdr * relsec,const Elf_Shdr * symtab)372838b3e28SLi Zhengyu int arch_kexec_apply_relocations_add(struct purgatory_info *pi,
373838b3e28SLi Zhengyu Elf_Shdr *section,
374838b3e28SLi Zhengyu const Elf_Shdr *relsec,
375838b3e28SLi Zhengyu const Elf_Shdr *symtab)
376838b3e28SLi Zhengyu {
377838b3e28SLi Zhengyu const char *strtab, *name, *shstrtab;
378838b3e28SLi Zhengyu const Elf_Shdr *sechdrs;
379a927444aSLi Zhengyu Elf64_Rela *relas;
380838b3e28SLi Zhengyu int i, r_type;
381838b3e28SLi Zhengyu
382838b3e28SLi Zhengyu /* String & section header string table */
383838b3e28SLi Zhengyu sechdrs = (void *)pi->ehdr + pi->ehdr->e_shoff;
384838b3e28SLi Zhengyu strtab = (char *)pi->ehdr + sechdrs[symtab->sh_link].sh_offset;
385838b3e28SLi Zhengyu shstrtab = (char *)pi->ehdr + sechdrs[pi->ehdr->e_shstrndx].sh_offset;
386838b3e28SLi Zhengyu
387838b3e28SLi Zhengyu relas = (void *)pi->ehdr + relsec->sh_offset;
388838b3e28SLi Zhengyu
389838b3e28SLi Zhengyu for (i = 0; i < relsec->sh_size / sizeof(*relas); i++) {
390838b3e28SLi Zhengyu const Elf_Sym *sym; /* symbol to relocate */
391838b3e28SLi Zhengyu unsigned long addr; /* final location after relocation */
392838b3e28SLi Zhengyu unsigned long val; /* relocated symbol value */
393838b3e28SLi Zhengyu unsigned long sec_base; /* relocated symbol value */
394838b3e28SLi Zhengyu void *loc; /* tmp location to modify */
395838b3e28SLi Zhengyu
396838b3e28SLi Zhengyu sym = (void *)pi->ehdr + symtab->sh_offset;
397838b3e28SLi Zhengyu sym += ELF64_R_SYM(relas[i].r_info);
398838b3e28SLi Zhengyu
399838b3e28SLi Zhengyu if (sym->st_name)
400838b3e28SLi Zhengyu name = strtab + sym->st_name;
401838b3e28SLi Zhengyu else
402838b3e28SLi Zhengyu name = shstrtab + sechdrs[sym->st_shndx].sh_name;
403838b3e28SLi Zhengyu
404838b3e28SLi Zhengyu loc = pi->purgatory_buf;
405838b3e28SLi Zhengyu loc += section->sh_offset;
406838b3e28SLi Zhengyu loc += relas[i].r_offset;
407838b3e28SLi Zhengyu
408838b3e28SLi Zhengyu if (sym->st_shndx == SHN_ABS)
409838b3e28SLi Zhengyu sec_base = 0;
410838b3e28SLi Zhengyu else if (sym->st_shndx >= pi->ehdr->e_shnum) {
411838b3e28SLi Zhengyu pr_err("Invalid section %d for symbol %s\n",
412838b3e28SLi Zhengyu sym->st_shndx, name);
413838b3e28SLi Zhengyu return -ENOEXEC;
414838b3e28SLi Zhengyu } else
415838b3e28SLi Zhengyu sec_base = pi->sechdrs[sym->st_shndx].sh_addr;
416838b3e28SLi Zhengyu
417838b3e28SLi Zhengyu val = sym->st_value;
418838b3e28SLi Zhengyu val += sec_base;
419838b3e28SLi Zhengyu val += relas[i].r_addend;
420838b3e28SLi Zhengyu
421838b3e28SLi Zhengyu addr = section->sh_addr + relas[i].r_offset;
422838b3e28SLi Zhengyu
423838b3e28SLi Zhengyu r_type = ELF64_R_TYPE(relas[i].r_info);
424838b3e28SLi Zhengyu
425838b3e28SLi Zhengyu switch (r_type) {
426838b3e28SLi Zhengyu case R_RISCV_BRANCH:
427838b3e28SLi Zhengyu *(u32 *)loc = CLEAN_IMM(BTYPE, *(u32 *)loc) |
428838b3e28SLi Zhengyu ENCODE_BTYPE_IMM(val - addr);
429838b3e28SLi Zhengyu break;
430838b3e28SLi Zhengyu case R_RISCV_JAL:
431838b3e28SLi Zhengyu *(u32 *)loc = CLEAN_IMM(JTYPE, *(u32 *)loc) |
432838b3e28SLi Zhengyu ENCODE_JTYPE_IMM(val - addr);
433838b3e28SLi Zhengyu break;
434838b3e28SLi Zhengyu /*
435838b3e28SLi Zhengyu * With no R_RISCV_PCREL_LO12_S, R_RISCV_PCREL_LO12_I
436838b3e28SLi Zhengyu * sym is expected to be next to R_RISCV_PCREL_HI20
437838b3e28SLi Zhengyu * in purgatory relsec. Handle it like R_RISCV_CALL
438838b3e28SLi Zhengyu * sym, instead of searching the whole relsec.
439838b3e28SLi Zhengyu */
440838b3e28SLi Zhengyu case R_RISCV_PCREL_HI20:
441d0b4f95aSTorsten Duwe case R_RISCV_CALL_PLT:
442838b3e28SLi Zhengyu case R_RISCV_CALL:
443838b3e28SLi Zhengyu *(u64 *)loc = CLEAN_IMM(UITYPE, *(u64 *)loc) |
444838b3e28SLi Zhengyu ENCODE_UJTYPE_IMM(val - addr);
445838b3e28SLi Zhengyu break;
446838b3e28SLi Zhengyu case R_RISCV_RVC_BRANCH:
447838b3e28SLi Zhengyu *(u32 *)loc = CLEAN_IMM(CBTYPE, *(u32 *)loc) |
448838b3e28SLi Zhengyu ENCODE_CBTYPE_IMM(val - addr);
449838b3e28SLi Zhengyu break;
450838b3e28SLi Zhengyu case R_RISCV_RVC_JUMP:
451838b3e28SLi Zhengyu *(u32 *)loc = CLEAN_IMM(CJTYPE, *(u32 *)loc) |
452838b3e28SLi Zhengyu ENCODE_CJTYPE_IMM(val - addr);
453838b3e28SLi Zhengyu break;
454*c6ebf2c5SYing Sun case R_RISCV_ADD16:
455*c6ebf2c5SYing Sun *(u16 *)loc += val;
456*c6ebf2c5SYing Sun break;
457*c6ebf2c5SYing Sun case R_RISCV_SUB16:
458*c6ebf2c5SYing Sun *(u16 *)loc -= val;
459*c6ebf2c5SYing Sun break;
460838b3e28SLi Zhengyu case R_RISCV_ADD32:
461838b3e28SLi Zhengyu *(u32 *)loc += val;
462838b3e28SLi Zhengyu break;
463838b3e28SLi Zhengyu case R_RISCV_SUB32:
464838b3e28SLi Zhengyu *(u32 *)loc -= val;
465838b3e28SLi Zhengyu break;
466838b3e28SLi Zhengyu /* It has been applied by R_RISCV_PCREL_HI20 sym */
467838b3e28SLi Zhengyu case R_RISCV_PCREL_LO12_I:
468838b3e28SLi Zhengyu case R_RISCV_ALIGN:
469838b3e28SLi Zhengyu case R_RISCV_RELAX:
470838b3e28SLi Zhengyu break;
471838b3e28SLi Zhengyu default:
472838b3e28SLi Zhengyu pr_err("Unknown rela relocation: %d\n", r_type);
473838b3e28SLi Zhengyu return -ENOEXEC;
474838b3e28SLi Zhengyu }
475838b3e28SLi Zhengyu }
476838b3e28SLi Zhengyu return 0;
477838b3e28SLi Zhengyu }
478838b3e28SLi Zhengyu
4796261586eSLiao Chang const struct kexec_file_ops elf_kexec_ops = {
4806261586eSLiao Chang .probe = kexec_elf_probe,
4816261586eSLiao Chang .load = elf_kexec_load,
4826261586eSLiao Chang };
483