.. index:: pair: shield; design .. _design-shield: Shield ====== .. mps:prefix:: design.mps.shield Introduction ------------ :mps:tag:`intro` This document contains a guide to the MPS Shield. There is no historical initial design, but in its place there are some early ideas and discussions: see :mps:ref:`.ideas`. :mps:tag:`readership` Any MPS developer. Not confidential. Overview -------- :mps:tag:`over` For incremental collection, we need *separate control* of collector access and mutator (client) access to memory. The collector must be able to incrementally scan objects, without the mutator being able to see them yet. Unfortunately common OSs do not support different access levels (protection maps) for different parts of the same process. The MPS Shield is an abstraction that does extra work to overcome this limitation, and give the rest of the MPS the illusion that we can control collector and mutator access separately. Control of mutator access ------------------------- The MPS uses :c:func:`ShieldRaise()` and :c:func:`ShieldLower()` to forbid or permit the mutator access to object memory (that is, memory allocated by MPS). .. c:function:: void ShieldRaise(Arena arena, Seg seg, AccessSet mode) Prevent the mutator accessing the memory in the specified mode (``AccessREAD``, ``AccessWRITE``, or both). .. c:function:: void ShieldLower(Arena arena, Seg seg, AccessSet mode) Allow the mutator to access the memory in the specified mode (``AccessREAD``, ``AccessWRITE``, or both). If the mutator attempts an access that hits a shield, the MPS gets a barrier hit (in the form of a fault, interrupt, exception), quickly does some necessary work, and then makes the access succeed. Some objects (for example registers) cannot be hardware protected: the only way to prevent mutator access to them is to halt all mutator threads. The MPS uses :c:func:`ShieldSuspend()` and :c:func:`ShieldResume()` to do this. .. c:function:: void ShieldSuspend(Arena arena) Stop all registered mutator threads. .. c:function:: void ShieldResume(Arena arena) Resume all registered mutator threads. Control of collector access --------------------------- When the collector wants to access object memory (that is, memory allocated by MPS), it must first call :c:func:`ShieldEnter()`, then wrap any accesses with a :c:func:`ShieldExpose()` and :c:func:`ShieldCover()` pair, and finally call :c:func:`ShieldLeave()`. :c:func:`ShieldEnter()` and :c:func:`ShieldLeave()` are called by :c:func:`ArenaEnter()` and :c:func:`ArenaLeave()` (approximately) -- so the shield is always entered when we are within MPS code (approximately). :c:func:`ShieldExpose()` might for example be called around: - format-scan (when scanning); - format-skip (when marking grains in a non-moving fix); - format-isMoved and :c:func:`AddrCopy()` (during a copying fix); - format-pad (during reclaim). Note that there is no need to call :c:func:`ShieldExpose()` when accessing pool management memory such as bit tables. This is not object memory, is never (legally) accessed by the mutator, and so is never shielded. On common operating systems, the only way to allow collector access is to allow access from the whole process, including the mutator. So if the Shield is asked to allow collector access but deny mutator access, it will halt all mutator threads to prevent any mutator access. The Shield performs suspension and restart; normal collector code does not need to worry about it. Collector code can make multiple sequential, overlapping, or nested calls to :c:func:`ShieldExpose()` on the same segment, as long as each is balanced by a corresponding :c:func:`ShieldCover()` before :c:func:`ShieldLeave()` is called). A usage count is maintained on each segment in ``seg->depth``: a positive "depth" means a positive number of outstanding *reasons* why the segment must be exposed to the collector. When the usage count reaches zero, there is no longer any reason the segment should be unprotected, and the Shield could re-instate hardware protection. However, as a performance-improving hysteresis, the Shield defers re-protection, maintaining a cache of the last ``ShieldCacheSIZE`` times a segment no longer had a reason to be collector-accessible. Presence in the cache counts as a reason: segments in the cache have ``seg->depth`` increased by one. As segments get pushed out of the cache, or at :c:func:`ShieldLeave()`, this artificial reason is decremented from ``seg->depth``, and (if ``seg->depth`` is now zero) the deferred reinstatement of hardware protection happens. So whenever hardware protection is temporarily removed to allow collector access, there is a *nurse* that will ensure this protection is re-established: the nurse is either the balancing :c:func:`ShieldCover()` call in collector code, or an entry in the shield cache. .. note:: 1. Why is there a fixed-size cache? This is not the simple approach! All we need is a chain of segs that might need their hardware protection to be sync'd with their shield mode. Head in the shield, and one pointer in each seg struct. I guess we try hard to avoid bloating :c:type:`SegStruct` (to maintain residency in the processor cache). But is 16 the right size? A cache-miss wastes two kernel calls. 2. I don't like the cache code. For example, why does :c:func:`ShieldFlush()` break out early if ``arena->shDepth`` is 0? This should never happen until the cache is completely flushed, that is, we have reached ``shCacheLimit``. Why does :c:func:`ShieldFlush()` not reset ``shCacheLimit``? Why does :c:func:`flush()` silently accept :c:macro:`NULL` cache entries? 3. Why is ``seg->depth`` never checked for overflow? It is only a 4-bit-wide bit field, currently. Richard Kistruck, 2006-12-19. Initial ideas ------------- :mps:tag:`ideas` There never was an initial design document, but [RB_1995-11-29]_ and [RB_1995-11-30]_ contain some initial ideas. References ---------- .. [RB_1995-11-29] Richard Brooksby. Harlequin. 1995-11-29. "`Shield protocol for barriers `__". .. [RB_1995-11-30] Richard Brooksby. Harlequin. 1995-11-30. "`Exegesis of Incremental Tracing `__".