简体   繁体   English

什么是内存映射io和基于端口的io

[英]What exactly is memory mapped io and port based io

Well I have some confusions about memory mapped io and port based io.好吧,我对内存映射 io 和基于端口的 io 有一些困惑。 Questions are:-问题是:-

  1. In port based io if I write to a port will it affect the corresponding memory address or not and vise versa.在基于端口的 io 中,如果我写入一个端口,它是否会影响相应的内存地址,反之亦然。
  2. Does x86 uses port based io or memory mapped io.(I think it is motherboard dependent and x86 can use both, correct me if I am wrong). x86 使用基于端口的 io 还是内存映射的 io。(我认为这取决于主板,x86 可以同时使用两者,如果我错了请纠正我)。
  3. In memory mapped io there are some problems related to cache, what are those problems?在 memory mapped io 中存在一些与缓存相关的问题,这些问题是什么?
  4. Can we use both types of io on a device?我们可以在设备上同时使用这两种类型的 io 吗?
  5. How is DMA used in context with hdd? DMA 如何与硬盘一起使用?

Thanks in advance.提前致谢。

IO space ( in / out ) is a separate address-space from physical memory, including in modern PCI / PCIe devices. IO 空间( in / out )是一个独立于物理内存的地址空间,包括在现代 PCI/PCIe 设备中。 It depends on the device how you need to talk to it;这取决于您需要如何与之交谈的设备; modern devices usually only (or mostly) have MMIO registers (in physical address space), because MMIO is more efficient than port IO.现代设备通常只有(或大部分)有 MMIO 寄存器(在物理地址空间中),因为 MMIO 比端口 IO 更有效。

Parts of physical address space containing MMIO registers should be set as uncacheable in your MTRR or PAT;包含 MMIO 寄存器的部分物理地址空间应在您的 MTRR 或 PAT 中设置为不可缓存; apparently CPUs don't support cacheable memory types at all for MMIO, and can lock up if you try.显然,CPU 根本不支持 MMIO 的可缓存内存类型,如果您尝试,可能会锁定。 (You can use WC write-combining memory for stuff like VGA RAM, ie device memory, allowing write combining to produce 64-byte store transactions. https://sites.utexas.edu/jdm4372/2013/05/29/notes-on-cached-access-to-memory-mapped-io-regions/ ) (您可以将 WC 写入组合内存用于 VGA RAM 之类的东西,即设备内存,允许写入组合产生 64 字节存储事务。https://sites.utexas.edu/jdm4372/2013/05/29/notes- on-cached-access-to-memory-mapped-io-regions/ )

How is DMA used in context with hdd? DMA 如何与 HDD 结合使用?

See https://wiki.osdev.org/AHCI for how to tell the controller (with MMIO accesses) you want it to DMA data to or from physical RAM.请参阅https://wiki.osdev.org/AHCI ,了解如何告诉控制器(具有 MMIO 访问权限)您希望它通过 DMA 将数据传入或传出物理 RAM。

In port based io if I write to a port will it affect the corresponding memory address or not and vise versa.在基于端口的 io 中,如果我写入一个端口,它是否会影响相应的内存地址,反之亦然。

No. A port is accessed with an in / out instruction, it doesn't have a "memory address" that the CPU can see.不可以。端口是通过in / out指令访问的,它没有 CPU 可以看到的“内存地址”。

mov dx,03C4h
mov al,02h
out dx,al

mov al, byte ptr [03C4h] ; this isn't going to make AL = 2 necessarily.

To make things even more confusing, a value written to a memory-mapped IO port doesn't always change the value at that address either.更令人困惑的是,写入内存映射 IO 端口的值并不总是会更改该地址的值。 It's very common for certain IO ports to have different behavior on read vs. write.某些 IO 端口在读取和写入时具有不同的行为是很常见的。

For example, many game consoles have a memory-mapped IO port that can be read to tell you which row of pixels on is being drawn right now.例如,许多游戏机都有一个内存映射 IO 端口,可以读取它来告诉您当前正在绘制哪一行像素。 But if you write to that address, it doesn't cause the video chip to start drawing at that line immediately.但是,如果您写入该地址,它不会导致视频芯片立即开始绘制该行。 Rather, it tells it "I want an interrupt to occur at the Xth row of pixels" where X is the value written to the port.相反,它告诉它“我希望在第 X 行像素处发生中断”,其中 X 是写入端口的值。 In other words, reading this address and writing to it represent two totally different things .也就是说,读这个地址和写这个地址代表着完全不同的两件事 C compilers don't really understand this as a concept and will try to optimize out behavior where you do something that would otherwise be pointless, like read from a memory address you just wrote to. C 编译器并不真正将此理解为一个概念,并且会尝试优化您执行某些本来毫无意义的行为,例如从您刚刚写入的内存地址中读取。 Which is pointless for normal memory, but not for memory-mapped IO.这对于普通内存毫无意义,但对于内存映射 IO 则不然。 The volatile keyword when applied to a variable prevents C from optimizing out reads or writes to it.应用于变量的volatile关键字可防止 C 优化读取或写入变量。 This might relate to your question about caching, but I'm not sure exactly.这可能与您关于缓存的问题有关,但我不确定。

In order to know what will or won't happen, you need to be familiar with the platform you're working on.为了知道会发生什么或不会发生什么,您需要熟悉您正在使用的平台。 Most of them are well-documented, but every once in a while you'll have some ambiguous edge cases where you're not sure what will happen.他们中的大多数都有详细的记录,但每隔一段时间你就会遇到一些模棱两可的边缘情况,你不确定会发生什么。

In memory mapped io there are some problems related to cache, what are those problems?在 memory mapped io 中存在一些与缓存相关的问题,这些问题是什么?

That I'm not entirely sure.我不完全确定。 I think there can be a problem where you try to read from one of these ports and since it's cached you're reading a previous value you didn't intend.我认为当您尝试从这些端口之一读取时可能会出现问题,因为它已被缓存,您正在读取您不想要的先前值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM