Home | History | Annotate | Line # | Download | only in dispatcher
dscontrol.c revision 1.4.2.1
      1      1.1    jruoho /******************************************************************************
      2      1.1    jruoho  *
      3      1.1    jruoho  * Module Name: dscontrol - Support for execution control opcodes -
      4      1.1    jruoho  *                          if/else/while/return
      5      1.1    jruoho  *
      6      1.1    jruoho  *****************************************************************************/
      7      1.1    jruoho 
      8      1.1    jruoho /*
      9  1.4.2.1     skrll  * Copyright (C) 2000 - 2015, Intel Corp.
     10      1.1    jruoho  * All rights reserved.
     11      1.1    jruoho  *
     12      1.1    jruoho  * Redistribution and use in source and binary forms, with or without
     13      1.1    jruoho  * modification, are permitted provided that the following conditions
     14      1.1    jruoho  * are met:
     15      1.1    jruoho  * 1. Redistributions of source code must retain the above copyright
     16      1.1    jruoho  *    notice, this list of conditions, and the following disclaimer,
     17      1.1    jruoho  *    without modification.
     18      1.1    jruoho  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
     19      1.1    jruoho  *    substantially similar to the "NO WARRANTY" disclaimer below
     20      1.1    jruoho  *    ("Disclaimer") and any redistribution must be conditioned upon
     21      1.1    jruoho  *    including a substantially similar Disclaimer requirement for further
     22      1.1    jruoho  *    binary redistribution.
     23      1.1    jruoho  * 3. Neither the names of the above-listed copyright holders nor the names
     24      1.1    jruoho  *    of any contributors may be used to endorse or promote products derived
     25      1.1    jruoho  *    from this software without specific prior written permission.
     26      1.1    jruoho  *
     27      1.1    jruoho  * Alternatively, this software may be distributed under the terms of the
     28      1.1    jruoho  * GNU General Public License ("GPL") version 2 as published by the Free
     29      1.1    jruoho  * Software Foundation.
     30      1.1    jruoho  *
     31      1.1    jruoho  * NO WARRANTY
     32      1.1    jruoho  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     33      1.1    jruoho  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     34      1.1    jruoho  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
     35      1.1    jruoho  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     36      1.1    jruoho  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     37      1.1    jruoho  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     38      1.1    jruoho  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     39      1.1    jruoho  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
     40      1.1    jruoho  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
     41      1.1    jruoho  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     42      1.1    jruoho  * POSSIBILITY OF SUCH DAMAGES.
     43      1.1    jruoho  */
     44      1.1    jruoho 
     45      1.1    jruoho #include "acpi.h"
     46      1.1    jruoho #include "accommon.h"
     47      1.1    jruoho #include "amlcode.h"
     48      1.1    jruoho #include "acdispat.h"
     49      1.1    jruoho #include "acinterp.h"
     50      1.1    jruoho 
     51      1.1    jruoho #define _COMPONENT          ACPI_DISPATCHER
     52      1.1    jruoho         ACPI_MODULE_NAME    ("dscontrol")
     53      1.1    jruoho 
     54      1.1    jruoho 
     55      1.1    jruoho /*******************************************************************************
     56      1.1    jruoho  *
     57      1.1    jruoho  * FUNCTION:    AcpiDsExecBeginControlOp
     58      1.1    jruoho  *
     59      1.1    jruoho  * PARAMETERS:  WalkList        - The list that owns the walk stack
     60      1.1    jruoho  *              Op              - The control Op
     61      1.1    jruoho  *
     62      1.1    jruoho  * RETURN:      Status
     63      1.1    jruoho  *
     64      1.1    jruoho  * DESCRIPTION: Handles all control ops encountered during control method
     65      1.1    jruoho  *              execution.
     66      1.1    jruoho  *
     67      1.1    jruoho  ******************************************************************************/
     68      1.1    jruoho 
     69      1.1    jruoho ACPI_STATUS
     70      1.1    jruoho AcpiDsExecBeginControlOp (
     71      1.1    jruoho     ACPI_WALK_STATE         *WalkState,
     72      1.1    jruoho     ACPI_PARSE_OBJECT       *Op)
     73      1.1    jruoho {
     74      1.1    jruoho     ACPI_STATUS             Status = AE_OK;
     75      1.1    jruoho     ACPI_GENERIC_STATE      *ControlState;
     76      1.1    jruoho 
     77      1.1    jruoho 
     78      1.1    jruoho     ACPI_FUNCTION_NAME (DsExecBeginControlOp);
     79      1.1    jruoho 
     80      1.1    jruoho 
     81      1.1    jruoho     ACPI_DEBUG_PRINT ((ACPI_DB_DISPATCH, "Op=%p Opcode=%2.2X State=%p\n",
     82      1.1    jruoho         Op, Op->Common.AmlOpcode, WalkState));
     83      1.1    jruoho 
     84      1.1    jruoho     switch (Op->Common.AmlOpcode)
     85      1.1    jruoho     {
     86      1.1    jruoho     case AML_WHILE_OP:
     87      1.1    jruoho         /*
     88      1.1    jruoho          * If this is an additional iteration of a while loop, continue.
     89      1.1    jruoho          * There is no need to allocate a new control state.
     90      1.1    jruoho          */
     91      1.1    jruoho         if (WalkState->ControlState)
     92      1.1    jruoho         {
     93      1.1    jruoho             if (WalkState->ControlState->Control.AmlPredicateStart ==
     94      1.1    jruoho                 (WalkState->ParserState.Aml - 1))
     95      1.1    jruoho             {
     96      1.1    jruoho                 /* Reset the state to start-of-loop */
     97      1.1    jruoho 
     98      1.1    jruoho                 WalkState->ControlState->Common.State =
     99      1.1    jruoho                     ACPI_CONTROL_CONDITIONAL_EXECUTING;
    100      1.1    jruoho                 break;
    101      1.1    jruoho             }
    102      1.1    jruoho         }
    103      1.1    jruoho 
    104      1.1    jruoho         /*lint -fallthrough */
    105      1.1    jruoho 
    106      1.1    jruoho     case AML_IF_OP:
    107      1.1    jruoho         /*
    108      1.1    jruoho          * IF/WHILE: Create a new control state to manage these
    109      1.1    jruoho          * constructs. We need to manage these as a stack, in order
    110      1.1    jruoho          * to handle nesting.
    111      1.1    jruoho          */
    112      1.1    jruoho         ControlState = AcpiUtCreateControlState ();
    113      1.1    jruoho         if (!ControlState)
    114      1.1    jruoho         {
    115      1.1    jruoho             Status = AE_NO_MEMORY;
    116      1.1    jruoho             break;
    117      1.1    jruoho         }
    118      1.1    jruoho         /*
    119      1.1    jruoho          * Save a pointer to the predicate for multiple executions
    120      1.1    jruoho          * of a loop
    121      1.1    jruoho          */
    122      1.1    jruoho         ControlState->Control.AmlPredicateStart = WalkState->ParserState.Aml - 1;
    123      1.1    jruoho         ControlState->Control.PackageEnd = WalkState->ParserState.PkgEnd;
    124      1.1    jruoho         ControlState->Control.Opcode = Op->Common.AmlOpcode;
    125      1.1    jruoho 
    126      1.1    jruoho 
    127      1.1    jruoho         /* Push the control state on this walk's control stack */
    128      1.1    jruoho 
    129      1.1    jruoho         AcpiUtPushGenericState (&WalkState->ControlState, ControlState);
    130      1.1    jruoho         break;
    131      1.1    jruoho 
    132      1.1    jruoho     case AML_ELSE_OP:
    133      1.1    jruoho 
    134      1.1    jruoho         /* Predicate is in the state object */
    135      1.1    jruoho         /* If predicate is true, the IF was executed, ignore ELSE part */
    136      1.1    jruoho 
    137      1.1    jruoho         if (WalkState->LastPredicate)
    138      1.1    jruoho         {
    139      1.1    jruoho             Status = AE_CTRL_TRUE;
    140      1.1    jruoho         }
    141      1.1    jruoho 
    142      1.1    jruoho         break;
    143      1.1    jruoho 
    144      1.1    jruoho     case AML_RETURN_OP:
    145      1.1    jruoho 
    146      1.1    jruoho         break;
    147      1.1    jruoho 
    148      1.1    jruoho     default:
    149      1.3  christos 
    150      1.1    jruoho         break;
    151      1.1    jruoho     }
    152      1.1    jruoho 
    153      1.1    jruoho     return (Status);
    154      1.1    jruoho }
    155      1.1    jruoho 
    156      1.1    jruoho 
    157      1.1    jruoho /*******************************************************************************
    158      1.1    jruoho  *
    159      1.1    jruoho  * FUNCTION:    AcpiDsExecEndControlOp
    160      1.1    jruoho  *
    161      1.1    jruoho  * PARAMETERS:  WalkList        - The list that owns the walk stack
    162      1.1    jruoho  *              Op              - The control Op
    163      1.1    jruoho  *
    164      1.1    jruoho  * RETURN:      Status
    165      1.1    jruoho  *
    166      1.1    jruoho  * DESCRIPTION: Handles all control ops encountered during control method
    167      1.1    jruoho  *              execution.
    168      1.1    jruoho  *
    169      1.1    jruoho  ******************************************************************************/
    170      1.1    jruoho 
    171      1.1    jruoho ACPI_STATUS
    172      1.1    jruoho AcpiDsExecEndControlOp (
    173      1.1    jruoho     ACPI_WALK_STATE         *WalkState,
    174      1.1    jruoho     ACPI_PARSE_OBJECT       *Op)
    175      1.1    jruoho {
    176      1.1    jruoho     ACPI_STATUS             Status = AE_OK;
    177      1.1    jruoho     ACPI_GENERIC_STATE      *ControlState;
    178      1.1    jruoho 
    179      1.1    jruoho 
    180      1.1    jruoho     ACPI_FUNCTION_NAME (DsExecEndControlOp);
    181      1.1    jruoho 
    182      1.1    jruoho 
    183      1.1    jruoho     switch (Op->Common.AmlOpcode)
    184      1.1    jruoho     {
    185      1.1    jruoho     case AML_IF_OP:
    186      1.1    jruoho 
    187      1.1    jruoho         ACPI_DEBUG_PRINT ((ACPI_DB_DISPATCH, "[IF_OP] Op=%p\n", Op));
    188      1.1    jruoho 
    189      1.1    jruoho         /*
    190      1.1    jruoho          * Save the result of the predicate in case there is an
    191      1.1    jruoho          * ELSE to come
    192      1.1    jruoho          */
    193      1.1    jruoho         WalkState->LastPredicate =
    194      1.1    jruoho             (BOOLEAN) WalkState->ControlState->Common.Value;
    195      1.1    jruoho 
    196      1.1    jruoho         /*
    197      1.1    jruoho          * Pop the control state that was created at the start
    198      1.1    jruoho          * of the IF and free it
    199      1.1    jruoho          */
    200      1.1    jruoho         ControlState = AcpiUtPopGenericState (&WalkState->ControlState);
    201      1.1    jruoho         AcpiUtDeleteGenericState (ControlState);
    202      1.1    jruoho         break;
    203      1.1    jruoho 
    204      1.1    jruoho     case AML_ELSE_OP:
    205      1.1    jruoho 
    206      1.1    jruoho         break;
    207      1.1    jruoho 
    208      1.1    jruoho     case AML_WHILE_OP:
    209      1.1    jruoho 
    210      1.1    jruoho         ACPI_DEBUG_PRINT ((ACPI_DB_DISPATCH, "[WHILE_OP] Op=%p\n", Op));
    211      1.1    jruoho 
    212      1.1    jruoho         ControlState = WalkState->ControlState;
    213      1.1    jruoho         if (ControlState->Common.Value)
    214      1.1    jruoho         {
    215      1.1    jruoho             /* Predicate was true, the body of the loop was just executed */
    216      1.1    jruoho 
    217      1.1    jruoho             /*
    218      1.1    jruoho              * This loop counter mechanism allows the interpreter to escape
    219      1.1    jruoho              * possibly infinite loops. This can occur in poorly written AML
    220      1.1    jruoho              * when the hardware does not respond within a while loop and the
    221      1.1    jruoho              * loop does not implement a timeout.
    222      1.1    jruoho              */
    223      1.1    jruoho             ControlState->Control.LoopCount++;
    224      1.1    jruoho             if (ControlState->Control.LoopCount > ACPI_MAX_LOOP_ITERATIONS)
    225      1.1    jruoho             {
    226      1.1    jruoho                 Status = AE_AML_INFINITE_LOOP;
    227      1.1    jruoho                 break;
    228      1.1    jruoho             }
    229      1.1    jruoho 
    230      1.1    jruoho             /*
    231      1.1    jruoho              * Go back and evaluate the predicate and maybe execute the loop
    232      1.1    jruoho              * another time
    233      1.1    jruoho              */
    234      1.1    jruoho             Status = AE_CTRL_PENDING;
    235      1.1    jruoho             WalkState->AmlLastWhile = ControlState->Control.AmlPredicateStart;
    236      1.1    jruoho             break;
    237      1.1    jruoho         }
    238      1.1    jruoho 
    239      1.1    jruoho         /* Predicate was false, terminate this while loop */
    240      1.1    jruoho 
    241      1.1    jruoho         ACPI_DEBUG_PRINT ((ACPI_DB_DISPATCH,
    242      1.1    jruoho             "[WHILE_OP] termination! Op=%p\n",Op));
    243      1.1    jruoho 
    244      1.1    jruoho         /* Pop this control state and free it */
    245      1.1    jruoho 
    246      1.1    jruoho         ControlState = AcpiUtPopGenericState (&WalkState->ControlState);
    247      1.1    jruoho         AcpiUtDeleteGenericState (ControlState);
    248      1.1    jruoho         break;
    249      1.1    jruoho 
    250      1.1    jruoho     case AML_RETURN_OP:
    251      1.1    jruoho 
    252      1.1    jruoho         ACPI_DEBUG_PRINT ((ACPI_DB_DISPATCH,
    253      1.1    jruoho             "[RETURN_OP] Op=%p Arg=%p\n",Op, Op->Common.Value.Arg));
    254      1.1    jruoho 
    255      1.1    jruoho         /*
    256      1.1    jruoho          * One optional operand -- the return value
    257      1.1    jruoho          * It can be either an immediate operand or a result that
    258      1.1    jruoho          * has been bubbled up the tree
    259      1.1    jruoho          */
    260      1.1    jruoho         if (Op->Common.Value.Arg)
    261      1.1    jruoho         {
    262      1.1    jruoho             /* Since we have a real Return(), delete any implicit return */
    263      1.1    jruoho 
    264      1.1    jruoho             AcpiDsClearImplicitReturn (WalkState);
    265      1.1    jruoho 
    266      1.1    jruoho             /* Return statement has an immediate operand */
    267      1.1    jruoho 
    268      1.1    jruoho             Status = AcpiDsCreateOperands (WalkState, Op->Common.Value.Arg);
    269      1.1    jruoho             if (ACPI_FAILURE (Status))
    270      1.1    jruoho             {
    271      1.1    jruoho                 return (Status);
    272      1.1    jruoho             }
    273      1.1    jruoho 
    274      1.1    jruoho             /*
    275      1.1    jruoho              * If value being returned is a Reference (such as
    276      1.1    jruoho              * an arg or local), resolve it now because it may
    277      1.1    jruoho              * cease to exist at the end of the method.
    278      1.1    jruoho              */
    279      1.1    jruoho             Status = AcpiExResolveToValue (&WalkState->Operands [0], WalkState);
    280      1.1    jruoho             if (ACPI_FAILURE (Status))
    281      1.1    jruoho             {
    282      1.1    jruoho                 return (Status);
    283      1.1    jruoho             }
    284      1.1    jruoho 
    285      1.1    jruoho             /*
    286      1.1    jruoho              * Get the return value and save as the last result
    287      1.3  christos              * value. This is the only place where WalkState->ReturnDesc
    288      1.1    jruoho              * is set to anything other than zero!
    289      1.1    jruoho              */
    290      1.1    jruoho             WalkState->ReturnDesc = WalkState->Operands[0];
    291      1.1    jruoho         }
    292      1.1    jruoho         else if (WalkState->ResultCount)
    293      1.1    jruoho         {
    294      1.1    jruoho             /* Since we have a real Return(), delete any implicit return */
    295      1.1    jruoho 
    296      1.1    jruoho             AcpiDsClearImplicitReturn (WalkState);
    297      1.1    jruoho 
    298      1.1    jruoho             /*
    299      1.1    jruoho              * The return value has come from a previous calculation.
    300      1.1    jruoho              *
    301      1.1    jruoho              * If value being returned is a Reference (such as
    302      1.1    jruoho              * an arg or local), resolve it now because it may
    303      1.1    jruoho              * cease to exist at the end of the method.
    304      1.1    jruoho              *
    305      1.1    jruoho              * Allow references created by the Index operator to return
    306      1.1    jruoho              * unchanged.
    307      1.1    jruoho              */
    308      1.1    jruoho             if ((ACPI_GET_DESCRIPTOR_TYPE (WalkState->Results->Results.ObjDesc[0]) == ACPI_DESC_TYPE_OPERAND) &&
    309      1.1    jruoho                 ((WalkState->Results->Results.ObjDesc [0])->Common.Type == ACPI_TYPE_LOCAL_REFERENCE) &&
    310      1.1    jruoho                 ((WalkState->Results->Results.ObjDesc [0])->Reference.Class != ACPI_REFCLASS_INDEX))
    311      1.1    jruoho             {
    312      1.1    jruoho                 Status = AcpiExResolveToValue (&WalkState->Results->Results.ObjDesc [0], WalkState);
    313      1.1    jruoho                 if (ACPI_FAILURE (Status))
    314      1.1    jruoho                 {
    315      1.1    jruoho                     return (Status);
    316      1.1    jruoho                 }
    317      1.1    jruoho             }
    318      1.1    jruoho 
    319      1.1    jruoho             WalkState->ReturnDesc = WalkState->Results->Results.ObjDesc [0];
    320      1.1    jruoho         }
    321      1.1    jruoho         else
    322      1.1    jruoho         {
    323      1.1    jruoho             /* No return operand */
    324      1.1    jruoho 
    325      1.1    jruoho             if (WalkState->NumOperands)
    326      1.1    jruoho             {
    327      1.1    jruoho                 AcpiUtRemoveReference (WalkState->Operands [0]);
    328      1.1    jruoho             }
    329      1.1    jruoho 
    330      1.1    jruoho             WalkState->Operands [0]     = NULL;
    331      1.1    jruoho             WalkState->NumOperands      = 0;
    332      1.1    jruoho             WalkState->ReturnDesc       = NULL;
    333      1.1    jruoho         }
    334      1.1    jruoho 
    335      1.1    jruoho 
    336      1.1    jruoho         ACPI_DEBUG_PRINT ((ACPI_DB_DISPATCH,
    337      1.1    jruoho             "Completed RETURN_OP State=%p, RetVal=%p\n",
    338      1.1    jruoho             WalkState, WalkState->ReturnDesc));
    339      1.1    jruoho 
    340      1.1    jruoho         /* End the control method execution right now */
    341      1.1    jruoho 
    342      1.1    jruoho         Status = AE_CTRL_TERMINATE;
    343      1.1    jruoho         break;
    344      1.1    jruoho 
    345      1.1    jruoho     case AML_NOOP_OP:
    346      1.1    jruoho 
    347      1.1    jruoho         /* Just do nothing! */
    348      1.3  christos 
    349      1.1    jruoho         break;
    350      1.1    jruoho 
    351      1.1    jruoho     case AML_BREAK_POINT_OP:
    352      1.1    jruoho 
    353      1.1    jruoho         /*
    354      1.1    jruoho          * Set the single-step flag. This will cause the debugger (if present)
    355      1.1    jruoho          * to break to the console within the AML debugger at the start of the
    356      1.1    jruoho          * next AML instruction.
    357      1.1    jruoho          */
    358      1.1    jruoho         ACPI_DEBUGGER_EXEC (
    359      1.1    jruoho             AcpiGbl_CmSingleStep = TRUE);
    360      1.1    jruoho         ACPI_DEBUGGER_EXEC (
    361      1.1    jruoho             AcpiOsPrintf ("**break** Executed AML BreakPoint opcode\n"));
    362      1.1    jruoho 
    363      1.1    jruoho         /* Call to the OSL in case OS wants a piece of the action */
    364      1.1    jruoho 
    365      1.1    jruoho         Status = AcpiOsSignal (ACPI_SIGNAL_BREAKPOINT,
    366      1.2    jruoho 	    __UNCONST("Executed AML Breakpoint opcode"));
    367      1.1    jruoho         break;
    368      1.1    jruoho 
    369      1.1    jruoho     case AML_BREAK_OP:
    370      1.1    jruoho     case AML_CONTINUE_OP: /* ACPI 2.0 */
    371      1.1    jruoho 
    372      1.1    jruoho         /* Pop and delete control states until we find a while */
    373      1.1    jruoho 
    374      1.1    jruoho         while (WalkState->ControlState &&
    375      1.1    jruoho                 (WalkState->ControlState->Control.Opcode != AML_WHILE_OP))
    376      1.1    jruoho         {
    377      1.1    jruoho             ControlState = AcpiUtPopGenericState (&WalkState->ControlState);
    378      1.1    jruoho             AcpiUtDeleteGenericState (ControlState);
    379      1.1    jruoho         }
    380      1.1    jruoho 
    381      1.1    jruoho         /* No while found? */
    382      1.1    jruoho 
    383      1.1    jruoho         if (!WalkState->ControlState)
    384      1.1    jruoho         {
    385      1.1    jruoho             return (AE_AML_NO_WHILE);
    386      1.1    jruoho         }
    387      1.1    jruoho 
    388      1.1    jruoho         /* Was: WalkState->AmlLastWhile = WalkState->ControlState->Control.AmlPredicateStart; */
    389      1.1    jruoho 
    390      1.1    jruoho         WalkState->AmlLastWhile = WalkState->ControlState->Control.PackageEnd;
    391      1.1    jruoho 
    392      1.1    jruoho         /* Return status depending on opcode */
    393      1.1    jruoho 
    394      1.1    jruoho         if (Op->Common.AmlOpcode == AML_BREAK_OP)
    395      1.1    jruoho         {
    396      1.1    jruoho             Status = AE_CTRL_BREAK;
    397      1.1    jruoho         }
    398      1.1    jruoho         else
    399      1.1    jruoho         {
    400      1.1    jruoho             Status = AE_CTRL_CONTINUE;
    401      1.1    jruoho         }
    402      1.1    jruoho         break;
    403      1.1    jruoho 
    404      1.1    jruoho     default:
    405      1.1    jruoho 
    406      1.1    jruoho         ACPI_ERROR ((AE_INFO, "Unknown control opcode=0x%X Op=%p",
    407      1.1    jruoho             Op->Common.AmlOpcode, Op));
    408      1.1    jruoho 
    409      1.1    jruoho         Status = AE_AML_BAD_OPCODE;
    410      1.1    jruoho         break;
    411      1.1    jruoho     }
    412      1.1    jruoho 
    413      1.1    jruoho     return (Status);
    414      1.1    jruoho }
    415