1
votes

How to use the kernel directly modify the data in a memory block address

I want to modify(read and write) the data in a user process virtual address space in kernel space, now i have convert the process virtual address converted to physical address, but how can i modify the data in kernel space. linux-3.14 arm64

in the old version(x86_64) i have used mmap to map / dev / mem, but it's doesn't work in arm64

#ifdef MMAP_INVALID_ARGUMENT /*    invalid argument    when mmap   */
    mapStart = (void volatile *)mmap(0, PAGE_SIZE, PROT_READ | PROT_WRITE,     MAP_SHARED | MAP_LOCKED, memfd, 0xF000);

#else
    mapStart = (void volatile *)mmap(0, PAGE_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_LOCKED, memfd, pa_base);

#endif

I find that if the pa_bae is large than 1M, i get invalid argument

1
There is no need to use physical addresses, why you can't just use copy_to_user API?Alex Hoppus
If you are not in current context kmap the needed page and go aheadAlex Hoppus
what I want to do is modify the data in the virtual address of the process like gdb do.gatieme

1 Answers

1
votes

Since you want to modify the data in userspace address inside the kernel, for that you will need kernel API get_user_pages(). Using get_user_pages() you can read or modify the userspace data in kernel. Here is the description-

get_user_pages():

Returns number of pages pinned. This may be fewer than the number requested. If nr_pages is 0 or negative, returns 0. If no pages were pinned, returns -errno. Each page returned must be released with a put_page call when it is finished with. vmas will only remain valid while mmap_sem is held. Must be called with mmap_sem held for read or write. get_user_pages walks a process's page tables and takes a reference to each struct page that each user address corresponds to at a given instant. That is, it takes the page that would be accessed if a user thread accesses the given user virtual address at that instant. This does not guarantee that the page exists in the user mappings when get_user_pages returns, and there may even be a completely different page there in some cases (eg. if mmapped pagecache has been invalidated and subsequently re faulted). However it does guarantee that the page won't be freed completely. And mostly callers simply care that the page contains data that was valid at some point in time. Typically, an IO or similar operation cannot guarantee anything stronger anyway because locks can't be held over the syscall boundary.

get_user_pages() is typically used for fewer-copy IO operations, to get a handle on the memory by some means other than accesses via the user virtual addresses.

You can refer following links: LWN LDD3