nseval.c 14 KB

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