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