xref: /illumos-gate/usr/src/cmd/acpi/iasl/aslmethod.c (revision 45ede40b2394db7967e59f19288fae9b62efd4aa)
1 /******************************************************************************
2  *
3  * Module Name: aslmethod.c - Control method analysis walk
4  *
5  *****************************************************************************/
6 
7 /******************************************************************************
8  *
9  * 1. Copyright Notice
10  *
11  * Some or all of this work - Copyright (c) 1999 - 2018, 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  * 3.3. Redistribution of Executable. Redistribution in executable form of any
67  * substantial portion of the Covered Code or modification must reproduce the
68  * above Copyright Notice, and the following Disclaimer and Export Compliance
69  * provision in the documentation and/or other materials provided with the
70  * distribution.
71  *
72  * 3.4. Intel retains all right, title, and interest in and to the Original
73  * Intel Code.
74  *
75  * 3.5. Neither the name Intel nor any other trademark owned or controlled by
76  * Intel shall be used in advertising or otherwise to promote the sale, use or
77  * other dealings in products derived from or relating to the Covered Code
78  * without prior written authorization from Intel.
79  *
80  * 4. Disclaimer and Export Compliance
81  *
82  * 4.1. INTEL MAKES NO WARRANTY OF ANY KIND REGARDING ANY SOFTWARE PROVIDED
83  * HERE. ANY SOFTWARE ORIGINATING FROM INTEL OR DERIVED FROM INTEL SOFTWARE
84  * IS PROVIDED "AS IS," AND INTEL WILL NOT PROVIDE ANY SUPPORT, ASSISTANCE,
85  * INSTALLATION, TRAINING OR OTHER SERVICES. INTEL WILL NOT PROVIDE ANY
86  * UPDATES, ENHANCEMENTS OR EXTENSIONS. INTEL SPECIFICALLY DISCLAIMS ANY
87  * IMPLIED WARRANTIES OF MERCHANTABILITY, NONINFRINGEMENT AND FITNESS FOR A
88  * PARTICULAR PURPOSE.
89  *
90  * 4.2. IN NO EVENT SHALL INTEL HAVE ANY LIABILITY TO LICENSEE, ITS LICENSEES
91  * OR ANY OTHER THIRD PARTY, FOR ANY LOST PROFITS, LOST DATA, LOSS OF USE OR
92  * COSTS OF PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, OR FOR ANY INDIRECT,
93  * SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THIS AGREEMENT, UNDER ANY
94  * CAUSE OF ACTION OR THEORY OF LIABILITY, AND IRRESPECTIVE OF WHETHER INTEL
95  * HAS ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES. THESE LIMITATIONS
96  * SHALL APPLY NOTWITHSTANDING THE FAILURE OF THE ESSENTIAL PURPOSE OF ANY
97  * LIMITED REMEDY.
98  *
99  * 4.3. Licensee shall not export, either directly or indirectly, any of this
100  * software or system incorporating such software without first obtaining any
101  * required license or other approval from the U. S. Department of Commerce or
102  * any other agency or department of the United States Government. In the
103  * event Licensee exports any such software from the United States or
104  * re-exports any such software from a foreign destination, Licensee shall
105  * ensure that the distribution and export/re-export of the software is in
106  * compliance with all laws, regulations, orders, or other restrictions of the
107  * U.S. Export Administration Regulations. Licensee agrees that neither it nor
108  * any of its subsidiaries will export/re-export any technical data, process,
109  * software, or service, directly or indirectly, to any country for which the
110  * United States government or any agency thereof requires an export license,
111  * other governmental approval, or letter of assurance, without first obtaining
112  * such license, approval or letter.
113  *
114  *****************************************************************************
115  *
116  * Alternatively, you may choose to be licensed under the terms of the
117  * following license:
118  *
119  * Redistribution and use in source and binary forms, with or without
120  * modification, are permitted provided that the following conditions
121  * are met:
122  * 1. Redistributions of source code must retain the above copyright
123  *    notice, this list of conditions, and the following disclaimer,
124  *    without modification.
125  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
126  *    substantially similar to the "NO WARRANTY" disclaimer below
127  *    ("Disclaimer") and any redistribution must be conditioned upon
128  *    including a substantially similar Disclaimer requirement for further
129  *    binary redistribution.
130  * 3. Neither the names of the above-listed copyright holders nor the names
131  *    of any contributors may be used to endorse or promote products derived
132  *    from this software without specific prior written permission.
133  *
134  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
135  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
136  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
137  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
138  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
139  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
140  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
141  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
142  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
143  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
144  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
145  *
146  * Alternatively, you may choose to be licensed under the terms of the
147  * GNU General Public License ("GPL") version 2 as published by the Free
148  * Software Foundation.
149  *
150  *****************************************************************************/
151 
152 #include "aslcompiler.h"
153 #include "aslcompiler.y.h"
154 #include "acparser.h"
155 #include "amlcode.h"
156 
157 
158 #define _COMPONENT          ACPI_COMPILER
159         ACPI_MODULE_NAME    ("aslmethod")
160 
161 
162 /* Local prototypes */
163 
164 static void
165 MtCheckNamedObjectInMethod (
166     ACPI_PARSE_OBJECT       *Op,
167     ASL_METHOD_INFO         *MethodInfo);
168 
169 
170 /*******************************************************************************
171  *
172  * FUNCTION:    MtMethodAnalysisWalkBegin
173  *
174  * PARAMETERS:  ASL_WALK_CALLBACK
175  *
176  * RETURN:      Status
177  *
178  * DESCRIPTION: Descending callback for the analysis walk. Check methods for:
179  *              1) Initialized local variables
180  *              2) Valid arguments
181  *              3) Return types
182  *
183  ******************************************************************************/
184 
185 ACPI_STATUS
186 MtMethodAnalysisWalkBegin (
187     ACPI_PARSE_OBJECT       *Op,
188     UINT32                  Level,
189     void                    *Context)
190 {
191     ASL_ANALYSIS_WALK_INFO  *WalkInfo = (ASL_ANALYSIS_WALK_INFO *) Context;
192     ASL_METHOD_INFO         *MethodInfo = WalkInfo->MethodStack;
193     ACPI_PARSE_OBJECT       *Next;
194     UINT32                  RegisterNumber;
195     UINT32                  i;
196     char                    LocalName[] = "Local0";
197     char                    ArgName[] = "Arg0";
198     ACPI_PARSE_OBJECT       *ArgNode;
199     ACPI_PARSE_OBJECT       *NextType;
200     ACPI_PARSE_OBJECT       *NextParamType;
201     UINT8                   ActualArgs = 0;
202 
203 
204     /* Build cross-reference output file if requested */
205 
206     if (Gbl_CrossReferenceOutput)
207     {
208         OtXrefWalkPart1 (Op, Level, MethodInfo);
209     }
210 
211     switch (Op->Asl.ParseOpcode)
212     {
213     case PARSEOP_METHOD:
214 
215         TotalMethods++;
216 
217         /* Create and init method info */
218 
219         MethodInfo = UtLocalCalloc (sizeof (ASL_METHOD_INFO));
220         MethodInfo->Next = WalkInfo->MethodStack;
221         MethodInfo->Op = Op;
222 
223         WalkInfo->MethodStack = MethodInfo;
224 
225         /*
226          * Special handling for _PSx methods. Dependency rules (same scope):
227          *
228          * 1) _PS0 - One of these must exist: _PS1, _PS2, _PS3
229          * 2) _PS1/_PS2/_PS3: A _PS0 must exist
230          */
231         if (ACPI_COMPARE_NAME (METHOD_NAME__PS0, Op->Asl.NameSeg))
232         {
233             /* For _PS0, one of _PS1/_PS2/_PS3 must exist */
234 
235             if ((!ApFindNameInScope (METHOD_NAME__PS1, Op)) &&
236                 (!ApFindNameInScope (METHOD_NAME__PS2, Op)) &&
237                 (!ApFindNameInScope (METHOD_NAME__PS3, Op)))
238             {
239                 AslError (ASL_WARNING, ASL_MSG_MISSING_DEPENDENCY, Op,
240                     "_PS0 requires one of _PS1/_PS2/_PS3 in same scope");
241             }
242         }
243         else if (
244             ACPI_COMPARE_NAME (METHOD_NAME__PS1, Op->Asl.NameSeg) ||
245             ACPI_COMPARE_NAME (METHOD_NAME__PS2, Op->Asl.NameSeg) ||
246             ACPI_COMPARE_NAME (METHOD_NAME__PS3, Op->Asl.NameSeg))
247         {
248             /* For _PS1/_PS2/_PS3, a _PS0 must exist */
249 
250             if (!ApFindNameInScope (METHOD_NAME__PS0, Op))
251             {
252                 sprintf (MsgBuffer,
253                     "%4.4s requires _PS0 in same scope", Op->Asl.NameSeg);
254 
255                 AslError (ASL_WARNING, ASL_MSG_MISSING_DEPENDENCY, Op,
256                     MsgBuffer);
257             }
258         }
259 
260         /* Get the name node */
261 
262         Next = Op->Asl.Child;
263 
264         /* Get the NumArguments node */
265 
266         Next = Next->Asl.Next;
267         MethodInfo->NumArguments = (UINT8)
268             (((UINT8) Next->Asl.Value.Integer) & 0x07);
269 
270         /* Get the SerializeRule and SyncLevel nodes, ignored here */
271 
272         Next = Next->Asl.Next;
273         MethodInfo->ShouldBeSerialized = (UINT8) Next->Asl.Value.Integer;
274 
275         Next = Next->Asl.Next;
276         ArgNode = Next;
277 
278         /* Get the ReturnType node */
279 
280         Next = Next->Asl.Next;
281 
282         NextType = Next->Asl.Child;
283         while (NextType)
284         {
285             /* Get and map each of the ReturnTypes */
286 
287             MethodInfo->ValidReturnTypes |= AnMapObjTypeToBtype (NextType);
288             NextType->Asl.ParseOpcode = PARSEOP_DEFAULT_ARG;
289             NextType = NextType->Asl.Next;
290         }
291 
292         /* Get the ParameterType node */
293 
294         Next = Next->Asl.Next;
295 
296         NextType = Next->Asl.Child;
297         while (NextType)
298         {
299             if (NextType->Asl.ParseOpcode == PARSEOP_DEFAULT_ARG)
300             {
301                 NextParamType = NextType->Asl.Child;
302                 while (NextParamType)
303                 {
304                     MethodInfo->ValidArgTypes[ActualArgs] |=
305                         AnMapObjTypeToBtype (NextParamType);
306 
307                     NextParamType->Asl.ParseOpcode = PARSEOP_DEFAULT_ARG;
308                     NextParamType = NextParamType->Asl.Next;
309                 }
310             }
311             else
312             {
313                 MethodInfo->ValidArgTypes[ActualArgs] =
314                     AnMapObjTypeToBtype (NextType);
315 
316                 NextType->Asl.ParseOpcode = PARSEOP_DEFAULT_ARG;
317                 ActualArgs++;
318             }
319 
320             NextType = NextType->Asl.Next;
321         }
322 
323         if ((MethodInfo->NumArguments) &&
324             (MethodInfo->NumArguments != ActualArgs))
325         {
326             /* error: Param list did not match number of args */
327         }
328 
329         /* Allow numarguments == 0 for Function() */
330 
331         if ((!MethodInfo->NumArguments) && (ActualArgs))
332         {
333             MethodInfo->NumArguments = ActualArgs;
334             ArgNode->Asl.Value.Integer |= ActualArgs;
335         }
336 
337         /*
338          * Actual arguments are initialized at method entry.
339          * All other ArgX "registers" can be used as locals, so we
340          * track their initialization.
341          */
342         for (i = 0; i < MethodInfo->NumArguments; i++)
343         {
344             MethodInfo->ArgInitialized[i] = TRUE;
345         }
346         break;
347 
348     case PARSEOP_METHODCALL:
349 
350         /* Check for a recursive method call */
351 
352         if (MethodInfo &&
353            (Op->Asl.Node == MethodInfo->Op->Asl.Node))
354         {
355             if (MethodInfo->CreatesNamedObjects)
356             {
357                 /*
358                  * This is an error, as it will fail at runtime on all ACPI
359                  * implementations. Any named object declarations will be
360                  * executed twice, causing failure the second time. Note,
361                  * this is independent of whether the method is declared
362                  * Serialized, because the same thread is attempting to
363                  * reenter the method, and this will always succeed.
364                  */
365                 AslDualParseOpError (ASL_ERROR, ASL_MSG_ILLEGAL_RECURSION, Op,
366                     Op->Asl.Value.String, ASL_MSG_FOUND_HERE, MethodInfo->Op,
367                     MethodInfo->Op->Asl.ExternalName);
368             }
369             else
370             {
371                 /* Method does not create objects, issue a remark */
372 
373                 AslError (ASL_REMARK, ASL_MSG_RECURSION, Op, Op->Asl.ExternalName);
374             }
375         }
376         break;
377 
378     case PARSEOP_LOCAL0:
379     case PARSEOP_LOCAL1:
380     case PARSEOP_LOCAL2:
381     case PARSEOP_LOCAL3:
382     case PARSEOP_LOCAL4:
383     case PARSEOP_LOCAL5:
384     case PARSEOP_LOCAL6:
385     case PARSEOP_LOCAL7:
386 
387         if (!MethodInfo)
388         {
389             /*
390              * Local was used outside a control method, or there was an error
391              * in the method declaration.
392              */
393             AslError (ASL_REMARK, ASL_MSG_LOCAL_OUTSIDE_METHOD,
394                 Op, Op->Asl.ExternalName);
395             return (AE_ERROR);
396         }
397 
398         RegisterNumber = (Op->Asl.AmlOpcode & 0x0007);
399 
400         /*
401          * If the local is being used as a target, mark the local
402          * initialized
403          */
404         if (Op->Asl.CompileFlags & OP_IS_TARGET)
405         {
406             MethodInfo->LocalInitialized[RegisterNumber] = TRUE;
407         }
408 
409         /*
410          * Otherwise, this is a reference, check if the local
411          * has been previously initialized.
412          *
413          * The only operator that accepts an uninitialized value is ObjectType()
414          */
415         else if ((!MethodInfo->LocalInitialized[RegisterNumber]) &&
416                  (Op->Asl.Parent->Asl.ParseOpcode != PARSEOP_OBJECTTYPE))
417         {
418             LocalName[strlen (LocalName) -1] = (char) (RegisterNumber + 0x30);
419             AslError (ASL_ERROR, ASL_MSG_LOCAL_INIT, Op, LocalName);
420         }
421         break;
422 
423     case PARSEOP_ARG0:
424     case PARSEOP_ARG1:
425     case PARSEOP_ARG2:
426     case PARSEOP_ARG3:
427     case PARSEOP_ARG4:
428     case PARSEOP_ARG5:
429     case PARSEOP_ARG6:
430 
431         if (!MethodInfo)
432         {
433             /*
434              * Arg was used outside a control method, or there was an error
435              * in the method declaration.
436              */
437             AslError (ASL_REMARK, ASL_MSG_LOCAL_OUTSIDE_METHOD,
438                 Op, Op->Asl.ExternalName);
439             return (AE_ERROR);
440         }
441 
442         RegisterNumber = (Op->Asl.AmlOpcode & 0x000F) - 8;
443         ArgName[strlen (ArgName) -1] = (char) (RegisterNumber + 0x30);
444 
445         /*
446          * If the Arg is being used as a target, mark the local
447          * initialized
448          */
449         if (Op->Asl.CompileFlags & OP_IS_TARGET)
450         {
451             MethodInfo->ArgInitialized[RegisterNumber] = TRUE;
452         }
453 
454         /*
455          * Otherwise, this is a reference, check if the Arg
456          * has been previously initialized.
457          *
458          * The only operator that accepts an uninitialized value is ObjectType()
459          */
460         else if ((!MethodInfo->ArgInitialized[RegisterNumber]) &&
461             (Op->Asl.Parent->Asl.ParseOpcode != PARSEOP_OBJECTTYPE))
462         {
463             AslError (ASL_ERROR, ASL_MSG_ARG_INIT, Op, ArgName);
464         }
465 
466         /* Flag this arg if it is not a "real" argument to the method */
467 
468         if (RegisterNumber >= MethodInfo->NumArguments)
469         {
470             AslError (ASL_REMARK, ASL_MSG_NOT_PARAMETER, Op, ArgName);
471         }
472         break;
473 
474     case PARSEOP_RETURN:
475 
476         if (!MethodInfo)
477         {
478             /*
479              * Probably was an error in the method declaration,
480              * no additional error here
481              */
482             ACPI_WARNING ((AE_INFO, "%p, No parent method", Op));
483             return (AE_ERROR);
484         }
485 
486         /*
487          * A child indicates a possible return value. A simple Return or
488          * Return() is marked with OP_IS_NULL_RETURN by the parser so
489          * that it is not counted as a "real" return-with-value, although
490          * the AML code that is actually emitted is Return(0). The AML
491          * definition of Return has a required parameter, so we are
492          * forced to convert a null return to Return(0).
493          */
494         if ((Op->Asl.Child) &&
495             (Op->Asl.Child->Asl.ParseOpcode != PARSEOP_DEFAULT_ARG) &&
496             (!(Op->Asl.Child->Asl.CompileFlags & OP_IS_NULL_RETURN)))
497         {
498             MethodInfo->NumReturnWithValue++;
499         }
500         else
501         {
502             MethodInfo->NumReturnNoValue++;
503         }
504         break;
505 
506     case PARSEOP_BREAK:
507     case PARSEOP_CONTINUE:
508 
509         Next = Op->Asl.Parent;
510         while (Next)
511         {
512             if (Next->Asl.ParseOpcode == PARSEOP_WHILE)
513             {
514                 break;
515             }
516             Next = Next->Asl.Parent;
517         }
518 
519         if (!Next)
520         {
521             AslError (ASL_ERROR, ASL_MSG_NO_WHILE, Op, NULL);
522         }
523         break;
524 
525     case PARSEOP_STALL:
526 
527         /* We can range check if the argument is an integer */
528 
529         if ((Op->Asl.Child->Asl.ParseOpcode == PARSEOP_INTEGER) &&
530             (Op->Asl.Child->Asl.Value.Integer > ACPI_UINT8_MAX))
531         {
532             AslError (ASL_ERROR, ASL_MSG_INVALID_TIME, Op, NULL);
533         }
534         break;
535 
536     case PARSEOP_DEVICE:
537 
538         if (!ApFindNameInDeviceTree (METHOD_NAME__HID, Op) &&
539             !ApFindNameInDeviceTree (METHOD_NAME__ADR, Op))
540         {
541             AslError (ASL_WARNING, ASL_MSG_MISSING_DEPENDENCY, Op,
542                 "Device object requires a _HID or _ADR in same scope");
543         }
544         break;
545 
546     case PARSEOP_EVENT:
547     case PARSEOP_MUTEX:
548     case PARSEOP_OPERATIONREGION:
549     case PARSEOP_POWERRESOURCE:
550     case PARSEOP_PROCESSOR:
551     case PARSEOP_THERMALZONE:
552 
553         /*
554          * The first operand is a name to be created in the namespace.
555          * Check against the reserved list.
556          */
557         i = ApCheckForPredefinedName (Op, Op->Asl.NameSeg);
558         if (i < ACPI_VALID_RESERVED_NAME_MAX)
559         {
560             AslError (ASL_ERROR, ASL_MSG_RESERVED_USE,
561                 Op, Op->Asl.ExternalName);
562         }
563         break;
564 
565     case PARSEOP_NAME:
566 
567         /* Typecheck any predefined names statically defined with Name() */
568 
569         ApCheckForPredefinedObject (Op, Op->Asl.NameSeg);
570 
571         /* Special typechecking for _HID */
572 
573         if (!strcmp (METHOD_NAME__HID, Op->Asl.NameSeg))
574         {
575             Next = Op->Asl.Child->Asl.Next;
576             AnCheckId (Next, ASL_TYPE_HID);
577         }
578 
579         /* Special typechecking for _CID */
580 
581         else if (!strcmp (METHOD_NAME__CID, Op->Asl.NameSeg))
582         {
583             Next = Op->Asl.Child->Asl.Next;
584 
585             if ((Next->Asl.ParseOpcode == PARSEOP_PACKAGE) ||
586                 (Next->Asl.ParseOpcode == PARSEOP_VAR_PACKAGE))
587             {
588                 Next = Next->Asl.Child;
589                 while (Next)
590                 {
591                     AnCheckId (Next, ASL_TYPE_CID);
592                     Next = Next->Asl.Next;
593                 }
594             }
595             else
596             {
597                 AnCheckId (Next, ASL_TYPE_CID);
598             }
599         }
600 
601         break;
602 
603     default:
604 
605         break;
606     }
607 
608     /* Check for named object creation within a non-serialized method */
609 
610     MtCheckNamedObjectInMethod (Op, MethodInfo);
611     return (AE_OK);
612 }
613 
614 
615 /*******************************************************************************
616  *
617  * FUNCTION:    MtCheckNamedObjectInMethod
618  *
619  * PARAMETERS:  Op                  - Current parser op
620  *              MethodInfo          - Info for method being parsed
621  *
622  * RETURN:      None
623  *
624  * DESCRIPTION: Detect if a non-serialized method is creating a named object,
625  *              which could possibly cause problems if two threads execute
626  *              the method concurrently. Emit a remark in this case.
627  *
628  ******************************************************************************/
629 
630 static void
631 MtCheckNamedObjectInMethod (
632     ACPI_PARSE_OBJECT       *Op,
633     ASL_METHOD_INFO         *MethodInfo)
634 {
635     const ACPI_OPCODE_INFO  *OpInfo;
636 
637 
638     /* We don't care about actual method declarations or scopes */
639 
640     if ((Op->Asl.AmlOpcode == AML_METHOD_OP) ||
641         (Op->Asl.AmlOpcode == AML_SCOPE_OP))
642     {
643         return;
644     }
645 
646     /* Determine if we are creating a named object within a method */
647 
648     if (!MethodInfo)
649     {
650         return;
651     }
652 
653     OpInfo = AcpiPsGetOpcodeInfo (Op->Asl.AmlOpcode);
654     if (OpInfo->Class == AML_CLASS_NAMED_OBJECT)
655     {
656         /*
657          * 1) Mark the method as a method that creates named objects.
658          *
659          * 2) If the method is non-serialized, emit a remark that the method
660          * should be serialized.
661          *
662          * Reason: If a thread blocks within the method for any reason, and
663          * another thread enters the method, the method will fail because
664          * an attempt will be made to create the same object twice.
665          */
666         MethodInfo->CreatesNamedObjects = TRUE;
667         if (!MethodInfo->ShouldBeSerialized)
668         {
669             AslError (ASL_REMARK, ASL_MSG_SERIALIZED_REQUIRED, MethodInfo->Op,
670                 "due to creation of named objects within");
671 
672             /* Emit message only ONCE per method */
673 
674             MethodInfo->ShouldBeSerialized = TRUE;
675         }
676     }
677 }
678 
679 
680 /*******************************************************************************
681  *
682  * FUNCTION:    MtMethodAnalysisWalkEnd
683  *
684  * PARAMETERS:  ASL_WALK_CALLBACK
685  *
686  * RETURN:      Status
687  *
688  * DESCRIPTION: Ascending callback for analysis walk. Complete method
689  *              return analysis.
690  *
691  ******************************************************************************/
692 
693 ACPI_STATUS
694 MtMethodAnalysisWalkEnd (
695     ACPI_PARSE_OBJECT       *Op,
696     UINT32                  Level,
697     void                    *Context)
698 {
699     ASL_ANALYSIS_WALK_INFO  *WalkInfo = (ASL_ANALYSIS_WALK_INFO *) Context;
700     ASL_METHOD_INFO         *MethodInfo = WalkInfo->MethodStack;
701 
702 
703     switch (Op->Asl.ParseOpcode)
704     {
705     case PARSEOP_METHOD:
706     case PARSEOP_RETURN:
707 
708         if (!MethodInfo)
709         {
710             printf ("No method info for method! [%s]\n", Op->Asl.Namepath);
711             AslError (ASL_ERROR, ASL_MSG_COMPILER_INTERNAL, Op,
712                 "No method info for this method");
713 
714             CmCleanupAndExit ();
715             return (AE_AML_INTERNAL);
716         }
717         break;
718 
719     default:
720 
721         break;
722     }
723 
724     switch (Op->Asl.ParseOpcode)
725     {
726     case PARSEOP_METHOD:
727 
728         WalkInfo->MethodStack = MethodInfo->Next;
729 
730         /*
731          * Check if there is no return statement at the end of the
732          * method AND we can actually get there -- i.e., the execution
733          * of the method can possibly terminate without a return statement.
734          */
735         if ((!AnLastStatementIsReturn (Op)) &&
736             (!(Op->Asl.CompileFlags & OP_HAS_NO_EXIT)))
737         {
738             /*
739              * No return statement, and execution can possibly exit
740              * via this path. This is equivalent to Return ()
741              */
742             MethodInfo->NumReturnNoValue++;
743         }
744 
745         /*
746          * Check for case where some return statements have a return value
747          * and some do not. Exit without a return statement is a return with
748          * no value
749          */
750         if (MethodInfo->NumReturnNoValue &&
751             MethodInfo->NumReturnWithValue)
752         {
753             AslError (ASL_WARNING, ASL_MSG_RETURN_TYPES, Op,
754                 Op->Asl.ExternalName);
755         }
756 
757         /*
758          * If there are any RETURN() statements with no value, or there is a
759          * control path that allows the method to exit without a return value,
760          * we mark the method as a method that does not return a value. This
761          * knowledge can be used to check method invocations that expect a
762          * returned value.
763          */
764         if (MethodInfo->NumReturnNoValue)
765         {
766             if (MethodInfo->NumReturnWithValue)
767             {
768                 Op->Asl.CompileFlags |= OP_METHOD_SOME_NO_RETVAL;
769             }
770             else
771             {
772                 Op->Asl.CompileFlags |= OP_METHOD_NO_RETVAL;
773             }
774         }
775 
776         /*
777          * Check predefined method names for correct return behavior
778          * and correct number of arguments. Also, some special checks
779          * For GPE and _REG methods.
780          */
781         if (ApCheckForPredefinedMethod (Op, MethodInfo))
782         {
783             /* Special check for two names like _L01 and _E01 in same scope */
784 
785             ApCheckForGpeNameConflict (Op);
786 
787             /*
788              * Special check for _REG: Must have an operation region definition
789              * within the same scope!
790              */
791             ApCheckRegMethod (Op);
792         }
793 
794         ACPI_FREE (MethodInfo);
795         break;
796 
797     case PARSEOP_NAME:
798 
799          /* Special check for two names like _L01 and _E01 in same scope */
800 
801         ApCheckForGpeNameConflict (Op);
802         break;
803 
804     case PARSEOP_RETURN:
805 
806         /*
807          * If the parent is a predefined method name, attempt to typecheck
808          * the return value. Only static types can be validated.
809          */
810         ApCheckPredefinedReturnValue (Op, MethodInfo);
811 
812         /*
813          * The parent block does not "exit" and continue execution -- the
814          * method is terminated here with the Return() statement.
815          */
816         Op->Asl.Parent->Asl.CompileFlags |= OP_HAS_NO_EXIT;
817 
818         /* Used in the "typing" pass later */
819 
820         Op->Asl.ParentMethod = MethodInfo->Op;
821 
822         /*
823          * If there is a peer node after the return statement, then this
824          * node is unreachable code -- i.e., it won't be executed because of
825          * the preceding Return() statement.
826          */
827         if (Op->Asl.Next)
828         {
829             AslError (ASL_WARNING, ASL_MSG_UNREACHABLE_CODE,
830                 Op->Asl.Next, NULL);
831         }
832         break;
833 
834     case PARSEOP_IF:
835 
836         if ((Op->Asl.CompileFlags & OP_HAS_NO_EXIT) &&
837             (Op->Asl.Next) &&
838             (Op->Asl.Next->Asl.ParseOpcode == PARSEOP_ELSE))
839         {
840             /*
841              * This IF has a corresponding ELSE. The IF block has no exit,
842              * (it contains an unconditional Return)
843              * mark the ELSE block to remember this fact.
844              */
845             Op->Asl.Next->Asl.CompileFlags |= OP_IF_HAS_NO_EXIT;
846         }
847         break;
848 
849     case PARSEOP_ELSE:
850 
851         if ((Op->Asl.CompileFlags & OP_HAS_NO_EXIT) &&
852             (Op->Asl.CompileFlags & OP_IF_HAS_NO_EXIT))
853         {
854             /*
855              * This ELSE block has no exit and the corresponding IF block
856              * has no exit either. Therefore, the parent node has no exit.
857              */
858             Op->Asl.Parent->Asl.CompileFlags |= OP_HAS_NO_EXIT;
859         }
860         break;
861 
862 
863     default:
864 
865         if ((Op->Asl.CompileFlags & OP_HAS_NO_EXIT) &&
866             (Op->Asl.Parent))
867         {
868             /* If this node has no exit, then the parent has no exit either */
869 
870             Op->Asl.Parent->Asl.CompileFlags |= OP_HAS_NO_EXIT;
871         }
872         break;
873     }
874 
875     return (AE_OK);
876 }
877