Author: Harold Wang
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.
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.
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:
There are at most 8192 Segment Descriptors in the GDT.
Author: Harold Wang
Using Segment
--Bacis Flat Model(Linux use this)
--Protected Flat Model
--Multi-Segment Model
From Logical address to Linear address
Author: Harold Wang
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
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
EveryNode uses structure mem_map to save the information of the page.
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
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!
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
3. Page Table Management
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
4. Allocating Memory in the Linux Drivers
Author: Harold Wang
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.
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