nseval.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  1. /*******************************************************************************
  2. *
  3. * Module Name: nseval - Object evaluation interfaces -- includes control
  4. * method lookup and execution.
  5. *
  6. ******************************************************************************/
  7. /*
  8. * Copyright (C) 2000 - 2005, R. Byron Moore
  9. * All rights reserved.
  10. *
  11. * Redistribution and use in source and binary forms, with or without
  12. * modification, are permitted provided that the following conditions
  13. * are met:
  14. * 1. Redistributions of source code must retain the above copyright
  15. * notice, this list of conditions, and the following disclaimer,
  16. * without modification.
  17. * 2. Redistributions in binary form must reproduce at minimum a disclaimer
  18. * substantially similar to the "NO WARRANTY" disclaimer below
  19. * ("Disclaimer") and any redistribution must be conditioned upon
  20. * including a substantially similar Disclaimer requirement for further
  21. * binary redistribution.
  22. * 3. Neither the names of the above-listed copyright holders nor the names
  23. * of any contributors may be used to endorse or promote products derived
  24. * from this software without specific prior written permission.
  25. *
  26. * Alternatively, this software may be distributed under the terms of the
  27. * GNU General Public License ("GPL") version 2 as published by the Free
  28. * Software Foundation.
  29. *
  30. * NO WARRANTY
  31. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  32. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  33. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
  34. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  35. * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  36. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  37. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  38. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  39. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  40. * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  41. * POSSIBILITY OF SUCH DAMAGES.
  42. */
  43. #include <acpi/acpi.h>
  44. #include <acpi/acparser.h>
  45. #include <acpi/acinterp.h>
  46. #include <acpi/acnamesp.h>
  47. #define _COMPONENT ACPI_NAMESPACE
  48. ACPI_MODULE_NAME ("nseval")
  49. /* Local prototypes */
  50. static acpi_status
  51. acpi_ns_execute_control_method (
  52. struct acpi_parameter_info *info);
  53. static acpi_status
  54. acpi_ns_get_object_value (
  55. struct acpi_parameter_info *info);
  56. /*******************************************************************************
  57. *
  58. * FUNCTION: acpi_ns_evaluate_relative
  59. *
  60. * PARAMETERS: Pathname - Name of method to execute, If NULL, the
  61. * handle is the object to execute
  62. * Info - Method info block, contains:
  63. * return_object - Where to put method's return value (if
  64. * any). If NULL, no value is returned.
  65. * Params - List of parameters to pass to the method,
  66. * terminated by NULL. Params itself may be
  67. * NULL if no parameters are being passed.
  68. *
  69. * RETURN: Status
  70. *
  71. * DESCRIPTION: Evaluate the object or find and execute the requested method
  72. *
  73. * MUTEX: Locks Namespace
  74. *
  75. ******************************************************************************/
  76. acpi_status
  77. acpi_ns_evaluate_relative (
  78. char *pathname,
  79. struct acpi_parameter_info *info)
  80. {
  81. acpi_status status;
  82. struct acpi_namespace_node *node = NULL;
  83. union acpi_generic_state *scope_info;
  84. char *internal_path = NULL;
  85. ACPI_FUNCTION_TRACE ("ns_evaluate_relative");
  86. /*
  87. * Must have a valid object handle
  88. */
  89. if (!info || !info->node) {
  90. return_ACPI_STATUS (AE_BAD_PARAMETER);
  91. }
  92. /* Build an internal name string for the method */
  93. status = acpi_ns_internalize_name (pathname, &internal_path);
  94. if (ACPI_FAILURE (status)) {
  95. return_ACPI_STATUS (status);
  96. }
  97. scope_info = acpi_ut_create_generic_state ();
  98. if (!scope_info) {
  99. goto cleanup1;
  100. }
  101. /* Get the prefix handle and Node */
  102. status = acpi_ut_acquire_mutex (ACPI_MTX_NAMESPACE);
  103. if (ACPI_FAILURE (status)) {
  104. goto cleanup;
  105. }
  106. info->node = acpi_ns_map_handle_to_node (info->node);
  107. if (!info->node) {
  108. (void) acpi_ut_release_mutex (ACPI_MTX_NAMESPACE);
  109. status = AE_BAD_PARAMETER;
  110. goto cleanup;
  111. }
  112. /* Lookup the name in the namespace */
  113. scope_info->scope.node = info->node;
  114. status = acpi_ns_lookup (scope_info, internal_path, ACPI_TYPE_ANY,
  115. ACPI_IMODE_EXECUTE, ACPI_NS_NO_UPSEARCH, NULL,
  116. &node);
  117. (void) acpi_ut_release_mutex (ACPI_MTX_NAMESPACE);
  118. if (ACPI_FAILURE (status)) {
  119. ACPI_DEBUG_PRINT ((ACPI_DB_NAMES, "Object [%s] not found [%s]\n",
  120. pathname, acpi_format_exception (status)));
  121. goto cleanup;
  122. }
  123. /*
  124. * Now that we have a handle to the object, we can attempt to evaluate it.
  125. */
  126. ACPI_DEBUG_PRINT ((ACPI_DB_NAMES, "%s [%p] Value %p\n",
  127. pathname, node, acpi_ns_get_attached_object (node)));
  128. info->node = node;
  129. status = acpi_ns_evaluate_by_handle (info);
  130. ACPI_DEBUG_PRINT ((ACPI_DB_NAMES, "*** Completed eval of object %s ***\n",
  131. pathname));
  132. cleanup:
  133. acpi_ut_delete_generic_state (scope_info);
  134. cleanup1:
  135. ACPI_MEM_FREE (internal_path);
  136. return_ACPI_STATUS (status);
  137. }
  138. /*******************************************************************************
  139. *
  140. * FUNCTION: acpi_ns_evaluate_by_name
  141. *
  142. * PARAMETERS: Pathname - Fully qualified pathname to the object
  143. * Info - Method info block, contains:
  144. * return_object - Where to put method's return value (if
  145. * any). If NULL, no value is returned.
  146. * Params - List of parameters to pass to the method,
  147. * terminated by NULL. Params itself may be
  148. * NULL if no parameters are being passed.
  149. *
  150. * RETURN: Status
  151. *
  152. * DESCRIPTION: Evaluate the object or rind and execute the requested method
  153. * passing the given parameters
  154. *
  155. * MUTEX: Locks Namespace
  156. *
  157. ******************************************************************************/
  158. acpi_status
  159. acpi_ns_evaluate_by_name (
  160. char *pathname,
  161. struct acpi_parameter_info *info)
  162. {
  163. acpi_status status;
  164. char *internal_path = NULL;
  165. ACPI_FUNCTION_TRACE ("ns_evaluate_by_name");
  166. /* Build an internal name string for the method */
  167. status = acpi_ns_internalize_name (pathname, &internal_path);
  168. if (ACPI_FAILURE (status)) {
  169. return_ACPI_STATUS (status);
  170. }
  171. status = acpi_ut_acquire_mutex (ACPI_MTX_NAMESPACE);
  172. if (ACPI_FAILURE (status)) {
  173. goto cleanup;
  174. }
  175. /* Lookup the name in the namespace */
  176. status = acpi_ns_lookup (NULL, internal_path, ACPI_TYPE_ANY,
  177. ACPI_IMODE_EXECUTE, ACPI_NS_NO_UPSEARCH, NULL,
  178. &info->node);
  179. (void) acpi_ut_release_mutex (ACPI_MTX_NAMESPACE);
  180. if (ACPI_FAILURE (status)) {
  181. ACPI_DEBUG_PRINT ((ACPI_DB_NAMES,
  182. "Object at [%s] was not found, status=%.4X\n",
  183. pathname, status));
  184. goto cleanup;
  185. }
  186. /*
  187. * Now that we have a handle to the object, we can attempt to evaluate it.
  188. */
  189. ACPI_DEBUG_PRINT ((ACPI_DB_NAMES, "%s [%p] Value %p\n",
  190. pathname, info->node, acpi_ns_get_attached_object (info->node)));
  191. status = acpi_ns_evaluate_by_handle (info);
  192. ACPI_DEBUG_PRINT ((ACPI_DB_NAMES, "*** Completed eval of object %s ***\n",
  193. pathname));
  194. cleanup:
  195. /* Cleanup */
  196. if (internal_path) {
  197. ACPI_MEM_FREE (internal_path);
  198. }
  199. return_ACPI_STATUS (status);
  200. }
  201. /*******************************************************************************
  202. *
  203. * FUNCTION: acpi_ns_evaluate_by_handle
  204. *
  205. * PARAMETERS: Info - Method info block, contains:
  206. * Node - Method/Object Node to execute
  207. * Parameters - List of parameters to pass to the method,
  208. * terminated by NULL. Params itself may be
  209. * NULL if no parameters are being passed.
  210. * return_object - Where to put method's return value (if
  211. * any). If NULL, no value is returned.
  212. * parameter_type - Type of Parameter list
  213. * return_object - Where to put method's return value (if
  214. * any). If NULL, no value is returned.
  215. *
  216. * RETURN: Status
  217. *
  218. * DESCRIPTION: Evaluate object or execute the requested method passing the
  219. * given parameters
  220. *
  221. * MUTEX: Locks Namespace
  222. *
  223. ******************************************************************************/
  224. acpi_status
  225. acpi_ns_evaluate_by_handle (
  226. struct acpi_parameter_info *info)
  227. {
  228. acpi_status status;
  229. ACPI_FUNCTION_TRACE ("ns_evaluate_by_handle");
  230. /* Check if namespace has been initialized */
  231. if (!acpi_gbl_root_node) {
  232. return_ACPI_STATUS (AE_NO_NAMESPACE);
  233. }
  234. /* Parameter Validation */
  235. if (!info) {
  236. return_ACPI_STATUS (AE_BAD_PARAMETER);
  237. }
  238. /* Initialize the return value to an invalid object */
  239. info->return_object = NULL;
  240. /* Get the prefix handle and Node */
  241. status = acpi_ut_acquire_mutex (ACPI_MTX_NAMESPACE);
  242. if (ACPI_FAILURE (status)) {
  243. return_ACPI_STATUS (status);
  244. }
  245. info->node = acpi_ns_map_handle_to_node (info->node);
  246. if (!info->node) {
  247. (void) acpi_ut_release_mutex (ACPI_MTX_NAMESPACE);
  248. return_ACPI_STATUS (AE_BAD_PARAMETER);
  249. }
  250. /*
  251. * For a method alias, we must grab the actual method node so that proper
  252. * scoping context will be established before execution.
  253. */
  254. if (acpi_ns_get_type (info->node) == ACPI_TYPE_LOCAL_METHOD_ALIAS) {
  255. info->node = ACPI_CAST_PTR (struct acpi_namespace_node, info->node->object);
  256. }
  257. /*
  258. * Two major cases here:
  259. * 1) The object is an actual control method -- execute it.
  260. * 2) The object is not a method -- just return it's current value
  261. *
  262. * In both cases, the namespace is unlocked by the acpi_ns* procedure
  263. */
  264. if (acpi_ns_get_type (info->node) == ACPI_TYPE_METHOD) {
  265. /*
  266. * Case 1) We have an actual control method to execute
  267. */
  268. status = acpi_ns_execute_control_method (info);
  269. }
  270. else {
  271. /*
  272. * Case 2) Object is NOT a method, just return its current value
  273. */
  274. status = acpi_ns_get_object_value (info);
  275. }
  276. /*
  277. * Check if there is a return value on the stack that must be dealt with
  278. */
  279. if (status == AE_CTRL_RETURN_VALUE) {
  280. /* Map AE_CTRL_RETURN_VALUE to AE_OK, we are done with it */
  281. status = AE_OK;
  282. }
  283. /*
  284. * Namespace was unlocked by the handling acpi_ns* function, so we
  285. * just return
  286. */
  287. return_ACPI_STATUS (status);
  288. }
  289. /*******************************************************************************
  290. *
  291. * FUNCTION: acpi_ns_execute_control_method
  292. *
  293. * PARAMETERS: Info - Method info block, contains:
  294. * Node - Method Node to execute
  295. * Parameters - List of parameters to pass to the method,
  296. * terminated by NULL. Params itself may be
  297. * NULL if no parameters are being passed.
  298. * return_object - Where to put method's return value (if
  299. * any). If NULL, no value is returned.
  300. * parameter_type - Type of Parameter list
  301. * return_object - Where to put method's return value (if
  302. * any). If NULL, no value is returned.
  303. *
  304. * RETURN: Status
  305. *
  306. * DESCRIPTION: Execute the requested method passing the given parameters
  307. *
  308. * MUTEX: Assumes namespace is locked
  309. *
  310. ******************************************************************************/
  311. static acpi_status
  312. acpi_ns_execute_control_method (
  313. struct acpi_parameter_info *info)
  314. {
  315. acpi_status status;
  316. union acpi_operand_object *obj_desc;
  317. ACPI_FUNCTION_TRACE ("ns_execute_control_method");
  318. /* Verify that there is a method associated with this object */
  319. obj_desc = acpi_ns_get_attached_object (info->node);
  320. if (!obj_desc) {
  321. ACPI_DEBUG_PRINT ((ACPI_DB_ERROR, "No attached method object\n"));
  322. (void) acpi_ut_release_mutex (ACPI_MTX_NAMESPACE);
  323. return_ACPI_STATUS (AE_NULL_OBJECT);
  324. }
  325. ACPI_DUMP_PATHNAME (info->node, "Execute Method:",
  326. ACPI_LV_INFO, _COMPONENT);
  327. ACPI_DEBUG_PRINT ((ACPI_DB_EXEC, "Method at AML address %p Length %X\n",
  328. obj_desc->method.aml_start + 1, obj_desc->method.aml_length - 1));
  329. /*
  330. * Unlock the namespace before execution. This allows namespace access
  331. * via the external Acpi* interfaces while a method is being executed.
  332. * However, any namespace deletion must acquire both the namespace and
  333. * interpreter locks to ensure that no thread is using the portion of the
  334. * namespace that is being deleted.
  335. */
  336. status = acpi_ut_release_mutex (ACPI_MTX_NAMESPACE);
  337. if (ACPI_FAILURE (status)) {
  338. return_ACPI_STATUS (status);
  339. }
  340. /*
  341. * Execute the method via the interpreter. The interpreter is locked
  342. * here before calling into the AML parser
  343. */
  344. status = acpi_ex_enter_interpreter ();
  345. if (ACPI_FAILURE (status)) {
  346. return_ACPI_STATUS (status);
  347. }
  348. status = acpi_psx_execute (info);
  349. acpi_ex_exit_interpreter ();
  350. return_ACPI_STATUS (status);
  351. }
  352. /*******************************************************************************
  353. *
  354. * FUNCTION: acpi_ns_get_object_value
  355. *
  356. * PARAMETERS: Info - Method info block, contains:
  357. * Node - Object's NS node
  358. * return_object - Where to put object value (if
  359. * any). If NULL, no value is returned.
  360. *
  361. * RETURN: Status
  362. *
  363. * DESCRIPTION: Return the current value of the object
  364. *
  365. * MUTEX: Assumes namespace is locked, leaves namespace unlocked
  366. *
  367. ******************************************************************************/
  368. static acpi_status
  369. acpi_ns_get_object_value (
  370. struct acpi_parameter_info *info)
  371. {
  372. acpi_status status = AE_OK;
  373. struct acpi_namespace_node *resolved_node = info->node;
  374. ACPI_FUNCTION_TRACE ("ns_get_object_value");
  375. /*
  376. * Objects require additional resolution steps (e.g., the Node may be a
  377. * field that must be read, etc.) -- we can't just grab the object out of
  378. * the node.
  379. */
  380. /*
  381. * Use resolve_node_to_value() to get the associated value. This call always
  382. * deletes obj_desc (allocated above).
  383. *
  384. * NOTE: we can get away with passing in NULL for a walk state because
  385. * obj_desc is guaranteed to not be a reference to either a method local or
  386. * a method argument (because this interface can only be called from the
  387. * acpi_evaluate external interface, never called from a running method.)
  388. *
  389. * Even though we do not directly invoke the interpreter for this, we must
  390. * enter it because we could access an opregion. The opregion access code
  391. * assumes that the interpreter is locked.
  392. *
  393. * We must release the namespace lock before entering the intepreter.
  394. */
  395. status = acpi_ut_release_mutex (ACPI_MTX_NAMESPACE);
  396. if (ACPI_FAILURE (status)) {
  397. return_ACPI_STATUS (status);
  398. }
  399. status = acpi_ex_enter_interpreter ();
  400. if (ACPI_SUCCESS (status)) {
  401. status = acpi_ex_resolve_node_to_value (&resolved_node, NULL);
  402. /*
  403. * If acpi_ex_resolve_node_to_value() succeeded, the return value was placed
  404. * in resolved_node.
  405. */
  406. acpi_ex_exit_interpreter ();
  407. if (ACPI_SUCCESS (status)) {
  408. status = AE_CTRL_RETURN_VALUE;
  409. info->return_object = ACPI_CAST_PTR
  410. (union acpi_operand_object, resolved_node);
  411. ACPI_DEBUG_PRINT ((ACPI_DB_NAMES, "Returning object %p [%s]\n",
  412. info->return_object,
  413. acpi_ut_get_object_type_name (info->return_object)));
  414. }
  415. }
  416. /* Namespace is unlocked */
  417. return_ACPI_STATUS (status);
  418. }