xref: /reactos/drivers/bus/acpi/acpica/parser/psloop.c (revision a4193ade)
1 /******************************************************************************
2  *
3  * Module Name: psloop - Main AML parse loop
4  *
5  *****************************************************************************/
6 
7 /*
8  * Copyright (C) 2000 - 2022, 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 MERCHANTABILITY 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 /*
45  * Parse the AML and build an operation tree as most interpreters, (such as
46  * Perl) do. Parsing is done by hand rather than with a YACC generated parser
47  * to tightly constrain stack and dynamic memory usage. Parsing is kept
48  * flexible and the code fairly compact by parsing based on a list of AML
49  * opcode templates in AmlOpInfo[].
50  */
51 
52 #include "acpi.h"
53 #include "accommon.h"
54 #include "acinterp.h"
55 #include "acparser.h"
56 #include "acdispat.h"
57 #include "amlcode.h"
58 #include "acconvert.h"
59 #include "acnamesp.h"
60 
61 #define _COMPONENT          ACPI_PARSER
62         ACPI_MODULE_NAME    ("psloop")
63 
64 
65 /* Local prototypes */
66 
67 static ACPI_STATUS
68 AcpiPsGetArguments (
69     ACPI_WALK_STATE         *WalkState,
70     UINT8                   *AmlOpStart,
71     ACPI_PARSE_OBJECT       *Op);
72 
73 
74 /*******************************************************************************
75  *
76  * FUNCTION:    AcpiPsGetArguments
77  *
78  * PARAMETERS:  WalkState           - Current state
79  *              AmlOpStart          - Op start in AML
80  *              Op                  - Current Op
81  *
82  * RETURN:      Status
83  *
84  * DESCRIPTION: Get arguments for passed Op.
85  *
86  ******************************************************************************/
87 
88 static ACPI_STATUS
89 AcpiPsGetArguments (
90     ACPI_WALK_STATE         *WalkState,
91     UINT8                   *AmlOpStart,
92     ACPI_PARSE_OBJECT       *Op)
93 {
94     ACPI_STATUS             Status = AE_OK;
95     ACPI_PARSE_OBJECT       *Arg = NULL;
96 
97 
98     ACPI_FUNCTION_TRACE_PTR (PsGetArguments, WalkState);
99 
100 
101     ACPI_DEBUG_PRINT ((ACPI_DB_PARSE,
102         "Get arguments for opcode [%s]\n", Op->Common.AmlOpName));
103 
104     switch (Op->Common.AmlOpcode)
105     {
106     case AML_BYTE_OP:       /* AML_BYTEDATA_ARG */
107     case AML_WORD_OP:       /* AML_WORDDATA_ARG */
108     case AML_DWORD_OP:      /* AML_DWORDATA_ARG */
109     case AML_QWORD_OP:      /* AML_QWORDATA_ARG */
110     case AML_STRING_OP:     /* AML_ASCIICHARLIST_ARG */
111 
112         /* Fill in constant or string argument directly */
113 
114         AcpiPsGetNextSimpleArg (&(WalkState->ParserState),
115             GET_CURRENT_ARG_TYPE (WalkState->ArgTypes), Op);
116         break;
117 
118     case AML_INT_NAMEPATH_OP:   /* AML_NAMESTRING_ARG */
119 
120         Status = AcpiPsGetNextNamepath (WalkState,
121             &(WalkState->ParserState), Op, ACPI_POSSIBLE_METHOD_CALL);
122         if (ACPI_FAILURE (Status))
123         {
124             return_ACPI_STATUS (Status);
125         }
126 
127         WalkState->ArgTypes = 0;
128         break;
129 
130     default:
131         /*
132          * Op is not a constant or string, append each argument to the Op
133          */
134         while (GET_CURRENT_ARG_TYPE (WalkState->ArgTypes) &&
135             !WalkState->ArgCount)
136         {
137             WalkState->Aml = WalkState->ParserState.Aml;
138 
139             switch (Op->Common.AmlOpcode)
140             {
141             case AML_METHOD_OP:
142             case AML_BUFFER_OP:
143             case AML_PACKAGE_OP:
144             case AML_VARIABLE_PACKAGE_OP:
145             case AML_WHILE_OP:
146 
147                 break;
148 
149             default:
150 
151                 ASL_CV_CAPTURE_COMMENTS (WalkState);
152                 break;
153             }
154 
155             Status = AcpiPsGetNextArg (WalkState, &(WalkState->ParserState),
156                 GET_CURRENT_ARG_TYPE (WalkState->ArgTypes), &Arg);
157             if (ACPI_FAILURE (Status))
158             {
159                 return_ACPI_STATUS (Status);
160             }
161 
162             if (Arg)
163             {
164                 AcpiPsAppendArg (Op, Arg);
165             }
166 
167             INCREMENT_ARG_LIST (WalkState->ArgTypes);
168         }
169 
170         ACPI_DEBUG_PRINT ((ACPI_DB_PARSE,
171             "Final argument count: %8.8X pass %u\n",
172             WalkState->ArgCount, WalkState->PassNumber));
173 
174         /* Special processing for certain opcodes */
175 
176         switch (Op->Common.AmlOpcode)
177         {
178         case AML_METHOD_OP:
179             /*
180              * Skip parsing of control method because we don't have enough
181              * info in the first pass to parse it correctly.
182              *
183              * Save the length and address of the body
184              */
185             Op->Named.Data = WalkState->ParserState.Aml;
186             Op->Named.Length = (UINT32)
187                 (WalkState->ParserState.PkgEnd - WalkState->ParserState.Aml);
188 
189             /* Skip body of method */
190 
191             WalkState->ParserState.Aml = WalkState->ParserState.PkgEnd;
192             WalkState->ArgCount = 0;
193             break;
194 
195         case AML_BUFFER_OP:
196         case AML_PACKAGE_OP:
197         case AML_VARIABLE_PACKAGE_OP:
198 
199             if ((Op->Common.Parent) &&
200                 (Op->Common.Parent->Common.AmlOpcode == AML_NAME_OP) &&
201                 (WalkState->PassNumber <= ACPI_IMODE_LOAD_PASS2))
202             {
203                 ACPI_DEBUG_PRINT ((ACPI_DB_PARSE,
204                     "Setup Package/Buffer: Pass %u, AML Ptr: %p\n",
205                     WalkState->PassNumber, AmlOpStart));
206 
207                 /*
208                  * Skip parsing of Buffers and Packages because we don't have
209                  * enough info in the first pass to parse them correctly.
210                  */
211                 Op->Named.Data = AmlOpStart;
212                 Op->Named.Length = (UINT32)
213                     (WalkState->ParserState.PkgEnd - AmlOpStart);
214 
215                 /* Skip body */
216 
217                 WalkState->ParserState.Aml = WalkState->ParserState.PkgEnd;
218                 WalkState->ArgCount = 0;
219             }
220             break;
221 
222         case AML_WHILE_OP:
223 
224             if (WalkState->ControlState)
225             {
226                 WalkState->ControlState->Control.PackageEnd =
227                     WalkState->ParserState.PkgEnd;
228             }
229             break;
230 
231         default:
232 
233             /* No action for all other opcodes */
234 
235             break;
236         }
237 
238         break;
239     }
240 
241     return_ACPI_STATUS (AE_OK);
242 }
243 
244 
245 /*******************************************************************************
246  *
247  * FUNCTION:    AcpiPsParseLoop
248  *
249  * PARAMETERS:  WalkState           - Current state
250  *
251  * RETURN:      Status
252  *
253  * DESCRIPTION: Parse AML (pointed to by the current parser state) and return
254  *              a tree of ops.
255  *
256  ******************************************************************************/
257 
258 ACPI_STATUS
259 AcpiPsParseLoop (
260     ACPI_WALK_STATE         *WalkState)
261 {
262     ACPI_STATUS             Status = AE_OK;
263     ACPI_PARSE_OBJECT       *Op = NULL;     /* current op */
264     ACPI_PARSE_STATE        *ParserState;
265     UINT8                   *AmlOpStart = NULL;
266     UINT8                   OpcodeLength;
267 
268 
269     ACPI_FUNCTION_TRACE_PTR (PsParseLoop, WalkState);
270 
271 
272     if (WalkState->DescendingCallback == NULL)
273     {
274         return_ACPI_STATUS (AE_BAD_PARAMETER);
275     }
276 
277     ParserState = &WalkState->ParserState;
278     WalkState->ArgTypes = 0;
279 
280 #ifndef ACPI_CONSTANT_EVAL_ONLY
281 
282     if (WalkState->WalkType & ACPI_WALK_METHOD_RESTART)
283     {
284         /* We are restarting a preempted control method */
285 
286         if (AcpiPsHasCompletedScope (ParserState))
287         {
288             /*
289              * We must check if a predicate to an IF or WHILE statement
290              * was just completed
291              */
292             if ((ParserState->Scope->ParseScope.Op) &&
293                ((ParserState->Scope->ParseScope.Op->Common.AmlOpcode == AML_IF_OP) ||
294                 (ParserState->Scope->ParseScope.Op->Common.AmlOpcode == AML_WHILE_OP)) &&
295                 (WalkState->ControlState) &&
296                 (WalkState->ControlState->Common.State ==
297                     ACPI_CONTROL_PREDICATE_EXECUTING))
298             {
299                 /*
300                  * A predicate was just completed, get the value of the
301                  * predicate and branch based on that value
302                  */
303                 WalkState->Op = NULL;
304                 Status = AcpiDsGetPredicateValue (WalkState, ACPI_TO_POINTER (TRUE));
305                 if (ACPI_FAILURE (Status) && !ACPI_CNTL_EXCEPTION (Status))
306                 {
307                     if (Status == AE_AML_NO_RETURN_VALUE)
308                     {
309                         ACPI_EXCEPTION ((AE_INFO, Status,
310                             "Invoked method did not return a value"));
311                     }
312 
313                     ACPI_EXCEPTION ((AE_INFO, Status, "GetPredicate Failed"));
314                     return_ACPI_STATUS (Status);
315                 }
316 
317                 Status = AcpiPsNextParseState (WalkState, Op, Status);
318             }
319 
320             AcpiPsPopScope (ParserState, &Op,
321                 &WalkState->ArgTypes, &WalkState->ArgCount);
322             ACPI_DEBUG_PRINT ((ACPI_DB_PARSE, "Popped scope, Op=%p\n", Op));
323         }
324         else if (WalkState->PrevOp)
325         {
326             /* We were in the middle of an op */
327 
328             Op = WalkState->PrevOp;
329             WalkState->ArgTypes = WalkState->PrevArgTypes;
330         }
331     }
332 #endif
333 
334     /* Iterative parsing loop, while there is more AML to process: */
335 
336     while ((ParserState->Aml < ParserState->AmlEnd) || (Op))
337     {
338         ASL_CV_CAPTURE_COMMENTS (WalkState);
339 
340         AmlOpStart = ParserState->Aml;
341         if (!Op)
342         {
343             Status = AcpiPsCreateOp (WalkState, AmlOpStart, &Op);
344             if (ACPI_FAILURE (Status))
345             {
346                 /*
347                  * ACPI_PARSE_MODULE_LEVEL means that we are loading a table by
348                  * executing it as a control method. However, if we encounter
349                  * an error while loading the table, we need to keep trying to
350                  * load the table rather than aborting the table load. Set the
351                  * status to AE_OK to proceed with the table load.
352                  */
353                 if ((WalkState->ParseFlags & ACPI_PARSE_MODULE_LEVEL) &&
354                     ((Status == AE_ALREADY_EXISTS) || (Status == AE_NOT_FOUND)))
355                 {
356                     Status = AE_OK;
357                 }
358                 if (Status == AE_CTRL_PARSE_CONTINUE)
359                 {
360                     continue;
361                 }
362 
363                 if (Status == AE_CTRL_PARSE_PENDING)
364                 {
365                     Status = AE_OK;
366                 }
367 
368                 if (Status == AE_CTRL_TERMINATE)
369                 {
370                     return_ACPI_STATUS (Status);
371                 }
372 
373                 Status = AcpiPsCompleteOp (WalkState, &Op, Status);
374                 if (ACPI_FAILURE (Status))
375                 {
376                     return_ACPI_STATUS (Status);
377                 }
378                 if (AcpiNsOpensScope (
379                     AcpiPsGetOpcodeInfo (WalkState->Opcode)->ObjectType))
380                 {
381                     /*
382                      * If the scope/device op fails to parse, skip the body of
383                      * the scope op because the parse failure indicates that
384                      * the device may not exist.
385                      */
386                     ACPI_INFO (("Skipping parse of AML opcode: %s (0x%4.4X)",
387                         AcpiPsGetOpcodeName (WalkState->Opcode), WalkState->Opcode));
388 
389                     /*
390                      * Determine the opcode length before skipping the opcode.
391                      * An opcode can be 1 byte or 2 bytes in length.
392                      */
393                     OpcodeLength = 1;
394                     if ((WalkState->Opcode & 0xFF00) == AML_EXTENDED_OPCODE)
395                     {
396                         OpcodeLength = 2;
397                     }
398                     WalkState->ParserState.Aml = WalkState->Aml + OpcodeLength;
399 
400                     WalkState->ParserState.Aml =
401                         AcpiPsGetNextPackageEnd(&WalkState->ParserState);
402                     WalkState->Aml = WalkState->ParserState.Aml;
403                 }
404 
405                 continue;
406             }
407 
408             AcpiExStartTraceOpcode (Op, WalkState);
409         }
410 
411         /*
412          * Start ArgCount at zero because we don't know if there are
413          * any args yet
414          */
415         WalkState->ArgCount = 0;
416 
417         switch (Op->Common.AmlOpcode)
418         {
419         case AML_BYTE_OP:
420         case AML_WORD_OP:
421         case AML_DWORD_OP:
422         case AML_QWORD_OP:
423 
424             break;
425 
426         default:
427 
428             ASL_CV_CAPTURE_COMMENTS (WalkState);
429             break;
430         }
431 
432         /* Are there any arguments that must be processed? */
433 
434         if (WalkState->ArgTypes)
435         {
436             /* Get arguments */
437 
438             Status = AcpiPsGetArguments (WalkState, AmlOpStart, Op);
439             if (ACPI_FAILURE (Status))
440             {
441                 Status = AcpiPsCompleteOp (WalkState, &Op, Status);
442                 if (ACPI_FAILURE (Status))
443                 {
444                     return_ACPI_STATUS (Status);
445                 }
446                 if ((WalkState->ControlState) &&
447                     ((WalkState->ControlState->Control.Opcode == AML_IF_OP) ||
448                     (WalkState->ControlState->Control.Opcode == AML_WHILE_OP)))
449                 {
450                     /*
451                      * If the if/while op fails to parse, we will skip parsing
452                      * the body of the op.
453                      */
454                     ParserState->Aml =
455                         WalkState->ControlState->Control.AmlPredicateStart + 1;
456                     ParserState->Aml =
457                         AcpiPsGetNextPackageEnd (ParserState);
458                     WalkState->Aml = ParserState->Aml;
459 
460                     ACPI_ERROR ((AE_INFO, "Skipping While/If block"));
461                     if (*WalkState->Aml == AML_ELSE_OP)
462                     {
463                         ACPI_ERROR ((AE_INFO, "Skipping Else block"));
464                         WalkState->ParserState.Aml = WalkState->Aml + 1;
465                         WalkState->ParserState.Aml =
466                             AcpiPsGetNextPackageEnd (ParserState);
467                         WalkState->Aml = ParserState->Aml;
468                     }
469                     ACPI_FREE(AcpiUtPopGenericState (&WalkState->ControlState));
470                 }
471                 Op = NULL;
472                 continue;
473             }
474         }
475 
476         /* Check for arguments that need to be processed */
477 
478         ACPI_DEBUG_PRINT ((ACPI_DB_PARSE,
479             "Parseloop: argument count: %8.8X\n", WalkState->ArgCount));
480 
481         if (WalkState->ArgCount)
482         {
483             /*
484              * There are arguments (complex ones), push Op and
485              * prepare for argument
486              */
487             Status = AcpiPsPushScope (ParserState, Op,
488                 WalkState->ArgTypes, WalkState->ArgCount);
489             if (ACPI_FAILURE (Status))
490             {
491                 Status = AcpiPsCompleteOp (WalkState, &Op, Status);
492                 if (ACPI_FAILURE (Status))
493                 {
494                     return_ACPI_STATUS (Status);
495                 }
496 
497                 continue;
498             }
499 
500             Op = NULL;
501             continue;
502         }
503 
504         /*
505          * All arguments have been processed -- Op is complete,
506          * prepare for next
507          */
508         WalkState->OpInfo = AcpiPsGetOpcodeInfo (Op->Common.AmlOpcode);
509         if (WalkState->OpInfo->Flags & AML_NAMED)
510         {
511             if (Op->Common.AmlOpcode == AML_REGION_OP ||
512                 Op->Common.AmlOpcode == AML_DATA_REGION_OP)
513             {
514                 /*
515                  * Skip parsing of control method or opregion body,
516                  * because we don't have enough info in the first pass
517                  * to parse them correctly.
518                  *
519                  * Completed parsing an OpRegion declaration, we now
520                  * know the length.
521                  */
522                 Op->Named.Length = (UINT32) (ParserState->Aml - Op->Named.Data);
523             }
524         }
525 
526         if (WalkState->OpInfo->Flags & AML_CREATE)
527         {
528             /*
529              * Backup to beginning of CreateXXXfield declaration (1 for
530              * Opcode)
531              *
532              * BodyLength is unknown until we parse the body
533              */
534             Op->Named.Length = (UINT32) (ParserState->Aml - Op->Named.Data);
535         }
536 
537         if (Op->Common.AmlOpcode == AML_BANK_FIELD_OP)
538         {
539             /*
540              * Backup to beginning of BankField declaration
541              *
542              * BodyLength is unknown until we parse the body
543              */
544             Op->Named.Length = (UINT32) (ParserState->Aml - Op->Named.Data);
545         }
546 
547         /* This op complete, notify the dispatcher */
548 
549         if (WalkState->AscendingCallback != NULL)
550         {
551             WalkState->Op = Op;
552             WalkState->Opcode = Op->Common.AmlOpcode;
553 
554             Status = WalkState->AscendingCallback (WalkState);
555             Status = AcpiPsNextParseState (WalkState, Op, Status);
556             if (Status == AE_CTRL_PENDING)
557             {
558                 Status = AE_OK;
559             }
560             else if ((WalkState->ParseFlags & ACPI_PARSE_MODULE_LEVEL) &&
561                 (ACPI_AML_EXCEPTION(Status) || Status == AE_ALREADY_EXISTS ||
562                 Status == AE_NOT_FOUND))
563             {
564                 /*
565                  * ACPI_PARSE_MODULE_LEVEL flag means that we are currently
566                  * loading a table by executing it as a control method.
567                  * However, if we encounter an error while loading the table,
568                  * we need to keep trying to load the table rather than
569                  * aborting the table load (setting the status to AE_OK
570                  * continues the table load). If we get a failure at this
571                  * point, it means that the dispatcher got an error while
572                  * trying to execute the Op.
573                  */
574                 Status = AE_OK;
575             }
576         }
577 
578         Status = AcpiPsCompleteOp (WalkState, &Op, Status);
579         if (ACPI_FAILURE (Status))
580         {
581             return_ACPI_STATUS (Status);
582         }
583 
584     } /* while ParserState->Aml */
585 
586     Status = AcpiPsCompleteFinalOp (WalkState, Op, Status);
587     return_ACPI_STATUS (Status);
588 }
589