xref: /freebsd/sys/contrib/dev/acpica/components/namespace/nseval.c (revision 7fdf597e96a02165cfe22ff357b857d5fa15ed8a)
1 /*******************************************************************************
2  *
3  * Module Name: nseval - Object evaluation, includes control method execution
4  *
5  ******************************************************************************/
6 
7 /******************************************************************************
8  *
9  * 1. Copyright Notice
10  *
11  * Some or all of this work - Copyright (c) 1999 - 2023, Intel Corp.
12  * All rights reserved.
13  *
14  * 2. License
15  *
16  * 2.1. This is your license from Intel Corp. under its intellectual property
17  * rights. You may have additional license terms from the party that provided
18  * you this software, covering your right to use that party's intellectual
19  * property rights.
20  *
21  * 2.2. Intel grants, free of charge, to any person ("Licensee") obtaining a
22  * copy of the source code appearing in this file ("Covered Code") an
23  * irrevocable, perpetual, worldwide license under Intel's copyrights in the
24  * base code distributed originally by Intel ("Original Intel Code") to copy,
25  * make derivatives, distribute, use and display any portion of the Covered
26  * Code in any form, with the right to sublicense such rights; and
27  *
28  * 2.3. Intel grants Licensee a non-exclusive and non-transferable patent
29  * license (with the right to sublicense), under only those claims of Intel
30  * patents that are infringed by the Original Intel Code, to make, use, sell,
31  * offer to sell, and import the Covered Code and derivative works thereof
32  * solely to the minimum extent necessary to exercise the above copyright
33  * license, and in no event shall the patent license extend to any additions
34  * to or modifications of the Original Intel Code. No other license or right
35  * is granted directly or by implication, estoppel or otherwise;
36  *
37  * The above copyright and patent license is granted only if the following
38  * conditions are met:
39  *
40  * 3. Conditions
41  *
42  * 3.1. Redistribution of Source with Rights to Further Distribute Source.
43  * Redistribution of source code of any substantial portion of the Covered
44  * Code or modification with rights to further distribute source must include
45  * the above Copyright Notice, the above License, this list of Conditions,
46  * and the following Disclaimer and Export Compliance provision. In addition,
47  * Licensee must cause all Covered Code to which Licensee contributes to
48  * contain a file documenting the changes Licensee made to create that Covered
49  * Code and the date of any change. Licensee must include in that file the
50  * documentation of any changes made by any predecessor Licensee. Licensee
51  * must include a prominent statement that the modification is derived,
52  * directly or indirectly, from Original Intel Code.
53  *
54  * 3.2. Redistribution of Source with no Rights to Further Distribute Source.
55  * Redistribution of source code of any substantial portion of the Covered
56  * Code or modification without rights to further distribute source must
57  * include the following Disclaimer and Export Compliance provision in the
58  * documentation and/or other materials provided with distribution. In
59  * addition, Licensee may not authorize further sublicense of source of any
60  * portion of the Covered Code, and must include terms to the effect that the
61  * license from Licensee to its licensee is limited to the intellectual
62  * property embodied in the software Licensee provides to its licensee, and
63  * not to intellectual property embodied in modifications its licensee may
64  * make.
65 
66  *
67  * 3.3. Redistribution of Executable. Redistribution in executable form of any
68  * substantial portion of the Covered Code or modification must reproduce the
69  * above Copyright Notice, and the following Disclaimer and Export Compliance
70  * provision in the documentation and/or other materials provided with the
71  * distribution.
72  *
73  * 3.4. Intel retains all right, title, and interest in and to the Original
74  * Intel Code.
75  *
76  * 3.5. Neither the name Intel nor any other trademark owned or controlled by
77  * Intel shall be used in advertising or otherwise to promote the sale, use or
78  * other dealings in products derived from or relating to the Covered Code
79  * without prior written authorization from Intel.
80  *
81  * 4. Disclaimer and Export Compliance
82  *
83  * 4.1. INTEL MAKES NO WARRANTY OF ANY KIND REGARDING ANY SOFTWARE PROVIDED
84  * HERE. ANY SOFTWARE ORIGINATING FROM INTEL OR DERIVED FROM INTEL SOFTWARE
85  * IS PROVIDED "AS IS," AND INTEL WILL NOT PROVIDE ANY SUPPORT, ASSISTANCE,
86  * INSTALLATION, TRAINING OR OTHER SERVICES. INTEL WILL NOT PROVIDE ANY
87  * UPDATES, ENHANCEMENTS OR EXTENSIONS. INTEL SPECIFICALLY DISCLAIMS ANY
88  * IMPLIED WARRANTIES OF MERCHANTABILITY, NONINFRINGEMENT AND FITNESS FOR A
89  * PARTICULAR PURPOSE.
90  *
91  * 4.2. IN NO EVENT SHALL INTEL HAVE ANY LIABILITY TO LICENSEE, ITS LICENSEES
92  * OR ANY OTHER THIRD PARTY, FOR ANY LOST PROFITS, LOST DATA, LOSS OF USE OR
93  * COSTS OF PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, OR FOR ANY INDIRECT,
94  * SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THIS AGREEMENT, UNDER ANY
95  * CAUSE OF ACTION OR THEORY OF LIABILITY, AND IRRESPECTIVE OF WHETHER INTEL
96  * HAS ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES. THESE LIMITATIONS
97  * SHALL APPLY NOTWITHSTANDING THE FAILURE OF THE ESSENTIAL PURPOSE OF ANY
98  * LIMITED REMEDY.
99  *
100  * 4.3. Licensee shall not export, either directly or indirectly, any of this
101  * software or system incorporating such software without first obtaining any
102  * required license or other approval from the U. S. Department of Commerce or
103  * any other agency or department of the United States Government. In the
104  * event Licensee exports any such software from the United States or
105  * re-exports any such software from a foreign destination, Licensee shall
106  * ensure that the distribution and export/re-export of the software is in
107  * compliance with all laws, regulations, orders, or other restrictions of the
108  * U.S. Export Administration Regulations. Licensee agrees that neither it nor
109  * any of its subsidiaries will export/re-export any technical data, process,
110  * software, or service, directly or indirectly, to any country for which the
111  * United States government or any agency thereof requires an export license,
112  * other governmental approval, or letter of assurance, without first obtaining
113  * such license, approval or letter.
114  *
115  *****************************************************************************
116  *
117  * Alternatively, you may choose to be licensed under the terms of the
118  * following license:
119  *
120  * Redistribution and use in source and binary forms, with or without
121  * modification, are permitted provided that the following conditions
122  * are met:
123  * 1. Redistributions of source code must retain the above copyright
124  *    notice, this list of conditions, and the following disclaimer,
125  *    without modification.
126  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
127  *    substantially similar to the "NO WARRANTY" disclaimer below
128  *    ("Disclaimer") and any redistribution must be conditioned upon
129  *    including a substantially similar Disclaimer requirement for further
130  *    binary redistribution.
131  * 3. Neither the names of the above-listed copyright holders nor the names
132  *    of any contributors may be used to endorse or promote products derived
133  *    from this software without specific prior written permission.
134  *
135  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
136  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
137  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
138  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
139  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
140  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
141  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
142  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
143  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
144  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
145  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
146  *
147  * Alternatively, you may choose to be licensed under the terms of the
148  * GNU General Public License ("GPL") version 2 as published by the Free
149  * Software Foundation.
150  *
151  *****************************************************************************/
152 
153 #include <contrib/dev/acpica/include/acpi.h>
154 #include <contrib/dev/acpica/include/accommon.h>
155 #include <contrib/dev/acpica/include/acparser.h>
156 #include <contrib/dev/acpica/include/acinterp.h>
157 #include <contrib/dev/acpica/include/acnamesp.h>
158 
159 
160 #define _COMPONENT          ACPI_NAMESPACE
161         ACPI_MODULE_NAME    ("nseval")
162 
163 
164 /*******************************************************************************
165  *
166  * FUNCTION:    AcpiNsEvaluate
167  *
168  * PARAMETERS:  Info            - Evaluation info block, contains these fields
169  *                                and more:
170  *                  PrefixNode      - Prefix or Method/Object Node to execute
171  *                  RelativePath    - Name of method to execute, If NULL, the
172  *                                    Node is the object to execute
173  *                  Parameters      - List of parameters to pass to the method,
174  *                                    terminated by NULL. Params itself may be
175  *                                    NULL if no parameters are being passed.
176  *                  ParameterType   - Type of Parameter list
177  *                  ReturnObject    - Where to put method's return value (if
178  *                                    any). If NULL, no value is returned.
179  *                  Flags           - ACPI_IGNORE_RETURN_VALUE to delete return
180  *
181  * RETURN:      Status
182  *
183  * DESCRIPTION: Execute a control method or return the current value of an
184  *              ACPI namespace object.
185  *
186  * MUTEX:       Locks interpreter
187  *
188  ******************************************************************************/
189 
190 ACPI_STATUS
191 AcpiNsEvaluate (
192     ACPI_EVALUATE_INFO      *Info)
193 {
194     ACPI_STATUS             Status;
195 
196 
197     ACPI_FUNCTION_TRACE (NsEvaluate);
198 
199 
200     if (!Info)
201     {
202         return_ACPI_STATUS (AE_BAD_PARAMETER);
203     }
204 
205     if (!Info->Node)
206     {
207         /*
208          * Get the actual namespace node for the target object if we
209          * need to. Handles these cases:
210          *
211          * 1) Null node, valid pathname from root (absolute path)
212          * 2) Node and valid pathname (path relative to Node)
213          * 3) Node, Null pathname
214          */
215         Status = AcpiNsGetNode (Info->PrefixNode, Info->RelativePathname,
216             ACPI_NS_NO_UPSEARCH, &Info->Node);
217         if (ACPI_FAILURE (Status))
218         {
219             return_ACPI_STATUS (Status);
220         }
221     }
222 
223     /*
224      * For a method alias, we must grab the actual method node so that
225      * proper scoping context will be established before execution.
226      */
227     if (AcpiNsGetType (Info->Node) == ACPI_TYPE_LOCAL_METHOD_ALIAS)
228     {
229         Info->Node = ACPI_CAST_PTR (
230             ACPI_NAMESPACE_NODE, Info->Node->Object);
231     }
232 
233     /* Complete the info block initialization */
234 
235     Info->ReturnObject = NULL;
236     Info->NodeFlags = Info->Node->Flags;
237     Info->ObjDesc = AcpiNsGetAttachedObject (Info->Node);
238 
239     ACPI_DEBUG_PRINT ((ACPI_DB_NAMES, "%s [%p] Value %p\n",
240         Info->RelativePathname, Info->Node,
241         AcpiNsGetAttachedObject (Info->Node)));
242 
243     /* Get info if we have a predefined name (_HID, etc.) */
244 
245     Info->Predefined = AcpiUtMatchPredefinedMethod (Info->Node->Name.Ascii);
246 
247     /* Get the full pathname to the object, for use in warning messages */
248 
249     Info->FullPathname = AcpiNsGetNormalizedPathname (Info->Node, TRUE);
250     if (!Info->FullPathname)
251     {
252         return_ACPI_STATUS (AE_NO_MEMORY);
253     }
254 
255     /* Optional object evaluation log */
256 
257     ACPI_DEBUG_PRINT_RAW ((ACPI_DB_EVALUATION,
258         "%-26s:  %s (%s)\n", "   Enter evaluation",
259         &Info->FullPathname[1], AcpiUtGetTypeName (Info->Node->Type)));
260 
261     /* Count the number of arguments being passed in */
262 
263     Info->ParamCount = 0;
264     if (Info->Parameters)
265     {
266         while (Info->Parameters[Info->ParamCount])
267         {
268             Info->ParamCount++;
269         }
270 
271         /* Warn on impossible argument count */
272 
273         if (Info->ParamCount > ACPI_METHOD_NUM_ARGS)
274         {
275             ACPI_WARN_PREDEFINED ((AE_INFO, Info->FullPathname, ACPI_WARN_ALWAYS,
276                 "Excess arguments (%u) - using only %u",
277                 Info->ParamCount, ACPI_METHOD_NUM_ARGS));
278 
279             Info->ParamCount = ACPI_METHOD_NUM_ARGS;
280         }
281     }
282 
283     /*
284      * For predefined names: Check that the declared argument count
285      * matches the ACPI spec -- otherwise this is a BIOS error.
286      */
287     AcpiNsCheckAcpiCompliance (Info->FullPathname, Info->Node,
288         Info->Predefined);
289 
290     /*
291      * For all names: Check that the incoming argument count for
292      * this method/object matches the actual ASL/AML definition.
293      */
294     AcpiNsCheckArgumentCount (Info->FullPathname, Info->Node,
295         Info->ParamCount, Info->Predefined);
296 
297     /* For predefined names: Typecheck all incoming arguments */
298 
299     AcpiNsCheckArgumentTypes (Info);
300 
301     /*
302      * Three major evaluation cases:
303      *
304      * 1) Object types that cannot be evaluated by definition
305      * 2) The object is a control method -- execute it
306      * 3) The object is not a method -- just return it's current value
307      */
308     switch (AcpiNsGetType (Info->Node))
309     {
310     case ACPI_TYPE_ANY:
311     case ACPI_TYPE_DEVICE:
312     case ACPI_TYPE_EVENT:
313     case ACPI_TYPE_MUTEX:
314     case ACPI_TYPE_REGION:
315     case ACPI_TYPE_THERMAL:
316     case ACPI_TYPE_LOCAL_SCOPE:
317         /*
318          * 1) Disallow evaluation of these object types. For these,
319          *    object evaluation is undefined.
320          */
321         ACPI_ERROR ((AE_INFO,
322             "%s: This object type [%s] "
323             "never contains data and cannot be evaluated",
324             Info->FullPathname, AcpiUtGetTypeName (Info->Node->Type)));
325 
326         Status = AE_TYPE;
327         goto Cleanup;
328 
329     case ACPI_TYPE_METHOD:
330         /*
331          * 2) Object is a control method - execute it
332          */
333 
334         /* Verify that there is a method object associated with this node */
335 
336         if (!Info->ObjDesc)
337         {
338             ACPI_ERROR ((AE_INFO, "%s: Method has no attached sub-object",
339                 Info->FullPathname));
340             Status = AE_NULL_OBJECT;
341             goto Cleanup;
342         }
343 
344         ACPI_DEBUG_PRINT ((ACPI_DB_EXEC,
345             "**** Execute method [%s] at AML address %p length %X\n",
346             Info->FullPathname,
347             Info->ObjDesc->Method.AmlStart + 1,
348             Info->ObjDesc->Method.AmlLength - 1));
349 
350         /*
351          * Any namespace deletion must acquire both the namespace and
352          * interpreter locks to ensure that no thread is using the portion of
353          * the namespace that is being deleted.
354          *
355          * Execute the method via the interpreter. The interpreter is locked
356          * here before calling into the AML parser
357          */
358         AcpiExEnterInterpreter ();
359         Status = AcpiPsExecuteMethod (Info);
360         AcpiExExitInterpreter ();
361         break;
362 
363     default:
364         /*
365          * 3) All other non-method objects -- get the current object value
366          */
367 
368         /*
369          * Some objects require additional resolution steps (e.g., the Node
370          * may be a field that must be read, etc.) -- we can't just grab
371          * the object out of the node.
372          *
373          * Use ResolveNodeToValue() to get the associated value.
374          *
375          * NOTE: we can get away with passing in NULL for a walk state because
376          * the Node is guaranteed to not be a reference to either a method
377          * local or a method argument (because this interface is never called
378          * from a running method.)
379          *
380          * Even though we do not directly invoke the interpreter for object
381          * resolution, we must lock it because we could access an OpRegion.
382          * The OpRegion access code assumes that the interpreter is locked.
383          */
384         AcpiExEnterInterpreter ();
385 
386         /* TBD: ResolveNodeToValue has a strange interface, fix */
387 
388         Info->ReturnObject = ACPI_CAST_PTR (ACPI_OPERAND_OBJECT, Info->Node);
389 
390         Status = AcpiExResolveNodeToValue (ACPI_CAST_INDIRECT_PTR (
391             ACPI_NAMESPACE_NODE, &Info->ReturnObject), NULL);
392         AcpiExExitInterpreter ();
393 
394         if (ACPI_FAILURE (Status))
395         {
396             Info->ReturnObject = NULL;
397             goto Cleanup;
398         }
399 
400         ACPI_DEBUG_PRINT ((ACPI_DB_NAMES, "Returned object %p [%s]\n",
401             Info->ReturnObject,
402             AcpiUtGetObjectTypeName (Info->ReturnObject)));
403 
404         Status = AE_CTRL_RETURN_VALUE; /* Always has a "return value" */
405         break;
406     }
407 
408     /*
409      * For predefined names, check the return value against the ACPI
410      * specification. Some incorrect return value types are repaired.
411      */
412     (void) AcpiNsCheckReturnValue (Info->Node, Info, Info->ParamCount,
413         Status, &Info->ReturnObject);
414 
415     /* Check if there is a return value that must be dealt with */
416 
417     if (Status == AE_CTRL_RETURN_VALUE)
418     {
419         /* If caller does not want the return value, delete it */
420 
421         if (Info->Flags & ACPI_IGNORE_RETURN_VALUE)
422         {
423             AcpiUtRemoveReference (Info->ReturnObject);
424             Info->ReturnObject = NULL;
425         }
426 
427         /* Map AE_CTRL_RETURN_VALUE to AE_OK, we are done with it */
428 
429         Status = AE_OK;
430     }
431     else if (ACPI_FAILURE(Status))
432     {
433         /* If ReturnObject exists, delete it */
434 
435         if (Info->ReturnObject)
436         {
437             AcpiUtRemoveReference (Info->ReturnObject);
438             Info->ReturnObject = NULL;
439         }
440     }
441 
442     ACPI_DEBUG_PRINT ((ACPI_DB_NAMES,
443         "*** Completed evaluation of object %s ***\n",
444         Info->RelativePathname));
445 
446 Cleanup:
447     /* Optional object evaluation log */
448 
449     ACPI_DEBUG_PRINT_RAW ((ACPI_DB_EVALUATION,
450         "%-26s:  %s\n", "   Exit evaluation",
451         &Info->FullPathname[1]));
452 
453     /*
454      * Namespace was unlocked by the handling AcpiNs* function, so we
455      * just free the pathname and return
456      */
457     ACPI_FREE (Info->FullPathname);
458     Info->FullPathname = NULL;
459     return_ACPI_STATUS (Status);
460 }
461