Import acpica-unix-20061109 from Intel.
[dragonfly.git] / sys / contrib / dev / acpica-unix-20061109 / interpreter / parser / psparse.c
blob7da4aa90a24e099f904844ea658306f58e9d50d6
1 /******************************************************************************
3 * Module Name: psparse - Parser top level AML parse routines
4 * $Revision: 1.169 $
6 *****************************************************************************/
8 /******************************************************************************
10 * 1. Copyright Notice
12 * Some or all of this work - Copyright (c) 1999 - 2006, Intel Corp.
13 * All rights reserved.
15 * 2. License
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
20 * property rights.
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
39 * conditions are met:
41 * 3. Conditions
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
65 * make.
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
71 * distribution.
73 * 3.4. Intel retains all right, title, and interest in and to the Original
74 * Intel Code.
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
89 * PARTICULAR PURPOSE.
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
98 * LIMITED REMEDY.
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 *****************************************************************************/
119 * Parse the AML and build an operation tree as most interpreters,
120 * like Perl, do. Parsing is done by hand rather than with a YACC
121 * generated parser to tightly constrain stack and dynamic memory
122 * usage. At the same time, parsing is kept flexible and the code
123 * fairly compact by parsing based on a list of AML opcode
124 * templates in AmlOpInfo[]
127 #include "acpi.h"
128 #include "acparser.h"
129 #include "acdispat.h"
130 #include "amlcode.h"
131 #include "acnamesp.h"
132 #include "acinterp.h"
134 #define _COMPONENT ACPI_PARSER
135 ACPI_MODULE_NAME ("psparse")
138 /*******************************************************************************
140 * FUNCTION: AcpiPsGetOpcodeSize
142 * PARAMETERS: Opcode - An AML opcode
144 * RETURN: Size of the opcode, in bytes (1 or 2)
146 * DESCRIPTION: Get the size of the current opcode.
148 ******************************************************************************/
150 UINT32
151 AcpiPsGetOpcodeSize (
152 UINT32 Opcode)
155 /* Extended (2-byte) opcode if > 255 */
157 if (Opcode > 0x00FF)
159 return (2);
162 /* Otherwise, just a single byte opcode */
164 return (1);
168 /*******************************************************************************
170 * FUNCTION: AcpiPsPeekOpcode
172 * PARAMETERS: ParserState - A parser state object
174 * RETURN: Next AML opcode
176 * DESCRIPTION: Get next AML opcode (without incrementing AML pointer)
178 ******************************************************************************/
180 UINT16
181 AcpiPsPeekOpcode (
182 ACPI_PARSE_STATE *ParserState)
184 UINT8 *Aml;
185 UINT16 Opcode;
188 Aml = ParserState->Aml;
189 Opcode = (UINT16) ACPI_GET8 (Aml);
191 if (Opcode == AML_EXTENDED_OP_PREFIX)
193 /* Extended opcode, get the second opcode byte */
195 Aml++;
196 Opcode = (UINT16) ((Opcode << 8) | ACPI_GET8 (Aml));
199 return (Opcode);
203 /*******************************************************************************
205 * FUNCTION: AcpiPsCompleteThisOp
207 * PARAMETERS: WalkState - Current State
208 * Op - Op to complete
210 * RETURN: Status
212 * DESCRIPTION: Perform any cleanup at the completion of an Op.
214 ******************************************************************************/
216 ACPI_STATUS
217 AcpiPsCompleteThisOp (
218 ACPI_WALK_STATE *WalkState,
219 ACPI_PARSE_OBJECT *Op)
221 ACPI_PARSE_OBJECT *Prev;
222 ACPI_PARSE_OBJECT *Next;
223 const ACPI_OPCODE_INFO *ParentInfo;
224 ACPI_PARSE_OBJECT *ReplacementOp = NULL;
227 ACPI_FUNCTION_TRACE_PTR (PsCompleteThisOp, Op);
230 /* Check for null Op, can happen if AML code is corrupt */
232 if (!Op)
234 return_ACPI_STATUS (AE_OK); /* OK for now */
237 /* Delete this op and the subtree below it if asked to */
239 if (((WalkState->ParseFlags & ACPI_PARSE_TREE_MASK) != ACPI_PARSE_DELETE_TREE) ||
240 (WalkState->OpInfo->Class == AML_CLASS_ARGUMENT))
242 return_ACPI_STATUS (AE_OK);
245 /* Make sure that we only delete this subtree */
247 if (Op->Common.Parent)
249 Prev = Op->Common.Parent->Common.Value.Arg;
250 if (!Prev)
252 /* Nothing more to do */
254 goto Cleanup;
258 * Check if we need to replace the operator and its subtree
259 * with a return value op (placeholder op)
261 ParentInfo = AcpiPsGetOpcodeInfo (Op->Common.Parent->Common.AmlOpcode);
263 switch (ParentInfo->Class)
265 case AML_CLASS_CONTROL:
266 break;
268 case AML_CLASS_CREATE:
271 * These opcodes contain TermArg operands. The current
272 * op must be replaced by a placeholder return op
274 ReplacementOp = AcpiPsAllocOp (AML_INT_RETURN_VALUE_OP);
275 if (!ReplacementOp)
277 goto AllocateError;
279 break;
281 case AML_CLASS_NAMED_OBJECT:
284 * These opcodes contain TermArg operands. The current
285 * op must be replaced by a placeholder return op
287 if ((Op->Common.Parent->Common.AmlOpcode == AML_REGION_OP) ||
288 (Op->Common.Parent->Common.AmlOpcode == AML_DATA_REGION_OP) ||
289 (Op->Common.Parent->Common.AmlOpcode == AML_BUFFER_OP) ||
290 (Op->Common.Parent->Common.AmlOpcode == AML_PACKAGE_OP) ||
291 (Op->Common.Parent->Common.AmlOpcode == AML_VAR_PACKAGE_OP))
293 ReplacementOp = AcpiPsAllocOp (AML_INT_RETURN_VALUE_OP);
294 if (!ReplacementOp)
296 goto AllocateError;
299 else if ((Op->Common.Parent->Common.AmlOpcode == AML_NAME_OP) &&
300 (WalkState->PassNumber <= ACPI_IMODE_LOAD_PASS2))
302 if ((Op->Common.AmlOpcode == AML_BUFFER_OP) ||
303 (Op->Common.AmlOpcode == AML_PACKAGE_OP) ||
304 (Op->Common.AmlOpcode == AML_VAR_PACKAGE_OP))
306 ReplacementOp = AcpiPsAllocOp (Op->Common.AmlOpcode);
307 if (!ReplacementOp)
309 goto AllocateError;
312 ReplacementOp->Named.Data = Op->Named.Data;
313 ReplacementOp->Named.Length = Op->Named.Length;
316 break;
318 default:
320 ReplacementOp = AcpiPsAllocOp (AML_INT_RETURN_VALUE_OP);
321 if (!ReplacementOp)
323 goto AllocateError;
327 /* We must unlink this op from the parent tree */
329 if (Prev == Op)
331 /* This op is the first in the list */
333 if (ReplacementOp)
335 ReplacementOp->Common.Parent = Op->Common.Parent;
336 ReplacementOp->Common.Value.Arg = NULL;
337 ReplacementOp->Common.Node = Op->Common.Node;
338 Op->Common.Parent->Common.Value.Arg = ReplacementOp;
339 ReplacementOp->Common.Next = Op->Common.Next;
341 else
343 Op->Common.Parent->Common.Value.Arg = Op->Common.Next;
347 /* Search the parent list */
349 else while (Prev)
351 /* Traverse all siblings in the parent's argument list */
353 Next = Prev->Common.Next;
354 if (Next == Op)
356 if (ReplacementOp)
358 ReplacementOp->Common.Parent = Op->Common.Parent;
359 ReplacementOp->Common.Value.Arg = NULL;
360 ReplacementOp->Common.Node = Op->Common.Node;
361 Prev->Common.Next = ReplacementOp;
362 ReplacementOp->Common.Next = Op->Common.Next;
363 Next = NULL;
365 else
367 Prev->Common.Next = Op->Common.Next;
368 Next = NULL;
371 Prev = Next;
376 Cleanup:
378 /* Now we can actually delete the subtree rooted at Op */
380 AcpiPsDeleteParseTree (Op);
381 return_ACPI_STATUS (AE_OK);
384 AllocateError:
386 /* Always delete the subtree, even on error */
388 AcpiPsDeleteParseTree (Op);
389 return_ACPI_STATUS (AE_NO_MEMORY);
393 /*******************************************************************************
395 * FUNCTION: AcpiPsNextParseState
397 * PARAMETERS: WalkState - Current state
398 * Op - Current parse op
399 * CallbackStatus - Status from previous operation
401 * RETURN: Status
403 * DESCRIPTION: Update the parser state based upon the return exception from
404 * the parser callback.
406 ******************************************************************************/
408 ACPI_STATUS
409 AcpiPsNextParseState (
410 ACPI_WALK_STATE *WalkState,
411 ACPI_PARSE_OBJECT *Op,
412 ACPI_STATUS CallbackStatus)
414 ACPI_PARSE_STATE *ParserState = &WalkState->ParserState;
415 ACPI_STATUS Status = AE_CTRL_PENDING;
418 ACPI_FUNCTION_TRACE_PTR (PsNextParseState, Op);
421 switch (CallbackStatus)
423 case AE_CTRL_TERMINATE:
425 * A control method was terminated via a RETURN statement.
426 * The walk of this method is complete.
428 ParserState->Aml = ParserState->AmlEnd;
429 Status = AE_CTRL_TERMINATE;
430 break;
433 case AE_CTRL_BREAK:
435 ParserState->Aml = WalkState->AmlLastWhile;
436 WalkState->ControlState->Common.Value = FALSE;
437 Status = AcpiDsResultStackPop (WalkState);
438 if (ACPI_SUCCESS (Status))
440 Status = AE_CTRL_BREAK;
442 break;
445 case AE_CTRL_CONTINUE:
447 ParserState->Aml = WalkState->AmlLastWhile;
448 Status = AcpiDsResultStackPop (WalkState);
449 if (ACPI_SUCCESS (Status))
451 Status = AE_CTRL_CONTINUE;
453 break;
456 case AE_CTRL_PENDING:
458 ParserState->Aml = WalkState->AmlLastWhile;
459 break;
461 #if 0
462 case AE_CTRL_SKIP:
464 ParserState->Aml = ParserState->Scope->ParseScope.PkgEnd;
465 Status = AE_OK;
466 break;
467 #endif
469 case AE_CTRL_TRUE:
471 * Predicate of an IF was true, and we are at the matching ELSE.
472 * Just close out this package
474 ParserState->Aml = AcpiPsGetNextPackageEnd (ParserState);
475 Status = AcpiDsResultStackPop (WalkState);
476 if (ACPI_SUCCESS (Status))
478 Status = AE_CTRL_PENDING;
480 break;
483 case AE_CTRL_FALSE:
485 * Either an IF/WHILE Predicate was false or we encountered a BREAK
486 * opcode. In both cases, we do not execute the rest of the
487 * package; We simply close out the parent (finishing the walk of
488 * this branch of the tree) and continue execution at the parent
489 * level.
491 ParserState->Aml = ParserState->Scope->ParseScope.PkgEnd;
493 /* In the case of a BREAK, just force a predicate (if any) to FALSE */
495 WalkState->ControlState->Common.Value = FALSE;
496 Status = AE_CTRL_END;
497 break;
500 case AE_CTRL_TRANSFER:
502 /* A method call (invocation) -- transfer control */
504 Status = AE_CTRL_TRANSFER;
505 WalkState->PrevOp = Op;
506 WalkState->MethodCallOp = Op;
507 WalkState->MethodCallNode = (Op->Common.Value.Arg)->Common.Node;
509 /* Will return value (if any) be used by the caller? */
511 WalkState->ReturnUsed = AcpiDsIsResultUsed (Op, WalkState);
512 break;
515 default:
517 Status = CallbackStatus;
518 if ((CallbackStatus & AE_CODE_MASK) == AE_CODE_CONTROL)
520 Status = AE_OK;
522 break;
525 return_ACPI_STATUS (Status);
529 /*******************************************************************************
531 * FUNCTION: AcpiPsParseAml
533 * PARAMETERS: WalkState - Current state
536 * RETURN: Status
538 * DESCRIPTION: Parse raw AML and return a tree of ops
540 ******************************************************************************/
542 ACPI_STATUS
543 AcpiPsParseAml (
544 ACPI_WALK_STATE *WalkState)
546 ACPI_STATUS Status;
547 ACPI_THREAD_STATE *Thread;
548 ACPI_THREAD_STATE *PrevWalkList = AcpiGbl_CurrentWalkList;
549 ACPI_WALK_STATE *PreviousWalkState;
552 ACPI_FUNCTION_TRACE (PsParseAml);
554 ACPI_DEBUG_PRINT ((ACPI_DB_PARSE,
555 "Entered with WalkState=%p Aml=%p size=%X\n",
556 WalkState, WalkState->ParserState.Aml,
557 WalkState->ParserState.AmlSize));
560 /* Create and initialize a new thread state */
562 Thread = AcpiUtCreateThreadState ();
563 if (!Thread)
565 AcpiDsDeleteWalkState (WalkState);
566 return_ACPI_STATUS (AE_NO_MEMORY);
569 WalkState->Thread = Thread;
572 * If executing a method, the starting SyncLevel is this method's
573 * SyncLevel
575 if (WalkState->MethodDesc)
577 WalkState->Thread->CurrentSyncLevel = WalkState->MethodDesc->Method.SyncLevel;
580 AcpiDsPushWalkState (WalkState, Thread);
583 * This global allows the AML debugger to get a handle to the currently
584 * executing control method.
586 AcpiGbl_CurrentWalkList = Thread;
589 * Execute the walk loop as long as there is a valid Walk State. This
590 * handles nested control method invocations without recursion.
592 ACPI_DEBUG_PRINT ((ACPI_DB_PARSE, "State=%p\n", WalkState));
594 Status = AE_OK;
595 while (WalkState)
597 if (ACPI_SUCCESS (Status))
600 * The ParseLoop executes AML until the method terminates
601 * or calls another method.
603 Status = AcpiPsParseLoop (WalkState);
606 ACPI_DEBUG_PRINT ((ACPI_DB_PARSE,
607 "Completed one call to walk loop, %s State=%p\n",
608 AcpiFormatException (Status), WalkState));
610 if (Status == AE_CTRL_TRANSFER)
613 * A method call was detected.
614 * Transfer control to the called control method
616 Status = AcpiDsCallControlMethod (Thread, WalkState, NULL);
617 if (ACPI_FAILURE (Status))
619 Status = AcpiDsMethodError (Status, WalkState);
623 * If the transfer to the new method method call worked, a new walk
624 * state was created -- get it
626 WalkState = AcpiDsGetCurrentWalkState (Thread);
627 continue;
629 else if (Status == AE_CTRL_TERMINATE)
631 Status = AE_OK;
633 else if ((Status != AE_OK) && (WalkState->MethodDesc))
635 /* Either the method parse or actual execution failed */
637 ACPI_ERROR_METHOD ("Method parse/execution failed",
638 WalkState->MethodNode, NULL, Status);
640 /* Check for possible multi-thread reentrancy problem */
642 if ((Status == AE_ALREADY_EXISTS) &&
643 (!WalkState->MethodDesc->Method.Mutex))
645 ACPI_INFO ((AE_INFO, "Marking method %4.4s as Serialized",
646 WalkState->MethodNode->Name.Ascii));
649 * Method tried to create an object twice. The probable cause is
650 * that the method cannot handle reentrancy.
652 * The method is marked NotSerialized, but it tried to create
653 * a named object, causing the second thread entrance to fail.
654 * Workaround this problem by marking the method permanently
655 * as Serialized.
657 WalkState->MethodDesc->Method.MethodFlags |= AML_METHOD_SERIALIZED;
658 WalkState->MethodDesc->Method.SyncLevel = 0;
662 /* We are done with this walk, move on to the parent if any */
664 WalkState = AcpiDsPopWalkState (Thread);
666 /* Reset the current scope to the beginning of scope stack */
668 AcpiDsScopeStackClear (WalkState);
671 * If we just returned from the execution of a control method or if we
672 * encountered an error during the method parse phase, there's lots of
673 * cleanup to do
675 if (((WalkState->ParseFlags & ACPI_PARSE_MODE_MASK) == ACPI_PARSE_EXECUTE) ||
676 (ACPI_FAILURE (Status)))
678 AcpiDsTerminateControlMethod (WalkState->MethodDesc, WalkState);
681 /* Delete this walk state and all linked control states */
683 AcpiPsCleanupScope (&WalkState->ParserState);
684 PreviousWalkState = WalkState;
686 ACPI_DEBUG_PRINT ((ACPI_DB_PARSE,
687 "ReturnValue=%p, ImplicitValue=%p State=%p\n",
688 WalkState->ReturnDesc, WalkState->ImplicitReturnObj, WalkState));
690 /* Check if we have restarted a preempted walk */
692 WalkState = AcpiDsGetCurrentWalkState (Thread);
693 if (WalkState)
695 if (ACPI_SUCCESS (Status))
698 * There is another walk state, restart it.
699 * If the method return value is not used by the parent,
700 * The object is deleted
702 if (!PreviousWalkState->ReturnDesc)
704 Status = AcpiDsRestartControlMethod (WalkState,
705 PreviousWalkState->ImplicitReturnObj);
707 else
710 * We have a valid return value, delete any implicit
711 * return value.
713 AcpiDsClearImplicitReturn (PreviousWalkState);
715 Status = AcpiDsRestartControlMethod (WalkState,
716 PreviousWalkState->ReturnDesc);
718 if (ACPI_SUCCESS (Status))
720 WalkState->WalkType |= ACPI_WALK_METHOD_RESTART;
723 else
725 /* On error, delete any return object */
727 AcpiUtRemoveReference (PreviousWalkState->ReturnDesc);
732 * Just completed a 1st-level method, save the final internal return
733 * value (if any)
735 else if (PreviousWalkState->CallerReturnDesc)
737 if (PreviousWalkState->ImplicitReturnObj)
739 *(PreviousWalkState->CallerReturnDesc) =
740 PreviousWalkState->ImplicitReturnObj;
742 else
744 /* NULL if no return value */
746 *(PreviousWalkState->CallerReturnDesc) =
747 PreviousWalkState->ReturnDesc;
750 else
752 if (PreviousWalkState->ReturnDesc)
754 /* Caller doesn't want it, must delete it */
756 AcpiUtRemoveReference (PreviousWalkState->ReturnDesc);
758 if (PreviousWalkState->ImplicitReturnObj)
760 /* Caller doesn't want it, must delete it */
762 AcpiUtRemoveReference (PreviousWalkState->ImplicitReturnObj);
766 AcpiDsDeleteWalkState (PreviousWalkState);
769 /* Normal exit */
771 AcpiExReleaseAllMutexes (Thread);
772 AcpiUtDeleteGenericState (ACPI_CAST_PTR (ACPI_GENERIC_STATE, Thread));
773 AcpiGbl_CurrentWalkList = PrevWalkList;
774 return_ACPI_STATUS (Status);