将物理地址映射到虚拟地址 linux [英] Mapping physical addresses to virtual address linux

查看:38
本文介绍了将物理地址映射到虚拟地址 linux的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个小型嵌入式系统.当我的 linux 启动到用户空间时,我知道我的设备在物理内存中的位置.我想将它们映射到用户空间虚拟地址.目前,我是通过内核模块来完成的.我使用 vmalloc/kmalloc(取决于大小),然后在返回的虚拟地址上使用 ioremap_page_range 来映射我的物理地址.我不认为这是正确的方法.首先,我正在分配内存,然后我要求内核将该虚拟地址空间重新映射到某个不同的物理地址空间.(最初映射到 vmcall/kmalloc 中的物理->虚拟有点没用,因为我不关心那些物理页面.这绝对不好.)

I am working on a small embedded system. When my linux boots up into user space, I know where are my devices in the physical memory. I want to map them into user space virtual addresses. Currently, I am doing it through a kernel module. I use vmalloc/kmalloc (depending on the size) and then I use ioremap_page_range on that returned virtual addresses to map my physical addresses. I dont think that is the correct way to go about. First of all I am allocating memory and then I am asking kernel to remap that virtual address space to some different physical address space. (Initially mapped physical->virtual in vmcall/kmalloc is kinda useless as I dont care about those physical pages. This is definitely not good.)

除此之外,还有更好的方法将已知物理内存映射到用户空间进程.(我知道除了我的用户空间进程,没有人会触及那个内存.)

Instead of this is there a better way to map the known physical memory into user space process. (I know other than my user space process, no one gonna touch that memory.)

谢谢

推荐答案

您要做的是访问所谓的IO 内存.我只能鼓励您阅读 Linux Device Drivers (LDD) 一书,更具体地说是第 9 章.

What you are trying to do is accessing what is called IO memory. I can only encourage you to read the Linux Device Drivers (LDD) book and more specifically the chapter 9.

要分配"这样的区域,您需要调用

To "allocate" such an area, you need to call

struct resource *request_mem_region(unsigned long start, unsigned long len, char *name)

.在您的驱动程序可以访问它之前,您必须为其分配一个虚拟地址,这是通过调用

. Before your driver can access it, you have to assign it a virtual address, this is done with a call to

void *ioremap(unsigned long phys_addr, unsigned long size)

为确保您的驱动程序能够在不同的架构/平台上运行,请务必对这些区域(ioread8/16/32 或 iowrite8/16/32 及其所有变体)使用一些访问器函数.

To ensure that your driver will then work on different architectures/platforms, be sure to use some accessor function to such areas ( ioread8/16/32 or iowrite8/16/32 and all of their variants).

这篇关于将物理地址映射到虚拟地址 linux的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