1/******************************************************************************
2 *
3 * Module Name: exoparg3 - AML execution - opcodes with 3 arguments
4 *
5 *****************************************************************************/
6
7/*
8 * Copyright (C) 2000 - 2015, Intel Corp.
9 * All rights reserved.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 *    notice, this list of conditions, and the following disclaimer,
16 *    without modification.
17 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
18 *    substantially similar to the "NO WARRANTY" disclaimer below
19 *    ("Disclaimer") and any redistribution must be conditioned upon
20 *    including a substantially similar Disclaimer requirement for further
21 *    binary redistribution.
22 * 3. Neither the names of the above-listed copyright holders nor the names
23 *    of any contributors may be used to endorse or promote products derived
24 *    from this software without specific prior written permission.
25 *
26 * Alternatively, this software may be distributed under the terms of the
27 * GNU General Public License ("GPL") version 2 as published by the Free
28 * Software Foundation.
29 *
30 * NO WARRANTY
31 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
34 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
40 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41 * POSSIBILITY OF SUCH DAMAGES.
42 */
43
44#include <acpi/acpi.h>
45#include "accommon.h"
46#include "acinterp.h"
47#include "acparser.h"
48#include "amlcode.h"
49
50#define _COMPONENT          ACPI_EXECUTER
51ACPI_MODULE_NAME("exoparg3")
52
53/*!
54 * Naming convention for AML interpreter execution routines.
55 *
56 * The routines that begin execution of AML opcodes are named with a common
57 * convention based upon the number of arguments, the number of target operands,
58 * and whether or not a value is returned:
59 *
60 *      AcpiExOpcode_xA_yT_zR
61 *
62 * Where:
63 *
64 * xA - ARGUMENTS:    The number of arguments (input operands) that are
65 *                    required for this opcode type (1 through 6 args).
66 * yT - TARGETS:      The number of targets (output operands) that are required
67 *                    for this opcode type (0, 1, or 2 targets).
68 * zR - RETURN VALUE: Indicates whether this opcode type returns a value
69 *                    as the function return (0 or 1).
70 *
71 * The AcpiExOpcode* functions are called via the Dispatcher component with
72 * fully resolved operands.
73!*/
74/*******************************************************************************
75 *
76 * FUNCTION:    acpi_ex_opcode_3A_0T_0R
77 *
78 * PARAMETERS:  walk_state          - Current walk state
79 *
80 * RETURN:      Status
81 *
82 * DESCRIPTION: Execute Triadic operator (3 operands)
83 *
84 ******************************************************************************/
85acpi_status acpi_ex_opcode_3A_0T_0R(struct acpi_walk_state *walk_state)
86{
87	union acpi_operand_object **operand = &walk_state->operands[0];
88	struct acpi_signal_fatal_info *fatal;
89	acpi_status status = AE_OK;
90
91	ACPI_FUNCTION_TRACE_STR(ex_opcode_3A_0T_0R,
92				acpi_ps_get_opcode_name(walk_state->opcode));
93
94	switch (walk_state->opcode) {
95	case AML_FATAL_OP:	/* Fatal (fatal_type fatal_code fatal_arg) */
96
97		ACPI_DEBUG_PRINT((ACPI_DB_INFO,
98				  "FatalOp: Type %X Code %X Arg %X <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n",
99				  (u32) operand[0]->integer.value,
100				  (u32) operand[1]->integer.value,
101				  (u32) operand[2]->integer.value));
102
103		fatal = ACPI_ALLOCATE(sizeof(struct acpi_signal_fatal_info));
104		if (fatal) {
105			fatal->type = (u32) operand[0]->integer.value;
106			fatal->code = (u32) operand[1]->integer.value;
107			fatal->argument = (u32) operand[2]->integer.value;
108		}
109
110		/* Always signal the OS! */
111
112		status = acpi_os_signal(ACPI_SIGNAL_FATAL, fatal);
113
114		/* Might return while OS is shutting down, just continue */
115
116		ACPI_FREE(fatal);
117		goto cleanup;
118
119	case AML_EXTERNAL_OP:
120		/*
121		 * If the interpreter sees this opcode, just ignore it. The External
122		 * op is intended for use by disassemblers in order to properly
123		 * disassemble control method invocations. The opcode or group of
124		 * opcodes should be surrounded by an "if (0)" clause to ensure that
125		 * AML interpreters never see the opcode.
126		 */
127		status = AE_OK;
128		goto cleanup;
129
130	default:
131
132		ACPI_ERROR((AE_INFO, "Unknown AML opcode 0x%X",
133			    walk_state->opcode));
134		status = AE_AML_BAD_OPCODE;
135		goto cleanup;
136	}
137
138cleanup:
139
140	return_ACPI_STATUS(status);
141}
142
143/*******************************************************************************
144 *
145 * FUNCTION:    acpi_ex_opcode_3A_1T_1R
146 *
147 * PARAMETERS:  walk_state          - Current walk state
148 *
149 * RETURN:      Status
150 *
151 * DESCRIPTION: Execute Triadic operator (3 operands)
152 *
153 ******************************************************************************/
154
155acpi_status acpi_ex_opcode_3A_1T_1R(struct acpi_walk_state *walk_state)
156{
157	union acpi_operand_object **operand = &walk_state->operands[0];
158	union acpi_operand_object *return_desc = NULL;
159	char *buffer = NULL;
160	acpi_status status = AE_OK;
161	u64 index;
162	acpi_size length;
163
164	ACPI_FUNCTION_TRACE_STR(ex_opcode_3A_1T_1R,
165				acpi_ps_get_opcode_name(walk_state->opcode));
166
167	switch (walk_state->opcode) {
168	case AML_MID_OP:	/* Mid (Source[0], Index[1], Length[2], Result[3]) */
169		/*
170		 * Create the return object. The Source operand is guaranteed to be
171		 * either a String or a Buffer, so just use its type.
172		 */
173		return_desc = acpi_ut_create_internal_object((operand[0])->
174							     common.type);
175		if (!return_desc) {
176			status = AE_NO_MEMORY;
177			goto cleanup;
178		}
179
180		/* Get the Integer values from the objects */
181
182		index = operand[1]->integer.value;
183		length = (acpi_size) operand[2]->integer.value;
184
185		/*
186		 * If the index is beyond the length of the String/Buffer, or if the
187		 * requested length is zero, return a zero-length String/Buffer
188		 */
189		if (index >= operand[0]->string.length) {
190			length = 0;
191		}
192
193		/* Truncate request if larger than the actual String/Buffer */
194
195		else if ((index + length) > operand[0]->string.length) {
196			length = (acpi_size) operand[0]->string.length -
197			    (acpi_size) index;
198		}
199
200		/* Strings always have a sub-pointer, not so for buffers */
201
202		switch ((operand[0])->common.type) {
203		case ACPI_TYPE_STRING:
204
205			/* Always allocate a new buffer for the String */
206
207			buffer = ACPI_ALLOCATE_ZEROED((acpi_size) length + 1);
208			if (!buffer) {
209				status = AE_NO_MEMORY;
210				goto cleanup;
211			}
212			break;
213
214		case ACPI_TYPE_BUFFER:
215
216			/* If the requested length is zero, don't allocate a buffer */
217
218			if (length > 0) {
219
220				/* Allocate a new buffer for the Buffer */
221
222				buffer = ACPI_ALLOCATE_ZEROED(length);
223				if (!buffer) {
224					status = AE_NO_MEMORY;
225					goto cleanup;
226				}
227			}
228			break;
229
230		default:	/* Should not happen */
231
232			status = AE_AML_OPERAND_TYPE;
233			goto cleanup;
234		}
235
236		if (buffer) {
237
238			/* We have a buffer, copy the portion requested */
239
240			memcpy(buffer, operand[0]->string.pointer + index,
241			       length);
242		}
243
244		/* Set the length of the new String/Buffer */
245
246		return_desc->string.pointer = buffer;
247		return_desc->string.length = (u32) length;
248
249		/* Mark buffer initialized */
250
251		return_desc->buffer.flags |= AOPOBJ_DATA_VALID;
252		break;
253
254	default:
255
256		ACPI_ERROR((AE_INFO, "Unknown AML opcode 0x%X",
257			    walk_state->opcode));
258		status = AE_AML_BAD_OPCODE;
259		goto cleanup;
260	}
261
262	/* Store the result in the target */
263
264	status = acpi_ex_store(return_desc, operand[3], walk_state);
265
266cleanup:
267
268	/* Delete return object on error */
269
270	if (ACPI_FAILURE(status) || walk_state->result_obj) {
271		acpi_ut_remove_reference(return_desc);
272		walk_state->result_obj = NULL;
273	}
274
275	/* Set the return object and exit */
276
277	else {
278		walk_state->result_obj = return_desc;
279	}
280	return_ACPI_STATUS(status);
281}
282