2 /******************************************************************************
4 * Module Name: exmisc - ACPI AML (p-code) execution - specific opcodes
6 *****************************************************************************/
9 * Copyright (C) 2000 - 2006, R. Byron Moore
10 * All rights reserved.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions, and the following disclaimer,
17 * without modification.
18 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
19 * substantially similar to the "NO WARRANTY" disclaimer below
20 * ("Disclaimer") and any redistribution must be conditioned upon
21 * including a substantially similar Disclaimer requirement for further
22 * binary redistribution.
23 * 3. Neither the names of the above-listed copyright holders nor the names
24 * of any contributors may be used to endorse or promote products derived
25 * from this software without specific prior written permission.
27 * Alternatively, this software may be distributed under the terms of the
28 * GNU General Public License ("GPL") version 2 as published by the Free
29 * Software Foundation.
32 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
33 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
34 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
35 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
36 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
37 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
38 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
39 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
40 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
41 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
42 * POSSIBILITY OF SUCH DAMAGES.
45 #include <acpi/acpi.h>
46 #include <acpi/acinterp.h>
47 #include <acpi/amlcode.h>
48 #include <acpi/amlresrc.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 (ACPI_GET_OBJECT_TYPE(obj_desc
) != 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
.opcode
) {
94 /* The referenced object is the pseudo-node for the local/arg */
96 referenced_obj
= obj_desc
->reference
.object
;
101 ACPI_REPORT_ERROR(("Unknown Reference opcode %X\n",
102 obj_desc
->reference
.opcode
));
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_REPORT_ERROR(("Invalid descriptor type %X\n",
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
.opcode
= AML_REF_OF_OP
;
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
;
170 ACPI_FUNCTION_TRACE("ex_concat_template");
173 * Find the end_tag descriptor in each resource template.
174 * Note: returned pointers point TO the end_tag, not past it.
176 * Compute the length of each resource template
178 status
= acpi_ut_get_resource_end_tag(operand0
, &end_tag
);
179 if (ACPI_FAILURE(status
)) {
180 return_ACPI_STATUS(status
);
183 length0
= ACPI_PTR_DIFF(end_tag
, operand0
->buffer
.pointer
);
185 status
= acpi_ut_get_resource_end_tag(operand1
, &end_tag
);
186 if (ACPI_FAILURE(status
)) {
187 return_ACPI_STATUS(status
);
190 /* Include the end_tag in the second template length */
192 length1
= ACPI_PTR_DIFF(end_tag
, operand1
->buffer
.pointer
) +
193 sizeof(struct aml_resource_end_tag
);
195 /* Create a new buffer object for the result */
197 return_desc
= acpi_ut_create_buffer_object(length0
+ length1
);
199 return_ACPI_STATUS(AE_NO_MEMORY
);
203 * Copy the templates to the new buffer, 0 first, then 1 follows. One
204 * end_tag descriptor is copied from Operand1.
206 new_buf
= return_desc
->buffer
.pointer
;
207 ACPI_MEMCPY(new_buf
, operand0
->buffer
.pointer
, length0
);
208 ACPI_MEMCPY(new_buf
+ length0
, operand1
->buffer
.pointer
, length1
);
210 /* Set the end_tag checksum to zero, means "ignore checksum" */
212 new_buf
[return_desc
->buffer
.length
- 1] = 0;
214 /* Return the completed resource template */
216 *actual_return_desc
= return_desc
;
217 return_ACPI_STATUS(AE_OK
);
220 /*******************************************************************************
222 * FUNCTION: acpi_ex_do_concatenate
224 * PARAMETERS: Operand0 - First source object
225 * Operand1 - Second source object
226 * actual_return_desc - Where to place the return object
227 * walk_state - Current walk state
231 * DESCRIPTION: Concatenate two objects OF THE SAME TYPE.
233 ******************************************************************************/
236 acpi_ex_do_concatenate(union acpi_operand_object
*operand0
,
237 union acpi_operand_object
*operand1
,
238 union acpi_operand_object
**actual_return_desc
,
239 struct acpi_walk_state
*walk_state
)
241 union acpi_operand_object
*local_operand1
= operand1
;
242 union acpi_operand_object
*return_desc
;
246 ACPI_FUNCTION_TRACE("ex_do_concatenate");
249 * Convert the second operand if necessary. The first operand
250 * determines the type of the second operand, (See the Data Types
251 * section of the ACPI specification.) Both object types are
252 * guaranteed to be either Integer/String/Buffer by the operand
253 * resolution mechanism.
255 switch (ACPI_GET_OBJECT_TYPE(operand0
)) {
256 case ACPI_TYPE_INTEGER
:
258 acpi_ex_convert_to_integer(operand1
, &local_operand1
, 16);
261 case ACPI_TYPE_STRING
:
262 status
= acpi_ex_convert_to_string(operand1
, &local_operand1
,
263 ACPI_IMPLICIT_CONVERT_HEX
);
266 case ACPI_TYPE_BUFFER
:
267 status
= acpi_ex_convert_to_buffer(operand1
, &local_operand1
);
271 ACPI_REPORT_ERROR(("Invalid object type: %X\n",
272 ACPI_GET_OBJECT_TYPE(operand0
)));
273 status
= AE_AML_INTERNAL
;
276 if (ACPI_FAILURE(status
)) {
281 * Both operands are now known to be the same object type
282 * (Both are Integer, String, or Buffer), and we can now perform the
287 * There are three cases to handle:
289 * 1) Two Integers concatenated to produce a new Buffer
290 * 2) Two Strings concatenated to produce a new String
291 * 3) Two Buffers concatenated to produce a new Buffer
293 switch (ACPI_GET_OBJECT_TYPE(operand0
)) {
294 case ACPI_TYPE_INTEGER
:
296 /* Result of two Integers is a Buffer */
297 /* Need enough buffer space for two integers */
299 return_desc
= acpi_ut_create_buffer_object((acpi_size
)
301 (acpi_gbl_integer_byte_width
));
303 status
= AE_NO_MEMORY
;
307 new_buf
= (char *)return_desc
->buffer
.pointer
;
309 /* Copy the first integer, LSB first */
311 ACPI_MEMCPY(new_buf
, &operand0
->integer
.value
,
312 acpi_gbl_integer_byte_width
);
314 /* Copy the second integer (LSB first) after the first */
316 ACPI_MEMCPY(new_buf
+ acpi_gbl_integer_byte_width
,
317 &local_operand1
->integer
.value
,
318 acpi_gbl_integer_byte_width
);
321 case ACPI_TYPE_STRING
:
323 /* Result of two Strings is a String */
325 return_desc
= acpi_ut_create_string_object((acpi_size
)
331 status
= AE_NO_MEMORY
;
335 new_buf
= return_desc
->string
.pointer
;
337 /* Concatenate the strings */
339 ACPI_STRCPY(new_buf
, operand0
->string
.pointer
);
340 ACPI_STRCPY(new_buf
+ operand0
->string
.length
,
341 local_operand1
->string
.pointer
);
344 case ACPI_TYPE_BUFFER
:
346 /* Result of two Buffers is a Buffer */
348 return_desc
= acpi_ut_create_buffer_object((acpi_size
)
354 status
= AE_NO_MEMORY
;
358 new_buf
= (char *)return_desc
->buffer
.pointer
;
360 /* Concatenate the buffers */
362 ACPI_MEMCPY(new_buf
, operand0
->buffer
.pointer
,
363 operand0
->buffer
.length
);
364 ACPI_MEMCPY(new_buf
+ operand0
->buffer
.length
,
365 local_operand1
->buffer
.pointer
,
366 local_operand1
->buffer
.length
);
371 /* Invalid object type, should not happen here */
373 ACPI_REPORT_ERROR(("Invalid object type: %X\n",
374 ACPI_GET_OBJECT_TYPE(operand0
)));
375 status
= AE_AML_INTERNAL
;
379 *actual_return_desc
= return_desc
;
382 if (local_operand1
!= operand1
) {
383 acpi_ut_remove_reference(local_operand1
);
385 return_ACPI_STATUS(status
);
388 /*******************************************************************************
390 * FUNCTION: acpi_ex_do_math_op
392 * PARAMETERS: Opcode - AML opcode
393 * Integer0 - Integer operand #0
394 * Integer1 - Integer operand #1
396 * RETURN: Integer result of the operation
398 * DESCRIPTION: Execute a math AML opcode. The purpose of having all of the
399 * math functions here is to prevent a lot of pointer dereferencing
400 * to obtain the operands.
402 ******************************************************************************/
405 acpi_ex_do_math_op(u16 opcode
, acpi_integer integer0
, acpi_integer integer1
)
408 ACPI_FUNCTION_ENTRY();
411 case AML_ADD_OP
: /* Add (Integer0, Integer1, Result) */
413 return (integer0
+ integer1
);
415 case AML_BIT_AND_OP
: /* And (Integer0, Integer1, Result) */
417 return (integer0
& integer1
);
419 case AML_BIT_NAND_OP
: /* NAnd (Integer0, Integer1, Result) */
421 return (~(integer0
& integer1
));
423 case AML_BIT_OR_OP
: /* Or (Integer0, Integer1, Result) */
425 return (integer0
| integer1
);
427 case AML_BIT_NOR_OP
: /* NOr (Integer0, Integer1, Result) */
429 return (~(integer0
| integer1
));
431 case AML_BIT_XOR_OP
: /* XOr (Integer0, Integer1, Result) */
433 return (integer0
^ integer1
);
435 case AML_MULTIPLY_OP
: /* Multiply (Integer0, Integer1, Result) */
437 return (integer0
* integer1
);
439 case AML_SHIFT_LEFT_OP
: /* shift_left (Operand, shift_count, Result) */
441 return (integer0
<< integer1
);
443 case AML_SHIFT_RIGHT_OP
: /* shift_right (Operand, shift_count, Result) */
445 return (integer0
>> integer1
);
447 case AML_SUBTRACT_OP
: /* Subtract (Integer0, Integer1, Result) */
449 return (integer0
- integer1
);
457 /*******************************************************************************
459 * FUNCTION: acpi_ex_do_logical_numeric_op
461 * PARAMETERS: Opcode - AML opcode
462 * Integer0 - Integer operand #0
463 * Integer1 - Integer operand #1
464 * logical_result - TRUE/FALSE result of the operation
468 * DESCRIPTION: Execute a logical "Numeric" AML opcode. For these Numeric
469 * operators (LAnd and LOr), both operands must be integers.
471 * Note: cleanest machine code seems to be produced by the code
472 * below, rather than using statements of the form:
473 * Result = (Integer0 && Integer1);
475 ******************************************************************************/
478 acpi_ex_do_logical_numeric_op(u16 opcode
,
479 acpi_integer integer0
,
480 acpi_integer integer1
, u8
* logical_result
)
482 acpi_status status
= AE_OK
;
483 u8 local_result
= FALSE
;
485 ACPI_FUNCTION_TRACE("ex_do_logical_numeric_op");
488 case AML_LAND_OP
: /* LAnd (Integer0, Integer1) */
490 if (integer0
&& integer1
) {
495 case AML_LOR_OP
: /* LOr (Integer0, Integer1) */
497 if (integer0
|| integer1
) {
503 status
= AE_AML_INTERNAL
;
507 /* Return the logical result and status */
509 *logical_result
= local_result
;
510 return_ACPI_STATUS(status
);
513 /*******************************************************************************
515 * FUNCTION: acpi_ex_do_logical_op
517 * PARAMETERS: Opcode - AML opcode
518 * Operand0 - operand #0
519 * Operand1 - operand #1
520 * logical_result - TRUE/FALSE result of the operation
524 * DESCRIPTION: Execute a logical AML opcode. The purpose of having all of the
525 * functions here is to prevent a lot of pointer dereferencing
526 * to obtain the operands and to simplify the generation of the
527 * logical value. For the Numeric operators (LAnd and LOr), both
528 * operands must be integers. For the other logical operators,
529 * operands can be any combination of Integer/String/Buffer. The
530 * first operand determines the type to which the second operand
533 * Note: cleanest machine code seems to be produced by the code
534 * below, rather than using statements of the form:
535 * Result = (Operand0 == Operand1);
537 ******************************************************************************/
540 acpi_ex_do_logical_op(u16 opcode
,
541 union acpi_operand_object
*operand0
,
542 union acpi_operand_object
*operand1
, u8
* logical_result
)
544 union acpi_operand_object
*local_operand1
= operand1
;
545 acpi_integer integer0
;
546 acpi_integer integer1
;
549 acpi_status status
= AE_OK
;
550 u8 local_result
= FALSE
;
553 ACPI_FUNCTION_TRACE("ex_do_logical_op");
556 * Convert the second operand if necessary. The first operand
557 * determines the type of the second operand, (See the Data Types
558 * section of the ACPI 3.0+ specification.) Both object types are
559 * guaranteed to be either Integer/String/Buffer by the operand
560 * resolution mechanism.
562 switch (ACPI_GET_OBJECT_TYPE(operand0
)) {
563 case ACPI_TYPE_INTEGER
:
565 acpi_ex_convert_to_integer(operand1
, &local_operand1
, 16);
568 case ACPI_TYPE_STRING
:
569 status
= acpi_ex_convert_to_string(operand1
, &local_operand1
,
570 ACPI_IMPLICIT_CONVERT_HEX
);
573 case ACPI_TYPE_BUFFER
:
574 status
= acpi_ex_convert_to_buffer(operand1
, &local_operand1
);
578 status
= AE_AML_INTERNAL
;
582 if (ACPI_FAILURE(status
)) {
587 * Two cases: 1) Both Integers, 2) Both Strings or Buffers
589 if (ACPI_GET_OBJECT_TYPE(operand0
) == ACPI_TYPE_INTEGER
) {
591 * 1) Both operands are of type integer
592 * Note: local_operand1 may have changed above
594 integer0
= operand0
->integer
.value
;
595 integer1
= local_operand1
->integer
.value
;
598 case AML_LEQUAL_OP
: /* LEqual (Operand0, Operand1) */
600 if (integer0
== integer1
) {
605 case AML_LGREATER_OP
: /* LGreater (Operand0, Operand1) */
607 if (integer0
> integer1
) {
612 case AML_LLESS_OP
: /* LLess (Operand0, Operand1) */
614 if (integer0
< integer1
) {
620 status
= AE_AML_INTERNAL
;
625 * 2) Both operands are Strings or both are Buffers
626 * Note: Code below takes advantage of common Buffer/String
627 * object fields. local_operand1 may have changed above. Use
628 * memcmp to handle nulls in buffers.
630 length0
= operand0
->buffer
.length
;
631 length1
= local_operand1
->buffer
.length
;
633 /* Lexicographic compare: compare the data bytes */
635 compare
= ACPI_MEMCMP(operand0
->buffer
.pointer
,
636 local_operand1
->buffer
.pointer
,
637 (length0
> length1
) ? length1
: length0
);
640 case AML_LEQUAL_OP
: /* LEqual (Operand0, Operand1) */
642 /* Length and all bytes must be equal */
644 if ((length0
== length1
) && (compare
== 0)) {
645 /* Length and all bytes match ==> TRUE */
651 case AML_LGREATER_OP
: /* LGreater (Operand0, Operand1) */
655 goto cleanup
; /* TRUE */
658 goto cleanup
; /* FALSE */
661 /* Bytes match (to shortest length), compare lengths */
663 if (length0
> length1
) {
668 case AML_LLESS_OP
: /* LLess (Operand0, Operand1) */
671 goto cleanup
; /* FALSE */
675 goto cleanup
; /* TRUE */
678 /* Bytes match (to shortest length), compare lengths */
680 if (length0
< length1
) {
686 status
= AE_AML_INTERNAL
;
693 /* New object was created if implicit conversion performed - delete */
695 if (local_operand1
!= operand1
) {
696 acpi_ut_remove_reference(local_operand1
);
699 /* Return the logical result and status */
701 *logical_result
= local_result
;
702 return_ACPI_STATUS(status
);