X-Git-Url: https://git.ucc.asn.au/?a=blobdiff_plain;f=Kernel%2Fbinary.c;h=b02ca6e98f35711b4fffcc3fa5dcccce85bf08f3;hb=ca05044548d5c1de87c030d625a305731a6cc665;hp=dae3cfaa8cf4c66138f1d50af58bf0e02913efba;hpb=9d3800f60f2212432e550a4e003ae65b498a4d36;p=tpg%2Facess2.git diff --git a/Kernel/binary.c b/Kernel/binary.c index dae3cfaa..b02ca6e9 100644 --- a/Kernel/binary.c +++ b/Kernel/binary.c @@ -3,15 +3,16 @@ * Common Binary Loader */ #define DEBUG 0 -#include +#include #include // === CONSTANTS === #define BIN_LOWEST MM_USER_MIN // 1MiB #define BIN_GRANUALITY 0x10000 // 64KiB +//! \todo Move 0xBC000000 to mm_virt.h #define BIN_HIGHEST (0xBC000000-BIN_GRANUALITY) // Just below the kernel #define KLIB_LOWEST MM_MODULE_MIN -#define KLIB_GRANUALITY 0x8000 // 32KiB +#define KLIB_GRANUALITY 0x10000 // 32KiB #define KLIB_HIGHEST (MM_MODULE_MAX-KLIB_GRANUALITY) // === TYPES === @@ -42,7 +43,7 @@ tBinary *Binary_DoLoad(char *truePath); void Binary_Dereference(tBinary *Info); Uint Binary_Relocate(void *Base); Uint Binary_GetSymbolEx(char *Name, Uint *Value); -Uint Binary_FindSymbol(void *Base, char *Name, Uint *val); +Uint Binary_FindSymbol(void *Base, char *Name, Uint *Val); // === GLOBALS === int glBinListLock = 0; @@ -54,6 +55,16 @@ tKernelBin *glLoadedKernelLibs; tBinaryType *gRegBinTypes = &gELF_Info; // === FUNCTIONS === +/** + * \brief Registers a binary type + */ +int Binary_RegisterType(tBinaryType *Type) +{ + Type->Next = gRegBinTypes; + gRegBinTypes = Type; + return 1; +} + /** * \fn int Proc_Spawn(char *Path) */ @@ -234,17 +245,16 @@ Uint Binary_Load(char *file, Uint *entryPoint) } /** - \fn tBinary *Binary_GetInfo(char *truePath) - \brief Finds a matching binary entry - \param truePath File Identifier (True path name) -*/ -tBinary *Binary_GetInfo(char *truePath) + * \brief Finds a matching binary entry + * \param TruePath File Identifier (True path name) + */ +tBinary *Binary_GetInfo(char *TruePath) { tBinary *pBinary; pBinary = glLoadedBinaries; while(pBinary) { - if(strcmp(pBinary->TruePath, truePath) == 0) + if(strcmp(pBinary->TruePath, TruePath) == 0) return pBinary; pBinary = pBinary->Next; } @@ -316,12 +326,23 @@ Uint Binary_MapIn(tBinary *binary) addr += base; LOG("%i - 0x%x to 0x%x", i, addr, binary->Pages[i].Physical); MM_Map( addr, (Uint) (binary->Pages[i].Physical) ); - if( binary->Pages[i].Physical & 1) // Read-Only + + // Read-Only? + if( binary->Pages[i].Flags & BIN_PAGEFLAG_RO) MM_SetFlags( addr, MM_PFLAG_RO, -1 ); else MM_SetFlags( addr, MM_PFLAG_COW, -1 ); + + // Execute? + if( binary->Pages[i].Flags & BIN_PAGEFLAG_EXEC ) + MM_SetFlags( addr, MM_PFLAG_EXEC, -1 ); + else + MM_SetFlags( addr, MM_PFLAG_EXEC, 0 ); + } + //Log("Mapped '%s' to 0x%x", binary->TruePath, base); + //LOG("*0x%x = 0x%x\n", binary->Pages[0].Virtual, *(Uint*)binary->Pages[0].Virtual); return base; @@ -417,23 +438,30 @@ tBinary *Binary_DoLoad(char *truePath) Uint dest; tPAddr paddr; paddr = (Uint)MM_AllocPhys(); + if(paddr == 0) { + Warning("Binary_DoLoad - Physical memory allocation failed"); + for( ; i--; ) { + MM_DerefPhys( pBinary->Pages[i].Physical ); + } + return NULL; + } MM_RefPhys( paddr ); // Make sure it is _NOT_ freed until we want it to be dest = MM_MapTemp( paddr ); dest += pBinary->Pages[i].Virtual & 0xFFF; LOG("dest = 0x%x, paddr = 0x%x", dest, paddr); - LOG("Pages[%i]={Physical:0x%x,Virtual:0x%x,Size:0x%x}", + LOG("Pages[%i]={Physical:0x%llx,Virtual:%p,Size:0x%x}", i, pBinary->Pages[i].Physical, pBinary->Pages[i].Virtual, pBinary->Pages[i].Size); // Pure Empty Page if(pBinary->Pages[i].Physical == -1) { LOG("%i - ZERO", i); - memsetd( (void*)dest, 0, 1024 ); + memsetd( (void*)dest, 0, 1024 - (pBinary->Pages[i].Virtual & 0xFFF)/4 ); } else { VFS_Seek( fp, pBinary->Pages[i].Physical, 1 ); if(pBinary->Pages[i].Size != 0x1000) { - LOG("%i - 0x%x - 0x%x bytes", + LOG("%i - 0x%llx - 0x%x bytes", i, pBinary->Pages[i].Physical, pBinary->Pages[i].Size); memset( (void*)dest, 0, 0x1000 -(dest&0xFFF) ); VFS_Read( fp, pBinary->Pages[i].Size, (void*)dest ); @@ -519,37 +547,37 @@ void Binary_Dereference(tBinary *Info) } /** - \fn char *Binary_RegInterp(char *path) - \brief Registers an Interpreter - \param path Path to interpreter provided by executable -*/ -char *Binary_RegInterp(char *path) + * \fn char *Binary_RegInterp(char *Path) + * \brief Registers an Interpreter + * \param Path Path to interpreter provided by executable + */ +char *Binary_RegInterp(char *Path) { int i; // NULL Check Argument - if(path == NULL) return NULL; + if(Path == NULL) return NULL; // NULL Check the array if(gsaRegInterps == NULL) { giRegInterps = 1; gsaRegInterps = malloc( sizeof(char*) ); - gsaRegInterps[0] = malloc( strlen(path) ); - strcpy(gsaRegInterps[0], path); + gsaRegInterps[0] = malloc( strlen(Path) ); + strcpy(gsaRegInterps[0], Path); return gsaRegInterps[0]; } // Scan Array for( i = 0; i < giRegInterps; i++ ) { - if(strcmp(gsaRegInterps[i], path) == 0) + if(strcmp(gsaRegInterps[i], Path) == 0) return gsaRegInterps[i]; } // Interpreter is not in list giRegInterps ++; gsaRegInterps = malloc( sizeof(char*)*giRegInterps ); - gsaRegInterps[i] = malloc( strlen(path) ); - strcpy(gsaRegInterps[i], path); + gsaRegInterps[i] = malloc( strlen(Path) ); + strcpy(gsaRegInterps[i], Path); return gsaRegInterps[i]; } @@ -557,11 +585,12 @@ char *Binary_RegInterp(char *path) // Kernel Binary Handling // ============ /** - * \fn void *Binary_LoadKernel(char *path) + * \fn void *Binary_LoadKernel(char *File) * \brief Load a binary into kernel space * \note This function shares much with #Binary_Load, but does it's own mapping + * \param File File to load into the kernel */ -void *Binary_LoadKernel(char *file) +void *Binary_LoadKernel(char *File) { char *sTruePath; tBinary *pBinary; @@ -570,16 +599,16 @@ void *Binary_LoadKernel(char *file) Uint addr; int i; - ENTER("sfile", file); + ENTER("sfile", File); // Sanity Check Argument - if(file == NULL) { + if(File == NULL) { LEAVE('n'); return 0; } // Get True File Path - sTruePath = VFS_GetTruePath(file); + sTruePath = VFS_GetTruePath(File); if(sTruePath == NULL) { LEAVE('n'); return 0; @@ -675,13 +704,9 @@ void *Binary_LoadKernel(char *file) LOG("%i - 0x%x to 0x%x", i, addr, pBinary->Pages[i].Physical); MM_Map( addr, (Uint) (pBinary->Pages[i].Physical) ); MM_SetFlags( addr, MM_PFLAG_KERNEL, MM_PFLAG_KERNEL ); - #if 0 // Why was this here? It's the kernel - if( pBinary->Pages[i].Physical & 1) // Read-Only + + if( pBinary->Pages[i].Flags & BIN_PAGEFLAG_RO) // Read-Only? MM_SetFlags( addr, MM_PFLAG_RO, MM_PFLAG_KERNEL ); - else - MM_SetFlags( addr, MM_PFLAG_COW, MM_PFLAG_KERNEL ); - //MM_SetCOW( addr ); - #endif } // Relocate Library @@ -725,7 +750,7 @@ Uint Binary_Relocate(void *Base) } Warning("[BIN ] 0x%x is an unknown file type. (0x%x 0x%x 0x%x 0x%x)", - Base, ident&0xFF, ident>>8, ident>>16, ident>>24); + Base, ident&0xFF, (ident>>8)&0xFF, (ident>>16)&0xFF, (ident>>24)&0xFF); return 0; } @@ -780,13 +805,13 @@ Uint Binary_GetSymbolEx(char *Name, Uint *Value) } /** - * \fn Uint Binary_GetSymbolBin(void *Base, char *Name, Uint *val) + * \fn Uint Binary_FindSymbol(void *Base, char *Name, Uint *Val) * \brief Get a symbol from the specified library * \param Base Base address * \param Name Name of symbol to find - * \param val Pointer to place final value + * \param Val Pointer to place final value */ -Uint Binary_FindSymbol(void *Base, char *Name, Uint *val) +Uint Binary_FindSymbol(void *Base, char *Name, Uint *Val) { Uint32 ident = *(Uint32*) Base; tBinaryType *bt = gRegBinTypes; @@ -794,10 +819,14 @@ Uint Binary_FindSymbol(void *Base, char *Name, Uint *val) for(; bt; bt = bt->Next) { if( (ident & bt->Mask) == (Uint)bt->Ident ) - return bt->GetSymbol(Base, Name, val); + return bt->GetSymbol(Base, Name, Val); } Warning("[BIN ] 0x%x is an unknown file type. (0x%x 0x%x 0x%x 0x%x)", Base, ident&0xFF, ident>>8, ident>>16, ident>>24); return 0; } + +// === EXPORTS === +EXPORT(Binary_FindSymbol); +EXPORT(Binary_Unload);