0

I would like to copy the variable "register_val" (unsigned int) to an address "addr_user" (unsigned int *) from the user space in the kernel module.

Why does the following code to copy not work?

*addr_user = register_val;
  • Where is that code? In the kernel module or userspace application? – Simon Doppler Jun 21 '21 at 12:48
  • its in the kernel module – zikerun42 Jun 21 '21 at 12:50
  • If `addr_user` is a variable in the kernel code that is pointing to a location in user (virtual address) space for the current process, you can use the `copy_to_user` macro from ``, which will return 0 if the copy was successful, or the number of uncopied bytes if the copy was not successful, e.g. `if (copy_to_user(addr_user, &register_val, sizeof(register_val)) != 0) {` `err = -EFAULT;` `goto out;` `}`. – Ian Abbott Jun 21 '21 at 12:51
  • You cannot copy data from userspace like this, you need to use the `put_user`/`copy_to_user` functions. I would suggest you get familiar with virtual memory first (see [this article](https://developer.ibm.com/technologies/linux/articles/l-kernel-memory-access/) for example). – Simon Doppler Jun 21 '21 at 12:54
  • Thank you very much understand. If I wanted to copy from userspace to the kernel module, I would have to use the get_user function, right? – zikerun42 Jun 21 '21 at 13:08
  • Yes, either `get_user` or `copy_from_user` to copy from userspace. (Note that in `copy_from_user` and `copy_to_user`, the first argument is the destination address, the second is the source address, and the third is the length, like `memcpy`.) – Ian Abbott Jun 21 '21 at 15:16

0 Answers0