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.
45 #include <acpi/acpi.h>
46 #include <acpi/acparser.h>
47 #include <acpi/amlcode.h>
48 #include <acpi/acdispat.h>
49 #include <acpi/acinterp.h>
50 #include <acpi/acnamesp.h>
51 #include <acpi/acdebug.h>
53 #define _COMPONENT ACPI_DISPATCHER
54 ACPI_MODULE_NAME ("dsutils")
57 #ifndef ACPI_NO_METHOD_EXECUTION
59 /*******************************************************************************
61 * FUNCTION: acpi_ds_is_result_used
63 * PARAMETERS: Op - Current Op
64 * walk_state - Current State
66 * RETURN: TRUE if result is used, FALSE otherwise
68 * DESCRIPTION: Check if a result object will be used by the parent
70 ******************************************************************************/
73 acpi_ds_is_result_used (
74 union acpi_parse_object *op,
75 struct acpi_walk_state *walk_state)
77 const struct acpi_opcode_info *parent_info;
80 ACPI_FUNCTION_TRACE_PTR ("ds_is_result_used", op);
83 /* Must have both an Op and a Result Object */
86 ACPI_DEBUG_PRINT ((ACPI_DB_ERROR, "Null Op\n"));
91 * If there is no parent, or the parent is a scope_op, we are executing
92 * at the method level. An executing method typically has no parent,
93 * since each method is parsed separately. A method invoked externally
94 * via execute_control_method has a scope_op as the parent.
96 if ((!op->common.parent) ||
97 (op->common.parent->common.aml_opcode == AML_SCOPE_OP)) {
99 * If this is the last statement in the method, we know it is not a
100 * Return() operator (would not come here.) The following code is the
101 * optional support for a so-called "implicit return". Some AML code
102 * assumes that the last value of the method is "implicitly" returned
103 * to the caller. Just save the last result as the return value.
104 * NOTE: this is optional because the ASL language does not actually
105 * support this behavior.
107 if ((acpi_gbl_enable_interpreter_slack) &&
108 (walk_state->parser_state.aml >= walk_state->parser_state.aml_end)) {
109 ACPI_DEBUG_PRINT ((ACPI_DB_DISPATCH,
110 "Result of [%s] will be implicitly returned\n",
111 acpi_ps_get_opcode_name (op->common.aml_opcode)));
113 /* Use the top of the result stack as the implicit return value */
115 walk_state->return_desc = walk_state->results->results.obj_desc[0];
119 /* No parent, the return value cannot possibly be used */
121 return_VALUE (FALSE);
124 /* Get info on the parent. The root_op is AML_SCOPE */
126 parent_info = acpi_ps_get_opcode_info (op->common.parent->common.aml_opcode);
127 if (parent_info->class == AML_CLASS_UNKNOWN) {
128 ACPI_DEBUG_PRINT ((ACPI_DB_ERROR, "Unknown parent opcode. Op=%p\n", op));
129 return_VALUE (FALSE);
133 * Decide what to do with the result based on the parent. If
134 * the parent opcode will not use the result, delete the object.
135 * Otherwise leave it as is, it will be deleted when it is used
136 * as an operand later.
138 switch (parent_info->class) {
139 case AML_CLASS_CONTROL:
141 switch (op->common.parent->common.aml_opcode) {
144 /* Never delete the return value associated with a return opcode */
152 * If we are executing the predicate AND this is the predicate op,
153 * we will use the return value
155 if ((walk_state->control_state->common.state == ACPI_CONTROL_PREDICATE_EXECUTING) &&
156 (walk_state->control_state->control.predicate_op == op)) {
162 /* Ignore other control opcodes */
166 /* The general control opcode returns no result */
168 goto result_not_used;
171 case AML_CLASS_CREATE:
174 * These opcodes allow term_arg(s) as operands and therefore
175 * the operands can be method calls. The result is used.
180 case AML_CLASS_NAMED_OBJECT:
182 if ((op->common.parent->common.aml_opcode == AML_REGION_OP) ||
183 (op->common.parent->common.aml_opcode == AML_DATA_REGION_OP) ||
184 (op->common.parent->common.aml_opcode == AML_PACKAGE_OP) ||
185 (op->common.parent->common.aml_opcode == AML_VAR_PACKAGE_OP) ||
186 (op->common.parent->common.aml_opcode == AML_BUFFER_OP) ||
187 (op->common.parent->common.aml_opcode == AML_INT_EVAL_SUBTREE_OP)) {
189 * These opcodes allow term_arg(s) as operands and therefore
190 * the operands can be method calls. The result is used.
195 goto result_not_used;
201 * In all other cases. the parent will actually use the return
202 * object, so keep it.
209 ACPI_DEBUG_PRINT ((ACPI_DB_DISPATCH, "Result of [%s] used by Parent [%s] Op=%p\n",
210 acpi_ps_get_opcode_name (op->common.aml_opcode),
211 acpi_ps_get_opcode_name (op->common.parent->common.aml_opcode), op));
217 ACPI_DEBUG_PRINT ((ACPI_DB_DISPATCH, "Result of [%s] not used by Parent [%s] Op=%p\n",
218 acpi_ps_get_opcode_name (op->common.aml_opcode),
219 acpi_ps_get_opcode_name (op->common.parent->common.aml_opcode), op));
221 return_VALUE (FALSE);
225 /*******************************************************************************
227 * FUNCTION: acpi_ds_delete_result_if_not_used
229 * PARAMETERS: Op - Current parse Op
230 * result_obj - Result of the operation
231 * walk_state - Current state
235 * DESCRIPTION: Used after interpretation of an opcode. If there is an internal
236 * result descriptor, check if the parent opcode will actually use
237 * this result. If not, delete the result now so that it will
238 * not become orphaned.
240 ******************************************************************************/
243 acpi_ds_delete_result_if_not_used (
244 union acpi_parse_object *op,
245 union acpi_operand_object *result_obj,
246 struct acpi_walk_state *walk_state)
248 union acpi_operand_object *obj_desc;
252 ACPI_FUNCTION_TRACE_PTR ("ds_delete_result_if_not_used", result_obj);
256 ACPI_DEBUG_PRINT ((ACPI_DB_ERROR, "Null Op\n"));
264 if (!acpi_ds_is_result_used (op, walk_state)) {
266 * Must pop the result stack (obj_desc should be equal to result_obj)
268 status = acpi_ds_result_pop (&obj_desc, walk_state);
269 if (ACPI_SUCCESS (status)) {
270 acpi_ut_remove_reference (result_obj);
278 /*******************************************************************************
280 * FUNCTION: acpi_ds_resolve_operands
282 * PARAMETERS: walk_state - Current walk state with operands on stack
286 * DESCRIPTION: Resolve all operands to their values. Used to prepare
287 * arguments to a control method invocation (a call from one
288 * method to another.)
290 ******************************************************************************/
293 acpi_ds_resolve_operands (
294 struct acpi_walk_state *walk_state)
297 acpi_status status = AE_OK;
300 ACPI_FUNCTION_TRACE_PTR ("ds_resolve_operands", walk_state);
304 * Attempt to resolve each of the valid operands
305 * Method arguments are passed by reference, not by value. This means
306 * that the actual objects are passed, not copies of the objects.
308 for (i = 0; i < walk_state->num_operands; i++) {
309 status = acpi_ex_resolve_to_value (&walk_state->operands[i], walk_state);
310 if (ACPI_FAILURE (status)) {
315 return_ACPI_STATUS (status);
319 /*******************************************************************************
321 * FUNCTION: acpi_ds_clear_operands
323 * PARAMETERS: walk_state - Current walk state with operands on stack
327 * DESCRIPTION: Clear all operands on the current walk state operand stack.
329 ******************************************************************************/
332 acpi_ds_clear_operands (
333 struct acpi_walk_state *walk_state)
338 ACPI_FUNCTION_TRACE_PTR ("ds_clear_operands", walk_state);
342 * Remove a reference on each operand on the stack
344 for (i = 0; i < walk_state->num_operands; i++) {
346 * Remove a reference to all operands, including both
347 * "Arguments" and "Targets".
349 acpi_ut_remove_reference (walk_state->operands[i]);
350 walk_state->operands[i] = NULL;
353 walk_state->num_operands = 0;
359 /*******************************************************************************
361 * FUNCTION: acpi_ds_create_operand
363 * PARAMETERS: walk_state - Current walk state
364 * Arg - Parse object for the argument
365 * arg_index - Which argument (zero based)
369 * DESCRIPTION: Translate a parse tree object that is an argument to an AML
370 * opcode to the equivalent interpreter object. This may include
371 * looking up a name or entering a new name into the internal
374 ******************************************************************************/
377 acpi_ds_create_operand (
378 struct acpi_walk_state *walk_state,
379 union acpi_parse_object *arg,
382 acpi_status status = AE_OK;
385 union acpi_operand_object *obj_desc;
386 union acpi_parse_object *parent_op;
388 acpi_interpreter_mode interpreter_mode;
389 const struct acpi_opcode_info *op_info;
392 ACPI_FUNCTION_TRACE_PTR ("ds_create_operand", arg);
395 /* A valid name must be looked up in the namespace */
397 if ((arg->common.aml_opcode == AML_INT_NAMEPATH_OP) &&
398 (arg->common.value.string)) {
399 ACPI_DEBUG_PRINT ((ACPI_DB_DISPATCH, "Getting a name: Arg=%p\n", arg));
401 /* Get the entire name string from the AML stream */
403 status = acpi_ex_get_name_string (ACPI_TYPE_ANY, arg->common.value.buffer,
404 &name_string, &name_length);
406 if (ACPI_FAILURE (status)) {
407 return_ACPI_STATUS (status);
411 * All prefixes have been handled, and the name is
417 * Special handling for buffer_field declarations. This is a deferred
418 * opcode that unfortunately defines the field name as the last
419 * parameter instead of the first. We get here when we are performing
420 * the deferred execution, so the actual name of the field is already
421 * in the namespace. We don't want to attempt to look it up again
422 * because we may be executing in a different scope than where the
423 * actual opcode exists.
425 if ((walk_state->deferred_node) &&
426 (walk_state->deferred_node->type == ACPI_TYPE_BUFFER_FIELD) &&
428 obj_desc = ACPI_CAST_PTR (union acpi_operand_object, walk_state->deferred_node);
431 else /* All other opcodes */ {
433 * Differentiate between a namespace "create" operation
434 * versus a "lookup" operation (IMODE_LOAD_PASS2 vs.
435 * IMODE_EXECUTE) in order to support the creation of
436 * namespace objects during the execution of control methods.
438 parent_op = arg->common.parent;
439 op_info = acpi_ps_get_opcode_info (parent_op->common.aml_opcode);
440 if ((op_info->flags & AML_NSNODE) &&
441 (parent_op->common.aml_opcode != AML_INT_METHODCALL_OP) &&
442 (parent_op->common.aml_opcode != AML_REGION_OP) &&
443 (parent_op->common.aml_opcode != AML_INT_NAMEPATH_OP)) {
444 /* Enter name into namespace if not found */
446 interpreter_mode = ACPI_IMODE_LOAD_PASS2;
449 /* Return a failure if name not found */
451 interpreter_mode = ACPI_IMODE_EXECUTE;
454 status = acpi_ns_lookup (walk_state->scope_info, name_string,
455 ACPI_TYPE_ANY, interpreter_mode,
456 ACPI_NS_SEARCH_PARENT | ACPI_NS_DONT_OPEN_SCOPE,
458 ACPI_CAST_INDIRECT_PTR (struct acpi_namespace_node, &obj_desc));
460 * The only case where we pass through (ignore) a NOT_FOUND
461 * error is for the cond_ref_of opcode.
463 if (status == AE_NOT_FOUND) {
464 if (parent_op->common.aml_opcode == AML_COND_REF_OF_OP) {
466 * For the Conditional Reference op, it's OK if
467 * the name is not found; We just need a way to
468 * indicate this to the interpreter, set the
471 obj_desc = ACPI_CAST_PTR (union acpi_operand_object, acpi_gbl_root_node);
476 * We just plain didn't find it -- which is a
477 * very serious error at this point
479 status = AE_AML_NAME_NOT_FOUND;
483 if (ACPI_FAILURE (status)) {
484 ACPI_REPORT_NSERROR (name_string, status);
488 /* Free the namestring created above */
490 ACPI_MEM_FREE (name_string);
492 /* Check status from the lookup */
494 if (ACPI_FAILURE (status)) {
495 return_ACPI_STATUS (status);
498 /* Put the resulting object onto the current object stack */
500 status = acpi_ds_obj_stack_push (obj_desc, walk_state);
501 if (ACPI_FAILURE (status)) {
502 return_ACPI_STATUS (status);
504 ACPI_DEBUGGER_EXEC (acpi_db_display_argument_object (obj_desc, walk_state));
507 /* Check for null name case */
509 if (arg->common.aml_opcode == AML_INT_NAMEPATH_OP) {
511 * If the name is null, this means that this is an
512 * optional result parameter that was not specified
513 * in the original ASL. Create a Zero Constant for a
514 * placeholder. (Store to a constant is a Noop.)
516 opcode = AML_ZERO_OP; /* Has no arguments! */
518 ACPI_DEBUG_PRINT ((ACPI_DB_DISPATCH, "Null namepath: Arg=%p\n", arg));
521 opcode = arg->common.aml_opcode;
524 /* Get the object type of the argument */
526 op_info = acpi_ps_get_opcode_info (opcode);
527 if (op_info->object_type == ACPI_TYPE_INVALID) {
528 return_ACPI_STATUS (AE_NOT_IMPLEMENTED);
531 if (op_info->flags & AML_HAS_RETVAL) {
532 ACPI_DEBUG_PRINT ((ACPI_DB_DISPATCH,
533 "Argument previously created, already stacked \n"));
535 ACPI_DEBUGGER_EXEC (acpi_db_display_argument_object (
536 walk_state->operands [walk_state->num_operands - 1], walk_state));
539 * Use value that was already previously returned
540 * by the evaluation of this argument
542 status = acpi_ds_result_pop_from_bottom (&obj_desc, walk_state);
543 if (ACPI_FAILURE (status)) {
545 * Only error is underflow, and this indicates
546 * a missing or null operand!
548 ACPI_DEBUG_PRINT ((ACPI_DB_ERROR, "Missing or null operand, %s\n",
549 acpi_format_exception (status)));
550 return_ACPI_STATUS (status);
554 /* Create an ACPI_INTERNAL_OBJECT for the argument */
556 obj_desc = acpi_ut_create_internal_object (op_info->object_type);
558 return_ACPI_STATUS (AE_NO_MEMORY);
561 /* Initialize the new object */
563 status = acpi_ds_init_object_from_op (walk_state, arg,
565 if (ACPI_FAILURE (status)) {
566 acpi_ut_delete_object_desc (obj_desc);
567 return_ACPI_STATUS (status);
571 /* Put the operand object on the object stack */
573 status = acpi_ds_obj_stack_push (obj_desc, walk_state);
574 if (ACPI_FAILURE (status)) {
575 return_ACPI_STATUS (status);
578 ACPI_DEBUGGER_EXEC (acpi_db_display_argument_object (obj_desc, walk_state));
581 return_ACPI_STATUS (AE_OK);
585 /*******************************************************************************
587 * FUNCTION: acpi_ds_create_operands
589 * PARAMETERS: first_arg - First argument of a parser argument tree
593 * DESCRIPTION: Convert an operator's arguments from a parse tree format to
594 * namespace objects and place those argument object on the object
595 * stack in preparation for evaluation by the interpreter.
597 ******************************************************************************/
600 acpi_ds_create_operands (
601 struct acpi_walk_state *walk_state,
602 union acpi_parse_object *first_arg)
604 acpi_status status = AE_OK;
605 union acpi_parse_object *arg;
609 ACPI_FUNCTION_TRACE_PTR ("ds_create_operands", first_arg);
612 /* For all arguments in the list... */
616 status = acpi_ds_create_operand (walk_state, arg, arg_count);
617 if (ACPI_FAILURE (status)) {
621 ACPI_DEBUG_PRINT ((ACPI_DB_DISPATCH, "Arg #%d (%p) done, Arg1=%p\n",
622 arg_count, arg, first_arg));
624 /* Move on to next argument, if any */
626 arg = arg->common.next;
630 return_ACPI_STATUS (status);
635 * We must undo everything done above; meaning that we must
636 * pop everything off of the operand stack and delete those
639 (void) acpi_ds_obj_stack_pop_and_delete (arg_count, walk_state);
641 ACPI_DEBUG_PRINT ((ACPI_DB_ERROR, "While creating Arg %d - %s\n",
642 (arg_count + 1), acpi_format_exception (status)));
643 return_ACPI_STATUS (status);