-- VMMgr>VMMControl.mesa  (last edited by Levin on August 3, 1982 4:09 pm)

-- Packing Considerations:  IsUtilityPilot[], IsDiagnosticPilot must be initially Swapped In.

DIRECTORY
   CachedRegion USING [Apply, Desc, Outcome, pageTop],
   CachedSpace USING [Desc, Get, Handle, Level],
   Environment USING [maxPagesInMDS, PageCount, PageNumber],
   File USING [nullID],
   Hierarchy USING [Insert],
   MapLog USING [WriteLog],
   Projection USING [Split, TranslateLevel],
   SimpleSpace USING [DisableInitialization],
   StoragePrograms USING [pageMDS],
   STree USING [STreeImpl],
   VM USING [Interval],
   VMMPrograms;

VMMControl: PROGRAM 
   IMPORTS CachedRegion, CachedSpace, Hierarchy, MapLog, Projection, SimpleSpace, StoragePrograms, STreeHier: STree, STreeProj: STree, VMMPrograms
   EXPORTS StoragePrograms
   SHARES File =

BEGIN

mds: CachedSpace.Handle = [level: FIRST[CachedSpace.Level]+1, page: StoragePrograms.pageMDS];

levelVM: CachedSpace.Level ← FIRST[CachedSpace.Level];
levelMDS: CachedSpace.Level ← levelVM + 1;  -- MDS is child of VM.

Bug: PRIVATE --PROGRAMMING-- ERROR [type: BugType] = CODE;
BugType: TYPE = {bug0, bug1};

InitializeVMMgr: PUBLIC PROCEDURE [countVM: Environment.PageCount, pMapLogDesc: LONG POINTER] =
   BEGIN
   START VMMPrograms.STLeafImpl[];
   START VMMPrograms.MapLogImpl[pMapLogDesc: pMapLogDesc];
   START STreeHier.STreeImpl[countVM: countVM, kind: hierarchy];
   START STreeProj.STreeImpl[countVM: countVM, kind: projection];
   START VMMPrograms.HierarchyImpl;
   START VMMPrograms.ProjectionImpl[countVM: countVM];
   FillHierarchyAndProjection[];
   VMMPrograms.InitializeSpace[countVM: countVM, handleMDS: mds];
   END;

IsDiagnosticPilot, IsUtilityPilot: PUBLIC PROCEDURE RETURNS [BOOLEAN] =
   BEGIN  RETURN[FALSE]  END;

-- See comments with DescribeSpace in StoragePrograms.mesa!
-- This procedure takes the space and region cache entries created by PilotControl.DescribeInitiallyResidentSpaces and SimpleSpaceImpl.DescribeSpace, and makes corresponding entries in the Hierarchy and Projection databases.
-- When the Hierarchy and Projection are created, they each have a single entry representing all of VM as a single unmapped space.  As new spaces and regions are created below, they inherit the properties of their parents i.e. they are unmapped.  In the VMMgr however, the algorithm for accessing a Hierarchy or Projection entry is: look in the cache;  if the entry is there, it is the truth;  if it is not there, the entry in the Hierarchy or Projection is the truth.  The code below demands that there already be an entry in the space cache for every unitary or family space (i.e. the ones that are (themselves) mapped) and an entry in the region cache for every swapUnit of every unitary or family space.  Therefore, we do not need to make the entries in the Hierarchy and Projection contain the proper attributes. 
FillHierarchyAndProjection: PROCEDURE =
   BEGIN
   pageRegion: Environment.PageNumber;
   outcome: CachedRegion.Outcome;
   region: CachedRegion.Desc;
   space: CachedSpace.Desc;  -- the desc of the current unitary or family space.
   SimpleSpace.DisableInitialization[];  -- no more simpleSpaces may be created.
   CreateSpace[mds.level, VM.Interval[mds.page, Environment.maxPagesInMDS], NIL];  -- create MDS space.
   space.interval.count ← 0;  -- there is no current space.  (No page is contained in our current space.)
   pageRegion ← FIRST[Environment.PageNumber];  -- From the beginning of VM..
   WHILE pageRegion < CachedRegion.pageTop DO --until all regions in cache processed--
      [outcome, pageRegion] ← CachedRegion.Apply[  -- What's next in the region cache?
	 pageRegion,
	 [ifMissing: report, ifCheckedOut: wait, afterForking: , vp: get[andResetDDirty: FALSE, pDescResult: @region]]];
      SELECT outcome.kind FROM
	 ok =>  -- we got the descriptor of the next region in the cache --
	   BEGIN
	   IF ~(region.interval.page IN [space.interval.page .. space.interval.page + space.interval.count)) THEN
	      BEGIN  -- this region is in a new primary space --
	      CachedSpace.Get[@space, [level: region.levelMapped, page: region.interval.page]];  -- get the desc of the unitary or family space which contins this region.
	      IF region.interval.page ~= space.interval.page THEN ERROR Bug[bug0];  -- primary space not completely tiled with regions.
	      CreateSpace[space.level, space.interval, IF space.state=mapped THEN @space ELSE NIL];  -- put unitary or family space into VMMgr database.
	      END;
	   IF region.interval ~= space.interval THEN CreateSpace[region.level, region.interval, NIL];  -- put swap unit of family space into VMMgr database.
	   END;
	 regionDMissing => NULL;  -- this region is unused.  Proceed to the next region boundary.
	 ENDCASE => ERROR Bug[bug1];
      ENDLOOP;
   END;

