Merge from vendor branch OPENSSH:
[dragonfly.git] / sys / contrib / dev / acpica / dsmethod.c
1 /******************************************************************************
2  *
3  * Module Name: dsmethod - Parser/Interpreter interface - control method parsing
4  *              $Revision: 90 $
5  *
6  *****************************************************************************/
7
8 /******************************************************************************
9  *
10  * 1. Copyright Notice
11  *
12  * Some or all of this work - Copyright (c) 1999 - 2003, Intel Corp.
13  * All rights reserved.
14  *
15  * 2. License
16  *
17  * 2.1. This is your license from Intel Corp. under its intellectual property
18  * rights.  You may have additional license terms from the party that provided
19  * you this software, covering your right to use that party's intellectual
20  * property rights.
21  *
22  * 2.2. Intel grants, free of charge, to any person ("Licensee") obtaining a
23  * copy of the source code appearing in this file ("Covered Code") an
24  * irrevocable, perpetual, worldwide license under Intel's copyrights in the
25  * base code distributed originally by Intel ("Original Intel Code") to copy,
26  * make derivatives, distribute, use and display any portion of the Covered
27  * Code in any form, with the right to sublicense such rights; and
28  *
29  * 2.3. Intel grants Licensee a non-exclusive and non-transferable patent
30  * license (with the right to sublicense), under only those claims of Intel
31  * patents that are infringed by the Original Intel Code, to make, use, sell,
32  * offer to sell, and import the Covered Code and derivative works thereof
33  * solely to the minimum extent necessary to exercise the above copyright
34  * license, and in no event shall the patent license extend to any additions
35  * to or modifications of the Original Intel Code.  No other license or right
36  * is granted directly or by implication, estoppel or otherwise;
37  *
38  * The above copyright and patent license is granted only if the following
39  * conditions are met:
40  *
41  * 3. Conditions
42  *
43  * 3.1. Redistribution of Source with Rights to Further Distribute Source.
44  * Redistribution of source code of any substantial portion of the Covered
45  * Code or modification with rights to further distribute source must include
46  * the above Copyright Notice, the above License, this list of Conditions,
47  * and the following Disclaimer and Export Compliance provision.  In addition,
48  * Licensee must cause all Covered Code to which Licensee contributes to
49  * contain a file documenting the changes Licensee made to create that Covered
50  * Code and the date of any change.  Licensee must include in that file the
51  * documentation of any changes made by any predecessor Licensee.  Licensee
52  * must include a prominent statement that the modification is derived,
53  * directly or indirectly, from Original Intel Code.
54  *
55  * 3.2. Redistribution of Source with no Rights to Further Distribute Source.
56  * Redistribution of source code of any substantial portion of the Covered
57  * Code or modification without rights to further distribute source must
58  * include the following Disclaimer and Export Compliance provision in the
59  * documentation and/or other materials provided with distribution.  In
60  * addition, Licensee may not authorize further sublicense of source of any
61  * portion of the Covered Code, and must include terms to the effect that the
62  * license from Licensee to its licensee is limited to the intellectual
63  * property embodied in the software Licensee provides to its licensee, and
64  * not to intellectual property embodied in modifications its licensee may
65  * make.
66  *
67  * 3.3. Redistribution of Executable. Redistribution in executable form of any
68  * substantial portion of the Covered Code or modification must reproduce the
69  * above Copyright Notice, and the following Disclaimer and Export Compliance
70  * provision in the documentation and/or other materials provided with the
71  * distribution.
72  *
73  * 3.4. Intel retains all right, title, and interest in and to the Original
74  * Intel Code.
75  *
76  * 3.5. Neither the name Intel nor any other trademark owned or controlled by
77  * Intel shall be used in advertising or otherwise to promote the sale, use or
78  * other dealings in products derived from or relating to the Covered Code
79  * without prior written authorization from Intel.
80  *
81  * 4. Disclaimer and Export Compliance
82  *
83  * 4.1. INTEL MAKES NO WARRANTY OF ANY KIND REGARDING ANY SOFTWARE PROVIDED
84  * HERE.  ANY SOFTWARE ORIGINATING FROM INTEL OR DERIVED FROM INTEL SOFTWARE
85  * IS PROVIDED "AS IS," AND INTEL WILL NOT PROVIDE ANY SUPPORT,  ASSISTANCE,
86  * INSTALLATION, TRAINING OR OTHER SERVICES.  INTEL WILL NOT PROVIDE ANY
87  * UPDATES, ENHANCEMENTS OR EXTENSIONS.  INTEL SPECIFICALLY DISCLAIMS ANY
88  * IMPLIED WARRANTIES OF MERCHANTABILITY, NONINFRINGEMENT AND FITNESS FOR A
89  * PARTICULAR PURPOSE.
90  *
91  * 4.2. IN NO EVENT SHALL INTEL HAVE ANY LIABILITY TO LICENSEE, ITS LICENSEES
92  * OR ANY OTHER THIRD PARTY, FOR ANY LOST PROFITS, LOST DATA, LOSS OF USE OR
93  * COSTS OF PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, OR FOR ANY INDIRECT,
94  * SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THIS AGREEMENT, UNDER ANY
95  * CAUSE OF ACTION OR THEORY OF LIABILITY, AND IRRESPECTIVE OF WHETHER INTEL
96  * HAS ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES.  THESE LIMITATIONS
97  * SHALL APPLY NOTWITHSTANDING THE FAILURE OF THE ESSENTIAL PURPOSE OF ANY
98  * LIMITED REMEDY.
99  *
100  * 4.3. Licensee shall not export, either directly or indirectly, any of this
101  * software or system incorporating such software without first obtaining any
102  * required license or other approval from the U. S. Department of Commerce or
103  * any other agency or department of the United States Government.  In the
104  * event Licensee exports any such software from the United States or
105  * re-exports any such software from a foreign destination, Licensee shall
106  * ensure that the distribution and export/re-export of the software is in
107  * compliance with all laws, regulations, orders, or other restrictions of the
108  * U.S. Export Administration Regulations. Licensee agrees that neither it nor
109  * any of its subsidiaries will export/re-export any technical data, process,
110  * software, or service, directly or indirectly, to any country for which the
111  * United States government or any agency thereof requires an export license,
112  * other governmental approval, or letter of assurance, without first obtaining
113  * such license, approval or letter.
114  *
115  *****************************************************************************/
116 /* $DragonFly: src/sys/contrib/dev/acpica/Attic/dsmethod.c,v 1.1 2003/09/24 03:32:15 drhodus Exp $                                                               */
117
118 #define __DSMETHOD_C__
119
120 #include "acpi.h"
121 #include "acparser.h"
122 #include "amlcode.h"
123 #include "acdispat.h"
124 #include "acinterp.h"
125 #include "acnamesp.h"
126
127
128 #define _COMPONENT          ACPI_DISPATCHER
129         ACPI_MODULE_NAME    ("dsmethod")
130
131
132 /*******************************************************************************
133  *
134  * FUNCTION:    AcpiDsParseMethod
135  *
136  * PARAMETERS:  ObjHandle       - Node of the method
137  *              Level           - Current nesting level
138  *              Context         - Points to a method counter
139  *              ReturnValue     - Not used
140  *
141  * RETURN:      Status
142  *
143  * DESCRIPTION: Call the parser and parse the AML that is
144  *              associated with the method.
145  *
146  * MUTEX:       Assumes parser is locked
147  *
148  ******************************************************************************/
149
150 ACPI_STATUS
151 AcpiDsParseMethod (
152     ACPI_HANDLE             ObjHandle)
153 {
154     ACPI_STATUS             Status;
155     ACPI_OPERAND_OBJECT     *ObjDesc;
156     ACPI_PARSE_OBJECT       *Op;
157     ACPI_NAMESPACE_NODE     *Node;
158     ACPI_OWNER_ID           OwnerId;
159     ACPI_WALK_STATE         *WalkState;
160
161
162     ACPI_FUNCTION_TRACE_PTR ("DsParseMethod", ObjHandle);
163
164
165     /* Parameter Validation */
166
167     if (!ObjHandle)
168     {
169         return_ACPI_STATUS (AE_NULL_ENTRY);
170     }
171
172     ACPI_DEBUG_PRINT ((ACPI_DB_PARSE, "**** Parsing [%4.4s] **** NamedObj=%p\n",
173         ((ACPI_NAMESPACE_NODE *) ObjHandle)->Name.Ascii, ObjHandle));
174
175     /* Extract the method object from the method Node */
176
177     Node = (ACPI_NAMESPACE_NODE *) ObjHandle;
178     ObjDesc = AcpiNsGetAttachedObject (Node);
179     if (!ObjDesc)
180     {
181         return_ACPI_STATUS (AE_NULL_OBJECT);
182     }
183
184     /* Create a mutex for the method if there is a concurrency limit */
185
186     if ((ObjDesc->Method.Concurrency != INFINITE_CONCURRENCY) &&
187         (!ObjDesc->Method.Semaphore))
188     {
189         Status = AcpiOsCreateSemaphore (ObjDesc->Method.Concurrency,
190                                         ObjDesc->Method.Concurrency,
191                                         &ObjDesc->Method.Semaphore);
192         if (ACPI_FAILURE (Status))
193         {
194             return_ACPI_STATUS (Status);
195         }
196     }
197
198     /*
199      * Allocate a new parser op to be the root of the parsed
200      * method tree
201      */
202     Op = AcpiPsAllocOp (AML_METHOD_OP);
203     if (!Op)
204     {
205         return_ACPI_STATUS (AE_NO_MEMORY);
206     }
207
208     /* Init new op with the method name and pointer back to the Node */
209
210     AcpiPsSetName (Op, Node->Name.Integer);
211     Op->Common.Node = Node;
212
213     /*
214      * Get a new OwnerId for objects created by this method.  Namespace
215      * objects (such as Operation Regions) can be created during the
216      * first pass parse.
217      */
218     OwnerId = AcpiUtAllocateOwnerId (ACPI_OWNER_TYPE_METHOD);
219     ObjDesc->Method.OwningId = OwnerId;
220
221     /* Create and initialize a new walk state */
222
223     WalkState = AcpiDsCreateWalkState (OwnerId, NULL, NULL, NULL);
224     if (!WalkState)
225     {
226         return_ACPI_STATUS (AE_NO_MEMORY);
227     }
228
229     Status = AcpiDsInitAmlWalk (WalkState, Op, Node, ObjDesc->Method.AmlStart,
230                     ObjDesc->Method.AmlLength, NULL, NULL, 1);
231     if (ACPI_FAILURE (Status))
232     {
233         AcpiDsDeleteWalkState (WalkState);
234         return_ACPI_STATUS (Status);
235     }
236
237     /*
238      * Parse the method, first pass
239      *
240      * The first pass load is where newly declared named objects are
241      * added into the namespace.  Actual evaluation of
242      * the named objects (what would be called a "second
243      * pass") happens during the actual execution of the
244      * method so that operands to the named objects can
245      * take on dynamic run-time values.
246      */
247     Status = AcpiPsParseAml (WalkState);
248     if (ACPI_FAILURE (Status))
249     {
250         return_ACPI_STATUS (Status);
251     }
252
253     ACPI_DEBUG_PRINT ((ACPI_DB_PARSE,
254         "**** [%4.4s] Parsed **** NamedObj=%p Op=%p\n",
255         ((ACPI_NAMESPACE_NODE *) ObjHandle)->Name.Ascii, ObjHandle, Op));
256
257     AcpiPsDeleteParseTree (Op);
258     return_ACPI_STATUS (Status);
259 }
260
261
262 /*******************************************************************************
263  *
264  * FUNCTION:    AcpiDsBeginMethodExecution
265  *
266  * PARAMETERS:  MethodNode          - Node of the method
267  *              ObjDesc             - The method object
268  *              CallingMethodNode   - Caller of this method (if non-null)
269  *
270  * RETURN:      Status
271  *
272  * DESCRIPTION: Prepare a method for execution.  Parses the method if necessary,
273  *              increments the thread count, and waits at the method semaphore
274  *              for clearance to execute.
275  *
276  * MUTEX:       Locks/unlocks parser.
277  *
278  ******************************************************************************/
279
280 ACPI_STATUS
281 AcpiDsBeginMethodExecution (
282     ACPI_NAMESPACE_NODE     *MethodNode,
283     ACPI_OPERAND_OBJECT     *ObjDesc,
284     ACPI_NAMESPACE_NODE     *CallingMethodNode)
285 {
286     ACPI_STATUS             Status = AE_OK;
287
288
289     ACPI_FUNCTION_TRACE_PTR ("DsBeginMethodExecution", MethodNode);
290
291
292     if (!MethodNode)
293     {
294         return_ACPI_STATUS (AE_NULL_ENTRY);
295     }
296
297     /*
298      * If there is a concurrency limit on this method, we need to
299      * obtain a unit from the method semaphore.
300      */
301     if (ObjDesc->Method.Semaphore)
302     {
303         /*
304          * Allow recursive method calls, up to the reentrancy/concurrency
305          * limit imposed by the SERIALIZED rule and the SyncLevel method
306          * parameter.
307          *
308          * The point of this code is to avoid permanently blocking a
309          * thread that is making recursive method calls.
310          */
311         if (MethodNode == CallingMethodNode)
312         {
313             if (ObjDesc->Method.ThreadCount >= ObjDesc->Method.Concurrency)
314             {
315                 return_ACPI_STATUS (AE_AML_METHOD_LIMIT);
316             }
317         }
318
319         /*
320          * Get a unit from the method semaphore. This releases the
321          * interpreter if we block
322          */
323         Status = AcpiExSystemWaitSemaphore (ObjDesc->Method.Semaphore,
324                                             ACPI_WAIT_FOREVER);
325     }
326
327     /*
328      * Increment the method parse tree thread count since it has been
329      * reentered one more time (even if it is the same thread)
330      */
331     ObjDesc->Method.ThreadCount++;
332     return_ACPI_STATUS (Status);
333 }
334
335
336 /*******************************************************************************
337  *
338  * FUNCTION:    AcpiDsCallControlMethod
339  *
340  * PARAMETERS:  WalkState           - Current state of the walk
341  *              Op                  - Current Op to be walked
342  *
343  * RETURN:      Status
344  *
345  * DESCRIPTION: Transfer execution to a called control method
346  *
347  ******************************************************************************/
348
349 ACPI_STATUS
350 AcpiDsCallControlMethod (
351     ACPI_THREAD_STATE       *Thread,
352     ACPI_WALK_STATE         *ThisWalkState,
353     ACPI_PARSE_OBJECT       *Op)
354 {
355     ACPI_STATUS             Status;
356     ACPI_NAMESPACE_NODE     *MethodNode;
357     ACPI_OPERAND_OBJECT     *ObjDesc;
358     ACPI_WALK_STATE         *NextWalkState;
359     UINT32                  i;
360
361
362     ACPI_FUNCTION_TRACE_PTR ("DsCallControlMethod", ThisWalkState);
363
364     ACPI_DEBUG_PRINT ((ACPI_DB_DISPATCH, "Execute method %p, currentstate=%p\n",
365         ThisWalkState->PrevOp, ThisWalkState));
366
367     /*
368      * Get the namespace entry for the control method we are about to call
369      */
370     MethodNode = ThisWalkState->MethodCallNode;
371     if (!MethodNode)
372     {
373         return_ACPI_STATUS (AE_NULL_ENTRY);
374     }
375
376     ObjDesc = AcpiNsGetAttachedObject (MethodNode);
377     if (!ObjDesc)
378     {
379         return_ACPI_STATUS (AE_NULL_OBJECT);
380     }
381
382     ObjDesc->Method.OwningId = AcpiUtAllocateOwnerId (ACPI_OWNER_TYPE_METHOD);
383
384     /* Init for new method, wait on concurrency semaphore */
385
386     Status = AcpiDsBeginMethodExecution (MethodNode, ObjDesc,
387                     ThisWalkState->MethodNode);
388     if (ACPI_FAILURE (Status))
389     {
390         return_ACPI_STATUS (Status);
391     }
392
393     /* 1) Parse: Create a new walk state for the preempting walk */
394
395     NextWalkState = AcpiDsCreateWalkState (ObjDesc->Method.OwningId,
396                                             Op, ObjDesc, NULL);
397     if (!NextWalkState)
398     {
399         return_ACPI_STATUS (AE_NO_MEMORY);
400     }
401
402     /* Create and init a Root Node */
403
404     Op = AcpiPsCreateScopeOp ();
405     if (!Op)
406     {
407         Status = AE_NO_MEMORY;
408         goto Cleanup;
409     }
410
411     Status = AcpiDsInitAmlWalk (NextWalkState, Op, MethodNode,
412                     ObjDesc->Method.AmlStart,  ObjDesc->Method.AmlLength,
413                     NULL, NULL, 1);
414     if (ACPI_FAILURE (Status))
415     {
416         AcpiDsDeleteWalkState (NextWalkState);
417         goto Cleanup;
418     }
419
420     /* Begin AML parse */
421
422     Status = AcpiPsParseAml (NextWalkState);
423     AcpiPsDeleteParseTree (Op);
424
425     /* 2) Execute: Create a new state for the preempting walk */
426
427     NextWalkState = AcpiDsCreateWalkState (ObjDesc->Method.OwningId,
428                                             NULL, ObjDesc, Thread);
429     if (!NextWalkState)
430     {
431         Status = AE_NO_MEMORY;
432         goto Cleanup;
433     }
434
435     /*
436      * The resolved arguments were put on the previous walk state's operand
437      * stack.  Operands on the previous walk state stack always
438      * start at index 0.
439      * Null terminate the list of arguments
440      */
441     ThisWalkState->Operands [ThisWalkState->NumOperands] = NULL;
442
443     Status = AcpiDsInitAmlWalk (NextWalkState, NULL, MethodNode,
444                     ObjDesc->Method.AmlStart, ObjDesc->Method.AmlLength,
445                     &ThisWalkState->Operands[0], NULL, 3);
446     if (ACPI_FAILURE (Status))
447     {
448         goto Cleanup;
449     }
450
451     /*
452      * Delete the operands on the previous walkstate operand stack
453      * (they were copied to new objects)
454      */
455     for (i = 0; i < ObjDesc->Method.ParamCount; i++)
456     {
457         AcpiUtRemoveReference (ThisWalkState->Operands [i]);
458         ThisWalkState->Operands [i] = NULL;
459     }
460
461     /* Clear the operand stack */
462
463     ThisWalkState->NumOperands = 0;
464
465     ACPI_DEBUG_PRINT ((ACPI_DB_DISPATCH,
466         "Starting nested execution, newstate=%p\n", NextWalkState));
467
468     return_ACPI_STATUS (AE_OK);
469
470
471     /* On error, we must delete the new walk state */
472
473 Cleanup:
474     (void) AcpiDsTerminateControlMethod (NextWalkState);
475     AcpiDsDeleteWalkState (NextWalkState);
476     return_ACPI_STATUS (Status);
477
478 }
479
480
481 /*******************************************************************************
482  *
483  * FUNCTION:    AcpiDsRestartControlMethod
484  *
485  * PARAMETERS:  WalkState           - State of the method when it was preempted
486  *              Op                  - Pointer to new current op
487  *
488  * RETURN:      Status
489  *
490  * DESCRIPTION: Restart a method that was preempted
491  *
492  ******************************************************************************/
493
494 ACPI_STATUS
495 AcpiDsRestartControlMethod (
496     ACPI_WALK_STATE         *WalkState,
497     ACPI_OPERAND_OBJECT     *ReturnDesc)
498 {
499     ACPI_STATUS             Status;
500
501
502     ACPI_FUNCTION_TRACE_PTR ("DsRestartControlMethod", WalkState);
503
504
505     if (ReturnDesc)
506     {
507         if (WalkState->ReturnUsed)
508         {
509             /*
510              * Get the return value (if any) from the previous method.
511              * NULL if no return value
512              */
513             Status = AcpiDsResultPush (ReturnDesc, WalkState);
514             if (ACPI_FAILURE (Status))
515             {
516                 AcpiUtRemoveReference (ReturnDesc);
517                 return_ACPI_STATUS (Status);
518             }
519         }
520         else
521         {
522             /*
523              * Delete the return value if it will not be used by the
524              * calling method
525              */
526             AcpiUtRemoveReference (ReturnDesc);
527         }
528     }
529
530     ACPI_DEBUG_PRINT ((ACPI_DB_DISPATCH,
531         "Method=%p Return=%p ReturnUsed?=%X ResStack=%p State=%p\n",
532         WalkState->MethodCallOp, ReturnDesc, WalkState->ReturnUsed,
533         WalkState->Results, WalkState));
534
535     return_ACPI_STATUS (AE_OK);
536 }
537
538
539 /*******************************************************************************
540  *
541  * FUNCTION:    AcpiDsTerminateControlMethod
542  *
543  * PARAMETERS:  WalkState           - State of the method
544  *
545  * RETURN:      Status
546  *
547  * DESCRIPTION: Terminate a control method.  Delete everything that the method
548  *              created, delete all locals and arguments, and delete the parse
549  *              tree if requested.
550  *
551  ******************************************************************************/
552
553 ACPI_STATUS
554 AcpiDsTerminateControlMethod (
555     ACPI_WALK_STATE         *WalkState)
556 {
557     ACPI_OPERAND_OBJECT     *ObjDesc;
558     ACPI_NAMESPACE_NODE     *MethodNode;
559     ACPI_STATUS             Status;
560
561
562     ACPI_FUNCTION_TRACE_PTR ("DsTerminateControlMethod", WalkState);
563
564
565     if (!WalkState)
566     {
567         return (AE_BAD_PARAMETER);
568     }
569
570     /* The current method object was saved in the walk state */
571
572     ObjDesc = WalkState->MethodDesc;
573     if (!ObjDesc)
574     {
575         return_ACPI_STATUS (AE_OK);
576     }
577
578     /* Delete all arguments and locals */
579
580     AcpiDsMethodDataDeleteAll (WalkState);
581
582     /*
583      * Lock the parser while we terminate this method.
584      * If this is the last thread executing the method,
585      * we have additional cleanup to perform
586      */
587     Status = AcpiUtAcquireMutex (ACPI_MTX_PARSER);
588     if (ACPI_FAILURE (Status))
589     {
590         return_ACPI_STATUS (Status);
591     }
592
593     /* Signal completion of the execution of this method if necessary */
594
595     if (WalkState->MethodDesc->Method.Semaphore)
596     {
597         Status = AcpiOsSignalSemaphore (
598                         WalkState->MethodDesc->Method.Semaphore, 1);
599         if (ACPI_FAILURE (Status))
600         {
601             ACPI_REPORT_ERROR (("Could not signal method semaphore\n"));
602             Status = AE_OK;
603
604             /* Ignore error and continue cleanup */
605         }
606     }
607
608     /* Decrement the thread count on the method parse tree */
609
610     WalkState->MethodDesc->Method.ThreadCount--;
611     if (!WalkState->MethodDesc->Method.ThreadCount)
612     {
613         /*
614          * There are no more threads executing this method.  Perform
615          * additional cleanup.
616          *
617          * The method Node is stored in the walk state
618          */
619         MethodNode = WalkState->MethodNode;
620
621         /*
622          * Delete any namespace entries created immediately underneath
623          * the method
624          */
625         Status = AcpiUtAcquireMutex (ACPI_MTX_NAMESPACE);
626         if (ACPI_FAILURE (Status))
627         {
628             return_ACPI_STATUS (Status);
629         }
630
631         if (MethodNode->Child)
632         {
633             AcpiNsDeleteNamespaceSubtree (MethodNode);
634         }
635
636         /*
637          * Delete any namespace entries created anywhere else within
638          * the namespace
639          */
640         AcpiNsDeleteNamespaceByOwner (WalkState->MethodDesc->Method.OwningId);
641         Status = AcpiUtReleaseMutex (ACPI_MTX_NAMESPACE);
642         if (ACPI_FAILURE (Status))
643         {
644             return_ACPI_STATUS (Status);
645         }
646     }
647
648     Status = AcpiUtReleaseMutex (ACPI_MTX_PARSER);
649     return_ACPI_STATUS (Status);
650 }
651
652