exmisc.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  1. /******************************************************************************
  2. *
  3. * Module Name: exmisc - ACPI AML (p-code) execution - specific opcodes
  4. *
  5. *****************************************************************************/
  6. /*
  7. * Copyright (C) 2000 - 2016, Intel Corp.
  8. * All rights reserved.
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions
  12. * are met:
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions, and the following disclaimer,
  15. * without modification.
  16. * 2. Redistributions in binary form must reproduce at minimum a disclaimer
  17. * substantially similar to the "NO WARRANTY" disclaimer below
  18. * ("Disclaimer") and any redistribution must be conditioned upon
  19. * including a substantially similar Disclaimer requirement for further
  20. * binary redistribution.
  21. * 3. Neither the names of the above-listed copyright holders nor the names
  22. * of any contributors may be used to endorse or promote products derived
  23. * from this software without specific prior written permission.
  24. *
  25. * Alternatively, this software may be distributed under the terms of the
  26. * GNU General Public License ("GPL") version 2 as published by the Free
  27. * Software Foundation.
  28. *
  29. * NO WARRANTY
  30. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  31. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  32. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
  33. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  34. * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  35. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  36. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  37. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  38. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  39. * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  40. * POSSIBILITY OF SUCH DAMAGES.
  41. */
  42. #include <acpi/acpi.h>
  43. #include "accommon.h"
  44. #include "acinterp.h"
  45. #include "amlcode.h"
  46. #define _COMPONENT ACPI_EXECUTER
  47. ACPI_MODULE_NAME("exmisc")
  48. /*******************************************************************************
  49. *
  50. * FUNCTION: acpi_ex_get_object_reference
  51. *
  52. * PARAMETERS: obj_desc - Create a reference to this object
  53. * return_desc - Where to store the reference
  54. * walk_state - Current state
  55. *
  56. * RETURN: Status
  57. *
  58. * DESCRIPTION: Obtain and return a "reference" to the target object
  59. * Common code for the ref_of_op and the cond_ref_of_op.
  60. *
  61. ******************************************************************************/
  62. acpi_status
  63. acpi_ex_get_object_reference(union acpi_operand_object *obj_desc,
  64. union acpi_operand_object **return_desc,
  65. struct acpi_walk_state *walk_state)
  66. {
  67. union acpi_operand_object *reference_obj;
  68. union acpi_operand_object *referenced_obj;
  69. ACPI_FUNCTION_TRACE_PTR(ex_get_object_reference, obj_desc);
  70. *return_desc = NULL;
  71. switch (ACPI_GET_DESCRIPTOR_TYPE(obj_desc)) {
  72. case ACPI_DESC_TYPE_OPERAND:
  73. if (obj_desc->common.type != ACPI_TYPE_LOCAL_REFERENCE) {
  74. return_ACPI_STATUS(AE_AML_OPERAND_TYPE);
  75. }
  76. /*
  77. * Must be a reference to a Local or Arg
  78. */
  79. switch (obj_desc->reference.class) {
  80. case ACPI_REFCLASS_LOCAL:
  81. case ACPI_REFCLASS_ARG:
  82. case ACPI_REFCLASS_DEBUG:
  83. /* The referenced object is the pseudo-node for the local/arg */
  84. referenced_obj = obj_desc->reference.object;
  85. break;
  86. default:
  87. ACPI_ERROR((AE_INFO, "Invalid Reference Class 0x%2.2X",
  88. obj_desc->reference.class));
  89. return_ACPI_STATUS(AE_AML_OPERAND_TYPE);
  90. }
  91. break;
  92. case ACPI_DESC_TYPE_NAMED:
  93. /*
  94. * A named reference that has already been resolved to a Node
  95. */
  96. referenced_obj = obj_desc;
  97. break;
  98. default:
  99. ACPI_ERROR((AE_INFO, "Invalid descriptor type 0x%X",
  100. ACPI_GET_DESCRIPTOR_TYPE(obj_desc)));
  101. return_ACPI_STATUS(AE_TYPE);
  102. }
  103. /* Create a new reference object */
  104. reference_obj =
  105. acpi_ut_create_internal_object(ACPI_TYPE_LOCAL_REFERENCE);
  106. if (!reference_obj) {
  107. return_ACPI_STATUS(AE_NO_MEMORY);
  108. }
  109. reference_obj->reference.class = ACPI_REFCLASS_REFOF;
  110. reference_obj->reference.object = referenced_obj;
  111. *return_desc = reference_obj;
  112. ACPI_DEBUG_PRINT((ACPI_DB_EXEC,
  113. "Object %p Type [%s], returning Reference %p\n",
  114. obj_desc, acpi_ut_get_object_type_name(obj_desc),
  115. *return_desc));
  116. return_ACPI_STATUS(AE_OK);
  117. }
  118. /*******************************************************************************
  119. *
  120. * FUNCTION: acpi_ex_do_math_op
  121. *
  122. * PARAMETERS: opcode - AML opcode
  123. * integer0 - Integer operand #0
  124. * integer1 - Integer operand #1
  125. *
  126. * RETURN: Integer result of the operation
  127. *
  128. * DESCRIPTION: Execute a math AML opcode. The purpose of having all of the
  129. * math functions here is to prevent a lot of pointer dereferencing
  130. * to obtain the operands.
  131. *
  132. ******************************************************************************/
  133. u64 acpi_ex_do_math_op(u16 opcode, u64 integer0, u64 integer1)
  134. {
  135. ACPI_FUNCTION_ENTRY();
  136. switch (opcode) {
  137. case AML_ADD_OP: /* Add (Integer0, Integer1, Result) */
  138. return (integer0 + integer1);
  139. case AML_BIT_AND_OP: /* And (Integer0, Integer1, Result) */
  140. return (integer0 & integer1);
  141. case AML_BIT_NAND_OP: /* NAnd (Integer0, Integer1, Result) */
  142. return (~(integer0 & integer1));
  143. case AML_BIT_OR_OP: /* Or (Integer0, Integer1, Result) */
  144. return (integer0 | integer1);
  145. case AML_BIT_NOR_OP: /* NOr (Integer0, Integer1, Result) */
  146. return (~(integer0 | integer1));
  147. case AML_BIT_XOR_OP: /* XOr (Integer0, Integer1, Result) */
  148. return (integer0 ^ integer1);
  149. case AML_MULTIPLY_OP: /* Multiply (Integer0, Integer1, Result) */
  150. return (integer0 * integer1);
  151. case AML_SHIFT_LEFT_OP: /* shift_left (Operand, shift_count, Result) */
  152. /*
  153. * We need to check if the shiftcount is larger than the integer bit
  154. * width since the behavior of this is not well-defined in the C language.
  155. */
  156. if (integer1 >= acpi_gbl_integer_bit_width) {
  157. return (0);
  158. }
  159. return (integer0 << integer1);
  160. case AML_SHIFT_RIGHT_OP: /* shift_right (Operand, shift_count, Result) */
  161. /*
  162. * We need to check if the shiftcount is larger than the integer bit
  163. * width since the behavior of this is not well-defined in the C language.
  164. */
  165. if (integer1 >= acpi_gbl_integer_bit_width) {
  166. return (0);
  167. }
  168. return (integer0 >> integer1);
  169. case AML_SUBTRACT_OP: /* Subtract (Integer0, Integer1, Result) */
  170. return (integer0 - integer1);
  171. default:
  172. return (0);
  173. }
  174. }
  175. /*******************************************************************************
  176. *
  177. * FUNCTION: acpi_ex_do_logical_numeric_op
  178. *
  179. * PARAMETERS: opcode - AML opcode
  180. * integer0 - Integer operand #0
  181. * integer1 - Integer operand #1
  182. * logical_result - TRUE/FALSE result of the operation
  183. *
  184. * RETURN: Status
  185. *
  186. * DESCRIPTION: Execute a logical "Numeric" AML opcode. For these Numeric
  187. * operators (LAnd and LOr), both operands must be integers.
  188. *
  189. * Note: cleanest machine code seems to be produced by the code
  190. * below, rather than using statements of the form:
  191. * Result = (Integer0 && Integer1);
  192. *
  193. ******************************************************************************/
  194. acpi_status
  195. acpi_ex_do_logical_numeric_op(u16 opcode,
  196. u64 integer0, u64 integer1, u8 *logical_result)
  197. {
  198. acpi_status status = AE_OK;
  199. u8 local_result = FALSE;
  200. ACPI_FUNCTION_TRACE(ex_do_logical_numeric_op);
  201. switch (opcode) {
  202. case AML_LAND_OP: /* LAnd (Integer0, Integer1) */
  203. if (integer0 && integer1) {
  204. local_result = TRUE;
  205. }
  206. break;
  207. case AML_LOR_OP: /* LOr (Integer0, Integer1) */
  208. if (integer0 || integer1) {
  209. local_result = TRUE;
  210. }
  211. break;
  212. default:
  213. status = AE_AML_INTERNAL;
  214. break;
  215. }
  216. /* Return the logical result and status */
  217. *logical_result = local_result;
  218. return_ACPI_STATUS(status);
  219. }
  220. /*******************************************************************************
  221. *
  222. * FUNCTION: acpi_ex_do_logical_op
  223. *
  224. * PARAMETERS: opcode - AML opcode
  225. * operand0 - operand #0
  226. * operand1 - operand #1
  227. * logical_result - TRUE/FALSE result of the operation
  228. *
  229. * RETURN: Status
  230. *
  231. * DESCRIPTION: Execute a logical AML opcode. The purpose of having all of the
  232. * functions here is to prevent a lot of pointer dereferencing
  233. * to obtain the operands and to simplify the generation of the
  234. * logical value. For the Numeric operators (LAnd and LOr), both
  235. * operands must be integers. For the other logical operators,
  236. * operands can be any combination of Integer/String/Buffer. The
  237. * first operand determines the type to which the second operand
  238. * will be converted.
  239. *
  240. * Note: cleanest machine code seems to be produced by the code
  241. * below, rather than using statements of the form:
  242. * Result = (Operand0 == Operand1);
  243. *
  244. ******************************************************************************/
  245. acpi_status
  246. acpi_ex_do_logical_op(u16 opcode,
  247. union acpi_operand_object *operand0,
  248. union acpi_operand_object *operand1, u8 * logical_result)
  249. {
  250. union acpi_operand_object *local_operand1 = operand1;
  251. u64 integer0;
  252. u64 integer1;
  253. u32 length0;
  254. u32 length1;
  255. acpi_status status = AE_OK;
  256. u8 local_result = FALSE;
  257. int compare;
  258. ACPI_FUNCTION_TRACE(ex_do_logical_op);
  259. /*
  260. * Convert the second operand if necessary. The first operand
  261. * determines the type of the second operand, (See the Data Types
  262. * section of the ACPI 3.0+ specification.) Both object types are
  263. * guaranteed to be either Integer/String/Buffer by the operand
  264. * resolution mechanism.
  265. */
  266. switch (operand0->common.type) {
  267. case ACPI_TYPE_INTEGER:
  268. status = acpi_ex_convert_to_integer(operand1, &local_operand1,
  269. ACPI_STRTOUL_BASE16);
  270. break;
  271. case ACPI_TYPE_STRING:
  272. status =
  273. acpi_ex_convert_to_string(operand1, &local_operand1,
  274. ACPI_IMPLICIT_CONVERT_HEX);
  275. break;
  276. case ACPI_TYPE_BUFFER:
  277. status = acpi_ex_convert_to_buffer(operand1, &local_operand1);
  278. break;
  279. default:
  280. status = AE_AML_INTERNAL;
  281. break;
  282. }
  283. if (ACPI_FAILURE(status)) {
  284. goto cleanup;
  285. }
  286. /*
  287. * Two cases: 1) Both Integers, 2) Both Strings or Buffers
  288. */
  289. if (operand0->common.type == ACPI_TYPE_INTEGER) {
  290. /*
  291. * 1) Both operands are of type integer
  292. * Note: local_operand1 may have changed above
  293. */
  294. integer0 = operand0->integer.value;
  295. integer1 = local_operand1->integer.value;
  296. switch (opcode) {
  297. case AML_LEQUAL_OP: /* LEqual (Operand0, Operand1) */
  298. if (integer0 == integer1) {
  299. local_result = TRUE;
  300. }
  301. break;
  302. case AML_LGREATER_OP: /* LGreater (Operand0, Operand1) */
  303. if (integer0 > integer1) {
  304. local_result = TRUE;
  305. }
  306. break;
  307. case AML_LLESS_OP: /* LLess (Operand0, Operand1) */
  308. if (integer0 < integer1) {
  309. local_result = TRUE;
  310. }
  311. break;
  312. default:
  313. status = AE_AML_INTERNAL;
  314. break;
  315. }
  316. } else {
  317. /*
  318. * 2) Both operands are Strings or both are Buffers
  319. * Note: Code below takes advantage of common Buffer/String
  320. * object fields. local_operand1 may have changed above. Use
  321. * memcmp to handle nulls in buffers.
  322. */
  323. length0 = operand0->buffer.length;
  324. length1 = local_operand1->buffer.length;
  325. /* Lexicographic compare: compare the data bytes */
  326. compare = memcmp(operand0->buffer.pointer,
  327. local_operand1->buffer.pointer,
  328. (length0 > length1) ? length1 : length0);
  329. switch (opcode) {
  330. case AML_LEQUAL_OP: /* LEqual (Operand0, Operand1) */
  331. /* Length and all bytes must be equal */
  332. if ((length0 == length1) && (compare == 0)) {
  333. /* Length and all bytes match ==> TRUE */
  334. local_result = TRUE;
  335. }
  336. break;
  337. case AML_LGREATER_OP: /* LGreater (Operand0, Operand1) */
  338. if (compare > 0) {
  339. local_result = TRUE;
  340. goto cleanup; /* TRUE */
  341. }
  342. if (compare < 0) {
  343. goto cleanup; /* FALSE */
  344. }
  345. /* Bytes match (to shortest length), compare lengths */
  346. if (length0 > length1) {
  347. local_result = TRUE;
  348. }
  349. break;
  350. case AML_LLESS_OP: /* LLess (Operand0, Operand1) */
  351. if (compare > 0) {
  352. goto cleanup; /* FALSE */
  353. }
  354. if (compare < 0) {
  355. local_result = TRUE;
  356. goto cleanup; /* TRUE */
  357. }
  358. /* Bytes match (to shortest length), compare lengths */
  359. if (length0 < length1) {
  360. local_result = TRUE;
  361. }
  362. break;
  363. default:
  364. status = AE_AML_INTERNAL;
  365. break;
  366. }
  367. }
  368. cleanup:
  369. /* New object was created if implicit conversion performed - delete */
  370. if (local_operand1 != operand1) {
  371. acpi_ut_remove_reference(local_operand1);
  372. }
  373. /* Return the logical result and status */
  374. *logical_result = local_result;
  375. return_ACPI_STATUS(status);
  376. }