xref: /freebsd/sys/contrib/dev/acpica/components/parser/psobject.c (revision 63a938566d524836885917d95bd491aa4400b181)
1 /******************************************************************************
2  *
3  * Module Name: psobject - Support for parse objects
4  *
5  *****************************************************************************/
6 
7 /******************************************************************************
8  *
9  * 1. Copyright Notice
10  *
11  * Some or all of this work - Copyright (c) 1999 - 2018, Intel Corp.
12  * All rights reserved.
13  *
14  * 2. License
15  *
16  * 2.1. This is your license from Intel Corp. under its intellectual property
17  * rights. You may have additional license terms from the party that provided
18  * you this software, covering your right to use that party's intellectual
19  * property rights.
20  *
21  * 2.2. Intel grants, free of charge, to any person ("Licensee") obtaining a
22  * copy of the source code appearing in this file ("Covered Code") an
23  * irrevocable, perpetual, worldwide license under Intel's copyrights in the
24  * base code distributed originally by Intel ("Original Intel Code") to copy,
25  * make derivatives, distribute, use and display any portion of the Covered
26  * Code in any form, with the right to sublicense such rights; and
27  *
28  * 2.3. Intel grants Licensee a non-exclusive and non-transferable patent
29  * license (with the right to sublicense), under only those claims of Intel
30  * patents that are infringed by the Original Intel Code, to make, use, sell,
31  * offer to sell, and import the Covered Code and derivative works thereof
32  * solely to the minimum extent necessary to exercise the above copyright
33  * license, and in no event shall the patent license extend to any additions
34  * to or modifications of the Original Intel Code. No other license or right
35  * is granted directly or by implication, estoppel or otherwise;
36  *
37  * The above copyright and patent license is granted only if the following
38  * conditions are met:
39  *
40  * 3. Conditions
41  *
42  * 3.1. Redistribution of Source with Rights to Further Distribute Source.
43  * Redistribution of source code of any substantial portion of the Covered
44  * Code or modification with rights to further distribute source must include
45  * the above Copyright Notice, the above License, this list of Conditions,
46  * and the following Disclaimer and Export Compliance provision. In addition,
47  * Licensee must cause all Covered Code to which Licensee contributes to
48  * contain a file documenting the changes Licensee made to create that Covered
49  * Code and the date of any change. Licensee must include in that file the
50  * documentation of any changes made by any predecessor Licensee. Licensee
51  * must include a prominent statement that the modification is derived,
52  * directly or indirectly, from Original Intel Code.
53  *
54  * 3.2. Redistribution of Source with no Rights to Further Distribute Source.
55  * Redistribution of source code of any substantial portion of the Covered
56  * Code or modification without rights to further distribute source must
57  * include the following Disclaimer and Export Compliance provision in the
58  * documentation and/or other materials provided with distribution. In
59  * addition, Licensee may not authorize further sublicense of source of any
60  * portion of the Covered Code, and must include terms to the effect that the
61  * license from Licensee to its licensee is limited to the intellectual
62  * property embodied in the software Licensee provides to its licensee, and
63  * not to intellectual property embodied in modifications its licensee may
64  * make.
65  *
66  * 3.3. Redistribution of Executable. Redistribution in executable form of any
67  * substantial portion of the Covered Code or modification must reproduce the
68  * above Copyright Notice, and the following Disclaimer and Export Compliance
69  * provision in the documentation and/or other materials provided with the
70  * distribution.
71  *
72  * 3.4. Intel retains all right, title, and interest in and to the Original
73  * Intel Code.
74  *
75  * 3.5. Neither the name Intel nor any other trademark owned or controlled by
76  * Intel shall be used in advertising or otherwise to promote the sale, use or
77  * other dealings in products derived from or relating to the Covered Code
78  * without prior written authorization from Intel.
79  *
80  * 4. Disclaimer and Export Compliance
81  *
82  * 4.1. INTEL MAKES NO WARRANTY OF ANY KIND REGARDING ANY SOFTWARE PROVIDED
83  * HERE. ANY SOFTWARE ORIGINATING FROM INTEL OR DERIVED FROM INTEL SOFTWARE
84  * IS PROVIDED "AS IS," AND INTEL WILL NOT PROVIDE ANY SUPPORT, ASSISTANCE,
85  * INSTALLATION, TRAINING OR OTHER SERVICES. INTEL WILL NOT PROVIDE ANY
86  * UPDATES, ENHANCEMENTS OR EXTENSIONS. INTEL SPECIFICALLY DISCLAIMS ANY
87  * IMPLIED WARRANTIES OF MERCHANTABILITY, NONINFRINGEMENT AND FITNESS FOR A
88  * PARTICULAR PURPOSE.
89  *
90  * 4.2. IN NO EVENT SHALL INTEL HAVE ANY LIABILITY TO LICENSEE, ITS LICENSEES
91  * OR ANY OTHER THIRD PARTY, FOR ANY LOST PROFITS, LOST DATA, LOSS OF USE OR
92  * COSTS OF PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, OR FOR ANY INDIRECT,
93  * SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THIS AGREEMENT, UNDER ANY
94  * CAUSE OF ACTION OR THEORY OF LIABILITY, AND IRRESPECTIVE OF WHETHER INTEL
95  * HAS ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES. THESE LIMITATIONS
96  * SHALL APPLY NOTWITHSTANDING THE FAILURE OF THE ESSENTIAL PURPOSE OF ANY
97  * LIMITED REMEDY.
98  *
99  * 4.3. Licensee shall not export, either directly or indirectly, any of this
100  * software or system incorporating such software without first obtaining any
101  * required license or other approval from the U. S. Department of Commerce or
102  * any other agency or department of the United States Government. In the
103  * event Licensee exports any such software from the United States or
104  * re-exports any such software from a foreign destination, Licensee shall
105  * ensure that the distribution and export/re-export of the software is in
106  * compliance with all laws, regulations, orders, or other restrictions of the
107  * U.S. Export Administration Regulations. Licensee agrees that neither it nor
108  * any of its subsidiaries will export/re-export any technical data, process,
109  * software, or service, directly or indirectly, to any country for which the
110  * United States government or any agency thereof requires an export license,
111  * other governmental approval, or letter of assurance, without first obtaining
112  * such license, approval or letter.
113  *
114  *****************************************************************************
115  *
116  * Alternatively, you may choose to be licensed under the terms of the
117  * following license:
118  *
119  * Redistribution and use in source and binary forms, with or without
120  * modification, are permitted provided that the following conditions
121  * are met:
122  * 1. Redistributions of source code must retain the above copyright
123  *    notice, this list of conditions, and the following disclaimer,
124  *    without modification.
125  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
126  *    substantially similar to the "NO WARRANTY" disclaimer below
127  *    ("Disclaimer") and any redistribution must be conditioned upon
128  *    including a substantially similar Disclaimer requirement for further
129  *    binary redistribution.
130  * 3. Neither the names of the above-listed copyright holders nor the names
131  *    of any contributors may be used to endorse or promote products derived
132  *    from this software without specific prior written permission.
133  *
134  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
135  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
136  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
137  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
138  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
139  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
140  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
141  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
142  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
143  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
144  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
145  *
146  * Alternatively, you may choose to be licensed under the terms of the
147  * GNU General Public License ("GPL") version 2 as published by the Free
148  * Software Foundation.
149  *
150  *****************************************************************************/
151 
152 #include <contrib/dev/acpica/include/acpi.h>
153 #include <contrib/dev/acpica/include/accommon.h>
154 #include <contrib/dev/acpica/include/acparser.h>
155 #include <contrib/dev/acpica/include/amlcode.h>
156 #include <contrib/dev/acpica/include/acconvert.h>
157 
158 #define _COMPONENT          ACPI_PARSER
159         ACPI_MODULE_NAME    ("psobject")
160 
161 
162 /* Local prototypes */
163 
164 static ACPI_STATUS
165 AcpiPsGetAmlOpcode (
166     ACPI_WALK_STATE         *WalkState);
167 
168 
169 /*******************************************************************************
170  *
171  * FUNCTION:    AcpiPsGetAmlOpcode
172  *
173  * PARAMETERS:  WalkState           - Current state
174  *
175  * RETURN:      Status
176  *
177  * DESCRIPTION: Extract the next AML opcode from the input stream.
178  *
179  ******************************************************************************/
180 
181 static ACPI_STATUS
182 AcpiPsGetAmlOpcode (
183     ACPI_WALK_STATE         *WalkState)
184 {
185     ACPI_ERROR_ONLY (UINT32 AmlOffset);
186 
187 
188     ACPI_FUNCTION_TRACE_PTR (PsGetAmlOpcode, WalkState);
189 
190 
191     WalkState->Aml = WalkState->ParserState.Aml;
192     WalkState->Opcode = AcpiPsPeekOpcode (&(WalkState->ParserState));
193 
194     /*
195      * First cut to determine what we have found:
196      * 1) A valid AML opcode
197      * 2) A name string
198      * 3) An unknown/invalid opcode
199      */
200     WalkState->OpInfo = AcpiPsGetOpcodeInfo (WalkState->Opcode);
201 
202     switch (WalkState->OpInfo->Class)
203     {
204     case AML_CLASS_ASCII:
205     case AML_CLASS_PREFIX:
206         /*
207          * Starts with a valid prefix or ASCII char, this is a name
208          * string. Convert the bare name string to a namepath.
209          */
210         WalkState->Opcode = AML_INT_NAMEPATH_OP;
211         WalkState->ArgTypes = ARGP_NAMESTRING;
212         break;
213 
214     case AML_CLASS_UNKNOWN:
215 
216         /* The opcode is unrecognized. Complain and skip unknown opcodes */
217 
218         if (WalkState->PassNumber == 2)
219         {
220             ACPI_ERROR_ONLY(AmlOffset = (UINT32) ACPI_PTR_DIFF (WalkState->Aml,
221                 WalkState->ParserState.AmlStart));
222 
223             ACPI_ERROR ((AE_INFO,
224                 "Unknown opcode 0x%.2X at table offset 0x%.4X, ignoring",
225                 WalkState->Opcode,
226                 (UINT32) (AmlOffset + sizeof (ACPI_TABLE_HEADER))));
227 
228             ACPI_DUMP_BUFFER ((WalkState->ParserState.Aml - 16), 48);
229 
230 #ifdef ACPI_ASL_COMPILER
231             /*
232              * This is executed for the disassembler only. Output goes
233              * to the disassembled ASL output file.
234              */
235             AcpiOsPrintf (
236                 "/*\nError: Unknown opcode 0x%.2X at table offset 0x%.4X, context:\n",
237                 WalkState->Opcode,
238                 (UINT32) (AmlOffset + sizeof (ACPI_TABLE_HEADER)));
239 
240             ACPI_ERROR ((AE_INFO,
241                 "Aborting disassembly, AML byte code is corrupt"));
242 
243             /* Dump the context surrounding the invalid opcode */
244 
245             AcpiUtDumpBuffer (((UINT8 *) WalkState->ParserState.Aml - 16),
246                 48, DB_BYTE_DISPLAY,
247                 (AmlOffset + sizeof (ACPI_TABLE_HEADER) - 16));
248             AcpiOsPrintf (" */\n");
249 
250             /*
251              * Just abort the disassembly, cannot continue because the
252              * parser is essentially lost. The disassembler can then
253              * randomly fail because an ill-constructed parse tree
254              * can result.
255              */
256             return_ACPI_STATUS (AE_AML_BAD_OPCODE);
257 #endif
258         }
259 
260         /* Increment past one-byte or two-byte opcode */
261 
262         WalkState->ParserState.Aml++;
263         if (WalkState->Opcode > 0xFF) /* Can only happen if first byte is 0x5B */
264         {
265             WalkState->ParserState.Aml++;
266         }
267 
268         return_ACPI_STATUS (AE_CTRL_PARSE_CONTINUE);
269 
270     default:
271 
272         /* Found opcode info, this is a normal opcode */
273 
274         WalkState->ParserState.Aml +=
275             AcpiPsGetOpcodeSize (WalkState->Opcode);
276         WalkState->ArgTypes = WalkState->OpInfo->ParseArgs;
277         break;
278     }
279 
280     return_ACPI_STATUS (AE_OK);
281 }
282 
283 
284 /*******************************************************************************
285  *
286  * FUNCTION:    AcpiPsBuildNamedOp
287  *
288  * PARAMETERS:  WalkState           - Current state
289  *              AmlOpStart          - Begin of named Op in AML
290  *              UnnamedOp           - Early Op (not a named Op)
291  *              Op                  - Returned Op
292  *
293  * RETURN:      Status
294  *
295  * DESCRIPTION: Parse a named Op
296  *
297  ******************************************************************************/
298 
299 ACPI_STATUS
300 AcpiPsBuildNamedOp (
301     ACPI_WALK_STATE         *WalkState,
302     UINT8                   *AmlOpStart,
303     ACPI_PARSE_OBJECT       *UnnamedOp,
304     ACPI_PARSE_OBJECT       **Op)
305 {
306     ACPI_STATUS             Status = AE_OK;
307     ACPI_PARSE_OBJECT       *Arg = NULL;
308 
309 
310     ACPI_FUNCTION_TRACE_PTR (PsBuildNamedOp, WalkState);
311 
312 
313     UnnamedOp->Common.Value.Arg = NULL;
314     UnnamedOp->Common.ArgListLength = 0;
315     UnnamedOp->Common.AmlOpcode = WalkState->Opcode;
316 
317     /*
318      * Get and append arguments until we find the node that contains
319      * the name (the type ARGP_NAME).
320      */
321     while (GET_CURRENT_ARG_TYPE (WalkState->ArgTypes) &&
322           (GET_CURRENT_ARG_TYPE (WalkState->ArgTypes) != ARGP_NAME))
323     {
324         ASL_CV_CAPTURE_COMMENTS (WalkState);
325         Status = AcpiPsGetNextArg (WalkState, &(WalkState->ParserState),
326             GET_CURRENT_ARG_TYPE (WalkState->ArgTypes), &Arg);
327         if (ACPI_FAILURE (Status))
328         {
329             return_ACPI_STATUS (Status);
330         }
331 
332         AcpiPsAppendArg (UnnamedOp, Arg);
333         INCREMENT_ARG_LIST (WalkState->ArgTypes);
334     }
335 
336     /* are there any inline comments associated with the NameSeg?? If so, save this. */
337 
338     ASL_CV_CAPTURE_COMMENTS (WalkState);
339 
340 #ifdef ACPI_ASL_COMPILER
341     if (AcpiGbl_CurrentInlineComment != NULL)
342     {
343         UnnamedOp->Common.NameComment = AcpiGbl_CurrentInlineComment;
344         AcpiGbl_CurrentInlineComment = NULL;
345     }
346 #endif
347 
348     /*
349      * Make sure that we found a NAME and didn't run out of arguments
350      */
351     if (!GET_CURRENT_ARG_TYPE (WalkState->ArgTypes))
352     {
353         return_ACPI_STATUS (AE_AML_NO_OPERAND);
354     }
355 
356     /* We know that this arg is a name, move to next arg */
357 
358     INCREMENT_ARG_LIST (WalkState->ArgTypes);
359 
360     /*
361      * Find the object. This will either insert the object into
362      * the namespace or simply look it up
363      */
364     WalkState->Op = NULL;
365 
366     Status = WalkState->DescendingCallback (WalkState, Op);
367     if (ACPI_FAILURE (Status))
368     {
369         if (Status != AE_CTRL_TERMINATE)
370         {
371             ACPI_EXCEPTION ((AE_INFO, Status, "During name lookup/catalog"));
372         }
373         return_ACPI_STATUS (Status);
374     }
375 
376     if (!*Op)
377     {
378         return_ACPI_STATUS (AE_CTRL_PARSE_CONTINUE);
379     }
380 
381     Status = AcpiPsNextParseState (WalkState, *Op, Status);
382     if (ACPI_FAILURE (Status))
383     {
384         if (Status == AE_CTRL_PENDING)
385         {
386             Status = AE_CTRL_PARSE_PENDING;
387         }
388         return_ACPI_STATUS (Status);
389     }
390 
391     AcpiPsAppendArg (*Op, UnnamedOp->Common.Value.Arg);
392 
393 #ifdef ACPI_ASL_COMPILER
394 
395     /* save any comments that might be associated with UnnamedOp. */
396 
397     (*Op)->Common.InlineComment     = UnnamedOp->Common.InlineComment;
398     (*Op)->Common.EndNodeComment    = UnnamedOp->Common.EndNodeComment;
399     (*Op)->Common.CloseBraceComment = UnnamedOp->Common.CloseBraceComment;
400     (*Op)->Common.NameComment       = UnnamedOp->Common.NameComment;
401     (*Op)->Common.CommentList       = UnnamedOp->Common.CommentList;
402     (*Op)->Common.EndBlkComment     = UnnamedOp->Common.EndBlkComment;
403     (*Op)->Common.CvFilename        = UnnamedOp->Common.CvFilename;
404     (*Op)->Common.CvParentFilename  = UnnamedOp->Common.CvParentFilename;
405     (*Op)->Named.Aml                = UnnamedOp->Common.Aml;
406 
407     UnnamedOp->Common.InlineComment     = NULL;
408     UnnamedOp->Common.EndNodeComment    = NULL;
409     UnnamedOp->Common.CloseBraceComment = NULL;
410     UnnamedOp->Common.NameComment       = NULL;
411     UnnamedOp->Common.CommentList       = NULL;
412     UnnamedOp->Common.EndBlkComment     = NULL;
413 #endif
414 
415     if ((*Op)->Common.AmlOpcode == AML_REGION_OP ||
416         (*Op)->Common.AmlOpcode == AML_DATA_REGION_OP)
417     {
418         /*
419          * Defer final parsing of an OperationRegion body, because we don't
420          * have enough info in the first pass to parse it correctly (i.e.,
421          * there may be method calls within the TermArg elements of the body.)
422          *
423          * However, we must continue parsing because the opregion is not a
424          * standalone package -- we don't know where the end is at this point.
425          *
426          * (Length is unknown until parse of the body complete)
427          */
428         (*Op)->Named.Data = AmlOpStart;
429         (*Op)->Named.Length = 0;
430     }
431 
432     return_ACPI_STATUS (AE_OK);
433 }
434 
435 
436 /*******************************************************************************
437  *
438  * FUNCTION:    AcpiPsCreateOp
439  *
440  * PARAMETERS:  WalkState           - Current state
441  *              AmlOpStart          - Op start in AML
442  *              NewOp               - Returned Op
443  *
444  * RETURN:      Status
445  *
446  * DESCRIPTION: Get Op from AML
447  *
448  ******************************************************************************/
449 
450 ACPI_STATUS
451 AcpiPsCreateOp (
452     ACPI_WALK_STATE         *WalkState,
453     UINT8                   *AmlOpStart,
454     ACPI_PARSE_OBJECT       **NewOp)
455 {
456     ACPI_STATUS             Status = AE_OK;
457     ACPI_PARSE_OBJECT       *Op;
458     ACPI_PARSE_OBJECT       *NamedOp = NULL;
459     ACPI_PARSE_OBJECT       *ParentScope;
460     UINT8                   ArgumentCount;
461     const ACPI_OPCODE_INFO  *OpInfo;
462 
463 
464     ACPI_FUNCTION_TRACE_PTR (PsCreateOp, WalkState);
465 
466 
467     Status = AcpiPsGetAmlOpcode (WalkState);
468     if (Status == AE_CTRL_PARSE_CONTINUE)
469     {
470         return_ACPI_STATUS (AE_CTRL_PARSE_CONTINUE);
471     }
472     if (ACPI_FAILURE (Status))
473     {
474         return_ACPI_STATUS (Status);
475     }
476 
477     /* Create Op structure and append to parent's argument list */
478 
479     WalkState->OpInfo = AcpiPsGetOpcodeInfo (WalkState->Opcode);
480     Op = AcpiPsAllocOp (WalkState->Opcode, AmlOpStart);
481     if (!Op)
482     {
483         return_ACPI_STATUS (AE_NO_MEMORY);
484     }
485 
486     if (WalkState->OpInfo->Flags & AML_NAMED)
487     {
488         Status = AcpiPsBuildNamedOp (WalkState, AmlOpStart, Op, &NamedOp);
489         AcpiPsFreeOp (Op);
490 
491 #ifdef ACPI_ASL_COMPILER
492         if (AcpiGbl_DisasmFlag && WalkState->Opcode == AML_EXTERNAL_OP &&
493             Status == AE_NOT_FOUND)
494         {
495             /*
496              * If parsing of AML_EXTERNAL_OP's name path fails, then skip
497              * past this opcode and keep parsing. This is a much better
498              * alternative than to abort the entire disassembler. At this
499              * point, the ParserState is at the end of the namepath of the
500              * external declaration opcode. Setting WalkState->Aml to
501              * WalkState->ParserState.Aml + 2 moves increments the
502              * WalkState->Aml past the object type and the paramcount of the
503              * external opcode.
504              */
505             WalkState->Aml = WalkState->ParserState.Aml + 2;
506             WalkState->ParserState.Aml = WalkState->Aml;
507             return_ACPI_STATUS (AE_CTRL_PARSE_CONTINUE);
508         }
509 #endif
510         if (ACPI_FAILURE (Status))
511         {
512             return_ACPI_STATUS (Status);
513         }
514 
515         *NewOp = NamedOp;
516         return_ACPI_STATUS (AE_OK);
517     }
518 
519     /* Not a named opcode, just allocate Op and append to parent */
520 
521     if (WalkState->OpInfo->Flags & AML_CREATE)
522     {
523         /*
524          * Backup to beginning of CreateXXXfield declaration
525          * BodyLength is unknown until we parse the body
526          */
527         Op->Named.Data = AmlOpStart;
528         Op->Named.Length = 0;
529     }
530 
531     if (WalkState->Opcode == AML_BANK_FIELD_OP)
532     {
533         /*
534          * Backup to beginning of BankField declaration
535          * BodyLength is unknown until we parse the body
536          */
537         Op->Named.Data = AmlOpStart;
538         Op->Named.Length = 0;
539     }
540 
541     ParentScope = AcpiPsGetParentScope (&(WalkState->ParserState));
542     AcpiPsAppendArg (ParentScope, Op);
543 
544     if (ParentScope)
545     {
546         OpInfo = AcpiPsGetOpcodeInfo (ParentScope->Common.AmlOpcode);
547         if (OpInfo->Flags & AML_HAS_TARGET)
548         {
549             ArgumentCount = AcpiPsGetArgumentCount (OpInfo->Type);
550             if (ParentScope->Common.ArgListLength > ArgumentCount)
551             {
552                 Op->Common.Flags |= ACPI_PARSEOP_TARGET;
553             }
554         }
555 
556         /*
557          * Special case for both Increment() and Decrement(), where
558          * the lone argument is both a source and a target.
559          */
560         else if ((ParentScope->Common.AmlOpcode == AML_INCREMENT_OP) ||
561                 (ParentScope->Common.AmlOpcode == AML_DECREMENT_OP))
562         {
563             Op->Common.Flags |= ACPI_PARSEOP_TARGET;
564         }
565     }
566 
567     if (WalkState->DescendingCallback != NULL)
568     {
569         /*
570          * Find the object. This will either insert the object into
571          * the namespace or simply look it up
572          */
573         WalkState->Op = *NewOp = Op;
574 
575         Status = WalkState->DescendingCallback (WalkState, &Op);
576         Status = AcpiPsNextParseState (WalkState, Op, Status);
577         if (Status == AE_CTRL_PENDING)
578         {
579             Status = AE_CTRL_PARSE_PENDING;
580         }
581     }
582 
583     return_ACPI_STATUS (Status);
584 }
585 
586 
587 /*******************************************************************************
588  *
589  * FUNCTION:    AcpiPsCompleteOp
590  *
591  * PARAMETERS:  WalkState           - Current state
592  *              Op                  - Returned Op
593  *              Status              - Parse status before complete Op
594  *
595  * RETURN:      Status
596  *
597  * DESCRIPTION: Complete Op
598  *
599  ******************************************************************************/
600 
601 ACPI_STATUS
602 AcpiPsCompleteOp (
603     ACPI_WALK_STATE         *WalkState,
604     ACPI_PARSE_OBJECT       **Op,
605     ACPI_STATUS             Status)
606 {
607     ACPI_STATUS             Status2;
608 
609 
610     ACPI_FUNCTION_TRACE_PTR (PsCompleteOp, WalkState);
611 
612 
613     /*
614      * Finished one argument of the containing scope
615      */
616     WalkState->ParserState.Scope->ParseScope.ArgCount--;
617 
618     /* Close this Op (will result in parse subtree deletion) */
619 
620     Status2 = AcpiPsCompleteThisOp (WalkState, *Op);
621     if (ACPI_FAILURE (Status2))
622     {
623         return_ACPI_STATUS (Status2);
624     }
625 
626     *Op = NULL;
627 
628     switch (Status)
629     {
630     case AE_OK:
631 
632         break;
633 
634     case AE_CTRL_TRANSFER:
635 
636         /* We are about to transfer to a called method */
637 
638         WalkState->PrevOp = NULL;
639         WalkState->PrevArgTypes = WalkState->ArgTypes;
640         return_ACPI_STATUS (Status);
641 
642     case AE_CTRL_END:
643 
644         AcpiPsPopScope (&(WalkState->ParserState), Op,
645             &WalkState->ArgTypes, &WalkState->ArgCount);
646 
647         if (*Op)
648         {
649             WalkState->Op = *Op;
650             WalkState->OpInfo = AcpiPsGetOpcodeInfo ((*Op)->Common.AmlOpcode);
651             WalkState->Opcode = (*Op)->Common.AmlOpcode;
652 
653             Status = WalkState->AscendingCallback (WalkState);
654             Status = AcpiPsNextParseState (WalkState, *Op, Status);
655 
656             Status2 = AcpiPsCompleteThisOp (WalkState, *Op);
657             if (ACPI_FAILURE (Status2))
658             {
659                 return_ACPI_STATUS (Status2);
660             }
661         }
662 
663         Status = AE_OK;
664         break;
665 
666     case AE_CTRL_BREAK:
667     case AE_CTRL_CONTINUE:
668 
669         /* Pop off scopes until we find the While */
670 
671         while (!(*Op) || ((*Op)->Common.AmlOpcode != AML_WHILE_OP))
672         {
673             AcpiPsPopScope (&(WalkState->ParserState), Op,
674                 &WalkState->ArgTypes, &WalkState->ArgCount);
675         }
676 
677         /* Close this iteration of the While loop */
678 
679         WalkState->Op = *Op;
680         WalkState->OpInfo = AcpiPsGetOpcodeInfo ((*Op)->Common.AmlOpcode);
681         WalkState->Opcode = (*Op)->Common.AmlOpcode;
682 
683         Status = WalkState->AscendingCallback (WalkState);
684         Status = AcpiPsNextParseState (WalkState, *Op, Status);
685 
686         Status2 = AcpiPsCompleteThisOp (WalkState, *Op);
687         if (ACPI_FAILURE (Status2))
688         {
689             return_ACPI_STATUS (Status2);
690         }
691 
692         Status = AE_OK;
693         break;
694 
695     case AE_CTRL_TERMINATE:
696 
697         /* Clean up */
698         do
699         {
700             if (*Op)
701             {
702                 Status2 = AcpiPsCompleteThisOp (WalkState, *Op);
703                 if (ACPI_FAILURE (Status2))
704                 {
705                     return_ACPI_STATUS (Status2);
706                 }
707 
708                 AcpiUtDeleteGenericState (
709                     AcpiUtPopGenericState (&WalkState->ControlState));
710             }
711 
712             AcpiPsPopScope (&(WalkState->ParserState), Op,
713                 &WalkState->ArgTypes, &WalkState->ArgCount);
714 
715         } while (*Op);
716 
717         return_ACPI_STATUS (AE_OK);
718 
719     default:  /* All other non-AE_OK status */
720 
721         do
722         {
723             if (*Op)
724             {
725                 Status2 = AcpiPsCompleteThisOp (WalkState, *Op);
726                 if (ACPI_FAILURE (Status2))
727                 {
728                     return_ACPI_STATUS (Status2);
729                 }
730             }
731 
732             AcpiPsPopScope (&(WalkState->ParserState), Op,
733                 &WalkState->ArgTypes, &WalkState->ArgCount);
734 
735         } while (*Op);
736 
737 
738 #if 0
739         /*
740          * TBD: Cleanup parse ops on error
741          */
742         if (*Op == NULL)
743         {
744             AcpiPsPopScope (ParserState, Op,
745                 &WalkState->ArgTypes, &WalkState->ArgCount);
746         }
747 #endif
748         WalkState->PrevOp = NULL;
749         WalkState->PrevArgTypes = WalkState->ArgTypes;
750         return_ACPI_STATUS (Status);
751     }
752 
753     /* This scope complete? */
754 
755     if (AcpiPsHasCompletedScope (&(WalkState->ParserState)))
756     {
757         AcpiPsPopScope (&(WalkState->ParserState), Op,
758             &WalkState->ArgTypes, &WalkState->ArgCount);
759         ACPI_DEBUG_PRINT ((ACPI_DB_PARSE, "Popped scope, Op=%p\n", *Op));
760     }
761     else
762     {
763         *Op = NULL;
764     }
765 
766     return_ACPI_STATUS (AE_OK);
767 }
768 
769 
770 /*******************************************************************************
771  *
772  * FUNCTION:    AcpiPsCompleteFinalOp
773  *
774  * PARAMETERS:  WalkState           - Current state
775  *              Op                  - Current Op
776  *              Status              - Current parse status before complete last
777  *                                    Op
778  *
779  * RETURN:      Status
780  *
781  * DESCRIPTION: Complete last Op.
782  *
783  ******************************************************************************/
784 
785 ACPI_STATUS
786 AcpiPsCompleteFinalOp (
787     ACPI_WALK_STATE         *WalkState,
788     ACPI_PARSE_OBJECT       *Op,
789     ACPI_STATUS             Status)
790 {
791     ACPI_STATUS             Status2;
792 
793 
794     ACPI_FUNCTION_TRACE_PTR (PsCompleteFinalOp, WalkState);
795 
796 
797     /*
798      * Complete the last Op (if not completed), and clear the scope stack.
799      * It is easily possible to end an AML "package" with an unbounded number
800      * of open scopes (such as when several ASL blocks are closed with
801      * sequential closing braces). We want to terminate each one cleanly.
802      */
803     ACPI_DEBUG_PRINT ((ACPI_DB_PARSE, "AML package complete at Op %p\n", Op));
804     do
805     {
806         if (Op)
807         {
808             if (WalkState->AscendingCallback != NULL)
809             {
810                 WalkState->Op = Op;
811                 WalkState->OpInfo = AcpiPsGetOpcodeInfo (Op->Common.AmlOpcode);
812                 WalkState->Opcode = Op->Common.AmlOpcode;
813 
814                 Status = WalkState->AscendingCallback (WalkState);
815                 Status = AcpiPsNextParseState (WalkState, Op, Status);
816                 if (Status == AE_CTRL_PENDING)
817                 {
818                     Status = AcpiPsCompleteOp (WalkState, &Op, AE_OK);
819                     if (ACPI_FAILURE (Status))
820                     {
821                         return_ACPI_STATUS (Status);
822                     }
823                 }
824 
825                 if (Status == AE_CTRL_TERMINATE)
826                 {
827                     Status = AE_OK;
828 
829                     /* Clean up */
830                     do
831                     {
832                         if (Op)
833                         {
834                             Status2 = AcpiPsCompleteThisOp (WalkState, Op);
835                             if (ACPI_FAILURE (Status2))
836                             {
837                                 return_ACPI_STATUS (Status2);
838                             }
839                         }
840 
841                         AcpiPsPopScope (&(WalkState->ParserState), &Op,
842                             &WalkState->ArgTypes, &WalkState->ArgCount);
843 
844                     } while (Op);
845 
846                     return_ACPI_STATUS (Status);
847                 }
848 
849                 else if (ACPI_FAILURE (Status))
850                 {
851                     /* First error is most important */
852 
853                     (void) AcpiPsCompleteThisOp (WalkState, Op);
854                     return_ACPI_STATUS (Status);
855                 }
856             }
857 
858             Status2 = AcpiPsCompleteThisOp (WalkState, Op);
859             if (ACPI_FAILURE (Status2))
860             {
861                 return_ACPI_STATUS (Status2);
862             }
863         }
864 
865         AcpiPsPopScope (&(WalkState->ParserState), &Op, &WalkState->ArgTypes,
866             &WalkState->ArgCount);
867 
868     } while (Op);
869 
870     return_ACPI_STATUS (Status);
871 }
872