kernel: Sync ACPICA with Intel's version 20140627.
[dragonfly.git] / sys / contrib / dev / acpica / source / compiler / aslmethod.c
1 /******************************************************************************
2  *
3  * Module Name: aslmethod.c - Control method analysis walk
4  *
5  *****************************************************************************/
6
7 /*
8  * Copyright (C) 2000 - 2014, 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
45 #include "aslcompiler.h"
46 #include "aslcompiler.y.h"
47 #include "acparser.h"
48 #include "amlcode.h"
49
50
51 #define _COMPONENT          ACPI_COMPILER
52         ACPI_MODULE_NAME    ("aslmethod")
53
54
55 /* Local prototypes */
56
57 void
58 MtCheckNamedObjectInMethod (
59     ACPI_PARSE_OBJECT       *Op,
60     ASL_METHOD_INFO         *MethodInfo);
61
62
63 /*******************************************************************************
64  *
65  * FUNCTION:    MtMethodAnalysisWalkBegin
66  *
67  * PARAMETERS:  ASL_WALK_CALLBACK
68  *
69  * RETURN:      Status
70  *
71  * DESCRIPTION: Descending callback for the analysis walk. Check methods for:
72  *              1) Initialized local variables
73  *              2) Valid arguments
74  *              3) Return types
75  *
76  ******************************************************************************/
77
78 ACPI_STATUS
79 MtMethodAnalysisWalkBegin (
80     ACPI_PARSE_OBJECT       *Op,
81     UINT32                  Level,
82     void                    *Context)
83 {
84     ASL_ANALYSIS_WALK_INFO  *WalkInfo = (ASL_ANALYSIS_WALK_INFO *) Context;
85     ASL_METHOD_INFO         *MethodInfo = WalkInfo->MethodStack;
86     ACPI_PARSE_OBJECT       *Next;
87     UINT32                  RegisterNumber;
88     UINT32                  i;
89     char                    LocalName[] = "Local0";
90     char                    ArgName[] = "Arg0";
91     ACPI_PARSE_OBJECT       *ArgNode;
92     ACPI_PARSE_OBJECT       *NextType;
93     ACPI_PARSE_OBJECT       *NextParamType;
94     UINT8                   ActualArgs = 0;
95
96
97     switch (Op->Asl.ParseOpcode)
98     {
99     case PARSEOP_METHOD:
100
101         TotalMethods++;
102
103         /* Create and init method info */
104
105         MethodInfo       = UtLocalCalloc (sizeof (ASL_METHOD_INFO));
106         MethodInfo->Next = WalkInfo->MethodStack;
107         MethodInfo->Op = Op;
108
109         WalkInfo->MethodStack = MethodInfo;
110
111         /* Get the name node */
112
113         Next = Op->Asl.Child;
114
115         /* Get the NumArguments node */
116
117         Next = Next->Asl.Next;
118         MethodInfo->NumArguments = (UINT8)
119             (((UINT8) Next->Asl.Value.Integer) & 0x07);
120
121         /* Get the SerializeRule and SyncLevel nodes, ignored here */
122
123         Next = Next->Asl.Next;
124         MethodInfo->ShouldBeSerialized = (UINT8) Next->Asl.Value.Integer;
125
126         Next = Next->Asl.Next;
127         ArgNode = Next;
128
129         /* Get the ReturnType node */
130
131         Next = Next->Asl.Next;
132
133         NextType = Next->Asl.Child;
134         while (NextType)
135         {
136             /* Get and map each of the ReturnTypes */
137
138             MethodInfo->ValidReturnTypes |= AnMapObjTypeToBtype (NextType);
139             NextType->Asl.ParseOpcode = PARSEOP_DEFAULT_ARG;
140             NextType = NextType->Asl.Next;
141         }
142
143         /* Get the ParameterType node */
144
145         Next = Next->Asl.Next;
146
147         NextType = Next->Asl.Child;
148         while (NextType)
149         {
150             if (NextType->Asl.ParseOpcode == PARSEOP_DEFAULT_ARG)
151             {
152                 NextParamType = NextType->Asl.Child;
153                 while (NextParamType)
154                 {
155                     MethodInfo->ValidArgTypes[ActualArgs] |= AnMapObjTypeToBtype (NextParamType);
156                     NextParamType->Asl.ParseOpcode = PARSEOP_DEFAULT_ARG;
157                     NextParamType = NextParamType->Asl.Next;
158                 }
159             }
160             else
161             {
162                 MethodInfo->ValidArgTypes[ActualArgs] =
163                     AnMapObjTypeToBtype (NextType);
164                 NextType->Asl.ParseOpcode = PARSEOP_DEFAULT_ARG;
165                 ActualArgs++;
166             }
167
168             NextType = NextType->Asl.Next;
169         }
170
171         if ((MethodInfo->NumArguments) &&
172             (MethodInfo->NumArguments != ActualArgs))
173         {
174             /* error: Param list did not match number of args */
175         }
176
177         /* Allow numarguments == 0 for Function() */
178
179         if ((!MethodInfo->NumArguments) && (ActualArgs))
180         {
181             MethodInfo->NumArguments = ActualArgs;
182             ArgNode->Asl.Value.Integer |= ActualArgs;
183         }
184
185         /*
186          * Actual arguments are initialized at method entry.
187          * All other ArgX "registers" can be used as locals, so we
188          * track their initialization.
189          */
190         for (i = 0; i < MethodInfo->NumArguments; i++)
191         {
192             MethodInfo->ArgInitialized[i] = TRUE;
193         }
194         break;
195
196     case PARSEOP_METHODCALL:
197
198         if (MethodInfo &&
199            (Op->Asl.Node == MethodInfo->Op->Asl.Node))
200         {
201             AslError (ASL_REMARK, ASL_MSG_RECURSION, Op, Op->Asl.ExternalName);
202         }
203         break;
204
205     case PARSEOP_LOCAL0:
206     case PARSEOP_LOCAL1:
207     case PARSEOP_LOCAL2:
208     case PARSEOP_LOCAL3:
209     case PARSEOP_LOCAL4:
210     case PARSEOP_LOCAL5:
211     case PARSEOP_LOCAL6:
212     case PARSEOP_LOCAL7:
213
214         if (!MethodInfo)
215         {
216             /*
217              * Local was used outside a control method, or there was an error
218              * in the method declaration.
219              */
220             AslError (ASL_REMARK, ASL_MSG_LOCAL_OUTSIDE_METHOD, Op, Op->Asl.ExternalName);
221             return (AE_ERROR);
222         }
223
224         RegisterNumber = (Op->Asl.AmlOpcode & 0x000F);
225
226         /*
227          * If the local is being used as a target, mark the local
228          * initialized
229          */
230         if (Op->Asl.CompileFlags & NODE_IS_TARGET)
231         {
232             MethodInfo->LocalInitialized[RegisterNumber] = TRUE;
233         }
234
235         /*
236          * Otherwise, this is a reference, check if the local
237          * has been previously initialized.
238          *
239          * The only operator that accepts an uninitialized value is ObjectType()
240          */
241         else if ((!MethodInfo->LocalInitialized[RegisterNumber]) &&
242                  (Op->Asl.Parent->Asl.ParseOpcode != PARSEOP_OBJECTTYPE))
243         {
244             LocalName[strlen (LocalName) -1] = (char) (RegisterNumber + 0x30);
245             AslError (ASL_ERROR, ASL_MSG_LOCAL_INIT, Op, LocalName);
246         }
247         break;
248
249     case PARSEOP_ARG0:
250     case PARSEOP_ARG1:
251     case PARSEOP_ARG2:
252     case PARSEOP_ARG3:
253     case PARSEOP_ARG4:
254     case PARSEOP_ARG5:
255     case PARSEOP_ARG6:
256
257         if (!MethodInfo)
258         {
259             /*
260              * Arg was used outside a control method, or there was an error
261              * in the method declaration.
262              */
263             AslError (ASL_REMARK, ASL_MSG_LOCAL_OUTSIDE_METHOD, Op, Op->Asl.ExternalName);
264             return (AE_ERROR);
265         }
266
267         RegisterNumber = (Op->Asl.AmlOpcode & 0x000F) - 8;
268         ArgName[strlen (ArgName) -1] = (char) (RegisterNumber + 0x30);
269
270         /*
271          * If the Arg is being used as a target, mark the local
272          * initialized
273          */
274         if (Op->Asl.CompileFlags & NODE_IS_TARGET)
275         {
276             MethodInfo->ArgInitialized[RegisterNumber] = TRUE;
277         }
278
279         /*
280          * Otherwise, this is a reference, check if the Arg
281          * has been previously initialized.
282          *
283          * The only operator that accepts an uninitialized value is ObjectType()
284          */
285         else if ((!MethodInfo->ArgInitialized[RegisterNumber]) &&
286                  (Op->Asl.Parent->Asl.ParseOpcode != PARSEOP_OBJECTTYPE))
287         {
288             AslError (ASL_ERROR, ASL_MSG_ARG_INIT, Op, ArgName);
289         }
290
291         /* Flag this arg if it is not a "real" argument to the method */
292
293         if (RegisterNumber >= MethodInfo->NumArguments)
294         {
295             AslError (ASL_REMARK, ASL_MSG_NOT_PARAMETER, Op, ArgName);
296         }
297         break;
298
299     case PARSEOP_RETURN:
300
301         if (!MethodInfo)
302         {
303             /*
304              * Probably was an error in the method declaration,
305              * no additional error here
306              */
307             ACPI_WARNING ((AE_INFO, "%p, No parent method", Op));
308             return (AE_ERROR);
309         }
310
311         /*
312          * A child indicates a possible return value. A simple Return or
313          * Return() is marked with NODE_IS_NULL_RETURN by the parser so
314          * that it is not counted as a "real" return-with-value, although
315          * the AML code that is actually emitted is Return(0). The AML
316          * definition of Return has a required parameter, so we are
317          * forced to convert a null return to Return(0).
318          */
319         if ((Op->Asl.Child) &&
320             (Op->Asl.Child->Asl.ParseOpcode != PARSEOP_DEFAULT_ARG) &&
321             (!(Op->Asl.Child->Asl.CompileFlags & NODE_IS_NULL_RETURN)))
322         {
323             MethodInfo->NumReturnWithValue++;
324         }
325         else
326         {
327             MethodInfo->NumReturnNoValue++;
328         }
329         break;
330
331     case PARSEOP_BREAK:
332     case PARSEOP_CONTINUE:
333
334         Next = Op->Asl.Parent;
335         while (Next)
336         {
337             if (Next->Asl.ParseOpcode == PARSEOP_WHILE)
338             {
339                 break;
340             }
341             Next = Next->Asl.Parent;
342         }
343
344         if (!Next)
345         {
346             AslError (ASL_ERROR, ASL_MSG_NO_WHILE, Op, NULL);
347         }
348         break;
349
350     case PARSEOP_STALL:
351
352         /* We can range check if the argument is an integer */
353
354         if ((Op->Asl.Child->Asl.ParseOpcode == PARSEOP_INTEGER) &&
355             (Op->Asl.Child->Asl.Value.Integer > ACPI_UINT8_MAX))
356         {
357             AslError (ASL_ERROR, ASL_MSG_INVALID_TIME, Op, NULL);
358         }
359         break;
360
361     case PARSEOP_DEVICE:
362     case PARSEOP_EVENT:
363     case PARSEOP_MUTEX:
364     case PARSEOP_OPERATIONREGION:
365     case PARSEOP_POWERRESOURCE:
366     case PARSEOP_PROCESSOR:
367     case PARSEOP_THERMALZONE:
368
369         /*
370          * The first operand is a name to be created in the namespace.
371          * Check against the reserved list.
372          */
373         i = ApCheckForPredefinedName (Op, Op->Asl.NameSeg);
374         if (i < ACPI_VALID_RESERVED_NAME_MAX)
375         {
376             AslError (ASL_ERROR, ASL_MSG_RESERVED_USE, Op, Op->Asl.ExternalName);
377         }
378         break;
379
380     case PARSEOP_NAME:
381
382         /* Typecheck any predefined names statically defined with Name() */
383
384         ApCheckForPredefinedObject (Op, Op->Asl.NameSeg);
385
386         /* Special typechecking for _HID */
387
388         if (!ACPI_STRCMP (METHOD_NAME__HID, Op->Asl.NameSeg))
389         {
390             Next = Op->Asl.Child->Asl.Next;
391             AnCheckId (Next, ASL_TYPE_HID);
392         }
393
394         /* Special typechecking for _CID */
395
396         else if (!ACPI_STRCMP (METHOD_NAME__CID, Op->Asl.NameSeg))
397         {
398             Next = Op->Asl.Child->Asl.Next;
399
400             if ((Next->Asl.ParseOpcode == PARSEOP_PACKAGE) ||
401                 (Next->Asl.ParseOpcode == PARSEOP_VAR_PACKAGE))
402             {
403                 Next = Next->Asl.Child;
404                 while (Next)
405                 {
406                     AnCheckId (Next, ASL_TYPE_CID);
407                     Next = Next->Asl.Next;
408                 }
409             }
410             else
411             {
412                 AnCheckId (Next, ASL_TYPE_CID);
413             }
414         }
415
416         break;
417
418     default:
419
420         break;
421     }
422
423     /* Check for named object creation within a non-serialized method */
424
425     MtCheckNamedObjectInMethod (Op, MethodInfo);
426     return (AE_OK);
427 }
428
429
430 /*******************************************************************************
431  *
432  * FUNCTION:    MtCheckNamedObjectInMethod
433  *
434  * PARAMETERS:  Op                  - Current parser op
435  *              MethodInfo          - Info for method being parsed
436  *
437  * RETURN:      None
438  *
439  * DESCRIPTION: Detect if a non-serialized method is creating a named object,
440  *              which could possibly cause problems if two threads execute
441  *              the method concurrently. Emit a remark in this case.
442  *
443  ******************************************************************************/
444
445 void
446 MtCheckNamedObjectInMethod (
447     ACPI_PARSE_OBJECT       *Op,
448     ASL_METHOD_INFO         *MethodInfo)
449 {
450     const ACPI_OPCODE_INFO  *OpInfo;
451
452
453     /* We don't care about actual method declarations */
454
455     if (Op->Asl.AmlOpcode == AML_METHOD_OP)
456     {
457         return;
458     }
459
460     /* Determine if we are creating a named object */
461
462     OpInfo = AcpiPsGetOpcodeInfo (Op->Asl.AmlOpcode);
463     if (OpInfo->Class == AML_CLASS_NAMED_OBJECT)
464     {
465         /*
466          * If we have a named object created within a non-serialized method,
467          * emit a remark that the method should be serialized.
468          *
469          * Reason: If a thread blocks within the method for any reason, and
470          * another thread enters the method, the method will fail because an
471          * attempt will be made to create the same object twice.
472          */
473         if (MethodInfo && !MethodInfo->ShouldBeSerialized)
474         {
475             AslError (ASL_REMARK, ASL_MSG_SERIALIZED_REQUIRED, MethodInfo->Op,
476                 "due to creation of named objects within");
477
478             /* Emit message only ONCE per method */
479
480             MethodInfo->ShouldBeSerialized = TRUE;
481         }
482     }
483 }
484
485
486 /*******************************************************************************
487  *
488  * FUNCTION:    MtMethodAnalysisWalkEnd
489  *
490  * PARAMETERS:  ASL_WALK_CALLBACK
491  *
492  * RETURN:      Status
493  *
494  * DESCRIPTION: Ascending callback for analysis walk. Complete method
495  *              return analysis.
496  *
497  ******************************************************************************/
498
499 ACPI_STATUS
500 MtMethodAnalysisWalkEnd (
501     ACPI_PARSE_OBJECT       *Op,
502     UINT32                  Level,
503     void                    *Context)
504 {
505     ASL_ANALYSIS_WALK_INFO  *WalkInfo = (ASL_ANALYSIS_WALK_INFO *) Context;
506     ASL_METHOD_INFO         *MethodInfo = WalkInfo->MethodStack;
507
508
509     switch (Op->Asl.ParseOpcode)
510     {
511     case PARSEOP_METHOD:
512     case PARSEOP_RETURN:
513
514         if (!MethodInfo)
515         {
516             printf ("No method info for method! [%s]\n", Op->Asl.Namepath);
517             AslError (ASL_ERROR, ASL_MSG_COMPILER_INTERNAL, Op,
518                 "No method info for this method");
519
520             CmCleanupAndExit ();
521             return (AE_AML_INTERNAL);
522         }
523         break;
524
525     default:
526
527         break;
528     }
529
530     switch (Op->Asl.ParseOpcode)
531     {
532     case PARSEOP_METHOD:
533
534         WalkInfo->MethodStack = MethodInfo->Next;
535
536         /*
537          * Check if there is no return statement at the end of the
538          * method AND we can actually get there -- i.e., the execution
539          * of the method can possibly terminate without a return statement.
540          */
541         if ((!AnLastStatementIsReturn (Op)) &&
542             (!(Op->Asl.CompileFlags & NODE_HAS_NO_EXIT)))
543         {
544             /*
545              * No return statement, and execution can possibly exit
546              * via this path. This is equivalent to Return ()
547              */
548             MethodInfo->NumReturnNoValue++;
549         }
550
551         /*
552          * Check for case where some return statements have a return value
553          * and some do not. Exit without a return statement is a return with
554          * no value
555          */
556         if (MethodInfo->NumReturnNoValue &&
557             MethodInfo->NumReturnWithValue)
558         {
559             AslError (ASL_WARNING, ASL_MSG_RETURN_TYPES, Op,
560                 Op->Asl.ExternalName);
561         }
562
563         /*
564          * If there are any RETURN() statements with no value, or there is a
565          * control path that allows the method to exit without a return value,
566          * we mark the method as a method that does not return a value. This
567          * knowledge can be used to check method invocations that expect a
568          * returned value.
569          */
570         if (MethodInfo->NumReturnNoValue)
571         {
572             if (MethodInfo->NumReturnWithValue)
573             {
574                 Op->Asl.CompileFlags |= NODE_METHOD_SOME_NO_RETVAL;
575             }
576             else
577             {
578                 Op->Asl.CompileFlags |= NODE_METHOD_NO_RETVAL;
579             }
580         }
581
582         /*
583          * Check predefined method names for correct return behavior
584          * and correct number of arguments. Also, some special checks
585          * For GPE and _REG methods.
586          */
587         if (ApCheckForPredefinedMethod (Op, MethodInfo))
588         {
589             /* Special check for two names like _L01 and _E01 in same scope */
590
591             ApCheckForGpeNameConflict (Op);
592
593             /*
594              * Special check for _REG: Must have an operation region definition
595              * within the same scope!
596              */
597             ApCheckRegMethod (Op);
598         }
599
600         ACPI_FREE (MethodInfo);
601         break;
602
603     case PARSEOP_NAME:
604
605          /* Special check for two names like _L01 and _E01 in same scope */
606
607         ApCheckForGpeNameConflict (Op);
608         break;
609
610     case PARSEOP_RETURN:
611
612         /*
613          * If the parent is a predefined method name, attempt to typecheck
614          * the return value. Only static types can be validated.
615          */
616         ApCheckPredefinedReturnValue (Op, MethodInfo);
617
618         /*
619          * The parent block does not "exit" and continue execution -- the
620          * method is terminated here with the Return() statement.
621          */
622         Op->Asl.Parent->Asl.CompileFlags |= NODE_HAS_NO_EXIT;
623
624         /* Used in the "typing" pass later */
625
626         Op->Asl.ParentMethod = MethodInfo->Op;
627
628         /*
629          * If there is a peer node after the return statement, then this
630          * node is unreachable code -- i.e., it won't be executed because of
631          * the preceding Return() statement.
632          */
633         if (Op->Asl.Next)
634         {
635             AslError (ASL_WARNING, ASL_MSG_UNREACHABLE_CODE, Op->Asl.Next, NULL);
636         }
637         break;
638
639     case PARSEOP_IF:
640
641         if ((Op->Asl.CompileFlags & NODE_HAS_NO_EXIT) &&
642             (Op->Asl.Next) &&
643             (Op->Asl.Next->Asl.ParseOpcode == PARSEOP_ELSE))
644         {
645             /*
646              * This IF has a corresponding ELSE. The IF block has no exit,
647              * (it contains an unconditional Return)
648              * mark the ELSE block to remember this fact.
649              */
650             Op->Asl.Next->Asl.CompileFlags |= NODE_IF_HAS_NO_EXIT;
651         }
652         break;
653
654     case PARSEOP_ELSE:
655
656         if ((Op->Asl.CompileFlags & NODE_HAS_NO_EXIT) &&
657             (Op->Asl.CompileFlags & NODE_IF_HAS_NO_EXIT))
658         {
659             /*
660              * This ELSE block has no exit and the corresponding IF block
661              * has no exit either. Therefore, the parent node has no exit.
662              */
663             Op->Asl.Parent->Asl.CompileFlags |= NODE_HAS_NO_EXIT;
664         }
665         break;
666
667
668     default:
669
670         if ((Op->Asl.CompileFlags & NODE_HAS_NO_EXIT) &&
671             (Op->Asl.Parent))
672         {
673             /* If this node has no exit, then the parent has no exit either */
674
675             Op->Asl.Parent->Asl.CompileFlags |= NODE_HAS_NO_EXIT;
676         }
677         break;
678     }
679
680     return (AE_OK);
681 }