exmisc.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
  2. /******************************************************************************
  3. *
  4. * Module Name: exmisc - ACPI AML (p-code) execution - specific opcodes
  5. *
  6. * Copyright (C) 2000 - 2018, Intel Corp.
  7. *
  8. *****************************************************************************/
  9. #include <acpi/acpi.h>
  10. #include "accommon.h"
  11. #include "acinterp.h"
  12. #include "amlcode.h"
  13. #define _COMPONENT ACPI_EXECUTER
  14. ACPI_MODULE_NAME("exmisc")
  15. /*******************************************************************************
  16. *
  17. * FUNCTION: acpi_ex_get_object_reference
  18. *
  19. * PARAMETERS: obj_desc - Create a reference to this object
  20. * return_desc - Where to store the reference
  21. * walk_state - Current state
  22. *
  23. * RETURN: Status
  24. *
  25. * DESCRIPTION: Obtain and return a "reference" to the target object
  26. * Common code for the ref_of_op and the cond_ref_of_op.
  27. *
  28. ******************************************************************************/
  29. acpi_status
  30. acpi_ex_get_object_reference(union acpi_operand_object *obj_desc,
  31. union acpi_operand_object **return_desc,
  32. struct acpi_walk_state *walk_state)
  33. {
  34. union acpi_operand_object *reference_obj;
  35. union acpi_operand_object *referenced_obj;
  36. ACPI_FUNCTION_TRACE_PTR(ex_get_object_reference, obj_desc);
  37. *return_desc = NULL;
  38. switch (ACPI_GET_DESCRIPTOR_TYPE(obj_desc)) {
  39. case ACPI_DESC_TYPE_OPERAND:
  40. if (obj_desc->common.type != ACPI_TYPE_LOCAL_REFERENCE) {
  41. return_ACPI_STATUS(AE_AML_OPERAND_TYPE);
  42. }
  43. /*
  44. * Must be a reference to a Local or Arg
  45. */
  46. switch (obj_desc->reference.class) {
  47. case ACPI_REFCLASS_LOCAL:
  48. case ACPI_REFCLASS_ARG:
  49. case ACPI_REFCLASS_DEBUG:
  50. /* The referenced object is the pseudo-node for the local/arg */
  51. referenced_obj = obj_desc->reference.object;
  52. break;
  53. default:
  54. ACPI_ERROR((AE_INFO, "Invalid Reference Class 0x%2.2X",
  55. obj_desc->reference.class));
  56. return_ACPI_STATUS(AE_AML_OPERAND_TYPE);
  57. }
  58. break;
  59. case ACPI_DESC_TYPE_NAMED:
  60. /*
  61. * A named reference that has already been resolved to a Node
  62. */
  63. referenced_obj = obj_desc;
  64. break;
  65. default:
  66. ACPI_ERROR((AE_INFO, "Invalid descriptor type 0x%X",
  67. ACPI_GET_DESCRIPTOR_TYPE(obj_desc)));
  68. return_ACPI_STATUS(AE_TYPE);
  69. }
  70. /* Create a new reference object */
  71. reference_obj =
  72. acpi_ut_create_internal_object(ACPI_TYPE_LOCAL_REFERENCE);
  73. if (!reference_obj) {
  74. return_ACPI_STATUS(AE_NO_MEMORY);
  75. }
  76. reference_obj->reference.class = ACPI_REFCLASS_REFOF;
  77. reference_obj->reference.object = referenced_obj;
  78. *return_desc = reference_obj;
  79. ACPI_DEBUG_PRINT((ACPI_DB_EXEC,
  80. "Object %p Type [%s], returning Reference %p\n",
  81. obj_desc, acpi_ut_get_object_type_name(obj_desc),
  82. *return_desc));
  83. return_ACPI_STATUS(AE_OK);
  84. }
  85. /*******************************************************************************
  86. *
  87. * FUNCTION: acpi_ex_do_math_op
  88. *
  89. * PARAMETERS: opcode - AML opcode
  90. * integer0 - Integer operand #0
  91. * integer1 - Integer operand #1
  92. *
  93. * RETURN: Integer result of the operation
  94. *
  95. * DESCRIPTION: Execute a math AML opcode. The purpose of having all of the
  96. * math functions here is to prevent a lot of pointer dereferencing
  97. * to obtain the operands.
  98. *
  99. ******************************************************************************/
  100. u64 acpi_ex_do_math_op(u16 opcode, u64 integer0, u64 integer1)
  101. {
  102. ACPI_FUNCTION_ENTRY();
  103. switch (opcode) {
  104. case AML_ADD_OP: /* Add (Integer0, Integer1, Result) */
  105. return (integer0 + integer1);
  106. case AML_BIT_AND_OP: /* And (Integer0, Integer1, Result) */
  107. return (integer0 & integer1);
  108. case AML_BIT_NAND_OP: /* NAnd (Integer0, Integer1, Result) */
  109. return (~(integer0 & integer1));
  110. case AML_BIT_OR_OP: /* Or (Integer0, Integer1, Result) */
  111. return (integer0 | integer1);
  112. case AML_BIT_NOR_OP: /* NOr (Integer0, Integer1, Result) */
  113. return (~(integer0 | integer1));
  114. case AML_BIT_XOR_OP: /* XOr (Integer0, Integer1, Result) */
  115. return (integer0 ^ integer1);
  116. case AML_MULTIPLY_OP: /* Multiply (Integer0, Integer1, Result) */
  117. return (integer0 * integer1);
  118. case AML_SHIFT_LEFT_OP: /* shift_left (Operand, shift_count, Result) */
  119. /*
  120. * We need to check if the shiftcount is larger than the integer bit
  121. * width since the behavior of this is not well-defined in the C language.
  122. */
  123. if (integer1 >= acpi_gbl_integer_bit_width) {
  124. return (0);
  125. }
  126. return (integer0 << integer1);
  127. case AML_SHIFT_RIGHT_OP: /* shift_right (Operand, shift_count, Result) */
  128. /*
  129. * We need to check if the shiftcount is larger than the integer bit
  130. * width since the behavior of this is not well-defined in the C language.
  131. */
  132. if (integer1 >= acpi_gbl_integer_bit_width) {
  133. return (0);
  134. }
  135. return (integer0 >> integer1);
  136. case AML_SUBTRACT_OP: /* Subtract (Integer0, Integer1, Result) */
  137. return (integer0 - integer1);
  138. default:
  139. return (0);
  140. }
  141. }
  142. /*******************************************************************************
  143. *
  144. * FUNCTION: acpi_ex_do_logical_numeric_op
  145. *
  146. * PARAMETERS: opcode - AML opcode
  147. * integer0 - Integer operand #0
  148. * integer1 - Integer operand #1
  149. * logical_result - TRUE/FALSE result of the operation
  150. *
  151. * RETURN: Status
  152. *
  153. * DESCRIPTION: Execute a logical "Numeric" AML opcode. For these Numeric
  154. * operators (LAnd and LOr), both operands must be integers.
  155. *
  156. * Note: cleanest machine code seems to be produced by the code
  157. * below, rather than using statements of the form:
  158. * Result = (Integer0 && Integer1);
  159. *
  160. ******************************************************************************/
  161. acpi_status
  162. acpi_ex_do_logical_numeric_op(u16 opcode,
  163. u64 integer0, u64 integer1, u8 *logical_result)
  164. {
  165. acpi_status status = AE_OK;
  166. u8 local_result = FALSE;
  167. ACPI_FUNCTION_TRACE(ex_do_logical_numeric_op);
  168. switch (opcode) {
  169. case AML_LOGICAL_AND_OP: /* LAnd (Integer0, Integer1) */
  170. if (integer0 && integer1) {
  171. local_result = TRUE;
  172. }
  173. break;
  174. case AML_LOGICAL_OR_OP: /* LOr (Integer0, Integer1) */
  175. if (integer0 || integer1) {
  176. local_result = TRUE;
  177. }
  178. break;
  179. default:
  180. ACPI_ERROR((AE_INFO,
  181. "Invalid numeric logical opcode: %X", opcode));
  182. status = AE_AML_INTERNAL;
  183. break;
  184. }
  185. /* Return the logical result and status */
  186. *logical_result = local_result;
  187. return_ACPI_STATUS(status);
  188. }
  189. /*******************************************************************************
  190. *
  191. * FUNCTION: acpi_ex_do_logical_op
  192. *
  193. * PARAMETERS: opcode - AML opcode
  194. * operand0 - operand #0
  195. * operand1 - operand #1
  196. * logical_result - TRUE/FALSE result of the operation
  197. *
  198. * RETURN: Status
  199. *
  200. * DESCRIPTION: Execute a logical AML opcode. The purpose of having all of the
  201. * functions here is to prevent a lot of pointer dereferencing
  202. * to obtain the operands and to simplify the generation of the
  203. * logical value. For the Numeric operators (LAnd and LOr), both
  204. * operands must be integers. For the other logical operators,
  205. * operands can be any combination of Integer/String/Buffer. The
  206. * first operand determines the type to which the second operand
  207. * will be converted.
  208. *
  209. * Note: cleanest machine code seems to be produced by the code
  210. * below, rather than using statements of the form:
  211. * Result = (Operand0 == Operand1);
  212. *
  213. ******************************************************************************/
  214. acpi_status
  215. acpi_ex_do_logical_op(u16 opcode,
  216. union acpi_operand_object *operand0,
  217. union acpi_operand_object *operand1, u8 * logical_result)
  218. {
  219. union acpi_operand_object *local_operand1 = operand1;
  220. u64 integer0;
  221. u64 integer1;
  222. u32 length0;
  223. u32 length1;
  224. acpi_status status = AE_OK;
  225. u8 local_result = FALSE;
  226. int compare;
  227. ACPI_FUNCTION_TRACE(ex_do_logical_op);
  228. /*
  229. * Convert the second operand if necessary. The first operand
  230. * determines the type of the second operand, (See the Data Types
  231. * section of the ACPI 3.0+ specification.) Both object types are
  232. * guaranteed to be either Integer/String/Buffer by the operand
  233. * resolution mechanism.
  234. */
  235. switch (operand0->common.type) {
  236. case ACPI_TYPE_INTEGER:
  237. status = acpi_ex_convert_to_integer(operand1, &local_operand1,
  238. ACPI_IMPLICIT_CONVERSION);
  239. break;
  240. case ACPI_TYPE_STRING:
  241. status =
  242. acpi_ex_convert_to_string(operand1, &local_operand1,
  243. ACPI_IMPLICIT_CONVERT_HEX);
  244. break;
  245. case ACPI_TYPE_BUFFER:
  246. status = acpi_ex_convert_to_buffer(operand1, &local_operand1);
  247. break;
  248. default:
  249. ACPI_ERROR((AE_INFO,
  250. "Invalid object type for logical operator: %X",
  251. operand0->common.type));
  252. status = AE_AML_INTERNAL;
  253. break;
  254. }
  255. if (ACPI_FAILURE(status)) {
  256. goto cleanup;
  257. }
  258. /*
  259. * Two cases: 1) Both Integers, 2) Both Strings or Buffers
  260. */
  261. if (operand0->common.type == ACPI_TYPE_INTEGER) {
  262. /*
  263. * 1) Both operands are of type integer
  264. * Note: local_operand1 may have changed above
  265. */
  266. integer0 = operand0->integer.value;
  267. integer1 = local_operand1->integer.value;
  268. switch (opcode) {
  269. case AML_LOGICAL_EQUAL_OP: /* LEqual (Operand0, Operand1) */
  270. if (integer0 == integer1) {
  271. local_result = TRUE;
  272. }
  273. break;
  274. case AML_LOGICAL_GREATER_OP: /* LGreater (Operand0, Operand1) */
  275. if (integer0 > integer1) {
  276. local_result = TRUE;
  277. }
  278. break;
  279. case AML_LOGICAL_LESS_OP: /* LLess (Operand0, Operand1) */
  280. if (integer0 < integer1) {
  281. local_result = TRUE;
  282. }
  283. break;
  284. default:
  285. ACPI_ERROR((AE_INFO,
  286. "Invalid comparison opcode: %X", opcode));
  287. status = AE_AML_INTERNAL;
  288. break;
  289. }
  290. } else {
  291. /*
  292. * 2) Both operands are Strings or both are Buffers
  293. * Note: Code below takes advantage of common Buffer/String
  294. * object fields. local_operand1 may have changed above. Use
  295. * memcmp to handle nulls in buffers.
  296. */
  297. length0 = operand0->buffer.length;
  298. length1 = local_operand1->buffer.length;
  299. /* Lexicographic compare: compare the data bytes */
  300. compare = memcmp(operand0->buffer.pointer,
  301. local_operand1->buffer.pointer,
  302. (length0 > length1) ? length1 : length0);
  303. switch (opcode) {
  304. case AML_LOGICAL_EQUAL_OP: /* LEqual (Operand0, Operand1) */
  305. /* Length and all bytes must be equal */
  306. if ((length0 == length1) && (compare == 0)) {
  307. /* Length and all bytes match ==> TRUE */
  308. local_result = TRUE;
  309. }
  310. break;
  311. case AML_LOGICAL_GREATER_OP: /* LGreater (Operand0, Operand1) */
  312. if (compare > 0) {
  313. local_result = TRUE;
  314. goto cleanup; /* TRUE */
  315. }
  316. if (compare < 0) {
  317. goto cleanup; /* FALSE */
  318. }
  319. /* Bytes match (to shortest length), compare lengths */
  320. if (length0 > length1) {
  321. local_result = TRUE;
  322. }
  323. break;
  324. case AML_LOGICAL_LESS_OP: /* LLess (Operand0, Operand1) */
  325. if (compare > 0) {
  326. goto cleanup; /* FALSE */
  327. }
  328. if (compare < 0) {
  329. local_result = TRUE;
  330. goto cleanup; /* TRUE */
  331. }
  332. /* Bytes match (to shortest length), compare lengths */
  333. if (length0 < length1) {
  334. local_result = TRUE;
  335. }
  336. break;
  337. default:
  338. ACPI_ERROR((AE_INFO,
  339. "Invalid comparison opcode: %X", opcode));
  340. status = AE_AML_INTERNAL;
  341. break;
  342. }
  343. }
  344. cleanup:
  345. /* New object was created if implicit conversion performed - delete */
  346. if (local_operand1 != operand1) {
  347. acpi_ut_remove_reference(local_operand1);
  348. }
  349. /* Return the logical result and status */
  350. *logical_result = local_result;
  351. return_ACPI_STATUS(status);
  352. }