views:

150

answers:

3

Hi,

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();
}
+1  A: 

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.

Alex Brown
A: 

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.

tommieb75
Being root or not makes no difference to which instructions you can execute. What makes a difference is the mode the CPU is runnig in, which is different between a user process and when the kernel is running. After all, the user-id of a process is no more than an integer stored in a structure in the kernel's memory.
martinwguy
The only apparent exception to this could be if a user process executed a privileged inst, the kernel catches the illegal instruction trap, decides that it's ok, performs the instruction for you, then returns you invisibly to the instruction after the privileged one. This is how floating point instructions are sometimes emulated in the absence of a physical FPU, or for hacks to run armv5t code on armv4t processors (e.g. http://benno.id.au/blog/2007/11/21/android-neo1973) but I've never heard of this mechanism being used to filter privileged instructions to root processes only.
martinwguy
A: 

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

martinwguy