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