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 Uint MM_ClearUser();
\r
28 extern void Threads_Exit();
\r
29 extern void Proc_StartUser(Uint Entrypoint, Uint *Bases, int ArgC, char **ArgV, char **EnvP, int DataSize);
\r
30 extern tKernelSymbol gKernelSymbols[];
\r
31 extern void gKernelSymbolsEnd;
\r
32 extern tBinaryType gELF_Info;
\r
34 // === PROTOTYPES ===
\r
35 int Proc_Execve(char *File, char **ArgV, char **EnvP);
\r
36 Uint Binary_Load(char *file, Uint *entryPoint);
\r
37 tBinary *Binary_GetInfo(char *truePath);
\r
38 Uint Binary_MapIn(tBinary *binary);
\r
39 Uint Binary_IsMapped(tBinary *binary);
\r
40 tBinary *Binary_DoLoad(char *truePath);
\r
41 void Binary_Dereference(tBinary *Info);
\r
42 Uint Binary_Relocate(void *Base);
\r
43 Uint Binary_GetSymbolEx(char *Name, Uint *Value);
\r
44 Uint Binary_FindSymbol(void *Base, char *Name, Uint *val);
\r
47 int glBinListLock = 0;
\r
48 tBinary *glLoadedBinaries = NULL;
\r
49 char **gsaRegInterps = NULL;
\r
50 int giRegInterps = 0;
\r
51 int glKBinListLock = 0;
\r
52 tKernelBin *glLoadedKernelLibs;
\r
53 tBinaryType *gRegBinTypes = &gELF_Info;
\r
55 // === FUNCTIONS ===
\r
57 * \fn int Proc_Spawn(char *Path)
\r
59 int Proc_Spawn(char *Path)
\r
61 char stackPath[strlen(Path)+1];
\r
63 strcpy(stackPath, Path);
\r
65 LOG("stackPath = '%s'\n", stackPath);
\r
67 if(Proc_Clone(NULL, CLONE_VM) == 0)
\r
70 char *args[2] = {stackPath, NULL};
\r
71 LOG("stackPath = '%s'\n", stackPath);
\r
72 Proc_Execve(stackPath, args, &args[1]);
\r
79 * \fn int Proc_Execve(char *File, char **ArgV, char **EnvP)
\r
80 * \brief Replace the current user image with another
\r
81 * \param File File to load as the next image
\r
82 * \param ArgV Arguments to pass to user
\r
83 * \param EnvP User's environment
\r
84 * \note Called Proc_ for historical reasons
\r
86 int Proc_Execve(char *File, char **ArgV, char **EnvP)
\r
90 char *argenvBuf, *strBuf;
\r
91 char **argvSaved, **envpSaved;
\r
94 Uint bases[2] = {0};
\r
96 ENTER("sFile pArgV pEnvP", File, ArgV, EnvP);
\r
98 // --- Save File, ArgV and EnvP (also get argc)
\r
100 // Count Arguments, Environment Variables and total string sizes
\r
102 for( argc = 0; ArgV && ArgV[argc]; argc++ )
\r
103 argenvBytes += strlen(ArgV[argc])+1;
\r
104 for( envc = 0; EnvP && EnvP[envc]; envc++ )
\r
105 argenvBytes += strlen(EnvP[envc])+1;
\r
106 argenvBytes = (argenvBytes + sizeof(void*)-1) & ~(sizeof(void*)-1);
\r
107 argenvBytes += (argc+1)*sizeof(void*) + (envc+1)*sizeof(void*);
\r
110 argenvBuf = malloc(argenvBytes);
\r
111 if(argenvBuf == NULL) {
\r
112 Warning("Proc_Execve - What the hell? The kernel is out of heap space");
\r
115 strBuf = argenvBuf + (argc+1)*sizeof(void*) + (envc+1)*sizeof(void*);
\r
118 argvSaved = (char **) argenvBuf;
\r
119 for( i = 0; i < argc; i++ )
\r
121 argvSaved[i] = strBuf;
\r
122 strcpy(argvSaved[i], ArgV[i]);
\r
123 strBuf += strlen(ArgV[i])+1;
\r
125 argvSaved[i] = NULL;
\r
126 envpSaved = &argvSaved[i+1];
\r
127 for( i = 0; i < envc; i++ )
\r
129 envpSaved[i] = strBuf;
\r
130 strcpy(envpSaved[i], EnvP[i]);
\r
131 strBuf += strlen(EnvP[i])+1;
\r
134 savedFile = malloc(strlen(File)+1);
\r
135 strcpy(savedFile, File);
\r
137 // --- Set Process Name
\r
138 Threads_SetName(File);
\r
140 // --- Clear User Address space
\r
143 // --- Load new binary
\r
144 bases[0] = Binary_Load(savedFile, &entry);
\r
148 Warning("Proc_Execve - Unable to load '%s'", File);
\r
153 LOG("entry = 0x%x, bases[0] = 0x%x", entry, bases[0]);
\r
155 // --- And... Jump to it
\r
156 Proc_StartUser(entry, bases, argc, argvSaved, envpSaved, argenvBytes);
\r
157 for(;;); // Tell GCC that we never return
\r
161 * \fn Uint Binary_Load(char *file, Uint *entryPoint)
\r
163 Uint Binary_Load(char *file, Uint *entryPoint)
\r
169 ENTER("sfile", file);
\r
171 // Sanity Check Argument
\r
177 // Get True File Path
\r
178 sTruePath = VFS_GetTruePath(file);
\r
180 if(sTruePath == NULL) {
\r
181 Warning("[BIN ] '%s' does not exist.", file);
\r
186 LOG("sTruePath = '%s'", sTruePath);
\r
188 // Check if the binary has already been loaded
\r
189 if( !(pBinary = Binary_GetInfo(sTruePath)) )
\r
190 pBinary = Binary_DoLoad(sTruePath); // Else load it
\r
196 if(pBinary == NULL) {
\r
202 if( (base = Binary_IsMapped(pBinary)) ) {
\r
208 // Map into process space
\r
209 base = Binary_MapIn(pBinary); // If so then map it in
\r
211 // Check for errors
\r
218 if(pBinary->Interpreter) {
\r
220 if( Binary_Load(pBinary->Interpreter, &start) == 0 ) {
\r
224 *entryPoint = start;
\r
227 *entryPoint = pBinary->Entry - pBinary->Base + base;
\r
230 LOG("*entryPoint = 0x%x", *entryPoint);
\r
232 return base; // Pass the base as an argument to the user if there is an interpreter
\r
236 \fn tBinary *Binary_GetInfo(char *truePath)
\r
237 \brief Finds a matching binary entry
\r
238 \param truePath File Identifier (True path name)
\r
240 tBinary *Binary_GetInfo(char *truePath)
\r
243 pBinary = glLoadedBinaries;
\r
246 if(strcmp(pBinary->TruePath, truePath) == 0)
\r
248 pBinary = pBinary->Next;
\r
254 \fn Uint Binary_MapIn(tBinary *binary)
\r
255 \brief Maps an already-loaded binary into an address space.
\r
256 \param binary Pointer to globally stored data.
\r
258 Uint Binary_MapIn(tBinary *binary)
\r
264 // Reference Executable (Makes sure that it isn't unloaded)
\r
265 binary->ReferenceCount ++;
\r
268 base = binary->Base;
\r
270 // Check if base is free
\r
273 for(i=0;i<binary->NumPages;i++)
\r
275 if( MM_GetPhysAddr( binary->Pages[i].Virtual & ~0xFFF ) ) {
\r
277 LOG("Address 0x%x is taken\n", binary->Pages[i].Virtual & ~0xFFF);
\r
283 // Check if the executable has no base or it is not free
\r
286 // If so, give it a base
\r
287 base = BIN_HIGHEST;
\r
288 while(base >= BIN_LOWEST)
\r
290 for(i=0;i<binary->NumPages;i++)
\r
292 addr = binary->Pages[i].Virtual & ~0xFFF;
\r
293 addr -= binary->Base;
\r
295 if( MM_GetPhysAddr( addr ) ) break;
\r
297 // If space was found, break
\r
298 if(i == binary->NumPages) break;
\r
299 // Else decrement pointer and try again
\r
300 base -= BIN_GRANUALITY;
\r
305 if(base < BIN_LOWEST) {
\r
306 Warning("[BIN ] Executable '%s' cannot be loaded, no space", binary->TruePath);
\r
310 // Map Executable In
\r
311 for(i=0;i<binary->NumPages;i++)
\r
313 addr = binary->Pages[i].Virtual & ~0xFFF;
\r
314 addr -= binary->Base;
\r
316 LOG("%i - 0x%x to 0x%x", i, addr, binary->Pages[i].Physical);
\r
317 MM_Map( addr, (Uint) (binary->Pages[i].Physical) );
\r
318 if( binary->Pages[i].Physical & 1) // Read-Only
\r
319 MM_SetFlags( addr, MM_PFLAG_RO, -1 );
\r
321 MM_SetFlags( addr, MM_PFLAG_COW, -1 );
\r
324 //LOG("*0x%x = 0x%x\n", binary->Pages[0].Virtual, *(Uint*)binary->Pages[0].Virtual);
\r
331 * \fn Uint Binary_IsMapped(tBinary *binary)
\r
332 * \brief Check if a binary is already mapped into the address space
\r
333 * \param binary Binary information to check
\r
334 * \return Current Base or 0
\r
336 Uint Binary_IsMapped(tBinary *binary)
\r
340 // Check prefered base
\r
341 iBase = binary->Base;
\r
342 if(MM_GetPage( iBase ) == (binary->Pages[0].Physical & ~0xFFF))
\r
345 for(iBase = BIN_HIGHEST;
\r
346 iBase >= BIN_LOWEST;
\r
347 iBase -= BIN_GRANUALITY)
\r
349 if(MM_GetPage( iBase ) == (binary->Pages[0].Physical & ~0xFFF))
\r
358 * \fn tBinary *Binary_DoLoad(char *truePath)
\r
359 * \brief Loads a binary file into memory
\r
360 * \param truePath Absolute filename of binary
\r
362 tBinary *Binary_DoLoad(char *truePath)
\r
367 tBinaryType *bt = gRegBinTypes;
\r
369 ENTER("struePath", truePath);
\r
372 fp = VFS_Open(truePath, VFS_OPENFLAG_READ);
\r
374 LOG("Unable to load file, access denied");
\r
380 VFS_Read(fp, 4, &ident);
\r
381 VFS_Seek(fp, 0, SEEK_SET);
\r
383 for(; bt; bt = bt->Next)
\r
385 if( (ident & bt->Mask) != (Uint)bt->Ident )
\r
387 pBinary = bt->Load(fp);
\r
391 Warning("[BIN ] '%s' is an unknown file type. (0x%x 0x%x 0x%x 0x%x)",
\r
392 truePath, ident&0xFF, (ident>>8)&0xFF, (ident>>16)&0xFF, (ident>>24)&0xFF);
\r
398 if(pBinary == NULL) {
\r
403 // Initialise Structure
\r
404 pBinary->ReferenceCount = 0;
\r
405 pBinary->TruePath = malloc( strlen(truePath) + 1 );
\r
406 strcpy(pBinary->TruePath, truePath);
\r
408 // Debug Information
\r
409 LOG("Interpreter: '%s'", pBinary->Interpreter);
\r
410 LOG("Base: 0x%x, Entry: 0x%x", pBinary->Base, pBinary->Entry);
\r
411 LOG("NumPages: %i", pBinary->NumPages);
\r
414 for(i=0;i<pBinary->NumPages;i++)
\r
418 paddr = (Uint)MM_AllocPhys();
\r
419 MM_RefPhys( paddr ); // Make sure it is _NOT_ freed until we want it to be
\r
420 dest = MM_MapTemp( paddr );
\r
421 dest += pBinary->Pages[i].Virtual & 0xFFF;
\r
422 LOG("dest = 0x%x, paddr = 0x%x", dest, paddr);
\r
423 LOG("Pages[%i]={Physical:0x%x,Virtual:0x%x,Size:0x%x}",
\r
424 i, pBinary->Pages[i].Physical, pBinary->Pages[i].Virtual, pBinary->Pages[i].Size);
\r
427 if(pBinary->Pages[i].Physical == -1) {
\r
428 LOG("%i - ZERO", i);
\r
429 memsetd( (void*)dest, 0, 1024 );
\r
433 VFS_Seek( fp, pBinary->Pages[i].Physical, 1 );
\r
434 if(pBinary->Pages[i].Size != 0x1000) {
\r
435 LOG("%i - 0x%x - 0x%x bytes",
\r
436 i, pBinary->Pages[i].Physical, pBinary->Pages[i].Size);
\r
437 memset( (void*)dest, 0, 0x1000 -(dest&0xFFF) );
\r
438 VFS_Read( fp, pBinary->Pages[i].Size, (void*)dest );
\r
440 LOG("%i - 0x%x", i, pBinary->Pages[i].Physical);
\r
441 VFS_Read( fp, 0x1000, (void*)dest );
\r
444 pBinary->Pages[i].Physical = paddr;
\r
445 MM_FreeTemp( dest );
\r
447 LOG("Page Count: %i", pBinary->NumPages);
\r
453 LOCK(&glBinListLock);
\r
454 pBinary->Next = glLoadedBinaries;
\r
455 glLoadedBinaries = pBinary;
\r
456 RELEASE(&glBinListLock);
\r
459 LEAVE('p', pBinary);
\r
464 * \fn void Binary_Unload(void *Base)
\r
465 * \brief Unload / Unmap a binary
\r
466 * \param Base Loaded Base
\r
467 * \note Currently used only for kernel libaries
\r
469 void Binary_Unload(void *Base)
\r
472 tKernelBin *prev = NULL;
\r
475 if((Uint)Base < 0xC0000000)
\r
477 // TODO: User Binaries
\r
478 Warning("[BIN ] Unloading user binaries is currently unimplemented");
\r
482 // Kernel Libraries
\r
483 for(pKBin = glLoadedKernelLibs;
\r
485 prev = pKBin, pKBin = pKBin->Next)
\r
488 if(pKBin->Base != Base) continue;
\r
489 // Deallocate Memory
\r
490 for(i = 0; i < pKBin->Info->NumPages; i++) {
\r
491 MM_Deallocate( (Uint)Base + (i << 12) );
\r
493 // Dereference Binary
\r
494 Binary_Dereference( pKBin->Info );
\r
495 // Remove from list
\r
496 if(prev) prev->Next = pKBin->Next;
\r
497 else glLoadedKernelLibs = pKBin->Next;
\r
505 * \fn void Binary_Dereference(tBinary *Info)
\r
506 * \brief Dereferences and if nessasary, deletes a binary
\r
507 * \param Info Binary information structure
\r
509 void Binary_Dereference(tBinary *Info)
\r
511 // Decrement reference count
\r
512 Info->ReferenceCount --;
\r
514 // Check if it is still in use
\r
515 if(Info->ReferenceCount) return;
\r
517 /// \todo Implement binary freeing
\r
521 \fn char *Binary_RegInterp(char *path)
\r
522 \brief Registers an Interpreter
\r
523 \param path Path to interpreter provided by executable
\r
525 char *Binary_RegInterp(char *path)
\r
528 // NULL Check Argument
\r
529 if(path == NULL) return NULL;
\r
530 // NULL Check the array
\r
531 if(gsaRegInterps == NULL)
\r
534 gsaRegInterps = malloc( sizeof(char*) );
\r
535 gsaRegInterps[0] = malloc( strlen(path) );
\r
536 strcpy(gsaRegInterps[0], path);
\r
537 return gsaRegInterps[0];
\r
541 for( i = 0; i < giRegInterps; i++ )
\r
543 if(strcmp(gsaRegInterps[i], path) == 0)
\r
544 return gsaRegInterps[i];
\r
547 // Interpreter is not in list
\r
549 gsaRegInterps = malloc( sizeof(char*)*giRegInterps );
\r
550 gsaRegInterps[i] = malloc( strlen(path) );
\r
551 strcpy(gsaRegInterps[i], path);
\r
552 return gsaRegInterps[i];
\r
556 // Kernel Binary Handling
\r
559 * \fn void *Binary_LoadKernel(char *path)
\r
560 * \brief Load a binary into kernel space
\r
561 * \note This function shares much with #Binary_Load, but does it's own mapping
\r
563 void *Binary_LoadKernel(char *file)
\r
567 tKernelBin *pKBinary;
\r
572 ENTER("sfile", file);
\r
574 // Sanity Check Argument
\r
580 // Get True File Path
\r
581 sTruePath = VFS_GetTruePath(file);
\r
582 if(sTruePath == NULL) {
\r
587 // Check if the binary has already been loaded
\r
588 if( (pBinary = Binary_GetInfo(sTruePath)) )
\r
590 for(pKBinary = glLoadedKernelLibs;
\r
592 pKBinary = pKBinary->Next )
\r
594 if(pKBinary->Info == pBinary) {
\r
595 LEAVE('p', pKBinary->Base);
\r
596 return pKBinary->Base;
\r
601 pBinary = Binary_DoLoad(sTruePath); // Else load it
\r
604 if(pBinary == NULL) {
\r
610 // Now pBinary is valid (either freshly loaded or only user mapped)
\r
611 // So, map it into kernel space
\r
614 // Reference Executable (Makes sure that it isn't unloaded)
\r
615 pBinary->ReferenceCount ++;
\r
617 // Check compiled base
\r
618 base = pBinary->Base;
\r
620 if(base < KLIB_LOWEST || base > KLIB_HIGHEST || base + (pBinary->NumPages<<12) > KLIB_HIGHEST) {
\r
623 // - Check if it is a valid base address
\r
626 for(i=0;i<pBinary->NumPages;i++)
\r
628 if( MM_GetPhysAddr( pBinary->Pages[i].Virtual & ~0xFFF ) ) {
\r
630 LOG("Address 0x%x is taken\n", pBinary->Pages[i].Virtual & ~0xFFF);
\r
636 // Check if the executable has no base or it is not free
\r
639 // If so, give it a base
\r
640 base = KLIB_LOWEST;
\r
641 while(base < KLIB_HIGHEST)
\r
643 for(i = 0; i < pBinary->NumPages; i++)
\r
645 addr = pBinary->Pages[i].Virtual & ~0xFFF;
\r
646 addr -= pBinary->Base;
\r
648 if( MM_GetPhysAddr( addr ) ) break;
\r
650 // If space was found, break
\r
651 if(i == pBinary->NumPages) break;
\r
652 // Else decrement pointer and try again
\r
653 base += KLIB_GRANUALITY;
\r
658 if(base >= KLIB_HIGHEST) {
\r
659 Warning("[BIN ] Executable '%s' cannot be loaded into kernel, no space", pBinary->TruePath);
\r
660 Binary_Dereference( pBinary );
\r
665 LOG("base = 0x%x", base);
\r
668 LOG("pBinary = {NumPages:%i, Pages=%p}", pBinary->NumPages, pBinary->Pages);
\r
669 for(i = 0; i < pBinary->NumPages; i++)
\r
671 addr = pBinary->Pages[i].Virtual & ~0xFFF;
\r
672 addr -= pBinary->Base;
\r
674 LOG("%i - 0x%x to 0x%x", i, addr, pBinary->Pages[i].Physical);
\r
675 MM_Map( addr, (Uint) (pBinary->Pages[i].Physical) );
\r
676 MM_SetFlags( addr, MM_PFLAG_KERNEL, MM_PFLAG_KERNEL );
\r
677 #if 0 // Why was this here? It's the kernel
\r
678 if( pBinary->Pages[i].Physical & 1) // Read-Only
\r
679 MM_SetFlags( addr, MM_PFLAG_RO, MM_PFLAG_KERNEL );
\r
681 MM_SetFlags( addr, MM_PFLAG_COW, MM_PFLAG_KERNEL );
\r
682 //MM_SetCOW( addr );
\r
686 // Relocate Library
\r
687 if( !Binary_Relocate( (void*)base ) )
\r
689 Warning("[BIN ] Relocation of '%s' failed, unloading", sTruePath);
\r
690 Binary_Unload( (void*)base );
\r
691 Binary_Dereference( pBinary );
\r
696 // Add to list (relocator must look at itself manually, not via Binary_GetSymbol)
\r
697 pKBinary = malloc(sizeof(*pKBinary));
\r
698 pKBinary->Base = (void*)base;
\r
699 pKBinary->Info = pBinary;
\r
700 LOCK( &glKBinListLock );
\r
701 pKBinary->Next = glLoadedKernelLibs;
\r
702 glLoadedKernelLibs = pKBinary;
\r
703 RELEASE( &glKBinListLock );
\r
706 return (void*)base;
\r
710 * \fn Uint Binary_Relocate(void *Base)
\r
711 * \brief Relocates a loaded binary (used by kernel libraries)
\r
712 * \param Base Loaded base address of binary
\r
713 * \return Boolean Success
\r
715 Uint Binary_Relocate(void *Base)
\r
717 Uint32 ident = *(Uint32*) Base;
\r
718 tBinaryType *bt = gRegBinTypes;
\r
720 for(; bt; bt = bt->Next)
\r
722 if( (ident & bt->Mask) == (Uint)bt->Ident )
\r
723 return bt->Relocate( (void*)Base);
\r
726 Warning("[BIN ] 0x%x is an unknown file type. (0x%x 0x%x 0x%x 0x%x)",
\r
727 Base, ident&0xFF, ident>>8, ident>>16, ident>>24);
\r
732 * \fn int Binary_GetSymbol(char *Name, Uint *Val)
\r
733 * \brief Get a symbol value
\r
734 * \return Value of symbol or -1 on error
\r
736 * Gets the value of a symbol from either the currently loaded
\r
737 * libraries or the kernel's exports.
\r
739 int Binary_GetSymbol(char *Name, Uint *Val)
\r
741 if( Binary_GetSymbolEx(Name, Val) ) return 1;
\r
746 * \fn Uint Binary_GetSymbolEx(char *Name, Uint *Value)
\r
747 * \brief Get a symbol value
\r
749 * Gets the value of a symbol from either the currently loaded
\r
750 * libraries or the kernel's exports.
\r
752 Uint Binary_GetSymbolEx(char *Name, Uint *Value)
\r
756 int numKSyms = ((Uint)&gKernelSymbolsEnd-(Uint)&gKernelSymbols)/sizeof(tKernelSymbol);
\r
759 for( i = 0; i < numKSyms; i++ )
\r
761 if(strcmp(Name, gKernelSymbols[i].Name) == 0) {
\r
762 *Value = gKernelSymbols[i].Value;
\r
767 // Scan Loaded Libraries
\r
768 for(pKBin = glLoadedKernelLibs;
\r
770 pKBin = pKBin->Next )
\r
772 if( Binary_FindSymbol(pKBin->Base, Name, Value) ) {
\r
777 Warning("[BIN ] Unable to find symbol '%s'", Name);
\r
782 * \fn Uint Binary_GetSymbolBin(void *Base, char *Name, Uint *val)
\r
783 * \brief Get a symbol from the specified library
\r
784 * \param Base Base address
\r
785 * \param Name Name of symbol to find
\r
786 * \param val Pointer to place final value
\r
788 Uint Binary_FindSymbol(void *Base, char *Name, Uint *val)
\r
790 Uint32 ident = *(Uint32*) Base;
\r
791 tBinaryType *bt = gRegBinTypes;
\r
793 for(; bt; bt = bt->Next)
\r
795 if( (ident & bt->Mask) == (Uint)bt->Ident )
\r
796 return bt->GetSymbol(Base, Name, val);
\r
799 Warning("[BIN ] 0x%x is an unknown file type. (0x%x 0x%x 0x%x 0x%x)",
\r
800 Base, ident&0xFF, ident>>8, ident>>16, ident>>24);
\r