内存管理

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

booting according to the information passed from BIOS. On i386, memory range information is usually stored in E820 table and EFI memory mao.
* Other names and brands may be claimed as the property of others
Stack TASK_SIZE
mm
Shared lib Heap
Elf’s code and data segment
SSG Core Sof t ware Division
9
Virtual Memory
• Page tables is the primary structure that kernel used to map virtual • • • • •
calls mmap munmap and brk.
• User space program can allocate virtual memory spaces via system
* Other names and brands may be claimed as the property of others
• Each process may have it’s own virtual memory space. • In Linux*, kernel and user space are in the unified virtual memory
space.
• On i386 platform, virtual memory space is divide into 2 regions, 03G for user space, 3G-4G for kernel space
12
I386 2-LEVEL page table
10bit s 10bit s 12bit s
PGD
PMD
Owenku.baidu.comfset 4k page
1024 entry
1024 entry
CR3
SSG Core Sof t ware Division
13
Virtual Memory
• PTE entry
Page Frame Number Page present Bit
• Each node is divided into zones, There are currently 4 zones in • Zone is the core structure of page allocator.
* Other names and brands may be claimed as the property of others
address to physical address Page tables is a multi-way multi-level tree structure. Different architecture may support different level of page tables. Kernel has generic code and macro to support 4 level page table, page table may be collapsed by macro define. Every mm structure has a pointer pgd pointed to base level entry of page tables. On i386, this pgd is corresponding to CR3 register. IA32 use 2 levels page table and 3 levesl page table
SSG Core Sof t ware Division
6
Virtual Memory
• Kernel can provide more virtual memory space than physical memory by
a method called demand paging. space is only 4G.
SSG Core Sof t ware Division
3
Page structure
• Linux* assume memory is based on unit of page • Each physical page of memory is described with a PFN (Page Frame
* Other names and brands may be claimed as the property of others
SSG Core Sof t ware Division
4
Page structure
flags Mem_map page struct page struct page struct …..
Physical Memory
• Linux* divide physical memory in to the data structure of node.
Each node represent a bank of memory attached to a NUMA(Non Uniform Memory Access) machine. On SMP(Symmetric Multi Processor) and UP(Single CPU) system, there is only 1 node. Linux*, ZONE_DMA, ZONE_DMA32, ZONE_NORMAL, ZONE_HIGHMEM.
• There is a red-black tree of vm_struct vma list in mm structure, those • vm_strcuture has different flags, kernel use this flag to support
SSG Core Sof t ware Division
memory space.
• There are hardware unit which can translate virtual address into
physical address called MMU (memory management unit). The basic structure of MMU is page table.
arch/i386/mm/mmap.c,
SSG Core Sof t ware Division
11
Page table
PGD index PUD index PMD index pte PTE index Offset
pud
pmd
ram
pgd
Pgd pointer
1 page
SSG Core Sof t ware Division
Vmas are organized into red-black tree and linked list
SSG Core Sof t ware Division
8
VMA
• Vma structure is mainly used in userspace for
define a processes virtual address ranges. define the access right of arrange of addresses.
SSG Core Sof t ware Division
2
Physical Memory
• Zone may contains holes, there is no assumption that physical
memory in a zone is continuous.
• Linux* kernel will setup node and zone structure at the time of
Linux* Virtual Memory Management
Intel® China Software Center
SSG Core Sof t ware Division
* Other names and brands may be claimed as the property of others
• Page structure itself is saved in a per node structure mem_map • Kernel will initialize all the page structure at boot time. • Pages are often organized in list
7
Virtual Memory
• typical virtual memory mapping of a on I386
Page table mapped kernel area Identity mapping user space stack mmaped space mmaped space .text and other static block of elf file mmaped .so files 4G – 128M 3G
_count
_mapcount Address spaceing Index LRU
SSG Core Sof t ware Division
5
Virtual Memory
• Virtual memory is a concept that each process can has his own linear
SSG Core Sof t ware Division
10
Summary
• Concept of physical memory • Concept of virtual memory • Page structure • Reference code mm/mmap.c, mm/page_alloc.c, mm/bootmem.c
Number).
• There is a structure page represent each physical page
• Page structure is the basic unit that kernel handle with memory
allocation and operation.
PFN
Protect bits
1
SWAP Block Number
Page present Bit
OFFSET
TYPE
0
SSG Core Sof t ware Division
14
Virtual Memory
• TLB (Translation Look Aside buffer)
TLB is a kind of associative cache which is used to translated virtual address to physical address. On I386, if hardware can’t find a TLB entry , it will automatically issue a search on page table. If page table entry is found, it will insert a TLB entry, otherwise hardware will inject a page fault where OS can handle. There are 2 ways to flush TLB, write to CR3 or with invlpg. Some pages are mapped globally that do not need to flush at the time of task switch. I386 need a TLB flush at the time of task switch, and at the time of unmap a region of virtual address. on SMP system, tlb flush need to be broadcasted to each CPU.
• On i386, Kernel can use more physical than 4G although 1 process’s VM • There is a mm structure in task structure to represent a tasks virtual
memory space status. Different threads within a process share 1 mm structure, kernel thread set the mm structure to NULL. vma are also organized into a sorted list. protection control of virtual memory.
相关文档
最新文档