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