Linux - Mapping user space memory in kernel code

安稳与你 提交于 2020-02-01 06:29:16

问题


i am writing a piece of code that needs to store 10k of memory located in specific physical address before the SOC shuts down.

My problem is that this physical address is not part of kernel space so i have to create an ad -hoc memory mapping so i can access this memory space.

i tried using io-remap but it doesn't (apparently) work on non-kernel space.

is there any API for doing this ? should i used kmap ?

Thanks in advance


回答1:


Sounds like memory mapped peripheral. For tight binding into your kernel, it would have entry added into initdata which goes to iotable_init(). For example arch/arm/mach-vexpress/ct-ca9x4.c ct_ca9x4_io_desc[]. That creates virtual to physical mapping. Then kernel code could use writel with virtual address to write there.




回答2:


Found the answer

the key is to use the vmap function which create a mapping for a given page table. the problem was how to initialize a page table structure to a certain physical address but it appears there exists an API for that as well

here is an example to allocate a single page

void *virt_addr_ptr
struct page **my_page = kmalloc(sizeof (*my_page), GFP_KERNEL);
my_page = phys_to_page(phys_addr_ptr);
virt_addr_ptr = vmap(my_page, 1, VM_MAP, PAGE_KERNEL);

/*now its possible to access this space */
memcpy(store_buffer, virt_addr_ptr, store_size);


来源:https://stackoverflow.com/questions/13586301/linux-mapping-user-space-memory-in-kernel-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!