1 /******************************************************************************
3 * Module Name: dsmethod - Parser/Interpreter interface - control method parsing
5 *****************************************************************************/
8 * Copyright (C) 2000 - 2011, Intel Corp.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions, and the following disclaimer,
16 * without modification.
17 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
18 * substantially similar to the "NO WARRANTY" disclaimer below
19 * ("Disclaimer") and any redistribution must be conditioned upon
20 * including a substantially similar Disclaimer requirement for further
21 * binary redistribution.
22 * 3. Neither the names of the above-listed copyright holders nor the names
23 * of any contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
26 * Alternatively, this software may be distributed under the terms of the
27 * GNU General Public License ("GPL") version 2 as published by the Free
28 * Software Foundation.
31 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
34 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
40 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41 * POSSIBILITY OF SUCH DAMAGES.
44 #define __DSMETHOD_C__
46 #include <contrib/dev/acpica/include/acpi.h>
47 #include <contrib/dev/acpica/include/accommon.h>
48 #include <contrib/dev/acpica/include/acdispat.h>
49 #include <contrib/dev/acpica/include/acinterp.h>
50 #include <contrib/dev/acpica/include/acnamesp.h>
51 #include <contrib/dev/acpica/include/acdisasm.h>
54 #define _COMPONENT ACPI_DISPATCHER
55 ACPI_MODULE_NAME ("dsmethod")
57 /* Local prototypes */
60 AcpiDsCreateMethodMutex (
61 ACPI_OPERAND_OBJECT *MethodDesc);
64 /*******************************************************************************
66 * FUNCTION: AcpiDsMethodError
68 * PARAMETERS: Status - Execution status
69 * WalkState - Current state
73 * DESCRIPTION: Called on method error. Invoke the global exception handler if
74 * present, dump the method data if the disassembler is configured
76 * Note: Allows the exception handler to change the status code
78 ******************************************************************************/
83 ACPI_WALK_STATE *WalkState)
85 ACPI_FUNCTION_ENTRY ();
88 /* Ignore AE_OK and control exception codes */
90 if (ACPI_SUCCESS (Status) ||
91 (Status & AE_CODE_CONTROL))
96 /* Invoke the global exception handler */
98 if (AcpiGbl_ExceptionHandler)
100 /* Exit the interpreter, allow handler to execute methods */
102 AcpiExExitInterpreter ();
105 * Handler can map the exception code to anything it wants, including
106 * AE_OK, in which case the executing method will not be aborted.
108 Status = AcpiGbl_ExceptionHandler (Status,
109 WalkState->MethodNode ?
110 WalkState->MethodNode->Name.Integer : 0,
111 WalkState->Opcode, WalkState->AmlOffset, NULL);
112 AcpiExEnterInterpreter ();
115 AcpiDsClearImplicitReturn (WalkState);
117 #ifdef ACPI_DISASSEMBLER
118 if (ACPI_FAILURE (Status))
120 /* Display method locals/args if disassembler is present */
122 AcpiDmDumpMethodInfo (Status, WalkState, WalkState->Op);
130 /*******************************************************************************
132 * FUNCTION: AcpiDsCreateMethodMutex
134 * PARAMETERS: ObjDesc - The method object
138 * DESCRIPTION: Create a mutex object for a serialized control method
140 ******************************************************************************/
143 AcpiDsCreateMethodMutex (
144 ACPI_OPERAND_OBJECT *MethodDesc)
146 ACPI_OPERAND_OBJECT *MutexDesc;
150 ACPI_FUNCTION_TRACE (DsCreateMethodMutex);
153 /* Create the new mutex object */
155 MutexDesc = AcpiUtCreateInternalObject (ACPI_TYPE_MUTEX);
158 return_ACPI_STATUS (AE_NO_MEMORY);
161 /* Create the actual OS Mutex */
163 Status = AcpiOsCreateMutex (&MutexDesc->Mutex.OsMutex);
164 if (ACPI_FAILURE (Status))
166 return_ACPI_STATUS (Status);
169 MutexDesc->Mutex.SyncLevel = MethodDesc->Method.SyncLevel;
170 MethodDesc->Method.Mutex = MutexDesc;
171 return_ACPI_STATUS (AE_OK);
175 /*******************************************************************************
177 * FUNCTION: AcpiDsBeginMethodExecution
179 * PARAMETERS: MethodNode - Node of the method
180 * ObjDesc - The method object
181 * WalkState - current state, NULL if not yet executing
186 * DESCRIPTION: Prepare a method for execution. Parses the method if necessary,
187 * increments the thread count, and waits at the method semaphore
188 * for clearance to execute.
190 ******************************************************************************/
193 AcpiDsBeginMethodExecution (
194 ACPI_NAMESPACE_NODE *MethodNode,
195 ACPI_OPERAND_OBJECT *ObjDesc,
196 ACPI_WALK_STATE *WalkState)
198 ACPI_STATUS Status = AE_OK;
201 ACPI_FUNCTION_TRACE_PTR (DsBeginMethodExecution, MethodNode);
206 return_ACPI_STATUS (AE_NULL_ENTRY);
209 /* Prevent wraparound of thread count */
211 if (ObjDesc->Method.ThreadCount == ACPI_UINT8_MAX)
213 ACPI_ERROR ((AE_INFO,
214 "Method reached maximum reentrancy limit (255)"));
215 return_ACPI_STATUS (AE_AML_METHOD_LIMIT);
219 * If this method is serialized, we need to acquire the method mutex.
221 if (ObjDesc->Method.InfoFlags & ACPI_METHOD_SERIALIZED)
224 * Create a mutex for the method if it is defined to be Serialized
225 * and a mutex has not already been created. We defer the mutex creation
226 * until a method is actually executed, to minimize the object count
228 if (!ObjDesc->Method.Mutex)
230 Status = AcpiDsCreateMethodMutex (ObjDesc);
231 if (ACPI_FAILURE (Status))
233 return_ACPI_STATUS (Status);
238 * The CurrentSyncLevel (per-thread) must be less than or equal to
239 * the sync level of the method. This mechanism provides some
240 * deadlock prevention
242 * Top-level method invocation has no walk state at this point
245 (WalkState->Thread->CurrentSyncLevel > ObjDesc->Method.Mutex->Mutex.SyncLevel))
247 ACPI_ERROR ((AE_INFO,
248 "Cannot acquire Mutex for method [%4.4s], current SyncLevel is too large (%u)",
249 AcpiUtGetNodeName (MethodNode),
250 WalkState->Thread->CurrentSyncLevel));
252 return_ACPI_STATUS (AE_AML_MUTEX_ORDER);
256 * Obtain the method mutex if necessary. Do not acquire mutex for a
260 !ObjDesc->Method.Mutex->Mutex.ThreadId ||
261 (WalkState->Thread->ThreadId != ObjDesc->Method.Mutex->Mutex.ThreadId))
264 * Acquire the method mutex. This releases the interpreter if we
265 * block (and reacquires it before it returns)
267 Status = AcpiExSystemWaitMutex (ObjDesc->Method.Mutex->Mutex.OsMutex,
269 if (ACPI_FAILURE (Status))
271 return_ACPI_STATUS (Status);
274 /* Update the mutex and walk info and save the original SyncLevel */
278 ObjDesc->Method.Mutex->Mutex.OriginalSyncLevel =
279 WalkState->Thread->CurrentSyncLevel;
281 ObjDesc->Method.Mutex->Mutex.ThreadId = WalkState->Thread->ThreadId;
282 WalkState->Thread->CurrentSyncLevel = ObjDesc->Method.SyncLevel;
286 ObjDesc->Method.Mutex->Mutex.OriginalSyncLevel =
287 ObjDesc->Method.Mutex->Mutex.SyncLevel;
291 /* Always increase acquisition depth */
293 ObjDesc->Method.Mutex->Mutex.AcquisitionDepth++;
297 * Allocate an Owner ID for this method, only if this is the first thread
298 * to begin concurrent execution. We only need one OwnerId, even if the
299 * method is invoked recursively.
301 if (!ObjDesc->Method.OwnerId)
303 Status = AcpiUtAllocateOwnerId (&ObjDesc->Method.OwnerId);
304 if (ACPI_FAILURE (Status))
311 * Increment the method parse tree thread count since it has been
312 * reentered one more time (even if it is the same thread)
314 ObjDesc->Method.ThreadCount++;
316 return_ACPI_STATUS (Status);
320 /* On error, must release the method mutex (if present) */
322 if (ObjDesc->Method.Mutex)
324 AcpiOsReleaseMutex (ObjDesc->Method.Mutex->Mutex.OsMutex);
326 return_ACPI_STATUS (Status);
330 /*******************************************************************************
332 * FUNCTION: AcpiDsCallControlMethod
334 * PARAMETERS: Thread - Info for this thread
335 * ThisWalkState - Current walk state
336 * Op - Current Op to be walked
340 * DESCRIPTION: Transfer execution to a called control method
342 ******************************************************************************/
345 AcpiDsCallControlMethod (
346 ACPI_THREAD_STATE *Thread,
347 ACPI_WALK_STATE *ThisWalkState,
348 ACPI_PARSE_OBJECT *Op)
351 ACPI_NAMESPACE_NODE *MethodNode;
352 ACPI_WALK_STATE *NextWalkState = NULL;
353 ACPI_OPERAND_OBJECT *ObjDesc;
354 ACPI_EVALUATE_INFO *Info;
358 ACPI_FUNCTION_TRACE_PTR (DsCallControlMethod, ThisWalkState);
360 ACPI_DEBUG_PRINT ((ACPI_DB_DISPATCH, "Calling method %p, currentstate=%p\n",
361 ThisWalkState->PrevOp, ThisWalkState));
364 * Get the namespace entry for the control method we are about to call
366 MethodNode = ThisWalkState->MethodCallNode;
369 return_ACPI_STATUS (AE_NULL_ENTRY);
372 ObjDesc = AcpiNsGetAttachedObject (MethodNode);
375 return_ACPI_STATUS (AE_NULL_OBJECT);
378 /* Init for new method, possibly wait on method mutex */
380 Status = AcpiDsBeginMethodExecution (MethodNode, ObjDesc,
382 if (ACPI_FAILURE (Status))
384 return_ACPI_STATUS (Status);
387 /* Begin method parse/execution. Create a new walk state */
389 NextWalkState = AcpiDsCreateWalkState (ObjDesc->Method.OwnerId,
390 NULL, ObjDesc, Thread);
393 Status = AE_NO_MEMORY;
398 * The resolved arguments were put on the previous walk state's operand
399 * stack. Operands on the previous walk state stack always
400 * start at index 0. Also, null terminate the list of arguments
402 ThisWalkState->Operands [ThisWalkState->NumOperands] = NULL;
405 * Allocate and initialize the evaluation information block
406 * TBD: this is somewhat inefficient, should change interface to
407 * DsInitAmlWalk. For now, keeps this struct off the CPU stack
409 Info = ACPI_ALLOCATE_ZEROED (sizeof (ACPI_EVALUATE_INFO));
412 return_ACPI_STATUS (AE_NO_MEMORY);
415 Info->Parameters = &ThisWalkState->Operands[0];
417 Status = AcpiDsInitAmlWalk (NextWalkState, NULL, MethodNode,
418 ObjDesc->Method.AmlStart, ObjDesc->Method.AmlLength,
419 Info, ACPI_IMODE_EXECUTE);
422 if (ACPI_FAILURE (Status))
428 * Delete the operands on the previous walkstate operand stack
429 * (they were copied to new objects)
431 for (i = 0; i < ObjDesc->Method.ParamCount; i++)
433 AcpiUtRemoveReference (ThisWalkState->Operands [i]);
434 ThisWalkState->Operands [i] = NULL;
437 /* Clear the operand stack */
439 ThisWalkState->NumOperands = 0;
441 ACPI_DEBUG_PRINT ((ACPI_DB_DISPATCH,
442 "**** Begin nested execution of [%4.4s] **** WalkState=%p\n",
443 MethodNode->Name.Ascii, NextWalkState));
445 /* Invoke an internal method if necessary */
447 if (ObjDesc->Method.InfoFlags & ACPI_METHOD_INTERNAL_ONLY)
449 Status = ObjDesc->Method.Dispatch.Implementation (NextWalkState);
452 Status = AE_CTRL_TERMINATE;
456 return_ACPI_STATUS (Status);
461 /* On error, we must terminate the method properly */
463 AcpiDsTerminateControlMethod (ObjDesc, NextWalkState);
466 AcpiDsDeleteWalkState (NextWalkState);
469 return_ACPI_STATUS (Status);
473 /*******************************************************************************
475 * FUNCTION: AcpiDsRestartControlMethod
477 * PARAMETERS: WalkState - State for preempted method (caller)
478 * ReturnDesc - Return value from the called method
482 * DESCRIPTION: Restart a method that was preempted by another (nested) method
483 * invocation. Handle the return value (if any) from the callee.
485 ******************************************************************************/
488 AcpiDsRestartControlMethod (
489 ACPI_WALK_STATE *WalkState,
490 ACPI_OPERAND_OBJECT *ReturnDesc)
493 int SameAsImplicitReturn;
496 ACPI_FUNCTION_TRACE_PTR (DsRestartControlMethod, WalkState);
499 ACPI_DEBUG_PRINT ((ACPI_DB_DISPATCH,
500 "****Restart [%4.4s] Op %p ReturnValueFromCallee %p\n",
501 AcpiUtGetNodeName (WalkState->MethodNode),
502 WalkState->MethodCallOp, ReturnDesc));
504 ACPI_DEBUG_PRINT ((ACPI_DB_DISPATCH,
505 " ReturnFromThisMethodUsed?=%X ResStack %p Walk %p\n",
506 WalkState->ReturnUsed,
507 WalkState->Results, WalkState));
509 /* Did the called method return a value? */
513 /* Is the implicit return object the same as the return desc? */
515 SameAsImplicitReturn = (WalkState->ImplicitReturnObj == ReturnDesc);
517 /* Are we actually going to use the return value? */
519 if (WalkState->ReturnUsed)
521 /* Save the return value from the previous method */
523 Status = AcpiDsResultPush (ReturnDesc, WalkState);
524 if (ACPI_FAILURE (Status))
526 AcpiUtRemoveReference (ReturnDesc);
527 return_ACPI_STATUS (Status);
531 * Save as THIS method's return value in case it is returned
532 * immediately to yet another method
534 WalkState->ReturnDesc = ReturnDesc;
538 * The following code is the optional support for the so-called
539 * "implicit return". Some AML code assumes that the last value of the
540 * method is "implicitly" returned to the caller, in the absence of an
541 * explicit return value.
543 * Just save the last result of the method as the return value.
545 * NOTE: this is optional because the ASL language does not actually
546 * support this behavior.
548 else if (!AcpiDsDoImplicitReturn (ReturnDesc, WalkState, FALSE) ||
549 SameAsImplicitReturn)
552 * Delete the return value if it will not be used by the
553 * calling method or remove one reference if the explicit return
554 * is the same as the implicit return value.
556 AcpiUtRemoveReference (ReturnDesc);
560 return_ACPI_STATUS (AE_OK);
564 /*******************************************************************************
566 * FUNCTION: AcpiDsTerminateControlMethod
568 * PARAMETERS: MethodDesc - Method object
569 * WalkState - State associated with the method
573 * DESCRIPTION: Terminate a control method. Delete everything that the method
574 * created, delete all locals and arguments, and delete the parse
577 * MUTEX: Interpreter is locked
579 ******************************************************************************/
582 AcpiDsTerminateControlMethod (
583 ACPI_OPERAND_OBJECT *MethodDesc,
584 ACPI_WALK_STATE *WalkState)
587 ACPI_FUNCTION_TRACE_PTR (DsTerminateControlMethod, WalkState);
590 /* MethodDesc is required, WalkState is optional */
599 /* Delete all arguments and locals */
601 AcpiDsMethodDataDeleteAll (WalkState);
604 * If method is serialized, release the mutex and restore the
605 * current sync level for this thread
607 if (MethodDesc->Method.Mutex)
609 /* Acquisition Depth handles recursive calls */
611 MethodDesc->Method.Mutex->Mutex.AcquisitionDepth--;
612 if (!MethodDesc->Method.Mutex->Mutex.AcquisitionDepth)
614 WalkState->Thread->CurrentSyncLevel =
615 MethodDesc->Method.Mutex->Mutex.OriginalSyncLevel;
617 AcpiOsReleaseMutex (MethodDesc->Method.Mutex->Mutex.OsMutex);
618 MethodDesc->Method.Mutex->Mutex.ThreadId = 0;
623 * Delete any namespace objects created anywhere within the
624 * namespace by the execution of this method. Unless:
625 * 1) This method is a module-level executable code method, in which
626 * case we want make the objects permanent.
627 * 2) There are other threads executing the method, in which case we
628 * will wait until the last thread has completed.
630 if (!(MethodDesc->Method.InfoFlags & ACPI_METHOD_MODULE_LEVEL) &&
631 (MethodDesc->Method.ThreadCount == 1))
633 /* Delete any direct children of (created by) this method */
635 AcpiNsDeleteNamespaceSubtree (WalkState->MethodNode);
638 * Delete any objects that were created by this method
639 * elsewhere in the namespace (if any were created).
640 * Use of the ACPI_METHOD_MODIFIED_NAMESPACE optimizes the
641 * deletion such that we don't have to perform an entire
642 * namespace walk for every control method execution.
644 if (MethodDesc->Method.InfoFlags & ACPI_METHOD_MODIFIED_NAMESPACE)
646 AcpiNsDeleteNamespaceByOwner (MethodDesc->Method.OwnerId);
647 MethodDesc->Method.InfoFlags &= ~ACPI_METHOD_MODIFIED_NAMESPACE;
652 /* Decrement the thread count on the method */
654 if (MethodDesc->Method.ThreadCount)
656 MethodDesc->Method.ThreadCount--;
660 ACPI_ERROR ((AE_INFO,
661 "Invalid zero thread count in method"));
664 /* Are there any other threads currently executing this method? */
666 if (MethodDesc->Method.ThreadCount)
669 * Additional threads. Do not release the OwnerId in this case,
670 * we immediately reuse it for the next thread executing this method
672 ACPI_DEBUG_PRINT ((ACPI_DB_DISPATCH,
673 "*** Completed execution of one thread, %u threads remaining\n",
674 MethodDesc->Method.ThreadCount));
678 /* This is the only executing thread for this method */
681 * Support to dynamically change a method from NotSerialized to
682 * Serialized if it appears that the method is incorrectly written and
683 * does not support multiple thread execution. The best example of this
684 * is if such a method creates namespace objects and blocks. A second
685 * thread will fail with an AE_ALREADY_EXISTS exception.
687 * This code is here because we must wait until the last thread exits
688 * before marking the method as serialized.
690 if (MethodDesc->Method.InfoFlags & ACPI_METHOD_SERIALIZED_PENDING)
695 "Marking method %4.4s as Serialized because of AE_ALREADY_EXISTS error",
696 WalkState->MethodNode->Name.Ascii));
700 * Method tried to create an object twice and was marked as
701 * "pending serialized". The probable cause is that the method
702 * cannot handle reentrancy.
704 * The method was created as NotSerialized, but it tried to create
705 * a named object and then blocked, causing the second thread
706 * entrance to begin and then fail. Workaround this problem by
707 * marking the method permanently as Serialized when the last
710 MethodDesc->Method.InfoFlags &= ~ACPI_METHOD_SERIALIZED_PENDING;
711 MethodDesc->Method.InfoFlags |= ACPI_METHOD_SERIALIZED;
712 MethodDesc->Method.SyncLevel = 0;
715 /* No more threads, we can free the OwnerId */
717 if (!(MethodDesc->Method.InfoFlags & ACPI_METHOD_MODULE_LEVEL))
719 AcpiUtReleaseOwnerId (&MethodDesc->Method.OwnerId);