blob: 5ad34566738c2b14dae2a7e7f47f3ac9871d4619 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001
2/******************************************************************************
3 *
4 * Module Name: exmisc - ACPI AML (p-code) execution - specific opcodes
5 *
6 *****************************************************************************/
7
8/*
Bob Moore4a90c7e2006-01-13 16:22:00 -05009 * Copyright (C) 2000 - 2006, R. Byron Moore
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 * All rights reserved.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
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.
26 *
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.
30 *
31 * NO WARRANTY
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.
43 */
44
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <acpi/acpi.h>
46#include <acpi/acinterp.h>
47#include <acpi/amlcode.h>
Bob Moore96db2552005-11-02 00:00:00 -050048#include <acpi/amlresrc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
Linus Torvalds1da177e2005-04-16 15:20:36 -070050#define _COMPONENT ACPI_EXECUTER
Len Brown4be44fc2005-08-05 00:44:28 -040051ACPI_MODULE_NAME("exmisc")
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
53/*******************************************************************************
54 *
55 * FUNCTION: acpi_ex_get_object_reference
56 *
57 * PARAMETERS: obj_desc - Create a reference to this object
58 * return_desc - Where to store the reference
59 * walk_state - Current state
60 *
61 * RETURN: Status
62 *
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.
65 *
66 ******************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -070067acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -040068acpi_ex_get_object_reference(union acpi_operand_object *obj_desc,
69 union acpi_operand_object **return_desc,
70 struct acpi_walk_state *walk_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -070071{
Len Brown4be44fc2005-08-05 00:44:28 -040072 union acpi_operand_object *reference_obj;
73 union acpi_operand_object *referenced_obj;
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
Len Brown4be44fc2005-08-05 00:44:28 -040075 ACPI_FUNCTION_TRACE_PTR("ex_get_object_reference", obj_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
77 *return_desc = NULL;
78
Len Brown4be44fc2005-08-05 00:44:28 -040079 switch (ACPI_GET_DESCRIPTOR_TYPE(obj_desc)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 case ACPI_DESC_TYPE_OPERAND:
81
Len Brown4be44fc2005-08-05 00:44:28 -040082 if (ACPI_GET_OBJECT_TYPE(obj_desc) != ACPI_TYPE_LOCAL_REFERENCE) {
83 return_ACPI_STATUS(AE_AML_OPERAND_TYPE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 }
85
86 /*
87 * Must be a reference to a Local or Arg
88 */
89 switch (obj_desc->reference.opcode) {
90 case AML_LOCAL_OP:
91 case AML_ARG_OP:
92 case AML_DEBUG_OP:
93
94 /* The referenced object is the pseudo-node for the local/arg */
95
96 referenced_obj = obj_desc->reference.object;
97 break;
98
99 default:
100
Bob Moore4a90c7e2006-01-13 16:22:00 -0500101 ACPI_REPORT_ERROR(("Unknown Reference opcode %X\n",
102 obj_desc->reference.opcode));
Len Brown4be44fc2005-08-05 00:44:28 -0400103 return_ACPI_STATUS(AE_AML_INTERNAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 }
105 break;
106
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 case ACPI_DESC_TYPE_NAMED:
108
109 /*
110 * A named reference that has already been resolved to a Node
111 */
112 referenced_obj = obj_desc;
113 break;
114
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 default:
116
Bob Moore4a90c7e2006-01-13 16:22:00 -0500117 ACPI_REPORT_ERROR(("Invalid descriptor type %X\n",
118 ACPI_GET_DESCRIPTOR_TYPE(obj_desc)));
Len Brown4be44fc2005-08-05 00:44:28 -0400119 return_ACPI_STATUS(AE_TYPE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 }
121
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 /* Create a new reference object */
123
Len Brown4be44fc2005-08-05 00:44:28 -0400124 reference_obj =
125 acpi_ut_create_internal_object(ACPI_TYPE_LOCAL_REFERENCE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 if (!reference_obj) {
Len Brown4be44fc2005-08-05 00:44:28 -0400127 return_ACPI_STATUS(AE_NO_MEMORY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 }
129
130 reference_obj->reference.opcode = AML_REF_OF_OP;
131 reference_obj->reference.object = referenced_obj;
132 *return_desc = reference_obj;
133
Len Brown4be44fc2005-08-05 00:44:28 -0400134 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),
137 *return_desc));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
Len Brown4be44fc2005-08-05 00:44:28 -0400139 return_ACPI_STATUS(AE_OK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140}
141
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142/*******************************************************************************
143 *
144 * FUNCTION: acpi_ex_concat_template
145 *
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
150 *
151 * RETURN: Status
152 *
153 * DESCRIPTION: Concatenate two resource templates
154 *
155 ******************************************************************************/
156
157acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400158acpi_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)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162{
Bob Moore96db2552005-11-02 00:00:00 -0500163 acpi_status status;
Len Brown4be44fc2005-08-05 00:44:28 -0400164 union acpi_operand_object *return_desc;
165 u8 *new_buf;
Bob Moore96db2552005-11-02 00:00:00 -0500166 u8 *end_tag;
167 acpi_size length0;
Len Brown4be44fc2005-08-05 00:44:28 -0400168 acpi_size length1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
Len Brown4be44fc2005-08-05 00:44:28 -0400170 ACPI_FUNCTION_TRACE("ex_concat_template");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171
Bob Moore96db2552005-11-02 00:00:00 -0500172 /*
173 * Find the end_tag descriptor in each resource template.
174 * Note: returned pointers point TO the end_tag, not past it.
175 *
176 * Compute the length of each resource template
177 */
178 status = acpi_ut_get_resource_end_tag(operand0, &end_tag);
179 if (ACPI_FAILURE(status)) {
180 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 }
182
Bob Moore96db2552005-11-02 00:00:00 -0500183 length0 = ACPI_PTR_DIFF(end_tag, operand0->buffer.pointer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184
Bob Moore96db2552005-11-02 00:00:00 -0500185 status = acpi_ut_get_resource_end_tag(operand1, &end_tag);
186 if (ACPI_FAILURE(status)) {
187 return_ACPI_STATUS(status);
188 }
189
190 /* Include the end_tag in the second template length */
191
192 length1 = ACPI_PTR_DIFF(end_tag, operand1->buffer.pointer) +
193 sizeof(struct aml_resource_end_tag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194
195 /* Create a new buffer object for the result */
196
Bob Moore96db2552005-11-02 00:00:00 -0500197 return_desc = acpi_ut_create_buffer_object(length0 + length1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 if (!return_desc) {
Len Brown4be44fc2005-08-05 00:44:28 -0400199 return_ACPI_STATUS(AE_NO_MEMORY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 }
201
Bob Moore96db2552005-11-02 00:00:00 -0500202 /*
203 * Copy the templates to the new buffer, 0 first, then 1 follows. One
204 * end_tag descriptor is copied from Operand1.
205 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 new_buf = return_desc->buffer.pointer;
Bob Moore96db2552005-11-02 00:00:00 -0500207 ACPI_MEMCPY(new_buf, operand0->buffer.pointer, length0);
208 ACPI_MEMCPY(new_buf + length0, operand1->buffer.pointer, length1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
Bob Moorec51a4de2005-11-17 13:07:00 -0500210 /* Set the end_tag checksum to zero, means "ignore checksum" */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
Bob Moorec51a4de2005-11-17 13:07:00 -0500212 new_buf[return_desc->buffer.length - 1] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213
Bob Moore96db2552005-11-02 00:00:00 -0500214 /* Return the completed resource template */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215
216 *actual_return_desc = return_desc;
Len Brown4be44fc2005-08-05 00:44:28 -0400217 return_ACPI_STATUS(AE_OK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218}
219
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220/*******************************************************************************
221 *
222 * FUNCTION: acpi_ex_do_concatenate
223 *
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
228 *
229 * RETURN: Status
230 *
231 * DESCRIPTION: Concatenate two objects OF THE SAME TYPE.
232 *
233 ******************************************************************************/
234
235acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400236acpi_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)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240{
Len Brown4be44fc2005-08-05 00:44:28 -0400241 union acpi_operand_object *local_operand1 = operand1;
242 union acpi_operand_object *return_desc;
243 char *new_buf;
244 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245
Len Brown4be44fc2005-08-05 00:44:28 -0400246 ACPI_FUNCTION_TRACE("ex_do_concatenate");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247
248 /*
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.
254 */
Len Brown4be44fc2005-08-05 00:44:28 -0400255 switch (ACPI_GET_OBJECT_TYPE(operand0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 case ACPI_TYPE_INTEGER:
Len Brown4be44fc2005-08-05 00:44:28 -0400257 status =
258 acpi_ex_convert_to_integer(operand1, &local_operand1, 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 break;
260
261 case ACPI_TYPE_STRING:
Len Brown4be44fc2005-08-05 00:44:28 -0400262 status = acpi_ex_convert_to_string(operand1, &local_operand1,
263 ACPI_IMPLICIT_CONVERT_HEX);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 break;
265
266 case ACPI_TYPE_BUFFER:
Len Brown4be44fc2005-08-05 00:44:28 -0400267 status = acpi_ex_convert_to_buffer(operand1, &local_operand1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 break;
269
270 default:
Bob Moore4a90c7e2006-01-13 16:22:00 -0500271 ACPI_REPORT_ERROR(("Invalid object type: %X\n",
Len Brown4be44fc2005-08-05 00:44:28 -0400272 ACPI_GET_OBJECT_TYPE(operand0)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 status = AE_AML_INTERNAL;
274 }
275
Len Brown4be44fc2005-08-05 00:44:28 -0400276 if (ACPI_FAILURE(status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 goto cleanup;
278 }
279
280 /*
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
283 * concatenation.
284 */
285
286 /*
287 * There are three cases to handle:
288 *
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
292 */
Len Brown4be44fc2005-08-05 00:44:28 -0400293 switch (ACPI_GET_OBJECT_TYPE(operand0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 case ACPI_TYPE_INTEGER:
295
296 /* Result of two Integers is a Buffer */
297 /* Need enough buffer space for two integers */
298
Len Brown4be44fc2005-08-05 00:44:28 -0400299 return_desc = acpi_ut_create_buffer_object((acpi_size)
300 ACPI_MUL_2
301 (acpi_gbl_integer_byte_width));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 if (!return_desc) {
303 status = AE_NO_MEMORY;
304 goto cleanup;
305 }
306
Len Brown4be44fc2005-08-05 00:44:28 -0400307 new_buf = (char *)return_desc->buffer.pointer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308
309 /* Copy the first integer, LSB first */
310
Bob Moorec51a4de2005-11-17 13:07:00 -0500311 ACPI_MEMCPY(new_buf, &operand0->integer.value,
Len Brown4be44fc2005-08-05 00:44:28 -0400312 acpi_gbl_integer_byte_width);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313
314 /* Copy the second integer (LSB first) after the first */
315
Len Brown4be44fc2005-08-05 00:44:28 -0400316 ACPI_MEMCPY(new_buf + acpi_gbl_integer_byte_width,
317 &local_operand1->integer.value,
318 acpi_gbl_integer_byte_width);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 break;
320
321 case ACPI_TYPE_STRING:
322
323 /* Result of two Strings is a String */
324
Bob Moorec51a4de2005-11-17 13:07:00 -0500325 return_desc = acpi_ut_create_string_object((acpi_size)
326 (operand0->string.
327 length +
328 local_operand1->
329 string.length));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 if (!return_desc) {
331 status = AE_NO_MEMORY;
332 goto cleanup;
333 }
334
335 new_buf = return_desc->string.pointer;
336
337 /* Concatenate the strings */
338
Len Brown4be44fc2005-08-05 00:44:28 -0400339 ACPI_STRCPY(new_buf, operand0->string.pointer);
340 ACPI_STRCPY(new_buf + operand0->string.length,
341 local_operand1->string.pointer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 break;
343
344 case ACPI_TYPE_BUFFER:
345
346 /* Result of two Buffers is a Buffer */
347
Len Brown4be44fc2005-08-05 00:44:28 -0400348 return_desc = acpi_ut_create_buffer_object((acpi_size)
Bob Moorec51a4de2005-11-17 13:07:00 -0500349 (operand0->buffer.
350 length +
351 local_operand1->
352 buffer.length));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 if (!return_desc) {
354 status = AE_NO_MEMORY;
355 goto cleanup;
356 }
357
Len Brown4be44fc2005-08-05 00:44:28 -0400358 new_buf = (char *)return_desc->buffer.pointer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359
360 /* Concatenate the buffers */
361
Bob Moorec51a4de2005-11-17 13:07:00 -0500362 ACPI_MEMCPY(new_buf, operand0->buffer.pointer,
363 operand0->buffer.length);
Len Brown4be44fc2005-08-05 00:44:28 -0400364 ACPI_MEMCPY(new_buf + operand0->buffer.length,
365 local_operand1->buffer.pointer,
366 local_operand1->buffer.length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 break;
368
369 default:
370
371 /* Invalid object type, should not happen here */
372
Bob Moore4a90c7e2006-01-13 16:22:00 -0500373 ACPI_REPORT_ERROR(("Invalid object type: %X\n",
Len Brown4be44fc2005-08-05 00:44:28 -0400374 ACPI_GET_OBJECT_TYPE(operand0)));
375 status = AE_AML_INTERNAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 goto cleanup;
377 }
378
379 *actual_return_desc = return_desc;
380
Len Brown4be44fc2005-08-05 00:44:28 -0400381 cleanup:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 if (local_operand1 != operand1) {
Len Brown4be44fc2005-08-05 00:44:28 -0400383 acpi_ut_remove_reference(local_operand1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 }
Len Brown4be44fc2005-08-05 00:44:28 -0400385 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386}
387
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388/*******************************************************************************
389 *
390 * FUNCTION: acpi_ex_do_math_op
391 *
392 * PARAMETERS: Opcode - AML opcode
393 * Integer0 - Integer operand #0
394 * Integer1 - Integer operand #1
395 *
396 * RETURN: Integer result of the operation
397 *
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.
401 *
402 ******************************************************************************/
403
404acpi_integer
Len Brown4be44fc2005-08-05 00:44:28 -0400405acpi_ex_do_math_op(u16 opcode, acpi_integer integer0, acpi_integer integer1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406{
407
Len Brown4be44fc2005-08-05 00:44:28 -0400408 ACPI_FUNCTION_ENTRY();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409
410 switch (opcode) {
Len Brown4be44fc2005-08-05 00:44:28 -0400411 case AML_ADD_OP: /* Add (Integer0, Integer1, Result) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412
413 return (integer0 + integer1);
414
Len Brown4be44fc2005-08-05 00:44:28 -0400415 case AML_BIT_AND_OP: /* And (Integer0, Integer1, Result) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416
417 return (integer0 & integer1);
418
Len Brown4be44fc2005-08-05 00:44:28 -0400419 case AML_BIT_NAND_OP: /* NAnd (Integer0, Integer1, Result) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420
421 return (~(integer0 & integer1));
422
Len Brown4be44fc2005-08-05 00:44:28 -0400423 case AML_BIT_OR_OP: /* Or (Integer0, Integer1, Result) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424
425 return (integer0 | integer1);
426
Len Brown4be44fc2005-08-05 00:44:28 -0400427 case AML_BIT_NOR_OP: /* NOr (Integer0, Integer1, Result) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428
429 return (~(integer0 | integer1));
430
Len Brown4be44fc2005-08-05 00:44:28 -0400431 case AML_BIT_XOR_OP: /* XOr (Integer0, Integer1, Result) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432
433 return (integer0 ^ integer1);
434
Len Brown4be44fc2005-08-05 00:44:28 -0400435 case AML_MULTIPLY_OP: /* Multiply (Integer0, Integer1, Result) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436
437 return (integer0 * integer1);
438
Len Brown4be44fc2005-08-05 00:44:28 -0400439 case AML_SHIFT_LEFT_OP: /* shift_left (Operand, shift_count, Result) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440
441 return (integer0 << integer1);
442
Len Brown4be44fc2005-08-05 00:44:28 -0400443 case AML_SHIFT_RIGHT_OP: /* shift_right (Operand, shift_count, Result) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444
445 return (integer0 >> integer1);
446
Len Brown4be44fc2005-08-05 00:44:28 -0400447 case AML_SUBTRACT_OP: /* Subtract (Integer0, Integer1, Result) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448
449 return (integer0 - integer1);
450
451 default:
452
453 return (0);
454 }
455}
456
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457/*******************************************************************************
458 *
459 * FUNCTION: acpi_ex_do_logical_numeric_op
460 *
461 * PARAMETERS: Opcode - AML opcode
462 * Integer0 - Integer operand #0
463 * Integer1 - Integer operand #1
464 * logical_result - TRUE/FALSE result of the operation
465 *
466 * RETURN: Status
467 *
468 * DESCRIPTION: Execute a logical "Numeric" AML opcode. For these Numeric
469 * operators (LAnd and LOr), both operands must be integers.
470 *
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);
474 *
475 ******************************************************************************/
476
477acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400478acpi_ex_do_logical_numeric_op(u16 opcode,
479 acpi_integer integer0,
480 acpi_integer integer1, u8 * logical_result)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481{
Len Brown4be44fc2005-08-05 00:44:28 -0400482 acpi_status status = AE_OK;
483 u8 local_result = FALSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484
Len Brown4be44fc2005-08-05 00:44:28 -0400485 ACPI_FUNCTION_TRACE("ex_do_logical_numeric_op");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486
487 switch (opcode) {
Len Brown4be44fc2005-08-05 00:44:28 -0400488 case AML_LAND_OP: /* LAnd (Integer0, Integer1) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489
490 if (integer0 && integer1) {
491 local_result = TRUE;
492 }
493 break;
494
Len Brown4be44fc2005-08-05 00:44:28 -0400495 case AML_LOR_OP: /* LOr (Integer0, Integer1) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496
497 if (integer0 || integer1) {
498 local_result = TRUE;
499 }
500 break;
501
502 default:
503 status = AE_AML_INTERNAL;
504 break;
505 }
506
507 /* Return the logical result and status */
508
509 *logical_result = local_result;
Len Brown4be44fc2005-08-05 00:44:28 -0400510 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511}
512
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513/*******************************************************************************
514 *
515 * FUNCTION: acpi_ex_do_logical_op
516 *
517 * PARAMETERS: Opcode - AML opcode
518 * Operand0 - operand #0
519 * Operand1 - operand #1
520 * logical_result - TRUE/FALSE result of the operation
521 *
522 * RETURN: Status
523 *
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
531 * will be converted.
532 *
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);
536 *
537 ******************************************************************************/
538
539acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400540acpi_ex_do_logical_op(u16 opcode,
541 union acpi_operand_object *operand0,
542 union acpi_operand_object *operand1, u8 * logical_result)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543{
Len Brown4be44fc2005-08-05 00:44:28 -0400544 union acpi_operand_object *local_operand1 = operand1;
545 acpi_integer integer0;
546 acpi_integer integer1;
547 u32 length0;
548 u32 length1;
549 acpi_status status = AE_OK;
550 u8 local_result = FALSE;
551 int compare;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552
Len Brown4be44fc2005-08-05 00:44:28 -0400553 ACPI_FUNCTION_TRACE("ex_do_logical_op");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554
555 /*
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.
561 */
Len Brown4be44fc2005-08-05 00:44:28 -0400562 switch (ACPI_GET_OBJECT_TYPE(operand0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 case ACPI_TYPE_INTEGER:
Len Brown4be44fc2005-08-05 00:44:28 -0400564 status =
565 acpi_ex_convert_to_integer(operand1, &local_operand1, 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 break;
567
568 case ACPI_TYPE_STRING:
Len Brown4be44fc2005-08-05 00:44:28 -0400569 status = acpi_ex_convert_to_string(operand1, &local_operand1,
570 ACPI_IMPLICIT_CONVERT_HEX);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 break;
572
573 case ACPI_TYPE_BUFFER:
Len Brown4be44fc2005-08-05 00:44:28 -0400574 status = acpi_ex_convert_to_buffer(operand1, &local_operand1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 break;
576
577 default:
578 status = AE_AML_INTERNAL;
579 break;
580 }
581
Len Brown4be44fc2005-08-05 00:44:28 -0400582 if (ACPI_FAILURE(status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 goto cleanup;
584 }
585
586 /*
587 * Two cases: 1) Both Integers, 2) Both Strings or Buffers
588 */
Len Brown4be44fc2005-08-05 00:44:28 -0400589 if (ACPI_GET_OBJECT_TYPE(operand0) == ACPI_TYPE_INTEGER) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 /*
591 * 1) Both operands are of type integer
592 * Note: local_operand1 may have changed above
593 */
594 integer0 = operand0->integer.value;
595 integer1 = local_operand1->integer.value;
596
597 switch (opcode) {
Len Brown4be44fc2005-08-05 00:44:28 -0400598 case AML_LEQUAL_OP: /* LEqual (Operand0, Operand1) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599
600 if (integer0 == integer1) {
601 local_result = TRUE;
602 }
603 break;
604
Len Brown4be44fc2005-08-05 00:44:28 -0400605 case AML_LGREATER_OP: /* LGreater (Operand0, Operand1) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606
607 if (integer0 > integer1) {
608 local_result = TRUE;
609 }
610 break;
611
Len Brown4be44fc2005-08-05 00:44:28 -0400612 case AML_LLESS_OP: /* LLess (Operand0, Operand1) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613
614 if (integer0 < integer1) {
615 local_result = TRUE;
616 }
617 break;
618
619 default:
620 status = AE_AML_INTERNAL;
621 break;
622 }
Len Brown4be44fc2005-08-05 00:44:28 -0400623 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 /*
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.
629 */
630 length0 = operand0->buffer.length;
631 length1 = local_operand1->buffer.length;
632
633 /* Lexicographic compare: compare the data bytes */
634
Bob Moore08978312005-10-21 00:00:00 -0400635 compare = ACPI_MEMCMP(operand0->buffer.pointer,
636 local_operand1->buffer.pointer,
Len Brown4be44fc2005-08-05 00:44:28 -0400637 (length0 > length1) ? length1 : length0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638
639 switch (opcode) {
Len Brown4be44fc2005-08-05 00:44:28 -0400640 case AML_LEQUAL_OP: /* LEqual (Operand0, Operand1) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641
642 /* Length and all bytes must be equal */
643
Len Brown4be44fc2005-08-05 00:44:28 -0400644 if ((length0 == length1) && (compare == 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 /* Length and all bytes match ==> TRUE */
646
647 local_result = TRUE;
648 }
649 break;
650
Len Brown4be44fc2005-08-05 00:44:28 -0400651 case AML_LGREATER_OP: /* LGreater (Operand0, Operand1) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652
653 if (compare > 0) {
654 local_result = TRUE;
Len Brown4be44fc2005-08-05 00:44:28 -0400655 goto cleanup; /* TRUE */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 }
657 if (compare < 0) {
Len Brown4be44fc2005-08-05 00:44:28 -0400658 goto cleanup; /* FALSE */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 }
660
661 /* Bytes match (to shortest length), compare lengths */
662
663 if (length0 > length1) {
664 local_result = TRUE;
665 }
666 break;
667
Len Brown4be44fc2005-08-05 00:44:28 -0400668 case AML_LLESS_OP: /* LLess (Operand0, Operand1) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669
670 if (compare > 0) {
Len Brown4be44fc2005-08-05 00:44:28 -0400671 goto cleanup; /* FALSE */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 }
673 if (compare < 0) {
674 local_result = TRUE;
Len Brown4be44fc2005-08-05 00:44:28 -0400675 goto cleanup; /* TRUE */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 }
677
678 /* Bytes match (to shortest length), compare lengths */
679
680 if (length0 < length1) {
681 local_result = TRUE;
682 }
683 break;
684
685 default:
686 status = AE_AML_INTERNAL;
687 break;
688 }
689 }
690
Len Brown4be44fc2005-08-05 00:44:28 -0400691 cleanup:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692
693 /* New object was created if implicit conversion performed - delete */
694
695 if (local_operand1 != operand1) {
Len Brown4be44fc2005-08-05 00:44:28 -0400696 acpi_ut_remove_reference(local_operand1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 }
698
699 /* Return the logical result and status */
700
701 *logical_result = local_result;
Len Brown4be44fc2005-08-05 00:44:28 -0400702 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703}