1 /******************************************************************************
3 * Module Name: aslmethod.c - Control method analysis walk
5 *****************************************************************************/
8 * Copyright (C) 2000 - 2014, 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 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] |= AnMapObjTypeToBtype (NextParamType);
190 NextParamType->Asl.ParseOpcode = PARSEOP_DEFAULT_ARG;
191 NextParamType = NextParamType->Asl.Next;
196 MethodInfo->ValidArgTypes[ActualArgs] =
197 AnMapObjTypeToBtype (NextType);
198 NextType->Asl.ParseOpcode = PARSEOP_DEFAULT_ARG;
202 NextType = NextType->Asl.Next;
205 if ((MethodInfo->NumArguments) &&
206 (MethodInfo->NumArguments != ActualArgs))
208 /* error: Param list did not match number of args */
211 /* Allow numarguments == 0 for Function() */
213 if ((!MethodInfo->NumArguments) && (ActualArgs))
215 MethodInfo->NumArguments = ActualArgs;
216 ArgNode->Asl.Value.Integer |= ActualArgs;
220 * Actual arguments are initialized at method entry.
221 * All other ArgX "registers" can be used as locals, so we
222 * track their initialization.
224 for (i = 0; i < MethodInfo->NumArguments; i++)
226 MethodInfo->ArgInitialized[i] = TRUE;
230 case PARSEOP_METHODCALL:
233 (Op->Asl.Node == MethodInfo->Op->Asl.Node))
235 AslError (ASL_REMARK, ASL_MSG_RECURSION, Op, Op->Asl.ExternalName);
251 * Local was used outside a control method, or there was an error
252 * in the method declaration.
254 AslError (ASL_REMARK, ASL_MSG_LOCAL_OUTSIDE_METHOD, Op, Op->Asl.ExternalName);
258 RegisterNumber = (Op->Asl.AmlOpcode & 0x000F);
261 * If the local is being used as a target, mark the local
264 if (Op->Asl.CompileFlags & NODE_IS_TARGET)
266 MethodInfo->LocalInitialized[RegisterNumber] = TRUE;
270 * Otherwise, this is a reference, check if the local
271 * has been previously initialized.
273 * The only operator that accepts an uninitialized value is ObjectType()
275 else if ((!MethodInfo->LocalInitialized[RegisterNumber]) &&
276 (Op->Asl.Parent->Asl.ParseOpcode != PARSEOP_OBJECTTYPE))
278 LocalName[strlen (LocalName) -1] = (char) (RegisterNumber + 0x30);
279 AslError (ASL_ERROR, ASL_MSG_LOCAL_INIT, Op, LocalName);
294 * Arg was used outside a control method, or there was an error
295 * in the method declaration.
297 AslError (ASL_REMARK, ASL_MSG_LOCAL_OUTSIDE_METHOD, Op, Op->Asl.ExternalName);
301 RegisterNumber = (Op->Asl.AmlOpcode & 0x000F) - 8;
302 ArgName[strlen (ArgName) -1] = (char) (RegisterNumber + 0x30);
305 * If the Arg is being used as a target, mark the local
308 if (Op->Asl.CompileFlags & NODE_IS_TARGET)
310 MethodInfo->ArgInitialized[RegisterNumber] = TRUE;
314 * Otherwise, this is a reference, check if the Arg
315 * has been previously initialized.
317 * The only operator that accepts an uninitialized value is ObjectType()
319 else if ((!MethodInfo->ArgInitialized[RegisterNumber]) &&
320 (Op->Asl.Parent->Asl.ParseOpcode != PARSEOP_OBJECTTYPE))
322 AslError (ASL_ERROR, ASL_MSG_ARG_INIT, Op, ArgName);
325 /* Flag this arg if it is not a "real" argument to the method */
327 if (RegisterNumber >= MethodInfo->NumArguments)
329 AslError (ASL_REMARK, ASL_MSG_NOT_PARAMETER, Op, ArgName);
338 * Probably was an error in the method declaration,
339 * no additional error here
341 ACPI_WARNING ((AE_INFO, "%p, No parent method", Op));
346 * A child indicates a possible return value. A simple Return or
347 * Return() is marked with NODE_IS_NULL_RETURN by the parser so
348 * that it is not counted as a "real" return-with-value, although
349 * the AML code that is actually emitted is Return(0). The AML
350 * definition of Return has a required parameter, so we are
351 * forced to convert a null return to Return(0).
353 if ((Op->Asl.Child) &&
354 (Op->Asl.Child->Asl.ParseOpcode != PARSEOP_DEFAULT_ARG) &&
355 (!(Op->Asl.Child->Asl.CompileFlags & NODE_IS_NULL_RETURN)))
357 MethodInfo->NumReturnWithValue++;
361 MethodInfo->NumReturnNoValue++;
366 case PARSEOP_CONTINUE:
368 Next = Op->Asl.Parent;
371 if (Next->Asl.ParseOpcode == PARSEOP_WHILE)
375 Next = Next->Asl.Parent;
380 AslError (ASL_ERROR, ASL_MSG_NO_WHILE, Op, NULL);
386 /* We can range check if the argument is an integer */
388 if ((Op->Asl.Child->Asl.ParseOpcode == PARSEOP_INTEGER) &&
389 (Op->Asl.Child->Asl.Value.Integer > ACPI_UINT8_MAX))
391 AslError (ASL_ERROR, ASL_MSG_INVALID_TIME, Op, NULL);
397 Next = Op->Asl.Child;
399 if (!ApFindNameInScope (METHOD_NAME__HID, Next) &&
400 !ApFindNameInScope (METHOD_NAME__ADR, Next))
402 AslError (ASL_WARNING, ASL_MSG_MISSING_DEPENDENCY, Op,
403 "Device object requires a _HID or _ADR in same scope");
409 case PARSEOP_OPERATIONREGION:
410 case PARSEOP_POWERRESOURCE:
411 case PARSEOP_PROCESSOR:
412 case PARSEOP_THERMALZONE:
415 * The first operand is a name to be created in the namespace.
416 * Check against the reserved list.
418 i = ApCheckForPredefinedName (Op, Op->Asl.NameSeg);
419 if (i < ACPI_VALID_RESERVED_NAME_MAX)
421 AslError (ASL_ERROR, ASL_MSG_RESERVED_USE, Op, Op->Asl.ExternalName);
427 /* Typecheck any predefined names statically defined with Name() */
429 ApCheckForPredefinedObject (Op, Op->Asl.NameSeg);
431 /* Special typechecking for _HID */
433 if (!ACPI_STRCMP (METHOD_NAME__HID, Op->Asl.NameSeg))
435 Next = Op->Asl.Child->Asl.Next;
436 AnCheckId (Next, ASL_TYPE_HID);
439 /* Special typechecking for _CID */
441 else if (!ACPI_STRCMP (METHOD_NAME__CID, Op->Asl.NameSeg))
443 Next = Op->Asl.Child->Asl.Next;
445 if ((Next->Asl.ParseOpcode == PARSEOP_PACKAGE) ||
446 (Next->Asl.ParseOpcode == PARSEOP_VAR_PACKAGE))
448 Next = Next->Asl.Child;
451 AnCheckId (Next, ASL_TYPE_CID);
452 Next = Next->Asl.Next;
457 AnCheckId (Next, ASL_TYPE_CID);
468 /* Check for named object creation within a non-serialized method */
470 MtCheckNamedObjectInMethod (Op, MethodInfo);
475 /*******************************************************************************
477 * FUNCTION: MtCheckNamedObjectInMethod
479 * PARAMETERS: Op - Current parser op
480 * MethodInfo - Info for method being parsed
484 * DESCRIPTION: Detect if a non-serialized method is creating a named object,
485 * which could possibly cause problems if two threads execute
486 * the method concurrently. Emit a remark in this case.
488 ******************************************************************************/
491 MtCheckNamedObjectInMethod (
492 ACPI_PARSE_OBJECT *Op,
493 ASL_METHOD_INFO *MethodInfo)
495 const ACPI_OPCODE_INFO *OpInfo;
498 /* We don't care about actual method declarations */
500 if (Op->Asl.AmlOpcode == AML_METHOD_OP)
505 /* Determine if we are creating a named object */
507 OpInfo = AcpiPsGetOpcodeInfo (Op->Asl.AmlOpcode);
508 if (OpInfo->Class == AML_CLASS_NAMED_OBJECT)
511 * If we have a named object created within a non-serialized method,
512 * emit a remark that the method should be serialized.
514 * Reason: If a thread blocks within the method for any reason, and
515 * another thread enters the method, the method will fail because an
516 * attempt will be made to create the same object twice.
518 if (MethodInfo && !MethodInfo->ShouldBeSerialized)
520 AslError (ASL_REMARK, ASL_MSG_SERIALIZED_REQUIRED, MethodInfo->Op,
521 "due to creation of named objects within");
523 /* Emit message only ONCE per method */
525 MethodInfo->ShouldBeSerialized = TRUE;
531 /*******************************************************************************
533 * FUNCTION: MtMethodAnalysisWalkEnd
535 * PARAMETERS: ASL_WALK_CALLBACK
539 * DESCRIPTION: Ascending callback for analysis walk. Complete method
542 ******************************************************************************/
545 MtMethodAnalysisWalkEnd (
546 ACPI_PARSE_OBJECT *Op,
550 ASL_ANALYSIS_WALK_INFO *WalkInfo = (ASL_ANALYSIS_WALK_INFO *) Context;
551 ASL_METHOD_INFO *MethodInfo = WalkInfo->MethodStack;
554 switch (Op->Asl.ParseOpcode)
561 printf ("No method info for method! [%s]\n", Op->Asl.Namepath);
562 AslError (ASL_ERROR, ASL_MSG_COMPILER_INTERNAL, Op,
563 "No method info for this method");
566 return (AE_AML_INTERNAL);
575 switch (Op->Asl.ParseOpcode)
579 WalkInfo->MethodStack = MethodInfo->Next;
582 * Check if there is no return statement at the end of the
583 * method AND we can actually get there -- i.e., the execution
584 * of the method can possibly terminate without a return statement.
586 if ((!AnLastStatementIsReturn (Op)) &&
587 (!(Op->Asl.CompileFlags & NODE_HAS_NO_EXIT)))
590 * No return statement, and execution can possibly exit
591 * via this path. This is equivalent to Return ()
593 MethodInfo->NumReturnNoValue++;
597 * Check for case where some return statements have a return value
598 * and some do not. Exit without a return statement is a return with
601 if (MethodInfo->NumReturnNoValue &&
602 MethodInfo->NumReturnWithValue)
604 AslError (ASL_WARNING, ASL_MSG_RETURN_TYPES, Op,
605 Op->Asl.ExternalName);
609 * If there are any RETURN() statements with no value, or there is a
610 * control path that allows the method to exit without a return value,
611 * we mark the method as a method that does not return a value. This
612 * knowledge can be used to check method invocations that expect a
615 if (MethodInfo->NumReturnNoValue)
617 if (MethodInfo->NumReturnWithValue)
619 Op->Asl.CompileFlags |= NODE_METHOD_SOME_NO_RETVAL;
623 Op->Asl.CompileFlags |= NODE_METHOD_NO_RETVAL;
628 * Check predefined method names for correct return behavior
629 * and correct number of arguments. Also, some special checks
630 * For GPE and _REG methods.
632 if (ApCheckForPredefinedMethod (Op, MethodInfo))
634 /* Special check for two names like _L01 and _E01 in same scope */
636 ApCheckForGpeNameConflict (Op);
639 * Special check for _REG: Must have an operation region definition
640 * within the same scope!
642 ApCheckRegMethod (Op);
645 ACPI_FREE (MethodInfo);
650 /* Special check for two names like _L01 and _E01 in same scope */
652 ApCheckForGpeNameConflict (Op);
658 * If the parent is a predefined method name, attempt to typecheck
659 * the return value. Only static types can be validated.
661 ApCheckPredefinedReturnValue (Op, MethodInfo);
664 * The parent block does not "exit" and continue execution -- the
665 * method is terminated here with the Return() statement.
667 Op->Asl.Parent->Asl.CompileFlags |= NODE_HAS_NO_EXIT;
669 /* Used in the "typing" pass later */
671 Op->Asl.ParentMethod = MethodInfo->Op;
674 * If there is a peer node after the return statement, then this
675 * node is unreachable code -- i.e., it won't be executed because of
676 * the preceding Return() statement.
680 AslError (ASL_WARNING, ASL_MSG_UNREACHABLE_CODE, Op->Asl.Next, NULL);
686 if ((Op->Asl.CompileFlags & NODE_HAS_NO_EXIT) &&
688 (Op->Asl.Next->Asl.ParseOpcode == PARSEOP_ELSE))
691 * This IF has a corresponding ELSE. The IF block has no exit,
692 * (it contains an unconditional Return)
693 * mark the ELSE block to remember this fact.
695 Op->Asl.Next->Asl.CompileFlags |= NODE_IF_HAS_NO_EXIT;
701 if ((Op->Asl.CompileFlags & NODE_HAS_NO_EXIT) &&
702 (Op->Asl.CompileFlags & NODE_IF_HAS_NO_EXIT))
705 * This ELSE block has no exit and the corresponding IF block
706 * has no exit either. Therefore, the parent node has no exit.
708 Op->Asl.Parent->Asl.CompileFlags |= NODE_HAS_NO_EXIT;
715 if ((Op->Asl.CompileFlags & NODE_HAS_NO_EXIT) &&
718 /* If this node has no exit, then the parent has no exit either */
720 Op->Asl.Parent->Asl.CompileFlags |= NODE_HAS_NO_EXIT;