home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / oper_sys / emerald / emrldsys.lha / Kernel / FileLoad / loadCode.c next >
Encoding:
C/C++ Source or Header  |  1990-08-17  |  103.5 KB  |  3,072 lines

  1.  
  2. /*  C O P Y R I G H T   N O T I C E :                                     */
  3. /* Copyright 1986 Eric Jul and Norm Hutchinson.     May not be used for any  */
  4. /* purpose without written permission from the authors.              */
  5.  
  6. /* This source module contains the stuff to dynamically load an object    */
  7. /* from a Ultrix file or from another kernel.                             */
  8.  
  9. #include <stdio.h>
  10.  
  11. #include "Kernel/h/system.h"
  12. #include "Kernel/h/macros.h"
  13. #include "Kernel/h/assert.h"
  14. #include <setjmp.h>
  15. #include <sys/file.h>
  16. #include <errno.h>
  17. #include <sys/time.h>
  18. #include <sys/stat.h>
  19.  
  20. #include "Kernel/h/errMsgs.h"
  21. #include "Kernel/h/hotsTypes.h"
  22. #include "Kernel/h/ecTypes.h"
  23. #include "Kernel/h/emTypes.h"
  24. #include "Kernel/h/kmdTypes.h"
  25. #include "Kernel/h/lmTypes.h"
  26. #include "Kernel/h/kEvents.h"
  27. #include "Kernel/h/emkDefs.h"
  28. #include "Kernel/h/lmCodes.h"
  29. #include "Kernel/h/emCodes.h"
  30. #include "Kernel/h/map.h"
  31. #include "Kernel/h/map96.h"
  32. #include "Kernel/h/sigio.h"
  33. #include "Kernel/h/emeraldTypes.h"
  34. #include "Kernel/h/consts.h"
  35. #include "Kernel/h/builtins.h"
  36. #include "Kernel/h/utils.h"
  37.  
  38. extern ODTag            stdCodeODTag, stdDotoODTag, stdAbConTag;
  39. extern DotoFilePtr      readDotoFile();
  40. extern void             translateDoto(), retranslate();
  41. extern SSPtr            NewProcess(), preemptRunning();
  42. extern void             unavail(), fail(), schedule(), StartLocate();
  43. extern ODP              OTLookup();
  44. extern void             StartProcessAtAddr(), OTInsert();
  45. extern OID              getNextOID();
  46. extern GODP             AllocateWithOID(), MakeObject();
  47. extern char            *PPSSPlace(), *PPPOID(), *PPGOID(), *PPFindLineNo(),
  48.                *PPOID(), *PPCOID(), *PPCodePtr();
  49. extern CodeODP          CreateCodeODEntry();
  50. extern GODP             CreateGODEntry();
  51. extern char             EMDIR[];
  52. extern EmLocation       thisNodeLocation;
  53. extern GODP             kernelStdInStream;
  54. extern GODP             kernelStdOutStream;
  55. extern void             DoCallBack();
  56. extern void             UnblockInitially();
  57. extern void             MakeStreamsFromSock();
  58.  
  59.  
  60. /* Global variables */
  61. Map                     CodeLoadMap;
  62. Map                     CompilerLoadMap;
  63. Map                     CheatingLoadMap;
  64. Map                     RemoteLoadMap;
  65.  
  66. /*
  67.  * UnknownATAbConMap maps ATOIDs into Sets.
  68.  * Every ATOID for which the AT is not resident (i.e., the CodePtr is NULL)
  69.  * there is a set of CTOIDs.  For every one of these CTOIDs there
  70.  * is an abcon with the ATOID and the CTOID.
  71.  * This AbCon, when it was created, had to be faked since the AT was not
  72.  * present (the CT might not be present either).
  73.  * When a previously non-resident AT arrives, this Map must be checked
  74.  * so that the faked AbCons can be fixed.
  75.  * After fixing the AbCons, the entry should be removed.
  76.  * If the CT is still missing then do not worry.  The abcon will
  77.  * get fixed when the CT arrives (the CT has a AbConSetPtr for this
  78.  * purpose).
  79.  * 
  80.  */
  81.  
  82. Map                     UnknownATAbConMap;
  83.  
  84. /*
  85.  * AbConMap96 is a map of (AT, restrictAT, CT) -> AbConPtr.
  86.  */
  87.  
  88. Map96                   AbConMap96;
  89. #define INITIALABCONMAP96SIZE 2048
  90. /*
  91.  * vFLRemoteLoadDeamonInterval is the time that the Remote Load Deamon
  92.  * waits before sending out remote load requests.
  93.  * The idea is to batch the requests together, if possible.
  94.  * In the common case, EnsurePresenceOfRequired types generates multiple
  95.  * request and these will all be satified from the same remote node.
  96.  */
  97.  
  98. Boolean                 RemoteLoadDeamonRunning = FALSE;
  99. #define                 REMOTELOADDEAMONINTERVAL 20000 /* microseconds */
  100. int                     vFLRemoteLoadDeamonInterval=REMOTELOADDEAMONINTERVAL;
  101.  
  102. #define                 MAXSOCK 32
  103. CompilerLoadReqPtr      CReq[MAXSOCK];
  104.  
  105. /* Local variables */
  106.  
  107. jmp_buf            errorExitPoint;
  108. int                     CompilerSock;
  109. struct sockaddr_in
  110.         compilerSocket = {AF_INET};       /* Socket for compiler requests */
  111.  
  112.  
  113. #define MAXDOTOFILES 1000
  114.  
  115. /* Forward */ void ReadCompilerRequest();
  116. /* Forward */ Boolean LoadRequest();
  117. /* Forward */ void CloseInStream(), CloseOutStream();
  118. /* Forward */ DotoFilePtr LoadCheatingFromCompiler();
  119. /* Forward */ Boolean EnsurePresenceOfRequiredTypes();
  120. /* Forward */ void CodeLoadCallBack();
  121.  
  122.  
  123. /**************************************************************************/
  124. /*      FindEntryPointForOp                          */
  125. /**************************************************************************/
  126.  
  127. CodeAddr FindEntryPointForOp(fCodePtr, fOpOID)
  128. CodePtr                         fCodePtr;
  129. OID                             fOpOID;
  130. {
  131.     register OpVectorPtr        ctOpVecPtr;
  132.     register OpVectorEntryPtr   entry;
  133.     register int                opCount, i;
  134.     /* Implementation note: binary search should be used */
  135.     ctOpVecPtr = 
  136.     (OpVectorPtr) addOffset(fCodePtr, fCodePtr->opVecOffset);
  137.     opCount = ctOpVecPtr->numEntries;
  138.     entry = &(ctOpVecPtr->entry[0]);
  139.     for (i = 0 ; i < opCount; entry++, i++)
  140.     if (entry->operationNameOID == fOpOID) {
  141.     KMDTrace("Invoke", 4, "OpOID 0x%08.8x -> (0x%05x) #%d in %s\n",
  142.         fOpOID, addOffset(fCodePtr, entry->offset), i,
  143.         PPCodePtr(fCodePtr));
  144.     return (SSAddr) addOffset(fCodePtr, entry->offset);
  145.     }
  146.     /* Message not understood ???? */
  147.     ErrMsg("OpOID 0x%08.8x not found in %s\n", fOpOID, PPCodePtr(fCodePtr));
  148.     assert (FALSE);
  149.     abort();
  150.     return (SSAddr) EMNIL;
  151. }
  152.  
  153. /**************************************************************************/
  154. /*      FindReturnByMoveMask                          */
  155. /**************************************************************************/
  156.  
  157. MoveMask FindReturnByMoveMask(fCodePtr, fEntryPoint)
  158. CodePtr                         fCodePtr;
  159. CodeAddr            fEntryPoint;
  160. {
  161.     register TemplateEntryPtr    t;
  162.     register integer        i, j;
  163.     register MoveMask        theMask;
  164.     TemplatePtr                 theTemplate;
  165.     Offset            entryPointOffset, tOffset;
  166.     IPMapPtr            map;
  167.     integer            moveIt;
  168.     
  169.     entryPointOffset        = (Offset) byteOffset(fCodePtr, fEntryPoint);
  170.     map    = (IPMapPtr) addOffset(fCodePtr, fCodePtr->templateMapOffset);
  171.     tOffset = IPMapLookup(map, entryPointOffset);
  172.     assert( 0 != tOffset );
  173.     theTemplate = (TemplatePtr) addOffset(fCodePtr, tOffset);
  174.     
  175.     /* Finally, we have digged out the template */
  176.     t         = &theTemplate->entry[0];
  177.     theMask    = 0;
  178.     for (i = theTemplate->B.numEntries; i > 0; i--) {
  179.     switch (t->TE.SS.Format) {
  180.     case ShortStaticF: {
  181.         KMDTrace("Invoke", 5,
  182.         "\tShortStaticF\t(%s) %s\tcount =%4d\n", 
  183.         PPBrand(t->TE.SS.theBrand),
  184.         PPParamInfo(t->TE.SS.paramInfo),
  185.         t->TE.SS.count);
  186.  
  187.         if (
  188.             (t->TE.SS.paramInfo == IsNotParam) ||
  189.         (t->TE.SS.paramInfo == IsArgument)
  190.         ) {    
  191.             i = 0; /* to stop loop (hack, but better than goto) */
  192.         break;
  193.         }
  194.  
  195.         /* moveIt is true iff the param is Move By Result */
  196.         moveIt = (t->TE.SS.paramInfo == IsMoveResult);
  197.         for (j = t->TE.SS.count; --j >= 0;) {
  198.         theMask = (theMask << 1) | moveIt;
  199.         }
  200.         t = (TemplateEntryPtr) addOffset(t, sizeof(ShortStatic));
  201.         break;
  202.         }
  203.     case RegisterF: {
  204.         t = (TemplateEntryPtr) addOffset(t, sizeof(t->TE.R));
  205.         break;
  206.     } /* case RegisterF */
  207.     
  208.     default: {
  209.         i = 0; /* to stop loop, hack, but better than goto */         
  210.         break;
  211.     } /* default action */
  212.  
  213.     } /* switch on t->TE.SS.Format */
  214.     } /* for (i = ...) */
  215.     KMDTrace("Invoke", 4, "MoveByResult Mask 0x%08x\n", theMask);
  216.     
  217.     return theMask;
  218. }
  219.  
  220. /************************************************************************/
  221. AbConPtr MakeAbCon(fATODP, fCTODP)
  222. ATODP           fATODP;
  223. CodeODP         fCTODP;
  224. {
  225.     int                 opCTCount, opATCount, i, j;
  226.     register AbConPtr   newAbConPtr;
  227.     OpVectorPtr         ctOpVecPtr, atOpVecPtr;
  228.     CodePtr             theCTCode, theATCode;
  229.    
  230.     /* At this point, we have a real AB and a real CON with dataPtr != NULL */
  231.     /* (Note, they might actually be ABAB or CONCON, but we have their code)*/
  232.  
  233.     theCTCode = fCTODP->dataPtr;
  234.     if (theCTCode->opVecOffset == 0) {
  235.     opCTCount = 0;
  236.     } else {
  237.     ctOpVecPtr = 
  238.         (OpVectorPtr) addOffset(theCTCode, theCTCode->opVecOffset);
  239.     opCTCount = ctOpVecPtr->numEntries;
  240.     }
  241.  
  242.     theATCode = fATODP->dataPtr;
  243.     if (theATCode->opVecOffset == 0) {
  244.     opATCount = 0;
  245.     } else {
  246.     atOpVecPtr = 
  247.         (OpVectorPtr) addOffset(theATCode, theATCode->opVecOffset);
  248.     opATCount = atOpVecPtr->numEntries;
  249.     }
  250.     newAbConPtr                 = NEWAbCon(opATCount);
  251.     KMDTrace("AbCon", 3, "Make real AbCon(%s, %s) @ 0x%06x\n",
  252.     PPCOID(fATODP->ownOID), PPCOID(fCTODP->ownOID), newAbConPtr);
  253.     KMDTrace("AbCon", 4, "opctCount %d, opatCount %d\n", opCTCount,
  254.     opATCount);
  255.     newAbConPtr->tag         = stdAbConTag;
  256.     newAbConPtr->numEntries     = opATCount;
  257.     newAbConPtr->tag.hasNoPointer = theCTCode->tag.hasNoPointer;
  258.     newAbConPtr->tag.allInstancesAreLocal = theCTCode->tag.allInstancesAreLocal;
  259.     newAbConPtr->tag.frozen     = ~theCTCode->tag.allInstancesAreLocal;
  260.     KMDTrace("AbCon", 5,
  261.     "  frozen: %d   allInstancesAreLocal %d   hasNoPointer %d\n",
  262.     newAbConPtr->tag.frozen, newAbConPtr->tag.allInstancesAreLocal,
  263.     newAbConPtr->tag.hasNoPointer);
  264.     newAbConPtr->ATOID          = fATODP->ownOID;
  265.     newAbConPtr->aPtr           = theATCode;
  266.     newAbConPtr->CodeOID        = fCTODP->ownOID;
  267.     newAbConPtr->cPtr           = theCTCode;
  268.     for (i=0, j=0; (i<opATCount) && (j<opCTCount); j++) {
  269.     if (ctOpVecPtr->entry[j].operationNameOID !=
  270.         atOpVecPtr->entry[i].operationNameOID) {
  271.         KMDTrace("AbCon", 5, "CT op #%d [0x%08x] skipped\n", j,
  272.         ctOpVecPtr->entry[j].operationNameOID);
  273.         continue;
  274.     }
  275.     newAbConPtr->opVector[i].opAddress = 
  276.         (CodeAddr) addOffset(theCTCode, ctOpVecPtr->entry[j].offset);
  277.     newAbConPtr->opVector[i].argumentCount =
  278.         ctOpVecPtr->entry[j].argumentCount;
  279.     newAbConPtr->opVector[i].resultCount   =
  280.         ctOpVecPtr->entry[j].resultCount;
  281.     newAbConPtr->opVector[i].operationNameOID =
  282.         ctOpVecPtr->entry[j].operationNameOID;
  283.     KMDTrace("AbCon", 5, "Op #%d [0x%08x] %d arg%s %d result%s\n",
  284.         i, newAbConPtr->opVector[i].operationNameOID,
  285.         newAbConPtr->opVector[i].argumentCount,
  286.         mPLURAL(newAbConPtr->opVector[i].argumentCount),
  287.         newAbConPtr->opVector[i].resultCount,
  288.         mPLURAL(newAbConPtr->opVector[i].resultCount));
  289.     i++;
  290.     };
  291.     if (i < opATCount) {
  292.     KMDTrace("AbCon", 1, " view mismatch -- contact norm@arizona.edu\n");
  293.     ErrMsg(
  294.             "View mismatch, CT does not conform to AT, see norm@arizona.edu\n");
  295.     ErrMsg("AT 0x%08x CT 0x%08x\n", newAbConPtr->ATOID,
  296.         newAbConPtr->CodeOID);
  297.     assert (FALSE);
  298.     }
  299.     return newAbConPtr;
  300. }
  301.  
  302. /**********************************************************************/
  303. /*      MakeAbAb                                                      */
  304. /**********************************************************************/
  305.  
  306. AbConPtr MakeAbAb(fATODP, fCTODP)
  307. ATODP           fATODP;
  308. CodeODP         fCTODP;
  309. /* Build an AbCon using the AT only */
  310. {
  311.     int                 opATCount, i;
  312.     register AbConPtr   newAbConPtr;
  313.     OpVectorPtr         atOpVecPtr;
  314.     CodePtr             theATCode;
  315.  
  316.     theATCode = fATODP->dataPtr;
  317.     if (theATCode->opVecOffset == 0) {
  318.     opATCount = 0;
  319.     } else {
  320.     atOpVecPtr = 
  321.         (OpVectorPtr) addOffset(theATCode, theATCode->opVecOffset);
  322.     opATCount = atOpVecPtr->numEntries;
  323.     }
  324.     newAbConPtr                 = NEWAbCon(opATCount);
  325.     KMDTrace("AbCon", 3, "MakeAbAb(%s, %s) @ 0x%06x\n",
  326.         PPCOID(fATODP->ownOID), PPCOID(fCTODP->ownOID), newAbConPtr);
  327.     KMDTrace("AbCon", 4, "Operation count %d\n", opATCount);
  328.     newAbConPtr->tag             = stdAbConTag;
  329.     newAbConPtr->numEntries     = opATCount;
  330.     newAbConPtr->tag.hasNoPointer = theATCode->tag.hasNoPointer;
  331.     newAbConPtr->tag.allInstancesAreLocal =
  332.                 theATCode->tag.allInstancesAreLocal;
  333.     newAbConPtr->tag.frozen = ~theATCode->tag.allInstancesAreLocal;
  334.     KMDTrace("AbCon", 5,
  335.     "  frozen: %d   allInstancesAreLocal %d   hasNoPointer %d\n",
  336.     newAbConPtr->tag.frozen, newAbConPtr->tag.allInstancesAreLocal,
  337.     newAbConPtr->tag.hasNoPointer);
  338.     newAbConPtr->ATOID          = fATODP->ownOID;
  339.     newAbConPtr->aPtr           = theATCode;
  340.     newAbConPtr->CodeOID        = fCTODP->ownOID;
  341.     newAbConPtr->cPtr           = (CodePtr) NULL;
  342.  
  343.     for (i=0; i<opATCount; i++) {
  344.     newAbConPtr->opVector[i].opAddress = (CodeAddr) NULL;
  345.     newAbConPtr->opVector[i].argumentCount =
  346.         atOpVecPtr->entry[i].argumentCount;
  347.     newAbConPtr->opVector[i].resultCount   =
  348.         atOpVecPtr->entry[i].resultCount;
  349.     newAbConPtr->opVector[i].operationNameOID =
  350.         atOpVecPtr->entry[i].operationNameOID;
  351.     KMDTrace("AbCon", 5, "Op #%d [0x%08x] %d arg%s %d result%s\n",
  352.         i, newAbConPtr->opVector[i].operationNameOID,
  353.         newAbConPtr->opVector[i].argumentCount,
  354.         mPLURAL(newAbConPtr->opVector[i].argumentCount),
  355.         newAbConPtr->opVector[i].resultCount,
  356.         mPLURAL(newAbConPtr->opVector[i].resultCount));
  357.     };
  358.     return newAbConPtr;
  359. }
  360.  
  361. /**********************************************************************/
  362. /*      MakeConCon                                                    */
  363. /**********************************************************************/
  364.  
  365. AbConPtr MakeConCon(fATODP, fCTODP)
  366. ATODP           fATODP;
  367. CodeODP         fCTODP;
  368. {
  369.     int                 opCTCount, i;
  370.     register AbConPtr   newAbConPtr;
  371.     OpVectorPtr         ctOpVecPtr;
  372.     CodePtr             theCTCode;
  373.  
  374.     theCTCode = fCTODP->dataPtr;
  375.     if (theCTCode->opVecOffset == 0) {
  376.     opCTCount = 0;
  377.     } else {
  378.     ctOpVecPtr = 
  379.         (OpVectorPtr) addOffset(theCTCode, theCTCode->opVecOffset);
  380.     opCTCount = ctOpVecPtr->numEntries;
  381.     }
  382.  
  383.     KMDTrace("AbCon", 3, "MakeConCon(0x%05x, 0x%05x)\n", fATODP->ownOID,
  384.     fCTODP->ownOID);
  385.     KMDTrace("AbCon", 4, "ConCon %d operation%s\n", opCTCount,
  386.     mPLURAL(opCTCount));
  387.     newAbConPtr                 = NEWAbCon(opCTCount);
  388.     newAbConPtr->tag         = stdAbConTag;
  389.     newAbConPtr->numEntries     = opCTCount;
  390.     newAbConPtr->tag.hasNoPointer = theCTCode->tag.hasNoPointer;
  391.     newAbConPtr->tag.allInstancesAreLocal =
  392.                 theCTCode->tag.allInstancesAreLocal;
  393.     newAbConPtr->tag.frozen = ~theCTCode->tag.allInstancesAreLocal;
  394.     KMDTrace("AbCon", 5,
  395.     "  frozen: %d   allInstancesAreLocal %d   hasNoPointer %d\n",
  396.     newAbConPtr->tag.frozen, newAbConPtr->tag.allInstancesAreLocal,
  397.     newAbConPtr->tag.hasNoPointer);
  398.     newAbConPtr->ATOID          = fATODP->ownOID;
  399.     newAbConPtr->aPtr           = (CodePtr) NULL;
  400.     newAbConPtr->CodeOID        = fCTODP->ownOID;
  401.     newAbConPtr->cPtr           = theCTCode;
  402.     
  403.     for (i=0; i<opCTCount; i++) {
  404.     newAbConPtr->opVector[i].opAddress =
  405.         (CodeAddr) addOffset(theCTCode, ctOpVecPtr->entry[i].offset);
  406.     newAbConPtr->opVector[i].argumentCount =
  407.         ctOpVecPtr->entry[i].argumentCount;
  408.     newAbConPtr->opVector[i].resultCount   =
  409.         ctOpVecPtr->entry[i].resultCount;
  410.     newAbConPtr->opVector[i].operationNameOID =
  411.         ctOpVecPtr->entry[i].operationNameOID;
  412.     KMDTrace("AbCon", 5, "Op #%d [0x%08x] %d arg%s %d result%s\n",
  413.         i, newAbConPtr->opVector[i].operationNameOID,
  414.         newAbConPtr->opVector[i].argumentCount,
  415.         mPLURAL(newAbConPtr->opVector[i].argumentCount),
  416.         newAbConPtr->opVector[i].resultCount,
  417.         mPLURAL(newAbConPtr->opVector[i].resultCount));
  418.     };
  419.     return newAbConPtr;
  420. }
  421.  
  422. /**********************************************************************/
  423. /*          MakeFakeAbCon                                             */
  424. /**********************************************************************/
  425.  
  426. AbConPtr MakeFakeAbCon(fATODP, fCTODP)
  427. ATODP           fATODP;
  428. CodeODP         fCTODP;
  429. {
  430.     register AbConPtr       newAbConPtr;
  431.     
  432.     newAbConPtr = NEWAbCon(100);
  433.     newAbConPtr->tag = stdAbConTag;
  434.     newAbConPtr->numEntries = 0;
  435.     newAbConPtr->ATOID = fATODP->ownOID;
  436.     newAbConPtr->CodeOID = fCTODP->ownOID;
  437.     newAbConPtr->aPtr = (ATPtr) NULL;
  438.     newAbConPtr->cPtr = (CodePtr) NULL;
  439.     newAbConPtr->tag.hasNoPointer = TRUE;
  440.     newAbConPtr->tag.allInstancesAreLocal = TRUE;
  441.     newAbConPtr->tag.frozen = FALSE;
  442.  
  443.     KMDTrace("AbCon", 3, "AbCon(0x%05x, 0x%05x) had to be faked\n",
  444.     fATODP->ownOID, fCTODP->ownOID);
  445.     return (newAbConPtr);
  446. }
  447.  
  448. /**********************************************************************/
  449. /*      ReDoAbCon                                                     */
  450. /**********************************************************************/
  451. void ReDoAbCon(fAbConPtr)
  452. AbConPtr                fAbConPtr;
  453. {
  454.     CodeODP             theCodeODP;
  455.     ATODP               theATODP;
  456.     CodePtr             theCodePtr, theATCodePtr;
  457.     OpVectorPtr         ctOpVecPtr, atOpVecPtr;
  458.     int                 opCTCount, opATCount, i, j;
  459.  
  460.     KMDTrace("AbCon", 3, "ReDoAbCon (%s, %s, %s)\n", PPCOID(fAbConPtr->ATOID),
  461.     PPCOID(fAbConPtr->restrictOID), PPCOID(fAbConPtr->CodeOID));
  462.     
  463.     theCodeODP          = (CodeODP) OTLookup(fAbConPtr->CodeOID);
  464.     if (NonNULL(theCodeODP) && NonNULL(theCodeODP->dataPtr)) {
  465.     theCodePtr      = theCodeODP->dataPtr;
  466.     
  467.     fAbConPtr->cPtr    = theCodePtr;
  468.  
  469.     ctOpVecPtr = 
  470.         (OpVectorPtr) addOffset(theCodePtr, theCodePtr->opVecOffset);
  471.     opCTCount = ctOpVecPtr->numEntries;
  472.     }
  473.  
  474.     theATODP            = (ATODP) OTLookup(fAbConPtr->ATOID);
  475.     if (NonNULL(theATODP) && NonNULL(theATODP->dataPtr)) {
  476.     theATCodePtr    = theATODP->dataPtr;
  477.     fAbConPtr->aPtr    = theATCodePtr;
  478.     }
  479.  
  480.     /* Now check to see, if we actually are able to redo the abcon */
  481.     if (IsNULL(fAbConPtr->aPtr) || IsNULL(fAbConPtr->cPtr)) {
  482.     KMDTrace("AbCon", 3, "Still have a piece missing, no final fixup\n");
  483.     return;
  484.     }
  485.     
  486.     theATODP->tag.frozen        =
  487.     theATODP->tag.setUpDone     =
  488.     theATODP->tag.isResident    = TRUE;
  489.  
  490.     if (IsNULL(theATCodePtr->opVecOffset)) {
  491.     KMDTrace("AbCon", 3, "Redoing AT with no op vector -- done\n");
  492.     return;
  493.     }
  494.  
  495.     atOpVecPtr = 
  496.     (OpVectorPtr) addOffset(theATCodePtr, theATCodePtr->opVecOffset);
  497.     opATCount = atOpVecPtr->numEntries;
  498.  
  499.     KMDTrace("AbCon", 4, "Op count:  AT has %d ops; CT has %d ops\n",
  500.     opATCount, opCTCount);
  501.  
  502.     for (i=0, j=0; (i<opATCount) && (j<opCTCount); j++) {
  503.     if (ctOpVecPtr->entry[j].operationNameOID !=
  504.         atOpVecPtr->entry[i].operationNameOID) {
  505.         KMDTrace("AbCon", 5, "CT op #%d [0x%08x] skipped\n", j,
  506.         ctOpVecPtr->entry[j].operationNameOID);
  507.         continue;
  508.     }
  509.     fAbConPtr->opVector[i].opAddress = 
  510.         (CodeAddr) addOffset(theCodePtr, ctOpVecPtr->entry[j].offset);
  511.     fAbConPtr->opVector[i].argumentCount =
  512.         ctOpVecPtr->entry[j].argumentCount;
  513.     fAbConPtr->opVector[i].resultCount   =
  514.         ctOpVecPtr->entry[j].resultCount;
  515.     fAbConPtr->opVector[i].operationNameOID =
  516.         ctOpVecPtr->entry[j].operationNameOID;
  517.     KMDTrace("AbCon", 5, "Op #%d [0x%08x] %d arg%s %d result%s\n",
  518.         i, fAbConPtr->opVector[i].operationNameOID,
  519.         fAbConPtr->opVector[i].argumentCount,
  520.         mPLURAL(fAbConPtr->opVector[i].argumentCount),
  521.         fAbConPtr->opVector[i].resultCount,
  522.         mPLURAL(fAbConPtr->opVector[i].resultCount));
  523.     i++;
  524.     };
  525.     if (i < opATCount) {
  526.     KMDTrace("AbCon", 1, " view mismatch -- contact norm@arizona\n");
  527.     ErrMsg("View mismatch, CT does not conform to AT -- contact norm\n");
  528.     ErrMsg("AT %s CT %s\n", PPCOID(fAbConPtr->ATOID),
  529.         PPCOID(fAbConPtr->CodeOID));
  530.     (void) fflush(stdout);
  531.     (void) fflush(stderr);
  532.     assert (FALSE);
  533.         abort();
  534.     }
  535. }
  536.  
  537. /**********************************************************************/
  538. /*      InsertUnknownATAbConMap                                       */
  539. /**********************************************************************/
  540. void InsertUnknownATAbConMap(fAbConPtr)
  541. register AbConPtr       fAbConPtr;
  542. {
  543.     Set                 theSet;
  544.     
  545.     KMDTrace("AbCon", 3, "Inserting (AT %s, CT %s) into UnknownATAbConMap\n",
  546.         PPCOID(fAbConPtr->ATOID), PPCOID(fAbConPtr->CodeOID));
  547.     theSet  = (Set) Map_Lookup(UnknownATAbConMap, (int) (fAbConPtr->ATOID));
  548.     if (IsNIL(theSet)) {
  549.     KMDTrace("AbCon", 5, "First abcon, creating set.\n");
  550.         theSet  = Set_Create();
  551.     Map_Insert(UnknownATAbConMap, (int) fAbConPtr->ATOID, (int) theSet);
  552.     }
  553.     Set_Insert(theSet, (int) fAbConPtr);
  554. }
  555. /**********************************************************************/
  556. /*      OIDOIDOIDToAbCon                                              */
  557. /**********************************************************************/
  558.  
  559. AbConPtr OIDOIDOIDToAbCon(fATOID, fROID, fCTOID)
  560. OID fATOID, fROID, fCTOID;
  561. /* Construct an AbCon vector given an Abstract OID and a code OID */
  562. {
  563.     register AbConPtr   newAbConPtr;
  564.     CodeODP             theCodeODP;
  565.     ATODP               theATODP;
  566.     
  567.     newAbConPtr = (AbConPtr)
  568.         Map96_Lookup(AbConMap96, (int) fATOID, (int) fROID, (int) fCTOID);
  569.  
  570.     if (NonNIL(newAbConPtr)) {
  571.     KMDTrace("AbCon", 5, "AbCon(0x%05x, 0x%05x, 0x%05x) @ 0x%05x\n",
  572.         fATOID, fROID, fCTOID, newAbConPtr);
  573.     KMDTrace("AbCon", 5, "Returning known abcon (0x%06x)\n", newAbConPtr);
  574.     return newAbConPtr;
  575.     }
  576.     
  577.     /*
  578.      * This abcon unknown, so construct it.
  579.      * An optimization would be to construct it from an earlier one.
  580.      */
  581.     
  582.     theCodeODP = (CodeODP) OTLookup(fCTOID);
  583.     if (IsNULL(theCodeODP)) {
  584.     /* Then create one */
  585.     theCodeODP = CreateCodeODEntry(fCTOID, (EmLocation) NULL);
  586.     }
  587.     theATODP   = (CodeODP) OTLookup(fATOID);
  588.     if (IsNULL(theATODP)) {
  589.     /* Then create one */
  590.     theATODP = CreateCodeODEntry(fATOID, (EmLocation) NULL);
  591.     }
  592.  
  593.     if (IsNULL(theCodeODP->dataPtr)) {
  594.     /* The code is unknown */
  595.     if (IsNULL(theATODP->dataPtr)) {
  596.         /* Unknown Code, unknown Ab */
  597.         newAbConPtr = MakeFakeAbCon(theATODP, theCodeODP);
  598.     } else {
  599.         /* Unknown Code, know ab */
  600.         newAbConPtr = MakeAbAb(theATODP, theCodeODP);
  601.     }
  602.     } else {
  603.     /* The code is known */
  604.     if (IsNULL(theATODP->dataPtr)) {
  605.         /* Known Code, unknown Ab */
  606.         newAbConPtr = MakeConCon(theATODP, theCodeODP);
  607.     } else {
  608.         /* Known Code, known ab */
  609.         newAbConPtr = MakeAbCon(theATODP, theCodeODP);
  610.     }
  611.     }
  612.     
  613.     newAbConPtr->restrictOID    = fROID;
  614.  
  615.     Map96_Insert(AbConMap96, (int) fATOID, (int) fROID, (int) fCTOID,
  616.     (int) newAbConPtr);
  617.  
  618.     KMDTrace("AbCon", 5, "New AbCon(0x%05x, 0x%05x, 0x%05x) @ 0x%05x\n",
  619.     fATOID, fROID, fCTOID, newAbConPtr);
  620.     
  621.     /* Put reference to abcon into the concrete OD */
  622.     Set_Insert((Set)(theCodeODP->AbConSetPtr), (int) newAbConPtr);
  623.     
  624.     /* If the AT is unknown put the abcon into the redo list */
  625.     if (IsNULL(newAbConPtr->aPtr)) {
  626.     /* Put the new abcon into the UnknownATAbConMap */
  627.         InsertUnknownATAbConMap(newAbConPtr);
  628.     }
  629.     return(newAbConPtr);
  630. }
  631.  
  632. /**********************************************************************/
  633. /*          GetAbCon                                                  */
  634. /**********************************************************************/
  635.  
  636. AbConPtr GetAbCon(fATOID, fROID, fCTOID, fTag)
  637. OID fATOID, fROID, fCTOID;
  638. ODTag   fTag;
  639. /* Construct an AbCon vector given an Abstract OID, a code OID, and a tag */
  640. {
  641.     register AbConPtr   newAbConPtr;
  642.     CodeODP             theCodeODP;
  643.  
  644.     newAbConPtr = OIDOIDOIDToAbCon(fATOID, fROID, fCTOID);
  645.     KMDTrace("AbCon", 3, "GetAbCon(0x%05x, 0x%05x 0x%05x) @ 0x%05x\n", fATOID,
  646.     fROID, fCTOID, newAbConPtr);
  647.     theCodeODP = (CodeODP) OTLookup(fCTOID);
  648.     if (IsNULL(theCodeODP) || IsNULL(theCodeODP->dataPtr)){
  649.     /* Use the given tag */
  650.     newAbConPtr->tag        = fTag;
  651.     } else {
  652.     assert (theCodeODP->dataPtr->tag.hasNoPointer == fTag.hasNoPointer);
  653.     assert(theCodeODP->dataPtr->tag.allInstancesAreLocal == fTag.allInstancesAreLocal);
  654.     }
  655.     return(newAbConPtr);
  656. }
  657.  
  658. /**********************************************************************/
  659. /*      ChangeViewPtr                                                 */
  660. /**********************************************************************/
  661.  
  662. /* Kernel entry */
  663. void ChangeViewPtr(fOldAbConPtr, fNewATPtr)
  664. register AbConPtr       fOldAbConPtr;
  665. register ATPtr          fNewATPtr;
  666. /* Return an abcon vector for the new view */
  667. /* Guarenteed to work */
  668. {
  669.     KMDTrace("View", 3, "Changing view from (%s, %s) to (%s, %s)\n",
  670.     PPCOID(fOldAbConPtr->ATOID), PPCOID(fOldAbConPtr->CodeOID),
  671.     PPCOID(fNewATPtr->ownOID), PPCOID(fOldAbConPtr->CodeOID));
  672.     currentSSP->resultBrand = ODPBrand;
  673.     currentSSP->regs.arg1 =
  674.     (int) OIDOIDOIDToAbCon(fNewATPtr->ownOID, fOldAbConPtr->restrictOID,
  675.         fOldAbConPtr->CodeOID);
  676. }
  677.  
  678. /**********************************************************************/
  679. /**********************************************************************/
  680. /*      Code dependencies                                             */
  681. /**********************************************************************/
  682.  
  683. /**********************************************************************/
  684. /* Often a piece of code has dependencies on another piece of code    */
  685. /* in the form of jump instructions.  When the destination code is    */
  686. /* non-resident, the jump instruction is made to jump to a kernel     */
  687. /* error routine.                                                     */
  688. /* During translation of the jump instruction, a code dependency is   */
  689. /* generated so that should the code ever become resident then the    */
  690. /* jump instruction can be fixed-up with the correct address.         */
  691. /**********************************************************************/
  692.  
  693. /**********************************************************************/
  694. /*      CodeDependency                                                */
  695. /**********************************************************************/
  696.  
  697. void CodeDependency(fDependentOID, fNonResidentOID)
  698. OID             fDependentOID;              /* OID for dependent */
  699. OID             fNonResidentOID;            /* OID for the non-resident code*/
  700. /* Generate fDependentOID dependes on fNonResidentOID */
  701. {
  702.     CodeODP             theCodeODP;
  703.     
  704.     theCodeODP = CreateCodeODEntry(fNonResidentOID, (EmLocation) NULL);
  705.     if (IsNULL(theCodeODP->depCode))
  706.     theCodeODP->depCode = (GenericPtr) Set_Create();
  707.     /* Using Set instead of Bag (since Bag is yet to be defined) */
  708.     Set_Insert((Set) theCodeODP->depCode, (int) fDependentOID);
  709.     KMDTrace("Code", 3, "(%s) depends on (%s)\n", PPCOID(fDependentOID),
  710.     PPCOID(fNonResidentOID));
  711. }
  712.  
  713. /**********************************************************************/
  714. /*      CodeArrived                                                   */
  715. /**********************************************************************/
  716.  
  717. void CodeArrived(fCodeODP)
  718. CodeODP                 fCodeODP;
  719. {
  720.     OID                 depCodeOID;
  721.     CodeODP             depCodeODP;
  722.     OID                 loadOID;
  723.     CodeLoadReqPtr      req;
  724.     AbConPtr            theAbConPtr;
  725.     Set                 theSet;
  726.  
  727.     loadOID = fCodeODP->ownOID;
  728.     fCodeODP->tag.isResident = TRUE;
  729.     
  730.     KMDTrace("Code", 3, "%s arrived; checking dependencies\n",
  731.     PPCodePtr(fCodeODP->dataPtr));
  732.  
  733.     /* Check for code dependencies */
  734.     if (NonNULL(fCodeODP->depCode)) {
  735.     KMDTrace("Code", 3, "%d dependencies\n",
  736.         Set_Count((Set) fCodeODP->depCode));
  737.     Set_For(((Set) fCodeODP->depCode), depCodeOID)
  738.         depCodeODP = (CodeODP) OTLookup(depCodeOID);
  739.         if (IsNULL(depCodeODP)) continue;
  740.         retranslate(depCodeODP);
  741.     Set_Next
  742.     Set_Destroy((Set) fCodeODP->depCode);
  743.     fCodeODP->depCode = (GenericPtr) NULL;
  744.     }
  745.     
  746.     /* Check for AbCons to fix */
  747.     theSet = (Set) (fCodeODP->AbConSetPtr);
  748.     if (NonNULL(theSet) && (Set_Count(theSet) > 0)) {
  749.     KMDTrace("Code", 3, "Checking %d AbCons on arrival of CT %s\n",
  750.         Set_Count(theSet), PPCodePtr(fCodeODP->dataPtr));
  751.     Set_For(theSet, theAbConPtr)    
  752.         KMDTrace("Code", 3, "Redoing AbCon 0x%04x\n", theAbConPtr);
  753.         ReDoAbCon(theAbConPtr);
  754.     Set_Next
  755.     }
  756.     
  757.     /* Check to see if this is a previously unknown AT that has
  758.        AbCons */
  759.     theSet  = (Set) Map_Lookup(UnknownATAbConMap, (int) loadOID);
  760.     if (NonNIL(theSet)) {
  761.     KMDTrace("AbCon", 5, "Redoing %d AbCons for newly resident AT %s\n",
  762.             Set_Count(theSet), PPCOID(loadOID));
  763.  
  764.     Set_For(theSet, theAbConPtr)
  765.             KMDTrace("AbCon", 5, "Redoing for CT %s\n",
  766.         PPCOID(theAbConPtr->CodeOID));
  767.         ReDoAbCon(theAbConPtr);
  768.     Set_Next;
  769.  
  770.         /* Now that the AbCons have been redone, delete from unknown map */
  771.         Map_Delete(UnknownATAbConMap, (int) loadOID);
  772.     Set_Destroy(theSet);
  773.     }
  774.  
  775.     /* Check for outstanding remote code load requests */
  776.     req = (CodeLoadReqPtr) Map_Lookup(RemoteLoadMap, (int) loadOID);
  777.     if (NonNIL(req)) {
  778.     KMDTrace("Code", 3,
  779.         "Code arrival causes unblocking of load request\n");
  780.     Map_Delete(RemoteLoadMap, (int) loadOID);
  781.     CodeLoadCallBack(req, loadOID);
  782.     }
  783. }
  784.  
  785. /**********************************************************************/
  786. /**********************************************************************/
  787.  
  788. /**********************************************************************/
  789. /*      KernelCheatingCreate                                          */
  790. /**********************************************************************/
  791.  
  792. ODP KernelCheatingCreate(fOID, fCodeOID, fParamCount, fParamArray)
  793. OID                     fOID, fCodeOID;
  794. int                     fParamCount;
  795. AVariable               fParamArray[];
  796. /* Let the kernel create an object with the given OID and run its initially
  797.    with the given parameters */
  798. {
  799.     SSPtr               p;
  800.     CodeODP             theCodeODP;
  801.     CodePtr             theCode;
  802.     ODP                 theObj;
  803.     int                 i;
  804.     
  805.     KMDTrace("Create", 2, "Cranking up object %s; one of %s\n",
  806.     PPGOID(fOID), PPCOID(fCodeOID));
  807.     theCodeODP = (CodeODP) OTLookup(fCodeOID);
  808.     assert(NonNULL(theCodeODP));
  809.     theCode = theCodeODP->dataPtr;
  810.     theObj = (ODP) AllocateWithOID(theCode, fOID, theCode->instanceSize);
  811.     if (theCode->initially.offset) {
  812.     /* Has an initially  (InitiallyDone does the process) */
  813.     KMDTrace("Create", 3,
  814.         "Starting initially for one of %s, %d argument%s\n",
  815.         PPCOID(fCodeOID), fParamCount, mPLURAL(fParamCount));
  816.     p = NewProcess();
  817.     for (i=0; i < fParamCount; i++) {
  818.         PUSHIT(p->regs.sp, fParamArray[i].myAbConPtr);
  819.         PUSHIT(p->regs.sp, fParamArray[i].myAddr);
  820.     }
  821.     StartProcessAtAddr(p, (GODP) theObj,
  822.         (theObj->G.tag.global ? theObj->G.dataPtr : (GODataPtr) theObj),
  823.         (CodeAddr) addOffset(theCode, theCode->initially.offset));
  824.     } else {
  825.     if (theObj->G.tag.global) {
  826.         theObj->G.tag.setUpDone    = TRUE;
  827.         theObj->G.tag.frozen       = FALSE;
  828.         UnblockInitially((GODP)theObj);
  829.     }
  830.     if (theCode->process.offset) {
  831.         /* No initially, but a process -- fire it up */
  832.         KMDTrace("Code", 3, "Starting its process\n");
  833.         p = NewProcess();
  834.         StartProcessAtAddr(p, (GODP) theObj, (GODataPtr) theObj,
  835.         (CodeAddr) addOffset(theCode, theCode->process.offset));
  836.     }
  837.     }
  838.     return theObj;
  839. }
  840. /**********************************************************************/
  841. void CheatingCreateCodeLoadDone(fReq, fCodeOID)
  842. CheatingLoadReqPtr          fReq;
  843. OID                         fCodeOID;
  844. {
  845.     ODP                             newObj;
  846.     register CheatingLoadReqPtr     req = fReq;
  847.     
  848.     KMDTrace("Create", 3, "Creating %s; one of %s\n", PPGOID(req->theObjOID),
  849.     PPCOID(req->theCodeOID));
  850.     assert(req->theCodeOID == fCodeOID);
  851.     newObj = KernelCheatingCreate(req->theObjOID, req->theCodeOID, 0, NULL);
  852.     OTInsert(newObj);
  853.     newObj->G.ownLoc                = thisNodeLocation;
  854.     newObj->G.tag.isResident        = TRUE;
  855.     newObj->G.tag.frozen            = !newObj->G.tag.setUpDone;
  856.     KMDTrace("FixMe", 3, "Should set the frozen bit right\n");
  857.     Map_Delete(CheatingLoadMap, (int) req->theObjOID);
  858.     DoCallBack((GenericReqPtr) req, req->theObjOID);
  859.     /* Check to see if there are location requests for this one */
  860.     FreeRequest((GenericReqPtr) req);
  861. }
  862.  
  863. /**********************************************************************/
  864. /*      CheatingCrankupHandler                                        */
  865. /**********************************************************************/
  866.  
  867. void CheatingCrankupHandler(fOID)
  868. OID                         fOID;
  869. {
  870.     register GODP           theODP;
  871.     DotoFilePtr             doto;
  872.     OID                     theCodeOID;
  873.     CodePtr                 theCode;
  874.     CheatingLoadReqPtr      req;
  875.     
  876.     KMDTrace("Create", 3, "Performing Cheating Create for %s\n",
  877.     PPGOID(fOID));
  878.     theODP          = (GODP) OTLookup(fOID);
  879.     if (IsNULL(theODP)) {
  880.     KMDTrace("Create", 2, "Object unknown in Cheating Crankup\n");
  881.     return;
  882.     }
  883.     if (theODP->tag.setUpDone) {
  884.     KMDTrace("Create", 3, "Already done\n");
  885.     return;
  886.     }
  887.     
  888.     /* Load the code and find its OID */
  889.     doto = LoadCheatingFromCompiler(fOID);
  890.     if (IsNULL(doto)) {
  891.     KMDTrace("Create", 2, "Did not find %s on disk ?\n", PPGOID(fOID));
  892.     return;
  893.     }
  894.     theCode                 = dotoCodePtr(doto);
  895.     theCodeOID              = theCode->ownOID;
  896.     req                     = mNewRequest(CheatingLoad);
  897.     req->status             = FoundSomewhere;
  898.     req->hdr.callBack       = (GenericHandlerPtr) CheatingCreateCodeLoadDone;
  899.     req->theObjOID          = fOID;
  900.     req->theCodeOID         = theCodeOID;
  901.     
  902.     KMDTrace("Code", 4, "Start translation of %s\n", PPCOID(theCodeOID));
  903.     Map_Insert(CheatingLoadMap, (int) fOID, (int) req);
  904.     if (LoadRequest(theCodeOID, (GenericReqPtr) req)) {
  905.     req->status         = Translated;    
  906.     KMDTrace("Code", 4, "Code loaded, now crank up\n");
  907.     CheatingCreateCodeLoadDone(req, req->theCodeOID);
  908.     }
  909. }
  910.  
  911. /**********************************************************************/
  912. /*      StartCheatingCrankup                                          */
  913. /**********************************************************************/
  914.  
  915. void StartCheatingCrankup(fOID, fReq)
  916. OID                         fOID;
  917. GenericReqPtr               fReq;
  918. /* Do a cheating creation of an object whose code is on our own disk */
  919. {
  920.     register GODP           theODP;
  921.     DotoFilePtr             doto;
  922.     OID                     theCodeOID;
  923.     CodePtr                 theCode;
  924.     CheatingLoadReqPtr      req;
  925.     
  926.     KMDTrace("Create", 3, "Cheating Create for %s from own disk.\n",
  927.     PPGOID(fOID));
  928.     theODP          = (GODP) OTLookup(fOID);
  929.     if (IsNULL(theODP)) {
  930.     KMDTrace("Create", 2, "Object unknown in Cheating Crankup\n");
  931.     return;
  932.     }
  933.     if (theODP->tag.setUpDone) {
  934.     KMDTrace("Create", 3, "Already done\n");
  935.     return;
  936.     }
  937.     
  938.     /* Load the code and find its OID */
  939.     doto = LoadCheatingFromCompiler(fOID);
  940.     if (IsNULL(doto)) {
  941.     KMDTrace("Create", 2, "Did not find %s on disk ?\n", PPGOID(fOID));
  942.     return;
  943.     }
  944.     theCode                 = dotoCodePtr(doto);
  945.     theCodeOID              = theCode->ownOID;
  946.     req                     = mNewRequest(CheatingLoad);
  947.     req->status             = FoundSomewhere;
  948.     req->hdr.callBack       = (GenericHandlerPtr) CheatingCreateCodeLoadDone;
  949.     req->theObjOID          = fOID;
  950.     req->theCodeOID         = theCodeOID;
  951.     
  952.     KMDTrace("Code", 4, "Start translation of %s\n", PPCOID(theCodeOID));
  953.     Map_Insert(CheatingLoadMap, (int) fOID, (int) req);
  954.     Map_Insert(fReq->hdr.wantList, (int) req, 0);
  955.     Map_Insert(req->hdr.reqBy, (int) fReq, 0);
  956.     if (LoadRequest(theCodeOID, (GenericReqPtr) req)) {
  957.     req->status         = Translated;    
  958.     KMDTrace("Code", 4, "Code loaded, now crank up\n");
  959.     CheatingCreateCodeLoadDone(req, req->theCodeOID);
  960.     }
  961. }
  962.  
  963. /**********************************************************************/
  964. /*      Translation routines                                          */
  965. /**********************************************************************/
  966.  
  967. RODataPtr OIDToODAP(fOID, fCodeOID)
  968. OID     fOID, fCodeOID;
  969. /* Translate OID to RODataPtr; if necessary, crank up the object */
  970. {
  971.     RODataPtr               dataPtr;
  972.     
  973.     dataPtr = (RODataPtr) OTLookup(fOID);
  974.     if (IsNULL(dataPtr)) {
  975.     dataPtr = (RODataPtr) KernelCheatingCreate(fOID, fCodeOID, 0, 0);
  976.     assert(NonNULL(dataPtr));
  977.     }
  978.     return dataPtr;
  979. }
  980.  
  981. unsigned int OpNumberToAddress(fOID, fOpNumber)
  982. OID     fOID;
  983. int     fOpNumber;
  984. {
  985.     register ODP            p;
  986.     register CodePtr        theCode;
  987.     register OpVectorPtr    theOpVec;
  988.     register CodeOffset     theOffset;
  989.  
  990.     p = (ODP) OTLookup(fOID);
  991.     if (IsNULL(p) || (p->C.tag.tag != CodeODTag) || IsNULL(p->C.dataPtr)) {
  992.     KMDTrace("Translate", 3, "Did not have code %s present\n",
  993.         PPCOID(fOID));
  994.     /* return 0 to indicate that code is not here */
  995.     return((unsigned int) 0);
  996.     }
  997.     if (p->C.dataPtr->opVecOffset == 0) {
  998.     KMDPrint("OpNumberToAddress: Code OID 0x%08x has no OpVector\n",
  999.         fOID);
  1000.     _longjmp(errorExitPoint, 4);
  1001.     }
  1002.     theCode = p->C.dataPtr;
  1003.     theOpVec = (OpVectorPtr) addOffset(theCode, theCode->opVecOffset);
  1004.     if ((theOpVec->numEntries <= fOpNumber) || (fOpNumber < 0)) {
  1005.     KMDPrint("OpNumberToAddress: Code OID 0x%08x OpNumber %d invalid\n",
  1006.         fOID, fOpNumber);
  1007.     _longjmp(errorExitPoint, 5);
  1008.     }
  1009.     theOffset = theOpVec->entry[fOpNumber].offset;
  1010.     if (IsNULL(theOffset)) {
  1011.     KMDPrint("OpNumberToAddress: Code OID 0x%08x OpVector[%d] invalid\n",
  1012.         fOID, fOpNumber);
  1013.     _longjmp(errorExitPoint, 6);
  1014.     }
  1015.     return((unsigned int) addOffset(theCode, theOffset));
  1016. }
  1017.  
  1018.  
  1019. /**********************************************************************/
  1020. /*      FLLoadFile                                                    */
  1021. /**********************************************************************/
  1022.  
  1023. void FLLoadFile(fCount, fName)
  1024. int          fCount;
  1025. char        *fName;
  1026. {
  1027.     time_t              theTime = time((time_t *) 0);
  1028.     DotoFilePtr         dotoFilePtr[MAXDOTOFILES];
  1029.     CodePtr             theCode;
  1030.     CodeODP             myCodeODP;
  1031.     CreateDirectivePtr  cdPtr;
  1032.     register int        i, j;
  1033.     char                fileName[400];
  1034.     int                 val;
  1035.     Map                 createdObj;
  1036.     int                 createdObjCount, c;
  1037.     GODP                x;
  1038.     
  1039.     createdObj = Map_Create();
  1040.     createdObjCount = 0;
  1041.     
  1042.     if (val = _setjmp(errorExitPoint)) {
  1043.     /* This means we longjmp to here upon detecting an error */
  1044.     KMDPrint("File load error -- request aborted at %d.\n", val);
  1045.     /* Clean up  (nothing for now) */
  1046.     Map_Destroy(createdObj);
  1047.     return;
  1048.     }
  1049.  
  1050.     KMDTrace("Code", 2, "Dynamic load request arrived %s\n", ctime(&theTime));
  1051.     KMDTrace("Code", 2, "File base name %s, %d files\n",
  1052.     fName, fCount);
  1053.     KMDPrint("File base name %s, %d files\n", fName, fCount);
  1054.  
  1055.     if ( fCount >= MAXDOTOFILES) {
  1056.     KMDPrint("Too many .o files: %d -- only %d allowed.\n", fCount,
  1057.         MAXDOTOFILES);
  1058.     Map_Destroy(createdObj);
  1059.     return;
  1060.     }
  1061.  
  1062.     if ( fCount < 0) {
  1063.     KMDPrint("File count negative.\n");
  1064.     KMDPrint(" *** Aborting load request ***\n");
  1065.     Map_Destroy(createdObj);
  1066.     return;
  1067.     }
  1068.  
  1069.     /* Phase 1:  Read in the files */
  1070.     for (i=0; i < fCount; i++) {
  1071.     sprintf(fileName, "%s.%d.o", fName, i);
  1072.     KMDPrint("Loading file #%d %s\n", i, fileName);
  1073.     dotoFilePtr[i] = readDotoFile(fileName);
  1074.     if (IsNULL(dotoFilePtr[i])) {
  1075.         KMDPrint("File %s cannot be read as a valid .o file\n", fileName);
  1076.         _longjmp(errorExitPoint, 1);
  1077.     };
  1078.     theCode                     = dotoCodePtr(dotoFilePtr[i]);
  1079.     KMDTrace("Code", 3, "File %s, Code OID 0x%08x Em version %s\n", fileName,
  1080.         theCode->ownOID, ctime(&theCode->emVersion));
  1081.     KMDPrint("Code OID 0x%08x\n", theCode->ownOID);
  1082.     if (theCode->emVersion != EMVERSION) {
  1083.         time_t emVersion = EMVERSION;
  1084.         KMDPrint("File %s version mismatch\n", fileName);
  1085.         KMDPrint("File Em version: %s", ctime(&theCode->emVersion));
  1086.         KMDPrint("Kernel  version: %s", ctime(&emVersion));
  1087.         _longjmp(errorExitPoint, 8);
  1088.     }
  1089.     };
  1090.  
  1091.     /* Phase 2: Create the code objects and the objects according to the
  1092.        Create directives. Creation proceeds in the inverse order, so that
  1093.        OIDs should still be allocated in ascending order. */
  1094.  
  1095.     for (i=fCount-1; i>=0; i--) {
  1096.     /* First, create a CodeOD for the CodeObject */
  1097.     theCode                     = dotoCodePtr(dotoFilePtr[i]);        
  1098.     myCodeODP                   = (CodeODP) emalloc(sizeof(CodeOD));
  1099.     myCodeODP->tag              = stdCodeODTag;
  1100.     myCodeODP->ownOID           = theCode->ownOID;
  1101.     myCodeODP->dataPtr          = theCode;
  1102.     myCodeODP->AbConSetPtr      = (GenericPtr) Set_Create();
  1103.     myCodeODP->depCode          = (GenericPtr) NULL;
  1104.  
  1105.     /* Insert it into the Object Table */
  1106.     OTInsert((ODP) myCodeODP);
  1107.  
  1108.     /* Now do the create directives */
  1109.     cdPtr   = dotoCreateDirectivePtr(dotoFilePtr[i]);
  1110.     if(cdPtr->numEntries > 0) {
  1111.         KMDPrint("\nCreate directive for file #%d, make %d object%s:\n",
  1112.         i, cdPtr->numEntries, mPLURAL(cdPtr->numEntries));
  1113.         for (j = 0; j < cdPtr->numEntries; j++) {
  1114.         x = AllocateWithOID(theCode, cdPtr->createOID[j],
  1115.             theCode->instanceSize);
  1116.         createdObjCount++;
  1117.         Map_Insert(createdObj, createdObjCount, (int) x);
  1118.         KMDPrint("\t%s\n", IsNULL(cdPtr->createOID[j]) ?
  1119.         "ANONYMOUS" : PPGOID(cdPtr->createOID[j]));
  1120.         }
  1121.     }
  1122.     }
  1123.     
  1124.     /* Phase 3: Translate the OIDs in the file and relocate ref to
  1125.        global variables. */
  1126.  
  1127.     for (i=fCount-1; i>=0; i--) {
  1128.     translateDoto(dotoFilePtr[i]);
  1129.     };
  1130.  
  1131.     /* Phase 4: Fire up the necessary processes. */
  1132.     c = 0;
  1133.     for (i = 0; i < fCount; i++) {
  1134.     /* Now do the create directives */
  1135.     cdPtr   = dotoCreateDirectivePtr(dotoFilePtr[i]);
  1136.     for (j = 0; j < cdPtr->numEntries; j++) {
  1137.         SSPtr p;
  1138.         GODP theObj;
  1139.         CodePtr cPtr = dotoCodePtr(dotoFilePtr[i]);
  1140.         c++;
  1141.         theObj = (GODP) Map_Lookup(createdObj, c);
  1142.         if (theObj == (GODP) EMNIL) {
  1143.         KMDPrint("Lookup of created object %s failed\n",
  1144.             PPGOID(cdPtr->createOID[j]));
  1145.         _longjmp(errorExitPoint, 2);
  1146.         }
  1147.         if(cPtr->initially.offset) {
  1148.         /* Has an initially  (InitiallyDone does the process) */
  1149.         p = NewProcess();    
  1150.         StartProcessAtAddr(p, theObj, theObj->dataPtr,
  1151.             (CodeAddr) addOffset(cPtr, cPtr->initially.offset));
  1152.         } else {
  1153.         theObj->tag.setUpDone = TRUE;
  1154.         theObj->tag.frozen = FALSE;
  1155.         if (cPtr->process.offset) {
  1156.             /* No initially, but a process -- fire it up */
  1157.             p = NewProcess();
  1158.             KMDPrint("Firing up process %s in object %s\n",
  1159.             PPPOID(p->processOID), IsNULL(theObj->ownOID) ?
  1160.             "ANONYMOUS" : PPGOID(theObj->ownOID));
  1161.             StartProcessAtAddr(p, theObj, theObj->dataPtr,
  1162.             (CodeAddr) addOffset(cPtr, cPtr->process.offset));
  1163.         }
  1164.         }
  1165.     }
  1166.     }
  1167.     Map_Destroy(createdObj);
  1168.     KMDPrint("\nLoad successfully completed\n");
  1169. }
  1170.  
  1171. /**************************************************************************/
  1172. void FLCreateOneOfCTOID(fCTOID)
  1173. OID             fCTOID;
  1174. {
  1175.     CodeODP         theCodeODP;
  1176.     CodePtr         theCode;
  1177.     OID             newOID;
  1178.     GODP            theObj;
  1179.     SSPtr           p;
  1180.  
  1181.     theCodeODP = (CodeODP) OTLookup(fCTOID);
  1182.     if (IsNULL(theCodeODP) || theCodeODP->tag.tag != CodeODTag) {
  1183.     KMDPrint("0x%05x is not a valid Code OID\n", fCTOID);
  1184.     return;
  1185.     }
  1186.     newOID = getNextOID();
  1187.     theCode = theCodeODP->dataPtr;
  1188.     if (theCode->initially.argumentCount >0) {
  1189.     KMDPrint("Cannot create: arg count >0 (is %d) for Code %s\n",
  1190.         theCode->initially.argumentCount, PPCOID(fCTOID));
  1191.     return;
  1192.     }
  1193.     theObj = MakeObject(theCode, newOID, theCode->instanceSize);
  1194.     KMDPrint("Create object OID 0x%05x; one of %s\n", newOID,
  1195.     PPCOID(fCTOID));
  1196.     if(theCode->initially.offset) {
  1197.     /* Has an initially  (InitiallyDone does the process) */
  1198.     p = NewProcess();    
  1199.     StartProcessAtAddr(p, theObj, theObj->dataPtr,
  1200.         (CodeAddr) addOffset(theCode, theCode->initially.offset));
  1201.     } else {
  1202.     theObj->tag.setUpDone = TRUE;
  1203.     theObj->tag.frozen = FALSE;
  1204.     if (theCode->process.offset) {
  1205.         /* No initially, but a process -- fire it up */
  1206.         p = NewProcess();
  1207.         StartProcessAtAddr(p, theObj, theObj->dataPtr,
  1208.         (CodeAddr) addOffset(theCode, theCode->process.offset));
  1209.     }
  1210.     }
  1211.  
  1212. }
  1213.  
  1214. /**************************************************************************/
  1215. DotoFilePtr FLReadInDoto(fName)
  1216. char        *fName;
  1217. {
  1218.     time_t              theTime = time((time_t *) 0);
  1219.     DotoFilePtr         dotoFilePtr;
  1220.     CodePtr             theCode;
  1221.     CodeODP             myCodeODP;
  1222.     CreateDirectivePtr  cdPtr;
  1223.     register int        j;
  1224.     char                fileName[400];
  1225.     int                 val;
  1226.  
  1227.     if (val = _setjmp(errorExitPoint)) {
  1228.     /* This means we longjmp to here upon detecting an error */
  1229.     ErrMsg("File load error -- request aborted at %d.\n", val);
  1230.     /* Clean up  (nothing for now) */
  1231.     return (DotoFilePtr) NULL;
  1232.     }
  1233.  
  1234.     KMDTrace("Code", 5, "Reading %s at %-15.15s\n", fName, 4+ctime(&theTime));
  1235.     /* Phase 1:  Read in the file */
  1236.     sprintf(fileName, "%s", fName);
  1237.     dotoFilePtr = readDotoFile(fileName);
  1238.     if (IsNULL(dotoFilePtr)) {
  1239.     ErrMsg("File %s cannot be read as a valid .o file\n", fileName);
  1240.     _longjmp(errorExitPoint, 1);
  1241.     };
  1242.     theCode                     = dotoCodePtr(dotoFilePtr);
  1243.     KMDTrace("Code", 3, "File %s, Code OID 0x%08x\n", fileName, theCode->ownOID);
  1244.     KMDTrace("Code", 5, "Em version %-15.15s\n",4+ctime(&theCode->emVersion));
  1245.     if (theCode->emVersion != EMVERSION) {
  1246.     time_t emVersion = EMVERSION;
  1247.     ErrMsg("File %s version mismatch\n", fileName);
  1248.     ErrMsg("File Em version: %s", ctime(&theCode->emVersion));
  1249.     ErrMsg("Kernel  version: %s", ctime(&emVersion));
  1250.     _longjmp(errorExitPoint, 8);
  1251.     }
  1252.     
  1253.     /* Phase 2: Create the code objects and the objects according to the
  1254.        Create directives. Creation proceeds in the inverse order, so that
  1255.        OIDs should still be allocated in ascending order. */
  1256.  
  1257.     /* First, create a CodeOD for the CodeObject */
  1258.     theCode                     = dotoCodePtr(dotoFilePtr);
  1259.     myCodeODP                   = (CodeODP) emalloc(sizeof(CodeOD));
  1260.     myCodeODP->tag              = stdCodeODTag;
  1261.     myCodeODP->ownOID           = theCode->ownOID;
  1262.     myCodeODP->dataPtr          = theCode;
  1263.     myCodeODP->AbConSetPtr      = (GenericPtr) Set_Create();
  1264.     myCodeODP->depCode          = (GenericPtr) NULL;
  1265.  
  1266.     /* Insert it into the Object Table */
  1267.     OTInsert((ODP) myCodeODP);
  1268.  
  1269.     /* Now do the create directives */
  1270.     cdPtr   = dotoCreateDirectivePtr(dotoFilePtr);
  1271.     if (cdPtr->numEntries > 0) {
  1272.     KMDTrace("Code", 3, "Create directive make %d object%s:\n",
  1273.         cdPtr->numEntries, mPLURAL(cdPtr->numEntries));
  1274.     for (j = 0; j < cdPtr->numEntries; j++) {
  1275.         (void) AllocateWithOID(theCode, cdPtr->createOID[j],
  1276.         theCode->instanceSize);
  1277.         KMDTrace("Code", 3, "\t%0.8x\n", cdPtr->createOID[j]);
  1278.     }
  1279.     }
  1280.     return dotoFilePtr;
  1281. }
  1282.  
  1283. /**************************************************************************/
  1284. void FLTranslateAndActivateDoto(fDotoFilePtr)
  1285. DotoFilePtr             fDotoFilePtr;
  1286. {
  1287.     DotoFilePtr         dotoFilePtr = fDotoFilePtr;
  1288.     CodePtr             theCode;
  1289.     CodeODP             theCodeODP;
  1290.     CreateDirectivePtr  cdPtr;
  1291.     register int        j;
  1292.     int                 val;
  1293.  
  1294.     if (val = _setjmp(errorExitPoint)) {
  1295.     /* This means we longjmp to here upon detecting an error */
  1296.     ErrMsg("File load error -- request aborted at %d.\n", val);
  1297.     /* Clean up  (nothing for now) */
  1298.     return;
  1299.     }
  1300.  
  1301.     theCode                     = dotoCodePtr(dotoFilePtr);
  1302.     KMDTrace("Code", 3, "Translating Code OID 0x%08x Em version %-15.15s\n",
  1303.     theCode->ownOID, 4+ctime(&theCode->emVersion));
  1304.  
  1305.     /* Phase 3: Translate the OIDs in the file and relocate ref to
  1306.        global variables. */
  1307.  
  1308.     translateDoto(dotoFilePtr);
  1309.  
  1310.  
  1311.     /* Phase 4: Fire up the necessary processes. */
  1312.  
  1313.     /* Now do the create directives */
  1314.     cdPtr   = dotoCreateDirectivePtr(dotoFilePtr);
  1315.     if (cdPtr->numEntries > 0) {
  1316.     KMDTrace("Code", 5, "Firing up processes\n");
  1317.     for (j = 0; j < cdPtr->numEntries; j++) {
  1318.         SSPtr p;
  1319.         GODP theObj;
  1320.         CodePtr cPtr = dotoCodePtr(dotoFilePtr);
  1321.         KMDTrace("Code", 5, "\t%0.8x\n", cdPtr->createOID[j]);
  1322.         theObj = (GODP) OTLookup(cdPtr->createOID[j]);
  1323.         if (IsNULL(theObj)){
  1324.         ErrMsg("Lookup of created object OID 0x%08x failed\n",
  1325.             cdPtr->createOID[j]);
  1326.         _longjmp(errorExitPoint, 2);
  1327.         }
  1328.         if(cPtr->initially.offset) {
  1329.         /* Has an initially  (InitiallyDone does the process) */
  1330.         p = NewProcess();    
  1331.         StartProcessAtAddr(p, theObj, theObj->dataPtr,
  1332.             (CodeAddr) addOffset(cPtr, cPtr->initially.offset));
  1333.         } else {
  1334.         theObj->tag.setUpDone = TRUE;
  1335.         theObj->tag.frozen = FALSE;
  1336.         if (cPtr->process.offset) {
  1337.             /* No initially, but a process -- fire it up */
  1338.             p = NewProcess();
  1339.             StartProcessAtAddr(p, theObj, theObj->dataPtr,
  1340.             (CodeAddr) addOffset(cPtr, cPtr->process.offset));
  1341.         }
  1342.         }
  1343.     }
  1344.     }
  1345.     
  1346.     /* Phase 5: Declare it done */
  1347.     theCode->tag.setUpDone          = TRUE;
  1348.     theCode->tag.frozen             = FALSE;
  1349.     theCodeODP = (CodeODP) OTLookup(theCode->ownOID);
  1350.     assert(NonNULL(theCodeODP));
  1351.     theCodeODP->tag.setUpDone       = TRUE;
  1352.     theCodeODP->tag.frozen          = FALSE;
  1353. }
  1354.  
  1355. /**********************************************************************/
  1356.  
  1357. void AcceptCode(fLMHandle)
  1358. LMHandle        fLMHandle;
  1359. {
  1360.     CodeTransferItem        item;
  1361.     int                     srcLNN = fLMHandle->mmMsgHdr.MsgSrc;
  1362.     int                     length;
  1363.     char                   *allocedData;
  1364.     DotoFilePtr             dotoptr;
  1365.     ODTag                  *ptr;
  1366.     CodePtr                 theCode;
  1367.     CodeODP                 myCodeODP;
  1368.  
  1369.     KMDTrace("Code", 3, "Accepting Code from LNN %d\n", srcLNN);
  1370.     length = sizeof(item);
  1371.     LMGetData(&fLMHandle, &item, &length);
  1372.     assert(length == sizeof(item));
  1373.     assert(item.hdr.itemTag == CodeTransferITag);
  1374.     length = item.hdr.size - sizeof(item);
  1375.     KMDTrace("Code", 4, "Code size is %d bytes.\n", length);
  1376.     allocedData = emalloc(length+sizeof(ODTag));
  1377.     ptr = (ODTag *) allocedData;
  1378.     *ptr = stdDotoODTag;
  1379.     dotoptr = (DotoFilePtr) (ptr+1);
  1380.     LMGetData(&fLMHandle, dotoptr, &length);
  1381.     KMDTrace("Code", 5, "Got %d bytes of code\n", length);
  1382.     LMClose(&fLMHandle);
  1383.  
  1384.     /* Verify validity of code */
  1385.     if(!isOkDoto(dotoptr)) {
  1386.     ErrMsg(2, "Bad format of code from LNN %d\n", srcLNN);
  1387.         emfree(allocedData);
  1388.     return;
  1389.     }
  1390.     theCode = dotoCodePtr(dotoptr);
  1391.     KMDTrace("Code", 3, "Code %s\n", PPCOID(theCode->ownOID));
  1392.  
  1393.     if (theCode->emVersion != EMVERSION) {
  1394.     time_t emVersion = EMVERSION;
  1395.     ErrMsg("File %s version mismatch in AcceptCode\n");
  1396.     ErrMsg("File Em version: %s", ctime(&theCode->emVersion));
  1397.     ErrMsg("Kernel  version: %s", ctime(&emVersion));
  1398.         emfree(allocedData);
  1399.     return;
  1400.     }
  1401.  
  1402.     myCodeODP = CreateCodeODEntry(theCode->ownOID, mMakeLocation(srcLNN, 1));
  1403.     if (NonNULL((myCodeODP->dataPtr))) {
  1404.     KMDTrace("Code", 3, "Already have Code %s\n",
  1405.         PPCOID(theCode->ownOID));
  1406.     emfree(allocedData);
  1407.     return;
  1408.     }
  1409.     myCodeODP->dataPtr  = theCode;
  1410.     myCodeODP->ownOID   = theCode->ownOID;
  1411.     myCodeODP->ownLoc   = thisNodeLocation;
  1412.     CodeArrived(myCodeODP);
  1413. }
  1414.  
  1415. /**********************************************************************/
  1416. /*      CheckOnDisk                                                   */
  1417. /**********************************************************************/
  1418. Boolean CheckOnDisk(fCTOID)
  1419. OID     fCTOID;
  1420. {
  1421.     char                fileName[200];
  1422.     struct stat         statBuf;
  1423.     int                 result;
  1424.     
  1425.     /*  Check in /usr/em/EC/Dotos/Objects_%0.1x/OID%08x.o */
  1426.     sprintf(fileName, "%sEC/Dotos/Objects_%1.1x/OID%08x.o", EMDIR,
  1427.     fCTOID & 0xf, fCTOID);
  1428.     KMDTrace("Code", 4, "Checking for %s in %s\n", PPCOID(fCTOID),
  1429.     fileName);
  1430.     result = stat(fileName, &statBuf);
  1431.     if (result < 0) {
  1432.     if (errno == ENOENT) {
  1433.         KMDTrace("Code", 4, "Not found on disk.\n");
  1434.         return FALSE;
  1435.     }
  1436.     ErrMsg("Code", 2, "Error on stat of %s\n.");
  1437.     perror("CheckOnDisk");
  1438.     return FALSE;
  1439.     }
  1440.     KMDTrace("Code", 3, "Found %s on disk.\n", fileName);
  1441.     KMDTrace("Code", 4, "Modify time %s", ctime(&statBuf.st_mtime));
  1442.     return TRUE;
  1443. }
  1444.  
  1445. /**********************************************************************/
  1446. /*      LoadFromCompiler                                              */
  1447. /**********************************************************************/
  1448. DotoFilePtr LoadFromCompiler(fCTOID)
  1449. OID     fCTOID;
  1450. {
  1451.     char                fileName[200];
  1452.     DotoFilePtr         theFile;
  1453.     CodeODP             theCodeODP;
  1454.     CodePtr             theCode;
  1455.  
  1456.     /*  Check in /usr/em/EC/Dotos/Objects_%1.1x/OID%08x.o */
  1457.     sprintf(fileName, "%sEC/Dotos/Objects_%1.1x/OID%08x.o", EMDIR,
  1458.     fCTOID & 0xf, fCTOID);
  1459.     KMDTrace("Code", 4, "Trying to load %s from %s\n", PPCOID(fCTOID),
  1460.     fileName);
  1461.     theFile = readDotoFile(fileName);
  1462.     if (IsNULL(theFile)) return theFile;
  1463.     theCode = dotoCodePtr(theFile);
  1464.     theCodeODP = (CodeODP) OTLookup(fCTOID);
  1465.     if (IsNULL(theCodeODP)) {
  1466.     /* Create the CodeOD */
  1467.     theCodeODP = CreateCodeODEntry(fCTOID, (EmLocation) NULL);
  1468.     }
  1469.     theCodeODP->dataPtr         = theCode;
  1470.     theCodeODP->ownOID          = theCode->ownOID;
  1471.     theCodeODP->ownLoc          = thisNodeLocation;
  1472.     
  1473.     theCode->tag.setUpDone      = FALSE;
  1474.     theCode->tag.frozen         = TRUE;
  1475.     theCodeODP->tag.setUpDone   = FALSE;
  1476.     theCodeODP->tag.frozen      = TRUE;
  1477.     
  1478.     CodeArrived(theCodeODP);
  1479.     
  1480.     return theFile;
  1481. }
  1482.  
  1483. /**********************************************************************/
  1484. /*      Loadcheatingfromcompiler                                      */
  1485. /**********************************************************************/
  1486.  
  1487. DotoFilePtr LoadCheatingFromCompiler(fObjOID)
  1488. OID                     fObjOID;
  1489. {
  1490.     char                fileName[200];
  1491.     DotoFilePtr         theFile;
  1492.     CodeODP             theCodeODP;
  1493.     CodePtr             theCode;
  1494.     OID                 codeOID;
  1495.     
  1496.     /*  Check in /usr/em/EC/Dotos/Objects_%0.1x/OID%08x.o */
  1497.     sprintf(fileName, "%sEC/Dotos/Objects_%1.1x/OID%08x.o", EMDIR,
  1498.     fObjOID & 0xf, fObjOID);
  1499.     KMDTrace("Code", 4, "Trying to load %s from %s\n", PPOID(fObjOID),
  1500.     fileName);
  1501.     theFile = readDotoFile(fileName);
  1502.     if (IsNULL(theFile)) return theFile;
  1503.     theCode = dotoCodePtr(theFile);
  1504.     codeOID     =   theCode->ownOID;
  1505.     theCodeODP = (CodeODP) OTLookup(codeOID);
  1506.     if (IsNULL(theCodeODP)) {
  1507.     /* Create the CodeOD */
  1508.     theCodeODP = CreateCodeODEntry(codeOID, thisNodeLocation);
  1509.     }
  1510.     theCodeODP->dataPtr = theCode;
  1511.     theCode->tag.setUpDone = FALSE;
  1512.     theCode->tag.frozen     = TRUE;
  1513.     theCodeODP->tag.setUpDone = FALSE;
  1514.     theCodeODP->tag.frozen  = TRUE;
  1515.     theCodeODP->tag.isResident = TRUE;
  1516.     
  1517.     CodeArrived(theCodeODP);
  1518.     
  1519.     return theFile;
  1520. }
  1521.  
  1522. /**********************************************************************/
  1523. int FLCompilerSockDead()
  1524. {
  1525.     ErrMsg("Socket for compiler usage died -- fatal\n");
  1526.     abort();
  1527.     return 0; /* ought to be void. */
  1528. }
  1529.  
  1530. /**********************************************************************/
  1531. void FLUserSockDead(fSock)
  1532. int             fSock;
  1533. {
  1534.     CompilerLoadReqPtr              req;
  1535.     EmInStreamPtr                   inStream;
  1536.     EmOutStreamPtr                  outStream;
  1537.     
  1538.     KMDTrace("UserIO", 3, "Socket #%d died.\n", fSock);
  1539.     req = CReq[fSock];
  1540.     if (NonNULL(req)) {
  1541.     /* Break streams if necessary */
  1542.     if (NonNULL(req->theInStream)) {
  1543.         /* Break instream */
  1544.         KMDTrace("UserIO", 2, "Instream on socket #%d broke.\n", fSock);
  1545.         inStream = (EmInStreamPtr) req->theInStream;
  1546.         inStream->isBroken = TRUE;
  1547.         req->theInStream = (GODP) NULL;
  1548.         CloseInStream(fSock);
  1549.     }
  1550.     if (NonNULL(req->theOutStream)) {
  1551.         /* Break outstream */
  1552.         KMDTrace("UserIO", 2, "Outstream on socket #%d broke.\n", fSock);
  1553.         outStream = (EmOutStreamPtr) req->theOutStream;
  1554.         outStream->isBroken = TRUE;
  1555.         req->theOutStream = (GODP) NULL;
  1556.         CloseOutStream(fSock);
  1557.     }
  1558.     }
  1559.     CReq[fSock] = (CompilerLoadReqPtr) NULL;
  1560. }
  1561. /**********************************************************************/
  1562. /*ARGSUSED*/
  1563. int FLUserSIGIO(fKind, fSock)
  1564. int             fKind;
  1565. int             fSock;
  1566. {
  1567.     register CompilerLoadReqPtr     req;
  1568.     
  1569.     KMDTrace("UserIO", 3, "UserIO on %d\n", fSock);
  1570.     req = CReq[fSock];
  1571.     assert(NonNULL(req));
  1572.     if (req->status == Connected) {
  1573.     SIRemoveSockHandler(fSock, SIREAD);
  1574.     HoldSigs();
  1575.     QueueTask(ReadCompilerRequest, (char *) req);
  1576.     ReleaseSigs();
  1577.     } else {
  1578.     /* Actual user io */
  1579.     KMDTrace("Fix me", 3, "FLUserSIGIO should read from socket\n");
  1580.     }
  1581.     return 0; /* ought to be declared as void */
  1582. }
  1583.  
  1584. /**********************************************************************/
  1585. /*      EnsurePresenceOfRequiredTypes                                 */
  1586. /**********************************************************************/
  1587. Boolean EnsurePresenceOfRequiredTypes(fDotoPtr, fReq)
  1588. DotoFilePtr             fDotoPtr;
  1589. GenericReqPtr           fReq;
  1590. /* Check a doto for references to types that it must have.
  1591.    Return TRUE if all done.
  1592.    Otherwise return FALSE and enter requests into the wantList */
  1593. {
  1594.   register int          i;
  1595.   long                 *relocatedAddress;
  1596.   RelocationInfoPtr     riptr;
  1597.   ARelocation           ar;
  1598.   CodePtr               cptr;
  1599.   ODP                   cheatingODP;
  1600.   Boolean               result = TRUE;  /* Assume success */
  1601.   
  1602.   assert(NonNULL(fDotoPtr));
  1603.   cptr = dotoCodePtr(fDotoPtr);
  1604.   assert(cptr->tag.tag == CodeTag);
  1605.   KMDTrace("Code", 4, "Ensure presence of types required for %s\n",
  1606.       PPCOID(cptr->ownOID));
  1607.   if (IsNULL(cptr->relocationInfoOffset)) return TRUE;
  1608.   riptr = (RelocationInfoPtr) addOffset(cptr, cptr->relocationInfoOffset);
  1609.   KMDTrace("Code", 5, "Emerald Relocation numEntries %d\n",
  1610.       riptr->numEntries);
  1611.   for (i = 0; i < riptr->numEntries; i++) {
  1612.     ar = riptr->relocation[i];
  1613.     relocatedAddress = (long *) addOffset(cptr, ar.where);
  1614.     KMDTrace("Code", 5, "%s: rel loc %d, abs 0x%05x (0x%05x, 0x%05x)\n",
  1615.     ar.kind == AR_OIDToODP ? "OIDToODP" :
  1616.     ar.kind == AR_OIDToODAP ? "OIDToODAP" :
  1617.     ar.kind == AR_OpNumberToAddress ? "OpNumberToAddress" :
  1618.     ar.kind == AR_OIDOIDToAbCon ? "OIDOIDToAbCon" :
  1619.     ar.kind == AR_OIDToCheatingODP ? "OIDToCheatingODP":
  1620.     ar.kind == AR_OIDToCodePtr ? "OIDToCodePtr" :
  1621.     "Unknown translation",
  1622.     ar.where, relocatedAddress, ar.theOID1, ar.theOID2);
  1623.     switch (ar.kind) {
  1624.       case AR_OIDToODP:
  1625.     /* Do not need the CT */
  1626.     break;
  1627.       case AR_OIDToODAP:
  1628.      /* Immutable object, need CT, must create obj later */
  1629.     result = LoadRequest(ar.theOID2, fReq) && result;
  1630.     break;
  1631.       case AR_OpNumberToAddress:
  1632.     /* Do not need to have it */
  1633.     break;
  1634.       case AR_OIDOIDToAbCon:
  1635.     /* Need AT */
  1636.     result = LoadRequest(ar.theOID1, fReq) && result;
  1637.     break;
  1638.       case AR_OIDToCheatingODP:
  1639.     /* Need CT */
  1640.     result = LoadRequest(ar.theOID2, fReq) && result;
  1641.     /* Start a location of the object.
  1642.     The translation stuff will either crank up the
  1643.     object or use the remote object. */
  1644.     cheatingODP = OTLookup(ar.theOID1);
  1645.     if (IsNULL(cheatingODP)) {
  1646.         cheatingODP = (ODP) CreateGODEntry(ar.theOID1, ar.theOID2);
  1647.     }
  1648.     if (!cheatingODP->G.tag.isResident) {
  1649.         /* The object is not here, so locate it */
  1650.         StartLocate(fReq, cheatingODP);
  1651.         result = FALSE;
  1652.     }
  1653.     break;
  1654.       case AR_OIDToCodePtr:
  1655.     /* Need CT */
  1656.     result = LoadRequest(ar.theOID1, fReq) && result;
  1657.     break;
  1658.       default:
  1659.     ErrMsg("Bad translation kind %d\n", ar.kind);
  1660.     assert(FALSE);
  1661.     break;
  1662.     }
  1663.   }
  1664.   return result;
  1665. }
  1666. /**********************************************************************/
  1667.  
  1668. /**********************************************************************/
  1669. /*      SendCode                                                      */
  1670. /**********************************************************************/
  1671.  
  1672. void SendCode(fODP, fLNN)
  1673. CodeODP             fODP;
  1674. NodeNum             fLNN;
  1675. /* Send the Code given by OID to the node given by fLNN */
  1676. /* Note: OID is represented as ASCII */
  1677. {
  1678.     register CodeODP    theCodeODP = fODP;
  1679.     OID                 theCodeOID = fODP->ownOID;
  1680.     LMHandle            myLMHandle;
  1681.     CodeTransferItem    item;
  1682.     HOTSRecord         *hotsPtr;
  1683.     DotoFilePtr         dotoptr;
  1684.  
  1685.     KMDTrace("Code", 3, "Sending Code %s to node #%d\n", PPCOID(theCodeOID),
  1686.     fLNN);
  1687.     if ((!mSUCCESS(HOTSSearchPtr(fLNN, &hotsPtr))) || (hotsPtr->NodeStat !=
  1688.     Alive) && (hotsPtr->NodeStat != Booting)) {
  1689.     KMDTrace("Code", 2, "Node %d down, SendCode(%s) ignored.\n", fLNN,
  1690.         PPCOID(fODP->ownOID));
  1691.     return;
  1692.     }
  1693.     dotoptr = (DotoFilePtr)
  1694.     addOffset(theCodeODP->dataPtr, -sizeof(dotoptr->header));
  1695.     LMStartMsg(&myLMHandle, KMSG_EmKernel, EMKM_FLAcceptCode, fLNN);
  1696.     item.hdr.itemTag = CodeTransferITag;
  1697.     item.hdr.size = N_STROFF(dotoptr->header) +
  1698.      ( * ( (int *)addOffset(dotoptr, N_STROFF(dotoptr->header) ) ) ) +
  1699.      sizeof(ItemHdr);
  1700.     LMPutData(&myLMHandle, &item, sizeof(item));
  1701.     LMPutData(&myLMHandle, dotoptr, item.hdr.size - sizeof(item));
  1702.     if (!mSUCCESS(LMSendMsg(&myLMHandle))) {
  1703.     KMDTrace("Code", 3,
  1704.         "Cannot send msg to node %d, SendCode(%s) ignored.\n", fLNN,
  1705.         PPCOID(fODP->ownOID));
  1706.     }
  1707. }
  1708.  
  1709.  
  1710. /**********************************************************************/
  1711. /*      RemoteLoadItemHandler                                         */
  1712. /**********************************************************************/
  1713. void RemoteLoadItemHandler(fHandlePtr, fHdr)
  1714. LMHandlePtr                 fHandlePtr;
  1715. ItemHdr                     fHdr;
  1716. {
  1717.     int                     length;
  1718.     OID                     theOID;
  1719.     CodeODP                 theODP;
  1720.     DotoFilePtr             doto;
  1721.  
  1722.     KMDTrace("Code", 5, "Remote Load request arrived\n");
  1723.     assert(fHdr.size == sizeof(RemoteLoadReqItem));
  1724.     length = sizeof(theOID);
  1725.     LMGetData(fHandlePtr, &theOID, &length);
  1726.     assert(length == sizeof(theOID));
  1727.     theODP = (CodeODP) OTLookup(theOID);
  1728.     if (NonNULL(theODP) && NonNULL(theODP->dataPtr)) {
  1729.     SendCode(theODP, (*fHandlePtr)->mmMsgHdr.MsgSrc);
  1730.     } else if (((((unsigned int) theOID) >> 24) & 0xFF) == 0xFF) {
  1731.     /* Trying in compiler directory */
  1732.     doto = LoadFromCompiler(theOID);
  1733.     if (IsNULL(doto)) {
  1734.         KMDTrace("Code", 5, "Did not find %s on disk\n", PPOID(theOID));
  1735.     } else {
  1736.         theODP = (CodeODP) OTLookup(theOID);
  1737.         assert(NonNULL(theODP));
  1738.         KMDTrace("Code", 3, "Found %s on disk.\n");
  1739.         SendCode(theODP, (*fHandlePtr)->mmMsgHdr.MsgSrc);
  1740.     }
  1741.     }
  1742. }
  1743.  
  1744. /**********************************************************************/
  1745. /*      RemoteLoadDeamon                                              */
  1746. /**********************************************************************/
  1747. void RemoteLoadDeamon()
  1748. {
  1749.     CodeLoadReqPtr          req;
  1750.     RemoteLoadReqItem       item;
  1751.     OID                     theOID;
  1752.     CodeODP                 theODP;
  1753.     Boolean                 started = FALSE;
  1754.     LMHandle                myHandle;
  1755.     int                     nodeLNN;
  1756.  
  1757.     KMDTrace("Code", 5, "RemoteLoadDeamon\n");
  1758.     KMDTrace("FixMe", 3, "(RemoteLoadDeamon should batch req together.)1\n");
  1759.     Map_For(RemoteLoadMap, theOID, req)
  1760.     if (req->status == FoundSomewhere) {
  1761.         req->status             = LoadRequested;
  1762.         theODP = (CodeODP) OTLookup(req->theCodeOID);
  1763.         assert(NonNULL(theODP));
  1764.         KMDTrace("Code", 3,
  1765.         "Issuing remote load request for %s at 0x%04x\n",
  1766.         PPCOID(theOID), theODP->ownLoc);
  1767.         nodeLNN = mGetLocNodeNum(theODP->ownLoc);
  1768.         if(IsNULL(nodeLNN)) {
  1769.         ErrMsg("Remote load request for %s to unknown node #%d\n",
  1770.             PPCOID(theODP->ownOID), nodeLNN);
  1771.         continue;
  1772.         }
  1773.         if(!started) {
  1774.         started = TRUE;
  1775.         LMStartMsg(&myHandle, KMSG_EmKernel, EMKM_Item, nodeLNN);
  1776.         }
  1777.         item.hdr.itemTag        = RemoteLoadReqITag;
  1778.         item.hdr.size           = sizeof(item);
  1779.         item.theOID             = theOID;
  1780.         LMPutData(&myHandle, &item, sizeof(item));
  1781.         if (started) {
  1782.         LMSendMsg(&myHandle);
  1783.         started             = FALSE;
  1784.         }
  1785.     }
  1786.     Map_Next
  1787.     RemoteLoadDeamonRunning = FALSE;
  1788. }
  1789.  
  1790. /**********************************************************************/
  1791. /*      EnsureRemoteLoadDeamon                                        */
  1792. /**********************************************************************/
  1793. void EnsureRemoteLoadDeamon()
  1794. /* Ensure that a deamon is running which regularily performs remote
  1795.    load requests.  Such requests are deferred many milliseconds as to
  1796.    get them batched together.
  1797. */
  1798. {
  1799.     if (!RemoteLoadDeamonRunning) {
  1800.     KMDTrace("Code", 5, "Scheduling RemoteLoad Deamon\n");
  1801.     RemoteLoadDeamonRunning = TRUE;
  1802.     (void) MMSetMicroTimer(vFLRemoteLoadDeamonInterval/1000000,
  1803.         vFLRemoteLoadDeamonInterval % 1000000, RemoteLoadDeamon, NULL,
  1804.         NULL);
  1805.     }
  1806. }
  1807.  
  1808. /**********************************************************************/
  1809. /*      EnsureLoadOfATs                                               */
  1810. /**********************************************************************/
  1811. Boolean EnsureLoadOfATs(fReq, fCodeODP)
  1812. GenericReqPtr               fReq;
  1813. CodeODP                     fCodeODP;
  1814. {
  1815.     Boolean                 gotItAll = TRUE;
  1816.     AbConPtr                theAbConPtr;
  1817.  
  1818.     if (IsNULL(fCodeODP) || (IsNULL(fCodeODP->AbConSetPtr)) ||
  1819.         Set_Count((Set) fCodeODP->AbConSetPtr) == 0
  1820.     ) {
  1821.     return TRUE;
  1822.     }
  1823.     
  1824.     Set_For((Set) fCodeODP->AbConSetPtr, theAbConPtr)
  1825.         KMDTrace("Code", 5, "Checking abcon @ 0x%04x\n", theAbConPtr);
  1826.         KMDTrace("AbCon", 5, "Checking abcon @ 0x%04x\n", theAbConPtr);
  1827.         if (IsNULL(theAbConPtr)) {
  1828.         KMDTrace("AbCon", 4, "Must load AT: %s for abcon (0x%04x)\n",
  1829.                 PPCOID(theAbConPtr->ATOID), theAbConPtr);
  1830.         gotItAll = LoadRequest(theAbConPtr->ATOID, fReq) && gotItAll;
  1831.     }
  1832.     Set_Next
  1833.     return gotItAll;
  1834. }
  1835.  
  1836. /**********************************************************************/
  1837. /*      CodeLoadCallBack                                              */
  1838. /**********************************************************************/
  1839. void CodeLoadCallBack(fReq, fCTOID)
  1840. CodeLoadReqPtr              fReq;
  1841. OID                         fCTOID;
  1842. /* Invoked when a state change occurs for a pending load request, e.g.,
  1843.    code arrives from another node, the code has been located on another
  1844.    node, etc. */
  1845. {
  1846.     register CodeLoadReqPtr req = fReq;
  1847.     
  1848.     KMDTrace("Code", 4, "CodeLoadCallBack for %s, status %s, OID:%s\n",
  1849.     PPCOID(req->theCodeOID),
  1850.     req->status == DoNotKnow ?      "Location not known" :
  1851.     req->status == FoundSomewhere ? "Located" :
  1852.     req->status == LoadRequested ?  "Load requested" :
  1853.     req->status == Transferred ?    "Code transferred to here" :
  1854.     req->status == Translated ?     "Code load done":
  1855.     "BAD STATUS",
  1856.     PPCOID(fCTOID));
  1857.  
  1858.     /*
  1859.      *  The following switch statement may be confusing:
  1860.      *  The status switched upon is the old status - not the new status
  1861.      */
  1862.     
  1863.     switch (req->status) {
  1864.     
  1865.     case DoNotKnow:{
  1866.     CodeODP             theCodeODP;
  1867.     CodePtr             codePtr;
  1868.     int                 nodeLNN;
  1869.  
  1870.     /* The Code has now been found somewhere */
  1871.     req->status         = FoundSomewhere;
  1872.  
  1873.  
  1874.     theCodeODP        = (CodeODP) OTLookup(req->theCodeOID);
  1875.  
  1876.     nodeLNN = mGetLocNodeNum(theCodeODP->ownLoc);
  1877.     if(IsNULL(nodeLNN)) { /* Did not find it */
  1878.         Map_Delete(CodeLoadMap, (int) req->theCodeOID);
  1879.         DoCallBack((GenericReqPtr) req, (OID) NULL);
  1880.         FreeRequest((GenericReqPtr) req);
  1881.         break;
  1882.     }
  1883.  
  1884.     /* Issue a remote load request */
  1885.     Map_Insert(RemoteLoadMap, (int) req->theCodeOID, (int) req);
  1886.     EnsureRemoteLoadDeamon();
  1887.     break;
  1888.     }
  1889.     
  1890.     case LoadRequested:
  1891.     case FoundSomewhere: {
  1892.     CodeODP             theCodeODP;
  1893.     CodePtr             codePtr;
  1894.     DotoFilePtr         doto;
  1895.     
  1896.     theCodeODP          = (CodeODP) OTLookup(req->theCodeOID);
  1897.     assert(NonNULL(theCodeODP));
  1898.     codePtr             = theCodeODP->dataPtr;
  1899.     if (IsNULL(codePtr)) {
  1900.         ErrMsg("Could not find code %s -- request now hung.\n",
  1901.         PPCOID(codePtr->ownOID));
  1902.         return;
  1903.     }
  1904.     doto                = mDotoFilePtrFromCodePtr(codePtr);
  1905.     req->status         = Transferred;
  1906.     /* Now fetch all needed types */
  1907.     if (!EnsurePresenceOfRequiredTypes(doto, (GenericReqPtr) req)) {
  1908.         KMDTrace("Code", 4, "Load of %s must await other loads\n",
  1909.         PPCOID(req->theCodeOID));
  1910.         return;
  1911.     }
  1912.     if (!EnsureLoadOfATs((GenericReqPtr) req, theCodeODP)) {
  1913.         KMDTrace("Code", 4, "Load of %s must await load of ATs\n",
  1914.         PPCOID(req->theCodeOID));
  1915.         KMDTrace("AbCon", 4, "Load of %s must await load of ATs\n",
  1916.         PPCOID(req->theCodeOID));
  1917.         return;
  1918.     }
  1919.     /* NBNB:  No break here ! */
  1920.     fCTOID = (OID) NULL; /* since we are falling through to Transferred */
  1921.     }
  1922.     
  1923.     case Transferred: {
  1924.     CodeODP             theCodeODP;
  1925.     CodePtr             theCode;
  1926.     DotoFilePtr         doto;
  1927.     
  1928.     /* We now have another of the required types loaded (given by fCTOID)
  1929.        check to see, if we are done */
  1930.     if (Map_Count(req->hdr.wantList) == 0) {
  1931.         
  1932.         theCodeODP                  = (CodeODP) OTLookup(req->theCodeOID);
  1933.         assert(NonNULL(theCodeODP));
  1934.             /*
  1935.          * Now we think that we are done but have to check for ATs to
  1936.          * load since there may be AbCons out there that absolutely need
  1937.          * to be redone.
  1938.          */
  1939.  
  1940.         if (!EnsureLoadOfATs((GenericReqPtr) req, theCodeODP)) {
  1941.         KMDTrace("Code", 4, "Load of %s must await load of ATs\n",
  1942.             PPCOID(req->theCodeOID));
  1943.         KMDTrace("AbCon", 4, "Load of %s must await load of ATs\n",
  1944.             PPCOID(req->theCodeOID));
  1945.         break;
  1946.         }
  1947.         
  1948.         /* Got them all */
  1949.         KMDTrace("Code", 4, "All required types for %s are now loaded\n",
  1950.         PPCOID(req->theCodeOID));
  1951.         theCode                     = theCodeODP->dataPtr;
  1952.         doto                        = mDotoFilePtrFromCodePtr(theCode);
  1953.         translateDoto(doto);
  1954.         theCodeODP->tag.setUpDone   = TRUE;
  1955.         theCodeODP->tag.frozen      = FALSE;
  1956.         theCodeODP->ownLoc          = thisNodeLocation;
  1957.         theCode->tag.setUpDone      = TRUE;
  1958.         theCode->tag.frozen         = FALSE;
  1959.         KMDTrace("Code", 3, "%s now fully loaded and done\n",
  1960.         PPCOID(theCodeODP->ownOID));
  1961.         Map_Delete(CodeLoadMap, (int) req->theCodeOID);
  1962.         DoCallBack((GenericReqPtr) req, req->theCodeOID);
  1963.         FreeRequest((GenericReqPtr) req);
  1964.         break;
  1965.     }
  1966.     KMDTrace("Code", 4, "Got %s for %s but still need more\n",
  1967.         PPCOID(req->theCodeOID), PPCOID(fCTOID));
  1968.     break;
  1969.     }
  1970.     
  1971.     case Translated: {
  1972.     break;
  1973.     }
  1974.     default:    assert(FALSE);
  1975.     }
  1976. }
  1977.  
  1978. /**********************************************************************/
  1979. /*      LoadRequest                                                   */
  1980. /**********************************************************************/
  1981.  
  1982. Boolean LoadRequest(fCTOID, fReq)
  1983. OID             fCTOID;
  1984. GenericReqPtr   fReq;
  1985. /*
  1986.    Load the specified code;  if necessary, crank up a CodeLoad Request
  1987.    and enter it into the want list of fReq.
  1988.    Return TRUE, if load completed, otherwise return FALSE after
  1989.    cranking up the CodeLoad Request */   
  1990. {
  1991.     register CodeLoadReqPtr         req;
  1992.     CodeODP                         theCodeODP;
  1993.     CodePtr                         theCode;
  1994.     DotoFilePtr                     doto;
  1995.  
  1996.     KMDTrace("Code", 4, "LoadRequest for %s\n", PPCOID(fCTOID));
  1997.     assert(fCTOID != (OID) NULL);
  1998.     theCodeODP = (CodeODP) OTLookup(fCTOID);
  1999.     if (IsNULL(theCodeODP)) {
  2000.     /* Create a CodeOD, location unknown */
  2001.     theCodeODP = CreateCodeODEntry(fCTOID, (EmLocation) NULL);
  2002.     }
  2003.     
  2004.     if (theCodeODP->tag.setUpDone) {
  2005.     /* It is there and ready, so we are done */
  2006.     KMDTrace("Code", 4, "Need and have %s\n", PPCOID(fCTOID));
  2007.     return TRUE;
  2008.     }
  2009.     req = (CodeLoadReqPtr) Map_Lookup(CodeLoadMap, (int) fCTOID);
  2010.     if ((int) req != EMNIL) {
  2011.     /* A load request for the CTOID already exists */
  2012.     if ((req->status == Transferred) || (req->status == Translated)) {
  2013.         /* We know enough to translated it so continue */
  2014.         /* (This happens, e.g., if the code refers to itself.) */
  2015.         KMDTrace("Code", 4,
  2016.         "Got %s loaded enough to use it for translation.\n",
  2017.         PPCOID(fCTOID));
  2018.         return TRUE;
  2019.     }
  2020.     if ( Map_Lookup(fReq->hdr.wantList, (int) req) != (int) EMNIL) {
  2021.         /* It is already in our want list so return */
  2022.         KMDTrace("Code", 4, "Already want %s\n", PPCOID(fCTOID));
  2023.         return FALSE;
  2024.     }
  2025.     /* He needs it, so put it into his want list */
  2026.     Map_Insert(req->hdr.reqBy, (int) fReq, 0);
  2027.     Map_Insert(fReq->hdr.wantList, (int) req, 0);
  2028.     KMDTrace("Code", 3, "%s load in progress; add it to the want list\n",
  2029.         PPCOID(fCTOID));
  2030.     return FALSE;
  2031.     }
  2032.     req = mNewRequest(CodeLoad);
  2033.     req->theCodeOID     = fCTOID;
  2034.     req->hdr.callBack   = (GenericHandlerPtr) CodeLoadCallBack;
  2035.     req->status         = DoNotKnow;
  2036.     KMDTrace("Code", 4, "Crank up load request for %s\n", PPCOID(fCTOID));
  2037.     Map_Insert(CodeLoadMap, (int) fCTOID, (int) req);
  2038.     Map_Insert(req->hdr.reqBy, (int) fReq, 0);
  2039.     Map_Insert(fReq->hdr.wantList, (int) req, 0);
  2040.  
  2041.     if (IsNULL(theCodeODP->dataPtr)) {
  2042.     /* We do not have the code, try the compiler directory */
  2043.     doto = LoadFromCompiler(fCTOID);
  2044.     if (NonNULL(doto)) {
  2045.         KMDTrace("Code", 3, "Found %s in the compiler directory\n",
  2046.         PPCOID(fCTOID));
  2047.     }
  2048.     } else {
  2049.     doto = mDotoFilePtrFromCodePtr(theCodeODP->dataPtr);
  2050.     }
  2051.     
  2052.     if (NonNULL(theCodeODP->dataPtr)) {
  2053.     req->status = Transferred;
  2054.         if (!EnsureLoadOfATs(fReq, theCodeODP)) {
  2055.         KMDTrace("Code", 5, "Need some ATs for AbCons\n");
  2056.     } else if (EnsurePresenceOfRequiredTypes(doto, (GenericReqPtr) req)) {
  2057.         /* They are all there, now translate and activate any processes */
  2058.         KMDTrace("Code", 4, "All required types for %s loaded\n",
  2059.         PPCOID(fCTOID));
  2060.         translateDoto(doto);
  2061.         theCodeODP->tag.setUpDone   = TRUE;
  2062.         theCodeODP->tag.frozen      = FALSE;
  2063.         theCodeODP->ownLoc          = thisNodeLocation;
  2064.         theCode                     = theCodeODP->dataPtr;
  2065.         theCode->tag.setUpDone      = TRUE;
  2066.         theCode->tag.frozen         = FALSE;
  2067.         Map_Delete(CodeLoadMap, (int) fCTOID);
  2068.         Map_Delete(req->hdr.reqBy, (int) fReq);
  2069.         Map_Delete(fReq->hdr.wantList, (int) req);
  2070.         FreeRequest((GenericReqPtr) req);
  2071.         KMDTrace("Code", 2, "%s fully loaded and done\n", PPCOID(fCTOID));
  2072.         return TRUE;
  2073.     }
  2074.     }
  2075.     KMDTrace("Code", 3, "Must wait for %s to be found and loaded\n",
  2076.     PPCOID(fCTOID));
  2077.     StartLocate((GenericReqPtr) req, (ODP) theCodeODP);
  2078.     return FALSE;
  2079. }
  2080.  
  2081. /**********************************************************************/
  2082. /*      DoCompilerCreate                                              */
  2083. /**********************************************************************/
  2084.  
  2085. void DoCompilerCreate(fReq, fOID)
  2086. register CompilerLoadReqPtr         fReq;
  2087. OID                                 fOID;
  2088. /* Callback routine for compiler creation */
  2089. {
  2090.     CodeODP                         theCodeODP;
  2091.     CodePtr                         theCode;
  2092.     GODP                            createdODP;
  2093.  
  2094.     KMDTrace("Code", 3,
  2095.     "Callback after loading code; creating object one of %s\n",
  2096.     PPGOID(fReq->createdCTOID));
  2097.  
  2098.     if (fReq->createdCTOID != fOID || IsNULL(fOID) ||
  2099.     IsNULL(OTLookup(fOID)))  /* rather safe than sorry */
  2100.     {
  2101.         char *badprog = "Emerald program not found (try recompiling)\n";
  2102.     int   length = strlen(badprog);
  2103.     int   count;
  2104.     
  2105.         KMDTrace("Code", 2, "CTOID %s not found, abort request\n",
  2106.       PPCOID(fReq->createdCTOID));
  2107.         KMDTrace("Failure", 3, "CTOID %s not found, abort request\n",
  2108.       PPCOID(fReq->createdCTOID));
  2109.       
  2110.     count = write(fReq->sock, badprog, length);
  2111.     if (count < 0) {
  2112.         ErrMsg("Couldn't write err msg to user in DoCompileCreate\n");
  2113.         perror("DoCompileCreate");
  2114.     }
  2115.     /* Close the connection to runec */
  2116.     CReq[fReq->sock]        = (CompilerLoadReqPtr) NULL;
  2117.     SIRemoveSockHandler(fReq->sock, SIREAD);
  2118.     SIRemoveSockHandler(fReq->sock, SIEXCEPT);
  2119.     (void) close(fReq->sock);
  2120.     FreeRequest((GenericReqPtr) fReq);
  2121.     return;
  2122.     }
  2123.     
  2124.     if (fReq->hangAround) {    
  2125.     /* Create streams */
  2126.     MakeStreamsFromSock(fReq->sock, &fReq->theInStream,
  2127.         &fReq->theOutStream);
  2128.     } else {
  2129.     /* Use kernel std in/out */
  2130.     fReq->theInStream       = kernelStdInStream;
  2131.     fReq->theOutStream      = kernelStdOutStream;
  2132.     /* Close the connection to runec */
  2133.     CReq[fReq->sock]        = (CompilerLoadReqPtr) NULL;
  2134.     SIRemoveSockHandler(fReq->sock, SIREAD);
  2135.     SIRemoveSockHandler(fReq->sock, SIEXCEPT);
  2136.     (void) close(fReq->sock);
  2137.     };
  2138.  
  2139.     assert(fOID == fReq->createdCTOID);
  2140.     theCodeODP          = (CodeODP) OTLookup(fReq->createdCTOID);
  2141.     theCode             = theCodeODP->dataPtr;
  2142.     fReq->status        = Translating;
  2143.     createdODP          = AllocateWithOID(theCode, (OID) NULL, 
  2144.                         theCode->instanceSize);
  2145.     fReq->createdObj    = createdODP;
  2146.     if (!fReq->hangAround) {
  2147.     Map_Delete(CompilerLoadMap, (int) fReq);
  2148.     FreeRequest((GenericReqPtr) fReq);
  2149.     }
  2150.     KMDTrace("Code", 2, "Created one of %s\n", PPCOID(fReq->createdCTOID));
  2151.     if(theCode->initially.offset) {
  2152.     SSPtr p;
  2153.     /* Has an initially  (InitiallyDone does the process) */
  2154.     KMDTrace("Code", 3, "Starting its initially.\n");
  2155.     p = NewProcess();
  2156.     StartProcessAtAddr(p, createdODP, createdODP->dataPtr,
  2157.         (CodeAddr) addOffset(theCode, theCode->initially.offset));
  2158.     } else {
  2159.     createdODP->tag.setUpDone = TRUE;
  2160.     createdODP->tag.frozen = FALSE;
  2161.     if (theCode->process.offset) {
  2162.         SSPtr p;
  2163.         /* No initially, but a process -- fire it up */
  2164.         KMDTrace("Code", 3, "Starting its process\n");
  2165.         p = NewProcess();
  2166.         StartProcessAtAddr(p, createdODP, createdODP->dataPtr,
  2167.         (CodeAddr) addOffset(theCode, theCode->process.offset));
  2168.     }
  2169.     }
  2170. }
  2171.  
  2172. /**********************************************************************/
  2173. /*      ReadCompilerRequest                                           */
  2174. /**********************************************************************/
  2175.  
  2176. void ReadCompilerRequest(fReq)
  2177. register CompilerLoadReqPtr         fReq;
  2178. /* Called to read the compiler request info and start the desired object */
  2179. {
  2180.     char                            buf[17], hangAround;
  2181.     OID                             ctOID;
  2182.     int                             count, n;
  2183.     CodeODP                         theCodeODP;
  2184.     CodePtr                         theCode;
  2185.     GODP                            createdODP;
  2186.     
  2187.     count = recv(fReq->sock, buf, 16, 0);
  2188.     buf[16] = (char) 0;
  2189.     if (count != 16) {
  2190.     ErrMsg("Bad msg on compiler socket");
  2191.     if (count < 0) perror("ReadCompilerRequest");
  2192.     return;
  2193.     }
  2194.  
  2195.     KMDTrace("Code", 3, "Program load request %-11.11s\n", buf);
  2196.     n = sscanf(buf, "0x%8x%c", &ctOID, &hangAround);
  2197.     if (n != 2) {
  2198.     ErrMsg("Compiler request bad format: %s\n", buf);
  2199.     (void) close(fReq->sock);
  2200.     return;
  2201.     }
  2202.     
  2203.     fReq->hangAround = (hangAround == 'T');
  2204.     fReq->status            = Loading;
  2205.     fReq->createdCTOID      = ctOID;
  2206.     fReq->hdr.callBack      = (GenericHandlerPtr) DoCompilerCreate;
  2207.     if (LoadRequest(ctOID, (GenericReqPtr) fReq)) {
  2208.     if (fReq->hangAround) {    
  2209.         /* Create streams */
  2210.         MakeStreamsFromSock(fReq->sock, &fReq->theInStream,
  2211.         &fReq->theOutStream);
  2212.     } else {
  2213.         /* Use kernel std in/out */
  2214.         fReq->theInStream       = kernelStdInStream;
  2215.         fReq->theOutStream      = kernelStdOutStream;
  2216.         /* Close the connection to runec */
  2217.         CReq[fReq->sock]        = (CompilerLoadReqPtr) NULL;
  2218.         SIRemoveSockHandler(fReq->sock, SIREAD);
  2219.         SIRemoveSockHandler(fReq->sock, SIEXCEPT);
  2220.         (void) close(fReq->sock);
  2221.     };
  2222.     theCodeODP          = (CodeODP) OTLookup(ctOID);
  2223.     theCode             = theCodeODP->dataPtr;
  2224.     fReq->status        = Translating;
  2225.     createdODP          = AllocateWithOID(theCode, (OID) NULL, 
  2226.                             theCode->instanceSize);
  2227.     fReq->createdObj    = createdODP;
  2228.     if (!fReq->hangAround) {
  2229.         Map_Delete(CompilerLoadMap, (int) fReq);
  2230.         FreeRequest((GenericReqPtr) fReq);
  2231.     }
  2232.     KMDTrace("Code", 2, "Created one of %s\n", PPCOID(ctOID));
  2233.     if(theCode->initially.offset) {
  2234.         SSPtr p;
  2235.         /* Has an initially  (InitiallyDone does the process) */
  2236.         KMDTrace("Code", 3, "Starting initially for one of %s\n",
  2237.         PPCOID(ctOID));
  2238.         p = NewProcess();
  2239.         StartProcessAtAddr(p, createdODP, createdODP->dataPtr,
  2240.         (CodeAddr) addOffset(theCode, theCode->initially.offset));
  2241.     } else {
  2242.         createdODP->tag.setUpDone = TRUE;
  2243.         createdODP->tag.frozen = FALSE;
  2244.         if (theCode->process.offset) {
  2245.         SSPtr p;
  2246.         /* No initially, but a process -- fire it up */
  2247.         KMDTrace("Code", 3, "Starting its process\n");
  2248.         p = NewProcess();
  2249.         StartProcessAtAddr(p, createdODP, createdODP->dataPtr,
  2250.             (CodeAddr) addOffset(theCode, theCode->process.offset));
  2251.         }
  2252.     }
  2253.     }
  2254. }
  2255.  
  2256. /**********************************************************************/
  2257. /*      FLNewRequest                                                  */
  2258. /**********************************************************************/
  2259.  
  2260. int FLNewRequest()
  2261. /* A sigio has arrived for the compiler socket, so empty out all the
  2262.  * connection requests it.
  2263.  * (Note, this procedure ought to be declared void since it does not
  2264.  * return any value.)
  2265.  */
  2266. {
  2267.     register CompilerLoadReqPtr     req;
  2268.     int                             newReqSock;
  2269.     int                             reqSrcLength;
  2270.     struct sockaddr_in              reqSrc; /* for lint, use sockaddr */
  2271.  
  2272.     while(1) {
  2273.     reqSrcLength = sizeof(reqSrc);
  2274.     newReqSock = accept(CompilerSock, (struct sockaddr *) &reqSrc,
  2275.         &reqSrcLength);
  2276.     if (newReqSock < 0) {
  2277.         KMDTrace("Code", 6, "No more compiler request\n");
  2278.         if (errno == EWOULDBLOCK) return 0;
  2279.         ErrMsg("Bad status from accept %d\n", errno);
  2280.         perror("FLNewRequest");
  2281.     } else {
  2282.         /* Accept and set up a new request */
  2283.         KMDTrace("UserIO", 3, "New connection, socket #%d\n", newReqSock);
  2284.  
  2285.         /* Clean out the socket */
  2286.         KMDTrace("FixMe", 3, "Need to clean out socket\n");
  2287.     
  2288.         /* Make the request line use SIGIO */
  2289.         if (fcntl(newReqSock, F_SETFL, FASYNC | FNDELAY) < 0 ) {
  2290.         perror("newReqSock: fcntl");
  2291.         return 0; /* ought to be declared as void */
  2292.         };
  2293.     
  2294.         if (fcntl(newReqSock, F_SETOWN, getpid()) < 0 ) {
  2295.         perror("newReqSock: fcntl");
  2296.         return 0; /* should be void */
  2297.         };
  2298.  
  2299.         SISetSockHandler(newReqSock, SIREAD, (SIHandlerPtr) FLUserSIGIO);
  2300.         SISetSockHandler(newReqSock, SIEXCEPT,
  2301.         (SIHandlerPtr) FLUserSockDead);
  2302.         /* Ensure that a SIGIO occurs soon (there might already be input
  2303.            available on the socket) */
  2304.         SIEnsureSIGIO();
  2305.  
  2306.         /* Put into request list and sooner or later something will
  2307.            arrive (or the connection will die).
  2308.         */
  2309.         
  2310.         KMDTrace("Code", 3, "Program load connection request\n");
  2311.  
  2312.         req                 = mNewRequest(CompilerLoad);
  2313.         req->sock           = newReqSock;
  2314.         req->status         = Connected;
  2315.         req->createdObj     = (GODP) NULL;
  2316.         req->waitingRead    =
  2317.         req->waitingWrite   = (SSPtr) NULL;
  2318.         CReq[newReqSock]    = req;
  2319.         Map_Insert(CompilerLoadMap, (int) req, 0);
  2320.     }
  2321.     }
  2322. }
  2323.  
  2324. /* Forward */ int StreamInputSIGIO();
  2325.  
  2326. /**********************************************************************/
  2327. /*      StreamInput                                                   */
  2328. /**********************************************************************/
  2329. void StreamInput(fSock)
  2330. int              fSock;
  2331. /* The socket has become available for input */
  2332. {
  2333.     int                             sock, i, count, length;
  2334.     VectorPtr                       vector;
  2335.     SSPtr                           theProcess;
  2336.     register CompilerLoadReqPtr     req;
  2337.  
  2338.     KMDTrace("UserIO", 3, "StreamInput on socket %d\n", fSock);
  2339.     sock = fSock;
  2340.     
  2341.     for (i=0; i < 32 && (IsNULL(CReq[i]) || CReq[i]->sock != sock); i++);
  2342.     
  2343.     if (i == 32) {
  2344.     KMDTrace("UserIO", 1, "StreamInput found socket #%d disconnected\n",
  2345.         sock);
  2346.     return;
  2347.     }
  2348.     req             = CReq[i];
  2349.     sock            = req->sock;
  2350.     length          = req->lengthRead;
  2351.     vector          = req->vectorRead;
  2352.     
  2353.     /* Attempt to read */
  2354.     count = read(sock, (char *) &vector->data[0], length);
  2355.     KMDTrace("UserIO", 5, "Read returned %d bytes\n", count);
  2356.     
  2357.     if (count < 0) {
  2358.     if (errno == EWOULDBLOCK) {
  2359.         KMDTrace("UserIO", 2, "StreamInput found no data\n");
  2360.         SISetSockHandler(sock, SIREAD, (SIHandlerPtr) StreamInputSIGIO);
  2361.         return;
  2362.     }
  2363.     KMDTrace("UserIO", 3, "Read error %d\n", errno);
  2364.     perror("StreamRead");
  2365.     /* Should cleanup socket */
  2366.     fail(req->waitingRead);
  2367.     req->waitingRead = (SSPtr) NULL;
  2368.     req->vectorRead = (VectorPtr) NULL;
  2369.     return;
  2370.     }
  2371.     theProcess                  = req->waitingRead;
  2372.     theProcess->resultBrand     = DataBrand;
  2373.     theProcess->regs.arg1       = count - 1; /* supposed to return maxValidIndex */
  2374.     req->waitingRead            = (SSPtr) NULL;
  2375.     req->vectorRead             = (VectorPtr) NULL;
  2376.     schedule(theProcess);
  2377.     if (count == 0) {
  2378.     CloseInStream(sock);
  2379.     }
  2380. }
  2381.  
  2382. /**********************************************************************/
  2383. /*      StreamInputSIGIO                                              */
  2384. /**********************************************************************/
  2385. /*ARGSUSED*/
  2386. int StreamInputSIGIO(fKind, fSock)
  2387. int         fKind, fSock;
  2388. {
  2389.     SIRemoveSockHandler(fSock, SIREAD);
  2390.     HoldSigs();
  2391.     QueueTask(StreamInput, (char *) fSock);
  2392.     ReleaseSigs();
  2393.     return 0; /* ought to be delcared as void */
  2394. }
  2395.  
  2396. /**********************************************************************/
  2397. /*      StreamRead                                                    */
  2398. /**********************************************************************/
  2399.  
  2400. /*Kernel Call */
  2401. void StreamRead(fSock, fCharVector)
  2402. int                 fSock;
  2403. GODP                fCharVector;
  2404. /* Asynchronously read from the socket returning up to fCharVector.upb
  2405.    characters in fCharVector */
  2406. {
  2407.     int                             sock, i, count, length;
  2408.     VectorPtr                       vector;
  2409.     SSPtr                           theProcess;
  2410.     register CompilerLoadReqPtr     req;
  2411.     AbConPtr                        charVectorAbCon;
  2412.     
  2413.     theProcess = preemptRunning();
  2414.  
  2415.     sock = fSock;
  2416.     
  2417.     KMDTrace("UserIO", 4, "StreamRead, sock = %d in %s\n", fSock,
  2418.     PPSSPlace(theProcess));
  2419.  
  2420.     for (i=0; i < 32 && (IsNULL(CReq[i]) || CReq[i]->sock != sock); i++);
  2421.  
  2422.     if (i == 32) {
  2423.     KMDTrace("UserIO", 2, "Read attempt on disconnect socket #%d\n",sock);
  2424.     fail(theProcess);
  2425.     return;
  2426.     }
  2427.     req = CReq[i];
  2428.  
  2429.     if (fCharVector == (GODP) EMNIL) {
  2430.     KMDTrace("UserIO", 2, "Char vector is NIL in StreamRead.\n");
  2431.     fail(theProcess);
  2432.     return;
  2433.     }
  2434.  
  2435.     if (!fCharVector->tag.isResident) {
  2436.     KMDTrace("UserIO", 2, "Char vector is non-resident.\n");
  2437.     charVectorAbCon = OIDOIDOIDToAbCon(
  2438.         fCharVector->dataPtr->myCodePtr->ownAbstractType,
  2439.         OIDOfBuiltin(B_INSTAT, ANYINDEX),
  2440.         fCharVector->dataPtr->myCodePtr->ownOID);
  2441.     unavail(theProcess, fCharVector, charVectorAbCon);
  2442.     return;
  2443.     }
  2444.  
  2445.     if (fCharVector->tag.global) {
  2446.     vector = (VectorPtr) fCharVector->dataPtr;
  2447.     } else vector = (VectorPtr) fCharVector;
  2448.  
  2449.     length                  = vector->upb+1;
  2450.     if (length <= 0) {
  2451.     KMDTrace("UserIO", 2,
  2452.         "Char vector length is not postive in StreamRead.\n");
  2453.     fail(theProcess);
  2454.     return;
  2455.     }
  2456.     
  2457.     /* Check for other readers */
  2458.     if (NonNULL(req->waitingRead)) {
  2459.     KMDTrace("UserIO", 2, "Read socket busy.\n");
  2460.     fail(theProcess);
  2461.     return;
  2462.     }
  2463.     
  2464.     /* Attempt to read */
  2465.     count = read(sock, (char *) &vector->data[0], length);
  2466.     KMDTrace("UserIO", 5, "Read returned %d bytes\n", count);
  2467.     
  2468.     if (count < 0) {
  2469.     if (errno == EWOULDBLOCK) {
  2470.         KMDTrace("UserIO", 3, "StreamRead blocking %s at %s\n",
  2471.         PPPOID(theProcess->processOID), PPSSPlace(theProcess));
  2472.         req->vectorRead         = vector;
  2473.         req->lengthRead         = length;
  2474.         req->waitingRead        = theProcess;
  2475.             theProcess->status.rs   = SSReadIOWait;
  2476.             KMDTrace("LineNumber", 3, "%s blocking on i/o read in %s\n",
  2477.         PPPOID(theProcess->processOID), PPSSPlace(theProcess));
  2478.         SISetSockHandler(req->sock, SIREAD,
  2479.         (SIHandlerPtr) StreamInputSIGIO);
  2480.         return;
  2481.     }
  2482.     KMDTrace("UserIO", 3, "Read error %d\n", errno);
  2483.     perror("StreamRead");
  2484.     /* Should cleanup socket */
  2485.     KMDTrace("FixMe", 3, "Should clean out socket #%d\n", sock);
  2486.     fail(theProcess);
  2487.     return;
  2488.     }
  2489.     theProcess->resultBrand = DataBrand;
  2490.     theProcess->regs.arg1 = count - 1; /* supposed to return maxvalidindex */
  2491.     schedule(theProcess);
  2492.     if (count == 0) {
  2493.     CloseInStream(sock);
  2494.     }
  2495. }
  2496.  
  2497. /**********************************************************************/
  2498. /*      StreamOutput                                                  */
  2499. /**********************************************************************/
  2500. void StreamOutput(fSock)
  2501. int              fSock;
  2502. /* The socket has become writable, now try writing on it. */
  2503. {
  2504.     int                             sock, i, count, length;
  2505.     VectorPtr                       vector;
  2506.     SSPtr                           theProcess;
  2507.     register CompilerLoadReqPtr     req;
  2508.  
  2509.     KMDTrace("UserIO", 3, "StreamOutput on socket %d\n", fSock);
  2510.     sock = fSock;
  2511.     
  2512.     for (i=0; i < 32 && (IsNULL(CReq[i]) || CReq[i]->sock != sock); i++);
  2513.     
  2514.     if (i == 32) {
  2515.     KMDTrace("UserIO", 2, "StreamOutput found stream #%d disconnected\n",
  2516.         sock);
  2517.     return;
  2518.     }
  2519.     req = CReq[i];
  2520.     length          = req->lengthWrite;
  2521.     vector          = req->vectorWrite;
  2522.     assert(NonNULL(req->waitingWrite));
  2523.     
  2524.     /* Attempt to write */
  2525.     count = write(sock, (char *) &vector->data[0], length);
  2526.     KMDTrace("UserIO", 5, "Write returned %d bytes\n", count);
  2527.    
  2528.     if (count < 0) {
  2529.     if (errno == EWOULDBLOCK) {
  2530.         KMDTrace("UserIO", 3, "StreamOutput would block\n");
  2531.         return;
  2532.     }
  2533.     KMDTrace("UserIO", 1, "Write error %d\n", errno);
  2534.     perror("StreamOutput");
  2535.     /* Should cleanup socket */
  2536.     fail(req->waitingWrite);
  2537.     req->waitingWrite = (SSPtr) NULL;
  2538.     req->vectorWrite = (VectorPtr) NULL;
  2539.     return;
  2540.     }
  2541.     theProcess              = req->waitingWrite;
  2542.     req->waitingWrite       = (SSPtr) NULL;
  2543.     req->vectorWrite        = (VectorPtr) NULL;
  2544.     theProcess->resultBrand = DataBrand;
  2545.     theProcess->regs.arg1   = count;
  2546.     schedule(theProcess);
  2547. }
  2548.  
  2549. /**********************************************************************/
  2550. /*      StreamOutputSIGIO                                             */
  2551. /**********************************************************************/
  2552. /*ARGSUSED*/
  2553. int StreamOutputSIGIO(fKind, fSock)
  2554. int         fKind, fSock;
  2555. {
  2556.     SIRemoveSockHandler(fSock, SIWRITE);
  2557.     HoldSigs();
  2558.     QueueTask(StreamOutput, (char *) fSock);
  2559.     ReleaseSigs();
  2560. }
  2561.  
  2562. /**********************************************************************/
  2563. /*      StreamWrite                                                   */
  2564. /**********************************************************************/
  2565.  
  2566. /* Kernel call */
  2567. void StreamWrite(fSock, fCharVector, fCount)
  2568. int                 fSock;
  2569. GODP                fCharVector;
  2570. int                 fCount;
  2571. {
  2572.     int                             sock, i, count, length;
  2573.     VectorPtr                       vector;
  2574.     register CompilerLoadReqPtr     req;
  2575.     AbConPtr                        charVectorAbCon;
  2576.  
  2577.     sock = fSock;
  2578.  
  2579.     KMDTrace("UserIO", 3, "StreamWrite on socket #%d\n", sock);
  2580.     
  2581.     for (i=0; i < 32 &&  (IsNULL(CReq[i]) || CReq[i]->sock != sock); i++);
  2582.     if (i == 32) {
  2583.     KMDTrace("UserIO", 2, "Write attempt on disconnect socket #%d\n",
  2584.         sock);
  2585.     fail(preemptRunning());
  2586.     return;
  2587.     }
  2588.     req = CReq[i];
  2589.  
  2590.     if (fCharVector == (GODP) EMNIL) {
  2591.     KMDTrace("UserIO", 2, "Char vector is NIL in StreamWrite.\n");
  2592.     fail(preemptRunning());
  2593.     return;
  2594.     }
  2595.  
  2596.     if (!fCharVector->tag.isResident) {
  2597.     KMDTrace("UserIO", 2, "Char vector is non-resident.\n");
  2598.     charVectorAbCon = OIDOIDOIDToAbCon(
  2599.         fCharVector->dataPtr->myCodePtr->ownAbstractType,
  2600.         OIDOfBuiltin(B_INSTAT, ANYINDEX),
  2601.         fCharVector->dataPtr->myCodePtr->ownOID);
  2602.     unavail(preemptRunning(), fCharVector, charVectorAbCon);
  2603.     return;
  2604.     }
  2605.  
  2606.     if (fCharVector->tag.global) {
  2607.     vector = (VectorPtr) fCharVector->dataPtr;
  2608.     } else vector = (VectorPtr) fCharVector;
  2609.  
  2610.     length                  = vector->upb+1;
  2611.     if (length <= 0) {
  2612.     KMDTrace("UserIO", 2,
  2613.         "Char vector length is not postive in StreamWrite.\n");
  2614.     fail(preemptRunning());
  2615.     return;
  2616.     }
  2617.     
  2618.     if (length < fCount) {
  2619.     KMDTrace("UserIO", 2,
  2620.         "Char vector too short in StreamWrite.\n");
  2621.     fail(preemptRunning());
  2622.     return;
  2623.     }
  2624.     
  2625.     /* Check for other writers */
  2626.     if (NonNULL(req->waitingWrite)) {
  2627.     KMDTrace("UserIO", 2, "Another process is writing on socket.\n");
  2628.     fail(preemptRunning());
  2629.     return;
  2630.     }
  2631.     
  2632.     /* Attempt to write */
  2633.     count = write(sock, (char *) &vector->data[0], fCount);
  2634.     KMDTrace("UserIO", 5, "Write returned %d byte%s\n", count,
  2635.     mPLURAL(count));
  2636.  
  2637.     if (count < 0) {
  2638.     if (errno == EWOULDBLOCK) {
  2639.         KMDTrace("UserIO", 3, "StreamWrite blocking %s at %s\n",
  2640.         PPPOID(currentSSP->processOID), PPSSPlace(currentSSP));
  2641.         req->vectorWrite         = vector;
  2642.         req->lengthWrite         = length;
  2643.         req->waitingWrite        = preemptRunning();
  2644.             req->waitingWrite->status.rs = SSWriteIOWait;
  2645.             KMDTrace("LineNumber", 3, "%s blocking on i/o write in %s\n",
  2646.         PPPOID(req->waitingWrite->processOID),
  2647.         PPSSPlace(req->waitingWrite));
  2648.         SISetSockHandler(req->sock, SIWRITE,
  2649.         (SIHandlerPtr) StreamOutputSIGIO);
  2650.         return;
  2651.     }
  2652.     KMDTrace("UserIO", 3, "Write error %d\n", errno);
  2653.     perror("StreamWrite");
  2654.     /* Should cleanup socket */
  2655.     fail(preemptRunning());
  2656.     return;
  2657.     }
  2658.  
  2659.     currentSSP->resultBrand = DataBrand;
  2660.     currentSSP->regs.arg1 = count;
  2661.  
  2662. }
  2663.  
  2664. /**********************************************************************/
  2665. /*      CloseInStream                                                 */
  2666. /**********************************************************************/
  2667. /* Kernel call */
  2668. void CloseInStream(fSock)
  2669. int             fSock;
  2670. {
  2671.     int                     i, sock;
  2672.     CompilerLoadReqPtr      req;
  2673.  
  2674.     KMDTrace("UserIO", 3, "CloseInStream for sock %d\n", fSock);
  2675.     sock = fSock;
  2676.  
  2677.     if (fSock == fileno(stdin)) {
  2678.     KMDTrace("UserIO", 3, "Close on kernel stdin ignored.\n");
  2679.     return;
  2680.     }
  2681.  
  2682.     for (i=0; i < 32 && (IsNULL(CReq[i]) || CReq[i]->sock != sock); i++);
  2683.  
  2684.     if (i == 32) {
  2685.     KMDTrace("UserIO", 2, "Close attempt on disconnected socket #%d\n",
  2686.         sock);
  2687.     return;
  2688.     }
  2689.  
  2690.     req = CReq[i];
  2691.     if (IsNULL(currentSSP)) {
  2692.     KMDTrace("UserIO", 2, "CloseInStream #%d: Null currentSSP\n", sock);
  2693.     } else if (currentSSP->regs.b != req->theInStream) {
  2694.     KMDTrace("UserIO", 2,
  2695.         "CloseInStream attempt on reconnected socket #%d\n",
  2696.         sock);
  2697.     return;
  2698.     }
  2699.  
  2700.     req->theInStream       = (GODP) NULL;
  2701.     if (NonNULL(req->waitingRead)) {
  2702.     fail(preemptRunning());
  2703.     }
  2704.     
  2705.     if (req->theOutStream == (GODP) NULL) {
  2706.     CReq[i] = (CompilerLoadReqPtr) NULL;
  2707.     FreeRequest((GenericReqPtr) req);
  2708.     (void) close(sock);
  2709.     }
  2710. }
  2711.  
  2712. /**********************************************************************/
  2713. /*      CloseOutStream                                                */
  2714. /**********************************************************************/
  2715. /* Kernel call */
  2716. void CloseOutStream(fSock)
  2717. int             fSock;
  2718. {
  2719.     int                     i, sock;
  2720.     CompilerLoadReqPtr      req;
  2721.     
  2722.     KMDTrace("UserIO", 3, "CloseOutStream for sock %d\n", fSock);
  2723.  
  2724.     sock = fSock;
  2725.  
  2726.     if (fSock == fileno(stdout)) {
  2727.     KMDTrace("UserIO", 3, "Close on kernel stdout ignored.\n");
  2728.     return;
  2729.     }
  2730.  
  2731.     for (i=0; i < 32 && (IsNULL(CReq[i]) || CReq[i]->sock != sock); i++);
  2732.  
  2733.     if (i == 32) {
  2734.     KMDTrace("UserIO", 2, "Close attempt on disconnected socket #%d\n",
  2735.         sock);
  2736.     return;
  2737.     }
  2738.  
  2739.     req = CReq[i];
  2740.     if (IsNULL(currentSSP)) {
  2741.     KMDTrace("UserIO", 2, "CloseOutStream #%d: Null currentSSP\n", sock);
  2742.     } else if (currentSSP->regs.b != req->theOutStream) {
  2743.     KMDTrace("UserIO", 2,
  2744.         "CloseOutStream attempt on reconnected socket #%d\n",
  2745.         sock);
  2746.     return;
  2747.     }
  2748.  
  2749.     req->theOutStream       = (GODP) NULL;
  2750.     if (NonNULL(req->waitingRead)) {
  2751.     fail(preemptRunning());
  2752.     }
  2753.     
  2754.     if (req->theInStream == (GODP) NULL) {
  2755.     CReq[i] = (CompilerLoadReqPtr) NULL;
  2756.     FreeRequest((GenericReqPtr) req);
  2757.     (void) close(sock);
  2758.     }
  2759. }
  2760. /**********************************************************************/
  2761. /**********************************************************************/
  2762. void GetCodeLoadRootSet(fRootSet)
  2763. Set             fRootSet;
  2764. {
  2765. }
  2766.  
  2767. /**********************************************************************/
  2768. /**********************************************************************/
  2769.  
  2770. /**********************************************************************/
  2771. /*      FLSendCode                                                    */
  2772. /**********************************************************************/
  2773.  
  2774. /* Snapshot for testing the sending of code to someone */
  2775. /* Snapshot */
  2776. void FLSendCode(fLNN, fOID)
  2777. int             fLNN;
  2778. char           *fOID;
  2779. /* Send the Code given by OID to the node given by fLNN */
  2780. /* Note: OID is represented as ASCII */
  2781. {
  2782.     CodeODP         theCodeODP;
  2783.     OID             theCodeOID;
  2784.     LMHandle        myLMHandle;
  2785.     CodeTransferItem item;
  2786.     HOTSRecord     *hotsPtr;
  2787.     DotoFilePtr     dotoptr;
  2788.  
  2789.     if (sscanf(fOID, "%x", &theCodeOID) != 1) {
  2790.     KMDPrint("Bad OID\n");
  2791.     return;
  2792.     }
  2793.  
  2794.     theCodeODP = (CodeODP) OTLookup(theCodeOID);
  2795.     KMDTrace("Code", 3, "Sending Code 0x%08x to host %d\n", theCodeOID, fLNN);
  2796.     if ((theCodeODP == NULL) || (theCodeODP->tag.tag != CodeODTag)) {
  2797.     KMDPrint("FLSendCode: no such code OID 0x%08x\n", theCodeOID);
  2798.     return;
  2799.     }
  2800.     if ((!mSUCCESS(HOTSSearchPtr(fLNN, &hotsPtr))) || (hotsPtr->NodeStat !=
  2801.     Alive) && (hotsPtr->NodeStat != Booting)) {
  2802.     KMDPrint("Node %d down.\n", fLNN);
  2803.     return;
  2804.     }
  2805.     dotoptr = (DotoFilePtr)
  2806.     addOffset(theCodeODP->dataPtr, -sizeof(dotoptr->header));
  2807.     LMStartMsg(&myLMHandle, KMSG_EmKernel, EMKM_FLAcceptCode, fLNN);
  2808.     item.hdr.itemTag = CodeTransferITag;
  2809.     item.hdr.size = N_STROFF(dotoptr->header) +
  2810.      ( * ( (int *)addOffset(dotoptr, N_STROFF(dotoptr->header) ) ) ) +
  2811.      sizeof(ItemHdr);
  2812.     LMPutData(&myLMHandle, &item, sizeof(item));
  2813.     LMPutData(&myLMHandle, dotoptr, item.hdr.size - sizeof(item));
  2814.     if (!mSUCCESS(LMSendMsg(&myLMHandle))) {
  2815.     KMDPrint("Could not send msg to host %d\n", fLNN);
  2816.     }
  2817. }
  2818.  
  2819. /**********************************************************************/
  2820. /*      FLCodeLoadMap                                                 */
  2821. /**********************************************************************/
  2822.  
  2823. /*Snapshot*/
  2824. void FLCodeLoadMap()
  2825. {
  2826.     CodeLoadReqPtr          req;
  2827.     OID                     theOID;
  2828.     KMDPrint("CodeLoadMap dump for node #%d\n", GetLNN());
  2829.     if (Map_Count(CodeLoadMap) == 0) {
  2830.     KMDPrint("No outstanding code load requests.\n");
  2831.     return;
  2832.     }
  2833.     KMDPrint("Code OID \tStatus\n");
  2834.     Map_For(CodeLoadMap, theOID, req);
  2835.     KMDPrint("C%08x\t%s\n", theOID,
  2836.         req->status == DoNotKnow ?      "Location unknown" :
  2837.         req->status == FoundSomewhere ? "Located" :
  2838.         req->status == LoadRequested  ? "Code load in progress" :
  2839.         req->status == Transferred ?    "Code transferred to here" :
  2840.         req->status == Translated ?     "Code load done":
  2841.         "BAD STATUS");
  2842.     Map_Next
  2843. }
  2844.  
  2845. /**********************************************************************/
  2846. /*      FLRemoteLoadMap                                               */
  2847. /**********************************************************************/
  2848.  
  2849. /*Snapshot*/
  2850. void FLRemoteLoadMap()
  2851. {
  2852.     CodeLoadReqPtr          req;
  2853.     OID                     theOID;
  2854.     KMDPrint("RemoteLoadMap dump for node #%d\n", GetLNN());
  2855.     if (Map_Count(CodeLoadMap) == 0) {
  2856.     KMDPrint("No outstanding code load requests.\n");
  2857.     return;
  2858.     }
  2859.     KMDPrint("Code OID \tStatus\n");
  2860.     Map_For(RemoteLoadMap, theOID, req);
  2861.     KMDPrint("C%08x\t%s\n", theOID,
  2862.         req->status == DoNotKnow ?      "Location unknown" :
  2863.         req->status == FoundSomewhere ? "Located" :
  2864.         req->status == LoadRequested  ? "Code load in progress" :
  2865.         req->status == Transferred ?    "Code transferred to here" :
  2866.         req->status == Translated ?     "Code load done":
  2867.         "BAD STATUS");
  2868.     Map_Next
  2869. }
  2870.  
  2871. /**********************************************************************/
  2872. /*      FLCompilerLoadMap                                             */
  2873. /**********************************************************************/
  2874.  
  2875. /*Snapshot*/
  2876. void FLCompilerLoadMap()
  2877. {
  2878.     CompilerLoadReqPtr      req;
  2879.     int                     dummy;
  2880.     
  2881.     KMDPrint("CompilerLoadMap dump for node #%d\n", GetLNN());
  2882.     if (Map_Count(CompilerLoadMap) == 0) {
  2883.     KMDPrint("No outstanding code load requests.\n");
  2884.     return;
  2885.     }
  2886.     KMDPrint("Compiler OID \tStatus\n");
  2887.     Map_For(CompilerLoadMap, req, dummy);
  2888.     KMDPrint("C%08x\t%s\tone of %s\n", req->createdCTOID,
  2889.         req->status == Connected ?      "Connected" :
  2890.         req->status == Loading ?        "Loading" :
  2891.         req->status == Translating ?    "Loaded" :
  2892.         "BAD STATUS", PPCOID(req->createdCTOID));
  2893.     if (req->status == Translating) {
  2894.         if (NonNULL(req->waitingRead)) {
  2895.         KMDPrint("\t\t%s blocked on read at %s\n",
  2896.             PPPOID(req->waitingRead->processOID),
  2897.             PPSSPlace(req->waitingRead));
  2898.         }
  2899.         if (NonNULL(req->waitingWrite)) {
  2900.         KMDPrint("\t\t%s blocked on write at %s\n",
  2901.             PPPOID(req->waitingWrite->processOID),
  2902.             PPSSPlace(req->waitingWrite));
  2903.         }
  2904.     }
  2905.     Map_Next
  2906. }
  2907.  
  2908. /**********************************************************************/
  2909. /*      FLCheatingLoadMap                                             */
  2910. /**********************************************************************/
  2911.  
  2912. /*Snapshot*/
  2913. void FLCheatingLoadMap()
  2914. {
  2915.     CheatingLoadReqPtr      req;
  2916.     OID                     theOID;
  2917.     KMDPrint("CheatingLoadMap dump for node #%d\n", GetLNN());
  2918.     if (Map_Count(CheatingLoadMap) == 0) {
  2919.     KMDPrint("No outstanding code load requests.\n");
  2920.     return;
  2921.     }
  2922.     KMDPrint("Code OID \tStatus\n");
  2923.     Map_For(CheatingLoadMap, theOID, req);
  2924.     KMDPrint("C%08x\t%s\n", theOID,
  2925.         req->status == DoNotKnow ?      "Unknown" :
  2926.         req->status == FoundSomewhere ? "Located" :
  2927.         req->status == LoadRequested  ? "Code load in progress" :
  2928.         req->status == Transferred ?    "Code transferred to here" :
  2929.         req->status == Translated ?     "Code load done":
  2930.         "BAD STATUS");
  2931.     Map_Next
  2932. }
  2933.  
  2934. /**********************************************************************/
  2935. /*      FLUnknownATAbConMap                                           */
  2936. /**********************************************************************/
  2937.  
  2938. /*Snapshot*/
  2939. void FLUnknownATAbConMap()
  2940. {
  2941.     Set                     theSet;
  2942.     OID                     theATOID;
  2943.     AbConPtr                theAbConPtr;
  2944.     
  2945.     KMDPrint("UnknownATAbConMap dump for node #%d\n", GetLNN());
  2946.     if (Map_Count(UnknownATAbConMap) == 0) {
  2947.     KMDPrint("No AbCons with non-resident ATs.\n");
  2948.     return;
  2949.     }
  2950.     KMDPrint("AT OID \tCT OID\n");
  2951.     Map_For(UnknownATAbConMap, theATOID, theSet);
  2952.     KMDPrint("%s\n", PPCOID(theATOID));
  2953.         Set_For(theSet, theAbConPtr);
  2954.             KMDPrint("\t%s\n", PPCOID(theAbConPtr->CodeOID));
  2955.     Set_Next
  2956.     Map_Next
  2957. }
  2958.  
  2959. /**********************************************************************/
  2960. /*      FLUnknownATAbConMap                                           */
  2961. /**********************************************************************/
  2962.  
  2963. /*Snapshot*/
  2964. void FLAbConMap96()
  2965. {
  2966.     OID                     theATOID, theRestrictOID, theCTOID;
  2967.     AbConPtr                theAbConPtr;
  2968.     
  2969.     KMDPrint("AbConMap96 dump for node #%d\n", GetLNN());
  2970.     if (Map_Count(AbConMap96) == 0) {
  2971.     KMDPrint("No AbCons.\n");
  2972.     return;
  2973.     }
  2974.     Map96_For(AbConMap96, theATOID, theRestrictOID, theCTOID, theAbConPtr);
  2975.         KMDPrint("Abcon @ 0x%06x\n:", theAbConPtr);
  2976.         KMDPrint("\tAT:\t%s\n", PPCOID(theATOID));
  2977.         KMDPrint("\trAT:\t%s\n", PPCOID(theRestrictOID));
  2978.     KMDPrint("\tCT:\t%s\n", PPCOID(theCTOID));
  2979.     Map96_Next
  2980. }
  2981.  
  2982. /**********************************************************************/
  2983. /*      FLInit                                                        */
  2984. /**********************************************************************/
  2985.  
  2986. void FLInit(fPort)
  2987. int         fPort;
  2988. {
  2989.     register int        i; 
  2990.     KMDSetSnap(FLLoadFile);
  2991.     KMDSetSnap(FLSendCode);
  2992.     KMDSetSnap(FLCreateOneOfCTOID);
  2993.     KMDSetSnap(FLCodeLoadMap);
  2994.     KMDSetSnap(FLCompilerLoadMap);
  2995.     KMDSetSnap(FLCheatingLoadMap);
  2996.     KMDSetSnap(FLRemoteLoadMap);
  2997.     KMDSetSnap(FLUnknownATAbConMap);
  2998.     KMDSetSnap(FLAbConMap96);
  2999.  
  3000.     KMDSetTrace(AbCon);
  3001.     KMDSetTrace(View);
  3002.     KMDSetTrace(Code);
  3003.     KMDSetTrace(Translate);
  3004.     KMDSetTrace(UserIO);
  3005.  
  3006.     KMDTrace("Code", 3, "File Load Init, port no = %d.\n", ntohs(fPort));
  3007.     CompilerSock = socket(AF_INET, SOCK_STREAM, 0);
  3008.     if (CompilerSock < 0) {
  3009.     perror("FLInit: socket");
  3010.     abort();
  3011.     };
  3012.     compilerSocket.sin_port = fPort;
  3013.     if (bind(CompilerSock, &compilerSocket, sizeof(compilerSocket)) < 0) {
  3014.     perror("FLInit: bind(CompilerSock)");
  3015.     if (errno == EADDRINUSE) {
  3016.         int retry;
  3017.         /* The socket is already in use --  wait for it to
  3018.            get unbound.  UNIX does this after a certain time has
  3019.            elapsed -- if a new kernel is rebooted right after
  3020.            a previous one has crashed, the bind may fail so
  3021.            retry it a number of times */
  3022.         for (retry = 1; retry < 20; retry++) {
  3023.         printf
  3024.           ("Waiting %d seconds for UNIX to clean up previous bind.\n",
  3025.           retry*retry);
  3026.         sleep ((unsigned) retry*retry);
  3027.         if (bind(CompilerSock, &compilerSocket,
  3028.             sizeof(compilerSocket)) < 0)
  3029.         {
  3030.             perror("FLInit: bind retry");
  3031.             if (errno != EADDRINUSE) break;
  3032.         } else goto ok;
  3033.         }
  3034.     }
  3035.     abort();
  3036.     }
  3037.  
  3038. ok:
  3039.     if (fcntl(CompilerSock, F_SETFL, FASYNC | FNDELAY ) < 0) {
  3040.     perror("FLInit: first fcntl");
  3041.     abort();
  3042.     };
  3043.     if (fcntl(CompilerSock, F_SETOWN, getpid()) < 0) {
  3044.     perror("FLInit: second fcntl");
  3045.     abort();
  3046.     };
  3047. #ifdef SO_DONTLINGER
  3048.     setsockopt(CompilerSock, SOL_SOCKET, SO_DONTLINGER, 0, 0);
  3049. #endif
  3050.     
  3051.     check(listen(CompilerSock, 5));
  3052.  
  3053.     for (i = 0; i < MAXSOCK; i++) CReq[i] = (CompilerLoadReqPtr) NULL;
  3054.     
  3055.     SISetSockHandler(CompilerSock, SIREAD, (SIHandlerPtr) FLNewRequest);
  3056.     SISetSockHandler(CompilerSock, SIEXCEPT,
  3057.     (SIHandlerPtr) FLCompilerSockDead);
  3058.     
  3059.     MMDefineMsgHandler(KMSG_EmKernel, EMKM_FLAcceptCode, AcceptCode, NULL);
  3060.     
  3061.     SetItemHandler(RemoteLoadReqITag, RemoteLoadItemHandler);
  3062.  
  3063.     CodeLoadMap         = Map_Create();
  3064.     CompilerLoadMap     = Map_Create();
  3065.     CheatingLoadMap     = Map_Create();
  3066.     RemoteLoadMap       = Map_Create();
  3067.     UnknownATAbConMap   = Map_Create();
  3068.     AbConMap96          = Map96_CreateSized(INITIALABCONMAP96SIZE);
  3069.     KMDTrace("AbCon", 5, "InitialAbConMap96 size: %d\n",
  3070.     INITIALABCONMAP96SIZE);
  3071. }
  3072.