Kernel - Slight reworks to timer code
[tpg/acess2.git] / Kernel / modules.c
index eb742d4..98ca1a1 100644 (file)
 #define        USE_UDI 0
 
 // === PROTOTYPES ===
- int   Modules_LoadBuiltins(void);
- int   Module_RegisterLoader(tModuleLoader *Loader);
- int   Module_LoadMem(void *Buffer, Uint Length, char *ArgString);
- int   Module_LoadFile(char *Path, char *ArgString);
+ int   Module_int_Initialise(tModule *Module, const char *ArgString);
+void   Modules_int_GetBuiltinArray(void);
+void   Modules_LoadBuiltins(void);
+void   Modules_SetBuiltinParams(const char *Name, char *ArgString);
+ int   Modules_InitialiseBuiltin(const char *Name);
+// int Module_RegisterLoader(tModuleLoader *Loader);
+// int Module_LoadMem(void *Buffer, Uint Length, char *ArgString);
+// int Module_LoadFile(char *Path, char *ArgString);
  int   Module_int_ResolveDeps(tModule *Info);
- int   Module_IsLoaded(char *Name);
+ int   Module_IsLoaded(const char *Name);
+// int Module_EnsureLoaded(const char *Name);
 
 // === EXPORTS ===
 EXPORT(Module_RegisterLoader);
@@ -24,35 +29,58 @@ EXPORT(Module_RegisterLoader);
 #if USE_UDI
 extern int     UDI_LoadDriver(void *Base);
 #endif
-extern void    StartupPrint(char *Str);
-extern tModule gKernelModules[];
-extern void    gKernelModulesEnd;
+extern void    StartupPrint(const char *Str);
+extern tModule gKernelModules;
+extern tModule gKernelModulesEnd;
 
 // === GLOBALS ===
  int   giNumBuiltinModules = 0;
-tSpinlock      glModuleSpinlock;
+tShortSpinlock glModuleSpinlock;
 tModule        *gLoadedModules = NULL;
 tModuleLoader  *gModule_Loaders = NULL;
 tModule        *gLoadingModules = NULL;
+tModule        **gapBuiltinModules = NULL;
+char   **gasBuiltinModuleArgs;
 
 // === CODE ===
 /**
  * \brief Initialises a module
  * \param Module       Pointer to the module header
+ * \param ArgString    Comma separated list of module arguments
  * \return Zero on success, eModuleErrors or -1 on error
  * \retval -1  Returned if a dependency fails, or a circular dependency
  *              exists.
  * \retval 0   Returned on success
  * \retval >0  Error code form the module's initialisation function
  */
-int Module_int_Initialise(tModule *Module)
+int Module_int_Initialise(tModule *Module, const char *ArgString)
 {
         int    i, j;
         int    ret;
-       char    **deps;
+       const char      **deps;
+       char    **args;
        tModule *mod;
        
        ENTER("pModule", Module);
+       LOG("Module->Magic = 0x%x", Module->Magic);
+       if(Module->Magic != MODULE_MAGIC) {
+               Log_Warning(
+                       "Module",
+                       "Module %p is no a valid Acess2 module (0x%08x != 0x%08x)",
+                       Module, Module->Magic, MODULE_MAGIC
+                       );
+               LEAVE('i', MODULE_ERR_BADMODULE);
+               return MODULE_ERR_BADMODULE;
+       }
+       LOG("Module->Name = %p \"%s\"", Module->Name, Module->Name);
+       
+       if(Module->Arch != MODULE_ARCH_ID) {
+               Log_Warning(
+                       "Module",
+                       "Module %p (%s) is for another architecture (%i)",
+                       Module, Module->Name, Module->Arch
+                       );
+       }
        
        deps = Module->Dependencies;
        
@@ -84,23 +112,28 @@ int Module_int_Initialise(tModule *Module)
                                break;
                }
                if( mod ) {
-                       Log_Warning("Module", "Circular dependency detected");
+                       Log_Warning("Module", "Circular dependency detected (%s and %s)",
+                               mod->Name, Module->Name);
                        LEAVE_RET('i', -1);
                }
                
                // So, if it's not loaded, we better load it then
                for( i = 0; i < giNumBuiltinModules; i ++ )
                {
-                       if( strcmp(deps[j], gKernelModules[i].Name) == 0 )
+                       if( strcmp(deps[j], gapBuiltinModules[i]->Name) == 0 )
                                break;
                }
                if( i == giNumBuiltinModules ) {
-                       Log_Warning("Module", "Dependency '%s' for module '%s' failed");
+                       Log_Warning("Module", "Dependency '%s' for module '%s' failed",
+                               deps[j], Module->Name);
                        return -1;
                }
                
                // Dependency is not loaded, so load it
