arch/x86/kernel/kprobes/opt.c
Source file repositories/reference/linux-study-clean/arch/x86/kernel/kprobes/opt.c
File Facts
- System
- Linux kernel
- Corpus path
arch/x86/kernel/kprobes/opt.c- Extension
.c- Size
- 14827 bytes
- Lines
- 552
- Domain
- Architecture Layer
- Bucket
- arch/x86
- 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/kprobes.hlinux/perf_event.hlinux/ptrace.hlinux/string.hlinux/slab.hlinux/hardirq.hlinux/preempt.hlinux/extable.hlinux/kdebug.hlinux/kallsyms.hlinux/kgdb.hlinux/ftrace.hlinux/objtool.hlinux/pgtable.hlinux/static_call.hasm/text-patching.hasm/cacheflush.hasm/desc.hlinux/uaccess.hasm/alternative.hasm/insn.hasm/debugreg.hasm/set_memory.hasm/sections.hasm/nospec-branch.hcommon.h
Detected Declarations
function Optimizationfunction synthesize_clacfunction synthesize_set_arg1function optimized_callbackfunction copy_optimized_instructionsfunction insn_is_indirect_jumpfunction insn_jump_into_rangefunction can_optimizefunction arch_check_optimized_kprobefunction arch_within_optimized_kprobefunction __arch_remove_optimized_kprobefunction arch_remove_optimized_kprobefunction relocatablefunction breakpointsfunction list_for_each_entry_safefunction jumpfunction arch_unoptimize_kprobesfunction list_for_each_entry_safefunction setup_detour_execution
Annotated Snippet
if (kp && kprobe_optimized(kp)) {
op = container_of(kp, struct optimized_kprobe, kp);
/* If op is optimized or under unoptimizing */
if (list_empty(&op->list) || optprobe_queued_unopt(op))
goto found;
}
}
return addr;
found:
/*
* If the kprobe can be optimized, original bytes which can be
* overwritten by jump destination address. In this case, original
* bytes must be recovered from op->optinsn.copied_insn buffer.
*/
if (copy_from_kernel_nofault(buf, (void *)addr,
MAX_INSN_SIZE * sizeof(kprobe_opcode_t)))
return 0UL;
if (addr == (unsigned long)kp->addr) {
buf[0] = kp->opcode;
memcpy(buf + 1, op->optinsn.copied_insn, DISP32_SIZE);
} else {
offs = addr - (unsigned long)kp->addr - 1;
memcpy(buf, op->optinsn.copied_insn + offs, DISP32_SIZE - offs);
}
return (unsigned long)buf;
}
static void synthesize_clac(kprobe_opcode_t *addr)
{
/*
* Can't be static_cpu_has() due to how objtool treats this feature bit.
* This isn't a fast path anyway.
*/
if (!boot_cpu_has(X86_FEATURE_SMAP))
return;
/* Replace the NOP3 with CLAC */
addr[0] = 0x0f;
addr[1] = 0x01;
addr[2] = 0xca;
}
/* Insert a move instruction which sets a pointer to eax/rdi (1st arg). */
static void synthesize_set_arg1(kprobe_opcode_t *addr, unsigned long val)
{
#ifdef CONFIG_X86_64
*addr++ = 0x48;
*addr++ = 0xbf;
#else
*addr++ = 0xb8;
#endif
*(unsigned long *)addr = val;
}
asm (
".pushsection .rodata\n"
".global optprobe_template_entry\n"
"optprobe_template_entry:\n"
#ifdef CONFIG_X86_64
" pushq $" __stringify(__KERNEL_DS) "\n"
/* Save the 'sp - 8', this will be fixed later. */
" pushq %rsp\n"
" pushfq\n"
".global optprobe_template_clac\n"
"optprobe_template_clac:\n"
ASM_NOP3
SAVE_REGS_STRING
" movq %rsp, %rsi\n"
".global optprobe_template_val\n"
"optprobe_template_val:\n"
ASM_NOP5
ASM_NOP5
".global optprobe_template_call\n"
"optprobe_template_call:\n"
ASM_NOP5
/* Copy 'regs->flags' into 'regs->ss'. */
" movq 18*8(%rsp), %rdx\n"
" movq %rdx, 20*8(%rsp)\n"
RESTORE_REGS_STRING
/* Skip 'regs->flags' and 'regs->sp'. */
" addq $16, %rsp\n"
/* And pop flags register from 'regs->ss'. */
" popfq\n"
#else /* CONFIG_X86_32 */
" pushl %ss\n"
/* Save the 'sp - 4', this will be fixed later. */
" pushl %esp\n"
Annotation
- Immediate include surface: `linux/kprobes.h`, `linux/perf_event.h`, `linux/ptrace.h`, `linux/string.h`, `linux/slab.h`, `linux/hardirq.h`, `linux/preempt.h`, `linux/extable.h`.
- Detected declarations: `function Optimization`, `function synthesize_clac`, `function synthesize_set_arg1`, `function optimized_callback`, `function copy_optimized_instructions`, `function insn_is_indirect_jump`, `function insn_jump_into_range`, `function can_optimize`, `function arch_check_optimized_kprobe`, `function arch_within_optimized_kprobe`.
- Atlas domain: Architecture Layer / arch/x86.
- 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.