how to set control register 0 (cr0) bits in x86-64 using gcc assembly on linux

前端 未结 5 2246
抹茶落季
抹茶落季 2021-02-08 20:29

I am using the following code to set the cr0 bit to disable cache. When I compile this

#include 

int main()
{
        __asm__(\"pushl  %eax\\n\\t         


        
5条回答
  •  遇见更好的自我
    2021-02-08 21:07

    Ok, so finally I wrote the following kernel module. Am not sure it is right, since I don't observe the drastic slowdown which should accompany when you disable cache. But this compiles and inserts properly.

    Any pointers will be helpful.

    Thanks!

    #include 
    #include 
    MODULE_LICENSE("Dual BSD/GPL");
    static int hello_init(void)
    {
            printk(KERN_ALERT "Hello, world\n");
            __asm__("push   %rax\n\t"
                    "mov    %cr0,%rax;\n\t"
                    "or     $(1 << 30),%rax;\n\t"
                    "mov    %rax,%cr0;\n\t"
                    "wbinvd\n\t"
                    "pop    %rax"
    );
            return 0;
    }
    static void hello_exit(void)
    {
            printk(KERN_ALERT "Goodbye, cruel world\n");
            __asm__("push   %rax\n\t"
                    "mov    %cr0,%rax;\n\t"
                    "and     $~(1 << 30),%rax;\n\t"
                    "mov    %rax,%cr0;\n\t"
                    "wbinvd\n\t"
                    "pop    %rax"
    );
    }
    module_init(hello_init);
    module_exit(hello_exit);
    

提交回复
热议问题