1 /******************************************************************************
3 * Module Name: psparse - Parser top level AML parse routines
5 *****************************************************************************/
8 * Copyright (C) 2000 - 2014, 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.
45 * Parse the AML and build an operation tree as most interpreters,
46 * like Perl, do. Parsing is done by hand rather than with a YACC
47 * generated parser to tightly constrain stack and dynamic memory
48 * usage. At the same time, parsing is kept flexible and the code
49 * fairly compact by parsing based on a list of AML opcode
50 * templates in AmlOpInfo[]
60 #define _COMPONENT ACPI_PARSER
61 ACPI_MODULE_NAME ("psparse")
64 /*******************************************************************************
66 * FUNCTION: AcpiPsGetOpcodeSize
68 * PARAMETERS: Opcode - An AML opcode
70 * RETURN: Size of the opcode, in bytes (1 or 2)
72 * DESCRIPTION: Get the size of the current opcode.
74 ******************************************************************************/
81 /* Extended (2-byte) opcode if > 255 */
88 /* Otherwise, just a single byte opcode */
94 /*******************************************************************************
96 * FUNCTION: AcpiPsPeekOpcode
98 * PARAMETERS: ParserState - A parser state object
100 * RETURN: Next AML opcode
102 * DESCRIPTION: Get next AML opcode (without incrementing AML pointer)
104 ******************************************************************************/
108 ACPI_PARSE_STATE *ParserState)
114 Aml = ParserState->Aml;
115 Opcode = (UINT16) ACPI_GET8 (Aml);
117 if (Opcode == AML_EXTENDED_OP_PREFIX)
119 /* Extended opcode, get the second opcode byte */
122 Opcode = (UINT16) ((Opcode << 8) | ACPI_GET8 (Aml));
129 /*******************************************************************************
131 * FUNCTION: AcpiPsCompleteThisOp
133 * PARAMETERS: WalkState - Current State
134 * Op - Op to complete
138 * DESCRIPTION: Perform any cleanup at the completion of an Op.
140 ******************************************************************************/
143 AcpiPsCompleteThisOp (
144 ACPI_WALK_STATE *WalkState,
145 ACPI_PARSE_OBJECT *Op)
147 ACPI_PARSE_OBJECT *Prev;
148 ACPI_PARSE_OBJECT *Next;
149 const ACPI_OPCODE_INFO *ParentInfo;
150 ACPI_PARSE_OBJECT *ReplacementOp = NULL;
151 ACPI_STATUS Status = AE_OK;
154 ACPI_FUNCTION_TRACE_PTR (PsCompleteThisOp, Op);
157 /* Check for null Op, can happen if AML code is corrupt */
161 return_ACPI_STATUS (AE_OK); /* OK for now */
164 /* Delete this op and the subtree below it if asked to */
166 if (((WalkState->ParseFlags & ACPI_PARSE_TREE_MASK) != ACPI_PARSE_DELETE_TREE) ||
167 (WalkState->OpInfo->Class == AML_CLASS_ARGUMENT))
169 return_ACPI_STATUS (AE_OK);
172 /* Make sure that we only delete this subtree */
174 if (Op->Common.Parent)
176 Prev = Op->Common.Parent->Common.Value.Arg;
179 /* Nothing more to do */
185 * Check if we need to replace the operator and its subtree
186 * with a return value op (placeholder op)
188 ParentInfo = AcpiPsGetOpcodeInfo (Op->Common.Parent->Common.AmlOpcode);
190 switch (ParentInfo->Class)
192 case AML_CLASS_CONTROL:
196 case AML_CLASS_CREATE:
198 * These opcodes contain TermArg operands. The current
199 * op must be replaced by a placeholder return op
201 ReplacementOp = AcpiPsAllocOp (AML_INT_RETURN_VALUE_OP);
204 Status = AE_NO_MEMORY;
208 case AML_CLASS_NAMED_OBJECT:
210 * These opcodes contain TermArg operands. The current
211 * op must be replaced by a placeholder return op
213 if ((Op->Common.Parent->Common.AmlOpcode == AML_REGION_OP) ||
214 (Op->Common.Parent->Common.AmlOpcode == AML_DATA_REGION_OP) ||
215 (Op->Common.Parent->Common.AmlOpcode == AML_BUFFER_OP) ||
216 (Op->Common.Parent->Common.AmlOpcode == AML_PACKAGE_OP) ||
217 (Op->Common.Parent->Common.AmlOpcode == AML_BANK_FIELD_OP) ||
218 (Op->Common.Parent->Common.AmlOpcode == AML_VAR_PACKAGE_OP))
220 ReplacementOp = AcpiPsAllocOp (AML_INT_RETURN_VALUE_OP);
223 Status = AE_NO_MEMORY;
226 else if ((Op->Common.Parent->Common.AmlOpcode == AML_NAME_OP) &&
227 (WalkState->PassNumber <= ACPI_IMODE_LOAD_PASS2))
229 if ((Op->Common.AmlOpcode == AML_BUFFER_OP) ||
230 (Op->Common.AmlOpcode == AML_PACKAGE_OP) ||
231 (Op->Common.AmlOpcode == AML_VAR_PACKAGE_OP))
233 ReplacementOp = AcpiPsAllocOp (Op->Common.AmlOpcode);
236 Status = AE_NO_MEMORY;
240 ReplacementOp->Named.Data = Op->Named.Data;
241 ReplacementOp->Named.Length = Op->Named.Length;
249 ReplacementOp = AcpiPsAllocOp (AML_INT_RETURN_VALUE_OP);
252 Status = AE_NO_MEMORY;
256 /* We must unlink this op from the parent tree */
260 /* This op is the first in the list */
264 ReplacementOp->Common.Parent = Op->Common.Parent;
265 ReplacementOp->Common.Value.Arg = NULL;
266 ReplacementOp->Common.Node = Op->Common.Node;
267 Op->Common.Parent->Common.Value.Arg = ReplacementOp;
268 ReplacementOp->Common.Next = Op->Common.Next;
272 Op->Common.Parent->Common.Value.Arg = Op->Common.Next;
276 /* Search the parent list */
280 /* Traverse all siblings in the parent's argument list */
282 Next = Prev->Common.Next;
287 ReplacementOp->Common.Parent = Op->Common.Parent;
288 ReplacementOp->Common.Value.Arg = NULL;
289 ReplacementOp->Common.Node = Op->Common.Node;
290 Prev->Common.Next = ReplacementOp;
291 ReplacementOp->Common.Next = Op->Common.Next;
296 Prev->Common.Next = Op->Common.Next;
307 /* Now we can actually delete the subtree rooted at Op */
309 AcpiPsDeleteParseTree (Op);
310 return_ACPI_STATUS (Status);
314 /*******************************************************************************
316 * FUNCTION: AcpiPsNextParseState
318 * PARAMETERS: WalkState - Current state
319 * Op - Current parse op
320 * CallbackStatus - Status from previous operation
324 * DESCRIPTION: Update the parser state based upon the return exception from
325 * the parser callback.
327 ******************************************************************************/
330 AcpiPsNextParseState (
331 ACPI_WALK_STATE *WalkState,
332 ACPI_PARSE_OBJECT *Op,
333 ACPI_STATUS CallbackStatus)
335 ACPI_PARSE_STATE *ParserState = &WalkState->ParserState;
336 ACPI_STATUS Status = AE_CTRL_PENDING;
339 ACPI_FUNCTION_TRACE_PTR (PsNextParseState, Op);
342 switch (CallbackStatus)
344 case AE_CTRL_TERMINATE:
346 * A control method was terminated via a RETURN statement.
347 * The walk of this method is complete.
349 ParserState->Aml = ParserState->AmlEnd;
350 Status = AE_CTRL_TERMINATE;
355 ParserState->Aml = WalkState->AmlLastWhile;
356 WalkState->ControlState->Common.Value = FALSE;
357 Status = AE_CTRL_BREAK;
360 case AE_CTRL_CONTINUE:
362 ParserState->Aml = WalkState->AmlLastWhile;
363 Status = AE_CTRL_CONTINUE;
366 case AE_CTRL_PENDING:
368 ParserState->Aml = WalkState->AmlLastWhile;
374 ParserState->Aml = ParserState->Scope->ParseScope.PkgEnd;
381 * Predicate of an IF was true, and we are at the matching ELSE.
382 * Just close out this package
384 ParserState->Aml = AcpiPsGetNextPackageEnd (ParserState);
385 Status = AE_CTRL_PENDING;
390 * Either an IF/WHILE Predicate was false or we encountered a BREAK
391 * opcode. In both cases, we do not execute the rest of the
392 * package; We simply close out the parent (finishing the walk of
393 * this branch of the tree) and continue execution at the parent
396 ParserState->Aml = ParserState->Scope->ParseScope.PkgEnd;
398 /* In the case of a BREAK, just force a predicate (if any) to FALSE */
400 WalkState->ControlState->Common.Value = FALSE;
401 Status = AE_CTRL_END;
404 case AE_CTRL_TRANSFER:
406 /* A method call (invocation) -- transfer control */
408 Status = AE_CTRL_TRANSFER;
409 WalkState->PrevOp = Op;
410 WalkState->MethodCallOp = Op;
411 WalkState->MethodCallNode = (Op->Common.Value.Arg)->Common.Node;
413 /* Will return value (if any) be used by the caller? */
415 WalkState->ReturnUsed = AcpiDsIsResultUsed (Op, WalkState);
420 Status = CallbackStatus;
421 if ((CallbackStatus & AE_CODE_MASK) == AE_CODE_CONTROL)
428 return_ACPI_STATUS (Status);
432 /*******************************************************************************
434 * FUNCTION: AcpiPsParseAml
436 * PARAMETERS: WalkState - Current state
441 * DESCRIPTION: Parse raw AML and return a tree of ops
443 ******************************************************************************/
447 ACPI_WALK_STATE *WalkState)
450 ACPI_THREAD_STATE *Thread;
451 ACPI_THREAD_STATE *PrevWalkList = AcpiGbl_CurrentWalkList;
452 ACPI_WALK_STATE *PreviousWalkState;
455 ACPI_FUNCTION_TRACE (PsParseAml);
457 ACPI_DEBUG_PRINT ((ACPI_DB_PARSE,
458 "Entered with WalkState=%p Aml=%p size=%X\n",
459 WalkState, WalkState->ParserState.Aml,
460 WalkState->ParserState.AmlSize));
462 if (!WalkState->ParserState.Aml)
464 return_ACPI_STATUS (AE_NULL_OBJECT);
467 /* Create and initialize a new thread state */
469 Thread = AcpiUtCreateThreadState ();
472 if (WalkState->MethodDesc)
474 /* Executing a control method - additional cleanup */
476 AcpiDsTerminateControlMethod (WalkState->MethodDesc, WalkState);
479 AcpiDsDeleteWalkState (WalkState);
480 return_ACPI_STATUS (AE_NO_MEMORY);
483 WalkState->Thread = Thread;
486 * If executing a method, the starting SyncLevel is this method's
489 if (WalkState->MethodDesc)
491 WalkState->Thread->CurrentSyncLevel = WalkState->MethodDesc->Method.SyncLevel;
494 AcpiDsPushWalkState (WalkState, Thread);
497 * This global allows the AML debugger to get a handle to the currently
498 * executing control method.
500 AcpiGbl_CurrentWalkList = Thread;
503 * Execute the walk loop as long as there is a valid Walk State. This
504 * handles nested control method invocations without recursion.
506 ACPI_DEBUG_PRINT ((ACPI_DB_PARSE, "State=%p\n", WalkState));
511 if (ACPI_SUCCESS (Status))
514 * The ParseLoop executes AML until the method terminates
515 * or calls another method.
517 Status = AcpiPsParseLoop (WalkState);
520 ACPI_DEBUG_PRINT ((ACPI_DB_PARSE,
521 "Completed one call to walk loop, %s State=%p\n",
522 AcpiFormatException (Status), WalkState));
524 if (Status == AE_CTRL_TRANSFER)
527 * A method call was detected.
528 * Transfer control to the called control method
530 Status = AcpiDsCallControlMethod (Thread, WalkState, NULL);
531 if (ACPI_FAILURE (Status))
533 Status = AcpiDsMethodError (Status, WalkState);
537 * If the transfer to the new method method call worked, a new walk
538 * state was created -- get it
540 WalkState = AcpiDsGetCurrentWalkState (Thread);
543 else if (Status == AE_CTRL_TERMINATE)
547 else if ((Status != AE_OK) && (WalkState->MethodDesc))
549 /* Either the method parse or actual execution failed */
551 ACPI_ERROR_METHOD ("Method parse/execution failed",
552 WalkState->MethodNode, NULL, Status);
554 /* Check for possible multi-thread reentrancy problem */
556 if ((Status == AE_ALREADY_EXISTS) &&
557 (!(WalkState->MethodDesc->Method.InfoFlags & ACPI_METHOD_SERIALIZED)))
560 * Method is not serialized and tried to create an object
561 * twice. The probable cause is that the method cannot
562 * handle reentrancy. Mark as "pending serialized" now, and
563 * then mark "serialized" when the last thread exits.
565 WalkState->MethodDesc->Method.InfoFlags |=
566 ACPI_METHOD_SERIALIZED_PENDING;
570 /* We are done with this walk, move on to the parent if any */
572 WalkState = AcpiDsPopWalkState (Thread);
574 /* Reset the current scope to the beginning of scope stack */
576 AcpiDsScopeStackClear (WalkState);
579 * If we just returned from the execution of a control method or if we
580 * encountered an error during the method parse phase, there's lots of
583 if (((WalkState->ParseFlags & ACPI_PARSE_MODE_MASK) == ACPI_PARSE_EXECUTE) ||
584 (ACPI_FAILURE (Status)))
586 AcpiDsTerminateControlMethod (WalkState->MethodDesc, WalkState);
589 /* Delete this walk state and all linked control states */
591 AcpiPsCleanupScope (&WalkState->ParserState);
592 PreviousWalkState = WalkState;
594 ACPI_DEBUG_PRINT ((ACPI_DB_PARSE,
595 "ReturnValue=%p, ImplicitValue=%p State=%p\n",
596 WalkState->ReturnDesc, WalkState->ImplicitReturnObj, WalkState));
598 /* Check if we have restarted a preempted walk */
600 WalkState = AcpiDsGetCurrentWalkState (Thread);
603 if (ACPI_SUCCESS (Status))
606 * There is another walk state, restart it.
607 * If the method return value is not used by the parent,
608 * The object is deleted
610 if (!PreviousWalkState->ReturnDesc)
613 * In slack mode execution, if there is no return value
614 * we should implicitly return zero (0) as a default value.
616 if (AcpiGbl_EnableInterpreterSlack &&
617 !PreviousWalkState->ImplicitReturnObj)
619 PreviousWalkState->ImplicitReturnObj =
620 AcpiUtCreateIntegerObject ((UINT64) 0);
621 if (!PreviousWalkState->ImplicitReturnObj)
623 return_ACPI_STATUS (AE_NO_MEMORY);
627 /* Restart the calling control method */
629 Status = AcpiDsRestartControlMethod (WalkState,
630 PreviousWalkState->ImplicitReturnObj);
635 * We have a valid return value, delete any implicit
638 AcpiDsClearImplicitReturn (PreviousWalkState);
640 Status = AcpiDsRestartControlMethod (WalkState,
641 PreviousWalkState->ReturnDesc);
643 if (ACPI_SUCCESS (Status))
645 WalkState->WalkType |= ACPI_WALK_METHOD_RESTART;
650 /* On error, delete any return object or implicit return */
652 AcpiUtRemoveReference (PreviousWalkState->ReturnDesc);
653 AcpiDsClearImplicitReturn (PreviousWalkState);
658 * Just completed a 1st-level method, save the final internal return
661 else if (PreviousWalkState->CallerReturnDesc)
663 if (PreviousWalkState->ImplicitReturnObj)
665 *(PreviousWalkState->CallerReturnDesc) =
666 PreviousWalkState->ImplicitReturnObj;
670 /* NULL if no return value */
672 *(PreviousWalkState->CallerReturnDesc) =
673 PreviousWalkState->ReturnDesc;
678 if (PreviousWalkState->ReturnDesc)
680 /* Caller doesn't want it, must delete it */
682 AcpiUtRemoveReference (PreviousWalkState->ReturnDesc);
684 if (PreviousWalkState->ImplicitReturnObj)
686 /* Caller doesn't want it, must delete it */
688 AcpiUtRemoveReference (PreviousWalkState->ImplicitReturnObj);
692 AcpiDsDeleteWalkState (PreviousWalkState);
697 AcpiExReleaseAllMutexes (Thread);
698 AcpiUtDeleteGenericState (ACPI_CAST_PTR (ACPI_GENERIC_STATE, Thread));
699 AcpiGbl_CurrentWalkList = PrevWalkList;
700 return_ACPI_STATUS (Status);