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
395 * external opcode. For the error message, only print the AML
396 * offset. We could attempt to print the name but this may cause
397 * a segmentation fault when printing the namepath because the
398 * AML may be incorrect.
401 "// Invalid external declaration at AML offset 0x%x.\n",
402 WalkState
->Aml
- WalkState
->ParserState
.AmlStart
);
403 WalkState
->Aml
= WalkState
->ParserState
.Aml
+ 2;
404 return_ACPI_STATUS (AE_CTRL_PARSE_CONTINUE
);
407 if (ACPI_FAILURE (Status
))
409 return_ACPI_STATUS (Status
);
413 return_ACPI_STATUS (AE_OK
);
416 /* Not a named opcode, just allocate Op and append to parent */
418 if (WalkState
->OpInfo
->Flags
& AML_CREATE
)
421 * Backup to beginning of CreateXXXfield declaration
422 * BodyLength is unknown until we parse the body
424 Op
->Named
.Data
= AmlOpStart
;
425 Op
->Named
.Length
= 0;
428 if (WalkState
->Opcode
== AML_BANK_FIELD_OP
)
431 * Backup to beginning of BankField declaration
432 * BodyLength is unknown until we parse the body
434 Op
->Named
.Data
= AmlOpStart
;
435 Op
->Named
.Length
= 0;
438 ParentScope
= AcpiPsGetParentScope (&(WalkState
->ParserState
));
439 AcpiPsAppendArg (ParentScope
, Op
);
443 OpInfo
= AcpiPsGetOpcodeInfo (ParentScope
->Common
.AmlOpcode
);
444 if (OpInfo
->Flags
& AML_HAS_TARGET
)
446 ArgumentCount
= AcpiPsGetArgumentCount (OpInfo
->Type
);
447 if (ParentScope
->Common
.ArgListLength
> ArgumentCount
)
449 Op
->Common
.Flags
|= ACPI_PARSEOP_TARGET
;
454 * Special case for both Increment() and Decrement(), where
455 * the lone argument is both a source and a target.
457 else if ((ParentScope
->Common
.AmlOpcode
== AML_INCREMENT_OP
) ||
458 (ParentScope
->Common
.AmlOpcode
== AML_DECREMENT_OP
))
460 Op
->Common
.Flags
|= ACPI_PARSEOP_TARGET
;
464 if (WalkState
->DescendingCallback
!= NULL
)
467 * Find the object. This will either insert the object into
468 * the namespace or simply look it up
470 WalkState
->Op
= *NewOp
= Op
;
472 Status
= WalkState
->DescendingCallback (WalkState
, &Op
);
473 Status
= AcpiPsNextParseState (WalkState
, Op
, Status
);
474 if (Status
== AE_CTRL_PENDING
)
476 Status
= AE_CTRL_PARSE_PENDING
;
480 return_ACPI_STATUS (Status
);
484 /*******************************************************************************
486 * FUNCTION: AcpiPsCompleteOp
488 * PARAMETERS: WalkState - Current state
490 * Status - Parse status before complete Op
494 * DESCRIPTION: Complete Op
496 ******************************************************************************/
500 ACPI_WALK_STATE
*WalkState
,
501 ACPI_PARSE_OBJECT
**Op
,
507 ACPI_FUNCTION_TRACE_PTR (PsCompleteOp
, WalkState
);
511 * Finished one argument of the containing scope
513 WalkState
->ParserState
.Scope
->ParseScope
.ArgCount
--;
515 /* Close this Op (will result in parse subtree deletion) */
517 Status2
= AcpiPsCompleteThisOp (WalkState
, *Op
);
518 if (ACPI_FAILURE (Status2
))
520 return_ACPI_STATUS (Status2
);
531 case AE_CTRL_TRANSFER
:
533 /* We are about to transfer to a called method */
535 WalkState
->PrevOp
= NULL
;
536 WalkState
->PrevArgTypes
= WalkState
->ArgTypes
;
537 return_ACPI_STATUS (Status
);
541 AcpiPsPopScope (&(WalkState
->ParserState
), Op
,
542 &WalkState
->ArgTypes
, &WalkState
->ArgCount
);
547 WalkState
->OpInfo
= AcpiPsGetOpcodeInfo ((*Op
)->Common
.AmlOpcode
);
548 WalkState
->Opcode
= (*Op
)->Common
.AmlOpcode
;
550 Status
= WalkState
->AscendingCallback (WalkState
);
551 Status
= AcpiPsNextParseState (WalkState
, *Op
, Status
);
553 Status2
= AcpiPsCompleteThisOp (WalkState
, *Op
);
554 if (ACPI_FAILURE (Status2
))
556 return_ACPI_STATUS (Status2
);
564 case AE_CTRL_CONTINUE
:
566 /* Pop off scopes until we find the While */
568 while (!(*Op
) || ((*Op
)->Common
.AmlOpcode
!= AML_WHILE_OP
))
570 AcpiPsPopScope (&(WalkState
->ParserState
), Op
,
571 &WalkState
->ArgTypes
, &WalkState
->ArgCount
);
574 /* Close this iteration of the While loop */
577 WalkState
->OpInfo
= AcpiPsGetOpcodeInfo ((*Op
)->Common
.AmlOpcode
);
578 WalkState
->Opcode
= (*Op
)->Common
.AmlOpcode
;
580 Status
= WalkState
->AscendingCallback (WalkState
);
581 Status
= AcpiPsNextParseState (WalkState
, *Op
, Status
);
583 Status2
= AcpiPsCompleteThisOp (WalkState
, *Op
);
584 if (ACPI_FAILURE (Status2
))
586 return_ACPI_STATUS (Status2
);
592 case AE_CTRL_TERMINATE
:
599 Status2
= AcpiPsCompleteThisOp (WalkState
, *Op
);
600 if (ACPI_FAILURE (Status2
))
602 return_ACPI_STATUS (Status2
);
605 AcpiUtDeleteGenericState (
606 AcpiUtPopGenericState (&WalkState
->ControlState
));
609 AcpiPsPopScope (&(WalkState
->ParserState
), Op
,
610 &WalkState
->ArgTypes
, &WalkState
->ArgCount
);
614 return_ACPI_STATUS (AE_OK
);
616 default: /* All other non-AE_OK status */
622 Status2
= AcpiPsCompleteThisOp (WalkState
, *Op
);
623 if (ACPI_FAILURE (Status2
))
625 return_ACPI_STATUS (Status2
);
629 AcpiPsPopScope (&(WalkState
->ParserState
), Op
,
630 &WalkState
->ArgTypes
, &WalkState
->ArgCount
);
637 * TBD: Cleanup parse ops on error
641 AcpiPsPopScope (ParserState
, Op
,
642 &WalkState
->ArgTypes
, &WalkState
->ArgCount
);
645 WalkState
->PrevOp
= NULL
;
646 WalkState
->PrevArgTypes
= WalkState
->ArgTypes
;
647 return_ACPI_STATUS (Status
);
650 /* This scope complete? */
652 if (AcpiPsHasCompletedScope (&(WalkState
->ParserState
)))
654 AcpiPsPopScope (&(WalkState
->ParserState
), Op
,
655 &WalkState
->ArgTypes
, &WalkState
->ArgCount
);
656 ACPI_DEBUG_PRINT ((ACPI_DB_PARSE
, "Popped scope, Op=%p\n", *Op
));
663 return_ACPI_STATUS (AE_OK
);
667 /*******************************************************************************
669 * FUNCTION: AcpiPsCompleteFinalOp
671 * PARAMETERS: WalkState - Current state
673 * Status - Current parse status before complete last
678 * DESCRIPTION: Complete last Op.
680 ******************************************************************************/
683 AcpiPsCompleteFinalOp (
684 ACPI_WALK_STATE
*WalkState
,
685 ACPI_PARSE_OBJECT
*Op
,
691 ACPI_FUNCTION_TRACE_PTR (PsCompleteFinalOp
, WalkState
);
695 * Complete the last Op (if not completed), and clear the scope stack.
696 * It is easily possible to end an AML "package" with an unbounded number
697 * of open scopes (such as when several ASL blocks are closed with
698 * sequential closing braces). We want to terminate each one cleanly.
700 ACPI_DEBUG_PRINT ((ACPI_DB_PARSE
, "AML package complete at Op %p\n", Op
));
705 if (WalkState
->AscendingCallback
!= NULL
)
708 WalkState
->OpInfo
= AcpiPsGetOpcodeInfo (Op
->Common
.AmlOpcode
);
709 WalkState
->Opcode
= Op
->Common
.AmlOpcode
;
711 Status
= WalkState
->AscendingCallback (WalkState
);
712 Status
= AcpiPsNextParseState (WalkState
, Op
, Status
);
713 if (Status
== AE_CTRL_PENDING
)
715 Status
= AcpiPsCompleteOp (WalkState
, &Op
, AE_OK
);
716 if (ACPI_FAILURE (Status
))
718 return_ACPI_STATUS (Status
);
722 if (Status
== AE_CTRL_TERMINATE
)
731 Status2
= AcpiPsCompleteThisOp (WalkState
, Op
);
732 if (ACPI_FAILURE (Status2
))
734 return_ACPI_STATUS (Status2
);
738 AcpiPsPopScope (&(WalkState
->ParserState
), &Op
,
739 &WalkState
->ArgTypes
, &WalkState
->ArgCount
);
743 return_ACPI_STATUS (Status
);
746 else if (ACPI_FAILURE (Status
))
748 /* First error is most important */
750 (void) AcpiPsCompleteThisOp (WalkState
, Op
);
751 return_ACPI_STATUS (Status
);
755 Status2
= AcpiPsCompleteThisOp (WalkState
, Op
);
756 if (ACPI_FAILURE (Status2
))
758 return_ACPI_STATUS (Status2
);
762 AcpiPsPopScope (&(WalkState
->ParserState
), &Op
, &WalkState
->ArgTypes
,
763 &WalkState
->ArgCount
);
767 return_ACPI_STATUS (Status
);