为什么内核需要虚拟寻址? [英] why kernel needs virtual addressing?

查看:93
本文介绍了为什么内核需要虚拟寻址?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Linux中,每个进程都有其虚拟地址空间(例如,对于32位系统,为4 GB,其中为进程保留3GB,为内核保留1GB).这种虚拟寻址机制有助于隔离每个进程的地址空间.在过程的情况下这是可以理解的,因为存在许多过程.但是既然我们只有1个内核,那为什么我们需要对内核进行虚拟寻址呢?

In Linux each process has its virtual address space (e.g. 4 GB in case of 32 bit system, wherein 3GB is reserved for process and 1 GB for kernel). This virtual addressing mechanism helps isolating the address space of each process. This is understandable in case of process since there are many processes. But since we have 1 kernel only so why do we need virtual addressing for kernel?

推荐答案

内核虚拟"的原因不是处理分页,这是因为处理器一次只能运行在一种模式下.因此,一旦打开页面内存映射(x86上CR0中的位31),处理器就会期望所有内存访问都将通过页面映射机制进行.因此,由于即使启用了分页(虚拟内存)后,我们也确实想访问内核,所以它需要存在于虚拟空间中的某个位置.

The reason the kernel is "virtual" is not to deal with paging as such, it is becuase the processor can only run in one mode at a time. So once you turn on paged memory mapping (Bit 31 in CR0 on x86), the processor is expecting ALL memory accesses to go through the page-mapping mechanism. So, since we do want to access the kernel even after we have enabled paging (virtual memory), it needs to exist somewhere in the virtual space.

保留"内存更多的是确定地址是内核空间还是用户空间的简便方法".完全有可能在地址12345-34121处放一些内核,在101900-102400处放一些内核,在40000000-40001000处放一些内核.但是,这将使内核和用户空间的各个方面的工作变得很困难-将会有很多空白/漏洞需要解决[已经有这样的漏洞/漏洞,但是拥有更多的漏洞/漏洞并不能完全帮助您].通过为用户空间从这里到这里,内核从用户空间的末端到X"设置一个固定的限制,在这方面,它使工作变得更加轻松.我们只能在某些代码中说kernel = 0; if (address > max_userspace) kernel=1;.

The "reserving" of memory is more about "easy way to determine if an address is kernel or user-space" than anything else. It would be perfectly possible to put a little bit of kernel at address 12345-34121, another bit of kernel at 101900-102400 and some other bit of kernel at 40000000-40001000. But it would make life difficult for every aspect of the kernel and userspace - there would be gaps/holes to deal with [there already are such holes/gapes, but having more wouldn't exactly help things]. By setting a fixed limit for "userspace is from here to here, kernel is from end of userspace to X", it makes life much easier in that respect. We can just say kernel = 0; if (address > max_userspace) kernel=1; in some code.

当然,kerneln只占用实际使用的物理内存-因此,通常的想法是浪费整个千兆字节的内核是浪费的"是错误的-内核本身是几个(一个非常大"的内核需要12个左右)兆字节.加载的模块可以轻松地增加多达几兆字节,而ATI和nVidia的图形驱动程序很容易再增加几兆字节,仅用于其本身的内核模块.内核还使用一些内存来存储内核数据",例如任务,队列,信号量,文件和内核必须处理的其他东西".也为此使用了几兆字节.

Of course, the kerneln only takes up as much PHYSICAL memory as it will actually use - so the common thinking that "it's a waste to take up a whole gigabyte for the kernel" is wrong - the kernel itself is a few (a dozen or so for a very "big" kernel) megabytes. The modules loaded can easily add up to several more megabytes, and graphics drivers from ATI and nVidia easily another few megabytes just for the kernel moduel for that itself. The kernel also uses some bits of memory to store "kernel data", such as tasks, queues, semaphores, files and other "stuff" the kernel has to deal with. A few megabytes is used for this as well.

这篇关于为什么内核需要虚拟寻址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