hulk inclusion category: feature bugzilla: https://gitee.com/openeuler/kernel/issues/I8MGE6
--------------------------------
Add breakpoint exception optimization support to improve livepatch success rate for arm.
Signed-off-by: Yang Jihong yangjihong1@huawei.com Signed-off-by: Zheng Yejian zhengyejian1@huawei.com --- arch/arm/include/asm/livepatch.h | 9 +++++++ arch/arm/kernel/livepatch.c | 42 ++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+)
diff --git a/arch/arm/include/asm/livepatch.h b/arch/arm/include/asm/livepatch.h index 892231ca55ce..62a65bdc85a2 100644 --- a/arch/arm/include/asm/livepatch.h +++ b/arch/arm/include/asm/livepatch.h @@ -30,8 +30,15 @@ #define LJMP_INSN_SIZE 1 #endif /* CONFIG_ARM_MODULE_PLTS */
+#define KLP_ARM_BREAKPOINT_INSTRUCTION 0xe7f001f9 + struct arch_klp_data { u32 old_insns[LJMP_INSN_SIZE]; + /* + * Saved opcode at the entry of the old func (which maybe replaced + * with breakpoint). + */ + u32 saved_opcode; };
#define KLP_MAX_REPLACE_SIZE sizeof_field(struct arch_klp_data, old_insns) @@ -45,6 +52,8 @@ void arch_klp_unpatch_func(struct klp_func *func); long arch_klp_save_old_code(struct arch_klp_data *arch_data, void *old_func); bool arch_check_jump_insn(unsigned long func_addr); int arch_klp_check_calltrace(bool (*check_func)(void *, int *, unsigned long), void *data); +int arch_klp_add_breakpoint(struct arch_klp_data *arch_data, void *old_func); +void arch_klp_remove_breakpoint(struct arch_klp_data *arch_data, void *old_func);
#endif /* CONFIG_LIVEPATCH_WO_FTRACE */
diff --git a/arch/arm/kernel/livepatch.c b/arch/arm/kernel/livepatch.c index 193cfcd0aca5..6a041aa003be 100644 --- a/arch/arm/kernel/livepatch.c +++ b/arch/arm/kernel/livepatch.c @@ -142,6 +142,48 @@ int arch_klp_check_calltrace(bool (*check_func)(void *, int *, unsigned long), v return do_check_calltrace(&args, klp_check_jump_func); }
+int arch_klp_add_breakpoint(struct arch_klp_data *arch_data, void *old_func) +{ + u32 *addr = (u32 *)old_func; + + arch_data->saved_opcode = le32_to_cpu(*addr); + patch_text(old_func, KLP_ARM_BREAKPOINT_INSTRUCTION); + return 0; +} + +void arch_klp_remove_breakpoint(struct arch_klp_data *arch_data, void *old_func) +{ + patch_text(old_func, arch_data->saved_opcode); +} + +static int klp_trap_handler(struct pt_regs *regs, unsigned int instr) +{ + void *brk_func = NULL; + unsigned long addr = regs->ARM_pc; + + brk_func = klp_get_brk_func((void *)addr); + if (!brk_func) { + pr_warn("Unrecoverable livepatch detected.\n"); + BUG(); + } + + regs->ARM_pc = (unsigned long)brk_func; + return 0; +} + +static struct undef_hook klp_arm_break_hook = { + .instr_mask = 0x0fffffff, + .instr_val = (KLP_ARM_BREAKPOINT_INSTRUCTION & 0x0fffffff), + .cpsr_mask = MODE_MASK, + .cpsr_val = SVC_MODE, + .fn = klp_trap_handler, +}; + +void arch_klp_init(void) +{ + register_undef_hook(&klp_arm_break_hook); +} + static inline bool offset_in_range(unsigned long pc, unsigned long addr, long range) {