43ee55d76f95a5933e720210c007b88180c5170f
[reactos.git] / reactos / drivers / bus / acpi / acpica / dispatcher / dsmethod.c
1 /******************************************************************************
2 *
3 * Module Name: dsmethod - Parser/Interpreter interface - control method parsing
4 *
5 *****************************************************************************/
6
7 /******************************************************************************
8 *
9 * 1. Copyright Notice
10 *
11 * Some or all of this work - Copyright (c) 1999 - 2015, Intel Corp.
12 * All rights reserved.
13 *
14 * 2. License
15 *
16 * 2.1. This is your license from Intel Corp. under its intellectual property
17 * rights. You may have additional license terms from the party that provided
18 * you this software, covering your right to use that party's intellectual
19 * property rights.
20 *
21 * 2.2. Intel grants, free of charge, to any person ("Licensee") obtaining a
22 * copy of the source code appearing in this file ("Covered Code") an
23 * irrevocable, perpetual, worldwide license under Intel's copyrights in the
24 * base code distributed originally by Intel ("Original Intel Code") to copy,
25 * make derivatives, distribute, use and display any portion of the Covered
26 * Code in any form, with the right to sublicense such rights; and
27 *
28 * 2.3. Intel grants Licensee a non-exclusive and non-transferable patent
29 * license (with the right to sublicense), under only those claims of Intel
30 * patents that are infringed by the Original Intel Code, to make, use, sell,
31 * offer to sell, and import the Covered Code and derivative works thereof
32 * solely to the minimum extent necessary to exercise the above copyright
33 * license, and in no event shall the patent license extend to any additions
34 * to or modifications of the Original Intel Code. No other license or right
35 * is granted directly or by implication, estoppel or otherwise;
36 *
37 * The above copyright and patent license is granted only if the following
38 * conditions are met:
39 *
40 * 3. Conditions
41 *
42 * 3.1. Redistribution of Source with Rights to Further Distribute Source.
43 * Redistribution of source code of any substantial portion of the Covered
44 * Code or modification with rights to further distribute source must include
45 * the above Copyright Notice, the above License, this list of Conditions,
46 * and the following Disclaimer and Export Compliance provision. In addition,
47 * Licensee must cause all Covered Code to which Licensee contributes to
48 * contain a file documenting the changes Licensee made to create that Covered
49 * Code and the date of any change. Licensee must include in that file the
50 * documentation of any changes made by any predecessor Licensee. Licensee
51 * must include a prominent statement that the modification is derived,
52 * directly or indirectly, from Original Intel Code.
53 *
54 * 3.2. Redistribution of Source with no Rights to Further Distribute Source.
55 * Redistribution of source code of any substantial portion of the Covered
56 * Code or modification without rights to further distribute source must
57 * include the following Disclaimer and Export Compliance provision in the
58 * documentation and/or other materials provided with distribution. In
59 * addition, Licensee may not authorize further sublicense of source of any
60 * portion of the Covered Code, and must include terms to the effect that the
61 * license from Licensee to its licensee is limited to the intellectual
62 * property embodied in the software Licensee provides to its licensee, and
63 * not to intellectual property embodied in modifications its licensee may
64 * make.
65 *
66 * 3.3. Redistribution of Executable. Redistribution in executable form of any
67 * substantial portion of the Covered Code or modification must reproduce the
68 * above Copyright Notice, and the following Disclaimer and Export Compliance
69 * provision in the documentation and/or other materials provided with the
70 * distribution.
71 *
72 * 3.4. Intel retains all right, title, and interest in and to the Original
73 * Intel Code.
74 *
75 * 3.5. Neither the name Intel nor any other trademark owned or controlled by
76 * Intel shall be used in advertising or otherwise to promote the sale, use or
77 * other dealings in products derived from or relating to the Covered Code
78 * without prior written authorization from Intel.
79 *
80 * 4. Disclaimer and Export Compliance
81 *
82 * 4.1. INTEL MAKES NO WARRANTY OF ANY KIND REGARDING ANY SOFTWARE PROVIDED
83 * HERE. ANY SOFTWARE ORIGINATING FROM INTEL OR DERIVED FROM INTEL SOFTWARE
84 * IS PROVIDED "AS IS," AND INTEL WILL NOT PROVIDE ANY SUPPORT, ASSISTANCE,
85 * INSTALLATION, TRAINING OR OTHER SERVICES. INTEL WILL NOT PROVIDE ANY
86 * UPDATES, ENHANCEMENTS OR EXTENSIONS. INTEL SPECIFICALLY DISCLAIMS ANY
87 * IMPLIED WARRANTIES OF MERCHANTABILITY, NONINFRINGEMENT AND FITNESS FOR A
88 * PARTICULAR PURPOSE.
89 *
90 * 4.2. IN NO EVENT SHALL INTEL HAVE ANY LIABILITY TO LICENSEE, ITS LICENSEES
91 * OR ANY OTHER THIRD PARTY, FOR ANY LOST PROFITS, LOST DATA, LOSS OF USE OR
92 * COSTS OF PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, OR FOR ANY INDIRECT,
93 * SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THIS AGREEMENT, UNDER ANY
94 * CAUSE OF ACTION OR THEORY OF LIABILITY, AND IRRESPECTIVE OF WHETHER INTEL
95 * HAS ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES. THESE LIMITATIONS
96 * SHALL APPLY NOTWITHSTANDING THE FAILURE OF THE ESSENTIAL PURPOSE OF ANY
97 * LIMITED REMEDY.
98 *
99 * 4.3. Licensee shall not export, either directly or indirectly, any of this
100 * software or system incorporating such software without first obtaining any
101 * required license or other approval from the U. S. Department of Commerce or
102 * any other agency or department of the United States Government. In the
103 * event Licensee exports any such software from the United States or
104 * re-exports any such software from a foreign destination, Licensee shall
105 * ensure that the distribution and export/re-export of the software is in
106 * compliance with all laws, regulations, orders, or other restrictions of the
107 * U.S. Export Administration Regulations. Licensee agrees that neither it nor
108 * any of its subsidiaries will export/re-export any technical data, process,
109 * software, or service, directly or indirectly, to any country for which the
110 * United States government or any agency thereof requires an export license,
111 * other governmental approval, or letter of assurance, without first obtaining
112 * such license, approval or letter.
113 *
114 *****************************************************************************/
115
116 #include "acpi.h"
117 #include "accommon.h"
118 #include "acdispat.h"
119 #include "acinterp.h"
120 #include "acnamesp.h"
121 #include "acdisasm.h"
122 #include "acparser.h"
123 #include "amlcode.h"
124
125
126 #define _COMPONENT ACPI_DISPATCHER
127 ACPI_MODULE_NAME ("dsmethod")
128
129 /* Local prototypes */
130
131 static ACPI_STATUS
132 AcpiDsDetectNamedOpcodes (
133 ACPI_WALK_STATE *WalkState,
134 ACPI_PARSE_OBJECT **OutOp);
135
136 static ACPI_STATUS
137 AcpiDsCreateMethodMutex (
138 ACPI_OPERAND_OBJECT *MethodDesc);
139
140
141 /*******************************************************************************
142 *
143 * FUNCTION: AcpiDsAutoSerializeMethod
144 *
145 * PARAMETERS: Node - Namespace Node of the method
146 * ObjDesc - Method object attached to node
147 *
148 * RETURN: Status
149 *
150 * DESCRIPTION: Parse a control method AML to scan for control methods that
151 * need serialization due to the creation of named objects.
152 *
153 * NOTE: It is a bit of overkill to mark all such methods serialized, since
154 * there is only a problem if the method actually blocks during execution.
155 * A blocking operation is, for example, a Sleep() operation, or any access
156 * to an operation region. However, it is probably not possible to easily
157 * detect whether a method will block or not, so we simply mark all suspicious
158 * methods as serialized.
159 *
160 * NOTE2: This code is essentially a generic routine for parsing a single
161 * control method.
162 *
163 ******************************************************************************/
164
165 ACPI_STATUS
166 AcpiDsAutoSerializeMethod (
167 ACPI_NAMESPACE_NODE *Node,
168 ACPI_OPERAND_OBJECT *ObjDesc)
169 {
170 ACPI_STATUS Status;
171 ACPI_PARSE_OBJECT *Op = NULL;
172 ACPI_WALK_STATE *WalkState;
173
174
175 ACPI_FUNCTION_TRACE_PTR (DsAutoSerializeMethod, Node);
176
177
178 ACPI_DEBUG_PRINT ((ACPI_DB_PARSE,
179 "Method auto-serialization parse [%4.4s] %p\n",
180 AcpiUtGetNodeName (Node), Node));
181
182 /* Create/Init a root op for the method parse tree */
183
184 Op = AcpiPsAllocOp (AML_METHOD_OP);
185 if (!Op)
186 {
187 return_ACPI_STATUS (AE_NO_MEMORY);
188 }
189
190 AcpiPsSetName (Op, Node->Name.Integer);
191 Op->Common.Node = Node;
192
193 /* Create and initialize a new walk state */
194
195 WalkState = AcpiDsCreateWalkState (Node->OwnerId, NULL, NULL, NULL);
196 if (!WalkState)
197 {
198 return_ACPI_STATUS (AE_NO_MEMORY);
199 }
200
201 Status = AcpiDsInitAmlWalk (WalkState, Op, Node, ObjDesc->Method.AmlStart,
202 ObjDesc->Method.AmlLength, NULL, 0);
203 if (ACPI_FAILURE (Status))
204 {
205 AcpiDsDeleteWalkState (WalkState);
206 return_ACPI_STATUS (Status);
207 }
208
209 WalkState->DescendingCallback = AcpiDsDetectNamedOpcodes;
210
211 /* Parse the method, scan for creation of named objects */
212
213 Status = AcpiPsParseAml (WalkState);
214 if (ACPI_FAILURE (Status))
215 {
216 return_ACPI_STATUS (Status);
217 }
218
219 AcpiPsDeleteParseTree (Op);
220 return_ACPI_STATUS (Status);
221 }
222
223
224 /*******************************************************************************
225 *
226 * FUNCTION: AcpiDsDetectNamedOpcodes
227 *
228 * PARAMETERS: WalkState - Current state of the parse tree walk
229 * OutOp - Unused, required for parser interface
230 *
231 * RETURN: Status
232 *
233 * DESCRIPTION: Descending callback used during the loading of ACPI tables.
234 * Currently used to detect methods that must be marked serialized
235 * in order to avoid problems with the creation of named objects.
236 *
237 ******************************************************************************/
238
239 static ACPI_STATUS
240 AcpiDsDetectNamedOpcodes (
241 ACPI_WALK_STATE *WalkState,
242 ACPI_PARSE_OBJECT **OutOp)
243 {
244
245 ACPI_FUNCTION_NAME (AcpiDsDetectNamedOpcodes);
246
247
248 /* We are only interested in opcodes that create a new name */
249
250 if (!(WalkState->OpInfo->Flags & (AML_NAMED | AML_CREATE | AML_FIELD)))
251 {
252 return (AE_OK);
253 }
254
255 /*
256 * At this point, we know we have a Named object opcode.
257 * Mark the method as serialized. Later code will create a mutex for
258 * this method to enforce serialization.
259 *
260 * Note, ACPI_METHOD_IGNORE_SYNC_LEVEL flag means that we will ignore the
261 * Sync Level mechanism for this method, even though it is now serialized.
262 * Otherwise, there can be conflicts with existing ASL code that actually
263 * uses sync levels.
264 */
265 WalkState->MethodDesc->Method.SyncLevel = 0;
266 WalkState->MethodDesc->Method.InfoFlags |=
267 (ACPI_METHOD_SERIALIZED | ACPI_METHOD_IGNORE_SYNC_LEVEL);
268
269 ACPI_DEBUG_PRINT ((ACPI_DB_INFO,
270 "Method serialized [%4.4s] %p - [%s] (%4.4X)\n",
271 WalkState->MethodNode->Name.Ascii, WalkState->MethodNode,
272 WalkState->OpInfo->Name, WalkState->Opcode));
273
274 /* Abort the parse, no need to examine this method any further */
275
276 return (AE_CTRL_TERMINATE);
277 }
278
279
280 /*******************************************************************************
281 *
282 * FUNCTION: AcpiDsMethodError
283 *
284 * PARAMETERS: Status - Execution status
285 * WalkState - Current state
286 *
287 * RETURN: Status
288 *
289 * DESCRIPTION: Called on method error. Invoke the global exception handler if
290 * present, dump the method data if the disassembler is configured
291 *
292 * Note: Allows the exception handler to change the status code
293 *
294 ******************************************************************************/
295
296 ACPI_STATUS
297 AcpiDsMethodError (
298 ACPI_STATUS Status,
299 ACPI_WALK_STATE *WalkState)
300 {
301 ACPI_FUNCTION_ENTRY ();
302
303
304 /* Ignore AE_OK and control exception codes */
305
306 if (ACPI_SUCCESS (Status) ||
307 (Status & AE_CODE_CONTROL))
308 {
309 return (Status);
310 }
311
312 /* Invoke the global exception handler */
313
314 if (AcpiGbl_ExceptionHandler)
315 {
316 /* Exit the interpreter, allow handler to execute methods */
317
318 AcpiExExitInterpreter ();
319
320 /*
321 * Handler can map the exception code to anything it wants, including
322 * AE_OK, in which case the executing method will not be aborted.
323 */
324 Status = AcpiGbl_ExceptionHandler (Status,
325 WalkState->MethodNode ?
326 WalkState->MethodNode->Name.Integer : 0,
327 WalkState->Opcode, WalkState->AmlOffset, NULL);
328 AcpiExEnterInterpreter ();
329 }
330
331 AcpiDsClearImplicitReturn (WalkState);
332
333 #ifdef ACPI_DISASSEMBLER
334 if (ACPI_FAILURE (Status))
335 {
336 /* Display method locals/args if disassembler is present */
337
338 AcpiDmDumpMethodInfo (Status, WalkState, WalkState->Op);
339 }
340 #endif
341
342 return (Status);
343 }
344
345
346 /*******************************************************************************
347 *
348 * FUNCTION: AcpiDsCreateMethodMutex
349 *
350 * PARAMETERS: ObjDesc - The method object
351 *
352 * RETURN: Status
353 *
354 * DESCRIPTION: Create a mutex object for a serialized control method
355 *
356 ******************************************************************************/
357
358 static ACPI_STATUS
359 AcpiDsCreateMethodMutex (
360 ACPI_OPERAND_OBJECT *MethodDesc)
361 {
362 ACPI_OPERAND_OBJECT *MutexDesc;
363 ACPI_STATUS Status;
364
365
366 ACPI_FUNCTION_TRACE (DsCreateMethodMutex);
367
368
369 /* Create the new mutex object */
370
371 MutexDesc = AcpiUtCreateInternalObject (ACPI_TYPE_MUTEX);
372 if (!MutexDesc)
373 {
374 return_ACPI_STATUS (AE_NO_MEMORY);
375 }
376
377 /* Create the actual OS Mutex */
378
379 Status = AcpiOsCreateMutex (&MutexDesc->Mutex.OsMutex);
380 if (ACPI_FAILURE (Status))
381 {
382 AcpiUtDeleteObjectDesc (MutexDesc);
383 return_ACPI_STATUS (Status);
384 }
385
386 MutexDesc->Mutex.SyncLevel = MethodDesc->Method.SyncLevel;
387 MethodDesc->Method.Mutex = MutexDesc;
388 return_ACPI_STATUS (AE_OK);
389 }
390
391
392 /*******************************************************************************
393 *
394 * FUNCTION: AcpiDsBeginMethodExecution
395 *
396 * PARAMETERS: MethodNode - Node of the method
397 * ObjDesc - The method object
398 * WalkState - current state, NULL if not yet executing
399 * a method.
400 *
401 * RETURN: Status
402 *
403 * DESCRIPTION: Prepare a method for execution. Parses the method if necessary,
404 * increments the thread count, and waits at the method semaphore
405 * for clearance to execute.
406 *
407 ******************************************************************************/
408
409 ACPI_STATUS
410 AcpiDsBeginMethodExecution (
411 ACPI_NAMESPACE_NODE *MethodNode,
412 ACPI_OPERAND_OBJECT *ObjDesc,
413 ACPI_WALK_STATE *WalkState)
414 {
415 ACPI_STATUS Status = AE_OK;
416
417
418 ACPI_FUNCTION_TRACE_PTR (DsBeginMethodExecution, MethodNode);
419
420
421 if (!MethodNode)
422 {
423 return_ACPI_STATUS (AE_NULL_ENTRY);
424 }
425
426 /* Prevent wraparound of thread count */
427
428 if (ObjDesc->Method.ThreadCount == ACPI_UINT8_MAX)
429 {
430 ACPI_ERROR ((AE_INFO,
431 "Method reached maximum reentrancy limit (255)"));
432 return_ACPI_STATUS (AE_AML_METHOD_LIMIT);
433 }
434
435 /*
436 * If this method is serialized, we need to acquire the method mutex.
437 */
438 if (ObjDesc->Method.InfoFlags & ACPI_METHOD_SERIALIZED)
439 {
440 /*
441 * Create a mutex for the method if it is defined to be Serialized
442 * and a mutex has not already been created. We defer the mutex creation
443 * until a method is actually executed, to minimize the object count
444 */
445 if (!ObjDesc->Method.Mutex)
446 {
447 Status = AcpiDsCreateMethodMutex (ObjDesc);
448 if (ACPI_FAILURE (Status))
449 {
450 return_ACPI_STATUS (Status);
451 }
452 }
453
454 /*
455 * The CurrentSyncLevel (per-thread) must be less than or equal to
456 * the sync level of the method. This mechanism provides some
457 * deadlock prevention.
458 *
459 * If the method was auto-serialized, we just ignore the sync level
460 * mechanism, because auto-serialization of methods can interfere
461 * with ASL code that actually uses sync levels.
462 *
463 * Top-level method invocation has no walk state at this point
464 */
465 if (WalkState &&
466 (!(ObjDesc->Method.InfoFlags & ACPI_METHOD_IGNORE_SYNC_LEVEL)) &&
467 (WalkState->Thread->CurrentSyncLevel > ObjDesc->Method.Mutex->Mutex.SyncLevel))
468 {
469 ACPI_ERROR ((AE_INFO,
470 "Cannot acquire Mutex for method [%4.4s], current SyncLevel is too large (%u)",
471 AcpiUtGetNodeName (MethodNode),
472 WalkState->Thread->CurrentSyncLevel));
473
474 return_ACPI_STATUS (AE_AML_MUTEX_ORDER);
475 }
476
477 /*
478 * Obtain the method mutex if necessary. Do not acquire mutex for a
479 * recursive call.
480 */
481 if (!WalkState ||
482 !ObjDesc->Method.Mutex->Mutex.ThreadId ||
483 (WalkState->Thread->ThreadId != ObjDesc->Method.Mutex->Mutex.ThreadId))
484 {
485 /*
486 * Acquire the method mutex. This releases the interpreter if we
487 * block (and reacquires it before it returns)
488 */
489 Status = AcpiExSystemWaitMutex (ObjDesc->Method.Mutex->Mutex.OsMutex,
490 ACPI_WAIT_FOREVER);
491 if (ACPI_FAILURE (Status))
492 {
493 return_ACPI_STATUS (Status);
494 }
495
496 /* Update the mutex and walk info and save the original SyncLevel */
497
498 if (WalkState)
499 {
500 ObjDesc->Method.Mutex->Mutex.OriginalSyncLevel =
501 WalkState->Thread->CurrentSyncLevel;
502
503 ObjDesc->Method.Mutex->Mutex.ThreadId = WalkState->Thread->ThreadId;
504 WalkState->Thread->CurrentSyncLevel = ObjDesc->Method.SyncLevel;
505 }
506 else
507 {
508 ObjDesc->Method.Mutex->Mutex.OriginalSyncLevel =
509 ObjDesc->Method.Mutex->Mutex.SyncLevel;
510 }
511 }
512
513 /* Always increase acquisition depth */
514
515 ObjDesc->Method.Mutex->Mutex.AcquisitionDepth++;
516 }
517
518 /*
519 * Allocate an Owner ID for this method, only if this is the first thread
520 * to begin concurrent execution. We only need one OwnerId, even if the
521 * method is invoked recursively.
522 */
523 if (!ObjDesc->Method.OwnerId)
524 {
525 Status = AcpiUtAllocateOwnerId (&ObjDesc->Method.OwnerId);
526 if (ACPI_FAILURE (Status))
527 {
528 goto Cleanup;
529 }
530 }
531
532 /*
533 * Increment the method parse tree thread count since it has been
534 * reentered one more time (even if it is the same thread)
535 */
536 ObjDesc->Method.ThreadCount++;
537 AcpiMethodCount++;
538 return_ACPI_STATUS (Status);
539
540
541 Cleanup:
542 /* On error, must release the method mutex (if present) */
543
544 if (ObjDesc->Method.Mutex)
545 {
546 AcpiOsReleaseMutex (ObjDesc->Method.Mutex->Mutex.OsMutex);
547 }
548 return_ACPI_STATUS (Status);
549 }
550
551
552 /*******************************************************************************
553 *
554 * FUNCTION: AcpiDsCallControlMethod
555 *
556 * PARAMETERS: Thread - Info for this thread
557 * ThisWalkState - Current walk state
558 * Op - Current Op to be walked
559 *
560 * RETURN: Status
561 *
562 * DESCRIPTION: Transfer execution to a called control method
563 *
564 ******************************************************************************/
565
566 ACPI_STATUS
567 AcpiDsCallControlMethod (
568 ACPI_THREAD_STATE *Thread,
569 ACPI_WALK_STATE *ThisWalkState,
570 ACPI_PARSE_OBJECT *Op)
571 {
572 ACPI_STATUS Status;
573 ACPI_NAMESPACE_NODE *MethodNode;
574 ACPI_WALK_STATE *NextWalkState = NULL;
575 ACPI_OPERAND_OBJECT *ObjDesc;
576 ACPI_EVALUATE_INFO *Info;
577 UINT32 i;
578
579
580 ACPI_FUNCTION_TRACE_PTR (DsCallControlMethod, ThisWalkState);
581
582 ACPI_DEBUG_PRINT ((ACPI_DB_DISPATCH, "Calling method %p, currentstate=%p\n",
583 ThisWalkState->PrevOp, ThisWalkState));
584
585 /*
586 * Get the namespace entry for the control method we are about to call
587 */
588 MethodNode = ThisWalkState->MethodCallNode;
589 if (!MethodNode)
590 {
591 return_ACPI_STATUS (AE_NULL_ENTRY);
592 }
593
594 ObjDesc = AcpiNsGetAttachedObject (MethodNode);
595 if (!ObjDesc)
596 {
597 return_ACPI_STATUS (AE_NULL_OBJECT);
598 }
599
600 /* Init for new method, possibly wait on method mutex */
601
602 Status = AcpiDsBeginMethodExecution (MethodNode, ObjDesc,
603 ThisWalkState);
604 if (ACPI_FAILURE (Status))
605 {
606 return_ACPI_STATUS (Status);
607 }
608
609 /* Begin method parse/execution. Create a new walk state */
610
611 NextWalkState = AcpiDsCreateWalkState (ObjDesc->Method.OwnerId,
612 NULL, ObjDesc, Thread);
613 if (!NextWalkState)
614 {
615 Status = AE_NO_MEMORY;
616 goto Cleanup;
617 }
618
619 /*
620 * The resolved arguments were put on the previous walk state's operand
621 * stack. Operands on the previous walk state stack always
622 * start at index 0. Also, null terminate the list of arguments
623 */
624 ThisWalkState->Operands [ThisWalkState->NumOperands] = NULL;
625
626 /*
627 * Allocate and initialize the evaluation information block
628 * TBD: this is somewhat inefficient, should change interface to
629 * DsInitAmlWalk. For now, keeps this struct off the CPU stack
630 */
631 Info = ACPI_ALLOCATE_ZEROED (sizeof (ACPI_EVALUATE_INFO));
632 if (!Info)
633 {
634 Status = AE_NO_MEMORY;
635 goto Cleanup;
636 }
637
638 Info->Parameters = &ThisWalkState->Operands[0];
639
640 Status = AcpiDsInitAmlWalk (NextWalkState, NULL, MethodNode,
641 ObjDesc->Method.AmlStart, ObjDesc->Method.AmlLength,
642 Info, ACPI_IMODE_EXECUTE);
643
644 ACPI_FREE (Info);
645 if (ACPI_FAILURE (Status))
646 {
647 goto Cleanup;
648 }
649
650 /*
651 * Delete the operands on the previous walkstate operand stack
652 * (they were copied to new objects)
653 */
654 for (i = 0; i < ObjDesc->Method.ParamCount; i++)
655 {
656 AcpiUtRemoveReference (ThisWalkState->Operands [i]);
657 ThisWalkState->Operands [i] = NULL;
658 }
659
660 /* Clear the operand stack */
661
662 ThisWalkState->NumOperands = 0;
663
664 ACPI_DEBUG_PRINT ((ACPI_DB_DISPATCH,
665 "**** Begin nested execution of [%4.4s] **** WalkState=%p\n",
666 MethodNode->Name.Ascii, NextWalkState));
667
668 /* Invoke an internal method if necessary */
669
670 if (ObjDesc->Method.InfoFlags & ACPI_METHOD_INTERNAL_ONLY)
671 {
672 Status = ObjDesc->Method.Dispatch.Implementation (NextWalkState);
673 if (Status == AE_OK)
674 {
675 Status = AE_CTRL_TERMINATE;
676 }
677 }
678
679 return_ACPI_STATUS (Status);
680
681
682 Cleanup:
683
684 /* On error, we must terminate the method properly */
685
686 AcpiDsTerminateControlMethod (ObjDesc, NextWalkState);
687 if (NextWalkState)
688 {
689 AcpiDsDeleteWalkState (NextWalkState);
690 }
691
692 return_ACPI_STATUS (Status);
693 }
694
695
696 /*******************************************************************************
697 *
698 * FUNCTION: AcpiDsRestartControlMethod
699 *
700 * PARAMETERS: WalkState - State for preempted method (caller)
701 * ReturnDesc - Return value from the called method
702 *
703 * RETURN: Status
704 *
705 * DESCRIPTION: Restart a method that was preempted by another (nested) method
706 * invocation. Handle the return value (if any) from the callee.
707 *
708 ******************************************************************************/
709
710 ACPI_STATUS
711 AcpiDsRestartControlMethod (
712 ACPI_WALK_STATE *WalkState,
713 ACPI_OPERAND_OBJECT *ReturnDesc)
714 {
715 ACPI_STATUS Status;
716 int SameAsImplicitReturn;
717
718
719 ACPI_FUNCTION_TRACE_PTR (DsRestartControlMethod, WalkState);
720
721
722 ACPI_DEBUG_PRINT ((ACPI_DB_DISPATCH,
723 "****Restart [%4.4s] Op %p ReturnValueFromCallee %p\n",
724 AcpiUtGetNodeName (WalkState->MethodNode),
725 WalkState->MethodCallOp, ReturnDesc));
726
727 ACPI_DEBUG_PRINT ((ACPI_DB_DISPATCH,
728 " ReturnFromThisMethodUsed?=%X ResStack %p Walk %p\n",
729 WalkState->ReturnUsed,
730 WalkState->Results, WalkState));
731
732 /* Did the called method return a value? */
733
734 if (ReturnDesc)
735 {
736 /* Is the implicit return object the same as the return desc? */
737
738 SameAsImplicitReturn = (WalkState->ImplicitReturnObj == ReturnDesc);
739
740 /* Are we actually going to use the return value? */
741
742 if (WalkState->ReturnUsed)
743 {
744 /* Save the return value from the previous method */
745
746 Status = AcpiDsResultPush (ReturnDesc, WalkState);
747 if (ACPI_FAILURE (Status))
748 {
749 AcpiUtRemoveReference (ReturnDesc);
750 return_ACPI_STATUS (Status);
751 }
752
753 /*
754 * Save as THIS method's return value in case it is returned
755 * immediately to yet another method
756 */
757 WalkState->ReturnDesc = ReturnDesc;
758 }
759
760 /*
761 * The following code is the optional support for the so-called
762 * "implicit return". Some AML code assumes that the last value of the
763 * method is "implicitly" returned to the caller, in the absence of an
764 * explicit return value.
765 *
766 * Just save the last result of the method as the return value.
767 *
768 * NOTE: this is optional because the ASL language does not actually
769 * support this behavior.
770 */
771 else if (!AcpiDsDoImplicitReturn (ReturnDesc, WalkState, FALSE) ||
772 SameAsImplicitReturn)
773 {
774 /*
775 * Delete the return value if it will not be used by the
776 * calling method or remove one reference if the explicit return
777 * is the same as the implicit return value.
778 */
779 AcpiUtRemoveReference (ReturnDesc);
780 }
781 }
782
783 return_ACPI_STATUS (AE_OK);
784 }
785
786
787 /*******************************************************************************
788 *
789 * FUNCTION: AcpiDsTerminateControlMethod
790 *
791 * PARAMETERS: MethodDesc - Method object
792 * WalkState - State associated with the method
793 *
794 * RETURN: None
795 *
796 * DESCRIPTION: Terminate a control method. Delete everything that the method
797 * created, delete all locals and arguments, and delete the parse
798 * tree if requested.
799 *
800 * MUTEX: Interpreter is locked
801 *
802 ******************************************************************************/
803
804 void
805 AcpiDsTerminateControlMethod (
806 ACPI_OPERAND_OBJECT *MethodDesc,
807 ACPI_WALK_STATE *WalkState)
808 {
809
810 ACPI_FUNCTION_TRACE_PTR (DsTerminateControlMethod, WalkState);
811
812
813 /* MethodDesc is required, WalkState is optional */
814
815 if (!MethodDesc)
816 {
817 return_VOID;
818 }
819
820 if (WalkState)
821 {
822 /* Delete all arguments and locals */
823
824 AcpiDsMethodDataDeleteAll (WalkState);
825
826 /*
827 * If method is serialized, release the mutex and restore the
828 * current sync level for this thread
829 */
830 if (MethodDesc->Method.Mutex)
831 {
832 /* Acquisition Depth handles recursive calls */
833
834 MethodDesc->Method.Mutex->Mutex.AcquisitionDepth--;
835 if (!MethodDesc->Method.Mutex->Mutex.AcquisitionDepth)
836 {
837 WalkState->Thread->CurrentSyncLevel =
838 MethodDesc->Method.Mutex->Mutex.OriginalSyncLevel;
839
840 AcpiOsReleaseMutex (MethodDesc->Method.Mutex->Mutex.OsMutex);
841 MethodDesc->Method.Mutex->Mutex.ThreadId = 0;
842 }
843 }
844
845 /*
846 * Delete any namespace objects created anywhere within the
847 * namespace by the execution of this method. Unless:
848 * 1) This method is a module-level executable code method, in which
849 * case we want make the objects permanent.
850 * 2) There are other threads executing the method, in which case we
851 * will wait until the last thread has completed.
852 */
853 if (!(MethodDesc->Method.InfoFlags & ACPI_METHOD_MODULE_LEVEL) &&
854 (MethodDesc->Method.ThreadCount == 1))
855 {
856 /* Delete any direct children of (created by) this method */
857
858 AcpiNsDeleteNamespaceSubtree (WalkState->MethodNode);
859
860 /*
861 * Delete any objects that were created by this method
862 * elsewhere in the namespace (if any were created).
863 * Use of the ACPI_METHOD_MODIFIED_NAMESPACE optimizes the
864 * deletion such that we don't have to perform an entire
865 * namespace walk for every control method execution.
866 */
867 if (MethodDesc->Method.InfoFlags & ACPI_METHOD_MODIFIED_NAMESPACE)
868 {
869 AcpiNsDeleteNamespaceByOwner (MethodDesc->Method.OwnerId);
870 MethodDesc->Method.InfoFlags &= ~ACPI_METHOD_MODIFIED_NAMESPACE;
871 }
872 }
873 }
874
875 /* Decrement the thread count on the method */
876
877 if (MethodDesc->Method.ThreadCount)
878 {
879 MethodDesc->Method.ThreadCount--;
880 }
881 else
882 {
883 ACPI_ERROR ((AE_INFO,
884 "Invalid zero thread count in method"));
885 }
886
887 /* Are there any other threads currently executing this method? */
888
889 if (MethodDesc->Method.ThreadCount)
890 {
891 /*
892 * Additional threads. Do not release the OwnerId in this case,
893 * we immediately reuse it for the next thread executing this method
894 */
895 ACPI_DEBUG_PRINT ((ACPI_DB_DISPATCH,
896 "*** Completed execution of one thread, %u threads remaining\n",
897 MethodDesc->Method.ThreadCount));
898 }
899 else
900 {
901 /* This is the only executing thread for this method */
902
903 /*
904 * Support to dynamically change a method from NotSerialized to
905 * Serialized if it appears that the method is incorrectly written and
906 * does not support multiple thread execution. The best example of this
907 * is if such a method creates namespace objects and blocks. A second
908 * thread will fail with an AE_ALREADY_EXISTS exception.
909 *
910 * This code is here because we must wait until the last thread exits
911 * before marking the method as serialized.
912 */
913 if (MethodDesc->Method.InfoFlags & ACPI_METHOD_SERIALIZED_PENDING)
914 {
915 if (WalkState)
916 {
917 ACPI_INFO ((AE_INFO,
918 "Marking method %4.4s as Serialized because of AE_ALREADY_EXISTS error",
919 WalkState->MethodNode->Name.Ascii));
920 }
921
922 /*
923 * Method tried to create an object twice and was marked as
924 * "pending serialized". The probable cause is that the method
925 * cannot handle reentrancy.
926 *
927 * The method was created as NotSerialized, but it tried to create
928 * a named object and then blocked, causing the second thread
929 * entrance to begin and then fail. Workaround this problem by
930 * marking the method permanently as Serialized when the last
931 * thread exits here.
932 */
933 MethodDesc->Method.InfoFlags &= ~ACPI_METHOD_SERIALIZED_PENDING;
934 MethodDesc->Method.InfoFlags |=
935 (ACPI_METHOD_SERIALIZED | ACPI_METHOD_IGNORE_SYNC_LEVEL);
936 MethodDesc->Method.SyncLevel = 0;
937 }
938
939 /* No more threads, we can free the OwnerId */
940
941 if (!(MethodDesc->Method.InfoFlags & ACPI_METHOD_MODULE_LEVEL))
942 {
943 AcpiUtReleaseOwnerId (&MethodDesc->Method.OwnerId);
944 }
945 }
946
947 return_VOID;
948 }