X-Git-Url: https://git.ucc.asn.au/?a=blobdiff_plain;f=Kernel%2Fbinary.c;h=77309f38f75e72701055bfd0921f2a5e515b2dd0;hb=0e9730abc6c9ba710a3f71356720a70d79e407ab;hp=7175799bf85b3adf07be5a71131051b9453f5bc2;hpb=bfdc62d196c8d94f35986fac0ddca5b8003ed3fb;p=tpg%2Facess2.git diff --git a/Kernel/binary.c b/Kernel/binary.c index 7175799b..77309f38 100644 --- a/Kernel/binary.c +++ b/Kernel/binary.c @@ -2,27 +2,17 @@ * Acess2 * Common Binary Loader */ -#include +#define DEBUG 0 +#include #include -#define DEBUG 1 - -#if DEBUG -#else -# undef ENTER -# undef LOG -# undef LEAVE -# define ENTER(...) -# define LOG(...) -# define LEAVE(...) -#endif - // === 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 === @@ -34,9 +24,9 @@ typedef struct sKernelBin { // === IMPORTS === extern int Proc_Clone(Uint *Err, Uint Flags); -extern void Proc_SetThreadName(char *Name); +extern char *Threads_GetName(int ID); +extern void Threads_Exit(int, int); extern Uint MM_ClearUser(); -extern void Proc_Exit(); extern void Proc_StartUser(Uint Entrypoint, Uint *Bases, int ArgC, char **ArgV, char **EnvP, int DataSize); extern tKernelSymbol gKernelSymbols[]; extern void gKernelSymbolsEnd; @@ -52,7 +42,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; @@ -64,6 +54,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) */ @@ -146,7 +146,7 @@ int Proc_Execve(char *File, char **ArgV, char **EnvP) strcpy(savedFile, File); // --- Set Process Name - Proc_SetThreadName(File); + Threads_SetName(File); // --- Clear User Address space MM_ClearUser(); @@ -156,8 +156,8 @@ int Proc_Execve(char *File, char **ArgV, char **EnvP) free(savedFile); if(bases[0] == 0) { - Warning("Proc_Execve - Unable to load '%s'", File); - Proc_Exit(); + Warning("Proc_Execve - Unable to load '%s'", Threads_GetName(-1)); + Threads_Exit(0, 0); for(;;); } @@ -189,7 +189,7 @@ Uint Binary_Load(char *file, Uint *entryPoint) sTruePath = VFS_GetTruePath(file); if(sTruePath == NULL) { - Warning("[BIN ] '%s' does not exist.", file); + Warning("[BIN ] '%s' does not exist.", file); LEAVE('x', 0); return 0; } @@ -244,17 +244,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; } @@ -326,12 +325,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; @@ -427,23 +437,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 ); @@ -529,37 +546,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]; } @@ -567,11 +584,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; @@ -580,16 +598,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; @@ -685,13 +703,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 @@ -735,7 +749,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; } @@ -790,13 +804,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; @@ -804,10 +818,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);