1 /****************************************************************************** 2 * 3 * Module Name: psloop - Main AML parse loop 4 * 5 *****************************************************************************/ 6 7 /* 8 * Copyright (C) 2000 - 2019, 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 /* 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) && 306 ((Status & AE_CODE_MASK) != AE_CODE_CONTROL)) 307 { 308 if (Status == AE_AML_NO_RETURN_VALUE) 309 { 310 ACPI_EXCEPTION ((AE_INFO, Status, 311 "Invoked method did not return a value")); 312 } 313 314 ACPI_EXCEPTION ((AE_INFO, Status, "GetPredicate Failed")); 315 return_ACPI_STATUS (Status); 316 } 317 318 Status = AcpiPsNextParseState (WalkState, Op, Status); 319 } 320 321 AcpiPsPopScope (ParserState, &Op, 322 &WalkState->ArgTypes, &WalkState->ArgCount); 323 ACPI_DEBUG_PRINT ((ACPI_DB_PARSE, "Popped scope, Op=%p\n", Op)); 324 } 325 else if (WalkState->PrevOp) 326 { 327 /* We were in the middle of an op */ 328 329 Op = WalkState->PrevOp; 330 WalkState->ArgTypes = WalkState->PrevArgTypes; 331 } 332 } 333 #endif 334 335 /* Iterative parsing loop, while there is more AML to process: */ 336 337 while ((ParserState->Aml < ParserState->AmlEnd) || (Op)) 338 { 339 ASL_CV_CAPTURE_COMMENTS (WalkState); 340 341 AmlOpStart = ParserState->Aml; 342 if (!Op) 343 { 344 Status = AcpiPsCreateOp (WalkState, AmlOpStart, &Op); 345 if (ACPI_FAILURE (Status)) 346 { 347 /* 348 * ACPI_PARSE_MODULE_LEVEL means that we are loading a table by 349 * executing it as a control method. However, if we encounter 350 * an error while loading the table, we need to keep trying to 351 * load the table rather than aborting the table load. Set the 352 * status to AE_OK to proceed with the table load. 353 */ 354 if ((WalkState->ParseFlags & ACPI_PARSE_MODULE_LEVEL) && 355 ((Status == AE_ALREADY_EXISTS) || (Status == AE_NOT_FOUND))) 356 { 357 Status = AE_OK; 358 } 359 if (Status == AE_CTRL_PARSE_CONTINUE) 360 { 361 continue; 362 } 363 364 if (Status == AE_CTRL_PARSE_PENDING) 365 { 366 Status = AE_OK; 367 } 368 369 if (Status == AE_CTRL_TERMINATE) 370 { 371 return_ACPI_STATUS (Status); 372 } 373 374 Status = AcpiPsCompleteOp (WalkState, &Op, Status); 375 if (ACPI_FAILURE (Status)) 376 { 377 return_ACPI_STATUS (Status); 378 } 379 if (AcpiNsOpensScope ( 380 AcpiPsGetOpcodeInfo (WalkState->Opcode)->ObjectType)) 381 { 382 /* 383 * If the scope/device op fails to parse, skip the body of 384 * the scope op because the parse failure indicates that 385 * the device may not exist. 386 */ 387 ACPI_INFO (("Skipping parse of AML opcode: %s (0x%4.4X)", 388 AcpiPsGetOpcodeName (WalkState->Opcode), WalkState->Opcode)); 389 390 /* 391 * Determine the opcode length before skipping the opcode. 392 * An opcode can be 1 byte or 2 bytes in length. 393 */ 394 OpcodeLength = 1; 395 if ((WalkState->Opcode & 0xFF00) == AML_EXTENDED_OPCODE) 396 { 397 OpcodeLength = 2; 398 } 399 WalkState->ParserState.Aml = WalkState->Aml + OpcodeLength; 400 401 WalkState->ParserState.Aml = 402 AcpiPsGetNextPackageEnd(&WalkState->ParserState); 403 WalkState->Aml = WalkState->ParserState.Aml; 404 } 405 406 continue; 407 } 408 409 AcpiExStartTraceOpcode (Op, WalkState); 410 } 411 412 /* 413 * Start ArgCount at zero because we don't know if there are 414 * any args yet 415 */ 416 WalkState->ArgCount = 0; 417 418 switch (Op->Common.AmlOpcode) 419 { 420 case AML_BYTE_OP: 421 case AML_WORD_OP: 422 case AML_DWORD_OP: 423 case AML_QWORD_OP: 424 425 break; 426 427 default: 428 429 ASL_CV_CAPTURE_COMMENTS (WalkState); 430 break; 431 } 432 433 /* Are there any arguments that must be processed? */ 434 435 if (WalkState->ArgTypes) 436 { 437 /* Get arguments */ 438 439 Status = AcpiPsGetArguments (WalkState, AmlOpStart, Op); 440 if (ACPI_FAILURE (Status)) 441 { 442 Status = AcpiPsCompleteOp (WalkState, &Op, Status); 443 if (ACPI_FAILURE (Status)) 444 { 445 return_ACPI_STATUS (Status); 446 } 447 if ((WalkState->ControlState) && 448 ((WalkState->ControlState->Control.Opcode == AML_IF_OP) || 449 (WalkState->ControlState->Control.Opcode == AML_WHILE_OP))) 450 { 451 /* 452 * If the if/while op fails to parse, we will skip parsing 453 * the body of the op. 454 */ 455 ParserState->Aml = 456 WalkState->ControlState->Control.AmlPredicateStart + 1; 457 ParserState->Aml = 458 AcpiPsGetNextPackageEnd (ParserState); 459 WalkState->Aml = ParserState->Aml; 460 461 ACPI_ERROR ((AE_INFO, "Skipping While/If block")); 462 if (*WalkState->Aml == AML_ELSE_OP) 463 { 464 ACPI_ERROR ((AE_INFO, "Skipping Else block")); 465 WalkState->ParserState.Aml = WalkState->Aml + 1; 466 WalkState->ParserState.Aml = 467 AcpiPsGetNextPackageEnd (ParserState); 468 WalkState->Aml = ParserState->Aml; 469 } 470 ACPI_FREE(AcpiUtPopGenericState (&WalkState->ControlState)); 471 } 472 Op = NULL; 473 continue; 474 } 475 } 476 477 /* Check for arguments that need to be processed */ 478 479 ACPI_DEBUG_PRINT ((ACPI_DB_PARSE, 480 "Parseloop: argument count: %8.8X\n", WalkState->ArgCount)); 481 482 if (WalkState->ArgCount) 483 { 484 /* 485 * There are arguments (complex ones), push Op and 486 * prepare for argument 487 */ 488 Status = AcpiPsPushScope (ParserState, Op, 489 WalkState->ArgTypes, WalkState->ArgCount); 490 if (ACPI_FAILURE (Status)) 491 { 492 Status = AcpiPsCompleteOp (WalkState, &Op, Status); 493 if (ACPI_FAILURE (Status)) 494 { 495 return_ACPI_STATUS (Status); 496 } 497 498 continue; 499 } 500 501 Op = NULL; 502 continue; 503 } 504 505 /* 506 * All arguments have been processed -- Op is complete, 507 * prepare for next 508 */ 509 WalkState->OpInfo = AcpiPsGetOpcodeInfo (Op->Common.AmlOpcode); 510 if (WalkState->OpInfo->Flags & AML_NAMED) 511 { 512 if (Op->Common.AmlOpcode == AML_REGION_OP || 513 Op->Common.AmlOpcode == AML_DATA_REGION_OP) 514 { 515 /* 516 * Skip parsing of control method or opregion body, 517 * because we don't have enough info in the first pass 518 * to parse them correctly. 519 * 520 * Completed parsing an OpRegion declaration, we now 521 * know the length. 522 */ 523 Op->Named.Length = (UINT32) (ParserState->Aml - Op->Named.Data); 524 } 525 } 526 527 if (WalkState->OpInfo->Flags & AML_CREATE) 528 { 529 /* 530 * Backup to beginning of CreateXXXfield declaration (1 for 531 * Opcode) 532 * 533 * BodyLength is unknown until we parse the body 534 */ 535 Op->Named.Length = (UINT32) (ParserState->Aml - Op->Named.Data); 536 } 537 538 if (Op->Common.AmlOpcode == AML_BANK_FIELD_OP) 539 { 540 /* 541 * Backup to beginning of BankField declaration 542 * 543 * BodyLength is unknown until we parse the body 544 */ 545 Op->Named.Length = (UINT32) (ParserState->Aml - Op->Named.Data); 546 } 547 548 /* This op complete, notify the dispatcher */ 549 550 if (WalkState->AscendingCallback != NULL) 551 { 552 WalkState->Op = Op; 553 WalkState->Opcode = Op->Common.AmlOpcode; 554 555 Status = WalkState->AscendingCallback (WalkState); 556 Status = AcpiPsNextParseState (WalkState, Op, Status); 557 if (Status == AE_CTRL_PENDING) 558 { 559 Status = AE_OK; 560 } 561 else if ((WalkState->ParseFlags & ACPI_PARSE_MODULE_LEVEL) && 562 (ACPI_AML_EXCEPTION(Status) || Status == AE_ALREADY_EXISTS || 563 Status == AE_NOT_FOUND)) 564 { 565 /* 566 * ACPI_PARSE_MODULE_LEVEL flag means that we are currently 567 * loading a table by executing it as a control method. 568 * However, if we encounter an error while loading the table, 569 * we need to keep trying to load the table rather than 570 * aborting the table load (setting the status to AE_OK 571 * continues the table load). If we get a failure at this 572 * point, it means that the dispatcher got an error while 573 * trying to execute the Op. 574 */ 575 Status = AE_OK; 576 } 577 } 578 579 Status = AcpiPsCompleteOp (WalkState, &Op, Status); 580 if (ACPI_FAILURE (Status)) 581 { 582 return_ACPI_STATUS (Status); 583 } 584 585 } /* while ParserState->Aml */ 586 587 Status = AcpiPsCompleteFinalOp (WalkState, Op, Status); 588 return_ACPI_STATUS (Status); 589 } 590