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)
77 ACPI_FUNCTION_TRACE_PTR (PsGetAmlOpcode, WalkState);
80 WalkState->AmlOffset = (UINT32) ACPI_PTR_DIFF (WalkState->ParserState.Aml,
81 WalkState->ParserState.AmlStart);
82 WalkState->Opcode = AcpiPsPeekOpcode (&(WalkState->ParserState));
85 * First cut to determine what we have found:
86 * 1) A valid AML opcode
88 * 3) An unknown/invalid opcode
90 WalkState->OpInfo = AcpiPsGetOpcodeInfo (WalkState->Opcode);
92 switch (WalkState->OpInfo->Class)
95 case AML_CLASS_PREFIX:
97 * Starts with a valid prefix or ASCII char, this is a name
98 * string. Convert the bare name string to a namepath.
100 WalkState->Opcode = AML_INT_NAMEPATH_OP;
101 WalkState->ArgTypes = ARGP_NAMESTRING;
104 case AML_CLASS_UNKNOWN:
106 /* The opcode is unrecognized. Complain and skip unknown opcodes */
108 if (WalkState->PassNumber == 2)
110 ACPI_ERROR ((AE_INFO,
111 "Unknown opcode 0x%.2X at table offset 0x%.4X, ignoring",
113 (UINT32) (WalkState->AmlOffset + sizeof (ACPI_TABLE_HEADER))));
115 ACPI_DUMP_BUFFER ((WalkState->ParserState.Aml - 16), 48);
117 #ifdef ACPI_ASL_COMPILER
119 * This is executed for the disassembler only. Output goes
120 * to the disassembled ASL output file.
123 "/*\nError: Unknown opcode 0x%.2X at table offset 0x%.4X, context:\n",
125 (UINT32) (WalkState->AmlOffset + sizeof (ACPI_TABLE_HEADER)));
127 /* Dump the context surrounding the invalid opcode */
129 AcpiUtDumpBuffer (((UINT8 *) WalkState->ParserState.Aml - 16),
131 (WalkState->AmlOffset + sizeof (ACPI_TABLE_HEADER) - 16));
132 AcpiOsPrintf (" */\n");
136 /* Increment past one-byte or two-byte opcode */
138 WalkState->ParserState.Aml++;
139 if (WalkState->Opcode > 0xFF) /* Can only happen if first byte is 0x5B */
141 WalkState->ParserState.Aml++;
144 return_ACPI_STATUS (AE_CTRL_PARSE_CONTINUE);
148 /* Found opcode info, this is a normal opcode */
150 WalkState->ParserState.Aml += AcpiPsGetOpcodeSize (WalkState->Opcode);
151 WalkState->ArgTypes = WalkState->OpInfo->ParseArgs;
155 return_ACPI_STATUS (AE_OK);
159 /*******************************************************************************
161 * FUNCTION: AcpiPsBuildNamedOp
163 * PARAMETERS: WalkState - Current state
164 * AmlOpStart - Begin of named Op in AML
165 * UnnamedOp - Early Op (not a named Op)
170 * DESCRIPTION: Parse a named Op
172 ******************************************************************************/
176 ACPI_WALK_STATE *WalkState,
178 ACPI_PARSE_OBJECT *UnnamedOp,
179 ACPI_PARSE_OBJECT **Op)
181 ACPI_STATUS Status = AE_OK;
182 ACPI_PARSE_OBJECT *Arg = NULL;
185 ACPI_FUNCTION_TRACE_PTR (PsBuildNamedOp, WalkState);
188 UnnamedOp->Common.Value.Arg = NULL;
189 UnnamedOp->Common.ArgListLength = 0;
190 UnnamedOp->Common.AmlOpcode = WalkState->Opcode;
193 * Get and append arguments until we find the node that contains
194 * the name (the type ARGP_NAME).
196 while (GET_CURRENT_ARG_TYPE (WalkState->ArgTypes) &&
197 (GET_CURRENT_ARG_TYPE (WalkState->ArgTypes) != ARGP_NAME))
199 Status = AcpiPsGetNextArg (WalkState, &(WalkState->ParserState),
200 GET_CURRENT_ARG_TYPE (WalkState->ArgTypes), &Arg);
201 if (ACPI_FAILURE (Status))
203 return_ACPI_STATUS (Status);
206 AcpiPsAppendArg (UnnamedOp, Arg);
207 INCREMENT_ARG_LIST (WalkState->ArgTypes);
211 * Make sure that we found a NAME and didn't run out of arguments
213 if (!GET_CURRENT_ARG_TYPE (WalkState->ArgTypes))
215 return_ACPI_STATUS (AE_AML_NO_OPERAND);
218 /* We know that this arg is a name, move to next arg */
220 INCREMENT_ARG_LIST (WalkState->ArgTypes);
223 * Find the object. This will either insert the object into
224 * the namespace or simply look it up
226 WalkState->Op = NULL;
228 Status = WalkState->DescendingCallback (WalkState, Op);
229 if (ACPI_FAILURE (Status))
231 if (Status != AE_CTRL_TERMINATE)
233 ACPI_EXCEPTION ((AE_INFO, Status, "During name lookup/catalog"));
235 return_ACPI_STATUS (Status);
240 return_ACPI_STATUS (AE_CTRL_PARSE_CONTINUE);
243 Status = AcpiPsNextParseState (WalkState, *Op, Status);
244 if (ACPI_FAILURE (Status))
246 if (Status == AE_CTRL_PENDING)
248 Status = AE_CTRL_PARSE_PENDING;
250 return_ACPI_STATUS (Status);
253 AcpiPsAppendArg (*Op, UnnamedOp->Common.Value.Arg);
255 if ((*Op)->Common.AmlOpcode == AML_REGION_OP ||
256 (*Op)->Common.AmlOpcode == AML_DATA_REGION_OP)
259 * Defer final parsing of an OperationRegion body, because we don't
260 * have enough info in the first pass to parse it correctly (i.e.,
261 * there may be method calls within the TermArg elements of the body.)
263 * However, we must continue parsing because the opregion is not a
264 * standalone package -- we don't know where the end is at this point.
266 * (Length is unknown until parse of the body complete)
268 (*Op)->Named.Data = AmlOpStart;
269 (*Op)->Named.Length = 0;
272 return_ACPI_STATUS (AE_OK);
276 /*******************************************************************************
278 * FUNCTION: AcpiPsCreateOp
280 * PARAMETERS: WalkState - Current state
281 * AmlOpStart - Op start in AML
282 * NewOp - Returned Op
286 * DESCRIPTION: Get Op from AML
288 ******************************************************************************/
292 ACPI_WALK_STATE *WalkState,
294 ACPI_PARSE_OBJECT **NewOp)
296 ACPI_STATUS Status = AE_OK;
297 ACPI_PARSE_OBJECT *Op;
298 ACPI_PARSE_OBJECT *NamedOp = NULL;
299 ACPI_PARSE_OBJECT *ParentScope;
301 const ACPI_OPCODE_INFO *OpInfo;
304 ACPI_FUNCTION_TRACE_PTR (PsCreateOp, WalkState);
307 Status = AcpiPsGetAmlOpcode (WalkState);
308 if (Status == AE_CTRL_PARSE_CONTINUE)
310 return_ACPI_STATUS (AE_CTRL_PARSE_CONTINUE);
313 /* Create Op structure and append to parent's argument list */
315 WalkState->OpInfo = AcpiPsGetOpcodeInfo (WalkState->Opcode);
316 Op = AcpiPsAllocOp (WalkState->Opcode);
319 return_ACPI_STATUS (AE_NO_MEMORY);
322 if (WalkState->OpInfo->Flags & AML_NAMED)
324 Status = AcpiPsBuildNamedOp (WalkState, AmlOpStart, Op, &NamedOp);
326 if (ACPI_FAILURE (Status))
328 return_ACPI_STATUS (Status);
332 return_ACPI_STATUS (AE_OK);
335 /* Not a named opcode, just allocate Op and append to parent */
337 if (WalkState->OpInfo->Flags & AML_CREATE)
340 * Backup to beginning of CreateXXXfield declaration
341 * BodyLength is unknown until we parse the body
343 Op->Named.Data = AmlOpStart;
344 Op->Named.Length = 0;
347 if (WalkState->Opcode == AML_BANK_FIELD_OP)
350 * Backup to beginning of BankField declaration
351 * BodyLength is unknown until we parse the body
353 Op->Named.Data = AmlOpStart;
354 Op->Named.Length = 0;
357 ParentScope = AcpiPsGetParentScope (&(WalkState->ParserState));
358 AcpiPsAppendArg (ParentScope, Op);
362 OpInfo = AcpiPsGetOpcodeInfo (ParentScope->Common.AmlOpcode);
363 if (OpInfo->Flags & AML_HAS_TARGET)
365 ArgumentCount = AcpiPsGetArgumentCount (OpInfo->Type);
366 if (ParentScope->Common.ArgListLength > ArgumentCount)
368 Op->Common.Flags |= ACPI_PARSEOP_TARGET;
371 else if (ParentScope->Common.AmlOpcode == AML_INCREMENT_OP)
373 Op->Common.Flags |= ACPI_PARSEOP_TARGET;
377 if (WalkState->DescendingCallback != NULL)
380 * Find the object. This will either insert the object into
381 * the namespace or simply look it up
383 WalkState->Op = *NewOp = Op;
385 Status = WalkState->DescendingCallback (WalkState, &Op);
386 Status = AcpiPsNextParseState (WalkState, Op, Status);
387 if (Status == AE_CTRL_PENDING)
389 Status = AE_CTRL_PARSE_PENDING;
393 return_ACPI_STATUS (Status);
397 /*******************************************************************************
399 * FUNCTION: AcpiPsCompleteOp
401 * PARAMETERS: WalkState - Current state
403 * Status - Parse status before complete Op
407 * DESCRIPTION: Complete Op
409 ******************************************************************************/
413 ACPI_WALK_STATE *WalkState,
414 ACPI_PARSE_OBJECT **Op,
420 ACPI_FUNCTION_TRACE_PTR (PsCompleteOp, WalkState);
424 * Finished one argument of the containing scope
426 WalkState->ParserState.Scope->ParseScope.ArgCount--;
428 /* Close this Op (will result in parse subtree deletion) */
430 Status2 = AcpiPsCompleteThisOp (WalkState, *Op);
431 if (ACPI_FAILURE (Status2))
433 return_ACPI_STATUS (Status2);
444 case AE_CTRL_TRANSFER:
446 /* We are about to transfer to a called method */
448 WalkState->PrevOp = NULL;
449 WalkState->PrevArgTypes = WalkState->ArgTypes;
450 return_ACPI_STATUS (Status);
454 AcpiPsPopScope (&(WalkState->ParserState), Op,
455 &WalkState->ArgTypes, &WalkState->ArgCount);
460 WalkState->OpInfo = AcpiPsGetOpcodeInfo ((*Op)->Common.AmlOpcode);
461 WalkState->Opcode = (*Op)->Common.AmlOpcode;
463 Status = WalkState->AscendingCallback (WalkState);
464 Status = AcpiPsNextParseState (WalkState, *Op, Status);
466 Status2 = AcpiPsCompleteThisOp (WalkState, *Op);
467 if (ACPI_FAILURE (Status2))
469 return_ACPI_STATUS (Status2);
477 case AE_CTRL_CONTINUE:
479 /* Pop off scopes until we find the While */
481 while (!(*Op) || ((*Op)->Common.AmlOpcode != AML_WHILE_OP))
483 AcpiPsPopScope (&(WalkState->ParserState), Op,
484 &WalkState->ArgTypes, &WalkState->ArgCount);
487 /* Close this iteration of the While loop */
490 WalkState->OpInfo = AcpiPsGetOpcodeInfo ((*Op)->Common.AmlOpcode);
491 WalkState->Opcode = (*Op)->Common.AmlOpcode;
493 Status = WalkState->AscendingCallback (WalkState);
494 Status = AcpiPsNextParseState (WalkState, *Op, Status);
496 Status2 = AcpiPsCompleteThisOp (WalkState, *Op);
497 if (ACPI_FAILURE (Status2))
499 return_ACPI_STATUS (Status2);
505 case AE_CTRL_TERMINATE:
512 Status2 = AcpiPsCompleteThisOp (WalkState, *Op);
513 if (ACPI_FAILURE (Status2))
515 return_ACPI_STATUS (Status2);
518 AcpiUtDeleteGenericState (
519 AcpiUtPopGenericState (&WalkState->ControlState));
522 AcpiPsPopScope (&(WalkState->ParserState), Op,
523 &WalkState->ArgTypes, &WalkState->ArgCount);
527 return_ACPI_STATUS (AE_OK);
529 default: /* All other non-AE_OK status */
535 Status2 = AcpiPsCompleteThisOp (WalkState, *Op);
536 if (ACPI_FAILURE (Status2))
538 return_ACPI_STATUS (Status2);
542 AcpiPsPopScope (&(WalkState->ParserState), Op,
543 &WalkState->ArgTypes, &WalkState->ArgCount);
550 * TBD: Cleanup parse ops on error
554 AcpiPsPopScope (ParserState, Op,
555 &WalkState->ArgTypes, &WalkState->ArgCount);
558 WalkState->PrevOp = NULL;
559 WalkState->PrevArgTypes = WalkState->ArgTypes;
560 return_ACPI_STATUS (Status);
563 /* This scope complete? */
565 if (AcpiPsHasCompletedScope (&(WalkState->ParserState)))
567 AcpiPsPopScope (&(WalkState->ParserState), Op,
568 &WalkState->ArgTypes, &WalkState->ArgCount);
569 ACPI_DEBUG_PRINT ((ACPI_DB_PARSE, "Popped scope, Op=%p\n", *Op));
576 return_ACPI_STATUS (AE_OK);
580 /*******************************************************************************
582 * FUNCTION: AcpiPsCompleteFinalOp
584 * PARAMETERS: WalkState - Current state
586 * Status - Current parse status before complete last
591 * DESCRIPTION: Complete last Op.
593 ******************************************************************************/
596 AcpiPsCompleteFinalOp (
597 ACPI_WALK_STATE *WalkState,
598 ACPI_PARSE_OBJECT *Op,
604 ACPI_FUNCTION_TRACE_PTR (PsCompleteFinalOp, WalkState);
608 * Complete the last Op (if not completed), and clear the scope stack.
609 * It is easily possible to end an AML "package" with an unbounded number
610 * of open scopes (such as when several ASL blocks are closed with
611 * sequential closing braces). We want to terminate each one cleanly.
613 ACPI_DEBUG_PRINT ((ACPI_DB_PARSE, "AML package complete at Op %p\n", Op));
618 if (WalkState->AscendingCallback != NULL)
621 WalkState->OpInfo = AcpiPsGetOpcodeInfo (Op->Common.AmlOpcode);
622 WalkState->Opcode = Op->Common.AmlOpcode;
624 Status = WalkState->AscendingCallback (WalkState);
625 Status = AcpiPsNextParseState (WalkState, Op, Status);
626 if (Status == AE_CTRL_PENDING)
628 Status = AcpiPsCompleteOp (WalkState, &Op, AE_OK);
629 if (ACPI_FAILURE (Status))
631 return_ACPI_STATUS (Status);
635 if (Status == AE_CTRL_TERMINATE)
644 Status2 = AcpiPsCompleteThisOp (WalkState, Op);
645 if (ACPI_FAILURE (Status2))
647 return_ACPI_STATUS (Status2);
651 AcpiPsPopScope (&(WalkState->ParserState), &Op,
652 &WalkState->ArgTypes, &WalkState->ArgCount);
656 return_ACPI_STATUS (Status);
659 else if (ACPI_FAILURE (Status))
661 /* First error is most important */
663 (void) AcpiPsCompleteThisOp (WalkState, Op);
664 return_ACPI_STATUS (Status);
668 Status2 = AcpiPsCompleteThisOp (WalkState, Op);
669 if (ACPI_FAILURE (Status2))
671 return_ACPI_STATUS (Status2);
675 AcpiPsPopScope (&(WalkState->ParserState), &Op, &WalkState->ArgTypes,
676 &WalkState->ArgCount);
680 return_ACPI_STATUS (Status);