1 /******************************************************************************
 
   3  * Module Name: dswstate - Dispatcher parse tree walk management routines
 
   5  *****************************************************************************/
 
   8  * Copyright (C) 2000 - 2008, Intel Corp.
 
  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.
 
  44 #include <acpi/acpi.h>
 
  50 #define _COMPONENT          ACPI_DISPATCHER
 
  51 ACPI_MODULE_NAME("dswstate")
 
  53   /* Local prototypes */
 
  54 static acpi_status acpi_ds_result_stack_push(struct acpi_walk_state *ws);
 
  55 static acpi_status acpi_ds_result_stack_pop(struct acpi_walk_state *ws);
 
  57 /*******************************************************************************
 
  59  * FUNCTION:    acpi_ds_result_pop
 
  61  * PARAMETERS:  Object              - Where to return the popped object
 
  62  *              walk_state          - Current Walk state
 
  66  * DESCRIPTION: Pop an object off the top of this walk's result stack
 
  68  ******************************************************************************/
 
  71 acpi_ds_result_pop(union acpi_operand_object **object,
 
  72                    struct acpi_walk_state *walk_state)
 
  75         union acpi_generic_state *state;
 
  78         ACPI_FUNCTION_NAME(ds_result_pop);
 
  80         state = walk_state->results;
 
  82         /* Incorrect state of result stack */
 
  84         if (state && !walk_state->result_count) {
 
  85                 ACPI_ERROR((AE_INFO, "No results on result stack"));
 
  86                 return (AE_AML_INTERNAL);
 
  89         if (!state && walk_state->result_count) {
 
  90                 ACPI_ERROR((AE_INFO, "No result state for result stack"));
 
  91                 return (AE_AML_INTERNAL);
 
  94         /* Empty result stack */
 
  97                 ACPI_ERROR((AE_INFO, "Result stack is empty! State=%p",
 
  99                 return (AE_AML_NO_RETURN_VALUE);
 
 102         /* Return object of the top element and clean that top element result stack */
 
 104         walk_state->result_count--;
 
 105         index = walk_state->result_count % ACPI_RESULTS_FRAME_OBJ_NUM;
 
 107         *object = state->results.obj_desc[index];
 
 110                             "No result objects on result stack, State=%p",
 
 112                 return (AE_AML_NO_RETURN_VALUE);
 
 115         state->results.obj_desc[index] = NULL;
 
 117                 status = acpi_ds_result_stack_pop(walk_state);
 
 118                 if (ACPI_FAILURE(status)) {
 
 123         ACPI_DEBUG_PRINT((ACPI_DB_EXEC,
 
 124                           "Obj=%p [%s] Index=%X State=%p Num=%X\n", *object,
 
 125                           acpi_ut_get_object_type_name(*object),
 
 126                           index, walk_state, walk_state->result_count));
 
 131 /*******************************************************************************
 
 133  * FUNCTION:    acpi_ds_result_push
 
 135  * PARAMETERS:  Object              - Where to return the popped object
 
 136  *              walk_state          - Current Walk state
 
 140  * DESCRIPTION: Push an object onto the current result stack
 
 142  ******************************************************************************/
 
 145 acpi_ds_result_push(union acpi_operand_object * object,
 
 146                     struct acpi_walk_state * walk_state)
 
 148         union acpi_generic_state *state;
 
 152         ACPI_FUNCTION_NAME(ds_result_push);
 
 154         if (walk_state->result_count > walk_state->result_size) {
 
 155                 ACPI_ERROR((AE_INFO, "Result stack is full"));
 
 156                 return (AE_AML_INTERNAL);
 
 157         } else if (walk_state->result_count == walk_state->result_size) {
 
 159                 /* Extend the result stack */
 
 161                 status = acpi_ds_result_stack_push(walk_state);
 
 162                 if (ACPI_FAILURE(status)) {
 
 164                                     "Failed to extend the result stack"));
 
 169         if (!(walk_state->result_count < walk_state->result_size)) {
 
 170                 ACPI_ERROR((AE_INFO, "No free elements in result stack"));
 
 171                 return (AE_AML_INTERNAL);
 
 174         state = walk_state->results;
 
 176                 ACPI_ERROR((AE_INFO, "No result stack frame during push"));
 
 177                 return (AE_AML_INTERNAL);
 
 182                             "Null Object! Obj=%p State=%p Num=%X",
 
 183                             object, walk_state, walk_state->result_count));
 
 184                 return (AE_BAD_PARAMETER);
 
 187         /* Assign the address of object to the top free element of result stack */
 
 189         index = walk_state->result_count % ACPI_RESULTS_FRAME_OBJ_NUM;
 
 190         state->results.obj_desc[index] = object;
 
 191         walk_state->result_count++;
 
 193         ACPI_DEBUG_PRINT((ACPI_DB_EXEC, "Obj=%p [%s] State=%p Num=%X Cur=%X\n",
 
 195                           acpi_ut_get_object_type_name((union
 
 196                                                         acpi_operand_object *)
 
 198                           walk_state->result_count,
 
 199                           walk_state->current_result));
 
 204 /*******************************************************************************
 
 206  * FUNCTION:    acpi_ds_result_stack_push
 
 208  * PARAMETERS:  walk_state          - Current Walk state
 
 212  * DESCRIPTION: Push an object onto the walk_state result stack
 
 214  ******************************************************************************/
 
 216 static acpi_status acpi_ds_result_stack_push(struct acpi_walk_state *walk_state)
 
 218         union acpi_generic_state *state;
 
 220         ACPI_FUNCTION_NAME(ds_result_stack_push);
 
 222         /* Check for stack overflow */
 
 224         if (((u32) walk_state->result_size + ACPI_RESULTS_FRAME_OBJ_NUM) >
 
 225             ACPI_RESULTS_OBJ_NUM_MAX) {
 
 226                 ACPI_ERROR((AE_INFO, "Result stack overflow: State=%p Num=%X",
 
 227                             walk_state, walk_state->result_size));
 
 228                 return (AE_STACK_OVERFLOW);
 
 231         state = acpi_ut_create_generic_state();
 
 233                 return (AE_NO_MEMORY);
 
 236         state->common.descriptor_type = ACPI_DESC_TYPE_STATE_RESULT;
 
 237         acpi_ut_push_generic_state(&walk_state->results, state);
 
 239         /* Increase the length of the result stack by the length of frame */
 
 241         walk_state->result_size += ACPI_RESULTS_FRAME_OBJ_NUM;
 
 243         ACPI_DEBUG_PRINT((ACPI_DB_EXEC, "Results=%p State=%p\n",
 
 249 /*******************************************************************************
 
 251  * FUNCTION:    acpi_ds_result_stack_pop
 
 253  * PARAMETERS:  walk_state          - Current Walk state
 
 257  * DESCRIPTION: Pop an object off of the walk_state result stack
 
 259  ******************************************************************************/
 
 261 static acpi_status acpi_ds_result_stack_pop(struct acpi_walk_state *walk_state)
 
 263         union acpi_generic_state *state;
 
 265         ACPI_FUNCTION_NAME(ds_result_stack_pop);
 
 267         /* Check for stack underflow */
 
 269         if (walk_state->results == NULL) {
 
 270                 ACPI_DEBUG_PRINT((ACPI_DB_EXEC,
 
 271                                   "Result stack underflow - State=%p\n",
 
 273                 return (AE_AML_NO_OPERAND);
 
 276         if (walk_state->result_size < ACPI_RESULTS_FRAME_OBJ_NUM) {
 
 277                 ACPI_ERROR((AE_INFO, "Insufficient result stack size"));
 
 278                 return (AE_AML_INTERNAL);
 
 281         state = acpi_ut_pop_generic_state(&walk_state->results);
 
 282         acpi_ut_delete_generic_state(state);
 
 284         /* Decrease the length of result stack by the length of frame */
 
 286         walk_state->result_size -= ACPI_RESULTS_FRAME_OBJ_NUM;
 
 288         ACPI_DEBUG_PRINT((ACPI_DB_EXEC,
 
 289                           "Result=%p RemainingResults=%X State=%p\n",
 
 290                           state, walk_state->result_count, walk_state));
 
 295 /*******************************************************************************
 
 297  * FUNCTION:    acpi_ds_obj_stack_push
 
 299  * PARAMETERS:  Object              - Object to push
 
 300  *              walk_state          - Current Walk state
 
 304  * DESCRIPTION: Push an object onto this walk's object/operand stack
 
 306  ******************************************************************************/
 
 309 acpi_ds_obj_stack_push(void *object, struct acpi_walk_state * walk_state)
 
 311         ACPI_FUNCTION_NAME(ds_obj_stack_push);
 
 313         /* Check for stack overflow */
 
 315         if (walk_state->num_operands >= ACPI_OBJ_NUM_OPERANDS) {
 
 317                             "Object stack overflow! Obj=%p State=%p #Ops=%X",
 
 318                             object, walk_state, walk_state->num_operands));
 
 319                 return (AE_STACK_OVERFLOW);
 
 322         /* Put the object onto the stack */
 
 324         walk_state->operands[walk_state->operand_index] = object;
 
 325         walk_state->num_operands++;
 
 327         /* For the usual order of filling the operand stack */
 
 329         walk_state->operand_index++;
 
 331         ACPI_DEBUG_PRINT((ACPI_DB_EXEC, "Obj=%p [%s] State=%p #Ops=%X\n",
 
 333                           acpi_ut_get_object_type_name((union
 
 334                                                         acpi_operand_object *)
 
 336                           walk_state->num_operands));
 
 341 /*******************************************************************************
 
 343  * FUNCTION:    acpi_ds_obj_stack_pop
 
 345  * PARAMETERS:  pop_count           - Number of objects/entries to pop
 
 346  *              walk_state          - Current Walk state
 
 350  * DESCRIPTION: Pop this walk's object stack.  Objects on the stack are NOT
 
 351  *              deleted by this routine.
 
 353  ******************************************************************************/
 
 356 acpi_ds_obj_stack_pop(u32 pop_count, struct acpi_walk_state * walk_state)
 
 360         ACPI_FUNCTION_NAME(ds_obj_stack_pop);
 
 362         for (i = 0; i < pop_count; i++) {
 
 364                 /* Check for stack underflow */
 
 366                 if (walk_state->num_operands == 0) {
 
 368                                     "Object stack underflow! Count=%X State=%p #Ops=%X",
 
 369                                     pop_count, walk_state,
 
 370                                     walk_state->num_operands));
 
 371                         return (AE_STACK_UNDERFLOW);
 
 374                 /* Just set the stack entry to null */
 
 376                 walk_state->num_operands--;
 
 377                 walk_state->operands[walk_state->num_operands] = NULL;
 
 380         ACPI_DEBUG_PRINT((ACPI_DB_EXEC, "Count=%X State=%p #Ops=%X\n",
 
 381                           pop_count, walk_state, walk_state->num_operands));
 
 386 /*******************************************************************************
 
 388  * FUNCTION:    acpi_ds_obj_stack_pop_and_delete
 
 390  * PARAMETERS:  pop_count           - Number of objects/entries to pop
 
 391  *              walk_state          - Current Walk state
 
 395  * DESCRIPTION: Pop this walk's object stack and delete each object that is
 
 398  ******************************************************************************/
 
 401 acpi_ds_obj_stack_pop_and_delete(u32 pop_count,
 
 402                                  struct acpi_walk_state *walk_state)
 
 405         union acpi_operand_object *obj_desc;
 
 407         ACPI_FUNCTION_NAME(ds_obj_stack_pop_and_delete);
 
 409         if (pop_count == 0) {
 
 413         for (i = (s32) pop_count - 1; i >= 0; i--) {
 
 414                 if (walk_state->num_operands == 0) {
 
 418                 /* Pop the stack and delete an object if present in this stack entry */
 
 420                 walk_state->num_operands--;
 
 421                 obj_desc = walk_state->operands[i];
 
 423                         acpi_ut_remove_reference(walk_state->operands[i]);
 
 424                         walk_state->operands[i] = NULL;
 
 428         ACPI_DEBUG_PRINT((ACPI_DB_EXEC, "Count=%X State=%p #Ops=%X\n",
 
 429                           pop_count, walk_state, walk_state->num_operands));
 
 432 /*******************************************************************************
 
 434  * FUNCTION:    acpi_ds_get_current_walk_state
 
 436  * PARAMETERS:  Thread          - Get current active state for this Thread
 
 438  * RETURN:      Pointer to the current walk state
 
 440  * DESCRIPTION: Get the walk state that is at the head of the list (the "current"
 
 443  ******************************************************************************/
 
 445 struct acpi_walk_state *acpi_ds_get_current_walk_state(struct acpi_thread_state
 
 448         ACPI_FUNCTION_NAME(ds_get_current_walk_state);
 
 454         ACPI_DEBUG_PRINT((ACPI_DB_PARSE, "Current WalkState %p\n",
 
 455                           thread->walk_state_list));
 
 457         return (thread->walk_state_list);
 
 460 /*******************************************************************************
 
 462  * FUNCTION:    acpi_ds_push_walk_state
 
 464  * PARAMETERS:  walk_state      - State to push
 
 465  *              Thread          - Thread state object
 
 469  * DESCRIPTION: Place the Thread state at the head of the state list
 
 471  ******************************************************************************/
 
 474 acpi_ds_push_walk_state(struct acpi_walk_state *walk_state,
 
 475                         struct acpi_thread_state *thread)
 
 477         ACPI_FUNCTION_TRACE(ds_push_walk_state);
 
 479         walk_state->next = thread->walk_state_list;
 
 480         thread->walk_state_list = walk_state;
 
 485 /*******************************************************************************
 
 487  * FUNCTION:    acpi_ds_pop_walk_state
 
 489  * PARAMETERS:  Thread      - Current thread state
 
 491  * RETURN:      A walk_state object popped from the thread's stack
 
 493  * DESCRIPTION: Remove and return the walkstate object that is at the head of
 
 494  *              the walk stack for the given walk list.  NULL indicates that
 
 497  ******************************************************************************/
 
 499 struct acpi_walk_state *acpi_ds_pop_walk_state(struct acpi_thread_state *thread)
 
 501         struct acpi_walk_state *walk_state;
 
 503         ACPI_FUNCTION_TRACE(ds_pop_walk_state);
 
 505         walk_state = thread->walk_state_list;
 
 509                 /* Next walk state becomes the current walk state */
 
 511                 thread->walk_state_list = walk_state->next;
 
 514                  * Don't clear the NEXT field, this serves as an indicator
 
 515                  * that there is a parent WALK STATE
 
 516                  * Do Not: walk_state->Next = NULL;
 
 520         return_PTR(walk_state);
 
 523 /*******************************************************************************
 
 525  * FUNCTION:    acpi_ds_create_walk_state
 
 527  * PARAMETERS:  owner_id        - ID for object creation
 
 528  *              Origin          - Starting point for this walk
 
 529  *              method_desc     - Method object
 
 530  *              Thread          - Current thread state
 
 532  * RETURN:      Pointer to the new walk state.
 
 534  * DESCRIPTION: Allocate and initialize a new walk state.  The current walk
 
 535  *              state is set to this new state.
 
 537  ******************************************************************************/
 
 539 struct acpi_walk_state *acpi_ds_create_walk_state(acpi_owner_id owner_id, union acpi_parse_object
 
 540                                                   *origin, union acpi_operand_object
 
 541                                                   *method_desc, struct acpi_thread_state
 
 544         struct acpi_walk_state *walk_state;
 
 546         ACPI_FUNCTION_TRACE(ds_create_walk_state);
 
 548         walk_state = ACPI_ALLOCATE_ZEROED(sizeof(struct acpi_walk_state));
 
 553         walk_state->descriptor_type = ACPI_DESC_TYPE_WALK;
 
 554         walk_state->method_desc = method_desc;
 
 555         walk_state->owner_id = owner_id;
 
 556         walk_state->origin = origin;
 
 557         walk_state->thread = thread;
 
 559         walk_state->parser_state.start_op = origin;
 
 561         /* Init the method args/local */
 
 563 #if (!defined (ACPI_NO_METHOD_EXECUTION) && !defined (ACPI_CONSTANT_EVAL_ONLY))
 
 564         acpi_ds_method_data_init(walk_state);
 
 567         /* Put the new state at the head of the walk list */
 
 570                 acpi_ds_push_walk_state(walk_state, thread);
 
 573         return_PTR(walk_state);
 
 576 /*******************************************************************************
 
 578  * FUNCTION:    acpi_ds_init_aml_walk
 
 580  * PARAMETERS:  walk_state      - New state to be initialized
 
 581  *              Op              - Current parse op
 
 582  *              method_node     - Control method NS node, if any
 
 583  *              aml_start       - Start of AML
 
 584  *              aml_length      - Length of AML
 
 585  *              Info            - Method info block (params, etc.)
 
 586  *              pass_number     - 1, 2, or 3
 
 590  * DESCRIPTION: Initialize a walk state for a pass 1 or 2 parse tree walk
 
 592  ******************************************************************************/
 
 595 acpi_ds_init_aml_walk(struct acpi_walk_state *walk_state,
 
 596                       union acpi_parse_object *op,
 
 597                       struct acpi_namespace_node *method_node,
 
 600                       struct acpi_evaluate_info *info, u8 pass_number)
 
 603         struct acpi_parse_state *parser_state = &walk_state->parser_state;
 
 604         union acpi_parse_object *extra_op;
 
 606         ACPI_FUNCTION_TRACE(ds_init_aml_walk);
 
 608         walk_state->parser_state.aml =
 
 609             walk_state->parser_state.aml_start = aml_start;
 
 610         walk_state->parser_state.aml_end =
 
 611             walk_state->parser_state.pkg_end = aml_start + aml_length;
 
 613         /* The next_op of the next_walk will be the beginning of the method */
 
 615         walk_state->next_op = NULL;
 
 616         walk_state->pass_number = pass_number;
 
 619                 walk_state->params = info->parameters;
 
 620                 walk_state->caller_return_desc = &info->return_object;
 
 623         status = acpi_ps_init_scope(&walk_state->parser_state, op);
 
 624         if (ACPI_FAILURE(status)) {
 
 625                 return_ACPI_STATUS(status);
 
 629                 walk_state->parser_state.start_node = method_node;
 
 630                 walk_state->walk_type = ACPI_WALK_METHOD;
 
 631                 walk_state->method_node = method_node;
 
 632                 walk_state->method_desc =
 
 633                     acpi_ns_get_attached_object(method_node);
 
 635                 /* Push start scope on scope stack and make it current  */
 
 638                     acpi_ds_scope_stack_push(method_node, ACPI_TYPE_METHOD,
 
 640                 if (ACPI_FAILURE(status)) {
 
 641                         return_ACPI_STATUS(status);
 
 644                 /* Init the method arguments */
 
 646                 status = acpi_ds_method_data_init_args(walk_state->params,
 
 647                                                        ACPI_METHOD_NUM_ARGS,
 
 649                 if (ACPI_FAILURE(status)) {
 
 650                         return_ACPI_STATUS(status);
 
 654                  * Setup the current scope.
 
 655                  * Find a Named Op that has a namespace node associated with it.
 
 656                  * search upwards from this Op.  Current scope is the first
 
 657                  * Op with a namespace node.
 
 659                 extra_op = parser_state->start_op;
 
 660                 while (extra_op && !extra_op->common.node) {
 
 661                         extra_op = extra_op->common.parent;
 
 665                         parser_state->start_node = NULL;
 
 667                         parser_state->start_node = extra_op->common.node;
 
 670                 if (parser_state->start_node) {
 
 672                         /* Push start scope on scope stack and make it current  */
 
 675                             acpi_ds_scope_stack_push(parser_state->start_node,
 
 676                                                      parser_state->start_node->
 
 678                         if (ACPI_FAILURE(status)) {
 
 679                                 return_ACPI_STATUS(status);
 
 684         status = acpi_ds_init_callbacks(walk_state, pass_number);
 
 685         return_ACPI_STATUS(status);
 
 688 /*******************************************************************************
 
 690  * FUNCTION:    acpi_ds_delete_walk_state
 
 692  * PARAMETERS:  walk_state      - State to delete
 
 696  * DESCRIPTION: Delete a walk state including all internal data structures
 
 698  ******************************************************************************/
 
 700 void acpi_ds_delete_walk_state(struct acpi_walk_state *walk_state)
 
 702         union acpi_generic_state *state;
 
 704         ACPI_FUNCTION_TRACE_PTR(ds_delete_walk_state, walk_state);
 
 710         if (walk_state->descriptor_type != ACPI_DESC_TYPE_WALK) {
 
 711                 ACPI_ERROR((AE_INFO, "%p is not a valid walk state",
 
 716         /* There should not be any open scopes */
 
 718         if (walk_state->parser_state.scope) {
 
 719                 ACPI_ERROR((AE_INFO, "%p walk still has a scope list",
 
 721                 acpi_ps_cleanup_scope(&walk_state->parser_state);
 
 724         /* Always must free any linked control states */
 
 726         while (walk_state->control_state) {
 
 727                 state = walk_state->control_state;
 
 728                 walk_state->control_state = state->common.next;
 
 730                 acpi_ut_delete_generic_state(state);
 
 733         /* Always must free any linked parse states */
 
 735         while (walk_state->scope_info) {
 
 736                 state = walk_state->scope_info;
 
 737                 walk_state->scope_info = state->common.next;
 
 739                 acpi_ut_delete_generic_state(state);
 
 742         /* Always must free any stacked result states */
 
 744         while (walk_state->results) {
 
 745                 state = walk_state->results;
 
 746                 walk_state->results = state->common.next;
 
 748                 acpi_ut_delete_generic_state(state);
 
 751         ACPI_FREE(walk_state);