1 /*******************************************************************************
2  *
3  * Module Name: dbexec - debugger control method execution
4  *
5  ******************************************************************************/
6 
7 /*
8  * Copyright (C) 2000 - 2015, Intel Corp.
9  * All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions, and the following disclaimer,
16  *    without modification.
17  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
18  *    substantially similar to the "NO WARRANTY" disclaimer below
19  *    ("Disclaimer") and any redistribution must be conditioned upon
20  *    including a substantially similar Disclaimer requirement for further
21  *    binary redistribution.
22  * 3. Neither the names of the above-listed copyright holders nor the names
23  *    of any contributors may be used to endorse or promote products derived
24  *    from this software without specific prior written permission.
25  *
26  * Alternatively, this software may be distributed under the terms of the
27  * GNU General Public License ("GPL") version 2 as published by the Free
28  * Software Foundation.
29  *
30  * NO WARRANTY
31  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
34  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
40  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41  * POSSIBILITY OF SUCH DAMAGES.
42  */
43 
44 #include "acpi.h"
45 #include "accommon.h"
46 #include "acdebug.h"
47 #include "acnamesp.h"
48 
49 #ifdef ACPI_DEBUGGER
50 
51 #define _COMPONENT          ACPI_CA_DEBUGGER
52         ACPI_MODULE_NAME    ("dbexec")
53 
54 
55 static ACPI_DB_METHOD_INFO          AcpiGbl_DbMethodInfo;
56 
57 /* Local prototypes */
58 
59 static ACPI_STATUS
60 AcpiDbExecuteMethod (
61     ACPI_DB_METHOD_INFO     *Info,
62     ACPI_BUFFER             *ReturnObj);
63 
64 static ACPI_STATUS
65 AcpiDbExecuteSetup (
66     ACPI_DB_METHOD_INFO     *Info);
67 
68 static UINT32
69 AcpiDbGetOutstandingAllocations (
70     void);
71 
72 static void ACPI_SYSTEM_XFACE
73 AcpiDbMethodThread (
74     void                    *Context);
75 
76 static ACPI_STATUS
77 AcpiDbExecutionWalk (
78     ACPI_HANDLE             ObjHandle,
79     UINT32                  NestingLevel,
80     void                    *Context,
81     void                    **ReturnValue);
82 
83 
84 /*******************************************************************************
85  *
86  * FUNCTION:    AcpiDbDeleteObjects
87  *
88  * PARAMETERS:  Count               - Count of objects in the list
89  *              Objects             - Array of ACPI_OBJECTs to be deleted
90  *
91  * RETURN:      None
92  *
93  * DESCRIPTION: Delete a list of ACPI_OBJECTS. Handles packages and nested
94  *              packages via recursion.
95  *
96  ******************************************************************************/
97 
98 void
99 AcpiDbDeleteObjects (
100     UINT32                  Count,
101     ACPI_OBJECT             *Objects)
102 {
103     UINT32                  i;
104 
105 
106     for (i = 0; i < Count; i++)
107     {
108         switch (Objects[i].Type)
109         {
110         case ACPI_TYPE_BUFFER:
111 
112             ACPI_FREE (Objects[i].Buffer.Pointer);
113             break;
114 
115         case ACPI_TYPE_PACKAGE:
116 
117             /* Recursive call to delete package elements */
118 
119             AcpiDbDeleteObjects (Objects[i].Package.Count,
120                 Objects[i].Package.Elements);
121 
122             /* Free the elements array */
123 
124             ACPI_FREE (Objects[i].Package.Elements);
125             break;
126 
127         default:
128 
129             break;
130         }
131     }
132 }
133 
134 
135 /*******************************************************************************
136  *
137  * FUNCTION:    AcpiDbExecuteMethod
138  *
139  * PARAMETERS:  Info            - Valid info segment
140  *              ReturnObj       - Where to put return object
141  *
142  * RETURN:      Status
143  *
144  * DESCRIPTION: Execute a control method.
145  *
146  ******************************************************************************/
147 
148 static ACPI_STATUS
149 AcpiDbExecuteMethod (
150     ACPI_DB_METHOD_INFO     *Info,
151     ACPI_BUFFER             *ReturnObj)
152 {
153     ACPI_STATUS             Status;
154     ACPI_OBJECT_LIST        ParamObjects;
155     ACPI_OBJECT             Params[ACPI_DEBUGGER_MAX_ARGS + 1];
156     UINT32                  i;
157 
158 
159     ACPI_FUNCTION_TRACE (DbExecuteMethod);
160 
161 
162     if (AcpiGbl_DbOutputToFile && !AcpiDbgLevel)
163     {
164         AcpiOsPrintf ("Warning: debug output is not enabled!\n");
165     }
166 
167     ParamObjects.Count = 0;
168     ParamObjects.Pointer = NULL;
169 
170     /* Pass through any command-line arguments */
171 
172     if (Info->Args && Info->Args[0])
173     {
174         /* Get arguments passed on the command line */
175 
176         for (i = 0; (Info->Args[i] && *(Info->Args[i])); i++)
177         {
178             /* Convert input string (token) to an actual ACPI_OBJECT */
179 
180             Status = AcpiDbConvertToObject (Info->Types[i],
181                 Info->Args[i], &Params[i]);
182             if (ACPI_FAILURE (Status))
183             {
184                 ACPI_EXCEPTION ((AE_INFO, Status,
185                     "While parsing method arguments"));
186                 goto Cleanup;
187             }
188         }
189 
190         ParamObjects.Count = i;
191         ParamObjects.Pointer = Params;
192     }
193 
194     /* Prepare for a return object of arbitrary size */
195 
196     ReturnObj->Pointer = AcpiGbl_DbBuffer;
197     ReturnObj->Length  = ACPI_DEBUG_BUFFER_SIZE;
198 
199     /* Do the actual method execution */
200 
201     AcpiGbl_MethodExecuting = TRUE;
202     Status = AcpiEvaluateObject (NULL, Info->Pathname,
203         &ParamObjects, ReturnObj);
204 
205     AcpiGbl_CmSingleStep = FALSE;
206     AcpiGbl_MethodExecuting = FALSE;
207 
208     if (ACPI_FAILURE (Status))
209     {
210         ACPI_EXCEPTION ((AE_INFO, Status,
211             "while executing %s from debugger", Info->Pathname));
212 
213         if (Status == AE_BUFFER_OVERFLOW)
214         {
215             ACPI_ERROR ((AE_INFO,
216                 "Possible overflow of internal debugger "
217                 "buffer (size 0x%X needed 0x%X)",
218                 ACPI_DEBUG_BUFFER_SIZE, (UINT32) ReturnObj->Length));
219         }
220     }
221 
222 Cleanup:
223     AcpiDbDeleteObjects (ParamObjects.Count, Params);
224     return_ACPI_STATUS (Status);
225 }
226 
227 
228 /*******************************************************************************
229  *
230  * FUNCTION:    AcpiDbExecuteSetup
231  *
232  * PARAMETERS:  Info            - Valid method info
233  *
234  * RETURN:      None
235  *
236  * DESCRIPTION: Setup info segment prior to method execution
237  *
238  ******************************************************************************/
239 
240 static ACPI_STATUS
241 AcpiDbExecuteSetup (
242     ACPI_DB_METHOD_INFO     *Info)
243 {
244     ACPI_STATUS             Status;
245 
246 
247     ACPI_FUNCTION_NAME (DbExecuteSetup);
248 
249 
250     /* Catenate the current scope to the supplied name */
251 
252     Info->Pathname[0] = 0;
253     if ((Info->Name[0] != '\\') &&
254         (Info->Name[0] != '/'))
255     {
256         if (AcpiUtSafeStrcat (Info->Pathname, sizeof (Info->Pathname),
257             AcpiGbl_DbScopeBuf))
258         {
259             Status = AE_BUFFER_OVERFLOW;
260             goto ErrorExit;
261         }
262     }
263 
264     if (AcpiUtSafeStrcat (Info->Pathname, sizeof (Info->Pathname),
265         Info->Name))
266     {
267         Status = AE_BUFFER_OVERFLOW;
268         goto ErrorExit;
269     }
270 
271     AcpiDbPrepNamestring (Info->Pathname);
272 
273     AcpiDbSetOutputDestination (ACPI_DB_DUPLICATE_OUTPUT);
274     AcpiOsPrintf ("Evaluating %s\n", Info->Pathname);
275 
276     if (Info->Flags & EX_SINGLE_STEP)
277     {
278         AcpiGbl_CmSingleStep = TRUE;
279         AcpiDbSetOutputDestination (ACPI_DB_CONSOLE_OUTPUT);
280     }
281 
282     else
283     {
284         /* No single step, allow redirection to a file */
285 
286         AcpiDbSetOutputDestination (ACPI_DB_REDIRECTABLE_OUTPUT);
287     }
288 
289     return (AE_OK);
290 
291 ErrorExit:
292 
293     ACPI_EXCEPTION ((AE_INFO, Status, "During setup for method execution"));
294     return (Status);
295 }
296 
297 
298 #ifdef ACPI_DBG_TRACK_ALLOCATIONS
299 UINT32
300 AcpiDbGetCacheInfo (
301     ACPI_MEMORY_LIST        *Cache)
302 {
303 
304     return (Cache->TotalAllocated - Cache->TotalFreed - Cache->CurrentDepth);
305 }
306 #endif
307 
308 /*******************************************************************************
309  *
310  * FUNCTION:    AcpiDbGetOutstandingAllocations
311  *
312  * PARAMETERS:  None
313  *
314  * RETURN:      Current global allocation count minus cache entries
315  *
316  * DESCRIPTION: Determine the current number of "outstanding" allocations --
317  *              those allocations that have not been freed and also are not
318  *              in one of the various object caches.
319  *
320  ******************************************************************************/
321 
322 static UINT32
323 AcpiDbGetOutstandingAllocations (
324     void)
325 {
326     UINT32                  Outstanding = 0;
327 
328 #ifdef ACPI_DBG_TRACK_ALLOCATIONS
329 
330     Outstanding += AcpiDbGetCacheInfo (AcpiGbl_StateCache);
331     Outstanding += AcpiDbGetCacheInfo (AcpiGbl_PsNodeCache);
332     Outstanding += AcpiDbGetCacheInfo (AcpiGbl_PsNodeExtCache);
333     Outstanding += AcpiDbGetCacheInfo (AcpiGbl_OperandCache);
334 #endif
335 
336     return (Outstanding);
337 }
338 
339 
340 /*******************************************************************************
341  *
342  * FUNCTION:    AcpiDbExecutionWalk
343  *
344  * PARAMETERS:  WALK_CALLBACK
345  *
346  * RETURN:      Status
347  *
348  * DESCRIPTION: Execute a control method. Name is relative to the current
349  *              scope.
350  *
351  ******************************************************************************/
352 
353 static ACPI_STATUS
354 AcpiDbExecutionWalk (
355     ACPI_HANDLE             ObjHandle,
356     UINT32                  NestingLevel,
357     void                    *Context,
358     void                    **ReturnValue)
359 {
360     ACPI_OPERAND_OBJECT     *ObjDesc;
361     ACPI_NAMESPACE_NODE     *Node = (ACPI_NAMESPACE_NODE *) ObjHandle;
362     ACPI_BUFFER             ReturnObj;
363     ACPI_STATUS             Status;
364 
365 
366     ObjDesc = AcpiNsGetAttachedObject (Node);
367     if (ObjDesc->Method.ParamCount)
368     {
369         return (AE_OK);
370     }
371 
372     ReturnObj.Pointer = NULL;
373     ReturnObj.Length = ACPI_ALLOCATE_BUFFER;
374 
375     AcpiNsPrintNodePathname (Node, "Evaluating");
376 
377     /* Do the actual method execution */
378 
379     AcpiOsPrintf ("\n");
380     AcpiGbl_MethodExecuting = TRUE;
381 
382     Status = AcpiEvaluateObject (Node, NULL, NULL, &ReturnObj);
383 
384     AcpiOsPrintf ("Evaluation of [%4.4s] returned %s\n",
385         AcpiUtGetNodeName (Node),
386         AcpiFormatException (Status));
387 
388     AcpiGbl_MethodExecuting = FALSE;
389     return (AE_OK);
390 }
391 
392 
393 /*******************************************************************************
394  *
395  * FUNCTION:    AcpiDbExecute
396  *
397  * PARAMETERS:  Name                - Name of method to execute
398  *              Args                - Parameters to the method
399  *              Types               -
400  *              Flags               - single step/no single step
401  *
402  * RETURN:      None
403  *
404  * DESCRIPTION: Execute a control method. Name is relative to the current
405  *              scope.
406  *
407  ******************************************************************************/
408 
409 void
410 AcpiDbExecute (
411     char                    *Name,
412     char                    **Args,
413     ACPI_OBJECT_TYPE        *Types,
414     UINT32                  Flags)
415 {
416     ACPI_STATUS             Status;
417     ACPI_BUFFER             ReturnObj;
418     char                    *NameString;
419 
420 #ifdef ACPI_DEBUG_OUTPUT
421     UINT32                  PreviousAllocations;
422     UINT32                  Allocations;
423 #endif
424 
425 
426     /*
427      * Allow one execution to be performed by debugger or single step
428      * execution will be dead locked by the interpreter mutexes.
429      */
430     if (AcpiGbl_MethodExecuting)
431     {
432         AcpiOsPrintf ("Only one debugger execution is allowed.\n");
433         return;
434     }
435 
436 #ifdef ACPI_DEBUG_OUTPUT
437     /* Memory allocation tracking */
438 
439     PreviousAllocations = AcpiDbGetOutstandingAllocations ();
440 #endif
441 
442     if (*Name == '*')
443     {
444         (void) AcpiWalkNamespace (ACPI_TYPE_METHOD, ACPI_ROOT_OBJECT,
445             ACPI_UINT32_MAX, AcpiDbExecutionWalk, NULL, NULL, NULL);
446         return;
447     }
448     else
449     {
450         NameString = ACPI_ALLOCATE (strlen (Name) + 1);
451         if (!NameString)
452         {
453             return;
454         }
455 
456         memset (&AcpiGbl_DbMethodInfo, 0, sizeof (ACPI_DB_METHOD_INFO));
457 
458         strcpy (NameString, Name);
459         AcpiUtStrupr (NameString);
460         AcpiGbl_DbMethodInfo.Name = NameString;
461         AcpiGbl_DbMethodInfo.Args = Args;
462         AcpiGbl_DbMethodInfo.Types = Types;
463         AcpiGbl_DbMethodInfo.Flags = Flags;
464 
465         ReturnObj.Pointer = NULL;
466         ReturnObj.Length = ACPI_ALLOCATE_BUFFER;
467 
468         Status = AcpiDbExecuteSetup (&AcpiGbl_DbMethodInfo);
469         if (ACPI_FAILURE (Status))
470         {
471             ACPI_FREE (NameString);
472             return;
473         }
474 
475         /* Get the NS node, determines existence also */
476 
477         Status = AcpiGetHandle (NULL, AcpiGbl_DbMethodInfo.Pathname,
478             &AcpiGbl_DbMethodInfo.Method);
479         if (ACPI_SUCCESS (Status))
480         {
481             Status = AcpiDbExecuteMethod (&AcpiGbl_DbMethodInfo,
482                 &ReturnObj);
483         }
484         ACPI_FREE (NameString);
485     }
486 
487     /*
488      * Allow any handlers in separate threads to complete.
489      * (Such as Notify handlers invoked from AML executed above).
490      */
491     AcpiOsSleep ((UINT64) 10);
492 
493 #ifdef ACPI_DEBUG_OUTPUT
494 
495     /* Memory allocation tracking */
496 
497     Allocations = AcpiDbGetOutstandingAllocations () - PreviousAllocations;
498 
499     AcpiDbSetOutputDestination (ACPI_DB_DUPLICATE_OUTPUT);
500 
501     if (Allocations > 0)
502     {
503         AcpiOsPrintf (
504             "0x%X Outstanding allocations after evaluation of %s\n",
505             Allocations, AcpiGbl_DbMethodInfo.Pathname);
506     }
507 #endif
508 
509     if (ACPI_FAILURE (Status))
510     {
511         AcpiOsPrintf ("Evaluation of %s failed with status %s\n",
512             AcpiGbl_DbMethodInfo.Pathname,
513             AcpiFormatException (Status));
514     }
515     else
516     {
517         /* Display a return object, if any */
518 
519         if (ReturnObj.Length)
520         {
521             AcpiOsPrintf (
522                 "Evaluation of %s returned object %p, "
523                 "external buffer length %X\n",
524                 AcpiGbl_DbMethodInfo.Pathname, ReturnObj.Pointer,
525                 (UINT32) ReturnObj.Length);
526 
527             AcpiDbDumpExternalObject (ReturnObj.Pointer, 1);
528 
529             /* Dump a _PLD buffer if present */
530 
531             if (ACPI_COMPARE_NAME ((ACPI_CAST_PTR (ACPI_NAMESPACE_NODE,
532                     AcpiGbl_DbMethodInfo.Method)->Name.Ascii),
533                     METHOD_NAME__PLD))
534             {
535                 AcpiDbDumpPldBuffer (ReturnObj.Pointer);
536             }
537         }
538         else
539         {
540             AcpiOsPrintf ("No object was returned from evaluation of %s\n",
541                 AcpiGbl_DbMethodInfo.Pathname);
542         }
543     }
544 
545     AcpiDbSetOutputDestination (ACPI_DB_CONSOLE_OUTPUT);
546 }
547 
548 
549 /*******************************************************************************
550  *
551  * FUNCTION:    AcpiDbMethodThread
552  *
553  * PARAMETERS:  Context             - Execution info segment
554  *
555  * RETURN:      None
556  *
557  * DESCRIPTION: Debugger execute thread. Waits for a command line, then
558  *              simply dispatches it.
559  *
560  ******************************************************************************/
561 
562 static void ACPI_SYSTEM_XFACE
563 AcpiDbMethodThread (
564     void                    *Context)
565 {
566     ACPI_STATUS             Status;
567     ACPI_DB_METHOD_INFO     *Info = Context;
568     ACPI_DB_METHOD_INFO     LocalInfo;
569     UINT32                  i;
570     UINT8                   Allow;
571     ACPI_BUFFER             ReturnObj;
572 
573 
574     /*
575      * AcpiGbl_DbMethodInfo.Arguments will be passed as method arguments.
576      * Prevent AcpiGbl_DbMethodInfo from being modified by multiple threads
577      * concurrently.
578      *
579      * Note: The arguments we are passing are used by the ASL test suite
580      * (aslts). Do not change them without updating the tests.
581      */
582     (void) AcpiOsWaitSemaphore (Info->InfoGate, 1, ACPI_WAIT_FOREVER);
583 
584     if (Info->InitArgs)
585     {
586         AcpiDbUint32ToHexString (Info->NumCreated,
587             Info->IndexOfThreadStr);
588         AcpiDbUint32ToHexString ((UINT32) AcpiOsGetThreadId (),
589             Info->IdOfThreadStr);
590     }
591 
592     if (Info->Threads && (Info->NumCreated < Info->NumThreads))
593     {
594         Info->Threads[Info->NumCreated++] = AcpiOsGetThreadId();
595     }
596 
597     LocalInfo = *Info;
598     LocalInfo.Args = LocalInfo.Arguments;
599     LocalInfo.Arguments[0] = LocalInfo.NumThreadsStr;
600     LocalInfo.Arguments[1] = LocalInfo.IdOfThreadStr;
601     LocalInfo.Arguments[2] = LocalInfo.IndexOfThreadStr;
602     LocalInfo.Arguments[3] = NULL;
603 
604     LocalInfo.Types = LocalInfo.ArgTypes;
605 
606     (void) AcpiOsSignalSemaphore (Info->InfoGate, 1);
607 
608     for (i = 0; i < Info->NumLoops; i++)
609     {
610         Status = AcpiDbExecuteMethod (&LocalInfo, &ReturnObj);
611         if (ACPI_FAILURE (Status))
612         {
613             AcpiOsPrintf ("%s During evaluation of %s at iteration %X\n",
614                 AcpiFormatException (Status), Info->Pathname, i);
615             if (Status == AE_ABORT_METHOD)
616             {
617                 break;
618             }
619         }
620 
621 #if 0
622         if ((i % 100) == 0)
623         {
624             AcpiOsPrintf ("%u loops, Thread 0x%x\n",
625                 i, AcpiOsGetThreadId ());
626         }
627 
628         if (ReturnObj.Length)
629         {
630             AcpiOsPrintf ("Evaluation of %s returned object %p Buflen %X\n",
631                 Info->Pathname, ReturnObj.Pointer, (UINT32) ReturnObj.Length);
632             AcpiDbDumpExternalObject (ReturnObj.Pointer, 1);
633         }
634 #endif
635     }
636 
637     /* Signal our completion */
638 
639     Allow = 0;
640     (void) AcpiOsWaitSemaphore (Info->ThreadCompleteGate,
641         1, ACPI_WAIT_FOREVER);
642     Info->NumCompleted++;
643 
644     if (Info->NumCompleted == Info->NumThreads)
645     {
646         /* Do signal for main thread once only */
647         Allow = 1;
648     }
649 
650     (void) AcpiOsSignalSemaphore (Info->ThreadCompleteGate, 1);
651 
652     if (Allow)
653     {
654         Status = AcpiOsSignalSemaphore (Info->MainThreadGate, 1);
655         if (ACPI_FAILURE (Status))
656         {
657             AcpiOsPrintf (
658                 "Could not signal debugger thread sync semaphore, %s\n",
659                 AcpiFormatException (Status));
660         }
661     }
662 }
663 
664 
665 /*******************************************************************************
666  *
667  * FUNCTION:    AcpiDbCreateExecutionThreads
668  *
669  * PARAMETERS:  NumThreadsArg           - Number of threads to create
670  *              NumLoopsArg             - Loop count for the thread(s)
671  *              MethodNameArg           - Control method to execute
672  *
673  * RETURN:      None
674  *
675  * DESCRIPTION: Create threads to execute method(s)
676  *
677  ******************************************************************************/
678 
679 void
680 AcpiDbCreateExecutionThreads (
681     char                    *NumThreadsArg,
682     char                    *NumLoopsArg,
683     char                    *MethodNameArg)
684 {
685     ACPI_STATUS             Status;
686     UINT32                  NumThreads;
687     UINT32                  NumLoops;
688     UINT32                  i;
689     UINT32                  Size;
690     ACPI_MUTEX              MainThreadGate;
691     ACPI_MUTEX              ThreadCompleteGate;
692     ACPI_MUTEX              InfoGate;
693 
694 
695     /* Get the arguments */
696 
697     NumThreads = strtoul (NumThreadsArg, NULL, 0);
698     NumLoops   = strtoul (NumLoopsArg, NULL, 0);
699 
700     if (!NumThreads || !NumLoops)
701     {
702         AcpiOsPrintf ("Bad argument: Threads %X, Loops %X\n",
703             NumThreads, NumLoops);
704         return;
705     }
706 
707     /*
708      * Create the semaphore for synchronization of
709      * the created threads with the main thread.
710      */
711     Status = AcpiOsCreateSemaphore (1, 0, &MainThreadGate);
712     if (ACPI_FAILURE (Status))
713     {
714         AcpiOsPrintf ("Could not create semaphore for "
715             "synchronization with the main thread, %s\n",
716             AcpiFormatException (Status));
717         return;
718     }
719 
720     /*
721      * Create the semaphore for synchronization
722      * between the created threads.
723      */
724     Status = AcpiOsCreateSemaphore (1, 1, &ThreadCompleteGate);
725     if (ACPI_FAILURE (Status))
726     {
727         AcpiOsPrintf ("Could not create semaphore for "
728             "synchronization between the created threads, %s\n",
729             AcpiFormatException (Status));
730 
731         (void) AcpiOsDeleteSemaphore (MainThreadGate);
732         return;
733     }
734 
735     Status = AcpiOsCreateSemaphore (1, 1, &InfoGate);
736     if (ACPI_FAILURE (Status))
737     {
738         AcpiOsPrintf ("Could not create semaphore for "
739             "synchronization of AcpiGbl_DbMethodInfo, %s\n",
740             AcpiFormatException (Status));
741 
742         (void) AcpiOsDeleteSemaphore (ThreadCompleteGate);
743         (void) AcpiOsDeleteSemaphore (MainThreadGate);
744         return;
745     }
746 
747     memset (&AcpiGbl_DbMethodInfo, 0, sizeof (ACPI_DB_METHOD_INFO));
748 
749     /* Array to store IDs of threads */
750 
751     AcpiGbl_DbMethodInfo.NumThreads = NumThreads;
752     Size = sizeof (ACPI_THREAD_ID) * AcpiGbl_DbMethodInfo.NumThreads;
753 
754     AcpiGbl_DbMethodInfo.Threads = AcpiOsAllocate (Size);
755     if (AcpiGbl_DbMethodInfo.Threads == NULL)
756     {
757         AcpiOsPrintf ("No memory for thread IDs array\n");
758         (void) AcpiOsDeleteSemaphore (MainThreadGate);
759         (void) AcpiOsDeleteSemaphore (ThreadCompleteGate);
760         (void) AcpiOsDeleteSemaphore (InfoGate);
761         return;
762     }
763     memset (AcpiGbl_DbMethodInfo.Threads, 0, Size);
764 
765     /* Setup the context to be passed to each thread */
766 
767     AcpiGbl_DbMethodInfo.Name = MethodNameArg;
768     AcpiGbl_DbMethodInfo.Flags = 0;
769     AcpiGbl_DbMethodInfo.NumLoops = NumLoops;
770     AcpiGbl_DbMethodInfo.MainThreadGate = MainThreadGate;
771     AcpiGbl_DbMethodInfo.ThreadCompleteGate = ThreadCompleteGate;
772     AcpiGbl_DbMethodInfo.InfoGate = InfoGate;
773 
774     /* Init arguments to be passed to method */
775 
776     AcpiGbl_DbMethodInfo.InitArgs = 1;
777     AcpiGbl_DbMethodInfo.Args = AcpiGbl_DbMethodInfo.Arguments;
778     AcpiGbl_DbMethodInfo.Arguments[0] = AcpiGbl_DbMethodInfo.NumThreadsStr;
779     AcpiGbl_DbMethodInfo.Arguments[1] = AcpiGbl_DbMethodInfo.IdOfThreadStr;
780     AcpiGbl_DbMethodInfo.Arguments[2] = AcpiGbl_DbMethodInfo.IndexOfThreadStr;
781     AcpiGbl_DbMethodInfo.Arguments[3] = NULL;
782 
783     AcpiGbl_DbMethodInfo.Types = AcpiGbl_DbMethodInfo.ArgTypes;
784     AcpiGbl_DbMethodInfo.ArgTypes[0] = ACPI_TYPE_INTEGER;
785     AcpiGbl_DbMethodInfo.ArgTypes[1] = ACPI_TYPE_INTEGER;
786     AcpiGbl_DbMethodInfo.ArgTypes[2] = ACPI_TYPE_INTEGER;
787 
788     AcpiDbUint32ToHexString (NumThreads, AcpiGbl_DbMethodInfo.NumThreadsStr);
789 
790     Status = AcpiDbExecuteSetup (&AcpiGbl_DbMethodInfo);
791     if (ACPI_FAILURE (Status))
792     {
793         goto CleanupAndExit;
794     }
795 
796     /* Get the NS node, determines existence also */
797 
798     Status = AcpiGetHandle (NULL, AcpiGbl_DbMethodInfo.Pathname,
799         &AcpiGbl_DbMethodInfo.Method);
800     if (ACPI_FAILURE (Status))
801     {
802         AcpiOsPrintf ("%s Could not get handle for %s\n",
803             AcpiFormatException (Status), AcpiGbl_DbMethodInfo.Pathname);
804         goto CleanupAndExit;
805     }
806 
807     /* Create the threads */
808 
809     AcpiOsPrintf ("Creating %X threads to execute %X times each\n",
810         NumThreads, NumLoops);
811 
812     for (i = 0; i < (NumThreads); i++)
813     {
814         Status = AcpiOsExecute (OSL_DEBUGGER_THREAD, AcpiDbMethodThread,
815             &AcpiGbl_DbMethodInfo);
816         if (ACPI_FAILURE (Status))
817         {
818             break;
819         }
820     }
821 
822     /* Wait for all threads to complete */
823 
824     (void) AcpiOsWaitSemaphore (MainThreadGate, 1, ACPI_WAIT_FOREVER);
825 
826     AcpiDbSetOutputDestination (ACPI_DB_DUPLICATE_OUTPUT);
827     AcpiOsPrintf ("All threads (%X) have completed\n", NumThreads);
828     AcpiDbSetOutputDestination (ACPI_DB_CONSOLE_OUTPUT);
829 
830 CleanupAndExit:
831 
832     /* Cleanup and exit */
833 
834     (void) AcpiOsDeleteSemaphore (MainThreadGate);
835     (void) AcpiOsDeleteSemaphore (ThreadCompleteGate);
836     (void) AcpiOsDeleteSemaphore (InfoGate);
837 
838     AcpiOsFree (AcpiGbl_DbMethodInfo.Threads);
839     AcpiGbl_DbMethodInfo.Threads = NULL;
840 }
841 
842 #endif /* ACPI_DEBUGGER */
843