1 /******************************************************************************
2  *
3  * Module Name: exoparg3 - AML execution - opcodes with 3 arguments
4  *
5  *****************************************************************************/
6 
7 /*
8  * Copyright (C) 2000 - 2016, 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.h"
45 #include "accommon.h"
46 #include "acinterp.h"
47 #include "acparser.h"
48 #include "amlcode.h"
49 
50 
51 #define _COMPONENT          ACPI_EXECUTER
52         ACPI_MODULE_NAME    ("exoparg3")
53 
54 
55 /*!
56  * Naming convention for AML interpreter execution routines.
57  *
58  * The routines that begin execution of AML opcodes are named with a common
59  * convention based upon the number of arguments, the number of target operands,
60  * and whether or not a value is returned:
61  *
62  *      AcpiExOpcode_xA_yT_zR
63  *
64  * Where:
65  *
66  * xA - ARGUMENTS:    The number of arguments (input operands) that are
67  *                    required for this opcode type (1 through 6 args).
68  * yT - TARGETS:      The number of targets (output operands) that are required
69  *                    for this opcode type (0, 1, or 2 targets).
70  * zR - RETURN VALUE: Indicates whether this opcode type returns a value
71  *                    as the function return (0 or 1).
72  *
73  * The AcpiExOpcode* functions are called via the Dispatcher component with
74  * fully resolved operands.
75 !*/
76 
77 
78 /*******************************************************************************
79  *
80  * FUNCTION:    AcpiExOpcode_3A_0T_0R
81  *
82  * PARAMETERS:  WalkState           - Current walk state
83  *
84  * RETURN:      Status
85  *
86  * DESCRIPTION: Execute Triadic operator (3 operands)
87  *
88  ******************************************************************************/
89 
90 ACPI_STATUS
91 AcpiExOpcode_3A_0T_0R (
92     ACPI_WALK_STATE         *WalkState)
93 {
94     ACPI_OPERAND_OBJECT     **Operand = &WalkState->Operands[0];
95     ACPI_SIGNAL_FATAL_INFO  *Fatal;
96     ACPI_STATUS             Status = AE_OK;
97 
98 
99     ACPI_FUNCTION_TRACE_STR (ExOpcode_3A_0T_0R,
100         AcpiPsGetOpcodeName (WalkState->Opcode));
101 
102 
103     switch (WalkState->Opcode)
104     {
105     case AML_FATAL_OP:          /* Fatal (FatalType  FatalCode  FatalArg) */
106 
107         ACPI_DEBUG_PRINT ((ACPI_DB_INFO,
108             "FatalOp: Type %X Code %X Arg %X "
109             "<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n",
110             (UINT32) Operand[0]->Integer.Value,
111             (UINT32) Operand[1]->Integer.Value,
112             (UINT32) Operand[2]->Integer.Value));
113 
114         Fatal = ACPI_ALLOCATE (sizeof (ACPI_SIGNAL_FATAL_INFO));
115         if (Fatal)
116         {
117             Fatal->Type = (UINT32) Operand[0]->Integer.Value;
118             Fatal->Code = (UINT32) Operand[1]->Integer.Value;
119             Fatal->Argument = (UINT32) Operand[2]->Integer.Value;
120         }
121 
122         /* Always signal the OS! */
123 
124         Status = AcpiOsSignal (ACPI_SIGNAL_FATAL, Fatal);
125 
126         /* Might return while OS is shutting down, just continue */
127 
128         ACPI_FREE (Fatal);
129         goto Cleanup;
130 
131     case AML_EXTERNAL_OP:
132         /*
133          * If the interpreter sees this opcode, just ignore it. The External
134          * op is intended for use by disassemblers in order to properly
135          * disassemble control method invocations. The opcode or group of
136          * opcodes should be surrounded by an "if (0)" clause to ensure that
137          * AML interpreters never see the opcode.
138          */
139         Status = AE_OK;
140         goto Cleanup;
141 
142     default:
143 
144         ACPI_ERROR ((AE_INFO, "Unknown AML opcode 0x%X",
145             WalkState->Opcode));
146 
147         Status = AE_AML_BAD_OPCODE;
148         goto Cleanup;
149     }
150 
151 
152 Cleanup:
153 
154     return_ACPI_STATUS (Status);
155 }
156 
157 
158 /*******************************************************************************
159  *
160  * FUNCTION:    AcpiExOpcode_3A_1T_1R
161  *
162  * PARAMETERS:  WalkState           - Current walk state
163  *
164  * RETURN:      Status
165  *
166  * DESCRIPTION: Execute Triadic operator (3 operands)
167  *
168  ******************************************************************************/
169 
170 ACPI_STATUS
171 AcpiExOpcode_3A_1T_1R (
172     ACPI_WALK_STATE         *WalkState)
173 {
174     ACPI_OPERAND_OBJECT     **Operand = &WalkState->Operands[0];
175     ACPI_OPERAND_OBJECT     *ReturnDesc = NULL;
176     char                    *Buffer = NULL;
177     ACPI_STATUS             Status = AE_OK;
178     UINT64                  Index;
179     ACPI_SIZE               Length;
180 
181 
182     ACPI_FUNCTION_TRACE_STR (ExOpcode_3A_1T_1R,
183         AcpiPsGetOpcodeName (WalkState->Opcode));
184 
185 
186     switch (WalkState->Opcode)
187     {
188     case AML_MID_OP:    /* Mid (Source[0], Index[1], Length[2], Result[3]) */
189         /*
190          * Create the return object. The Source operand is guaranteed to be
191          * either a String or a Buffer, so just use its type.
192          */
193         ReturnDesc = AcpiUtCreateInternalObject (
194             (Operand[0])->Common.Type);
195         if (!ReturnDesc)
196         {
197             Status = AE_NO_MEMORY;
198             goto Cleanup;
199         }
200 
201         /* Get the Integer values from the objects */
202 
203         Index = Operand[1]->Integer.Value;
204         Length = (ACPI_SIZE) Operand[2]->Integer.Value;
205 
206         /*
207          * If the index is beyond the length of the String/Buffer, or if the
208          * requested length is zero, return a zero-length String/Buffer
209          */
210         if (Index >= Operand[0]->String.Length)
211         {
212             Length = 0;
213         }
214 
215         /* Truncate request if larger than the actual String/Buffer */
216 
217         else if ((Index + Length) > Operand[0]->String.Length)
218         {
219             Length =
220                 (ACPI_SIZE) Operand[0]->String.Length - (ACPI_SIZE) Index;
221         }
222 
223         /* Strings always have a sub-pointer, not so for buffers */
224 
225         switch ((Operand[0])->Common.Type)
226         {
227         case ACPI_TYPE_STRING:
228 
229             /* Always allocate a new buffer for the String */
230 
231             Buffer = ACPI_ALLOCATE_ZEROED ((ACPI_SIZE) Length + 1);
232             if (!Buffer)
233             {
234                 Status = AE_NO_MEMORY;
235                 goto Cleanup;
236             }
237             break;
238 
239         case ACPI_TYPE_BUFFER:
240 
241             /* If the requested length is zero, don't allocate a buffer */
242 
243             if (Length > 0)
244             {
245                 /* Allocate a new buffer for the Buffer */
246 
247                 Buffer = ACPI_ALLOCATE_ZEROED (Length);
248                 if (!Buffer)
249                 {
250                     Status = AE_NO_MEMORY;
251                     goto Cleanup;
252                 }
253             }
254             break;
255 
256         default:                        /* Should not happen */
257 
258             Status = AE_AML_OPERAND_TYPE;
259             goto Cleanup;
260         }
261 
262         if (Buffer)
263         {
264             /* We have a buffer, copy the portion requested */
265 
266             memcpy (Buffer,
267                 Operand[0]->String.Pointer + Index, Length);
268         }
269 
270         /* Set the length of the new String/Buffer */
271 
272         ReturnDesc->String.Pointer = Buffer;
273         ReturnDesc->String.Length = (UINT32) Length;
274 
275         /* Mark buffer initialized */
276 
277         ReturnDesc->Buffer.Flags |= AOPOBJ_DATA_VALID;
278         break;
279 
280     default:
281 
282         ACPI_ERROR ((AE_INFO, "Unknown AML opcode 0x%X",
283             WalkState->Opcode));
284 
285         Status = AE_AML_BAD_OPCODE;
286         goto Cleanup;
287     }
288 
289     /* Store the result in the target */
290 
291     Status = AcpiExStore (ReturnDesc, Operand[3], WalkState);
292 
293 Cleanup:
294 
295     /* Delete return object on error */
296 
297     if (ACPI_FAILURE (Status) || WalkState->ResultObj)
298     {
299         AcpiUtRemoveReference (ReturnDesc);
300         WalkState->ResultObj = NULL;
301     }
302     else
303     {
304         /* Set the return object and exit */
305 
306         WalkState->ResultObj = ReturnDesc;
307     }
308 
309     return_ACPI_STATUS (Status);
310 }
311