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.
imageLinux Kernel Memory Management (1)
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 _disibledevent=>imageimageLinux Kernel Memory Management (1)
NOTE: program should fill in the Segment-Registers BEFORE accessing segment. Although there are many segment selectors, _disibledevent=>imageimageimageLinux Kernel Memory Management (1)
imageimageimageimageLinux Kernel Memory Management (1)
There are at most 8192 Segment Descriptors in the GDT.
Author: Harold Wang
http://blog.csdn.net/hero7935
imageimageimageimageimageLinux Kernel Memory Management (1)
imageimageimageimageimageimageLinux Kernel Memory Management (1)
Using Segment
--Bacis Flat Model(Linux use this)
imageimageimageimageimageimageimageLinux Kernel Memory Management (1)
--Protected Flat Model
imageimageimageimageimageimageimageimageLinux Kernel Memory Management (1)
--Multi-Segment Model
imageimageimageimageimageimageimageimageimageLinux Kernel Memory Management (1)
From Logical address to Linear address
imageimageimageimageimageimageimageimageimageimageLinux Kernel Memory Management (1)
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. _disibledevent=>imageimageimageimageimageimageimageimageimageimageimageLinux Kernel Memory Management (1) imageimageimageimageimageimageimageimageimageimageimageimageLinux Kernel Memory Management (1)
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
imageimageimageimageimageimageimageimageimageimageimageimageimageLinux Kernel Memory Management (1)
EveryNode uses structure mem_map to save the information of the page.
imageimageimageimageimageimageimageimageimageimageimageimageimageimageLinux Kernel Memory Management (1)
program in user mode or kernel mode can access linear address from 0x00000000 to 0xbffffff, but accessing linear address above 0xc0000000 _disibledevent=> Author: Harold Wang
http://blog.csdn.net/hero7935
imageimageimageimageimageimageimageimageimageimageimageimageimageimageimageLinux Kernel Memory Management (1) _disibledevent=>In tast structure, mm structure is used to pointed to the virtual memory space of a task. KERNEL thread’s mm structure is NULL!
imageimageimageimageimageimageimageimageimageimageimageimageimageimageimageimageLinux Kernel Memory Management (1)
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
imageimageimageimageimageimageimageimageimageimageimageimageimageimageimageimageimageLinux Kernel Memory Management (1) imageimageimageimageimageimageimageimageimageimageimageimageimageimageimageimageimageimageLinux Kernel Memory Management (1)
3. Page Table Management
imageimageimageimageimageimageimageimageimageimageimageimageimageimageimageimageimageimageimageLinux Kernel Memory Management (1) imageimageimageimageimageimageimageimageimageimageimageimageimageimageimageimageimageimageimageimageLinux Kernel Memory Management (1)
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 _disibledevent=>
imageimageimageimageimageimageimageimageimageimageimageimageimageimageimageimageimageimageimageimageimageLinux Kernel Memory Management (1)
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.
imageimageimageimageimageimageimageimageimageimageimageimageimageimageimageimageimageimageimageimageimageimageLinux Kernel Memory Management (1)
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
Tags: 

延伸阅读

最新评论

发表评论