Indentation fix, cleanup.
[AROS.git] / arch / all-pc / acpica / source / components / dispatcher / dsmethod.c
blob283ef4ca6b0aa9e196004a219e88e129f3d6c3a6
1 /******************************************************************************
3 * Module Name: dsmethod - Parser/Interpreter interface - control method parsing
5 *****************************************************************************/
7 /*
8 * Copyright (C) 2000 - 2013, Intel Corp.
9 * All rights reserved.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
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.
30 * NO WARRANTY
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 "acpi.h"
47 #include "accommon.h"
48 #include "acdispat.h"
49 #include "acinterp.h"
50 #include "acnamesp.h"
51 #include "acdisasm.h"
54 #define _COMPONENT ACPI_DISPATCHER
55 ACPI_MODULE_NAME ("dsmethod")
57 /* Local prototypes */
59 static ACPI_STATUS
60 AcpiDsCreateMethodMutex (
61 ACPI_OPERAND_OBJECT *MethodDesc);
64 /*******************************************************************************
66 * FUNCTION: AcpiDsMethodError
68 * PARAMETERS: Status - Execution status
69 * WalkState - Current state
71 * RETURN: Status
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 ******************************************************************************/
80 ACPI_STATUS
81 AcpiDsMethodError (
82 ACPI_STATUS Status,
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))
93 return (Status);
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);
124 #endif
126 return (Status);
130 /*******************************************************************************
132 * FUNCTION: AcpiDsCreateMethodMutex
134 * PARAMETERS: ObjDesc - The method object
136 * RETURN: Status
138 * DESCRIPTION: Create a mutex object for a serialized control method
140 ******************************************************************************/
142 static ACPI_STATUS
143 AcpiDsCreateMethodMutex (
144 ACPI_OPERAND_OBJECT *MethodDesc)
146 ACPI_OPERAND_OBJECT *MutexDesc;
147 ACPI_STATUS Status;
150 ACPI_FUNCTION_TRACE (DsCreateMethodMutex);
153 /* Create the new mutex object */
155 MutexDesc = AcpiUtCreateInternalObject (ACPI_TYPE_MUTEX);
156 if (!MutexDesc)
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 AcpiUtDeleteObjectDesc (MutexDesc);
167 return_ACPI_STATUS (Status);
170 MutexDesc->Mutex.SyncLevel = MethodDesc->Method.SyncLevel;
171 MethodDesc->Method.Mutex = MutexDesc;
172 return_ACPI_STATUS (AE_OK);
176 /*******************************************************************************
178 * FUNCTION: AcpiDsBeginMethodExecution
180 * PARAMETERS: MethodNode - Node of the method
181 * ObjDesc - The method object
182 * WalkState - current state, NULL if not yet executing
183 * a method.
185 * RETURN: Status
187 * DESCRIPTION: Prepare a method for execution. Parses the method if necessary,
188 * increments the thread count, and waits at the method semaphore
189 * for clearance to execute.
191 ******************************************************************************/
193 ACPI_STATUS
194 AcpiDsBeginMethodExecution (
195 ACPI_NAMESPACE_NODE *MethodNode,
196 ACPI_OPERAND_OBJECT *ObjDesc,
197 ACPI_WALK_STATE *WalkState)
199 ACPI_STATUS Status = AE_OK;
202 ACPI_FUNCTION_TRACE_PTR (DsBeginMethodExecution, MethodNode);
205 if (!MethodNode)
207 return_ACPI_STATUS (AE_NULL_ENTRY);
210 /* Prevent wraparound of thread count */
212 if (ObjDesc->Method.ThreadCount == ACPI_UINT8_MAX)
214 ACPI_ERROR ((AE_INFO,
215 "Method reached maximum reentrancy limit (255)"));
216 return_ACPI_STATUS (AE_AML_METHOD_LIMIT);
220 * If this method is serialized, we need to acquire the method mutex.
222 if (ObjDesc->Method.InfoFlags & ACPI_METHOD_SERIALIZED)
225 * Create a mutex for the method if it is defined to be Serialized
226 * and a mutex has not already been created. We defer the mutex creation
227 * until a method is actually executed, to minimize the object count
229 if (!ObjDesc->Method.Mutex)
231 Status = AcpiDsCreateMethodMutex (ObjDesc);
232 if (ACPI_FAILURE (Status))
234 return_ACPI_STATUS (Status);
239 * The CurrentSyncLevel (per-thread) must be less than or equal to
240 * the sync level of the method. This mechanism provides some
241 * deadlock prevention
243 * Top-level method invocation has no walk state at this point
245 if (WalkState &&
246 (WalkState->Thread->CurrentSyncLevel > ObjDesc->Method.Mutex->Mutex.SyncLevel))
248 ACPI_ERROR ((AE_INFO,
249 "Cannot acquire Mutex for method [%4.4s], current SyncLevel is too large (%u)",
250 AcpiUtGetNodeName (MethodNode),
251 WalkState->Thread->CurrentSyncLevel));
253 return_ACPI_STATUS (AE_AML_MUTEX_ORDER);
257 * Obtain the method mutex if necessary. Do not acquire mutex for a
258 * recursive call.
260 if (!WalkState ||
261 !ObjDesc->Method.Mutex->Mutex.ThreadId ||
262 (WalkState->Thread->ThreadId != ObjDesc->Method.Mutex->Mutex.ThreadId))
265 * Acquire the method mutex. This releases the interpreter if we
266 * block (and reacquires it before it returns)
268 Status = AcpiExSystemWaitMutex (ObjDesc->Method.Mutex->Mutex.OsMutex,
269 ACPI_WAIT_FOREVER);
270 if (ACPI_FAILURE (Status))
272 return_ACPI_STATUS (Status);
275 /* Update the mutex and walk info and save the original SyncLevel */
277 if (WalkState)
279 ObjDesc->Method.Mutex->Mutex.OriginalSyncLevel =
280 WalkState->Thread->CurrentSyncLevel;
282 ObjDesc->Method.Mutex->Mutex.ThreadId = WalkState->Thread->ThreadId;
283 WalkState->Thread->CurrentSyncLevel = ObjDesc->Method.SyncLevel;
285 else
287 ObjDesc->Method.Mutex->Mutex.OriginalSyncLevel =
288 ObjDesc->Method.Mutex->Mutex.SyncLevel;
292 /* Always increase acquisition depth */
294 ObjDesc->Method.Mutex->Mutex.AcquisitionDepth++;
298 * Allocate an Owner ID for this method, only if this is the first thread
299 * to begin concurrent execution. We only need one OwnerId, even if the
300 * method is invoked recursively.
302 if (!ObjDesc->Method.OwnerId)
304 Status = AcpiUtAllocateOwnerId (&ObjDesc->Method.OwnerId);
305 if (ACPI_FAILURE (Status))
307 goto Cleanup;
312 * Increment the method parse tree thread count since it has been
313 * reentered one more time (even if it is the same thread)
315 ObjDesc->Method.ThreadCount++;
316 AcpiMethodCount++;
317 return_ACPI_STATUS (Status);
320 Cleanup:
321 /* On error, must release the method mutex (if present) */
323 if (ObjDesc->Method.Mutex)
325 AcpiOsReleaseMutex (ObjDesc->Method.Mutex->Mutex.OsMutex);
327 return_ACPI_STATUS (Status);
331 /*******************************************************************************
333 * FUNCTION: AcpiDsCallControlMethod
335 * PARAMETERS: Thread - Info for this thread
336 * ThisWalkState - Current walk state
337 * Op - Current Op to be walked
339 * RETURN: Status
341 * DESCRIPTION: Transfer execution to a called control method
343 ******************************************************************************/
345 ACPI_STATUS
346 AcpiDsCallControlMethod (
347 ACPI_THREAD_STATE *Thread,
348 ACPI_WALK_STATE *ThisWalkState,
349 ACPI_PARSE_OBJECT *Op)
351 ACPI_STATUS Status;
352 ACPI_NAMESPACE_NODE *MethodNode;
353 ACPI_WALK_STATE *NextWalkState = NULL;
354 ACPI_OPERAND_OBJECT *ObjDesc;
355 ACPI_EVALUATE_INFO *Info;
356 UINT32 i;
359 ACPI_FUNCTION_TRACE_PTR (DsCallControlMethod, ThisWalkState);
361 ACPI_DEBUG_PRINT ((ACPI_DB_DISPATCH, "Calling method %p, currentstate=%p\n",
362 ThisWalkState->PrevOp, ThisWalkState));
365 * Get the namespace entry for the control method we are about to call
367 MethodNode = ThisWalkState->MethodCallNode;
368 if (!MethodNode)
370 return_ACPI_STATUS (AE_NULL_ENTRY);
373 ObjDesc = AcpiNsGetAttachedObject (MethodNode);
374 if (!ObjDesc)
376 return_ACPI_STATUS (AE_NULL_OBJECT);
379 /* Init for new method, possibly wait on method mutex */
381 Status = AcpiDsBeginMethodExecution (MethodNode, ObjDesc,
382 ThisWalkState);
383 if (ACPI_FAILURE (Status))
385 return_ACPI_STATUS (Status);
388 /* Begin method parse/execution. Create a new walk state */
390 NextWalkState = AcpiDsCreateWalkState (ObjDesc->Method.OwnerId,
391 NULL, ObjDesc, Thread);
392 if (!NextWalkState)
394 Status = AE_NO_MEMORY;
395 goto Cleanup;
399 * The resolved arguments were put on the previous walk state's operand
400 * stack. Operands on the previous walk state stack always
401 * start at index 0. Also, null terminate the list of arguments
403 ThisWalkState->Operands [ThisWalkState->NumOperands] = NULL;
406 * Allocate and initialize the evaluation information block
407 * TBD: this is somewhat inefficient, should change interface to
408 * DsInitAmlWalk. For now, keeps this struct off the CPU stack
410 Info = ACPI_ALLOCATE_ZEROED (sizeof (ACPI_EVALUATE_INFO));
411 if (!Info)
413 Status = AE_NO_MEMORY;
414 goto Cleanup;
417 Info->Parameters = &ThisWalkState->Operands[0];
419 Status = AcpiDsInitAmlWalk (NextWalkState, NULL, MethodNode,
420 ObjDesc->Method.AmlStart, ObjDesc->Method.AmlLength,
421 Info, ACPI_IMODE_EXECUTE);
423 ACPI_FREE (Info);
424 if (ACPI_FAILURE (Status))
426 goto Cleanup;
430 * Delete the operands on the previous walkstate operand stack
431 * (they were copied to new objects)
433 for (i = 0; i < ObjDesc->Method.ParamCount; i++)
435 AcpiUtRemoveReference (ThisWalkState->Operands [i]);
436 ThisWalkState->Operands [i] = NULL;
439 /* Clear the operand stack */
441 ThisWalkState->NumOperands = 0;
443 ACPI_DEBUG_PRINT ((ACPI_DB_DISPATCH,
444 "**** Begin nested execution of [%4.4s] **** WalkState=%p\n",
445 MethodNode->Name.Ascii, NextWalkState));
447 /* Invoke an internal method if necessary */
449 if (ObjDesc->Method.InfoFlags & ACPI_METHOD_INTERNAL_ONLY)
451 Status = ObjDesc->Method.Dispatch.Implementation (NextWalkState);
452 if (Status == AE_OK)
454 Status = AE_CTRL_TERMINATE;
458 return_ACPI_STATUS (Status);
461 Cleanup:
463 /* On error, we must terminate the method properly */
465 AcpiDsTerminateControlMethod (ObjDesc, NextWalkState);
466 if (NextWalkState)
468 AcpiDsDeleteWalkState (NextWalkState);
471 return_ACPI_STATUS (Status);
475 /*******************************************************************************
477 * FUNCTION: AcpiDsRestartControlMethod
479 * PARAMETERS: WalkState - State for preempted method (caller)
480 * ReturnDesc - Return value from the called method
482 * RETURN: Status
484 * DESCRIPTION: Restart a method that was preempted by another (nested) method
485 * invocation. Handle the return value (if any) from the callee.
487 ******************************************************************************/
489 ACPI_STATUS
490 AcpiDsRestartControlMethod (
491 ACPI_WALK_STATE *WalkState,
492 ACPI_OPERAND_OBJECT *ReturnDesc)
494 ACPI_STATUS Status;
495 int SameAsImplicitReturn;
498 ACPI_FUNCTION_TRACE_PTR (DsRestartControlMethod, WalkState);
501 ACPI_DEBUG_PRINT ((ACPI_DB_DISPATCH,
502 "****Restart [%4.4s] Op %p ReturnValueFromCallee %p\n",
503 AcpiUtGetNodeName (WalkState->MethodNode),
504 WalkState->MethodCallOp, ReturnDesc));
506 ACPI_DEBUG_PRINT ((ACPI_DB_DISPATCH,
507 " ReturnFromThisMethodUsed?=%X ResStack %p Walk %p\n",
508 WalkState->ReturnUsed,
509 WalkState->Results, WalkState));
511 /* Did the called method return a value? */
513 if (ReturnDesc)
515 /* Is the implicit return object the same as the return desc? */
517 SameAsImplicitReturn = (WalkState->ImplicitReturnObj == ReturnDesc);
519 /* Are we actually going to use the return value? */
521 if (WalkState->ReturnUsed)
523 /* Save the return value from the previous method */
525 Status = AcpiDsResultPush (ReturnDesc, WalkState);
526 if (ACPI_FAILURE (Status))
528 AcpiUtRemoveReference (ReturnDesc);
529 return_ACPI_STATUS (Status);
533 * Save as THIS method's return value in case it is returned
534 * immediately to yet another method
536 WalkState->ReturnDesc = ReturnDesc;
540 * The following code is the optional support for the so-called
541 * "implicit return". Some AML code assumes that the last value of the
542 * method is "implicitly" returned to the caller, in the absence of an
543 * explicit return value.
545 * Just save the last result of the method as the return value.
547 * NOTE: this is optional because the ASL language does not actually
548 * support this behavior.
550 else if (!AcpiDsDoImplicitReturn (ReturnDesc, WalkState, FALSE) ||
551 SameAsImplicitReturn)
554 * Delete the return value if it will not be used by the
555 * calling method or remove one reference if the explicit return
556 * is the same as the implicit return value.
558 AcpiUtRemoveReference (ReturnDesc);
562 return_ACPI_STATUS (AE_OK);
566 /*******************************************************************************
568 * FUNCTION: AcpiDsTerminateControlMethod
570 * PARAMETERS: MethodDesc - Method object
571 * WalkState - State associated with the method
573 * RETURN: None
575 * DESCRIPTION: Terminate a control method. Delete everything that the method
576 * created, delete all locals and arguments, and delete the parse
577 * tree if requested.
579 * MUTEX: Interpreter is locked
581 ******************************************************************************/
583 void
584 AcpiDsTerminateControlMethod (
585 ACPI_OPERAND_OBJECT *MethodDesc,
586 ACPI_WALK_STATE *WalkState)
589 ACPI_FUNCTION_TRACE_PTR (DsTerminateControlMethod, WalkState);
592 /* MethodDesc is required, WalkState is optional */
594 if (!MethodDesc)
596 return_VOID;
599 if (WalkState)
601 /* Delete all arguments and locals */
603 AcpiDsMethodDataDeleteAll (WalkState);
606 * If method is serialized, release the mutex and restore the
607 * current sync level for this thread
609 if (MethodDesc->Method.Mutex)
611 /* Acquisition Depth handles recursive calls */
613 MethodDesc->Method.Mutex->Mutex.AcquisitionDepth--;
614 if (!MethodDesc->Method.Mutex->Mutex.AcquisitionDepth)
616 WalkState->Thread->CurrentSyncLevel =
617 MethodDesc->Method.Mutex->Mutex.OriginalSyncLevel;
619 AcpiOsReleaseMutex (MethodDesc->Method.Mutex->Mutex.OsMutex);
620 MethodDesc->Method.Mutex->Mutex.ThreadId = 0;
625 * Delete any namespace objects created anywhere within the
626 * namespace by the execution of this method. Unless:
627 * 1) This method is a module-level executable code method, in which
628 * case we want make the objects permanent.
629 * 2) There are other threads executing the method, in which case we
630 * will wait until the last thread has completed.
632 if (!(MethodDesc->Method.InfoFlags & ACPI_METHOD_MODULE_LEVEL) &&
633 (MethodDesc->Method.ThreadCount == 1))
635 /* Delete any direct children of (created by) this method */
637 AcpiNsDeleteNamespaceSubtree (WalkState->MethodNode);
640 * Delete any objects that were created by this method
641 * elsewhere in the namespace (if any were created).
642 * Use of the ACPI_METHOD_MODIFIED_NAMESPACE optimizes the
643 * deletion such that we don't have to perform an entire
644 * namespace walk for every control method execution.
646 if (MethodDesc->Method.InfoFlags & ACPI_METHOD_MODIFIED_NAMESPACE)
648 AcpiNsDeleteNamespaceByOwner (MethodDesc->Method.OwnerId);
649 MethodDesc->Method.InfoFlags &= ~ACPI_METHOD_MODIFIED_NAMESPACE;
654 /* Decrement the thread count on the method */
656 if (MethodDesc->Method.ThreadCount)
658 MethodDesc->Method.ThreadCount--;
660 else
662 ACPI_ERROR ((AE_INFO,
663 "Invalid zero thread count in method"));
666 /* Are there any other threads currently executing this method? */
668 if (MethodDesc->Method.ThreadCount)
671 * Additional threads. Do not release the OwnerId in this case,
672 * we immediately reuse it for the next thread executing this method
674 ACPI_DEBUG_PRINT ((ACPI_DB_DISPATCH,
675 "*** Completed execution of one thread, %u threads remaining\n",
676 MethodDesc->Method.ThreadCount));
678 else
680 /* This is the only executing thread for this method */
683 * Support to dynamically change a method from NotSerialized to
684 * Serialized if it appears that the method is incorrectly written and
685 * does not support multiple thread execution. The best example of this
686 * is if such a method creates namespace objects and blocks. A second
687 * thread will fail with an AE_ALREADY_EXISTS exception.
689 * This code is here because we must wait until the last thread exits
690 * before marking the method as serialized.
692 if (MethodDesc->Method.InfoFlags & ACPI_METHOD_SERIALIZED_PENDING)
694 if (WalkState)
696 ACPI_INFO ((AE_INFO,
697 "Marking method %4.4s as Serialized because of AE_ALREADY_EXISTS error",
698 WalkState->MethodNode->Name.Ascii));
702 * Method tried to create an object twice and was marked as
703 * "pending serialized". The probable cause is that the method
704 * cannot handle reentrancy.
706 * The method was created as NotSerialized, but it tried to create
707 * a named object and then blocked, causing the second thread
708 * entrance to begin and then fail. Workaround this problem by
709 * marking the method permanently as Serialized when the last
710 * thread exits here.
712 MethodDesc->Method.InfoFlags &= ~ACPI_METHOD_SERIALIZED_PENDING;
713 MethodDesc->Method.InfoFlags |= ACPI_METHOD_SERIALIZED;
714 MethodDesc->Method.SyncLevel = 0;
717 /* No more threads, we can free the OwnerId */
719 if (!(MethodDesc->Method.InfoFlags & ACPI_METHOD_MODULE_LEVEL))
721 AcpiUtReleaseOwnerId (&MethodDesc->Method.OwnerId);
725 return_VOID;