1 /******************************************************************************
3 * Module Name: psobject - Support for parse objects
5 *****************************************************************************/
8 * Copyright (C) 2000 - 2014, 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.
50 #define _COMPONENT ACPI_PARSER
51 ACPI_MODULE_NAME ("psobject")
54 /* Local prototypes */
58 ACPI_WALK_STATE *WalkState);
61 /*******************************************************************************
63 * FUNCTION: AcpiPsGetAmlOpcode
65 * PARAMETERS: WalkState - Current state
69 * DESCRIPTION: Extract the next AML opcode from the input stream.
71 ******************************************************************************/
75 ACPI_WALK_STATE *WalkState)
78 ACPI_FUNCTION_TRACE_PTR (PsGetAmlOpcode, WalkState);
81 WalkState->AmlOffset = (UINT32) ACPI_PTR_DIFF (WalkState->ParserState.Aml,
82 WalkState->ParserState.AmlStart);
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 ACPI_ERROR ((AE_INFO,
112 "Unknown opcode 0x%.2X at table offset 0x%.4X, ignoring",
114 (UINT32) (WalkState->AmlOffset + sizeof (ACPI_TABLE_HEADER))));
116 ACPI_DUMP_BUFFER ((WalkState->ParserState.Aml - 16), 48);
118 #ifdef ACPI_ASL_COMPILER
120 * This is executed for the disassembler only. Output goes
121 * to the disassembled ASL output file.
124 "/*\nError: Unknown opcode 0x%.2X at table offset 0x%.4X, context:\n",
126 (UINT32) (WalkState->AmlOffset + sizeof (ACPI_TABLE_HEADER)));
128 /* Dump the context surrounding the invalid opcode */
130 AcpiUtDumpBuffer (((UINT8 *) WalkState->ParserState.Aml - 16),
132 (WalkState->AmlOffset + sizeof (ACPI_TABLE_HEADER) - 16));
133 AcpiOsPrintf (" */\n");
137 /* Increment past one-byte or two-byte opcode */
139 WalkState->ParserState.Aml++;
140 if (WalkState->Opcode > 0xFF) /* Can only happen if first byte is 0x5B */
142 WalkState->ParserState.Aml++;
145 return_ACPI_STATUS (AE_CTRL_PARSE_CONTINUE);
149 /* Found opcode info, this is a normal opcode */
151 WalkState->ParserState.Aml += AcpiPsGetOpcodeSize (WalkState->Opcode);
152 WalkState->ArgTypes = WalkState->OpInfo->ParseArgs;
156 return_ACPI_STATUS (AE_OK);
160 /*******************************************************************************
162 * FUNCTION: AcpiPsBuildNamedOp
164 * PARAMETERS: WalkState - Current state
165 * AmlOpStart - Begin of named Op in AML
166 * UnnamedOp - Early Op (not a named Op)
171 * DESCRIPTION: Parse a named Op
173 ******************************************************************************/
177 ACPI_WALK_STATE *WalkState,
179 ACPI_PARSE_OBJECT *UnnamedOp,
180 ACPI_PARSE_OBJECT **Op)
182 ACPI_STATUS Status = AE_OK;
183 ACPI_PARSE_OBJECT *Arg = NULL;
186 ACPI_FUNCTION_TRACE_PTR (PsBuildNamedOp, WalkState);
189 UnnamedOp->Common.Value.Arg = NULL;
190 UnnamedOp->Common.ArgListLength = 0;
191 UnnamedOp->Common.AmlOpcode = WalkState->Opcode;
194 * Get and append arguments until we find the node that contains
195 * the name (the type ARGP_NAME).
197 while (GET_CURRENT_ARG_TYPE (WalkState->ArgTypes) &&
198 (GET_CURRENT_ARG_TYPE (WalkState->ArgTypes) != ARGP_NAME))
200 Status = AcpiPsGetNextArg (WalkState, &(WalkState->ParserState),
201 GET_CURRENT_ARG_TYPE (WalkState->ArgTypes), &Arg);
202 if (ACPI_FAILURE (Status))
204 return_ACPI_STATUS (Status);
207 AcpiPsAppendArg (UnnamedOp, Arg);
208 INCREMENT_ARG_LIST (WalkState->ArgTypes);
212 * Make sure that we found a NAME and didn't run out of arguments
214 if (!GET_CURRENT_ARG_TYPE (WalkState->ArgTypes))
216 return_ACPI_STATUS (AE_AML_NO_OPERAND);
219 /* We know that this arg is a name, move to next arg */
221 INCREMENT_ARG_LIST (WalkState->ArgTypes);
224 * Find the object. This will either insert the object into
225 * the namespace or simply look it up
227 WalkState->Op = NULL;
229 Status = WalkState->DescendingCallback (WalkState, Op);
230 if (ACPI_FAILURE (Status))
232 if (Status != AE_CTRL_TERMINATE)
234 ACPI_EXCEPTION ((AE_INFO, Status, "During name lookup/catalog"));
236 return_ACPI_STATUS (Status);
241 return_ACPI_STATUS (AE_CTRL_PARSE_CONTINUE);
244 Status = AcpiPsNextParseState (WalkState, *Op, Status);
245 if (ACPI_FAILURE (Status))
247 if (Status == AE_CTRL_PENDING)
249 Status = AE_CTRL_PARSE_PENDING;
251 return_ACPI_STATUS (Status);
254 AcpiPsAppendArg (*Op, UnnamedOp->Common.Value.Arg);
256 if ((*Op)->Common.AmlOpcode == AML_REGION_OP ||
257 (*Op)->Common.AmlOpcode == AML_DATA_REGION_OP)
260 * Defer final parsing of an OperationRegion body, because we don't
261 * have enough info in the first pass to parse it correctly (i.e.,
262 * there may be method calls within the TermArg elements of the body.)
264 * However, we must continue parsing because the opregion is not a
265 * standalone package -- we don't know where the end is at this point.
267 * (Length is unknown until parse of the body complete)
269 (*Op)->Named.Data = AmlOpStart;
270 (*Op)->Named.Length = 0;
273 return_ACPI_STATUS (AE_OK);
277 /*******************************************************************************
279 * FUNCTION: AcpiPsCreateOp
281 * PARAMETERS: WalkState - Current state
282 * AmlOpStart - Op start in AML
283 * NewOp - Returned Op
287 * DESCRIPTION: Get Op from AML
289 ******************************************************************************/
293 ACPI_WALK_STATE *WalkState,
295 ACPI_PARSE_OBJECT **NewOp)
297 ACPI_STATUS Status = AE_OK;
298 ACPI_PARSE_OBJECT *Op;
299 ACPI_PARSE_OBJECT *NamedOp = NULL;
300 ACPI_PARSE_OBJECT *ParentScope;
302 const ACPI_OPCODE_INFO *OpInfo;
305 ACPI_FUNCTION_TRACE_PTR (PsCreateOp, WalkState);
308 Status = AcpiPsGetAmlOpcode (WalkState);
309 if (Status == AE_CTRL_PARSE_CONTINUE)
311 return_ACPI_STATUS (AE_CTRL_PARSE_CONTINUE);
314 /* Create Op structure and append to parent's argument list */
316 WalkState->OpInfo = AcpiPsGetOpcodeInfo (WalkState->Opcode);
317 Op = AcpiPsAllocOp (WalkState->Opcode);
320 return_ACPI_STATUS (AE_NO_MEMORY);
323 if (WalkState->OpInfo->Flags & AML_NAMED)
325 Status = AcpiPsBuildNamedOp (WalkState, AmlOpStart, Op, &NamedOp);
327 if (ACPI_FAILURE (Status))
329 return_ACPI_STATUS (Status);
333 return_ACPI_STATUS (AE_OK);
336 /* Not a named opcode, just allocate Op and append to parent */
338 if (WalkState->OpInfo->Flags & AML_CREATE)
341 * Backup to beginning of CreateXXXfield declaration
342 * BodyLength is unknown until we parse the body
344 Op->Named.Data = AmlOpStart;
345 Op->Named.Length = 0;
348 if (WalkState->Opcode == AML_BANK_FIELD_OP)
351 * Backup to beginning of BankField declaration
352 * BodyLength is unknown until we parse the body
354 Op->Named.Data = AmlOpStart;
355 Op->Named.Length = 0;
358 ParentScope = AcpiPsGetParentScope (&(WalkState->ParserState));
359 AcpiPsAppendArg (ParentScope, Op);
363 OpInfo = AcpiPsGetOpcodeInfo (ParentScope->Common.AmlOpcode);
364 if (OpInfo->Flags & AML_HAS_TARGET)
366 ArgumentCount = AcpiPsGetArgumentCount (OpInfo->Type);
367 if (ParentScope->Common.ArgListLength > ArgumentCount)
369 Op->Common.Flags |= ACPI_PARSEOP_TARGET;
372 else if (ParentScope->Common.AmlOpcode == AML_INCREMENT_OP)
374 Op->Common.Flags |= ACPI_PARSEOP_TARGET;
378 if (WalkState->DescendingCallback != NULL)
381 * Find the object. This will either insert the object into
382 * the namespace or simply look it up
384 WalkState->Op = *NewOp = Op;
386 Status = WalkState->DescendingCallback (WalkState, &Op);
387 Status = AcpiPsNextParseState (WalkState, Op, Status);
388 if (Status == AE_CTRL_PENDING)
390 Status = AE_CTRL_PARSE_PENDING;
394 return_ACPI_STATUS (Status);
398 /*******************************************************************************
400 * FUNCTION: AcpiPsCompleteOp
402 * PARAMETERS: WalkState - Current state
404 * Status - Parse status before complete Op
408 * DESCRIPTION: Complete Op
410 ******************************************************************************/
414 ACPI_WALK_STATE *WalkState,
415 ACPI_PARSE_OBJECT **Op,
421 ACPI_FUNCTION_TRACE_PTR (PsCompleteOp, WalkState);
425 * Finished one argument of the containing scope
427 WalkState->ParserState.Scope->ParseScope.ArgCount--;
429 /* Close this Op (will result in parse subtree deletion) */
431 Status2 = AcpiPsCompleteThisOp (WalkState, *Op);
432 if (ACPI_FAILURE (Status2))
434 return_ACPI_STATUS (Status2);
445 case AE_CTRL_TRANSFER:
447 /* We are about to transfer to a called method */
449 WalkState->PrevOp = NULL;
450 WalkState->PrevArgTypes = WalkState->ArgTypes;
451 return_ACPI_STATUS (Status);
455 AcpiPsPopScope (&(WalkState->ParserState), Op,
456 &WalkState->ArgTypes, &WalkState->ArgCount);
461 WalkState->OpInfo = AcpiPsGetOpcodeInfo ((*Op)->Common.AmlOpcode);
462 WalkState->Opcode = (*Op)->Common.AmlOpcode;
464 Status = WalkState->AscendingCallback (WalkState);
465 Status = AcpiPsNextParseState (WalkState, *Op, Status);
467 Status2 = AcpiPsCompleteThisOp (WalkState, *Op);
468 if (ACPI_FAILURE (Status2))
470 return_ACPI_STATUS (Status2);
478 case AE_CTRL_CONTINUE:
480 /* Pop off scopes until we find the While */
482 while (!(*Op) || ((*Op)->Common.AmlOpcode != AML_WHILE_OP))
484 AcpiPsPopScope (&(WalkState->ParserState), Op,
485 &WalkState->ArgTypes, &WalkState->ArgCount);
488 /* Close this iteration of the While loop */
491 WalkState->OpInfo = AcpiPsGetOpcodeInfo ((*Op)->Common.AmlOpcode);
492 WalkState->Opcode = (*Op)->Common.AmlOpcode;
494 Status = WalkState->AscendingCallback (WalkState);
495 Status = AcpiPsNextParseState (WalkState, *Op, Status);
497 Status2 = AcpiPsCompleteThisOp (WalkState, *Op);
498 if (ACPI_FAILURE (Status2))
500 return_ACPI_STATUS (Status2);
506 case AE_CTRL_TERMINATE:
513 Status2 = AcpiPsCompleteThisOp (WalkState, *Op);
514 if (ACPI_FAILURE (Status2))
516 return_ACPI_STATUS (Status2);
519 AcpiUtDeleteGenericState (
520 AcpiUtPopGenericState (&WalkState->ControlState));
523 AcpiPsPopScope (&(WalkState->ParserState), Op,
524 &WalkState->ArgTypes, &WalkState->ArgCount);
528 return_ACPI_STATUS (AE_OK);
530 default: /* All other non-AE_OK status */
536 Status2 = AcpiPsCompleteThisOp (WalkState, *Op);
537 if (ACPI_FAILURE (Status2))
539 return_ACPI_STATUS (Status2);
543 AcpiPsPopScope (&(WalkState->ParserState), Op,
544 &WalkState->ArgTypes, &WalkState->ArgCount);
551 * TBD: Cleanup parse ops on error
555 AcpiPsPopScope (ParserState, Op,
556 &WalkState->ArgTypes, &WalkState->ArgCount);
559 WalkState->PrevOp = NULL;
560 WalkState->PrevArgTypes = WalkState->ArgTypes;
561 return_ACPI_STATUS (Status);
564 /* This scope complete? */
566 if (AcpiPsHasCompletedScope (&(WalkState->ParserState)))
568 AcpiPsPopScope (&(WalkState->ParserState), Op,
569 &WalkState->ArgTypes, &WalkState->ArgCount);
570 ACPI_DEBUG_PRINT ((ACPI_DB_PARSE, "Popped scope, Op=%p\n", *Op));
577 return_ACPI_STATUS (AE_OK);
581 /*******************************************************************************
583 * FUNCTION: AcpiPsCompleteFinalOp
585 * PARAMETERS: WalkState - Current state
587 * Status - Current parse status before complete last
592 * DESCRIPTION: Complete last Op.
594 ******************************************************************************/
597 AcpiPsCompleteFinalOp (
598 ACPI_WALK_STATE *WalkState,
599 ACPI_PARSE_OBJECT *Op,
605 ACPI_FUNCTION_TRACE_PTR (PsCompleteFinalOp, WalkState);
609 * Complete the last Op (if not completed), and clear the scope stack.
610 * It is easily possible to end an AML "package" with an unbounded number
611 * of open scopes (such as when several ASL blocks are closed with
612 * sequential closing braces). We want to terminate each one cleanly.
614 ACPI_DEBUG_PRINT ((ACPI_DB_PARSE, "AML package complete at Op %p\n", Op));
619 if (WalkState->AscendingCallback != NULL)
622 WalkState->OpInfo = AcpiPsGetOpcodeInfo (Op->Common.AmlOpcode);
623 WalkState->Opcode = Op->Common.AmlOpcode;
625 Status = WalkState->AscendingCallback (WalkState);
626 Status = AcpiPsNextParseState (WalkState, Op, Status);
627 if (Status == AE_CTRL_PENDING)
629 Status = AcpiPsCompleteOp (WalkState, &Op, AE_OK);
630 if (ACPI_FAILURE (Status))
632 return_ACPI_STATUS (Status);
636 if (Status == AE_CTRL_TERMINATE)
645 Status2 = AcpiPsCompleteThisOp (WalkState, Op);
646 if (ACPI_FAILURE (Status2))
648 return_ACPI_STATUS (Status2);
652 AcpiPsPopScope (&(WalkState->ParserState), &Op,
653 &WalkState->ArgTypes, &WalkState->ArgCount);
657 return_ACPI_STATUS (Status);
660 else if (ACPI_FAILURE (Status))
662 /* First error is most important */
664 (void) AcpiPsCompleteThisOp (WalkState, Op);
665 return_ACPI_STATUS (Status);
669 Status2 = AcpiPsCompleteThisOp (WalkState, Op);
670 if (ACPI_FAILURE (Status2))
672 return_ACPI_STATUS (Status2);
676 AcpiPsPopScope (&(WalkState->ParserState), &Op, &WalkState->ArgTypes,
677 &WalkState->ArgCount);
681 return_ACPI_STATUS (Status);