1 // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0 2 /****************************************************************************** 3 * 4 * Module Name: psloop - Main AML parse loop 5 * 6 * Copyright (C) 2000 - 2018, Intel Corp. 7 * 8 *****************************************************************************/ 9 10 /* 11 * Parse the AML and build an operation tree as most interpreters, (such as 12 * Perl) do. Parsing is done by hand rather than with a YACC generated parser 13 * to tightly constrain stack and dynamic memory usage. Parsing is kept 14 * flexible and the code fairly compact by parsing based on a list of AML 15 * opcode templates in aml_op_info[]. 16 */ 17 18 #include <acpi/acpi.h> 19 #include "accommon.h" 20 #include "acinterp.h" 21 #include "acparser.h" 22 #include "acdispat.h" 23 #include "amlcode.h" 24 #include "acconvert.h" 25 #include "acnamesp.h" 26 27 #define _COMPONENT ACPI_PARSER 28 ACPI_MODULE_NAME("psloop") 29 30 /* Local prototypes */ 31 static acpi_status 32 acpi_ps_get_arguments(struct acpi_walk_state *walk_state, 33 u8 * aml_op_start, union acpi_parse_object *op); 34 35 static void 36 acpi_ps_link_module_code(union acpi_parse_object *parent_op, 37 u8 *aml_start, u32 aml_length, acpi_owner_id owner_id); 38 39 /******************************************************************************* 40 * 41 * FUNCTION: acpi_ps_get_arguments 42 * 43 * PARAMETERS: walk_state - Current state 44 * aml_op_start - Op start in AML 45 * op - Current Op 46 * 47 * RETURN: Status 48 * 49 * DESCRIPTION: Get arguments for passed Op. 50 * 51 ******************************************************************************/ 52 53 static acpi_status 54 acpi_ps_get_arguments(struct acpi_walk_state *walk_state, 55 u8 * aml_op_start, union acpi_parse_object *op) 56 { 57 acpi_status status = AE_OK; 58 union acpi_parse_object *arg = NULL; 59 const struct acpi_opcode_info *op_info; 60 61 ACPI_FUNCTION_TRACE_PTR(ps_get_arguments, walk_state); 62 63 ACPI_DEBUG_PRINT((ACPI_DB_PARSE, 64 "Get arguments for opcode [%s]\n", 65 op->common.aml_op_name)); 66 67 switch (op->common.aml_opcode) { 68 case AML_BYTE_OP: /* AML_BYTEDATA_ARG */ 69 case AML_WORD_OP: /* AML_WORDDATA_ARG */ 70 case AML_DWORD_OP: /* AML_DWORDATA_ARG */ 71 case AML_QWORD_OP: /* AML_QWORDATA_ARG */ 72 case AML_STRING_OP: /* AML_ASCIICHARLIST_ARG */ 73 74 /* Fill in constant or string argument directly */ 75 76 acpi_ps_get_next_simple_arg(&(walk_state->parser_state), 77 GET_CURRENT_ARG_TYPE(walk_state-> 78 arg_types), 79 op); 80 break; 81 82 case AML_INT_NAMEPATH_OP: /* AML_NAMESTRING_ARG */ 83 84 status = acpi_ps_get_next_namepath(walk_state, 85 &(walk_state->parser_state), 86 op, 87 ACPI_POSSIBLE_METHOD_CALL); 88 if (ACPI_FAILURE(status)) { 89 return_ACPI_STATUS(status); 90 } 91 92 walk_state->arg_types = 0; 93 break; 94 95 default: 96 /* 97 * Op is not a constant or string, append each argument to the Op 98 */ 99 while (GET_CURRENT_ARG_TYPE(walk_state->arg_types) && 100 !walk_state->arg_count) { 101 walk_state->aml = walk_state->parser_state.aml; 102 103 switch (op->common.aml_opcode) { 104 case AML_METHOD_OP: 105 case AML_BUFFER_OP: 106 case AML_PACKAGE_OP: 107 case AML_VARIABLE_PACKAGE_OP: 108 case AML_WHILE_OP: 109 110 break; 111 112 default: 113 114 ASL_CV_CAPTURE_COMMENTS(walk_state); 115 break; 116 } 117 118 status = 119 acpi_ps_get_next_arg(walk_state, 120 &(walk_state->parser_state), 121 GET_CURRENT_ARG_TYPE 122 (walk_state->arg_types), &arg); 123 if (ACPI_FAILURE(status)) { 124 return_ACPI_STATUS(status); 125 } 126 127 if (arg) { 128 acpi_ps_append_arg(op, arg); 129 } 130 131 INCREMENT_ARG_LIST(walk_state->arg_types); 132 } 133 134 ACPI_DEBUG_PRINT((ACPI_DB_PARSE, 135 "Final argument count: %8.8X pass %u\n", 136 walk_state->arg_count, 137 walk_state->pass_number)); 138 139 /* 140 * This case handles the legacy option that groups all module-level 141 * code blocks together and defers execution until all of the tables 142 * are loaded. Execute all of these blocks at this time. 143 * Execute any module-level code that was detected during the table 144 * load phase. 145 * 146 * Note: this option is deprecated and will be eliminated in the 147 * future. Use of this option can cause problems with AML code that 148 * depends upon in-order immediate execution of module-level code. 149 */ 150 if (!acpi_gbl_execute_tables_as_methods && 151 (walk_state->pass_number <= ACPI_IMODE_LOAD_PASS2) && 152 ((walk_state->parse_flags & ACPI_PARSE_DISASSEMBLE) == 0)) { 153 /* 154 * We want to skip If/Else/While constructs during Pass1 because we 155 * want to actually conditionally execute the code during Pass2. 156 * 157 * Except for disassembly, where we always want to walk the 158 * If/Else/While packages 159 */ 160 switch (op->common.aml_opcode) { 161 case AML_IF_OP: 162 case AML_ELSE_OP: 163 case AML_WHILE_OP: 164 /* 165 * Currently supported module-level opcodes are: 166 * IF/ELSE/WHILE. These appear to be the most common, 167 * and easiest to support since they open an AML 168 * package. 169 */ 170 if (walk_state->pass_number == 171 ACPI_IMODE_LOAD_PASS1) { 172 acpi_ps_link_module_code(op->common. 173 parent, 174 aml_op_start, 175 (u32) 176 (walk_state-> 177 parser_state. 178 pkg_end - 179 aml_op_start), 180 walk_state-> 181 owner_id); 182 } 183 184 ACPI_DEBUG_PRINT((ACPI_DB_PARSE, 185 "Pass1: Skipping an If/Else/While body\n")); 186 187 /* Skip body of if/else/while in pass 1 */ 188 189 walk_state->parser_state.aml = 190 walk_state->parser_state.pkg_end; 191 walk_state->arg_count = 0; 192 break; 193 194 default: 195 /* 196 * Check for an unsupported executable opcode at module 197 * level. We must be in PASS1, the parent must be a SCOPE, 198 * The opcode class must be EXECUTE, and the opcode must 199 * not be an argument to another opcode. 200 */ 201 if ((walk_state->pass_number == 202 ACPI_IMODE_LOAD_PASS1) 203 && (op->common.parent->common.aml_opcode == 204 AML_SCOPE_OP)) { 205 op_info = 206 acpi_ps_get_opcode_info(op->common. 207 aml_opcode); 208 if ((op_info->class == 209 AML_CLASS_EXECUTE) && (!arg)) { 210 ACPI_WARNING((AE_INFO, 211 "Unsupported module-level executable opcode " 212 "0x%.2X at table offset 0x%.4X", 213 op->common. 214 aml_opcode, 215 (u32) 216 (ACPI_PTR_DIFF 217 (aml_op_start, 218 walk_state-> 219 parser_state. 220 aml_start) + 221 sizeof(struct 222 acpi_table_header)))); 223 } 224 } 225 break; 226 } 227 } 228 229 /* Special processing for certain opcodes */ 230 231 switch (op->common.aml_opcode) { 232 case AML_METHOD_OP: 233 /* 234 * Skip parsing of control method because we don't have enough 235 * info in the first pass to parse it correctly. 236 * 237 * Save the length and address of the body 238 */ 239 op->named.data = walk_state->parser_state.aml; 240 op->named.length = (u32) 241 (walk_state->parser_state.pkg_end - 242 walk_state->parser_state.aml); 243 244 /* Skip body of method */ 245 246 walk_state->parser_state.aml = 247 walk_state->parser_state.pkg_end; 248 walk_state->arg_count = 0; 249 break; 250 251 case AML_BUFFER_OP: 252 case AML_PACKAGE_OP: 253 case AML_VARIABLE_PACKAGE_OP: 254 255 if ((op->common.parent) && 256 (op->common.parent->common.aml_opcode == 257 AML_NAME_OP) 258 && (walk_state->pass_number <= 259 ACPI_IMODE_LOAD_PASS2)) { 260 ACPI_DEBUG_PRINT((ACPI_DB_PARSE, 261 "Setup Package/Buffer: Pass %u, AML Ptr: %p\n", 262 walk_state->pass_number, 263 aml_op_start)); 264 265 /* 266 * Skip parsing of Buffers and Packages because we don't have 267 * enough info in the first pass to parse them correctly. 268 */ 269 op->named.data = aml_op_start; 270 op->named.length = (u32) 271 (walk_state->parser_state.pkg_end - 272 aml_op_start); 273 274 /* Skip body */ 275 276 walk_state->parser_state.aml = 277 walk_state->parser_state.pkg_end; 278 walk_state->arg_count = 0; 279 } 280 break; 281 282 case AML_WHILE_OP: 283 284 if (walk_state->control_state) { 285 walk_state->control_state->control.package_end = 286 walk_state->parser_state.pkg_end; 287 } 288 break; 289 290 default: 291 292 /* No action for all other opcodes */ 293 294 break; 295 } 296 297 break; 298 } 299 300 return_ACPI_STATUS(AE_OK); 301 } 302 303 /******************************************************************************* 304 * 305 * FUNCTION: acpi_ps_link_module_code 306 * 307 * PARAMETERS: parent_op - Parent parser op 308 * aml_start - Pointer to the AML 309 * aml_length - Length of executable AML 310 * owner_id - owner_id of module level code 311 * 312 * RETURN: None. 313 * 314 * DESCRIPTION: Wrap the module-level code with a method object and link the 315 * object to the global list. Note, the mutex field of the method 316 * object is used to link multiple module-level code objects. 317 * 318 * NOTE: In this legacy option, each block of detected executable AML 319 * code that is outside of any control method is wrapped with a temporary 320 * control method object and placed on a global list below. 321 * 322 * This function executes the module-level code for all tables only after 323 * all of the tables have been loaded. It is a legacy option and is 324 * not compatible with other ACPI implementations. See acpi_ns_load_table. 325 * 326 * This function will be removed when the legacy option is removed. 327 * 328 ******************************************************************************/ 329 330 static void 331 acpi_ps_link_module_code(union acpi_parse_object *parent_op, 332 u8 *aml_start, u32 aml_length, acpi_owner_id owner_id) 333 { 334 union acpi_operand_object *prev; 335 union acpi_operand_object *next; 336 union acpi_operand_object *method_obj; 337 struct acpi_namespace_node *parent_node; 338 339 ACPI_FUNCTION_TRACE(ps_link_module_code); 340 341 /* Get the tail of the list */ 342 343 prev = next = acpi_gbl_module_code_list; 344 while (next) { 345 prev = next; 346 next = next->method.mutex; 347 } 348 349 /* 350 * Insert the module level code into the list. Merge it if it is 351 * adjacent to the previous element. 352 */ 353 if (!prev || 354 ((prev->method.aml_start + prev->method.aml_length) != aml_start)) { 355 356 /* Create, initialize, and link a new temporary method object */ 357 358 method_obj = acpi_ut_create_internal_object(ACPI_TYPE_METHOD); 359 if (!method_obj) { 360 return_VOID; 361 } 362 363 ACPI_DEBUG_PRINT((ACPI_DB_PARSE, 364 "Create/Link new code block: %p\n", 365 method_obj)); 366 367 if (parent_op->common.node) { 368 parent_node = parent_op->common.node; 369 } else { 370 parent_node = acpi_gbl_root_node; 371 } 372 373 method_obj->method.aml_start = aml_start; 374 method_obj->method.aml_length = aml_length; 375 method_obj->method.owner_id = owner_id; 376 method_obj->method.info_flags |= ACPI_METHOD_MODULE_LEVEL; 377 378 /* 379 * Save the parent node in next_object. This is cheating, but we 380 * don't want to expand the method object. 381 */ 382 method_obj->method.next_object = 383 ACPI_CAST_PTR(union acpi_operand_object, parent_node); 384 385 if (!prev) { 386 acpi_gbl_module_code_list = method_obj; 387 } else { 388 prev->method.mutex = method_obj; 389 } 390 } else { 391 ACPI_DEBUG_PRINT((ACPI_DB_PARSE, 392 "Appending to existing code block: %p\n", 393 prev)); 394 395 prev->method.aml_length += aml_length; 396 } 397 398 return_VOID; 399 } 400 401 /******************************************************************************* 402 * 403 * FUNCTION: acpi_ps_parse_loop 404 * 405 * PARAMETERS: walk_state - Current state 406 * 407 * RETURN: Status 408 * 409 * DESCRIPTION: Parse AML (pointed to by the current parser state) and return 410 * a tree of ops. 411 * 412 ******************************************************************************/ 413 414 acpi_status acpi_ps_parse_loop(struct acpi_walk_state *walk_state) 415 { 416 acpi_status status = AE_OK; 417 union acpi_parse_object *op = NULL; /* current op */ 418 struct acpi_parse_state *parser_state; 419 u8 *aml_op_start = NULL; 420 u8 opcode_length; 421 422 ACPI_FUNCTION_TRACE_PTR(ps_parse_loop, walk_state); 423 424 if (walk_state->descending_callback == NULL) { 425 return_ACPI_STATUS(AE_BAD_PARAMETER); 426 } 427 428 parser_state = &walk_state->parser_state; 429 walk_state->arg_types = 0; 430 431 #ifndef ACPI_CONSTANT_EVAL_ONLY 432 433 if (walk_state->walk_type & ACPI_WALK_METHOD_RESTART) { 434 435 /* We are restarting a preempted control method */ 436 437 if (acpi_ps_has_completed_scope(parser_state)) { 438 /* 439 * We must check if a predicate to an IF or WHILE statement 440 * was just completed 441 */ 442 if ((parser_state->scope->parse_scope.op) && 443 ((parser_state->scope->parse_scope.op->common. 444 aml_opcode == AML_IF_OP) 445 || (parser_state->scope->parse_scope.op->common. 446 aml_opcode == AML_WHILE_OP)) 447 && (walk_state->control_state) 448 && (walk_state->control_state->common.state == 449 ACPI_CONTROL_PREDICATE_EXECUTING)) { 450 /* 451 * A predicate was just completed, get the value of the 452 * predicate and branch based on that value 453 */ 454 walk_state->op = NULL; 455 status = 456 acpi_ds_get_predicate_value(walk_state, 457 ACPI_TO_POINTER 458 (TRUE)); 459 if (ACPI_FAILURE(status) 460 && ((status & AE_CODE_MASK) != 461 AE_CODE_CONTROL)) { 462 if (status == AE_AML_NO_RETURN_VALUE) { 463 ACPI_EXCEPTION((AE_INFO, status, 464 "Invoked method did not return a value")); 465 } 466 467 ACPI_EXCEPTION((AE_INFO, status, 468 "GetPredicate Failed")); 469 return_ACPI_STATUS(status); 470 } 471 472 status = 473 acpi_ps_next_parse_state(walk_state, op, 474 status); 475 } 476 477 acpi_ps_pop_scope(parser_state, &op, 478 &walk_state->arg_types, 479 &walk_state->arg_count); 480 ACPI_DEBUG_PRINT((ACPI_DB_PARSE, 481 "Popped scope, Op=%p\n", op)); 482 } else if (walk_state->prev_op) { 483 484 /* We were in the middle of an op */ 485 486 op = walk_state->prev_op; 487 walk_state->arg_types = walk_state->prev_arg_types; 488 } 489 } 490 #endif 491 492 /* Iterative parsing loop, while there is more AML to process: */ 493 494 while ((parser_state->aml < parser_state->aml_end) || (op)) { 495 ASL_CV_CAPTURE_COMMENTS(walk_state); 496 497 aml_op_start = parser_state->aml; 498 if (!op) { 499 status = 500 acpi_ps_create_op(walk_state, aml_op_start, &op); 501 if (ACPI_FAILURE(status)) { 502 /* 503 * ACPI_PARSE_MODULE_LEVEL means that we are loading a table by 504 * executing it as a control method. However, if we encounter 505 * an error while loading the table, we need to keep trying to 506 * load the table rather than aborting the table load. Set the 507 * status to AE_OK to proceed with the table load. 508 */ 509 if ((walk_state-> 510 parse_flags & ACPI_PARSE_MODULE_LEVEL) 511 && ((status == AE_ALREADY_EXISTS) 512 || (status == AE_NOT_FOUND))) { 513 status = AE_OK; 514 } 515 if (status == AE_CTRL_PARSE_CONTINUE) { 516 continue; 517 } 518 519 if (status == AE_CTRL_PARSE_PENDING) { 520 status = AE_OK; 521 } 522 523 if (status == AE_CTRL_TERMINATE) { 524 return_ACPI_STATUS(status); 525 } 526 527 status = 528 acpi_ps_complete_op(walk_state, &op, 529 status); 530 if (ACPI_FAILURE(status)) { 531 return_ACPI_STATUS(status); 532 } 533 if (acpi_ns_opens_scope 534 (acpi_ps_get_opcode_info 535 (walk_state->opcode)->object_type)) { 536 /* 537 * If the scope/device op fails to parse, skip the body of 538 * the scope op because the parse failure indicates that 539 * the device may not exist. 540 */ 541 ACPI_INFO(("Skipping parse of AML opcode: %s (0x%4.4X)", acpi_ps_get_opcode_name(walk_state->opcode), walk_state->opcode)); 542 543 /* 544 * Determine the opcode length before skipping the opcode. 545 * An opcode can be 1 byte or 2 bytes in length. 546 */ 547 opcode_length = 1; 548 if ((walk_state->opcode & 0xFF00) == 549 AML_EXTENDED_OPCODE) { 550 opcode_length = 2; 551 } 552 walk_state->parser_state.aml = 553 walk_state->aml + opcode_length; 554 555 walk_state->parser_state.aml = 556 acpi_ps_get_next_package_end 557 (&walk_state->parser_state); 558 walk_state->aml = 559 walk_state->parser_state.aml; 560 } 561 562 continue; 563 } 564 565 acpi_ex_start_trace_opcode(op, walk_state); 566 } 567 568 /* 569 * Start arg_count at zero because we don't know if there are 570 * any args yet 571 */ 572 walk_state->arg_count = 0; 573 574 switch (op->common.aml_opcode) { 575 case AML_BYTE_OP: 576 case AML_WORD_OP: 577 case AML_DWORD_OP: 578 case AML_QWORD_OP: 579 580 break; 581 582 default: 583 584 ASL_CV_CAPTURE_COMMENTS(walk_state); 585 break; 586 } 587 588 /* Are there any arguments that must be processed? */ 589 590 if (walk_state->arg_types) { 591 592 /* Get arguments */ 593 594 status = 595 acpi_ps_get_arguments(walk_state, aml_op_start, op); 596 if (ACPI_FAILURE(status)) { 597 status = 598 acpi_ps_complete_op(walk_state, &op, 599 status); 600 if (ACPI_FAILURE(status)) { 601 return_ACPI_STATUS(status); 602 } 603 if ((walk_state->control_state) && 604 ((walk_state->control_state->control. 605 opcode == AML_IF_OP) 606 || (walk_state->control_state->control. 607 opcode == AML_WHILE_OP))) { 608 /* 609 * If the if/while op fails to parse, we will skip parsing 610 * the body of the op. 611 */ 612 parser_state->aml = 613 walk_state->control_state->control. 614 aml_predicate_start + 1; 615 parser_state->aml = 616 acpi_ps_get_next_package_end 617 (parser_state); 618 walk_state->aml = parser_state->aml; 619 620 ACPI_ERROR((AE_INFO, 621 "Skipping While/If block")); 622 if (*walk_state->aml == AML_ELSE_OP) { 623 ACPI_ERROR((AE_INFO, 624 "Skipping Else block")); 625 walk_state->parser_state.aml = 626 walk_state->aml + 1; 627 walk_state->parser_state.aml = 628 acpi_ps_get_next_package_end 629 (parser_state); 630 walk_state->aml = 631 parser_state->aml; 632 } 633 ACPI_FREE(acpi_ut_pop_generic_state 634 (&walk_state->control_state)); 635 } 636 op = NULL; 637 continue; 638 } 639 } 640 641 /* Check for arguments that need to be processed */ 642 643 ACPI_DEBUG_PRINT((ACPI_DB_PARSE, 644 "Parseloop: argument count: %8.8X\n", 645 walk_state->arg_count)); 646 647 if (walk_state->arg_count) { 648 /* 649 * There are arguments (complex ones), push Op and 650 * prepare for argument 651 */ 652 status = acpi_ps_push_scope(parser_state, op, 653 walk_state->arg_types, 654 walk_state->arg_count); 655 if (ACPI_FAILURE(status)) { 656 status = 657 acpi_ps_complete_op(walk_state, &op, 658 status); 659 if (ACPI_FAILURE(status)) { 660 return_ACPI_STATUS(status); 661 } 662 663 continue; 664 } 665 666 op = NULL; 667 continue; 668 } 669 670 /* 671 * All arguments have been processed -- Op is complete, 672 * prepare for next 673 */ 674 walk_state->op_info = 675 acpi_ps_get_opcode_info(op->common.aml_opcode); 676 if (walk_state->op_info->flags & AML_NAMED) { 677 if (op->common.aml_opcode == AML_REGION_OP || 678 op->common.aml_opcode == AML_DATA_REGION_OP) { 679 /* 680 * Skip parsing of control method or opregion body, 681 * because we don't have enough info in the first pass 682 * to parse them correctly. 683 * 684 * Completed parsing an op_region declaration, we now 685 * know the length. 686 */ 687 op->named.length = 688 (u32) (parser_state->aml - op->named.data); 689 } 690 } 691 692 if (walk_state->op_info->flags & AML_CREATE) { 693 /* 694 * Backup to beginning of create_XXXfield declaration (1 for 695 * Opcode) 696 * 697 * body_length is unknown until we parse the body 698 */ 699 op->named.length = 700 (u32) (parser_state->aml - op->named.data); 701 } 702 703 if (op->common.aml_opcode == AML_BANK_FIELD_OP) { 704 /* 705 * Backup to beginning of bank_field declaration 706 * 707 * body_length is unknown until we parse the body 708 */ 709 op->named.length = 710 (u32) (parser_state->aml - op->named.data); 711 } 712 713 /* This op complete, notify the dispatcher */ 714 715 if (walk_state->ascending_callback != NULL) { 716 walk_state->op = op; 717 walk_state->opcode = op->common.aml_opcode; 718 719 status = walk_state->ascending_callback(walk_state); 720 status = 721 acpi_ps_next_parse_state(walk_state, op, status); 722 if (status == AE_CTRL_PENDING) { 723 status = AE_OK; 724 } else 725 if ((walk_state-> 726 parse_flags & ACPI_PARSE_MODULE_LEVEL) 727 && (ACPI_AML_EXCEPTION(status) 728 || status == AE_ALREADY_EXISTS 729 || status == AE_NOT_FOUND)) { 730 /* 731 * ACPI_PARSE_MODULE_LEVEL flag means that we 732 * are currently loading a table by executing 733 * it as a control method. However, if we 734 * encounter an error while loading the table, 735 * we need to keep trying to load the table 736 * rather than aborting the table load (setting 737 * the status to AE_OK continues the table 738 * load). If we get a failure at this point, it 739 * means that the dispatcher got an error while 740 * trying to execute the Op. 741 */ 742 status = AE_OK; 743 } 744 } 745 746 status = acpi_ps_complete_op(walk_state, &op, status); 747 if (ACPI_FAILURE(status)) { 748 return_ACPI_STATUS(status); 749 } 750 751 } /* while parser_state->Aml */ 752 753 status = acpi_ps_complete_final_op(walk_state, op, status); 754 return_ACPI_STATUS(status); 755 } 756