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 - 2021, 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 "acnamesp.h"
155 #include "acparser.h"
156 #include "amlcode.h"
157 
158 
159 #define _COMPONENT          ACPI_COMPILER
160         ACPI_MODULE_NAME    ("aslmethod")
161 
162 
163 /* Local prototypes */
164 
165 static void
166 MtCheckNamedObjectInMethod (
167     ACPI_PARSE_OBJECT       *Op,
168     ASL_METHOD_INFO         *MethodInfo);
169 
170 static void
171 MtCheckStaticOperationRegionInMethod (
172     ACPI_PARSE_OBJECT       *Op);
173 
174 
175 /*******************************************************************************
176  *
177  * FUNCTION:    MtMethodAnalysisWalkBegin
178  *
179  * PARAMETERS:  ASL_WALK_CALLBACK
180  *
181  * RETURN:      Status
182  *
183  * DESCRIPTION: Descending callback for the analysis walk. Check methods for:
184  *              1) Initialized local variables
185  *              2) Valid arguments
186  *              3) Return types
187  *
188  ******************************************************************************/
189 
190 ACPI_STATUS
191 MtMethodAnalysisWalkBegin (
192     ACPI_PARSE_OBJECT       *Op,
193     UINT32                  Level,
194     void                    *Context)
195 {
196     ASL_ANALYSIS_WALK_INFO  *WalkInfo = (ASL_ANALYSIS_WALK_INFO *) Context;
197     ASL_METHOD_INFO         *MethodInfo = WalkInfo->MethodStack;
198     ACPI_PARSE_OBJECT       *Next;
199     UINT32                  RegisterNumber;
200     UINT32                  i;
201     char                    LocalName[] = "Local0";
202     char                    ArgName[] = "Arg0";
203     ACPI_PARSE_OBJECT       *ArgNode;
204     ACPI_PARSE_OBJECT       *NextType;
205     UINT8                   ActualArgs = 0;
206     BOOLEAN                 HidExists;
207     BOOLEAN                 AdrExists;
208     BOOLEAN                 PrsExists;
209     BOOLEAN                 CrsExists;
210     BOOLEAN                 SrsExists;
211     BOOLEAN                 DisExists;
212 
213 
214     /* Build cross-reference output file if requested */
215 
216     if (AslGbl_CrossReferenceOutput)
217     {
218         OtXrefWalkPart1 (Op, Level, MethodInfo);
219     }
220 
221     switch (Op->Asl.ParseOpcode)
222     {
223     case PARSEOP_METHOD:
224 
225         AslGbl_TotalMethods++;
226 
227         /* Create and init method info */
228 
229         MethodInfo = UtLocalCalloc (sizeof (ASL_METHOD_INFO));
230         MethodInfo->Next = WalkInfo->MethodStack;
231         MethodInfo->Op = Op;
232 
233         WalkInfo->MethodStack = MethodInfo;
234 
235         /*
236          * Special handling for _PSx methods. Dependency rules (same scope):
237          *
238          * 1) _PS0 - One of these must exist: _PS1, _PS2, _PS3
239          * 2) _PS1/_PS2/_PS3: A _PS0 must exist
240          */
241         if (ACPI_COMPARE_NAMESEG (METHOD_NAME__PS0, Op->Asl.NameSeg))
242         {
243             /* For _PS0, one of _PS1/_PS2/_PS3 must exist */
244 
245             if ((!ApFindNameInScope (METHOD_NAME__PS1, Op)) &&
246                 (!ApFindNameInScope (METHOD_NAME__PS2, Op)) &&
247                 (!ApFindNameInScope (METHOD_NAME__PS3, Op)))
248             {
249                 AslError (ASL_WARNING, ASL_MSG_MISSING_DEPENDENCY, Op,
250                     "_PS0 requires one of _PS1/_PS2/_PS3 in same scope");
251             }
252         }
253         else if (
254             ACPI_COMPARE_NAMESEG (METHOD_NAME__PS1, Op->Asl.NameSeg) ||
255             ACPI_COMPARE_NAMESEG (METHOD_NAME__PS2, Op->Asl.NameSeg) ||
256             ACPI_COMPARE_NAMESEG (METHOD_NAME__PS3, Op->Asl.NameSeg))
257         {
258             /* For _PS1/_PS2/_PS3, a _PS0 must exist */
259 
260             if (!ApFindNameInScope (METHOD_NAME__PS0, Op))
261             {
262                 sprintf (AslGbl_MsgBuffer,
263                     "%4.4s requires _PS0 in same scope", Op->Asl.NameSeg);
264 
265                 AslError (ASL_WARNING, ASL_MSG_MISSING_DEPENDENCY, Op,
266                     AslGbl_MsgBuffer);
267             }
268         }
269 
270         /* Get the name node */
271 
272         Next = Op->Asl.Child;
273 
274         /* Get the NumArguments node */
275 
276         Next = Next->Asl.Next;
277         MethodInfo->NumArguments = (UINT8)
278             (((UINT8) Next->Asl.Value.Integer) & 0x07);
279 
280         /* Get the SerializeRule and SyncLevel nodes, ignored here */
281 
282         Next = Next->Asl.Next;
283         MethodInfo->ShouldBeSerialized = (UINT8) Next->Asl.Value.Integer;
284 
285         Next = Next->Asl.Next;
286         ArgNode = Next;
287 
288         /* Get the ReturnType node */
289 
290         Next = Next->Asl.Next;
291 
292         NextType = Next->Asl.Child;
293 
294         MethodInfo->ValidReturnTypes = MtProcessTypeOp (NextType);
295         Op->Asl.AcpiBtype |= MethodInfo->ValidReturnTypes;
296 
297         /* Get the ParameterType node */
298 
299         Next = Next->Asl.Next;
300 
301         NextType = Next->Asl.Child;
302         if (!NextType)
303         {
304             /*
305              * The optional parameter types list was omitted  at the source
306              * level. Use the Argument count parameter instead.
307              */
308             ActualArgs = MethodInfo->NumArguments;
309         }
310         else
311         {
312             ActualArgs = MtProcessParameterTypeList (NextType,
313                 MethodInfo->ValidArgTypes);
314             MethodInfo->NumArguments = ActualArgs;
315             ArgNode->Asl.Value.Integer |= ActualArgs;
316         }
317 
318         if ((MethodInfo->NumArguments) &&
319             (MethodInfo->NumArguments != ActualArgs))
320         {
321             sprintf (AslGbl_MsgBuffer,
322                 "Length = %u", ActualArgs);
323             AslError (ASL_ERROR, ASL_MSG_ARG_COUNT_MISMATCH,
324                 Op->Asl.Child->Asl.Next, AslGbl_MsgBuffer);
325         }
326 
327         /* Allow numarguments == 0 for Function() */
328 
329         if ((!MethodInfo->NumArguments) && (ActualArgs))
330         {
331             MethodInfo->NumArguments = ActualArgs;
332             ArgNode->Asl.Value.Integer |= ActualArgs;
333         }
334 
335         /*
336          * Actual arguments are initialized at method entry.
337          * All other ArgX "registers" can be used as locals, so we
338          * track their initialization.
339          */
340         for (i = 0; i < MethodInfo->NumArguments; i++)
341         {
342             MethodInfo->ArgInitialized[i] = TRUE;
343         }
344         break;
345 
346     case PARSEOP_METHODCALL:
347 
348         /* Check for a recursive method call */
349 
350         if (MethodInfo &&
351            (Op->Asl.Node == MethodInfo->Op->Asl.Node))
352         {
353             if (MethodInfo->CreatesNamedObjects)
354             {
355                 /*
356                  * This is an error, as it will fail at runtime on all ACPI
357                  * implementations. Any named object declarations will be
358                  * executed twice, causing failure the second time. Note,
359                  * this is independent of whether the method is declared
360                  * Serialized, because the same thread is attempting to
361                  * reenter the method, and this will always succeed.
362                  */
363                 AslDualParseOpError (ASL_ERROR, ASL_MSG_ILLEGAL_RECURSION, Op,
364                     Op->Asl.Value.String, ASL_MSG_FOUND_HERE, MethodInfo->Op,
365                     MethodInfo->Op->Asl.ExternalName);
366             }
367             else
368             {
369                 /* Method does not create objects, issue a remark */
370 
371                 AslError (ASL_REMARK, ASL_MSG_RECURSION, Op, Op->Asl.ExternalName);
372             }
373         }
374         break;
375 
376     case PARSEOP_LOCAL0:
377     case PARSEOP_LOCAL1:
378     case PARSEOP_LOCAL2:
379     case PARSEOP_LOCAL3:
380     case PARSEOP_LOCAL4:
381     case PARSEOP_LOCAL5:
382     case PARSEOP_LOCAL6:
383     case PARSEOP_LOCAL7:
384 
385         if (!MethodInfo)
386         {
387             /*
388              * Local was used outside a control method, or there was an error
389              * in the method declaration.
390              */
391             AslError (ASL_REMARK, ASL_MSG_LOCAL_OUTSIDE_METHOD,
392                 Op, Op->Asl.ExternalName);
393             return (AE_ERROR);
394         }
395 
396         RegisterNumber = (Op->Asl.AmlOpcode & 0x0007);
397 
398         /*
399          * If the local is being used as a target, mark the local
400          * initialized
401          */
402         if (Op->Asl.CompileFlags & OP_IS_TARGET)
403         {
404             MethodInfo->LocalInitialized[RegisterNumber] = TRUE;
405         }
406 
407         /*
408          * Otherwise, this is a reference, check if the local
409          * has been previously initialized.
410          *
411          * The only operator that accepts an uninitialized value is ObjectType()
412          */
413         else if ((!MethodInfo->LocalInitialized[RegisterNumber]) &&
414                  (Op->Asl.Parent->Asl.ParseOpcode != PARSEOP_OBJECTTYPE))
415         {
416             LocalName[strlen (LocalName) -1] = (char) (RegisterNumber + 0x30);
417             AslError (ASL_ERROR, ASL_MSG_LOCAL_INIT, Op, LocalName);
418         }
419         break;
420 
421     case PARSEOP_ARG0:
422     case PARSEOP_ARG1:
423     case PARSEOP_ARG2:
424     case PARSEOP_ARG3:
425     case PARSEOP_ARG4:
426     case PARSEOP_ARG5:
427     case PARSEOP_ARG6:
428 
429         if (!MethodInfo)
430         {
431             /*
432              * Arg was used outside a control method, or there was an error
433              * in the method declaration.
434              */
435             AslError (ASL_REMARK, ASL_MSG_LOCAL_OUTSIDE_METHOD,
436                 Op, Op->Asl.ExternalName);
437             return (AE_ERROR);
438         }
439 
440         RegisterNumber = (Op->Asl.AmlOpcode & 0x000F) - 8;
441         ArgName[strlen (ArgName) -1] = (char) (RegisterNumber + 0x30);
442 
443         /*
444          * If the Arg is being used as a target, mark the local
445          * initialized
446          */
447         if (Op->Asl.CompileFlags & OP_IS_TARGET)
448         {
449             MethodInfo->ArgInitialized[RegisterNumber] = TRUE;
450         }
451 
452         /*
453          * Otherwise, this is a reference, check if the Arg
454          * has been previously initialized.
455          *
456          * The only operator that accepts an uninitialized value is ObjectType()
457          */
458         else if ((!MethodInfo->ArgInitialized[RegisterNumber]) &&
459             (Op->Asl.Parent->Asl.ParseOpcode != PARSEOP_OBJECTTYPE))
460         {
461             AslError (ASL_ERROR, ASL_MSG_ARG_INIT, Op, ArgName);
462         }
463 
464         /* Flag this arg if it is not a "real" argument to the method */
465 
466         if (RegisterNumber >= MethodInfo->NumArguments)
467         {
468             AslError (ASL_REMARK, ASL_MSG_NOT_PARAMETER, Op, ArgName);
469         }
470         break;
471 
472     case PARSEOP_RETURN:
473 
474         if (!MethodInfo)
475         {
476             /*
477              * Probably was an error in the method declaration,
478              * no additional error here
479              */
480             ACPI_WARNING ((AE_INFO, "%p, No parent method", Op));
481             return (AE_ERROR);
482         }
483 
484         /*
485          * A child indicates a possible return value. A simple Return or
486          * Return() is marked with OP_IS_NULL_RETURN by the parser so
487          * that it is not counted as a "real" return-with-value, although
488          * the AML code that is actually emitted is Return(0). The AML
489          * definition of Return has a required parameter, so we are
490          * forced to convert a null return to Return(0).
491          */
492         if ((Op->Asl.Child) &&
493             (Op->Asl.Child->Asl.ParseOpcode != PARSEOP_DEFAULT_ARG) &&
494             (!(Op->Asl.Child->Asl.CompileFlags & OP_IS_NULL_RETURN)))
495         {
496             MethodInfo->NumReturnWithValue++;
497         }
498         else
499         {
500             MethodInfo->NumReturnNoValue++;
501         }
502         break;
503 
504     case PARSEOP_BREAK:
505     case PARSEOP_CONTINUE:
506 
507         Next = Op->Asl.Parent;
508         while (Next)
509         {
510             if (Next->Asl.ParseOpcode == PARSEOP_WHILE)
511             {
512                 break;
513             }
514             Next = Next->Asl.Parent;
515         }
516 
517         if (!Next)
518         {
519             AslError (ASL_ERROR, ASL_MSG_NO_WHILE, Op, NULL);
520         }
521         break;
522 
523     case PARSEOP_STALL:
524 
525         /* We can range check if the argument is an integer */
526 
527         if ((Op->Asl.Child->Asl.ParseOpcode == PARSEOP_INTEGER) &&
528             (Op->Asl.Child->Asl.Value.Integer > ACPI_UINT8_MAX))
529         {
530             AslError (ASL_ERROR, ASL_MSG_INVALID_TIME, Op, NULL);
531         }
532         break;
533 
534     case PARSEOP_DEVICE:
535 
536         /* Check usage of _HID and _ADR objects */
537 
538         HidExists = ApFindNameInDeviceTree (METHOD_NAME__HID, Op);
539         AdrExists = ApFindNameInDeviceTree (METHOD_NAME__ADR, Op);
540 
541         if (!HidExists && !AdrExists)
542         {
543             AslError (ASL_ERROR, ASL_MSG_MISSING_DEPENDENCY, Op,
544                 "Device object requires a _HID or _ADR");
545         }
546         else if (HidExists && AdrExists)
547         {
548             /*
549              * According to the ACPI spec, "A device object must contain
550              * either an _HID object or an _ADR object, but should not contain
551              * both".
552              */
553             AslError (ASL_WARNING, ASL_MSG_MULTIPLE_TYPES, Op,
554                 "Device object requires either a _HID or _ADR, but not both");
555         }
556 
557         /*
558          * Check usage of _CRS, _DIS, _PRS, and _SRS objects (July 2021).
559          *
560          * Under the Device Object:
561          *
562          * 1) If _DIS is present, must have a _CRS, _PRS, and _SRS
563          * 2) If _PRS is present, must have a _CRS and _SRS
564          * 3) If _SRS is present, must have a _CRS and _PRS
565          */
566         CrsExists = ApFindNameInDeviceTree (METHOD_NAME__CRS, Op);
567         DisExists = ApFindNameInDeviceTree (METHOD_NAME__DIS, Op);
568         PrsExists = ApFindNameInDeviceTree (METHOD_NAME__PRS, Op);
569         SrsExists = ApFindNameInDeviceTree (METHOD_NAME__SRS, Op);
570 
571         /* 1) If _DIS is present, must have a _CRS, _PRS, and _SRS */
572 
573         if (DisExists)
574         {
575             if (!CrsExists)
576             {
577                 AslError (ASL_WARNING, ASL_MSG_MISSING_DEPENDENCY, Op,
578                     "_DIS is missing a _CRS, requires a _CRS, _PRS, and a _SRS");
579             }
580 
581             if (!PrsExists)
582             {
583                 AslError (ASL_WARNING, ASL_MSG_MISSING_DEPENDENCY, Op,
584                     "_DIS is missing a _PRS, requires a _CRS, _PRS, and a _SRS");
585             }
586 
587             if (!SrsExists)
588             {
589                 AslError (ASL_WARNING, ASL_MSG_MISSING_DEPENDENCY, Op,
590                     "_DIS is missing a _SRS, requires a _CRS, _PRS, and a _SRS");
591             }
592         }
593 
594         /* 2) If _PRS is present, must have a _CRS and _SRS */
595 
596         if (PrsExists)
597         {
598             if (!CrsExists)
599             {
600                 AslError (ASL_WARNING, ASL_MSG_MISSING_DEPENDENCY, Op,
601                     "_PRS is missing a _CRS, requires a _CRS and a _SRS");
602             }
603 
604             if (!SrsExists)
605             {
606                 AslError (ASL_WARNING, ASL_MSG_MISSING_DEPENDENCY, Op,
607                     "_PRS is missing a _SRS, requires a _CRS and a _SRS");
608             }
609         }
610 
611         /* 3) If _SRS is present, must have a _CRS and _PRS */
612 
613         if (SrsExists)
614         {
615             if (!CrsExists)
616             {
617                 AslError (ASL_WARNING, ASL_MSG_MISSING_DEPENDENCY, Op,
618                     "_SRS is missing a _CRS, requires a _CRS and a _PRS");
619             }
620             if (!PrsExists)
621             {
622                 AslError (ASL_WARNING, ASL_MSG_MISSING_DEPENDENCY, Op,
623                     "_SRS is missing a _PRS, requires a _CRS and a _PRS");
624             }
625             if (!DisExists)
626             {
627                 AslError (ASL_REMARK, ASL_MSG_MISSING_DEPENDENCY, Op,
628                     "_SRS is missing a _DIS");
629             }
630         }
631         break;
632 
633     case PARSEOP_EVENT:
634     case PARSEOP_MUTEX:
635     case PARSEOP_OPERATIONREGION:
636     case PARSEOP_POWERRESOURCE:
637     case PARSEOP_PROCESSOR:
638     case PARSEOP_THERMALZONE:
639 
640         /*
641          * The first operand is a name to be created in the namespace.
642          * Check against the reserved list.
643          */
644         i = ApCheckForPredefinedName (Op, Op->Asl.NameSeg);
645         if (i < ACPI_VALID_RESERVED_NAME_MAX)
646         {
647             AslError (ASL_ERROR, ASL_MSG_RESERVED_USE,
648                 Op, Op->Asl.ExternalName);
649         }
650 
651         MtCheckStaticOperationRegionInMethod (Op);
652         break;
653 
654     case PARSEOP_NAME:
655 
656         /* Typecheck any predefined names statically defined with Name() */
657 
658         ApCheckForPredefinedObject (Op, Op->Asl.NameSeg);
659 
660         /* Special typechecking for _HID */
661 
662         if (ACPI_COMPARE_NAMESEG (METHOD_NAME__HID, Op->Asl.NameSeg))
663         {
664             Next = Op->Asl.Child->Asl.Next;
665             AnCheckId (Next, ASL_TYPE_HID);
666         }
667 
668         /* Special typechecking for _CID */
669 
670         else if (ACPI_COMPARE_NAMESEG (METHOD_NAME__CID, Op->Asl.NameSeg))
671         {
672             Next = Op->Asl.Child->Asl.Next;
673 
674             if ((Next->Asl.ParseOpcode == PARSEOP_PACKAGE) ||
675                 (Next->Asl.ParseOpcode == PARSEOP_VAR_PACKAGE))
676             {
677                 Next = Next->Asl.Child;
678                 while (Next)
679                 {
680                     AnCheckId (Next, ASL_TYPE_CID);
681                     Next = Next->Asl.Next;
682                 }
683             }
684             else
685             {
686                 AnCheckId (Next, ASL_TYPE_CID);
687             }
688         }
689 
690         break;
691 
692     default:
693 
694         break;
695     }
696 
697     /* Check for named object creation within a non-serialized method */
698 
699     MtCheckNamedObjectInMethod (Op, MethodInfo);
700     return (AE_OK);
701 }
702 
703 
704 /*******************************************************************************
705  *
706  * FUNCTION:    MtProcessTypeOp
707  *
708  * PARAMETERS:  Op                  - Op representing a btype
709  *
710  * RETURN:      Btype represented by Op
711  *
712  * DESCRIPTION: Process a parse object that represents single parameter type or
713  *              a return type in method, function, and external declarations.
714  *
715  ******************************************************************************/
716 
717 UINT32
718 MtProcessTypeOp (
719     ACPI_PARSE_OBJECT       *TypeOp)
720 {
721     UINT32                  Btype = ACPI_BTYPE_ANY;
722 
723 
724     while (TypeOp)
725     {
726         Btype |= AnMapObjTypeToBtype (TypeOp);
727         TypeOp->Asl.ParseOpcode = PARSEOP_DEFAULT_ARG;
728         TypeOp = TypeOp->Asl.Next;
729     }
730 
731     return (Btype);
732 }
733 
734 
735 /*******************************************************************************
736  *
737  * FUNCTION:    MtProcessParameterTypeList
738  *
739  * PARAMETERS:  Op                  - Op representing a btype
740  *
741  * RETURN:      Btype represented by Op
742  *
743  * DESCRIPTION: Process a parse object that represents a parameter type list in
744  *              method, function, and external declarations.
745  *
746  ******************************************************************************/
747 
748 UINT8
749 MtProcessParameterTypeList (
750     ACPI_PARSE_OBJECT       *ParamTypeOp,
751     UINT32                  *TypeList)
752 {
753     UINT8                   ParameterCount = 0;
754 
755 
756     if (ParamTypeOp && ParamTypeOp->Asl.ParseOpcode != PARSEOP_DEFAULT_ARG)
757     {
758         /* Special case for a single parameter without braces */
759 
760         TypeList[ParameterCount] =
761             MtProcessTypeOp (ParamTypeOp);
762 
763         return (1);
764     }
765 
766     while (ParamTypeOp)
767     {
768         TypeList[ParameterCount] =
769             MtProcessTypeOp (ParamTypeOp->Asl.Child);
770 
771         ParameterCount++;
772         ParamTypeOp = ParamTypeOp->Asl.Next;
773     }
774 
775     return (ParameterCount);
776 }
777 
778 
779 /*******************************************************************************
780  *
781  * FUNCTION:    MtCheckNamedObjectInMethod
782  *
783  * PARAMETERS:  Op                  - Current parser op
784  *              MethodInfo          - Info for method being parsed
785  *
786  * RETURN:      None
787  *
788  * DESCRIPTION: Detect if a non-serialized method is creating a named object,
789  *              which could possibly cause problems if two threads execute
790  *              the method concurrently. Emit a remark in this case.
791  *
792  ******************************************************************************/
793 
794 static void
795 MtCheckNamedObjectInMethod (
796     ACPI_PARSE_OBJECT       *Op,
797     ASL_METHOD_INFO         *MethodInfo)
798 {
799     const ACPI_OPCODE_INFO  *OpInfo;
800     char                    *ExternalPath;
801 
802 
803     /* We don't care about actual method declarations or scopes */
804 
805     if ((Op->Asl.AmlOpcode == AML_METHOD_OP) ||
806         (Op->Asl.AmlOpcode == AML_SCOPE_OP))
807     {
808         return;
809     }
810 
811     /* Determine if we are creating a named object within a method */
812 
813     if (!MethodInfo)
814     {
815         return;
816     }
817 
818     OpInfo = AcpiPsGetOpcodeInfo (Op->Asl.AmlOpcode);
819     if ((OpInfo->Class == AML_CLASS_NAMED_OBJECT) && (Op->Asl.AmlOpcode != AML_FIELD_OP))
820     {
821         /*
822          * 1) Mark the method as a method that creates named objects.
823          *
824          * 2) Issue a remark indicating the inefficiency of creating named
825          * objects within a method (Except for compiler-emitted temporary
826          * variables).
827          *
828          * 3) If the method is non-serialized, emit a remark that the method
829          * should be serialized.
830          *
831          * Reason: If a thread blocks within the method for any reason, and
832          * another thread enters the method, the method will fail because
833          * an attempt will be made to create the same object twice.
834          *
835          * Note: The Field opcode is disallowed here because Field() does not
836          * create a new named object.
837          */
838         ExternalPath = AcpiNsGetNormalizedPathname (MethodInfo->Op->Asl.Node, TRUE);
839 
840         /* No error for compiler temp variables (name starts with "_T_") */
841 
842         if ((Op->Asl.NameSeg[0] != '_') &&
843             (Op->Asl.NameSeg[1] != 'T') &&
844             (Op->Asl.NameSeg[2] != '_'))
845         {
846             AslError (ASL_REMARK, ASL_MSG_NAMED_OBJECT_CREATION, Op,
847                 ExternalPath);
848         }
849 
850         MethodInfo->CreatesNamedObjects = TRUE;
851         if (!MethodInfo->ShouldBeSerialized)
852         {
853             AslError (ASL_REMARK, ASL_MSG_SERIALIZED_REQUIRED, MethodInfo->Op,
854                 ExternalPath);
855 
856             /* Emit message only ONCE per method */
857 
858             MethodInfo->ShouldBeSerialized = TRUE;
859         }
860 
861         if (ExternalPath)
862         {
863             ACPI_FREE (ExternalPath);
864         }
865     }
866 }
867 
868 
869 /*******************************************************************************
870  *
871  * FUNCTION:    MtCheckStaticOperationRegionInMethod
872  *
873  * PARAMETERS:  Op                  - Current parser op
874  *
875  * RETURN:      None
876  *
877  * DESCRIPTION: Warns if an Operation Region with static address or length
878  *              is declared inside a control method
879  *
880  ******************************************************************************/
881 
882 static void
883 MtCheckStaticOperationRegionInMethod(
884     ACPI_PARSE_OBJECT*       Op)
885 {
886     ACPI_PARSE_OBJECT*       AddressOp;
887     ACPI_PARSE_OBJECT*       LengthOp;
888 
889 
890     if (Op->Asl.ParseOpcode != PARSEOP_OPERATIONREGION)
891     {
892         return;
893     }
894 
895     /*
896      * OperationRegion should have 4 arguments defined. At this point, we
897      * assume that the parse tree is well-formed.
898      */
899     AddressOp = Op->Asl.Child->Asl.Next->Asl.Next;
900     LengthOp = Op->Asl.Child->Asl.Next->Asl.Next->Asl.Next;
901 
902     if (UtGetParentMethodOp (Op) &&
903         AddressOp->Asl.ParseOpcode == PARSEOP_INTEGER &&
904         LengthOp->Asl.ParseOpcode == PARSEOP_INTEGER)
905     {
906         /*
907          * At this point, a static operation region declared inside of a
908          * control method has been found. Throw a warning because this is
909          * highly inefficient.
910          */
911         AslError(ASL_WARNING, ASL_MSG_STATIC_OPREGION_IN_METHOD, Op, NULL);
912     }
913 
914     return;
915 }
916 
917 
918 /*******************************************************************************
919  *
920  * FUNCTION:    MtMethodAnalysisWalkEnd
921  *
922  * PARAMETERS:  ASL_WALK_CALLBACK
923  *
924  * RETURN:      Status
925  *
926  * DESCRIPTION: Ascending callback for analysis walk. Complete method
927  *              return analysis.
928  *
929  ******************************************************************************/
930 
931 ACPI_STATUS
932 MtMethodAnalysisWalkEnd (
933     ACPI_PARSE_OBJECT       *Op,
934     UINT32                  Level,
935     void                    *Context)
936 {
937     ASL_ANALYSIS_WALK_INFO  *WalkInfo = (ASL_ANALYSIS_WALK_INFO *) Context;
938     ASL_METHOD_INFO         *MethodInfo = WalkInfo->MethodStack;
939     char                    *ExternalPath;
940 
941 
942     switch (Op->Asl.ParseOpcode)
943     {
944     case PARSEOP_METHOD:
945     case PARSEOP_RETURN:
946 
947         if (!MethodInfo)
948         {
949             printf ("No method info for method! [%s]\n", Op->Asl.Namepath);
950             AslError (ASL_ERROR, ASL_MSG_COMPILER_INTERNAL, Op,
951                 "No method info for this method");
952 
953             CmCleanupAndExit ();
954             return (AE_AML_INTERNAL);
955         }
956         break;
957 
958     default:
959 
960         break;
961     }
962 
963     switch (Op->Asl.ParseOpcode)
964     {
965     case PARSEOP_METHOD:
966 
967         WalkInfo->MethodStack = MethodInfo->Next;
968 
969         /*
970          * Check if there is no return statement at the end of the
971          * method AND we can actually get there -- i.e., the execution
972          * of the method can possibly terminate without a return statement.
973          */
974         if ((!AnLastStatementIsReturn (Op)) &&
975             (!(Op->Asl.CompileFlags & OP_HAS_NO_EXIT)))
976         {
977             /*
978              * No return statement, and execution can possibly exit
979              * via this path. This is equivalent to Return ()
980              */
981             MethodInfo->NumReturnNoValue++;
982         }
983 
984         /*
985          * Check for case where some return statements have a return value
986          * and some do not. Exit without a return statement is a return with
987          * no value
988          */
989         if (MethodInfo->NumReturnNoValue &&
990             MethodInfo->NumReturnWithValue)
991         {
992             ExternalPath = AcpiNsGetNormalizedPathname (Op->Asl.Node, TRUE);
993 
994             AslError (ASL_WARNING, ASL_MSG_RETURN_TYPES, Op,
995                 ExternalPath);
996 
997             if (ExternalPath)
998             {
999                 ACPI_FREE (ExternalPath);
1000             }
1001         }
1002 
1003         /*
1004          * If there are any RETURN() statements with no value, or there is a
1005          * control path that allows the method to exit without a return value,
1006          * we mark the method as a method that does not return a value. This
1007          * knowledge can be used to check method invocations that expect a
1008          * returned value.
1009          */
1010         if (MethodInfo->NumReturnNoValue)
1011         {
1012             if (MethodInfo->NumReturnWithValue)
1013             {
1014                 Op->Asl.CompileFlags |= OP_METHOD_SOME_NO_RETVAL;
1015             }
1016             else
1017             {
1018                 Op->Asl.CompileFlags |= OP_METHOD_NO_RETVAL;
1019             }
1020         }
1021 
1022         /*
1023          * Check predefined method names for correct return behavior
1024          * and correct number of arguments. Also, some special checks
1025          * For GPE and _REG methods.
1026          */
1027         if (ApCheckForPredefinedMethod (Op, MethodInfo))
1028         {
1029             /* Special check for two names like _L01 and _E01 in same scope */
1030 
1031             ApCheckForGpeNameConflict (Op);
1032 
1033             /*
1034              * Special check for _REG: Must have an operation region definition
1035              * within the same scope!
1036              */
1037             ApCheckRegMethod (Op);
1038         }
1039 
1040         ACPI_FREE (MethodInfo);
1041         break;
1042 
1043     case PARSEOP_NAME:
1044 
1045          /* Special check for two names like _L01 and _E01 in same scope */
1046 
1047         ApCheckForGpeNameConflict (Op);
1048         break;
1049 
1050     case PARSEOP_RETURN:
1051 
1052         /*
1053          * If the parent is a predefined method name, attempt to typecheck
1054          * the return value. Only static types can be validated.
1055          */
1056         ApCheckPredefinedReturnValue (Op, MethodInfo);
1057 
1058         /*
1059          * The parent block does not "exit" and continue execution -- the
1060          * method is terminated here with the Return() statement.
1061          */
1062         Op->Asl.Parent->Asl.CompileFlags |= OP_HAS_NO_EXIT;
1063 
1064         /* Used in the "typing" pass later */
1065 
1066         Op->Asl.ParentMethod = MethodInfo->Op;
1067 
1068         /*
1069          * If there is a peer node after the return statement, then this
1070          * node is unreachable code -- i.e., it won't be executed because of
1071          * the preceding Return() statement.
1072          */
1073         if (Op->Asl.Next)
1074         {
1075             AslError (ASL_WARNING, ASL_MSG_UNREACHABLE_CODE,
1076                 Op->Asl.Next, NULL);
1077         }
1078         break;
1079 
1080     case PARSEOP_IF:
1081 
1082         if ((Op->Asl.CompileFlags & OP_HAS_NO_EXIT) &&
1083             (Op->Asl.Next) &&
1084             (Op->Asl.Next->Asl.ParseOpcode == PARSEOP_ELSE))
1085         {
1086             /*
1087              * This IF has a corresponding ELSE. The IF block has no exit,
1088              * (it contains an unconditional Return)
1089              * mark the ELSE block to remember this fact.
1090              */
1091             Op->Asl.Next->Asl.CompileFlags |= OP_IF_HAS_NO_EXIT;
1092         }
1093         break;
1094 
1095     case PARSEOP_ELSE:
1096 
1097         if ((Op->Asl.CompileFlags & OP_HAS_NO_EXIT) &&
1098             (Op->Asl.CompileFlags & OP_IF_HAS_NO_EXIT))
1099         {
1100             /*
1101              * This ELSE block has no exit and the corresponding IF block
1102              * has no exit either. Therefore, the parent node has no exit.
1103              */
1104             Op->Asl.Parent->Asl.CompileFlags |= OP_HAS_NO_EXIT;
1105         }
1106         break;
1107 
1108 
1109     default:
1110 
1111         if ((Op->Asl.CompileFlags & OP_HAS_NO_EXIT) &&
1112             (Op->Asl.Parent))
1113         {
1114             /* If this node has no exit, then the parent has no exit either */
1115 
1116             Op->Asl.Parent->Asl.CompileFlags |= OP_HAS_NO_EXIT;
1117         }
1118         break;
1119     }
1120 
1121     return (AE_OK);
1122 }
1123