I'm trying to set bit 30 of cr0 register with inline assembly. I'm using the following assembly in my kernel module,
__asm__ (
"mov %%cr0, %%rax\n\t"
"or 0x40000000, %%eax\n\t"
"mov %%rax, %%cr0\n\t"
::
:"%rax"
);
My module compiles but upon inserting the module my terminal freezes. From a new terminal when I try to remove the module, it shows the following,
rmmod: ERROR: Module xxx is in use
and dmesg shows the following in red color,
RIP [<ffffffffc09c604c>] hello_start+0x4c/0x1000 [ModuleName]
how to set control register 0 (cr0) bits in x86-64 using gcc assembly on linux and Trying to disable paging through cr0 register also talk about the same problem. I try to follow their solution but I just can't make it work. Any help, where am I making mistake in my inline assembly?
Update post:
I have fixed the code according @prl suggestions, following is my full source code,
u64 get_cr0(void){
u64 cr0;
__asm__ (
"mov %%cr0, %%rax\n\t"
"mov %%eax, %0\n\t"
: "=m" (cr0)
: /* no input */
: "%rax"
);
return cr0;
}
static int __init hello_start(void){
printk(KERN_INFO "Loading hello module...\n");
printk(KERN_INFO "Hello world\n");
printk(KERN_INFO "cr0 = 0x%8.8X\n", get_cr0());
__asm__ (
"mov %%cr0, %%rax\n\t"
"or $0x40000000, %%eax\n\t"
"mov %%rax, %%cr0\n\t"
::
:"%rax"
);
printk(KERN_INFO "cr0 after change = 0x%8.8X\n", get_cr0());
return 0;
}
static void __exit hello_end(void){
printk(KERN_INFO "Goodbye Mr.\n");
__asm__ (
"mov %%cr0, %%rax\n\t"
"and $~(0x40000000), %%eax\n\t"
"mov %%rax, %%cr0\n\t"
::
:"%rax"
);
}
Indeed, my system is running super slow after loading the module. But after changing the bit I still did not see any difference in cr0 register value. Following is the output in dmesg
[ +0.000400] Loading hello module...
[ +0.000001] Hello world
[ +0.000002] cr0 = 0x80050033
[ +0.000312] cr0 after change = 0x80050033
[ +6.085675] perf interrupt took too long (2522 > 2500), lowering kernel.perf_event_max_sample_rate to 50000
Why can't I see the change in bit 30 of the cr0 register?
Therefore, Upon removing the module, I tried to clear bit 30 hoping my system will start to respond normally. But seems like that did not work. My system is still running slow. Any thoughts, how to bring back the system to its normal functional state after modifying the cr0?