xref: /freebsd/sys/contrib/dev/acpica/components/debugger/dbexec.c (revision 8ef24a0d4b28fe230e20637f56869cc4148cd2ca)
1 /*******************************************************************************
2  *
3  * Module Name: dbexec - debugger control method execution
4  *
5  ******************************************************************************/
6 
7 /*
8  * Copyright (C) 2000 - 2016, 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 <contrib/dev/acpica/include/acpi.h>
45 #include <contrib/dev/acpica/include/accommon.h>
46 #include <contrib/dev/acpica/include/acdebug.h>
47 #include <contrib/dev/acpica/include/acnamesp.h>
48 
49 
50 #define _COMPONENT          ACPI_CA_DEBUGGER
51         ACPI_MODULE_NAME    ("dbexec")
52 
53 
54 static ACPI_DB_METHOD_INFO          AcpiGbl_DbMethodInfo;
55 
56 /* Local prototypes */
57 
58 static ACPI_STATUS
59 AcpiDbExecuteMethod (
60     ACPI_DB_METHOD_INFO     *Info,
61     ACPI_BUFFER             *ReturnObj);
62 
63 static ACPI_STATUS
64 AcpiDbExecuteSetup (
65     ACPI_DB_METHOD_INFO     *Info);
66 
67 static UINT32
68 AcpiDbGetOutstandingAllocations (
69     void);
70 
71 static void ACPI_SYSTEM_XFACE
72 AcpiDbMethodThread (
73     void                    *Context);
74 
75 static ACPI_STATUS
76 AcpiDbExecutionWalk (
77     ACPI_HANDLE             ObjHandle,
78     UINT32                  NestingLevel,
79     void                    *Context,
80     void                    **ReturnValue);
81 
82 
83 /*******************************************************************************
84  *
85  * FUNCTION:    AcpiDbDeleteObjects
86  *
87  * PARAMETERS:  Count               - Count of objects in the list
88  *              Objects             - Array of ACPI_OBJECTs to be deleted
89  *
90  * RETURN:      None
91  *
92  * DESCRIPTION: Delete a list of ACPI_OBJECTS. Handles packages and nested
93  *              packages via recursion.
94  *
95  ******************************************************************************/
96 
97 void
98 AcpiDbDeleteObjects (
99     UINT32                  Count,
100     ACPI_OBJECT             *Objects)
101 {
102     UINT32                  i;
103 
104 
105     for (i = 0; i < Count; i++)
106     {
107         switch (Objects[i].Type)
108         {
109         case ACPI_TYPE_BUFFER:
110 
111             ACPI_FREE (Objects[i].Buffer.Pointer);
112             break;
113 
114         case ACPI_TYPE_PACKAGE:
115 
116             /* Recursive call to delete package elements */
117 
118             AcpiDbDeleteObjects (Objects[i].Package.Count,
119                 Objects[i].Package.Elements);
120 
121             /* Free the elements array */
122 
123             ACPI_FREE (Objects[i].Package.Elements);
124             break;
125 
126         default:
127 
128             break;
129         }
130     }
131 }
132 
133 
134 /*******************************************************************************
135  *
136  * FUNCTION:    AcpiDbExecuteMethod
137  *
138  * PARAMETERS:  Info            - Valid info segment
139  *              ReturnObj       - Where to put return object
140  *
141  * RETURN:      Status
142  *
143  * DESCRIPTION: Execute a control method.
144  *
145  ******************************************************************************/
146 
147 static ACPI_STATUS
148 AcpiDbExecuteMethod (
149     ACPI_DB_METHOD_INFO     *Info,
150     ACPI_BUFFER             *ReturnObj)
151 {
152     ACPI_STATUS             Status;
153     ACPI_OBJECT_LIST        ParamObjects;
154     ACPI_OBJECT             Params[ACPI_DEBUGGER_MAX_ARGS + 1];
155     UINT32                  i;
156 
157 
158     ACPI_FUNCTION_TRACE (DbExecuteMethod);
159 
160 
161     if (AcpiGbl_DbOutputToFile && !AcpiDbgLevel)
162     {
163         AcpiOsPrintf ("Warning: debug output is not enabled!\n");
164     }
165 
166     ParamObjects.Count = 0;
167     ParamObjects.Pointer = NULL;
168 
169     /* Pass through any command-line arguments */
170 
171     if (Info->Args && Info->Args[0])
172     {
173         /* Get arguments passed on the command line */
174 
175         for (i = 0; (Info->Args[i] && *(Info->Args[i])); i++)
176         {
177             /* Convert input string (token) to an actual ACPI_OBJECT */
178 
179             Status = AcpiDbConvertToObject (Info->Types[i],
180                 Info->Args[i], &Params[i]);
181             if (ACPI_FAILURE (Status))
182             {
183                 ACPI_EXCEPTION ((AE_INFO, Status,
184                     "While parsing method arguments"));
185                 goto Cleanup;
186             }
187         }
188 
189         ParamObjects.Count = i;
190         ParamObjects.Pointer = Params;
191     }
192 
193     /* Prepare for a return object of arbitrary size */
194 
195     ReturnObj->Pointer = AcpiGbl_DbBuffer;
196     ReturnObj->Length  = ACPI_DEBUG_BUFFER_SIZE;
197 
198     /* Do the actual method execution */
199 
200     AcpiGbl_MethodExecuting = TRUE;
201     Status = AcpiEvaluateObject (NULL, Info->Pathname,
202         &ParamObjects, ReturnObj);
203 
204     AcpiGbl_CmSingleStep = FALSE;
205     AcpiGbl_MethodExecuting = FALSE;
206 
207     if (ACPI_FAILURE (Status))
208     {
209         ACPI_EXCEPTION ((AE_INFO, Status,
210             "while executing %s from debugger", Info->Pathname));
211 
212         if (Status == AE_BUFFER_OVERFLOW)
213         {
214             ACPI_ERROR ((AE_INFO,
215                 "Possible overflow of internal debugger "
216                 "buffer (size 0x%X needed 0x%X)",
217                 ACPI_DEBUG_BUFFER_SIZE, (UINT32) ReturnObj->Length));
218         }
219     }
220 
221 Cleanup:
222     AcpiDbDeleteObjects (ParamObjects.Count, Params);
223     return_ACPI_STATUS (Status);
224 }
225 
226 
227 /*******************************************************************************
228  *
229  * FUNCTION:    AcpiDbExecuteSetup
230  *
231  * PARAMETERS:  Info            - Valid method info
232  *
233  * RETURN:      None
234  *
235  * DESCRIPTION: Setup info segment prior to method execution
236  *
237  ******************************************************************************/
238 
239 static ACPI_STATUS
240 AcpiDbExecuteSetup (
241     ACPI_DB_METHOD_INFO     *Info)
242 {
243     ACPI_STATUS             Status;
244 
245 
246     ACPI_FUNCTION_NAME (DbExecuteSetup);
247 
248 
249     /* Catenate the current scope to the supplied name */
250 
251     Info->Pathname[0] = 0;
252     if ((Info->Name[0] != '\\') &&
253         (Info->Name[0] != '/'))
254     {
255         if (AcpiUtSafeStrcat (Info->Pathname, sizeof (Info->Pathname),
256             AcpiGbl_DbScopeBuf))
257         {
258             Status = AE_BUFFER_OVERFLOW;
259             goto ErrorExit;
260         }
261     }
262 
263     if (AcpiUtSafeStrcat (Info->Pathname, sizeof (Info->Pathname),
264         Info->Name))
265     {
266         Status = AE_BUFFER_OVERFLOW;
267         goto ErrorExit;
268     }
269 
270     AcpiDbPrepNamestring (Info->Pathname);
271 
272     AcpiDbSetOutputDestination (ACPI_DB_DUPLICATE_OUTPUT);
273     AcpiOsPrintf ("Evaluating %s\n", Info->Pathname);
274 
275     if (Info->Flags & EX_SINGLE_STEP)
276     {
277         AcpiGbl_CmSingleStep = TRUE;
278         AcpiDbSetOutputDestination (ACPI_DB_CONSOLE_OUTPUT);
279     }
280 
281     else
282     {
283         /* No single step, allow redirection to a file */
284 
285         AcpiDbSetOutputDestination (ACPI_DB_REDIRECTABLE_OUTPUT);
286     }
287 
288     return (AE_OK);
289 
290 ErrorExit:
291 
292     ACPI_EXCEPTION ((AE_INFO, Status, "During setup for method execution"));
293     return (Status);
294 }
295 
296 
297 #ifdef ACPI_DBG_TRACK_ALLOCATIONS
298 UINT32
299 AcpiDbGetCacheInfo (
300     ACPI_MEMORY_LIST        *Cache)
301 {
302 
303     return (Cache->TotalAllocated - Cache->TotalFreed - Cache->CurrentDepth);
304 }
305 #endif
306 
307 /*******************************************************************************
308  *
309  * FUNCTION:    AcpiDbGetOutstandingAllocations
310  *
311  * PARAMETERS:  None
312  *
313  * RETURN:      Current global allocation count minus cache entries
314  *
315  * DESCRIPTION: Determine the current number of "outstanding" allocations --
316  *              those allocations that have not been freed and also are not
317  *              in one of the various object caches.
318  *
319  ******************************************************************************/
320 
321 static UINT32
322 AcpiDbGetOutstandingAllocations (
323     void)
324 {
325     UINT32                  Outstanding = 0;
326 
327 #ifdef ACPI_DBG_TRACK_ALLOCATIONS
328 
329     Outstanding += AcpiDbGetCacheInfo (AcpiGbl_StateCache);
330     Outstanding += AcpiDbGetCacheInfo (AcpiGbl_PsNodeCache);
331     Outstanding += AcpiDbGetCacheInfo (AcpiGbl_PsNodeExtCache);
332     Outstanding += AcpiDbGetCacheInfo (AcpiGbl_OperandCache);
333 #endif
334 
335     return (Outstanding);
336 }
337 
338 
339 /*******************************************************************************
340  *
341  * FUNCTION:    AcpiDbExecutionWalk
342  *
343  * PARAMETERS:  WALK_CALLBACK
344  *
345  * RETURN:      Status
346  *
347  * DESCRIPTION: Execute a control method. Name is relative to the current
348  *              scope.
349  *
350  ******************************************************************************/
351 
352 static ACPI_STATUS
353 AcpiDbExecutionWalk (
354     ACPI_HANDLE             ObjHandle,
355     UINT32                  NestingLevel,
356     void                    *Context,
357     void                    **ReturnValue)
358 {
359     ACPI_OPERAND_OBJECT     *ObjDesc;
360     ACPI_NAMESPACE_NODE     *Node = (ACPI_NAMESPACE_NODE *) ObjHandle;
361     ACPI_BUFFER             ReturnObj;
362     ACPI_STATUS             Status;
363 
364 
365     ObjDesc = AcpiNsGetAttachedObject (Node);
366     if (ObjDesc->Method.ParamCount)
367     {
368         return (AE_OK);
369     }
370 
371     ReturnObj.Pointer = NULL;
372     ReturnObj.Length = ACPI_ALLOCATE_BUFFER;
373 
374     AcpiNsPrintNodePathname (Node, "Evaluating");
375 
376     /* Do the actual method execution */
377 
378     AcpiOsPrintf ("\n");
379     AcpiGbl_MethodExecuting = TRUE;
380 
381     Status = AcpiEvaluateObject (Node, NULL, NULL, &ReturnObj);
382 
383     AcpiOsPrintf ("Evaluation of [%4.4s] returned %s\n",
384         AcpiUtGetNodeName (Node),
385         AcpiFormatException (Status));
386 
387     AcpiGbl_MethodExecuting = FALSE;
388     return (AE_OK);
389 }
390 
391 
392 /*******************************************************************************
393  *
394  * FUNCTION:    AcpiDbExecute
395  *
396  * PARAMETERS:  Name                - Name of method to execute
397  *              Args                - Parameters to the method
398  *              Types               -
399  *              Flags               - single step/no single step
400  *
401  * RETURN:      None
402  *
403  * DESCRIPTION: Execute a control method. Name is relative to the current
404  *              scope.
405  *
406  ******************************************************************************/
407 
408 void
409 AcpiDbExecute (
410     char                    *Name,
411     char                    **Args,
412     ACPI_OBJECT_TYPE        *Types,
413     UINT32                  Flags)
414 {
415     ACPI_STATUS             Status;
416     ACPI_BUFFER             ReturnObj;
417     char                    *NameString;
418 
419 #ifdef ACPI_DEBUG_OUTPUT
420     UINT32                  PreviousAllocations;
421     UINT32                  Allocations;
422 #endif
423 
424 
425     /*
426      * Allow one execution to be performed by debugger or single step
427      * execution will be dead locked by the interpreter mutexes.
428      */
429     if (AcpiGbl_MethodExecuting)
430     {
431         AcpiOsPrintf ("Only one debugger execution is allowed.\n");
432         return;
433     }
434 
435 #ifdef ACPI_DEBUG_OUTPUT
436     /* Memory allocation tracking */
437 
438     PreviousAllocations = AcpiDbGetOutstandingAllocations ();
439 #endif
440 
441     if (*Name == '*')
442     {
443         (void) AcpiWalkNamespace (ACPI_TYPE_METHOD, ACPI_ROOT_OBJECT,
444             ACPI_UINT32_MAX, AcpiDbExecutionWalk, NULL, NULL, NULL);
445         return;
446     }
447     else
448     {
449         NameString = ACPI_ALLOCATE (strlen (Name) + 1);
450         if (!NameString)
451         {
452             return;
453         }
454 
455         memset (&AcpiGbl_DbMethodInfo, 0, sizeof (ACPI_DB_METHOD_INFO));
456 
457         strcpy (NameString, Name);
458         AcpiUtStrupr (NameString);
459         AcpiGbl_DbMethodInfo.Name = NameString;
460         AcpiGbl_DbMethodInfo.Args = Args;
461         AcpiGbl_DbMethodInfo.Types = Types;
462         AcpiGbl_DbMethodInfo.Flags = Flags;
463 
464         ReturnObj.Pointer = NULL;
465         ReturnObj.Length = ACPI_ALLOCATE_BUFFER;
466 
467         Status = AcpiDbExecuteSetup (&AcpiGbl_DbMethodInfo);
468         if (ACPI_FAILURE (Status))
469         {
470             ACPI_FREE (NameString);
471             return;
472         }
473 
474         /* Get the NS node, determines existence also */
475 
476         Status = AcpiGetHandle (NULL, AcpiGbl_DbMethodInfo.Pathname,
477             &AcpiGbl_DbMethodInfo.Method);
478         if (ACPI_SUCCESS (Status))
479         {
480             Status = AcpiDbExecuteMethod (&AcpiGbl_DbMethodInfo,
481                 &ReturnObj);
482         }
483         ACPI_FREE (NameString);
484     }
485 
486     /*
487      * Allow any handlers in separate threads to complete.
488      * (Such as Notify handlers invoked from AML executed above).
489      */
490     AcpiOsSleep ((UINT64) 10);
491 
492 #ifdef ACPI_DEBUG_OUTPUT
493 
494     /* Memory allocation tracking */
495 
496     Allocations = AcpiDbGetOutstandingAllocations () - PreviousAllocations;
497 
498     AcpiDbSetOutputDestination (ACPI_DB_DUPLICATE_OUTPUT);
499 
500     if (Allocations > 0)
501     {
502         AcpiOsPrintf (
503             "0x%X Outstanding allocations after evaluation of %s\n",
504             Allocations, AcpiGbl_DbMethodInfo.Pathname);
505     }
506 #endif
507 
508     if (ACPI_FAILURE (Status))
509     {
510         AcpiOsPrintf ("Evaluation of %s failed with status %s\n",
511             AcpiGbl_DbMethodInfo.Pathname,
512             AcpiFormatException (Status));
513     }
514     else
515     {
516         /* Display a return object, if any */
517 
518         if (ReturnObj.Length)
519         {
520             AcpiOsPrintf (
521                 "Evaluation of %s returned object %p, "
522                 "external buffer length %X\n",
523                 AcpiGbl_DbMethodInfo.Pathname, ReturnObj.Pointer,
524                 (UINT32) ReturnObj.Length);
525 
526             AcpiDbDumpExternalObject (ReturnObj.Pointer, 1);
527 
528             /* Dump a _PLD buffer if present */
529 
530             if (ACPI_COMPARE_NAME ((ACPI_CAST_PTR (ACPI_NAMESPACE_NODE,
531                 AcpiGbl_DbMethodInfo.Method)->Name.Ascii),
532                 METHOD_NAME__PLD))
533             {
534                 AcpiDbDumpPldBuffer (ReturnObj.Pointer);
535             }
536         }
537         else
538         {
539             AcpiOsPrintf ("No object was returned from evaluation 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 
561 static void ACPI_SYSTEM_XFACE
562 AcpiDbMethodThread (
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,
586             Info->IndexOfThreadStr);
587         AcpiDbUint32ToHexString ((UINT32) AcpiOsGetThreadId (),
588             Info->IdOfThreadStr);
589     }
590 
591     if (Info->Threads && (Info->NumCreated < Info->NumThreads))
592     {
593         Info->Threads[Info->NumCreated++] = AcpiOsGetThreadId();
594     }
595 
596     LocalInfo = *Info;
597     LocalInfo.Args = LocalInfo.Arguments;
598     LocalInfo.Arguments[0] = LocalInfo.NumThreadsStr;
599     LocalInfo.Arguments[1] = LocalInfo.IdOfThreadStr;
600     LocalInfo.Arguments[2] = LocalInfo.IndexOfThreadStr;
601     LocalInfo.Arguments[3] = NULL;
602 
603     LocalInfo.Types = LocalInfo.ArgTypes;
604 
605     (void) AcpiOsSignalSemaphore (Info->InfoGate, 1);
606 
607     for (i = 0; i < Info->NumLoops; i++)
608     {
609         Status = AcpiDbExecuteMethod (&LocalInfo, &ReturnObj);
610         if (ACPI_FAILURE (Status))
611         {
612             AcpiOsPrintf ("%s During evaluation of %s at iteration %X\n",
613                 AcpiFormatException (Status), Info->Pathname, i);
614             if (Status == AE_ABORT_METHOD)
615             {
616                 break;
617             }
618         }
619 
620 #if 0
621         if ((i % 100) == 0)
622         {
623             AcpiOsPrintf ("%u loops, Thread 0x%x\n",
624                 i, AcpiOsGetThreadId ());
625         }
626 
627         if (ReturnObj.Length)
628         {
629             AcpiOsPrintf ("Evaluation of %s returned object %p Buflen %X\n",
630                 Info->Pathname, ReturnObj.Pointer,
631                 (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_EXEC_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