arch/arm64/kernel/alternative.c
Source file repositories/reference/linux-study-clean/arch/arm64/kernel/alternative.c
File Facts
- System
- Linux kernel
- Corpus path
arch/arm64/kernel/alternative.c- Extension
.c- Size
- 7850 bytes
- Lines
- 306
- Domain
- Architecture Layer
- Bucket
- arch/arm64
- Inferred role
- Architecture Layer: exported/initcall integration point
- Status
- integration 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.
- Exports symbols or registers init work; inspect boot/module ordering and who consumes the exported contract.
- Defines or uses C structs; map object ownership, embedded links, reference counts, and lock ownership.
Dependency Surface
linux/init.hlinux/cpu.hlinux/elf.hasm/cacheflush.hasm/alternative.hasm/cpufeature.hasm/insn.hasm/module.hasm/sections.hasm/vdso.hlinux/stop_machine.h
Detected Declarations
struct alt_regionfunction alternative_is_appliedfunction branch_insn_requires_updatefunction get_alt_insnfunction patch_alternativefunction clean_dcache_range_nopatchfunction __apply_alternativesfunction flush_module_icachefunction apply_alternatives_vdsofunction __apply_alternatives_multi_stopfunction apply_alternatives_allfunction processfunction apply_alternatives_modulefunction alt_cb_patch_nopsexport alt_cb_patch_nops
Annotated Snippet
struct alt_region {
struct alt_instr *begin;
struct alt_instr *end;
};
bool alternative_is_applied(u16 cpucap)
{
if (WARN_ON(cpucap >= ARM64_NCAPS))
return false;
return test_bit(cpucap, applied_alternatives);
}
/*
* Check if the target PC is within an alternative block.
*/
static __always_inline bool branch_insn_requires_update(struct alt_instr *alt, unsigned long pc)
{
unsigned long replptr = (unsigned long)ALT_REPL_PTR(alt);
return !(pc >= replptr && pc <= (replptr + alt->alt_len));
}
#define align_down(x, a) ((unsigned long)(x) & ~(((unsigned long)(a)) - 1))
static __always_inline u32 get_alt_insn(struct alt_instr *alt, __le32 *insnptr, __le32 *altinsnptr)
{
u32 insn;
insn = le32_to_cpu(*altinsnptr);
if (aarch64_insn_is_branch_imm(insn)) {
s32 offset = aarch64_get_branch_offset(insn);
unsigned long target;
target = (unsigned long)altinsnptr + offset;
/*
* If we're branching inside the alternate sequence,
* do not rewrite the instruction, as it is already
* correct. Otherwise, generate the new instruction.
*/
if (branch_insn_requires_update(alt, target)) {
offset = target - (unsigned long)insnptr;
insn = aarch64_set_branch_offset(insn, offset);
}
} else if (aarch64_insn_is_adrp(insn)) {
s32 orig_offset, new_offset;
unsigned long target;
/*
* If we're replacing an adrp instruction, which uses PC-relative
* immediate addressing, adjust the offset to reflect the new
* PC. adrp operates on 4K aligned addresses.
*/
orig_offset = aarch64_insn_adrp_get_offset(insn);
target = align_down(altinsnptr, SZ_4K) + orig_offset;
new_offset = target - align_down(insnptr, SZ_4K);
insn = aarch64_insn_adrp_set_offset(insn, new_offset);
} else if (aarch64_insn_uses_literal(insn)) {
/*
* Disallow patching unhandled instructions using PC relative
* literal addresses
*/
BUG();
}
return insn;
}
static noinstr void patch_alternative(struct alt_instr *alt,
__le32 *origptr, __le32 *updptr, int nr_inst)
{
__le32 *replptr;
int i;
replptr = ALT_REPL_PTR(alt);
for (i = 0; i < nr_inst; i++) {
u32 insn;
insn = get_alt_insn(alt, origptr + i, replptr + i);
updptr[i] = cpu_to_le32(insn);
}
}
/*
* We provide our own, private D-cache cleaning function so that we don't
* accidentally call into the cache.S code, which is patched by us at
* runtime.
*/
static noinstr void clean_dcache_range_nopatch(u64 start, u64 end)
Annotation
- Immediate include surface: `linux/init.h`, `linux/cpu.h`, `linux/elf.h`, `asm/cacheflush.h`, `asm/alternative.h`, `asm/cpufeature.h`, `asm/insn.h`, `asm/module.h`.
- Detected declarations: `struct alt_region`, `function alternative_is_applied`, `function branch_insn_requires_update`, `function get_alt_insn`, `function patch_alternative`, `function clean_dcache_range_nopatch`, `function __apply_alternatives`, `function flush_module_icache`, `function apply_alternatives_vdso`, `function __apply_alternatives_multi_stop`.
- Atlas domain: Architecture Layer / arch/arm64.
- Implementation status: integration 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.