[PATCH] ppc32: Fix address checking on lmw/stmw align exception
[linux-2.6/verdex.git] / drivers / acpi / dispatcher / dsutils.c
blob462c5d83e747fe558dd79fc7a224e1c0fb87bd2d
1 /*******************************************************************************
3 * Module Name: dsutils - Dispatcher utilities
5 ******************************************************************************/
7 /*
8 * Copyright (C) 2000 - 2005, R. Byron Moore
9 * All rights reserved.
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.
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.
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.
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 /*******************************************************************************
59 * FUNCTION: acpi_ds_clear_implicit_return
61 * PARAMETERS: walk_state - Current State
63 * RETURN: None.
65 * DESCRIPTION: Clear and remove a reference on an implicit return value. Used
66 * to delete "stale" return values (if enabled, the return value
67 * from every operator is saved at least momentarily, in case the
68 * parent method exits.)
70 ******************************************************************************/
72 void
73 acpi_ds_clear_implicit_return (
74 struct acpi_walk_state *walk_state)
76 ACPI_FUNCTION_NAME ("ds_clear_implicit_return");
80 * Slack must be enabled for this feature
82 if (!acpi_gbl_enable_interpreter_slack) {
83 return;
86 if (walk_state->implicit_return_obj) {
88 * Delete any "stale" implicit return. However, in
89 * complex statements, the implicit return value can be
90 * bubbled up several levels.
92 ACPI_DEBUG_PRINT ((ACPI_DB_DISPATCH,
93 "Removing reference on stale implicit return obj %p\n",
94 walk_state->implicit_return_obj));
96 acpi_ut_remove_reference (walk_state->implicit_return_obj);
97 walk_state->implicit_return_obj = NULL;
102 #ifndef ACPI_NO_METHOD_EXECUTION
104 /*******************************************************************************
106 * FUNCTION: acpi_ds_do_implicit_return
108 * PARAMETERS: return_desc - The return value
109 * walk_state - Current State
110 * add_reference - True if a reference should be added to the
111 * return object
113 * RETURN: TRUE if implicit return enabled, FALSE otherwise
115 * DESCRIPTION: Implements the optional "implicit return". We save the result
116 * of every ASL operator and control method invocation in case the
117 * parent method exit. Before storing a new return value, we
118 * delete the previous return value.
120 ******************************************************************************/
123 acpi_ds_do_implicit_return (
124 union acpi_operand_object *return_desc,
125 struct acpi_walk_state *walk_state,
126 u8 add_reference)
128 ACPI_FUNCTION_NAME ("ds_do_implicit_return");
132 * Slack must be enabled for this feature, and we must
133 * have a valid return object
135 if ((!acpi_gbl_enable_interpreter_slack) ||
136 (!return_desc)) {
137 return (FALSE);
140 ACPI_DEBUG_PRINT ((ACPI_DB_DISPATCH,
141 "Result %p will be implicitly returned; Prev=%p\n",
142 return_desc,
143 walk_state->implicit_return_obj));
146 * Delete any "stale" implicit return value first. However, in
147 * complex statements, the implicit return value can be
148 * bubbled up several levels, so we don't clear the value if it
149 * is the same as the return_desc.
151 if (walk_state->implicit_return_obj) {
152 if (walk_state->implicit_return_obj == return_desc) {
153 return (TRUE);
155 acpi_ds_clear_implicit_return (walk_state);
158 /* Save the implicit return value, add a reference if requested */
160 walk_state->implicit_return_obj = return_desc;
161 if (add_reference) {
162 acpi_ut_add_reference (return_desc);
165 return (TRUE);
169 /*******************************************************************************
171 * FUNCTION: acpi_ds_is_result_used
173 * PARAMETERS: Op - Current Op
174 * walk_state - Current State
176 * RETURN: TRUE if result is used, FALSE otherwise
178 * DESCRIPTION: Check if a result object will be used by the parent
180 ******************************************************************************/
183 acpi_ds_is_result_used (
184 union acpi_parse_object *op,
185 struct acpi_walk_state *walk_state)
187 const struct acpi_opcode_info *parent_info;
189 ACPI_FUNCTION_TRACE_PTR ("ds_is_result_used", op);
192 /* Must have both an Op and a Result Object */
194 if (!op) {
195 ACPI_DEBUG_PRINT ((ACPI_DB_ERROR, "Null Op\n"));
196 return_VALUE (TRUE);
200 * We know that this operator is not a
201 * Return() operator (would not come here.) The following code is the
202 * optional support for a so-called "implicit return". Some AML code
203 * assumes that the last value of the method is "implicitly" returned
204 * to the caller. Just save the last result as the return value.
205 * NOTE: this is optional because the ASL language does not actually
206 * support this behavior.
208 acpi_ds_do_implicit_return (walk_state->result_obj, walk_state, TRUE);
211 * Now determine if the parent will use the result
213 * If there is no parent, or the parent is a scope_op, we are executing
214 * at the method level. An executing method typically has no parent,
215 * since each method is parsed separately. A method invoked externally
216 * via execute_control_method has a scope_op as the parent.
218 if ((!op->common.parent) ||
219 (op->common.parent->common.aml_opcode == AML_SCOPE_OP)) {
220 /* No parent, the return value cannot possibly be used */
222 ACPI_DEBUG_PRINT ((ACPI_DB_DISPATCH, "At Method level, result of [%s] not used\n",
223 acpi_ps_get_opcode_name (op->common.aml_opcode)));
224 return_VALUE (FALSE);
227 /* Get info on the parent. The root_op is AML_SCOPE */
229 parent_info = acpi_ps_get_opcode_info (op->common.parent->common.aml_opcode);
230 if (parent_info->class == AML_CLASS_UNKNOWN) {
231 ACPI_DEBUG_PRINT ((ACPI_DB_ERROR, "Unknown parent opcode. Op=%p\n", op));
232 return_VALUE (FALSE);
236 * Decide what to do with the result based on the parent. If
237 * the parent opcode will not use the result, delete the object.
238 * Otherwise leave it as is, it will be deleted when it is used
239 * as an operand later.
241 switch (parent_info->class) {
242 case AML_CLASS_CONTROL:
244 switch (op->common.parent->common.aml_opcode) {
245 case AML_RETURN_OP:
247 /* Never delete the return value associated with a return opcode */
249 goto result_used;
251 case AML_IF_OP:
252 case AML_WHILE_OP:
255 * If we are executing the predicate AND this is the predicate op,
256 * we will use the return value
258 if ((walk_state->control_state->common.state == ACPI_CONTROL_PREDICATE_EXECUTING) &&
259 (walk_state->control_state->control.predicate_op == op)) {
260 goto result_used;
262 break;
264 default:
265 /* Ignore other control opcodes */
266 break;
269 /* The general control opcode returns no result */
271 goto result_not_used;
274 case AML_CLASS_CREATE:
277 * These opcodes allow term_arg(s) as operands and therefore
278 * the operands can be method calls. The result is used.
280 goto result_used;
283 case AML_CLASS_NAMED_OBJECT:
285 if ((op->common.parent->common.aml_opcode == AML_REGION_OP) ||
286 (op->common.parent->common.aml_opcode == AML_DATA_REGION_OP) ||
287 (op->common.parent->common.aml_opcode == AML_PACKAGE_OP) ||
288 (op->common.parent->common.aml_opcode == AML_VAR_PACKAGE_OP) ||
289 (op->common.parent->common.aml_opcode == AML_BUFFER_OP) ||
290 (op->common.parent->common.aml_opcode == AML_INT_EVAL_SUBTREE_OP)) {
292 * These opcodes allow term_arg(s) as operands and therefore
293 * the operands can be method calls. The result is used.
295 goto result_used;
298 goto result_not_used;
301 default:
304 * In all other cases. the parent will actually use the return
305 * object, so keep it.
307 goto result_used;
311 result_used:
312 ACPI_DEBUG_PRINT ((ACPI_DB_DISPATCH, "Result of [%s] used by Parent [%s] Op=%p\n",
313 acpi_ps_get_opcode_name (op->common.aml_opcode),
314 acpi_ps_get_opcode_name (op->common.parent->common.aml_opcode), op));
316 return_VALUE (TRUE);
319 result_not_used:
320 ACPI_DEBUG_PRINT ((ACPI_DB_DISPATCH, "Result of [%s] not used by Parent [%s] Op=%p\n",
321 acpi_ps_get_opcode_name (op->common.aml_opcode),
322 acpi_ps_get_opcode_name (op->common.parent->common.aml_opcode), op));
324 return_VALUE (FALSE);
328 /*******************************************************************************
330 * FUNCTION: acpi_ds_delete_result_if_not_used
332 * PARAMETERS: Op - Current parse Op
333 * result_obj - Result of the operation
334 * walk_state - Current state
336 * RETURN: Status
338 * DESCRIPTION: Used after interpretation of an opcode. If there is an internal
339 * result descriptor, check if the parent opcode will actually use
340 * this result. If not, delete the result now so that it will
341 * not become orphaned.
343 ******************************************************************************/
345 void
346 acpi_ds_delete_result_if_not_used (
347 union acpi_parse_object *op,
348 union acpi_operand_object *result_obj,
349 struct acpi_walk_state *walk_state)
351 union acpi_operand_object *obj_desc;
352 acpi_status status;
355 ACPI_FUNCTION_TRACE_PTR ("ds_delete_result_if_not_used", result_obj);
358 if (!op) {
359 ACPI_DEBUG_PRINT ((ACPI_DB_ERROR, "Null Op\n"));
360 return_VOID;
363 if (!result_obj) {
364 return_VOID;
367 if (!acpi_ds_is_result_used (op, walk_state)) {
368 /* Must pop the result stack (obj_desc should be equal to result_obj) */
370 status = acpi_ds_result_pop (&obj_desc, walk_state);
371 if (ACPI_SUCCESS (status)) {
372 acpi_ut_remove_reference (result_obj);
376 return_VOID;
380 /*******************************************************************************
382 * FUNCTION: acpi_ds_resolve_operands
384 * PARAMETERS: walk_state - Current walk state with operands on stack
386 * RETURN: Status
388 * DESCRIPTION: Resolve all operands to their values. Used to prepare
389 * arguments to a control method invocation (a call from one
390 * method to another.)
392 ******************************************************************************/
394 acpi_status
395 acpi_ds_resolve_operands (
396 struct acpi_walk_state *walk_state)
398 u32 i;
399 acpi_status status = AE_OK;
402 ACPI_FUNCTION_TRACE_PTR ("ds_resolve_operands", walk_state);
406 * Attempt to resolve each of the valid operands
407 * Method arguments are passed by reference, not by value. This means
408 * that the actual objects are passed, not copies of the objects.
410 for (i = 0; i < walk_state->num_operands; i++) {
411 status = acpi_ex_resolve_to_value (&walk_state->operands[i], walk_state);
412 if (ACPI_FAILURE (status)) {
413 break;
417 return_ACPI_STATUS (status);
421 /*******************************************************************************
423 * FUNCTION: acpi_ds_clear_operands
425 * PARAMETERS: walk_state - Current walk state with operands on stack
427 * RETURN: None
429 * DESCRIPTION: Clear all operands on the current walk state operand stack.
431 ******************************************************************************/
433 void
434 acpi_ds_clear_operands (
435 struct acpi_walk_state *walk_state)
437 u32 i;
440 ACPI_FUNCTION_TRACE_PTR ("ds_clear_operands", walk_state);
443 /* Remove a reference on each operand on the stack */
445 for (i = 0; i < walk_state->num_operands; i++) {
447 * Remove a reference to all operands, including both
448 * "Arguments" and "Targets".
450 acpi_ut_remove_reference (walk_state->operands[i]);
451 walk_state->operands[i] = NULL;
454 walk_state->num_operands = 0;
455 return_VOID;
457 #endif
460 /*******************************************************************************
462 * FUNCTION: acpi_ds_create_operand
464 * PARAMETERS: walk_state - Current walk state
465 * Arg - Parse object for the argument
466 * arg_index - Which argument (zero based)
468 * RETURN: Status
470 * DESCRIPTION: Translate a parse tree object that is an argument to an AML
471 * opcode to the equivalent interpreter object. This may include
472 * looking up a name or entering a new name into the internal
473 * namespace.
475 ******************************************************************************/
477 acpi_status
478 acpi_ds_create_operand (
479 struct acpi_walk_state *walk_state,
480 union acpi_parse_object *arg,
481 u32 arg_index)
483 acpi_status status = AE_OK;
484 char *name_string;
485 u32 name_length;
486 union acpi_operand_object *obj_desc;
487 union acpi_parse_object *parent_op;
488 u16 opcode;
489 acpi_interpreter_mode interpreter_mode;
490 const struct acpi_opcode_info *op_info;
493 ACPI_FUNCTION_TRACE_PTR ("ds_create_operand", arg);
496 /* A valid name must be looked up in the namespace */
498 if ((arg->common.aml_opcode == AML_INT_NAMEPATH_OP) &&
499 (arg->common.value.string)) {
500 ACPI_DEBUG_PRINT ((ACPI_DB_DISPATCH, "Getting a name: Arg=%p\n", arg));
502 /* Get the entire name string from the AML stream */
504 status = acpi_ex_get_name_string (ACPI_TYPE_ANY, arg->common.value.buffer,
505 &name_string, &name_length);
507 if (ACPI_FAILURE (status)) {
508 return_ACPI_STATUS (status);
511 /* All prefixes have been handled, and the name is in name_string */
514 * Special handling for buffer_field declarations. This is a deferred
515 * opcode that unfortunately defines the field name as the last
516 * parameter instead of the first. We get here when we are performing
517 * the deferred execution, so the actual name of the field is already
518 * in the namespace. We don't want to attempt to look it up again
519 * because we may be executing in a different scope than where the
520 * actual opcode exists.
522 if ((walk_state->deferred_node) &&
523 (walk_state->deferred_node->type == ACPI_TYPE_BUFFER_FIELD) &&
524 (arg_index != 0)) {
525 obj_desc = ACPI_CAST_PTR (union acpi_operand_object, walk_state->deferred_node);
526 status = AE_OK;
528 else /* All other opcodes */ {
530 * Differentiate between a namespace "create" operation
531 * versus a "lookup" operation (IMODE_LOAD_PASS2 vs.
532 * IMODE_EXECUTE) in order to support the creation of
533 * namespace objects during the execution of control methods.
535 parent_op = arg->common.parent;
536 op_info = acpi_ps_get_opcode_info (parent_op->common.aml_opcode);
537 if ((op_info->flags & AML_NSNODE) &&
538 (parent_op->common.aml_opcode != AML_INT_METHODCALL_OP) &&
539 (parent_op->common.aml_opcode != AML_REGION_OP) &&
540 (parent_op->common.aml_opcode != AML_INT_NAMEPATH_OP)) {
541 /* Enter name into namespace if not found */
543 interpreter_mode = ACPI_IMODE_LOAD_PASS2;
545 else {
546 /* Return a failure if name not found */
548 interpreter_mode = ACPI_IMODE_EXECUTE;
551 status = acpi_ns_lookup (walk_state->scope_info, name_string,
552 ACPI_TYPE_ANY, interpreter_mode,
553 ACPI_NS_SEARCH_PARENT | ACPI_NS_DONT_OPEN_SCOPE,
554 walk_state,
555 ACPI_CAST_INDIRECT_PTR (struct acpi_namespace_node, &obj_desc));
557 * The only case where we pass through (ignore) a NOT_FOUND
558 * error is for the cond_ref_of opcode.
560 if (status == AE_NOT_FOUND) {
561 if (parent_op->common.aml_opcode == AML_COND_REF_OF_OP) {
563 * For the Conditional Reference op, it's OK if
564 * the name is not found; We just need a way to
565 * indicate this to the interpreter, set the
566 * object to the root
568 obj_desc = ACPI_CAST_PTR (union acpi_operand_object, acpi_gbl_root_node);
569 status = AE_OK;
571 else {
573 * We just plain didn't find it -- which is a
574 * very serious error at this point
576 status = AE_AML_NAME_NOT_FOUND;
580 if (ACPI_FAILURE (status)) {
581 ACPI_REPORT_NSERROR (name_string, status);
585 /* Free the namestring created above */
587 ACPI_MEM_FREE (name_string);
589 /* Check status from the lookup */
591 if (ACPI_FAILURE (status)) {
592 return_ACPI_STATUS (status);
595 /* Put the resulting object onto the current object stack */
597 status = acpi_ds_obj_stack_push (obj_desc, walk_state);
598 if (ACPI_FAILURE (status)) {
599 return_ACPI_STATUS (status);
601 ACPI_DEBUGGER_EXEC (acpi_db_display_argument_object (obj_desc, walk_state));
603 else {
604 /* Check for null name case */
606 if (arg->common.aml_opcode == AML_INT_NAMEPATH_OP) {
608 * If the name is null, this means that this is an
609 * optional result parameter that was not specified
610 * in the original ASL. Create a Zero Constant for a
611 * placeholder. (Store to a constant is a Noop.)
613 opcode = AML_ZERO_OP; /* Has no arguments! */
615 ACPI_DEBUG_PRINT ((ACPI_DB_DISPATCH, "Null namepath: Arg=%p\n", arg));
617 else {
618 opcode = arg->common.aml_opcode;
621 /* Get the object type of the argument */
623 op_info = acpi_ps_get_opcode_info (opcode);
624 if (op_info->object_type == ACPI_TYPE_INVALID) {
625 return_ACPI_STATUS (AE_NOT_IMPLEMENTED);
628 if (op_info->flags & AML_HAS_RETVAL) {
629 ACPI_DEBUG_PRINT ((ACPI_DB_DISPATCH,
630 "Argument previously created, already stacked \n"));
632 ACPI_DEBUGGER_EXEC (acpi_db_display_argument_object (
633 walk_state->operands [walk_state->num_operands - 1], walk_state));
636 * Use value that was already previously returned
637 * by the evaluation of this argument
639 status = acpi_ds_result_pop_from_bottom (&obj_desc, walk_state);
640 if (ACPI_FAILURE (status)) {
642 * Only error is underflow, and this indicates
643 * a missing or null operand!
645 ACPI_DEBUG_PRINT ((ACPI_DB_ERROR, "Missing or null operand, %s\n",
646 acpi_format_exception (status)));
647 return_ACPI_STATUS (status);
650 else {
651 /* Create an ACPI_INTERNAL_OBJECT for the argument */
653 obj_desc = acpi_ut_create_internal_object (op_info->object_type);
654 if (!obj_desc) {
655 return_ACPI_STATUS (AE_NO_MEMORY);
658 /* Initialize the new object */
660 status = acpi_ds_init_object_from_op (walk_state, arg,
661 opcode, &obj_desc);
662 if (ACPI_FAILURE (status)) {
663 acpi_ut_delete_object_desc (obj_desc);
664 return_ACPI_STATUS (status);
668 /* Put the operand object on the object stack */
670 status = acpi_ds_obj_stack_push (obj_desc, walk_state);
671 if (ACPI_FAILURE (status)) {
672 return_ACPI_STATUS (status);
675 ACPI_DEBUGGER_EXEC (acpi_db_display_argument_object (obj_desc, walk_state));
678 return_ACPI_STATUS (AE_OK);
682 /*******************************************************************************
684 * FUNCTION: acpi_ds_create_operands
686 * PARAMETERS: walk_state - Current state
687 * first_arg - First argument of a parser argument tree
689 * RETURN: Status
691 * DESCRIPTION: Convert an operator's arguments from a parse tree format to
692 * namespace objects and place those argument object on the object
693 * stack in preparation for evaluation by the interpreter.
695 ******************************************************************************/
697 acpi_status
698 acpi_ds_create_operands (
699 struct acpi_walk_state *walk_state,
700 union acpi_parse_object *first_arg)
702 acpi_status status = AE_OK;
703 union acpi_parse_object *arg;
704 u32 arg_count = 0;
707 ACPI_FUNCTION_TRACE_PTR ("ds_create_operands", first_arg);
710 /* For all arguments in the list... */
712 arg = first_arg;
713 while (arg) {
714 status = acpi_ds_create_operand (walk_state, arg, arg_count);
715 if (ACPI_FAILURE (status)) {
716 goto cleanup;
719 ACPI_DEBUG_PRINT ((ACPI_DB_DISPATCH, "Arg #%d (%p) done, Arg1=%p\n",
720 arg_count, arg, first_arg));
722 /* Move on to next argument, if any */
724 arg = arg->common.next;
725 arg_count++;
728 return_ACPI_STATUS (status);
731 cleanup:
733 * We must undo everything done above; meaning that we must
734 * pop everything off of the operand stack and delete those
735 * objects
737 (void) acpi_ds_obj_stack_pop_and_delete (arg_count, walk_state);
739 ACPI_DEBUG_PRINT ((ACPI_DB_ERROR, "While creating Arg %d - %s\n",
740 (arg_count + 1), acpi_format_exception (status)));
741 return_ACPI_STATUS (status);