1 /******************************************************************************
3 * Module Name: psparse - Parser top level AML parse routines
5 *****************************************************************************/
8 * Copyright (C) 2000 - 2013, 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[]
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
:
197 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
:
211 * These opcodes contain TermArg operands. The current
212 * op must be replaced by a placeholder return op
214 if ((Op
->Common
.Parent
->Common
.AmlOpcode
== AML_REGION_OP
) ||
215 (Op
->Common
.Parent
->Common
.AmlOpcode
== AML_DATA_REGION_OP
) ||
216 (Op
->Common
.Parent
->Common
.AmlOpcode
== AML_BUFFER_OP
) ||
217 (Op
->Common
.Parent
->Common
.AmlOpcode
== AML_PACKAGE_OP
) ||
218 (Op
->Common
.Parent
->Common
.AmlOpcode
== AML_BANK_FIELD_OP
) ||
219 (Op
->Common
.Parent
->Common
.AmlOpcode
== AML_VAR_PACKAGE_OP
))
221 ReplacementOp
= AcpiPsAllocOp (AML_INT_RETURN_VALUE_OP
);
224 Status
= AE_NO_MEMORY
;
227 else if ((Op
->Common
.Parent
->Common
.AmlOpcode
== AML_NAME_OP
) &&
228 (WalkState
->PassNumber
<= ACPI_IMODE_LOAD_PASS2
))
230 if ((Op
->Common
.AmlOpcode
== AML_BUFFER_OP
) ||
231 (Op
->Common
.AmlOpcode
== AML_PACKAGE_OP
) ||
232 (Op
->Common
.AmlOpcode
== AML_VAR_PACKAGE_OP
))
234 ReplacementOp
= AcpiPsAllocOp (Op
->Common
.AmlOpcode
);
237 Status
= AE_NO_MEMORY
;
241 ReplacementOp
->Named
.Data
= Op
->Named
.Data
;
242 ReplacementOp
->Named
.Length
= Op
->Named
.Length
;
250 ReplacementOp
= AcpiPsAllocOp (AML_INT_RETURN_VALUE_OP
);
253 Status
= AE_NO_MEMORY
;
257 /* We must unlink this op from the parent tree */
261 /* This op is the first in the list */
265 ReplacementOp
->Common
.Parent
= Op
->Common
.Parent
;
266 ReplacementOp
->Common
.Value
.Arg
= NULL
;
267 ReplacementOp
->Common
.Node
= Op
->Common
.Node
;
268 Op
->Common
.Parent
->Common
.Value
.Arg
= ReplacementOp
;
269 ReplacementOp
->Common
.Next
= Op
->Common
.Next
;
273 Op
->Common
.Parent
->Common
.Value
.Arg
= Op
->Common
.Next
;
277 /* Search the parent list */
281 /* Traverse all siblings in the parent's argument list */
283 Next
= Prev
->Common
.Next
;
288 ReplacementOp
->Common
.Parent
= Op
->Common
.Parent
;
289 ReplacementOp
->Common
.Value
.Arg
= NULL
;
290 ReplacementOp
->Common
.Node
= Op
->Common
.Node
;
291 Prev
->Common
.Next
= ReplacementOp
;
292 ReplacementOp
->Common
.Next
= Op
->Common
.Next
;
297 Prev
->Common
.Next
= Op
->Common
.Next
;
308 /* Now we can actually delete the subtree rooted at Op */
310 AcpiPsDeleteParseTree (Op
);
311 return_ACPI_STATUS (Status
);
315 /*******************************************************************************
317 * FUNCTION: AcpiPsNextParseState
319 * PARAMETERS: WalkState - Current state
320 * Op - Current parse op
321 * CallbackStatus - Status from previous operation
325 * DESCRIPTION: Update the parser state based upon the return exception from
326 * the parser callback.
328 ******************************************************************************/
331 AcpiPsNextParseState (
332 ACPI_WALK_STATE
*WalkState
,
333 ACPI_PARSE_OBJECT
*Op
,
334 ACPI_STATUS CallbackStatus
)
336 ACPI_PARSE_STATE
*ParserState
= &WalkState
->ParserState
;
337 ACPI_STATUS Status
= AE_CTRL_PENDING
;
340 ACPI_FUNCTION_TRACE_PTR (PsNextParseState
, Op
);
343 switch (CallbackStatus
)
345 case AE_CTRL_TERMINATE
:
347 * A control method was terminated via a RETURN statement.
348 * The walk of this method is complete.
350 ParserState
->Aml
= ParserState
->AmlEnd
;
351 Status
= AE_CTRL_TERMINATE
;
356 ParserState
->Aml
= WalkState
->AmlLastWhile
;
357 WalkState
->ControlState
->Common
.Value
= FALSE
;
358 Status
= AE_CTRL_BREAK
;
361 case AE_CTRL_CONTINUE
:
363 ParserState
->Aml
= WalkState
->AmlLastWhile
;
364 Status
= AE_CTRL_CONTINUE
;
367 case AE_CTRL_PENDING
:
369 ParserState
->Aml
= WalkState
->AmlLastWhile
;
375 ParserState
->Aml
= ParserState
->Scope
->ParseScope
.PkgEnd
;
382 * Predicate of an IF was true, and we are at the matching ELSE.
383 * Just close out this package
385 ParserState
->Aml
= AcpiPsGetNextPackageEnd (ParserState
);
386 Status
= AE_CTRL_PENDING
;
391 * Either an IF/WHILE Predicate was false or we encountered a BREAK
392 * opcode. In both cases, we do not execute the rest of the
393 * package; We simply close out the parent (finishing the walk of
394 * this branch of the tree) and continue execution at the parent
397 ParserState
->Aml
= ParserState
->Scope
->ParseScope
.PkgEnd
;
399 /* In the case of a BREAK, just force a predicate (if any) to FALSE */
401 WalkState
->ControlState
->Common
.Value
= FALSE
;
402 Status
= AE_CTRL_END
;
405 case AE_CTRL_TRANSFER
:
407 /* A method call (invocation) -- transfer control */
409 Status
= AE_CTRL_TRANSFER
;
410 WalkState
->PrevOp
= Op
;
411 WalkState
->MethodCallOp
= Op
;
412 WalkState
->MethodCallNode
= (Op
->Common
.Value
.Arg
)->Common
.Node
;
414 /* Will return value (if any) be used by the caller? */
416 WalkState
->ReturnUsed
= AcpiDsIsResultUsed (Op
, WalkState
);
421 Status
= CallbackStatus
;
422 if ((CallbackStatus
& AE_CODE_MASK
) == AE_CODE_CONTROL
)
429 return_ACPI_STATUS (Status
);
433 /*******************************************************************************
435 * FUNCTION: AcpiPsParseAml
437 * PARAMETERS: WalkState - Current state
442 * DESCRIPTION: Parse raw AML and return a tree of ops
444 ******************************************************************************/
448 ACPI_WALK_STATE
*WalkState
)
451 ACPI_THREAD_STATE
*Thread
;
452 ACPI_THREAD_STATE
*PrevWalkList
= AcpiGbl_CurrentWalkList
;
453 ACPI_WALK_STATE
*PreviousWalkState
;
456 ACPI_FUNCTION_TRACE (PsParseAml
);
458 ACPI_DEBUG_PRINT ((ACPI_DB_PARSE
,
459 "Entered with WalkState=%p Aml=%p size=%X\n",
460 WalkState
, WalkState
->ParserState
.Aml
,
461 WalkState
->ParserState
.AmlSize
));
463 if (!WalkState
->ParserState
.Aml
)
465 return_ACPI_STATUS (AE_NULL_OBJECT
);
468 /* Create and initialize a new thread state */
470 Thread
= AcpiUtCreateThreadState ();
473 if (WalkState
->MethodDesc
)
475 /* Executing a control method - additional cleanup */
477 AcpiDsTerminateControlMethod (WalkState
->MethodDesc
, WalkState
);
480 AcpiDsDeleteWalkState (WalkState
);
481 return_ACPI_STATUS (AE_NO_MEMORY
);
484 WalkState
->Thread
= Thread
;
487 * If executing a method, the starting SyncLevel is this method's
490 if (WalkState
->MethodDesc
)
492 WalkState
->Thread
->CurrentSyncLevel
= WalkState
->MethodDesc
->Method
.SyncLevel
;
495 AcpiDsPushWalkState (WalkState
, Thread
);
498 * This global allows the AML debugger to get a handle to the currently
499 * executing control method.
501 AcpiGbl_CurrentWalkList
= Thread
;
504 * Execute the walk loop as long as there is a valid Walk State. This
505 * handles nested control method invocations without recursion.
507 ACPI_DEBUG_PRINT ((ACPI_DB_PARSE
, "State=%p\n", WalkState
));
512 if (ACPI_SUCCESS (Status
))
515 * The ParseLoop executes AML until the method terminates
516 * or calls another method.
518 Status
= AcpiPsParseLoop (WalkState
);
521 ACPI_DEBUG_PRINT ((ACPI_DB_PARSE
,
522 "Completed one call to walk loop, %s State=%p\n",
523 AcpiFormatException (Status
), WalkState
));
525 if (Status
== AE_CTRL_TRANSFER
)
528 * A method call was detected.
529 * Transfer control to the called control method
531 Status
= AcpiDsCallControlMethod (Thread
, WalkState
, NULL
);
532 if (ACPI_FAILURE (Status
))
534 Status
= AcpiDsMethodError (Status
, WalkState
);
538 * If the transfer to the new method method call worked, a new walk
539 * state was created -- get it
541 WalkState
= AcpiDsGetCurrentWalkState (Thread
);
544 else if (Status
== AE_CTRL_TERMINATE
)
548 else if ((Status
!= AE_OK
) && (WalkState
->MethodDesc
))
550 /* Either the method parse or actual execution failed */
552 ACPI_ERROR_METHOD ("Method parse/execution failed",
553 WalkState
->MethodNode
, NULL
, Status
);
555 /* Check for possible multi-thread reentrancy problem */
557 if ((Status
== AE_ALREADY_EXISTS
) &&
558 (!(WalkState
->MethodDesc
->Method
.InfoFlags
& ACPI_METHOD_SERIALIZED
)))
561 * Method is not serialized and tried to create an object
562 * twice. The probable cause is that the method cannot
563 * handle reentrancy. Mark as "pending serialized" now, and
564 * then mark "serialized" when the last thread exits.
566 WalkState
->MethodDesc
->Method
.InfoFlags
|=
567 ACPI_METHOD_SERIALIZED_PENDING
;
571 /* We are done with this walk, move on to the parent if any */
573 WalkState
= AcpiDsPopWalkState (Thread
);
575 /* Reset the current scope to the beginning of scope stack */
577 AcpiDsScopeStackClear (WalkState
);
580 * If we just returned from the execution of a control method or if we
581 * encountered an error during the method parse phase, there's lots of
584 if (((WalkState
->ParseFlags
& ACPI_PARSE_MODE_MASK
) == ACPI_PARSE_EXECUTE
) ||
585 (ACPI_FAILURE (Status
)))
587 AcpiDsTerminateControlMethod (WalkState
->MethodDesc
, WalkState
);
590 /* Delete this walk state and all linked control states */
592 AcpiPsCleanupScope (&WalkState
->ParserState
);
593 PreviousWalkState
= WalkState
;
595 ACPI_DEBUG_PRINT ((ACPI_DB_PARSE
,
596 "ReturnValue=%p, ImplicitValue=%p State=%p\n",
597 WalkState
->ReturnDesc
, WalkState
->ImplicitReturnObj
, WalkState
));
599 /* Check if we have restarted a preempted walk */
601 WalkState
= AcpiDsGetCurrentWalkState (Thread
);
604 if (ACPI_SUCCESS (Status
))
607 * There is another walk state, restart it.
608 * If the method return value is not used by the parent,
609 * The object is deleted
611 if (!PreviousWalkState
->ReturnDesc
)
614 * In slack mode execution, if there is no return value
615 * we should implicitly return zero (0) as a default value.
617 if (AcpiGbl_EnableInterpreterSlack
&&
618 !PreviousWalkState
->ImplicitReturnObj
)
620 PreviousWalkState
->ImplicitReturnObj
=
621 AcpiUtCreateIntegerObject ((UINT64
) 0);
622 if (!PreviousWalkState
->ImplicitReturnObj
)
624 return_ACPI_STATUS (AE_NO_MEMORY
);
628 /* Restart the calling control method */
630 Status
= AcpiDsRestartControlMethod (WalkState
,
631 PreviousWalkState
->ImplicitReturnObj
);
636 * We have a valid return value, delete any implicit
639 AcpiDsClearImplicitReturn (PreviousWalkState
);
641 Status
= AcpiDsRestartControlMethod (WalkState
,
642 PreviousWalkState
->ReturnDesc
);
644 if (ACPI_SUCCESS (Status
))
646 WalkState
->WalkType
|= ACPI_WALK_METHOD_RESTART
;
651 /* On error, delete any return object or implicit return */
653 AcpiUtRemoveReference (PreviousWalkState
->ReturnDesc
);
654 AcpiDsClearImplicitReturn (PreviousWalkState
);
659 * Just completed a 1st-level method, save the final internal return
662 else if (PreviousWalkState
->CallerReturnDesc
)
664 if (PreviousWalkState
->ImplicitReturnObj
)
666 *(PreviousWalkState
->CallerReturnDesc
) =
667 PreviousWalkState
->ImplicitReturnObj
;
671 /* NULL if no return value */
673 *(PreviousWalkState
->CallerReturnDesc
) =
674 PreviousWalkState
->ReturnDesc
;
679 if (PreviousWalkState
->ReturnDesc
)
681 /* Caller doesn't want it, must delete it */
683 AcpiUtRemoveReference (PreviousWalkState
->ReturnDesc
);
685 if (PreviousWalkState
->ImplicitReturnObj
)
687 /* Caller doesn't want it, must delete it */
689 AcpiUtRemoveReference (PreviousWalkState
->ImplicitReturnObj
);
693 AcpiDsDeleteWalkState (PreviousWalkState
);
698 AcpiExReleaseAllMutexes (Thread
);
699 AcpiUtDeleteGenericState (ACPI_CAST_PTR (ACPI_GENERIC_STATE
, Thread
));
700 AcpiGbl_CurrentWalkList
= PrevWalkList
;
701 return_ACPI_STATUS (Status
);