Fixed bug in keyboard driver on real hardware (aux p/s2 port code)
[tpg/acess2.git] / Kernel / binary.c
index 7175799..77309f3 100644 (file)
@@ -2,27 +2,17 @@
  * Acess2\r
  * Common Binary Loader\r
  */\r
-#include <common.h>\r
+#define DEBUG  0\r
+#include <acess.h>\r
 #include <binary.h>\r
 \r
-#define DEBUG  1\r
-\r
-#if DEBUG\r
-#else\r
-# undef ENTER\r
-# undef LOG\r
-# undef LEAVE\r
-# define ENTER(...)\r
-# define LOG(...)\r
-# define LEAVE(...)\r
-#endif\r
-\r
 // === CONSTANTS ===\r
 #define BIN_LOWEST     MM_USER_MIN             // 1MiB\r
 #define BIN_GRANUALITY 0x10000         // 64KiB\r
+//! \todo Move 0xBC000000 to mm_virt.h\r
 #define BIN_HIGHEST    (0xBC000000-BIN_GRANUALITY)             // Just below the kernel\r
 #define        KLIB_LOWEST     MM_MODULE_MIN\r
-#define KLIB_GRANUALITY        0x8000          // 32KiB\r
+#define KLIB_GRANUALITY        0x10000         // 32KiB\r
 #define        KLIB_HIGHEST    (MM_MODULE_MAX-KLIB_GRANUALITY)\r
 \r
 // === TYPES ===\r
