1*4882a593Smuzhiyun // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
2*4882a593Smuzhiyun /******************************************************************************
3*4882a593Smuzhiyun *
4*4882a593Smuzhiyun * Module Name: exoparg3 - AML execution - opcodes with 3 arguments
5*4882a593Smuzhiyun *
6*4882a593Smuzhiyun * Copyright (C) 2000 - 2020, Intel Corp.
7*4882a593Smuzhiyun *
8*4882a593Smuzhiyun *****************************************************************************/
9*4882a593Smuzhiyun
10*4882a593Smuzhiyun #include <acpi/acpi.h>
11*4882a593Smuzhiyun #include "accommon.h"
12*4882a593Smuzhiyun #include "acinterp.h"
13*4882a593Smuzhiyun #include "acparser.h"
14*4882a593Smuzhiyun #include "amlcode.h"
15*4882a593Smuzhiyun
16*4882a593Smuzhiyun #define _COMPONENT ACPI_EXECUTER
17*4882a593Smuzhiyun ACPI_MODULE_NAME("exoparg3")
18*4882a593Smuzhiyun
19*4882a593Smuzhiyun /*!
20*4882a593Smuzhiyun * Naming convention for AML interpreter execution routines.
21*4882a593Smuzhiyun *
22*4882a593Smuzhiyun * The routines that begin execution of AML opcodes are named with a common
23*4882a593Smuzhiyun * convention based upon the number of arguments, the number of target operands,
24*4882a593Smuzhiyun * and whether or not a value is returned:
25*4882a593Smuzhiyun *
26*4882a593Smuzhiyun * AcpiExOpcode_xA_yT_zR
27*4882a593Smuzhiyun *
28*4882a593Smuzhiyun * Where:
29*4882a593Smuzhiyun *
30*4882a593Smuzhiyun * xA - ARGUMENTS: The number of arguments (input operands) that are
31*4882a593Smuzhiyun * required for this opcode type (1 through 6 args).
32*4882a593Smuzhiyun * yT - TARGETS: The number of targets (output operands) that are required
33*4882a593Smuzhiyun * for this opcode type (0, 1, or 2 targets).
34*4882a593Smuzhiyun * zR - RETURN VALUE: Indicates whether this opcode type returns a value
35*4882a593Smuzhiyun * as the function return (0 or 1).
36*4882a593Smuzhiyun *
37*4882a593Smuzhiyun * The AcpiExOpcode* functions are called via the Dispatcher component with
38*4882a593Smuzhiyun * fully resolved operands.
39*4882a593Smuzhiyun !*/
40*4882a593Smuzhiyun /*******************************************************************************
41*4882a593Smuzhiyun *
42*4882a593Smuzhiyun * FUNCTION: acpi_ex_opcode_3A_0T_0R
43*4882a593Smuzhiyun *
44*4882a593Smuzhiyun * PARAMETERS: walk_state - Current walk state
45*4882a593Smuzhiyun *
46*4882a593Smuzhiyun * RETURN: Status
47*4882a593Smuzhiyun *
48*4882a593Smuzhiyun * DESCRIPTION: Execute Triadic operator (3 operands)
49*4882a593Smuzhiyun *
50*4882a593Smuzhiyun ******************************************************************************/
acpi_ex_opcode_3A_0T_0R(struct acpi_walk_state * walk_state)51*4882a593Smuzhiyun acpi_status acpi_ex_opcode_3A_0T_0R(struct acpi_walk_state *walk_state)
52*4882a593Smuzhiyun {
53*4882a593Smuzhiyun union acpi_operand_object **operand = &walk_state->operands[0];
54*4882a593Smuzhiyun struct acpi_signal_fatal_info *fatal;
55*4882a593Smuzhiyun acpi_status status = AE_OK;
56*4882a593Smuzhiyun
57*4882a593Smuzhiyun ACPI_FUNCTION_TRACE_STR(ex_opcode_3A_0T_0R,
58*4882a593Smuzhiyun acpi_ps_get_opcode_name(walk_state->opcode));
59*4882a593Smuzhiyun
60*4882a593Smuzhiyun switch (walk_state->opcode) {
61*4882a593Smuzhiyun case AML_FATAL_OP: /* Fatal (fatal_type fatal_code fatal_arg) */
62*4882a593Smuzhiyun
63*4882a593Smuzhiyun ACPI_DEBUG_PRINT((ACPI_DB_INFO,
64*4882a593Smuzhiyun "FatalOp: Type %X Code %X Arg %X "
65*4882a593Smuzhiyun "<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n",
66*4882a593Smuzhiyun (u32)operand[0]->integer.value,
67*4882a593Smuzhiyun (u32)operand[1]->integer.value,
68*4882a593Smuzhiyun (u32)operand[2]->integer.value));
69*4882a593Smuzhiyun
70*4882a593Smuzhiyun fatal = ACPI_ALLOCATE(sizeof(struct acpi_signal_fatal_info));
71*4882a593Smuzhiyun if (fatal) {
72*4882a593Smuzhiyun fatal->type = (u32) operand[0]->integer.value;
73*4882a593Smuzhiyun fatal->code = (u32) operand[1]->integer.value;
74*4882a593Smuzhiyun fatal->argument = (u32) operand[2]->integer.value;
75*4882a593Smuzhiyun }
76*4882a593Smuzhiyun
77*4882a593Smuzhiyun /* Always signal the OS! */
78*4882a593Smuzhiyun
79*4882a593Smuzhiyun status = acpi_os_signal(ACPI_SIGNAL_FATAL, fatal);
80*4882a593Smuzhiyun
81*4882a593Smuzhiyun /* Might return while OS is shutting down, just continue */
82*4882a593Smuzhiyun
83*4882a593Smuzhiyun ACPI_FREE(fatal);
84*4882a593Smuzhiyun goto cleanup;
85*4882a593Smuzhiyun
86*4882a593Smuzhiyun case AML_EXTERNAL_OP:
87*4882a593Smuzhiyun /*
88*4882a593Smuzhiyun * If the interpreter sees this opcode, just ignore it. The External
89*4882a593Smuzhiyun * op is intended for use by disassemblers in order to properly
90*4882a593Smuzhiyun * disassemble control method invocations. The opcode or group of
91*4882a593Smuzhiyun * opcodes should be surrounded by an "if (0)" clause to ensure that
92*4882a593Smuzhiyun * AML interpreters never see the opcode. Thus, something is
93*4882a593Smuzhiyun * wrong if an external opcode ever gets here.
94*4882a593Smuzhiyun */
95*4882a593Smuzhiyun ACPI_ERROR((AE_INFO, "Executed External Op"));
96*4882a593Smuzhiyun status = AE_OK;
97*4882a593Smuzhiyun goto cleanup;
98*4882a593Smuzhiyun
99*4882a593Smuzhiyun default:
100*4882a593Smuzhiyun
101*4882a593Smuzhiyun ACPI_ERROR((AE_INFO, "Unknown AML opcode 0x%X",
102*4882a593Smuzhiyun walk_state->opcode));
103*4882a593Smuzhiyun
104*4882a593Smuzhiyun status = AE_AML_BAD_OPCODE;
105*4882a593Smuzhiyun goto cleanup;
106*4882a593Smuzhiyun }
107*4882a593Smuzhiyun
108*4882a593Smuzhiyun cleanup:
109*4882a593Smuzhiyun
110*4882a593Smuzhiyun return_ACPI_STATUS(status);
111*4882a593Smuzhiyun }
112*4882a593Smuzhiyun
113*4882a593Smuzhiyun /*******************************************************************************
114*4882a593Smuzhiyun *
115*4882a593Smuzhiyun * FUNCTION: acpi_ex_opcode_3A_1T_1R
116*4882a593Smuzhiyun *
117*4882a593Smuzhiyun * PARAMETERS: walk_state - Current walk state
118*4882a593Smuzhiyun *
119*4882a593Smuzhiyun * RETURN: Status
120*4882a593Smuzhiyun *
121*4882a593Smuzhiyun * DESCRIPTION: Execute Triadic operator (3 operands)
122*4882a593Smuzhiyun *
123*4882a593Smuzhiyun ******************************************************************************/
124*4882a593Smuzhiyun
acpi_ex_opcode_3A_1T_1R(struct acpi_walk_state * walk_state)125*4882a593Smuzhiyun acpi_status acpi_ex_opcode_3A_1T_1R(struct acpi_walk_state *walk_state)
126*4882a593Smuzhiyun {
127*4882a593Smuzhiyun union acpi_operand_object **operand = &walk_state->operands[0];
128*4882a593Smuzhiyun union acpi_operand_object *return_desc = NULL;
129*4882a593Smuzhiyun char *buffer = NULL;
130*4882a593Smuzhiyun acpi_status status = AE_OK;
131*4882a593Smuzhiyun u64 index;
132*4882a593Smuzhiyun acpi_size length;
133*4882a593Smuzhiyun
134*4882a593Smuzhiyun ACPI_FUNCTION_TRACE_STR(ex_opcode_3A_1T_1R,
135*4882a593Smuzhiyun acpi_ps_get_opcode_name(walk_state->opcode));
136*4882a593Smuzhiyun
137*4882a593Smuzhiyun switch (walk_state->opcode) {
138*4882a593Smuzhiyun case AML_MID_OP: /* Mid (Source[0], Index[1], Length[2], Result[3]) */
139*4882a593Smuzhiyun /*
140*4882a593Smuzhiyun * Create the return object. The Source operand is guaranteed to be
141*4882a593Smuzhiyun * either a String or a Buffer, so just use its type.
142*4882a593Smuzhiyun */
143*4882a593Smuzhiyun return_desc = acpi_ut_create_internal_object((operand[0])->
144*4882a593Smuzhiyun common.type);
145*4882a593Smuzhiyun if (!return_desc) {
146*4882a593Smuzhiyun status = AE_NO_MEMORY;
147*4882a593Smuzhiyun goto cleanup;
148*4882a593Smuzhiyun }
149*4882a593Smuzhiyun
150*4882a593Smuzhiyun /* Get the Integer values from the objects */
151*4882a593Smuzhiyun
152*4882a593Smuzhiyun index = operand[1]->integer.value;
153*4882a593Smuzhiyun length = (acpi_size)operand[2]->integer.value;
154*4882a593Smuzhiyun
155*4882a593Smuzhiyun /*
156*4882a593Smuzhiyun * If the index is beyond the length of the String/Buffer, or if the
157*4882a593Smuzhiyun * requested length is zero, return a zero-length String/Buffer
158*4882a593Smuzhiyun */
159*4882a593Smuzhiyun if (index >= operand[0]->string.length) {
160*4882a593Smuzhiyun length = 0;
161*4882a593Smuzhiyun }
162*4882a593Smuzhiyun
163*4882a593Smuzhiyun /* Truncate request if larger than the actual String/Buffer */
164*4882a593Smuzhiyun
165*4882a593Smuzhiyun else if ((index + length) > operand[0]->string.length) {
166*4882a593Smuzhiyun length =
167*4882a593Smuzhiyun (acpi_size)operand[0]->string.length -
168*4882a593Smuzhiyun (acpi_size)index;
169*4882a593Smuzhiyun }
170*4882a593Smuzhiyun
171*4882a593Smuzhiyun /* Strings always have a sub-pointer, not so for buffers */
172*4882a593Smuzhiyun
173*4882a593Smuzhiyun switch ((operand[0])->common.type) {
174*4882a593Smuzhiyun case ACPI_TYPE_STRING:
175*4882a593Smuzhiyun
176*4882a593Smuzhiyun /* Always allocate a new buffer for the String */
177*4882a593Smuzhiyun
178*4882a593Smuzhiyun buffer = ACPI_ALLOCATE_ZEROED((acpi_size)length + 1);
179*4882a593Smuzhiyun if (!buffer) {
180*4882a593Smuzhiyun status = AE_NO_MEMORY;
181*4882a593Smuzhiyun goto cleanup;
182*4882a593Smuzhiyun }
183*4882a593Smuzhiyun break;
184*4882a593Smuzhiyun
185*4882a593Smuzhiyun case ACPI_TYPE_BUFFER:
186*4882a593Smuzhiyun
187*4882a593Smuzhiyun /* If the requested length is zero, don't allocate a buffer */
188*4882a593Smuzhiyun
189*4882a593Smuzhiyun if (length > 0) {
190*4882a593Smuzhiyun
191*4882a593Smuzhiyun /* Allocate a new buffer for the Buffer */
192*4882a593Smuzhiyun
193*4882a593Smuzhiyun buffer = ACPI_ALLOCATE_ZEROED(length);
194*4882a593Smuzhiyun if (!buffer) {
195*4882a593Smuzhiyun status = AE_NO_MEMORY;
196*4882a593Smuzhiyun goto cleanup;
197*4882a593Smuzhiyun }
198*4882a593Smuzhiyun }
199*4882a593Smuzhiyun break;
200*4882a593Smuzhiyun
201*4882a593Smuzhiyun default: /* Should not happen */
202*4882a593Smuzhiyun
203*4882a593Smuzhiyun status = AE_AML_OPERAND_TYPE;
204*4882a593Smuzhiyun goto cleanup;
205*4882a593Smuzhiyun }
206*4882a593Smuzhiyun
207*4882a593Smuzhiyun if (buffer) {
208*4882a593Smuzhiyun
209*4882a593Smuzhiyun /* We have a buffer, copy the portion requested */
210*4882a593Smuzhiyun
211*4882a593Smuzhiyun memcpy(buffer,
212*4882a593Smuzhiyun operand[0]->string.pointer + index, length);
213*4882a593Smuzhiyun }
214*4882a593Smuzhiyun
215*4882a593Smuzhiyun /* Set the length of the new String/Buffer */
216*4882a593Smuzhiyun
217*4882a593Smuzhiyun return_desc->string.pointer = buffer;
218*4882a593Smuzhiyun return_desc->string.length = (u32) length;
219*4882a593Smuzhiyun
220*4882a593Smuzhiyun /* Mark buffer initialized */
221*4882a593Smuzhiyun
222*4882a593Smuzhiyun return_desc->buffer.flags |= AOPOBJ_DATA_VALID;
223*4882a593Smuzhiyun break;
224*4882a593Smuzhiyun
225*4882a593Smuzhiyun default:
226*4882a593Smuzhiyun
227*4882a593Smuzhiyun ACPI_ERROR((AE_INFO, "Unknown AML opcode 0x%X",
228*4882a593Smuzhiyun walk_state->opcode));
229*4882a593Smuzhiyun
230*4882a593Smuzhiyun status = AE_AML_BAD_OPCODE;
231*4882a593Smuzhiyun goto cleanup;
232*4882a593Smuzhiyun }
233*4882a593Smuzhiyun
234*4882a593Smuzhiyun /* Store the result in the target */
235*4882a593Smuzhiyun
236*4882a593Smuzhiyun status = acpi_ex_store(return_desc, operand[3], walk_state);
237*4882a593Smuzhiyun
238*4882a593Smuzhiyun cleanup:
239*4882a593Smuzhiyun
240*4882a593Smuzhiyun /* Delete return object on error */
241*4882a593Smuzhiyun
242*4882a593Smuzhiyun if (ACPI_FAILURE(status) || walk_state->result_obj) {
243*4882a593Smuzhiyun acpi_ut_remove_reference(return_desc);
244*4882a593Smuzhiyun walk_state->result_obj = NULL;
245*4882a593Smuzhiyun } else {
246*4882a593Smuzhiyun /* Set the return object and exit */
247*4882a593Smuzhiyun
248*4882a593Smuzhiyun walk_state->result_obj = return_desc;
249*4882a593Smuzhiyun }
250*4882a593Smuzhiyun
251*4882a593Smuzhiyun return_ACPI_STATUS(status);
252*4882a593Smuzhiyun }
253