如何在Linux上使用gcc程序集在x86-64中设置控制寄存器0(cr0)位 [英] how to set control register 0 (cr0) bits in x86-64 using gcc assembly on linux

查看:68
本文介绍了如何在Linux上使用gcc程序集在x86-64中设置控制寄存器0(cr0)位的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下代码将cr0位置1以禁用缓存.当我编译这个

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

#include <stdio.h>

int main()
{
        __asm__("pushl  %eax\n\t"
                "mov    %cr0,%eax;\n\t"
                "orl    $(1 << 30),%eax;\n\t"
                "mov    %eax,%cr0;\n\t"
                "wbinvd\n\t"
                "popl   %eax"
);

        return 0;
}

我收到错误消息,说操作数对mov无效.

I am getting error saying that the operands are invalid for mov.

有人可以为我提供一份出色的gcc x86-64指南,以帮助您完成这些事情吗?上面的代码到底还存在什么问题?

Can anyone please point me to a good gcc x86-64 guide for doing these kinds of things? Also what exactly is wrong with the above code?

推荐答案

好,所以最后我编写了以下内核模块.不确定是否正确,因为我没有看到禁用缓存时会伴随的急剧减速.但这可以正确编译并插入.

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.

谢谢!

#include <linux/init.h>
#include <linux/module.h>
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);

这篇关于如何在Linux上使用gcc程序集在x86-64中设置控制寄存器0(cr0)位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