Linux Kernel Memory Management (1)

Author: Harold Wang

http://blog.csdn.net/hero7935

1. Memory Addressing

Logical address: used in machine instruction, including Segment and Segment-offset
Linear address(virtual address): in 32 bit machine, the space is 4GB.
Physical address: address used to access phsical address unit.
image

Memory Arbiter:
--used for concureency between MultiCPUs
--used for concureency between SigleCPU and DMA
 

Protection Mode—segmentation and paging

– Segmentation provides a mechanism of isolating individual code,
data, and stack modules so that multiple programs (or tasks) can
run on the same processor without interfering with one another.
– Paging provides a mechanism for implementing a conventional
demand-paged, virtual-memory system where sections of a
program’s execution environment are mapped into physical
memory as needed. The use of paging, however, is optional.
image
NOTE: program should fill in the Segment-Registers BEFORE  accessing segment. Although there are many segment selectors, only 6 Segment-Registers existing, they are: CS SS DS ES FS GS, including the visible part and hidden part:image
image 
There are at most 8192 Segment Descriptors in the GDT.

Author: Harold Wang

http://blog.csdn.net/hero7935



image
image

Using Segment

--Bacis Flat Model(Linux use this)
image
--Protected Flat Model
image
--Multi-Segment Model
image
From  Logical address to Linear address

image

Author: Harold Wang

http://blog.csdn.net/hero7935


NOTE: SigleCPU has sigle GDT, MultiCPUs have many GDTs each.

In Linux, Logical addresss is equal to linear address!
Difference: user mode and kernel mode(Current Privilege level)
_USER_CS & _USER_DS
_KERNEL_CS & _KERNEL_DS

what’s the real meaning of Segment memory management ?
--allocating different linear address to each process & limiting the access of a process by different linear address space!

The critical point is different process has different PageTable! So, same linear address has different physical address. Using alloc_page() function.

2. Introduction to Linux Virtual Memory

Linux uses data structure—Node to illustrate the phsical memory, represating a bank in memory. On normal x86 PCs(which use UMA model), Linux uses a single node to represent physical memory—>contig_page_data
image image

Node is devided into 3 Zones: ZONE_DMA(contains pages capable of undergoing DMA)  ZONE_NORMAL(contains regularly mapped pages)  ZONE_HIGHMEM(contains pages not permanently mapped into the kernel address space:

– ZONE DMA First 16MiB of memory
– ZONE NORMAL 16MiB - 896MiB
– ZONE HIGHMEM 896 MiB – End

image

EveryNode uses structure mem_map to save the information of the page.

image

program in user mode or kernel mode can access linear address from 0x00000000 to 0xbffffff, but accessing linear address above 0xc0000000 only in kernel mode. PAGE_OFFSET= 0xc0000000(3GB) , master kernel page global directory

Author: Harold Wang

http://blog.csdn.net/hero7935

image  ONLY in i386
In tast structure, mm structure is used to pointed to the virtual memory space of a task. KERNEL thread’s mm structure is NULL!

image

memory in ZONE_DMA and ZONE_NORMAL is directmapped
and all page frames are described by mem_map array
kernel virtua address –>physical address—>struct page
(use physical addresss as an index into the mem_map array)

Virtual memory in User space

image image

3. Page Table Management

image image

PSE:
-- starting with the pentium model, 80x86 introduces the PSE falg of cr4 to indicate the page frames to be 4MB instead of 4KB in size. in these cases, the kernel can do without intermediate Page Tables and thus save memory and preserve TLB entries

PAE:
--Starting with the Pentium Pro, all Intel processors are now able to address up to 236 = 64 GB of RAM by setting the Physical Address Extension(PAE) flag in the cr4(in this cases, page size is 2MB instead of 4KB)
--translates 32-bit linear addresses into 36-bit physical ones. a PAE Page Table entry: 36 =12 flag bits + 24 physical address bits

image

 

4. Allocating Memory in the Linux Drivers

Author: Harold Wang

http://blog.csdn.net/hero7935

void* kmalloc(size_t size, int flags);


Look asideCache:
   keme_cache_t *kmem_cache_create(const char* name,
     size_t size, size_t offset, unsigned long flags,
    void(*constructor)(void*,kmem_cache_t*,unsigned long flags),
  void(*destructor)(void*, kmem_cache_t*, unsigned long flags));

Memory Pool(mempool)
There are places in the kernel where memory allocations cannot be allowed to fail. As a way of guaranteeing allocations in those situations, the kernel developers created an abstraction known as a memory pool.

A memory pool is really just a form of a lookaside
cache that tries to always keep a list of free memory
around for use in emergencies.

image

get_free_page:
--kernel allocate memory block in size of a page

vmalloc:
--allocate virtual memory not contiguous.
void vfee(void* addr);

5. Physical Page Allocation(updating)

 

Author: Harold Wang

http://blog.csdn.net/hero7935

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值