3 * Common Binary Loader
\r
20 // === CONSTANTS ===
\r
21 #define BIN_LOWEST MM_USER_MIN // 1MiB
\r
22 #define BIN_GRANUALITY 0x10000 // 64KiB
\r
23 #define BIN_HIGHEST (0xBC000000-BIN_GRANUALITY) // Just below the kernel
\r
24 #define KLIB_LOWEST MM_MODULE_MIN
\r
25 #define KLIB_GRANUALITY 0x8000 // 32KiB
\r
26 #define KLIB_HIGHEST (MM_MODULE_MAX-KLIB_GRANUALITY)
\r
29 typedef struct sKernelBin {
\r
30 struct sKernelBin *Next;
\r
36 extern int Proc_Clone(Uint *Err, Uint Flags);
\r
37 extern void Proc_SetThreadName(char *Name);
\r
38 extern Uint MM_ClearUser();
\r
39 extern void Proc_Exit();
\r
40 extern void Proc_StartUser(Uint Entrypoint, Uint *Bases, int ArgC, char **ArgV, char **EnvP, int DataSize);
\r
41 extern tKernelSymbol gKernelSymbols[];
\r
42 extern void gKernelSymbolsEnd;
\r
43 extern tBinaryType gELF_Info;
\r
45 // === PROTOTYPES ===
\r
46 int Proc_Execve(char *File, char **ArgV, char **EnvP);
\r
47 Uint Binary_Load(char *file, Uint *entryPoint);
\r
48 tBinary *Binary_GetInfo(char *truePath);
\r
49 Uint Binary_MapIn(tBinary *binary);
\r
50 Uint Binary_IsMapped(tBinary *binary);
\r
51 tBinary *Binary_DoLoad(char *truePath);
\r
52 void Binary_Dereference(tBinary *Info);
\r
53 Uint Binary_Relocate(void *Base);
\r
54 Uint Binary_GetSymbolEx(char *Name, Uint *Value);
\r
55 Uint Binary_FindSymbol(void *Base, char *Name, Uint *val);
\r
58 int glBinListLock = 0;
\r
59 tBinary *glLoadedBinaries = NULL;
\r
60 char **gsaRegInterps = NULL;
\r
61 int giRegInterps = 0;
\r
62 int glKBinListLock = 0;
\r
63 tKernelBin *glLoadedKernelLibs;
\r
64 tBinaryType *gRegBinTypes = &gELF_Info;
\r
66 // === FUNCTIONS ===
\r
68 * \fn int Proc_Spawn(char *Path)
\r
70 int Proc_Spawn(char *Path)
\r
72 char stackPath[strlen(Path)+1];
\r
74 strcpy(stackPath, Path);
\r
76 LOG("stackPath = '%s'\n", stackPath);
\r
78 if(Proc_Clone(NULL, CLONE_VM) == 0)
\r
81 char *args[2] = {stackPath, NULL};
\r
82 LOG("stackPath = '%s'\n", stackPath);
\r
83 Proc_Execve(stackPath, args, &args[1]);
\r
90 * \fn int Proc_Execve(char *File, char **ArgV, char **EnvP)
\r
91 * \brief Replace the current user image with another
\r
92 * \param File File to load as the next image
\r
93 * \param ArgV Arguments to pass to user
\r
94 * \param EnvP User's environment
\r
95 * \note Called Proc_ for historical reasons
\r
97 int Proc_Execve(char *File, char **ArgV, char **EnvP)
\r
101 char *argenvBuf, *strBuf;
\r
102 char **argvSaved, **envpSaved;
\r
105 Uint bases[2] = {0};
\r
107 ENTER("sFile pArgV pEnvP", File, ArgV, EnvP);
\r
109 // --- Save File, ArgV and EnvP (also get argc)
\r
111 // Count Arguments, Environment Variables and total string sizes
\r
113 for( argc = 0; ArgV && ArgV[argc]; argc++ )
\r
114 argenvBytes += strlen(ArgV[argc])+1;
\r
115 for( envc = 0; EnvP && EnvP[envc]; envc++ )
\r
116 argenvBytes += strlen(EnvP[envc])+1;
\r
117 argenvBytes = (argenvBytes + sizeof(void*)-1) & ~(sizeof(void*)-1);
\r
118 argenvBytes += (argc+1)*sizeof(void*) + (envc+1)*sizeof(void*);
\r
121 argenvBuf = malloc(argenvBytes);
\r
122 if(argenvBuf == NULL) {
\r
123 Warning("Proc_Execve - What the hell? The kernel is out of heap space");
\r
126 strBuf = argenvBuf + (argc+1)*sizeof(void*) + (envc+1)*sizeof(void*);
\r
129 argvSaved = (char **) argenvBuf;
\r
130 for( i = 0; i < argc; i++ )
\r
132 argvSaved[i] = strBuf;
\r
133 strcpy(argvSaved[i], ArgV[i]);
\r
134 strBuf += strlen(ArgV[i])+1;
\r
136 argvSaved[i] = NULL;
\r
137 envpSaved = &argvSaved[i+1];
\r
138 for( i = 0; i < envc; i++ )
\r
140 envpSaved[i] = strBuf;
\r
141 strcpy(envpSaved[i], EnvP[i]);
\r
142 strBuf += strlen(EnvP[i])+1;
\r
145 savedFile = malloc(strlen(File)+1);
\r
146 strcpy(savedFile, File);
\r
148 // --- Set Process Name
\r
149 Proc_SetThreadName(File);
\r
151 // --- Clear User Address space
\r
154 // --- Load new binary
\r
155 bases[0] = Binary_Load(savedFile, &entry);
\r
159 Warning("Proc_Execve - Unable to load '%s'", File);
\r
164 LOG("entry = 0x%x, bases[0] = 0x%x", entry, bases[0]);
\r
166 // --- And... Jump to it
\r
167 Proc_StartUser(entry, bases, argc, argvSaved, envpSaved, argenvBytes);
\r
168 for(;;); // Tell GCC that we never return
\r
172 * \fn Uint Binary_Load(char *file, Uint *entryPoint)
\r
174 Uint Binary_Load(char *file, Uint *entryPoint)
\r
180 ENTER("sfile", file);
\r
182 // Sanity Check Argument
\r
188 // Get True File Path
\r
189 sTruePath = VFS_GetTruePath(file);
\r
191 if(sTruePath == NULL) {
\r
192 Warning("[BIN ] '%s' does not exist.", file);
\r
197 LOG("sTruePath = '%s'", sTruePath);
\r
199 // Check if the binary has already been loaded
\r
200 if( !(pBinary = Binary_GetInfo(sTruePath)) )
\r
201 pBinary = Binary_DoLoad(sTruePath); // Else load it
\r
207 if(pBinary == NULL) {
\r
213 if( (base = Binary_IsMapped(pBinary)) ) {
\r
219 // Map into process space
\r
220 base = Binary_MapIn(pBinary); // If so then map it in
\r
222 // Check for errors
\r
229 if(pBinary->Interpreter) {
\r
231 if( Binary_Load(pBinary->Interpreter, &start) == 0 ) {
\r
235 *entryPoint = start;
\r
238 *entryPoint = pBinary->Entry - pBinary->Base + base;
\r
241 LOG("*entryPoint = 0x%x", *entryPoint);
\r
243 return base; // Pass the base as an argument to the user if there is an interpreter
\r
247 \fn tBinary *Binary_GetInfo(char *truePath)
\r
248 \brief Finds a matching binary entry
\r
249 \param truePath File Identifier (True path name)
\r
251 tBinary *Binary_GetInfo(char *truePath)
\r
254 pBinary = glLoadedBinaries;
\r
257 if(strcmp(pBinary->TruePath, truePath) == 0)
\r
259 pBinary = pBinary->Next;
\r
265 \fn Uint Binary_MapIn(tBinary *binary)
\r
266 \brief Maps an already-loaded binary into an address space.
\r
267 \param binary Pointer to globally stored data.
\r
269 Uint Binary_MapIn(tBinary *binary)
\r
275 // Reference Executable (Makes sure that it isn't unloaded)
\r
276 binary->ReferenceCount ++;
\r
279 base = binary->Base;
\r
281 // Check if base is free
\r
284 for(i=0;i<binary->NumPages;i++)
\r
286 if( MM_GetPhysAddr( binary->Pages[i].Virtual & ~0xFFF ) ) {
\r
288 LOG("Address 0x%x is taken\n", binary->Pages[i].Virtual & ~0xFFF);
\r
294 // Check if the executable has no base or it is not free
\r
297 // If so, give it a base
\r
298 base = BIN_HIGHEST;
\r
299 while(base >= BIN_LOWEST)
\r
301 for(i=0;i<binary->NumPages;i++)
\r
303 addr = binary->Pages[i].Virtual & ~0xFFF;
\r
304 addr -= binary->Base;
\r
306 if( MM_GetPhysAddr( addr ) ) break;
\r
308 // If space was found, break
\r
309 if(i == binary->NumPages) break;
\r
310 // Else decrement pointer and try again
\r
311 base -= BIN_GRANUALITY;
\r
316 if(base < BIN_LOWEST) {
\r
317 Warning("[BIN ] Executable '%s' cannot be loaded, no space", binary->TruePath);
\r
321 // Map Executable In
\r
322 for(i=0;i<binary->NumPages;i++)
\r
324 addr = binary->Pages[i].Virtual & ~0xFFF;
\r
325 addr -= binary->Base;
\r
327 LOG("%i - 0x%x to 0x%x", i, addr, binary->Pages[i].Physical);
\r
328 MM_Map( addr, (Uint) (binary->Pages[i].Physical) );
\r
329 if( binary->Pages[i].Physical & 1) // Read-Only
\r
330 MM_SetFlags( addr, MM_PFLAG_RO, -1 );
\r
332 MM_SetFlags( addr, MM_PFLAG_COW, -1 );
\r
335 //LOG("*0x%x = 0x%x\n", binary->Pages[0].Virtual, *(Uint*)binary->Pages[0].Virtual);
\r
342 * \fn Uint Binary_IsMapped(tBinary *binary)
\r
343 * \brief Check if a binary is already mapped into the address space
\r
344 * \param binary Binary information to check
\r
345 * \return Current Base or 0
\r
347 Uint Binary_IsMapped(tBinary *binary)
\r
351 // Check prefered base
\r
352 iBase = binary->Base;
\r
353 if(MM_GetPage( iBase ) == (binary->Pages[0].Physical & ~0xFFF))
\r
356 for(iBase = BIN_HIGHEST;
\r
357 iBase >= BIN_LOWEST;
\r
358 iBase -= BIN_GRANUALITY)
\r
360 if(MM_GetPage( iBase ) == (binary->Pages[0].Physical & ~0xFFF))
\r
369 * \fn tBinary *Binary_DoLoad(char *truePath)
\r
370 * \brief Loads a binary file into memory
\r
371 * \param truePath Absolute filename of binary
\r
373 tBinary *Binary_DoLoad(char *truePath)
\r
378 tBinaryType *bt = gRegBinTypes;
\r
380 ENTER("struePath", truePath);
\r
383 fp = VFS_Open(truePath, VFS_OPENFLAG_READ);
\r
385 LOG("Unable to load file, access denied");
\r
391 VFS_Read(fp, 4, &ident);
\r
392 VFS_Seek(fp, 0, SEEK_SET);
\r
394 for(; bt; bt = bt->Next)
\r
396 if( (ident & bt->Mask) != (Uint)bt->Ident )
\r
398 pBinary = bt->Load(fp);
\r
402 Warning("[BIN ] '%s' is an unknown file type. (0x%x 0x%x 0x%x 0x%x)",
\r
403 truePath, ident&0xFF, (ident>>8)&0xFF, (ident>>16)&0xFF, (ident>>24)&0xFF);
\r
409 if(pBinary == NULL) {
\r
414 // Initialise Structure
\r
415 pBinary->ReferenceCount = 0;
\r
416 pBinary->TruePath = malloc( strlen(truePath) + 1 );
\r
417 strcpy(pBinary->TruePath, truePath);
\r
419 // Debug Information
\r
420 LOG("Interpreter: '%s'", pBinary->Interpreter);
\r
421 LOG("Base: 0x%x, Entry: 0x%x", pBinary->Base, pBinary->Entry);
\r
422 LOG("NumPages: %i", pBinary->NumPages);
\r
425 for(i=0;i<pBinary->NumPages;i++)
\r
429 paddr = (Uint)MM_AllocPhys();
\r
430 MM_RefPhys( paddr ); // Make sure it is _NOT_ freed until we want it to be
\r
431 dest = MM_MapTemp( paddr );
\r
432 dest += pBinary->Pages[i].Virtual & 0xFFF;
\r
433 LOG("dest = 0x%x, paddr = 0x%x", dest, paddr);
\r
434 LOG("Pages[%i]={Physical:0x%x,Virtual:0x%x,Size:0x%x}",
\r
435 i, pBinary->Pages[i].Physical, pBinary->Pages[i].Virtual, pBinary->Pages[i].Size);
\r
438 if(pBinary->Pages[i].Physical == -1) {
\r
439 LOG("%i - ZERO", i);
\r
440 memsetd( (void*)dest, 0, 1024 );
\r
444 VFS_Seek( fp, pBinary->Pages[i].Physical, 1 );
\r
445 if(pBinary->Pages[i].Size != 0x1000) {
\r
446 LOG("%i - 0x%x - 0x%x bytes",
\r
447 i, pBinary->Pages[i].Physical, pBinary->Pages[i].Size);
\r
448 memset( (void*)dest, 0, 0x1000 -(dest&0xFFF) );
\r
449 VFS_Read( fp, pBinary->Pages[i].Size, (void*)dest );
\r
451 LOG("%i - 0x%x", i, pBinary->Pages[i].Physical);
\r
452 VFS_Read( fp, 0x1000, (void*)dest );
\r
455 pBinary->Pages[i].Physical = paddr;
\r
456 MM_FreeTemp( dest );
\r
458 LOG("Page Count: %i", pBinary->NumPages);
\r
464 LOCK(&glBinListLock);
\r
465 pBinary->Next = glLoadedBinaries;
\r
466 glLoadedBinaries = pBinary;
\r
467 RELEASE(&glBinListLock);
\r
470 LEAVE('p', pBinary);
\r
475 * \fn void Binary_Unload(void *Base)
\r
476 * \brief Unload / Unmap a binary
\r
477 * \param Base Loaded Base
\r
478 * \note Currently used only for kernel libaries
\r
480 void Binary_Unload(void *Base)
\r
483 tKernelBin *prev = NULL;
\r
486 if((Uint)Base < 0xC0000000)
\r
488 // TODO: User Binaries
\r
489 Warning("[BIN ] Unloading user binaries is currently unimplemented");
\r
493 // Kernel Libraries
\r
494 for(pKBin = glLoadedKernelLibs;
\r
496 prev = pKBin, pKBin = pKBin->Next)
\r
499 if(pKBin->Base != Base) continue;
\r
500 // Deallocate Memory
\r
501 for(i = 0; i < pKBin->Info->NumPages; i++) {
\r
502 MM_Deallocate( (Uint)Base + (i << 12) );
\r
504 // Dereference Binary
\r
505 Binary_Dereference( pKBin->Info );
\r
506 // Remove from list
\r
507 if(prev) prev->Next = pKBin->Next;
\r
508 else glLoadedKernelLibs = pKBin->Next;
\r
516 * \fn void Binary_Dereference(tBinary *Info)
\r
517 * \brief Dereferences and if nessasary, deletes a binary
\r
518 * \param Info Binary information structure
\r
520 void Binary_Dereference(tBinary *Info)
\r
522 // Decrement reference count
\r
523 Info->ReferenceCount --;
\r
525 // Check if it is still in use
\r
526 if(Info->ReferenceCount) return;
\r
528 /// \todo Implement binary freeing
\r
532 \fn char *Binary_RegInterp(char *path)
\r
533 \brief Registers an Interpreter
\r
534 \param path Path to interpreter provided by executable
\r
536 char *Binary_RegInterp(char *path)
\r
539 // NULL Check Argument
\r
540 if(path == NULL) return NULL;
\r
541 // NULL Check the array
\r
542 if(gsaRegInterps == NULL)
\r
545 gsaRegInterps = malloc( sizeof(char*) );
\r
546 gsaRegInterps[0] = malloc( strlen(path) );
\r
547 strcpy(gsaRegInterps[0], path);
\r
548 return gsaRegInterps[0];
\r
552 for( i = 0; i < giRegInterps; i++ )
\r
554 if(strcmp(gsaRegInterps[i], path) == 0)
\r
555 return gsaRegInterps[i];
\r
558 // Interpreter is not in list
\r
560 gsaRegInterps = malloc( sizeof(char*)*giRegInterps );
\r
561 gsaRegInterps[i] = malloc( strlen(path) );
\r
562 strcpy(gsaRegInterps[i], path);
\r
563 return gsaRegInterps[i];
\r
567 // Kernel Binary Handling
\r
570 * \fn void *Binary_LoadKernel(char *path)
\r
571 * \brief Load a binary into kernel space
\r
572 * \note This function shares much with #Binary_Load, but does it's own mapping
\r
574 void *Binary_LoadKernel(char *file)
\r
578 tKernelBin *pKBinary;
\r
583 ENTER("sfile", file);
\r
585 // Sanity Check Argument
\r
591 // Get True File Path
\r
592 sTruePath = VFS_GetTruePath(file);
\r
593 if(sTruePath == NULL) {
\r
598 // Check if the binary has already been loaded
\r
599 if( (pBinary = Binary_GetInfo(sTruePath)) )
\r
601 for(pKBinary = glLoadedKernelLibs;
\r
603 pKBinary = pKBinary->Next )
\r
605 if(pKBinary->Info == pBinary) {
\r
606 LEAVE('p', pKBinary->Base);
\r
607 return pKBinary->Base;
\r
612 pBinary = Binary_DoLoad(sTruePath); // Else load it
\r
615 if(pBinary == NULL) {
\r
621 // Now pBinary is valid (either freshly loaded or only user mapped)
\r
622 // So, map it into kernel space
\r
625 // Reference Executable (Makes sure that it isn't unloaded)
\r
626 pBinary->ReferenceCount ++;
\r
628 // Check compiled base
\r
629 base = pBinary->Base;
\r
631 if(base < KLIB_LOWEST || base > KLIB_HIGHEST || base + (pBinary->NumPages<<12) > KLIB_HIGHEST) {
\r
634 // - Check if it is a valid base address
\r
637 for(i=0;i<pBinary->NumPages;i++)
\r
639 if( MM_GetPhysAddr( pBinary->Pages[i].Virtual & ~0xFFF ) ) {
\r
641 LOG("Address 0x%x is taken\n", pBinary->Pages[i].Virtual & ~0xFFF);
\r
647 // Check if the executable has no base or it is not free
\r
650 // If so, give it a base
\r
651 base = KLIB_LOWEST;
\r
652 while(base < KLIB_HIGHEST)
\r
654 for(i = 0; i < pBinary->NumPages; i++)
\r
656 addr = pBinary->Pages[i].Virtual & ~0xFFF;
\r
657 addr -= pBinary->Base;
\r
659 if( MM_GetPhysAddr( addr ) ) break;
\r
661 // If space was found, break
\r
662 if(i == pBinary->NumPages) break;
\r
663 // Else decrement pointer and try again
\r
664 base += KLIB_GRANUALITY;
\r
669 if(base >= KLIB_HIGHEST) {
\r
670 Warning("[BIN ] Executable '%s' cannot be loaded into kernel, no space", pBinary->TruePath);
\r
671 Binary_Dereference( pBinary );
\r
676 LOG("base = 0x%x", base);
\r
679 LOG("pBinary = {NumPages:%i, Pages=%p}", pBinary->NumPages, pBinary->Pages);
\r
680 for(i = 0; i < pBinary->NumPages; i++)
\r
682 addr = pBinary->Pages[i].Virtual & ~0xFFF;
\r
683 addr -= pBinary->Base;
\r
685 LOG("%i - 0x%x to 0x%x", i, addr, pBinary->Pages[i].Physical);
\r
686 MM_Map( addr, (Uint) (pBinary->Pages[i].Physical) );
\r
687 MM_SetFlags( addr, MM_PFLAG_KERNEL, MM_PFLAG_KERNEL );
\r
688 #if 0 // Why was this here? It's the kernel
\r
689 if( pBinary->Pages[i].Physical & 1) // Read-Only
\r
690 MM_SetFlags( addr, MM_PFLAG_RO, MM_PFLAG_KERNEL );
\r
692 MM_SetFlags( addr, MM_PFLAG_COW, MM_PFLAG_KERNEL );
\r
693 //MM_SetCOW( addr );
\r
697 // Relocate Library
\r
698 if( !Binary_Relocate( (void*)base ) )
\r
700 Warning("[BIN ] Relocation of '%s' failed, unloading", sTruePath);
\r
701 Binary_Unload( (void*)base );
\r
702 Binary_Dereference( pBinary );
\r
707 // Add to list (relocator must look at itself manually, not via Binary_GetSymbol)
\r
708 pKBinary = malloc(sizeof(*pKBinary));
\r
709 pKBinary->Base = (void*)base;
\r
710 pKBinary->Info = pBinary;
\r
711 LOCK( &glKBinListLock );
\r
712 pKBinary->Next = glLoadedKernelLibs;
\r
713 glLoadedKernelLibs = pKBinary;
\r
714 RELEASE( &glKBinListLock );
\r
717 return (void*)base;
\r
721 * \fn Uint Binary_Relocate(void *Base)
\r
722 * \brief Relocates a loaded binary (used by kernel libraries)
\r
723 * \param Base Loaded base address of binary
\r
724 * \return Boolean Success
\r
726 Uint Binary_Relocate(void *Base)
\r
728 Uint32 ident = *(Uint32*) Base;
\r
729 tBinaryType *bt = gRegBinTypes;
\r
731 for(; bt; bt = bt->Next)
\r
733 if( (ident & bt->Mask) == (Uint)bt->Ident )
\r
734 return bt->Relocate( (void*)Base);
\r
737 Warning("[BIN ] 0x%x is an unknown file type. (0x%x 0x%x 0x%x 0x%x)",
\r
738 Base, ident&0xFF, ident>>8, ident>>16, ident>>24);
\r
743 * \fn int Binary_GetSymbol(char *Name, Uint *Val)
\r
744 * \brief Get a symbol value
\r
745 * \return Value of symbol or -1 on error
\r
747 * Gets the value of a symbol from either the currently loaded
\r
748 * libraries or the kernel's exports.
\r
750 int Binary_GetSymbol(char *Name, Uint *Val)
\r
752 if( Binary_GetSymbolEx(Name, Val) ) return 1;
\r
757 * \fn Uint Binary_GetSymbolEx(char *Name, Uint *Value)
\r
758 * \brief Get a symbol value
\r
760 * Gets the value of a symbol from either the currently loaded
\r
761 * libraries or the kernel's exports.
\r
763 Uint Binary_GetSymbolEx(char *Name, Uint *Value)
\r
767 int numKSyms = ((Uint)&gKernelSymbolsEnd-(Uint)&gKernelSymbols)/sizeof(tKernelSymbol);
\r
770 for( i = 0; i < numKSyms; i++ )
\r
772 if(strcmp(Name, gKernelSymbols[i].Name) == 0) {
\r
773 *Value = gKernelSymbols[i].Value;
\r
778 // Scan Loaded Libraries
\r
779 for(pKBin = glLoadedKernelLibs;
\r
781 pKBin = pKBin->Next )
\r
783 if( Binary_FindSymbol(pKBin->Base, Name, Value) ) {
\r
788 Warning("[BIN ] Unable to find symbol '%s'", Name);
\r
793 * \fn Uint Binary_GetSymbolBin(void *Base, char *Name, Uint *val)
\r
794 * \brief Get a symbol from the specified library
\r
795 * \param Base Base address
\r
796 * \param Name Name of symbol to find
\r
797 * \param val Pointer to place final value
\r
799 Uint Binary_FindSymbol(void *Base, char *Name, Uint *val)
\r
801 Uint32 ident = *(Uint32*) Base;
\r
802 tBinaryType *bt = gRegBinTypes;
\r
804 for(; bt; bt = bt->Next)
\r
806 if( (ident & bt->Mask) == (Uint)bt->Ident )
\r
807 return bt->GetSymbol(Base, Name, val);
\r
810 Warning("[BIN ] 0x%x is an unknown file type. (0x%x 0x%x 0x%x 0x%x)",
\r
811 Base, ident&0xFF, ident>>8, ident>>16, ident>>24);
\r