既然你说你只是在“玩弄”,我将建议一个有点肮脏但非常简单的解决方案。
ARM 的 Linux 内核有自己处理未定义指令的方式来模拟它们,这是通过简单的“未定义指令挂钩”完成的,在 arch/arm64/include/asm/traps.h 中定义:
struct undef_hook {
struct list_head node;
u32 instr_mask;
u32 instr_val;
u64 pstate_mask;
u64 pstate_val;
int (*fn)(struct pt_regs *regs, u32 instr);
};
这些钩子是通过(很遗憾没有导出)函数register_undef_hook() 添加的,并通过unregister_undef_hook() 删除。
要解决您的问题,您有两种选择:
-
通过修改arch/arm64/kernel/traps.c添加以下两行代码来导出这两个函数:
// after register_undef_hook
EXPORT_SYMBOL(register_undef_hook);
// after unregister_undef_hook
EXPORT_SYMBOL(unregister_undef_hook);
现在重新编译内核,函数将被导出并可在模块中使用。您现在可以按照自己的方式轻松处理未定义的指令。
使用kallsyms_lookup_name() 在运行时直接从您的模块中查找符号,无需重新编译内核。有点混乱,但可能更容易,而且总体上肯定是一个更快的解决方案。
对于选项 #1,这里有一个示例模块,可以完全满足您的需求:
// SPDX-License-Identifier: GPL-3.0
#include <linux/init.h> // module_{init,exit}()
#include <linux/module.h> // THIS_MODULE, MODULE_VERSION, ...
#include <asm/traps.h> // struct undef_hook, register_undef_hook()
#include <asm/ptrace.h> // struct pt_regs
#ifdef pr_fmt
#undef pr_fmt
#endif
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
static void whoops(void)
{
// Execute a known invalid instruction.
asm volatile (".word 0xf7f0a000");
}
static int undef_instr_handler(struct pt_regs *regs, u32 instr)
{
pr_info("*gotcha*\n");
// Just skip over to the next instruction.
regs->pc += 4;
return 0; // All fine!
}
static struct undef_hook uh = {
.instr_mask = 0x0, // any instruction
.instr_val = 0x0, // any instruction
.pstate_mask = 0x0, // any pstate
.pstate_val = 0x0, // any pstate
.fn = undef_instr_handler
};
static int __init modinit(void)
{
register_undef_hook(&uh);
pr_info("Jumping off a cliff...\n");
whoops();
pr_info("Woah, I survived!\n");
return 0;
}
static void __exit modexit(void)
{
unregister_undef_hook(&uf);
}
module_init(modinit);
module_exit(modexit);
MODULE_VERSION("0.1");
MODULE_DESCRIPTION("Test undefined instruction handling on arm64.");
MODULE_AUTHOR("Marco Bonelli");
MODULE_LICENSE("GPL");
对于选项#2,您只需修改上面的代码添加以下内容:
#include <linux/kallsyms.h> // kallsyms_lookup_name()
// Define two global pointers.
static void (*register_undef_hook_ptr)(struct undef_hook *);
static void (*unregister_undef_hook_ptr)(struct undef_hook *);
static int __init modinit(void)
{
// Lookup wanted symbols.
register_undef_hook_ptr = (void *)kallsyms_lookup_name("register_undef_hook");
unregister_undef_hook_ptr = (void *)kallsyms_lookup_name("unregister_undef_hook");
if (!register_undef_hook_ptr)
return -EFAULT;
// ...
return 0;
}
static void __exit modexit(void)
{
if (unregister_undef_hook_ptr)
unregister_undef_hook_ptr(&uh);
}
这是dmesg 的输出:
[ 1.508253] testmod: Jumping off a cliff...
[ 1.508781] testmod: *gotcha*
[ 1.509207] testmod: Woah, I survived!
一些注意事项
-
以上示例将undef_hook 指令/pstate 掩码/值设置为0x0,这意味着将为执行的任何 未定义指令调用挂钩。您可能希望将其限制为msr XX,YY,并且您应该可以这样做:
// didn't test these, you might want to double-check
.instr_mask = 0xfff00000,
.instr_val = 0xd5100000,
0xfff00000 匹配除操作数之外的所有内容(根据the manual,PDF 的第 779 页)。你可以look at the source code 看看这些值是如何被检查来决定是否调用钩子的,这很简单。您还可以检查传递给挂钩的instr 值:pr_info("Instr: %x\n", instr)。
从 cmets 看来,上面的内容似乎不太正确,我对 ARM 的了解并不多,无法对这些值给出正确答案,但应该很容易解决。
您可以查看struct pt_regs 以了解它是如何定义的。您可能只想跳过指令并打印一些东西,在这种情况下,我在上面的示例中所做的就足够了。如果您愿意,您可以更改任何寄存器值。
在 Linux 内核 v5.6,qemu-system-aarch64 上测试。