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 "aslcompiler.h"
45 #include "aslcompiler.y.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 switch (Op->Asl.ParseOpcode)
102 /* Create and init method info */
104 MethodInfo = UtLocalCalloc (sizeof (ASL_METHOD_INFO));
105 MethodInfo->Next = WalkInfo->MethodStack;
108 WalkInfo->MethodStack = MethodInfo;
111 * Special handling for _PSx methods. Dependency rules (same scope):
113 * 1) _PS0 - One of these must exist: _PS1, _PS2, _PS3
114 * 2) _PS1/_PS2/_PS3: A _PS0 must exist
116 if (ACPI_COMPARE_NAME (METHOD_NAME__PS0, Op->Asl.NameSeg))
118 /* For _PS0, one of _PS1/_PS2/_PS3 must exist */
120 if ((!ApFindNameInScope (METHOD_NAME__PS1, Op)) &&
121 (!ApFindNameInScope (METHOD_NAME__PS2, Op)) &&
122 (!ApFindNameInScope (METHOD_NAME__PS3, Op)))
124 AslError (ASL_WARNING, ASL_MSG_MISSING_DEPENDENCY, Op,
125 "_PS0 requires one of _PS1/_PS2/_PS3 in same scope");
129 ACPI_COMPARE_NAME (METHOD_NAME__PS1, Op->Asl.NameSeg) ||
130 ACPI_COMPARE_NAME (METHOD_NAME__PS2, Op->Asl.NameSeg) ||
131 ACPI_COMPARE_NAME (METHOD_NAME__PS3, Op->Asl.NameSeg))
133 /* For _PS1/_PS2/_PS3, a _PS0 must exist */
135 if (!ApFindNameInScope (METHOD_NAME__PS0, Op))
138 "%4.4s requires _PS0 in same scope", Op->Asl.NameSeg);
140 AslError (ASL_WARNING, ASL_MSG_MISSING_DEPENDENCY, Op,
145 /* Get the name node */
147 Next = Op->Asl.Child;
149 /* Get the NumArguments node */
151 Next = Next->Asl.Next;
152 MethodInfo->NumArguments = (UINT8)
153 (((UINT8) Next->Asl.Value.Integer) & 0x07);
155 /* Get the SerializeRule and SyncLevel nodes, ignored here */
157 Next = Next->Asl.Next;
158 MethodInfo->ShouldBeSerialized = (UINT8) Next->Asl.Value.Integer;
160 Next = Next->Asl.Next;
163 /* Get the ReturnType node */
165 Next = Next->Asl.Next;
167 NextType = Next->Asl.Child;
170 /* Get and map each of the ReturnTypes */
172 MethodInfo->ValidReturnTypes |= AnMapObjTypeToBtype (NextType);
173 NextType->Asl.ParseOpcode = PARSEOP_DEFAULT_ARG;
174 NextType = NextType->Asl.Next;
177 /* Get the ParameterType node */
179 Next = Next->Asl.Next;
181 NextType = Next->Asl.Child;
184 if (NextType->Asl.ParseOpcode == PARSEOP_DEFAULT_ARG)
186 NextParamType = NextType->Asl.Child;
187 while (NextParamType)
189 MethodInfo->ValidArgTypes[ActualArgs] |=
190 AnMapObjTypeToBtype (NextParamType);
192 NextParamType->Asl.ParseOpcode = PARSEOP_DEFAULT_ARG;
193 NextParamType = NextParamType->Asl.Next;
198 MethodInfo->ValidArgTypes[ActualArgs] =
199 AnMapObjTypeToBtype (NextType);
201 NextType->Asl.ParseOpcode = PARSEOP_DEFAULT_ARG;
205 NextType = NextType->Asl.Next;
208 if ((MethodInfo->NumArguments) &&
209 (MethodInfo->NumArguments != ActualArgs))
211 /* error: Param list did not match number of args */
214 /* Allow numarguments == 0 for Function() */
216 if ((!MethodInfo->NumArguments) && (ActualArgs))
218 MethodInfo->NumArguments = ActualArgs;
219 ArgNode->Asl.Value.Integer |= ActualArgs;
223 * Actual arguments are initialized at method entry.
224 * All other ArgX "registers" can be used as locals, so we
225 * track their initialization.
227 for (i = 0; i < MethodInfo->NumArguments; i++)
229 MethodInfo->ArgInitialized[i] = TRUE;
233 case PARSEOP_METHODCALL:
236 (Op->Asl.Node == MethodInfo->Op->Asl.Node))
238 AslError (ASL_REMARK, ASL_MSG_RECURSION, Op, Op->Asl.ExternalName);
254 * Local was used outside a control method, or there was an error
255 * in the method declaration.
257 AslError (ASL_REMARK, ASL_MSG_LOCAL_OUTSIDE_METHOD,
258 Op, Op->Asl.ExternalName);
262 RegisterNumber = (Op->Asl.AmlOpcode & 0x0007);
265 * If the local is being used as a target, mark the local
268 if (Op->Asl.CompileFlags & NODE_IS_TARGET)
270 MethodInfo->LocalInitialized[RegisterNumber] = TRUE;
274 * Otherwise, this is a reference, check if the local
275 * has been previously initialized.
277 * The only operator that accepts an uninitialized value is ObjectType()
279 else if ((!MethodInfo->LocalInitialized[RegisterNumber]) &&
280 (Op->Asl.Parent->Asl.ParseOpcode != PARSEOP_OBJECTTYPE))
282 LocalName[strlen (LocalName) -1] = (char) (RegisterNumber + 0x30);
283 AslError (ASL_ERROR, ASL_MSG_LOCAL_INIT, Op, LocalName);
298 * Arg was used outside a control method, or there was an error
299 * in the method declaration.
301 AslError (ASL_REMARK, ASL_MSG_LOCAL_OUTSIDE_METHOD,
302 Op, Op->Asl.ExternalName);
306 RegisterNumber = (Op->Asl.AmlOpcode & 0x000F) - 8;
307 ArgName[strlen (ArgName) -1] = (char) (RegisterNumber + 0x30);
310 * If the Arg is being used as a target, mark the local
313 if (Op->Asl.CompileFlags & NODE_IS_TARGET)
315 MethodInfo->ArgInitialized[RegisterNumber] = TRUE;
319 * Otherwise, this is a reference, check if the Arg
320 * has been previously initialized.
322 * The only operator that accepts an uninitialized value is ObjectType()
324 else if ((!MethodInfo->ArgInitialized[RegisterNumber]) &&
325 (Op->Asl.Parent->Asl.ParseOpcode != PARSEOP_OBJECTTYPE))
327 AslError (ASL_ERROR, ASL_MSG_ARG_INIT, Op, ArgName);
330 /* Flag this arg if it is not a "real" argument to the method */
332 if (RegisterNumber >= MethodInfo->NumArguments)
334 AslError (ASL_REMARK, ASL_MSG_NOT_PARAMETER, Op, ArgName);
343 * Probably was an error in the method declaration,
344 * no additional error here
346 ACPI_WARNING ((AE_INFO, "%p, No parent method", Op));
351 * A child indicates a possible return value. A simple Return or
352 * Return() is marked with NODE_IS_NULL_RETURN by the parser so
353 * that it is not counted as a "real" return-with-value, although
354 * the AML code that is actually emitted is Return(0). The AML
355 * definition of Return has a required parameter, so we are
356 * forced to convert a null return to Return(0).
358 if ((Op->Asl.Child) &&
359 (Op->Asl.Child->Asl.ParseOpcode != PARSEOP_DEFAULT_ARG) &&
360 (!(Op->Asl.Child->Asl.CompileFlags & NODE_IS_NULL_RETURN)))
362 MethodInfo->NumReturnWithValue++;
366 MethodInfo->NumReturnNoValue++;
371 case PARSEOP_CONTINUE:
373 Next = Op->Asl.Parent;
376 if (Next->Asl.ParseOpcode == PARSEOP_WHILE)
380 Next = Next->Asl.Parent;
385 AslError (ASL_ERROR, ASL_MSG_NO_WHILE, Op, NULL);
391 /* We can range check if the argument is an integer */
393 if ((Op->Asl.Child->Asl.ParseOpcode == PARSEOP_INTEGER) &&
394 (Op->Asl.Child->Asl.Value.Integer > ACPI_UINT8_MAX))
396 AslError (ASL_ERROR, ASL_MSG_INVALID_TIME, Op, NULL);
402 Next = Op->Asl.Child;
404 if (!ApFindNameInScope (METHOD_NAME__HID, Next) &&
405 !ApFindNameInScope (METHOD_NAME__ADR, Next))
407 AslError (ASL_WARNING, ASL_MSG_MISSING_DEPENDENCY, Op,
408 "Device object requires a _HID or _ADR in same scope");
414 case PARSEOP_OPERATIONREGION:
415 case PARSEOP_POWERRESOURCE:
416 case PARSEOP_PROCESSOR:
417 case PARSEOP_THERMALZONE:
420 * The first operand is a name to be created in the namespace.
421 * Check against the reserved list.
423 i = ApCheckForPredefinedName (Op, Op->Asl.NameSeg);
424 if (i < ACPI_VALID_RESERVED_NAME_MAX)
426 AslError (ASL_ERROR, ASL_MSG_RESERVED_USE,
427 Op, Op->Asl.ExternalName);
433 /* Typecheck any predefined names statically defined with Name() */
435 ApCheckForPredefinedObject (Op, Op->Asl.NameSeg);
437 /* Special typechecking for _HID */
439 if (!strcmp (METHOD_NAME__HID, Op->Asl.NameSeg))
441 Next = Op->Asl.Child->Asl.Next;
442 AnCheckId (Next, ASL_TYPE_HID);
445 /* Special typechecking for _CID */
447 else if (!strcmp (METHOD_NAME__CID, Op->Asl.NameSeg))
449 Next = Op->Asl.Child->Asl.Next;
451 if ((Next->Asl.ParseOpcode == PARSEOP_PACKAGE) ||
452 (Next->Asl.ParseOpcode == PARSEOP_VAR_PACKAGE))
454 Next = Next->Asl.Child;
457 AnCheckId (Next, ASL_TYPE_CID);
458 Next = Next->Asl.Next;
463 AnCheckId (Next, ASL_TYPE_CID);
474 /* Check for named object creation within a non-serialized method */
476 MtCheckNamedObjectInMethod (Op, MethodInfo);
481 /*******************************************************************************
483 * FUNCTION: MtCheckNamedObjectInMethod
485 * PARAMETERS: Op - Current parser op
486 * MethodInfo - Info for method being parsed
490 * DESCRIPTION: Detect if a non-serialized method is creating a named object,
491 * which could possibly cause problems if two threads execute
492 * the method concurrently. Emit a remark in this case.
494 ******************************************************************************/
497 MtCheckNamedObjectInMethod (
498 ACPI_PARSE_OBJECT *Op,
499 ASL_METHOD_INFO *MethodInfo)
501 const ACPI_OPCODE_INFO *OpInfo;
504 /* We don't care about actual method declarations or scopes */
506 if ((Op->Asl.AmlOpcode == AML_METHOD_OP) ||
507 (Op->Asl.AmlOpcode == AML_SCOPE_OP))
512 /* Determine if we are creating a named object */
514 OpInfo = AcpiPsGetOpcodeInfo (Op->Asl.AmlOpcode);
515 if (OpInfo->Class == AML_CLASS_NAMED_OBJECT)
518 * If we have a named object created within a non-serialized method,
519 * emit a remark that the method should be serialized.
521 * Reason: If a thread blocks within the method for any reason, and
522 * another thread enters the method, the method will fail because an
523 * attempt will be made to create the same object twice.
525 if (MethodInfo && !MethodInfo->ShouldBeSerialized)
527 AslError (ASL_REMARK, ASL_MSG_SERIALIZED_REQUIRED, MethodInfo->Op,
528 "due to creation of named objects within");
530 /* Emit message only ONCE per method */
532 MethodInfo->ShouldBeSerialized = TRUE;
538 /*******************************************************************************
540 * FUNCTION: MtMethodAnalysisWalkEnd
542 * PARAMETERS: ASL_WALK_CALLBACK
546 * DESCRIPTION: Ascending callback for analysis walk. Complete method
549 ******************************************************************************/
552 MtMethodAnalysisWalkEnd (
553 ACPI_PARSE_OBJECT *Op,
557 ASL_ANALYSIS_WALK_INFO *WalkInfo = (ASL_ANALYSIS_WALK_INFO *) Context;
558 ASL_METHOD_INFO *MethodInfo = WalkInfo->MethodStack;
561 switch (Op->Asl.ParseOpcode)
568 printf ("No method info for method! [%s]\n", Op->Asl.Namepath);
569 AslError (ASL_ERROR, ASL_MSG_COMPILER_INTERNAL, Op,
570 "No method info for this method");
573 return (AE_AML_INTERNAL);
582 switch (Op->Asl.ParseOpcode)
586 WalkInfo->MethodStack = MethodInfo->Next;
589 * Check if there is no return statement at the end of the
590 * method AND we can actually get there -- i.e., the execution
591 * of the method can possibly terminate without a return statement.
593 if ((!AnLastStatementIsReturn (Op)) &&
594 (!(Op->Asl.CompileFlags & NODE_HAS_NO_EXIT)))
597 * No return statement, and execution can possibly exit
598 * via this path. This is equivalent to Return ()
600 MethodInfo->NumReturnNoValue++;
604 * Check for case where some return statements have a return value
605 * and some do not. Exit without a return statement is a return with
608 if (MethodInfo->NumReturnNoValue &&
609 MethodInfo->NumReturnWithValue)
611 AslError (ASL_WARNING, ASL_MSG_RETURN_TYPES, Op,
612 Op->Asl.ExternalName);
616 * If there are any RETURN() statements with no value, or there is a
617 * control path that allows the method to exit without a return value,
618 * we mark the method as a method that does not return a value. This
619 * knowledge can be used to check method invocations that expect a
622 if (MethodInfo->NumReturnNoValue)
624 if (MethodInfo->NumReturnWithValue)
626 Op->Asl.CompileFlags |= NODE_METHOD_SOME_NO_RETVAL;
630 Op->Asl.CompileFlags |= NODE_METHOD_NO_RETVAL;
635 * Check predefined method names for correct return behavior
636 * and correct number of arguments. Also, some special checks
637 * For GPE and _REG methods.
639 if (ApCheckForPredefinedMethod (Op, MethodInfo))
641 /* Special check for two names like _L01 and _E01 in same scope */
643 ApCheckForGpeNameConflict (Op);
646 * Special check for _REG: Must have an operation region definition
647 * within the same scope!
649 ApCheckRegMethod (Op);
652 ACPI_FREE (MethodInfo);
657 /* Special check for two names like _L01 and _E01 in same scope */
659 ApCheckForGpeNameConflict (Op);
665 * If the parent is a predefined method name, attempt to typecheck
666 * the return value. Only static types can be validated.
668 ApCheckPredefinedReturnValue (Op, MethodInfo);
671 * The parent block does not "exit" and continue execution -- the
672 * method is terminated here with the Return() statement.
674 Op->Asl.Parent->Asl.CompileFlags |= NODE_HAS_NO_EXIT;
676 /* Used in the "typing" pass later */
678 Op->Asl.ParentMethod = MethodInfo->Op;
681 * If there is a peer node after the return statement, then this
682 * node is unreachable code -- i.e., it won't be executed because of
683 * the preceding Return() statement.
687 AslError (ASL_WARNING, ASL_MSG_UNREACHABLE_CODE,
694 if ((Op->Asl.CompileFlags & NODE_HAS_NO_EXIT) &&
696 (Op->Asl.Next->Asl.ParseOpcode == PARSEOP_ELSE))
699 * This IF has a corresponding ELSE. The IF block has no exit,
700 * (it contains an unconditional Return)
701 * mark the ELSE block to remember this fact.
703 Op->Asl.Next->Asl.CompileFlags |= NODE_IF_HAS_NO_EXIT;
709 if ((Op->Asl.CompileFlags & NODE_HAS_NO_EXIT) &&
710 (Op->Asl.CompileFlags & NODE_IF_HAS_NO_EXIT))
713 * This ELSE block has no exit and the corresponding IF block
714 * has no exit either. Therefore, the parent node has no exit.
716 Op->Asl.Parent->Asl.CompileFlags |= NODE_HAS_NO_EXIT;
723 if ((Op->Asl.CompileFlags & NODE_HAS_NO_EXIT) &&
726 /* If this node has no exit, then the parent has no exit either */
728 Op->Asl.Parent->Asl.CompileFlags |= NODE_HAS_NO_EXIT;