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