1 /******************************************************************************
3 * Module Name: exmisc - ACPI AML (p-code) execution - specific opcodes
5 *****************************************************************************/
8 * Copyright (C) 2000 - 2017, 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.
50 #define _COMPONENT ACPI_EXECUTER
51 ACPI_MODULE_NAME ("exmisc")
54 /*******************************************************************************
56 * FUNCTION: AcpiExGetObjectReference
58 * PARAMETERS: ObjDesc - Create a reference to this object
59 * ReturnDesc - Where to store the reference
60 * WalkState - Current state
64 * DESCRIPTION: Obtain and return a "reference" to the target object
65 * Common code for the RefOfOp and the CondRefOfOp.
67 ******************************************************************************/
70 AcpiExGetObjectReference (
71 ACPI_OPERAND_OBJECT
*ObjDesc
,
72 ACPI_OPERAND_OBJECT
**ReturnDesc
,
73 ACPI_WALK_STATE
*WalkState
)
75 ACPI_OPERAND_OBJECT
*ReferenceObj
;
76 ACPI_OPERAND_OBJECT
*ReferencedObj
;
79 ACPI_FUNCTION_TRACE_PTR (ExGetObjectReference
, ObjDesc
);
84 switch (ACPI_GET_DESCRIPTOR_TYPE (ObjDesc
))
86 case ACPI_DESC_TYPE_OPERAND
:
88 if (ObjDesc
->Common
.Type
!= ACPI_TYPE_LOCAL_REFERENCE
)
90 return_ACPI_STATUS (AE_AML_OPERAND_TYPE
);
94 * Must be a reference to a Local or Arg
96 switch (ObjDesc
->Reference
.Class
)
98 case ACPI_REFCLASS_LOCAL
:
99 case ACPI_REFCLASS_ARG
:
100 case ACPI_REFCLASS_DEBUG
:
102 /* The referenced object is the pseudo-node for the local/arg */
104 ReferencedObj
= ObjDesc
->Reference
.Object
;
109 ACPI_ERROR ((AE_INFO
, "Invalid Reference Class 0x%2.2X",
110 ObjDesc
->Reference
.Class
));
111 return_ACPI_STATUS (AE_AML_OPERAND_TYPE
);
115 case ACPI_DESC_TYPE_NAMED
:
117 * A named reference that has already been resolved to a Node
119 ReferencedObj
= ObjDesc
;
124 ACPI_ERROR ((AE_INFO
, "Invalid descriptor type 0x%X",
125 ACPI_GET_DESCRIPTOR_TYPE (ObjDesc
)));
126 return_ACPI_STATUS (AE_TYPE
);
130 /* Create a new reference object */
132 ReferenceObj
= AcpiUtCreateInternalObject (ACPI_TYPE_LOCAL_REFERENCE
);
135 return_ACPI_STATUS (AE_NO_MEMORY
);
138 ReferenceObj
->Reference
.Class
= ACPI_REFCLASS_REFOF
;
139 ReferenceObj
->Reference
.Object
= ReferencedObj
;
140 *ReturnDesc
= ReferenceObj
;
142 ACPI_DEBUG_PRINT ((ACPI_DB_EXEC
,
143 "Object %p Type [%s], returning Reference %p\n",
144 ObjDesc
, AcpiUtGetObjectTypeName (ObjDesc
), *ReturnDesc
));
146 return_ACPI_STATUS (AE_OK
);
150 /*******************************************************************************
152 * FUNCTION: AcpiExDoMathOp
154 * PARAMETERS: Opcode - AML opcode
155 * Integer0 - Integer operand #0
156 * Integer1 - Integer operand #1
158 * RETURN: Integer result of the operation
160 * DESCRIPTION: Execute a math AML opcode. The purpose of having all of the
161 * math functions here is to prevent a lot of pointer dereferencing
162 * to obtain the operands.
164 ******************************************************************************/
173 ACPI_FUNCTION_ENTRY ();
178 case AML_ADD_OP
: /* Add (Integer0, Integer1, Result) */
180 return (Integer0
+ Integer1
);
182 case AML_BIT_AND_OP
: /* And (Integer0, Integer1, Result) */
184 return (Integer0
& Integer1
);
186 case AML_BIT_NAND_OP
: /* NAnd (Integer0, Integer1, Result) */
188 return (~(Integer0
& Integer1
));
190 case AML_BIT_OR_OP
: /* Or (Integer0, Integer1, Result) */
192 return (Integer0
| Integer1
);
194 case AML_BIT_NOR_OP
: /* NOr (Integer0, Integer1, Result) */
196 return (~(Integer0
| Integer1
));
198 case AML_BIT_XOR_OP
: /* XOr (Integer0, Integer1, Result) */
200 return (Integer0
^ Integer1
);
202 case AML_MULTIPLY_OP
: /* Multiply (Integer0, Integer1, Result) */
204 return (Integer0
* Integer1
);
206 case AML_SHIFT_LEFT_OP
: /* ShiftLeft (Operand, ShiftCount, Result)*/
209 * We need to check if the shiftcount is larger than the integer bit
210 * width since the behavior of this is not well-defined in the C language.
212 if (Integer1
>= AcpiGbl_IntegerBitWidth
)
216 return (Integer0
<< Integer1
);
218 case AML_SHIFT_RIGHT_OP
: /* ShiftRight (Operand, ShiftCount, Result) */
221 * We need to check if the shiftcount is larger than the integer bit
222 * width since the behavior of this is not well-defined in the C language.
224 if (Integer1
>= AcpiGbl_IntegerBitWidth
)
228 return (Integer0
>> Integer1
);
230 case AML_SUBTRACT_OP
: /* Subtract (Integer0, Integer1, Result) */
232 return (Integer0
- Integer1
);
241 /*******************************************************************************
243 * FUNCTION: AcpiExDoLogicalNumericOp
245 * PARAMETERS: Opcode - AML opcode
246 * Integer0 - Integer operand #0
247 * Integer1 - Integer operand #1
248 * LogicalResult - TRUE/FALSE result of the operation
252 * DESCRIPTION: Execute a logical "Numeric" AML opcode. For these Numeric
253 * operators (LAnd and LOr), both operands must be integers.
255 * Note: cleanest machine code seems to be produced by the code
256 * below, rather than using statements of the form:
257 * Result = (Integer0 && Integer1);
259 ******************************************************************************/
262 AcpiExDoLogicalNumericOp (
266 BOOLEAN
*LogicalResult
)
268 ACPI_STATUS Status
= AE_OK
;
269 BOOLEAN LocalResult
= FALSE
;
272 ACPI_FUNCTION_TRACE (ExDoLogicalNumericOp
);
277 case AML_LOGICAL_AND_OP
: /* LAnd (Integer0, Integer1) */
279 if (Integer0
&& Integer1
)
285 case AML_LOGICAL_OR_OP
: /* LOr (Integer0, Integer1) */
287 if (Integer0
|| Integer1
)
295 ACPI_ERROR ((AE_INFO
,
296 "Invalid numeric logical opcode: %X", Opcode
));
297 Status
= AE_AML_INTERNAL
;
301 /* Return the logical result and status */
303 *LogicalResult
= LocalResult
;
304 return_ACPI_STATUS (Status
);
308 /*******************************************************************************
310 * FUNCTION: AcpiExDoLogicalOp
312 * PARAMETERS: Opcode - AML opcode
313 * Operand0 - operand #0
314 * Operand1 - operand #1
315 * LogicalResult - TRUE/FALSE result of the operation
319 * DESCRIPTION: Execute a logical AML opcode. The purpose of having all of the
320 * functions here is to prevent a lot of pointer dereferencing
321 * to obtain the operands and to simplify the generation of the
322 * logical value. For the Numeric operators (LAnd and LOr), both
323 * operands must be integers. For the other logical operators,
324 * operands can be any combination of Integer/String/Buffer. The
325 * first operand determines the type to which the second operand
328 * Note: cleanest machine code seems to be produced by the code
329 * below, rather than using statements of the form:
330 * Result = (Operand0 == Operand1);
332 ******************************************************************************/
337 ACPI_OPERAND_OBJECT
*Operand0
,
338 ACPI_OPERAND_OBJECT
*Operand1
,
339 BOOLEAN
*LogicalResult
)
341 ACPI_OPERAND_OBJECT
*LocalOperand1
= Operand1
;
346 ACPI_STATUS Status
= AE_OK
;
347 BOOLEAN LocalResult
= FALSE
;
351 ACPI_FUNCTION_TRACE (ExDoLogicalOp
);
355 * Convert the second operand if necessary. The first operand
356 * determines the type of the second operand, (See the Data Types
357 * section of the ACPI 3.0+ specification.) Both object types are
358 * guaranteed to be either Integer/String/Buffer by the operand
359 * resolution mechanism.
361 switch (Operand0
->Common
.Type
)
363 case ACPI_TYPE_INTEGER
:
365 Status
= AcpiExConvertToInteger (Operand1
, &LocalOperand1
,
366 ACPI_STRTOUL_BASE16
);
369 case ACPI_TYPE_STRING
:
371 Status
= AcpiExConvertToString (
372 Operand1
, &LocalOperand1
, ACPI_IMPLICIT_CONVERT_HEX
);
375 case ACPI_TYPE_BUFFER
:
377 Status
= AcpiExConvertToBuffer (Operand1
, &LocalOperand1
);
382 ACPI_ERROR ((AE_INFO
,
383 "Invalid object type for logical operator: %X",
384 Operand0
->Common
.Type
));
385 Status
= AE_AML_INTERNAL
;
389 if (ACPI_FAILURE (Status
))
395 * Two cases: 1) Both Integers, 2) Both Strings or Buffers
397 if (Operand0
->Common
.Type
== ACPI_TYPE_INTEGER
)
400 * 1) Both operands are of type integer
401 * Note: LocalOperand1 may have changed above
403 Integer0
= Operand0
->Integer
.Value
;
404 Integer1
= LocalOperand1
->Integer
.Value
;
408 case AML_LOGICAL_EQUAL_OP
: /* LEqual (Operand0, Operand1) */
410 if (Integer0
== Integer1
)
416 case AML_LOGICAL_GREATER_OP
: /* LGreater (Operand0, Operand1) */
418 if (Integer0
> Integer1
)
424 case AML_LOGICAL_LESS_OP
: /* LLess (Operand0, Operand1) */
426 if (Integer0
< Integer1
)
434 ACPI_ERROR ((AE_INFO
,
435 "Invalid comparison opcode: %X", Opcode
));
436 Status
= AE_AML_INTERNAL
;
443 * 2) Both operands are Strings or both are Buffers
444 * Note: Code below takes advantage of common Buffer/String
445 * object fields. LocalOperand1 may have changed above. Use
446 * memcmp to handle nulls in buffers.
448 Length0
= Operand0
->Buffer
.Length
;
449 Length1
= LocalOperand1
->Buffer
.Length
;
451 /* Lexicographic compare: compare the data bytes */
453 Compare
= memcmp (Operand0
->Buffer
.Pointer
,
454 LocalOperand1
->Buffer
.Pointer
,
455 (Length0
> Length1
) ? Length1
: Length0
);
459 case AML_LOGICAL_EQUAL_OP
: /* LEqual (Operand0, Operand1) */
461 /* Length and all bytes must be equal */
463 if ((Length0
== Length1
) &&
466 /* Length and all bytes match ==> TRUE */
472 case AML_LOGICAL_GREATER_OP
: /* LGreater (Operand0, Operand1) */
477 goto Cleanup
; /* TRUE */
481 goto Cleanup
; /* FALSE */
484 /* Bytes match (to shortest length), compare lengths */
486 if (Length0
> Length1
)
492 case AML_LOGICAL_LESS_OP
: /* LLess (Operand0, Operand1) */
496 goto Cleanup
; /* FALSE */
501 goto Cleanup
; /* TRUE */
504 /* Bytes match (to shortest length), compare lengths */
506 if (Length0
< Length1
)
514 ACPI_ERROR ((AE_INFO
,
515 "Invalid comparison opcode: %X", Opcode
));
516 Status
= AE_AML_INTERNAL
;
523 /* New object was created if implicit conversion performed - delete */
525 if (LocalOperand1
!= Operand1
)
527 AcpiUtRemoveReference (LocalOperand1
);
530 /* Return the logical result and status */
532 *LogicalResult
= LocalResult
;
533 return_ACPI_STATUS (Status
);