nseval.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
  2. /*******************************************************************************
  3. *
  4. * Module Name: nseval - Object evaluation, includes control method execution
  5. *
  6. ******************************************************************************/
  7. #include <acpi/acpi.h>
  8. #include "accommon.h"
  9. #include "acparser.h"
  10. #include "acinterp.h"
  11. #include "acnamesp.h"
  12. #define _COMPONENT ACPI_NAMESPACE
  13. ACPI_MODULE_NAME("nseval")
  14. /*******************************************************************************
  15. *
  16. * FUNCTION: acpi_ns_evaluate
  17. *
  18. * PARAMETERS: info - Evaluation info block, contains these fields
  19. * and more:
  20. * prefix_node - Prefix or Method/Object Node to execute
  21. * relative_path - Name of method to execute, If NULL, the
  22. * Node is the object to execute
  23. * parameters - List of parameters to pass to the method,
  24. * terminated by NULL. Params itself may be
  25. * NULL if no parameters are being passed.
  26. * parameter_type - Type of Parameter list
  27. * return_object - Where to put method's return value (if
  28. * any). If NULL, no value is returned.
  29. * flags - ACPI_IGNORE_RETURN_VALUE to delete return
  30. *
  31. * RETURN: Status
  32. *
  33. * DESCRIPTION: Execute a control method or return the current value of an
  34. * ACPI namespace object.
  35. *
  36. * MUTEX: Locks interpreter
  37. *
  38. ******************************************************************************/
  39. acpi_status acpi_ns_evaluate(struct acpi_evaluate_info *info)
  40. {
  41. acpi_status status;
  42. ACPI_FUNCTION_TRACE(ns_evaluate);
  43. if (!info) {
  44. return_ACPI_STATUS(AE_BAD_PARAMETER);
  45. }
  46. if (!info->node) {
  47. /*
  48. * Get the actual namespace node for the target object if we
  49. * need to. Handles these cases:
  50. *
  51. * 1) Null node, valid pathname from root (absolute path)
  52. * 2) Node and valid pathname (path relative to Node)
  53. * 3) Node, Null pathname
  54. */
  55. status =
  56. acpi_ns_get_node(info->prefix_node, info->relative_pathname,
  57. ACPI_NS_NO_UPSEARCH, &info->node);
  58. if (ACPI_FAILURE(status)) {
  59. return_ACPI_STATUS(status);
  60. }
  61. }
  62. /*
  63. * For a method alias, we must grab the actual method node so that
  64. * proper scoping context will be established before execution.
  65. */
  66. if (acpi_ns_get_type(info->node) == ACPI_TYPE_LOCAL_METHOD_ALIAS) {
  67. info->node =
  68. ACPI_CAST_PTR(struct acpi_namespace_node,
  69. info->node->object);
  70. }
  71. /* Complete the info block initialization */
  72. info->return_object = NULL;
  73. info->node_flags = info->node->flags;
  74. info->obj_desc = acpi_ns_get_attached_object(info->node);
  75. ACPI_DEBUG_PRINT((ACPI_DB_NAMES, "%s [%p] Value %p\n",
  76. info->relative_pathname, info->node,
  77. acpi_ns_get_attached_object(info->node)));
  78. /* Get info if we have a predefined name (_HID, etc.) */
  79. info->predefined =
  80. acpi_ut_match_predefined_method(info->node->name.ascii);
  81. /* Get the full pathname to the object, for use in warning messages */
  82. info->full_pathname = acpi_ns_get_normalized_pathname(info->node, TRUE);
  83. if (!info->full_pathname) {
  84. return_ACPI_STATUS(AE_NO_MEMORY);
  85. }
  86. /* Optional object evaluation log */
  87. ACPI_DEBUG_PRINT_RAW((ACPI_DB_EVALUATION,
  88. "%-26s: %s (%s)\n", " Enter evaluation",
  89. &info->full_pathname[1],
  90. acpi_ut_get_type_name(info->node->type)));
  91. /* Count the number of arguments being passed in */
  92. info->param_count = 0;
  93. if (info->parameters) {
  94. while (info->parameters[info->param_count]) {
  95. info->param_count++;
  96. }
  97. /* Warn on impossible argument count */
  98. if (info->param_count > ACPI_METHOD_NUM_ARGS) {
  99. ACPI_WARN_PREDEFINED((AE_INFO, info->full_pathname,
  100. ACPI_WARN_ALWAYS,
  101. "Excess arguments (%u) - using only %u",
  102. info->param_count,
  103. ACPI_METHOD_NUM_ARGS));
  104. info->param_count = ACPI_METHOD_NUM_ARGS;
  105. }
  106. }
  107. /*
  108. * For predefined names: Check that the declared argument count
  109. * matches the ACPI spec -- otherwise this is a BIOS error.
  110. */
  111. acpi_ns_check_acpi_compliance(info->full_pathname, info->node,
  112. info->predefined);
  113. /*
  114. * For all names: Check that the incoming argument count for
  115. * this method/object matches the actual ASL/AML definition.
  116. */
  117. acpi_ns_check_argument_count(info->full_pathname, info->node,
  118. info->param_count, info->predefined);
  119. /* For predefined names: Typecheck all incoming arguments */
  120. acpi_ns_check_argument_types(info);
  121. /*
  122. * Three major evaluation cases:
  123. *
  124. * 1) Object types that cannot be evaluated by definition
  125. * 2) The object is a control method -- execute it
  126. * 3) The object is not a method -- just return it's current value
  127. */
  128. switch (acpi_ns_get_type(info->node)) {
  129. case ACPI_TYPE_ANY:
  130. case ACPI_TYPE_DEVICE:
  131. case ACPI_TYPE_EVENT:
  132. case ACPI_TYPE_MUTEX:
  133. case ACPI_TYPE_REGION:
  134. case ACPI_TYPE_THERMAL:
  135. case ACPI_TYPE_LOCAL_SCOPE:
  136. /*
  137. * 1) Disallow evaluation of these object types. For these,
  138. * object evaluation is undefined.
  139. */
  140. ACPI_ERROR((AE_INFO,
  141. "%s: This object type [%s] "
  142. "never contains data and cannot be evaluated",
  143. info->full_pathname,
  144. acpi_ut_get_type_name(info->node->type)));
  145. status = AE_TYPE;
  146. goto cleanup;
  147. case ACPI_TYPE_METHOD:
  148. /*
  149. * 2) Object is a control method - execute it
  150. */
  151. /* Verify that there is a method object associated with this node */
  152. if (!info->obj_desc) {
  153. ACPI_ERROR((AE_INFO,
  154. "%s: Method has no attached sub-object",
  155. info->full_pathname));
  156. status = AE_NULL_OBJECT;
  157. goto cleanup;
  158. }
  159. ACPI_DEBUG_PRINT((ACPI_DB_EXEC,
  160. "**** Execute method [%s] at AML address %p length %X\n",
  161. info->full_pathname,
  162. info->obj_desc->method.aml_start + 1,
  163. info->obj_desc->method.aml_length - 1));
  164. /*
  165. * Any namespace deletion must acquire both the namespace and
  166. * interpreter locks to ensure that no thread is using the portion of
  167. * the namespace that is being deleted.
  168. *
  169. * Execute the method via the interpreter. The interpreter is locked
  170. * here before calling into the AML parser
  171. */
  172. acpi_ex_enter_interpreter();
  173. status = acpi_ps_execute_method(info);
  174. acpi_ex_exit_interpreter();
  175. break;
  176. default:
  177. /*
  178. * 3) All other non-method objects -- get the current object value
  179. */
  180. /*
  181. * Some objects require additional resolution steps (e.g., the Node
  182. * may be a field that must be read, etc.) -- we can't just grab
  183. * the object out of the node.
  184. *
  185. * Use resolve_node_to_value() to get the associated value.
  186. *
  187. * NOTE: we can get away with passing in NULL for a walk state because
  188. * the Node is guaranteed to not be a reference to either a method
  189. * local or a method argument (because this interface is never called
  190. * from a running method.)
  191. *
  192. * Even though we do not directly invoke the interpreter for object
  193. * resolution, we must lock it because we could access an op_region.
  194. * The op_region access code assumes that the interpreter is locked.
  195. */
  196. acpi_ex_enter_interpreter();
  197. /* TBD: resolve_node_to_value has a strange interface, fix */
  198. info->return_object =
  199. ACPI_CAST_PTR(union acpi_operand_object, info->node);
  200. status =
  201. acpi_ex_resolve_node_to_value(ACPI_CAST_INDIRECT_PTR
  202. (struct acpi_namespace_node,
  203. &info->return_object), NULL);
  204. acpi_ex_exit_interpreter();
  205. if (ACPI_FAILURE(status)) {
  206. info->return_object = NULL;
  207. goto cleanup;
  208. }
  209. ACPI_DEBUG_PRINT((ACPI_DB_NAMES, "Returned object %p [%s]\n",
  210. info->return_object,
  211. acpi_ut_get_object_type_name(info->
  212. return_object)));
  213. status = AE_CTRL_RETURN_VALUE; /* Always has a "return value" */
  214. break;
  215. }
  216. /*
  217. * For predefined names, check the return value against the ACPI
  218. * specification. Some incorrect return value types are repaired.
  219. */
  220. (void)acpi_ns_check_return_value(info->node, info, info->param_count,
  221. status, &info->return_object);
  222. /* Check if there is a return value that must be dealt with */
  223. if (status == AE_CTRL_RETURN_VALUE) {
  224. /* If caller does not want the return value, delete it */
  225. if (info->flags & ACPI_IGNORE_RETURN_VALUE) {
  226. acpi_ut_remove_reference(info->return_object);
  227. info->return_object = NULL;
  228. }
  229. /* Map AE_CTRL_RETURN_VALUE to AE_OK, we are done with it */
  230. status = AE_OK;
  231. } else if (ACPI_FAILURE(status)) {
  232. /* If return_object exists, delete it */
  233. if (info->return_object) {
  234. acpi_ut_remove_reference(info->return_object);
  235. info->return_object = NULL;
  236. }
  237. }
  238. ACPI_DEBUG_PRINT((ACPI_DB_NAMES,
  239. "*** Completed evaluation of object %s ***\n",
  240. info->relative_pathname));
  241. cleanup:
  242. /* Optional object evaluation log */
  243. ACPI_DEBUG_PRINT_RAW((ACPI_DB_EVALUATION,
  244. "%-26s: %s\n", " Exit evaluation",
  245. &info->full_pathname[1]));
  246. /*
  247. * Namespace was unlocked by the handling acpi_ns* function, so we
  248. * just free the pathname and return
  249. */
  250. ACPI_FREE(info->full_pathname);
  251. info->full_pathname = NULL;
  252. return_ACPI_STATUS(status);
  253. }