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