exoparg6.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
  2. /******************************************************************************
  3. *
  4. * Module Name: exoparg6 - AML execution - opcodes with 6 arguments
  5. *
  6. * Copyright (C) 2000 - 2022, Intel Corp.
  7. *
  8. *****************************************************************************/
  9. #include <acpi/acpi.h>
  10. #include "accommon.h"
  11. #include "acinterp.h"
  12. #include "acparser.h"
  13. #include "amlcode.h"
  14. #define _COMPONENT ACPI_EXECUTER
  15. ACPI_MODULE_NAME("exoparg6")
  16. /*!
  17. * Naming convention for AML interpreter execution routines.
  18. *
  19. * The routines that begin execution of AML opcodes are named with a common
  20. * convention based upon the number of arguments, the number of target operands,
  21. * and whether or not a value is returned:
  22. *
  23. * AcpiExOpcode_xA_yT_zR
  24. *
  25. * Where:
  26. *
  27. * xA - ARGUMENTS: The number of arguments (input operands) that are
  28. * required for this opcode type (1 through 6 args).
  29. * yT - TARGETS: The number of targets (output operands) that are required
  30. * for this opcode type (0, 1, or 2 targets).
  31. * zR - RETURN VALUE: Indicates whether this opcode type returns a value
  32. * as the function return (0 or 1).
  33. *
  34. * The AcpiExOpcode* functions are called via the Dispatcher component with
  35. * fully resolved operands.
  36. !*/
  37. /* Local prototypes */
  38. static u8
  39. acpi_ex_do_match(u32 match_op,
  40. union acpi_operand_object *package_obj,
  41. union acpi_operand_object *match_obj);
  42. /*******************************************************************************
  43. *
  44. * FUNCTION: acpi_ex_do_match
  45. *
  46. * PARAMETERS: match_op - The AML match operand
  47. * package_obj - Object from the target package
  48. * match_obj - Object to be matched
  49. *
  50. * RETURN: TRUE if the match is successful, FALSE otherwise
  51. *
  52. * DESCRIPTION: Implements the low-level match for the ASL Match operator.
  53. * Package elements will be implicitly converted to the type of
  54. * the match object (Integer/Buffer/String).
  55. *
  56. ******************************************************************************/
  57. static u8
  58. acpi_ex_do_match(u32 match_op,
  59. union acpi_operand_object *package_obj,
  60. union acpi_operand_object *match_obj)
  61. {
  62. u8 logical_result = TRUE;
  63. acpi_status status;
  64. /*
  65. * Note: Since the package_obj/match_obj ordering is opposite to that of
  66. * the standard logical operators, we have to reverse them when we call
  67. * do_logical_op in order to make the implicit conversion rules work
  68. * correctly. However, this means we have to flip the entire equation
  69. * also. A bit ugly perhaps, but overall, better than fussing the
  70. * parameters around at runtime, over and over again.
  71. *
  72. * Below, P[i] refers to the package element, M refers to the Match object.
  73. */
  74. switch (match_op) {
  75. case MATCH_MTR:
  76. /* Always true */
  77. break;
  78. case MATCH_MEQ:
  79. /*
  80. * True if equal: (P[i] == M)
  81. * Change to: (M == P[i])
  82. */
  83. status =
  84. acpi_ex_do_logical_op(AML_LOGICAL_EQUAL_OP, match_obj,
  85. package_obj, &logical_result);
  86. if (ACPI_FAILURE(status)) {
  87. return (FALSE);
  88. }
  89. break;
  90. case MATCH_MLE:
  91. /*
  92. * True if less than or equal: (P[i] <= M) (P[i] not_greater than M)
  93. * Change to: (M >= P[i]) (M not_less than P[i])
  94. */
  95. status =
  96. acpi_ex_do_logical_op(AML_LOGICAL_LESS_OP, match_obj,
  97. package_obj, &logical_result);
  98. if (ACPI_FAILURE(status)) {
  99. return (FALSE);
  100. }
  101. logical_result = (u8) ! logical_result;
  102. break;
  103. case MATCH_MLT:
  104. /*
  105. * True if less than: (P[i] < M)
  106. * Change to: (M > P[i])
  107. */
  108. status =
  109. acpi_ex_do_logical_op(AML_LOGICAL_GREATER_OP, match_obj,
  110. package_obj, &logical_result);
  111. if (ACPI_FAILURE(status)) {
  112. return (FALSE);
  113. }
  114. break;
  115. case MATCH_MGE:
  116. /*
  117. * True if greater than or equal: (P[i] >= M) (P[i] not_less than M)
  118. * Change to: (M <= P[i]) (M not_greater than P[i])
  119. */
  120. status =
  121. acpi_ex_do_logical_op(AML_LOGICAL_GREATER_OP, match_obj,
  122. package_obj, &logical_result);
  123. if (ACPI_FAILURE(status)) {
  124. return (FALSE);
  125. }
  126. logical_result = (u8) ! logical_result;
  127. break;
  128. case MATCH_MGT:
  129. /*
  130. * True if greater than: (P[i] > M)
  131. * Change to: (M < P[i])
  132. */
  133. status =
  134. acpi_ex_do_logical_op(AML_LOGICAL_LESS_OP, match_obj,
  135. package_obj, &logical_result);
  136. if (ACPI_FAILURE(status)) {
  137. return (FALSE);
  138. }
  139. break;
  140. default:
  141. /* Undefined */
  142. return (FALSE);
  143. }
  144. return (logical_result);
  145. }
  146. /*******************************************************************************
  147. *
  148. * FUNCTION: acpi_ex_opcode_6A_0T_1R
  149. *
  150. * PARAMETERS: walk_state - Current walk state
  151. *
  152. * RETURN: Status
  153. *
  154. * DESCRIPTION: Execute opcode with 6 arguments, no target, and a return value
  155. *
  156. ******************************************************************************/
  157. acpi_status acpi_ex_opcode_6A_0T_1R(struct acpi_walk_state *walk_state)
  158. {
  159. union acpi_operand_object **operand = &walk_state->operands[0];
  160. union acpi_operand_object *return_desc = NULL;
  161. acpi_status status = AE_OK;
  162. u64 index;
  163. union acpi_operand_object *this_element;
  164. ACPI_FUNCTION_TRACE_STR(ex_opcode_6A_0T_1R,
  165. acpi_ps_get_opcode_name(walk_state->opcode));
  166. switch (walk_state->opcode) {
  167. case AML_MATCH_OP:
  168. /*
  169. * Match (search_pkg[0], match_op1[1], match_obj1[2],
  170. * match_op2[3], match_obj2[4], start_index[5])
  171. */
  172. /* Validate both Match Term Operators (MTR, MEQ, etc.) */
  173. if ((operand[1]->integer.value > MAX_MATCH_OPERATOR) ||
  174. (operand[3]->integer.value > MAX_MATCH_OPERATOR)) {
  175. ACPI_ERROR((AE_INFO, "Match operator out of range"));
  176. status = AE_AML_OPERAND_VALUE;
  177. goto cleanup;
  178. }
  179. /* Get the package start_index, validate against the package length */
  180. index = operand[5]->integer.value;
  181. if (index >= operand[0]->package.count) {
  182. ACPI_ERROR((AE_INFO,
  183. "Index (0x%8.8X%8.8X) beyond package end (0x%X)",
  184. ACPI_FORMAT_UINT64(index),
  185. operand[0]->package.count));
  186. status = AE_AML_PACKAGE_LIMIT;
  187. goto cleanup;
  188. }
  189. /* Create an integer for the return value */
  190. /* Default return value is ACPI_UINT64_MAX if no match found */
  191. return_desc = acpi_ut_create_integer_object(ACPI_UINT64_MAX);
  192. if (!return_desc) {
  193. status = AE_NO_MEMORY;
  194. goto cleanup;
  195. }
  196. /*
  197. * Examine each element until a match is found. Both match conditions
  198. * must be satisfied for a match to occur. Within the loop,
  199. * "continue" signifies that the current element does not match
  200. * and the next should be examined.
  201. *
  202. * Upon finding a match, the loop will terminate via "break" at
  203. * the bottom. If it terminates "normally", match_value will be
  204. * ACPI_UINT64_MAX (Ones) (its initial value) indicating that no
  205. * match was found.
  206. */
  207. for (; index < operand[0]->package.count; index++) {
  208. /* Get the current package element */
  209. this_element = operand[0]->package.elements[index];
  210. /* Treat any uninitialized (NULL) elements as non-matching */
  211. if (!this_element) {
  212. continue;
  213. }
  214. /*
  215. * Both match conditions must be satisfied. Execution of a continue
  216. * (proceed to next iteration of enclosing for loop) signifies a
  217. * non-match.
  218. */
  219. if (!acpi_ex_do_match((u32) operand[1]->integer.value,
  220. this_element, operand[2])) {
  221. continue;
  222. }
  223. if (!acpi_ex_do_match((u32) operand[3]->integer.value,
  224. this_element, operand[4])) {
  225. continue;
  226. }
  227. /* Match found: Index is the return value */
  228. return_desc->integer.value = index;
  229. break;
  230. }
  231. break;
  232. case AML_LOAD_TABLE_OP:
  233. status = acpi_ex_load_table_op(walk_state, &return_desc);
  234. break;
  235. default:
  236. ACPI_ERROR((AE_INFO, "Unknown AML opcode 0x%X",
  237. walk_state->opcode));
  238. status = AE_AML_BAD_OPCODE;
  239. goto cleanup;
  240. }
  241. cleanup:
  242. /* Delete return object on error */
  243. if (ACPI_FAILURE(status)) {
  244. acpi_ut_remove_reference(return_desc);
  245. }
  246. /* Save return object on success */
  247. else {
  248. walk_state->result_obj = return_desc;
  249. }
  250. return_ACPI_STATUS(status);
  251. }