xref: /minix3/minix/drivers/power/acpi/parser/psparse.c (revision 29492bb71c7148a089a5afafa0c99409161218df)
1433d6423SLionel Sambuc /******************************************************************************
2433d6423SLionel Sambuc  *
3433d6423SLionel Sambuc  * Module Name: psparse - Parser top level AML parse routines
4433d6423SLionel Sambuc  *
5433d6423SLionel Sambuc  *****************************************************************************/
6433d6423SLionel Sambuc 
7*29492bb7SDavid van Moolenbroek /*
8*29492bb7SDavid van Moolenbroek  * Copyright (C) 2000 - 2014, Intel Corp.
9433d6423SLionel Sambuc  * All rights reserved.
10433d6423SLionel Sambuc  *
11*29492bb7SDavid van Moolenbroek  * Redistribution and use in source and binary forms, with or without
12*29492bb7SDavid van Moolenbroek  * modification, are permitted provided that the following conditions
13*29492bb7SDavid van Moolenbroek  * are met:
14*29492bb7SDavid van Moolenbroek  * 1. Redistributions of source code must retain the above copyright
15*29492bb7SDavid van Moolenbroek  *    notice, this list of conditions, and the following disclaimer,
16*29492bb7SDavid van Moolenbroek  *    without modification.
17*29492bb7SDavid van Moolenbroek  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
18*29492bb7SDavid van Moolenbroek  *    substantially similar to the "NO WARRANTY" disclaimer below
19*29492bb7SDavid van Moolenbroek  *    ("Disclaimer") and any redistribution must be conditioned upon
20*29492bb7SDavid van Moolenbroek  *    including a substantially similar Disclaimer requirement for further
21*29492bb7SDavid van Moolenbroek  *    binary redistribution.
22*29492bb7SDavid van Moolenbroek  * 3. Neither the names of the above-listed copyright holders nor the names
23*29492bb7SDavid van Moolenbroek  *    of any contributors may be used to endorse or promote products derived
24*29492bb7SDavid van Moolenbroek  *    from this software without specific prior written permission.
25433d6423SLionel Sambuc  *
26*29492bb7SDavid van Moolenbroek  * Alternatively, this software may be distributed under the terms of the
27*29492bb7SDavid van Moolenbroek  * GNU General Public License ("GPL") version 2 as published by the Free
28*29492bb7SDavid van Moolenbroek  * Software Foundation.
29433d6423SLionel Sambuc  *
30*29492bb7SDavid van Moolenbroek  * NO WARRANTY
31*29492bb7SDavid van Moolenbroek  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32*29492bb7SDavid van Moolenbroek  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33*29492bb7SDavid van Moolenbroek  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
34*29492bb7SDavid van Moolenbroek  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35*29492bb7SDavid van Moolenbroek  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36*29492bb7SDavid van Moolenbroek  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37*29492bb7SDavid van Moolenbroek  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38*29492bb7SDavid van Moolenbroek  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39*29492bb7SDavid van Moolenbroek  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
40*29492bb7SDavid van Moolenbroek  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41*29492bb7SDavid van Moolenbroek  * POSSIBILITY OF SUCH DAMAGES.
42*29492bb7SDavid van Moolenbroek  */
43433d6423SLionel Sambuc 
44433d6423SLionel Sambuc /*
45433d6423SLionel Sambuc  * Parse the AML and build an operation tree as most interpreters,
46433d6423SLionel Sambuc  * like Perl, do. Parsing is done by hand rather than with a YACC
47433d6423SLionel Sambuc  * generated parser to tightly constrain stack and dynamic memory
48433d6423SLionel Sambuc  * usage. At the same time, parsing is kept flexible and the code
49433d6423SLionel Sambuc  * fairly compact by parsing based on a list of AML opcode
50433d6423SLionel Sambuc  * templates in AmlOpInfo[]
51433d6423SLionel Sambuc  */
52433d6423SLionel Sambuc 
53433d6423SLionel Sambuc #include "acpi.h"
54433d6423SLionel Sambuc #include "accommon.h"
55433d6423SLionel Sambuc #include "acparser.h"
56433d6423SLionel Sambuc #include "acdispat.h"
57433d6423SLionel Sambuc #include "amlcode.h"
58433d6423SLionel Sambuc #include "acinterp.h"
59433d6423SLionel Sambuc 
60433d6423SLionel Sambuc #define _COMPONENT          ACPI_PARSER
61433d6423SLionel Sambuc         ACPI_MODULE_NAME    ("psparse")
62433d6423SLionel Sambuc 
63433d6423SLionel Sambuc 
64433d6423SLionel Sambuc /*******************************************************************************
65433d6423SLionel Sambuc  *
66433d6423SLionel Sambuc  * FUNCTION:    AcpiPsGetOpcodeSize
67433d6423SLionel Sambuc  *
68433d6423SLionel Sambuc  * PARAMETERS:  Opcode          - An AML opcode
69433d6423SLionel Sambuc  *
70433d6423SLionel Sambuc  * RETURN:      Size of the opcode, in bytes (1 or 2)
71433d6423SLionel Sambuc  *
72433d6423SLionel Sambuc  * DESCRIPTION: Get the size of the current opcode.
73433d6423SLionel Sambuc  *
74433d6423SLionel Sambuc  ******************************************************************************/
75433d6423SLionel Sambuc 
76433d6423SLionel Sambuc UINT32
AcpiPsGetOpcodeSize(UINT32 Opcode)77433d6423SLionel Sambuc AcpiPsGetOpcodeSize (
78433d6423SLionel Sambuc     UINT32                  Opcode)
79433d6423SLionel Sambuc {
80433d6423SLionel Sambuc 
81433d6423SLionel Sambuc     /* Extended (2-byte) opcode if > 255 */
82433d6423SLionel Sambuc 
83433d6423SLionel Sambuc     if (Opcode > 0x00FF)
84433d6423SLionel Sambuc     {
85433d6423SLionel Sambuc         return (2);
86433d6423SLionel Sambuc     }
87433d6423SLionel Sambuc 
88433d6423SLionel Sambuc     /* Otherwise, just a single byte opcode */
89433d6423SLionel Sambuc 
90433d6423SLionel Sambuc     return (1);
91433d6423SLionel Sambuc }
92433d6423SLionel Sambuc 
93433d6423SLionel Sambuc 
94433d6423SLionel Sambuc /*******************************************************************************
95433d6423SLionel Sambuc  *
96433d6423SLionel Sambuc  * FUNCTION:    AcpiPsPeekOpcode
97433d6423SLionel Sambuc  *
98433d6423SLionel Sambuc  * PARAMETERS:  ParserState         - A parser state object
99433d6423SLionel Sambuc  *
100433d6423SLionel Sambuc  * RETURN:      Next AML opcode
101433d6423SLionel Sambuc  *
102433d6423SLionel Sambuc  * DESCRIPTION: Get next AML opcode (without incrementing AML pointer)
103433d6423SLionel Sambuc  *
104433d6423SLionel Sambuc  ******************************************************************************/
105433d6423SLionel Sambuc 
106433d6423SLionel Sambuc UINT16
AcpiPsPeekOpcode(ACPI_PARSE_STATE * ParserState)107433d6423SLionel Sambuc AcpiPsPeekOpcode (
108433d6423SLionel Sambuc     ACPI_PARSE_STATE        *ParserState)
109433d6423SLionel Sambuc {
110433d6423SLionel Sambuc     UINT8                   *Aml;
111433d6423SLionel Sambuc     UINT16                  Opcode;
112433d6423SLionel Sambuc 
113433d6423SLionel Sambuc 
114433d6423SLionel Sambuc     Aml = ParserState->Aml;
115433d6423SLionel Sambuc     Opcode = (UINT16) ACPI_GET8 (Aml);
116433d6423SLionel Sambuc 
117433d6423SLionel Sambuc     if (Opcode == AML_EXTENDED_OP_PREFIX)
118433d6423SLionel Sambuc     {
119433d6423SLionel Sambuc         /* Extended opcode, get the second opcode byte */
120433d6423SLionel Sambuc 
121433d6423SLionel Sambuc         Aml++;
122433d6423SLionel Sambuc         Opcode = (UINT16) ((Opcode << 8) | ACPI_GET8 (Aml));
123433d6423SLionel Sambuc     }
124433d6423SLionel Sambuc 
125433d6423SLionel Sambuc     return (Opcode);
126433d6423SLionel Sambuc }
127433d6423SLionel Sambuc 
128433d6423SLionel Sambuc 
129433d6423SLionel Sambuc /*******************************************************************************
130433d6423SLionel Sambuc  *
131433d6423SLionel Sambuc  * FUNCTION:    AcpiPsCompleteThisOp
132433d6423SLionel Sambuc  *
133433d6423SLionel Sambuc  * PARAMETERS:  WalkState       - Current State
134433d6423SLionel Sambuc  *              Op              - Op to complete
135433d6423SLionel Sambuc  *
136433d6423SLionel Sambuc  * RETURN:      Status
137433d6423SLionel Sambuc  *
138433d6423SLionel Sambuc  * DESCRIPTION: Perform any cleanup at the completion of an Op.
139433d6423SLionel Sambuc  *
140433d6423SLionel Sambuc  ******************************************************************************/
141433d6423SLionel Sambuc 
142433d6423SLionel Sambuc ACPI_STATUS
AcpiPsCompleteThisOp(ACPI_WALK_STATE * WalkState,ACPI_PARSE_OBJECT * Op)143433d6423SLionel Sambuc AcpiPsCompleteThisOp (
144433d6423SLionel Sambuc     ACPI_WALK_STATE         *WalkState,
145433d6423SLionel Sambuc     ACPI_PARSE_OBJECT       *Op)
146433d6423SLionel Sambuc {
147433d6423SLionel Sambuc     ACPI_PARSE_OBJECT       *Prev;
148433d6423SLionel Sambuc     ACPI_PARSE_OBJECT       *Next;
149433d6423SLionel Sambuc     const ACPI_OPCODE_INFO  *ParentInfo;
150433d6423SLionel Sambuc     ACPI_PARSE_OBJECT       *ReplacementOp = NULL;
151433d6423SLionel Sambuc     ACPI_STATUS             Status = AE_OK;
152433d6423SLionel Sambuc 
153433d6423SLionel Sambuc 
154433d6423SLionel Sambuc     ACPI_FUNCTION_TRACE_PTR (PsCompleteThisOp, Op);
155433d6423SLionel Sambuc 
156433d6423SLionel Sambuc 
157433d6423SLionel Sambuc     /* Check for null Op, can happen if AML code is corrupt */
158433d6423SLionel Sambuc 
159433d6423SLionel Sambuc     if (!Op)
160433d6423SLionel Sambuc     {
161433d6423SLionel Sambuc         return_ACPI_STATUS (AE_OK);  /* OK for now */
162433d6423SLionel Sambuc     }
163433d6423SLionel Sambuc 
164433d6423SLionel Sambuc     /* Delete this op and the subtree below it if asked to */
165433d6423SLionel Sambuc 
166433d6423SLionel Sambuc     if (((WalkState->ParseFlags & ACPI_PARSE_TREE_MASK) != ACPI_PARSE_DELETE_TREE) ||
167433d6423SLionel Sambuc          (WalkState->OpInfo->Class == AML_CLASS_ARGUMENT))
168433d6423SLionel Sambuc     {
169433d6423SLionel Sambuc         return_ACPI_STATUS (AE_OK);
170433d6423SLionel Sambuc     }
171433d6423SLionel Sambuc 
172433d6423SLionel Sambuc     /* Make sure that we only delete this subtree */
173433d6423SLionel Sambuc 
174433d6423SLionel Sambuc     if (Op->Common.Parent)
175433d6423SLionel Sambuc     {
176433d6423SLionel Sambuc         Prev = Op->Common.Parent->Common.Value.Arg;
177433d6423SLionel Sambuc         if (!Prev)
178433d6423SLionel Sambuc         {
179433d6423SLionel Sambuc             /* Nothing more to do */
180433d6423SLionel Sambuc 
181433d6423SLionel Sambuc             goto Cleanup;
182433d6423SLionel Sambuc         }
183433d6423SLionel Sambuc 
184433d6423SLionel Sambuc         /*
185433d6423SLionel Sambuc          * Check if we need to replace the operator and its subtree
186433d6423SLionel Sambuc          * with a return value op (placeholder op)
187433d6423SLionel Sambuc          */
188433d6423SLionel Sambuc         ParentInfo = AcpiPsGetOpcodeInfo (Op->Common.Parent->Common.AmlOpcode);
189433d6423SLionel Sambuc 
190433d6423SLionel Sambuc         switch (ParentInfo->Class)
191433d6423SLionel Sambuc         {
192433d6423SLionel Sambuc         case AML_CLASS_CONTROL:
193*29492bb7SDavid van Moolenbroek 
194433d6423SLionel Sambuc             break;
195433d6423SLionel Sambuc 
196433d6423SLionel Sambuc         case AML_CLASS_CREATE:
197433d6423SLionel Sambuc             /*
198433d6423SLionel Sambuc              * These opcodes contain TermArg operands. The current
199433d6423SLionel Sambuc              * op must be replaced by a placeholder return op
200433d6423SLionel Sambuc              */
201433d6423SLionel Sambuc             ReplacementOp = AcpiPsAllocOp (AML_INT_RETURN_VALUE_OP);
202433d6423SLionel Sambuc             if (!ReplacementOp)
203433d6423SLionel Sambuc             {
204433d6423SLionel Sambuc                 Status = AE_NO_MEMORY;
205433d6423SLionel Sambuc             }
206433d6423SLionel Sambuc             break;
207433d6423SLionel Sambuc 
208433d6423SLionel Sambuc         case AML_CLASS_NAMED_OBJECT:
209433d6423SLionel Sambuc             /*
210433d6423SLionel Sambuc              * These opcodes contain TermArg operands. The current
211433d6423SLionel Sambuc              * op must be replaced by a placeholder return op
212433d6423SLionel Sambuc              */
213433d6423SLionel Sambuc             if ((Op->Common.Parent->Common.AmlOpcode == AML_REGION_OP)       ||
214433d6423SLionel Sambuc                 (Op->Common.Parent->Common.AmlOpcode == AML_DATA_REGION_OP)  ||
215433d6423SLionel Sambuc                 (Op->Common.Parent->Common.AmlOpcode == AML_BUFFER_OP)       ||
216433d6423SLionel Sambuc                 (Op->Common.Parent->Common.AmlOpcode == AML_PACKAGE_OP)      ||
217433d6423SLionel Sambuc                 (Op->Common.Parent->Common.AmlOpcode == AML_BANK_FIELD_OP)   ||
218433d6423SLionel Sambuc                 (Op->Common.Parent->Common.AmlOpcode == AML_VAR_PACKAGE_OP))
219433d6423SLionel Sambuc             {
220433d6423SLionel Sambuc                 ReplacementOp = AcpiPsAllocOp (AML_INT_RETURN_VALUE_OP);
221433d6423SLionel Sambuc                 if (!ReplacementOp)
222433d6423SLionel Sambuc                 {
223433d6423SLionel Sambuc                     Status = AE_NO_MEMORY;
224433d6423SLionel Sambuc                 }
225433d6423SLionel Sambuc             }
226433d6423SLionel Sambuc             else if ((Op->Common.Parent->Common.AmlOpcode == AML_NAME_OP) &&
227433d6423SLionel Sambuc                      (WalkState->PassNumber <= ACPI_IMODE_LOAD_PASS2))
228433d6423SLionel Sambuc             {
229433d6423SLionel Sambuc                 if ((Op->Common.AmlOpcode == AML_BUFFER_OP) ||
230433d6423SLionel Sambuc                     (Op->Common.AmlOpcode == AML_PACKAGE_OP) ||
231433d6423SLionel Sambuc                     (Op->Common.AmlOpcode == AML_VAR_PACKAGE_OP))
232433d6423SLionel Sambuc                 {
233433d6423SLionel Sambuc                     ReplacementOp = AcpiPsAllocOp (Op->Common.AmlOpcode);
234433d6423SLionel Sambuc                     if (!ReplacementOp)
235433d6423SLionel Sambuc                     {
236433d6423SLionel Sambuc                         Status = AE_NO_MEMORY;
237433d6423SLionel Sambuc                     }
238433d6423SLionel Sambuc                     else
239433d6423SLionel Sambuc                     {
240433d6423SLionel Sambuc                         ReplacementOp->Named.Data = Op->Named.Data;
241433d6423SLionel Sambuc                         ReplacementOp->Named.Length = Op->Named.Length;
242433d6423SLionel Sambuc                     }
243433d6423SLionel Sambuc                 }
244433d6423SLionel Sambuc             }
245433d6423SLionel Sambuc             break;
246433d6423SLionel Sambuc 
247433d6423SLionel Sambuc         default:
248433d6423SLionel Sambuc 
249433d6423SLionel Sambuc             ReplacementOp = AcpiPsAllocOp (AML_INT_RETURN_VALUE_OP);
250433d6423SLionel Sambuc             if (!ReplacementOp)
251433d6423SLionel Sambuc             {
252433d6423SLionel Sambuc                 Status = AE_NO_MEMORY;
253433d6423SLionel Sambuc             }
254433d6423SLionel Sambuc         }
255433d6423SLionel Sambuc 
256433d6423SLionel Sambuc         /* We must unlink this op from the parent tree */
257433d6423SLionel Sambuc 
258433d6423SLionel Sambuc         if (Prev == Op)
259433d6423SLionel Sambuc         {
260433d6423SLionel Sambuc             /* This op is the first in the list */
261433d6423SLionel Sambuc 
262433d6423SLionel Sambuc             if (ReplacementOp)
263433d6423SLionel Sambuc             {
264433d6423SLionel Sambuc                 ReplacementOp->Common.Parent        = Op->Common.Parent;
265433d6423SLionel Sambuc                 ReplacementOp->Common.Value.Arg     = NULL;
266433d6423SLionel Sambuc                 ReplacementOp->Common.Node          = Op->Common.Node;
267433d6423SLionel Sambuc                 Op->Common.Parent->Common.Value.Arg = ReplacementOp;
268433d6423SLionel Sambuc                 ReplacementOp->Common.Next          = Op->Common.Next;
269433d6423SLionel Sambuc             }
270433d6423SLionel Sambuc             else
271433d6423SLionel Sambuc             {
272433d6423SLionel Sambuc                 Op->Common.Parent->Common.Value.Arg = Op->Common.Next;
273433d6423SLionel Sambuc             }
274433d6423SLionel Sambuc         }
275433d6423SLionel Sambuc 
276433d6423SLionel Sambuc         /* Search the parent list */
277433d6423SLionel Sambuc 
278433d6423SLionel Sambuc         else while (Prev)
279433d6423SLionel Sambuc         {
280433d6423SLionel Sambuc             /* Traverse all siblings in the parent's argument list */
281433d6423SLionel Sambuc 
282433d6423SLionel Sambuc             Next = Prev->Common.Next;
283433d6423SLionel Sambuc             if (Next == Op)
284433d6423SLionel Sambuc             {
285433d6423SLionel Sambuc                 if (ReplacementOp)
286433d6423SLionel Sambuc                 {
287433d6423SLionel Sambuc                     ReplacementOp->Common.Parent    = Op->Common.Parent;
288433d6423SLionel Sambuc                     ReplacementOp->Common.Value.Arg = NULL;
289433d6423SLionel Sambuc                     ReplacementOp->Common.Node      = Op->Common.Node;
290433d6423SLionel Sambuc                     Prev->Common.Next               = ReplacementOp;
291433d6423SLionel Sambuc                     ReplacementOp->Common.Next      = Op->Common.Next;
292433d6423SLionel Sambuc                     Next = NULL;
293433d6423SLionel Sambuc                 }
294433d6423SLionel Sambuc                 else
295433d6423SLionel Sambuc                 {
296433d6423SLionel Sambuc                     Prev->Common.Next = Op->Common.Next;
297433d6423SLionel Sambuc                     Next = NULL;
298433d6423SLionel Sambuc                 }
299433d6423SLionel Sambuc             }
300433d6423SLionel Sambuc             Prev = Next;
301433d6423SLionel Sambuc         }
302433d6423SLionel Sambuc     }
303433d6423SLionel Sambuc 
304433d6423SLionel Sambuc 
305433d6423SLionel Sambuc Cleanup:
306433d6423SLionel Sambuc 
307433d6423SLionel Sambuc     /* Now we can actually delete the subtree rooted at Op */
308433d6423SLionel Sambuc 
309433d6423SLionel Sambuc     AcpiPsDeleteParseTree (Op);
310433d6423SLionel Sambuc     return_ACPI_STATUS (Status);
311433d6423SLionel Sambuc }
312433d6423SLionel Sambuc 
313433d6423SLionel Sambuc 
314433d6423SLionel Sambuc /*******************************************************************************
315433d6423SLionel Sambuc  *
316433d6423SLionel Sambuc  * FUNCTION:    AcpiPsNextParseState
317433d6423SLionel Sambuc  *
318433d6423SLionel Sambuc  * PARAMETERS:  WalkState           - Current state
319433d6423SLionel Sambuc  *              Op                  - Current parse op
320433d6423SLionel Sambuc  *              CallbackStatus      - Status from previous operation
321433d6423SLionel Sambuc  *
322433d6423SLionel Sambuc  * RETURN:      Status
323433d6423SLionel Sambuc  *
324433d6423SLionel Sambuc  * DESCRIPTION: Update the parser state based upon the return exception from
325433d6423SLionel Sambuc  *              the parser callback.
326433d6423SLionel Sambuc  *
327433d6423SLionel Sambuc  ******************************************************************************/
328433d6423SLionel Sambuc 
329433d6423SLionel Sambuc ACPI_STATUS
AcpiPsNextParseState(ACPI_WALK_STATE * WalkState,ACPI_PARSE_OBJECT * Op,ACPI_STATUS CallbackStatus)330433d6423SLionel Sambuc AcpiPsNextParseState (
331433d6423SLionel Sambuc     ACPI_WALK_STATE         *WalkState,
332433d6423SLionel Sambuc     ACPI_PARSE_OBJECT       *Op,
333433d6423SLionel Sambuc     ACPI_STATUS             CallbackStatus)
334433d6423SLionel Sambuc {
335433d6423SLionel Sambuc     ACPI_PARSE_STATE        *ParserState = &WalkState->ParserState;
336433d6423SLionel Sambuc     ACPI_STATUS             Status = AE_CTRL_PENDING;
337433d6423SLionel Sambuc 
338433d6423SLionel Sambuc 
339433d6423SLionel Sambuc     ACPI_FUNCTION_TRACE_PTR (PsNextParseState, Op);
340433d6423SLionel Sambuc 
341433d6423SLionel Sambuc 
342433d6423SLionel Sambuc     switch (CallbackStatus)
343433d6423SLionel Sambuc     {
344433d6423SLionel Sambuc     case AE_CTRL_TERMINATE:
345433d6423SLionel Sambuc         /*
346433d6423SLionel Sambuc          * A control method was terminated via a RETURN statement.
347433d6423SLionel Sambuc          * The walk of this method is complete.
348433d6423SLionel Sambuc          */
349433d6423SLionel Sambuc         ParserState->Aml = ParserState->AmlEnd;
350433d6423SLionel Sambuc         Status = AE_CTRL_TERMINATE;
351433d6423SLionel Sambuc         break;
352433d6423SLionel Sambuc 
353433d6423SLionel Sambuc     case AE_CTRL_BREAK:
354433d6423SLionel Sambuc 
355433d6423SLionel Sambuc         ParserState->Aml = WalkState->AmlLastWhile;
356433d6423SLionel Sambuc         WalkState->ControlState->Common.Value = FALSE;
357433d6423SLionel Sambuc         Status = AE_CTRL_BREAK;
358433d6423SLionel Sambuc         break;
359433d6423SLionel Sambuc 
360433d6423SLionel Sambuc     case AE_CTRL_CONTINUE:
361433d6423SLionel Sambuc 
362433d6423SLionel Sambuc         ParserState->Aml = WalkState->AmlLastWhile;
363433d6423SLionel Sambuc         Status = AE_CTRL_CONTINUE;
364433d6423SLionel Sambuc         break;
365433d6423SLionel Sambuc 
366433d6423SLionel Sambuc     case AE_CTRL_PENDING:
367433d6423SLionel Sambuc 
368433d6423SLionel Sambuc         ParserState->Aml = WalkState->AmlLastWhile;
369433d6423SLionel Sambuc         break;
370433d6423SLionel Sambuc 
371433d6423SLionel Sambuc #if 0
372433d6423SLionel Sambuc     case AE_CTRL_SKIP:
373433d6423SLionel Sambuc 
374433d6423SLionel Sambuc         ParserState->Aml = ParserState->Scope->ParseScope.PkgEnd;
375433d6423SLionel Sambuc         Status = AE_OK;
376433d6423SLionel Sambuc         break;
377433d6423SLionel Sambuc #endif
378433d6423SLionel Sambuc 
379433d6423SLionel Sambuc     case AE_CTRL_TRUE:
380433d6423SLionel Sambuc         /*
381433d6423SLionel Sambuc          * Predicate of an IF was true, and we are at the matching ELSE.
382433d6423SLionel Sambuc          * Just close out this package
383433d6423SLionel Sambuc          */
384433d6423SLionel Sambuc         ParserState->Aml = AcpiPsGetNextPackageEnd (ParserState);
385433d6423SLionel Sambuc         Status = AE_CTRL_PENDING;
386433d6423SLionel Sambuc         break;
387433d6423SLionel Sambuc 
388433d6423SLionel Sambuc     case AE_CTRL_FALSE:
389433d6423SLionel Sambuc         /*
390433d6423SLionel Sambuc          * Either an IF/WHILE Predicate was false or we encountered a BREAK
391433d6423SLionel Sambuc          * opcode. In both cases, we do not execute the rest of the
392433d6423SLionel Sambuc          * package;  We simply close out the parent (finishing the walk of
393433d6423SLionel Sambuc          * this branch of the tree) and continue execution at the parent
394433d6423SLionel Sambuc          * level.
395433d6423SLionel Sambuc          */
396433d6423SLionel Sambuc         ParserState->Aml = ParserState->Scope->ParseScope.PkgEnd;
397433d6423SLionel Sambuc 
398433d6423SLionel Sambuc         /* In the case of a BREAK, just force a predicate (if any) to FALSE */
399433d6423SLionel Sambuc 
400433d6423SLionel Sambuc         WalkState->ControlState->Common.Value = FALSE;
401433d6423SLionel Sambuc         Status = AE_CTRL_END;
402433d6423SLionel Sambuc         break;
403433d6423SLionel Sambuc 
404433d6423SLionel Sambuc     case AE_CTRL_TRANSFER:
405433d6423SLionel Sambuc 
406433d6423SLionel Sambuc         /* A method call (invocation) -- transfer control */
407433d6423SLionel Sambuc 
408433d6423SLionel Sambuc         Status = AE_CTRL_TRANSFER;
409433d6423SLionel Sambuc         WalkState->PrevOp = Op;
410433d6423SLionel Sambuc         WalkState->MethodCallOp = Op;
411433d6423SLionel Sambuc         WalkState->MethodCallNode = (Op->Common.Value.Arg)->Common.Node;
412433d6423SLionel Sambuc 
413433d6423SLionel Sambuc         /* Will return value (if any) be used by the caller? */
414433d6423SLionel Sambuc 
415433d6423SLionel Sambuc         WalkState->ReturnUsed = AcpiDsIsResultUsed (Op, WalkState);
416433d6423SLionel Sambuc         break;
417433d6423SLionel Sambuc 
418433d6423SLionel Sambuc     default:
419433d6423SLionel Sambuc 
420433d6423SLionel Sambuc         Status = CallbackStatus;
421433d6423SLionel Sambuc         if ((CallbackStatus & AE_CODE_MASK) == AE_CODE_CONTROL)
422433d6423SLionel Sambuc         {
423433d6423SLionel Sambuc             Status = AE_OK;
424433d6423SLionel Sambuc         }
425433d6423SLionel Sambuc         break;
426433d6423SLionel Sambuc     }
427433d6423SLionel Sambuc 
428433d6423SLionel Sambuc     return_ACPI_STATUS (Status);
429433d6423SLionel Sambuc }
430433d6423SLionel Sambuc 
431433d6423SLionel Sambuc 
432433d6423SLionel Sambuc /*******************************************************************************
433433d6423SLionel Sambuc  *
434433d6423SLionel Sambuc  * FUNCTION:    AcpiPsParseAml
435433d6423SLionel Sambuc  *
436433d6423SLionel Sambuc  * PARAMETERS:  WalkState       - Current state
437433d6423SLionel Sambuc  *
438433d6423SLionel Sambuc  *
439433d6423SLionel Sambuc  * RETURN:      Status
440433d6423SLionel Sambuc  *
441433d6423SLionel Sambuc  * DESCRIPTION: Parse raw AML and return a tree of ops
442433d6423SLionel Sambuc  *
443433d6423SLionel Sambuc  ******************************************************************************/
444433d6423SLionel Sambuc 
445433d6423SLionel Sambuc ACPI_STATUS
AcpiPsParseAml(ACPI_WALK_STATE * WalkState)446433d6423SLionel Sambuc AcpiPsParseAml (
447433d6423SLionel Sambuc     ACPI_WALK_STATE         *WalkState)
448433d6423SLionel Sambuc {
449433d6423SLionel Sambuc     ACPI_STATUS             Status;
450433d6423SLionel Sambuc     ACPI_THREAD_STATE       *Thread;
451433d6423SLionel Sambuc     ACPI_THREAD_STATE       *PrevWalkList = AcpiGbl_CurrentWalkList;
452433d6423SLionel Sambuc     ACPI_WALK_STATE         *PreviousWalkState;
453433d6423SLionel Sambuc 
454433d6423SLionel Sambuc 
455433d6423SLionel Sambuc     ACPI_FUNCTION_TRACE (PsParseAml);
456433d6423SLionel Sambuc 
457433d6423SLionel Sambuc     ACPI_DEBUG_PRINT ((ACPI_DB_PARSE,
458433d6423SLionel Sambuc         "Entered with WalkState=%p Aml=%p size=%X\n",
459433d6423SLionel Sambuc         WalkState, WalkState->ParserState.Aml,
460433d6423SLionel Sambuc         WalkState->ParserState.AmlSize));
461433d6423SLionel Sambuc 
462433d6423SLionel Sambuc     if (!WalkState->ParserState.Aml)
463433d6423SLionel Sambuc     {
464433d6423SLionel Sambuc         return_ACPI_STATUS (AE_NULL_OBJECT);
465433d6423SLionel Sambuc     }
466433d6423SLionel Sambuc 
467433d6423SLionel Sambuc     /* Create and initialize a new thread state */
468433d6423SLionel Sambuc 
469433d6423SLionel Sambuc     Thread = AcpiUtCreateThreadState ();
470433d6423SLionel Sambuc     if (!Thread)
471433d6423SLionel Sambuc     {
472433d6423SLionel Sambuc         if (WalkState->MethodDesc)
473433d6423SLionel Sambuc         {
474433d6423SLionel Sambuc             /* Executing a control method - additional cleanup */
475433d6423SLionel Sambuc 
476433d6423SLionel Sambuc             AcpiDsTerminateControlMethod (WalkState->MethodDesc, WalkState);
477433d6423SLionel Sambuc         }
478433d6423SLionel Sambuc 
479433d6423SLionel Sambuc         AcpiDsDeleteWalkState (WalkState);
480433d6423SLionel Sambuc         return_ACPI_STATUS (AE_NO_MEMORY);
481433d6423SLionel Sambuc     }
482433d6423SLionel Sambuc 
483433d6423SLionel Sambuc     WalkState->Thread = Thread;
484433d6423SLionel Sambuc 
485433d6423SLionel Sambuc     /*
486433d6423SLionel Sambuc      * If executing a method, the starting SyncLevel is this method's
487433d6423SLionel Sambuc      * SyncLevel
488433d6423SLionel Sambuc      */
489433d6423SLionel Sambuc     if (WalkState->MethodDesc)
490433d6423SLionel Sambuc     {
491433d6423SLionel Sambuc         WalkState->Thread->CurrentSyncLevel = WalkState->MethodDesc->Method.SyncLevel;
492433d6423SLionel Sambuc     }
493433d6423SLionel Sambuc 
494433d6423SLionel Sambuc     AcpiDsPushWalkState (WalkState, Thread);
495433d6423SLionel Sambuc 
496433d6423SLionel Sambuc     /*
497433d6423SLionel Sambuc      * This global allows the AML debugger to get a handle to the currently
498433d6423SLionel Sambuc      * executing control method.
499433d6423SLionel Sambuc      */
500433d6423SLionel Sambuc     AcpiGbl_CurrentWalkList = Thread;
501433d6423SLionel Sambuc 
502433d6423SLionel Sambuc     /*
503433d6423SLionel Sambuc      * Execute the walk loop as long as there is a valid Walk State. This
504433d6423SLionel Sambuc      * handles nested control method invocations without recursion.
505433d6423SLionel Sambuc      */
506433d6423SLionel Sambuc     ACPI_DEBUG_PRINT ((ACPI_DB_PARSE, "State=%p\n", WalkState));
507433d6423SLionel Sambuc 
508433d6423SLionel Sambuc     Status = AE_OK;
509433d6423SLionel Sambuc     while (WalkState)
510433d6423SLionel Sambuc     {
511433d6423SLionel Sambuc         if (ACPI_SUCCESS (Status))
512433d6423SLionel Sambuc         {
513433d6423SLionel Sambuc             /*
514433d6423SLionel Sambuc              * The ParseLoop executes AML until the method terminates
515433d6423SLionel Sambuc              * or calls another method.
516433d6423SLionel Sambuc              */
517433d6423SLionel Sambuc             Status = AcpiPsParseLoop (WalkState);
518433d6423SLionel Sambuc         }
519433d6423SLionel Sambuc 
520433d6423SLionel Sambuc         ACPI_DEBUG_PRINT ((ACPI_DB_PARSE,
521433d6423SLionel Sambuc             "Completed one call to walk loop, %s State=%p\n",
522433d6423SLionel Sambuc             AcpiFormatException (Status), WalkState));
523433d6423SLionel Sambuc 
524433d6423SLionel Sambuc         if (Status == AE_CTRL_TRANSFER)
525433d6423SLionel Sambuc         {
526433d6423SLionel Sambuc             /*
527433d6423SLionel Sambuc              * A method call was detected.
528433d6423SLionel Sambuc              * Transfer control to the called control method
529433d6423SLionel Sambuc              */
530433d6423SLionel Sambuc             Status = AcpiDsCallControlMethod (Thread, WalkState, NULL);
531433d6423SLionel Sambuc             if (ACPI_FAILURE (Status))
532433d6423SLionel Sambuc             {
533433d6423SLionel Sambuc                 Status = AcpiDsMethodError (Status, WalkState);
534433d6423SLionel Sambuc             }
535433d6423SLionel Sambuc 
536433d6423SLionel Sambuc             /*
537433d6423SLionel Sambuc              * If the transfer to the new method method call worked, a new walk
538433d6423SLionel Sambuc              * state was created -- get it
539433d6423SLionel Sambuc              */
540433d6423SLionel Sambuc             WalkState = AcpiDsGetCurrentWalkState (Thread);
541433d6423SLionel Sambuc             continue;
542433d6423SLionel Sambuc         }
543433d6423SLionel Sambuc         else if (Status == AE_CTRL_TERMINATE)
544433d6423SLionel Sambuc         {
545433d6423SLionel Sambuc             Status = AE_OK;
546433d6423SLionel Sambuc         }
547433d6423SLionel Sambuc         else if ((Status != AE_OK) && (WalkState->MethodDesc))
548433d6423SLionel Sambuc         {
549433d6423SLionel Sambuc             /* Either the method parse or actual execution failed */
550433d6423SLionel Sambuc 
551433d6423SLionel Sambuc             ACPI_ERROR_METHOD ("Method parse/execution failed",
552433d6423SLionel Sambuc                 WalkState->MethodNode, NULL, Status);
553433d6423SLionel Sambuc 
554433d6423SLionel Sambuc             /* Check for possible multi-thread reentrancy problem */
555433d6423SLionel Sambuc 
556433d6423SLionel Sambuc             if ((Status == AE_ALREADY_EXISTS) &&
557*29492bb7SDavid van Moolenbroek                 (!(WalkState->MethodDesc->Method.InfoFlags & ACPI_METHOD_SERIALIZED)))
558433d6423SLionel Sambuc             {
559433d6423SLionel Sambuc                 /*
560*29492bb7SDavid van Moolenbroek                  * Method is not serialized and tried to create an object
561*29492bb7SDavid van Moolenbroek                  * twice. The probable cause is that the method cannot
562*29492bb7SDavid van Moolenbroek                  * handle reentrancy. Mark as "pending serialized" now, and
563*29492bb7SDavid van Moolenbroek                  * then mark "serialized" when the last thread exits.
564433d6423SLionel Sambuc                  */
565*29492bb7SDavid van Moolenbroek                 WalkState->MethodDesc->Method.InfoFlags |=
566*29492bb7SDavid van Moolenbroek                     ACPI_METHOD_SERIALIZED_PENDING;
567433d6423SLionel Sambuc             }
568433d6423SLionel Sambuc         }
569433d6423SLionel Sambuc 
570433d6423SLionel Sambuc         /* We are done with this walk, move on to the parent if any */
571433d6423SLionel Sambuc 
572433d6423SLionel Sambuc         WalkState = AcpiDsPopWalkState (Thread);
573433d6423SLionel Sambuc 
574433d6423SLionel Sambuc         /* Reset the current scope to the beginning of scope stack */
575433d6423SLionel Sambuc 
576433d6423SLionel Sambuc         AcpiDsScopeStackClear (WalkState);
577433d6423SLionel Sambuc 
578433d6423SLionel Sambuc         /*
579433d6423SLionel Sambuc          * If we just returned from the execution of a control method or if we
580433d6423SLionel Sambuc          * encountered an error during the method parse phase, there's lots of
581433d6423SLionel Sambuc          * cleanup to do
582433d6423SLionel Sambuc          */
583433d6423SLionel Sambuc         if (((WalkState->ParseFlags & ACPI_PARSE_MODE_MASK) == ACPI_PARSE_EXECUTE) ||
584433d6423SLionel Sambuc             (ACPI_FAILURE (Status)))
585433d6423SLionel Sambuc         {
586433d6423SLionel Sambuc             AcpiDsTerminateControlMethod (WalkState->MethodDesc, WalkState);
587433d6423SLionel Sambuc         }
588433d6423SLionel Sambuc 
589433d6423SLionel Sambuc         /* Delete this walk state and all linked control states */
590433d6423SLionel Sambuc 
591433d6423SLionel Sambuc         AcpiPsCleanupScope (&WalkState->ParserState);
592433d6423SLionel Sambuc         PreviousWalkState = WalkState;
593433d6423SLionel Sambuc 
594433d6423SLionel Sambuc         ACPI_DEBUG_PRINT ((ACPI_DB_PARSE,
595433d6423SLionel Sambuc             "ReturnValue=%p, ImplicitValue=%p State=%p\n",
596433d6423SLionel Sambuc             WalkState->ReturnDesc, WalkState->ImplicitReturnObj, WalkState));
597433d6423SLionel Sambuc 
598433d6423SLionel Sambuc         /* Check if we have restarted a preempted walk */
599433d6423SLionel Sambuc 
600433d6423SLionel Sambuc         WalkState = AcpiDsGetCurrentWalkState (Thread);
601433d6423SLionel Sambuc         if (WalkState)
602433d6423SLionel Sambuc         {
603433d6423SLionel Sambuc             if (ACPI_SUCCESS (Status))
604433d6423SLionel Sambuc             {
605433d6423SLionel Sambuc                 /*
606433d6423SLionel Sambuc                  * There is another walk state, restart it.
607433d6423SLionel Sambuc                  * If the method return value is not used by the parent,
608433d6423SLionel Sambuc                  * The object is deleted
609433d6423SLionel Sambuc                  */
610433d6423SLionel Sambuc                 if (!PreviousWalkState->ReturnDesc)
611433d6423SLionel Sambuc                 {
612433d6423SLionel Sambuc                     /*
613433d6423SLionel Sambuc                      * In slack mode execution, if there is no return value
614433d6423SLionel Sambuc                      * we should implicitly return zero (0) as a default value.
615433d6423SLionel Sambuc                      */
616433d6423SLionel Sambuc                     if (AcpiGbl_EnableInterpreterSlack &&
617433d6423SLionel Sambuc                         !PreviousWalkState->ImplicitReturnObj)
618433d6423SLionel Sambuc                     {
619433d6423SLionel Sambuc                         PreviousWalkState->ImplicitReturnObj =
620433d6423SLionel Sambuc                             AcpiUtCreateIntegerObject ((UINT64) 0);
621433d6423SLionel Sambuc                         if (!PreviousWalkState->ImplicitReturnObj)
622433d6423SLionel Sambuc                         {
623433d6423SLionel Sambuc                             return_ACPI_STATUS (AE_NO_MEMORY);
624433d6423SLionel Sambuc                         }
625433d6423SLionel Sambuc                     }
626433d6423SLionel Sambuc 
627433d6423SLionel Sambuc                     /* Restart the calling control method */
628433d6423SLionel Sambuc 
629433d6423SLionel Sambuc                     Status = AcpiDsRestartControlMethod (WalkState,
630433d6423SLionel Sambuc                                 PreviousWalkState->ImplicitReturnObj);
631433d6423SLionel Sambuc                 }
632433d6423SLionel Sambuc                 else
633433d6423SLionel Sambuc                 {
634433d6423SLionel Sambuc                     /*
635433d6423SLionel Sambuc                      * We have a valid return value, delete any implicit
636433d6423SLionel Sambuc                      * return value.
637433d6423SLionel Sambuc                      */
638433d6423SLionel Sambuc                     AcpiDsClearImplicitReturn (PreviousWalkState);
639433d6423SLionel Sambuc 
640433d6423SLionel Sambuc                     Status = AcpiDsRestartControlMethod (WalkState,
641433d6423SLionel Sambuc                                 PreviousWalkState->ReturnDesc);
642433d6423SLionel Sambuc                 }
643433d6423SLionel Sambuc                 if (ACPI_SUCCESS (Status))
644433d6423SLionel Sambuc                 {
645433d6423SLionel Sambuc                     WalkState->WalkType |= ACPI_WALK_METHOD_RESTART;
646433d6423SLionel Sambuc                 }
647433d6423SLionel Sambuc             }
648433d6423SLionel Sambuc             else
649433d6423SLionel Sambuc             {
650433d6423SLionel Sambuc                 /* On error, delete any return object or implicit return */
651433d6423SLionel Sambuc 
652433d6423SLionel Sambuc                 AcpiUtRemoveReference (PreviousWalkState->ReturnDesc);
653433d6423SLionel Sambuc                 AcpiDsClearImplicitReturn (PreviousWalkState);
654433d6423SLionel Sambuc             }
655433d6423SLionel Sambuc         }
656433d6423SLionel Sambuc 
657433d6423SLionel Sambuc         /*
658433d6423SLionel Sambuc          * Just completed a 1st-level method, save the final internal return
659433d6423SLionel Sambuc          * value (if any)
660433d6423SLionel Sambuc          */
661433d6423SLionel Sambuc         else if (PreviousWalkState->CallerReturnDesc)
662433d6423SLionel Sambuc         {
663433d6423SLionel Sambuc             if (PreviousWalkState->ImplicitReturnObj)
664433d6423SLionel Sambuc             {
665433d6423SLionel Sambuc                 *(PreviousWalkState->CallerReturnDesc) =
666433d6423SLionel Sambuc                     PreviousWalkState->ImplicitReturnObj;
667433d6423SLionel Sambuc             }
668433d6423SLionel Sambuc             else
669433d6423SLionel Sambuc             {
670433d6423SLionel Sambuc                  /* NULL if no return value */
671433d6423SLionel Sambuc 
672433d6423SLionel Sambuc                 *(PreviousWalkState->CallerReturnDesc) =
673433d6423SLionel Sambuc                     PreviousWalkState->ReturnDesc;
674433d6423SLionel Sambuc             }
675433d6423SLionel Sambuc         }
676433d6423SLionel Sambuc         else
677433d6423SLionel Sambuc         {
678433d6423SLionel Sambuc             if (PreviousWalkState->ReturnDesc)
679433d6423SLionel Sambuc             {
680433d6423SLionel Sambuc                 /* Caller doesn't want it, must delete it */
681433d6423SLionel Sambuc 
682433d6423SLionel Sambuc                 AcpiUtRemoveReference (PreviousWalkState->ReturnDesc);
683433d6423SLionel Sambuc             }
684433d6423SLionel Sambuc             if (PreviousWalkState->ImplicitReturnObj)
685433d6423SLionel Sambuc             {
686433d6423SLionel Sambuc                 /* Caller doesn't want it, must delete it */
687433d6423SLionel Sambuc 
688433d6423SLionel Sambuc                 AcpiUtRemoveReference (PreviousWalkState->ImplicitReturnObj);
689433d6423SLionel Sambuc             }
690433d6423SLionel Sambuc         }
691433d6423SLionel Sambuc 
692433d6423SLionel Sambuc         AcpiDsDeleteWalkState (PreviousWalkState);
693433d6423SLionel Sambuc     }
694433d6423SLionel Sambuc 
695433d6423SLionel Sambuc     /* Normal exit */
696433d6423SLionel Sambuc 
697433d6423SLionel Sambuc     AcpiExReleaseAllMutexes (Thread);
698433d6423SLionel Sambuc     AcpiUtDeleteGenericState (ACPI_CAST_PTR (ACPI_GENERIC_STATE, Thread));
699433d6423SLionel Sambuc     AcpiGbl_CurrentWalkList = PrevWalkList;
700433d6423SLionel Sambuc     return_ACPI_STATUS (Status);
701433d6423SLionel Sambuc }
702