/*******************************************************************************1*2* Module Name: dsmthdat - control method arguments and local variables3*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 "acdispat.h"46#include "acnamesp.h"47#include "acinterp.h"4849#define _COMPONENT ACPI_DISPATCHER50ACPI_MODULE_NAME("dsmthdat")5152/* Local prototypes */53static void54acpi_ds_method_data_delete_value(u8 type,55u32 index, struct acpi_walk_state *walk_state);5657static acpi_status58acpi_ds_method_data_set_value(u8 type,59u32 index,60union acpi_operand_object *object,61struct acpi_walk_state *walk_state);6263#ifdef ACPI_OBSOLETE_FUNCTIONS64acpi_object_type65acpi_ds_method_data_get_type(u16 opcode,66u32 index, struct acpi_walk_state *walk_state);67#endif6869/*******************************************************************************70*71* FUNCTION: acpi_ds_method_data_init72*73* PARAMETERS: walk_state - Current walk state object74*75* RETURN: Status76*77* DESCRIPTION: Initialize the data structures that hold the method's arguments78* and locals. The data struct is an array of namespace nodes for79* each - this allows ref_of and de_ref_of to work properly for these80* special data types.81*82* NOTES: walk_state fields are initialized to zero by the83* ACPI_ALLOCATE_ZEROED().84*85* A pseudo-Namespace Node is assigned to each argument and local86* so that ref_of() can return a pointer to the Node.87*88******************************************************************************/8990void acpi_ds_method_data_init(struct acpi_walk_state *walk_state)91{92u32 i;9394ACPI_FUNCTION_TRACE(ds_method_data_init);9596/* Init the method arguments */9798for (i = 0; i < ACPI_METHOD_NUM_ARGS; i++) {99ACPI_MOVE_32_TO_32(&walk_state->arguments[i].name,100NAMEOF_ARG_NTE);101walk_state->arguments[i].name.integer |= (i << 24);102walk_state->arguments[i].descriptor_type = ACPI_DESC_TYPE_NAMED;103walk_state->arguments[i].type = ACPI_TYPE_ANY;104walk_state->arguments[i].flags = ANOBJ_METHOD_ARG;105}106107/* Init the method locals */108109for (i = 0; i < ACPI_METHOD_NUM_LOCALS; i++) {110ACPI_MOVE_32_TO_32(&walk_state->local_variables[i].name,111NAMEOF_LOCAL_NTE);112113walk_state->local_variables[i].name.integer |= (i << 24);114walk_state->local_variables[i].descriptor_type =115ACPI_DESC_TYPE_NAMED;116walk_state->local_variables[i].type = ACPI_TYPE_ANY;117walk_state->local_variables[i].flags = ANOBJ_METHOD_LOCAL;118}119120return_VOID;121}122123/*******************************************************************************124*125* FUNCTION: acpi_ds_method_data_delete_all126*127* PARAMETERS: walk_state - Current walk state object128*129* RETURN: None130*131* DESCRIPTION: Delete method locals and arguments. Arguments are only132* deleted if this method was called from another method.133*134******************************************************************************/135136void acpi_ds_method_data_delete_all(struct acpi_walk_state *walk_state)137{138u32 index;139140ACPI_FUNCTION_TRACE(ds_method_data_delete_all);141142/* Detach the locals */143144for (index = 0; index < ACPI_METHOD_NUM_LOCALS; index++) {145if (walk_state->local_variables[index].object) {146ACPI_DEBUG_PRINT((ACPI_DB_EXEC, "Deleting Local%u=%p\n",147index,148walk_state->local_variables[index].149object));150151/* Detach object (if present) and remove a reference */152153acpi_ns_detach_object(&walk_state->154local_variables[index]);155}156}157158/* Detach the arguments */159160for (index = 0; index < ACPI_METHOD_NUM_ARGS; index++) {161if (walk_state->arguments[index].object) {162ACPI_DEBUG_PRINT((ACPI_DB_EXEC, "Deleting Arg%u=%p\n",163index,164walk_state->arguments[index].object));165166/* Detach object (if present) and remove a reference */167168acpi_ns_detach_object(&walk_state->arguments[index]);169}170}171172return_VOID;173}174175/*******************************************************************************176*177* FUNCTION: acpi_ds_method_data_init_args178*179* PARAMETERS: *Params - Pointer to a parameter list for the method180* max_param_count - The arg count for this method181* walk_state - Current walk state object182*183* RETURN: Status184*185* DESCRIPTION: Initialize arguments for a method. The parameter list is a list186* of ACPI operand objects, either null terminated or whose length187* is defined by max_param_count.188*189******************************************************************************/190191acpi_status192acpi_ds_method_data_init_args(union acpi_operand_object **params,193u32 max_param_count,194struct acpi_walk_state *walk_state)195{196acpi_status status;197u32 index = 0;198199ACPI_FUNCTION_TRACE_PTR(ds_method_data_init_args, params);200201if (!params) {202ACPI_DEBUG_PRINT((ACPI_DB_EXEC,203"No param list passed to method\n"));204return_ACPI_STATUS(AE_OK);205}206207/* Copy passed parameters into the new method stack frame */208209while ((index < ACPI_METHOD_NUM_ARGS) &&210(index < max_param_count) && params[index]) {211/*212* A valid parameter.213* Store the argument in the method/walk descriptor.214* Do not copy the arg in order to implement call by reference215*/216status = acpi_ds_method_data_set_value(ACPI_REFCLASS_ARG, index,217params[index],218walk_state);219if (ACPI_FAILURE(status)) {220return_ACPI_STATUS(status);221}222223index++;224}225226ACPI_DEBUG_PRINT((ACPI_DB_EXEC, "%u args passed to method\n", index));227return_ACPI_STATUS(AE_OK);228}229230/*******************************************************************************231*232* FUNCTION: acpi_ds_method_data_get_node233*234* PARAMETERS: Type - Either ACPI_REFCLASS_LOCAL or235* ACPI_REFCLASS_ARG236* Index - Which Local or Arg whose type to get237* walk_state - Current walk state object238* Node - Where the node is returned.239*240* RETURN: Status and node241*242* DESCRIPTION: Get the Node associated with a local or arg.243*244******************************************************************************/245246acpi_status247acpi_ds_method_data_get_node(u8 type,248u32 index,249struct acpi_walk_state *walk_state,250struct acpi_namespace_node **node)251{252ACPI_FUNCTION_TRACE(ds_method_data_get_node);253254/*255* Method Locals and Arguments are supported256*/257switch (type) {258case ACPI_REFCLASS_LOCAL:259260if (index > ACPI_METHOD_MAX_LOCAL) {261ACPI_ERROR((AE_INFO,262"Local index %u is invalid (max %u)",263index, ACPI_METHOD_MAX_LOCAL));264return_ACPI_STATUS(AE_AML_INVALID_INDEX);265}266267/* Return a pointer to the pseudo-node */268269*node = &walk_state->local_variables[index];270break;271272case ACPI_REFCLASS_ARG:273274if (index > ACPI_METHOD_MAX_ARG) {275ACPI_ERROR((AE_INFO,276"Arg index %u is invalid (max %u)",277index, ACPI_METHOD_MAX_ARG));278return_ACPI_STATUS(AE_AML_INVALID_INDEX);279}280281/* Return a pointer to the pseudo-node */282283*node = &walk_state->arguments[index];284break;285286default:287ACPI_ERROR((AE_INFO, "Type %u is invalid", type));288return_ACPI_STATUS(AE_TYPE);289}290291return_ACPI_STATUS(AE_OK);292}293294/*******************************************************************************295*296* FUNCTION: acpi_ds_method_data_set_value297*298* PARAMETERS: Type - Either ACPI_REFCLASS_LOCAL or299* ACPI_REFCLASS_ARG300* Index - Which Local or Arg to get301* Object - Object to be inserted into the stack entry302* walk_state - Current walk state object303*304* RETURN: Status305*306* DESCRIPTION: Insert an object onto the method stack at entry Opcode:Index.307* Note: There is no "implicit conversion" for locals.308*309******************************************************************************/310311static acpi_status312acpi_ds_method_data_set_value(u8 type,313u32 index,314union acpi_operand_object *object,315struct acpi_walk_state *walk_state)316{317acpi_status status;318struct acpi_namespace_node *node;319320ACPI_FUNCTION_TRACE(ds_method_data_set_value);321322ACPI_DEBUG_PRINT((ACPI_DB_EXEC,323"NewObj %p Type %2.2X, Refs=%u [%s]\n", object,324type, object->common.reference_count,325acpi_ut_get_type_name(object->common.type)));326327/* Get the namespace node for the arg/local */328329status = acpi_ds_method_data_get_node(type, index, walk_state, &node);330if (ACPI_FAILURE(status)) {331return_ACPI_STATUS(status);332}333334/*335* Increment ref count so object can't be deleted while installed.336* NOTE: We do not copy the object in order to preserve the call by337* reference semantics of ACPI Control Method invocation.338* (See ACPI Specification 2.0_c)339*/340acpi_ut_add_reference(object);341342/* Install the object */343344node->object = object;345return_ACPI_STATUS(status);346}347348/*******************************************************************************349*350* FUNCTION: acpi_ds_method_data_get_value351*352* PARAMETERS: Type - Either ACPI_REFCLASS_LOCAL or353* ACPI_REFCLASS_ARG354* Index - Which local_var or argument to get355* walk_state - Current walk state object356* dest_desc - Where Arg or Local value is returned357*358* RETURN: Status359*360* DESCRIPTION: Retrieve value of selected Arg or Local for this method361* Used only in acpi_ex_resolve_to_value().362*363******************************************************************************/364365acpi_status366acpi_ds_method_data_get_value(u8 type,367u32 index,368struct acpi_walk_state *walk_state,369union acpi_operand_object **dest_desc)370{371acpi_status status;372struct acpi_namespace_node *node;373union acpi_operand_object *object;374375ACPI_FUNCTION_TRACE(ds_method_data_get_value);376377/* Validate the object descriptor */378379if (!dest_desc) {380ACPI_ERROR((AE_INFO, "Null object descriptor pointer"));381return_ACPI_STATUS(AE_BAD_PARAMETER);382}383384/* Get the namespace node for the arg/local */385386status = acpi_ds_method_data_get_node(type, index, walk_state, &node);387if (ACPI_FAILURE(status)) {388return_ACPI_STATUS(status);389}390391/* Get the object from the node */392393object = node->object;394395/* Examine the returned object, it must be valid. */396397if (!object) {398/*399* Index points to uninitialized object.400* This means that either 1) The expected argument was401* not passed to the method, or 2) A local variable402* was referenced by the method (via the ASL)403* before it was initialized. Either case is an error.404*/405406/* If slack enabled, init the local_x/arg_x to an Integer of value zero */407408if (acpi_gbl_enable_interpreter_slack) {409object = acpi_ut_create_integer_object((u64) 0);410if (!object) {411return_ACPI_STATUS(AE_NO_MEMORY);412}413414node->object = object;415}416417/* Otherwise, return the error */418419else420switch (type) {421case ACPI_REFCLASS_ARG:422423ACPI_ERROR((AE_INFO,424"Uninitialized Arg[%u] at node %p",425index, node));426427return_ACPI_STATUS(AE_AML_UNINITIALIZED_ARG);428429case ACPI_REFCLASS_LOCAL:430431/*432* No error message for this case, will be trapped again later to433* detect and ignore cases of Store(local_x,local_x)434*/435return_ACPI_STATUS(AE_AML_UNINITIALIZED_LOCAL);436437default:438439ACPI_ERROR((AE_INFO,440"Not a Arg/Local opcode: 0x%X",441type));442return_ACPI_STATUS(AE_AML_INTERNAL);443}444}445446/*447* The Index points to an initialized and valid object.448* Return an additional reference to the object449*/450*dest_desc = object;451acpi_ut_add_reference(object);452453return_ACPI_STATUS(AE_OK);454}455456/*******************************************************************************457*458* FUNCTION: acpi_ds_method_data_delete_value459*460* PARAMETERS: Type - Either ACPI_REFCLASS_LOCAL or461* ACPI_REFCLASS_ARG462* Index - Which local_var or argument to delete463* walk_state - Current walk state object464*465* RETURN: None466*467* DESCRIPTION: Delete the entry at Opcode:Index. Inserts468* a null into the stack slot after the object is deleted.469*470******************************************************************************/471472static void473acpi_ds_method_data_delete_value(u8 type,474u32 index, struct acpi_walk_state *walk_state)475{476acpi_status status;477struct acpi_namespace_node *node;478union acpi_operand_object *object;479480ACPI_FUNCTION_TRACE(ds_method_data_delete_value);481482/* Get the namespace node for the arg/local */483484status = acpi_ds_method_data_get_node(type, index, walk_state, &node);485if (ACPI_FAILURE(status)) {486return_VOID;487}488489/* Get the associated object */490491object = acpi_ns_get_attached_object(node);492493/*494* Undefine the Arg or Local by setting its descriptor495* pointer to NULL. Locals/Args can contain both496* ACPI_OPERAND_OBJECTS and ACPI_NAMESPACE_NODEs497*/498node->object = NULL;499500if ((object) &&501(ACPI_GET_DESCRIPTOR_TYPE(object) == ACPI_DESC_TYPE_OPERAND)) {502/*503* There is a valid object.504* Decrement the reference count by one to balance the505* increment when the object was stored.506*/507acpi_ut_remove_reference(object);508}509510return_VOID;511}512513/*******************************************************************************514*515* FUNCTION: acpi_ds_store_object_to_local516*517* PARAMETERS: Type - Either ACPI_REFCLASS_LOCAL or518* ACPI_REFCLASS_ARG519* Index - Which Local or Arg to set520* obj_desc - Value to be stored521* walk_state - Current walk state522*523* RETURN: Status524*525* DESCRIPTION: Store a value in an Arg or Local. The obj_desc is installed526* as the new value for the Arg or Local and the reference count527* for obj_desc is incremented.528*529******************************************************************************/530531acpi_status532acpi_ds_store_object_to_local(u8 type,533u32 index,534union acpi_operand_object *obj_desc,535struct acpi_walk_state *walk_state)536{537acpi_status status;538struct acpi_namespace_node *node;539union acpi_operand_object *current_obj_desc;540union acpi_operand_object *new_obj_desc;541542ACPI_FUNCTION_TRACE(ds_store_object_to_local);543ACPI_DEBUG_PRINT((ACPI_DB_EXEC, "Type=%2.2X Index=%u Obj=%p\n",544type, index, obj_desc));545546/* Parameter validation */547548if (!obj_desc) {549return_ACPI_STATUS(AE_BAD_PARAMETER);550}551552/* Get the namespace node for the arg/local */553554status = acpi_ds_method_data_get_node(type, index, walk_state, &node);555if (ACPI_FAILURE(status)) {556return_ACPI_STATUS(status);557}558559current_obj_desc = acpi_ns_get_attached_object(node);560if (current_obj_desc == obj_desc) {561ACPI_DEBUG_PRINT((ACPI_DB_EXEC, "Obj=%p already installed!\n",562obj_desc));563return_ACPI_STATUS(status);564}565566/*567* If the reference count on the object is more than one, we must568* take a copy of the object before we store. A reference count569* of exactly 1 means that the object was just created during the570* evaluation of an expression, and we can safely use it since it571* is not used anywhere else.572*/573new_obj_desc = obj_desc;574if (obj_desc->common.reference_count > 1) {575status =576acpi_ut_copy_iobject_to_iobject(obj_desc, &new_obj_desc,577walk_state);578if (ACPI_FAILURE(status)) {579return_ACPI_STATUS(status);580}581}582583/*584* If there is an object already in this slot, we either585* have to delete it, or if this is an argument and there586* is an object reference stored there, we have to do587* an indirect store!588*/589if (current_obj_desc) {590/*591* Check for an indirect store if an argument592* contains an object reference (stored as an Node).593* We don't allow this automatic dereferencing for594* locals, since a store to a local should overwrite595* anything there, including an object reference.596*597* If both Arg0 and Local0 contain ref_of (Local4):598*599* Store (1, Arg0) - Causes indirect store to local4600* Store (1, Local0) - Stores 1 in local0, overwriting601* the reference to local4602* Store (1, de_refof (Local0)) - Causes indirect store to local4603*604* Weird, but true.605*/606if (type == ACPI_REFCLASS_ARG) {607/*608* If we have a valid reference object that came from ref_of(),609* do the indirect store610*/611if ((ACPI_GET_DESCRIPTOR_TYPE(current_obj_desc) ==612ACPI_DESC_TYPE_OPERAND)613&& (current_obj_desc->common.type ==614ACPI_TYPE_LOCAL_REFERENCE)615&& (current_obj_desc->reference.class ==616ACPI_REFCLASS_REFOF)) {617ACPI_DEBUG_PRINT((ACPI_DB_EXEC,618"Arg (%p) is an ObjRef(Node), storing in node %p\n",619new_obj_desc,620current_obj_desc));621622/*623* Store this object to the Node (perform the indirect store)624* NOTE: No implicit conversion is performed, as per the ACPI625* specification rules on storing to Locals/Args.626*/627status =628acpi_ex_store_object_to_node(new_obj_desc,629current_obj_desc->630reference.631object,632walk_state,633ACPI_NO_IMPLICIT_CONVERSION);634635/* Remove local reference if we copied the object above */636637if (new_obj_desc != obj_desc) {638acpi_ut_remove_reference(new_obj_desc);639}640return_ACPI_STATUS(status);641}642}643644/* Delete the existing object before storing the new one */645646acpi_ds_method_data_delete_value(type, index, walk_state);647}648649/*650* Install the Obj descriptor (*new_obj_desc) into651* the descriptor for the Arg or Local.652* (increments the object reference count by one)653*/654status =655acpi_ds_method_data_set_value(type, index, new_obj_desc,656walk_state);657658/* Remove local reference if we copied the object above */659660if (new_obj_desc != obj_desc) {661acpi_ut_remove_reference(new_obj_desc);662}663664return_ACPI_STATUS(status);665}666667#ifdef ACPI_OBSOLETE_FUNCTIONS668/*******************************************************************************669*670* FUNCTION: acpi_ds_method_data_get_type671*672* PARAMETERS: Opcode - Either AML_LOCAL_OP or AML_ARG_OP673* Index - Which Local or Arg whose type to get674* walk_state - Current walk state object675*676* RETURN: Data type of current value of the selected Arg or Local677*678* DESCRIPTION: Get the type of the object stored in the Local or Arg679*680******************************************************************************/681682acpi_object_type683acpi_ds_method_data_get_type(u16 opcode,684u32 index, struct acpi_walk_state *walk_state)685{686acpi_status status;687struct acpi_namespace_node *node;688union acpi_operand_object *object;689690ACPI_FUNCTION_TRACE(ds_method_data_get_type);691692/* Get the namespace node for the arg/local */693694status = acpi_ds_method_data_get_node(opcode, index, walk_state, &node);695if (ACPI_FAILURE(status)) {696return_VALUE((ACPI_TYPE_NOT_FOUND));697}698699/* Get the object */700701object = acpi_ns_get_attached_object(node);702if (!object) {703704/* Uninitialized local/arg, return TYPE_ANY */705706return_VALUE(ACPI_TYPE_ANY);707}708709/* Get the object type */710711return_VALUE(object->type);712}713#endif714715716