/******************************************************************************1*2* Module Name: dswstate - Dispatcher parse tree walk management routines3*4*****************************************************************************/56/*7* Copyright (C) 2000 - 2011, Intel Corp.8* All rights reserved.9*10* Redistribution and use in source and binary forms, with or without11* modification, are permitted provided that the following conditions12* are met:13* 1. Redistributions of source code must retain the above copyright14* notice, this list of conditions, and the following disclaimer,15* without modification.16* 2. Redistributions in binary form must reproduce at minimum a disclaimer17* substantially similar to the "NO WARRANTY" disclaimer below18* ("Disclaimer") and any redistribution must be conditioned upon19* including a substantially similar Disclaimer requirement for further20* binary redistribution.21* 3. Neither the names of the above-listed copyright holders nor the names22* of any contributors may be used to endorse or promote products derived23* from this software without specific prior written permission.24*25* Alternatively, this software may be distributed under the terms of the26* GNU General Public License ("GPL") version 2 as published by the Free27* Software Foundation.28*29* NO WARRANTY30* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS31* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT32* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR33* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT34* HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL35* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS36* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)37* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,38* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING39* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE40* POSSIBILITY OF SUCH DAMAGES.41*/4243#include <acpi/acpi.h>44#include "accommon.h"45#include "acparser.h"46#include "acdispat.h"47#include "acnamesp.h"4849#define _COMPONENT ACPI_DISPATCHER50ACPI_MODULE_NAME("dswstate")5152/* Local prototypes */53static acpi_status acpi_ds_result_stack_push(struct acpi_walk_state *ws);54static acpi_status acpi_ds_result_stack_pop(struct acpi_walk_state *ws);5556/*******************************************************************************57*58* FUNCTION: acpi_ds_result_pop59*60* PARAMETERS: Object - Where to return the popped object61* walk_state - Current Walk state62*63* RETURN: Status64*65* DESCRIPTION: Pop an object off the top of this walk's result stack66*67******************************************************************************/6869acpi_status70acpi_ds_result_pop(union acpi_operand_object **object,71struct acpi_walk_state *walk_state)72{73u32 index;74union acpi_generic_state *state;75acpi_status status;7677ACPI_FUNCTION_NAME(ds_result_pop);7879state = walk_state->results;8081/* Incorrect state of result stack */8283if (state && !walk_state->result_count) {84ACPI_ERROR((AE_INFO, "No results on result stack"));85return (AE_AML_INTERNAL);86}8788if (!state && walk_state->result_count) {89ACPI_ERROR((AE_INFO, "No result state for result stack"));90return (AE_AML_INTERNAL);91}9293/* Empty result stack */9495if (!state) {96ACPI_ERROR((AE_INFO, "Result stack is empty! State=%p",97walk_state));98return (AE_AML_NO_RETURN_VALUE);99}100101/* Return object of the top element and clean that top element result stack */102103walk_state->result_count--;104index = (u32)walk_state->result_count % ACPI_RESULTS_FRAME_OBJ_NUM;105106*object = state->results.obj_desc[index];107if (!*object) {108ACPI_ERROR((AE_INFO,109"No result objects on result stack, State=%p",110walk_state));111return (AE_AML_NO_RETURN_VALUE);112}113114state->results.obj_desc[index] = NULL;115if (index == 0) {116status = acpi_ds_result_stack_pop(walk_state);117if (ACPI_FAILURE(status)) {118return (status);119}120}121122ACPI_DEBUG_PRINT((ACPI_DB_EXEC,123"Obj=%p [%s] Index=%X State=%p Num=%X\n", *object,124acpi_ut_get_object_type_name(*object),125index, walk_state, walk_state->result_count));126127return (AE_OK);128}129130/*******************************************************************************131*132* FUNCTION: acpi_ds_result_push133*134* PARAMETERS: Object - Where to return the popped object135* walk_state - Current Walk state136*137* RETURN: Status138*139* DESCRIPTION: Push an object onto the current result stack140*141******************************************************************************/142143acpi_status144acpi_ds_result_push(union acpi_operand_object * object,145struct acpi_walk_state * walk_state)146{147union acpi_generic_state *state;148acpi_status status;149u32 index;150151ACPI_FUNCTION_NAME(ds_result_push);152153if (walk_state->result_count > walk_state->result_size) {154ACPI_ERROR((AE_INFO, "Result stack is full"));155return (AE_AML_INTERNAL);156} else if (walk_state->result_count == walk_state->result_size) {157158/* Extend the result stack */159160status = acpi_ds_result_stack_push(walk_state);161if (ACPI_FAILURE(status)) {162ACPI_ERROR((AE_INFO,163"Failed to extend the result stack"));164return (status);165}166}167168if (!(walk_state->result_count < walk_state->result_size)) {169ACPI_ERROR((AE_INFO, "No free elements in result stack"));170return (AE_AML_INTERNAL);171}172173state = walk_state->results;174if (!state) {175ACPI_ERROR((AE_INFO, "No result stack frame during push"));176return (AE_AML_INTERNAL);177}178179if (!object) {180ACPI_ERROR((AE_INFO,181"Null Object! Obj=%p State=%p Num=%u",182object, walk_state, walk_state->result_count));183return (AE_BAD_PARAMETER);184}185186/* Assign the address of object to the top free element of result stack */187188index = (u32)walk_state->result_count % ACPI_RESULTS_FRAME_OBJ_NUM;189state->results.obj_desc[index] = object;190walk_state->result_count++;191192ACPI_DEBUG_PRINT((ACPI_DB_EXEC, "Obj=%p [%s] State=%p Num=%X Cur=%X\n",193object,194acpi_ut_get_object_type_name((union195acpi_operand_object *)196object), walk_state,197walk_state->result_count,198walk_state->current_result));199200return (AE_OK);201}202203/*******************************************************************************204*205* FUNCTION: acpi_ds_result_stack_push206*207* PARAMETERS: walk_state - Current Walk state208*209* RETURN: Status210*211* DESCRIPTION: Push an object onto the walk_state result stack212*213******************************************************************************/214215static acpi_status acpi_ds_result_stack_push(struct acpi_walk_state *walk_state)216{217union acpi_generic_state *state;218219ACPI_FUNCTION_NAME(ds_result_stack_push);220221/* Check for stack overflow */222223if (((u32) walk_state->result_size + ACPI_RESULTS_FRAME_OBJ_NUM) >224ACPI_RESULTS_OBJ_NUM_MAX) {225ACPI_ERROR((AE_INFO, "Result stack overflow: State=%p Num=%u",226walk_state, walk_state->result_size));227return (AE_STACK_OVERFLOW);228}229230state = acpi_ut_create_generic_state();231if (!state) {232return (AE_NO_MEMORY);233}234235state->common.descriptor_type = ACPI_DESC_TYPE_STATE_RESULT;236acpi_ut_push_generic_state(&walk_state->results, state);237238/* Increase the length of the result stack by the length of frame */239240walk_state->result_size += ACPI_RESULTS_FRAME_OBJ_NUM;241242ACPI_DEBUG_PRINT((ACPI_DB_EXEC, "Results=%p State=%p\n",243state, walk_state));244245return (AE_OK);246}247248/*******************************************************************************249*250* FUNCTION: acpi_ds_result_stack_pop251*252* PARAMETERS: walk_state - Current Walk state253*254* RETURN: Status255*256* DESCRIPTION: Pop an object off of the walk_state result stack257*258******************************************************************************/259260static acpi_status acpi_ds_result_stack_pop(struct acpi_walk_state *walk_state)261{262union acpi_generic_state *state;263264ACPI_FUNCTION_NAME(ds_result_stack_pop);265266/* Check for stack underflow */267268if (walk_state->results == NULL) {269ACPI_DEBUG_PRINT((ACPI_DB_EXEC,270"Result stack underflow - State=%p\n",271walk_state));272return (AE_AML_NO_OPERAND);273}274275if (walk_state->result_size < ACPI_RESULTS_FRAME_OBJ_NUM) {276ACPI_ERROR((AE_INFO, "Insufficient result stack size"));277return (AE_AML_INTERNAL);278}279280state = acpi_ut_pop_generic_state(&walk_state->results);281acpi_ut_delete_generic_state(state);282283/* Decrease the length of result stack by the length of frame */284285walk_state->result_size -= ACPI_RESULTS_FRAME_OBJ_NUM;286287ACPI_DEBUG_PRINT((ACPI_DB_EXEC,288"Result=%p RemainingResults=%X State=%p\n",289state, walk_state->result_count, walk_state));290291return (AE_OK);292}293294/*******************************************************************************295*296* FUNCTION: acpi_ds_obj_stack_push297*298* PARAMETERS: Object - Object to push299* walk_state - Current Walk state300*301* RETURN: Status302*303* DESCRIPTION: Push an object onto this walk's object/operand stack304*305******************************************************************************/306307acpi_status308acpi_ds_obj_stack_push(void *object, struct acpi_walk_state * walk_state)309{310ACPI_FUNCTION_NAME(ds_obj_stack_push);311312/* Check for stack overflow */313314if (walk_state->num_operands >= ACPI_OBJ_NUM_OPERANDS) {315ACPI_ERROR((AE_INFO,316"Object stack overflow! Obj=%p State=%p #Ops=%u",317object, walk_state, walk_state->num_operands));318return (AE_STACK_OVERFLOW);319}320321/* Put the object onto the stack */322323walk_state->operands[walk_state->operand_index] = object;324walk_state->num_operands++;325326/* For the usual order of filling the operand stack */327328walk_state->operand_index++;329330ACPI_DEBUG_PRINT((ACPI_DB_EXEC, "Obj=%p [%s] State=%p #Ops=%X\n",331object,332acpi_ut_get_object_type_name((union333acpi_operand_object *)334object), walk_state,335walk_state->num_operands));336337return (AE_OK);338}339340/*******************************************************************************341*342* FUNCTION: acpi_ds_obj_stack_pop343*344* PARAMETERS: pop_count - Number of objects/entries to pop345* walk_state - Current Walk state346*347* RETURN: Status348*349* DESCRIPTION: Pop this walk's object stack. Objects on the stack are NOT350* deleted by this routine.351*352******************************************************************************/353354acpi_status355acpi_ds_obj_stack_pop(u32 pop_count, struct acpi_walk_state * walk_state)356{357u32 i;358359ACPI_FUNCTION_NAME(ds_obj_stack_pop);360361for (i = 0; i < pop_count; i++) {362363/* Check for stack underflow */364365if (walk_state->num_operands == 0) {366ACPI_ERROR((AE_INFO,367"Object stack underflow! Count=%X State=%p #Ops=%u",368pop_count, walk_state,369walk_state->num_operands));370return (AE_STACK_UNDERFLOW);371}372373/* Just set the stack entry to null */374375walk_state->num_operands--;376walk_state->operands[walk_state->num_operands] = NULL;377}378379ACPI_DEBUG_PRINT((ACPI_DB_EXEC, "Count=%X State=%p #Ops=%u\n",380pop_count, walk_state, walk_state->num_operands));381382return (AE_OK);383}384385/*******************************************************************************386*387* FUNCTION: acpi_ds_obj_stack_pop_and_delete388*389* PARAMETERS: pop_count - Number of objects/entries to pop390* walk_state - Current Walk state391*392* RETURN: Status393*394* DESCRIPTION: Pop this walk's object stack and delete each object that is395* popped off.396*397******************************************************************************/398399void400acpi_ds_obj_stack_pop_and_delete(u32 pop_count,401struct acpi_walk_state *walk_state)402{403s32 i;404union acpi_operand_object *obj_desc;405406ACPI_FUNCTION_NAME(ds_obj_stack_pop_and_delete);407408if (pop_count == 0) {409return;410}411412for (i = (s32) pop_count - 1; i >= 0; i--) {413if (walk_state->num_operands == 0) {414return;415}416417/* Pop the stack and delete an object if present in this stack entry */418419walk_state->num_operands--;420obj_desc = walk_state->operands[i];421if (obj_desc) {422acpi_ut_remove_reference(walk_state->operands[i]);423walk_state->operands[i] = NULL;424}425}426427ACPI_DEBUG_PRINT((ACPI_DB_EXEC, "Count=%X State=%p #Ops=%X\n",428pop_count, walk_state, walk_state->num_operands));429}430431/*******************************************************************************432*433* FUNCTION: acpi_ds_get_current_walk_state434*435* PARAMETERS: Thread - Get current active state for this Thread436*437* RETURN: Pointer to the current walk state438*439* DESCRIPTION: Get the walk state that is at the head of the list (the "current"440* walk state.)441*442******************************************************************************/443444struct acpi_walk_state *acpi_ds_get_current_walk_state(struct acpi_thread_state445*thread)446{447ACPI_FUNCTION_NAME(ds_get_current_walk_state);448449if (!thread) {450return (NULL);451}452453ACPI_DEBUG_PRINT((ACPI_DB_PARSE, "Current WalkState %p\n",454thread->walk_state_list));455456return (thread->walk_state_list);457}458459/*******************************************************************************460*461* FUNCTION: acpi_ds_push_walk_state462*463* PARAMETERS: walk_state - State to push464* Thread - Thread state object465*466* RETURN: None467*468* DESCRIPTION: Place the Thread state at the head of the state list469*470******************************************************************************/471472void473acpi_ds_push_walk_state(struct acpi_walk_state *walk_state,474struct acpi_thread_state *thread)475{476ACPI_FUNCTION_TRACE(ds_push_walk_state);477478walk_state->next = thread->walk_state_list;479thread->walk_state_list = walk_state;480481return_VOID;482}483484/*******************************************************************************485*486* FUNCTION: acpi_ds_pop_walk_state487*488* PARAMETERS: Thread - Current thread state489*490* RETURN: A walk_state object popped from the thread's stack491*492* DESCRIPTION: Remove and return the walkstate object that is at the head of493* the walk stack for the given walk list. NULL indicates that494* the list is empty.495*496******************************************************************************/497498struct acpi_walk_state *acpi_ds_pop_walk_state(struct acpi_thread_state *thread)499{500struct acpi_walk_state *walk_state;501502ACPI_FUNCTION_TRACE(ds_pop_walk_state);503504walk_state = thread->walk_state_list;505506if (walk_state) {507508/* Next walk state becomes the current walk state */509510thread->walk_state_list = walk_state->next;511512/*513* Don't clear the NEXT field, this serves as an indicator514* that there is a parent WALK STATE515* Do Not: walk_state->Next = NULL;516*/517}518519return_PTR(walk_state);520}521522/*******************************************************************************523*524* FUNCTION: acpi_ds_create_walk_state525*526* PARAMETERS: owner_id - ID for object creation527* Origin - Starting point for this walk528* method_desc - Method object529* Thread - Current thread state530*531* RETURN: Pointer to the new walk state.532*533* DESCRIPTION: Allocate and initialize a new walk state. The current walk534* state is set to this new state.535*536******************************************************************************/537538struct acpi_walk_state *acpi_ds_create_walk_state(acpi_owner_id owner_id, union acpi_parse_object539*origin, union acpi_operand_object540*method_desc, struct acpi_thread_state541*thread)542{543struct acpi_walk_state *walk_state;544545ACPI_FUNCTION_TRACE(ds_create_walk_state);546547walk_state = ACPI_ALLOCATE_ZEROED(sizeof(struct acpi_walk_state));548if (!walk_state) {549return_PTR(NULL);550}551552walk_state->descriptor_type = ACPI_DESC_TYPE_WALK;553walk_state->method_desc = method_desc;554walk_state->owner_id = owner_id;555walk_state->origin = origin;556walk_state->thread = thread;557558walk_state->parser_state.start_op = origin;559560/* Init the method args/local */561562#if (!defined (ACPI_NO_METHOD_EXECUTION) && !defined (ACPI_CONSTANT_EVAL_ONLY))563acpi_ds_method_data_init(walk_state);564#endif565566/* Put the new state at the head of the walk list */567568if (thread) {569acpi_ds_push_walk_state(walk_state, thread);570}571572return_PTR(walk_state);573}574575/*******************************************************************************576*577* FUNCTION: acpi_ds_init_aml_walk578*579* PARAMETERS: walk_state - New state to be initialized580* Op - Current parse op581* method_node - Control method NS node, if any582* aml_start - Start of AML583* aml_length - Length of AML584* Info - Method info block (params, etc.)585* pass_number - 1, 2, or 3586*587* RETURN: Status588*589* DESCRIPTION: Initialize a walk state for a pass 1 or 2 parse tree walk590*591******************************************************************************/592593acpi_status594acpi_ds_init_aml_walk(struct acpi_walk_state *walk_state,595union acpi_parse_object *op,596struct acpi_namespace_node *method_node,597u8 * aml_start,598u32 aml_length,599struct acpi_evaluate_info *info, u8 pass_number)600{601acpi_status status;602struct acpi_parse_state *parser_state = &walk_state->parser_state;603union acpi_parse_object *extra_op;604605ACPI_FUNCTION_TRACE(ds_init_aml_walk);606607walk_state->parser_state.aml =608walk_state->parser_state.aml_start = aml_start;609walk_state->parser_state.aml_end =610walk_state->parser_state.pkg_end = aml_start + aml_length;611612/* The next_op of the next_walk will be the beginning of the method */613614walk_state->next_op = NULL;615walk_state->pass_number = pass_number;616617if (info) {618walk_state->params = info->parameters;619walk_state->caller_return_desc = &info->return_object;620}621622status = acpi_ps_init_scope(&walk_state->parser_state, op);623if (ACPI_FAILURE(status)) {624return_ACPI_STATUS(status);625}626627if (method_node) {628walk_state->parser_state.start_node = method_node;629walk_state->walk_type = ACPI_WALK_METHOD;630walk_state->method_node = method_node;631walk_state->method_desc =632acpi_ns_get_attached_object(method_node);633634/* Push start scope on scope stack and make it current */635636status =637acpi_ds_scope_stack_push(method_node, ACPI_TYPE_METHOD,638walk_state);639if (ACPI_FAILURE(status)) {640return_ACPI_STATUS(status);641}642643/* Init the method arguments */644645status = acpi_ds_method_data_init_args(walk_state->params,646ACPI_METHOD_NUM_ARGS,647walk_state);648if (ACPI_FAILURE(status)) {649return_ACPI_STATUS(status);650}651} else {652/*653* Setup the current scope.654* Find a Named Op that has a namespace node associated with it.655* search upwards from this Op. Current scope is the first656* Op with a namespace node.657*/658extra_op = parser_state->start_op;659while (extra_op && !extra_op->common.node) {660extra_op = extra_op->common.parent;661}662663if (!extra_op) {664parser_state->start_node = NULL;665} else {666parser_state->start_node = extra_op->common.node;667}668669if (parser_state->start_node) {670671/* Push start scope on scope stack and make it current */672673status =674acpi_ds_scope_stack_push(parser_state->start_node,675parser_state->start_node->676type, walk_state);677if (ACPI_FAILURE(status)) {678return_ACPI_STATUS(status);679}680}681}682683status = acpi_ds_init_callbacks(walk_state, pass_number);684return_ACPI_STATUS(status);685}686687/*******************************************************************************688*689* FUNCTION: acpi_ds_delete_walk_state690*691* PARAMETERS: walk_state - State to delete692*693* RETURN: Status694*695* DESCRIPTION: Delete a walk state including all internal data structures696*697******************************************************************************/698699void acpi_ds_delete_walk_state(struct acpi_walk_state *walk_state)700{701union acpi_generic_state *state;702703ACPI_FUNCTION_TRACE_PTR(ds_delete_walk_state, walk_state);704705if (!walk_state) {706return;707}708709if (walk_state->descriptor_type != ACPI_DESC_TYPE_WALK) {710ACPI_ERROR((AE_INFO, "%p is not a valid walk state",711walk_state));712return;713}714715/* There should not be any open scopes */716717if (walk_state->parser_state.scope) {718ACPI_ERROR((AE_INFO, "%p walk still has a scope list",719walk_state));720acpi_ps_cleanup_scope(&walk_state->parser_state);721}722723/* Always must free any linked control states */724725while (walk_state->control_state) {726state = walk_state->control_state;727walk_state->control_state = state->common.next;728729acpi_ut_delete_generic_state(state);730}731732/* Always must free any linked parse states */733734while (walk_state->scope_info) {735state = walk_state->scope_info;736walk_state->scope_info = state->common.next;737738acpi_ut_delete_generic_state(state);739}740741/* Always must free any stacked result states */742743while (walk_state->results) {744state = walk_state->results;745walk_state->results = state->common.next;746747acpi_ut_delete_generic_state(state);748}749750ACPI_FREE(walk_state);751return_VOID;752}753754755