dbexec.c revision 216471
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 - 2010, 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
117#include <contrib/dev/acpica/include/acpi.h>
118#include <contrib/dev/acpica/include/accommon.h>
119#include <contrib/dev/acpica/include/acdebug.h>
120#include <contrib/dev/acpica/include/acnamesp.h>
121
122#ifdef ACPI_DEBUGGER
123
124#define _COMPONENT          ACPI_CA_DEBUGGER
125        ACPI_MODULE_NAME    ("dbexec")
126
127
128static ACPI_DB_METHOD_INFO  AcpiGbl_DbMethodInfo;
129
130/* Local prototypes */
131
132static ACPI_STATUS
133AcpiDbExecuteMethod (
134    ACPI_DB_METHOD_INFO     *Info,
135    ACPI_BUFFER             *ReturnObj);
136
137static void
138AcpiDbExecuteSetup (
139    ACPI_DB_METHOD_INFO     *Info);
140
141static UINT32
142AcpiDbGetOutstandingAllocations (
143    void);
144
145static void ACPI_SYSTEM_XFACE
146AcpiDbMethodThread (
147    void                    *Context);
148
149static ACPI_STATUS
150AcpiDbExecutionWalk (
151    ACPI_HANDLE             ObjHandle,
152    UINT32                  NestingLevel,
153    void                    *Context,
154    void                    **ReturnValue);
155
156
157/*******************************************************************************
158 *
159 * FUNCTION:    AcpiDbExecuteMethod
160 *
161 * PARAMETERS:  Info            - Valid info segment
162 *              ReturnObj       - Where to put return object
163 *
164 * RETURN:      Status
165 *
166 * DESCRIPTION: Execute a control method.
167 *
168 ******************************************************************************/
169
170static ACPI_STATUS
171AcpiDbExecuteMethod (
172    ACPI_DB_METHOD_INFO     *Info,
173    ACPI_BUFFER             *ReturnObj)
174{
175    ACPI_STATUS             Status;
176    ACPI_OBJECT_LIST        ParamObjects;
177    ACPI_OBJECT             Params[ACPI_METHOD_NUM_ARGS];
178    ACPI_HANDLE             Handle;
179    UINT32                  i;
180    ACPI_DEVICE_INFO        *ObjInfo;
181
182
183    ACPI_FUNCTION_TRACE (DbExecuteMethod);
184
185
186    if (AcpiGbl_DbOutputToFile && !AcpiDbgLevel)
187    {
188        AcpiOsPrintf ("Warning: debug output is not enabled!\n");
189    }
190
191    /* Get the NS node, determines existence also */
192
193    Status = AcpiGetHandle (NULL, Info->Pathname, &Handle);
194    if (ACPI_FAILURE (Status))
195    {
196        return_ACPI_STATUS (Status);
197    }
198
199    /* Get the object info for number of method parameters */
200
201    Status = AcpiGetObjectInfo (Handle, &ObjInfo);
202    if (ACPI_FAILURE (Status))
203    {
204        return_ACPI_STATUS (Status);
205    }
206
207    ParamObjects.Pointer = NULL;
208    ParamObjects.Count   = 0;
209
210    if (ObjInfo->Type == ACPI_TYPE_METHOD)
211    {
212        /* Are there arguments to the method? */
213
214        if (Info->Args && Info->Args[0])
215        {
216            for (i = 0; Info->Args[i] && i < ACPI_METHOD_NUM_ARGS; i++)
217            {
218                Params[i].Type          = ACPI_TYPE_INTEGER;
219                Params[i].Integer.Value = ACPI_STRTOUL (Info->Args[i], NULL, 16);
220            }
221
222            ParamObjects.Pointer = Params;
223            ParamObjects.Count   = i;
224        }
225        else
226        {
227            /* Setup default parameters */
228
229            for (i = 0; i < ObjInfo->ParamCount; i++)
230            {
231                switch (i)
232                {
233                case 0:
234
235                    Params[0].Type           = ACPI_TYPE_INTEGER;
236                    Params[0].Integer.Value  = 0x01020304;
237                    break;
238
239                case 1:
240
241                    Params[1].Type           = ACPI_TYPE_STRING;
242                    Params[1].String.Length  = 12;
243                    Params[1].String.Pointer = "AML Debugger";
244                    break;
245
246                default:
247
248                    Params[i].Type           = ACPI_TYPE_INTEGER;
249                    Params[i].Integer.Value  = i * (UINT64) 0x1000;
250                    break;
251                }
252            }
253
254            ParamObjects.Pointer     = Params;
255            ParamObjects.Count       = ObjInfo->ParamCount;
256        }
257    }
258
259    ACPI_FREE (ObjInfo);
260
261    /* Prepare for a return object of arbitrary size */
262
263    ReturnObj->Pointer = AcpiGbl_DbBuffer;
264    ReturnObj->Length  = ACPI_DEBUG_BUFFER_SIZE;
265
266    /* Do the actual method execution */
267
268    AcpiGbl_MethodExecuting = TRUE;
269    Status = AcpiEvaluateObject (NULL,
270                Info->Pathname, &ParamObjects, ReturnObj);
271
272    AcpiGbl_CmSingleStep = FALSE;
273    AcpiGbl_MethodExecuting = FALSE;
274
275    if (ACPI_FAILURE (Status))
276    {
277        ACPI_EXCEPTION ((AE_INFO, Status,
278        "while executing %s from debugger", Info->Pathname));
279
280        if (Status == AE_BUFFER_OVERFLOW)
281        {
282            ACPI_ERROR ((AE_INFO,
283            "Possible overflow of internal debugger buffer (size 0x%X needed 0x%X)",
284                ACPI_DEBUG_BUFFER_SIZE, (UINT32) ReturnObj->Length));
285        }
286    }
287
288    return_ACPI_STATUS (Status);
289}
290
291
292/*******************************************************************************
293 *
294 * FUNCTION:    AcpiDbExecuteSetup
295 *
296 * PARAMETERS:  Info            - Valid method info
297 *
298 * RETURN:      None
299 *
300 * DESCRIPTION: Setup info segment prior to method execution
301 *
302 ******************************************************************************/
303
304static void
305AcpiDbExecuteSetup (
306    ACPI_DB_METHOD_INFO     *Info)
307{
308
309    /* Catenate the current scope to the supplied name */
310
311    Info->Pathname[0] = 0;
312    if ((Info->Name[0] != '\\') &&
313        (Info->Name[0] != '/'))
314    {
315        ACPI_STRCAT (Info->Pathname, AcpiGbl_DbScopeBuf);
316    }
317
318    ACPI_STRCAT (Info->Pathname, Info->Name);
319    AcpiDbPrepNamestring (Info->Pathname);
320
321    AcpiDbSetOutputDestination (ACPI_DB_DUPLICATE_OUTPUT);
322    AcpiOsPrintf ("Executing %s\n", Info->Pathname);
323
324    if (Info->Flags & EX_SINGLE_STEP)
325    {
326        AcpiGbl_CmSingleStep = TRUE;
327        AcpiDbSetOutputDestination (ACPI_DB_CONSOLE_OUTPUT);
328    }
329
330    else
331    {
332        /* No single step, allow redirection to a file */
333
334        AcpiDbSetOutputDestination (ACPI_DB_REDIRECTABLE_OUTPUT);
335    }
336}
337
338
339#ifdef ACPI_DBG_TRACK_ALLOCATIONS
340UINT32
341AcpiDbGetCacheInfo (
342    ACPI_MEMORY_LIST        *Cache)
343{
344
345    return (Cache->TotalAllocated - Cache->TotalFreed - Cache->CurrentDepth);
346}
347#endif
348
349/*******************************************************************************
350 *
351 * FUNCTION:    AcpiDbGetOutstandingAllocations
352 *
353 * PARAMETERS:  None
354 *
355 * RETURN:      Current global allocation count minus cache entries
356 *
357 * DESCRIPTION: Determine the current number of "outstanding" allocations --
358 *              those allocations that have not been freed and also are not
359 *              in one of the various object caches.
360 *
361 ******************************************************************************/
362
363static UINT32
364AcpiDbGetOutstandingAllocations (
365    void)
366{
367    UINT32                  Outstanding = 0;
368
369#ifdef ACPI_DBG_TRACK_ALLOCATIONS
370
371    Outstanding += AcpiDbGetCacheInfo (AcpiGbl_StateCache);
372    Outstanding += AcpiDbGetCacheInfo (AcpiGbl_PsNodeCache);
373    Outstanding += AcpiDbGetCacheInfo (AcpiGbl_PsNodeExtCache);
374    Outstanding += AcpiDbGetCacheInfo (AcpiGbl_OperandCache);
375#endif
376
377    return (Outstanding);
378}
379
380
381/*******************************************************************************
382 *
383 * FUNCTION:    AcpiDbExecutionWalk
384 *
385 * PARAMETERS:  WALK_CALLBACK
386 *
387 * RETURN:      Status
388 *
389 * DESCRIPTION: Execute a control method.  Name is relative to the current
390 *              scope.
391 *
392 ******************************************************************************/
393
394static ACPI_STATUS
395AcpiDbExecutionWalk (
396    ACPI_HANDLE             ObjHandle,
397    UINT32                  NestingLevel,
398    void                    *Context,
399    void                    **ReturnValue)
400{
401    ACPI_OPERAND_OBJECT     *ObjDesc;
402    ACPI_NAMESPACE_NODE     *Node = (ACPI_NAMESPACE_NODE *) ObjHandle;
403    ACPI_BUFFER             ReturnObj;
404    ACPI_STATUS             Status;
405
406
407    ObjDesc = AcpiNsGetAttachedObject (Node);
408    if (ObjDesc->Method.ParamCount)
409    {
410        return (AE_OK);
411    }
412
413    ReturnObj.Pointer = NULL;
414    ReturnObj.Length = ACPI_ALLOCATE_BUFFER;
415
416    AcpiNsPrintNodePathname (Node, "Execute");
417
418    /* Do the actual method execution */
419
420    AcpiOsPrintf ("\n");
421    AcpiGbl_MethodExecuting = TRUE;
422
423    Status = AcpiEvaluateObject (Node, NULL, NULL, &ReturnObj);
424
425    AcpiOsPrintf ("[%4.4s] returned %s\n", AcpiUtGetNodeName (Node),
426            AcpiFormatException (Status));
427    AcpiGbl_MethodExecuting = FALSE;
428
429    return (AE_OK);
430}
431
432
433/*******************************************************************************
434 *
435 * FUNCTION:    AcpiDbExecute
436 *
437 * PARAMETERS:  Name                - Name of method to execute
438 *              Args                - Parameters to the method
439 *              Flags               - single step/no single step
440 *
441 * RETURN:      None
442 *
443 * DESCRIPTION: Execute a control method.  Name is relative to the current
444 *              scope.
445 *
446 ******************************************************************************/
447
448void
449AcpiDbExecute (
450    char                    *Name,
451    char                    **Args,
452    UINT32                  Flags)
453{
454    ACPI_STATUS             Status;
455    ACPI_BUFFER             ReturnObj;
456    char                    *NameString;
457
458
459#ifdef ACPI_DEBUG_OUTPUT
460    UINT32                  PreviousAllocations;
461    UINT32                  Allocations;
462
463
464    /* Memory allocation tracking */
465
466    PreviousAllocations = AcpiDbGetOutstandingAllocations ();
467#endif
468
469    if (*Name == '*')
470    {
471        (void) AcpiWalkNamespace (ACPI_TYPE_METHOD, ACPI_ROOT_OBJECT,
472                    ACPI_UINT32_MAX, AcpiDbExecutionWalk, NULL, NULL, NULL);
473        return;
474    }
475    else
476    {
477        NameString = ACPI_ALLOCATE (ACPI_STRLEN (Name) + 1);
478        if (!NameString)
479        {
480            return;
481        }
482
483        ACPI_MEMSET (&AcpiGbl_DbMethodInfo, 0, sizeof (ACPI_DB_METHOD_INFO));
484
485        ACPI_STRCPY (NameString, Name);
486        AcpiUtStrupr (NameString);
487        AcpiGbl_DbMethodInfo.Name = NameString;
488        AcpiGbl_DbMethodInfo.Args = Args;
489        AcpiGbl_DbMethodInfo.Flags = Flags;
490
491        ReturnObj.Pointer = NULL;
492        ReturnObj.Length = ACPI_ALLOCATE_BUFFER;
493
494        AcpiDbExecuteSetup (&AcpiGbl_DbMethodInfo);
495        Status = AcpiDbExecuteMethod (&AcpiGbl_DbMethodInfo, &ReturnObj);
496        ACPI_FREE (NameString);
497    }
498
499    /*
500     * Allow any handlers in separate threads to complete.
501     * (Such as Notify handlers invoked from AML executed above).
502     */
503    AcpiOsSleep ((UINT64) 10);
504
505
506#ifdef ACPI_DEBUG_OUTPUT
507
508    /* Memory allocation tracking */
509
510    Allocations = AcpiDbGetOutstandingAllocations () - PreviousAllocations;
511
512    AcpiDbSetOutputDestination (ACPI_DB_DUPLICATE_OUTPUT);
513
514    if (Allocations > 0)
515    {
516        AcpiOsPrintf ("Outstanding: 0x%X allocations after execution\n",
517                        Allocations);
518    }
519#endif
520
521    if (ACPI_FAILURE (Status))
522    {
523        AcpiOsPrintf ("Execution of %s failed with status %s\n",
524            AcpiGbl_DbMethodInfo.Pathname, AcpiFormatException (Status));
525    }
526    else
527    {
528        /* Display a return object, if any */
529
530        if (ReturnObj.Length)
531        {
532            AcpiOsPrintf ("Execution of %s returned object %p Buflen %X\n",
533                AcpiGbl_DbMethodInfo.Pathname, ReturnObj.Pointer,
534                (UINT32) ReturnObj.Length);
535            AcpiDbDumpExternalObject (ReturnObj.Pointer, 1);
536        }
537        else
538        {
539            AcpiOsPrintf ("No return object from execution of %s\n",
540                AcpiGbl_DbMethodInfo.Pathname);
541        }
542    }
543
544    AcpiDbSetOutputDestination (ACPI_DB_CONSOLE_OUTPUT);
545}
546
547
548/*******************************************************************************
549 *
550 * FUNCTION:    AcpiDbMethodThread
551 *
552 * PARAMETERS:  Context             - Execution info segment
553 *
554 * RETURN:      None
555 *
556 * DESCRIPTION: Debugger execute thread.  Waits for a command line, then
557 *              simply dispatches it.
558 *
559 ******************************************************************************/
560
561static void ACPI_SYSTEM_XFACE
562AcpiDbMethodThread (
563    void                    *Context)
564{
565    ACPI_STATUS             Status;
566    ACPI_DB_METHOD_INFO     *Info = Context;
567    ACPI_DB_METHOD_INFO     LocalInfo;
568    UINT32                  i;
569    UINT8                   Allow;
570    ACPI_BUFFER             ReturnObj;
571
572
573    /*
574     * AcpiGbl_DbMethodInfo.Arguments will be passed as method arguments.
575     * Prevent AcpiGbl_DbMethodInfo from being modified by multiple threads
576     * concurrently.
577     *
578     * Note: The arguments we are passing are used by the ASL test suite
579     * (aslts). Do not change them without updating the tests.
580     */
581    (void) AcpiOsWaitSemaphore (Info->InfoGate, 1, ACPI_WAIT_FOREVER);
582
583    if (Info->InitArgs)
584    {
585        AcpiDbUInt32ToHexString (Info->NumCreated, Info->IndexOfThreadStr);
586        AcpiDbUInt32ToHexString ((UINT32) AcpiOsGetThreadId (), Info->IdOfThreadStr);
587    }
588
589    if (Info->Threads && (Info->NumCreated < Info->NumThreads))
590    {
591        Info->Threads[Info->NumCreated++] = AcpiOsGetThreadId();
592    }
593
594    LocalInfo = *Info;
595    LocalInfo.Args = LocalInfo.Arguments;
596    LocalInfo.Arguments[0] = LocalInfo.NumThreadsStr;
597    LocalInfo.Arguments[1] = LocalInfo.IdOfThreadStr;
598    LocalInfo.Arguments[2] = LocalInfo.IndexOfThreadStr;
599    LocalInfo.Arguments[3] = NULL;
600
601    (void) AcpiOsSignalSemaphore (Info->InfoGate, 1);
602
603    for (i = 0; i < Info->NumLoops; i++)
604    {
605        Status = AcpiDbExecuteMethod (&LocalInfo, &ReturnObj);
606        if (ACPI_FAILURE (Status))
607        {
608            AcpiOsPrintf ("%s During execution of %s at iteration %X\n",
609                AcpiFormatException (Status), Info->Pathname, i);
610            if (Status == AE_ABORT_METHOD)
611            {
612                break;
613            }
614        }
615
616#if 0
617        if ((i % 100) == 0)
618        {
619            AcpiOsPrintf ("%u executions, Thread 0x%x\n", i, AcpiOsGetThreadId ());
620        }
621
622        if (ReturnObj.Length)
623        {
624            AcpiOsPrintf ("Execution of %s returned object %p Buflen %X\n",
625                Info->Pathname, ReturnObj.Pointer, (UINT32) ReturnObj.Length);
626            AcpiDbDumpExternalObject (ReturnObj.Pointer, 1);
627        }
628#endif
629    }
630
631    /* Signal our completion */
632
633    Allow = 0;
634    (void) AcpiOsWaitSemaphore (Info->ThreadCompleteGate, 1, ACPI_WAIT_FOREVER);
635    Info->NumCompleted++;
636
637    if (Info->NumCompleted == Info->NumThreads)
638    {
639        /* Do signal for main thread once only */
640        Allow = 1;
641    }
642
643    (void) AcpiOsSignalSemaphore (Info->ThreadCompleteGate, 1);
644
645    if (Allow)
646    {
647        Status = AcpiOsSignalSemaphore (Info->MainThreadGate, 1);
648        if (ACPI_FAILURE (Status))
649        {
650            AcpiOsPrintf ("Could not signal debugger thread sync semaphore, %s\n",
651                AcpiFormatException (Status));
652        }
653    }
654}
655
656
657/*******************************************************************************
658 *
659 * FUNCTION:    AcpiDbCreateExecutionThreads
660 *
661 * PARAMETERS:  NumThreadsArg           - Number of threads to create
662 *              NumLoopsArg             - Loop count for the thread(s)
663 *              MethodNameArg           - Control method to execute
664 *
665 * RETURN:      None
666 *
667 * DESCRIPTION: Create threads to execute method(s)
668 *
669 ******************************************************************************/
670
671void
672AcpiDbCreateExecutionThreads (
673    char                    *NumThreadsArg,
674    char                    *NumLoopsArg,
675    char                    *MethodNameArg)
676{
677    ACPI_STATUS             Status;
678    UINT32                  NumThreads;
679    UINT32                  NumLoops;
680    UINT32                  i;
681    UINT32                  Size;
682    ACPI_MUTEX              MainThreadGate;
683    ACPI_MUTEX              ThreadCompleteGate;
684    ACPI_MUTEX              InfoGate;
685
686
687    /* Get the arguments */
688
689    NumThreads = ACPI_STRTOUL (NumThreadsArg, NULL, 0);
690    NumLoops   = ACPI_STRTOUL (NumLoopsArg, NULL, 0);
691
692    if (!NumThreads || !NumLoops)
693    {
694        AcpiOsPrintf ("Bad argument: Threads %X, Loops %X\n",
695            NumThreads, NumLoops);
696        return;
697    }
698
699    /*
700     * Create the semaphore for synchronization of
701     * the created threads with the main thread.
702     */
703    Status = AcpiOsCreateSemaphore (1, 0, &MainThreadGate);
704    if (ACPI_FAILURE (Status))
705    {
706        AcpiOsPrintf ("Could not create semaphore for synchronization with the main thread, %s\n",
707            AcpiFormatException (Status));
708        return;
709    }
710
711    /*
712     * Create the semaphore for synchronization
713     * between the created threads.
714     */
715    Status = AcpiOsCreateSemaphore (1, 1, &ThreadCompleteGate);
716    if (ACPI_FAILURE (Status))
717    {
718        AcpiOsPrintf ("Could not create semaphore for synchronization between the created threads, %s\n",
719            AcpiFormatException (Status));
720        (void) AcpiOsDeleteSemaphore (MainThreadGate);
721        return;
722    }
723
724    Status = AcpiOsCreateSemaphore (1, 1, &InfoGate);
725    if (ACPI_FAILURE (Status))
726    {
727        AcpiOsPrintf ("Could not create semaphore for synchronization of AcpiGbl_DbMethodInfo, %s\n",
728            AcpiFormatException (Status));
729        (void) AcpiOsDeleteSemaphore (ThreadCompleteGate);
730        (void) AcpiOsDeleteSemaphore (MainThreadGate);
731        return;
732    }
733
734    ACPI_MEMSET (&AcpiGbl_DbMethodInfo, 0, sizeof (ACPI_DB_METHOD_INFO));
735
736    /* Array to store IDs of threads */
737
738    AcpiGbl_DbMethodInfo.NumThreads = NumThreads;
739    Size = sizeof (ACPI_THREAD_ID) * AcpiGbl_DbMethodInfo.NumThreads;
740    AcpiGbl_DbMethodInfo.Threads = AcpiOsAllocate (Size);
741    if (AcpiGbl_DbMethodInfo.Threads == NULL)
742    {
743        AcpiOsPrintf ("No memory for thread IDs array\n");
744        (void) AcpiOsDeleteSemaphore (MainThreadGate);
745        (void) AcpiOsDeleteSemaphore (ThreadCompleteGate);
746        (void) AcpiOsDeleteSemaphore (InfoGate);
747        return;
748    }
749    ACPI_MEMSET (AcpiGbl_DbMethodInfo.Threads, 0, Size);
750
751    /* Setup the context to be passed to each thread */
752
753    AcpiGbl_DbMethodInfo.Name = MethodNameArg;
754    AcpiGbl_DbMethodInfo.Flags = 0;
755    AcpiGbl_DbMethodInfo.NumLoops = NumLoops;
756    AcpiGbl_DbMethodInfo.MainThreadGate = MainThreadGate;
757    AcpiGbl_DbMethodInfo.ThreadCompleteGate = ThreadCompleteGate;
758    AcpiGbl_DbMethodInfo.InfoGate = InfoGate;
759
760    /* Init arguments to be passed to method */
761
762    AcpiGbl_DbMethodInfo.InitArgs = 1;
763    AcpiGbl_DbMethodInfo.Args = AcpiGbl_DbMethodInfo.Arguments;
764    AcpiGbl_DbMethodInfo.Arguments[0] = AcpiGbl_DbMethodInfo.NumThreadsStr;
765    AcpiGbl_DbMethodInfo.Arguments[1] = AcpiGbl_DbMethodInfo.IdOfThreadStr;
766    AcpiGbl_DbMethodInfo.Arguments[2] = AcpiGbl_DbMethodInfo.IndexOfThreadStr;
767    AcpiGbl_DbMethodInfo.Arguments[3] = NULL;
768    AcpiDbUInt32ToHexString (NumThreads, AcpiGbl_DbMethodInfo.NumThreadsStr);
769
770    AcpiDbExecuteSetup (&AcpiGbl_DbMethodInfo);
771
772    /* Create the threads */
773
774    AcpiOsPrintf ("Creating %X threads to execute %X times each\n",
775        NumThreads, NumLoops);
776
777    for (i = 0; i < (NumThreads); i++)
778    {
779        Status = AcpiOsExecute (OSL_DEBUGGER_THREAD, AcpiDbMethodThread,
780            &AcpiGbl_DbMethodInfo);
781        if (ACPI_FAILURE (Status))
782        {
783            break;
784        }
785    }
786
787    /* Wait for all threads to complete */
788
789    (void) AcpiOsWaitSemaphore (MainThreadGate, 1, ACPI_WAIT_FOREVER);
790
791    AcpiDbSetOutputDestination (ACPI_DB_DUPLICATE_OUTPUT);
792    AcpiOsPrintf ("All threads (%X) have completed\n", NumThreads);
793    AcpiDbSetOutputDestination (ACPI_DB_CONSOLE_OUTPUT);
794
795    /* Cleanup and exit */
796
797    (void) AcpiOsDeleteSemaphore (MainThreadGate);
798    (void) AcpiOsDeleteSemaphore (ThreadCompleteGate);
799    (void) AcpiOsDeleteSemaphore (InfoGate);
800
801    AcpiOsFree (AcpiGbl_DbMethodInfo.Threads);
802    AcpiGbl_DbMethodInfo.Threads = NULL;
803}
804
805#endif /* ACPI_DEBUGGER */
806
807
808