- Memory Addressing
- Segmentation
- Physical and Virtual Memory
- Page Table Management
- Data Structures
- Allocators
- vmalloc
- Page Allocation
- sla/ub
- Process address space
- Logical Address
- In machine language instruction, used to determine a operator or instruction address
- Include segment and offset
- Linear Address (aka. Virtual Address) => "Express Ability"
- In 32-bit machine => 4GB i.e. 4294967295. From 0x00000000 to 0xFFFFFFFF
- Physical Address => "Actually You Have"
Logical Address ===Segmentation Unit==> Linear Address ===Paging Unit==> Physical address
\ /
Memory Management Unit (MMU)IA-32 Memory Management Facilities:
- Segmentation: from Logical Address to Linear Address
- Paging (demand paging): from Linear Address to Phsical Address
In Linux, it used these two mechanism at the same time. (because you have to support hardware... but tend to weaker the segmentation)
- Index
- which is an index into the GDT and thus points to an entry of the GDT called a segment descriptor
- Table Indicator
- which indicates whether the selector points to a GDT or an LDT
- Requestor Privilege Level (RPL)
- which is the privilege level of the CPU when the selector is loaded
Privilege Level in Linux only 0 and 3. One for operating system (kernel) level, another for user space level.
-
RPL (Request Privilege Level)
-
CPL (Current Privilege Level)
-
DPL (Descriptor Privilege Level)
-
Access data, gate descripter: max(CPL, RPL) <= DPL
-
...
- Visible part: Segment Selector
- Hidden part: Base Address, Limit, Access Information
Linux uses segmentation only when required by the 80x86 architecture!
Continuous, non-segment address space
Linux .....
NUMA (Non Uniform Memory Access)
In x86 used UMA
Each node split into many zones
Because of hardware limitations, the kernel cannot treat all pages as identical
ZONE_DMAZONE_NORMALZONE_HIGHMEM(in 32-bit system, there may have some physical address that cannot be expressed by virtual address thus need this zone.)
In X86
ZONE_DMA=> 16MB of memoryZONE_NORMAL=> 16MB ~ 896MBZONE_HIGHMEM=> 896MB ~ End (in 64-bit system it is empty)
...TBD
1GB Kernel Space + 3GB User Space
- All the user share the 1GB Kernel Space
- But for a single thread vision. It can see a 4GB memory space.
- vmalloc address space
- Noncontiguous physical memory allocation
- kmap address space
- Allocation of memory from
ZONE_HITHMEM
- Allocation of memory from
- Fixed mapping
- Compile-time virtual memory allocation
Used only 48-bit. => 128TB for user and another half for kernel
- Memory in
ZONE_DMAandZONE_NORMALis direct-mapped and all page frames are described bymem_maparray
Kernel virtual address => physical address
Kernel virtual address => struct page
arch/x86/include/asm/io.h - line 131
/**
* virt_to_phys - map virtual addresses to physical
* @address: address to remap
*
* The returned physical address is the physical (CPU) mapping for
* the memory address given. It is only valid to use this function on
* addresses directly mapped or allocated via kmalloc.
*
* This function does not give bus mappings for DMA transfers. In
* almost all conceivable cases a device driver should not be using
* this function
*/
static inline phys_addr_t virt_to_phys(volatile void *address)
{
return __pa(address);
}
#define virt_to_phys virt_to_physarch/x86/include/asm/page.h - line 42 and 69
#define __pa(x) __phys_addr((unsigned long)(x))
#define virt_to_page(kaddr) pfn_to_page(__pa(kaddr) >> PAGE_SHIFT)- Page: a block of data, which may be stored in any page frame or on disk! (not necessary be in memory)
- Page Frame: physical addresses in main memory
4 level paging model
- PGD Page Global Directory (頁全局目錄)
- PUD Page Upper Directory (頁上級目錄)
- PMD Page Middle Directory (頁中間目錄)
- PT Page Table (頁表)
Multi-level is good when data is sparse
TBD
PDPT Page Directory Pointer Table
include/linux/mmzone.h
mm/memory.c
include/linux/mm_types.h
include/linux/page-flags.h
TBD
#define PAGE_SHIFT 12
#ifdef __ASSEMBLY__
#define PAGE_SIZE (1 << PAGE_SHIFT)
#else
#define PAGE_SIZE (1UL << PAGE_SHIFT)
#endif
#define PAGE_MASK (~(PAGE_SIZE-1))typedef struct {
unsigned long pte;
} pte_t;
typedef struct {
unsigned long pmd[16];
} pmd_t;
typedef struct {
unsigned long pgd;
} pgd_t;
typedef struct {
unsigned long pgprot;
} pgprot_t;
typedef struct page *pgtable_t;#define pte_val(x) ((x).pte)
#define pmd_val(x) ((&x)->pmd[0])
#define pgd_val(x) ((x).pgd)
#define pgprot_val(x) ((x).pgprot)
#define __pte(x) ((pte_t) { (x) } )
#define __pmd(x) ((pmd_t) { (x) } )
#define __pgd(x) ((pgd_t) { (x) } )
#define __pgprot(x) ((pgprot_t) { (x) } )- Boot Memory Allocator
alloc_bootmem()free_bootmem()
- Physical Page Allocator (buddy system)
alloc_pages()__get_free_pages()
- Slab Allocation
kmalloc()kfree()
- Virtual Memory Allocator
vmalloc()vfree()
vmalloc.c
Not yet



