xref: /freebsd/sys/contrib/dev/acpica/components/debugger/dbexec.c (revision 193d9e768ba63fcfb187cfd17f461f7d41345048)
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 - 2017, 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 <contrib/dev/acpica/include/acpi.h>
153 #include <contrib/dev/acpica/include/accommon.h>
154 #include <contrib/dev/acpica/include/acdebug.h>
155 #include <contrib/dev/acpica/include/acnamesp.h>
156 
157 
158 #define _COMPONENT          ACPI_CA_DEBUGGER
159         ACPI_MODULE_NAME    ("dbexec")
160 
161 
162 static ACPI_DB_METHOD_INFO          AcpiGbl_DbMethodInfo;
163 
164 /* Local prototypes */
165 
166 static ACPI_STATUS
167 AcpiDbExecuteMethod (
168     ACPI_DB_METHOD_INFO     *Info,
169     ACPI_BUFFER             *ReturnObj);
170 
171 static ACPI_STATUS
172 AcpiDbExecuteSetup (
173     ACPI_DB_METHOD_INFO     *Info);
174 
175 static UINT32
176 AcpiDbGetOutstandingAllocations (
177     void);
178 
179 static void ACPI_SYSTEM_XFACE
180 AcpiDbMethodThread (
181     void                    *Context);
182 
183 static ACPI_STATUS
184 AcpiDbExecutionWalk (
185     ACPI_HANDLE             ObjHandle,
186     UINT32                  NestingLevel,
187     void                    *Context,
188     void                    **ReturnValue);
189 
190 
191 /*******************************************************************************
192  *
193  * FUNCTION:    AcpiDbDeleteObjects
194  *
195  * PARAMETERS:  Count               - Count of objects in the list
196  *              Objects             - Array of ACPI_OBJECTs to be deleted
197  *
198  * RETURN:      None
199  *
200  * DESCRIPTION: Delete a list of ACPI_OBJECTS. Handles packages and nested
201  *              packages via recursion.
202  *
203  ******************************************************************************/
204 
205 void
206 AcpiDbDeleteObjects (
207     UINT32                  Count,
208     ACPI_OBJECT             *Objects)
209 {
210     UINT32                  i;
211 
212 
213     for (i = 0; i < Count; i++)
214     {
215         switch (Objects[i].Type)
216         {
217         case ACPI_TYPE_BUFFER:
218 
219             ACPI_FREE (Objects[i].Buffer.Pointer);
220             break;
221 
222         case ACPI_TYPE_PACKAGE:
223 
224             /* Recursive call to delete package elements */
225 
226             AcpiDbDeleteObjects (Objects[i].Package.Count,
227                 Objects[i].Package.Elements);
228 
229             /* Free the elements array */
230 
231             ACPI_FREE (Objects[i].Package.Elements);
232             break;
233 
234         default:
235 
236             break;
237         }
238     }
239 }
240 
241 
242 /*******************************************************************************
243  *
244  * FUNCTION:    AcpiDbExecuteMethod
245  *
246  * PARAMETERS:  Info            - Valid info segment
247  *              ReturnObj       - Where to put return object
248  *
249  * RETURN:      Status
250  *
251  * DESCRIPTION: Execute a control method.
252  *
253  ******************************************************************************/
254 
255 static ACPI_STATUS
256 AcpiDbExecuteMethod (
257     ACPI_DB_METHOD_INFO     *Info,
258     ACPI_BUFFER             *ReturnObj)
259 {
260     ACPI_STATUS             Status;
261     ACPI_OBJECT_LIST        ParamObjects;
262     ACPI_OBJECT             Params[ACPI_DEBUGGER_MAX_ARGS + 1];
263     UINT32                  i;
264 
265 
266     ACPI_FUNCTION_TRACE (DbExecuteMethod);
267 
268 
269     if (AcpiGbl_DbOutputToFile && !AcpiDbgLevel)
270     {
271         AcpiOsPrintf ("Warning: debug output is not enabled!\n");
272     }
273 
274     ParamObjects.Count = 0;
275     ParamObjects.Pointer = NULL;
276 
277     /* Pass through any command-line arguments */
278 
279     if (Info->Args && Info->Args[0])
280     {
281         /* Get arguments passed on the command line */
282 
283         for (i = 0; (Info->Args[i] && *(Info->Args[i])); i++)
284         {
285             /* Convert input string (token) to an actual ACPI_OBJECT */
286 
287             Status = AcpiDbConvertToObject (Info->Types[i],
288                 Info->Args[i], &Params[i]);
289             if (ACPI_FAILURE (Status))
290             {
291                 ACPI_EXCEPTION ((AE_INFO, Status,
292                     "While parsing method arguments"));
293                 goto Cleanup;
294             }
295         }
296 
297         ParamObjects.Count = i;
298         ParamObjects.Pointer = Params;
299     }
300 
301     /* Prepare for a return object of arbitrary size */
302 
303     ReturnObj->Pointer = AcpiGbl_DbBuffer;
304     ReturnObj->Length  = ACPI_DEBUG_BUFFER_SIZE;
305 
306     /* Do the actual method execution */
307 
308     AcpiGbl_MethodExecuting = TRUE;
309     Status = AcpiEvaluateObject (NULL, Info->Pathname,
310         &ParamObjects, ReturnObj);
311 
312     AcpiGbl_CmSingleStep = FALSE;
313     AcpiGbl_MethodExecuting = FALSE;
314 
315     if (ACPI_FAILURE (Status))
316     {
317         ACPI_EXCEPTION ((AE_INFO, Status,
318             "while executing %s from debugger", Info->Pathname));
319 
320         if (Status == AE_BUFFER_OVERFLOW)
321         {
322             ACPI_ERROR ((AE_INFO,
323                 "Possible overflow of internal debugger "
324                 "buffer (size 0x%X needed 0x%X)",
325                 ACPI_DEBUG_BUFFER_SIZE, (UINT32) ReturnObj->Length));
326         }
327     }
328 
329 Cleanup:
330     AcpiDbDeleteObjects (ParamObjects.Count, Params);
331     return_ACPI_STATUS (Status);
332 }
333 
334 
335 /*******************************************************************************
336  *
337  * FUNCTION:    AcpiDbExecuteSetup
338  *
339  * PARAMETERS:  Info            - Valid method info
340  *
341  * RETURN:      None
342  *
343  * DESCRIPTION: Setup info segment prior to method execution
344  *
345  ******************************************************************************/
346 
347 static ACPI_STATUS
348 AcpiDbExecuteSetup (
349     ACPI_DB_METHOD_INFO     *Info)
350 {
351     ACPI_STATUS             Status;
352 
353 
354     ACPI_FUNCTION_NAME (DbExecuteSetup);
355 
356 
357     /* Catenate the current scope to the supplied name */
358 
359     Info->Pathname[0] = 0;
360     if ((Info->Name[0] != '\\') &&
361         (Info->Name[0] != '/'))
362     {
363         if (AcpiUtSafeStrcat (Info->Pathname, sizeof (Info->Pathname),
364             AcpiGbl_DbScopeBuf))
365         {
366             Status = AE_BUFFER_OVERFLOW;
367             goto ErrorExit;
368         }
369     }
370 
371     if (AcpiUtSafeStrcat (Info->Pathname, sizeof (Info->Pathname),
372         Info->Name))
373     {
374         Status = AE_BUFFER_OVERFLOW;
375         goto ErrorExit;
376     }
377 
378     AcpiDbPrepNamestring (Info->Pathname);
379 
380     AcpiDbSetOutputDestination (ACPI_DB_DUPLICATE_OUTPUT);
381     AcpiOsPrintf ("Evaluating %s\n", Info->Pathname);
382 
383     if (Info->Flags & EX_SINGLE_STEP)
384     {
385         AcpiGbl_CmSingleStep = TRUE;
386         AcpiDbSetOutputDestination (ACPI_DB_CONSOLE_OUTPUT);
387     }
388 
389     else
390     {
391         /* No single step, allow redirection to a file */
392 
393         AcpiDbSetOutputDestination (ACPI_DB_REDIRECTABLE_OUTPUT);
394     }
395 
396     return (AE_OK);
397 
398 ErrorExit:
399 
400     ACPI_EXCEPTION ((AE_INFO, Status, "During setup for method execution"));
401     return (Status);
402 }
403 
404 
405 #ifdef ACPI_DBG_TRACK_ALLOCATIONS
406 UINT32
407 AcpiDbGetCacheInfo (
408     ACPI_MEMORY_LIST        *Cache)
409 {
410 
411     return (Cache->TotalAllocated - Cache->TotalFreed - Cache->CurrentDepth);
412 }
413 #endif
414 
415 /*******************************************************************************
416  *
417  * FUNCTION:    AcpiDbGetOutstandingAllocations
418  *
419  * PARAMETERS:  None
420  *
421  * RETURN:      Current global allocation count minus cache entries
422  *
423  * DESCRIPTION: Determine the current number of "outstanding" allocations --
424  *              those allocations that have not been freed and also are not
425  *              in one of the various object caches.
426  *
427  ******************************************************************************/
428 
429 static UINT32
430 AcpiDbGetOutstandingAllocations (
431     void)
432 {
433     UINT32                  Outstanding = 0;
434 
435 #ifdef ACPI_DBG_TRACK_ALLOCATIONS
436 
437     Outstanding += AcpiDbGetCacheInfo (AcpiGbl_StateCache);
438     Outstanding += AcpiDbGetCacheInfo (AcpiGbl_PsNodeCache);
439     Outstanding += AcpiDbGetCacheInfo (AcpiGbl_PsNodeExtCache);
440     Outstanding += AcpiDbGetCacheInfo (AcpiGbl_OperandCache);
441 #endif
442 
443     return (Outstanding);
444 }
445 
446 
447 /*******************************************************************************
448  *
449  * FUNCTION:    AcpiDbExecutionWalk
450  *
451  * PARAMETERS:  WALK_CALLBACK
452  *
453  * RETURN:      Status
454  *
455  * DESCRIPTION: Execute a control method. Name is relative to the current
456  *              scope.
457  *
458  ******************************************************************************/
459 
460 static ACPI_STATUS
461 AcpiDbExecutionWalk (
462     ACPI_HANDLE             ObjHandle,
463     UINT32                  NestingLevel,
464     void                    *Context,
465     void                    **ReturnValue)
466 {
467     ACPI_OPERAND_OBJECT     *ObjDesc;
468     ACPI_NAMESPACE_NODE     *Node = (ACPI_NAMESPACE_NODE *) ObjHandle;
469     ACPI_BUFFER             ReturnObj;
470     ACPI_STATUS             Status;
471 
472 
473     ObjDesc = AcpiNsGetAttachedObject (Node);
474     if (ObjDesc->Method.ParamCount)
475     {
476         return (AE_OK);
477     }
478 
479     ReturnObj.Pointer = NULL;
480     ReturnObj.Length = ACPI_ALLOCATE_BUFFER;
481 
482     AcpiNsPrintNodePathname (Node, "Evaluating");
483 
484     /* Do the actual method execution */
485 
486     AcpiOsPrintf ("\n");
487     AcpiGbl_MethodExecuting = TRUE;
488 
489     Status = AcpiEvaluateObject (Node, NULL, NULL, &ReturnObj);
490 
491     AcpiOsPrintf ("Evaluation of [%4.4s] returned %s\n",
492         AcpiUtGetNodeName (Node),
493         AcpiFormatException (Status));
494 
495     AcpiGbl_MethodExecuting = FALSE;
496     return (AE_OK);
497 }
498 
499 
500 /*******************************************************************************
501  *
502  * FUNCTION:    AcpiDbExecute
503  *
504  * PARAMETERS:  Name                - Name of method to execute
505  *              Args                - Parameters to the method
506  *              Types               -
507  *              Flags               - single step/no single step
508  *
509  * RETURN:      None
510  *
511  * DESCRIPTION: Execute a control method. Name is relative to the current
512  *              scope.
513  *
514  ******************************************************************************/
515 
516 void
517 AcpiDbExecute (
518     char                    *Name,
519     char                    **Args,
520     ACPI_OBJECT_TYPE        *Types,
521     UINT32                  Flags)
522 {
523     ACPI_STATUS             Status;
524     ACPI_BUFFER             ReturnObj;
525     char                    *NameString;
526 
527 #ifdef ACPI_DEBUG_OUTPUT
528     UINT32                  PreviousAllocations;
529     UINT32                  Allocations;
530 #endif
531 
532 
533     /*
534      * Allow one execution to be performed by debugger or single step
535      * execution will be dead locked by the interpreter mutexes.
536      */
537     if (AcpiGbl_MethodExecuting)
538     {
539         AcpiOsPrintf ("Only one debugger execution is allowed.\n");
540         return;
541     }
542 
543 #ifdef ACPI_DEBUG_OUTPUT
544     /* Memory allocation tracking */
545 
546     PreviousAllocations = AcpiDbGetOutstandingAllocations ();
547 #endif
548 
549     if (*Name == '*')
550     {
551         (void) AcpiWalkNamespace (ACPI_TYPE_METHOD, ACPI_ROOT_OBJECT,
552             ACPI_UINT32_MAX, AcpiDbExecutionWalk, NULL, NULL, NULL);
553         return;
554     }
555 
556     NameString = ACPI_ALLOCATE (strlen (Name) + 1);
557     if (!NameString)
558     {
559         return;
560     }
561 
562     memset (&AcpiGbl_DbMethodInfo, 0, sizeof (ACPI_DB_METHOD_INFO));
563     strcpy (NameString, Name);
564     AcpiUtStrupr (NameString);
565 
566     /* Subcommand to Execute all predefined names in the namespace */
567 
568     if (!strncmp (NameString, "PREDEF", 6))
569     {
570         AcpiDbEvaluatePredefinedNames ();
571         ACPI_FREE (NameString);
572         return;
573     }
574 
575     AcpiGbl_DbMethodInfo.Name = NameString;
576     AcpiGbl_DbMethodInfo.Args = Args;
577     AcpiGbl_DbMethodInfo.Types = Types;
578     AcpiGbl_DbMethodInfo.Flags = Flags;
579 
580     ReturnObj.Pointer = NULL;
581     ReturnObj.Length = ACPI_ALLOCATE_BUFFER;
582 
583     Status = AcpiDbExecuteSetup (&AcpiGbl_DbMethodInfo);
584     if (ACPI_FAILURE (Status))
585     {
586         ACPI_FREE (NameString);
587         return;
588     }
589 
590     /* Get the NS node, determines existence also */
591 
592     Status = AcpiGetHandle (NULL, AcpiGbl_DbMethodInfo.Pathname,
593         &AcpiGbl_DbMethodInfo.Method);
594     if (ACPI_SUCCESS (Status))
595     {
596         Status = AcpiDbExecuteMethod (&AcpiGbl_DbMethodInfo,
597             &ReturnObj);
598     }
599     ACPI_FREE (NameString);
600 
601     /*
602      * Allow any handlers in separate threads to complete.
603      * (Such as Notify handlers invoked from AML executed above).
604      */
605     AcpiOsSleep ((UINT64) 10);
606 
607 #ifdef ACPI_DEBUG_OUTPUT
608 
609     /* Memory allocation tracking */
610 
611     Allocations = AcpiDbGetOutstandingAllocations () - PreviousAllocations;
612 
613     AcpiDbSetOutputDestination (ACPI_DB_DUPLICATE_OUTPUT);
614 
615     if (Allocations > 0)
616     {
617         AcpiOsPrintf (
618             "0x%X Outstanding allocations after evaluation of %s\n",
619             Allocations, AcpiGbl_DbMethodInfo.Pathname);
620     }
621 #endif
622 
623     if (ACPI_FAILURE (Status))
624     {
625         AcpiOsPrintf ("Evaluation of %s failed with status %s\n",
626             AcpiGbl_DbMethodInfo.Pathname,
627             AcpiFormatException (Status));
628     }
629     else
630     {
631         /* Display a return object, if any */
632 
633         if (ReturnObj.Length)
634         {
635             AcpiOsPrintf (
636                 "Evaluation of %s returned object %p, "
637                 "external buffer length %X\n",
638                 AcpiGbl_DbMethodInfo.Pathname, ReturnObj.Pointer,
639                 (UINT32) ReturnObj.Length);
640 
641             AcpiDbDumpExternalObject (ReturnObj.Pointer, 1);
642 
643             /* Dump a _PLD buffer if present */
644 
645             if (ACPI_COMPARE_NAME ((ACPI_CAST_PTR (ACPI_NAMESPACE_NODE,
646                 AcpiGbl_DbMethodInfo.Method)->Name.Ascii),
647                 METHOD_NAME__PLD))
648             {
649                 AcpiDbDumpPldBuffer (ReturnObj.Pointer);
650             }
651         }
652         else
653         {
654             AcpiOsPrintf ("No object was returned from evaluation of %s\n",
655                 AcpiGbl_DbMethodInfo.Pathname);
656         }
657     }
658 
659     AcpiDbSetOutputDestination (ACPI_DB_CONSOLE_OUTPUT);
660 }
661 
662 
663 /*******************************************************************************
664  *
665  * FUNCTION:    AcpiDbMethodThread
666  *
667  * PARAMETERS:  Context             - Execution info segment
668  *
669  * RETURN:      None
670  *
671  * DESCRIPTION: Debugger execute thread. Waits for a command line, then
672  *              simply dispatches it.
673  *
674  ******************************************************************************/
675 
676 static void ACPI_SYSTEM_XFACE
677 AcpiDbMethodThread (
678     void                    *Context)
679 {
680     ACPI_STATUS             Status;
681     ACPI_DB_METHOD_INFO     *Info = Context;
682     ACPI_DB_METHOD_INFO     LocalInfo;
683     UINT32                  i;
684     UINT8                   Allow;
685     ACPI_BUFFER             ReturnObj;
686 
687 
688     /*
689      * AcpiGbl_DbMethodInfo.Arguments will be passed as method arguments.
690      * Prevent AcpiGbl_DbMethodInfo from being modified by multiple threads
691      * concurrently.
692      *
693      * Note: The arguments we are passing are used by the ASL test suite
694      * (aslts). Do not change them without updating the tests.
695      */
696     (void) AcpiOsWaitSemaphore (Info->InfoGate, 1, ACPI_WAIT_FOREVER);
697 
698     if (Info->InitArgs)
699     {
700         AcpiDbUint32ToHexString (Info->NumCreated,
701             Info->IndexOfThreadStr);
702         AcpiDbUint32ToHexString ((UINT32) AcpiOsGetThreadId (),
703             Info->IdOfThreadStr);
704     }
705 
706     if (Info->Threads && (Info->NumCreated < Info->NumThreads))
707     {
708         Info->Threads[Info->NumCreated++] = AcpiOsGetThreadId();
709     }
710 
711     LocalInfo = *Info;
712     LocalInfo.Args = LocalInfo.Arguments;
713     LocalInfo.Arguments[0] = LocalInfo.NumThreadsStr;
714     LocalInfo.Arguments[1] = LocalInfo.IdOfThreadStr;
715     LocalInfo.Arguments[2] = LocalInfo.IndexOfThreadStr;
716     LocalInfo.Arguments[3] = NULL;
717 
718     LocalInfo.Types = LocalInfo.ArgTypes;
719 
720     (void) AcpiOsSignalSemaphore (Info->InfoGate, 1);
721 
722     for (i = 0; i < Info->NumLoops; i++)
723     {
724         Status = AcpiDbExecuteMethod (&LocalInfo, &ReturnObj);
725         if (ACPI_FAILURE (Status))
726         {
727             AcpiOsPrintf ("%s During evaluation of %s at iteration %X\n",
728                 AcpiFormatException (Status), Info->Pathname, i);
729             if (Status == AE_ABORT_METHOD)
730             {
731                 break;
732             }
733         }
734 
735 #if 0
736         if ((i % 100) == 0)
737         {
738             AcpiOsPrintf ("%u loops, Thread 0x%x\n",
739                 i, AcpiOsGetThreadId ());
740         }
741 
742         if (ReturnObj.Length)
743         {
744             AcpiOsPrintf ("Evaluation of %s returned object %p Buflen %X\n",
745                 Info->Pathname, ReturnObj.Pointer,
746                 (UINT32) ReturnObj.Length);
747             AcpiDbDumpExternalObject (ReturnObj.Pointer, 1);
748         }
749 #endif
750     }
751 
752     /* Signal our completion */
753 
754     Allow = 0;
755     (void) AcpiOsWaitSemaphore (Info->ThreadCompleteGate,
756         1, ACPI_WAIT_FOREVER);
757     Info->NumCompleted++;
758 
759     if (Info->NumCompleted == Info->NumThreads)
760     {
761         /* Do signal for main thread once only */
762         Allow = 1;
763     }
764 
765     (void) AcpiOsSignalSemaphore (Info->ThreadCompleteGate, 1);
766 
767     if (Allow)
768     {
769         Status = AcpiOsSignalSemaphore (Info->MainThreadGate, 1);
770         if (ACPI_FAILURE (Status))
771         {
772             AcpiOsPrintf (
773                 "Could not signal debugger thread sync semaphore, %s\n",
774                 AcpiFormatException (Status));
775         }
776     }
777 }
778 
779 
780 /*******************************************************************************
781  *
782  * FUNCTION:    AcpiDbCreateExecutionThreads
783  *
784  * PARAMETERS:  NumThreadsArg           - Number of threads to create
785  *              NumLoopsArg             - Loop count for the thread(s)
786  *              MethodNameArg           - Control method to execute
787  *
788  * RETURN:      None
789  *
790  * DESCRIPTION: Create threads to execute method(s)
791  *
792  ******************************************************************************/
793 
794 void
795 AcpiDbCreateExecutionThreads (
796     char                    *NumThreadsArg,
797     char                    *NumLoopsArg,
798     char                    *MethodNameArg)
799 {
800     ACPI_STATUS             Status;
801     UINT32                  NumThreads;
802     UINT32                  NumLoops;
803     UINT32                  i;
804     UINT32                  Size;
805     ACPI_MUTEX              MainThreadGate;
806     ACPI_MUTEX              ThreadCompleteGate;
807     ACPI_MUTEX              InfoGate;
808 
809 
810     /* Get the arguments */
811 
812     NumThreads = strtoul (NumThreadsArg, NULL, 0);
813     NumLoops = strtoul (NumLoopsArg, NULL, 0);
814 
815     if (!NumThreads || !NumLoops)
816     {
817         AcpiOsPrintf ("Bad argument: Threads %X, Loops %X\n",
818             NumThreads, NumLoops);
819         return;
820     }
821 
822     /*
823      * Create the semaphore for synchronization of
824      * the created threads with the main thread.
825      */
826     Status = AcpiOsCreateSemaphore (1, 0, &MainThreadGate);
827     if (ACPI_FAILURE (Status))
828     {
829         AcpiOsPrintf ("Could not create semaphore for "
830             "synchronization with the main thread, %s\n",
831             AcpiFormatException (Status));
832         return;
833     }
834 
835     /*
836      * Create the semaphore for synchronization
837      * between the created threads.
838      */
839     Status = AcpiOsCreateSemaphore (1, 1, &ThreadCompleteGate);
840     if (ACPI_FAILURE (Status))
841     {
842         AcpiOsPrintf ("Could not create semaphore for "
843             "synchronization between the created threads, %s\n",
844             AcpiFormatException (Status));
845 
846         (void) AcpiOsDeleteSemaphore (MainThreadGate);
847         return;
848     }
849 
850     Status = AcpiOsCreateSemaphore (1, 1, &InfoGate);
851     if (ACPI_FAILURE (Status))
852     {
853         AcpiOsPrintf ("Could not create semaphore for "
854             "synchronization of AcpiGbl_DbMethodInfo, %s\n",
855             AcpiFormatException (Status));
856 
857         (void) AcpiOsDeleteSemaphore (ThreadCompleteGate);
858         (void) AcpiOsDeleteSemaphore (MainThreadGate);
859         return;
860     }
861 
862     memset (&AcpiGbl_DbMethodInfo, 0, sizeof (ACPI_DB_METHOD_INFO));
863 
864     /* Array to store IDs of threads */
865 
866     AcpiGbl_DbMethodInfo.NumThreads = NumThreads;
867     Size = sizeof (ACPI_THREAD_ID) * AcpiGbl_DbMethodInfo.NumThreads;
868 
869     AcpiGbl_DbMethodInfo.Threads = AcpiOsAllocate (Size);
870     if (AcpiGbl_DbMethodInfo.Threads == NULL)
871     {
872         AcpiOsPrintf ("No memory for thread IDs array\n");
873         (void) AcpiOsDeleteSemaphore (MainThreadGate);
874         (void) AcpiOsDeleteSemaphore (ThreadCompleteGate);
875         (void) AcpiOsDeleteSemaphore (InfoGate);
876         return;
877     }
878     memset (AcpiGbl_DbMethodInfo.Threads, 0, Size);
879 
880     /* Setup the context to be passed to each thread */
881 
882     AcpiGbl_DbMethodInfo.Name = MethodNameArg;
883     AcpiGbl_DbMethodInfo.Flags = 0;
884     AcpiGbl_DbMethodInfo.NumLoops = NumLoops;
885     AcpiGbl_DbMethodInfo.MainThreadGate = MainThreadGate;
886     AcpiGbl_DbMethodInfo.ThreadCompleteGate = ThreadCompleteGate;
887     AcpiGbl_DbMethodInfo.InfoGate = InfoGate;
888 
889     /* Init arguments to be passed to method */
890 
891     AcpiGbl_DbMethodInfo.InitArgs = 1;
892     AcpiGbl_DbMethodInfo.Args = AcpiGbl_DbMethodInfo.Arguments;
893     AcpiGbl_DbMethodInfo.Arguments[0] = AcpiGbl_DbMethodInfo.NumThreadsStr;
894     AcpiGbl_DbMethodInfo.Arguments[1] = AcpiGbl_DbMethodInfo.IdOfThreadStr;
895     AcpiGbl_DbMethodInfo.Arguments[2] = AcpiGbl_DbMethodInfo.IndexOfThreadStr;
896     AcpiGbl_DbMethodInfo.Arguments[3] = NULL;
897 
898     AcpiGbl_DbMethodInfo.Types = AcpiGbl_DbMethodInfo.ArgTypes;
899     AcpiGbl_DbMethodInfo.ArgTypes[0] = ACPI_TYPE_INTEGER;
900     AcpiGbl_DbMethodInfo.ArgTypes[1] = ACPI_TYPE_INTEGER;
901     AcpiGbl_DbMethodInfo.ArgTypes[2] = ACPI_TYPE_INTEGER;
902 
903     AcpiDbUint32ToHexString (NumThreads, AcpiGbl_DbMethodInfo.NumThreadsStr);
904 
905     Status = AcpiDbExecuteSetup (&AcpiGbl_DbMethodInfo);
906     if (ACPI_FAILURE (Status))
907     {
908         goto CleanupAndExit;
909     }
910 
911     /* Get the NS node, determines existence also */
912 
913     Status = AcpiGetHandle (NULL, AcpiGbl_DbMethodInfo.Pathname,
914         &AcpiGbl_DbMethodInfo.Method);
915     if (ACPI_FAILURE (Status))
916     {
917         AcpiOsPrintf ("%s Could not get handle for %s\n",
918             AcpiFormatException (Status), AcpiGbl_DbMethodInfo.Pathname);
919         goto CleanupAndExit;
920     }
921 
922     /* Create the threads */
923 
924     AcpiOsPrintf ("Creating %X threads to execute %X times each\n",
925         NumThreads, NumLoops);
926 
927     for (i = 0; i < (NumThreads); i++)
928     {
929         Status = AcpiOsExecute (OSL_DEBUGGER_EXEC_THREAD, AcpiDbMethodThread,
930             &AcpiGbl_DbMethodInfo);
931         if (ACPI_FAILURE (Status))
932         {
933             break;
934         }
935     }
936 
937     /* Wait for all threads to complete */
938 
939     (void) AcpiOsWaitSemaphore (MainThreadGate, 1, ACPI_WAIT_FOREVER);
940 
941     AcpiDbSetOutputDestination (ACPI_DB_DUPLICATE_OUTPUT);
942     AcpiOsPrintf ("All threads (%X) have completed\n", NumThreads);
943     AcpiDbSetOutputDestination (ACPI_DB_CONSOLE_OUTPUT);
944 
945 CleanupAndExit:
946 
947     /* Cleanup and exit */
948 
949     (void) AcpiOsDeleteSemaphore (MainThreadGate);
950     (void) AcpiOsDeleteSemaphore (ThreadCompleteGate);
951     (void) AcpiOsDeleteSemaphore (InfoGate);
952 
953     AcpiOsFree (AcpiGbl_DbMethodInfo.Threads);
954     AcpiGbl_DbMethodInfo.Threads = NULL;
955 }
956