Changeset 2830
- Timestamp:
- Oct 23, 2006, 10:53:11 PM (19 years ago)
- Location:
- trunk/kLdr
- Files:
-
- 4 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 * * 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 * . 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 kldr) 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 -
trunk/kLdr/kLdr.h
r2829 r2830 68 68 KLDRPROT_EXECUTE, 69 69 /** Execute & read. */ 70 KLDRPROT_EXECUTE_READ ONLY,70 KLDRPROT_EXECUTE_READ, 71 71 /** Execute, read & write. */ 72 72 KLDRPROT_EXECUTE_READWRITE, -
trunk/kLdr/kLdrHlp.c
r2828 r2830 41 41 #include <kLdr.h> 42 42 #include "kLdrHlp.h" 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 43 382 44 383 … … 137 476 } 138 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 139 495 /** Internal worker for kldrHlpAssertMsg. */ 140 496 static void int2dec(char *pszLine, unsigned iLine) -
trunk/kLdr/kLdrHlp.h
r2828 r2830 112 112 113 113 114 int kldrSemInit(void); 115 int kldrSemTerm(void); 116 int kldrSemRequest(void); 117 int kldrSemRelease(void); 118 int kldrSemGlobalRequest(void); 119 int kldrSemGlobalRelease(void); 114 int kldrHlpSemInit(void); 115 void kldrHlpSemTerm(void); 116 int kldrHlpSemRequest(void); 117 void kldrHlpSemRelease(void); 120 118 121 int kldrPrivateAlloc(void *pv, size_t cb, unsigned fFlags, void **ppv); 122 int kldrPrivateFree(void *pv, size_t cb); 123 int kldrSharedAlloc(void *pv, size_t cb, unsigned fFlags, const char *pszFilename, void *File, void **ppv); 124 int kldrSharedFree(void *pv, size_t cb); 119 int kldrHlpPageAlloc(void **ppv, size_t cb, KLDRPROT enmProt, unsigned fFixed); 120 int kldrHlpPageProtect(void *pv, size_t cb, KLDRPROT enmProt); 121 int kldrHlpPageFree(void *pv, size_t cb); 125 122 126 123 int kldrHlpHeapInit(void); … … 132 129 int kldrHlpGetEnv(const char *pszVar, char *pszVal, size_t *pcchVal); 133 130 void kldrHlpExit(int rc); 131 134 132 void kldrHlpAssertMsg(const char *pszExpr, const char *pszFile, unsigned iLine, const char *pszFunction); 135 133
Note:
See TracChangeset
for help on using the changeset viewer.
