dscontrol.c revision 229989
1254225Speter/******************************************************************************
2254225Speter *
3254225Speter * Module Name: dscontrol - Support for execution control opcodes -
4254225Speter *                          if/else/while/return
5254225Speter *
6254225Speter *****************************************************************************/
7254225Speter
8254225Speter/*
9254225Speter * Copyright (C) 2000 - 2012, Intel Corp.
10254225Speter * All rights reserved.
11254225Speter *
12254225Speter * Redistribution and use in source and binary forms, with or without
13254225Speter * modification, are permitted provided that the following conditions
14254225Speter * are met:
15254225Speter * 1. Redistributions of source code must retain the above copyright
16254225Speter *    notice, this list of conditions, and the following disclaimer,
17254225Speter *    without modification.
18254225Speter * 2. Redistributions in binary form must reproduce at minimum a disclaimer
19254225Speter *    substantially similar to the "NO WARRANTY" disclaimer below
20254225Speter *    ("Disclaimer") and any redistribution must be conditioned upon
21254225Speter *    including a substantially similar Disclaimer requirement for further
22254225Speter *    binary redistribution.
23254225Speter * 3. Neither the names of the above-listed copyright holders nor the names
24254225Speter *    of any contributors may be used to endorse or promote products derived
25254225Speter *    from this software without specific prior written permission.
26254225Speter *
27254225Speter * Alternatively, this software may be distributed under the terms of the
28254225Speter * GNU General Public License ("GPL") version 2 as published by the Free
29254225Speter * Software Foundation.
30254225Speter *
31254225Speter * NO WARRANTY
32254225Speter * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
33254225Speter * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
34254225Speter * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
35254225Speter * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
36254225Speter * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
37254225Speter * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
38254225Speter * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
39254225Speter * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
40254225Speter * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
41254225Speter * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
42254225Speter * POSSIBILITY OF SUCH DAMAGES.
43254225Speter */
44254225Speter
45254225Speter#define __DSCONTROL_C__
46254225Speter
47254225Speter#include <contrib/dev/acpica/include/acpi.h>
48254225Speter#include <contrib/dev/acpica/include/accommon.h>
49254225Speter#include <contrib/dev/acpica/include/amlcode.h>
50254225Speter#include <contrib/dev/acpica/include/acdispat.h>
51254225Speter#include <contrib/dev/acpica/include/acinterp.h>
52254225Speter
53254225Speter#define _COMPONENT          ACPI_DISPATCHER
54254225Speter        ACPI_MODULE_NAME    ("dscontrol")
55254225Speter
56254225Speter
57254225Speter/*******************************************************************************
58254225Speter *
59254225Speter * FUNCTION:    AcpiDsExecBeginControlOp
60254225Speter *
61254225Speter * PARAMETERS:  WalkList        - The list that owns the walk stack
62254225Speter *              Op              - The control Op
63254225Speter *
64254225Speter * RETURN:      Status
65254225Speter *
66254225Speter * DESCRIPTION: Handles all control ops encountered during control method
67254225Speter *              execution.
68254225Speter *
69254225Speter ******************************************************************************/
70254225Speter
71254225SpeterACPI_STATUS
72254225SpeterAcpiDsExecBeginControlOp (
73254225Speter    ACPI_WALK_STATE         *WalkState,
74254225Speter    ACPI_PARSE_OBJECT       *Op)
75254225Speter{
76254225Speter    ACPI_STATUS             Status = AE_OK;
77254225Speter    ACPI_GENERIC_STATE      *ControlState;
78254225Speter
79254225Speter
80254225Speter    ACPI_FUNCTION_NAME (DsExecBeginControlOp);
81254225Speter
82254225Speter
83254225Speter    ACPI_DEBUG_PRINT ((ACPI_DB_DISPATCH, "Op=%p Opcode=%2.2X State=%p\n",
84254225Speter        Op, Op->Common.AmlOpcode, WalkState));
85254225Speter
86254225Speter    switch (Op->Common.AmlOpcode)
87254225Speter    {
88254225Speter    case AML_WHILE_OP:
89254225Speter
90254225Speter        /*
91254225Speter         * If this is an additional iteration of a while loop, continue.
92254225Speter         * There is no need to allocate a new control state.
93254225Speter         */
94254225Speter        if (WalkState->ControlState)
95254225Speter        {
96254225Speter            if (WalkState->ControlState->Control.AmlPredicateStart ==
97254225Speter                (WalkState->ParserState.Aml - 1))
98254225Speter            {
99254225Speter                /* Reset the state to start-of-loop */
100254225Speter
101254225Speter                WalkState->ControlState->Common.State =
102254225Speter                    ACPI_CONTROL_CONDITIONAL_EXECUTING;
103254225Speter                break;
104254225Speter            }
105254225Speter        }
106254225Speter
107254225Speter        /*lint -fallthrough */
108254225Speter
109254225Speter    case AML_IF_OP:
110254225Speter
111254225Speter        /*
112254225Speter         * IF/WHILE: Create a new control state to manage these
113254225Speter         * constructs. We need to manage these as a stack, in order
114254225Speter         * to handle nesting.
115254225Speter         */
116254225Speter        ControlState = AcpiUtCreateControlState ();
117254225Speter        if (!ControlState)
118254225Speter        {
119254225Speter            Status = AE_NO_MEMORY;
120254225Speter            break;
121254225Speter        }
122254225Speter        /*
123254225Speter         * Save a pointer to the predicate for multiple executions
124254225Speter         * of a loop
125254225Speter         */
126254225Speter        ControlState->Control.AmlPredicateStart = WalkState->ParserState.Aml - 1;
127254225Speter        ControlState->Control.PackageEnd = WalkState->ParserState.PkgEnd;
128254225Speter        ControlState->Control.Opcode = Op->Common.AmlOpcode;
129254225Speter
130254225Speter
131254225Speter        /* Push the control state on this walk's control stack */
132254225Speter
133254225Speter        AcpiUtPushGenericState (&WalkState->ControlState, ControlState);
134254225Speter        break;
135254225Speter
136254225Speter    case AML_ELSE_OP:
137254225Speter
138254225Speter        /* Predicate is in the state object */
139254225Speter        /* If predicate is true, the IF was executed, ignore ELSE part */
140254225Speter
141254225Speter        if (WalkState->LastPredicate)
142254225Speter        {
143254225Speter            Status = AE_CTRL_TRUE;
144254225Speter        }
145254225Speter
146254225Speter        break;
147254225Speter
148254225Speter    case AML_RETURN_OP:
149254225Speter
150254225Speter        break;
151254225Speter
152254225Speter    default:
153254225Speter        break;
154254225Speter    }
155254225Speter
156254225Speter    return (Status);
157254225Speter}
158254225Speter
159254225Speter
160254225Speter/*******************************************************************************
161254225Speter *
162254225Speter * FUNCTION:    AcpiDsExecEndControlOp
163254225Speter *
164254225Speter * PARAMETERS:  WalkList        - The list that owns the walk stack
165254225Speter *              Op              - The control Op
166254225Speter *
167254225Speter * RETURN:      Status
168254225Speter *
169254225Speter * DESCRIPTION: Handles all control ops encountered during control method
170254225Speter *              execution.
171254225Speter *
172254225Speter ******************************************************************************/
173254225Speter
174254225SpeterACPI_STATUS
175254225SpeterAcpiDsExecEndControlOp (
176254225Speter    ACPI_WALK_STATE         *WalkState,
177    ACPI_PARSE_OBJECT       *Op)
178{
179    ACPI_STATUS             Status = AE_OK;
180    ACPI_GENERIC_STATE      *ControlState;
181
182
183    ACPI_FUNCTION_NAME (DsExecEndControlOp);
184
185
186    switch (Op->Common.AmlOpcode)
187    {
188    case AML_IF_OP:
189
190        ACPI_DEBUG_PRINT ((ACPI_DB_DISPATCH, "[IF_OP] Op=%p\n", Op));
191
192        /*
193         * Save the result of the predicate in case there is an
194         * ELSE to come
195         */
196        WalkState->LastPredicate =
197            (BOOLEAN) WalkState->ControlState->Common.Value;
198
199        /*
200         * Pop the control state that was created at the start
201         * of the IF and free it
202         */
203        ControlState = AcpiUtPopGenericState (&WalkState->ControlState);
204        AcpiUtDeleteGenericState (ControlState);
205        break;
206
207
208    case AML_ELSE_OP:
209
210        break;
211
212
213    case AML_WHILE_OP:
214
215        ACPI_DEBUG_PRINT ((ACPI_DB_DISPATCH, "[WHILE_OP] Op=%p\n", Op));
216
217        ControlState = WalkState->ControlState;
218        if (ControlState->Common.Value)
219        {
220            /* Predicate was true, the body of the loop was just executed */
221
222            /*
223             * This loop counter mechanism allows the interpreter to escape
224             * possibly infinite loops. This can occur in poorly written AML
225             * when the hardware does not respond within a while loop and the
226             * loop does not implement a timeout.
227             */
228            ControlState->Control.LoopCount++;
229            if (ControlState->Control.LoopCount > ACPI_MAX_LOOP_ITERATIONS)
230            {
231                Status = AE_AML_INFINITE_LOOP;
232                break;
233            }
234
235            /*
236             * Go back and evaluate the predicate and maybe execute the loop
237             * another time
238             */
239            Status = AE_CTRL_PENDING;
240            WalkState->AmlLastWhile = ControlState->Control.AmlPredicateStart;
241            break;
242        }
243
244        /* Predicate was false, terminate this while loop */
245
246        ACPI_DEBUG_PRINT ((ACPI_DB_DISPATCH,
247            "[WHILE_OP] termination! Op=%p\n",Op));
248
249        /* Pop this control state and free it */
250
251        ControlState = AcpiUtPopGenericState (&WalkState->ControlState);
252        AcpiUtDeleteGenericState (ControlState);
253        break;
254
255
256    case AML_RETURN_OP:
257
258        ACPI_DEBUG_PRINT ((ACPI_DB_DISPATCH,
259            "[RETURN_OP] Op=%p Arg=%p\n",Op, Op->Common.Value.Arg));
260
261        /*
262         * One optional operand -- the return value
263         * It can be either an immediate operand or a result that
264         * has been bubbled up the tree
265         */
266        if (Op->Common.Value.Arg)
267        {
268            /* Since we have a real Return(), delete any implicit return */
269
270            AcpiDsClearImplicitReturn (WalkState);
271
272            /* Return statement has an immediate operand */
273
274            Status = AcpiDsCreateOperands (WalkState, Op->Common.Value.Arg);
275            if (ACPI_FAILURE (Status))
276            {
277                return (Status);
278            }
279
280            /*
281             * If value being returned is a Reference (such as
282             * an arg or local), resolve it now because it may
283             * cease to exist at the end of the method.
284             */
285            Status = AcpiExResolveToValue (&WalkState->Operands [0], WalkState);
286            if (ACPI_FAILURE (Status))
287            {
288                return (Status);
289            }
290
291            /*
292             * Get the return value and save as the last result
293             * value.  This is the only place where WalkState->ReturnDesc
294             * is set to anything other than zero!
295             */
296            WalkState->ReturnDesc = WalkState->Operands[0];
297        }
298        else if (WalkState->ResultCount)
299        {
300            /* Since we have a real Return(), delete any implicit return */
301
302            AcpiDsClearImplicitReturn (WalkState);
303
304            /*
305             * The return value has come from a previous calculation.
306             *
307             * If value being returned is a Reference (such as
308             * an arg or local), resolve it now because it may
309             * cease to exist at the end of the method.
310             *
311             * Allow references created by the Index operator to return
312             * unchanged.
313             */
314            if ((ACPI_GET_DESCRIPTOR_TYPE (WalkState->Results->Results.ObjDesc[0]) == ACPI_DESC_TYPE_OPERAND) &&
315                ((WalkState->Results->Results.ObjDesc [0])->Common.Type == ACPI_TYPE_LOCAL_REFERENCE) &&
316                ((WalkState->Results->Results.ObjDesc [0])->Reference.Class != ACPI_REFCLASS_INDEX))
317            {
318                Status = AcpiExResolveToValue (&WalkState->Results->Results.ObjDesc [0], WalkState);
319                if (ACPI_FAILURE (Status))
320                {
321                    return (Status);
322                }
323            }
324
325            WalkState->ReturnDesc = WalkState->Results->Results.ObjDesc [0];
326        }
327        else
328        {
329            /* No return operand */
330
331            if (WalkState->NumOperands)
332            {
333                AcpiUtRemoveReference (WalkState->Operands [0]);
334            }
335
336            WalkState->Operands [0]     = NULL;
337            WalkState->NumOperands      = 0;
338            WalkState->ReturnDesc       = NULL;
339        }
340
341
342        ACPI_DEBUG_PRINT ((ACPI_DB_DISPATCH,
343            "Completed RETURN_OP State=%p, RetVal=%p\n",
344            WalkState, WalkState->ReturnDesc));
345
346        /* End the control method execution right now */
347
348        Status = AE_CTRL_TERMINATE;
349        break;
350
351
352    case AML_NOOP_OP:
353
354        /* Just do nothing! */
355        break;
356
357
358    case AML_BREAK_POINT_OP:
359
360        /*
361         * Set the single-step flag. This will cause the debugger (if present)
362         * to break to the console within the AML debugger at the start of the
363         * next AML instruction.
364         */
365        ACPI_DEBUGGER_EXEC (
366            AcpiGbl_CmSingleStep = TRUE);
367        ACPI_DEBUGGER_EXEC (
368            AcpiOsPrintf ("**break** Executed AML BreakPoint opcode\n"));
369
370        /* Call to the OSL in case OS wants a piece of the action */
371
372        Status = AcpiOsSignal (ACPI_SIGNAL_BREAKPOINT,
373                    "Executed AML Breakpoint opcode");
374        break;
375
376
377    case AML_BREAK_OP:
378    case AML_CONTINUE_OP: /* ACPI 2.0 */
379
380
381        /* Pop and delete control states until we find a while */
382
383        while (WalkState->ControlState &&
384                (WalkState->ControlState->Control.Opcode != AML_WHILE_OP))
385        {
386            ControlState = AcpiUtPopGenericState (&WalkState->ControlState);
387            AcpiUtDeleteGenericState (ControlState);
388        }
389
390        /* No while found? */
391
392        if (!WalkState->ControlState)
393        {
394            return (AE_AML_NO_WHILE);
395        }
396
397        /* Was: WalkState->AmlLastWhile = WalkState->ControlState->Control.AmlPredicateStart; */
398
399        WalkState->AmlLastWhile = WalkState->ControlState->Control.PackageEnd;
400
401        /* Return status depending on opcode */
402
403        if (Op->Common.AmlOpcode == AML_BREAK_OP)
404        {
405            Status = AE_CTRL_BREAK;
406        }
407        else
408        {
409            Status = AE_CTRL_CONTINUE;
410        }
411        break;
412
413
414    default:
415
416        ACPI_ERROR ((AE_INFO, "Unknown control opcode=0x%X Op=%p",
417            Op->Common.AmlOpcode, Op));
418
419        Status = AE_AML_BAD_OPCODE;
420        break;
421    }
422
423    return (Status);
424}
425