CreateSpace: PROCEDURE [level: CachedSpace.Level, interval: VM.Interval, pSpaceD: POINTER TO CachedSpace.Desc]=
   -- Creates Hierarchy & Projection entries for one space;  dirty cache entries should already exist if the space or region is mapped.
   BEGIN OPEN i: interval;
   Hierarchy.Insert[ CachedSpace.Handle[level, i.page], i.count];
   Projection.Split[i.page];  Projection.Split[i.page+i.count];  Projection.TranslateLevel[pageMember: i.page, delta: 1];
   IF pSpaceD~=NIL AND pSpaceD.window.file.fID~=File.nullID THEN MapLog.WriteLog[interval, pSpaceD];
   END;

END.


LOG

Time: June 20, 1978  5:58 PM	By: McJones	Action: Created file
Time: June 22, 1978  6:12 PM	By: McJones	Action: Added projection(/hierarchy?) init
Time: June 23, 1978  1:23 PM	By: McJones	Action: Added countVM parameter to ProjectionImpl, SpaceImpl
Time: July 10, 1978  10:18 AM	By: McJones	Action: Added creation of first64K
Time: July 18, 1978  10:59 AM	By: McJones	Action: Added countReal, ...
Time: August 2, 1978  10:18 AM	By: McJones	Action: Added MapLogImpl
Time: August 7, 1978  8:11 PM	By: Purcell	Action: Call SimpleSpace.DisableInitialization;  new Hierarchy/Projection filling logic
Time: August 29, 1978  4:14 PM	By: McJones	Action: (De)typed pMapLogDesc
Time: September 6, 1978  10:18 AM	By: McJones	Action: Fixed test for first subspace of parent
Time: September 15, 1978  5:20 PM	By: McJones	Action: Added map logging
Time: July 11, 1979  3:28 PM	By: McJones	Action: Nonparent mapped space handled incorrectly
Time: July 31, 1979  2:03 PM	By: McJones	Action: Changed MapLog to include writeProtected
Time: August 29, 1979  3:40 PM	By: Knutsen	Action: Changed VMMControl: PROGRAM to InitVMMgr: PROCEDURE.
Time: September 7, 1979  8:50 AM	By: McJones	Action: Added V key stop
Time: November 26, 1979  3:53 PM	By: Knutsen	Action: Deleted volumeData parameter of InitSpace.
Time: April 10, 1980  4:35 PM	By: Knutsen	Action: Changes for nonzero MDS. Changed VMMControl: PROGRAM to InitVMMgr: PROCEDURE AGAIN!
Time: May 29, 1980  3:18 PM	By: Knutsen	Action: Moved Key Stop V to STLeafImpl.
Time: July 1, 1980  10:44 AM	By: Knutsen	Action: Minor cleanups.
Time: August 3, 1982 4:09 pm	By: Levin	Action: Correct all occurrences of ~IN.