使用sudo运行ARM协处理器指令

时间:2016-11-10 20:59:06

标签: c++ assembly raspberry-pi arm inline-assembly

我的目标是写入协处理器p15寄存器,现在我只是想读它。

我有以下示例c ++程序,第一个asm指令只是一个简单的ror,它运行得很好。第二个asm指令,我试图只读SCTLR寄存器。

我使用g ++ test_program.cpp -o test编译程序 并使用./test或sudo ./test

运行

如果我使用./test运行,我会得到输出:

Value: 10   Result: 8
Illegal instruction

如果我使用sudo ./test运行,我会得到:

Value: 10   Result: 8

很明显,该指令不起作用,因为它不打印“在这里”或“SCTLR”这一行。还有什么我必须做的事情来执行协处理器读取吗?我在覆盆子pi(Cortex A-53)上运行它。

#include <cstdlib>
#include <cstdio>

int main(){
unsigned int y, x;
x = 16;

//Assembly rotate right 1 example
asm("mov    %[result], %[value], ror #1"

           : [result]"=r" (y) /* Rotation result. */
           : [value]"r"   (x) /* Rotated value. */
           : /* No clobbers */
    );

printf("Value: %x   Result: %x\n", x, y);


// Assembly read SCTLR, program crashes if this is run
unsigned int id = 0;
asm("MRC    p15, 0, %[result], c0, c0, 0"
           : [result]"=r" (id)  //Rotation result.
    );

printf("Got here\n");

printf("SCTLR: %x", id);


return 0;
}

0 个答案:

没有答案
相关问题