--- trap_hook.c.orig 2020-03-23 05:00:10.000000000 +0300 +++ trap_hook.c 2024-03-17 17:03:40.867117029 +0300 @@ -10,6 +10,8 @@ #define pr_fmt(fmt) "OPEMU: " fmt +#define HAVE_INV_OP_HANDLER 1 + #include #include #include @@ -19,12 +21,18 @@ #include #include +#ifdef HAVE_INV_OP_HANDLER +#include +#endif + #include "opemu.h" MODULE_DESCRIPTION("Intel Instruction set Emulation"); MODULE_AUTHOR("Meowthra"); MODULE_LICENSE("GPL"); +#ifndef HAVE_INV_OP_HANDLER + #define USE_FENTRY_OFFSET 0 #if defined(CONFIG_X86_64) && (LINUX_VERSION_CODE >= KERNEL_VERSION(4,17,0)) @@ -256,13 +264,33 @@ //HOOK("do_trap", fh_do_trap, &orig_do_trap), }; +#else /* !HAVE_INV_OP_HANDLER */ + +int custom_inv_op_handler(struct pt_regs *regs) { + + if (user_mode(regs)) { + int tmp_res; + tmp_res = opemu_utrap(regs); + return tmp_res; + } + return 0; +} + +#endif + static int fh_init(void) { int err; +#ifdef HAVE_INV_OP_HANDLER + setup_invalid_op_handler(custom_inv_op_handler); + pr_info("OPEMU is using custom_inv_op_handler.\n"); +#else err = fh_install_hooks(demo_hooks, ARRAY_SIZE(demo_hooks)); if (err) return err; + pr_info("OPEMU is using ftrace.\n"); +#endif pr_info("module loaded\n"); return 0; @@ -271,7 +299,11 @@ static void fh_exit(void) { +#ifdef HAVE_INV_OP_HANDLER + remove_invalid_op_handler(); +#else fh_remove_hooks(demo_hooks, ARRAY_SIZE(demo_hooks)); +#endif pr_info("module unloaded\n"); }