gpt4 book ai didi

linux - 如何访问内核空间的物理地址

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:45:31 24 4
gpt4 key购买 nike

如何使用内核直接修改某个内存块地址中的数据

我想在内核空间中修改(读写)用户进程虚拟地址空间中的数据,现在我已经将进程虚拟地址转换为物理地址,但是如何修改内核空间中的数据。
linux-3.14 arm64

在旧版本(x86_64)中,我使用 mmap 映射/dev/mem,但它在 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

我发现如果 pa_bae 大于 1M,我会得到 invalid argument

最佳答案

因为你想在内核中修改用户空间地址中的数据,为此你需要内核 API get_user_pages()。使用 get_user_pages() 您可以读取或修改内核中的用户空间数据。这是描述-

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.

您可以引用以下链接: LWN LDD3

关于linux - 如何访问内核空间的物理地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37639090/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com