@@ -34,9 +24,9 @@ typedef struct sKernelBin {
 \r
 // === IMPORTS ===\r
 extern int     Proc_Clone(Uint *Err, Uint Flags);\r
-extern void    Proc_SetThreadName(char *Name);\r
+extern char    *Threads_GetName(int ID);\r
+extern void    Threads_Exit(int, int);\r
 extern Uint    MM_ClearUser();\r
-extern void    Proc_Exit();\r
 extern void    Proc_StartUser(Uint Entrypoint, Uint *Bases, int ArgC, char **ArgV, char **EnvP, int DataSize);\r
 extern tKernelSymbol   gKernelSymbols[];\r
 extern void            gKernelSymbolsEnd;\r
@@ -52,7 +42,7 @@ tBinary *Binary_DoLoad(char *truePath);
 void   Binary_Dereference(tBinary *Info);\r
 Uint   Binary_Relocate(void *Base);\r
 Uint   Binary_GetSymbolEx(char *Name, Uint *Value);\r
-Uint   Binary_FindSymbol(void *Base, char *Name, Uint *val);\r
+Uint   Binary_FindSymbol(void *Base, char *Name, Uint *Val);\r
 \r
 // === GLOBALS ===\r
  int   glBinListLock = 0;\r
@@ -64,6 +54,16 @@ tKernelBin   *glLoadedKernelLibs;
 tBinaryType    *gRegBinTypes = &gELF_Info;\r
  \r
 // === FUNCTIONS ===\r
+/**\r
+ * \brief Registers a binary type\r
+ */\r
+int Binary_RegisterType(tBinaryType *Type)\r
+{\r
+       Type->Next = gRegBinTypes;\r
+       gRegBinTypes = Type;\r
+       return 1;\r
+}\r
+\r
 /**\r
  * \fn int Proc_Spawn(char *Path)\r
  */\r
@@ -146,7 +146,7 @@ int Proc_Execve(char *File, char **ArgV, char **EnvP)
        strcpy(savedFile, File);\r
        \r
        // --- Set Process Name\r
-       Proc_SetThreadName(File);\r
+       Threads_SetName(File);\r
        \r
        // --- Clear User Address space\r
        MM_ClearUser();\r
@@ -156,8 +156,8 @@ int Proc_Execve(char *File, char **ArgV, char **EnvP)
        free(savedFile);\r
        if(bases[0] == 0)\r
        {\r
-               Warning("Proc_Execve - Unable to load '%s'", File);\r
-               Proc_Exit();\r
+               Warning("Proc_Execve - Unable to load '%s'", Threads_GetName(-1));\r
+               Threads_Exit(0, 0);\r
                for(;;);\r
        }\r
        \r
@@ -189,7 +189,7 @@ Uint Binary_Load(char *file, Uint *entryPoint)
        sTruePath = VFS_GetTruePath(file);\r
        \r
        if(sTruePath == NULL) {\r
-               Warning("[BIN ] '%s' does not exist.", file);\r
+               Warning("[BIN  ] '%s' does not exist.", file);\r
                LEAVE('x', 0);\r
                return 0;\r
        }\r
@@ -244,17 +244,16 @@ Uint Binary_Load(char *file, Uint *entryPoint)
 }\r
 \r
 /**\r
- \fn tBinary *Binary_GetInfo(char *truePath)\r
- \brief Finds a matching binary entry\r
- \param truePath       File Identifier (True path name)\r
-*/\r
-tBinary *Binary_GetInfo(char *truePath)\r
+ * \brief Finds a matching binary entry\r
+ * \param TruePath     File Identifier (True path name)\r
+ */\r
+tBinary *Binary_GetInfo(char *TruePath)\r
 {\r
        tBinary *pBinary;\r
        pBinary = glLoadedBinaries;\r
        while(pBinary)\r
        {\r
-               if(strcmp(pBinary->TruePath, truePath) == 0)\r
+               if(strcmp(pBinary->TruePath, TruePath) == 0)\r
                        return pBinary;\r
                pBinary = pBinary->Next;\r
        }\r
@@ -326,12 +325,23 @@ Uint Binary_MapIn(tBinary *binary)
                addr += base;\r
                LOG("%i - 0x%x to 0x%x", i, addr, binary->Pages[i].Physical);\r
                MM_Map( addr, (Uint) (binary->Pages[i].Physical) );\r
-               if( binary->Pages[i].Physical & 1)      // Read-Only\r
+               \r
+               // Read-Only?\r
+               if( binary->Pages[i].Flags & BIN_PAGEFLAG_RO)\r
                        MM_SetFlags( addr, MM_PFLAG_RO, -1 );\r
                else\r
                        MM_SetFlags( addr, MM_PFLAG_COW, -1 );\r
+               \r
+               // Execute?\r
+               if( binary->Pages[i].Flags & BIN_PAGEFLAG_EXEC )\r
+                       MM_SetFlags( addr, MM_PFLAG_EXEC, -1 );\r
+               else\r
+                       MM_SetFlags( addr, MM_PFLAG_EXEC, 0 );\r
+               \r
        }\r
        \r
+       //Log("Mapped '%s' to 0x%x", binary->TruePath, base);\r
+       \r
        //LOG("*0x%x = 0x%x\n", binary->Pages[0].Virtual, *(Uint*)binary->Pages[0].Virtual);\r
        \r
        return base;\r
@@ -427,23 +437,30 @@ tBinary *Binary_DoLoad(char *truePath)
                Uint    dest;\r
                tPAddr  paddr;\r
                paddr = (Uint)MM_AllocPhys();\r
+               if(paddr == 0) {\r
+                       Warning("Binary_DoLoad - Physical memory allocation failed");\r
+                       for( ; i--; ) {\r
+                               MM_DerefPhys( pBinary->Pages[i].Physical );\r
+                       }\r
+                       return NULL;\r
+               }\r
                MM_RefPhys( paddr );    // Make sure it is _NOT_ freed until we want it to be\r
                dest = MM_MapTemp( paddr );\r
                dest += pBinary->Pages[i].Virtual & 0xFFF;\r
                LOG("dest = 0x%x, paddr = 0x%x", dest, paddr);\r
