Home | History | Annotate | Line # | Download | only in namespace
nseval.c revision 1.17
      1   1.1    jruoho /*******************************************************************************
      2   1.1    jruoho  *
      3   1.1    jruoho  * Module Name: nseval - Object evaluation, includes control method execution
      4   1.1    jruoho  *
      5   1.1    jruoho  ******************************************************************************/
      6   1.1    jruoho 
      7   1.3    jruoho /*
      8  1.17  christos  * Copyright (C) 2000 - 2021, Intel Corp.
      9   1.1    jruoho  * All rights reserved.
     10   1.1    jruoho  *
     11   1.3    jruoho  * Redistribution and use in source and binary forms, with or without
     12   1.3    jruoho  * modification, are permitted provided that the following conditions
     13   1.3    jruoho  * are met:
     14   1.3    jruoho  * 1. Redistributions of source code must retain the above copyright
     15   1.3    jruoho  *    notice, this list of conditions, and the following disclaimer,
     16   1.3    jruoho  *    without modification.
     17   1.3    jruoho  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
     18   1.3    jruoho  *    substantially similar to the "NO WARRANTY" disclaimer below
     19   1.3    jruoho  *    ("Disclaimer") and any redistribution must be conditioned upon
     20   1.3    jruoho  *    including a substantially similar Disclaimer requirement for further
     21   1.3    jruoho  *    binary redistribution.
     22   1.3    jruoho  * 3. Neither the names of the above-listed copyright holders nor the names
     23   1.3    jruoho  *    of any contributors may be used to endorse or promote products derived
     24   1.3    jruoho  *    from this software without specific prior written permission.
     25   1.3    jruoho  *
     26   1.3    jruoho  * Alternatively, this software may be distributed under the terms of the
     27   1.3    jruoho  * GNU General Public License ("GPL") version 2 as published by the Free
     28   1.3    jruoho  * Software Foundation.
     29   1.3    jruoho  *
     30   1.3    jruoho  * NO WARRANTY
     31   1.3    jruoho  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     32   1.3    jruoho  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     33  1.17  christos  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
     34   1.3    jruoho  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     35   1.3    jruoho  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     36   1.3    jruoho  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     37   1.3    jruoho  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     38   1.3    jruoho  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
     39   1.3    jruoho  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
     40   1.3    jruoho  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     41   1.3    jruoho  * POSSIBILITY OF SUCH DAMAGES.
     42   1.3    jruoho  */
     43   1.1    jruoho 
     44   1.1    jruoho #include "acpi.h"
     45   1.1    jruoho #include "accommon.h"
     46   1.1    jruoho #include "acparser.h"
     47   1.1    jruoho #include "acinterp.h"
     48   1.1    jruoho #include "acnamesp.h"
     49   1.1    jruoho 
     50   1.1    jruoho 
     51   1.1    jruoho #define _COMPONENT          ACPI_NAMESPACE
     52   1.1    jruoho         ACPI_MODULE_NAME    ("nseval")
     53   1.1    jruoho 
     54   1.1    jruoho 
     55   1.1    jruoho /*******************************************************************************
     56   1.1    jruoho  *
     57   1.1    jruoho  * FUNCTION:    AcpiNsEvaluate
     58   1.1    jruoho  *
     59   1.7  christos  * PARAMETERS:  Info            - Evaluation info block, contains these fields
     60   1.7  christos  *                                and more:
     61   1.1    jruoho  *                  PrefixNode      - Prefix or Method/Object Node to execute
     62   1.4  christos  *                  RelativePath    - Name of method to execute, If NULL, the
     63   1.1    jruoho  *                                    Node is the object to execute
     64   1.1    jruoho  *                  Parameters      - List of parameters to pass to the method,
     65   1.1    jruoho  *                                    terminated by NULL. Params itself may be
     66   1.1    jruoho  *                                    NULL if no parameters are being passed.
     67   1.1    jruoho  *                  ParameterType   - Type of Parameter list
     68   1.1    jruoho  *                  ReturnObject    - Where to put method's return value (if
     69   1.1    jruoho  *                                    any). If NULL, no value is returned.
     70   1.1    jruoho  *                  Flags           - ACPI_IGNORE_RETURN_VALUE to delete return
     71   1.1    jruoho  *
     72   1.1    jruoho  * RETURN:      Status
     73   1.1    jruoho  *
     74   1.1    jruoho  * DESCRIPTION: Execute a control method or return the current value of an
     75   1.1    jruoho  *              ACPI namespace object.
     76   1.1    jruoho  *
     77   1.1    jruoho  * MUTEX:       Locks interpreter
     78   1.1    jruoho  *
     79   1.1    jruoho  ******************************************************************************/
     80   1.1    jruoho 
     81   1.1    jruoho ACPI_STATUS
     82   1.1    jruoho AcpiNsEvaluate (
     83   1.1    jruoho     ACPI_EVALUATE_INFO      *Info)
     84   1.1    jruoho {
     85   1.1    jruoho     ACPI_STATUS             Status;
     86   1.1    jruoho 
     87   1.1    jruoho 
     88   1.1    jruoho     ACPI_FUNCTION_TRACE (NsEvaluate);
     89   1.1    jruoho 
     90   1.1    jruoho 
     91   1.1    jruoho     if (!Info)
     92   1.1    jruoho     {
     93   1.1    jruoho         return_ACPI_STATUS (AE_BAD_PARAMETER);
     94   1.1    jruoho     }
     95   1.1    jruoho 
     96   1.4  christos     if (!Info->Node)
     97   1.4  christos     {
     98   1.4  christos         /*
     99   1.4  christos          * Get the actual namespace node for the target object if we
    100   1.4  christos          * need to. Handles these cases:
    101   1.4  christos          *
    102   1.4  christos          * 1) Null node, valid pathname from root (absolute path)
    103   1.4  christos          * 2) Node and valid pathname (path relative to Node)
    104   1.4  christos          * 3) Node, Null pathname
    105   1.4  christos          */
    106   1.4  christos         Status = AcpiNsGetNode (Info->PrefixNode, Info->RelativePathname,
    107   1.4  christos             ACPI_NS_NO_UPSEARCH, &Info->Node);
    108   1.4  christos         if (ACPI_FAILURE (Status))
    109   1.4  christos         {
    110   1.4  christos             return_ACPI_STATUS (Status);
    111   1.4  christos         }
    112   1.4  christos     }
    113   1.4  christos 
    114   1.4  christos     /*
    115   1.4  christos      * For a method alias, we must grab the actual method node so that
    116   1.4  christos      * proper scoping context will be established before execution.
    117   1.4  christos      */
    118   1.4  christos     if (AcpiNsGetType (Info->Node) == ACPI_TYPE_LOCAL_METHOD_ALIAS)
    119   1.4  christos     {
    120   1.4  christos         Info->Node = ACPI_CAST_PTR (
    121   1.4  christos             ACPI_NAMESPACE_NODE, Info->Node->Object);
    122   1.4  christos     }
    123   1.4  christos 
    124   1.4  christos     /* Complete the info block initialization */
    125   1.1    jruoho 
    126   1.1    jruoho     Info->ReturnObject = NULL;
    127   1.4  christos     Info->NodeFlags = Info->Node->Flags;
    128   1.4  christos     Info->ObjDesc = AcpiNsGetAttachedObject (Info->Node);
    129   1.4  christos 
    130   1.4  christos     ACPI_DEBUG_PRINT ((ACPI_DB_NAMES, "%s [%p] Value %p\n",
    131   1.4  christos         Info->RelativePathname, Info->Node,
    132   1.4  christos         AcpiNsGetAttachedObject (Info->Node)));
    133   1.4  christos 
    134   1.4  christos     /* Get info if we have a predefined name (_HID, etc.) */
    135   1.4  christos 
    136   1.4  christos     Info->Predefined = AcpiUtMatchPredefinedMethod (Info->Node->Name.Ascii);
    137   1.4  christos 
    138   1.4  christos     /* Get the full pathname to the object, for use in warning messages */
    139   1.4  christos 
    140   1.8  christos     Info->FullPathname = AcpiNsGetNormalizedPathname (Info->Node, TRUE);
    141   1.4  christos     if (!Info->FullPathname)
    142   1.4  christos     {
    143   1.4  christos         return_ACPI_STATUS (AE_NO_MEMORY);
    144   1.4  christos     }
    145   1.4  christos 
    146  1.13  christos     /* Optional object evaluation log */
    147  1.13  christos 
    148  1.13  christos     ACPI_DEBUG_PRINT_RAW ((ACPI_DB_EVALUATION,
    149  1.13  christos         "%-26s:  %s (%s)\n", "   Enter evaluation",
    150  1.13  christos         &Info->FullPathname[1], AcpiUtGetTypeName (Info->Node->Type)));
    151  1.13  christos 
    152   1.4  christos     /* Count the number of arguments being passed in */
    153   1.4  christos 
    154   1.1    jruoho     Info->ParamCount = 0;
    155   1.4  christos     if (Info->Parameters)
    156   1.4  christos     {
    157   1.4  christos         while (Info->Parameters[Info->ParamCount])
    158   1.4  christos         {
    159   1.4  christos             Info->ParamCount++;
    160   1.4  christos         }
    161   1.4  christos 
    162   1.4  christos         /* Warn on impossible argument count */
    163   1.4  christos 
    164   1.4  christos         if (Info->ParamCount > ACPI_METHOD_NUM_ARGS)
    165   1.4  christos         {
    166   1.4  christos             ACPI_WARN_PREDEFINED ((AE_INFO, Info->FullPathname, ACPI_WARN_ALWAYS,
    167   1.4  christos                 "Excess arguments (%u) - using only %u",
    168   1.4  christos                 Info->ParamCount, ACPI_METHOD_NUM_ARGS));
    169   1.4  christos 
    170   1.4  christos             Info->ParamCount = ACPI_METHOD_NUM_ARGS;
    171   1.4  christos         }
    172   1.4  christos     }
    173   1.1    jruoho 
    174   1.1    jruoho     /*
    175   1.4  christos      * For predefined names: Check that the declared argument count
    176   1.4  christos      * matches the ACPI spec -- otherwise this is a BIOS error.
    177   1.1    jruoho      */
    178   1.4  christos     AcpiNsCheckAcpiCompliance (Info->FullPathname, Info->Node,
    179   1.4  christos         Info->Predefined);
    180   1.1    jruoho 
    181   1.1    jruoho     /*
    182   1.4  christos      * For all names: Check that the incoming argument count for
    183   1.4  christos      * this method/object matches the actual ASL/AML definition.
    184   1.1    jruoho      */
    185   1.4  christos     AcpiNsCheckArgumentCount (Info->FullPathname, Info->Node,
    186   1.4  christos         Info->ParamCount, Info->Predefined);
    187   1.1    jruoho 
    188   1.4  christos     /* For predefined names: Typecheck all incoming arguments */
    189   1.1    jruoho 
    190   1.4  christos     AcpiNsCheckArgumentTypes (Info);
    191   1.1    jruoho 
    192   1.1    jruoho     /*
    193   1.4  christos      * Three major evaluation cases:
    194   1.1    jruoho      *
    195   1.4  christos      * 1) Object types that cannot be evaluated by definition
    196   1.4  christos      * 2) The object is a control method -- execute it
    197   1.4  christos      * 3) The object is not a method -- just return it's current value
    198   1.1    jruoho      */
    199   1.4  christos     switch (AcpiNsGetType (Info->Node))
    200   1.1    jruoho     {
    201  1.11  christos     case ACPI_TYPE_ANY:
    202   1.4  christos     case ACPI_TYPE_DEVICE:
    203   1.4  christos     case ACPI_TYPE_EVENT:
    204   1.4  christos     case ACPI_TYPE_MUTEX:
    205   1.4  christos     case ACPI_TYPE_REGION:
    206   1.4  christos     case ACPI_TYPE_THERMAL:
    207   1.4  christos     case ACPI_TYPE_LOCAL_SCOPE:
    208   1.1    jruoho         /*
    209  1.11  christos          * 1) Disallow evaluation of these object types. For these,
    210  1.11  christos          *    object evaluation is undefined.
    211   1.4  christos          */
    212   1.4  christos         ACPI_ERROR ((AE_INFO,
    213  1.11  christos             "%s: This object type [%s] "
    214  1.11  christos             "never contains data and cannot be evaluated",
    215  1.11  christos             Info->FullPathname, AcpiUtGetTypeName (Info->Node->Type)));
    216   1.4  christos 
    217   1.4  christos         Status = AE_TYPE;
    218   1.4  christos         goto Cleanup;
    219   1.4  christos 
    220   1.4  christos     case ACPI_TYPE_METHOD:
    221   1.4  christos         /*
    222   1.4  christos          * 2) Object is a control method - execute it
    223   1.1    jruoho          */
    224   1.1    jruoho 
    225   1.1    jruoho         /* Verify that there is a method object associated with this node */
    226   1.1    jruoho 
    227   1.1    jruoho         if (!Info->ObjDesc)
    228   1.1    jruoho         {
    229   1.4  christos             ACPI_ERROR ((AE_INFO, "%s: Method has no attached sub-object",
    230   1.4  christos                 Info->FullPathname));
    231   1.4  christos             Status = AE_NULL_OBJECT;
    232   1.4  christos             goto Cleanup;
    233   1.1    jruoho         }
    234   1.1    jruoho 
    235   1.1    jruoho         ACPI_DEBUG_PRINT ((ACPI_DB_EXEC,
    236   1.4  christos             "**** Execute method [%s] at AML address %p length %X\n",
    237   1.4  christos             Info->FullPathname,
    238   1.1    jruoho             Info->ObjDesc->Method.AmlStart + 1,
    239   1.1    jruoho             Info->ObjDesc->Method.AmlLength - 1));
    240   1.1    jruoho 
    241   1.1    jruoho         /*
    242   1.1    jruoho          * Any namespace deletion must acquire both the namespace and
    243   1.1    jruoho          * interpreter locks to ensure that no thread is using the portion of
    244   1.1    jruoho          * the namespace that is being deleted.
    245   1.1    jruoho          *
    246   1.1    jruoho          * Execute the method via the interpreter. The interpreter is locked
    247   1.1    jruoho          * here before calling into the AML parser
    248   1.1    jruoho          */
    249   1.1    jruoho         AcpiExEnterInterpreter ();
    250   1.1    jruoho         Status = AcpiPsExecuteMethod (Info);
    251   1.1    jruoho         AcpiExExitInterpreter ();
    252   1.4  christos         break;
    253   1.4  christos 
    254   1.4  christos     default:
    255   1.1    jruoho         /*
    256   1.4  christos          * 3) All other non-method objects -- get the current object value
    257   1.1    jruoho          */
    258   1.1    jruoho 
    259   1.1    jruoho         /*
    260   1.4  christos          * Some objects require additional resolution steps (e.g., the Node
    261   1.4  christos          * may be a field that must be read, etc.) -- we can't just grab
    262   1.4  christos          * the object out of the node.
    263   1.1    jruoho          *
    264   1.1    jruoho          * Use ResolveNodeToValue() to get the associated value.
    265   1.1    jruoho          *
    266   1.1    jruoho          * NOTE: we can get away with passing in NULL for a walk state because
    267   1.4  christos          * the Node is guaranteed to not be a reference to either a method
    268   1.1    jruoho          * local or a method argument (because this interface is never called
    269   1.1    jruoho          * from a running method.)
    270   1.1    jruoho          *
    271   1.1    jruoho          * Even though we do not directly invoke the interpreter for object
    272   1.4  christos          * resolution, we must lock it because we could access an OpRegion.
    273   1.4  christos          * The OpRegion access code assumes that the interpreter is locked.
    274   1.1    jruoho          */
    275   1.1    jruoho         AcpiExEnterInterpreter ();
    276   1.1    jruoho 
    277   1.4  christos         /* TBD: ResolveNodeToValue has a strange interface, fix */
    278   1.4  christos 
    279   1.4  christos         Info->ReturnObject = ACPI_CAST_PTR (ACPI_OPERAND_OBJECT, Info->Node);
    280   1.1    jruoho 
    281   1.4  christos         Status = AcpiExResolveNodeToValue (ACPI_CAST_INDIRECT_PTR (
    282   1.4  christos             ACPI_NAMESPACE_NODE, &Info->ReturnObject), NULL);
    283   1.1    jruoho         AcpiExExitInterpreter ();
    284   1.1    jruoho 
    285   1.4  christos         if (ACPI_FAILURE (Status))
    286   1.1    jruoho         {
    287   1.8  christos             Info->ReturnObject = NULL;
    288   1.4  christos             goto Cleanup;
    289   1.1    jruoho         }
    290   1.4  christos 
    291   1.4  christos         ACPI_DEBUG_PRINT ((ACPI_DB_NAMES, "Returned object %p [%s]\n",
    292   1.4  christos             Info->ReturnObject,
    293   1.4  christos             AcpiUtGetObjectTypeName (Info->ReturnObject)));
    294   1.4  christos 
    295   1.4  christos         Status = AE_CTRL_RETURN_VALUE; /* Always has a "return value" */
    296   1.4  christos         break;
    297   1.1    jruoho     }
    298   1.1    jruoho 
    299   1.1    jruoho     /*
    300   1.4  christos      * For predefined names, check the return value against the ACPI
    301   1.4  christos      * specification. Some incorrect return value types are repaired.
    302   1.1    jruoho      */
    303   1.4  christos     (void) AcpiNsCheckReturnValue (Info->Node, Info, Info->ParamCount,
    304   1.4  christos         Status, &Info->ReturnObject);
    305   1.1    jruoho 
    306   1.1    jruoho     /* Check if there is a return value that must be dealt with */
    307   1.1    jruoho 
    308   1.1    jruoho     if (Status == AE_CTRL_RETURN_VALUE)
    309   1.1    jruoho     {
    310   1.1    jruoho         /* If caller does not want the return value, delete it */
    311   1.1    jruoho 
    312   1.1    jruoho         if (Info->Flags & ACPI_IGNORE_RETURN_VALUE)
    313   1.1    jruoho         {
    314   1.1    jruoho             AcpiUtRemoveReference (Info->ReturnObject);
    315   1.1    jruoho             Info->ReturnObject = NULL;
    316   1.1    jruoho         }
    317   1.1    jruoho 
    318   1.1    jruoho         /* Map AE_CTRL_RETURN_VALUE to AE_OK, we are done with it */
    319   1.1    jruoho 
    320   1.1    jruoho         Status = AE_OK;
    321   1.1    jruoho     }
    322  1.12  christos     else if (ACPI_FAILURE(Status))
    323  1.11  christos     {
    324  1.11  christos         /* If ReturnObject exists, delete it */
    325  1.11  christos 
    326  1.12  christos         if (Info->ReturnObject)
    327  1.11  christos         {
    328  1.11  christos             AcpiUtRemoveReference (Info->ReturnObject);
    329  1.11  christos             Info->ReturnObject = NULL;
    330  1.11  christos         }
    331  1.11  christos     }
    332   1.1    jruoho 
    333   1.1    jruoho     ACPI_DEBUG_PRINT ((ACPI_DB_NAMES,
    334   1.4  christos         "*** Completed evaluation of object %s ***\n",
    335   1.4  christos         Info->RelativePathname));
    336   1.1    jruoho 
    337   1.4  christos Cleanup:
    338  1.13  christos     /* Optional object evaluation log */
    339  1.13  christos 
    340  1.13  christos     ACPI_DEBUG_PRINT_RAW ((ACPI_DB_EVALUATION,
    341  1.13  christos         "%-26s:  %s\n", "   Exit evaluation",
    342  1.13  christos         &Info->FullPathname[1]));
    343  1.13  christos 
    344   1.1    jruoho     /*
    345   1.1    jruoho      * Namespace was unlocked by the handling AcpiNs* function, so we
    346   1.4  christos      * just free the pathname and return
    347   1.1    jruoho      */
    348   1.4  christos     ACPI_FREE (Info->FullPathname);
    349   1.4  christos     Info->FullPathname = NULL;
    350   1.1    jruoho     return_ACPI_STATUS (Status);
    351   1.1    jruoho }
    352