一、进程的用户空间管理
每个进程最大可拥有3GB的私有虚拟内存空间。如何划分这3GB的空间?概括地说,用户程序经过编译、链接后形成的二进制映像文件有一个代码段和数据段,代码段在下,数据段在上。数据段中包括所有静态分配的数据空间,既全局变量和所有声明为static的局部变量,是在建立一个进程的运行映像时分配好的。除此之外,堆栈使用空间也属于基本要求,也是在建立进程时分配好的。BSS未初始化的数据段。
每个进程有3GB的用户空间,但其中的地址都是虚地址。用户进程在这个虚拟内存中并不能运行起来,必须把用户空间中的虚地址映射到物理存储空间,这种映射的建立和管理由内核完成。向内核申请一块空间,是指请求内核分配一块虚存区间和相应的若干物理页面,并建立映射关系。
内核在创建进程时根据需要才真正分配一些物理页面而建立映射。系统利用请页机制来避免对物理内存的过分使用。因为进程访问的用户空间中的页可能当前不在物理内存中,操作系统通过请页机制把数据从磁盘装入到物理内存。系统需要修改进程的页表,标志用户空间中的页已经装入到物理页面中。
1、进程用户空间描述
一个进程的用户地址空间由两个数据结构来描述。mm_struct结构对进程的整个用户空间进行描述,简称内存描述符;vm_area_struct结构对用户空间中各个区间(虚存区)进行描述(未初始化数据区、数据区以及堆栈区)。
1)mm_struct结构
mm_struct结构用来描述一个进程的虚拟地址空间,该结构用来描述进程的整个用户空间。
struct mm_struct {
struct vm_area_struct *mmap; /* list of VMAs */
struct rb_root mm_rb;
u32 vmacache_seqnum; /* per-thread vmacache */
#ifdef CONFIG_MMU
unsigned long (*get_unmapped_area) (struct file *filp,
unsigned long addr, unsigned long len,
unsigned long pgoff, unsigned long flags);
#endif
unsigned long mmap_base; /* base of mmap area */
unsigned long mmap_legacy_base; /* base of mmap area in bottom-up allocations */
unsigned long task_size; /* size of task vm space */
unsigned long highest_vm_end; /* highest vma end address */
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) */
atomic_long_t nr_ptes; /* PTE page table pages */
#if CONFIG_PGTABLE_LEVELS > 2
atomic_long_t nr_pmds; /* PMD page table pages */
#endif
int map_count; /* number of VMAs */
spinlock_t page_table_lock; /* Protects page tables and some counters */
struct rw_semaphore mmap_sem;
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
*/
unsigned long hiwater_rss; /* High-watermark of RSS usage */
unsigned long hiwater_vm; /* High-water virtual memory usage */
unsigned long total_vm; /* Total pages mapped */
unsigned long locked_vm; /* Pages that have PG_mlocked set */
unsigned long pinned_vm; /* Refcount permanently increased */
unsigned long shared_vm; /* Shared pages (files) */
unsigned long exec_vm; /* VM_EXEC & ~VM_WRITE */
unsigned long stack_vm; /* VM_GROWSUP/DOWN */
unsigned long def_flags;
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;
&