xref: /netbsd-src/sys/external/bsd/acpica/dist/namespace/nseval.c (revision bdc22b2e01993381dcefeff2bc9b56ca75a4235c)
1 /*******************************************************************************
2  *
3  * Module Name: nseval - Object evaluation, includes control method execution
4  *
5  ******************************************************************************/
6 
7 /*
8  * Copyright (C) 2000 - 2018, Intel Corp.
9  * All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions, and the following disclaimer,
16  *    without modification.
17  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
18  *    substantially similar to the "NO WARRANTY" disclaimer below
19  *    ("Disclaimer") and any redistribution must be conditioned upon
20  *    including a substantially similar Disclaimer requirement for further
21  *    binary redistribution.
22  * 3. Neither the names of the above-listed copyright holders nor the names
23  *    of any contributors may be used to endorse or promote products derived
24  *    from this software without specific prior written permission.
25  *
26  * Alternatively, this software may be distributed under the terms of the
27  * GNU General Public License ("GPL") version 2 as published by the Free
28  * Software Foundation.
29  *
30  * NO WARRANTY
31  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
34  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
40  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41  * POSSIBILITY OF SUCH DAMAGES.
42  */
43 
44 #include "acpi.h"
45 #include "accommon.h"
46 #include "acparser.h"
47 #include "acinterp.h"
48 #include "acnamesp.h"
49 
50 
51 #define _COMPONENT          ACPI_NAMESPACE
52         ACPI_MODULE_NAME    ("nseval")
53 
54 /* Local prototypes */
55 
56 static void
57 AcpiNsExecModuleCode (
58     ACPI_OPERAND_OBJECT     *MethodObj,
59     ACPI_EVALUATE_INFO      *Info);
60 
61 
62 /*******************************************************************************
63  *
64  * FUNCTION:    AcpiNsEvaluate
65  *
66  * PARAMETERS:  Info            - Evaluation info block, contains these fields
67  *                                and more:
68  *                  PrefixNode      - Prefix or Method/Object Node to execute
69  *                  RelativePath    - Name of method to execute, If NULL, the
70  *                                    Node is the object to execute
71  *                  Parameters      - List of parameters to pass to the method,
72  *                                    terminated by NULL. Params itself may be
73  *                                    NULL if no parameters are being passed.
74  *                  ParameterType   - Type of Parameter list
75  *                  ReturnObject    - Where to put method's return value (if
76  *                                    any). If NULL, no value is returned.
77  *                  Flags           - ACPI_IGNORE_RETURN_VALUE to delete return
78  *
79  * RETURN:      Status
80  *
81  * DESCRIPTION: Execute a control method or return the current value of an
82  *              ACPI namespace object.
83  *
84  * MUTEX:       Locks interpreter
85  *
86  ******************************************************************************/
87 
88 ACPI_STATUS
89 AcpiNsEvaluate (
90     ACPI_EVALUATE_INFO      *Info)
91 {
92     ACPI_STATUS             Status;
93 
94 
95     ACPI_FUNCTION_TRACE (NsEvaluate);
96 
97 
98     if (!Info)
99     {
100         return_ACPI_STATUS (AE_BAD_PARAMETER);
101     }
102 
103     if (!Info->Node)
104     {
105         /*
106          * Get the actual namespace node for the target object if we
107          * need to. Handles these cases:
108          *
109          * 1) Null node, valid pathname from root (absolute path)
110          * 2) Node and valid pathname (path relative to Node)
111          * 3) Node, Null pathname
112          */
113         Status = AcpiNsGetNode (Info->PrefixNode, Info->RelativePathname,
114             ACPI_NS_NO_UPSEARCH, &Info->Node);
115         if (ACPI_FAILURE (Status))
116         {
117             return_ACPI_STATUS (Status);
118         }
119     }
120 
121     /*
122      * For a method alias, we must grab the actual method node so that
123      * proper scoping context will be established before execution.
124      */
125     if (AcpiNsGetType (Info->Node) == ACPI_TYPE_LOCAL_METHOD_ALIAS)
126     {
127         Info->Node = ACPI_CAST_PTR (
128             ACPI_NAMESPACE_NODE, Info->Node->Object);
129     }
130 
131     /* Complete the info block initialization */
132 
133     Info->ReturnObject = NULL;
134     Info->NodeFlags = Info->Node->Flags;
135     Info->ObjDesc = AcpiNsGetAttachedObject (Info->Node);
136 
137     ACPI_DEBUG_PRINT ((ACPI_DB_NAMES, "%s [%p] Value %p\n",
138         Info->RelativePathname, Info->Node,
139         AcpiNsGetAttachedObject (Info->Node)));
140 
141     /* Get info if we have a predefined name (_HID, etc.) */
142 
143     Info->Predefined = AcpiUtMatchPredefinedMethod (Info->Node->Name.Ascii);
144 
145     /* Get the full pathname to the object, for use in warning messages */
146 
147     Info->FullPathname = AcpiNsGetNormalizedPathname (Info->Node, TRUE);
148     if (!Info->FullPathname)
149     {
150         return_ACPI_STATUS (AE_NO_MEMORY);
151     }
152 
153     /* Count the number of arguments being passed in */
154 
155     Info->ParamCount = 0;
156     if (Info->Parameters)
157     {
158         while (Info->Parameters[Info->ParamCount])
159         {
160             Info->ParamCount++;
161         }
162 
163         /* Warn on impossible argument count */
164 
165         if (Info->ParamCount > ACPI_METHOD_NUM_ARGS)
166         {
167             ACPI_WARN_PREDEFINED ((AE_INFO, Info->FullPathname, ACPI_WARN_ALWAYS,
168                 "Excess arguments (%u) - using only %u",
169                 Info->ParamCount, ACPI_METHOD_NUM_ARGS));
170 
171             Info->ParamCount = ACPI_METHOD_NUM_ARGS;
172         }
173     }
174 
175     /*
176      * For predefined names: Check that the declared argument count
177      * matches the ACPI spec -- otherwise this is a BIOS error.
178      */
179     AcpiNsCheckAcpiCompliance (Info->FullPathname, Info->Node,
180         Info->Predefined);
181 
182     /*
183      * For all names: Check that the incoming argument count for
184      * this method/object matches the actual ASL/AML definition.
185      */
186     AcpiNsCheckArgumentCount (Info->FullPathname, Info->Node,
187         Info->ParamCount, Info->Predefined);
188 
189     /* For predefined names: Typecheck all incoming arguments */
190 
191     AcpiNsCheckArgumentTypes (Info);
192 
193     /*
194      * Three major evaluation cases:
195      *
196      * 1) Object types that cannot be evaluated by definition
197      * 2) The object is a control method -- execute it
198      * 3) The object is not a method -- just return it's current value
199      */
200     switch (AcpiNsGetType (Info->Node))
201     {
202     case ACPI_TYPE_ANY:
203     case ACPI_TYPE_DEVICE:
204     case ACPI_TYPE_EVENT:
205     case ACPI_TYPE_MUTEX:
206     case ACPI_TYPE_REGION:
207     case ACPI_TYPE_THERMAL:
208     case ACPI_TYPE_LOCAL_SCOPE:
209         /*
210          * 1) Disallow evaluation of these object types. For these,
211          *    object evaluation is undefined.
212          */
213         ACPI_ERROR ((AE_INFO,
214             "%s: This object type [%s] "
215             "never contains data and cannot be evaluated",
216             Info->FullPathname, AcpiUtGetTypeName (Info->Node->Type)));
217 
218         Status = AE_TYPE;
219         goto Cleanup;
220 
221     case ACPI_TYPE_METHOD:
222         /*
223          * 2) Object is a control method - execute it
224          */
225 
226         /* Verify that there is a method object associated with this node */
227 
228         if (!Info->ObjDesc)
229         {
230             ACPI_ERROR ((AE_INFO, "%s: Method has no attached sub-object",
231                 Info->FullPathname));
232             Status = AE_NULL_OBJECT;
233             goto Cleanup;
234         }
235 
236         ACPI_DEBUG_PRINT ((ACPI_DB_EXEC,
237             "**** Execute method [%s] at AML address %p length %X\n",
238             Info->FullPathname,
239             Info->ObjDesc->Method.AmlStart + 1,
240             Info->ObjDesc->Method.AmlLength - 1));
241 
242         /*
243          * Any namespace deletion must acquire both the namespace and
244          * interpreter locks to ensure that no thread is using the portion of
245          * the namespace that is being deleted.
246          *
247          * Execute the method via the interpreter. The interpreter is locked
248          * here before calling into the AML parser
249          */
250         AcpiExEnterInterpreter ();
251         Status = AcpiPsExecuteMethod (Info);
252         AcpiExExitInterpreter ();
253         break;
254 
255     default:
256         /*
257          * 3) All other non-method objects -- get the current object value
258          */
259 
260         /*
261          * Some objects require additional resolution steps (e.g., the Node
262          * may be a field that must be read, etc.) -- we can't just grab
263          * the object out of the node.
264          *
265          * Use ResolveNodeToValue() to get the associated value.
266          *
267          * NOTE: we can get away with passing in NULL for a walk state because
268          * the Node is guaranteed to not be a reference to either a method
269          * local or a method argument (because this interface is never called
270          * from a running method.)
271          *
272          * Even though we do not directly invoke the interpreter for object
273          * resolution, we must lock it because we could access an OpRegion.
274          * The OpRegion access code assumes that the interpreter is locked.
275          */
276         AcpiExEnterInterpreter ();
277 
278         /* TBD: ResolveNodeToValue has a strange interface, fix */
279 
280         Info->ReturnObject = ACPI_CAST_PTR (ACPI_OPERAND_OBJECT, Info->Node);
281 
282         Status = AcpiExResolveNodeToValue (ACPI_CAST_INDIRECT_PTR (
283             ACPI_NAMESPACE_NODE, &Info->ReturnObject), NULL);
284         AcpiExExitInterpreter ();
285 
286         if (ACPI_FAILURE (Status))
287         {
288             Info->ReturnObject = NULL;
289             goto Cleanup;
290         }
291 
292         ACPI_DEBUG_PRINT ((ACPI_DB_NAMES, "Returned object %p [%s]\n",
293             Info->ReturnObject,
294             AcpiUtGetObjectTypeName (Info->ReturnObject)));
295 
296         Status = AE_CTRL_RETURN_VALUE; /* Always has a "return value" */
297         break;
298     }
299 
300     /*
301      * For predefined names, check the return value against the ACPI
302      * specification. Some incorrect return value types are repaired.
303      */
304     (void) AcpiNsCheckReturnValue (Info->Node, Info, Info->ParamCount,
305         Status, &Info->ReturnObject);
306 
307     /* Check if there is a return value that must be dealt with */
308 
309     if (Status == AE_CTRL_RETURN_VALUE)
310     {
311         /* If caller does not want the return value, delete it */
312 
313         if (Info->Flags & ACPI_IGNORE_RETURN_VALUE)
314         {
315             AcpiUtRemoveReference (Info->ReturnObject);
316             Info->ReturnObject = NULL;
317         }
318 
319         /* Map AE_CTRL_RETURN_VALUE to AE_OK, we are done with it */
320 
321         Status = AE_OK;
322     }
323     else if (ACPI_FAILURE(Status))
324     {
325         /* If ReturnObject exists, delete it */
326 
327         if (Info->ReturnObject)
328         {
329             AcpiUtRemoveReference (Info->ReturnObject);
330             Info->ReturnObject = NULL;
331         }
332     }
333 
334     ACPI_DEBUG_PRINT ((ACPI_DB_NAMES,
335         "*** Completed evaluation of object %s ***\n",
336         Info->RelativePathname));
337 
338 Cleanup:
339     /*
340      * Namespace was unlocked by the handling AcpiNs* function, so we
341      * just free the pathname and return
342      */
343     ACPI_FREE (Info->FullPathname);
344     Info->FullPathname = NULL;
345     return_ACPI_STATUS (Status);
346 }
347 
348 
349 /*******************************************************************************
350  *
351  * FUNCTION:    AcpiNsExecModuleCodeList
352  *
353  * PARAMETERS:  None
354  *
355  * RETURN:      None. Exceptions during method execution are ignored, since
356  *              we cannot abort a table load.
357  *
358  * DESCRIPTION: Execute all elements of the global module-level code list.
359  *              Each element is executed as a single control method.
360  *
361  * NOTE: With this option enabled, each block of detected executable AML
362  * code that is outside of any control method is wrapped with a temporary
363  * control method object and placed on a global list. The methods on this
364  * list are executed below.
365  *
366  * This function executes the module-level code for all tables only after
367  * all of the tables have been loaded. It is a legacy option and is
368  * not compatible with other ACPI implementations. See AcpiNsLoadTable.
369  *
370  * This function will be removed when the legacy option is removed.
371  *
372  ******************************************************************************/
373 
374 void
375 AcpiNsExecModuleCodeList (
376     void)
377 {
378     ACPI_OPERAND_OBJECT     *Prev;
379     ACPI_OPERAND_OBJECT     *Next;
380     ACPI_EVALUATE_INFO      *Info;
381     UINT32                  MethodCount = 0;
382 
383 
384     ACPI_FUNCTION_TRACE (NsExecModuleCodeList);
385 
386 
387     /* Exit now if the list is empty */
388 
389     Next = AcpiGbl_ModuleCodeList;
390     if (!Next)
391     {
392         ACPI_DEBUG_PRINT ((ACPI_DB_INIT_NAMES,
393             "Legacy MLC block list is empty\n"));
394 
395         return_VOID;
396     }
397 
398     /* Allocate the evaluation information block */
399 
400     Info = ACPI_ALLOCATE (sizeof (ACPI_EVALUATE_INFO));
401     if (!Info)
402     {
403         return_VOID;
404     }
405 
406     /* Walk the list, executing each "method" */
407 
408     while (Next)
409     {
410         Prev = Next;
411         Next = Next->Method.Mutex;
412 
413         /* Clear the link field and execute the method */
414 
415         Prev->Method.Mutex = NULL;
416         AcpiNsExecModuleCode (Prev, Info);
417         MethodCount++;
418 
419         /* Delete the (temporary) method object */
420 
421         AcpiUtRemoveReference (Prev);
422     }
423 
424     ACPI_INFO ((
425         "Executed %u blocks of module-level executable AML code",
426         MethodCount));
427 
428     ACPI_FREE (Info);
429     AcpiGbl_ModuleCodeList = NULL;
430     return_VOID;
431 }
432 
433 
434 /*******************************************************************************
435  *
436  * FUNCTION:    AcpiNsExecModuleCode
437  *
438  * PARAMETERS:  MethodObj           - Object container for the module-level code
439  *              Info                - Info block for method evaluation
440  *
441  * RETURN:      None. Exceptions during method execution are ignored, since
442  *              we cannot abort a table load.
443  *
444  * DESCRIPTION: Execute a control method containing a block of module-level
445  *              executable AML code. The control method is temporarily
446  *              installed to the root node, then evaluated.
447  *
448  ******************************************************************************/
449 
450 static void
451 AcpiNsExecModuleCode (
452     ACPI_OPERAND_OBJECT     *MethodObj,
453     ACPI_EVALUATE_INFO      *Info)
454 {
455     ACPI_OPERAND_OBJECT     *ParentObj;
456     ACPI_NAMESPACE_NODE     *ParentNode;
457     ACPI_OBJECT_TYPE        Type;
458     ACPI_STATUS             Status;
459 
460 
461     ACPI_FUNCTION_TRACE (NsExecModuleCode);
462 
463 
464     /*
465      * Get the parent node. We cheat by using the NextObject field
466      * of the method object descriptor.
467      */
468     ParentNode = ACPI_CAST_PTR (
469         ACPI_NAMESPACE_NODE, MethodObj->Method.NextObject);
470     Type = AcpiNsGetType (ParentNode);
471 
472     /*
473      * Get the region handler and save it in the method object. We may need
474      * this if an operation region declaration causes a _REG method to be run.
475      *
476      * We can't do this in AcpiPsLinkModuleCode because
477      * AcpiGbl_RootNode->Object is NULL at PASS1.
478      */
479     if ((Type == ACPI_TYPE_DEVICE) && ParentNode->Object)
480     {
481         MethodObj->Method.Dispatch.Handler =
482             ParentNode->Object->Device.Handler;
483     }
484 
485     /* Must clear NextObject (AcpiNsAttachObject needs the field) */
486 
487     MethodObj->Method.NextObject = NULL;
488 
489     /* Initialize the evaluation information block */
490 
491     memset (Info, 0, sizeof (ACPI_EVALUATE_INFO));
492     Info->PrefixNode = ParentNode;
493 
494     /*
495      * Get the currently attached parent object. Add a reference,
496      * because the ref count will be decreased when the method object
497      * is installed to the parent node.
498      */
499     ParentObj = AcpiNsGetAttachedObject (ParentNode);
500     if (ParentObj)
501     {
502         AcpiUtAddReference (ParentObj);
503     }
504 
505     /* Install the method (module-level code) in the parent node */
506 
507     Status = AcpiNsAttachObject (ParentNode, MethodObj, ACPI_TYPE_METHOD);
508     if (ACPI_FAILURE (Status))
509     {
510         goto Exit;
511     }
512 
513     /* Execute the parent node as a control method */
514 
515     Status = AcpiNsEvaluate (Info);
516 
517     ACPI_DEBUG_PRINT ((ACPI_DB_INIT_NAMES,
518         "Executed module-level code at %p\n",
519         MethodObj->Method.AmlStart));
520 
521     /* Delete a possible implicit return value (in slack mode) */
522 
523     if (Info->ReturnObject)
524     {
525         AcpiUtRemoveReference (Info->ReturnObject);
526     }
527 
528     /* Detach the temporary method object */
529 
530     AcpiNsDetachObject (ParentNode);
531 
532     /* Restore the original parent object */
533 
534     if (ParentObj)
535     {
536         Status = AcpiNsAttachObject (ParentNode, ParentObj, Type);
537     }
538     else
539     {
540         ParentNode->Type = (UINT8) Type;
541     }
542 
543 Exit:
544     if (ParentObj)
545     {
546         AcpiUtRemoveReference (ParentObj);
547     }
548     return_VOID;
549 }
550