Usermode/libc - Fix strchr and strrchr behavior
[tpg/acess2.git] / KernelLand / Kernel / arch / x86 / acpica.c
index f310fc5..5603add 100644 (file)
@@ -5,7 +5,8 @@
  * acpica.c
  * - ACPICA Interface
  */
-#define DEBUG  1
+#define ACPI_DEBUG_OUTPUT      0
+#define DEBUG  0
 #define _AcpiModuleName "Shim"
 #define _COMPONENT     "Acess"
 #include <acpi.h>
@@ -48,7 +49,6 @@ int ACPICA_Initialise(void)
                return -1;
        }
 
-       // AcpiInitializeTables?
        rv = AcpiLoadTables();
        if( ACPI_FAILURE(rv) )
        {
@@ -93,7 +93,7 @@ ACPI_PHYSICAL_ADDRESS AcpiOsGetRootPointer(void)
        if( ACPI_FAILURE(rv) )
                return 0;
 
-       LOG("val=%x", val);
+       LOG("val=0x%x", val);
        
        return val;
        // (Or use EFI)
@@ -118,6 +118,7 @@ ACPI_STATUS AcpiOsPhysicalTableOverride(ACPI_TABLE_HEADER *ExisitingTable, ACPI_
 }
 
 // -- Memory Management ---
+#if USE_ACESS_ACPI_CACHE
 struct sACPICache
 {
        Uint16  nObj;
@@ -133,8 +134,17 @@ ACPI_STATUS AcpiOsCreateCache(char *CacheName, UINT16 ObjectSize, UINT16 MaxDept
         int    namelen = (CacheName ? strlen(CacheName) : 0) + 1;
        LOG("CacheName=%s, ObjSize=%x, MaxDepth=%x", CacheName, ObjectSize, MaxDepth);
 
+       if( ReturnCache == NULL || ObjectSize < 16) {
+               return AE_BAD_PARAMETER;
+       }
+
+       namelen = (namelen + 3) & ~3;
+
        ret = malloc(sizeof(*ret) + MaxDepth*sizeof(char) + namelen + MaxDepth*ObjectSize);
-       if( !ret )      return AE_NO_MEMORY;
+       if( !ret ) {
+               Log_Notice("ACPICA", "%s: malloc() fail", __func__);
+               return AE_NO_MEMORY;
+       }
 
        ret->nObj = MaxDepth;
        ret->ObjectSize = ObjectSize;
@@ -145,8 +155,8 @@ ACPI_STATUS AcpiOsCreateCache(char *CacheName, UINT16 ObjectSize, UINT16 MaxDept
        else
                ret->Name[0] = 0;
        memset(ret->ObjectStates, 0, sizeof(char)*MaxDepth);
-
-       LOG("Allocated cache '%s' (%i x 0x%x)", CacheName, MaxDepth, ObjectSize);
+       
+       LOG("Allocated cache %p '%s' (%i x 0x%x)", ret, CacheName, MaxDepth, ObjectSize);
        
        *ReturnCache = ret;
        
@@ -164,25 +174,41 @@ ACPI_STATUS AcpiOsDeleteCache(ACPI_CACHE_T *Cache)
 
 ACPI_STATUS AcpiOsPurgeCache(ACPI_CACHE_T *Cache)
 {
-       if( Cache == NULL )
+       ENTER("pCache", Cache);
+       if( Cache == NULL ) {
+               LEAVE('i', AE_BAD_PARAMETER);
                return AE_BAD_PARAMETER;
+       }
 
        memset(Cache->ObjectStates, 0, sizeof(char)*Cache->nObj);
 
+       LEAVE('i', AE_OK);
        return AE_OK;
 }
 
 void *AcpiOsAcquireObject(ACPI_CACHE_T *Cache)
 {
-       LOG("(Cache=%p)", Cache);
+       ENTER("pCache", Cache);
+       LOG("Called by %p", __builtin_return_address(0));
        for(int i = 0; i < Cache->nObj; i ++ )
        {
                if( !Cache->ObjectStates[i] ) {
                        Cache->ObjectStates[i] = 1;
-                       return (char*)Cache->First + i*Cache->ObjectSize;
+                       void *rv = (char*)Cache->First + i*Cache->ObjectSize;
+                       if(!rv) {
+                               LEAVE('n');
+                               return NULL;
+                       }
+                       memset(rv, 0, Cache->ObjectSize);
+                       LEAVE('p', rv);
+                       return rv;
                }
        }
-       // TODO
+
+       Log_Debug("ACPICA", "AcpiOsAcquireObject: All %i objects used in '%s'",
+               Cache->nObj, Cache->Name);
+
+       LEAVE('n');
        return NULL;
 }
 
@@ -190,41 +216,56 @@ ACPI_STATUS AcpiOsReleaseObject(ACPI_CACHE_T *Cache, void *Object)
 {
        if( Cache == NULL || Object == NULL )
                return AE_BAD_PARAMETER;
+       ENTER("pCache pObject", Cache, Object);
 
        tVAddr delta = (tVAddr)Object - (tVAddr)Cache->First;
        delta /= Cache->ObjectSize;
+       LOG("Cache=%p, delta = %i, (limit %i)", Cache, delta, Cache->nObj);
        
-       if( delta > Cache->nObj )
+       if( delta >= Cache->nObj ) {
+               LEAVE('i', AE_BAD_PARAMETER);
                return AE_BAD_PARAMETER;
+       }
        
        Cache->ObjectStates[delta] = 0;
 
+       LEAVE('i', AE_OK);
        return AE_OK;
 }
+#endif
 
 void *AcpiOsMapMemory(ACPI_PHYSICAL_ADDRESS PhysicalAddress, ACPI_SIZE Length)
 {
-       if( PhysicalAddress < ONEMEG )
+       if( PhysicalAddress < ONEMEG ) {
+               ASSERTCR(Length, <=, ONEMEG-PhysicalAddress, NULL);
                return (void*)(KERNEL_BASE | PhysicalAddress);
+       }
        
        Uint    ofs = PhysicalAddress & (PAGE_SIZE-1);
        int npages = (ofs + Length + (PAGE_SIZE-1)) / PAGE_SIZE;
-       void *rv = ((char*)MM_MapHWPages(PhysicalAddress, npages)) + ofs;
+       char *maploc = MM_MapHWPages(PhysicalAddress, npages);
+       if(!maploc) {
+               LOG("Mapping %P+0x%x failed", PhysicalAddress, Length);
+               return NULL;
+       }
+//     MM_DumpTables(0, -1);
+       void *rv = maploc + ofs;
        LOG("Map (%P+%i pg) to %p", PhysicalAddress, npages, rv);
        return rv;
 }
 
 void AcpiOsUnmapMemory(void *LogicalAddress, ACPI_SIZE Length)
 {
-       if( (tVAddr)LogicalAddress - KERNEL_BASE < ONEMEG )
+       if( (tVAddr)LogicalAddress - KERNEL_BASE < ONEMEG ) {
                return ;
+       }
 
        LOG("%p", LogicalAddress);
 
        Uint    ofs = (tVAddr)LogicalAddress & (PAGE_SIZE-1);
        int npages = (ofs + Length + (PAGE_SIZE-1)) / PAGE_SIZE;
        // TODO: Validate `Length` is the same as was passed to AcpiOsMapMemory
-       MM_UnmapHWPages( (tVAddr)LogicalAddress, npages);
+       MM_UnmapHWPages( LogicalAddress, npages);
 }
 
 ACPI_STATUS AcpiOsGetPhysicalAddress(void *LogicalAddress, ACPI_PHYSICAL_ADDRESS *PhysicalAddress)
@@ -301,8 +342,10 @@ ACPI_STATUS AcpiOsCreateMutex(ACPI_MUTEX *OutHandle)
        if( !OutHandle )
                return AE_BAD_PARAMETER;
        tMutex  *ret = calloc( sizeof(tMutex), 1 );
-       if( !ret )
+       if( !ret ) {
+               Log_Notice("ACPICA", "%s: malloc() fail", __func__);
                return AE_NO_MEMORY;
+       }
        ret->Name = "AcpiOsCreateMutex";
        *OutHandle = ret;
        
@@ -311,6 +354,7 @@ ACPI_STATUS AcpiOsCreateMutex(ACPI_MUTEX *OutHandle)
 
 void AcpiOsDeleteMutex(ACPI_MUTEX Handle)
 {
+       //  TODO: Need `Mutex_Destroy`
        Mutex_Acquire(Handle);
        free(Handle);
 }
@@ -336,8 +380,10 @@ ACPI_STATUS AcpiOsCreateSemaphore(UINT32 MaxUnits, UINT32 InitialUnits, ACPI_SEM
        if( !OutHandle )
                return AE_BAD_PARAMETER;
        tSemaphore      *ret = calloc( sizeof(tSemaphore), 1 );
-       if( !ret )
+       if( !ret ) {
+               Log_Notice("ACPICA", "%s: malloc() fail", __func__);
                return AE_NO_MEMORY;
+       }
        
        Semaphore_Init(ret, InitialUnits, MaxUnits, "AcpiOsCreateSemaphore", "");
        *OutHandle = ret;
@@ -490,6 +536,8 @@ ACPI_STATUS AcpiOsReadMemory(ACPI_PHYSICAL_ADDRESS Address, UINT64 *Value, UINT3
                MM_FreeTemp(ptr);
        }
 
+       LOG("*%P = [%i]%X", Address, Width, *Value);
+
        return AE_OK;
 }
 
@@ -513,7 +561,7 @@ ACPI_STATUS AcpiOsWriteMemory(ACPI_PHYSICAL_ADDRESS Address, UINT64 Value, UINT3
                return AE_BAD_PARAMETER;
        }
 
-       if( Address >= 1024*1024 ) {
+       if( Address >= ONEMEG ) {
                MM_FreeTemp(ptr);
        }
        

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