Virtual mapping
author | richard |
copyright | See Copyright and License. |
date | 1998-05-11 |
index terms | pair: virtual mapping; design |
revision | //info.ravenbrook.com/project/mps/version/1.114/design/vm.txt#1 |
status | incomplete design |
tag | design.mps.vm |
Introduction
.intro: This the design of the VM interface. The VM interface provides a simple, low-level, operating-system independent interface to address-space. Each call to VMInit() reserves (from the operating-system) a single contiguous range of addresses, and updates a VMStruct thereafter used to manage this address-space. The VM interface has separate implementations for each platform that supports it (at least conceptually, in practice some of them may be the same). The VM module provides a mechanism to reserve large (relative to the amount of RAM) amounts of address space, and functions to map (back with RAM) and unmap portions of this address space.
.motivation: The VM is used by the VM Arena Class. It provides the basic substrate to provide sparse address maps. Sparse address maps have at least two uses: to encode information into the address of an object which is used in tracing (the Zone Test) to speed things up; to avoid fragmentation at the segment level and above (since the amount of address space reserved is large compared to the RAM, the hope is that there will also be enough address space somewhere to fit any particular segment in).
Definitions
.def.reserve: The reserve operation: exclusively reserve a portion of the virtual address space without arranging RAM or backing store for the virtual addresses. The intention is that no other component in the process will make use of the reserved virtual addresses, but in practice this may entail assuming a certain amount of cooperation. When reserving address space, the requester simply asks for a particular size, not a particular range of virtual addresses. Accessing (read/write/execute) reserved addresses is illegal unless those addresses have been mapped.
.def.map: The map operation: arrange that a specified portion of the virtual address space is mapped from the swap, effectively allocating RAM and/or swap space for a particular range of addresses. If successful, accessing the addresses is now legal. Only reserved addresses should be mapped.
.def.unmap: The unmap operation: the inverse of the map operation. Arrange that a specified portion of the virtual address space is no longer mapped, effectively freeing up the RAM and swap space that was in use. Accessing the addresses is now illegal. The addresses return to the reserved state.
.def.vm: "VM" stands for Virtual Memory. Various meanings: a processor architecture's virtual space and structure; the generic idea, interface, or implementation of the MPS VM module; the C structure (struct VMStruct) used to encapsulate the functionality of the MPS VM module; an instance of such a structure.
.def.vm.mps: In the MPS, a "VM" is a VMStruct, providing access to the single contiguous range of address-space that was reserved (from the operating-system) when VMInit() was called.
Interface
Size PageSize(void)
.if.page.size: Return the operating system's "page size", that is, the granularity with which the operating system can map and unmap virtual memory. For speed (on systems like Windows where determining the page size involves a system call), this is cached in each VM descriptor and accessible via the VMPageSize() function.
Res VMParamFromArgs(void *params, size_t paramSize, ArgList args)
.if.param.from.args: This function processes the keyword arguments (the ones that are relevant to the VM implementation) in the args parameter and stores a description of them in the buffer pointed to by params (which is paramSize bytes long). It is an error if the buffer is not big enough store the parameters for this VM implementation.
Res VMInit(VM vm, Size size, Size grainSize, void *params)
.if.init: VMInit() is responsible for reserving an amount of virtual address space. The params argument points to a parameter block initialized by a call to VMParamFromArgs(). If successful, the VM descriptor given by the parameter vm is updated to describe the address space, and ResOK is returned. The created VM has at least size bytes of virtual memory reserved starting at an address which is a multiple of grainSize.
If there's not enough address space to reserve a block of the given size, ResRESOURCE is returned. The reserved virtual memory can be mapped and unmapped by calling VMMap() and VMUnmap().
void VMFinish(VM vm)
.if.finish: A VM is destroyed by calling VMFinish(). Any address space that was mapped through this VM is unmapped.
Res VMMap(VM vm, Addr base, Addr limit)
.if.map: Map the range of addresses from base (inclusive) to limit (exclusive) into memory. It is an error if the range does not lie between VMBase(vm) and VMLimit(vm), or if base and limit are not multiples of VMPageSize(vm). Return ResOK if successful, ResMEMORY if not.
void VMUnmap(VM vm, Addr base, Addr limit)
.if.unmap: Unmap the range of addresses from base (inclusive) to limit (exclusive). The conditions are the same as for VMMap().
Addr VMBase(VM vm)
.if.base: Return the base address of the VM (the lowest address in the VM that is a multiple of the grain size).
Addr VMLimit(VM vm)
.if.limit: Return the limit address of the VM (the limit of the last grain that is wholly inside the VM).
Size VMReserved(VM vm)
.if.reserved: Return the total number of bytes of address space reserved by the VM. (This may include addresses that are not available for mapping because of the requirement for VMBase(vm) and VMLimit(vm) to be multiples of the grain size.)
Size VMMapped(VM vm)
.if.mapped: Return the total number of bytes of address space currently mapped into memory by the VM.
Notes
.testing: It is important to test that a VM implementation will work in extreme cases.
.testing.large: It must be able to reserve a large address space. Clients will want multi-GB spaces, more than that OSs will allow. If they ask for too much, mps_arena_create() (and hence VMInit()) must fail in a predictable way.
.testing.larger: It must be possible to allocate in a large space; sometimes commiting will fail, because there's not enough space to replace the "reserve" mapping. See request.epcore.160201 for details.
.testing.lots: It must be possible to have lots of mappings. The OS must either combine adjacent mappings or have lots of space in the kernel tables. See request.epcore.160117 for ideas on how to test this.
Document History
Copyright and License
Copyright © 2013-2014 Ravenbrook Limited. All rights reserved. <http://www.ravenbrook.com/>. This is an open source license. Contact Ravenbrook for commercial licensing options.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
- Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
- Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
- Redistributions in any form must be accompanied by information on how to obtain complete source code for this software and any accompanying software that uses this software. The source code must either be included in the distribution or be available for no more than the cost of distribution plus a nominal fee, and must be freely redistributable under reasonable conditions. For an executable file, complete source code means the source code for all modules it contains. It does not include source code for modules or files that typically accompany the major components of the operating system on which the executable file runs.
This software is provided by the copyright holders and contributors "as is" and any express or implied warranties, including, but not limited to, the implied warranties of merchantability, fitness for a particular purpose, or non-infringement, are disclaimed. In no event shall the copyright holders and contributors be liable for any direct, indirect, incidental, special, exemplary, or consequential damages (including, but not limited to, procurement of substitute goods or services; loss of use, data, or profits; or business interruption) however caused and on any theory of liability, whether in contract, strict liability, or tort (including negligence or otherwise) arising in any way out of the use of this software, even if advised of the possibility of such damage.