nseval.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  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. /* Local prototypes */
  15. static void
  16. acpi_ns_exec_module_code(union acpi_operand_object *method_obj,
  17. struct acpi_evaluate_info *info);
  18. /*******************************************************************************
  19. *
  20. * FUNCTION: acpi_ns_evaluate
  21. *
  22. * PARAMETERS: info - Evaluation info block, contains these fields
  23. * and more:
  24. * prefix_node - Prefix or Method/Object Node to execute
  25. * relative_path - Name of method to execute, If NULL, the
  26. * Node is the object to execute
  27. * parameters - List of parameters to pass to the method,
  28. * terminated by NULL. Params itself may be
  29. * NULL if no parameters are being passed.
  30. * parameter_type - Type of Parameter list
  31. * return_object - Where to put method's return value (if
  32. * any). If NULL, no value is returned.
  33. * flags - ACPI_IGNORE_RETURN_VALUE to delete return
  34. *
  35. * RETURN: Status
  36. *
  37. * DESCRIPTION: Execute a control method or return the current value of an
  38. * ACPI namespace object.
  39. *
  40. * MUTEX: Locks interpreter
  41. *
  42. ******************************************************************************/
  43. acpi_status acpi_ns_evaluate(struct acpi_evaluate_info *info)
  44. {
  45. acpi_status status;
  46. ACPI_FUNCTION_TRACE(ns_evaluate);
  47. if (!info) {
  48. return_ACPI_STATUS(AE_BAD_PARAMETER);
  49. }
  50. if (!info->node) {
  51. /*
  52. * Get the actual namespace node for the target object if we
  53. * need to. Handles these cases:
  54. *
  55. * 1) Null node, valid pathname from root (absolute path)
  56. * 2) Node and valid pathname (path relative to Node)
  57. * 3) Node, Null pathname
  58. */
  59. status =
  60. acpi_ns_get_node(info->prefix_node, info->relative_pathname,
  61. ACPI_NS_NO_UPSEARCH, &info->node);
  62. if (ACPI_FAILURE(status)) {
  63. return_ACPI_STATUS(status);
  64. }
  65. }
  66. /*
  67. * For a method alias, we must grab the actual method node so that
  68. * proper scoping context will be established before execution.
  69. */
  70. if (acpi_ns_get_type(info->node) == ACPI_TYPE_LOCAL_METHOD_ALIAS) {
  71. info->node =
  72. ACPI_CAST_PTR(struct acpi_namespace_node,
  73. info->node->object);
  74. }
  75. /* Complete the info block initialization */
  76. info->return_object = NULL;
  77. info->node_flags = info->node->flags;
  78. info->obj_desc = acpi_ns_get_attached_object(info->node);
  79. ACPI_DEBUG_PRINT((ACPI_DB_NAMES, "%s [%p] Value %p\n",
  80. info->relative_pathname, info->node,
  81. acpi_ns_get_attached_object(info->node)));
  82. /* Get info if we have a predefined name (_HID, etc.) */
  83. info->predefined =
  84. acpi_ut_match_predefined_method(info->node->name.ascii);
  85. /* Get the full pathname to the object, for use in warning messages */
  86. info->full_pathname = acpi_ns_get_normalized_pathname(info->node, TRUE);
  87. if (!info->full_pathname) {
  88. return_ACPI_STATUS(AE_NO_MEMORY);
  89. }
  90. /* Count the number of arguments being passed in */
  91. info->param_count = 0;
  92. if (info->parameters) {
  93. while (info->parameters[info->param_count]) {
  94. info->param_count++;
  95. }
  96. /* Warn on impossible argument count */
  97. if (info->param_count > ACPI_METHOD_NUM_ARGS) {
  98. ACPI_WARN_PREDEFINED((AE_INFO, info->full_pathname,
  99. ACPI_WARN_ALWAYS,
  100. "Excess arguments (%u) - using only %u",
  101. info->param_count,
  102. ACPI_METHOD_NUM_ARGS));
  103. info->param_count = ACPI_METHOD_NUM_ARGS;
  104. }
  105. }
  106. /*
  107. * For predefined names: Check that the declared argument count
  108. * matches the ACPI spec -- otherwise this is a BIOS error.
  109. */
  110. acpi_ns_check_acpi_compliance(info->full_pathname, info->node,
  111. info->predefined);
  112. /*
  113. * For all names: Check that the incoming argument count for
  114. * this method/object matches the actual ASL/AML definition.
  115. */
  116. acpi_ns_check_argument_count(info->full_pathname, info->node,
  117. info->param_count, info->predefined);
  118. /* For predefined names: Typecheck all incoming arguments */
  119. acpi_ns_check_argument_types(info);
  120. /*
  121. * Three major evaluation cases:
  122. *
  123. * 1) Object types that cannot be evaluated by definition
  124. * 2) The object is a control method -- execute it
  125. * 3) The object is not a method -- just return it's current value
  126. */
  127. switch (acpi_ns_get_type(info->node)) {
  128. case ACPI_TYPE_ANY:
  129. case ACPI_TYPE_DEVICE:
  130. case ACPI_TYPE_EVENT:
  131. case ACPI_TYPE_MUTEX:
  132. case ACPI_TYPE_REGION:
  133. case ACPI_TYPE_THERMAL:
  134. case ACPI_TYPE_LOCAL_SCOPE:
  135. /*
  136. * 1) Disallow evaluation of these object types. For these,
  137. * object evaluation is undefined.
  138. */
  139. ACPI_ERROR((AE_INFO,
  140. "%s: This object type [%s] "
  141. "never contains data and cannot be evaluated",
  142. info->full_pathname,
  143. acpi_ut_get_type_name(info->node->type)));
  144. status = AE_TYPE;
  145. goto cleanup;
  146. case ACPI_TYPE_METHOD:
  147. /*
  148. * 2) Object is a control method - execute it
  149. */
  150. /* Verify that there is a method object associated with this node */
  151. if (!info->obj_desc) {
  152. ACPI_ERROR((AE_INFO,
  153. "%s: Method has no attached sub-object",
  154. info->full_pathname));
  155. status = AE_NULL_OBJECT;
  156. goto cleanup;
  157. }
  158. ACPI_DEBUG_PRINT((ACPI_DB_EXEC,
  159. "**** Execute method [%s] at AML address %p length %X\n",
  160. info->full_pathname,
  161. info->obj_desc->method.aml_start + 1,
  162. info->obj_desc->method.aml_length - 1));
  163. /*
  164. * Any namespace deletion must acquire both the namespace and
  165. * interpreter locks to ensure that no thread is using the portion of
  166. * the namespace that is being deleted.
  167. *
  168. * Execute the method via the interpreter. The interpreter is locked
  169. * here before calling into the AML parser
  170. */
  171. acpi_ex_enter_interpreter();
  172. status = acpi_ps_execute_method(info);
  173. acpi_ex_exit_interpreter();
  174. break;
  175. default:
  176. /*
  177. * 3) All other non-method objects -- get the current object value
  178. */
  179. /*
  180. * Some objects require additional resolution steps (e.g., the Node
  181. * may be a field that must be read, etc.) -- we can't just grab
  182. * the object out of the node.
  183. *
  184. * Use resolve_node_to_value() to get the associated value.
  185. *
  186. * NOTE: we can get away with passing in NULL for a walk state because
  187. * the Node is guaranteed to not be a reference to either a method
  188. * local or a method argument (because this interface is never called
  189. * from a running method.)
  190. *
  191. * Even though we do not directly invoke the interpreter for object
  192. * resolution, we must lock it because we could access an op_region.
  193. * The op_region access code assumes that the interpreter is locked.
  194. */
  195. acpi_ex_enter_interpreter();
  196. /* TBD: resolve_node_to_value has a strange interface, fix */
  197. info->return_object =
  198. ACPI_CAST_PTR(union acpi_operand_object, info->node);
  199. status =
  200. acpi_ex_resolve_node_to_value(ACPI_CAST_INDIRECT_PTR
  201. (struct acpi_namespace_node,
  202. &info->return_object), NULL);
  203. acpi_ex_exit_interpreter();
  204. if (ACPI_FAILURE(status)) {
  205. info->return_object = NULL;
  206. goto cleanup;
  207. }
  208. ACPI_DEBUG_PRINT((ACPI_DB_NAMES, "Returned object %p [%s]\n",
  209. info->return_object,
  210. acpi_ut_get_object_type_name(info->
  211. return_object)));
  212. status = AE_CTRL_RETURN_VALUE; /* Always has a "return value" */
  213. break;
  214. }
  215. /*
  216. * For predefined names, check the return value against the ACPI
  217. * specification. Some incorrect return value types are repaired.
  218. */
  219. (void)acpi_ns_check_return_value(info->node, info, info->param_count,
  220. status, &info->return_object);
  221. /* Check if there is a return value that must be dealt with */
  222. if (status == AE_CTRL_RETURN_VALUE) {
  223. /* If caller does not want the return value, delete it */
  224. if (info->flags & ACPI_IGNORE_RETURN_VALUE) {
  225. acpi_ut_remove_reference(info->return_object);
  226. info->return_object = NULL;
  227. }
  228. /* Map AE_CTRL_RETURN_VALUE to AE_OK, we are done with it */
  229. status = AE_OK;
  230. } else if (ACPI_FAILURE(status)) {
  231. /* If return_object exists, delete it */
  232. if (info->return_object) {
  233. acpi_ut_remove_reference(info->return_object);
  234. info->return_object = NULL;
  235. }
  236. }
  237. ACPI_DEBUG_PRINT((ACPI_DB_NAMES,
  238. "*** Completed evaluation of object %s ***\n",
  239. info->relative_pathname));
  240. cleanup:
  241. /*
  242. * Namespace was unlocked by the handling acpi_ns* function, so we
  243. * just free the pathname and return
  244. */
  245. ACPI_FREE(info->full_pathname);
  246. info->full_pathname = NULL;
  247. return_ACPI_STATUS(status);
  248. }
  249. /*******************************************************************************
  250. *
  251. * FUNCTION: acpi_ns_exec_module_code_list
  252. *
  253. * PARAMETERS: None
  254. *
  255. * RETURN: None. Exceptions during method execution are ignored, since
  256. * we cannot abort a table load.
  257. *
  258. * DESCRIPTION: Execute all elements of the global module-level code list.
  259. * Each element is executed as a single control method.
  260. *
  261. * NOTE: With this option enabled, each block of detected executable AML
  262. * code that is outside of any control method is wrapped with a temporary
  263. * control method object and placed on a global list. The methods on this
  264. * list are executed below.
  265. *
  266. * This function executes the module-level code for all tables only after
  267. * all of the tables have been loaded. It is a legacy option and is
  268. * not compatible with other ACPI implementations. See acpi_ns_load_table.
  269. *
  270. * This function will be removed when the legacy option is removed.
  271. *
  272. ******************************************************************************/
  273. void acpi_ns_exec_module_code_list(void)
  274. {
  275. union acpi_operand_object *prev;
  276. union acpi_operand_object *next;
  277. struct acpi_evaluate_info *info;
  278. u32 method_count = 0;
  279. ACPI_FUNCTION_TRACE(ns_exec_module_code_list);
  280. /* Exit now if the list is empty */
  281. next = acpi_gbl_module_code_list;
  282. if (!next) {
  283. ACPI_DEBUG_PRINT((ACPI_DB_INIT_NAMES,
  284. "Legacy MLC block list is empty\n"));
  285. return_VOID;
  286. }
  287. /* Allocate the evaluation information block */
  288. info = ACPI_ALLOCATE(sizeof(struct acpi_evaluate_info));
  289. if (!info) {
  290. return_VOID;
  291. }
  292. /* Walk the list, executing each "method" */
  293. while (next) {
  294. prev = next;
  295. next = next->method.mutex;
  296. /* Clear the link field and execute the method */
  297. prev->method.mutex = NULL;
  298. acpi_ns_exec_module_code(prev, info);
  299. method_count++;
  300. /* Delete the (temporary) method object */
  301. acpi_ut_remove_reference(prev);
  302. }
  303. ACPI_INFO(("Executed %u blocks of module-level executable AML code",
  304. method_count));
  305. ACPI_FREE(info);
  306. acpi_gbl_module_code_list = NULL;
  307. return_VOID;
  308. }
  309. /*******************************************************************************
  310. *
  311. * FUNCTION: acpi_ns_exec_module_code
  312. *
  313. * PARAMETERS: method_obj - Object container for the module-level code
  314. * info - Info block for method evaluation
  315. *
  316. * RETURN: None. Exceptions during method execution are ignored, since
  317. * we cannot abort a table load.
  318. *
  319. * DESCRIPTION: Execute a control method containing a block of module-level
  320. * executable AML code. The control method is temporarily
  321. * installed to the root node, then evaluated.
  322. *
  323. ******************************************************************************/
  324. static void
  325. acpi_ns_exec_module_code(union acpi_operand_object *method_obj,
  326. struct acpi_evaluate_info *info)
  327. {
  328. union acpi_operand_object *parent_obj;
  329. struct acpi_namespace_node *parent_node;
  330. acpi_object_type type;
  331. acpi_status status;
  332. ACPI_FUNCTION_TRACE(ns_exec_module_code);
  333. /*
  334. * Get the parent node. We cheat by using the next_object field
  335. * of the method object descriptor.
  336. */
  337. parent_node =
  338. ACPI_CAST_PTR(struct acpi_namespace_node,
  339. method_obj->method.next_object);
  340. type = acpi_ns_get_type(parent_node);
  341. /*
  342. * Get the region handler and save it in the method object. We may need
  343. * this if an operation region declaration causes a _REG method to be run.
  344. *
  345. * We can't do this in acpi_ps_link_module_code because
  346. * acpi_gbl_root_node->Object is NULL at PASS1.
  347. */
  348. if ((type == ACPI_TYPE_DEVICE) && parent_node->object) {
  349. method_obj->method.dispatch.handler =
  350. parent_node->object->device.handler;
  351. }
  352. /* Must clear next_object (acpi_ns_attach_object needs the field) */
  353. method_obj->method.next_object = NULL;
  354. /* Initialize the evaluation information block */
  355. memset(info, 0, sizeof(struct acpi_evaluate_info));
  356. info->prefix_node = parent_node;
  357. /*
  358. * Get the currently attached parent object. Add a reference,
  359. * because the ref count will be decreased when the method object
  360. * is installed to the parent node.
  361. */
  362. parent_obj = acpi_ns_get_attached_object(parent_node);
  363. if (parent_obj) {
  364. acpi_ut_add_reference(parent_obj);
  365. }
  366. /* Install the method (module-level code) in the parent node */
  367. status =
  368. acpi_ns_attach_object(parent_node, method_obj, ACPI_TYPE_METHOD);
  369. if (ACPI_FAILURE(status)) {
  370. goto exit;
  371. }
  372. /* Execute the parent node as a control method */
  373. status = acpi_ns_evaluate(info);
  374. ACPI_DEBUG_PRINT((ACPI_DB_INIT_NAMES,
  375. "Executed module-level code at %p\n",
  376. method_obj->method.aml_start));
  377. /* Delete a possible implicit return value (in slack mode) */
  378. if (info->return_object) {
  379. acpi_ut_remove_reference(info->return_object);
  380. }
  381. /* Detach the temporary method object */
  382. acpi_ns_detach_object(parent_node);
  383. /* Restore the original parent object */
  384. if (parent_obj) {
  385. status = acpi_ns_attach_object(parent_node, parent_obj, type);
  386. } else {
  387. parent_node->type = (u8)type;
  388. }
  389. exit:
  390. if (parent_obj) {
  391. acpi_ut_remove_reference(parent_obj);
  392. }
  393. return_VOID;
  394. }