Flylib.com

Books Software

 
 
 

Virtual memory routines


Virtual memory routines

Now let's move on to the next set of kernel memory functions: those dealing with the chunks of virtual address space in use by the various processes on the system.

A substantial part of the kernel is devoted to virtual memory processing. Some of these routines handle very specific hardware details, which we won't get into because they will be different for every architecture and MMU. However, the sections dealing with more generic, "virtual" topics can be covered. These are functions that manipulate the address spaces of processes (and the as structure) and the individual segments ( seg structures), page-handling routines, and a general high-level interface to the hat layer, for hardware address translation.

Along with these you will find more specialized routines handling anonymous memory, swap space, and the seg_vn structure, which deals with segments that refer to actual vnodes (files). There are some common naming conventions that should enable you to identify functions in each of these areas, although we will touch on only a few specific function names .

Let's start at the highest level and work down. The include files, which define the structures we're interested in and often list the important routines, are all contained in the /usr/include/vm directory for both SunOS 4 and Solaris 2.


Address spaces

Every process has its own address space, so every process table entry contains a pointer to an address space structure just for that process. The structure serves mostly as an anchor for a list of segments, which describe the valid ranges of virtual addresses that each individual process has. The structure contains global pieces of information, such as a flag that indicates that the entire process is locked in memory, and a count of the number of physical pages actually in use by this process (the a_rss field: the resident set size ).

A few functions deal with the address space as a whole. Some of these are:

  • as_segat() ” Find and return the segment that contains the specified address.

  • as_alloc() ” Allocate a new address space structure.

  • as_dup() ” Create a copy of an existing structure.

  • as_fault() ” Handle a fault at the given address for "size" bytes. This function is the one eventually called to handle a page fault.

  • as_unmap() ” Unmap all the segments that cover the given address range.

There are more functions; they have similar names and deal with various requirements at this level: locking and unlocking address ranges, finding an empty memory range for mapping something in, adding a segment to the list, and so forth.


Segments

Segments are individual chunks of continuous address space, generally with separate characteristics (such as belonging to a particular file). Each segment structure ( struct seg ) contains a base address and a size , links to other segments that belong to the same process, and a pointer to a list of functions. This list, defined in the seg_ops structure, contains addresses of functions that perform a certain set of operations specifically for the given type of segment. These operations include things like:

  • Unmapping a portion of a segment

  • Removing an entire segment and any hardware mappings for the pages

  • Handling a page fault that occurs inside this segment

  • Performing a sync

  • Returning a pointer to the vnode that is associated with this segment

Some actual functions for the different segment types are:

  • segvn_unmap() ” Do an unmap operation on a seg_vn type of segment. A seg_vn is one that refers to a file (a vnode). A memory-mapped file would be a seg_vn type.

  • segdev_free() ” Free a segment that referred to a device. A frame buffer would be handled as a seg_dev type of segment.

There are also some generic segment handling functions that deal with the segment structures themselves .

  • seg_alloc() ” Allocate a segment for the specified address range and attach it to the list of segments for a particular address space structure.

  • seg_attach() ” Insert a segment structure into a list of segments. Called by seg_alloc() to actually do the work.

  • seg_unmap() ” Is a front end to the segment-specific unmap operation.

  • seg_free() ” Remove a segment structure from the list and actually free up the structure. This function also calls the segment type-specific free function to take care of releasing any resources used by the segment.

Segments are another "virtual" layer, like vnodes, to allow a range of addresses to refer to many different things (a file, a device like a frame buffer, anonymous memory) but still be handled at a high level in a standardized way.