1 /******************************************************************************
3 * Module Name: psparse - Parser top level AML parse routines
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.
46 * Parse the AML and build an operation tree as most interpreters,
47 * like Perl, do. Parsing is done by hand rather than with a YACC
48 * generated parser to tightly constrain stack and dynamic memory
49 * usage. At the same time, parsing is kept flexible and the code
50 * fairly compact by parsing based on a list of AML opcode
51 * templates in AmlOpInfo[]
54 #include <contrib/dev/acpica/include/acpi.h>
55 #include <contrib/dev/acpica/include/accommon.h>
56 #include <contrib/dev/acpica/include/acparser.h>
57 #include <contrib/dev/acpica/include/acdispat.h>
58 #include <contrib/dev/acpica/include/amlcode.h>
59 #include <contrib/dev/acpica/include/acinterp.h>
61 #define _COMPONENT ACPI_PARSER
62 ACPI_MODULE_NAME ("psparse")
65 /*******************************************************************************
67 * FUNCTION: AcpiPsGetOpcodeSize
69 * PARAMETERS: Opcode - An AML opcode
71 * RETURN: Size of the opcode, in bytes (1 or 2)
73 * DESCRIPTION: Get the size of the current opcode.
75 ******************************************************************************/
82 /* Extended (2-byte) opcode if > 255 */
89 /* Otherwise, just a single byte opcode */
95 /*******************************************************************************
97 * FUNCTION: AcpiPsPeekOpcode
99 * PARAMETERS: ParserState - A parser state object
101 * RETURN: Next AML opcode
103 * DESCRIPTION: Get next AML opcode (without incrementing AML pointer)
105 ******************************************************************************/
109 ACPI_PARSE_STATE *ParserState)
115 Aml = ParserState->Aml;
116 Opcode = (UINT16) ACPI_GET8 (Aml);
118 if (Opcode == AML_EXTENDED_OP_PREFIX)
120 /* Extended opcode, get the second opcode byte */
123 Opcode = (UINT16) ((Opcode << 8) | ACPI_GET8 (Aml));
130 /*******************************************************************************
132 * FUNCTION: AcpiPsCompleteThisOp
134 * PARAMETERS: WalkState - Current State
135 * Op - Op to complete
139 * DESCRIPTION: Perform any cleanup at the completion of an Op.
141 ******************************************************************************/
144 AcpiPsCompleteThisOp (
145 ACPI_WALK_STATE *WalkState,
146 ACPI_PARSE_OBJECT *Op)
148 ACPI_PARSE_OBJECT *Prev;
149 ACPI_PARSE_OBJECT *Next;
150 const ACPI_OPCODE_INFO *ParentInfo;
151 ACPI_PARSE_OBJECT *ReplacementOp = NULL;
152 ACPI_STATUS Status = AE_OK;
155 ACPI_FUNCTION_TRACE_PTR (PsCompleteThisOp, Op);
158 /* Check for null Op, can happen if AML code is corrupt */
162 return_ACPI_STATUS (AE_OK); /* OK for now */
165 /* Delete this op and the subtree below it if asked to */
167 if (((WalkState->ParseFlags & ACPI_PARSE_TREE_MASK) != ACPI_PARSE_DELETE_TREE) ||
168 (WalkState->OpInfo->Class == AML_CLASS_ARGUMENT))
170 return_ACPI_STATUS (AE_OK);
173 /* Make sure that we only delete this subtree */
175 if (Op->Common.Parent)
177 Prev = Op->Common.Parent->Common.Value.Arg;
180 /* Nothing more to do */
186 * Check if we need to replace the operator and its subtree
187 * with a return value op (placeholder op)
189 ParentInfo = AcpiPsGetOpcodeInfo (Op->Common.Parent->Common.AmlOpcode);
191 switch (ParentInfo->Class)
193 case AML_CLASS_CONTROL:
196 case AML_CLASS_CREATE:
199 * These opcodes contain TermArg operands. The current
200 * op must be replaced by a placeholder return op
202 ReplacementOp = AcpiPsAllocOp (AML_INT_RETURN_VALUE_OP);
205 Status = AE_NO_MEMORY;
209 case AML_CLASS_NAMED_OBJECT:
212 * These opcodes contain TermArg operands. The current
213 * op must be replaced by a placeholder return op
215 if ((Op->Common.Parent->Common.AmlOpcode == AML_REGION_OP) ||
216 (Op->Common.Parent->Common.AmlOpcode == AML_DATA_REGION_OP) ||
217 (Op->Common.Parent->Common.AmlOpcode == AML_BUFFER_OP) ||
218 (Op->Common.Parent->Common.AmlOpcode == AML_PACKAGE_OP) ||
219 (Op->Common.Parent->Common.AmlOpcode == AML_BANK_FIELD_OP) ||
220 (Op->Common.Parent->Common.AmlOpcode == AML_VAR_PACKAGE_OP))
222 ReplacementOp = AcpiPsAllocOp (AML_INT_RETURN_VALUE_OP);
225 Status = AE_NO_MEMORY;
228 else if ((Op->Common.Parent->Common.AmlOpcode == AML_NAME_OP) &&
229 (WalkState->PassNumber <= ACPI_IMODE_LOAD_PASS2))
231 if ((Op->Common.AmlOpcode == AML_BUFFER_OP) ||
232 (Op->Common.AmlOpcode == AML_PACKAGE_OP) ||
233 (Op->Common.AmlOpcode == AML_VAR_PACKAGE_OP))
235 ReplacementOp = AcpiPsAllocOp (Op->Common.AmlOpcode);
238 Status = AE_NO_MEMORY;
242 ReplacementOp->Named.Data = Op->Named.Data;
243 ReplacementOp->Named.Length = Op->Named.Length;
251 ReplacementOp = AcpiPsAllocOp (AML_INT_RETURN_VALUE_OP);
254 Status = AE_NO_MEMORY;
258 /* We must unlink this op from the parent tree */
262 /* This op is the first in the list */
266 ReplacementOp->Common.Parent = Op->Common.Parent;
267 ReplacementOp->Common.Value.Arg = NULL;
268 ReplacementOp->Common.Node = Op->Common.Node;
269 Op->Common.Parent->Common.Value.Arg = ReplacementOp;
270 ReplacementOp->Common.Next = Op->Common.Next;
274 Op->Common.Parent->Common.Value.Arg = Op->Common.Next;
278 /* Search the parent list */
282 /* Traverse all siblings in the parent's argument list */
284 Next = Prev->Common.Next;
289 ReplacementOp->Common.Parent = Op->Common.Parent;
290 ReplacementOp->Common.Value.Arg = NULL;
291 ReplacementOp->Common.Node = Op->Common.Node;
292 Prev->Common.Next = ReplacementOp;
293 ReplacementOp->Common.Next = Op->Common.Next;
298 Prev->Common.Next = Op->Common.Next;
309 /* Now we can actually delete the subtree rooted at Op */
311 AcpiPsDeleteParseTree (Op);
312 return_ACPI_STATUS (Status);
316 /*******************************************************************************
318 * FUNCTION: AcpiPsNextParseState
320 * PARAMETERS: WalkState - Current state
321 * Op - Current parse op
322 * CallbackStatus - Status from previous operation
326 * DESCRIPTION: Update the parser state based upon the return exception from
327 * the parser callback.
329 ******************************************************************************/
332 AcpiPsNextParseState (
333 ACPI_WALK_STATE *WalkState,
334 ACPI_PARSE_OBJECT *Op,
335 ACPI_STATUS CallbackStatus)
337 ACPI_PARSE_STATE *ParserState = &WalkState->ParserState;
338 ACPI_STATUS Status = AE_CTRL_PENDING;
341 ACPI_FUNCTION_TRACE_PTR (PsNextParseState, Op);
344 switch (CallbackStatus)
346 case AE_CTRL_TERMINATE:
348 * A control method was terminated via a RETURN statement.
349 * The walk of this method is complete.
351 ParserState->Aml = ParserState->AmlEnd;
352 Status = AE_CTRL_TERMINATE;
358 ParserState->Aml = WalkState->AmlLastWhile;
359 WalkState->ControlState->Common.Value = FALSE;
360 Status = AE_CTRL_BREAK;
364 case AE_CTRL_CONTINUE:
366 ParserState->Aml = WalkState->AmlLastWhile;
367 Status = AE_CTRL_CONTINUE;
371 case AE_CTRL_PENDING:
373 ParserState->Aml = WalkState->AmlLastWhile;
379 ParserState->Aml = ParserState->Scope->ParseScope.PkgEnd;
386 * Predicate of an IF was true, and we are at the matching ELSE.
387 * Just close out this package
389 ParserState->Aml = AcpiPsGetNextPackageEnd (ParserState);
390 Status = AE_CTRL_PENDING;
396 * Either an IF/WHILE Predicate was false or we encountered a BREAK
397 * opcode. In both cases, we do not execute the rest of the
398 * package; We simply close out the parent (finishing the walk of
399 * this branch of the tree) and continue execution at the parent
402 ParserState->Aml = ParserState->Scope->ParseScope.PkgEnd;
404 /* In the case of a BREAK, just force a predicate (if any) to FALSE */
406 WalkState->ControlState->Common.Value = FALSE;
407 Status = AE_CTRL_END;
411 case AE_CTRL_TRANSFER:
413 /* A method call (invocation) -- transfer control */
415 Status = AE_CTRL_TRANSFER;
416 WalkState->PrevOp = Op;
417 WalkState->MethodCallOp = Op;
418 WalkState->MethodCallNode = (Op->Common.Value.Arg)->Common.Node;
420 /* Will return value (if any) be used by the caller? */
422 WalkState->ReturnUsed = AcpiDsIsResultUsed (Op, WalkState);
428 Status = CallbackStatus;
429 if ((CallbackStatus & AE_CODE_MASK) == AE_CODE_CONTROL)
436 return_ACPI_STATUS (Status);
440 /*******************************************************************************
442 * FUNCTION: AcpiPsParseAml
444 * PARAMETERS: WalkState - Current state
449 * DESCRIPTION: Parse raw AML and return a tree of ops
451 ******************************************************************************/
455 ACPI_WALK_STATE *WalkState)
458 ACPI_THREAD_STATE *Thread;
459 ACPI_THREAD_STATE *PrevWalkList = AcpiGbl_CurrentWalkList;
460 ACPI_WALK_STATE *PreviousWalkState;
463 ACPI_FUNCTION_TRACE (PsParseAml);
465 ACPI_DEBUG_PRINT ((ACPI_DB_PARSE,
466 "Entered with WalkState=%p Aml=%p size=%X\n",
467 WalkState, WalkState->ParserState.Aml,
468 WalkState->ParserState.AmlSize));
470 if (!WalkState->ParserState.Aml)
472 return_ACPI_STATUS (AE_NULL_OBJECT);
475 /* Create and initialize a new thread state */
477 Thread = AcpiUtCreateThreadState ();
480 if (WalkState->MethodDesc)
482 /* Executing a control method - additional cleanup */
484 AcpiDsTerminateControlMethod (WalkState->MethodDesc, WalkState);
487 AcpiDsDeleteWalkState (WalkState);
488 return_ACPI_STATUS (AE_NO_MEMORY);
491 WalkState->Thread = Thread;
494 * If executing a method, the starting SyncLevel is this method's
497 if (WalkState->MethodDesc)
499 WalkState->Thread->CurrentSyncLevel = WalkState->MethodDesc->Method.SyncLevel;
502 AcpiDsPushWalkState (WalkState, Thread);
505 * This global allows the AML debugger to get a handle to the currently
506 * executing control method.
508 AcpiGbl_CurrentWalkList = Thread;
511 * Execute the walk loop as long as there is a valid Walk State. This
512 * handles nested control method invocations without recursion.
514 ACPI_DEBUG_PRINT ((ACPI_DB_PARSE, "State=%p\n", WalkState));
519 if (ACPI_SUCCESS (Status))
522 * The ParseLoop executes AML until the method terminates
523 * or calls another method.
525 Status = AcpiPsParseLoop (WalkState);
528 ACPI_DEBUG_PRINT ((ACPI_DB_PARSE,
529 "Completed one call to walk loop, %s State=%p\n",
530 AcpiFormatException (Status), WalkState));
532 if (Status == AE_CTRL_TRANSFER)
535 * A method call was detected.
536 * Transfer control to the called control method
538 Status = AcpiDsCallControlMethod (Thread, WalkState, NULL);
539 if (ACPI_FAILURE (Status))
541 Status = AcpiDsMethodError (Status, WalkState);
545 * If the transfer to the new method method call worked, a new walk
546 * state was created -- get it
548 WalkState = AcpiDsGetCurrentWalkState (Thread);
551 else if (Status == AE_CTRL_TERMINATE)
555 else if ((Status != AE_OK) && (WalkState->MethodDesc))
557 /* Either the method parse or actual execution failed */
559 ACPI_ERROR_METHOD ("Method parse/execution failed",
560 WalkState->MethodNode, NULL, Status);
562 /* Check for possible multi-thread reentrancy problem */
564 if ((Status == AE_ALREADY_EXISTS) &&
565 (!(WalkState->MethodDesc->Method.InfoFlags & ACPI_METHOD_SERIALIZED)))
568 * Method is not serialized and tried to create an object
569 * twice. The probable cause is that the method cannot
570 * handle reentrancy. Mark as "pending serialized" now, and
571 * then mark "serialized" when the last thread exits.
573 WalkState->MethodDesc->Method.InfoFlags |=
574 ACPI_METHOD_SERIALIZED_PENDING;
578 /* We are done with this walk, move on to the parent if any */
580 WalkState = AcpiDsPopWalkState (Thread);
582 /* Reset the current scope to the beginning of scope stack */
584 AcpiDsScopeStackClear (WalkState);
587 * If we just returned from the execution of a control method or if we
588 * encountered an error during the method parse phase, there's lots of
591 if (((WalkState->ParseFlags & ACPI_PARSE_MODE_MASK) == ACPI_PARSE_EXECUTE) ||
592 (ACPI_FAILURE (Status)))
594 AcpiDsTerminateControlMethod (WalkState->MethodDesc, WalkState);
597 /* Delete this walk state and all linked control states */
599 AcpiPsCleanupScope (&WalkState->ParserState);
600 PreviousWalkState = WalkState;
602 ACPI_DEBUG_PRINT ((ACPI_DB_PARSE,
603 "ReturnValue=%p, ImplicitValue=%p State=%p\n",
604 WalkState->ReturnDesc, WalkState->ImplicitReturnObj, WalkState));
606 /* Check if we have restarted a preempted walk */
608 WalkState = AcpiDsGetCurrentWalkState (Thread);
611 if (ACPI_SUCCESS (Status))
614 * There is another walk state, restart it.
615 * If the method return value is not used by the parent,
616 * The object is deleted
618 if (!PreviousWalkState->ReturnDesc)
621 * In slack mode execution, if there is no return value
622 * we should implicitly return zero (0) as a default value.
624 if (AcpiGbl_EnableInterpreterSlack &&
625 !PreviousWalkState->ImplicitReturnObj)
627 PreviousWalkState->ImplicitReturnObj =
628 AcpiUtCreateIntegerObject ((UINT64) 0);
629 if (!PreviousWalkState->ImplicitReturnObj)
631 return_ACPI_STATUS (AE_NO_MEMORY);
635 /* Restart the calling control method */
637 Status = AcpiDsRestartControlMethod (WalkState,
638 PreviousWalkState->ImplicitReturnObj);
643 * We have a valid return value, delete any implicit
646 AcpiDsClearImplicitReturn (PreviousWalkState);
648 Status = AcpiDsRestartControlMethod (WalkState,
649 PreviousWalkState->ReturnDesc);
651 if (ACPI_SUCCESS (Status))
653 WalkState->WalkType |= ACPI_WALK_METHOD_RESTART;
658 /* On error, delete any return object or implicit return */
660 AcpiUtRemoveReference (PreviousWalkState->ReturnDesc);
661 AcpiDsClearImplicitReturn (PreviousWalkState);
666 * Just completed a 1st-level method, save the final internal return
669 else if (PreviousWalkState->CallerReturnDesc)
671 if (PreviousWalkState->ImplicitReturnObj)
673 *(PreviousWalkState->CallerReturnDesc) =
674 PreviousWalkState->ImplicitReturnObj;
678 /* NULL if no return value */
680 *(PreviousWalkState->CallerReturnDesc) =
681 PreviousWalkState->ReturnDesc;
686 if (PreviousWalkState->ReturnDesc)
688 /* Caller doesn't want it, must delete it */
690 AcpiUtRemoveReference (PreviousWalkState->ReturnDesc);
692 if (PreviousWalkState->ImplicitReturnObj)
694 /* Caller doesn't want it, must delete it */
696 AcpiUtRemoveReference (PreviousWalkState->ImplicitReturnObj);
700 AcpiDsDeleteWalkState (PreviousWalkState);
705 AcpiExReleaseAllMutexes (Thread);
706 AcpiUtDeleteGenericState (ACPI_CAST_PTR (ACPI_GENERIC_STATE, Thread));
707 AcpiGbl_CurrentWalkList = PrevWalkList;
708 return_ACPI_STATUS (Status);