用户空间的汇编程序

发布于 2024-08-19 10:59:57 字数 341 浏览 5 评论 0原文

用户空间中是否可以有这样的一段代码? 我的意思是是否可以在 Netbsd / Linux 的用户空间中读/写协处理器寄存器?

XYZ]# cat pmc.c 

static inline int
arm11_pmc_ctrl_read(void)
{
    unsigned int val;

    __asm volatile ("mrc p15, 0, %0, c15, c12, 0" : "=r" (val));

    return val;
}
int main(){
    unsigned int ctrl;
        ctrl = arm11_pmc_ctrl_read();
}

Is it possible to have a piece of code like this in user space?
I mean is it possible to read/write co-processor register in user space in Netbsd / Linux?

XYZ]# cat pmc.c 

static inline int
arm11_pmc_ctrl_read(void)
{
    unsigned int val;

    __asm volatile ("mrc p15, 0, %0, c15, c12, 0" : "=r" (val));

    return val;
}
int main(){
    unsigned int ctrl;
        ctrl = arm11_pmc_ctrl_read();
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

空城缀染半城烟沙 2024-08-26 10:59:57

用户空间无权访问特权指令或寄存器。查看汇编程序手册,了解您使用的指令和寄存器是否具有特权。

User space does not have access to privileged instructions or registers. Look in your assembler manual to find out if the instructions and registers you are using are privileged.

晌融 2024-08-26 10:59:57

您可能必须通过在其上应用 suid 位来更改二进制可执行文件的权限,并且它将以 root 身份运行,我知道,这可能听起来像一个安全漏洞,但不幸的是,root 拥有运行它的权限,而不是普通用户。

或者您可以使用 mknod 创建一个设备,即 /dev/mydev 并编写一个设备驱动程序,普通用户可以在其中与设备驱动程序交互,而设备驱动程序又是在内核空间中运行并执行汇编魔法并将其返回到用户空间,这种方法是更好的选择。

希望这有帮助,
此致,
汤姆.

You may have to change the permissions of the binary executable by applying a suid bit on it and it will run as root, I know, it may sound like a security hole, but unfortunately, root would have that privilege to run it, and not the normal user.

Or you could create a device i.e. a /dev/mydev using mknod and write a device driver in which the normal user can then interact with the device driver, which in turn is running in kernel space and do the assembly magic and return it back to the userland space, this method is preferable.

Hope this helps,
Best regards,
Tom.

ζ澈沫 2024-08-26 10:59:57

是的,您可以作为用户读取/写入协处理器寄存器。例如,所有浮点指令都是协处理器指令,用户空间二进制文件非常愉快地调用它们,从 ARM 寄存器读取/写入 FPU 寄存器值。

指令的可用性取决于CPU模式,这在用户进程中与内核在做某事时是不同的,因此可能某些指令,无论是在协处理器上还是在主处理器上,只允许在内核模式下使用。停止 CPU 的指令是一个非 coproc 示例。

如果您在评论中说了神秘的 mrc 指令应该实现什么目标,那么会更容易判断这是否是特权指令。

希望有帮助

Yes, you can read/write coprocessor registers as a user. For example, all floating point instructions are coprocessor instructions, and user-space binaries call them quite happily, reading/writing FPU register values to/from the ARM registers.

Instruction availability depends on the CPU mode, which is different in user processes than it is while the kernel is doing something, so it may be that some instructions, whether on the coprocesor or the main processor, are only allowed in kernel mode. The instruction to halt the CPU is one non-coproc example.

If you said in a comment what the cryptic mrc instruction is supposed to achieve it would be easier to tell if that is a privileged instruction or not.

Hope that helps

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文