-               ret = Module_int_Initialise( &gKernelModules[i] );
+               ret = Module_int_Initialise(
+                       gapBuiltinModules[i],
+                       gasBuiltinModuleArgs ? gasBuiltinModuleArgs[i] : NULL
+                       );
                if( ret )
                {
                        // The only "ok" error is NOTNEEDED
@@ -111,12 +144,23 @@ int Module_int_Initialise(tModule *Module)
        
        // All Dependencies OK? Initialise
        StartupPrint(Module->Name);
-       Log_Log("Module", "Initialising %p '%s' v%i.%i...",
+       Log_Log("Module", "Starting %p '%s' v%i.%i",
                Module, Module->Name,
                Module->Version >> 8, Module->Version & 0xFF
                );
        
-       ret = Module->Init(NULL);
+       if( ArgString )
+               args = str_split( ArgString, ',' );
+       else
+               args = NULL;
+       
+       ret = Module->Init(args);
+       
+       if(args)        free(args);
+       
+       // Remove from loading list
+       gLoadingModules = gLoadingModules->Next;
+       
        if( ret != MODULE_ERR_OK ) {
                switch(ret)
                {
@@ -124,7 +168,7 @@ int Module_int_Initialise(tModule *Module)
                        Log_Warning("Module", "Unable to load, reason: Miscelanious");
                        break;
                case MODULE_ERR_NOTNEEDED:
-                       Log_Warning("Module", "Unable to load, reason: Module not needed");
+                       Log_Debug("Module", "Unable to load, reason: Module not needed");
                        break;
                case MODULE_ERR_MALLOC:
                        Log_Warning("Module", "Unable to load, reason: Error in malloc/realloc/calloc, probably not good");
@@ -136,36 +180,130 @@ int Module_int_Initialise(tModule *Module)
                LEAVE_RET('i', ret);
                return ret;
        }
-       
-       // Remove from loading list
-       gLoadingModules = gLoadingModules->Next;
+       LOG("ret = %i", ret);
        
        // Add to loaded list
-       LOCK( &glModuleSpinlock );
+       SHORTLOCK( &glModuleSpinlock );
        Module->Next = gLoadedModules;
        gLoadedModules = Module;
-       RELEASE( &glModuleSpinlock );
+       SHORTREL( &glModuleSpinlock );
        
        LEAVE_RET('i', 0);
 }
 
+/**
+ * \brief Scans the builtin modules and creates an array of them
+ */
+void Modules_int_GetBuiltinArray(void)
+{
+        int    i;
+       tModule *module;
+       
+       // Count
+       module = &gKernelModules;
+       i = 0;
+       while( (tVAddr)module < (tVAddr)&gKernelModulesEnd )
+       {
+               if(module->Magic == MODULE_MAGIC) {
+                       i ++;
+                       module ++;
+               }
+               else
+                       module = (void*)( (tVAddr)module + 4 );
+       }
+       
+       // Create
+       giNumBuiltinModules = i;
+       gasBuiltinModuleArgs = calloc( giNumBuiltinModules, sizeof(char*) );
+       gapBuiltinModules = malloc( giNumBuiltinModules * sizeof(tModule*) );
+       
+       
+       // Fill
+       module = &gKernelModules;
+       i = 0;
+       while( (tVAddr)module < (tVAddr)&gKernelModulesEnd )
+       {
+               if(module->Magic == MODULE_MAGIC) {
+                       gapBuiltinModules[i] = module;
+                       i ++;
+                       module ++;
+               }
+               else
+                       module = (void*)( (tVAddr)module + 4 );
+       }
+}
+
 /**
  * \brief Initialises builtin modules
  */
-int Modules_LoadBuiltins()
+void Modules_LoadBuiltins()
 {
         int    i;
        
-       // Count modules
-       giNumBuiltinModules = (Uint)&gKernelModulesEnd - (Uint)&gKernelModules;
-       giNumBuiltinModules /= sizeof(tModule);
+       if( !gapBuiltinModules )
+               Modules_int_GetBuiltinArray();
        
        for( i = 0; i < giNumBuiltinModules; i++ )
        {
-               Module_int_Initialise( &gKernelModules[i] );
+               Module_int_Initialise(
+                       gapBuiltinModules[i],
+                       (gasBuiltinModuleArgs ? gasBuiltinModuleArgs[i] : NULL)
+                       );
        }
        
-       return 0;
+       if( gasBuiltinModuleArgs != NULL )
+               free(gasBuiltinModuleArgs);
+}
+
+/**
+ * \brief Initialise a builtin module given it's name
+ * 
+ * E.g. Used by VTerm to load an alternate video driver at runtime
+ */
+int Modules_InitialiseBuiltin(const char *Name)
+{
+        int    i;
+       
+       // Check if it's loaded
+       if( Module_IsLoaded(Name) )
+               return 0;
+       
+       if( !gapBuiltinModules )
+               Modules_int_GetBuiltinArray();
+       
+       for( i = 0; i < giNumBuiltinModules; i++ )
+       {
+               if( strcmp(gapBuiltinModules[i]->Name, Name) == 0 ) {
+                       return Module_int_Initialise(gapBuiltinModules[i],
+                               (gasBuiltinModuleArgs ? gasBuiltinModuleArgs[i] : NULL)
+                               );
+               }
+       }
+       return -1;
+}
+
+/**
+ * \brief Sets the parameters for a builtin module
+ */
+void Modules_SetBuiltinParams(const char *Name, char *ArgString)
+{
+        int    i;
+       
+       if( gasBuiltinModuleArgs == NULL )
+       {
+               Modules_int_GetBuiltinArray();
+       }
+       
+       // I hate expensive scans
+       for( i = 0; i < giNumBuiltinModules; i++ )
+       {
+               if(strcmp( gapBuiltinModules[i]->Name, Name ) == 0) {
+                       gasBuiltinModuleArgs[i] = ArgString;
+                       return ;
+               }
+       }
+       
+       Log_Warning("Modules", "Unknown builtin kernel module '%s'", Name);
 }
 
 /**
@@ -186,7 +324,7 @@ int Module_RegisterLoader(tModuleLoader *Loader)
  * \fn int Module_LoadMem(void *Buffer, Uint Length, char *ArgString)
  * \brief Load a module from a memory location
  */
-int Module_LoadMem(void *Buffer, Uint Length, char *ArgString)
+int Module_LoadMem(void *Buffer, Uint Length, const char *ArgString)
 {
        char    path[VFS_MEMPATH_SIZE];
        
@@ -196,10 +334,10 @@ int Module_LoadMem(void *Buffer, Uint Length, char *ArgString)
 }
 
 /**
- * \fn int Module_LoadFile(char *Path, char *ArgString)
+ * \fn int Module_LoadFile(const char *Path, const char *ArgString)
  * \brief Load a module from a file
  */
-int Module_LoadFile(char *Path, char *ArgString)
+int Module_LoadFile(const char *Path, const char *ArgString)
 {
        void    *base;
        tModule *info;
@@ -242,54 +380,13 @@ int Module_LoadFile(char *Path, char *ArgString)
                return 0;
        }
        
-       // Check magic number
-       if(info->Magic != MODULE_MAGIC)
-       {
-               Log_Warning("Module", "Module's magic value is invalid (0x%x != 0x%x)", info->Magic, MODULE_MAGIC);
-               return 0;
-       }
-       
-       // Check Architecture
-       if(info->Arch != MODULE_ARCH_ID)
-       {
-               Log_Warning("Module", "Module is for a different architecture");
-               return 0;
-       }
-       
-       #if 1
-       if( Module_int_Initialise( info ) )
-       {
-               Binary_Unload(base);
-               return 0;
-       }
-       #else
-       // Resolve Dependencies
-       if( !Module_int_ResolveDeps(info) ) {
-               Binary_Unload(base);
-               return 0;
-       }
-       
-       Log_Log("Module", "Initialising %p '%s' v%i.%i...",
-                               info,
-                               info->Name,
-                               info->Version>>8, info->Version & 0xFF
-                               );
-       
-       // Call Initialiser
-       //if( info->Init( ArgString ) != 0 )
-       if( info->Init( NULL ) == 0 )
+       // Initialise (and register)
+       if( Module_int_Initialise( info, ArgString ) )
        {
                Binary_Unload(base);
                return 0;
        }
        
-       // Add to list
-       LOCK( &glModuleSpinlock );
-       info->Next = gLoadedModules;
-       gLoadedModules = info;
-       RELEASE( &glModuleSpinlock );
-       #endif
-       
        return 1;
 }
 
@@ -301,7 +398,7 @@ int Module_LoadFile(char *Path, char *ArgString)
  */
 int Module_int_ResolveDeps(tModule *Info)
 {
-       char    **names = Info->Dependencies;
+       const char      **names = Info->Dependencies;
        
        // Walk dependencies array
        for( ; *names; names++ )
@@ -316,11 +413,11 @@ int Module_int_ResolveDeps(tModule *Info)
 }
 
 /**
- * \fn int Module_IsLoaded(char *Name)
+ * \fn int Module_IsLoaded(const char *Name)
  * \brief Checks if a module is loaded
  * \param Name Name of module to find
  */
-int Module_IsLoaded(char *Name)
+int Module_IsLoaded(const char *Name)
 {
        tModule *mod = gLoadedModules;
        
@@ -334,3 +431,19 @@ int Module_IsLoaded(char *Name)
        // not found - return false
        return 0;
 }
+
+/**
+ * \brief Load a module if needed
+ */
+int Module_EnsureLoaded(const char *Name)
+{
+       if( Module_IsLoaded(Name) )
+               return 0;
+
+       if( Modules_InitialiseBuiltin(Name) == 0 )
+               return 0;
+
+       // TODO: Load from a file?
+
+       return -1;
+}

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