[haiku-development] A genearl introduction to Haiku vm

  • From: "Zhao Shuai" <upczhsh@xxxxxxx>
  • To: <haiku-development@xxxxxxxxxxxxx>
  • Date: Fri, 11 Apr 2008 17:45:12 +0800

Hi,

I was investigating the Haiku vm code these days and found it a little hard to 
understand  without directions. It's a pity that Haiku still doesn't have its 
vm tutorial. So I intend to write something and hope it can help the kernel 
newbies to get a quick start.

Several articles are needed to introduce different aspects of the vm system. 
Today, I finished one introducing the basic data structures used in the vm 
system.

There may be mistakes in my article. Hope you can pointed them out in order not 
to mislead others.

A General Introduction to Haiku Virtual Memory Management
The Big Picture


Figure 1  The Framework of the vm subsystem

The Major Facilities of Haiku Virtual Memory System
There are 5 key data structures in the system that handles the virtual memory 
management:

l        vm_address_space: describes the virtual address space of a process. 

l        vm_area: represents a range of virtual memory address with the same 
attributes (e.g. the text section of a process can be an area and data section 
can be another).

l        vm_page: represents one physical memory page.

l        vm_cache: describes the virtual address space of a process. 

l        vm_store: key structure supporting page swap.

Virtual Address Space
The virtual address space of a process is the range of memory addresses that 
are presented to the process as its environment; some addresses are mapped to 
physical memory, some are not. A process’s virtual address space is skeleton is 
created by the time when the new process is created. Each process virtual 
address space has at least four segments:

(1)   Text segment: the executable instructions mapped from the disk binary

(2)   Data segment: the initialized variables mapped from the disk binary

(3)   Heap: space for dynamic memory allocation

(4)   Stack: space for function calls



Figure 2  A typical process’s virtual address space

Virtual Memory Areas
An area is a chunk of virtual memory of any size. A process may divide its 
address space into a number of areas(regions). Initially a process begins with 
four areas; a shared area with its text, a private area for its initialized 
data, a private area for its uninitialized data and heap, and a private area 
for its stack. In addition to these areas, a process may allocate new ones. The 
areas may not overlap and the system may impose an alignment constraint, but 
the size of the region should not be limited beyond the constraints of the size 
of the virtual address space.

One type of region is “anonymous memory”. Uninitiated data uses such an area, 
privately mapped; it is zero-fill-on-demand and its contents are abandoned when 
the last reference is dropped. Unlike a area that is mapped from a file, the 
contents of an anonymous area will never be read from or written to a disk 
unless memory is short and part of the area must be paged to a swap area. If 
one of these areas is mapped shared, then all processes see the changes in the 
area. This difference has important performance considerations; the overhead of 
reading, flushing, and possibly allocating a file is much higher than simply 
zeroing memory.



Figure 3  Virtual address space and virtual memory areas

Page Cache
A page cache is a list of pages that are backed by regular files, block devices 
or swap. Pages exist in this cache for two reasons. The first is to eliminate 
unnecessary disk reads. Pages read from disk are stored in a page hash table 
hashed on the struct vm_address_space and the offset. This table is always 
searched before the disk is accessed. The second reason is that the page cache 
forms the queue as a basis for the page replacement algorithm to select which 
page to discard or pageout.

Pages
Pages are the basic unit of the memory management subsystem. Physical memory is 
managed on a page-by-page basis through the vm_page structure. Each physical 
frame has associated with it a vm_page structure.

Pages of physical memory are categorized through the placement of their 
respective vm_page structures on one of several paging queues. There are 5 
types of queues for storing pages:

(1) sFreePageQueue: 

(2) sClearPageQueue: 

(3) sModifiedPageQueue: 

(4) sInactivePageQueue: 

(5) sActivePageQueue: 



Figure 4  Link all the structures together

 

JPEG image

GIF image

GIF image

GIF image

Other related posts: