1 /******************************************************************************
2 *
3 * Module Name: adwalk - Disassembler routines for switch statements
4 *
5 *****************************************************************************/
6
7 /******************************************************************************
8 *
9 * 1. Copyright Notice
10 *
11 * Some or all of this work - Copyright (c) 1999 - 2018, 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 "acparser.h"
155 #include "amlcode.h"
156 #include "acdisasm.h"
157 #include "acdispat.h"
158 #include "acnamesp.h"
159 #include "acapps.h"
160
161
162 #define _COMPONENT ACPI_CA_DISASSEMBLER
163 ACPI_MODULE_NAME ("dmswitch")
164
165 static BOOLEAN
166 AcpiDmIsSwitchBlock (
167 ACPI_PARSE_OBJECT *Op,
168 char **Temp);
169
170 static BOOLEAN
171 AcpiDmIsCaseBlock (
172 ACPI_PARSE_OBJECT *Op);
173
174
175 /*******************************************************************************
176 *
177 * FUNCTION: AcpiDmProcessSwitch
178 *
179 * PARAMETERS: Op - Object to be examined
180 *
181 * RETURN: ACPI_STATUS
182 *
183 * DESCRIPTION: Walk function to create a list of all temporary (_T_) objects.
184 * If a While loop is found that can be converted to a Switch, do
185 * the conversion, remove the temporary name from the list, and
186 * mark the parse op with an IGNORE flag.
187 *
188 ******************************************************************************/
189
190 ACPI_STATUS
AcpiDmProcessSwitch(ACPI_PARSE_OBJECT * Op)191 AcpiDmProcessSwitch (
192 ACPI_PARSE_OBJECT *Op)
193 {
194 char *Temp = NULL;
195 ACPI_PARSE_OBJECT_LIST *NewTemp;
196 ACPI_PARSE_OBJECT_LIST *Current;
197 ACPI_PARSE_OBJECT_LIST *Previous;
198 BOOLEAN FoundTemp = FALSE;
199
200
201 switch (Op->Common.AmlOpcode)
202 {
203 case AML_NAME_OP:
204
205 Temp = (char *) (&Op->Named.Name);
206
207 if (!strncmp(Temp, "_T_", 3))
208 {
209 /* Allocate and init a new Temp List node */
210
211 NewTemp = ACPI_ALLOCATE_ZEROED (sizeof (ACPI_PARSE_OBJECT_LIST));
212 if (!NewTemp)
213 {
214 return (AE_NO_MEMORY);
215 }
216
217 if (AcpiGbl_TempListHead)
218 {
219 Current = AcpiGbl_TempListHead;
220 AcpiGbl_TempListHead = NewTemp;
221 AcpiGbl_TempListHead->Op = Op;
222 AcpiGbl_TempListHead->Next = Current;
223 }
224 else
225 {
226 AcpiGbl_TempListHead = NewTemp;
227 AcpiGbl_TempListHead->Op = Op;
228 AcpiGbl_TempListHead->Next = NULL;
229 }
230 }
231 break;
232
233 case AML_WHILE_OP:
234
235 if (!AcpiDmIsSwitchBlock (Op, &Temp))
236 {
237 break;
238 }
239
240 /* Found a Switch */
241
242 Op->Common.DisasmOpcode = ACPI_DASM_SWITCH;
243
244 Previous = Current = AcpiGbl_TempListHead;
245 while (Current)
246 {
247 /* Note, if we get here Temp is not NULL */
248
249 if (!strncmp(Temp, (char *) (&Current->Op->Named.Name), 4))
250 {
251 /* Match found. Ignore disassembly */
252
253 Current->Op->Common.DisasmFlags |= ACPI_PARSEOP_IGNORE;
254
255 /* Remove from list */
256
257 if (Current == AcpiGbl_TempListHead)
258 {
259 AcpiGbl_TempListHead = Current->Next;
260 }
261 else
262 {
263 Previous->Next = Current->Next;
264 }
265
266 Current->Op = NULL;
267 Current->Next = NULL;
268 ACPI_FREE (Current);
269 FoundTemp = TRUE;
270 break;
271 }
272
273 Previous = Current;
274 Current = Current->Next;
275 }
276
277 if (!FoundTemp)
278 {
279 fprintf (stderr,
280 "Warning: Declaration for temp name %.4s not found\n", Temp);
281 }
282 break;
283
284 default:
285 break;
286 }
287
288 return (AE_OK);
289 }
290
291
292 /*******************************************************************************
293 *
294 * FUNCTION: AcpiDmClearTempList
295 *
296 * PARAMETERS: None
297 *
298 * RETURN: None
299 *
300 * DESCRIPTION: Removes any remaining temporary objects from global list and
301 * frees
302 *
303 ******************************************************************************/
304
305 void
AcpiDmClearTempList(void)306 AcpiDmClearTempList (
307 void)
308 {
309 ACPI_PARSE_OBJECT_LIST *Current;
310
311
312 while (AcpiGbl_TempListHead)
313 {
314 Current = AcpiGbl_TempListHead;
315 AcpiGbl_TempListHead = AcpiGbl_TempListHead->Next;
316 Current->Op = NULL;
317 Current->Next = NULL;
318 ACPI_FREE (Current);
319 }
320 }
321
322
323 /*******************************************************************************
324 *
325 * FUNCTION: AcpiDmIsSwitchBlock
326 *
327 * PARAMETERS: Op - While Object
328 *
329 * RETURN: TRUE if While block can be converted to a Switch/Case block
330 *
331 * DESCRIPTION: Determines if While block is a Switch/Case statement. Modifies
332 * parse tree to allow for Switch/Case disassembly during walk.
333 *
334 * EXAMPLE: Example of parse tree to be converted
335 *
336 * While
337 * One
338 * Store
339 * ByteConst
340 * -NamePath-
341 * If
342 * LEqual
343 * -NamePath-
344 * Zero
345 * Return
346 * One
347 * Else
348 * Return
349 * WordConst
350 * Break
351 *
352 ******************************************************************************/
353
354 BOOLEAN
AcpiDmIsSwitchBlock(ACPI_PARSE_OBJECT * Op,char ** Temp)355 AcpiDmIsSwitchBlock (
356 ACPI_PARSE_OBJECT *Op,
357 char **Temp)
358 {
359 ACPI_PARSE_OBJECT *OneOp;
360 ACPI_PARSE_OBJECT *StoreOp;
361 ACPI_PARSE_OBJECT *NamePathOp;
362 ACPI_PARSE_OBJECT *PredicateOp;
363 ACPI_PARSE_OBJECT *CurrentOp;
364 ACPI_PARSE_OBJECT *TempOp;
365
366
367 /* Check for One Op Predicate */
368
369 OneOp = AcpiPsGetArg (Op, 0);
370 if (!OneOp || (OneOp->Common.AmlOpcode != AML_ONE_OP))
371 {
372 return (FALSE);
373 }
374
375 /* Check for Store Op */
376
377 StoreOp = OneOp->Common.Next;
378 if (!StoreOp || (StoreOp->Common.AmlOpcode != AML_STORE_OP))
379 {
380 return (FALSE);
381 }
382
383 /* Check for Name Op with _T_ string */
384
385 NamePathOp = AcpiPsGetArg (StoreOp, 1);
386 if (!NamePathOp ||
387 (NamePathOp->Common.AmlOpcode != AML_INT_NAMEPATH_OP))
388 {
389 return (FALSE);
390 }
391
392 if (strncmp ((char *) (NamePathOp->Common.Value.Name), "_T_", 3))
393 {
394 return (FALSE);
395 }
396
397 *Temp = (char *) (NamePathOp->Common.Value.Name);
398
399 /* This is a Switch/Case control block */
400
401 /* Ignore the One Op Predicate */
402
403 OneOp->Common.DisasmFlags |= ACPI_PARSEOP_IGNORE;
404
405 /* Ignore the Store Op, but not the children */
406
407 StoreOp->Common.DisasmOpcode = ACPI_DASM_IGNORE_SINGLE;
408
409 /*
410 * First arg of Store Op is the Switch condition.
411 * Mark it as a Switch predicate and as a parameter list for paren
412 * closing and correct indentation.
413 */
414 PredicateOp = AcpiPsGetArg (StoreOp, 0);
415 PredicateOp->Common.DisasmOpcode = ACPI_DASM_SWITCH_PREDICATE;
416 PredicateOp->Common.DisasmFlags |= ACPI_PARSEOP_PARAMETER_LIST;
417
418 /* Ignore the Name Op */
419
420 NamePathOp->Common.DisasmFlags = ACPI_PARSEOP_IGNORE;
421
422 /* Remaining opcodes are the Case statements (If/ElseIf's) */
423
424 CurrentOp = StoreOp->Common.Next;
425 while (AcpiDmIsCaseBlock (CurrentOp))
426 {
427 /* Block is a Case structure */
428
429 if (CurrentOp->Common.AmlOpcode == AML_ELSE_OP)
430 {
431 /* ElseIf */
432
433 CurrentOp->Common.DisasmOpcode = ACPI_DASM_CASE;
434 CurrentOp = AcpiPsGetArg (CurrentOp, 0);
435 }
436
437 /* If */
438
439 CurrentOp->Common.DisasmOpcode = ACPI_DASM_CASE;
440
441 /*
442 * Mark the parse tree for Case disassembly. There are two
443 * types of Case statements. The first type of statement begins with
444 * an LEqual. The second starts with an LNot and uses a Match statement
445 * on a Package of constants.
446 */
447 TempOp = AcpiPsGetArg (CurrentOp, 0);
448 switch (TempOp->Common.AmlOpcode)
449 {
450 case (AML_LOGICAL_EQUAL_OP):
451
452 /* Ignore just the LEqual Op */
453
454 TempOp->Common.DisasmOpcode = ACPI_DASM_IGNORE_SINGLE;
455
456 /* Ignore the NamePath Op */
457
458 TempOp = AcpiPsGetArg (TempOp, 0);
459 TempOp->Common.DisasmFlags = ACPI_PARSEOP_IGNORE;
460
461 /*
462 * Second arg of LEqual will be the Case predicate.
463 * Mark it as a predicate and also as a parameter list for paren
464 * closing and correct indentation.
465 */
466 PredicateOp = TempOp->Common.Next;
467 PredicateOp->Common.DisasmOpcode = ACPI_DASM_SWITCH_PREDICATE;
468 PredicateOp->Common.DisasmFlags |= ACPI_PARSEOP_PARAMETER_LIST;
469 break;
470
471 case (AML_LOGICAL_NOT_OP):
472
473 /*
474 * The Package will be the predicate of the Case statement.
475 * It's under:
476 * LNOT
477 * LEQUAL
478 * MATCH
479 * PACKAGE
480 */
481
482 /* Get the LEqual Op from LNot */
483
484 TempOp = AcpiPsGetArg (TempOp, 0);
485
486 /* Get the Match Op from LEqual */
487
488 TempOp = AcpiPsGetArg (TempOp, 0);
489
490 /* Get the Package Op from Match */
491
492 PredicateOp = AcpiPsGetArg (TempOp, 0);
493
494 /* Mark as parameter list for paren closing */
495
496 PredicateOp->Common.DisasmFlags |= ACPI_PARSEOP_PARAMETER_LIST;
497
498 /*
499 * The Package list would be too deeply indented if we
500 * chose to simply ignore the all the parent opcodes, so
501 * we rearrange the parse tree instead.
502 */
503
504 /*
505 * Save the second arg of the If/Else Op which is the
506 * block code of code for this Case statement.
507 */
508 TempOp = AcpiPsGetArg (CurrentOp, 1);
509
510 /*
511 * Move the Package Op to the child (predicate) of the
512 * Case statement.
513 */
514 CurrentOp->Common.Value.Arg = PredicateOp;
515 PredicateOp->Common.Parent = CurrentOp;
516
517 /* Add the block code */
518
519 PredicateOp->Common.Next = TempOp;
520 break;
521
522 default:
523
524 /* Should never get here */
525 break;
526 }
527
528 /* Advance to next Case block */
529
530 CurrentOp = CurrentOp->Common.Next;
531 }
532
533 /* If CurrentOp is now an Else, then this is a Default block */
534
535 if (CurrentOp && CurrentOp->Common.AmlOpcode == AML_ELSE_OP)
536 {
537 CurrentOp->Common.DisasmOpcode = ACPI_DASM_DEFAULT;
538 }
539
540 /*
541 * From the first If advance to the Break op. It's possible to
542 * have an Else (Default) op here when there is only one Case
543 * statement, so check for it.
544 */
545 CurrentOp = StoreOp->Common.Next->Common.Next;
546 if (!CurrentOp)
547 {
548 return (FALSE);
549 }
550 if (CurrentOp->Common.AmlOpcode == AML_ELSE_OP)
551 {
552 CurrentOp = CurrentOp->Common.Next;
553 }
554
555 /* Ignore the Break Op */
556
557 CurrentOp->Common.DisasmFlags |= ACPI_PARSEOP_IGNORE;
558 return (TRUE);
559 }
560
561
562 /*******************************************************************************
563 *
564 * FUNCTION: AcpiDmIsCaseBlock
565 *
566 * PARAMETERS: Op - Object to test
567 *
568 * RETURN: TRUE if Object is beginning of a Case block.
569 *
570 * DESCRIPTION: Determines if an Object is the beginning of a Case block for a
571 * Switch/Case statement. Parse tree must be one of the following
572 * forms:
573 *
574 * Else (Optional)
575 * If
576 * LEqual
577 * -NamePath- _T_x
578 *
579 * Else (Optional)
580 * If
581 * LNot
582 * LEqual
583 * Match
584 * Package
585 * ByteConst
586 * -NamePath- _T_x
587 *
588 ******************************************************************************/
589
590 static BOOLEAN
AcpiDmIsCaseBlock(ACPI_PARSE_OBJECT * Op)591 AcpiDmIsCaseBlock (
592 ACPI_PARSE_OBJECT *Op)
593 {
594 ACPI_PARSE_OBJECT *CurrentOp;
595
596
597 if (!Op)
598 {
599 return (FALSE);
600 }
601
602 /* Look for an If or ElseIf */
603
604 CurrentOp = Op;
605 if (CurrentOp->Common.AmlOpcode == AML_ELSE_OP)
606 {
607 CurrentOp = AcpiPsGetArg (CurrentOp, 0);
608 if (!CurrentOp)
609 {
610 return (FALSE);
611 }
612 }
613
614 if (!CurrentOp || CurrentOp->Common.AmlOpcode != AML_IF_OP)
615 {
616 return (FALSE);
617 }
618
619 /* Child must be LEqual or LNot */
620
621 CurrentOp = AcpiPsGetArg (CurrentOp, 0);
622 if (!CurrentOp)
623 {
624 return (FALSE);
625 }
626
627 switch (CurrentOp->Common.AmlOpcode)
628 {
629 case (AML_LOGICAL_EQUAL_OP):
630
631 /* Next child must be NamePath with string _T_ */
632
633 CurrentOp = AcpiPsGetArg (CurrentOp, 0);
634 if (!CurrentOp || !CurrentOp->Common.Value.Name ||
635 strncmp(CurrentOp->Common.Value.Name, "_T_", 3))
636 {
637 return (FALSE);
638 }
639 break;
640
641 case (AML_LOGICAL_NOT_OP):
642
643 /* Child of LNot must be LEqual op */
644
645 CurrentOp = AcpiPsGetArg (CurrentOp, 0);
646 if (!CurrentOp || (CurrentOp->Common.AmlOpcode != AML_LOGICAL_EQUAL_OP))
647 {
648 return (FALSE);
649 }
650
651 /* Child of LNot must be Match op */
652
653 CurrentOp = AcpiPsGetArg (CurrentOp, 0);
654 if (!CurrentOp || (CurrentOp->Common.AmlOpcode != AML_MATCH_OP))
655 {
656 return (FALSE);
657 }
658
659 /* First child of Match must be Package op */
660
661 CurrentOp = AcpiPsGetArg (CurrentOp, 0);
662 if (!CurrentOp || (CurrentOp->Common.AmlOpcode != AML_PACKAGE_OP))
663 {
664 return (FALSE);
665 }
666
667 /* Third child of Match must be NamePath with string _T_ */
668
669 CurrentOp = AcpiPsGetArg (CurrentOp->Common.Parent, 2);
670 if (!CurrentOp || !CurrentOp->Common.Value.Name ||
671 strncmp(CurrentOp->Common.Value.Name, "_T_", 3))
672 {
673 return (FALSE);
674 }
675 break;
676
677 default:
678
679 return (FALSE);
680 }
681
682 return (TRUE);
683 }
684