1 /******************************************************************************
3 * Module Name: psobject - Support for parse objects
5 *****************************************************************************/
8 * Copyright (C) 2000 - 2015, 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.
44 #include <contrib/dev/acpica/include/acpi.h>
45 #include <contrib/dev/acpica/include/accommon.h>
46 #include <contrib/dev/acpica/include/acparser.h>
47 #include <contrib/dev/acpica/include/amlcode.h>
49 #define _COMPONENT ACPI_PARSER
50 ACPI_MODULE_NAME ("psobject")
53 /* Local prototypes */
57 ACPI_WALK_STATE *WalkState);
60 /*******************************************************************************
62 * FUNCTION: AcpiPsGetAmlOpcode
64 * PARAMETERS: WalkState - Current state
68 * DESCRIPTION: Extract the next AML opcode from the input stream.
70 ******************************************************************************/
74 ACPI_WALK_STATE *WalkState)
79 ACPI_FUNCTION_TRACE_PTR (PsGetAmlOpcode, WalkState);
82 WalkState->Aml = WalkState->ParserState.Aml;
83 WalkState->Opcode = AcpiPsPeekOpcode (&(WalkState->ParserState));
86 * First cut to determine what we have found:
87 * 1) A valid AML opcode
89 * 3) An unknown/invalid opcode
91 WalkState->OpInfo = AcpiPsGetOpcodeInfo (WalkState->Opcode);
93 switch (WalkState->OpInfo->Class)
96 case AML_CLASS_PREFIX:
98 * Starts with a valid prefix or ASCII char, this is a name
99 * string. Convert the bare name string to a namepath.
101 WalkState->Opcode = AML_INT_NAMEPATH_OP;
102 WalkState->ArgTypes = ARGP_NAMESTRING;
105 case AML_CLASS_UNKNOWN:
107 /* The opcode is unrecognized. Complain and skip unknown opcodes */
109 if (WalkState->PassNumber == 2)
111 AmlOffset = (UINT32) ACPI_PTR_DIFF (WalkState->Aml,
112 WalkState->ParserState.AmlStart);
114 ACPI_ERROR ((AE_INFO,
115 "Unknown opcode 0x%.2X at table offset 0x%.4X, ignoring",
117 (UINT32) (AmlOffset + sizeof (ACPI_TABLE_HEADER))));
119 ACPI_DUMP_BUFFER ((WalkState->ParserState.Aml - 16), 48);
121 #ifdef ACPI_ASL_COMPILER
123 * This is executed for the disassembler only. Output goes
124 * to the disassembled ASL output file.
127 "/*\nError: Unknown opcode 0x%.2X at table offset 0x%.4X, context:\n",
129 (UINT32) (AmlOffset + sizeof (ACPI_TABLE_HEADER)));
131 /* Dump the context surrounding the invalid opcode */
133 AcpiUtDumpBuffer (((UINT8 *) WalkState->ParserState.Aml - 16),
135 (AmlOffset + sizeof (ACPI_TABLE_HEADER) - 16));
136 AcpiOsPrintf (" */\n");
140 /* Increment past one-byte or two-byte opcode */
142 WalkState->ParserState.Aml++;
143 if (WalkState->Opcode > 0xFF) /* Can only happen if first byte is 0x5B */
145 WalkState->ParserState.Aml++;
148 return_ACPI_STATUS (AE_CTRL_PARSE_CONTINUE);
152 /* Found opcode info, this is a normal opcode */
154 WalkState->ParserState.Aml += AcpiPsGetOpcodeSize (WalkState->Opcode);
155 WalkState->ArgTypes = WalkState->OpInfo->ParseArgs;
159 return_ACPI_STATUS (AE_OK);
163 /*******************************************************************************
165 * FUNCTION: AcpiPsBuildNamedOp
167 * PARAMETERS: WalkState - Current state
168 * AmlOpStart - Begin of named Op in AML
169 * UnnamedOp - Early Op (not a named Op)
174 * DESCRIPTION: Parse a named Op
176 ******************************************************************************/
180 ACPI_WALK_STATE *WalkState,
182 ACPI_PARSE_OBJECT *UnnamedOp,
183 ACPI_PARSE_OBJECT **Op)
185 ACPI_STATUS Status = AE_OK;
186 ACPI_PARSE_OBJECT *Arg = NULL;
189 ACPI_FUNCTION_TRACE_PTR (PsBuildNamedOp, WalkState);
192 UnnamedOp->Common.Value.Arg = NULL;
193 UnnamedOp->Common.ArgListLength = 0;
194 UnnamedOp->Common.AmlOpcode = WalkState->Opcode;
197 * Get and append arguments until we find the node that contains
198 * the name (the type ARGP_NAME).
200 while (GET_CURRENT_ARG_TYPE (WalkState->ArgTypes) &&
201 (GET_CURRENT_ARG_TYPE (WalkState->ArgTypes) != ARGP_NAME))
203 Status = AcpiPsGetNextArg (WalkState, &(WalkState->ParserState),
204 GET_CURRENT_ARG_TYPE (WalkState->ArgTypes), &Arg);
205 if (ACPI_FAILURE (Status))
207 return_ACPI_STATUS (Status);
210 AcpiPsAppendArg (UnnamedOp, Arg);
211 INCREMENT_ARG_LIST (WalkState->ArgTypes);
215 * Make sure that we found a NAME and didn't run out of arguments
217 if (!GET_CURRENT_ARG_TYPE (WalkState->ArgTypes))
219 return_ACPI_STATUS (AE_AML_NO_OPERAND);
222 /* We know that this arg is a name, move to next arg */
224 INCREMENT_ARG_LIST (WalkState->ArgTypes);
227 * Find the object. This will either insert the object into
228 * the namespace or simply look it up
230 WalkState->Op = NULL;
232 Status = WalkState->DescendingCallback (WalkState, Op);
233 if (ACPI_FAILURE (Status))
235 if (Status != AE_CTRL_TERMINATE)
237 ACPI_EXCEPTION ((AE_INFO, Status, "During name lookup/catalog"));
239 return_ACPI_STATUS (Status);
244 return_ACPI_STATUS (AE_CTRL_PARSE_CONTINUE);
247 Status = AcpiPsNextParseState (WalkState, *Op, Status);
248 if (ACPI_FAILURE (Status))
250 if (Status == AE_CTRL_PENDING)
252 Status = AE_CTRL_PARSE_PENDING;
254 return_ACPI_STATUS (Status);
257 AcpiPsAppendArg (*Op, UnnamedOp->Common.Value.Arg);
259 if ((*Op)->Common.AmlOpcode == AML_REGION_OP ||
260 (*Op)->Common.AmlOpcode == AML_DATA_REGION_OP)
263 * Defer final parsing of an OperationRegion body, because we don't
264 * have enough info in the first pass to parse it correctly (i.e.,
265 * there may be method calls within the TermArg elements of the body.)
267 * However, we must continue parsing because the opregion is not a
268 * standalone package -- we don't know where the end is at this point.
270 * (Length is unknown until parse of the body complete)
272 (*Op)->Named.Data = AmlOpStart;
273 (*Op)->Named.Length = 0;
276 return_ACPI_STATUS (AE_OK);
280 /*******************************************************************************
282 * FUNCTION: AcpiPsCreateOp
284 * PARAMETERS: WalkState - Current state
285 * AmlOpStart - Op start in AML
286 * NewOp - Returned Op
290 * DESCRIPTION: Get Op from AML
292 ******************************************************************************/
296 ACPI_WALK_STATE *WalkState,
298 ACPI_PARSE_OBJECT **NewOp)
300 ACPI_STATUS Status = AE_OK;
301 ACPI_PARSE_OBJECT *Op;
302 ACPI_PARSE_OBJECT *NamedOp = NULL;
303 ACPI_PARSE_OBJECT *ParentScope;
305 const ACPI_OPCODE_INFO *OpInfo;
308 ACPI_FUNCTION_TRACE_PTR (PsCreateOp, WalkState);
311 Status = AcpiPsGetAmlOpcode (WalkState);
312 if (Status == AE_CTRL_PARSE_CONTINUE)
314 return_ACPI_STATUS (AE_CTRL_PARSE_CONTINUE);
317 /* Create Op structure and append to parent's argument list */
319 WalkState->OpInfo = AcpiPsGetOpcodeInfo (WalkState->Opcode);
320 Op = AcpiPsAllocOp (WalkState->Opcode, AmlOpStart);
323 return_ACPI_STATUS (AE_NO_MEMORY);
326 if (WalkState->OpInfo->Flags & AML_NAMED)
328 Status = AcpiPsBuildNamedOp (WalkState, AmlOpStart, Op, &NamedOp);
330 if (ACPI_FAILURE (Status))
332 return_ACPI_STATUS (Status);
336 return_ACPI_STATUS (AE_OK);
339 /* Not a named opcode, just allocate Op and append to parent */
341 if (WalkState->OpInfo->Flags & AML_CREATE)
344 * Backup to beginning of CreateXXXfield declaration
345 * BodyLength is unknown until we parse the body
347 Op->Named.Data = AmlOpStart;
348 Op->Named.Length = 0;
351 if (WalkState->Opcode == AML_BANK_FIELD_OP)
354 * Backup to beginning of BankField declaration
355 * BodyLength is unknown until we parse the body
357 Op->Named.Data = AmlOpStart;
358 Op->Named.Length = 0;
361 ParentScope = AcpiPsGetParentScope (&(WalkState->ParserState));
362 AcpiPsAppendArg (ParentScope, Op);
366 OpInfo = AcpiPsGetOpcodeInfo (ParentScope->Common.AmlOpcode);
367 if (OpInfo->Flags & AML_HAS_TARGET)
369 ArgumentCount = AcpiPsGetArgumentCount (OpInfo->Type);
370 if (ParentScope->Common.ArgListLength > ArgumentCount)
372 Op->Common.Flags |= ACPI_PARSEOP_TARGET;
375 else if (ParentScope->Common.AmlOpcode == AML_INCREMENT_OP)
377 Op->Common.Flags |= ACPI_PARSEOP_TARGET;
381 if (WalkState->DescendingCallback != NULL)
384 * Find the object. This will either insert the object into
385 * the namespace or simply look it up
387 WalkState->Op = *NewOp = Op;
389 Status = WalkState->DescendingCallback (WalkState, &Op);
390 Status = AcpiPsNextParseState (WalkState, Op, Status);
391 if (Status == AE_CTRL_PENDING)
393 Status = AE_CTRL_PARSE_PENDING;
397 return_ACPI_STATUS (Status);
401 /*******************************************************************************
403 * FUNCTION: AcpiPsCompleteOp
405 * PARAMETERS: WalkState - Current state
407 * Status - Parse status before complete Op
411 * DESCRIPTION: Complete Op
413 ******************************************************************************/
417 ACPI_WALK_STATE *WalkState,
418 ACPI_PARSE_OBJECT **Op,
424 ACPI_FUNCTION_TRACE_PTR (PsCompleteOp, WalkState);
428 * Finished one argument of the containing scope
430 WalkState->ParserState.Scope->ParseScope.ArgCount--;
432 /* Close this Op (will result in parse subtree deletion) */
434 Status2 = AcpiPsCompleteThisOp (WalkState, *Op);
435 if (ACPI_FAILURE (Status2))
437 return_ACPI_STATUS (Status2);
448 case AE_CTRL_TRANSFER:
450 /* We are about to transfer to a called method */
452 WalkState->PrevOp = NULL;
453 WalkState->PrevArgTypes = WalkState->ArgTypes;
454 return_ACPI_STATUS (Status);
458 AcpiPsPopScope (&(WalkState->ParserState), Op,
459 &WalkState->ArgTypes, &WalkState->ArgCount);
464 WalkState->OpInfo = AcpiPsGetOpcodeInfo ((*Op)->Common.AmlOpcode);
465 WalkState->Opcode = (*Op)->Common.AmlOpcode;
467 Status = WalkState->AscendingCallback (WalkState);
468 Status = AcpiPsNextParseState (WalkState, *Op, Status);
470 Status2 = AcpiPsCompleteThisOp (WalkState, *Op);
471 if (ACPI_FAILURE (Status2))
473 return_ACPI_STATUS (Status2);
481 case AE_CTRL_CONTINUE:
483 /* Pop off scopes until we find the While */
485 while (!(*Op) || ((*Op)->Common.AmlOpcode != AML_WHILE_OP))
487 AcpiPsPopScope (&(WalkState->ParserState), Op,
488 &WalkState->ArgTypes, &WalkState->ArgCount);
491 /* Close this iteration of the While loop */
494 WalkState->OpInfo = AcpiPsGetOpcodeInfo ((*Op)->Common.AmlOpcode);
495 WalkState->Opcode = (*Op)->Common.AmlOpcode;
497 Status = WalkState->AscendingCallback (WalkState);
498 Status = AcpiPsNextParseState (WalkState, *Op, Status);
500 Status2 = AcpiPsCompleteThisOp (WalkState, *Op);
501 if (ACPI_FAILURE (Status2))
503 return_ACPI_STATUS (Status2);
509 case AE_CTRL_TERMINATE:
516 Status2 = AcpiPsCompleteThisOp (WalkState, *Op);
517 if (ACPI_FAILURE (Status2))
519 return_ACPI_STATUS (Status2);
522 AcpiUtDeleteGenericState (
523 AcpiUtPopGenericState (&WalkState->ControlState));
526 AcpiPsPopScope (&(WalkState->ParserState), Op,
527 &WalkState->ArgTypes, &WalkState->ArgCount);
531 return_ACPI_STATUS (AE_OK);
533 default: /* All other non-AE_OK status */
539 Status2 = AcpiPsCompleteThisOp (WalkState, *Op);
540 if (ACPI_FAILURE (Status2))
542 return_ACPI_STATUS (Status2);
546 AcpiPsPopScope (&(WalkState->ParserState), Op,
547 &WalkState->ArgTypes, &WalkState->ArgCount);
554 * TBD: Cleanup parse ops on error
558 AcpiPsPopScope (ParserState, Op,
559 &WalkState->ArgTypes, &WalkState->ArgCount);
562 WalkState->PrevOp = NULL;
563 WalkState->PrevArgTypes = WalkState->ArgTypes;
564 return_ACPI_STATUS (Status);
567 /* This scope complete? */
569 if (AcpiPsHasCompletedScope (&(WalkState->ParserState)))
571 AcpiPsPopScope (&(WalkState->ParserState), Op,
572 &WalkState->ArgTypes, &WalkState->ArgCount);
573 ACPI_DEBUG_PRINT ((ACPI_DB_PARSE, "Popped scope, Op=%p\n", *Op));
580 return_ACPI_STATUS (AE_OK);
584 /*******************************************************************************
586 * FUNCTION: AcpiPsCompleteFinalOp
588 * PARAMETERS: WalkState - Current state
590 * Status - Current parse status before complete last
595 * DESCRIPTION: Complete last Op.
597 ******************************************************************************/
600 AcpiPsCompleteFinalOp (
601 ACPI_WALK_STATE *WalkState,
602 ACPI_PARSE_OBJECT *Op,
608 ACPI_FUNCTION_TRACE_PTR (PsCompleteFinalOp, WalkState);
612 * Complete the last Op (if not completed), and clear the scope stack.
613 * It is easily possible to end an AML "package" with an unbounded number
614 * of open scopes (such as when several ASL blocks are closed with
615 * sequential closing braces). We want to terminate each one cleanly.
617 ACPI_DEBUG_PRINT ((ACPI_DB_PARSE, "AML package complete at Op %p\n", Op));
622 if (WalkState->AscendingCallback != NULL)
625 WalkState->OpInfo = AcpiPsGetOpcodeInfo (Op->Common.AmlOpcode);
626 WalkState->Opcode = Op->Common.AmlOpcode;
628 Status = WalkState->AscendingCallback (WalkState);
629 Status = AcpiPsNextParseState (WalkState, Op, Status);
630 if (Status == AE_CTRL_PENDING)
632 Status = AcpiPsCompleteOp (WalkState, &Op, AE_OK);
633 if (ACPI_FAILURE (Status))
635 return_ACPI_STATUS (Status);
639 if (Status == AE_CTRL_TERMINATE)
648 Status2 = AcpiPsCompleteThisOp (WalkState, Op);
649 if (ACPI_FAILURE (Status2))
651 return_ACPI_STATUS (Status2);
655 AcpiPsPopScope (&(WalkState->ParserState), &Op,
656 &WalkState->ArgTypes, &WalkState->ArgCount);
660 return_ACPI_STATUS (Status);
663 else if (ACPI_FAILURE (Status))
665 /* First error is most important */
667 (void) AcpiPsCompleteThisOp (WalkState, Op);
668 return_ACPI_STATUS (Status);
672 Status2 = AcpiPsCompleteThisOp (WalkState, Op);
673 if (ACPI_FAILURE (Status2))
675 return_ACPI_STATUS (Status2);
679 AcpiPsPopScope (&(WalkState->ParserState), &Op, &WalkState->ArgTypes,
680 &WalkState->ArgCount);
684 return_ACPI_STATUS (Status);