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