1 /******************************************************************************
3 * Module Name: psobject - Support for parse objects
5 *****************************************************************************/
8 * Copyright (C) 2000 - 2017, 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.
48 #include "acconvert.h"
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
)
80 ACPI_FUNCTION_TRACE_PTR (PsGetAmlOpcode
, WalkState
);
83 WalkState
->Aml
= WalkState
->ParserState
.Aml
;
84 WalkState
->Opcode
= AcpiPsPeekOpcode (&(WalkState
->ParserState
));
87 * First cut to determine what we have found:
88 * 1) A valid AML opcode
90 * 3) An unknown/invalid opcode
92 WalkState
->OpInfo
= AcpiPsGetOpcodeInfo (WalkState
->Opcode
);
94 switch (WalkState
->OpInfo
->Class
)
97 case AML_CLASS_PREFIX
:
99 * Starts with a valid prefix or ASCII char, this is a name
100 * string. Convert the bare name string to a namepath.
102 WalkState
->Opcode
= AML_INT_NAMEPATH_OP
;
103 WalkState
->ArgTypes
= ARGP_NAMESTRING
;
106 case AML_CLASS_UNKNOWN
:
108 /* The opcode is unrecognized. Complain and skip unknown opcodes */
110 if (WalkState
->PassNumber
== 2)
112 AmlOffset
= (UINT32
) ACPI_PTR_DIFF (WalkState
->Aml
,
113 WalkState
->ParserState
.AmlStart
);
115 ACPI_ERROR ((AE_INFO
,
116 "Unknown opcode 0x%.2X at table offset 0x%.4X, ignoring",
118 (UINT32
) (AmlOffset
+ sizeof (ACPI_TABLE_HEADER
))));
120 ACPI_DUMP_BUFFER ((WalkState
->ParserState
.Aml
- 16), 48);
122 #ifdef ACPI_ASL_COMPILER
124 * This is executed for the disassembler only. Output goes
125 * to the disassembled ASL output file.
128 "/*\nError: Unknown opcode 0x%.2X at table offset 0x%.4X, context:\n",
130 (UINT32
) (AmlOffset
+ sizeof (ACPI_TABLE_HEADER
)));
132 ACPI_ERROR ((AE_INFO
,
133 "Aborting disassembly, AML byte code is corrupt"));
135 /* Dump the context surrounding the invalid opcode */
137 AcpiUtDumpBuffer (((UINT8
*) WalkState
->ParserState
.Aml
- 16),
139 (AmlOffset
+ sizeof (ACPI_TABLE_HEADER
) - 16));
140 AcpiOsPrintf (" */\n");
143 * Just abort the disassembly, cannot continue because the
144 * parser is essentially lost. The disassembler can then
145 * randomly fail because an ill-constructed parse tree
148 return_ACPI_STATUS (AE_AML_BAD_OPCODE
);
152 /* Increment past one-byte or two-byte opcode */
154 WalkState
->ParserState
.Aml
++;
155 if (WalkState
->Opcode
> 0xFF) /* Can only happen if first byte is 0x5B */
157 WalkState
->ParserState
.Aml
++;
160 return_ACPI_STATUS (AE_CTRL_PARSE_CONTINUE
);
164 /* Found opcode info, this is a normal opcode */
166 WalkState
->ParserState
.Aml
+=
167 AcpiPsGetOpcodeSize (WalkState
->Opcode
);
168 WalkState
->ArgTypes
= WalkState
->OpInfo
->ParseArgs
;
172 return_ACPI_STATUS (AE_OK
);
176 /*******************************************************************************
178 * FUNCTION: AcpiPsBuildNamedOp
180 * PARAMETERS: WalkState - Current state
181 * AmlOpStart - Begin of named Op in AML
182 * UnnamedOp - Early Op (not a named Op)
187 * DESCRIPTION: Parse a named Op
189 ******************************************************************************/
193 ACPI_WALK_STATE
*WalkState
,
195 ACPI_PARSE_OBJECT
*UnnamedOp
,
196 ACPI_PARSE_OBJECT
**Op
)
198 ACPI_STATUS Status
= AE_OK
;
199 ACPI_PARSE_OBJECT
*Arg
= NULL
;
202 ACPI_FUNCTION_TRACE_PTR (PsBuildNamedOp
, WalkState
);
205 UnnamedOp
->Common
.Value
.Arg
= NULL
;
206 UnnamedOp
->Common
.ArgListLength
= 0;
207 UnnamedOp
->Common
.AmlOpcode
= WalkState
->Opcode
;
210 * Get and append arguments until we find the node that contains
211 * the name (the type ARGP_NAME).
213 while (GET_CURRENT_ARG_TYPE (WalkState
->ArgTypes
) &&
214 (GET_CURRENT_ARG_TYPE (WalkState
->ArgTypes
) != ARGP_NAME
))
216 ASL_CV_CAPTURE_COMMENTS (WalkState
);
217 Status
= AcpiPsGetNextArg (WalkState
, &(WalkState
->ParserState
),
218 GET_CURRENT_ARG_TYPE (WalkState
->ArgTypes
), &Arg
);
219 if (ACPI_FAILURE (Status
))
221 return_ACPI_STATUS (Status
);
224 AcpiPsAppendArg (UnnamedOp
, Arg
);
225 INCREMENT_ARG_LIST (WalkState
->ArgTypes
);
228 /* are there any inline comments associated with the NameSeg?? If so, save this. */
230 ASL_CV_CAPTURE_COMMENTS (WalkState
);
232 #ifdef ACPI_ASL_COMPILER
233 if (AcpiGbl_CurrentInlineComment
!= NULL
)
235 UnnamedOp
->Common
.NameComment
= AcpiGbl_CurrentInlineComment
;
236 AcpiGbl_CurrentInlineComment
= NULL
;
241 * Make sure that we found a NAME and didn't run out of arguments
243 if (!GET_CURRENT_ARG_TYPE (WalkState
->ArgTypes
))
245 return_ACPI_STATUS (AE_AML_NO_OPERAND
);
248 /* We know that this arg is a name, move to next arg */
250 INCREMENT_ARG_LIST (WalkState
->ArgTypes
);
253 * Find the object. This will either insert the object into
254 * the namespace or simply look it up
256 WalkState
->Op
= NULL
;
258 Status
= WalkState
->DescendingCallback (WalkState
, Op
);
259 if (ACPI_FAILURE (Status
))
261 if (Status
!= AE_CTRL_TERMINATE
)
263 ACPI_EXCEPTION ((AE_INFO
, Status
, "During name lookup/catalog"));
265 return_ACPI_STATUS (Status
);
270 return_ACPI_STATUS (AE_CTRL_PARSE_CONTINUE
);
273 Status
= AcpiPsNextParseState (WalkState
, *Op
, Status
);
274 if (ACPI_FAILURE (Status
))
276 if (Status
== AE_CTRL_PENDING
)
278 Status
= AE_CTRL_PARSE_PENDING
;
280 return_ACPI_STATUS (Status
);
283 AcpiPsAppendArg (*Op
, UnnamedOp
->Common
.Value
.Arg
);
285 #ifdef ACPI_ASL_COMPILER
287 /* save any comments that might be associated with UnnamedOp. */
289 (*Op
)->Common
.InlineComment
= UnnamedOp
->Common
.InlineComment
;
290 (*Op
)->Common
.EndNodeComment
= UnnamedOp
->Common
.EndNodeComment
;
291 (*Op
)->Common
.CloseBraceComment
= UnnamedOp
->Common
.CloseBraceComment
;
292 (*Op
)->Common
.NameComment
= UnnamedOp
->Common
.NameComment
;
293 (*Op
)->Common
.CommentList
= UnnamedOp
->Common
.CommentList
;
294 (*Op
)->Common
.EndBlkComment
= UnnamedOp
->Common
.EndBlkComment
;
295 (*Op
)->Common
.CvFilename
= UnnamedOp
->Common
.CvFilename
;
296 (*Op
)->Common
.CvParentFilename
= UnnamedOp
->Common
.CvParentFilename
;
297 (*Op
)->Named
.Aml
= UnnamedOp
->Common
.Aml
;
299 UnnamedOp
->Common
.InlineComment
= NULL
;
300 UnnamedOp
->Common
.EndNodeComment
= NULL
;
301 UnnamedOp
->Common
.CloseBraceComment
= NULL
;
302 UnnamedOp
->Common
.NameComment
= NULL
;
303 UnnamedOp
->Common
.CommentList
= NULL
;
304 UnnamedOp
->Common
.EndBlkComment
= NULL
;
307 if ((*Op
)->Common
.AmlOpcode
== AML_REGION_OP
||
308 (*Op
)->Common
.AmlOpcode
== AML_DATA_REGION_OP
)
311 * Defer final parsing of an OperationRegion body, because we don't
312 * have enough info in the first pass to parse it correctly (i.e.,
313 * there may be method calls within the TermArg elements of the body.)
315 * However, we must continue parsing because the opregion is not a
316 * standalone package -- we don't know where the end is at this point.
318 * (Length is unknown until parse of the body complete)
320 (*Op
)->Named
.Data
= AmlOpStart
;
321 (*Op
)->Named
.Length
= 0;
324 return_ACPI_STATUS (AE_OK
);
328 /*******************************************************************************
330 * FUNCTION: AcpiPsCreateOp
332 * PARAMETERS: WalkState - Current state
333 * AmlOpStart - Op start in AML
334 * NewOp - Returned Op
338 * DESCRIPTION: Get Op from AML
340 ******************************************************************************/
344 ACPI_WALK_STATE
*WalkState
,
346 ACPI_PARSE_OBJECT
**NewOp
)
348 ACPI_STATUS Status
= AE_OK
;
349 ACPI_PARSE_OBJECT
*Op
;
350 ACPI_PARSE_OBJECT
*NamedOp
= NULL
;
351 ACPI_PARSE_OBJECT
*ParentScope
;
353 const ACPI_OPCODE_INFO
*OpInfo
;
356 ACPI_FUNCTION_TRACE_PTR (PsCreateOp
, WalkState
);
359 Status
= AcpiPsGetAmlOpcode (WalkState
);
360 if (Status
== AE_CTRL_PARSE_CONTINUE
)
362 return_ACPI_STATUS (AE_CTRL_PARSE_CONTINUE
);
364 if (ACPI_FAILURE (Status
))
366 return_ACPI_STATUS (Status
);
369 /* Create Op structure and append to parent's argument list */
371 WalkState
->OpInfo
= AcpiPsGetOpcodeInfo (WalkState
->Opcode
);
372 Op
= AcpiPsAllocOp (WalkState
->Opcode
, AmlOpStart
);
375 return_ACPI_STATUS (AE_NO_MEMORY
);
378 if (WalkState
->OpInfo
->Flags
& AML_NAMED
)
380 Status
= AcpiPsBuildNamedOp (WalkState
, AmlOpStart
, Op
, &NamedOp
);
383 #ifdef ACPI_ASL_COMPILER
384 if (AcpiGbl_DisasmFlag
&& WalkState
->Opcode
== AML_EXTERNAL_OP
&&
385 Status
== AE_NOT_FOUND
)
388 * If parsing of AML_EXTERNAL_OP's name path fails, then skip
389 * past this opcode and keep parsing. This is a much better
390 * alternative than to abort the entire disassembler. At this
391 * point, the ParserState is at the end of the namepath of the
392 * external declaration opcode. Setting WalkState->Aml to
393 * WalkState->ParserState.Aml + 2 moves increments the
394 * WalkState->Aml past the object type and the paramcount of the
397 WalkState
->Aml
= WalkState
->ParserState
.Aml
+ 2;
398 WalkState
->ParserState
.Aml
= WalkState
->Aml
;
399 return_ACPI_STATUS (AE_CTRL_PARSE_CONTINUE
);
402 if (ACPI_FAILURE (Status
))
404 return_ACPI_STATUS (Status
);
408 return_ACPI_STATUS (AE_OK
);
411 /* Not a named opcode, just allocate Op and append to parent */
413 if (WalkState
->OpInfo
->Flags
& AML_CREATE
)
416 * Backup to beginning of CreateXXXfield declaration
417 * BodyLength is unknown until we parse the body
419 Op
->Named
.Data
= AmlOpStart
;
420 Op
->Named
.Length
= 0;
423 if (WalkState
->Opcode
== AML_BANK_FIELD_OP
)
426 * Backup to beginning of BankField declaration
427 * BodyLength is unknown until we parse the body
429 Op
->Named
.Data
= AmlOpStart
;
430 Op
->Named
.Length
= 0;
433 ParentScope
= AcpiPsGetParentScope (&(WalkState
->ParserState
));
434 AcpiPsAppendArg (ParentScope
, Op
);
438 OpInfo
= AcpiPsGetOpcodeInfo (ParentScope
->Common
.AmlOpcode
);
439 if (OpInfo
->Flags
& AML_HAS_TARGET
)
441 ArgumentCount
= AcpiPsGetArgumentCount (OpInfo
->Type
);
442 if (ParentScope
->Common
.ArgListLength
> ArgumentCount
)
444 Op
->Common
.Flags
|= ACPI_PARSEOP_TARGET
;
449 * Special case for both Increment() and Decrement(), where
450 * the lone argument is both a source and a target.
452 else if ((ParentScope
->Common
.AmlOpcode
== AML_INCREMENT_OP
) ||
453 (ParentScope
->Common
.AmlOpcode
== AML_DECREMENT_OP
))
455 Op
->Common
.Flags
|= ACPI_PARSEOP_TARGET
;
459 if (WalkState
->DescendingCallback
!= NULL
)
462 * Find the object. This will either insert the object into
463 * the namespace or simply look it up
465 WalkState
->Op
= *NewOp
= Op
;
467 Status
= WalkState
->DescendingCallback (WalkState
, &Op
);
468 Status
= AcpiPsNextParseState (WalkState
, Op
, Status
);
469 if (Status
== AE_CTRL_PENDING
)
471 Status
= AE_CTRL_PARSE_PENDING
;
475 return_ACPI_STATUS (Status
);
479 /*******************************************************************************
481 * FUNCTION: AcpiPsCompleteOp
483 * PARAMETERS: WalkState - Current state
485 * Status - Parse status before complete Op
489 * DESCRIPTION: Complete Op
491 ******************************************************************************/
495 ACPI_WALK_STATE
*WalkState
,
496 ACPI_PARSE_OBJECT
**Op
,
502 ACPI_FUNCTION_TRACE_PTR (PsCompleteOp
, WalkState
);
506 * Finished one argument of the containing scope
508 WalkState
->ParserState
.Scope
->ParseScope
.ArgCount
--;
510 /* Close this Op (will result in parse subtree deletion) */
512 Status2
= AcpiPsCompleteThisOp (WalkState
, *Op
);
513 if (ACPI_FAILURE (Status2
))
515 return_ACPI_STATUS (Status2
);
526 case AE_CTRL_TRANSFER
:
528 /* We are about to transfer to a called method */
530 WalkState
->PrevOp
= NULL
;
531 WalkState
->PrevArgTypes
= WalkState
->ArgTypes
;
532 return_ACPI_STATUS (Status
);
536 AcpiPsPopScope (&(WalkState
->ParserState
), Op
,
537 &WalkState
->ArgTypes
, &WalkState
->ArgCount
);
542 WalkState
->OpInfo
= AcpiPsGetOpcodeInfo ((*Op
)->Common
.AmlOpcode
);
543 WalkState
->Opcode
= (*Op
)->Common
.AmlOpcode
;
545 Status
= WalkState
->AscendingCallback (WalkState
);
546 Status
= AcpiPsNextParseState (WalkState
, *Op
, Status
);
548 Status2
= AcpiPsCompleteThisOp (WalkState
, *Op
);
549 if (ACPI_FAILURE (Status2
))
551 return_ACPI_STATUS (Status2
);
559 case AE_CTRL_CONTINUE
:
561 /* Pop off scopes until we find the While */
563 while (!(*Op
) || ((*Op
)->Common
.AmlOpcode
!= AML_WHILE_OP
))
565 AcpiPsPopScope (&(WalkState
->ParserState
), Op
,
566 &WalkState
->ArgTypes
, &WalkState
->ArgCount
);
569 /* Close this iteration of the While loop */
572 WalkState
->OpInfo
= AcpiPsGetOpcodeInfo ((*Op
)->Common
.AmlOpcode
);
573 WalkState
->Opcode
= (*Op
)->Common
.AmlOpcode
;
575 Status
= WalkState
->AscendingCallback (WalkState
);
576 Status
= AcpiPsNextParseState (WalkState
, *Op
, Status
);
578 Status2
= AcpiPsCompleteThisOp (WalkState
, *Op
);
579 if (ACPI_FAILURE (Status2
))
581 return_ACPI_STATUS (Status2
);
587 case AE_CTRL_TERMINATE
:
594 Status2
= AcpiPsCompleteThisOp (WalkState
, *Op
);
595 if (ACPI_FAILURE (Status2
))
597 return_ACPI_STATUS (Status2
);
600 AcpiUtDeleteGenericState (
601 AcpiUtPopGenericState (&WalkState
->ControlState
));
604 AcpiPsPopScope (&(WalkState
->ParserState
), Op
,
605 &WalkState
->ArgTypes
, &WalkState
->ArgCount
);
609 return_ACPI_STATUS (AE_OK
);
611 default: /* All other non-AE_OK status */
617 Status2
= AcpiPsCompleteThisOp (WalkState
, *Op
);
618 if (ACPI_FAILURE (Status2
))
620 return_ACPI_STATUS (Status2
);
624 AcpiPsPopScope (&(WalkState
->ParserState
), Op
,
625 &WalkState
->ArgTypes
, &WalkState
->ArgCount
);
632 * TBD: Cleanup parse ops on error
636 AcpiPsPopScope (ParserState
, Op
,
637 &WalkState
->ArgTypes
, &WalkState
->ArgCount
);
640 WalkState
->PrevOp
= NULL
;
641 WalkState
->PrevArgTypes
= WalkState
->ArgTypes
;
642 return_ACPI_STATUS (Status
);
645 /* This scope complete? */
647 if (AcpiPsHasCompletedScope (&(WalkState
->ParserState
)))
649 AcpiPsPopScope (&(WalkState
->ParserState
), Op
,
650 &WalkState
->ArgTypes
, &WalkState
->ArgCount
);
651 ACPI_DEBUG_PRINT ((ACPI_DB_PARSE
, "Popped scope, Op=%p\n", *Op
));
658 return_ACPI_STATUS (AE_OK
);
662 /*******************************************************************************
664 * FUNCTION: AcpiPsCompleteFinalOp
666 * PARAMETERS: WalkState - Current state
668 * Status - Current parse status before complete last
673 * DESCRIPTION: Complete last Op.
675 ******************************************************************************/
678 AcpiPsCompleteFinalOp (
679 ACPI_WALK_STATE
*WalkState
,
680 ACPI_PARSE_OBJECT
*Op
,
686 ACPI_FUNCTION_TRACE_PTR (PsCompleteFinalOp
, WalkState
);
690 * Complete the last Op (if not completed), and clear the scope stack.
691 * It is easily possible to end an AML "package" with an unbounded number
692 * of open scopes (such as when several ASL blocks are closed with
693 * sequential closing braces). We want to terminate each one cleanly.
695 ACPI_DEBUG_PRINT ((ACPI_DB_PARSE
, "AML package complete at Op %p\n", Op
));
700 if (WalkState
->AscendingCallback
!= NULL
)
703 WalkState
->OpInfo
= AcpiPsGetOpcodeInfo (Op
->Common
.AmlOpcode
);
704 WalkState
->Opcode
= Op
->Common
.AmlOpcode
;
706 Status
= WalkState
->AscendingCallback (WalkState
);
707 Status
= AcpiPsNextParseState (WalkState
, Op
, Status
);
708 if (Status
== AE_CTRL_PENDING
)
710 Status
= AcpiPsCompleteOp (WalkState
, &Op
, AE_OK
);
711 if (ACPI_FAILURE (Status
))
713 return_ACPI_STATUS (Status
);
717 if (Status
== AE_CTRL_TERMINATE
)
726 Status2
= AcpiPsCompleteThisOp (WalkState
, Op
);
727 if (ACPI_FAILURE (Status2
))
729 return_ACPI_STATUS (Status2
);
733 AcpiPsPopScope (&(WalkState
->ParserState
), &Op
,
734 &WalkState
->ArgTypes
, &WalkState
->ArgCount
);
738 return_ACPI_STATUS (Status
);
741 else if (ACPI_FAILURE (Status
))
743 /* First error is most important */
745 (void) AcpiPsCompleteThisOp (WalkState
, Op
);
746 return_ACPI_STATUS (Status
);
750 Status2
= AcpiPsCompleteThisOp (WalkState
, Op
);
751 if (ACPI_FAILURE (Status2
))
753 return_ACPI_STATUS (Status2
);
757 AcpiPsPopScope (&(WalkState
->ParserState
), &Op
, &WalkState
->ArgTypes
,
758 &WalkState
->ArgCount
);
762 return_ACPI_STATUS (Status
);