星期三, 十月 31, 2007

Linux 内存寻址 1GB(896MB) 限制问题

《Understanding the LINUX KERNEL》(《深入理解LINUX内核》)一书,P51 提到:
In theory, up to 4GB of RAM could be installed on such systems; in practice, due to the linear address space requirements of User Mode processes, the kernel cannot directly address more than 1GB of RAM, as we will see in the later secion "Paging in Linux."


然后,在 P70 中(Paging in Linux -> Kernel Page Tables)中首先说明的是"Final kernel Page Table when RAM size is less than 896MB"。这个 896MB 根据后面的说明,是 1GB - 128MB = 896MB,128MB 有别的用处。但这个 1GB 是怎么来的呢?

参考 P47 关于 "Regular Paging" 的说明,可以看到,实际上每个进程(Process)都有它自己的 Linear Address 空间,默认会分配一个 Page Directory 和 Page Table(因此最小内存占用量为 8KB),而在 "Paging in Linux" 中讲到,系统被分为 User Mode 和 Kernel Mode,如果将 kernel 看作一个进程(init),那么系统最多为它预留的内存只能是 1GB,否则它将不得不使用 User Mode 的内存作为内核之用。因为 User Mode 无法获得直接获得 Kernel Mode 的内容,而 Kernel Mode 则可以从 User Mode 中存取数据,所以以最通常的情况 4GB 最大寻址空间为例,则 3GB 作为 User Mode 使用,1GB 给 Kernel Mode 使用,而 Kernel Mode 的线性地址空间为:0xc0000000 到 0xffffffff。

这个 4GB 的最大寻址空间是指 32 位使用最简单的线性地址表示方法时可以映射的最大寻址空间,也就是只有一个 Page Directory 和 1024 个 Page Table 的情况,此时是一个两级的目录状态。为了支持 Intel 处理器的 PAE(Physical Address Extension)功能以支持超过 4GB 的物理内存,Linux 可以在 PD(称为 Page Gloabl Directory, PGD) 和 PT 之间增加 Page Upper Directory 和 Page Middle Directory,显然这增加了目录级数,也就意味着增加了寻址时间。所以如果不使用那么大的内存,Linux 会将 PUD 和 PMD 的长度设定为 0 并直接映射为 PGD 的一个条目。

但这个 Kernel Mode 和 Low Memory/High Memory 有联系吗?

没有评论: