Skip to content

Commit

Permalink
Optimize CR0 register update for Kernel 5.3.0+ (matpool#16)
Browse files Browse the repository at this point in the history
Signed-off-by: Limstash <[email protected]>
  • Loading branch information
limstash committed Jan 13, 2024
1 parent feab0f6 commit e7a2558
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions src/mpu_syscall_hook.c
Original file line number Diff line number Diff line change
Expand Up @@ -82,14 +82,28 @@ static dev_t get_rdev(unsigned int fd)
return rdev;
}

#if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 3, 0)
static inline void write_cr0_forced(unsigned long val)
{
unsigned long __force_order;

asm volatile(
"mov %0, %%cr0"
: "+r"(val), "+m"(__force_order));
}
#define WRITE_CR0(f) write_cr0_forced(f)
#else
#define WRITE_CR0(f) write_cr0(f)
#endif

static void write_syscall(unsigned long **syscall_tbl, ioctl_fn sys_ioctl)
{
unsigned long local_cr0;

local_cr0 = read_cr0();
write_cr0(local_cr0 & ~0x00010000);
WRITE_CR0(local_cr0 & ~0x00010000);
syscall_tbl[__NR_ioctl] = (unsigned long *)sys_ioctl;
write_cr0(local_cr0);
WRITE_CR0(local_cr0);
}

int mpu_init_ioctl_hook(mpu_module_t *module, mpu_ctx_t *ctx)
Expand Down

0 comments on commit e7a2558

Please sign in to comment.