Linux内核在直接映射(线性映射)区域中映射尽可能多的RAM的合理性是什么?

时间:2022-04-06 23:46:59

The discussion below applies to 32-bit ARM Linux.

以下讨论适用于32位ARM Linux。

  1. Suppose there are 512MB physical RAM in my system. For common configurations, all these 512MB physical RAM will be mapped via direct mapping by kernel(0xC000 0000 to 0xE000 0000). Question is: kernel itself only uses part of these RAM; most of these RAM would be allocated to user space. Why bother mapping all these 512MB physical RAM in kernel's virtual space(0xC000 0000 to 0xE000 0000)? Why doesn't kernel just map part of these RAM for its only usage(say 64MB RAM)?

    假设我的系统中有512MB物理RAM。对于常见配置,所有这些512MB物理RAM将通过内核的直接映射(0xC000 0000到0xE000 0000)进行映射。问题是:内核本身只使用这些RAM的一部分;大多数这些RAM都将分配给用户空间。为什么要在内核的虚拟空间(0xC000 0000到0xE000 0000)中映射所有这些512MB物理RAM呢?为什么内核只将这些RAM的一部分用于其唯一用途(比如64MB RAM)?

  2. If physical RAM is greater than 1GB, things get a little complicated. Let's say directly-mapped area is 768MB in size. The result would be 768MB out of 1GB being directly mapped to kernel's virtual space. I guess the rest of the RAM(256MB) goes to two places: either high memory area or allocated by kernel to user space. But I still don't see any advantage of mapping so many physical RAM into kernel's virtual space.

    如果物理RAM大于1GB,事情会变得有点复杂。假设直接映射区域的大小为768MB。结果将是1GB中的768MB被直接映射到内核的虚拟空间。我猜其余的RAM(256MB)分为两个地方:高内存区域或内核分配给用户空间。但我仍然没有看到将如此多的物理RAM映射到内核虚拟空间的任何优势。

1 个解决方案

#1


3  

Actually this question can be reduced to: what are the drawbacks if kernel only directly maps a small part of physical RAM(say 64MB out of 512MB)?

实际上这个问题可以简化为:如果内核只直接映射物理RAM的一小部分(比如说512MB中的64MB)会有什么缺点?

Before further discussion, it is beneficial to know that

在进一步讨论之前,了解这一点是有益的

  • After MMU is turned on, every address issued by CPU is virtual address.
  • MMU打开后,CPU发出的每个地址都是虚拟地址。
  • If kernel wants to access ANY address in RAM, a mapping must be set up before the actual access happens.
  • 如果内核想要访问RAM中的任何地址,则必须在实际访问发生之前设置映射。

If kernel only directly maps a small part of physical RAM, the cost is that every time kernel needs to access other parts of RAM, it needs to set up a temporary mapping before accessing that address and torn down that mapping after the access, which is very tedious and low efficiency.

如果内核只直接映射物理RAM的一小部分,那么成本是每次内核需要访问RAM的其他部分时,它需要在访问该地址之前设置临时映射并在访问后拆除该映射,这是非常乏味和低效率。

If that mapping is set up in advance and is always there, it saves quite a lot of trouble for kernel.

如果该映射是预先设置的并且始终存在,那么它为内核节省了很多麻烦。

#1


3  

Actually this question can be reduced to: what are the drawbacks if kernel only directly maps a small part of physical RAM(say 64MB out of 512MB)?

实际上这个问题可以简化为:如果内核只直接映射物理RAM的一小部分(比如说512MB中的64MB)会有什么缺点?

Before further discussion, it is beneficial to know that

在进一步讨论之前,了解这一点是有益的

  • After MMU is turned on, every address issued by CPU is virtual address.
  • MMU打开后,CPU发出的每个地址都是虚拟地址。
  • If kernel wants to access ANY address in RAM, a mapping must be set up before the actual access happens.
  • 如果内核想要访问RAM中的任何地址,则必须在实际访问发生之前设置映射。

If kernel only directly maps a small part of physical RAM, the cost is that every time kernel needs to access other parts of RAM, it needs to set up a temporary mapping before accessing that address and torn down that mapping after the access, which is very tedious and low efficiency.

如果内核只直接映射物理RAM的一小部分,那么成本是每次内核需要访问RAM的其他部分时,它需要在访问该地址之前设置临时映射并在访问后拆除该映射,这是非常乏味和低效率。

If that mapping is set up in advance and is always there, it saves quite a lot of trouble for kernel.

如果该映射是预先设置的并且始终存在,那么它为内核节省了很多麻烦。