Linux Execution and Virtual Memory Utilization
Linux执行以及虚拟内存之用
When Linux boots, it starts with the MMU disabled, so initially it deals only with physical
memory. The kernel image is copied to physical address 0x8000 in DRAM and executed. First a master page table is created and placed just before the kernel, which describes all available DRAM. The MMU is then switched on, mapping all of DRAM to virtual address 0xC0000000. The kernel reserves 64K of virtual memory for the interrupt handler code (usually at the very top of virtual memory), and sets up all the mappings for the hardware registers (UART, USB, LCD, GPIO, etc). Once kernel space is established and all drivers are initialized, the linux kernel moves on to establishing user space. This involves reading in the file system and actually executing processes.
当Linux启动时,是以MMU禁用来开启它的旅途的,因此,它起初只关注物理内存。内核映像(kernel image)被拷贝到DRAM的物理地址 0x8000处,并得到执行权。第一个主页表( master page table)从而诞生,并紧挨着内核映像存放,这个页表是对全部可用DRAM的描述。此后,MMU被启用,从而把全部DRAM映射到从0xC0000000开始的虚地址。内核为中断处理程序保留64K虚拟内存(通常在虚拟内存的顶端),然后为所有的硬件寄存器(UART, USB, LCD, GPIO, etc)建立映射。一旦内核空间建立起来,并且所有的驱动程序都被初始化,则Linux内核转向建立用户空间。这涉及到在文件系统中读取并实际执行进程。
Each process that runs on the system does so in its own memory “context”. Each context
has its virtual memory space established and maintained by the kernel using a separate page table. So each process can “see” the entire user space, but its underlying physical memory is different from the other processes. Multiple processes and their contexts are run in time slices. This means that each process executes for a time, then is halted, and execution is passed to the next process in a rotating queue. The act of passing execution is called “context switching” and its frequency varies, depending on the kernel configuration and the current system load.
运行在系统中的每个进程都的确在它自己的内存“上下文”中执行。每个上下文都有自己的虚拟内存空间,这一空间的建立和维护是内核通过独立的页表进行的。因此,每个进程可以“看到”整个用户空间,但是,其对应的物理内存互不重叠。多个进程及其上下文以时间片轮流执行。这就意味着,每个进程执行一段时间,然后停下来,从而把执行权传递给轮流队列中的下一进程。传递执行权的行为就是所谓的“上下文切换”,传递的频率随内核的配置及当前负载而变化。
Each context includes the mappings for both user and kernel space because each new
page table is initialized with the master page table contents. So, code executing in kernel space has full access to the entire memory map for the current process. Switching back and forth between kernel and user space execution does not require a page table swap.
每个上下文既包含用户空间的映射,也包含内核空间的映射,这是因为,每个新的页表都是用主页表内容进行初始化的。因此,对当前进程而言,在内核空间执行的代码具有对整个内存映射访问的权利。在内核空间和用户空间之间来回切换根本无需切换页表。
When a process is executed, the kernel creates a new context, and maps just enough
physical memory to copy the executable image into DRAM (starting at least a page above virtual address 0, because 0 is reserved for faults). The executable is broken into the same sections as the kernel: code and data (uninitialized global data, zero-initialized global data). Once the kernel schedules the process for execution, it sets the stack to grow down from the top of user space, and the heap to grow up from the end of the executable image. This way the chances of the two spaces colliding is minimized. Any dynamic libraries the process pulls in are placed at virtual address 0x40000000.
当一个进程执行时,内核创建新的上下文,并为把可执行映像拷贝到DRAM仅映射足够用的物理内存(从虚地址0之上的某个地址开始一个页,因为0地址留作存放出错信息)。可执行映像如内核一样也被划分为两部分:代码和数据(未初始化数据和初始化为0的全局数据)。一旦内核调度进程执行,它就设置栈和堆,前者从用户空间的顶部向下增长,后者从可执行映像的尾部向上增长。以这种方式,两个空间冲突的机会就大大减少。进程调用的动态库都存放在 0x40000000开始的地址处。
The kernel reserves virtual memory whenever a process or driver requests it, but it doesn’t
actually map in the underlying physical memory until the memory is accessed (copy-on-write). So, the physical DRAM is used only when it absolutely has to be, and the kernel controls the allocation through a global memory management scheme. Ideally, the kernel tries to allocate contiguous blocks of physical memory as contiguous memory is most likely to evenly fill physically tagged caches. Thus, the kernel organizes memory to grow inward from the ends of physical memory, maximizing contiguous memory in the middle. The physical memory map is described in detail in the next section.
只要进程或驱动程序请求内存,内核就留出虚拟内存来,但,并不实际映射到真正的物理内存,直到要访问相应的内存时才进行映射(写时复制)。因此,只有在万不得已时才用到物理DRAM,内核通过全局内存管理模式控制分配。理想的情况下,内核尽量分配连续的物理块,这是因为连续内存很可能均匀地填充物理上紧挨的高速缓存。因此,内核对内存的组织采用从物理内存的末端向内方向增长,以图在中间最大化连续内存。物理内存的映射在下一部分将详细给予描述。
Linux执行以及虚拟内存之用
最新推荐文章于 2024-08-24 15:46:50 发布