2 /******************************************************************************
4 * Module Name: exmisc - ACPI AML (p-code) execution - specific opcodes
6 *****************************************************************************/
9 * Copyright (C) 2000 - 2004, 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.
46 #include <acpi/acpi.h>
47 #include <acpi/acinterp.h>
48 #include <acpi/amlcode.h>
51 #define _COMPONENT ACPI_EXECUTER
52 ACPI_MODULE_NAME ("exmisc")
55 /*******************************************************************************
57 * FUNCTION: acpi_ex_get_object_reference
59 * PARAMETERS: obj_desc - Create a reference to this object
60 * return_desc - Where to store the reference
61 * walk_state - Current state
65 * DESCRIPTION: Obtain and return a "reference" to the target object
66 * Common code for the ref_of_op and the cond_ref_of_op.
68 ******************************************************************************/
71 acpi_ex_get_object_reference (
72 union acpi_operand_object
*obj_desc
,
73 union acpi_operand_object
**return_desc
,
74 struct acpi_walk_state
*walk_state
)
76 union acpi_operand_object
*reference_obj
;
77 union acpi_operand_object
*referenced_obj
;
80 ACPI_FUNCTION_TRACE_PTR ("ex_get_object_reference", obj_desc
);
85 switch (ACPI_GET_DESCRIPTOR_TYPE (obj_desc
)) {
86 case ACPI_DESC_TYPE_OPERAND
:
88 if (ACPI_GET_OBJECT_TYPE (obj_desc
) != ACPI_TYPE_LOCAL_REFERENCE
) {
89 return_ACPI_STATUS (AE_AML_OPERAND_TYPE
);
93 * Must be a reference to a Local or Arg
95 switch (obj_desc
->reference
.opcode
) {
99 /* The referenced object is the pseudo-node for the local/arg */
101 referenced_obj
= obj_desc
->reference
.object
;
106 ACPI_REPORT_ERROR (("Unknown Reference subtype in get ref %X\n",
107 obj_desc
->reference
.opcode
));
108 return_ACPI_STATUS (AE_AML_INTERNAL
);
113 case ACPI_DESC_TYPE_NAMED
:
116 * A named reference that has already been resolved to a Node
118 referenced_obj
= obj_desc
;
124 ACPI_REPORT_ERROR (("Invalid descriptor type in get ref: %X\n",
125 ACPI_GET_DESCRIPTOR_TYPE (obj_desc
)));
126 return_ACPI_STATUS (AE_TYPE
);
130 /* Create a new reference object */
132 reference_obj
= acpi_ut_create_internal_object (ACPI_TYPE_LOCAL_REFERENCE
);
133 if (!reference_obj
) {
134 return_ACPI_STATUS (AE_NO_MEMORY
);
137 reference_obj
->reference
.opcode
= AML_REF_OF_OP
;
138 reference_obj
->reference
.object
= referenced_obj
;
139 *return_desc
= reference_obj
;
141 ACPI_DEBUG_PRINT ((ACPI_DB_EXEC
, "Object %p Type [%s], returning Reference %p\n",
142 obj_desc
, acpi_ut_get_object_type_name (obj_desc
), *return_desc
));
144 return_ACPI_STATUS (AE_OK
);
148 /*******************************************************************************
150 * FUNCTION: acpi_ex_concat_template
152 * PARAMETERS: *obj_desc - Object to be converted. Must be an
153 * Integer, Buffer, or String
154 * walk_state - Current walk state
158 * DESCRIPTION: Concatenate two resource templates
160 ******************************************************************************/
163 acpi_ex_concat_template (
164 union acpi_operand_object
*obj_desc1
,
165 union acpi_operand_object
*obj_desc2
,
166 union acpi_operand_object
**actual_return_desc
,
167 struct acpi_walk_state
*walk_state
)
169 union acpi_operand_object
*return_desc
;
177 ACPI_FUNCTION_TRACE ("ex_concat_template");
180 /* Find the end_tags in each resource template */
182 end_tag1
= acpi_ut_get_resource_end_tag (obj_desc1
);
183 end_tag2
= acpi_ut_get_resource_end_tag (obj_desc2
);
184 if (!end_tag1
|| !end_tag2
) {
185 return_ACPI_STATUS (AE_AML_OPERAND_TYPE
);
188 /* Compute the length of each part */
190 length1
= ACPI_PTR_DIFF (end_tag1
, obj_desc1
->buffer
.pointer
);
191 length2
= ACPI_PTR_DIFF (end_tag2
, obj_desc2
->buffer
.pointer
) +
192 2; /* Size of END_TAG */
194 /* Create a new buffer object for the result */
196 return_desc
= acpi_ut_create_buffer_object (length1
+ length2
);
198 return_ACPI_STATUS (AE_NO_MEMORY
);
201 /* Copy the templates to the new descriptor */
203 new_buf
= return_desc
->buffer
.pointer
;
204 ACPI_MEMCPY (new_buf
, obj_desc1
->buffer
.pointer
, length1
);
205 ACPI_MEMCPY (new_buf
+ length1
, obj_desc2
->buffer
.pointer
, length2
);
207 /* Compute the new checksum */
209 new_buf
[return_desc
->buffer
.length
- 1] =
210 acpi_ut_generate_checksum (return_desc
->buffer
.pointer
,
211 (return_desc
->buffer
.length
- 1));
213 /* Return the completed template descriptor */
215 *actual_return_desc
= return_desc
;
216 return_ACPI_STATUS (AE_OK
);
220 /*******************************************************************************
222 * FUNCTION: acpi_ex_do_concatenate
224 * PARAMETERS: obj_desc1 - First source object
225 * obj_desc2 - 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 (
237 union acpi_operand_object
*obj_desc1
,
238 union acpi_operand_object
*obj_desc2
,
239 union acpi_operand_object
**actual_return_desc
,
240 struct acpi_walk_state
*walk_state
)
244 acpi_integer this_integer
;
245 union acpi_operand_object
*return_desc
;
249 ACPI_FUNCTION_ENTRY ();
253 * There are three cases to handle:
255 * 1) Two Integers concatenated to produce a new Buffer
256 * 2) Two Strings concatenated to produce a new String
257 * 3) Two Buffers concatenated to produce a new Buffer
259 switch (ACPI_GET_OBJECT_TYPE (obj_desc1
)) {
260 case ACPI_TYPE_INTEGER
:
262 /* Result of two Integers is a Buffer */
263 /* Need enough buffer space for two integers */
265 return_desc
= acpi_ut_create_buffer_object (acpi_gbl_integer_byte_width
* 2);
267 return (AE_NO_MEMORY
);
270 new_buf
= (char *) return_desc
->buffer
.pointer
;
272 /* Convert the first integer */
274 this_integer
= obj_desc1
->integer
.value
;
275 for (i
= 0; i
< acpi_gbl_integer_byte_width
; i
++) {
276 new_buf
[i
] = (char) this_integer
;
280 /* Convert the second integer */
282 this_integer
= obj_desc2
->integer
.value
;
283 for (; i
< (ACPI_MUL_2 (acpi_gbl_integer_byte_width
)); i
++) {
284 new_buf
[i
] = (char) this_integer
;
291 case ACPI_TYPE_STRING
:
293 /* Result of two Strings is a String */
295 return_desc
= acpi_ut_create_internal_object (ACPI_TYPE_STRING
);
297 return (AE_NO_MEMORY
);
300 /* Operand0 is string */
302 new_buf
= ACPI_MEM_CALLOCATE ((acpi_size
) obj_desc1
->string
.length
+
303 (acpi_size
) obj_desc2
->string
.length
+ 1);
306 (("ex_do_concatenate: String allocation failure\n"));
307 status
= AE_NO_MEMORY
;
311 /* Concatenate the strings */
313 ACPI_STRCPY (new_buf
, obj_desc1
->string
.pointer
);
314 ACPI_STRCPY (new_buf
+ obj_desc1
->string
.length
,
315 obj_desc2
->string
.pointer
);
317 /* Complete the String object initialization */
319 return_desc
->string
.pointer
= new_buf
;
320 return_desc
->string
.length
= obj_desc1
->string
.length
+
321 obj_desc2
->string
.length
;
325 case ACPI_TYPE_BUFFER
:
327 /* Result of two Buffers is a Buffer */
329 return_desc
= acpi_ut_create_buffer_object (
330 (acpi_size
) obj_desc1
->buffer
.length
+
331 (acpi_size
) obj_desc2
->buffer
.length
);
333 return (AE_NO_MEMORY
);
336 new_buf
= (char *) return_desc
->buffer
.pointer
;
338 /* Concatenate the buffers */
340 ACPI_MEMCPY (new_buf
, obj_desc1
->buffer
.pointer
,
341 obj_desc1
->buffer
.length
);
342 ACPI_MEMCPY (new_buf
+ obj_desc1
->buffer
.length
, obj_desc2
->buffer
.pointer
,
343 obj_desc2
->buffer
.length
);
350 /* Invalid object type, should not happen here */
352 ACPI_REPORT_ERROR (("Concat - invalid obj type: %X\n",
353 ACPI_GET_OBJECT_TYPE (obj_desc1
)));
354 status
= AE_AML_INTERNAL
;
358 *actual_return_desc
= return_desc
;
364 acpi_ut_remove_reference (return_desc
);
369 /*******************************************************************************
371 * FUNCTION: acpi_ex_do_math_op
373 * PARAMETERS: Opcode - AML opcode
374 * Operand0 - Integer operand #0
375 * Operand1 - Integer operand #1
377 * RETURN: Integer result of the operation
379 * DESCRIPTION: Execute a math AML opcode. The purpose of having all of the
380 * math functions here is to prevent a lot of pointer dereferencing
381 * to obtain the operands.
383 ******************************************************************************/
388 acpi_integer operand0
,
389 acpi_integer operand1
)
392 ACPI_FUNCTION_ENTRY ();
396 case AML_ADD_OP
: /* Add (Operand0, Operand1, Result) */
398 return (operand0
+ operand1
);
401 case AML_BIT_AND_OP
: /* And (Operand0, Operand1, Result) */
403 return (operand0
& operand1
);
406 case AML_BIT_NAND_OP
: /* NAnd (Operand0, Operand1, Result) */
408 return (~(operand0
& operand1
));
411 case AML_BIT_OR_OP
: /* Or (Operand0, Operand1, Result) */
413 return (operand0
| operand1
);
416 case AML_BIT_NOR_OP
: /* NOr (Operand0, Operand1, Result) */
418 return (~(operand0
| operand1
));
421 case AML_BIT_XOR_OP
: /* XOr (Operand0, Operand1, Result) */
423 return (operand0
^ operand1
);
426 case AML_MULTIPLY_OP
: /* Multiply (Operand0, Operand1, Result) */
428 return (operand0
* operand1
);
431 case AML_SHIFT_LEFT_OP
: /* shift_left (Operand, shift_count, Result) */
433 return (operand0
<< operand1
);
436 case AML_SHIFT_RIGHT_OP
: /* shift_right (Operand, shift_count, Result) */
438 return (operand0
>> operand1
);
441 case AML_SUBTRACT_OP
: /* Subtract (Operand0, Operand1, Result) */
443 return (operand0
- operand1
);
452 /*******************************************************************************
454 * FUNCTION: acpi_ex_do_logical_op
456 * PARAMETERS: Opcode - AML opcode
457 * obj_desc0 - operand #0
458 * obj_desc1 - operand #1
460 * RETURN: TRUE/FALSE result of the operation
462 * DESCRIPTION: Execute a logical AML opcode. The purpose of having all of the
463 * functions here is to prevent a lot of pointer dereferencing
464 * to obtain the operands and to simplify the generation of the
465 * logical value. Both operands must already be validated as
466 * 1) Both the same type, and
467 * 2) Either Integer, Buffer, or String type.
469 * Note: cleanest machine code seems to be produced by the code
470 * below, rather than using statements of the form:
471 * Result = (Operand0 == Operand1);
473 ******************************************************************************/
476 acpi_ex_do_logical_op (
478 union acpi_operand_object
*obj_desc0
,
479 union acpi_operand_object
*obj_desc1
)
481 acpi_integer operand0
;
482 acpi_integer operand1
;
490 ACPI_FUNCTION_ENTRY ();
493 if (ACPI_GET_OBJECT_TYPE (obj_desc0
) == ACPI_TYPE_INTEGER
) {
494 /* Both operands are of type integer */
496 operand0
= obj_desc0
->integer
.value
;
497 operand1
= obj_desc1
->integer
.value
;
500 case AML_LAND_OP
: /* LAnd (Operand0, Operand1) */
502 if (operand0
&& operand1
) {
507 case AML_LEQUAL_OP
: /* LEqual (Operand0, Operand1) */
509 if (operand0
== operand1
) {
514 case AML_LGREATER_OP
: /* LGreater (Operand0, Operand1) */
516 if (operand0
> operand1
) {
521 case AML_LLESS_OP
: /* LLess (Operand0, Operand1) */
523 if (operand0
< operand1
) {
528 case AML_LOR_OP
: /* LOr (Operand0, Operand1) */
530 if (operand0
|| operand1
) {
541 * Case for Buffer/String objects.
542 * NOTE: takes advantage of common Buffer/String object fields
544 length0
= obj_desc0
->buffer
.length
;
545 ptr0
= obj_desc0
->buffer
.pointer
;
547 length1
= obj_desc1
->buffer
.length
;
548 ptr1
= obj_desc1
->buffer
.pointer
;
551 case AML_LEQUAL_OP
: /* LEqual (Operand0, Operand1) */
553 /* Length and all bytes must be equal */
555 if (length0
!= length1
) {
559 for (i
= 0; i
< length0
; i
++) {
560 if (ptr0
[i
] != ptr1
[i
]) {
566 case AML_LGREATER_OP
: /* LGreater (Operand0, Operand1) */
568 /* Lexicographic compare: Scan the 1-to-1 data */
570 for (i
= 0; (i
< length0
) && (i
< length1
); i
++) {
571 if (ptr0
[i
] > ptr1
[i
]) {
576 /* Bytes match, now check lengths */
578 if (length0
> length1
) {
582 /* Length0 <= Length1 */
586 case AML_LLESS_OP
: /* LLess (Operand0, Operand1) */
588 /* Lexicographic compare: Scan the 1-to-1 data */
590 for (i
= 0; (i
< length0
) && (i
< length1
); i
++) {
591 if (ptr0
[i
] < ptr1
[i
]) {
596 /* Bytes match, now check lengths */
598 if (length0
< length1
) {
602 /* Length0 >= Length1 */