CPU如何为硬盘生成逻辑地址

huangapple go评论48阅读模式
英文:

How does Cpu generate the logical address for the disk

问题

CPU生成逻辑地址,然后将其转换为物理地址,但问题在于CPU如何生成存储在磁盘上的数据的逻辑地址。

英文:

If you think question is not proper please edit or make it correct, i am asking what i google and extract from the internet.

Cpu generates the logical address which is converted into physical address but the question here is how does the cpu generates the logical address for the data that is stored on the disk.

答案1

得分: 1

CPU生成逻辑地址,然后将其转换为物理地址,但这里的问题是CPU如何为存储在磁盘上的数据生成逻辑地址。

它并不是以你想象的方式生成逻辑地址。

通常,程序尝试访问虚拟地址的内存,但CPU看到“虚拟地址不存在”并通过页错误向操作系统(内核)报告。页错误处理程序会找出出了什么问题,将数据从磁盘加载到RAM,将RAM映射到虚拟地址空间,然后让程序继续/重试,就好像什么都没有发生过。第二次CPU尝试执行代码时,数据在RAM中,所以它能够正常运行。

当然,操作系统必须知道虚拟地址上的数据不存在的原因,这意味着操作系统必须跟踪CPU没有的额外信息 - 虚拟地址是否根本无效(例如NULL),或者数据是否在交换空间中(以及在哪里),或者数据是否是内存映射文件的一部分(以及哪个文件的哪个偏移量)。

英文:

> Cpu generates the logical address which is converted into physical address but the question here is how does the cpu generates the logical address for the data that is stored on the disk.

It doesn't, at least not the way you're thinking it does.

Normally a program tries to access memory at a virtual address, but the CPU sees "virtual address isn't present" and complains to the OS (kernel) via. a page fault. The page fault handler figures out what went wrong, loads the data from disk into RAM, maps the RAM into the virtual address space, then lets the program continue/retry as if nothing happened. The second time the CPU tries to execute the code the data is in RAM so it works fine.

Of course the OS has to know the reason why data at a virtual address wasn't present, which means that the OS has to keep track of extra information that the CPU doesn't have - if the virtual address actually isn't valid at all (e.g. NULL), or if the data is in swap space (and where), or if the data is part of a memory mapped file (and which offset of which file).

答案2

得分: 0

虚拟地址空间和物理地址空间。虚拟地址空间定义了程序的地址空间。假设有一个4GB的程序。在这种情况下,我们可以将该程序的地址空间表示为32位(2^32 = 4GB),从0到0xFFFFFFFF。
这是程序认为自己拥有的空间。

在编译程序时,根据程序的地址空间,会给程序分配逻辑地址。

在加载到内存后,分配给该程序的程序计数器将指向这些地址(逻辑/虚拟地址),CPU只想获取这些地址,其中包含程序指令。CPU不知道指令在内存中的位置,这由内存管理单元(MMU)负责翻译地址。

主要的问题是CPU实际上并不生成这些地址,而是在编译时为程序分配的地址,程序中的指令使用这些地址相互引用。所以CPU只看程序计数器指向的位置并生成/请求这些指令,这些指令位于物理内存中。

每当需要获取数据或操作数的地址时,由PC指向的指令,CPU会请求这些地址。

英文:

There is Virtual address space and a physical address space. virtual address space defines the address space of the program. say there is a program of 4GB. in that case we can represent the address space for that program as 32 bits. (2^32 = 4GB) from 0 to 0xFFFFFFFF.
this is the space the program thinks it has.

while compilation of the program, the program is given logical addresses based on the address space of the program.

after loading in to the memory. the program counter that is assign to this program will point to these addresses (logical/virtual addresses) and cpu will only want to fetch these addresses where the program instruction's are. cpu doesn't know where are the instructions located in the memory. that is up to MMU to translate the addresses.

the main thing is CPU doesn't actually generate these addresses, these are the addresses that where given to the program while compilation, using these, the instructions in the program reference each other. so cpu just see what program counter is pointing and generate / asks for these instruction. which are located in the physical memory.

when ever a address for fetching data or operand , instruction pointed by the PC, cpu call for these addresses.

答案3

得分: 0

磁盘不使用虚拟地址,而是直接使用物理地址读取/写入RAM中的数据。磁盘传输是通过写入磁盘控制器的内存映射寄存器(例如AHCI,NVME或其他)来启动的。

控制器应该使用的每个内存映射寄存器和内存都需要通过更改页表中的一些标志来取消缓存。这是因为如果数据被缓存,那么很难保持实际写入的一致性。CPU可能会在实际写入RAM之前缓存数据,这会阻止控制器的正常操作。

页表通常被编写以访问与控制器相关的物理内存,如PCI-e配置空间(寄存器)和其他正在使用的内存。只要访问不被缓存,操作系统可以决定将任何虚拟地址与控制器正在使用的物理内存相关联。

分页机制使其看起来有一块连续的内存,这块内存比可用的物理内存要大,但底层机制并不那么简单。操作系统负责通过在单个页面上设置存在标志,并使用页面故障异常机制来实现这一外观,以便在页面被交换到硬盘时得到通知。例如,Linux在页表条目本身保存了一个指向结构的指针,该结构提供了定位在硬盘上交换的页面的信息。

一旦发生页面故障,操作系统通过查看进程的PCB来确定虚拟地址空间中是否已分配了该内存区域。如果是这种情况,那么必须意味着页面已经被交换到硬盘上,因此可以查看存储在页表相应条目中的指针,以找到页面。然后必须找到一个当前不太可能被使用的页面来交换该页面,并将进程要访问的页面带回。

英文:

The disk doesn't work with virtual addresses. It works with physical addresses directly reading/writing data in RAM. Disk transfers are started by writing memory mapped registers of the disk controller such as AHCI (https://www.intel.com/content/www/us/en/io/serial-ata/ahci.html), NVME or others.

Every memory mapped register and memory that's supposed to be used by the controller need to be uncached by changing some flags in the page tables. This is necessary because, if the data is cached, then it is hard to keep consistency with what is actually written. The CPU might cache the data before actually writing it in RAM which prevent proper operations of the controller.

The page tables are simply written to access the physical memory related to the controller such as the PCI-e configuration space (registers) and other memory in use. The OS can decide to relate any virtual address to the physical memory in use by the controller as long as the accesses are not cached.

The paging mechanism does make it look like there is contiguous memory which is bigger than available physical memory but the underlying mechanism isn't that simple. The OS is responsible to give that appearance by setting the present flag on individual pages and using the page fault exception mechanism to be notified in case a page was swapped to the hard-disk. For example, Linux saves a pointer to a structure in the page table entry itself which gives information to locate the swapped pages on the hard-disk (https://www.kernel.org/doc/gorman/html/understand/understand014.html).

Once a page fault occurs, the OS determines if the process has that memory region of the virtual address space allocated by looking in its PCB. If it is the case, then it must mean that the page was swapped on the hard-disk so it can look at the pointer stored in that corresponding entry of the page table to find the page. It must then find a page which is less likely to be used right now to swap that page and bring back the one that the process wants to access.

huangapple
  • 本文由 发表于 2023年2月19日 17:20:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/75499107.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定