1 /******************************************************************************
3 * Module Name: dsmethod - Parser/Interpreter interface - control method parsing
6 *****************************************************************************/
8 /******************************************************************************
12 * Some or all of this work - Copyright (c) 1999 - 2005, Intel Corp.
13 * All rights reserved.
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
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
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;
38 * The above copyright and patent license is granted only if the following
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.
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
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
73 * 3.4. Intel retains all right, title, and interest in and to the Original
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.
81 * 4. Disclaimer and Export Compliance
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
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
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.
115 *****************************************************************************/
117 #define __DSMETHOD_C__
119 #include <contrib/dev/acpica/acpi.h>
120 #include <contrib/dev/acpica/acparser.h>
121 #include <contrib/dev/acpica/amlcode.h>
122 #include <contrib/dev/acpica/acdispat.h>
123 #include <contrib/dev/acpica/acinterp.h>
124 #include <contrib/dev/acpica/acnamesp.h>
127 #define _COMPONENT ACPI_DISPATCHER
128 ACPI_MODULE_NAME ("dsmethod")
131 /*******************************************************************************
133 * FUNCTION: AcpiDsParseMethod
135 * PARAMETERS: Node - Method node
139 * DESCRIPTION: Parse the AML that is associated with the method.
141 * MUTEX: Assumes parser is locked
143 ******************************************************************************/
147 ACPI_NAMESPACE_NODE *Node)
150 ACPI_OPERAND_OBJECT *ObjDesc;
151 ACPI_PARSE_OBJECT *Op;
152 ACPI_WALK_STATE *WalkState;
155 ACPI_FUNCTION_TRACE_PTR ("DsParseMethod", Node);
158 /* Parameter Validation */
162 return_ACPI_STATUS (AE_NULL_ENTRY);
165 ACPI_DEBUG_PRINT ((ACPI_DB_PARSE, "**** Parsing [%4.4s] **** NamedObj=%p\n",
166 AcpiUtGetNodeName (Node), Node));
168 /* Extract the method object from the method Node */
170 ObjDesc = AcpiNsGetAttachedObject (Node);
173 return_ACPI_STATUS (AE_NULL_OBJECT);
176 /* Create a mutex for the method if there is a concurrency limit */
178 if ((ObjDesc->Method.Concurrency != ACPI_INFINITE_CONCURRENCY) &&
179 (!ObjDesc->Method.Semaphore))
181 Status = AcpiOsCreateSemaphore (ObjDesc->Method.Concurrency,
182 ObjDesc->Method.Concurrency,
183 &ObjDesc->Method.Semaphore);
184 if (ACPI_FAILURE (Status))
186 return_ACPI_STATUS (Status);
191 * Allocate a new parser op to be the root of the parsed
194 Op = AcpiPsAllocOp (AML_METHOD_OP);
197 return_ACPI_STATUS (AE_NO_MEMORY);
200 /* Init new op with the method name and pointer back to the Node */
202 AcpiPsSetName (Op, Node->Name.Integer);
203 Op->Common.Node = Node;
206 * Get a new OwnerId for objects created by this method. Namespace
207 * objects (such as Operation Regions) can be created during the
210 Status = AcpiUtAllocateOwnerId (&ObjDesc->Method.OwnerId);
211 if (ACPI_FAILURE (Status))
216 /* Create and initialize a new walk state */
218 WalkState = AcpiDsCreateWalkState (
219 ObjDesc->Method.OwnerId, NULL, NULL, NULL);
222 Status = AE_NO_MEMORY;
226 Status = AcpiDsInitAmlWalk (WalkState, Op, Node,
227 ObjDesc->Method.AmlStart,
228 ObjDesc->Method.AmlLength, NULL, 1);
229 if (ACPI_FAILURE (Status))
231 AcpiDsDeleteWalkState (WalkState);
236 * Parse the method, first pass
238 * The first pass load is where newly declared named objects are added into
239 * the namespace. Actual evaluation of the named objects (what would be
240 * called a "second pass") happens during the actual execution of the
241 * method so that operands to the named objects can take on dynamic
244 Status = AcpiPsParseAml (WalkState);
245 if (ACPI_FAILURE (Status))
250 ACPI_DEBUG_PRINT ((ACPI_DB_PARSE,
251 "**** [%4.4s] Parsed **** NamedObj=%p Op=%p\n",
252 AcpiUtGetNodeName (Node), Node, Op));
255 * Delete the parse tree. We simply re-parse the method for every
256 * execution since there isn't much overhead (compared to keeping lots
257 * of parse trees around)
259 AcpiNsDeleteNamespaceSubtree (Node);
260 AcpiNsDeleteNamespaceByOwner (ObjDesc->Method.OwnerId);
263 AcpiUtReleaseOwnerId (&ObjDesc->Method.OwnerId);
266 AcpiPsDeleteParseTree (Op);
267 return_ACPI_STATUS (Status);
271 /*******************************************************************************
273 * FUNCTION: AcpiDsBeginMethodExecution
275 * PARAMETERS: MethodNode - Node of the method
276 * ObjDesc - The method object
277 * CallingMethodNode - Caller of this method (if non-null)
281 * DESCRIPTION: Prepare a method for execution. Parses the method if necessary,
282 * increments the thread count, and waits at the method semaphore
283 * for clearance to execute.
285 ******************************************************************************/
288 AcpiDsBeginMethodExecution (
289 ACPI_NAMESPACE_NODE *MethodNode,
290 ACPI_OPERAND_OBJECT *ObjDesc,
291 ACPI_NAMESPACE_NODE *CallingMethodNode)
293 ACPI_STATUS Status = AE_OK;
296 ACPI_FUNCTION_TRACE_PTR ("DsBeginMethodExecution", MethodNode);
301 return_ACPI_STATUS (AE_NULL_ENTRY);
304 /* Prevent wraparound of thread count */
306 if (ObjDesc->Method.ThreadCount == ACPI_UINT8_MAX)
309 "Method reached maximum reentrancy limit (255)\n"));
310 return_ACPI_STATUS (AE_AML_METHOD_LIMIT);
314 * If there is a concurrency limit on this method, we need to
315 * obtain a unit from the method semaphore.
317 if (ObjDesc->Method.Semaphore)
320 * Allow recursive method calls, up to the reentrancy/concurrency
321 * limit imposed by the SERIALIZED rule and the SyncLevel method
324 * The point of this code is to avoid permanently blocking a
325 * thread that is making recursive method calls.
327 if (MethodNode == CallingMethodNode)
329 if (ObjDesc->Method.ThreadCount >= ObjDesc->Method.Concurrency)
331 return_ACPI_STATUS (AE_AML_METHOD_LIMIT);
336 * Get a unit from the method semaphore. This releases the
337 * interpreter if we block
339 Status = AcpiExSystemWaitSemaphore (ObjDesc->Method.Semaphore,
344 * Allocate an Owner ID for this method, only if this is the first thread
345 * to begin concurrent execution. We only need one OwnerId, even if the
346 * method is invoked recursively.
348 if (!ObjDesc->Method.OwnerId)
350 Status = AcpiUtAllocateOwnerId (&ObjDesc->Method.OwnerId);
351 if (ACPI_FAILURE (Status))
353 return_ACPI_STATUS (Status);
358 * Increment the method parse tree thread count since it has been
359 * reentered one more time (even if it is the same thread)
361 ObjDesc->Method.ThreadCount++;
362 return_ACPI_STATUS (Status);
366 /*******************************************************************************
368 * FUNCTION: AcpiDsCallControlMethod
370 * PARAMETERS: Thread - Info for this thread
371 * ThisWalkState - Current walk state
372 * Op - Current Op to be walked
376 * DESCRIPTION: Transfer execution to a called control method
378 ******************************************************************************/
381 AcpiDsCallControlMethod (
382 ACPI_THREAD_STATE *Thread,
383 ACPI_WALK_STATE *ThisWalkState,
384 ACPI_PARSE_OBJECT *Op)
387 ACPI_NAMESPACE_NODE *MethodNode;
388 ACPI_WALK_STATE *NextWalkState = NULL;
389 ACPI_OPERAND_OBJECT *ObjDesc;
390 ACPI_PARAMETER_INFO Info;
394 ACPI_FUNCTION_TRACE_PTR ("DsCallControlMethod", ThisWalkState);
396 ACPI_DEBUG_PRINT ((ACPI_DB_DISPATCH, "Execute method %p, currentstate=%p\n",
397 ThisWalkState->PrevOp, ThisWalkState));
400 * Get the namespace entry for the control method we are about to call
402 MethodNode = ThisWalkState->MethodCallNode;
405 return_ACPI_STATUS (AE_NULL_ENTRY);
408 ObjDesc = AcpiNsGetAttachedObject (MethodNode);
411 return_ACPI_STATUS (AE_NULL_OBJECT);
414 /* Init for new method, wait on concurrency semaphore */
416 Status = AcpiDsBeginMethodExecution (MethodNode, ObjDesc,
417 ThisWalkState->MethodNode);
418 if (ACPI_FAILURE (Status))
423 if (!(ObjDesc->Method.MethodFlags & AML_METHOD_INTERNAL_ONLY))
425 /* 1) Parse: Create a new walk state for the preempting walk */
427 NextWalkState = AcpiDsCreateWalkState (ObjDesc->Method.OwnerId,
431 return_ACPI_STATUS (AE_NO_MEMORY);
434 /* Create and init a Root Node */
436 Op = AcpiPsCreateScopeOp ();
439 Status = AE_NO_MEMORY;
443 Status = AcpiDsInitAmlWalk (NextWalkState, Op, MethodNode,
444 ObjDesc->Method.AmlStart, ObjDesc->Method.AmlLength,
446 if (ACPI_FAILURE (Status))
448 AcpiDsDeleteWalkState (NextWalkState);
452 /* Begin AML parse */
454 Status = AcpiPsParseAml (NextWalkState);
455 AcpiPsDeleteParseTree (Op);
458 /* 2) Execute: Create a new state for the preempting walk */
460 NextWalkState = AcpiDsCreateWalkState (ObjDesc->Method.OwnerId,
461 NULL, ObjDesc, Thread);
464 Status = AE_NO_MEMORY;
468 * The resolved arguments were put on the previous walk state's operand
469 * stack. Operands on the previous walk state stack always
470 * start at index 0. Also, null terminate the list of arguments
472 ThisWalkState->Operands [ThisWalkState->NumOperands] = NULL;
474 Info.Parameters = &ThisWalkState->Operands[0];
475 Info.ParameterType = ACPI_PARAM_ARGS;
477 Status = AcpiDsInitAmlWalk (NextWalkState, NULL, MethodNode,
478 ObjDesc->Method.AmlStart, ObjDesc->Method.AmlLength,
480 if (ACPI_FAILURE (Status))
486 * Delete the operands on the previous walkstate operand stack
487 * (they were copied to new objects)
489 for (i = 0; i < ObjDesc->Method.ParamCount; i++)
491 AcpiUtRemoveReference (ThisWalkState->Operands [i]);
492 ThisWalkState->Operands [i] = NULL;
495 /* Clear the operand stack */
497 ThisWalkState->NumOperands = 0;
499 ACPI_DEBUG_PRINT ((ACPI_DB_DISPATCH,
500 "Starting nested execution, newstate=%p\n", NextWalkState));
502 if (ObjDesc->Method.MethodFlags & AML_METHOD_INTERNAL_ONLY)
504 Status = ObjDesc->Method.Implementation (NextWalkState);
507 return_ACPI_STATUS (Status);
511 /* Decrement the thread count on the method parse tree */
513 if (NextWalkState && (NextWalkState->MethodDesc))
515 NextWalkState->MethodDesc->Method.ThreadCount--;
518 /* On error, we must delete the new walk state */
520 AcpiDsTerminateControlMethod (NextWalkState);
521 AcpiDsDeleteWalkState (NextWalkState);
522 return_ACPI_STATUS (Status);
526 /*******************************************************************************
528 * FUNCTION: AcpiDsRestartControlMethod
530 * PARAMETERS: WalkState - State for preempted method (caller)
531 * ReturnDesc - Return value from the called method
535 * DESCRIPTION: Restart a method that was preempted by another (nested) method
536 * invocation. Handle the return value (if any) from the callee.
538 ******************************************************************************/
541 AcpiDsRestartControlMethod (
542 ACPI_WALK_STATE *WalkState,
543 ACPI_OPERAND_OBJECT *ReturnDesc)
548 ACPI_FUNCTION_TRACE_PTR ("DsRestartControlMethod", WalkState);
551 ACPI_DEBUG_PRINT ((ACPI_DB_DISPATCH,
552 "****Restart [%4.4s] Op %p ReturnValueFromCallee %p\n",
553 (char *) &WalkState->MethodNode->Name, WalkState->MethodCallOp,
556 ACPI_DEBUG_PRINT ((ACPI_DB_DISPATCH,
557 " ReturnFromThisMethodUsed?=%X ResStack %p Walk %p\n",
558 WalkState->ReturnUsed,
559 WalkState->Results, WalkState));
561 /* Did the called method return a value? */
565 /* Are we actually going to use the return value? */
567 if (WalkState->ReturnUsed)
569 /* Save the return value from the previous method */
571 Status = AcpiDsResultPush (ReturnDesc, WalkState);
572 if (ACPI_FAILURE (Status))
574 AcpiUtRemoveReference (ReturnDesc);
575 return_ACPI_STATUS (Status);
579 * Save as THIS method's return value in case it is returned
580 * immediately to yet another method
582 WalkState->ReturnDesc = ReturnDesc;
586 * The following code is the
587 * optional support for a so-called "implicit return". Some AML code
588 * assumes that the last value of the method is "implicitly" returned
589 * to the caller. Just save the last result as the return value.
590 * NOTE: this is optional because the ASL language does not actually
591 * support this behavior.
593 else if (!AcpiDsDoImplicitReturn (ReturnDesc, WalkState, FALSE))
596 * Delete the return value if it will not be used by the
599 AcpiUtRemoveReference (ReturnDesc);
603 return_ACPI_STATUS (AE_OK);
607 /*******************************************************************************
609 * FUNCTION: AcpiDsTerminateControlMethod
611 * PARAMETERS: WalkState - State of the method
615 * DESCRIPTION: Terminate a control method. Delete everything that the method
616 * created, delete all locals and arguments, and delete the parse
619 ******************************************************************************/
622 AcpiDsTerminateControlMethod (
623 ACPI_WALK_STATE *WalkState)
625 ACPI_OPERAND_OBJECT *ObjDesc;
626 ACPI_NAMESPACE_NODE *MethodNode;
630 ACPI_FUNCTION_TRACE_PTR ("DsTerminateControlMethod", WalkState);
638 /* The current method object was saved in the walk state */
640 ObjDesc = WalkState->MethodDesc;
646 /* Delete all arguments and locals */
648 AcpiDsMethodDataDeleteAll (WalkState);
651 * Lock the parser while we terminate this method.
652 * If this is the last thread executing the method,
653 * we have additional cleanup to perform
655 Status = AcpiUtAcquireMutex (ACPI_MTX_PARSER);
656 if (ACPI_FAILURE (Status))
661 /* Signal completion of the execution of this method if necessary */
663 if (WalkState->MethodDesc->Method.Semaphore)
665 Status = AcpiOsSignalSemaphore (
666 WalkState->MethodDesc->Method.Semaphore, 1);
667 if (ACPI_FAILURE (Status))
669 ACPI_REPORT_ERROR (("Could not signal method semaphore\n"));
671 /* Ignore error and continue cleanup */
675 if (WalkState->MethodDesc->Method.ThreadCount)
677 ACPI_DEBUG_PRINT ((ACPI_DB_DISPATCH,
678 "*** Not deleting method namespace, there are still %d threads\n",
679 WalkState->MethodDesc->Method.ThreadCount));
681 else /* This is the last executing thread */
684 * Support to dynamically change a method from NotSerialized to
685 * Serialized if it appears that the method is written foolishly and
686 * does not support multiple thread execution. The best example of this
687 * is if such a method creates namespace objects and blocks. A second
688 * thread will fail with an AE_ALREADY_EXISTS exception
690 * This code is here because we must wait until the last thread exits
691 * before creating the synchronization semaphore.
693 if ((WalkState->MethodDesc->Method.Concurrency == 1) &&
694 (!WalkState->MethodDesc->Method.Semaphore))
696 Status = AcpiOsCreateSemaphore (1, 1,
697 &WalkState->MethodDesc->Method.Semaphore);
701 * There are no more threads executing this method. Perform
702 * additional cleanup.
704 * The method Node is stored in the walk state
706 MethodNode = WalkState->MethodNode;
709 * Delete any namespace entries created immediately underneath
712 Status = AcpiUtAcquireMutex (ACPI_MTX_NAMESPACE);
713 if (ACPI_FAILURE (Status))
718 if (MethodNode->Child)
720 AcpiNsDeleteNamespaceSubtree (MethodNode);
724 * Delete any namespace entries created anywhere else within
727 AcpiNsDeleteNamespaceByOwner (WalkState->MethodDesc->Method.OwnerId);
728 Status = AcpiUtReleaseMutex (ACPI_MTX_NAMESPACE);
729 AcpiUtReleaseOwnerId (&WalkState->MethodDesc->Method.OwnerId);
733 (void) AcpiUtReleaseMutex (ACPI_MTX_PARSER);