340 likes | 540 Views
Chapter 5 Memory Management ---- Memory Segment . Chen Yu yuchen@tsinghua.edu.cn. Outline. Introduction to Memory Segments Memory Segmemt Driver: seg_vn Memory mapped files Shared mapped files. Memory segments characteristics.
E N D
Chapter 5 Memory Management---- Memory Segment • Chen Yu • yuchen@tsinghua.edu.cn
Outline • Introduction to Memory Segments • Memory Segmemt Driver: seg_vn • Memory mapped files • Shared mapped files
Memory segments characteristics • Memory segments manage the mapping of a linear range of virtual memory into an address space • allow both memory and devices to be mapped into an address space • allows different behaviors for different segments
Segment associated data structures----Header File <vm/seg.h> • struct seg { • caddr_t s_base; /* base virtual address */ • size_t s_size; /* size in bytes */ • struct as *s_as; /* containing address space */ • seg_next s_next; /* next seg in this address space */ • struct seg *s_prev; /* prev seg in this address space */ • struct seg_ops *s_ops; /* ops vector: see below */ • void *s_data; /* private data for instance */ • };
segment driver • a segment driver implementation is required to provide at least the following: • functions to create a mapping for a linear address range • page fault handling routines to deal with machine exceptions within that linear address range • a function to destroy the mapping
Common Segment Drivers • The common Segment Drivers: • seg_vn: The vnode mappings into process address spaces are managed with the seg_vn device driver • seg_kmem: The segment from which the bulk of nonpageable kernel memory is allocated • seg_dev: Mapped hardware devices. • seg_kp, seg_spt, seg_map……
Outline • Introduction to Memory Segments • Memory Segmemt Driver: seg_vn • Memory mapped files • Shared mapped files
seg_vn’s characteristics • seg_vn segment driver manages the following mappings into process address space • Executable text • Executable data • Heap and stack (anonymous memory) • Shared libraries • Mapped files
Outline • Introduction to Memory Segments • Memory Segmemt Driver: seg_vn • Memory mapped files • Shared mapped files
Characteristics of Memory mapped files • We can map a file into a process’s address space with the mmap system call • A vnode segment handles memory address translation and page faults for the memory range requested in the mmap system call • When we create a file mapping, we put the vnode and offset of the file being mapped into the segvn_data structure members, vp and offset.
Creating a mapping for a file • Steps: • mmap() system call, • ufs_map() on UFS file system • segvn_create() function in seg_vn driver
Outline • Introduction to Memory Segments • Memory Segmemt Driver: seg_vn • Memory mapped files • Shared mapped files
Characteristics of Shared mapped files • The address space segment architecture makes it easy for two or more processes to map the same file into their address space • Each process has its own virtual memory mapping to the file, but they all share the same physical memory pages for the files.
Copy on Write: When & How • When • The copy-on-write process occurs when a process writes to a page that is mapped with MAP_PRIVATE • How • seg_vn implements a copy-on-write by setting the hardware MMU permissions of a segment to read-only and setting the segment permissions to read-write.
Chapter 9 Memory Management---- Anonymous Memory • Chen Yu • yuchen@tsinghua.edu.cn
Outline • Instroduction to Anonymous memory • Anonymous memory layer • Swap space
Characteristics of Anonymous Memory • Anonymous memory refers to pages that are not directly associated with a vnode. • process’s heap space • process’s stack • copy-on-write pages.
Outline • Instroduction to Anonymous memory • Anonymous memory layer • Swap space
Characteristics of Anonymous Memory Layer • Anonymous pages are created through the anon layer interfaces • The first time a segment receives a page fault, it allocates an anon map structure and puts a pointer to the anon header into the amp field of the anonymous map • Each anon slot points to an anon structure, which describes the virtual page of memory corresponding to the page-sized area in the address space.
Data Structure • struct anon { • struct vnode *an_vp; /* vnode of anon page */ • struct vnode *an_pvp; /* vnode of physical backing store */ • anoff_t an_off; /* offset of anon page */ • anoff_t an_poff; /* offset in vnode */ • struct anon *an_hash; /* hash table of anon slots */ • int an_refcnt; /* # of people sharing slot */ • };
Outline • Instroduction to Anonymous memory • Anonymous memory layer • Swap space
The swapfs Layer • Swap space is used as a backing store for anonymous pages of memory • when we are short of memory, we can copy a page out to disk and free up a page of memory.