Changeset 2830 for trunk/kLdr/kLdr.c
- Timestamp:
- Oct 23, 2006, 10:53:11 PM (19 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/kLdr/kLdr.c
r2826 r2830 49 49 #include "kLdrHlp.h" 50 50 51 #if 052 /*******************************************************************************53 * Global Variables *54 *******************************************************************************/55 /** Pointer to the head module (the executable).56 * (This is exported, so no prefix.) */57 PKLDRMOD kLdrModuleHead = NULL;58 /** Pointer to the tail module.59 * (This is exported, so no prefix.) */60 PKLDRMOD kLdrModuleTail = NULL;61 /** The Library search path. */62 char kLdrLibraryPath[4096];63 /** The executable flags. */64 uint32_t kLdrFlags;65 /** Bootstrap stack and temporary space. */66 char abStack[8192];67 /** Set if we've initialized the loader. */68 int fInitialized = 0;69 70 51 71 52 /******************************************************************************* 72 * Internal Functions*53 * Header Files * 73 54 *******************************************************************************/ 74 static int kldrInit(void); 75 static int kldrTerm(void); 76 77 78 /** 79 * Initialize the loader.55 /** Flag indicating whether we've initialized the loader or not. 56 * 57 * 0 if not initialized. 58 * -1 if we're initializing or terminating. 59 * 1 if we've successfully initialized it. 60 * -2 if initialization failed. 80 61 */ 81 int kldrInit(void) 82 { 83 if (fInitialized) 84 return 0; 85 /** @todo */ 86 return 0; 87 } 88 89 90 91 void kldrLoadExe(PKLDREXEARGS pArgs) 92 { 93 /* 94 * Copy the arguments into the globals and do load init. 95 */ 96 kLdrFlags = pArgs->fFlags; 97 kLdrMemCopy(kLdrLibraryPath, pArgs->szLibPath, KLDR_MIN(sizeof(pArgs->szLibPath), sizeof(kLdrLibraryPath))); 98 int rc = kldrInit(); 99 if (rc) 100 kldrFailure(rc, "kLdr: Init failure, rc=%d\n", rc); 101 102 /* 103 * Open the executable module. 104 */ 105 PKLDRMOD pExe; 106 kldrOpenExe(pArgs->szExecutable, &pExe); 107 108 /* Map the segments. */ 109 kldrModMapSegments(pExe); 110 111 /* 112 * This is the point where we switch to the executable 113 * stack, allocating it if necessary. 114 */ 115 void *pvBottom; 116 kldrModSetupStack(pExe, &pvBottom); 117 kldrLoadExecSwitchStack(pvBottom); 118 } 119 120 121 void kldrLoadExeOnNewStack(void) 122 { 123 /* 124 * Load all dependant modules. 125 */ 126 PKLDRMOD pCur; 127 do for (pCur = kLdrModuleHead; pCur; pCur = pCur->pNext) 128 { 129 if (pCur->enmState >= KLDRSTATE_DEPS) 130 continue; 131 kldrModLoadDeps(pCur); 132 } 133 while (pCur); 134 135 /* 136 * Do fixups (FIFO). 137 */ 138 for (pCur = kLdrModuleHead; pCur; pCur = pCur->pNext) 139 { 140 if (pCur->enmState >= KLDRSTATE_FIXED) 141 continue; 142 kldrModFixup(pCur, 0); 143 } 144 145 /* 146 * Do module initialization. 147 */ 148 for (pCur = kLdrModuleTail; pCur != kLdrModuleTail; pCur = pCur->pPrev) 149 { 150 if (pCur->enmState >= KLDRSTATE_INITED) 151 continue; 152 kldrModCallInit(pCur); 153 } 154 155 /* 156 * Get the executable start address and commit the work that's been done. 157 */ 158 void *pvEntry; 159 kldrModGetExeEntry(&pvEntry); 160 161 for (pCur = kLdrModuleHead; pCur; pCur = pCur->pNext) 162 if (pCur->enmState == KLDRSTATE_INITED) 163 pCur->enmState = KLDRSTATE_LOADED; 164 165 kldrSemRelease(); 166 167 /* 168 * We're now ready for starting the executable code. 169 */ 170 kldrOSStartExe(pLdrModuleHead, pvEntry); 171 } 172 173 174 int kLdrLoadDll(const char *pszFilename, unsigned fFlags, void *pvmod) 175 { 176 177 return -1; 178 } 179 62 static int volatile g_fInitialized; 180 63 181 64 182 65 183 66 /** 184 * Panic / failure 185 * 186 * @returns rc if we're in a position where we can return. 187 * @param rc Return code. 188 * @param pszFormat Message string. Limited fprintf like formatted. 189 * @param ... Message string arguments. 67 * Initializes the loader. 68 * @returns 0 on success, non-zero OS status code on failure. 190 69 */ 191 int kldr Failure(int rc, const char *pszFormat, ...)70 int kldrInit(void) 192 71 { 193 kldrExit(1); 72 int rc; 73 74 /* check we're already good. */ 75 if (g_fInitialized == 1) 76 return 0; 77 78 /* a tiny serialization effort. */ 79 for (;;) 80 { 81 if (g_fInitialized == 1) 82 return 0; 83 if (g_fInitialized == -2) 84 return -1; 85 /** @todo atomic test and set if we care. */ 86 if (g_fInitialized == 0) 87 { 88 g_fInitialized = -1; 89 break; 90 } 91 kldrHlpSleep(1); 92 } 93 94 /* 95 * Do the initialization. 96 */ 97 rc = kldrHlpHeapInit(); 98 if (!rc) 99 { 100 rc = kldrHlpSemInit(); 101 if (!rc) 102 { 103 g_fInitialized = 1; 104 return 0; 105 } 106 kldrHlpHeapTerm(); 107 } 108 g_fInitialized = -2; 194 109 return rc; 195 110 } 196 111 197 #endif 112 113 /** 114 * Terminates the loader. 115 */ 116 void kldrTerm(void) 117 { 118 /* can't terminate unless it's initialized. */ 119 if (g_fInitialized != 1) 120 return; 121 g_fInitialized = -1; 122 123 /* 124 * Do the termination. 125 */ 126 kldrHlpSemTerm(); 127 kldrHlpHeapTerm(); 128 129 /* done */ 130 g_fInitialized = 0; 131 } 132
Note:
See TracChangeset
for help on using the changeset viewer.