3 * Common Binary Loader
\r
10 #define BIN_LOWEST MM_USER_MIN // 1MiB
\r
11 #define BIN_GRANUALITY 0x10000 // 64KiB
\r
12 #define BIN_HIGHEST (0xBC000000-BIN_GRANUALITY) // Just below the kernel
\r
13 #define KLIB_LOWEST MM_MODULE_MIN
\r
14 #define KLIB_GRANUALITY 0x8000 // 32KiB
\r
15 #define KLIB_HIGHEST (MM_MODULE_MAX-KLIB_GRANUALITY)
\r
18 typedef struct sKernelBin {
\r
19 struct sKernelBin *Next;
\r
25 extern int Proc_Clone(Uint *Err, Uint Flags);
\r
26 extern void Threads_SetName(char *Name);
\r
27 extern char *Threads_GetName(int ID);
\r
28 extern Uint MM_ClearUser();
\r
29 extern void Threads_Exit();
\r
30 extern void Proc_StartUser(Uint Entrypoint, Uint *Bases, int ArgC, char **ArgV, char **EnvP, int DataSize);
\r
31 extern tKernelSymbol gKernelSymbols[];
\r
32 extern void gKernelSymbolsEnd;
\r
33 extern tBinaryType gELF_Info;
\r
35 // === PROTOTYPES ===
\r
36 int Proc_Execve(char *File, char **ArgV, char **EnvP);
\r
37 Uint Binary_Load(char *file, Uint *entryPoint);
\r
38 tBinary *Binary_GetInfo(char *truePath);
\r
39 Uint Binary_MapIn(tBinary *binary);
\r
40 Uint Binary_IsMapped(tBinary *binary);
\r
41 tBinary *Binary_DoLoad(char *truePath);
\r
42 void Binary_Dereference(tBinary *Info);
\r
43 Uint Binary_Relocate(void *Base);
\r
44 Uint Binary_GetSymbolEx(char *Name, Uint *Value);
\r
45 Uint Binary_FindSymbol(void *Base, char *Name, Uint *val);
\r
48 int glBinListLock = 0;
\r
49 tBinary *glLoadedBinaries = NULL;
\r
50 char **gsaRegInterps = NULL;
\r
51 int giRegInterps = 0;
\r
52 int glKBinListLock = 0;
\r
53 tKernelBin *glLoadedKernelLibs;
\r
54 tBinaryType *gRegBinTypes = &gELF_Info;
\r
56 // === FUNCTIONS ===
\r
58 * \fn int Proc_Spawn(char *Path)
\r
60 int Proc_Spawn(char *Path)
\r
62 char stackPath[strlen(Path)+1];
\r
64 strcpy(stackPath, Path);
\r
66 LOG("stackPath = '%s'\n", stackPath);
\r
68 if(Proc_Clone(NULL, CLONE_VM) == 0)
\r
71 char *args[2] = {stackPath, NULL};
\r
72 LOG("stackPath = '%s'\n", stackPath);
\r
73 Proc_Execve(stackPath, args, &args[1]);
\r
80 * \fn int Proc_Execve(char *File, char **ArgV, char **EnvP)
\r
81 * \brief Replace the current user image with another
\r
82 * \param File File to load as the next image
\r
83 * \param ArgV Arguments to pass to user
\r
84 * \param EnvP User's environment
\r
85 * \note Called Proc_ for historical reasons
\r
87 int Proc_Execve(char *File, char **ArgV, char **EnvP)
\r
91 char *argenvBuf, *strBuf;
\r
92 char **argvSaved, **envpSaved;
\r
95 Uint bases[2] = {0};
\r
97 ENTER("sFile pArgV pEnvP", File, ArgV, EnvP);
\r
99 // --- Save File, ArgV and EnvP (also get argc)
\r
101 // Count Arguments, Environment Variables and total string sizes
\r
103 for( argc = 0; ArgV && ArgV[argc]; argc++ )
\r
104 argenvBytes += strlen(ArgV[argc])+1;
\r
105 for( envc = 0; EnvP && EnvP[envc]; envc++ )
\r
106 argenvBytes += strlen(EnvP[envc])+1;
\r
107 argenvBytes = (argenvBytes + sizeof(void*)-1) & ~(sizeof(void*)-1);
\r
108 argenvBytes += (argc+1)*sizeof(void*) + (envc+1)*sizeof(void*);
\r
111 argenvBuf = malloc(argenvBytes);
\r
112 if(argenvBuf == NULL) {
\r
113 Warning("Proc_Execve - What the hell? The kernel is out of heap space");
\r
116 strBuf = argenvBuf + (argc+1)*sizeof(void*) + (envc+1)*sizeof(void*);
\r
119 argvSaved = (char **) argenvBuf;
\r
120 for( i = 0; i < argc; i++ )
\r
122 argvSaved[i] = strBuf;
\r
123 strcpy(argvSaved[i], ArgV[i]);
\r
124 strBuf += strlen(ArgV[i])+1;
\r
126 argvSaved[i] = NULL;
\r
127 envpSaved = &argvSaved[i+1];
\r
128 for( i = 0; i < envc; i++ )
\r
130 envpSaved[i] = strBuf;
\r
131 strcpy(envpSaved[i], EnvP[i]);
\r
132 strBuf += strlen(EnvP[i])+1;
\r
135 savedFile = malloc(strlen(File)+1);
\r
136 strcpy(savedFile, File);
\r
138 // --- Set Process Name
\r
139 Threads_SetName(File);
\r
141 // --- Clear User Address space
\r
144 // --- Load new binary
\r
145 bases[0] = Binary_Load(savedFile, &entry);
\r
149 Warning("Proc_Execve - Unable to load '%s'", Threads_GetName(-1));
\r
154 LOG("entry = 0x%x, bases[0] = 0x%x", entry, bases[0]);
\r
156 // --- And... Jump to it
\r
157 Proc_StartUser(entry, bases, argc, argvSaved, envpSaved, argenvBytes);
\r
158 for(;;); // Tell GCC that we never return
\r
162 * \fn Uint Binary_Load(char *file, Uint *entryPoint)
\r
164 Uint Binary_Load(char *file, Uint *entryPoint)
\r
170 ENTER("sfile", file);
\r
172 // Sanity Check Argument
\r
178 // Get True File Path
\r
179 sTruePath = VFS_GetTruePath(file);
\r
181 if(sTruePath == NULL) {
\r
182 Warning("[BIN ] '%s' does not exist.", file);
\r
187 LOG("sTruePath = '%s'", sTruePath);
\r
189 // Check if the binary has already been loaded
\r
190 if( !(pBinary = Binary_GetInfo(sTruePath)) )
\r
191 pBinary = Binary_DoLoad(sTruePath); // Else load it
\r
197 if(pBinary == NULL) {
\r
203 if( (base = Binary_IsMapped(pBinary)) ) {
\r
209 // Map into process space
\r
210 base = Binary_MapIn(pBinary); // If so then map it in
\r
212 // Check for errors
\r
219 if(pBinary->Interpreter) {
\r
221 if( Binary_Load(pBinary->Interpreter, &start) == 0 ) {
\r
225 *entryPoint = start;
\r
228 *entryPoint = pBinary->Entry - pBinary->Base + base;
\r
231 LOG("*entryPoint = 0x%x", *entryPoint);
\r
233 return base; // Pass the base as an argument to the user if there is an interpreter
\r
237 \fn tBinary *Binary_GetInfo(char *truePath)
\r
238 \brief Finds a matching binary entry
\r
239 \param truePath File Identifier (True path name)
\r
241 tBinary *Binary_GetInfo(char *truePath)
\r
244 pBinary = glLoadedBinaries;
\r
247 if(strcmp(pBinary->TruePath, truePath) == 0)
\r
249 pBinary = pBinary->Next;
\r
255 \fn Uint Binary_MapIn(tBinary *binary)
\r
256 \brief Maps an already-loaded binary into an address space.
\r
257 \param binary Pointer to globally stored data.
\r
259 Uint Binary_MapIn(tBinary *binary)
\r
265 // Reference Executable (Makes sure that it isn't unloaded)
\r
266 binary->ReferenceCount ++;
\r
269 base = binary->Base;
\r
271 // Check if base is free
\r
274 for(i=0;i<binary->NumPages;i++)
\r
276 if( MM_GetPhysAddr( binary->Pages[i].Virtual & ~0xFFF ) ) {
\r
278 LOG("Address 0x%x is taken\n", binary->Pages[i].Virtual & ~0xFFF);
\r
284 // Check if the executable has no base or it is not free
\r
287 // If so, give it a base
\r
288 base = BIN_HIGHEST;
\r
289 while(base >= BIN_LOWEST)
\r
291 for(i=0;i<binary->NumPages;i++)
\r
293 addr = binary->Pages[i].Virtual & ~0xFFF;
\r
294 addr -= binary->Base;
\r
296 if( MM_GetPhysAddr( addr ) ) break;
\r
298 // If space was found, break
\r
299 if(i == binary->NumPages) break;
\r
300 // Else decrement pointer and try again
\r
301 base -= BIN_GRANUALITY;
\r
306 if(base < BIN_LOWEST) {
\r
307 Warning("[BIN ] Executable '%s' cannot be loaded, no space", binary->TruePath);
\r
311 // Map Executable In
\r
312 for(i=0;i<binary->NumPages;i++)
\r
314 addr = binary->Pages[i].Virtual & ~0xFFF;
\r
315 addr -= binary->Base;
\r
317 LOG("%i - 0x%x to 0x%x", i, addr, binary->Pages[i].Physical);
\r
318 MM_Map( addr, (Uint) (binary->Pages[i].Physical) );
\r
319 if( binary->Pages[i].Physical & 1) // Read-Only
\r
320 MM_SetFlags( addr, MM_PFLAG_RO, -1 );
\r
322 MM_SetFlags( addr, MM_PFLAG_COW, -1 );
\r
325 //LOG("*0x%x = 0x%x\n", binary->Pages[0].Virtual, *(Uint*)binary->Pages[0].Virtual);
\r
332 * \fn Uint Binary_IsMapped(tBinary *binary)
\r
333 * \brief Check if a binary is already mapped into the address space
\r
334 * \param binary Binary information to check
\r
335 * \return Current Base or 0
\r
337 Uint Binary_IsMapped(tBinary *binary)
\r
341 // Check prefered base
\r
342 iBase = binary->Base;
\r
343 if(MM_GetPage( iBase ) == (binary->Pages[0].Physical & ~0xFFF))
\r
346 for(iBase = BIN_HIGHEST;
\r
347 iBase >= BIN_LOWEST;
\r
348 iBase -= BIN_GRANUALITY)
\r
350 if(MM_GetPage( iBase ) == (binary->Pages[0].Physical & ~0xFFF))
\r
359 * \fn tBinary *Binary_DoLoad(char *truePath)
\r
360 * \brief Loads a binary file into memory
\r
361 * \param truePath Absolute filename of binary
\r
363 tBinary *Binary_DoLoad(char *truePath)
\r
368 tBinaryType *bt = gRegBinTypes;
\r
370 ENTER("struePath", truePath);
\r
373 fp = VFS_Open(truePath, VFS_OPENFLAG_READ);
\r
375 LOG("Unable to load file, access denied");
\r
381 VFS_Read(fp, 4, &ident);
\r
382 VFS_Seek(fp, 0, SEEK_SET);
\r
384 for(; bt; bt = bt->Next)
\r
386 if( (ident & bt->Mask) != (Uint)bt->Ident )
\r
388 pBinary = bt->Load(fp);
\r
392 Warning("[BIN ] '%s' is an unknown file type. (0x%x 0x%x 0x%x 0x%x)",
\r
393 truePath, ident&0xFF, (ident>>8)&0xFF, (ident>>16)&0xFF, (ident>>24)&0xFF);
\r
399 if(pBinary == NULL) {
\r
404 // Initialise Structure
\r
405 pBinary->ReferenceCount = 0;
\r
406 pBinary->TruePath = malloc( strlen(truePath) + 1 );
\r
407 strcpy(pBinary->TruePath, truePath);
\r
409 // Debug Information
\r
410 LOG("Interpreter: '%s'", pBinary->Interpreter);
\r
411 LOG("Base: 0x%x, Entry: 0x%x", pBinary->Base, pBinary->Entry);
\r
412 LOG("NumPages: %i", pBinary->NumPages);
\r
415 for(i=0;i<pBinary->NumPages;i++)
\r
419 paddr = (Uint)MM_AllocPhys();
\r
420 MM_RefPhys( paddr ); // Make sure it is _NOT_ freed until we want it to be
\r
421 dest = MM_MapTemp( paddr );
\r
422 dest += pBinary->Pages[i].Virtual & 0xFFF;
\r
423 LOG("dest = 0x%x, paddr = 0x%x", dest, paddr);
\r
424 LOG("Pages[%i]={Physical:0x%x,Virtual:0x%x,Size:0x%x}",
\r
425 i, pBinary->Pages[i].Physical, pBinary->Pages[i].Virtual, pBinary->Pages[i].Size);
\r
428 if(pBinary->Pages[i].Physical == -1) {
\r
429 LOG("%i - ZERO", i);
\r
430 memsetd( (void*)dest, 0, 1024 );
\r
434 VFS_Seek( fp, pBinary->Pages[i].Physical, 1 );
\r
435 if(pBinary->Pages[i].Size != 0x1000) {
\r
436 LOG("%i - 0x%x - 0x%x bytes",
\r
437 i, pBinary->Pages[i].Physical, pBinary->Pages[i].Size);
\r
438 memset( (void*)dest, 0, 0x1000 -(dest&0xFFF) );
\r
439 VFS_Read( fp, pBinary->Pages[i].Size, (void*)dest );
\r
441 LOG("%i - 0x%x", i, pBinary->Pages[i].Physical);
\r
442 VFS_Read( fp, 0x1000, (void*)dest );
\r
445 pBinary->Pages[i].Physical = paddr;
\r
446 MM_FreeTemp( dest );
\r
448 LOG("Page Count: %i", pBinary->NumPages);
\r
454 LOCK(&glBinListLock);
\r
455 pBinary->Next = glLoadedBinaries;
\r
456 glLoadedBinaries = pBinary;
\r
457 RELEASE(&glBinListLock);
\r
460 LEAVE('p', pBinary);
\r
465 * \fn void Binary_Unload(void *Base)
\r
466 * \brief Unload / Unmap a binary
\r
467 * \param Base Loaded Base
\r
468 * \note Currently used only for kernel libaries
\r
470 void Binary_Unload(void *Base)
\r
473 tKernelBin *prev = NULL;
\r
476 if((Uint)Base < 0xC0000000)
\r
478 // TODO: User Binaries
\r
479 Warning("[BIN ] Unloading user binaries is currently unimplemented");
\r
483 // Kernel Libraries
\r
484 for(pKBin = glLoadedKernelLibs;
\r
486 prev = pKBin, pKBin = pKBin->Next)
\r
489 if(pKBin->Base != Base) continue;
\r
490 // Deallocate Memory
\r
491 for(i = 0; i < pKBin->Info->NumPages; i++) {
\r
492 MM_Deallocate( (Uint)Base + (i << 12) );
\r
494 // Dereference Binary
\r
495 Binary_Dereference( pKBin->Info );
\r
496 // Remove from list
\r
497 if(prev) prev->Next = pKBin->Next;
\r
498 else glLoadedKernelLibs = pKBin->Next;
\r
506 * \fn void Binary_Dereference(tBinary *Info)
\r
507 * \brief Dereferences and if nessasary, deletes a binary
\r
508 * \param Info Binary information structure
\r
510 void Binary_Dereference(tBinary *Info)
\r
512 // Decrement reference count
\r
513 Info->ReferenceCount --;
\r
515 // Check if it is still in use
\r
516 if(Info->ReferenceCount) return;
\r
518 /// \todo Implement binary freeing
\r
522 \fn char *Binary_RegInterp(char *path)
\r
523 \brief Registers an Interpreter
\r
524 \param path Path to interpreter provided by executable
\r
526 char *Binary_RegInterp(char *path)
\r
529 // NULL Check Argument
\r
530 if(path == NULL) return NULL;
\r
531 // NULL Check the array
\r
532 if(gsaRegInterps == NULL)
\r
535 gsaRegInterps = malloc( sizeof(char*) );
\r
536 gsaRegInterps[0] = malloc( strlen(path) );
\r
537 strcpy(gsaRegInterps[0], path);
\r
538 return gsaRegInterps[0];
\r
542 for( i = 0; i < giRegInterps; i++ )
\r
544 if(strcmp(gsaRegInterps[i], path) == 0)
\r
545 return gsaRegInterps[i];
\r
548 // Interpreter is not in list
\r
550 gsaRegInterps = malloc( sizeof(char*)*giRegInterps );
\r
551 gsaRegInterps[i] = malloc( strlen(path) );
\r
552 strcpy(gsaRegInterps[i], path);
\r
553 return gsaRegInterps[i];
\r
557 // Kernel Binary Handling
\r
560 * \fn void *Binary_LoadKernel(char *path)
\r
561 * \brief Load a binary into kernel space
\r
562 * \note This function shares much with #Binary_Load, but does it's own mapping
\r
564 void *Binary_LoadKernel(char *file)
\r
568 tKernelBin *pKBinary;
\r
573 ENTER("sfile", file);
\r
575 // Sanity Check Argument
\r
581 // Get True File Path
\r
582 sTruePath = VFS_GetTruePath(file);
\r
583 if(sTruePath == NULL) {
\r
588 // Check if the binary has already been loaded
\r
589 if( (pBinary = Binary_GetInfo(sTruePath)) )
\r
591 for(pKBinary = glLoadedKernelLibs;
\r
593 pKBinary = pKBinary->Next )
\r
595 if(pKBinary->Info == pBinary) {
\r
596 LEAVE('p', pKBinary->Base);
\r
597 return pKBinary->Base;
\r
602 pBinary = Binary_DoLoad(sTruePath); // Else load it
\r
605 if(pBinary == NULL) {
\r
611 // Now pBinary is valid (either freshly loaded or only user mapped)
\r
612 // So, map it into kernel space
\r
615 // Reference Executable (Makes sure that it isn't unloaded)
\r
616 pBinary->ReferenceCount ++;
\r
618 // Check compiled base
\r
619 base = pBinary->Base;
\r
621 if(base < KLIB_LOWEST || base > KLIB_HIGHEST || base + (pBinary->NumPages<<12) > KLIB_HIGHEST) {
\r
624 // - Check if it is a valid base address
\r
627 for(i=0;i<pBinary->NumPages;i++)
\r
629 if( MM_GetPhysAddr( pBinary->Pages[i].Virtual & ~0xFFF ) ) {
\r
631 LOG("Address 0x%x is taken\n", pBinary->Pages[i].Virtual & ~0xFFF);
\r
637 // Check if the executable has no base or it is not free
\r
640 // If so, give it a base
\r
641 base = KLIB_LOWEST;
\r
642 while(base < KLIB_HIGHEST)
\r
644 for(i = 0; i < pBinary->NumPages; i++)
\r
646 addr = pBinary->Pages[i].Virtual & ~0xFFF;
\r
647 addr -= pBinary->Base;
\r
649 if( MM_GetPhysAddr( addr ) ) break;
\r
651 // If space was found, break
\r
652 if(i == pBinary->NumPages) break;
\r
653 // Else decrement pointer and try again
\r
654 base += KLIB_GRANUALITY;
\r
659 if(base >= KLIB_HIGHEST) {
\r
660 Warning("[BIN ] Executable '%s' cannot be loaded into kernel, no space", pBinary->TruePath);
\r
661 Binary_Dereference( pBinary );
\r
666 LOG("base = 0x%x", base);
\r
669 LOG("pBinary = {NumPages:%i, Pages=%p}", pBinary->NumPages, pBinary->Pages);
\r
670 for(i = 0; i < pBinary->NumPages; i++)
\r
672 addr = pBinary->Pages[i].Virtual & ~0xFFF;
\r
673 addr -= pBinary->Base;
\r
675 LOG("%i - 0x%x to 0x%x", i, addr, pBinary->Pages[i].Physical);
\r
676 MM_Map( addr, (Uint) (pBinary->Pages[i].Physical) );
\r
677 MM_SetFlags( addr, MM_PFLAG_KERNEL, MM_PFLAG_KERNEL );
\r
678 #if 0 // Why was this here? It's the kernel
\r
679 if( pBinary->Pages[i].Physical & 1) // Read-Only
\r
680 MM_SetFlags( addr, MM_PFLAG_RO, MM_PFLAG_KERNEL );
\r
682 MM_SetFlags( addr, MM_PFLAG_COW, MM_PFLAG_KERNEL );
\r
683 //MM_SetCOW( addr );
\r
687 // Relocate Library
\r
688 if( !Binary_Relocate( (void*)base ) )
\r
690 Warning("[BIN ] Relocation of '%s' failed, unloading", sTruePath);
\r
691 Binary_Unload( (void*)base );
\r
692 Binary_Dereference( pBinary );
\r
697 // Add to list (relocator must look at itself manually, not via Binary_GetSymbol)
\r
698 pKBinary = malloc(sizeof(*pKBinary));
\r
699 pKBinary->Base = (void*)base;
\r
700 pKBinary->Info = pBinary;
\r
701 LOCK( &glKBinListLock );
\r
702 pKBinary->Next = glLoadedKernelLibs;
\r
703 glLoadedKernelLibs = pKBinary;
\r
704 RELEASE( &glKBinListLock );
\r
707 return (void*)base;
\r
711 * \fn Uint Binary_Relocate(void *Base)
\r
712 * \brief Relocates a loaded binary (used by kernel libraries)
\r
713 * \param Base Loaded base address of binary
\r
714 * \return Boolean Success
\r
716 Uint Binary_Relocate(void *Base)
\r
718 Uint32 ident = *(Uint32*) Base;
\r
719 tBinaryType *bt = gRegBinTypes;
\r
721 for(; bt; bt = bt->Next)
\r
723 if( (ident & bt->Mask) == (Uint)bt->Ident )
\r
724 return bt->Relocate( (void*)Base);
\r
727 Warning("[BIN ] 0x%x is an unknown file type. (0x%x 0x%x 0x%x 0x%x)",
\r
728 Base, ident&0xFF, ident>>8, ident>>16, ident>>24);
\r
733 * \fn int Binary_GetSymbol(char *Name, Uint *Val)
\r
734 * \brief Get a symbol value
\r
735 * \return Value of symbol or -1 on error
\r
737 * Gets the value of a symbol from either the currently loaded
\r
738 * libraries or the kernel's exports.
\r
740 int Binary_GetSymbol(char *Name, Uint *Val)
\r
742 if( Binary_GetSymbolEx(Name, Val) ) return 1;
\r
747 * \fn Uint Binary_GetSymbolEx(char *Name, Uint *Value)
\r
748 * \brief Get a symbol value
\r
750 * Gets the value of a symbol from either the currently loaded
\r
751 * libraries or the kernel's exports.
\r
753 Uint Binary_GetSymbolEx(char *Name, Uint *Value)
\r
757 int numKSyms = ((Uint)&gKernelSymbolsEnd-(Uint)&gKernelSymbols)/sizeof(tKernelSymbol);
\r
760 for( i = 0; i < numKSyms; i++ )
\r
762 if(strcmp(Name, gKernelSymbols[i].Name) == 0) {
\r
763 *Value = gKernelSymbols[i].Value;
\r
768 // Scan Loaded Libraries
\r
769 for(pKBin = glLoadedKernelLibs;
\r
771 pKBin = pKBin->Next )
\r
773 if( Binary_FindSymbol(pKBin->Base, Name, Value) ) {
\r
778 Warning("[BIN ] Unable to find symbol '%s'", Name);
\r
783 * \fn Uint Binary_GetSymbolBin(void *Base, char *Name, Uint *val)
\r
784 * \brief Get a symbol from the specified library
\r
785 * \param Base Base address
\r
786 * \param Name Name of symbol to find
\r
787 * \param val Pointer to place final value
\r
789 Uint Binary_FindSymbol(void *Base, char *Name, Uint *val)
\r
791 Uint32 ident = *(Uint32*) Base;
\r
792 tBinaryType *bt = gRegBinTypes;
\r
794 for(; bt; bt = bt->Next)
\r
796 if( (ident & bt->Mask) == (Uint)bt->Ident )
\r
797 return bt->GetSymbol(Base, Name, val);
\r
800 Warning("[BIN ] 0x%x is an unknown file type. (0x%x 0x%x 0x%x 0x%x)",
\r
801 Base, ident&0xFF, ident>>8, ident>>16, ident>>24);
\r