* Intel ACPI 20030228 distribution with local DragonFly changes.
[dragonfly.git] / sys / contrib / dev / acpica / psparse.c
1 /******************************************************************************
2  *
3  * Module Name: psparse - Parser top level AML parse routines
4  *              $Revision: 139 $
5  *
6  *****************************************************************************/
7
8 /******************************************************************************
9  *
10  * 1. Copyright Notice
11  *
12  * Some or all of this work - Copyright (c) 1999 - 2003, Intel Corp.
13  * All rights reserved.
14  *
15  * 2. License
16  *
17  * 2.1. This is your license from Intel Corp. under its intellectual property
18  * rights.  You may have additional license terms from the party that provided
19  * you this software, covering your right to use that party's intellectual
20  * property rights.
21  *
22  * 2.2. Intel grants, free of charge, to any person ("Licensee") obtaining a
23  * copy of the source code appearing in this file ("Covered Code") an
24  * irrevocable, perpetual, worldwide license under Intel's copyrights in the
25  * base code distributed originally by Intel ("Original Intel Code") to copy,
26  * make derivatives, distribute, use and display any portion of the Covered
27  * Code in any form, with the right to sublicense such rights; and
28  *
29  * 2.3. Intel grants Licensee a non-exclusive and non-transferable patent
30  * license (with the right to sublicense), under only those claims of Intel
31  * patents that are infringed by the Original Intel Code, to make, use, sell,
32  * offer to sell, and import the Covered Code and derivative works thereof
33  * solely to the minimum extent necessary to exercise the above copyright
34  * license, and in no event shall the patent license extend to any additions
35  * to or modifications of the Original Intel Code.  No other license or right
36  * is granted directly or by implication, estoppel or otherwise;
37  *
38  * The above copyright and patent license is granted only if the following
39  * conditions are met:
40  *
41  * 3. Conditions
42  *
43  * 3.1. Redistribution of Source with Rights to Further Distribute Source.
44  * Redistribution of source code of any substantial portion of the Covered
45  * Code or modification with rights to further distribute source must include
46  * the above Copyright Notice, the above License, this list of Conditions,
47  * and the following Disclaimer and Export Compliance provision.  In addition,
48  * Licensee must cause all Covered Code to which Licensee contributes to
49  * contain a file documenting the changes Licensee made to create that Covered
50  * Code and the date of any change.  Licensee must include in that file the
51  * documentation of any changes made by any predecessor Licensee.  Licensee
52  * must include a prominent statement that the modification is derived,
53  * directly or indirectly, from Original Intel Code.
54  *
55  * 3.2. Redistribution of Source with no Rights to Further Distribute Source.
56  * Redistribution of source code of any substantial portion of the Covered
57  * Code or modification without rights to further distribute source must
58  * include the following Disclaimer and Export Compliance provision in the
59  * documentation and/or other materials provided with distribution.  In
60  * addition, Licensee may not authorize further sublicense of source of any
61  * portion of the Covered Code, and must include terms to the effect that the
62  * license from Licensee to its licensee is limited to the intellectual
63  * property embodied in the software Licensee provides to its licensee, and
64  * not to intellectual property embodied in modifications its licensee may
65  * make.
66  *
67  * 3.3. Redistribution of Executable. Redistribution in executable form of any
68  * substantial portion of the Covered Code or modification must reproduce the
69  * above Copyright Notice, and the following Disclaimer and Export Compliance
70  * provision in the documentation and/or other materials provided with the
71  * distribution.
72  *
73  * 3.4. Intel retains all right, title, and interest in and to the Original
74  * Intel Code.
75  *
76  * 3.5. Neither the name Intel nor any other trademark owned or controlled by
77  * Intel shall be used in advertising or otherwise to promote the sale, use or
78  * other dealings in products derived from or relating to the Covered Code
79  * without prior written authorization from Intel.
80  *
81  * 4. Disclaimer and Export Compliance
82  *
83  * 4.1. INTEL MAKES NO WARRANTY OF ANY KIND REGARDING ANY SOFTWARE PROVIDED
84  * HERE.  ANY SOFTWARE ORIGINATING FROM INTEL OR DERIVED FROM INTEL SOFTWARE
85  * IS PROVIDED "AS IS," AND INTEL WILL NOT PROVIDE ANY SUPPORT,  ASSISTANCE,
86  * INSTALLATION, TRAINING OR OTHER SERVICES.  INTEL WILL NOT PROVIDE ANY
87  * UPDATES, ENHANCEMENTS OR EXTENSIONS.  INTEL SPECIFICALLY DISCLAIMS ANY
88  * IMPLIED WARRANTIES OF MERCHANTABILITY, NONINFRINGEMENT AND FITNESS FOR A
89  * PARTICULAR PURPOSE.
90  *
91  * 4.2. IN NO EVENT SHALL INTEL HAVE ANY LIABILITY TO LICENSEE, ITS LICENSEES
92  * OR ANY OTHER THIRD PARTY, FOR ANY LOST PROFITS, LOST DATA, LOSS OF USE OR
93  * COSTS OF PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, OR FOR ANY INDIRECT,
94  * SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THIS AGREEMENT, UNDER ANY
95  * CAUSE OF ACTION OR THEORY OF LIABILITY, AND IRRESPECTIVE OF WHETHER INTEL
96  * HAS ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES.  THESE LIMITATIONS
97  * SHALL APPLY NOTWITHSTANDING THE FAILURE OF THE ESSENTIAL PURPOSE OF ANY
98  * LIMITED REMEDY.
99  *
100  * 4.3. Licensee shall not export, either directly or indirectly, any of this
101  * software or system incorporating such software without first obtaining any
102  * required license or other approval from the U. S. Department of Commerce or
103  * any other agency or department of the United States Government.  In the
104  * event Licensee exports any such software from the United States or
105  * re-exports any such software from a foreign destination, Licensee shall
106  * ensure that the distribution and export/re-export of the software is in
107  * compliance with all laws, regulations, orders, or other restrictions of the
108  * U.S. Export Administration Regulations. Licensee agrees that neither it nor
109  * any of its subsidiaries will export/re-export any technical data, process,
110  * software, or service, directly or indirectly, to any country for which the
111  * United States government or any agency thereof requires an export license,
112  * other governmental approval, or letter of assurance, without first obtaining
113  * such license, approval or letter.
114  *
115  *****************************************************************************/
116 /* $DragonFly: src/sys/contrib/dev/acpica/Attic/psparse.c,v 1.1 2003/09/24 03:32:16 drhodus Exp $                                                               */
117
118
119 /*
120  * Parse the AML and build an operation tree as most interpreters,
121  * like Perl, do.  Parsing is done by hand rather than with a YACC
122  * generated parser to tightly constrain stack and dynamic memory
123  * usage.  At the same time, parsing is kept flexible and the code
124  * fairly compact by parsing based on a list of AML opcode
125  * templates in AmlOpInfo[]
126  */
127
128 #include "acpi.h"
129 #include "acparser.h"
130 #include "acdispat.h"
131 #include "amlcode.h"
132 #include "acnamesp.h"
133 #include "acinterp.h"
134
135 #define _COMPONENT          ACPI_PARSER
136         ACPI_MODULE_NAME    ("psparse")
137
138
139 static UINT32               AcpiGbl_Depth = 0;
140
141
142 /*******************************************************************************
143  *
144  * FUNCTION:    AcpiPsGetOpcodeSize
145  *
146  * PARAMETERS:  Opcode          - An AML opcode
147  *
148  * RETURN:      Size of the opcode, in bytes (1 or 2)
149  *
150  * DESCRIPTION: Get the size of the current opcode.
151  *
152  ******************************************************************************/
153
154 UINT32
155 AcpiPsGetOpcodeSize (
156     UINT32                  Opcode)
157 {
158
159     /* Extended (2-byte) opcode if > 255 */
160
161     if (Opcode > 0x00FF)
162     {
163         return (2);
164     }
165
166     /* Otherwise, just a single byte opcode */
167
168     return (1);
169 }
170
171
172 /*******************************************************************************
173  *
174  * FUNCTION:    AcpiPsPeekOpcode
175  *
176  * PARAMETERS:  ParserState         - A parser state object
177  *
178  * RETURN:      Status
179  *
180  * DESCRIPTION: Get next AML opcode (without incrementing AML pointer)
181  *
182  ******************************************************************************/
183
184 UINT16
185 AcpiPsPeekOpcode (
186     ACPI_PARSE_STATE        *ParserState)
187 {
188     UINT8                   *Aml;
189     UINT16                  Opcode;
190
191
192     Aml = ParserState->Aml;
193     Opcode = (UINT16) ACPI_GET8 (Aml);
194
195
196     if (Opcode == AML_EXTOP)
197     {
198         /* Extended opcode */
199
200         Aml++;
201         Opcode = (UINT16) ((Opcode << 8) | ACPI_GET8 (Aml));
202     }
203
204     return (Opcode);
205 }
206
207
208 /*******************************************************************************
209  *
210  * FUNCTION:    AcpiPsCompleteThisOp
211  *
212  * PARAMETERS:  WalkState       - Current State
213  *              Op              - Op to complete
214  *
215  * RETURN:      None.
216  *
217  * DESCRIPTION: Perform any cleanup at the completion of an Op.
218  *
219  ******************************************************************************/
220
221 void
222 AcpiPsCompleteThisOp (
223     ACPI_WALK_STATE         *WalkState,
224     ACPI_PARSE_OBJECT       *Op)
225 {
226     ACPI_PARSE_OBJECT       *Prev;
227     ACPI_PARSE_OBJECT       *Next;
228     const ACPI_OPCODE_INFO  *ParentInfo;
229     ACPI_PARSE_OBJECT       *ReplacementOp = NULL;
230
231
232     ACPI_FUNCTION_TRACE_PTR ("PsCompleteThisOp", Op);
233
234
235     /* Check for null Op, can happen if AML code is corrupt */
236
237     if (!Op)
238     {
239         return_VOID;
240     }
241
242     /* Delete this op and the subtree below it if asked to */
243
244     if (((WalkState->ParseFlags & ACPI_PARSE_TREE_MASK) == ACPI_PARSE_DELETE_TREE) &&
245         (WalkState->OpInfo->Class != AML_CLASS_ARGUMENT))
246     {
247         /* Make sure that we only delete this subtree */
248
249         if (Op->Common.Parent)
250         {
251             /*
252              * Check if we need to replace the operator and its subtree
253              * with a return value op (placeholder op)
254              */
255             ParentInfo  = AcpiPsGetOpcodeInfo (Op->Common.Parent->Common.AmlOpcode);
256
257             switch (ParentInfo->Class)
258             {
259             case AML_CLASS_CONTROL:
260                 break;
261
262             case AML_CLASS_CREATE:
263
264                 /*
265                  * These opcodes contain TermArg operands.  The current
266                  * op must be replaced by a placeholder return op
267                  */
268                 ReplacementOp = AcpiPsAllocOp (AML_INT_RETURN_VALUE_OP);
269                 if (!ReplacementOp)
270                 {
271                     return_VOID;
272                 }
273                 break;
274
275             case AML_CLASS_NAMED_OBJECT:
276
277                 /*
278                  * These opcodes contain TermArg operands.  The current
279                  * op must be replaced by a placeholder return op
280                  */
281                 if ((Op->Common.Parent->Common.AmlOpcode == AML_REGION_OP)       ||
282                     (Op->Common.Parent->Common.AmlOpcode == AML_DATA_REGION_OP)  ||
283                     (Op->Common.Parent->Common.AmlOpcode == AML_BUFFER_OP)       ||
284                     (Op->Common.Parent->Common.AmlOpcode == AML_PACKAGE_OP)      ||
285                     (Op->Common.Parent->Common.AmlOpcode == AML_VAR_PACKAGE_OP))
286                 {
287                     ReplacementOp = AcpiPsAllocOp (AML_INT_RETURN_VALUE_OP);
288                     if (!ReplacementOp)
289                     {
290                         return_VOID;
291                     }
292                 }
293
294                 if ((Op->Common.Parent->Common.AmlOpcode == AML_NAME_OP) &&
295                     (WalkState->DescendingCallback != AcpiDsExecBeginOp))
296
297                 {
298                     if ((Op->Common.AmlOpcode == AML_BUFFER_OP) ||
299                         (Op->Common.AmlOpcode == AML_PACKAGE_OP) ||
300                         (Op->Common.AmlOpcode == AML_VAR_PACKAGE_OP))
301                     {
302                         ReplacementOp = AcpiPsAllocOp (Op->Common.AmlOpcode);
303                         if (!ReplacementOp)
304                         {
305                             return_VOID;
306                         }
307
308                         ReplacementOp->Named.Data = Op->Named.Data;
309                         ReplacementOp->Named.Length = Op->Named.Length;
310                     }
311                 }
312                 break;
313
314             default:
315                 ReplacementOp = AcpiPsAllocOp (AML_INT_RETURN_VALUE_OP);
316                 if (!ReplacementOp)
317                 {
318                     return_VOID;
319                 }
320             }
321
322             /* We must unlink this op from the parent tree */
323
324             Prev = Op->Common.Parent->Common.Value.Arg;
325             if (Prev == Op)
326             {
327                 /* This op is the first in the list */
328
329                 if (ReplacementOp)
330                 {
331                     ReplacementOp->Common.Parent        = Op->Common.Parent;
332                     ReplacementOp->Common.Value.Arg     = NULL;
333                     ReplacementOp->Common.Node          = Op->Common.Node;
334                     Op->Common.Parent->Common.Value.Arg = ReplacementOp;
335                     ReplacementOp->Common.Next          = Op->Common.Next;
336                 }
337                 else
338                 {
339                     Op->Common.Parent->Common.Value.Arg    = Op->Common.Next;
340                 }
341             }
342
343             /* Search the parent list */
344
345             else while (Prev)
346             {
347                 /* Traverse all siblings in the parent's argument list */
348
349                 Next = Prev->Common.Next;
350                 if (Next == Op)
351                 {
352                     if (ReplacementOp)
353                     {
354                         ReplacementOp->Common.Parent    = Op->Common.Parent;
355                         ReplacementOp->Common.Value.Arg = NULL;
356                         ReplacementOp->Common.Node      = Op->Common.Node;
357                         Prev->Common.Next               = ReplacementOp;
358                         ReplacementOp->Common.Next      = Op->Common.Next;
359                         Next = NULL;
360                     }
361                     else
362                     {
363                         Prev->Common.Next = Op->Common.Next;
364                         Next = NULL;
365                     }
366                 }
367
368                 Prev = Next;
369             }
370         }
371
372         /* Now we can actually delete the subtree rooted at op */
373
374         AcpiPsDeleteParseTree (Op);
375
376         return_VOID;
377     }
378
379     return_VOID;
380 }
381
382
383 /*******************************************************************************
384  *
385  * FUNCTION:    AcpiPsNextParseState
386  *
387  * PARAMETERS:  ParserState         - Current parser state object
388  *
389  * RETURN:      Status
390  *
391  * DESCRIPTION: Update the parser state based upon the return exception from
392  *              the parser callback.
393  *
394  ******************************************************************************/
395
396 ACPI_STATUS
397 AcpiPsNextParseState (
398     ACPI_WALK_STATE         *WalkState,
399     ACPI_PARSE_OBJECT       *Op,
400     ACPI_STATUS             CallbackStatus)
401 {
402     ACPI_PARSE_STATE        *ParserState = &WalkState->ParserState;
403     ACPI_STATUS             Status = AE_CTRL_PENDING;
404
405
406     ACPI_FUNCTION_TRACE_PTR ("PsNextParseState", Op);
407
408
409     switch (CallbackStatus)
410     {
411     case AE_CTRL_TERMINATE:
412
413         /*
414          * A control method was terminated via a RETURN statement.
415          * The walk of this method is complete.
416          */
417         ParserState->Aml = ParserState->AmlEnd;
418         Status = AE_CTRL_TERMINATE;
419         break;
420
421
422     case AE_CTRL_BREAK:
423
424         ParserState->Aml = WalkState->AmlLastWhile;
425         WalkState->ControlState->Common.Value = FALSE;
426         Status = AE_CTRL_BREAK;
427         break;
428
429     case AE_CTRL_CONTINUE:
430
431
432         ParserState->Aml = WalkState->AmlLastWhile;
433         Status = AE_CTRL_CONTINUE;
434         break;
435
436     case AE_CTRL_PENDING:
437
438         ParserState->Aml = WalkState->AmlLastWhile;
439         break;
440
441 #if 0
442     case AE_CTRL_SKIP:
443
444         ParserState->Aml = ParserState->Scope->ParseScope.PkgEnd;
445         Status = AE_OK;
446         break;
447 #endif
448
449     case AE_CTRL_TRUE:
450
451         /*
452          * Predicate of an IF was true, and we are at the matching ELSE.
453          * Just close out this package
454          */
455         ParserState->Aml = AcpiPsGetNextPackageEnd (ParserState);
456         break;
457
458
459     case AE_CTRL_FALSE:
460
461         /*
462          * Either an IF/WHILE Predicate was false or we encountered a BREAK
463          * opcode.  In both cases, we do not execute the rest of the
464          * package;  We simply close out the parent (finishing the walk of
465          * this branch of the tree) and continue execution at the parent
466          * level.
467          */
468         ParserState->Aml = ParserState->Scope->ParseScope.PkgEnd;
469
470         /* In the case of a BREAK, just force a predicate (if any) to FALSE */
471
472         WalkState->ControlState->Common.Value = FALSE;
473         Status = AE_CTRL_END;
474         break;
475
476
477     case AE_CTRL_TRANSFER:
478
479         /*
480          * A method call (invocation) -- transfer control
481          */
482         Status = AE_CTRL_TRANSFER;
483         WalkState->PrevOp = Op;
484         WalkState->MethodCallOp = Op;
485         WalkState->MethodCallNode = (Op->Common.Value.Arg)->Common.Node;
486
487         /* Will return value (if any) be used by the caller? */
488
489         WalkState->ReturnUsed = AcpiDsIsResultUsed (Op, WalkState);
490         break;
491
492
493     default:
494         Status = CallbackStatus;
495         if ((CallbackStatus & AE_CODE_MASK) == AE_CODE_CONTROL)
496         {
497             Status = AE_OK;
498         }
499         break;
500     }
501
502     return_ACPI_STATUS (Status);
503 }
504
505
506 /*******************************************************************************
507  *
508  * FUNCTION:    AcpiPsParseLoop
509  *
510  * PARAMETERS:  ParserState         - Current parser state object
511  *
512  * RETURN:      Status
513  *
514  * DESCRIPTION: Parse AML (pointed to by the current parser state) and return
515  *              a tree of ops.
516  *
517  ******************************************************************************/
518
519 ACPI_STATUS
520 AcpiPsParseLoop (
521     ACPI_WALK_STATE         *WalkState)
522 {
523     ACPI_STATUS             Status = AE_OK;
524     ACPI_PARSE_OBJECT       *Op = NULL;     /* current op */
525     ACPI_PARSE_OBJECT       *Arg = NULL;
526     ACPI_PARSE_OBJECT       PreOp;
527     ACPI_PARSE_STATE        *ParserState;
528     UINT8                   *AmlOpStart = NULL;
529
530
531     ACPI_FUNCTION_TRACE_PTR ("PsParseLoop", WalkState);
532
533     if (WalkState->DescendingCallback == NULL)
534     {
535         return_ACPI_STATUS (AE_BAD_PARAMETER);
536     }
537
538
539     ParserState = &WalkState->ParserState;
540     WalkState->ArgTypes = 0;
541
542 #if (!defined (ACPI_NO_METHOD_EXECUTION) && !defined (ACPI_CONSTANT_EVAL_ONLY))
543     if (WalkState->WalkType & ACPI_WALK_METHOD_RESTART)
544     {
545         /* We are restarting a preempted control method */
546
547         if (AcpiPsHasCompletedScope (ParserState))
548         {
549             /*
550              * We must check if a predicate to an IF or WHILE statement
551              * was just completed
552              */
553             if ((ParserState->Scope->ParseScope.Op) &&
554                ((ParserState->Scope->ParseScope.Op->Common.AmlOpcode == AML_IF_OP) ||
555                 (ParserState->Scope->ParseScope.Op->Common.AmlOpcode == AML_WHILE_OP)) &&
556                 (WalkState->ControlState) &&
557                 (WalkState->ControlState->Common.State ==
558                     ACPI_CONTROL_PREDICATE_EXECUTING))
559             {
560                 /*
561                  * A predicate was just completed, get the value of the
562                  * predicate and branch based on that value
563                  */
564                 WalkState->Op = NULL;
565                 Status = AcpiDsGetPredicateValue (WalkState, ACPI_TO_POINTER (TRUE));
566                 if (ACPI_FAILURE (Status) &&
567                     ((Status & AE_CODE_MASK) != AE_CODE_CONTROL))
568                 {
569                     if (Status == AE_AML_NO_RETURN_VALUE)
570                     {
571                         ACPI_DEBUG_PRINT ((ACPI_DB_ERROR,
572                             "Invoked method did not return a value, %s\n",
573                             AcpiFormatException (Status)));
574
575                     }
576                     ACPI_DEBUG_PRINT ((ACPI_DB_ERROR, "GetPredicate Failed, %s\n",
577                         AcpiFormatException (Status)));
578                     return_ACPI_STATUS (Status);
579                 }
580
581                 Status = AcpiPsNextParseState (WalkState, Op, Status);
582             }
583
584             AcpiPsPopScope (ParserState, &Op,
585                 &WalkState->ArgTypes, &WalkState->ArgCount);
586             ACPI_DEBUG_PRINT ((ACPI_DB_PARSE, "Popped scope, Op=%p\n", Op));
587         }
588         else if (WalkState->PrevOp)
589         {
590             /* We were in the middle of an op */
591
592             Op = WalkState->PrevOp;
593             WalkState->ArgTypes = WalkState->PrevArgTypes;
594         }
595     }
596 #endif
597
598     /*
599      * Iterative parsing loop, while there is more aml to process:
600      */
601     while ((ParserState->Aml < ParserState->AmlEnd) || (Op))
602     {
603         AmlOpStart = ParserState->Aml;
604         if (!Op)
605         {
606             /* Get the next opcode from the AML stream */
607
608             WalkState->AmlOffset = ACPI_PTR_DIFF (ParserState->Aml,
609                                                   ParserState->AmlStart);
610             WalkState->Opcode    = AcpiPsPeekOpcode (ParserState);
611
612             /*
613              * First cut to determine what we have found:
614              * 1) A valid AML opcode
615              * 2) A name string
616              * 3) An unknown/invalid opcode
617              */
618             WalkState->OpInfo = AcpiPsGetOpcodeInfo (WalkState->Opcode);
619             switch (WalkState->OpInfo->Class)
620             {
621             case AML_CLASS_ASCII:
622             case AML_CLASS_PREFIX:
623                 /*
624                  * Starts with a valid prefix or ASCII char, this is a name
625                  * string.  Convert the bare name string to a namepath.
626                  */
627                 WalkState->Opcode = AML_INT_NAMEPATH_OP;
628                 WalkState->ArgTypes = ARGP_NAMESTRING;
629                 break;
630
631             case AML_CLASS_UNKNOWN:
632
633                 /* The opcode is unrecognized.  Just skip unknown opcodes */
634
635                 ACPI_DEBUG_PRINT ((ACPI_DB_ERROR,
636                     "Found unknown opcode %X at AML address %p offset %X, ignoring\n",
637                     WalkState->Opcode, ParserState->Aml, WalkState->AmlOffset));
638
639                 ACPI_DUMP_BUFFER (ParserState->Aml, 128);
640
641                 /* Assume one-byte bad opcode */
642
643                 ParserState->Aml++;
644                 continue;
645
646             default:
647
648                 /* Found opcode info, this is a normal opcode */
649
650                 ParserState->Aml += AcpiPsGetOpcodeSize (WalkState->Opcode);
651                 WalkState->ArgTypes = WalkState->OpInfo->ParseArgs;
652                 break;
653             }
654
655             /* Create Op structure and append to parent's argument list */
656
657             if (WalkState->OpInfo->Flags & AML_NAMED)
658             {
659                 PreOp.Common.Value.Arg = NULL;
660                 PreOp.Common.AmlOpcode = WalkState->Opcode;
661
662                 /*
663                  * Get and append arguments until we find the node that contains
664                  * the name (the type ARGP_NAME).
665                  */
666                 while (GET_CURRENT_ARG_TYPE (WalkState->ArgTypes) &&
667                       (GET_CURRENT_ARG_TYPE (WalkState->ArgTypes) != ARGP_NAME))
668                 {
669                     Status = AcpiPsGetNextArg (WalkState, ParserState,
670                                 GET_CURRENT_ARG_TYPE (WalkState->ArgTypes), &Arg);
671                     if (ACPI_FAILURE (Status))
672                     {
673                         goto CloseThisOp;
674                     }
675
676                     AcpiPsAppendArg (&PreOp, Arg);
677                     INCREMENT_ARG_LIST (WalkState->ArgTypes);
678                 }
679
680                 /* Make sure that we found a NAME and didn't run out of arguments */
681
682                 if (!GET_CURRENT_ARG_TYPE (WalkState->ArgTypes))
683                 {
684                     return_ACPI_STATUS (AE_AML_NO_OPERAND);
685                 }
686
687                 /* We know that this arg is a name, move to next arg */
688
689                 INCREMENT_ARG_LIST (WalkState->ArgTypes);
690
691                 /*
692                  * Find the object.  This will either insert the object into
693                  * the namespace or simply look it up
694                  */
695                 WalkState->Op = NULL;
696
697                 Status = WalkState->DescendingCallback (WalkState, &Op);
698                 if (ACPI_FAILURE (Status))
699                 {
700                     ACPI_DEBUG_PRINT ((ACPI_DB_ERROR, "During name lookup/catalog, %s\n",
701                             AcpiFormatException (Status)));
702                     goto CloseThisOp;
703                 }
704
705                 if (Op == NULL)
706                 {
707                     continue;
708                 }
709
710                 Status = AcpiPsNextParseState (WalkState, Op, Status);
711                 if (Status == AE_CTRL_PENDING)
712                 {
713                     Status = AE_OK;
714                     goto CloseThisOp;
715                 }
716
717                 if (ACPI_FAILURE (Status))
718                 {
719                     goto CloseThisOp;
720                 }
721
722                 AcpiPsAppendArg (Op, PreOp.Common.Value.Arg);
723                 AcpiGbl_Depth++;
724
725                 if (Op->Common.AmlOpcode == AML_REGION_OP)
726                 {
727                     /*
728                      * Defer final parsing of an OperationRegion body,
729                      * because we don't have enough info in the first pass
730                      * to parse it correctly (i.e., there may be method
731                      * calls within the TermArg elements of the body.)
732                      *
733                      * However, we must continue parsing because
734                      * the opregion is not a standalone package --
735                      * we don't know where the end is at this point.
736                      *
737                      * (Length is unknown until parse of the body complete)
738                      */
739                     Op->Named.Data    = AmlOpStart;
740                     Op->Named.Length  = 0;
741                 }
742             }
743             else
744             {
745                 /* Not a named opcode, just allocate Op and append to parent */
746
747                 WalkState->OpInfo = AcpiPsGetOpcodeInfo (WalkState->Opcode);
748                 Op = AcpiPsAllocOp (WalkState->Opcode);
749                 if (!Op)
750                 {
751                     return_ACPI_STATUS (AE_NO_MEMORY);
752                 }
753
754                 if (WalkState->OpInfo->Flags & AML_CREATE)
755                 {
756                     /*
757                      * Backup to beginning of CreateXXXfield declaration
758                      * BodyLength is unknown until we parse the body
759                      */
760                     Op->Named.Data    = AmlOpStart;
761                     Op->Named.Length  = 0;
762                 }
763
764                 AcpiPsAppendArg (AcpiPsGetParentScope (ParserState), Op);
765
766                 if ((WalkState->DescendingCallback != NULL))
767                 {
768                     /*
769                      * Find the object.  This will either insert the object into
770                      * the namespace or simply look it up
771                      */
772                     WalkState->Op = Op;
773
774                     Status = WalkState->DescendingCallback (WalkState, &Op);
775                     Status = AcpiPsNextParseState (WalkState, Op, Status);
776                     if (Status == AE_CTRL_PENDING)
777                     {
778                         Status = AE_OK;
779                         goto CloseThisOp;
780                     }
781
782                     if (ACPI_FAILURE (Status))
783                     {
784                         goto CloseThisOp;
785                     }
786                 }
787             }
788
789             Op->Common.AmlOffset = WalkState->AmlOffset;
790
791             if (WalkState->OpInfo)
792             {
793                 ACPI_DEBUG_PRINT ((ACPI_DB_PARSE,
794                     "Opcode %4.4X [%s] Op %p Aml %p AmlOffset %5.5X\n",
795                      (UINT32) Op->Common.AmlOpcode, WalkState->OpInfo->Name,
796                      Op, ParserState->Aml, Op->Common.AmlOffset));
797             }
798         }
799
800
801         /* Start ArgCount at zero because we don't know if there are any args yet */
802
803         WalkState->ArgCount  = 0;
804
805         if (WalkState->ArgTypes)   /* Are there any arguments that must be processed? */
806         {
807             /* Get arguments */
808
809             switch (Op->Common.AmlOpcode)
810             {
811             case AML_BYTE_OP:       /* AML_BYTEDATA_ARG */
812             case AML_WORD_OP:       /* AML_WORDDATA_ARG */
813             case AML_DWORD_OP:      /* AML_DWORDATA_ARG */
814             case AML_QWORD_OP:      /* AML_QWORDATA_ARG */
815             case AML_STRING_OP:     /* AML_ASCIICHARLIST_ARG */
816
817                 /* Fill in constant or string argument directly */
818
819                 AcpiPsGetNextSimpleArg (ParserState,
820                     GET_CURRENT_ARG_TYPE (WalkState->ArgTypes), Op);
821                 break;
822
823             case AML_INT_NAMEPATH_OP:   /* AML_NAMESTRING_ARG */
824
825                 Status = AcpiPsGetNextNamepath (WalkState, ParserState, Op, 1);
826                 if (ACPI_FAILURE (Status))
827                 {
828                     goto CloseThisOp;
829                 }
830
831                 WalkState->ArgTypes = 0;
832                 break;
833
834
835             default:
836
837                 /* Op is not a constant or string, append each argument */
838
839                 while (GET_CURRENT_ARG_TYPE (WalkState->ArgTypes) &&
840                         !WalkState->ArgCount)
841                 {
842                     WalkState->AmlOffset = ACPI_PTR_DIFF (ParserState->Aml,
843                                                           ParserState->AmlStart);
844                     Status = AcpiPsGetNextArg (WalkState, ParserState,
845                                 GET_CURRENT_ARG_TYPE (WalkState->ArgTypes), &Arg);
846                     if (ACPI_FAILURE (Status))
847                     {
848                         goto CloseThisOp;
849                     }
850
851                     if (Arg)
852                     {
853                         Arg->Common.AmlOffset = WalkState->AmlOffset;
854                         AcpiPsAppendArg (Op, Arg);
855                     }
856                     INCREMENT_ARG_LIST (WalkState->ArgTypes);
857                 }
858
859                 switch (Op->Common.AmlOpcode)
860                 {
861                 case AML_METHOD_OP:
862
863                     /* For a method, save the length and address of the body */
864
865                     /*
866                      * Skip parsing of control method or opregion body,
867                      * because we don't have enough info in the first pass
868                      * to parse them correctly.
869                      */
870                     Op->Named.Data   = ParserState->Aml;
871                     Op->Named.Length = (UINT32) (ParserState->PkgEnd - ParserState->Aml);
872                     /*
873                      * Skip body of method.  For OpRegions, we must continue
874                      * parsing because the opregion is not a standalone
875                      * package (We don't know where the end is).
876                      */
877                     ParserState->Aml    = ParserState->PkgEnd;
878                     WalkState->ArgCount = 0;
879                     break;
880
881                 case AML_BUFFER_OP:
882                 case AML_PACKAGE_OP:
883                 case AML_VAR_PACKAGE_OP:
884
885                     if ((Op->Common.Parent) &&
886                         (Op->Common.Parent->Common.AmlOpcode == AML_NAME_OP) &&
887                         (WalkState->DescendingCallback != AcpiDsExecBeginOp))
888                     {
889                         /*
890                          * Skip parsing of
891                          * because we don't have enough info in the first pass
892                          * to parse them correctly.
893                          */
894                         Op->Named.Data   = AmlOpStart;
895                         Op->Named.Length = (UINT32) (ParserState->PkgEnd - AmlOpStart);
896                         /*
897                          * Skip body
898                          */
899                         ParserState->Aml    = ParserState->PkgEnd;
900                         WalkState->ArgCount = 0;
901                     }
902                     break;
903
904                 case AML_WHILE_OP:
905
906                     if (WalkState->ControlState)
907                     {
908                         WalkState->ControlState->Control.PackageEnd = ParserState->PkgEnd;
909                     }
910                     break;
911
912                 default:
913                     /* No action for all other opcodes */
914                     break;
915                 }
916                 break;
917             }
918         }
919
920         /* Check for arguments that need to be processed */
921
922         if (WalkState->ArgCount)
923         {
924             /* There are arguments (complex ones), push Op and prepare for argument */
925
926             Status = AcpiPsPushScope (ParserState, Op,
927                         WalkState->ArgTypes, WalkState->ArgCount);
928             if (ACPI_FAILURE (Status))
929             {
930                 return_ACPI_STATUS (Status);
931             }
932             Op = NULL;
933             continue;
934         }
935
936         /* All arguments have been processed -- Op is complete, prepare for next */
937
938         WalkState->OpInfo = AcpiPsGetOpcodeInfo (Op->Common.AmlOpcode);
939         if (WalkState->OpInfo->Flags & AML_NAMED)
940         {
941             if (AcpiGbl_Depth)
942             {
943                 AcpiGbl_Depth--;
944             }
945
946             if (Op->Common.AmlOpcode == AML_REGION_OP)
947             {
948                 /*
949                  * Skip parsing of control method or opregion body,
950                  * because we don't have enough info in the first pass
951                  * to parse them correctly.
952                  *
953                  * Completed parsing an OpRegion declaration, we now
954                  * know the length.
955                  */
956                 Op->Named.Length = (UINT32) (ParserState->Aml - Op->Named.Data);
957             }
958         }
959
960         if (WalkState->OpInfo->Flags & AML_CREATE)
961         {
962             /*
963              * Backup to beginning of CreateXXXfield declaration (1 for
964              * Opcode)
965              *
966              * BodyLength is unknown until we parse the body
967              */
968             Op->Named.Length = (UINT32) (ParserState->Aml - Op->Named.Data);
969         }
970
971         /* This op complete, notify the dispatcher */
972
973         if (WalkState->AscendingCallback != NULL)
974         {
975             WalkState->Op     = Op;
976             WalkState->Opcode = Op->Common.AmlOpcode;
977
978             Status = WalkState->AscendingCallback (WalkState);
979             Status = AcpiPsNextParseState (WalkState, Op, Status);
980             if (Status == AE_CTRL_PENDING)
981             {
982                 Status = AE_OK;
983                 goto CloseThisOp;
984             }
985         }
986
987
988 CloseThisOp:
989         /*
990          * Finished one argument of the containing scope
991          */
992         ParserState->Scope->ParseScope.ArgCount--;
993
994         /* Close this Op (will result in parse subtree deletion) */
995
996         AcpiPsCompleteThisOp (WalkState, Op);
997         Op = NULL;
998
999         switch (Status)
1000         {
1001         case AE_OK:
1002             break;
1003
1004
1005         case AE_CTRL_TRANSFER:
1006
1007             /*
1008              * We are about to transfer to a called method.
1009              */
1010             WalkState->PrevOp = Op;
1011             WalkState->PrevArgTypes = WalkState->ArgTypes;
1012             return_ACPI_STATUS (Status);
1013
1014
1015         case AE_CTRL_END:
1016
1017             AcpiPsPopScope (ParserState, &Op,
1018                 &WalkState->ArgTypes, &WalkState->ArgCount);
1019
1020             if (Op)
1021             {
1022                 WalkState->Op     = Op;
1023                 WalkState->OpInfo = AcpiPsGetOpcodeInfo (Op->Common.AmlOpcode);
1024                 WalkState->Opcode = Op->Common.AmlOpcode;
1025
1026                 Status = WalkState->AscendingCallback (WalkState);
1027                 Status = AcpiPsNextParseState (WalkState, Op, Status);
1028
1029                 AcpiPsCompleteThisOp (WalkState, Op);
1030                 Op = NULL;
1031             }
1032             Status = AE_OK;
1033             break;
1034
1035
1036         case AE_CTRL_BREAK:
1037         case AE_CTRL_CONTINUE:
1038
1039             /* Pop off scopes until we find the While */
1040
1041             while (!Op || (Op->Common.AmlOpcode != AML_WHILE_OP))
1042             {
1043                 AcpiPsPopScope (ParserState, &Op,
1044                     &WalkState->ArgTypes, &WalkState->ArgCount);
1045             }
1046
1047             /* Close this iteration of the While loop */
1048
1049             WalkState->Op     = Op;
1050             WalkState->OpInfo = AcpiPsGetOpcodeInfo (Op->Common.AmlOpcode);
1051             WalkState->Opcode = Op->Common.AmlOpcode;
1052
1053             Status = WalkState->AscendingCallback (WalkState);
1054             Status = AcpiPsNextParseState (WalkState, Op, Status);
1055
1056             AcpiPsCompleteThisOp (WalkState, Op);
1057             Op = NULL;
1058
1059             Status = AE_OK;
1060             break;
1061
1062
1063         case AE_CTRL_TERMINATE:
1064
1065             Status = AE_OK;
1066
1067             /* Clean up */
1068             do
1069             {
1070                 if (Op)
1071                 {
1072                     AcpiPsCompleteThisOp (WalkState, Op);
1073                 }
1074                 AcpiPsPopScope (ParserState, &Op,
1075                     &WalkState->ArgTypes, &WalkState->ArgCount);
1076
1077             } while (Op);
1078
1079             return_ACPI_STATUS (Status);
1080
1081
1082         default:  /* All other non-AE_OK status */
1083
1084             do
1085             {
1086                 if (Op)
1087                 {
1088                     AcpiPsCompleteThisOp (WalkState, Op);
1089                 }
1090                 AcpiPsPopScope (ParserState, &Op,
1091                     &WalkState->ArgTypes, &WalkState->ArgCount);
1092
1093             } while (Op);
1094
1095
1096             /*
1097              * TBD: Cleanup parse ops on error
1098              */
1099 #if 0
1100             if (Op == NULL)
1101             {
1102                 AcpiPsPopScope (ParserState, &Op,
1103                     &WalkState->ArgTypes, &WalkState->ArgCount);
1104             }
1105 #endif
1106             WalkState->PrevOp = Op;
1107             WalkState->PrevArgTypes = WalkState->ArgTypes;
1108             return_ACPI_STATUS (Status);
1109         }
1110
1111         /* This scope complete? */
1112
1113         if (AcpiPsHasCompletedScope (ParserState))
1114         {
1115             AcpiPsPopScope (ParserState, &Op,
1116                 &WalkState->ArgTypes, &WalkState->ArgCount);
1117             ACPI_DEBUG_PRINT ((ACPI_DB_PARSE, "Popped scope, Op=%p\n", Op));
1118         }
1119         else
1120         {
1121             Op = NULL;
1122         }
1123
1124     } /* while ParserState->Aml */
1125
1126
1127     /*
1128      * Complete the last Op (if not completed), and clear the scope stack.
1129      * It is easily possible to end an AML "package" with an unbounded number
1130      * of open scopes (such as when several ASL blocks are closed with
1131      * sequential closing braces).  We want to terminate each one cleanly.
1132      */
1133     ACPI_DEBUG_PRINT ((ACPI_DB_PARSE, "AML package complete at Op %p\n", Op));
1134     do
1135     {
1136         if (Op)
1137         {
1138             if (WalkState->AscendingCallback != NULL)
1139             {
1140                 WalkState->Op     = Op;
1141                 WalkState->OpInfo = AcpiPsGetOpcodeInfo (Op->Common.AmlOpcode);
1142                 WalkState->Opcode = Op->Common.AmlOpcode;
1143
1144                 Status = WalkState->AscendingCallback (WalkState);
1145                 Status = AcpiPsNextParseState (WalkState, Op, Status);
1146                 if (Status == AE_CTRL_PENDING)
1147                 {
1148                     Status = AE_OK;
1149                     goto CloseThisOp;
1150                 }
1151
1152                 if (Status == AE_CTRL_TERMINATE)
1153                 {
1154                     Status = AE_OK;
1155
1156                     /* Clean up */
1157                     do
1158                     {
1159                         if (Op)
1160                         {
1161                             AcpiPsCompleteThisOp (WalkState, Op);
1162                         }
1163
1164                         AcpiPsPopScope (ParserState, &Op,
1165                             &WalkState->ArgTypes, &WalkState->ArgCount);
1166
1167                     } while (Op);
1168
1169                     return_ACPI_STATUS (Status);
1170                 }
1171
1172                 else if (ACPI_FAILURE (Status))
1173                 {
1174                     AcpiPsCompleteThisOp (WalkState, Op);
1175                     return_ACPI_STATUS (Status);
1176                 }
1177             }
1178
1179             AcpiPsCompleteThisOp (WalkState, Op);
1180         }
1181
1182         AcpiPsPopScope (ParserState, &Op, &WalkState->ArgTypes,
1183             &WalkState->ArgCount);
1184
1185     } while (Op);
1186
1187     return_ACPI_STATUS (Status);
1188 }
1189
1190
1191 /*******************************************************************************
1192  *
1193  * FUNCTION:    AcpiPsParseAml
1194  *
1195  * PARAMETERS:  StartScope      - The starting point of the parse.  Becomes the
1196  *                                root of the parsed op tree.
1197  *              Aml             - Pointer to the raw AML code to parse
1198  *              AmlSize         - Length of the AML to parse
1199  *
1200  *
1201  * RETURN:      Status
1202  *
1203  * DESCRIPTION: Parse raw AML and return a tree of ops
1204  *
1205  ******************************************************************************/
1206
1207 ACPI_STATUS
1208 AcpiPsParseAml (
1209     ACPI_WALK_STATE         *WalkState)
1210 {
1211     ACPI_STATUS             Status;
1212     ACPI_STATUS             TerminateStatus;
1213     ACPI_THREAD_STATE       *Thread;
1214     ACPI_THREAD_STATE       *PrevWalkList = AcpiGbl_CurrentWalkList;
1215     ACPI_WALK_STATE         *PreviousWalkState;
1216     ACPI_OPERAND_OBJECT     **CallerReturnDesc = WalkState->CallerReturnDesc;
1217     ACPI_OPERAND_OBJECT     *EffectiveReturnDesc = NULL;
1218
1219
1220
1221     ACPI_FUNCTION_TRACE ("PsParseAml");
1222
1223     ACPI_DEBUG_PRINT ((ACPI_DB_PARSE, "Entered with WalkState=%p Aml=%p size=%X\n",
1224         WalkState, WalkState->ParserState.Aml, WalkState->ParserState.AmlSize));
1225
1226
1227     /* Create and initialize a new thread state */
1228
1229     Thread = AcpiUtCreateThreadState ();
1230     if (!Thread)
1231     {
1232         return_ACPI_STATUS (AE_NO_MEMORY);
1233     }
1234
1235     WalkState->Thread = Thread;
1236     AcpiDsPushWalkState (WalkState, Thread);
1237
1238     /*
1239      * This global allows the AML debugger to get a handle to the currently
1240      * executing control method.
1241      */
1242     AcpiGbl_CurrentWalkList = Thread;
1243
1244     /*
1245      * Execute the walk loop as long as there is a valid Walk State.  This
1246      * handles nested control method invocations without recursion.
1247      */
1248     ACPI_DEBUG_PRINT ((ACPI_DB_PARSE, "State=%p\n", WalkState));
1249
1250     Status = AE_OK;
1251     while (WalkState)
1252     {
1253         if (ACPI_SUCCESS (Status))
1254         {
1255             /*
1256              * The ParseLoop executes AML until the method terminates
1257              * or calls another method.
1258              */
1259             Status = AcpiPsParseLoop (WalkState);
1260         }
1261
1262         ACPI_DEBUG_PRINT ((ACPI_DB_PARSE,
1263             "Completed one call to walk loop, %s State=%p\n",
1264             AcpiFormatException (Status), WalkState));
1265
1266         if (Status == AE_CTRL_TRANSFER)
1267         {
1268             /*
1269              * A method call was detected.
1270              * Transfer control to the called control method
1271              */
1272             Status = AcpiDsCallControlMethod (Thread, WalkState, NULL);
1273
1274             /*
1275              * If the transfer to the new method method call worked, a new walk
1276              * state was created -- get it
1277              */
1278             WalkState = AcpiDsGetCurrentWalkState (Thread);
1279             continue;
1280         }
1281         else if (Status == AE_CTRL_TERMINATE)
1282         {
1283             Status = AE_OK;
1284         }
1285         else if (Status != AE_OK)
1286         {
1287             ACPI_REPORT_METHOD_ERROR ("Method execution failed",
1288                 WalkState->MethodNode, NULL, Status);
1289         }
1290
1291         /* We are done with this walk, move on to the parent if any */
1292
1293         WalkState = AcpiDsPopWalkState (Thread);
1294         /* Save the last effective return value */
1295
1296         if (CallerReturnDesc && WalkState->ReturnDesc)
1297         {
1298             AcpiUtRemoveReference (EffectiveReturnDesc);
1299             EffectiveReturnDesc = WalkState->ReturnDesc;
1300             AcpiUtAddReference (EffectiveReturnDesc);
1301         }
1302
1303         /* Reset the current scope to the beginning of scope stack */
1304
1305         AcpiDsScopeStackClear (WalkState);
1306
1307         /*
1308          * If we just returned from the execution of a control method,
1309          * there's lots of cleanup to do
1310          */
1311         if ((WalkState->ParseFlags & ACPI_PARSE_MODE_MASK) == ACPI_PARSE_EXECUTE)
1312         {
1313             TerminateStatus = AcpiDsTerminateControlMethod (WalkState);
1314             if (ACPI_FAILURE (TerminateStatus))
1315             {
1316                 ACPI_REPORT_ERROR ((
1317                     "Could not terminate control method properly\n"));
1318
1319                 /* Ignore error and continue */
1320             }
1321         }
1322
1323         /* Delete this walk state and all linked control states */
1324
1325         AcpiPsCleanupScope (&WalkState->ParserState);
1326
1327         PreviousWalkState = WalkState;
1328
1329         ACPI_DEBUG_PRINT ((ACPI_DB_PARSE, "ReturnValue=%p, State=%p\n",
1330             WalkState->ReturnDesc, WalkState));
1331
1332         /* Check if we have restarted a preempted walk */
1333
1334         WalkState = AcpiDsGetCurrentWalkState (Thread);
1335         if (WalkState)
1336         {
1337             if (ACPI_SUCCESS (Status))
1338             {
1339                 /*
1340                  * There is another walk state, restart it.
1341                  * If the method return value is not used by the parent,
1342                  * The object is deleted
1343                  */
1344                 Status = AcpiDsRestartControlMethod (WalkState,
1345                             PreviousWalkState->ReturnDesc);
1346                 if (ACPI_SUCCESS (Status))
1347                 {
1348                     WalkState->WalkType |= ACPI_WALK_METHOD_RESTART;
1349                 }
1350             }
1351             else
1352             {
1353                 /* On error, delete any return object */
1354
1355                 AcpiUtRemoveReference (PreviousWalkState->ReturnDesc);
1356             }
1357         }
1358
1359         /*
1360          * Just completed a 1st-level method, save the final internal return
1361          * value (if any)
1362          */
1363         else if (PreviousWalkState->CallerReturnDesc)
1364         {
1365             /*
1366              * Some AML code expects return value w/o ReturnOp.
1367              * Return the saved effective return value instead.
1368              */
1369
1370             if (PreviousWalkState->ReturnDesc == NULL && EffectiveReturnDesc != NULL)
1371             {
1372                 PreviousWalkState->ReturnDesc = EffectiveReturnDesc;
1373                 AcpiUtAddReference (PreviousWalkState->ReturnDesc);
1374             }
1375
1376             *(PreviousWalkState->CallerReturnDesc) = PreviousWalkState->ReturnDesc; /* NULL if no return value */
1377         }
1378         else if (PreviousWalkState->ReturnDesc)
1379         {
1380             /* Caller doesn't want it, must delete it */
1381
1382             AcpiUtRemoveReference (PreviousWalkState->ReturnDesc);
1383         }
1384
1385         AcpiDsDeleteWalkState (PreviousWalkState);
1386     }
1387
1388     /* Normal exit */
1389
1390     AcpiUtRemoveReference (EffectiveReturnDesc);
1391     AcpiExReleaseAllMutexes (Thread);
1392     AcpiUtDeleteGenericState (ACPI_CAST_PTR (ACPI_GENERIC_STATE, Thread));
1393     AcpiGbl_CurrentWalkList = PrevWalkList;
1394     return_ACPI_STATUS (Status);
1395 }
1396
1397