assembler programming in user space

By : kumar
Source: Stackoverflow.com
Question!

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();
}
By : kumar


Answers

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.

By : t0mm13b


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



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.



This video can help you solving your question :)
By: admin