2 /******************************************************************************
4 * Module Name: exmisc - ACPI AML (p-code) execution - specific opcodes
6 *****************************************************************************/
9 * Copyright (C) 2000 - 2005, 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 in get_reference %X\n", obj_desc->reference.opcode));
102 return_ACPI_STATUS(AE_AML_INTERNAL);
106 case ACPI_DESC_TYPE_NAMED:
109 * A named reference that has already been resolved to a Node
111 referenced_obj = obj_desc;
116 ACPI_REPORT_ERROR(("Invalid descriptor type in get_reference: %X\n", ACPI_GET_DESCRIPTOR_TYPE(obj_desc)));
117 return_ACPI_STATUS(AE_TYPE);
120 /* Create a new reference object */
123 acpi_ut_create_internal_object(ACPI_TYPE_LOCAL_REFERENCE);
124 if (!reference_obj) {
125 return_ACPI_STATUS(AE_NO_MEMORY);
128 reference_obj->reference.opcode = AML_REF_OF_OP;
129 reference_obj->reference.object = referenced_obj;
130 *return_desc = reference_obj;
132 ACPI_DEBUG_PRINT((ACPI_DB_EXEC,
133 "Object %p Type [%s], returning Reference %p\n",
134 obj_desc, acpi_ut_get_object_type_name(obj_desc),
137 return_ACPI_STATUS(AE_OK);
140 /*******************************************************************************
142 * FUNCTION: acpi_ex_concat_template
144 * PARAMETERS: Operand0 - First source object
145 * Operand1 - Second source object
146 * actual_return_desc - Where to place the return object
147 * walk_state - Current walk state
151 * DESCRIPTION: Concatenate two resource templates
153 ******************************************************************************/
156 acpi_ex_concat_template(union acpi_operand_object *operand0,
157 union acpi_operand_object *operand1,
158 union acpi_operand_object **actual_return_desc,
159 struct acpi_walk_state *walk_state)
162 union acpi_operand_object *return_desc;
168 ACPI_FUNCTION_TRACE("ex_concat_template");
171 * Find the end_tag descriptor in each resource template.
172 * Note: returned pointers point TO the end_tag, not past it.
174 * Compute the length of each resource template
176 status = acpi_ut_get_resource_end_tag(operand0, &end_tag);
177 if (ACPI_FAILURE(status)) {
178 return_ACPI_STATUS(status);
181 length0 = ACPI_PTR_DIFF(end_tag, operand0->buffer.pointer);
183 status = acpi_ut_get_resource_end_tag(operand1, &end_tag);
184 if (ACPI_FAILURE(status)) {
185 return_ACPI_STATUS(status);
188 /* Include the end_tag in the second template length */
190 length1 = ACPI_PTR_DIFF(end_tag, operand1->buffer.pointer) +
191 sizeof(struct aml_resource_end_tag);
193 /* Create a new buffer object for the result */
195 return_desc = acpi_ut_create_buffer_object(length0 + length1);
197 return_ACPI_STATUS(AE_NO_MEMORY);
201 * Copy the templates to the new buffer, 0 first, then 1 follows. One
202 * end_tag descriptor is copied from Operand1.
204 new_buf = return_desc->buffer.pointer;
205 ACPI_MEMCPY(new_buf, operand0->buffer.pointer, length0);
206 ACPI_MEMCPY(new_buf + length0, operand1->buffer.pointer, length1);
208 /* Set the end_tag checksum to zero, means "ignore checksum" */
210 new_buf[return_desc->buffer.length - 1] = 0;
212 /* Return the completed resource template */
214 *actual_return_desc = return_desc;
215 return_ACPI_STATUS(AE_OK);
218 /*******************************************************************************
220 * FUNCTION: acpi_ex_do_concatenate
222 * PARAMETERS: Operand0 - First source object
223 * Operand1 - Second source object
224 * actual_return_desc - Where to place the return object
225 * walk_state - Current walk state
229 * DESCRIPTION: Concatenate two objects OF THE SAME TYPE.
231 ******************************************************************************/
234 acpi_ex_do_concatenate(union acpi_operand_object *operand0,
235 union acpi_operand_object *operand1,
236 union acpi_operand_object **actual_return_desc,
237 struct acpi_walk_state *walk_state)
239 union acpi_operand_object *local_operand1 = operand1;
240 union acpi_operand_object *return_desc;
244 ACPI_FUNCTION_TRACE("ex_do_concatenate");
247 * Convert the second operand if necessary. The first operand
248 * determines the type of the second operand, (See the Data Types
249 * section of the ACPI specification.) Both object types are
250 * guaranteed to be either Integer/String/Buffer by the operand
251 * resolution mechanism.
253 switch (ACPI_GET_OBJECT_TYPE(operand0)) {
254 case ACPI_TYPE_INTEGER:
256 acpi_ex_convert_to_integer(operand1, &local_operand1, 16);
259 case ACPI_TYPE_STRING:
260 status = acpi_ex_convert_to_string(operand1, &local_operand1,
261 ACPI_IMPLICIT_CONVERT_HEX);
264 case ACPI_TYPE_BUFFER:
265 status = acpi_ex_convert_to_buffer(operand1, &local_operand1);
269 ACPI_REPORT_ERROR(("Concatanate - invalid object type: %X\n",
270 ACPI_GET_OBJECT_TYPE(operand0)));
271 status = AE_AML_INTERNAL;
274 if (ACPI_FAILURE(status)) {
279 * Both operands are now known to be the same object type
280 * (Both are Integer, String, or Buffer), and we can now perform the
285 * There are three cases to handle:
287 * 1) Two Integers concatenated to produce a new Buffer
288 * 2) Two Strings concatenated to produce a new String
289 * 3) Two Buffers concatenated to produce a new Buffer
291 switch (ACPI_GET_OBJECT_TYPE(operand0)) {
292 case ACPI_TYPE_INTEGER:
294 /* Result of two Integers is a Buffer */
295 /* Need enough buffer space for two integers */
297 return_desc = acpi_ut_create_buffer_object((acpi_size)
299 (acpi_gbl_integer_byte_width));
301 status = AE_NO_MEMORY;
305 new_buf = (char *)return_desc->buffer.pointer;
307 /* Copy the first integer, LSB first */
309 ACPI_MEMCPY(new_buf, &operand0->integer.value,
310 acpi_gbl_integer_byte_width);
312 /* Copy the second integer (LSB first) after the first */
314 ACPI_MEMCPY(new_buf + acpi_gbl_integer_byte_width,
315 &local_operand1->integer.value,
316 acpi_gbl_integer_byte_width);
319 case ACPI_TYPE_STRING:
321 /* Result of two Strings is a String */
323 return_desc = acpi_ut_create_string_object((acpi_size)
329 status = AE_NO_MEMORY;
333 new_buf = return_desc->string.pointer;
335 /* Concatenate the strings */
337 ACPI_STRCPY(new_buf, operand0->string.pointer);
338 ACPI_STRCPY(new_buf + operand0->string.length,
339 local_operand1->string.pointer);
342 case ACPI_TYPE_BUFFER:
344 /* Result of two Buffers is a Buffer */
346 return_desc = acpi_ut_create_buffer_object((acpi_size)
352 status = AE_NO_MEMORY;
356 new_buf = (char *)return_desc->buffer.pointer;
358 /* Concatenate the buffers */
360 ACPI_MEMCPY(new_buf, operand0->buffer.pointer,
361 operand0->buffer.length);
362 ACPI_MEMCPY(new_buf + operand0->buffer.length,
363 local_operand1->buffer.pointer,
364 local_operand1->buffer.length);
369 /* Invalid object type, should not happen here */
371 ACPI_REPORT_ERROR(("Concatenate - Invalid object type: %X\n",
372 ACPI_GET_OBJECT_TYPE(operand0)));
373 status = AE_AML_INTERNAL;
377 *actual_return_desc = return_desc;
380 if (local_operand1 != operand1) {
381 acpi_ut_remove_reference(local_operand1);
383 return_ACPI_STATUS(status);
386 /*******************************************************************************
388 * FUNCTION: acpi_ex_do_math_op
390 * PARAMETERS: Opcode - AML opcode
391 * Integer0 - Integer operand #0
392 * Integer1 - Integer operand #1
394 * RETURN: Integer result of the operation
396 * DESCRIPTION: Execute a math AML opcode. The purpose of having all of the
397 * math functions here is to prevent a lot of pointer dereferencing
398 * to obtain the operands.
400 ******************************************************************************/
403 acpi_ex_do_math_op(u16 opcode, acpi_integer integer0, acpi_integer integer1)
406 ACPI_FUNCTION_ENTRY();
409 case AML_ADD_OP: /* Add (Integer0, Integer1, Result) */
411 return (integer0 + integer1);
413 case AML_BIT_AND_OP: /* And (Integer0, Integer1, Result) */
415 return (integer0 & integer1);
417 case AML_BIT_NAND_OP: /* NAnd (Integer0, Integer1, Result) */
419 return (~(integer0 & integer1));
421 case AML_BIT_OR_OP: /* Or (Integer0, Integer1, Result) */
423 return (integer0 | integer1);
425 case AML_BIT_NOR_OP: /* NOr (Integer0, Integer1, Result) */
427 return (~(integer0 | integer1));
429 case AML_BIT_XOR_OP: /* XOr (Integer0, Integer1, Result) */
431 return (integer0 ^ integer1);
433 case AML_MULTIPLY_OP: /* Multiply (Integer0, Integer1, Result) */
435 return (integer0 * integer1);
437 case AML_SHIFT_LEFT_OP: /* shift_left (Operand, shift_count, Result) */
439 return (integer0 << integer1);
441 case AML_SHIFT_RIGHT_OP: /* shift_right (Operand, shift_count, Result) */
443 return (integer0 >> integer1);
445 case AML_SUBTRACT_OP: /* Subtract (Integer0, Integer1, Result) */
447 return (integer0 - integer1);
455 /*******************************************************************************
457 * FUNCTION: acpi_ex_do_logical_numeric_op
459 * PARAMETERS: Opcode - AML opcode
460 * Integer0 - Integer operand #0
461 * Integer1 - Integer operand #1
462 * logical_result - TRUE/FALSE result of the operation
466 * DESCRIPTION: Execute a logical "Numeric" AML opcode. For these Numeric
467 * operators (LAnd and LOr), both operands must be integers.
469 * Note: cleanest machine code seems to be produced by the code
470 * below, rather than using statements of the form:
471 * Result = (Integer0 && Integer1);
473 ******************************************************************************/
476 acpi_ex_do_logical_numeric_op(u16 opcode,
477 acpi_integer integer0,
478 acpi_integer integer1, u8 * logical_result)
480 acpi_status status = AE_OK;
481 u8 local_result = FALSE;
483 ACPI_FUNCTION_TRACE("ex_do_logical_numeric_op");
486 case AML_LAND_OP: /* LAnd (Integer0, Integer1) */
488 if (integer0 && integer1) {
493 case AML_LOR_OP: /* LOr (Integer0, Integer1) */
495 if (integer0 || integer1) {
501 status = AE_AML_INTERNAL;
505 /* Return the logical result and status */
507 *logical_result = local_result;
508 return_ACPI_STATUS(status);
511 /*******************************************************************************
513 * FUNCTION: acpi_ex_do_logical_op
515 * PARAMETERS: Opcode - AML opcode
516 * Operand0 - operand #0
517 * Operand1 - operand #1
518 * logical_result - TRUE/FALSE result of the operation
522 * DESCRIPTION: Execute a logical AML opcode. The purpose of having all of the
523 * functions here is to prevent a lot of pointer dereferencing
524 * to obtain the operands and to simplify the generation of the
525 * logical value. For the Numeric operators (LAnd and LOr), both
526 * operands must be integers. For the other logical operators,
527 * operands can be any combination of Integer/String/Buffer. The
528 * first operand determines the type to which the second operand
531 * Note: cleanest machine code seems to be produced by the code
532 * below, rather than using statements of the form:
533 * Result = (Operand0 == Operand1);
535 ******************************************************************************/
538 acpi_ex_do_logical_op(u16 opcode,
539 union acpi_operand_object *operand0,
540 union acpi_operand_object *operand1, u8 * logical_result)
542 union acpi_operand_object *local_operand1 = operand1;
543 acpi_integer integer0;
544 acpi_integer integer1;
547 acpi_status status = AE_OK;
548 u8 local_result = FALSE;
551 ACPI_FUNCTION_TRACE("ex_do_logical_op");
554 * Convert the second operand if necessary. The first operand
555 * determines the type of the second operand, (See the Data Types
556 * section of the ACPI 3.0+ specification.) Both object types are
557 * guaranteed to be either Integer/String/Buffer by the operand
558 * resolution mechanism.
560 switch (ACPI_GET_OBJECT_TYPE(operand0)) {
561 case ACPI_TYPE_INTEGER:
563 acpi_ex_convert_to_integer(operand1, &local_operand1, 16);
566 case ACPI_TYPE_STRING:
567 status = acpi_ex_convert_to_string(operand1, &local_operand1,
568 ACPI_IMPLICIT_CONVERT_HEX);
571 case ACPI_TYPE_BUFFER:
572 status = acpi_ex_convert_to_buffer(operand1, &local_operand1);
576 status = AE_AML_INTERNAL;
580 if (ACPI_FAILURE(status)) {
585 * Two cases: 1) Both Integers, 2) Both Strings or Buffers
587 if (ACPI_GET_OBJECT_TYPE(operand0) == ACPI_TYPE_INTEGER) {
589 * 1) Both operands are of type integer
590 * Note: local_operand1 may have changed above
592 integer0 = operand0->integer.value;
593 integer1 = local_operand1->integer.value;
596 case AML_LEQUAL_OP: /* LEqual (Operand0, Operand1) */
598 if (integer0 == integer1) {
603 case AML_LGREATER_OP: /* LGreater (Operand0, Operand1) */
605 if (integer0 > integer1) {
610 case AML_LLESS_OP: /* LLess (Operand0, Operand1) */
612 if (integer0 < integer1) {
618 status = AE_AML_INTERNAL;
623 * 2) Both operands are Strings or both are Buffers
624 * Note: Code below takes advantage of common Buffer/String
625 * object fields. local_operand1 may have changed above. Use
626 * memcmp to handle nulls in buffers.
628 length0 = operand0->buffer.length;
629 length1 = local_operand1->buffer.length;
631 /* Lexicographic compare: compare the data bytes */
633 compare = ACPI_MEMCMP(operand0->buffer.pointer,
634 local_operand1->buffer.pointer,
635 (length0 > length1) ? length1 : length0);
638 case AML_LEQUAL_OP: /* LEqual (Operand0, Operand1) */
640 /* Length and all bytes must be equal */
642 if ((length0 == length1) && (compare == 0)) {
643 /* Length and all bytes match ==> TRUE */
649 case AML_LGREATER_OP: /* LGreater (Operand0, Operand1) */
653 goto cleanup; /* TRUE */
656 goto cleanup; /* FALSE */
659 /* Bytes match (to shortest length), compare lengths */
661 if (length0 > length1) {
666 case AML_LLESS_OP: /* LLess (Operand0, Operand1) */
669 goto cleanup; /* FALSE */
673 goto cleanup; /* TRUE */
676 /* Bytes match (to shortest length), compare lengths */
678 if (length0 < length1) {
684 status = AE_AML_INTERNAL;
691 /* New object was created if implicit conversion performed - delete */
693 if (local_operand1 != operand1) {
694 acpi_ut_remove_reference(local_operand1);
697 /* Return the logical result and status */
699 *logical_result = local_result;
700 return_ACPI_STATUS(status);