Home | History | Annotate | Line # | Download | only in debugger
dbexec.c revision 1.1.1.15
      1 /*******************************************************************************
      2  *
      3  * Module Name: dbexec - debugger control method execution
      4  *
      5  ******************************************************************************/
      6 
      7 /*
      8  * Copyright (C) 2000 - 2020, Intel Corp.
      9  * All rights reserved.
     10  *
     11  * Redistribution and use in source and binary forms, with or without
     12  * modification, are permitted provided that the following conditions
     13  * are met:
     14  * 1. Redistributions of source code must retain the above copyright
     15  *    notice, this list of conditions, and the following disclaimer,
     16  *    without modification.
     17  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
     18  *    substantially similar to the "NO WARRANTY" disclaimer below
     19  *    ("Disclaimer") and any redistribution must be conditioned upon
     20  *    including a substantially similar Disclaimer requirement for further
     21  *    binary redistribution.
     22  * 3. Neither the names of the above-listed copyright holders nor the names
     23  *    of any contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * Alternatively, this software may be distributed under the terms of the
     27  * GNU General Public License ("GPL") version 2 as published by the Free
     28  * Software Foundation.
     29  *
     30  * NO WARRANTY
     31  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     32  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     33  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
     34  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     35  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     36  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     37  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     38  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
     39  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
     40  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     41  * POSSIBILITY OF SUCH DAMAGES.
     42  */
     43 
     44 #include "acpi.h"
     45 #include "accommon.h"
     46 #include "acdebug.h"
     47 #include "acnamesp.h"
     48 
     49 
     50 #define _COMPONENT          ACPI_CA_DEBUGGER
     51         ACPI_MODULE_NAME    ("dbexec")
     52 
     53 
     54 static ACPI_DB_METHOD_INFO          AcpiGbl_DbMethodInfo;
     55 
     56 /* Local prototypes */
     57 
     58 static ACPI_STATUS
     59 AcpiDbExecuteMethod (
     60     ACPI_DB_METHOD_INFO     *Info,
     61     ACPI_BUFFER             *ReturnObj);
     62 
     63 static ACPI_STATUS
     64 AcpiDbExecuteSetup (
     65     ACPI_DB_METHOD_INFO     *Info);
     66 
     67 static UINT32
     68 AcpiDbGetOutstandingAllocations (
     69     void);
     70 
     71 static void ACPI_SYSTEM_XFACE
     72 AcpiDbMethodThread (
     73     void                    *Context);
     74 
     75 static ACPI_STATUS
     76 AcpiDbExecutionWalk (
     77     ACPI_HANDLE             ObjHandle,
     78     UINT32                  NestingLevel,
     79     void                    *Context,
     80     void                    **ReturnValue);
     81 
     82 static void ACPI_SYSTEM_XFACE
     83 AcpiDbSingleExecutionThread (
     84     void                    *Context);
     85 
     86 
     87 /*******************************************************************************
     88  *
     89  * FUNCTION:    AcpiDbDeleteObjects
     90  *
     91  * PARAMETERS:  Count               - Count of objects in the list
     92  *              Objects             - Array of ACPI_OBJECTs to be deleted
     93  *
     94  * RETURN:      None
     95  *
     96  * DESCRIPTION: Delete a list of ACPI_OBJECTS. Handles packages and nested
     97  *              packages via recursion.
     98  *
     99  ******************************************************************************/
    100 
    101 void
    102 AcpiDbDeleteObjects (
    103     UINT32                  Count,
    104     ACPI_OBJECT             *Objects)
    105 {
    106     UINT32                  i;
    107 
    108 
    109     for (i = 0; i < Count; i++)
    110     {
    111         switch (Objects[i].Type)
    112         {
    113         case ACPI_TYPE_BUFFER:
    114 
    115             ACPI_FREE (Objects[i].Buffer.Pointer);
    116             break;
    117 
    118         case ACPI_TYPE_PACKAGE:
    119 
    120             /* Recursive call to delete package elements */
    121 
    122             AcpiDbDeleteObjects (Objects[i].Package.Count,
    123                 Objects[i].Package.Elements);
    124 
    125             /* Free the elements array */
    126 
    127             ACPI_FREE (Objects[i].Package.Elements);
    128             break;
    129 
    130         default:
    131 
    132             break;
    133         }
    134     }
    135 }
    136 
    137 
    138 /*******************************************************************************
    139  *
    140  * FUNCTION:    AcpiDbExecuteMethod
    141  *
    142  * PARAMETERS:  Info            - Valid info segment
    143  *              ReturnObj       - Where to put return object
    144  *
    145  * RETURN:      Status
    146  *
    147  * DESCRIPTION: Execute a control method.
    148  *
    149  ******************************************************************************/
    150 
    151 static ACPI_STATUS
    152 AcpiDbExecuteMethod (
    153     ACPI_DB_METHOD_INFO     *Info,
    154     ACPI_BUFFER             *ReturnObj)
    155 {
    156     ACPI_STATUS             Status;
    157     ACPI_OBJECT_LIST        ParamObjects;
    158     ACPI_OBJECT             Params[ACPI_DEBUGGER_MAX_ARGS + 1];
    159     UINT32                  i;
    160 
    161 
    162     ACPI_FUNCTION_TRACE (DbExecuteMethod);
    163 
    164 
    165     if (AcpiGbl_DbOutputToFile && !AcpiDbgLevel)
    166     {
    167         AcpiOsPrintf ("Warning: debug output is not enabled!\n");
    168     }
    169 
    170     ParamObjects.Count = 0;
    171     ParamObjects.Pointer = NULL;
    172 
    173     /* Pass through any command-line arguments */
    174 
    175     if (Info->Args && Info->Args[0])
    176     {
    177         /* Get arguments passed on the command line */
    178 
    179         for (i = 0; (Info->Args[i] && *(Info->Args[i])); i++)
    180         {
    181             /* Convert input string (token) to an actual ACPI_OBJECT */
    182 
    183             Status = AcpiDbConvertToObject (Info->Types[i],
    184                 Info->Args[i], &Params[i]);
    185             if (ACPI_FAILURE (Status))
    186             {
    187                 ACPI_EXCEPTION ((AE_INFO, Status,
    188                     "While parsing method arguments"));
    189                 goto Cleanup;
    190             }
    191         }
    192 
    193         ParamObjects.Count = i;
    194         ParamObjects.Pointer = Params;
    195     }
    196 
    197     /* Prepare for a return object of arbitrary size */
    198 
    199     ReturnObj->Pointer = AcpiGbl_DbBuffer;
    200     ReturnObj->Length  = ACPI_DEBUG_BUFFER_SIZE;
    201 
    202     /* Do the actual method execution */
    203 
    204     AcpiGbl_MethodExecuting = TRUE;
    205     Status = AcpiEvaluateObject (NULL, Info->Pathname,
    206         &ParamObjects, ReturnObj);
    207 
    208     AcpiGbl_CmSingleStep = FALSE;
    209     AcpiGbl_MethodExecuting = FALSE;
    210 
    211     if (ACPI_FAILURE (Status))
    212     {
    213         if ((Status == AE_ABORT_METHOD) || AcpiGbl_AbortMethod)
    214         {
    215             /* Clear the abort and fall back to the debugger prompt */
    216 
    217             ACPI_EXCEPTION ((AE_INFO, Status,
    218                 "Aborting top-level method"));
    219 
    220             AcpiGbl_AbortMethod = FALSE;
    221             Status = AE_OK;
    222             goto Cleanup;
    223         }
    224 
    225         ACPI_EXCEPTION ((AE_INFO, Status,
    226             "while executing %s from AML Debugger", Info->Pathname));
    227 
    228         if (Status == AE_BUFFER_OVERFLOW)
    229         {
    230             ACPI_ERROR ((AE_INFO,
    231                 "Possible buffer overflow within AML Debugger "
    232                 "buffer (size 0x%X needed 0x%X)",
    233                 ACPI_DEBUG_BUFFER_SIZE, (UINT32) ReturnObj->Length));
    234         }
    235     }
    236 
    237 Cleanup:
    238     AcpiDbDeleteObjects (ParamObjects.Count, Params);
    239     return_ACPI_STATUS (Status);
    240 }
    241 
    242 
    243 /*******************************************************************************
    244  *
    245  * FUNCTION:    AcpiDbExecuteSetup
    246  *
    247  * PARAMETERS:  Info            - Valid method info
    248  *
    249  * RETURN:      None
    250  *
    251  * DESCRIPTION: Setup info segment prior to method execution
    252  *
    253  ******************************************************************************/
    254 
    255 static ACPI_STATUS
    256 AcpiDbExecuteSetup (
    257     ACPI_DB_METHOD_INFO     *Info)
    258 {
    259     ACPI_STATUS             Status;
    260 
    261 
    262     ACPI_FUNCTION_NAME (DbExecuteSetup);
    263 
    264 
    265     /* Concatenate the current scope to the supplied name */
    266 
    267     Info->Pathname[0] = 0;
    268     if ((Info->Name[0] != '\\') &&
    269         (Info->Name[0] != '/'))
    270     {
    271         if (AcpiUtSafeStrcat (Info->Pathname, sizeof (Info->Pathname),
    272             AcpiGbl_DbScopeBuf))
    273         {
    274             Status = AE_BUFFER_OVERFLOW;
    275             goto ErrorExit;
    276         }
    277     }
    278 
    279     if (AcpiUtSafeStrcat (Info->Pathname, sizeof (Info->Pathname),
    280         Info->Name))
    281     {
    282         Status = AE_BUFFER_OVERFLOW;
    283         goto ErrorExit;
    284     }
    285 
    286     AcpiDbPrepNamestring (Info->Pathname);
    287 
    288     AcpiDbSetOutputDestination (ACPI_DB_DUPLICATE_OUTPUT);
    289     AcpiOsPrintf ("Evaluating %s\n", Info->Pathname);
    290 
    291     if (Info->Flags & EX_SINGLE_STEP)
    292     {
    293         AcpiGbl_CmSingleStep = TRUE;
    294         AcpiDbSetOutputDestination (ACPI_DB_CONSOLE_OUTPUT);
    295     }
    296 
    297     else
    298     {
    299         /* No single step, allow redirection to a file */
    300 
    301         AcpiDbSetOutputDestination (ACPI_DB_REDIRECTABLE_OUTPUT);
    302     }
    303 
    304     return (AE_OK);
    305 
    306 ErrorExit:
    307 
    308     ACPI_EXCEPTION ((AE_INFO, Status, "During setup for method execution"));
    309     return (Status);
    310 }
    311 
    312 
    313 #ifdef ACPI_DBG_TRACK_ALLOCATIONS
    314 UINT32
    315 AcpiDbGetCacheInfo (
    316     ACPI_MEMORY_LIST        *Cache)
    317 {
    318 
    319     return (Cache->TotalAllocated - Cache->TotalFreed - Cache->CurrentDepth);
    320 }
    321 #endif
    322 
    323 /*******************************************************************************
    324  *
    325  * FUNCTION:    AcpiDbGetOutstandingAllocations
    326  *
    327  * PARAMETERS:  None
    328  *
    329  * RETURN:      Current global allocation count minus cache entries
    330  *
    331  * DESCRIPTION: Determine the current number of "outstanding" allocations --
    332  *              those allocations that have not been freed and also are not
    333  *              in one of the various object caches.
    334  *
    335  ******************************************************************************/
    336 
    337 static UINT32
    338 AcpiDbGetOutstandingAllocations (
    339     void)
    340 {
    341     UINT32                  Outstanding = 0;
    342 
    343 #ifdef ACPI_DBG_TRACK_ALLOCATIONS
    344 
    345     Outstanding += AcpiDbGetCacheInfo (AcpiGbl_StateCache);
    346     Outstanding += AcpiDbGetCacheInfo (AcpiGbl_PsNodeCache);
    347     Outstanding += AcpiDbGetCacheInfo (AcpiGbl_PsNodeExtCache);
    348     Outstanding += AcpiDbGetCacheInfo (AcpiGbl_OperandCache);
    349 #endif
    350 
    351     return (Outstanding);
    352 }
    353 
    354 
    355 /*******************************************************************************
    356  *
    357  * FUNCTION:    AcpiDbExecutionWalk
    358  *
    359  * PARAMETERS:  WALK_CALLBACK
    360  *
    361  * RETURN:      Status
    362  *
    363  * DESCRIPTION: Execute a control method. Name is relative to the current
    364  *              scope.
    365  *
    366  ******************************************************************************/
    367 
    368 static ACPI_STATUS
    369 AcpiDbExecutionWalk (
    370     ACPI_HANDLE             ObjHandle,
    371     UINT32                  NestingLevel,
    372     void                    *Context,
    373     void                    **ReturnValue)
    374 {
    375     ACPI_OPERAND_OBJECT     *ObjDesc;
    376     ACPI_NAMESPACE_NODE     *Node = (ACPI_NAMESPACE_NODE *) ObjHandle;
    377     ACPI_BUFFER             ReturnObj;
    378     ACPI_STATUS             Status;
    379 
    380 
    381     ObjDesc = AcpiNsGetAttachedObject (Node);
    382     if (ObjDesc->Method.ParamCount)
    383     {
    384         return (AE_OK);
    385     }
    386 
    387     ReturnObj.Pointer = NULL;
    388     ReturnObj.Length = ACPI_ALLOCATE_BUFFER;
    389 
    390     AcpiNsPrintNodePathname (Node, "Evaluating");
    391 
    392     /* Do the actual method execution */
    393 
    394     AcpiOsPrintf ("\n");
    395     AcpiGbl_MethodExecuting = TRUE;
    396 
    397     Status = AcpiEvaluateObject (Node, NULL, NULL, &ReturnObj);
    398 
    399     AcpiOsPrintf ("Evaluation of [%4.4s] returned %s\n",
    400         AcpiUtGetNodeName (Node),
    401         AcpiFormatException (Status));
    402 
    403     AcpiGbl_MethodExecuting = FALSE;
    404     return (AE_OK);
    405 }
    406 
    407 
    408 /*******************************************************************************
    409  *
    410  * FUNCTION:    AcpiDbExecute
    411  *
    412  * PARAMETERS:  Name                - Name of method to execute
    413  *              Args                - Parameters to the method
    414  *              Types               -
    415  *              Flags               - single step/no single step
    416  *
    417  * RETURN:      None
    418  *
    419  * DESCRIPTION: Execute a control method. Name is relative to the current
    420  *              scope.
    421  *
    422  ******************************************************************************/
    423 
    424 void
    425 AcpiDbExecute (
    426     char                    *Name,
    427     char                    **Args,
    428     ACPI_OBJECT_TYPE        *Types,
    429     UINT32                  Flags)
    430 {
    431     ACPI_STATUS             Status;
    432     ACPI_BUFFER             ReturnObj;
    433     char                    *NameString;
    434 
    435 #ifdef ACPI_DEBUG_OUTPUT
    436     UINT32                  PreviousAllocations;
    437     UINT32                  Allocations;
    438 #endif
    439 
    440 
    441     /*
    442      * Allow one execution to be performed by debugger or single step
    443      * execution will be dead locked by the interpreter mutexes.
    444      */
    445     if (AcpiGbl_MethodExecuting)
    446     {
    447         AcpiOsPrintf ("Only one debugger execution is allowed.\n");
    448         return;
    449     }
    450 
    451 #ifdef ACPI_DEBUG_OUTPUT
    452     /* Memory allocation tracking */
    453 
    454     PreviousAllocations = AcpiDbGetOutstandingAllocations ();
    455 #endif
    456 
    457     if (*Name == '*')
    458     {
    459         (void) AcpiWalkNamespace (ACPI_TYPE_METHOD, ACPI_ROOT_OBJECT,
    460             ACPI_UINT32_MAX, AcpiDbExecutionWalk, NULL, NULL, NULL);
    461         return;
    462     }
    463 
    464     NameString = ACPI_ALLOCATE (strlen (Name) + 1);
    465     if (!NameString)
    466     {
    467         return;
    468     }
    469 
    470     memset (&AcpiGbl_DbMethodInfo, 0, sizeof (ACPI_DB_METHOD_INFO));
    471     strcpy (NameString, Name);
    472     AcpiUtStrupr (NameString);
    473 
    474     /* Subcommand to Execute all predefined names in the namespace */
    475 
    476     if (!strncmp (NameString, "PREDEF", 6))
    477     {
    478         AcpiDbEvaluatePredefinedNames ();
    479         ACPI_FREE (NameString);
    480         return;
    481     }
    482 
    483     AcpiGbl_DbMethodInfo.Name = NameString;
    484     AcpiGbl_DbMethodInfo.Args = Args;
    485     AcpiGbl_DbMethodInfo.Types = Types;
    486     AcpiGbl_DbMethodInfo.Flags = Flags;
    487 
    488     ReturnObj.Pointer = NULL;
    489     ReturnObj.Length = ACPI_ALLOCATE_BUFFER;
    490 
    491     Status = AcpiDbExecuteSetup (&AcpiGbl_DbMethodInfo);
    492     if (ACPI_FAILURE (Status))
    493     {
    494         ACPI_FREE (NameString);
    495         return;
    496     }
    497 
    498     /* Get the NS node, determines existence also */
    499 
    500     Status = AcpiGetHandle (NULL, AcpiGbl_DbMethodInfo.Pathname,
    501         &AcpiGbl_DbMethodInfo.Method);
    502     if (ACPI_SUCCESS (Status))
    503     {
    504         Status = AcpiDbExecuteMethod (&AcpiGbl_DbMethodInfo,
    505             &ReturnObj);
    506     }
    507     ACPI_FREE (NameString);
    508 
    509     /*
    510      * Allow any handlers in separate threads to complete.
    511      * (Such as Notify handlers invoked from AML executed above).
    512      */
    513     AcpiOsSleep ((UINT64) 10);
    514 
    515 #ifdef ACPI_DEBUG_OUTPUT
    516 
    517     /* Memory allocation tracking */
    518 
    519     Allocations = AcpiDbGetOutstandingAllocations () - PreviousAllocations;
    520 
    521     AcpiDbSetOutputDestination (ACPI_DB_DUPLICATE_OUTPUT);
    522 
    523     if (Allocations > 0)
    524     {
    525         AcpiOsPrintf (
    526             "0x%X Outstanding allocations after evaluation of %s\n",
    527             Allocations, AcpiGbl_DbMethodInfo.Pathname);
    528     }
    529 #endif
    530 
    531     if (ACPI_FAILURE (Status))
    532     {
    533         AcpiOsPrintf ("Evaluation of %s failed with status %s\n",
    534             AcpiGbl_DbMethodInfo.Pathname,
    535             AcpiFormatException (Status));
    536     }
    537     else
    538     {
    539         /* Display a return object, if any */
    540 
    541         if (ReturnObj.Length)
    542         {
    543             AcpiOsPrintf (
    544                 "Evaluation of %s returned object %p, "
    545                 "external buffer length %X\n",
    546                 AcpiGbl_DbMethodInfo.Pathname, ReturnObj.Pointer,
    547                 (UINT32) ReturnObj.Length);
    548 
    549             AcpiDbDumpExternalObject (ReturnObj.Pointer, 1);
    550 
    551             /* Dump a _PLD buffer if present */
    552 
    553             if (ACPI_COMPARE_NAMESEG ((ACPI_CAST_PTR (ACPI_NAMESPACE_NODE,
    554                 AcpiGbl_DbMethodInfo.Method)->Name.Ascii),
    555                 METHOD_NAME__PLD))
    556             {
    557                 AcpiDbDumpPldBuffer (ReturnObj.Pointer);
    558             }
    559         }
    560         else
    561         {
    562             AcpiOsPrintf ("No object was returned from evaluation of %s\n",
    563                 AcpiGbl_DbMethodInfo.Pathname);
    564         }
    565     }
    566 
    567     AcpiDbSetOutputDestination (ACPI_DB_CONSOLE_OUTPUT);
    568 }
    569 
    570 
    571 /*******************************************************************************
    572  *
    573  * FUNCTION:    AcpiDbMethodThread
    574  *
    575  * PARAMETERS:  Context             - Execution info segment
    576  *
    577  * RETURN:      None
    578  *
    579  * DESCRIPTION: Debugger execute thread. Waits for a command line, then
    580  *              simply dispatches it.
    581  *
    582  ******************************************************************************/
    583 
    584 static void ACPI_SYSTEM_XFACE
    585 AcpiDbMethodThread (
    586     void                    *Context)
    587 {
    588     ACPI_STATUS             Status;
    589     ACPI_DB_METHOD_INFO     *Info = Context;
    590     ACPI_DB_METHOD_INFO     LocalInfo;
    591     UINT32                  i;
    592     UINT8                   Allow;
    593     ACPI_BUFFER             ReturnObj;
    594 
    595 
    596     /*
    597      * AcpiGbl_DbMethodInfo.Arguments will be passed as method arguments.
    598      * Prevent AcpiGbl_DbMethodInfo from being modified by multiple threads
    599      * concurrently.
    600      *
    601      * Note: The arguments we are passing are used by the ASL test suite
    602      * (aslts). Do not change them without updating the tests.
    603      */
    604     (void) AcpiOsWaitSemaphore (Info->InfoGate, 1, ACPI_WAIT_FOREVER);
    605 
    606     if (Info->InitArgs)
    607     {
    608         AcpiDbUint32ToHexString (Info->NumCreated,
    609             Info->IndexOfThreadStr);
    610         AcpiDbUint32ToHexString ((UINT32) AcpiOsGetThreadId (),
    611             Info->IdOfThreadStr);
    612     }
    613 
    614     if (Info->Threads && (Info->NumCreated < Info->NumThreads))
    615     {
    616         Info->Threads[Info->NumCreated++] = AcpiOsGetThreadId();
    617     }
    618 
    619     LocalInfo = *Info;
    620     LocalInfo.Args = LocalInfo.Arguments;
    621     LocalInfo.Arguments[0] = LocalInfo.NumThreadsStr;
    622     LocalInfo.Arguments[1] = LocalInfo.IdOfThreadStr;
    623     LocalInfo.Arguments[2] = LocalInfo.IndexOfThreadStr;
    624     LocalInfo.Arguments[3] = NULL;
    625 
    626     LocalInfo.Types = LocalInfo.ArgTypes;
    627 
    628     (void) AcpiOsSignalSemaphore (Info->InfoGate, 1);
    629 
    630     for (i = 0; i < Info->NumLoops; i++)
    631     {
    632         Status = AcpiDbExecuteMethod (&LocalInfo, &ReturnObj);
    633         if (ACPI_FAILURE (Status))
    634         {
    635             AcpiOsPrintf ("%s During evaluation of %s at iteration %X\n",
    636                 AcpiFormatException (Status), Info->Pathname, i);
    637             if (Status == AE_ABORT_METHOD)
    638             {
    639                 break;
    640             }
    641         }
    642 
    643 #if 0
    644         if ((i % 100) == 0)
    645         {
    646             AcpiOsPrintf ("%u loops, Thread 0x%x\n",
    647                 i, AcpiOsGetThreadId ());
    648         }
    649 
    650         if (ReturnObj.Length)
    651         {
    652             AcpiOsPrintf ("Evaluation of %s returned object %p Buflen %X\n",
    653                 Info->Pathname, ReturnObj.Pointer,
    654                 (UINT32) ReturnObj.Length);
    655             AcpiDbDumpExternalObject (ReturnObj.Pointer, 1);
    656         }
    657 #endif
    658     }
    659 
    660     /* Signal our completion */
    661 
    662     Allow = 0;
    663     (void) AcpiOsWaitSemaphore (Info->ThreadCompleteGate,
    664         1, ACPI_WAIT_FOREVER);
    665     Info->NumCompleted++;
    666 
    667     if (Info->NumCompleted == Info->NumThreads)
    668     {
    669         /* Do signal for main thread once only */
    670         Allow = 1;
    671     }
    672 
    673     (void) AcpiOsSignalSemaphore (Info->ThreadCompleteGate, 1);
    674 
    675     if (Allow)
    676     {
    677         Status = AcpiOsSignalSemaphore (Info->MainThreadGate, 1);
    678         if (ACPI_FAILURE (Status))
    679         {
    680             AcpiOsPrintf (
    681                 "Could not signal debugger thread sync semaphore, %s\n",
    682                 AcpiFormatException (Status));
    683         }
    684     }
    685 }
    686 
    687 
    688 /*******************************************************************************
    689  *
    690  * FUNCTION:    AcpiDbSingleExecutionThread
    691  *
    692  * PARAMETERS:  Context                 - Method info struct
    693  *
    694  * RETURN:      None
    695  *
    696  * DESCRIPTION: Create one thread and execute a method
    697  *
    698  ******************************************************************************/
    699 
    700 static void ACPI_SYSTEM_XFACE
    701 AcpiDbSingleExecutionThread (
    702     void                    *Context)
    703 {
    704     ACPI_DB_METHOD_INFO     *Info = Context;
    705     ACPI_STATUS             Status;
    706     ACPI_BUFFER             ReturnObj;
    707 
    708 
    709     AcpiOsPrintf ("\n");
    710 
    711     Status = AcpiDbExecuteMethod (Info, &ReturnObj);
    712     if (ACPI_FAILURE (Status))
    713     {
    714         AcpiOsPrintf ("%s During evaluation of %s\n",
    715             AcpiFormatException (Status), Info->Pathname);
    716         return;
    717     }
    718 
    719     /* Display a return object, if any */
    720 
    721     if (ReturnObj.Length)
    722     {
    723         AcpiOsPrintf ("Evaluation of %s returned object %p, "
    724             "external buffer length %X\n",
    725             AcpiGbl_DbMethodInfo.Pathname, ReturnObj.Pointer,
    726             (UINT32) ReturnObj.Length);
    727 
    728         AcpiDbDumpExternalObject (ReturnObj.Pointer, 1);
    729     }
    730 
    731     AcpiOsPrintf ("\nBackground thread completed\n%c ",
    732         ACPI_DEBUGGER_COMMAND_PROMPT);
    733 }
    734 
    735 
    736 /*******************************************************************************
    737  *
    738  * FUNCTION:    AcpiDbCreateExecutionThread
    739  *
    740  * PARAMETERS:  MethodNameArg           - Control method to execute
    741  *              Arguments               - Array of arguments to the method
    742  *              Types                   - Corresponding array of object types
    743  *
    744  * RETURN:      None
    745  *
    746  * DESCRIPTION: Create a single thread to evaluate a namespace object. Handles
    747  *              arguments passed on command line for control methods.
    748  *
    749  ******************************************************************************/
    750 
    751 void
    752 AcpiDbCreateExecutionThread (
    753     char                    *MethodNameArg,
    754     char                    **Arguments,
    755     ACPI_OBJECT_TYPE        *Types)
    756 {
    757     ACPI_STATUS             Status;
    758     UINT32                  i;
    759 
    760 
    761     memset (&AcpiGbl_DbMethodInfo, 0, sizeof (ACPI_DB_METHOD_INFO));
    762     AcpiGbl_DbMethodInfo.Name = MethodNameArg;
    763     AcpiGbl_DbMethodInfo.InitArgs = 1;
    764     AcpiGbl_DbMethodInfo.Args = AcpiGbl_DbMethodInfo.Arguments;
    765     AcpiGbl_DbMethodInfo.Types = AcpiGbl_DbMethodInfo.ArgTypes;
    766 
    767     /* Setup method arguments, up to 7 (0-6) */
    768 
    769     for (i = 0; (i < ACPI_METHOD_NUM_ARGS) && *Arguments; i++)
    770     {
    771         AcpiGbl_DbMethodInfo.Arguments[i] = *Arguments;
    772         Arguments++;
    773 
    774         AcpiGbl_DbMethodInfo.ArgTypes[i] = *Types;
    775         Types++;
    776     }
    777 
    778     Status = AcpiDbExecuteSetup (&AcpiGbl_DbMethodInfo);
    779     if (ACPI_FAILURE (Status))
    780     {
    781         return;
    782     }
    783 
    784     /* Get the NS node, determines existence also */
    785 
    786     Status = AcpiGetHandle (NULL, AcpiGbl_DbMethodInfo.Pathname,
    787         &AcpiGbl_DbMethodInfo.Method);
    788     if (ACPI_FAILURE (Status))
    789     {
    790         AcpiOsPrintf ("%s Could not get handle for %s\n",
    791             AcpiFormatException (Status), AcpiGbl_DbMethodInfo.Pathname);
    792         return;
    793     }
    794 
    795     Status = AcpiOsExecute (OSL_DEBUGGER_EXEC_THREAD,
    796         AcpiDbSingleExecutionThread, &AcpiGbl_DbMethodInfo);
    797     if (ACPI_FAILURE (Status))
    798     {
    799         return;
    800     }
    801 
    802     AcpiOsPrintf ("\nBackground thread started\n");
    803 }
    804 
    805 
    806 /*******************************************************************************
    807  *
    808  * FUNCTION:    AcpiDbCreateExecutionThreads
    809  *
    810  * PARAMETERS:  NumThreadsArg           - Number of threads to create
    811  *              NumLoopsArg             - Loop count for the thread(s)
    812  *              MethodNameArg           - Control method to execute
    813  *
    814  * RETURN:      None
    815  *
    816  * DESCRIPTION: Create threads to execute method(s)
    817  *
    818  ******************************************************************************/
    819 
    820 void
    821 AcpiDbCreateExecutionThreads (
    822     char                    *NumThreadsArg,
    823     char                    *NumLoopsArg,
    824     char                    *MethodNameArg)
    825 {
    826     ACPI_STATUS             Status;
    827     UINT32                  NumThreads;
    828     UINT32                  NumLoops;
    829     UINT32                  i;
    830     UINT32                  Size;
    831     ACPI_MUTEX              MainThreadGate;
    832     ACPI_MUTEX              ThreadCompleteGate;
    833     ACPI_MUTEX              InfoGate;
    834 
    835 
    836     /* Get the arguments */
    837 
    838     NumThreads = strtoul (NumThreadsArg, NULL, 0);
    839     NumLoops = strtoul (NumLoopsArg, NULL, 0);
    840 
    841     if (!NumThreads || !NumLoops)
    842     {
    843         AcpiOsPrintf ("Bad argument: Threads %X, Loops %X\n",
    844             NumThreads, NumLoops);
    845         return;
    846     }
    847 
    848     /*
    849      * Create the semaphore for synchronization of
    850      * the created threads with the main thread.
    851      */
    852     Status = AcpiOsCreateSemaphore (1, 0, &MainThreadGate);
    853     if (ACPI_FAILURE (Status))
    854     {
    855         AcpiOsPrintf ("Could not create semaphore for "
    856             "synchronization with the main thread, %s\n",
    857             AcpiFormatException (Status));
    858         return;
    859     }
    860 
    861     /*
    862      * Create the semaphore for synchronization
    863      * between the created threads.
    864      */
    865     Status = AcpiOsCreateSemaphore (1, 1, &ThreadCompleteGate);
    866     if (ACPI_FAILURE (Status))
    867     {
    868         AcpiOsPrintf ("Could not create semaphore for "
    869             "synchronization between the created threads, %s\n",
    870             AcpiFormatException (Status));
    871 
    872         (void) AcpiOsDeleteSemaphore (MainThreadGate);
    873         return;
    874     }
    875 
    876     Status = AcpiOsCreateSemaphore (1, 1, &InfoGate);
    877     if (ACPI_FAILURE (Status))
    878     {
    879         AcpiOsPrintf ("Could not create semaphore for "
    880             "synchronization of AcpiGbl_DbMethodInfo, %s\n",
    881             AcpiFormatException (Status));
    882 
    883         (void) AcpiOsDeleteSemaphore (ThreadCompleteGate);
    884         (void) AcpiOsDeleteSemaphore (MainThreadGate);
    885         return;
    886     }
    887 
    888     memset (&AcpiGbl_DbMethodInfo, 0, sizeof (ACPI_DB_METHOD_INFO));
    889 
    890     /* Array to store IDs of threads */
    891 
    892     AcpiGbl_DbMethodInfo.NumThreads = NumThreads;
    893     Size = sizeof (ACPI_THREAD_ID) * AcpiGbl_DbMethodInfo.NumThreads;
    894 
    895     AcpiGbl_DbMethodInfo.Threads = AcpiOsAllocate (Size);
    896     if (AcpiGbl_DbMethodInfo.Threads == NULL)
    897     {
    898         AcpiOsPrintf ("No memory for thread IDs array\n");
    899         (void) AcpiOsDeleteSemaphore (MainThreadGate);
    900         (void) AcpiOsDeleteSemaphore (ThreadCompleteGate);
    901         (void) AcpiOsDeleteSemaphore (InfoGate);
    902         return;
    903     }
    904     memset (AcpiGbl_DbMethodInfo.Threads, 0, Size);
    905 
    906     /* Setup the context to be passed to each thread */
    907 
    908     AcpiGbl_DbMethodInfo.Name = MethodNameArg;
    909     AcpiGbl_DbMethodInfo.Flags = 0;
    910     AcpiGbl_DbMethodInfo.NumLoops = NumLoops;
    911     AcpiGbl_DbMethodInfo.MainThreadGate = MainThreadGate;
    912     AcpiGbl_DbMethodInfo.ThreadCompleteGate = ThreadCompleteGate;
    913     AcpiGbl_DbMethodInfo.InfoGate = InfoGate;
    914 
    915     /* Init arguments to be passed to method */
    916 
    917     AcpiGbl_DbMethodInfo.InitArgs = 1;
    918     AcpiGbl_DbMethodInfo.Args = AcpiGbl_DbMethodInfo.Arguments;
    919     AcpiGbl_DbMethodInfo.Arguments[0] = AcpiGbl_DbMethodInfo.NumThreadsStr;
    920     AcpiGbl_DbMethodInfo.Arguments[1] = AcpiGbl_DbMethodInfo.IdOfThreadStr;
    921     AcpiGbl_DbMethodInfo.Arguments[2] = AcpiGbl_DbMethodInfo.IndexOfThreadStr;
    922     AcpiGbl_DbMethodInfo.Arguments[3] = NULL;
    923 
    924     AcpiGbl_DbMethodInfo.Types = AcpiGbl_DbMethodInfo.ArgTypes;
    925     AcpiGbl_DbMethodInfo.ArgTypes[0] = ACPI_TYPE_INTEGER;
    926     AcpiGbl_DbMethodInfo.ArgTypes[1] = ACPI_TYPE_INTEGER;
    927     AcpiGbl_DbMethodInfo.ArgTypes[2] = ACPI_TYPE_INTEGER;
    928 
    929     AcpiDbUint32ToHexString (NumThreads, AcpiGbl_DbMethodInfo.NumThreadsStr);
    930 
    931     Status = AcpiDbExecuteSetup (&AcpiGbl_DbMethodInfo);
    932     if (ACPI_FAILURE (Status))
    933     {
    934         goto CleanupAndExit;
    935     }
    936 
    937     /* Get the NS node, determines existence also */
    938 
    939     Status = AcpiGetHandle (NULL, AcpiGbl_DbMethodInfo.Pathname,
    940         &AcpiGbl_DbMethodInfo.Method);
    941     if (ACPI_FAILURE (Status))
    942     {
    943         AcpiOsPrintf ("%s Could not get handle for %s\n",
    944             AcpiFormatException (Status), AcpiGbl_DbMethodInfo.Pathname);
    945         goto CleanupAndExit;
    946     }
    947 
    948     /* Create the threads */
    949 
    950     AcpiOsPrintf ("Creating %X threads to execute %X times each\n",
    951         NumThreads, NumLoops);
    952 
    953     for (i = 0; i < (NumThreads); i++)
    954     {
    955         Status = AcpiOsExecute (OSL_DEBUGGER_EXEC_THREAD, AcpiDbMethodThread,
    956             &AcpiGbl_DbMethodInfo);
    957         if (ACPI_FAILURE (Status))
    958         {
    959             break;
    960         }
    961     }
    962 
    963     /* Wait for all threads to complete */
    964 
    965     (void) AcpiOsWaitSemaphore (MainThreadGate, 1, ACPI_WAIT_FOREVER);
    966 
    967     AcpiDbSetOutputDestination (ACPI_DB_DUPLICATE_OUTPUT);
    968     AcpiOsPrintf ("All threads (%X) have completed\n", NumThreads);
    969     AcpiDbSetOutputDestination (ACPI_DB_CONSOLE_OUTPUT);
    970 
    971 CleanupAndExit:
    972 
    973     /* Cleanup and exit */
    974 
    975     (void) AcpiOsDeleteSemaphore (MainThreadGate);
    976     (void) AcpiOsDeleteSemaphore (ThreadCompleteGate);
    977     (void) AcpiOsDeleteSemaphore (InfoGate);
    978 
    979     AcpiOsFree (AcpiGbl_DbMethodInfo.Threads);
    980     AcpiGbl_DbMethodInfo.Threads = NULL;
    981 }
    982