gpt4 book ai didi

android - 物理地址、设备地址和虚拟地址的区别

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

设备地址物理地址虚拟地址有什么区别?

实际上我正在尝试在驱动程序中使用 mmap,我坚持这个概念。

最佳答案

documentation说:

The kernel normally uses virtual addresses. Any address returned bykmalloc(), vmalloc(), and similar interfaces is a virtual address and canbe stored in a "void *".

The virtual memory system (TLB, page tables, etc.) translates virtualaddresses to CPU physical addresses, which are stored as "phys_addr_t" or"resource_size_t". The kernel manages device resources like registers asphysical addresses. These are the addresses in /proc/iomem. The physicaladdress is not directly useful to a driver; it must use ioremap() to mapthe space and produce a virtual address.

I/O devices use a third kind of address: a "bus address" or "DMA address".If a device has registers at an MMIO address, or if it performs DMA to reador write system memory, the addresses used by the device are bus addresses.In some systems, bus addresses are identical to CPU physical addresses, butin general they are not. IOMMUs and host bridges can produce arbitrarymappings between physical and bus addresses.

Here's a picture and some examples:

             CPU                  CPU                  Bus
Virtual Physical Address
Address Address Space
Space Space

+-------+ +------+ +------+
| | |MMIO | Offset | |
| | Virtual |Space | applied | |
C +-------+ --------> B +------+ ----------> +------+ A
| | mapping | | by host | |
+-----+ | | | | bridge | | +--------+
| | | | +------+ | | | |
| CPU | | | | RAM | | | | Device |
| | | | | | | | | |
+-----+ +-------+ +------+ +------+ +--------+
| | Virtual |Buffer| Mapping | |
X +-------+ --------> Y +------+ <---------- +------+ Z
| | mapping | RAM | by IOMMU
| | | |
| | | |
+-------+ +------+

During the enumeration process, the kernel learns about I/O devices andtheir MMIO space and the host bridges that connect them to the system. Forexample, if a PCI device has a BAR, the kernel reads the bus address (A)from the BAR and converts it to a CPU physical address (B). The address Bis stored in a struct resource and usually exposed via /proc/iomem. When adriver claims a device, it typically uses ioremap() to map physical addressB at a virtual address (C). It can then use, e.g., ioread32(C), to accessthe device registers at bus address A.

If the device supports DMA, the driver sets up a buffer using kmalloc() ora similar interface, which returns a virtual address (X). The virtualmemory system maps X to a physical address (Y) in system RAM. The drivercan use virtual address X to access the buffer, but the device itselfcannot because DMA doesn't go through the CPU virtual memory system.

In some simple systems, the device can do DMA directly to physical addressY. But in many others, there is IOMMU hardware that translates busaddresses to physical addresses, e.g., it translates Z to Y. This is partof the reason for the DMA API: the driver can give a virtual address X toan interface like dma_map_single(), which sets up any required IOMMUmapping and returns the bus address Z. The driver then tells the device todo DMA to Z, and the IOMMU maps it to the buffer at address Y in systemRAM.

关于android - 物理地址、设备地址和虚拟地址的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24903841/

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