arch/x86/kernel/hw_breakpoint.c
Source file repositories/reference/linux-study-clean/arch/x86/kernel/hw_breakpoint.c
File Facts
- System
- Linux kernel
- Corpus path
arch/x86/kernel/hw_breakpoint.c- Extension
.c- Size
- 14311 bytes
- Lines
- 594
- Domain
- Architecture Layer
- Bucket
- arch/x86
- 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/perf_event.hlinux/hw_breakpoint.hlinux/irqflags.hlinux/notifier.hlinux/kallsyms.hlinux/kprobes.hlinux/percpu.hlinux/kdebug.hlinux/kernel.hlinux/kvm_types.hlinux/export.hlinux/sched.hlinux/smp.hasm/hw_breakpoint.hasm/processor.hasm/debugreg.hasm/user.hasm/desc.hasm/tlbflush.h
Detected Declarations
function __encode_dr7function encode_dr7function decode_dr7function arch_install_hw_breakpointfunction arch_uninstall_hw_breakpointfunction arch_bp_generic_lenfunction arch_bp_generic_fieldsfunction arch_check_bp_in_kernelspacefunction within_areafunction within_cpu_entryfunction for_each_possible_cpufunction arch_build_bp_infofunction hw_breakpoint_arch_parsefunction flush_ptrace_hw_breakpointfunction hw_breakpoint_restorefunction do_debugfunction hw_breakpoint_exceptions_notifyfunction hw_breakpoint_pmu_read
Annotated Snippet
if (!*slot) {
*slot = bp;
break;
}
}
if (WARN_ONCE(i == HBP_NUM, "Can't find any breakpoint slot"))
return -EBUSY;
set_debugreg(info->address, i);
__this_cpu_write(cpu_debugreg[i], info->address);
dr7 = this_cpu_ptr(&cpu_dr7);
*dr7 |= encode_dr7(i, info->len, info->type);
/*
* Ensure we first write cpu_dr7 before we set the DR7 register.
* This ensures an NMI never see cpu_dr7 0 when DR7 is not.
*/
barrier();
set_debugreg(*dr7, 7);
if (info->mask)
amd_set_dr_addr_mask(info->mask, i);
return 0;
}
/*
* Uninstall the breakpoint contained in the given counter.
*
* First we search the debug address register it uses and then we disable
* it.
*
* Atomic: we hold the counter->ctx->lock and we only handle variables
* and registers local to this cpu.
*/
void arch_uninstall_hw_breakpoint(struct perf_event *bp)
{
struct arch_hw_breakpoint *info = counter_arch_bp(bp);
unsigned long dr7;
int i;
lockdep_assert_irqs_disabled();
for (i = 0; i < HBP_NUM; i++) {
struct perf_event **slot = this_cpu_ptr(&bp_per_reg[i]);
if (*slot == bp) {
*slot = NULL;
break;
}
}
if (WARN_ONCE(i == HBP_NUM, "Can't find any breakpoint slot"))
return;
dr7 = this_cpu_read(cpu_dr7);
dr7 &= ~__encode_dr7(i, info->len, info->type);
set_debugreg(dr7, 7);
if (info->mask)
amd_set_dr_addr_mask(0, i);
/*
* Ensure the write to cpu_dr7 is after we've set the DR7 register.
* This ensures an NMI never see cpu_dr7 0 when DR7 is not.
*/
barrier();
this_cpu_write(cpu_dr7, dr7);
}
static int arch_bp_generic_len(int x86_len)
{
switch (x86_len) {
case X86_BREAKPOINT_LEN_1:
return HW_BREAKPOINT_LEN_1;
case X86_BREAKPOINT_LEN_2:
return HW_BREAKPOINT_LEN_2;
case X86_BREAKPOINT_LEN_4:
return HW_BREAKPOINT_LEN_4;
#ifdef CONFIG_X86_64
case X86_BREAKPOINT_LEN_8:
return HW_BREAKPOINT_LEN_8;
#endif
default:
return -EINVAL;
}
}
Annotation
- Immediate include surface: `linux/perf_event.h`, `linux/hw_breakpoint.h`, `linux/irqflags.h`, `linux/notifier.h`, `linux/kallsyms.h`, `linux/kprobes.h`, `linux/percpu.h`, `linux/kdebug.h`.
- Detected declarations: `function __encode_dr7`, `function encode_dr7`, `function decode_dr7`, `function arch_install_hw_breakpoint`, `function arch_uninstall_hw_breakpoint`, `function arch_bp_generic_len`, `function arch_bp_generic_fields`, `function arch_check_bp_in_kernelspace`, `function within_area`, `function within_cpu_entry`.
- Atlas domain: Architecture Layer / arch/x86.
- 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.