1 /******************************************************************************
3 * Module Name: exmisc - ACPI AML (p-code) execution - specific opcodes
5 *****************************************************************************/
8 * Copyright (C) 2000 - 2016, Intel Corp.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
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.
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.
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.
51 #define _COMPONENT ACPI_EXECUTER
52 ACPI_MODULE_NAME ("exmisc")
55 /*******************************************************************************
57 * FUNCTION: AcpiExGetObjectReference
59 * PARAMETERS: ObjDesc - Create a reference to this object
60 * ReturnDesc - Where to store the reference
61 * WalkState - Current state
65 * DESCRIPTION: Obtain and return a "reference" to the target object
66 * Common code for the RefOfOp and the CondRefOfOp.
68 ******************************************************************************/
71 AcpiExGetObjectReference (
72 ACPI_OPERAND_OBJECT
*ObjDesc
,
73 ACPI_OPERAND_OBJECT
**ReturnDesc
,
74 ACPI_WALK_STATE
*WalkState
)
76 ACPI_OPERAND_OBJECT
*ReferenceObj
;
77 ACPI_OPERAND_OBJECT
*ReferencedObj
;
80 ACPI_FUNCTION_TRACE_PTR (ExGetObjectReference
, ObjDesc
);
85 switch (ACPI_GET_DESCRIPTOR_TYPE (ObjDesc
))
87 case ACPI_DESC_TYPE_OPERAND
:
89 if (ObjDesc
->Common
.Type
!= ACPI_TYPE_LOCAL_REFERENCE
)
91 return_ACPI_STATUS (AE_AML_OPERAND_TYPE
);
95 * Must be a reference to a Local or Arg
97 switch (ObjDesc
->Reference
.Class
)
99 case ACPI_REFCLASS_LOCAL
:
100 case ACPI_REFCLASS_ARG
:
101 case ACPI_REFCLASS_DEBUG
:
103 /* The referenced object is the pseudo-node for the local/arg */
105 ReferencedObj
= ObjDesc
->Reference
.Object
;
110 ACPI_ERROR ((AE_INFO
, "Invalid Reference Class 0x%2.2X",
111 ObjDesc
->Reference
.Class
));
112 return_ACPI_STATUS (AE_AML_OPERAND_TYPE
);
116 case ACPI_DESC_TYPE_NAMED
:
118 * A named reference that has already been resolved to a Node
120 ReferencedObj
= ObjDesc
;
125 ACPI_ERROR ((AE_INFO
, "Invalid descriptor type 0x%X",
126 ACPI_GET_DESCRIPTOR_TYPE (ObjDesc
)));
127 return_ACPI_STATUS (AE_TYPE
);
131 /* Create a new reference object */
133 ReferenceObj
= AcpiUtCreateInternalObject (ACPI_TYPE_LOCAL_REFERENCE
);
136 return_ACPI_STATUS (AE_NO_MEMORY
);
139 ReferenceObj
->Reference
.Class
= ACPI_REFCLASS_REFOF
;
140 ReferenceObj
->Reference
.Object
= ReferencedObj
;
141 *ReturnDesc
= ReferenceObj
;
143 ACPI_DEBUG_PRINT ((ACPI_DB_EXEC
,
144 "Object %p Type [%s], returning Reference %p\n",
145 ObjDesc
, AcpiUtGetObjectTypeName (ObjDesc
), *ReturnDesc
));
147 return_ACPI_STATUS (AE_OK
);
151 /*******************************************************************************
153 * FUNCTION: AcpiExConcatTemplate
155 * PARAMETERS: Operand0 - First source object
156 * Operand1 - Second source object
157 * ActualReturnDesc - Where to place the return object
158 * WalkState - Current walk state
162 * DESCRIPTION: Concatenate two resource templates
164 ******************************************************************************/
167 AcpiExConcatTemplate (
168 ACPI_OPERAND_OBJECT
*Operand0
,
169 ACPI_OPERAND_OBJECT
*Operand1
,
170 ACPI_OPERAND_OBJECT
**ActualReturnDesc
,
171 ACPI_WALK_STATE
*WalkState
)
174 ACPI_OPERAND_OBJECT
*ReturnDesc
;
182 ACPI_FUNCTION_TRACE (ExConcatTemplate
);
186 * Find the EndTag descriptor in each resource template.
187 * Note1: returned pointers point TO the EndTag, not past it.
188 * Note2: zero-length buffers are allowed; treated like one EndTag
191 /* Get the length of the first resource template */
193 Status
= AcpiUtGetResourceEndTag (Operand0
, &EndTag
);
194 if (ACPI_FAILURE (Status
))
196 return_ACPI_STATUS (Status
);
199 Length0
= ACPI_PTR_DIFF (EndTag
, Operand0
->Buffer
.Pointer
);
201 /* Get the length of the second resource template */
203 Status
= AcpiUtGetResourceEndTag (Operand1
, &EndTag
);
204 if (ACPI_FAILURE (Status
))
206 return_ACPI_STATUS (Status
);
209 Length1
= ACPI_PTR_DIFF (EndTag
, Operand1
->Buffer
.Pointer
);
211 /* Combine both lengths, minimum size will be 2 for EndTag */
213 NewLength
= Length0
+ Length1
+ sizeof (AML_RESOURCE_END_TAG
);
215 /* Create a new buffer object for the result (with one EndTag) */
217 ReturnDesc
= AcpiUtCreateBufferObject (NewLength
);
220 return_ACPI_STATUS (AE_NO_MEMORY
);
224 * Copy the templates to the new buffer, 0 first, then 1 follows. One
225 * EndTag descriptor is copied from Operand1.
227 NewBuf
= ReturnDesc
->Buffer
.Pointer
;
228 memcpy (NewBuf
, Operand0
->Buffer
.Pointer
, Length0
);
229 memcpy (NewBuf
+ Length0
, Operand1
->Buffer
.Pointer
, Length1
);
231 /* Insert EndTag and set the checksum to zero, means "ignore checksum" */
233 NewBuf
[NewLength
- 1] = 0;
234 NewBuf
[NewLength
- 2] = ACPI_RESOURCE_NAME_END_TAG
| 1;
236 /* Return the completed resource template */
238 *ActualReturnDesc
= ReturnDesc
;
239 return_ACPI_STATUS (AE_OK
);
243 /*******************************************************************************
245 * FUNCTION: AcpiExDoConcatenate
247 * PARAMETERS: Operand0 - First source object
248 * Operand1 - Second source object
249 * ActualReturnDesc - Where to place the return object
250 * WalkState - Current walk state
254 * DESCRIPTION: Concatenate two objects OF THE SAME TYPE.
256 ******************************************************************************/
259 AcpiExDoConcatenate (
260 ACPI_OPERAND_OBJECT
*Operand0
,
261 ACPI_OPERAND_OBJECT
*Operand1
,
262 ACPI_OPERAND_OBJECT
**ActualReturnDesc
,
263 ACPI_WALK_STATE
*WalkState
)
265 ACPI_OPERAND_OBJECT
*LocalOperand1
= Operand1
;
266 ACPI_OPERAND_OBJECT
*ReturnDesc
;
268 const char *TypeString
;
272 ACPI_FUNCTION_TRACE (ExDoConcatenate
);
276 * Convert the second operand if necessary. The first operand
277 * determines the type of the second operand, (See the Data Types
278 * section of the ACPI specification.) Both object types are
279 * guaranteed to be either Integer/String/Buffer by the operand
280 * resolution mechanism.
282 switch (Operand0
->Common
.Type
)
284 case ACPI_TYPE_INTEGER
:
286 Status
= AcpiExConvertToInteger (Operand1
, &LocalOperand1
, 16);
289 case ACPI_TYPE_STRING
:
291 * Per the ACPI spec, Concatenate only supports int/str/buf.
292 * However, we support all objects here as an extension.
293 * This improves the usefulness of the Printf() macro.
296 switch (Operand1
->Common
.Type
)
298 case ACPI_TYPE_INTEGER
:
299 case ACPI_TYPE_STRING
:
300 case ACPI_TYPE_BUFFER
:
302 Status
= AcpiExConvertToString (
303 Operand1
, &LocalOperand1
, ACPI_IMPLICIT_CONVERT_HEX
);
308 * Just emit a string containing the object type.
310 TypeString
= AcpiUtGetTypeName (Operand1
->Common
.Type
);
312 LocalOperand1
= AcpiUtCreateStringObject (
313 ((ACPI_SIZE
) strlen (TypeString
) + 9)); /* 9 For "[Object]" */
316 Status
= AE_NO_MEMORY
;
320 strcpy (LocalOperand1
->String
.Pointer
, "[");
321 strcat (LocalOperand1
->String
.Pointer
, TypeString
);
322 strcat (LocalOperand1
->String
.Pointer
, " Object]");
328 case ACPI_TYPE_BUFFER
:
330 Status
= AcpiExConvertToBuffer (Operand1
, &LocalOperand1
);
335 ACPI_ERROR ((AE_INFO
, "Invalid object type: 0x%X",
336 Operand0
->Common
.Type
));
337 Status
= AE_AML_INTERNAL
;
340 if (ACPI_FAILURE (Status
))
346 * Both operands are now known to be the same object type
347 * (Both are Integer, String, or Buffer), and we can now perform the
352 * There are three cases to handle:
354 * 1) Two Integers concatenated to produce a new Buffer
355 * 2) Two Strings concatenated to produce a new String
356 * 3) Two Buffers concatenated to produce a new Buffer
358 switch (Operand0
->Common
.Type
)
360 case ACPI_TYPE_INTEGER
:
362 /* Result of two Integers is a Buffer */
363 /* Need enough buffer space for two integers */
365 ReturnDesc
= AcpiUtCreateBufferObject (
366 (ACPI_SIZE
) ACPI_MUL_2 (AcpiGbl_IntegerByteWidth
));
369 Status
= AE_NO_MEMORY
;
373 NewBuf
= (char *) ReturnDesc
->Buffer
.Pointer
;
375 /* Copy the first integer, LSB first */
377 memcpy (NewBuf
, &Operand0
->Integer
.Value
,
378 AcpiGbl_IntegerByteWidth
);
380 /* Copy the second integer (LSB first) after the first */
382 memcpy (NewBuf
+ AcpiGbl_IntegerByteWidth
,
383 &LocalOperand1
->Integer
.Value
, AcpiGbl_IntegerByteWidth
);
386 case ACPI_TYPE_STRING
:
388 /* Result of two Strings is a String */
390 ReturnDesc
= AcpiUtCreateStringObject (
391 ((ACPI_SIZE
) Operand0
->String
.Length
+
392 LocalOperand1
->String
.Length
));
395 Status
= AE_NO_MEMORY
;
399 NewBuf
= ReturnDesc
->String
.Pointer
;
401 /* Concatenate the strings */
403 strcpy (NewBuf
, Operand0
->String
.Pointer
);
404 strcat (NewBuf
, LocalOperand1
->String
.Pointer
);
407 case ACPI_TYPE_BUFFER
:
409 /* Result of two Buffers is a Buffer */
411 ReturnDesc
= AcpiUtCreateBufferObject (
412 ((ACPI_SIZE
) Operand0
->Buffer
.Length
+
413 LocalOperand1
->Buffer
.Length
));
416 Status
= AE_NO_MEMORY
;
420 NewBuf
= (char *) ReturnDesc
->Buffer
.Pointer
;
422 /* Concatenate the buffers */
424 memcpy (NewBuf
, Operand0
->Buffer
.Pointer
,
425 Operand0
->Buffer
.Length
);
426 memcpy (NewBuf
+ Operand0
->Buffer
.Length
,
427 LocalOperand1
->Buffer
.Pointer
,
428 LocalOperand1
->Buffer
.Length
);
433 /* Invalid object type, should not happen here */
435 ACPI_ERROR ((AE_INFO
, "Invalid object type: 0x%X",
436 Operand0
->Common
.Type
));
437 Status
=AE_AML_INTERNAL
;
441 *ActualReturnDesc
= ReturnDesc
;
444 if (LocalOperand1
!= Operand1
)
446 AcpiUtRemoveReference (LocalOperand1
);
448 return_ACPI_STATUS (Status
);
452 /*******************************************************************************
454 * FUNCTION: AcpiExDoMathOp
456 * PARAMETERS: Opcode - AML opcode
457 * Integer0 - Integer operand #0
458 * Integer1 - Integer operand #1
460 * RETURN: Integer result of the operation
462 * DESCRIPTION: Execute a math AML opcode. The purpose of having all of the
463 * math functions here is to prevent a lot of pointer dereferencing
464 * to obtain the operands.
466 ******************************************************************************/
475 ACPI_FUNCTION_ENTRY ();
480 case AML_ADD_OP
: /* Add (Integer0, Integer1, Result) */
482 return (Integer0
+ Integer1
);
484 case AML_BIT_AND_OP
: /* And (Integer0, Integer1, Result) */
486 return (Integer0
& Integer1
);
488 case AML_BIT_NAND_OP
: /* NAnd (Integer0, Integer1, Result) */
490 return (~(Integer0
& Integer1
));
492 case AML_BIT_OR_OP
: /* Or (Integer0, Integer1, Result) */
494 return (Integer0
| Integer1
);
496 case AML_BIT_NOR_OP
: /* NOr (Integer0, Integer1, Result) */
498 return (~(Integer0
| Integer1
));
500 case AML_BIT_XOR_OP
: /* XOr (Integer0, Integer1, Result) */
502 return (Integer0
^ Integer1
);
504 case AML_MULTIPLY_OP
: /* Multiply (Integer0, Integer1, Result) */
506 return (Integer0
* Integer1
);
508 case AML_SHIFT_LEFT_OP
: /* ShiftLeft (Operand, ShiftCount, Result)*/
511 * We need to check if the shiftcount is larger than the integer bit
512 * width since the behavior of this is not well-defined in the C language.
514 if (Integer1
>= AcpiGbl_IntegerBitWidth
)
518 return (Integer0
<< Integer1
);
520 case AML_SHIFT_RIGHT_OP
: /* ShiftRight (Operand, ShiftCount, Result) */
523 * We need to check if the shiftcount is larger than the integer bit
524 * width since the behavior of this is not well-defined in the C language.
526 if (Integer1
>= AcpiGbl_IntegerBitWidth
)
530 return (Integer0
>> Integer1
);
532 case AML_SUBTRACT_OP
: /* Subtract (Integer0, Integer1, Result) */
534 return (Integer0
- Integer1
);
543 /*******************************************************************************
545 * FUNCTION: AcpiExDoLogicalNumericOp
547 * PARAMETERS: Opcode - AML opcode
548 * Integer0 - Integer operand #0
549 * Integer1 - Integer operand #1
550 * LogicalResult - TRUE/FALSE result of the operation
554 * DESCRIPTION: Execute a logical "Numeric" AML opcode. For these Numeric
555 * operators (LAnd and LOr), both operands must be integers.
557 * Note: cleanest machine code seems to be produced by the code
558 * below, rather than using statements of the form:
559 * Result = (Integer0 && Integer1);
561 ******************************************************************************/
564 AcpiExDoLogicalNumericOp (
568 BOOLEAN
*LogicalResult
)
570 ACPI_STATUS Status
= AE_OK
;
571 BOOLEAN LocalResult
= FALSE
;
574 ACPI_FUNCTION_TRACE (ExDoLogicalNumericOp
);
579 case AML_LAND_OP
: /* LAnd (Integer0, Integer1) */
581 if (Integer0
&& Integer1
)
587 case AML_LOR_OP
: /* LOr (Integer0, Integer1) */
589 if (Integer0
|| Integer1
)
597 Status
= AE_AML_INTERNAL
;
601 /* Return the logical result and status */
603 *LogicalResult
= LocalResult
;
604 return_ACPI_STATUS (Status
);
608 /*******************************************************************************
610 * FUNCTION: AcpiExDoLogicalOp
612 * PARAMETERS: Opcode - AML opcode
613 * Operand0 - operand #0
614 * Operand1 - operand #1
615 * LogicalResult - TRUE/FALSE result of the operation
619 * DESCRIPTION: Execute a logical AML opcode. The purpose of having all of the
620 * functions here is to prevent a lot of pointer dereferencing
621 * to obtain the operands and to simplify the generation of the
622 * logical value. For the Numeric operators (LAnd and LOr), both
623 * operands must be integers. For the other logical operators,
624 * operands can be any combination of Integer/String/Buffer. The
625 * first operand determines the type to which the second operand
628 * Note: cleanest machine code seems to be produced by the code
629 * below, rather than using statements of the form:
630 * Result = (Operand0 == Operand1);
632 ******************************************************************************/
637 ACPI_OPERAND_OBJECT
*Operand0
,
638 ACPI_OPERAND_OBJECT
*Operand1
,
639 BOOLEAN
*LogicalResult
)
641 ACPI_OPERAND_OBJECT
*LocalOperand1
= Operand1
;
646 ACPI_STATUS Status
= AE_OK
;
647 BOOLEAN LocalResult
= FALSE
;
651 ACPI_FUNCTION_TRACE (ExDoLogicalOp
);
655 * Convert the second operand if necessary. The first operand
656 * determines the type of the second operand, (See the Data Types
657 * section of the ACPI 3.0+ specification.) Both object types are
658 * guaranteed to be either Integer/String/Buffer by the operand
659 * resolution mechanism.
661 switch (Operand0
->Common
.Type
)
663 case ACPI_TYPE_INTEGER
:
665 Status
= AcpiExConvertToInteger (Operand1
, &LocalOperand1
, 16);
668 case ACPI_TYPE_STRING
:
670 Status
= AcpiExConvertToString (
671 Operand1
, &LocalOperand1
, ACPI_IMPLICIT_CONVERT_HEX
);
674 case ACPI_TYPE_BUFFER
:
676 Status
= AcpiExConvertToBuffer (Operand1
, &LocalOperand1
);
681 Status
= AE_AML_INTERNAL
;
685 if (ACPI_FAILURE (Status
))
691 * Two cases: 1) Both Integers, 2) Both Strings or Buffers
693 if (Operand0
->Common
.Type
== ACPI_TYPE_INTEGER
)
696 * 1) Both operands are of type integer
697 * Note: LocalOperand1 may have changed above
699 Integer0
= Operand0
->Integer
.Value
;
700 Integer1
= LocalOperand1
->Integer
.Value
;
704 case AML_LEQUAL_OP
: /* LEqual (Operand0, Operand1) */
706 if (Integer0
== Integer1
)
712 case AML_LGREATER_OP
: /* LGreater (Operand0, Operand1) */
714 if (Integer0
> Integer1
)
720 case AML_LLESS_OP
: /* LLess (Operand0, Operand1) */
722 if (Integer0
< Integer1
)
730 Status
= AE_AML_INTERNAL
;
737 * 2) Both operands are Strings or both are Buffers
738 * Note: Code below takes advantage of common Buffer/String
739 * object fields. LocalOperand1 may have changed above. Use
740 * memcmp to handle nulls in buffers.
742 Length0
= Operand0
->Buffer
.Length
;
743 Length1
= LocalOperand1
->Buffer
.Length
;
745 /* Lexicographic compare: compare the data bytes */
747 Compare
= memcmp (Operand0
->Buffer
.Pointer
,
748 LocalOperand1
->Buffer
.Pointer
,
749 (Length0
> Length1
) ? Length1
: Length0
);
753 case AML_LEQUAL_OP
: /* LEqual (Operand0, Operand1) */
755 /* Length and all bytes must be equal */
757 if ((Length0
== Length1
) &&
760 /* Length and all bytes match ==> TRUE */
766 case AML_LGREATER_OP
: /* LGreater (Operand0, Operand1) */
771 goto Cleanup
; /* TRUE */
775 goto Cleanup
; /* FALSE */
778 /* Bytes match (to shortest length), compare lengths */
780 if (Length0
> Length1
)
786 case AML_LLESS_OP
: /* LLess (Operand0, Operand1) */
790 goto Cleanup
; /* FALSE */
795 goto Cleanup
; /* TRUE */
798 /* Bytes match (to shortest length), compare lengths */
800 if (Length0
< Length1
)
808 Status
= AE_AML_INTERNAL
;
815 /* New object was created if implicit conversion performed - delete */
817 if (LocalOperand1
!= Operand1
)
819 AcpiUtRemoveReference (LocalOperand1
);
822 /* Return the logical result and status */
824 *LogicalResult
= LocalResult
;
825 return_ACPI_STATUS (Status
);