xref: /haiku/src/add-ons/kernel/bus_managers/acpi/acpica/components/executer/exmisc.c (revision 6c2abee2f5e73c3fc81c33da51ac610f8bf1117a)
1 /******************************************************************************
2  *
3  * Module Name: exmisc - ACPI AML (p-code) execution - specific opcodes
4  *
5  *****************************************************************************/
6 
7 /******************************************************************************
8  *
9  * 1. Copyright Notice
10  *
11  * Some or all of this work - Copyright (c) 1999 - 2016, Intel Corp.
12  * All rights reserved.
13  *
14  * 2. License
15  *
16  * 2.1. This is your license from Intel Corp. under its intellectual property
17  * rights. You may have additional license terms from the party that provided
18  * you this software, covering your right to use that party's intellectual
19  * property rights.
20  *
21  * 2.2. Intel grants, free of charge, to any person ("Licensee") obtaining a
22  * copy of the source code appearing in this file ("Covered Code") an
23  * irrevocable, perpetual, worldwide license under Intel's copyrights in the
24  * base code distributed originally by Intel ("Original Intel Code") to copy,
25  * make derivatives, distribute, use and display any portion of the Covered
26  * Code in any form, with the right to sublicense such rights; and
27  *
28  * 2.3. Intel grants Licensee a non-exclusive and non-transferable patent
29  * license (with the right to sublicense), under only those claims of Intel
30  * patents that are infringed by the Original Intel Code, to make, use, sell,
31  * offer to sell, and import the Covered Code and derivative works thereof
32  * solely to the minimum extent necessary to exercise the above copyright
33  * license, and in no event shall the patent license extend to any additions
34  * to or modifications of the Original Intel Code. No other license or right
35  * is granted directly or by implication, estoppel or otherwise;
36  *
37  * The above copyright and patent license is granted only if the following
38  * conditions are met:
39  *
40  * 3. Conditions
41  *
42  * 3.1. Redistribution of Source with Rights to Further Distribute Source.
43  * Redistribution of source code of any substantial portion of the Covered
44  * Code or modification with rights to further distribute source must include
45  * the above Copyright Notice, the above License, this list of Conditions,
46  * and the following Disclaimer and Export Compliance provision. In addition,
47  * Licensee must cause all Covered Code to which Licensee contributes to
48  * contain a file documenting the changes Licensee made to create that Covered
49  * Code and the date of any change. Licensee must include in that file the
50  * documentation of any changes made by any predecessor Licensee. Licensee
51  * must include a prominent statement that the modification is derived,
52  * directly or indirectly, from Original Intel Code.
53  *
54  * 3.2. Redistribution of Source with no Rights to Further Distribute Source.
55  * Redistribution of source code of any substantial portion of the Covered
56  * Code or modification without rights to further distribute source must
57  * include the following Disclaimer and Export Compliance provision in the
58  * documentation and/or other materials provided with distribution. In
59  * addition, Licensee may not authorize further sublicense of source of any
60  * portion of the Covered Code, and must include terms to the effect that the
61  * license from Licensee to its licensee is limited to the intellectual
62  * property embodied in the software Licensee provides to its licensee, and
63  * not to intellectual property embodied in modifications its licensee may
64  * make.
65  *
66  * 3.3. Redistribution of Executable. Redistribution in executable form of any
67  * substantial portion of the Covered Code or modification must reproduce the
68  * above Copyright Notice, and the following Disclaimer and Export Compliance
69  * provision in the documentation and/or other materials provided with the
70  * distribution.
71  *
72  * 3.4. Intel retains all right, title, and interest in and to the Original
73  * Intel Code.
74  *
75  * 3.5. Neither the name Intel nor any other trademark owned or controlled by
76  * Intel shall be used in advertising or otherwise to promote the sale, use or
77  * other dealings in products derived from or relating to the Covered Code
78  * without prior written authorization from Intel.
79  *
80  * 4. Disclaimer and Export Compliance
81  *
82  * 4.1. INTEL MAKES NO WARRANTY OF ANY KIND REGARDING ANY SOFTWARE PROVIDED
83  * HERE. ANY SOFTWARE ORIGINATING FROM INTEL OR DERIVED FROM INTEL SOFTWARE
84  * IS PROVIDED "AS IS," AND INTEL WILL NOT PROVIDE ANY SUPPORT, ASSISTANCE,
85  * INSTALLATION, TRAINING OR OTHER SERVICES. INTEL WILL NOT PROVIDE ANY
86 
87  * UPDATES, ENHANCEMENTS OR EXTENSIONS. INTEL SPECIFICALLY DISCLAIMS ANY
88  * IMPLIED WARRANTIES OF MERCHANTABILITY, NONINFRINGEMENT AND FITNESS FOR A
89  * PARTICULAR PURPOSE.
90  *
91  * 4.2. IN NO EVENT SHALL INTEL HAVE ANY LIABILITY TO LICENSEE, ITS LICENSEES
92  * OR ANY OTHER THIRD PARTY, FOR ANY LOST PROFITS, LOST DATA, LOSS OF USE OR
93  * COSTS OF PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, OR FOR ANY INDIRECT,
94  * SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THIS AGREEMENT, UNDER ANY
95  * CAUSE OF ACTION OR THEORY OF LIABILITY, AND IRRESPECTIVE OF WHETHER INTEL
96  * HAS ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES. THESE LIMITATIONS
97  * SHALL APPLY NOTWITHSTANDING THE FAILURE OF THE ESSENTIAL PURPOSE OF ANY
98  * LIMITED REMEDY.
99  *
100  * 4.3. Licensee shall not export, either directly or indirectly, any of this
101  * software or system incorporating such software without first obtaining any
102  * required license or other approval from the U. S. Department of Commerce or
103  * any other agency or department of the United States Government. In the
104  * event Licensee exports any such software from the United States or
105  * re-exports any such software from a foreign destination, Licensee shall
106  * ensure that the distribution and export/re-export of the software is in
107  * compliance with all laws, regulations, orders, or other restrictions of the
108  * U.S. Export Administration Regulations. Licensee agrees that neither it nor
109  * any of its subsidiaries will export/re-export any technical data, process,
110  * software, or service, directly or indirectly, to any country for which the
111  * United States government or any agency thereof requires an export license,
112  * other governmental approval, or letter of assurance, without first obtaining
113  * such license, approval or letter.
114  *
115  *****************************************************************************/
116 
117 #include "acpi.h"
118 #include "accommon.h"
119 #include "acinterp.h"
120 #include "amlcode.h"
121 
122 
123 #define _COMPONENT          ACPI_EXECUTER
124         ACPI_MODULE_NAME    ("exmisc")
125 
126 
127 /*******************************************************************************
128  *
129  * FUNCTION:    AcpiExGetObjectReference
130  *
131  * PARAMETERS:  ObjDesc             - Create a reference to this object
132  *              ReturnDesc          - Where to store the reference
133  *              WalkState           - Current state
134  *
135  * RETURN:      Status
136  *
137  * DESCRIPTION: Obtain and return a "reference" to the target object
138  *              Common code for the RefOfOp and the CondRefOfOp.
139  *
140  ******************************************************************************/
141 
142 ACPI_STATUS
143 AcpiExGetObjectReference (
144     ACPI_OPERAND_OBJECT     *ObjDesc,
145     ACPI_OPERAND_OBJECT     **ReturnDesc,
146     ACPI_WALK_STATE         *WalkState)
147 {
148     ACPI_OPERAND_OBJECT     *ReferenceObj;
149     ACPI_OPERAND_OBJECT     *ReferencedObj;
150 
151 
152     ACPI_FUNCTION_TRACE_PTR (ExGetObjectReference, ObjDesc);
153 
154 
155     *ReturnDesc = NULL;
156 
157     switch (ACPI_GET_DESCRIPTOR_TYPE (ObjDesc))
158     {
159     case ACPI_DESC_TYPE_OPERAND:
160 
161         if (ObjDesc->Common.Type != ACPI_TYPE_LOCAL_REFERENCE)
162         {
163             return_ACPI_STATUS (AE_AML_OPERAND_TYPE);
164         }
165 
166         /*
167          * Must be a reference to a Local or Arg
168          */
169         switch (ObjDesc->Reference.Class)
170         {
171         case ACPI_REFCLASS_LOCAL:
172         case ACPI_REFCLASS_ARG:
173         case ACPI_REFCLASS_DEBUG:
174 
175             /* The referenced object is the pseudo-node for the local/arg */
176 
177             ReferencedObj = ObjDesc->Reference.Object;
178             break;
179 
180         default:
181 
182             ACPI_ERROR ((AE_INFO, "Invalid Reference Class 0x%2.2X",
183                 ObjDesc->Reference.Class));
184             return_ACPI_STATUS (AE_AML_OPERAND_TYPE);
185         }
186         break;
187 
188     case ACPI_DESC_TYPE_NAMED:
189         /*
190          * A named reference that has already been resolved to a Node
191          */
192         ReferencedObj = ObjDesc;
193         break;
194 
195     default:
196 
197         ACPI_ERROR ((AE_INFO, "Invalid descriptor type 0x%X",
198             ACPI_GET_DESCRIPTOR_TYPE (ObjDesc)));
199         return_ACPI_STATUS (AE_TYPE);
200     }
201 
202 
203     /* Create a new reference object */
204 
205     ReferenceObj = AcpiUtCreateInternalObject (ACPI_TYPE_LOCAL_REFERENCE);
206     if (!ReferenceObj)
207     {
208         return_ACPI_STATUS (AE_NO_MEMORY);
209     }
210 
211     ReferenceObj->Reference.Class = ACPI_REFCLASS_REFOF;
212     ReferenceObj->Reference.Object = ReferencedObj;
213     *ReturnDesc = ReferenceObj;
214 
215     ACPI_DEBUG_PRINT ((ACPI_DB_EXEC,
216         "Object %p Type [%s], returning Reference %p\n",
217         ObjDesc, AcpiUtGetObjectTypeName (ObjDesc), *ReturnDesc));
218 
219     return_ACPI_STATUS (AE_OK);
220 }
221 
222 
223 /*******************************************************************************
224  *
225  * FUNCTION:    AcpiExDoMathOp
226  *
227  * PARAMETERS:  Opcode              - AML opcode
228  *              Integer0            - Integer operand #0
229  *              Integer1            - Integer operand #1
230  *
231  * RETURN:      Integer result of the operation
232  *
233  * DESCRIPTION: Execute a math AML opcode. The purpose of having all of the
234  *              math functions here is to prevent a lot of pointer dereferencing
235  *              to obtain the operands.
236  *
237  ******************************************************************************/
238 
239 UINT64
240 AcpiExDoMathOp (
241     UINT16                  Opcode,
242     UINT64                  Integer0,
243     UINT64                  Integer1)
244 {
245 
246     ACPI_FUNCTION_ENTRY ();
247 
248 
249     switch (Opcode)
250     {
251     case AML_ADD_OP:                /* Add (Integer0, Integer1, Result) */
252 
253         return (Integer0 + Integer1);
254 
255     case AML_BIT_AND_OP:            /* And (Integer0, Integer1, Result) */
256 
257         return (Integer0 & Integer1);
258 
259     case AML_BIT_NAND_OP:           /* NAnd (Integer0, Integer1, Result) */
260 
261         return (~(Integer0 & Integer1));
262 
263     case AML_BIT_OR_OP:             /* Or (Integer0, Integer1, Result) */
264 
265         return (Integer0 | Integer1);
266 
267     case AML_BIT_NOR_OP:            /* NOr (Integer0, Integer1, Result) */
268 
269         return (~(Integer0 | Integer1));
270 
271     case AML_BIT_XOR_OP:            /* XOr (Integer0, Integer1, Result) */
272 
273         return (Integer0 ^ Integer1);
274 
275     case AML_MULTIPLY_OP:           /* Multiply (Integer0, Integer1, Result) */
276 
277         return (Integer0 * Integer1);
278 
279     case AML_SHIFT_LEFT_OP:         /* ShiftLeft (Operand, ShiftCount, Result)*/
280 
281         /*
282          * We need to check if the shiftcount is larger than the integer bit
283          * width since the behavior of this is not well-defined in the C language.
284          */
285         if (Integer1 >= AcpiGbl_IntegerBitWidth)
286         {
287             return (0);
288         }
289         return (Integer0 << Integer1);
290 
291     case AML_SHIFT_RIGHT_OP:        /* ShiftRight (Operand, ShiftCount, Result) */
292 
293         /*
294          * We need to check if the shiftcount is larger than the integer bit
295          * width since the behavior of this is not well-defined in the C language.
296          */
297         if (Integer1 >= AcpiGbl_IntegerBitWidth)
298         {
299             return (0);
300         }
301         return (Integer0 >> Integer1);
302 
303     case AML_SUBTRACT_OP:           /* Subtract (Integer0, Integer1, Result) */
304 
305         return (Integer0 - Integer1);
306 
307     default:
308 
309         return (0);
310     }
311 }
312 
313 
314 /*******************************************************************************
315  *
316  * FUNCTION:    AcpiExDoLogicalNumericOp
317  *
318  * PARAMETERS:  Opcode              - AML opcode
319  *              Integer0            - Integer operand #0
320  *              Integer1            - Integer operand #1
321  *              LogicalResult       - TRUE/FALSE result of the operation
322  *
323  * RETURN:      Status
324  *
325  * DESCRIPTION: Execute a logical "Numeric" AML opcode. For these Numeric
326  *              operators (LAnd and LOr), both operands must be integers.
327  *
328  *              Note: cleanest machine code seems to be produced by the code
329  *              below, rather than using statements of the form:
330  *                  Result = (Integer0 && Integer1);
331  *
332  ******************************************************************************/
333 
334 ACPI_STATUS
335 AcpiExDoLogicalNumericOp (
336     UINT16                  Opcode,
337     UINT64                  Integer0,
338     UINT64                  Integer1,
339     BOOLEAN                 *LogicalResult)
340 {
341     ACPI_STATUS             Status = AE_OK;
342     BOOLEAN                 LocalResult = FALSE;
343 
344 
345     ACPI_FUNCTION_TRACE (ExDoLogicalNumericOp);
346 
347 
348     switch (Opcode)
349     {
350     case AML_LAND_OP:               /* LAnd (Integer0, Integer1) */
351 
352         if (Integer0 && Integer1)
353         {
354             LocalResult = TRUE;
355         }
356         break;
357 
358     case AML_LOR_OP:                /* LOr (Integer0, Integer1) */
359 
360         if (Integer0 || Integer1)
361         {
362             LocalResult = TRUE;
363         }
364         break;
365 
366     default:
367 
368         Status = AE_AML_INTERNAL;
369         break;
370     }
371 
372     /* Return the logical result and status */
373 
374     *LogicalResult = LocalResult;
375     return_ACPI_STATUS (Status);
376 }
377 
378 
379 /*******************************************************************************
380  *
381  * FUNCTION:    AcpiExDoLogicalOp
382  *
383  * PARAMETERS:  Opcode              - AML opcode
384  *              Operand0            - operand #0
385  *              Operand1            - operand #1
386  *              LogicalResult       - TRUE/FALSE result of the operation
387  *
388  * RETURN:      Status
389  *
390  * DESCRIPTION: Execute a logical AML opcode. The purpose of having all of the
391  *              functions here is to prevent a lot of pointer dereferencing
392  *              to obtain the operands and to simplify the generation of the
393  *              logical value. For the Numeric operators (LAnd and LOr), both
394  *              operands must be integers. For the other logical operators,
395  *              operands can be any combination of Integer/String/Buffer. The
396  *              first operand determines the type to which the second operand
397  *              will be converted.
398  *
399  *              Note: cleanest machine code seems to be produced by the code
400  *              below, rather than using statements of the form:
401  *                  Result = (Operand0 == Operand1);
402  *
403  ******************************************************************************/
404 
405 ACPI_STATUS
406 AcpiExDoLogicalOp (
407     UINT16                  Opcode,
408     ACPI_OPERAND_OBJECT     *Operand0,
409     ACPI_OPERAND_OBJECT     *Operand1,
410     BOOLEAN                 *LogicalResult)
411 {
412     ACPI_OPERAND_OBJECT     *LocalOperand1 = Operand1;
413     UINT64                  Integer0;
414     UINT64                  Integer1;
415     UINT32                  Length0;
416     UINT32                  Length1;
417     ACPI_STATUS             Status = AE_OK;
418     BOOLEAN                 LocalResult = FALSE;
419     int                     Compare;
420 
421 
422     ACPI_FUNCTION_TRACE (ExDoLogicalOp);
423 
424 
425     /*
426      * Convert the second operand if necessary. The first operand
427      * determines the type of the second operand, (See the Data Types
428      * section of the ACPI 3.0+ specification.)  Both object types are
429      * guaranteed to be either Integer/String/Buffer by the operand
430      * resolution mechanism.
431      */
432     switch (Operand0->Common.Type)
433     {
434     case ACPI_TYPE_INTEGER:
435 
436         Status = AcpiExConvertToInteger (Operand1, &LocalOperand1, 16);
437         break;
438 
439     case ACPI_TYPE_STRING:
440 
441         Status = AcpiExConvertToString (
442             Operand1, &LocalOperand1, ACPI_IMPLICIT_CONVERT_HEX);
443         break;
444 
445     case ACPI_TYPE_BUFFER:
446 
447         Status = AcpiExConvertToBuffer (Operand1, &LocalOperand1);
448         break;
449 
450     default:
451 
452         Status = AE_AML_INTERNAL;
453         break;
454     }
455 
456     if (ACPI_FAILURE (Status))
457     {
458         goto Cleanup;
459     }
460 
461     /*
462      * Two cases: 1) Both Integers, 2) Both Strings or Buffers
463      */
464     if (Operand0->Common.Type == ACPI_TYPE_INTEGER)
465     {
466         /*
467          * 1) Both operands are of type integer
468          *    Note: LocalOperand1 may have changed above
469          */
470         Integer0 = Operand0->Integer.Value;
471         Integer1 = LocalOperand1->Integer.Value;
472 
473         switch (Opcode)
474         {
475         case AML_LEQUAL_OP:             /* LEqual (Operand0, Operand1) */
476 
477             if (Integer0 == Integer1)
478             {
479                 LocalResult = TRUE;
480             }
481             break;
482 
483         case AML_LGREATER_OP:           /* LGreater (Operand0, Operand1) */
484 
485             if (Integer0 > Integer1)
486             {
487                 LocalResult = TRUE;
488             }
489             break;
490 
491         case AML_LLESS_OP:              /* LLess (Operand0, Operand1) */
492 
493             if (Integer0 < Integer1)
494             {
495                 LocalResult = TRUE;
496             }
497             break;
498 
499         default:
500 
501             Status = AE_AML_INTERNAL;
502             break;
503         }
504     }
505     else
506     {
507         /*
508          * 2) Both operands are Strings or both are Buffers
509          *    Note: Code below takes advantage of common Buffer/String
510          *          object fields. LocalOperand1 may have changed above. Use
511          *          memcmp to handle nulls in buffers.
512          */
513         Length0 = Operand0->Buffer.Length;
514         Length1 = LocalOperand1->Buffer.Length;
515 
516         /* Lexicographic compare: compare the data bytes */
517 
518         Compare = memcmp (Operand0->Buffer.Pointer,
519             LocalOperand1->Buffer.Pointer,
520             (Length0 > Length1) ? Length1 : Length0);
521 
522         switch (Opcode)
523         {
524         case AML_LEQUAL_OP:             /* LEqual (Operand0, Operand1) */
525 
526             /* Length and all bytes must be equal */
527 
528             if ((Length0 == Length1) &&
529                 (Compare == 0))
530             {
531                 /* Length and all bytes match ==> TRUE */
532 
533                 LocalResult = TRUE;
534             }
535             break;
536 
537         case AML_LGREATER_OP:           /* LGreater (Operand0, Operand1) */
538 
539             if (Compare > 0)
540             {
541                 LocalResult = TRUE;
542                 goto Cleanup;   /* TRUE */
543             }
544             if (Compare < 0)
545             {
546                 goto Cleanup;   /* FALSE */
547             }
548 
549             /* Bytes match (to shortest length), compare lengths */
550 
551             if (Length0 > Length1)
552             {
553                 LocalResult = TRUE;
554             }
555             break;
556 
557         case AML_LLESS_OP:              /* LLess (Operand0, Operand1) */
558 
559             if (Compare > 0)
560             {
561                 goto Cleanup;   /* FALSE */
562             }
563             if (Compare < 0)
564             {
565                 LocalResult = TRUE;
566                 goto Cleanup;   /* TRUE */
567             }
568 
569             /* Bytes match (to shortest length), compare lengths */
570 
571             if (Length0 < Length1)
572             {
573                 LocalResult = TRUE;
574             }
575             break;
576 
577         default:
578 
579             Status = AE_AML_INTERNAL;
580             break;
581         }
582     }
583 
584 Cleanup:
585 
586     /* New object was created if implicit conversion performed - delete */
587 
588     if (LocalOperand1 != Operand1)
589     {
590         AcpiUtRemoveReference (LocalOperand1);
591     }
592 
593     /* Return the logical result and status */
594 
595     *LogicalResult = LocalResult;
596     return_ACPI_STATUS (Status);
597 }
598