1 /*******************************************************************************
 
   3  * Module Name: dsutils - Dispatcher utilities
 
   5  ******************************************************************************/
 
   8  * Copyright (C) 2000 - 2005, R. Byron Moore
 
  11  * Redistribution and use in source and binary forms, with or without
 
  12  * modification, are permitted provided that the following conditions
 
  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.
 
  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.
 
  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.
 
  44 #include <acpi/acpi.h>
 
  45 #include <acpi/acparser.h>
 
  46 #include <acpi/amlcode.h>
 
  47 #include <acpi/acdispat.h>
 
  48 #include <acpi/acinterp.h>
 
  49 #include <acpi/acnamesp.h>
 
  50 #include <acpi/acdebug.h>
 
  52 #define _COMPONENT          ACPI_DISPATCHER
 
  53 ACPI_MODULE_NAME("dsutils")
 
  55 /*******************************************************************************
 
  57  * FUNCTION:    acpi_ds_clear_implicit_return
 
  59  * PARAMETERS:  walk_state          - Current State
 
  63  * DESCRIPTION: Clear and remove a reference on an implicit return value.  Used
 
  64  *              to delete "stale" return values (if enabled, the return value
 
  65  *              from every operator is saved at least momentarily, in case the
 
  66  *              parent method exits.)
 
  68  ******************************************************************************/
 
  69 void acpi_ds_clear_implicit_return(struct acpi_walk_state *walk_state)
 
  71         ACPI_FUNCTION_NAME("ds_clear_implicit_return");
 
  74          * Slack must be enabled for this feature
 
  76         if (!acpi_gbl_enable_interpreter_slack) {
 
  80         if (walk_state->implicit_return_obj) {
 
  82                  * Delete any "stale" implicit return. However, in
 
  83                  * complex statements, the implicit return value can be
 
  84                  * bubbled up several levels.
 
  86                 ACPI_DEBUG_PRINT((ACPI_DB_DISPATCH,
 
  87                                   "Removing reference on stale implicit return obj %p\n",
 
  88                                   walk_state->implicit_return_obj));
 
  90                 acpi_ut_remove_reference(walk_state->implicit_return_obj);
 
  91                 walk_state->implicit_return_obj = NULL;
 
  95 #ifndef ACPI_NO_METHOD_EXECUTION
 
  96 /*******************************************************************************
 
  98  * FUNCTION:    acpi_ds_do_implicit_return
 
 100  * PARAMETERS:  return_desc         - The return value
 
 101  *              walk_state          - Current State
 
 102  *              add_reference       - True if a reference should be added to the
 
 105  * RETURN:      TRUE if implicit return enabled, FALSE otherwise
 
 107  * DESCRIPTION: Implements the optional "implicit return".  We save the result
 
 108  *              of every ASL operator and control method invocation in case the
 
 109  *              parent method exit.  Before storing a new return value, we
 
 110  *              delete the previous return value.
 
 112  ******************************************************************************/
 
 115 acpi_ds_do_implicit_return(union acpi_operand_object *return_desc,
 
 116                            struct acpi_walk_state *walk_state, u8 add_reference)
 
 118         ACPI_FUNCTION_NAME("ds_do_implicit_return");
 
 121          * Slack must be enabled for this feature, and we must
 
 122          * have a valid return object
 
 124         if ((!acpi_gbl_enable_interpreter_slack) || (!return_desc)) {
 
 128         ACPI_DEBUG_PRINT((ACPI_DB_DISPATCH,
 
 129                           "Result %p will be implicitly returned; Prev=%p\n",
 
 130                           return_desc, walk_state->implicit_return_obj));
 
 133          * Delete any "stale" implicit return value first. However, in
 
 134          * complex statements, the implicit return value can be
 
 135          * bubbled up several levels, so we don't clear the value if it
 
 136          * is the same as the return_desc.
 
 138         if (walk_state->implicit_return_obj) {
 
 139                 if (walk_state->implicit_return_obj == return_desc) {
 
 142                 acpi_ds_clear_implicit_return(walk_state);
 
 145         /* Save the implicit return value, add a reference if requested */
 
 147         walk_state->implicit_return_obj = return_desc;
 
 149                 acpi_ut_add_reference(return_desc);
 
 155 /*******************************************************************************
 
 157  * FUNCTION:    acpi_ds_is_result_used
 
 159  * PARAMETERS:  Op                  - Current Op
 
 160  *              walk_state          - Current State
 
 162  * RETURN:      TRUE if result is used, FALSE otherwise
 
 164  * DESCRIPTION: Check if a result object will be used by the parent
 
 166  ******************************************************************************/
 
 169 acpi_ds_is_result_used(union acpi_parse_object * op,
 
 170                        struct acpi_walk_state * walk_state)
 
 172         const struct acpi_opcode_info *parent_info;
 
 174         ACPI_FUNCTION_TRACE_PTR("ds_is_result_used", op);
 
 176         /* Must have both an Op and a Result Object */
 
 179                 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Null Op\n"));
 
 184          * We know that this operator is not a
 
 185          * Return() operator (would not come here.) The following code is the
 
 186          * optional support for a so-called "implicit return". Some AML code
 
 187          * assumes that the last value of the method is "implicitly" returned
 
 188          * to the caller. Just save the last result as the return value.
 
 189          * NOTE: this is optional because the ASL language does not actually
 
 190          * support this behavior.
 
 192         (void)acpi_ds_do_implicit_return(walk_state->result_obj, walk_state,
 
 196          * Now determine if the parent will use the result
 
 198          * If there is no parent, or the parent is a scope_op, we are executing
 
 199          * at the method level. An executing method typically has no parent,
 
 200          * since each method is parsed separately.  A method invoked externally
 
 201          * via execute_control_method has a scope_op as the parent.
 
 203         if ((!op->common.parent) ||
 
 204             (op->common.parent->common.aml_opcode == AML_SCOPE_OP)) {
 
 205                 /* No parent, the return value cannot possibly be used */
 
 207                 ACPI_DEBUG_PRINT((ACPI_DB_DISPATCH,
 
 208                                   "At Method level, result of [%s] not used\n",
 
 209                                   acpi_ps_get_opcode_name(op->common.
 
 214         /* Get info on the parent. The root_op is AML_SCOPE */
 
 217             acpi_ps_get_opcode_info(op->common.parent->common.aml_opcode);
 
 218         if (parent_info->class == AML_CLASS_UNKNOWN) {
 
 219                 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
 
 220                                   "Unknown parent opcode. Op=%p\n", op));
 
 225          * Decide what to do with the result based on the parent.  If
 
 226          * the parent opcode will not use the result, delete the object.
 
 227          * Otherwise leave it as is, it will be deleted when it is used
 
 228          * as an operand later.
 
 230         switch (parent_info->class) {
 
 231         case AML_CLASS_CONTROL:
 
 233                 switch (op->common.parent->common.aml_opcode) {
 
 236                         /* Never delete the return value associated with a return opcode */
 
 244                          * If we are executing the predicate AND this is the predicate op,
 
 245                          * we will use the return value
 
 247                         if ((walk_state->control_state->common.state ==
 
 248                              ACPI_CONTROL_PREDICATE_EXECUTING)
 
 249                             && (walk_state->control_state->control.
 
 250                                 predicate_op == op)) {
 
 256                         /* Ignore other control opcodes */
 
 260                 /* The general control opcode returns no result */
 
 262                 goto result_not_used;
 
 264         case AML_CLASS_CREATE:
 
 267                  * These opcodes allow term_arg(s) as operands and therefore
 
 268                  * the operands can be method calls.  The result is used.
 
 272         case AML_CLASS_NAMED_OBJECT:
 
 274                 if ((op->common.parent->common.aml_opcode == AML_REGION_OP) ||
 
 275                     (op->common.parent->common.aml_opcode == AML_DATA_REGION_OP)
 
 276                     || (op->common.parent->common.aml_opcode == AML_PACKAGE_OP)
 
 277                     || (op->common.parent->common.aml_opcode ==
 
 279                     || (op->common.parent->common.aml_opcode == AML_BUFFER_OP)
 
 280                     || (op->common.parent->common.aml_opcode ==
 
 281                         AML_INT_EVAL_SUBTREE_OP)) {
 
 283                          * These opcodes allow term_arg(s) as operands and therefore
 
 284                          * the operands can be method calls.  The result is used.
 
 289                 goto result_not_used;
 
 294                  * In all other cases. the parent will actually use the return
 
 295                  * object, so keep it.
 
 301         ACPI_DEBUG_PRINT((ACPI_DB_DISPATCH,
 
 302                           "Result of [%s] used by Parent [%s] Op=%p\n",
 
 303                           acpi_ps_get_opcode_name(op->common.aml_opcode),
 
 304                           acpi_ps_get_opcode_name(op->common.parent->common.
 
 310         ACPI_DEBUG_PRINT((ACPI_DB_DISPATCH,
 
 311                           "Result of [%s] not used by Parent [%s] Op=%p\n",
 
 312                           acpi_ps_get_opcode_name(op->common.aml_opcode),
 
 313                           acpi_ps_get_opcode_name(op->common.parent->common.
 
 319 /*******************************************************************************
 
 321  * FUNCTION:    acpi_ds_delete_result_if_not_used
 
 323  * PARAMETERS:  Op              - Current parse Op
 
 324  *              result_obj      - Result of the operation
 
 325  *              walk_state      - Current state
 
 329  * DESCRIPTION: Used after interpretation of an opcode.  If there is an internal
 
 330  *              result descriptor, check if the parent opcode will actually use
 
 331  *              this result.  If not, delete the result now so that it will
 
 332  *              not become orphaned.
 
 334  ******************************************************************************/
 
 337 acpi_ds_delete_result_if_not_used(union acpi_parse_object *op,
 
 338                                   union acpi_operand_object *result_obj,
 
 339                                   struct acpi_walk_state *walk_state)
 
 341         union acpi_operand_object *obj_desc;
 
 344         ACPI_FUNCTION_TRACE_PTR("ds_delete_result_if_not_used", result_obj);
 
 347                 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Null Op\n"));
 
 355         if (!acpi_ds_is_result_used(op, walk_state)) {
 
 356                 /* Must pop the result stack (obj_desc should be equal to result_obj) */
 
 358                 status = acpi_ds_result_pop(&obj_desc, walk_state);
 
 359                 if (ACPI_SUCCESS(status)) {
 
 360                         acpi_ut_remove_reference(result_obj);
 
 367 /*******************************************************************************
 
 369  * FUNCTION:    acpi_ds_resolve_operands
 
 371  * PARAMETERS:  walk_state          - Current walk state with operands on stack
 
 375  * DESCRIPTION: Resolve all operands to their values.  Used to prepare
 
 376  *              arguments to a control method invocation (a call from one
 
 377  *              method to another.)
 
 379  ******************************************************************************/
 
 381 acpi_status acpi_ds_resolve_operands(struct acpi_walk_state *walk_state)
 
 384         acpi_status status = AE_OK;
 
 386         ACPI_FUNCTION_TRACE_PTR("ds_resolve_operands", walk_state);
 
 389          * Attempt to resolve each of the valid operands
 
 390          * Method arguments are passed by reference, not by value.  This means
 
 391          * that the actual objects are passed, not copies of the objects.
 
 393         for (i = 0; i < walk_state->num_operands; i++) {
 
 395                     acpi_ex_resolve_to_value(&walk_state->operands[i],
 
 397                 if (ACPI_FAILURE(status)) {
 
 402         return_ACPI_STATUS(status);
 
 405 /*******************************************************************************
 
 407  * FUNCTION:    acpi_ds_clear_operands
 
 409  * PARAMETERS:  walk_state          - Current walk state with operands on stack
 
 413  * DESCRIPTION: Clear all operands on the current walk state operand stack.
 
 415  ******************************************************************************/
 
 417 void acpi_ds_clear_operands(struct acpi_walk_state *walk_state)
 
 421         ACPI_FUNCTION_TRACE_PTR("ds_clear_operands", walk_state);
 
 423         /* Remove a reference on each operand on the stack */
 
 425         for (i = 0; i < walk_state->num_operands; i++) {
 
 427                  * Remove a reference to all operands, including both
 
 428                  * "Arguments" and "Targets".
 
 430                 acpi_ut_remove_reference(walk_state->operands[i]);
 
 431                 walk_state->operands[i] = NULL;
 
 434         walk_state->num_operands = 0;
 
 439 /*******************************************************************************
 
 441  * FUNCTION:    acpi_ds_create_operand
 
 443  * PARAMETERS:  walk_state      - Current walk state
 
 444  *              Arg             - Parse object for the argument
 
 445  *              arg_index       - Which argument (zero based)
 
 449  * DESCRIPTION: Translate a parse tree object that is an argument to an AML
 
 450  *              opcode to the equivalent interpreter object.  This may include
 
 451  *              looking up a name or entering a new name into the internal
 
 454  ******************************************************************************/
 
 457 acpi_ds_create_operand(struct acpi_walk_state *walk_state,
 
 458                        union acpi_parse_object *arg, u32 arg_index)
 
 460         acpi_status status = AE_OK;
 
 463         union acpi_operand_object *obj_desc;
 
 464         union acpi_parse_object *parent_op;
 
 466         acpi_interpreter_mode interpreter_mode;
 
 467         const struct acpi_opcode_info *op_info;
 
 469         ACPI_FUNCTION_TRACE_PTR("ds_create_operand", arg);
 
 471         /* A valid name must be looked up in the namespace */
 
 473         if ((arg->common.aml_opcode == AML_INT_NAMEPATH_OP) &&
 
 474             (arg->common.value.string)) {
 
 475                 ACPI_DEBUG_PRINT((ACPI_DB_DISPATCH, "Getting a name: Arg=%p\n",
 
 478                 /* Get the entire name string from the AML stream */
 
 481                     acpi_ex_get_name_string(ACPI_TYPE_ANY,
 
 482                                             arg->common.value.buffer,
 
 483                                             &name_string, &name_length);
 
 485                 if (ACPI_FAILURE(status)) {
 
 486                         return_ACPI_STATUS(status);
 
 489                 /* All prefixes have been handled, and the name is in name_string */
 
 492                  * Special handling for buffer_field declarations. This is a deferred
 
 493                  * opcode that unfortunately defines the field name as the last
 
 494                  * parameter instead of the first.  We get here when we are performing
 
 495                  * the deferred execution, so the actual name of the field is already
 
 496                  * in the namespace.  We don't want to attempt to look it up again
 
 497                  * because we may be executing in a different scope than where the
 
 498                  * actual opcode exists.
 
 500                 if ((walk_state->deferred_node) &&
 
 501                     (walk_state->deferred_node->type == ACPI_TYPE_BUFFER_FIELD)
 
 502                     && (arg_index != 0)) {
 
 504                             ACPI_CAST_PTR(union acpi_operand_object,
 
 505                                           walk_state->deferred_node);
 
 507                 } else {        /* All other opcodes */
 
 510                          * Differentiate between a namespace "create" operation
 
 511                          * versus a "lookup" operation (IMODE_LOAD_PASS2 vs.
 
 512                          * IMODE_EXECUTE) in order to support the creation of
 
 513                          * namespace objects during the execution of control methods.
 
 515                         parent_op = arg->common.parent;
 
 517                             acpi_ps_get_opcode_info(parent_op->common.
 
 519                         if ((op_info->flags & AML_NSNODE)
 
 520                             && (parent_op->common.aml_opcode !=
 
 521                                 AML_INT_METHODCALL_OP)
 
 522                             && (parent_op->common.aml_opcode != AML_REGION_OP)
 
 523                             && (parent_op->common.aml_opcode !=
 
 524                                 AML_INT_NAMEPATH_OP)) {
 
 525                                 /* Enter name into namespace if not found */
 
 527                                 interpreter_mode = ACPI_IMODE_LOAD_PASS2;
 
 529                                 /* Return a failure if name not found */
 
 531                                 interpreter_mode = ACPI_IMODE_EXECUTE;
 
 535                             acpi_ns_lookup(walk_state->scope_info, name_string,
 
 536                                            ACPI_TYPE_ANY, interpreter_mode,
 
 537                                            ACPI_NS_SEARCH_PARENT |
 
 538                                            ACPI_NS_DONT_OPEN_SCOPE, walk_state,
 
 539                                            ACPI_CAST_INDIRECT_PTR(struct
 
 543                          * The only case where we pass through (ignore) a NOT_FOUND
 
 544                          * error is for the cond_ref_of opcode.
 
 546                         if (status == AE_NOT_FOUND) {
 
 547                                 if (parent_op->common.aml_opcode ==
 
 548                                     AML_COND_REF_OF_OP) {
 
 550                                          * For the Conditional Reference op, it's OK if
 
 551                                          * the name is not found;  We just need a way to
 
 552                                          * indicate this to the interpreter, set the
 
 562                                          * We just plain didn't find it -- which is a
 
 563                                          * very serious error at this point
 
 565                                         status = AE_AML_NAME_NOT_FOUND;
 
 569                         if (ACPI_FAILURE(status)) {
 
 570                                 ACPI_REPORT_NSERROR(name_string, status);
 
 574                 /* Free the namestring created above */
 
 576                 ACPI_MEM_FREE(name_string);
 
 578                 /* Check status from the lookup */
 
 580                 if (ACPI_FAILURE(status)) {
 
 581                         return_ACPI_STATUS(status);
 
 584                 /* Put the resulting object onto the current object stack */
 
 586                 status = acpi_ds_obj_stack_push(obj_desc, walk_state);
 
 587                 if (ACPI_FAILURE(status)) {
 
 588                         return_ACPI_STATUS(status);
 
 590                 ACPI_DEBUGGER_EXEC(acpi_db_display_argument_object
 
 591                                    (obj_desc, walk_state));
 
 593                 /* Check for null name case */
 
 595                 if (arg->common.aml_opcode == AML_INT_NAMEPATH_OP) {
 
 597                          * If the name is null, this means that this is an
 
 598                          * optional result parameter that was not specified
 
 599                          * in the original ASL.  Create a Zero Constant for a
 
 600                          * placeholder.  (Store to a constant is a Noop.)
 
 602                         opcode = AML_ZERO_OP;   /* Has no arguments! */
 
 604                         ACPI_DEBUG_PRINT((ACPI_DB_DISPATCH,
 
 605                                           "Null namepath: Arg=%p\n", arg));
 
 607                         opcode = arg->common.aml_opcode;
 
 610                 /* Get the object type of the argument */
 
 612                 op_info = acpi_ps_get_opcode_info(opcode);
 
 613                 if (op_info->object_type == ACPI_TYPE_INVALID) {
 
 614                         return_ACPI_STATUS(AE_NOT_IMPLEMENTED);
 
 617                 if (op_info->flags & AML_HAS_RETVAL) {
 
 618                         ACPI_DEBUG_PRINT((ACPI_DB_DISPATCH,
 
 619                                           "Argument previously created, already stacked \n"));
 
 621                         ACPI_DEBUGGER_EXEC(acpi_db_display_argument_object
 
 623                                             operands[walk_state->num_operands -
 
 627                          * Use value that was already previously returned
 
 628                          * by the evaluation of this argument
 
 631                             acpi_ds_result_pop_from_bottom(&obj_desc,
 
 633                         if (ACPI_FAILURE(status)) {
 
 635                                  * Only error is underflow, and this indicates
 
 636                                  * a missing or null operand!
 
 638                                 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
 
 639                                                   "Missing or null operand, %s\n",
 
 640                                                   acpi_format_exception
 
 642                                 return_ACPI_STATUS(status);
 
 645                         /* Create an ACPI_INTERNAL_OBJECT for the argument */
 
 648                             acpi_ut_create_internal_object(op_info->
 
 651                                 return_ACPI_STATUS(AE_NO_MEMORY);
 
 654                         /* Initialize the new object */
 
 657                             acpi_ds_init_object_from_op(walk_state, arg, opcode,
 
 659                         if (ACPI_FAILURE(status)) {
 
 660                                 acpi_ut_delete_object_desc(obj_desc);
 
 661                                 return_ACPI_STATUS(status);
 
 665                 /* Put the operand object on the object stack */
 
 667                 status = acpi_ds_obj_stack_push(obj_desc, walk_state);
 
 668                 if (ACPI_FAILURE(status)) {
 
 669                         return_ACPI_STATUS(status);
 
 672                 ACPI_DEBUGGER_EXEC(acpi_db_display_argument_object
 
 673                                    (obj_desc, walk_state));
 
 676         return_ACPI_STATUS(AE_OK);
 
 679 /*******************************************************************************
 
 681  * FUNCTION:    acpi_ds_create_operands
 
 683  * PARAMETERS:  walk_state          - Current state
 
 684  *              first_arg           - First argument of a parser argument tree
 
 688  * DESCRIPTION: Convert an operator's arguments from a parse tree format to
 
 689  *              namespace objects and place those argument object on the object
 
 690  *              stack in preparation for evaluation by the interpreter.
 
 692  ******************************************************************************/
 
 695 acpi_ds_create_operands(struct acpi_walk_state *walk_state,
 
 696                         union acpi_parse_object *first_arg)
 
 698         acpi_status status = AE_OK;
 
 699         union acpi_parse_object *arg;
 
 702         ACPI_FUNCTION_TRACE_PTR("ds_create_operands", first_arg);
 
 704         /* For all arguments in the list... */
 
 708                 status = acpi_ds_create_operand(walk_state, arg, arg_count);
 
 709                 if (ACPI_FAILURE(status)) {
 
 713                 ACPI_DEBUG_PRINT((ACPI_DB_DISPATCH,
 
 714                                   "Arg #%d (%p) done, Arg1=%p\n", arg_count,
 
 717                 /* Move on to next argument, if any */
 
 719                 arg = arg->common.next;
 
 723         return_ACPI_STATUS(status);
 
 727          * We must undo everything done above; meaning that we must
 
 728          * pop everything off of the operand stack and delete those
 
 731         (void)acpi_ds_obj_stack_pop_and_delete(arg_count, walk_state);
 
 733         ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "While creating Arg %d - %s\n",
 
 734                           (arg_count + 1), acpi_format_exception(status)));
 
 735         return_ACPI_STATUS(status);