1 /*******************************************************************************
3 * Module Name: dsutils - Dispatcher utilities
5 ******************************************************************************/
8 * Copyright (C) 2000 - 2004, 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
69 * DESCRIPTION: Check if a result object will be used by the parent
71 ******************************************************************************/
74 acpi_ds_is_result_used (
75 union acpi_parse_object *op,
76 struct acpi_walk_state *walk_state)
78 const struct acpi_opcode_info *parent_info;
81 ACPI_FUNCTION_TRACE_PTR ("ds_is_result_used", op);
84 /* Must have both an Op and a Result Object */
87 ACPI_DEBUG_PRINT ((ACPI_DB_ERROR, "Null Op\n"));
92 * If there is no parent, the result can't possibly be used!
93 * (An executing method typically has no parent, since each
94 * method is parsed separately) However, a method that is
95 * invoked from another method has a parent.
97 if (!op->common.parent) {
102 * Get info on the parent. The root Op is AML_SCOPE
104 parent_info = acpi_ps_get_opcode_info (op->common.parent->common.aml_opcode);
105 if (parent_info->class == AML_CLASS_UNKNOWN) {
106 ACPI_DEBUG_PRINT ((ACPI_DB_ERROR, "Unknown parent opcode. Op=%p\n", op));
107 return_VALUE (FALSE);
111 * Decide what to do with the result based on the parent. If
112 * the parent opcode will not use the result, delete the object.
113 * Otherwise leave it as is, it will be deleted when it is used
114 * as an operand later.
116 switch (parent_info->class) {
117 case AML_CLASS_CONTROL:
119 switch (op->common.parent->common.aml_opcode) {
122 /* Never delete the return value associated with a return opcode */
130 * If we are executing the predicate AND this is the predicate op,
131 * we will use the return value
133 if ((walk_state->control_state->common.state == ACPI_CONTROL_PREDICATE_EXECUTING) &&
134 (walk_state->control_state->control.predicate_op == op)) {
140 /* Ignore other control opcodes */
144 /* The general control opcode returns no result */
146 goto result_not_used;
149 case AML_CLASS_CREATE:
152 * These opcodes allow term_arg(s) as operands and therefore
153 * the operands can be method calls. The result is used.
158 case AML_CLASS_NAMED_OBJECT:
160 if ((op->common.parent->common.aml_opcode == AML_REGION_OP) ||
161 (op->common.parent->common.aml_opcode == AML_DATA_REGION_OP) ||
162 (op->common.parent->common.aml_opcode == AML_PACKAGE_OP) ||
163 (op->common.parent->common.aml_opcode == AML_VAR_PACKAGE_OP) ||
164 (op->common.parent->common.aml_opcode == AML_BUFFER_OP) ||
165 (op->common.parent->common.aml_opcode == AML_INT_EVAL_SUBTREE_OP)) {
167 * These opcodes allow term_arg(s) as operands and therefore
168 * the operands can be method calls. The result is used.
173 goto result_not_used;
179 * In all other cases. the parent will actually use the return
180 * object, so keep it.
187 ACPI_DEBUG_PRINT ((ACPI_DB_DISPATCH, "Result of [%s] used by Parent [%s] Op=%p\n",
188 acpi_ps_get_opcode_name (op->common.aml_opcode),
189 acpi_ps_get_opcode_name (op->common.parent->common.aml_opcode), op));
195 ACPI_DEBUG_PRINT ((ACPI_DB_DISPATCH, "Result of [%s] not used by Parent [%s] Op=%p\n",
196 acpi_ps_get_opcode_name (op->common.aml_opcode),
197 acpi_ps_get_opcode_name (op->common.parent->common.aml_opcode), op));
199 return_VALUE (FALSE);
203 /*******************************************************************************
205 * FUNCTION: acpi_ds_delete_result_if_not_used
213 * DESCRIPTION: Used after interpretation of an opcode. If there is an internal
214 * result descriptor, check if the parent opcode will actually use
215 * this result. If not, delete the result now so that it will
216 * not become orphaned.
218 ******************************************************************************/
221 acpi_ds_delete_result_if_not_used (
222 union acpi_parse_object *op,
223 union acpi_operand_object *result_obj,
224 struct acpi_walk_state *walk_state)
226 union acpi_operand_object *obj_desc;
230 ACPI_FUNCTION_TRACE_PTR ("ds_delete_result_if_not_used", result_obj);
234 ACPI_DEBUG_PRINT ((ACPI_DB_ERROR, "Null Op\n"));
242 if (!acpi_ds_is_result_used (op, walk_state)) {
244 * Must pop the result stack (obj_desc should be equal to result_obj)
246 status = acpi_ds_result_pop (&obj_desc, walk_state);
247 if (ACPI_SUCCESS (status)) {
248 acpi_ut_remove_reference (result_obj);
256 /*******************************************************************************
258 * FUNCTION: acpi_ds_resolve_operands
260 * PARAMETERS: walk_state - Current walk state with operands on stack
264 * DESCRIPTION: Resolve all operands to their values. Used to prepare
265 * arguments to a control method invocation (a call from one
266 * method to another.)
268 ******************************************************************************/
271 acpi_ds_resolve_operands (
272 struct acpi_walk_state *walk_state)
275 acpi_status status = AE_OK;
278 ACPI_FUNCTION_TRACE_PTR ("ds_resolve_operands", walk_state);
282 * Attempt to resolve each of the valid operands
283 * Method arguments are passed by reference, not by value. This means
284 * that the actual objects are passed, not copies of the objects.
286 for (i = 0; i < walk_state->num_operands; i++) {
287 status = acpi_ex_resolve_to_value (&walk_state->operands[i], walk_state);
288 if (ACPI_FAILURE (status)) {
293 return_ACPI_STATUS (status);
297 /*******************************************************************************
299 * FUNCTION: acpi_ds_clear_operands
301 * PARAMETERS: walk_state - Current walk state with operands on stack
305 * DESCRIPTION: Clear all operands on the current walk state operand stack.
307 ******************************************************************************/
310 acpi_ds_clear_operands (
311 struct acpi_walk_state *walk_state)
316 ACPI_FUNCTION_TRACE_PTR ("acpi_ds_clear_operands", walk_state);
320 * Remove a reference on each operand on the stack
322 for (i = 0; i < walk_state->num_operands; i++) {
324 * Remove a reference to all operands, including both
325 * "Arguments" and "Targets".
327 acpi_ut_remove_reference (walk_state->operands[i]);
328 walk_state->operands[i] = NULL;
331 walk_state->num_operands = 0;
337 /*******************************************************************************
339 * FUNCTION: acpi_ds_create_operand
341 * PARAMETERS: walk_state
346 * DESCRIPTION: Translate a parse tree object that is an argument to an AML
347 * opcode to the equivalent interpreter object. This may include
348 * looking up a name or entering a new name into the internal
351 ******************************************************************************/
354 acpi_ds_create_operand (
355 struct acpi_walk_state *walk_state,
356 union acpi_parse_object *arg,
359 acpi_status status = AE_OK;
362 union acpi_operand_object *obj_desc;
363 union acpi_parse_object *parent_op;
365 acpi_interpreter_mode interpreter_mode;
366 const struct acpi_opcode_info *op_info;
369 ACPI_FUNCTION_TRACE_PTR ("ds_create_operand", arg);
372 /* A valid name must be looked up in the namespace */
374 if ((arg->common.aml_opcode == AML_INT_NAMEPATH_OP) &&
375 (arg->common.value.string)) {
376 ACPI_DEBUG_PRINT ((ACPI_DB_DISPATCH, "Getting a name: Arg=%p\n", arg));
378 /* Get the entire name string from the AML stream */
380 status = acpi_ex_get_name_string (ACPI_TYPE_ANY, arg->common.value.buffer,
381 &name_string, &name_length);
383 if (ACPI_FAILURE (status)) {
384 return_ACPI_STATUS (status);
388 * All prefixes have been handled, and the name is
394 * Special handling for buffer_field declarations. This is a deferred
395 * opcode that unfortunately defines the field name as the last
396 * parameter instead of the first. We get here when we are performing
397 * the deferred execution, so the actual name of the field is already
398 * in the namespace. We don't want to attempt to look it up again
399 * because we may be executing in a different scope than where the
400 * actual opcode exists.
402 if ((walk_state->deferred_node) &&
403 (walk_state->deferred_node->type == ACPI_TYPE_BUFFER_FIELD) &&
405 obj_desc = ACPI_CAST_PTR (union acpi_operand_object, walk_state->deferred_node);
408 else /* All other opcodes */ {
410 * Differentiate between a namespace "create" operation
411 * versus a "lookup" operation (IMODE_LOAD_PASS2 vs.
412 * IMODE_EXECUTE) in order to support the creation of
413 * namespace objects during the execution of control methods.
415 parent_op = arg->common.parent;
416 op_info = acpi_ps_get_opcode_info (parent_op->common.aml_opcode);
417 if ((op_info->flags & AML_NSNODE) &&
418 (parent_op->common.aml_opcode != AML_INT_METHODCALL_OP) &&
419 (parent_op->common.aml_opcode != AML_REGION_OP) &&
420 (parent_op->common.aml_opcode != AML_INT_NAMEPATH_OP)) {
421 /* Enter name into namespace if not found */
423 interpreter_mode = ACPI_IMODE_LOAD_PASS2;
426 /* Return a failure if name not found */
428 interpreter_mode = ACPI_IMODE_EXECUTE;
431 status = acpi_ns_lookup (walk_state->scope_info, name_string,
432 ACPI_TYPE_ANY, interpreter_mode,
433 ACPI_NS_SEARCH_PARENT | ACPI_NS_DONT_OPEN_SCOPE,
435 ACPI_CAST_INDIRECT_PTR (struct acpi_namespace_node, &obj_desc));
437 * The only case where we pass through (ignore) a NOT_FOUND
438 * error is for the cond_ref_of opcode.
440 if (status == AE_NOT_FOUND) {
441 if (parent_op->common.aml_opcode == AML_COND_REF_OF_OP) {
443 * For the Conditional Reference op, it's OK if
444 * the name is not found; We just need a way to
445 * indicate this to the interpreter, set the
448 obj_desc = ACPI_CAST_PTR (union acpi_operand_object, acpi_gbl_root_node);
453 * We just plain didn't find it -- which is a
454 * very serious error at this point
456 status = AE_AML_NAME_NOT_FOUND;
460 if (ACPI_FAILURE (status)) {
461 ACPI_REPORT_NSERROR (name_string, status);
465 /* Free the namestring created above */
467 ACPI_MEM_FREE (name_string);
469 /* Check status from the lookup */
471 if (ACPI_FAILURE (status)) {
472 return_ACPI_STATUS (status);
475 /* Put the resulting object onto the current object stack */
477 status = acpi_ds_obj_stack_push (obj_desc, walk_state);
478 if (ACPI_FAILURE (status)) {
479 return_ACPI_STATUS (status);
481 ACPI_DEBUGGER_EXEC (acpi_db_display_argument_object (obj_desc, walk_state));
484 /* Check for null name case */
486 if (arg->common.aml_opcode == AML_INT_NAMEPATH_OP) {
488 * If the name is null, this means that this is an
489 * optional result parameter that was not specified
490 * in the original ASL. Create a Zero Constant for a
491 * placeholder. (Store to a constant is a Noop.)
493 opcode = AML_ZERO_OP; /* Has no arguments! */
495 ACPI_DEBUG_PRINT ((ACPI_DB_DISPATCH, "Null namepath: Arg=%p\n", arg));
498 opcode = arg->common.aml_opcode;
501 /* Get the object type of the argument */
503 op_info = acpi_ps_get_opcode_info (opcode);
504 if (op_info->object_type == ACPI_TYPE_INVALID) {
505 return_ACPI_STATUS (AE_NOT_IMPLEMENTED);
508 if (op_info->flags & AML_HAS_RETVAL) {
509 ACPI_DEBUG_PRINT ((ACPI_DB_DISPATCH,
510 "Argument previously created, already stacked \n"));
512 ACPI_DEBUGGER_EXEC (acpi_db_display_argument_object (
513 walk_state->operands [walk_state->num_operands - 1], walk_state));
516 * Use value that was already previously returned
517 * by the evaluation of this argument
519 status = acpi_ds_result_pop_from_bottom (&obj_desc, walk_state);
520 if (ACPI_FAILURE (status)) {
522 * Only error is underflow, and this indicates
523 * a missing or null operand!
525 ACPI_DEBUG_PRINT ((ACPI_DB_ERROR, "Missing or null operand, %s\n",
526 acpi_format_exception (status)));
527 return_ACPI_STATUS (status);
531 /* Create an ACPI_INTERNAL_OBJECT for the argument */
533 obj_desc = acpi_ut_create_internal_object (op_info->object_type);
535 return_ACPI_STATUS (AE_NO_MEMORY);
538 /* Initialize the new object */
540 status = acpi_ds_init_object_from_op (walk_state, arg,
542 if (ACPI_FAILURE (status)) {
543 acpi_ut_delete_object_desc (obj_desc);
544 return_ACPI_STATUS (status);
548 /* Put the operand object on the object stack */
550 status = acpi_ds_obj_stack_push (obj_desc, walk_state);
551 if (ACPI_FAILURE (status)) {
552 return_ACPI_STATUS (status);
555 ACPI_DEBUGGER_EXEC (acpi_db_display_argument_object (obj_desc, walk_state));
558 return_ACPI_STATUS (AE_OK);
562 /*******************************************************************************
564 * FUNCTION: acpi_ds_create_operands
566 * PARAMETERS: first_arg - First argument of a parser argument tree
570 * DESCRIPTION: Convert an operator's arguments from a parse tree format to
571 * namespace objects and place those argument object on the object
572 * stack in preparation for evaluation by the interpreter.
574 ******************************************************************************/
577 acpi_ds_create_operands (
578 struct acpi_walk_state *walk_state,
579 union acpi_parse_object *first_arg)
581 acpi_status status = AE_OK;
582 union acpi_parse_object *arg;
586 ACPI_FUNCTION_TRACE_PTR ("ds_create_operands", first_arg);
589 /* For all arguments in the list... */
593 status = acpi_ds_create_operand (walk_state, arg, arg_count);
594 if (ACPI_FAILURE (status)) {
598 ACPI_DEBUG_PRINT ((ACPI_DB_DISPATCH, "Arg #%d (%p) done, Arg1=%p\n",
599 arg_count, arg, first_arg));
601 /* Move on to next argument, if any */
603 arg = arg->common.next;
607 return_ACPI_STATUS (status);
612 * We must undo everything done above; meaning that we must
613 * pop everything off of the operand stack and delete those
616 (void) acpi_ds_obj_stack_pop_and_delete (arg_count, walk_state);
618 ACPI_DEBUG_PRINT ((ACPI_DB_ERROR, "While creating Arg %d - %s\n",
619 (arg_count + 1), acpi_format_exception (status)));
620 return_ACPI_STATUS (status);