arch/riscv/kernel/machine_kexec_file.c
Source file repositories/reference/linux-study-clean/arch/riscv/kernel/machine_kexec_file.c
File Facts
- System
- Linux kernel
- Corpus path
arch/riscv/kernel/machine_kexec_file.c- Extension
.c- Size
- 9999 bytes
- Lines
- 377
- Domain
- Architecture Layer
- Bucket
- arch/riscv
- Inferred role
- Architecture Layer: implementation source
- Status
- source implementation candidate
Why This File Exists
CPU and platform-specific kernel glue: boot entry, traps, syscall entry, interrupts, page tables, context switch, and low-level barriers.
- CPU and platform-specific kernel glue: boot entry, traps, syscall entry, interrupts, page tables, context switch, and low-level barriers.
- Allocates kernel memory; connect allocation flags and lifetime to context constraints.
- Defines or uses C structs; map object ownership, embedded links, reference counts, and lock ownership.
Dependency Surface
linux/kexec.hlinux/elf.hlinux/slab.hlinux/of.hlinux/libfdt.hlinux/types.hlinux/memblock.hlinux/pfn.hlinux/vmalloc.hasm/setup.hasm/insn.h
Detected Declarations
function arch_kimage_file_post_load_cleanupfunction get_nr_ram_ranges_callbackfunction prepare_elf64_ram_headers_callbackfunction prepare_elf_headersfunction arch_kexec_apply_relocations_addfunction load_extra_segments
Annotated Snippet
else if (sym->st_shndx >= pi->ehdr->e_shnum) {
pr_err("Invalid section %d for symbol %s\n",
sym->st_shndx, name);
return -ENOEXEC;
} else
sec_base = pi->sechdrs[sym->st_shndx].sh_addr;
val = sym->st_value;
val += sec_base;
val += relas[i].r_addend;
addr = section->sh_addr + relas[i].r_offset;
r_type = ELF64_R_TYPE(relas[i].r_info);
switch (r_type) {
case R_RISCV_BRANCH:
*(u32 *)loc = CLEAN_IMM(BTYPE, *(u32 *)loc) |
ENCODE_BTYPE_IMM(val - addr);
break;
case R_RISCV_JAL:
*(u32 *)loc = CLEAN_IMM(JTYPE, *(u32 *)loc) |
ENCODE_JTYPE_IMM(val - addr);
break;
/*
* With no R_RISCV_PCREL_LO12_S, R_RISCV_PCREL_LO12_I
* sym is expected to be next to R_RISCV_PCREL_HI20
* in purgatory relsec. Handle it like R_RISCV_CALL
* sym, instead of searching the whole relsec.
*/
case R_RISCV_PCREL_HI20:
case R_RISCV_CALL_PLT:
case R_RISCV_CALL:
*(u64 *)loc = CLEAN_IMM(UITYPE, *(u64 *)loc) |
ENCODE_UJTYPE_IMM(val - addr);
break;
case R_RISCV_RVC_BRANCH:
*(u32 *)loc = CLEAN_IMM(CBTYPE, *(u32 *)loc) |
ENCODE_CBTYPE_IMM(val - addr);
break;
case R_RISCV_RVC_JUMP:
*(u32 *)loc = CLEAN_IMM(CJTYPE, *(u32 *)loc) |
ENCODE_CJTYPE_IMM(val - addr);
break;
case R_RISCV_ADD16:
*(u16 *)loc += val;
break;
case R_RISCV_SUB16:
*(u16 *)loc -= val;
break;
case R_RISCV_ADD32:
*(u32 *)loc += val;
break;
case R_RISCV_SUB32:
*(u32 *)loc -= val;
break;
/* It has been applied by R_RISCV_PCREL_HI20 sym */
case R_RISCV_PCREL_LO12_I:
case R_RISCV_ALIGN:
case R_RISCV_RELAX:
break;
case R_RISCV_64:
*(u64 *)loc = val;
break;
default:
pr_err("Unknown rela relocation: %d\n", r_type);
return -ENOEXEC;
}
}
return 0;
}
int load_extra_segments(struct kimage *image, unsigned long kernel_start,
unsigned long kernel_len, char *initrd,
unsigned long initrd_len, char *cmdline,
unsigned long cmdline_len)
{
int ret;
void *fdt;
unsigned long initrd_pbase = 0UL;
struct kexec_buf kbuf = {};
char *modified_cmdline = NULL;
kbuf.image = image;
kbuf.buf_min = kernel_start + kernel_len;
kbuf.buf_max = PFN_PHYS(max_low_pfn);
#ifdef CONFIG_CRASH_DUMP
/* Add elfcorehdr */
Annotation
- Immediate include surface: `linux/kexec.h`, `linux/elf.h`, `linux/slab.h`, `linux/of.h`, `linux/libfdt.h`, `linux/types.h`, `linux/memblock.h`, `linux/pfn.h`.
- Detected declarations: `function arch_kimage_file_post_load_cleanup`, `function get_nr_ram_ranges_callback`, `function prepare_elf64_ram_headers_callback`, `function prepare_elf_headers`, `function arch_kexec_apply_relocations_add`, `function load_extra_segments`.
- Atlas domain: Architecture Layer / arch/riscv.
- Implementation status: source implementation candidate.
Implementation Notes
- This generated page is the file-by-file coverage layer; curated subsystem chapters should link here when they synthesize a multi-file control flow.
- Core OS pages should be promoted from atlas-only to deep-reviewed when they explain data structures, invariants, locking, lifecycle, and C implementation snippets.
- Driver-family pages are intentionally pattern-oriented unless they are part of the selected PCIe/NVMe representative device path.