arch/riscv/kernel/patch.c
Source file repositories/reference/linux-study-clean/arch/riscv/kernel/patch.c
File Facts
- System
- Linux kernel
- Corpus path
arch/riscv/kernel/patch.c- Extension
.c- Size
- 7274 bytes
- Lines
- 303
- 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.
- Uses kernel synchronization; read lock ordering, sleepability, and interrupt context assumptions before translating.
- 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/spinlock.hlinux/mm.hlinux/memory.hlinux/string.hlinux/uaccess.hlinux/stop_machine.hasm/kprobes.hasm/cacheflush.hasm/fixmap.hasm/ftrace.hasm/text-patching.hasm/sections.h
Detected Declarations
struct patch_insnfunction is_kernel_exittextfunction fix_to_virtfunction patch_unmapfunction __patch_insn_setfunction __patch_insn_writefunction __patch_insn_setfunction __patch_insn_writefunction patch_insn_setfunction __patch_insn_setfunction patch_text_set_nosyncfunction patch_insn_writefunction __patch_insn_writefunction patch_text_nosyncfunction patch_text_cbfunction patch_text
Annotated Snippet
struct patch_insn {
void *addr;
u32 *insns;
size_t len;
atomic_t cpu_count;
};
int riscv_patch_in_stop_machine = false;
#ifdef CONFIG_MMU
static inline bool is_kernel_exittext(uintptr_t addr)
{
return system_state < SYSTEM_RUNNING &&
addr >= (uintptr_t)__exittext_begin &&
addr < (uintptr_t)__exittext_end;
}
/*
* The fix_to_virt(, idx) needs a const value (not a dynamic variable of
* reg-a0) or BUILD_BUG_ON failed with "idx >= __end_of_fixed_addresses".
* So use '__always_inline' and 'const unsigned int fixmap' here.
*/
static __always_inline void *patch_map(void *addr, const unsigned int fixmap)
{
uintptr_t uintaddr = (uintptr_t) addr;
phys_addr_t phys;
if (core_kernel_text(uintaddr) || is_kernel_exittext(uintaddr)) {
phys = __pa_symbol(addr);
} else if (IS_ENABLED(CONFIG_STRICT_MODULE_RWX)) {
struct page *page = vmalloc_to_page(addr);
BUG_ON(!page);
phys = page_to_phys(page) + offset_in_page(addr);
} else {
return addr;
}
return (void *)set_fixmap_offset(fixmap, phys);
}
static void patch_unmap(int fixmap)
{
clear_fixmap(fixmap);
}
NOKPROBE_SYMBOL(patch_unmap);
static int __patch_insn_set(void *addr, u8 c, size_t len)
{
bool across_pages = (offset_in_page(addr) + len) > PAGE_SIZE;
void *waddr = addr;
/*
* Only two pages can be mapped at a time for writing.
*/
if (len + offset_in_page(addr) > 2 * PAGE_SIZE)
return -EINVAL;
/*
* Before reaching here, it was expected to lock the text_mutex
* already, so we don't need to give another lock here and could
* ensure that it was safe between each cores.
*/
lockdep_assert_held(&text_mutex);
preempt_disable();
if (across_pages)
patch_map(addr + PAGE_SIZE, FIX_TEXT_POKE1);
waddr = patch_map(addr, FIX_TEXT_POKE0);
memset(waddr, c, len);
/*
* We could have just patched a function that is about to be
* called so make sure we don't execute partially patched
* instructions by flushing the icache as soon as possible.
*/
local_flush_icache_range((unsigned long)waddr,
(unsigned long)waddr + len);
patch_unmap(FIX_TEXT_POKE0);
if (across_pages)
patch_unmap(FIX_TEXT_POKE1);
preempt_enable();
return 0;
Annotation
- Immediate include surface: `linux/spinlock.h`, `linux/mm.h`, `linux/memory.h`, `linux/string.h`, `linux/uaccess.h`, `linux/stop_machine.h`, `asm/kprobes.h`, `asm/cacheflush.h`.
- Detected declarations: `struct patch_insn`, `function is_kernel_exittext`, `function fix_to_virt`, `function patch_unmap`, `function __patch_insn_set`, `function __patch_insn_write`, `function __patch_insn_set`, `function __patch_insn_write`, `function patch_insn_set`, `function __patch_insn_set`.
- Atlas domain: Architecture Layer / arch/riscv.
- Implementation status: source implementation candidate.
- Synchronization appears in or near this file; preserve lock ordering, sleepability, and interrupt-context constraints.
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.