-               LOG("Pages[%i]={Physical:0x%x,Virtual:0x%x,Size:0x%x}",\r
+               LOG("Pages[%i]={Physical:0x%llx,Virtual:%p,Size:0x%x}",\r
                        i, pBinary->Pages[i].Physical, pBinary->Pages[i].Virtual, pBinary->Pages[i].Size);\r
                \r
                // Pure Empty Page\r
                if(pBinary->Pages[i].Physical == -1) {\r
                        LOG("%i - ZERO", i);\r
-                       memsetd( (void*)dest, 0, 1024 );\r
+                       memsetd( (void*)dest, 0, 1024 - (pBinary->Pages[i].Virtual & 0xFFF)/4 );\r
                }\r
                else\r
                {\r
                        VFS_Seek( fp, pBinary->Pages[i].Physical, 1 );\r
                        if(pBinary->Pages[i].Size != 0x1000) {\r
-                               LOG("%i - 0x%x - 0x%x bytes",\r
+                               LOG("%i - 0x%llx - 0x%x bytes",\r
                                        i, pBinary->Pages[i].Physical, pBinary->Pages[i].Size);\r
                                memset( (void*)dest, 0, 0x1000 -(dest&0xFFF) );\r
                                VFS_Read( fp, pBinary->Pages[i].Size, (void*)dest );\r
@@ -529,37 +546,37 @@ void Binary_Dereference(tBinary *Info)
 }\r
 \r
 /**\r
\fn char *Binary_RegInterp(char *path)\r
- \brief Registers an Interpreter\r
\param path   Path to interpreter provided by executable\r
-*/\r
-char *Binary_RegInterp(char *path)\r
* \fn char *Binary_RegInterp(char *Path)\r
\brief Registers an Interpreter\r
* \param Path Path to interpreter provided by executable\r
+ */\r
+char *Binary_RegInterp(char *Path)\r
 {\r
         int    i;\r
        // NULL Check Argument\r
-       if(path == NULL)        return NULL;\r
+       if(Path == NULL)        return NULL;\r
        // NULL Check the array\r
        if(gsaRegInterps == NULL)\r
        {\r
                giRegInterps = 1;\r
                gsaRegInterps = malloc( sizeof(char*) );\r
-               gsaRegInterps[0] = malloc( strlen(path) );\r
-               strcpy(gsaRegInterps[0], path);\r
+               gsaRegInterps[0] = malloc( strlen(Path) );\r
+               strcpy(gsaRegInterps[0], Path);\r
                return gsaRegInterps[0];\r
        }\r
        \r
        // Scan Array\r
        for( i = 0; i < giRegInterps; i++ )\r
        {\r
-               if(strcmp(gsaRegInterps[i], path) == 0)\r
+               if(strcmp(gsaRegInterps[i], Path) == 0)\r
                        return gsaRegInterps[i];\r
        }\r
        \r
        // Interpreter is not in list\r
        giRegInterps ++;\r
        gsaRegInterps = malloc( sizeof(char*)*giRegInterps );\r
-       gsaRegInterps[i] = malloc( strlen(path) );\r
-       strcpy(gsaRegInterps[i], path);\r
+       gsaRegInterps[i] = malloc( strlen(Path) );\r
+       strcpy(gsaRegInterps[i], Path);\r
        return gsaRegInterps[i];\r
 }\r
 \r
@@ -567,11 +584,12 @@ char *Binary_RegInterp(char *path)
 // Kernel Binary Handling\r
 // ============\r
 /**\r
- * \fn void *Binary_LoadKernel(char *path)\r
+ * \fn void *Binary_LoadKernel(char *File)\r
  * \brief Load a binary into kernel space\r
  * \note This function shares much with #Binary_Load, but does it's own mapping\r
+ * \param File File to load into the kernel\r
  */\r
-void *Binary_LoadKernel(char *file)\r
+void *Binary_LoadKernel(char *File)\r
 {\r
        char    *sTruePath;\r
        tBinary *pBinary;\r
@@ -580,16 +598,16 @@ void *Binary_LoadKernel(char *file)
        Uint    addr;\r
         int    i;\r
 \r
-       ENTER("sfile", file);\r
+       ENTER("sfile", File);\r
        \r
        // Sanity Check Argument\r
-       if(file == NULL) {\r
+       if(File == NULL) {\r
                LEAVE('n');\r
                return 0;\r
        }\r
 \r
        // Get True File Path\r
-       sTruePath = VFS_GetTruePath(file);\r
+       sTruePath = VFS_GetTruePath(File);\r
        if(sTruePath == NULL) {\r
                LEAVE('n');\r
                return 0;\r
@@ -685,13 +703,9 @@ void *Binary_LoadKernel(char *file)
                LOG("%i - 0x%x to 0x%x", i, addr, pBinary->Pages[i].Physical);\r
                MM_Map( addr, (Uint) (pBinary->Pages[i].Physical) );\r
                MM_SetFlags( addr, MM_PFLAG_KERNEL, MM_PFLAG_KERNEL );\r
-               #if 0   // Why was this here? It's the kernel\r
-               if( pBinary->Pages[i].Physical & 1)     // Read-Only\r
+               \r
+               if( pBinary->Pages[i].Flags & BIN_PAGEFLAG_RO)  // Read-Only?\r
                        MM_SetFlags( addr, MM_PFLAG_RO, MM_PFLAG_KERNEL );\r
-               else\r
-                       MM_SetFlags( addr, MM_PFLAG_COW, MM_PFLAG_KERNEL );\r
-                       //MM_SetCOW( addr );\r
-               #endif\r
        }
 \r
        // Relocate Library\r
@@ -735,7 +749,7 @@ Uint Binary_Relocate(void *Base)
        }\r
        \r
        Warning("[BIN ] 0x%x is an unknown file type. (0x%x 0x%x 0x%x 0x%x)",\r
-               Base, ident&0xFF, ident>>8, ident>>16, ident>>24);\r
+               Base, ident&0xFF, (ident>>8)&0xFF, (ident>>16)&0xFF, (ident>>24)&0xFF);\r
        return 0;\r
 }\r
 \r
@@ -790,13 +804,13 @@ Uint Binary_GetSymbolEx(char *Name, Uint *Value)
 }\r
 \r
 /**\r
- * \fn Uint Binary_GetSymbolBin(void *Base, char *Name, Uint *val)\r
+ * \fn Uint Binary_FindSymbol(void *Base, char *Name, Uint *Val)\r
  * \brief Get a symbol from the specified library\r
  * \param Base Base address\r
  * \param Name Name of symbol to find\r
- * \param val  Pointer to place final value\r
+ * \param Val  Pointer to place final value\r
  */\r
-Uint Binary_FindSymbol(void *Base, char *Name, Uint *val)\r
+Uint Binary_FindSymbol(void *Base, char *Name, Uint *Val)\r
 {\r
        Uint32  ident = *(Uint32*) Base;\r
        tBinaryType     *bt = gRegBinTypes;\r
@@ -804,10 +818,14 @@ Uint Binary_FindSymbol(void *Base, char *Name, Uint *val)
        for(; bt; bt = bt->Next)\r
        {\r
                if( (ident & bt->Mask) == (Uint)bt->Ident )\r
-                       return bt->GetSymbol(Base, Name, val);\r
+                       return bt->GetSymbol(Base, Name, Val);\r
        }\r
        \r
        Warning("[BIN ] 0x%x is an unknown file type. (0x%x 0x%x 0x%x 0x%x)",\r
                Base, ident&0xFF, ident>>8, ident>>16, ident>>24);\r
        return 0;\r
 }\r
+\r
+// === EXPORTS ===\r
+EXPORT(Binary_FindSymbol);\r
+EXPORT(Binary_Unload);\r

UCC git Repository :: git.ucc.asn.au