1 // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0 2 /****************************************************************************** 3 * 4 * Module Name: psobject - Support for parse objects 5 * 6 * Copyright (C) 2000 - 2018, Intel Corp. 7 * 8 *****************************************************************************/ 9 10 #include <acpi/acpi.h> 11 #include "accommon.h" 12 #include "acparser.h" 13 #include "amlcode.h" 14 #include "acconvert.h" 15 16 #define _COMPONENT ACPI_PARSER 17 ACPI_MODULE_NAME("psobject") 18 19 /* Local prototypes */ 20 static acpi_status acpi_ps_get_aml_opcode(struct acpi_walk_state *walk_state); 21 22 /******************************************************************************* 23 * 24 * FUNCTION: acpi_ps_get_aml_opcode 25 * 26 * PARAMETERS: walk_state - Current state 27 * 28 * RETURN: Status 29 * 30 * DESCRIPTION: Extract the next AML opcode from the input stream. 31 * 32 ******************************************************************************/ 33 34 static acpi_status acpi_ps_get_aml_opcode(struct acpi_walk_state *walk_state) 35 { 36 ACPI_ERROR_ONLY(u32 aml_offset); 37 38 ACPI_FUNCTION_TRACE_PTR(ps_get_aml_opcode, walk_state); 39 40 walk_state->aml = walk_state->parser_state.aml; 41 walk_state->opcode = acpi_ps_peek_opcode(&(walk_state->parser_state)); 42 43 /* 44 * First cut to determine what we have found: 45 * 1) A valid AML opcode 46 * 2) A name string 47 * 3) An unknown/invalid opcode 48 */ 49 walk_state->op_info = acpi_ps_get_opcode_info(walk_state->opcode); 50 51 switch (walk_state->op_info->class) { 52 case AML_CLASS_ASCII: 53 case AML_CLASS_PREFIX: 54 /* 55 * Starts with a valid prefix or ASCII char, this is a name 56 * string. Convert the bare name string to a namepath. 57 */ 58 walk_state->opcode = AML_INT_NAMEPATH_OP; 59 walk_state->arg_types = ARGP_NAMESTRING; 60 break; 61 62 case AML_CLASS_UNKNOWN: 63 64 /* The opcode is unrecognized. Complain and skip unknown opcodes */ 65 66 if (walk_state->pass_number == 2) { 67 ACPI_ERROR_ONLY(aml_offset = 68 (u32)ACPI_PTR_DIFF(walk_state->aml, 69 walk_state-> 70 parser_state. 71 aml_start)); 72 73 ACPI_ERROR((AE_INFO, 74 "Unknown opcode 0x%.2X at table offset 0x%.4X, ignoring", 75 walk_state->opcode, 76 (u32)(aml_offset + 77 sizeof(struct acpi_table_header)))); 78 79 ACPI_DUMP_BUFFER((walk_state->parser_state.aml - 16), 80 48); 81 82 #ifdef ACPI_ASL_COMPILER 83 /* 84 * This is executed for the disassembler only. Output goes 85 * to the disassembled ASL output file. 86 */ 87 acpi_os_printf 88 ("/*\nError: Unknown opcode 0x%.2X at table offset 0x%.4X, context:\n", 89 walk_state->opcode, 90 (u32)(aml_offset + 91 sizeof(struct acpi_table_header))); 92 93 ACPI_ERROR((AE_INFO, 94 "Aborting disassembly, AML byte code is corrupt")); 95 96 /* Dump the context surrounding the invalid opcode */ 97 98 acpi_ut_dump_buffer(((u8 *)walk_state->parser_state. 99 aml - 16), 48, DB_BYTE_DISPLAY, 100 (aml_offset + 101 sizeof(struct acpi_table_header) - 102 16)); 103 acpi_os_printf(" */\n"); 104 105 /* 106 * Just abort the disassembly, cannot continue because the 107 * parser is essentially lost. The disassembler can then 108 * randomly fail because an ill-constructed parse tree 109 * can result. 110 */ 111 return_ACPI_STATUS(AE_AML_BAD_OPCODE); 112 #endif 113 } 114 115 /* Increment past one-byte or two-byte opcode */ 116 117 walk_state->parser_state.aml++; 118 if (walk_state->opcode > 0xFF) { /* Can only happen if first byte is 0x5B */ 119 walk_state->parser_state.aml++; 120 } 121 122 return_ACPI_STATUS(AE_CTRL_PARSE_CONTINUE); 123 124 default: 125 126 /* Found opcode info, this is a normal opcode */ 127 128 walk_state->parser_state.aml += 129 acpi_ps_get_opcode_size(walk_state->opcode); 130 walk_state->arg_types = walk_state->op_info->parse_args; 131 break; 132 } 133 134 return_ACPI_STATUS(AE_OK); 135 } 136 137 /******************************************************************************* 138 * 139 * FUNCTION: acpi_ps_build_named_op 140 * 141 * PARAMETERS: walk_state - Current state 142 * aml_op_start - Begin of named Op in AML 143 * unnamed_op - Early Op (not a named Op) 144 * op - Returned Op 145 * 146 * RETURN: Status 147 * 148 * DESCRIPTION: Parse a named Op 149 * 150 ******************************************************************************/ 151 152 acpi_status 153 acpi_ps_build_named_op(struct acpi_walk_state *walk_state, 154 u8 *aml_op_start, 155 union acpi_parse_object *unnamed_op, 156 union acpi_parse_object **op) 157 { 158 acpi_status status = AE_OK; 159 union acpi_parse_object *arg = NULL; 160 161 ACPI_FUNCTION_TRACE_PTR(ps_build_named_op, walk_state); 162 163 unnamed_op->common.value.arg = NULL; 164 unnamed_op->common.arg_list_length = 0; 165 unnamed_op->common.aml_opcode = walk_state->opcode; 166 167 /* 168 * Get and append arguments until we find the node that contains 169 * the name (the type ARGP_NAME). 170 */ 171 while (GET_CURRENT_ARG_TYPE(walk_state->arg_types) && 172 (GET_CURRENT_ARG_TYPE(walk_state->arg_types) != ARGP_NAME)) { 173 ASL_CV_CAPTURE_COMMENTS(walk_state); 174 status = 175 acpi_ps_get_next_arg(walk_state, 176 &(walk_state->parser_state), 177 GET_CURRENT_ARG_TYPE(walk_state-> 178 arg_types), &arg); 179 if (ACPI_FAILURE(status)) { 180 return_ACPI_STATUS(status); 181 } 182 183 acpi_ps_append_arg(unnamed_op, arg); 184 INCREMENT_ARG_LIST(walk_state->arg_types); 185 } 186 187 /* are there any inline comments associated with the name_seg?? If so, save this. */ 188 189 ASL_CV_CAPTURE_COMMENTS(walk_state); 190 191 #ifdef ACPI_ASL_COMPILER 192 if (acpi_gbl_current_inline_comment != NULL) { 193 unnamed_op->common.name_comment = 194 acpi_gbl_current_inline_comment; 195 acpi_gbl_current_inline_comment = NULL; 196 } 197 #endif 198 199 /* 200 * Make sure that we found a NAME and didn't run out of arguments 201 */ 202 if (!GET_CURRENT_ARG_TYPE(walk_state->arg_types)) { 203 return_ACPI_STATUS(AE_AML_NO_OPERAND); 204 } 205 206 /* We know that this arg is a name, move to next arg */ 207 208 INCREMENT_ARG_LIST(walk_state->arg_types); 209 210 /* 211 * Find the object. This will either insert the object into 212 * the namespace or simply look it up 213 */ 214 walk_state->op = NULL; 215 216 status = walk_state->descending_callback(walk_state, op); 217 if (ACPI_FAILURE(status)) { 218 if (status != AE_CTRL_TERMINATE) { 219 ACPI_EXCEPTION((AE_INFO, status, 220 "During name lookup/catalog")); 221 } 222 return_ACPI_STATUS(status); 223 } 224 225 if (!*op) { 226 return_ACPI_STATUS(AE_CTRL_PARSE_CONTINUE); 227 } 228 229 status = acpi_ps_next_parse_state(walk_state, *op, status); 230 if (ACPI_FAILURE(status)) { 231 if (status == AE_CTRL_PENDING) { 232 status = AE_CTRL_PARSE_PENDING; 233 } 234 return_ACPI_STATUS(status); 235 } 236 237 acpi_ps_append_arg(*op, unnamed_op->common.value.arg); 238 239 #ifdef ACPI_ASL_COMPILER 240 241 /* save any comments that might be associated with unnamed_op. */ 242 243 (*op)->common.inline_comment = unnamed_op->common.inline_comment; 244 (*op)->common.end_node_comment = unnamed_op->common.end_node_comment; 245 (*op)->common.close_brace_comment = 246 unnamed_op->common.close_brace_comment; 247 (*op)->common.name_comment = unnamed_op->common.name_comment; 248 (*op)->common.comment_list = unnamed_op->common.comment_list; 249 (*op)->common.end_blk_comment = unnamed_op->common.end_blk_comment; 250 (*op)->common.cv_filename = unnamed_op->common.cv_filename; 251 (*op)->common.cv_parent_filename = 252 unnamed_op->common.cv_parent_filename; 253 (*op)->named.aml = unnamed_op->common.aml; 254 255 unnamed_op->common.inline_comment = NULL; 256 unnamed_op->common.end_node_comment = NULL; 257 unnamed_op->common.close_brace_comment = NULL; 258 unnamed_op->common.name_comment = NULL; 259 unnamed_op->common.comment_list = NULL; 260 unnamed_op->common.end_blk_comment = NULL; 261 #endif 262 263 if ((*op)->common.aml_opcode == AML_REGION_OP || 264 (*op)->common.aml_opcode == AML_DATA_REGION_OP) { 265 /* 266 * Defer final parsing of an operation_region body, because we don't 267 * have enough info in the first pass to parse it correctly (i.e., 268 * there may be method calls within the term_arg elements of the body.) 269 * 270 * However, we must continue parsing because the opregion is not a 271 * standalone package -- we don't know where the end is at this point. 272 * 273 * (Length is unknown until parse of the body complete) 274 */ 275 (*op)->named.data = aml_op_start; 276 (*op)->named.length = 0; 277 } 278 279 return_ACPI_STATUS(AE_OK); 280 } 281 282 /******************************************************************************* 283 * 284 * FUNCTION: acpi_ps_create_op 285 * 286 * PARAMETERS: walk_state - Current state 287 * aml_op_start - Op start in AML 288 * new_op - Returned Op 289 * 290 * RETURN: Status 291 * 292 * DESCRIPTION: Get Op from AML 293 * 294 ******************************************************************************/ 295 296 acpi_status 297 acpi_ps_create_op(struct acpi_walk_state *walk_state, 298 u8 *aml_op_start, union acpi_parse_object **new_op) 299 { 300 acpi_status status = AE_OK; 301 union acpi_parse_object *op; 302 union acpi_parse_object *named_op = NULL; 303 union acpi_parse_object *parent_scope; 304 u8 argument_count; 305 const struct acpi_opcode_info *op_info; 306 307 ACPI_FUNCTION_TRACE_PTR(ps_create_op, walk_state); 308 309 status = acpi_ps_get_aml_opcode(walk_state); 310 if (status == AE_CTRL_PARSE_CONTINUE) { 311 return_ACPI_STATUS(AE_CTRL_PARSE_CONTINUE); 312 } 313 if (ACPI_FAILURE(status)) { 314 return_ACPI_STATUS(status); 315 } 316 317 /* Create Op structure and append to parent's argument list */ 318 319 walk_state->op_info = acpi_ps_get_opcode_info(walk_state->opcode); 320 op = acpi_ps_alloc_op(walk_state->opcode, aml_op_start); 321 if (!op) { 322 return_ACPI_STATUS(AE_NO_MEMORY); 323 } 324 325 if (walk_state->op_info->flags & AML_NAMED) { 326 status = 327 acpi_ps_build_named_op(walk_state, aml_op_start, op, 328 &named_op); 329 acpi_ps_free_op(op); 330 331 #ifdef ACPI_ASL_COMPILER 332 if (acpi_gbl_disasm_flag 333 && walk_state->opcode == AML_EXTERNAL_OP 334 && status == AE_NOT_FOUND) { 335 /* 336 * If parsing of AML_EXTERNAL_OP's name path fails, then skip 337 * past this opcode and keep parsing. This is a much better 338 * alternative than to abort the entire disassembler. At this 339 * point, the parser_state is at the end of the namepath of the 340 * external declaration opcode. Setting walk_state->Aml to 341 * walk_state->parser_state.Aml + 2 moves increments the 342 * walk_state->Aml past the object type and the paramcount of the 343 * external opcode. 344 */ 345 walk_state->aml = walk_state->parser_state.aml + 2; 346 walk_state->parser_state.aml = walk_state->aml; 347 return_ACPI_STATUS(AE_CTRL_PARSE_CONTINUE); 348 } 349 #endif 350 if (ACPI_FAILURE(status)) { 351 return_ACPI_STATUS(status); 352 } 353 354 *new_op = named_op; 355 return_ACPI_STATUS(AE_OK); 356 } 357 358 /* Not a named opcode, just allocate Op and append to parent */ 359 360 if (walk_state->op_info->flags & AML_CREATE) { 361 /* 362 * Backup to beginning of create_XXXfield declaration 363 * body_length is unknown until we parse the body 364 */ 365 op->named.data = aml_op_start; 366 op->named.length = 0; 367 } 368 369 if (walk_state->opcode == AML_BANK_FIELD_OP) { 370 /* 371 * Backup to beginning of bank_field declaration 372 * body_length is unknown until we parse the body 373 */ 374 op->named.data = aml_op_start; 375 op->named.length = 0; 376 } 377 378 parent_scope = acpi_ps_get_parent_scope(&(walk_state->parser_state)); 379 acpi_ps_append_arg(parent_scope, op); 380 381 if (parent_scope) { 382 op_info = 383 acpi_ps_get_opcode_info(parent_scope->common.aml_opcode); 384 if (op_info->flags & AML_HAS_TARGET) { 385 argument_count = 386 acpi_ps_get_argument_count(op_info->type); 387 if (parent_scope->common.arg_list_length > 388 argument_count) { 389 op->common.flags |= ACPI_PARSEOP_TARGET; 390 } 391 } 392 393 /* 394 * Special case for both Increment() and Decrement(), where 395 * the lone argument is both a source and a target. 396 */ 397 else if ((parent_scope->common.aml_opcode == AML_INCREMENT_OP) 398 || (parent_scope->common.aml_opcode == 399 AML_DECREMENT_OP)) { 400 op->common.flags |= ACPI_PARSEOP_TARGET; 401 } 402 } 403 404 if (walk_state->descending_callback != NULL) { 405 /* 406 * Find the object. This will either insert the object into 407 * the namespace or simply look it up 408 */ 409 walk_state->op = *new_op = op; 410 411 status = walk_state->descending_callback(walk_state, &op); 412 status = acpi_ps_next_parse_state(walk_state, op, status); 413 if (status == AE_CTRL_PENDING) { 414 status = AE_CTRL_PARSE_PENDING; 415 } 416 } 417 418 return_ACPI_STATUS(status); 419 } 420 421 /******************************************************************************* 422 * 423 * FUNCTION: acpi_ps_complete_op 424 * 425 * PARAMETERS: walk_state - Current state 426 * op - Returned Op 427 * status - Parse status before complete Op 428 * 429 * RETURN: Status 430 * 431 * DESCRIPTION: Complete Op 432 * 433 ******************************************************************************/ 434 435 acpi_status 436 acpi_ps_complete_op(struct acpi_walk_state *walk_state, 437 union acpi_parse_object **op, acpi_status status) 438 { 439 acpi_status status2; 440 441 ACPI_FUNCTION_TRACE_PTR(ps_complete_op, walk_state); 442 443 /* 444 * Finished one argument of the containing scope 445 */ 446 walk_state->parser_state.scope->parse_scope.arg_count--; 447 448 /* Close this Op (will result in parse subtree deletion) */ 449 450 status2 = acpi_ps_complete_this_op(walk_state, *op); 451 if (ACPI_FAILURE(status2)) { 452 return_ACPI_STATUS(status2); 453 } 454 455 *op = NULL; 456 457 switch (status) { 458 case AE_OK: 459 460 break; 461 462 case AE_CTRL_TRANSFER: 463 464 /* We are about to transfer to a called method */ 465 466 walk_state->prev_op = NULL; 467 walk_state->prev_arg_types = walk_state->arg_types; 468 return_ACPI_STATUS(status); 469 470 case AE_CTRL_END: 471 472 acpi_ps_pop_scope(&(walk_state->parser_state), op, 473 &walk_state->arg_types, 474 &walk_state->arg_count); 475 476 if (*op) { 477 walk_state->op = *op; 478 walk_state->op_info = 479 acpi_ps_get_opcode_info((*op)->common.aml_opcode); 480 walk_state->opcode = (*op)->common.aml_opcode; 481 482 status = walk_state->ascending_callback(walk_state); 483 status = 484 acpi_ps_next_parse_state(walk_state, *op, status); 485 486 status2 = acpi_ps_complete_this_op(walk_state, *op); 487 if (ACPI_FAILURE(status2)) { 488 return_ACPI_STATUS(status2); 489 } 490 } 491 492 status = AE_OK; 493 break; 494 495 case AE_CTRL_BREAK: 496 case AE_CTRL_CONTINUE: 497 498 /* Pop off scopes until we find the While */ 499 500 while (!(*op) || ((*op)->common.aml_opcode != AML_WHILE_OP)) { 501 acpi_ps_pop_scope(&(walk_state->parser_state), op, 502 &walk_state->arg_types, 503 &walk_state->arg_count); 504 } 505 506 /* Close this iteration of the While loop */ 507 508 walk_state->op = *op; 509 walk_state->op_info = 510 acpi_ps_get_opcode_info((*op)->common.aml_opcode); 511 walk_state->opcode = (*op)->common.aml_opcode; 512 513 status = walk_state->ascending_callback(walk_state); 514 status = acpi_ps_next_parse_state(walk_state, *op, status); 515 516 status2 = acpi_ps_complete_this_op(walk_state, *op); 517 if (ACPI_FAILURE(status2)) { 518 return_ACPI_STATUS(status2); 519 } 520 521 status = AE_OK; 522 break; 523 524 case AE_CTRL_TERMINATE: 525 526 /* Clean up */ 527 do { 528 if (*op) { 529 status2 = 530 acpi_ps_complete_this_op(walk_state, *op); 531 if (ACPI_FAILURE(status2)) { 532 return_ACPI_STATUS(status2); 533 } 534 535 acpi_ut_delete_generic_state 536 (acpi_ut_pop_generic_state 537 (&walk_state->control_state)); 538 } 539 540 acpi_ps_pop_scope(&(walk_state->parser_state), op, 541 &walk_state->arg_types, 542 &walk_state->arg_count); 543 544 } while (*op); 545 546 return_ACPI_STATUS(AE_OK); 547 548 default: /* All other non-AE_OK status */ 549 550 do { 551 if (*op) { 552 status2 = 553 acpi_ps_complete_this_op(walk_state, *op); 554 if (ACPI_FAILURE(status2)) { 555 return_ACPI_STATUS(status2); 556 } 557 } 558 559 acpi_ps_pop_scope(&(walk_state->parser_state), op, 560 &walk_state->arg_types, 561 &walk_state->arg_count); 562 563 } while (*op); 564 565 #if 0 566 /* 567 * TBD: Cleanup parse ops on error 568 */ 569 if (*op == NULL) { 570 acpi_ps_pop_scope(parser_state, op, 571 &walk_state->arg_types, 572 &walk_state->arg_count); 573 } 574 #endif 575 walk_state->prev_op = NULL; 576 walk_state->prev_arg_types = walk_state->arg_types; 577 return_ACPI_STATUS(status); 578 } 579 580 /* This scope complete? */ 581 582 if (acpi_ps_has_completed_scope(&(walk_state->parser_state))) { 583 acpi_ps_pop_scope(&(walk_state->parser_state), op, 584 &walk_state->arg_types, 585 &walk_state->arg_count); 586 ACPI_DEBUG_PRINT((ACPI_DB_PARSE, "Popped scope, Op=%p\n", *op)); 587 } else { 588 *op = NULL; 589 } 590 591 return_ACPI_STATUS(AE_OK); 592 } 593 594 /******************************************************************************* 595 * 596 * FUNCTION: acpi_ps_complete_final_op 597 * 598 * PARAMETERS: walk_state - Current state 599 * op - Current Op 600 * status - Current parse status before complete last 601 * Op 602 * 603 * RETURN: Status 604 * 605 * DESCRIPTION: Complete last Op. 606 * 607 ******************************************************************************/ 608 609 acpi_status 610 acpi_ps_complete_final_op(struct acpi_walk_state *walk_state, 611 union acpi_parse_object *op, acpi_status status) 612 { 613 acpi_status status2; 614 615 ACPI_FUNCTION_TRACE_PTR(ps_complete_final_op, walk_state); 616 617 /* 618 * Complete the last Op (if not completed), and clear the scope stack. 619 * It is easily possible to end an AML "package" with an unbounded number 620 * of open scopes (such as when several ASL blocks are closed with 621 * sequential closing braces). We want to terminate each one cleanly. 622 */ 623 ACPI_DEBUG_PRINT((ACPI_DB_PARSE, "AML package complete at Op %p\n", 624 op)); 625 do { 626 if (op) { 627 if (walk_state->ascending_callback != NULL) { 628 walk_state->op = op; 629 walk_state->op_info = 630 acpi_ps_get_opcode_info(op->common. 631 aml_opcode); 632 walk_state->opcode = op->common.aml_opcode; 633 634 status = 635 walk_state->ascending_callback(walk_state); 636 status = 637 acpi_ps_next_parse_state(walk_state, op, 638 status); 639 if (status == AE_CTRL_PENDING) { 640 status = 641 acpi_ps_complete_op(walk_state, &op, 642 AE_OK); 643 if (ACPI_FAILURE(status)) { 644 return_ACPI_STATUS(status); 645 } 646 } 647 648 if (status == AE_CTRL_TERMINATE) { 649 status = AE_OK; 650 651 /* Clean up */ 652 do { 653 if (op) { 654 status2 = 655 acpi_ps_complete_this_op 656 (walk_state, op); 657 if (ACPI_FAILURE 658 (status2)) { 659 return_ACPI_STATUS 660 (status2); 661 } 662 } 663 664 acpi_ps_pop_scope(& 665 (walk_state-> 666 parser_state), 667 &op, 668 &walk_state-> 669 arg_types, 670 &walk_state-> 671 arg_count); 672 673 } while (op); 674 675 return_ACPI_STATUS(status); 676 } 677 678 else if (ACPI_FAILURE(status)) { 679 680 /* First error is most important */ 681 682 (void) 683 acpi_ps_complete_this_op(walk_state, 684 op); 685 return_ACPI_STATUS(status); 686 } 687 } 688 689 status2 = acpi_ps_complete_this_op(walk_state, op); 690 if (ACPI_FAILURE(status2)) { 691 return_ACPI_STATUS(status2); 692 } 693 } 694 695 acpi_ps_pop_scope(&(walk_state->parser_state), &op, 696 &walk_state->arg_types, 697 &walk_state->arg_count); 698 699 } while (op); 700 701 return_ACPI_STATUS(status); 702 } 703