1 /******************************************************************************
3 * Module Name: dsmethod - Parser/Interpreter interface - control method parsing
6 *****************************************************************************/
8 /******************************************************************************
12 * Some or all of this work - Copyright (c) 1999 - 2006, 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__
120 #include "acparser.h"
122 #include "acdispat.h"
123 #include "acinterp.h"
124 #include "acnamesp.h"
125 #include "acdisasm.h"
128 #define _COMPONENT ACPI_DISPATCHER
129 ACPI_MODULE_NAME ("dsmethod")
131 /* Local prototypes */
134 AcpiDsCreateMethodMutex (
135 ACPI_OPERAND_OBJECT
*MethodDesc
);
138 /*******************************************************************************
140 * FUNCTION: AcpiDsMethodError
142 * PARAMETERS: Status - Execution status
143 * WalkState - Current state
147 * DESCRIPTION: Called on method error. Invoke the global exception handler if
148 * present, dump the method data if the disassembler is configured
150 * Note: Allows the exception handler to change the status code
152 ******************************************************************************/
157 ACPI_WALK_STATE
*WalkState
)
159 ACPI_FUNCTION_ENTRY ();
162 /* Ignore AE_OK and control exception codes */
164 if (ACPI_SUCCESS (Status
) ||
165 (Status
& AE_CODE_CONTROL
))
170 /* Invoke the global exception handler */
172 if (AcpiGbl_ExceptionHandler
)
174 /* Exit the interpreter, allow handler to execute methods */
176 AcpiExExitInterpreter ();
179 * Handler can map the exception code to anything it wants, including
180 * AE_OK, in which case the executing method will not be aborted.
182 Status
= AcpiGbl_ExceptionHandler (Status
,
183 WalkState
->MethodNode
?
184 WalkState
->MethodNode
->Name
.Integer
: 0,
185 WalkState
->Opcode
, WalkState
->AmlOffset
, NULL
);
186 (void) AcpiExEnterInterpreter ();
189 #ifdef ACPI_DISASSEMBLER
190 if (ACPI_FAILURE (Status
))
192 /* Display method locals/args if disassembler is present */
194 AcpiDmDumpMethodInfo (Status
, WalkState
, WalkState
->Op
);
202 /*******************************************************************************
204 * FUNCTION: AcpiDsCreateMethodMutex
206 * PARAMETERS: ObjDesc - The method object
210 * DESCRIPTION: Create a mutex object for a serialized control method
212 ******************************************************************************/
215 AcpiDsCreateMethodMutex (
216 ACPI_OPERAND_OBJECT
*MethodDesc
)
218 ACPI_OPERAND_OBJECT
*MutexDesc
;
222 ACPI_FUNCTION_TRACE (DsCreateMethodMutex
);
225 /* Create the new mutex object */
227 MutexDesc
= AcpiUtCreateInternalObject (ACPI_TYPE_MUTEX
);
230 return_ACPI_STATUS (AE_NO_MEMORY
);
233 /* Create the actual OS Mutex */
235 Status
= AcpiOsCreateMutex (&MutexDesc
->Mutex
.OsMutex
);
236 if (ACPI_FAILURE (Status
))
238 return_ACPI_STATUS (Status
);
241 MutexDesc
->Mutex
.SyncLevel
= MethodDesc
->Method
.SyncLevel
;
242 MethodDesc
->Method
.Mutex
= MutexDesc
;
243 return_ACPI_STATUS (AE_OK
);
247 /*******************************************************************************
249 * FUNCTION: AcpiDsBeginMethodExecution
251 * PARAMETERS: MethodNode - Node of the method
252 * ObjDesc - The method object
253 * WalkState - current state, NULL if not yet executing
258 * DESCRIPTION: Prepare a method for execution. Parses the method if necessary,
259 * increments the thread count, and waits at the method semaphore
260 * for clearance to execute.
262 ******************************************************************************/
265 AcpiDsBeginMethodExecution (
266 ACPI_NAMESPACE_NODE
*MethodNode
,
267 ACPI_OPERAND_OBJECT
*ObjDesc
,
268 ACPI_WALK_STATE
*WalkState
)
270 ACPI_STATUS Status
= AE_OK
;
273 ACPI_FUNCTION_TRACE_PTR (DsBeginMethodExecution
, MethodNode
);
278 return_ACPI_STATUS (AE_NULL_ENTRY
);
281 /* Prevent wraparound of thread count */
283 if (ObjDesc
->Method
.ThreadCount
== ACPI_UINT8_MAX
)
285 ACPI_ERROR ((AE_INFO
,
286 "Method reached maximum reentrancy limit (255)"));
287 return_ACPI_STATUS (AE_AML_METHOD_LIMIT
);
291 * If this method is serialized, we need to acquire the method mutex.
293 if (ObjDesc
->Method
.MethodFlags
& AML_METHOD_SERIALIZED
)
296 * Create a mutex for the method if it is defined to be Serialized
297 * and a mutex has not already been created. We defer the mutex creation
298 * until a method is actually executed, to minimize the object count
300 if (!ObjDesc
->Method
.Mutex
)
302 Status
= AcpiDsCreateMethodMutex (ObjDesc
);
303 if (ACPI_FAILURE (Status
))
305 return_ACPI_STATUS (Status
);
310 * The CurrentSyncLevel (per-thread) must be less than or equal to
311 * the sync level of the method. This mechanism provides some
312 * deadlock prevention
314 * Top-level method invocation has no walk state at this point
317 (WalkState
->Thread
->CurrentSyncLevel
> ObjDesc
->Method
.Mutex
->Mutex
.SyncLevel
))
319 ACPI_ERROR ((AE_INFO
,
320 "Cannot acquire Mutex for method [%4.4s], current SyncLevel is too large (%d)",
321 AcpiUtGetNodeName (MethodNode
),
322 WalkState
->Thread
->CurrentSyncLevel
));
324 return_ACPI_STATUS (AE_AML_MUTEX_ORDER
);
328 * Obtain the method mutex if necessary. Do not acquire mutex for a
332 !ObjDesc
->Method
.Mutex
->Mutex
.OwnerThread
||
333 (WalkState
->Thread
!= ObjDesc
->Method
.Mutex
->Mutex
.OwnerThread
))
336 * Acquire the method mutex. This releases the interpreter if we
337 * block (and reacquires it before it returns)
339 Status
= AcpiExSystemWaitMutex (ObjDesc
->Method
.Mutex
->Mutex
.OsMutex
,
341 if (ACPI_FAILURE (Status
))
343 return_ACPI_STATUS (Status
);
346 /* Update the mutex and walk info and save the original SyncLevel */
350 ObjDesc
->Method
.Mutex
->Mutex
.OriginalSyncLevel
=
351 WalkState
->Thread
->CurrentSyncLevel
;
353 ObjDesc
->Method
.Mutex
->Mutex
.OwnerThread
= WalkState
->Thread
;
354 WalkState
->Thread
->CurrentSyncLevel
= ObjDesc
->Method
.SyncLevel
;
358 ObjDesc
->Method
.Mutex
->Mutex
.OriginalSyncLevel
=
359 ObjDesc
->Method
.Mutex
->Mutex
.SyncLevel
;
363 /* Always increase acquisition depth */
365 ObjDesc
->Method
.Mutex
->Mutex
.AcquisitionDepth
++;
369 * Allocate an Owner ID for this method, only if this is the first thread
370 * to begin concurrent execution. We only need one OwnerId, even if the
371 * method is invoked recursively.
373 if (!ObjDesc
->Method
.OwnerId
)
375 Status
= AcpiUtAllocateOwnerId (&ObjDesc
->Method
.OwnerId
);
376 if (ACPI_FAILURE (Status
))
383 * Increment the method parse tree thread count since it has been
384 * reentered one more time (even if it is the same thread)
386 ObjDesc
->Method
.ThreadCount
++;
387 return_ACPI_STATUS (Status
);
391 /* On error, must release the method mutex (if present) */
393 if (ObjDesc
->Method
.Mutex
)
395 AcpiOsReleaseMutex (ObjDesc
->Method
.Mutex
->Mutex
.OsMutex
);
397 return_ACPI_STATUS (Status
);
401 /*******************************************************************************
403 * FUNCTION: AcpiDsCallControlMethod
405 * PARAMETERS: Thread - Info for this thread
406 * ThisWalkState - Current walk state
407 * Op - Current Op to be walked
411 * DESCRIPTION: Transfer execution to a called control method
413 ******************************************************************************/
416 AcpiDsCallControlMethod (
417 ACPI_THREAD_STATE
*Thread
,
418 ACPI_WALK_STATE
*ThisWalkState
,
419 ACPI_PARSE_OBJECT
*Op
)
422 ACPI_NAMESPACE_NODE
*MethodNode
;
423 ACPI_WALK_STATE
*NextWalkState
= NULL
;
424 ACPI_OPERAND_OBJECT
*ObjDesc
;
425 ACPI_EVALUATE_INFO
*Info
;
429 ACPI_FUNCTION_TRACE_PTR (DsCallControlMethod
, ThisWalkState
);
431 ACPI_DEBUG_PRINT ((ACPI_DB_DISPATCH
, "Calling method %p, currentstate=%p\n",
432 ThisWalkState
->PrevOp
, ThisWalkState
));
435 * Get the namespace entry for the control method we are about to call
437 MethodNode
= ThisWalkState
->MethodCallNode
;
440 return_ACPI_STATUS (AE_NULL_ENTRY
);
443 ObjDesc
= AcpiNsGetAttachedObject (MethodNode
);
446 return_ACPI_STATUS (AE_NULL_OBJECT
);
449 /* Init for new method, possibly wait on method mutex */
451 Status
= AcpiDsBeginMethodExecution (MethodNode
, ObjDesc
,
453 if (ACPI_FAILURE (Status
))
455 return_ACPI_STATUS (Status
);
458 /* Begin method parse/execution. Create a new walk state */
460 NextWalkState
= AcpiDsCreateWalkState (ObjDesc
->Method
.OwnerId
,
461 NULL
, ObjDesc
, Thread
);
464 Status
= AE_NO_MEMORY
;
469 * The resolved arguments were put on the previous walk state's operand
470 * stack. Operands on the previous walk state stack always
471 * start at index 0. Also, null terminate the list of arguments
473 ThisWalkState
->Operands
[ThisWalkState
->NumOperands
] = NULL
;
476 * Allocate and initialize the evaluation information block
477 * TBD: this is somewhat inefficient, should change interface to
478 * DsInitAmlWalk. For now, keeps this struct off the CPU stack
480 Info
= ACPI_ALLOCATE_ZEROED (sizeof (ACPI_EVALUATE_INFO
));
483 return_ACPI_STATUS (AE_NO_MEMORY
);
486 Info
->Parameters
= &ThisWalkState
->Operands
[0];
487 Info
->ParameterType
= ACPI_PARAM_ARGS
;
489 Status
= AcpiDsInitAmlWalk (NextWalkState
, NULL
, MethodNode
,
490 ObjDesc
->Method
.AmlStart
, ObjDesc
->Method
.AmlLength
,
491 Info
, ACPI_IMODE_EXECUTE
);
494 if (ACPI_FAILURE (Status
))
500 * Delete the operands on the previous walkstate operand stack
501 * (they were copied to new objects)
503 for (i
= 0; i
< ObjDesc
->Method
.ParamCount
; i
++)
505 AcpiUtRemoveReference (ThisWalkState
->Operands
[i
]);
506 ThisWalkState
->Operands
[i
] = NULL
;
509 /* Clear the operand stack */
511 ThisWalkState
->NumOperands
= 0;
513 ACPI_DEBUG_PRINT ((ACPI_DB_DISPATCH
,
514 "**** Begin nested execution of [%4.4s] **** WalkState=%p\n",
515 MethodNode
->Name
.Ascii
, NextWalkState
));
517 /* Invoke an internal method if necessary */
519 if (ObjDesc
->Method
.MethodFlags
& AML_METHOD_INTERNAL_ONLY
)
521 Status
= ObjDesc
->Method
.Implementation (NextWalkState
);
524 return_ACPI_STATUS (Status
);
529 /* On error, we must terminate the method properly */
531 AcpiDsTerminateControlMethod (ObjDesc
, NextWalkState
);
534 AcpiDsDeleteWalkState (NextWalkState
);
537 return_ACPI_STATUS (Status
);
541 /*******************************************************************************
543 * FUNCTION: AcpiDsRestartControlMethod
545 * PARAMETERS: WalkState - State for preempted method (caller)
546 * ReturnDesc - Return value from the called method
550 * DESCRIPTION: Restart a method that was preempted by another (nested) method
551 * invocation. Handle the return value (if any) from the callee.
553 ******************************************************************************/
556 AcpiDsRestartControlMethod (
557 ACPI_WALK_STATE
*WalkState
,
558 ACPI_OPERAND_OBJECT
*ReturnDesc
)
561 int SameAsImplicitReturn
;
564 ACPI_FUNCTION_TRACE_PTR (DsRestartControlMethod
, WalkState
);
567 ACPI_DEBUG_PRINT ((ACPI_DB_DISPATCH
,
568 "****Restart [%4.4s] Op %p ReturnValueFromCallee %p\n",
569 AcpiUtGetNodeName (WalkState
->MethodNode
),
570 WalkState
->MethodCallOp
, ReturnDesc
));
572 ACPI_DEBUG_PRINT ((ACPI_DB_DISPATCH
,
573 " ReturnFromThisMethodUsed?=%X ResStack %p Walk %p\n",
574 WalkState
->ReturnUsed
,
575 WalkState
->Results
, WalkState
));
577 /* Did the called method return a value? */
581 /* Is the implicit return object the same as the return desc? */
583 SameAsImplicitReturn
= (WalkState
->ImplicitReturnObj
== ReturnDesc
);
585 /* Are we actually going to use the return value? */
587 if (WalkState
->ReturnUsed
)
589 /* Save the return value from the previous method */
591 Status
= AcpiDsResultPush (ReturnDesc
, WalkState
);
592 if (ACPI_FAILURE (Status
))
594 AcpiUtRemoveReference (ReturnDesc
);
595 return_ACPI_STATUS (Status
);
599 * Save as THIS method's return value in case it is returned
600 * immediately to yet another method
602 WalkState
->ReturnDesc
= ReturnDesc
;
606 * The following code is the optional support for the so-called
607 * "implicit return". Some AML code assumes that the last value of the
608 * method is "implicitly" returned to the caller, in the absence of an
609 * explicit return value.
611 * Just save the last result of the method as the return value.
613 * NOTE: this is optional because the ASL language does not actually
614 * support this behavior.
616 else if (!AcpiDsDoImplicitReturn (ReturnDesc
, WalkState
, FALSE
) ||
617 SameAsImplicitReturn
)
620 * Delete the return value if it will not be used by the
621 * calling method or remove one reference if the explicit return
622 * is the same as the implicit return value.
624 AcpiUtRemoveReference (ReturnDesc
);
628 return_ACPI_STATUS (AE_OK
);
632 /*******************************************************************************
634 * FUNCTION: AcpiDsTerminateControlMethod
636 * PARAMETERS: MethodDesc - Method object
637 * WalkState - State associated with the method
641 * DESCRIPTION: Terminate a control method. Delete everything that the method
642 * created, delete all locals and arguments, and delete the parse
645 * MUTEX: Interpreter is locked
647 ******************************************************************************/
650 AcpiDsTerminateControlMethod (
651 ACPI_OPERAND_OBJECT
*MethodDesc
,
652 ACPI_WALK_STATE
*WalkState
)
654 ACPI_NAMESPACE_NODE
*MethodNode
;
658 ACPI_FUNCTION_TRACE_PTR (DsTerminateControlMethod
, WalkState
);
661 /* MethodDesc is required, WalkState is optional */
670 /* Delete all arguments and locals */
672 AcpiDsMethodDataDeleteAll (WalkState
);
676 * If method is serialized, release the mutex and restore the
677 * current sync level for this thread
679 if (MethodDesc
->Method
.Mutex
)
681 /* Acquisition Depth handles recursive calls */
683 MethodDesc
->Method
.Mutex
->Mutex
.AcquisitionDepth
--;
684 if (!MethodDesc
->Method
.Mutex
->Mutex
.AcquisitionDepth
)
686 WalkState
->Thread
->CurrentSyncLevel
=
687 MethodDesc
->Method
.Mutex
->Mutex
.OriginalSyncLevel
;
689 AcpiOsReleaseMutex (MethodDesc
->Method
.Mutex
->Mutex
.OsMutex
);
690 MethodDesc
->Method
.Mutex
->Mutex
.OwnerThread
= NULL
;
697 * Delete any objects created by this method during execution.
698 * The method Node is stored in the walk state
700 MethodNode
= WalkState
->MethodNode
;
703 * Delete any namespace objects created anywhere within
704 * the namespace by the execution of this method
706 AcpiNsDeleteNamespaceByOwner (MethodDesc
->Method
.OwnerId
);
709 /* Decrement the thread count on the method */
711 if (MethodDesc
->Method
.ThreadCount
)
713 MethodDesc
->Method
.ThreadCount
--;
717 ACPI_ERROR ((AE_INFO
,
718 "Invalid zero thread count in method"));
721 /* Are there any other threads currently executing this method? */
723 if (MethodDesc
->Method
.ThreadCount
)
726 * Additional threads. Do not release the OwnerId in this case,
727 * we immediately reuse it for the next thread executing this method
729 ACPI_DEBUG_PRINT ((ACPI_DB_DISPATCH
,
730 "*** Completed execution of one thread, %d threads remaining\n",
731 MethodDesc
->Method
.ThreadCount
));
735 /* This is the only executing thread for this method */
738 * Support to dynamically change a method from NotSerialized to
739 * Serialized if it appears that the method is incorrectly written and
740 * does not support multiple thread execution. The best example of this
741 * is if such a method creates namespace objects and blocks. A second
742 * thread will fail with an AE_ALREADY_EXISTS exception
744 * This code is here because we must wait until the last thread exits
745 * before creating the synchronization semaphore.
747 if ((MethodDesc
->Method
.MethodFlags
& AML_METHOD_SERIALIZED
) &&
748 (!MethodDesc
->Method
.Mutex
))
750 Status
= AcpiDsCreateMethodMutex (MethodDesc
);
753 /* No more threads, we can free the OwnerId */
755 AcpiUtReleaseOwnerId (&MethodDesc
->Method
.OwnerId
);