Merge branch 'vendor/GCC50'
[dragonfly.git] / sys / contrib / dev / acpica / source / components / executer / exoparg3.c
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.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 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n",
109             (UINT32) Operand[0]->Integer.Value,
110             (UINT32) Operand[1]->Integer.Value,
111             (UINT32) Operand[2]->Integer.Value));
112
113         Fatal = ACPI_ALLOCATE (sizeof (ACPI_SIGNAL_FATAL_INFO));
114         if (Fatal)
115         {
116             Fatal->Type     = (UINT32) Operand[0]->Integer.Value;
117             Fatal->Code     = (UINT32) Operand[1]->Integer.Value;
118             Fatal->Argument = (UINT32) Operand[2]->Integer.Value;
119         }
120
121         /* Always signal the OS! */
122
123         Status = AcpiOsSignal (ACPI_SIGNAL_FATAL, Fatal);
124
125         /* Might return while OS is shutting down, just continue */
126
127         ACPI_FREE (Fatal);
128         break;
129
130     default:
131
132         ACPI_ERROR ((AE_INFO, "Unknown AML opcode 0x%X",
133             WalkState->Opcode));
134         Status = AE_AML_BAD_OPCODE;
135         goto Cleanup;
136     }
137
138
139 Cleanup:
140
141     return_ACPI_STATUS (Status);
142 }
143
144
145 /*******************************************************************************
146  *
147  * FUNCTION:    AcpiExOpcode_3A_1T_1R
148  *
149  * PARAMETERS:  WalkState           - Current walk state
150  *
151  * RETURN:      Status
152  *
153  * DESCRIPTION: Execute Triadic operator (3 operands)
154  *
155  ******************************************************************************/
156
157 ACPI_STATUS
158 AcpiExOpcode_3A_1T_1R (
159     ACPI_WALK_STATE         *WalkState)
160 {
161     ACPI_OPERAND_OBJECT     **Operand = &WalkState->Operands[0];
162     ACPI_OPERAND_OBJECT     *ReturnDesc = NULL;
163     char                    *Buffer = NULL;
164     ACPI_STATUS             Status = AE_OK;
165     UINT64                  Index;
166     ACPI_SIZE               Length;
167
168
169     ACPI_FUNCTION_TRACE_STR (ExOpcode_3A_1T_1R,
170         AcpiPsGetOpcodeName (WalkState->Opcode));
171
172
173     switch (WalkState->Opcode)
174     {
175     case AML_MID_OP:    /* Mid (Source[0], Index[1], Length[2], Result[3]) */
176         /*
177          * Create the return object. The Source operand is guaranteed to be
178          * either a String or a Buffer, so just use its type.
179          */
180         ReturnDesc = AcpiUtCreateInternalObject (
181                         (Operand[0])->Common.Type);
182         if (!ReturnDesc)
183         {
184             Status = AE_NO_MEMORY;
185             goto Cleanup;
186         }
187
188         /* Get the Integer values from the objects */
189
190         Index = Operand[1]->Integer.Value;
191         Length = (ACPI_SIZE) Operand[2]->Integer.Value;
192
193         /*
194          * If the index is beyond the length of the String/Buffer, or if the
195          * requested length is zero, return a zero-length String/Buffer
196          */
197         if (Index >= Operand[0]->String.Length)
198         {
199             Length = 0;
200         }
201
202         /* Truncate request if larger than the actual String/Buffer */
203
204         else if ((Index + Length) > Operand[0]->String.Length)
205         {
206             Length = (ACPI_SIZE) Operand[0]->String.Length -
207                         (ACPI_SIZE) Index;
208         }
209
210         /* Strings always have a sub-pointer, not so for buffers */
211
212         switch ((Operand[0])->Common.Type)
213         {
214         case ACPI_TYPE_STRING:
215
216             /* Always allocate a new buffer for the String */
217
218             Buffer = ACPI_ALLOCATE_ZEROED ((ACPI_SIZE) Length + 1);
219             if (!Buffer)
220             {
221                 Status = AE_NO_MEMORY;
222                 goto Cleanup;
223             }
224             break;
225
226         case ACPI_TYPE_BUFFER:
227
228             /* If the requested length is zero, don't allocate a buffer */
229
230             if (Length > 0)
231             {
232                 /* Allocate a new buffer for the Buffer */
233
234                 Buffer = ACPI_ALLOCATE_ZEROED (Length);
235                 if (!Buffer)
236                 {
237                     Status = AE_NO_MEMORY;
238                     goto Cleanup;
239                 }
240             }
241             break;
242
243         default:                        /* Should not happen */
244
245             Status = AE_AML_OPERAND_TYPE;
246             goto Cleanup;
247         }
248
249         if (Buffer)
250         {
251             /* We have a buffer, copy the portion requested */
252
253             ACPI_MEMCPY (Buffer, Operand[0]->String.Pointer + Index,
254                          Length);
255         }
256
257         /* Set the length of the new String/Buffer */
258
259         ReturnDesc->String.Pointer = Buffer;
260         ReturnDesc->String.Length = (UINT32) Length;
261
262         /* Mark buffer initialized */
263
264         ReturnDesc->Buffer.Flags |= AOPOBJ_DATA_VALID;
265         break;
266
267     default:
268
269         ACPI_ERROR ((AE_INFO, "Unknown AML opcode 0x%X",
270             WalkState->Opcode));
271         Status = AE_AML_BAD_OPCODE;
272         goto Cleanup;
273     }
274
275     /* Store the result in the target */
276
277     Status = AcpiExStore (ReturnDesc, Operand[3], WalkState);
278
279 Cleanup:
280
281     /* Delete return object on error */
282
283     if (ACPI_FAILURE (Status) || WalkState->ResultObj)
284     {
285         AcpiUtRemoveReference (ReturnDesc);
286         WalkState->ResultObj = NULL;
287     }
288
289     /* Set the return object and exit */
290
291     else
292     {
293         WalkState->ResultObj = ReturnDesc;
294     }
295     return_ACPI_STATUS (Status);
296 }