内核虚拟地址和内核逻辑地址之间的区别?
我无法准确区分内核逻辑地址和虚拟地址。 Linux设备驱动书中说所有的逻辑地址都是内核虚拟地址,而虚拟地址没有任何线性映射。但是从逻辑上讲,当我们说它是逻辑的时,当我们说虚拟时,我们在什么情况下使用这两个?
I am not able to exactly difference between kernel logical address and virtual address. In Linux device driver book it says that all logical address are kernel virtual address, and virtual address doesn't have any linear mapping. But logically wise when we say it is logical and when we say virtual and in which situation we use these two ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
Linux内核将属于内核的大部分虚拟地址空间与物理内存第一部分的偏移量进行1:1映射。 (略小于 32 位 x86 的 1Gb,对于其他处理器或配置可能有所不同)。例如,对于 x86 地址 0xc00000001 上的内核代码,映射到物理地址 0x1。
这称为逻辑映射 - 1:1 映射(带有偏移量),允许内核访问机器的大部分物理内存。
但这还不够 - 有时我们在 32 位机器上拥有超过 1Gb 的物理内存,有时我们希望将非连续物理内存块引用为连续以使事情变得简单,有时我们希望映射不是 RAM 的内存映射 IO 区域。
为此,内核在其虚拟地址空间的顶部保留一个区域,在其中进行“随机”页到页映射。那里的映射不遵循逻辑映射区域的 1:1 模式。这就是我们所说的虚拟映射。
需要补充的是,在许多平台上(x86 就是一个例子),逻辑映射和虚拟映射都是使用相同的硬件机制(TLB 控制虚拟内存)完成的。在许多情况下,“逻辑映射”实际上是使用处理器的虚拟内存设施完成的,因此这可能有点令人困惑。因此,区别在于完成映射的模式:逻辑为 1:1,虚拟为随机。
The Linux kernel maps most of the virtual address space that belongs to the kernel to perform 1:1 mapping with an offset of the first part of physical memory. (slightly less then for 1Gb for 32bit x86, can be different for other processors or configurations). For example, for kernel code on x86 address 0xc00000001 is mapped to physical address 0x1.
This is called logical mapping - a 1:1 mapping (with an offset) that allows the kernel to access most of the physical memory of the machine.
But this is not enough - sometime we have more then 1Gb physical memory on a 32bit machine, sometime we want to reference non contiguous physical memory blocks as contiguous to make thing simple, sometime we want to map memory mapped IO regions which are not RAM.
For this, the kernel keeps a region at the top of its virtual address space where it does a "random" page to page mapping. The mapping there do not follow the 1:1 pattern of the logical mapping area. This is what we call the virtual mapping.
It is important to add that on many platforms (x86 is an example), both the logical and virtual mapping are done using the same hardware mechanism (TLB controlling virtual memory). In many cases, the "logical mapping" is actually done using virtual memory facility of the processor, so this can be a little confusing. The difference therefore is the pattern according to which the mapping is done: 1:1 for logical, something random for virtual.
基本上有3种寻址,即
现在,在linux中,内核内存(在地址空间中)超过3GB(3GB到4GB),即0xc000000..内核使用的地址不是物理地址。为了映射虚拟地址,它使用 PAGE_OFFSET。必须注意不要涉及页面翻译。即这些地址本质上是连续的。但对此有一个限制,即 x86 上为 896 MB。除此之外,还使用分页进行翻译。当您使用vmalloc时,将返回这些地址以访问分配的内存。
简而言之,当有人在用户空间上下文中引用虚拟内存时,就是通过分页。如果提到内核虚拟内存,那么它是 PAGE_OFFSETed 或 vmalloced 地址。
(参考 - Understanding Linux Kernel - 2.6 based )
Shash
Basically there are 3 kinds of addressing, namely
Now, in linux, Kernel memory (in address space) is beyond 3 GB ( 3GB to 4GB), i.e. 0xc000000..The addresses used by Kernel are not Physical addresses. To map the virtual address it uses PAGE_OFFSET. Care must be taken that no page translation is involved. i.e. these addresses are contiguous in nature. However there is a limit to this, i.e. 896 MB on x86. Beyond which paging is used for translation. When you use vmalloc, these addresses are returned to access the allocated memory.
In short, when someone refers to Virtual Memory in context of User Space, then it is through Paging. If Kernel Virtual Memory is mentioned then it is either PAGE_OFFSETed or vmalloced address.
(Reference - Understanding Linux Kernel - 2.6 based )
Shash
内核逻辑地址是内核代码可以通过正常的 CPU 内存访问函数访问的映射。在 32 位系统上,仅存在 4GB 的内核逻辑地址空间,即使使用的物理内存多于此。由物理内存支持的逻辑地址空间可以使用
kmalloc
进行分配。虚拟地址不一定有对应的逻辑地址。您可以使用 vmalloc 分配物理内存并取回没有相应逻辑地址的虚拟地址(例如,在具有 PAE 的 32 位系统上)。然后,您可以使用
kmap
为该虚拟地址分配逻辑地址。Kernel logical addresses are mappings accessible to kernel code through normal CPU memory access functions. On 32-bit systems, only 4GB of kernel logical address space exists, even if more physical memory than that is in use. Logical address space backed by physical memory can be allocated with
kmalloc
.Virtual addresses do not necessarily have corresponding logical addresses. You can allocate physical memory with
vmalloc
and get back a virtual address that has no corresponding logical address (on 32-bit systems with PAE, for example). You can then usekmap
to assign a logical address to that virtual address.简单来说,虚拟地址会包含“高端内存”,它不会与物理地址进行1:1的映射,如果你的RAM大小超过了内核的地址范围(通常,对于X86中的1G/3G,你的RAM是3G但你的内核寻址范围是1G)以及kmap()和vmalloc()返回的地址,这需要内核为内存映射建立页表。由于逻辑地址始终是由内核映射的内存(1:1映射),因此您不需要显式调用内核API,例如set_pte来设置特定页面的页表条目。
所以虚拟地址不可能一直是逻辑地址。
Simply speaking, virtual address would include "high memory", which doesn't do the 1:1 mapping for the physical address,if your RAM size is more than the address range of kernel(typically,For 1G/3G in X86,your RAM is 3G but your kernel addressing range is 1G) and also the address return from kmap() and vmalloc(), which requires the kernel to establish page table for the memory mapping. since logic address is always memory mapped by the kernel(1:1 mapping), you don't need to explicitly call kernel API,like set_pte to set up the page table entry for the particular page.
so virtual address can't be logic address all the time.