Linux

来源:互联网 发布:sql拒绝访问 编辑:程序博客网 时间:2024/05/19 22:56

Linux内存

地址类型

用户虚拟地址:用户空间看到的常规地址,通过页表可以将虚拟地址和物理地址映射起来
物理地址:用在CPU和内存之间的地址叫做物理地址
总线地址:外围总线和内存之间的地址叫做总线地址,通常他们和物理地址相同的
内核逻辑地址:内核的常规地址空间,必定有对应的物理内存与之映射的,kmalloc返回的就是内核逻辑地址的
内核虚拟地址:内核虚拟地址和内核逻辑地址的相同之处在于,他们都将内核空间的地址映射到物理内存上,但是内核虚拟地址不一定是线性的,和一对一的,vmalloc返回的就是内核虚拟地址

虚拟内存

虚拟内存是用来描述一种不直接映射计算机物理内存的方法,分页是在虚拟内存与物理内存转换时候用到的额,
低于896MB的每页内存都会被映射到内存空间,高于896MB的内存,又称为高端内存,不会一直映射到内存空间的,而是使用kmap和kmap_atomic来临时映射的,剩余的126MB内存一部分用于映射高端 内存的

内核内存从PAGE_OFFSET开始的,在x86架构中的它的值是0xc0000000(3G),高于PAGE_OFFSET的虚拟内存用于内核空间的,低于的用于用户空间

x86系统内存子系统初始化
首先设置页表(可能有多级的)
完成内核内存映射(内核中的物理内存和逻辑地址只有一个固定的OFFSET,PAGE_OFFSET)

用户空间的内存管理
struct mm_struct 进程内存空间的最高级别管理结构
struct vm_area_struct 内存区域,组成进程内存
pgd_t *pgd 进程页表指针

物理地址和页
物理地址被分成离散的单元,称为页,目前大多数系统的页面大小都是4k的,实际使用的时候应该使用指定体系架构下的页面大小PAGE_SIZE。PAGE_SHIFT可以将地址转换称为页帧

高端和低端内存
系统中逻辑地址和虚拟地址不一致的情况产生了高端内存和低端内存的说法,

通常Linux x86内核将4GB的虚拟地址分割为用户空间和内核空间,在二者的上下文中使用相同的映射,一个典型的分配是将低地址3GB分给用户空间的,将剩下的高地址1GB分给内核空间的,这样由于内核只能直接操作已经映射了物理内存的虚拟地址,所以内核在大内存系统中就不能直接访问所有的物理内存,这样就产生了高端内存和低端内存的说法。

高端内存HighMemory
高端内存是没有直接映射到物理内存的内核逻辑地址

应对高端内存
高端物理内存在需要时候会被临时映射到内核虚拟内存上的
内核经常访问的数据被放在低端内存上
内核偶尔访问的数据最好放在高端内存上
不同内存区域的内存分配和换页应该有一个平衡

临时映射
kmap和kunmap产生一个永久的内存映射,但是他们有一个全局锁,不适合SMP系统的
kmap_atomic和kunmap_atomic,常用于SMP系统,产生的映射地址是每个CPU私有的

在访问特定的高端内存之前,内核必须建立明确的虚拟映射,使该页可以在内核地址空间被访问
总的来说高端内存就是没有逻辑地址的内存,反之就是低端内存

内存映射和结构
由于高端内存中无法使用逻辑地址,所以内核中处理内存的函数趋向于使用指向page结构的指针,该结构保存了内核需要知道的所有物理内存信息的,系统中的每个物理页都和一个page结构对应的
page结构和虚拟地址之间转换的函数和宏
struct page *vrt_to_page(void *kaddir);
struct page *pfn_to_page(int pfn);
void *page_addr(struct page *page);

分页
In a virtual memory system all of them addresses are virtual addresses and not physical addresses , These virtual addresses are converted into physical addresses by the processor based on information held in a set of tables maintained by the operating system
在虚拟内存系统中,所有的地址都是虚拟地址而不是物理地址,这些虚拟地址可以通过操作系统维护的一系列的表转换为物理地址

To make this translation easier,virtual and physical memory are divided into handy sized chunks called pages, These pages are all the same size,they need not be but if they were not ,
the system would be very hard to administer,Linux on Alpha AXP systems uses 8Kbytes pages and on Intel x86 systems it uses 4Kbytes pages ,Each of these pages is given a unique number; the page frame number (PFN),为了使这个转换更简单,虚拟地址和物理地址都被分成叫做内存页面小的内存块,所有的页面都是同样大小的,每页内存都有一个唯一的编号,这种编号叫做页帧号。

In this paged model , a virtual address is composed of two parts ; an effort and a virtual page frame number,If the page size is 4Kbytes,bits 11:0 of the virtual address contain the offset and bits 12 and above are the virtual page frame number,Each time the processor encounters a virtual address it must extract the offset and the virtual page frame number ,the processor must translate the virtual page frame number into a physical one and then access the location at the correct offset into that physical page ,To do this the processor uses page tabels
在这种分页模式下,虚拟地址由俩部分组成:页帧内的偏移和虚拟页帧号。如果页面大小是4KB,11:0这些位就是页帧内偏移,12位以上的叫做页帧号,每当处理器遇到虚拟内存地址,它就会把地址中的页内偏移和页帧号解出来,处理器通过页表把虚拟帧号转换成物理帧号,然后加上页内偏移就可以找到对应的物理地址了,

页表
现代系统中,处理器需要使用某种机制将虚拟地址转换成物理地址,这种机制被称为页表,它基本上是一个多层树形结构,结构化的数组中包含了虚拟地址到物理地址的映射和相关的标志位

demand paging
Linux uses demang paging to load executable images into a process virtual memory ,Whenever a command is executed, the file containing it is opened and its contents are mapped into the processes virtual memory ,This is done by modifying the data structures describing this processes memory map and is known as memory mapping,However ,only the first part of the image is actually brought into physical memory ,The rest of the image is left on disk ,As the image executes ,it generates page faults and Linux uses the processes memory map in order to determine which parts of the image to bring into memory for execution ,Linux使用按需分页来将可执行镜像载入到进程的虚拟内存空间,每当命令执行时候,命令的文件被打开,内容被映射到进程的虚拟内存上。这里是通过修改进程的内存映射相关结构体来实现的,这个过程也叫做内存映射,不过,只有镜像的开头部分被真正的放进了物理内存。余下部分还在磁盘上,镜像执行的时候,它将持续的产生页面异常,linux通过进程的内存映射表来确定镜像的哪个部分需要被载入物理内存执行的

Shared virtual memory
Virtual memory makes it easy for serveral processes to share memory,All memory access are made via page tables and each processes has its own separte page table ,For two processes sharing a physical page of memory ,its physical page frame number must appear in a page table entry in both of their page tables
虚拟内存使得多个进程共享内存更加简单,所有的内存访问都要通过页表来实现的额,对于共享一个物理内存的俩个进程来说,这个物理页面必须同时在俩个进程的页表中都有相应的页表项

虚拟内存区
VMA是用于管理进程地址空间中不同区域内核数据结构
进程的内存映射至少包含下面这些区域:
程序可执行代码区域text
数据区bss,stack,data
与每个活动的内存映射区对应的区域

可以通过cat /proc/