1/****************************************************************************** 2 * 3 * Module Name: psloop - Main AML parse loop 4 * 5 *****************************************************************************/ 6 7/* 8 * Copyright (C) 2000 - 2010, 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/* 45 * Parse the AML and build an operation tree as most interpreters, (such as 46 * Perl) do. Parsing is done by hand rather than with a YACC generated parser 47 * to tightly constrain stack and dynamic memory usage. Parsing is kept 48 * flexible and the code fairly compact by parsing based on a list of AML 49 * opcode templates in aml_op_info[]. 50 */ 51 52#include <acpi/acpi.h> 53#include "accommon.h" 54#include "acparser.h" 55#include "acdispat.h" 56#include "amlcode.h" 57 58#define _COMPONENT ACPI_PARSER 59ACPI_MODULE_NAME("psloop") 60 61static u32 acpi_gbl_depth = 0; 62 63/* Local prototypes */ 64 65static acpi_status acpi_ps_get_aml_opcode(struct acpi_walk_state *walk_state); 66 67static acpi_status 68acpi_ps_build_named_op(struct acpi_walk_state *walk_state, 69 u8 * aml_op_start, 70 union acpi_parse_object *unnamed_op, 71 union acpi_parse_object **op); 72 73static acpi_status 74acpi_ps_create_op(struct acpi_walk_state *walk_state, 75 u8 * aml_op_start, union acpi_parse_object **new_op); 76 77static acpi_status 78acpi_ps_get_arguments(struct acpi_walk_state *walk_state, 79 u8 * aml_op_start, union acpi_parse_object *op); 80 81static acpi_status 82acpi_ps_complete_op(struct acpi_walk_state *walk_state, 83 union acpi_parse_object **op, acpi_status status); 84 85static acpi_status 86acpi_ps_complete_final_op(struct acpi_walk_state *walk_state, 87 union acpi_parse_object *op, acpi_status status); 88 89static void 90acpi_ps_link_module_code(union acpi_parse_object *parent_op, 91 u8 *aml_start, u32 aml_length, acpi_owner_id owner_id); 92 93/******************************************************************************* 94 * 95 * FUNCTION: acpi_ps_get_aml_opcode 96 * 97 * PARAMETERS: walk_state - Current state 98 * 99 * RETURN: Status 100 * 101 * DESCRIPTION: Extract the next AML opcode from the input stream. 102 * 103 ******************************************************************************/ 104 105static acpi_status acpi_ps_get_aml_opcode(struct acpi_walk_state *walk_state) 106{ 107 108 ACPI_FUNCTION_TRACE_PTR(ps_get_aml_opcode, walk_state); 109 110 walk_state->aml_offset = 111 (u32) ACPI_PTR_DIFF(walk_state->parser_state.aml, 112 walk_state->parser_state.aml_start); 113 walk_state->opcode = acpi_ps_peek_opcode(&(walk_state->parser_state)); 114 115 /* 116 * First cut to determine what we have found: 117 * 1) A valid AML opcode 118 * 2) A name string 119 * 3) An unknown/invalid opcode 120 */ 121 walk_state->op_info = acpi_ps_get_opcode_info(walk_state->opcode); 122 123 switch (walk_state->op_info->class) { 124 case AML_CLASS_ASCII: 125 case AML_CLASS_PREFIX: 126 /* 127 * Starts with a valid prefix or ASCII char, this is a name 128 * string. Convert the bare name string to a namepath. 129 */ 130 walk_state->opcode = AML_INT_NAMEPATH_OP; 131 walk_state->arg_types = ARGP_NAMESTRING; 132 break; 133 134 case AML_CLASS_UNKNOWN: 135 136 /* The opcode is unrecognized. Just skip unknown opcodes */ 137 138 ACPI_ERROR((AE_INFO, 139 "Found unknown opcode 0x%X at AML address %p offset 0x%X, ignoring", 140 walk_state->opcode, walk_state->parser_state.aml, 141 walk_state->aml_offset)); 142 143 ACPI_DUMP_BUFFER(walk_state->parser_state.aml, 128); 144 145 /* Assume one-byte bad opcode */ 146 147 walk_state->parser_state.aml++; 148 return_ACPI_STATUS(AE_CTRL_PARSE_CONTINUE); 149 150 default: 151 152 /* Found opcode info, this is a normal opcode */ 153 154 walk_state->parser_state.aml += 155 acpi_ps_get_opcode_size(walk_state->opcode); 156 walk_state->arg_types = walk_state->op_info->parse_args; 157 break; 158 } 159 160 return_ACPI_STATUS(AE_OK); 161} 162 163/******************************************************************************* 164 * 165 * FUNCTION: acpi_ps_build_named_op 166 * 167 * PARAMETERS: walk_state - Current state 168 * aml_op_start - Begin of named Op in AML 169 * unnamed_op - Early Op (not a named Op) 170 * Op - Returned Op 171 * 172 * RETURN: Status 173 * 174 * DESCRIPTION: Parse a named Op 175 * 176 ******************************************************************************/ 177 178static acpi_status 179acpi_ps_build_named_op(struct acpi_walk_state *walk_state, 180 u8 * aml_op_start, 181 union acpi_parse_object *unnamed_op, 182 union acpi_parse_object **op) 183{ 184 acpi_status status = AE_OK; 185 union acpi_parse_object *arg = NULL; 186 187 ACPI_FUNCTION_TRACE_PTR(ps_build_named_op, walk_state); 188 189 unnamed_op->common.value.arg = NULL; 190 unnamed_op->common.arg_list_length = 0; 191 unnamed_op->common.aml_opcode = walk_state->opcode; 192 193 /* 194 * Get and append arguments until we find the node that contains 195 * the name (the type ARGP_NAME). 196 */ 197 while (GET_CURRENT_ARG_TYPE(walk_state->arg_types) && 198 (GET_CURRENT_ARG_TYPE(walk_state->arg_types) != ARGP_NAME)) { 199 status = 200 acpi_ps_get_next_arg(walk_state, 201 &(walk_state->parser_state), 202 GET_CURRENT_ARG_TYPE(walk_state-> 203 arg_types), &arg); 204 if (ACPI_FAILURE(status)) { 205 return_ACPI_STATUS(status); 206 } 207 208 acpi_ps_append_arg(unnamed_op, arg); 209 INCREMENT_ARG_LIST(walk_state->arg_types); 210 } 211 212 /* 213 * Make sure that we found a NAME and didn't run out of arguments 214 */ 215 if (!GET_CURRENT_ARG_TYPE(walk_state->arg_types)) { 216 return_ACPI_STATUS(AE_AML_NO_OPERAND); 217 } 218 219 /* We know that this arg is a name, move to next arg */ 220 221 INCREMENT_ARG_LIST(walk_state->arg_types); 222 223 /* 224 * Find the object. This will either insert the object into 225 * the namespace or simply look it up 226 */ 227 walk_state->op = NULL; 228 229 status = walk_state->descending_callback(walk_state, op); 230 if (ACPI_FAILURE(status)) { 231 ACPI_EXCEPTION((AE_INFO, status, "During name lookup/catalog")); 232 return_ACPI_STATUS(status); 233 } 234 235 if (!*op) { 236 return_ACPI_STATUS(AE_CTRL_PARSE_CONTINUE); 237 } 238 239 status = acpi_ps_next_parse_state(walk_state, *op, status); 240 if (ACPI_FAILURE(status)) { 241 if (status == AE_CTRL_PENDING) { 242 return_ACPI_STATUS(AE_CTRL_PARSE_PENDING); 243 } 244 return_ACPI_STATUS(status); 245 } 246 247 acpi_ps_append_arg(*op, unnamed_op->common.value.arg); 248 acpi_gbl_depth++; 249 250 if ((*op)->common.aml_opcode == AML_REGION_OP || 251 (*op)->common.aml_opcode == AML_DATA_REGION_OP) { 252 /* 253 * Defer final parsing of an operation_region body, because we don't 254 * have enough info in the first pass to parse it correctly (i.e., 255 * there may be method calls within the term_arg elements of the body.) 256 * 257 * However, we must continue parsing because the opregion is not a 258 * standalone package -- we don't know where the end is at this point. 259 * 260 * (Length is unknown until parse of the body complete) 261 */ 262 (*op)->named.data = aml_op_start; 263 (*op)->named.length = 0; 264 } 265 266 return_ACPI_STATUS(AE_OK); 267} 268 269/******************************************************************************* 270 * 271 * FUNCTION: acpi_ps_create_op 272 * 273 * PARAMETERS: walk_state - Current state 274 * aml_op_start - Op start in AML 275 * new_op - Returned Op 276 * 277 * RETURN: Status 278 * 279 * DESCRIPTION: Get Op from AML 280 * 281 ******************************************************************************/ 282 283static acpi_status 284acpi_ps_create_op(struct acpi_walk_state *walk_state, 285 u8 * aml_op_start, union acpi_parse_object **new_op) 286{ 287 acpi_status status = AE_OK; 288 union acpi_parse_object *op; 289 union acpi_parse_object *named_op = NULL; 290 union acpi_parse_object *parent_scope; 291 u8 argument_count; 292 const struct acpi_opcode_info *op_info; 293 294 ACPI_FUNCTION_TRACE_PTR(ps_create_op, walk_state); 295 296 status = acpi_ps_get_aml_opcode(walk_state); 297 if (status == AE_CTRL_PARSE_CONTINUE) { 298 return_ACPI_STATUS(AE_CTRL_PARSE_CONTINUE); 299 } 300 301 /* Create Op structure and append to parent's argument list */ 302 303 walk_state->op_info = acpi_ps_get_opcode_info(walk_state->opcode); 304 op = acpi_ps_alloc_op(walk_state->opcode); 305 if (!op) { 306 return_ACPI_STATUS(AE_NO_MEMORY); 307 } 308 309 if (walk_state->op_info->flags & AML_NAMED) { 310 status = 311 acpi_ps_build_named_op(walk_state, aml_op_start, op, 312 &named_op); 313 acpi_ps_free_op(op); 314 if (ACPI_FAILURE(status)) { 315 return_ACPI_STATUS(status); 316 } 317 318 *new_op = named_op; 319 return_ACPI_STATUS(AE_OK); 320 } 321 322 /* Not a named opcode, just allocate Op and append to parent */ 323 324 if (walk_state->op_info->flags & AML_CREATE) { 325 /* 326 * Backup to beginning of create_xXXfield declaration 327 * body_length is unknown until we parse the body 328 */ 329 op->named.data = aml_op_start; 330 op->named.length = 0; 331 } 332 333 if (walk_state->opcode == AML_BANK_FIELD_OP) { 334 /* 335 * Backup to beginning of bank_field declaration 336 * body_length is unknown until we parse the body 337 */ 338 op->named.data = aml_op_start; 339 op->named.length = 0; 340 } 341 342 parent_scope = acpi_ps_get_parent_scope(&(walk_state->parser_state)); 343 acpi_ps_append_arg(parent_scope, op); 344 345 if (parent_scope) { 346 op_info = 347 acpi_ps_get_opcode_info(parent_scope->common.aml_opcode); 348 if (op_info->flags & AML_HAS_TARGET) { 349 argument_count = 350 acpi_ps_get_argument_count(op_info->type); 351 if (parent_scope->common.arg_list_length > 352 argument_count) { 353 op->common.flags |= ACPI_PARSEOP_TARGET; 354 } 355 } else if (parent_scope->common.aml_opcode == AML_INCREMENT_OP) { 356 op->common.flags |= ACPI_PARSEOP_TARGET; 357 } 358 } 359 360 if (walk_state->descending_callback != NULL) { 361 /* 362 * Find the object. This will either insert the object into 363 * the namespace or simply look it up 364 */ 365 walk_state->op = *new_op = op; 366 367 status = walk_state->descending_callback(walk_state, &op); 368 status = acpi_ps_next_parse_state(walk_state, op, status); 369 if (status == AE_CTRL_PENDING) { 370 status = AE_CTRL_PARSE_PENDING; 371 } 372 } 373 374 return_ACPI_STATUS(status); 375} 376 377/******************************************************************************* 378 * 379 * FUNCTION: acpi_ps_get_arguments 380 * 381 * PARAMETERS: walk_state - Current state 382 * aml_op_start - Op start in AML 383 * Op - Current Op 384 * 385 * RETURN: Status 386 * 387 * DESCRIPTION: Get arguments for passed Op. 388 * 389 ******************************************************************************/ 390 391static acpi_status 392acpi_ps_get_arguments(struct acpi_walk_state *walk_state, 393 u8 * aml_op_start, union acpi_parse_object *op) 394{ 395 acpi_status status = AE_OK; 396 union acpi_parse_object *arg = NULL; 397 const struct acpi_opcode_info *op_info; 398 399 ACPI_FUNCTION_TRACE_PTR(ps_get_arguments, walk_state); 400 401 switch (op->common.aml_opcode) { 402 case AML_BYTE_OP: /* AML_BYTEDATA_ARG */ 403 case AML_WORD_OP: /* AML_WORDDATA_ARG */ 404 case AML_DWORD_OP: /* AML_DWORDATA_ARG */ 405 case AML_QWORD_OP: /* AML_QWORDATA_ARG */ 406 case AML_STRING_OP: /* AML_ASCIICHARLIST_ARG */ 407 408 /* Fill in constant or string argument directly */ 409 410 acpi_ps_get_next_simple_arg(&(walk_state->parser_state), 411 GET_CURRENT_ARG_TYPE(walk_state-> 412 arg_types), 413 op); 414 break; 415 416 case AML_INT_NAMEPATH_OP: /* AML_NAMESTRING_ARG */ 417 418 status = 419 acpi_ps_get_next_namepath(walk_state, 420 &(walk_state->parser_state), op, 421 1); 422 if (ACPI_FAILURE(status)) { 423 return_ACPI_STATUS(status); 424 } 425 426 walk_state->arg_types = 0; 427 break; 428 429 default: 430 /* 431 * Op is not a constant or string, append each argument to the Op 432 */ 433 while (GET_CURRENT_ARG_TYPE(walk_state->arg_types) 434 && !walk_state->arg_count) { 435 walk_state->aml_offset = 436 (u32) ACPI_PTR_DIFF(walk_state->parser_state.aml, 437 walk_state->parser_state. 438 aml_start); 439 440 status = 441 acpi_ps_get_next_arg(walk_state, 442 &(walk_state->parser_state), 443 GET_CURRENT_ARG_TYPE 444 (walk_state->arg_types), &arg); 445 if (ACPI_FAILURE(status)) { 446 return_ACPI_STATUS(status); 447 } 448 449 if (arg) { 450 arg->common.aml_offset = walk_state->aml_offset; 451 acpi_ps_append_arg(op, arg); 452 } 453 454 INCREMENT_ARG_LIST(walk_state->arg_types); 455 } 456 457 /* 458 * Handle executable code at "module-level". This refers to 459 * executable opcodes that appear outside of any control method. 460 */ 461 if ((walk_state->pass_number <= ACPI_IMODE_LOAD_PASS2) && 462 ((walk_state->parse_flags & ACPI_PARSE_DISASSEMBLE) == 0)) { 463 /* 464 * We want to skip If/Else/While constructs during Pass1 because we 465 * want to actually conditionally execute the code during Pass2. 466 * 467 * Except for disassembly, where we always want to walk the 468 * If/Else/While packages 469 */ 470 switch (op->common.aml_opcode) { 471 case AML_IF_OP: 472 case AML_ELSE_OP: 473 case AML_WHILE_OP: 474 475 /* 476 * Currently supported module-level opcodes are: 477 * IF/ELSE/WHILE. These appear to be the most common, 478 * and easiest to support since they open an AML 479 * package. 480 */ 481 if (walk_state->pass_number == 482 ACPI_IMODE_LOAD_PASS1) { 483 acpi_ps_link_module_code(op->common. 484 parent, 485 aml_op_start, 486 (u32) 487 (walk_state-> 488 parser_state. 489 pkg_end - 490 aml_op_start), 491 walk_state-> 492 owner_id); 493 } 494 495 ACPI_DEBUG_PRINT((ACPI_DB_PARSE, 496 "Pass1: Skipping an If/Else/While body\n")); 497 498 /* Skip body of if/else/while in pass 1 */ 499 500 walk_state->parser_state.aml = 501 walk_state->parser_state.pkg_end; 502 walk_state->arg_count = 0; 503 break; 504 505 default: 506 /* 507 * Check for an unsupported executable opcode at module 508 * level. We must be in PASS1, the parent must be a SCOPE, 509 * The opcode class must be EXECUTE, and the opcode must 510 * not be an argument to another opcode. 511 */ 512 if ((walk_state->pass_number == 513 ACPI_IMODE_LOAD_PASS1) 514 && (op->common.parent->common.aml_opcode == 515 AML_SCOPE_OP)) { 516 op_info = 517 acpi_ps_get_opcode_info(op->common. 518 aml_opcode); 519 if ((op_info->class == 520 AML_CLASS_EXECUTE) && (!arg)) { 521 ACPI_WARNING((AE_INFO, 522 "Detected an unsupported executable opcode " 523 "at module-level: [0x%.4X] at table offset 0x%.4X", 524 op->common.aml_opcode, 525 (u32)((aml_op_start - walk_state->parser_state.aml_start) 526 + sizeof(struct acpi_table_header)))); 527 } 528 } 529 break; 530 } 531 } 532 533 /* Special processing for certain opcodes */ 534 535 switch (op->common.aml_opcode) { 536 case AML_METHOD_OP: 537 /* 538 * Skip parsing of control method because we don't have enough 539 * info in the first pass to parse it correctly. 540 * 541 * Save the length and address of the body 542 */ 543 op->named.data = walk_state->parser_state.aml; 544 op->named.length = (u32) 545 (walk_state->parser_state.pkg_end - 546 walk_state->parser_state.aml); 547 548 /* Skip body of method */ 549 550 walk_state->parser_state.aml = 551 walk_state->parser_state.pkg_end; 552 walk_state->arg_count = 0; 553 break; 554 555 case AML_BUFFER_OP: 556 case AML_PACKAGE_OP: 557 case AML_VAR_PACKAGE_OP: 558 559 if ((op->common.parent) && 560 (op->common.parent->common.aml_opcode == 561 AML_NAME_OP) 562 && (walk_state->pass_number <= 563 ACPI_IMODE_LOAD_PASS2)) { 564 /* 565 * Skip parsing of Buffers and Packages because we don't have 566 * enough info in the first pass to parse them correctly. 567 */ 568 op->named.data = aml_op_start; 569 op->named.length = (u32) 570 (walk_state->parser_state.pkg_end - 571 aml_op_start); 572 573 /* Skip body */ 574 575 walk_state->parser_state.aml = 576 walk_state->parser_state.pkg_end; 577 walk_state->arg_count = 0; 578 } 579 break; 580 581 case AML_WHILE_OP: 582 583 if (walk_state->control_state) { 584 walk_state->control_state->control.package_end = 585 walk_state->parser_state.pkg_end; 586 } 587 break; 588 589 default: 590 591 /* No action for all other opcodes */ 592 break; 593 } 594 595 break; 596 } 597 598 return_ACPI_STATUS(AE_OK); 599} 600 601/******************************************************************************* 602 * 603 * FUNCTION: acpi_ps_link_module_code 604 * 605 * PARAMETERS: parent_op - Parent parser op 606 * aml_start - Pointer to the AML 607 * aml_length - Length of executable AML 608 * owner_id - owner_id of module level code 609 * 610 * RETURN: None. 611 * 612 * DESCRIPTION: Wrap the module-level code with a method object and link the 613 * object to the global list. Note, the mutex field of the method 614 * object is used to link multiple module-level code objects. 615 * 616 ******************************************************************************/ 617 618static void 619acpi_ps_link_module_code(union acpi_parse_object *parent_op, 620 u8 *aml_start, u32 aml_length, acpi_owner_id owner_id) 621{ 622 union acpi_operand_object *prev; 623 union acpi_operand_object *next; 624 union acpi_operand_object *method_obj; 625 struct acpi_namespace_node *parent_node; 626 627 /* Get the tail of the list */ 628 629 prev = next = acpi_gbl_module_code_list; 630 while (next) { 631 prev = next; 632 next = next->method.mutex; 633 } 634 635 /* 636 * Insert the module level code into the list. Merge it if it is 637 * adjacent to the previous element. 638 */ 639 if (!prev || 640 ((prev->method.aml_start + prev->method.aml_length) != aml_start)) { 641 642 /* Create, initialize, and link a new temporary method object */ 643 644 method_obj = acpi_ut_create_internal_object(ACPI_TYPE_METHOD); 645 if (!method_obj) { 646 return; 647 } 648 649 if (parent_op->common.node) { 650 parent_node = parent_op->common.node; 651 } else { 652 parent_node = acpi_gbl_root_node; 653 } 654 655 method_obj->method.aml_start = aml_start; 656 method_obj->method.aml_length = aml_length; 657 method_obj->method.owner_id = owner_id; 658 method_obj->method.flags |= AOPOBJ_MODULE_LEVEL; 659 660 /* 661 * Save the parent node in next_object. This is cheating, but we 662 * don't want to expand the method object. 663 */ 664 method_obj->method.next_object = 665 ACPI_CAST_PTR(union acpi_operand_object, parent_node); 666 667 if (!prev) { 668 acpi_gbl_module_code_list = method_obj; 669 } else { 670 prev->method.mutex = method_obj; 671 } 672 } else { 673 prev->method.aml_length += aml_length; 674 } 675} 676 677/******************************************************************************* 678 * 679 * FUNCTION: acpi_ps_complete_op 680 * 681 * PARAMETERS: walk_state - Current state 682 * Op - Returned Op 683 * Status - Parse status before complete Op 684 * 685 * RETURN: Status 686 * 687 * DESCRIPTION: Complete Op 688 * 689 ******************************************************************************/ 690 691static acpi_status 692acpi_ps_complete_op(struct acpi_walk_state *walk_state, 693 union acpi_parse_object **op, acpi_status status) 694{ 695 acpi_status status2; 696 697 ACPI_FUNCTION_TRACE_PTR(ps_complete_op, walk_state); 698 699 /* 700 * Finished one argument of the containing scope 701 */ 702 walk_state->parser_state.scope->parse_scope.arg_count--; 703 704 /* Close this Op (will result in parse subtree deletion) */ 705 706 status2 = acpi_ps_complete_this_op(walk_state, *op); 707 if (ACPI_FAILURE(status2)) { 708 return_ACPI_STATUS(status2); 709 } 710 711 *op = NULL; 712 713 switch (status) { 714 case AE_OK: 715 break; 716 717 case AE_CTRL_TRANSFER: 718 719 /* We are about to transfer to a called method */ 720 721 walk_state->prev_op = NULL; 722 walk_state->prev_arg_types = walk_state->arg_types; 723 return_ACPI_STATUS(status); 724 725 case AE_CTRL_END: 726 727 acpi_ps_pop_scope(&(walk_state->parser_state), op, 728 &walk_state->arg_types, 729 &walk_state->arg_count); 730 731 if (*op) { 732 walk_state->op = *op; 733 walk_state->op_info = 734 acpi_ps_get_opcode_info((*op)->common.aml_opcode); 735 walk_state->opcode = (*op)->common.aml_opcode; 736 737 status = walk_state->ascending_callback(walk_state); 738 status = 739 acpi_ps_next_parse_state(walk_state, *op, status); 740 741 status2 = acpi_ps_complete_this_op(walk_state, *op); 742 if (ACPI_FAILURE(status2)) { 743 return_ACPI_STATUS(status2); 744 } 745 } 746 747 status = AE_OK; 748 break; 749 750 case AE_CTRL_BREAK: 751 case AE_CTRL_CONTINUE: 752 753 /* Pop off scopes until we find the While */ 754 755 while (!(*op) || ((*op)->common.aml_opcode != AML_WHILE_OP)) { 756 acpi_ps_pop_scope(&(walk_state->parser_state), op, 757 &walk_state->arg_types, 758 &walk_state->arg_count); 759 } 760 761 /* Close this iteration of the While loop */ 762 763 walk_state->op = *op; 764 walk_state->op_info = 765 acpi_ps_get_opcode_info((*op)->common.aml_opcode); 766 walk_state->opcode = (*op)->common.aml_opcode; 767 768 status = walk_state->ascending_callback(walk_state); 769 status = acpi_ps_next_parse_state(walk_state, *op, status); 770 771 status2 = acpi_ps_complete_this_op(walk_state, *op); 772 if (ACPI_FAILURE(status2)) { 773 return_ACPI_STATUS(status2); 774 } 775 776 status = AE_OK; 777 break; 778 779 case AE_CTRL_TERMINATE: 780 781 /* Clean up */ 782 do { 783 if (*op) { 784 status2 = 785 acpi_ps_complete_this_op(walk_state, *op); 786 if (ACPI_FAILURE(status2)) { 787 return_ACPI_STATUS(status2); 788 } 789 790 acpi_ut_delete_generic_state 791 (acpi_ut_pop_generic_state 792 (&walk_state->control_state)); 793 } 794 795 acpi_ps_pop_scope(&(walk_state->parser_state), op, 796 &walk_state->arg_types, 797 &walk_state->arg_count); 798 799 } while (*op); 800 801 return_ACPI_STATUS(AE_OK); 802 803 default: /* All other non-AE_OK status */ 804 805 do { 806 if (*op) { 807 status2 = 808 acpi_ps_complete_this_op(walk_state, *op); 809 if (ACPI_FAILURE(status2)) { 810 return_ACPI_STATUS(status2); 811 } 812 } 813 814 acpi_ps_pop_scope(&(walk_state->parser_state), op, 815 &walk_state->arg_types, 816 &walk_state->arg_count); 817 818 } while (*op); 819 820 walk_state->prev_op = NULL; 821 walk_state->prev_arg_types = walk_state->arg_types; 822 return_ACPI_STATUS(status); 823 } 824 825 /* This scope complete? */ 826 827 if (acpi_ps_has_completed_scope(&(walk_state->parser_state))) { 828 acpi_ps_pop_scope(&(walk_state->parser_state), op, 829 &walk_state->arg_types, 830 &walk_state->arg_count); 831 ACPI_DEBUG_PRINT((ACPI_DB_PARSE, "Popped scope, Op=%p\n", *op)); 832 } else { 833 *op = NULL; 834 } 835 836 ACPI_PREEMPTION_POINT(); 837 838 return_ACPI_STATUS(AE_OK); 839} 840 841/******************************************************************************* 842 * 843 * FUNCTION: acpi_ps_complete_final_op 844 * 845 * PARAMETERS: walk_state - Current state 846 * Op - Current Op 847 * Status - Current parse status before complete last 848 * Op 849 * 850 * RETURN: Status 851 * 852 * DESCRIPTION: Complete last Op. 853 * 854 ******************************************************************************/ 855 856static acpi_status 857acpi_ps_complete_final_op(struct acpi_walk_state *walk_state, 858 union acpi_parse_object *op, acpi_status status) 859{ 860 acpi_status status2; 861 862 ACPI_FUNCTION_TRACE_PTR(ps_complete_final_op, walk_state); 863 864 /* 865 * Complete the last Op (if not completed), and clear the scope stack. 866 * It is easily possible to end an AML "package" with an unbounded number 867 * of open scopes (such as when several ASL blocks are closed with 868 * sequential closing braces). We want to terminate each one cleanly. 869 */ 870 ACPI_DEBUG_PRINT((ACPI_DB_PARSE, "AML package complete at Op %p\n", 871 op)); 872 do { 873 if (op) { 874 if (walk_state->ascending_callback != NULL) { 875 walk_state->op = op; 876 walk_state->op_info = 877 acpi_ps_get_opcode_info(op->common. 878 aml_opcode); 879 walk_state->opcode = op->common.aml_opcode; 880 881 status = 882 walk_state->ascending_callback(walk_state); 883 status = 884 acpi_ps_next_parse_state(walk_state, op, 885 status); 886 if (status == AE_CTRL_PENDING) { 887 status = 888 acpi_ps_complete_op(walk_state, &op, 889 AE_OK); 890 if (ACPI_FAILURE(status)) { 891 return_ACPI_STATUS(status); 892 } 893 } 894 895 if (status == AE_CTRL_TERMINATE) { 896 status = AE_OK; 897 898 /* Clean up */ 899 do { 900 if (op) { 901 status2 = 902 acpi_ps_complete_this_op 903 (walk_state, op); 904 if (ACPI_FAILURE 905 (status2)) { 906 return_ACPI_STATUS 907 (status2); 908 } 909 } 910 911 acpi_ps_pop_scope(& 912 (walk_state-> 913 parser_state), 914 &op, 915 &walk_state-> 916 arg_types, 917 &walk_state-> 918 arg_count); 919 920 } while (op); 921 922 return_ACPI_STATUS(status); 923 } 924 925 else if (ACPI_FAILURE(status)) { 926 927 /* First error is most important */ 928 929 (void) 930 acpi_ps_complete_this_op(walk_state, 931 op); 932 return_ACPI_STATUS(status); 933 } 934 } 935 936 status2 = acpi_ps_complete_this_op(walk_state, op); 937 if (ACPI_FAILURE(status2)) { 938 return_ACPI_STATUS(status2); 939 } 940 } 941 942 acpi_ps_pop_scope(&(walk_state->parser_state), &op, 943 &walk_state->arg_types, 944 &walk_state->arg_count); 945 946 } while (op); 947 948 return_ACPI_STATUS(status); 949} 950 951/******************************************************************************* 952 * 953 * FUNCTION: acpi_ps_parse_loop 954 * 955 * PARAMETERS: walk_state - Current state 956 * 957 * RETURN: Status 958 * 959 * DESCRIPTION: Parse AML (pointed to by the current parser state) and return 960 * a tree of ops. 961 * 962 ******************************************************************************/ 963 964acpi_status acpi_ps_parse_loop(struct acpi_walk_state *walk_state) 965{ 966 acpi_status status = AE_OK; 967 union acpi_parse_object *op = NULL; /* current op */ 968 struct acpi_parse_state *parser_state; 969 u8 *aml_op_start = NULL; 970 971 ACPI_FUNCTION_TRACE_PTR(ps_parse_loop, walk_state); 972 973 if (walk_state->descending_callback == NULL) { 974 return_ACPI_STATUS(AE_BAD_PARAMETER); 975 } 976 977 parser_state = &walk_state->parser_state; 978 walk_state->arg_types = 0; 979 980#if (!defined(ACPI_NO_METHOD_EXECUTION) && !defined(ACPI_CONSTANT_EVAL_ONLY)) 981 982 if (walk_state->walk_type & ACPI_WALK_METHOD_RESTART) { 983 984 /* We are restarting a preempted control method */ 985 986 if (acpi_ps_has_completed_scope(parser_state)) { 987 /* 988 * We must check if a predicate to an IF or WHILE statement 989 * was just completed 990 */ 991 if ((parser_state->scope->parse_scope.op) && 992 ((parser_state->scope->parse_scope.op->common. 993 aml_opcode == AML_IF_OP) 994 || (parser_state->scope->parse_scope.op->common. 995 aml_opcode == AML_WHILE_OP)) 996 && (walk_state->control_state) 997 && (walk_state->control_state->common.state == 998 ACPI_CONTROL_PREDICATE_EXECUTING)) { 999 /* 1000 * A predicate was just completed, get the value of the 1001 * predicate and branch based on that value 1002 */ 1003 walk_state->op = NULL; 1004 status = 1005 acpi_ds_get_predicate_value(walk_state, 1006 ACPI_TO_POINTER 1007 (TRUE)); 1008 if (ACPI_FAILURE(status) 1009 && ((status & AE_CODE_MASK) != 1010 AE_CODE_CONTROL)) { 1011 if (status == AE_AML_NO_RETURN_VALUE) { 1012 ACPI_EXCEPTION((AE_INFO, status, 1013 "Invoked method did not return a value")); 1014 } 1015 1016 ACPI_EXCEPTION((AE_INFO, status, 1017 "GetPredicate Failed")); 1018 return_ACPI_STATUS(status); 1019 } 1020 1021 status = 1022 acpi_ps_next_parse_state(walk_state, op, 1023 status); 1024 } 1025 1026 acpi_ps_pop_scope(parser_state, &op, 1027 &walk_state->arg_types, 1028 &walk_state->arg_count); 1029 ACPI_DEBUG_PRINT((ACPI_DB_PARSE, 1030 "Popped scope, Op=%p\n", op)); 1031 } else if (walk_state->prev_op) { 1032 1033 /* We were in the middle of an op */ 1034 1035 op = walk_state->prev_op; 1036 walk_state->arg_types = walk_state->prev_arg_types; 1037 } 1038 } 1039#endif 1040 1041 /* Iterative parsing loop, while there is more AML to process: */ 1042 1043 while ((parser_state->aml < parser_state->aml_end) || (op)) { 1044 aml_op_start = parser_state->aml; 1045 if (!op) { 1046 status = 1047 acpi_ps_create_op(walk_state, aml_op_start, &op); 1048 if (ACPI_FAILURE(status)) { 1049 if (status == AE_CTRL_PARSE_CONTINUE) { 1050 continue; 1051 } 1052 1053 if (status == AE_CTRL_PARSE_PENDING) { 1054 status = AE_OK; 1055 } 1056 1057 status = 1058 acpi_ps_complete_op(walk_state, &op, 1059 status); 1060 if (ACPI_FAILURE(status)) { 1061 return_ACPI_STATUS(status); 1062 } 1063 1064 continue; 1065 } 1066 1067 op->common.aml_offset = walk_state->aml_offset; 1068 1069 if (walk_state->op_info) { 1070 ACPI_DEBUG_PRINT((ACPI_DB_PARSE, 1071 "Opcode %4.4X [%s] Op %p Aml %p AmlOffset %5.5X\n", 1072 (u32) op->common.aml_opcode, 1073 walk_state->op_info->name, op, 1074 parser_state->aml, 1075 op->common.aml_offset)); 1076 } 1077 } 1078 1079 /* 1080 * Start arg_count at zero because we don't know if there are 1081 * any args yet 1082 */ 1083 walk_state->arg_count = 0; 1084 1085 /* Are there any arguments that must be processed? */ 1086 1087 if (walk_state->arg_types) { 1088 1089 /* Get arguments */ 1090 1091 status = 1092 acpi_ps_get_arguments(walk_state, aml_op_start, op); 1093 if (ACPI_FAILURE(status)) { 1094 status = 1095 acpi_ps_complete_op(walk_state, &op, 1096 status); 1097 if (ACPI_FAILURE(status)) { 1098 return_ACPI_STATUS(status); 1099 } 1100 1101 continue; 1102 } 1103 } 1104 1105 /* Check for arguments that need to be processed */ 1106 1107 if (walk_state->arg_count) { 1108 /* 1109 * There are arguments (complex ones), push Op and 1110 * prepare for argument 1111 */ 1112 status = acpi_ps_push_scope(parser_state, op, 1113 walk_state->arg_types, 1114 walk_state->arg_count); 1115 if (ACPI_FAILURE(status)) { 1116 status = 1117 acpi_ps_complete_op(walk_state, &op, 1118 status); 1119 if (ACPI_FAILURE(status)) { 1120 return_ACPI_STATUS(status); 1121 } 1122 1123 continue; 1124 } 1125 1126 op = NULL; 1127 continue; 1128 } 1129 1130 /* 1131 * All arguments have been processed -- Op is complete, 1132 * prepare for next 1133 */ 1134 walk_state->op_info = 1135 acpi_ps_get_opcode_info(op->common.aml_opcode); 1136 if (walk_state->op_info->flags & AML_NAMED) { 1137 if (acpi_gbl_depth) { 1138 acpi_gbl_depth--; 1139 } 1140 1141 if (op->common.aml_opcode == AML_REGION_OP || 1142 op->common.aml_opcode == AML_DATA_REGION_OP) { 1143 /* 1144 * Skip parsing of control method or opregion body, 1145 * because we don't have enough info in the first pass 1146 * to parse them correctly. 1147 * 1148 * Completed parsing an op_region declaration, we now 1149 * know the length. 1150 */ 1151 op->named.length = 1152 (u32) (parser_state->aml - op->named.data); 1153 } 1154 } 1155 1156 if (walk_state->op_info->flags & AML_CREATE) { 1157 /* 1158 * Backup to beginning of create_xXXfield declaration (1 for 1159 * Opcode) 1160 * 1161 * body_length is unknown until we parse the body 1162 */ 1163 op->named.length = 1164 (u32) (parser_state->aml - op->named.data); 1165 } 1166 1167 if (op->common.aml_opcode == AML_BANK_FIELD_OP) { 1168 /* 1169 * Backup to beginning of bank_field declaration 1170 * 1171 * body_length is unknown until we parse the body 1172 */ 1173 op->named.length = 1174 (u32) (parser_state->aml - op->named.data); 1175 } 1176 1177 /* This op complete, notify the dispatcher */ 1178 1179 if (walk_state->ascending_callback != NULL) { 1180 walk_state->op = op; 1181 walk_state->opcode = op->common.aml_opcode; 1182 1183 status = walk_state->ascending_callback(walk_state); 1184 status = 1185 acpi_ps_next_parse_state(walk_state, op, status); 1186 if (status == AE_CTRL_PENDING) { 1187 status = AE_OK; 1188 } 1189 } 1190 1191 status = acpi_ps_complete_op(walk_state, &op, status); 1192 if (ACPI_FAILURE(status)) { 1193 return_ACPI_STATUS(status); 1194 } 1195 1196 } /* while parser_state->Aml */ 1197 1198 status = acpi_ps_complete_final_op(walk_state, op, status); 1199 return_ACPI_STATUS(status); 1200} 1201