1 /******************************************************************************
3 * Module Name: psparse - Parser top level AML parse routines
5 *****************************************************************************/
8 * Copyright (C) 2000 - 2018, 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[]
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_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 AcpiExStopTraceOpcode (Op
, WalkState
);
167 /* Delete this op and the subtree below it if asked to */
169 if (((WalkState
->ParseFlags
& ACPI_PARSE_TREE_MASK
) != ACPI_PARSE_DELETE_TREE
) ||
170 (WalkState
->OpInfo
->Class
== AML_CLASS_ARGUMENT
))
172 return_ACPI_STATUS (AE_OK
);
175 /* Make sure that we only delete this subtree */
177 if (Op
->Common
.Parent
)
179 Prev
= Op
->Common
.Parent
->Common
.Value
.Arg
;
182 /* Nothing more to do */
188 * Check if we need to replace the operator and its subtree
189 * with a return value op (placeholder op)
191 ParentInfo
= AcpiPsGetOpcodeInfo (Op
->Common
.Parent
->Common
.AmlOpcode
);
193 switch (ParentInfo
->Class
)
195 case AML_CLASS_CONTROL
:
199 case AML_CLASS_CREATE
:
201 * These opcodes contain TermArg operands. The current
202 * op must be replaced by a placeholder return op
204 ReplacementOp
= AcpiPsAllocOp (
205 AML_INT_RETURN_VALUE_OP
, Op
->Common
.Aml
);
208 Status
= AE_NO_MEMORY
;
212 case AML_CLASS_NAMED_OBJECT
:
214 * These opcodes contain TermArg operands. The current
215 * op must be replaced by a placeholder return op
217 if ((Op
->Common
.Parent
->Common
.AmlOpcode
== AML_REGION_OP
) ||
218 (Op
->Common
.Parent
->Common
.AmlOpcode
== AML_DATA_REGION_OP
) ||
219 (Op
->Common
.Parent
->Common
.AmlOpcode
== AML_BUFFER_OP
) ||
220 (Op
->Common
.Parent
->Common
.AmlOpcode
== AML_PACKAGE_OP
) ||
221 (Op
->Common
.Parent
->Common
.AmlOpcode
== AML_BANK_FIELD_OP
) ||
222 (Op
->Common
.Parent
->Common
.AmlOpcode
== AML_VARIABLE_PACKAGE_OP
))
224 ReplacementOp
= AcpiPsAllocOp (
225 AML_INT_RETURN_VALUE_OP
, Op
->Common
.Aml
);
228 Status
= AE_NO_MEMORY
;
231 else if ((Op
->Common
.Parent
->Common
.AmlOpcode
== AML_NAME_OP
) &&
232 (WalkState
->PassNumber
<= ACPI_IMODE_LOAD_PASS2
))
234 if ((Op
->Common
.AmlOpcode
== AML_BUFFER_OP
) ||
235 (Op
->Common
.AmlOpcode
== AML_PACKAGE_OP
) ||
236 (Op
->Common
.AmlOpcode
== AML_VARIABLE_PACKAGE_OP
))
238 ReplacementOp
= AcpiPsAllocOp (Op
->Common
.AmlOpcode
,
242 Status
= AE_NO_MEMORY
;
246 ReplacementOp
->Named
.Data
= Op
->Named
.Data
;
247 ReplacementOp
->Named
.Length
= Op
->Named
.Length
;
255 ReplacementOp
= AcpiPsAllocOp (
256 AML_INT_RETURN_VALUE_OP
, Op
->Common
.Aml
);
259 Status
= AE_NO_MEMORY
;
263 /* We must unlink this op from the parent tree */
267 /* This op is the first in the list */
271 ReplacementOp
->Common
.Parent
= Op
->Common
.Parent
;
272 ReplacementOp
->Common
.Value
.Arg
= NULL
;
273 ReplacementOp
->Common
.Node
= Op
->Common
.Node
;
274 Op
->Common
.Parent
->Common
.Value
.Arg
= ReplacementOp
;
275 ReplacementOp
->Common
.Next
= Op
->Common
.Next
;
279 Op
->Common
.Parent
->Common
.Value
.Arg
= Op
->Common
.Next
;
283 /* Search the parent list */
287 /* Traverse all siblings in the parent's argument list */
289 Next
= Prev
->Common
.Next
;
294 ReplacementOp
->Common
.Parent
= Op
->Common
.Parent
;
295 ReplacementOp
->Common
.Value
.Arg
= NULL
;
296 ReplacementOp
->Common
.Node
= Op
->Common
.Node
;
297 Prev
->Common
.Next
= ReplacementOp
;
298 ReplacementOp
->Common
.Next
= Op
->Common
.Next
;
303 Prev
->Common
.Next
= Op
->Common
.Next
;
314 /* Now we can actually delete the subtree rooted at Op */
316 AcpiPsDeleteParseTree (Op
);
317 return_ACPI_STATUS (Status
);
321 /*******************************************************************************
323 * FUNCTION: AcpiPsNextParseState
325 * PARAMETERS: WalkState - Current state
326 * Op - Current parse op
327 * CallbackStatus - Status from previous operation
331 * DESCRIPTION: Update the parser state based upon the return exception from
332 * the parser callback.
334 ******************************************************************************/
337 AcpiPsNextParseState (
338 ACPI_WALK_STATE
*WalkState
,
339 ACPI_PARSE_OBJECT
*Op
,
340 ACPI_STATUS CallbackStatus
)
342 ACPI_PARSE_STATE
*ParserState
= &WalkState
->ParserState
;
343 ACPI_STATUS Status
= AE_CTRL_PENDING
;
346 ACPI_FUNCTION_TRACE_PTR (PsNextParseState
, Op
);
349 switch (CallbackStatus
)
351 case AE_CTRL_TERMINATE
:
353 * A control method was terminated via a RETURN statement.
354 * The walk of this method is complete.
356 ParserState
->Aml
= ParserState
->AmlEnd
;
357 Status
= AE_CTRL_TERMINATE
;
362 ParserState
->Aml
= WalkState
->AmlLastWhile
;
363 WalkState
->ControlState
->Common
.Value
= FALSE
;
364 Status
= AE_CTRL_BREAK
;
367 case AE_CTRL_CONTINUE
:
369 ParserState
->Aml
= WalkState
->AmlLastWhile
;
370 Status
= AE_CTRL_CONTINUE
;
373 case AE_CTRL_PENDING
:
375 ParserState
->Aml
= WalkState
->AmlLastWhile
;
381 ParserState
->Aml
= ParserState
->Scope
->ParseScope
.PkgEnd
;
388 * Predicate of an IF was true, and we are at the matching ELSE.
389 * Just close out this package
391 ParserState
->Aml
= AcpiPsGetNextPackageEnd (ParserState
);
392 Status
= AE_CTRL_PENDING
;
397 * Either an IF/WHILE Predicate was false or we encountered a BREAK
398 * opcode. In both cases, we do not execute the rest of the
399 * package; We simply close out the parent (finishing the walk of
400 * this branch of the tree) and continue execution at the parent
403 ParserState
->Aml
= ParserState
->Scope
->ParseScope
.PkgEnd
;
405 /* In the case of a BREAK, just force a predicate (if any) to FALSE */
407 WalkState
->ControlState
->Common
.Value
= FALSE
;
408 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
);
427 Status
= CallbackStatus
;
428 if ((CallbackStatus
& AE_CODE_MASK
) == AE_CODE_CONTROL
)
435 return_ACPI_STATUS (Status
);
439 /*******************************************************************************
441 * FUNCTION: AcpiPsParseAml
443 * PARAMETERS: WalkState - Current state
448 * DESCRIPTION: Parse raw AML and return a tree of ops
450 ******************************************************************************/
454 ACPI_WALK_STATE
*WalkState
)
457 ACPI_THREAD_STATE
*Thread
;
458 ACPI_THREAD_STATE
*PrevWalkList
= AcpiGbl_CurrentWalkList
;
459 ACPI_WALK_STATE
*PreviousWalkState
;
462 ACPI_FUNCTION_TRACE (PsParseAml
);
464 ACPI_DEBUG_PRINT ((ACPI_DB_PARSE
,
465 "Entered with WalkState=%p Aml=%p size=%X\n",
466 WalkState
, WalkState
->ParserState
.Aml
,
467 WalkState
->ParserState
.AmlSize
));
469 if (!WalkState
->ParserState
.Aml
)
471 return_ACPI_STATUS (AE_BAD_ADDRESS
);
474 /* Create and initialize a new thread state */
476 Thread
= AcpiUtCreateThreadState ();
479 if (WalkState
->MethodDesc
)
481 /* Executing a control method - additional cleanup */
483 AcpiDsTerminateControlMethod (WalkState
->MethodDesc
, WalkState
);
486 AcpiDsDeleteWalkState (WalkState
);
487 return_ACPI_STATUS (AE_NO_MEMORY
);
490 WalkState
->Thread
= Thread
;
493 * If executing a method, the starting SyncLevel is this method's
496 if (WalkState
->MethodDesc
)
498 WalkState
->Thread
->CurrentSyncLevel
=
499 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
546 *, a new walk 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 AcpiExExitInterpreter ();
560 if (Status
== AE_ABORT_METHOD
)
562 AcpiNsPrintNodePathname (
563 WalkState
->MethodNode
, "Method aborted:");
568 ACPI_ERROR_METHOD ("Method parse/execution failed",
569 WalkState
->MethodNode
, NULL
, Status
);
571 AcpiExEnterInterpreter ();
573 /* Check for possible multi-thread reentrancy problem */
575 if ((Status
== AE_ALREADY_EXISTS
) &&
576 (!(WalkState
->MethodDesc
->Method
.InfoFlags
&
577 ACPI_METHOD_SERIALIZED
)))
580 * Method is not serialized and tried to create an object
581 * twice. The probable cause is that the method cannot
582 * handle reentrancy. Mark as "pending serialized" now, and
583 * then mark "serialized" when the last thread exits.
585 WalkState
->MethodDesc
->Method
.InfoFlags
|=
586 ACPI_METHOD_SERIALIZED_PENDING
;
590 /* We are done with this walk, move on to the parent if any */
592 WalkState
= AcpiDsPopWalkState (Thread
);
594 /* Reset the current scope to the beginning of scope stack */
596 AcpiDsScopeStackClear (WalkState
);
599 * If we just returned from the execution of a control method or if we
600 * encountered an error during the method parse phase, there's lots of
603 if (((WalkState
->ParseFlags
& ACPI_PARSE_MODE_MASK
) ==
604 ACPI_PARSE_EXECUTE
&&
605 !(WalkState
->ParseFlags
& ACPI_PARSE_MODULE_LEVEL
)) ||
606 (ACPI_FAILURE (Status
)))
608 AcpiDsTerminateControlMethod (WalkState
->MethodDesc
, WalkState
);
611 /* Delete this walk state and all linked control states */
613 AcpiPsCleanupScope (&WalkState
->ParserState
);
614 PreviousWalkState
= WalkState
;
616 ACPI_DEBUG_PRINT ((ACPI_DB_PARSE
,
617 "ReturnValue=%p, ImplicitValue=%p State=%p\n",
618 WalkState
->ReturnDesc
, WalkState
->ImplicitReturnObj
, WalkState
));
620 /* Check if we have restarted a preempted walk */
622 WalkState
= AcpiDsGetCurrentWalkState (Thread
);
625 if (ACPI_SUCCESS (Status
))
628 * There is another walk state, restart it.
629 * If the method return value is not used by the parent,
630 * The object is deleted
632 if (!PreviousWalkState
->ReturnDesc
)
635 * In slack mode execution, if there is no return value
636 * we should implicitly return zero (0) as a default value.
638 if (AcpiGbl_EnableInterpreterSlack
&&
639 !PreviousWalkState
->ImplicitReturnObj
)
641 PreviousWalkState
->ImplicitReturnObj
=
642 AcpiUtCreateIntegerObject ((UINT64
) 0);
643 if (!PreviousWalkState
->ImplicitReturnObj
)
645 return_ACPI_STATUS (AE_NO_MEMORY
);
649 /* Restart the calling control method */
651 Status
= AcpiDsRestartControlMethod (WalkState
,
652 PreviousWalkState
->ImplicitReturnObj
);
657 * We have a valid return value, delete any implicit
660 AcpiDsClearImplicitReturn (PreviousWalkState
);
662 Status
= AcpiDsRestartControlMethod (WalkState
,
663 PreviousWalkState
->ReturnDesc
);
665 if (ACPI_SUCCESS (Status
))
667 WalkState
->WalkType
|= ACPI_WALK_METHOD_RESTART
;
672 /* On error, delete any return object or implicit return */
674 AcpiUtRemoveReference (PreviousWalkState
->ReturnDesc
);
675 AcpiDsClearImplicitReturn (PreviousWalkState
);
680 * Just completed a 1st-level method, save the final internal return
683 else if (PreviousWalkState
->CallerReturnDesc
)
685 if (PreviousWalkState
->ImplicitReturnObj
)
687 *(PreviousWalkState
->CallerReturnDesc
) =
688 PreviousWalkState
->ImplicitReturnObj
;
692 /* NULL if no return value */
694 *(PreviousWalkState
->CallerReturnDesc
) =
695 PreviousWalkState
->ReturnDesc
;
700 if (PreviousWalkState
->ReturnDesc
)
702 /* Caller doesn't want it, must delete it */
704 AcpiUtRemoveReference (PreviousWalkState
->ReturnDesc
);
706 if (PreviousWalkState
->ImplicitReturnObj
)
708 /* Caller doesn't want it, must delete it */
710 AcpiUtRemoveReference (PreviousWalkState
->ImplicitReturnObj
);
714 AcpiDsDeleteWalkState (PreviousWalkState
);
719 AcpiExReleaseAllMutexes (Thread
);
720 AcpiUtDeleteGenericState (ACPI_CAST_PTR (ACPI_GENERIC_STATE
, Thread
));
721 AcpiGbl_CurrentWalkList
= PrevWalkList
;
722 return_ACPI_STATUS (Status
);