ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / drivers / acpi / dispatcher / dswexec.c
1 /******************************************************************************
2  *
3  * Module Name: dswexec - Dispatcher method execution callbacks;
4  *                        dispatch to interpreter.
5  *
6  *****************************************************************************/
7
8 /*
9  * Copyright (C) 2000 - 2004, R. Byron Moore
10  * All rights reserved.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions, and the following disclaimer,
17  *    without modification.
18  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
19  *    substantially similar to the "NO WARRANTY" disclaimer below
20  *    ("Disclaimer") and any redistribution must be conditioned upon
21  *    including a substantially similar Disclaimer requirement for further
22  *    binary redistribution.
23  * 3. Neither the names of the above-listed copyright holders nor the names
24  *    of any contributors may be used to endorse or promote products derived
25  *    from this software without specific prior written permission.
26  *
27  * Alternatively, this software may be distributed under the terms of the
28  * GNU General Public License ("GPL") version 2 as published by the Free
29  * Software Foundation.
30  *
31  * NO WARRANTY
32  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
33  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
34  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
35  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
36  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
37  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
38  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
39  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
40  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
41  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
42  * POSSIBILITY OF SUCH DAMAGES.
43  */
44
45
46 #include <acpi/acpi.h>
47 #include <acpi/acparser.h>
48 #include <acpi/amlcode.h>
49 #include <acpi/acdispat.h>
50 #include <acpi/acinterp.h>
51 #include <acpi/acnamesp.h>
52 #include <acpi/acdebug.h>
53 #include <acpi/acdisasm.h>
54
55
56 #define _COMPONENT          ACPI_DISPATCHER
57          ACPI_MODULE_NAME    ("dswexec")
58
59 /*
60  * Dispatch table for opcode classes
61  */
62 static ACPI_EXECUTE_OP      acpi_gbl_op_type_dispatch [] = {
63                          acpi_ex_opcode_1A_0T_0R,
64                          acpi_ex_opcode_1A_0T_1R,
65                          acpi_ex_opcode_1A_1T_0R,
66                          acpi_ex_opcode_1A_1T_1R,
67                          acpi_ex_opcode_2A_0T_0R,
68                          acpi_ex_opcode_2A_0T_1R,
69                          acpi_ex_opcode_2A_1T_1R,
70                          acpi_ex_opcode_2A_2T_1R,
71                          acpi_ex_opcode_3A_0T_0R,
72                          acpi_ex_opcode_3A_1T_1R,
73                          acpi_ex_opcode_6A_0T_1R};
74
75 /*****************************************************************************
76  *
77  * FUNCTION:    acpi_ds_get_predicate_value
78  *
79  * PARAMETERS:  walk_state      - Current state of the parse tree walk
80  *
81  * RETURN:      Status
82  *
83  * DESCRIPTION: Get the result of a predicate evaluation
84  *
85  ****************************************************************************/
86
87 acpi_status
88 acpi_ds_get_predicate_value (
89         struct acpi_walk_state          *walk_state,
90         union acpi_operand_object       *result_obj) {
91         acpi_status                     status = AE_OK;
92         union acpi_operand_object       *obj_desc;
93
94
95         ACPI_FUNCTION_TRACE_PTR ("ds_get_predicate_value", walk_state);
96
97
98         walk_state->control_state->common.state = 0;
99
100         if (result_obj) {
101                 status = acpi_ds_result_pop (&obj_desc, walk_state);
102                 if (ACPI_FAILURE (status)) {
103                         ACPI_DEBUG_PRINT ((ACPI_DB_ERROR,
104                                 "Could not get result from predicate evaluation, %s\n",
105                                 acpi_format_exception (status)));
106
107                         return_ACPI_STATUS (status);
108                 }
109         }
110         else {
111                 status = acpi_ds_create_operand (walk_state, walk_state->op, 0);
112                 if (ACPI_FAILURE (status)) {
113                         return_ACPI_STATUS (status);
114                 }
115
116                 status = acpi_ex_resolve_to_value (&walk_state->operands [0], walk_state);
117                 if (ACPI_FAILURE (status)) {
118                         return_ACPI_STATUS (status);
119                 }
120
121                 obj_desc = walk_state->operands [0];
122         }
123
124         if (!obj_desc) {
125                 ACPI_DEBUG_PRINT ((ACPI_DB_ERROR, "No predicate obj_desc=%p State=%p\n",
126                         obj_desc, walk_state));
127
128                 return_ACPI_STATUS (AE_AML_NO_OPERAND);
129         }
130
131         /*
132          * Result of predicate evaluation currently must
133          * be a number
134          */
135         if (ACPI_GET_OBJECT_TYPE (obj_desc) != ACPI_TYPE_INTEGER) {
136                 ACPI_DEBUG_PRINT ((ACPI_DB_ERROR,
137                         "Bad predicate (not a number) obj_desc=%p State=%p Type=%X\n",
138                         obj_desc, walk_state, ACPI_GET_OBJECT_TYPE (obj_desc)));
139
140                 status = AE_AML_OPERAND_TYPE;
141                 goto cleanup;
142         }
143
144         /* Truncate the predicate to 32-bits if necessary */
145
146         acpi_ex_truncate_for32bit_table (obj_desc);
147
148         /*
149          * Save the result of the predicate evaluation on
150          * the control stack
151          */
152         if (obj_desc->integer.value) {
153                 walk_state->control_state->common.value = TRUE;
154         }
155         else {
156                 /*
157                  * Predicate is FALSE, we will just toss the
158                  * rest of the package
159                  */
160                 walk_state->control_state->common.value = FALSE;
161                 status = AE_CTRL_FALSE;
162         }
163
164
165 cleanup:
166
167         ACPI_DEBUG_PRINT ((ACPI_DB_EXEC, "Completed a predicate eval=%X Op=%p\n",
168                 walk_state->control_state->common.value, walk_state->op));
169
170          /* Break to debugger to display result */
171
172         ACPI_DEBUGGER_EXEC (acpi_db_display_result_object (obj_desc, walk_state));
173
174         /*
175          * Delete the predicate result object (we know that
176          * we don't need it anymore)
177          */
178         acpi_ut_remove_reference (obj_desc);
179
180         walk_state->control_state->common.state = ACPI_CONTROL_NORMAL;
181         return_ACPI_STATUS (status);
182 }
183
184
185 /*****************************************************************************
186  *
187  * FUNCTION:    acpi_ds_exec_begin_op
188  *
189  * PARAMETERS:  walk_state      - Current state of the parse tree walk
190  *              out_op          - Return op if a new one is created
191  *
192  * RETURN:      Status
193  *
194  * DESCRIPTION: Descending callback used during the execution of control
195  *              methods.  This is where most operators and operands are
196  *              dispatched to the interpreter.
197  *
198  ****************************************************************************/
199
200 acpi_status
201 acpi_ds_exec_begin_op (
202         struct acpi_walk_state          *walk_state,
203         union acpi_parse_object         **out_op)
204 {
205         union acpi_parse_object         *op;
206         acpi_status                     status = AE_OK;
207         u32                             opcode_class;
208
209
210         ACPI_FUNCTION_TRACE_PTR ("ds_exec_begin_op", walk_state);
211
212
213         op = walk_state->op;
214         if (!op) {
215                 status = acpi_ds_load2_begin_op (walk_state, out_op);
216                 if (ACPI_FAILURE (status)) {
217                         return_ACPI_STATUS (status);
218                 }
219
220                 op = *out_op;
221                 walk_state->op = op;
222                 walk_state->opcode = op->common.aml_opcode;
223                 walk_state->op_info = acpi_ps_get_opcode_info (op->common.aml_opcode);
224
225                 if (acpi_ns_opens_scope (walk_state->op_info->object_type)) {
226                         ACPI_DEBUG_PRINT ((ACPI_DB_DISPATCH, "(%s) Popping scope for Op %p\n",
227                                 acpi_ut_get_type_name (walk_state->op_info->object_type), op));
228
229                         status = acpi_ds_scope_stack_pop (walk_state);
230                         if (ACPI_FAILURE (status)) {
231                                 return_ACPI_STATUS (status);
232                         }
233                 }
234         }
235
236         if (op == walk_state->origin) {
237                 if (out_op) {
238                         *out_op = op;
239                 }
240
241                 return_ACPI_STATUS (AE_OK);
242         }
243
244         /*
245          * If the previous opcode was a conditional, this opcode
246          * must be the beginning of the associated predicate.
247          * Save this knowledge in the current scope descriptor
248          */
249         if ((walk_state->control_state) &&
250                 (walk_state->control_state->common.state ==
251                         ACPI_CONTROL_CONDITIONAL_EXECUTING)) {
252                 ACPI_DEBUG_PRINT ((ACPI_DB_EXEC, "Exec predicate Op=%p State=%p\n",
253                                   op, walk_state));
254
255                 walk_state->control_state->common.state = ACPI_CONTROL_PREDICATE_EXECUTING;
256
257                 /* Save start of predicate */
258
259                 walk_state->control_state->control.predicate_op = op;
260         }
261
262
263         opcode_class = walk_state->op_info->class;
264
265         /* We want to send namepaths to the load code */
266
267         if (op->common.aml_opcode == AML_INT_NAMEPATH_OP) {
268                 opcode_class = AML_CLASS_NAMED_OBJECT;
269         }
270
271         /*
272          * Handle the opcode based upon the opcode type
273          */
274         switch (opcode_class) {
275         case AML_CLASS_CONTROL:
276
277                 status = acpi_ds_result_stack_push (walk_state);
278                 if (ACPI_FAILURE (status)) {
279                         return_ACPI_STATUS (status);
280                 }
281
282                 status = acpi_ds_exec_begin_control_op (walk_state, op);
283                 break;
284
285
286         case AML_CLASS_NAMED_OBJECT:
287
288                 if (walk_state->walk_type == ACPI_WALK_METHOD) {
289                         /*
290                          * Found a named object declaration during method
291                          * execution;  we must enter this object into the
292                          * namespace.  The created object is temporary and
293                          * will be deleted upon completion of the execution
294                          * of this method.
295                          */
296                         status = acpi_ds_load2_begin_op (walk_state, NULL);
297                 }
298
299                 if (op->common.aml_opcode == AML_REGION_OP) {
300                         status = acpi_ds_result_stack_push (walk_state);
301                 }
302                 break;
303
304
305         case AML_CLASS_EXECUTE:
306         case AML_CLASS_CREATE:
307
308                 /* most operators with arguments */
309                 /* Start a new result/operand state */
310
311                 status = acpi_ds_result_stack_push (walk_state);
312                 break;
313
314
315         default:
316                 break;
317         }
318
319         /* Nothing to do here during method execution */
320
321         return_ACPI_STATUS (status);
322 }
323
324
325 /*****************************************************************************
326  *
327  * FUNCTION:    acpi_ds_exec_end_op
328  *
329  * PARAMETERS:  walk_state      - Current state of the parse tree walk
330  *              Op              - Op that has been just been completed in the
331  *                                walk;  Arguments have now been evaluated.
332  *
333  * RETURN:      Status
334  *
335  * DESCRIPTION: Ascending callback used during the execution of control
336  *              methods.  The only thing we really need to do here is to
337  *              notice the beginning of IF, ELSE, and WHILE blocks.
338  *
339  ****************************************************************************/
340
341 acpi_status
342 acpi_ds_exec_end_op (
343         struct acpi_walk_state          *walk_state)
344 {
345         union acpi_parse_object         *op;
346         acpi_status                     status = AE_OK;
347         u32                             op_type;
348         u32                             op_class;
349         union acpi_parse_object         *next_op;
350         union acpi_parse_object         *first_arg;
351
352
353         ACPI_FUNCTION_TRACE_PTR ("ds_exec_end_op", walk_state);
354
355
356         op      = walk_state->op;
357         op_type = walk_state->op_info->type;
358         op_class = walk_state->op_info->class;
359
360         if (op_class == AML_CLASS_UNKNOWN) {
361                 ACPI_DEBUG_PRINT ((ACPI_DB_ERROR, "Unknown opcode %X\n", op->common.aml_opcode));
362                 return_ACPI_STATUS (AE_NOT_IMPLEMENTED);
363         }
364
365         first_arg = op->common.value.arg;
366
367         /* Init the walk state */
368
369         walk_state->num_operands = 0;
370         walk_state->return_desc = NULL;
371         walk_state->result_obj = NULL;
372
373         /* Call debugger for single step support (DEBUG build only) */
374
375         ACPI_DEBUGGER_EXEC (status = acpi_db_single_step (walk_state, op, op_class));
376         ACPI_DEBUGGER_EXEC (if (ACPI_FAILURE (status)) {return_ACPI_STATUS (status);});
377
378         /* Decode the Opcode Class */
379
380         switch (op_class) {
381         case AML_CLASS_ARGUMENT:    /* constants, literals, etc. -- do nothing */
382                 break;
383
384
385         case AML_CLASS_EXECUTE:     /* most operators with arguments */
386
387                 /* Build resolved operand stack */
388
389                 status = acpi_ds_create_operands (walk_state, first_arg);
390                 if (ACPI_FAILURE (status)) {
391                         goto cleanup;
392                 }
393
394                 /* Done with this result state (Now that operand stack is built) */
395
396                 status = acpi_ds_result_stack_pop (walk_state);
397                 if (ACPI_FAILURE (status)) {
398                         goto cleanup;
399                 }
400
401                 /* Resolve all operands */
402
403                 status = acpi_ex_resolve_operands (walk_state->opcode,
404                                   &(walk_state->operands [walk_state->num_operands -1]),
405                                   walk_state);
406                 if (ACPI_SUCCESS (status)) {
407                         ACPI_DUMP_OPERANDS (ACPI_WALK_OPERANDS, ACPI_IMODE_EXECUTE,
408                                           acpi_ps_get_opcode_name (walk_state->opcode),
409                                           walk_state->num_operands, "after ex_resolve_operands");
410
411                         /*
412                          * Dispatch the request to the appropriate interpreter handler
413                          * routine.  There is one routine per opcode "type" based upon the
414                          * number of opcode arguments and return type.
415                          */
416                         status = acpi_gbl_op_type_dispatch [op_type] (walk_state);
417                 }
418                 else {
419                         /*
420                          * Treat constructs of the form "Store(local_x,local_x)" as noops when the
421                          * Local is uninitialized.
422                          */
423                         if  ((status == AE_AML_UNINITIALIZED_LOCAL) &&
424                                 (walk_state->opcode == AML_STORE_OP) &&
425                                 (walk_state->operands[0]->common.type == ACPI_TYPE_LOCAL_REFERENCE) &&
426                                 (walk_state->operands[1]->common.type == ACPI_TYPE_LOCAL_REFERENCE) &&
427                                 (walk_state->operands[0]->reference.opcode ==
428                                  walk_state->operands[1]->reference.opcode)) {
429                                 status = AE_OK;
430                         }
431                         else {
432                                 ACPI_DEBUG_PRINT ((ACPI_DB_ERROR,
433                                         "[%s]: Could not resolve operands, %s\n",
434                                         acpi_ps_get_opcode_name (walk_state->opcode),
435                                         acpi_format_exception (status)));
436                         }
437                 }
438
439                 /* Always delete the argument objects and clear the operand stack */
440
441                 acpi_ds_clear_operands (walk_state);
442
443                 /*
444                  * If a result object was returned from above, push it on the
445                  * current result stack
446                  */
447                 if (ACPI_SUCCESS (status) &&
448                         walk_state->result_obj) {
449                         status = acpi_ds_result_push (walk_state->result_obj, walk_state);
450                 }
451
452                 break;
453
454
455         default:
456
457                 switch (op_type) {
458                 case AML_TYPE_CONTROL:    /* Type 1 opcode, IF/ELSE/WHILE/NOOP */
459
460                         /* 1 Operand, 0 external_result, 0 internal_result */
461
462                         status = acpi_ds_exec_end_control_op (walk_state, op);
463                         if (ACPI_FAILURE (status)) {
464                                 break;
465                         }
466
467                         status = acpi_ds_result_stack_pop (walk_state);
468                         break;
469
470
471                 case AML_TYPE_METHOD_CALL:
472
473                         ACPI_DEBUG_PRINT ((ACPI_DB_DISPATCH, "Method invocation, Op=%p\n", op));
474
475                         /*
476                          * (AML_METHODCALL) Op->Value->Arg->Node contains
477                          * the method Node pointer
478                          */
479                         /* next_op points to the op that holds the method name */
480
481                         next_op = first_arg;
482
483                         /* next_op points to first argument op */
484
485                         next_op = next_op->common.next;
486
487                         /*
488                          * Get the method's arguments and put them on the operand stack
489                          */
490                         status = acpi_ds_create_operands (walk_state, next_op);
491                         if (ACPI_FAILURE (status)) {
492                                 break;
493                         }
494
495                         /*
496                          * Since the operands will be passed to another control method,
497                          * we must resolve all local references here (Local variables,
498                          * arguments to *this* method, etc.)
499                          */
500                         status = acpi_ds_resolve_operands (walk_state);
501                         if (ACPI_FAILURE (status)) {
502                                 /* On error, clear all resolved operands */
503
504                                 acpi_ds_clear_operands (walk_state);
505                                 break;
506                         }
507
508                         /*
509                          * Tell the walk loop to preempt this running method and
510                          * execute the new method
511                          */
512                         status = AE_CTRL_TRANSFER;
513
514                         /*
515                          * Return now; we don't want to disturb anything,
516                          * especially the operand count!
517                          */
518                         return_ACPI_STATUS (status);
519
520
521                 case AML_TYPE_CREATE_FIELD:
522
523                         ACPI_DEBUG_PRINT ((ACPI_DB_EXEC,
524                                 "Executing create_field Buffer/Index Op=%p\n", op));
525
526                         status = acpi_ds_load2_end_op (walk_state);
527                         if (ACPI_FAILURE (status)) {
528                                 break;
529                         }
530
531                         status = acpi_ds_eval_buffer_field_operands (walk_state, op);
532                         break;
533
534
535                 case AML_TYPE_CREATE_OBJECT:
536
537                         ACPI_DEBUG_PRINT ((ACPI_DB_EXEC,
538                                 "Executing create_object (Buffer/Package) Op=%p\n", op));
539
540                         switch (op->common.parent->common.aml_opcode) {
541                         case AML_NAME_OP:
542
543                                 /*
544                                  * Put the Node on the object stack (Contains the ACPI Name of
545                                  * this object)
546                                  */
547                                 walk_state->operands[0] = (void *) op->common.parent->common.node;
548                                 walk_state->num_operands = 1;
549
550                                 status = acpi_ds_create_node (walk_state, op->common.parent->common.node, op->common.parent);
551                                 if (ACPI_FAILURE (status)) {
552                                         break;
553                                 }
554
555                                 /* Fall through */
556                                 /*lint -fallthrough */
557
558                         case AML_INT_EVAL_SUBTREE_OP:
559
560                                 status = acpi_ds_eval_data_object_operands (walk_state, op,
561                                                   acpi_ns_get_attached_object (op->common.parent->common.node));
562                                 break;
563
564                         default:
565
566                                 status = acpi_ds_eval_data_object_operands (walk_state, op, NULL);
567                                 break;
568                         }
569
570                         /*
571                          * If a result object was returned from above, push it on the
572                          * current result stack
573                          */
574                         if (ACPI_SUCCESS (status) &&
575                                 walk_state->result_obj) {
576                                 status = acpi_ds_result_push (walk_state->result_obj, walk_state);
577                         }
578                         break;
579
580
581                 case AML_TYPE_NAMED_FIELD:
582                 case AML_TYPE_NAMED_COMPLEX:
583                 case AML_TYPE_NAMED_SIMPLE:
584                 case AML_TYPE_NAMED_NO_OBJ:
585
586                         status = acpi_ds_load2_end_op (walk_state);
587                         if (ACPI_FAILURE (status)) {
588                                 break;
589                         }
590
591                         if (op->common.aml_opcode == AML_REGION_OP) {
592                                 ACPI_DEBUG_PRINT ((ACPI_DB_EXEC,
593                                         "Executing op_region Address/Length Op=%p\n", op));
594
595                                 status = acpi_ds_eval_region_operands (walk_state, op);
596                                 if (ACPI_FAILURE (status)) {
597                                         break;
598                                 }
599
600                                 status = acpi_ds_result_stack_pop (walk_state);
601                         }
602
603                         break;
604
605
606                 case AML_TYPE_UNDEFINED:
607
608                         ACPI_DEBUG_PRINT ((ACPI_DB_ERROR, "Undefined opcode type Op=%p\n", op));
609                         return_ACPI_STATUS (AE_NOT_IMPLEMENTED);
610
611
612                 case AML_TYPE_BOGUS:
613
614                         ACPI_DEBUG_PRINT ((ACPI_DB_DISPATCH,
615                                 "Internal opcode=%X type Op=%p\n",
616                                 walk_state->opcode, op));
617                         break;
618
619
620                 default:
621
622                         ACPI_DEBUG_PRINT ((ACPI_DB_ERROR,
623                                 "Unimplemented opcode, class=%X type=%X Opcode=%X Op=%p\n",
624                                 op_class, op_type, op->common.aml_opcode, op));
625
626                         status = AE_NOT_IMPLEMENTED;
627                         break;
628                 }
629         }
630
631         /*
632          * ACPI 2.0 support for 64-bit integers: Truncate numeric
633          * result value if we are executing from a 32-bit ACPI table
634          */
635         acpi_ex_truncate_for32bit_table (walk_state->result_obj);
636
637         /*
638          * Check if we just completed the evaluation of a
639          * conditional predicate
640          */
641
642         if ((walk_state->control_state) &&
643                 (walk_state->control_state->common.state ==
644                         ACPI_CONTROL_PREDICATE_EXECUTING) &&
645                 (walk_state->control_state->control.predicate_op == op)) {
646                 status = acpi_ds_get_predicate_value (walk_state, walk_state->result_obj);
647                 walk_state->result_obj = NULL;
648         }
649
650
651 cleanup:
652         if (walk_state->result_obj) {
653                 /* Break to debugger to display result */
654
655                 ACPI_DEBUGGER_EXEC (acpi_db_display_result_object (walk_state->result_obj, walk_state));
656
657                 /*
658                  * Delete the result op if and only if:
659                  * Parent will not use the result -- such as any
660                  * non-nested type2 op in a method (parent will be method)
661                  */
662                 acpi_ds_delete_result_if_not_used (op, walk_state->result_obj, walk_state);
663         }
664
665 #ifdef _UNDER_DEVELOPMENT
666
667         if (walk_state->parser_state.aml == walk_state->parser_state.aml_end) {
668                 acpi_db_method_end (walk_state);
669         }
670 #endif
671
672         /* Always clear the object stack */
673
674         walk_state->num_operands = 0;
675
676 #ifdef ACPI_DISASSEMBLER
677
678         /* On error, display method locals/args */
679
680         if (ACPI_FAILURE (status)) {
681                 acpi_dm_dump_method_info (status, walk_state, op);
682         }
683 #endif
684
685         return_ACPI_STATUS (status);
686 }
687
688