// SPDX-License-Identifier: BSD-3-Clause OR GPL-2.01/*******************************************************************************2*3* Module Name: dsutils - Dispatcher utilities4*5******************************************************************************/67#include <acpi/acpi.h>8#include "accommon.h"9#include "acparser.h"10#include "amlcode.h"11#include "acdispat.h"12#include "acinterp.h"13#include "acnamesp.h"14#include "acdebug.h"1516#define _COMPONENT ACPI_DISPATCHER17ACPI_MODULE_NAME("dsutils")1819/*******************************************************************************20*21* FUNCTION: acpi_ds_clear_implicit_return22*23* PARAMETERS: walk_state - Current State24*25* RETURN: None.26*27* DESCRIPTION: Clear and remove a reference on an implicit return value. Used28* to delete "stale" return values (if enabled, the return value29* from every operator is saved at least momentarily, in case the30* parent method exits.)31*32******************************************************************************/33void acpi_ds_clear_implicit_return(struct acpi_walk_state *walk_state)34{35ACPI_FUNCTION_NAME(ds_clear_implicit_return);3637/*38* Slack must be enabled for this feature39*/40if (!acpi_gbl_enable_interpreter_slack) {41return;42}4344if (walk_state->implicit_return_obj) {45/*46* Delete any "stale" implicit return. However, in47* complex statements, the implicit return value can be48* bubbled up several levels.49*/50ACPI_DEBUG_PRINT((ACPI_DB_DISPATCH,51"Removing reference on stale implicit return obj %p\n",52walk_state->implicit_return_obj));5354acpi_ut_remove_reference(walk_state->implicit_return_obj);55walk_state->implicit_return_obj = NULL;56}57}5859/*******************************************************************************60*61* FUNCTION: acpi_ds_do_implicit_return62*63* PARAMETERS: return_desc - The return value64* walk_state - Current State65* add_reference - True if a reference should be added to the66* return object67*68* RETURN: TRUE if implicit return enabled, FALSE otherwise69*70* DESCRIPTION: Implements the optional "implicit return". We save the result71* of every ASL operator and control method invocation in case the72* parent method exit. Before storing a new return value, we73* delete the previous return value.74*75******************************************************************************/7677u878acpi_ds_do_implicit_return(union acpi_operand_object *return_desc,79struct acpi_walk_state *walk_state, u8 add_reference)80{81ACPI_FUNCTION_NAME(ds_do_implicit_return);8283/*84* Slack must be enabled for this feature, and we must85* have a valid return object86*/87if ((!acpi_gbl_enable_interpreter_slack) || (!return_desc)) {88return (FALSE);89}9091ACPI_DEBUG_PRINT((ACPI_DB_DISPATCH,92"Result %p will be implicitly returned; Prev=%p\n",93return_desc, walk_state->implicit_return_obj));9495/*96* Delete any "stale" implicit return value first. However, in97* complex statements, the implicit return value can be98* bubbled up several levels, so we don't clear the value if it99* is the same as the return_desc.100*/101if (walk_state->implicit_return_obj) {102if (walk_state->implicit_return_obj == return_desc) {103return (TRUE);104}105acpi_ds_clear_implicit_return(walk_state);106}107108/* Save the implicit return value, add a reference if requested */109110walk_state->implicit_return_obj = return_desc;111if (add_reference) {112acpi_ut_add_reference(return_desc);113}114115return (TRUE);116}117118/*******************************************************************************119*120* FUNCTION: acpi_ds_is_result_used121*122* PARAMETERS: op - Current Op123* walk_state - Current State124*125* RETURN: TRUE if result is used, FALSE otherwise126*127* DESCRIPTION: Check if a result object will be used by the parent128*129******************************************************************************/130131u8132acpi_ds_is_result_used(union acpi_parse_object * op,133struct acpi_walk_state * walk_state)134{135const struct acpi_opcode_info *parent_info;136137ACPI_FUNCTION_TRACE_PTR(ds_is_result_used, op);138139/* Must have both an Op and a Result Object */140141if (!op) {142ACPI_ERROR((AE_INFO, "Null Op"));143return_UINT8(TRUE);144}145146/*147* We know that this operator is not a148* Return() operator (would not come here.) The following code is the149* optional support for a so-called "implicit return". Some AML code150* assumes that the last value of the method is "implicitly" returned151* to the caller. Just save the last result as the return value.152* NOTE: this is optional because the ASL language does not actually153* support this behavior.154*/155(void)acpi_ds_do_implicit_return(walk_state->result_obj, walk_state,156TRUE);157158/*159* Now determine if the parent will use the result160*161* If there is no parent, or the parent is a scope_op, we are executing162* at the method level. An executing method typically has no parent,163* since each method is parsed separately. A method invoked externally164* via execute_control_method has a scope_op as the parent.165*/166if ((!op->common.parent) ||167(op->common.parent->common.aml_opcode == AML_SCOPE_OP)) {168169/* No parent, the return value cannot possibly be used */170171ACPI_DEBUG_PRINT((ACPI_DB_DISPATCH,172"At Method level, result of [%s] not used\n",173acpi_ps_get_opcode_name(op->common.174aml_opcode)));175return_UINT8(FALSE);176}177178/* Get info on the parent. The root_op is AML_SCOPE */179180parent_info =181acpi_ps_get_opcode_info(op->common.parent->common.aml_opcode);182if (parent_info->class == AML_CLASS_UNKNOWN) {183ACPI_ERROR((AE_INFO, "Unknown parent opcode Op=%p", op));184return_UINT8(FALSE);185}186187/*188* Decide what to do with the result based on the parent. If189* the parent opcode will not use the result, delete the object.190* Otherwise leave it as is, it will be deleted when it is used191* as an operand later.192*/193switch (parent_info->class) {194case AML_CLASS_CONTROL:195196switch (op->common.parent->common.aml_opcode) {197case AML_RETURN_OP:198199/* Never delete the return value associated with a return opcode */200201goto result_used;202203case AML_IF_OP:204case AML_WHILE_OP:205/*206* If we are executing the predicate AND this is the predicate op,207* we will use the return value208*/209if ((walk_state->control_state->common.state ==210ACPI_CONTROL_PREDICATE_EXECUTING) &&211(walk_state->control_state->control.predicate_op ==212op)) {213goto result_used;214}215break;216217default:218219/* Ignore other control opcodes */220221break;222}223224/* The general control opcode returns no result */225226goto result_not_used;227228case AML_CLASS_CREATE:229/*230* These opcodes allow term_arg(s) as operands and therefore231* the operands can be method calls. The result is used.232*/233goto result_used;234235case AML_CLASS_NAMED_OBJECT:236237if ((op->common.parent->common.aml_opcode == AML_REGION_OP) ||238(op->common.parent->common.aml_opcode == AML_DATA_REGION_OP)239|| (op->common.parent->common.aml_opcode == AML_PACKAGE_OP)240|| (op->common.parent->common.aml_opcode == AML_BUFFER_OP)241|| (op->common.parent->common.aml_opcode ==242AML_VARIABLE_PACKAGE_OP)243|| (op->common.parent->common.aml_opcode ==244AML_INT_EVAL_SUBTREE_OP)245|| (op->common.parent->common.aml_opcode ==246AML_BANK_FIELD_OP)) {247/*248* These opcodes allow term_arg(s) as operands and therefore249* the operands can be method calls. The result is used.250*/251goto result_used;252}253254goto result_not_used;255256default:257/*258* In all other cases. the parent will actually use the return259* object, so keep it.260*/261goto result_used;262}263264result_used:265ACPI_DEBUG_PRINT((ACPI_DB_DISPATCH,266"Result of [%s] used by Parent [%s] Op=%p\n",267acpi_ps_get_opcode_name(op->common.aml_opcode),268acpi_ps_get_opcode_name(op->common.parent->common.269aml_opcode), op));270271return_UINT8(TRUE);272273result_not_used:274ACPI_DEBUG_PRINT((ACPI_DB_DISPATCH,275"Result of [%s] not used by Parent [%s] Op=%p\n",276acpi_ps_get_opcode_name(op->common.aml_opcode),277acpi_ps_get_opcode_name(op->common.parent->common.278aml_opcode), op));279280return_UINT8(FALSE);281}282283/*******************************************************************************284*285* FUNCTION: acpi_ds_delete_result_if_not_used286*287* PARAMETERS: op - Current parse Op288* result_obj - Result of the operation289* walk_state - Current state290*291* RETURN: Status292*293* DESCRIPTION: Used after interpretation of an opcode. If there is an internal294* result descriptor, check if the parent opcode will actually use295* this result. If not, delete the result now so that it will296* not become orphaned.297*298******************************************************************************/299300void301acpi_ds_delete_result_if_not_used(union acpi_parse_object *op,302union acpi_operand_object *result_obj,303struct acpi_walk_state *walk_state)304{305union acpi_operand_object *obj_desc;306acpi_status status;307308ACPI_FUNCTION_TRACE_PTR(ds_delete_result_if_not_used, result_obj);309310if (!op) {311ACPI_ERROR((AE_INFO, "Null Op"));312return_VOID;313}314315if (!result_obj) {316return_VOID;317}318319if (!acpi_ds_is_result_used(op, walk_state)) {320321/* Must pop the result stack (obj_desc should be equal to result_obj) */322323status = acpi_ds_result_pop(&obj_desc, walk_state);324if (ACPI_SUCCESS(status)) {325acpi_ut_remove_reference(result_obj);326}327}328329return_VOID;330}331332/*******************************************************************************333*334* FUNCTION: acpi_ds_resolve_operands335*336* PARAMETERS: walk_state - Current walk state with operands on stack337*338* RETURN: Status339*340* DESCRIPTION: Resolve all operands to their values. Used to prepare341* arguments to a control method invocation (a call from one342* method to another.)343*344******************************************************************************/345346acpi_status acpi_ds_resolve_operands(struct acpi_walk_state *walk_state)347{348u32 i;349acpi_status status = AE_OK;350351ACPI_FUNCTION_TRACE_PTR(ds_resolve_operands, walk_state);352353/*354* Attempt to resolve each of the valid operands355* Method arguments are passed by reference, not by value. This means356* that the actual objects are passed, not copies of the objects.357*/358for (i = 0; i < walk_state->num_operands; i++) {359status =360acpi_ex_resolve_to_value(&walk_state->operands[i],361walk_state);362if (ACPI_FAILURE(status)) {363break;364}365}366367return_ACPI_STATUS(status);368}369370/*******************************************************************************371*372* FUNCTION: acpi_ds_clear_operands373*374* PARAMETERS: walk_state - Current walk state with operands on stack375*376* RETURN: None377*378* DESCRIPTION: Clear all operands on the current walk state operand stack.379*380******************************************************************************/381382void acpi_ds_clear_operands(struct acpi_walk_state *walk_state)383{384u32 i;385386ACPI_FUNCTION_TRACE_PTR(ds_clear_operands, walk_state);387388/* Remove a reference on each operand on the stack */389390for (i = 0; i < walk_state->num_operands; i++) {391/*392* Remove a reference to all operands, including both393* "Arguments" and "Targets".394*/395acpi_ut_remove_reference(walk_state->operands[i]);396walk_state->operands[i] = NULL;397}398399walk_state->num_operands = 0;400return_VOID;401}402403/*******************************************************************************404*405* FUNCTION: acpi_ds_create_operand406*407* PARAMETERS: walk_state - Current walk state408* arg - Parse object for the argument409* arg_index - Which argument (zero based)410*411* RETURN: Status412*413* DESCRIPTION: Translate a parse tree object that is an argument to an AML414* opcode to the equivalent interpreter object. This may include415* looking up a name or entering a new name into the internal416* namespace.417*418******************************************************************************/419420acpi_status421acpi_ds_create_operand(struct acpi_walk_state *walk_state,422union acpi_parse_object *arg, u32 arg_index)423{424acpi_status status = AE_OK;425char *name_string;426u32 name_length;427union acpi_operand_object *obj_desc;428union acpi_parse_object *parent_op;429u16 opcode;430acpi_interpreter_mode interpreter_mode;431const struct acpi_opcode_info *op_info;432433ACPI_FUNCTION_TRACE_PTR(ds_create_operand, arg);434435/* A valid name must be looked up in the namespace */436437if ((arg->common.aml_opcode == AML_INT_NAMEPATH_OP) &&438(arg->common.value.string) &&439!(arg->common.flags & ACPI_PARSEOP_IN_STACK)) {440ACPI_DEBUG_PRINT((ACPI_DB_DISPATCH, "Getting a name: Arg=%p\n",441arg));442443/* Get the entire name string from the AML stream */444445status = acpi_ex_get_name_string(ACPI_TYPE_ANY,446arg->common.value.buffer,447&name_string, &name_length);448449if (ACPI_FAILURE(status)) {450return_ACPI_STATUS(status);451}452453/* All prefixes have been handled, and the name is in name_string */454455/*456* Special handling for buffer_field declarations. This is a deferred457* opcode that unfortunately defines the field name as the last458* parameter instead of the first. We get here when we are performing459* the deferred execution, so the actual name of the field is already460* in the namespace. We don't want to attempt to look it up again461* because we may be executing in a different scope than where the462* actual opcode exists.463*/464if ((walk_state->deferred_node) &&465(walk_state->deferred_node->type == ACPI_TYPE_BUFFER_FIELD)466&& (arg_index == (u32)467((walk_state->opcode == AML_CREATE_FIELD_OP) ? 3 : 2))) {468obj_desc =469ACPI_CAST_PTR(union acpi_operand_object,470walk_state->deferred_node);471status = AE_OK;472} else { /* All other opcodes */473474/*475* Differentiate between a namespace "create" operation476* versus a "lookup" operation (IMODE_LOAD_PASS2 vs.477* IMODE_EXECUTE) in order to support the creation of478* namespace objects during the execution of control methods.479*/480parent_op = arg->common.parent;481op_info =482acpi_ps_get_opcode_info(parent_op->common.483aml_opcode);484485if ((op_info->flags & AML_NSNODE) &&486(parent_op->common.aml_opcode !=487AML_INT_METHODCALL_OP)488&& (parent_op->common.aml_opcode != AML_REGION_OP)489&& (parent_op->common.aml_opcode !=490AML_INT_NAMEPATH_OP)) {491492/* Enter name into namespace if not found */493494interpreter_mode = ACPI_IMODE_LOAD_PASS2;495} else {496/* Return a failure if name not found */497498interpreter_mode = ACPI_IMODE_EXECUTE;499}500501status =502acpi_ns_lookup(walk_state->scope_info, name_string,503ACPI_TYPE_ANY, interpreter_mode,504ACPI_NS_SEARCH_PARENT |505ACPI_NS_DONT_OPEN_SCOPE, walk_state,506ACPI_CAST_INDIRECT_PTR(struct507acpi_namespace_node,508&obj_desc));509/*510* The only case where we pass through (ignore) a NOT_FOUND511* error is for the cond_ref_of opcode.512*/513if (status == AE_NOT_FOUND) {514if (parent_op->common.aml_opcode ==515AML_CONDITIONAL_REF_OF_OP) {516/*517* For the Conditional Reference op, it's OK if518* the name is not found; We just need a way to519* indicate this to the interpreter, set the520* object to the root521*/522obj_desc =523ACPI_CAST_PTR(union524acpi_operand_object,525acpi_gbl_root_node);526status = AE_OK;527} else if (parent_op->common.aml_opcode ==528AML_EXTERNAL_OP) {529/*530* This opcode should never appear here. It is used only531* by AML disassemblers and is surrounded by an If(0)532* by the ASL compiler.533*534* Therefore, if we see it here, it is a serious error.535*/536status = AE_AML_BAD_OPCODE;537} else {538/*539* We just plain didn't find it -- which is a540* very serious error at this point541*/542status = AE_AML_NAME_NOT_FOUND;543}544}545546if (ACPI_FAILURE(status)) {547ACPI_ERROR_NAMESPACE(walk_state->scope_info,548name_string, status);549}550}551552/* Free the namestring created above */553554ACPI_FREE(name_string);555556/* Check status from the lookup */557558if (ACPI_FAILURE(status)) {559return_ACPI_STATUS(status);560}561562/* Put the resulting object onto the current object stack */563564status = acpi_ds_obj_stack_push(obj_desc, walk_state);565if (ACPI_FAILURE(status)) {566return_ACPI_STATUS(status);567}568569acpi_db_display_argument_object(obj_desc, walk_state);570} else {571/* Check for null name case */572573if ((arg->common.aml_opcode == AML_INT_NAMEPATH_OP) &&574!(arg->common.flags & ACPI_PARSEOP_IN_STACK)) {575/*576* If the name is null, this means that this is an577* optional result parameter that was not specified578* in the original ASL. Create a Zero Constant for a579* placeholder. (Store to a constant is a Noop.)580*/581opcode = AML_ZERO_OP; /* Has no arguments! */582583ACPI_DEBUG_PRINT((ACPI_DB_DISPATCH,584"Null namepath: Arg=%p\n", arg));585} else {586opcode = arg->common.aml_opcode;587}588589/* Get the object type of the argument */590591op_info = acpi_ps_get_opcode_info(opcode);592if (op_info->object_type == ACPI_TYPE_INVALID) {593return_ACPI_STATUS(AE_NOT_IMPLEMENTED);594}595596if ((op_info->flags & AML_HAS_RETVAL) ||597(arg->common.flags & ACPI_PARSEOP_IN_STACK)) {598/*599* Use value that was already previously returned600* by the evaluation of this argument601*/602status = acpi_ds_result_pop(&obj_desc, walk_state);603if (ACPI_FAILURE(status)) {604/*605* Only error is underflow, and this indicates606* a missing or null operand!607*/608ACPI_EXCEPTION((AE_INFO, status,609"Missing or null operand"));610return_ACPI_STATUS(status);611}612} else {613/* Create an ACPI_INTERNAL_OBJECT for the argument */614615obj_desc =616acpi_ut_create_internal_object(op_info->617object_type);618if (!obj_desc) {619return_ACPI_STATUS(AE_NO_MEMORY);620}621622/* Initialize the new object */623624status =625acpi_ds_init_object_from_op(walk_state, arg, opcode,626&obj_desc);627if (ACPI_FAILURE(status)) {628acpi_ut_delete_object_desc(obj_desc);629return_ACPI_STATUS(status);630}631}632633/* Put the operand object on the object stack */634635status = acpi_ds_obj_stack_push(obj_desc, walk_state);636if (ACPI_FAILURE(status)) {637return_ACPI_STATUS(status);638}639640acpi_db_display_argument_object(obj_desc, walk_state);641}642643return_ACPI_STATUS(AE_OK);644}645646/*******************************************************************************647*648* FUNCTION: acpi_ds_create_operands649*650* PARAMETERS: walk_state - Current state651* first_arg - First argument of a parser argument tree652*653* RETURN: Status654*655* DESCRIPTION: Convert an operator's arguments from a parse tree format to656* namespace objects and place those argument object on the object657* stack in preparation for evaluation by the interpreter.658*659******************************************************************************/660661acpi_status662acpi_ds_create_operands(struct acpi_walk_state *walk_state,663union acpi_parse_object *first_arg)664{665acpi_status status = AE_OK;666union acpi_parse_object *arg;667union acpi_parse_object *arguments[ACPI_OBJ_NUM_OPERANDS];668u32 arg_count = 0;669u32 index = walk_state->num_operands;670u32 prev_num_operands = walk_state->num_operands;671u32 new_num_operands;672u32 i;673674ACPI_FUNCTION_TRACE_PTR(ds_create_operands, first_arg);675676/* Get all arguments in the list */677678arg = first_arg;679while (arg) {680if (index >= ACPI_OBJ_NUM_OPERANDS) {681return_ACPI_STATUS(AE_BAD_DATA);682}683684arguments[index] = arg;685walk_state->operands[index] = NULL;686687/* Move on to next argument, if any */688689arg = arg->common.next;690arg_count++;691index++;692}693694ACPI_DEBUG_PRINT((ACPI_DB_DISPATCH,695"NumOperands %d, ArgCount %d, Index %d\n",696walk_state->num_operands, arg_count, index));697698/* Create the interpreter arguments, in reverse order */699700new_num_operands = index;701index--;702for (i = 0; i < arg_count; i++) {703arg = arguments[index];704walk_state->operand_index = (u8)index;705706status = acpi_ds_create_operand(walk_state, arg, index);707if (ACPI_FAILURE(status)) {708goto cleanup;709}710711ACPI_DEBUG_PRINT((ACPI_DB_DISPATCH,712"Created Arg #%u (%p) %u args total\n",713index, arg, arg_count));714index--;715}716717return_ACPI_STATUS(status);718719cleanup:720/*721* We must undo everything done above; meaning that we must722* pop everything off of the operand stack and delete those723* objects724*/725walk_state->num_operands = (u8)(i);726acpi_ds_obj_stack_pop_and_delete(new_num_operands, walk_state);727728/* Restore operand count */729walk_state->num_operands = (u8)(prev_num_operands);730731ACPI_EXCEPTION((AE_INFO, status, "While creating Arg %u", index));732return_ACPI_STATUS(status);733}734735/*****************************************************************************736*737* FUNCTION: acpi_ds_evaluate_name_path738*739* PARAMETERS: walk_state - Current state of the parse tree walk,740* the opcode of current operation should be741* AML_INT_NAMEPATH_OP742*743* RETURN: Status744*745* DESCRIPTION: Translate the -name_path- parse tree object to the equivalent746* interpreter object, convert it to value, if needed, duplicate747* it, if needed, and push it onto the current result stack.748*749****************************************************************************/750751acpi_status acpi_ds_evaluate_name_path(struct acpi_walk_state *walk_state)752{753acpi_status status = AE_OK;754union acpi_parse_object *op = walk_state->op;755union acpi_operand_object **operand = &walk_state->operands[0];756union acpi_operand_object *new_obj_desc;757u8 type;758759ACPI_FUNCTION_TRACE_PTR(ds_evaluate_name_path, walk_state);760761if (!op->common.parent) {762763/* This happens after certain exception processing */764765goto exit;766}767768if ((op->common.parent->common.aml_opcode == AML_PACKAGE_OP) ||769(op->common.parent->common.aml_opcode == AML_VARIABLE_PACKAGE_OP) ||770(op->common.parent->common.aml_opcode == AML_REF_OF_OP)) {771772/* TBD: Should we specify this feature as a bit of op_info->Flags of these opcodes? */773774goto exit;775}776777status = acpi_ds_create_operand(walk_state, op, 0);778if (ACPI_FAILURE(status)) {779goto exit;780}781782if (op->common.flags & ACPI_PARSEOP_TARGET) {783new_obj_desc = *operand;784goto push_result;785}786787type = (*operand)->common.type;788789status = acpi_ex_resolve_to_value(operand, walk_state);790if (ACPI_FAILURE(status)) {791goto exit;792}793794if (type == ACPI_TYPE_INTEGER) {795796/* It was incremented by acpi_ex_resolve_to_value */797798acpi_ut_remove_reference(*operand);799800status =801acpi_ut_copy_iobject_to_iobject(*operand, &new_obj_desc,802walk_state);803if (ACPI_FAILURE(status)) {804goto exit;805}806} else {807/*808* The object either was anew created or is809* a Namespace node - don't decrement it.810*/811new_obj_desc = *operand;812}813814/* Cleanup for name-path operand */815816status = acpi_ds_obj_stack_pop(1, walk_state);817if (ACPI_FAILURE(status)) {818walk_state->result_obj = new_obj_desc;819goto exit;820}821822push_result:823824walk_state->result_obj = new_obj_desc;825826status = acpi_ds_result_push(walk_state->result_obj, walk_state);827if (ACPI_SUCCESS(status)) {828829/* Force to take it from stack */830831op->common.flags |= ACPI_PARSEOP_IN_STACK;832}833834exit:835836return_ACPI_STATUS(status);837}838839840