Sync ACPICA with Intel's version 20160831.
[dragonfly.git] / sys / contrib / dev / acpica / source / components / parser / psparse.c
blob8f230320bab45c909061bc33458f21482ff94884
1 /******************************************************************************
3 * Module Name: psparse - Parser top level AML parse routines
5 *****************************************************************************/
7 /*
8 * Copyright (C) 2000 - 2016, Intel Corp.
9 * All rights reserved.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
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.
30 * NO WARRANTY
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[]
53 #include "acpi.h"
54 #include "accommon.h"
55 #include "acparser.h"
56 #include "acdispat.h"
57 #include "amlcode.h"
58 #include "acinterp.h"
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 ******************************************************************************/
76 UINT32
77 AcpiPsGetOpcodeSize (
78 UINT32 Opcode)
81 /* Extended (2-byte) opcode if > 255 */
83 if (Opcode > 0x00FF)
85 return (2);
88 /* Otherwise, just a single byte opcode */
90 return (1);
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 ******************************************************************************/
106 UINT16
107 AcpiPsPeekOpcode (
108 ACPI_PARSE_STATE *ParserState)
110 UINT8 *Aml;
111 UINT16 Opcode;
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 */
121 Aml++;
122 Opcode = (UINT16) ((Opcode << 8) | ACPI_GET8 (Aml));
125 return (Opcode);
129 /*******************************************************************************
131 * FUNCTION: AcpiPsCompleteThisOp
133 * PARAMETERS: WalkState - Current State
134 * Op - Op to complete
136 * RETURN: Status
138 * DESCRIPTION: Perform any cleanup at the completion of an Op.
140 ******************************************************************************/
142 ACPI_STATUS
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 */
159 if (!Op)
161 return_ACPI_STATUS (AE_OK); /* OK for now */
164 AcpiExStopTraceOpcode (Op, WalkState);
166 /* Delete this op and the subtree below it if asked to */
168 if (((WalkState->ParseFlags & ACPI_PARSE_TREE_MASK) != ACPI_PARSE_DELETE_TREE) ||
169 (WalkState->OpInfo->Class == AML_CLASS_ARGUMENT))
171 return_ACPI_STATUS (AE_OK);
174 /* Make sure that we only delete this subtree */
176 if (Op->Common.Parent)
178 Prev = Op->Common.Parent->Common.Value.Arg;
179 if (!Prev)
181 /* Nothing more to do */
183 goto Cleanup;
187 * Check if we need to replace the operator and its subtree
188 * with a return value op (placeholder op)
190 ParentInfo = AcpiPsGetOpcodeInfo (Op->Common.Parent->Common.AmlOpcode);
192 switch (ParentInfo->Class)
194 case AML_CLASS_CONTROL:
196 break;
198 case AML_CLASS_CREATE:
200 * These opcodes contain TermArg operands. The current
201 * op must be replaced by a placeholder return op
203 ReplacementOp = AcpiPsAllocOp (
204 AML_INT_RETURN_VALUE_OP, Op->Common.Aml);
205 if (!ReplacementOp)
207 Status = AE_NO_MEMORY;
209 break;
211 case AML_CLASS_NAMED_OBJECT:
213 * These opcodes contain TermArg operands. The current
214 * op must be replaced by a placeholder return op
216 if ((Op->Common.Parent->Common.AmlOpcode == AML_REGION_OP) ||
217 (Op->Common.Parent->Common.AmlOpcode == AML_DATA_REGION_OP) ||
218 (Op->Common.Parent->Common.AmlOpcode == AML_BUFFER_OP) ||
219 (Op->Common.Parent->Common.AmlOpcode == AML_PACKAGE_OP) ||
220 (Op->Common.Parent->Common.AmlOpcode == AML_BANK_FIELD_OP) ||
221 (Op->Common.Parent->Common.AmlOpcode == AML_VAR_PACKAGE_OP))
223 ReplacementOp = AcpiPsAllocOp (
224 AML_INT_RETURN_VALUE_OP, Op->Common.Aml);
225 if (!ReplacementOp)
227 Status = AE_NO_MEMORY;
230 else if ((Op->Common.Parent->Common.AmlOpcode == AML_NAME_OP) &&
231 (WalkState->PassNumber <= ACPI_IMODE_LOAD_PASS2))
233 if ((Op->Common.AmlOpcode == AML_BUFFER_OP) ||
234 (Op->Common.AmlOpcode == AML_PACKAGE_OP) ||
235 (Op->Common.AmlOpcode == AML_VAR_PACKAGE_OP))
237 ReplacementOp = AcpiPsAllocOp (Op->Common.AmlOpcode,
238 Op->Common.Aml);
239 if (!ReplacementOp)
241 Status = AE_NO_MEMORY;
243 else
245 ReplacementOp->Named.Data = Op->Named.Data;
246 ReplacementOp->Named.Length = Op->Named.Length;
250 break;
252 default:
254 ReplacementOp = AcpiPsAllocOp (
255 AML_INT_RETURN_VALUE_OP, Op->Common.Aml);
256 if (!ReplacementOp)
258 Status = AE_NO_MEMORY;
262 /* We must unlink this op from the parent tree */
264 if (Prev == Op)
266 /* This op is the first in the list */
268 if (ReplacementOp)
270 ReplacementOp->Common.Parent = Op->Common.Parent;
271 ReplacementOp->Common.Value.Arg = NULL;
272 ReplacementOp->Common.Node = Op->Common.Node;
273 Op->Common.Parent->Common.Value.Arg = ReplacementOp;
274 ReplacementOp->Common.Next = Op->Common.Next;
276 else
278 Op->Common.Parent->Common.Value.Arg = Op->Common.Next;
282 /* Search the parent list */
284 else while (Prev)
286 /* Traverse all siblings in the parent's argument list */
288 Next = Prev->Common.Next;
289 if (Next == Op)
291 if (ReplacementOp)
293 ReplacementOp->Common.Parent = Op->Common.Parent;
294 ReplacementOp->Common.Value.Arg = NULL;
295 ReplacementOp->Common.Node = Op->Common.Node;
296 Prev->Common.Next = ReplacementOp;
297 ReplacementOp->Common.Next = Op->Common.Next;
298 Next = NULL;
300 else
302 Prev->Common.Next = Op->Common.Next;
303 Next = NULL;
306 Prev = Next;
311 Cleanup:
313 /* Now we can actually delete the subtree rooted at Op */
315 AcpiPsDeleteParseTree (Op);
316 return_ACPI_STATUS (Status);
320 /*******************************************************************************
322 * FUNCTION: AcpiPsNextParseState
324 * PARAMETERS: WalkState - Current state
325 * Op - Current parse op
326 * CallbackStatus - Status from previous operation
328 * RETURN: Status
330 * DESCRIPTION: Update the parser state based upon the return exception from
331 * the parser callback.
333 ******************************************************************************/
335 ACPI_STATUS
336 AcpiPsNextParseState (
337 ACPI_WALK_STATE *WalkState,
338 ACPI_PARSE_OBJECT *Op,
339 ACPI_STATUS CallbackStatus)
341 ACPI_PARSE_STATE *ParserState = &WalkState->ParserState;
342 ACPI_STATUS Status = AE_CTRL_PENDING;
345 ACPI_FUNCTION_TRACE_PTR (PsNextParseState, Op);
348 switch (CallbackStatus)
350 case AE_CTRL_TERMINATE:
352 * A control method was terminated via a RETURN statement.
353 * The walk of this method is complete.
355 ParserState->Aml = ParserState->AmlEnd;
356 Status = AE_CTRL_TERMINATE;
357 break;
359 case AE_CTRL_BREAK:
361 ParserState->Aml = WalkState->AmlLastWhile;
362 WalkState->ControlState->Common.Value = FALSE;
363 Status = AE_CTRL_BREAK;
364 break;
366 case AE_CTRL_CONTINUE:
368 ParserState->Aml = WalkState->AmlLastWhile;
369 Status = AE_CTRL_CONTINUE;
370 break;
372 case AE_CTRL_PENDING:
374 ParserState->Aml = WalkState->AmlLastWhile;
375 break;
377 #if 0
378 case AE_CTRL_SKIP:
380 ParserState->Aml = ParserState->Scope->ParseScope.PkgEnd;
381 Status = AE_OK;
382 break;
383 #endif
385 case AE_CTRL_TRUE:
387 * Predicate of an IF was true, and we are at the matching ELSE.
388 * Just close out this package
390 ParserState->Aml = AcpiPsGetNextPackageEnd (ParserState);
391 Status = AE_CTRL_PENDING;
392 break;
394 case AE_CTRL_FALSE:
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
400 * level.
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;
408 break;
410 case AE_CTRL_TRANSFER:
412 /* A method call (invocation) -- transfer control */
414 Status = AE_CTRL_TRANSFER;
415 WalkState->PrevOp = Op;
416 WalkState->MethodCallOp = Op;
417 WalkState->MethodCallNode = (Op->Common.Value.Arg)->Common.Node;
419 /* Will return value (if any) be used by the caller? */
421 WalkState->ReturnUsed = AcpiDsIsResultUsed (Op, WalkState);
422 break;
424 default:
426 Status = CallbackStatus;
427 if ((CallbackStatus & AE_CODE_MASK) == AE_CODE_CONTROL)
429 Status = AE_OK;
431 break;
434 return_ACPI_STATUS (Status);
438 /*******************************************************************************
440 * FUNCTION: AcpiPsParseAml
442 * PARAMETERS: WalkState - Current state
445 * RETURN: Status
447 * DESCRIPTION: Parse raw AML and return a tree of ops
449 ******************************************************************************/
451 ACPI_STATUS
452 AcpiPsParseAml (
453 ACPI_WALK_STATE *WalkState)
455 ACPI_STATUS Status;
456 ACPI_THREAD_STATE *Thread;
457 ACPI_THREAD_STATE *PrevWalkList = AcpiGbl_CurrentWalkList;
458 ACPI_WALK_STATE *PreviousWalkState;
461 ACPI_FUNCTION_TRACE (PsParseAml);
463 ACPI_DEBUG_PRINT ((ACPI_DB_PARSE,
464 "Entered with WalkState=%p Aml=%p size=%X\n",
465 WalkState, WalkState->ParserState.Aml,
466 WalkState->ParserState.AmlSize));
468 if (!WalkState->ParserState.Aml)
470 return_ACPI_STATUS (AE_NULL_OBJECT);
473 /* Create and initialize a new thread state */
475 Thread = AcpiUtCreateThreadState ();
476 if (!Thread)
478 if (WalkState->MethodDesc)
480 /* Executing a control method - additional cleanup */
482 AcpiDsTerminateControlMethod (WalkState->MethodDesc, WalkState);
485 AcpiDsDeleteWalkState (WalkState);
486 return_ACPI_STATUS (AE_NO_MEMORY);
489 WalkState->Thread = Thread;
492 * If executing a method, the starting SyncLevel is this method's
493 * SyncLevel
495 if (WalkState->MethodDesc)
497 WalkState->Thread->CurrentSyncLevel =
498 WalkState->MethodDesc->Method.SyncLevel;
501 AcpiDsPushWalkState (WalkState, Thread);
504 * This global allows the AML debugger to get a handle to the currently
505 * executing control method.
507 AcpiGbl_CurrentWalkList = Thread;
510 * Execute the walk loop as long as there is a valid Walk State. This
511 * handles nested control method invocations without recursion.
513 ACPI_DEBUG_PRINT ((ACPI_DB_PARSE, "State=%p\n", WalkState));
515 Status = AE_OK;
516 while (WalkState)
518 if (ACPI_SUCCESS (Status))
521 * The ParseLoop executes AML until the method terminates
522 * or calls another method.
524 Status = AcpiPsParseLoop (WalkState);
527 ACPI_DEBUG_PRINT ((ACPI_DB_PARSE,
528 "Completed one call to walk loop, %s State=%p\n",
529 AcpiFormatException (Status), WalkState));
531 if (Status == AE_CTRL_TRANSFER)
534 * A method call was detected.
535 * Transfer control to the called control method
537 Status = AcpiDsCallControlMethod (Thread, WalkState, NULL);
538 if (ACPI_FAILURE (Status))
540 Status = AcpiDsMethodError (Status, WalkState);
544 * If the transfer to the new method method call worked
545 *, a new walk state was created -- get it
547 WalkState = AcpiDsGetCurrentWalkState (Thread);
548 continue;
550 else if (Status == AE_CTRL_TERMINATE)
552 Status = AE_OK;
554 else if ((Status != AE_OK) && (WalkState->MethodDesc))
556 /* Either the method parse or actual execution failed */
558 AcpiExExitInterpreter ();
559 ACPI_ERROR_METHOD ("Method parse/execution failed",
560 WalkState->MethodNode, NULL, Status);
561 AcpiExEnterInterpreter ();
563 /* Check for possible multi-thread reentrancy problem */
565 if ((Status == AE_ALREADY_EXISTS) &&
566 (!(WalkState->MethodDesc->Method.InfoFlags &
567 ACPI_METHOD_SERIALIZED)))
570 * Method is not serialized and tried to create an object
571 * twice. The probable cause is that the method cannot
572 * handle reentrancy. Mark as "pending serialized" now, and
573 * then mark "serialized" when the last thread exits.
575 WalkState->MethodDesc->Method.InfoFlags |=
576 ACPI_METHOD_SERIALIZED_PENDING;
580 /* We are done with this walk, move on to the parent if any */
582 WalkState = AcpiDsPopWalkState (Thread);
584 /* Reset the current scope to the beginning of scope stack */
586 AcpiDsScopeStackClear (WalkState);
589 * If we just returned from the execution of a control method or if we
590 * encountered an error during the method parse phase, there's lots of
591 * cleanup to do
593 if (((WalkState->ParseFlags & ACPI_PARSE_MODE_MASK) ==
594 ACPI_PARSE_EXECUTE &&
595 !(WalkState->ParseFlags & ACPI_PARSE_MODULE_LEVEL)) ||
596 (ACPI_FAILURE (Status)))
598 AcpiDsTerminateControlMethod (WalkState->MethodDesc, WalkState);
601 /* Delete this walk state and all linked control states */
603 AcpiPsCleanupScope (&WalkState->ParserState);
604 PreviousWalkState = WalkState;
606 ACPI_DEBUG_PRINT ((ACPI_DB_PARSE,
607 "ReturnValue=%p, ImplicitValue=%p State=%p\n",
608 WalkState->ReturnDesc, WalkState->ImplicitReturnObj, WalkState));
610 /* Check if we have restarted a preempted walk */
612 WalkState = AcpiDsGetCurrentWalkState (Thread);
613 if (WalkState)
615 if (ACPI_SUCCESS (Status))
618 * There is another walk state, restart it.
619 * If the method return value is not used by the parent,
620 * The object is deleted
622 if (!PreviousWalkState->ReturnDesc)
625 * In slack mode execution, if there is no return value
626 * we should implicitly return zero (0) as a default value.
628 if (AcpiGbl_EnableInterpreterSlack &&
629 !PreviousWalkState->ImplicitReturnObj)
631 PreviousWalkState->ImplicitReturnObj =
632 AcpiUtCreateIntegerObject ((UINT64) 0);
633 if (!PreviousWalkState->ImplicitReturnObj)
635 return_ACPI_STATUS (AE_NO_MEMORY);
639 /* Restart the calling control method */
641 Status = AcpiDsRestartControlMethod (WalkState,
642 PreviousWalkState->ImplicitReturnObj);
644 else
647 * We have a valid return value, delete any implicit
648 * return value.
650 AcpiDsClearImplicitReturn (PreviousWalkState);
652 Status = AcpiDsRestartControlMethod (WalkState,
653 PreviousWalkState->ReturnDesc);
655 if (ACPI_SUCCESS (Status))
657 WalkState->WalkType |= ACPI_WALK_METHOD_RESTART;
660 else
662 /* On error, delete any return object or implicit return */
664 AcpiUtRemoveReference (PreviousWalkState->ReturnDesc);
665 AcpiDsClearImplicitReturn (PreviousWalkState);
670 * Just completed a 1st-level method, save the final internal return
671 * value (if any)
673 else if (PreviousWalkState->CallerReturnDesc)
675 if (PreviousWalkState->ImplicitReturnObj)
677 *(PreviousWalkState->CallerReturnDesc) =
678 PreviousWalkState->ImplicitReturnObj;
680 else
682 /* NULL if no return value */
684 *(PreviousWalkState->CallerReturnDesc) =
685 PreviousWalkState->ReturnDesc;
688 else
690 if (PreviousWalkState->ReturnDesc)
692 /* Caller doesn't want it, must delete it */
694 AcpiUtRemoveReference (PreviousWalkState->ReturnDesc);
696 if (PreviousWalkState->ImplicitReturnObj)
698 /* Caller doesn't want it, must delete it */
700 AcpiUtRemoveReference (PreviousWalkState->ImplicitReturnObj);
704 AcpiDsDeleteWalkState (PreviousWalkState);
707 /* Normal exit */
709 AcpiExReleaseAllMutexes (Thread);
710 AcpiUtDeleteGenericState (ACPI_CAST_PTR (ACPI_GENERIC_STATE, Thread));
711 AcpiGbl_CurrentWalkList = PrevWalkList;
712 return_ACPI_STATUS (Status);