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 - 2023, 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 
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 static void ACPI_SYSTEM_XFACE
191 AcpiDbSingleExecutionThread (
192     void                    *Context);
193 
194 
195 /*******************************************************************************
196  *
197  * FUNCTION:    AcpiDbDeleteObjects
198  *
199  * PARAMETERS:  Count               - Count of objects in the list
200  *              Objects             - Array of ACPI_OBJECTs to be deleted
201  *
202  * RETURN:      None
203  *
204  * DESCRIPTION: Delete a list of ACPI_OBJECTS. Handles packages and nested
205  *              packages via recursion.
206  *
207  ******************************************************************************/
208 
209 void
AcpiDbDeleteObjects(UINT32 Count,ACPI_OBJECT * Objects)210 AcpiDbDeleteObjects (
211     UINT32                  Count,
212     ACPI_OBJECT             *Objects)
213 {
214     UINT32                  i;
215 
216 
217     for (i = 0; i < Count; i++)
218     {
219         switch (Objects[i].Type)
220         {
221         case ACPI_TYPE_BUFFER:
222 
223             ACPI_FREE (Objects[i].Buffer.Pointer);
224             break;
225 
226         case ACPI_TYPE_PACKAGE:
227 
228             /* Recursive call to delete package elements */
229 
230             AcpiDbDeleteObjects (Objects[i].Package.Count,
231                 Objects[i].Package.Elements);
232 
233             /* Free the elements array */
234 
235             ACPI_FREE (Objects[i].Package.Elements);
236             break;
237 
238         default:
239 
240             break;
241         }
242     }
243 }
244 
245 
246 /*******************************************************************************
247  *
248  * FUNCTION:    AcpiDbExecuteMethod
249  *
250  * PARAMETERS:  Info            - Valid info segment
251  *              ReturnObj       - Where to put return object
252  *
253  * RETURN:      Status
254  *
255  * DESCRIPTION: Execute a control method. Used to evaluate objects via the
256  *              "EXECUTE" or "EVALUATE" commands.
257  *
258  ******************************************************************************/
259 
260 static ACPI_STATUS
AcpiDbExecuteMethod(ACPI_DB_METHOD_INFO * Info,ACPI_BUFFER * ReturnObj)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
AcpiDbExecuteSetup(ACPI_DB_METHOD_INFO * Info)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
AcpiDbGetCacheInfo(ACPI_MEMORY_LIST * Cache)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
AcpiDbGetOutstandingAllocations(void)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
AcpiDbExecutionWalk(ACPI_HANDLE ObjHandle,UINT32 NestingLevel,void * Context,void ** ReturnValue)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     AcpiGbl_MethodExecuting = FALSE;
509 
510     AcpiOsPrintf ("Evaluation of [%4.4s] returned %s\n",
511         AcpiUtGetNodeName (Node),
512         AcpiFormatException (Status));
513 
514     return (AE_OK);
515 }
516 
517 
518 /*******************************************************************************
519  *
520  * FUNCTION:    AcpiDbExecute
521  *
522  * PARAMETERS:  Name                - Name of method to execute
523  *              Args                - Parameters to the method
524  *              Types               -
525  *              Flags               - single step/no single step
526  *
527  * RETURN:      None
528  *
529  * DESCRIPTION: Execute a control method. Name is relative to the current
530  *              scope. Function used for the "EXECUTE", "EVALUATE", and
531  *              "ALL" commands
532  *
533  ******************************************************************************/
534 
535 void
AcpiDbExecute(char * Name,char ** Args,ACPI_OBJECT_TYPE * Types,UINT32 Flags)536 AcpiDbExecute (
537     char                    *Name,
538     char                    **Args,
539     ACPI_OBJECT_TYPE        *Types,
540     UINT32                  Flags)
541 {
542     ACPI_STATUS             Status;
543     ACPI_BUFFER             ReturnObj;
544     char                    *NameString;
545 
546 #ifdef ACPI_DEBUG_OUTPUT
547     UINT32                  PreviousAllocations;
548     UINT32                  Allocations;
549 #endif
550 
551 
552     /*
553      * Allow one execution to be performed by debugger or single step
554      * execution will be dead locked by the interpreter mutexes.
555      */
556     if (AcpiGbl_MethodExecuting)
557     {
558         AcpiOsPrintf ("Only one debugger execution is allowed.\n");
559         return;
560     }
561 
562 #ifdef ACPI_DEBUG_OUTPUT
563     /* Memory allocation tracking */
564 
565     PreviousAllocations = AcpiDbGetOutstandingAllocations ();
566 #endif
567 
568     if (*Name == '*')
569     {
570         (void) AcpiWalkNamespace (ACPI_TYPE_METHOD, ACPI_ROOT_OBJECT,
571             ACPI_UINT32_MAX, AcpiDbExecutionWalk, NULL, NULL, NULL);
572         return;
573     }
574 
575     if ((Flags & EX_ALL) && (strlen (Name) > 4))
576     {
577         AcpiOsPrintf ("Input name (%s) must be a 4-char NameSeg\n", Name);
578         return;
579     }
580 
581     NameString = ACPI_ALLOCATE (strlen (Name) + 1);
582     if (!NameString)
583     {
584         return;
585     }
586 
587     memset (&AcpiGbl_DbMethodInfo, 0, sizeof (ACPI_DB_METHOD_INFO));
588     strcpy (NameString, Name);
589     AcpiUtStrupr (NameString);
590 
591     /* Subcommand to Execute all predefined names in the namespace */
592 
593     if (!strncmp (NameString, "PREDEF", 6))
594     {
595         AcpiDbEvaluatePredefinedNames ();
596         ACPI_FREE (NameString);
597         return;
598     }
599 
600     /* Command (ALL <nameseg>) to execute all methods of a particular name */
601 
602     else if (Flags & EX_ALL)
603     {
604         AcpiGbl_DbMethodInfo.Name = NameString;
605         ReturnObj.Pointer = NULL;
606         ReturnObj.Length = ACPI_ALLOCATE_BUFFER;
607         AcpiDbEvaluateAll (NameString);
608         ACPI_FREE (NameString);
609         return;
610     }
611     else
612     {
613         AcpiGbl_DbMethodInfo.Name = NameString;
614         AcpiGbl_DbMethodInfo.Args = Args;
615         AcpiGbl_DbMethodInfo.Types = Types;
616         AcpiGbl_DbMethodInfo.Flags = Flags;
617 
618         ReturnObj.Pointer = NULL;
619         ReturnObj.Length = ACPI_ALLOCATE_BUFFER;
620     }
621 
622     Status = AcpiDbExecuteSetup (&AcpiGbl_DbMethodInfo);
623     if (ACPI_FAILURE (Status))
624     {
625         ACPI_FREE (NameString);
626         return;
627     }
628 
629     /* Get the NS node, determines existence also */
630 
631     Status = AcpiGetHandle (NULL, AcpiGbl_DbMethodInfo.Pathname,
632         &AcpiGbl_DbMethodInfo.Method);
633     if (ACPI_SUCCESS (Status))
634     {
635         Status = AcpiDbExecuteMethod (&AcpiGbl_DbMethodInfo,
636             &ReturnObj);
637     }
638     ACPI_FREE (NameString);
639 
640     /*
641      * Allow any handlers in separate threads to complete.
642      * (Such as Notify handlers invoked from AML executed above).
643      */
644     AcpiOsSleep ((UINT64) 10);
645 
646 #ifdef ACPI_DEBUG_OUTPUT
647 
648     /* Memory allocation tracking */
649 
650     Allocations = AcpiDbGetOutstandingAllocations () - PreviousAllocations;
651 
652     AcpiDbSetOutputDestination (ACPI_DB_DUPLICATE_OUTPUT);
653 
654     if (Allocations > 0)
655     {
656         AcpiOsPrintf (
657             "0x%X Outstanding allocations after evaluation of %s\n",
658             Allocations, AcpiGbl_DbMethodInfo.Pathname);
659     }
660 #endif
661 
662     if (ACPI_FAILURE (Status))
663     {
664         AcpiOsPrintf ("Evaluation of %s failed with status %s\n",
665             AcpiGbl_DbMethodInfo.Pathname,
666             AcpiFormatException (Status));
667     }
668     else
669     {
670         /* Display a return object, if any */
671 
672         if (ReturnObj.Length)
673         {
674             AcpiOsPrintf (
675                 "Evaluation of %s returned object %p, "
676                 "external buffer length %X\n",
677                 AcpiGbl_DbMethodInfo.Pathname, ReturnObj.Pointer,
678                 (UINT32) ReturnObj.Length);
679 
680             AcpiDbDumpExternalObject (ReturnObj.Pointer, 1);
681             AcpiOsPrintf ("\n");
682 
683             /* Dump a _PLD buffer if present */
684 
685             if (ACPI_COMPARE_NAMESEG ((ACPI_CAST_PTR (ACPI_NAMESPACE_NODE,
686                 AcpiGbl_DbMethodInfo.Method)->Name.Ascii),
687                 METHOD_NAME__PLD))
688             {
689                 AcpiDbDumpPldBuffer (ReturnObj.Pointer);
690             }
691         }
692         else
693         {
694             AcpiOsPrintf ("No object was returned from evaluation of %s\n",
695                 AcpiGbl_DbMethodInfo.Pathname);
696         }
697     }
698 
699     AcpiDbSetOutputDestination (ACPI_DB_CONSOLE_OUTPUT);
700 }
701 
702 
703 /*******************************************************************************
704  *
705  * FUNCTION:    AcpiDbMethodThread
706  *
707  * PARAMETERS:  Context             - Execution info segment
708  *
709  * RETURN:      None
710  *
711  * DESCRIPTION: Debugger execute thread. Waits for a command line, then
712  *              simply dispatches it.
713  *
714  ******************************************************************************/
715 
716 static void ACPI_SYSTEM_XFACE
AcpiDbMethodThread(void * Context)717 AcpiDbMethodThread (
718     void                    *Context)
719 {
720     ACPI_STATUS             Status;
721     ACPI_DB_METHOD_INFO     *Info = Context;
722     ACPI_DB_METHOD_INFO     LocalInfo;
723     UINT32                  i;
724     UINT8                   Allow;
725     ACPI_BUFFER             ReturnObj;
726 
727 
728     /*
729      * AcpiGbl_DbMethodInfo.Arguments will be passed as method arguments.
730      * Prevent AcpiGbl_DbMethodInfo from being modified by multiple threads
731      * concurrently.
732      *
733      * Note: The arguments we are passing are used by the ASL test suite
734      * (aslts). Do not change them without updating the tests.
735      */
736     (void) AcpiOsWaitSemaphore (Info->InfoGate, 1, ACPI_WAIT_FOREVER);
737 
738     if (Info->InitArgs)
739     {
740         AcpiDbUint32ToHexString (Info->NumCreated,
741             Info->IndexOfThreadStr);
742         AcpiDbUint32ToHexString ((UINT32) AcpiOsGetThreadId (),
743             Info->IdOfThreadStr);
744     }
745 
746     if (Info->Threads && (Info->NumCreated < Info->NumThreads))
747     {
748         Info->Threads[Info->NumCreated++] = AcpiOsGetThreadId();
749     }
750 
751     LocalInfo = *Info;
752     LocalInfo.Args = LocalInfo.Arguments;
753     LocalInfo.Arguments[0] = LocalInfo.NumThreadsStr;
754     LocalInfo.Arguments[1] = LocalInfo.IdOfThreadStr;
755     LocalInfo.Arguments[2] = LocalInfo.IndexOfThreadStr;
756     LocalInfo.Arguments[3] = NULL;
757 
758     LocalInfo.Types = LocalInfo.ArgTypes;
759 
760     (void) AcpiOsSignalSemaphore (Info->InfoGate, 1);
761 
762     for (i = 0; i < Info->NumLoops; i++)
763     {
764         Status = AcpiDbExecuteMethod (&LocalInfo, &ReturnObj);
765         if (ACPI_FAILURE (Status))
766         {
767             AcpiOsPrintf ("%s During evaluation of %s at iteration %X\n",
768                 AcpiFormatException (Status), Info->Pathname, i);
769             if (Status == AE_ABORT_METHOD)
770             {
771                 break;
772             }
773         }
774 
775 #if 0
776         if ((i % 100) == 0)
777         {
778             AcpiOsPrintf ("%u loops, Thread 0x%x\n",
779                 i, AcpiOsGetThreadId ());
780         }
781 
782         if (ReturnObj.Length)
783         {
784             AcpiOsPrintf ("Evaluation of %s returned object %p Buflen %X\n",
785                 Info->Pathname, ReturnObj.Pointer,
786                 (UINT32) ReturnObj.Length);
787             AcpiDbDumpExternalObject (ReturnObj.Pointer, 1);
788         }
789 #endif
790     }
791 
792     /* Signal our completion */
793 
794     Allow = 0;
795     (void) AcpiOsWaitSemaphore (Info->ThreadCompleteGate,
796         1, ACPI_WAIT_FOREVER);
797     Info->NumCompleted++;
798 
799     if (Info->NumCompleted == Info->NumThreads)
800     {
801         /* Do signal for main thread once only */
802         Allow = 1;
803     }
804 
805     (void) AcpiOsSignalSemaphore (Info->ThreadCompleteGate, 1);
806 
807     if (Allow)
808     {
809         Status = AcpiOsSignalSemaphore (Info->MainThreadGate, 1);
810         if (ACPI_FAILURE (Status))
811         {
812             AcpiOsPrintf (
813                 "Could not signal debugger thread sync semaphore, %s\n",
814                 AcpiFormatException (Status));
815         }
816     }
817 }
818 
819 
820 /*******************************************************************************
821  *
822  * FUNCTION:    AcpiDbSingleExecutionThread
823  *
824  * PARAMETERS:  Context                 - Method info struct
825  *
826  * RETURN:      None
827  *
828  * DESCRIPTION: Create one thread and execute a method
829  *
830  ******************************************************************************/
831 
832 static void ACPI_SYSTEM_XFACE
AcpiDbSingleExecutionThread(void * Context)833 AcpiDbSingleExecutionThread (
834     void                    *Context)
835 {
836     ACPI_DB_METHOD_INFO     *Info = Context;
837     ACPI_STATUS             Status;
838     ACPI_BUFFER             ReturnObj;
839 
840 
841     AcpiOsPrintf ("\n");
842 
843     Status = AcpiDbExecuteMethod (Info, &ReturnObj);
844     if (ACPI_FAILURE (Status))
845     {
846         AcpiOsPrintf ("%s During evaluation of %s\n",
847             AcpiFormatException (Status), Info->Pathname);
848         return;
849     }
850 
851     /* Display a return object, if any */
852 
853     if (ReturnObj.Length)
854     {
855         AcpiOsPrintf ("Evaluation of %s returned object %p, "
856             "external buffer length %X\n",
857             AcpiGbl_DbMethodInfo.Pathname, ReturnObj.Pointer,
858             (UINT32) ReturnObj.Length);
859 
860         AcpiDbDumpExternalObject (ReturnObj.Pointer, 1);
861     }
862 
863     AcpiOsPrintf ("\nBackground thread completed\n%c ",
864         ACPI_DEBUGGER_COMMAND_PROMPT);
865 }
866 
867 
868 /*******************************************************************************
869  *
870  * FUNCTION:    AcpiDbCreateExecutionThread
871  *
872  * PARAMETERS:  MethodNameArg           - Control method to execute
873  *              Arguments               - Array of arguments to the method
874  *              Types                   - Corresponding array of object types
875  *
876  * RETURN:      None
877  *
878  * DESCRIPTION: Create a single thread to evaluate a namespace object. Handles
879  *              arguments passed on command line for control methods.
880  *
881  ******************************************************************************/
882 
883 void
AcpiDbCreateExecutionThread(char * MethodNameArg,char ** Arguments,ACPI_OBJECT_TYPE * Types)884 AcpiDbCreateExecutionThread (
885     char                    *MethodNameArg,
886     char                    **Arguments,
887     ACPI_OBJECT_TYPE        *Types)
888 {
889     ACPI_STATUS             Status;
890     UINT32                  i;
891 
892 
893     memset (&AcpiGbl_DbMethodInfo, 0, sizeof (ACPI_DB_METHOD_INFO));
894     AcpiGbl_DbMethodInfo.Name = MethodNameArg;
895     AcpiGbl_DbMethodInfo.InitArgs = 1;
896     AcpiGbl_DbMethodInfo.Args = AcpiGbl_DbMethodInfo.Arguments;
897     AcpiGbl_DbMethodInfo.Types = AcpiGbl_DbMethodInfo.ArgTypes;
898 
899     /* Setup method arguments, up to 7 (0-6) */
900 
901     for (i = 0; (i < ACPI_METHOD_NUM_ARGS) && *Arguments; i++)
902     {
903         AcpiGbl_DbMethodInfo.Arguments[i] = *Arguments;
904         Arguments++;
905 
906         AcpiGbl_DbMethodInfo.ArgTypes[i] = *Types;
907         Types++;
908     }
909 
910     Status = AcpiDbExecuteSetup (&AcpiGbl_DbMethodInfo);
911     if (ACPI_FAILURE (Status))
912     {
913         return;
914     }
915 
916     /* Get the NS node, determines existence also */
917 
918     Status = AcpiGetHandle (NULL, AcpiGbl_DbMethodInfo.Pathname,
919         &AcpiGbl_DbMethodInfo.Method);
920     if (ACPI_FAILURE (Status))
921     {
922         AcpiOsPrintf ("%s Could not get handle for %s\n",
923             AcpiFormatException (Status), AcpiGbl_DbMethodInfo.Pathname);
924         return;
925     }
926 
927     Status = AcpiOsExecute (OSL_DEBUGGER_EXEC_THREAD,
928         AcpiDbSingleExecutionThread, &AcpiGbl_DbMethodInfo);
929     if (ACPI_FAILURE (Status))
930     {
931         return;
932     }
933 
934     AcpiOsPrintf ("\nBackground thread started\n");
935 }
936 
937 
938 /*******************************************************************************
939  *
940  * FUNCTION:    AcpiDbCreateExecutionThreads
941  *
942  * PARAMETERS:  NumThreadsArg           - Number of threads to create
943  *              NumLoopsArg             - Loop count for the thread(s)
944  *              MethodNameArg           - Control method to execute
945  *
946  * RETURN:      None
947  *
948  * DESCRIPTION: Create threads to execute method(s)
949  *
950  ******************************************************************************/
951 
952 void
AcpiDbCreateExecutionThreads(char * NumThreadsArg,char * NumLoopsArg,char * MethodNameArg)953 AcpiDbCreateExecutionThreads (
954     char                    *NumThreadsArg,
955     char                    *NumLoopsArg,
956     char                    *MethodNameArg)
957 {
958     ACPI_STATUS             Status;
959     UINT32                  NumThreads;
960     UINT32                  NumLoops;
961     UINT32                  i;
962     UINT32                  Size;
963     ACPI_MUTEX              MainThreadGate;
964     ACPI_MUTEX              ThreadCompleteGate;
965     ACPI_MUTEX              InfoGate;
966 
967 
968     /* Get the arguments */
969 
970     NumThreads = strtoul (NumThreadsArg, NULL, 0);
971     NumLoops = strtoul (NumLoopsArg, NULL, 0);
972 
973     if (!NumThreads || !NumLoops)
974     {
975         AcpiOsPrintf ("Bad argument: Threads %X, Loops %X\n",
976             NumThreads, NumLoops);
977         return;
978     }
979 
980     /*
981      * Create the semaphore for synchronization of
982      * the created threads with the main thread.
983      */
984     Status = AcpiOsCreateSemaphore (1, 0, &MainThreadGate);
985     if (ACPI_FAILURE (Status))
986     {
987         AcpiOsPrintf ("Could not create semaphore for "
988             "synchronization with the main thread, %s\n",
989             AcpiFormatException (Status));
990         return;
991     }
992 
993     /*
994      * Create the semaphore for synchronization
995      * between the created threads.
996      */
997     Status = AcpiOsCreateSemaphore (1, 1, &ThreadCompleteGate);
998     if (ACPI_FAILURE (Status))
999     {
1000         AcpiOsPrintf ("Could not create semaphore for "
1001             "synchronization between the created threads, %s\n",
1002             AcpiFormatException (Status));
1003 
1004         (void) AcpiOsDeleteSemaphore (MainThreadGate);
1005         return;
1006     }
1007 
1008     Status = AcpiOsCreateSemaphore (1, 1, &InfoGate);
1009     if (ACPI_FAILURE (Status))
1010     {
1011         AcpiOsPrintf ("Could not create semaphore for "
1012             "synchronization of AcpiGbl_DbMethodInfo, %s\n",
1013             AcpiFormatException (Status));
1014 
1015         (void) AcpiOsDeleteSemaphore (ThreadCompleteGate);
1016         (void) AcpiOsDeleteSemaphore (MainThreadGate);
1017         return;
1018     }
1019 
1020     memset (&AcpiGbl_DbMethodInfo, 0, sizeof (ACPI_DB_METHOD_INFO));
1021 
1022     /* Array to store IDs of threads */
1023 
1024     AcpiGbl_DbMethodInfo.NumThreads = NumThreads;
1025     Size = sizeof (ACPI_THREAD_ID) * AcpiGbl_DbMethodInfo.NumThreads;
1026 
1027     AcpiGbl_DbMethodInfo.Threads = AcpiOsAllocate (Size);
1028     if (AcpiGbl_DbMethodInfo.Threads == NULL)
1029     {
1030         AcpiOsPrintf ("No memory for thread IDs array\n");
1031         (void) AcpiOsDeleteSemaphore (MainThreadGate);
1032         (void) AcpiOsDeleteSemaphore (ThreadCompleteGate);
1033         (void) AcpiOsDeleteSemaphore (InfoGate);
1034         return;
1035     }
1036     memset (AcpiGbl_DbMethodInfo.Threads, 0, Size);
1037 
1038     /* Setup the context to be passed to each thread */
1039 
1040     AcpiGbl_DbMethodInfo.Name = MethodNameArg;
1041     AcpiGbl_DbMethodInfo.Flags = 0;
1042     AcpiGbl_DbMethodInfo.NumLoops = NumLoops;
1043     AcpiGbl_DbMethodInfo.MainThreadGate = MainThreadGate;
1044     AcpiGbl_DbMethodInfo.ThreadCompleteGate = ThreadCompleteGate;
1045     AcpiGbl_DbMethodInfo.InfoGate = InfoGate;
1046 
1047     /* Init arguments to be passed to method */
1048 
1049     AcpiGbl_DbMethodInfo.InitArgs = 1;
1050     AcpiGbl_DbMethodInfo.Args = AcpiGbl_DbMethodInfo.Arguments;
1051     AcpiGbl_DbMethodInfo.Arguments[0] = AcpiGbl_DbMethodInfo.NumThreadsStr;
1052     AcpiGbl_DbMethodInfo.Arguments[1] = AcpiGbl_DbMethodInfo.IdOfThreadStr;
1053     AcpiGbl_DbMethodInfo.Arguments[2] = AcpiGbl_DbMethodInfo.IndexOfThreadStr;
1054     AcpiGbl_DbMethodInfo.Arguments[3] = NULL;
1055 
1056     AcpiGbl_DbMethodInfo.Types = AcpiGbl_DbMethodInfo.ArgTypes;
1057     AcpiGbl_DbMethodInfo.ArgTypes[0] = ACPI_TYPE_INTEGER;
1058     AcpiGbl_DbMethodInfo.ArgTypes[1] = ACPI_TYPE_INTEGER;
1059     AcpiGbl_DbMethodInfo.ArgTypes[2] = ACPI_TYPE_INTEGER;
1060 
1061     AcpiDbUint32ToHexString (NumThreads, AcpiGbl_DbMethodInfo.NumThreadsStr);
1062 
1063     Status = AcpiDbExecuteSetup (&AcpiGbl_DbMethodInfo);
1064     if (ACPI_FAILURE (Status))
1065     {
1066         goto CleanupAndExit;
1067     }
1068 
1069     /* Get the NS node, determines existence also */
1070 
1071     Status = AcpiGetHandle (NULL, AcpiGbl_DbMethodInfo.Pathname,
1072         &AcpiGbl_DbMethodInfo.Method);
1073     if (ACPI_FAILURE (Status))
1074     {
1075         AcpiOsPrintf ("%s Could not get handle for %s\n",
1076             AcpiFormatException (Status), AcpiGbl_DbMethodInfo.Pathname);
1077         goto CleanupAndExit;
1078     }
1079 
1080     /* Create the threads */
1081 
1082     AcpiOsPrintf ("Creating %X threads to execute %X times each\n",
1083         NumThreads, NumLoops);
1084 
1085     for (i = 0; i < (NumThreads); i++)
1086     {
1087         Status = AcpiOsExecute (OSL_DEBUGGER_EXEC_THREAD, AcpiDbMethodThread,
1088             &AcpiGbl_DbMethodInfo);
1089         if (ACPI_FAILURE (Status))
1090         {
1091             break;
1092         }
1093     }
1094 
1095     /* Wait for all threads to complete */
1096 
1097     (void) AcpiOsWaitSemaphore (MainThreadGate, 1, ACPI_WAIT_FOREVER);
1098 
1099     AcpiDbSetOutputDestination (ACPI_DB_DUPLICATE_OUTPUT);
1100     AcpiOsPrintf ("All threads (%X) have completed\n", NumThreads);
1101     AcpiDbSetOutputDestination (ACPI_DB_CONSOLE_OUTPUT);
1102 
1103 CleanupAndExit:
1104 
1105     /* Cleanup and exit */
1106 
1107     (void) AcpiOsDeleteSemaphore (MainThreadGate);
1108     (void) AcpiOsDeleteSemaphore (ThreadCompleteGate);
1109     (void) AcpiOsDeleteSemaphore (InfoGate);
1110 
1111     AcpiOsFree (AcpiGbl_DbMethodInfo.Threads);
1112     AcpiGbl_DbMethodInfo.Threads = NULL;
1113 }
1114