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 #if (!defined (ACPI_NO_METHOD_EXECUTION) && !defined (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_OK; 513 } 514 if (status == AE_CTRL_PARSE_CONTINUE) { 515 continue; 516 } 517 518 if (status == AE_CTRL_PARSE_PENDING) { 519 status = AE_OK; 520 } 521 522 if (status == AE_CTRL_TERMINATE) { 523 return_ACPI_STATUS(status); 524 } 525 526 status = 527 acpi_ps_complete_op(walk_state, &op, 528 status); 529 if (ACPI_FAILURE(status)) { 530 return_ACPI_STATUS(status); 531 } 532 if (acpi_ns_opens_scope 533 (acpi_ps_get_opcode_info 534 (walk_state->opcode)->object_type)) { 535 /* 536 * If the scope/device op fails to parse, skip the body of 537 * the scope op because the parse failure indicates that 538 * the device may not exist. 539 */ 540 ACPI_ERROR((AE_INFO, 541 "Skip parsing opcode %s", 542 acpi_ps_get_opcode_name 543 (walk_state->opcode))); 544 545 /* 546 * Determine the opcode length before skipping the opcode. 547 * An opcode can be 1 byte or 2 bytes in length. 548 */ 549 opcode_length = 1; 550 if ((walk_state->opcode & 0xFF00) == 551 AML_EXTENDED_OPCODE) { 552 opcode_length = 2; 553 } 554 walk_state->parser_state.aml = 555 walk_state->aml + opcode_length; 556 557 walk_state->parser_state.aml = 558 acpi_ps_get_next_package_end 559 (&walk_state->parser_state); 560 walk_state->aml = 561 walk_state->parser_state.aml; 562 } 563 564 continue; 565 } 566 567 acpi_ex_start_trace_opcode(op, walk_state); 568 } 569 570 /* 571 * Start arg_count at zero because we don't know if there are 572 * any args yet 573 */ 574 walk_state->arg_count = 0; 575 576 switch (op->common.aml_opcode) { 577 case AML_BYTE_OP: 578 case AML_WORD_OP: 579 case AML_DWORD_OP: 580 case AML_QWORD_OP: 581 582 break; 583 584 default: 585 586 ASL_CV_CAPTURE_COMMENTS(walk_state); 587 break; 588 } 589 590 /* Are there any arguments that must be processed? */ 591 592 if (walk_state->arg_types) { 593 594 /* Get arguments */ 595 596 status = 597 acpi_ps_get_arguments(walk_state, aml_op_start, op); 598 if (ACPI_FAILURE(status)) { 599 status = 600 acpi_ps_complete_op(walk_state, &op, 601 status); 602 if (ACPI_FAILURE(status)) { 603 return_ACPI_STATUS(status); 604 } 605 if ((walk_state->control_state) && 606 ((walk_state->control_state->control. 607 opcode == AML_IF_OP) 608 || (walk_state->control_state->control. 609 opcode == AML_WHILE_OP))) { 610 /* 611 * If the if/while op fails to parse, we will skip parsing 612 * the body of the op. 613 */ 614 parser_state->aml = 615 walk_state->control_state->control. 616 aml_predicate_start + 1; 617 parser_state->aml = 618 acpi_ps_get_next_package_end 619 (parser_state); 620 walk_state->aml = parser_state->aml; 621 622 ACPI_ERROR((AE_INFO, 623 "Skipping While/If block")); 624 if (*walk_state->aml == AML_ELSE_OP) { 625 ACPI_ERROR((AE_INFO, 626 "Skipping Else block")); 627 walk_state->parser_state.aml = 628 walk_state->aml + 1; 629 walk_state->parser_state.aml = 630 acpi_ps_get_next_package_end 631 (parser_state); 632 walk_state->aml = 633 parser_state->aml; 634 } 635 ACPI_FREE(acpi_ut_pop_generic_state 636 (&walk_state->control_state)); 637 } 638 op = NULL; 639 continue; 640 } 641 } 642 643 /* Check for arguments that need to be processed */ 644 645 ACPI_DEBUG_PRINT((ACPI_DB_PARSE, 646 "Parseloop: argument count: %8.8X\n", 647 walk_state->arg_count)); 648 649 if (walk_state->arg_count) { 650 /* 651 * There are arguments (complex ones), push Op and 652 * prepare for argument 653 */ 654 status = acpi_ps_push_scope(parser_state, op, 655 walk_state->arg_types, 656 walk_state->arg_count); 657 if (ACPI_FAILURE(status)) { 658 status = 659 acpi_ps_complete_op(walk_state, &op, 660 status); 661 if (ACPI_FAILURE(status)) { 662 return_ACPI_STATUS(status); 663 } 664 665 continue; 666 } 667 668 op = NULL; 669 continue; 670 } 671 672 /* 673 * All arguments have been processed -- Op is complete, 674 * prepare for next 675 */ 676 walk_state->op_info = 677 acpi_ps_get_opcode_info(op->common.aml_opcode); 678 if (walk_state->op_info->flags & AML_NAMED) { 679 if (op->common.aml_opcode == AML_REGION_OP || 680 op->common.aml_opcode == AML_DATA_REGION_OP) { 681 /* 682 * Skip parsing of control method or opregion body, 683 * because we don't have enough info in the first pass 684 * to parse them correctly. 685 * 686 * Completed parsing an op_region declaration, we now 687 * know the length. 688 */ 689 op->named.length = 690 (u32) (parser_state->aml - op->named.data); 691 } 692 } 693 694 if (walk_state->op_info->flags & AML_CREATE) { 695 /* 696 * Backup to beginning of create_XXXfield declaration (1 for 697 * Opcode) 698 * 699 * body_length is unknown until we parse the body 700 */ 701 op->named.length = 702 (u32) (parser_state->aml - op->named.data); 703 } 704 705 if (op->common.aml_opcode == AML_BANK_FIELD_OP) { 706 /* 707 * Backup to beginning of bank_field declaration 708 * 709 * body_length is unknown until we parse the body 710 */ 711 op->named.length = 712 (u32) (parser_state->aml - op->named.data); 713 } 714 715 /* This op complete, notify the dispatcher */ 716 717 if (walk_state->ascending_callback != NULL) { 718 walk_state->op = op; 719 walk_state->opcode = op->common.aml_opcode; 720 721 status = walk_state->ascending_callback(walk_state); 722 status = 723 acpi_ps_next_parse_state(walk_state, op, status); 724 if (status == AE_CTRL_PENDING) { 725 status = AE_OK; 726 } else 727 if ((walk_state-> 728 parse_flags & ACPI_PARSE_MODULE_LEVEL) 729 && (ACPI_AML_EXCEPTION(status) 730 || status == AE_ALREADY_EXISTS 731 || status == AE_NOT_FOUND)) { 732 /* 733 * ACPI_PARSE_MODULE_LEVEL flag means that we 734 * are currently loading a table by executing 735 * it as a control method. However, if we 736 * encounter an error while loading the table, 737 * we need to keep trying to load the table 738 * rather than aborting the table load (setting 739 * the status to AE_OK continues the table 740 * load). If we get a failure at this point, it 741 * means that the dispatcher got an error while 742 * trying to execute the Op. 743 */ 744 status = AE_OK; 745 } 746 } 747 748 status = acpi_ps_complete_op(walk_state, &op, status); 749 if (ACPI_FAILURE(status)) { 750 return_ACPI_STATUS(status); 751 } 752 753 } /* while parser_state->Aml */ 754 755 status = acpi_ps_complete_final_op(walk_state, op, status); 756 return_ACPI_STATUS(status); 757 } 758