I will allocate memory in user application using malloc
and send the malloc
returned address to a kernel module through character driver interface.
I will pin the pages for this memory using get_user_pages_fast
in kernel module.
Can I use virt_to_phys
to get the address returned by malloc
.
Is it valid? If not then how can I get proper physical address?
My aim is to get physical address of user space allocated memory. I m limiting my transfer size to pagesize (4KB).
Whilst a user-space program is not allowed to access kernel memory, it is possible for the kernel to access user memory. However, the kernel must never execute user-space memory and it must also never access user-space memory without explicit expectation to do so.
The function copy_to_user is used to copy data from the kernel address space to the address space of the user program. For example, to copy a buffer which has been allocated with kmalloc to the buffer provided by the user.
The copy_to_user function copies a block of data from the kernel into user space.
The most common way of implementing a user mode separate from kernel mode involves operating system protection rings. Protection rings, in turn, are implemented using CPU modes. Typically, kernel space programs run in kernel mode, also called supervisor mode; normal applications in user space run in user mode.
No you can't, virt_to_phys converts kernel virtual addresses into physical addresses. There exist 3 (or 4) types of addresses in linux:
Note that page table 'layout' depends on the architecture of the processor, so you need to implement a software page table walk that corresponds to the architecture you work on.
And last word, the 4th kind of addresses that exists is :
malloc
returns the user virtual address. So I think you can not use the address returned by malloc
from inside the driver.
virt_to_phys
: The returned physical address is the physical (CPU) mapping for the memory address given. It is only valid to use this function on addresses directly mapped or allocated via kmalloc. It means It is used by the kernel to translate kernel virtual address (not user virtual address) to physical address
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With