1 /*******************************************************************************
2  *
3  * Module Name: dbmethod - Debug commands for control methods
4  *
5  ******************************************************************************/
6 
7 /******************************************************************************
8  *
9  * 1. Copyright Notice
10  *
11  * Some or all of this work - Copyright (c) 1999 - 2017, 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 "acpi.h"
153 #include "accommon.h"
154 #include "acdispat.h"
155 #include "acnamesp.h"
156 #include "acdebug.h"
157 #include "acparser.h"
158 #include "acpredef.h"
159 
160 
161 #ifdef ACPI_DEBUGGER
162 
163 #define _COMPONENT          ACPI_CA_DEBUGGER
164         ACPI_MODULE_NAME    ("dbmethod")
165 
166 /* Local prototypes */
167 
168 static ACPI_STATUS
169 AcpiDbWalkForExecute (
170     ACPI_HANDLE             ObjHandle,
171     UINT32                  NestingLevel,
172     void                    *Context,
173     void                    **ReturnValue);
174 
175 
176 /*******************************************************************************
177  *
178  * FUNCTION:    AcpiDbSetMethodBreakpoint
179  *
180  * PARAMETERS:  Location            - AML offset of breakpoint
181  *              WalkState           - Current walk info
182  *              Op                  - Current Op (from parse walk)
183  *
184  * RETURN:      None
185  *
186  * DESCRIPTION: Set a breakpoint in a control method at the specified
187  *              AML offset
188  *
189  ******************************************************************************/
190 
191 void
192 AcpiDbSetMethodBreakpoint (
193     char                    *Location,
194     ACPI_WALK_STATE         *WalkState,
195     ACPI_PARSE_OBJECT       *Op)
196 {
197     UINT32                  Address;
198     UINT32                  AmlOffset;
199 
200 
201     if (!Op)
202     {
203         AcpiOsPrintf ("There is no method currently executing\n");
204         return;
205     }
206 
207     /* Get and verify the breakpoint address */
208 
209     Address = strtoul (Location, NULL, 16);
210     AmlOffset = (UINT32) ACPI_PTR_DIFF (Op->Common.Aml,
211         WalkState->ParserState.AmlStart);
212     if (Address <= AmlOffset)
213     {
214         AcpiOsPrintf ("Breakpoint %X is beyond current address %X\n",
215             Address, AmlOffset);
216     }
217 
218     /* Save breakpoint in current walk */
219 
220     WalkState->UserBreakpoint = Address;
221     AcpiOsPrintf ("Breakpoint set at AML offset %X\n", Address);
222 }
223 
224 
225 /*******************************************************************************
226  *
227  * FUNCTION:    AcpiDbSetMethodCallBreakpoint
228  *
229  * PARAMETERS:  Op                  - Current Op (from parse walk)
230  *
231  * RETURN:      None
232  *
233  * DESCRIPTION: Set a breakpoint in a control method at the specified
234  *              AML offset
235  *
236  ******************************************************************************/
237 
238 void
239 AcpiDbSetMethodCallBreakpoint (
240     ACPI_PARSE_OBJECT       *Op)
241 {
242 
243 
244     if (!Op)
245     {
246         AcpiOsPrintf ("There is no method currently executing\n");
247         return;
248     }
249 
250     AcpiGbl_StepToNextCall = TRUE;
251 }
252 
253 
254 /*******************************************************************************
255  *
256  * FUNCTION:    AcpiDbSetMethodData
257  *
258  * PARAMETERS:  TypeArg         - L for local, A for argument
259  *              IndexArg        - which one
260  *              ValueArg        - Value to set.
261  *
262  * RETURN:      None
263  *
264  * DESCRIPTION: Set a local or argument for the running control method.
265  *              NOTE: only object supported is Number.
266  *
267  ******************************************************************************/
268 
269 void
270 AcpiDbSetMethodData (
271     char                    *TypeArg,
272     char                    *IndexArg,
273     char                    *ValueArg)
274 {
275     char                    Type;
276     UINT32                  Index;
277     UINT32                  Value;
278     ACPI_WALK_STATE         *WalkState;
279     ACPI_OPERAND_OBJECT     *ObjDesc;
280     ACPI_STATUS             Status;
281     ACPI_NAMESPACE_NODE     *Node;
282 
283 
284     /* Validate TypeArg */
285 
286     AcpiUtStrupr (TypeArg);
287     Type = TypeArg[0];
288     if ((Type != 'L') &&
289         (Type != 'A') &&
290         (Type != 'N'))
291     {
292         AcpiOsPrintf ("Invalid SET operand: %s\n", TypeArg);
293         return;
294     }
295 
296     Value = strtoul (ValueArg, NULL, 16);
297 
298     if (Type == 'N')
299     {
300         Node = AcpiDbConvertToNode (IndexArg);
301         if (!Node)
302         {
303             return;
304         }
305 
306         if (Node->Type != ACPI_TYPE_INTEGER)
307         {
308             AcpiOsPrintf ("Can only set Integer nodes\n");
309             return;
310         }
311         ObjDesc = Node->Object;
312         ObjDesc->Integer.Value = Value;
313         return;
314     }
315 
316     /* Get the index and value */
317 
318     Index = strtoul (IndexArg, NULL, 16);
319 
320     WalkState = AcpiDsGetCurrentWalkState (AcpiGbl_CurrentWalkList);
321     if (!WalkState)
322     {
323         AcpiOsPrintf ("There is no method currently executing\n");
324         return;
325     }
326 
327     /* Create and initialize the new object */
328 
329     ObjDesc = AcpiUtCreateIntegerObject ((UINT64) Value);
330     if (!ObjDesc)
331     {
332         AcpiOsPrintf ("Could not create an internal object\n");
333         return;
334     }
335 
336     /* Store the new object into the target */
337 
338     switch (Type)
339     {
340     case 'A':
341 
342         /* Set a method argument */
343 
344         if (Index > ACPI_METHOD_MAX_ARG)
345         {
346             AcpiOsPrintf ("Arg%u - Invalid argument name\n",
347                 Index);
348             goto Cleanup;
349         }
350 
351         Status = AcpiDsStoreObjectToLocal (ACPI_REFCLASS_ARG,
352             Index, ObjDesc, WalkState);
353         if (ACPI_FAILURE (Status))
354         {
355             goto Cleanup;
356         }
357 
358         ObjDesc = WalkState->Arguments[Index].Object;
359 
360         AcpiOsPrintf ("Arg%u: ", Index);
361         AcpiDbDisplayInternalObject (ObjDesc, WalkState);
362         break;
363 
364     case 'L':
365 
366         /* Set a method local */
367 
368         if (Index > ACPI_METHOD_MAX_LOCAL)
369         {
370             AcpiOsPrintf ("Local%u - Invalid local variable name\n",
371                 Index);
372             goto Cleanup;
373         }
374 
375         Status = AcpiDsStoreObjectToLocal (ACPI_REFCLASS_LOCAL,
376             Index, ObjDesc, WalkState);
377         if (ACPI_FAILURE (Status))
378         {
379             goto Cleanup;
380         }
381 
382         ObjDesc = WalkState->LocalVariables[Index].Object;
383 
384         AcpiOsPrintf ("Local%u: ", Index);
385         AcpiDbDisplayInternalObject (ObjDesc, WalkState);
386         break;
387 
388     default:
389 
390         break;
391     }
392 
393 Cleanup:
394     AcpiUtRemoveReference (ObjDesc);
395 }
396 
397 
398 /*******************************************************************************
399  *
400  * FUNCTION:    AcpiDbDisassembleAml
401  *
402  * PARAMETERS:  Statements          - Number of statements to disassemble
403  *              Op                  - Current Op (from parse walk)
404  *
405  * RETURN:      None
406  *
407  * DESCRIPTION: Display disassembled AML (ASL) starting from Op for the number
408  *              of statements specified.
409  *
410  ******************************************************************************/
411 
412 void
413 AcpiDbDisassembleAml (
414     char                    *Statements,
415     ACPI_PARSE_OBJECT       *Op)
416 {
417     UINT32                  NumStatements = 8;
418 
419 
420     if (!Op)
421     {
422         AcpiOsPrintf ("There is no method currently executing\n");
423         return;
424     }
425 
426     if (Statements)
427     {
428         NumStatements = strtoul (Statements, NULL, 0);
429     }
430 
431 #ifdef ACPI_DISASSEMBLER
432     AcpiDmDisassemble (NULL, Op, NumStatements);
433 #endif
434 }
435 
436 
437 /*******************************************************************************
438  *
439  * FUNCTION:    AcpiDbDisassembleMethod
440  *
441  * PARAMETERS:  Name            - Name of control method
442  *
443  * RETURN:      None
444  *
445  * DESCRIPTION: Display disassembled AML (ASL) starting from Op for the number
446  *              of statements specified.
447  *
448  ******************************************************************************/
449 
450 ACPI_STATUS
451 AcpiDbDisassembleMethod (
452     char                    *Name)
453 {
454     ACPI_STATUS             Status;
455     ACPI_PARSE_OBJECT       *Op;
456     ACPI_WALK_STATE         *WalkState;
457     ACPI_OPERAND_OBJECT     *ObjDesc;
458     ACPI_NAMESPACE_NODE     *Method;
459 
460 
461     Method = AcpiDbConvertToNode (Name);
462     if (!Method)
463     {
464         return (AE_BAD_PARAMETER);
465     }
466 
467     if (Method->Type != ACPI_TYPE_METHOD)
468     {
469         ACPI_ERROR ((AE_INFO, "%s (%s): Object must be a control method",
470             Name, AcpiUtGetTypeName (Method->Type)));
471         return (AE_BAD_PARAMETER);
472     }
473 
474     ObjDesc = Method->Object;
475 
476     Op = AcpiPsCreateScopeOp (ObjDesc->Method.AmlStart);
477     if (!Op)
478     {
479         return (AE_NO_MEMORY);
480     }
481 
482     /* Create and initialize a new walk state */
483 
484     WalkState = AcpiDsCreateWalkState (0, Op, NULL, NULL);
485     if (!WalkState)
486     {
487         return (AE_NO_MEMORY);
488     }
489 
490     Status = AcpiDsInitAmlWalk (WalkState, Op, NULL,
491         ObjDesc->Method.AmlStart,
492         ObjDesc->Method.AmlLength, NULL, ACPI_IMODE_LOAD_PASS1);
493     if (ACPI_FAILURE (Status))
494     {
495         return (Status);
496     }
497 
498     Status = AcpiUtAllocateOwnerId (&ObjDesc->Method.OwnerId);
499     WalkState->OwnerId = ObjDesc->Method.OwnerId;
500 
501     /* Push start scope on scope stack and make it current */
502 
503     Status = AcpiDsScopeStackPush (Method,
504         Method->Type, WalkState);
505     if (ACPI_FAILURE (Status))
506     {
507         return (Status);
508     }
509 
510     /* Parse the entire method AML including deferred operators */
511 
512     WalkState->ParseFlags &= ~ACPI_PARSE_DELETE_TREE;
513     WalkState->ParseFlags |= ACPI_PARSE_DISASSEMBLE;
514 
515     Status = AcpiPsParseAml (WalkState);
516 
517 #ifdef ACPI_DISASSEMBLER
518     (void) AcpiDmParseDeferredOps (Op);
519 
520     /* Now we can disassemble the method */
521 
522     AcpiGbl_DmOpt_Verbose = FALSE;
523     AcpiDmDisassemble (NULL, Op, 0);
524     AcpiGbl_DmOpt_Verbose = TRUE;
525 #endif
526 
527     AcpiPsDeleteParseTree (Op);
528 
529     /* Method cleanup */
530 
531     AcpiNsDeleteNamespaceSubtree (Method);
532     AcpiNsDeleteNamespaceByOwner (ObjDesc->Method.OwnerId);
533     AcpiUtReleaseOwnerId (&ObjDesc->Method.OwnerId);
534     return (AE_OK);
535 }
536 
537 
538 /*******************************************************************************
539  *
540  * FUNCTION:    AcpiDbWalkForExecute
541  *
542  * PARAMETERS:  Callback from WalkNamespace
543  *
544  * RETURN:      Status
545  *
546  * DESCRIPTION: Batch execution module. Currently only executes predefined
547  *              ACPI names.
548  *
549  ******************************************************************************/
550 
551 static ACPI_STATUS
552 AcpiDbWalkForExecute (
553     ACPI_HANDLE             ObjHandle,
554     UINT32                  NestingLevel,
555     void                    *Context,
556     void                    **ReturnValue)
557 {
558     ACPI_NAMESPACE_NODE     *Node = (ACPI_NAMESPACE_NODE *) ObjHandle;
559     ACPI_DB_EXECUTE_WALK    *Info = (ACPI_DB_EXECUTE_WALK *) Context;
560     ACPI_BUFFER             ReturnObj;
561     ACPI_STATUS             Status;
562     char                    *Pathname;
563     UINT32                  i;
564     ACPI_DEVICE_INFO        *ObjInfo;
565     ACPI_OBJECT_LIST        ParamObjects;
566     ACPI_OBJECT             Params[ACPI_METHOD_NUM_ARGS];
567     const ACPI_PREDEFINED_INFO *Predefined;
568 
569 
570     Predefined = AcpiUtMatchPredefinedMethod (Node->Name.Ascii);
571     if (!Predefined)
572     {
573         return (AE_OK);
574     }
575 
576     if (Node->Type == ACPI_TYPE_LOCAL_SCOPE)
577     {
578         return (AE_OK);
579     }
580 
581     Pathname = AcpiNsGetExternalPathname (Node);
582     if (!Pathname)
583     {
584         return (AE_OK);
585     }
586 
587     /* Get the object info for number of method parameters */
588 
589     Status = AcpiGetObjectInfo (ObjHandle, &ObjInfo);
590     if (ACPI_FAILURE (Status))
591     {
592         ACPI_FREE (Pathname);
593         return (Status);
594     }
595 
596     ParamObjects.Pointer = NULL;
597     ParamObjects.Count   = 0;
598 
599     if (ObjInfo->Type == ACPI_TYPE_METHOD)
600     {
601         /* Setup default parameters */
602 
603         for (i = 0; i < ObjInfo->ParamCount; i++)
604         {
605             Params[i].Type           = ACPI_TYPE_INTEGER;
606             Params[i].Integer.Value  = 1;
607         }
608 
609         ParamObjects.Pointer     = Params;
610         ParamObjects.Count       = ObjInfo->ParamCount;
611     }
612 
613     ACPI_FREE (ObjInfo);
614     ReturnObj.Pointer = NULL;
615     ReturnObj.Length = ACPI_ALLOCATE_BUFFER;
616 
617     /* Do the actual method execution */
618 
619     AcpiGbl_MethodExecuting = TRUE;
620 
621     Status = AcpiEvaluateObject (Node, NULL, &ParamObjects, &ReturnObj);
622 
623     AcpiOsPrintf ("%-32s returned %s\n", Pathname, AcpiFormatException (Status));
624     AcpiGbl_MethodExecuting = FALSE;
625     ACPI_FREE (Pathname);
626 
627     /* Ignore status from method execution */
628 
629     Status = AE_OK;
630 
631     /* Update count, check if we have executed enough methods */
632 
633     Info->Count++;
634     if (Info->Count >= Info->MaxCount)
635     {
636         Status = AE_CTRL_TERMINATE;
637     }
638 
639     return (Status);
640 }
641 
642 
643 /*******************************************************************************
644  *
645  * FUNCTION:    AcpiDbEvaluatePredefinedNames
646  *
647  * PARAMETERS:  None
648  *
649  * RETURN:      None
650  *
651  * DESCRIPTION: Namespace batch execution. Execute predefined names in the
652  *              namespace, up to the max count, if specified.
653  *
654  ******************************************************************************/
655 
656 void
657 AcpiDbEvaluatePredefinedNames (
658     void)
659 {
660     ACPI_DB_EXECUTE_WALK    Info;
661 
662 
663     Info.Count = 0;
664     Info.MaxCount = ACPI_UINT32_MAX;
665 
666     /* Search all nodes in namespace */
667 
668     (void) AcpiWalkNamespace (ACPI_TYPE_ANY, ACPI_ROOT_OBJECT, ACPI_UINT32_MAX,
669                 AcpiDbWalkForExecute, NULL, (void *) &Info, NULL);
670 
671     AcpiOsPrintf ("Evaluated %u predefined names in the namespace\n", Info.Count);
672 }
673 
674 #endif /* ACPI_DEBUGGER */
675