1 /******************************************************************************
3 * Module Name: aslmethod.c - Control method analysis walk
5 *****************************************************************************/
8 * Copyright (C) 2000 - 2016, Intel Corp.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
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.
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.
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.
44 #include <contrib/dev/acpica/compiler/aslcompiler.h>
45 #include "aslcompiler.y.h"
46 #include <contrib/dev/acpica/include/acparser.h>
47 #include <contrib/dev/acpica/include/amlcode.h>
50 #define _COMPONENT ACPI_COMPILER
51 ACPI_MODULE_NAME ("aslmethod")
54 /* Local prototypes */
57 MtCheckNamedObjectInMethod (
58 ACPI_PARSE_OBJECT *Op,
59 ASL_METHOD_INFO *MethodInfo);
62 /*******************************************************************************
64 * FUNCTION: MtMethodAnalysisWalkBegin
66 * PARAMETERS: ASL_WALK_CALLBACK
70 * DESCRIPTION: Descending callback for the analysis walk. Check methods for:
71 * 1) Initialized local variables
75 ******************************************************************************/
78 MtMethodAnalysisWalkBegin (
79 ACPI_PARSE_OBJECT *Op,
83 ASL_ANALYSIS_WALK_INFO *WalkInfo = (ASL_ANALYSIS_WALK_INFO *) Context;
84 ASL_METHOD_INFO *MethodInfo = WalkInfo->MethodStack;
85 ACPI_PARSE_OBJECT *Next;
86 UINT32 RegisterNumber;
88 char LocalName[] = "Local0";
89 char ArgName[] = "Arg0";
90 ACPI_PARSE_OBJECT *ArgNode;
91 ACPI_PARSE_OBJECT *NextType;
92 ACPI_PARSE_OBJECT *NextParamType;
96 /* Build cross-reference output file if requested */
98 if (Gbl_CrossReferenceOutput)
100 OtXrefWalkPart1 (Op, Level, MethodInfo);
103 switch (Op->Asl.ParseOpcode)
109 /* Create and init method info */
111 MethodInfo = UtLocalCalloc (sizeof (ASL_METHOD_INFO));
112 MethodInfo->Next = WalkInfo->MethodStack;
115 WalkInfo->MethodStack = MethodInfo;
118 * Special handling for _PSx methods. Dependency rules (same scope):
120 * 1) _PS0 - One of these must exist: _PS1, _PS2, _PS3
121 * 2) _PS1/_PS2/_PS3: A _PS0 must exist
123 if (ACPI_COMPARE_NAME (METHOD_NAME__PS0, Op->Asl.NameSeg))
125 /* For _PS0, one of _PS1/_PS2/_PS3 must exist */
127 if ((!ApFindNameInScope (METHOD_NAME__PS1, Op)) &&
128 (!ApFindNameInScope (METHOD_NAME__PS2, Op)) &&
129 (!ApFindNameInScope (METHOD_NAME__PS3, Op)))
131 AslError (ASL_WARNING, ASL_MSG_MISSING_DEPENDENCY, Op,
132 "_PS0 requires one of _PS1/_PS2/_PS3 in same scope");
136 ACPI_COMPARE_NAME (METHOD_NAME__PS1, Op->Asl.NameSeg) ||
137 ACPI_COMPARE_NAME (METHOD_NAME__PS2, Op->Asl.NameSeg) ||
138 ACPI_COMPARE_NAME (METHOD_NAME__PS3, Op->Asl.NameSeg))
140 /* For _PS1/_PS2/_PS3, a _PS0 must exist */
142 if (!ApFindNameInScope (METHOD_NAME__PS0, Op))
145 "%4.4s requires _PS0 in same scope", Op->Asl.NameSeg);
147 AslError (ASL_WARNING, ASL_MSG_MISSING_DEPENDENCY, Op,
152 /* Get the name node */
154 Next = Op->Asl.Child;
156 /* Get the NumArguments node */
158 Next = Next->Asl.Next;
159 MethodInfo->NumArguments = (UINT8)
160 (((UINT8) Next->Asl.Value.Integer) & 0x07);
162 /* Get the SerializeRule and SyncLevel nodes, ignored here */
164 Next = Next->Asl.Next;
165 MethodInfo->ShouldBeSerialized = (UINT8) Next->Asl.Value.Integer;
167 Next = Next->Asl.Next;
170 /* Get the ReturnType node */
172 Next = Next->Asl.Next;
174 NextType = Next->Asl.Child;
177 /* Get and map each of the ReturnTypes */
179 MethodInfo->ValidReturnTypes |= AnMapObjTypeToBtype (NextType);
180 NextType->Asl.ParseOpcode = PARSEOP_DEFAULT_ARG;
181 NextType = NextType->Asl.Next;
184 /* Get the ParameterType node */
186 Next = Next->Asl.Next;
188 NextType = Next->Asl.Child;
191 if (NextType->Asl.ParseOpcode == PARSEOP_DEFAULT_ARG)
193 NextParamType = NextType->Asl.Child;
194 while (NextParamType)
196 MethodInfo->ValidArgTypes[ActualArgs] |=
197 AnMapObjTypeToBtype (NextParamType);
199 NextParamType->Asl.ParseOpcode = PARSEOP_DEFAULT_ARG;
200 NextParamType = NextParamType->Asl.Next;
205 MethodInfo->ValidArgTypes[ActualArgs] =
206 AnMapObjTypeToBtype (NextType);
208 NextType->Asl.ParseOpcode = PARSEOP_DEFAULT_ARG;
212 NextType = NextType->Asl.Next;
215 if ((MethodInfo->NumArguments) &&
216 (MethodInfo->NumArguments != ActualArgs))
218 /* error: Param list did not match number of args */
221 /* Allow numarguments == 0 for Function() */
223 if ((!MethodInfo->NumArguments) && (ActualArgs))
225 MethodInfo->NumArguments = ActualArgs;
226 ArgNode->Asl.Value.Integer |= ActualArgs;
230 * Actual arguments are initialized at method entry.
231 * All other ArgX "registers" can be used as locals, so we
232 * track their initialization.
234 for (i = 0; i < MethodInfo->NumArguments; i++)
236 MethodInfo->ArgInitialized[i] = TRUE;
240 case PARSEOP_METHODCALL:
243 (Op->Asl.Node == MethodInfo->Op->Asl.Node))
245 AslError (ASL_REMARK, ASL_MSG_RECURSION, Op, Op->Asl.ExternalName);
261 * Local was used outside a control method, or there was an error
262 * in the method declaration.
264 AslError (ASL_REMARK, ASL_MSG_LOCAL_OUTSIDE_METHOD,
265 Op, Op->Asl.ExternalName);
269 RegisterNumber = (Op->Asl.AmlOpcode & 0x0007);
272 * If the local is being used as a target, mark the local
275 if (Op->Asl.CompileFlags & NODE_IS_TARGET)
277 MethodInfo->LocalInitialized[RegisterNumber] = TRUE;
281 * Otherwise, this is a reference, check if the local
282 * has been previously initialized.
284 * The only operator that accepts an uninitialized value is ObjectType()
286 else if ((!MethodInfo->LocalInitialized[RegisterNumber]) &&
287 (Op->Asl.Parent->Asl.ParseOpcode != PARSEOP_OBJECTTYPE))
289 LocalName[strlen (LocalName) -1] = (char) (RegisterNumber + 0x30);
290 AslError (ASL_ERROR, ASL_MSG_LOCAL_INIT, Op, LocalName);
305 * Arg was used outside a control method, or there was an error
306 * in the method declaration.
308 AslError (ASL_REMARK, ASL_MSG_LOCAL_OUTSIDE_METHOD,
309 Op, Op->Asl.ExternalName);
313 RegisterNumber = (Op->Asl.AmlOpcode & 0x000F) - 8;
314 ArgName[strlen (ArgName) -1] = (char) (RegisterNumber + 0x30);
317 * If the Arg is being used as a target, mark the local
320 if (Op->Asl.CompileFlags & NODE_IS_TARGET)
322 MethodInfo->ArgInitialized[RegisterNumber] = TRUE;
326 * Otherwise, this is a reference, check if the Arg
327 * has been previously initialized.
329 * The only operator that accepts an uninitialized value is ObjectType()
331 else if ((!MethodInfo->ArgInitialized[RegisterNumber]) &&
332 (Op->Asl.Parent->Asl.ParseOpcode != PARSEOP_OBJECTTYPE))
334 AslError (ASL_ERROR, ASL_MSG_ARG_INIT, Op, ArgName);
337 /* Flag this arg if it is not a "real" argument to the method */
339 if (RegisterNumber >= MethodInfo->NumArguments)
341 AslError (ASL_REMARK, ASL_MSG_NOT_PARAMETER, Op, ArgName);
350 * Probably was an error in the method declaration,
351 * no additional error here
353 ACPI_WARNING ((AE_INFO, "%p, No parent method", Op));
358 * A child indicates a possible return value. A simple Return or
359 * Return() is marked with NODE_IS_NULL_RETURN by the parser so
360 * that it is not counted as a "real" return-with-value, although
361 * the AML code that is actually emitted is Return(0). The AML
362 * definition of Return has a required parameter, so we are
363 * forced to convert a null return to Return(0).
365 if ((Op->Asl.Child) &&
366 (Op->Asl.Child->Asl.ParseOpcode != PARSEOP_DEFAULT_ARG) &&
367 (!(Op->Asl.Child->Asl.CompileFlags & NODE_IS_NULL_RETURN)))
369 MethodInfo->NumReturnWithValue++;
373 MethodInfo->NumReturnNoValue++;
378 case PARSEOP_CONTINUE:
380 Next = Op->Asl.Parent;
383 if (Next->Asl.ParseOpcode == PARSEOP_WHILE)
387 Next = Next->Asl.Parent;
392 AslError (ASL_ERROR, ASL_MSG_NO_WHILE, Op, NULL);
398 /* We can range check if the argument is an integer */
400 if ((Op->Asl.Child->Asl.ParseOpcode == PARSEOP_INTEGER) &&
401 (Op->Asl.Child->Asl.Value.Integer > ACPI_UINT8_MAX))
403 AslError (ASL_ERROR, ASL_MSG_INVALID_TIME, Op, NULL);
409 if (!ApFindNameInDeviceTree (METHOD_NAME__HID, Op) &&
410 !ApFindNameInDeviceTree (METHOD_NAME__ADR, Op))
412 AslError (ASL_WARNING, ASL_MSG_MISSING_DEPENDENCY, Op,
413 "Device object requires a _HID or _ADR in same scope");
419 case PARSEOP_OPERATIONREGION:
420 case PARSEOP_POWERRESOURCE:
421 case PARSEOP_PROCESSOR:
422 case PARSEOP_THERMALZONE:
425 * The first operand is a name to be created in the namespace.
426 * Check against the reserved list.
428 i = ApCheckForPredefinedName (Op, Op->Asl.NameSeg);
429 if (i < ACPI_VALID_RESERVED_NAME_MAX)
431 AslError (ASL_ERROR, ASL_MSG_RESERVED_USE,
432 Op, Op->Asl.ExternalName);
438 /* Typecheck any predefined names statically defined with Name() */
440 ApCheckForPredefinedObject (Op, Op->Asl.NameSeg);
442 /* Special typechecking for _HID */
444 if (!strcmp (METHOD_NAME__HID, Op->Asl.NameSeg))
446 Next = Op->Asl.Child->Asl.Next;
447 AnCheckId (Next, ASL_TYPE_HID);
450 /* Special typechecking for _CID */
452 else if (!strcmp (METHOD_NAME__CID, Op->Asl.NameSeg))
454 Next = Op->Asl.Child->Asl.Next;
456 if ((Next->Asl.ParseOpcode == PARSEOP_PACKAGE) ||
457 (Next->Asl.ParseOpcode == PARSEOP_VAR_PACKAGE))
459 Next = Next->Asl.Child;
462 AnCheckId (Next, ASL_TYPE_CID);
463 Next = Next->Asl.Next;
468 AnCheckId (Next, ASL_TYPE_CID);
479 /* Check for named object creation within a non-serialized method */
481 MtCheckNamedObjectInMethod (Op, MethodInfo);
486 /*******************************************************************************
488 * FUNCTION: MtCheckNamedObjectInMethod
490 * PARAMETERS: Op - Current parser op
491 * MethodInfo - Info for method being parsed
495 * DESCRIPTION: Detect if a non-serialized method is creating a named object,
496 * which could possibly cause problems if two threads execute
497 * the method concurrently. Emit a remark in this case.
499 ******************************************************************************/
502 MtCheckNamedObjectInMethod (
503 ACPI_PARSE_OBJECT *Op,
504 ASL_METHOD_INFO *MethodInfo)
506 const ACPI_OPCODE_INFO *OpInfo;
509 /* We don't care about actual method declarations or scopes */
511 if ((Op->Asl.AmlOpcode == AML_METHOD_OP) ||
512 (Op->Asl.AmlOpcode == AML_SCOPE_OP))
517 /* Determine if we are creating a named object */
519 OpInfo = AcpiPsGetOpcodeInfo (Op->Asl.AmlOpcode);
520 if (OpInfo->Class == AML_CLASS_NAMED_OBJECT)
523 * If we have a named object created within a non-serialized method,
524 * emit a remark that the method should be serialized.
526 * Reason: If a thread blocks within the method for any reason, and
527 * another thread enters the method, the method will fail because an
528 * attempt will be made to create the same object twice.
530 if (MethodInfo && !MethodInfo->ShouldBeSerialized)
532 AslError (ASL_REMARK, ASL_MSG_SERIALIZED_REQUIRED, MethodInfo->Op,
533 "due to creation of named objects within");
535 /* Emit message only ONCE per method */
537 MethodInfo->ShouldBeSerialized = TRUE;
543 /*******************************************************************************
545 * FUNCTION: MtMethodAnalysisWalkEnd
547 * PARAMETERS: ASL_WALK_CALLBACK
551 * DESCRIPTION: Ascending callback for analysis walk. Complete method
554 ******************************************************************************/
557 MtMethodAnalysisWalkEnd (
558 ACPI_PARSE_OBJECT *Op,
562 ASL_ANALYSIS_WALK_INFO *WalkInfo = (ASL_ANALYSIS_WALK_INFO *) Context;
563 ASL_METHOD_INFO *MethodInfo = WalkInfo->MethodStack;
566 switch (Op->Asl.ParseOpcode)
573 printf ("No method info for method! [%s]\n", Op->Asl.Namepath);
574 AslError (ASL_ERROR, ASL_MSG_COMPILER_INTERNAL, Op,
575 "No method info for this method");
578 return (AE_AML_INTERNAL);
587 switch (Op->Asl.ParseOpcode)
591 WalkInfo->MethodStack = MethodInfo->Next;
594 * Check if there is no return statement at the end of the
595 * method AND we can actually get there -- i.e., the execution
596 * of the method can possibly terminate without a return statement.
598 if ((!AnLastStatementIsReturn (Op)) &&
599 (!(Op->Asl.CompileFlags & NODE_HAS_NO_EXIT)))
602 * No return statement, and execution can possibly exit
603 * via this path. This is equivalent to Return ()
605 MethodInfo->NumReturnNoValue++;
609 * Check for case where some return statements have a return value
610 * and some do not. Exit without a return statement is a return with
613 if (MethodInfo->NumReturnNoValue &&
614 MethodInfo->NumReturnWithValue)
616 AslError (ASL_WARNING, ASL_MSG_RETURN_TYPES, Op,
617 Op->Asl.ExternalName);
621 * If there are any RETURN() statements with no value, or there is a
622 * control path that allows the method to exit without a return value,
623 * we mark the method as a method that does not return a value. This
624 * knowledge can be used to check method invocations that expect a
627 if (MethodInfo->NumReturnNoValue)
629 if (MethodInfo->NumReturnWithValue)
631 Op->Asl.CompileFlags |= NODE_METHOD_SOME_NO_RETVAL;
635 Op->Asl.CompileFlags |= NODE_METHOD_NO_RETVAL;
640 * Check predefined method names for correct return behavior
641 * and correct number of arguments. Also, some special checks
642 * For GPE and _REG methods.
644 if (ApCheckForPredefinedMethod (Op, MethodInfo))
646 /* Special check for two names like _L01 and _E01 in same scope */
648 ApCheckForGpeNameConflict (Op);
651 * Special check for _REG: Must have an operation region definition
652 * within the same scope!
654 ApCheckRegMethod (Op);
657 ACPI_FREE (MethodInfo);
662 /* Special check for two names like _L01 and _E01 in same scope */
664 ApCheckForGpeNameConflict (Op);
670 * If the parent is a predefined method name, attempt to typecheck
671 * the return value. Only static types can be validated.
673 ApCheckPredefinedReturnValue (Op, MethodInfo);
676 * The parent block does not "exit" and continue execution -- the
677 * method is terminated here with the Return() statement.
679 Op->Asl.Parent->Asl.CompileFlags |= NODE_HAS_NO_EXIT;
681 /* Used in the "typing" pass later */
683 Op->Asl.ParentMethod = MethodInfo->Op;
686 * If there is a peer node after the return statement, then this
687 * node is unreachable code -- i.e., it won't be executed because of
688 * the preceding Return() statement.
692 AslError (ASL_WARNING, ASL_MSG_UNREACHABLE_CODE,
699 if ((Op->Asl.CompileFlags & NODE_HAS_NO_EXIT) &&
701 (Op->Asl.Next->Asl.ParseOpcode == PARSEOP_ELSE))
704 * This IF has a corresponding ELSE. The IF block has no exit,
705 * (it contains an unconditional Return)
706 * mark the ELSE block to remember this fact.
708 Op->Asl.Next->Asl.CompileFlags |= NODE_IF_HAS_NO_EXIT;
714 if ((Op->Asl.CompileFlags & NODE_HAS_NO_EXIT) &&
715 (Op->Asl.CompileFlags & NODE_IF_HAS_NO_EXIT))
718 * This ELSE block has no exit and the corresponding IF block
719 * has no exit either. Therefore, the parent node has no exit.
721 Op->Asl.Parent->Asl.CompileFlags |= NODE_HAS_NO_EXIT;
728 if ((Op->Asl.CompileFlags & NODE_HAS_NO_EXIT) &&
731 /* If this node has no exit, then the parent has no exit either */
733 Op->Asl.Parent->Asl.CompileFlags |= NODE_HAS_NO_EXIT;