.. mode: -*- rst -*-

Virtual mapping
===============

:Tag: design.mps.vm
:Author: richard
:Date: 1998-05-11
:Status: incomplete design
:Revision: $Id: //info.ravenbrook.com/project/mps/version/1.113/design/vm.txt#1 $
:Copyright: See `Copyright and License`_.
:Index terms:    pair: virtual mapping; design


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 ``VMCreate()`` reserves (from the
operating-system) a single contiguous range of addresses, and returns
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 / 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 ``VMCreate()`` was called.


Interface
---------

``Res VMCreate(VM *VMReturn, Size size)``

_`.if.create`: ``VMCreate()`` is responsible both for allocating a
``VMStruct`` and for reserving an amount of virtual address space. A
VM is created and a pointer to it is returned in the return parameter
``VMReturn``. This VM has at least size bytes of virtual memory
reserved. If there's not enough space to allocate the VM,
``ResMEMORY`` is returned. 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 using ``VMMap()``
and ``VMUnmap()``.

``void VMDestroy(VM vm)``

_`.if.destroy`: A VM is destroyed by calling ``VMDestroy()``. Any
address space that was mapped through this VM is unmapped.

.. note::

    Lots of interfaces are missing here.


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
``VMCreate()``) 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.

.. _request.epcore.160201: https://info.ravenbrook.com/project/mps/import/2001-11-05/mmprevol/request/epcore/160201

_`.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.

.. _request.epcore.160117: https://info.ravenbrook.com/project/mps/import/2001-11-05/mmprevol/request/epcore/160117


Document History
----------------

- 1998-05-11 RB_ Incomplete design.

- 2002-06-07 RB_ Converted from MMInfo database design document.

- 2013-05-23 GDR_ Converted to reStructuredText.

.. _RB: http://www.ravenbrook.com/consultants/rb/
.. _GDR: http://www.ravenbrook.com/consultants/gdr/


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.**
