vserver 1.9.5.x5
[linux-2.6.git] / drivers / acpi / dispatcher / dsutils.c
1 /*******************************************************************************
2  *
3  * Module Name: dsutils - Dispatcher utilities
4  *
5  ******************************************************************************/
6
7 /*
8  * Copyright (C) 2000 - 2005, R. Byron Moore
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 #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>
52
53 #define _COMPONENT          ACPI_DISPATCHER
54          ACPI_MODULE_NAME    ("dsutils")
55
56
57 #ifndef ACPI_NO_METHOD_EXECUTION
58
59 /*******************************************************************************
60  *
61  * FUNCTION:    acpi_ds_is_result_used
62  *
63  * PARAMETERS:  Op                  - Current Op
64  *              walk_state          - Current State
65  *
66  * RETURN:      TRUE if result is used, FALSE otherwise
67  *
68  * DESCRIPTION: Check if a result object will be used by the parent
69  *
70  ******************************************************************************/
71
72 u8
73 acpi_ds_is_result_used (
74         union acpi_parse_object         *op,
75         struct acpi_walk_state          *walk_state)
76 {
77         const struct acpi_opcode_info   *parent_info;
78
79
80         ACPI_FUNCTION_TRACE_PTR ("ds_is_result_used", op);
81
82
83         /* Must have both an Op and a Result Object */
84
85         if (!op) {
86                 ACPI_DEBUG_PRINT ((ACPI_DB_ERROR, "Null Op\n"));
87                 return_VALUE (TRUE);
88         }
89
90         /*
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.
95          */
96         if ((!op->common.parent) ||
97                 (op->common.parent->common.aml_opcode == AML_SCOPE_OP)) {
98                 /*
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.
106                  */
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)));
112
113                         /* Use the top of the result stack as the implicit return value */
114
115                         walk_state->return_desc = walk_state->results->results.obj_desc[0];
116                         return_VALUE (TRUE);
117                 }
118
119                 /* No parent, the return value cannot possibly be used */
120
121                 return_VALUE (FALSE);
122         }
123
124         /* Get info on the parent. The root_op is AML_SCOPE */
125
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);
130         }
131
132         /*
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.
137          */
138         switch (parent_info->class) {
139         case AML_CLASS_CONTROL:
140
141                 switch (op->common.parent->common.aml_opcode) {
142                 case AML_RETURN_OP:
143
144                         /* Never delete the return value associated with a return opcode */
145
146                         goto result_used;
147
148                 case AML_IF_OP:
149                 case AML_WHILE_OP:
150
151                         /*
152                          * If we are executing the predicate AND this is the predicate op,
153                          * we will use the return value
154                          */
155                         if ((walk_state->control_state->common.state == ACPI_CONTROL_PREDICATE_EXECUTING) &&
156                                 (walk_state->control_state->control.predicate_op == op)) {
157                                 goto result_used;
158                         }
159                         break;
160
161                 default:
162                         /* Ignore other control opcodes */
163                         break;
164                 }
165
166                 /* The general control opcode returns no result */
167
168                 goto result_not_used;
169
170
171         case AML_CLASS_CREATE:
172
173                 /*
174                  * These opcodes allow term_arg(s) as operands and therefore
175                  * the operands can be method calls.  The result is used.
176                  */
177                 goto result_used;
178
179
180         case AML_CLASS_NAMED_OBJECT:
181
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)) {
188                         /*
189                          * These opcodes allow term_arg(s) as operands and therefore
190                          * the operands can be method calls.  The result is used.
191                          */
192                         goto result_used;
193                 }
194
195                 goto result_not_used;
196
197
198         default:
199
200                 /*
201                  * In all other cases. the parent will actually use the return
202                  * object, so keep it.
203                  */
204                 goto result_used;
205         }
206
207
208 result_used:
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));
212
213         return_VALUE (TRUE);
214
215
216 result_not_used:
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));
220
221         return_VALUE (FALSE);
222 }
223
224
225 /*******************************************************************************
226  *
227  * FUNCTION:    acpi_ds_delete_result_if_not_used
228  *
229  * PARAMETERS:  Op              - Current parse Op
230  *              result_obj      - Result of the operation
231  *              walk_state      - Current state
232  *
233  * RETURN:      Status
234  *
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.
239  *
240  ******************************************************************************/
241
242 void
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)
247 {
248         union acpi_operand_object       *obj_desc;
249         acpi_status                     status;
250
251
252         ACPI_FUNCTION_TRACE_PTR ("ds_delete_result_if_not_used", result_obj);
253
254
255         if (!op) {
256                 ACPI_DEBUG_PRINT ((ACPI_DB_ERROR, "Null Op\n"));
257                 return_VOID;
258         }
259
260         if (!result_obj) {
261                 return_VOID;
262         }
263
264         if (!acpi_ds_is_result_used (op, walk_state)) {
265                 /*
266                  * Must pop the result stack (obj_desc should be equal to result_obj)
267                  */
268                 status = acpi_ds_result_pop (&obj_desc, walk_state);
269                 if (ACPI_SUCCESS (status)) {
270                         acpi_ut_remove_reference (result_obj);
271                 }
272         }
273
274         return_VOID;
275 }
276
277
278 /*******************************************************************************
279  *
280  * FUNCTION:    acpi_ds_resolve_operands
281  *
282  * PARAMETERS:  walk_state          - Current walk state with operands on stack
283  *
284  * RETURN:      Status
285  *
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.)
289  *
290  ******************************************************************************/
291
292 acpi_status
293 acpi_ds_resolve_operands (
294         struct acpi_walk_state          *walk_state)
295 {
296         u32                             i;
297         acpi_status                     status = AE_OK;
298
299
300         ACPI_FUNCTION_TRACE_PTR ("ds_resolve_operands", walk_state);
301
302
303         /*
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.
307          */
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)) {
311                         break;
312                 }
313         }
314
315         return_ACPI_STATUS (status);
316 }
317
318
319 /*******************************************************************************
320  *
321  * FUNCTION:    acpi_ds_clear_operands
322  *
323  * PARAMETERS:  walk_state          - Current walk state with operands on stack
324  *
325  * RETURN:      None
326  *
327  * DESCRIPTION: Clear all operands on the current walk state operand stack.
328  *
329  ******************************************************************************/
330
331 void
332 acpi_ds_clear_operands (
333         struct acpi_walk_state          *walk_state)
334 {
335         u32                             i;
336
337
338         ACPI_FUNCTION_TRACE_PTR ("ds_clear_operands", walk_state);
339
340
341         /*
342          * Remove a reference on each operand on the stack
343          */
344         for (i = 0; i < walk_state->num_operands; i++) {
345                 /*
346                  * Remove a reference to all operands, including both
347                  * "Arguments" and "Targets".
348                  */
349                 acpi_ut_remove_reference (walk_state->operands[i]);
350                 walk_state->operands[i] = NULL;
351         }
352
353         walk_state->num_operands = 0;
354         return_VOID;
355 }
356 #endif
357
358
359 /*******************************************************************************
360  *
361  * FUNCTION:    acpi_ds_create_operand
362  *
363  * PARAMETERS:  walk_state      - Current walk state
364  *              Arg             - Parse object for the argument
365  *              arg_index       - Which argument (zero based)
366  *
367  * RETURN:      Status
368  *
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
372  *              namespace.
373  *
374  ******************************************************************************/
375
376 acpi_status
377 acpi_ds_create_operand (
378         struct acpi_walk_state          *walk_state,
379         union acpi_parse_object         *arg,
380         u32                             arg_index)
381 {
382         acpi_status                     status = AE_OK;
383         char                            *name_string;
384         u32                             name_length;
385         union acpi_operand_object       *obj_desc;
386         union acpi_parse_object         *parent_op;
387         u16                             opcode;
388         acpi_interpreter_mode           interpreter_mode;
389         const struct acpi_opcode_info   *op_info;
390
391
392         ACPI_FUNCTION_TRACE_PTR ("ds_create_operand", arg);
393
394
395         /* A valid name must be looked up in the namespace */
396
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));
400
401                 /* Get the entire name string from the AML stream */
402
403                 status = acpi_ex_get_name_string (ACPI_TYPE_ANY, arg->common.value.buffer,
404                                   &name_string, &name_length);
405
406                 if (ACPI_FAILURE (status)) {
407                         return_ACPI_STATUS (status);
408                 }
409
410                 /*
411                  * All prefixes have been handled, and the name is
412                  * in name_string
413                  */
414
415
416                 /*
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.
424                  */
425                 if ((walk_state->deferred_node) &&
426                         (walk_state->deferred_node->type == ACPI_TYPE_BUFFER_FIELD) &&
427                         (arg_index != 0)) {
428                         obj_desc = ACPI_CAST_PTR (union acpi_operand_object, walk_state->deferred_node);
429                         status = AE_OK;
430                 }
431                 else    /* All other opcodes */ {
432                         /*
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.
437                          */
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 */
445
446                                 interpreter_mode = ACPI_IMODE_LOAD_PASS2;
447                         }
448                         else {
449                                 /* Return a failure if name not found */
450
451                                 interpreter_mode = ACPI_IMODE_EXECUTE;
452                         }
453
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,
457                                          walk_state,
458                                          ACPI_CAST_INDIRECT_PTR (struct acpi_namespace_node, &obj_desc));
459                         /*
460                          * The only case where we pass through (ignore) a NOT_FOUND
461                          * error is for the cond_ref_of opcode.
462                          */
463                         if (status == AE_NOT_FOUND) {
464                                 if (parent_op->common.aml_opcode == AML_COND_REF_OF_OP) {
465                                         /*
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
469                                          * object to the root
470                                          */
471                                         obj_desc = ACPI_CAST_PTR (union acpi_operand_object, acpi_gbl_root_node);
472                                         status = AE_OK;
473                                 }
474                                 else {
475                                         /*
476                                          * We just plain didn't find it -- which is a
477                                          * very serious error at this point
478                                          */
479                                         status = AE_AML_NAME_NOT_FOUND;
480                                 }
481                         }
482
483                         if (ACPI_FAILURE (status)) {
484                                 ACPI_REPORT_NSERROR (name_string, status);
485                         }
486                 }
487
488                 /* Free the namestring created above */
489
490                 ACPI_MEM_FREE (name_string);
491
492                 /* Check status from the lookup */
493
494                 if (ACPI_FAILURE (status)) {
495                         return_ACPI_STATUS (status);
496                 }
497
498                 /* Put the resulting object onto the current object stack */
499
500                 status = acpi_ds_obj_stack_push (obj_desc, walk_state);
501                 if (ACPI_FAILURE (status)) {
502                         return_ACPI_STATUS (status);
503                 }
504                 ACPI_DEBUGGER_EXEC (acpi_db_display_argument_object (obj_desc, walk_state));
505         }
506         else {
507                 /* Check for null name case */
508
509                 if (arg->common.aml_opcode == AML_INT_NAMEPATH_OP) {
510                         /*
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.)
515                          */
516                         opcode = AML_ZERO_OP;       /* Has no arguments! */
517
518                         ACPI_DEBUG_PRINT ((ACPI_DB_DISPATCH, "Null namepath: Arg=%p\n", arg));
519                 }
520                 else {
521                         opcode = arg->common.aml_opcode;
522                 }
523
524                 /* Get the object type of the argument */
525
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);
529                 }
530
531                 if (op_info->flags & AML_HAS_RETVAL) {
532                         ACPI_DEBUG_PRINT ((ACPI_DB_DISPATCH,
533                                 "Argument previously created, already stacked \n"));
534
535                         ACPI_DEBUGGER_EXEC (acpi_db_display_argument_object (
536                                 walk_state->operands [walk_state->num_operands - 1], walk_state));
537
538                         /*
539                          * Use value that was already previously returned
540                          * by the evaluation of this argument
541                          */
542                         status = acpi_ds_result_pop_from_bottom (&obj_desc, walk_state);
543                         if (ACPI_FAILURE (status)) {
544                                 /*
545                                  * Only error is underflow, and this indicates
546                                  * a missing or null operand!
547                                  */
548                                 ACPI_DEBUG_PRINT ((ACPI_DB_ERROR, "Missing or null operand, %s\n",
549                                         acpi_format_exception (status)));
550                                 return_ACPI_STATUS (status);
551                         }
552                 }
553                 else {
554                         /* Create an ACPI_INTERNAL_OBJECT for the argument */
555
556                         obj_desc = acpi_ut_create_internal_object (op_info->object_type);
557                         if (!obj_desc) {
558                                 return_ACPI_STATUS (AE_NO_MEMORY);
559                         }
560
561                         /* Initialize the new object */
562
563                         status = acpi_ds_init_object_from_op (walk_state, arg,
564                                          opcode, &obj_desc);
565                         if (ACPI_FAILURE (status)) {
566                                 acpi_ut_delete_object_desc (obj_desc);
567                                 return_ACPI_STATUS (status);
568                         }
569                 }
570
571                 /* Put the operand object on the object stack */
572
573                 status = acpi_ds_obj_stack_push (obj_desc, walk_state);
574                 if (ACPI_FAILURE (status)) {
575                         return_ACPI_STATUS (status);
576                 }
577
578                 ACPI_DEBUGGER_EXEC (acpi_db_display_argument_object (obj_desc, walk_state));
579         }
580
581         return_ACPI_STATUS (AE_OK);
582 }
583
584
585 /*******************************************************************************
586  *
587  * FUNCTION:    acpi_ds_create_operands
588  *
589  * PARAMETERS:  first_arg           - First argument of a parser argument tree
590  *
591  * RETURN:      Status
592  *
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.
596  *
597  ******************************************************************************/
598
599 acpi_status
600 acpi_ds_create_operands (
601         struct acpi_walk_state          *walk_state,
602         union acpi_parse_object         *first_arg)
603 {
604         acpi_status                     status = AE_OK;
605         union acpi_parse_object         *arg;
606         u32                             arg_count = 0;
607
608
609         ACPI_FUNCTION_TRACE_PTR ("ds_create_operands", first_arg);
610
611
612         /* For all arguments in the list... */
613
614         arg = first_arg;
615         while (arg) {
616                 status = acpi_ds_create_operand (walk_state, arg, arg_count);
617                 if (ACPI_FAILURE (status)) {
618                         goto cleanup;
619                 }
620
621                 ACPI_DEBUG_PRINT ((ACPI_DB_DISPATCH, "Arg #%d (%p) done, Arg1=%p\n",
622                         arg_count, arg, first_arg));
623
624                 /* Move on to next argument, if any */
625
626                 arg = arg->common.next;
627                 arg_count++;
628         }
629
630         return_ACPI_STATUS (status);
631
632
633 cleanup:
634         /*
635          * We must undo everything done above; meaning that we must
636          * pop everything off of the operand stack and delete those
637          * objects
638          */
639         (void) acpi_ds_obj_stack_pop_and_delete (arg_count, walk_state);
640
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);
644 }
645
646