Search code examples
clinux-kernelx86kernel-modulecpu-registers

Get userspace RBP register from kernel syscall


I am writing a kernel system call and I want to read the base pointer register (RBP) of the user. Maybe I can do that using the pt_regs struct that is passed for parameter, isn't it?

Example code:

unsigned long int data;
asmlinkage int my_read(int d)
{
    get_rbp_of_userStack(&data);#or somthing like that 

}

I know this data saved somewhere for the context switch, how can I get to it?

this is my user code

 void rar()
{//rbp here should be rsp when it call so it basically the return addres of the main
  char t[10];
getchar();
 }
 
int main()
{
  rar();
}

Solution

  • You can use the task_pt_regs() macro to get the current task's user registers (saved at the moment of syscall entry):

    #include <asm/processor.h>
    
    SYSCALL_DEFINE1(foo, int, d)
    {
        const struct pt_regs *user_regs = task_pt_regs(current);
        unsigned long rbp = user_regs->bp;
    
        /* Do whatever you need... */
    
        return 0;
    }