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 - 2020, 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 #ifdef ACPI_DISASSEMBLER
399 /*******************************************************************************
400  *
401  * FUNCTION:    AcpiDbDisassembleAml
402  *
403  * PARAMETERS:  Statements          - Number of statements to disassemble
404  *              Op                  - Current Op (from parse walk)
405  *
406  * RETURN:      None
407  *
408  * DESCRIPTION: Display disassembled AML (ASL) starting from Op for the number
409  *              of statements specified.
410  *
411  ******************************************************************************/
412 
413 void
414 AcpiDbDisassembleAml (
415     char                    *Statements,
416     ACPI_PARSE_OBJECT       *Op)
417 {
418     UINT32                  NumStatements = 8;
419 
420 
421     if (!Op)
422     {
423         AcpiOsPrintf ("There is no method currently executing\n");
424         return;
425     }
426 
427     if (Statements)
428     {
429         NumStatements = strtoul (Statements, NULL, 0);
430     }
431 
432     AcpiDmDisassemble (NULL, Op, NumStatements);
433 }
434 
435 
436 /*******************************************************************************
437  *
438  * FUNCTION:    AcpiDbDisassembleMethod
439  *
440  * PARAMETERS:  Name            - Name of control method
441  *
442  * RETURN:      None
443  *
444  * DESCRIPTION: Display disassembled AML (ASL) starting from Op for the number
445  *              of statements specified.
446  *
447  ******************************************************************************/
448 
449 ACPI_STATUS
450 AcpiDbDisassembleMethod (
451     char                    *Name)
452 {
453     ACPI_STATUS             Status;
454     ACPI_PARSE_OBJECT       *Op;
455     ACPI_WALK_STATE         *WalkState;
456     ACPI_OPERAND_OBJECT     *ObjDesc;
457     ACPI_NAMESPACE_NODE     *Method;
458 
459 
460     Method = AcpiDbConvertToNode (Name);
461     if (!Method)
462     {
463         return (AE_BAD_PARAMETER);
464     }
465 
466     if (Method->Type != ACPI_TYPE_METHOD)
467     {
468         ACPI_ERROR ((AE_INFO, "%s (%s): Object must be a control method",
469             Name, AcpiUtGetTypeName (Method->Type)));
470         return (AE_BAD_PARAMETER);
471     }
472 
473     ObjDesc = Method->Object;
474 
475     Op = AcpiPsCreateScopeOp (ObjDesc->Method.AmlStart);
476     if (!Op)
477     {
478         return (AE_NO_MEMORY);
479     }
480 
481     /* Create and initialize a new walk state */
482 
483     WalkState = AcpiDsCreateWalkState (0, Op, NULL, NULL);
484     if (!WalkState)
485     {
486         return (AE_NO_MEMORY);
487     }
488 
489     Status = AcpiDsInitAmlWalk (WalkState, Op, NULL,
490         ObjDesc->Method.AmlStart,
491         ObjDesc->Method.AmlLength, NULL, ACPI_IMODE_LOAD_PASS1);
492     if (ACPI_FAILURE (Status))
493     {
494         return (Status);
495     }
496 
497     Status = AcpiUtAllocateOwnerId (&ObjDesc->Method.OwnerId);
498     if (ACPI_FAILURE(Status))
499     {
500         return (Status);
501     }
502 
503     WalkState->OwnerId = ObjDesc->Method.OwnerId;
504 
505     /* Push start scope on scope stack and make it current */
506 
507     Status = AcpiDsScopeStackPush (Method,
508         Method->Type, WalkState);
509     if (ACPI_FAILURE (Status))
510     {
511         return (Status);
512     }
513 
514     /* Parse the entire method AML including deferred operators */
515 
516     WalkState->ParseFlags &= ~ACPI_PARSE_DELETE_TREE;
517     WalkState->ParseFlags |= ACPI_PARSE_DISASSEMBLE;
518 
519     Status = AcpiPsParseAml (WalkState);
520     if (ACPI_FAILURE(Status))
521     {
522         return (Status);
523     }
524 
525     (void) AcpiDmParseDeferredOps (Op);
526 
527     /* Now we can disassemble the method */
528 
529     AcpiGbl_DmOpt_Verbose = FALSE;
530     AcpiDmDisassemble (NULL, Op, 0);
531     AcpiGbl_DmOpt_Verbose = TRUE;
532 
533     AcpiPsDeleteParseTree (Op);
534 
535     /* Method cleanup */
536 
537     AcpiNsDeleteNamespaceSubtree (Method);
538     AcpiNsDeleteNamespaceByOwner (ObjDesc->Method.OwnerId);
539     AcpiUtReleaseOwnerId (&ObjDesc->Method.OwnerId);
540     return (AE_OK);
541 }
542 #endif
543 
544 
545 /*******************************************************************************
546  *
547  * FUNCTION:    AcpiDbWalkForExecute
548  *
549  * PARAMETERS:  Callback from WalkNamespace
550  *
551  * RETURN:      Status
552  *
553  * DESCRIPTION: Batch execution module. Currently only executes predefined
554  *              ACPI names.
555  *
556  ******************************************************************************/
557 
558 static ACPI_STATUS
559 AcpiDbWalkForExecute (
560     ACPI_HANDLE             ObjHandle,
561     UINT32                  NestingLevel,
562     void                    *Context,
563     void                    **ReturnValue)
564 {
565     ACPI_NAMESPACE_NODE     *Node = (ACPI_NAMESPACE_NODE *) ObjHandle;
566     ACPI_DB_EXECUTE_WALK    *Info = (ACPI_DB_EXECUTE_WALK *) Context;
567     ACPI_BUFFER             ReturnObj;
568     ACPI_STATUS             Status;
569     char                    *Pathname;
570     UINT32                  i;
571     ACPI_DEVICE_INFO        *ObjInfo;
572     ACPI_OBJECT_LIST        ParamObjects;
573     ACPI_OBJECT             Params[ACPI_METHOD_NUM_ARGS];
574     const ACPI_PREDEFINED_INFO *Predefined;
575 
576 
577     Predefined = AcpiUtMatchPredefinedMethod (Node->Name.Ascii);
578     if (!Predefined)
579     {
580         return (AE_OK);
581     }
582 
583     if (Node->Type == ACPI_TYPE_LOCAL_SCOPE)
584     {
585         return (AE_OK);
586     }
587 
588     Pathname = AcpiNsGetExternalPathname (Node);
589     if (!Pathname)
590     {
591         return (AE_OK);
592     }
593 
594     /* Get the object info for number of method parameters */
595 
596     Status = AcpiGetObjectInfo (ObjHandle, &ObjInfo);
597     if (ACPI_FAILURE (Status))
598     {
599         ACPI_FREE (Pathname);
600         return (Status);
601     }
602 
603     ParamObjects.Pointer = NULL;
604     ParamObjects.Count   = 0;
605 
606     if (ObjInfo->Type == ACPI_TYPE_METHOD)
607     {
608         /* Setup default parameters */
609 
610         for (i = 0; i < ObjInfo->ParamCount; i++)
611         {
612             Params[i].Type           = ACPI_TYPE_INTEGER;
613             Params[i].Integer.Value  = 1;
614         }
615 
616         ParamObjects.Pointer     = Params;
617         ParamObjects.Count       = ObjInfo->ParamCount;
618     }
619 
620     ACPI_FREE (ObjInfo);
621     ReturnObj.Pointer = NULL;
622     ReturnObj.Length = ACPI_ALLOCATE_BUFFER;
623 
624     /* Do the actual method execution */
625 
626     AcpiGbl_MethodExecuting = TRUE;
627 
628     Status = AcpiEvaluateObject (Node, NULL, &ParamObjects, &ReturnObj);
629 
630     AcpiOsPrintf ("%-32s returned %s\n", Pathname, AcpiFormatException (Status));
631     AcpiGbl_MethodExecuting = FALSE;
632     ACPI_FREE (Pathname);
633 
634     /* Ignore status from method execution */
635 
636     Status = AE_OK;
637 
638     /* Update count, check if we have executed enough methods */
639 
640     Info->Count++;
641     if (Info->Count >= Info->MaxCount)
642     {
643         Status = AE_CTRL_TERMINATE;
644     }
645 
646     return (Status);
647 }
648 
649 
650 /*******************************************************************************
651  *
652  * FUNCTION:    AcpiDbEvaluatePredefinedNames
653  *
654  * PARAMETERS:  None
655  *
656  * RETURN:      None
657  *
658  * DESCRIPTION: Namespace batch execution. Execute predefined names in the
659  *              namespace, up to the max count, if specified.
660  *
661  ******************************************************************************/
662 
663 void
664 AcpiDbEvaluatePredefinedNames (
665     void)
666 {
667     ACPI_DB_EXECUTE_WALK    Info;
668 
669 
670     Info.Count = 0;
671     Info.MaxCount = ACPI_UINT32_MAX;
672 
673     /* Search all nodes in namespace */
674 
675     (void) AcpiWalkNamespace (ACPI_TYPE_ANY, ACPI_ROOT_OBJECT, ACPI_UINT32_MAX,
676                 AcpiDbWalkForExecute, NULL, (void *) &Info, NULL);
677 
678     AcpiOsPrintf ("Evaluated %u predefined names in the namespace\n", Info.Count);
679 }
680 
681 #endif /* ACPI_DEBUGGER */
682