1 /******************************************************************************
3 * Module Name: exmisc - ACPI AML (p-code) execution - specific opcodes
5 *****************************************************************************/
8 * Copyright (C) 2000 - 2012, 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.
44 #include <acpi/acpi.h>
50 #define _COMPONENT ACPI_EXECUTER
51 ACPI_MODULE_NAME("exmisc")
53 /*******************************************************************************
55 * FUNCTION: acpi_ex_get_object_reference
57 * PARAMETERS: obj_desc - Create a reference to this object
58 * return_desc - Where to store the reference
59 * walk_state - Current state
63 * DESCRIPTION: Obtain and return a "reference" to the target object
64 * Common code for the ref_of_op and the cond_ref_of_op.
66 ******************************************************************************/
68 acpi_ex_get_object_reference(union acpi_operand_object
*obj_desc
,
69 union acpi_operand_object
**return_desc
,
70 struct acpi_walk_state
*walk_state
)
72 union acpi_operand_object
*reference_obj
;
73 union acpi_operand_object
*referenced_obj
;
75 ACPI_FUNCTION_TRACE_PTR(ex_get_object_reference
, obj_desc
);
79 switch (ACPI_GET_DESCRIPTOR_TYPE(obj_desc
)) {
80 case ACPI_DESC_TYPE_OPERAND
:
82 if (obj_desc
->common
.type
!= ACPI_TYPE_LOCAL_REFERENCE
) {
83 return_ACPI_STATUS(AE_AML_OPERAND_TYPE
);
87 * Must be a reference to a Local or Arg
89 switch (obj_desc
->reference
.class) {
90 case ACPI_REFCLASS_LOCAL
:
91 case ACPI_REFCLASS_ARG
:
92 case ACPI_REFCLASS_DEBUG
:
94 /* The referenced object is the pseudo-node for the local/arg */
96 referenced_obj
= obj_desc
->reference
.object
;
101 ACPI_ERROR((AE_INFO
, "Unknown Reference Class 0x%2.2X",
102 obj_desc
->reference
.class));
103 return_ACPI_STATUS(AE_AML_INTERNAL
);
107 case ACPI_DESC_TYPE_NAMED
:
110 * A named reference that has already been resolved to a Node
112 referenced_obj
= obj_desc
;
117 ACPI_ERROR((AE_INFO
, "Invalid descriptor type 0x%X",
118 ACPI_GET_DESCRIPTOR_TYPE(obj_desc
)));
119 return_ACPI_STATUS(AE_TYPE
);
122 /* Create a new reference object */
125 acpi_ut_create_internal_object(ACPI_TYPE_LOCAL_REFERENCE
);
126 if (!reference_obj
) {
127 return_ACPI_STATUS(AE_NO_MEMORY
);
130 reference_obj
->reference
.class = ACPI_REFCLASS_REFOF
;
131 reference_obj
->reference
.object
= referenced_obj
;
132 *return_desc
= reference_obj
;
134 ACPI_DEBUG_PRINT((ACPI_DB_EXEC
,
135 "Object %p Type [%s], returning Reference %p\n",
136 obj_desc
, acpi_ut_get_object_type_name(obj_desc
),
139 return_ACPI_STATUS(AE_OK
);
142 /*******************************************************************************
144 * FUNCTION: acpi_ex_concat_template
146 * PARAMETERS: operand0 - First source object
147 * operand1 - Second source object
148 * actual_return_desc - Where to place the return object
149 * walk_state - Current walk state
153 * DESCRIPTION: Concatenate two resource templates
155 ******************************************************************************/
158 acpi_ex_concat_template(union acpi_operand_object
*operand0
,
159 union acpi_operand_object
*operand1
,
160 union acpi_operand_object
**actual_return_desc
,
161 struct acpi_walk_state
*walk_state
)
164 union acpi_operand_object
*return_desc
;
169 acpi_size new_length
;
171 ACPI_FUNCTION_TRACE(ex_concat_template
);
174 * Find the end_tag descriptor in each resource template.
175 * Note1: returned pointers point TO the end_tag, not past it.
176 * Note2: zero-length buffers are allowed; treated like one end_tag
179 /* Get the length of the first resource template */
181 status
= acpi_ut_get_resource_end_tag(operand0
, &end_tag
);
182 if (ACPI_FAILURE(status
)) {
183 return_ACPI_STATUS(status
);
186 length0
= ACPI_PTR_DIFF(end_tag
, operand0
->buffer
.pointer
);
188 /* Get the length of the second resource template */
190 status
= acpi_ut_get_resource_end_tag(operand1
, &end_tag
);
191 if (ACPI_FAILURE(status
)) {
192 return_ACPI_STATUS(status
);
195 length1
= ACPI_PTR_DIFF(end_tag
, operand1
->buffer
.pointer
);
197 /* Combine both lengths, minimum size will be 2 for end_tag */
199 new_length
= length0
+ length1
+ sizeof(struct aml_resource_end_tag
);
201 /* Create a new buffer object for the result (with one end_tag) */
203 return_desc
= acpi_ut_create_buffer_object(new_length
);
205 return_ACPI_STATUS(AE_NO_MEMORY
);
209 * Copy the templates to the new buffer, 0 first, then 1 follows. One
210 * end_tag descriptor is copied from Operand1.
212 new_buf
= return_desc
->buffer
.pointer
;
213 ACPI_MEMCPY(new_buf
, operand0
->buffer
.pointer
, length0
);
214 ACPI_MEMCPY(new_buf
+ length0
, operand1
->buffer
.pointer
, length1
);
216 /* Insert end_tag and set the checksum to zero, means "ignore checksum" */
218 new_buf
[new_length
- 1] = 0;
219 new_buf
[new_length
- 2] = ACPI_RESOURCE_NAME_END_TAG
| 1;
221 /* Return the completed resource template */
223 *actual_return_desc
= return_desc
;
224 return_ACPI_STATUS(AE_OK
);
227 /*******************************************************************************
229 * FUNCTION: acpi_ex_do_concatenate
231 * PARAMETERS: operand0 - First source object
232 * operand1 - Second source object
233 * actual_return_desc - Where to place the return object
234 * walk_state - Current walk state
238 * DESCRIPTION: Concatenate two objects OF THE SAME TYPE.
240 ******************************************************************************/
243 acpi_ex_do_concatenate(union acpi_operand_object
*operand0
,
244 union acpi_operand_object
*operand1
,
245 union acpi_operand_object
**actual_return_desc
,
246 struct acpi_walk_state
*walk_state
)
248 union acpi_operand_object
*local_operand1
= operand1
;
249 union acpi_operand_object
*return_desc
;
253 ACPI_FUNCTION_TRACE(ex_do_concatenate
);
256 * Convert the second operand if necessary. The first operand
257 * determines the type of the second operand, (See the Data Types
258 * section of the ACPI specification.) Both object types are
259 * guaranteed to be either Integer/String/Buffer by the operand
260 * resolution mechanism.
262 switch (operand0
->common
.type
) {
263 case ACPI_TYPE_INTEGER
:
265 acpi_ex_convert_to_integer(operand1
, &local_operand1
, 16);
268 case ACPI_TYPE_STRING
:
269 status
= acpi_ex_convert_to_string(operand1
, &local_operand1
,
270 ACPI_IMPLICIT_CONVERT_HEX
);
273 case ACPI_TYPE_BUFFER
:
274 status
= acpi_ex_convert_to_buffer(operand1
, &local_operand1
);
278 ACPI_ERROR((AE_INFO
, "Invalid object type: 0x%X",
279 operand0
->common
.type
));
280 status
= AE_AML_INTERNAL
;
283 if (ACPI_FAILURE(status
)) {
288 * Both operands are now known to be the same object type
289 * (Both are Integer, String, or Buffer), and we can now perform the
294 * There are three cases to handle:
296 * 1) Two Integers concatenated to produce a new Buffer
297 * 2) Two Strings concatenated to produce a new String
298 * 3) Two Buffers concatenated to produce a new Buffer
300 switch (operand0
->common
.type
) {
301 case ACPI_TYPE_INTEGER
:
303 /* Result of two Integers is a Buffer */
304 /* Need enough buffer space for two integers */
306 return_desc
= acpi_ut_create_buffer_object((acpi_size
)
308 (acpi_gbl_integer_byte_width
));
310 status
= AE_NO_MEMORY
;
314 new_buf
= (char *)return_desc
->buffer
.pointer
;
316 /* Copy the first integer, LSB first */
318 ACPI_MEMCPY(new_buf
, &operand0
->integer
.value
,
319 acpi_gbl_integer_byte_width
);
321 /* Copy the second integer (LSB first) after the first */
323 ACPI_MEMCPY(new_buf
+ acpi_gbl_integer_byte_width
,
324 &local_operand1
->integer
.value
,
325 acpi_gbl_integer_byte_width
);
328 case ACPI_TYPE_STRING
:
330 /* Result of two Strings is a String */
332 return_desc
= acpi_ut_create_string_object(((acpi_size
)
338 status
= AE_NO_MEMORY
;
342 new_buf
= return_desc
->string
.pointer
;
344 /* Concatenate the strings */
346 ACPI_STRCPY(new_buf
, operand0
->string
.pointer
);
347 ACPI_STRCPY(new_buf
+ operand0
->string
.length
,
348 local_operand1
->string
.pointer
);
351 case ACPI_TYPE_BUFFER
:
353 /* Result of two Buffers is a Buffer */
355 return_desc
= acpi_ut_create_buffer_object(((acpi_size
)
361 status
= AE_NO_MEMORY
;
365 new_buf
= (char *)return_desc
->buffer
.pointer
;
367 /* Concatenate the buffers */
369 ACPI_MEMCPY(new_buf
, operand0
->buffer
.pointer
,
370 operand0
->buffer
.length
);
371 ACPI_MEMCPY(new_buf
+ operand0
->buffer
.length
,
372 local_operand1
->buffer
.pointer
,
373 local_operand1
->buffer
.length
);
378 /* Invalid object type, should not happen here */
380 ACPI_ERROR((AE_INFO
, "Invalid object type: 0x%X",
381 operand0
->common
.type
));
382 status
= AE_AML_INTERNAL
;
386 *actual_return_desc
= return_desc
;
389 if (local_operand1
!= operand1
) {
390 acpi_ut_remove_reference(local_operand1
);
392 return_ACPI_STATUS(status
);
395 /*******************************************************************************
397 * FUNCTION: acpi_ex_do_math_op
399 * PARAMETERS: opcode - AML opcode
400 * integer0 - Integer operand #0
401 * integer1 - Integer operand #1
403 * RETURN: Integer result of the operation
405 * DESCRIPTION: Execute a math AML opcode. The purpose of having all of the
406 * math functions here is to prevent a lot of pointer dereferencing
407 * to obtain the operands.
409 ******************************************************************************/
411 u64
acpi_ex_do_math_op(u16 opcode
, u64 integer0
, u64 integer1
)
414 ACPI_FUNCTION_ENTRY();
417 case AML_ADD_OP
: /* Add (Integer0, Integer1, Result) */
419 return (integer0
+ integer1
);
421 case AML_BIT_AND_OP
: /* And (Integer0, Integer1, Result) */
423 return (integer0
& integer1
);
425 case AML_BIT_NAND_OP
: /* NAnd (Integer0, Integer1, Result) */
427 return (~(integer0
& integer1
));
429 case AML_BIT_OR_OP
: /* Or (Integer0, Integer1, Result) */
431 return (integer0
| integer1
);
433 case AML_BIT_NOR_OP
: /* NOr (Integer0, Integer1, Result) */
435 return (~(integer0
| integer1
));
437 case AML_BIT_XOR_OP
: /* XOr (Integer0, Integer1, Result) */
439 return (integer0
^ integer1
);
441 case AML_MULTIPLY_OP
: /* Multiply (Integer0, Integer1, Result) */
443 return (integer0
* integer1
);
445 case AML_SHIFT_LEFT_OP
: /* shift_left (Operand, shift_count, Result) */
448 * We need to check if the shiftcount is larger than the integer bit
449 * width since the behavior of this is not well-defined in the C language.
451 if (integer1
>= acpi_gbl_integer_bit_width
) {
454 return (integer0
<< integer1
);
456 case AML_SHIFT_RIGHT_OP
: /* shift_right (Operand, shift_count, Result) */
459 * We need to check if the shiftcount is larger than the integer bit
460 * width since the behavior of this is not well-defined in the C language.
462 if (integer1
>= acpi_gbl_integer_bit_width
) {
465 return (integer0
>> integer1
);
467 case AML_SUBTRACT_OP
: /* Subtract (Integer0, Integer1, Result) */
469 return (integer0
- integer1
);
477 /*******************************************************************************
479 * FUNCTION: acpi_ex_do_logical_numeric_op
481 * PARAMETERS: opcode - AML opcode
482 * integer0 - Integer operand #0
483 * integer1 - Integer operand #1
484 * logical_result - TRUE/FALSE result of the operation
488 * DESCRIPTION: Execute a logical "Numeric" AML opcode. For these Numeric
489 * operators (LAnd and LOr), both operands must be integers.
491 * Note: cleanest machine code seems to be produced by the code
492 * below, rather than using statements of the form:
493 * Result = (Integer0 && Integer1);
495 ******************************************************************************/
498 acpi_ex_do_logical_numeric_op(u16 opcode
,
499 u64 integer0
, u64 integer1
, u8
*logical_result
)
501 acpi_status status
= AE_OK
;
502 u8 local_result
= FALSE
;
504 ACPI_FUNCTION_TRACE(ex_do_logical_numeric_op
);
507 case AML_LAND_OP
: /* LAnd (Integer0, Integer1) */
509 if (integer0
&& integer1
) {
514 case AML_LOR_OP
: /* LOr (Integer0, Integer1) */
516 if (integer0
|| integer1
) {
522 status
= AE_AML_INTERNAL
;
526 /* Return the logical result and status */
528 *logical_result
= local_result
;
529 return_ACPI_STATUS(status
);
532 /*******************************************************************************
534 * FUNCTION: acpi_ex_do_logical_op
536 * PARAMETERS: opcode - AML opcode
537 * operand0 - operand #0
538 * operand1 - operand #1
539 * logical_result - TRUE/FALSE result of the operation
543 * DESCRIPTION: Execute a logical AML opcode. The purpose of having all of the
544 * functions here is to prevent a lot of pointer dereferencing
545 * to obtain the operands and to simplify the generation of the
546 * logical value. For the Numeric operators (LAnd and LOr), both
547 * operands must be integers. For the other logical operators,
548 * operands can be any combination of Integer/String/Buffer. The
549 * first operand determines the type to which the second operand
552 * Note: cleanest machine code seems to be produced by the code
553 * below, rather than using statements of the form:
554 * Result = (Operand0 == Operand1);
556 ******************************************************************************/
559 acpi_ex_do_logical_op(u16 opcode
,
560 union acpi_operand_object
*operand0
,
561 union acpi_operand_object
*operand1
, u8
* logical_result
)
563 union acpi_operand_object
*local_operand1
= operand1
;
568 acpi_status status
= AE_OK
;
569 u8 local_result
= FALSE
;
572 ACPI_FUNCTION_TRACE(ex_do_logical_op
);
575 * Convert the second operand if necessary. The first operand
576 * determines the type of the second operand, (See the Data Types
577 * section of the ACPI 3.0+ specification.) Both object types are
578 * guaranteed to be either Integer/String/Buffer by the operand
579 * resolution mechanism.
581 switch (operand0
->common
.type
) {
582 case ACPI_TYPE_INTEGER
:
584 acpi_ex_convert_to_integer(operand1
, &local_operand1
, 16);
587 case ACPI_TYPE_STRING
:
588 status
= acpi_ex_convert_to_string(operand1
, &local_operand1
,
589 ACPI_IMPLICIT_CONVERT_HEX
);
592 case ACPI_TYPE_BUFFER
:
593 status
= acpi_ex_convert_to_buffer(operand1
, &local_operand1
);
597 status
= AE_AML_INTERNAL
;
601 if (ACPI_FAILURE(status
)) {
606 * Two cases: 1) Both Integers, 2) Both Strings or Buffers
608 if (operand0
->common
.type
== ACPI_TYPE_INTEGER
) {
610 * 1) Both operands are of type integer
611 * Note: local_operand1 may have changed above
613 integer0
= operand0
->integer
.value
;
614 integer1
= local_operand1
->integer
.value
;
617 case AML_LEQUAL_OP
: /* LEqual (Operand0, Operand1) */
619 if (integer0
== integer1
) {
624 case AML_LGREATER_OP
: /* LGreater (Operand0, Operand1) */
626 if (integer0
> integer1
) {
631 case AML_LLESS_OP
: /* LLess (Operand0, Operand1) */
633 if (integer0
< integer1
) {
639 status
= AE_AML_INTERNAL
;
644 * 2) Both operands are Strings or both are Buffers
645 * Note: Code below takes advantage of common Buffer/String
646 * object fields. local_operand1 may have changed above. Use
647 * memcmp to handle nulls in buffers.
649 length0
= operand0
->buffer
.length
;
650 length1
= local_operand1
->buffer
.length
;
652 /* Lexicographic compare: compare the data bytes */
654 compare
= ACPI_MEMCMP(operand0
->buffer
.pointer
,
655 local_operand1
->buffer
.pointer
,
656 (length0
> length1
) ? length1
: length0
);
659 case AML_LEQUAL_OP
: /* LEqual (Operand0, Operand1) */
661 /* Length and all bytes must be equal */
663 if ((length0
== length1
) && (compare
== 0)) {
665 /* Length and all bytes match ==> TRUE */
671 case AML_LGREATER_OP
: /* LGreater (Operand0, Operand1) */
675 goto cleanup
; /* TRUE */
678 goto cleanup
; /* FALSE */
681 /* Bytes match (to shortest length), compare lengths */
683 if (length0
> length1
) {
688 case AML_LLESS_OP
: /* LLess (Operand0, Operand1) */
691 goto cleanup
; /* FALSE */
695 goto cleanup
; /* TRUE */
698 /* Bytes match (to shortest length), compare lengths */
700 if (length0
< length1
) {
706 status
= AE_AML_INTERNAL
;
713 /* New object was created if implicit conversion performed - delete */
715 if (local_operand1
!= operand1
) {
716 acpi_ut_remove_reference(local_operand1
);
719 /* Return the logical result and status */
721 *logical_result
= local_result
;
722 return_ACPI_STATUS(status
);