xref: /freebsd/sys/contrib/dev/acpica/components/executer/exoparg2.c (revision 8d20be1e22095c27faf8fe8b2f0d089739cc742e)
1 /******************************************************************************
2  *
3  * Module Name: exoparg2 - AML execution - opcodes with 2 arguments
4  *
5  *****************************************************************************/
6 
7 /*
8  * Copyright (C) 2000 - 2013, 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 
45 #define __EXOPARG2_C__
46 
47 #include <contrib/dev/acpica/include/acpi.h>
48 #include <contrib/dev/acpica/include/accommon.h>
49 #include <contrib/dev/acpica/include/acparser.h>
50 #include <contrib/dev/acpica/include/acinterp.h>
51 #include <contrib/dev/acpica/include/acevents.h>
52 #include <contrib/dev/acpica/include/amlcode.h>
53 
54 
55 #define _COMPONENT          ACPI_EXECUTER
56         ACPI_MODULE_NAME    ("exoparg2")
57 
58 
59 /*!
60  * Naming convention for AML interpreter execution routines.
61  *
62  * The routines that begin execution of AML opcodes are named with a common
63  * convention based upon the number of arguments, the number of target operands,
64  * and whether or not a value is returned:
65  *
66  *      AcpiExOpcode_xA_yT_zR
67  *
68  * Where:
69  *
70  * xA - ARGUMENTS:    The number of arguments (input operands) that are
71  *                    required for this opcode type (1 through 6 args).
72  * yT - TARGETS:      The number of targets (output operands) that are required
73  *                    for this opcode type (0, 1, or 2 targets).
74  * zR - RETURN VALUE: Indicates whether this opcode type returns a value
75  *                    as the function return (0 or 1).
76  *
77  * The AcpiExOpcode* functions are called via the Dispatcher component with
78  * fully resolved operands.
79 !*/
80 
81 
82 /*******************************************************************************
83  *
84  * FUNCTION:    AcpiExOpcode_2A_0T_0R
85  *
86  * PARAMETERS:  WalkState           - Current walk state
87  *
88  * RETURN:      Status
89  *
90  * DESCRIPTION: Execute opcode with two arguments, no target, and no return
91  *              value.
92  *
93  * ALLOCATION:  Deletes both operands
94  *
95  ******************************************************************************/
96 
97 ACPI_STATUS
98 AcpiExOpcode_2A_0T_0R (
99     ACPI_WALK_STATE         *WalkState)
100 {
101     ACPI_OPERAND_OBJECT     **Operand = &WalkState->Operands[0];
102     ACPI_NAMESPACE_NODE     *Node;
103     UINT32                  Value;
104     ACPI_STATUS             Status = AE_OK;
105 
106 
107     ACPI_FUNCTION_TRACE_STR (ExOpcode_2A_0T_0R,
108             AcpiPsGetOpcodeName (WalkState->Opcode));
109 
110 
111     /* Examine the opcode */
112 
113     switch (WalkState->Opcode)
114     {
115     case AML_NOTIFY_OP:         /* Notify (NotifyObject, NotifyValue) */
116 
117         /* The first operand is a namespace node */
118 
119         Node = (ACPI_NAMESPACE_NODE *) Operand[0];
120 
121         /* Second value is the notify value */
122 
123         Value = (UINT32) Operand[1]->Integer.Value;
124 
125         /* Are notifies allowed on this object? */
126 
127         if (!AcpiEvIsNotifyObject (Node))
128         {
129             ACPI_ERROR ((AE_INFO,
130                 "Unexpected notify object type [%s]",
131                 AcpiUtGetTypeName (Node->Type)));
132 
133             Status = AE_AML_OPERAND_TYPE;
134             break;
135         }
136 
137         /*
138          * Dispatch the notify to the appropriate handler
139          * NOTE: the request is queued for execution after this method
140          * completes. The notify handlers are NOT invoked synchronously
141          * from this thread -- because handlers may in turn run other
142          * control methods.
143          */
144         Status = AcpiEvQueueNotifyRequest (Node, Value);
145         break;
146 
147     default:
148 
149         ACPI_ERROR ((AE_INFO, "Unknown AML opcode 0x%X",
150             WalkState->Opcode));
151         Status = AE_AML_BAD_OPCODE;
152     }
153 
154     return_ACPI_STATUS (Status);
155 }
156 
157 
158 /*******************************************************************************
159  *
160  * FUNCTION:    AcpiExOpcode_2A_2T_1R
161  *
162  * PARAMETERS:  WalkState           - Current walk state
163  *
164  * RETURN:      Status
165  *
166  * DESCRIPTION: Execute a dyadic operator (2 operands) with 2 output targets
167  *              and one implicit return value.
168  *
169  ******************************************************************************/
170 
171 ACPI_STATUS
172 AcpiExOpcode_2A_2T_1R (
173     ACPI_WALK_STATE         *WalkState)
174 {
175     ACPI_OPERAND_OBJECT     **Operand = &WalkState->Operands[0];
176     ACPI_OPERAND_OBJECT     *ReturnDesc1 = NULL;
177     ACPI_OPERAND_OBJECT     *ReturnDesc2 = NULL;
178     ACPI_STATUS             Status;
179 
180 
181     ACPI_FUNCTION_TRACE_STR (ExOpcode_2A_2T_1R,
182         AcpiPsGetOpcodeName (WalkState->Opcode));
183 
184 
185     /* Execute the opcode */
186 
187     switch (WalkState->Opcode)
188     {
189     case AML_DIVIDE_OP:
190 
191         /* Divide (Dividend, Divisor, RemainderResult QuotientResult) */
192 
193         ReturnDesc1 = AcpiUtCreateInternalObject (ACPI_TYPE_INTEGER);
194         if (!ReturnDesc1)
195         {
196             Status = AE_NO_MEMORY;
197             goto Cleanup;
198         }
199 
200         ReturnDesc2 = AcpiUtCreateInternalObject (ACPI_TYPE_INTEGER);
201         if (!ReturnDesc2)
202         {
203             Status = AE_NO_MEMORY;
204             goto Cleanup;
205         }
206 
207         /* Quotient to ReturnDesc1, remainder to ReturnDesc2 */
208 
209         Status = AcpiUtDivide (Operand[0]->Integer.Value,
210                                Operand[1]->Integer.Value,
211                                &ReturnDesc1->Integer.Value,
212                                &ReturnDesc2->Integer.Value);
213         if (ACPI_FAILURE (Status))
214         {
215             goto Cleanup;
216         }
217         break;
218 
219     default:
220 
221         ACPI_ERROR ((AE_INFO, "Unknown AML opcode 0x%X",
222             WalkState->Opcode));
223         Status = AE_AML_BAD_OPCODE;
224         goto Cleanup;
225     }
226 
227     /* Store the results to the target reference operands */
228 
229     Status = AcpiExStore (ReturnDesc2, Operand[2], WalkState);
230     if (ACPI_FAILURE (Status))
231     {
232         goto Cleanup;
233     }
234 
235     Status = AcpiExStore (ReturnDesc1, Operand[3], WalkState);
236     if (ACPI_FAILURE (Status))
237     {
238         goto Cleanup;
239     }
240 
241 Cleanup:
242     /*
243      * Since the remainder is not returned indirectly, remove a reference to
244      * it. Only the quotient is returned indirectly.
245      */
246     AcpiUtRemoveReference (ReturnDesc2);
247 
248     if (ACPI_FAILURE (Status))
249     {
250         /* Delete the return object */
251 
252         AcpiUtRemoveReference (ReturnDesc1);
253     }
254 
255     /* Save return object (the remainder) on success */
256 
257     else
258     {
259         WalkState->ResultObj = ReturnDesc1;
260     }
261 
262     return_ACPI_STATUS (Status);
263 }
264 
265 
266 /*******************************************************************************
267  *
268  * FUNCTION:    AcpiExOpcode_2A_1T_1R
269  *
270  * PARAMETERS:  WalkState           - Current walk state
271  *
272  * RETURN:      Status
273  *
274  * DESCRIPTION: Execute opcode with two arguments, one target, and a return
275  *              value.
276  *
277  ******************************************************************************/
278 
279 ACPI_STATUS
280 AcpiExOpcode_2A_1T_1R (
281     ACPI_WALK_STATE         *WalkState)
282 {
283     ACPI_OPERAND_OBJECT     **Operand = &WalkState->Operands[0];
284     ACPI_OPERAND_OBJECT     *ReturnDesc = NULL;
285     UINT64                  Index;
286     ACPI_STATUS             Status = AE_OK;
287     ACPI_SIZE               Length = 0;
288 
289 
290     ACPI_FUNCTION_TRACE_STR (ExOpcode_2A_1T_1R,
291         AcpiPsGetOpcodeName (WalkState->Opcode));
292 
293 
294     /* Execute the opcode */
295 
296     if (WalkState->OpInfo->Flags & AML_MATH)
297     {
298         /* All simple math opcodes (add, etc.) */
299 
300         ReturnDesc = AcpiUtCreateInternalObject (ACPI_TYPE_INTEGER);
301         if (!ReturnDesc)
302         {
303             Status = AE_NO_MEMORY;
304             goto Cleanup;
305         }
306 
307         ReturnDesc->Integer.Value = AcpiExDoMathOp (WalkState->Opcode,
308                                                 Operand[0]->Integer.Value,
309                                                 Operand[1]->Integer.Value);
310         goto StoreResultToTarget;
311     }
312 
313     switch (WalkState->Opcode)
314     {
315     case AML_MOD_OP: /* Mod (Dividend, Divisor, RemainderResult (ACPI 2.0) */
316 
317         ReturnDesc = AcpiUtCreateInternalObject (ACPI_TYPE_INTEGER);
318         if (!ReturnDesc)
319         {
320             Status = AE_NO_MEMORY;
321             goto Cleanup;
322         }
323 
324         /* ReturnDesc will contain the remainder */
325 
326         Status = AcpiUtDivide (Operand[0]->Integer.Value,
327                                Operand[1]->Integer.Value,
328                                NULL,
329                                &ReturnDesc->Integer.Value);
330         break;
331 
332     case AML_CONCAT_OP: /* Concatenate (Data1, Data2, Result) */
333 
334         Status = AcpiExDoConcatenate (Operand[0], Operand[1],
335                     &ReturnDesc, WalkState);
336         break;
337 
338     case AML_TO_STRING_OP: /* ToString (Buffer, Length, Result) (ACPI 2.0) */
339         /*
340          * Input object is guaranteed to be a buffer at this point (it may have
341          * been converted.)  Copy the raw buffer data to a new object of
342          * type String.
343          */
344 
345         /*
346          * Get the length of the new string. It is the smallest of:
347          * 1) Length of the input buffer
348          * 2) Max length as specified in the ToString operator
349          * 3) Length of input buffer up to a zero byte (null terminator)
350          *
351          * NOTE: A length of zero is ok, and will create a zero-length, null
352          *       terminated string.
353          */
354         while ((Length < Operand[0]->Buffer.Length) &&
355                (Length < Operand[1]->Integer.Value) &&
356                (Operand[0]->Buffer.Pointer[Length]))
357         {
358             Length++;
359         }
360 
361         /* Allocate a new string object */
362 
363         ReturnDesc = AcpiUtCreateStringObject (Length);
364         if (!ReturnDesc)
365         {
366             Status = AE_NO_MEMORY;
367             goto Cleanup;
368         }
369 
370         /*
371          * Copy the raw buffer data with no transform.
372          * (NULL terminated already)
373          */
374         ACPI_MEMCPY (ReturnDesc->String.Pointer,
375             Operand[0]->Buffer.Pointer, Length);
376         break;
377 
378     case AML_CONCAT_RES_OP:
379 
380         /* ConcatenateResTemplate (Buffer, Buffer, Result) (ACPI 2.0) */
381 
382         Status = AcpiExConcatTemplate (Operand[0], Operand[1],
383                     &ReturnDesc, WalkState);
384         break;
385 
386     case AML_INDEX_OP:              /* Index (Source Index Result) */
387 
388         /* Create the internal return object */
389 
390         ReturnDesc = AcpiUtCreateInternalObject (ACPI_TYPE_LOCAL_REFERENCE);
391         if (!ReturnDesc)
392         {
393             Status = AE_NO_MEMORY;
394             goto Cleanup;
395         }
396 
397         /* Initialize the Index reference object */
398 
399         Index = Operand[1]->Integer.Value;
400         ReturnDesc->Reference.Value = (UINT32) Index;
401         ReturnDesc->Reference.Class = ACPI_REFCLASS_INDEX;
402 
403         /*
404          * At this point, the Source operand is a String, Buffer, or Package.
405          * Verify that the index is within range.
406          */
407         switch ((Operand[0])->Common.Type)
408         {
409         case ACPI_TYPE_STRING:
410 
411             if (Index >= Operand[0]->String.Length)
412             {
413                 Length = Operand[0]->String.Length;
414                 Status = AE_AML_STRING_LIMIT;
415             }
416 
417             ReturnDesc->Reference.TargetType = ACPI_TYPE_BUFFER_FIELD;
418             break;
419 
420         case ACPI_TYPE_BUFFER:
421 
422             if (Index >= Operand[0]->Buffer.Length)
423             {
424                 Length = Operand[0]->Buffer.Length;
425                 Status = AE_AML_BUFFER_LIMIT;
426             }
427 
428             ReturnDesc->Reference.TargetType = ACPI_TYPE_BUFFER_FIELD;
429             break;
430 
431         case ACPI_TYPE_PACKAGE:
432 
433             if (Index >= Operand[0]->Package.Count)
434             {
435                 Length = Operand[0]->Package.Count;
436                 Status = AE_AML_PACKAGE_LIMIT;
437             }
438 
439             ReturnDesc->Reference.TargetType = ACPI_TYPE_PACKAGE;
440             ReturnDesc->Reference.Where = &Operand[0]->Package.Elements [Index];
441             break;
442 
443         default:
444 
445             Status = AE_AML_INTERNAL;
446             goto Cleanup;
447         }
448 
449         /* Failure means that the Index was beyond the end of the object */
450 
451         if (ACPI_FAILURE (Status))
452         {
453             ACPI_EXCEPTION ((AE_INFO, Status,
454                 "Index (0x%X%8.8X) is beyond end of object (length 0x%X)",
455                 ACPI_FORMAT_UINT64 (Index), (UINT32) Length));
456             goto Cleanup;
457         }
458 
459         /*
460          * Save the target object and add a reference to it for the life
461          * of the index
462          */
463         ReturnDesc->Reference.Object = Operand[0];
464         AcpiUtAddReference (Operand[0]);
465 
466         /* Store the reference to the Target */
467 
468         Status = AcpiExStore (ReturnDesc, Operand[2], WalkState);
469 
470         /* Return the reference */
471 
472         WalkState->ResultObj = ReturnDesc;
473         goto Cleanup;
474 
475     default:
476 
477         ACPI_ERROR ((AE_INFO, "Unknown AML opcode 0x%X",
478             WalkState->Opcode));
479         Status = AE_AML_BAD_OPCODE;
480         break;
481     }
482 
483 
484 StoreResultToTarget:
485 
486     if (ACPI_SUCCESS (Status))
487     {
488         /*
489          * Store the result of the operation (which is now in ReturnDesc) into
490          * the Target descriptor.
491          */
492         Status = AcpiExStore (ReturnDesc, Operand[2], WalkState);
493         if (ACPI_FAILURE (Status))
494         {
495             goto Cleanup;
496         }
497 
498         if (!WalkState->ResultObj)
499         {
500             WalkState->ResultObj = ReturnDesc;
501         }
502     }
503 
504 
505 Cleanup:
506 
507     /* Delete return object on error */
508 
509     if (ACPI_FAILURE (Status))
510     {
511         AcpiUtRemoveReference (ReturnDesc);
512         WalkState->ResultObj = NULL;
513     }
514 
515     return_ACPI_STATUS (Status);
516 }
517 
518 
519 /*******************************************************************************
520  *
521  * FUNCTION:    AcpiExOpcode_2A_0T_1R
522  *
523  * PARAMETERS:  WalkState           - Current walk state
524  *
525  * RETURN:      Status
526  *
527  * DESCRIPTION: Execute opcode with 2 arguments, no target, and a return value
528  *
529  ******************************************************************************/
530 
531 ACPI_STATUS
532 AcpiExOpcode_2A_0T_1R (
533     ACPI_WALK_STATE         *WalkState)
534 {
535     ACPI_OPERAND_OBJECT     **Operand = &WalkState->Operands[0];
536     ACPI_OPERAND_OBJECT     *ReturnDesc = NULL;
537     ACPI_STATUS             Status = AE_OK;
538     BOOLEAN                 LogicalResult = FALSE;
539 
540 
541     ACPI_FUNCTION_TRACE_STR (ExOpcode_2A_0T_1R,
542         AcpiPsGetOpcodeName (WalkState->Opcode));
543 
544 
545     /* Create the internal return object */
546 
547     ReturnDesc = AcpiUtCreateInternalObject (ACPI_TYPE_INTEGER);
548     if (!ReturnDesc)
549     {
550         Status = AE_NO_MEMORY;
551         goto Cleanup;
552     }
553 
554     /* Execute the Opcode */
555 
556     if (WalkState->OpInfo->Flags & AML_LOGICAL_NUMERIC)
557     {
558         /* LogicalOp  (Operand0, Operand1) */
559 
560         Status = AcpiExDoLogicalNumericOp (WalkState->Opcode,
561                         Operand[0]->Integer.Value, Operand[1]->Integer.Value,
562                         &LogicalResult);
563         goto StoreLogicalResult;
564     }
565     else if (WalkState->OpInfo->Flags & AML_LOGICAL)
566     {
567         /* LogicalOp  (Operand0, Operand1) */
568 
569         Status = AcpiExDoLogicalOp (WalkState->Opcode, Operand[0],
570                     Operand[1], &LogicalResult);
571         goto StoreLogicalResult;
572     }
573 
574     switch (WalkState->Opcode)
575     {
576     case AML_ACQUIRE_OP:            /* Acquire (MutexObject, Timeout) */
577 
578         Status = AcpiExAcquireMutex (Operand[1], Operand[0], WalkState);
579         if (Status == AE_TIME)
580         {
581             LogicalResult = TRUE;       /* TRUE = Acquire timed out */
582             Status = AE_OK;
583         }
584         break;
585 
586 
587     case AML_WAIT_OP:               /* Wait (EventObject, Timeout) */
588 
589         Status = AcpiExSystemWaitEvent (Operand[1], Operand[0]);
590         if (Status == AE_TIME)
591         {
592             LogicalResult = TRUE;       /* TRUE, Wait timed out */
593             Status = AE_OK;
594         }
595         break;
596 
597     default:
598 
599         ACPI_ERROR ((AE_INFO, "Unknown AML opcode 0x%X",
600             WalkState->Opcode));
601         Status = AE_AML_BAD_OPCODE;
602         goto Cleanup;
603     }
604 
605 
606 StoreLogicalResult:
607     /*
608      * Set return value to according to LogicalResult. logical TRUE (all ones)
609      * Default is FALSE (zero)
610      */
611     if (LogicalResult)
612     {
613         ReturnDesc->Integer.Value = ACPI_UINT64_MAX;
614     }
615 
616 Cleanup:
617 
618     /* Delete return object on error */
619 
620     if (ACPI_FAILURE (Status))
621     {
622         AcpiUtRemoveReference (ReturnDesc);
623     }
624 
625     /* Save return object on success */
626 
627     else
628     {
629         WalkState->ResultObj = ReturnDesc;
630     }
631 
632     return_ACPI_STATUS (Status);
633 }
634