要了解 VMA(Virtual Memory Area)的「整體觀念」,最好的方式就是圖解說明。下圖說明了 process 與 VMA 的整體觀念。
圖:Process 與 VMA 整體觀念
Memory Descriptor
Linux 的「Process Descriptor」資料結構為 struct task_struct(include/linux/sched.h)。Process descriptor 裡的 mm field 紀錄了 process 的 VMA 資訊:
struct task_struct {
...
struct mm_struct *mm;
...
}
struct mm_struct 即是 Linux 提供的「Memory Descriptor」資料結構,以下是 struct mm_struct 的原型宣告:
struct mm_struct {
struct vm_area_struct * mmap; /* list of VMAs */
struct rb_root mm_rb;
struct vm_area_struct * mmap_cache; /* last find_vma result */
unsigned long (*get_unmapped_area) (struct file *filp,
unsigned long addr, unsigned long len,
unsigned long pgoff, unsigned long flags);
void (*unmap_area) (struct mm_struct *mm, unsigned long addr);
unsigned long mmap_base; /* base of mmap area */
unsigned long task_size; /* size of task vm space */
unsigned long cached_hole_size; /* if non-zero, the largest hole below free_area_cache */
unsigned long free_area_cache; /* first hole of size cached_hole_size or larger */
pgd_t * pgd;
atomic_t mm_users; /* How many users with user space? */
atomic_t mm_count; /* How many references to "struct mm_struct" (users count as 1) */
int map_count; /* number of VMAs */
struct rw_semaphore mmap_sem;
spinlock_t page_table_lock; /* Protects page tables and some counters */
struct list_head mmlist; /* List of maybe swapped mm's. These are globally strung
* together off init_mm.mmlist, and are protected
* by mmlist_lock
*/
/* Special counters, in some configurations protected by the
* page_table_lock, in other configurations by being atomic.
*/
mm_counter_t _file_rss;
mm_counter_t _anon_rss;
unsigned long hiwater_rss; /* High-watermark of RSS usage */
unsigned long hiwater_vm; /* High-water virtual memory usage */
unsigned long total_vm, locked_vm, shared_vm, exec_vm;
unsigned long stack_vm, reserved_vm, def_flags, nr_ptes;
unsigned long start_code, end_code, start_data, end_data;
unsigned long start_brk, brk, start_stack;
unsigned long arg_start, arg_end, env_start, env_end;
unsigned long saved_auxv[AT_VECTOR_SIZE]; /* for /proc/PID/auxv */
unsigned dumpable:2;
cpumask_t cpu_vm_mask;
/* Architecture-specific MM context */
mm_context_t context;
/* Token based thrashing protection. */
unsigned long swap_token_time;
char recent_pagein;
/* coredumping support */
int core_waiters;
struct completion *core_startup_done, core_done;
/* aio bits */
rwlock_t ioctx_list_lock;
struct kioctx *ioctx_list;
};
Memory descriptor 故名思義,是用來描述 process 記憶體資訊的資料結構。由 struct mm_struct 裡可以看到一個稱為 mmap 的 field,mmap 的 data type 為 struct vm_area_struct,這個資料結構即是我們在「Linux 的 Virtual Memory Areas(VMA):基本概念介紹」所介紹的 VMA 資料結構。
VMA 與 ELF Image 的對映關係
在「Linux 的 Virtual Memory Areas(VMA):基本概念介紹」曾經介紹過,Process 的 VMA 對映,可以由 /proc/<pid>/maps 檔案查詢;例如 pid 1(init)的 VMA mapping 為:
$ cat /proc/1/maps 08048000-0804e000 r-xp 00000000 08:01 12118 /sbin/init 0804e000-08050000 rw-p 00005000 08:01 12118 /sbin/init 08050000-08054000 rwxp 00000000 00:00 0 40000000-40016000 r-xp 00000000 08:01 52297 /lib/ld-2.2.4.so 40016000-40017000 rw-p 00015000 08:01 52297 /lib/ld-2.2.4.so 40024000-40025000 rw-p 00000000 00:00 0 40025000-40157000 r-xp 00000000 08:01 58241 /lib/i686/libc-2.2.4.so 40157000-4015c000 rw-p 00131000 08:01 58241 /lib/i686/libc-2.2.4.so 4015c000-40160000 rw-p 00000000 00:00 0 bfffe000-c0000000 rwxp fffff000 00:00 0
列表結果便能用來說明 VMA 與 ELF image 之間的關係。搭配上圖來說明列表結果的 VMA 對映關係,如下:
另外,要留意的是,在文中所指的 code section 與 data section 不見得就是 ELF 的 .text section 與 .data section;我們以 code section 來表示所有可執行的節區,以 data section 來表示包含資料的節區。
在整個 VMA 的討論過程中,我們只針對 code section 與 data section 做討論(如圖),至於 .bss section 的話,原則上另案來討論其核心實作會比較實際一些。
| Also See |
|
Jollen's Blog 使用 Github issues 與讀者交流討論。請點擊上方的文章專屬 issue,或 open a new issue
您可透過電子郵件 jollen@jollen.org,或是 Linkedin 與我連絡。更歡迎使用微信,請搜尋 WeChat ID:jollentw