xref: /netbsd-src/sys/external/bsd/acpica/dist/debugger/dbmethod.c (revision 154bfe8e089c1a0a4e9ed8414f08d3da90949162)
1 /*******************************************************************************
2  *
3  * Module Name: dbmethod - Debug commands for control methods
4  *
5  ******************************************************************************/
6 
7 /*
8  * Copyright (C) 2000 - 2020, Intel Corp.
9  * All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
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.
25  *
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.
29  *
30  * NO WARRANTY
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.
42  */
43 
44 #include "acpi.h"
45 #include "accommon.h"
46 #include "acdispat.h"
47 #include "acnamesp.h"
48 #include "acdebug.h"
49 #include "acparser.h"
50 #include "acpredef.h"
51 
52 
53 #define _COMPONENT          ACPI_CA_DEBUGGER
54         ACPI_MODULE_NAME    ("dbmethod")
55 
56 /* Local prototypes */
57 
58 static ACPI_STATUS
59 AcpiDbWalkForExecute (
60     ACPI_HANDLE             ObjHandle,
61     UINT32                  NestingLevel,
62     void                    *Context,
63     void                    **ReturnValue);
64 
65 
66 /*******************************************************************************
67  *
68  * FUNCTION:    AcpiDbSetMethodBreakpoint
69  *
70  * PARAMETERS:  Location            - AML offset of breakpoint
71  *              WalkState           - Current walk info
72  *              Op                  - Current Op (from parse walk)
73  *
74  * RETURN:      None
75  *
76  * DESCRIPTION: Set a breakpoint in a control method at the specified
77  *              AML offset
78  *
79  ******************************************************************************/
80 
81 void
82 AcpiDbSetMethodBreakpoint (
83     char                    *Location,
84     ACPI_WALK_STATE         *WalkState,
85     ACPI_PARSE_OBJECT       *Op)
86 {
87     UINT32                  Address;
88     UINT32                  AmlOffset;
89 
90 
91     if (!Op)
92     {
93         AcpiOsPrintf ("There is no method currently executing\n");
94         return;
95     }
96 
97     /* Get and verify the breakpoint address */
98 
99     Address = strtoul (Location, NULL, 16);
100     AmlOffset = (UINT32) ACPI_PTR_DIFF (Op->Common.Aml,
101         WalkState->ParserState.AmlStart);
102     if (Address <= AmlOffset)
103     {
104         AcpiOsPrintf ("Breakpoint %X is beyond current address %X\n",
105             Address, AmlOffset);
106     }
107 
108     /* Save breakpoint in current walk */
109 
110     WalkState->UserBreakpoint = Address;
111     AcpiOsPrintf ("Breakpoint set at AML offset %X\n", Address);
112 }
113 
114 
115 /*******************************************************************************
116  *
117  * FUNCTION:    AcpiDbSetMethodCallBreakpoint
118  *
119  * PARAMETERS:  Op                  - Current Op (from parse walk)
120  *
121  * RETURN:      None
122  *
123  * DESCRIPTION: Set a breakpoint in a control method at the specified
124  *              AML offset
125  *
126  ******************************************************************************/
127 
128 void
129 AcpiDbSetMethodCallBreakpoint (
130     ACPI_PARSE_OBJECT       *Op)
131 {
132 
133 
134     if (!Op)
135     {
136         AcpiOsPrintf ("There is no method currently executing\n");
137         return;
138     }
139 
140     AcpiGbl_StepToNextCall = TRUE;
141 }
142 
143 
144 /*******************************************************************************
145  *
146  * FUNCTION:    AcpiDbSetMethodData
147  *
148  * PARAMETERS:  TypeArg         - L for local, A for argument
149  *              IndexArg        - which one
150  *              ValueArg        - Value to set.
151  *
152  * RETURN:      None
153  *
154  * DESCRIPTION: Set a local or argument for the running control method.
155  *              NOTE: only object supported is Number.
156  *
157  ******************************************************************************/
158 
159 void
160 AcpiDbSetMethodData (
161     char                    *TypeArg,
162     char                    *IndexArg,
163     char                    *ValueArg)
164 {
165     char                    Type;
166     UINT32                  Index;
167     UINT32                  Value;
168     ACPI_WALK_STATE         *WalkState;
169     ACPI_OPERAND_OBJECT     *ObjDesc;
170     ACPI_STATUS             Status;
171     ACPI_NAMESPACE_NODE     *Node;
172 
173 
174     /* Validate TypeArg */
175 
176     AcpiUtStrupr (TypeArg);
177     Type = TypeArg[0];
178     if ((Type != 'L') &&
179         (Type != 'A') &&
180         (Type != 'N'))
181     {
182         AcpiOsPrintf ("Invalid SET operand: %s\n", TypeArg);
183         return;
184     }
185 
186     Value = strtoul (ValueArg, NULL, 16);
187 
188     if (Type == 'N')
189     {
190         Node = AcpiDbConvertToNode (IndexArg);
191         if (!Node)
192         {
193             return;
194         }
195 
196         if (Node->Type != ACPI_TYPE_INTEGER)
197         {
198             AcpiOsPrintf ("Can only set Integer nodes\n");
199             return;
200         }
201         ObjDesc = Node->Object;
202         ObjDesc->Integer.Value = Value;
203         return;
204     }
205 
206     /* Get the index and value */
207 
208     Index = strtoul (IndexArg, NULL, 16);
209 
210     WalkState = AcpiDsGetCurrentWalkState (AcpiGbl_CurrentWalkList);
211     if (!WalkState)
212     {
213         AcpiOsPrintf ("There is no method currently executing\n");
214         return;
215     }
216 
217     /* Create and initialize the new object */
218 
219     ObjDesc = AcpiUtCreateIntegerObject ((UINT64) Value);
220     if (!ObjDesc)
221     {
222         AcpiOsPrintf ("Could not create an internal object\n");
223         return;
224     }
225 
226     /* Store the new object into the target */
227 
228     switch (Type)
229     {
230     case 'A':
231 
232         /* Set a method argument */
233 
234         if (Index > ACPI_METHOD_MAX_ARG)
235         {
236             AcpiOsPrintf ("Arg%u - Invalid argument name\n",
237                 Index);
238             goto Cleanup;
239         }
240 
241         Status = AcpiDsStoreObjectToLocal (ACPI_REFCLASS_ARG,
242             Index, ObjDesc, WalkState);
243         if (ACPI_FAILURE (Status))
244         {
245             goto Cleanup;
246         }
247 
248         ObjDesc = WalkState->Arguments[Index].Object;
249 
250         AcpiOsPrintf ("Arg%u: ", Index);
251         AcpiDbDisplayInternalObject (ObjDesc, WalkState);
252         break;
253 
254     case 'L':
255 
256         /* Set a method local */
257 
258         if (Index > ACPI_METHOD_MAX_LOCAL)
259         {
260             AcpiOsPrintf ("Local%u - Invalid local variable name\n",
261                 Index);
262             goto Cleanup;
263         }
264 
265         Status = AcpiDsStoreObjectToLocal (ACPI_REFCLASS_LOCAL,
266             Index, ObjDesc, WalkState);
267         if (ACPI_FAILURE (Status))
268         {
269             goto Cleanup;
270         }
271 
272         ObjDesc = WalkState->LocalVariables[Index].Object;
273 
274         AcpiOsPrintf ("Local%u: ", Index);
275         AcpiDbDisplayInternalObject (ObjDesc, WalkState);
276         break;
277 
278     default:
279 
280         break;
281     }
282 
283 Cleanup:
284     AcpiUtRemoveReference (ObjDesc);
285 }
286 
287 
288 #ifdef ACPI_DISASSEMBLER
289 /*******************************************************************************
290  *
291  * FUNCTION:    AcpiDbDisassembleAml
292  *
293  * PARAMETERS:  Statements          - Number of statements to disassemble
294  *              Op                  - Current Op (from parse walk)
295  *
296  * RETURN:      None
297  *
298  * DESCRIPTION: Display disassembled AML (ASL) starting from Op for the number
299  *              of statements specified.
300  *
301  ******************************************************************************/
302 
303 void
304 AcpiDbDisassembleAml (
305     char                    *Statements,
306     ACPI_PARSE_OBJECT       *Op)
307 {
308     UINT32                  NumStatements = 8;
309 
310 
311     if (!Op)
312     {
313         AcpiOsPrintf ("There is no method currently executing\n");
314         return;
315     }
316 
317     if (Statements)
318     {
319         NumStatements = strtoul (Statements, NULL, 0);
320     }
321 
322     AcpiDmDisassemble (NULL, Op, NumStatements);
323 }
324 
325 
326 /*******************************************************************************
327  *
328  * FUNCTION:    AcpiDbDisassembleMethod
329  *
330  * PARAMETERS:  Name            - Name of control method
331  *
332  * RETURN:      None
333  *
334  * DESCRIPTION: Display disassembled AML (ASL) starting from Op for the number
335  *              of statements specified.
336  *
337  ******************************************************************************/
338 
339 ACPI_STATUS
340 AcpiDbDisassembleMethod (
341     char                    *Name)
342 {
343     ACPI_STATUS             Status;
344     ACPI_PARSE_OBJECT       *Op;
345     ACPI_WALK_STATE         *WalkState;
346     ACPI_OPERAND_OBJECT     *ObjDesc;
347     ACPI_NAMESPACE_NODE     *Method;
348 
349 
350     Method = AcpiDbConvertToNode (Name);
351     if (!Method)
352     {
353         return (AE_BAD_PARAMETER);
354     }
355 
356     if (Method->Type != ACPI_TYPE_METHOD)
357     {
358         ACPI_ERROR ((AE_INFO, "%s (%s): Object must be a control method",
359             Name, AcpiUtGetTypeName (Method->Type)));
360         return (AE_BAD_PARAMETER);
361     }
362 
363     ObjDesc = Method->Object;
364 
365     Op = AcpiPsCreateScopeOp (ObjDesc->Method.AmlStart);
366     if (!Op)
367     {
368         return (AE_NO_MEMORY);
369     }
370 
371     /* Create and initialize a new walk state */
372 
373     WalkState = AcpiDsCreateWalkState (0, Op, NULL, NULL);
374     if (!WalkState)
375     {
376         return (AE_NO_MEMORY);
377     }
378 
379     Status = AcpiDsInitAmlWalk (WalkState, Op, NULL,
380         ObjDesc->Method.AmlStart,
381         ObjDesc->Method.AmlLength, NULL, ACPI_IMODE_LOAD_PASS1);
382     if (ACPI_FAILURE (Status))
383     {
384         return (Status);
385     }
386 
387     Status = AcpiUtAllocateOwnerId (&ObjDesc->Method.OwnerId);
388     if (ACPI_FAILURE(Status))
389     {
390         return (Status);
391     }
392 
393     WalkState->OwnerId = ObjDesc->Method.OwnerId;
394 
395     /* Push start scope on scope stack and make it current */
396 
397     Status = AcpiDsScopeStackPush (Method,
398         Method->Type, WalkState);
399     if (ACPI_FAILURE (Status))
400     {
401         return (Status);
402     }
403 
404     /* Parse the entire method AML including deferred operators */
405 
406     WalkState->ParseFlags &= ~ACPI_PARSE_DELETE_TREE;
407     WalkState->ParseFlags |= ACPI_PARSE_DISASSEMBLE;
408 
409     Status = AcpiPsParseAml (WalkState);
410     if (ACPI_FAILURE(Status))
411     {
412         return (Status);
413     }
414 
415     (void) AcpiDmParseDeferredOps (Op);
416 
417     /* Now we can disassemble the method */
418 
419     AcpiGbl_DmOpt_Verbose = FALSE;
420     AcpiDmDisassemble (NULL, Op, 0);
421     AcpiGbl_DmOpt_Verbose = TRUE;
422 
423     AcpiPsDeleteParseTree (Op);
424 
425     /* Method cleanup */
426 
427     AcpiNsDeleteNamespaceSubtree (Method);
428     AcpiNsDeleteNamespaceByOwner (ObjDesc->Method.OwnerId);
429     AcpiUtReleaseOwnerId (&ObjDesc->Method.OwnerId);
430     return (AE_OK);
431 }
432 #endif
433 
434 
435 /*******************************************************************************
436  *
437  * FUNCTION:    AcpiDbWalkForExecute
438  *
439  * PARAMETERS:  Callback from WalkNamespace
440  *
441  * RETURN:      Status
442  *
443  * DESCRIPTION: Batch execution module. Currently only executes predefined
444  *              ACPI names.
445  *
446  ******************************************************************************/
447 
448 static ACPI_STATUS
449 AcpiDbWalkForExecute (
450     ACPI_HANDLE             ObjHandle,
451     UINT32                  NestingLevel,
452     void                    *Context,
453     void                    **ReturnValue)
454 {
455     ACPI_NAMESPACE_NODE     *Node = (ACPI_NAMESPACE_NODE *) ObjHandle;
456     ACPI_DB_EXECUTE_WALK    *Info = (ACPI_DB_EXECUTE_WALK *) Context;
457     ACPI_BUFFER             ReturnObj;
458     ACPI_STATUS             Status;
459     char                    *Pathname;
460     UINT32                  i;
461     ACPI_DEVICE_INFO        *ObjInfo;
462     ACPI_OBJECT_LIST        ParamObjects;
463     ACPI_OBJECT             Params[ACPI_METHOD_NUM_ARGS];
464     const ACPI_PREDEFINED_INFO *Predefined;
465 
466 
467     Predefined = AcpiUtMatchPredefinedMethod (Node->Name.Ascii);
468     if (!Predefined)
469     {
470         return (AE_OK);
471     }
472 
473     if (Node->Type == ACPI_TYPE_LOCAL_SCOPE)
474     {
475         return (AE_OK);
476     }
477 
478     Pathname = AcpiNsGetExternalPathname (Node);
479     if (!Pathname)
480     {
481         return (AE_OK);
482     }
483 
484     /* Get the object info for number of method parameters */
485 
486     Status = AcpiGetObjectInfo (ObjHandle, &ObjInfo);
487     if (ACPI_FAILURE (Status))
488     {
489         ACPI_FREE (Pathname);
490         return (Status);
491     }
492 
493     ParamObjects.Pointer = NULL;
494     ParamObjects.Count   = 0;
495 
496     if (ObjInfo->Type == ACPI_TYPE_METHOD)
497     {
498         /* Setup default parameters */
499 
500         for (i = 0; i < ObjInfo->ParamCount; i++)
501         {
502             Params[i].Type           = ACPI_TYPE_INTEGER;
503             Params[i].Integer.Value  = 1;
504         }
505 
506         ParamObjects.Pointer     = Params;
507         ParamObjects.Count       = ObjInfo->ParamCount;
508     }
509 
510     ACPI_FREE (ObjInfo);
511     ReturnObj.Pointer = NULL;
512     ReturnObj.Length = ACPI_ALLOCATE_BUFFER;
513 
514     /* Do the actual method execution */
515 
516     AcpiGbl_MethodExecuting = TRUE;
517 
518     Status = AcpiEvaluateObject (Node, NULL, &ParamObjects, &ReturnObj);
519 
520     AcpiOsPrintf ("%-32s returned %s\n", Pathname, AcpiFormatException (Status));
521     AcpiGbl_MethodExecuting = FALSE;
522     ACPI_FREE (Pathname);
523 
524     /* Ignore status from method execution */
525 
526     Status = AE_OK;
527 
528     /* Update count, check if we have executed enough methods */
529 
530     Info->Count++;
531     if (Info->Count >= Info->MaxCount)
532     {
533         Status = AE_CTRL_TERMINATE;
534     }
535 
536     return (Status);
537 }
538 
539 
540 /*******************************************************************************
541  *
542  * FUNCTION:    AcpiDbEvaluatePredefinedNames
543  *
544  * PARAMETERS:  None
545  *
546  * RETURN:      None
547  *
548  * DESCRIPTION: Namespace batch execution. Execute predefined names in the
549  *              namespace, up to the max count, if specified.
550  *
551  ******************************************************************************/
552 
553 void
554 AcpiDbEvaluatePredefinedNames (
555     void)
556 {
557     ACPI_DB_EXECUTE_WALK    Info;
558 
559 
560     Info.Count = 0;
561     Info.MaxCount = ACPI_UINT32_MAX;
562 
563     /* Search all nodes in namespace */
564 
565     (void) AcpiWalkNamespace (ACPI_TYPE_ANY, ACPI_ROOT_OBJECT, ACPI_UINT32_MAX,
566                 AcpiDbWalkForExecute, NULL, (void *) &Info, NULL);
567 
568     AcpiOsPrintf ("Evaluated %u predefined names in the namespace\n", Info.Count);
569 }
570